#!/bin/bash
# Copyright (c) 2017 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

oniePlatform=${ONIE_PLATFORM:-$(grep -Po '(?<=onie_platform=)[^ ]+' /proc/cmdline)}
platform=${EOS_PLATFORM:-$(xargs -n1 < /proc/cmdline |
                                        sed -ne's/^platform=//p')}

# the slowest known system to powercycle is Wheatland hence the long sleep value
powercycle_wait=10

# use a dedicated script to powercycle the platform if one exists
powercycleScript="/usr/bin/powercycle-$platform"
if [ -x "$powercycleScript" ]; then
   # Execute powercycle script
   if "$powercycleScript"; then
      # Sleep if the execution of the dedicated powercycle was sucessful.
      # Some platforms do not have the ability to powercycle beyond cpu reset.
      # These should not be stuck waiting for nothing.
      sleep $powercycle_wait
   fi
   # This line should not be reached
   echo "Powercycle script $powercycleScript had no effect"
   exit
elif [ ! -z "$oniePlatform" ]; then
   # Whiteboxes are expected to provide a dedicated script for powercycle.
   # Print a message on the console if none is available.
   echo "Missing powercycle script $powercycleScript"
   exit
fi

# product is not a whitebox
case "$platform" in
   blackbird)
      # On PtReyes systems, we need to reboot through toggling a powercycle gpio.
      pchgpio --reboot
      ;;
   grackle)
       # grackle reboots via powercycle gpio as well
      kabinigpio 51 1
      ;;
   rook|sprucefish|sprucefishS|dawson|surfbird)
      # These systems have two scd devices.On these systems cpu/supe cpld is
      # also considered as scd device and powrcycle register cpu/supe cpld,
      # so need to explicitly mention the pci addr of cpu/supe cpld to power cycle.
      scd ff:0b.3 write 0x7000 0x0000DEAD
      ;;
   woodpecker)
      # These systems have two scd devices. On these systems cpu cpld is
      # also considered as scd device and powercycle register is on cpu cpld,
      # so need to explicitly mention the pci addr of cpu cpld to power cycle.
      scd 00:09.0 write 0x7000 0x0000DEAD
      ;;
   belvedere)
      scd write 0x7000 0x0000DEAD
      sleep 1
      # If powercycle via scd does not work fall back to gpio.
      kabinigpio 92 1
      ;;
   lorikeet)
      # lorikeet reboots via powercycle gpio as well
      kabinigpio 4 1
      ;;
   newport)
      # BUG514910 - Remove SCD powercycle method when P1 duts are decommissioned.
      # For P2s and onwards we will rely only on the GPIO.
      scd write 0x7000 0x0000DEAD
      sleep 1
      # If powercycle via scd does not work fall back to gpio.
      kabinigpio 33 1
      ;;
   *)
      # powercycle the system via the scd
      scd write 0x7000 0x0000DEAD

      # the above command does nothing on Bodega if the primary fpga is
      # non functional. As such write to the secondary CPLD's suicide
      # register. Napa boards should not arrive at this code.
      smbus write8 /sb/1/0x23 0x04 0xde
      ;;
esac

# and wait for it to go
sleep $powercycle_wait

# this line should not be reached
echo "Powercycle script had no effect; platform = $platform"
