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