diff --git a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/fani.c b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/fani.c index 7c9334a7..0ffdc3cb 100755 --- a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/fani.c +++ b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/fani.c @@ -34,8 +34,8 @@ } \ } while(0) -#define MAX_FAN_SPEED 15400 -#define BIT(i) (1 << (i)) +/*From minipack_system_spec, max RPM is 12000+10%.*/ +#define MAX_FAN_SPEED (13200) enum fan_id { FAN_1_ON_FAN_BOARD = 1, @@ -45,11 +45,14 @@ enum fan_id { FAN_5_ON_FAN_BOARD, FAN_6_ON_FAN_BOARD, FAN_7_ON_FAN_BOARD, - FAN_8_ON_FAN_BOARD, + FAN_8_ON_FAN_BOARD, }; -#define FCM_TOP_BOARD_PATH "/sys/bus/i2c/devices/64-0033/fantray%d_present| head -1" -#define FCM_BOT_BOARD_PATH "/sys/bus/i2c/devices/72-0033/fantray%d_present| head -1" +/* Depent on RPM to judge if fan is present.*/ +/* +#define FCM_TOP_BOARD_PATH "/sys/bus/i2c/devices/72-0033/fantray%d_present| head -1" +#define FCM_BOT_BOARD_PATH "/sys/bus/i2c/devices/64-0033/fantray%d_present| head -1" +*/ #define FAN_BOARD_PATH "/sys/bus/platform/devices/minipack_psensor/" @@ -88,9 +91,8 @@ onlp_fani_init(void) int onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) { - int value = 0, fid, board_id; + int value = 0, fid, fan_input; char path[128]= {0}; - char *fantray_path[2] = {FCM_TOP_BOARD_PATH, FCM_BOT_BOARD_PATH}; VALIDATE(id); fid = ONLP_OID_ID_GET(id); @@ -98,35 +100,33 @@ onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) /* get fan present status */ - board_id = (fid-1)/(CHASSIS_FAN_COUNT/2); - sprintf(path, fantray_path[board_id], ((fid-1)/2)+1); - if (bmc_file_read_int(&value, path, 16) < 0) { - AIM_LOG_ERROR("Unable to read status from file (%s)\r\n", path); - return ONLP_STATUS_E_INTERNAL; - } - - if (value) { - return ONLP_STATUS_OK; - } info->status |= ONLP_FAN_STATUS_PRESENT; - /* get front fan rpm */ - sprintf(path, "%s""fan%d_input", FAN_BOARD_PATH, fid*2 - 1); + fan_input = ((fid-1) % 2)*CHASSIS_FAN_COUNT; + fan_input += (fid) + (fid%2); + + DEBUG_PRINT("fan%d_input: for fid:%d\n",fan_input, fid); + sprintf(path, "%s""fan%d_input", FAN_BOARD_PATH, fan_input - 1); if (onlp_file_read_int(&value, path) < 0) { AIM_LOG_ERROR("Unable to read status from file (%s)\r\n", path); return ONLP_STATUS_E_INTERNAL; - } + } info->rpm = value; /* get rear fan rpm */ - sprintf(path, "%s""fan%d_input", FAN_BOARD_PATH, fid*2); + sprintf(path, "%s""fan%d_input", FAN_BOARD_PATH, fan_input); if (onlp_file_read_int(&value, path) < 0) { AIM_LOG_ERROR("Unable to read status from file (%s)\r\n", path); return ONLP_STATUS_E_INTERNAL; - } + } + + if (info->rpm == 0 && value == 0) { + info->status = info->status & ~ONLP_FAN_STATUS_PRESENT; + return ONLP_STATUS_OK; + } /* take the min value from front/rear fan speed */ @@ -134,7 +134,6 @@ onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) info->rpm = value; } - /* set fan status based on rpm */ if (!info->rpm) { @@ -142,8 +141,7 @@ onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) return ONLP_STATUS_OK; } - - /* get speed percentage from rpm + /* get speed percentage from rpm */ info->percentage = (info->rpm * 100)/MAX_FAN_SPEED; @@ -180,10 +178,11 @@ int onlp_fani_percentage_set(onlp_oid_t id, int p) { char cmd[32] = {0}; + char resp[256]; sprintf(cmd, "set_fan_speed.sh %d", p); - if (bmc_send_command(cmd) < 0) { + if (bmc_reply(cmd, resp, sizeof(resp)) < 0) { AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); return ONLP_STATUS_E_INTERNAL; } diff --git a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.c b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.c index 21286b61..0d875423 100755 --- a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.c +++ b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.c @@ -24,6 +24,8 @@ ***********************************************************/ #include #include +#include +#include #include #include #include @@ -31,150 +33,209 @@ #define TTY_DEVICE "/dev/ttyACM0" #define TTY_PROMPT "@bmc-oob:" -#define TTY_I2C_TIMEOUT 55000 +#define TTY_USER "root" +#define TTY_I2C_TIMEOUT 100000 #define TTY_BMC_LOGIN_TIMEOUT 1000000 +#define TTY_LOGIN_RETRY (8) #define TTY_RETRY PLATFOTM_H_TTY_RETRY -#define MAXIMUM_TTY_BUFFER_LENGTH 1024 -#define MAXIMUM_TTY_STRING_LENGTH (MAXIMUM_TTY_BUFFER_LENGTH - 1) -static int tty_fd = -1; -static char tty_buf[MAXIMUM_TTY_BUFFER_LENGTH] = {0}; - -static int tty_open(void) +static int tty_open(int *fd) { + int ret; int i = 20; struct termios attr; - if (tty_fd > -1) { - return 0; + if (*fd > -1) { + return ONLP_STATUS_OK; } do { - if ((tty_fd = open(TTY_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY)) > -1) { - tcgetattr(tty_fd, &attr); - attr.c_cflag = B57600 | CS8 | CLOCAL | CREAD; - attr.c_iflag = IGNPAR; - attr.c_oflag = 0; - attr.c_lflag = 0; - attr.c_cc[VMIN] = (unsigned char) - ((MAXIMUM_TTY_STRING_LENGTH > 0xFF) ? 0xFF : MAXIMUM_TTY_STRING_LENGTH); - attr.c_cc[VTIME] = 0; - cfsetospeed(&attr, B57600); - cfsetispeed(&attr, B57600); - tcsetattr(tty_fd, TCSANOW, &attr); - return 0; + if ((*fd = open(TTY_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY)) > -1) { + ret = flock(*fd, LOCK_EX | LOCK_NB); + if (ret == -1 && errno != EWOULDBLOCK) { + AIM_LOG_ERROR("ERROR: Cannot flock TTY device\n"); + return ONLP_STATUS_E_INTERNAL; + } else { + tcgetattr(*fd, &attr); + attr.c_cflag = B57600 | CS8 | CLOCAL | CREAD; + attr.c_iflag = IGNPAR; + attr.c_oflag = 0; + attr.c_lflag = 0; + attr.c_cc[VMIN] = (unsigned char) + ((MAXIMUM_TTY_STRING_LENGTH > 0xFF) ? 0xFF : MAXIMUM_TTY_STRING_LENGTH); + attr.c_cc[VTIME] = 0; + cfsetospeed(&attr, B57600); + cfsetispeed(&attr, B57600); + tcsetattr(*fd, TCSANOW, &attr); + return ONLP_STATUS_OK; + } } i--; usleep(100000); } while (i > 0); - - return -1; + return ONLP_STATUS_E_GENERIC; } -static int tty_close(void) +static int tty_close(int *fd) { - close(tty_fd); - tty_fd = -1; + if (flock(*fd, LOCK_UN) == -1) { + AIM_LOG_ERROR("ERROR: Cannot unlock TTY device file\n"); + return ONLP_STATUS_E_INTERNAL; + } + close(*fd); + *fd = -1; return 0; } -static int tty_exec_buf(unsigned long udelay, const char *str) -{ - if (tty_fd < 0) - return -1; +static int tty_clear_rxbuf(int fd, char* buf, int max_size) { + int ret; - write(tty_fd, tty_buf, strlen(tty_buf)+1); - usleep(udelay); - usleep(50000); - memset(tty_buf, 0, MAXIMUM_TTY_BUFFER_LENGTH); - read(tty_fd, tty_buf, MAXIMUM_TTY_BUFFER_LENGTH); - return (strstr(tty_buf, str) != NULL) ? 0 : -1; + if (!buf) { + return ONLP_STATUS_E_PARAM; + } + + if (fd < 0) { + return -1; + } + + do { + usleep(100000); + ret = read(fd, buf, max_size); + if (ret > 0) + DEBUG_PRINT("clear %d bytes, \"%s\"\n", ret, buf); + + memset(buf, 0, max_size); + } while (ret > 0); + + return ret; } -static int tty_login(void) +static int tty_write_and_read( int fd, const char *cmd, + unsigned long udelay, char *buf, int buf_size) +{ + int ret, len, retry; + if (fd < 0 || !cmd) { + return ONLP_STATUS_E_PARAM; + } + + tty_clear_rxbuf(fd, buf, buf_size); + retry = 0; + len = strlen(cmd)+1; + do { + ret = write(fd, cmd, len); + retry++ ; + } while(ret != len && retry<5 && usleep(100000*retry)); + + DEBUG_PRINT("sent cmd:%s\n",cmd); + usleep(udelay); + usleep(100000); + memset(buf, 0, buf_size); + ret = read(fd, buf, buf_size); + DEBUG_PRINT("Read %d bytes, \"%s\", \n", ret, buf); + return ret; +} + +static int tty_access_and_match( int fd, const char *cmd, + unsigned long udelay, const char *keywd) +{ + int num; + char resp[256] = {0}; + + num = tty_write_and_read(fd, cmd, udelay, resp, sizeof(resp)); + if (num <= 0) { + return ONLP_STATUS_E_GENERIC; + } + return (strstr(resp, keywd) != NULL) ? + ONLP_STATUS_OK : ONLP_STATUS_E_GENERIC; +} + +static bool is_logged_in(int fd, char *resp, int max_size) +{ + int num; + + num = tty_write_and_read(fd, "\r\r", TTY_I2C_TIMEOUT, resp, sizeof(resp)); + if (num <= 0) { + return ONLP_STATUS_E_GENERIC; + } + return (strstr(resp, TTY_PROMPT) != NULL) ? + ONLP_STATUS_OK : ONLP_STATUS_E_GENERIC; +} + +static int tty_login(int fd) { int i; + char resp[256]; - for (i = 1; i <= TTY_RETRY; i++) { - snprintf(tty_buf, MAXIMUM_TTY_BUFFER_LENGTH, "\r\r"); - if (!tty_exec_buf(0, TTY_PROMPT)) { - return 0; + for (i = 0; i < TTY_LOGIN_RETRY; i++) { + if (is_logged_in(fd, resp, sizeof(resp))) { + DEBUG_PRINT("Been logged in!\n"); + return ONLP_STATUS_OK; } - if (strstr(tty_buf, "bmc-oob login:") != NULL) + + DEBUG_PRINT("Try to login!\n"); + if (strstr(resp, "bmc") != NULL && + (strstr(resp, "login:") != NULL)) { - snprintf(tty_buf, MAXIMUM_TTY_BUFFER_LENGTH, "root\r"); - if (!tty_exec_buf(TTY_BMC_LOGIN_TIMEOUT, "Password:")) { - snprintf(tty_buf, MAXIMUM_TTY_BUFFER_LENGTH, "0penBmc\r"); - if (!tty_exec_buf(TTY_BMC_LOGIN_TIMEOUT, TTY_PROMPT)) { - return 0; + if (!tty_access_and_match(fd, TTY_USER"\r",TTY_BMC_LOGIN_TIMEOUT, "Password:")) { + if (!tty_access_and_match(fd, "0penBmc\r", TTY_BMC_LOGIN_TIMEOUT, TTY_PROMPT)) { + return ONLP_STATUS_OK; } } } - usleep(50000); + usleep(50000*i); } - - return -1; + return ONLP_STATUS_E_GENERIC; } -int bmc_tty_init(void) +static int tty_transaction(const char *cmd, unsigned long udelay, char *resp, int max_size) { - int i; - if (tty_fd >= 0) { - return 0; + int i; + char *buf; + int tty_fd = -1; + int num, ret = ONLP_STATUS_OK; + int buf_size = MAXIMUM_TTY_BUFFER_LENGTH; + + if (!cmd || !resp || !max_size) + return ONLP_STATUS_E_PARAM; + + if (tty_open(&tty_fd) != ONLP_STATUS_OK) { + AIM_LOG_ERROR("ERROR: Cannot open TTY device\n"); + return ONLP_STATUS_E_GENERIC; } - for (i = 1; i <= TTY_RETRY; i++) { - if (tty_open() != 0) { - AIM_LOG_ERROR("ERROR: Cannot open TTY device\n"); - continue; + /*Tried to login.*/ + for (i = 0; i < TTY_RETRY; i++) { + if (tty_login(tty_fd) == ONLP_STATUS_OK) { + break; } - - if (tty_login() != 0) { - AIM_LOG_ERROR("ERROR: Cannot login TTY device\n"); - tty_close(); - continue; - } - - return 0; } - - AIM_LOG_ERROR("Unable to init bmc tty\r\n"); - return -1; + if(i == TTY_RETRY) { + AIM_LOG_ERROR("ERROR: Cannot login TTY device\n"); + return ONLP_STATUS_E_GENERIC; + } + buf = (char *)calloc(buf_size, sizeof(char)); + if (buf == NULL) + { + AIM_LOG_ERROR("ERROR: Cannot allocate memory\n"); + goto exit; + } + num = tty_write_and_read(tty_fd, cmd, udelay, buf, buf_size); + if (num <= 0) + { + AIM_LOG_ERROR("ERROR: Cannot read/write TTY device\n"); + goto exit; + } + strncpy(resp, buf, max_size); + DEBUG_PRINT("Resp:\"%s\", \n", resp); +exit: + free(buf); + tty_close(&tty_fd); + return ret; } -int bmc_tty_deinit(void) -{ - if( tty_fd != -1 ) { - tty_close(); - } - else { - AIM_LOG_ERROR("ERROR: TTY not open\n"); - } - return 0; -} - -int bmc_send_command(char *cmd) -{ - int i, ret = 0; - - bmc_tty_init(); - memset(tty_buf, 0, MAXIMUM_TTY_BUFFER_LENGTH); - for (i = 1; i <= TTY_RETRY; i++) { - snprintf(tty_buf, MAXIMUM_TTY_BUFFER_LENGTH, "%s", cmd); - ret = tty_exec_buf(TTY_I2C_TIMEOUT, TTY_PROMPT); - if (ret != 0) { -// AIM_LOG_ERROR("ERROR: bmc_send_command(%s) timed out\n", cmd); - continue; - } - return 0; - } - AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); - return -1; -} - -int chk_numeric_char(char *data, int base) +static int chk_numeric_char(char *data, int base) { int len, i, orig = 0; @@ -232,19 +293,79 @@ int chk_numeric_char(char *data, int base) return 0; } +static int strip_off_prompt(char *buf, int max_size) +{ + char *p; + + p = strstr(buf, TTY_USER TTY_PROMPT); + if (p != NULL && p < (buf+max_size)) { + *p = '\0'; + } + return ONLP_STATUS_OK; +} + +int bmc_reply_pure(char *cmd, unsigned long udelay, char *resp, int max_size) +{ + int i, ret = 0; + char *p; + char cmdr[128]; + + /*In case, caller forgets put the "enter" at the very end of cmd.*/ + snprintf(cmdr, sizeof(cmdr), "%s%s", cmd, "\r"); + + for (i = 1; i <= TTY_RETRY; i++) { + ret = tty_transaction(cmdr, udelay, resp, max_size); + if (ret != ONLP_STATUS_OK) { + usleep(100000*i); + continue; + } + + strip_off_prompt(resp, max_size); + /*Find if cmd is inside the response.*/ + p = strstr(resp, cmd); + if (p != NULL) { + memcpy(resp, p+strlen(cmd), max_size); + return ONLP_STATUS_OK; + } + DEBUG_PRINT("Resp: [%s]\n", resp); + } + AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); + return ONLP_STATUS_E_GENERIC; +} + +int bmc_reply(char *cmd, char *resp, int max_size) +{ + int i, ret = 0; + + for (i = 1; i <= TTY_RETRY; i++) { + ret = tty_transaction(cmd, TTY_I2C_TIMEOUT, resp, max_size); + if (ret != ONLP_STATUS_OK) { + continue; + } + if (strstr(resp, TTY_PROMPT) == NULL) { + continue; + } + return ONLP_STATUS_OK; + } + AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); + return ONLP_STATUS_E_GENERIC; +} + int bmc_command_read_int(int* value, char *cmd, int base) { int len; int i; + char resp[256]; char *prev_str = NULL; char *current_str= NULL; - if (bmc_send_command(cmd) < 0) { + if (bmc_reply(cmd, resp, sizeof(resp)) < 0) { return ONLP_STATUS_E_INTERNAL; } + len = (int)strlen(cmd); - prev_str = strstr(tty_buf, cmd); + prev_str = strstr(resp, cmd); if (prev_str == NULL) { return -1; } @@ -294,19 +415,21 @@ int bmc_i2c_writeb(uint8_t bus, uint8_t devaddr, uint8_t addr, uint8_t value) { char cmd[128] = {0}; + char resp[128]; snprintf(cmd, sizeof(cmd), "i2cset -f -y %d 0x%x 0x%02x 0x%x\r\n", bus, devaddr, addr, value); - return bmc_send_command(cmd); + return bmc_reply(cmd, resp, sizeof(resp)); } int -bmc_i2c_readw(uint8_t bus, uint8_t devaddr, uint8_t addr) +bmc_i2c_readw(uint8_t bus, uint8_t devaddr, uint8_t addr, uint16_t *data) { int ret = 0, value; char cmd[128] = {0}; snprintf(cmd, sizeof(cmd), "i2cget -f -y %d 0x%x 0x%02x w\r\n", bus, devaddr, addr); ret = bmc_command_read_int(&value, cmd, 16); - return (ret < 0) ? ret : value; + *data = value; + return ret; } int @@ -314,15 +437,16 @@ bmc_i2c_readraw(uint8_t bus, uint8_t devaddr, uint8_t addr, char* data, int data { int data_len, i = 0; char cmd[128] = {0}; + char resp[256]; char *str = NULL; snprintf(cmd, sizeof(cmd), "i2craw -w 0x%x -r 0 %d 0x%02x\r\n", addr, bus, devaddr); - if (bmc_send_command(cmd) < 0) { + if (bmc_reply(cmd, resp, sizeof(resp)) < 0) { AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); return ONLP_STATUS_E_INTERNAL; } - str = strstr(tty_buf, "Received:\r\n "); + str = strstr(resp, "Received:\r\n "); if (str == NULL) { return -1; } diff --git a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.h b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.h index 1a48fe12..cb022b6e 100755 --- a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.h +++ b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/platform_lib.h @@ -32,11 +32,13 @@ #if (DEBUG_MODE == 1) #define DEBUG_PRINT(fmt, args...) \ - printf("%s:%s[%d]: " fmt "\r\n", __FILE__, __FUNCTION__, __LINE__, ##args) + printf("%s#%d: " fmt "\r\n", __FUNCTION__, __LINE__, ##args) #else #define DEBUG_PRINT(fmt, args...) #endif +#define BIT(i) (1 << (i)) + #define CHASSIS_FAN_COUNT 8 #define CHASSIS_THERMAL_COUNT 8 #define CHASSIS_LED_COUNT 2 @@ -45,6 +47,8 @@ #define IDPROM_PATH "/sys/bus/i2c/devices/1-0057/eeprom" #define PLATFOTM_H_TTY_RETRY (5) +#define MAXIMUM_TTY_BUFFER_LENGTH 1024 +#define MAXIMUM_TTY_STRING_LENGTH (MAXIMUM_TTY_BUFFER_LENGTH - 1) enum onlp_thermal_id { @@ -59,16 +63,13 @@ enum onlp_thermal_id THERMAL_7_ON_MAIN_BROAD, }; -int bmc_send_command(char *cmd); +int bmc_reply_pure(char *cmd, unsigned long udelay, char *resp, int max_size); +int bmc_reply(char *cmd, char *resp, int max_size); int bmc_file_read_int(int* value, char *file, int base); int bmc_i2c_readb(uint8_t bus, uint8_t devaddr, uint8_t addr); int bmc_i2c_writeb(uint8_t bus, uint8_t devaddr, uint8_t addr, uint8_t value); -int bmc_i2c_readw(uint8_t bus, uint8_t devaddr, uint8_t addr); +int bmc_i2c_readw(uint8_t bus, uint8_t devaddr, uint8_t addr, uint16_t *data); int bmc_i2c_readraw(uint8_t bus, uint8_t devaddr, uint8_t addr, char* data, int data_size); - -int bmc_tty_init(void); -int bmc_tty_deinit(void); - #endif /* __PLATFORM_LIB_H__ */ diff --git a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/psui.c b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/psui.c old mode 100755 new mode 100644 index 2bc2fb57..84ca6a81 --- a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/psui.c +++ b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/psui.c @@ -37,10 +37,10 @@ } while(0) enum { - PSU1_ID = 1, /*Left-upper*/ - PSU2_ID, /*Left-lower*/ - PSU3_ID, /*Right-upper*/ - PSU4_ID, /*Right-lower*/ + PSU1_ID = 1, /*At the left-upper of the front view.*/ + PSU2_ID, /*At the left-lower of the front view.*/ + PSU3_ID, /*At the right-upper of the front view.*/ + PSU4_ID, /*At the right-lower of the front view.*/ }; /* @@ -52,7 +52,7 @@ static onlp_psu_info_t pinfo[] = { {ONLP_PSU_ID_CREATE(PSU1_ID), "PSU-1", 0}, }, { {ONLP_PSU_ID_CREATE(PSU2_ID), "PSU-2", 0}, }, { {ONLP_PSU_ID_CREATE(PSU3_ID), "PSU-3", 0}, }, - { {ONLP_PSU_ID_CREATE(PSU4_ID), "PSU-4", 0}, }, + { {ONLP_PSU_ID_CREATE(PSU4_ID), "PSU-4", 0}, }, }; int @@ -60,30 +60,78 @@ onlp_psui_init(void) { return ONLP_STATUS_OK; } -/* -static int -twos_complement_to_int(uint16_t data, uint8_t valid_bit, int mask) -{ - uint16_t valid_data = data & mask; - bool is_negative = valid_data >> (valid_bit - 1); - return is_negative ? (-(((~valid_data) & mask) + 1)) : valid_data; +#define PMBUS_PATH_FORMAT "/sys/bus/platform/devices/minipack_psensor/%s%d_input" + +static int +onlp_psui_rm_special_char(char* in, char* out, int max) +{ + int i, j; + char c; + + DEBUG_PRINT("Before strip: [%s]\n", in); + j = 0; + for (i = 0; in[i]; i++) { + c = in[i]; + if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'z')) { + out[j]=in[i]; + j++; + } + if (j == max) + break; + } + out[j] = '\0'; + DEBUG_PRINT("After strip: [%s]\n", out); + return j; } static int -pmbus_parse_literal_format(uint16_t value) +onlp_psui_get_BMC_info(int pid, onlp_psu_info_t* info) { - int exponent, mantissa, multiplier = 1000; + char cmd[128] = {0}; + char resp[128] = {0}; - exponent = twos_complement_to_int(value >> 11, 5, 0x1f); - mantissa = twos_complement_to_int(value & 0x7ff, 11, 0x7ff); + int i2c_addr[][2] = {{49, 0x59},{48, 0x58},{57, 0x59},{56, 0x58}}; + int model = 0x9a; + int serial = 0x9e; + int bus, addr, offset; + char *bcmd = "i2cdump -y -f %d 0x%x s 0x%x|tail -n +2|cut -c56-"; - return (exponent >= 0) ? (mantissa << exponent) * multiplier : - (mantissa * multiplier) / (1 << -exponent); -}*/ + bus = i2c_addr[pid-1][0]; + addr = i2c_addr[pid-1][1]; -#define PMBUS_PATH_FORMAT "/sys/bus/platform/devices/minipack_psensor/%s%d_input" + /* PSU is present if its model name can be retrieved.*/ + offset = model; + sprintf(cmd, bcmd, bus, addr, offset); + memset(info->model, 0, sizeof(info->model)); + if (bmc_reply_pure(cmd, 200000, resp, sizeof(resp)) < 0) { + AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_OK; + } + + /*i2cdump return "failed" when slave is not present.*/ + if (strstr(resp, "failed") != NULL) { + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_OK; + } + + info->status |= ONLP_PSU_STATUS_PRESENT; + info->caps = ONLP_PSU_CAPS_AC; + onlp_psui_rm_special_char(resp, info->model, sizeof(info->model)-1); + + offset = serial; + sprintf(cmd, bcmd, bus, addr, offset); + memset(info->serial, 0, sizeof(info->serial)); + if (bmc_reply_pure(cmd, 200000, resp, sizeof(resp)) < 0) { + AIM_LOG_ERROR("Unable to send command to bmc(%s)\r\n", cmd); + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_OK; + } + onlp_psui_rm_special_char(resp, info->serial, sizeof(info->serial)-1); + return ONLP_STATUS_OK; +} int onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) @@ -92,53 +140,24 @@ onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) char path[64] = {0}; VALIDATE(id); - pid = ONLP_OID_ID_GET(id); *info = pinfo[pid]; /* Set the onlp_oid_hdr_t */ /* Get the present status */ -#if 0 - uint8_t mask = 0; - mask = 1 << ((pid-1) * 4); - value = onlp_i2c_readb(1, 0x32, 0x10, ONLP_I2C_F_FORCE); - if (value < 0) { + + if (onlp_psui_get_BMC_info(pid, info) != ONLP_STATUS_OK) + { return ONLP_STATUS_E_INTERNAL; } - - if (value & mask) { - info->status &= ~ONLP_PSU_STATUS_PRESENT; - return ONLP_STATUS_OK; - } - info->status |= ONLP_PSU_STATUS_PRESENT; - info->caps = ONLP_PSU_CAPS_AC; - - /* Get power good status - */ - mask = 1 << ((pid-1) * 4 + 1); - if (!(value & mask)) { - info->status |= ONLP_PSU_STATUS_FAILED; + if (!(info->status & ONLP_PSU_STATUS_PRESENT)) { return ONLP_STATUS_OK; } - - /* Get input output power status - */ - value = (pid == PSU1_ID) ? 0x2 : 0x1; /* mux channel for psu */ - if (bmc_i2c_writeb(7, 0x70, 0, value) < 0) { - return ONLP_STATUS_E_INTERNAL; - } - usleep(1200); -#else - - info->status |= ONLP_PSU_STATUS_PRESENT; - info->caps = ONLP_PSU_CAPS_AC; -#endif pid_in = (pid * 2) - 1; pid_out = pid * 2; - - + DEBUG_PRINT("in%d_input: for pid:%d\n",pid_in, pid); /* Read vin */ sprintf(path, PMBUS_PATH_FORMAT, "in", pid_in); if (onlp_file_read_int(&value, path) < 0) { @@ -179,8 +198,8 @@ onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) return ONLP_STATUS_E_INTERNAL; } if (value >= 0) { - info->mvout = value; - info->caps |= ONLP_PSU_CAPS_VOUT; + info->mvout = value; + info->caps |= ONLP_PSU_CAPS_VOUT; } /* Read iout */ @@ -204,14 +223,6 @@ onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) info->mpout = value/1000; /*power is in unit of microWatts.*/; info->caps |= ONLP_PSU_CAPS_POUT; } - - /* Get model name */ - //bmc_i2c_readraw(7, addr, 0x9a, info->model, sizeof(info->model)); - info->model[0] = '0'; - /* Get serial number */ - //bmc_i2c_readraw(7, addr, 0x9e, info->serial, sizeof(info->serial)); - info->serial[0] = '0'; - return 0; } diff --git a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/sfpi.c b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/sfpi.c index 7823b144..9065186b 100755 --- a/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/sfpi.c +++ b/packages/platforms/accton/x86-64/x86-64-accton-minipack/onlp/builds/src/module/src/sfpi.c @@ -28,10 +28,8 @@ #include #include #include "platform_lib.h" - #include "x86_64_accton_minipack_log.h" -#define BIT(i) (1 << (i)) #define NUM_OF_PIM (8) #define TYPES_OF_PIM (2) #define NUM_OF_SFP_PORT (128) @@ -146,6 +144,8 @@ onlp_pim_is_present(int pim) present = bmc_i2c_readb(bus, addr, offset); if (present < 0) { ps.pim.valid = 0; + ps.pim.present = 0xff; /*1 means absent*/ + present = 0xff; return ONLP_STATUS_E_INTERNAL; } ps.pim.valid = 1; @@ -158,10 +158,20 @@ onlp_pim_is_present(int pim) return !(present & BIT(pim % NUM_OF_PIM)); } +static int update_ports(present_status_t *ports, uint32_t present) { + ports->valid = 1; + ports->present = present; + ports->last_poll = time (NULL); + return ONLP_STATUS_OK; +} + + +/*bit_array is the present bitmap of a PIM, not all ports of PIMs.*/ static int -get_port_present_bmap(int port, uint32_t *bit_array) +get_pim_port_present_bmap(int port, uint32_t *bit_array) { time_t cur, elapse; + int ret; uint32_t present, pim; present_status_t *ports; int bus[NUM_OF_PIM] = {80, 88, 96, 104, 112, 120, 128, 136}; @@ -169,20 +179,26 @@ get_port_present_bmap(int port, uint32_t *bit_array) int offset = 0x12; pim = port/SFP_PORT_PER_PIM; - ports = &ps.port_at_pim[pim]; + + /*If PIM not present, set all 1's to pbmap.*/ + if(onlp_pim_is_present(pim) == 0) { + update_ports(ports, 0xffff); + *bit_array = onlp_sfpi_reg_val_to_port_sequence(ports->present, 0); + return ONLP_STATUS_OK; + } + cur = time (NULL); elapse = cur - ports->last_poll; - if (!ports->valid || (elapse > PORT_POLL_INTERVAL)) { - present = bmc_i2c_readw(bus[pim], addr[0], offset); - if (present < 0) { + ret = bmc_i2c_readw(bus[pim], addr[0], offset, (uint16_t*)&present); + if (ret < 0) { ports->valid = 0; + ports->present = 0xffff; /*No needs to flip in port order.*/ + *bit_array = 0xffff; /*No needs to flip in port order.*/ return ONLP_STATUS_E_INTERNAL; } - ports->valid = 1; - ports->present = present; - ports->last_poll = time (NULL); + update_ports(ports, present); } else { present = ports->present; } @@ -191,7 +207,6 @@ get_port_present_bmap(int port, uint32_t *bit_array) return ONLP_STATUS_OK; } - int onlp_sfpi_is_present(int port) { @@ -206,21 +221,22 @@ onlp_sfpi_is_present(int port) pim = port/SFP_PORT_PER_PIM; present = onlp_pim_is_present(pim); if (present < 0) { - return ONLP_STATUS_E_INTERNAL; + return present; } if (!present) { + update_ports(&ps.port_at_pim[pim], 0xff); return 0; } - ret = get_port_present_bmap(port, &bit_array); + ret = get_pim_port_present_bmap(port, &bit_array); if (ret < 0) { return ret; } - return !(bit_array & BIT(port % SFP_PORT_PER_PIM)); } + int onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst) { @@ -231,7 +247,7 @@ onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst) /*Get present bitmap per PIM.*/ for (i = 0; i < NUM_OF_PIM; i++) { port = i*SFP_PORT_PER_PIM; - ret = get_port_present_bmap(port, &bmap_pim[i]); + ret = get_pim_port_present_bmap(port, &bmap_pim[i]); if (ret < 0) return ret; } @@ -266,10 +282,9 @@ sfpi_eeprom_close_all_channels(void) int channels = 8; for (i = 0; i < channels; i++) { - /* Skip checking if PIM is plugged. Cuz BMC traffic may not be ready at init. if (onlp_pim_is_present(i) != 1) - continue; - */ + continue; + value = 1<