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