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