summaryrefslogtreecommitdiff
path: root/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
blob: d9bcb0cf275d5f481d8c7c57bd3e407eb3681aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash

source /usr/sbin/kudo-lib.sh

# Usage of this utility
function usage() {
  echo "usage: power-util mb [on|off|graceful_shutdown|host_reset|host_cycle|shutdown_ack|hotswap|power_button]";
}

hotswap() {
  kudo.sh rst hotswap
}

force_off() {
  echo "Powering down Server"

  set_gpio_ctrl 203 out 1
  sleep 6
  set_gpio_ctrl 203 out 0
}

power_off() {
  busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
  busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off
}

power_on() {
  echo "Powering on Server"

  set_gpio_ctrl 203 out 1
  sleep 1
  set_gpio_ctrl 203 out 0
  busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
}

power_status() {
  st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d "." -f6)
  if [ "$st" == "On\"" ]; then
  echo "on"
  else
  echo "off"
  fi
}

host_status() {
  BOOT_OK=$(get_gpio_ctrl 194)
  S5_N=$(get_gpio_ctrl 204)
  if [ $S5_N == 1 ] || [ $BOOT_OK == 1 ]; then
    echo "on"
  else
    echo "off"
  fi
}

timestamp() {
  date +"%s" # current time
}

graceful_shutdown() {
  if [ -f "/run/openbmc/host@0-request" ]; then
    echo "Shutdown host immediately"
    power_off
  else
    echo "Triggering graceful shutdown"
    mkdir /run/openbmc
    echo "$(timestamp)" > "/run/openbmc/host@0-shutdown-req-time"
    set_gpio_ctrl 70 out 0
    sleep 3
    set_gpio_ctrl 70 out 1
  fi
}

host_reset() {
  if [ $(host_status) == "on" ]; then
    echo "Triggering sysreset pin"
    busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
    set_gpio_ctrl 65 out 0
    sleep 1
    set_gpio_ctrl 65 out 1
  else
    echo "Host is off, cannot reset."
  fi
}

host_cycle() {
  echo "DC cycling host"
  force_off
  sleep 2
  power_on
}

shutdown_ack() {
  echo "Receive shutdown ACK triggered"
  power_off

  if [ -f "/run/openbmc/host@0-shutdown-req-time" ]; then
    rm -rf "/run/openbmc/host@0-shutdown-req-time"
  fi
}

power_button() {
  echo "Power button trigger event."
  current_time="$(timestamp)"
  if [ -f "/run/openbmc/power-button" ]; then
    echo "Power button released"
    press_time="$(cat /run/openbmc/power-button)"
    if [[ "$current_time" -le "(($press_time + 1))" ]]; then
      power_on
    elif [[ "$current_time" -ge "(($press_time + 5))" ]]; then
      power_off
    else
      echo "Button press did not match interval."
    fi
    rm "/run/openbmc/power-button"
  else
    echo "Power button pressed"
    echo "$(timestamp)" > "/run/openbmc/power-button"
  fi
}

if [ $# -lt 2 ]; then
  echo "Total number of parameter=$#"
  echo "Insufficient parameter"
  usage;
  exit 0;
fi

if [ $1 != "mb" ]; then
  echo "Invalid parameter1=$1"
  usage;
  exit 0;
fi

if [ $2 = "on" ]; then
  sleep 3
  if [ $(power_status) == "off" ]; then
    power_on
  fi
elif [ $2 = "off" ]; then
  if [ $(power_status) == "on" ]; then
    power_off
    sleep 6
    if [ $(host_status) == "on" ]; then
      force_off
    fi
  fi
elif [[ $2 == "hotswap" ]]; then
  hotswap
elif [[ $2 == "graceful_shutdown" ]]; then
  graceful_shutdown
elif [ $2 == "host_reset" ]; then
  host_reset
elif [ $2 == "host_cycle" ]; then
  host_cycle
elif [ $2 == "shutdown_ack" ]; then
  shutdown_ack
elif [ $2 == "power_button" ]; then
  power_button
else
  echo "Invalid parameter2=$2"
  usage;
fi

exit 0;