1#!/bin/bash
2# Intel MIC Platform Software Stack (MPSS)
3#
4# Copyright(c) 2013 Intel Corporation.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License, version 2, as
8# published by the Free Software Foundation.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# The full GNU General Public License is included in this distribution in
16# the file called "COPYING".
17#
18# Intel MIC User Space Tools.
19#
20# mpss	Start mpssd.
21#
22# chkconfig: 2345 95 05
23# description: start MPSS stack processing.
24#
25### BEGIN INIT INFO
26# Provides: mpss
27# Required-Start:
28# Required-Stop:
29# Short-Description: MPSS stack control
30# Description: MPSS stack control
31### END INIT INFO
32
33# Source function library.
34. /etc/init.d/functions
35
36exec=/usr/sbin/mpssd
37sysfs="/sys/class/mic"
38
39start()
40{
41	[ -x $exec ] || exit 5
42
43	if [ "`ps -e | awk '{print $4}' | grep mpssd | head -1`" = "mpssd" ]; then
44		echo -e $"MPSSD already running! "
45		success
46		echo
47		return 0
48	fi
49
50	echo -e $"Starting MPSS Stack"
51	echo -e $"Loading MIC_X100_DMA & MIC_HOST Modules"
52
53	for f in "mic_host" "mic_x100_dma"
54	do
55		modprobe $f
56		RETVAL=$?
57		if [ $RETVAL -ne 0 ]; then
58			failure
59			echo
60			return $RETVAL
61		fi
62	done
63
64	# Start the daemon
65	echo -n $"Starting MPSSD "
66	$exec
67	RETVAL=$?
68	if [ $RETVAL -ne 0 ]; then
69		failure
70		echo
71		return $RETVAL
72	fi
73	success
74	echo
75
76	sleep 5
77
78	# Boot the cards
79	micctrl -b
80
81	# Wait till ping works
82	for f in $sysfs/*
83	do
84		count=100
85		ipaddr=`cat $f/cmdline`
86		ipaddr=${ipaddr#*address,}
87		ipaddr=`echo $ipaddr | cut -d, -f1 | cut -d\; -f1`
88		while [ $count -ge 0 ]
89		do
90			echo -e "Pinging "`basename $f`" "
91			ping -c 1 $ipaddr &> /dev/null
92			RETVAL=$?
93			if [ $RETVAL -eq 0 ]; then
94				success
95				break
96			fi
97			sleep 1
98			count=`expr $count - 1`
99		done
100		[ $RETVAL -ne 0 ] && failure || success
101		echo
102	done
103	return $RETVAL
104}
105
106stop()
107{
108	echo -e $"Shutting down MPSS Stack: "
109
110	# Bail out if module is unloaded
111	if [ ! -d "$sysfs" ]; then
112		echo -n $"Module unloaded "
113		success
114		echo
115		return 0
116	fi
117
118	# Shut down the cards.
119	micctrl -S
120
121	# Wait for the cards to go offline
122	for f in $sysfs/*
123	do
124		while [ "`cat $f/state`" != "offline" ]
125		do
126			sleep 1
127			echo -e "Waiting for "`basename $f`" to go offline"
128		done
129	done
130
131	# Display the status of the cards
132	micctrl -s
133
134	# Kill MPSSD now
135	echo -n $"Killing MPSSD"
136	killall -9 mpssd 2>/dev/null
137	RETVAL=$?
138	[ $RETVAL -ne 0 ] && failure || success
139	echo
140	return $RETVAL
141}
142
143restart()
144{
145	stop
146	sleep 5
147	start
148}
149
150status()
151{
152	micctrl -s
153	if [ "`ps -e | awk '{print $4}' | grep mpssd | head -n 1`" = "mpssd" ]; then
154		echo "mpssd is running"
155	else
156		echo "mpssd is stopped"
157	fi
158	return 0
159}
160
161unload()
162{
163	if [ ! -d "$sysfs" ]; then
164		echo -n $"No MIC_HOST Module: "
165		success
166		echo
167		return
168	fi
169
170	stop
171
172	sleep 5
173	echo -n $"Removing MIC_HOST & MIC_X100_DMA Modules: "
174	modprobe -r mic_host mic_x100_dma
175	RETVAL=$?
176	[ $RETVAL -ne 0 ] && failure || success
177	echo
178	return $RETVAL
179}
180
181case $1 in
182	start)
183		start
184		;;
185	stop)
186		stop
187		;;
188	restart)
189		restart
190		;;
191	status)
192		status
193		;;
194	unload)
195		unload
196		;;
197	*)
198		echo $"Usage: $0 {start|stop|restart|status|unload}"
199		exit 2
200esac
201
202exit $?
203