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