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