1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2023, Intel Corporation. */
3
4 #include "ice_common.h"
5 #include "ice_sched.h"
6 #include "ice_adminq_cmd.h"
7 #include "ice_flow.h"
8 #include "ice_ptp_hw.h"
9
10 #define ICE_PF_RESET_WAIT_COUNT 300
11
12 static const char * const ice_link_mode_str_low[] = {
13 [0] = "100BASE_TX",
14 [1] = "100M_SGMII",
15 [2] = "1000BASE_T",
16 [3] = "1000BASE_SX",
17 [4] = "1000BASE_LX",
18 [5] = "1000BASE_KX",
19 [6] = "1G_SGMII",
20 [7] = "2500BASE_T",
21 [8] = "2500BASE_X",
22 [9] = "2500BASE_KX",
23 [10] = "5GBASE_T",
24 [11] = "5GBASE_KR",
25 [12] = "10GBASE_T",
26 [13] = "10G_SFI_DA",
27 [14] = "10GBASE_SR",
28 [15] = "10GBASE_LR",
29 [16] = "10GBASE_KR_CR1",
30 [17] = "10G_SFI_AOC_ACC",
31 [18] = "10G_SFI_C2C",
32 [19] = "25GBASE_T",
33 [20] = "25GBASE_CR",
34 [21] = "25GBASE_CR_S",
35 [22] = "25GBASE_CR1",
36 [23] = "25GBASE_SR",
37 [24] = "25GBASE_LR",
38 [25] = "25GBASE_KR",
39 [26] = "25GBASE_KR_S",
40 [27] = "25GBASE_KR1",
41 [28] = "25G_AUI_AOC_ACC",
42 [29] = "25G_AUI_C2C",
43 [30] = "40GBASE_CR4",
44 [31] = "40GBASE_SR4",
45 [32] = "40GBASE_LR4",
46 [33] = "40GBASE_KR4",
47 [34] = "40G_XLAUI_AOC_ACC",
48 [35] = "40G_XLAUI",
49 [36] = "50GBASE_CR2",
50 [37] = "50GBASE_SR2",
51 [38] = "50GBASE_LR2",
52 [39] = "50GBASE_KR2",
53 [40] = "50G_LAUI2_AOC_ACC",
54 [41] = "50G_LAUI2",
55 [42] = "50G_AUI2_AOC_ACC",
56 [43] = "50G_AUI2",
57 [44] = "50GBASE_CP",
58 [45] = "50GBASE_SR",
59 [46] = "50GBASE_FR",
60 [47] = "50GBASE_LR",
61 [48] = "50GBASE_KR_PAM4",
62 [49] = "50G_AUI1_AOC_ACC",
63 [50] = "50G_AUI1",
64 [51] = "100GBASE_CR4",
65 [52] = "100GBASE_SR4",
66 [53] = "100GBASE_LR4",
67 [54] = "100GBASE_KR4",
68 [55] = "100G_CAUI4_AOC_ACC",
69 [56] = "100G_CAUI4",
70 [57] = "100G_AUI4_AOC_ACC",
71 [58] = "100G_AUI4",
72 [59] = "100GBASE_CR_PAM4",
73 [60] = "100GBASE_KR_PAM4",
74 [61] = "100GBASE_CP2",
75 [62] = "100GBASE_SR2",
76 [63] = "100GBASE_DR",
77 };
78
79 static const char * const ice_link_mode_str_high[] = {
80 [0] = "100GBASE_KR2_PAM4",
81 [1] = "100G_CAUI2_AOC_ACC",
82 [2] = "100G_CAUI2",
83 [3] = "100G_AUI2_AOC_ACC",
84 [4] = "100G_AUI2",
85 };
86
87 /**
88 * ice_dump_phy_type - helper function to dump phy_type
89 * @hw: pointer to the HW structure
90 * @low: 64 bit value for phy_type_low
91 * @high: 64 bit value for phy_type_high
92 * @prefix: prefix string to differentiate multiple dumps
93 */
94 static void
ice_dump_phy_type(struct ice_hw * hw,u64 low,u64 high,const char * prefix)95 ice_dump_phy_type(struct ice_hw *hw, u64 low, u64 high, const char *prefix)
96 {
97 ice_debug(hw, ICE_DBG_PHY, "%s: phy_type_low: 0x%016llx\n", prefix, low);
98
99 for (u32 i = 0; i < BITS_PER_TYPE(typeof(low)); i++) {
100 if (low & BIT_ULL(i))
101 ice_debug(hw, ICE_DBG_PHY, "%s: bit(%d): %s\n",
102 prefix, i, ice_link_mode_str_low[i]);
103 }
104
105 ice_debug(hw, ICE_DBG_PHY, "%s: phy_type_high: 0x%016llx\n", prefix, high);
106
107 for (u32 i = 0; i < BITS_PER_TYPE(typeof(high)); i++) {
108 if (high & BIT_ULL(i))
109 ice_debug(hw, ICE_DBG_PHY, "%s: bit(%d): %s\n",
110 prefix, i, ice_link_mode_str_high[i]);
111 }
112 }
113
114 /**
115 * ice_set_mac_type - Sets MAC type
116 * @hw: pointer to the HW structure
117 *
118 * This function sets the MAC type of the adapter based on the
119 * vendor ID and device ID stored in the HW structure.
120 */
ice_set_mac_type(struct ice_hw * hw)121 static int ice_set_mac_type(struct ice_hw *hw)
122 {
123 if (hw->vendor_id != PCI_VENDOR_ID_INTEL)
124 return -ENODEV;
125
126 switch (hw->device_id) {
127 case ICE_DEV_ID_E810C_BACKPLANE:
128 case ICE_DEV_ID_E810C_QSFP:
129 case ICE_DEV_ID_E810C_SFP:
130 case ICE_DEV_ID_E810_XXV_BACKPLANE:
131 case ICE_DEV_ID_E810_XXV_QSFP:
132 case ICE_DEV_ID_E810_XXV_SFP:
133 hw->mac_type = ICE_MAC_E810;
134 break;
135 case ICE_DEV_ID_E823C_10G_BASE_T:
136 case ICE_DEV_ID_E823C_BACKPLANE:
137 case ICE_DEV_ID_E823C_QSFP:
138 case ICE_DEV_ID_E823C_SFP:
139 case ICE_DEV_ID_E823C_SGMII:
140 case ICE_DEV_ID_E822C_10G_BASE_T:
141 case ICE_DEV_ID_E822C_BACKPLANE:
142 case ICE_DEV_ID_E822C_QSFP:
143 case ICE_DEV_ID_E822C_SFP:
144 case ICE_DEV_ID_E822C_SGMII:
145 case ICE_DEV_ID_E822L_10G_BASE_T:
146 case ICE_DEV_ID_E822L_BACKPLANE:
147 case ICE_DEV_ID_E822L_SFP:
148 case ICE_DEV_ID_E822L_SGMII:
149 case ICE_DEV_ID_E823L_10G_BASE_T:
150 case ICE_DEV_ID_E823L_1GBE:
151 case ICE_DEV_ID_E823L_BACKPLANE:
152 case ICE_DEV_ID_E823L_QSFP:
153 case ICE_DEV_ID_E823L_SFP:
154 hw->mac_type = ICE_MAC_GENERIC;
155 break;
156 case ICE_DEV_ID_E830_BACKPLANE:
157 case ICE_DEV_ID_E830_QSFP56:
158 case ICE_DEV_ID_E830_SFP:
159 case ICE_DEV_ID_E830_SFP_DD:
160 hw->mac_type = ICE_MAC_E830;
161 break;
162 default:
163 hw->mac_type = ICE_MAC_UNKNOWN;
164 break;
165 }
166
167 ice_debug(hw, ICE_DBG_INIT, "mac_type: %d\n", hw->mac_type);
168 return 0;
169 }
170
171 /**
172 * ice_is_e810
173 * @hw: pointer to the hardware structure
174 *
175 * returns true if the device is E810 based, false if not.
176 */
ice_is_e810(struct ice_hw * hw)177 bool ice_is_e810(struct ice_hw *hw)
178 {
179 return hw->mac_type == ICE_MAC_E810;
180 }
181
182 /**
183 * ice_is_e810t
184 * @hw: pointer to the hardware structure
185 *
186 * returns true if the device is E810T based, false if not.
187 */
ice_is_e810t(struct ice_hw * hw)188 bool ice_is_e810t(struct ice_hw *hw)
189 {
190 switch (hw->device_id) {
191 case ICE_DEV_ID_E810C_SFP:
192 switch (hw->subsystem_device_id) {
193 case ICE_SUBDEV_ID_E810T:
194 case ICE_SUBDEV_ID_E810T2:
195 case ICE_SUBDEV_ID_E810T3:
196 case ICE_SUBDEV_ID_E810T4:
197 case ICE_SUBDEV_ID_E810T6:
198 case ICE_SUBDEV_ID_E810T7:
199 return true;
200 }
201 break;
202 case ICE_DEV_ID_E810C_QSFP:
203 switch (hw->subsystem_device_id) {
204 case ICE_SUBDEV_ID_E810T2:
205 case ICE_SUBDEV_ID_E810T3:
206 case ICE_SUBDEV_ID_E810T5:
207 return true;
208 }
209 break;
210 default:
211 break;
212 }
213
214 return false;
215 }
216
217 /**
218 * ice_is_e823
219 * @hw: pointer to the hardware structure
220 *
221 * returns true if the device is E823-L or E823-C based, false if not.
222 */
ice_is_e823(struct ice_hw * hw)223 bool ice_is_e823(struct ice_hw *hw)
224 {
225 switch (hw->device_id) {
226 case ICE_DEV_ID_E823L_BACKPLANE:
227 case ICE_DEV_ID_E823L_SFP:
228 case ICE_DEV_ID_E823L_10G_BASE_T:
229 case ICE_DEV_ID_E823L_1GBE:
230 case ICE_DEV_ID_E823L_QSFP:
231 case ICE_DEV_ID_E823C_BACKPLANE:
232 case ICE_DEV_ID_E823C_QSFP:
233 case ICE_DEV_ID_E823C_SFP:
234 case ICE_DEV_ID_E823C_10G_BASE_T:
235 case ICE_DEV_ID_E823C_SGMII:
236 return true;
237 default:
238 return false;
239 }
240 }
241
242 /**
243 * ice_clear_pf_cfg - Clear PF configuration
244 * @hw: pointer to the hardware structure
245 *
246 * Clears any existing PF configuration (VSIs, VSI lists, switch rules, port
247 * configuration, flow director filters, etc.).
248 */
ice_clear_pf_cfg(struct ice_hw * hw)249 int ice_clear_pf_cfg(struct ice_hw *hw)
250 {
251 struct ice_aq_desc desc;
252
253 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pf_cfg);
254
255 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
256 }
257
258 /**
259 * ice_aq_manage_mac_read - manage MAC address read command
260 * @hw: pointer to the HW struct
261 * @buf: a virtual buffer to hold the manage MAC read response
262 * @buf_size: Size of the virtual buffer
263 * @cd: pointer to command details structure or NULL
264 *
265 * This function is used to return per PF station MAC address (0x0107).
266 * NOTE: Upon successful completion of this command, MAC address information
267 * is returned in user specified buffer. Please interpret user specified
268 * buffer as "manage_mac_read" response.
269 * Response such as various MAC addresses are stored in HW struct (port.mac)
270 * ice_discover_dev_caps is expected to be called before this function is
271 * called.
272 */
273 static int
ice_aq_manage_mac_read(struct ice_hw * hw,void * buf,u16 buf_size,struct ice_sq_cd * cd)274 ice_aq_manage_mac_read(struct ice_hw *hw, void *buf, u16 buf_size,
275 struct ice_sq_cd *cd)
276 {
277 struct ice_aqc_manage_mac_read_resp *resp;
278 struct ice_aqc_manage_mac_read *cmd;
279 struct ice_aq_desc desc;
280 int status;
281 u16 flags;
282 u8 i;
283
284 cmd = &desc.params.mac_read;
285
286 if (buf_size < sizeof(*resp))
287 return -EINVAL;
288
289 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_read);
290
291 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
292 if (status)
293 return status;
294
295 resp = buf;
296 flags = le16_to_cpu(cmd->flags) & ICE_AQC_MAN_MAC_READ_M;
297
298 if (!(flags & ICE_AQC_MAN_MAC_LAN_ADDR_VALID)) {
299 ice_debug(hw, ICE_DBG_LAN, "got invalid MAC address\n");
300 return -EIO;
301 }
302
303 /* A single port can report up to two (LAN and WoL) addresses */
304 for (i = 0; i < cmd->num_addr; i++)
305 if (resp[i].addr_type == ICE_AQC_MAN_MAC_ADDR_TYPE_LAN) {
306 ether_addr_copy(hw->port_info->mac.lan_addr,
307 resp[i].mac_addr);
308 ether_addr_copy(hw->port_info->mac.perm_addr,
309 resp[i].mac_addr);
310 break;
311 }
312
313 return 0;
314 }
315
316 /**
317 * ice_aq_get_phy_caps - returns PHY capabilities
318 * @pi: port information structure
319 * @qual_mods: report qualified modules
320 * @report_mode: report mode capabilities
321 * @pcaps: structure for PHY capabilities to be filled
322 * @cd: pointer to command details structure or NULL
323 *
324 * Returns the various PHY capabilities supported on the Port (0x0600)
325 */
326 int
ice_aq_get_phy_caps(struct ice_port_info * pi,bool qual_mods,u8 report_mode,struct ice_aqc_get_phy_caps_data * pcaps,struct ice_sq_cd * cd)327 ice_aq_get_phy_caps(struct ice_port_info *pi, bool qual_mods, u8 report_mode,
328 struct ice_aqc_get_phy_caps_data *pcaps,
329 struct ice_sq_cd *cd)
330 {
331 struct ice_aqc_get_phy_caps *cmd;
332 u16 pcaps_size = sizeof(*pcaps);
333 struct ice_aq_desc desc;
334 const char *prefix;
335 struct ice_hw *hw;
336 int status;
337
338 cmd = &desc.params.get_phy;
339
340 if (!pcaps || (report_mode & ~ICE_AQC_REPORT_MODE_M) || !pi)
341 return -EINVAL;
342 hw = pi->hw;
343
344 if (report_mode == ICE_AQC_REPORT_DFLT_CFG &&
345 !ice_fw_supports_report_dflt_cfg(hw))
346 return -EINVAL;
347
348 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_phy_caps);
349
350 if (qual_mods)
351 cmd->param0 |= cpu_to_le16(ICE_AQC_GET_PHY_RQM);
352
353 cmd->param0 |= cpu_to_le16(report_mode);
354 status = ice_aq_send_cmd(hw, &desc, pcaps, pcaps_size, cd);
355
356 ice_debug(hw, ICE_DBG_LINK, "get phy caps dump\n");
357
358 switch (report_mode) {
359 case ICE_AQC_REPORT_TOPO_CAP_MEDIA:
360 prefix = "phy_caps_media";
361 break;
362 case ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA:
363 prefix = "phy_caps_no_media";
364 break;
365 case ICE_AQC_REPORT_ACTIVE_CFG:
366 prefix = "phy_caps_active";
367 break;
368 case ICE_AQC_REPORT_DFLT_CFG:
369 prefix = "phy_caps_default";
370 break;
371 default:
372 prefix = "phy_caps_invalid";
373 }
374
375 ice_dump_phy_type(hw, le64_to_cpu(pcaps->phy_type_low),
376 le64_to_cpu(pcaps->phy_type_high), prefix);
377
378 ice_debug(hw, ICE_DBG_LINK, "%s: report_mode = 0x%x\n",
379 prefix, report_mode);
380 ice_debug(hw, ICE_DBG_LINK, "%s: caps = 0x%x\n", prefix, pcaps->caps);
381 ice_debug(hw, ICE_DBG_LINK, "%s: low_power_ctrl_an = 0x%x\n", prefix,
382 pcaps->low_power_ctrl_an);
383 ice_debug(hw, ICE_DBG_LINK, "%s: eee_cap = 0x%x\n", prefix,
384 pcaps->eee_cap);
385 ice_debug(hw, ICE_DBG_LINK, "%s: eeer_value = 0x%x\n", prefix,
386 pcaps->eeer_value);
387 ice_debug(hw, ICE_DBG_LINK, "%s: link_fec_options = 0x%x\n", prefix,
388 pcaps->link_fec_options);
389 ice_debug(hw, ICE_DBG_LINK, "%s: module_compliance_enforcement = 0x%x\n",
390 prefix, pcaps->module_compliance_enforcement);
391 ice_debug(hw, ICE_DBG_LINK, "%s: extended_compliance_code = 0x%x\n",
392 prefix, pcaps->extended_compliance_code);
393 ice_debug(hw, ICE_DBG_LINK, "%s: module_type[0] = 0x%x\n", prefix,
394 pcaps->module_type[0]);
395 ice_debug(hw, ICE_DBG_LINK, "%s: module_type[1] = 0x%x\n", prefix,
396 pcaps->module_type[1]);
397 ice_debug(hw, ICE_DBG_LINK, "%s: module_type[2] = 0x%x\n", prefix,
398 pcaps->module_type[2]);
399
400 if (!status && report_mode == ICE_AQC_REPORT_TOPO_CAP_MEDIA) {
401 pi->phy.phy_type_low = le64_to_cpu(pcaps->phy_type_low);
402 pi->phy.phy_type_high = le64_to_cpu(pcaps->phy_type_high);
403 memcpy(pi->phy.link_info.module_type, &pcaps->module_type,
404 sizeof(pi->phy.link_info.module_type));
405 }
406
407 return status;
408 }
409
410 /**
411 * ice_aq_get_link_topo_handle - get link topology node return status
412 * @pi: port information structure
413 * @node_type: requested node type
414 * @cd: pointer to command details structure or NULL
415 *
416 * Get link topology node return status for specified node type (0x06E0)
417 *
418 * Node type cage can be used to determine if cage is present. If AQC
419 * returns error (ENOENT), then no cage present. If no cage present, then
420 * connection type is backplane or BASE-T.
421 */
422 static int
ice_aq_get_link_topo_handle(struct ice_port_info * pi,u8 node_type,struct ice_sq_cd * cd)423 ice_aq_get_link_topo_handle(struct ice_port_info *pi, u8 node_type,
424 struct ice_sq_cd *cd)
425 {
426 struct ice_aqc_get_link_topo *cmd;
427 struct ice_aq_desc desc;
428
429 cmd = &desc.params.get_link_topo;
430
431 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
432
433 cmd->addr.topo_params.node_type_ctx =
434 (ICE_AQC_LINK_TOPO_NODE_CTX_PORT <<
435 ICE_AQC_LINK_TOPO_NODE_CTX_S);
436
437 /* set node type */
438 cmd->addr.topo_params.node_type_ctx |=
439 (ICE_AQC_LINK_TOPO_NODE_TYPE_M & node_type);
440
441 return ice_aq_send_cmd(pi->hw, &desc, NULL, 0, cd);
442 }
443
444 /**
445 * ice_is_media_cage_present
446 * @pi: port information structure
447 *
448 * Returns true if media cage is present, else false. If no cage, then
449 * media type is backplane or BASE-T.
450 */
ice_is_media_cage_present(struct ice_port_info * pi)451 static bool ice_is_media_cage_present(struct ice_port_info *pi)
452 {
453 /* Node type cage can be used to determine if cage is present. If AQC
454 * returns error (ENOENT), then no cage present. If no cage present then
455 * connection type is backplane or BASE-T.
456 */
457 return !ice_aq_get_link_topo_handle(pi,
458 ICE_AQC_LINK_TOPO_NODE_TYPE_CAGE,
459 NULL);
460 }
461
462 /**
463 * ice_get_media_type - Gets media type
464 * @pi: port information structure
465 */
ice_get_media_type(struct ice_port_info * pi)466 static enum ice_media_type ice_get_media_type(struct ice_port_info *pi)
467 {
468 struct ice_link_status *hw_link_info;
469
470 if (!pi)
471 return ICE_MEDIA_UNKNOWN;
472
473 hw_link_info = &pi->phy.link_info;
474 if (hw_link_info->phy_type_low && hw_link_info->phy_type_high)
475 /* If more than one media type is selected, report unknown */
476 return ICE_MEDIA_UNKNOWN;
477
478 if (hw_link_info->phy_type_low) {
479 /* 1G SGMII is a special case where some DA cable PHYs
480 * may show this as an option when it really shouldn't
481 * be since SGMII is meant to be between a MAC and a PHY
482 * in a backplane. Try to detect this case and handle it
483 */
484 if (hw_link_info->phy_type_low == ICE_PHY_TYPE_LOW_1G_SGMII &&
485 (hw_link_info->module_type[ICE_AQC_MOD_TYPE_IDENT] ==
486 ICE_AQC_MOD_TYPE_BYTE1_SFP_PLUS_CU_ACTIVE ||
487 hw_link_info->module_type[ICE_AQC_MOD_TYPE_IDENT] ==
488 ICE_AQC_MOD_TYPE_BYTE1_SFP_PLUS_CU_PASSIVE))
489 return ICE_MEDIA_DA;
490
491 switch (hw_link_info->phy_type_low) {
492 case ICE_PHY_TYPE_LOW_1000BASE_SX:
493 case ICE_PHY_TYPE_LOW_1000BASE_LX:
494 case ICE_PHY_TYPE_LOW_10GBASE_SR:
495 case ICE_PHY_TYPE_LOW_10GBASE_LR:
496 case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
497 case ICE_PHY_TYPE_LOW_25GBASE_SR:
498 case ICE_PHY_TYPE_LOW_25GBASE_LR:
499 case ICE_PHY_TYPE_LOW_40GBASE_SR4:
500 case ICE_PHY_TYPE_LOW_40GBASE_LR4:
501 case ICE_PHY_TYPE_LOW_50GBASE_SR2:
502 case ICE_PHY_TYPE_LOW_50GBASE_LR2:
503 case ICE_PHY_TYPE_LOW_50GBASE_SR:
504 case ICE_PHY_TYPE_LOW_50GBASE_FR:
505 case ICE_PHY_TYPE_LOW_50GBASE_LR:
506 case ICE_PHY_TYPE_LOW_100GBASE_SR4:
507 case ICE_PHY_TYPE_LOW_100GBASE_LR4:
508 case ICE_PHY_TYPE_LOW_100GBASE_SR2:
509 case ICE_PHY_TYPE_LOW_100GBASE_DR:
510 case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
511 case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
512 case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
513 case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
514 case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
515 case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
516 case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
517 case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
518 return ICE_MEDIA_FIBER;
519 case ICE_PHY_TYPE_LOW_100BASE_TX:
520 case ICE_PHY_TYPE_LOW_1000BASE_T:
521 case ICE_PHY_TYPE_LOW_2500BASE_T:
522 case ICE_PHY_TYPE_LOW_5GBASE_T:
523 case ICE_PHY_TYPE_LOW_10GBASE_T:
524 case ICE_PHY_TYPE_LOW_25GBASE_T:
525 return ICE_MEDIA_BASET;
526 case ICE_PHY_TYPE_LOW_10G_SFI_DA:
527 case ICE_PHY_TYPE_LOW_25GBASE_CR:
528 case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
529 case ICE_PHY_TYPE_LOW_25GBASE_CR1:
530 case ICE_PHY_TYPE_LOW_40GBASE_CR4:
531 case ICE_PHY_TYPE_LOW_50GBASE_CR2:
532 case ICE_PHY_TYPE_LOW_50GBASE_CP:
533 case ICE_PHY_TYPE_LOW_100GBASE_CR4:
534 case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
535 case ICE_PHY_TYPE_LOW_100GBASE_CP2:
536 return ICE_MEDIA_DA;
537 case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
538 case ICE_PHY_TYPE_LOW_40G_XLAUI:
539 case ICE_PHY_TYPE_LOW_50G_LAUI2:
540 case ICE_PHY_TYPE_LOW_50G_AUI2:
541 case ICE_PHY_TYPE_LOW_50G_AUI1:
542 case ICE_PHY_TYPE_LOW_100G_AUI4:
543 case ICE_PHY_TYPE_LOW_100G_CAUI4:
544 if (ice_is_media_cage_present(pi))
545 return ICE_MEDIA_DA;
546 fallthrough;
547 case ICE_PHY_TYPE_LOW_1000BASE_KX:
548 case ICE_PHY_TYPE_LOW_2500BASE_KX:
549 case ICE_PHY_TYPE_LOW_2500BASE_X:
550 case ICE_PHY_TYPE_LOW_5GBASE_KR:
551 case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
552 case ICE_PHY_TYPE_LOW_25GBASE_KR:
553 case ICE_PHY_TYPE_LOW_25GBASE_KR1:
554 case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
555 case ICE_PHY_TYPE_LOW_40GBASE_KR4:
556 case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
557 case ICE_PHY_TYPE_LOW_50GBASE_KR2:
558 case ICE_PHY_TYPE_LOW_100GBASE_KR4:
559 case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
560 return ICE_MEDIA_BACKPLANE;
561 }
562 } else {
563 switch (hw_link_info->phy_type_high) {
564 case ICE_PHY_TYPE_HIGH_100G_AUI2:
565 case ICE_PHY_TYPE_HIGH_100G_CAUI2:
566 if (ice_is_media_cage_present(pi))
567 return ICE_MEDIA_DA;
568 fallthrough;
569 case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
570 return ICE_MEDIA_BACKPLANE;
571 case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
572 case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
573 return ICE_MEDIA_FIBER;
574 }
575 }
576 return ICE_MEDIA_UNKNOWN;
577 }
578
579 /**
580 * ice_aq_get_link_info
581 * @pi: port information structure
582 * @ena_lse: enable/disable LinkStatusEvent reporting
583 * @link: pointer to link status structure - optional
584 * @cd: pointer to command details structure or NULL
585 *
586 * Get Link Status (0x607). Returns the link status of the adapter.
587 */
588 int
ice_aq_get_link_info(struct ice_port_info * pi,bool ena_lse,struct ice_link_status * link,struct ice_sq_cd * cd)589 ice_aq_get_link_info(struct ice_port_info *pi, bool ena_lse,
590 struct ice_link_status *link, struct ice_sq_cd *cd)
591 {
592 struct ice_aqc_get_link_status_data link_data = { 0 };
593 struct ice_aqc_get_link_status *resp;
594 struct ice_link_status *li_old, *li;
595 enum ice_media_type *hw_media_type;
596 struct ice_fc_info *hw_fc_info;
597 bool tx_pause, rx_pause;
598 struct ice_aq_desc desc;
599 struct ice_hw *hw;
600 u16 cmd_flags;
601 int status;
602
603 if (!pi)
604 return -EINVAL;
605 hw = pi->hw;
606 li_old = &pi->phy.link_info_old;
607 hw_media_type = &pi->phy.media_type;
608 li = &pi->phy.link_info;
609 hw_fc_info = &pi->fc;
610
611 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_status);
612 cmd_flags = (ena_lse) ? ICE_AQ_LSE_ENA : ICE_AQ_LSE_DIS;
613 resp = &desc.params.get_link_status;
614 resp->cmd_flags = cpu_to_le16(cmd_flags);
615 resp->lport_num = pi->lport;
616
617 status = ice_aq_send_cmd(hw, &desc, &link_data, sizeof(link_data), cd);
618
619 if (status)
620 return status;
621
622 /* save off old link status information */
623 *li_old = *li;
624
625 /* update current link status information */
626 li->link_speed = le16_to_cpu(link_data.link_speed);
627 li->phy_type_low = le64_to_cpu(link_data.phy_type_low);
628 li->phy_type_high = le64_to_cpu(link_data.phy_type_high);
629 *hw_media_type = ice_get_media_type(pi);
630 li->link_info = link_data.link_info;
631 li->link_cfg_err = link_data.link_cfg_err;
632 li->an_info = link_data.an_info;
633 li->ext_info = link_data.ext_info;
634 li->max_frame_size = le16_to_cpu(link_data.max_frame_size);
635 li->fec_info = link_data.cfg & ICE_AQ_FEC_MASK;
636 li->topo_media_conflict = link_data.topo_media_conflict;
637 li->pacing = link_data.cfg & (ICE_AQ_CFG_PACING_M |
638 ICE_AQ_CFG_PACING_TYPE_M);
639
640 /* update fc info */
641 tx_pause = !!(link_data.an_info & ICE_AQ_LINK_PAUSE_TX);
642 rx_pause = !!(link_data.an_info & ICE_AQ_LINK_PAUSE_RX);
643 if (tx_pause && rx_pause)
644 hw_fc_info->current_mode = ICE_FC_FULL;
645 else if (tx_pause)
646 hw_fc_info->current_mode = ICE_FC_TX_PAUSE;
647 else if (rx_pause)
648 hw_fc_info->current_mode = ICE_FC_RX_PAUSE;
649 else
650 hw_fc_info->current_mode = ICE_FC_NONE;
651
652 li->lse_ena = !!(resp->cmd_flags & cpu_to_le16(ICE_AQ_LSE_IS_ENABLED));
653
654 ice_debug(hw, ICE_DBG_LINK, "get link info\n");
655 ice_debug(hw, ICE_DBG_LINK, " link_speed = 0x%x\n", li->link_speed);
656 ice_debug(hw, ICE_DBG_LINK, " phy_type_low = 0x%llx\n",
657 (unsigned long long)li->phy_type_low);
658 ice_debug(hw, ICE_DBG_LINK, " phy_type_high = 0x%llx\n",
659 (unsigned long long)li->phy_type_high);
660 ice_debug(hw, ICE_DBG_LINK, " media_type = 0x%x\n", *hw_media_type);
661 ice_debug(hw, ICE_DBG_LINK, " link_info = 0x%x\n", li->link_info);
662 ice_debug(hw, ICE_DBG_LINK, " link_cfg_err = 0x%x\n", li->link_cfg_err);
663 ice_debug(hw, ICE_DBG_LINK, " an_info = 0x%x\n", li->an_info);
664 ice_debug(hw, ICE_DBG_LINK, " ext_info = 0x%x\n", li->ext_info);
665 ice_debug(hw, ICE_DBG_LINK, " fec_info = 0x%x\n", li->fec_info);
666 ice_debug(hw, ICE_DBG_LINK, " lse_ena = 0x%x\n", li->lse_ena);
667 ice_debug(hw, ICE_DBG_LINK, " max_frame = 0x%x\n",
668 li->max_frame_size);
669 ice_debug(hw, ICE_DBG_LINK, " pacing = 0x%x\n", li->pacing);
670
671 /* save link status information */
672 if (link)
673 *link = *li;
674
675 /* flag cleared so calling functions don't call AQ again */
676 pi->phy.get_link_info = false;
677
678 return 0;
679 }
680
681 /**
682 * ice_fill_tx_timer_and_fc_thresh
683 * @hw: pointer to the HW struct
684 * @cmd: pointer to MAC cfg structure
685 *
686 * Add Tx timer and FC refresh threshold info to Set MAC Config AQ command
687 * descriptor
688 */
689 static void
ice_fill_tx_timer_and_fc_thresh(struct ice_hw * hw,struct ice_aqc_set_mac_cfg * cmd)690 ice_fill_tx_timer_and_fc_thresh(struct ice_hw *hw,
691 struct ice_aqc_set_mac_cfg *cmd)
692 {
693 u32 val, fc_thres_m;
694
695 /* We read back the transmit timer and FC threshold value of
696 * LFC. Thus, we will use index =
697 * PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA_MAX_INDEX.
698 *
699 * Also, because we are operating on transmit timer and FC
700 * threshold of LFC, we don't turn on any bit in tx_tmr_priority
701 */
702 #define E800_IDX_OF_LFC E800_PRTMAC_HSEC_CTL_TX_PS_QNT_MAX
703 #define E800_REFRESH_TMR E800_PRTMAC_HSEC_CTL_TX_PS_RFSH_TMR
704
705 if (hw->mac_type == ICE_MAC_E830) {
706 /* Retrieve the transmit timer */
707 val = rd32(hw, E830_PRTMAC_CL01_PS_QNT);
708 cmd->tx_tmr_value =
709 le16_encode_bits(val, E830_PRTMAC_CL01_PS_QNT_CL0_M);
710
711 /* Retrieve the fc threshold */
712 val = rd32(hw, E830_PRTMAC_CL01_QNT_THR);
713 fc_thres_m = E830_PRTMAC_CL01_QNT_THR_CL0_M;
714 } else {
715 /* Retrieve the transmit timer */
716 val = rd32(hw,
717 E800_PRTMAC_HSEC_CTL_TX_PS_QNT(E800_IDX_OF_LFC));
718 cmd->tx_tmr_value =
719 le16_encode_bits(val,
720 E800_PRTMAC_HSEC_CTL_TX_PS_QNT_M);
721
722 /* Retrieve the fc threshold */
723 val = rd32(hw,
724 E800_REFRESH_TMR(E800_IDX_OF_LFC));
725 fc_thres_m = E800_PRTMAC_HSEC_CTL_TX_PS_RFSH_TMR_M;
726 }
727 cmd->fc_refresh_threshold = le16_encode_bits(val, fc_thres_m);
728 }
729
730 /**
731 * ice_aq_set_mac_cfg
732 * @hw: pointer to the HW struct
733 * @max_frame_size: Maximum Frame Size to be supported
734 * @cd: pointer to command details structure or NULL
735 *
736 * Set MAC configuration (0x0603)
737 */
738 int
ice_aq_set_mac_cfg(struct ice_hw * hw,u16 max_frame_size,struct ice_sq_cd * cd)739 ice_aq_set_mac_cfg(struct ice_hw *hw, u16 max_frame_size, struct ice_sq_cd *cd)
740 {
741 struct ice_aqc_set_mac_cfg *cmd;
742 struct ice_aq_desc desc;
743
744 cmd = &desc.params.set_mac_cfg;
745
746 if (max_frame_size == 0)
747 return -EINVAL;
748
749 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_mac_cfg);
750
751 cmd->max_frame_size = cpu_to_le16(max_frame_size);
752
753 ice_fill_tx_timer_and_fc_thresh(hw, cmd);
754
755 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
756 }
757
758 /**
759 * ice_init_fltr_mgmt_struct - initializes filter management list and locks
760 * @hw: pointer to the HW struct
761 */
ice_init_fltr_mgmt_struct(struct ice_hw * hw)762 static int ice_init_fltr_mgmt_struct(struct ice_hw *hw)
763 {
764 struct ice_switch_info *sw;
765 int status;
766
767 hw->switch_info = devm_kzalloc(ice_hw_to_dev(hw),
768 sizeof(*hw->switch_info), GFP_KERNEL);
769 sw = hw->switch_info;
770
771 if (!sw)
772 return -ENOMEM;
773
774 INIT_LIST_HEAD(&sw->vsi_list_map_head);
775 sw->prof_res_bm_init = 0;
776
777 status = ice_init_def_sw_recp(hw);
778 if (status) {
779 devm_kfree(ice_hw_to_dev(hw), hw->switch_info);
780 return status;
781 }
782 return 0;
783 }
784
785 /**
786 * ice_cleanup_fltr_mgmt_struct - cleanup filter management list and locks
787 * @hw: pointer to the HW struct
788 */
ice_cleanup_fltr_mgmt_struct(struct ice_hw * hw)789 static void ice_cleanup_fltr_mgmt_struct(struct ice_hw *hw)
790 {
791 struct ice_switch_info *sw = hw->switch_info;
792 struct ice_vsi_list_map_info *v_pos_map;
793 struct ice_vsi_list_map_info *v_tmp_map;
794 struct ice_sw_recipe *recps;
795 u8 i;
796
797 list_for_each_entry_safe(v_pos_map, v_tmp_map, &sw->vsi_list_map_head,
798 list_entry) {
799 list_del(&v_pos_map->list_entry);
800 devm_kfree(ice_hw_to_dev(hw), v_pos_map);
801 }
802 recps = sw->recp_list;
803 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
804 struct ice_recp_grp_entry *rg_entry, *tmprg_entry;
805
806 recps[i].root_rid = i;
807 list_for_each_entry_safe(rg_entry, tmprg_entry,
808 &recps[i].rg_list, l_entry) {
809 list_del(&rg_entry->l_entry);
810 devm_kfree(ice_hw_to_dev(hw), rg_entry);
811 }
812
813 if (recps[i].adv_rule) {
814 struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
815 struct ice_adv_fltr_mgmt_list_entry *lst_itr;
816
817 mutex_destroy(&recps[i].filt_rule_lock);
818 list_for_each_entry_safe(lst_itr, tmp_entry,
819 &recps[i].filt_rules,
820 list_entry) {
821 list_del(&lst_itr->list_entry);
822 devm_kfree(ice_hw_to_dev(hw), lst_itr->lkups);
823 devm_kfree(ice_hw_to_dev(hw), lst_itr);
824 }
825 } else {
826 struct ice_fltr_mgmt_list_entry *lst_itr, *tmp_entry;
827
828 mutex_destroy(&recps[i].filt_rule_lock);
829 list_for_each_entry_safe(lst_itr, tmp_entry,
830 &recps[i].filt_rules,
831 list_entry) {
832 list_del(&lst_itr->list_entry);
833 devm_kfree(ice_hw_to_dev(hw), lst_itr);
834 }
835 }
836 devm_kfree(ice_hw_to_dev(hw), recps[i].root_buf);
837 }
838 ice_rm_all_sw_replay_rule_info(hw);
839 devm_kfree(ice_hw_to_dev(hw), sw->recp_list);
840 devm_kfree(ice_hw_to_dev(hw), sw);
841 }
842
843 /**
844 * ice_get_fw_log_cfg - get FW logging configuration
845 * @hw: pointer to the HW struct
846 */
ice_get_fw_log_cfg(struct ice_hw * hw)847 static int ice_get_fw_log_cfg(struct ice_hw *hw)
848 {
849 struct ice_aq_desc desc;
850 __le16 *config;
851 int status;
852 u16 size;
853
854 size = sizeof(*config) * ICE_AQC_FW_LOG_ID_MAX;
855 config = kzalloc(size, GFP_KERNEL);
856 if (!config)
857 return -ENOMEM;
858
859 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_fw_logging_info);
860
861 status = ice_aq_send_cmd(hw, &desc, config, size, NULL);
862 if (!status) {
863 u16 i;
864
865 /* Save FW logging information into the HW structure */
866 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
867 u16 v, m, flgs;
868
869 v = le16_to_cpu(config[i]);
870 m = (v & ICE_AQC_FW_LOG_ID_M) >> ICE_AQC_FW_LOG_ID_S;
871 flgs = (v & ICE_AQC_FW_LOG_EN_M) >> ICE_AQC_FW_LOG_EN_S;
872
873 if (m < ICE_AQC_FW_LOG_ID_MAX)
874 hw->fw_log.evnts[m].cur = flgs;
875 }
876 }
877
878 kfree(config);
879
880 return status;
881 }
882
883 /**
884 * ice_cfg_fw_log - configure FW logging
885 * @hw: pointer to the HW struct
886 * @enable: enable certain FW logging events if true, disable all if false
887 *
888 * This function enables/disables the FW logging via Rx CQ events and a UART
889 * port based on predetermined configurations. FW logging via the Rx CQ can be
890 * enabled/disabled for individual PF's. However, FW logging via the UART can
891 * only be enabled/disabled for all PFs on the same device.
892 *
893 * To enable overall FW logging, the "cq_en" and "uart_en" enable bits in
894 * hw->fw_log need to be set accordingly, e.g. based on user-provided input,
895 * before initializing the device.
896 *
897 * When re/configuring FW logging, callers need to update the "cfg" elements of
898 * the hw->fw_log.evnts array with the desired logging event configurations for
899 * modules of interest. When disabling FW logging completely, the callers can
900 * just pass false in the "enable" parameter. On completion, the function will
901 * update the "cur" element of the hw->fw_log.evnts array with the resulting
902 * logging event configurations of the modules that are being re/configured. FW
903 * logging modules that are not part of a reconfiguration operation retain their
904 * previous states.
905 *
906 * Before resetting the device, it is recommended that the driver disables FW
907 * logging before shutting down the control queue. When disabling FW logging
908 * ("enable" = false), the latest configurations of FW logging events stored in
909 * hw->fw_log.evnts[] are not overridden to allow them to be reconfigured after
910 * a device reset.
911 *
912 * When enabling FW logging to emit log messages via the Rx CQ during the
913 * device's initialization phase, a mechanism alternative to interrupt handlers
914 * needs to be used to extract FW log messages from the Rx CQ periodically and
915 * to prevent the Rx CQ from being full and stalling other types of control
916 * messages from FW to SW. Interrupts are typically disabled during the device's
917 * initialization phase.
918 */
ice_cfg_fw_log(struct ice_hw * hw,bool enable)919 static int ice_cfg_fw_log(struct ice_hw *hw, bool enable)
920 {
921 struct ice_aqc_fw_logging *cmd;
922 u16 i, chgs = 0, len = 0;
923 struct ice_aq_desc desc;
924 __le16 *data = NULL;
925 u8 actv_evnts = 0;
926 void *buf = NULL;
927 int status = 0;
928
929 if (!hw->fw_log.cq_en && !hw->fw_log.uart_en)
930 return 0;
931
932 /* Disable FW logging only when the control queue is still responsive */
933 if (!enable &&
934 (!hw->fw_log.actv_evnts || !ice_check_sq_alive(hw, &hw->adminq)))
935 return 0;
936
937 /* Get current FW log settings */
938 status = ice_get_fw_log_cfg(hw);
939 if (status)
940 return status;
941
942 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_fw_logging);
943 cmd = &desc.params.fw_logging;
944
945 /* Indicate which controls are valid */
946 if (hw->fw_log.cq_en)
947 cmd->log_ctrl_valid |= ICE_AQC_FW_LOG_AQ_VALID;
948
949 if (hw->fw_log.uart_en)
950 cmd->log_ctrl_valid |= ICE_AQC_FW_LOG_UART_VALID;
951
952 if (enable) {
953 /* Fill in an array of entries with FW logging modules and
954 * logging events being reconfigured.
955 */
956 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
957 u16 val;
958
959 /* Keep track of enabled event types */
960 actv_evnts |= hw->fw_log.evnts[i].cfg;
961
962 if (hw->fw_log.evnts[i].cfg == hw->fw_log.evnts[i].cur)
963 continue;
964
965 if (!data) {
966 data = devm_kcalloc(ice_hw_to_dev(hw),
967 ICE_AQC_FW_LOG_ID_MAX,
968 sizeof(*data),
969 GFP_KERNEL);
970 if (!data)
971 return -ENOMEM;
972 }
973
974 val = i << ICE_AQC_FW_LOG_ID_S;
975 val |= hw->fw_log.evnts[i].cfg << ICE_AQC_FW_LOG_EN_S;
976 data[chgs++] = cpu_to_le16(val);
977 }
978
979 /* Only enable FW logging if at least one module is specified.
980 * If FW logging is currently enabled but all modules are not
981 * enabled to emit log messages, disable FW logging altogether.
982 */
983 if (actv_evnts) {
984 /* Leave if there is effectively no change */
985 if (!chgs)
986 goto out;
987
988 if (hw->fw_log.cq_en)
989 cmd->log_ctrl |= ICE_AQC_FW_LOG_AQ_EN;
990
991 if (hw->fw_log.uart_en)
992 cmd->log_ctrl |= ICE_AQC_FW_LOG_UART_EN;
993
994 buf = data;
995 len = sizeof(*data) * chgs;
996 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
997 }
998 }
999
1000 status = ice_aq_send_cmd(hw, &desc, buf, len, NULL);
1001 if (!status) {
1002 /* Update the current configuration to reflect events enabled.
1003 * hw->fw_log.cq_en and hw->fw_log.uart_en indicate if the FW
1004 * logging mode is enabled for the device. They do not reflect
1005 * actual modules being enabled to emit log messages. So, their
1006 * values remain unchanged even when all modules are disabled.
1007 */
1008 u16 cnt = enable ? chgs : (u16)ICE_AQC_FW_LOG_ID_MAX;
1009
1010 hw->fw_log.actv_evnts = actv_evnts;
1011 for (i = 0; i < cnt; i++) {
1012 u16 v, m;
1013
1014 if (!enable) {
1015 /* When disabling all FW logging events as part
1016 * of device's de-initialization, the original
1017 * configurations are retained, and can be used
1018 * to reconfigure FW logging later if the device
1019 * is re-initialized.
1020 */
1021 hw->fw_log.evnts[i].cur = 0;
1022 continue;
1023 }
1024
1025 v = le16_to_cpu(data[i]);
1026 m = (v & ICE_AQC_FW_LOG_ID_M) >> ICE_AQC_FW_LOG_ID_S;
1027 hw->fw_log.evnts[m].cur = hw->fw_log.evnts[m].cfg;
1028 }
1029 }
1030
1031 out:
1032 devm_kfree(ice_hw_to_dev(hw), data);
1033
1034 return status;
1035 }
1036
1037 /**
1038 * ice_output_fw_log
1039 * @hw: pointer to the HW struct
1040 * @desc: pointer to the AQ message descriptor
1041 * @buf: pointer to the buffer accompanying the AQ message
1042 *
1043 * Formats a FW Log message and outputs it via the standard driver logs.
1044 */
ice_output_fw_log(struct ice_hw * hw,struct ice_aq_desc * desc,void * buf)1045 void ice_output_fw_log(struct ice_hw *hw, struct ice_aq_desc *desc, void *buf)
1046 {
1047 ice_debug(hw, ICE_DBG_FW_LOG, "[ FW Log Msg Start ]\n");
1048 ice_debug_array(hw, ICE_DBG_FW_LOG, 16, 1, (u8 *)buf,
1049 le16_to_cpu(desc->datalen));
1050 ice_debug(hw, ICE_DBG_FW_LOG, "[ FW Log Msg End ]\n");
1051 }
1052
1053 /**
1054 * ice_get_itr_intrl_gran
1055 * @hw: pointer to the HW struct
1056 *
1057 * Determines the ITR/INTRL granularities based on the maximum aggregate
1058 * bandwidth according to the device's configuration during power-on.
1059 */
ice_get_itr_intrl_gran(struct ice_hw * hw)1060 static void ice_get_itr_intrl_gran(struct ice_hw *hw)
1061 {
1062 u8 max_agg_bw = (rd32(hw, GL_PWR_MODE_CTL) &
1063 GL_PWR_MODE_CTL_CAR_MAX_BW_M) >>
1064 GL_PWR_MODE_CTL_CAR_MAX_BW_S;
1065
1066 switch (max_agg_bw) {
1067 case ICE_MAX_AGG_BW_200G:
1068 case ICE_MAX_AGG_BW_100G:
1069 case ICE_MAX_AGG_BW_50G:
1070 hw->itr_gran = ICE_ITR_GRAN_ABOVE_25;
1071 hw->intrl_gran = ICE_INTRL_GRAN_ABOVE_25;
1072 break;
1073 case ICE_MAX_AGG_BW_25G:
1074 hw->itr_gran = ICE_ITR_GRAN_MAX_25;
1075 hw->intrl_gran = ICE_INTRL_GRAN_MAX_25;
1076 break;
1077 }
1078 }
1079
1080 /**
1081 * ice_init_hw - main hardware initialization routine
1082 * @hw: pointer to the hardware structure
1083 */
ice_init_hw(struct ice_hw * hw)1084 int ice_init_hw(struct ice_hw *hw)
1085 {
1086 struct ice_aqc_get_phy_caps_data *pcaps;
1087 u16 mac_buf_len;
1088 void *mac_buf;
1089 int status;
1090
1091 /* Set MAC type based on DeviceID */
1092 status = ice_set_mac_type(hw);
1093 if (status)
1094 return status;
1095
1096 hw->pf_id = (u8)(rd32(hw, PF_FUNC_RID) &
1097 PF_FUNC_RID_FUNC_NUM_M) >>
1098 PF_FUNC_RID_FUNC_NUM_S;
1099
1100 status = ice_reset(hw, ICE_RESET_PFR);
1101 if (status)
1102 return status;
1103
1104 ice_get_itr_intrl_gran(hw);
1105
1106 status = ice_create_all_ctrlq(hw);
1107 if (status)
1108 goto err_unroll_cqinit;
1109
1110 /* Enable FW logging. Not fatal if this fails. */
1111 status = ice_cfg_fw_log(hw, true);
1112 if (status)
1113 ice_debug(hw, ICE_DBG_INIT, "Failed to enable FW logging.\n");
1114
1115 status = ice_clear_pf_cfg(hw);
1116 if (status)
1117 goto err_unroll_cqinit;
1118
1119 /* Set bit to enable Flow Director filters */
1120 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
1121 INIT_LIST_HEAD(&hw->fdir_list_head);
1122
1123 ice_clear_pxe_mode(hw);
1124
1125 status = ice_init_nvm(hw);
1126 if (status)
1127 goto err_unroll_cqinit;
1128
1129 status = ice_get_caps(hw);
1130 if (status)
1131 goto err_unroll_cqinit;
1132
1133 if (!hw->port_info)
1134 hw->port_info = devm_kzalloc(ice_hw_to_dev(hw),
1135 sizeof(*hw->port_info),
1136 GFP_KERNEL);
1137 if (!hw->port_info) {
1138 status = -ENOMEM;
1139 goto err_unroll_cqinit;
1140 }
1141
1142 /* set the back pointer to HW */
1143 hw->port_info->hw = hw;
1144
1145 /* Initialize port_info struct with switch configuration data */
1146 status = ice_get_initial_sw_cfg(hw);
1147 if (status)
1148 goto err_unroll_alloc;
1149
1150 hw->evb_veb = true;
1151
1152 /* init xarray for identifying scheduling nodes uniquely */
1153 xa_init_flags(&hw->port_info->sched_node_ids, XA_FLAGS_ALLOC);
1154
1155 /* Query the allocated resources for Tx scheduler */
1156 status = ice_sched_query_res_alloc(hw);
1157 if (status) {
1158 ice_debug(hw, ICE_DBG_SCHED, "Failed to get scheduler allocated resources\n");
1159 goto err_unroll_alloc;
1160 }
1161 ice_sched_get_psm_clk_freq(hw);
1162
1163 /* Initialize port_info struct with scheduler data */
1164 status = ice_sched_init_port(hw->port_info);
1165 if (status)
1166 goto err_unroll_sched;
1167
1168 pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps), GFP_KERNEL);
1169 if (!pcaps) {
1170 status = -ENOMEM;
1171 goto err_unroll_sched;
1172 }
1173
1174 /* Initialize port_info struct with PHY capabilities */
1175 status = ice_aq_get_phy_caps(hw->port_info, false,
1176 ICE_AQC_REPORT_TOPO_CAP_MEDIA, pcaps,
1177 NULL);
1178 devm_kfree(ice_hw_to_dev(hw), pcaps);
1179 if (status)
1180 dev_warn(ice_hw_to_dev(hw), "Get PHY capabilities failed status = %d, continuing anyway\n",
1181 status);
1182
1183 /* Initialize port_info struct with link information */
1184 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
1185 if (status)
1186 goto err_unroll_sched;
1187
1188 /* need a valid SW entry point to build a Tx tree */
1189 if (!hw->sw_entry_point_layer) {
1190 ice_debug(hw, ICE_DBG_SCHED, "invalid sw entry point\n");
1191 status = -EIO;
1192 goto err_unroll_sched;
1193 }
1194 INIT_LIST_HEAD(&hw->agg_list);
1195 /* Initialize max burst size */
1196 if (!hw->max_burst_size)
1197 ice_cfg_rl_burst_size(hw, ICE_SCHED_DFLT_BURST_SIZE);
1198
1199 status = ice_init_fltr_mgmt_struct(hw);
1200 if (status)
1201 goto err_unroll_sched;
1202
1203 /* Get MAC information */
1204 /* A single port can report up to two (LAN and WoL) addresses */
1205 mac_buf = devm_kcalloc(ice_hw_to_dev(hw), 2,
1206 sizeof(struct ice_aqc_manage_mac_read_resp),
1207 GFP_KERNEL);
1208 mac_buf_len = 2 * sizeof(struct ice_aqc_manage_mac_read_resp);
1209
1210 if (!mac_buf) {
1211 status = -ENOMEM;
1212 goto err_unroll_fltr_mgmt_struct;
1213 }
1214
1215 status = ice_aq_manage_mac_read(hw, mac_buf, mac_buf_len, NULL);
1216 devm_kfree(ice_hw_to_dev(hw), mac_buf);
1217
1218 if (status)
1219 goto err_unroll_fltr_mgmt_struct;
1220 /* enable jumbo frame support at MAC level */
1221 status = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
1222 if (status)
1223 goto err_unroll_fltr_mgmt_struct;
1224 /* Obtain counter base index which would be used by flow director */
1225 status = ice_alloc_fd_res_cntr(hw, &hw->fd_ctr_base);
1226 if (status)
1227 goto err_unroll_fltr_mgmt_struct;
1228 status = ice_init_hw_tbls(hw);
1229 if (status)
1230 goto err_unroll_fltr_mgmt_struct;
1231 mutex_init(&hw->tnl_lock);
1232 return 0;
1233
1234 err_unroll_fltr_mgmt_struct:
1235 ice_cleanup_fltr_mgmt_struct(hw);
1236 err_unroll_sched:
1237 ice_sched_cleanup_all(hw);
1238 err_unroll_alloc:
1239 devm_kfree(ice_hw_to_dev(hw), hw->port_info);
1240 err_unroll_cqinit:
1241 ice_destroy_all_ctrlq(hw);
1242 return status;
1243 }
1244
1245 /**
1246 * ice_deinit_hw - unroll initialization operations done by ice_init_hw
1247 * @hw: pointer to the hardware structure
1248 *
1249 * This should be called only during nominal operation, not as a result of
1250 * ice_init_hw() failing since ice_init_hw() will take care of unrolling
1251 * applicable initializations if it fails for any reason.
1252 */
ice_deinit_hw(struct ice_hw * hw)1253 void ice_deinit_hw(struct ice_hw *hw)
1254 {
1255 ice_free_fd_res_cntr(hw, hw->fd_ctr_base);
1256 ice_cleanup_fltr_mgmt_struct(hw);
1257
1258 ice_sched_cleanup_all(hw);
1259 ice_sched_clear_agg(hw);
1260 ice_free_seg(hw);
1261 ice_free_hw_tbls(hw);
1262 mutex_destroy(&hw->tnl_lock);
1263
1264 /* Attempt to disable FW logging before shutting down control queues */
1265 ice_cfg_fw_log(hw, false);
1266 ice_destroy_all_ctrlq(hw);
1267
1268 /* Clear VSI contexts if not already cleared */
1269 ice_clear_all_vsi_ctx(hw);
1270 }
1271
1272 /**
1273 * ice_check_reset - Check to see if a global reset is complete
1274 * @hw: pointer to the hardware structure
1275 */
ice_check_reset(struct ice_hw * hw)1276 int ice_check_reset(struct ice_hw *hw)
1277 {
1278 u32 cnt, reg = 0, grst_timeout, uld_mask;
1279
1280 /* Poll for Device Active state in case a recent CORER, GLOBR,
1281 * or EMPR has occurred. The grst delay value is in 100ms units.
1282 * Add 1sec for outstanding AQ commands that can take a long time.
1283 */
1284 grst_timeout = ((rd32(hw, GLGEN_RSTCTL) & GLGEN_RSTCTL_GRSTDEL_M) >>
1285 GLGEN_RSTCTL_GRSTDEL_S) + 10;
1286
1287 for (cnt = 0; cnt < grst_timeout; cnt++) {
1288 mdelay(100);
1289 reg = rd32(hw, GLGEN_RSTAT);
1290 if (!(reg & GLGEN_RSTAT_DEVSTATE_M))
1291 break;
1292 }
1293
1294 if (cnt == grst_timeout) {
1295 ice_debug(hw, ICE_DBG_INIT, "Global reset polling failed to complete.\n");
1296 return -EIO;
1297 }
1298
1299 #define ICE_RESET_DONE_MASK (GLNVM_ULD_PCIER_DONE_M |\
1300 GLNVM_ULD_PCIER_DONE_1_M |\
1301 GLNVM_ULD_CORER_DONE_M |\
1302 GLNVM_ULD_GLOBR_DONE_M |\
1303 GLNVM_ULD_POR_DONE_M |\
1304 GLNVM_ULD_POR_DONE_1_M |\
1305 GLNVM_ULD_PCIER_DONE_2_M)
1306
1307 uld_mask = ICE_RESET_DONE_MASK | (hw->func_caps.common_cap.rdma ?
1308 GLNVM_ULD_PE_DONE_M : 0);
1309
1310 /* Device is Active; check Global Reset processes are done */
1311 for (cnt = 0; cnt < ICE_PF_RESET_WAIT_COUNT; cnt++) {
1312 reg = rd32(hw, GLNVM_ULD) & uld_mask;
1313 if (reg == uld_mask) {
1314 ice_debug(hw, ICE_DBG_INIT, "Global reset processes done. %d\n", cnt);
1315 break;
1316 }
1317 mdelay(10);
1318 }
1319
1320 if (cnt == ICE_PF_RESET_WAIT_COUNT) {
1321 ice_debug(hw, ICE_DBG_INIT, "Wait for Reset Done timed out. GLNVM_ULD = 0x%x\n",
1322 reg);
1323 return -EIO;
1324 }
1325
1326 return 0;
1327 }
1328
1329 /**
1330 * ice_pf_reset - Reset the PF
1331 * @hw: pointer to the hardware structure
1332 *
1333 * If a global reset has been triggered, this function checks
1334 * for its completion and then issues the PF reset
1335 */
ice_pf_reset(struct ice_hw * hw)1336 static int ice_pf_reset(struct ice_hw *hw)
1337 {
1338 u32 cnt, reg;
1339
1340 /* If at function entry a global reset was already in progress, i.e.
1341 * state is not 'device active' or any of the reset done bits are not
1342 * set in GLNVM_ULD, there is no need for a PF Reset; poll until the
1343 * global reset is done.
1344 */
1345 if ((rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_DEVSTATE_M) ||
1346 (rd32(hw, GLNVM_ULD) & ICE_RESET_DONE_MASK) ^ ICE_RESET_DONE_MASK) {
1347 /* poll on global reset currently in progress until done */
1348 if (ice_check_reset(hw))
1349 return -EIO;
1350
1351 return 0;
1352 }
1353
1354 /* Reset the PF */
1355 reg = rd32(hw, PFGEN_CTRL);
1356
1357 wr32(hw, PFGEN_CTRL, (reg | PFGEN_CTRL_PFSWR_M));
1358
1359 /* Wait for the PFR to complete. The wait time is the global config lock
1360 * timeout plus the PFR timeout which will account for a possible reset
1361 * that is occurring during a download package operation.
1362 */
1363 for (cnt = 0; cnt < ICE_GLOBAL_CFG_LOCK_TIMEOUT +
1364 ICE_PF_RESET_WAIT_COUNT; cnt++) {
1365 reg = rd32(hw, PFGEN_CTRL);
1366 if (!(reg & PFGEN_CTRL_PFSWR_M))
1367 break;
1368
1369 mdelay(1);
1370 }
1371
1372 if (cnt == ICE_PF_RESET_WAIT_COUNT) {
1373 ice_debug(hw, ICE_DBG_INIT, "PF reset polling failed to complete.\n");
1374 return -EIO;
1375 }
1376
1377 return 0;
1378 }
1379
1380 /**
1381 * ice_reset - Perform different types of reset
1382 * @hw: pointer to the hardware structure
1383 * @req: reset request
1384 *
1385 * This function triggers a reset as specified by the req parameter.
1386 *
1387 * Note:
1388 * If anything other than a PF reset is triggered, PXE mode is restored.
1389 * This has to be cleared using ice_clear_pxe_mode again, once the AQ
1390 * interface has been restored in the rebuild flow.
1391 */
ice_reset(struct ice_hw * hw,enum ice_reset_req req)1392 int ice_reset(struct ice_hw *hw, enum ice_reset_req req)
1393 {
1394 u32 val = 0;
1395
1396 switch (req) {
1397 case ICE_RESET_PFR:
1398 return ice_pf_reset(hw);
1399 case ICE_RESET_CORER:
1400 ice_debug(hw, ICE_DBG_INIT, "CoreR requested\n");
1401 val = GLGEN_RTRIG_CORER_M;
1402 break;
1403 case ICE_RESET_GLOBR:
1404 ice_debug(hw, ICE_DBG_INIT, "GlobalR requested\n");
1405 val = GLGEN_RTRIG_GLOBR_M;
1406 break;
1407 default:
1408 return -EINVAL;
1409 }
1410
1411 val |= rd32(hw, GLGEN_RTRIG);
1412 wr32(hw, GLGEN_RTRIG, val);
1413 ice_flush(hw);
1414
1415 /* wait for the FW to be ready */
1416 return ice_check_reset(hw);
1417 }
1418
1419 /**
1420 * ice_copy_rxq_ctx_to_hw
1421 * @hw: pointer to the hardware structure
1422 * @ice_rxq_ctx: pointer to the rxq context
1423 * @rxq_index: the index of the Rx queue
1424 *
1425 * Copies rxq context from dense structure to HW register space
1426 */
1427 static int
ice_copy_rxq_ctx_to_hw(struct ice_hw * hw,u8 * ice_rxq_ctx,u32 rxq_index)1428 ice_copy_rxq_ctx_to_hw(struct ice_hw *hw, u8 *ice_rxq_ctx, u32 rxq_index)
1429 {
1430 u8 i;
1431
1432 if (!ice_rxq_ctx)
1433 return -EINVAL;
1434
1435 if (rxq_index > QRX_CTRL_MAX_INDEX)
1436 return -EINVAL;
1437
1438 /* Copy each dword separately to HW */
1439 for (i = 0; i < ICE_RXQ_CTX_SIZE_DWORDS; i++) {
1440 wr32(hw, QRX_CONTEXT(i, rxq_index),
1441 *((u32 *)(ice_rxq_ctx + (i * sizeof(u32)))));
1442
1443 ice_debug(hw, ICE_DBG_QCTX, "qrxdata[%d]: %08X\n", i,
1444 *((u32 *)(ice_rxq_ctx + (i * sizeof(u32)))));
1445 }
1446
1447 return 0;
1448 }
1449
1450 /* LAN Rx Queue Context */
1451 static const struct ice_ctx_ele ice_rlan_ctx_info[] = {
1452 /* Field Width LSB */
1453 ICE_CTX_STORE(ice_rlan_ctx, head, 13, 0),
1454 ICE_CTX_STORE(ice_rlan_ctx, cpuid, 8, 13),
1455 ICE_CTX_STORE(ice_rlan_ctx, base, 57, 32),
1456 ICE_CTX_STORE(ice_rlan_ctx, qlen, 13, 89),
1457 ICE_CTX_STORE(ice_rlan_ctx, dbuf, 7, 102),
1458 ICE_CTX_STORE(ice_rlan_ctx, hbuf, 5, 109),
1459 ICE_CTX_STORE(ice_rlan_ctx, dtype, 2, 114),
1460 ICE_CTX_STORE(ice_rlan_ctx, dsize, 1, 116),
1461 ICE_CTX_STORE(ice_rlan_ctx, crcstrip, 1, 117),
1462 ICE_CTX_STORE(ice_rlan_ctx, l2tsel, 1, 119),
1463 ICE_CTX_STORE(ice_rlan_ctx, hsplit_0, 4, 120),
1464 ICE_CTX_STORE(ice_rlan_ctx, hsplit_1, 2, 124),
1465 ICE_CTX_STORE(ice_rlan_ctx, showiv, 1, 127),
1466 ICE_CTX_STORE(ice_rlan_ctx, rxmax, 14, 174),
1467 ICE_CTX_STORE(ice_rlan_ctx, tphrdesc_ena, 1, 193),
1468 ICE_CTX_STORE(ice_rlan_ctx, tphwdesc_ena, 1, 194),
1469 ICE_CTX_STORE(ice_rlan_ctx, tphdata_ena, 1, 195),
1470 ICE_CTX_STORE(ice_rlan_ctx, tphhead_ena, 1, 196),
1471 ICE_CTX_STORE(ice_rlan_ctx, lrxqthresh, 3, 198),
1472 ICE_CTX_STORE(ice_rlan_ctx, prefena, 1, 201),
1473 { 0 }
1474 };
1475
1476 /**
1477 * ice_write_rxq_ctx
1478 * @hw: pointer to the hardware structure
1479 * @rlan_ctx: pointer to the rxq context
1480 * @rxq_index: the index of the Rx queue
1481 *
1482 * Converts rxq context from sparse to dense structure and then writes
1483 * it to HW register space and enables the hardware to prefetch descriptors
1484 * instead of only fetching them on demand
1485 */
1486 int
ice_write_rxq_ctx(struct ice_hw * hw,struct ice_rlan_ctx * rlan_ctx,u32 rxq_index)1487 ice_write_rxq_ctx(struct ice_hw *hw, struct ice_rlan_ctx *rlan_ctx,
1488 u32 rxq_index)
1489 {
1490 u8 ctx_buf[ICE_RXQ_CTX_SZ] = { 0 };
1491
1492 if (!rlan_ctx)
1493 return -EINVAL;
1494
1495 rlan_ctx->prefena = 1;
1496
1497 ice_set_ctx(hw, (u8 *)rlan_ctx, ctx_buf, ice_rlan_ctx_info);
1498 return ice_copy_rxq_ctx_to_hw(hw, ctx_buf, rxq_index);
1499 }
1500
1501 /* LAN Tx Queue Context */
1502 const struct ice_ctx_ele ice_tlan_ctx_info[] = {
1503 /* Field Width LSB */
1504 ICE_CTX_STORE(ice_tlan_ctx, base, 57, 0),
1505 ICE_CTX_STORE(ice_tlan_ctx, port_num, 3, 57),
1506 ICE_CTX_STORE(ice_tlan_ctx, cgd_num, 5, 60),
1507 ICE_CTX_STORE(ice_tlan_ctx, pf_num, 3, 65),
1508 ICE_CTX_STORE(ice_tlan_ctx, vmvf_num, 10, 68),
1509 ICE_CTX_STORE(ice_tlan_ctx, vmvf_type, 2, 78),
1510 ICE_CTX_STORE(ice_tlan_ctx, src_vsi, 10, 80),
1511 ICE_CTX_STORE(ice_tlan_ctx, tsyn_ena, 1, 90),
1512 ICE_CTX_STORE(ice_tlan_ctx, internal_usage_flag, 1, 91),
1513 ICE_CTX_STORE(ice_tlan_ctx, alt_vlan, 1, 92),
1514 ICE_CTX_STORE(ice_tlan_ctx, cpuid, 8, 93),
1515 ICE_CTX_STORE(ice_tlan_ctx, wb_mode, 1, 101),
1516 ICE_CTX_STORE(ice_tlan_ctx, tphrd_desc, 1, 102),
1517 ICE_CTX_STORE(ice_tlan_ctx, tphrd, 1, 103),
1518 ICE_CTX_STORE(ice_tlan_ctx, tphwr_desc, 1, 104),
1519 ICE_CTX_STORE(ice_tlan_ctx, cmpq_id, 9, 105),
1520 ICE_CTX_STORE(ice_tlan_ctx, qnum_in_func, 14, 114),
1521 ICE_CTX_STORE(ice_tlan_ctx, itr_notification_mode, 1, 128),
1522 ICE_CTX_STORE(ice_tlan_ctx, adjust_prof_id, 6, 129),
1523 ICE_CTX_STORE(ice_tlan_ctx, qlen, 13, 135),
1524 ICE_CTX_STORE(ice_tlan_ctx, quanta_prof_idx, 4, 148),
1525 ICE_CTX_STORE(ice_tlan_ctx, tso_ena, 1, 152),
1526 ICE_CTX_STORE(ice_tlan_ctx, tso_qnum, 11, 153),
1527 ICE_CTX_STORE(ice_tlan_ctx, legacy_int, 1, 164),
1528 ICE_CTX_STORE(ice_tlan_ctx, drop_ena, 1, 165),
1529 ICE_CTX_STORE(ice_tlan_ctx, cache_prof_idx, 2, 166),
1530 ICE_CTX_STORE(ice_tlan_ctx, pkt_shaper_prof_idx, 3, 168),
1531 ICE_CTX_STORE(ice_tlan_ctx, int_q_state, 122, 171),
1532 { 0 }
1533 };
1534
1535 /* Sideband Queue command wrappers */
1536
1537 /**
1538 * ice_sbq_send_cmd - send Sideband Queue command to Sideband Queue
1539 * @hw: pointer to the HW struct
1540 * @desc: descriptor describing the command
1541 * @buf: buffer to use for indirect commands (NULL for direct commands)
1542 * @buf_size: size of buffer for indirect commands (0 for direct commands)
1543 * @cd: pointer to command details structure
1544 */
1545 static int
ice_sbq_send_cmd(struct ice_hw * hw,struct ice_sbq_cmd_desc * desc,void * buf,u16 buf_size,struct ice_sq_cd * cd)1546 ice_sbq_send_cmd(struct ice_hw *hw, struct ice_sbq_cmd_desc *desc,
1547 void *buf, u16 buf_size, struct ice_sq_cd *cd)
1548 {
1549 return ice_sq_send_cmd(hw, ice_get_sbq(hw),
1550 (struct ice_aq_desc *)desc, buf, buf_size, cd);
1551 }
1552
1553 /**
1554 * ice_sbq_rw_reg - Fill Sideband Queue command
1555 * @hw: pointer to the HW struct
1556 * @in: message info to be filled in descriptor
1557 */
ice_sbq_rw_reg(struct ice_hw * hw,struct ice_sbq_msg_input * in)1558 int ice_sbq_rw_reg(struct ice_hw *hw, struct ice_sbq_msg_input *in)
1559 {
1560 struct ice_sbq_cmd_desc desc = {0};
1561 struct ice_sbq_msg_req msg = {0};
1562 u16 msg_len;
1563 int status;
1564
1565 msg_len = sizeof(msg);
1566
1567 msg.dest_dev = in->dest_dev;
1568 msg.opcode = in->opcode;
1569 msg.flags = ICE_SBQ_MSG_FLAGS;
1570 msg.sbe_fbe = ICE_SBQ_MSG_SBE_FBE;
1571 msg.msg_addr_low = cpu_to_le16(in->msg_addr_low);
1572 msg.msg_addr_high = cpu_to_le32(in->msg_addr_high);
1573
1574 if (in->opcode)
1575 msg.data = cpu_to_le32(in->data);
1576 else
1577 /* data read comes back in completion, so shorten the struct by
1578 * sizeof(msg.data)
1579 */
1580 msg_len -= sizeof(msg.data);
1581
1582 desc.flags = cpu_to_le16(ICE_AQ_FLAG_RD);
1583 desc.opcode = cpu_to_le16(ice_sbq_opc_neigh_dev_req);
1584 desc.param0.cmd_len = cpu_to_le16(msg_len);
1585 status = ice_sbq_send_cmd(hw, &desc, &msg, msg_len, NULL);
1586 if (!status && !in->opcode)
1587 in->data = le32_to_cpu
1588 (((struct ice_sbq_msg_cmpl *)&msg)->data);
1589 return status;
1590 }
1591
1592 /* FW Admin Queue command wrappers */
1593
1594 /* Software lock/mutex that is meant to be held while the Global Config Lock
1595 * in firmware is acquired by the software to prevent most (but not all) types
1596 * of AQ commands from being sent to FW
1597 */
1598 DEFINE_MUTEX(ice_global_cfg_lock_sw);
1599
1600 /**
1601 * ice_should_retry_sq_send_cmd
1602 * @opcode: AQ opcode
1603 *
1604 * Decide if we should retry the send command routine for the ATQ, depending
1605 * on the opcode.
1606 */
ice_should_retry_sq_send_cmd(u16 opcode)1607 static bool ice_should_retry_sq_send_cmd(u16 opcode)
1608 {
1609 switch (opcode) {
1610 case ice_aqc_opc_get_link_topo:
1611 case ice_aqc_opc_lldp_stop:
1612 case ice_aqc_opc_lldp_start:
1613 case ice_aqc_opc_lldp_filter_ctrl:
1614 return true;
1615 }
1616
1617 return false;
1618 }
1619
1620 /**
1621 * ice_sq_send_cmd_retry - send command to Control Queue (ATQ)
1622 * @hw: pointer to the HW struct
1623 * @cq: pointer to the specific Control queue
1624 * @desc: prefilled descriptor describing the command
1625 * @buf: buffer to use for indirect commands (or NULL for direct commands)
1626 * @buf_size: size of buffer for indirect commands (or 0 for direct commands)
1627 * @cd: pointer to command details structure
1628 *
1629 * Retry sending the FW Admin Queue command, multiple times, to the FW Admin
1630 * Queue if the EBUSY AQ error is returned.
1631 */
1632 static int
ice_sq_send_cmd_retry(struct ice_hw * hw,struct ice_ctl_q_info * cq,struct ice_aq_desc * desc,void * buf,u16 buf_size,struct ice_sq_cd * cd)1633 ice_sq_send_cmd_retry(struct ice_hw *hw, struct ice_ctl_q_info *cq,
1634 struct ice_aq_desc *desc, void *buf, u16 buf_size,
1635 struct ice_sq_cd *cd)
1636 {
1637 struct ice_aq_desc desc_cpy;
1638 bool is_cmd_for_retry;
1639 u8 idx = 0;
1640 u16 opcode;
1641 int status;
1642
1643 opcode = le16_to_cpu(desc->opcode);
1644 is_cmd_for_retry = ice_should_retry_sq_send_cmd(opcode);
1645 memset(&desc_cpy, 0, sizeof(desc_cpy));
1646
1647 if (is_cmd_for_retry) {
1648 /* All retryable cmds are direct, without buf. */
1649 WARN_ON(buf);
1650
1651 memcpy(&desc_cpy, desc, sizeof(desc_cpy));
1652 }
1653
1654 do {
1655 status = ice_sq_send_cmd(hw, cq, desc, buf, buf_size, cd);
1656
1657 if (!is_cmd_for_retry || !status ||
1658 hw->adminq.sq_last_status != ICE_AQ_RC_EBUSY)
1659 break;
1660
1661 memcpy(desc, &desc_cpy, sizeof(desc_cpy));
1662
1663 msleep(ICE_SQ_SEND_DELAY_TIME_MS);
1664
1665 } while (++idx < ICE_SQ_SEND_MAX_EXECUTE);
1666
1667 return status;
1668 }
1669
1670 /**
1671 * ice_aq_send_cmd - send FW Admin Queue command to FW Admin Queue
1672 * @hw: pointer to the HW struct
1673 * @desc: descriptor describing the command
1674 * @buf: buffer to use for indirect commands (NULL for direct commands)
1675 * @buf_size: size of buffer for indirect commands (0 for direct commands)
1676 * @cd: pointer to command details structure
1677 *
1678 * Helper function to send FW Admin Queue commands to the FW Admin Queue.
1679 */
1680 int
ice_aq_send_cmd(struct ice_hw * hw,struct ice_aq_desc * desc,void * buf,u16 buf_size,struct ice_sq_cd * cd)1681 ice_aq_send_cmd(struct ice_hw *hw, struct ice_aq_desc *desc, void *buf,
1682 u16 buf_size, struct ice_sq_cd *cd)
1683 {
1684 struct ice_aqc_req_res *cmd = &desc->params.res_owner;
1685 bool lock_acquired = false;
1686 int status;
1687
1688 /* When a package download is in process (i.e. when the firmware's
1689 * Global Configuration Lock resource is held), only the Download
1690 * Package, Get Version, Get Package Info List, Upload Section,
1691 * Update Package, Set Port Parameters, Get/Set VLAN Mode Parameters,
1692 * Add Recipe, Set Recipes to Profile Association, Get Recipe, and Get
1693 * Recipes to Profile Association, and Release Resource (with resource
1694 * ID set to Global Config Lock) AdminQ commands are allowed; all others
1695 * must block until the package download completes and the Global Config
1696 * Lock is released. See also ice_acquire_global_cfg_lock().
1697 */
1698 switch (le16_to_cpu(desc->opcode)) {
1699 case ice_aqc_opc_download_pkg:
1700 case ice_aqc_opc_get_pkg_info_list:
1701 case ice_aqc_opc_get_ver:
1702 case ice_aqc_opc_upload_section:
1703 case ice_aqc_opc_update_pkg:
1704 case ice_aqc_opc_set_port_params:
1705 case ice_aqc_opc_get_vlan_mode_parameters:
1706 case ice_aqc_opc_set_vlan_mode_parameters:
1707 case ice_aqc_opc_add_recipe:
1708 case ice_aqc_opc_recipe_to_profile:
1709 case ice_aqc_opc_get_recipe:
1710 case ice_aqc_opc_get_recipe_to_profile:
1711 break;
1712 case ice_aqc_opc_release_res:
1713 if (le16_to_cpu(cmd->res_id) == ICE_AQC_RES_ID_GLBL_LOCK)
1714 break;
1715 fallthrough;
1716 default:
1717 mutex_lock(&ice_global_cfg_lock_sw);
1718 lock_acquired = true;
1719 break;
1720 }
1721
1722 status = ice_sq_send_cmd_retry(hw, &hw->adminq, desc, buf, buf_size, cd);
1723 if (lock_acquired)
1724 mutex_unlock(&ice_global_cfg_lock_sw);
1725
1726 return status;
1727 }
1728
1729 /**
1730 * ice_aq_get_fw_ver
1731 * @hw: pointer to the HW struct
1732 * @cd: pointer to command details structure or NULL
1733 *
1734 * Get the firmware version (0x0001) from the admin queue commands
1735 */
ice_aq_get_fw_ver(struct ice_hw * hw,struct ice_sq_cd * cd)1736 int ice_aq_get_fw_ver(struct ice_hw *hw, struct ice_sq_cd *cd)
1737 {
1738 struct ice_aqc_get_ver *resp;
1739 struct ice_aq_desc desc;
1740 int status;
1741
1742 resp = &desc.params.get_ver;
1743
1744 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_ver);
1745
1746 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1747
1748 if (!status) {
1749 hw->fw_branch = resp->fw_branch;
1750 hw->fw_maj_ver = resp->fw_major;
1751 hw->fw_min_ver = resp->fw_minor;
1752 hw->fw_patch = resp->fw_patch;
1753 hw->fw_build = le32_to_cpu(resp->fw_build);
1754 hw->api_branch = resp->api_branch;
1755 hw->api_maj_ver = resp->api_major;
1756 hw->api_min_ver = resp->api_minor;
1757 hw->api_patch = resp->api_patch;
1758 }
1759
1760 return status;
1761 }
1762
1763 /**
1764 * ice_aq_send_driver_ver
1765 * @hw: pointer to the HW struct
1766 * @dv: driver's major, minor version
1767 * @cd: pointer to command details structure or NULL
1768 *
1769 * Send the driver version (0x0002) to the firmware
1770 */
1771 int
ice_aq_send_driver_ver(struct ice_hw * hw,struct ice_driver_ver * dv,struct ice_sq_cd * cd)1772 ice_aq_send_driver_ver(struct ice_hw *hw, struct ice_driver_ver *dv,
1773 struct ice_sq_cd *cd)
1774 {
1775 struct ice_aqc_driver_ver *cmd;
1776 struct ice_aq_desc desc;
1777 u16 len;
1778
1779 cmd = &desc.params.driver_ver;
1780
1781 if (!dv)
1782 return -EINVAL;
1783
1784 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_driver_ver);
1785
1786 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1787 cmd->major_ver = dv->major_ver;
1788 cmd->minor_ver = dv->minor_ver;
1789 cmd->build_ver = dv->build_ver;
1790 cmd->subbuild_ver = dv->subbuild_ver;
1791
1792 len = 0;
1793 while (len < sizeof(dv->driver_string) &&
1794 isascii(dv->driver_string[len]) && dv->driver_string[len])
1795 len++;
1796
1797 return ice_aq_send_cmd(hw, &desc, dv->driver_string, len, cd);
1798 }
1799
1800 /**
1801 * ice_aq_q_shutdown
1802 * @hw: pointer to the HW struct
1803 * @unloading: is the driver unloading itself
1804 *
1805 * Tell the Firmware that we're shutting down the AdminQ and whether
1806 * or not the driver is unloading as well (0x0003).
1807 */
ice_aq_q_shutdown(struct ice_hw * hw,bool unloading)1808 int ice_aq_q_shutdown(struct ice_hw *hw, bool unloading)
1809 {
1810 struct ice_aqc_q_shutdown *cmd;
1811 struct ice_aq_desc desc;
1812
1813 cmd = &desc.params.q_shutdown;
1814
1815 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_q_shutdown);
1816
1817 if (unloading)
1818 cmd->driver_unloading = ICE_AQC_DRIVER_UNLOADING;
1819
1820 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1821 }
1822
1823 /**
1824 * ice_aq_req_res
1825 * @hw: pointer to the HW struct
1826 * @res: resource ID
1827 * @access: access type
1828 * @sdp_number: resource number
1829 * @timeout: the maximum time in ms that the driver may hold the resource
1830 * @cd: pointer to command details structure or NULL
1831 *
1832 * Requests common resource using the admin queue commands (0x0008).
1833 * When attempting to acquire the Global Config Lock, the driver can
1834 * learn of three states:
1835 * 1) 0 - acquired lock, and can perform download package
1836 * 2) -EIO - did not get lock, driver should fail to load
1837 * 3) -EALREADY - did not get lock, but another driver has
1838 * successfully downloaded the package; the driver does
1839 * not have to download the package and can continue
1840 * loading
1841 *
1842 * Note that if the caller is in an acquire lock, perform action, release lock
1843 * phase of operation, it is possible that the FW may detect a timeout and issue
1844 * a CORER. In this case, the driver will receive a CORER interrupt and will
1845 * have to determine its cause. The calling thread that is handling this flow
1846 * will likely get an error propagated back to it indicating the Download
1847 * Package, Update Package or the Release Resource AQ commands timed out.
1848 */
1849 static int
ice_aq_req_res(struct ice_hw * hw,enum ice_aq_res_ids res,enum ice_aq_res_access_type access,u8 sdp_number,u32 * timeout,struct ice_sq_cd * cd)1850 ice_aq_req_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1851 enum ice_aq_res_access_type access, u8 sdp_number, u32 *timeout,
1852 struct ice_sq_cd *cd)
1853 {
1854 struct ice_aqc_req_res *cmd_resp;
1855 struct ice_aq_desc desc;
1856 int status;
1857
1858 cmd_resp = &desc.params.res_owner;
1859
1860 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_req_res);
1861
1862 cmd_resp->res_id = cpu_to_le16(res);
1863 cmd_resp->access_type = cpu_to_le16(access);
1864 cmd_resp->res_number = cpu_to_le32(sdp_number);
1865 cmd_resp->timeout = cpu_to_le32(*timeout);
1866 *timeout = 0;
1867
1868 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1869
1870 /* The completion specifies the maximum time in ms that the driver
1871 * may hold the resource in the Timeout field.
1872 */
1873
1874 /* Global config lock response utilizes an additional status field.
1875 *
1876 * If the Global config lock resource is held by some other driver, the
1877 * command completes with ICE_AQ_RES_GLBL_IN_PROG in the status field
1878 * and the timeout field indicates the maximum time the current owner
1879 * of the resource has to free it.
1880 */
1881 if (res == ICE_GLOBAL_CFG_LOCK_RES_ID) {
1882 if (le16_to_cpu(cmd_resp->status) == ICE_AQ_RES_GLBL_SUCCESS) {
1883 *timeout = le32_to_cpu(cmd_resp->timeout);
1884 return 0;
1885 } else if (le16_to_cpu(cmd_resp->status) ==
1886 ICE_AQ_RES_GLBL_IN_PROG) {
1887 *timeout = le32_to_cpu(cmd_resp->timeout);
1888 return -EIO;
1889 } else if (le16_to_cpu(cmd_resp->status) ==
1890 ICE_AQ_RES_GLBL_DONE) {
1891 return -EALREADY;
1892 }
1893
1894 /* invalid FW response, force a timeout immediately */
1895 *timeout = 0;
1896 return -EIO;
1897 }
1898
1899 /* If the resource is held by some other driver, the command completes
1900 * with a busy return value and the timeout field indicates the maximum
1901 * time the current owner of the resource has to free it.
1902 */
1903 if (!status || hw->adminq.sq_last_status == ICE_AQ_RC_EBUSY)
1904 *timeout = le32_to_cpu(cmd_resp->timeout);
1905
1906 return status;
1907 }
1908
1909 /**
1910 * ice_aq_release_res
1911 * @hw: pointer to the HW struct
1912 * @res: resource ID
1913 * @sdp_number: resource number
1914 * @cd: pointer to command details structure or NULL
1915 *
1916 * release common resource using the admin queue commands (0x0009)
1917 */
1918 static int
ice_aq_release_res(struct ice_hw * hw,enum ice_aq_res_ids res,u8 sdp_number,struct ice_sq_cd * cd)1919 ice_aq_release_res(struct ice_hw *hw, enum ice_aq_res_ids res, u8 sdp_number,
1920 struct ice_sq_cd *cd)
1921 {
1922 struct ice_aqc_req_res *cmd;
1923 struct ice_aq_desc desc;
1924
1925 cmd = &desc.params.res_owner;
1926
1927 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_release_res);
1928
1929 cmd->res_id = cpu_to_le16(res);
1930 cmd->res_number = cpu_to_le32(sdp_number);
1931
1932 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1933 }
1934
1935 /**
1936 * ice_acquire_res
1937 * @hw: pointer to the HW structure
1938 * @res: resource ID
1939 * @access: access type (read or write)
1940 * @timeout: timeout in milliseconds
1941 *
1942 * This function will attempt to acquire the ownership of a resource.
1943 */
1944 int
ice_acquire_res(struct ice_hw * hw,enum ice_aq_res_ids res,enum ice_aq_res_access_type access,u32 timeout)1945 ice_acquire_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1946 enum ice_aq_res_access_type access, u32 timeout)
1947 {
1948 #define ICE_RES_POLLING_DELAY_MS 10
1949 u32 delay = ICE_RES_POLLING_DELAY_MS;
1950 u32 time_left = timeout;
1951 int status;
1952
1953 status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1954
1955 /* A return code of -EALREADY means that another driver has
1956 * previously acquired the resource and performed any necessary updates;
1957 * in this case the caller does not obtain the resource and has no
1958 * further work to do.
1959 */
1960 if (status == -EALREADY)
1961 goto ice_acquire_res_exit;
1962
1963 if (status)
1964 ice_debug(hw, ICE_DBG_RES, "resource %d acquire type %d failed.\n", res, access);
1965
1966 /* If necessary, poll until the current lock owner timeouts */
1967 timeout = time_left;
1968 while (status && timeout && time_left) {
1969 mdelay(delay);
1970 timeout = (timeout > delay) ? timeout - delay : 0;
1971 status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1972
1973 if (status == -EALREADY)
1974 /* lock free, but no work to do */
1975 break;
1976
1977 if (!status)
1978 /* lock acquired */
1979 break;
1980 }
1981 if (status && status != -EALREADY)
1982 ice_debug(hw, ICE_DBG_RES, "resource acquire timed out.\n");
1983
1984 ice_acquire_res_exit:
1985 if (status == -EALREADY) {
1986 if (access == ICE_RES_WRITE)
1987 ice_debug(hw, ICE_DBG_RES, "resource indicates no work to do.\n");
1988 else
1989 ice_debug(hw, ICE_DBG_RES, "Warning: -EALREADY not expected\n");
1990 }
1991 return status;
1992 }
1993
1994 /**
1995 * ice_release_res
1996 * @hw: pointer to the HW structure
1997 * @res: resource ID
1998 *
1999 * This function will release a resource using the proper Admin Command.
2000 */
ice_release_res(struct ice_hw * hw,enum ice_aq_res_ids res)2001 void ice_release_res(struct ice_hw *hw, enum ice_aq_res_ids res)
2002 {
2003 unsigned long timeout;
2004 int status;
2005
2006 /* there are some rare cases when trying to release the resource
2007 * results in an admin queue timeout, so handle them correctly
2008 */
2009 timeout = jiffies + 10 * ICE_CTL_Q_SQ_CMD_TIMEOUT;
2010 do {
2011 status = ice_aq_release_res(hw, res, 0, NULL);
2012 if (status != -EIO)
2013 break;
2014 usleep_range(1000, 2000);
2015 } while (time_before(jiffies, timeout));
2016 }
2017
2018 /**
2019 * ice_aq_alloc_free_res - command to allocate/free resources
2020 * @hw: pointer to the HW struct
2021 * @buf: Indirect buffer to hold data parameters and response
2022 * @buf_size: size of buffer for indirect commands
2023 * @opc: pass in the command opcode
2024 *
2025 * Helper function to allocate/free resources using the admin queue commands
2026 */
ice_aq_alloc_free_res(struct ice_hw * hw,struct ice_aqc_alloc_free_res_elem * buf,u16 buf_size,enum ice_adminq_opc opc)2027 int ice_aq_alloc_free_res(struct ice_hw *hw,
2028 struct ice_aqc_alloc_free_res_elem *buf, u16 buf_size,
2029 enum ice_adminq_opc opc)
2030 {
2031 struct ice_aqc_alloc_free_res_cmd *cmd;
2032 struct ice_aq_desc desc;
2033
2034 cmd = &desc.params.sw_res_ctrl;
2035
2036 if (!buf || buf_size < flex_array_size(buf, elem, 1))
2037 return -EINVAL;
2038
2039 ice_fill_dflt_direct_cmd_desc(&desc, opc);
2040
2041 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2042
2043 cmd->num_entries = cpu_to_le16(1);
2044
2045 return ice_aq_send_cmd(hw, &desc, buf, buf_size, NULL);
2046 }
2047
2048 /**
2049 * ice_alloc_hw_res - allocate resource
2050 * @hw: pointer to the HW struct
2051 * @type: type of resource
2052 * @num: number of resources to allocate
2053 * @btm: allocate from bottom
2054 * @res: pointer to array that will receive the resources
2055 */
2056 int
ice_alloc_hw_res(struct ice_hw * hw,u16 type,u16 num,bool btm,u16 * res)2057 ice_alloc_hw_res(struct ice_hw *hw, u16 type, u16 num, bool btm, u16 *res)
2058 {
2059 struct ice_aqc_alloc_free_res_elem *buf;
2060 u16 buf_len;
2061 int status;
2062
2063 buf_len = struct_size(buf, elem, num);
2064 buf = kzalloc(buf_len, GFP_KERNEL);
2065 if (!buf)
2066 return -ENOMEM;
2067
2068 /* Prepare buffer to allocate resource. */
2069 buf->num_elems = cpu_to_le16(num);
2070 buf->res_type = cpu_to_le16(type | ICE_AQC_RES_TYPE_FLAG_DEDICATED |
2071 ICE_AQC_RES_TYPE_FLAG_IGNORE_INDEX);
2072 if (btm)
2073 buf->res_type |= cpu_to_le16(ICE_AQC_RES_TYPE_FLAG_SCAN_BOTTOM);
2074
2075 status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_alloc_res);
2076 if (status)
2077 goto ice_alloc_res_exit;
2078
2079 memcpy(res, buf->elem, sizeof(*buf->elem) * num);
2080
2081 ice_alloc_res_exit:
2082 kfree(buf);
2083 return status;
2084 }
2085
2086 /**
2087 * ice_free_hw_res - free allocated HW resource
2088 * @hw: pointer to the HW struct
2089 * @type: type of resource to free
2090 * @num: number of resources
2091 * @res: pointer to array that contains the resources to free
2092 */
ice_free_hw_res(struct ice_hw * hw,u16 type,u16 num,u16 * res)2093 int ice_free_hw_res(struct ice_hw *hw, u16 type, u16 num, u16 *res)
2094 {
2095 struct ice_aqc_alloc_free_res_elem *buf;
2096 u16 buf_len;
2097 int status;
2098
2099 buf_len = struct_size(buf, elem, num);
2100 buf = kzalloc(buf_len, GFP_KERNEL);
2101 if (!buf)
2102 return -ENOMEM;
2103
2104 /* Prepare buffer to free resource. */
2105 buf->num_elems = cpu_to_le16(num);
2106 buf->res_type = cpu_to_le16(type);
2107 memcpy(buf->elem, res, sizeof(*buf->elem) * num);
2108
2109 status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_free_res);
2110 if (status)
2111 ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
2112
2113 kfree(buf);
2114 return status;
2115 }
2116
2117 /**
2118 * ice_get_num_per_func - determine number of resources per PF
2119 * @hw: pointer to the HW structure
2120 * @max: value to be evenly split between each PF
2121 *
2122 * Determine the number of valid functions by going through the bitmap returned
2123 * from parsing capabilities and use this to calculate the number of resources
2124 * per PF based on the max value passed in.
2125 */
ice_get_num_per_func(struct ice_hw * hw,u32 max)2126 static u32 ice_get_num_per_func(struct ice_hw *hw, u32 max)
2127 {
2128 u8 funcs;
2129
2130 #define ICE_CAPS_VALID_FUNCS_M 0xFF
2131 funcs = hweight8(hw->dev_caps.common_cap.valid_functions &
2132 ICE_CAPS_VALID_FUNCS_M);
2133
2134 if (!funcs)
2135 return 0;
2136
2137 return max / funcs;
2138 }
2139
2140 /**
2141 * ice_parse_common_caps - parse common device/function capabilities
2142 * @hw: pointer to the HW struct
2143 * @caps: pointer to common capabilities structure
2144 * @elem: the capability element to parse
2145 * @prefix: message prefix for tracing capabilities
2146 *
2147 * Given a capability element, extract relevant details into the common
2148 * capability structure.
2149 *
2150 * Returns: true if the capability matches one of the common capability ids,
2151 * false otherwise.
2152 */
2153 static bool
ice_parse_common_caps(struct ice_hw * hw,struct ice_hw_common_caps * caps,struct ice_aqc_list_caps_elem * elem,const char * prefix)2154 ice_parse_common_caps(struct ice_hw *hw, struct ice_hw_common_caps *caps,
2155 struct ice_aqc_list_caps_elem *elem, const char *prefix)
2156 {
2157 u32 logical_id = le32_to_cpu(elem->logical_id);
2158 u32 phys_id = le32_to_cpu(elem->phys_id);
2159 u32 number = le32_to_cpu(elem->number);
2160 u16 cap = le16_to_cpu(elem->cap);
2161 bool found = true;
2162
2163 switch (cap) {
2164 case ICE_AQC_CAPS_VALID_FUNCTIONS:
2165 caps->valid_functions = number;
2166 ice_debug(hw, ICE_DBG_INIT, "%s: valid_functions (bitmap) = %d\n", prefix,
2167 caps->valid_functions);
2168 break;
2169 case ICE_AQC_CAPS_SRIOV:
2170 caps->sr_iov_1_1 = (number == 1);
2171 ice_debug(hw, ICE_DBG_INIT, "%s: sr_iov_1_1 = %d\n", prefix,
2172 caps->sr_iov_1_1);
2173 break;
2174 case ICE_AQC_CAPS_DCB:
2175 caps->dcb = (number == 1);
2176 caps->active_tc_bitmap = logical_id;
2177 caps->maxtc = phys_id;
2178 ice_debug(hw, ICE_DBG_INIT, "%s: dcb = %d\n", prefix, caps->dcb);
2179 ice_debug(hw, ICE_DBG_INIT, "%s: active_tc_bitmap = %d\n", prefix,
2180 caps->active_tc_bitmap);
2181 ice_debug(hw, ICE_DBG_INIT, "%s: maxtc = %d\n", prefix, caps->maxtc);
2182 break;
2183 case ICE_AQC_CAPS_RSS:
2184 caps->rss_table_size = number;
2185 caps->rss_table_entry_width = logical_id;
2186 ice_debug(hw, ICE_DBG_INIT, "%s: rss_table_size = %d\n", prefix,
2187 caps->rss_table_size);
2188 ice_debug(hw, ICE_DBG_INIT, "%s: rss_table_entry_width = %d\n", prefix,
2189 caps->rss_table_entry_width);
2190 break;
2191 case ICE_AQC_CAPS_RXQS:
2192 caps->num_rxq = number;
2193 caps->rxq_first_id = phys_id;
2194 ice_debug(hw, ICE_DBG_INIT, "%s: num_rxq = %d\n", prefix,
2195 caps->num_rxq);
2196 ice_debug(hw, ICE_DBG_INIT, "%s: rxq_first_id = %d\n", prefix,
2197 caps->rxq_first_id);
2198 break;
2199 case ICE_AQC_CAPS_TXQS:
2200 caps->num_txq = number;
2201 caps->txq_first_id = phys_id;
2202 ice_debug(hw, ICE_DBG_INIT, "%s: num_txq = %d\n", prefix,
2203 caps->num_txq);
2204 ice_debug(hw, ICE_DBG_INIT, "%s: txq_first_id = %d\n", prefix,
2205 caps->txq_first_id);
2206 break;
2207 case ICE_AQC_CAPS_MSIX:
2208 caps->num_msix_vectors = number;
2209 caps->msix_vector_first_id = phys_id;
2210 ice_debug(hw, ICE_DBG_INIT, "%s: num_msix_vectors = %d\n", prefix,
2211 caps->num_msix_vectors);
2212 ice_debug(hw, ICE_DBG_INIT, "%s: msix_vector_first_id = %d\n", prefix,
2213 caps->msix_vector_first_id);
2214 break;
2215 case ICE_AQC_CAPS_PENDING_NVM_VER:
2216 caps->nvm_update_pending_nvm = true;
2217 ice_debug(hw, ICE_DBG_INIT, "%s: update_pending_nvm\n", prefix);
2218 break;
2219 case ICE_AQC_CAPS_PENDING_OROM_VER:
2220 caps->nvm_update_pending_orom = true;
2221 ice_debug(hw, ICE_DBG_INIT, "%s: update_pending_orom\n", prefix);
2222 break;
2223 case ICE_AQC_CAPS_PENDING_NET_VER:
2224 caps->nvm_update_pending_netlist = true;
2225 ice_debug(hw, ICE_DBG_INIT, "%s: update_pending_netlist\n", prefix);
2226 break;
2227 case ICE_AQC_CAPS_NVM_MGMT:
2228 caps->nvm_unified_update =
2229 (number & ICE_NVM_MGMT_UNIFIED_UPD_SUPPORT) ?
2230 true : false;
2231 ice_debug(hw, ICE_DBG_INIT, "%s: nvm_unified_update = %d\n", prefix,
2232 caps->nvm_unified_update);
2233 break;
2234 case ICE_AQC_CAPS_RDMA:
2235 caps->rdma = (number == 1);
2236 ice_debug(hw, ICE_DBG_INIT, "%s: rdma = %d\n", prefix, caps->rdma);
2237 break;
2238 case ICE_AQC_CAPS_MAX_MTU:
2239 caps->max_mtu = number;
2240 ice_debug(hw, ICE_DBG_INIT, "%s: max_mtu = %d\n",
2241 prefix, caps->max_mtu);
2242 break;
2243 case ICE_AQC_CAPS_PCIE_RESET_AVOIDANCE:
2244 caps->pcie_reset_avoidance = (number > 0);
2245 ice_debug(hw, ICE_DBG_INIT,
2246 "%s: pcie_reset_avoidance = %d\n", prefix,
2247 caps->pcie_reset_avoidance);
2248 break;
2249 case ICE_AQC_CAPS_POST_UPDATE_RESET_RESTRICT:
2250 caps->reset_restrict_support = (number == 1);
2251 ice_debug(hw, ICE_DBG_INIT,
2252 "%s: reset_restrict_support = %d\n", prefix,
2253 caps->reset_restrict_support);
2254 break;
2255 case ICE_AQC_CAPS_FW_LAG_SUPPORT:
2256 caps->roce_lag = !!(number & ICE_AQC_BIT_ROCEV2_LAG);
2257 ice_debug(hw, ICE_DBG_INIT, "%s: roce_lag = %u\n",
2258 prefix, caps->roce_lag);
2259 caps->sriov_lag = !!(number & ICE_AQC_BIT_SRIOV_LAG);
2260 ice_debug(hw, ICE_DBG_INIT, "%s: sriov_lag = %u\n",
2261 prefix, caps->sriov_lag);
2262 break;
2263 default:
2264 /* Not one of the recognized common capabilities */
2265 found = false;
2266 }
2267
2268 return found;
2269 }
2270
2271 /**
2272 * ice_recalc_port_limited_caps - Recalculate port limited capabilities
2273 * @hw: pointer to the HW structure
2274 * @caps: pointer to capabilities structure to fix
2275 *
2276 * Re-calculate the capabilities that are dependent on the number of physical
2277 * ports; i.e. some features are not supported or function differently on
2278 * devices with more than 4 ports.
2279 */
2280 static void
ice_recalc_port_limited_caps(struct ice_hw * hw,struct ice_hw_common_caps * caps)2281 ice_recalc_port_limited_caps(struct ice_hw *hw, struct ice_hw_common_caps *caps)
2282 {
2283 /* This assumes device capabilities are always scanned before function
2284 * capabilities during the initialization flow.
2285 */
2286 if (hw->dev_caps.num_funcs > 4) {
2287 /* Max 4 TCs per port */
2288 caps->maxtc = 4;
2289 ice_debug(hw, ICE_DBG_INIT, "reducing maxtc to %d (based on #ports)\n",
2290 caps->maxtc);
2291 if (caps->rdma) {
2292 ice_debug(hw, ICE_DBG_INIT, "forcing RDMA off\n");
2293 caps->rdma = 0;
2294 }
2295
2296 /* print message only when processing device capabilities
2297 * during initialization.
2298 */
2299 if (caps == &hw->dev_caps.common_cap)
2300 dev_info(ice_hw_to_dev(hw), "RDMA functionality is not available with the current device configuration.\n");
2301 }
2302 }
2303
2304 /**
2305 * ice_parse_vf_func_caps - Parse ICE_AQC_CAPS_VF function caps
2306 * @hw: pointer to the HW struct
2307 * @func_p: pointer to function capabilities structure
2308 * @cap: pointer to the capability element to parse
2309 *
2310 * Extract function capabilities for ICE_AQC_CAPS_VF.
2311 */
2312 static void
ice_parse_vf_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_p,struct ice_aqc_list_caps_elem * cap)2313 ice_parse_vf_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_p,
2314 struct ice_aqc_list_caps_elem *cap)
2315 {
2316 u32 logical_id = le32_to_cpu(cap->logical_id);
2317 u32 number = le32_to_cpu(cap->number);
2318
2319 func_p->num_allocd_vfs = number;
2320 func_p->vf_base_id = logical_id;
2321 ice_debug(hw, ICE_DBG_INIT, "func caps: num_allocd_vfs = %d\n",
2322 func_p->num_allocd_vfs);
2323 ice_debug(hw, ICE_DBG_INIT, "func caps: vf_base_id = %d\n",
2324 func_p->vf_base_id);
2325 }
2326
2327 /**
2328 * ice_parse_vsi_func_caps - Parse ICE_AQC_CAPS_VSI function caps
2329 * @hw: pointer to the HW struct
2330 * @func_p: pointer to function capabilities structure
2331 * @cap: pointer to the capability element to parse
2332 *
2333 * Extract function capabilities for ICE_AQC_CAPS_VSI.
2334 */
2335 static void
ice_parse_vsi_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_p,struct ice_aqc_list_caps_elem * cap)2336 ice_parse_vsi_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_p,
2337 struct ice_aqc_list_caps_elem *cap)
2338 {
2339 func_p->guar_num_vsi = ice_get_num_per_func(hw, ICE_MAX_VSI);
2340 ice_debug(hw, ICE_DBG_INIT, "func caps: guar_num_vsi (fw) = %d\n",
2341 le32_to_cpu(cap->number));
2342 ice_debug(hw, ICE_DBG_INIT, "func caps: guar_num_vsi = %d\n",
2343 func_p->guar_num_vsi);
2344 }
2345
2346 /**
2347 * ice_parse_1588_func_caps - Parse ICE_AQC_CAPS_1588 function caps
2348 * @hw: pointer to the HW struct
2349 * @func_p: pointer to function capabilities structure
2350 * @cap: pointer to the capability element to parse
2351 *
2352 * Extract function capabilities for ICE_AQC_CAPS_1588.
2353 */
2354 static void
ice_parse_1588_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_p,struct ice_aqc_list_caps_elem * cap)2355 ice_parse_1588_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_p,
2356 struct ice_aqc_list_caps_elem *cap)
2357 {
2358 struct ice_ts_func_info *info = &func_p->ts_func_info;
2359 u32 number = le32_to_cpu(cap->number);
2360
2361 info->ena = ((number & ICE_TS_FUNC_ENA_M) != 0);
2362 func_p->common_cap.ieee_1588 = info->ena;
2363
2364 info->src_tmr_owned = ((number & ICE_TS_SRC_TMR_OWND_M) != 0);
2365 info->tmr_ena = ((number & ICE_TS_TMR_ENA_M) != 0);
2366 info->tmr_index_owned = ((number & ICE_TS_TMR_IDX_OWND_M) != 0);
2367 info->tmr_index_assoc = ((number & ICE_TS_TMR_IDX_ASSOC_M) != 0);
2368
2369 info->clk_freq = (number & ICE_TS_CLK_FREQ_M) >> ICE_TS_CLK_FREQ_S;
2370 info->clk_src = ((number & ICE_TS_CLK_SRC_M) != 0);
2371
2372 if (info->clk_freq < NUM_ICE_TIME_REF_FREQ) {
2373 info->time_ref = (enum ice_time_ref_freq)info->clk_freq;
2374 } else {
2375 /* Unknown clock frequency, so assume a (probably incorrect)
2376 * default to avoid out-of-bounds look ups of frequency
2377 * related information.
2378 */
2379 ice_debug(hw, ICE_DBG_INIT, "1588 func caps: unknown clock frequency %u\n",
2380 info->clk_freq);
2381 info->time_ref = ICE_TIME_REF_FREQ_25_000;
2382 }
2383
2384 ice_debug(hw, ICE_DBG_INIT, "func caps: ieee_1588 = %u\n",
2385 func_p->common_cap.ieee_1588);
2386 ice_debug(hw, ICE_DBG_INIT, "func caps: src_tmr_owned = %u\n",
2387 info->src_tmr_owned);
2388 ice_debug(hw, ICE_DBG_INIT, "func caps: tmr_ena = %u\n",
2389 info->tmr_ena);
2390 ice_debug(hw, ICE_DBG_INIT, "func caps: tmr_index_owned = %u\n",
2391 info->tmr_index_owned);
2392 ice_debug(hw, ICE_DBG_INIT, "func caps: tmr_index_assoc = %u\n",
2393 info->tmr_index_assoc);
2394 ice_debug(hw, ICE_DBG_INIT, "func caps: clk_freq = %u\n",
2395 info->clk_freq);
2396 ice_debug(hw, ICE_DBG_INIT, "func caps: clk_src = %u\n",
2397 info->clk_src);
2398 }
2399
2400 /**
2401 * ice_parse_fdir_func_caps - Parse ICE_AQC_CAPS_FD function caps
2402 * @hw: pointer to the HW struct
2403 * @func_p: pointer to function capabilities structure
2404 *
2405 * Extract function capabilities for ICE_AQC_CAPS_FD.
2406 */
2407 static void
ice_parse_fdir_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_p)2408 ice_parse_fdir_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_p)
2409 {
2410 u32 reg_val, gsize, bsize;
2411
2412 reg_val = rd32(hw, GLQF_FD_SIZE);
2413 switch (hw->mac_type) {
2414 case ICE_MAC_E830:
2415 gsize = FIELD_GET(E830_GLQF_FD_SIZE_FD_GSIZE_M, reg_val);
2416 bsize = FIELD_GET(E830_GLQF_FD_SIZE_FD_BSIZE_M, reg_val);
2417 break;
2418 case ICE_MAC_E810:
2419 default:
2420 gsize = FIELD_GET(E800_GLQF_FD_SIZE_FD_GSIZE_M, reg_val);
2421 bsize = FIELD_GET(E800_GLQF_FD_SIZE_FD_BSIZE_M, reg_val);
2422 }
2423 func_p->fd_fltr_guar = ice_get_num_per_func(hw, gsize);
2424 func_p->fd_fltr_best_effort = bsize;
2425
2426 ice_debug(hw, ICE_DBG_INIT, "func caps: fd_fltr_guar = %d\n",
2427 func_p->fd_fltr_guar);
2428 ice_debug(hw, ICE_DBG_INIT, "func caps: fd_fltr_best_effort = %d\n",
2429 func_p->fd_fltr_best_effort);
2430 }
2431
2432 /**
2433 * ice_parse_func_caps - Parse function capabilities
2434 * @hw: pointer to the HW struct
2435 * @func_p: pointer to function capabilities structure
2436 * @buf: buffer containing the function capability records
2437 * @cap_count: the number of capabilities
2438 *
2439 * Helper function to parse function (0x000A) capabilities list. For
2440 * capabilities shared between device and function, this relies on
2441 * ice_parse_common_caps.
2442 *
2443 * Loop through the list of provided capabilities and extract the relevant
2444 * data into the function capabilities structured.
2445 */
2446 static void
ice_parse_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_p,void * buf,u32 cap_count)2447 ice_parse_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_p,
2448 void *buf, u32 cap_count)
2449 {
2450 struct ice_aqc_list_caps_elem *cap_resp;
2451 u32 i;
2452
2453 cap_resp = buf;
2454
2455 memset(func_p, 0, sizeof(*func_p));
2456
2457 for (i = 0; i < cap_count; i++) {
2458 u16 cap = le16_to_cpu(cap_resp[i].cap);
2459 bool found;
2460
2461 found = ice_parse_common_caps(hw, &func_p->common_cap,
2462 &cap_resp[i], "func caps");
2463
2464 switch (cap) {
2465 case ICE_AQC_CAPS_VF:
2466 ice_parse_vf_func_caps(hw, func_p, &cap_resp[i]);
2467 break;
2468 case ICE_AQC_CAPS_VSI:
2469 ice_parse_vsi_func_caps(hw, func_p, &cap_resp[i]);
2470 break;
2471 case ICE_AQC_CAPS_1588:
2472 ice_parse_1588_func_caps(hw, func_p, &cap_resp[i]);
2473 break;
2474 case ICE_AQC_CAPS_FD:
2475 ice_parse_fdir_func_caps(hw, func_p);
2476 break;
2477 default:
2478 /* Don't list common capabilities as unknown */
2479 if (!found)
2480 ice_debug(hw, ICE_DBG_INIT, "func caps: unknown capability[%d]: 0x%x\n",
2481 i, cap);
2482 break;
2483 }
2484 }
2485
2486 ice_recalc_port_limited_caps(hw, &func_p->common_cap);
2487 }
2488
2489 /**
2490 * ice_parse_valid_functions_cap - Parse ICE_AQC_CAPS_VALID_FUNCTIONS caps
2491 * @hw: pointer to the HW struct
2492 * @dev_p: pointer to device capabilities structure
2493 * @cap: capability element to parse
2494 *
2495 * Parse ICE_AQC_CAPS_VALID_FUNCTIONS for device capabilities.
2496 */
2497 static void
ice_parse_valid_functions_cap(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,struct ice_aqc_list_caps_elem * cap)2498 ice_parse_valid_functions_cap(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2499 struct ice_aqc_list_caps_elem *cap)
2500 {
2501 u32 number = le32_to_cpu(cap->number);
2502
2503 dev_p->num_funcs = hweight32(number);
2504 ice_debug(hw, ICE_DBG_INIT, "dev caps: num_funcs = %d\n",
2505 dev_p->num_funcs);
2506 }
2507
2508 /**
2509 * ice_parse_vf_dev_caps - Parse ICE_AQC_CAPS_VF device caps
2510 * @hw: pointer to the HW struct
2511 * @dev_p: pointer to device capabilities structure
2512 * @cap: capability element to parse
2513 *
2514 * Parse ICE_AQC_CAPS_VF for device capabilities.
2515 */
2516 static void
ice_parse_vf_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,struct ice_aqc_list_caps_elem * cap)2517 ice_parse_vf_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2518 struct ice_aqc_list_caps_elem *cap)
2519 {
2520 u32 number = le32_to_cpu(cap->number);
2521
2522 dev_p->num_vfs_exposed = number;
2523 ice_debug(hw, ICE_DBG_INIT, "dev_caps: num_vfs_exposed = %d\n",
2524 dev_p->num_vfs_exposed);
2525 }
2526
2527 /**
2528 * ice_parse_vsi_dev_caps - Parse ICE_AQC_CAPS_VSI device caps
2529 * @hw: pointer to the HW struct
2530 * @dev_p: pointer to device capabilities structure
2531 * @cap: capability element to parse
2532 *
2533 * Parse ICE_AQC_CAPS_VSI for device capabilities.
2534 */
2535 static void
ice_parse_vsi_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,struct ice_aqc_list_caps_elem * cap)2536 ice_parse_vsi_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2537 struct ice_aqc_list_caps_elem *cap)
2538 {
2539 u32 number = le32_to_cpu(cap->number);
2540
2541 dev_p->num_vsi_allocd_to_host = number;
2542 ice_debug(hw, ICE_DBG_INIT, "dev caps: num_vsi_allocd_to_host = %d\n",
2543 dev_p->num_vsi_allocd_to_host);
2544 }
2545
2546 /**
2547 * ice_parse_1588_dev_caps - Parse ICE_AQC_CAPS_1588 device caps
2548 * @hw: pointer to the HW struct
2549 * @dev_p: pointer to device capabilities structure
2550 * @cap: capability element to parse
2551 *
2552 * Parse ICE_AQC_CAPS_1588 for device capabilities.
2553 */
2554 static void
ice_parse_1588_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,struct ice_aqc_list_caps_elem * cap)2555 ice_parse_1588_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2556 struct ice_aqc_list_caps_elem *cap)
2557 {
2558 struct ice_ts_dev_info *info = &dev_p->ts_dev_info;
2559 u32 logical_id = le32_to_cpu(cap->logical_id);
2560 u32 phys_id = le32_to_cpu(cap->phys_id);
2561 u32 number = le32_to_cpu(cap->number);
2562
2563 info->ena = ((number & ICE_TS_DEV_ENA_M) != 0);
2564 dev_p->common_cap.ieee_1588 = info->ena;
2565
2566 info->tmr0_owner = number & ICE_TS_TMR0_OWNR_M;
2567 info->tmr0_owned = ((number & ICE_TS_TMR0_OWND_M) != 0);
2568 info->tmr0_ena = ((number & ICE_TS_TMR0_ENA_M) != 0);
2569
2570 info->tmr1_owner = (number & ICE_TS_TMR1_OWNR_M) >> ICE_TS_TMR1_OWNR_S;
2571 info->tmr1_owned = ((number & ICE_TS_TMR1_OWND_M) != 0);
2572 info->tmr1_ena = ((number & ICE_TS_TMR1_ENA_M) != 0);
2573
2574 info->ts_ll_read = ((number & ICE_TS_LL_TX_TS_READ_M) != 0);
2575
2576 info->ena_ports = logical_id;
2577 info->tmr_own_map = phys_id;
2578
2579 ice_debug(hw, ICE_DBG_INIT, "dev caps: ieee_1588 = %u\n",
2580 dev_p->common_cap.ieee_1588);
2581 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr0_owner = %u\n",
2582 info->tmr0_owner);
2583 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr0_owned = %u\n",
2584 info->tmr0_owned);
2585 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr0_ena = %u\n",
2586 info->tmr0_ena);
2587 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr1_owner = %u\n",
2588 info->tmr1_owner);
2589 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr1_owned = %u\n",
2590 info->tmr1_owned);
2591 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr1_ena = %u\n",
2592 info->tmr1_ena);
2593 ice_debug(hw, ICE_DBG_INIT, "dev caps: ts_ll_read = %u\n",
2594 info->ts_ll_read);
2595 ice_debug(hw, ICE_DBG_INIT, "dev caps: ieee_1588 ena_ports = %u\n",
2596 info->ena_ports);
2597 ice_debug(hw, ICE_DBG_INIT, "dev caps: tmr_own_map = %u\n",
2598 info->tmr_own_map);
2599 }
2600
2601 /**
2602 * ice_parse_fdir_dev_caps - Parse ICE_AQC_CAPS_FD device caps
2603 * @hw: pointer to the HW struct
2604 * @dev_p: pointer to device capabilities structure
2605 * @cap: capability element to parse
2606 *
2607 * Parse ICE_AQC_CAPS_FD for device capabilities.
2608 */
2609 static void
ice_parse_fdir_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,struct ice_aqc_list_caps_elem * cap)2610 ice_parse_fdir_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2611 struct ice_aqc_list_caps_elem *cap)
2612 {
2613 u32 number = le32_to_cpu(cap->number);
2614
2615 dev_p->num_flow_director_fltr = number;
2616 ice_debug(hw, ICE_DBG_INIT, "dev caps: num_flow_director_fltr = %d\n",
2617 dev_p->num_flow_director_fltr);
2618 }
2619
2620 /**
2621 * ice_parse_dev_caps - Parse device capabilities
2622 * @hw: pointer to the HW struct
2623 * @dev_p: pointer to device capabilities structure
2624 * @buf: buffer containing the device capability records
2625 * @cap_count: the number of capabilities
2626 *
2627 * Helper device to parse device (0x000B) capabilities list. For
2628 * capabilities shared between device and function, this relies on
2629 * ice_parse_common_caps.
2630 *
2631 * Loop through the list of provided capabilities and extract the relevant
2632 * data into the device capabilities structured.
2633 */
2634 static void
ice_parse_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_p,void * buf,u32 cap_count)2635 ice_parse_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_p,
2636 void *buf, u32 cap_count)
2637 {
2638 struct ice_aqc_list_caps_elem *cap_resp;
2639 u32 i;
2640
2641 cap_resp = buf;
2642
2643 memset(dev_p, 0, sizeof(*dev_p));
2644
2645 for (i = 0; i < cap_count; i++) {
2646 u16 cap = le16_to_cpu(cap_resp[i].cap);
2647 bool found;
2648
2649 found = ice_parse_common_caps(hw, &dev_p->common_cap,
2650 &cap_resp[i], "dev caps");
2651
2652 switch (cap) {
2653 case ICE_AQC_CAPS_VALID_FUNCTIONS:
2654 ice_parse_valid_functions_cap(hw, dev_p, &cap_resp[i]);
2655 break;
2656 case ICE_AQC_CAPS_VF:
2657 ice_parse_vf_dev_caps(hw, dev_p, &cap_resp[i]);
2658 break;
2659 case ICE_AQC_CAPS_VSI:
2660 ice_parse_vsi_dev_caps(hw, dev_p, &cap_resp[i]);
2661 break;
2662 case ICE_AQC_CAPS_1588:
2663 ice_parse_1588_dev_caps(hw, dev_p, &cap_resp[i]);
2664 break;
2665 case ICE_AQC_CAPS_FD:
2666 ice_parse_fdir_dev_caps(hw, dev_p, &cap_resp[i]);
2667 break;
2668 default:
2669 /* Don't list common capabilities as unknown */
2670 if (!found)
2671 ice_debug(hw, ICE_DBG_INIT, "dev caps: unknown capability[%d]: 0x%x\n",
2672 i, cap);
2673 break;
2674 }
2675 }
2676
2677 ice_recalc_port_limited_caps(hw, &dev_p->common_cap);
2678 }
2679
2680 /**
2681 * ice_aq_get_netlist_node
2682 * @hw: pointer to the hw struct
2683 * @cmd: get_link_topo AQ structure
2684 * @node_part_number: output node part number if node found
2685 * @node_handle: output node handle parameter if node found
2686 */
2687 static int
ice_aq_get_netlist_node(struct ice_hw * hw,struct ice_aqc_get_link_topo * cmd,u8 * node_part_number,u16 * node_handle)2688 ice_aq_get_netlist_node(struct ice_hw *hw, struct ice_aqc_get_link_topo *cmd,
2689 u8 *node_part_number, u16 *node_handle)
2690 {
2691 struct ice_aq_desc desc;
2692
2693 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
2694 desc.params.get_link_topo = *cmd;
2695
2696 if (ice_aq_send_cmd(hw, &desc, NULL, 0, NULL))
2697 return -EIO;
2698
2699 if (node_handle)
2700 *node_handle = le16_to_cpu(desc.params.get_link_topo.addr.handle);
2701 if (node_part_number)
2702 *node_part_number = desc.params.get_link_topo.node_part_num;
2703
2704 return 0;
2705 }
2706
2707 /**
2708 * ice_is_pf_c827 - check if pf contains c827 phy
2709 * @hw: pointer to the hw struct
2710 */
ice_is_pf_c827(struct ice_hw * hw)2711 bool ice_is_pf_c827(struct ice_hw *hw)
2712 {
2713 struct ice_aqc_get_link_topo cmd = {};
2714 u8 node_part_number;
2715 u16 node_handle;
2716 int status;
2717
2718 if (hw->mac_type != ICE_MAC_E810)
2719 return false;
2720
2721 if (hw->device_id != ICE_DEV_ID_E810C_QSFP)
2722 return true;
2723
2724 cmd.addr.topo_params.node_type_ctx =
2725 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_TYPE_M, ICE_AQC_LINK_TOPO_NODE_TYPE_PHY) |
2726 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M, ICE_AQC_LINK_TOPO_NODE_CTX_PORT);
2727 cmd.addr.topo_params.index = 0;
2728
2729 status = ice_aq_get_netlist_node(hw, &cmd, &node_part_number,
2730 &node_handle);
2731
2732 if (status || node_part_number != ICE_AQC_GET_LINK_TOPO_NODE_NR_C827)
2733 return false;
2734
2735 if (node_handle == E810C_QSFP_C827_0_HANDLE || node_handle == E810C_QSFP_C827_1_HANDLE)
2736 return true;
2737
2738 return false;
2739 }
2740
2741 /**
2742 * ice_aq_list_caps - query function/device capabilities
2743 * @hw: pointer to the HW struct
2744 * @buf: a buffer to hold the capabilities
2745 * @buf_size: size of the buffer
2746 * @cap_count: if not NULL, set to the number of capabilities reported
2747 * @opc: capabilities type to discover, device or function
2748 * @cd: pointer to command details structure or NULL
2749 *
2750 * Get the function (0x000A) or device (0x000B) capabilities description from
2751 * firmware and store it in the buffer.
2752 *
2753 * If the cap_count pointer is not NULL, then it is set to the number of
2754 * capabilities firmware will report. Note that if the buffer size is too
2755 * small, it is possible the command will return ICE_AQ_ERR_ENOMEM. The
2756 * cap_count will still be updated in this case. It is recommended that the
2757 * buffer size be set to ICE_AQ_MAX_BUF_LEN (the largest possible buffer that
2758 * firmware could return) to avoid this.
2759 */
2760 int
ice_aq_list_caps(struct ice_hw * hw,void * buf,u16 buf_size,u32 * cap_count,enum ice_adminq_opc opc,struct ice_sq_cd * cd)2761 ice_aq_list_caps(struct ice_hw *hw, void *buf, u16 buf_size, u32 *cap_count,
2762 enum ice_adminq_opc opc, struct ice_sq_cd *cd)
2763 {
2764 struct ice_aqc_list_caps *cmd;
2765 struct ice_aq_desc desc;
2766 int status;
2767
2768 cmd = &desc.params.get_cap;
2769
2770 if (opc != ice_aqc_opc_list_func_caps &&
2771 opc != ice_aqc_opc_list_dev_caps)
2772 return -EINVAL;
2773
2774 ice_fill_dflt_direct_cmd_desc(&desc, opc);
2775 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2776
2777 if (cap_count)
2778 *cap_count = le32_to_cpu(cmd->count);
2779
2780 return status;
2781 }
2782
2783 /**
2784 * ice_discover_dev_caps - Read and extract device capabilities
2785 * @hw: pointer to the hardware structure
2786 * @dev_caps: pointer to device capabilities structure
2787 *
2788 * Read the device capabilities and extract them into the dev_caps structure
2789 * for later use.
2790 */
2791 int
ice_discover_dev_caps(struct ice_hw * hw,struct ice_hw_dev_caps * dev_caps)2792 ice_discover_dev_caps(struct ice_hw *hw, struct ice_hw_dev_caps *dev_caps)
2793 {
2794 u32 cap_count = 0;
2795 void *cbuf;
2796 int status;
2797
2798 cbuf = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
2799 if (!cbuf)
2800 return -ENOMEM;
2801
2802 /* Although the driver doesn't know the number of capabilities the
2803 * device will return, we can simply send a 4KB buffer, the maximum
2804 * possible size that firmware can return.
2805 */
2806 cap_count = ICE_AQ_MAX_BUF_LEN / sizeof(struct ice_aqc_list_caps_elem);
2807
2808 status = ice_aq_list_caps(hw, cbuf, ICE_AQ_MAX_BUF_LEN, &cap_count,
2809 ice_aqc_opc_list_dev_caps, NULL);
2810 if (!status)
2811 ice_parse_dev_caps(hw, dev_caps, cbuf, cap_count);
2812 kfree(cbuf);
2813
2814 return status;
2815 }
2816
2817 /**
2818 * ice_discover_func_caps - Read and extract function capabilities
2819 * @hw: pointer to the hardware structure
2820 * @func_caps: pointer to function capabilities structure
2821 *
2822 * Read the function capabilities and extract them into the func_caps structure
2823 * for later use.
2824 */
2825 static int
ice_discover_func_caps(struct ice_hw * hw,struct ice_hw_func_caps * func_caps)2826 ice_discover_func_caps(struct ice_hw *hw, struct ice_hw_func_caps *func_caps)
2827 {
2828 u32 cap_count = 0;
2829 void *cbuf;
2830 int status;
2831
2832 cbuf = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
2833 if (!cbuf)
2834 return -ENOMEM;
2835
2836 /* Although the driver doesn't know the number of capabilities the
2837 * device will return, we can simply send a 4KB buffer, the maximum
2838 * possible size that firmware can return.
2839 */
2840 cap_count = ICE_AQ_MAX_BUF_LEN / sizeof(struct ice_aqc_list_caps_elem);
2841
2842 status = ice_aq_list_caps(hw, cbuf, ICE_AQ_MAX_BUF_LEN, &cap_count,
2843 ice_aqc_opc_list_func_caps, NULL);
2844 if (!status)
2845 ice_parse_func_caps(hw, func_caps, cbuf, cap_count);
2846 kfree(cbuf);
2847
2848 return status;
2849 }
2850
2851 /**
2852 * ice_set_safe_mode_caps - Override dev/func capabilities when in safe mode
2853 * @hw: pointer to the hardware structure
2854 */
ice_set_safe_mode_caps(struct ice_hw * hw)2855 void ice_set_safe_mode_caps(struct ice_hw *hw)
2856 {
2857 struct ice_hw_func_caps *func_caps = &hw->func_caps;
2858 struct ice_hw_dev_caps *dev_caps = &hw->dev_caps;
2859 struct ice_hw_common_caps cached_caps;
2860 u32 num_funcs;
2861
2862 /* cache some func_caps values that should be restored after memset */
2863 cached_caps = func_caps->common_cap;
2864
2865 /* unset func capabilities */
2866 memset(func_caps, 0, sizeof(*func_caps));
2867
2868 #define ICE_RESTORE_FUNC_CAP(name) \
2869 func_caps->common_cap.name = cached_caps.name
2870
2871 /* restore cached values */
2872 ICE_RESTORE_FUNC_CAP(valid_functions);
2873 ICE_RESTORE_FUNC_CAP(txq_first_id);
2874 ICE_RESTORE_FUNC_CAP(rxq_first_id);
2875 ICE_RESTORE_FUNC_CAP(msix_vector_first_id);
2876 ICE_RESTORE_FUNC_CAP(max_mtu);
2877 ICE_RESTORE_FUNC_CAP(nvm_unified_update);
2878 ICE_RESTORE_FUNC_CAP(nvm_update_pending_nvm);
2879 ICE_RESTORE_FUNC_CAP(nvm_update_pending_orom);
2880 ICE_RESTORE_FUNC_CAP(nvm_update_pending_netlist);
2881
2882 /* one Tx and one Rx queue in safe mode */
2883 func_caps->common_cap.num_rxq = 1;
2884 func_caps->common_cap.num_txq = 1;
2885
2886 /* two MSIX vectors, one for traffic and one for misc causes */
2887 func_caps->common_cap.num_msix_vectors = 2;
2888 func_caps->guar_num_vsi = 1;
2889
2890 /* cache some dev_caps values that should be restored after memset */
2891 cached_caps = dev_caps->common_cap;
2892 num_funcs = dev_caps->num_funcs;
2893
2894 /* unset dev capabilities */
2895 memset(dev_caps, 0, sizeof(*dev_caps));
2896
2897 #define ICE_RESTORE_DEV_CAP(name) \
2898 dev_caps->common_cap.name = cached_caps.name
2899
2900 /* restore cached values */
2901 ICE_RESTORE_DEV_CAP(valid_functions);
2902 ICE_RESTORE_DEV_CAP(txq_first_id);
2903 ICE_RESTORE_DEV_CAP(rxq_first_id);
2904 ICE_RESTORE_DEV_CAP(msix_vector_first_id);
2905 ICE_RESTORE_DEV_CAP(max_mtu);
2906 ICE_RESTORE_DEV_CAP(nvm_unified_update);
2907 ICE_RESTORE_DEV_CAP(nvm_update_pending_nvm);
2908 ICE_RESTORE_DEV_CAP(nvm_update_pending_orom);
2909 ICE_RESTORE_DEV_CAP(nvm_update_pending_netlist);
2910 dev_caps->num_funcs = num_funcs;
2911
2912 /* one Tx and one Rx queue per function in safe mode */
2913 dev_caps->common_cap.num_rxq = num_funcs;
2914 dev_caps->common_cap.num_txq = num_funcs;
2915
2916 /* two MSIX vectors per function */
2917 dev_caps->common_cap.num_msix_vectors = 2 * num_funcs;
2918 }
2919
2920 /**
2921 * ice_get_caps - get info about the HW
2922 * @hw: pointer to the hardware structure
2923 */
ice_get_caps(struct ice_hw * hw)2924 int ice_get_caps(struct ice_hw *hw)
2925 {
2926 int status;
2927
2928 status = ice_discover_dev_caps(hw, &hw->dev_caps);
2929 if (status)
2930 return status;
2931
2932 return ice_discover_func_caps(hw, &hw->func_caps);
2933 }
2934
2935 /**
2936 * ice_aq_manage_mac_write - manage MAC address write command
2937 * @hw: pointer to the HW struct
2938 * @mac_addr: MAC address to be written as LAA/LAA+WoL/Port address
2939 * @flags: flags to control write behavior
2940 * @cd: pointer to command details structure or NULL
2941 *
2942 * This function is used to write MAC address to the NVM (0x0108).
2943 */
2944 int
ice_aq_manage_mac_write(struct ice_hw * hw,const u8 * mac_addr,u8 flags,struct ice_sq_cd * cd)2945 ice_aq_manage_mac_write(struct ice_hw *hw, const u8 *mac_addr, u8 flags,
2946 struct ice_sq_cd *cd)
2947 {
2948 struct ice_aqc_manage_mac_write *cmd;
2949 struct ice_aq_desc desc;
2950
2951 cmd = &desc.params.mac_write;
2952 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_write);
2953
2954 cmd->flags = flags;
2955 ether_addr_copy(cmd->mac_addr, mac_addr);
2956
2957 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2958 }
2959
2960 /**
2961 * ice_aq_clear_pxe_mode
2962 * @hw: pointer to the HW struct
2963 *
2964 * Tell the firmware that the driver is taking over from PXE (0x0110).
2965 */
ice_aq_clear_pxe_mode(struct ice_hw * hw)2966 static int ice_aq_clear_pxe_mode(struct ice_hw *hw)
2967 {
2968 struct ice_aq_desc desc;
2969
2970 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pxe_mode);
2971 desc.params.clear_pxe.rx_cnt = ICE_AQC_CLEAR_PXE_RX_CNT;
2972
2973 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
2974 }
2975
2976 /**
2977 * ice_clear_pxe_mode - clear pxe operations mode
2978 * @hw: pointer to the HW struct
2979 *
2980 * Make sure all PXE mode settings are cleared, including things
2981 * like descriptor fetch/write-back mode.
2982 */
ice_clear_pxe_mode(struct ice_hw * hw)2983 void ice_clear_pxe_mode(struct ice_hw *hw)
2984 {
2985 if (ice_check_sq_alive(hw, &hw->adminq))
2986 ice_aq_clear_pxe_mode(hw);
2987 }
2988
2989 /**
2990 * ice_aq_set_port_params - set physical port parameters.
2991 * @pi: pointer to the port info struct
2992 * @double_vlan: if set double VLAN is enabled
2993 * @cd: pointer to command details structure or NULL
2994 *
2995 * Set Physical port parameters (0x0203)
2996 */
2997 int
ice_aq_set_port_params(struct ice_port_info * pi,bool double_vlan,struct ice_sq_cd * cd)2998 ice_aq_set_port_params(struct ice_port_info *pi, bool double_vlan,
2999 struct ice_sq_cd *cd)
3000
3001 {
3002 struct ice_aqc_set_port_params *cmd;
3003 struct ice_hw *hw = pi->hw;
3004 struct ice_aq_desc desc;
3005 u16 cmd_flags = 0;
3006
3007 cmd = &desc.params.set_port_params;
3008
3009 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_params);
3010 if (double_vlan)
3011 cmd_flags |= ICE_AQC_SET_P_PARAMS_DOUBLE_VLAN_ENA;
3012 cmd->cmd_flags = cpu_to_le16(cmd_flags);
3013
3014 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
3015 }
3016
3017 /**
3018 * ice_is_100m_speed_supported
3019 * @hw: pointer to the HW struct
3020 *
3021 * returns true if 100M speeds are supported by the device,
3022 * false otherwise.
3023 */
ice_is_100m_speed_supported(struct ice_hw * hw)3024 bool ice_is_100m_speed_supported(struct ice_hw *hw)
3025 {
3026 switch (hw->device_id) {
3027 case ICE_DEV_ID_E822C_SGMII:
3028 case ICE_DEV_ID_E822L_SGMII:
3029 case ICE_DEV_ID_E823L_1GBE:
3030 case ICE_DEV_ID_E823C_SGMII:
3031 return true;
3032 default:
3033 return false;
3034 }
3035 }
3036
3037 /**
3038 * ice_get_link_speed_based_on_phy_type - returns link speed
3039 * @phy_type_low: lower part of phy_type
3040 * @phy_type_high: higher part of phy_type
3041 *
3042 * This helper function will convert an entry in PHY type structure
3043 * [phy_type_low, phy_type_high] to its corresponding link speed.
3044 * Note: In the structure of [phy_type_low, phy_type_high], there should
3045 * be one bit set, as this function will convert one PHY type to its
3046 * speed.
3047 * If no bit gets set, ICE_AQ_LINK_SPEED_UNKNOWN will be returned
3048 * If more than one bit gets set, ICE_AQ_LINK_SPEED_UNKNOWN will be returned
3049 */
3050 static u16
ice_get_link_speed_based_on_phy_type(u64 phy_type_low,u64 phy_type_high)3051 ice_get_link_speed_based_on_phy_type(u64 phy_type_low, u64 phy_type_high)
3052 {
3053 u16 speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
3054 u16 speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
3055
3056 switch (phy_type_low) {
3057 case ICE_PHY_TYPE_LOW_100BASE_TX:
3058 case ICE_PHY_TYPE_LOW_100M_SGMII:
3059 speed_phy_type_low = ICE_AQ_LINK_SPEED_100MB;
3060 break;
3061 case ICE_PHY_TYPE_LOW_1000BASE_T:
3062 case ICE_PHY_TYPE_LOW_1000BASE_SX:
3063 case ICE_PHY_TYPE_LOW_1000BASE_LX:
3064 case ICE_PHY_TYPE_LOW_1000BASE_KX:
3065 case ICE_PHY_TYPE_LOW_1G_SGMII:
3066 speed_phy_type_low = ICE_AQ_LINK_SPEED_1000MB;
3067 break;
3068 case ICE_PHY_TYPE_LOW_2500BASE_T:
3069 case ICE_PHY_TYPE_LOW_2500BASE_X:
3070 case ICE_PHY_TYPE_LOW_2500BASE_KX:
3071 speed_phy_type_low = ICE_AQ_LINK_SPEED_2500MB;
3072 break;
3073 case ICE_PHY_TYPE_LOW_5GBASE_T:
3074 case ICE_PHY_TYPE_LOW_5GBASE_KR:
3075 speed_phy_type_low = ICE_AQ_LINK_SPEED_5GB;
3076 break;
3077 case ICE_PHY_TYPE_LOW_10GBASE_T:
3078 case ICE_PHY_TYPE_LOW_10G_SFI_DA:
3079 case ICE_PHY_TYPE_LOW_10GBASE_SR:
3080 case ICE_PHY_TYPE_LOW_10GBASE_LR:
3081 case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
3082 case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
3083 case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
3084 speed_phy_type_low = ICE_AQ_LINK_SPEED_10GB;
3085 break;
3086 case ICE_PHY_TYPE_LOW_25GBASE_T:
3087 case ICE_PHY_TYPE_LOW_25GBASE_CR:
3088 case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
3089 case ICE_PHY_TYPE_LOW_25GBASE_CR1:
3090 case ICE_PHY_TYPE_LOW_25GBASE_SR:
3091 case ICE_PHY_TYPE_LOW_25GBASE_LR:
3092 case ICE_PHY_TYPE_LOW_25GBASE_KR:
3093 case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
3094 case ICE_PHY_TYPE_LOW_25GBASE_KR1:
3095 case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
3096 case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
3097 speed_phy_type_low = ICE_AQ_LINK_SPEED_25GB;
3098 break;
3099 case ICE_PHY_TYPE_LOW_40GBASE_CR4:
3100 case ICE_PHY_TYPE_LOW_40GBASE_SR4:
3101 case ICE_PHY_TYPE_LOW_40GBASE_LR4:
3102 case ICE_PHY_TYPE_LOW_40GBASE_KR4:
3103 case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
3104 case ICE_PHY_TYPE_LOW_40G_XLAUI:
3105 speed_phy_type_low = ICE_AQ_LINK_SPEED_40GB;
3106 break;
3107 case ICE_PHY_TYPE_LOW_50GBASE_CR2:
3108 case ICE_PHY_TYPE_LOW_50GBASE_SR2:
3109 case ICE_PHY_TYPE_LOW_50GBASE_LR2:
3110 case ICE_PHY_TYPE_LOW_50GBASE_KR2:
3111 case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
3112 case ICE_PHY_TYPE_LOW_50G_LAUI2:
3113 case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
3114 case ICE_PHY_TYPE_LOW_50G_AUI2:
3115 case ICE_PHY_TYPE_LOW_50GBASE_CP:
3116 case ICE_PHY_TYPE_LOW_50GBASE_SR:
3117 case ICE_PHY_TYPE_LOW_50GBASE_FR:
3118 case ICE_PHY_TYPE_LOW_50GBASE_LR:
3119 case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
3120 case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
3121 case ICE_PHY_TYPE_LOW_50G_AUI1:
3122 speed_phy_type_low = ICE_AQ_LINK_SPEED_50GB;
3123 break;
3124 case ICE_PHY_TYPE_LOW_100GBASE_CR4:
3125 case ICE_PHY_TYPE_LOW_100GBASE_SR4:
3126 case ICE_PHY_TYPE_LOW_100GBASE_LR4:
3127 case ICE_PHY_TYPE_LOW_100GBASE_KR4:
3128 case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
3129 case ICE_PHY_TYPE_LOW_100G_CAUI4:
3130 case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
3131 case ICE_PHY_TYPE_LOW_100G_AUI4:
3132 case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
3133 case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
3134 case ICE_PHY_TYPE_LOW_100GBASE_CP2:
3135 case ICE_PHY_TYPE_LOW_100GBASE_SR2:
3136 case ICE_PHY_TYPE_LOW_100GBASE_DR:
3137 speed_phy_type_low = ICE_AQ_LINK_SPEED_100GB;
3138 break;
3139 default:
3140 speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
3141 break;
3142 }
3143
3144 switch (phy_type_high) {
3145 case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
3146 case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
3147 case ICE_PHY_TYPE_HIGH_100G_CAUI2:
3148 case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
3149 case ICE_PHY_TYPE_HIGH_100G_AUI2:
3150 speed_phy_type_high = ICE_AQ_LINK_SPEED_100GB;
3151 break;
3152 default:
3153 speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
3154 break;
3155 }
3156
3157 if (speed_phy_type_low == ICE_AQ_LINK_SPEED_UNKNOWN &&
3158 speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
3159 return ICE_AQ_LINK_SPEED_UNKNOWN;
3160 else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
3161 speed_phy_type_high != ICE_AQ_LINK_SPEED_UNKNOWN)
3162 return ICE_AQ_LINK_SPEED_UNKNOWN;
3163 else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
3164 speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
3165 return speed_phy_type_low;
3166 else
3167 return speed_phy_type_high;
3168 }
3169
3170 /**
3171 * ice_update_phy_type
3172 * @phy_type_low: pointer to the lower part of phy_type
3173 * @phy_type_high: pointer to the higher part of phy_type
3174 * @link_speeds_bitmap: targeted link speeds bitmap
3175 *
3176 * Note: For the link_speeds_bitmap structure, you can check it at
3177 * [ice_aqc_get_link_status->link_speed]. Caller can pass in
3178 * link_speeds_bitmap include multiple speeds.
3179 *
3180 * Each entry in this [phy_type_low, phy_type_high] structure will
3181 * present a certain link speed. This helper function will turn on bits
3182 * in [phy_type_low, phy_type_high] structure based on the value of
3183 * link_speeds_bitmap input parameter.
3184 */
3185 void
ice_update_phy_type(u64 * phy_type_low,u64 * phy_type_high,u16 link_speeds_bitmap)3186 ice_update_phy_type(u64 *phy_type_low, u64 *phy_type_high,
3187 u16 link_speeds_bitmap)
3188 {
3189 u64 pt_high;
3190 u64 pt_low;
3191 int index;
3192 u16 speed;
3193
3194 /* We first check with low part of phy_type */
3195 for (index = 0; index <= ICE_PHY_TYPE_LOW_MAX_INDEX; index++) {
3196 pt_low = BIT_ULL(index);
3197 speed = ice_get_link_speed_based_on_phy_type(pt_low, 0);
3198
3199 if (link_speeds_bitmap & speed)
3200 *phy_type_low |= BIT_ULL(index);
3201 }
3202
3203 /* We then check with high part of phy_type */
3204 for (index = 0; index <= ICE_PHY_TYPE_HIGH_MAX_INDEX; index++) {
3205 pt_high = BIT_ULL(index);
3206 speed = ice_get_link_speed_based_on_phy_type(0, pt_high);
3207
3208 if (link_speeds_bitmap & speed)
3209 *phy_type_high |= BIT_ULL(index);
3210 }
3211 }
3212
3213 /**
3214 * ice_aq_set_phy_cfg
3215 * @hw: pointer to the HW struct
3216 * @pi: port info structure of the interested logical port
3217 * @cfg: structure with PHY configuration data to be set
3218 * @cd: pointer to command details structure or NULL
3219 *
3220 * Set the various PHY configuration parameters supported on the Port.
3221 * One or more of the Set PHY config parameters may be ignored in an MFP
3222 * mode as the PF may not have the privilege to set some of the PHY Config
3223 * parameters. This status will be indicated by the command response (0x0601).
3224 */
3225 int
ice_aq_set_phy_cfg(struct ice_hw * hw,struct ice_port_info * pi,struct ice_aqc_set_phy_cfg_data * cfg,struct ice_sq_cd * cd)3226 ice_aq_set_phy_cfg(struct ice_hw *hw, struct ice_port_info *pi,
3227 struct ice_aqc_set_phy_cfg_data *cfg, struct ice_sq_cd *cd)
3228 {
3229 struct ice_aq_desc desc;
3230 int status;
3231
3232 if (!cfg)
3233 return -EINVAL;
3234
3235 /* Ensure that only valid bits of cfg->caps can be turned on. */
3236 if (cfg->caps & ~ICE_AQ_PHY_ENA_VALID_MASK) {
3237 ice_debug(hw, ICE_DBG_PHY, "Invalid bit is set in ice_aqc_set_phy_cfg_data->caps : 0x%x\n",
3238 cfg->caps);
3239
3240 cfg->caps &= ICE_AQ_PHY_ENA_VALID_MASK;
3241 }
3242
3243 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_phy_cfg);
3244 desc.params.set_phy.lport_num = pi->lport;
3245 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
3246
3247 ice_debug(hw, ICE_DBG_LINK, "set phy cfg\n");
3248 ice_debug(hw, ICE_DBG_LINK, " phy_type_low = 0x%llx\n",
3249 (unsigned long long)le64_to_cpu(cfg->phy_type_low));
3250 ice_debug(hw, ICE_DBG_LINK, " phy_type_high = 0x%llx\n",
3251 (unsigned long long)le64_to_cpu(cfg->phy_type_high));
3252 ice_debug(hw, ICE_DBG_LINK, " caps = 0x%x\n", cfg->caps);
3253 ice_debug(hw, ICE_DBG_LINK, " low_power_ctrl_an = 0x%x\n",
3254 cfg->low_power_ctrl_an);
3255 ice_debug(hw, ICE_DBG_LINK, " eee_cap = 0x%x\n", cfg->eee_cap);
3256 ice_debug(hw, ICE_DBG_LINK, " eeer_value = 0x%x\n", cfg->eeer_value);
3257 ice_debug(hw, ICE_DBG_LINK, " link_fec_opt = 0x%x\n",
3258 cfg->link_fec_opt);
3259
3260 status = ice_aq_send_cmd(hw, &desc, cfg, sizeof(*cfg), cd);
3261 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3262 status = 0;
3263
3264 if (!status)
3265 pi->phy.curr_user_phy_cfg = *cfg;
3266
3267 return status;
3268 }
3269
3270 /**
3271 * ice_update_link_info - update status of the HW network link
3272 * @pi: port info structure of the interested logical port
3273 */
ice_update_link_info(struct ice_port_info * pi)3274 int ice_update_link_info(struct ice_port_info *pi)
3275 {
3276 struct ice_link_status *li;
3277 int status;
3278
3279 if (!pi)
3280 return -EINVAL;
3281
3282 li = &pi->phy.link_info;
3283
3284 status = ice_aq_get_link_info(pi, true, NULL, NULL);
3285 if (status)
3286 return status;
3287
3288 if (li->link_info & ICE_AQ_MEDIA_AVAILABLE) {
3289 struct ice_aqc_get_phy_caps_data *pcaps;
3290 struct ice_hw *hw;
3291
3292 hw = pi->hw;
3293 pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps),
3294 GFP_KERNEL);
3295 if (!pcaps)
3296 return -ENOMEM;
3297
3298 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
3299 pcaps, NULL);
3300
3301 devm_kfree(ice_hw_to_dev(hw), pcaps);
3302 }
3303
3304 return status;
3305 }
3306
3307 /**
3308 * ice_cache_phy_user_req
3309 * @pi: port information structure
3310 * @cache_data: PHY logging data
3311 * @cache_mode: PHY logging mode
3312 *
3313 * Log the user request on (FC, FEC, SPEED) for later use.
3314 */
3315 static void
ice_cache_phy_user_req(struct ice_port_info * pi,struct ice_phy_cache_mode_data cache_data,enum ice_phy_cache_mode cache_mode)3316 ice_cache_phy_user_req(struct ice_port_info *pi,
3317 struct ice_phy_cache_mode_data cache_data,
3318 enum ice_phy_cache_mode cache_mode)
3319 {
3320 if (!pi)
3321 return;
3322
3323 switch (cache_mode) {
3324 case ICE_FC_MODE:
3325 pi->phy.curr_user_fc_req = cache_data.data.curr_user_fc_req;
3326 break;
3327 case ICE_SPEED_MODE:
3328 pi->phy.curr_user_speed_req =
3329 cache_data.data.curr_user_speed_req;
3330 break;
3331 case ICE_FEC_MODE:
3332 pi->phy.curr_user_fec_req = cache_data.data.curr_user_fec_req;
3333 break;
3334 default:
3335 break;
3336 }
3337 }
3338
3339 /**
3340 * ice_caps_to_fc_mode
3341 * @caps: PHY capabilities
3342 *
3343 * Convert PHY FC capabilities to ice FC mode
3344 */
ice_caps_to_fc_mode(u8 caps)3345 enum ice_fc_mode ice_caps_to_fc_mode(u8 caps)
3346 {
3347 if (caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE &&
3348 caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
3349 return ICE_FC_FULL;
3350
3351 if (caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
3352 return ICE_FC_TX_PAUSE;
3353
3354 if (caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
3355 return ICE_FC_RX_PAUSE;
3356
3357 return ICE_FC_NONE;
3358 }
3359
3360 /**
3361 * ice_caps_to_fec_mode
3362 * @caps: PHY capabilities
3363 * @fec_options: Link FEC options
3364 *
3365 * Convert PHY FEC capabilities to ice FEC mode
3366 */
ice_caps_to_fec_mode(u8 caps,u8 fec_options)3367 enum ice_fec_mode ice_caps_to_fec_mode(u8 caps, u8 fec_options)
3368 {
3369 if (caps & ICE_AQC_PHY_EN_AUTO_FEC)
3370 return ICE_FEC_AUTO;
3371
3372 if (fec_options & (ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN |
3373 ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
3374 ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN |
3375 ICE_AQC_PHY_FEC_25G_KR_REQ))
3376 return ICE_FEC_BASER;
3377
3378 if (fec_options & (ICE_AQC_PHY_FEC_25G_RS_528_REQ |
3379 ICE_AQC_PHY_FEC_25G_RS_544_REQ |
3380 ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN))
3381 return ICE_FEC_RS;
3382
3383 return ICE_FEC_NONE;
3384 }
3385
3386 /**
3387 * ice_cfg_phy_fc - Configure PHY FC data based on FC mode
3388 * @pi: port information structure
3389 * @cfg: PHY configuration data to set FC mode
3390 * @req_mode: FC mode to configure
3391 */
3392 int
ice_cfg_phy_fc(struct ice_port_info * pi,struct ice_aqc_set_phy_cfg_data * cfg,enum ice_fc_mode req_mode)3393 ice_cfg_phy_fc(struct ice_port_info *pi, struct ice_aqc_set_phy_cfg_data *cfg,
3394 enum ice_fc_mode req_mode)
3395 {
3396 struct ice_phy_cache_mode_data cache_data;
3397 u8 pause_mask = 0x0;
3398
3399 if (!pi || !cfg)
3400 return -EINVAL;
3401
3402 switch (req_mode) {
3403 case ICE_FC_FULL:
3404 pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
3405 pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
3406 break;
3407 case ICE_FC_RX_PAUSE:
3408 pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
3409 break;
3410 case ICE_FC_TX_PAUSE:
3411 pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
3412 break;
3413 default:
3414 break;
3415 }
3416
3417 /* clear the old pause settings */
3418 cfg->caps &= ~(ICE_AQC_PHY_EN_TX_LINK_PAUSE |
3419 ICE_AQC_PHY_EN_RX_LINK_PAUSE);
3420
3421 /* set the new capabilities */
3422 cfg->caps |= pause_mask;
3423
3424 /* Cache user FC request */
3425 cache_data.data.curr_user_fc_req = req_mode;
3426 ice_cache_phy_user_req(pi, cache_data, ICE_FC_MODE);
3427
3428 return 0;
3429 }
3430
3431 /**
3432 * ice_set_fc
3433 * @pi: port information structure
3434 * @aq_failures: pointer to status code, specific to ice_set_fc routine
3435 * @ena_auto_link_update: enable automatic link update
3436 *
3437 * Set the requested flow control mode.
3438 */
3439 int
ice_set_fc(struct ice_port_info * pi,u8 * aq_failures,bool ena_auto_link_update)3440 ice_set_fc(struct ice_port_info *pi, u8 *aq_failures, bool ena_auto_link_update)
3441 {
3442 struct ice_aqc_set_phy_cfg_data cfg = { 0 };
3443 struct ice_aqc_get_phy_caps_data *pcaps;
3444 struct ice_hw *hw;
3445 int status;
3446
3447 if (!pi || !aq_failures)
3448 return -EINVAL;
3449
3450 *aq_failures = 0;
3451 hw = pi->hw;
3452
3453 pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps), GFP_KERNEL);
3454 if (!pcaps)
3455 return -ENOMEM;
3456
3457 /* Get the current PHY config */
3458 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG,
3459 pcaps, NULL);
3460 if (status) {
3461 *aq_failures = ICE_SET_FC_AQ_FAIL_GET;
3462 goto out;
3463 }
3464
3465 ice_copy_phy_caps_to_cfg(pi, pcaps, &cfg);
3466
3467 /* Configure the set PHY data */
3468 status = ice_cfg_phy_fc(pi, &cfg, pi->fc.req_mode);
3469 if (status)
3470 goto out;
3471
3472 /* If the capabilities have changed, then set the new config */
3473 if (cfg.caps != pcaps->caps) {
3474 int retry_count, retry_max = 10;
3475
3476 /* Auto restart link so settings take effect */
3477 if (ena_auto_link_update)
3478 cfg.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
3479
3480 status = ice_aq_set_phy_cfg(hw, pi, &cfg, NULL);
3481 if (status) {
3482 *aq_failures = ICE_SET_FC_AQ_FAIL_SET;
3483 goto out;
3484 }
3485
3486 /* Update the link info
3487 * It sometimes takes a really long time for link to
3488 * come back from the atomic reset. Thus, we wait a
3489 * little bit.
3490 */
3491 for (retry_count = 0; retry_count < retry_max; retry_count++) {
3492 status = ice_update_link_info(pi);
3493
3494 if (!status)
3495 break;
3496
3497 mdelay(100);
3498 }
3499
3500 if (status)
3501 *aq_failures = ICE_SET_FC_AQ_FAIL_UPDATE;
3502 }
3503
3504 out:
3505 devm_kfree(ice_hw_to_dev(hw), pcaps);
3506 return status;
3507 }
3508
3509 /**
3510 * ice_phy_caps_equals_cfg
3511 * @phy_caps: PHY capabilities
3512 * @phy_cfg: PHY configuration
3513 *
3514 * Helper function to determine if PHY capabilities matches PHY
3515 * configuration
3516 */
3517 bool
ice_phy_caps_equals_cfg(struct ice_aqc_get_phy_caps_data * phy_caps,struct ice_aqc_set_phy_cfg_data * phy_cfg)3518 ice_phy_caps_equals_cfg(struct ice_aqc_get_phy_caps_data *phy_caps,
3519 struct ice_aqc_set_phy_cfg_data *phy_cfg)
3520 {
3521 u8 caps_mask, cfg_mask;
3522
3523 if (!phy_caps || !phy_cfg)
3524 return false;
3525
3526 /* These bits are not common between capabilities and configuration.
3527 * Do not use them to determine equality.
3528 */
3529 caps_mask = ICE_AQC_PHY_CAPS_MASK & ~(ICE_AQC_PHY_AN_MODE |
3530 ICE_AQC_GET_PHY_EN_MOD_QUAL);
3531 cfg_mask = ICE_AQ_PHY_ENA_VALID_MASK & ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
3532
3533 if (phy_caps->phy_type_low != phy_cfg->phy_type_low ||
3534 phy_caps->phy_type_high != phy_cfg->phy_type_high ||
3535 ((phy_caps->caps & caps_mask) != (phy_cfg->caps & cfg_mask)) ||
3536 phy_caps->low_power_ctrl_an != phy_cfg->low_power_ctrl_an ||
3537 phy_caps->eee_cap != phy_cfg->eee_cap ||
3538 phy_caps->eeer_value != phy_cfg->eeer_value ||
3539 phy_caps->link_fec_options != phy_cfg->link_fec_opt)
3540 return false;
3541
3542 return true;
3543 }
3544
3545 /**
3546 * ice_copy_phy_caps_to_cfg - Copy PHY ability data to configuration data
3547 * @pi: port information structure
3548 * @caps: PHY ability structure to copy date from
3549 * @cfg: PHY configuration structure to copy data to
3550 *
3551 * Helper function to copy AQC PHY get ability data to PHY set configuration
3552 * data structure
3553 */
3554 void
ice_copy_phy_caps_to_cfg(struct ice_port_info * pi,struct ice_aqc_get_phy_caps_data * caps,struct ice_aqc_set_phy_cfg_data * cfg)3555 ice_copy_phy_caps_to_cfg(struct ice_port_info *pi,
3556 struct ice_aqc_get_phy_caps_data *caps,
3557 struct ice_aqc_set_phy_cfg_data *cfg)
3558 {
3559 if (!pi || !caps || !cfg)
3560 return;
3561
3562 memset(cfg, 0, sizeof(*cfg));
3563 cfg->phy_type_low = caps->phy_type_low;
3564 cfg->phy_type_high = caps->phy_type_high;
3565 cfg->caps = caps->caps;
3566 cfg->low_power_ctrl_an = caps->low_power_ctrl_an;
3567 cfg->eee_cap = caps->eee_cap;
3568 cfg->eeer_value = caps->eeer_value;
3569 cfg->link_fec_opt = caps->link_fec_options;
3570 cfg->module_compliance_enforcement =
3571 caps->module_compliance_enforcement;
3572 }
3573
3574 /**
3575 * ice_cfg_phy_fec - Configure PHY FEC data based on FEC mode
3576 * @pi: port information structure
3577 * @cfg: PHY configuration data to set FEC mode
3578 * @fec: FEC mode to configure
3579 */
3580 int
ice_cfg_phy_fec(struct ice_port_info * pi,struct ice_aqc_set_phy_cfg_data * cfg,enum ice_fec_mode fec)3581 ice_cfg_phy_fec(struct ice_port_info *pi, struct ice_aqc_set_phy_cfg_data *cfg,
3582 enum ice_fec_mode fec)
3583 {
3584 struct ice_aqc_get_phy_caps_data *pcaps;
3585 struct ice_hw *hw;
3586 int status;
3587
3588 if (!pi || !cfg)
3589 return -EINVAL;
3590
3591 hw = pi->hw;
3592
3593 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3594 if (!pcaps)
3595 return -ENOMEM;
3596
3597 status = ice_aq_get_phy_caps(pi, false,
3598 (ice_fw_supports_report_dflt_cfg(hw) ?
3599 ICE_AQC_REPORT_DFLT_CFG :
3600 ICE_AQC_REPORT_TOPO_CAP_MEDIA), pcaps, NULL);
3601 if (status)
3602 goto out;
3603
3604 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
3605 cfg->link_fec_opt = pcaps->link_fec_options;
3606
3607 switch (fec) {
3608 case ICE_FEC_BASER:
3609 /* Clear RS bits, and AND BASE-R ability
3610 * bits and OR request bits.
3611 */
3612 cfg->link_fec_opt &= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN |
3613 ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN;
3614 cfg->link_fec_opt |= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
3615 ICE_AQC_PHY_FEC_25G_KR_REQ;
3616 break;
3617 case ICE_FEC_RS:
3618 /* Clear BASE-R bits, and AND RS ability
3619 * bits and OR request bits.
3620 */
3621 cfg->link_fec_opt &= ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN;
3622 cfg->link_fec_opt |= ICE_AQC_PHY_FEC_25G_RS_528_REQ |
3623 ICE_AQC_PHY_FEC_25G_RS_544_REQ;
3624 break;
3625 case ICE_FEC_NONE:
3626 /* Clear all FEC option bits. */
3627 cfg->link_fec_opt &= ~ICE_AQC_PHY_FEC_MASK;
3628 break;
3629 case ICE_FEC_AUTO:
3630 /* AND auto FEC bit, and all caps bits. */
3631 cfg->caps &= ICE_AQC_PHY_CAPS_MASK;
3632 cfg->link_fec_opt |= pcaps->link_fec_options;
3633 break;
3634 default:
3635 status = -EINVAL;
3636 break;
3637 }
3638
3639 if (fec == ICE_FEC_AUTO && ice_fw_supports_link_override(hw) &&
3640 !ice_fw_supports_report_dflt_cfg(hw)) {
3641 struct ice_link_default_override_tlv tlv = { 0 };
3642
3643 status = ice_get_link_default_override(&tlv, pi);
3644 if (status)
3645 goto out;
3646
3647 if (!(tlv.options & ICE_LINK_OVERRIDE_STRICT_MODE) &&
3648 (tlv.options & ICE_LINK_OVERRIDE_EN))
3649 cfg->link_fec_opt = tlv.fec_options;
3650 }
3651
3652 out:
3653 kfree(pcaps);
3654
3655 return status;
3656 }
3657
3658 /**
3659 * ice_get_link_status - get status of the HW network link
3660 * @pi: port information structure
3661 * @link_up: pointer to bool (true/false = linkup/linkdown)
3662 *
3663 * Variable link_up is true if link is up, false if link is down.
3664 * The variable link_up is invalid if status is non zero. As a
3665 * result of this call, link status reporting becomes enabled
3666 */
ice_get_link_status(struct ice_port_info * pi,bool * link_up)3667 int ice_get_link_status(struct ice_port_info *pi, bool *link_up)
3668 {
3669 struct ice_phy_info *phy_info;
3670 int status = 0;
3671
3672 if (!pi || !link_up)
3673 return -EINVAL;
3674
3675 phy_info = &pi->phy;
3676
3677 if (phy_info->get_link_info) {
3678 status = ice_update_link_info(pi);
3679
3680 if (status)
3681 ice_debug(pi->hw, ICE_DBG_LINK, "get link status error, status = %d\n",
3682 status);
3683 }
3684
3685 *link_up = phy_info->link_info.link_info & ICE_AQ_LINK_UP;
3686
3687 return status;
3688 }
3689
3690 /**
3691 * ice_aq_set_link_restart_an
3692 * @pi: pointer to the port information structure
3693 * @ena_link: if true: enable link, if false: disable link
3694 * @cd: pointer to command details structure or NULL
3695 *
3696 * Sets up the link and restarts the Auto-Negotiation over the link.
3697 */
3698 int
ice_aq_set_link_restart_an(struct ice_port_info * pi,bool ena_link,struct ice_sq_cd * cd)3699 ice_aq_set_link_restart_an(struct ice_port_info *pi, bool ena_link,
3700 struct ice_sq_cd *cd)
3701 {
3702 struct ice_aqc_restart_an *cmd;
3703 struct ice_aq_desc desc;
3704
3705 cmd = &desc.params.restart_an;
3706
3707 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_restart_an);
3708
3709 cmd->cmd_flags = ICE_AQC_RESTART_AN_LINK_RESTART;
3710 cmd->lport_num = pi->lport;
3711 if (ena_link)
3712 cmd->cmd_flags |= ICE_AQC_RESTART_AN_LINK_ENABLE;
3713 else
3714 cmd->cmd_flags &= ~ICE_AQC_RESTART_AN_LINK_ENABLE;
3715
3716 return ice_aq_send_cmd(pi->hw, &desc, NULL, 0, cd);
3717 }
3718
3719 /**
3720 * ice_aq_set_event_mask
3721 * @hw: pointer to the HW struct
3722 * @port_num: port number of the physical function
3723 * @mask: event mask to be set
3724 * @cd: pointer to command details structure or NULL
3725 *
3726 * Set event mask (0x0613)
3727 */
3728 int
ice_aq_set_event_mask(struct ice_hw * hw,u8 port_num,u16 mask,struct ice_sq_cd * cd)3729 ice_aq_set_event_mask(struct ice_hw *hw, u8 port_num, u16 mask,
3730 struct ice_sq_cd *cd)
3731 {
3732 struct ice_aqc_set_event_mask *cmd;
3733 struct ice_aq_desc desc;
3734
3735 cmd = &desc.params.set_event_mask;
3736
3737 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_event_mask);
3738
3739 cmd->lport_num = port_num;
3740
3741 cmd->event_mask = cpu_to_le16(mask);
3742 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
3743 }
3744
3745 /**
3746 * ice_aq_set_mac_loopback
3747 * @hw: pointer to the HW struct
3748 * @ena_lpbk: Enable or Disable loopback
3749 * @cd: pointer to command details structure or NULL
3750 *
3751 * Enable/disable loopback on a given port
3752 */
3753 int
ice_aq_set_mac_loopback(struct ice_hw * hw,bool ena_lpbk,struct ice_sq_cd * cd)3754 ice_aq_set_mac_loopback(struct ice_hw *hw, bool ena_lpbk, struct ice_sq_cd *cd)
3755 {
3756 struct ice_aqc_set_mac_lb *cmd;
3757 struct ice_aq_desc desc;
3758
3759 cmd = &desc.params.set_mac_lb;
3760
3761 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_mac_lb);
3762 if (ena_lpbk)
3763 cmd->lb_mode = ICE_AQ_MAC_LB_EN;
3764
3765 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
3766 }
3767
3768 /**
3769 * ice_aq_set_port_id_led
3770 * @pi: pointer to the port information
3771 * @is_orig_mode: is this LED set to original mode (by the net-list)
3772 * @cd: pointer to command details structure or NULL
3773 *
3774 * Set LED value for the given port (0x06e9)
3775 */
3776 int
ice_aq_set_port_id_led(struct ice_port_info * pi,bool is_orig_mode,struct ice_sq_cd * cd)3777 ice_aq_set_port_id_led(struct ice_port_info *pi, bool is_orig_mode,
3778 struct ice_sq_cd *cd)
3779 {
3780 struct ice_aqc_set_port_id_led *cmd;
3781 struct ice_hw *hw = pi->hw;
3782 struct ice_aq_desc desc;
3783
3784 cmd = &desc.params.set_port_id_led;
3785
3786 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_id_led);
3787
3788 if (is_orig_mode)
3789 cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_ORIG;
3790 else
3791 cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_BLINK;
3792
3793 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
3794 }
3795
3796 /**
3797 * ice_aq_get_port_options
3798 * @hw: pointer to the HW struct
3799 * @options: buffer for the resultant port options
3800 * @option_count: input - size of the buffer in port options structures,
3801 * output - number of returned port options
3802 * @lport: logical port to call the command with (optional)
3803 * @lport_valid: when false, FW uses port owned by the PF instead of lport,
3804 * when PF owns more than 1 port it must be true
3805 * @active_option_idx: index of active port option in returned buffer
3806 * @active_option_valid: active option in returned buffer is valid
3807 * @pending_option_idx: index of pending port option in returned buffer
3808 * @pending_option_valid: pending option in returned buffer is valid
3809 *
3810 * Calls Get Port Options AQC (0x06ea) and verifies result.
3811 */
3812 int
ice_aq_get_port_options(struct ice_hw * hw,struct ice_aqc_get_port_options_elem * options,u8 * option_count,u8 lport,bool lport_valid,u8 * active_option_idx,bool * active_option_valid,u8 * pending_option_idx,bool * pending_option_valid)3813 ice_aq_get_port_options(struct ice_hw *hw,
3814 struct ice_aqc_get_port_options_elem *options,
3815 u8 *option_count, u8 lport, bool lport_valid,
3816 u8 *active_option_idx, bool *active_option_valid,
3817 u8 *pending_option_idx, bool *pending_option_valid)
3818 {
3819 struct ice_aqc_get_port_options *cmd;
3820 struct ice_aq_desc desc;
3821 int status;
3822 u8 i;
3823
3824 /* options buffer shall be able to hold max returned options */
3825 if (*option_count < ICE_AQC_PORT_OPT_COUNT_M)
3826 return -EINVAL;
3827
3828 cmd = &desc.params.get_port_options;
3829 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_port_options);
3830
3831 if (lport_valid)
3832 cmd->lport_num = lport;
3833 cmd->lport_num_valid = lport_valid;
3834
3835 status = ice_aq_send_cmd(hw, &desc, options,
3836 *option_count * sizeof(*options), NULL);
3837 if (status)
3838 return status;
3839
3840 /* verify direct FW response & set output parameters */
3841 *option_count = FIELD_GET(ICE_AQC_PORT_OPT_COUNT_M,
3842 cmd->port_options_count);
3843 ice_debug(hw, ICE_DBG_PHY, "options: %x\n", *option_count);
3844 *active_option_valid = FIELD_GET(ICE_AQC_PORT_OPT_VALID,
3845 cmd->port_options);
3846 if (*active_option_valid) {
3847 *active_option_idx = FIELD_GET(ICE_AQC_PORT_OPT_ACTIVE_M,
3848 cmd->port_options);
3849 if (*active_option_idx > (*option_count - 1))
3850 return -EIO;
3851 ice_debug(hw, ICE_DBG_PHY, "active idx: %x\n",
3852 *active_option_idx);
3853 }
3854
3855 *pending_option_valid = FIELD_GET(ICE_AQC_PENDING_PORT_OPT_VALID,
3856 cmd->pending_port_option_status);
3857 if (*pending_option_valid) {
3858 *pending_option_idx = FIELD_GET(ICE_AQC_PENDING_PORT_OPT_IDX_M,
3859 cmd->pending_port_option_status);
3860 if (*pending_option_idx > (*option_count - 1))
3861 return -EIO;
3862 ice_debug(hw, ICE_DBG_PHY, "pending idx: %x\n",
3863 *pending_option_idx);
3864 }
3865
3866 /* mask output options fields */
3867 for (i = 0; i < *option_count; i++) {
3868 options[i].pmd = FIELD_GET(ICE_AQC_PORT_OPT_PMD_COUNT_M,
3869 options[i].pmd);
3870 options[i].max_lane_speed = FIELD_GET(ICE_AQC_PORT_OPT_MAX_LANE_M,
3871 options[i].max_lane_speed);
3872 ice_debug(hw, ICE_DBG_PHY, "pmds: %x max speed: %x\n",
3873 options[i].pmd, options[i].max_lane_speed);
3874 }
3875
3876 return 0;
3877 }
3878
3879 /**
3880 * ice_aq_set_port_option
3881 * @hw: pointer to the HW struct
3882 * @lport: logical port to call the command with
3883 * @lport_valid: when false, FW uses port owned by the PF instead of lport,
3884 * when PF owns more than 1 port it must be true
3885 * @new_option: new port option to be written
3886 *
3887 * Calls Set Port Options AQC (0x06eb).
3888 */
3889 int
ice_aq_set_port_option(struct ice_hw * hw,u8 lport,u8 lport_valid,u8 new_option)3890 ice_aq_set_port_option(struct ice_hw *hw, u8 lport, u8 lport_valid,
3891 u8 new_option)
3892 {
3893 struct ice_aqc_set_port_option *cmd;
3894 struct ice_aq_desc desc;
3895
3896 if (new_option > ICE_AQC_PORT_OPT_COUNT_M)
3897 return -EINVAL;
3898
3899 cmd = &desc.params.set_port_option;
3900 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_option);
3901
3902 if (lport_valid)
3903 cmd->lport_num = lport;
3904
3905 cmd->lport_num_valid = lport_valid;
3906 cmd->selected_port_option = new_option;
3907
3908 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
3909 }
3910
3911 /**
3912 * ice_aq_sff_eeprom
3913 * @hw: pointer to the HW struct
3914 * @lport: bits [7:0] = logical port, bit [8] = logical port valid
3915 * @bus_addr: I2C bus address of the eeprom (typically 0xA0, 0=topo default)
3916 * @mem_addr: I2C offset. lower 8 bits for address, 8 upper bits zero padding.
3917 * @page: QSFP page
3918 * @set_page: set or ignore the page
3919 * @data: pointer to data buffer to be read/written to the I2C device.
3920 * @length: 1-16 for read, 1 for write.
3921 * @write: 0 read, 1 for write.
3922 * @cd: pointer to command details structure or NULL
3923 *
3924 * Read/Write SFF EEPROM (0x06EE)
3925 */
3926 int
ice_aq_sff_eeprom(struct ice_hw * hw,u16 lport,u8 bus_addr,u16 mem_addr,u8 page,u8 set_page,u8 * data,u8 length,bool write,struct ice_sq_cd * cd)3927 ice_aq_sff_eeprom(struct ice_hw *hw, u16 lport, u8 bus_addr,
3928 u16 mem_addr, u8 page, u8 set_page, u8 *data, u8 length,
3929 bool write, struct ice_sq_cd *cd)
3930 {
3931 struct ice_aqc_sff_eeprom *cmd;
3932 struct ice_aq_desc desc;
3933 int status;
3934
3935 if (!data || (mem_addr & 0xff00))
3936 return -EINVAL;
3937
3938 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_sff_eeprom);
3939 cmd = &desc.params.read_write_sff_param;
3940 desc.flags = cpu_to_le16(ICE_AQ_FLAG_RD);
3941 cmd->lport_num = (u8)(lport & 0xff);
3942 cmd->lport_num_valid = (u8)((lport >> 8) & 0x01);
3943 cmd->i2c_bus_addr = cpu_to_le16(((bus_addr >> 1) &
3944 ICE_AQC_SFF_I2CBUS_7BIT_M) |
3945 ((set_page <<
3946 ICE_AQC_SFF_SET_EEPROM_PAGE_S) &
3947 ICE_AQC_SFF_SET_EEPROM_PAGE_M));
3948 cmd->i2c_mem_addr = cpu_to_le16(mem_addr & 0xff);
3949 cmd->eeprom_page = cpu_to_le16((u16)page << ICE_AQC_SFF_EEPROM_PAGE_S);
3950 if (write)
3951 cmd->i2c_bus_addr |= cpu_to_le16(ICE_AQC_SFF_IS_WRITE);
3952
3953 status = ice_aq_send_cmd(hw, &desc, data, length, cd);
3954 return status;
3955 }
3956
ice_lut_type_to_size(enum ice_lut_type type)3957 static enum ice_lut_size ice_lut_type_to_size(enum ice_lut_type type)
3958 {
3959 switch (type) {
3960 case ICE_LUT_VSI:
3961 return ICE_LUT_VSI_SIZE;
3962 case ICE_LUT_GLOBAL:
3963 return ICE_LUT_GLOBAL_SIZE;
3964 case ICE_LUT_PF:
3965 return ICE_LUT_PF_SIZE;
3966 }
3967 WARN_ONCE(1, "incorrect type passed");
3968 return ICE_LUT_VSI_SIZE;
3969 }
3970
ice_lut_size_to_flag(enum ice_lut_size size)3971 static enum ice_aqc_lut_flags ice_lut_size_to_flag(enum ice_lut_size size)
3972 {
3973 switch (size) {
3974 case ICE_LUT_VSI_SIZE:
3975 return ICE_AQC_LUT_SIZE_SMALL;
3976 case ICE_LUT_GLOBAL_SIZE:
3977 return ICE_AQC_LUT_SIZE_512;
3978 case ICE_LUT_PF_SIZE:
3979 return ICE_AQC_LUT_SIZE_2K;
3980 }
3981 WARN_ONCE(1, "incorrect size passed");
3982 return 0;
3983 }
3984
3985 /**
3986 * __ice_aq_get_set_rss_lut
3987 * @hw: pointer to the hardware structure
3988 * @params: RSS LUT parameters
3989 * @set: set true to set the table, false to get the table
3990 *
3991 * Internal function to get (0x0B05) or set (0x0B03) RSS look up table
3992 */
3993 static int
__ice_aq_get_set_rss_lut(struct ice_hw * hw,struct ice_aq_get_set_rss_lut_params * params,bool set)3994 __ice_aq_get_set_rss_lut(struct ice_hw *hw,
3995 struct ice_aq_get_set_rss_lut_params *params, bool set)
3996 {
3997 u16 opcode, vsi_id, vsi_handle = params->vsi_handle, glob_lut_idx = 0;
3998 enum ice_lut_type lut_type = params->lut_type;
3999 struct ice_aqc_get_set_rss_lut *desc_params;
4000 enum ice_aqc_lut_flags flags;
4001 enum ice_lut_size lut_size;
4002 struct ice_aq_desc desc;
4003 u8 *lut = params->lut;
4004
4005
4006 if (!lut || !ice_is_vsi_valid(hw, vsi_handle))
4007 return -EINVAL;
4008
4009 lut_size = ice_lut_type_to_size(lut_type);
4010 if (lut_size > params->lut_size)
4011 return -EINVAL;
4012 else if (set && lut_size != params->lut_size)
4013 return -EINVAL;
4014
4015 opcode = set ? ice_aqc_opc_set_rss_lut : ice_aqc_opc_get_rss_lut;
4016 ice_fill_dflt_direct_cmd_desc(&desc, opcode);
4017 if (set)
4018 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4019
4020 desc_params = &desc.params.get_set_rss_lut;
4021 vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4022 desc_params->vsi_id = cpu_to_le16(vsi_id | ICE_AQC_RSS_VSI_VALID);
4023
4024 if (lut_type == ICE_LUT_GLOBAL)
4025 glob_lut_idx = FIELD_PREP(ICE_AQC_LUT_GLOBAL_IDX,
4026 params->global_lut_id);
4027
4028 flags = lut_type | glob_lut_idx | ice_lut_size_to_flag(lut_size);
4029 desc_params->flags = cpu_to_le16(flags);
4030
4031 return ice_aq_send_cmd(hw, &desc, lut, lut_size, NULL);
4032 }
4033
4034 /**
4035 * ice_aq_get_rss_lut
4036 * @hw: pointer to the hardware structure
4037 * @get_params: RSS LUT parameters used to specify which RSS LUT to get
4038 *
4039 * get the RSS lookup table, PF or VSI type
4040 */
4041 int
ice_aq_get_rss_lut(struct ice_hw * hw,struct ice_aq_get_set_rss_lut_params * get_params)4042 ice_aq_get_rss_lut(struct ice_hw *hw, struct ice_aq_get_set_rss_lut_params *get_params)
4043 {
4044 return __ice_aq_get_set_rss_lut(hw, get_params, false);
4045 }
4046
4047 /**
4048 * ice_aq_set_rss_lut
4049 * @hw: pointer to the hardware structure
4050 * @set_params: RSS LUT parameters used to specify how to set the RSS LUT
4051 *
4052 * set the RSS lookup table, PF or VSI type
4053 */
4054 int
ice_aq_set_rss_lut(struct ice_hw * hw,struct ice_aq_get_set_rss_lut_params * set_params)4055 ice_aq_set_rss_lut(struct ice_hw *hw, struct ice_aq_get_set_rss_lut_params *set_params)
4056 {
4057 return __ice_aq_get_set_rss_lut(hw, set_params, true);
4058 }
4059
4060 /**
4061 * __ice_aq_get_set_rss_key
4062 * @hw: pointer to the HW struct
4063 * @vsi_id: VSI FW index
4064 * @key: pointer to key info struct
4065 * @set: set true to set the key, false to get the key
4066 *
4067 * get (0x0B04) or set (0x0B02) the RSS key per VSI
4068 */
4069 static int
__ice_aq_get_set_rss_key(struct ice_hw * hw,u16 vsi_id,struct ice_aqc_get_set_rss_keys * key,bool set)4070 __ice_aq_get_set_rss_key(struct ice_hw *hw, u16 vsi_id,
4071 struct ice_aqc_get_set_rss_keys *key, bool set)
4072 {
4073 struct ice_aqc_get_set_rss_key *desc_params;
4074 u16 key_size = sizeof(*key);
4075 struct ice_aq_desc desc;
4076
4077 if (set) {
4078 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_rss_key);
4079 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4080 } else {
4081 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_rss_key);
4082 }
4083
4084 desc_params = &desc.params.get_set_rss_key;
4085 desc_params->vsi_id = cpu_to_le16(vsi_id | ICE_AQC_RSS_VSI_VALID);
4086
4087 return ice_aq_send_cmd(hw, &desc, key, key_size, NULL);
4088 }
4089
4090 /**
4091 * ice_aq_get_rss_key
4092 * @hw: pointer to the HW struct
4093 * @vsi_handle: software VSI handle
4094 * @key: pointer to key info struct
4095 *
4096 * get the RSS key per VSI
4097 */
4098 int
ice_aq_get_rss_key(struct ice_hw * hw,u16 vsi_handle,struct ice_aqc_get_set_rss_keys * key)4099 ice_aq_get_rss_key(struct ice_hw *hw, u16 vsi_handle,
4100 struct ice_aqc_get_set_rss_keys *key)
4101 {
4102 if (!ice_is_vsi_valid(hw, vsi_handle) || !key)
4103 return -EINVAL;
4104
4105 return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
4106 key, false);
4107 }
4108
4109 /**
4110 * ice_aq_set_rss_key
4111 * @hw: pointer to the HW struct
4112 * @vsi_handle: software VSI handle
4113 * @keys: pointer to key info struct
4114 *
4115 * set the RSS key per VSI
4116 */
4117 int
ice_aq_set_rss_key(struct ice_hw * hw,u16 vsi_handle,struct ice_aqc_get_set_rss_keys * keys)4118 ice_aq_set_rss_key(struct ice_hw *hw, u16 vsi_handle,
4119 struct ice_aqc_get_set_rss_keys *keys)
4120 {
4121 if (!ice_is_vsi_valid(hw, vsi_handle) || !keys)
4122 return -EINVAL;
4123
4124 return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
4125 keys, true);
4126 }
4127
4128 /**
4129 * ice_aq_add_lan_txq
4130 * @hw: pointer to the hardware structure
4131 * @num_qgrps: Number of added queue groups
4132 * @qg_list: list of queue groups to be added
4133 * @buf_size: size of buffer for indirect command
4134 * @cd: pointer to command details structure or NULL
4135 *
4136 * Add Tx LAN queue (0x0C30)
4137 *
4138 * NOTE:
4139 * Prior to calling add Tx LAN queue:
4140 * Initialize the following as part of the Tx queue context:
4141 * Completion queue ID if the queue uses Completion queue, Quanta profile,
4142 * Cache profile and Packet shaper profile.
4143 *
4144 * After add Tx LAN queue AQ command is completed:
4145 * Interrupts should be associated with specific queues,
4146 * Association of Tx queue to Doorbell queue is not part of Add LAN Tx queue
4147 * flow.
4148 */
4149 static int
ice_aq_add_lan_txq(struct ice_hw * hw,u8 num_qgrps,struct ice_aqc_add_tx_qgrp * qg_list,u16 buf_size,struct ice_sq_cd * cd)4150 ice_aq_add_lan_txq(struct ice_hw *hw, u8 num_qgrps,
4151 struct ice_aqc_add_tx_qgrp *qg_list, u16 buf_size,
4152 struct ice_sq_cd *cd)
4153 {
4154 struct ice_aqc_add_tx_qgrp *list;
4155 struct ice_aqc_add_txqs *cmd;
4156 struct ice_aq_desc desc;
4157 u16 i, sum_size = 0;
4158
4159 cmd = &desc.params.add_txqs;
4160
4161 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_txqs);
4162
4163 if (!qg_list)
4164 return -EINVAL;
4165
4166 if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
4167 return -EINVAL;
4168
4169 for (i = 0, list = qg_list; i < num_qgrps; i++) {
4170 sum_size += struct_size(list, txqs, list->num_txqs);
4171 list = (struct ice_aqc_add_tx_qgrp *)(list->txqs +
4172 list->num_txqs);
4173 }
4174
4175 if (buf_size != sum_size)
4176 return -EINVAL;
4177
4178 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4179
4180 cmd->num_qgrps = num_qgrps;
4181
4182 return ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
4183 }
4184
4185 /**
4186 * ice_aq_dis_lan_txq
4187 * @hw: pointer to the hardware structure
4188 * @num_qgrps: number of groups in the list
4189 * @qg_list: the list of groups to disable
4190 * @buf_size: the total size of the qg_list buffer in bytes
4191 * @rst_src: if called due to reset, specifies the reset source
4192 * @vmvf_num: the relative VM or VF number that is undergoing the reset
4193 * @cd: pointer to command details structure or NULL
4194 *
4195 * Disable LAN Tx queue (0x0C31)
4196 */
4197 static int
ice_aq_dis_lan_txq(struct ice_hw * hw,u8 num_qgrps,struct ice_aqc_dis_txq_item * qg_list,u16 buf_size,enum ice_disq_rst_src rst_src,u16 vmvf_num,struct ice_sq_cd * cd)4198 ice_aq_dis_lan_txq(struct ice_hw *hw, u8 num_qgrps,
4199 struct ice_aqc_dis_txq_item *qg_list, u16 buf_size,
4200 enum ice_disq_rst_src rst_src, u16 vmvf_num,
4201 struct ice_sq_cd *cd)
4202 {
4203 struct ice_aqc_dis_txq_item *item;
4204 struct ice_aqc_dis_txqs *cmd;
4205 struct ice_aq_desc desc;
4206 u16 i, sz = 0;
4207 int status;
4208
4209 cmd = &desc.params.dis_txqs;
4210 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_dis_txqs);
4211
4212 /* qg_list can be NULL only in VM/VF reset flow */
4213 if (!qg_list && !rst_src)
4214 return -EINVAL;
4215
4216 if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
4217 return -EINVAL;
4218
4219 cmd->num_entries = num_qgrps;
4220
4221 cmd->vmvf_and_timeout = cpu_to_le16((5 << ICE_AQC_Q_DIS_TIMEOUT_S) &
4222 ICE_AQC_Q_DIS_TIMEOUT_M);
4223
4224 switch (rst_src) {
4225 case ICE_VM_RESET:
4226 cmd->cmd_type = ICE_AQC_Q_DIS_CMD_VM_RESET;
4227 cmd->vmvf_and_timeout |=
4228 cpu_to_le16(vmvf_num & ICE_AQC_Q_DIS_VMVF_NUM_M);
4229 break;
4230 case ICE_VF_RESET:
4231 cmd->cmd_type = ICE_AQC_Q_DIS_CMD_VF_RESET;
4232 /* In this case, FW expects vmvf_num to be absolute VF ID */
4233 cmd->vmvf_and_timeout |=
4234 cpu_to_le16((vmvf_num + hw->func_caps.vf_base_id) &
4235 ICE_AQC_Q_DIS_VMVF_NUM_M);
4236 break;
4237 case ICE_NO_RESET:
4238 default:
4239 break;
4240 }
4241
4242 /* flush pipe on time out */
4243 cmd->cmd_type |= ICE_AQC_Q_DIS_CMD_FLUSH_PIPE;
4244 /* If no queue group info, we are in a reset flow. Issue the AQ */
4245 if (!qg_list)
4246 goto do_aq;
4247
4248 /* set RD bit to indicate that command buffer is provided by the driver
4249 * and it needs to be read by the firmware
4250 */
4251 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4252
4253 for (i = 0, item = qg_list; i < num_qgrps; i++) {
4254 u16 item_size = struct_size(item, q_id, item->num_qs);
4255
4256 /* If the num of queues is even, add 2 bytes of padding */
4257 if ((item->num_qs % 2) == 0)
4258 item_size += 2;
4259
4260 sz += item_size;
4261
4262 item = (struct ice_aqc_dis_txq_item *)((u8 *)item + item_size);
4263 }
4264
4265 if (buf_size != sz)
4266 return -EINVAL;
4267
4268 do_aq:
4269 status = ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
4270 if (status) {
4271 if (!qg_list)
4272 ice_debug(hw, ICE_DBG_SCHED, "VM%d disable failed %d\n",
4273 vmvf_num, hw->adminq.sq_last_status);
4274 else
4275 ice_debug(hw, ICE_DBG_SCHED, "disable queue %d failed %d\n",
4276 le16_to_cpu(qg_list[0].q_id[0]),
4277 hw->adminq.sq_last_status);
4278 }
4279 return status;
4280 }
4281
4282 /**
4283 * ice_aq_cfg_lan_txq
4284 * @hw: pointer to the hardware structure
4285 * @buf: buffer for command
4286 * @buf_size: size of buffer in bytes
4287 * @num_qs: number of queues being configured
4288 * @oldport: origination lport
4289 * @newport: destination lport
4290 * @cd: pointer to command details structure or NULL
4291 *
4292 * Move/Configure LAN Tx queue (0x0C32)
4293 *
4294 * There is a better AQ command to use for moving nodes, so only coding
4295 * this one for configuring the node.
4296 */
4297 int
ice_aq_cfg_lan_txq(struct ice_hw * hw,struct ice_aqc_cfg_txqs_buf * buf,u16 buf_size,u16 num_qs,u8 oldport,u8 newport,struct ice_sq_cd * cd)4298 ice_aq_cfg_lan_txq(struct ice_hw *hw, struct ice_aqc_cfg_txqs_buf *buf,
4299 u16 buf_size, u16 num_qs, u8 oldport, u8 newport,
4300 struct ice_sq_cd *cd)
4301 {
4302 struct ice_aqc_cfg_txqs *cmd;
4303 struct ice_aq_desc desc;
4304 int status;
4305
4306 cmd = &desc.params.cfg_txqs;
4307 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_cfg_txqs);
4308 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4309
4310 if (!buf)
4311 return -EINVAL;
4312
4313 cmd->cmd_type = ICE_AQC_Q_CFG_TC_CHNG;
4314 cmd->num_qs = num_qs;
4315 cmd->port_num_chng = (oldport & ICE_AQC_Q_CFG_SRC_PRT_M);
4316 cmd->port_num_chng |= (newport << ICE_AQC_Q_CFG_DST_PRT_S) &
4317 ICE_AQC_Q_CFG_DST_PRT_M;
4318 cmd->time_out = (5 << ICE_AQC_Q_CFG_TIMEOUT_S) &
4319 ICE_AQC_Q_CFG_TIMEOUT_M;
4320 cmd->blocked_cgds = 0;
4321
4322 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
4323 if (status)
4324 ice_debug(hw, ICE_DBG_SCHED, "Failed to reconfigure nodes %d\n",
4325 hw->adminq.sq_last_status);
4326 return status;
4327 }
4328
4329 /**
4330 * ice_aq_add_rdma_qsets
4331 * @hw: pointer to the hardware structure
4332 * @num_qset_grps: Number of RDMA Qset groups
4333 * @qset_list: list of Qset groups to be added
4334 * @buf_size: size of buffer for indirect command
4335 * @cd: pointer to command details structure or NULL
4336 *
4337 * Add Tx RDMA Qsets (0x0C33)
4338 */
4339 static int
ice_aq_add_rdma_qsets(struct ice_hw * hw,u8 num_qset_grps,struct ice_aqc_add_rdma_qset_data * qset_list,u16 buf_size,struct ice_sq_cd * cd)4340 ice_aq_add_rdma_qsets(struct ice_hw *hw, u8 num_qset_grps,
4341 struct ice_aqc_add_rdma_qset_data *qset_list,
4342 u16 buf_size, struct ice_sq_cd *cd)
4343 {
4344 struct ice_aqc_add_rdma_qset_data *list;
4345 struct ice_aqc_add_rdma_qset *cmd;
4346 struct ice_aq_desc desc;
4347 u16 i, sum_size = 0;
4348
4349 cmd = &desc.params.add_rdma_qset;
4350
4351 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_rdma_qset);
4352
4353 if (num_qset_grps > ICE_LAN_TXQ_MAX_QGRPS)
4354 return -EINVAL;
4355
4356 for (i = 0, list = qset_list; i < num_qset_grps; i++) {
4357 u16 num_qsets = le16_to_cpu(list->num_qsets);
4358
4359 sum_size += struct_size(list, rdma_qsets, num_qsets);
4360 list = (struct ice_aqc_add_rdma_qset_data *)(list->rdma_qsets +
4361 num_qsets);
4362 }
4363
4364 if (buf_size != sum_size)
4365 return -EINVAL;
4366
4367 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
4368
4369 cmd->num_qset_grps = num_qset_grps;
4370
4371 return ice_aq_send_cmd(hw, &desc, qset_list, buf_size, cd);
4372 }
4373
4374 /* End of FW Admin Queue command wrappers */
4375
4376 /**
4377 * ice_write_byte - write a byte to a packed context structure
4378 * @src_ctx: the context structure to read from
4379 * @dest_ctx: the context to be written to
4380 * @ce_info: a description of the struct to be filled
4381 */
4382 static void
ice_write_byte(u8 * src_ctx,u8 * dest_ctx,const struct ice_ctx_ele * ce_info)4383 ice_write_byte(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
4384 {
4385 u8 src_byte, dest_byte, mask;
4386 u8 *from, *dest;
4387 u16 shift_width;
4388
4389 /* copy from the next struct field */
4390 from = src_ctx + ce_info->offset;
4391
4392 /* prepare the bits and mask */
4393 shift_width = ce_info->lsb % 8;
4394 mask = (u8)(BIT(ce_info->width) - 1);
4395
4396 src_byte = *from;
4397 src_byte &= mask;
4398
4399 /* shift to correct alignment */
4400 mask <<= shift_width;
4401 src_byte <<= shift_width;
4402
4403 /* get the current bits from the target bit string */
4404 dest = dest_ctx + (ce_info->lsb / 8);
4405
4406 memcpy(&dest_byte, dest, sizeof(dest_byte));
4407
4408 dest_byte &= ~mask; /* get the bits not changing */
4409 dest_byte |= src_byte; /* add in the new bits */
4410
4411 /* put it all back */
4412 memcpy(dest, &dest_byte, sizeof(dest_byte));
4413 }
4414
4415 /**
4416 * ice_write_word - write a word to a packed context structure
4417 * @src_ctx: the context structure to read from
4418 * @dest_ctx: the context to be written to
4419 * @ce_info: a description of the struct to be filled
4420 */
4421 static void
ice_write_word(u8 * src_ctx,u8 * dest_ctx,const struct ice_ctx_ele * ce_info)4422 ice_write_word(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
4423 {
4424 u16 src_word, mask;
4425 __le16 dest_word;
4426 u8 *from, *dest;
4427 u16 shift_width;
4428
4429 /* copy from the next struct field */
4430 from = src_ctx + ce_info->offset;
4431
4432 /* prepare the bits and mask */
4433 shift_width = ce_info->lsb % 8;
4434 mask = BIT(ce_info->width) - 1;
4435
4436 /* don't swizzle the bits until after the mask because the mask bits
4437 * will be in a different bit position on big endian machines
4438 */
4439 src_word = *(u16 *)from;
4440 src_word &= mask;
4441
4442 /* shift to correct alignment */
4443 mask <<= shift_width;
4444 src_word <<= shift_width;
4445
4446 /* get the current bits from the target bit string */
4447 dest = dest_ctx + (ce_info->lsb / 8);
4448
4449 memcpy(&dest_word, dest, sizeof(dest_word));
4450
4451 dest_word &= ~(cpu_to_le16(mask)); /* get the bits not changing */
4452 dest_word |= cpu_to_le16(src_word); /* add in the new bits */
4453
4454 /* put it all back */
4455 memcpy(dest, &dest_word, sizeof(dest_word));
4456 }
4457
4458 /**
4459 * ice_write_dword - write a dword to a packed context structure
4460 * @src_ctx: the context structure to read from
4461 * @dest_ctx: the context to be written to
4462 * @ce_info: a description of the struct to be filled
4463 */
4464 static void
ice_write_dword(u8 * src_ctx,u8 * dest_ctx,const struct ice_ctx_ele * ce_info)4465 ice_write_dword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
4466 {
4467 u32 src_dword, mask;
4468 __le32 dest_dword;
4469 u8 *from, *dest;
4470 u16 shift_width;
4471
4472 /* copy from the next struct field */
4473 from = src_ctx + ce_info->offset;
4474
4475 /* prepare the bits and mask */
4476 shift_width = ce_info->lsb % 8;
4477
4478 /* if the field width is exactly 32 on an x86 machine, then the shift
4479 * operation will not work because the SHL instructions count is masked
4480 * to 5 bits so the shift will do nothing
4481 */
4482 if (ce_info->width < 32)
4483 mask = BIT(ce_info->width) - 1;
4484 else
4485 mask = (u32)~0;
4486
4487 /* don't swizzle the bits until after the mask because the mask bits
4488 * will be in a different bit position on big endian machines
4489 */
4490 src_dword = *(u32 *)from;
4491 src_dword &= mask;
4492
4493 /* shift to correct alignment */
4494 mask <<= shift_width;
4495 src_dword <<= shift_width;
4496
4497 /* get the current bits from the target bit string */
4498 dest = dest_ctx + (ce_info->lsb / 8);
4499
4500 memcpy(&dest_dword, dest, sizeof(dest_dword));
4501
4502 dest_dword &= ~(cpu_to_le32(mask)); /* get the bits not changing */
4503 dest_dword |= cpu_to_le32(src_dword); /* add in the new bits */
4504
4505 /* put it all back */
4506 memcpy(dest, &dest_dword, sizeof(dest_dword));
4507 }
4508
4509 /**
4510 * ice_write_qword - write a qword to a packed context structure
4511 * @src_ctx: the context structure to read from
4512 * @dest_ctx: the context to be written to
4513 * @ce_info: a description of the struct to be filled
4514 */
4515 static void
ice_write_qword(u8 * src_ctx,u8 * dest_ctx,const struct ice_ctx_ele * ce_info)4516 ice_write_qword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
4517 {
4518 u64 src_qword, mask;
4519 __le64 dest_qword;
4520 u8 *from, *dest;
4521 u16 shift_width;
4522
4523 /* copy from the next struct field */
4524 from = src_ctx + ce_info->offset;
4525
4526 /* prepare the bits and mask */
4527 shift_width = ce_info->lsb % 8;
4528
4529 /* if the field width is exactly 64 on an x86 machine, then the shift
4530 * operation will not work because the SHL instructions count is masked
4531 * to 6 bits so the shift will do nothing
4532 */
4533 if (ce_info->width < 64)
4534 mask = BIT_ULL(ce_info->width) - 1;
4535 else
4536 mask = (u64)~0;
4537
4538 /* don't swizzle the bits until after the mask because the mask bits
4539 * will be in a different bit position on big endian machines
4540 */
4541 src_qword = *(u64 *)from;
4542 src_qword &= mask;
4543
4544 /* shift to correct alignment */
4545 mask <<= shift_width;
4546 src_qword <<= shift_width;
4547
4548 /* get the current bits from the target bit string */
4549 dest = dest_ctx + (ce_info->lsb / 8);
4550
4551 memcpy(&dest_qword, dest, sizeof(dest_qword));
4552
4553 dest_qword &= ~(cpu_to_le64(mask)); /* get the bits not changing */
4554 dest_qword |= cpu_to_le64(src_qword); /* add in the new bits */
4555
4556 /* put it all back */
4557 memcpy(dest, &dest_qword, sizeof(dest_qword));
4558 }
4559
4560 /**
4561 * ice_set_ctx - set context bits in packed structure
4562 * @hw: pointer to the hardware structure
4563 * @src_ctx: pointer to a generic non-packed context structure
4564 * @dest_ctx: pointer to memory for the packed structure
4565 * @ce_info: a description of the structure to be transformed
4566 */
4567 int
ice_set_ctx(struct ice_hw * hw,u8 * src_ctx,u8 * dest_ctx,const struct ice_ctx_ele * ce_info)4568 ice_set_ctx(struct ice_hw *hw, u8 *src_ctx, u8 *dest_ctx,
4569 const struct ice_ctx_ele *ce_info)
4570 {
4571 int f;
4572
4573 for (f = 0; ce_info[f].width; f++) {
4574 /* We have to deal with each element of the FW response
4575 * using the correct size so that we are correct regardless
4576 * of the endianness of the machine.
4577 */
4578 if (ce_info[f].width > (ce_info[f].size_of * BITS_PER_BYTE)) {
4579 ice_debug(hw, ICE_DBG_QCTX, "Field %d width of %d bits larger than size of %d byte(s) ... skipping write\n",
4580 f, ce_info[f].width, ce_info[f].size_of);
4581 continue;
4582 }
4583 switch (ce_info[f].size_of) {
4584 case sizeof(u8):
4585 ice_write_byte(src_ctx, dest_ctx, &ce_info[f]);
4586 break;
4587 case sizeof(u16):
4588 ice_write_word(src_ctx, dest_ctx, &ce_info[f]);
4589 break;
4590 case sizeof(u32):
4591 ice_write_dword(src_ctx, dest_ctx, &ce_info[f]);
4592 break;
4593 case sizeof(u64):
4594 ice_write_qword(src_ctx, dest_ctx, &ce_info[f]);
4595 break;
4596 default:
4597 return -EINVAL;
4598 }
4599 }
4600
4601 return 0;
4602 }
4603
4604 /**
4605 * ice_get_lan_q_ctx - get the LAN queue context for the given VSI and TC
4606 * @hw: pointer to the HW struct
4607 * @vsi_handle: software VSI handle
4608 * @tc: TC number
4609 * @q_handle: software queue handle
4610 */
4611 struct ice_q_ctx *
ice_get_lan_q_ctx(struct ice_hw * hw,u16 vsi_handle,u8 tc,u16 q_handle)4612 ice_get_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 q_handle)
4613 {
4614 struct ice_vsi_ctx *vsi;
4615 struct ice_q_ctx *q_ctx;
4616
4617 vsi = ice_get_vsi_ctx(hw, vsi_handle);
4618 if (!vsi)
4619 return NULL;
4620 if (q_handle >= vsi->num_lan_q_entries[tc])
4621 return NULL;
4622 if (!vsi->lan_q_ctx[tc])
4623 return NULL;
4624 q_ctx = vsi->lan_q_ctx[tc];
4625 return &q_ctx[q_handle];
4626 }
4627
4628 /**
4629 * ice_ena_vsi_txq
4630 * @pi: port information structure
4631 * @vsi_handle: software VSI handle
4632 * @tc: TC number
4633 * @q_handle: software queue handle
4634 * @num_qgrps: Number of added queue groups
4635 * @buf: list of queue groups to be added
4636 * @buf_size: size of buffer for indirect command
4637 * @cd: pointer to command details structure or NULL
4638 *
4639 * This function adds one LAN queue
4640 */
4641 int
ice_ena_vsi_txq(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 q_handle,u8 num_qgrps,struct ice_aqc_add_tx_qgrp * buf,u16 buf_size,struct ice_sq_cd * cd)4642 ice_ena_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 q_handle,
4643 u8 num_qgrps, struct ice_aqc_add_tx_qgrp *buf, u16 buf_size,
4644 struct ice_sq_cd *cd)
4645 {
4646 struct ice_aqc_txsched_elem_data node = { 0 };
4647 struct ice_sched_node *parent;
4648 struct ice_q_ctx *q_ctx;
4649 struct ice_hw *hw;
4650 int status;
4651
4652 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4653 return -EIO;
4654
4655 if (num_qgrps > 1 || buf->num_txqs > 1)
4656 return -ENOSPC;
4657
4658 hw = pi->hw;
4659
4660 if (!ice_is_vsi_valid(hw, vsi_handle))
4661 return -EINVAL;
4662
4663 mutex_lock(&pi->sched_lock);
4664
4665 q_ctx = ice_get_lan_q_ctx(hw, vsi_handle, tc, q_handle);
4666 if (!q_ctx) {
4667 ice_debug(hw, ICE_DBG_SCHED, "Enaq: invalid queue handle %d\n",
4668 q_handle);
4669 status = -EINVAL;
4670 goto ena_txq_exit;
4671 }
4672
4673 /* find a parent node */
4674 parent = ice_sched_get_free_qparent(pi, vsi_handle, tc,
4675 ICE_SCHED_NODE_OWNER_LAN);
4676 if (!parent) {
4677 status = -EINVAL;
4678 goto ena_txq_exit;
4679 }
4680
4681 buf->parent_teid = parent->info.node_teid;
4682 node.parent_teid = parent->info.node_teid;
4683 /* Mark that the values in the "generic" section as valid. The default
4684 * value in the "generic" section is zero. This means that :
4685 * - Scheduling mode is Bytes Per Second (BPS), indicated by Bit 0.
4686 * - 0 priority among siblings, indicated by Bit 1-3.
4687 * - WFQ, indicated by Bit 4.
4688 * - 0 Adjustment value is used in PSM credit update flow, indicated by
4689 * Bit 5-6.
4690 * - Bit 7 is reserved.
4691 * Without setting the generic section as valid in valid_sections, the
4692 * Admin queue command will fail with error code ICE_AQ_RC_EINVAL.
4693 */
4694 buf->txqs[0].info.valid_sections =
4695 ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
4696 ICE_AQC_ELEM_VALID_EIR;
4697 buf->txqs[0].info.generic = 0;
4698 buf->txqs[0].info.cir_bw.bw_profile_idx =
4699 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
4700 buf->txqs[0].info.cir_bw.bw_alloc =
4701 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
4702 buf->txqs[0].info.eir_bw.bw_profile_idx =
4703 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
4704 buf->txqs[0].info.eir_bw.bw_alloc =
4705 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
4706
4707 /* add the LAN queue */
4708 status = ice_aq_add_lan_txq(hw, num_qgrps, buf, buf_size, cd);
4709 if (status) {
4710 ice_debug(hw, ICE_DBG_SCHED, "enable queue %d failed %d\n",
4711 le16_to_cpu(buf->txqs[0].txq_id),
4712 hw->adminq.sq_last_status);
4713 goto ena_txq_exit;
4714 }
4715
4716 node.node_teid = buf->txqs[0].q_teid;
4717 node.data.elem_type = ICE_AQC_ELEM_TYPE_LEAF;
4718 q_ctx->q_handle = q_handle;
4719 q_ctx->q_teid = le32_to_cpu(node.node_teid);
4720
4721 /* add a leaf node into scheduler tree queue layer */
4722 status = ice_sched_add_node(pi, hw->num_tx_sched_layers - 1, &node, NULL);
4723 if (!status)
4724 status = ice_sched_replay_q_bw(pi, q_ctx);
4725
4726 ena_txq_exit:
4727 mutex_unlock(&pi->sched_lock);
4728 return status;
4729 }
4730
4731 /**
4732 * ice_dis_vsi_txq
4733 * @pi: port information structure
4734 * @vsi_handle: software VSI handle
4735 * @tc: TC number
4736 * @num_queues: number of queues
4737 * @q_handles: pointer to software queue handle array
4738 * @q_ids: pointer to the q_id array
4739 * @q_teids: pointer to queue node teids
4740 * @rst_src: if called due to reset, specifies the reset source
4741 * @vmvf_num: the relative VM or VF number that is undergoing the reset
4742 * @cd: pointer to command details structure or NULL
4743 *
4744 * This function removes queues and their corresponding nodes in SW DB
4745 */
4746 int
ice_dis_vsi_txq(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u8 num_queues,u16 * q_handles,u16 * q_ids,u32 * q_teids,enum ice_disq_rst_src rst_src,u16 vmvf_num,struct ice_sq_cd * cd)4747 ice_dis_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u8 num_queues,
4748 u16 *q_handles, u16 *q_ids, u32 *q_teids,
4749 enum ice_disq_rst_src rst_src, u16 vmvf_num,
4750 struct ice_sq_cd *cd)
4751 {
4752 struct ice_aqc_dis_txq_item *qg_list;
4753 struct ice_q_ctx *q_ctx;
4754 int status = -ENOENT;
4755 struct ice_hw *hw;
4756 u16 i, buf_size;
4757
4758 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4759 return -EIO;
4760
4761 hw = pi->hw;
4762
4763 if (!num_queues) {
4764 /* if queue is disabled already yet the disable queue command
4765 * has to be sent to complete the VF reset, then call
4766 * ice_aq_dis_lan_txq without any queue information
4767 */
4768 if (rst_src)
4769 return ice_aq_dis_lan_txq(hw, 0, NULL, 0, rst_src,
4770 vmvf_num, NULL);
4771 return -EIO;
4772 }
4773
4774 buf_size = struct_size(qg_list, q_id, 1);
4775 qg_list = kzalloc(buf_size, GFP_KERNEL);
4776 if (!qg_list)
4777 return -ENOMEM;
4778
4779 mutex_lock(&pi->sched_lock);
4780
4781 for (i = 0; i < num_queues; i++) {
4782 struct ice_sched_node *node;
4783
4784 node = ice_sched_find_node_by_teid(pi->root, q_teids[i]);
4785 if (!node)
4786 continue;
4787 q_ctx = ice_get_lan_q_ctx(hw, vsi_handle, tc, q_handles[i]);
4788 if (!q_ctx) {
4789 ice_debug(hw, ICE_DBG_SCHED, "invalid queue handle%d\n",
4790 q_handles[i]);
4791 continue;
4792 }
4793 if (q_ctx->q_handle != q_handles[i]) {
4794 ice_debug(hw, ICE_DBG_SCHED, "Err:handles %d %d\n",
4795 q_ctx->q_handle, q_handles[i]);
4796 continue;
4797 }
4798 qg_list->parent_teid = node->info.parent_teid;
4799 qg_list->num_qs = 1;
4800 qg_list->q_id[0] = cpu_to_le16(q_ids[i]);
4801 status = ice_aq_dis_lan_txq(hw, 1, qg_list, buf_size, rst_src,
4802 vmvf_num, cd);
4803
4804 if (status)
4805 break;
4806 ice_free_sched_node(pi, node);
4807 q_ctx->q_handle = ICE_INVAL_Q_HANDLE;
4808 q_ctx->q_teid = ICE_INVAL_TEID;
4809 }
4810 mutex_unlock(&pi->sched_lock);
4811 kfree(qg_list);
4812 return status;
4813 }
4814
4815 /**
4816 * ice_cfg_vsi_qs - configure the new/existing VSI queues
4817 * @pi: port information structure
4818 * @vsi_handle: software VSI handle
4819 * @tc_bitmap: TC bitmap
4820 * @maxqs: max queues array per TC
4821 * @owner: LAN or RDMA
4822 *
4823 * This function adds/updates the VSI queues per TC.
4824 */
4825 static int
ice_cfg_vsi_qs(struct ice_port_info * pi,u16 vsi_handle,u8 tc_bitmap,u16 * maxqs,u8 owner)4826 ice_cfg_vsi_qs(struct ice_port_info *pi, u16 vsi_handle, u8 tc_bitmap,
4827 u16 *maxqs, u8 owner)
4828 {
4829 int status = 0;
4830 u8 i;
4831
4832 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4833 return -EIO;
4834
4835 if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4836 return -EINVAL;
4837
4838 mutex_lock(&pi->sched_lock);
4839
4840 ice_for_each_traffic_class(i) {
4841 /* configuration is possible only if TC node is present */
4842 if (!ice_sched_get_tc_node(pi, i))
4843 continue;
4844
4845 status = ice_sched_cfg_vsi(pi, vsi_handle, i, maxqs[i], owner,
4846 ice_is_tc_ena(tc_bitmap, i));
4847 if (status)
4848 break;
4849 }
4850
4851 mutex_unlock(&pi->sched_lock);
4852 return status;
4853 }
4854
4855 /**
4856 * ice_cfg_vsi_lan - configure VSI LAN queues
4857 * @pi: port information structure
4858 * @vsi_handle: software VSI handle
4859 * @tc_bitmap: TC bitmap
4860 * @max_lanqs: max LAN queues array per TC
4861 *
4862 * This function adds/updates the VSI LAN queues per TC.
4863 */
4864 int
ice_cfg_vsi_lan(struct ice_port_info * pi,u16 vsi_handle,u8 tc_bitmap,u16 * max_lanqs)4865 ice_cfg_vsi_lan(struct ice_port_info *pi, u16 vsi_handle, u8 tc_bitmap,
4866 u16 *max_lanqs)
4867 {
4868 return ice_cfg_vsi_qs(pi, vsi_handle, tc_bitmap, max_lanqs,
4869 ICE_SCHED_NODE_OWNER_LAN);
4870 }
4871
4872 /**
4873 * ice_cfg_vsi_rdma - configure the VSI RDMA queues
4874 * @pi: port information structure
4875 * @vsi_handle: software VSI handle
4876 * @tc_bitmap: TC bitmap
4877 * @max_rdmaqs: max RDMA queues array per TC
4878 *
4879 * This function adds/updates the VSI RDMA queues per TC.
4880 */
4881 int
ice_cfg_vsi_rdma(struct ice_port_info * pi,u16 vsi_handle,u16 tc_bitmap,u16 * max_rdmaqs)4882 ice_cfg_vsi_rdma(struct ice_port_info *pi, u16 vsi_handle, u16 tc_bitmap,
4883 u16 *max_rdmaqs)
4884 {
4885 return ice_cfg_vsi_qs(pi, vsi_handle, tc_bitmap, max_rdmaqs,
4886 ICE_SCHED_NODE_OWNER_RDMA);
4887 }
4888
4889 /**
4890 * ice_ena_vsi_rdma_qset
4891 * @pi: port information structure
4892 * @vsi_handle: software VSI handle
4893 * @tc: TC number
4894 * @rdma_qset: pointer to RDMA Qset
4895 * @num_qsets: number of RDMA Qsets
4896 * @qset_teid: pointer to Qset node TEIDs
4897 *
4898 * This function adds RDMA Qset
4899 */
4900 int
ice_ena_vsi_rdma_qset(struct ice_port_info * pi,u16 vsi_handle,u8 tc,u16 * rdma_qset,u16 num_qsets,u32 * qset_teid)4901 ice_ena_vsi_rdma_qset(struct ice_port_info *pi, u16 vsi_handle, u8 tc,
4902 u16 *rdma_qset, u16 num_qsets, u32 *qset_teid)
4903 {
4904 struct ice_aqc_txsched_elem_data node = { 0 };
4905 struct ice_aqc_add_rdma_qset_data *buf;
4906 struct ice_sched_node *parent;
4907 struct ice_hw *hw;
4908 u16 i, buf_size;
4909 int ret;
4910
4911 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4912 return -EIO;
4913 hw = pi->hw;
4914
4915 if (!ice_is_vsi_valid(hw, vsi_handle))
4916 return -EINVAL;
4917
4918 buf_size = struct_size(buf, rdma_qsets, num_qsets);
4919 buf = kzalloc(buf_size, GFP_KERNEL);
4920 if (!buf)
4921 return -ENOMEM;
4922 mutex_lock(&pi->sched_lock);
4923
4924 parent = ice_sched_get_free_qparent(pi, vsi_handle, tc,
4925 ICE_SCHED_NODE_OWNER_RDMA);
4926 if (!parent) {
4927 ret = -EINVAL;
4928 goto rdma_error_exit;
4929 }
4930 buf->parent_teid = parent->info.node_teid;
4931 node.parent_teid = parent->info.node_teid;
4932
4933 buf->num_qsets = cpu_to_le16(num_qsets);
4934 for (i = 0; i < num_qsets; i++) {
4935 buf->rdma_qsets[i].tx_qset_id = cpu_to_le16(rdma_qset[i]);
4936 buf->rdma_qsets[i].info.valid_sections =
4937 ICE_AQC_ELEM_VALID_GENERIC | ICE_AQC_ELEM_VALID_CIR |
4938 ICE_AQC_ELEM_VALID_EIR;
4939 buf->rdma_qsets[i].info.generic = 0;
4940 buf->rdma_qsets[i].info.cir_bw.bw_profile_idx =
4941 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
4942 buf->rdma_qsets[i].info.cir_bw.bw_alloc =
4943 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
4944 buf->rdma_qsets[i].info.eir_bw.bw_profile_idx =
4945 cpu_to_le16(ICE_SCHED_DFLT_RL_PROF_ID);
4946 buf->rdma_qsets[i].info.eir_bw.bw_alloc =
4947 cpu_to_le16(ICE_SCHED_DFLT_BW_WT);
4948 }
4949 ret = ice_aq_add_rdma_qsets(hw, 1, buf, buf_size, NULL);
4950 if (ret) {
4951 ice_debug(hw, ICE_DBG_RDMA, "add RDMA qset failed\n");
4952 goto rdma_error_exit;
4953 }
4954 node.data.elem_type = ICE_AQC_ELEM_TYPE_LEAF;
4955 for (i = 0; i < num_qsets; i++) {
4956 node.node_teid = buf->rdma_qsets[i].qset_teid;
4957 ret = ice_sched_add_node(pi, hw->num_tx_sched_layers - 1,
4958 &node, NULL);
4959 if (ret)
4960 break;
4961 qset_teid[i] = le32_to_cpu(node.node_teid);
4962 }
4963 rdma_error_exit:
4964 mutex_unlock(&pi->sched_lock);
4965 kfree(buf);
4966 return ret;
4967 }
4968
4969 /**
4970 * ice_dis_vsi_rdma_qset - free RDMA resources
4971 * @pi: port_info struct
4972 * @count: number of RDMA Qsets to free
4973 * @qset_teid: TEID of Qset node
4974 * @q_id: list of queue IDs being disabled
4975 */
4976 int
ice_dis_vsi_rdma_qset(struct ice_port_info * pi,u16 count,u32 * qset_teid,u16 * q_id)4977 ice_dis_vsi_rdma_qset(struct ice_port_info *pi, u16 count, u32 *qset_teid,
4978 u16 *q_id)
4979 {
4980 struct ice_aqc_dis_txq_item *qg_list;
4981 struct ice_hw *hw;
4982 int status = 0;
4983 u16 qg_size;
4984 int i;
4985
4986 if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4987 return -EIO;
4988
4989 hw = pi->hw;
4990
4991 qg_size = struct_size(qg_list, q_id, 1);
4992 qg_list = kzalloc(qg_size, GFP_KERNEL);
4993 if (!qg_list)
4994 return -ENOMEM;
4995
4996 mutex_lock(&pi->sched_lock);
4997
4998 for (i = 0; i < count; i++) {
4999 struct ice_sched_node *node;
5000
5001 node = ice_sched_find_node_by_teid(pi->root, qset_teid[i]);
5002 if (!node)
5003 continue;
5004
5005 qg_list->parent_teid = node->info.parent_teid;
5006 qg_list->num_qs = 1;
5007 qg_list->q_id[0] =
5008 cpu_to_le16(q_id[i] |
5009 ICE_AQC_Q_DIS_BUF_ELEM_TYPE_RDMA_QSET);
5010
5011 status = ice_aq_dis_lan_txq(hw, 1, qg_list, qg_size,
5012 ICE_NO_RESET, 0, NULL);
5013 if (status)
5014 break;
5015
5016 ice_free_sched_node(pi, node);
5017 }
5018
5019 mutex_unlock(&pi->sched_lock);
5020 kfree(qg_list);
5021 return status;
5022 }
5023
5024 /**
5025 * ice_replay_pre_init - replay pre initialization
5026 * @hw: pointer to the HW struct
5027 *
5028 * Initializes required config data for VSI, FD, ACL, and RSS before replay.
5029 */
ice_replay_pre_init(struct ice_hw * hw)5030 static int ice_replay_pre_init(struct ice_hw *hw)
5031 {
5032 struct ice_switch_info *sw = hw->switch_info;
5033 u8 i;
5034
5035 /* Delete old entries from replay filter list head if there is any */
5036 ice_rm_all_sw_replay_rule_info(hw);
5037 /* In start of replay, move entries into replay_rules list, it
5038 * will allow adding rules entries back to filt_rules list,
5039 * which is operational list.
5040 */
5041 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
5042 list_replace_init(&sw->recp_list[i].filt_rules,
5043 &sw->recp_list[i].filt_replay_rules);
5044 ice_sched_replay_agg_vsi_preinit(hw);
5045
5046 return 0;
5047 }
5048
5049 /**
5050 * ice_replay_vsi - replay VSI configuration
5051 * @hw: pointer to the HW struct
5052 * @vsi_handle: driver VSI handle
5053 *
5054 * Restore all VSI configuration after reset. It is required to call this
5055 * function with main VSI first.
5056 */
ice_replay_vsi(struct ice_hw * hw,u16 vsi_handle)5057 int ice_replay_vsi(struct ice_hw *hw, u16 vsi_handle)
5058 {
5059 int status;
5060
5061 if (!ice_is_vsi_valid(hw, vsi_handle))
5062 return -EINVAL;
5063
5064 /* Replay pre-initialization if there is any */
5065 if (vsi_handle == ICE_MAIN_VSI_HANDLE) {
5066 status = ice_replay_pre_init(hw);
5067 if (status)
5068 return status;
5069 }
5070 /* Replay per VSI all RSS configurations */
5071 status = ice_replay_rss_cfg(hw, vsi_handle);
5072 if (status)
5073 return status;
5074 /* Replay per VSI all filters */
5075 status = ice_replay_vsi_all_fltr(hw, vsi_handle);
5076 if (!status)
5077 status = ice_replay_vsi_agg(hw, vsi_handle);
5078 return status;
5079 }
5080
5081 /**
5082 * ice_replay_post - post replay configuration cleanup
5083 * @hw: pointer to the HW struct
5084 *
5085 * Post replay cleanup.
5086 */
ice_replay_post(struct ice_hw * hw)5087 void ice_replay_post(struct ice_hw *hw)
5088 {
5089 /* Delete old entries from replay filter list head */
5090 ice_rm_all_sw_replay_rule_info(hw);
5091 ice_sched_replay_agg(hw);
5092 }
5093
5094 /**
5095 * ice_stat_update40 - read 40 bit stat from the chip and update stat values
5096 * @hw: ptr to the hardware info
5097 * @reg: offset of 64 bit HW register to read from
5098 * @prev_stat_loaded: bool to specify if previous stats are loaded
5099 * @prev_stat: ptr to previous loaded stat value
5100 * @cur_stat: ptr to current stat value
5101 */
5102 void
ice_stat_update40(struct ice_hw * hw,u32 reg,bool prev_stat_loaded,u64 * prev_stat,u64 * cur_stat)5103 ice_stat_update40(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
5104 u64 *prev_stat, u64 *cur_stat)
5105 {
5106 u64 new_data = rd64(hw, reg) & (BIT_ULL(40) - 1);
5107
5108 /* device stats are not reset at PFR, they likely will not be zeroed
5109 * when the driver starts. Thus, save the value from the first read
5110 * without adding to the statistic value so that we report stats which
5111 * count up from zero.
5112 */
5113 if (!prev_stat_loaded) {
5114 *prev_stat = new_data;
5115 return;
5116 }
5117
5118 /* Calculate the difference between the new and old values, and then
5119 * add it to the software stat value.
5120 */
5121 if (new_data >= *prev_stat)
5122 *cur_stat += new_data - *prev_stat;
5123 else
5124 /* to manage the potential roll-over */
5125 *cur_stat += (new_data + BIT_ULL(40)) - *prev_stat;
5126
5127 /* Update the previously stored value to prepare for next read */
5128 *prev_stat = new_data;
5129 }
5130
5131 /**
5132 * ice_stat_update32 - read 32 bit stat from the chip and update stat values
5133 * @hw: ptr to the hardware info
5134 * @reg: offset of HW register to read from
5135 * @prev_stat_loaded: bool to specify if previous stats are loaded
5136 * @prev_stat: ptr to previous loaded stat value
5137 * @cur_stat: ptr to current stat value
5138 */
5139 void
ice_stat_update32(struct ice_hw * hw,u32 reg,bool prev_stat_loaded,u64 * prev_stat,u64 * cur_stat)5140 ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
5141 u64 *prev_stat, u64 *cur_stat)
5142 {
5143 u32 new_data;
5144
5145 new_data = rd32(hw, reg);
5146
5147 /* device stats are not reset at PFR, they likely will not be zeroed
5148 * when the driver starts. Thus, save the value from the first read
5149 * without adding to the statistic value so that we report stats which
5150 * count up from zero.
5151 */
5152 if (!prev_stat_loaded) {
5153 *prev_stat = new_data;
5154 return;
5155 }
5156
5157 /* Calculate the difference between the new and old values, and then
5158 * add it to the software stat value.
5159 */
5160 if (new_data >= *prev_stat)
5161 *cur_stat += new_data - *prev_stat;
5162 else
5163 /* to manage the potential roll-over */
5164 *cur_stat += (new_data + BIT_ULL(32)) - *prev_stat;
5165
5166 /* Update the previously stored value to prepare for next read */
5167 *prev_stat = new_data;
5168 }
5169
5170 /**
5171 * ice_sched_query_elem - query element information from HW
5172 * @hw: pointer to the HW struct
5173 * @node_teid: node TEID to be queried
5174 * @buf: buffer to element information
5175 *
5176 * This function queries HW element information
5177 */
5178 int
ice_sched_query_elem(struct ice_hw * hw,u32 node_teid,struct ice_aqc_txsched_elem_data * buf)5179 ice_sched_query_elem(struct ice_hw *hw, u32 node_teid,
5180 struct ice_aqc_txsched_elem_data *buf)
5181 {
5182 u16 buf_size, num_elem_ret = 0;
5183 int status;
5184
5185 buf_size = sizeof(*buf);
5186 memset(buf, 0, buf_size);
5187 buf->node_teid = cpu_to_le32(node_teid);
5188 status = ice_aq_query_sched_elems(hw, 1, buf, buf_size, &num_elem_ret,
5189 NULL);
5190 if (status || num_elem_ret != 1)
5191 ice_debug(hw, ICE_DBG_SCHED, "query element failed\n");
5192 return status;
5193 }
5194
5195 /**
5196 * ice_aq_read_i2c
5197 * @hw: pointer to the hw struct
5198 * @topo_addr: topology address for a device to communicate with
5199 * @bus_addr: 7-bit I2C bus address
5200 * @addr: I2C memory address (I2C offset) with up to 16 bits
5201 * @params: I2C parameters: bit [7] - Repeated start,
5202 * bits [6:5] data offset size,
5203 * bit [4] - I2C address type,
5204 * bits [3:0] - data size to read (0-16 bytes)
5205 * @data: pointer to data (0 to 16 bytes) to be read from the I2C device
5206 * @cd: pointer to command details structure or NULL
5207 *
5208 * Read I2C (0x06E2)
5209 */
5210 int
ice_aq_read_i2c(struct ice_hw * hw,struct ice_aqc_link_topo_addr topo_addr,u16 bus_addr,__le16 addr,u8 params,u8 * data,struct ice_sq_cd * cd)5211 ice_aq_read_i2c(struct ice_hw *hw, struct ice_aqc_link_topo_addr topo_addr,
5212 u16 bus_addr, __le16 addr, u8 params, u8 *data,
5213 struct ice_sq_cd *cd)
5214 {
5215 struct ice_aq_desc desc = { 0 };
5216 struct ice_aqc_i2c *cmd;
5217 u8 data_size;
5218 int status;
5219
5220 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_read_i2c);
5221 cmd = &desc.params.read_write_i2c;
5222
5223 if (!data)
5224 return -EINVAL;
5225
5226 data_size = FIELD_GET(ICE_AQC_I2C_DATA_SIZE_M, params);
5227
5228 cmd->i2c_bus_addr = cpu_to_le16(bus_addr);
5229 cmd->topo_addr = topo_addr;
5230 cmd->i2c_params = params;
5231 cmd->i2c_addr = addr;
5232
5233 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5234 if (!status) {
5235 struct ice_aqc_read_i2c_resp *resp;
5236 u8 i;
5237
5238 resp = &desc.params.read_i2c_resp;
5239 for (i = 0; i < data_size; i++) {
5240 *data = resp->i2c_data[i];
5241 data++;
5242 }
5243 }
5244
5245 return status;
5246 }
5247
5248 /**
5249 * ice_aq_write_i2c
5250 * @hw: pointer to the hw struct
5251 * @topo_addr: topology address for a device to communicate with
5252 * @bus_addr: 7-bit I2C bus address
5253 * @addr: I2C memory address (I2C offset) with up to 16 bits
5254 * @params: I2C parameters: bit [4] - I2C address type, bits [3:0] - data size to write (0-7 bytes)
5255 * @data: pointer to data (0 to 4 bytes) to be written to the I2C device
5256 * @cd: pointer to command details structure or NULL
5257 *
5258 * Write I2C (0x06E3)
5259 *
5260 * * Return:
5261 * * 0 - Successful write to the i2c device
5262 * * -EINVAL - Data size greater than 4 bytes
5263 * * -EIO - FW error
5264 */
5265 int
ice_aq_write_i2c(struct ice_hw * hw,struct ice_aqc_link_topo_addr topo_addr,u16 bus_addr,__le16 addr,u8 params,const u8 * data,struct ice_sq_cd * cd)5266 ice_aq_write_i2c(struct ice_hw *hw, struct ice_aqc_link_topo_addr topo_addr,
5267 u16 bus_addr, __le16 addr, u8 params, const u8 *data,
5268 struct ice_sq_cd *cd)
5269 {
5270 struct ice_aq_desc desc = { 0 };
5271 struct ice_aqc_i2c *cmd;
5272 u8 data_size;
5273
5274 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_write_i2c);
5275 cmd = &desc.params.read_write_i2c;
5276
5277 data_size = FIELD_GET(ICE_AQC_I2C_DATA_SIZE_M, params);
5278
5279 /* data_size limited to 4 */
5280 if (data_size > 4)
5281 return -EINVAL;
5282
5283 cmd->i2c_bus_addr = cpu_to_le16(bus_addr);
5284 cmd->topo_addr = topo_addr;
5285 cmd->i2c_params = params;
5286 cmd->i2c_addr = addr;
5287
5288 memcpy(cmd->i2c_data, data, data_size);
5289
5290 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5291 }
5292
5293 /**
5294 * ice_aq_set_driver_param - Set driver parameter to share via firmware
5295 * @hw: pointer to the HW struct
5296 * @idx: parameter index to set
5297 * @value: the value to set the parameter to
5298 * @cd: pointer to command details structure or NULL
5299 *
5300 * Set the value of one of the software defined parameters. All PFs connected
5301 * to this device can read the value using ice_aq_get_driver_param.
5302 *
5303 * Note that firmware provides no synchronization or locking, and will not
5304 * save the parameter value during a device reset. It is expected that
5305 * a single PF will write the parameter value, while all other PFs will only
5306 * read it.
5307 */
5308 int
ice_aq_set_driver_param(struct ice_hw * hw,enum ice_aqc_driver_params idx,u32 value,struct ice_sq_cd * cd)5309 ice_aq_set_driver_param(struct ice_hw *hw, enum ice_aqc_driver_params idx,
5310 u32 value, struct ice_sq_cd *cd)
5311 {
5312 struct ice_aqc_driver_shared_params *cmd;
5313 struct ice_aq_desc desc;
5314
5315 if (idx >= ICE_AQC_DRIVER_PARAM_MAX)
5316 return -EIO;
5317
5318 cmd = &desc.params.drv_shared_params;
5319
5320 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_driver_shared_params);
5321
5322 cmd->set_or_get_op = ICE_AQC_DRIVER_PARAM_SET;
5323 cmd->param_indx = idx;
5324 cmd->param_val = cpu_to_le32(value);
5325
5326 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5327 }
5328
5329 /**
5330 * ice_aq_get_driver_param - Get driver parameter shared via firmware
5331 * @hw: pointer to the HW struct
5332 * @idx: parameter index to set
5333 * @value: storage to return the shared parameter
5334 * @cd: pointer to command details structure or NULL
5335 *
5336 * Get the value of one of the software defined parameters.
5337 *
5338 * Note that firmware provides no synchronization or locking. It is expected
5339 * that only a single PF will write a given parameter.
5340 */
5341 int
ice_aq_get_driver_param(struct ice_hw * hw,enum ice_aqc_driver_params idx,u32 * value,struct ice_sq_cd * cd)5342 ice_aq_get_driver_param(struct ice_hw *hw, enum ice_aqc_driver_params idx,
5343 u32 *value, struct ice_sq_cd *cd)
5344 {
5345 struct ice_aqc_driver_shared_params *cmd;
5346 struct ice_aq_desc desc;
5347 int status;
5348
5349 if (idx >= ICE_AQC_DRIVER_PARAM_MAX)
5350 return -EIO;
5351
5352 cmd = &desc.params.drv_shared_params;
5353
5354 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_driver_shared_params);
5355
5356 cmd->set_or_get_op = ICE_AQC_DRIVER_PARAM_GET;
5357 cmd->param_indx = idx;
5358
5359 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5360 if (status)
5361 return status;
5362
5363 *value = le32_to_cpu(cmd->param_val);
5364
5365 return 0;
5366 }
5367
5368 /**
5369 * ice_aq_set_gpio
5370 * @hw: pointer to the hw struct
5371 * @gpio_ctrl_handle: GPIO controller node handle
5372 * @pin_idx: IO Number of the GPIO that needs to be set
5373 * @value: SW provide IO value to set in the LSB
5374 * @cd: pointer to command details structure or NULL
5375 *
5376 * Sends 0x06EC AQ command to set the GPIO pin state that's part of the topology
5377 */
5378 int
ice_aq_set_gpio(struct ice_hw * hw,u16 gpio_ctrl_handle,u8 pin_idx,bool value,struct ice_sq_cd * cd)5379 ice_aq_set_gpio(struct ice_hw *hw, u16 gpio_ctrl_handle, u8 pin_idx, bool value,
5380 struct ice_sq_cd *cd)
5381 {
5382 struct ice_aqc_gpio *cmd;
5383 struct ice_aq_desc desc;
5384
5385 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_gpio);
5386 cmd = &desc.params.read_write_gpio;
5387 cmd->gpio_ctrl_handle = cpu_to_le16(gpio_ctrl_handle);
5388 cmd->gpio_num = pin_idx;
5389 cmd->gpio_val = value ? 1 : 0;
5390
5391 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5392 }
5393
5394 /**
5395 * ice_aq_get_gpio
5396 * @hw: pointer to the hw struct
5397 * @gpio_ctrl_handle: GPIO controller node handle
5398 * @pin_idx: IO Number of the GPIO that needs to be set
5399 * @value: IO value read
5400 * @cd: pointer to command details structure or NULL
5401 *
5402 * Sends 0x06ED AQ command to get the value of a GPIO signal which is part of
5403 * the topology
5404 */
5405 int
ice_aq_get_gpio(struct ice_hw * hw,u16 gpio_ctrl_handle,u8 pin_idx,bool * value,struct ice_sq_cd * cd)5406 ice_aq_get_gpio(struct ice_hw *hw, u16 gpio_ctrl_handle, u8 pin_idx,
5407 bool *value, struct ice_sq_cd *cd)
5408 {
5409 struct ice_aqc_gpio *cmd;
5410 struct ice_aq_desc desc;
5411 int status;
5412
5413 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_gpio);
5414 cmd = &desc.params.read_write_gpio;
5415 cmd->gpio_ctrl_handle = cpu_to_le16(gpio_ctrl_handle);
5416 cmd->gpio_num = pin_idx;
5417
5418 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
5419 if (status)
5420 return status;
5421
5422 *value = !!cmd->gpio_val;
5423 return 0;
5424 }
5425
5426 /**
5427 * ice_is_fw_api_min_ver
5428 * @hw: pointer to the hardware structure
5429 * @maj: major version
5430 * @min: minor version
5431 * @patch: patch version
5432 *
5433 * Checks if the firmware API is minimum version
5434 */
ice_is_fw_api_min_ver(struct ice_hw * hw,u8 maj,u8 min,u8 patch)5435 static bool ice_is_fw_api_min_ver(struct ice_hw *hw, u8 maj, u8 min, u8 patch)
5436 {
5437 if (hw->api_maj_ver == maj) {
5438 if (hw->api_min_ver > min)
5439 return true;
5440 if (hw->api_min_ver == min && hw->api_patch >= patch)
5441 return true;
5442 } else if (hw->api_maj_ver > maj) {
5443 return true;
5444 }
5445
5446 return false;
5447 }
5448
5449 /**
5450 * ice_fw_supports_link_override
5451 * @hw: pointer to the hardware structure
5452 *
5453 * Checks if the firmware supports link override
5454 */
ice_fw_supports_link_override(struct ice_hw * hw)5455 bool ice_fw_supports_link_override(struct ice_hw *hw)
5456 {
5457 return ice_is_fw_api_min_ver(hw, ICE_FW_API_LINK_OVERRIDE_MAJ,
5458 ICE_FW_API_LINK_OVERRIDE_MIN,
5459 ICE_FW_API_LINK_OVERRIDE_PATCH);
5460 }
5461
5462 /**
5463 * ice_get_link_default_override
5464 * @ldo: pointer to the link default override struct
5465 * @pi: pointer to the port info struct
5466 *
5467 * Gets the link default override for a port
5468 */
5469 int
ice_get_link_default_override(struct ice_link_default_override_tlv * ldo,struct ice_port_info * pi)5470 ice_get_link_default_override(struct ice_link_default_override_tlv *ldo,
5471 struct ice_port_info *pi)
5472 {
5473 u16 i, tlv, tlv_len, tlv_start, buf, offset;
5474 struct ice_hw *hw = pi->hw;
5475 int status;
5476
5477 status = ice_get_pfa_module_tlv(hw, &tlv, &tlv_len,
5478 ICE_SR_LINK_DEFAULT_OVERRIDE_PTR);
5479 if (status) {
5480 ice_debug(hw, ICE_DBG_INIT, "Failed to read link override TLV.\n");
5481 return status;
5482 }
5483
5484 /* Each port has its own config; calculate for our port */
5485 tlv_start = tlv + pi->lport * ICE_SR_PFA_LINK_OVERRIDE_WORDS +
5486 ICE_SR_PFA_LINK_OVERRIDE_OFFSET;
5487
5488 /* link options first */
5489 status = ice_read_sr_word(hw, tlv_start, &buf);
5490 if (status) {
5491 ice_debug(hw, ICE_DBG_INIT, "Failed to read override link options.\n");
5492 return status;
5493 }
5494 ldo->options = buf & ICE_LINK_OVERRIDE_OPT_M;
5495 ldo->phy_config = (buf & ICE_LINK_OVERRIDE_PHY_CFG_M) >>
5496 ICE_LINK_OVERRIDE_PHY_CFG_S;
5497
5498 /* link PHY config */
5499 offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_FEC_OFFSET;
5500 status = ice_read_sr_word(hw, offset, &buf);
5501 if (status) {
5502 ice_debug(hw, ICE_DBG_INIT, "Failed to read override phy config.\n");
5503 return status;
5504 }
5505 ldo->fec_options = buf & ICE_LINK_OVERRIDE_FEC_OPT_M;
5506
5507 /* PHY types low */
5508 offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_PHY_OFFSET;
5509 for (i = 0; i < ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS; i++) {
5510 status = ice_read_sr_word(hw, (offset + i), &buf);
5511 if (status) {
5512 ice_debug(hw, ICE_DBG_INIT, "Failed to read override link options.\n");
5513 return status;
5514 }
5515 /* shift 16 bits at a time to fill 64 bits */
5516 ldo->phy_type_low |= ((u64)buf << (i * 16));
5517 }
5518
5519 /* PHY types high */
5520 offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_PHY_OFFSET +
5521 ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS;
5522 for (i = 0; i < ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS; i++) {
5523 status = ice_read_sr_word(hw, (offset + i), &buf);
5524 if (status) {
5525 ice_debug(hw, ICE_DBG_INIT, "Failed to read override link options.\n");
5526 return status;
5527 }
5528 /* shift 16 bits at a time to fill 64 bits */
5529 ldo->phy_type_high |= ((u64)buf << (i * 16));
5530 }
5531
5532 return status;
5533 }
5534
5535 /**
5536 * ice_is_phy_caps_an_enabled - check if PHY capabilities autoneg is enabled
5537 * @caps: get PHY capability data
5538 */
ice_is_phy_caps_an_enabled(struct ice_aqc_get_phy_caps_data * caps)5539 bool ice_is_phy_caps_an_enabled(struct ice_aqc_get_phy_caps_data *caps)
5540 {
5541 if (caps->caps & ICE_AQC_PHY_AN_MODE ||
5542 caps->low_power_ctrl_an & (ICE_AQC_PHY_AN_EN_CLAUSE28 |
5543 ICE_AQC_PHY_AN_EN_CLAUSE73 |
5544 ICE_AQC_PHY_AN_EN_CLAUSE37))
5545 return true;
5546
5547 return false;
5548 }
5549
5550 /**
5551 * ice_aq_set_lldp_mib - Set the LLDP MIB
5552 * @hw: pointer to the HW struct
5553 * @mib_type: Local, Remote or both Local and Remote MIBs
5554 * @buf: pointer to the caller-supplied buffer to store the MIB block
5555 * @buf_size: size of the buffer (in bytes)
5556 * @cd: pointer to command details structure or NULL
5557 *
5558 * Set the LLDP MIB. (0x0A08)
5559 */
5560 int
ice_aq_set_lldp_mib(struct ice_hw * hw,u8 mib_type,void * buf,u16 buf_size,struct ice_sq_cd * cd)5561 ice_aq_set_lldp_mib(struct ice_hw *hw, u8 mib_type, void *buf, u16 buf_size,
5562 struct ice_sq_cd *cd)
5563 {
5564 struct ice_aqc_lldp_set_local_mib *cmd;
5565 struct ice_aq_desc desc;
5566
5567 cmd = &desc.params.lldp_set_mib;
5568
5569 if (buf_size == 0 || !buf)
5570 return -EINVAL;
5571
5572 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_set_local_mib);
5573
5574 desc.flags |= cpu_to_le16((u16)ICE_AQ_FLAG_RD);
5575 desc.datalen = cpu_to_le16(buf_size);
5576
5577 cmd->type = mib_type;
5578 cmd->length = cpu_to_le16(buf_size);
5579
5580 return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
5581 }
5582
5583 /**
5584 * ice_fw_supports_lldp_fltr_ctrl - check NVM version supports lldp_fltr_ctrl
5585 * @hw: pointer to HW struct
5586 */
ice_fw_supports_lldp_fltr_ctrl(struct ice_hw * hw)5587 bool ice_fw_supports_lldp_fltr_ctrl(struct ice_hw *hw)
5588 {
5589 if (hw->mac_type != ICE_MAC_E810)
5590 return false;
5591
5592 return ice_is_fw_api_min_ver(hw, ICE_FW_API_LLDP_FLTR_MAJ,
5593 ICE_FW_API_LLDP_FLTR_MIN,
5594 ICE_FW_API_LLDP_FLTR_PATCH);
5595 }
5596
5597 /**
5598 * ice_lldp_fltr_add_remove - add or remove a LLDP Rx switch filter
5599 * @hw: pointer to HW struct
5600 * @vsi_num: absolute HW index for VSI
5601 * @add: boolean for if adding or removing a filter
5602 */
5603 int
ice_lldp_fltr_add_remove(struct ice_hw * hw,u16 vsi_num,bool add)5604 ice_lldp_fltr_add_remove(struct ice_hw *hw, u16 vsi_num, bool add)
5605 {
5606 struct ice_aqc_lldp_filter_ctrl *cmd;
5607 struct ice_aq_desc desc;
5608
5609 cmd = &desc.params.lldp_filter_ctrl;
5610
5611 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_filter_ctrl);
5612
5613 if (add)
5614 cmd->cmd_flags = ICE_AQC_LLDP_FILTER_ACTION_ADD;
5615 else
5616 cmd->cmd_flags = ICE_AQC_LLDP_FILTER_ACTION_DELETE;
5617
5618 cmd->vsi_num = cpu_to_le16(vsi_num);
5619
5620 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
5621 }
5622
5623 /**
5624 * ice_lldp_execute_pending_mib - execute LLDP pending MIB request
5625 * @hw: pointer to HW struct
5626 */
ice_lldp_execute_pending_mib(struct ice_hw * hw)5627 int ice_lldp_execute_pending_mib(struct ice_hw *hw)
5628 {
5629 struct ice_aq_desc desc;
5630
5631 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_execute_pending_mib);
5632
5633 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
5634 }
5635
5636 /**
5637 * ice_fw_supports_report_dflt_cfg
5638 * @hw: pointer to the hardware structure
5639 *
5640 * Checks if the firmware supports report default configuration
5641 */
ice_fw_supports_report_dflt_cfg(struct ice_hw * hw)5642 bool ice_fw_supports_report_dflt_cfg(struct ice_hw *hw)
5643 {
5644 return ice_is_fw_api_min_ver(hw, ICE_FW_API_REPORT_DFLT_CFG_MAJ,
5645 ICE_FW_API_REPORT_DFLT_CFG_MIN,
5646 ICE_FW_API_REPORT_DFLT_CFG_PATCH);
5647 }
5648
5649 /* each of the indexes into the following array match the speed of a return
5650 * value from the list of AQ returned speeds like the range:
5651 * ICE_AQ_LINK_SPEED_10MB .. ICE_AQ_LINK_SPEED_100GB excluding
5652 * ICE_AQ_LINK_SPEED_UNKNOWN which is BIT(15) and maps to BIT(14) in this
5653 * array. The array is defined as 15 elements long because the link_speed
5654 * returned by the firmware is a 16 bit * value, but is indexed
5655 * by [fls(speed) - 1]
5656 */
5657 static const u32 ice_aq_to_link_speed[] = {
5658 SPEED_10, /* BIT(0) */
5659 SPEED_100,
5660 SPEED_1000,
5661 SPEED_2500,
5662 SPEED_5000,
5663 SPEED_10000,
5664 SPEED_20000,
5665 SPEED_25000,
5666 SPEED_40000,
5667 SPEED_50000,
5668 SPEED_100000, /* BIT(10) */
5669 };
5670
5671 /**
5672 * ice_get_link_speed - get integer speed from table
5673 * @index: array index from fls(aq speed) - 1
5674 *
5675 * Returns: u32 value containing integer speed
5676 */
ice_get_link_speed(u16 index)5677 u32 ice_get_link_speed(u16 index)
5678 {
5679 if (index >= ARRAY_SIZE(ice_aq_to_link_speed))
5680 return 0;
5681
5682 return ice_aq_to_link_speed[index];
5683 }
5684