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