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