blob: a2261fb8174274bbf8e88a94e82e12fca0678a9f [file] [log] [blame]
Vijay Khemka45269ba2018-12-13 11:07:06 -08001#!/bin/bash
2# Usage of this utility
3function usage() {
Amithash Prasad645ef292019-03-08 14:24:26 -08004 echo "usage: power-util mb [on|off|status|cycle|reset]";
5 echo " power-util sled-cycle"
Vijay Khemka45269ba2018-12-13 11:07:06 -08006}
7
8GPIO_BASE=$(cat /sys/class/gpio/gpio*/base)
9PWR_GPIO=$(($GPIO_BASE + 32 + 3))
10BMC_RDY_GPIO=$(($GPIO_BASE + 144 +1))
Amithash Prasad645ef292019-03-08 14:24:26 -080011PWR_GOOD=$(($GPIO_BASE + 8 + 6))
12PWR_RESET=$(($GPIO_BASE + 33))
Vijay Khemka45269ba2018-12-13 11:07:06 -080013
Amithash Prasad645ef292019-03-08 14:24:26 -080014power_off() {
15 echo "Shutting down Server $2"
16 echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
17 sleep 1
18 echo 0 > /sys/class/gpio/gpio${PWR_GPIO}/value
19 sleep 6
20 echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
21}
22
23power_on() {
24 echo "Powering on Server $2"
25 echo 0 > /sys/class/gpio/gpio${BMC_RDY_GPIO}/value
26 echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
27 echo 0 > /sys/class/gpio/gpio${PWR_GPIO}/value
28 sleep 1
29 echo 1 > /sys/class/gpio/gpio${PWR_GPIO}/value
30}
31
32power_status() {
33 st=$(cat /sys/class/gpio/gpio${PWR_GOOD}/value)
34 if [ "$st" == "0" ]; then
35 echo "off"
36 else
37 echo "on"
38 fi
39}
40
41power_reset() {
42 echo "Reset on server $2"
43 echo 0 > /sys/class/gpio/gpio${PWR_RESET}/value
44 sleep 0.1
45 echo 1 > /sys/class/gpio/gpio${PWR_RESET}/value
46}
47
48sled_cycle() {
49 i2cset -y 7 0x45 0xd9 c
50}
51
52if [ $# -lt 2 ]; then
Vijay Khemka45269ba2018-12-13 11:07:06 -080053 echo "Total number of parameter=$#"
54 echo "Insufficient parameter"
55 usage;
56 exit 0;
57fi
58
Amithash Prasad645ef292019-03-08 14:24:26 -080059if [ $1 == "sled-cycle" ]; then
60 sled_cycle
61fi
62
Vijay Khemka45269ba2018-12-13 11:07:06 -080063if [ $1 != "mb" ]; then
64 echo "Invalid parameter1=$1"
65 usage;
66 exit 0;
67fi
68
Amithash Prasad645ef292019-03-08 14:24:26 -080069if [ $2 = "on" ]; then
70 if [ $(power_status) == "off" ]; then
71 power_on
72 fi
73elif [ $2 = "off" ]; then
74 if [ $(power_status) == "on" ]; then
75 power_off
76 fi
77elif [ $2 == "cycle" ]; then
78 if [ $(power_status) == "on" ]; then
79 power_off
80 else
81 echo "WARNING: Powering on server"
82 fi
83 power_on
84elif [ $2 == "reset" ]; then
85 if [ $(power_status) == "on" ]; then
86 power_reset
87 else
88 echo "ERROR: Server not powered on"
89 fi
90elif [ $2 == "status" ]; then
91 power_status
Vijay Khemka45269ba2018-12-13 11:07:06 -080092else
Amithash Prasad645ef292019-03-08 14:24:26 -080093 echo "Invalid parameter2=$2"
Vijay Khemka45269ba2018-12-13 11:07:06 -080094 usage;
95fi
96
97exit 0;