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