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