net/ice/base: avoid undefined behavior
[dpdk.git] / drivers / net / ice / base / ice_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4
5 #include "ice_common.h"
6 #include "ice_sched.h"
7 #include "ice_adminq_cmd.h"
8
9 #include "ice_flow.h"
10 #include "ice_switch.h"
11
12 #define ICE_PF_RESET_WAIT_COUNT 300
13
14 /**
15  * ice_set_mac_type - Sets MAC type
16  * @hw: pointer to the HW structure
17  *
18  * This function sets the MAC type of the adapter based on the
19  * vendor ID and device ID stored in the HW structure.
20  */
21 static enum ice_status ice_set_mac_type(struct ice_hw *hw)
22 {
23         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
24
25         if (hw->vendor_id != ICE_INTEL_VENDOR_ID)
26                 return ICE_ERR_DEVICE_NOT_SUPPORTED;
27
28         switch (hw->device_id) {
29         case ICE_DEV_ID_E810C_BACKPLANE:
30         case ICE_DEV_ID_E810C_QSFP:
31         case ICE_DEV_ID_E810C_SFP:
32         case ICE_DEV_ID_E810_XXV_BACKPLANE:
33         case ICE_DEV_ID_E810_XXV_QSFP:
34         case ICE_DEV_ID_E810_XXV_SFP:
35                 hw->mac_type = ICE_MAC_E810;
36                 break;
37         case ICE_DEV_ID_E822C_10G_BASE_T:
38         case ICE_DEV_ID_E822C_BACKPLANE:
39         case ICE_DEV_ID_E822C_QSFP:
40         case ICE_DEV_ID_E822C_SFP:
41         case ICE_DEV_ID_E822C_SGMII:
42         case ICE_DEV_ID_E822L_10G_BASE_T:
43         case ICE_DEV_ID_E822L_BACKPLANE:
44         case ICE_DEV_ID_E822L_SFP:
45         case ICE_DEV_ID_E822L_SGMII:
46                 hw->mac_type = ICE_MAC_GENERIC;
47                 break;
48         default:
49                 hw->mac_type = ICE_MAC_UNKNOWN;
50                 break;
51         }
52
53         ice_debug(hw, ICE_DBG_INIT, "mac_type: %d\n", hw->mac_type);
54         return ICE_SUCCESS;
55 }
56
57 /**
58  * ice_clear_pf_cfg - Clear PF configuration
59  * @hw: pointer to the hardware structure
60  *
61  * Clears any existing PF configuration (VSIs, VSI lists, switch rules, port
62  * configuration, flow director filters, etc.).
63  */
64 enum ice_status ice_clear_pf_cfg(struct ice_hw *hw)
65 {
66         struct ice_aq_desc desc;
67
68         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pf_cfg);
69
70         return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
71 }
72
73 /**
74  * ice_aq_manage_mac_read - manage MAC address read command
75  * @hw: pointer to the HW struct
76  * @buf: a virtual buffer to hold the manage MAC read response
77  * @buf_size: Size of the virtual buffer
78  * @cd: pointer to command details structure or NULL
79  *
80  * This function is used to return per PF station MAC address (0x0107).
81  * NOTE: Upon successful completion of this command, MAC address information
82  * is returned in user specified buffer. Please interpret user specified
83  * buffer as "manage_mac_read" response.
84  * Response such as various MAC addresses are stored in HW struct (port.mac)
85  * ice_aq_discover_caps is expected to be called before this function is called.
86  */
87 static enum ice_status
88 ice_aq_manage_mac_read(struct ice_hw *hw, void *buf, u16 buf_size,
89                        struct ice_sq_cd *cd)
90 {
91         struct ice_aqc_manage_mac_read_resp *resp;
92         struct ice_aqc_manage_mac_read *cmd;
93         struct ice_aq_desc desc;
94         enum ice_status status;
95         u16 flags;
96         u8 i;
97
98         cmd = &desc.params.mac_read;
99
100         if (buf_size < sizeof(*resp))
101                 return ICE_ERR_BUF_TOO_SHORT;
102
103         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_read);
104
105         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
106         if (status)
107                 return status;
108
109         resp = (struct ice_aqc_manage_mac_read_resp *)buf;
110         flags = LE16_TO_CPU(cmd->flags) & ICE_AQC_MAN_MAC_READ_M;
111
112         if (!(flags & ICE_AQC_MAN_MAC_LAN_ADDR_VALID)) {
113                 ice_debug(hw, ICE_DBG_LAN, "got invalid MAC address\n");
114                 return ICE_ERR_CFG;
115         }
116
117         /* A single port can report up to two (LAN and WoL) addresses */
118         for (i = 0; i < cmd->num_addr; i++)
119                 if (resp[i].addr_type == ICE_AQC_MAN_MAC_ADDR_TYPE_LAN) {
120                         ice_memcpy(hw->port_info->mac.lan_addr,
121                                    resp[i].mac_addr, ETH_ALEN,
122                                    ICE_DMA_TO_NONDMA);
123                         ice_memcpy(hw->port_info->mac.perm_addr,
124                                    resp[i].mac_addr,
125                                    ETH_ALEN, ICE_DMA_TO_NONDMA);
126                         break;
127                 }
128         return ICE_SUCCESS;
129 }
130
131 /**
132  * ice_aq_get_phy_caps - returns PHY capabilities
133  * @pi: port information structure
134  * @qual_mods: report qualified modules
135  * @report_mode: report mode capabilities
136  * @pcaps: structure for PHY capabilities to be filled
137  * @cd: pointer to command details structure or NULL
138  *
139  * Returns the various PHY capabilities supported on the Port (0x0600)
140  */
141 enum ice_status
142 ice_aq_get_phy_caps(struct ice_port_info *pi, bool qual_mods, u8 report_mode,
143                     struct ice_aqc_get_phy_caps_data *pcaps,
144                     struct ice_sq_cd *cd)
145 {
146         struct ice_aqc_get_phy_caps *cmd;
147         u16 pcaps_size = sizeof(*pcaps);
148         struct ice_aq_desc desc;
149         enum ice_status status;
150
151         cmd = &desc.params.get_phy;
152
153         if (!pcaps || (report_mode & ~ICE_AQC_REPORT_MODE_M) || !pi)
154                 return ICE_ERR_PARAM;
155
156         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_phy_caps);
157
158         if (qual_mods)
159                 cmd->param0 |= CPU_TO_LE16(ICE_AQC_GET_PHY_RQM);
160
161         cmd->param0 |= CPU_TO_LE16(report_mode);
162         status = ice_aq_send_cmd(pi->hw, &desc, pcaps, pcaps_size, cd);
163
164         if (status == ICE_SUCCESS && report_mode == ICE_AQC_REPORT_TOPO_CAP) {
165                 pi->phy.phy_type_low = LE64_TO_CPU(pcaps->phy_type_low);
166                 pi->phy.phy_type_high = LE64_TO_CPU(pcaps->phy_type_high);
167         }
168
169         return status;
170 }
171
172 /**
173  * ice_aq_get_link_topo_handle - get link topology node return status
174  * @pi: port information structure
175  * @node_type: requested node type
176  * @cd: pointer to command details structure or NULL
177  *
178  * Get link topology node return status for specified node type (0x06E0)
179  *
180  * Node type cage can be used to determine if cage is present. If AQC
181  * returns error (ENOENT), then no cage present. If no cage present, then
182  * connection type is backplane or BASE-T.
183  */
184 static enum ice_status
185 ice_aq_get_link_topo_handle(struct ice_port_info *pi, u8 node_type,
186                             struct ice_sq_cd *cd)
187 {
188         struct ice_aqc_get_link_topo *cmd;
189         struct ice_aq_desc desc;
190
191         cmd = &desc.params.get_link_topo;
192
193         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
194
195         cmd->addr.node_type_ctx = (ICE_AQC_LINK_TOPO_NODE_CTX_PORT <<
196                                    ICE_AQC_LINK_TOPO_NODE_CTX_S);
197
198         /* set node type */
199         cmd->addr.node_type_ctx |= (ICE_AQC_LINK_TOPO_NODE_TYPE_M & node_type);
200
201         return ice_aq_send_cmd(pi->hw, &desc, NULL, 0, cd);
202 }
203
204 /**
205  * ice_is_media_cage_present
206  * @pi: port information structure
207  *
208  * Returns true if media cage is present, else false. If no cage, then
209  * media type is backplane or BASE-T.
210  */
211 static bool ice_is_media_cage_present(struct ice_port_info *pi)
212 {
213         /* Node type cage can be used to determine if cage is present. If AQC
214          * returns error (ENOENT), then no cage present. If no cage present then
215          * connection type is backplane or BASE-T.
216          */
217         return !ice_aq_get_link_topo_handle(pi,
218                                             ICE_AQC_LINK_TOPO_NODE_TYPE_CAGE,
219                                             NULL);
220 }
221
222 /**
223  * ice_get_media_type - Gets media type
224  * @pi: port information structure
225  */
226 static enum ice_media_type ice_get_media_type(struct ice_port_info *pi)
227 {
228         struct ice_link_status *hw_link_info;
229
230         if (!pi)
231                 return ICE_MEDIA_UNKNOWN;
232
233         hw_link_info = &pi->phy.link_info;
234         if (hw_link_info->phy_type_low && hw_link_info->phy_type_high)
235                 /* If more than one media type is selected, report unknown */
236                 return ICE_MEDIA_UNKNOWN;
237
238         if (hw_link_info->phy_type_low) {
239                 switch (hw_link_info->phy_type_low) {
240                 case ICE_PHY_TYPE_LOW_1000BASE_SX:
241                 case ICE_PHY_TYPE_LOW_1000BASE_LX:
242                 case ICE_PHY_TYPE_LOW_10GBASE_SR:
243                 case ICE_PHY_TYPE_LOW_10GBASE_LR:
244                 case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
245                 case ICE_PHY_TYPE_LOW_25GBASE_SR:
246                 case ICE_PHY_TYPE_LOW_25GBASE_LR:
247                 case ICE_PHY_TYPE_LOW_40GBASE_SR4:
248                 case ICE_PHY_TYPE_LOW_40GBASE_LR4:
249                 case ICE_PHY_TYPE_LOW_50GBASE_SR2:
250                 case ICE_PHY_TYPE_LOW_50GBASE_LR2:
251                 case ICE_PHY_TYPE_LOW_50GBASE_SR:
252                 case ICE_PHY_TYPE_LOW_50GBASE_FR:
253                 case ICE_PHY_TYPE_LOW_50GBASE_LR:
254                 case ICE_PHY_TYPE_LOW_100GBASE_SR4:
255                 case ICE_PHY_TYPE_LOW_100GBASE_LR4:
256                 case ICE_PHY_TYPE_LOW_100GBASE_SR2:
257                 case ICE_PHY_TYPE_LOW_100GBASE_DR:
258                         return ICE_MEDIA_FIBER;
259                 case ICE_PHY_TYPE_LOW_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(hw, (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(hw, (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(hw, (u8 *)tx_drbell_q_ctx, ctx_buf,
1214                     ice_tx_drbell_q_ctx_info);
1215         return ice_copy_tx_drbell_q_ctx_to_hw(hw, ctx_buf, tx_drbell_q_index);
1216 }
1217
1218 /**
1219  * ice_clear_tx_drbell_q_ctx
1220  * @hw: pointer to the hardware structure
1221  * @tx_drbell_q_index: the index of the doorbell queue to clear
1222  *
1223  * Clears doorbell queue context in HW register space
1224  */
1225 enum ice_status
1226 ice_clear_tx_drbell_q_ctx(struct ice_hw *hw, u32 tx_drbell_q_index)
1227 {
1228         u8 i;
1229
1230         if (tx_drbell_q_index > QTX_COMM_DBLQ_DBELL_MAX_INDEX)
1231                 return ICE_ERR_PARAM;
1232
1233         /* Clear each dword register separately */
1234         for (i = 0; i < ICE_TX_DRBELL_Q_CTX_SIZE_DWORDS; i++)
1235                 wr32(hw, QTX_COMM_DBLQ_CNTX(i, tx_drbell_q_index), 0);
1236
1237         return ICE_SUCCESS;
1238 }
1239
1240 /* FW Admin Queue command wrappers */
1241
1242 /**
1243  * ice_aq_send_cmd - send FW Admin Queue command to FW Admin Queue
1244  * @hw: pointer to the HW struct
1245  * @desc: descriptor describing the command
1246  * @buf: buffer to use for indirect commands (NULL for direct commands)
1247  * @buf_size: size of buffer for indirect commands (0 for direct commands)
1248  * @cd: pointer to command details structure
1249  *
1250  * Helper function to send FW Admin Queue commands to the FW Admin Queue.
1251  */
1252 enum ice_status
1253 ice_aq_send_cmd(struct ice_hw *hw, struct ice_aq_desc *desc, void *buf,
1254                 u16 buf_size, struct ice_sq_cd *cd)
1255 {
1256         if (hw->aq_send_cmd_fn) {
1257                 enum ice_status status = ICE_ERR_NOT_READY;
1258                 u16 retval = ICE_AQ_RC_OK;
1259
1260                 ice_acquire_lock(&hw->adminq.sq_lock);
1261                 if (!hw->aq_send_cmd_fn(hw->aq_send_cmd_param, desc,
1262                                         buf, buf_size)) {
1263                         retval = LE16_TO_CPU(desc->retval);
1264                         /* strip off FW internal code */
1265                         if (retval)
1266                                 retval &= 0xff;
1267                         if (retval == ICE_AQ_RC_OK)
1268                                 status = ICE_SUCCESS;
1269                         else
1270                                 status = ICE_ERR_AQ_ERROR;
1271                 }
1272
1273                 hw->adminq.sq_last_status = (enum ice_aq_err)retval;
1274                 ice_release_lock(&hw->adminq.sq_lock);
1275
1276                 return status;
1277         }
1278         return ice_sq_send_cmd(hw, &hw->adminq, desc, buf, buf_size, cd);
1279 }
1280
1281 /**
1282  * ice_aq_get_fw_ver
1283  * @hw: pointer to the HW struct
1284  * @cd: pointer to command details structure or NULL
1285  *
1286  * Get the firmware version (0x0001) from the admin queue commands
1287  */
1288 enum ice_status ice_aq_get_fw_ver(struct ice_hw *hw, struct ice_sq_cd *cd)
1289 {
1290         struct ice_aqc_get_ver *resp;
1291         struct ice_aq_desc desc;
1292         enum ice_status status;
1293
1294         resp = &desc.params.get_ver;
1295
1296         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_ver);
1297
1298         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1299
1300         if (!status) {
1301                 hw->fw_branch = resp->fw_branch;
1302                 hw->fw_maj_ver = resp->fw_major;
1303                 hw->fw_min_ver = resp->fw_minor;
1304                 hw->fw_patch = resp->fw_patch;
1305                 hw->fw_build = LE32_TO_CPU(resp->fw_build);
1306                 hw->api_branch = resp->api_branch;
1307                 hw->api_maj_ver = resp->api_major;
1308                 hw->api_min_ver = resp->api_minor;
1309                 hw->api_patch = resp->api_patch;
1310         }
1311
1312         return status;
1313 }
1314
1315 /**
1316  * ice_aq_send_driver_ver
1317  * @hw: pointer to the HW struct
1318  * @dv: driver's major, minor version
1319  * @cd: pointer to command details structure or NULL
1320  *
1321  * Send the driver version (0x0002) to the firmware
1322  */
1323 enum ice_status
1324 ice_aq_send_driver_ver(struct ice_hw *hw, struct ice_driver_ver *dv,
1325                        struct ice_sq_cd *cd)
1326 {
1327         struct ice_aqc_driver_ver *cmd;
1328         struct ice_aq_desc desc;
1329         u16 len;
1330
1331         cmd = &desc.params.driver_ver;
1332
1333         if (!dv)
1334                 return ICE_ERR_PARAM;
1335
1336         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_driver_ver);
1337
1338         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
1339         cmd->major_ver = dv->major_ver;
1340         cmd->minor_ver = dv->minor_ver;
1341         cmd->build_ver = dv->build_ver;
1342         cmd->subbuild_ver = dv->subbuild_ver;
1343
1344         len = 0;
1345         while (len < sizeof(dv->driver_string) &&
1346                IS_ASCII(dv->driver_string[len]) && dv->driver_string[len])
1347                 len++;
1348
1349         return ice_aq_send_cmd(hw, &desc, dv->driver_string, len, cd);
1350 }
1351
1352 /**
1353  * ice_aq_q_shutdown
1354  * @hw: pointer to the HW struct
1355  * @unloading: is the driver unloading itself
1356  *
1357  * Tell the Firmware that we're shutting down the AdminQ and whether
1358  * or not the driver is unloading as well (0x0003).
1359  */
1360 enum ice_status ice_aq_q_shutdown(struct ice_hw *hw, bool unloading)
1361 {
1362         struct ice_aqc_q_shutdown *cmd;
1363         struct ice_aq_desc desc;
1364
1365         cmd = &desc.params.q_shutdown;
1366
1367         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_q_shutdown);
1368
1369         if (unloading)
1370                 cmd->driver_unloading = ICE_AQC_DRIVER_UNLOADING;
1371
1372         return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1373 }
1374
1375 /**
1376  * ice_aq_req_res
1377  * @hw: pointer to the HW struct
1378  * @res: resource ID
1379  * @access: access type
1380  * @sdp_number: resource number
1381  * @timeout: the maximum time in ms that the driver may hold the resource
1382  * @cd: pointer to command details structure or NULL
1383  *
1384  * Requests common resource using the admin queue commands (0x0008).
1385  * When attempting to acquire the Global Config Lock, the driver can
1386  * learn of three states:
1387  *  1) ICE_SUCCESS -        acquired lock, and can perform download package
1388  *  2) ICE_ERR_AQ_ERROR -   did not get lock, driver should fail to load
1389  *  3) ICE_ERR_AQ_NO_WORK - did not get lock, but another driver has
1390  *                          successfully downloaded the package; the driver does
1391  *                          not have to download the package and can continue
1392  *                          loading
1393  *
1394  * Note that if the caller is in an acquire lock, perform action, release lock
1395  * phase of operation, it is possible that the FW may detect a timeout and issue
1396  * a CORER. In this case, the driver will receive a CORER interrupt and will
1397  * have to determine its cause. The calling thread that is handling this flow
1398  * will likely get an error propagated back to it indicating the Download
1399  * Package, Update Package or the Release Resource AQ commands timed out.
1400  */
1401 static enum ice_status
1402 ice_aq_req_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1403                enum ice_aq_res_access_type access, u8 sdp_number, u32 *timeout,
1404                struct ice_sq_cd *cd)
1405 {
1406         struct ice_aqc_req_res *cmd_resp;
1407         struct ice_aq_desc desc;
1408         enum ice_status status;
1409
1410         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1411
1412         cmd_resp = &desc.params.res_owner;
1413
1414         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_req_res);
1415
1416         cmd_resp->res_id = CPU_TO_LE16(res);
1417         cmd_resp->access_type = CPU_TO_LE16(access);
1418         cmd_resp->res_number = CPU_TO_LE32(sdp_number);
1419         cmd_resp->timeout = CPU_TO_LE32(*timeout);
1420         *timeout = 0;
1421
1422         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1423
1424         /* The completion specifies the maximum time in ms that the driver
1425          * may hold the resource in the Timeout field.
1426          */
1427
1428         /* Global config lock response utilizes an additional status field.
1429          *
1430          * If the Global config lock resource is held by some other driver, the
1431          * command completes with ICE_AQ_RES_GLBL_IN_PROG in the status field
1432          * and the timeout field indicates the maximum time the current owner
1433          * of the resource has to free it.
1434          */
1435         if (res == ICE_GLOBAL_CFG_LOCK_RES_ID) {
1436                 if (LE16_TO_CPU(cmd_resp->status) == ICE_AQ_RES_GLBL_SUCCESS) {
1437                         *timeout = LE32_TO_CPU(cmd_resp->timeout);
1438                         return ICE_SUCCESS;
1439                 } else if (LE16_TO_CPU(cmd_resp->status) ==
1440                            ICE_AQ_RES_GLBL_IN_PROG) {
1441                         *timeout = LE32_TO_CPU(cmd_resp->timeout);
1442                         return ICE_ERR_AQ_ERROR;
1443                 } else if (LE16_TO_CPU(cmd_resp->status) ==
1444                            ICE_AQ_RES_GLBL_DONE) {
1445                         return ICE_ERR_AQ_NO_WORK;
1446                 }
1447
1448                 /* invalid FW response, force a timeout immediately */
1449                 *timeout = 0;
1450                 return ICE_ERR_AQ_ERROR;
1451         }
1452
1453         /* If the resource is held by some other driver, the command completes
1454          * with a busy return value and the timeout field indicates the maximum
1455          * time the current owner of the resource has to free it.
1456          */
1457         if (!status || hw->adminq.sq_last_status == ICE_AQ_RC_EBUSY)
1458                 *timeout = LE32_TO_CPU(cmd_resp->timeout);
1459
1460         return status;
1461 }
1462
1463 /**
1464  * ice_aq_release_res
1465  * @hw: pointer to the HW struct
1466  * @res: resource ID
1467  * @sdp_number: resource number
1468  * @cd: pointer to command details structure or NULL
1469  *
1470  * release common resource using the admin queue commands (0x0009)
1471  */
1472 static enum ice_status
1473 ice_aq_release_res(struct ice_hw *hw, enum ice_aq_res_ids res, u8 sdp_number,
1474                    struct ice_sq_cd *cd)
1475 {
1476         struct ice_aqc_req_res *cmd;
1477         struct ice_aq_desc desc;
1478
1479         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1480
1481         cmd = &desc.params.res_owner;
1482
1483         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_release_res);
1484
1485         cmd->res_id = CPU_TO_LE16(res);
1486         cmd->res_number = CPU_TO_LE32(sdp_number);
1487
1488         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1489 }
1490
1491 /**
1492  * ice_acquire_res
1493  * @hw: pointer to the HW structure
1494  * @res: resource ID
1495  * @access: access type (read or write)
1496  * @timeout: timeout in milliseconds
1497  *
1498  * This function will attempt to acquire the ownership of a resource.
1499  */
1500 enum ice_status
1501 ice_acquire_res(struct ice_hw *hw, enum ice_aq_res_ids res,
1502                 enum ice_aq_res_access_type access, u32 timeout)
1503 {
1504 #define ICE_RES_POLLING_DELAY_MS        10
1505         u32 delay = ICE_RES_POLLING_DELAY_MS;
1506         u32 time_left = timeout;
1507         enum ice_status status;
1508
1509         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1510
1511         status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1512
1513         /* A return code of ICE_ERR_AQ_NO_WORK means that another driver has
1514          * previously acquired the resource and performed any necessary updates;
1515          * in this case the caller does not obtain the resource and has no
1516          * further work to do.
1517          */
1518         if (status == ICE_ERR_AQ_NO_WORK)
1519                 goto ice_acquire_res_exit;
1520
1521         if (status)
1522                 ice_debug(hw, ICE_DBG_RES,
1523                           "resource %d acquire type %d failed.\n", res, access);
1524
1525         /* If necessary, poll until the current lock owner timeouts */
1526         timeout = time_left;
1527         while (status && timeout && time_left) {
1528                 ice_msec_delay(delay, true);
1529                 timeout = (timeout > delay) ? timeout - delay : 0;
1530                 status = ice_aq_req_res(hw, res, access, 0, &time_left, NULL);
1531
1532                 if (status == ICE_ERR_AQ_NO_WORK)
1533                         /* lock free, but no work to do */
1534                         break;
1535
1536                 if (!status)
1537                         /* lock acquired */
1538                         break;
1539         }
1540         if (status && status != ICE_ERR_AQ_NO_WORK)
1541                 ice_debug(hw, ICE_DBG_RES, "resource acquire timed out.\n");
1542
1543 ice_acquire_res_exit:
1544         if (status == ICE_ERR_AQ_NO_WORK) {
1545                 if (access == ICE_RES_WRITE)
1546                         ice_debug(hw, ICE_DBG_RES,
1547                                   "resource indicates no work to do.\n");
1548                 else
1549                         ice_debug(hw, ICE_DBG_RES,
1550                                   "Warning: ICE_ERR_AQ_NO_WORK not expected\n");
1551         }
1552         return status;
1553 }
1554
1555 /**
1556  * ice_release_res
1557  * @hw: pointer to the HW structure
1558  * @res: resource ID
1559  *
1560  * This function will release a resource using the proper Admin Command.
1561  */
1562 void ice_release_res(struct ice_hw *hw, enum ice_aq_res_ids res)
1563 {
1564         enum ice_status status;
1565         u32 total_delay = 0;
1566
1567         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1568
1569         status = ice_aq_release_res(hw, res, 0, NULL);
1570
1571         /* there are some rare cases when trying to release the resource
1572          * results in an admin queue timeout, so handle them correctly
1573          */
1574         while ((status == ICE_ERR_AQ_TIMEOUT) &&
1575                (total_delay < hw->adminq.sq_cmd_timeout)) {
1576                 ice_msec_delay(1, true);
1577                 status = ice_aq_release_res(hw, res, 0, NULL);
1578                 total_delay++;
1579         }
1580 }
1581
1582 /**
1583  * ice_aq_alloc_free_res - command to allocate/free resources
1584  * @hw: pointer to the HW struct
1585  * @num_entries: number of resource entries in buffer
1586  * @buf: Indirect buffer to hold data parameters and response
1587  * @buf_size: size of buffer for indirect commands
1588  * @opc: pass in the command opcode
1589  * @cd: pointer to command details structure or NULL
1590  *
1591  * Helper function to allocate/free resources using the admin queue commands
1592  */
1593 enum ice_status
1594 ice_aq_alloc_free_res(struct ice_hw *hw, u16 num_entries,
1595                       struct ice_aqc_alloc_free_res_elem *buf, u16 buf_size,
1596                       enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1597 {
1598         struct ice_aqc_alloc_free_res_cmd *cmd;
1599         struct ice_aq_desc desc;
1600
1601         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1602
1603         cmd = &desc.params.sw_res_ctrl;
1604
1605         if (!buf)
1606                 return ICE_ERR_PARAM;
1607
1608         if (buf_size < (num_entries * sizeof(buf->elem[0])))
1609                 return ICE_ERR_PARAM;
1610
1611         ice_fill_dflt_direct_cmd_desc(&desc, opc);
1612
1613         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
1614
1615         cmd->num_entries = CPU_TO_LE16(num_entries);
1616
1617         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1618 }
1619
1620 /**
1621  * ice_alloc_hw_res - allocate resource
1622  * @hw: pointer to the HW struct
1623  * @type: type of resource
1624  * @num: number of resources to allocate
1625  * @btm: allocate from bottom
1626  * @res: pointer to array that will receive the resources
1627  */
1628 enum ice_status
1629 ice_alloc_hw_res(struct ice_hw *hw, u16 type, u16 num, bool btm, u16 *res)
1630 {
1631         struct ice_aqc_alloc_free_res_elem *buf;
1632         enum ice_status status;
1633         u16 buf_len;
1634
1635         buf_len = ice_struct_size(buf, elem, num - 1);
1636         buf = (struct ice_aqc_alloc_free_res_elem *)
1637                 ice_malloc(hw, buf_len);
1638         if (!buf)
1639                 return ICE_ERR_NO_MEMORY;
1640
1641         /* Prepare buffer to allocate resource. */
1642         buf->num_elems = CPU_TO_LE16(num);
1643         buf->res_type = CPU_TO_LE16(type | ICE_AQC_RES_TYPE_FLAG_DEDICATED |
1644                                     ICE_AQC_RES_TYPE_FLAG_IGNORE_INDEX);
1645         if (btm)
1646                 buf->res_type |= CPU_TO_LE16(ICE_AQC_RES_TYPE_FLAG_SCAN_BOTTOM);
1647
1648         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
1649                                        ice_aqc_opc_alloc_res, NULL);
1650         if (status)
1651                 goto ice_alloc_res_exit;
1652
1653         ice_memcpy(res, buf->elem, sizeof(buf->elem) * num,
1654                    ICE_NONDMA_TO_NONDMA);
1655
1656 ice_alloc_res_exit:
1657         ice_free(hw, buf);
1658         return status;
1659 }
1660
1661 /**
1662  * ice_free_hw_res - free allocated HW resource
1663  * @hw: pointer to the HW struct
1664  * @type: type of resource to free
1665  * @num: number of resources
1666  * @res: pointer to array that contains the resources to free
1667  */
1668 enum ice_status
1669 ice_free_hw_res(struct ice_hw *hw, u16 type, u16 num, u16 *res)
1670 {
1671         struct ice_aqc_alloc_free_res_elem *buf;
1672         enum ice_status status;
1673         u16 buf_len;
1674
1675         buf_len = ice_struct_size(buf, elem, num - 1);
1676         buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
1677         if (!buf)
1678                 return ICE_ERR_NO_MEMORY;
1679
1680         /* Prepare buffer to free resource. */
1681         buf->num_elems = CPU_TO_LE16(num);
1682         buf->res_type = CPU_TO_LE16(type);
1683         ice_memcpy(buf->elem, res, sizeof(buf->elem) * num,
1684                    ICE_NONDMA_TO_NONDMA);
1685
1686         status = ice_aq_alloc_free_res(hw, num, buf, buf_len,
1687                                        ice_aqc_opc_free_res, NULL);
1688         if (status)
1689                 ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
1690
1691         ice_free(hw, buf);
1692         return status;
1693 }
1694
1695 /**
1696  * ice_get_num_per_func - determine number of resources per PF
1697  * @hw: pointer to the HW structure
1698  * @max: value to be evenly split between each PF
1699  *
1700  * Determine the number of valid functions by going through the bitmap returned
1701  * from parsing capabilities and use this to calculate the number of resources
1702  * per PF based on the max value passed in.
1703  */
1704 static u32 ice_get_num_per_func(struct ice_hw *hw, u32 max)
1705 {
1706         u8 funcs;
1707
1708 #define ICE_CAPS_VALID_FUNCS_M  0xFF
1709         funcs = ice_hweight8(hw->dev_caps.common_cap.valid_functions &
1710                              ICE_CAPS_VALID_FUNCS_M);
1711
1712         if (!funcs)
1713                 return 0;
1714
1715         return max / funcs;
1716 }
1717
1718 /**
1719  * ice_parse_caps - parse function/device capabilities
1720  * @hw: pointer to the HW struct
1721  * @buf: pointer to a buffer containing function/device capability records
1722  * @cap_count: number of capability records in the list
1723  * @opc: type of capabilities list to parse
1724  *
1725  * Helper function to parse function(0x000a)/device(0x000b) capabilities list.
1726  */
1727 static void
1728 ice_parse_caps(struct ice_hw *hw, void *buf, u32 cap_count,
1729                enum ice_adminq_opc opc)
1730 {
1731         struct ice_aqc_list_caps_elem *cap_resp;
1732         struct ice_hw_func_caps *func_p = NULL;
1733         struct ice_hw_dev_caps *dev_p = NULL;
1734         struct ice_hw_common_caps *caps;
1735         char const *prefix;
1736         u32 i;
1737
1738         if (!buf)
1739                 return;
1740
1741         cap_resp = (struct ice_aqc_list_caps_elem *)buf;
1742
1743         if (opc == ice_aqc_opc_list_dev_caps) {
1744                 dev_p = &hw->dev_caps;
1745                 caps = &dev_p->common_cap;
1746                 prefix = "dev cap";
1747         } else if (opc == ice_aqc_opc_list_func_caps) {
1748                 func_p = &hw->func_caps;
1749                 caps = &func_p->common_cap;
1750                 prefix = "func cap";
1751         } else {
1752                 ice_debug(hw, ICE_DBG_INIT, "wrong opcode\n");
1753                 return;
1754         }
1755
1756         for (i = 0; caps && i < cap_count; i++, cap_resp++) {
1757                 u32 logical_id = LE32_TO_CPU(cap_resp->logical_id);
1758                 u32 phys_id = LE32_TO_CPU(cap_resp->phys_id);
1759                 u32 number = LE32_TO_CPU(cap_resp->number);
1760                 u16 cap = LE16_TO_CPU(cap_resp->cap);
1761
1762                 switch (cap) {
1763                 case ICE_AQC_CAPS_VALID_FUNCTIONS:
1764                         caps->valid_functions = number;
1765                         ice_debug(hw, ICE_DBG_INIT,
1766                                   "%s: valid_functions (bitmap) = %d\n", prefix,
1767                                   caps->valid_functions);
1768
1769                         /* store func count for resource management purposes */
1770                         if (dev_p)
1771                                 dev_p->num_funcs = ice_hweight32(number);
1772                         break;
1773                 case ICE_AQC_CAPS_VSI:
1774                         if (dev_p) {
1775                                 dev_p->num_vsi_allocd_to_host = number;
1776                                 ice_debug(hw, ICE_DBG_INIT,
1777                                           "%s: num_vsi_allocd_to_host = %d\n",
1778                                           prefix,
1779                                           dev_p->num_vsi_allocd_to_host);
1780                         } else if (func_p) {
1781                                 func_p->guar_num_vsi =
1782                                         ice_get_num_per_func(hw, ICE_MAX_VSI);
1783                                 ice_debug(hw, ICE_DBG_INIT,
1784                                           "%s: guar_num_vsi (fw) = %d\n",
1785                                           prefix, number);
1786                                 ice_debug(hw, ICE_DBG_INIT,
1787                                           "%s: guar_num_vsi = %d\n",
1788                                           prefix, func_p->guar_num_vsi);
1789                         }
1790                         break;
1791                 case ICE_AQC_CAPS_DCB:
1792                         caps->dcb = (number == 1);
1793                         caps->active_tc_bitmap = logical_id;
1794                         caps->maxtc = phys_id;
1795                         ice_debug(hw, ICE_DBG_INIT,
1796                                   "%s: dcb = %d\n", prefix, caps->dcb);
1797                         ice_debug(hw, ICE_DBG_INIT,
1798                                   "%s: active_tc_bitmap = %d\n", prefix,
1799                                   caps->active_tc_bitmap);
1800                         ice_debug(hw, ICE_DBG_INIT,
1801                                   "%s: maxtc = %d\n", prefix, caps->maxtc);
1802                         break;
1803                 case ICE_AQC_CAPS_RSS:
1804                         caps->rss_table_size = number;
1805                         caps->rss_table_entry_width = logical_id;
1806                         ice_debug(hw, ICE_DBG_INIT,
1807                                   "%s: rss_table_size = %d\n", prefix,
1808                                   caps->rss_table_size);
1809                         ice_debug(hw, ICE_DBG_INIT,
1810                                   "%s: rss_table_entry_width = %d\n", prefix,
1811                                   caps->rss_table_entry_width);
1812                         break;
1813                 case ICE_AQC_CAPS_RXQS:
1814                         caps->num_rxq = number;
1815                         caps->rxq_first_id = phys_id;
1816                         ice_debug(hw, ICE_DBG_INIT,
1817                                   "%s: num_rxq = %d\n", prefix,
1818                                   caps->num_rxq);
1819                         ice_debug(hw, ICE_DBG_INIT,
1820                                   "%s: rxq_first_id = %d\n", prefix,
1821                                   caps->rxq_first_id);
1822                         break;
1823                 case ICE_AQC_CAPS_TXQS:
1824                         caps->num_txq = number;
1825                         caps->txq_first_id = phys_id;
1826                         ice_debug(hw, ICE_DBG_INIT,
1827                                   "%s: num_txq = %d\n", prefix,
1828                                   caps->num_txq);
1829                         ice_debug(hw, ICE_DBG_INIT,
1830                                   "%s: txq_first_id = %d\n", prefix,
1831                                   caps->txq_first_id);
1832                         break;
1833                 case ICE_AQC_CAPS_MSIX:
1834                         caps->num_msix_vectors = number;
1835                         caps->msix_vector_first_id = phys_id;
1836                         ice_debug(hw, ICE_DBG_INIT,
1837                                   "%s: num_msix_vectors = %d\n", prefix,
1838                                   caps->num_msix_vectors);
1839                         ice_debug(hw, ICE_DBG_INIT,
1840                                   "%s: msix_vector_first_id = %d\n", prefix,
1841                                   caps->msix_vector_first_id);
1842                         break;
1843                 case ICE_AQC_CAPS_FD:
1844                         if (dev_p) {
1845                                 dev_p->num_flow_director_fltr = number;
1846                                 ice_debug(hw, ICE_DBG_INIT,
1847                                           "%s: num_flow_director_fltr = %d\n",
1848                                           prefix,
1849                                           dev_p->num_flow_director_fltr);
1850                         }
1851                         if (func_p) {
1852                                 u32 reg_val, val;
1853                                 if (hw->dcf_enabled)
1854                                         break;
1855                                 reg_val = rd32(hw, GLQF_FD_SIZE);
1856                                 val = (reg_val & GLQF_FD_SIZE_FD_GSIZE_M) >>
1857                                       GLQF_FD_SIZE_FD_GSIZE_S;
1858                                 func_p->fd_fltr_guar =
1859                                         ice_get_num_per_func(hw, val);
1860                                 val = (reg_val & GLQF_FD_SIZE_FD_BSIZE_M) >>
1861                                       GLQF_FD_SIZE_FD_BSIZE_S;
1862                                 func_p->fd_fltr_best_effort = val;
1863                                 ice_debug(hw, ICE_DBG_INIT,
1864                                           "%s: fd_fltr_guar = %d\n",
1865                                           prefix, func_p->fd_fltr_guar);
1866                                 ice_debug(hw, ICE_DBG_INIT,
1867                                           "%s: fd_fltr_best_effort = %d\n",
1868                                           prefix, func_p->fd_fltr_best_effort);
1869                         }
1870                         break;
1871                 case ICE_AQC_CAPS_MAX_MTU:
1872                         caps->max_mtu = number;
1873                         ice_debug(hw, ICE_DBG_INIT, "%s: max_mtu = %d\n",
1874                                   prefix, caps->max_mtu);
1875                         break;
1876                 default:
1877                         ice_debug(hw, ICE_DBG_INIT,
1878                                   "%s: unknown capability[%d]: 0x%x\n", prefix,
1879                                   i, cap);
1880                         break;
1881                 }
1882         }
1883
1884         /* Re-calculate capabilities that are dependent on the number of
1885          * physical ports; i.e. some features are not supported or function
1886          * differently on devices with more than 4 ports.
1887          */
1888         if (hw->dev_caps.num_funcs > 4) {
1889                 /* Max 4 TCs per port */
1890                 caps->maxtc = 4;
1891                 ice_debug(hw, ICE_DBG_INIT,
1892                           "%s: maxtc = %d (based on #ports)\n", prefix,
1893                           caps->maxtc);
1894         }
1895 }
1896
1897 /**
1898  * ice_aq_discover_caps - query function/device capabilities
1899  * @hw: pointer to the HW struct
1900  * @buf: a virtual buffer to hold the capabilities
1901  * @buf_size: Size of the virtual buffer
1902  * @cap_count: cap count needed if AQ err==ENOMEM
1903  * @opc: capabilities type to discover - pass in the command opcode
1904  * @cd: pointer to command details structure or NULL
1905  *
1906  * Get the function(0x000a)/device(0x000b) capabilities description from
1907  * the firmware.
1908  */
1909 static enum ice_status
1910 ice_aq_discover_caps(struct ice_hw *hw, void *buf, u16 buf_size, u32 *cap_count,
1911                      enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1912 {
1913         struct ice_aqc_list_caps *cmd;
1914         struct ice_aq_desc desc;
1915         enum ice_status status;
1916
1917         cmd = &desc.params.get_cap;
1918
1919         if (opc != ice_aqc_opc_list_func_caps &&
1920             opc != ice_aqc_opc_list_dev_caps)
1921                 return ICE_ERR_PARAM;
1922
1923         ice_fill_dflt_direct_cmd_desc(&desc, opc);
1924
1925         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1926         if (!status)
1927                 ice_parse_caps(hw, buf, LE32_TO_CPU(cmd->count), opc);
1928         else if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOMEM)
1929                 *cap_count = LE32_TO_CPU(cmd->count);
1930         return status;
1931 }
1932
1933 /**
1934  * ice_discover_caps - get info about the HW
1935  * @hw: pointer to the hardware structure
1936  * @opc: capabilities type to discover - pass in the command opcode
1937  */
1938 static enum ice_status
1939 ice_discover_caps(struct ice_hw *hw, enum ice_adminq_opc opc)
1940 {
1941         enum ice_status status;
1942         u32 cap_count;
1943         u16 cbuf_len;
1944         u8 retries;
1945
1946         /* The driver doesn't know how many capabilities the device will return
1947          * so the buffer size required isn't known ahead of time. The driver
1948          * starts with cbuf_len and if this turns out to be insufficient, the
1949          * device returns ICE_AQ_RC_ENOMEM and also the cap_count it needs.
1950          * The driver then allocates the buffer based on the count and retries
1951          * the operation. So it follows that the retry count is 2.
1952          */
1953 #define ICE_GET_CAP_BUF_COUNT   40
1954 #define ICE_GET_CAP_RETRY_COUNT 2
1955
1956         cap_count = ICE_GET_CAP_BUF_COUNT;
1957         retries = ICE_GET_CAP_RETRY_COUNT;
1958
1959         do {
1960                 void *cbuf;
1961
1962                 cbuf_len = (u16)(cap_count *
1963                                  sizeof(struct ice_aqc_list_caps_elem));
1964                 cbuf = ice_malloc(hw, cbuf_len);
1965                 if (!cbuf)
1966                         return ICE_ERR_NO_MEMORY;
1967
1968                 status = ice_aq_discover_caps(hw, cbuf, cbuf_len, &cap_count,
1969                                               opc, NULL);
1970                 ice_free(hw, cbuf);
1971
1972                 if (!status || hw->adminq.sq_last_status != ICE_AQ_RC_ENOMEM)
1973                         break;
1974
1975                 /* If ENOMEM is returned, try again with bigger buffer */
1976         } while (--retries);
1977
1978         return status;
1979 }
1980
1981 /**
1982  * ice_set_safe_mode_caps - Override dev/func capabilities when in safe mode
1983  * @hw: pointer to the hardware structure
1984  */
1985 void ice_set_safe_mode_caps(struct ice_hw *hw)
1986 {
1987         struct ice_hw_func_caps *func_caps = &hw->func_caps;
1988         struct ice_hw_dev_caps *dev_caps = &hw->dev_caps;
1989         u32 valid_func, rxq_first_id, txq_first_id;
1990         u32 msix_vector_first_id, max_mtu;
1991         u32 num_funcs;
1992
1993         /* cache some func_caps values that should be restored after memset */
1994         valid_func = func_caps->common_cap.valid_functions;
1995         txq_first_id = func_caps->common_cap.txq_first_id;
1996         rxq_first_id = func_caps->common_cap.rxq_first_id;
1997         msix_vector_first_id = func_caps->common_cap.msix_vector_first_id;
1998         max_mtu = func_caps->common_cap.max_mtu;
1999
2000         /* unset func capabilities */
2001         memset(func_caps, 0, sizeof(*func_caps));
2002
2003         /* restore cached values */
2004         func_caps->common_cap.valid_functions = valid_func;
2005         func_caps->common_cap.txq_first_id = txq_first_id;
2006         func_caps->common_cap.rxq_first_id = rxq_first_id;
2007         func_caps->common_cap.msix_vector_first_id = msix_vector_first_id;
2008         func_caps->common_cap.max_mtu = max_mtu;
2009
2010         /* one Tx and one Rx queue in safe mode */
2011         func_caps->common_cap.num_rxq = 1;
2012         func_caps->common_cap.num_txq = 1;
2013
2014         /* two MSIX vectors, one for traffic and one for misc causes */
2015         func_caps->common_cap.num_msix_vectors = 2;
2016         func_caps->guar_num_vsi = 1;
2017
2018         /* cache some dev_caps values that should be restored after memset */
2019         valid_func = dev_caps->common_cap.valid_functions;
2020         txq_first_id = dev_caps->common_cap.txq_first_id;
2021         rxq_first_id = dev_caps->common_cap.rxq_first_id;
2022         msix_vector_first_id = dev_caps->common_cap.msix_vector_first_id;
2023         max_mtu = dev_caps->common_cap.max_mtu;
2024         num_funcs = dev_caps->num_funcs;
2025
2026         /* unset dev capabilities */
2027         memset(dev_caps, 0, sizeof(*dev_caps));
2028
2029         /* restore cached values */
2030         dev_caps->common_cap.valid_functions = valid_func;
2031         dev_caps->common_cap.txq_first_id = txq_first_id;
2032         dev_caps->common_cap.rxq_first_id = rxq_first_id;
2033         dev_caps->common_cap.msix_vector_first_id = msix_vector_first_id;
2034         dev_caps->common_cap.max_mtu = max_mtu;
2035         dev_caps->num_funcs = num_funcs;
2036
2037         /* one Tx and one Rx queue per function in safe mode */
2038         dev_caps->common_cap.num_rxq = num_funcs;
2039         dev_caps->common_cap.num_txq = num_funcs;
2040
2041         /* two MSIX vectors per function */
2042         dev_caps->common_cap.num_msix_vectors = 2 * num_funcs;
2043 }
2044
2045 /**
2046  * ice_get_caps - get info about the HW
2047  * @hw: pointer to the hardware structure
2048  */
2049 enum ice_status ice_get_caps(struct ice_hw *hw)
2050 {
2051         enum ice_status status;
2052
2053         status = ice_discover_caps(hw, ice_aqc_opc_list_dev_caps);
2054         if (!status)
2055                 status = ice_discover_caps(hw, ice_aqc_opc_list_func_caps);
2056
2057         return status;
2058 }
2059
2060 /**
2061  * ice_aq_manage_mac_write - manage MAC address write command
2062  * @hw: pointer to the HW struct
2063  * @mac_addr: MAC address to be written as LAA/LAA+WoL/Port address
2064  * @flags: flags to control write behavior
2065  * @cd: pointer to command details structure or NULL
2066  *
2067  * This function is used to write MAC address to the NVM (0x0108).
2068  */
2069 enum ice_status
2070 ice_aq_manage_mac_write(struct ice_hw *hw, const u8 *mac_addr, u8 flags,
2071                         struct ice_sq_cd *cd)
2072 {
2073         struct ice_aqc_manage_mac_write *cmd;
2074         struct ice_aq_desc desc;
2075
2076         cmd = &desc.params.mac_write;
2077         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_manage_mac_write);
2078
2079         cmd->flags = flags;
2080         ice_memcpy(cmd->mac_addr, mac_addr, ETH_ALEN, ICE_NONDMA_TO_DMA);
2081
2082         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2083 }
2084
2085 /**
2086  * ice_aq_clear_pxe_mode
2087  * @hw: pointer to the HW struct
2088  *
2089  * Tell the firmware that the driver is taking over from PXE (0x0110).
2090  */
2091 static enum ice_status ice_aq_clear_pxe_mode(struct ice_hw *hw)
2092 {
2093         struct ice_aq_desc desc;
2094
2095         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_clear_pxe_mode);
2096         desc.params.clear_pxe.rx_cnt = ICE_AQC_CLEAR_PXE_RX_CNT;
2097
2098         return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
2099 }
2100
2101 /**
2102  * ice_clear_pxe_mode - clear pxe operations mode
2103  * @hw: pointer to the HW struct
2104  *
2105  * Make sure all PXE mode settings are cleared, including things
2106  * like descriptor fetch/write-back mode.
2107  */
2108 void ice_clear_pxe_mode(struct ice_hw *hw)
2109 {
2110         if (ice_check_sq_alive(hw, &hw->adminq))
2111                 ice_aq_clear_pxe_mode(hw);
2112 }
2113
2114 /**
2115  * ice_get_link_speed_based_on_phy_type - returns link speed
2116  * @phy_type_low: lower part of phy_type
2117  * @phy_type_high: higher part of phy_type
2118  *
2119  * This helper function will convert an entry in PHY type structure
2120  * [phy_type_low, phy_type_high] to its corresponding link speed.
2121  * Note: In the structure of [phy_type_low, phy_type_high], there should
2122  * be one bit set, as this function will convert one PHY type to its
2123  * speed.
2124  * If no bit gets set, ICE_LINK_SPEED_UNKNOWN will be returned
2125  * If more than one bit gets set, ICE_LINK_SPEED_UNKNOWN will be returned
2126  */
2127 static u16
2128 ice_get_link_speed_based_on_phy_type(u64 phy_type_low, u64 phy_type_high)
2129 {
2130         u16 speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
2131         u16 speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
2132
2133         switch (phy_type_low) {
2134         case ICE_PHY_TYPE_LOW_100BASE_TX:
2135         case ICE_PHY_TYPE_LOW_100M_SGMII:
2136                 speed_phy_type_low = ICE_AQ_LINK_SPEED_100MB;
2137                 break;
2138         case ICE_PHY_TYPE_LOW_1000BASE_T:
2139         case ICE_PHY_TYPE_LOW_1000BASE_SX:
2140         case ICE_PHY_TYPE_LOW_1000BASE_LX:
2141         case ICE_PHY_TYPE_LOW_1000BASE_KX:
2142         case ICE_PHY_TYPE_LOW_1G_SGMII:
2143                 speed_phy_type_low = ICE_AQ_LINK_SPEED_1000MB;
2144                 break;
2145         case ICE_PHY_TYPE_LOW_2500BASE_T:
2146         case ICE_PHY_TYPE_LOW_2500BASE_X:
2147         case ICE_PHY_TYPE_LOW_2500BASE_KX:
2148                 speed_phy_type_low = ICE_AQ_LINK_SPEED_2500MB;
2149                 break;
2150         case ICE_PHY_TYPE_LOW_5GBASE_T:
2151         case ICE_PHY_TYPE_LOW_5GBASE_KR:
2152                 speed_phy_type_low = ICE_AQ_LINK_SPEED_5GB;
2153                 break;
2154         case ICE_PHY_TYPE_LOW_10GBASE_T:
2155         case ICE_PHY_TYPE_LOW_10G_SFI_DA:
2156         case ICE_PHY_TYPE_LOW_10GBASE_SR:
2157         case ICE_PHY_TYPE_LOW_10GBASE_LR:
2158         case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
2159         case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
2160         case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
2161                 speed_phy_type_low = ICE_AQ_LINK_SPEED_10GB;
2162                 break;
2163         case ICE_PHY_TYPE_LOW_25GBASE_T:
2164         case ICE_PHY_TYPE_LOW_25GBASE_CR:
2165         case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
2166         case ICE_PHY_TYPE_LOW_25GBASE_CR1:
2167         case ICE_PHY_TYPE_LOW_25GBASE_SR:
2168         case ICE_PHY_TYPE_LOW_25GBASE_LR:
2169         case ICE_PHY_TYPE_LOW_25GBASE_KR:
2170         case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
2171         case ICE_PHY_TYPE_LOW_25GBASE_KR1:
2172         case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
2173         case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
2174                 speed_phy_type_low = ICE_AQ_LINK_SPEED_25GB;
2175                 break;
2176         case ICE_PHY_TYPE_LOW_40GBASE_CR4:
2177         case ICE_PHY_TYPE_LOW_40GBASE_SR4:
2178         case ICE_PHY_TYPE_LOW_40GBASE_LR4:
2179         case ICE_PHY_TYPE_LOW_40GBASE_KR4:
2180         case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
2181         case ICE_PHY_TYPE_LOW_40G_XLAUI:
2182                 speed_phy_type_low = ICE_AQ_LINK_SPEED_40GB;
2183                 break;
2184         case ICE_PHY_TYPE_LOW_50GBASE_CR2:
2185         case ICE_PHY_TYPE_LOW_50GBASE_SR2:
2186         case ICE_PHY_TYPE_LOW_50GBASE_LR2:
2187         case ICE_PHY_TYPE_LOW_50GBASE_KR2:
2188         case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
2189         case ICE_PHY_TYPE_LOW_50G_LAUI2:
2190         case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
2191         case ICE_PHY_TYPE_LOW_50G_AUI2:
2192         case ICE_PHY_TYPE_LOW_50GBASE_CP:
2193         case ICE_PHY_TYPE_LOW_50GBASE_SR:
2194         case ICE_PHY_TYPE_LOW_50GBASE_FR:
2195         case ICE_PHY_TYPE_LOW_50GBASE_LR:
2196         case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
2197         case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
2198         case ICE_PHY_TYPE_LOW_50G_AUI1:
2199                 speed_phy_type_low = ICE_AQ_LINK_SPEED_50GB;
2200                 break;
2201         case ICE_PHY_TYPE_LOW_100GBASE_CR4:
2202         case ICE_PHY_TYPE_LOW_100GBASE_SR4:
2203         case ICE_PHY_TYPE_LOW_100GBASE_LR4:
2204         case ICE_PHY_TYPE_LOW_100GBASE_KR4:
2205         case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
2206         case ICE_PHY_TYPE_LOW_100G_CAUI4:
2207         case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
2208         case ICE_PHY_TYPE_LOW_100G_AUI4:
2209         case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
2210         case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
2211         case ICE_PHY_TYPE_LOW_100GBASE_CP2:
2212         case ICE_PHY_TYPE_LOW_100GBASE_SR2:
2213         case ICE_PHY_TYPE_LOW_100GBASE_DR:
2214                 speed_phy_type_low = ICE_AQ_LINK_SPEED_100GB;
2215                 break;
2216         default:
2217                 speed_phy_type_low = ICE_AQ_LINK_SPEED_UNKNOWN;
2218                 break;
2219         }
2220
2221         switch (phy_type_high) {
2222         case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
2223         case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
2224         case ICE_PHY_TYPE_HIGH_100G_CAUI2:
2225         case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
2226         case ICE_PHY_TYPE_HIGH_100G_AUI2:
2227                 speed_phy_type_high = ICE_AQ_LINK_SPEED_100GB;
2228                 break;
2229         default:
2230                 speed_phy_type_high = ICE_AQ_LINK_SPEED_UNKNOWN;
2231                 break;
2232         }
2233
2234         if (speed_phy_type_low == ICE_AQ_LINK_SPEED_UNKNOWN &&
2235             speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
2236                 return ICE_AQ_LINK_SPEED_UNKNOWN;
2237         else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
2238                  speed_phy_type_high != ICE_AQ_LINK_SPEED_UNKNOWN)
2239                 return ICE_AQ_LINK_SPEED_UNKNOWN;
2240         else if (speed_phy_type_low != ICE_AQ_LINK_SPEED_UNKNOWN &&
2241                  speed_phy_type_high == ICE_AQ_LINK_SPEED_UNKNOWN)
2242                 return speed_phy_type_low;
2243         else
2244                 return speed_phy_type_high;
2245 }
2246
2247 /**
2248  * ice_update_phy_type
2249  * @phy_type_low: pointer to the lower part of phy_type
2250  * @phy_type_high: pointer to the higher part of phy_type
2251  * @link_speeds_bitmap: targeted link speeds bitmap
2252  *
2253  * Note: For the link_speeds_bitmap structure, you can check it at
2254  * [ice_aqc_get_link_status->link_speed]. Caller can pass in
2255  * link_speeds_bitmap include multiple speeds.
2256  *
2257  * Each entry in this [phy_type_low, phy_type_high] structure will
2258  * present a certain link speed. This helper function will turn on bits
2259  * in [phy_type_low, phy_type_high] structure based on the value of
2260  * link_speeds_bitmap input parameter.
2261  */
2262 void
2263 ice_update_phy_type(u64 *phy_type_low, u64 *phy_type_high,
2264                     u16 link_speeds_bitmap)
2265 {
2266         u64 pt_high;
2267         u64 pt_low;
2268         int index;
2269         u16 speed;
2270
2271         /* We first check with low part of phy_type */
2272         for (index = 0; index <= ICE_PHY_TYPE_LOW_MAX_INDEX; index++) {
2273                 pt_low = BIT_ULL(index);
2274                 speed = ice_get_link_speed_based_on_phy_type(pt_low, 0);
2275
2276                 if (link_speeds_bitmap & speed)
2277                         *phy_type_low |= BIT_ULL(index);
2278         }
2279
2280         /* We then check with high part of phy_type */
2281         for (index = 0; index <= ICE_PHY_TYPE_HIGH_MAX_INDEX; index++) {
2282                 pt_high = BIT_ULL(index);
2283                 speed = ice_get_link_speed_based_on_phy_type(0, pt_high);
2284
2285                 if (link_speeds_bitmap & speed)
2286                         *phy_type_high |= BIT_ULL(index);
2287         }
2288 }
2289
2290 /**
2291  * ice_aq_set_phy_cfg
2292  * @hw: pointer to the HW struct
2293  * @pi: port info structure of the interested logical port
2294  * @cfg: structure with PHY configuration data to be set
2295  * @cd: pointer to command details structure or NULL
2296  *
2297  * Set the various PHY configuration parameters supported on the Port.
2298  * One or more of the Set PHY config parameters may be ignored in an MFP
2299  * mode as the PF may not have the privilege to set some of the PHY Config
2300  * parameters. This status will be indicated by the command response (0x0601).
2301  */
2302 enum ice_status
2303 ice_aq_set_phy_cfg(struct ice_hw *hw, struct ice_port_info *pi,
2304                    struct ice_aqc_set_phy_cfg_data *cfg, struct ice_sq_cd *cd)
2305 {
2306         struct ice_aq_desc desc;
2307         enum ice_status status;
2308
2309         if (!cfg)
2310                 return ICE_ERR_PARAM;
2311
2312         /* Ensure that only valid bits of cfg->caps can be turned on. */
2313         if (cfg->caps & ~ICE_AQ_PHY_ENA_VALID_MASK) {
2314                 ice_debug(hw, ICE_DBG_PHY,
2315                           "Invalid bit is set in ice_aqc_set_phy_cfg_data->caps : 0x%x\n",
2316                           cfg->caps);
2317
2318                 cfg->caps &= ICE_AQ_PHY_ENA_VALID_MASK;
2319         }
2320
2321         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_phy_cfg);
2322         desc.params.set_phy.lport_num = pi->lport;
2323         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2324
2325         ice_debug(hw, ICE_DBG_LINK, "phy_type_low = 0x%llx\n",
2326                   (unsigned long long)LE64_TO_CPU(cfg->phy_type_low));
2327         ice_debug(hw, ICE_DBG_LINK, "phy_type_high = 0x%llx\n",
2328                   (unsigned long long)LE64_TO_CPU(cfg->phy_type_high));
2329         ice_debug(hw, ICE_DBG_LINK, "caps = 0x%x\n", cfg->caps);
2330         ice_debug(hw, ICE_DBG_LINK, "low_power_ctrl_an = 0x%x\n",
2331                   cfg->low_power_ctrl_an);
2332         ice_debug(hw, ICE_DBG_LINK, "eee_cap = 0x%x\n", cfg->eee_cap);
2333         ice_debug(hw, ICE_DBG_LINK, "eeer_value = 0x%x\n", cfg->eeer_value);
2334         ice_debug(hw, ICE_DBG_LINK, "link_fec_opt = 0x%x\n", cfg->link_fec_opt);
2335
2336         status = ice_aq_send_cmd(hw, &desc, cfg, sizeof(*cfg), cd);
2337
2338         if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
2339                 status = ICE_SUCCESS;
2340
2341         if (!status)
2342                 pi->phy.curr_user_phy_cfg = *cfg;
2343
2344         return status;
2345 }
2346
2347 /**
2348  * ice_update_link_info - update status of the HW network link
2349  * @pi: port info structure of the interested logical port
2350  */
2351 enum ice_status ice_update_link_info(struct ice_port_info *pi)
2352 {
2353         struct ice_link_status *li;
2354         enum ice_status status;
2355
2356         if (!pi)
2357                 return ICE_ERR_PARAM;
2358
2359         li = &pi->phy.link_info;
2360
2361         status = ice_aq_get_link_info(pi, true, NULL, NULL);
2362         if (status)
2363                 return status;
2364
2365         if (li->link_info & ICE_AQ_MEDIA_AVAILABLE) {
2366                 struct ice_aqc_get_phy_caps_data *pcaps;
2367                 struct ice_hw *hw;
2368
2369                 hw = pi->hw;
2370                 pcaps = (struct ice_aqc_get_phy_caps_data *)
2371                         ice_malloc(hw, sizeof(*pcaps));
2372                 if (!pcaps)
2373                         return ICE_ERR_NO_MEMORY;
2374
2375                 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
2376                                              pcaps, NULL);
2377                 if (status == ICE_SUCCESS)
2378                         ice_memcpy(li->module_type, &pcaps->module_type,
2379                                    sizeof(li->module_type),
2380                                    ICE_NONDMA_TO_NONDMA);
2381
2382                 ice_free(hw, pcaps);
2383         }
2384
2385         return status;
2386 }
2387
2388 /**
2389  * ice_cache_phy_user_req
2390  * @pi: port information structure
2391  * @cache_data: PHY logging data
2392  * @cache_mode: PHY logging mode
2393  *
2394  * Log the user request on (FC, FEC, SPEED) for later user.
2395  */
2396 static void
2397 ice_cache_phy_user_req(struct ice_port_info *pi,
2398                        struct ice_phy_cache_mode_data cache_data,
2399                        enum ice_phy_cache_mode cache_mode)
2400 {
2401         if (!pi)
2402                 return;
2403
2404         switch (cache_mode) {
2405         case ICE_FC_MODE:
2406                 pi->phy.curr_user_fc_req = cache_data.data.curr_user_fc_req;
2407                 break;
2408         case ICE_SPEED_MODE:
2409                 pi->phy.curr_user_speed_req =
2410                         cache_data.data.curr_user_speed_req;
2411                 break;
2412         case ICE_FEC_MODE:
2413                 pi->phy.curr_user_fec_req = cache_data.data.curr_user_fec_req;
2414                 break;
2415         default:
2416                 break;
2417         }
2418 }
2419
2420 /**
2421  * ice_caps_to_fc_mode
2422  * @caps: PHY capabilities
2423  *
2424  * Convert PHY FC capabilities to ice FC mode
2425  */
2426 enum ice_fc_mode ice_caps_to_fc_mode(u8 caps)
2427 {
2428         if (caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE &&
2429             caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2430                 return ICE_FC_FULL;
2431
2432         if (caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2433                 return ICE_FC_TX_PAUSE;
2434
2435         if (caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2436                 return ICE_FC_RX_PAUSE;
2437
2438         return ICE_FC_NONE;
2439 }
2440
2441 /**
2442  * ice_caps_to_fec_mode
2443  * @caps: PHY capabilities
2444  * @fec_options: Link FEC options
2445  *
2446  * Convert PHY FEC capabilities to ice FEC mode
2447  */
2448 enum ice_fec_mode ice_caps_to_fec_mode(u8 caps, u8 fec_options)
2449 {
2450         if (caps & ICE_AQC_PHY_EN_AUTO_FEC)
2451                 return ICE_FEC_AUTO;
2452
2453         if (fec_options & (ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN |
2454                            ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
2455                            ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN |
2456                            ICE_AQC_PHY_FEC_25G_KR_REQ))
2457                 return ICE_FEC_BASER;
2458
2459         if (fec_options & (ICE_AQC_PHY_FEC_25G_RS_528_REQ |
2460                            ICE_AQC_PHY_FEC_25G_RS_544_REQ |
2461                            ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN))
2462                 return ICE_FEC_RS;
2463
2464         return ICE_FEC_NONE;
2465 }
2466
2467 static enum ice_status
2468 ice_cfg_phy_fc(struct ice_port_info *pi, struct ice_aqc_set_phy_cfg_data *cfg,
2469                enum ice_fc_mode req_mode)
2470 {
2471         struct ice_aqc_get_phy_caps_data *pcaps = NULL;
2472         struct ice_phy_cache_mode_data cache_data;
2473         enum ice_status status = ICE_SUCCESS;
2474         u8 pause_mask = 0x0;
2475
2476         if (!pi || !cfg)
2477                 return ICE_ERR_BAD_PTR;
2478
2479         pcaps = (struct ice_aqc_get_phy_caps_data *)
2480                 ice_malloc(pi->hw, sizeof(*pcaps));
2481         if (!pcaps)
2482                 return ICE_ERR_NO_MEMORY;
2483
2484         /* Cache user FC request */
2485         cache_data.data.curr_user_fc_req = req_mode;
2486         ice_cache_phy_user_req(pi, cache_data, ICE_FC_MODE);
2487
2488         switch (req_mode) {
2489         case ICE_FC_AUTO:
2490                 /* Query the value of FC that both the NIC and attached media
2491                  * can do.
2492                  */
2493                 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
2494                                              pcaps, NULL);
2495                 if (status)
2496                         goto out;
2497
2498                 pause_mask |= pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE;
2499                 pause_mask |= pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE;
2500                 break;
2501         case ICE_FC_FULL:
2502                 pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
2503                 pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
2504                 break;
2505         case ICE_FC_RX_PAUSE:
2506                 pause_mask |= ICE_AQC_PHY_EN_RX_LINK_PAUSE;
2507                 break;
2508         case ICE_FC_TX_PAUSE:
2509                 pause_mask |= ICE_AQC_PHY_EN_TX_LINK_PAUSE;
2510                 break;
2511         default:
2512                 break;
2513         }
2514
2515         /* clear the old pause settings */
2516         cfg->caps &= ~(ICE_AQC_PHY_EN_TX_LINK_PAUSE |
2517                 ICE_AQC_PHY_EN_RX_LINK_PAUSE);
2518
2519         /* set the new capabilities */
2520         cfg->caps |= pause_mask;
2521
2522 out:
2523         ice_free(pi->hw, pcaps);
2524         return status;
2525 }
2526
2527 /**
2528  * ice_set_fc
2529  * @pi: port information structure
2530  * @aq_failures: pointer to status code, specific to ice_set_fc routine
2531  * @ena_auto_link_update: enable automatic link update
2532  *
2533  * Set the requested flow control mode.
2534  */
2535 enum ice_status
2536 ice_set_fc(struct ice_port_info *pi, u8 *aq_failures, bool ena_auto_link_update)
2537 {
2538         struct ice_aqc_set_phy_cfg_data  cfg = { 0 };
2539         struct ice_aqc_get_phy_caps_data *pcaps;
2540         enum ice_status status;
2541         struct ice_hw *hw;
2542
2543         if (!pi || !aq_failures)
2544                 return ICE_ERR_BAD_PTR;
2545
2546         hw = pi->hw;
2547
2548         pcaps = (struct ice_aqc_get_phy_caps_data *)
2549                 ice_malloc(hw, sizeof(*pcaps));
2550         if (!pcaps)
2551                 return ICE_ERR_NO_MEMORY;
2552
2553         /* Get the current PHY config */
2554         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2555                                      NULL);
2556         if (status) {
2557                 *aq_failures = ICE_SET_FC_AQ_FAIL_GET;
2558                 goto out;
2559         }
2560
2561         ice_copy_phy_caps_to_cfg(pi, pcaps, &cfg);
2562
2563         /* Configure the set phy data */
2564         status = ice_cfg_phy_fc(pi, &cfg, pi->fc.req_mode);
2565         if (status) {
2566                 if (status != ICE_ERR_BAD_PTR)
2567                         *aq_failures = ICE_SET_FC_AQ_FAIL_GET;
2568
2569                 goto out;
2570         }
2571
2572         /* If the capabilities have changed, then set the new config */
2573         if (cfg.caps != pcaps->caps) {
2574                 int retry_count, retry_max = 10;
2575
2576                 /* Auto restart link so settings take effect */
2577                 if (ena_auto_link_update)
2578                         cfg.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2579
2580                 status = ice_aq_set_phy_cfg(hw, pi, &cfg, NULL);
2581                 if (status) {
2582                         *aq_failures = ICE_SET_FC_AQ_FAIL_SET;
2583                         goto out;
2584                 }
2585
2586                 /* Update the link info
2587                  * It sometimes takes a really long time for link to
2588                  * come back from the atomic reset. Thus, we wait a
2589                  * little bit.
2590                  */
2591                 for (retry_count = 0; retry_count < retry_max; retry_count++) {
2592                         status = ice_update_link_info(pi);
2593
2594                         if (status == ICE_SUCCESS)
2595                                 break;
2596
2597                         ice_msec_delay(100, true);
2598                 }
2599
2600                 if (status)
2601                         *aq_failures = ICE_SET_FC_AQ_FAIL_UPDATE;
2602         }
2603
2604 out:
2605         ice_free(hw, pcaps);
2606         return status;
2607 }
2608
2609 /**
2610  * ice_phy_caps_equals_cfg
2611  * @phy_caps: PHY capabilities
2612  * @phy_cfg: PHY configuration
2613  *
2614  * Helper function to determine if PHY capabilities matches PHY
2615  * configuration
2616  */
2617 bool
2618 ice_phy_caps_equals_cfg(struct ice_aqc_get_phy_caps_data *phy_caps,
2619                         struct ice_aqc_set_phy_cfg_data *phy_cfg)
2620 {
2621         u8 caps_mask, cfg_mask;
2622
2623         if (!phy_caps || !phy_cfg)
2624                 return false;
2625
2626         /* These bits are not common between capabilities and configuration.
2627          * Do not use them to determine equality.
2628          */
2629         caps_mask = ICE_AQC_PHY_CAPS_MASK & ~(ICE_AQC_PHY_AN_MODE |
2630                                               ICE_AQC_PHY_EN_MOD_QUAL);
2631         cfg_mask = ICE_AQ_PHY_ENA_VALID_MASK & ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2632
2633         if (phy_caps->phy_type_low != phy_cfg->phy_type_low ||
2634             phy_caps->phy_type_high != phy_cfg->phy_type_high ||
2635             ((phy_caps->caps & caps_mask) != (phy_cfg->caps & cfg_mask)) ||
2636             phy_caps->low_power_ctrl_an != phy_cfg->low_power_ctrl_an ||
2637             phy_caps->eee_cap != phy_cfg->eee_cap ||
2638             phy_caps->eeer_value != phy_cfg->eeer_value ||
2639             phy_caps->link_fec_options != phy_cfg->link_fec_opt)
2640                 return false;
2641
2642         return true;
2643 }
2644
2645 /**
2646  * ice_copy_phy_caps_to_cfg - Copy PHY ability data to configuration data
2647  * @pi: port information structure
2648  * @caps: PHY ability structure to copy date from
2649  * @cfg: PHY configuration structure to copy data to
2650  *
2651  * Helper function to copy AQC PHY get ability data to PHY set configuration
2652  * data structure
2653  */
2654 void
2655 ice_copy_phy_caps_to_cfg(struct ice_port_info *pi,
2656                          struct ice_aqc_get_phy_caps_data *caps,
2657                          struct ice_aqc_set_phy_cfg_data *cfg)
2658 {
2659         if (!pi || !caps || !cfg)
2660                 return;
2661
2662         ice_memset(cfg, 0, sizeof(*cfg), ICE_NONDMA_MEM);
2663         cfg->phy_type_low = caps->phy_type_low;
2664         cfg->phy_type_high = caps->phy_type_high;
2665         cfg->caps = caps->caps;
2666         cfg->low_power_ctrl_an = caps->low_power_ctrl_an;
2667         cfg->eee_cap = caps->eee_cap;
2668         cfg->eeer_value = caps->eeer_value;
2669         cfg->link_fec_opt = caps->link_fec_options;
2670         cfg->module_compliance_enforcement =
2671                 caps->module_compliance_enforcement;
2672
2673         if (ice_fw_supports_link_override(pi->hw)) {
2674                 struct ice_link_default_override_tlv tlv;
2675
2676                 if (ice_get_link_default_override(&tlv, pi))
2677                         return;
2678
2679                 if (tlv.options & ICE_LINK_OVERRIDE_STRICT_MODE)
2680                         cfg->module_compliance_enforcement |=
2681                                 ICE_LINK_OVERRIDE_STRICT_MODE;
2682         }
2683 }
2684
2685 /**
2686  * ice_cfg_phy_fec - Configure PHY FEC data based on FEC mode
2687  * @pi: port information structure
2688  * @cfg: PHY configuration data to set FEC mode
2689  * @fec: FEC mode to configure
2690  */
2691 enum ice_status
2692 ice_cfg_phy_fec(struct ice_port_info *pi, struct ice_aqc_set_phy_cfg_data *cfg,
2693                 enum ice_fec_mode fec)
2694 {
2695         struct ice_aqc_get_phy_caps_data *pcaps;
2696         enum ice_status status = ICE_SUCCESS;
2697         struct ice_hw *hw;
2698
2699         if (!pi || !cfg)
2700                 return ICE_ERR_BAD_PTR;
2701
2702         hw = pi->hw;
2703
2704         pcaps = (struct ice_aqc_get_phy_caps_data *)
2705                 ice_malloc(hw, sizeof(*pcaps));
2706         if (!pcaps)
2707                 return ICE_ERR_NO_MEMORY;
2708
2709         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
2710                                      NULL);
2711         if (status)
2712                 goto out;
2713
2714         switch (fec) {
2715         case ICE_FEC_BASER:
2716                 /* Clear RS bits, and AND BASE-R ability
2717                  * bits and OR request bits.
2718                  */
2719                 cfg->link_fec_opt &= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN |
2720                                      ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN;
2721                 cfg->link_fec_opt |= ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ |
2722                                      ICE_AQC_PHY_FEC_25G_KR_REQ;
2723                 break;
2724         case ICE_FEC_RS:
2725                 /* Clear BASE-R bits, and AND RS ability
2726                  * bits and OR request bits.
2727                  */
2728                 cfg->link_fec_opt &= ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN;
2729                 cfg->link_fec_opt |= ICE_AQC_PHY_FEC_25G_RS_528_REQ |
2730                                      ICE_AQC_PHY_FEC_25G_RS_544_REQ;
2731                 break;
2732         case ICE_FEC_NONE:
2733                 /* Clear all FEC option bits. */
2734                 cfg->link_fec_opt &= ~ICE_AQC_PHY_FEC_MASK;
2735                 break;
2736         case ICE_FEC_AUTO:
2737                 /* AND auto FEC bit, and all caps bits. */
2738                 cfg->caps &= ICE_AQC_PHY_CAPS_MASK;
2739                 cfg->link_fec_opt |= pcaps->link_fec_options;
2740                 break;
2741         default:
2742                 status = ICE_ERR_PARAM;
2743                 break;
2744         }
2745
2746         if (fec == ICE_FEC_AUTO && ice_fw_supports_link_override(pi->hw)) {
2747                 struct ice_link_default_override_tlv tlv;
2748
2749                 if (ice_get_link_default_override(&tlv, pi))
2750                         goto out;
2751
2752                 if (!(tlv.options & ICE_LINK_OVERRIDE_STRICT_MODE) &&
2753                     (tlv.options & ICE_LINK_OVERRIDE_EN))
2754                         cfg->link_fec_opt = tlv.fec_options;
2755         }
2756
2757 out:
2758         ice_free(hw, pcaps);
2759
2760         return status;
2761 }
2762
2763 /**
2764  * ice_get_link_status - get status of the HW network link
2765  * @pi: port information structure
2766  * @link_up: pointer to bool (true/false = linkup/linkdown)
2767  *
2768  * Variable link_up is true if link is up, false if link is down.
2769  * The variable link_up is invalid if status is non zero. As a
2770  * result of this call, link status reporting becomes enabled
2771  */
2772 enum ice_status ice_get_link_status(struct ice_port_info *pi, bool *link_up)
2773 {
2774         struct ice_phy_info *phy_info;
2775         enum ice_status status = ICE_SUCCESS;
2776
2777         if (!pi || !link_up)
2778                 return ICE_ERR_PARAM;
2779
2780         phy_info = &pi->phy;
2781
2782         if (phy_info->get_link_info) {
2783                 status = ice_update_link_info(pi);
2784
2785                 if (status)
2786                         ice_debug(pi->hw, ICE_DBG_LINK,
2787                                   "get link status error, status = %d\n",
2788                                   status);
2789         }
2790
2791         *link_up = phy_info->link_info.link_info & ICE_AQ_LINK_UP;
2792
2793         return status;
2794 }
2795
2796 /**
2797  * ice_aq_set_link_restart_an
2798  * @pi: pointer to the port information structure
2799  * @ena_link: if true: enable link, if false: disable link
2800  * @cd: pointer to command details structure or NULL
2801  *
2802  * Sets up the link and restarts the Auto-Negotiation over the link.
2803  */
2804 enum ice_status
2805 ice_aq_set_link_restart_an(struct ice_port_info *pi, bool ena_link,
2806                            struct ice_sq_cd *cd)
2807 {
2808         struct ice_aqc_restart_an *cmd;
2809         struct ice_aq_desc desc;
2810
2811         cmd = &desc.params.restart_an;
2812
2813         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_restart_an);
2814
2815         cmd->cmd_flags = ICE_AQC_RESTART_AN_LINK_RESTART;
2816         cmd->lport_num = pi->lport;
2817         if (ena_link)
2818                 cmd->cmd_flags |= ICE_AQC_RESTART_AN_LINK_ENABLE;
2819         else
2820                 cmd->cmd_flags &= ~ICE_AQC_RESTART_AN_LINK_ENABLE;
2821
2822         return ice_aq_send_cmd(pi->hw, &desc, NULL, 0, cd);
2823 }
2824
2825 /**
2826  * ice_aq_set_event_mask
2827  * @hw: pointer to the HW struct
2828  * @port_num: port number of the physical function
2829  * @mask: event mask to be set
2830  * @cd: pointer to command details structure or NULL
2831  *
2832  * Set event mask (0x0613)
2833  */
2834 enum ice_status
2835 ice_aq_set_event_mask(struct ice_hw *hw, u8 port_num, u16 mask,
2836                       struct ice_sq_cd *cd)
2837 {
2838         struct ice_aqc_set_event_mask *cmd;
2839         struct ice_aq_desc desc;
2840
2841         cmd = &desc.params.set_event_mask;
2842
2843         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_event_mask);
2844
2845         cmd->lport_num = port_num;
2846
2847         cmd->event_mask = CPU_TO_LE16(mask);
2848         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2849 }
2850
2851 /**
2852  * ice_aq_set_mac_loopback
2853  * @hw: pointer to the HW struct
2854  * @ena_lpbk: Enable or Disable loopback
2855  * @cd: pointer to command details structure or NULL
2856  *
2857  * Enable/disable loopback on a given port
2858  */
2859 enum ice_status
2860 ice_aq_set_mac_loopback(struct ice_hw *hw, bool ena_lpbk, struct ice_sq_cd *cd)
2861 {
2862         struct ice_aqc_set_mac_lb *cmd;
2863         struct ice_aq_desc desc;
2864
2865         cmd = &desc.params.set_mac_lb;
2866
2867         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_mac_lb);
2868         if (ena_lpbk)
2869                 cmd->lb_mode = ICE_AQ_MAC_LB_EN;
2870
2871         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2872 }
2873
2874 /**
2875  * ice_aq_set_port_id_led
2876  * @pi: pointer to the port information
2877  * @is_orig_mode: is this LED set to original mode (by the net-list)
2878  * @cd: pointer to command details structure or NULL
2879  *
2880  * Set LED value for the given port (0x06e9)
2881  */
2882 enum ice_status
2883 ice_aq_set_port_id_led(struct ice_port_info *pi, bool is_orig_mode,
2884                        struct ice_sq_cd *cd)
2885 {
2886         struct ice_aqc_set_port_id_led *cmd;
2887         struct ice_hw *hw = pi->hw;
2888         struct ice_aq_desc desc;
2889
2890         cmd = &desc.params.set_port_id_led;
2891
2892         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_id_led);
2893
2894         if (is_orig_mode)
2895                 cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_ORIG;
2896         else
2897                 cmd->ident_mode = ICE_AQC_PORT_IDENT_LED_BLINK;
2898
2899         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2900 }
2901
2902 /**
2903  * ice_aq_sff_eeprom
2904  * @hw: pointer to the HW struct
2905  * @lport: bits [7:0] = logical port, bit [8] = logical port valid
2906  * @bus_addr: I2C bus address of the eeprom (typically 0xA0, 0=topo default)
2907  * @mem_addr: I2C offset. lower 8 bits for address, 8 upper bits zero padding.
2908  * @page: QSFP page
2909  * @set_page: set or ignore the page
2910  * @data: pointer to data buffer to be read/written to the I2C device.
2911  * @length: 1-16 for read, 1 for write.
2912  * @write: 0 read, 1 for write.
2913  * @cd: pointer to command details structure or NULL
2914  *
2915  * Read/Write SFF EEPROM (0x06EE)
2916  */
2917 enum ice_status
2918 ice_aq_sff_eeprom(struct ice_hw *hw, u16 lport, u8 bus_addr,
2919                   u16 mem_addr, u8 page, u8 set_page, u8 *data, u8 length,
2920                   bool write, struct ice_sq_cd *cd)
2921 {
2922         struct ice_aqc_sff_eeprom *cmd;
2923         struct ice_aq_desc desc;
2924         enum ice_status status;
2925
2926         if (!data || (mem_addr & 0xff00))
2927                 return ICE_ERR_PARAM;
2928
2929         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_sff_eeprom);
2930         cmd = &desc.params.read_write_sff_param;
2931         desc.flags = CPU_TO_LE16(ICE_AQ_FLAG_RD | ICE_AQ_FLAG_BUF);
2932         cmd->lport_num = (u8)(lport & 0xff);
2933         cmd->lport_num_valid = (u8)((lport >> 8) & 0x01);
2934         cmd->i2c_bus_addr = CPU_TO_LE16(((bus_addr >> 1) &
2935                                          ICE_AQC_SFF_I2CBUS_7BIT_M) |
2936                                         ((set_page <<
2937                                           ICE_AQC_SFF_SET_EEPROM_PAGE_S) &
2938                                          ICE_AQC_SFF_SET_EEPROM_PAGE_M));
2939         cmd->i2c_mem_addr = CPU_TO_LE16(mem_addr & 0xff);
2940         cmd->eeprom_page = CPU_TO_LE16((u16)page << ICE_AQC_SFF_EEPROM_PAGE_S);
2941         if (write)
2942                 cmd->i2c_bus_addr |= CPU_TO_LE16(ICE_AQC_SFF_IS_WRITE);
2943
2944         status = ice_aq_send_cmd(hw, &desc, data, length, cd);
2945         return status;
2946 }
2947
2948 /**
2949  * __ice_aq_get_set_rss_lut
2950  * @hw: pointer to the hardware structure
2951  * @vsi_id: VSI FW index
2952  * @lut_type: LUT table type
2953  * @lut: pointer to the LUT buffer provided by the caller
2954  * @lut_size: size of the LUT buffer
2955  * @glob_lut_idx: global LUT index
2956  * @set: set true to set the table, false to get the table
2957  *
2958  * Internal function to get (0x0B05) or set (0x0B03) RSS look up table
2959  */
2960 static enum ice_status
2961 __ice_aq_get_set_rss_lut(struct ice_hw *hw, u16 vsi_id, u8 lut_type, u8 *lut,
2962                          u16 lut_size, u8 glob_lut_idx, bool set)
2963 {
2964         struct ice_aqc_get_set_rss_lut *cmd_resp;
2965         struct ice_aq_desc desc;
2966         enum ice_status status;
2967         u16 flags = 0;
2968
2969         cmd_resp = &desc.params.get_set_rss_lut;
2970
2971         if (set) {
2972                 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_rss_lut);
2973                 desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2974         } else {
2975                 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_rss_lut);
2976         }
2977
2978         cmd_resp->vsi_id = CPU_TO_LE16(((vsi_id <<
2979                                          ICE_AQC_GSET_RSS_LUT_VSI_ID_S) &
2980                                         ICE_AQC_GSET_RSS_LUT_VSI_ID_M) |
2981                                        ICE_AQC_GSET_RSS_LUT_VSI_VALID);
2982
2983         switch (lut_type) {
2984         case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI:
2985         case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF:
2986         case ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_GLOBAL:
2987                 flags |= ((lut_type << ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_S) &
2988                           ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_M);
2989                 break;
2990         default:
2991                 status = ICE_ERR_PARAM;
2992                 goto ice_aq_get_set_rss_lut_exit;
2993         }
2994
2995         if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_GLOBAL) {
2996                 flags |= ((glob_lut_idx << ICE_AQC_GSET_RSS_LUT_GLOBAL_IDX_S) &
2997                           ICE_AQC_GSET_RSS_LUT_GLOBAL_IDX_M);
2998
2999                 if (!set)
3000                         goto ice_aq_get_set_rss_lut_send;
3001         } else if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF) {
3002                 if (!set)
3003                         goto ice_aq_get_set_rss_lut_send;
3004         } else {
3005                 goto ice_aq_get_set_rss_lut_send;
3006         }
3007
3008         /* LUT size is only valid for Global and PF table types */
3009         switch (lut_size) {
3010         case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_128:
3011                 flags |= (ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_128_FLAG <<
3012                           ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_S) &
3013                          ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_M;
3014                 break;
3015         case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_512:
3016                 flags |= (ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_512_FLAG <<
3017                           ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_S) &
3018                          ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_M;
3019                 break;
3020         case ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_2K:
3021                 if (lut_type == ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF) {
3022                         flags |= (ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_2K_FLAG <<
3023                                   ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_S) &
3024                                  ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_M;
3025                         break;
3026                 }
3027                 /* fall-through */
3028         default:
3029                 status = ICE_ERR_PARAM;
3030                 goto ice_aq_get_set_rss_lut_exit;
3031         }
3032
3033 ice_aq_get_set_rss_lut_send:
3034         cmd_resp->flags = CPU_TO_LE16(flags);
3035         status = ice_aq_send_cmd(hw, &desc, lut, lut_size, NULL);
3036
3037 ice_aq_get_set_rss_lut_exit:
3038         return status;
3039 }
3040
3041 /**
3042  * ice_aq_get_rss_lut
3043  * @hw: pointer to the hardware structure
3044  * @vsi_handle: software VSI handle
3045  * @lut_type: LUT table type
3046  * @lut: pointer to the LUT buffer provided by the caller
3047  * @lut_size: size of the LUT buffer
3048  *
3049  * get the RSS lookup table, PF or VSI type
3050  */
3051 enum ice_status
3052 ice_aq_get_rss_lut(struct ice_hw *hw, u16 vsi_handle, u8 lut_type,
3053                    u8 *lut, u16 lut_size)
3054 {
3055         if (!ice_is_vsi_valid(hw, vsi_handle) || !lut)
3056                 return ICE_ERR_PARAM;
3057
3058         return __ice_aq_get_set_rss_lut(hw, ice_get_hw_vsi_num(hw, vsi_handle),
3059                                         lut_type, lut, lut_size, 0, false);
3060 }
3061
3062 /**
3063  * ice_aq_set_rss_lut
3064  * @hw: pointer to the hardware structure
3065  * @vsi_handle: software VSI handle
3066  * @lut_type: LUT table type
3067  * @lut: pointer to the LUT buffer provided by the caller
3068  * @lut_size: size of the LUT buffer
3069  *
3070  * set the RSS lookup table, PF or VSI type
3071  */
3072 enum ice_status
3073 ice_aq_set_rss_lut(struct ice_hw *hw, u16 vsi_handle, u8 lut_type,
3074                    u8 *lut, u16 lut_size)
3075 {
3076         if (!ice_is_vsi_valid(hw, vsi_handle) || !lut)
3077                 return ICE_ERR_PARAM;
3078
3079         return __ice_aq_get_set_rss_lut(hw, ice_get_hw_vsi_num(hw, vsi_handle),
3080                                         lut_type, lut, lut_size, 0, true);
3081 }
3082
3083 /**
3084  * __ice_aq_get_set_rss_key
3085  * @hw: pointer to the HW struct
3086  * @vsi_id: VSI FW index
3087  * @key: pointer to key info struct
3088  * @set: set true to set the key, false to get the key
3089  *
3090  * get (0x0B04) or set (0x0B02) the RSS key per VSI
3091  */
3092 static enum
3093 ice_status __ice_aq_get_set_rss_key(struct ice_hw *hw, u16 vsi_id,
3094                                     struct ice_aqc_get_set_rss_keys *key,
3095                                     bool set)
3096 {
3097         struct ice_aqc_get_set_rss_key *cmd_resp;
3098         u16 key_size = sizeof(*key);
3099         struct ice_aq_desc desc;
3100
3101         cmd_resp = &desc.params.get_set_rss_key;
3102
3103         if (set) {
3104                 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_rss_key);
3105                 desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
3106         } else {
3107                 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_rss_key);
3108         }
3109
3110         cmd_resp->vsi_id = CPU_TO_LE16(((vsi_id <<
3111                                          ICE_AQC_GSET_RSS_KEY_VSI_ID_S) &
3112                                         ICE_AQC_GSET_RSS_KEY_VSI_ID_M) |
3113                                        ICE_AQC_GSET_RSS_KEY_VSI_VALID);
3114
3115         return ice_aq_send_cmd(hw, &desc, key, key_size, NULL);
3116 }
3117
3118 /**
3119  * ice_aq_get_rss_key
3120  * @hw: pointer to the HW struct
3121  * @vsi_handle: software VSI handle
3122  * @key: pointer to key info struct
3123  *
3124  * get the RSS key per VSI
3125  */
3126 enum ice_status
3127 ice_aq_get_rss_key(struct ice_hw *hw, u16 vsi_handle,
3128                    struct ice_aqc_get_set_rss_keys *key)
3129 {
3130         if (!ice_is_vsi_valid(hw, vsi_handle) || !key)
3131                 return ICE_ERR_PARAM;
3132
3133         return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
3134                                         key, false);
3135 }
3136
3137 /**
3138  * ice_aq_set_rss_key
3139  * @hw: pointer to the HW struct
3140  * @vsi_handle: software VSI handle
3141  * @keys: pointer to key info struct
3142  *
3143  * set the RSS key per VSI
3144  */
3145 enum ice_status
3146 ice_aq_set_rss_key(struct ice_hw *hw, u16 vsi_handle,
3147                    struct ice_aqc_get_set_rss_keys *keys)
3148 {
3149         if (!ice_is_vsi_valid(hw, vsi_handle) || !keys)
3150                 return ICE_ERR_PARAM;
3151
3152         return __ice_aq_get_set_rss_key(hw, ice_get_hw_vsi_num(hw, vsi_handle),
3153                                         keys, true);
3154 }
3155
3156 /**
3157  * ice_aq_add_lan_txq
3158  * @hw: pointer to the hardware structure
3159  * @num_qgrps: Number of added queue groups
3160  * @qg_list: list of queue groups to be added
3161  * @buf_size: size of buffer for indirect command
3162  * @cd: pointer to command details structure or NULL
3163  *
3164  * Add Tx LAN queue (0x0C30)
3165  *
3166  * NOTE:
3167  * Prior to calling add Tx LAN queue:
3168  * Initialize the following as part of the Tx queue context:
3169  * Completion queue ID if the queue uses Completion queue, Quanta profile,
3170  * Cache profile and Packet shaper profile.
3171  *
3172  * After add Tx LAN queue AQ command is completed:
3173  * Interrupts should be associated with specific queues,
3174  * Association of Tx queue to Doorbell queue is not part of Add LAN Tx queue
3175  * flow.
3176  */
3177 enum ice_status
3178 ice_aq_add_lan_txq(struct ice_hw *hw, u8 num_qgrps,
3179                    struct ice_aqc_add_tx_qgrp *qg_list, u16 buf_size,
3180                    struct ice_sq_cd *cd)
3181 {
3182         u16 i, sum_header_size, sum_q_size = 0;
3183         struct ice_aqc_add_tx_qgrp *list;
3184         struct ice_aqc_add_txqs *cmd;
3185         struct ice_aq_desc desc;
3186
3187         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3188
3189         cmd = &desc.params.add_txqs;
3190
3191         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_txqs);
3192
3193         if (!qg_list)
3194                 return ICE_ERR_PARAM;
3195
3196         if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
3197                 return ICE_ERR_PARAM;
3198
3199         sum_header_size = num_qgrps *
3200                 (sizeof(*qg_list) - sizeof(*qg_list->txqs));
3201
3202         list = qg_list;
3203         for (i = 0; i < num_qgrps; i++) {
3204                 struct ice_aqc_add_txqs_perq *q = list->txqs;
3205
3206                 sum_q_size += list->num_txqs * sizeof(*q);
3207                 list = (struct ice_aqc_add_tx_qgrp *)(q + list->num_txqs);
3208         }
3209
3210         if (buf_size != (sum_header_size + sum_q_size))
3211                 return ICE_ERR_PARAM;
3212
3213         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
3214
3215         cmd->num_qgrps = num_qgrps;
3216
3217         return ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
3218 }
3219
3220 /**
3221  * ice_aq_dis_lan_txq
3222  * @hw: pointer to the hardware structure
3223  * @num_qgrps: number of groups in the list
3224  * @qg_list: the list of groups to disable
3225  * @buf_size: the total size of the qg_list buffer in bytes
3226  * @rst_src: if called due to reset, specifies the reset source
3227  * @vmvf_num: the relative VM or VF number that is undergoing the reset
3228  * @cd: pointer to command details structure or NULL
3229  *
3230  * Disable LAN Tx queue (0x0C31)
3231  */
3232 static enum ice_status
3233 ice_aq_dis_lan_txq(struct ice_hw *hw, u8 num_qgrps,
3234                    struct ice_aqc_dis_txq_item *qg_list, u16 buf_size,
3235                    enum ice_disq_rst_src rst_src, u16 vmvf_num,
3236                    struct ice_sq_cd *cd)
3237 {
3238         struct ice_aqc_dis_txqs *cmd;
3239         struct ice_aq_desc desc;
3240         enum ice_status status;
3241         u16 i, sz = 0;
3242
3243         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3244         cmd = &desc.params.dis_txqs;
3245         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_dis_txqs);
3246
3247         /* qg_list can be NULL only in VM/VF reset flow */
3248         if (!qg_list && !rst_src)
3249                 return ICE_ERR_PARAM;
3250
3251         if (num_qgrps > ICE_LAN_TXQ_MAX_QGRPS)
3252                 return ICE_ERR_PARAM;
3253
3254         cmd->num_entries = num_qgrps;
3255
3256         cmd->vmvf_and_timeout = CPU_TO_LE16((5 << ICE_AQC_Q_DIS_TIMEOUT_S) &
3257                                             ICE_AQC_Q_DIS_TIMEOUT_M);
3258
3259         switch (rst_src) {
3260         case ICE_VM_RESET:
3261                 cmd->cmd_type = ICE_AQC_Q_DIS_CMD_VM_RESET;
3262                 cmd->vmvf_and_timeout |=
3263                         CPU_TO_LE16(vmvf_num & ICE_AQC_Q_DIS_VMVF_NUM_M);
3264                 break;
3265         case ICE_NO_RESET:
3266         default:
3267                 break;
3268         }
3269
3270         /* flush pipe on time out */
3271         cmd->cmd_type |= ICE_AQC_Q_DIS_CMD_FLUSH_PIPE;
3272         /* If no queue group info, we are in a reset flow. Issue the AQ */
3273         if (!qg_list)
3274                 goto do_aq;
3275
3276         /* set RD bit to indicate that command buffer is provided by the driver
3277          * and it needs to be read by the firmware
3278          */
3279         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
3280
3281         for (i = 0; i < num_qgrps; ++i) {
3282                 /* Calculate the size taken up by the queue IDs in this group */
3283                 sz += qg_list[i].num_qs * sizeof(qg_list[i].q_id);
3284
3285                 /* Add the size of the group header */
3286                 sz += sizeof(qg_list[i]) - sizeof(qg_list[i].q_id);
3287
3288                 /* If the num of queues is even, add 2 bytes of padding */
3289                 if ((qg_list[i].num_qs % 2) == 0)
3290                         sz += 2;
3291         }
3292
3293         if (buf_size != sz)
3294                 return ICE_ERR_PARAM;
3295
3296 do_aq:
3297         status = ice_aq_send_cmd(hw, &desc, qg_list, buf_size, cd);
3298         if (status) {
3299                 if (!qg_list)
3300                         ice_debug(hw, ICE_DBG_SCHED, "VM%d disable failed %d\n",
3301                                   vmvf_num, hw->adminq.sq_last_status);
3302                 else
3303                         ice_debug(hw, ICE_DBG_SCHED, "disable queue %d failed %d\n",
3304                                   LE16_TO_CPU(qg_list[0].q_id[0]),
3305                                   hw->adminq.sq_last_status);
3306         }
3307         return status;
3308 }
3309
3310 /**
3311  * ice_aq_move_recfg_lan_txq
3312  * @hw: pointer to the hardware structure
3313  * @num_qs: number of queues to move/reconfigure
3314  * @is_move: true if this operation involves node movement
3315  * @is_tc_change: true if this operation involves a TC change
3316  * @subseq_call: true if this operation is a subsequent call
3317  * @flush_pipe: on timeout, true to flush pipe, false to return EAGAIN
3318  * @timeout: timeout in units of 100 usec (valid values 0-50)
3319  * @blocked_cgds: out param, bitmap of CGDs that timed out if returning EAGAIN
3320  * @buf: struct containing src/dest TEID and per-queue info
3321  * @buf_size: size of buffer for indirect command
3322  * @txqs_moved: out param, number of queues successfully moved
3323  * @cd: pointer to command details structure or NULL
3324  *
3325  * Move / Reconfigure Tx LAN queues (0x0C32)
3326  */
3327 enum ice_status
3328 ice_aq_move_recfg_lan_txq(struct ice_hw *hw, u8 num_qs, bool is_move,
3329                           bool is_tc_change, bool subseq_call, bool flush_pipe,
3330                           u8 timeout, u32 *blocked_cgds,
3331                           struct ice_aqc_move_txqs_data *buf, u16 buf_size,
3332                           u8 *txqs_moved, struct ice_sq_cd *cd)
3333 {
3334         struct ice_aqc_move_txqs *cmd;
3335         struct ice_aq_desc desc;
3336         enum ice_status status;
3337
3338         cmd = &desc.params.move_txqs;
3339         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_move_recfg_txqs);
3340
3341 #define ICE_LAN_TXQ_MOVE_TIMEOUT_MAX 50
3342         if (timeout > ICE_LAN_TXQ_MOVE_TIMEOUT_MAX)
3343                 return ICE_ERR_PARAM;
3344
3345         if (is_tc_change && !flush_pipe && !blocked_cgds)
3346                 return ICE_ERR_PARAM;
3347
3348         if (!is_move && !is_tc_change)
3349                 return ICE_ERR_PARAM;
3350
3351         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
3352
3353         if (is_move)
3354                 cmd->cmd_type |= ICE_AQC_Q_CMD_TYPE_MOVE;
3355
3356         if (is_tc_change)
3357                 cmd->cmd_type |= ICE_AQC_Q_CMD_TYPE_TC_CHANGE;
3358
3359         if (subseq_call)
3360                 cmd->cmd_type |= ICE_AQC_Q_CMD_SUBSEQ_CALL;
3361
3362         if (flush_pipe)
3363                 cmd->cmd_type |= ICE_AQC_Q_CMD_FLUSH_PIPE;
3364
3365         cmd->num_qs = num_qs;
3366         cmd->timeout = ((timeout << ICE_AQC_Q_CMD_TIMEOUT_S) &
3367                         ICE_AQC_Q_CMD_TIMEOUT_M);
3368
3369         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
3370
3371         if (!status && txqs_moved)
3372                 *txqs_moved = cmd->num_qs;
3373
3374         if (hw->adminq.sq_last_status == ICE_AQ_RC_EAGAIN &&
3375             is_tc_change && !flush_pipe)
3376                 *blocked_cgds = LE32_TO_CPU(cmd->blocked_cgds);
3377
3378         return status;
3379 }
3380
3381 /* End of FW Admin Queue command wrappers */
3382
3383 /**
3384  * ice_write_byte - write a byte to a packed context structure
3385  * @src_ctx:  the context structure to read from
3386  * @dest_ctx: the context to be written to
3387  * @ce_info:  a description of the struct to be filled
3388  */
3389 static void
3390 ice_write_byte(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3391 {
3392         u8 src_byte, dest_byte, mask;
3393         u8 *from, *dest;
3394         u16 shift_width;
3395
3396         /* copy from the next struct field */
3397         from = src_ctx + ce_info->offset;
3398
3399         /* prepare the bits and mask */
3400         shift_width = ce_info->lsb % 8;
3401         mask = (u8)(BIT(ce_info->width) - 1);
3402
3403         src_byte = *from;
3404         src_byte &= mask;
3405
3406         /* shift to correct alignment */
3407         mask <<= shift_width;
3408         src_byte <<= shift_width;
3409
3410         /* get the current bits from the target bit string */
3411         dest = dest_ctx + (ce_info->lsb / 8);
3412
3413         ice_memcpy(&dest_byte, dest, sizeof(dest_byte), ICE_DMA_TO_NONDMA);
3414
3415         dest_byte &= ~mask;     /* get the bits not changing */
3416         dest_byte |= src_byte;  /* add in the new bits */
3417
3418         /* put it all back */
3419         ice_memcpy(dest, &dest_byte, sizeof(dest_byte), ICE_NONDMA_TO_DMA);
3420 }
3421
3422 /**
3423  * ice_write_word - write a word to a packed context structure
3424  * @src_ctx:  the context structure to read from
3425  * @dest_ctx: the context to be written to
3426  * @ce_info:  a description of the struct to be filled
3427  */
3428 static void
3429 ice_write_word(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3430 {
3431         u16 src_word, mask;
3432         __le16 dest_word;
3433         u8 *from, *dest;
3434         u16 shift_width;
3435
3436         /* copy from the next struct field */
3437         from = src_ctx + ce_info->offset;
3438
3439         /* prepare the bits and mask */
3440         shift_width = ce_info->lsb % 8;
3441         mask = BIT(ce_info->width) - 1;
3442
3443         /* don't swizzle the bits until after the mask because the mask bits
3444          * will be in a different bit position on big endian machines
3445          */
3446         src_word = *(u16 *)from;
3447         src_word &= mask;
3448
3449         /* shift to correct alignment */
3450         mask <<= shift_width;
3451         src_word <<= shift_width;
3452
3453         /* get the current bits from the target bit string */
3454         dest = dest_ctx + (ce_info->lsb / 8);
3455
3456         ice_memcpy(&dest_word, dest, sizeof(dest_word), ICE_DMA_TO_NONDMA);
3457
3458         dest_word &= ~(CPU_TO_LE16(mask));      /* get the bits not changing */
3459         dest_word |= CPU_TO_LE16(src_word);     /* add in the new bits */
3460
3461         /* put it all back */
3462         ice_memcpy(dest, &dest_word, sizeof(dest_word), ICE_NONDMA_TO_DMA);
3463 }
3464
3465 /**
3466  * ice_write_dword - write a dword to a packed context structure
3467  * @src_ctx:  the context structure to read from
3468  * @dest_ctx: the context to be written to
3469  * @ce_info:  a description of the struct to be filled
3470  */
3471 static void
3472 ice_write_dword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3473 {
3474         u32 src_dword, mask;
3475         __le32 dest_dword;
3476         u8 *from, *dest;
3477         u16 shift_width;
3478
3479         /* copy from the next struct field */
3480         from = src_ctx + ce_info->offset;
3481
3482         /* prepare the bits and mask */
3483         shift_width = ce_info->lsb % 8;
3484
3485         /* if the field width is exactly 32 on an x86 machine, then the shift
3486          * operation will not work because the SHL instructions count is masked
3487          * to 5 bits so the shift will do nothing
3488          */
3489         if (ce_info->width < 32)
3490                 mask = BIT(ce_info->width) - 1;
3491         else
3492                 mask = (u32)~0;
3493
3494         /* don't swizzle the bits until after the mask because the mask bits
3495          * will be in a different bit position on big endian machines
3496          */
3497         src_dword = *(u32 *)from;
3498         src_dword &= mask;
3499
3500         /* shift to correct alignment */
3501         mask <<= shift_width;
3502         src_dword <<= shift_width;
3503
3504         /* get the current bits from the target bit string */
3505         dest = dest_ctx + (ce_info->lsb / 8);
3506
3507         ice_memcpy(&dest_dword, dest, sizeof(dest_dword), ICE_DMA_TO_NONDMA);
3508
3509         dest_dword &= ~(CPU_TO_LE32(mask));     /* get the bits not changing */
3510         dest_dword |= CPU_TO_LE32(src_dword);   /* add in the new bits */
3511
3512         /* put it all back */
3513         ice_memcpy(dest, &dest_dword, sizeof(dest_dword), ICE_NONDMA_TO_DMA);
3514 }
3515
3516 /**
3517  * ice_write_qword - write a qword to a packed context structure
3518  * @src_ctx:  the context structure to read from
3519  * @dest_ctx: the context to be written to
3520  * @ce_info:  a description of the struct to be filled
3521  */
3522 static void
3523 ice_write_qword(u8 *src_ctx, u8 *dest_ctx, const struct ice_ctx_ele *ce_info)
3524 {
3525         u64 src_qword, mask;
3526         __le64 dest_qword;
3527         u8 *from, *dest;
3528         u16 shift_width;
3529
3530         /* copy from the next struct field */
3531         from = src_ctx + ce_info->offset;
3532
3533         /* prepare the bits and mask */
3534         shift_width = ce_info->lsb % 8;
3535
3536         /* if the field width is exactly 64 on an x86 machine, then the shift
3537          * operation will not work because the SHL instructions count is masked
3538          * to 6 bits so the shift will do nothing
3539          */
3540         if (ce_info->width < 64)
3541                 mask = BIT_ULL(ce_info->width) - 1;
3542         else
3543                 mask = (u64)~0;
3544
3545         /* don't swizzle the bits until after the mask because the mask bits
3546          * will be in a different bit position on big endian machines
3547          */
3548         src_qword = *(u64 *)from;
3549         src_qword &= mask;
3550
3551         /* shift to correct alignment */
3552         mask <<= shift_width;
3553         src_qword <<= shift_width;
3554
3555         /* get the current bits from the target bit string */
3556         dest = dest_ctx + (ce_info->lsb / 8);
3557
3558         ice_memcpy(&dest_qword, dest, sizeof(dest_qword), ICE_DMA_TO_NONDMA);
3559
3560         dest_qword &= ~(CPU_TO_LE64(mask));     /* get the bits not changing */
3561         dest_qword |= CPU_TO_LE64(src_qword);   /* add in the new bits */
3562
3563         /* put it all back */
3564         ice_memcpy(dest, &dest_qword, sizeof(dest_qword), ICE_NONDMA_TO_DMA);
3565 }
3566
3567 /**
3568  * ice_set_ctx - set context bits in packed structure
3569  * @hw: pointer to the hardware structure
3570  * @src_ctx:  pointer to a generic non-packed context structure
3571  * @dest_ctx: pointer to memory for the packed structure
3572  * @ce_info:  a description of the structure to be transformed
3573  */
3574 enum ice_status
3575 ice_set_ctx(struct ice_hw *hw, u8 *src_ctx, u8 *dest_ctx,
3576             const struct ice_ctx_ele *ce_info)
3577 {
3578         int f;
3579
3580         for (f = 0; ce_info[f].width; f++) {
3581                 /* We have to deal with each element of the FW response
3582                  * using the correct size so that we are correct regardless
3583                  * of the endianness of the machine.
3584                  */
3585                 if (ce_info[f].width > (ce_info[f].size_of * BITS_PER_BYTE)) {
3586                         ice_debug(hw, ICE_DBG_QCTX,
3587                                   "Field %d width of %d bits larger than size of %d byte(s) ... skipping write\n",
3588                                   f, ce_info[f].width, ce_info[f].size_of);
3589                         continue;
3590                 }
3591                 switch (ce_info[f].size_of) {
3592                 case sizeof(u8):
3593                         ice_write_byte(src_ctx, dest_ctx, &ce_info[f]);
3594                         break;
3595                 case sizeof(u16):
3596                         ice_write_word(src_ctx, dest_ctx, &ce_info[f]);
3597                         break;
3598                 case sizeof(u32):
3599                         ice_write_dword(src_ctx, dest_ctx, &ce_info[f]);
3600                         break;
3601                 case sizeof(u64):
3602                         ice_write_qword(src_ctx, dest_ctx, &ce_info[f]);
3603                         break;
3604                 default:
3605                         return ICE_ERR_INVAL_SIZE;
3606                 }
3607         }
3608
3609         return ICE_SUCCESS;
3610 }
3611
3612 /**
3613  * ice_read_byte - read context byte into struct
3614  * @src_ctx:  the context structure to read from
3615  * @dest_ctx: the context to be written to
3616  * @ce_info:  a description of the struct to be filled
3617  */
3618 static void
3619 ice_read_byte(u8 *src_ctx, u8 *dest_ctx, struct ice_ctx_ele *ce_info)
3620 {
3621         u8 dest_byte, mask;
3622         u8 *src, *target;
3623         u16 shift_width;
3624
3625         /* prepare the bits and mask */
3626         shift_width = ce_info->lsb % 8;
3627         mask = (u8)(BIT(ce_info->width) - 1);
3628
3629         /* shift to correct alignment */
3630         mask <<= shift_width;
3631
3632         /* get the current bits from the src bit string */
3633         src = src_ctx + (ce_info->lsb / 8);
3634
3635         ice_memcpy(&dest_byte, src, sizeof(dest_byte), ICE_DMA_TO_NONDMA);
3636
3637         dest_byte &= ~(mask);
3638
3639         dest_byte >>= shift_width;
3640
3641         /* get the address from the struct field */
3642         target = dest_ctx + ce_info->offset;
3643
3644         /* put it back in the struct */
3645         ice_memcpy(target, &dest_byte, sizeof(dest_byte), ICE_NONDMA_TO_DMA);
3646 }
3647
3648 /**
3649  * ice_read_word - read context word into struct
3650  * @src_ctx:  the context structure to read from
3651  * @dest_ctx: the context to be written to
3652  * @ce_info:  a description of the struct to be filled
3653  */
3654 static void
3655 ice_read_word(u8 *src_ctx, u8 *dest_ctx, struct ice_ctx_ele *ce_info)
3656 {
3657         u16 dest_word, mask;
3658         u8 *src, *target;
3659         __le16 src_word;
3660         u16 shift_width;
3661
3662         /* prepare the bits and mask */
3663         shift_width = ce_info->lsb % 8;
3664         mask = BIT(ce_info->width) - 1;
3665
3666         /* shift to correct alignment */
3667         mask <<= shift_width;
3668
3669         /* get the current bits from the src bit string */
3670         src = src_ctx + (ce_info->lsb / 8);
3671
3672         ice_memcpy(&src_word, src, sizeof(src_word), ICE_DMA_TO_NONDMA);
3673
3674         /* the data in the memory is stored as little endian so mask it
3675          * correctly
3676          */
3677         src_word &= ~(CPU_TO_LE16(mask));
3678
3679         /* get the data back into host order before shifting */
3680         dest_word = LE16_TO_CPU(src_word);
3681
3682         dest_word >>= shift_width;
3683
3684         /* get the address from the struct field */
3685         target = dest_ctx + ce_info->offset;
3686
3687         /* put it back in the struct */
3688         ice_memcpy(target, &dest_word, sizeof(dest_word), ICE_NONDMA_TO_DMA);
3689 }
3690
3691 /**
3692  * ice_read_dword - read context dword into struct
3693  * @src_ctx:  the context structure to read from
3694  * @dest_ctx: the context to be written to
3695  * @ce_info:  a description of the struct to be filled
3696  */
3697 static void
3698 ice_read_dword(u8 *src_ctx, u8 *dest_ctx, struct ice_ctx_ele *ce_info)
3699 {
3700         u32 dest_dword, mask;
3701         __le32 src_dword;
3702         u8 *src, *target;
3703         u16 shift_width;
3704
3705         /* prepare the bits and mask */
3706         shift_width = ce_info->lsb % 8;
3707
3708         /* if the field width is exactly 32 on an x86 machine, then the shift
3709          * operation will not work because the SHL instructions count is masked
3710          * to 5 bits so the shift will do nothing
3711          */
3712         if (ce_info->width < 32)
3713                 mask = BIT(ce_info->width) - 1;
3714         else
3715                 mask = (u32)~0;
3716
3717         /* shift to correct alignment */
3718         mask <<= shift_width;
3719
3720         /* get the current bits from the src bit string */
3721         src = src_ctx + (ce_info->lsb / 8);
3722
3723         ice_memcpy(&src_dword, src, sizeof(src_dword), ICE_DMA_TO_NONDMA);
3724
3725         /* the data in the memory is stored as little endian so mask it
3726          * correctly
3727          */
3728         src_dword &= ~(CPU_TO_LE32(mask));
3729
3730         /* get the data back into host order before shifting */
3731         dest_dword = LE32_TO_CPU(src_dword);
3732
3733         dest_dword >>= shift_width;
3734
3735         /* get the address from the struct field */
3736         target = dest_ctx + ce_info->offset;
3737
3738         /* put it back in the struct */
3739         ice_memcpy(target, &dest_dword, sizeof(dest_dword), ICE_NONDMA_TO_DMA);
3740 }
3741
3742 /**
3743  * ice_read_qword - read context qword into struct
3744  * @src_ctx:  the context structure to read from
3745  * @dest_ctx: the context to be written to
3746  * @ce_info:  a description of the struct to be filled
3747  */
3748 static void
3749 ice_read_qword(u8 *src_ctx, u8 *dest_ctx, struct ice_ctx_ele *ce_info)
3750 {
3751         u64 dest_qword, mask;
3752         __le64 src_qword;
3753         u8 *src, *target;
3754         u16 shift_width;
3755
3756         /* prepare the bits and mask */
3757         shift_width = ce_info->lsb % 8;
3758
3759         /* if the field width is exactly 64 on an x86 machine, then the shift
3760          * operation will not work because the SHL instructions count is masked
3761          * to 6 bits so the shift will do nothing
3762          */
3763         if (ce_info->width < 64)
3764                 mask = BIT_ULL(ce_info->width) - 1;
3765         else
3766                 mask = (u64)~0;
3767
3768         /* shift to correct alignment */
3769         mask <<= shift_width;
3770
3771         /* get the current bits from the src bit string */
3772         src = src_ctx + (ce_info->lsb / 8);
3773
3774         ice_memcpy(&src_qword, src, sizeof(src_qword), ICE_DMA_TO_NONDMA);
3775
3776         /* the data in the memory is stored as little endian so mask it
3777          * correctly
3778          */
3779         src_qword &= ~(CPU_TO_LE64(mask));
3780
3781         /* get the data back into host order before shifting */
3782         dest_qword = LE64_TO_CPU(src_qword);
3783
3784         dest_qword >>= shift_width;
3785
3786         /* get the address from the struct field */
3787         target = dest_ctx + ce_info->offset;
3788
3789         /* put it back in the struct */
3790         ice_memcpy(target, &dest_qword, sizeof(dest_qword), ICE_NONDMA_TO_DMA);
3791 }
3792
3793 /**
3794  * ice_get_ctx - extract context bits from a packed structure
3795  * @src_ctx:  pointer to a generic packed context structure
3796  * @dest_ctx: pointer to a generic non-packed context structure
3797  * @ce_info:  a description of the structure to be read from
3798  */
3799 enum ice_status
3800 ice_get_ctx(u8 *src_ctx, u8 *dest_ctx, struct ice_ctx_ele *ce_info)
3801 {
3802         int f;
3803
3804         for (f = 0; ce_info[f].width; f++) {
3805                 switch (ce_info[f].size_of) {
3806                 case 1:
3807                         ice_read_byte(src_ctx, dest_ctx, &ce_info[f]);
3808                         break;
3809                 case 2:
3810                         ice_read_word(src_ctx, dest_ctx, &ce_info[f]);
3811                         break;
3812                 case 4:
3813                         ice_read_dword(src_ctx, dest_ctx, &ce_info[f]);
3814                         break;
3815                 case 8:
3816                         ice_read_qword(src_ctx, dest_ctx, &ce_info[f]);
3817                         break;
3818                 default:
3819                         /* nothing to do, just keep going */
3820                         break;
3821                 }
3822         }
3823
3824         return ICE_SUCCESS;
3825 }
3826
3827 /**
3828  * ice_get_lan_q_ctx - get the LAN queue context for the given VSI and TC
3829  * @hw: pointer to the HW struct
3830  * @vsi_handle: software VSI handle
3831  * @tc: TC number
3832  * @q_handle: software queue handle
3833  */
3834 struct ice_q_ctx *
3835 ice_get_lan_q_ctx(struct ice_hw *hw, u16 vsi_handle, u8 tc, u16 q_handle)
3836 {
3837         struct ice_vsi_ctx *vsi;
3838         struct ice_q_ctx *q_ctx;
3839
3840         vsi = ice_get_vsi_ctx(hw, vsi_handle);
3841         if (!vsi)
3842                 return NULL;
3843         if (q_handle >= vsi->num_lan_q_entries[tc])
3844                 return NULL;
3845         if (!vsi->lan_q_ctx[tc])
3846                 return NULL;
3847         q_ctx = vsi->lan_q_ctx[tc];
3848         return &q_ctx[q_handle];
3849 }
3850
3851 /**
3852  * ice_ena_vsi_txq
3853  * @pi: port information structure
3854  * @vsi_handle: software VSI handle
3855  * @tc: TC number
3856  * @q_handle: software queue handle
3857  * @num_qgrps: Number of added queue groups
3858  * @buf: list of queue groups to be added
3859  * @buf_size: size of buffer for indirect command
3860  * @cd: pointer to command details structure or NULL
3861  *
3862  * This function adds one LAN queue
3863  */
3864 enum ice_status
3865 ice_ena_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u16 q_handle,
3866                 u8 num_qgrps, struct ice_aqc_add_tx_qgrp *buf, u16 buf_size,
3867                 struct ice_sq_cd *cd)
3868 {
3869         struct ice_aqc_txsched_elem_data node = { 0 };
3870         struct ice_sched_node *parent;
3871         struct ice_q_ctx *q_ctx;
3872         enum ice_status status;
3873         struct ice_hw *hw;
3874
3875         if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
3876                 return ICE_ERR_CFG;
3877
3878         if (num_qgrps > 1 || buf->num_txqs > 1)
3879                 return ICE_ERR_MAX_LIMIT;
3880
3881         hw = pi->hw;
3882
3883         if (!ice_is_vsi_valid(hw, vsi_handle))
3884                 return ICE_ERR_PARAM;
3885
3886         ice_acquire_lock(&pi->sched_lock);
3887
3888         q_ctx = ice_get_lan_q_ctx(hw, vsi_handle, tc, q_handle);
3889         if (!q_ctx) {
3890                 ice_debug(hw, ICE_DBG_SCHED, "Enaq: invalid queue handle %d\n",
3891                           q_handle);
3892                 status = ICE_ERR_PARAM;
3893                 goto ena_txq_exit;
3894         }
3895
3896         /* find a parent node */
3897         parent = ice_sched_get_free_qparent(pi, vsi_handle, tc,
3898                                             ICE_SCHED_NODE_OWNER_LAN);
3899         if (!parent) {
3900                 status = ICE_ERR_PARAM;
3901                 goto ena_txq_exit;
3902         }
3903
3904         buf->parent_teid = parent->info.node_teid;
3905         node.parent_teid = parent->info.node_teid;
3906         /* Mark that the values in the "generic" section as valid. The default
3907          * value in the "generic" section is zero. This means that :
3908          * - Scheduling mode is Bytes Per Second (BPS), indicated by Bit 0.
3909          * - 0 priority among siblings, indicated by Bit 1-3.
3910          * - WFQ, indicated by Bit 4.
3911          * - 0 Adjustment value is used in PSM credit update flow, indicated by
3912          * Bit 5-6.
3913          * - Bit 7 is reserved.
3914          * Without setting the generic section as valid in valid_sections, the
3915          * Admin queue command will fail with error code ICE_AQ_RC_EINVAL.
3916          */
3917         buf->txqs[0].info.valid_sections = ICE_AQC_ELEM_VALID_GENERIC;
3918
3919         /* add the LAN queue */
3920         status = ice_aq_add_lan_txq(hw, num_qgrps, buf, buf_size, cd);
3921         if (status != ICE_SUCCESS) {
3922                 ice_debug(hw, ICE_DBG_SCHED, "enable queue %d failed %d\n",
3923                           LE16_TO_CPU(buf->txqs[0].txq_id),
3924                           hw->adminq.sq_last_status);
3925                 goto ena_txq_exit;
3926         }
3927
3928         node.node_teid = buf->txqs[0].q_teid;
3929         node.data.elem_type = ICE_AQC_ELEM_TYPE_LEAF;
3930         q_ctx->q_handle = q_handle;
3931         q_ctx->q_teid = LE32_TO_CPU(node.node_teid);
3932
3933         /* add a leaf node into scheduler tree queue layer */
3934         status = ice_sched_add_node(pi, hw->num_tx_sched_layers - 1, &node);
3935         if (!status)
3936                 status = ice_sched_replay_q_bw(pi, q_ctx);
3937
3938 ena_txq_exit:
3939         ice_release_lock(&pi->sched_lock);
3940         return status;
3941 }
3942
3943 /**
3944  * ice_dis_vsi_txq
3945  * @pi: port information structure
3946  * @vsi_handle: software VSI handle
3947  * @tc: TC number
3948  * @num_queues: number of queues
3949  * @q_handles: pointer to software queue handle array
3950  * @q_ids: pointer to the q_id array
3951  * @q_teids: pointer to queue node teids
3952  * @rst_src: if called due to reset, specifies the reset source
3953  * @vmvf_num: the relative VM or VF number that is undergoing the reset
3954  * @cd: pointer to command details structure or NULL
3955  *
3956  * This function removes queues and their corresponding nodes in SW DB
3957  */
3958 enum ice_status
3959 ice_dis_vsi_txq(struct ice_port_info *pi, u16 vsi_handle, u8 tc, u8 num_queues,
3960                 u16 *q_handles, u16 *q_ids, u32 *q_teids,
3961                 enum ice_disq_rst_src rst_src, u16 vmvf_num,
3962                 struct ice_sq_cd *cd)
3963 {
3964         enum ice_status status = ICE_ERR_DOES_NOT_EXIST;
3965         struct ice_aqc_dis_txq_item qg_list;
3966         struct ice_q_ctx *q_ctx;
3967         u16 i;
3968
3969         if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
3970                 return ICE_ERR_CFG;
3971
3972         if (!num_queues) {
3973                 /* if queue is disabled already yet the disable queue command
3974                  * has to be sent to complete the VF reset, then call
3975                  * ice_aq_dis_lan_txq without any queue information
3976                  */
3977                 if (rst_src)
3978                         return ice_aq_dis_lan_txq(pi->hw, 0, NULL, 0, rst_src,
3979                                                   vmvf_num, NULL);
3980                 return ICE_ERR_CFG;
3981         }
3982
3983         ice_acquire_lock(&pi->sched_lock);
3984
3985         for (i = 0; i < num_queues; i++) {
3986                 struct ice_sched_node *node;
3987
3988                 node = ice_sched_find_node_by_teid(pi->root, q_teids[i]);
3989                 if (!node)
3990                         continue;
3991                 q_ctx = ice_get_lan_q_ctx(pi->hw, vsi_handle, tc, q_handles[i]);
3992                 if (!q_ctx) {
3993                         ice_debug(pi->hw, ICE_DBG_SCHED, "invalid queue handle%d\n",
3994                                   q_handles[i]);
3995                         continue;
3996                 }
3997                 if (q_ctx->q_handle != q_handles[i]) {
3998                         ice_debug(pi->hw, ICE_DBG_SCHED, "Err:handles %d %d\n",
3999                                   q_ctx->q_handle, q_handles[i]);
4000                         continue;
4001                 }
4002                 qg_list.parent_teid = node->info.parent_teid;
4003                 qg_list.num_qs = 1;
4004                 qg_list.q_id[0] = CPU_TO_LE16(q_ids[i]);
4005                 status = ice_aq_dis_lan_txq(pi->hw, 1, &qg_list,
4006                                             sizeof(qg_list), rst_src, vmvf_num,
4007                                             cd);
4008
4009                 if (status != ICE_SUCCESS)
4010                         break;
4011                 ice_free_sched_node(pi, node);
4012                 q_ctx->q_handle = ICE_INVAL_Q_HANDLE;
4013         }
4014         ice_release_lock(&pi->sched_lock);
4015         return status;
4016 }
4017
4018 /**
4019  * ice_cfg_vsi_qs - configure the new/existing VSI queues
4020  * @pi: port information structure
4021  * @vsi_handle: software VSI handle
4022  * @tc_bitmap: TC bitmap
4023  * @maxqs: max queues array per TC
4024  * @owner: LAN or RDMA
4025  *
4026  * This function adds/updates the VSI queues per TC.
4027  */
4028 static enum ice_status
4029 ice_cfg_vsi_qs(struct ice_port_info *pi, u16 vsi_handle, u16 tc_bitmap,
4030                u16 *maxqs, u8 owner)
4031 {
4032         enum ice_status status = ICE_SUCCESS;
4033         u8 i;
4034
4035         if (!pi || pi->port_state != ICE_SCHED_PORT_STATE_READY)
4036                 return ICE_ERR_CFG;
4037
4038         if (!ice_is_vsi_valid(pi->hw, vsi_handle))
4039                 return ICE_ERR_PARAM;
4040
4041         ice_acquire_lock(&pi->sched_lock);
4042
4043         ice_for_each_traffic_class(i) {
4044                 /* configuration is possible only if TC node is present */
4045                 if (!ice_sched_get_tc_node(pi, i))
4046                         continue;
4047
4048                 status = ice_sched_cfg_vsi(pi, vsi_handle, i, maxqs[i], owner,
4049                                            ice_is_tc_ena(tc_bitmap, i));
4050                 if (status)
4051                         break;
4052         }
4053
4054         ice_release_lock(&pi->sched_lock);
4055         return status;
4056 }
4057
4058 /**
4059  * ice_cfg_vsi_lan - configure VSI LAN queues
4060  * @pi: port information structure
4061  * @vsi_handle: software VSI handle
4062  * @tc_bitmap: TC bitmap
4063  * @max_lanqs: max LAN queues array per TC
4064  *
4065  * This function adds/updates the VSI LAN queues per TC.
4066  */
4067 enum ice_status
4068 ice_cfg_vsi_lan(struct ice_port_info *pi, u16 vsi_handle, u16 tc_bitmap,
4069                 u16 *max_lanqs)
4070 {
4071         return ice_cfg_vsi_qs(pi, vsi_handle, tc_bitmap, max_lanqs,
4072                               ICE_SCHED_NODE_OWNER_LAN);
4073 }
4074
4075 /**
4076  * ice_replay_pre_init - replay pre initialization
4077  * @hw: pointer to the HW struct
4078  *
4079  * Initializes required config data for VSI, FD, ACL, and RSS before replay.
4080  */
4081 static enum ice_status ice_replay_pre_init(struct ice_hw *hw)
4082 {
4083         struct ice_switch_info *sw = hw->switch_info;
4084         u8 i;
4085
4086         /* Delete old entries from replay filter list head if there is any */
4087         ice_rm_all_sw_replay_rule_info(hw);
4088         /* In start of replay, move entries into replay_rules list, it
4089          * will allow adding rules entries back to filt_rules list,
4090          * which is operational list.
4091          */
4092         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
4093                 LIST_REPLACE_INIT(&sw->recp_list[i].filt_rules,
4094                                   &sw->recp_list[i].filt_replay_rules);
4095         ice_sched_replay_agg_vsi_preinit(hw);
4096
4097         return ice_sched_replay_tc_node_bw(hw->port_info);
4098 }
4099
4100 /**
4101  * ice_replay_vsi - replay VSI configuration
4102  * @hw: pointer to the HW struct
4103  * @vsi_handle: driver VSI handle
4104  *
4105  * Restore all VSI configuration after reset. It is required to call this
4106  * function with main VSI first.
4107  */
4108 enum ice_status ice_replay_vsi(struct ice_hw *hw, u16 vsi_handle)
4109 {
4110         enum ice_status status;
4111
4112         if (!ice_is_vsi_valid(hw, vsi_handle))
4113                 return ICE_ERR_PARAM;
4114
4115         /* Replay pre-initialization if there is any */
4116         if (vsi_handle == ICE_MAIN_VSI_HANDLE) {
4117                 status = ice_replay_pre_init(hw);
4118                 if (status)
4119                         return status;
4120         }
4121         /* Replay per VSI all RSS configurations */
4122         status = ice_replay_rss_cfg(hw, vsi_handle);
4123         if (status)
4124                 return status;
4125         /* Replay per VSI all filters */
4126         status = ice_replay_vsi_all_fltr(hw, vsi_handle);
4127         if (!status)
4128                 status = ice_replay_vsi_agg(hw, vsi_handle);
4129         return status;
4130 }
4131
4132 /**
4133  * ice_replay_post - post replay configuration cleanup
4134  * @hw: pointer to the HW struct
4135  *
4136  * Post replay cleanup.
4137  */
4138 void ice_replay_post(struct ice_hw *hw)
4139 {
4140         /* Delete old entries from replay filter list head */
4141         ice_rm_all_sw_replay_rule_info(hw);
4142         ice_sched_replay_agg(hw);
4143 }
4144
4145 /**
4146  * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4147  * @hw: ptr to the hardware info
4148  * @reg: offset of 64 bit HW register to read from
4149  * @prev_stat_loaded: bool to specify if previous stats are loaded
4150  * @prev_stat: ptr to previous loaded stat value
4151  * @cur_stat: ptr to current stat value
4152  */
4153 void
4154 ice_stat_update40(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4155                   u64 *prev_stat, u64 *cur_stat)
4156 {
4157         u64 new_data = rd64(hw, reg) & (BIT_ULL(40) - 1);
4158
4159         /* device stats are not reset at PFR, they likely will not be zeroed
4160          * when the driver starts. Thus, save the value from the first read
4161          * without adding to the statistic value so that we report stats which
4162          * count up from zero.
4163          */
4164         if (!prev_stat_loaded) {
4165                 *prev_stat = new_data;
4166                 return;
4167         }
4168
4169         /* Calculate the difference between the new and old values, and then
4170          * add it to the software stat value.
4171          */
4172         if (new_data >= *prev_stat)
4173                 *cur_stat += new_data - *prev_stat;
4174         else
4175                 /* to manage the potential roll-over */
4176                 *cur_stat += (new_data + BIT_ULL(40)) - *prev_stat;
4177
4178         /* Update the previously stored value to prepare for next read */
4179         *prev_stat = new_data;
4180 }
4181
4182 /**
4183  * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4184  * @hw: ptr to the hardware info
4185  * @reg: offset of HW register to read from
4186  * @prev_stat_loaded: bool to specify if previous stats are loaded
4187  * @prev_stat: ptr to previous loaded stat value
4188  * @cur_stat: ptr to current stat value
4189  */
4190 void
4191 ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4192                   u64 *prev_stat, u64 *cur_stat)
4193 {
4194         u32 new_data;
4195
4196         new_data = rd32(hw, reg);
4197
4198         /* device stats are not reset at PFR, they likely will not be zeroed
4199          * when the driver starts. Thus, save the value from the first read
4200          * without adding to the statistic value so that we report stats which
4201          * count up from zero.
4202          */
4203         if (!prev_stat_loaded) {
4204                 *prev_stat = new_data;
4205                 return;
4206         }
4207
4208         /* Calculate the difference between the new and old values, and then
4209          * add it to the software stat value.
4210          */
4211         if (new_data >= *prev_stat)
4212                 *cur_stat += new_data - *prev_stat;
4213         else
4214                 /* to manage the potential roll-over */
4215                 *cur_stat += (new_data + BIT_ULL(32)) - *prev_stat;
4216
4217         /* Update the previously stored value to prepare for next read */
4218         *prev_stat = new_data;
4219 }
4220
4221 /**
4222  * ice_stat_update_repc - read GLV_REPC stats from chip and update stat values
4223  * @hw: ptr to the hardware info
4224  * @vsi_handle: VSI handle
4225  * @prev_stat_loaded: bool to specify if the previous stat values are loaded
4226  * @cur_stats: ptr to current stats structure
4227  *
4228  * The GLV_REPC statistic register actually tracks two 16bit statistics, and
4229  * thus cannot be read using the normal ice_stat_update32 function.
4230  *
4231  * Read the GLV_REPC register associated with the given VSI, and update the
4232  * rx_no_desc and rx_error values in the ice_eth_stats structure.
4233  *
4234  * Because the statistics in GLV_REPC stick at 0xFFFF, the register must be
4235  * cleared each time it's read.
4236  *
4237  * Note that the GLV_RDPC register also counts the causes that would trigger
4238  * GLV_REPC. However, it does not give the finer grained detail about why the
4239  * packets are being dropped. The GLV_REPC values can be used to distinguish
4240  * whether Rx packets are dropped due to errors or due to no available
4241  * descriptors.
4242  */
4243 void
4244 ice_stat_update_repc(struct ice_hw *hw, u16 vsi_handle, bool prev_stat_loaded,
4245                      struct ice_eth_stats *cur_stats)
4246 {
4247         u16 vsi_num, no_desc, error_cnt;
4248         u32 repc;
4249
4250         if (!ice_is_vsi_valid(hw, vsi_handle))
4251                 return;
4252
4253         vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
4254
4255         /* If we haven't loaded stats yet, just clear the current value */
4256         if (!prev_stat_loaded) {
4257                 wr32(hw, GLV_REPC(vsi_num), 0);
4258                 return;
4259         }
4260
4261         repc = rd32(hw, GLV_REPC(vsi_num));
4262         no_desc = (repc & GLV_REPC_NO_DESC_CNT_M) >> GLV_REPC_NO_DESC_CNT_S;
4263         error_cnt = (repc & GLV_REPC_ERROR_CNT_M) >> GLV_REPC_ERROR_CNT_S;
4264
4265         /* Clear the count by writing to the stats register */
4266         wr32(hw, GLV_REPC(vsi_num), 0);
4267
4268         cur_stats->rx_no_desc += no_desc;
4269         cur_stats->rx_errors += error_cnt;
4270 }
4271
4272 /**
4273  * ice_sched_query_elem - query element information from HW
4274  * @hw: pointer to the HW struct
4275  * @node_teid: node TEID to be queried
4276  * @buf: buffer to element information
4277  *
4278  * This function queries HW element information
4279  */
4280 enum ice_status
4281 ice_sched_query_elem(struct ice_hw *hw, u32 node_teid,
4282                      struct ice_aqc_get_elem *buf)
4283 {
4284         u16 buf_size, num_elem_ret = 0;
4285         enum ice_status status;
4286
4287         buf_size = sizeof(*buf);
4288         ice_memset(buf, 0, buf_size, ICE_NONDMA_MEM);
4289         buf->generic[0].node_teid = CPU_TO_LE32(node_teid);
4290         status = ice_aq_query_sched_elems(hw, 1, buf, buf_size, &num_elem_ret,
4291                                           NULL);
4292         if (status != ICE_SUCCESS || num_elem_ret != 1)
4293                 ice_debug(hw, ICE_DBG_SCHED, "query element failed\n");
4294         return status;
4295 }
4296
4297 /**
4298  * ice_get_fw_mode - returns FW mode
4299  * @hw: pointer to the HW struct
4300  */
4301 enum ice_fw_modes ice_get_fw_mode(struct ice_hw *hw)
4302 {
4303 #define ICE_FW_MODE_DBG_M BIT(0)
4304 #define ICE_FW_MODE_REC_M BIT(1)
4305 #define ICE_FW_MODE_ROLLBACK_M BIT(2)
4306         u32 fw_mode;
4307
4308         /* check the current FW mode */
4309         fw_mode = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_MODES_M;
4310
4311         if (fw_mode & ICE_FW_MODE_DBG_M)
4312                 return ICE_FW_MODE_DBG;
4313         else if (fw_mode & ICE_FW_MODE_REC_M)
4314                 return ICE_FW_MODE_REC;
4315         else if (fw_mode & ICE_FW_MODE_ROLLBACK_M)
4316                 return ICE_FW_MODE_ROLLBACK;
4317         else
4318                 return ICE_FW_MODE_NORMAL;
4319 }
4320
4321 /**
4322  * ice_fw_supports_link_override
4323  * @hw: pointer to the hardware structure
4324  *
4325  * Checks if the firmware supports link override
4326  */
4327 bool ice_fw_supports_link_override(struct ice_hw *hw)
4328 {
4329         if (hw->api_maj_ver == ICE_FW_API_LINK_OVERRIDE_MAJ) {
4330                 if (hw->api_min_ver > ICE_FW_API_LINK_OVERRIDE_MIN)
4331                         return true;
4332                 if (hw->api_min_ver == ICE_FW_API_LINK_OVERRIDE_MIN &&
4333                     hw->api_patch >= ICE_FW_API_LINK_OVERRIDE_PATCH)
4334                         return true;
4335         } else if (hw->api_maj_ver > ICE_FW_API_LINK_OVERRIDE_MAJ) {
4336                 return true;
4337         }
4338
4339         return false;
4340 }
4341
4342 /**
4343  * ice_get_link_default_override
4344  * @ldo: pointer to the link default override struct
4345  * @pi: pointer to the port info struct
4346  *
4347  * Gets the link default override for a port
4348  */
4349 enum ice_status
4350 ice_get_link_default_override(struct ice_link_default_override_tlv *ldo,
4351                               struct ice_port_info *pi)
4352 {
4353         u16 i, tlv, tlv_len, tlv_start, buf, offset;
4354         struct ice_hw *hw = pi->hw;
4355         enum ice_status status;
4356
4357         status = ice_get_pfa_module_tlv(hw, &tlv, &tlv_len,
4358                                         ICE_SR_LINK_DEFAULT_OVERRIDE_PTR);
4359         if (status) {
4360                 ice_debug(hw, ICE_DBG_INIT,
4361                           "Failed to read link override TLV.\n");
4362                 return status;
4363         }
4364
4365         /* Each port has its own config; calculate for our port */
4366         tlv_start = tlv + pi->lport * ICE_SR_PFA_LINK_OVERRIDE_WORDS +
4367                 ICE_SR_PFA_LINK_OVERRIDE_OFFSET;
4368
4369         /* link options first */
4370         status = ice_read_sr_word(hw, tlv_start, &buf);
4371         if (status) {
4372                 ice_debug(hw, ICE_DBG_INIT,
4373                           "Failed to read override link options.\n");
4374                 return status;
4375         }
4376         ldo->options = buf & ICE_LINK_OVERRIDE_OPT_M;
4377         ldo->phy_config = (buf & ICE_LINK_OVERRIDE_PHY_CFG_M) >>
4378                 ICE_LINK_OVERRIDE_PHY_CFG_S;
4379
4380         /* link PHY config */
4381         offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_FEC_OFFSET;
4382         status = ice_read_sr_word(hw, offset, &buf);
4383         if (status) {
4384                 ice_debug(hw, ICE_DBG_INIT,
4385                           "Failed to read override phy config.\n");
4386                 return status;
4387         }
4388         ldo->fec_options = buf & ICE_LINK_OVERRIDE_FEC_OPT_M;
4389
4390         /* PHY types low */
4391         offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_PHY_OFFSET;
4392         for (i = 0; i < ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS; i++) {
4393                 status = ice_read_sr_word(hw, (offset + i), &buf);
4394                 if (status) {
4395                         ice_debug(hw, ICE_DBG_INIT,
4396                                   "Failed to read override link options.\n");
4397                         return status;
4398                 }
4399                 /* shift 16 bits at a time to fill 64 bits */
4400                 ldo->phy_type_low |= ((u64)buf << (i * 16));
4401         }
4402
4403         /* PHY types high */
4404         offset = tlv_start + ICE_SR_PFA_LINK_OVERRIDE_PHY_OFFSET +
4405                 ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS;
4406         for (i = 0; i < ICE_SR_PFA_LINK_OVERRIDE_PHY_WORDS; i++) {
4407                 status = ice_read_sr_word(hw, (offset + i), &buf);
4408                 if (status) {
4409                         ice_debug(hw, ICE_DBG_INIT,
4410                                   "Failed to read override link options.\n");
4411                         return status;
4412                 }
4413                 /* shift 16 bits at a time to fill 64 bits */
4414                 ldo->phy_type_high |= ((u64)buf << (i * 16));
4415         }
4416
4417         return status;
4418 }