From 7637066fa093329ba53dbd18965d8fdb382e6527 Mon Sep 17 00:00:00 2001 From: Pradchaya Date: Sat, 9 Jun 2018 16:31:27 +0700 Subject: [PATCH 1/7] Add Celestica seastone dx010 psuutil.py plugins --- .../x86_64-cel_seastone-r0/plugins/psuutil.py | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py new file mode 100644 index 000000000000..9e8f99e7a7f2 --- /dev/null +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -0,0 +1,102 @@ +# +# psuutil.py +# Platform-specific PSU status interface for SONiC +# + +import os.path + +try: + from sonic_psu.psu_base import PsuBase +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + + +class PsuUtil(PsuBase): + """Platform-specific PSUutil class""" + + def __init__(self): + PsuBase.__init__(self) + # DX010 PSU pin mapping + self.psu = [ + {'base':216}, # Reserved + {'abs':27, 'power':22}, + {'abs':28, 'power':25} + ] + + def init_psu_gpio(self, pinnum): + # export pin, input as default + gpio_base = self.psu[0]['base'] + export_file = "/sys/class/gpio/export" + direction_file = '/sys/class/gpio/gpio' + str(gpio_base+pinnum) + '/direction' + + try: + with open(export_file, 'w') as fd: + fd.write(str(gpio_base+pinnum)) + except Exception as error: + logging.error("Unable to export gpio ", pinnum) + + + # Get a psu status and presence + def read_psu_statuses(self, pinnum): + sys_gpio_dir = "/sys/class/gpio" + retval = 'ERR' + gpio_base = self.psu[0]['base'] + + gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) + gpio_file = gpio_dir + "/value" + + # init gpio + if (not os.path.isdir(gpio_dir)): + self.init_psu_gpio(pinnum) + + try: + with open(gpio_file, 'r') as fd: + retval = fd.read() + except Exception as error: + logging.error("Unable to open ", gpio_file, "file !") + + retval = retval.rstrip('\r\n') + return retval + + def get_num_psus(self): + """ + Retrieves the number of PSUs available on the device + :return: An integer, the number of PSUs available on the device + """ + DX010_MAX_PSUS = 2 + return DX010_MAX_PSUS + + def get_psu_status(self, index): + """ + Retrieves the oprational status of power supply unit (PSU) defined + by index + :param index: An integer, index of the PSU of which to query status + :return: Boolean, True if PSU is operating properly, False if PSU is\ + faulty + """ + status = 0 + psu_status = self.read_psu_statuses(self.psu[index]['power']) + if (psu_status != 'ERR'): + psu_status = int(psu_status, 10) + # Check for PSU status + if (psu_status == 1): + status = 1 + + return status + + def get_psu_presence(self, index): + """ + Retrieves the presence status of power supply unit (PSU) defined + by index + :param index: An integer, index of the PSU of which to query status + :return: Boolean, True if PSU is plugged, False if not + """ + status = 0 + psu_absence = self.read_psu_statuses(self.psu[index]['abs']) + if (psu_absence != 'ERR'): + psu_absence = (int(psu_absence, 10)) + # Check for PSU presence + if (psu_absence == 0): + status = 1 + + return status From 000a63a51990587a2650838d72d98922d92197ef Mon Sep 17 00:00:00 2001 From: Pradchaya Date: Wed, 13 Jun 2018 10:38:17 +0700 Subject: [PATCH 2/7] Fix psuutil to use dynamic sysfs gpio base. --- .../x86_64-cel_seastone-r0/plugins/psuutil.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 9e8f99e7a7f2..9c415d837e8c 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -3,7 +3,8 @@ # Platform-specific PSU status interface for SONiC # -import os.path +import os +import logging try: from sonic_psu.psu_base import PsuBase @@ -22,6 +23,14 @@ def __init__(self): {'abs':27, 'power':22}, {'abs':28, 'power':25} ] + self.psu[0]['base'] = self.get_gpio_base() + + def get_gpio_base(self): + sys_gpio_dir = "/sys/class/gpio" + for r in os.listdir(sys_gpio_dir): + if "gpiochip" in r: + return int(r[8:],10) + def init_psu_gpio(self, pinnum): # export pin, input as default @@ -33,14 +42,14 @@ def init_psu_gpio(self, pinnum): with open(export_file, 'w') as fd: fd.write(str(gpio_base+pinnum)) except Exception as error: - logging.error("Unable to export gpio ", pinnum) + logging.error("Unable to export gpio ", str(gpio_base+pinnum)) # Get a psu status and presence def read_psu_statuses(self, pinnum): sys_gpio_dir = "/sys/class/gpio" - retval = 'ERR' gpio_base = self.psu[0]['base'] + retval = 'ERR' gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) gpio_file = gpio_dir + "/value" From f393c4ce0b8f3033ce5220a601bbee3195c34f9a Mon Sep 17 00:00:00 2001 From: wirut Date: Wed, 13 Jun 2018 13:48:03 +0800 Subject: [PATCH 3/7] add exeption handler for psuutil --- .../x86_64-cel_seastone-r0/plugins/psuutil.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 9c415d837e8c..2870d8267372 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -5,6 +5,7 @@ import os import logging +import sys try: from sonic_psu.psu_base import PsuBase @@ -19,30 +20,33 @@ def __init__(self): PsuBase.__init__(self) # DX010 PSU pin mapping self.psu = [ - {'base':216}, # Reserved + {'base': self.get_gpio_base()}, {'abs':27, 'power':22}, {'abs':28, 'power':25} ] - self.psu[0]['base'] = self.get_gpio_base() def get_gpio_base(self): sys_gpio_dir = "/sys/class/gpio" - for r in os.listdir(sys_gpio_dir): - if "gpiochip" in r: - return int(r[8:],10) + try: + for r in os.listdir(sys_gpio_dir): + if "gpiochip" in r: + return int(r[8:],10) + except Exception as error: + logging.error("Unable to get gpio base") + sys.exit(0) def init_psu_gpio(self, pinnum): # export pin, input as default gpio_base = self.psu[0]['base'] export_file = "/sys/class/gpio/export" - direction_file = '/sys/class/gpio/gpio' + str(gpio_base+pinnum) + '/direction' try: with open(export_file, 'w') as fd: fd.write(str(gpio_base+pinnum)) except Exception as error: logging.error("Unable to export gpio ", str(gpio_base+pinnum)) + sys.exit(0) # Get a psu status and presence @@ -53,7 +57,7 @@ def read_psu_statuses(self, pinnum): gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) gpio_file = gpio_dir + "/value" - + # init gpio if (not os.path.isdir(gpio_dir)): self.init_psu_gpio(pinnum) @@ -63,6 +67,7 @@ def read_psu_statuses(self, pinnum): retval = fd.read() except Exception as error: logging.error("Unable to open ", gpio_file, "file !") + sys.exit(0) retval = retval.rstrip('\r\n') return retval From 2846424f77fe9fef08ac91e6837d0443a07017c5 Mon Sep 17 00:00:00 2001 From: wirut Date: Wed, 13 Jun 2018 16:32:53 +0800 Subject: [PATCH 4/7] remove sys.exit from psuutil --- device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 8e2945a605e3..1a80b2fd4820 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -5,7 +5,6 @@ import os import logging -import sys try: @@ -34,7 +33,6 @@ def get_gpio_base(self): return int(r[8:],10) except Exception as error: logging.error("Unable to get gpio base") - sys.exit(0) def init_psu_gpio(self, pinnum): @@ -46,7 +44,6 @@ def init_psu_gpio(self, pinnum): fd.write(str(gpio_base+pinnum)) except Exception as error: logging.error("Unable to export gpio ", str(gpio_base+pinnum)) - sys.exit(0) # Get a psu status and presence @@ -67,7 +64,6 @@ def read_psu_statuses(self, pinnum): retval = fd.read() except Exception as error: logging.error("Unable to open ", gpio_file, "file !") - sys.exit(0) retval = retval.rstrip('\r\n') return retval From a5c64bfa26769f2d0959b0f6136be28429bfba24 Mon Sep 17 00:00:00 2001 From: wirut Date: Fri, 15 Jun 2018 12:55:31 +0800 Subject: [PATCH 5/7] Change logging format for psuutil --- device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 1a80b2fd4820..1d47624d1273 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -43,7 +43,7 @@ def init_psu_gpio(self, pinnum): with open(export_file, 'w') as fd: fd.write(str(gpio_base+pinnum)) except Exception as error: - logging.error("Unable to export gpio ", str(gpio_base+pinnum)) + logging.error("Unable to export gpio " + str(gpio_base+pinnum)) # Get a psu status and presence @@ -63,7 +63,7 @@ def read_psu_statuses(self, pinnum): with open(gpio_file, 'r') as fd: retval = fd.read() except Exception as error: - logging.error("Unable to open ", gpio_file, "file !") + logging.error("Unable to open " + gpio_file + "file !") retval = retval.rstrip('\r\n') return retval From 1c991b5bcd1cbd46aad330040a91f4cb8acd09d1 Mon Sep 17 00:00:00 2001 From: wirut Date: Tue, 19 Jun 2018 16:44:24 +0700 Subject: [PATCH 6/7] Update exception handler --- .../x86_64-cel_seastone-r0/plugins/psuutil.py | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 1d47624d1273..0b988e613a27 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -4,7 +4,6 @@ # import os -import logging try: @@ -27,12 +26,10 @@ def __init__(self): def get_gpio_base(self): sys_gpio_dir = "/sys/class/gpio" - try: - for r in os.listdir(sys_gpio_dir): - if "gpiochip" in r: - return int(r[8:],10) - except Exception as error: - logging.error("Unable to get gpio base") + for r in os.listdir(sys_gpio_dir): + if "gpiochip" in r: + return int(r[8:],10) + return 216 #Reserve def init_psu_gpio(self, pinnum): @@ -42,15 +39,14 @@ def init_psu_gpio(self, pinnum): try: with open(export_file, 'w') as fd: fd.write(str(gpio_base+pinnum)) - except Exception as error: - logging.error("Unable to export gpio " + str(gpio_base+pinnum)) + except IOError: + raise IOError("Unable to export gpio " + str(gpio_base+pinnum)) # Get a psu status and presence def read_psu_statuses(self, pinnum): sys_gpio_dir = "/sys/class/gpio" gpio_base = self.psu[0]['base'] - retval = 'ERR' gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) gpio_file = gpio_dir + "/value" @@ -62,8 +58,8 @@ def read_psu_statuses(self, pinnum): try: with open(gpio_file, 'r') as fd: retval = fd.read() - except Exception as error: - logging.error("Unable to open " + gpio_file + "file !") + except IOError: + raise IOError("Unable to open " + gpio_file + "file !") retval = retval.rstrip('\r\n') return retval @@ -86,11 +82,10 @@ def get_psu_status(self, index): """ status = 0 psu_status = self.read_psu_statuses(self.psu[index]['power']) - if (psu_status != 'ERR'): - psu_status = int(psu_status, 10) - # Check for PSU status - if (psu_status == 1): - status = 1 + psu_status = int(psu_status, 10) + # Check for PSU status + if (psu_status == 1): + status = 1 return status @@ -103,10 +98,9 @@ def get_psu_presence(self, index): """ status = 0 psu_absence = self.read_psu_statuses(self.psu[index]['abs']) - if (psu_absence != 'ERR'): - psu_absence = (int(psu_absence, 10)) - # Check for PSU presence - if (psu_absence == 0): - status = 1 + psu_absence = (int(psu_absence, 10)) + # Check for PSU presence + if (psu_absence == 0): + status = 1 return status From bcc3484cf526e890e6990ef40ff7ae468f03d3fa Mon Sep 17 00:00:00 2001 From: wirut Date: Thu, 21 Jun 2018 16:55:57 +0700 Subject: [PATCH 7/7] Remove init psu gpio method --- .../x86_64-cel_seastone-r0/plugins/psuutil.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py index 0b988e613a27..510c03a43ff1 100644 --- a/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py +++ b/device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py @@ -32,17 +32,6 @@ def get_gpio_base(self): return 216 #Reserve - def init_psu_gpio(self, pinnum): - # export pin, input as default - gpio_base = self.psu[0]['base'] - export_file = "/sys/class/gpio/export" - try: - with open(export_file, 'w') as fd: - fd.write(str(gpio_base+pinnum)) - except IOError: - raise IOError("Unable to export gpio " + str(gpio_base+pinnum)) - - # Get a psu status and presence def read_psu_statuses(self, pinnum): sys_gpio_dir = "/sys/class/gpio" @@ -51,10 +40,6 @@ def read_psu_statuses(self, pinnum): gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) gpio_file = gpio_dir + "/value" - # init gpio - if (not os.path.isdir(gpio_dir)): - self.init_psu_gpio(pinnum) - try: with open(gpio_file, 'r') as fd: retval = fd.read()