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