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