Skip to content

Commit

Permalink
[Nokia ixs7215] Watchdog timer support (#8377)
Browse files Browse the repository at this point in the history
  • Loading branch information
dflynn-Nokia authored and qiluo-msft committed Aug 12, 2021
1 parent 01e2235 commit 1fc4cb1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python

from sonic_platform.chassis import Chassis


def main():
print("---------------------")
print("Chassis Watchdog Test")
print("---------------------")

chassis = Chassis()

watchdog = chassis.get_watchdog()

print(" Armed: {}".format(watchdog.is_armed()))
print(" Time Left: {}".format(watchdog.get_remaining_time()))

return


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
import array

from sonic_platform_base.watchdog_base import WatchdogBase
from sonic_py_common import logger

""" ioctl constants """
IO_WRITE = 0x40000000
IO_READ = 0x80000000
IO_READ_WRITE = 0xC0000000
IO_SIZE_INT = 0x00040000
IO_TYPE_WATCHDOG = ord('W') << 8

WDR_INT = IO_READ | IO_SIZE_INT | IO_TYPE_WATCHDOG
WDWR_INT = IO_READ_WRITE | IO_SIZE_INT | IO_TYPE_WATCHDOG

""" Watchdog ioctl commands """
WDIOC_SETOPTIONS = 4 | WDR_INT
WDIOC_KEEPALIVE = 5 | WDR_INT
WDIOC_SETTIMEOUT = 6 | WDWR_INT
WDIOC_GETTIMEOUT = 7 | WDR_INT
WDIOC_SETPRETIMEOUT = 8 | WDWR_INT
WDIOC_GETPRETIMEOUT = 9 | WDR_INT
WDIOC_GETTIMELEFT = 10 | WDR_INT

""" Watchdog status constants """
WDIOS_DISABLECARD = 0x0001
Expand All @@ -33,8 +39,6 @@

WD_COMMON_ERROR = -1

sonic_logger = logger.Logger()


class WatchdogImplBase(WatchdogBase):
"""
Expand All @@ -47,6 +51,7 @@ def __init__(self, wd_device_path):
Open a watchdog handle
@param wd_device_path Path to watchdog device
"""
super(WatchdogImplBase, self).__init__()

self.watchdog_path = wd_device_path
self.watchdog = os.open(self.watchdog_path, os.O_WRONLY)
Expand All @@ -57,32 +62,40 @@ def __init__(self, wd_device_path):
self.armed = False
self.timeout = self._gettimeout()

def disarm(self):
def _disablewatchdog(self):
"""
Turn off the watchdog timer
"""
Disarm the hardware watchdog

Returns:
A boolean, True if watchdog is disarmed successfully, False
if not
req = array.array('h', [WDIOS_DISABLECARD])
fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False)

def _enablewatchdog(self):
"""
Turn on the watchdog timer
"""
sonic_logger.log_info(" Debug disarm watchdog ")

try:
self._disablewatchdog()
self.armed = False
self.timeout = 0
except IOError:
return False
req = array.array('h', [WDIOS_ENABLECARD])
fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False)

return True
def _keepalive(self):
"""
Keep alive watchdog timer
"""

def _disablewatchdog(self):
fcntl.ioctl(self.watchdog, WDIOC_KEEPALIVE)

def _settimeout(self, seconds):
"""
Turn off the watchdog timer
Set watchdog timer timeout
@param seconds - timeout in seconds
@return is the actual set timeout
"""

req = array.array('h', [WDIOS_DISABLECARD])
fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False)
req = array.array('I', [seconds])
fcntl.ioctl(self.watchdog, WDIOC_SETTIMEOUT, req, True)

return int(req[0])

def _gettimeout(self):
"""
Expand All @@ -95,11 +108,22 @@ def _gettimeout(self):

return int(req[0])

def _gettimeleft(self):
"""
Get time left before watchdog timer expires
@return time left in seconds
"""

req = array.array('I', [0])
fcntl.ioctl(self.watchdog, WDIOC_GETTIMELEFT, req, True)

return int(req[0])

def arm(self, seconds):
"""
Implements arm WatchdogBase API
Arm the hardware watchdog
"""
sonic_logger.log_info(" Debug arm watchdog4 ")

ret = WD_COMMON_ERROR
if seconds < 0:
return ret
Expand All @@ -110,7 +134,6 @@ def arm(self, seconds):
if self.armed:
self._keepalive()
else:
sonic_logger.log_info(" Debug arm watchdog5 ")
self._enablewatchdog()
self.armed = True
ret = self.timeout
Expand All @@ -119,17 +142,42 @@ def arm(self, seconds):

return ret

def _enablewatchdog(self):
def disarm(self):
"""
Turn on the watchdog timer
Disarm the hardware watchdog
Returns:
A boolean, True if watchdog is disarmed successfully, False
if not
"""

req = array.array('h', [WDIOS_ENABLECARD])
fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False)
try:
self._disablewatchdog()
self.armed = False
self.timeout = 0
except IOError:
return False

def _keepalive(self):
return True

def is_armed(self):
"""
Keep alive watchdog timer
Implements is_armed WatchdogBase API
"""

fcntl.ioctl(self.watchdog, WDIOC_KEEPALIVE)
return self.armed

def get_remaining_time(self):
"""
Implements get_remaining_time WatchdogBase API
"""

timeleft = WD_COMMON_ERROR

if self.armed:
try:
timeleft = self._gettimeleft()
except IOError:
pass

return timeleft

0 comments on commit 1fc4cb1

Please sign in to comment.