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