1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, 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 
9 #define ICE_PF_RESET_WAIT_COUNT	300
10 
11 /**
12  * ice_set_mac_type - Sets MAC type
13  * @hw: pointer to the HW structure
14  *
15  * This function sets the MAC type of the adapter based on the
16  * vendor ID and device ID stored in the HW structure.
17  */
18 static enum ice_status ice_set_mac_type(struct ice_hw *hw)
19 {
20 	if (hw->vendor_id != PCI_VENDOR_ID_INTEL)
21 		return ICE_ERR_DEVICE_NOT_SUPPORTED;
22 
23 	hw->mac_type = ICE_MAC_GENERIC;
24 	return 0;
25 }
26 
27 /**
28  * ice_clear_pf_cfg - Clear PF configuration
29  * @hw: pointer to the hardware structure
30  *
31  * Clears any existing PF configuration (VSIs, VSI lists, switch rules, port
32  * configuration, flow director filters, etc.).
33  */
34 enum ice_status ice_clear_pf_cfg(struct ice_hw *hw)
35 {
36 	struct ice_aq_desc desc;
37 
38 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pf_cfg);
39 
40 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
41 }
42 
43 /**
44  * ice_aq_manage_mac_read - manage MAC address read command
45  * @hw: pointer to the HW struct
46  * @buf: a virtual buffer to hold the manage MAC read response
47  * @buf_size: Size of the virtual buffer
48  * @cd: pointer to command details structure or NULL
49  *
50  * This function is used to return per PF station MAC address (0x0107).
51  * NOTE: Upon successful completion of this command, MAC address information
52  * is returned in user specified buffer. Please interpret user specified
53  * buffer as "manage_mac_read" response.
54  * Response such as various MAC addresses are stored in HW struct (port.mac)
55  * ice_aq_discover_caps is expected to be called before this function is called.
56  */
57 static enum ice_status
58 ice_aq_manage_mac_read(struct ice_hw *hw, void *buf, u16 buf_size,
59 		       struct ice_sq_cd *cd)
60 {
61 	struct ice_aqc_manage_mac_read_resp *resp;
62 	struct ice_aqc_manage_mac_read *cmd;
63 	struct ice_aq_desc desc;
64 	enum ice_status status;
65 	u16 flags;
66 	u8 i;
67 
68 	cmd = &desc.params.mac_read;
69 
70 	if (buf_size < sizeof(*resp))
71 		return ICE_ERR_BUF_TOO_SHORT;
72 
73 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_read);
74 
75 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
76 	if (status)
77 		return status;
78 
79 	resp = (struct ice_aqc_manage_mac_read_resp *)buf;
80 	flags = le16_to_cpu(cmd->flags) & ICE_AQC_MAN_MAC_READ_M;
81 
82 	if (!(flags & ICE_AQC_MAN_MAC_LAN_ADDR_VALID)) {
83 		ice_debug(hw, ICE_DBG_LAN, "got invalid MAC address\n");
84 		return ICE_ERR_CFG;
85 	}
86 
87 	/* A single port can report up to two (LAN and WoL) addresses */
88 	for (i = 0; i < cmd->num_addr; i++)
89 		if (resp[i].addr_type == ICE_AQC_MAN_MAC_ADDR_TYPE_LAN) {
90 			ether_addr_copy(hw->port_info->mac.lan_addr,
91 					resp[i].mac_addr);
92 			ether_addr_copy(hw->port_info->mac.perm_addr,
93 					resp[i].mac_addr);
94 			break;
95 		}
96 
97 	return 0;
98 }
99 
100 /**
101  * ice_aq_get_phy_caps - returns PHY capabilities
102  * @pi: port information structure
103  * @qual_mods: report qualified modules
104  * @report_mode: report mode capabilities
105  * @pcaps: structure for PHY capabilities to be filled
106  * @cd: pointer to command details structure or NULL
107  *
108  * Returns the various PHY capabilities supported on the Port (0x0600)
109  */
110 enum ice_status
111 ice_aq_get_phy_caps(struct ice_port_info *pi, bool qual_mods, u8 report_mode,
112 		    struct ice_aqc_get_phy_caps_data *pcaps,
113 		    struct ice_sq_cd *cd)
114 {
115 	struct ice_aqc_get_phy_caps *cmd;
116 	u16 pcaps_size = sizeof(*pcaps);
117 	struct ice_aq_desc desc;
118 	enum ice_status status;
119 
120 	cmd = &desc.params.get_phy;
121 
122 	if (!pcaps || (report_mode & ~ICE_AQC_REPORT_MODE_M) || !pi)
123 		return ICE_ERR_PARAM;
124 
125 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_phy_caps);
126 
127 	if (qual_mods)
128 		cmd->param0 |= cpu_to_le16(ICE_AQC_GET_PHY_RQM);
129 
130 	cmd->param0 |= cpu_to_le16(report_mode);
131 	status = ice_aq_send_cmd(pi->hw, &desc, pcaps, pcaps_size, cd);
132 
133 	if (!status && report_mode == ICE_AQC_REPORT_TOPO_CAP) {
134 		pi->phy.phy_type_low = le64_to_cpu(pcaps->phy_type_low);
135 		pi->phy.phy_type_high = le64_to_cpu(pcaps->phy_type_high);
136 	}
137 
138 	return status;
139 }
140 
141 /**
142  * ice_get_media_type - Gets media type
143  * @pi: port information structure
144  */
145 static enum ice_media_type ice_get_media_type(struct ice_port_info *pi)
146 {
147 	struct ice_link_status *hw_link_info;
148 
149 	if (!pi)
150 		return ICE_MEDIA_UNKNOWN;
151 
152 	hw_link_info = &pi->phy.link_info;
153 	if (hw_link_info->phy_type_low && hw_link_info->phy_type_high)
154 		/* If more than one media type is selected, report unknown */
155 		return ICE_MEDIA_UNKNOWN;
156 
157 	if (hw_link_info->phy_type_low) {
158 		switch (hw_link_info->phy_type_low) {
159 		case ICE_PHY_TYPE_LOW_1000BASE_SX:
160 		case ICE_PHY_TYPE_LOW_1000BASE_LX:
161 		case ICE_PHY_TYPE_LOW_10GBASE_SR:
162 		case ICE_PHY_TYPE_LOW_10GBASE_LR:
163 		case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
164 		case ICE_PHY_TYPE_LOW_25GBASE_SR:
165 		case ICE_PHY_TYPE_LOW_25GBASE_LR:
166 		case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
167 		case ICE_PHY_TYPE_LOW_40GBASE_SR4:
168 		case ICE_PHY_TYPE_LOW_40GBASE_LR4:
169 		case ICE_PHY_TYPE_LOW_50GBASE_SR2:
170 		case ICE_PHY_TYPE_LOW_50GBASE_LR2:
171 		case ICE_PHY_TYPE_LOW_50GBASE_SR:
172 		case ICE_PHY_TYPE_LOW_50GBASE_FR:
173 		case ICE_PHY_TYPE_LOW_50GBASE_LR:
174 		case ICE_PHY_TYPE_LOW_100GBASE_SR4:
175 		case ICE_PHY_TYPE_LOW_100GBASE_LR4:
176 		case ICE_PHY_TYPE_LOW_100GBASE_SR2:
177 		case ICE_PHY_TYPE_LOW_100GBASE_DR:
178 			return ICE_MEDIA_FIBER;
179 		case ICE_PHY_TYPE_LOW_100BASE_TX:
180 		case ICE_PHY_TYPE_LOW_1000BASE_T:
181 		case ICE_PHY_TYPE_LOW_2500BASE_T:
182 		case ICE_PHY_TYPE_LOW_5GBASE_T:
183 		case ICE_PHY_TYPE_LOW_10GBASE_T:
184 		case ICE_PHY_TYPE_LOW_25GBASE_T:
185 			return ICE_MEDIA_BASET;
186 		case ICE_PHY_TYPE_LOW_10G_SFI_DA:
187 		case ICE_PHY_TYPE_LOW_25GBASE_CR:
188 		case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
189 		case ICE_PHY_TYPE_LOW_25GBASE_CR1:
190 		case ICE_PHY_TYPE_LOW_40GBASE_CR4:
191 		case ICE_PHY_TYPE_LOW_50GBASE_CR2:
192 		case ICE_PHY_TYPE_LOW_50GBASE_CP:
193 		case ICE_PHY_TYPE_LOW_100GBASE_CR4:
194 		case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
195 		case ICE_PHY_TYPE_LOW_100GBASE_CP2:
196 			return ICE_MEDIA_DA;
197 		case ICE_PHY_TYPE_LOW_1000BASE_KX:
198 		case ICE_PHY_TYPE_LOW_2500BASE_KX:
199 		case ICE_PHY_TYPE_LOW_2500BASE_X:
200 		case ICE_PHY_TYPE_LOW_5GBASE_KR:
201 		case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
202 		case ICE_PHY_TYPE_LOW_25GBASE_KR:
203 		case ICE_PHY_TYPE_LOW_25GBASE_KR1:
204 		case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
205 		case ICE_PHY_TYPE_LOW_40GBASE_KR4:
206 		case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
207 		case ICE_PHY_TYPE_LOW_50GBASE_KR2:
208 		case ICE_PHY_TYPE_LOW_100GBASE_KR4:
209 		case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
210 			return ICE_MEDIA_BACKPLANE;
211 		}
212 	} else {
213 		switch (hw_link_info->phy_type_high) {
214 		case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
215 			return ICE_MEDIA_BACKPLANE;
216 		}
217 	}
218 	return ICE_MEDIA_UNKNOWN;
219 }
220 
221 /**
222  * ice_aq_get_link_info
223  * @pi: port information structure
224  * @ena_lse: enable/disable LinkStatusEvent reporting
225  * @link: pointer to link status structure - optional
226  * @cd: pointer to command details structure or NULL
227  *
228  * Get Link Status (0x607). Returns the link status of the adapter.
229  */
230 enum ice_status
231 ice_aq_get_link_info(struct ice_port_info *pi, bool ena_lse,
232 		     struct ice_link_status *link, struct ice_sq_cd *cd)
233 {
234 	struct ice_aqc_get_link_status_data link_data = { 0 };
235 	struct ice_aqc_get_link_status *resp;
236 	struct ice_link_status *li_old, *li;
237 	enum ice_media_type *hw_media_type;
238 	struct ice_fc_info *hw_fc_info;
239 	bool tx_pause, rx_pause;
240 	struct ice_aq_desc desc;
241 	enum ice_status status;
242 	struct ice_hw *hw;
243 	u16 cmd_flags;
244 
245 	if (!pi)
246 		return ICE_ERR_PARAM;
247 	hw = pi->hw;
248 	li_old = &pi->phy.link_info_old;
249 	hw_media_type = &pi->phy.media_type;
250 	li = &pi->phy.link_info;
251 	hw_fc_info = &pi->fc;
252 
253 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_status);
254 	cmd_flags = (ena_lse) ? ICE_AQ_LSE_ENA : ICE_AQ_LSE_DIS;
255 	resp = &desc.params.get_link_status;
256 	resp->cmd_flags = cpu_to_le16(cmd_flags);
257 	resp->lport_num = pi->lport;
258 
259 	status = ice_aq_send_cmd(hw, &desc, &link_data, sizeof(link_data), cd);
260 
261 	if (status)
262 		return status;
263 
264 	/* save off old link status information */
265 	*li_old = *li;
266 
267 	/* update current link status information */
268 	li->link_speed = le16_to_cpu(link_data.link_speed);
269 	li->phy_type_low = le64_to_cpu(link_data.phy_type_low);
270 	li->phy_type_high = le64_to_cpu(link_data.phy_type_high);
271 	*hw_media_type = ice_get_media_type(pi);
272 	li->link_info = link_data.link_info;
273 	li->an_info = link_data.an_info;
274 	li->ext_info = link_data.ext_info;
275 	li->max_frame_size = le16_to_cpu(link_data.max_frame_size);
276 	li->fec_info = link_data.cfg & ICE_AQ_FEC_MASK;
277 	li->topo_media_conflict = link_data.topo_media_conflict;
278 	li->pacing = link_data.cfg & (ICE_AQ_CFG_PACING_M |
279 				      ICE_AQ_CFG_PACING_TYPE_M);
280 
281 	/* update fc info */
282 	tx_pause = !!(link_data.an_info & ICE_AQ_LINK_PAUSE_TX);
283 	rx_pause = !!(link_data.an_info & ICE_AQ_LINK_PAUSE_RX);
284 	if (tx_pause && rx_pause)
285 		hw_fc_info->current_mode = ICE_FC_FULL;
286 	else if (tx_pause)
287 		hw_fc_info->current_mode = ICE_FC_TX_PAUSE;
288 	else if (rx_pause)
289 		hw_fc_info->current_mode = ICE_FC_RX_PAUSE;
290 	else
291 		hw_fc_info->current_mode = ICE_FC_NONE;
292 
293 	li->lse_ena = !!(resp->cmd_flags & cpu_to_le16(ICE_AQ_LSE_IS_ENABLED));
294 
295 	ice_debug(hw, ICE_DBG_LINK, "link_speed = 0x%x\n", li->link_speed);
296 	ice_debug(hw, ICE_DBG_LINK, "phy_type_low = 0x%llx\n",
297 		  (unsigned long long)li->phy_type_low);
298 	ice_debug(hw, ICE_DBG_LINK, "phy_type_high = 0x%llx\n",
299 		  (unsigned long long)li->phy_type_high);
300 	ice_debug(hw, ICE_DBG_LINK, "media_type = 0x%x\n", *hw_media_type);
301 	ice_debug(hw, ICE_DBG_LINK, "link_info = 0x%x\n", li->link_info);
302 	ice_debug(hw, ICE_DBG_LINK, "an_info = 0x%x\n", li->an_info);
303 	ice_debug(hw, ICE_DBG_LINK, "ext_info = 0x%x\n", li->ext_info);
304 	ice_debug(hw, ICE_DBG_LINK, "lse_ena = 0x%x\n", li->lse_ena);
305 	ice_debug(hw, ICE_DBG_LINK, "max_frame = 0x%x\n", li->max_frame_size);
306 	ice_debug(hw, ICE_DBG_LINK, "pacing = 0x%x\n", li->pacing);
307 
308 	/* save link status information */
309 	if (link)
310 		*link = *li;
311 
312 	/* flag cleared so calling functions don't call AQ again */
313 	pi->phy.get_link_info = false;
314 
315 	return 0;
316 }
317 
318 /**
319  * ice_fill_tx_timer_and_fc_thresh
320  * @hw: pointer to the HW struct
321  * @cmd: pointer to MAC cfg structure
322  *
323  * Add Tx timer and FC refresh threshold info to Set MAC Config AQ command
324  * descriptor
325  */
326 static void
327 ice_fill_tx_timer_and_fc_thresh(struct ice_hw *hw,
328 				struct ice_aqc_set_mac_cfg *cmd)
329 {
330 	u16 fc_thres_val, tx_timer_val;
331 	u32 val;
332 
333 	/* We read back the transmit timer and FC threshold value of
334 	 * LFC. Thus, we will use index =
335 	 * PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA_MAX_INDEX.
336 	 *
337 	 * Also, because we are operating on transmit timer and FC
338 	 * threshold of LFC, we don't turn on any bit in tx_tmr_priority
339 	 */
340 #define IDX_OF_LFC PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA_MAX_INDEX
341 
342 	/* Retrieve the transmit timer */
343 	val = rd32(hw, PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA(IDX_OF_LFC));
344 	tx_timer_val = val &
345 		PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA_HSEC_CTL_TX_PAUSE_QUANTA_M;
346 	cmd->tx_tmr_value = cpu_to_le16(tx_timer_val);
347 
348 	/* Retrieve the FC threshold */
349 	val = rd32(hw, PRTMAC_HSEC_CTL_TX_PAUSE_REFRESH_TIMER(IDX_OF_LFC));
350 	fc_thres_val = val & PRTMAC_HSEC_CTL_TX_PAUSE_REFRESH_TIMER_M;
351 
352 	cmd->fc_refresh_threshold = cpu_to_le16(fc_thres_val);
353 }
354 
355 /**
356  * ice_aq_set_mac_cfg
357  * @hw: pointer to the HW struct
358  * @max_frame_size: Maximum Frame Size to be supported
359  * @cd: pointer to command details structure or NULL
360  *
361  * Set MAC configuration (0x0603)
362  */
363 enum ice_status
364 ice_aq_set_mac_cfg(struct ice_hw *hw, u16 max_frame_size, struct ice_sq_cd *cd)
365 {
366 	struct ice_aqc_set_mac_cfg *cmd;
367 	struct ice_aq_desc desc;
368 
369 	cmd = &desc.params.set_mac_cfg;
370 
371 	if (max_frame_size == 0)
372 		return ICE_ERR_PARAM;
373 
374 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_mac_cfg);
375 
376 	cmd->max_frame_size = cpu_to_le16(max_frame_size);
377 
378 	ice_fill_tx_timer_and_fc_thresh(hw, cmd);
379 
380 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
381 }
382 
383 /**
384  * ice_init_fltr_mgmt_struct - initializes filter management list and locks
385  * @hw: pointer to the HW struct
386  */
387 static enum ice_status ice_init_fltr_mgmt_struct(struct ice_hw *hw)
388 {
389 	struct ice_switch_info *sw;
390 	enum ice_status status;
391 
392 	hw->switch_info = devm_kzalloc(ice_hw_to_dev(hw),
393 				       sizeof(*hw->switch_info), GFP_KERNEL);
394 	sw = hw->switch_info;
395 
396 	if (!sw)
397 		return ICE_ERR_NO_MEMORY;
398 
399 	INIT_LIST_HEAD(&sw->vsi_list_map_head);
400 
401 	status = ice_init_def_sw_recp(hw);
402 	if (status) {
403 		devm_kfree(ice_hw_to_dev(hw), hw->switch_info);
404 		return status;
405 	}
406 	return 0;
407 }
408 
409 /**
410  * ice_cleanup_fltr_mgmt_struct - cleanup filter management list and locks
411  * @hw: pointer to the HW struct
412  */
413 static void ice_cleanup_fltr_mgmt_struct(struct ice_hw *hw)
414 {
415 	struct ice_switch_info *sw = hw->switch_info;
416 	struct ice_vsi_list_map_info *v_pos_map;
417 	struct ice_vsi_list_map_info *v_tmp_map;
418 	struct ice_sw_recipe *recps;
419 	u8 i;
420 
421 	list_for_each_entry_safe(v_pos_map, v_tmp_map, &sw->vsi_list_map_head,
422 				 list_entry) {
423 		list_del(&v_pos_map->list_entry);
424 		devm_kfree(ice_hw_to_dev(hw), v_pos_map);
425 	}
426 	recps = hw->switch_info->recp_list;
427 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
428 		struct ice_fltr_mgmt_list_entry *lst_itr, *tmp_entry;
429 
430 		recps[i].root_rid = i;
431 		mutex_destroy(&recps[i].filt_rule_lock);
432 		list_for_each_entry_safe(lst_itr, tmp_entry,
433 					 &recps[i].filt_rules, list_entry) {
434 			list_del(&lst_itr->list_entry);
435 			devm_kfree(ice_hw_to_dev(hw), lst_itr);
436 		}
437 	}
438 	ice_rm_all_sw_replay_rule_info(hw);
439 	devm_kfree(ice_hw_to_dev(hw), sw->recp_list);
440 	devm_kfree(ice_hw_to_dev(hw), sw);
441 }
442 
443 /**
444  * ice_get_fw_log_cfg - get FW logging configuration
445  * @hw: pointer to the HW struct
446  */
447 static enum ice_status ice_get_fw_log_cfg(struct ice_hw *hw)
448 {
449 	struct ice_aq_desc desc;
450 	enum ice_status status;
451 	__le16 *config;
452 	u16 size;
453 
454 	size = sizeof(*config) * ICE_AQC_FW_LOG_ID_MAX;
455 	config = devm_kzalloc(ice_hw_to_dev(hw), size, GFP_KERNEL);
456 	if (!config)
457 		return ICE_ERR_NO_MEMORY;
458 
459 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_fw_logging_info);
460 
461 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
462 
463 	status = ice_aq_send_cmd(hw, &desc, config, size, NULL);
464 	if (!status) {
465 		u16 i;
466 
467 		/* Save FW logging information into the HW structure */
468 		for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
469 			u16 v, m, flgs;
470 
471 			v = le16_to_cpu(config[i]);
472 			m = (v & ICE_AQC_FW_LOG_ID_M) >> ICE_AQC_FW_LOG_ID_S;
473 			flgs = (v & ICE_AQC_FW_LOG_EN_M) >> ICE_AQC_FW_LOG_EN_S;
474 
475 			if (m < ICE_AQC_FW_LOG_ID_MAX)
476 				hw->fw_log.evnts[m].cur = flgs;
477 		}
478 	}
479 
480 	devm_kfree(ice_hw_to_dev(hw), config);
481 
482 	return status;
483 }
484 
485 /**
486  * ice_cfg_fw_log - configure FW logging
487  * @hw: pointer to the HW struct
488  * @enable: enable certain FW logging events if true, disable all if false
489  *
490  * This function enables/disables the FW logging via Rx CQ events and a UART
491  * port based on predetermined configurations. FW logging via the Rx CQ can be
492  * enabled/disabled for individual PF's. However, FW logging via the UART can
493  * only be enabled/disabled for all PFs on the same device.
494  *
495  * To enable overall FW logging, the "cq_en" and "uart_en" enable bits in
496  * hw->fw_log need to be set accordingly, e.g. based on user-provided input,
497  * before initializing the device.
498  *
499  * When re/configuring FW logging, callers need to update the "cfg" elements of
500  * the hw->fw_log.evnts array with the desired logging event configurations for
501  * modules of interest. When disabling FW logging completely, the callers can
502  * just pass false in the "enable" parameter. On completion, the function will
503  * update the "cur" element of the hw->fw_log.evnts array with the resulting
504  * logging event configurations of the modules that are being re/configured. FW
505  * logging modules that are not part of a reconfiguration operation retain their
506  * previous states.
507  *
508  * Before resetting the device, it is recommended that the driver disables FW
509  * logging before shutting down the control queue. When disabling FW logging
510  * ("enable" = false), the latest configurations of FW logging events stored in
511  * hw->fw_log.evnts[] are not overridden to allow them to be reconfigured after
512  * a device reset.
513  *
514  * When enabling FW logging to emit log messages via the Rx CQ during the
515  * device's initialization phase, a mechanism alternative to interrupt handlers
516  * needs to be used to extract FW log messages from the Rx CQ periodically and
517  * to prevent the Rx CQ from being full and stalling other types of control
518  * messages from FW to SW. Interrupts are typically disabled during the device's
519  * initialization phase.
520  */
521 static enum ice_status ice_cfg_fw_log(struct ice_hw *hw, bool enable)
522 {
523 	struct ice_aqc_fw_logging *cmd;
524 	enum ice_status status = 0;
525 	u16 i, chgs = 0, len = 0;
526 	struct ice_aq_desc desc;
527 	__le16 *data = NULL;
528 	u8 actv_evnts = 0;
529 	void *buf = NULL;
530 
531 	if (!hw->fw_log.cq_en && !hw->fw_log.uart_en)
532 		return 0;
533 
534 	/* Disable FW logging only when the control queue is still responsive */
535 	if (!enable &&
536 	    (!hw->fw_log.actv_evnts || !ice_check_sq_alive(hw, &hw->adminq)))
537 		return 0;
538 
539 	/* Get current FW log settings */
540 	status = ice_get_fw_log_cfg(hw);
541 	if (status)
542 		return status;
543 
544 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_fw_logging);
545 	cmd = &desc.params.fw_logging;
546 
547 	/* Indicate which controls are valid */
548 	if (hw->fw_log.cq_en)
549 		cmd->log_ctrl_valid |= ICE_AQC_FW_LOG_AQ_VALID;
550 
551 	if (hw->fw_log.uart_en)
552 		cmd->log_ctrl_valid |= ICE_AQC_FW_LOG_UART_VALID;
553 
554 	if (enable) {
555 		/* Fill in an array of entries with FW logging modules and
556 		 * logging events being reconfigured.
557 		 */
558 		for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
559 			u16 val;
560 
561 			/* Keep track of enabled event types */
562 			actv_evnts |= hw->fw_log.evnts[i].cfg;
563 
564 			if (hw->fw_log.evnts[i].cfg == hw->fw_log.evnts[i].cur)
565 				continue;
566 
567 			if (!data) {
568 				data = devm_kcalloc(ice_hw_to_dev(hw),
569 						    sizeof(*data),
570 						    ICE_AQC_FW_LOG_ID_MAX,
571 						    GFP_KERNEL);
572 				if (!data)
573 					return ICE_ERR_NO_MEMORY;
574 			}
575 
576 			val = i << ICE_AQC_FW_LOG_ID_S;
577 			val |= hw->fw_log.evnts[i].cfg << ICE_AQC_FW_LOG_EN_S;
578 			data[chgs++] = cpu_to_le16(val);
579 		}
580 
581 		/* Only enable FW logging if at least one module is specified.
582 		 * If FW logging is currently enabled but all modules are not
583 		 * enabled to emit log messages, disable FW logging altogether.
584 		 */
585 		if (actv_evnts) {
586 			/* Leave if there is effectively no change */
587 			if (!chgs)
588 				goto out;
589 
590 			if (hw->fw_log.cq_en)
591 				cmd->log_ctrl |= ICE_AQC_FW_LOG_AQ_EN;
592 
593 			if (hw->fw_log.uart_en)
594 				cmd->log_ctrl |= ICE_AQC_FW_LOG_UART_EN;
595 
596 			buf = data;
597 			len = sizeof(*data) * chgs;
598 			desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
599 		}
600 	}
601 
602 	status = ice_aq_send_cmd(hw, &desc, buf, len, NULL);
603 	if (!status) {
604 		/* Update the current configuration to reflect events enabled.
605 		 * hw->fw_log.cq_en and hw->fw_log.uart_en indicate if the FW
606 		 * logging mode is enabled for the device. They do not reflect
607 		 * actual modules being enabled to emit log messages. So, their
608 		 * values remain unchanged even when all modules are disabled.
609 		 */
610 		u16 cnt = enable ? chgs : (u16)ICE_AQC_FW_LOG_ID_MAX;
611 
612 		hw->fw_log.actv_evnts = actv_evnts;
613 		for (i = 0; i < cnt; i++) {
614 			u16 v, m;
615 
616 			if (!enable) {
617 				/* When disabling all FW logging events as part
618 				 * of device's de-initialization, the original
619 				 * configurations are retained, and can be used
620 				 * to reconfigure FW logging later if the device
621 				 * is re-initialized.
622 				 */
623 				hw->fw_log.evnts[i].cur = 0;
624 				continue;
625 			}
626 
627 			v = le16_to_cpu(data[i]);
628 			m = (v & ICE_AQC_FW_LOG_ID_M) >> ICE_AQC_FW_LOG_ID_S;
629 			hw->fw_log.evnts[m].cur = hw->fw_log.evnts[m].cfg;
630 		}
631 	}
632 
633 out:
634 	if (data)
635 		devm_kfree(ice_hw_to_dev(hw), data);
636 
637 	return status;
638 }
639 
640 /**
641  * ice_output_fw_log
642  * @hw: pointer to the HW struct
643  * @desc: pointer to the AQ message descriptor
644  * @buf: pointer to the buffer accompanying the AQ message
645  *
646  * Formats a FW Log message and outputs it via the standard driver logs.
647  */
648 void ice_output_fw_log(struct ice_hw *hw, struct ice_aq_desc *desc, void *buf)
649 {
650 	ice_debug(hw, ICE_DBG_FW_LOG, "[ FW Log Msg Start ]\n");
651 	ice_debug_array(hw, ICE_DBG_FW_LOG, 16, 1, (u8 *)buf,
652 			le16_to_cpu(desc->datalen));
653 	ice_debug(hw, ICE_DBG_FW_LOG, "[ FW Log Msg End ]\n");
654 }
655 
656 /**
657  * ice_get_itr_intrl_gran
658  * @hw: pointer to the HW struct
659  *
660  * Determines the ITR/INTRL granularities based on the maximum aggregate
661  * bandwidth according to the device's configuration during power-on.
662  */
663 static void ice_get_itr_intrl_gran(struct ice_hw *hw)
664 {
665 	u8 max_agg_bw = (rd32(hw, GL_PWR_MODE_CTL) &
666 			 GL_PWR_MODE_CTL_CAR_MAX_BW_M) >>
667 			GL_PWR_MODE_CTL_CAR_MAX_BW_S;
668 
669 	switch (max_agg_bw) {
670 	case ICE_MAX_AGG_BW_200G:
671 	case ICE_MAX_AGG_BW_100G:
672 	case ICE_MAX_AGG_BW_50G:
673 		hw->itr_gran = ICE_ITR_GRAN_ABOVE_25;
674 		hw->intrl_gran = ICE_INTRL_GRAN_ABOVE_25;
675 		break;
676 	case ICE_MAX_AGG_BW_25G:
677 		hw->itr_gran = ICE_ITR_GRAN_MAX_25;
678 		hw->intrl_gran = ICE_INTRL_GRAN_MAX_25;
679 		break;
680 	}
681 }
682 
683 /**
684  * ice_init_hw - main hardware initialization routine
685  * @hw: pointer to the hardware structure
686  */
687 enum ice_status ice_init_hw(struct ice_hw *hw)
688 {
689 	struct ice_aqc_get_phy_caps_data *pcaps;
690 	enum ice_status status;
691 	u16 mac_buf_len;
692 	void *mac_buf;
693 
694 	/* Set MAC type based on DeviceID */
695 	status = ice_set_mac_type(hw);
696 	if (status)
697 		return status;
698 
699 	hw->pf_id = (u8)(rd32(hw, PF_FUNC_RID) &
700 			 PF_FUNC_RID_FUNC_NUM_M) >>
701 		PF_FUNC_RID_FUNC_NUM_S;
702 
703 	status = ice_reset(hw, ICE_RESET_PFR);
704 	if (status)
705 		return status;
706 
707 	ice_get_itr_intrl_gran(hw);
708 
709 	status = ice_create_all_ctrlq(hw);
710 	if (status)
711 		goto err_unroll_cqinit;
712 
713 	/* Enable FW logging. Not fatal if this fails. */
714 	status = ice_cfg_fw_log(hw, true);
715 	if (status)
716 		ice_debug(hw, ICE_DBG_INIT, "Failed to enable FW logging.\n");
717 
718 	status = ice_clear_pf_cfg(hw);
719 	if (status)
720 		goto err_unroll_cqinit;
721 
722 	/* Set bit to enable Flow Director filters */
723 	wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
724 	INIT_LIST_HEAD(&hw->fdir_list_head);
725 
726 	ice_clear_pxe_mode(hw);
727 
728 	status = ice_init_nvm(hw);
729 	if (status)
730 		goto err_unroll_cqinit;
731 
732 	status = ice_get_caps(hw);
733 	if (status)
734 		goto err_unroll_cqinit;
735 
736 	hw->port_info = devm_kzalloc(ice_hw_to_dev(hw),
737 				     sizeof(*hw->port_info), GFP_KERNEL);
738 	if (!hw->port_info) {
739 		status = ICE_ERR_NO_MEMORY;
740 		goto err_unroll_cqinit;
741 	}
742 
743 	/* set the back pointer to HW */
744 	hw->port_info->hw = hw;
745 
746 	/* Initialize port_info struct with switch configuration data */
747 	status = ice_get_initial_sw_cfg(hw);
748 	if (status)
749 		goto err_unroll_alloc;
750 
751 	hw->evb_veb = true;
752 
753 	/* Query the allocated resources for Tx scheduler */
754 	status = ice_sched_query_res_alloc(hw);
755 	if (status) {
756 		ice_debug(hw, ICE_DBG_SCHED,
757 			  "Failed to get scheduler allocated resources\n");
758 		goto err_unroll_alloc;
759 	}
760 
761 	/* Initialize port_info struct with scheduler data */
762 	status = ice_sched_init_port(hw->port_info);
763 	if (status)
764 		goto err_unroll_sched;
765 
766 	pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps), GFP_KERNEL);
767 	if (!pcaps) {
768 		status = ICE_ERR_NO_MEMORY;
769 		goto err_unroll_sched;
770 	}
771 
772 	/* Initialize port_info struct with PHY capabilities */
773 	status = ice_aq_get_phy_caps(hw->port_info, false,
774 				     ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
775 	devm_kfree(ice_hw_to_dev(hw), pcaps);
776 	if (status)
777 		goto err_unroll_sched;
778 
779 	/* Initialize port_info struct with link information */
780 	status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
781 	if (status)
782 		goto err_unroll_sched;
783 
784 	/* need a valid SW entry point to build a Tx tree */
785 	if (!hw->sw_entry_point_layer) {
786 		ice_debug(hw, ICE_DBG_SCHED, "invalid sw entry point\n");
787 		status = ICE_ERR_CFG;
788 		goto err_unroll_sched;
789 	}
790 	INIT_LIST_HEAD(&hw->agg_list);
791 	/* Initialize max burst size */
792 	if (!hw->max_burst_size)
793 		ice_cfg_rl_burst_size(hw, ICE_SCHED_DFLT_BURST_SIZE);
794 
795 	status = ice_init_fltr_mgmt_struct(hw);
796 	if (status)
797 		goto err_unroll_sched;
798 
799 	/* Get MAC information */
800 	/* A single port can report up to two (LAN and WoL) addresses */
801 	mac_buf = devm_kcalloc(ice_hw_to_dev(hw), 2,
802 			       sizeof(struct ice_aqc_manage_mac_read_resp),
803 			       GFP_KERNEL);
804 	mac_buf_len = 2 * sizeof(struct ice_aqc_manage_mac_read_resp);
805 
806 	if (!mac_buf) {
807 		status = ICE_ERR_NO_MEMORY;
808 		goto err_unroll_fltr_mgmt_struct;
809 	}
810 
811 	status = ice_aq_manage_mac_read(hw, mac_buf, mac_buf_len, NULL);
812 	devm_kfree(ice_hw_to_dev(hw), mac_buf);
813 
814 	if (status)
815 		goto err_unroll_fltr_mgmt_struct;
816 	/* enable jumbo frame support at MAC level */
817 	status = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
818 	if (status)
819 		goto err_unroll_fltr_mgmt_struct;
820 	/* Obtain counter base index which would be used by flow director */
821 	status = ice_alloc_fd_res_cntr(hw, &hw->fd_ctr_base);
822 	if (status)
823 		goto err_unroll_fltr_mgmt_struct;
824 	status = ice_init_hw_tbls(hw);
825 	if (status)
826 		goto err_unroll_fltr_mgmt_struct;
827 	mutex_init(&hw->tnl_lock);
828 	return 0;
829 
830 err_unroll_fltr_mgmt_struct:
831 	ice_cleanup_fltr_mgmt_struct(hw);
832 err_unroll_sched:
833 	ice_sched_cleanup_all(hw);
834 err_unroll_alloc:
835 	devm_kfree(ice_hw_to_dev(hw), hw->port_info);
836 err_unroll_cqinit:
837 	ice_destroy_all_ctrlq(hw);
838 	return status;
839 }
840 
841 /**
842  * ice_deinit_hw - unroll initialization operations done by ice_init_hw
843  * @hw: pointer to the hardware structure
844  *
845  * This should be called only during nominal operation, not as a result of
846  * ice_init_hw() failing since ice_init_hw() will take care of unrolling
847  * applicable initializations if it fails for any reason.
848  */
849 void ice_deinit_hw(struct ice_hw *hw)
850 {
851 	ice_free_fd_res_cntr(hw, hw->fd_ctr_base);
852 	ice_cleanup_fltr_mgmt_struct(hw);
853 
854 	ice_sched_cleanup_all(hw);
855 	ice_sched_clear_agg(hw);
856 	ice_free_seg(hw);
857 	ice_free_hw_tbls(hw);
858 	mutex_destroy(&hw->tnl_lock);
859 
860 	if (hw->port_info) {
861 		devm_kfree(ice_hw_to_dev(hw), hw->port_info);
862 		hw->port_info = NULL;
863 	}
864 
865 	/* Attempt to disable FW logging before shutting down control queues */
866 	ice_cfg_fw_log(hw, false);
867 	ice_destroy_all_ctrlq(hw);
868 
869 	/* Clear VSI contexts if not already cleared */
870 	ice_clear_all_vsi_ctx(hw);
871 }
872 
873 /**
874  * ice_check_reset - Check to see if a global reset is complete
875  * @hw: pointer to the hardware structure
876  */
877 enum ice_status ice_check_reset(struct ice_hw *hw)
878 {
879 	u32 cnt, reg = 0, grst_delay, uld_mask;
880 
881 	/* Poll for Device Active state in case a recent CORER, GLOBR,
882 	 * or EMPR has occurred. The grst delay value is in 100ms units.
883 	 * Add 1sec for outstanding AQ commands that can take a long time.
884 	 */
885 	grst_delay = ((rd32(hw, GLGEN_RSTCTL) & GLGEN_RSTCTL_GRSTDEL_M) >>
886 		      GLGEN_RSTCTL_GRSTDEL_S) + 10;
887 
888 	for (cnt = 0; cnt < grst_delay; cnt++) {
889 		mdelay(100);
890 		reg = rd32(hw, GLGEN_RSTAT);
891 		if (!(reg & GLGEN_RSTAT_DEVSTATE_M))
892 			break;
893 	}
894 
895 	if (cnt == grst_delay) {
896 		ice_debug(hw, ICE_DBG_INIT,
897 			  "Global reset polling failed to complete.\n");
898 		return ICE_ERR_RESET_FAILED;
899 	}
900 
901 #define ICE_RESET_DONE_MASK	(GLNVM_ULD_PCIER_DONE_M |\
902 				 GLNVM_ULD_PCIER_DONE_1_M |\
903 				 GLNVM_ULD_CORER_DONE_M |\
904 				 GLNVM_ULD_GLOBR_DONE_M |\
905 				 GLNVM_ULD_POR_DONE_M |\
906 				 GLNVM_ULD_POR_DONE_1_M |\
907 				 GLNVM_ULD_PCIER_DONE_2_M)
908 
909 	uld_mask = ICE_RESET_DONE_MASK;
910 
911 	/* Device is Active; check Global Reset processes are done */
912 	for (cnt = 0; cnt < ICE_PF_RESET_WAIT_COUNT; cnt++) {
913 		reg = rd32(hw, GLNVM_ULD) & uld_mask;
914 		if (reg == uld_mask) {
915 			ice_debug(hw, ICE_DBG_INIT,
916 				  "Global reset processes done. %d\n", cnt);
917 			break;
918 		}
919 		mdelay(10);
920 	}
921 
922 	if (cnt == ICE_PF_RESET_WAIT_COUNT) {
923 		ice_debug(hw, ICE_DBG_INIT,
924 			  "Wait for Reset Done timed out. GLNVM_ULD = 0x%x\n",
925 			  reg);
926 		return ICE_ERR_RESET_FAILED;
927 	}
928 
929 	return 0;
930 }
931 
932 /**
933  * ice_pf_reset - Reset the PF
934  * @hw: pointer to the hardware structure
935  *
936  * If a global reset has been triggered, this function checks
937  * for its completion and then issues the PF reset
938  */
939 static enum ice_status ice_pf_reset(struct ice_hw *hw)
940 {
941 	u32 cnt, reg;
942 
943 	/* If at function entry a global reset was already in progress, i.e.
944 	 * state is not 'device active' or any of the reset done bits are not
945 	 * set in GLNVM_ULD, there is no need for a PF Reset; poll until the
946 	 * global reset is done.
947 	 */
948 	if ((rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_DEVSTATE_M) ||
949 	    (rd32(hw, GLNVM_ULD) & ICE_RESET_DONE_MASK) ^ ICE_RESET_DONE_MASK) {
950 		/* poll on global reset currently in progress until done */
951 		if (ice_check_reset(hw))
952 			return ICE_ERR_RESET_FAILED;
953 
954 		return 0;
955 	}
956 
957 	/* Reset the PF */
958 	reg = rd32(hw, PFGEN_CTRL);
959 
960 	wr32(hw, PFGEN_CTRL, (reg | PFGEN_CTRL_PFSWR_M));
961 
962 	/* Wait for the PFR to complete. The wait time is the global config lock
963 	 * timeout plus the PFR timeout which will account for a possible reset
964 	 * that is occurring during a download package operation.
965 	 */
966 	for (cnt = 0; cnt < ICE_GLOBAL_CFG_LOCK_TIMEOUT +
967 	     ICE_PF_RESET_WAIT_COUNT; cnt++) {
968 		reg = rd32(hw, PFGEN_CTRL);
969 		if (!(reg & PFGEN_CTRL_PFSWR_M))
970 			break;
971 
972 		mdelay(1);
973 	}
974 
975 	if (cnt == ICE_PF_RESET_WAIT_COUNT) {
976 		ice_debug(hw, ICE_DBG_INIT,
977 			  "PF reset polling failed to complete.\n");
978 		return ICE_ERR_RESET_FAILED;
979 	}
980 
981 	return 0;
982 }
983 
984 /**
985  * ice_reset - Perform different types of reset
986  * @hw: pointer to the hardware structure
987  * @req: reset request
988  *
989  * This function triggers a reset as specified by the req parameter.
990  *
991  * Note:
992  * If anything other than a PF reset is triggered, PXE mode is restored.
993  * This has to be cleared using ice_clear_pxe_mode again, once the AQ
994  * interface has been restored in the rebuild flow.
995  */
996 enum ice_status ice_reset(struct ice_hw *hw, enum ice_reset_req req)
997 {
998 	u32 val = 0;
999 
1000 	switch (req) {
1001 	case ICE_RESET_PFR:
1002 		return ice_pf_reset(hw);
1003 	case ICE_RESET_CORER:
1004 		ice_debug(hw, ICE_DBG_INIT, "CoreR requested\n");
1005 		val = GLGEN_RTRIG_CORER_M;
1006 		break;
1007 	case ICE_RESET_GLOBR:
1008 		ice_debug(hw, ICE_DBG_INIT, "GlobalR requested\n");
1009 		val = GLGEN_RTRIG_GLOBR_M;
1010 		break;
1011 	default:
1012 		return ICE_ERR_PARAM;
1013 	}
1014 
1015 	val |= rd32(hw, GLGEN_RTRIG);
1016 	wr32(hw, GLGEN_RTRIG, val);
1017 	ice_flush(hw);
1018 
1019 	/* wait for the FW to be ready */
1020 	return ice_check_reset(hw);
1021 }
1022 
1023 /**
1024  * ice_copy_rxq_ctx_to_hw
1025  * @hw: pointer to the hardware structure
1026  * @ice_rxq_ctx: pointer to the rxq context
1027  * @rxq_index: the index of the Rx queue
1028  *
1029  * Copies rxq context from dense structure to HW register space
1030  */
1031 static enum ice_status
1032 ice_copy_rxq_ctx_to_hw(struct ice_hw *hw, u8 *ice_rxq_ctx, u32 rxq_index)
1033 {
1034 	u8 i;
1035 
1036 	if (!ice_rxq_ctx)
1037 		return ICE_ERR_BAD_PTR;
1038 
1039 	if (rxq_index > QRX_CTRL_MAX_INDEX)
1040 		return ICE_ERR_PARAM;
1041 
1042 	/* Copy each dword separately to HW */
1043 	for (i = 0; i < ICE_RXQ_CTX_SIZE_DWORDS; i++) {
1044 		wr32(hw, QRX_CONTEXT(i, rxq_index),
1045 		     *((u32 *)(ice_rxq_ctx + (i * sizeof(u32)))));
1046 
1047 		ice_debug(hw, ICE_DBG_QCTX, "qrxdata[%d]: %08X\n", i,
1048 			  *((u32 *)(ice_rxq_ctx + (i * sizeof(u32)))));
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 /* LAN Rx Queue Context */
1055 static const struct ice_ctx_ele ice_rlan_ctx_info[] = {
1056 	/* Field		Width	LSB */
1057 	ICE_CTX_STORE(ice_rlan_ctx, head,		13,	0),
1058 	ICE_CTX_STORE(ice_rlan_ctx, cpuid,		8,	13),
1059 	ICE_CTX_STORE(ice_rlan_ctx, base,		57,	32),
1060 	ICE_CTX_STORE(ice_rlan_ctx, qlen,		13,	89),
1061 	ICE_CTX_STORE(ice_rlan_ctx, dbuf,		7,	102),
1062 	ICE_CTX_STORE(ice_rlan_ctx, hbuf,		5,	109),
1063 	ICE_CTX_STORE(ice_rlan_ctx, dtype,		2,	114),
1064 	ICE_CTX_STORE(ice_rlan_ctx, dsize,		1,	116),
1065 	ICE_CTX_STORE(ice_rlan_ctx, crcstrip,		1,	117),
1066 	ICE_CTX_STORE(ice_rlan_ctx, l2tsel,		1,	119),
1067 	ICE_CTX_STORE(ice_rlan_ctx, hsplit_0,		4,	120),
1068 	ICE_CTX_STORE(ice_rlan_ctx, hsplit_1,		2,	124),
1069 	ICE_CTX_STORE(ice_rlan_ctx, showiv,		1,	127),
1070 	ICE_CTX_STORE(ice_rlan_ctx, rxmax,		14,	174),
1071 	ICE_CTX_STORE(ice_rlan_ctx, tphrdesc_ena,	1,	193),
1072 	ICE_CTX_STORE(ice_rlan_ctx, tphwdesc_ena,	1,	194),
1073 	ICE_CTX_STORE(ice_rlan_ctx, tphdata_ena,	1,	195),
1074 	ICE_CTX_STORE(ice_rlan_ctx, tphhead_ena,	1,	196),
1075 	ICE_CTX_STORE(ice_rlan_ctx, lrxqthresh,		3,	198),
1076 	ICE_CTX_STORE(ice_rlan_ctx, prefena,		1,	201),
1077 	{ 0 }
1078 };
1079 
1080 /**
1081  * ice_write_rxq_ctx
1082  * @hw: pointer to the hardware structure
1083  * @rlan_ctx: pointer to the rxq context
1084  * @rxq_index: the index of the Rx queue
1085  *
1086  * Converts rxq context from sparse to dense structure and then writes
1087  * it to HW register space and enables the hardware to prefetch descriptors
1088  * instead of only fetching them on demand
1089  */
1090 enum ice_status
1091 ice_write_rxq_ctx(struct ice_hw *hw, struct ice_rlan_ctx *rlan_ctx,
1092 		  u32 rxq_index)
1093 {
1094 	u8 ctx_buf[ICE_RXQ_CTX_SZ] = { 0 };
1095 
1096 	if (!rlan_ctx)
1097 		return ICE_ERR_BAD_PTR;
1098 
1099 	rlan_ctx->prefena = 1;
1100 
1101 	ice_set_ctx(hw, (u8 *)rlan_ctx, ctx_buf, ice_rlan_ctx_info);
1102 	return ice_copy_rxq_ctx_to_hw(hw, ctx_buf, rxq_index);
1103 }
1104 
1105 /* LAN Tx Queue Context */
1106 const struct ice_ctx_ele ice_tlan_ctx_info[] = {
1107 				    /* Field			Width	LSB */
1108 	ICE_CTX_STORE(ice_tlan_ctx, base,			57,	0),
1109 	ICE_CTX_STORE(ice_tlan_ctx, port_num,			3,	57),
1110 	ICE_CTX_STORE(ice_tlan_ctx, cgd_num,			5,	60),
1111 	ICE_CTX_STORE(ice_tlan_ctx, pf_num,			3,	65),
1112 	ICE_CTX_STORE(ice_tlan_ctx, vmvf_num,			10,	68),
1113 	ICE_CTX_STORE(ice_tlan_ctx, vmvf_type,			2,	78),
1114 	ICE_CTX_STORE(ice_tlan_ctx, src_vsi,			10,	80),
1115 	ICE_CTX_STORE(ice_tlan_ctx, tsyn_ena,			1,	90),
1116 	ICE_CTX_STORE(ice_tlan_ctx, internal_usage_flag,	1,	91),
1117 	ICE_CTX_STORE(ice_tlan_ctx, alt_vlan,			1,	92),
1118 	ICE_CTX_STORE(ice_tlan_ctx, cpuid,			8,	93),
1119 	ICE_CTX_STORE(ice_tlan_ctx, wb_mode,			1,	101),
1120 	ICE_CTX_STORE(ice_tlan_ctx, tphrd_desc,			1,	102),
1121 	ICE_CTX_STORE(ice_tlan_ctx, tphrd,			1,	103),
1122 	ICE_CTX_STORE(ice_tlan_ctx, tphwr_desc,			1,	104),
1123 	ICE_CTX_STORE(ice_tlan_ctx, cmpq_id,			9,	105),
1124 	ICE_CTX_STORE(ice_tlan_ctx, qnum_in_func,		14,	114),
1125 	ICE_CTX_STORE(ice_tlan_ctx, itr_notification_mode,	1,	128),
1126 	ICE_CTX_STORE(ice_tlan_ctx, adjust_prof_id,		6,	129),
1127 	ICE_CTX_STORE(ice_tlan_ctx, qlen,			13,	135),
1128 	ICE_CTX_STORE(ice_tlan_ctx, quanta_prof_idx,		4,	148),
1129 	ICE_CTX_STORE(ice_tlan_ctx, tso_ena,			1,	152),
1130 	ICE_CTX_STORE(ice_tlan_ctx, tso_qnum,			11,	153),
1131 	ICE_CTX_STORE(ice_tlan_ctx, legacy_int,			1,	164),
1132 	ICE_CTX_STORE(ice_tlan_ctx, drop_ena,			1,	165),
1133 	ICE_CTX_STORE(ice_tlan_ctx, cache_prof_idx,		2,	166),
1134 	ICE_CTX_STORE(ice_tlan_ctx, pkt_shaper_prof_idx,	3,	168),
1135 	ICE_CTX_STORE(ice_tlan_ctx, int_q_state,		122,	171),
1136 	{ 0 }
1137 };
1138 
1139 /* FW Admin Queue command wrappers */
1140 
1141 /* Software lock/mutex that is meant to be held while the Global Config Lock
1142  * in firmware is acquired by the software to prevent most (but not all) types
1143  * of AQ commands from being sent to FW
1144  */
1145 DEFINE_MUTEX(ice_global_cfg_lock_sw);
1146 
1147 /**
1148  * ice_aq_send_cmd - send FW Admin Queue command to FW Admin Queue
1149  * @hw: pointer to the HW struct
1150  * @desc: descriptor describing the command
1151  * @buf: buffer to use for indirect commands (NULL for direct commands)
1152  * @buf_size: size of buffer for indirect commands (0 for direct commands)
1153  * @cd: pointer to command details structure
1154  *
1155  * Helper function to send FW Admin Queue commands to the FW Admin Queue.
1156  */
1157 enum ice_status
1158 ice_aq_send_cmd(struct ice_hw *hw, struct ice_aq_desc *desc, void *buf,
1159 		u16 buf_size, struct ice_sq_cd *cd)
1160 {
1161 	struct ice_aqc_req_res *cmd = &desc->params.res_owner;
1162 	bool lock_acquired = false;
1163 	enum ice_status status;
1164 
1165 	/* When a package download is in process (i.e. when the firmware's
1166 	 * Global Configuration Lock resource is held), only the Download
1167 	 * Package, Get Version, Get Package Info List and Release Resource
1168 	 * (with resource ID set to Global Config Lock) AdminQ commands are
1169 	 * allowed; all others must block until the package download completes
1170 	 * and the Global Config Lock is released.  See also
1171 	 * ice_acquire_global_cfg_lock().
1172 	 */
1173 	switch (le16_to_cpu(desc->opcode)) {
1174 	case ice_aqc_opc_download_pkg:
1175 	case ice_aqc_opc_get_pkg_info_list:
1176 	case ice_aqc_opc_get_ver:
1177 		break;
1178 	case ice_aqc_opc_release_res:
1179 		if (le16_to_cpu(cmd->res_id) == ICE_AQC_RES_ID_GLBL_LOCK)
1180 			break;
1181 		fallthrough;
1182 	default:
1183 		mutex_lock(&ice_global_cfg_lock_sw);
1184 		lock_acquired = true;
1185 		break;
1186 	}
1187 
1188 	status = ice_sq_send_cmd(hw, &hw->adminq, desc, buf, buf_size, cd);
1189 	if (lock_acquired)
1190 		mutex_unlock(&ice_global_cfg_lock_sw);
1191 
1192 	return status;
1193 }
1194 
1195 /**
1196  * ice_aq_get_fw_ver
1197  * @hw: pointer to the HW struct
1198  * @cd: pointer to command details structure or NULL
1199  *
1200  * Get the firmware version (0x0001) from the admin queue commands
1201  */
1202 enum ice_status ice_aq_get_fw_ver(struct ice_hw *hw, struct ice_sq_cd *cd)
1203 {
1204 	struct ice_aqc_get_ver *resp;
1205 	struct ice_aq_desc desc;
1206 	enum ice_status status;
1207 
1208 	resp = &desc.params.get_ver;
1209 
1210 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_ver);
1211 
1212 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1213 
1214 	if (!status) {
1215 		hw->fw_branch = resp->fw_branch;
1216 		hw->fw_maj_ver = resp->fw_major;
1217 		hw->fw_min_ver = resp->fw_minor;
1218 		hw->fw_patch = resp->fw_patch;
1219 		hw->fw_build = le32_to_cpu(resp->fw_build);
1220 		hw->api_branch = resp->api_branch;
1221 		hw->api_maj_ver = resp->api_major;
1222 		hw->api_min_ver = resp->api_minor;
1223 		hw->api_patch = resp->api_patch;
1224 	}
1225 
1226 	return status;
1227 }
1228 
1229 /**
1230  * ice_aq_send_driver_ver
1231  * @hw: pointer to the HW struct
1232  * @dv: driver's major, minor version
1233  * @cd: pointer to command details structure or NULL
1234  *
1235  * Send the driver version (0x0002) to the firmware
1236  */
1237 enum ice_status
1238 ice_aq_send_driver_ver(struct ice_hw *hw, struct ice_driver_ver *dv,
1239 		       struct ice_sq_cd *cd)
1240 {
1241 	struct ice_aqc_driver_ver *cmd;
1242 	struct ice_aq_desc desc;
1243 	u16 len;
1244 
1245 	cmd = &desc.params.driver_ver;
1246 
1247 	if (!dv)
1248 		return ICE_ERR_PARAM;
1249 
1250 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_driver_ver);
1251 
1252 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1253 	cmd->major_ver = dv->major_ver;
1254 	cmd->minor_ver = dv->minor_ver;
1255 	cmd->build_ver = dv->build_ver;
1256 	cmd->subbuild_ver = dv->subbuild_ver;
1257 
1258 	len = 0;
1259 	while (len < sizeof(dv->driver_string) &&
1260 	       isascii(dv->driver_string[len]) && dv->driver_string[len])
1261 		len++;
1262 
1263 	return ice_aq_send_cmd(hw, &desc, dv->driver_string, len, cd);
1264 }
1265 
1266 /**
1267  * ice_aq_q_shutdown
1268  * @hw: pointer to the HW struct
1269  * @unloading: is the driver unloading itself
1270  *
1271  * Tell the Firmware that we're shutting down the AdminQ and whether
1272  * or not the driver is unloading as well (0x0003).
1273  */
1274 enum ice_status ice_aq_q_shutdown(struct ice_hw *hw, bool unloading)
1275 {
1276 	struct ice_aqc_q_shutdown *cmd;
1277 	struct ice_aq_desc desc;
1278 
1279 	cmd = &desc.params.q_shutdown;
1280 
1281 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_q_shutdown);
1282 
1283 	if (unloading)
1284 		cmd->driver_unloading = ICE_AQC_DRIVER_UNLOADING;
1285 
1286 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1287 }
1288 
1289 /**
1290  * ice_aq_req_res
1291  * @hw: pointer to the HW struct
1292  * @res: resource ID
1293  * @access: access type
1294  * @sdp_number: resource number
1295  * @timeout: the maximum time in ms that the driver may hold the resource
1296  * @cd: pointer to command details structure or NULL
1297  *
1298  * Requests common resource using the admin queue commands (0x0008).
1299  * When attempting to acquire the Global Config Lock, the driver can
1300  * learn of three states:
1301  *  1) ICE_SUCCESS -        acquired lock, and can perform download package
1302  *  2) ICE_ERR_AQ_ERROR -   did not get lock, driver should fail to load
1303  *  3) ICE_ERR_AQ_NO_WORK - did not get lock, but another driver has
1304  *                          successfully downloaded the package; the driver does
1305  *                          not have to download the package and can continue
1306  *                          loading
1307  *
1308  * Note that if the caller is in an acquire lock, perform action, release lock
1309  * phase of operation, it is possible that the FW may detect a timeout and issue
1310  * a CORER. In this case, the driver will receive a CORER interrupt and will
1311  * have to determine its cause. The calling thread that is handling this flow
1312  * will likely get an error propagated back to it indicating the Download
1313  * Package, Update Package or the Release Resource AQ commands timed out.
1314  */
1315 static enum ice_status
1316 ice_aq_req_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1317 	       enum ice_aq_res_access_type access, u8 sdp_number, u32 *timeout,
1318 	       struct ice_sq_cd *cd)
1319 {
1320 	struct ice_aqc_req_res *cmd_resp;
1321 	struct ice_aq_desc desc;
1322 	enum ice_status status;
1323 
1324 	cmd_resp = &desc.params.res_owner;
1325 
1326 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_req_res);
1327 
1328 	cmd_resp->res_id = cpu_to_le16(res);
1329 	cmd_resp->access_type = cpu_to_le16(access);
1330 	cmd_resp->res_number = cpu_to_le32(sdp_number);
1331 	cmd_resp->timeout = cpu_to_le32(*timeout);
1332 	*timeout = 0;
1333 
1334 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1335 
1336 	/* The completion specifies the maximum time in ms that the driver
1337 	 * may hold the resource in the Timeout field.
1338 	 */
1339 
1340 	/* Global config lock response utilizes an additional status field.
1341 	 *
1342 	 * If the Global config lock resource is held by some other driver, the
1343 	 * command completes with ICE_AQ_RES_GLBL_IN_PROG in the status field
1344 	 * and the timeout field indicates the maximum time the current owner
1345 	 * of the resource has to free it.
1346 	 */
1347 	if (res == ICE_GLOBAL_CFG_LOCK_RES_ID) {
1348 		if (le16_to_cpu(cmd_resp->status) == ICE_AQ_RES_GLBL_SUCCESS) {
1349 			*timeout = le32_to_cpu(cmd_resp->timeout);
1350 			return 0;
1351 		} else if (le16_to_cpu(cmd_resp->status) ==
1352 			   ICE_AQ_RES_GLBL_IN_PROG) {
1353 			*timeout = le32_to_cpu(cmd_resp->timeout);
1354 			return ICE_ERR_AQ_ERROR;
1355 		} else if (le16_to_cpu(cmd_resp->status) ==
1356 			   ICE_AQ_RES_GLBL_DONE) {
1357 			return ICE_ERR_AQ_NO_WORK;
1358 		}
1359 
1360 		/* invalid FW response, force a timeout immediately */
1361 		*timeout = 0;
1362 		return ICE_ERR_AQ_ERROR;
1363 	}
1364 
1365 	/* If the resource is held by some other driver, the command completes
1366 	 * with a busy return value and the timeout field indicates the maximum
1367 	 * time the current owner of the resource has to free it.
1368 	 */
1369 	if (!status || hw->adminq.sq_last_status == ICE_AQ_RC_EBUSY)
1370 		*timeout = le32_to_cpu(cmd_resp->timeout);
1371 
1372 	return status;
1373 }
1374 
1375 /**
1376  * ice_aq_release_res
1377  * @hw: pointer to the HW struct
1378  * @res: resource ID
1379  * @sdp_number: resource number
1380  * @cd: pointer to command details structure or NULL
1381  *
1382  * release common resource using the admin queue commands (0x0009)
1383  */
1384 static enum ice_status
1385 ice_aq_release_res(struct ice_hw *hw, enum ice_aq_res_ids res, u8 sdp_number,
1386 		   struct ice_sq_cd *cd)
1387 {
1388 	struct ice_aqc_req_res *cmd;
1389 	struct ice_aq_desc desc;
1390 
1391 	cmd = &desc.params.res_owner;
1392 
1393 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_release_res);
1394 
1395 	cmd->res_id = cpu_to_le16(res);
1396 	cmd->res_number = cpu_to_le32(sdp_number);
1397 
1398 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1399 }
1400 
1401 /**
1402  * ice_acquire_res
1403  * @hw: pointer to the HW structure
1404  * @res: resource ID
1405  * @access: access type (read or write)
1406  * @timeout: timeout in milliseconds
1407  *
1408  * This function will attempt to acquire the ownership of a resource.
1409  */
1410 enum ice_status
1411 ice_acquire_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1412 		enum ice_aq_res_access_type access, u32 timeout)
1413 {
1414 #define ICE_RES_POLLING_DELAY_MS	10
1415 	u32 delay = ICE_RES_POLLING_DELAY_MS;
1416 	u32 time_left = timeout;
1417 	enum ice_status status;
1418 
1419 	status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1420 
1421 	/* A return code of ICE_ERR_AQ_NO_WORK means that another driver has
1422 	 * previously acquired the resource and performed any necessary updates;
1423 	 * in this case the caller does not obtain the resource and has no
1424 	 * further work to do.
1425 	 */
1426 	if (status == ICE_ERR_AQ_NO_WORK)
1427 		goto ice_acquire_res_exit;
1428 
1429 	if (status)
1430 		ice_debug(hw, ICE_DBG_RES,
1431 			  "resource %d acquire type %d failed.\n", res, access);
1432 
1433 	/* If necessary, poll until the current lock owner timeouts */
1434 	timeout = time_left;
1435 	while (status && timeout && time_left) {
1436 		mdelay(delay);
1437 		timeout = (timeout > delay) ? timeout - delay : 0;
1438 		status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1439 
1440 		if (status == ICE_ERR_AQ_NO_WORK)
1441 			/* lock free, but no work to do */
1442 			break;
1443 
1444 		if (!status)
1445 			/* lock acquired */
1446 			break;
1447 	}
1448 	if (status && status != ICE_ERR_AQ_NO_WORK)
1449 		ice_debug(hw, ICE_DBG_RES, "resource acquire timed out.\n");
1450 
1451 ice_acquire_res_exit:
1452 	if (status == ICE_ERR_AQ_NO_WORK) {
1453 		if (access == ICE_RES_WRITE)
1454 			ice_debug(hw, ICE_DBG_RES,
1455 				  "resource indicates no work to do.\n");
1456 		else
1457 			ice_debug(hw, ICE_DBG_RES,
1458 				  "Warning: ICE_ERR_AQ_NO_WORK not expected\n");
1459 	}
1460 	return status;
1461 }
1462 
1463 /**
1464  * ice_release_res
1465  * @hw: pointer to the HW structure
1466  * @res: resource ID
1467  *
1468  * This function will release a resource using the proper Admin Command.
1469  */
1470 void ice_release_res(struct ice_hw *hw, enum ice_aq_res_ids res)
1471 {
1472 	enum ice_status status;
1473 	u32 total_delay = 0;
1474 
1475 	status = ice_aq_release_res(hw, res, 0, NULL);
1476 
1477 	/* there are some rare cases when trying to release the resource
1478 	 * results in an admin queue timeout, so handle them correctly
1479 	 */
1480 	while ((status == ICE_ERR_AQ_TIMEOUT) &&
1481 	       (total_delay < hw->adminq.sq_cmd_timeout)) {
1482 		mdelay(1);
1483 		status = ice_aq_release_res(hw, res, 0, NULL);
1484 		total_delay++;
1485 	}
1486 }
1487 
1488 /**
1489  * ice_aq_alloc_free_res - command to allocate/free resources
1490  * @hw: pointer to the HW struct
1491  * @num_entries: number of resource entries in buffer
1492  * @buf: Indirect buffer to hold data parameters and response
1493  * @buf_size: size of buffer for indirect commands
1494  * @opc: pass in the command opcode
1495  * @cd: pointer to command details structure or NULL
1496  *
1497  * Helper function to allocate/free resources using the admin queue commands
1498  */
1499 enum ice_status
1500 ice_aq_alloc_free_res(struct ice_hw *hw, u16 num_entries,
1501 		      struct ice_aqc_alloc_free_res_elem *buf, u16 buf_size,
1502 		      enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1503 {
1504 	struct ice_aqc_alloc_free_res_cmd *cmd;
1505 	struct ice_aq_desc desc;
1506 
1507 	cmd = &desc.params.sw_res_ctrl;
1508 
1509 	if (!buf)
1510 		return ICE_ERR_PARAM;
1511 
1512 	if (buf_size < (num_entries * sizeof(buf->elem[0])))
1513 		return ICE_ERR_PARAM;
1514 
1515 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
1516 
1517 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1518 
1519 	cmd->num_entries = cpu_to_le16(num_entries);
1520 
1521 	return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1522 }
1523 
1524 /**
1525  * ice_alloc_hw_res - allocate resource
1526  * @hw: pointer to the HW struct
1527  * @type: type of resource
1528  * @num: number of resources to allocate
1529  * @btm: allocate from bottom
1530  * @res: pointer to array that will receive the resources
1531  */
1532 enum ice_status
1533 ice_alloc_hw_res(struct ice_hw *hw, u16 type, u16 num, bool btm, u16 *res)
1534 {
1535 	struct ice_aqc_alloc_free_res_elem *buf;
1536 	enum ice_status status;
1537 	u16 buf_len;
1538 
1539 	buf_len = struct_size(buf, elem, num);
1540 	buf = kzalloc(buf_len, GFP_KERNEL);
1541 	if (!buf)
1542 		return ICE_ERR_NO_MEMORY;
1543 
1544 	/* Prepare buffer to allocate resource. */
1545 	buf->num_elems = cpu_to_le16(num);
1546 	buf->res_type = cpu_to_le16(type | ICE_AQC_RES_TYPE_FLAG_DEDICATED |
1547 				    ICE_AQC_RES_TYPE_FLAG_IGNORE_INDEX);
1548 	if (btm)
1549 		buf->res_type |= cpu_to_le16(ICE_AQC_RES_TYPE_FLAG_SCAN_BOTTOM);
1550 
1551 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
1552 				       ice_aqc_opc_alloc_res, NULL);
1553 	if (status)
1554 		goto ice_alloc_res_exit;
1555 
1556 	memcpy(res, buf->elem, sizeof(*buf->elem) * num);
1557 
1558 ice_alloc_res_exit:
1559 	kfree(buf);
1560 	return status;
1561 }
1562 
1563 /**
1564  * ice_free_hw_res - free allocated HW resource
1565  * @hw: pointer to the HW struct
1566  * @type: type of resource to free
1567  * @num: number of resources
1568  * @res: pointer to array that contains the resources to free
1569  */
1570 enum ice_status
1571 ice_free_hw_res(struct ice_hw *hw, u16 type, u16 num, u16 *res)
1572 {
1573 	struct ice_aqc_alloc_free_res_elem *buf;
1574 	enum ice_status status;
1575 	u16 buf_len;
1576 
1577 	buf_len = struct_size(buf, elem, num);
1578 	buf = kzalloc(buf_len, GFP_KERNEL);
1579 	if (!buf)
1580 		return ICE_ERR_NO_MEMORY;
1581 
1582 	/* Prepare buffer to free resource. */
1583 	buf->num_elems = cpu_to_le16(num);
1584 	buf->res_type = cpu_to_le16(type);
1585 	memcpy(buf->elem, res, sizeof(*buf->elem) * num);
1586 
1587 	status = ice_aq_alloc_free_res(hw, num, buf, buf_len,
1588 				       ice_aqc_opc_free_res, NULL);
1589 	if (status)
1590 		ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
1591 
1592 	kfree(buf);
1593 	return status;
1594 }
1595 
1596 /**
1597  * ice_get_num_per_func - determine number of resources per PF
1598  * @hw: pointer to the HW structure
1599  * @max: value to be evenly split between each PF
1600  *
1601  * Determine the number of valid functions by going through the bitmap returned
1602  * from parsing capabilities and use this to calculate the number of resources
1603  * per PF based on the max value passed in.
1604  */
1605 static u32 ice_get_num_per_func(struct ice_hw *hw, u32 max)
1606 {
1607 	u8 funcs;
1608 
1609 #define ICE_CAPS_VALID_FUNCS_M	0xFF
1610 	funcs = hweight8(hw->dev_caps.common_cap.valid_functions &
1611 			 ICE_CAPS_VALID_FUNCS_M);
1612 
1613 	if (!funcs)
1614 		return 0;
1615 
1616 	return max / funcs;
1617 }
1618 
1619 /**
1620  * ice_parse_caps - parse function/device capabilities
1621  * @hw: pointer to the HW struct
1622  * @buf: pointer to a buffer containing function/device capability records
1623  * @cap_count: number of capability records in the list
1624  * @opc: type of capabilities list to parse
1625  *
1626  * Helper function to parse function(0x000a)/device(0x000b) capabilities list.
1627  */
1628 static void
1629 ice_parse_caps(struct ice_hw *hw, void *buf, u32 cap_count,
1630 	       enum ice_adminq_opc opc)
1631 {
1632 	struct ice_aqc_list_caps_elem *cap_resp;
1633 	struct ice_hw_func_caps *func_p = NULL;
1634 	struct ice_hw_dev_caps *dev_p = NULL;
1635 	struct ice_hw_common_caps *caps;
1636 	char const *prefix;
1637 	u32 i;
1638 
1639 	if (!buf)
1640 		return;
1641 
1642 	cap_resp = (struct ice_aqc_list_caps_elem *)buf;
1643 
1644 	if (opc == ice_aqc_opc_list_dev_caps) {
1645 		dev_p = &hw->dev_caps;
1646 		caps = &dev_p->common_cap;
1647 		prefix = "dev cap";
1648 	} else if (opc == ice_aqc_opc_list_func_caps) {
1649 		func_p = &hw->func_caps;
1650 		caps = &func_p->common_cap;
1651 		prefix = "func cap";
1652 	} else {
1653 		ice_debug(hw, ICE_DBG_INIT, "wrong opcode\n");
1654 		return;
1655 	}
1656 
1657 	for (i = 0; caps && i < cap_count; i++, cap_resp++) {
1658 		u32 logical_id = le32_to_cpu(cap_resp->logical_id);
1659 		u32 phys_id = le32_to_cpu(cap_resp->phys_id);
1660 		u32 number = le32_to_cpu(cap_resp->number);
1661 		u16 cap = le16_to_cpu(cap_resp->cap);
1662 
1663 		switch (cap) {
1664 		case ICE_AQC_CAPS_VALID_FUNCTIONS:
1665 			caps->valid_functions = number;
1666 			ice_debug(hw, ICE_DBG_INIT,
1667 				  "%s: valid_functions (bitmap) = %d\n", prefix,
1668 				  caps->valid_functions);
1669 
1670 			/* store func count for resource management purposes */
1671 			if (dev_p)
1672 				dev_p->num_funcs = hweight32(number);
1673 			break;
1674 		case ICE_AQC_CAPS_SRIOV:
1675 			caps->sr_iov_1_1 = (number == 1);
1676 			ice_debug(hw, ICE_DBG_INIT,
1677 				  "%s: sr_iov_1_1 = %d\n", prefix,
1678 				  caps->sr_iov_1_1);
1679 			break;
1680 		case ICE_AQC_CAPS_VF:
1681 			if (dev_p) {
1682 				dev_p->num_vfs_exposed = number;
1683 				ice_debug(hw, ICE_DBG_INIT,
1684 					  "%s: num_vfs_exposed = %d\n", prefix,
1685 					  dev_p->num_vfs_exposed);
1686 			} else if (func_p) {
1687 				func_p->num_allocd_vfs = number;
1688 				func_p->vf_base_id = logical_id;
1689 				ice_debug(hw, ICE_DBG_INIT,
1690 					  "%s: num_allocd_vfs = %d\n", prefix,
1691 					  func_p->num_allocd_vfs);
1692 				ice_debug(hw, ICE_DBG_INIT,
1693 					  "%s: vf_base_id = %d\n", prefix,
1694 					  func_p->vf_base_id);
1695 			}
1696 			break;
1697 		case ICE_AQC_CAPS_VSI:
1698 			if (dev_p) {
1699 				dev_p->num_vsi_allocd_to_host = number;
1700 				ice_debug(hw, ICE_DBG_INIT,
1701 					  "%s: num_vsi_allocd_to_host = %d\n",
1702 					  prefix,
1703 					  dev_p->num_vsi_allocd_to_host);
1704 			} else if (func_p) {
1705 				func_p->guar_num_vsi =
1706 					ice_get_num_per_func(hw, ICE_MAX_VSI);
1707 				ice_debug(hw, ICE_DBG_INIT,
1708 					  "%s: guar_num_vsi (fw) = %d\n",
1709 					  prefix, number);
1710 				ice_debug(hw, ICE_DBG_INIT,
1711 					  "%s: guar_num_vsi = %d\n",
1712 					  prefix, func_p->guar_num_vsi);
1713 			}
1714 			break;
1715 		case ICE_AQC_CAPS_DCB:
1716 			caps->dcb = (number == 1);
1717 			caps->active_tc_bitmap = logical_id;
1718 			caps->maxtc = phys_id;
1719 			ice_debug(hw, ICE_DBG_INIT,
1720 				  "%s: dcb = %d\n", prefix, caps->dcb);
1721 			ice_debug(hw, ICE_DBG_INIT,
1722 				  "%s: active_tc_bitmap = %d\n", prefix,
1723 				  caps->active_tc_bitmap);
1724 			ice_debug(hw, ICE_DBG_INIT,
1725 				  "%s: maxtc = %d\n", prefix, caps->maxtc);
1726 			break;
1727 		case ICE_AQC_CAPS_RSS:
1728 			caps->rss_table_size = number;
1729 			caps->rss_table_entry_width = logical_id;
1730 			ice_debug(hw, ICE_DBG_INIT,
1731 				  "%s: rss_table_size = %d\n", prefix,
1732 				  caps->rss_table_size);
1733 			ice_debug(hw, ICE_DBG_INIT,
1734 				  "%s: rss_table_entry_width = %d\n", prefix,
1735 				  caps->rss_table_entry_width);
1736 			break;
1737 		case ICE_AQC_CAPS_RXQS:
1738 			caps->num_rxq = number;
1739 			caps->rxq_first_id = phys_id;
1740 			ice_debug(hw, ICE_DBG_INIT,
1741 				  "%s: num_rxq = %d\n", prefix,
1742 				  caps->num_rxq);
1743 			ice_debug(hw, ICE_DBG_INIT,
1744 				  "%s: rxq_first_id = %d\n", prefix,
1745 				  caps->rxq_first_id);
1746 			break;
1747 		case ICE_AQC_CAPS_TXQS:
1748 			caps->num_txq = number;
1749 			caps->txq_first_id = phys_id;
1750 			ice_debug(hw, ICE_DBG_INIT,
1751 				  "%s: num_txq = %d\n", prefix,
1752 				  caps->num_txq);
1753 			ice_debug(hw, ICE_DBG_INIT,
1754 				  "%s: txq_first_id = %d\n", prefix,
1755 				  caps->txq_first_id);
1756 			break;
1757 		case ICE_AQC_CAPS_MSIX:
1758 			caps->num_msix_vectors = number;
1759 			caps->msix_vector_first_id = phys_id;
1760 			ice_debug(hw, ICE_DBG_INIT,
1761 				  "%s: num_msix_vectors = %d\n", prefix,
1762 				  caps->num_msix_vectors);
1763 			ice_debug(hw, ICE_DBG_INIT,
1764 				  "%s: msix_vector_first_id = %d\n", prefix,
1765 				  caps->msix_vector_first_id);
1766 			break;
1767 		case ICE_AQC_CAPS_FD:
1768 			if (dev_p) {
1769 				dev_p->num_flow_director_fltr = number;
1770 				ice_debug(hw, ICE_DBG_INIT,
1771 					  "%s: num_flow_director_fltr = %d\n",
1772 					  prefix,
1773 					  dev_p->num_flow_director_fltr);
1774 			}
1775 			if (func_p) {
1776 				u32 reg_val, val;
1777 
1778 				reg_val = rd32(hw, GLQF_FD_SIZE);
1779 				val = (reg_val & GLQF_FD_SIZE_FD_GSIZE_M) >>
1780 				      GLQF_FD_SIZE_FD_GSIZE_S;
1781 				func_p->fd_fltr_guar =
1782 				      ice_get_num_per_func(hw, val);
1783 				val = (reg_val & GLQF_FD_SIZE_FD_BSIZE_M) >>
1784 				      GLQF_FD_SIZE_FD_BSIZE_S;
1785 				func_p->fd_fltr_best_effort = val;
1786 				ice_debug(hw, ICE_DBG_INIT,
1787 					  "%s: fd_fltr_guar = %d\n",
1788 					  prefix, func_p->fd_fltr_guar);
1789 				ice_debug(hw, ICE_DBG_INIT,
1790 					  "%s: fd_fltr_best_effort = %d\n",
1791 					  prefix, func_p->fd_fltr_best_effort);
1792 			}
1793 			break;
1794 		case ICE_AQC_CAPS_MAX_MTU:
1795 			caps->max_mtu = number;
1796 			ice_debug(hw, ICE_DBG_INIT, "%s: max_mtu = %d\n",
1797 				  prefix, caps->max_mtu);
1798 			break;
1799 		default:
1800 			ice_debug(hw, ICE_DBG_INIT,
1801 				  "%s: unknown capability[%d]: 0x%x\n", prefix,
1802 				  i, cap);
1803 			break;
1804 		}
1805 	}
1806 
1807 	/* Re-calculate capabilities that are dependent on the number of
1808 	 * physical ports; i.e. some features are not supported or function
1809 	 * differently on devices with more than 4 ports.
1810 	 */
1811 	if (hw->dev_caps.num_funcs > 4) {
1812 		/* Max 4 TCs per port */
1813 		caps->maxtc = 4;
1814 		ice_debug(hw, ICE_DBG_INIT,
1815 			  "%s: maxtc = %d (based on #ports)\n", prefix,
1816 			  caps->maxtc);
1817 	}
1818 }
1819 
1820 /**
1821  * ice_aq_list_caps - query function/device capabilities
1822  * @hw: pointer to the HW struct
1823  * @buf: a buffer to hold the capabilities
1824  * @buf_size: size of the buffer
1825  * @cap_count: if not NULL, set to the number of capabilities reported
1826  * @opc: capabilities type to discover, device or function
1827  * @cd: pointer to command details structure or NULL
1828  *
1829  * Get the function (0x000A) or device (0x000B) capabilities description from
1830  * firmware and store it in the buffer.
1831  *
1832  * If the cap_count pointer is not NULL, then it is set to the number of
1833  * capabilities firmware will report. Note that if the buffer size is too
1834  * small, it is possible the command will return ICE_AQ_ERR_ENOMEM. The
1835  * cap_count will still be updated in this case. It is recommended that the
1836  * buffer size be set to ICE_AQ_MAX_BUF_LEN (the largest possible buffer that
1837  * firmware could return) to avoid this.
1838  */
1839 enum ice_status
1840 ice_aq_list_caps(struct ice_hw *hw, void *buf, u16 buf_size, u32 *cap_count,
1841 		 enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1842 {
1843 	struct ice_aqc_list_caps *cmd;
1844 	struct ice_aq_desc desc;
1845 	enum ice_status status;
1846 
1847 	cmd = &desc.params.get_cap;
1848 
1849 	if (opc != ice_aqc_opc_list_func_caps &&
1850 	    opc != ice_aqc_opc_list_dev_caps)
1851 		return ICE_ERR_PARAM;
1852 
1853 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
1854 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1855 
1856 	if (cap_count)
1857 		*cap_count = le32_to_cpu(cmd->count);
1858 
1859 	return status;
1860 }
1861 
1862 /**
1863  * ice_aq_discover_caps - query function/device capabilities
1864  * @hw: pointer to the HW struct
1865  * @buf: a virtual buffer to hold the capabilities
1866  * @buf_size: Size of the virtual buffer
1867  * @cap_count: cap count needed if AQ err==ENOMEM
1868  * @opc: capabilities type to discover - pass in the command opcode
1869  * @cd: pointer to command details structure or NULL
1870  *
1871  * Get the function(0x000a)/device(0x000b) capabilities description from
1872  * the firmware.
1873  *
1874  * NOTE: this function has the side effect of updating the hw->dev_caps or
1875  * hw->func_caps by way of calling ice_parse_caps.
1876  */
1877 static enum ice_status
1878 ice_aq_discover_caps(struct ice_hw *hw, void *buf, u16 buf_size, u32 *cap_count,
1879 		     enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1880 {
1881 	u32 local_cap_count = 0;
1882 	enum ice_status status;
1883 
1884 	status = ice_aq_list_caps(hw, buf, buf_size, &local_cap_count,
1885 				  opc, cd);
1886 	if (!status)
1887 		ice_parse_caps(hw, buf, local_cap_count, opc);
1888 	else if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOMEM)
1889 		*cap_count = local_cap_count;
1890 
1891 	return status;
1892 }
1893 
1894 /**
1895  * ice_discover_caps - get info about the HW
1896  * @hw: pointer to the hardware structure
1897  * @opc: capabilities type to discover - pass in the command opcode
1898  */
1899 static enum ice_status
1900 ice_discover_caps(struct ice_hw *hw, enum ice_adminq_opc opc)
1901 {
1902 	enum ice_status status;
1903 	u32 cap_count;
1904 	u16 cbuf_len;
1905 	u8 retries;
1906 
1907 	/* The driver doesn't know how many capabilities the device will return
1908 	 * so the buffer size required isn't known ahead of time. The driver
1909 	 * starts with cbuf_len and if this turns out to be insufficient, the
1910 	 * device returns ICE_AQ_RC_ENOMEM and also the cap_count it needs.
1911 	 * The driver then allocates the buffer based on the count and retries
1912 	 * the operation. So it follows that the retry count is 2.
1913 	 */
1914 #define ICE_GET_CAP_BUF_COUNT	40
1915 #define ICE_GET_CAP_RETRY_COUNT	2
1916 
1917 	cap_count = ICE_GET_CAP_BUF_COUNT;
1918 	retries = ICE_GET_CAP_RETRY_COUNT;
1919 
1920 	do {
1921 		void *cbuf;
1922 
1923 		cbuf_len = (u16)(cap_count *
1924 				 sizeof(struct ice_aqc_list_caps_elem));
1925 		cbuf = devm_kzalloc(ice_hw_to_dev(hw), cbuf_len, GFP_KERNEL);
1926 		if (!cbuf)
1927 			return ICE_ERR_NO_MEMORY;
1928 
1929 		status = ice_aq_discover_caps(hw, cbuf, cbuf_len, &cap_count,
1930 					      opc, NULL);
1931 		devm_kfree(ice_hw_to_dev(hw), cbuf);
1932 
1933 		if (!status || hw->adminq.sq_last_status != ICE_AQ_RC_ENOMEM)
1934 			break;
1935 
1936 		/* If ENOMEM is returned, try again with bigger buffer */
1937 	} while (--retries);
1938 
1939 	return status;
1940 }
1941 
1942 /**
1943  * ice_set_safe_mode_caps - Override dev/func capabilities when in safe mode
1944  * @hw: pointer to the hardware structure
1945  */
1946 void ice_set_safe_mode_caps(struct ice_hw *hw)
1947 {
1948 	struct ice_hw_func_caps *func_caps = &hw->func_caps;
1949 	struct ice_hw_dev_caps *dev_caps = &hw->dev_caps;
1950 	u32 valid_func, rxq_first_id, txq_first_id;
1951 	u32 msix_vector_first_id, max_mtu;
1952 	u32 num_funcs;
1953 
1954 	/* cache some func_caps values that should be restored after memset */
1955 	valid_func = func_caps->common_cap.valid_functions;
1956 	txq_first_id = func_caps->common_cap.txq_first_id;
1957 	rxq_first_id = func_caps->common_cap.rxq_first_id;
1958 	msix_vector_first_id = func_caps->common_cap.msix_vector_first_id;
1959 	max_mtu = func_caps->common_cap.max_mtu;
1960 
1961 	/* unset func capabilities */
1962 	memset(func_caps, 0, sizeof(*func_caps));
1963 
1964 	/* restore cached values */
1965 	func_caps->common_cap.valid_functions = valid_func;
1966 	func_caps->common_cap.txq_first_id = txq_first_id;
1967 	func_caps->common_cap.rxq_first_id = rxq_first_id;
1968 	func_caps->common_cap.msix_vector_first_id = msix_vector_first_id;
1969 	func_caps->common_cap.max_mtu = max_mtu;
1970 
1971 	/* one Tx and one Rx queue in safe mode */
1972 	func_caps->common_cap.num_rxq = 1;
1973 	func_caps->common_cap.num_txq = 1;
1974 
1975 	/* two MSIX vectors, one for traffic and one for misc causes */
1976 	func_caps->common_cap.num_msix_vectors = 2;
1977 	func_caps->guar_num_vsi = 1;
1978 
1979 	/* cache some dev_caps values that should be restored after memset */
1980 	valid_func = dev_caps->common_cap.valid_functions;
1981 	txq_first_id = dev_caps->common_cap.txq_first_id;
1982 	rxq_first_id = dev_caps->common_cap.rxq_first_id;
1983 	msix_vector_first_id = dev_caps->common_cap.msix_vector_first_id;
1984 	max_mtu = dev_caps->common_cap.max_mtu;
1985 	num_funcs = dev_caps->num_funcs;
1986 
1987 	/* unset dev capabilities */
1988 	memset(dev_caps, 0, sizeof(*dev_caps));
1989 
1990 	/* restore cached values */
1991 	dev_caps->common_cap.valid_functions = valid_func;
1992 	dev_caps->common_cap.txq_first_id = txq_first_id;
1993 	dev_caps->common_cap.rxq_first_id = rxq_first_id;
1994 	dev_caps->common_cap.msix_vector_first_id = msix_vector_first_id;
1995 	dev_caps->common_cap.max_mtu = max_mtu;
1996 	dev_caps->num_funcs = num_funcs;
1997 
1998 	/* one Tx and one Rx queue per function in safe mode */
1999 	dev_caps->common_cap.num_rxq = num_funcs;
2000 	dev_caps->common_cap.num_txq = num_funcs;
2001 
2002 	/* two MSIX vectors per function */
2003 	dev_caps->common_cap.num_msix_vectors = 2 * num_funcs;
2004 }
2005 
2006 /**
2007  * ice_get_caps - get info about the HW
2008  * @hw: pointer to the hardware structure
2009  */
2010 enum ice_status ice_get_caps(struct ice_hw *hw)
2011 {
2012 	enum ice_status status;
2013 
2014 	status = ice_discover_caps(hw, ice_aqc_opc_list_dev_caps);
2015 	if (!status)
2016 		status = ice_discover_caps(hw, ice_aqc_opc_list_func_caps);
2017 
2018 	return status;
2019 }
2020 
2021 /**
2022  * ice_aq_manage_mac_write - manage MAC address write command
2023  * @hw: pointer to the HW struct
2024  * @mac_addr: MAC address to be written as LAA/LAA+WoL/Port address
2025  * @flags: flags to control write behavior
2026  * @cd: pointer to command details structure or NULL
2027  *
2028  * This function is used to write MAC address to the NVM (0x0108).
2029  */
2030 enum ice_status
2031 ice_aq_manage_mac_write(struct ice_hw *hw, const u8 *mac_addr, u8 flags,
2032 			struct ice_sq_cd *cd)
2033 {
2034 	struct ice_aqc_manage_mac_write *cmd;
2035 	struct ice_aq_desc desc;
2036 
2037 	cmd = &desc.params.mac_write;
2038 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_write);
2039 
2040 	cmd->flags = flags;
2041 	ether_addr_copy(cmd->mac_addr, mac_addr);
2042 
2043 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2044 }
2045 
2046 /**
2047  * ice_aq_clear_pxe_mode
2048  * @hw: pointer to the HW struct
2049  *
2050  * Tell the firmware that the driver is taking over from PXE (0x0110).
2051  */
2052 static enum ice_status ice_aq_clear_pxe_mode(struct ice_hw *hw)
2053 {
2054 	struct ice_aq_desc desc;
2055 
2056 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pxe_mode);
2057 	desc.params.clear_pxe.rx_cnt = ICE_AQC_CLEAR_PXE_RX_CNT;
2058 
2059 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
2060 }
2061 
2062 /**
2063  * ice_clear_pxe_mode - clear pxe operations mode
2064  * @hw: pointer to the HW struct
2065  *
2066  * Make sure all PXE mode settings are cleared, including things
2067  * like descriptor fetch/write-back mode.
2068  */
2069 void ice_clear_pxe_mode(struct ice_hw *hw)
2070 {
2071 	if (ice_check_sq_alive(hw, &hw->adminq))
2072 		ice_aq_clear_pxe_mode(hw);
2073 }
2074 
2075 /**
2076  * ice_get_link_speed_based_on_phy_type - returns link speed
2077  * @phy_type_low: lower part of phy_type
2078  * @phy_type_high: higher part of phy_type
2079  *
2080  * This helper function will convert an entry in PHY type structure
2081  * [phy_type_low, phy_type_high] to its corresponding link speed.
2082  * Note: In the structure of [phy_type_low, phy_type_high], there should
2083  * be one bit set, as this function will convert one PHY type to its
2084  * speed.
2085  * If no bit gets set, ICE_LINK_SPEED_UNKNOWN will be returned
2086  * If more than one bit gets set, ICE_LINK_SPEED_UNKNOWN will be returned
2087  */
2088 static u16
2089 ice_get_link_speed_based_on_phy_type(u64 phy_type_low, u64 phy_type_high)
2090 {
2091 	u16 speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
2092 	u16 speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
2093 
2094 	switch (phy_type_low) {
2095 	case ICE_PHY_TYPE_LOW_100BASE_TX:
2096 	case ICE_PHY_TYPE_LOW_100M_SGMII:
2097 		speed_phy_type_low = ICE_AQ_LINK_SPEED_100MB;
2098 		break;
2099 	case ICE_PHY_TYPE_LOW_1000BASE_T:
2100 	case ICE_PHY_TYPE_LOW_1000BASE_SX:
2101 	case ICE_PHY_TYPE_LOW_1000BASE_LX:
2102 	case ICE_PHY_TYPE_LOW_1000BASE_KX:
2103 	case ICE_PHY_TYPE_LOW_1G_SGMII:
2104 		speed_phy_type_low = ICE_AQ_LINK_SPEED_1000MB;
2105 		break;
2106 	case ICE_PHY_TYPE_LOW_2500BASE_T:
2107 	case ICE_PHY_TYPE_LOW_2500BASE_X:
2108 	case ICE_PHY_TYPE_LOW_2500BASE_KX:
2109 		speed_phy_type_low = ICE_AQ_LINK_SPEED_2500MB;
2110 		break;
2111 	case ICE_PHY_TYPE_LOW_5GBASE_T:
2112 	case ICE_PHY_TYPE_LOW_5GBASE_KR:
2113 		speed_phy_type_low = ICE_AQ_LINK_SPEED_5GB;
2114 		break;
2115 	case ICE_PHY_TYPE_LOW_10GBASE_T:
2116 	case ICE_PHY_TYPE_LOW_10G_SFI_DA:
2117 	case ICE_PHY_TYPE_LOW_10GBASE_SR:
2118 	case ICE_PHY_TYPE_LOW_10GBASE_LR:
2119 	case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
2120 	case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
2121 	case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
2122 		speed_phy_type_low = ICE_AQ_LINK_SPEED_10GB;
2123 		break;
2124 	case ICE_PHY_TYPE_LOW_25GBASE_T:
2125 	case ICE_PHY_TYPE_LOW_25GBASE_CR:
2126 	case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
2127 	case ICE_PHY_TYPE_LOW_25GBASE_CR1:
2128 	case ICE_PHY_TYPE_LOW_25GBASE_SR:
2129 	case ICE_PHY_TYPE_LOW_25GBASE_LR:
2130 	case ICE_PHY_TYPE_LOW_25GBASE_KR:
2131 	case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
2132 	case ICE_PHY_TYPE_LOW_25GBASE_KR1:
2133 	case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
2134 	case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
2135 		speed_phy_type_low = ICE_AQ_LINK_SPEED_25GB;
2136 		break;
2137 	case ICE_PHY_TYPE_LOW_40GBASE_CR4:
2138 	case ICE_PHY_TYPE_LOW_40GBASE_SR4:
2139 	case ICE_PHY_TYPE_LOW_40GBASE_LR4:
2140 	case ICE_PHY_TYPE_LOW_40GBASE_KR4:
2141 	case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
2142 	case ICE_PHY_TYPE_LOW_40G_XLAUI:
2143 		speed_phy_type_low = ICE_AQ_LINK_SPEED_40GB;
2144 		break;
2145 	case ICE_PHY_TYPE_LOW_50GBASE_CR2:
2146 	case ICE_PHY_TYPE_LOW_50GBASE_SR2:
2147 	case ICE_PHY_TYPE_LOW_50GBASE_LR2:
2148 	case ICE_PHY_TYPE_LOW_50GBASE_KR2:
2149 	case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
2150 	case ICE_PHY_TYPE_LOW_50G_LAUI2:
2151 	case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
2152 	case ICE_PHY_TYPE_LOW_50G_AUI2:
2153 	case ICE_PHY_TYPE_LOW_50GBASE_CP:
2154 	case ICE_PHY_TYPE_LOW_50GBASE_SR:
2155 	case ICE_PHY_TYPE_LOW_50GBASE_FR:
2156 	case ICE_PHY_TYPE_LOW_50GBASE_LR:
2157 	case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
2158 	case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
2159 	case ICE_PHY_TYPE_LOW_50G_AUI1:
2160 		speed_phy_type_low = ICE_AQ_LINK_SPEED_50GB;
2161 		break;
2162 	case ICE_PHY_TYPE_LOW_100GBASE_CR4:
2163 	case ICE_PHY_TYPE_LOW_100GBASE_SR4:
2164 	case ICE_PHY_TYPE_LOW_100GBASE_LR4:
2165 	case ICE_PHY_TYPE_LOW_100GBASE_KR4:
2166 	case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
2167 	case ICE_PHY_TYPE_LOW_100G_CAUI4:
2168 	case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
2169 	case ICE_PHY_TYPE_LOW_100G_AUI4:
2170 	case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
2171 	case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
2172 	case ICE_PHY_TYPE_LOW_100GBASE_CP2:
2173 	case ICE_PHY_TYPE_LOW_100GBASE_SR2:
2174 	case ICE_PHY_TYPE_LOW_100GBASE_DR:
2175 		speed_phy_type_low = ICE_AQ_LINK_SPEED_100GB;
2176 		break;
2177 	default:
2178 		speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
2179 		break;
2180 	}
2181 
2182 	switch (phy_type_high) {
2183 	case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
2184 	case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
2185 	case ICE_PHY_TYPE_HIGH_100G_CAUI2:
2186 	case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
2187 	case ICE_PHY_TYPE_HIGH_100G_AUI2:
2188 		speed_phy_type_high = ICE_AQ_LINK_SPEED_100GB;
2189 		break;
2190 	default:
2191 		speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
2192 		break;
2193 	}
2194 
2195 	if (speed_phy_type_low == ICE_AQ_LINK_SPEED_UNKNOWN &&
2196 	    speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
2197 		return ICE_AQ_LINK_SPEED_UNKNOWN;
2198 	else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
2199 		 speed_phy_type_high != ICE_AQ_LINK_SPEED_UNKNOWN)
2200 		return ICE_AQ_LINK_SPEED_UNKNOWN;
2201 	else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
2202 		 speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
2203 		return speed_phy_type_low;
2204 	else
2205 		return speed_phy_type_high;
2206 }
2207 
2208 /**
2209  * ice_update_phy_type
2210  * @phy_type_low: pointer to the lower part of phy_type
2211  * @phy_type_high: pointer to the higher part of phy_type
2212  * @link_speeds_bitmap: targeted link speeds bitmap
2213  *
2214  * Note: For the link_speeds_bitmap structure, you can check it at
2215  * [ice_aqc_get_link_status->link_speed]. Caller can pass in
2216  * link_speeds_bitmap include multiple speeds.
2217  *
2218  * Each entry in this [phy_type_low, phy_type_high] structure will
2219  * present a certain link speed. This helper function will turn on bits
2220  * in [phy_type_low, phy_type_high] structure based on the value of
2221  * link_speeds_bitmap input parameter.
2222  */
2223 void
2224 ice_update_phy_type(u64 *phy_type_low, u64 *phy_type_high,
2225 		    u16 link_speeds_bitmap)
2226 {
2227 	u64 pt_high;
2228 	u64 pt_low;
2229 	int index;
2230 	u16 speed;
2231 
2232 	/* We first check with low part of phy_type */
2233 	for (index = 0; index <= ICE_PHY_TYPE_LOW_MAX_INDEX; index++) {
2234 		pt_low = BIT_ULL(index);
2235 		speed = ice_get_link_speed_based_on_phy_type(pt_low, 0);
2236 
2237 		if (link_speeds_bitmap & speed)
2238 			*phy_type_low |= BIT_ULL(index);
2239 	}
2240 
2241 	/* We then check with high part of phy_type */
2242 	for (index = 0; index <= ICE_PHY_TYPE_HIGH_MAX_INDEX; index++) {
2243 		pt_high = BIT_ULL(index);
2244 		speed = ice_get_link_speed_based_on_phy_type(0, pt_high);
2245 
2246 		if (link_speeds_bitmap & speed)
2247 			*phy_type_high |= BIT_ULL(index);
2248 	}
2249 }
2250 
2251 /**
2252  * ice_aq_set_phy_cfg
2253  * @hw: pointer to the HW struct
2254  * @lport: logical port number
2255  * @cfg: structure with PHY configuration data to be set
2256  * @cd: pointer to command details structure or NULL
2257  *
2258  * Set the various PHY configuration parameters supported on the Port.
2259  * One or more of the Set PHY config parameters may be ignored in an MFP
2260  * mode as the PF may not have the privilege to set some of the PHY Config
2261  * parameters. This status will be indicated by the command response (0x0601).
2262  */
2263 enum ice_status
2264 ice_aq_set_phy_cfg(struct ice_hw *hw, u8 lport,
2265 		   struct ice_aqc_set_phy_cfg_data *cfg, struct ice_sq_cd *cd)
2266 {
2267 	struct ice_aq_desc desc;
2268 	enum ice_status status;
2269 
2270 	if (!cfg)
2271 		return ICE_ERR_PARAM;
2272 
2273 	/* Ensure that only valid bits of cfg->caps can be turned on. */
2274 	if (cfg->caps & ~ICE_AQ_PHY_ENA_VALID_MASK) {
2275 		ice_debug(hw, ICE_DBG_PHY,
2276 			  "Invalid bit is set in ice_aqc_set_phy_cfg_data->caps : 0x%x\n",
2277 			  cfg->caps);
2278 
2279 		cfg->caps &= ICE_AQ_PHY_ENA_VALID_MASK;
2280 	}
2281 
2282 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_phy_cfg);
2283 	desc.params.set_phy.lport_num = lport;
2284 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2285 
2286 	ice_debug(hw, ICE_DBG_LINK, "phy_type_low = 0x%llx\n",
2287 		  (unsigned long long)le64_to_cpu(cfg->phy_type_low));
2288 	ice_debug(hw, ICE_DBG_LINK, "phy_type_high = 0x%llx\n",
2289 		  (unsigned long long)le64_to_cpu(cfg->phy_type_high));
2290 	ice_debug(hw, ICE_DBG_LINK, "caps = 0x%x\n", cfg->caps);
2291 	ice_debug(hw, ICE_DBG_LINK, "low_power_ctrl = 0x%x\n",
2292 		  cfg->low_power_ctrl);
2293 	ice_debug(hw, ICE_DBG_LINK, "eee_cap = 0x%x\n", cfg->eee_cap);
2294 	ice_debug(hw, ICE_DBG_LINK, "eeer_value = 0x%x\n", cfg->eeer_value);
2295 	ice_debug(hw, ICE_DBG_LINK, "link_fec_opt = 0x%x\n", cfg->link_fec_opt);
2296 
2297 	status = ice_aq_send_cmd(hw, &desc, cfg, sizeof(*cfg), cd);
2298 	if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
2299 		status = 0;
2300 
2301 	return status;
2302 }
2303 
2304 /**
2305  * ice_update_link_info - update status of the HW network link
2306  * @pi: port info structure of the interested logical port
2307  */
2308 enum ice_status ice_update_link_info(struct ice_port_info *pi)
2309 {
2310 	struct ice_link_status *li;
2311 	enum ice_status status;
2312 
2313 	if (!pi)
2314 		return ICE_ERR_PARAM;
2315 
2316 	li = &pi->phy.link_info;
2317 
2318 	status = ice_aq_get_link_info(pi, true, NULL, NULL);
2319 	if (status)
2320 		return status;
2321 
2322 	if (li->link_info & ICE_AQ_MEDIA_AVAILABLE) {
2323 		struct ice_aqc_get_phy_caps_data *pcaps;
2324 		struct ice_hw *hw;
2325 
2326 		hw = pi->hw;
2327 		pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps),
2328 				     GFP_KERNEL);
2329 		if (!pcaps)
2330 			return ICE_ERR_NO_MEMORY;
2331 
2332 		status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
2333 					     pcaps, NULL);
2334 		if (!status)
2335 			memcpy(li->module_type, &pcaps->module_type,
2336 			       sizeof(li->module_type));
2337 
2338 		devm_kfree(ice_hw_to_dev(hw), pcaps);
2339 	}
2340 
2341 	return status;
2342 }
2343 
2344 /**
2345  * ice_set_fc
2346  * @pi: port information structure
2347  * @aq_failures: pointer to status code, specific to ice_set_fc routine
2348  * @ena_auto_link_update: enable automatic link update
2349  *
2350  * Set the requested flow control mode.
2351  */
2352 enum ice_status
2353 ice_set_fc(struct ice_port_info *pi, u8 *aq_failures, bool ena_auto_link_update)
2354 {
2355 	struct ice_aqc_set_phy_cfg_data cfg = { 0 };
2356 	struct ice_aqc_get_phy_caps_data *pcaps;
2357 	enum ice_status status;
2358 	u8 pause_mask = 0x0;
2359 	struct ice_hw *hw;
2360 
2361 	if (!pi)
2362 		return ICE_ERR_PARAM;
2363 	hw = pi->hw;
2364 	*aq_failures = ICE_SET_FC_AQ_FAIL_NONE;
2365 
2366 	switch (pi->fc.req_mode) {
2367 	case ICE_FC_FULL:
2368 		pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
2369 		pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
2370 		break;
2371 	case ICE_FC_RX_PAUSE:
2372 		pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
2373 		break;
2374 	case ICE_FC_TX_PAUSE:
2375 		pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
2376 		break;
2377 	default:
2378 		break;
2379 	}
2380 
2381 	pcaps = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*pcaps), GFP_KERNEL);
2382 	if (!pcaps)
2383 		return ICE_ERR_NO_MEMORY;
2384 
2385 	/* Get the current PHY config */
2386 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2387 				     NULL);
2388 	if (status) {
2389 		*aq_failures = ICE_SET_FC_AQ_FAIL_GET;
2390 		goto out;
2391 	}
2392 
2393 	/* clear the old pause settings */
2394 	cfg.caps = pcaps->caps & ~(ICE_AQC_PHY_EN_TX_LINK_PAUSE |
2395 				   ICE_AQC_PHY_EN_RX_LINK_PAUSE);
2396 
2397 	/* set the new capabilities */
2398 	cfg.caps |= pause_mask;
2399 
2400 	/* If the capabilities have changed, then set the new config */
2401 	if (cfg.caps != pcaps->caps) {
2402 		int retry_count, retry_max = 10;
2403 
2404 		/* Auto restart link so settings take effect */
2405 		if (ena_auto_link_update)
2406 			cfg.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2407 		/* Copy over all the old settings */
2408 		cfg.phy_type_high = pcaps->phy_type_high;
2409 		cfg.phy_type_low = pcaps->phy_type_low;
2410 		cfg.low_power_ctrl = pcaps->low_power_ctrl;
2411 		cfg.eee_cap = pcaps->eee_cap;
2412 		cfg.eeer_value = pcaps->eeer_value;
2413 		cfg.link_fec_opt = pcaps->link_fec_options;
2414 
2415 		status = ice_aq_set_phy_cfg(hw, pi->lport, &cfg, NULL);
2416 		if (status) {
2417 			*aq_failures = ICE_SET_FC_AQ_FAIL_SET;
2418 			goto out;
2419 		}
2420 
2421 		/* Update the link info
2422 		 * It sometimes takes a really long time for link to
2423 		 * come back from the atomic reset. Thus, we wait a
2424 		 * little bit.
2425 		 */
2426 		for (retry_count = 0; retry_count < retry_max; retry_count++) {
2427 			status = ice_update_link_info(pi);
2428 
2429 			if (!status)
2430 				break;
2431 
2432 			mdelay(100);
2433 		}
2434 
2435 		if (status)
2436 			*aq_failures = ICE_SET_FC_AQ_FAIL_UPDATE;
2437 	}
2438 
2439 out:
2440 	devm_kfree(ice_hw_to_dev(hw), pcaps);
2441 	return status;
2442 }
2443 
2444 /**
2445  * ice_copy_phy_caps_to_cfg - Copy PHY ability data to configuration data
2446  * @caps: PHY ability structure to copy date from
2447  * @cfg: PHY configuration structure to copy data to
2448  *
2449  * Helper function to copy AQC PHY get ability data to PHY set configuration
2450  * data structure
2451  */
2452 void
2453 ice_copy_phy_caps_to_cfg(struct ice_aqc_get_phy_caps_data *caps,
2454 			 struct ice_aqc_set_phy_cfg_data *cfg)
2455 {
2456 	if (!caps || !cfg)
2457 		return;
2458 
2459 	cfg->phy_type_low = caps->phy_type_low;
2460 	cfg->phy_type_high = caps->phy_type_high;
2461 	cfg->caps = caps->caps;
2462 	cfg->low_power_ctrl = caps->low_power_ctrl;
2463 	cfg->eee_cap = caps->eee_cap;
2464 	cfg->eeer_value = caps->eeer_value;
2465 	cfg->link_fec_opt = caps->link_fec_options;
2466 }
2467 
2468 /**
2469  * ice_cfg_phy_fec - Configure PHY FEC data based on FEC mode
2470  * @cfg: PHY configuration data to set FEC mode
2471  * @fec: FEC mode to configure
2472  *
2473  * Caller should copy ice_aqc_get_phy_caps_data.caps ICE_AQC_PHY_EN_AUTO_FEC
2474  * (bit 7) and ice_aqc_get_phy_caps_data.link_fec_options to cfg.caps
2475  * ICE_AQ_PHY_ENA_AUTO_FEC (bit 7) and cfg.link_fec_options before calling.
2476  */
2477 void
2478 ice_cfg_phy_fec(struct ice_aqc_set_phy_cfg_data *cfg, enum ice_fec_mode fec)
2479 {
2480 	switch (fec) {
2481 	case ICE_FEC_BASER:
2482 		/* Clear RS bits, and AND BASE-R ability
2483 		 * bits and OR request bits.
2484 		 */
2485 		cfg->link_fec_opt &= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN |
2486 				     ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN;
2487 		cfg->link_fec_opt |= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
2488 				     ICE_AQC_PHY_FEC_25G_KR_REQ;
2489 		break;
2490 	case ICE_FEC_RS:
2491 		/* Clear BASE-R bits, and AND RS ability
2492 		 * bits and OR request bits.
2493 		 */
2494 		cfg->link_fec_opt &= ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN;
2495 		cfg->link_fec_opt |= ICE_AQC_PHY_FEC_25G_RS_528_REQ |
2496 				     ICE_AQC_PHY_FEC_25G_RS_544_REQ;
2497 		break;
2498 	case ICE_FEC_NONE:
2499 		/* Clear all FEC option bits. */
2500 		cfg->link_fec_opt &= ~ICE_AQC_PHY_FEC_MASK;
2501 		break;
2502 	case ICE_FEC_AUTO:
2503 		/* AND auto FEC bit, and all caps bits. */
2504 		cfg->caps &= ICE_AQC_PHY_CAPS_MASK;
2505 		break;
2506 	}
2507 }
2508 
2509 /**
2510  * ice_get_link_status - get status of the HW network link
2511  * @pi: port information structure
2512  * @link_up: pointer to bool (true/false = linkup/linkdown)
2513  *
2514  * Variable link_up is true if link is up, false if link is down.
2515  * The variable link_up is invalid if status is non zero. As a
2516  * result of this call, link status reporting becomes enabled
2517  */
2518 enum ice_status ice_get_link_status(struct ice_port_info *pi, bool *link_up)
2519 {
2520 	struct ice_phy_info *phy_info;
2521 	enum ice_status status = 0;
2522 
2523 	if (!pi || !link_up)
2524 		return ICE_ERR_PARAM;
2525 
2526 	phy_info = &pi->phy;
2527 
2528 	if (phy_info->get_link_info) {
2529 		status = ice_update_link_info(pi);
2530 
2531 		if (status)
2532 			ice_debug(pi->hw, ICE_DBG_LINK,
2533 				  "get link status error, status = %d\n",
2534 				  status);
2535 	}
2536 
2537 	*link_up = phy_info->link_info.link_info & ICE_AQ_LINK_UP;
2538 
2539 	return status;
2540 }
2541 
2542 /**
2543  * ice_aq_set_link_restart_an
2544  * @pi: pointer to the port information structure
2545  * @ena_link: if true: enable link, if false: disable link
2546  * @cd: pointer to command details structure or NULL
2547  *
2548  * Sets up the link and restarts the Auto-Negotiation over the link.
2549  */
2550 enum ice_status
2551 ice_aq_set_link_restart_an(struct ice_port_info *pi, bool ena_link,
2552 			   struct ice_sq_cd *cd)
2553 {
2554 	struct ice_aqc_restart_an *cmd;
2555 	struct ice_aq_desc desc;
2556 
2557 	cmd = &desc.params.restart_an;
2558 
2559 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_restart_an);
2560 
2561 	cmd->cmd_flags = ICE_AQC_RESTART_AN_LINK_RESTART;
2562 	cmd->lport_num = pi->lport;
2563 	if (ena_link)
2564 		cmd->cmd_flags |= ICE_AQC_RESTART_AN_LINK_ENABLE;
2565 	else
2566 		cmd->cmd_flags &= ~ICE_AQC_RESTART_AN_LINK_ENABLE;
2567 
2568 	return ice_aq_send_cmd(pi->hw, &desc, NULL, 0, cd);
2569 }
2570 
2571 /**
2572  * ice_aq_set_event_mask
2573  * @hw: pointer to the HW struct
2574  * @port_num: port number of the physical function
2575  * @mask: event mask to be set
2576  * @cd: pointer to command details structure or NULL
2577  *
2578  * Set event mask (0x0613)
2579  */
2580 enum ice_status
2581 ice_aq_set_event_mask(struct ice_hw *hw, u8 port_num, u16 mask,
2582 		      struct ice_sq_cd *cd)
2583 {
2584 	struct ice_aqc_set_event_mask *cmd;
2585 	struct ice_aq_desc desc;
2586 
2587 	cmd = &desc.params.set_event_mask;
2588 
2589 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_event_mask);
2590 
2591 	cmd->lport_num = port_num;
2592 
2593 	cmd->event_mask = cpu_to_le16(mask);
2594 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2595 }
2596 
2597 /**
2598  * ice_aq_set_mac_loopback
2599  * @hw: pointer to the HW struct
2600  * @ena_lpbk: Enable or Disable loopback
2601  * @cd: pointer to command details structure or NULL
2602  *
2603  * Enable/disable loopback on a given port
2604  */
2605 enum ice_status
2606 ice_aq_set_mac_loopback(struct ice_hw *hw, bool ena_lpbk, struct ice_sq_cd *cd)
2607 {
2608 	struct ice_aqc_set_mac_lb *cmd;
2609 	struct ice_aq_desc desc;
2610 
2611 	cmd = &desc.params.set_mac_lb;
2612 
2613 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_mac_lb);
2614 	if (ena_lpbk)
2615 		cmd->lb_mode = ICE_AQ_MAC_LB_EN;
2616 
2617 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2618 }
2619 
2620 /**
2621  * ice_aq_set_port_id_led
2622  * @pi: pointer to the port information
2623  * @is_orig_mode: is this LED set to original mode (by the net-list)
2624  * @cd: pointer to command details structure or NULL
2625  *
2626  * Set LED value for the given port (0x06e9)
2627  */
2628 enum ice_status
2629 ice_aq_set_port_id_led(struct ice_port_info *pi, bool is_orig_mode,
2630 		       struct ice_sq_cd *cd)
2631 {
2632 	struct ice_aqc_set_port_id_led *cmd;
2633 	struct ice_hw *hw = pi->hw;
2634 	struct ice_aq_desc desc;
2635 
2636 	cmd = &desc.params.set_port_id_led;
2637 
2638 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_id_led);
2639 
2640 	if (is_orig_mode)
2641 		cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_ORIG;
2642 	else
2643 		cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_BLINK;
2644 
2645 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2646 }
2647 
2648 /**
2649  * ice_aq_sff_eeprom
2650  * @hw: pointer to the HW struct
2651  * @lport: bits [7:0] = logical port, bit [8] = logical port valid
2652  * @bus_addr: I2C bus address of the eeprom (typically 0xA0, 0=topo default)
2653  * @mem_addr: I2C offset. lower 8 bits for address, 8 upper bits zero padding.
2654  * @page: QSFP page
2655  * @set_page: set or ignore the page
2656  * @data: pointer to data buffer to be read/written to the I2C device.
2657  * @length: 1-16 for read, 1 for write.
2658  * @write: 0 read, 1 for write.
2659  * @cd: pointer to command details structure or NULL
2660  *
2661  * Read/Write SFF EEPROM (0x06EE)
2662  */
2663 enum ice_status
2664 ice_aq_sff_eeprom(struct ice_hw *hw, u16 lport, u8 bus_addr,
2665 		  u16 mem_addr, u8 page, u8 set_page, u8 *data, u8 length,
2666 		  bool write, struct ice_sq_cd *cd)
2667 {
2668 	struct ice_aqc_sff_eeprom *cmd;
2669 	struct ice_aq_desc desc;
2670 	enum ice_status status;
2671 
2672 	if (!data || (mem_addr & 0xff00))
2673 		return ICE_ERR_PARAM;
2674 
2675 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_sff_eeprom);
2676 	cmd = &desc.params.read_write_sff_param;
2677 	desc.flags = cpu_to_le16(ICE_AQ_FLAG_RD | ICE_AQ_FLAG_BUF);
2678 	cmd->lport_num = (u8)(lport & 0xff);
2679 	cmd->lport_num_valid = (u8)((lport >> 8) & 0x01);
2680 	cmd->i2c_bus_addr = cpu_to_le16(((bus_addr >> 1) &
2681 					 ICE_AQC_SFF_I2CBUS_7BIT_M) |
2682 					((set_page <<
2683 					  ICE_AQC_SFF_SET_EEPROM_PAGE_S) &
2684 					 ICE_AQC_SFF_SET_EEPROM_PAGE_M));
2685 	cmd->i2c_mem_addr = cpu_to_le16(mem_addr & 0xff);
2686 	cmd->eeprom_page = cpu_to_le16((u16)page << ICE_AQC_SFF_EEPROM_PAGE_S);
2687 	if (write)
2688 		cmd->i2c_bus_addr |= cpu_to_le16(ICE_AQC_SFF_IS_WRITE);
2689 
2690 	status = ice_aq_send_cmd(hw, &desc, data, length, cd);
2691 	return status;
2692 }
2693 
2694 /**
2695  * __ice_aq_get_set_rss_lut
2696  * @hw: pointer to the hardware structure
2697  * @vsi_id: VSI FW index
2698  * @lut_type: LUT table type
2699  * @lut: pointer to the LUT buffer provided by the caller
2700  * @lut_size: size of the LUT buffer
2701  * @glob_lut_idx: global LUT index
2702  * @set: set true to set the table, false to get the table
2703  *
2704  * Internal function to get (0x0B05) or set (0x0B03) RSS look up table
2705  */
2706 static enum ice_status
2707 __ice_aq_get_set_rss_lut(struct ice_hw *hw, u16 vsi_id, u8 lut_type, u8 *lut,
2708 			 u16 lut_size, u8 glob_lut_idx, bool set)
2709 {
2710 	struct ice_aqc_get_set_rss_lut *cmd_resp;
2711 	struct ice_aq_desc desc;
2712 	enum ice_status status;
2713 	u16 flags = 0;
2714 
2715 	cmd_resp = &desc.params.get_set_rss_lut;
2716 
2717 	if (set) {
2718 		ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_rss_lut);
2719 		desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2720 	} else {
2721 		ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_rss_lut);
2722 	}
2723 
2724 	cmd_resp->vsi_id = cpu_to_le16(((vsi_id <<
2725 					 ICE_AQC_GSET_RSS_LUT_VSI_ID_S) &
2726 					ICE_AQC_GSET_RSS_LUT_VSI_ID_M) |
2727 				       ICE_AQC_GSET_RSS_LUT_VSI_VALID);
2728 
2729 	switch (lut_type) {
2730 	case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI:
2731 	case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF:
2732 	case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_GLOBAL:
2733 		flags |= ((lut_type << ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_S) &
2734 			  ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_M);
2735 		break;
2736 	default:
2737 		status = ICE_ERR_PARAM;
2738 		goto ice_aq_get_set_rss_lut_exit;
2739 	}
2740 
2741 	if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_GLOBAL) {
2742 		flags |= ((glob_lut_idx << ICE_AQC_GSET_RSS_LUT_GLOBAL_IDX_S) &
2743 			  ICE_AQC_GSET_RSS_LUT_GLOBAL_IDX_M);
2744 
2745 		if (!set)
2746 			goto ice_aq_get_set_rss_lut_send;
2747 	} else if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF) {
2748 		if (!set)
2749 			goto ice_aq_get_set_rss_lut_send;
2750 	} else {
2751 		goto ice_aq_get_set_rss_lut_send;
2752 	}
2753 
2754 	/* LUT size is only valid for Global and PF table types */
2755 	switch (lut_size) {
2756 	case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_128:
2757 		break;
2758 	case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_512:
2759 		flags |= (ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_512_FLAG <<
2760 			  ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_S) &
2761 			 ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_M;
2762 		break;
2763 	case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_2K:
2764 		if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF) {
2765 			flags |= (ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_2K_FLAG <<
2766 				  ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_S) &
2767 				 ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_M;
2768 			break;
2769 		}
2770 		fallthrough;
2771 	default:
2772 		status = ICE_ERR_PARAM;
2773 		goto ice_aq_get_set_rss_lut_exit;
2774 	}
2775 
2776 ice_aq_get_set_rss_lut_send:
2777 	cmd_resp->flags = cpu_to_le16(flags);
2778 	status = ice_aq_send_cmd(hw, &desc, lut, lut_size, NULL);
2779 
2780 ice_aq_get_set_rss_lut_exit:
2781 	return status;
2782 }
2783 
2784 /**
2785  * ice_aq_get_rss_lut
2786  * @hw: pointer to the hardware structure
2787  * @vsi_handle: software VSI handle
2788  * @lut_type: LUT table type
2789  * @lut: pointer to the LUT buffer provided by the caller
2790  * @lut_size: size of the LUT buffer
2791  *
2792  * get the RSS lookup table, PF or VSI type
2793  */
2794 enum ice_status
2795 ice_aq_get_rss_lut(struct ice_hw *hw, u16 vsi_handle, u8 lut_type,
2796 		   u8 *lut, u16 lut_size)
2797 {
2798 	if (!ice_is_vsi_valid(hw, vsi_handle) || !lut)
2799 		return ICE_ERR_PARAM;
2800 
2801 	return __ice_aq_get_set_rss_lut(hw, ice_get_hw_vsi_num(hw, vsi_handle),
2802 					lut_type, lut, lut_size, 0, false);
2803 }
2804 
2805 /**
2806  * ice_aq_set_rss_lut
2807  * @hw: pointer to the hardware structure
2808  * @vsi_handle: software VSI handle
2809  * @lut_type: LUT table type
2810  * @lut: pointer to the LUT buffer provided by the caller
2811  * @lut_size: size of the LUT buffer
2812  *
2813  * set the RSS lookup table, PF or VSI type
2814  */
2815 enum ice_status
2816 ice_aq_set_rss_lut(struct ice_hw *hw, u16 vsi_handle, u8 lut_type,
2817 		   u8 *lut, u16 lut_size)
2818 {
2819 	if (!ice_is_vsi_valid(hw, vsi_handle) || !lut)
2820 		return ICE_ERR_PARAM;
2821 
2822 	return __ice_aq_get_set_rss_lut(hw, ice_get_hw_vsi_num(hw, vsi_handle),
2823 					lut_type, lut, lut_size, 0, true);
2824 }
2825 
2826 /**
2827  * __ice_aq_get_set_rss_key
2828  * @hw: pointer to the HW struct
2829  * @vsi_id: VSI FW index
2830  * @key: pointer to key info struct
2831  * @set: set true to set the key, false to get the key
2832  *
2833  * get (0x0B04) or set (0x0B02) the RSS key per VSI
2834  */
2835 static enum
2836 ice_status __ice_aq_get_set_rss_key(struct ice_hw *hw, u16 vsi_id,
2837 				    struct ice_aqc_get_set_rss_keys *key,
2838 				    bool set)
2839 {
2840 	struct ice_aqc_get_set_rss_key *cmd_resp;
2841 	u16 key_size = sizeof(*key);
2842 	struct ice_aq_desc desc;
2843 
2844 	cmd_resp = &desc.params.get_set_rss_key;
2845 
2846 	if (set) {
2847 		ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_rss_key);
2848 		desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2849 	} else {
2850 		ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_rss_key);
2851 	}
2852 
2853 	cmd_resp->vsi_id = cpu_to_le16(((vsi_id <<
2854 					 ICE_AQC_GSET_RSS_KEY_VSI_ID_S) &
2855 					ICE_AQC_GSET_RSS_KEY_VSI_ID_M) |
2856 				       ICE_AQC_GSET_RSS_KEY_VSI_VALID);
2857 
2858 	return ice_aq_send_cmd(hw, &desc, key, key_size, NULL);
2859 }
2860 
2861 /**
2862  * ice_aq_get_rss_key
2863  * @hw: pointer to the HW struct
2864  * @vsi_handle: software VSI handle
2865  * @key: pointer to key info struct
2866  *
2867  * get the RSS key per VSI
2868  */
2869 enum ice_status
2870 ice_aq_get_rss_key(struct ice_hw *hw, u16 vsi_handle,
2871 		   struct ice_aqc_get_set_rss_keys *key)
2872 {
2873 	if (!ice_is_vsi_valid(hw, vsi_handle) || !key)
2874 		return ICE_ERR_PARAM;
2875 
2876 	return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
2877 					key, false);
2878 }
2879 
2880 /**
2881  * ice_aq_set_rss_key
2882  * @hw: pointer to the HW struct
2883  * @vsi_handle: software VSI handle
2884  * @keys: pointer to key info struct
2885  *
2886  * set the RSS key per VSI
2887  */
2888 enum ice_status
2889 ice_aq_set_rss_key(struct ice_hw *hw, u16 vsi_handle,
2890 		   struct ice_aqc_get_set_rss_keys *keys)
2891 {
2892 	if (!ice_is_vsi_valid(hw, vsi_handle) || !keys)
2893 		return ICE_ERR_PARAM;
2894 
2895 	return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
2896 					keys, true);
2897 }
2898 
2899 /**
2900  * ice_aq_add_lan_txq
2901  * @hw: pointer to the hardware structure
2902  * @num_qgrps: Number of added queue groups
2903  * @qg_list: list of queue groups to be added
2904  * @buf_size: size of buffer for indirect command
2905  * @cd: pointer to command details structure or NULL
2906  *
2907  * Add Tx LAN queue (0x0C30)
2908  *
2909  * NOTE:
2910  * Prior to calling add Tx LAN queue:
2911  * Initialize the following as part of the Tx queue context:
2912  * Completion queue ID if the queue uses Completion queue, Quanta profile,
2913  * Cache profile and Packet shaper profile.
2914  *
2915  * After add Tx LAN queue AQ command is completed:
2916  * Interrupts should be associated with specific queues,
2917  * Association of Tx queue to Doorbell queue is not part of Add LAN Tx queue
2918  * flow.
2919  */
2920 static enum ice_status
2921 ice_aq_add_lan_txq(struct ice_hw *hw, u8 num_qgrps,
2922 		   struct ice_aqc_add_tx_qgrp *qg_list, u16 buf_size,
2923 		   struct ice_sq_cd *cd)
2924 {
2925 	struct ice_aqc_add_tx_qgrp *list;
2926 	struct ice_aqc_add_txqs *cmd;
2927 	struct ice_aq_desc desc;
2928 	u16 i, sum_size = 0;
2929 
2930 	cmd = &desc.params.add_txqs;
2931 
2932 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_txqs);
2933 
2934 	if (!qg_list)
2935 		return ICE_ERR_PARAM;
2936 
2937 	if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
2938 		return ICE_ERR_PARAM;
2939 
2940 	for (i = 0, list = qg_list; i < num_qgrps; i++) {
2941 		sum_size += struct_size(list, txqs, list->num_txqs);
2942 		list = (struct ice_aqc_add_tx_qgrp *)(list->txqs +
2943 						      list->num_txqs);
2944 	}
2945 
2946 	if (buf_size != sum_size)
2947 		return ICE_ERR_PARAM;
2948 
2949 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2950 
2951 	cmd->num_qgrps = num_qgrps;
2952 
2953 	return ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
2954 }
2955 
2956 /**
2957  * ice_aq_dis_lan_txq
2958  * @hw: pointer to the hardware structure
2959  * @num_qgrps: number of groups in the list
2960  * @qg_list: the list of groups to disable
2961  * @buf_size: the total size of the qg_list buffer in bytes
2962  * @rst_src: if called due to reset, specifies the reset source
2963  * @vmvf_num: the relative VM or VF number that is undergoing the reset
2964  * @cd: pointer to command details structure or NULL
2965  *
2966  * Disable LAN Tx queue (0x0C31)
2967  */
2968 static enum ice_status
2969 ice_aq_dis_lan_txq(struct ice_hw *hw, u8 num_qgrps,
2970 		   struct ice_aqc_dis_txq_item *qg_list, u16 buf_size,
2971 		   enum ice_disq_rst_src rst_src, u16 vmvf_num,
2972 		   struct ice_sq_cd *cd)
2973 {
2974 	struct ice_aqc_dis_txq_item *item;
2975 	struct ice_aqc_dis_txqs *cmd;
2976 	struct ice_aq_desc desc;
2977 	enum ice_status status;
2978 	u16 i, sz = 0;
2979 
2980 	cmd = &desc.params.dis_txqs;
2981 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_dis_txqs);
2982 
2983 	/* qg_list can be NULL only in VM/VF reset flow */
2984 	if (!qg_list && !rst_src)
2985 		return ICE_ERR_PARAM;
2986 
2987 	if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
2988 		return ICE_ERR_PARAM;
2989 
2990 	cmd->num_entries = num_qgrps;
2991 
2992 	cmd->vmvf_and_timeout = cpu_to_le16((5 << ICE_AQC_Q_DIS_TIMEOUT_S) &
2993 					    ICE_AQC_Q_DIS_TIMEOUT_M);
2994 
2995 	switch (rst_src) {
2996 	case ICE_VM_RESET:
2997 		cmd->cmd_type = ICE_AQC_Q_DIS_CMD_VM_RESET;
2998 		cmd->vmvf_and_timeout |=
2999 			cpu_to_le16(vmvf_num & ICE_AQC_Q_DIS_VMVF_NUM_M);
3000 		break;
3001 	case ICE_VF_RESET:
3002 		cmd->cmd_type = ICE_AQC_Q_DIS_CMD_VF_RESET;
3003 		/* In this case, FW expects vmvf_num to be absolute VF ID */
3004 		cmd->vmvf_and_timeout |=
3005 			cpu_to_le16((vmvf_num + hw->func_caps.vf_base_id) &
3006 				    ICE_AQC_Q_DIS_VMVF_NUM_M);
3007 		break;
3008 	case ICE_NO_RESET:
3009 	default:
3010 		break;
3011 	}
3012 
3013 	/* flush pipe on time out */
3014 	cmd->cmd_type |= ICE_AQC_Q_DIS_CMD_FLUSH_PIPE;
3015 	/* If no queue group info, we are in a reset flow. Issue the AQ */
3016 	if (!qg_list)
3017 		goto do_aq;
3018 
3019 	/* set RD bit to indicate that command buffer is provided by the driver
3020 	 * and it needs to be read by the firmware
3021 	 */
3022 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
3023 
3024 	for (i = 0, item = qg_list; i < num_qgrps; i++) {
3025 		u16 item_size = struct_size(item, q_id, item->num_qs);
3026 
3027 		/* If the num of queues is even, add 2 bytes of padding */
3028 		if ((item->num_qs % 2) == 0)
3029 			item_size += 2;
3030 
3031 		sz += item_size;
3032 
3033 		item = (struct ice_aqc_dis_txq_item *)((u8 *)item + item_size);
3034 	}
3035 
3036 	if (buf_size != sz)
3037 		return ICE_ERR_PARAM;
3038 
3039 do_aq:
3040 	status = ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
3041 	if (status) {
3042 		if (!qg_list)
3043 			ice_debug(hw, ICE_DBG_SCHED, "VM%d disable failed %d\n",
3044 				  vmvf_num, hw->adminq.sq_last_status);
3045 		else
3046 			ice_debug(hw, ICE_DBG_SCHED, "disable queue %d failed %d\n",
3047 				  le16_to_cpu(qg_list[0].q_id[0]),
3048 				  hw->adminq.sq_last_status);
3049 	}
3050 	return status;
3051 }
3052 
3053 /* End of FW Admin Queue command wrappers */
3054 
3055 /**
3056  * ice_write_byte - write a byte to a packed context structure
3057  * @src_ctx:  the context structure to read from
3058  * @dest_ctx: the context to be written to
3059  * @ce_info:  a description of the struct to be filled
3060  */
3061 static void
3062 ice_write_byte(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3063 {
3064 	u8 src_byte, dest_byte, mask;
3065 	u8 *from, *dest;
3066 	u16 shift_width;
3067 
3068 	/* copy from the next struct field */
3069 	from = src_ctx + ce_info->offset;
3070 
3071 	/* prepare the bits and mask */
3072 	shift_width = ce_info->lsb % 8;
3073 	mask = (u8)(BIT(ce_info->width) - 1);
3074 
3075 	src_byte = *from;
3076 	src_byte &= mask;
3077 
3078 	/* shift to correct alignment */
3079 	mask <<= shift_width;
3080 	src_byte <<= shift_width;
3081 
3082 	/* get the current bits from the target bit string */
3083 	dest = dest_ctx + (ce_info->lsb / 8);
3084 
3085 	memcpy(&dest_byte, dest, sizeof(dest_byte));
3086 
3087 	dest_byte &= ~mask;	/* get the bits not changing */
3088 	dest_byte |= src_byte;	/* add in the new bits */
3089 
3090 	/* put it all back */
3091 	memcpy(dest, &dest_byte, sizeof(dest_byte));
3092 }
3093 
3094 /**
3095  * ice_write_word - write a word to a packed context structure
3096  * @src_ctx:  the context structure to read from
3097  * @dest_ctx: the context to be written to
3098  * @ce_info:  a description of the struct to be filled
3099  */
3100 static void
3101 ice_write_word(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3102 {
3103 	u16 src_word, mask;
3104 	__le16 dest_word;
3105 	u8 *from, *dest;
3106 	u16 shift_width;
3107 
3108 	/* copy from the next struct field */
3109 	from = src_ctx + ce_info->offset;
3110 
3111 	/* prepare the bits and mask */
3112 	shift_width = ce_info->lsb % 8;
3113 	mask = BIT(ce_info->width) - 1;
3114 
3115 	/* don't swizzle the bits until after the mask because the mask bits
3116 	 * will be in a different bit position on big endian machines
3117 	 */
3118 	src_word = *(u16 *)from;
3119 	src_word &= mask;
3120 
3121 	/* shift to correct alignment */
3122 	mask <<= shift_width;
3123 	src_word <<= shift_width;
3124 
3125 	/* get the current bits from the target bit string */
3126 	dest = dest_ctx + (ce_info->lsb / 8);
3127 
3128 	memcpy(&dest_word, dest, sizeof(dest_word));
3129 
3130 	dest_word &= ~(cpu_to_le16(mask));	/* get the bits not changing */
3131 	dest_word |= cpu_to_le16(src_word);	/* add in the new bits */
3132 
3133 	/* put it all back */
3134 	memcpy(dest, &dest_word, sizeof(dest_word));
3135 }
3136 
3137 /**
3138  * ice_write_dword - write a dword to a packed context structure
3139  * @src_ctx:  the context structure to read from
3140  * @dest_ctx: the context to be written to
3141  * @ce_info:  a description of the struct to be filled
3142  */
3143 static void
3144 ice_write_dword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3145 {
3146 	u32 src_dword, mask;
3147 	__le32 dest_dword;
3148 	u8 *from, *dest;
3149 	u16 shift_width;
3150 
3151 	/* copy from the next struct field */
3152 	from = src_ctx + ce_info->offset;
3153 
3154 	/* prepare the bits and mask */
3155 	shift_width = ce_info->lsb % 8;
3156 
3157 	/* if the field width is exactly 32 on an x86 machine, then the shift
3158 	 * operation will not work because the SHL instructions count is masked
3159 	 * to 5 bits so the shift will do nothing
3160 	 */
3161 	if (ce_info->width < 32)
3162 		mask = BIT(ce_info->width) - 1;
3163 	else
3164 		mask = (u32)~0;
3165 
3166 	/* don't swizzle the bits until after the mask because the mask bits
3167 	 * will be in a different bit position on big endian machines
3168 	 */
3169 	src_dword = *(u32 *)from;
3170 	src_dword &= mask;
3171 
3172 	/* shift to correct alignment */
3173 	mask <<= shift_width;
3174 	src_dword <<= shift_width;
3175 
3176 	/* get the current bits from the target bit string */
3177 	dest = dest_ctx + (ce_info->lsb / 8);
3178 
3179 	memcpy(&dest_dword, dest, sizeof(dest_dword));
3180 
3181 	dest_dword &= ~(cpu_to_le32(mask));	/* get the bits not changing */
3182 	dest_dword |= cpu_to_le32(src_dword);	/* add in the new bits */
3183 
3184 	/* put it all back */
3185 	memcpy(dest, &dest_dword, sizeof(dest_dword));
3186 }
3187 
3188 /**
3189  * ice_write_qword - write a qword to a packed context structure
3190  * @src_ctx:  the context structure to read from
3191  * @dest_ctx: the context to be written to
3192  * @ce_info:  a description of the struct to be filled
3193  */
3194 static void
3195 ice_write_qword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3196 {
3197 	u64 src_qword, mask;
3198 	__le64 dest_qword;
3199 	u8 *from, *dest;
3200 	u16 shift_width;
3201 
3202 	/* copy from the next struct field */
3203 	from = src_ctx + ce_info->offset;
3204 
3205 	/* prepare the bits and mask */
3206 	shift_width = ce_info->lsb % 8;
3207 
3208 	/* if the field width is exactly 64 on an x86 machine, then the shift
3209 	 * operation will not work because the SHL instructions count is masked
3210 	 * to 6 bits so the shift will do nothing
3211 	 */
3212 	if (ce_info->width < 64)
3213 		mask = BIT_ULL(ce_info->width) - 1;
3214 	else
3215 		mask = (u64)~0;
3216 
3217 	/* don't swizzle the bits until after the mask because the mask bits
3218 	 * will be in a different bit position on big endian machines
3219 	 */
3220 	src_qword = *(u64 *)from;
3221 	src_qword &= mask;
3222 
3223 	/* shift to correct alignment */
3224 	mask <<= shift_width;
3225 	src_qword <<= shift_width;
3226 
3227 	/* get the current bits from the target bit string */
3228 	dest = dest_ctx + (ce_info->lsb / 8);
3229 
3230 	memcpy(&dest_qword, dest, sizeof(dest_qword));
3231 
3232 	dest_qword &= ~(cpu_to_le64(mask));	/* get the bits not changing */
3233 	dest_qword |= cpu_to_le64(src_qword);	/* add in the new bits */
3234 
3235 	/* put it all back */
3236 	memcpy(dest, &dest_qword, sizeof(dest_qword));
3237 }
3238 
3239 /**
3240  * ice_set_ctx - set context bits in packed structure
3241  * @hw: pointer to the hardware structure
3242  * @src_ctx:  pointer to a generic non-packed context structure
3243  * @dest_ctx: pointer to memory for the packed structure
3244  * @ce_info:  a description of the structure to be transformed
3245  */
3246 enum ice_status
3247 ice_set_ctx(struct ice_hw *hw, u8 *src_ctx, u8 *dest_ctx,
3248 	    const struct ice_ctx_ele *ce_info)
3249 {
3250 	int f;
3251 
3252 	for (f = 0; ce_info[f].width; f++) {
3253 		/* We have to deal with each element of the FW response
3254 		 * using the correct size so that we are correct regardless
3255 		 * of the endianness of the machine.
3256 		 */
3257 		if (ce_info[f].width > (ce_info[f].size_of * BITS_PER_BYTE)) {
3258 			ice_debug(hw, ICE_DBG_QCTX,
3259 				  "Field %d width of %d bits larger than size of %d byte(s) ... skipping write\n",
3260 				  f, ce_info[f].width, ce_info[f].size_of);
3261 			continue;
3262 		}
3263 		switch (ce_info[f].size_of) {
3264 		case sizeof(u8):
3265 			ice_write_byte(src_ctx, dest_ctx, &ce_info[f]);
3266 			break;
3267 		case sizeof(u16):
3268 			ice_write_word(src_ctx, dest_ctx, &ce_info[f]);
3269 			break;
3270 		case sizeof(u32):
3271 			ice_write_dword(src_ctx, dest_ctx, &ce_info[f]);
3272 			break;
3273 		case sizeof(u64):
3274 			ice_write_qword(src_ctx, dest_ctx, &ce_info[f]);
3275 			break;
3276 		default:
3277 			return ICE_ERR_INVAL_SIZE;
3278 		}
3279 	}
3280 
3281 	return 0;
3282 }
3283 
3284 /**
3285  * ice_get_lan_q_ctx - get the LAN queue context for the given VSI and TC
3286  * @hw: pointer to the HW struct
3287  * @vsi_handle: software VSI handle
3288  * @tc: TC number
3289  * @q_handle: software queue handle
3290  */
3291 struct ice_q_ctx *
3292 ice_get_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 q_handle)
3293 {
3294 	struct ice_vsi_ctx *vsi;
3295 	struct ice_q_ctx *q_ctx;
3296 
3297 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
3298 	if (!vsi)
3299 		return NULL;
3300 	if (q_handle >= vsi->num_lan_q_entries[tc])
3301 		return NULL;
3302 	if (!vsi->lan_q_ctx[tc])
3303 		return NULL;
3304 	q_ctx = vsi->lan_q_ctx[tc];
3305 	return &q_ctx[q_handle];
3306 }
3307 
3308 /**
3309  * ice_ena_vsi_txq
3310  * @pi: port information structure
3311  * @vsi_handle: software VSI handle
3312  * @tc: TC number
3313  * @q_handle: software queue handle
3314  * @num_qgrps: Number of added queue groups
3315  * @buf: list of queue groups to be added
3316  * @buf_size: size of buffer for indirect command
3317  * @cd: pointer to command details structure or NULL
3318  *
3319  * This function adds one LAN queue
3320  */
3321 enum ice_status
3322 ice_ena_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 q_handle,
3323 		u8 num_qgrps, struct ice_aqc_add_tx_qgrp *buf, u16 buf_size,
3324 		struct ice_sq_cd *cd)
3325 {
3326 	struct ice_aqc_txsched_elem_data node = { 0 };
3327 	struct ice_sched_node *parent;
3328 	struct ice_q_ctx *q_ctx;
3329 	enum ice_status status;
3330 	struct ice_hw *hw;
3331 
3332 	if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
3333 		return ICE_ERR_CFG;
3334 
3335 	if (num_qgrps > 1 || buf->num_txqs > 1)
3336 		return ICE_ERR_MAX_LIMIT;
3337 
3338 	hw = pi->hw;
3339 
3340 	if (!ice_is_vsi_valid(hw, vsi_handle))
3341 		return ICE_ERR_PARAM;
3342 
3343 	mutex_lock(&pi->sched_lock);
3344 
3345 	q_ctx = ice_get_lan_q_ctx(hw, vsi_handle, tc, q_handle);
3346 	if (!q_ctx) {
3347 		ice_debug(hw, ICE_DBG_SCHED, "Enaq: invalid queue handle %d\n",
3348 			  q_handle);
3349 		status = ICE_ERR_PARAM;
3350 		goto ena_txq_exit;
3351 	}
3352 
3353 	/* find a parent node */
3354 	parent = ice_sched_get_free_qparent(pi, vsi_handle, tc,
3355 					    ICE_SCHED_NODE_OWNER_LAN);
3356 	if (!parent) {
3357 		status = ICE_ERR_PARAM;
3358 		goto ena_txq_exit;
3359 	}
3360 
3361 	buf->parent_teid = parent->info.node_teid;
3362 	node.parent_teid = parent->info.node_teid;
3363 	/* Mark that the values in the "generic" section as valid. The default
3364 	 * value in the "generic" section is zero. This means that :
3365 	 * - Scheduling mode is Bytes Per Second (BPS), indicated by Bit 0.
3366 	 * - 0 priority among siblings, indicated by Bit 1-3.
3367 	 * - WFQ, indicated by Bit 4.
3368 	 * - 0 Adjustment value is used in PSM credit update flow, indicated by
3369 	 * Bit 5-6.
3370 	 * - Bit 7 is reserved.
3371 	 * Without setting the generic section as valid in valid_sections, the
3372 	 * Admin queue command will fail with error code ICE_AQ_RC_EINVAL.
3373 	 */
3374 	buf->txqs[0].info.valid_sections = ICE_AQC_ELEM_VALID_GENERIC;
3375 
3376 	/* add the LAN queue */
3377 	status = ice_aq_add_lan_txq(hw, num_qgrps, buf, buf_size, cd);
3378 	if (status) {
3379 		ice_debug(hw, ICE_DBG_SCHED, "enable queue %d failed %d\n",
3380 			  le16_to_cpu(buf->txqs[0].txq_id),
3381 			  hw->adminq.sq_last_status);
3382 		goto ena_txq_exit;
3383 	}
3384 
3385 	node.node_teid = buf->txqs[0].q_teid;
3386 	node.data.elem_type = ICE_AQC_ELEM_TYPE_LEAF;
3387 	q_ctx->q_handle = q_handle;
3388 	q_ctx->q_teid = le32_to_cpu(node.node_teid);
3389 
3390 	/* add a leaf node into scheduler tree queue layer */
3391 	status = ice_sched_add_node(pi, hw->num_tx_sched_layers - 1, &node);
3392 	if (!status)
3393 		status = ice_sched_replay_q_bw(pi, q_ctx);
3394 
3395 ena_txq_exit:
3396 	mutex_unlock(&pi->sched_lock);
3397 	return status;
3398 }
3399 
3400 /**
3401  * ice_dis_vsi_txq
3402  * @pi: port information structure
3403  * @vsi_handle: software VSI handle
3404  * @tc: TC number
3405  * @num_queues: number of queues
3406  * @q_handles: pointer to software queue handle array
3407  * @q_ids: pointer to the q_id array
3408  * @q_teids: pointer to queue node teids
3409  * @rst_src: if called due to reset, specifies the reset source
3410  * @vmvf_num: the relative VM or VF number that is undergoing the reset
3411  * @cd: pointer to command details structure or NULL
3412  *
3413  * This function removes queues and their corresponding nodes in SW DB
3414  */
3415 enum ice_status
3416 ice_dis_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u8 num_queues,
3417 		u16 *q_handles, u16 *q_ids, u32 *q_teids,
3418 		enum ice_disq_rst_src rst_src, u16 vmvf_num,
3419 		struct ice_sq_cd *cd)
3420 {
3421 	enum ice_status status = ICE_ERR_DOES_NOT_EXIST;
3422 	struct ice_aqc_dis_txq_item *qg_list;
3423 	struct ice_q_ctx *q_ctx;
3424 	struct ice_hw *hw;
3425 	u16 i, buf_size;
3426 
3427 	if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
3428 		return ICE_ERR_CFG;
3429 
3430 	hw = pi->hw;
3431 
3432 	if (!num_queues) {
3433 		/* if queue is disabled already yet the disable queue command
3434 		 * has to be sent to complete the VF reset, then call
3435 		 * ice_aq_dis_lan_txq without any queue information
3436 		 */
3437 		if (rst_src)
3438 			return ice_aq_dis_lan_txq(hw, 0, NULL, 0, rst_src,
3439 						  vmvf_num, NULL);
3440 		return ICE_ERR_CFG;
3441 	}
3442 
3443 	buf_size = struct_size(qg_list, q_id, 1);
3444 	qg_list = kzalloc(buf_size, GFP_KERNEL);
3445 	if (!qg_list)
3446 		return ICE_ERR_NO_MEMORY;
3447 
3448 	mutex_lock(&pi->sched_lock);
3449 
3450 	for (i = 0; i < num_queues; i++) {
3451 		struct ice_sched_node *node;
3452 
3453 		node = ice_sched_find_node_by_teid(pi->root, q_teids[i]);
3454 		if (!node)
3455 			continue;
3456 		q_ctx = ice_get_lan_q_ctx(hw, vsi_handle, tc, q_handles[i]);
3457 		if (!q_ctx) {
3458 			ice_debug(hw, ICE_DBG_SCHED, "invalid queue handle%d\n",
3459 				  q_handles[i]);
3460 			continue;
3461 		}
3462 		if (q_ctx->q_handle != q_handles[i]) {
3463 			ice_debug(hw, ICE_DBG_SCHED, "Err:handles %d %d\n",
3464 				  q_ctx->q_handle, q_handles[i]);
3465 			continue;
3466 		}
3467 		qg_list->parent_teid = node->info.parent_teid;
3468 		qg_list->num_qs = 1;
3469 		qg_list->q_id[0] = cpu_to_le16(q_ids[i]);
3470 		status = ice_aq_dis_lan_txq(hw, 1, qg_list, buf_size, rst_src,
3471 					    vmvf_num, cd);
3472 
3473 		if (status)
3474 			break;
3475 		ice_free_sched_node(pi, node);
3476 		q_ctx->q_handle = ICE_INVAL_Q_HANDLE;
3477 	}
3478 	mutex_unlock(&pi->sched_lock);
3479 	kfree(qg_list);
3480 	return status;
3481 }
3482 
3483 /**
3484  * ice_cfg_vsi_qs - configure the new/existing VSI queues
3485  * @pi: port information structure
3486  * @vsi_handle: software VSI handle
3487  * @tc_bitmap: TC bitmap
3488  * @maxqs: max queues array per TC
3489  * @owner: LAN or RDMA
3490  *
3491  * This function adds/updates the VSI queues per TC.
3492  */
3493 static enum ice_status
3494 ice_cfg_vsi_qs(struct ice_port_info *pi, u16 vsi_handle, u8 tc_bitmap,
3495 	       u16 *maxqs, u8 owner)
3496 {
3497 	enum ice_status status = 0;
3498 	u8 i;
3499 
3500 	if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
3501 		return ICE_ERR_CFG;
3502 
3503 	if (!ice_is_vsi_valid(pi->hw, vsi_handle))
3504 		return ICE_ERR_PARAM;
3505 
3506 	mutex_lock(&pi->sched_lock);
3507 
3508 	ice_for_each_traffic_class(i) {
3509 		/* configuration is possible only if TC node is present */
3510 		if (!ice_sched_get_tc_node(pi, i))
3511 			continue;
3512 
3513 		status = ice_sched_cfg_vsi(pi, vsi_handle, i, maxqs[i], owner,
3514 					   ice_is_tc_ena(tc_bitmap, i));
3515 		if (status)
3516 			break;
3517 	}
3518 
3519 	mutex_unlock(&pi->sched_lock);
3520 	return status;
3521 }
3522 
3523 /**
3524  * ice_cfg_vsi_lan - configure VSI LAN queues
3525  * @pi: port information structure
3526  * @vsi_handle: software VSI handle
3527  * @tc_bitmap: TC bitmap
3528  * @max_lanqs: max LAN queues array per TC
3529  *
3530  * This function adds/updates the VSI LAN queues per TC.
3531  */
3532 enum ice_status
3533 ice_cfg_vsi_lan(struct ice_port_info *pi, u16 vsi_handle, u8 tc_bitmap,
3534 		u16 *max_lanqs)
3535 {
3536 	return ice_cfg_vsi_qs(pi, vsi_handle, tc_bitmap, max_lanqs,
3537 			      ICE_SCHED_NODE_OWNER_LAN);
3538 }
3539 
3540 /**
3541  * ice_replay_pre_init - replay pre initialization
3542  * @hw: pointer to the HW struct
3543  *
3544  * Initializes required config data for VSI, FD, ACL, and RSS before replay.
3545  */
3546 static enum ice_status ice_replay_pre_init(struct ice_hw *hw)
3547 {
3548 	struct ice_switch_info *sw = hw->switch_info;
3549 	u8 i;
3550 
3551 	/* Delete old entries from replay filter list head if there is any */
3552 	ice_rm_all_sw_replay_rule_info(hw);
3553 	/* In start of replay, move entries into replay_rules list, it
3554 	 * will allow adding rules entries back to filt_rules list,
3555 	 * which is operational list.
3556 	 */
3557 	for (i = 0; i < ICE_SW_LKUP_LAST; i++)
3558 		list_replace_init(&sw->recp_list[i].filt_rules,
3559 				  &sw->recp_list[i].filt_replay_rules);
3560 
3561 	return 0;
3562 }
3563 
3564 /**
3565  * ice_replay_vsi - replay VSI configuration
3566  * @hw: pointer to the HW struct
3567  * @vsi_handle: driver VSI handle
3568  *
3569  * Restore all VSI configuration after reset. It is required to call this
3570  * function with main VSI first.
3571  */
3572 enum ice_status ice_replay_vsi(struct ice_hw *hw, u16 vsi_handle)
3573 {
3574 	enum ice_status status;
3575 
3576 	if (!ice_is_vsi_valid(hw, vsi_handle))
3577 		return ICE_ERR_PARAM;
3578 
3579 	/* Replay pre-initialization if there is any */
3580 	if (vsi_handle == ICE_MAIN_VSI_HANDLE) {
3581 		status = ice_replay_pre_init(hw);
3582 		if (status)
3583 			return status;
3584 	}
3585 	/* Replay per VSI all RSS configurations */
3586 	status = ice_replay_rss_cfg(hw, vsi_handle);
3587 	if (status)
3588 		return status;
3589 	/* Replay per VSI all filters */
3590 	status = ice_replay_vsi_all_fltr(hw, vsi_handle);
3591 	return status;
3592 }
3593 
3594 /**
3595  * ice_replay_post - post replay configuration cleanup
3596  * @hw: pointer to the HW struct
3597  *
3598  * Post replay cleanup.
3599  */
3600 void ice_replay_post(struct ice_hw *hw)
3601 {
3602 	/* Delete old entries from replay filter list head */
3603 	ice_rm_all_sw_replay_rule_info(hw);
3604 }
3605 
3606 /**
3607  * ice_stat_update40 - read 40 bit stat from the chip and update stat values
3608  * @hw: ptr to the hardware info
3609  * @reg: offset of 64 bit HW register to read from
3610  * @prev_stat_loaded: bool to specify if previous stats are loaded
3611  * @prev_stat: ptr to previous loaded stat value
3612  * @cur_stat: ptr to current stat value
3613  */
3614 void
3615 ice_stat_update40(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
3616 		  u64 *prev_stat, u64 *cur_stat)
3617 {
3618 	u64 new_data = rd64(hw, reg) & (BIT_ULL(40) - 1);
3619 
3620 	/* device stats are not reset at PFR, they likely will not be zeroed
3621 	 * when the driver starts. Thus, save the value from the first read
3622 	 * without adding to the statistic value so that we report stats which
3623 	 * count up from zero.
3624 	 */
3625 	if (!prev_stat_loaded) {
3626 		*prev_stat = new_data;
3627 		return;
3628 	}
3629 
3630 	/* Calculate the difference between the new and old values, and then
3631 	 * add it to the software stat value.
3632 	 */
3633 	if (new_data >= *prev_stat)
3634 		*cur_stat += new_data - *prev_stat;
3635 	else
3636 		/* to manage the potential roll-over */
3637 		*cur_stat += (new_data + BIT_ULL(40)) - *prev_stat;
3638 
3639 	/* Update the previously stored value to prepare for next read */
3640 	*prev_stat = new_data;
3641 }
3642 
3643 /**
3644  * ice_stat_update32 - read 32 bit stat from the chip and update stat values
3645  * @hw: ptr to the hardware info
3646  * @reg: offset of HW register to read from
3647  * @prev_stat_loaded: bool to specify if previous stats are loaded
3648  * @prev_stat: ptr to previous loaded stat value
3649  * @cur_stat: ptr to current stat value
3650  */
3651 void
3652 ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
3653 		  u64 *prev_stat, u64 *cur_stat)
3654 {
3655 	u32 new_data;
3656 
3657 	new_data = rd32(hw, reg);
3658 
3659 	/* device stats are not reset at PFR, they likely will not be zeroed
3660 	 * when the driver starts. Thus, save the value from the first read
3661 	 * without adding to the statistic value so that we report stats which
3662 	 * count up from zero.
3663 	 */
3664 	if (!prev_stat_loaded) {
3665 		*prev_stat = new_data;
3666 		return;
3667 	}
3668 
3669 	/* Calculate the difference between the new and old values, and then
3670 	 * add it to the software stat value.
3671 	 */
3672 	if (new_data >= *prev_stat)
3673 		*cur_stat += new_data - *prev_stat;
3674 	else
3675 		/* to manage the potential roll-over */
3676 		*cur_stat += (new_data + BIT_ULL(32)) - *prev_stat;
3677 
3678 	/* Update the previously stored value to prepare for next read */
3679 	*prev_stat = new_data;
3680 }
3681 
3682 /**
3683  * ice_sched_query_elem - query element information from HW
3684  * @hw: pointer to the HW struct
3685  * @node_teid: node TEID to be queried
3686  * @buf: buffer to element information
3687  *
3688  * This function queries HW element information
3689  */
3690 enum ice_status
3691 ice_sched_query_elem(struct ice_hw *hw, u32 node_teid,
3692 		     struct ice_aqc_txsched_elem_data *buf)
3693 {
3694 	u16 buf_size, num_elem_ret = 0;
3695 	enum ice_status status;
3696 
3697 	buf_size = sizeof(*buf);
3698 	memset(buf, 0, buf_size);
3699 	buf->node_teid = cpu_to_le32(node_teid);
3700 	status = ice_aq_query_sched_elems(hw, 1, buf, buf_size, &num_elem_ret,
3701 					  NULL);
3702 	if (status || num_elem_ret != 1)
3703 		ice_debug(hw, ICE_DBG_SCHED, "query element failed\n");
3704 	return status;
3705 }
3706