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