1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
13 static int mlxsw_env_validate_cable_ident(struct mlxsw_core *core, int id,
14 bool *qsfp, bool *cmis)
16 char eeprom_tmp[MLXSW_REG_MCIA_EEPROM_SIZE];
17 char mcia_pl[MLXSW_REG_MCIA_LEN];
21 mlxsw_reg_mcia_pack(mcia_pl, id, 0, MLXSW_REG_MCIA_PAGE0_LO_OFF, 0, 1,
22 MLXSW_REG_MCIA_I2C_ADDR_LOW);
23 err = mlxsw_reg_query(core, MLXSW_REG(mcia), mcia_pl);
26 mlxsw_reg_mcia_eeprom_memcpy_from(mcia_pl, eeprom_tmp);
27 ident = eeprom_tmp[0];
30 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_SFP:
33 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP:
34 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP_PLUS:
35 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP28:
38 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP_DD:
50 mlxsw_env_query_module_eeprom(struct mlxsw_core *mlxsw_core, int module,
51 u16 offset, u16 size, void *data,
52 bool qsfp, unsigned int *p_read_size)
54 char eeprom_tmp[MLXSW_REG_MCIA_EEPROM_SIZE];
55 char mcia_pl[MLXSW_REG_MCIA_LEN];
61 /* MCIA register accepts buffer size <= 48. Page of size 128 should be
62 * read by chunks of size 48, 48, 32. Align the size of the last chunk
63 * to avoid reading after the end of the page.
65 size = min_t(u16, size, MLXSW_REG_MCIA_EEPROM_SIZE);
67 if (offset < MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH &&
68 offset + size > MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH)
69 /* Cross pages read, read until offset 256 in low page */
70 size = MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH - offset;
72 i2c_addr = MLXSW_REG_MCIA_I2C_ADDR_LOW;
73 if (offset >= MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH) {
75 /* When reading upper pages 1, 2 and 3 the offset
76 * starts at 128. Please refer to "QSFP+ Memory Map"
77 * figure in SFF-8436 specification and to "CMIS Module
78 * Memory Map" figure in CMIS specification for
79 * graphical depiction.
81 page = MLXSW_REG_MCIA_PAGE_GET(offset);
82 offset -= MLXSW_REG_MCIA_EEPROM_UP_PAGE_LENGTH * page;
83 if (offset + size > MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH)
84 size = MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH - offset;
86 /* When reading upper pages 1, 2 and 3 the offset
87 * starts at 0 and I2C high address is used. Please refer
88 * refer to "Memory Organization" figure in SFF-8472
89 * specification for graphical depiction.
91 i2c_addr = MLXSW_REG_MCIA_I2C_ADDR_HIGH;
92 offset -= MLXSW_REG_MCIA_EEPROM_PAGE_LENGTH;
96 mlxsw_reg_mcia_pack(mcia_pl, module, 0, page, offset, size, i2c_addr);
98 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mcia), mcia_pl);
102 status = mlxsw_reg_mcia_status_get(mcia_pl);
106 mlxsw_reg_mcia_eeprom_memcpy_from(mcia_pl, eeprom_tmp);
107 memcpy(data, eeprom_tmp, size);
113 int mlxsw_env_module_temp_thresholds_get(struct mlxsw_core *core, int module,
116 char eeprom_tmp[MLXSW_REG_MCIA_EEPROM_SIZE];
118 u8 buf[MLXSW_REG_MCIA_TH_ITEM_SIZE];
121 char mcia_pl[MLXSW_REG_MCIA_LEN] = {0};
122 char mtmp_pl[MLXSW_REG_MTMP_LEN];
123 unsigned int module_temp;
128 mlxsw_reg_mtmp_pack(mtmp_pl, MLXSW_REG_MTMP_MODULE_INDEX_MIN + module,
130 err = mlxsw_reg_query(core, MLXSW_REG(mtmp), mtmp_pl);
133 mlxsw_reg_mtmp_unpack(mtmp_pl, &module_temp, NULL, NULL);
139 /* Read Free Side Device Temperature Thresholds from page 03h
140 * (MSB at lower byte address).
142 * 128-129 - Temp High Alarm (SFP_TEMP_HIGH_ALARM);
143 * 130-131 - Temp Low Alarm (SFP_TEMP_LOW_ALARM);
144 * 132-133 - Temp High Warning (SFP_TEMP_HIGH_WARN);
145 * 134-135 - Temp Low Warning (SFP_TEMP_LOW_WARN);
148 /* Validate module identifier value. */
149 err = mlxsw_env_validate_cable_ident(core, module, &qsfp, &cmis);
154 /* For QSFP/CMIS module-defined thresholds are located in page
155 * 02h, otherwise in page 03h.
158 page = MLXSW_REG_MCIA_TH_PAGE_CMIS_NUM;
160 page = MLXSW_REG_MCIA_TH_PAGE_NUM;
161 mlxsw_reg_mcia_pack(mcia_pl, module, 0, page,
162 MLXSW_REG_MCIA_TH_PAGE_OFF + off,
163 MLXSW_REG_MCIA_TH_ITEM_SIZE,
164 MLXSW_REG_MCIA_I2C_ADDR_LOW);
166 mlxsw_reg_mcia_pack(mcia_pl, module, 0,
167 MLXSW_REG_MCIA_PAGE0_LO,
168 off, MLXSW_REG_MCIA_TH_ITEM_SIZE,
169 MLXSW_REG_MCIA_I2C_ADDR_HIGH);
172 err = mlxsw_reg_query(core, MLXSW_REG(mcia), mcia_pl);
176 mlxsw_reg_mcia_eeprom_memcpy_from(mcia_pl, eeprom_tmp);
177 memcpy(temp_thresh.buf, eeprom_tmp, MLXSW_REG_MCIA_TH_ITEM_SIZE);
178 *temp = temp_thresh.temp * 1000;
183 int mlxsw_env_get_module_info(struct mlxsw_core *mlxsw_core, int module,
184 struct ethtool_modinfo *modinfo)
186 u8 module_info[MLXSW_REG_MCIA_EEPROM_MODULE_INFO_SIZE];
187 u16 offset = MLXSW_REG_MCIA_EEPROM_MODULE_INFO_SIZE;
188 u8 module_rev_id, module_id, diag_mon;
189 unsigned int read_size;
192 err = mlxsw_env_query_module_eeprom(mlxsw_core, module, 0, offset,
193 module_info, false, &read_size);
197 if (read_size < offset)
200 module_rev_id = module_info[MLXSW_REG_MCIA_EEPROM_MODULE_INFO_REV_ID];
201 module_id = module_info[MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID];
204 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP:
205 modinfo->type = ETH_MODULE_SFF_8436;
206 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
208 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP_PLUS:
209 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP28:
210 if (module_id == MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP28 ||
212 MLXSW_REG_MCIA_EEPROM_MODULE_INFO_REV_ID_8636) {
213 modinfo->type = ETH_MODULE_SFF_8636;
214 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
216 modinfo->type = ETH_MODULE_SFF_8436;
217 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
220 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_SFP:
221 /* Verify if transceiver provides diagnostic monitoring page */
222 err = mlxsw_env_query_module_eeprom(mlxsw_core, module,
223 SFP_DIAGMON, 1, &diag_mon,
231 modinfo->type = ETH_MODULE_SFF_8472;
233 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
235 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN / 2;
237 case MLXSW_REG_MCIA_EEPROM_MODULE_INFO_ID_QSFP_DD:
238 /* Use SFF_8636 as base type. ethtool should recognize specific
239 * type through the identifier value.
241 modinfo->type = ETH_MODULE_SFF_8636;
242 /* Verify if module EEPROM is a flat memory. In case of flat
243 * memory only page 00h (0-255 bytes) can be read. Otherwise
244 * upper pages 01h and 02h can also be read. Upper pages 10h
245 * and 11h are currently not supported by the driver.
247 if (module_info[MLXSW_REG_MCIA_EEPROM_MODULE_INFO_TYPE_ID] &
248 MLXSW_REG_MCIA_EEPROM_CMIS_FLAT_MEMORY)
249 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
251 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
259 EXPORT_SYMBOL(mlxsw_env_get_module_info);
261 int mlxsw_env_get_module_eeprom(struct net_device *netdev,
262 struct mlxsw_core *mlxsw_core, int module,
263 struct ethtool_eeprom *ee, u8 *data)
265 int offset = ee->offset;
266 unsigned int read_size;
274 memset(data, 0, ee->len);
275 /* Validate module identifier value. */
276 err = mlxsw_env_validate_cable_ident(mlxsw_core, module, &qsfp, &cmis);
280 while (i < ee->len) {
281 err = mlxsw_env_query_module_eeprom(mlxsw_core, module, offset,
282 ee->len - i, data + i,
285 netdev_err(netdev, "Eeprom query failed\n");
295 EXPORT_SYMBOL(mlxsw_env_get_module_eeprom);