|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Mellanox |
| 5 | +
|
| 6 | +Module contains an implementation of SONiC Platform Base API and |
| 7 | +provides access to hardware watchdog on Mellanox platforms |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import fcntl |
| 12 | +import array |
| 13 | +import time |
| 14 | + |
| 15 | +from sonic_platform_base.watchdog_base import WatchdogBase |
| 16 | + |
| 17 | +""" ioctl constants """ |
| 18 | +IO_WRITE = 0x40000000 |
| 19 | +IO_READ = 0x80000000 |
| 20 | +IO_READ_WRITE = 0xC0000000 |
| 21 | +IO_SIZE_INT = 0x00040000 |
| 22 | +IO_SIZE_40 = 0x00280000 |
| 23 | +IO_TYPE_WATCHDOG = ord('W') << 8 |
| 24 | + |
| 25 | +WDR_INT = IO_READ | IO_SIZE_INT | IO_TYPE_WATCHDOG |
| 26 | +WDR_40 = IO_READ | IO_SIZE_40 | IO_TYPE_WATCHDOG |
| 27 | +WDWR_INT = IO_READ_WRITE | IO_SIZE_INT | IO_TYPE_WATCHDOG |
| 28 | + |
| 29 | +""" Watchdog ioctl commands """ |
| 30 | +WDIOC_GETSUPPORT = 0 | WDR_40 |
| 31 | +WDIOC_GETSTATUS = 1 | WDR_INT |
| 32 | +WDIOC_GETBOOTSTATUS = 2 | WDR_INT |
| 33 | +WDIOC_GETTEMP = 3 | WDR_INT |
| 34 | +WDIOC_SETOPTIONS = 4 | WDR_INT |
| 35 | +WDIOC_KEEPALIVE = 5 | WDR_INT |
| 36 | +WDIOC_SETTIMEOUT = 6 | WDWR_INT |
| 37 | +WDIOC_GETTIMEOUT = 7 | WDR_INT |
| 38 | +WDIOC_SETPRETIMEOUT = 8 | WDWR_INT |
| 39 | +WDIOC_GETPRETIMEOUT = 9 | WDR_INT |
| 40 | +WDIOC_GETTIMELEFT = 10 | WDR_INT |
| 41 | + |
| 42 | +""" Watchdog status constants """ |
| 43 | +WDIOS_DISABLECARD = 0x0001 |
| 44 | +WDIOS_ENABLECARD = 0x0002 |
| 45 | + |
| 46 | +""" Mellanox main watchdog identity string """ |
| 47 | +WD_MLNX_MAIN_IDENTITY = "mlx-wdt-main" |
| 48 | +""" watchdog sysfs """ |
| 49 | +WD_SYSFS_PATH = "/sys/class/watchdog/" |
| 50 | + |
| 51 | + |
| 52 | +WD_COMMON_ERROR = -1 |
| 53 | + |
| 54 | + |
| 55 | +class WatchdogImplBase(WatchdogBase): |
| 56 | + """ |
| 57 | + Base class that implements common logic for interacting |
| 58 | + with watchdog using ioctl commands |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, wd_device_path): |
| 62 | + """ |
| 63 | + Open a watchdog handle |
| 64 | + @param wd_device_path Path to watchdog device |
| 65 | + """ |
| 66 | + |
| 67 | + self.watchdog_path = wd_device_path |
| 68 | + self.watchdog = os.open(self.watchdog_path, os.O_WRONLY) |
| 69 | + |
| 70 | + # Opening a watchdog descriptor starts |
| 71 | + # watchdog timer; |
| 72 | + # by default it should be stopped |
| 73 | + self._disablecard() |
| 74 | + self.armed = False |
| 75 | + |
| 76 | + self.timeout = self._gettimeout() |
| 77 | + |
| 78 | + def _enablecard(self): |
| 79 | + """ |
| 80 | + Turn on the watchdog timer |
| 81 | + """ |
| 82 | + |
| 83 | + req = array.array('h', [WDIOS_ENABLECARD]) |
| 84 | + fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False) |
| 85 | + |
| 86 | + def _disablecard(self): |
| 87 | + """ |
| 88 | + Turn off the watchdog timer |
| 89 | + """ |
| 90 | + |
| 91 | + req = array.array('h', [WDIOS_DISABLECARD]) |
| 92 | + fcntl.ioctl(self.watchdog, WDIOC_SETOPTIONS, req, False) |
| 93 | + |
| 94 | + def _keepalive(self): |
| 95 | + """ |
| 96 | + Keep alive watchdog timer |
| 97 | + """ |
| 98 | + |
| 99 | + fcntl.ioctl(self.watchdog, WDIOC_KEEPALIVE) |
| 100 | + |
| 101 | + def _settimeout(self, seconds): |
| 102 | + """ |
| 103 | + Set watchdog timer timeout |
| 104 | + @param seconds - timeout in seconds |
| 105 | + @return is the actual set timeout |
| 106 | + """ |
| 107 | + |
| 108 | + req = array.array('I', [seconds]) |
| 109 | + fcntl.ioctl(self.watchdog, WDIOC_SETTIMEOUT, req, True) |
| 110 | + |
| 111 | + return int(req[0]) |
| 112 | + |
| 113 | + def _gettimeout(self): |
| 114 | + """ |
| 115 | + Get watchdog timeout |
| 116 | + @return watchdog timeout |
| 117 | + """ |
| 118 | + |
| 119 | + req = array.array('I', [0]) |
| 120 | + fcntl.ioctl(self.watchdog, WDIOC_GETTIMEOUT, req, True) |
| 121 | + |
| 122 | + return int(req[0]) |
| 123 | + |
| 124 | + def _gettimeleft(self): |
| 125 | + """ |
| 126 | + Get time left before watchdog timer expires |
| 127 | + @return time left in seconds |
| 128 | + """ |
| 129 | + |
| 130 | + req = array.array('I', [0]) |
| 131 | + fcntl.ioctl(self.watchdog, WDIOC_GETTIMELEFT, req, True) |
| 132 | + |
| 133 | + return int(req[0]) |
| 134 | + |
| 135 | + def arm(self, seconds): |
| 136 | + """ |
| 137 | + Implements arm WatchdogBase API |
| 138 | + """ |
| 139 | + |
| 140 | + ret = WD_COMMON_ERROR |
| 141 | + if seconds < 0: |
| 142 | + return ret |
| 143 | + |
| 144 | + try: |
| 145 | + if self.timeout != seconds: |
| 146 | + self.timeout = self._settimeout(seconds) |
| 147 | + if self.armed: |
| 148 | + self._keepalive() |
| 149 | + else: |
| 150 | + self._enablecard() |
| 151 | + self.armed = True |
| 152 | + ret = self.timeout |
| 153 | + except IOError: |
| 154 | + pass |
| 155 | + |
| 156 | + return ret |
| 157 | + |
| 158 | + def disarm(self): |
| 159 | + """ |
| 160 | + Implements disarm WatchdogBase API |
| 161 | + """ |
| 162 | + |
| 163 | + disarmed = False |
| 164 | + if self.armed: |
| 165 | + try: |
| 166 | + self._disablecard() |
| 167 | + self.armed = False |
| 168 | + disarmed = True |
| 169 | + except IOError: |
| 170 | + pass |
| 171 | + |
| 172 | + return disarmed |
| 173 | + |
| 174 | + def is_armed(self): |
| 175 | + """ |
| 176 | + Implements is_armed WatchdogBase API |
| 177 | + """ |
| 178 | + |
| 179 | + return self.armed |
| 180 | + |
| 181 | + def get_remaining_time(self): |
| 182 | + """ |
| 183 | + Implements get_remaining_time WatchdogBase API |
| 184 | + """ |
| 185 | + |
| 186 | + timeleft = WD_COMMON_ERROR |
| 187 | + |
| 188 | + if self.armed: |
| 189 | + try: |
| 190 | + timeleft = self._gettimeleft() |
| 191 | + except IOError: |
| 192 | + pass |
| 193 | + |
| 194 | + return timeleft |
| 195 | + |
| 196 | + def __del__(self): |
| 197 | + """ |
| 198 | + Close watchdog |
| 199 | + """ |
| 200 | + |
| 201 | + os.close(self.watchdog) |
| 202 | + |
| 203 | + |
| 204 | +class WatchdogType1(WatchdogImplBase): |
| 205 | + """ |
| 206 | + Watchdog type 1 |
| 207 | + """ |
| 208 | + |
| 209 | + def arm(self, seconds): |
| 210 | + """ |
| 211 | + Call arm from WatchdgoImplBase and save the timestamp |
| 212 | + when the watchdog was armed |
| 213 | + """ |
| 214 | + |
| 215 | + ret = WatchdogImplBase.arm(self, seconds) |
| 216 | + # Save the watchdog arm timestamp |
| 217 | + # requiered for get_remaining_time() |
| 218 | + self.arm_timestamp = time.time() |
| 219 | + |
| 220 | + return ret |
| 221 | + |
| 222 | + def get_remaining_time(self): |
| 223 | + """ |
| 224 | + Watchdog Type 1 does not support timeleft |
| 225 | + operation, we will calculate timeleft based |
| 226 | + on timeout and arm timestamp |
| 227 | + """ |
| 228 | + |
| 229 | + timeleft = WD_COMMON_ERROR |
| 230 | + |
| 231 | + if self.armed: |
| 232 | + timeleft = int(self.timeout - (time.time() - self.arm_timestamp)) |
| 233 | + |
| 234 | + return timeleft |
| 235 | + |
| 236 | +class WatchdogType2(WatchdogImplBase): |
| 237 | + """ |
| 238 | + Watchdog type 2 |
| 239 | + """ |
| 240 | + |
| 241 | + pass |
| 242 | + |
| 243 | + |
| 244 | +def is_mlnx_wd_main(dev): |
| 245 | + """ |
| 246 | + Checks if dev is Mellanox main watchdog |
| 247 | + """ |
| 248 | + |
| 249 | + try: |
| 250 | + with open("{}/{}/identity".format(WD_SYSFS_PATH, dev)) as identity_file: |
| 251 | + identity = identity_file.read().strip() |
| 252 | + if identity == WD_MLNX_MAIN_IDENTITY: |
| 253 | + return True |
| 254 | + except IOError: |
| 255 | + pass |
| 256 | + |
| 257 | + return False |
| 258 | + |
| 259 | + |
| 260 | +def is_wd_type2(dev): |
| 261 | + """ |
| 262 | + Checks if dev is Mellanox type 2 watchdog |
| 263 | + """ |
| 264 | + |
| 265 | + return os.path.exists("{}/{}/timeleft".format(WD_SYSFS_PATH, dev)) |
| 266 | + |
| 267 | + |
| 268 | +def get_watchdog(): |
| 269 | + """ |
| 270 | + Return WatchdogType1 or WatchdogType2 based on system |
| 271 | + """ |
| 272 | + |
| 273 | + watchdog_main_device_name = None |
| 274 | + |
| 275 | + for device in os.listdir("/dev/"): |
| 276 | + if device.startswith("watchdog") and is_mlnx_wd_main(device): |
| 277 | + watchdog_main_device_name = device |
| 278 | + |
| 279 | + watchdog_device_path = "/dev/{}".format(watchdog_main_device_name) |
| 280 | + |
| 281 | + watchdog = None |
| 282 | + |
| 283 | + if is_wd_type2(watchdog_main_device_name): |
| 284 | + watchdog = WatchdogType2(watchdog_device_path) |
| 285 | + else: |
| 286 | + watchdog = WatchdogType1(watchdog_device_path) |
| 287 | + |
| 288 | + return watchdog |
0 commit comments