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