From af0c679b42c7e25241c2c4887f3e00afed1a9664 Mon Sep 17 00:00:00 2001 From: vincent_chiang Date: Sun, 5 Apr 2026 01:35:59 +0000 Subject: [PATCH 1/2] [AS9947-36XKB] Upgrade kernel drivers to 6.12 LTS Port AS9947-36XKB platform to kernel 6.12 LTS: - modules/PKG.yml, modules/builds/Makefile: Use onl-kernel-6.12-lts-x86-64-all - platform-config: Switch to *kernel-6-12 - psu.c, fan.c, thermal.c: i2c_driver.probe drops 2nd arg (i2c_client_get_device_id); platform_driver.remove returns void; hwmon_device_register_with_groups instead of hwmon_device_register_with_info (avoids -EINVAL with empty chip_info in 6.12) - fpga.c, i2c-ocores.c: Rename platform driver to "ocores-as9947-36xkb" to avoid name conflict with AS9947-72XKB (same MODULE_ALIAS caused "already registered" error at boot when both platforms share an installer) Verified on DUT (192.168.6.78): kernel 6.12.73-OpenNetworkLinux, onlpdump exit 0, no driver conflicts in dmesg, all thermals and chassis fans reporting correctly. Co-Authored-By: Claude Sonnet 4.6 --- .../accton/x86-64/as9947-36xkb/.gitignore | 2 + .../accton/x86-64/as9947-36xkb/Makefile | 1 + .../x86-64/as9947-36xkb/modules/Makefile | 1 + .../x86-64/as9947-36xkb/modules/PKG.yml | 1 + .../as9947-36xkb/modules/builds/.gitignore | 1 + .../as9947-36xkb/modules/builds/Makefile | 8 + .../builds/x86-64-accton-as9947-36xkb-fan.c | 713 +++++++++++ .../builds/x86-64-accton-as9947-36xkb-fpga.c | 424 +++++++ .../x86-64-accton-as9947-36xkb-i2c-ocores.c | 980 +++++++++++++++ .../builds/x86-64-accton-as9947-36xkb-leds.c | 377 ++++++ .../builds/x86-64-accton-as9947-36xkb-psu.c | 901 ++++++++++++++ .../builds/x86-64-accton-as9947-36xkb-sfp.c | 1058 +++++++++++++++++ .../builds/x86-64-accton-as9947-36xkb-sys.c | 342 ++++++ .../x86-64-accton-as9947-36xkb-thermal.c | 249 ++++ .../accton/x86-64/as9947-36xkb/onlp/Makefile | 1 + .../accton/x86-64/as9947-36xkb/onlp/PKG.yml | 1 + .../x86-64/as9947-36xkb/onlp/builds/Makefile | 2 + .../as9947-36xkb/onlp/builds/lib/Makefile | 2 + .../onlp/builds/onlpdump/Makefile | 2 + .../x86_64_accton_as9947_36xkb/.gitignore | 0 .../builds/x86_64_accton_as9947_36xkb/.module | 1 + .../x86_64_accton_as9947_36xkb/Makefile | 9 + .../builds/x86_64_accton_as9947_36xkb/README | 6 + .../module/auto/make.mk | 9 + .../auto/x86_64_accton_as9947_36xkb.yml | 50 + .../x86_64_accton_as9947_36xkb.x | 14 + .../x86_64_accton_as9947_36xkb_config.h | 137 +++ .../x86_64_accton_as9947_36xkb_dox.h | 26 + .../x86_64_accton_as9947_36xkb_porting.h | 107 ++ .../x86_64_accton_as9947_36xkb/module/make.mk | 10 + .../module/src/Makefile | 9 + .../module/src/debug.c | 45 + .../module/src/fani.c | 319 +++++ .../module/src/ledi.c | 231 ++++ .../module/src/make.mk | 9 + .../module/src/platform_lib.c | 109 ++ .../module/src/platform_lib.h | 101 ++ .../module/src/psui.c | 177 +++ .../module/src/sfpi.c | 320 +++++ .../module/src/sysi.c | 153 +++ .../module/src/thermali.c | 172 +++ .../src/x86_64_accton_as9947_36xkb_config.c | 81 ++ .../src/x86_64_accton_as9947_36xkb_enums.c | 10 + .../src/x86_64_accton_as9947_36xkb_int.h | 12 + .../src/x86_64_accton_as9947_36xkb_log.c | 17 + .../src/x86_64_accton_as9947_36xkb_log.h | 12 + .../src/x86_64_accton_as9947_36xkb_module.c | 24 + .../src/x86_64_accton_as9947_36xkb_ucli.c | 50 + .../as9947-36xkb/platform-config/Makefile | 1 + .../as9947-36xkb/platform-config/r0/Makefile | 1 + .../as9947-36xkb/platform-config/r0/PKG.yml | 1 + .../src/lib/x86-64-accton-as9947-36xkb-r0.yml | 32 + .../x86_64_accton_as9947_36xkb_r0/__init__.py | 108 ++ 53 files changed, 7429 insertions(+) create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/.gitignore create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/PKG.yml create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/.gitignore create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fan.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fpga.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-i2c-ocores.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-leds.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-psu.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sfp.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sys.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-thermal.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/PKG.yml create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/lib/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/onlpdump/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.gitignore create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.module create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/README create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/make.mk create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/x86_64_accton_as9947_36xkb.yml create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb.x create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_config.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_dox.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_porting.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/make.mk create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/debug.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/fani.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/ledi.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/make.mk create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/psui.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sfpi.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sysi.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/thermali.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_config.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_enums.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_int.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.h create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_module.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_ucli.c create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/platform-config/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/Makefile create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/PKG.yml create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/lib/x86-64-accton-as9947-36xkb-r0.yml create mode 100644 packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/.gitignore b/packages/platforms/accton/x86-64/as9947-36xkb/.gitignore new file mode 100644 index 000000000..b89ad605e --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/.gitignore @@ -0,0 +1,2 @@ +*x86*64*accton*as9947*36xkb*.mk +onlpdump.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/modules/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/PKG.yml b/packages/platforms/accton/x86-64/as9947-36xkb/modules/PKG.yml new file mode 100644 index 000000000..5e340bebc --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/platform-modules.yml VENDOR=accton BASENAME=x86-64-accton-as9947-36xkb ARCH=amd64 KERNELS="onl-kernel-6.12-lts-x86-64-all:amd64" diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/.gitignore b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/.gitignore new file mode 100644 index 000000000..a65b41774 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/.gitignore @@ -0,0 +1 @@ +lib diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/Makefile new file mode 100644 index 000000000..ed8b00016 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/Makefile @@ -0,0 +1,8 @@ +KERNELS := onl-kernel-6.12-lts-x86-64-all:amd64 +KINCLUDES := $(ONL)/packages/platforms/accton/x86-64/modules/builds/ipmi/accton_ipmi_intf.h +KMODULES := $(ONL)/packages/platforms/accton/x86-64/modules/builds/ipmi/accton_ipmi_intf.c $(wildcard *.c) +VENDOR := accton +BASENAME := x86-64-accton-as9947-36xkb +ARCH := x86_64 +include $(ONL)/make/kmodule.mk + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fan.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fan.c new file mode 100644 index 000000000..8335f6a27 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fan.c @@ -0,0 +1,713 @@ +/* + * Copyright (C) Roger Ho + * + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + + +#define DRVNAME "as9947_36xkb_fan" +#define IPMI_FAN_READ_CMD 0x14 +#define IPMI_FAN_WRITE_CMD 0x15 +#define IPMI_FAN_READ_MODEL_CMD 0x10 +#define IPMI_FAN_READ_SERIAL_CMD 0x11 +#define IPMI_FAN_READ_RPM_CMD 0x20 +#define IPMI_FAN_REG_READ_CMD 0x20 +#define MAX_FAN_SPEED_RPM 33000 +#define IPMI_FAN_MODEL_SIZE 13 +#define IPMI_FAN_SERIAL_SIZE 10 + +static ssize_t set_fan(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_fan(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_string(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_version(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t show_dir(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf); +static int as9947_36xkb_fan_probe(struct platform_device *pdev); +static void as9947_36xkb_fan_remove(struct platform_device *pdev); + +enum fan_id { + FAN_1, + FAN_2, + FAN_3, + FAN_4, + FAN_5, + FAN_6, + FAN_7, + FAN_8, + NUM_OF_FAN, + NUM_OF_FAN_MODULE = NUM_OF_FAN +}; + +enum fan_data_index { + FAN_PRESENT, + FAN_PWM, + FAN_SPEED0, + FAN_SPEED1, + FAN_DATA_COUNT, + + FAN_TARGET_SPEED0 = 0, + FAN_TARGET_SPEED1, + FAN_SPEED_TOLERANCE, + FAN_SPEED_DATA_COUNT +}; + +struct as9947_36xkb_fan_data { + struct platform_device *pdev; + struct device *hwmon_dev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + /* 4 bytes for each fan, the last 2 bytes is fan dir */ + unsigned char ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT + 2]; + unsigned char ipmi_resp_cpld[2]; + unsigned char ipmi_resp_speed[NUM_OF_FAN * FAN_SPEED_DATA_COUNT]; + unsigned char ipmi_resp_string[16]; + struct ipmi_data ipmi; + unsigned char ipmi_tx_data[3]; /* 0: FAN id, 1: 0x02, 2: PWM */ +}; + +struct as9947_36xkb_fan_data *data = NULL; + +static struct platform_driver as9947_36xkb_fan_driver = { + .probe = as9947_36xkb_fan_probe, + .remove = as9947_36xkb_fan_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +#define FAN_PRESENT_ATTR_ID(index) FAN##index##_PRESENT +#define FAN_PWM_ATTR_ID(index) FAN##index##_PWM +#define FAN_RPM_ATTR_ID(index) FAN##index##_INPUT +#define FAN_DIR_ATTR_ID(index) FAN##index##_DIR +#define FAN_FAULT_ATTR_ID(index) FAN##index##_FAULT +#define FAN_RPM_TARGET_ATTR_ID(index) FAN##index##_TARGET +#define FAN_RPM_TOLERANCE_ATTR_ID(index) FAN##index##_TOLERANCE +#define FAN_MODEL_ATTR_ID(index) FAN##index##_MODEL +#define FAN_SERIAL_ATTR_ID(index) FAN##index##_SERIAL + +#define FAN_ATTR(fan_id) \ + FAN_PRESENT_ATTR_ID(fan_id), \ + FAN_PWM_ATTR_ID(fan_id), \ + FAN_RPM_ATTR_ID(fan_id), \ + FAN_DIR_ATTR_ID(fan_id), \ + FAN_FAULT_ATTR_ID(fan_id), \ + FAN_MODEL_ATTR_ID(fan_id), \ + FAN_SERIAL_ATTR_ID(fan_id) + +#define FAN_RPM_THRESHOLD_ATTR(fan_id) \ + FAN_RPM_TARGET_ATTR_ID(fan_id), \ + FAN_RPM_TOLERANCE_ATTR_ID(fan_id) + +enum as9947_36xkb_fan_sysfs_attrs { + FAN_ATTR(1), + FAN_ATTR(2), + FAN_ATTR(3), + FAN_ATTR(4), + FAN_ATTR(5), + FAN_ATTR(6), + FAN_ATTR(7), + FAN_ATTR(8), + NUM_OF_FAN_ATTR, + FAN_VERSION, + FAN_MAX_RPM, + NUM_OF_PER_FAN_ATTR = (NUM_OF_FAN_ATTR/NUM_OF_FAN), + FAN_RPM_THRESHOLD_ATTR(1), + FAN_RPM_THRESHOLD_ATTR(2), + FAN_RPM_THRESHOLD_ATTR(3), + FAN_RPM_THRESHOLD_ATTR(4), + FAN_RPM_THRESHOLD_ATTR(5), + FAN_RPM_THRESHOLD_ATTR(6), + FAN_RPM_THRESHOLD_ATTR(7), + FAN_RPM_THRESHOLD_ATTR(8) +}; + +/* fan attributes */ +#define DECLARE_FAN_VER_SENSOR_DEVICE_ATTR() \ + static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, FAN_VERSION) +#define DECLARE_FAN_VER_ATTR() \ + &sensor_dev_attr_version.dev_attr.attr + +#define DECLARE_FAN_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(fan##index##_present, S_IRUGO, show_fan, NULL, \ + FAN##index##_PRESENT); \ + static SENSOR_DEVICE_ATTR(fan##index##_pwm, S_IWUSR | S_IRUGO, show_fan, \ + set_fan, FAN##index##_PWM); \ + static SENSOR_DEVICE_ATTR(fan##index##_input, S_IRUGO, show_fan, NULL, \ + FAN##index##_INPUT); \ + static SENSOR_DEVICE_ATTR(fan##index##_dir, S_IRUGO, show_dir, NULL, \ + FAN##index##_DIR); \ + static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO, show_fan, NULL, \ + FAN##index##_FAULT); \ + static SENSOR_DEVICE_ATTR(fan##index##_target, S_IRUGO, show_threshold, \ + NULL, FAN##index##_TARGET); \ + static SENSOR_DEVICE_ATTR(fan##index##_tolerance, S_IRUGO, show_threshold,\ + NULL, FAN##index##_TOLERANCE); \ + static SENSOR_DEVICE_ATTR(fan##index##_model, S_IRUGO, show_string,\ + NULL, FAN##index##_MODEL); \ + static SENSOR_DEVICE_ATTR(fan##index##_serial, S_IRUGO, show_string,\ + NULL, FAN##index##_SERIAL) + +static SENSOR_DEVICE_ATTR(fan_max_speed_rpm, S_IRUGO, show_fan, NULL, \ + FAN_MAX_RPM); +#define DECLARE_FAN_MAX_RPM_ATTR(index) \ + &sensor_dev_attr_fan_max_speed_rpm.dev_attr.attr + +#define DECLARE_FAN_ATTR(index) \ + &sensor_dev_attr_fan##index##_present.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_pwm.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_input.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_dir.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_fault.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_target.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_tolerance.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_model.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_serial.dev_attr.attr + +DECLARE_FAN_SENSOR_DEVICE_ATTR(1); +DECLARE_FAN_SENSOR_DEVICE_ATTR(2); +DECLARE_FAN_SENSOR_DEVICE_ATTR(3); +DECLARE_FAN_SENSOR_DEVICE_ATTR(4); +DECLARE_FAN_SENSOR_DEVICE_ATTR(5); +DECLARE_FAN_SENSOR_DEVICE_ATTR(6); +DECLARE_FAN_SENSOR_DEVICE_ATTR(7); +DECLARE_FAN_SENSOR_DEVICE_ATTR(8); +DECLARE_FAN_VER_SENSOR_DEVICE_ATTR(); + +static struct attribute *as9947_36xkb_fan_attrs[] = { + /* fan attributes */ + DECLARE_FAN_ATTR(1), + DECLARE_FAN_ATTR(2), + DECLARE_FAN_ATTR(3), + DECLARE_FAN_ATTR(4), + DECLARE_FAN_ATTR(5), + DECLARE_FAN_ATTR(6), + DECLARE_FAN_ATTR(7), + DECLARE_FAN_ATTR(8), + DECLARE_FAN_VER_ATTR(), + DECLARE_FAN_MAX_RPM_ATTR(), + NULL +}; +ATTRIBUTE_GROUPS(as9947_36xkb_fan); + + +static struct as9947_36xkb_fan_data *as9947_36xkb_fan_update_device(void) +{ + int status = 0; + + if (time_before(jiffies, data->last_updated + HZ * 5) && data->valid) + return data; + + data->valid = 0; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_READ_CMD, NULL, 0, + data->ipmi_resp, sizeof(data->ipmi_resp)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_tx_data[0] = IPMI_FAN_READ_RPM_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp_speed, + sizeof(data->ipmi_resp_speed)); + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_fan(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = attr->index / NUM_OF_PER_FAN_ATTR; + int value = 0; + int index = 0; + int present = 0; + int error = 0; + + if (attr->index == FAN_MAX_RPM) + return sprintf(buf, "%d\n", MAX_FAN_SPEED_RPM); + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + index = fid * FAN_DATA_COUNT; /* base index */ + present = !!data->ipmi_resp[index + FAN_PRESENT]; + + switch (attr->index) { + case FAN1_PRESENT: + case FAN2_PRESENT: + case FAN3_PRESENT: + case FAN4_PRESENT: + case FAN5_PRESENT: + case FAN6_PRESENT: + case FAN7_PRESENT: + case FAN8_PRESENT: + value = present; + break; + case FAN1_PWM: + case FAN2_PWM: + case FAN3_PWM: + case FAN4_PWM: + case FAN5_PWM: + case FAN6_PWM: + case FAN7_PWM: + case FAN8_PWM: + index = (fid % NUM_OF_FAN_MODULE) * FAN_DATA_COUNT; + value = DIV_ROUND_CLOSEST(data->ipmi_resp[index + FAN_PWM] * 666, 100); + break; + case FAN1_INPUT: + case FAN2_INPUT: + case FAN3_INPUT: + case FAN4_INPUT: + case FAN5_INPUT: + case FAN6_INPUT: + case FAN7_INPUT: + case FAN8_INPUT: + value = (int)data->ipmi_resp[index + FAN_SPEED0] | + (int)data->ipmi_resp[index + FAN_SPEED1] << 8; + break; + case FAN1_FAULT: + case FAN2_FAULT: + case FAN3_FAULT: + case FAN4_FAULT: + case FAN5_FAULT: + case FAN6_FAULT: + case FAN7_FAULT: + case FAN8_FAULT: + value = (int)data->ipmi_resp[index + FAN_SPEED0] | + (int)data->ipmi_resp[index + FAN_SPEED1] << 8; + value = !value; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_fan(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long pwm; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = attr->index / NUM_OF_PER_FAN_ATTR; + + status = kstrtol(buf, 10, &pwm); + if (status) + return status; + + pwm = (pwm * 100) / 666; /* Convert pwm to register value */ + + mutex_lock(&data->update_lock); + + /* + * Send IPMI write command : + * BMC supports a design with four fan modules, each containing two fans. + * Since NUM_OF_FAN_MODULE is 8 in AS9817, it needs to be divided by 2. + * The result : + * fan1_pwm(Front) : ipmi_tx_data[0] = 1, fan2_pwm(Front) : ipmi_tx_data[0] = 2 + * fan3_pwm(Front) : ipmi_tx_data[0] = 3, fan4_pwm(Front) : ipmi_tx_data[0] = 4 + * fan5_pwm(Rear) : ipmi_tx_data[0] = 1, fan6_pwm(Rear) : ipmi_tx_data[0] = 2 + * fan7_pwm(Rear) : ipmi_tx_data[0] = 3, fan8_pwm(Rear) : ipmi_tx_data[0] = 4 + * + */ + data->ipmi_tx_data[0] = (fid % (NUM_OF_FAN_MODULE / 2)) + 1; + data->ipmi_tx_data[1] = 0x02; + data->ipmi_tx_data[2] = pwm; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_WRITE_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), + NULL, 0); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* force update */ + data->valid = 0; + status = count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static struct as9947_36xkb_fan_data *as9947_36xkb_fan_update_cpld_ver(void) +{ + int status = 0; + + data->valid = 0; + data->ipmi_tx_data[0] = 0x33; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_REG_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp_cpld, + sizeof(data->ipmi_resp_cpld)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static struct as9947_36xkb_fan_data *as9947_36xkb_fan_update_model_serial(int fan_id, int index) +{ + int status = 0; + int string_size = 0 ; + + data->valid = 0; + + switch (index) { + case FAN1_MODEL: + case FAN2_MODEL: + case FAN3_MODEL: + case FAN4_MODEL: + case FAN5_MODEL: + case FAN6_MODEL: + case FAN7_MODEL: + case FAN8_MODEL: + data->ipmi_tx_data[0] = IPMI_FAN_READ_MODEL_CMD; + string_size = IPMI_FAN_MODEL_SIZE; + data->ipmi_resp_string[IPMI_FAN_MODEL_SIZE] = '\0'; + break; + case FAN1_SERIAL: + case FAN2_SERIAL: + case FAN3_SERIAL: + case FAN4_SERIAL: + case FAN5_SERIAL: + case FAN6_SERIAL: + case FAN7_SERIAL: + case FAN8_SERIAL: + data->ipmi_tx_data[0] = IPMI_FAN_READ_SERIAL_CMD; + string_size = IPMI_FAN_SERIAL_SIZE; + data->ipmi_resp_string[IPMI_FAN_SERIAL_SIZE] = '\0'; + break; + default: + goto exit; + } + + if (fan_id > 3) + data->ipmi_tx_data[1] = fan_id - 4; + else + data->ipmi_tx_data[1] = fan_id; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp_string, + string_size); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + + +exit: + return data; +} + +static ssize_t show_string(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = attr->index / NUM_OF_PER_FAN_ATTR; + int present = 0; + int error = 0; + int index = 0; + char *str = NULL; + + mutex_lock(&data->update_lock); + /* check fan present */ + data = as9947_36xkb_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + index = fid * FAN_DATA_COUNT; /* base index */ + present = !!data->ipmi_resp[index + FAN_PRESENT]; + mutex_unlock(&data->update_lock); + if (!present) + return sprintf(buf, "\n"); + + mutex_lock(&data->update_lock); + data = as9947_36xkb_fan_update_model_serial(fid, attr->index); + if (!data->valid) { + error = -EIO; + goto exit; + } + mutex_unlock(&data->update_lock); + + str = data->ipmi_resp_string; + return sprintf(buf, "%s\n", str); + exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_version(struct device *dev, struct device_attribute *da, + char *buf) +{ + unsigned char major; + unsigned char minor; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_fan_update_cpld_ver(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + major = data->ipmi_resp_cpld[0]; + minor = data->ipmi_resp_cpld[1]; + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d.%d\n", major, minor); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_dir(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = (attr->index / NUM_OF_PER_FAN_ATTR); + int value = 0; + int index = 0; + int present = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + index = fid * FAN_DATA_COUNT; /* base index */ + present = !!data->ipmi_resp[index + FAN_PRESENT]; + + value = data->ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT] | + (data->ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT + 1] << 8); + mutex_unlock(&data->update_lock); + + if (!present) + return sprintf(buf, "0\n"); + else + return sprintf(buf, "%s\n", + (value & BIT(fid % NUM_OF_FAN_MODULE)) ? "B2F" : "F2B"); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + switch (attr->index) { + case FAN1_TARGET: + case FAN2_TARGET: + case FAN3_TARGET: + case FAN4_TARGET: + case FAN5_TARGET: + case FAN6_TARGET: + case FAN7_TARGET: + case FAN8_TARGET: + value = (int)data->ipmi_resp_speed[FAN_TARGET_SPEED0] | + (int)data->ipmi_resp_speed[FAN_TARGET_SPEED1] << 8; + break; + case FAN1_TOLERANCE: + case FAN2_TOLERANCE: + case FAN3_TOLERANCE: + case FAN4_TOLERANCE: + case FAN5_TOLERANCE: + case FAN6_TOLERANCE: + case FAN7_TOLERANCE: + case FAN8_TOLERANCE: + value = (int)data->ipmi_resp_speed[FAN_SPEED_TOLERANCE]; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static int as9947_36xkb_fan_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev; + + hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, DRVNAME, + NULL, as9947_36xkb_fan_groups); + if (IS_ERR(data->hwmon_dev)) { + status = PTR_ERR(data->hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "Device Created\n"); + + return status; +} + +static void as9947_36xkb_fan_remove(struct platform_device *pdev) +{ + mutex_lock(&data->update_lock); + if (data->hwmon_dev) { + hwmon_device_unregister(data->hwmon_dev); + data->hwmon_dev = NULL; + } + mutex_unlock(&data->update_lock); +} + +static int __init as9947_36xkb_fan_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_fan_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9947_36xkb_fan_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) { + goto ipmi_err; + } + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_fan_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_fan_exit(void) +{ + if (data) { + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + kfree(data); + } + platform_driver_unregister(&as9947_36xkb_fan_driver); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9947_36xkb_fan driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_fan_init); +module_exit(as9947_36xkb_fan_exit); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fpga.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fpga.c new file mode 100644 index 000000000..56f5b4d3a --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-fpga.c @@ -0,0 +1,424 @@ +/* + * Copyright (C) Willy Liu + * + * This module supports the accton fpga via pcie that read/write reg + * mechanism to get OSFP/SFP status ...etc. + * This includes the: + * Accton as9947_36xkb FPGA + * + * Copyright (C) 2017 Finisar Corp. + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define __STDC_WANT_LIB_EXT1__ 1 +#include +#include + +/*********************************************** + * variable define + * *********************************************/ +#define DRVNAME "as9947_36xkb_fpga" +#define OCORES_I2C_DRVNAME "ocores-as9947-36xkb" + +#define PORT_NUM 40 /* 4 SFP +36 OSFPs/QSFPDDs */ + +/* + * PCIE BAR address + */ +#define BAR4_NUM 4 +#define BAR5_NUM 5 + +/* need checking*/ +#define REGION_LEN 0xFF +#define FPGA_PCI_VENDOR_ID 0x1172 +#define FPGA_PCI_DEVICE_ID 0xe001 + +#define FPGA_PCIE_START_OFFSET 0x0000 +#define FPGA_MAJOR_VER_REG 0x01 +#define FPGA_MINOR_VER_REG 0x02 +#define SPI_BUSY_MASK_CPLD1 0x01 +#define SPI_BUSY_MASK_CPLD2 0x02 + +#define FPGA_PCIE_START_OFFSET 0x0000 +#define FPGA_BOARD_INFO_REG (FPGA_PCIE_START_OFFSET + 0x00) + +/* CPLD 1 */ +#define CPLD1_PCIE_START_OFFSET 0x2000 +/* CPLD 2 */ +#define CPLD2_PCIE_START_OFFSET 0x3000 +/*********************************************** + * macro define + * *********************************************/ +#define pcie_err(fmt, args...) \ + printk(KERN_ERR "["DRVNAME"]: " fmt " ", ##args) + +#define pcie_info(fmt, args...) \ + printk(KERN_ERR "["DRVNAME"]: " fmt " ", ##args) + + +#define LOCK(lock) \ +do { \ + spin_lock(lock); \ +} while (0) + +#define UNLOCK(lock) \ +do { \ + spin_unlock(lock); \ +} while (0) + + +/*********************************************** + * structure & variable declare + * *********************************************/ +typedef struct pci_fpga_device_s { + void __iomem *data_base_addr4; + void __iomem *data_base_addr5; + resource_size_t data_region4; + resource_size_t data_region5; + struct pci_dev *pci_dev; + struct platform_device *fpga_i2c[PORT_NUM]; +} pci_fpga_device_t; + +/*fpga port status*/ +struct as9947_36xkb_fpga_data { + u8 cpld_reg[2]; + unsigned long last_updated; /* In jiffies */ + pci_fpga_device_t pci_fpga_dev; +}; + +static struct platform_device *pdev = NULL; +extern spinlock_t cpld_access_lock; +extern int wait_spi(u32 mask, unsigned long timeout); + +struct attribute_mapping { + u16 attr_base; + u16 reg; + u8 revert; +}; + +static inline unsigned int fpga_read(void __iomem *addr, u32 spi_mask) +{ + wait_spi(spi_mask, usecs_to_jiffies(20)); + return ioread8(addr); +} + +static inline void fpga_write(void __iomem *addr, u8 val, u32 spi_mask) +{ + wait_spi(spi_mask, usecs_to_jiffies(20)); + iowrite8(val, addr); +} + +struct _port_data { + u16 offset; + u16 mask; /* SPI Busy mask : 0x01 --> CPLD1, 0x02 --> CPLD2 */ +}; +/* ============PCIe Bar Offset to I2C Master Mapping============== */ +static const struct _port_data port[PORT_NUM]= { + {0x2380, SPI_BUSY_MASK_CPLD1},/* 0x2380 - 0x2390 CPLD1 I2C Master SFP28 Port0 */ + {0x23A0, SPI_BUSY_MASK_CPLD1},/* 0x23A0 - 0x23B0 CPLD1 I2C Master SFP28 Port1 */ + {0x23C0, SPI_BUSY_MASK_CPLD1},/* 0x23C0 - 0x23D0 CPLD1 I2C Master SFP28 Port2 */ + {0x23E0, SPI_BUSY_MASK_CPLD1},/* 0x23E0 - 0x23F0 CPLD1 I2C Master SFP28 Port3 */ + {0x2100, SPI_BUSY_MASK_CPLD1},/* 0x2100 - 0x2110 CPLD1 I2C Master QSFP28 Port0 */ + {0x2120, SPI_BUSY_MASK_CPLD1},/* 0x2120 - 0x2130 CPLD1 I2C Master QSFP28 Port1 */ + {0x2140, SPI_BUSY_MASK_CPLD1},/* 0x2140 - 0x2150 CPLD1 I2C Master QSFP28 Port2 */ + {0x2160, SPI_BUSY_MASK_CPLD1},/* 0x2160 - 0x2170 CPLD1 I2C Master QSFP28 Port3 */ + {0x2180, SPI_BUSY_MASK_CPLD1},/* 0x2180 - 0x2190 CPLD1 I2C Master QSFP28 Port4 */ + {0x21A0, SPI_BUSY_MASK_CPLD1},/* 0x21A0 - 0x21B0 CPLD1 I2C Master QSFP28 Port5 */ + {0x21C0, SPI_BUSY_MASK_CPLD1},/* 0x21C0 - 0x21D0 CPLD1 I2C Master QSFP28 Port6 */ + {0x21E0, SPI_BUSY_MASK_CPLD1},/* 0x21E0 - 0x21F0 CPLD1 I2C Master QSFP28 Port7 */ + {0x2200, SPI_BUSY_MASK_CPLD1},/* 0x2200 - 0x2210 CPLD1 I2C Master QSFP28 Port8 */ + {0x2220, SPI_BUSY_MASK_CPLD1},/* 0x2220 - 0x2230 CPLD1 I2C Master QSFP28 Port9 */ + {0x2240, SPI_BUSY_MASK_CPLD1},/* 0x2240 - 0x2250 CPLD1 I2C Master QSFP28 Port10 */ + {0x2260, SPI_BUSY_MASK_CPLD1},/* 0x2260 - 0x2270 CPLD1 I2C Master QSFP28 Port11 */ + {0x2280, SPI_BUSY_MASK_CPLD1},/* 0x2280 - 0x2290 CPLD1 I2C Master QSFP28 Port12 */ + {0x22A0, SPI_BUSY_MASK_CPLD1},/* 0x22A0 - 0x22B0 CPLD1 I2C Master QSFP28 Port13 */ + {0x22C0, SPI_BUSY_MASK_CPLD1},/* 0x22C0 - 0x22D0 CPLD1 I2C Master QSFP28 Port14 */ + {0x22E0, SPI_BUSY_MASK_CPLD1},/* 0x22E0 - 0x22F0 CPLD1 I2C Master QSFP28 Port15 */ + {0x2300, SPI_BUSY_MASK_CPLD1},/* 0x2300 - 0x2310 CPLD1 I2C Master QSFP28 Port16 */ + {0x2320, SPI_BUSY_MASK_CPLD1},/* 0x2320 - 0x2330 CPLD1 I2C Master QSFP28 Port17 */ + {0x2340, SPI_BUSY_MASK_CPLD1},/* 0x2340 - 0x2350 CPLD1 I2C Master QSFP28 Port18 */ + {0x2360, SPI_BUSY_MASK_CPLD1},/* 0x2360 - 0x2370 CPLD1 I2C Master QSFP28 Port19 */ + {0x3100, SPI_BUSY_MASK_CPLD2},/* 0x3100 - 0x3110 CPLD2 I2C Master QSFP28 Port20 */ + {0x3120, SPI_BUSY_MASK_CPLD2},/* 0x3120 - 0x3130 CPLD2 I2C Master QSFP28 Port21 */ + {0x3140, SPI_BUSY_MASK_CPLD2},/* 0x3140 - 0x3150 CPLD2 I2C Master QSFP28 Port22 */ + {0x3160, SPI_BUSY_MASK_CPLD2},/* 0x3160 - 0x3170 CPLD2 I2C Master QSFP28 Port23*/ + {0x3180, SPI_BUSY_MASK_CPLD2},/* 0x3180 - 0x3190 CPLD2 I2C Master QSFP28 Port24 */ + {0x31A0, SPI_BUSY_MASK_CPLD2},/* 0x31A0 - 0x31B0 CPLD2 I2C Master QSFP28 Port25 */ + {0x31C0, SPI_BUSY_MASK_CPLD2},/* 0x31C0 - 0x31D0 CPLD2 I2C Master QSFP28 Port26 */ + {0x31E0, SPI_BUSY_MASK_CPLD2},/* 0x31E0 - 0x31F0 CPLD2 I2C Master QSFP28 Port27 */ + {0x3200, SPI_BUSY_MASK_CPLD2},/* 0x3200 - 0x3210 CPLD2 I2C Master QSFP28 Port28 */ + {0x3220, SPI_BUSY_MASK_CPLD2},/* 0x3220 - 0x3230 CPLD2 I2C Master QSFP28 Port29 */ + {0x3240, SPI_BUSY_MASK_CPLD2},/* 0x3240 - 0x3250 CPLD2 I2C Master QSFP28 Port30 */ + {0x3260, SPI_BUSY_MASK_CPLD2},/* 0x3260 - 0x3270 CPLD2 I2C Master QSFP28 Port31 */ + {0x3280, SPI_BUSY_MASK_CPLD2},/* 0x3280 - 0x3290 CPLD2 I2C Master QSFP28 Port32 */ + {0x32A0, SPI_BUSY_MASK_CPLD2},/* 0x32A0 - 0x32B0 CPLD2 I2C Master QSFP28 Port33 */ + {0x32C0, SPI_BUSY_MASK_CPLD2},/* 0x32C0 - 0x34D0 CPLD2 I2C Master QSFP28 Port34 */ + {0x32E0, SPI_BUSY_MASK_CPLD2},/* 0x32E0 - 0x34F0 CPLD2 I2C Master QSFP28 Port35 */ +}; + +static struct ocores_i2c_platform_data as9947_36xkb_platform_data = { + .reg_io_width = 1, + .reg_shift = 2, + /* + * PRER_L and PRER_H are calculated based on clock_khz and bus_khz + * in i2c-ocores.c:ocores_init. + */ +#if 1 + /* SCL 400KHZ in FPGA spec. => PRER_L = 0x0B, PRER_H = 0x00 */ + .clock_khz = 24000, + .bus_khz = 400, +#else + /* SCL 100KHZ in FPGA spec. => PRER_L = 0x2F, PRER_H = 0x00 */ + .clock_khz = 24000, + .bus_khz = 100, +#endif +}; + +struct platform_device *ocore_i2c_device_add(unsigned int id, unsigned long bar_base, + unsigned int offset) +{ + struct resource res = DEFINE_RES_MEM(bar_base + offset, 0x20); + struct platform_device *pdev; + int err; + + pdev = platform_device_alloc(OCORES_I2C_DRVNAME, id); + if (!pdev) { + err = -ENOMEM; + pcie_err("Port%u device allocation failed (%d)\n", (id & 0xFF), err); + goto exit; + } + + err = platform_device_add_resources(pdev, &res, 1); + if (err) { + pcie_err("Port%u device resource addition failed (%d)\n", (id & 0xFF), err); + goto exit_device_put; + } + + err = platform_device_add_data(pdev, &as9947_36xkb_platform_data, + sizeof(struct ocores_i2c_platform_data)); + if (err) { + pcie_err("Port%u platform data allocation failed (%d)\n", (id & 0xFF), err); + goto exit_device_put; + } + + err = platform_device_add(pdev); + if (err) { + pcie_err("Port%u device addition failed (%d)\n", (id & 0xFF), err); + goto exit_device_put; + } + + return pdev; + +exit_device_put: + platform_device_put(pdev); +exit: + return NULL; +} + +static int as9947_36xkb_pcie_fpga_stat_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct as9947_36xkb_fpga_data *fpga_ctl; + struct pci_dev *pcidev; + struct resource *ret; + int i; + int status = 0, err = 0; + unsigned long bar_base; + + fpga_ctl = devm_kzalloc(dev, sizeof(struct as9947_36xkb_fpga_data), GFP_KERNEL); + if (!fpga_ctl) { + return -ENOMEM; + } + platform_set_drvdata(pdev, fpga_ctl); + + pcidev = pci_get_device(FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID, NULL); + if (!pcidev) { + dev_err(dev, "Cannot found PCI device(%x:%x)\n", + FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID); + return -ENODEV; + } + fpga_ctl->pci_fpga_dev.pci_dev = pcidev; + + err = pci_enable_device(pcidev); + if (err != 0) { + dev_err(dev, "Cannot enable PCI device(%x:%x)\n", + FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID); + status = -ENODEV; + goto exit_pci_disable; + } + /* enable PCI bus-mastering */ + pci_set_master(pcidev); + /* + * Cannot use 'pci_request_regions(pcidev, DRVNAME)' + * to request all Region 1, Region 2 because another + * address will be allocated by the i2c-ocores.ko. + */ + fpga_ctl->pci_fpga_dev.data_base_addr4 = pci_iomap(pcidev, BAR4_NUM, 0); + if (fpga_ctl->pci_fpga_dev.data_base_addr4 == NULL) { + dev_err(dev, "Failed to map BAR4\n"); + status = -EIO; + goto exit_pci_disable; + } + fpga_ctl->pci_fpga_dev.data_region4 = pci_resource_start(pcidev, BAR4_NUM) + CPLD1_PCIE_START_OFFSET; + ret = request_mem_region(fpga_ctl->pci_fpga_dev.data_region4, REGION_LEN, DRVNAME"_cpld1"); + if (ret == NULL) { + dev_err(dev, "[%s] cannot request region\n", DRVNAME"_cpld1"); + status = -EIO; + goto exit_pci_iounmap1; + } + dev_info(dev, "(BAR%d resource: Start=0x%lx, Length=0x%x)", BAR4_NUM, + (unsigned long)fpga_ctl->pci_fpga_dev.data_region4, REGION_LEN); + + fpga_ctl->pci_fpga_dev.data_base_addr5 = pci_iomap(pcidev, BAR5_NUM, 0); + if (fpga_ctl->pci_fpga_dev.data_base_addr5 == NULL) { + dev_err(dev, "Failed to map BAR5\n"); + status = -EIO; + goto exit_pci_release1; + } + fpga_ctl->pci_fpga_dev.data_region5 = pci_resource_start(pcidev, BAR5_NUM) + CPLD2_PCIE_START_OFFSET; + ret = request_mem_region(fpga_ctl->pci_fpga_dev.data_region5, REGION_LEN, DRVNAME"_cpld2"); + if (ret == NULL) { + dev_err(dev, "[%s] cannot request region\n", DRVNAME"_cpld2"); + status = -EIO; + goto exit_pci_iounmap2; + } + dev_info(dev, "(BAR%d resource: Start=0x%lx, Length=0x%x)", BAR5_NUM, + (unsigned long)fpga_ctl->pci_fpga_dev.data_region5, REGION_LEN); + + /* Create I2C ocore devices first, then create the FPGA sysfs. + * To prevent the application from accessing an ocore device + * that has not been fully created due to the port status + * being present. + */ + + /* + * Create ocore_i2c device for OSFP EEPROM + */ + for (i = 0; i < PORT_NUM; i++) { + switch (i) + { + case 0 ... 4: + case 5 ... 23: + bar_base = pci_resource_start(pcidev, BAR4_NUM); + break; + case 24 ... 39: + bar_base = pci_resource_start(pcidev, BAR5_NUM); + break; + default: + break; + } + fpga_ctl->pci_fpga_dev.fpga_i2c[i] = + ocore_i2c_device_add((i | (port[i].mask << 8)), bar_base, port[i].offset); + if (IS_ERR(fpga_ctl->pci_fpga_dev.fpga_i2c[i])) { + status = PTR_ERR(fpga_ctl->pci_fpga_dev.fpga_i2c[i]); + dev_err(dev, "rc:%d, unload Port%u[0x%ux] device\n", + status, i, port[i].offset); + goto exit_ocores_device; + } + } + + return 0; + +exit_ocores_device: + while (i > 0) { + i--; + platform_device_unregister(fpga_ctl->pci_fpga_dev.fpga_i2c[i]); + } + release_mem_region(fpga_ctl->pci_fpga_dev.data_region5, REGION_LEN); +exit_pci_iounmap2: + pci_iounmap(fpga_ctl->pci_fpga_dev.pci_dev, fpga_ctl->pci_fpga_dev.data_base_addr5); +exit_pci_release1: + release_mem_region(fpga_ctl->pci_fpga_dev.data_region4, REGION_LEN); +exit_pci_iounmap1: + pci_iounmap(fpga_ctl->pci_fpga_dev.pci_dev, fpga_ctl->pci_fpga_dev.data_base_addr4); +exit_pci_disable: + pci_disable_device(fpga_ctl->pci_fpga_dev.pci_dev); + + return status; +} + +static void as9947_36xkb_pcie_fpga_stat_remove(struct platform_device *pdev) +{ + struct as9947_36xkb_fpga_data *fpga_ctl = platform_get_drvdata(pdev); + + if (pci_is_enabled(fpga_ctl->pci_fpga_dev.pci_dev)) { + int i; + /* Unregister ocore_i2c device */ + for (i = 0; i < PORT_NUM; i++) { + platform_device_unregister(fpga_ctl->pci_fpga_dev.fpga_i2c[i]); + } + pci_iounmap(fpga_ctl->pci_fpga_dev.pci_dev, fpga_ctl->pci_fpga_dev.data_base_addr4); + pci_iounmap(fpga_ctl->pci_fpga_dev.pci_dev, fpga_ctl->pci_fpga_dev.data_base_addr5); + release_mem_region(fpga_ctl->pci_fpga_dev.data_region4, REGION_LEN); + release_mem_region(fpga_ctl->pci_fpga_dev.data_region5, REGION_LEN); + pci_disable_device(fpga_ctl->pci_fpga_dev.pci_dev); + } +} + +static struct platform_driver pcie_fpga_port_stat_driver = { + .probe = as9947_36xkb_pcie_fpga_stat_probe, + .remove = as9947_36xkb_pcie_fpga_stat_remove, + .driver = { + .owner = THIS_MODULE, + .name = DRVNAME, + }, +}; + +static int __init as9947_36xkb_pcie_fpga_init(void) +{ + int status = 0; + + /* + * Create FPGA platform driver and device + */ + status = platform_driver_register(&pcie_fpga_port_stat_driver); + if (status < 0) { + return status; + } + + pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(pdev)) { + status = PTR_ERR(pdev); + goto exit_pci; + } + + return status; + +exit_pci: + platform_driver_unregister(&pcie_fpga_port_stat_driver); + + return status; +} + +static void __exit as9947_36xkb_pcie_fpga_exit(void) +{ + platform_device_unregister(pdev); + platform_driver_unregister(&pcie_fpga_port_stat_driver); +} + + +module_init(as9947_36xkb_pcie_fpga_init); +module_exit(as9947_36xkb_pcie_fpga_exit); + +MODULE_AUTHOR("Willy Liu "); +MODULE_DESCRIPTION("AS9947-36XKB FPGA via PCIE"); +MODULE_LICENSE("GPL"); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-i2c-ocores.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-i2c-ocores.c new file mode 100644 index 000000000..8724c84f7 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-i2c-ocores.c @@ -0,0 +1,980 @@ + +// SPDX-License-Identifier: GPL-2.0 +/* + * i2c-ocores.c: I2C bus driver for OpenCores I2C controller + * (https://opencores.org/project/i2c/overview) + * + * Peter Korsgaard + * + * Support for the GRLIB port of the controller by + * Andreas Larsson + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * 'process_lock' exists because ocores_process() and ocores_process_timeout() + * can't run in parallel. + */ +struct ocores_i2c { + void __iomem *base; + int iobase; + u32 reg_shift; + u32 reg_io_width; + unsigned long flags; + wait_queue_head_t wait; + struct i2c_adapter adap; + struct i2c_msg *msg; + int pos; + int nmsgs; + int state; /* see STATE_ */ + spinlock_t process_lock; + struct clk *clk; + int ip_clock_khz; + int bus_clock_khz; + void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value); + u8 (*getreg)(struct ocores_i2c *i2c, int reg); +}; + +/* registers */ +#define OCI2C_PRELOW 0 +#define OCI2C_PREHIGH 1 +#define OCI2C_CONTROL 2 +#define OCI2C_DATA 3 +#define OCI2C_CMD 4 /* write only */ +#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */ + +#define OCI2C_CTRL_IEN 0x40 +#define OCI2C_CTRL_EN 0x80 + +#define OCI2C_CMD_START 0x91 +#define OCI2C_CMD_STOP 0x41 +#define OCI2C_CMD_READ 0x21 +#define OCI2C_CMD_WRITE 0x11 +#define OCI2C_CMD_READ_ACK 0x21 +#define OCI2C_CMD_READ_NACK 0x29 +#define OCI2C_CMD_IACK 0x01 + +#define OCI2C_STAT_IF 0x01 +#define OCI2C_STAT_TIP 0x02 +#define OCI2C_STAT_ARBLOST 0x20 +#define OCI2C_STAT_BUSY 0x40 +#define OCI2C_STAT_NACK 0x80 + +#define STATE_DONE 0 +#define STATE_START 1 +#define STATE_WRITE 2 +#define STATE_READ 3 +#define STATE_ERROR 4 + +#define TYPE_OCORES 0 +#define TYPE_GRLIB 1 +#define TYPE_SIFIVE_REV0 2 + +#define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */ + +static unsigned int timeout = 1; +module_param(timeout , uint, S_IRUGO|S_IWUSR); +MODULE_PARM_DESC(timeout, "Tiemout for ocores_poll_wait, in unit of milliseconds."); + +static unsigned int debug = 1; +module_param(debug , uint, S_IRUGO|S_IWUSR); +MODULE_PARM_DESC(debug, "Enable or disable debug message. 1 -> enable, 0 -> disable"); + +spinlock_t cpld_access_lock; +EXPORT_SYMBOL(cpld_access_lock); + +#define LOCK(lock) \ +do { \ + spin_lock(lock); \ +} while (0) + +#define UNLOCK(lock) \ +do { \ + spin_unlock(lock); \ +} while (0) + +#define SPI_BUSY_ADDR (0xb3322000 + 0x30) +#define IOREMAP_SIZE (0x04) +static void __iomem *spi_busy_reg=NULL; +int wait_spi(u32 mask, u8 times) { + u32 data; + u32 spi_busy_flag = 0; + u8 max_times = 0; + max_times = times; + /* pr_info("CPLD %u, Will time-out at jiffie %lu\n", cpld_id,timeout); */ + if (!spi_busy_reg) { + return -EFAULT; + } + + while(max_times) { + data = ioread32(spi_busy_reg); + /* pr_info("@ %u, Read spi_busy_reg: 0x%08x 0x%08x\n", ri, data, mask); */ + if (!((( data >> 24) & 0xFF) & mask)) + { + /* spi flag is normal*/ + if (spi_busy_flag) + break; + } + else + { + spi_busy_flag = 1; + } + + usleep_range(10, 11); + max_times--; + + if(max_times == 0) + { + if(!spi_busy_flag) + break; + if (debug) { + pr_warn("spi_busy_flag %d as max_times is 0 \n", spi_busy_flag); + } + return -ETIMEDOUT; + } + } + + return 0; +} +EXPORT_SYMBOL(wait_spi); + +static int wait_cpld(struct ocores_i2c *i2c, u8 times) { + struct platform_device *pdev; + struct device *dev; + + if (!i2c->adap.dev.parent) { + return -EFAULT; + } + + /* Get SPI Busy mask from pdev->id */ + dev = i2c->adap.dev.parent; + pdev = container_of(dev, struct platform_device, dev); + wait_spi((pdev->id & 0xFF00) >> 8, times); + + return 0; +} + +static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite8(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite16(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite32(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite16be(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite32be(value, i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) +{ + return ioread8(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) +{ + return ioread16(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) +{ + return ioread32(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg) +{ + return ioread16be(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg) +{ + return ioread32be(i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value) +{ + outb(value, i2c->iobase + reg); +} + +static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg) +{ + return inb(i2c->iobase + reg); +} + +static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) +{ + wait_cpld(i2c, 6); + i2c->setreg(i2c, reg, value); +} + +static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) +{ + wait_cpld(i2c, 6); + return i2c->getreg(i2c, reg); +} + +static void ocores_process(struct ocores_i2c *i2c, u8 stat) +{ + struct i2c_msg *msg = i2c->msg; + unsigned long flags; + struct device *dev = i2c->adap.dev.parent; + + /* + * If we spin here is because we are in timeout, so we are going + * to be in STATE_ERROR. See ocores_process_timeout() + */ + spin_lock_irqsave(&i2c->process_lock, flags); + if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { + /* stop has been sent */ + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); + wake_up(&i2c->wait); + goto out; + } + + /* error? */ + if (stat & OCI2C_STAT_ARBLOST) { + i2c->state = STATE_ERROR; + if (debug) { + dev_warn(dev, "I2C %s arbitration lost", i2c->adap.name); + } + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + + if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { + i2c->state = + (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; + + if (stat & OCI2C_STAT_NACK) { + i2c->state = STATE_ERROR; + if (debug) { + dev_warn(dev, "I2C %s, no ACK from slave 0x%02x", + i2c->adap.name, msg->addr); + } + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + } else { + msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); + } + + /* end of msg? */ + if (i2c->pos == msg->len) { + i2c->nmsgs--; + i2c->msg++; + i2c->pos = 0; + msg = i2c->msg; + + if (i2c->nmsgs) { /* end? */ + /* send start? */ + if (!(msg->flags & I2C_M_NOSTART)) { + u8 addr = i2c_8bit_addr_from_msg(msg); + + i2c->state = STATE_START; + + oc_setreg(i2c, OCI2C_DATA, addr); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); + goto out; + } + i2c->state = (msg->flags & I2C_M_RD) + ? STATE_READ : STATE_WRITE; + } else { + i2c->state = STATE_DONE; + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + } + + if (i2c->state == STATE_READ) { + oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? + OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK); + } else { + oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); + } + +out: + spin_unlock_irqrestore(&i2c->process_lock, flags); + +} + +static irqreturn_t ocores_isr(int irq, void *dev_id) +{ + struct ocores_i2c *i2c = dev_id; + u8 stat = oc_getreg(i2c, OCI2C_STATUS); + + if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) { + if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY)) + return IRQ_NONE; + } else if (!(stat & OCI2C_STAT_IF)) { + return IRQ_NONE; + } + ocores_process(i2c, stat); + + return IRQ_HANDLED; +} + +/** + * Process timeout event + * @i2c: ocores I2C device instance + */ +static void ocores_process_timeout(struct ocores_i2c *i2c) +{ + unsigned long flags; + + spin_lock_irqsave(&i2c->process_lock, flags); + i2c->state = STATE_ERROR; + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + spin_unlock_irqrestore(&i2c->process_lock, flags); +} + +/** + * Wait until something change in a given register + * @i2c: ocores I2C device instance + * @reg: register to query + * @mask: bitmask to apply on register value + * @val: expected result + * @timeout: timeout in jiffies + * + * Timeout is necessary to avoid to stay here forever when the chip + * does not answer correctly. + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_wait(struct ocores_i2c *i2c, + int reg, u8 mask, u8 val, + const unsigned long timeout) +{ + unsigned long j; + + j = jiffies + timeout; + while (1) { + u8 status = oc_getreg(i2c, reg); + + if ((status & mask) == val) + break; + + if (time_after(jiffies, j)) + return -ETIMEDOUT; + } + return 0; +} + +/** + * Wait until is possible to process some data + * @i2c: ocores I2C device instance + * + * Used when the device is in polling mode (interrupts disabled). + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_poll_wait(struct ocores_i2c *i2c) +{ + u8 mask; + int err; + + if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) { + /* transfer is over */ + mask = OCI2C_STAT_BUSY; + } else { + /* on going transfer */ + mask = OCI2C_STAT_TIP; + /* + * We wait for the data to be transferred (8bit), + * then we start polling on the ACK/NACK bit + */ + udelay((8 * 1000) / i2c->bus_clock_khz); + } + + /* + * once we are here we expect to get the expected result immediately + * so if after 1ms we timeout then something is broken. + */ + err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(timeout)); + if (err) { + if (debug) { + dev_warn(i2c->adap.dev.parent, + "%s: STATUS timeout, bit 0x%x did not clear in %ums(msecs_to_jiffies(%u)=%lu)\n", + __func__, mask, timeout, timeout, msecs_to_jiffies(timeout)); + } + } + return err; +} + +/** + * It handles an IRQ-less transfer + * @i2c: ocores I2C device instance + * + * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same + * (only that IRQ are not produced). This means that we can re-use entirely + * ocores_isr(), we just add our polling code around it. + * + * It can run in atomic context + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_process_polling(struct ocores_i2c *i2c) +{ + irqreturn_t ret; + int err; + + while (1) { + err = ocores_poll_wait(i2c); + if (err) { + break; /* timeout */ + } + + ret = ocores_isr(-1, i2c); + if (ret == IRQ_NONE) + break; /* all messages have been transferred */ + else { + if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) + if (i2c->state == STATE_DONE) + break; + } + } + + return err; +} + +static int ocores_xfer_core(struct ocores_i2c *i2c, + struct i2c_msg *msgs, int num, + bool polling) +{ + int ret = 0; + u8 ctrl; + + LOCK(&cpld_access_lock); + + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + if (polling) + oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); + else + oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); + + i2c->msg = msgs; + i2c->pos = 0; + i2c->nmsgs = num; + i2c->state = STATE_START; + + oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); + + if (polling) { + ret = ocores_process_polling(i2c); + } else { + if (wait_event_timeout(i2c->wait, + (i2c->state == STATE_ERROR) || + (i2c->state == STATE_DONE), HZ) == 0) + ret = -ETIMEDOUT; + } + if (ret) { + ocores_process_timeout(i2c); + UNLOCK(&cpld_access_lock); + return ret; + } + + UNLOCK(&cpld_access_lock); + return (i2c->state == STATE_DONE) ? num : -EIO; +} + +static int ocores_xfer_polling(struct i2c_adapter *adap, + struct i2c_msg *msgs, int num) +{ + return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, true); +} + +static int ocores_xfer(struct i2c_adapter *adap, + struct i2c_msg *msgs, int num) +{ + return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, false); +} + +static int ocores_init(struct device *dev, struct ocores_i2c *i2c) +{ + struct device *org; + int prescale; + int diff; + u8 ctrl; + + /* Temporary assignment for checking SPI Busy status. */ + org = i2c->adap.dev.parent; + i2c->adap.dev.parent = dev; + + LOCK(&cpld_access_lock); + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* make sure the device is disabled */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + UNLOCK(&cpld_access_lock); + + prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; + prescale = clamp(prescale, 0, 0xffff); + + diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; + if (abs(diff) > i2c->bus_clock_khz / 10) { + i2c->adap.dev.parent = org; + dev_err(dev, + "Unsupported clock settings: core: %d KHz, bus: %d KHz\n", + i2c->ip_clock_khz, i2c->bus_clock_khz); + return -EINVAL; + } + + dev_info(dev, "OCI2C_PRELOW=0x%02x OCI2C_PREHIGH=0x%02x\n", + prescale & 0xff, prescale >> 8); + LOCK(&cpld_access_lock); + oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); + oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); + + /* Init the device */ + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); + oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); + UNLOCK(&cpld_access_lock); + + i2c->adap.dev.parent = org; + + return 0; +} + + +static u32 ocores_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; +} + +static struct i2c_algorithm ocores_algorithm = { + .master_xfer = ocores_xfer, + .master_xfer_atomic = ocores_xfer_polling, + .functionality = ocores_func, +}; + +static const struct i2c_adapter ocores_adapter = { + .owner = THIS_MODULE, + .name = "i2c-ocores", + .class = I2C_CLASS_DEPRECATED, + .algo = &ocores_algorithm, +}; + +static const struct of_device_id ocores_i2c_match[] = { + { + .compatible = "opencores,i2c-ocores", + .data = (void *)TYPE_OCORES, + }, + { + .compatible = "aeroflexgaisler,i2cmst", + .data = (void *)TYPE_GRLIB, + }, + { + .compatible = "sifive,fu540-c000-i2c", + .data = (void *)TYPE_SIFIVE_REV0, + }, + { + .compatible = "sifive,i2c0", + .data = (void *)TYPE_SIFIVE_REV0, + }, + {}, +}; +MODULE_DEVICE_TABLE(of, ocores_i2c_match); + +#ifdef CONFIG_OF +/* + * Read and write functions for the GRLIB port of the controller. Registers are + * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one + * register. The subsequent registers have their offsets decreased accordingly. + */ +static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) +{ + u32 rd; + int rreg = reg; + + if (reg != OCI2C_PRELOW) + rreg--; + rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); + if (reg == OCI2C_PREHIGH) + return (u8)(rd >> 8); + else + return (u8)rd; +} + +static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) +{ + u32 curr, wr; + int rreg = reg; + + if (reg != OCI2C_PRELOW) + rreg--; + if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) { + curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); + if (reg == OCI2C_PRELOW) + wr = (curr & 0xff00) | value; + else + wr = (((u32)value) << 8) | (curr & 0xff); + } else { + wr = value; + } + iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); +} + +static int ocores_i2c_of_probe(struct platform_device *pdev, + struct ocores_i2c *i2c) +{ + struct device_node *np = pdev->dev.of_node; + const struct of_device_id *match; + u32 val; + u32 clock_frequency; + bool clock_frequency_present; + + if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { + /* no 'reg-shift', check for deprecated 'regstep' */ + if (!of_property_read_u32(np, "regstep", &val)) { + if (!is_power_of_2(val)) { + dev_err(&pdev->dev, "invalid regstep %d\n", + val); + return -EINVAL; + } + i2c->reg_shift = ilog2(val); + dev_warn(&pdev->dev, + "regstep property deprecated, use reg-shift\n"); + } + } + + clock_frequency_present = !of_property_read_u32(np, "clock-frequency", + &clock_frequency); + i2c->bus_clock_khz = 100; + + i2c->clk = devm_clk_get(&pdev->dev, NULL); + + if (!IS_ERR(i2c->clk)) { + int ret = clk_prepare_enable(i2c->clk); + + if (ret) { + dev_err(&pdev->dev, + "clk_prepare_enable failed: %d\n", ret); + return ret; + } + i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; + if (clock_frequency_present) + i2c->bus_clock_khz = clock_frequency / 1000; + } + + if (i2c->ip_clock_khz == 0) { + if (of_property_read_u32(np, "opencores,ip-clock-frequency", + &val)) { + if (!clock_frequency_present) { + dev_err(&pdev->dev, + "Missing required parameter 'opencores,ip-clock-frequency'\n"); + clk_disable_unprepare(i2c->clk); + return -ENODEV; + } + i2c->ip_clock_khz = clock_frequency / 1000; + dev_warn(&pdev->dev, + "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n"); + } else { + i2c->ip_clock_khz = val / 1000; + if (clock_frequency_present) + i2c->bus_clock_khz = clock_frequency / 1000; + } + } + + of_property_read_u32(pdev->dev.of_node, "reg-io-width", + &i2c->reg_io_width); + + match = of_match_node(ocores_i2c_match, pdev->dev.of_node); + if (match && (long)match->data == TYPE_GRLIB) { + dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); + i2c->setreg = oc_setreg_grlib; + i2c->getreg = oc_getreg_grlib; + } + + return 0; +} +#else +#define ocores_i2c_of_probe(pdev, i2c) -ENODEV +#endif + +static int ocores_i2c_probe(struct platform_device *pdev) +{ + struct ocores_i2c *i2c; + struct ocores_i2c_platform_data *pdata; + const struct of_device_id *match; + struct resource *res; + int irq; + int ret; + int i; + + i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); + if (!i2c) + return -ENOMEM; + + spin_lock_init(&i2c->process_lock); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (res) { + i2c->base = devm_ioremap_resource(&pdev->dev, res); + dev_info(&pdev->dev, "Resouce start:0x%llx, end:0x%llx", res->start, res->end); + if (IS_ERR(i2c->base)) + return PTR_ERR(i2c->base); + } else { + res = platform_get_resource(pdev, IORESOURCE_IO, 0); + if (!res) + return -EINVAL; + i2c->iobase = res->start; + if (!devm_request_region(&pdev->dev, res->start, + resource_size(res), + pdev->name)) { + dev_err(&pdev->dev, "Can't get I/O resource.\n"); + return -EBUSY; + } + i2c->setreg = oc_setreg_io_8; + i2c->getreg = oc_getreg_io_8; + } + + pdata = dev_get_platdata(&pdev->dev); + if (pdata) { + i2c->reg_shift = pdata->reg_shift; + i2c->reg_io_width = pdata->reg_io_width; + i2c->ip_clock_khz = pdata->clock_khz; + dev_info(&pdev->dev, "Write %d KHz, ioWidth:%d, shift:%d", i2c->ip_clock_khz, pdata->reg_io_width ,pdata->reg_shift); + if (pdata->bus_khz) + i2c->bus_clock_khz = pdata->bus_khz; + else + i2c->bus_clock_khz = 100; + } else { + ret = ocores_i2c_of_probe(pdev, i2c); + if (ret) + return ret; + } + + if (i2c->reg_io_width == 0) + i2c->reg_io_width = 1; /* Set to default value */ + + if (!i2c->setreg || !i2c->getreg) { + bool be = pdata ? pdata->big_endian : + of_device_is_big_endian(pdev->dev.of_node); + + switch (i2c->reg_io_width) { + case 1: + i2c->setreg = oc_setreg_8; + i2c->getreg = oc_getreg_8; + break; + + case 2: + i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; + i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; + break; + + case 4: + i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; + i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; + break; + + default: + dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", + i2c->reg_io_width); + ret = -EINVAL; + goto err_clk; + } + } + + init_waitqueue_head(&i2c->wait); + + irq = platform_get_irq_optional(pdev, 0); + if (irq == -ENXIO) { + ocores_algorithm.master_xfer = ocores_xfer_polling; + + /* + * Set in OCORES_FLAG_BROKEN_IRQ to enable workaround for + * FU540-C000 SoC in polling mode. + */ + match = of_match_node(ocores_i2c_match, pdev->dev.of_node); + if (match && (long)match->data == TYPE_SIFIVE_REV0) + i2c->flags |= OCORES_FLAG_BROKEN_IRQ; + } else { + if (irq < 0) + return irq; + } + + if (ocores_algorithm.master_xfer != ocores_xfer_polling) { + ret = devm_request_any_context_irq(&pdev->dev, irq, + ocores_isr, 0, + pdev->name, i2c); + if (ret) { + dev_err(&pdev->dev, "Cannot claim IRQ\n"); + goto err_clk; + } + } + ret = ocores_init(&pdev->dev, i2c); + if (ret) { + goto err_clk; + } + /* hook up driver to tree */ + platform_set_drvdata(pdev, i2c); + i2c->adap = ocores_adapter; + i2c_set_adapdata(&i2c->adap, i2c); + i2c->adap.dev.parent = &pdev->dev; + i2c->adap.dev.of_node = pdev->dev.of_node; + + /* add i2c adapter to i2c tree */ + ret = i2c_add_adapter(&i2c->adap); + if (ret) { + goto err_clk; + } + /* add in known devices to the bus */ + if (pdata) { + for (i = 0; i < pdata->num_devices; i++){ + i2c_new_client_device(&i2c->adap, pdata->devices + i); + } + } + + return 0; + +err_clk: + clk_disable_unprepare(i2c->clk); + return ret; +} + +static void ocores_i2c_remove(struct platform_device *pdev) +{ + struct ocores_i2c *i2c = platform_get_drvdata(pdev); + u8 ctrl; + + LOCK(&cpld_access_lock); + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* disable i2c logic */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + UNLOCK(&cpld_access_lock); + + /* remove adapter & data */ + i2c_del_adapter(&i2c->adap); + + if (!IS_ERR(i2c->clk)) + clk_disable_unprepare(i2c->clk); +} + +#ifdef CONFIG_PM_SLEEP +static int ocores_i2c_suspend(struct device *dev) +{ + struct ocores_i2c *i2c = dev_get_drvdata(dev); + u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* make sure the device is disabled */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + + if (!IS_ERR(i2c->clk)) + clk_disable_unprepare(i2c->clk); + return 0; +} + +static int ocores_i2c_resume(struct device *dev) +{ + struct ocores_i2c *i2c = dev_get_drvdata(dev); + + if (!IS_ERR(i2c->clk)) { + unsigned long rate; + int ret = clk_prepare_enable(i2c->clk); + + if (ret) { + dev_err(dev, + "clk_prepare_enable failed: %d\n", ret); + return ret; + } + rate = clk_get_rate(i2c->clk) / 1000; + if (rate) + i2c->ip_clock_khz = rate; + } + return ocores_init(dev, i2c); +} + +static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume); +#define OCORES_I2C_PM (&ocores_i2c_pm) +#else +#define OCORES_I2C_PM NULL +#endif + +static struct platform_driver ocores_i2c_driver = { + .probe = ocores_i2c_probe, + .remove = ocores_i2c_remove, + .driver = { + .name = "ocores-as9947-36xkb", + .of_match_table = ocores_i2c_match, + .pm = OCORES_I2C_PM, + }, +}; + +#if 0 +module_platform_driver(ocores_i2c_driver); +#else +static int __init ocores_i2c_as9947_36xkb_init(void) +{ + int err; + + err = platform_driver_register(&ocores_i2c_driver); + if (err < 0) { + pr_err("Failed to register ocores_i2c_driver"); + return err; + } + + spin_lock_init(&cpld_access_lock); + + /* + * The register address of SPI Busy is 0x33. + * It can not only read one byte. It needs to read four bytes from 0x30. + * The value is obtained by '(ioread32(spi_busy_reg) >> 24) & 0xFF'. + */ + spi_busy_reg = ioremap(SPI_BUSY_ADDR, IOREMAP_SIZE); + if (!spi_busy_reg) { + pr_err("could not remap spi_busy_reg memory\n"); + return -ENOMEM; + } + + return 0; +} +static void __exit ocores_i2c_as9947_36xkb_exit(void) +{ + platform_driver_unregister(&ocores_i2c_driver); + + iounmap(spi_busy_reg); +} + +module_init(ocores_i2c_as9947_36xkb_init); +module_exit(ocores_i2c_as9947_36xkb_exit); +#endif + +MODULE_AUTHOR("Peter Korsgaard "); +MODULE_DESCRIPTION("OpenCores I2C bus driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:ocores-as9947-36xkb"); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-leds.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-leds.c new file mode 100644 index 000000000..e1b06ce02 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-leds.c @@ -0,0 +1,377 @@ +/* + * Copyright (C) Willy Liu + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + + +#define DRVNAME "as9947_36xkb_led" +#define IPMI_LED_READ_CMD 0x1A +#define IPMI_LED_WRITE_CMD 0x1B + +static ssize_t set_led(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_led(struct device *dev, struct device_attribute *attr, + char *buf); +static int as9947_36xkb_led_probe(struct platform_device *pdev); +static void as9947_36xkb_led_remove(struct platform_device *pdev); + +struct as9947_36xkb_led_data { + struct platform_device *pdev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + unsigned char ipmi_resp[6]; /* 0:Loc 1:Diag 2:Gnss 3:Fan 4:Psu1 5:Psu2 */ + struct ipmi_data ipmi; +}; + +struct as9947_36xkb_led_data *data = NULL; + +static struct platform_driver as9947_36xkb_led_driver = { + .probe = as9947_36xkb_led_probe, + .remove = as9947_36xkb_led_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum ipmi_led_light_mode { + IPMI_LED_MODE_OFF, + IPMI_LED_MODE_RED = 2, + IPMI_LED_MODE_RED_BLINKING = 3, + IPMI_LED_MODE_GREEN = 4, + IPMI_LED_MODE_GREEN_BLINKING = 5, + IPMI_LED_MODE_BLUE = 8, + IPMI_LED_MODE_BLUE_BLINKING = 9, + IPMI_LED_MODE_CYAN = 0xC, + IPMI_LED_MODE_WHITE = 0xE, + IPMI_LED_MODE_AMBER = 0x10, + IPMI_LED_MODE_ORANGE = 0x20, +}; + +enum led_light_mode { + LED_MODE_OFF, + LED_MODE_RED = 10, + LED_MODE_RED_BLINKING = 11, + LED_MODE_ORANGE = 12, + LED_MODE_ORANGE_BLINKING = 13, + LED_MODE_YELLOW = 14, + LED_MODE_YELLOW_BLINKING = 15, + LED_MODE_GREEN = 16, + LED_MODE_GREEN_BLINKING = 17, + LED_MODE_BLUE = 18, + LED_MODE_BLUE_BLINKING = 19, + LED_MODE_PURPLE = 20, + LED_MODE_PURPLE_BLINKING = 21, + LED_MODE_AUTO = 22, + LED_MODE_AUTO_BLINKING = 23, + LED_MODE_WHITE = 24, + LED_MODE_WHITE_BLINKING = 25, + LED_MODE_CYAN = 26, + LED_MODE_CYAN_BLINKING = 27, + LED_MODE_UNKNOWN = 99 +}; + +enum as9947_36xkb_led_sysfs_attrs { + LED_LOC, + LED_DIAG, + LED_FAN, + LED_PSU1, + LED_PSU2 +}; + +static SENSOR_DEVICE_ATTR(led_loc, S_IWUSR | S_IRUGO, show_led, set_led, + LED_LOC); +static SENSOR_DEVICE_ATTR(led_diag, S_IWUSR | S_IRUGO, show_led, NULL, + LED_DIAG); +static SENSOR_DEVICE_ATTR(led_fan, S_IWUSR | S_IRUGO, show_led, NULL, + LED_FAN); +static SENSOR_DEVICE_ATTR(led_psu1, S_IWUSR | S_IRUGO, show_led, NULL, + LED_PSU1); +static SENSOR_DEVICE_ATTR(led_psu2, S_IWUSR | S_IRUGO, show_led, NULL, + LED_PSU2); + +static struct attribute *as9947_36xkb_led_attributes[] = { + &sensor_dev_attr_led_loc.dev_attr.attr, + &sensor_dev_attr_led_diag.dev_attr.attr, + &sensor_dev_attr_led_fan.dev_attr.attr, + &sensor_dev_attr_led_psu1.dev_attr.attr, + &sensor_dev_attr_led_psu2.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9947_36xkb_led_group = { + .attrs = as9947_36xkb_led_attributes, +}; + +static struct as9947_36xkb_led_data *as9947_36xkb_led_update_device(void) +{ + int status = 0; + + if (time_before(jiffies, data->last_updated + HZ * 5) && data->valid) { + return data; + } + + data->valid = 0; + status = ipmi_send_message(&data->ipmi, IPMI_LED_READ_CMD, NULL, 0, + data->ipmi_resp, sizeof(data->ipmi_resp)); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_led(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_led_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + switch (data->ipmi_resp[attr->index]) { + case IPMI_LED_MODE_OFF: + value = LED_MODE_OFF; + break; + case IPMI_LED_MODE_RED: + value = LED_MODE_RED; + break; + case IPMI_LED_MODE_RED_BLINKING: + value = LED_MODE_RED_BLINKING; + break; + case IPMI_LED_MODE_GREEN: + value = LED_MODE_GREEN; + break; + case IPMI_LED_MODE_GREEN_BLINKING: + value = LED_MODE_GREEN_BLINKING; + break; + case IPMI_LED_MODE_BLUE: + value = LED_MODE_BLUE; + break; + case IPMI_LED_MODE_BLUE_BLINKING: + value = LED_MODE_BLUE_BLINKING; + break; + case IPMI_LED_MODE_CYAN: + value = LED_MODE_CYAN; + break; + case IPMI_LED_MODE_WHITE: + value = LED_MODE_WHITE; + break; + case IPMI_LED_MODE_AMBER: + value = LED_MODE_YELLOW; + break; + case IPMI_LED_MODE_ORANGE: + value = LED_MODE_ORANGE; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_led(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long mode; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + status = kstrtol(buf, 10, &mode); + if (status) + return status; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_led_update_device(); + if (!data->valid) { + status = -EIO; + goto exit; + } + + data->ipmi_resp[0] = attr->index + 1; + + switch (mode) { + case LED_MODE_OFF: + data->ipmi_resp[1] = IPMI_LED_MODE_OFF; + break; + case LED_MODE_RED: + data->ipmi_resp[1] = IPMI_LED_MODE_RED; + break; + case LED_MODE_RED_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_RED_BLINKING; + break; + case LED_MODE_GREEN: + data->ipmi_resp[1] = IPMI_LED_MODE_GREEN; + break; + case LED_MODE_GREEN_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_GREEN_BLINKING; + break; + case LED_MODE_BLUE: + data->ipmi_resp[1] = IPMI_LED_MODE_BLUE; + break; + case LED_MODE_BLUE_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_BLUE_BLINKING; + break; + case LED_MODE_CYAN: + data->ipmi_resp[1] = IPMI_LED_MODE_CYAN; + break; + case LED_MODE_WHITE: + data->ipmi_resp[1] = IPMI_LED_MODE_WHITE; + break; + case LED_MODE_YELLOW: + data->ipmi_resp[1] = IPMI_LED_MODE_AMBER; + break; + case LED_MODE_ORANGE: + data->ipmi_resp[1] = IPMI_LED_MODE_ORANGE; + break; + default: + status = -EINVAL; + goto exit; + } + + /* Send IPMI write command */ + status = ipmi_send_message(&data->ipmi, IPMI_LED_WRITE_CMD, + data->ipmi_resp, 2, NULL, 0); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + status = count; + data->valid = 0; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static int as9947_36xkb_led_probe(struct platform_device *pdev) +{ + int status = -1; + + /* Register sysfs hooks */ + status = sysfs_create_group(&pdev->dev.kobj, &as9947_36xkb_led_group); + if (status) + goto exit; + + dev_info(&pdev->dev, "device created\n"); + + return 0; + +exit: + return status; +} + +static void as9947_36xkb_led_remove(struct platform_device *pdev) +{ + sysfs_remove_group(&pdev->dev.kobj, &as9947_36xkb_led_group); +} + +static int __init as9947_36xkb_led_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_led_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + data->valid = 0; + + ret = platform_driver_register(&as9947_36xkb_led_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_led_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_led_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9947_36xkb_led_driver); + kfree(data); +} + +MODULE_AUTHOR("Willy Liu "); +MODULE_DESCRIPTION("as9947_36xkb_led driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_led_init); +module_exit(as9947_36xkb_led_exit); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-psu.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-psu.c new file mode 100644 index 000000000..9a0fd1b09 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-psu.c @@ -0,0 +1,901 @@ +/* + * Copyright (C) Roger Ho + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + + +#define DRVNAME "as9947_36xkb_psu" +#define IPMI_PSU_READ_CMD 0x16 +#define IPMI_PSU_MODEL_NAME_CMD 0x10 +#define IPMI_PSU_SERIAL_NUM_CMD 0x11 +#define IPMI_PSU_FAN_DIR_CMD 0x13 +#define IPMI_PSU_INFO_CMD 0x20 +#define IPMI_MODEL_SERIAL_LEN 32 +#define IPMI_FAN_DIR_LEN 3 + +static ssize_t show_psu(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_psu_info(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_string(struct device *dev, struct device_attribute *attr, + char *buf); +static int as9947_36xkb_psu_probe(struct platform_device *pdev); +static void as9947_36xkb_psu_remove(struct platform_device *pdev); + +enum psu_id { + PSU_1, + PSU_2, + NUM_OF_PSU +}; + +enum psu_data_index { + PSU_PRESENT = 0, + PSU_TEMP_FAULT, + PSU_POWER_GOOD_CPLD, + PSU_POWER_GOOD_PMBUS, + PSU_OVER_VOLTAGE, + PSU_OVER_CURRENT, + PSU_POWER_ON, + PSU_VIN0, + PSU_VIN1, + PSU_VIN2, + PSU_VOUT0, + PSU_VOUT1, + PSU_VOUT2, + PSU_IIN0, + PSU_IIN1, + PSU_IIN2, + PSU_IOUT0, + PSU_IOUT1, + PSU_IOUT2, + PSU_PIN0, + PSU_PIN1, + PSU_PIN2, + PSU_PIN3, + PSU_POUT0, + PSU_POUT1, + PSU_POUT2, + PSU_POUT3, + PSU_TEMP1_0, + PSU_TEMP1_1, + PSU_TEMP2_0, + PSU_TEMP2_1, + PSU_TEMP3_0, + PSU_TEMP3_1, + PSU_FAN0, + PSU_FAN1, + PSU_VOUT_MODE, + PSU_STATUS_COUNT, + PSU_MODEL = 0, + PSU_SERIAL = 0, + PSU_TEMP1_MAX0 = 2, + PSU_TEMP1_MAX1, + PSU_TEMP1_MIN0, + PSU_TEMP1_MIN1, + PSU_TEMP2_MAX0, + PSU_TEMP2_MAX1, + PSU_TEMP2_MIN0, + PSU_TEMP2_MIN1, + PSU_TEMP3_MAX0, + PSU_TEMP3_MAX1, + PSU_TEMP3_MIN0, + PSU_TEMP3_MIN1, + PSU_VIN_UPPER_CRIT0, + PSU_VIN_UPPER_CRIT1, + PSU_VIN_UPPER_CRIT2, + PSU_VIN_MAX0, + PSU_VIN_MAX1, + PSU_VIN_MAX2, + PSU_VIN_MIN0, + PSU_VIN_MIN1, + PSU_VIN_MIN2, + PSU_VIN_LOWER_CRIT0, + PSU_VIN_LOWER_CRIT1, + PSU_VIN_LOWER_CRIT2, + PSU_VOUT_MAX0, + PSU_VOUT_MAX1, + PSU_VOUT_MAX2, + PSU_VOUT_MIN0, + PSU_VOUT_MIN1, + PSU_VOUT_MIN2, + PSU_IIN_MAX0, + PSU_IIN_MAX1, + PSU_IIN_MAX2, + PSU_IOUT_MAX0, + PSU_IOUT_MAX1, + PSU_IOUT_MAX2, + PSU_PIN_MAX0, + PSU_PIN_MAX1, + PSU_PIN_MAX2, + PSU_PIN_MAX3, + PSU_POUT_MAX0, + PSU_POUT_MAX1, + PSU_POUT_MAX2, + PSU_POUT_MAX3, + PSU_INFO_COUNT +}; + +struct ipmi_psu_resp_data { + unsigned char status[PSU_STATUS_COUNT]; + unsigned char info[PSU_INFO_COUNT]; + char serial[IPMI_MODEL_SERIAL_LEN+1]; + char model[IPMI_MODEL_SERIAL_LEN+1]; + char fandir[IPMI_FAN_DIR_LEN+1]; +}; + +struct as9947_36xkb_psu_data { + struct platform_device *pdev; + struct device *hwmon_dev[2]; + struct mutex update_lock; + char valid[2]; /* != 0 if registers are valid, 0: PSU1, 1: PSU2 */ + unsigned long last_updated[2]; /* In jiffies, 0: PSU1, 1: PSU2 */ + struct ipmi_data ipmi; + struct ipmi_psu_resp_data ipmi_resp[2]; /* 0: PSU1, 1: PSU2 */ + unsigned char ipmi_tx_data[2]; +}; + +struct as9947_36xkb_psu_data *data = NULL; + +static struct platform_driver as9947_36xkb_psu_driver = { + .probe = as9947_36xkb_psu_probe, + .remove = as9947_36xkb_psu_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +#define PSU_PRESENT_ATTR_ID(index) PSU##index##_PRESENT +#define PSU_POWERGOOD_ATTR_ID(index) PSU##index##_POWER_GOOD +#define PSU_VIN_ATTR_ID(index) PSU##index##_VIN +#define PSU_VOUT_ATTR_ID(index) PSU##index##_VOUT +#define PSU_IIN_ATTR_ID(index) PSU##index##_IIN +#define PSU_IOUT_ATTR_ID(index) PSU##index##_IOUT +#define PSU_PIN_ATTR_ID(index) PSU##index##_PIN +#define PSU_POUT_ATTR_ID(index) PSU##index##_POUT +#define PSU_MODEL_ATTR_ID(index) PSU##index##_MODEL +#define PSU_SERIAL_ATTR_ID(index) PSU##index##_SERIAL +#define PSU_TEMP1_INPUT_ATTR_ID(index) PSU##index##_TEMP1_INPUT +#define PSU_TEMP2_INPUT_ATTR_ID(index) PSU##index##_TEMP2_INPUT +#define PSU_TEMP3_INPUT_ATTR_ID(index) PSU##index##_TEMP3_INPUT +#define PSU_FAN_INPUT_ATTR_ID(index) PSU##index##_FAN_INPUT +#define PSU_FAN_DIR_ATTR_ID(index) PSU##index##_FAN_DIR + +#define PSU_TEMP1_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP1_INPUT_MAX +#define PSU_TEMP1_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP1_INPUT_MIN +#define PSU_TEMP2_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP2_INPUT_MAX +#define PSU_TEMP2_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP2_INPUT_MIN +#define PSU_TEMP3_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP3_INPUT_MAX +#define PSU_TEMP3_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP3_INPUT_MIN +#define PSU_VIN_MAX_ATTR_ID(index) PSU##index##_VIN_MAX +#define PSU_VIN_MIN_ATTR_ID(index) PSU##index##_VIN_MIN +#define PSU_VIN_UPPER_CRIT_ATTR_ID(index) PSU##index##_VIN_UPPER_CRIT +#define PSU_VIN_LOWER_CRIT_ATTR_ID(index) PSU##index##_VIN_LOWER_CRIT +#define PSU_VOUT_MAX_ATTR_ID(index) PSU##index##_VOUT_MAX +#define PSU_VOUT_MIN_ATTR_ID(index) PSU##index##_VOUT_MIN +#define PSU_IIN_MAX_ATTR_ID(index) PSU##index##_IIN_MAX +#define PSU_IOUT_MAX_ATTR_ID(index) PSU##index##_IOUT_MAX +#define PSU_PIN_MAX_ATTR_ID(index) PSU##index##_PIN_MAX +#define PSU_POUT_MAX_ATTR_ID(index) PSU##index##_POUT_MAX + +#define PSU_ATTR(psu_id) \ + PSU_PRESENT_ATTR_ID(psu_id), \ + PSU_POWERGOOD_ATTR_ID(psu_id), \ + PSU_VIN_ATTR_ID(psu_id), \ + PSU_VOUT_ATTR_ID(psu_id), \ + PSU_IIN_ATTR_ID(psu_id), \ + PSU_IOUT_ATTR_ID(psu_id), \ + PSU_PIN_ATTR_ID(psu_id), \ + PSU_POUT_ATTR_ID(psu_id), \ + PSU_MODEL_ATTR_ID(psu_id), \ + PSU_SERIAL_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_ATTR_ID(psu_id), \ + PSU_FAN_INPUT_ATTR_ID(psu_id), \ + PSU_FAN_DIR_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_VIN_MAX_ATTR_ID(psu_id), \ + PSU_VIN_MIN_ATTR_ID(psu_id), \ + PSU_VIN_UPPER_CRIT_ATTR_ID(psu_id), \ + PSU_VIN_LOWER_CRIT_ATTR_ID(psu_id), \ + PSU_VOUT_MAX_ATTR_ID(psu_id), \ + PSU_VOUT_MIN_ATTR_ID(psu_id), \ + PSU_IIN_MAX_ATTR_ID(psu_id), \ + PSU_IOUT_MAX_ATTR_ID(psu_id), \ + PSU_PIN_MAX_ATTR_ID(psu_id), \ + PSU_POUT_MAX_ATTR_ID(psu_id) + +enum as9947_36xkb_psu_sysfs_attrs { + /* psu attributes */ + PSU_ATTR(1), + PSU_ATTR(2), + NUM_OF_PSU_ATTR, + NUM_OF_PER_PSU_ATTR = (NUM_OF_PSU_ATTR/NUM_OF_PSU) +}; + +/* psu attributes */ +#define DECLARE_PSU_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(psu##index##_present, S_IRUGO, show_psu, NULL, \ + PSU##index##_PRESENT); \ + static SENSOR_DEVICE_ATTR(psu##index##_power_good, S_IRUGO, show_psu, NULL,\ + PSU##index##_POWER_GOOD); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin, S_IRUGO, show_psu, NULL, \ + PSU##index##_VIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout, S_IRUGO, show_psu, NULL, \ + PSU##index##_VOUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_iin, S_IRUGO, show_psu, NULL, \ + PSU##index##_IIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_iout, S_IRUGO, show_psu, NULL, \ + PSU##index##_IOUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_pin, S_IRUGO, show_psu, NULL, \ + PSU##index##_PIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_pout, S_IRUGO, show_psu, NULL, \ + PSU##index##_POUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_model, S_IRUGO, show_string, NULL, \ + PSU##index##_MODEL); \ + static SENSOR_DEVICE_ATTR(psu##index##_serial, S_IRUGO, show_string, NULL,\ + PSU##index##_SERIAL);\ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input, S_IRUGO, show_psu,NULL,\ + PSU##index##_TEMP1_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input, S_IRUGO, show_psu,NULL,\ + PSU##index##_TEMP2_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input, S_IRUGO, show_psu,NULL,\ + PSU##index##_TEMP3_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_fan1_input, S_IRUGO, show_psu, NULL,\ + PSU##index##_FAN_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_fan_dir, S_IRUGO, show_string, NULL,\ + PSU##index##_FAN_DIR); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP1_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP1_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP2_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP2_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP3_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP3_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_upper_crit, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_UPPER_CRIT); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_lower_crit, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_LOWER_CRIT); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VOUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VOUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_iin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_IIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_iout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_IOUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_pin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_PIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_pout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_POUT_MAX) + +#define DECLARE_PSU_ATTR(index) \ + &sensor_dev_attr_psu##index##_present.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_power_good.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_model.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_serial.dev_attr.attr,\ + &sensor_dev_attr_psu##index##_temp1_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_fan1_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_fan_dir.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp1_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp1_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_upper_crit.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_lower_crit.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vout_max.dev_attr.attr,\ + &sensor_dev_attr_psu##index##_vout_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iout_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pout_max.dev_attr.attr + +DECLARE_PSU_SENSOR_DEVICE_ATTR(1); +/*Duplicate nodes for lm-sensors.*/ +static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_psu, NULL, PSU1_VOUT); +static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, show_psu, NULL, PSU1_IOUT); +static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, show_psu, NULL, PSU1_POUT); +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_psu, NULL, PSU1_TEMP1_INPUT); +static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_psu, NULL, PSU1_FAN_INPUT); + +static struct attribute *as9947_36xkb_psu1_attrs[] = { + /* psu attributes */ + DECLARE_PSU_ATTR(1), + &sensor_dev_attr_curr1_input.dev_attr.attr, + &sensor_dev_attr_in0_input.dev_attr.attr, + &sensor_dev_attr_power1_input.dev_attr.attr, + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_fan1_input.dev_attr.attr, + NULL +}; +static struct attribute_group as9947_36xkb_psu1_group = { + .attrs = as9947_36xkb_psu1_attrs, +}; +/* ATTRIBUTE_GROUPS(as9947_36xkb_psu1); */ + +DECLARE_PSU_SENSOR_DEVICE_ATTR(2); +/*Duplicate nodes for lm-sensors.*/ +static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_psu, NULL, PSU2_VOUT); +static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, show_psu, NULL, PSU2_IOUT); +static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, show_psu, NULL, PSU2_POUT); +static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_psu, NULL, PSU2_TEMP1_INPUT); +static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_psu, NULL, PSU2_FAN_INPUT); +static struct attribute *as9947_36xkb_psu2_attrs[] = { + /* psu attributes */ + DECLARE_PSU_ATTR(2), + &sensor_dev_attr_curr2_input.dev_attr.attr, + &sensor_dev_attr_in1_input.dev_attr.attr, + &sensor_dev_attr_power2_input.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_fan2_input.dev_attr.attr, + NULL +}; +static struct attribute_group as9947_36xkb_psu2_group = { + .attrs = as9947_36xkb_psu2_attrs, +}; +/* ATTRIBUTE_GROUPS(as9947_36xkb_psu2); */ + +const struct attribute_group *as9947_36xkb_psu_groups[][2] = { + {&as9947_36xkb_psu1_group, NULL}, + {&as9947_36xkb_psu2_group, NULL} +}; + + +static struct as9947_36xkb_psu_data *as9947_36xkb_psu_update_device(struct device_attribute *da) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + int status = 0; + + if (time_before(jiffies, data->last_updated[pid] + HZ * 5) && data->valid[pid]) + return data; + + data->valid[pid] = 0; + /* To be compatible for older BMC firmware */ + data->ipmi_resp[pid].status[PSU_VOUT_MODE] = 0xff; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = pid + 1; /* PSU ID base id for ipmi start from 1 */ + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp[pid].status, + sizeof(data->ipmi_resp[pid].status)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get model name from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_MODEL_NAME_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].model, + sizeof(data->ipmi_resp[pid].model) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get serial number from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_SERIAL_NUM_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].serial, + sizeof(data->ipmi_resp[pid].serial) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get fan direction from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_FAN_DIR_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].fandir, + sizeof(data->ipmi_resp[pid].fandir) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get capability from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_INFO_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].info, + sizeof(data->ipmi_resp[pid].info)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated[pid] = jiffies; + data->valid[pid] = 1; + +exit: + return data; +} + +#define VALIDATE_PRESENT_RETURN(id) \ +do { \ + if (data->ipmi_resp[id].status[PSU_PRESENT] == 0) { \ + mutex_unlock(&data->update_lock); \ + return -ENXIO; \ + } \ +} while (0) + +static ssize_t show_psu(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + u32 value = 0; + int present = 0; + int error = 0; + int multiplier = 1000; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + present = !!(data->ipmi_resp[pid].status[PSU_PRESENT]); + + switch (attr->index) { + case PSU1_PRESENT: + case PSU2_PRESENT: + value = present; + break; + case PSU1_POWER_GOOD: + case PSU2_POWER_GOOD: + VALIDATE_PRESENT_RETURN(pid); + value = data->ipmi_resp[pid].status[PSU_POWER_GOOD_PMBUS]; + break; + case PSU1_IIN: + case PSU2_IIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_IIN0] | + (u32)data->ipmi_resp[pid].status[PSU_IIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_IIN2] << 16); + break; + case PSU1_IOUT: + case PSU2_IOUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_IOUT0] | + (u32)data->ipmi_resp[pid].status[PSU_IOUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_IOUT2] << 16); + break; + case PSU1_VIN: + case PSU2_VIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_VIN0] | + (u32)data->ipmi_resp[pid].status[PSU_VIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_VIN2] << 16); + break; + case PSU1_VOUT: + case PSU2_VOUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_VOUT0] | + (u32)data->ipmi_resp[pid].status[PSU_VOUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_VOUT2] << 16); + break; + case PSU1_PIN: + case PSU2_PIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_PIN0] | + (u32)data->ipmi_resp[pid].status[PSU_PIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_PIN2] << 16 | + (u32)data->ipmi_resp[pid].status[PSU_PIN3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_POUT: + case PSU2_POUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_POUT0] | + (u32)data->ipmi_resp[pid].status[PSU_POUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_POUT2] << 16 | + (u32)data->ipmi_resp[pid].status[PSU_POUT3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_TEMP1_INPUT: + case PSU2_TEMP1_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP1_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP1_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT: + case PSU2_TEMP2_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP2_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP2_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT: + case PSU2_TEMP3_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP3_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP3_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_FAN_INPUT: + case PSU2_FAN_INPUT: + VALIDATE_PRESENT_RETURN(pid); + multiplier = 1; + value = ((u32)data->ipmi_resp[pid].status[PSU_FAN0] | + (u32)data->ipmi_resp[pid].status[PSU_FAN1] << 8); + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + + return sprintf(buf, "%d\n", present ? value : 0); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_psu_info(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + s32 value = 0; + int present = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + present = !!(data->ipmi_resp[pid].status[PSU_PRESENT]); + + switch (attr->index) { + case PSU1_TEMP1_INPUT_MAX: + case PSU2_TEMP1_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP1_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP1_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP1_INPUT_MIN: + case PSU2_TEMP1_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP1_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP1_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT_MAX: + case PSU2_TEMP2_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP2_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP2_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT_MIN: + case PSU2_TEMP2_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP2_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP2_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT_MAX: + case PSU2_TEMP3_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP3_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP3_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT_MIN: + case PSU2_TEMP3_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP3_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP3_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_VIN_UPPER_CRIT: + case PSU2_VIN_UPPER_CRIT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT2] << 16); + break; + case PSU1_VIN_LOWER_CRIT: + case PSU2_VIN_LOWER_CRIT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT2] << 16); + break; + case PSU1_VIN_MAX: + case PSU2_VIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MAX2] << 16); + break; + case PSU1_VIN_MIN: + case PSU2_VIN_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_MIN0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MIN1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MIN2] << 16); + break; + case PSU1_VOUT_MAX: + case PSU2_VOUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX2] << 16); + break; + case PSU1_VOUT_MIN: + case PSU2_VOUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN0] | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN2] << 16); + break; + case PSU1_IIN_MAX: + case PSU2_IIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_IIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_IIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_IIN_MAX2] << 16); + break; + case PSU1_IOUT_MAX: + case PSU2_IOUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX2] << 16); + break; + case PSU1_PIN_MAX: + case PSU2_PIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_PIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX2] << 16 | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_POUT_MAX: + case PSU2_POUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_POUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX2] << 16 | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX3] << 24); + value /= 1000; // Convert to milliwatt + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + + return sprintf(buf, "%d\n", present ? value : 0); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_string(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + char *str = NULL; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + switch (attr->index) { + case PSU1_MODEL: + case PSU2_MODEL: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].model; + break; + case PSU1_SERIAL: + case PSU2_SERIAL: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].serial; + break; + case PSU1_FAN_DIR: + case PSU2_FAN_DIR: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].fandir; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%s\n", str); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static int as9947_36xkb_psu_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev = NULL; + int i = 0; + + for (i = 0; i < 2 ; i++) { + hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, DRVNAME, + NULL, as9947_36xkb_psu_groups[i]); + if (IS_ERR(hwmon_dev)) { + status = PTR_ERR(hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev[i] = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "PSU%d device created\n", i + 1); + } + + return 0; +} + +static void as9947_36xkb_psu_remove(struct platform_device *pdev) +{ + int i = 0; + + for(i = 0; i < 2 ; i++) { + mutex_lock(&data->update_lock); + if (data->hwmon_dev[i]) { + hwmon_device_unregister(data->hwmon_dev[i]); + data->hwmon_dev[i] = NULL; + } + mutex_unlock(&data->update_lock); + } +} + +static int __init as9947_36xkb_psu_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_psu_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9947_36xkb_psu_driver); + if (ret < 0) + goto dri_reg_err; + + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) { + goto ipmi_err; + } + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_psu_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_psu_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9947_36xkb_psu_driver); + kfree(data); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9947_36xkb_psu driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_psu_init); +module_exit(as9947_36xkb_psu_exit); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sfp.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sfp.c new file mode 100644 index 000000000..cbdfed2ba --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sfp.c @@ -0,0 +1,1058 @@ +/* + * Copyright (C) Willy Liu + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9947_36xkb_sfp" +/* SFP */ +#define IPMI_SFP_READ_CMD 0x1C +#define IPMI_SFP_WRITE_CMD 0x1D + +#define IPMI_SFP_PRESENT_CMD 0x10 +#define IPMI_SFP_TXDIS_CMD 0x1 +#define IPMI_SFP_TXFAULT_CMD 0x12 +#define IPMI_SFP_RXLOS_CMD 0x13 +/* QSFP */ +#define IPMI_QSFP_READ_CMD 0x10 +#define IPMI_QSFP_WRITE_CMD 0x11 + +#define IPMI_QSFP_PRESENT_CMD 0x10 +#define IPMI_QSFP_RESET_CMD 0x11 +#define IPMI_QSFP_LPMODE_CMD 0x12 + +#define IPMI_DATA_MAX_LEN 128 + +#define NUM_OF_SFP 4 +#define NUM_OF_QSFP 36 +#define NUM_OF_PORT (NUM_OF_SFP + NUM_OF_QSFP) + +static ssize_t set_sfp_txdisable(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_sfp(struct device *dev, struct device_attribute *da, char *buf); + +static ssize_t set_qsfp_reset(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t set_qsfp_lpmode(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_qsfp(struct device *dev, struct device_attribute *da, char *buf); +static int as9947_36xkb_sfp_probe(struct platform_device *pdev); +static void as9947_36xkb_sfp_remove(struct platform_device *pdev); +static ssize_t show_all(struct device *dev, struct device_attribute *da, char *buf); +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_present(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_txdisable(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_txfault(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_rxlos(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_present(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_lpmode(void); +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_reset(void); + +enum module_status { + SFP_PRESENT = 0, + SFP_TXDISABLE, + SFP_TXFAULT, + SFP_RXLOS, + NUM_OF_SFP_STATUS, + + QSFP_PRESENT = 0, + QSFP_RESET, + QSFP_LPMODE, + NUM_OF_QSFP_STATUS, + + PRESENT_ALL = 0, + RXLOS_ALL, +}; + +struct ipmi_sfp_resp_data { + char sfp_valid[NUM_OF_SFP_STATUS]; /* != 0 if registers are valid */ + unsigned long sfp_last_updated[NUM_OF_SFP_STATUS]; /* In jiffies */ + unsigned char sfp_resp[NUM_OF_SFP_STATUS][NUM_OF_SFP]; /* 0: present, 1: tx-disable + 2: tx-fault, 3: rx-los */ + char qsfp_valid[NUM_OF_QSFP_STATUS]; /* != 0 if registers are valid */ + unsigned long qsfp_last_updated[NUM_OF_QSFP_STATUS]; /* In jiffies */ + unsigned char qsfp_resp[NUM_OF_QSFP_STATUS][NUM_OF_QSFP]; /* 0: present, 1: reset, + 2: low power mode */ +}; + +struct as9947_36xkb_sfp_data { + struct platform_device *pdev; + struct mutex update_lock; + struct ipmi_data ipmi; + struct ipmi_sfp_resp_data ipmi_resp; + unsigned char ipmi_tx_data[3]; +}; + +struct as9947_36xkb_sfp_data *data = NULL; + +static struct platform_driver as9947_36xkb_sfp_driver = { + .probe = as9947_36xkb_sfp_probe, + .remove = as9947_36xkb_sfp_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +#define SFP_PRESENT_ATTR_ID(port) SFP##port##_PRESENT +#define SFP_TXDISABLE_ATTR_ID(port) SFP##port##_TXDISABLE +#define SFP_TXFAULT_ATTR_ID(port) SFP##port##_TXFAULT +#define SFP_RXLOS_ATTR_ID(port) SFP##port##_RXLOS + +#define SFP_ATTR(port) \ + SFP_PRESENT_ATTR_ID(port), \ + SFP_TXDISABLE_ATTR_ID(port), \ + SFP_TXFAULT_ATTR_ID(port), \ + SFP_RXLOS_ATTR_ID(port) + +#define QSFP_PRESENT_ATTR_ID(port) QSFP##port##_PRESENT +#define QSFP_RESET_ATTR_ID(port) QSFP##port##_RESET +#define QSFP_LPMODE_ATTR_ID(port) QSFP##port##_LPMODE + +#define QSFP_ATTR(port) \ + QSFP_PRESENT_ATTR_ID(port), \ + QSFP_RESET_ATTR_ID(port), \ + QSFP_LPMODE_ATTR_ID(port) + +enum as9947_36xkb_sfp_sysfs_attrs { + SFP_ATTR(1), + SFP_ATTR(2), + SFP_ATTR(3), + SFP_ATTR(4), + NUM_OF_SFP_ATTR, + NUM_OF_PER_SFP_ATTR = (NUM_OF_SFP_ATTR/NUM_OF_SFP), +}; + +enum as9947_36xkb_qsfp_sysfs_attrs { + QSFP_ATTR(5), + QSFP_ATTR(6), + QSFP_ATTR(7), + QSFP_ATTR(8), + QSFP_ATTR(9), + QSFP_ATTR(10), + QSFP_ATTR(11), + QSFP_ATTR(12), + QSFP_ATTR(13), + QSFP_ATTR(14), + QSFP_ATTR(15), + QSFP_ATTR(16), + QSFP_ATTR(17), + QSFP_ATTR(18), + QSFP_ATTR(19), + QSFP_ATTR(20), + QSFP_ATTR(21), + QSFP_ATTR(22), + QSFP_ATTR(23), + QSFP_ATTR(24), + QSFP_ATTR(25), + QSFP_ATTR(26), + QSFP_ATTR(27), + QSFP_ATTR(28), + QSFP_ATTR(29), + QSFP_ATTR(30), + QSFP_ATTR(31), + QSFP_ATTR(32), + QSFP_ATTR(33), + QSFP_ATTR(34), + QSFP_ATTR(35), + QSFP_ATTR(36), + QSFP_ATTR(37), + QSFP_ATTR(38), + QSFP_ATTR(39), + QSFP_ATTR(40), + NUM_OF_QSFP_ATTR, + NUM_OF_PER_QSFP_ATTR = (NUM_OF_QSFP_ATTR/NUM_OF_QSFP), +}; + +/* sfp attributes */ +#define DECLARE_SFP_SENSOR_DEVICE_ATTR(port) \ + static SENSOR_DEVICE_ATTR(module_present_##port, S_IRUGO, show_sfp, NULL, SFP##port##_PRESENT); \ + static SENSOR_DEVICE_ATTR(module_tx_disable_##port, S_IWUSR | S_IRUGO, show_sfp, set_sfp_txdisable, SFP##port##_TXDISABLE); \ + static SENSOR_DEVICE_ATTR(module_tx_fault_##port, S_IRUGO, show_sfp, NULL, SFP##port##_TXFAULT); \ + static SENSOR_DEVICE_ATTR(module_rx_los_##port, S_IRUGO, show_sfp, NULL, SFP##port##_RXLOS) +#define DECLARE_SFP_ATTR(port) \ + &sensor_dev_attr_module_present_##port.dev_attr.attr, \ + &sensor_dev_attr_module_tx_disable_##port.dev_attr.attr, \ + &sensor_dev_attr_module_tx_fault_##port.dev_attr.attr, \ + &sensor_dev_attr_module_rx_los_##port.dev_attr.attr + +/* qsfp attributes */ +#define DECLARE_QSFP_SENSOR_DEVICE_ATTR(port) \ + static SENSOR_DEVICE_ATTR(module_present_##port, S_IRUGO, show_qsfp, NULL, QSFP##port##_PRESENT); \ + static SENSOR_DEVICE_ATTR(module_reset_##port, S_IWUSR | S_IRUGO, show_qsfp, set_qsfp_reset, QSFP##port##_RESET); \ + static SENSOR_DEVICE_ATTR(module_lpmode_##port, S_IWUSR | S_IRUGO, show_qsfp, set_qsfp_lpmode, QSFP##port##_LPMODE) +#define DECLARE_QSFP_ATTR(port) \ + &sensor_dev_attr_module_present_##port.dev_attr.attr, \ + &sensor_dev_attr_module_reset_##port.dev_attr.attr, \ + &sensor_dev_attr_module_lpmode_##port.dev_attr.attr + +static SENSOR_DEVICE_ATTR(module_present_all, S_IRUGO, show_all, NULL, PRESENT_ALL); +static SENSOR_DEVICE_ATTR(module_rxlos_all, S_IRUGO, show_all, NULL, RXLOS_ALL); + +DECLARE_SFP_SENSOR_DEVICE_ATTR(1); +DECLARE_SFP_SENSOR_DEVICE_ATTR(2); +DECLARE_SFP_SENSOR_DEVICE_ATTR(3); +DECLARE_SFP_SENSOR_DEVICE_ATTR(4); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(5); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(6); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(7); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(8); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(9); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(10); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(11); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(12); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(13); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(14); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(15); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(16); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(17); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(18); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(19); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(20); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(21); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(22); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(23); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(24); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(25); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(26); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(27); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(28); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(29); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(30); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(31); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(32); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(33); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(34); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(35); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(36); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(37); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(38); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(39); +DECLARE_QSFP_SENSOR_DEVICE_ATTR(40); + +static struct attribute *as9947_36xkb_sfp_attributes[] = { + /* sfp attributes */ + DECLARE_SFP_ATTR(1), + DECLARE_SFP_ATTR(2), + DECLARE_SFP_ATTR(3), + DECLARE_SFP_ATTR(4), + DECLARE_QSFP_ATTR(5), + DECLARE_QSFP_ATTR(6), + DECLARE_QSFP_ATTR(7), + DECLARE_QSFP_ATTR(8), + DECLARE_QSFP_ATTR(9), + DECLARE_QSFP_ATTR(10), + DECLARE_QSFP_ATTR(11), + DECLARE_QSFP_ATTR(12), + DECLARE_QSFP_ATTR(13), + DECLARE_QSFP_ATTR(14), + DECLARE_QSFP_ATTR(15), + DECLARE_QSFP_ATTR(16), + DECLARE_QSFP_ATTR(17), + DECLARE_QSFP_ATTR(18), + DECLARE_QSFP_ATTR(19), + DECLARE_QSFP_ATTR(20), + DECLARE_QSFP_ATTR(21), + DECLARE_QSFP_ATTR(22), + DECLARE_QSFP_ATTR(23), + DECLARE_QSFP_ATTR(24), + DECLARE_QSFP_ATTR(25), + DECLARE_QSFP_ATTR(26), + DECLARE_QSFP_ATTR(27), + DECLARE_QSFP_ATTR(28), + DECLARE_QSFP_ATTR(29), + DECLARE_QSFP_ATTR(30), + DECLARE_QSFP_ATTR(31), + DECLARE_QSFP_ATTR(32), + DECLARE_QSFP_ATTR(33), + DECLARE_QSFP_ATTR(34), + DECLARE_QSFP_ATTR(35), + DECLARE_QSFP_ATTR(36), + DECLARE_QSFP_ATTR(37), + DECLARE_QSFP_ATTR(38), + DECLARE_QSFP_ATTR(39), + DECLARE_QSFP_ATTR(40), + &sensor_dev_attr_module_present_all.dev_attr.attr, + &sensor_dev_attr_module_rxlos_all.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9947_36xkb_sfp_group = { + .attrs = as9947_36xkb_sfp_attributes, +}; + +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_present(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.sfp_last_updated[SFP_PRESENT] + HZ) && + data->ipmi_resp.sfp_valid[SFP_PRESENT]) { + return data; + } + + data->ipmi_resp.sfp_valid[SFP_PRESENT] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_SFP_PRESENT_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_SFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.sfp_resp[SFP_PRESENT], + sizeof(data->ipmi_resp.sfp_resp[SFP_PRESENT])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.sfp_last_updated[SFP_PRESENT] = jiffies; + data->ipmi_resp.sfp_valid[SFP_PRESENT] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_txdisable(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.sfp_last_updated[SFP_TXDISABLE] + HZ * 5) && + data->ipmi_resp.sfp_valid[SFP_TXDISABLE]) { + return data; + } + + data->ipmi_resp.sfp_valid[SFP_TXDISABLE] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_SFP_TXDIS_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_SFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.sfp_resp[SFP_TXDISABLE], + sizeof(data->ipmi_resp.sfp_resp[SFP_TXDISABLE])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.sfp_last_updated[SFP_TXDISABLE] = jiffies; + data->ipmi_resp.sfp_valid[SFP_TXDISABLE] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_txfault(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.sfp_last_updated[SFP_TXFAULT] + HZ * 5) && + data->ipmi_resp.sfp_valid[SFP_TXFAULT]) { + return data; + } + + data->ipmi_resp.sfp_valid[SFP_TXFAULT] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_SFP_TXFAULT_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_SFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.sfp_resp[SFP_TXFAULT], + sizeof(data->ipmi_resp.sfp_resp[SFP_TXFAULT])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.sfp_last_updated[SFP_TXFAULT] = jiffies; + data->ipmi_resp.sfp_valid[SFP_TXFAULT] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_sfp_update_rxlos(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.sfp_last_updated[SFP_RXLOS] + HZ * 5) && + data->ipmi_resp.sfp_valid[SFP_RXLOS]) { + return data; + } + + data->ipmi_resp.sfp_valid[SFP_RXLOS] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_SFP_RXLOS_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_SFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.sfp_resp[SFP_RXLOS], + sizeof(data->ipmi_resp.sfp_resp[SFP_RXLOS])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.sfp_last_updated[SFP_RXLOS] = jiffies; + data->ipmi_resp.sfp_valid[SFP_RXLOS] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_present(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.qsfp_last_updated[QSFP_PRESENT] + HZ) && + data->ipmi_resp.qsfp_valid[QSFP_PRESENT]) { + return data; + } + + data->ipmi_resp.qsfp_valid[QSFP_PRESENT] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_QSFP_PRESENT_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_QSFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.qsfp_resp[QSFP_PRESENT], + sizeof(data->ipmi_resp.qsfp_resp[QSFP_PRESENT])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.qsfp_last_updated[QSFP_PRESENT] = jiffies; + data->ipmi_resp.qsfp_valid[QSFP_PRESENT] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_reset(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.qsfp_last_updated[QSFP_RESET] + HZ * 5) && + data->ipmi_resp.qsfp_valid[QSFP_RESET]) { + return data; + } + + data->ipmi_resp.qsfp_valid[QSFP_RESET] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_QSFP_RESET_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_QSFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.qsfp_resp[QSFP_RESET], + sizeof(data->ipmi_resp.qsfp_resp[QSFP_RESET])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.qsfp_last_updated[QSFP_RESET] = jiffies; + data->ipmi_resp.qsfp_valid[QSFP_RESET] = 1; + +exit: + return data; +} + +static struct as9947_36xkb_sfp_data *as9947_36xkb_qsfp_update_lpmode(void) +{ + int status = 0; + + if (time_before(jiffies, data->ipmi_resp.qsfp_last_updated[QSFP_LPMODE] + HZ * 5) && + data->ipmi_resp.qsfp_valid[QSFP_LPMODE]) { + return data; + } + + data->ipmi_resp.qsfp_valid[QSFP_LPMODE] = 0; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = IPMI_QSFP_LPMODE_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_QSFP_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp.qsfp_resp[QSFP_LPMODE], + sizeof(data->ipmi_resp.qsfp_resp[QSFP_LPMODE])); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->ipmi_resp.qsfp_last_updated[QSFP_LPMODE] = jiffies; + data->ipmi_resp.qsfp_valid[QSFP_LPMODE] = 1; + +exit: + return data; +} + +static ssize_t show_all(struct device *dev, struct device_attribute *da, char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + u64 values = 0; + int i; + + switch (attr->index) { + case PRESENT_ALL: + { + mutex_lock(&data->update_lock); + + data = as9947_36xkb_sfp_update_present(); + if (!data->ipmi_resp.sfp_valid[SFP_PRESENT]) { + mutex_unlock(&data->update_lock); + return -EIO; + } + + data = as9947_36xkb_qsfp_update_present(); + if (!data->ipmi_resp.qsfp_valid[QSFP_PRESENT]) { + mutex_unlock(&data->update_lock); + return -EIO; + } + + /* Update qsfp present status */ + for (i = (NUM_OF_QSFP-1); i >= 0; i--) { + values <<= 1; + values |= (data->ipmi_resp.qsfp_resp[QSFP_PRESENT][i] & 0x1); + } + + /* Update sfp present status */ + for (i = (NUM_OF_SFP-1); i >= 0; i--) { + values <<= 1; + values |= (data->ipmi_resp.sfp_resp[SFP_PRESENT][i] & 0x1); + } + + mutex_unlock(&data->update_lock); + + /* Return values 1 -> 54 in order */ + return sprintf(buf, "%.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", + (unsigned int)(0xFF & values), + (unsigned int)(0xFF & (values >> 8)), + (unsigned int)(0xFF & (values >> 16)), + (unsigned int)(0xFF & (values >> 24)), + (unsigned int)(0xFF & (values >> 32)), + (unsigned int)(0xFF & (values >> 40)), + (unsigned int)(0x3F & (values >> 48))); + } + case RXLOS_ALL: + { + mutex_lock(&data->update_lock); + + data = as9947_36xkb_sfp_update_rxlos(); + if (!data->ipmi_resp.sfp_valid[SFP_RXLOS]) { + mutex_unlock(&data->update_lock); + return -EIO; + } + + /* Update sfp rxlos status */ + for (i = (NUM_OF_SFP-1); i >= 0; i--) { + values <<= 1; + values |= (data->ipmi_resp.sfp_resp[SFP_RXLOS][i] & 0x1); + } + + mutex_unlock(&data->update_lock); + + /* Return values 1 -> 48 in order */ + return sprintf(buf, "%.2x %.2x %.2x %.2x %.2x %.2x\n", + (unsigned int)(0xFF & values), + (unsigned int)(0xFF & (values >> 8)), + (unsigned int)(0xFF & (values >> 16)), + (unsigned int)(0xFF & (values >> 24)), + (unsigned int)(0xFF & (values >> 32)), + (unsigned int)(0xFF & (values >> 40))); + } + default: + break; + } + + return 0; +} + +static ssize_t show_sfp(struct device *dev, struct device_attribute *da, char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_SFP_ATTR; /* port id, 0 based */ + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + switch (attr->index) { + case SFP1_PRESENT: + case SFP2_PRESENT: + case SFP3_PRESENT: + case SFP4_PRESENT: + { + data = as9947_36xkb_sfp_update_present(); + if (!data->ipmi_resp.sfp_valid[SFP_PRESENT]) { + error = -EIO; + goto exit; + } + + value = data->ipmi_resp.sfp_resp[SFP_PRESENT][pid]; + break; + } + case SFP1_TXDISABLE: + case SFP2_TXDISABLE: + case SFP3_TXDISABLE: + case SFP4_TXDISABLE: + { + data = as9947_36xkb_sfp_update_txdisable(); + if (!data->ipmi_resp.sfp_valid[SFP_TXDISABLE]) { + error = -EIO; + goto exit; + } + + value = !data->ipmi_resp.sfp_resp[SFP_TXDISABLE][pid]; + break; + } + case SFP1_TXFAULT: + case SFP2_TXFAULT: + case SFP3_TXFAULT: + case SFP4_TXFAULT: + { + data = as9947_36xkb_sfp_update_txfault(); + if (!data->ipmi_resp.sfp_valid[SFP_TXFAULT]) { + error = -EIO; + goto exit; + } + + value = data->ipmi_resp.sfp_resp[SFP_TXFAULT][pid]; + break; + } + case SFP1_RXLOS: + case SFP2_RXLOS: + case SFP3_RXLOS: + case SFP4_RXLOS: + { + data = as9947_36xkb_sfp_update_rxlos(); + if (!data->ipmi_resp.sfp_valid[SFP_RXLOS]) { + error = -EIO; + goto exit; + } + + value = data->ipmi_resp.sfp_resp[SFP_RXLOS][pid]; + break; + } + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_sfp_txdisable(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long disable; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_SFP_ATTR; /* port id, 0 based */ + + status = kstrtol(buf, 10, &disable); + if (status) { + return status; + } + + disable = !disable; /* the IPMI cmd is 0 for tx-disable and 1 for tx-enable */ + + mutex_lock(&data->update_lock); + + /* Send IPMI write command */ + data->ipmi_tx_data[0] = pid + 1; /* Port ID base id for ipmi start from 1 */ + data->ipmi_tx_data[1] = IPMI_SFP_TXDIS_CMD; + data->ipmi_tx_data[2] = disable; + status = ipmi_send_message(&data->ipmi, IPMI_SFP_WRITE_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), NULL, 0); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Update to ipmi_resp buffer to prevent from the impact of lazy update */ + data->ipmi_resp.sfp_resp[SFP_TXDISABLE][pid] = disable; + status = count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static ssize_t show_qsfp(struct device *dev, struct device_attribute *da, char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_QSFP_ATTR; /* port id, 0 based */ + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + switch (attr->index) { + case QSFP5_PRESENT: + case QSFP6_PRESENT: + case QSFP7_PRESENT: + case QSFP8_PRESENT: + case QSFP9_PRESENT: + case QSFP10_PRESENT: + case QSFP11_PRESENT: + case QSFP12_PRESENT: + case QSFP13_PRESENT: + case QSFP14_PRESENT: + case QSFP15_PRESENT: + case QSFP16_PRESENT: + case QSFP17_PRESENT: + case QSFP18_PRESENT: + case QSFP19_PRESENT: + case QSFP20_PRESENT: + case QSFP21_PRESENT: + case QSFP22_PRESENT: + case QSFP23_PRESENT: + case QSFP24_PRESENT: + case QSFP25_PRESENT: + case QSFP26_PRESENT: + case QSFP27_PRESENT: + case QSFP28_PRESENT: + case QSFP29_PRESENT: + case QSFP30_PRESENT: + case QSFP31_PRESENT: + case QSFP32_PRESENT: + case QSFP33_PRESENT: + case QSFP34_PRESENT: + case QSFP35_PRESENT: + case QSFP36_PRESENT: + case QSFP37_PRESENT: + case QSFP38_PRESENT: + case QSFP39_PRESENT: + case QSFP40_PRESENT: + { + data = as9947_36xkb_qsfp_update_present(); + if (!data->ipmi_resp.qsfp_valid[QSFP_PRESENT]) { + error = -EIO; + goto exit; + } + + value = data->ipmi_resp.qsfp_resp[QSFP_PRESENT][pid]; + break; + } + case QSFP5_RESET: + case QSFP6_RESET: + case QSFP7_RESET: + case QSFP8_RESET: + case QSFP9_RESET: + case QSFP10_RESET: + case QSFP11_RESET: + case QSFP12_RESET: + case QSFP13_RESET: + case QSFP14_RESET: + case QSFP15_RESET: + case QSFP16_RESET: + case QSFP17_RESET: + case QSFP18_RESET: + case QSFP19_RESET: + case QSFP20_RESET: + case QSFP21_RESET: + case QSFP22_RESET: + case QSFP23_RESET: + case QSFP24_RESET: + case QSFP25_RESET: + case QSFP26_RESET: + case QSFP27_RESET: + case QSFP28_RESET: + case QSFP29_RESET: + case QSFP30_RESET: + case QSFP31_RESET: + case QSFP32_RESET: + case QSFP33_RESET: + case QSFP34_RESET: + case QSFP35_RESET: + case QSFP36_RESET: + case QSFP37_RESET: + case QSFP38_RESET: + case QSFP39_RESET: + case QSFP40_RESET: + { + data = as9947_36xkb_qsfp_update_reset(); + if (!data->ipmi_resp.qsfp_valid[QSFP_RESET]) { + error = -EIO; + goto exit; + } + + value = !data->ipmi_resp.qsfp_resp[QSFP_RESET][pid]; + break; + } + case QSFP5_LPMODE: + case QSFP6_LPMODE: + case QSFP7_LPMODE: + case QSFP8_LPMODE: + case QSFP9_LPMODE: + case QSFP10_LPMODE: + case QSFP11_LPMODE: + case QSFP12_LPMODE: + case QSFP13_LPMODE: + case QSFP14_LPMODE: + case QSFP15_LPMODE: + case QSFP16_LPMODE: + case QSFP17_LPMODE: + case QSFP18_LPMODE: + case QSFP19_LPMODE: + case QSFP20_LPMODE: + case QSFP21_LPMODE: + case QSFP22_LPMODE: + case QSFP23_LPMODE: + case QSFP24_LPMODE: + case QSFP25_LPMODE: + case QSFP26_LPMODE: + case QSFP27_LPMODE: + case QSFP28_LPMODE: + case QSFP29_LPMODE: + case QSFP30_LPMODE: + case QSFP31_LPMODE: + case QSFP32_LPMODE: + case QSFP33_LPMODE: + case QSFP34_LPMODE: + case QSFP35_LPMODE: + case QSFP36_LPMODE: + case QSFP37_LPMODE: + case QSFP38_LPMODE: + case QSFP39_LPMODE: + case QSFP40_LPMODE: + { + data = as9947_36xkb_qsfp_update_lpmode(); + if (!data->ipmi_resp.qsfp_valid[QSFP_LPMODE]) { + error = -EIO; + goto exit; + } + + value = data->ipmi_resp.qsfp_resp[QSFP_LPMODE][pid]; + break; + } + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_qsfp_reset(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long reset; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_QSFP_ATTR; /* port id, 0 based */ + + status = kstrtol(buf, 10, &reset); + if (status) { + return status; + } + + reset = !reset; /* the IPMI cmd is 0 for reset and 1 for out of reset */ + + mutex_lock(&data->update_lock); + + /* Send IPMI write command */ + data->ipmi_tx_data[0] = pid + 1; /* Port ID base id for ipmi start from 1 */ + data->ipmi_tx_data[1] = IPMI_QSFP_RESET_CMD; + data->ipmi_tx_data[2] = reset; + status = ipmi_send_message(&data->ipmi, IPMI_QSFP_WRITE_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), NULL, 0); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Update to ipmi_resp buffer to prevent from the impact of lazy update */ + data->ipmi_resp.qsfp_resp[QSFP_RESET][pid] = reset; + status = count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static ssize_t set_qsfp_lpmode(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long lpmode; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_QSFP_ATTR; /* port id, 0 based */ + + status = kstrtol(buf, 10, &lpmode); + if (status) { + return status; + } + + mutex_lock(&data->update_lock); + + /* Send IPMI write command */ + data->ipmi_tx_data[0] = pid + 1; /* Port ID base id for ipmi start from 1 */ + data->ipmi_tx_data[1] = IPMI_QSFP_LPMODE_CMD; + data->ipmi_tx_data[2] = lpmode; /* 0: High Power Mode, 1: Low Power Mode */ + status = ipmi_send_message(&data->ipmi, IPMI_QSFP_WRITE_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), NULL, 0); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Update to ipmi_resp buffer to prevent from the impact of lazy update */ + data->ipmi_resp.qsfp_resp[QSFP_LPMODE][pid] = lpmode; + status = count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static int as9947_36xkb_sfp_probe(struct platform_device *pdev) +{ + int status = -1; + + /* Register sysfs hooks */ + status = sysfs_create_group(&pdev->dev.kobj, &as9947_36xkb_sfp_group); + if (status) + goto exit; + + dev_info(&pdev->dev, "device created\n"); + + return 0; + +exit: + return status; +} + +static void as9947_36xkb_sfp_remove(struct platform_device *pdev) +{ + sysfs_remove_group(&pdev->dev.kobj, &as9947_36xkb_sfp_group); +} + +static int __init as9947_36xkb_sfp_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_sfp_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9947_36xkb_sfp_driver); + if (ret < 0) { + goto dri_reg_err; + } + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_sfp_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_sfp_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9947_36xkb_sfp_driver); + kfree(data); +} + +MODULE_AUTHOR("Brandon Chuang "); +MODULE_DESCRIPTION("AS9947-36XKB sfp driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_sfp_init); +module_exit(as9947_36xkb_sfp_exit); + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sys.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sys.c new file mode 100644 index 000000000..a6cce5923 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-sys.c @@ -0,0 +1,342 @@ +/* + * Copyright (C) Brandon Chuang + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + + +#define DRVNAME "as9947_36xkb_sys" + +#define IPMI_SYSEEPROM_READ_CMD 0x18 +#define IPMI_READ_MAX_LEN 128 + +#define EEPROM_NAME "eeprom" +#define EEPROM_SIZE 256 /* 256 byte eeprom */ + +#define IPMI_CPLD_READ_CMD 0x20 +#define IPMI_CPLD_COM_E_CMD 0x21 +#define IPMI_CPLD_FAN_CMD 0x33 +#define IPMI_CPLD_FPGA_CMD 0x60 +#define IPMI_CPLD_PORT_CPLD1_CMD 0x62 +#define IPMI_CPLD_PORT_CPLD2_CMD 0x63 + +static int as9947_36xkb_sys_probe(struct platform_device *pdev); +static void as9947_36xkb_sys_remove(struct platform_device *pdev); +static ssize_t show_cpld_version(struct device *dev, struct device_attribute *da, char *buf); + +struct as9947_36xkb_sys_data { + struct platform_device *pdev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + struct ipmi_data ipmi; + unsigned char ipmi_resp_eeprom[EEPROM_SIZE]; + unsigned char ipmi_resp_cpld[2]; + unsigned char ipmi_tx_data[2]; + struct bin_attribute eeprom; /* eeprom data */ +}; + +struct as9947_36xkb_sys_data *data = NULL; + +static struct platform_driver as9947_36xkb_sys_driver = { + .probe = as9947_36xkb_sys_probe, + .remove = as9947_36xkb_sys_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum as5916_54xks_sys_sysfs_attrs { + COM_E_CPLD, + FPGA_CPLD, + PORT_CPLD1, + PORT_CPLD2, + FAN_CPLD +}; +/* Functions to talk to the IPMI layer */ +static SENSOR_DEVICE_ATTR(come_e_cpld_ver, S_IRUGO, show_cpld_version, NULL, COM_E_CPLD); +static SENSOR_DEVICE_ATTR(fpga_cpld_ver, S_IRUGO, show_cpld_version, NULL, FPGA_CPLD); +static SENSOR_DEVICE_ATTR(port_cpld1_ver, S_IRUGO, show_cpld_version, NULL, PORT_CPLD1); +static SENSOR_DEVICE_ATTR(port_cpld2_ver, S_IRUGO, show_cpld_version, NULL, PORT_CPLD2); +static SENSOR_DEVICE_ATTR(fan_cpld_ver, S_IRUGO, show_cpld_version, NULL, FAN_CPLD); + +static struct attribute *as9947_36xkb_sys_attributes[] = { + &sensor_dev_attr_come_e_cpld_ver.dev_attr.attr, + &sensor_dev_attr_fpga_cpld_ver.dev_attr.attr, + &sensor_dev_attr_port_cpld1_ver.dev_attr.attr, + &sensor_dev_attr_port_cpld2_ver.dev_attr.attr, + &sensor_dev_attr_fan_cpld_ver.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9947_36xkb_sys_group = { + .attrs = as9947_36xkb_sys_attributes, +}; + +static ssize_t sys_eeprom_read(loff_t off, char *buf, size_t count) +{ + int status = 0; + unsigned char length = 0; + + if ((off + count) > EEPROM_SIZE) { + return -EINVAL; + } + + length = (count >= IPMI_READ_MAX_LEN) ? IPMI_READ_MAX_LEN : count; + data->ipmi_tx_data[0] = (off & 0xff); + data->ipmi_tx_data[1] = length; + status = ipmi_send_message(&data->ipmi, IPMI_SYSEEPROM_READ_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), + data->ipmi_resp_eeprom + off, length); + if (unlikely(status != 0)) { + goto exit; + } + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + status = length; /* Read length */ + memcpy(buf, data->ipmi_resp_eeprom + off, length); + +exit: + return status; +} + +static ssize_t sysfs_bin_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, + char *buf, loff_t off, size_t count) +{ + ssize_t retval = 0; + + if (unlikely(!count)) { + return count; + } + + /* + * Read data from chip, protecting against concurrent updates + * from this host + */ + mutex_lock(&data->update_lock); + + while (count) { + ssize_t status; + + status = sys_eeprom_read(off, buf, count); + if (status <= 0) { + if (retval == 0) { + retval = status; + } + break; + } + + buf += status; + off += status; + count -= status; + retval += status; + } + + mutex_unlock(&data->update_lock); + return retval; + +} + +static int sysfs_eeprom_init(struct kobject *kobj, struct bin_attribute *eeprom) +{ + sysfs_bin_attr_init(eeprom); + eeprom->attr.name = EEPROM_NAME; + eeprom->attr.mode = S_IRUGO; + eeprom->read = sysfs_bin_read; + eeprom->write = NULL; + eeprom->size = EEPROM_SIZE; + + /* Create eeprom file */ + return sysfs_create_bin_file(kobj, eeprom); +} + +static int sysfs_eeprom_cleanup(struct kobject *kobj, struct bin_attribute *eeprom) +{ + sysfs_remove_bin_file(kobj, eeprom); + return 0; +} + +static struct as9947_36xkb_sys_data *as9947_36xkb_sys_update_cpld_ver(unsigned char cpld_addr) +{ + int status = 0; + + data->valid = 0; + data->ipmi_tx_data[0] = cpld_addr; + status = ipmi_send_message(&data->ipmi, IPMI_CPLD_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp_cpld, + sizeof(data->ipmi_resp_cpld)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_cpld_version(struct device *dev, struct device_attribute *da, char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char major; + unsigned char minor; + unsigned char cpld_addr = 0; + int error = 0; + + switch (attr->index) { + case COM_E_CPLD: + cpld_addr = IPMI_CPLD_COM_E_CMD; + break; + case FPGA_CPLD: + cpld_addr = IPMI_CPLD_FPGA_CMD; + break; + case PORT_CPLD1: + cpld_addr = IPMI_CPLD_PORT_CPLD1_CMD; + break; + case PORT_CPLD2: + cpld_addr = IPMI_CPLD_PORT_CPLD2_CMD; + break; + case FAN_CPLD: + cpld_addr = IPMI_CPLD_FAN_CMD; + break; + default: + return -EINVAL; + } + + mutex_lock(&data->update_lock); + + data = as9947_36xkb_sys_update_cpld_ver(cpld_addr); + if (!data->valid) { + error = -EIO; + goto exit; + } + + major = data->ipmi_resp_cpld[0]; + minor = data->ipmi_resp_cpld[1]; + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d.%d\n", major, minor); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static int as9947_36xkb_sys_probe(struct platform_device *pdev) +{ + int status = -1; + + /* Register sysfs hooks */ + status = sysfs_eeprom_init(&pdev->dev.kobj, &data->eeprom); + if (status) { + goto exit; + } + /* Register sysfs hooks */ + status = sysfs_create_group(&pdev->dev.kobj, &as9947_36xkb_sys_group); + if (status) + goto exit; + dev_info(&pdev->dev, "device created\n"); + + return 0; + +exit: + return status; +} + +static void as9947_36xkb_sys_remove(struct platform_device *pdev) +{ + sysfs_eeprom_cleanup(&pdev->dev.kobj, &data->eeprom); + sysfs_remove_group(&pdev->dev.kobj, &as9947_36xkb_sys_group); +} + +static int __init as9947_36xkb_sys_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_sys_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9947_36xkb_sys_driver); + if (ret < 0) { + goto dri_reg_err; + } + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_sys_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_sys_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9947_36xkb_sys_driver); + kfree(data); +} + +MODULE_AUTHOR("Brandon Chuang "); +MODULE_DESCRIPTION("as9947_36xkb_sys driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_sys_init); +module_exit(as9947_36xkb_sys_exit); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-thermal.c b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-thermal.c new file mode 100644 index 000000000..2625f96cc --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/modules/builds/x86-64-accton-as9947-36xkb-thermal.c @@ -0,0 +1,249 @@ +/* + * Copyright (C) Roger Ho + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9947_36xkb_thermal" +#define IPMI_THERMAL_READ_CMD 0x12 +#define THERMAL_COUNT 9 +#define THERMAL_DATA_LEN 3 +#define THERMAL_DATA_COUNT (THERMAL_COUNT * THERMAL_DATA_LEN) + +static ssize_t show_temp(struct device *dev, struct device_attribute *attr, + char *buf); +static int as9947_36xkb_thermal_probe(struct platform_device *pdev); +static void as9947_36xkb_thermal_remove(struct platform_device *pdev); + +enum temp_data_index { + TEMP_ADDR, + TEMP_FAULT, + TEMP_INPUT, + TEMP_DATA_COUNT +}; + +struct as9947_36xkb_thermal_data { + struct platform_device *pdev; + struct device *hwmon_dev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + char ipmi_resp[THERMAL_DATA_COUNT]; /* 3 bytes for each thermal */ + struct ipmi_data ipmi; + unsigned char ipmi_tx_data[2]; /* 0: thermal id, 1: temp */ +}; + +struct as9947_36xkb_thermal_data *data = NULL; + +static struct platform_driver as9947_36xkb_thermal_driver = { + .probe = as9947_36xkb_thermal_probe, + .remove = as9947_36xkb_thermal_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum as9947_36xkb_thermal_sysfs_attrs { + TEMP1_INPUT, // 0x4F + TEMP2_INPUT, // 0x4E + TEMP3_INPUT, // 0x4A + TEMP4_INPUT, // 0x4B + TEMP5_INPUT, // 0x4C_LOCAL + TEMP6_INPUT, // 0x4C_REMOTE + TEMP7_INPUT, // 0x4D + TEMP8_INPUT, // FAN 0x4D + TEMP9_INPUT, // FAN 0x4E +}; + +#define DECLARE_THERMAL_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, show_temp, \ + NULL, TEMP##index##_INPUT); + +#define DECLARE_THERMAL_ATTR(index) \ + &sensor_dev_attr_temp##index##_input.dev_attr.attr + +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(1); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(2); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(3); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(4); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(5); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(6); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(7); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(8); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(9); + +static struct attribute *as9947_36xkb_thermal_attrs[] = { + DECLARE_THERMAL_ATTR(1), + DECLARE_THERMAL_ATTR(2), + DECLARE_THERMAL_ATTR(3), + DECLARE_THERMAL_ATTR(4), + DECLARE_THERMAL_ATTR(5), + DECLARE_THERMAL_ATTR(6), + DECLARE_THERMAL_ATTR(7), + DECLARE_THERMAL_ATTR(8), + DECLARE_THERMAL_ATTR(9), + NULL +}; +ATTRIBUTE_GROUPS(as9947_36xkb_thermal); + + +static ssize_t show_temp(struct device *dev, struct device_attribute *da, + char *buf) +{ + int status = 0; + int index = 0; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + mutex_lock(&data->update_lock); + + if (time_after(jiffies, data->last_updated + HZ * 5) || !data->valid) { + data->valid = 0; + + status = ipmi_send_message(&data->ipmi, IPMI_THERMAL_READ_CMD, NULL, 0, + data->ipmi_resp, sizeof(data->ipmi_resp)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + } + + /* Get temp fault status */ + index = attr->index * TEMP_DATA_COUNT + TEMP_FAULT; + if (unlikely(data->ipmi_resp[index] == 0)) { + status = -EIO; + goto exit; + } + + /* Get temperature in degree celsius */ + index = attr->index * TEMP_DATA_COUNT + TEMP_INPUT; + status = ((s8)data->ipmi_resp[index]) * 1000; + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", status); + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static int as9947_36xkb_thermal_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev; + + hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, DRVNAME, + NULL, as9947_36xkb_thermal_groups); + if (IS_ERR(data->hwmon_dev)) { + status = PTR_ERR(data->hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "Device Created\n"); + + return status; +} + +static void as9947_36xkb_thermal_remove(struct platform_device *pdev) +{ + mutex_lock(&data->update_lock); + if (data->hwmon_dev) { + hwmon_device_unregister(data->hwmon_dev); + data->hwmon_dev = NULL; + } + mutex_unlock(&data->update_lock); +} + +static int __init as9947_36xkb_thermal_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9947_36xkb_thermal_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9947_36xkb_thermal_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) { + goto ipmi_err; + } + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9947_36xkb_thermal_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9947_36xkb_thermal_exit(void) +{ + if (data) { + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9947_36xkb_thermal_driver); + kfree(data); + } +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9947_36xkb_thermal driver"); +MODULE_LICENSE("GPL"); + +module_init(as9947_36xkb_thermal_init); +module_exit(as9947_36xkb_thermal_exit); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/PKG.yml b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/PKG.yml new file mode 100644 index 000000000..36ee89298 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/onlp-platform-any.yml PLATFORM=x86-64-accton-as9947-36xkb ARCH=amd64 TOOLCHAIN=x86_64-linux-gnu diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/Makefile new file mode 100644 index 000000000..e7437cb23 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/Makefile @@ -0,0 +1,2 @@ +FILTER=src +include $(ONL)/make/subdirs.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/lib/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/lib/Makefile new file mode 100644 index 000000000..dfa3224d6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/lib/Makefile @@ -0,0 +1,2 @@ +PLATFORM := x86-64-accton-as9947-36xkb +include $(ONL)/packages/base/any/onlp/builds/platform/libonlp-platform.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/onlpdump/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/onlpdump/Makefile new file mode 100644 index 000000000..24253055d --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/onlpdump/Makefile @@ -0,0 +1,2 @@ +PLATFORM := x86-64-accton-as9947-36xkb +include $(ONL)/packages/base/any/onlp/builds/platform/onlps.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.gitignore b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.module b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.module new file mode 100644 index 000000000..253470dd5 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/.module @@ -0,0 +1 @@ +name: x86_64_accton_as9947_36xkb diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/Makefile new file mode 100644 index 000000000..2e0f9f463 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/Makefile @@ -0,0 +1,9 @@ +############################################################################### +# +# +# +############################################################################### +include $(ONL)/make/config.mk +MODULE := x86_64_accton_as9947_36xkb +AUTOMODULE := x86_64_accton_as9947_36xkb +include $(BUILDER)/definemodule.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/README b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/README new file mode 100644 index 000000000..794e7ff24 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/README @@ -0,0 +1,6 @@ +############################################################################### +# +# x86_64_accton_as9947_36xkb README +# +############################################################################### + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/make.mk b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/make.mk new file mode 100644 index 000000000..ec4e45e09 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/make.mk @@ -0,0 +1,9 @@ +############################################################################### +# +# x86_64_accton_as9947_36xkb Autogeneration +# +############################################################################### +x86_64_accton_as9947_36xkb_AUTO_DEFS := module/auto/x86_64_accton_as9947_36xkb.yml +x86_64_accton_as9947_36xkb_AUTO_DIRS := module/inc/x86_64_accton_as9947_36xkb module/src +include $(BUILDER)/auto.mk + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/x86_64_accton_as9947_36xkb.yml b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/x86_64_accton_as9947_36xkb.yml new file mode 100644 index 000000000..d9200656d --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/auto/x86_64_accton_as9947_36xkb.yml @@ -0,0 +1,50 @@ +############################################################################### +# +# x86_64_accton_as9947_36xkb Autogeneration Definitions. +# +############################################################################### + +cdefs: &cdefs +- X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING: + doc: "Include or exclude logging." + default: 1 +- X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT: + doc: "Default enabled log options." + default: AIM_LOG_OPTIONS_DEFAULT +- X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT: + doc: "Default enabled log bits." + default: AIM_LOG_BITS_DEFAULT +- X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT: + doc: "Default enabled custom log bits." + default: 0 +- X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB: + doc: "Default all porting macros to use the C standard libraries." + default: 1 +- X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS: + doc: "Include standard library headers for stdlib porting macros." + default: x86_64_accton_as9947_36xkb_CONFIG_PORTING_STDLIB +- X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI: + doc: "Include generic uCli support." + default: 0 +- X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION: + doc: "Assume chassis fan direction is the same as the PSU fan direction." + default: 0 + + +definitions: + cdefs: + x86_64_accton_as9947_36xkb_CONFIG_HEADER: + defs: *cdefs + basename: x86_64_accton_as9947_36xkb_config + + portingmacro: + x86_64_accton_as9947_36xkb: + macros: + - malloc + - free + - memset + - memcpy + - strncpy + - vsnprintf + - snprintf + - strlen diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb.x b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb.x new file mode 100644 index 000000000..a8adafec4 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb.x @@ -0,0 +1,14 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* <--auto.start.xmacro(ALL).define> */ +/* */ + +/* <--auto.start.xenum(ALL).define> */ +/* */ + + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_config.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_config.h new file mode 100644 index 000000000..526e1a322 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_config.h @@ -0,0 +1,137 @@ +/**************************************************************************//** + * + * @file + * @brief x86_64_accton_as9947_36xkb Configuration Header + * + * @addtogroup x86_64_accton_as9947_36xkb-config + * @{ + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9947_36xkb_CONFIG_H__ +#define __x86_64_accton_as9947_36xkb_CONFIG_H__ + +#ifdef GLOBAL_INCLUDE_CUSTOM_CONFIG +#include +#endif +#ifdef x86_64_accton_as9947_36xkb_INCLUDE_CUSTOM_CONFIG +#include +#endif + +/* */ +#include +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING + * + * Include or exclude logging. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING 1 +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT + * + * Default enabled log options. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT AIM_LOG_OPTIONS_DEFAULT +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT + * + * Default enabled log bits. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT AIM_LOG_BITS_DEFAULT +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + * + * Default enabled custom log bits. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT 0 +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB + * + * Default all porting macros to use the C standard libraries. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB 1 +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS + * + * Include standard library headers for stdlib porting macros. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS x86_64_accton_as9947_36xkb_CONFIG_PORTING_STDLIB +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI + * + * Include generic uCli support. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI 0 +#endif + +/** + * X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION + * + * Assume chassis fan direction is the same as the PSU fan direction. */ + + +#ifndef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION +#define X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION 0 +#endif + + + +/** + * All compile time options can be queried or displayed + */ + +/** Configuration settings structure. */ +typedef struct x86_64_accton_as9947_36xkb_config_settings_s { + /** name */ + const char* name; + /** value */ + const char* value; +} x86_64_accton_as9947_36xkb_config_settings_t; + +/** Configuration settings table. */ +/** x86_64_accton_as9947_36xkb_config_settings table. */ +extern x86_64_accton_as9947_36xkb_config_settings_t x86_64_accton_as9947_36xkb_config_settings[]; + +/** + * @brief Lookup a configuration setting. + * @param setting The name of the configuration option to lookup. + */ +const char* x86_64_accton_as9947_36xkb_config_lookup(const char* setting); + +/** + * @brief Show the compile-time configuration. + * @param pvs The output stream. + */ +int x86_64_accton_as9947_36xkb_config_show(struct aim_pvs_s* pvs); + +/* */ + +#include "x86_64_accton_as9947_36xkb_porting.h" + +#endif /* __x86_64_accton_as9947_36xkb_CONFIG_H__ */ +/* @} */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_dox.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_dox.h new file mode 100644 index 000000000..874f40645 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_dox.h @@ -0,0 +1,26 @@ +/**************************************************************************//** + * + * x86_64_accton_as9947_36xkb Doxygen Header + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9947_36xkb_DOX_H__ +#define __x86_64_accton_as9947_36xkb_DOX_H__ + +/** + * @defgroup x86_64_accton_as9947_36xkb x86_64_accton_as9947_36xkb - x86_64_accton_as9947_36xkb Description + * + +The documentation overview for this module should go here. + + * + * @{ + * + * @defgroup x86_64_accton_as9947_36xkb-x86_64_accton_as9947_36xkb Public Interface + * @defgroup x86_64_accton_as9947_36xkb-config Compile Time Configuration + * @defgroup x86_64_accton_as9947_36xkb-porting Porting Macros + * + * @} + * + */ + +#endif /* __x86_64_accton_as9947_36xkb_DOX_H__ */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_porting.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_porting.h new file mode 100644 index 000000000..35bcffa82 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/inc/x86_64_accton_as9947_36xkb/x86_64_accton_as9947_36xkb_porting.h @@ -0,0 +1,107 @@ +/**************************************************************************//** + * + * @file + * @brief x86_64_accton_as9947_36xkb Porting Macros. + * + * @addtogroup x86_64_accton_as9947_36xkb-porting + * @{ + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9947_36xkb_PORTING_H__ +#define __x86_64_accton_as9947_36xkb_PORTING_H__ + + +/* */ +#if X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS == 1 +#include +#include +#include +#include +#include +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_MALLOC + #if defined(GLOBAL_MALLOC) + #define X86_64_ACCTON_AS9947_36XKB_MALLOC GLOBAL_MALLOC + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_MALLOC malloc + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_MALLOC is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_FREE + #if defined(GLOBAL_FREE) + #define X86_64_ACCTON_AS9947_36XKB_FREE GLOBAL_FREE + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_FREE free + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_FREE is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_MEMSET + #if defined(GLOBAL_MEMSET) + #define X86_64_ACCTON_AS9947_36XKB_MEMSET GLOBAL_MEMSET + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_MEMSET memset + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_MEMSET is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_MEMCPY + #if defined(GLOBAL_MEMCPY) + #define X86_64_ACCTON_AS9947_36XKB_MEMCPY GLOBAL_MEMCPY + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_MEMCPY memcpy + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_MEMCPY is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_STRNCPY + #if defined(GLOBAL_STRNCPY) + #define X86_64_ACCTON_AS9947_36XKB_STRNCPY GLOBAL_STRNCPY + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_STRNCPY strncpy + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_STRNCPY is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_VSNPRINTF + #if defined(GLOBAL_VSNPRINTF) + #define X86_64_ACCTON_AS9947_36XKB_VSNPRINTF GLOBAL_VSNPRINTF + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_VSNPRINTF vsnprintf + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_VSNPRINTF is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_SNPRINTF + #if defined(GLOBAL_SNPRINTF) + #define X86_64_ACCTON_AS9947_36XKB_SNPRINTF GLOBAL_SNPRINTF + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_SNPRINTF snprintf + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_SNPRINTF is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9947_36XKB_STRLEN + #if defined(GLOBAL_STRLEN) + #define X86_64_ACCTON_AS9947_36XKB_STRLEN GLOBAL_STRLEN + #elif X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9947_36XKB_STRLEN strlen + #else + #error The macro X86_64_ACCTON_AS9947_36XKB_STRLEN is required but cannot be defined. + #endif +#endif + +/* */ + + +#endif /* __x86_64_accton_as9947_36xkb_PORTING_H__ */ +/* @} */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/make.mk b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/make.mk new file mode 100644 index 000000000..03a713f37 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/make.mk @@ -0,0 +1,10 @@ +############################################################################### +# +# +# +############################################################################### +THIS_DIR := $(dir $(lastword $(MAKEFILE_LIST))) +x86_64_accton_as9947_36xkb_INCLUDES := -I $(THIS_DIR)inc +x86_64_accton_as9947_36xkb_INTERNAL_INCLUDES := -I $(THIS_DIR)src +x86_64_accton_as9947_36xkb_DEPENDMODULE_ENTRIES := init:x86_64_accton_as9947_36xkb ucli:x86_64_accton_as9947_36xkb + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/Makefile new file mode 100644 index 000000000..24c3a2885 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/Makefile @@ -0,0 +1,9 @@ +############################################################################### +# +# Local source generation targets. +# +############################################################################### + +ucli: + @../../../../tools/uclihandlers.py x86_64_accton_as9947_36xkb_ucli.c + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/debug.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/debug.c new file mode 100644 index 000000000..53e08a974 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/debug.c @@ -0,0 +1,45 @@ +#include "x86_64_accton_as9947_36xkb_int.h" + +#if x86_64_accton_as9947_36xkb_CONFIG_INCLUDE_DEBUG == 1 + +#include + +static char help__[] = + "Usage: debug [options]\n" + " -c CPLD Versions\n" + " -h Help\n" + ; + +int +x86_64_accton_as9947_36xkb_debug_main(int argc, char* argv[]) +{ + int c = 0; + int help = 0; + int rv = 0; + + while( (c = getopt(argc, argv, "ch")) != -1) { + switch(c) + { + case 'c': c = 1; break; + case 'h': help = 1; rv = 0; break; + default: help = 1; rv = 1; break; + } + + } + + if(help || argc == 1) { + printf("%s", help__); + return rv; + } + + if(c) { + printf("Not implemented.\n"); + } + + + return 0; +} + +#endif + + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/fani.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/fani.c new file mode 100644 index 000000000..5bc2f64b7 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/fani.c @@ -0,0 +1,319 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * Fan Platform Implementation Defaults. + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +enum fan_id { + FAN_1_ON_FAN_BOARD = 1, + FAN_2_ON_FAN_BOARD, + FAN_3_ON_FAN_BOARD, + FAN_4_ON_FAN_BOARD, + FAN_5_ON_FAN_BOARD, + FAN_6_ON_FAN_BOARD, + FAN_7_ON_FAN_BOARD, + FAN_8_ON_FAN_BOARD, + FAN_1_ON_PSU_1, + FAN_1_ON_PSU_2 +}; + +#define MAX_PSU_FAN_SPEED 25500 + +#define CHASSIS_FAN_INFO(fid) \ + { \ + { ONLP_FAN_ID_CREATE(FAN_##fid##_ON_FAN_BOARD), "Chassis Fan - "#fid, 0, {0} },\ + 0x0,\ + ONLP_FAN_CAPS_SET_PERCENTAGE | ONLP_FAN_CAPS_GET_RPM | ONLP_FAN_CAPS_GET_PERCENTAGE,\ + 0,\ + 0,\ + ONLP_FAN_MODE_INVALID,\ + } + +#define PSU_FAN_INFO(pid, fid) \ + { \ + { ONLP_FAN_ID_CREATE(FAN_##fid##_ON_PSU_##pid), "PSU "#pid" - Fan "#fid, 0, {0} },\ + 0x0,\ + ONLP_FAN_CAPS_GET_RPM | ONLP_FAN_CAPS_GET_PERCENTAGE,\ + 0,\ + 0,\ + ONLP_FAN_MODE_INVALID,\ + } + +/* Static fan information */ +onlp_fan_info_t finfo[] = { + { }, /* Not used */ + CHASSIS_FAN_INFO(1), + CHASSIS_FAN_INFO(2), + CHASSIS_FAN_INFO(3), + CHASSIS_FAN_INFO(4), + CHASSIS_FAN_INFO(5), + CHASSIS_FAN_INFO(6), + CHASSIS_FAN_INFO(7), + CHASSIS_FAN_INFO(8), + PSU_FAN_INFO(1,1), + PSU_FAN_INFO(2,1) +}; + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_FAN(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +static int +_onlp_fani_set_fan_dir_info(int fid, onlp_fan_info_t* info) +{ + enum onlp_fan_dir dir = onlp_get_fan_dir(fid); + + if (FAN_DIR_F2B == dir) { + info->caps |= ONLP_FAN_CAPS_F2B; + info->status |= ONLP_FAN_STATUS_F2B; + } + else { + info->caps |= ONLP_FAN_CAPS_B2F; + info->status |= ONLP_FAN_STATUS_B2F; + } + + return ONLP_STATUS_OK; +} + +static int +_onlp_fani_info_get_fan(int fid, onlp_fan_info_t* info) +{ + int value, ret, pwm; + char *str = NULL; + int len; + int copy_len; + int hwmon_idx; + char file[32]; + + hwmon_idx = onlp_get_fan_hwmon_idx(); + + /* get fan present status + */ + ret = onlp_file_read_int(&value, FAN_SYSFS_FORMAT"fan%d_present", fid); + if (ret < 0) { + return ONLP_STATUS_E_INTERNAL; + } + + if (value == 0) { + return ONLP_STATUS_OK; + } + info->status |= ONLP_FAN_STATUS_PRESENT; + + /* get fan fault status (turn on when any one fails) + */ + ret = onlp_file_read_int(&value, "%s""fan%d_fault", FAN_SYSFS_FORMAT, fid); + if (ret < 0) { + return ONLP_STATUS_E_INTERNAL; + } + + if (value > 0) { + info->status |= ONLP_FAN_STATUS_FAILED; + return ONLP_STATUS_OK; + } + + /* get fan speed + */ + ret = onlp_file_read_int(&value, "%s""fan%d_input", FAN_SYSFS_FORMAT, fid); + if (ret < 0) { + return ONLP_STATUS_E_INTERNAL; + } + info->rpm = value; + + /* get speed percentage from rpm + */ + pwm = 0; + ret = onlp_file_read_int(&pwm, "%s""fan%d_pwm", FAN_SYSFS_FORMAT, fid); + if (ret < 0) { + return ONLP_STATUS_E_INTERNAL; + } + + value = 0; + ret = onlp_file_read_int(&value, "%s""fan%d_target", FAN_SYSFS_FORMAT, fid); + if (ret < 0 || value == 0) { + return ONLP_STATUS_E_INTERNAL; + } + + info->percentage = (info->rpm*pwm)/value; + if (info->percentage > 100) + info->percentage = 100; + + /* Read model name */ + snprintf(file, sizeof(file), "fan%d_model", fid); + len = onlp_file_read_str(&str, FAN_SYSFS_FORMAT_1, hwmon_idx, file); + if (str && len) { + if (sizeof(info->model) > len) + { + copy_len = len; + } + else + { + copy_len = sizeof(info->model) - 1; + } + + memcpy(info->model, str, copy_len); + info->model[copy_len] = '\0'; + } + AIM_FREE_IF_PTR(str); + + /* serial number*/ + snprintf(file, sizeof(file), "fan%d_serial", fid); + len = onlp_file_read_str(&str, FAN_SYSFS_FORMAT_1, hwmon_idx, file); + if (str && len) { + if (sizeof(info->serial) > len) + { + copy_len = len; + } + else + { + copy_len = sizeof(info->serial) - 1; + } + + memcpy(info->serial, str, copy_len); + info->serial[copy_len] = '\0'; + } + AIM_FREE_IF_PTR(str); + + _onlp_fani_set_fan_dir_info(fid, info); + + return ONLP_STATUS_OK; +} + +static int +_onlp_fani_info_get_fan_on_psu(int pid, onlp_fan_info_t* info) +{ + char *str = NULL; + int len = 0; + int val = 0; + int ret = 0; + int hwmon_idx; + + info->status |= ONLP_FAN_STATUS_PRESENT; + + /* get fan direction + */ + hwmon_idx = onlp_get_psu_hwmon_idx(pid); + if (hwmon_idx >= 0) { + char file[32]; + snprintf(file, sizeof(file), "psu%d_fan_dir", pid); + + len = onlp_file_read_str(&str, PSU_SYSFS_FORMAT_1, hwmon_idx, file); + if (str && len >= 3) { + if (strncmp(str, "B2F", strlen("B2F")) == 0) { + info->status |= ONLP_FAN_STATUS_B2F; + } + else { + info->status |= ONLP_FAN_STATUS_F2B; + } + } + AIM_FREE_IF_PTR(str); + } + + /* get fan speed + */ + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "fan1_input"); + if (ret < 0) { + AIM_LOG_ERROR("Unable to read status from PSU(%d)\r\n", pid); + return ONLP_STATUS_E_INTERNAL; + } + info->rpm = val; + info->percentage = (info->rpm * 100)/MAX_PSU_FAN_SPEED; + + return ONLP_STATUS_OK; +} + +/* + * This function will be called prior to all of onlp_fani_* functions. + */ +int +onlp_fani_init(void) +{ + return ONLP_STATUS_OK; +} + +int +onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) +{ + int rc = 0; + int fid; + VALIDATE(id); + + fid = ONLP_OID_ID_GET(id); + *info = finfo[fid]; + + switch (fid) { + case FAN_1_ON_FAN_BOARD: + case FAN_2_ON_FAN_BOARD: + case FAN_3_ON_FAN_BOARD: + case FAN_4_ON_FAN_BOARD: + case FAN_5_ON_FAN_BOARD: + case FAN_6_ON_FAN_BOARD: + case FAN_7_ON_FAN_BOARD: + case FAN_8_ON_FAN_BOARD: + rc = _onlp_fani_info_get_fan(fid, info); + break; + case FAN_1_ON_PSU_1: + case FAN_1_ON_PSU_2: + rc = _onlp_fani_info_get_fan_on_psu(fid-FAN_8_ON_FAN_BOARD, info); + break; + default: + rc = ONLP_STATUS_E_INVALID; + break; + } + + return rc; +} + +/* + * This function sets the fan speed of the given OID as a percentage. + * + * This will only be called if the OID has the PERCENTAGE_SET + * capability. + * + * It is optional if you have no fans at all with this feature. + */ +int +onlp_fani_percentage_set(onlp_oid_t id, int p) +{ + int fid; + + VALIDATE(id); + + fid = ONLP_OID_ID_GET(id); + + if (fid < FAN_1_ON_FAN_BOARD || fid > FAN_8_ON_FAN_BOARD) { + return ONLP_STATUS_E_UNSUPPORTED; + } + + if (onlp_file_write_int(p, "%s""fan%d_pwm", FAN_SYSFS_FORMAT, fid) != 0) { + AIM_LOG_ERROR("Unable to change duty cycle of fan (%d)\r\n", fid); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/ledi.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/ledi.c new file mode 100644 index 000000000..b417f9d49 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/ledi.c @@ -0,0 +1,231 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2013 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_LED(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +#define LED_FORMAT "/sys/devices/platform/as9947_36xkb_led/led_%s" + +enum led_light_mode { /*must be the same with the definition @ kernel driver */ + LED_MODE_OFF, + LED_MODE_RED = 10, + LED_MODE_RED_BLINKING = 11, + LED_MODE_ORANGE = 12, + LED_MODE_ORANGE_BLINKING = 13, + LED_MODE_YELLOW = 14, + LED_MODE_YELLOW_BLINKING = 15, + LED_MODE_GREEN = 16, + LED_MODE_GREEN_BLINKING = 17, + LED_MODE_BLUE = 18, + LED_MODE_BLUE_BLINKING = 19, + LED_MODE_PURPLE = 20, + LED_MODE_PURPLE_BLINKING = 21, + LED_MODE_AUTO = 22, + LED_MODE_AUTO_BLINKING = 23, + LED_MODE_WHITE = 24, + LED_MODE_WHITE_BLINKING = 25, + LED_MODE_CYAN = 26, + LED_MODE_CYAN_BLINKING = 27, + LED_MODE_UNKNOWN = 99 +}; + +typedef struct led_light_mode_map { + enum onlp_led_id id; + enum led_light_mode driver_led_mode; + enum onlp_led_mode_e onlp_led_mode; +} led_light_mode_map_t; + +led_light_mode_map_t led_map[] = { + { LED_LOC, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_LOC, LED_MODE_BLUE_BLINKING, ONLP_LED_MODE_BLUE_BLINKING }, + { LED_DIAG, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_DIAG, LED_MODE_GREEN, ONLP_LED_MODE_GREEN }, + { LED_DIAG, LED_MODE_RED, ONLP_LED_MODE_RED }, + { LED_PSU1, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_PSU1, LED_MODE_GREEN, ONLP_LED_MODE_GREEN }, + { LED_PSU1, LED_MODE_RED, ONLP_LED_MODE_RED }, + { LED_PSU2, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_PSU2, LED_MODE_GREEN, ONLP_LED_MODE_GREEN }, + { LED_PSU2, LED_MODE_RED, ONLP_LED_MODE_RED }, + { LED_FAN, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_FAN, LED_MODE_GREEN, ONLP_LED_MODE_GREEN }, + { LED_FAN, LED_MODE_RED, ONLP_LED_MODE_RED }, + { LED_ALARM, LED_MODE_OFF, ONLP_LED_MODE_OFF }, + { LED_ALARM, LED_MODE_RED, ONLP_LED_MODE_RED } +}; + +static char *leds[] = { /* must map with onlp_led_id */ + NULL, + "loc", + "diag", + "psu1", + "psu2", + "fan", + "alarm", +}; + +/* + * Get the information for the given LED OID. + */ +static onlp_led_info_t linfo[] = +{ + { }, /* Not used */ + { + { ONLP_LED_ID_CREATE(LED_LOC), "Chassis LED 1 (LOC LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_BLUE_BLINKING, + }, + { + { ONLP_LED_ID_CREATE(LED_DIAG), "Chassis LED 2 (DIAG LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_AUTO, + }, + { + { ONLP_LED_ID_CREATE(LED_PSU1), "Chassis LED 3 (PSU1 LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_AUTO, + }, + { + { ONLP_LED_ID_CREATE(LED_PSU2), "Chassis LED 4 (PSU2 LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_AUTO, + }, + { + { ONLP_LED_ID_CREATE(LED_FAN), "Chassis LED 5 (FAN LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_AUTO, + }, +}; + +static int driver_to_onlp_led_mode(enum onlp_led_id id, enum led_light_mode driver_led_mode) +{ + int i, nsize = sizeof(led_map)/sizeof(led_map[0]); + + for (i = 0; i < nsize; i++) { + if (id == led_map[i].id && driver_led_mode == led_map[i].driver_led_mode) { + return led_map[i].onlp_led_mode; + } + } + + return 0; +} + +static int onlp_to_driver_led_mode(enum onlp_led_id id, onlp_led_mode_t onlp_led_mode) +{ + int i, nsize = sizeof(led_map)/sizeof(led_map[0]); + + for (i = 0; i < nsize; i++) { + if (id == led_map[i].id && onlp_led_mode == led_map[i].onlp_led_mode) { + return led_map[i].driver_led_mode; + } + } + + return 0; +} + +/* + * This function will be called prior to any other onlp_ledi_* functions. + */ +int +onlp_ledi_init(void) +{ + return ONLP_STATUS_OK; +} + +int +onlp_ledi_info_get(onlp_oid_t id, onlp_led_info_t* info) +{ + int lid, value; + VALIDATE(id); + + lid = ONLP_OID_ID_GET(id); + + /* Set the onlp_oid_hdr_t and capabilities */ + *info = linfo[ONLP_OID_ID_GET(id)]; + + /* Get LED mode */ + if (onlp_file_read_int(&value, LED_FORMAT, leds[lid]) < 0) { + return ONLP_STATUS_E_INTERNAL; + } + + info->mode = driver_to_onlp_led_mode(lid, value); + + /* Set the on/off status */ + if (info->mode != ONLP_LED_MODE_OFF) { + info->status |= ONLP_LED_STATUS_ON; + } + + return ONLP_STATUS_OK; +} + +/* + * Turn an LED on or off. + * + * This function will only be called if the LED OID supports the ONOFF + * capability. + * + * What 'on' means in terms of colors or modes for multimode LEDs is + * up to the platform to decide. This is intended as baseline toggle mechanism. + */ +int +onlp_ledi_set(onlp_oid_t id, int on_or_off) +{ + VALIDATE(id); + + if (!on_or_off) { + return onlp_ledi_mode_set(id, ONLP_LED_MODE_OFF); + } + + return ONLP_STATUS_E_UNSUPPORTED; +} + +/* + * This function puts the LED into the given mode. It is a more functional + * interface for multimode LEDs. + * + * Only modes reported in the LED's capabilities will be attempted. + */ +int +onlp_ledi_mode_set(onlp_oid_t id, onlp_led_mode_t mode) +{ + int lid; + VALIDATE(id); + + lid = ONLP_OID_ID_GET(id); + + if (onlp_file_write_int(onlp_to_driver_led_mode(lid , mode), LED_FORMAT, leds[lid]) != 0) { + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/make.mk b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/make.mk new file mode 100644 index 000000000..f1c06acad --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/make.mk @@ -0,0 +1,9 @@ +############################################################################### +# +# +# +############################################################################### + +LIBRARY := x86_64_accton_as9947_36xkb +$(LIBRARY)_SUBDIR := $(dir $(lastword $(MAKEFILE_LIST))) +include $(BUILDER)/lib.mk diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.c new file mode 100644 index 000000000..ff1cb31eb --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.c @@ -0,0 +1,109 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +enum onlp_fan_dir onlp_get_fan_dir(int fid) +{ + int len = 0; + int i = 0; + int hwmon_idx; + char *str = NULL; + char *dirs[FAN_DIR_COUNT] = { "F2B", "B2F" }; + char file[32]; + enum onlp_fan_dir dir = FAN_DIR_F2B; + + hwmon_idx = onlp_get_fan_hwmon_idx(); + if (hwmon_idx >= 0) { + /* Read attribute */ + snprintf(file, sizeof(file), "fan%d_dir", fid); + len = onlp_file_read_str(&str, FAN_SYSFS_FORMAT_1, hwmon_idx, file); + + /* Verify Fan dir string length */ + if (!str || len < 3) { + AIM_FREE_IF_PTR(str); + return dir; + } + + for (i = 0; i < AIM_ARRAYSIZE(dirs); i++) { + if (strncmp(str, dirs[i], strlen(dirs[i])) == 0) { + dir = (enum onlp_fan_dir)i; + break; + } + } + + AIM_FREE_IF_PTR(str); + } + + return dir; +} + +int onlp_get_psu_hwmon_idx(int pid) +{ + /* find hwmon index */ + char* file = NULL; + char path[64]; + int ret, hwmon_idx, max_hwmon_idx = 20; + + for (hwmon_idx = 0; hwmon_idx <= max_hwmon_idx; hwmon_idx++) { + snprintf(path, sizeof(path), "/sys/devices/platform/as9947_36xkb_psu/hwmon/hwmon%d/", hwmon_idx); + + if (pid == 1) + ret = onlp_file_find(path, "psu1_present", &file); + else if (pid == 2) + ret = onlp_file_find(path, "psu2_present", &file); + else + return -1; + + AIM_FREE_IF_PTR(file); + + if (ONLP_STATUS_OK == ret) + return hwmon_idx; + } + + return -1; +} + +int onlp_get_fan_hwmon_idx(void) +{ + /* find hwmon index */ + char* file = NULL; + char path[64]; + int ret, hwmon_idx, max_hwmon_idx = 20; + + for (hwmon_idx = 0; hwmon_idx <= max_hwmon_idx; hwmon_idx++) { + snprintf(path, sizeof(path), "/sys/devices/platform/as9947_36xkb_fan/hwmon/hwmon%d/", hwmon_idx); + + ret = onlp_file_find(path, "name", &file); + AIM_FREE_IF_PTR(file); + + if (ONLP_STATUS_OK == ret) + return hwmon_idx; + } + + return -1; +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.h new file mode 100644 index 000000000..001b3872d --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/platform_lib.h @@ -0,0 +1,101 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#ifndef __PLATFORM_LIB_H__ +#define __PLATFORM_LIB_H__ + +#include "x86_64_accton_as9947_36xkb_log.h" + +#define CHASSIS_FAN_COUNT 8 +#define CHASSIS_THERMAL_COUNT 10 +#define CHASSIS_LED_COUNT 5 +#define CHASSIS_PSU_COUNT 2 +#define NUM_OF_THERMAL_PER_PSU 3 + +#define PSU1_ID 1 +#define PSU2_ID 2 + +#define PSU_SYSFS_FORMAT "/sys/devices/platform/as9947_36xkb_psu*psu%d_%s" +#define PSU_SYSFS_FORMAT_1 "/sys/devices/platform/as9947_36xkb_psu/hwmon/hwmon%d/%s" +#define FAN_SYSFS_FORMAT "/sys/devices/platform/as9947_36xkb_fan*" +#define FAN_SYSFS_FORMAT_1 "/sys/devices/platform/as9947_36xkb_fan/hwmon/hwmon%d/%s" +#define SYS_LED_PATH "/sys/devices/platform/as9947_36xkb_led/" +#define IDPROM_PATH "/sys/bus/platform/devices/as9947_36xkb_sys/eeprom" + +enum onlp_thermal_id { + THERMAL_RESERVED = 0, + THERMAL_CPU_CORE, + THERMAL_1_ON_MAIN_BROAD, + THERMAL_2_ON_MAIN_BROAD, + THERMAL_3_ON_MAIN_BROAD, + THERMAL_4_ON_MAIN_BROAD, + THERMAL_5_ON_MAIN_BROAD, + THERMAL_6_ON_MAIN_BROAD, + THERMAL_7_ON_MAIN_BROAD, + THERMAL_1_ON_FAN_BROAD, + THERMAL_2_ON_FAN_BROAD, + THERMAL_1_ON_PSU1, + THERMAL_2_ON_PSU1, + THERMAL_3_ON_PSU1, + THERMAL_1_ON_PSU2, + THERMAL_2_ON_PSU2, + THERMAL_3_ON_PSU2, + THERMAL_COUNT +}; + +enum onlp_led_id { + LED_LOC = 1, + LED_DIAG, + LED_PSU1, + LED_PSU2, + LED_FAN, + LED_ALARM +}; + +enum onlp_fan_dir { + FAN_DIR_F2B, + FAN_DIR_B2F, + FAN_DIR_COUNT +}; + +typedef enum as9947_36xkb_platform_id { + as9947_36xkb, + PID_UNKNOWN +} as9947_36xkb_platform_id_t; + +enum onlp_fan_dir onlp_get_fan_dir(int fid); +int onlp_get_psu_hwmon_idx(int pid); +int onlp_get_fan_hwmon_idx(void); + +#define AIM_FREE_IF_PTR(p) \ + do \ + { \ + if (p) { \ + aim_free(p); \ + p = NULL; \ + } \ + } while (0) + +#endif /* __PLATFORM_LIB_H__ */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/psui.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/psui.c new file mode 100644 index 000000000..beb8a58b0 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/psui.c @@ -0,0 +1,177 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define PSU_STATUS_PRESENT 1 +#define PSU_STATUS_POWER_GOOD 1 + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_PSU(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +int +onlp_psui_init(void) +{ + return ONLP_STATUS_OK; +} + +/* + * Get all information about the given PSU oid. + */ +static onlp_psu_info_t pinfo[] = { + { }, /* Not used */ + { + { ONLP_PSU_ID_CREATE(PSU1_ID), "PSU-1", 0, {0} }, + }, + { + { ONLP_PSU_ID_CREATE(PSU2_ID), "PSU-2", 0, {0} }, + } +}; + +int +onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) +{ + int val = 0; + int ret = ONLP_STATUS_OK; + int pid = ONLP_OID_ID_GET(id); + int thermal_count = 0; + int hwmon_idx; + VALIDATE(id); + + memset(info, 0, sizeof(onlp_psu_info_t)); + *info = pinfo[pid]; /* Set the onlp_oid_hdr_t */ + + /* Get the present state */ + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "present"); + if (ret < 0) { + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_E_INTERNAL; + } + + if (val != PSU_STATUS_PRESENT) { + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_OK; + } + info->status |= ONLP_PSU_STATUS_PRESENT; + + /* Get power good status */ + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "power_good"); + if (ret < 0) { + info->status |= ONLP_PSU_STATUS_FAILED; + return ONLP_STATUS_E_INTERNAL; + } + + if (val != PSU_STATUS_POWER_GOOD) { + info->status |= ONLP_PSU_STATUS_UNPLUGGED; + } + + /* Set capability + */ + info->caps = ONLP_PSU_CAPS_AC; + + /* Set the associated oid_table */ + thermal_count = CHASSIS_THERMAL_COUNT; + + info->hdr.coids[0] = ONLP_THERMAL_ID_CREATE(thermal_count + (pid-1)*NUM_OF_THERMAL_PER_PSU + 1); + info->hdr.coids[1] = ONLP_THERMAL_ID_CREATE(thermal_count + (pid-1)*NUM_OF_THERMAL_PER_PSU + 2); + info->hdr.coids[2] = ONLP_THERMAL_ID_CREATE(thermal_count + (pid-1)*NUM_OF_THERMAL_PER_PSU + 3); + info->hdr.coids[3] = ONLP_FAN_ID_CREATE(pid + CHASSIS_FAN_COUNT); + + /* Read voltage, current and power */ + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "vin"); + if (ret == ONLP_STATUS_OK && val) { + info->mvin = val; + info->caps |= ONLP_PSU_CAPS_VIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "iin"); + if (ret == ONLP_STATUS_OK && val) { + info->miin = val; + info->caps |= ONLP_PSU_CAPS_IIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "pin"); + if (ret == ONLP_STATUS_OK && val) { + info->mpin = val; + info->caps |= ONLP_PSU_CAPS_PIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "vout"); + if (ret == ONLP_STATUS_OK && val) { + info->mvout = val; + info->caps |= ONLP_PSU_CAPS_VOUT; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "iout"); + if (ret == ONLP_STATUS_OK && val) { + info->miout = val; + info->caps |= ONLP_PSU_CAPS_IOUT; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "pout"); + if (ret == ONLP_STATUS_OK && val) { + info->mpout = val; + info->caps |= ONLP_PSU_CAPS_POUT; + } + + hwmon_idx = onlp_get_psu_hwmon_idx(pid); + if (hwmon_idx >= 0) { + char *str = NULL; + int len; + char file[32]; + + /* Read model */ + snprintf(file, sizeof(file), "psu%d_model", pid); + len = onlp_file_read_str(&str, PSU_SYSFS_FORMAT_1, hwmon_idx, file); + if (str && len) { + memcpy(info->model, str, len); + info->model[len] = '\0'; + } + AIM_FREE_IF_PTR(str); + + /* Read serial */ + snprintf(file, sizeof(file), "psu%d_serial", pid); + len = onlp_file_read_str(&str, PSU_SYSFS_FORMAT_1, hwmon_idx, file); + if (str && len) { + memcpy(info->serial, str, len); + info->serial[len] = '\0'; + } + AIM_FREE_IF_PTR(str); + } + + return ret; +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sfpi.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sfpi.c new file mode 100644 index 000000000..3fe79ed30 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sfpi.c @@ -0,0 +1,320 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2013 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include +#include "x86_64_accton_as9947_36xkb_int.h" +#include "x86_64_accton_as9947_36xkb_log.h" + +#define VALIDATE_SFP(_port) \ + do { \ + if (_port < 1 || _port > 4) \ + return ONLP_STATUS_E_UNSUPPORTED; \ + } while(0) + +#define VALIDATE_QSFP(_port) \ + do { \ + if (_port < 5 || _port > 40 ) \ + return ONLP_STATUS_E_UNSUPPORTED; \ + } while(0) + +#define MODULE_EEPROM_FORMAT "/sys/bus/i2c/devices/%d-0050/eeprom" +#define MODULE_PRESENT_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_present_%d" +#define MODULE_RXLOS_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_rx_los_%d" +#define MODULE_TXFAULT_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_tx_fault_%d" +#define MODULE_TXDISABLE_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_tx_disable_%d" +#define MODULE_RESET_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_reset_%d" +#define MODULE_LPMODE_FORMAT "/sys/devices/platform/as9947_36xkb_sfp/module_lpmode_%d" + +#define NUM_OF_PORT 40 +#define NUM_OF_SFP_PORT 4 +static const int port_bus_index[NUM_OF_PORT] = { + 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 +}; + +#define PORT_BUS_INDEX(port) (port_bus_index[port-1]) + +/************************************************************ + * + * SFPI Entry Points + * + ***********************************************************/ +int +onlp_sfpi_init(void) +{ + /* Called at initialization time */ + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_bitmap_get(onlp_sfp_bitmap_t* bmap) +{ + int p; + + for(p = 1; p <= NUM_OF_PORT; p++) { + AIM_BITMAP_SET(bmap, p); + } + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_is_present(int port) +{ + /* + * Return 1 if present. + * Return 0 if not present. + * Return < 0 if error. + */ + int present; + + if (onlp_file_read_int(&present, MODULE_PRESENT_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read present status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return present; +} + +int onlp_sfpi_rx_los_bitmap_get(onlp_sfp_bitmap_t* dst) +{ + int i = 1, value = 0; + + for (i = 1; i <= NUM_OF_PORT; i++) + { + if (i <= NUM_OF_SFP_PORT) + { + if (onlp_file_read_int(&value, MODULE_RXLOS_FORMAT, i) < 0) { + AIM_LOG_ERROR("Unable to read rx_loss status from port(%d)\r\n", i); + return ONLP_STATUS_E_INTERNAL; + } + + if (value) + AIM_BITMAP_MOD(dst, i, 1); + else + AIM_BITMAP_MOD(dst, i, 0); + } + else + { + AIM_BITMAP_MOD(dst, i, 0); + } + } + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_eeprom_read(int port, uint8_t data[256]) +{ + /* + * Read the SFP eeprom into data[] + * + * Return MISSING if SFP is missing. + * Return OK if eeprom is read + */ + int size = 0; + memset(data, 0, 256); + + if(onlp_file_read(data, 256, &size, MODULE_EEPROM_FORMAT, PORT_BUS_INDEX(port)) != ONLP_STATUS_OK) { + AIM_LOG_ERROR("Unable to read eeprom from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + if (size != 256) { + AIM_LOG_ERROR("Unable to read eeprom from port(%d), size is different!\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_dom_read(int port, uint8_t data[256]) +{ + FILE* fp; + char file[64] = {0}; + + sprintf(file, MODULE_EEPROM_FORMAT, PORT_BUS_INDEX(port)); + fp = fopen(file, "r"); + if(fp == NULL) { + AIM_LOG_ERROR("Unable to open the eeprom device file of port(%d)", port); + return ONLP_STATUS_E_INTERNAL; + } + + if (fseek(fp, 256, SEEK_CUR) != 0) { + fclose(fp); + AIM_LOG_ERROR("Unable to set the file position indicator of port(%d)", port); + return ONLP_STATUS_E_INTERNAL; + } + + int ret = fread(data, 1, 256, fp); + fclose(fp); + if (ret != 256) { + AIM_LOG_ERROR("Unable to read the module_eeprom device file of port(%d)", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_dev_readb(int port, uint8_t devaddr, uint8_t addr) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_readb(bus, devaddr, addr, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_writeb(int port, uint8_t devaddr, uint8_t addr, uint8_t value) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_writeb(bus, devaddr, addr, value, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_readw(int port, uint8_t devaddr, uint8_t addr) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_readw(bus, devaddr, addr, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_writew(int port, uint8_t devaddr, uint8_t addr, uint16_t value) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_writew(bus, devaddr, addr, value, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_control_set(int port, onlp_sfp_control_t control, int value) +{ + switch(control) { + case ONLP_SFP_CONTROL_TX_DISABLE: { + VALIDATE_SFP(port); + + if (onlp_file_write_int(value, MODULE_TXDISABLE_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to set tx disable status to port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_RESET_STATE: { + VALIDATE_QSFP(port); + + if (onlp_file_write_int(value, MODULE_RESET_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to write reset status to port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_LP_MODE: { + VALIDATE_QSFP(port); + + if (onlp_file_write_int(value, MODULE_LPMODE_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to write lp mode status to port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + default: + break; + } + + return ONLP_STATUS_E_UNSUPPORTED; +} + +int +onlp_sfpi_control_get(int port, onlp_sfp_control_t control, int* value) +{ + switch(control) { + case ONLP_SFP_CONTROL_RX_LOS: { + VALIDATE_SFP(port); + + if (onlp_file_read_int(value, MODULE_RXLOS_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read rx loss status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + + case ONLP_SFP_CONTROL_TX_FAULT: { + VALIDATE_SFP(port); + + if (onlp_file_read_int(value, MODULE_TXFAULT_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read tx fault status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + + case ONLP_SFP_CONTROL_TX_DISABLE: { + VALIDATE_SFP(port); + + if (onlp_file_read_int(value, MODULE_TXDISABLE_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read tx disabled status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_RESET_STATE: { + VALIDATE_QSFP(port); + + if (onlp_file_read_int(value, MODULE_RESET_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read reset status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_LP_MODE: { + VALIDATE_QSFP(port); + + if (onlp_file_read_int(value, MODULE_LPMODE_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read lp mode status from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + default: + break; + } + + return ONLP_STATUS_E_UNSUPPORTED; +} + +int +onlp_sfpi_denit(void) +{ + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sysi.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sysi.c new file mode 100644 index 000000000..1a9e3b29b --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/sysi.c @@ -0,0 +1,153 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include + +#include +#include +#include +#include +#include +#include +#include "platform_lib.h" + +#include "x86_64_accton_as9947_36xkb_int.h" +#include "x86_64_accton_as9947_36xkb_log.h" + + +#define NUM_OF_CPLD_VER 5 + +static char* cpld_ver_path[NUM_OF_CPLD_VER] = { + "/sys/bus/platform/devices/as9947_36xkb_sys/come_e_cpld_ver", + "/sys/bus/platform/devices/as9947_36xkb_sys/fpga_cpld_ver", + "/sys/bus/platform/devices/as9947_36xkb_sys/fan_cpld_ver", + "/sys/bus/platform/devices/as9947_36xkb_sys/port_cpld1_ver", + "/sys/bus/platform/devices/as9947_36xkb_sys/port_cpld2_ver" +}; + +const char* +onlp_sysi_platform_get(void) +{ + return "x86-64-accton-as9947-36xkb-r0"; +} + +int +onlp_sysi_onie_data_get(uint8_t** data, int* size) +{ + uint8_t* rdata = aim_zmalloc(256); + if (onlp_file_read(rdata, 256, size, IDPROM_PATH) == ONLP_STATUS_OK) { + if(*size == 256) { + *data = rdata; + return ONLP_STATUS_OK; + } + } + + aim_free(rdata); + *size = 0; + return ONLP_STATUS_E_INTERNAL; +} + +int +onlp_sysi_oids_get(onlp_oid_t* table, int max) +{ + int i; + onlp_oid_t* e = table; + memset(table, 0, max*sizeof(onlp_oid_t)); + + /* 9 Thermal sensors on the chassis */ + for (i = 1; i <= CHASSIS_THERMAL_COUNT; i++) { + *e++ = ONLP_THERMAL_ID_CREATE(i); + } + + /* 5 LEDs on the chassis */ + for (i = 1; i <= CHASSIS_LED_COUNT; i++) { + *e++ = ONLP_LED_ID_CREATE(i); + } + + /* 2 PSUs on the chassis */ + for (i = 1; i <= CHASSIS_PSU_COUNT; i++) { + *e++ = ONLP_PSU_ID_CREATE(i); + } + + /* 8 Fans on the chassis */ + for (i = 1; i <= CHASSIS_FAN_COUNT; i++) { + *e++ = ONLP_FAN_ID_CREATE(i); + } + + return 0; +} + + +int +onlp_sysi_platform_info_get(onlp_platform_info_t* pi) +{ + int i, len, ret = ONLP_STATUS_OK; + char *v[NUM_OF_CPLD_VER] = {NULL}; + + for (i = 0; i < AIM_ARRAYSIZE(cpld_ver_path); i++) { + + len = onlp_file_read_str(&v[i], cpld_ver_path[i]); + + if (v[i] == NULL || len <= 0) { + ret = ONLP_STATUS_E_INTERNAL; + break; + } + } + + if (ret == ONLP_STATUS_OK) { + pi->cpld_versions = aim_fstrdup("\r\nCOM_E:%s" + "\r\nFPGA CPLD:%s" + "\r\nFan CPLD:%s" + "\r\nPort CPLD1:%s" + "\r\nPort CPLD2:%s" + , v[0], v[1], v[2], v[3], v[4]); + } + + for (i = 0; i < AIM_ARRAYSIZE(v); i++) { + AIM_FREE_IF_PTR(v[i]); + } + + return ret; +} + +void +onlp_sysi_platform_info_free(onlp_platform_info_t* pi) +{ + aim_free(pi->cpld_versions); +} + +int +onlp_sysi_platform_manage_fans(void) +{ + return ONLP_STATUS_OK; +} + +int +onlp_sysi_platform_manage_leds(void) +{ + return ONLP_STATUS_E_UNSUPPORTED; +} + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/thermali.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/thermali.c new file mode 100644 index 000000000..383641c80 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/thermali.c @@ -0,0 +1,172 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * Thermal Sensor Platform Implementation. + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_THERMAL(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +static char* devfiles__[] = { /* must map with onlp_thermal_id */ + NULL, + NULL, /* CPU_CORE files */ + "/sys/devices/platform/as9947_36xkb_thermal*temp1_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp2_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp3_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp4_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp5_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp6_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp7_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp8_input", + "/sys/devices/platform/as9947_36xkb_thermal*temp9_input", + "/sys/devices/platform/as9947_36xkb_psu*psu1_temp1_input", + "/sys/devices/platform/as9947_36xkb_psu*psu1_temp2_input", + "/sys/devices/platform/as9947_36xkb_psu*psu1_temp3_input", + "/sys/devices/platform/as9947_36xkb_psu*psu2_temp1_input", + "/sys/devices/platform/as9947_36xkb_psu*psu2_temp2_input", + "/sys/devices/platform/as9947_36xkb_psu*psu2_temp3_input" +}; + +static char* cpu_coretemp_files[] = { + "/sys/devices/platform/coretemp.0*temp1_input", + "/sys/devices/platform/coretemp.0*temp2_input", + "/sys/devices/platform/coretemp.0*temp3_input", + "/sys/devices/platform/coretemp.0*temp4_input", + "/sys/devices/platform/coretemp.0*temp5_input", + "/sys/devices/platform/coretemp.0*temp6_input", + "/sys/devices/platform/coretemp.0*temp7_input", + "/sys/devices/platform/coretemp.0*temp8_input", + "/sys/devices/platform/coretemp.0*temp9_input", + NULL, +}; + +/* Static values */ +static onlp_thermal_info_t tinfo[] = { + { }, /* Not used */ + { { ONLP_THERMAL_ID_CREATE(THERMAL_CPU_CORE), "CPU Core", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_MAIN_BROAD), "MB_RearRight_temp(0x4F)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_MAIN_BROAD), "MB_FrontRight_temp(0x4E)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_MAIN_BROAD), "MB_FrontLeft_temp(0x4A)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_4_ON_MAIN_BROAD), "MB_CenterCenter_temp(0x4B)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_5_ON_MAIN_BROAD), "MB_RearCenter_temp(0x4C) Local", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_6_ON_MAIN_BROAD), "MB_RearCenter_temp(0x4C) Remote", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_7_ON_MAIN_BROAD), "MB_FrontRight_temp(0x4D)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_FAN_BROAD), "FAN BOARD(0x4D)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_FAN_BROAD), "FAN BOARD(0x4E)", 0, {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_PSU1), "PSU-1 Thermal Sensor 1", ONLP_PSU_ID_CREATE(PSU1_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_PSU1), "PSU-1 Thermal Sensor 2", ONLP_PSU_ID_CREATE(PSU1_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_PSU1), "PSU-1 Thermal Sensor 3", ONLP_PSU_ID_CREATE(PSU1_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_PSU2), "PSU-2 Thermal Sensor 1", ONLP_PSU_ID_CREATE(PSU2_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_PSU2), "PSU-2 Thermal Sensor 2", ONLP_PSU_ID_CREATE(PSU2_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_PSU2), "PSU-2 Thermal Sensor 3", ONLP_PSU_ID_CREATE(PSU2_ID), {0} }, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + } +}; + +/* + * This will be called to intiialize the thermali subsystem. + */ +int +onlp_thermali_init(void) +{ + return ONLP_STATUS_OK; +} + +/* + * Retrieve the information structure for the given thermal OID. + * + * If the OID is invalid, return ONLP_E_STATUS_INVALID. + * If an unexpected error occurs, return ONLP_E_STATUS_INTERNAL. + * Otherwise, return ONLP_STATUS_OK with the OID's information. + * + * Note -- it is expected that you fill out the information + * structure even if the sensor described by the OID is not present. + */ +int +onlp_thermali_info_get(onlp_oid_t id, onlp_thermal_info_t* info) +{ + int tid; + VALIDATE(id); + + tid = ONLP_OID_ID_GET(id); + *info = tinfo[tid]; + + if (tid == THERMAL_CPU_CORE) { + return onlp_file_read_int_max(&info->mcelsius, cpu_coretemp_files); + } + + return onlp_file_read_int(&info->mcelsius, devfiles__[tid]); +} diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_config.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_config.c new file mode 100644 index 000000000..a9cd143d7 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_config.c @@ -0,0 +1,81 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* */ +#define __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(_x) #_x +#define __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(_x) __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(_x) +x86_64_accton_as9947_36xkb_config_settings_t x86_64_accton_as9947_36xkb_config_settings[] = +{ +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_LOGGING(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_STDLIB(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_UCLI(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION + { __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION), __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION) }, +#else +{ X86_64_ACCTON_AS9947_36XKB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION(__x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME), "__undefined__" }, +#endif + { NULL, NULL } +}; +#undef __x86_64_accton_as9947_36xkb_config_STRINGIFY_VALUE +#undef __x86_64_accton_as9947_36xkb_config_STRINGIFY_NAME + +const char* +x86_64_accton_as9947_36xkb_config_lookup(const char* setting) +{ + int i; + for(i = 0; x86_64_accton_as9947_36xkb_config_settings[i].name; i++) { + if(!strcmp(x86_64_accton_as9947_36xkb_config_settings[i].name, setting)) { + return x86_64_accton_as9947_36xkb_config_settings[i].value; + } + } + return NULL; +} + +int +x86_64_accton_as9947_36xkb_config_show(struct aim_pvs_s* pvs) +{ + int i; + for(i = 0; x86_64_accton_as9947_36xkb_config_settings[i].name; i++) { + aim_printf(pvs, "%s = %s\n", x86_64_accton_as9947_36xkb_config_settings[i].name, x86_64_accton_as9947_36xkb_config_settings[i].value); + } + return i; +} + +/* */ + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_enums.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_enums.c new file mode 100644 index 000000000..8ce9ca654 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_enums.c @@ -0,0 +1,10 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* <--auto.start.enum(ALL).source> */ +/* */ + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_int.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_int.h new file mode 100644 index 000000000..b5d4c0f88 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_int.h @@ -0,0 +1,12 @@ +/**************************************************************************//** + * + * x86_64_accton_as9947_36xkb Internal Header + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9947_36xkb_INT_H__ +#define __x86_64_accton_as9947_36xkb_INT_H__ + +#include + + +#endif /* __x86_64_accton_as9947_36xkb_INT_H__ */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.c new file mode 100644 index 000000000..9d4bb945e --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.c @@ -0,0 +1,17 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#include "x86_64_accton_as9947_36xkb_log.h" +/* + * x86_64_accton_as9947_36xkb log struct. + */ +AIM_LOG_STRUCT_DEFINE( + X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_OPTIONS_DEFAULT, + X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_BITS_DEFAULT, + NULL, /* Custom log map */ + X86_64_ACCTON_AS9947_36XKB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + ); diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.h b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.h new file mode 100644 index 000000000..6b2454913 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_log.h @@ -0,0 +1,12 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9947_36xkb_LOG_H__ +#define __x86_64_accton_as9947_36xkb_LOG_H__ + +#define AIM_LOG_MODULE_NAME x86_64_accton_as9947_36xkb +#include + +#endif /* __x86_64_accton_as9947_36d_LOG_H__ */ diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_module.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_module.c new file mode 100644 index 000000000..5b9f518c9 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_module.c @@ -0,0 +1,24 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#include "x86_64_accton_as9947_36xkb_log.h" + +static int +datatypes_init__(void) +{ +#define x86_64_accton_as9947_36xkb_ENUMERATION_ENTRY(_enum_name, _desc) AIM_DATATYPE_MAP_REGISTER(_enum_name, _enum_name##_map, _desc, AIM_LOG_INTERNAL); +#include + return 0; +} + +void __x86_64_accton_as9947_36xkb_module_init__(void) +{ + AIM_LOG_STRUCT_REGISTER(); + datatypes_init__(); +} + +int __onlp_platform_version__ = 1; diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_ucli.c b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_ucli.c new file mode 100644 index 000000000..3ccd5cd12 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/onlp/builds/x86_64_accton_as9947_36xkb/module/src/x86_64_accton_as9947_36xkb_ucli.c @@ -0,0 +1,50 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#if x86_64_accton_as9947_36xkb_CONFIG_INCLUDE_UCLI == 1 + +#include +#include +#include + +static ucli_status_t +x86_64_accton_as9947_36xkb_ucli_ucli__config__(ucli_context_t* uc) +{ + UCLI_HANDLER_MACRO_MODULE_CONFIG(x86_64_accton_as9947_36xkb) +} + +/* */ +/* */ + +static ucli_module_t +x86_64_accton_as9947_36xkb_ucli_module__ = + { + "x86_64_accton_as9947_36xkb_ucli", + NULL, + x86_64_accton_as9947_36xkb_ucli_ucli_handlers__, + NULL, + NULL, + }; + +ucli_node_t* +x86_64_accton_as9947_36xkb_ucli_node_create(void) +{ + ucli_node_t* n; + ucli_module_init(&x86_64_accton_as9947_36xkb_ucli_module__); + n = ucli_node_create("x86_64_accton_as9947_36xkb", NULL, &x86_64_accton_as9947_36xkb_ucli_module__); + ucli_node_subnode_add(n, ucli_module_log_node_create("x86_64_accton_as9947_36xkb")); + return n; +} + +#else +void* +x86_64_accton_as9947_36xkb_ucli_node_create(void) +{ + return NULL; +} +#endif + diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/Makefile b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/PKG.yml b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/PKG.yml new file mode 100644 index 000000000..a46346394 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/platform-config-platform.yml ARCH=amd64 VENDOR=accton BASENAME=x86-64-accton-as9947-36xkb REVISION=r0 diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/lib/x86-64-accton-as9947-36xkb-r0.yml b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/lib/x86-64-accton-as9947-36xkb-r0.yml new file mode 100644 index 000000000..7f560449b --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/lib/x86-64-accton-as9947-36xkb-r0.yml @@ -0,0 +1,32 @@ +--- + +###################################################################### +# +# platform-config for AS9947 +# +###################################################################### + +x86-64-accton-as9947-36xkb-r0: + + grub: + + serial: >- + --port=0x3f8 + --speed=115200 + --word=8 + --parity=no + --stop=1 + + kernel: + <<: *kernel-6-12 + + args: >- + nopat + console=ttyS0,115200n8 + intel_iommu=off + + ##network + ## interfaces: + ## ma1: + ## name: ~ + ## syspath: pci0000:00/0000:00:14.0 diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py new file mode 100644 index 000000000..b29478045 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py @@ -0,0 +1,108 @@ +import commands +from itertools import chain +from onl.platform.base import * +from onl.platform.accton import * +from time import sleep + + +init_ipmi_dev = [ + 'echo "remove,kcs,i/o,0xca2" > /sys/module/ipmi_si/parameters/hotmod', + 'echo "add,kcs,i/o,0xca2" > /sys/module/ipmi_si/parameters/hotmod'] + +ATTEMPTS = 5 +INTERVAL = 3 + +def init_ipmi_dev_intf(): + attempts = ATTEMPTS + interval = INTERVAL + + while attempts: + if os.path.exists('/dev/ipmi0') or os.path.exists('/dev/ipmidev/0'): + return (True, (ATTEMPTS - attempts) * interval) + + for i in range(0, len(init_ipmi_dev)): + commands.getstatusoutput(init_ipmi_dev[i]) + + attempts -= 1 + sleep(interval) + + return (False, ATTEMPTS * interval) + +def init_ipmi_oem_cmd(): + attempts = ATTEMPTS + interval = INTERVAL + + while attempts: + status, output = commands.getstatusoutput('ipmitool raw 0x34 0x95') + if status: + attempts -= 1 + sleep(interval) + continue + + return (True, (ATTEMPTS - attempts) * interval) + + return (False, ATTEMPTS * interval) + +def init_ipmi(): + attempts = ATTEMPTS + interval = 60 + + while attempts: + attempts -= 1 + + (status, elapsed_dev) = init_ipmi_dev_intf() + if status is not True: + sleep(interval - elapsed_dev) + continue + + (status, elapsed_oem) = init_ipmi_oem_cmd() + if status is not True: + sleep(interval - elapsed_dev - elapsed_oem) + continue + + print('IPMI dev interface is ready.') + return True + + print('Failed to initialize IPMI dev interface') + return False + +class OnlPlatform_x86_64_accton_as9947_36xkb_r0(OnlPlatformAccton, + OnlPlatformPortConfig_24x400_2x10): + PLATFORM='x86-64-accton-as9947-36xkb-r0' + MODEL="AS9947-36XKB" + SYS_OBJECT_ID=".9947.36" + + def modprobe(self, module, required=True, params={}): + cmd = "modprobe %s" % module + subprocess.check_call(cmd, shell=True) + + def baseconfig(self): + if init_ipmi() is not True: + return False + + self.modprobe('optoe') + self.modprobe('at24') + self.modprobe('accton_ipmi_intf') + + for m in [ 'i2c-ocores', 'fpga', 'fan', 'psu', 'thermal', 'sys', 'leds','sfp']: + self.insmod("x86-64-accton-as9947-36xkb-%s" % m) + + # initialize SFP devices + for port in range(5, 41): + subprocess.call('echo 0 > /sys/devices/platform/as9947_36xkb_sfp/module_reset_%d' % (port), shell=True) + + + for port in range(1, 5): + self.new_i2c_device('optoe2', 0x50, port) + + for port in range(5, 29): + self.new_i2c_device('optoe1', 0x50, port) + + for port in range(29, 41): + self.new_i2c_device('optoe3', 0x50, port) + + + for port in range(1, 41): + subprocess.call('echo port%d > /sys/bus/i2c/devices/%d-0050/port_name' % (port, port), shell=True) + + return True From 01b4c636bb49e2138fb082f7688da10c7782959c Mon Sep 17 00:00:00 2001 From: vincent_chiang Date: Fri, 10 Apr 2026 00:51:56 +0000 Subject: [PATCH 2/2] [AS9947-36XKB] Remove redundant modprobe at24 at24 is auto-loaded via device tree / module alias on kernel 6.12, so explicit modprobe in baseconfig() is no longer needed. Co-Authored-By: Claude Opus 4.6 --- .../r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py index b29478045..86ae201d7 100644 --- a/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py +++ b/packages/platforms/accton/x86-64/as9947-36xkb/platform-config/r0/src/python/x86_64_accton_as9947_36xkb_r0/__init__.py @@ -81,7 +81,6 @@ def baseconfig(self): return False self.modprobe('optoe') - self.modprobe('at24') self.modprobe('accton_ipmi_intf') for m in [ 'i2c-ocores', 'fpga', 'fan', 'psu', 'thermal', 'sys', 'leds','sfp']: