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