summaryrefslogtreecommitdiff
path: root/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
blob: 8d58a08273a2f70cfca9e73baa3a4db4c6e943f0 (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
#!/bin/bash

source /usr/sbin/kudo-lib.sh

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

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
}

force_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
}

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
}

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 == "graceful_shutdown" ]]; then
  graceful_shutdown
elif [ $2 == "force_reset" ]; then
  force_reset
elif [ $2 == "shutdown_ack" ]; then
  shutdown_ack
else
  echo "Invalid parameter2=$2"
  usage;
fi

exit 0;