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# micctrl - Controls MIC boot/start/stop.
21#
22# chkconfig: 2345 95 05
23# description: start MPSS stack processing.
24#
25### BEGIN INIT INFO
26# Provides: micctrl
27### END INIT INFO
28
29# Source function library.
30. /etc/init.d/functions
31
32sysfs="/sys/class/mic"
33
34_status()
35{
36	f=$sysfs/$1
37	echo -e $1 state: "`cat $f/state`" shutdown_status: "`cat $f/shutdown_status`"
38}
39
40status()
41{
42	if [ "`echo $1 | head -c3`" == "mic" ]; then
43		_status $1
44		return $?
45	fi
46	for f in $sysfs/*
47	do
48		_status `basename $f`
49		RETVAL=$?
50		[ $RETVAL -ne 0 ] && return $RETVAL
51	done
52	return 0
53}
54
55_reset()
56{
57	f=$sysfs/$1
58	echo reset > $f/state
59}
60
61reset()
62{
63	if [ "`echo $1 | head -c3`" == "mic" ]; then
64		_reset $1
65		return $?
66	fi
67	for f in $sysfs/*
68	do
69		_reset `basename $f`
70		RETVAL=$?
71		[ $RETVAL -ne 0 ] && return $RETVAL
72	done
73	return 0
74}
75
76_boot()
77{
78	f=$sysfs/$1
79	echo "linux" > $f/bootmode
80	echo "mic/uos.img" > $f/firmware
81	echo "mic/$1.image" > $f/ramdisk
82	echo "boot" > $f/state
83}
84
85boot()
86{
87	if [ "`echo $1 | head -c3`" == "mic" ]; then
88		_boot $1
89		return $?
90	fi
91	for f in $sysfs/*
92	do
93		_boot `basename $f`
94		RETVAL=$?
95		[ $RETVAL -ne 0 ] && return $RETVAL
96	done
97	return 0
98}
99
100_shutdown()
101{
102	f=$sysfs/$1
103	echo shutdown > $f/state
104}
105
106shutdown()
107{
108	if [ "`echo $1 | head -c3`" == "mic" ]; then
109		_shutdown $1
110		return $?
111	fi
112	for f in $sysfs/*
113	do
114		_shutdown `basename $f`
115		RETVAL=$?
116		[ $RETVAL -ne 0 ] && return $RETVAL
117	done
118	return 0
119}
120
121_wait()
122{
123	f=$sysfs/$1
124	while [ "`cat $f/state`" != "offline" -a "`cat $f/state`" != "online" ]
125	do
126		sleep 1
127		echo -e "Waiting for $1 to go offline"
128	done
129}
130
131wait()
132{
133	if [ "`echo $1 | head -c3`" == "mic" ]; then
134		_wait $1
135		return $?
136	fi
137	# Wait for the cards to go offline
138	for f in $sysfs/*
139	do
140		_wait `basename $f`
141		RETVAL=$?
142		[ $RETVAL -ne 0 ] && return $RETVAL
143	done
144	return 0
145}
146
147if [ ! -d "$sysfs" ]; then
148	echo -e $"Module unloaded "
149	exit 3
150fi
151
152case $1 in
153	-s)
154		status $2
155		;;
156	-r)
157		reset $2
158		;;
159	-b)
160		boot $2
161		;;
162	-S)
163		shutdown $2
164		;;
165	-w)
166		wait $2
167		;;
168	*)
169		echo $"Usage: $0 {-s (status) |-r (reset) |-b (boot) |-S (shutdown) |-w (wait)}"
170		exit 2
171esac
172
173exit $?
174