LCOV - code coverage report
Current view: top level - drivers/net/ntnic/nim - i2c_nim.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 337 0.0 %
Date: 2025-08-01 17:49:26 Functions: 0 29 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 157 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * SPDX-License-Identifier: BSD-3-Clause
       3                 :            :  * Copyright(c) 2023 Napatech A/S
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <string.h>
       7                 :            : 
       8                 :            : #include "nthw_drv.h"
       9                 :            : #include "i2c_nim.h"
      10                 :            : #include "ntlog.h"
      11                 :            : #include "nt_util.h"
      12                 :            : #include "ntnic_mod_reg.h"
      13                 :            : #include "qsfp_registers.h"
      14                 :            : #include "nim_defines.h"
      15                 :            : 
      16                 :            : int nim_agx_read_id(struct nim_i2c_ctx *ctx);
      17                 :            : static void nim_agx_read(struct nim_i2c_ctx *ctx, uint8_t dev_addr, uint8_t reg_addr,
      18                 :            :         uint8_t data_len, void *p_data);
      19                 :            : static void nim_agx_write(struct nim_i2c_ctx *ctx, uint8_t dev_addr, uint8_t reg_addr,
      20                 :            :         uint8_t data_len, void *p_data);
      21                 :            : 
      22                 :            : #define NIM_READ false
      23                 :            : #define NIM_WRITE true
      24                 :            : #define NIM_PAGE_SEL_REGISTER 127
      25                 :            : #define NIM_I2C_0XA0 0xA0       /* Basic I2C address */
      26                 :            : 
      27                 :            : 
      28                 :          0 : static bool page_addressing(nt_nim_identifier_t id)
      29                 :            : {
      30         [ #  # ]:          0 :         switch (id) {
      31                 :            :         case NT_NIM_QSFP:
      32                 :            :         case NT_NIM_QSFP_PLUS:
      33                 :            :         case NT_NIM_QSFP28:
      34                 :            :                 return true;
      35                 :            : 
      36                 :          0 :         default:
      37                 :          0 :                 NT_LOG(DBG, NTNIC, "Unknown NIM identifier %d", id);
      38                 :          0 :                 return false;
      39                 :            :         }
      40                 :            : }
      41                 :            : 
      42                 :            : static nt_nim_identifier_t translate_nimid(const nim_i2c_ctx_t *ctx)
      43                 :            : {
      44                 :          0 :         return (nt_nim_identifier_t)ctx->nim_id;
      45                 :            : }
      46                 :            : 
      47                 :          0 : static int nim_read_write_i2c_data(nim_i2c_ctx_p ctx, bool do_write, uint16_t lin_addr,
      48                 :            :         uint8_t i2c_addr, uint8_t a_reg_addr, uint8_t seq_cnt,
      49                 :            :         uint8_t *p_data)
      50                 :            : {
      51                 :            :         /* Divide i2c_addr by 2 because nthw_iic_read/writeData multiplies by 2 */
      52                 :          0 :         const uint8_t i2c_devaddr = i2c_addr / 2U;
      53                 :            :         (void)lin_addr; /* Unused */
      54                 :            : 
      55         [ #  # ]:          0 :         if (do_write) {
      56         [ #  # ]:          0 :                 if (ctx->type == I2C_HWIIC) {
      57                 :          0 :                         return nthw_iic_write_data(&ctx->hwiic, i2c_devaddr, a_reg_addr, seq_cnt,
      58                 :            :                                         p_data);
      59                 :            :                 }
      60                 :            : 
      61                 :          0 :                 nim_agx_write(ctx, i2c_addr, a_reg_addr, seq_cnt, p_data);
      62                 :          0 :                 return 0;
      63                 :            :         }
      64                 :            : 
      65         [ #  # ]:          0 :         if (ctx->type == I2C_HWIIC)
      66                 :          0 :                 return nthw_iic_read_data(&ctx->hwiic, i2c_devaddr, a_reg_addr, seq_cnt, p_data);
      67                 :            : 
      68                 :          0 :         nim_agx_read(ctx, i2c_addr, a_reg_addr, seq_cnt, p_data);
      69                 :          0 :         return 0;
      70                 :            : }
      71                 :            : 
      72                 :            : /*
      73                 :            :  * ------------------------------------------------------------------------------
      74                 :            :  * Selects a new page for page addressing. This is only relevant if the NIM
      75                 :            :  * supports this. Since page switching can take substantial time the current page
      76                 :            :  * select is read and subsequently only changed if necessary.
      77                 :            :  * Important:
      78                 :            :  * XFP Standard 8077, Ver 4.5, Page 61 states that:
      79                 :            :  * If the host attempts to write a table select value which is not supported in
      80                 :            :  * a particular module, the table select byte will revert to 01h.
      81                 :            :  * This can lead to some surprising result that some pages seems to be duplicated.
      82                 :            :  * ------------------------------------------------------------------------------
      83                 :            :  */
      84                 :            : 
      85                 :          0 : static int nim_setup_page(nim_i2c_ctx_p ctx, uint8_t page_sel)
      86                 :            : {
      87                 :            :         uint8_t curr_page_sel;
      88                 :            : 
      89                 :            :         /* Read the current page select value */
      90         [ #  # ]:          0 :         if (nim_read_write_i2c_data(ctx, NIM_READ, NIM_PAGE_SEL_REGISTER, NIM_I2C_0XA0,
      91                 :            :                         NIM_PAGE_SEL_REGISTER, sizeof(curr_page_sel),
      92                 :            :                         &curr_page_sel) != 0) {
      93                 :            :                 return -1;
      94                 :            :         }
      95                 :            : 
      96                 :            :         /* Only write new page select value if necessary */
      97         [ #  # ]:          0 :         if (page_sel != curr_page_sel) {
      98         [ #  # ]:          0 :                 if (nim_read_write_i2c_data(ctx, NIM_WRITE, NIM_PAGE_SEL_REGISTER, NIM_I2C_0XA0,
      99                 :            :                                 NIM_PAGE_SEL_REGISTER, sizeof(page_sel),
     100                 :            :                                 &page_sel) != 0) {
     101                 :          0 :                         return -1;
     102                 :            :                 }
     103                 :            :         }
     104                 :            : 
     105                 :            :         return 0;
     106                 :            : }
     107                 :            : 
     108                 :          0 : static int nim_read_write_data_lin(nim_i2c_ctx_p ctx, bool m_page_addressing, uint16_t lin_addr,
     109                 :            :         uint16_t length, uint8_t *p_data, bool do_write)
     110                 :            : {
     111                 :            :         uint16_t i;
     112                 :            :         uint8_t a_reg_addr;     /* The actual register address in I2C device */
     113                 :            :         uint8_t i2c_addr;
     114                 :            :         int block_size = 128;   /* Equal to size of MSA pages */
     115                 :            :         int seq_cnt;
     116                 :            :         int max_seq_cnt = 1;
     117                 :            :         int multi_byte = 1;     /* One byte per I2C register is default */
     118                 :            : 
     119         [ #  # ]:          0 :         for (i = 0; i < length;) {
     120                 :            :                 bool use_page_select = false;
     121                 :            : 
     122                 :            :                 /*
     123                 :            :                  * Find out how much can be read from the current block in case of
     124                 :            :                  * single byte access
     125                 :            :                  */
     126                 :          0 :                 max_seq_cnt = block_size - (lin_addr % block_size);
     127                 :            : 
     128         [ #  # ]:          0 :                 if (m_page_addressing) {
     129         [ #  # ]:          0 :                         if (lin_addr >= 128) {       /* Only page setup above this address */
     130                 :            :                                 use_page_select = true;
     131                 :            : 
     132                 :            :                                 /* Map to [128..255] of 0xA0 device */
     133                 :          0 :                                 a_reg_addr = (uint8_t)(block_size + (lin_addr % block_size));
     134                 :            : 
     135                 :            :                         } else {
     136                 :          0 :                                 a_reg_addr = (uint8_t)lin_addr;
     137                 :            :                         }
     138                 :            : 
     139                 :            :                         i2c_addr = NIM_I2C_0XA0;/* Base I2C address */
     140                 :            : 
     141         [ #  # ]:          0 :                 } else if (lin_addr >= 256) {
     142                 :            :                         /* Map to address [0..255] of 0xA2 device */
     143                 :          0 :                         a_reg_addr = (uint8_t)(lin_addr - 256);
     144                 :            :                         i2c_addr = NIM_I2C_0XA2;
     145                 :            : 
     146                 :            :                 } else {
     147                 :          0 :                         a_reg_addr = (uint8_t)lin_addr;
     148                 :            :                         i2c_addr = NIM_I2C_0XA0;/* Base I2C address */
     149                 :            :                 }
     150                 :            : 
     151                 :            :                 /* Now actually do the reading/writing */
     152                 :          0 :                 seq_cnt = length - i;   /* Number of remaining bytes */
     153                 :            : 
     154                 :            :                 if (seq_cnt > max_seq_cnt)
     155                 :            :                         seq_cnt = max_seq_cnt;
     156                 :            : 
     157                 :            :                 /*
     158                 :            :                  * Read a number of bytes without explicitly specifying a new address.
     159                 :            :                  * This can speed up I2C access since automatic incrementation of the
     160                 :            :                  * I2C device internal address counter can be used. It also allows
     161                 :            :                  * a HW implementation, that can deal with block access.
     162                 :            :                  * Furthermore it also allows for access to data that must be accessed
     163                 :            :                  * as 16bit words reading two bytes at each address eg PHYs.
     164                 :            :                  */
     165         [ #  # ]:          0 :                 if (use_page_select) {
     166         [ #  # ]:          0 :                         if (nim_setup_page(ctx, (uint8_t)((lin_addr / 128) - 1)) != 0) {
     167                 :          0 :                                 NT_LOG(ERR, NTNIC,
     168                 :            :                                         "Cannot set up page for linear address %u", lin_addr);
     169                 :          0 :                                 return -1;
     170                 :            :                         }
     171                 :            :                 }
     172                 :            : 
     173         [ #  # ]:          0 :                 if (nim_read_write_i2c_data(ctx, do_write, lin_addr, i2c_addr, a_reg_addr,
     174                 :            :                                 (uint8_t)seq_cnt, p_data) != 0) {
     175                 :          0 :                         NT_LOG(ERR, NTNIC, " Call to nim_read_write_i2c_data failed");
     176                 :          0 :                         return -1;
     177                 :            :                 }
     178                 :            : 
     179                 :          0 :                 p_data += seq_cnt;
     180                 :          0 :                 i = (uint16_t)(i + seq_cnt);
     181                 :          0 :                 lin_addr = (uint16_t)(lin_addr + (seq_cnt / multi_byte));
     182                 :            :         }
     183                 :            : 
     184                 :            :         return 0;
     185                 :            : }
     186                 :            : 
     187                 :          0 : static int read_data_lin(nim_i2c_ctx_p ctx, uint16_t lin_addr, uint16_t length, void *data)
     188                 :            : {
     189                 :            :         /* Wrapper for using Mutex for QSFP TODO */
     190                 :          0 :         return nim_read_write_data_lin(ctx, page_addressing(ctx->nim_id), lin_addr, length, data,
     191                 :            :                         NIM_READ);
     192                 :            : }
     193                 :            : 
     194                 :          0 : static int write_data_lin(nim_i2c_ctx_p ctx, uint16_t lin_addr, uint16_t length, void *data)
     195                 :            : {
     196                 :            :         /* Wrapper for using Mutex for QSFP TODO */
     197                 :          0 :         return nim_read_write_data_lin(ctx, page_addressing(ctx->nim_id), lin_addr, length, data,
     198                 :            :                 NIM_WRITE);
     199                 :            : }
     200                 :            : 
     201                 :            : /* Read and return a single byte */
     202                 :            : static uint8_t read_byte(nim_i2c_ctx_p ctx, uint16_t addr)
     203                 :            : {
     204                 :            :         uint8_t data;
     205                 :          0 :         read_data_lin(ctx, addr, sizeof(data), &data);
     206                 :          0 :         return data;
     207                 :            : }
     208                 :            : 
     209                 :            : static int nim_read_id(nim_i2c_ctx_t *ctx)
     210                 :            : {
     211                 :            :         /* We are only reading the first byte so we don't care about pages here. */
     212                 :            :         const bool USE_PAGE_ADDRESSING = false;
     213                 :            : 
     214         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, USE_PAGE_ADDRESSING, NIM_IDENTIFIER_ADDR,
     215                 :            :                         sizeof(ctx->nim_id), &ctx->nim_id, NIM_READ) != 0) {
     216                 :            :                 return -1;
     217                 :            :         }
     218                 :            : 
     219                 :            :         return 0;
     220                 :            : }
     221                 :            : 
     222                 :          0 : static int i2c_nim_common_construct(nim_i2c_ctx_p ctx)
     223                 :            : {
     224                 :          0 :         ctx->nim_id = 0;
     225                 :            :         int res;
     226                 :            : 
     227         [ #  # ]:          0 :         if (ctx->type == I2C_HWIIC)
     228                 :            :                 res = nim_read_id(ctx);
     229                 :            : 
     230                 :            :         else
     231                 :          0 :                 res = nim_agx_read_id(ctx);
     232                 :            : 
     233         [ #  # ]:          0 :         if (res) {
     234                 :          0 :                 NT_LOG(ERR, NTNIC, "Can't read NIM id.");
     235                 :          0 :                 return res;
     236                 :            :         }
     237                 :            : 
     238                 :          0 :         memset(ctx->vendor_name, 0, sizeof(ctx->vendor_name));
     239                 :          0 :         memset(ctx->prod_no, 0, sizeof(ctx->prod_no));
     240                 :          0 :         memset(ctx->serial_no, 0, sizeof(ctx->serial_no));
     241                 :          0 :         memset(ctx->date, 0, sizeof(ctx->date));
     242                 :          0 :         memset(ctx->rev, 0, sizeof(ctx->rev));
     243                 :            : 
     244                 :          0 :         ctx->content_valid = false;
     245                 :          0 :         memset(ctx->len_info, 0, sizeof(ctx->len_info));
     246                 :          0 :         ctx->pwr_level_req = 0;
     247                 :          0 :         ctx->pwr_level_cur = 0;
     248                 :          0 :         ctx->avg_pwr = false;
     249                 :          0 :         ctx->tx_disable = false;
     250                 :          0 :         ctx->lane_idx = -1;
     251                 :          0 :         ctx->lane_count = 1;
     252                 :          0 :         ctx->options = 0;
     253                 :          0 :         return 0;
     254                 :            : }
     255                 :            : 
     256                 :            : /*
     257                 :            :  * Read vendor information at a certain address. Any trailing whitespace is
     258                 :            :  * removed and a missing string termination in the NIM data is handled.
     259                 :            :  */
     260                 :          0 : static int nim_read_vendor_info(nim_i2c_ctx_p ctx, uint16_t addr, uint8_t max_len, char *p_data)
     261                 :            : {
     262                 :          0 :         const bool pg_addr = page_addressing(ctx->nim_id);
     263                 :            :         int i;
     264                 :            :         /* Subtract "1" from max_len that includes a terminating "0" */
     265                 :            : 
     266         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, addr, (uint8_t)(max_len - 1), (uint8_t *)p_data,
     267                 :            :                         NIM_READ) != 0) {
     268                 :            :                 return -1;
     269                 :            :         }
     270                 :            : 
     271                 :            :         /* Terminate at first found white space */
     272         [ #  # ]:          0 :         for (i = 0; i < max_len - 1; i++) {
     273         [ #  # ]:          0 :                 if (*p_data == ' ' || *p_data == '\n' || *p_data == '\t' || *p_data == '\v' ||
     274                 :            :                         *p_data == '\f' || *p_data == '\r') {
     275                 :          0 :                         *p_data = '\0';
     276                 :          0 :                         return 0;
     277                 :            :                 }
     278                 :            : 
     279                 :          0 :                 p_data++;
     280                 :            :         }
     281                 :            : 
     282                 :            :         /*
     283                 :            :          * Add line termination as the very last character, if it was missing in the
     284                 :            :          * NIM data
     285                 :            :          */
     286                 :          0 :         *p_data = '\0';
     287                 :          0 :         return 0;
     288                 :            : }
     289                 :            : 
     290                 :          0 : static void qsfp_read_vendor_info(nim_i2c_ctx_t *ctx)
     291                 :            : {
     292                 :          0 :         nim_read_vendor_info(ctx, QSFP_VENDOR_NAME_LIN_ADDR, sizeof(ctx->vendor_name),
     293                 :          0 :                 ctx->vendor_name);
     294                 :          0 :         nim_read_vendor_info(ctx, QSFP_VENDOR_PN_LIN_ADDR, sizeof(ctx->prod_no), ctx->prod_no);
     295                 :          0 :         nim_read_vendor_info(ctx, QSFP_VENDOR_SN_LIN_ADDR, sizeof(ctx->serial_no), ctx->serial_no);
     296                 :          0 :         nim_read_vendor_info(ctx, QSFP_VENDOR_DATE_LIN_ADDR, sizeof(ctx->date), ctx->date);
     297                 :          0 :         nim_read_vendor_info(ctx, QSFP_VENDOR_REV_LIN_ADDR, (uint8_t)(sizeof(ctx->rev) - 2),
     298                 :          0 :                 ctx->rev);   /*OBS Only two bytes*/
     299                 :          0 : }
     300   [ #  #  #  # ]:          0 : static int qsfp_nim_state_build(nim_i2c_ctx_t *ctx, sfp_nim_state_t *state)
     301                 :            : {
     302                 :            :         int res = 0;    /* unused due to no readings from HW */
     303                 :            : 
     304                 :            :         RTE_ASSERT(ctx && state);
     305                 :            :         RTE_ASSERT(ctx->nim_id != NT_NIM_UNKNOWN && "Nim is not initialized");
     306                 :            : 
     307                 :            :         memset(state, 0, sizeof(*state));
     308                 :            : 
     309   [ #  #  #  # ]:          0 :         switch (ctx->nim_id) {
     310                 :          0 :         case 12U:
     311                 :          0 :                 state->br = 10U;/* QSFP: 4 x 1G = 4G */
     312                 :          0 :                 break;
     313                 :            : 
     314                 :          0 :         case 13U:
     315                 :          0 :                 state->br = 103U;    /* QSFP+: 4 x 10G = 40G */
     316                 :          0 :                 break;
     317                 :            : 
     318                 :          0 :         case 17U:
     319                 :          0 :                 state->br = 255U;    /* QSFP28: 4 x 25G = 100G */
     320                 :          0 :                 break;
     321                 :            : 
     322                 :          0 :         default:
     323                 :          0 :                 NT_LOG(INF, NTNIC, "nim_id = %u is not an QSFP/QSFP+/QSFP28 module", ctx->nim_id);
     324                 :            :                 res = -1;
     325                 :            :         }
     326                 :            : 
     327                 :          0 :         return res;
     328                 :            : }
     329                 :            : 
     330                 :          0 : int nthw_nim_state_build(nim_i2c_ctx_t *ctx, sfp_nim_state_t *state)
     331                 :            : {
     332                 :          0 :         return qsfp_nim_state_build(ctx, state);
     333                 :            : }
     334                 :            : 
     335                 :          0 : const char *nthw_nim_id_to_text(uint8_t nim_id)
     336                 :            : {
     337   [ #  #  #  #  :          0 :         switch (nim_id) {
                      # ]
     338                 :            :         case 0x0:
     339                 :            :                 return "UNKNOWN";
     340                 :            : 
     341                 :          0 :         case 0x0C:
     342                 :          0 :                 return "QSFP";
     343                 :            : 
     344                 :          0 :         case 0x0D:
     345                 :          0 :                 return "QSFP+";
     346                 :            : 
     347                 :          0 :         case 0x11:
     348                 :          0 :                 return "QSFP28";
     349                 :            : 
     350                 :          0 :         default:
     351                 :          0 :                 return "ILLEGAL!";
     352                 :            :         }
     353                 :            : }
     354                 :            : 
     355                 :            : /*
     356                 :            :  * Disable laser for specific lane or all lanes
     357                 :            :  */
     358                 :          0 : int nthw_nim_qsfp_plus_nim_set_tx_laser_disable(nim_i2c_ctx_p ctx, bool disable, int lane_idx)
     359                 :            : {
     360                 :            :         uint8_t value;
     361                 :            :         uint8_t mask;
     362                 :          0 :         const bool pg_addr = page_addressing(ctx->nim_id);
     363                 :            : 
     364         [ #  # ]:          0 :         if (lane_idx < 0)    /* If no lane is specified then all lanes */
     365                 :            :                 mask = QSFP_SOFT_TX_ALL_DISABLE_BITS;
     366                 :            : 
     367                 :            :         else
     368                 :          0 :                 mask = (uint8_t)(1U << lane_idx);
     369                 :            : 
     370         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, QSFP_CONTROL_STATUS_LIN_ADDR, sizeof(value),
     371                 :            :                         &value, NIM_READ) != 0) {
     372                 :            :                 return -1;
     373                 :            :         }
     374                 :            : 
     375         [ #  # ]:          0 :         if (disable)
     376                 :          0 :                 value |= mask;
     377                 :            : 
     378                 :            :         else
     379                 :          0 :                 value &= (uint8_t)(~mask);
     380                 :            : 
     381         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, QSFP_CONTROL_STATUS_LIN_ADDR, sizeof(value),
     382                 :            :                         &value, NIM_WRITE) != 0) {
     383                 :          0 :                 return -1;
     384                 :            :         }
     385                 :            : 
     386                 :            :         return 0;
     387                 :            : }
     388                 :            : 
     389                 :            : /*
     390                 :            :  * Import length info in various units from NIM module data and convert to meters
     391                 :            :  */
     392                 :            : static void nim_import_len_info(nim_i2c_ctx_p ctx, uint8_t *p_nim_len_info, uint16_t *p_nim_units)
     393                 :            : {
     394                 :            :         size_t i;
     395                 :            : 
     396         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(ctx->len_info); i++)
     397         [ #  # ]:          0 :                 if (*(p_nim_len_info + i) == 255) {
     398                 :          0 :                         ctx->len_info[i] = 65535;
     399                 :            : 
     400                 :            :                 } else {
     401                 :          0 :                         uint32_t len = *(p_nim_len_info + i) * *(p_nim_units + i);
     402                 :            : 
     403         [ #  # ]:          0 :                         if (len > 65535)
     404                 :          0 :                                 ctx->len_info[i] = 65535;
     405                 :            : 
     406                 :            :                         else
     407                 :          0 :                                 ctx->len_info[i] = (uint16_t)len;
     408                 :            :                 }
     409                 :            : }
     410                 :            : 
     411                 :          0 : static int qsfpplus_read_basic_data(nim_i2c_ctx_t *ctx)
     412                 :            : {
     413                 :          0 :         const bool pg_addr = page_addressing(ctx->nim_id);
     414                 :            :         uint8_t options;
     415                 :            :         uint8_t value;
     416                 :            :         uint8_t nim_len_info[5];
     417                 :          0 :         uint16_t nim_units[5] = { 1000, 2, 1, 1, 1 };   /* QSFP MSA units in meters */
     418                 :          0 :         const char *yes_no[2] = { "No", "Yes" };
     419                 :            :         (void)yes_no;
     420                 :          0 :         NT_LOG(DBG, NTNIC, "Instance %d: NIM id: %s (%d)", ctx->instance,
     421                 :            :                 nthw_nim_id_to_text(ctx->nim_id), ctx->nim_id);
     422                 :            : 
     423                 :            :         /* Read DMI options */
     424         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, QSFP_DMI_OPTION_LIN_ADDR, sizeof(options),
     425                 :            :                         &options, NIM_READ) != 0) {
     426                 :            :                 return -1;
     427                 :            :         }
     428                 :            : 
     429                 :          0 :         ctx->avg_pwr = options & QSFP_DMI_AVG_PWR_BIT;
     430                 :          0 :         NT_LOG(DBG, NTNIC, "Instance %d: NIM options: (DMI: Yes, AvgPwr: %s)", ctx->instance,
     431                 :            :                 yes_no[ctx->avg_pwr]);
     432                 :            : 
     433                 :          0 :         qsfp_read_vendor_info(ctx);
     434                 :          0 :         NT_LOG(DBG, NTNIC,
     435                 :            :                 "Instance %d: NIM info: (Vendor: %s, PN: %s, SN: %s, Date: %s, Rev: %s)",
     436                 :            :                 ctx->instance, ctx->vendor_name, ctx->prod_no, ctx->serial_no, ctx->date, ctx->rev);
     437                 :            : 
     438         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, QSFP_SUP_LEN_INFO_LIN_ADDR, sizeof(nim_len_info),
     439                 :            :                         nim_len_info, NIM_READ) != 0) {
     440                 :            :                 return -1;
     441                 :            :         }
     442                 :            : 
     443                 :            :         /*
     444                 :            :          * Returns supported length information in meters for various fibers as 5 indivi-
     445                 :            :          * dual values: [SM(9um), EBW(50um), MM(50um), MM(62.5um), Copper]
     446                 :            :          * If no length information is available for a certain entry, the returned value
     447                 :            :          * will be zero. This will be the case for SFP modules - EBW entry.
     448                 :            :          * If the MSBit is set the returned value in the lower 31 bits indicates that the
     449                 :            :          * supported length is greater than this.
     450                 :            :          */
     451                 :            : 
     452                 :            :         nim_import_len_info(ctx, nim_len_info, nim_units);
     453                 :            : 
     454                 :            :         /* Read required power level */
     455         [ #  # ]:          0 :         if (nim_read_write_data_lin(ctx, pg_addr, QSFP_EXTENDED_IDENTIFIER, sizeof(value), &value,
     456                 :            :                         NIM_READ) != 0) {
     457                 :            :                 return -1;
     458                 :            :         }
     459                 :            : 
     460                 :            :         /*
     461                 :            :          * Get power class according to SFF-8636 Rev 2.7, Table 6-16, Page 43:
     462                 :            :          * If power class >= 5 setHighPower must be called for the module to be fully
     463                 :            :          * functional
     464                 :            :          */
     465         [ #  # ]:          0 :         if ((value & QSFP_POWER_CLASS_BITS_5_7) == 0) {
     466                 :            :                 /* NIM in power class 1 - 4 */
     467                 :          0 :                 ctx->pwr_level_req = (uint8_t)(((value & QSFP_POWER_CLASS_BITS_1_4) >> 6) + 1);
     468                 :            : 
     469                 :            :         } else {
     470                 :            :                 /* NIM in power class 5 - 7 */
     471                 :          0 :                 ctx->pwr_level_req = (uint8_t)((value & QSFP_POWER_CLASS_BITS_5_7) + 4);
     472                 :            :         }
     473                 :            : 
     474                 :            :         return 0;
     475                 :            : }
     476                 :            : 
     477                 :          0 : static void qsfp28_find_port_params(nim_i2c_ctx_p ctx)
     478                 :            : {
     479                 :            :         uint8_t fiber_chan_speed;
     480                 :            : 
     481                 :            :         /* Table 6-17 SFF-8636 */
     482                 :          0 :         read_data_lin(ctx, QSFP_SPEC_COMPLIANCE_CODES_ADDR, 1, &fiber_chan_speed);
     483                 :            : 
     484         [ #  # ]:          0 :         if (fiber_chan_speed & (1 << 7)) {
     485                 :            :                 /* SFF-8024, Rev 4.7, Table 4-4 */
     486                 :          0 :                 uint8_t extended_specification_compliance_code = 0;
     487                 :          0 :                 read_data_lin(ctx, QSFP_EXT_SPEC_COMPLIANCE_CODES_ADDR, 1,
     488                 :            :                         &extended_specification_compliance_code);
     489                 :            : 
     490   [ #  #  #  #  :          0 :                 switch (extended_specification_compliance_code) {
             #  #  #  #  
                      # ]
     491                 :          0 :                 case 0x02:
     492                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_SR4;
     493                 :          0 :                         break;
     494                 :            : 
     495                 :          0 :                 case 0x03:
     496                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_LR4;
     497                 :          0 :                         break;
     498                 :            : 
     499                 :          0 :                 case 0x0B:
     500                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_CR_CA_L;
     501                 :          0 :                         break;
     502                 :            : 
     503                 :          0 :                 case 0x0C:
     504                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_CR_CA_S;
     505                 :          0 :                         break;
     506                 :            : 
     507                 :          0 :                 case 0x0D:
     508                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_CR_CA_N;
     509                 :          0 :                         break;
     510                 :            : 
     511                 :          0 :                 case 0x25:
     512                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_DR;
     513                 :          0 :                         break;
     514                 :            : 
     515                 :          0 :                 case 0x26:
     516                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_FR;
     517                 :          0 :                         break;
     518                 :            : 
     519                 :          0 :                 case 0x27:
     520                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28_LR;
     521                 :          0 :                         break;
     522                 :            : 
     523                 :          0 :                 default:
     524                 :          0 :                         ctx->port_type = NT_PORT_TYPE_QSFP28;
     525                 :            :                 }
     526                 :            : 
     527                 :            :         } else {
     528                 :          0 :                 ctx->port_type = NT_PORT_TYPE_QSFP28;
     529                 :            :         }
     530                 :          0 : }
     531                 :            : 
     532                 :            : /*
     533                 :            :  * If true the user must actively select the desired rate. If false the module
     534                 :            :  * however can still support several rates without the user is required to select
     535                 :            :  * one of them. Supported rates must then be deduced from the product number.
     536                 :            :  * SFF-8636, Rev 2.10a:
     537                 :            :  * p40: 6.2.7 Rate Select
     538                 :            :  * p85: A.2 Rate Select
     539                 :            :  */
     540                 :          0 : static bool qsfp28_is_rate_selection_enabled(nim_i2c_ctx_p ctx)
     541                 :            : {
     542                 :            :         const uint8_t ext_rate_select_compl_reg_addr = 141;
     543                 :            :         const uint8_t options_reg_addr = 195;
     544                 :            :         const uint8_t enh_options_reg_addr = 221;
     545                 :            : 
     546                 :          0 :         uint8_t rate_select_ena = (read_byte(ctx, options_reg_addr) >> 5) & 0x01;     /* bit: 5 */
     547                 :            : 
     548         [ #  # ]:          0 :         if (rate_select_ena == 0)
     549                 :            :                 return false;
     550                 :            : 
     551                 :          0 :         uint8_t rate_select_type =
     552                 :          0 :                 (read_byte(ctx, enh_options_reg_addr) >> 2) & 0x03;   /* bit 3..2 */
     553                 :            : 
     554         [ #  # ]:          0 :         if (rate_select_type != 2) {
     555                 :          0 :                 NT_LOG(DBG, NTNIC, "NIM has unhandled rate select type (%d)", rate_select_type);
     556                 :          0 :                 return false;
     557                 :            :         }
     558                 :            : 
     559                 :          0 :         uint8_t ext_rate_select_ver =
     560                 :            :                 read_byte(ctx, ext_rate_select_compl_reg_addr) & 0x03;      /* bit 1..0 */
     561                 :            : 
     562         [ #  # ]:          0 :         if (ext_rate_select_ver != 0x02) {
     563                 :          0 :                 NT_LOG(DBG, NTNIC, "NIM has unhandled extended rate select version (%d)",
     564                 :            :                         ext_rate_select_ver);
     565                 :          0 :                 return false;
     566                 :            :         }
     567                 :            : 
     568                 :            :         return true;    /* When true selectRate() can be used */
     569                 :            : }
     570                 :            : 
     571                 :          0 : static void qsfp28_set_speed_mask(nim_i2c_ctx_p ctx)
     572                 :            : {
     573         [ #  # ]:          0 :         if (ctx->port_type == NT_PORT_TYPE_QSFP28_FR || ctx->port_type == NT_PORT_TYPE_QSFP28_DR ||
     574                 :            :                 ctx->port_type == NT_PORT_TYPE_QSFP28_LR) {
     575         [ #  # ]:          0 :                 if (ctx->lane_idx < 0)
     576                 :          0 :                         ctx->speed_mask = NT_LINK_SPEED_100G;
     577                 :            : 
     578                 :            :                 else
     579                 :            :                         /* PAM-4 modules can only run on all lanes together */
     580                 :          0 :                         ctx->speed_mask = 0;
     581                 :            : 
     582                 :            :         } else {
     583         [ #  # ]:          0 :                 if (ctx->lane_idx < 0)
     584                 :          0 :                         ctx->speed_mask = NT_LINK_SPEED_100G;
     585                 :            : 
     586                 :            :                 else
     587                 :          0 :                         ctx->speed_mask = NT_LINK_SPEED_25G;
     588                 :            : 
     589         [ #  # ]:          0 :                 if (qsfp28_is_rate_selection_enabled(ctx)) {
     590                 :            :                         /*
     591                 :            :                          * It is assumed that if the module supports dual rates then the other rate
     592                 :            :                          * is 10G per lane or 40G for all lanes.
     593                 :            :                          */
     594         [ #  # ]:          0 :                         if (ctx->lane_idx < 0)
     595                 :          0 :                                 ctx->speed_mask |= NT_LINK_SPEED_40G;
     596                 :            : 
     597                 :            :                         else
     598                 :          0 :                                 ctx->speed_mask = NT_LINK_SPEED_10G;
     599                 :            :                 }
     600                 :            :         }
     601                 :          0 : }
     602                 :            : 
     603                 :          0 : static void qsfpplus_find_port_params(nim_i2c_ctx_p ctx)
     604                 :            : {
     605                 :            :         uint8_t device_tech;
     606                 :          0 :         read_data_lin(ctx, QSFP_TRANSMITTER_TYPE_LIN_ADDR, sizeof(device_tech), &device_tech);
     607                 :            : 
     608         [ #  # ]:          0 :         switch (device_tech & 0xF0) {
     609                 :            :         case 0xA0:      /* Copper cable unequalized */
     610                 :            :                 break;
     611                 :            : 
     612                 :            :         case 0xC0:      /* Copper cable, near and far end limiting active equalizers */
     613                 :            :         case 0xD0:      /* Copper cable, far end limiting active equalizers */
     614                 :            :         case 0xE0:      /* Copper cable, near end limiting active equalizers */
     615                 :            :                 break;
     616                 :            : 
     617                 :          0 :         default:/* Optical */
     618                 :          0 :                 ctx->port_type = NT_PORT_TYPE_QSFP_PLUS;
     619                 :          0 :                 break;
     620                 :            :         }
     621                 :          0 : }
     622                 :            : 
     623                 :            : static void qsfpplus_set_speed_mask(nim_i2c_ctx_p ctx)
     624                 :            : {
     625                 :          0 :         ctx->speed_mask = (ctx->lane_idx < 0) ? NT_LINK_SPEED_40G : (NT_LINK_SPEED_10G);
     626                 :            : }
     627                 :            : 
     628                 :            : static void qsfpplus_construct(nim_i2c_ctx_p ctx, int8_t lane_idx)
     629                 :            : {
     630                 :            :         RTE_ASSERT(lane_idx < 4);
     631                 :          0 :         ctx->specific_u.qsfp.qsfp28 = false;
     632                 :          0 :         ctx->lane_idx = lane_idx;
     633                 :          0 :         ctx->lane_count = 4;
     634                 :            : }
     635                 :            : 
     636                 :          0 : static int qsfpplus_preinit(nim_i2c_ctx_p ctx, int8_t lane_idx)
     637                 :            : {
     638                 :            :         qsfpplus_construct(ctx, lane_idx);
     639                 :          0 :         int res = qsfpplus_read_basic_data(ctx);
     640                 :            : 
     641         [ #  # ]:          0 :         if (!res) {
     642                 :          0 :                 qsfpplus_find_port_params(ctx);
     643                 :            : 
     644                 :            :                 /*
     645                 :            :                  * Read if TX_DISABLE has been implemented
     646                 :            :                  * For passive optical modules this is required while it for copper and active
     647                 :            :                  * optical modules is optional. Under all circumstances register 195.4 will
     648                 :            :                  * indicate, if TX_DISABLE has been implemented in register 86.0-3
     649                 :            :                  */
     650                 :            :                 uint8_t value;
     651                 :          0 :                 read_data_lin(ctx, QSFP_OPTION3_LIN_ADDR, sizeof(value), &value);
     652                 :            : 
     653                 :          0 :                 ctx->tx_disable = (value & QSFP_OPTION3_TX_DISABLE_BIT) != 0;
     654                 :            : 
     655         [ #  # ]:          0 :                 if (ctx->tx_disable)
     656                 :          0 :                         ctx->options |= (1 << NIM_OPTION_TX_DISABLE);
     657                 :            : 
     658                 :            :                 /*
     659                 :            :                  * Previously - considering AFBR-89BRDZ - code tried to establish if a module was
     660                 :            :                  * RxOnly by testing the state of the lasers after reset. Lasers were for this
     661                 :            :                  * module default disabled.
     662                 :            :                  * However that code did not work for GigaLight, GQS-MPO400-SR4C so it was
     663                 :            :                  * decided that this option should not be detected automatically but from PN
     664                 :            :                  */
     665         [ #  # ]:          0 :                 ctx->specific_u.qsfp.rx_only = (ctx->options & (1 << NIM_OPTION_RX_ONLY)) != 0;
     666                 :            :                 qsfpplus_set_speed_mask(ctx);
     667                 :            :         }
     668                 :            : 
     669                 :          0 :         return res;
     670                 :            : }
     671                 :            : 
     672                 :          0 : static void qsfp28_wait_for_ready_after_reset(nim_i2c_ctx_p ctx)
     673                 :            : {
     674                 :            :         uint8_t data;
     675                 :            :         bool init_complete_flag_present = false;
     676                 :            : 
     677                 :            :         /*
     678                 :            :          * Revision compliance
     679                 :            :          * 7: SFF-8636 Rev 2.5, 2.6 and 2.7
     680                 :            :          * 8: SFF-8636 Rev 2.8, 2.9 and 2.10
     681                 :            :          */
     682                 :          0 :         read_data_lin(ctx, 1, sizeof(ctx->specific_u.qsfp.specific_u.qsfp28.rev_compliance),
     683                 :          0 :                 &ctx->specific_u.qsfp.specific_u.qsfp28.rev_compliance);
     684                 :          0 :         NT_LOG(DBG, NTHW, "NIM RevCompliance = %d",
     685                 :            :                 ctx->specific_u.qsfp.specific_u.qsfp28.rev_compliance);
     686                 :            : 
     687                 :            :         /* Wait if lane_idx == -1 (all lanes are used) or lane_idx == 0 (the first lane) */
     688         [ #  # ]:          0 :         if (ctx->lane_idx > 0)
     689                 :          0 :                 return;
     690                 :            : 
     691         [ #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.rev_compliance >= 7) {
     692                 :            :                 /* Check if init complete flag is implemented */
     693                 :          0 :                 read_data_lin(ctx, 221, sizeof(data), &data);
     694                 :          0 :                 init_complete_flag_present = (data & (1 << 4)) != 0;
     695                 :            :         }
     696                 :            : 
     697                 :          0 :         NT_LOG(DBG, NTHW, "NIM InitCompleteFlagPresent = %d", init_complete_flag_present);
     698                 :            : 
     699                 :            :         /*
     700                 :            :          * If the init complete flag is not present then wait 500ms that together with 500ms
     701                 :            :          * after reset (in the adapter code) should be enough to read data from upper pages
     702                 :            :          * that otherwise would not be ready. Especially BiDi modules AFBR-89BDDZ have been
     703                 :            :          * prone to this when trying to read sensor options using getQsfpOptionsFromData()
     704                 :            :          * Probably because access to the paged address space is required.
     705                 :            :          */
     706         [ #  # ]:          0 :         if (!init_complete_flag_present) {
     707                 :          0 :                 nt_os_wait_usec(500000);
     708                 :          0 :                 return;
     709                 :            :         }
     710                 :            : 
     711                 :            :         /* Otherwise wait for the init complete flag to be set */
     712                 :            :         int count = 0;
     713                 :            : 
     714                 :            :         while (true) {
     715         [ #  # ]:          0 :                 if (count > 10) {    /* 1 s timeout */
     716                 :          0 :                         NT_LOG(WRN, NTHW, "Timeout waiting for module ready");
     717                 :          0 :                         break;
     718                 :            :                 }
     719                 :            : 
     720                 :          0 :                 read_data_lin(ctx, 6, sizeof(data), &data);
     721                 :            : 
     722         [ #  # ]:          0 :                 if (data & 0x01) {
     723                 :          0 :                         NT_LOG(DBG, NTHW, "Module ready after %dms", count * 100);
     724                 :          0 :                         break;
     725                 :            :                 }
     726                 :            : 
     727                 :          0 :                 nt_os_wait_usec(100000);/* 100 ms */
     728                 :          0 :                 count++;
     729                 :            :         }
     730                 :            : }
     731                 :            : 
     732                 :          0 : static void qsfp28_get_fec_options(nim_i2c_ctx_p ctx)
     733                 :            : {
     734                 :          0 :         const char *const nim_list[] = {
     735                 :            :                 "AFBR-89BDDZ",        /* Avago BiDi */
     736                 :            :                 "AFBR-89BRDZ",        /* Avago BiDi, RxOnly */
     737                 :            :                 "FTLC4352RKPL",       /* Finisar QSFP28-LR */
     738                 :            :                 "FTLC4352RHPL",       /* Finisar QSFP28-DR */
     739                 :            :                 "FTLC4352RJPL",       /* Finisar QSFP28-FR */
     740                 :            :                 "SFBR-89BDDZ-CS4",    /* Foxconn, QSFP28 100G/40G BiDi */
     741                 :            :         };
     742                 :            : 
     743         [ #  # ]:          0 :         for (size_t i = 0; i < ARRAY_SIZE(nim_list); i++) {
     744         [ #  # ]:          0 :                 if (ctx->prod_no == nim_list[i]) {
     745                 :          0 :                         ctx->options |= (1 << NIM_OPTION_MEDIA_SIDE_FEC);
     746                 :          0 :                         ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ena = true;
     747                 :          0 :                         NT_LOG(DBG, NTHW, "Found FEC info via PN list");
     748                 :          0 :                         return;
     749                 :            :                 }
     750                 :            :         }
     751                 :            : 
     752                 :            :         /*
     753                 :            :          * For modules not in the list find FEC info via registers
     754                 :            :          * Read if the module has controllable FEC
     755                 :            :          * SFF-8636, Rev 2.10a TABLE 6-28 Equalizer, Emphasis, Amplitude and Timing)
     756                 :            :          * (Page 03h, Bytes 224-229)
     757                 :            :          */
     758                 :            :         uint8_t data;
     759                 :            :         uint16_t addr = 227 + 3 * 128;
     760                 :          0 :         read_data_lin(ctx, addr, sizeof(data), &data);
     761                 :            : 
     762                 :            :         /* Check if the module has FEC support that can be controlled */
     763                 :          0 :         ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ctrl = (data & (1 << 6)) != 0;
     764                 :          0 :         ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ctrl = (data & (1 << 7)) != 0;
     765                 :            : 
     766         [ #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ctrl)
     767                 :          0 :                 ctx->options |= (1 << NIM_OPTION_MEDIA_SIDE_FEC);
     768                 :            : 
     769         [ #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ctrl)
     770                 :          0 :                 ctx->options |= (1 << NIM_OPTION_HOST_SIDE_FEC);
     771                 :            : }
     772                 :            : 
     773                 :          0 : static int qsfp28_preinit(nim_i2c_ctx_p ctx, int8_t lane_idx)
     774                 :            : {
     775                 :          0 :         int res = qsfpplus_preinit(ctx, lane_idx);
     776                 :            : 
     777         [ #  # ]:          0 :         if (!res) {
     778                 :          0 :                 qsfp28_wait_for_ready_after_reset(ctx);
     779                 :          0 :                 memset(&ctx->specific_u.qsfp.specific_u.qsfp28, 0,
     780                 :            :                         sizeof(ctx->specific_u.qsfp.specific_u.qsfp28));
     781                 :          0 :                 ctx->specific_u.qsfp.qsfp28 = true;
     782                 :          0 :                 qsfp28_find_port_params(ctx);
     783                 :          0 :                 qsfp28_get_fec_options(ctx);
     784                 :          0 :                 qsfp28_set_speed_mask(ctx);
     785                 :            :         }
     786                 :            : 
     787                 :          0 :         return res;
     788                 :            : }
     789                 :            : 
     790                 :          0 : int nthw_construct_and_preinit_nim(nim_i2c_ctx_p ctx, void *extra)
     791                 :            : {
     792                 :          0 :         int res = i2c_nim_common_construct(ctx);
     793                 :            : 
     794      [ #  #  # ]:          0 :         switch (translate_nimid(ctx)) {
     795                 :          0 :         case NT_NIM_QSFP_PLUS:
     796         [ #  # ]:          0 :                 qsfpplus_preinit(ctx, extra ? *(int8_t *)extra : (int8_t)-1);
     797                 :          0 :                 break;
     798                 :            : 
     799                 :          0 :         case NT_NIM_QSFP28:
     800         [ #  # ]:          0 :                 qsfp28_preinit(ctx, extra ? *(int8_t *)extra : (int8_t)-1);
     801                 :          0 :                 break;
     802                 :            : 
     803                 :          0 :         default:
     804                 :            :                 res = 1;
     805                 :          0 :                 NT_LOG(ERR, NTHW, "NIM type %s is not supported", nthw_nim_id_to_text(ctx->nim_id));
     806                 :            :         }
     807                 :            : 
     808                 :          0 :         return res;
     809                 :            : }
     810                 :            : 
     811                 :            : /*
     812                 :            :  * Enables high power according to SFF-8636 Rev 2.7, Table 6-9, Page 35:
     813                 :            :  * When set (= 1b) enables Power Classes 5 to 7 in Byte 129 to exceed 3.5W.
     814                 :            :  * When cleared (= 0b), modules with power classes 5 to 7 must dissipate less
     815                 :            :  * than 3.5W (but are not required to be fully functional). Default 0.
     816                 :            :  */
     817                 :          0 : void nthw_qsfp28_set_high_power(nim_i2c_ctx_p ctx)
     818                 :            : {
     819                 :            :         const uint16_t addr = 93;
     820                 :            :         uint8_t data;
     821                 :            : 
     822                 :            :         /* Enable high power class; Set Page 00h, Byte 93 bit 2 to 1 */
     823                 :          0 :         read_data_lin(ctx, addr, sizeof(data), &data);
     824                 :          0 :         data |= (1 << 2);
     825                 :          0 :         write_data_lin(ctx, addr, sizeof(data), &data);
     826                 :          0 : }
     827                 :            : 
     828                 :            : /*
     829                 :            :  * Enable FEC on media and/or host side. If the operation could be carried out
     830                 :            :  * return true. For some modules media side FEC is enabled but cannot be changed
     831                 :            :  *  while others allow changing the FEC state.
     832                 :            :  * For LR4 modules write operations (which are also not necessary) to the control
     833                 :            :  *  register must be avoided as this introduces I2C errors on NT200A01.
     834                 :            :  */
     835                 :            : 
     836                 :          0 : bool nthw_qsfp28_set_fec_enable(nim_i2c_ctx_p ctx, bool media_side_fec, bool host_side_fec)
     837                 :            : {
     838                 :            :         /*
     839                 :            :          * If the current FEC state does not match the wanted and the FEC cannot be
     840                 :            :          * controlled then the operation cannot be carried out
     841                 :            :          */
     842         [ #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ena != media_side_fec &&
     843         [ #  # ]:          0 :                 !ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ctrl)
     844                 :            :                 return false;
     845                 :            : 
     846         [ #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ena != host_side_fec &&
     847         [ #  # ]:          0 :                 !ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ctrl)
     848                 :            :                 return false;
     849                 :            : 
     850                 :            :         /*
     851                 :            :          * If the current media and host side FEC state matches the wanted states then
     852                 :            :          * no need to do more
     853                 :            :          */
     854   [ #  #  #  # ]:          0 :         if (ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ena == media_side_fec &&
     855                 :            :                 ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ena == host_side_fec)
     856                 :            :                 return true;
     857                 :            : 
     858                 :            :         /*
     859                 :            :          * SFF-8636, Rev 2.10a TABLE 6-29 Optional Channel Control)
     860                 :            :          * (Page 03h, Bytes 230-241)
     861                 :            :          */
     862                 :            :         const uint16_t addr = 230 + 3 * 128;
     863                 :          0 :         uint8_t data = 0;
     864                 :          0 :         read_data_lin(ctx, addr, sizeof(data), &data);
     865                 :            : 
     866         [ #  # ]:          0 :         if (media_side_fec)
     867                 :          0 :                 data &= (uint8_t)(~(1 << 6));
     868                 :            : 
     869                 :            :         else
     870                 :          0 :                 data |= (uint8_t)(1 << 6);
     871                 :            : 
     872         [ #  # ]:          0 :         if (host_side_fec)
     873                 :          0 :                 data |= (uint8_t)(1 << 7);
     874                 :            : 
     875                 :            :         else
     876                 :          0 :                 data &= (uint8_t)(~(1 << 7));
     877                 :            : 
     878                 :          0 :         write_data_lin(ctx, addr, sizeof(data), &data);
     879                 :          0 :         ctx->specific_u.qsfp.specific_u.qsfp28.media_side_fec_ena = media_side_fec;
     880                 :          0 :         ctx->specific_u.qsfp.specific_u.qsfp28.host_side_fec_ena = host_side_fec;
     881                 :          0 :         return true;
     882                 :            : }
     883                 :            : 
     884                 :          0 : void nim_agx_setup(struct nim_i2c_ctx *ctx, nthw_pcal6416a_t *p_io_nim, nthw_i2cm_t *p_nt_i2cm,
     885                 :            :         nthw_pca9849_t *p_ca9849)
     886                 :            : {
     887                 :          0 :         ctx->hwagx.p_nt_i2cm = p_nt_i2cm;
     888                 :          0 :         ctx->hwagx.p_ca9849 = p_ca9849;
     889                 :          0 :         ctx->hwagx.p_io_nim = p_io_nim;
     890                 :          0 : }
     891                 :            : 
     892                 :          0 : static void nim_agx_read(struct nim_i2c_ctx *ctx,
     893                 :            :         uint8_t dev_addr,
     894                 :            :         uint8_t reg_addr,
     895                 :            :         uint8_t data_len,
     896                 :            :         void *p_data)
     897                 :            : {
     898                 :          0 :         nthw_i2cm_t *p_nt_i2cm = ctx->hwagx.p_nt_i2cm;
     899                 :          0 :         nthw_pca9849_t *p_ca9849 = ctx->hwagx.p_ca9849;
     900                 :            :         uint8_t *data = (uint8_t *)p_data;
     901                 :            : 
     902                 :          0 :         rte_spinlock_lock(&p_nt_i2cm->i2cmmutex);
     903                 :          0 :         nthw_pca9849_set_channel(p_ca9849, ctx->hwagx.mux_channel);
     904                 :            : 
     905         [ #  # ]:          0 :         for (uint8_t i = 0; i < data_len; i++) {
     906                 :          0 :                 nthw_i2cm_read(p_nt_i2cm, (uint8_t)(dev_addr >> 1), (uint8_t)(reg_addr + i), data);
     907                 :          0 :                 data++;
     908                 :            :         }
     909                 :            : 
     910                 :            :         rte_spinlock_unlock(&p_nt_i2cm->i2cmmutex);
     911                 :          0 : }
     912                 :            : 
     913                 :          0 : static void nim_agx_write(struct nim_i2c_ctx *ctx,
     914                 :            :         uint8_t dev_addr,
     915                 :            :         uint8_t reg_addr,
     916                 :            :         uint8_t data_len,
     917                 :            :         void *p_data)
     918                 :            : {
     919                 :          0 :         nthw_i2cm_t *p_nt_i2cm = ctx->hwagx.p_nt_i2cm;
     920                 :          0 :         nthw_pca9849_t *p_ca9849 = ctx->hwagx.p_ca9849;
     921                 :            :         uint8_t *data = (uint8_t *)p_data;
     922                 :            : 
     923                 :          0 :         rte_spinlock_lock(&p_nt_i2cm->i2cmmutex);
     924                 :          0 :         nthw_pca9849_set_channel(p_ca9849, ctx->hwagx.mux_channel);
     925                 :            : 
     926         [ #  # ]:          0 :         for (uint8_t i = 0; i < data_len; i++) {
     927                 :          0 :                 nthw_i2cm_write(p_nt_i2cm, (uint8_t)(dev_addr >> 1), (uint8_t)(reg_addr + i),
     928                 :          0 :                         *data++);
     929                 :            :         }
     930                 :            : 
     931                 :            :         rte_spinlock_unlock(&p_nt_i2cm->i2cmmutex);
     932                 :          0 : }
     933                 :            : 
     934                 :          0 : int nim_agx_read_id(struct nim_i2c_ctx *ctx)
     935                 :            : {
     936                 :          0 :         nim_agx_read(ctx, 0xA0, 0, sizeof(ctx->nim_id), &ctx->nim_id);
     937                 :          0 :         return 0;
     938                 :            : }

Generated by: LCOV version 1.14