1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 #include "ice_dcb_lib.h"
10 #include "ice_devlink.h"
11 #include "ice_vsi_vlan_ops.h"
14 * ice_vsi_type_str - maps VSI type enum to string equivalents
15 * @vsi_type: VSI type enum
17 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
25 return "ICE_VSI_CTRL";
27 return "ICE_VSI_CHNL";
30 case ICE_VSI_SWITCHDEV_CTRL:
31 return "ICE_VSI_SWITCHDEV_CTRL";
38 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
39 * @vsi: the VSI being configured
40 * @ena: start or stop the Rx rings
42 * First enable/disable all of the Rx rings, flush any remaining writes, and
43 * then verify that they have all been enabled/disabled successfully. This will
44 * let all of the register writes complete when enabling/disabling the Rx rings
45 * before waiting for the change in hardware to complete.
47 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
52 ice_for_each_rxq(vsi, i)
53 ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
55 ice_flush(&vsi->back->hw);
57 ice_for_each_rxq(vsi, i) {
58 ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
67 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
70 * On error: returns error code (negative)
71 * On success: returns 0
73 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
75 struct ice_pf *pf = vsi->back;
78 dev = ice_pf_to_dev(pf);
79 if (vsi->type == ICE_VSI_CHNL)
82 /* allocate memory for both Tx and Rx ring pointers */
83 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
84 sizeof(*vsi->tx_rings), GFP_KERNEL);
88 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
89 sizeof(*vsi->rx_rings), GFP_KERNEL);
93 /* txq_map needs to have enough space to track both Tx (stack) rings
94 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
95 * so use num_possible_cpus() as we want to always provide XDP ring
96 * per CPU, regardless of queue count settings from user that might
97 * have come from ethtool's set_channels() callback;
99 vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
100 sizeof(*vsi->txq_map), GFP_KERNEL);
105 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
106 sizeof(*vsi->rxq_map), GFP_KERNEL);
110 /* There is no need to allocate q_vectors for a loopback VSI. */
111 if (vsi->type == ICE_VSI_LB)
114 /* allocate memory for q_vector pointers */
115 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
116 sizeof(*vsi->q_vectors), GFP_KERNEL);
120 vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
121 if (!vsi->af_xdp_zc_qps)
127 devm_kfree(dev, vsi->q_vectors);
129 devm_kfree(dev, vsi->rxq_map);
131 devm_kfree(dev, vsi->txq_map);
133 devm_kfree(dev, vsi->rx_rings);
135 devm_kfree(dev, vsi->tx_rings);
140 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
141 * @vsi: the VSI being configured
143 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
147 case ICE_VSI_SWITCHDEV_CTRL:
150 /* a user could change the values of num_[tr]x_desc using
151 * ethtool -G so we should keep those values instead of
152 * overwriting them with the defaults.
154 if (!vsi->num_rx_desc)
155 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
156 if (!vsi->num_tx_desc)
157 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
160 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
167 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
168 * @vsi: the VSI being configured
169 * @vf_id: ID of the VF being configured
171 * Return 0 on success and a negative value on error
173 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
175 struct ice_pf *pf = vsi->back;
176 struct ice_vf *vf = NULL;
178 if (vsi->type == ICE_VSI_VF)
181 vsi->vf_id = ICE_INVAL_VFID;
186 vsi->alloc_txq = vsi->req_txq;
187 vsi->num_txq = vsi->req_txq;
189 vsi->alloc_txq = min3(pf->num_lan_msix,
190 ice_get_avail_txq_count(pf),
191 (u16)num_online_cpus());
194 pf->num_lan_tx = vsi->alloc_txq;
196 /* only 1 Rx queue unless RSS is enabled */
197 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
201 vsi->alloc_rxq = vsi->req_rxq;
202 vsi->num_rxq = vsi->req_rxq;
204 vsi->alloc_rxq = min3(pf->num_lan_msix,
205 ice_get_avail_rxq_count(pf),
206 (u16)num_online_cpus());
210 pf->num_lan_rx = vsi->alloc_rxq;
212 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
213 max_t(int, vsi->alloc_rxq,
216 case ICE_VSI_SWITCHDEV_CTRL:
217 /* The number of queues for ctrl VSI is equal to number of VFs.
218 * Each ring is associated to the corresponding VF_PR netdev.
220 vsi->alloc_txq = pf->num_alloc_vfs;
221 vsi->alloc_rxq = pf->num_alloc_vfs;
222 vsi->num_q_vectors = 1;
225 vf = &pf->vf[vsi->vf_id];
227 vf->num_vf_qs = vf->num_req_qs;
228 vsi->alloc_txq = vf->num_vf_qs;
229 vsi->alloc_rxq = vf->num_vf_qs;
230 /* pf->num_msix_per_vf includes (VF miscellaneous vector +
231 * data queue interrupts). Since vsi->num_q_vectors is number
232 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
233 * original vector count
235 vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
240 vsi->num_q_vectors = 1;
251 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
255 ice_vsi_set_num_desc(vsi);
259 * ice_get_free_slot - get the next non-NULL location index in array
260 * @array: array to search
261 * @size: size of the array
262 * @curr: last known occupied index to be used as a search hint
264 * void * is being used to keep the functionality generic. This lets us use this
265 * function on any array of pointers.
267 static int ice_get_free_slot(void *array, int size, int curr)
269 int **tmp_array = (int **)array;
272 if (curr < (size - 1) && !tmp_array[curr + 1]) {
277 while ((i < size) && (tmp_array[i]))
288 * ice_vsi_delete - delete a VSI from the switch
289 * @vsi: pointer to VSI being removed
291 void ice_vsi_delete(struct ice_vsi *vsi)
293 struct ice_pf *pf = vsi->back;
294 struct ice_vsi_ctx *ctxt;
297 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
301 if (vsi->type == ICE_VSI_VF)
302 ctxt->vf_num = vsi->vf_id;
303 ctxt->vsi_num = vsi->vsi_num;
305 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
307 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
309 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
310 vsi->vsi_num, status);
316 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
317 * @vsi: pointer to VSI being cleared
319 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
321 struct ice_pf *pf = vsi->back;
324 dev = ice_pf_to_dev(pf);
326 if (vsi->af_xdp_zc_qps) {
327 bitmap_free(vsi->af_xdp_zc_qps);
328 vsi->af_xdp_zc_qps = NULL;
330 /* free the ring and vector containers */
331 if (vsi->q_vectors) {
332 devm_kfree(dev, vsi->q_vectors);
333 vsi->q_vectors = NULL;
336 devm_kfree(dev, vsi->tx_rings);
337 vsi->tx_rings = NULL;
340 devm_kfree(dev, vsi->rx_rings);
341 vsi->rx_rings = NULL;
344 devm_kfree(dev, vsi->txq_map);
348 devm_kfree(dev, vsi->rxq_map);
354 * ice_vsi_clear - clean up and deallocate the provided VSI
355 * @vsi: pointer to VSI being cleared
357 * This deallocates the VSI's queue resources, removes it from the PF's
358 * VSI array if necessary, and deallocates the VSI
360 * Returns 0 on success, negative on failure
362 int ice_vsi_clear(struct ice_vsi *vsi)
364 struct ice_pf *pf = NULL;
374 dev = ice_pf_to_dev(pf);
376 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
377 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
381 mutex_lock(&pf->sw_mutex);
382 /* updates the PF for this cleared VSI */
384 pf->vsi[vsi->idx] = NULL;
385 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
386 pf->next_vsi = vsi->idx;
387 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
388 vsi->vf_id != ICE_INVAL_VFID)
389 pf->next_vsi = vsi->idx;
391 ice_vsi_free_arrays(vsi);
392 mutex_unlock(&pf->sw_mutex);
393 devm_kfree(dev, vsi);
399 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
400 * @irq: interrupt number
401 * @data: pointer to a q_vector
403 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
405 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
407 if (!q_vector->tx.tx_ring)
410 #define FDIR_RX_DESC_CLEAN_BUDGET 64
411 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
412 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
418 * ice_msix_clean_rings - MSIX mode Interrupt Handler
419 * @irq: interrupt number
420 * @data: pointer to a q_vector
422 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
424 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
426 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
429 q_vector->total_events++;
431 napi_schedule(&q_vector->napi);
436 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
438 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
439 struct ice_pf *pf = q_vector->vsi->back;
442 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
445 ice_for_each_vf(pf, i)
446 napi_schedule(&pf->vf[i].repr->q_vector->napi);
452 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
453 * @pf: board private structure
454 * @vsi_type: type of VSI
455 * @ch: ptr to channel
456 * @vf_id: ID of the VF being configured
458 * returns a pointer to a VSI on success, NULL on failure.
460 static struct ice_vsi *
461 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type,
462 struct ice_channel *ch, u16 vf_id)
464 struct device *dev = ice_pf_to_dev(pf);
465 struct ice_vsi *vsi = NULL;
467 /* Need to protect the allocation of the VSIs at the PF level */
468 mutex_lock(&pf->sw_mutex);
470 /* If we have already allocated our maximum number of VSIs,
471 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
472 * is available to be populated
474 if (pf->next_vsi == ICE_NO_VSI) {
475 dev_dbg(dev, "out of VSI slots!\n");
479 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
483 vsi->type = vsi_type;
485 set_bit(ICE_VSI_DOWN, vsi->state);
487 if (vsi_type == ICE_VSI_VF)
488 ice_vsi_set_num_qs(vsi, vf_id);
489 else if (vsi_type != ICE_VSI_CHNL)
490 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
493 case ICE_VSI_SWITCHDEV_CTRL:
494 if (ice_vsi_alloc_arrays(vsi))
497 /* Setup eswitch MSIX irq handler for VSI */
498 vsi->irq_handler = ice_eswitch_msix_clean_rings;
501 if (ice_vsi_alloc_arrays(vsi))
504 /* Setup default MSIX irq handler for VSI */
505 vsi->irq_handler = ice_msix_clean_rings;
508 if (ice_vsi_alloc_arrays(vsi))
511 /* Setup ctrl VSI MSIX irq handler */
512 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
515 if (ice_vsi_alloc_arrays(vsi))
521 vsi->num_rxq = ch->num_rxq;
522 vsi->num_txq = ch->num_txq;
523 vsi->next_base_q = ch->base_q;
526 if (ice_vsi_alloc_arrays(vsi))
530 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
534 if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
535 /* Use the last VSI slot as the index for PF control VSI */
536 vsi->idx = pf->num_alloc_vsi - 1;
537 pf->ctrl_vsi_idx = vsi->idx;
538 pf->vsi[vsi->idx] = vsi;
540 /* fill slot and make note of the index */
541 vsi->idx = pf->next_vsi;
542 pf->vsi[pf->next_vsi] = vsi;
544 /* prepare pf->next_vsi for next use */
545 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
549 if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
550 pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
554 devm_kfree(dev, vsi);
557 mutex_unlock(&pf->sw_mutex);
562 * ice_alloc_fd_res - Allocate FD resource for a VSI
563 * @vsi: pointer to the ice_vsi
565 * This allocates the FD resources
567 * Returns 0 on success, -EPERM on no-op or -EIO on failure
569 static int ice_alloc_fd_res(struct ice_vsi *vsi)
571 struct ice_pf *pf = vsi->back;
574 /* Flow Director filters are only allocated/assigned to the PF VSI or
575 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
576 * add/delete filters so resources are not allocated to it
578 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
581 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
582 vsi->type == ICE_VSI_CHNL))
585 /* FD filters from guaranteed pool per VSI */
586 g_val = pf->hw.func_caps.fd_fltr_guar;
590 /* FD filters from best effort pool */
591 b_val = pf->hw.func_caps.fd_fltr_best_effort;
595 /* PF main VSI gets only 64 FD resources from guaranteed pool
596 * when ADQ is configured.
598 #define ICE_PF_VSI_GFLTR 64
600 /* determine FD filter resources per VSI from shared(best effort) and
603 if (vsi->type == ICE_VSI_PF) {
604 vsi->num_gfltr = g_val;
605 /* if MQPRIO is configured, main VSI doesn't get all FD
606 * resources from guaranteed pool. PF VSI gets 64 FD resources
608 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
609 if (g_val < ICE_PF_VSI_GFLTR)
611 /* allow bare minimum entries for PF VSI */
612 vsi->num_gfltr = ICE_PF_VSI_GFLTR;
615 /* each VSI gets same "best_effort" quota */
616 vsi->num_bfltr = b_val;
617 } else if (vsi->type == ICE_VSI_VF) {
620 /* each VSI gets same "best_effort" quota */
621 vsi->num_bfltr = b_val;
623 struct ice_vsi *main_vsi;
626 main_vsi = ice_get_main_vsi(pf);
630 if (!main_vsi->all_numtc)
633 /* figure out ADQ numtc */
634 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
636 /* only one TC but still asking resources for channels,
639 if (numtc < ICE_CHNL_START_TC)
642 g_val -= ICE_PF_VSI_GFLTR;
643 /* channel VSIs gets equal share from guaranteed pool */
644 vsi->num_gfltr = g_val / numtc;
646 /* each VSI gets same "best_effort" quota */
647 vsi->num_bfltr = b_val;
654 * ice_vsi_get_qs - Assign queues from PF to VSI
655 * @vsi: the VSI to assign queues to
657 * Returns 0 on success and a negative value on error
659 static int ice_vsi_get_qs(struct ice_vsi *vsi)
661 struct ice_pf *pf = vsi->back;
662 struct ice_qs_cfg tx_qs_cfg = {
663 .qs_mutex = &pf->avail_q_mutex,
664 .pf_map = pf->avail_txqs,
665 .pf_map_size = pf->max_pf_txqs,
666 .q_count = vsi->alloc_txq,
667 .scatter_count = ICE_MAX_SCATTER_TXQS,
668 .vsi_map = vsi->txq_map,
670 .mapping_mode = ICE_VSI_MAP_CONTIG
672 struct ice_qs_cfg rx_qs_cfg = {
673 .qs_mutex = &pf->avail_q_mutex,
674 .pf_map = pf->avail_rxqs,
675 .pf_map_size = pf->max_pf_rxqs,
676 .q_count = vsi->alloc_rxq,
677 .scatter_count = ICE_MAX_SCATTER_RXQS,
678 .vsi_map = vsi->rxq_map,
680 .mapping_mode = ICE_VSI_MAP_CONTIG
684 if (vsi->type == ICE_VSI_CHNL)
687 ret = __ice_vsi_get_qs(&tx_qs_cfg);
690 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
692 ret = __ice_vsi_get_qs(&rx_qs_cfg);
695 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
701 * ice_vsi_put_qs - Release queues from VSI to PF
702 * @vsi: the VSI that is going to release queues
704 static void ice_vsi_put_qs(struct ice_vsi *vsi)
706 struct ice_pf *pf = vsi->back;
709 mutex_lock(&pf->avail_q_mutex);
711 ice_for_each_alloc_txq(vsi, i) {
712 clear_bit(vsi->txq_map[i], pf->avail_txqs);
713 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
716 ice_for_each_alloc_rxq(vsi, i) {
717 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
718 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
721 mutex_unlock(&pf->avail_q_mutex);
726 * @pf: pointer to the PF struct
728 * returns true if driver is in safe mode, false otherwise
730 bool ice_is_safe_mode(struct ice_pf *pf)
732 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
737 * @pf: pointer to the PF struct
739 * returns true if AUX devices/drivers are supported, false otherwise
741 bool ice_is_aux_ena(struct ice_pf *pf)
743 return test_bit(ICE_FLAG_AUX_ENA, pf->flags);
747 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
748 * @vsi: the VSI being cleaned up
750 * This function deletes RSS input set for all flows that were configured
753 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
755 struct ice_pf *pf = vsi->back;
758 if (ice_is_safe_mode(pf))
761 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
763 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
764 vsi->vsi_num, status);
768 * ice_rss_clean - Delete RSS related VSI structures and configuration
769 * @vsi: the VSI being removed
771 static void ice_rss_clean(struct ice_vsi *vsi)
773 struct ice_pf *pf = vsi->back;
776 dev = ice_pf_to_dev(pf);
778 if (vsi->rss_hkey_user)
779 devm_kfree(dev, vsi->rss_hkey_user);
780 if (vsi->rss_lut_user)
781 devm_kfree(dev, vsi->rss_lut_user);
783 ice_vsi_clean_rss_flow_fld(vsi);
784 /* remove RSS replay list */
785 if (!ice_is_safe_mode(pf))
786 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
790 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
791 * @vsi: the VSI being configured
793 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
795 struct ice_hw_common_caps *cap;
796 struct ice_pf *pf = vsi->back;
798 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
803 cap = &pf->hw.func_caps.common_cap;
807 /* PF VSI will inherit RSS instance of PF */
808 vsi->rss_table_size = (u16)cap->rss_table_size;
809 if (vsi->type == ICE_VSI_CHNL)
810 vsi->rss_size = min_t(u16, vsi->num_rxq,
811 BIT(cap->rss_table_entry_width));
813 vsi->rss_size = min_t(u16, num_online_cpus(),
814 BIT(cap->rss_table_entry_width));
815 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
817 case ICE_VSI_SWITCHDEV_CTRL:
818 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
819 vsi->rss_size = min_t(u16, num_online_cpus(),
820 BIT(cap->rss_table_entry_width));
821 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
824 /* VF VSI will get a small RSS table.
825 * For VSI_LUT, LUT size should be set to 64 bytes.
827 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
828 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
829 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
834 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
835 ice_vsi_type_str(vsi->type));
841 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
842 * @ctxt: the VSI context being set
844 * This initializes a default VSI context for all sections except the Queues.
846 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
850 memset(&ctxt->info, 0, sizeof(ctxt->info));
851 /* VSI's should be allocated from shared pool */
852 ctxt->alloc_from_pool = true;
853 /* Src pruning enabled by default */
854 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
855 /* Traffic from VSI can be sent to LAN */
856 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
857 /* By default bits 3 and 4 in inner_vlan_flags are 0's which results in legacy
858 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
859 * packets untagged/tagged.
861 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
862 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
863 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
864 /* Have 1:1 UP mapping for both ingress/egress tables */
865 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
866 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
867 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
868 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
869 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
870 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
871 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
872 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
873 ctxt->info.ingress_table = cpu_to_le32(table);
874 ctxt->info.egress_table = cpu_to_le32(table);
875 /* Have 1:1 UP mapping for outer to inner UP table */
876 ctxt->info.outer_up_table = cpu_to_le32(table);
877 /* No Outer tag support outer_tag_flags remains to zero */
881 * ice_vsi_setup_q_map - Setup a VSI queue map
882 * @vsi: the VSI being configured
883 * @ctxt: VSI context structure
885 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
887 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
888 u16 num_txq_per_tc, num_rxq_per_tc;
889 u16 qcount_tx = vsi->alloc_txq;
890 u16 qcount_rx = vsi->alloc_rxq;
894 if (!vsi->tc_cfg.numtc) {
895 /* at least TC0 should be enabled by default */
896 vsi->tc_cfg.numtc = 1;
897 vsi->tc_cfg.ena_tc = 1;
900 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
903 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
907 /* find the (rounded up) power-of-2 of qcount */
908 pow = (u16)order_base_2(num_rxq_per_tc);
910 /* TC mapping is a function of the number of Rx queues assigned to the
911 * VSI for each traffic class and the offset of these queues.
912 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
913 * queues allocated to TC0. No:of queues is a power-of-2.
915 * If TC is not enabled, the queue offset is set to 0, and allocate one
916 * queue, this way, traffic for the given TC will be sent to the default
919 * Setup number and offset of Rx queues for all TCs for the VSI
921 ice_for_each_traffic_class(i) {
922 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
923 /* TC is not enabled */
924 vsi->tc_cfg.tc_info[i].qoffset = 0;
925 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
926 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
927 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
928 ctxt->info.tc_mapping[i] = 0;
933 vsi->tc_cfg.tc_info[i].qoffset = offset;
934 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
935 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
936 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
938 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
939 ICE_AQ_VSI_TC_Q_OFFSET_M) |
940 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
941 ICE_AQ_VSI_TC_Q_NUM_M);
942 offset += num_rxq_per_tc;
943 tx_count += num_txq_per_tc;
944 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
947 /* if offset is non-zero, means it is calculated correctly based on
948 * enabled TCs for a given VSI otherwise qcount_rx will always
949 * be correct and non-zero because it is based off - VSI's
950 * allocated Rx queues which is at least 1 (hence qcount_tx will be
954 vsi->num_rxq = offset;
956 vsi->num_rxq = num_rxq_per_tc;
958 vsi->num_txq = tx_count;
960 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
961 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
962 /* since there is a chance that num_rxq could have been changed
963 * in the above for loop, make num_txq equal to num_rxq.
965 vsi->num_txq = vsi->num_rxq;
968 /* Rx queue mapping */
969 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
970 /* q_mapping buffer holds the info for the first queue allocated for
971 * this VSI in the PF space and also the number of queues associated
974 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
975 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
979 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
980 * @ctxt: the VSI context being set
981 * @vsi: the VSI being configured
983 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
985 u8 dflt_q_group, dflt_q_prio;
986 u16 dflt_q, report_q, val;
988 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
989 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
992 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
993 ctxt->info.valid_sections |= cpu_to_le16(val);
999 /* enable flow director filtering/programming */
1000 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1001 ctxt->info.fd_options = cpu_to_le16(val);
1002 /* max of allocated flow director filters */
1003 ctxt->info.max_fd_fltr_dedicated =
1004 cpu_to_le16(vsi->num_gfltr);
1005 /* max of shared flow director filters any VSI may program */
1006 ctxt->info.max_fd_fltr_shared =
1007 cpu_to_le16(vsi->num_bfltr);
1008 /* default queue index within the VSI of the default FD */
1009 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
1010 ICE_AQ_VSI_FD_DEF_Q_M);
1011 /* target queue or queue group to the FD filter */
1012 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
1013 ICE_AQ_VSI_FD_DEF_GRP_M);
1014 ctxt->info.fd_def_q = cpu_to_le16(val);
1015 /* queue index on which FD filter completion is reported */
1016 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
1017 ICE_AQ_VSI_FD_REPORT_Q_M);
1018 /* priority of the default qindex action */
1019 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
1020 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
1021 ctxt->info.fd_report_opt = cpu_to_le16(val);
1025 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1026 * @ctxt: the VSI context being set
1027 * @vsi: the VSI being configured
1029 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1031 u8 lut_type, hash_type;
1036 dev = ice_pf_to_dev(pf);
1038 switch (vsi->type) {
1041 /* PF VSI will inherit RSS instance of PF */
1042 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1043 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1046 /* VF VSI will gets a small RSS table which is a VSI LUT type */
1047 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1048 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1051 dev_dbg(dev, "Unsupported VSI type %s\n",
1052 ice_vsi_type_str(vsi->type));
1056 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1057 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1058 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1059 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1063 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1065 struct ice_pf *pf = vsi->back;
1070 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1072 pow = order_base_2(qcount);
1073 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1074 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1075 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1076 ICE_AQ_VSI_TC_Q_NUM_M);
1078 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1079 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1080 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1081 ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1085 * ice_vsi_init - Create and initialize a VSI
1086 * @vsi: the VSI being configured
1087 * @init_vsi: is this call creating a VSI
1089 * This initializes a VSI context depending on the VSI type to be added and
1090 * passes it down to the add_vsi aq command to create a new VSI.
1092 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
1094 struct ice_pf *pf = vsi->back;
1095 struct ice_hw *hw = &pf->hw;
1096 struct ice_vsi_ctx *ctxt;
1100 dev = ice_pf_to_dev(pf);
1101 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1105 switch (vsi->type) {
1109 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1111 case ICE_VSI_SWITCHDEV_CTRL:
1113 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1116 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1117 /* VF number here is the absolute VF number (0-255) */
1118 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
1125 /* Handle VLAN pruning for channel VSI if main VSI has VLAN
1128 if (vsi->type == ICE_VSI_CHNL) {
1129 struct ice_vsi *main_vsi;
1131 main_vsi = ice_get_main_vsi(pf);
1132 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1133 ctxt->info.sw_flags2 |=
1134 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1136 ctxt->info.sw_flags2 &=
1137 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1140 ice_set_dflt_vsi_ctx(ctxt);
1141 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1142 ice_set_fd_vsi_ctx(ctxt, vsi);
1143 /* if the switch is in VEB mode, allow VSI loopback */
1144 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1145 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1147 /* Set LUT type and HASH type if RSS is enabled */
1148 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1149 vsi->type != ICE_VSI_CTRL) {
1150 ice_set_rss_vsi_ctx(ctxt, vsi);
1151 /* if updating VSI context, make sure to set valid_section:
1152 * to indicate which section of VSI context being updated
1155 ctxt->info.valid_sections |=
1156 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1159 ctxt->info.sw_id = vsi->port_info->sw_id;
1160 if (vsi->type == ICE_VSI_CHNL) {
1161 ice_chnl_vsi_setup_q_map(vsi, ctxt);
1163 ice_vsi_setup_q_map(vsi, ctxt);
1164 if (!init_vsi) /* means VSI being updated */
1165 /* must to indicate which section of VSI context are
1168 ctxt->info.valid_sections |=
1169 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1172 /* Allow control frames out of main VSI */
1173 if (vsi->type == ICE_VSI_PF) {
1174 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1175 ctxt->info.valid_sections |=
1176 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1180 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1182 dev_err(dev, "Add VSI failed, err %d\n", ret);
1187 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1189 dev_err(dev, "Update VSI failed, err %d\n", ret);
1195 /* keep context for update VSI operations */
1196 vsi->info = ctxt->info;
1198 /* record VSI number returned */
1199 vsi->vsi_num = ctxt->vsi_num;
1207 * ice_free_res - free a block of resources
1208 * @res: pointer to the resource
1209 * @index: starting index previously returned by ice_get_res
1210 * @id: identifier to track owner
1212 * Returns number of resources freed
1214 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1219 if (!res || index >= res->end)
1222 id |= ICE_RES_VALID_BIT;
1223 for (i = index; i < res->end && res->list[i] == id; i++) {
1232 * ice_search_res - Search the tracker for a block of resources
1233 * @res: pointer to the resource
1234 * @needed: size of the block needed
1235 * @id: identifier to track owner
1237 * Returns the base item index of the block, or -ENOMEM for error
1239 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1241 u16 start = 0, end = 0;
1243 if (needed > res->end)
1246 id |= ICE_RES_VALID_BIT;
1249 /* skip already allocated entries */
1250 if (res->list[end++] & ICE_RES_VALID_BIT) {
1252 if ((start + needed) > res->end)
1256 if (end == (start + needed)) {
1259 /* there was enough, so assign it to the requestor */
1261 res->list[i++] = id;
1265 } while (end < res->end);
1271 * ice_get_free_res_count - Get free count from a resource tracker
1272 * @res: Resource tracker instance
1274 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1278 for (i = 0; i < res->end; i++)
1279 if (!(res->list[i] & ICE_RES_VALID_BIT))
1286 * ice_get_res - get a block of resources
1287 * @pf: board private structure
1288 * @res: pointer to the resource
1289 * @needed: size of the block needed
1290 * @id: identifier to track owner
1292 * Returns the base item index of the block, or negative for error
1295 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1300 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1301 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1302 needed, res->num_entries, id);
1306 return ice_search_res(res, needed, id);
1310 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1311 * @vsi: ptr to the VSI
1313 * This should only be called after ice_vsi_alloc() which allocates the
1314 * corresponding SW VSI structure and initializes num_queue_pairs for the
1315 * newly allocated VSI.
1317 * Returns 0 on success or negative on failure
1319 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1321 struct ice_pf *pf = vsi->back;
1326 dev = ice_pf_to_dev(pf);
1327 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1328 if (vsi->type == ICE_VSI_VF)
1330 if (vsi->type == ICE_VSI_CHNL)
1333 if (vsi->base_vector) {
1334 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1335 vsi->vsi_num, vsi->base_vector);
1339 num_q_vectors = vsi->num_q_vectors;
1340 /* reserve slots from OS requested IRQs */
1341 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1344 ice_for_each_vf(pf, i) {
1345 struct ice_vf *vf = &pf->vf[i];
1347 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1348 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1352 if (i == pf->num_alloc_vfs)
1353 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1354 ICE_RES_VF_CTRL_VEC_ID);
1356 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1361 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1362 ice_get_free_res_count(pf->irq_tracker),
1363 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1366 vsi->base_vector = (u16)base;
1367 pf->num_avail_sw_msix -= num_q_vectors;
1373 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1374 * @vsi: the VSI having rings deallocated
1376 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1380 /* Avoid stale references by clearing map from vector to ring */
1381 if (vsi->q_vectors) {
1382 ice_for_each_q_vector(vsi, i) {
1383 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1386 q_vector->tx.tx_ring = NULL;
1387 q_vector->rx.rx_ring = NULL;
1392 if (vsi->tx_rings) {
1393 ice_for_each_alloc_txq(vsi, i) {
1394 if (vsi->tx_rings[i]) {
1395 kfree_rcu(vsi->tx_rings[i], rcu);
1396 WRITE_ONCE(vsi->tx_rings[i], NULL);
1400 if (vsi->rx_rings) {
1401 ice_for_each_alloc_rxq(vsi, i) {
1402 if (vsi->rx_rings[i]) {
1403 kfree_rcu(vsi->rx_rings[i], rcu);
1404 WRITE_ONCE(vsi->rx_rings[i], NULL);
1411 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1412 * @vsi: VSI which is having rings allocated
1414 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1416 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1417 struct ice_pf *pf = vsi->back;
1421 dev = ice_pf_to_dev(pf);
1422 /* Allocate Tx rings */
1423 ice_for_each_alloc_txq(vsi, i) {
1424 struct ice_tx_ring *ring;
1426 /* allocate with kzalloc(), free with kfree_rcu() */
1427 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1433 ring->reg_idx = vsi->txq_map[i];
1435 ring->tx_tstamps = &pf->ptp.port.tx;
1437 ring->count = vsi->num_tx_desc;
1439 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1441 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1442 WRITE_ONCE(vsi->tx_rings[i], ring);
1445 /* Allocate Rx rings */
1446 ice_for_each_alloc_rxq(vsi, i) {
1447 struct ice_rx_ring *ring;
1449 /* allocate with kzalloc(), free with kfree_rcu() */
1450 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1455 ring->reg_idx = vsi->rxq_map[i];
1457 ring->netdev = vsi->netdev;
1459 ring->count = vsi->num_rx_desc;
1460 WRITE_ONCE(vsi->rx_rings[i], ring);
1466 ice_vsi_clear_rings(vsi);
1471 * ice_vsi_manage_rss_lut - disable/enable RSS
1472 * @vsi: the VSI being changed
1473 * @ena: boolean value indicating if this is an enable or disable request
1475 * In the event of disable request for RSS, this function will zero out RSS
1476 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1479 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1483 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1488 if (vsi->rss_lut_user)
1489 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1491 ice_fill_rss_lut(lut, vsi->rss_table_size,
1495 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1500 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1501 * @vsi: VSI to be configured
1503 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1505 struct ice_pf *pf = vsi->back;
1510 dev = ice_pf_to_dev(pf);
1511 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1512 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1513 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1515 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1517 /* If orig_rss_size is valid and it is less than determined
1518 * main VSI's rss_size, update main VSI's rss_size to be
1519 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1520 * RSS table gets programmed to be correct (whatever it was
1521 * to begin with (prior to setup-tc for ADQ config)
1523 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1524 vsi->orig_rss_size <= vsi->num_rxq) {
1525 vsi->rss_size = vsi->orig_rss_size;
1526 /* now orig_rss_size is used, reset it to zero */
1527 vsi->orig_rss_size = 0;
1531 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1535 if (vsi->rss_lut_user)
1536 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1538 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1540 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1542 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1543 goto ice_vsi_cfg_rss_exit;
1546 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1549 goto ice_vsi_cfg_rss_exit;
1552 if (vsi->rss_hkey_user)
1553 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1555 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1557 err = ice_set_rss_key(vsi, key);
1559 dev_err(dev, "set_rss_key failed, error %d\n", err);
1562 ice_vsi_cfg_rss_exit:
1568 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1569 * @vsi: VSI to be configured
1571 * This function will only be called during the VF VSI setup. Upon successful
1572 * completion of package download, this function will configure default RSS
1573 * input sets for VF VSI.
1575 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1577 struct ice_pf *pf = vsi->back;
1581 dev = ice_pf_to_dev(pf);
1582 if (ice_is_safe_mode(pf)) {
1583 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1588 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1590 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1591 vsi->vsi_num, status);
1595 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1596 * @vsi: VSI to be configured
1598 * This function will only be called after successful download package call
1599 * during initialization of PF. Since the downloaded package will erase the
1600 * RSS section, this function will configure RSS input sets for different
1601 * flow types. The last profile added has the highest priority, therefore 2
1602 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1603 * (i.e. IPv4 src/dst TCP src/dst port).
1605 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1607 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1608 struct ice_pf *pf = vsi->back;
1609 struct ice_hw *hw = &pf->hw;
1613 dev = ice_pf_to_dev(pf);
1614 if (ice_is_safe_mode(pf)) {
1615 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1619 /* configure RSS for IPv4 with input set IP src/dst */
1620 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1621 ICE_FLOW_SEG_HDR_IPV4);
1623 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1626 /* configure RSS for IPv6 with input set IPv6 src/dst */
1627 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1628 ICE_FLOW_SEG_HDR_IPV6);
1630 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1633 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1634 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1635 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1637 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1640 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1641 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1642 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1644 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1647 /* configure RSS for sctp4 with input set IP src/dst */
1648 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1649 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1651 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1654 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1655 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1656 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1658 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1661 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1662 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1663 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1665 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1668 /* configure RSS for sctp6 with input set IPv6 src/dst */
1669 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1670 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1672 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1677 * ice_pf_state_is_nominal - checks the PF for nominal state
1678 * @pf: pointer to PF to check
1680 * Check the PF's state for a collection of bits that would indicate
1681 * the PF is in a state that would inhibit normal operation for
1682 * driver functionality.
1684 * Returns true if PF is in a nominal state, false otherwise
1686 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1688 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1693 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1694 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1701 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1702 * @vsi: the VSI to be updated
1704 void ice_update_eth_stats(struct ice_vsi *vsi)
1706 struct ice_eth_stats *prev_es, *cur_es;
1707 struct ice_hw *hw = &vsi->back->hw;
1708 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1710 prev_es = &vsi->eth_stats_prev;
1711 cur_es = &vsi->eth_stats;
1713 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1714 &prev_es->rx_bytes, &cur_es->rx_bytes);
1716 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1717 &prev_es->rx_unicast, &cur_es->rx_unicast);
1719 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1720 &prev_es->rx_multicast, &cur_es->rx_multicast);
1722 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1723 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1725 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1726 &prev_es->rx_discards, &cur_es->rx_discards);
1728 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1729 &prev_es->tx_bytes, &cur_es->tx_bytes);
1731 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1732 &prev_es->tx_unicast, &cur_es->tx_unicast);
1734 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1735 &prev_es->tx_multicast, &cur_es->tx_multicast);
1737 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1738 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1740 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1741 &prev_es->tx_errors, &cur_es->tx_errors);
1743 vsi->stat_offsets_loaded = true;
1747 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1750 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1752 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1753 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1754 vsi->rx_buf_len = ICE_RXBUF_2048;
1755 #if (PAGE_SIZE < 8192)
1756 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1757 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1758 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1759 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1762 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1763 #if (PAGE_SIZE < 8192)
1764 vsi->rx_buf_len = ICE_RXBUF_3072;
1766 vsi->rx_buf_len = ICE_RXBUF_2048;
1772 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1774 * @pf_q: index of the Rx queue in the PF's queue space
1775 * @rxdid: flexible descriptor RXDID
1776 * @prio: priority for the RXDID for this queue
1777 * @ena_ts: true to enable timestamp and false to disable timestamp
1780 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1783 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1785 /* clear any previous values */
1786 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1787 QRXFLXP_CNTXT_RXDID_PRIO_M |
1788 QRXFLXP_CNTXT_TS_M);
1790 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1791 QRXFLXP_CNTXT_RXDID_IDX_M;
1793 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1794 QRXFLXP_CNTXT_RXDID_PRIO_M;
1797 /* Enable TimeSync on this queue */
1798 regval |= QRXFLXP_CNTXT_TS_M;
1800 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1803 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1805 if (q_idx >= vsi->num_rxq)
1808 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1811 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1813 struct ice_aqc_add_tx_qgrp *qg_buf;
1816 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1819 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1823 qg_buf->num_txqs = 1;
1825 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1831 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1832 * @vsi: the VSI being configured
1834 * Return 0 on success and a negative value on error
1835 * Configure the Rx VSI for operation.
1837 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1841 if (vsi->type == ICE_VSI_VF)
1844 ice_vsi_cfg_frame_size(vsi);
1846 /* set up individual rings */
1847 ice_for_each_rxq(vsi, i) {
1848 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1858 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1859 * @vsi: the VSI being configured
1860 * @rings: Tx ring array to be configured
1861 * @count: number of Tx ring array elements
1863 * Return 0 on success and a negative value on error
1864 * Configure the Tx VSI for operation.
1867 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1869 struct ice_aqc_add_tx_qgrp *qg_buf;
1873 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1877 qg_buf->num_txqs = 1;
1879 for (q_idx = 0; q_idx < count; q_idx++) {
1880 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1891 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1892 * @vsi: the VSI being configured
1894 * Return 0 on success and a negative value on error
1895 * Configure the Tx VSI for operation.
1897 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1899 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1903 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1904 * @vsi: the VSI being configured
1906 * Return 0 on success and a negative value on error
1907 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1909 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1914 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1918 ice_for_each_xdp_txq(vsi, i)
1919 vsi->xdp_rings[i]->xsk_pool = ice_tx_xsk_pool(vsi->xdp_rings[i]);
1925 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1926 * @intrl: interrupt rate limit in usecs
1927 * @gran: interrupt rate limit granularity in usecs
1929 * This function converts a decimal interrupt rate limit in usecs to the format
1930 * expected by firmware.
1932 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1934 u32 val = intrl / gran;
1937 return val | GLINT_RATE_INTRL_ENA_M;
1942 * ice_write_intrl - write throttle rate limit to interrupt specific register
1943 * @q_vector: pointer to interrupt specific structure
1944 * @intrl: throttle rate limit in microseconds to write
1946 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1948 struct ice_hw *hw = &q_vector->vsi->back->hw;
1950 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1951 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1954 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1957 case ICE_RX_CONTAINER:
1959 return rc->rx_ring->q_vector;
1961 case ICE_TX_CONTAINER:
1963 return rc->tx_ring->q_vector;
1973 * __ice_write_itr - write throttle rate to register
1974 * @q_vector: pointer to interrupt data structure
1975 * @rc: pointer to ring container
1976 * @itr: throttle rate in microseconds to write
1978 static void __ice_write_itr(struct ice_q_vector *q_vector,
1979 struct ice_ring_container *rc, u16 itr)
1981 struct ice_hw *hw = &q_vector->vsi->back->hw;
1983 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1984 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1988 * ice_write_itr - write throttle rate to queue specific register
1989 * @rc: pointer to ring container
1990 * @itr: throttle rate in microseconds to write
1992 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1994 struct ice_q_vector *q_vector;
1996 q_vector = ice_pull_qvec_from_rc(rc);
2000 __ice_write_itr(q_vector, rc, itr);
2004 * ice_set_q_vector_intrl - set up interrupt rate limiting
2005 * @q_vector: the vector to be configured
2007 * Interrupt rate limiting is local to the vector, not per-queue so we must
2008 * detect if either ring container has dynamic moderation enabled to decide
2009 * what to set the interrupt rate limit to via INTRL settings. In the case that
2010 * dynamic moderation is disabled on both, write the value with the cached
2011 * setting to make sure INTRL register matches the user visible value.
2013 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2015 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2016 /* in the case of dynamic enabled, cap each vector to no more
2017 * than (4 us) 250,000 ints/sec, which allows low latency
2018 * but still less than 500,000 interrupts per second, which
2019 * reduces CPU a bit in the case of the lowest latency
2020 * setting. The 4 here is a value in microseconds.
2022 ice_write_intrl(q_vector, 4);
2024 ice_write_intrl(q_vector, q_vector->intrl);
2029 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2030 * @vsi: the VSI being configured
2032 * This configures MSIX mode interrupts for the PF VSI, and should not be used
2035 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2037 struct ice_pf *pf = vsi->back;
2038 struct ice_hw *hw = &pf->hw;
2039 u16 txq = 0, rxq = 0;
2042 ice_for_each_q_vector(vsi, i) {
2043 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2044 u16 reg_idx = q_vector->reg_idx;
2046 ice_cfg_itr(hw, q_vector);
2048 /* Both Transmit Queue Interrupt Cause Control register
2049 * and Receive Queue Interrupt Cause control register
2050 * expects MSIX_INDX field to be the vector index
2051 * within the function space and not the absolute
2052 * vector index across PF or across device.
2053 * For SR-IOV VF VSIs queue vector index always starts
2054 * with 1 since first vector index(0) is used for OICR
2055 * in VF space. Since VMDq and other PF VSIs are within
2056 * the PF function space, use the vector index that is
2057 * tracked for this PF.
2059 for (q = 0; q < q_vector->num_ring_tx; q++) {
2060 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2061 q_vector->tx.itr_idx);
2065 for (q = 0; q < q_vector->num_ring_rx; q++) {
2066 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2067 q_vector->rx.itr_idx);
2074 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2075 * @vsi: the VSI whose rings are to be enabled
2077 * Returns 0 on success and a negative value on error
2079 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2081 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2085 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2086 * @vsi: the VSI whose rings are to be disabled
2088 * Returns 0 on success and a negative value on error
2090 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2092 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2096 * ice_vsi_stop_tx_rings - Disable Tx rings
2097 * @vsi: the VSI being configured
2098 * @rst_src: reset source
2099 * @rel_vmvf_num: Relative ID of VF/VM
2100 * @rings: Tx ring array to be stopped
2101 * @count: number of Tx ring array elements
2104 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2105 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2109 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2112 for (q_idx = 0; q_idx < count; q_idx++) {
2113 struct ice_txq_meta txq_meta = { };
2116 if (!rings || !rings[q_idx])
2119 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2120 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2121 rings[q_idx], &txq_meta);
2131 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2132 * @vsi: the VSI being configured
2133 * @rst_src: reset source
2134 * @rel_vmvf_num: Relative ID of VF/VM
2137 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2140 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2144 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2145 * @vsi: the VSI being configured
2147 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2149 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2153 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2154 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2156 * returns true if Rx VLAN pruning is enabled and false otherwise.
2158 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2163 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2166 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2168 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2169 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2170 vsi->tc_cfg.numtc = 1;
2174 /* set VSI TC information based on DCB config */
2175 ice_vsi_set_dcb_tc_cfg(vsi);
2179 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2180 * @vsi: VSI to set the q_vectors register index on
2183 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2187 if (!vsi || !vsi->q_vectors)
2190 ice_for_each_q_vector(vsi, i) {
2191 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2194 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2199 if (vsi->type == ICE_VSI_VF) {
2200 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2202 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2205 q_vector->v_idx + vsi->base_vector;
2212 ice_for_each_q_vector(vsi, i) {
2213 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2216 q_vector->reg_idx = 0;
2223 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2224 * @vsi: the VSI being configured
2225 * @tx: bool to determine Tx or Rx rule
2226 * @create: bool to determine create or remove Rule
2228 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2230 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2231 enum ice_sw_fwd_act_type act);
2232 struct ice_pf *pf = vsi->back;
2236 dev = ice_pf_to_dev(pf);
2237 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2240 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2243 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2244 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2247 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2253 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2254 create ? "adding" : "removing", tx ? "TX" : "RX",
2255 vsi->vsi_num, status);
2259 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2260 * @vsi: pointer to the VSI
2262 * This function will allocate new scheduler aggregator now if needed and will
2263 * move specified VSI into it.
2265 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2267 struct device *dev = ice_pf_to_dev(vsi->back);
2268 struct ice_agg_node *agg_node_iter = NULL;
2269 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2270 struct ice_agg_node *agg_node = NULL;
2271 int node_offset, max_agg_nodes = 0;
2272 struct ice_port_info *port_info;
2273 struct ice_pf *pf = vsi->back;
2274 u32 agg_node_id_start = 0;
2277 /* create (as needed) scheduler aggregator node and move VSI into
2278 * corresponding aggregator node
2279 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2280 * - VF aggregator nodes will contain VF VSI
2282 port_info = pf->hw.port_info;
2286 switch (vsi->type) {
2291 case ICE_VSI_SWITCHDEV_CTRL:
2292 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2293 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2294 agg_node_iter = &pf->pf_agg_node[0];
2297 /* user can create 'n' VFs on a given PF, but since max children
2298 * per aggregator node can be only 64. Following code handles
2299 * aggregator(s) for VF VSIs, either selects a agg_node which
2300 * was already created provided num_vsis < 64, otherwise
2301 * select next available node, which will be created
2303 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2304 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2305 agg_node_iter = &pf->vf_agg_node[0];
2308 /* other VSI type, handle later if needed */
2309 dev_dbg(dev, "unexpected VSI type %s\n",
2310 ice_vsi_type_str(vsi->type));
2314 /* find the appropriate aggregator node */
2315 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2316 /* see if we can find space in previously created
2317 * node if num_vsis < 64, otherwise skip
2319 if (agg_node_iter->num_vsis &&
2320 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2325 if (agg_node_iter->valid &&
2326 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2327 agg_id = agg_node_iter->agg_id;
2328 agg_node = agg_node_iter;
2332 /* find unclaimed agg_id */
2333 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2334 agg_id = node_offset + agg_node_id_start;
2335 agg_node = agg_node_iter;
2338 /* move to next agg_node */
2345 /* if selected aggregator node was not created, create it */
2346 if (!agg_node->valid) {
2347 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2348 (u8)vsi->tc_cfg.ena_tc);
2350 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2354 /* aggregator node is created, store the neeeded info */
2355 agg_node->valid = true;
2356 agg_node->agg_id = agg_id;
2359 /* move VSI to corresponding aggregator node */
2360 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2361 (u8)vsi->tc_cfg.ena_tc);
2363 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2368 /* keep active children count for aggregator node */
2369 agg_node->num_vsis++;
2371 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2372 * to aggregator node
2374 vsi->agg_node = agg_node;
2375 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2376 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2377 vsi->agg_node->num_vsis);
2381 * ice_vsi_setup - Set up a VSI by a given type
2382 * @pf: board private structure
2383 * @pi: pointer to the port_info instance
2384 * @vsi_type: VSI type
2385 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2386 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2387 * fill-in ICE_INVAL_VFID as input.
2388 * @ch: ptr to channel
2390 * This allocates the sw VSI structure and its queue resources.
2392 * Returns pointer to the successfully allocated and configured VSI sw struct on
2393 * success, NULL on failure.
2396 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2397 enum ice_vsi_type vsi_type, u16 vf_id, struct ice_channel *ch)
2399 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2400 struct device *dev = ice_pf_to_dev(pf);
2401 struct ice_vsi *vsi;
2404 if (vsi_type == ICE_VSI_CHNL)
2405 vsi = ice_vsi_alloc(pf, vsi_type, ch, ICE_INVAL_VFID);
2406 else if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2407 vsi = ice_vsi_alloc(pf, vsi_type, NULL, vf_id);
2409 vsi = ice_vsi_alloc(pf, vsi_type, NULL, ICE_INVAL_VFID);
2412 dev_err(dev, "could not allocate VSI\n");
2416 vsi->port_info = pi;
2417 vsi->vsw = pf->first_sw;
2418 if (vsi->type == ICE_VSI_PF)
2419 vsi->ethtype = ETH_P_PAUSE;
2421 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2424 ice_alloc_fd_res(vsi);
2426 if (vsi_type != ICE_VSI_CHNL) {
2427 if (ice_vsi_get_qs(vsi)) {
2428 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2430 goto unroll_vsi_alloc;
2434 /* set RSS capabilities */
2435 ice_vsi_set_rss_params(vsi);
2437 /* set TC configuration */
2438 ice_vsi_set_tc_cfg(vsi);
2440 /* create the VSI */
2441 ret = ice_vsi_init(vsi, true);
2445 ice_vsi_init_vlan_ops(vsi);
2447 switch (vsi->type) {
2449 case ICE_VSI_SWITCHDEV_CTRL:
2451 ret = ice_vsi_alloc_q_vectors(vsi);
2453 goto unroll_vsi_init;
2455 ret = ice_vsi_setup_vector_base(vsi);
2457 goto unroll_alloc_q_vector;
2459 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2461 goto unroll_vector_base;
2463 ret = ice_vsi_alloc_rings(vsi);
2465 goto unroll_vector_base;
2467 ice_vsi_map_rings_to_vectors(vsi);
2469 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2470 if (vsi->type != ICE_VSI_CTRL)
2471 /* Do not exit if configuring RSS had an issue, at
2472 * least receive traffic on first queue. Hence no
2473 * need to capture return value
2475 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2476 ice_vsi_cfg_rss_lut_key(vsi);
2477 ice_vsi_set_rss_flow_fld(vsi);
2482 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2483 ice_vsi_cfg_rss_lut_key(vsi);
2484 ice_vsi_set_rss_flow_fld(vsi);
2488 /* VF driver will take care of creating netdev for this type and
2489 * map queues to vectors through Virtchnl, PF driver only
2490 * creates a VSI and corresponding structures for bookkeeping
2493 ret = ice_vsi_alloc_q_vectors(vsi);
2495 goto unroll_vsi_init;
2497 ret = ice_vsi_alloc_rings(vsi);
2499 goto unroll_alloc_q_vector;
2501 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2503 goto unroll_vector_base;
2505 /* Do not exit if configuring RSS had an issue, at least
2506 * receive traffic on first queue. Hence no need to capture
2509 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2510 ice_vsi_cfg_rss_lut_key(vsi);
2511 ice_vsi_set_vf_rss_flow_fld(vsi);
2515 ret = ice_vsi_alloc_rings(vsi);
2517 goto unroll_vsi_init;
2520 /* clean up the resources and exit */
2521 goto unroll_vsi_init;
2524 /* configure VSI nodes based on number of queues and TC's */
2525 ice_for_each_traffic_class(i) {
2526 if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2529 if (vsi->type == ICE_VSI_CHNL) {
2530 if (!vsi->alloc_txq && vsi->num_txq)
2531 max_txqs[i] = vsi->num_txq;
2533 max_txqs[i] = pf->num_lan_tx;
2535 max_txqs[i] = vsi->alloc_txq;
2539 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2540 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2543 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2545 goto unroll_clear_rings;
2548 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2549 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2550 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2551 * The rule is added once for PF VSI in order to create appropriate
2552 * recipe, since VSI/VSI list is ignored with drop action...
2553 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2554 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2555 * settings in the HW.
2557 if (!ice_is_safe_mode(pf))
2558 if (vsi->type == ICE_VSI_PF) {
2559 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2561 ice_cfg_sw_lldp(vsi, true, true);
2565 ice_set_agg_vsi(vsi);
2569 ice_vsi_clear_rings(vsi);
2571 /* reclaim SW interrupts back to the common pool */
2572 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2573 pf->num_avail_sw_msix += vsi->num_q_vectors;
2574 unroll_alloc_q_vector:
2575 ice_vsi_free_q_vectors(vsi);
2577 ice_vsi_delete(vsi);
2579 ice_vsi_put_qs(vsi);
2581 if (vsi_type == ICE_VSI_VF)
2582 ice_enable_lag(pf->lag);
2589 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2590 * @vsi: the VSI being cleaned up
2592 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2594 struct ice_pf *pf = vsi->back;
2595 struct ice_hw *hw = &pf->hw;
2600 ice_for_each_q_vector(vsi, i) {
2601 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2603 ice_write_intrl(q_vector, 0);
2604 for (q = 0; q < q_vector->num_ring_tx; q++) {
2605 ice_write_itr(&q_vector->tx, 0);
2606 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2607 if (ice_is_xdp_ena_vsi(vsi)) {
2608 u32 xdp_txq = txq + vsi->num_xdp_txq;
2610 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2615 for (q = 0; q < q_vector->num_ring_rx; q++) {
2616 ice_write_itr(&q_vector->rx, 0);
2617 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2626 * ice_vsi_free_irq - Free the IRQ association with the OS
2627 * @vsi: the VSI being configured
2629 void ice_vsi_free_irq(struct ice_vsi *vsi)
2631 struct ice_pf *pf = vsi->back;
2632 int base = vsi->base_vector;
2635 if (!vsi->q_vectors || !vsi->irqs_ready)
2638 ice_vsi_release_msix(vsi);
2639 if (vsi->type == ICE_VSI_VF)
2642 vsi->irqs_ready = false;
2643 ice_for_each_q_vector(vsi, i) {
2644 u16 vector = i + base;
2647 irq_num = pf->msix_entries[vector].vector;
2649 /* free only the irqs that were actually requested */
2650 if (!vsi->q_vectors[i] ||
2651 !(vsi->q_vectors[i]->num_ring_tx ||
2652 vsi->q_vectors[i]->num_ring_rx))
2655 /* clear the affinity notifier in the IRQ descriptor */
2656 irq_set_affinity_notifier(irq_num, NULL);
2658 /* clear the affinity_mask in the IRQ descriptor */
2659 irq_set_affinity_hint(irq_num, NULL);
2660 synchronize_irq(irq_num);
2661 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2666 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2667 * @vsi: the VSI having resources freed
2669 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2676 ice_for_each_txq(vsi, i)
2677 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2678 ice_free_tx_ring(vsi->tx_rings[i]);
2682 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2683 * @vsi: the VSI having resources freed
2685 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2692 ice_for_each_rxq(vsi, i)
2693 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2694 ice_free_rx_ring(vsi->rx_rings[i]);
2698 * ice_vsi_close - Shut down a VSI
2699 * @vsi: the VSI being shut down
2701 void ice_vsi_close(struct ice_vsi *vsi)
2703 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2706 ice_vsi_free_irq(vsi);
2707 ice_vsi_free_tx_rings(vsi);
2708 ice_vsi_free_rx_rings(vsi);
2712 * ice_ena_vsi - resume a VSI
2713 * @vsi: the VSI being resume
2714 * @locked: is the rtnl_lock already held
2716 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2720 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2723 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2725 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2726 if (netif_running(vsi->netdev)) {
2730 err = ice_open_internal(vsi->netdev);
2735 } else if (vsi->type == ICE_VSI_CTRL) {
2736 err = ice_vsi_open_ctrl(vsi);
2743 * ice_dis_vsi - pause a VSI
2744 * @vsi: the VSI being paused
2745 * @locked: is the rtnl_lock already held
2747 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2749 if (test_bit(ICE_VSI_DOWN, vsi->state))
2752 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2754 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2755 if (netif_running(vsi->netdev)) {
2766 } else if (vsi->type == ICE_VSI_CTRL ||
2767 vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2773 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2774 * @vsi: the VSI being un-configured
2776 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2778 int base = vsi->base_vector;
2779 struct ice_pf *pf = vsi->back;
2780 struct ice_hw *hw = &pf->hw;
2784 /* disable interrupt causation from each queue */
2785 if (vsi->tx_rings) {
2786 ice_for_each_txq(vsi, i) {
2787 if (vsi->tx_rings[i]) {
2790 reg = vsi->tx_rings[i]->reg_idx;
2791 val = rd32(hw, QINT_TQCTL(reg));
2792 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2793 wr32(hw, QINT_TQCTL(reg), val);
2798 if (vsi->rx_rings) {
2799 ice_for_each_rxq(vsi, i) {
2800 if (vsi->rx_rings[i]) {
2803 reg = vsi->rx_rings[i]->reg_idx;
2804 val = rd32(hw, QINT_RQCTL(reg));
2805 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2806 wr32(hw, QINT_RQCTL(reg), val);
2811 /* disable each interrupt */
2812 ice_for_each_q_vector(vsi, i) {
2813 if (!vsi->q_vectors[i])
2815 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2820 /* don't call synchronize_irq() for VF's from the host */
2821 if (vsi->type == ICE_VSI_VF)
2824 ice_for_each_q_vector(vsi, i)
2825 synchronize_irq(pf->msix_entries[i + base].vector);
2829 * ice_napi_del - Remove NAPI handler for the VSI
2830 * @vsi: VSI for which NAPI handler is to be removed
2832 void ice_napi_del(struct ice_vsi *vsi)
2839 ice_for_each_q_vector(vsi, v_idx)
2840 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2844 * ice_vsi_release - Delete a VSI and free its resources
2845 * @vsi: the VSI being removed
2847 * Returns 0 on success or < 0 on error
2849 int ice_vsi_release(struct ice_vsi *vsi)
2858 /* do not unregister while driver is in the reset recovery pending
2859 * state. Since reset/rebuild happens through PF service task workqueue,
2860 * it's not a good idea to unregister netdev that is associated to the
2861 * PF that is running the work queue items currently. This is done to
2862 * avoid check_flush_dependency() warning on this wq
2864 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2865 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2866 unregister_netdev(vsi->netdev);
2867 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2870 if (vsi->type == ICE_VSI_PF)
2871 ice_devlink_destroy_pf_port(pf);
2873 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2876 /* Disable VSI and free resources */
2877 if (vsi->type != ICE_VSI_LB)
2878 ice_vsi_dis_irq(vsi);
2881 /* SR-IOV determines needed MSIX resources all at once instead of per
2882 * VSI since when VFs are spawned we know how many VFs there are and how
2883 * many interrupts each VF needs. SR-IOV MSIX resources are also
2884 * cleared in the same manner.
2886 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2889 ice_for_each_vf(pf, i) {
2890 struct ice_vf *vf = &pf->vf[i];
2892 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2895 if (i == pf->num_alloc_vfs) {
2896 /* No other VFs left that have control VSI, reclaim SW
2897 * interrupts back to the common pool
2899 ice_free_res(pf->irq_tracker, vsi->base_vector,
2900 ICE_RES_VF_CTRL_VEC_ID);
2901 pf->num_avail_sw_msix += vsi->num_q_vectors;
2903 } else if (vsi->type != ICE_VSI_VF) {
2904 /* reclaim SW interrupts back to the common pool */
2905 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2906 pf->num_avail_sw_msix += vsi->num_q_vectors;
2909 if (!ice_is_safe_mode(pf)) {
2910 if (vsi->type == ICE_VSI_PF) {
2911 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2913 ice_cfg_sw_lldp(vsi, true, false);
2914 /* The Rx rule will only exist to remove if the LLDP FW
2915 * engine is currently stopped
2917 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2918 ice_cfg_sw_lldp(vsi, false, false);
2922 ice_fltr_remove_all(vsi);
2923 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2924 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2926 dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2928 ice_vsi_delete(vsi);
2929 ice_vsi_free_q_vectors(vsi);
2932 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2933 unregister_netdev(vsi->netdev);
2934 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2936 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2937 free_netdev(vsi->netdev);
2939 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2943 if (vsi->type == ICE_VSI_VF &&
2944 vsi->agg_node && vsi->agg_node->valid)
2945 vsi->agg_node->num_vsis--;
2946 ice_vsi_clear_rings(vsi);
2948 ice_vsi_put_qs(vsi);
2950 /* retain SW VSI data structure since it is needed to unregister and
2951 * free VSI netdev when PF is not in reset recovery pending state,\
2952 * for ex: during rmmod.
2954 if (!ice_is_reset_in_progress(pf->state))
2961 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2962 * @vsi: VSI connected with q_vectors
2963 * @coalesce: array of struct with stored coalesce
2965 * Returns array size.
2968 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2969 struct ice_coalesce_stored *coalesce)
2973 ice_for_each_q_vector(vsi, i) {
2974 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2976 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2977 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2978 coalesce[i].intrl = q_vector->intrl;
2980 if (i < vsi->num_txq)
2981 coalesce[i].tx_valid = true;
2982 if (i < vsi->num_rxq)
2983 coalesce[i].rx_valid = true;
2986 return vsi->num_q_vectors;
2990 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2991 * @vsi: VSI connected with q_vectors
2992 * @coalesce: pointer to array of struct with stored coalesce
2993 * @size: size of coalesce array
2995 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2996 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3000 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3001 struct ice_coalesce_stored *coalesce, int size)
3003 struct ice_ring_container *rc;
3006 if ((size && !coalesce) || !vsi)
3009 /* There are a couple of cases that have to be handled here:
3010 * 1. The case where the number of queue vectors stays the same, but
3011 * the number of Tx or Rx rings changes (the first for loop)
3012 * 2. The case where the number of queue vectors increased (the
3015 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3016 /* There are 2 cases to handle here and they are the same for
3018 * if the entry was valid previously (coalesce[i].[tr]x_valid
3019 * and the loop variable is less than the number of rings
3020 * allocated, then write the previous values
3022 * if the entry was not valid previously, but the number of
3023 * rings is less than are allocated (this means the number of
3024 * rings increased from previously), then write out the
3025 * values in the first element
3027 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3028 * as there is no harm because the dynamic algorithm
3029 * will just overwrite.
3031 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3032 rc = &vsi->q_vectors[i]->rx;
3033 rc->itr_setting = coalesce[i].itr_rx;
3034 ice_write_itr(rc, rc->itr_setting);
3035 } else if (i < vsi->alloc_rxq) {
3036 rc = &vsi->q_vectors[i]->rx;
3037 rc->itr_setting = coalesce[0].itr_rx;
3038 ice_write_itr(rc, rc->itr_setting);
3041 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3042 rc = &vsi->q_vectors[i]->tx;
3043 rc->itr_setting = coalesce[i].itr_tx;
3044 ice_write_itr(rc, rc->itr_setting);
3045 } else if (i < vsi->alloc_txq) {
3046 rc = &vsi->q_vectors[i]->tx;
3047 rc->itr_setting = coalesce[0].itr_tx;
3048 ice_write_itr(rc, rc->itr_setting);
3051 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3052 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3055 /* the number of queue vectors increased so write whatever is in
3058 for (; i < vsi->num_q_vectors; i++) {
3060 rc = &vsi->q_vectors[i]->tx;
3061 rc->itr_setting = coalesce[0].itr_tx;
3062 ice_write_itr(rc, rc->itr_setting);
3065 rc = &vsi->q_vectors[i]->rx;
3066 rc->itr_setting = coalesce[0].itr_rx;
3067 ice_write_itr(rc, rc->itr_setting);
3069 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3070 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3075 * ice_vsi_rebuild - Rebuild VSI after reset
3076 * @vsi: VSI to be rebuild
3077 * @init_vsi: is this an initialization or a reconfigure of the VSI
3079 * Returns 0 on success and negative value on failure
3081 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3083 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3084 struct ice_coalesce_stored *coalesce;
3085 int prev_num_q_vectors = 0;
3086 struct ice_vf *vf = NULL;
3087 enum ice_vsi_type vtype;
3096 if (vtype == ICE_VSI_VF)
3097 vf = &pf->vf[vsi->vf_id];
3099 ice_vsi_init_vlan_ops(vsi);
3101 coalesce = kcalloc(vsi->num_q_vectors,
3102 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3106 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3108 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3109 ret = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3111 dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3113 ice_vsi_free_q_vectors(vsi);
3115 /* SR-IOV determines needed MSIX resources all at once instead of per
3116 * VSI since when VFs are spawned we know how many VFs there are and how
3117 * many interrupts each VF needs. SR-IOV MSIX resources are also
3118 * cleared in the same manner.
3120 if (vtype != ICE_VSI_VF) {
3121 /* reclaim SW interrupts back to the common pool */
3122 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3123 pf->num_avail_sw_msix += vsi->num_q_vectors;
3124 vsi->base_vector = 0;
3127 if (ice_is_xdp_ena_vsi(vsi))
3128 /* return value check can be skipped here, it always returns
3129 * 0 if reset is in progress
3131 ice_destroy_xdp_rings(vsi);
3132 ice_vsi_put_qs(vsi);
3133 ice_vsi_clear_rings(vsi);
3134 ice_vsi_free_arrays(vsi);
3135 if (vtype == ICE_VSI_VF)
3136 ice_vsi_set_num_qs(vsi, vf->vf_id);
3138 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3140 ret = ice_vsi_alloc_arrays(vsi);
3144 ice_vsi_get_qs(vsi);
3146 ice_alloc_fd_res(vsi);
3147 ice_vsi_set_tc_cfg(vsi);
3149 /* Initialize VSI struct elements and create VSI in FW */
3150 ret = ice_vsi_init(vsi, init_vsi);
3156 case ICE_VSI_SWITCHDEV_CTRL:
3158 ret = ice_vsi_alloc_q_vectors(vsi);
3162 ret = ice_vsi_setup_vector_base(vsi);
3166 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3170 ret = ice_vsi_alloc_rings(vsi);
3174 ice_vsi_map_rings_to_vectors(vsi);
3175 if (ice_is_xdp_ena_vsi(vsi)) {
3176 ret = ice_vsi_determine_xdp_res(vsi);
3179 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3183 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3184 if (vtype != ICE_VSI_CTRL)
3185 /* Do not exit if configuring RSS had an issue, at
3186 * least receive traffic on first queue. Hence no
3187 * need to capture return value
3189 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3190 ice_vsi_cfg_rss_lut_key(vsi);
3193 ret = ice_vsi_alloc_q_vectors(vsi);
3197 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3201 ret = ice_vsi_alloc_rings(vsi);
3207 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3208 ice_vsi_cfg_rss_lut_key(vsi);
3209 ice_vsi_set_rss_flow_fld(vsi);
3216 /* configure VSI nodes based on number of queues and TC's */
3217 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3218 /* configure VSI nodes based on number of queues and TC's.
3219 * ADQ creates VSIs for each TC/Channel but doesn't
3220 * allocate queues instead it reconfigures the PF queues
3221 * as per the TC command. So max_txqs should point to the
3224 if (vtype == ICE_VSI_CHNL)
3225 max_txqs[i] = pf->num_lan_tx;
3227 max_txqs[i] = vsi->alloc_txq;
3229 if (ice_is_xdp_ena_vsi(vsi))
3230 max_txqs[i] += vsi->num_xdp_txq;
3233 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3234 /* If MQPRIO is set, means channel code path, hence for main
3235 * VSI's, use TC as 1
3237 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3239 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3240 vsi->tc_cfg.ena_tc, max_txqs);
3243 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %d\n",
3249 return ice_schedule_reset(pf, ICE_RESET_PFR);
3252 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3258 ice_vsi_free_q_vectors(vsi);
3261 vsi->current_netdev_flags = 0;
3262 unregister_netdev(vsi->netdev);
3263 free_netdev(vsi->netdev);
3268 set_bit(ICE_RESET_FAILED, pf->state);
3274 * ice_is_reset_in_progress - check for a reset in progress
3275 * @state: PF state field
3277 bool ice_is_reset_in_progress(unsigned long *state)
3279 return test_bit(ICE_RESET_OICR_RECV, state) ||
3280 test_bit(ICE_PFR_REQ, state) ||
3281 test_bit(ICE_CORER_REQ, state) ||
3282 test_bit(ICE_GLOBR_REQ, state);
3286 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3287 * @pf: pointer to the PF structure
3288 * @timeout: length of time to wait, in jiffies
3290 * Wait (sleep) for a short time until the driver finishes cleaning up from
3291 * a device reset. The caller must be able to sleep. Use this to delay
3292 * operations that could fail while the driver is cleaning up after a device
3295 * Returns 0 on success, -EBUSY if the reset is not finished within the
3296 * timeout, and -ERESTARTSYS if the thread was interrupted.
3298 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3302 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3303 !ice_is_reset_in_progress(pf->state),
3314 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3315 * @vsi: VSI being configured
3316 * @ctx: the context buffer returned from AQ VSI update command
3318 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3320 vsi->info.mapping_flags = ctx->info.mapping_flags;
3321 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3322 sizeof(vsi->info.q_mapping));
3323 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3324 sizeof(vsi->info.tc_mapping));
3328 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3329 * @vsi: the VSI being configured
3330 * @ena_tc: TC map to be enabled
3332 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3334 struct net_device *netdev = vsi->netdev;
3335 struct ice_pf *pf = vsi->back;
3336 int numtc = vsi->tc_cfg.numtc;
3337 struct ice_dcbx_cfg *dcbcfg;
3344 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3345 if (vsi->type == ICE_VSI_CHNL)
3349 netdev_reset_tc(netdev);
3353 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3354 numtc = vsi->all_numtc;
3356 if (netdev_set_num_tc(netdev, numtc))
3359 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3361 ice_for_each_traffic_class(i)
3362 if (vsi->tc_cfg.ena_tc & BIT(i))
3363 netdev_set_tc_queue(netdev,
3364 vsi->tc_cfg.tc_info[i].netdev_tc,
3365 vsi->tc_cfg.tc_info[i].qcount_tx,
3366 vsi->tc_cfg.tc_info[i].qoffset);
3367 /* setup TC queue map for CHNL TCs */
3368 ice_for_each_chnl_tc(i) {
3369 if (!(vsi->all_enatc & BIT(i)))
3371 if (!vsi->mqprio_qopt.qopt.count[i])
3373 netdev_set_tc_queue(netdev, i,
3374 vsi->mqprio_qopt.qopt.count[i],
3375 vsi->mqprio_qopt.qopt.offset[i]);
3378 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3381 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3382 u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3384 /* Get the mapped netdev TC# for the UP */
3385 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3386 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3391 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3392 * @vsi: the VSI being configured,
3393 * @ctxt: VSI context structure
3394 * @ena_tc: number of traffic classes to enable
3396 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3399 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3402 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3403 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3404 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3408 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3410 pow = order_base_2(tc0_qcount);
3411 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3412 ICE_AQ_VSI_TC_Q_OFFSET_M) |
3413 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3415 ice_for_each_traffic_class(i) {
3416 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3417 /* TC is not enabled */
3418 vsi->tc_cfg.tc_info[i].qoffset = 0;
3419 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3420 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3421 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3422 ctxt->info.tc_mapping[i] = 0;
3426 offset = vsi->mqprio_qopt.qopt.offset[i];
3427 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3428 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3429 vsi->tc_cfg.tc_info[i].qoffset = offset;
3430 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3431 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3432 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3435 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3436 ice_for_each_chnl_tc(i) {
3437 if (!(vsi->all_enatc & BIT(i)))
3439 offset = vsi->mqprio_qopt.qopt.offset[i];
3440 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3441 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3445 /* Set actual Tx/Rx queue pairs */
3446 vsi->num_txq = offset + qcount_tx;
3447 vsi->num_rxq = offset + qcount_rx;
3449 /* Setup queue TC[0].qmap for given VSI context */
3450 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3451 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3452 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3454 /* Find queue count available for channel VSIs and starting offset
3457 if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3458 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3459 vsi->next_base_q = tc0_qcount;
3461 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq);
3462 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq);
3463 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3464 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3468 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3469 * @vsi: VSI to be configured
3470 * @ena_tc: TC bitmap
3472 * VSI queues expected to be quiesced before calling this function
3474 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3476 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3477 struct ice_pf *pf = vsi->back;
3478 struct ice_vsi_ctx *ctx;
3483 dev = ice_pf_to_dev(pf);
3484 if (vsi->tc_cfg.ena_tc == ena_tc &&
3485 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3488 ice_for_each_traffic_class(i) {
3489 /* build bitmap of enabled TCs */
3490 if (ena_tc & BIT(i))
3492 /* populate max_txqs per TC */
3493 max_txqs[i] = vsi->alloc_txq;
3494 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3495 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3497 if (vsi->type == ICE_VSI_CHNL &&
3498 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3499 max_txqs[i] = vsi->num_txq;
3502 vsi->tc_cfg.ena_tc = ena_tc;
3503 vsi->tc_cfg.numtc = num_tc;
3505 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3510 ctx->info = vsi->info;
3512 if (vsi->type == ICE_VSI_PF &&
3513 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3514 ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3516 ice_vsi_setup_q_map(vsi, ctx);
3518 /* must to indicate which section of VSI context are being modified */
3519 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3520 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3522 dev_info(dev, "Failed VSI Update\n");
3526 if (vsi->type == ICE_VSI_PF &&
3527 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3528 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3530 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3531 vsi->tc_cfg.ena_tc, max_txqs);
3534 dev_err(dev, "VSI %d failed TC config, error %d\n",
3538 ice_vsi_update_q_map(vsi, ctx);
3539 vsi->info.valid_sections = 0;
3541 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3548 * ice_update_ring_stats - Update ring statistics
3549 * @stats: stats to be updated
3550 * @pkts: number of processed packets
3551 * @bytes: number of processed bytes
3553 * This function assumes that caller has acquired a u64_stats_sync lock.
3555 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3557 stats->bytes += bytes;
3558 stats->pkts += pkts;
3562 * ice_update_tx_ring_stats - Update Tx ring specific counters
3563 * @tx_ring: ring to update
3564 * @pkts: number of processed packets
3565 * @bytes: number of processed bytes
3567 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3569 u64_stats_update_begin(&tx_ring->syncp);
3570 ice_update_ring_stats(&tx_ring->stats, pkts, bytes);
3571 u64_stats_update_end(&tx_ring->syncp);
3575 * ice_update_rx_ring_stats - Update Rx ring specific counters
3576 * @rx_ring: ring to update
3577 * @pkts: number of processed packets
3578 * @bytes: number of processed bytes
3580 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3582 u64_stats_update_begin(&rx_ring->syncp);
3583 ice_update_ring_stats(&rx_ring->stats, pkts, bytes);
3584 u64_stats_update_end(&rx_ring->syncp);
3588 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3589 * @sw: switch to check if its default forwarding VSI is free
3591 * Return true if the default forwarding VSI is already being used, else returns
3592 * false signalling that it's available to use.
3594 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3596 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3600 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3601 * @sw: switch for the default forwarding VSI to compare against
3602 * @vsi: VSI to compare against default forwarding VSI
3604 * If this VSI passed in is the default forwarding VSI then return true, else
3607 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3609 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3613 * ice_set_dflt_vsi - set the default forwarding VSI
3614 * @sw: switch used to assign the default forwarding VSI
3615 * @vsi: VSI getting set as the default forwarding VSI on the switch
3617 * If the VSI passed in is already the default VSI and it's enabled just return
3620 * If there is already a default VSI on the switch and it's enabled then return
3621 * -EEXIST since there can only be one default VSI per switch.
3623 * Otherwise try to set the VSI passed in as the switch's default VSI and
3624 * return the result.
3626 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3634 dev = ice_pf_to_dev(vsi->back);
3636 /* the VSI passed in is already the default VSI */
3637 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3638 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3643 /* another VSI is already the default VSI for this switch */
3644 if (ice_is_dflt_vsi_in_use(sw)) {
3645 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3646 sw->dflt_vsi->vsi_num);
3650 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3652 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3653 vsi->vsi_num, status);
3658 sw->dflt_vsi_ena = true;
3664 * ice_clear_dflt_vsi - clear the default forwarding VSI
3665 * @sw: switch used to clear the default VSI
3667 * If the switch has no default VSI or it's not enabled then return error.
3669 * Otherwise try to clear the default VSI and return the result.
3671 int ice_clear_dflt_vsi(struct ice_sw *sw)
3673 struct ice_vsi *dflt_vsi;
3680 dev = ice_pf_to_dev(sw->pf);
3682 dflt_vsi = sw->dflt_vsi;
3684 /* there is no default VSI configured */
3685 if (!ice_is_dflt_vsi_in_use(sw))
3688 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3691 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3692 dflt_vsi->vsi_num, status);
3696 sw->dflt_vsi = NULL;
3697 sw->dflt_vsi_ena = false;
3703 * ice_get_link_speed_mbps - get link speed in Mbps
3704 * @vsi: the VSI whose link speed is being queried
3706 * Return current VSI link speed and 0 if the speed is unknown.
3708 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3710 switch (vsi->port_info->phy.link_info.link_speed) {
3711 case ICE_AQ_LINK_SPEED_100GB:
3712 return SPEED_100000;
3713 case ICE_AQ_LINK_SPEED_50GB:
3715 case ICE_AQ_LINK_SPEED_40GB:
3717 case ICE_AQ_LINK_SPEED_25GB:
3719 case ICE_AQ_LINK_SPEED_20GB:
3721 case ICE_AQ_LINK_SPEED_10GB:
3723 case ICE_AQ_LINK_SPEED_5GB:
3725 case ICE_AQ_LINK_SPEED_2500MB:
3727 case ICE_AQ_LINK_SPEED_1000MB:
3729 case ICE_AQ_LINK_SPEED_100MB:
3731 case ICE_AQ_LINK_SPEED_10MB:
3733 case ICE_AQ_LINK_SPEED_UNKNOWN:
3740 * ice_get_link_speed_kbps - get link speed in Kbps
3741 * @vsi: the VSI whose link speed is being queried
3743 * Return current VSI link speed and 0 if the speed is unknown.
3745 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3749 speed_mbps = ice_get_link_speed_mbps(vsi);
3751 return speed_mbps * 1000;
3755 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3756 * @vsi: VSI to be configured
3757 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3759 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3760 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3763 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3765 struct ice_pf *pf = vsi->back;
3770 dev = ice_pf_to_dev(pf);
3771 if (!vsi->port_info) {
3772 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3773 vsi->idx, vsi->type);
3777 speed = ice_get_link_speed_kbps(vsi);
3778 if (min_tx_rate > (u64)speed) {
3779 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3780 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3785 /* Configure min BW for VSI limit */
3787 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3788 ICE_MIN_BW, min_tx_rate);
3790 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3791 min_tx_rate, ice_vsi_type_str(vsi->type),
3796 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3797 min_tx_rate, ice_vsi_type_str(vsi->type));
3799 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3803 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3804 ice_vsi_type_str(vsi->type), vsi->idx);
3808 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3809 ice_vsi_type_str(vsi->type), vsi->idx);
3816 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3817 * @vsi: VSI to be configured
3818 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3820 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3821 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3824 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3826 struct ice_pf *pf = vsi->back;
3831 dev = ice_pf_to_dev(pf);
3832 if (!vsi->port_info) {
3833 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3834 vsi->idx, vsi->type);
3838 speed = ice_get_link_speed_kbps(vsi);
3839 if (max_tx_rate > (u64)speed) {
3840 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3841 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3846 /* Configure max BW for VSI limit */
3848 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3849 ICE_MAX_BW, max_tx_rate);
3851 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3852 max_tx_rate, ice_vsi_type_str(vsi->type),
3857 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3858 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3860 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3864 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3865 ice_vsi_type_str(vsi->type), vsi->idx);
3869 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3870 ice_vsi_type_str(vsi->type), vsi->idx);
3877 * ice_set_link - turn on/off physical link
3878 * @vsi: VSI to modify physical link on
3879 * @ena: turn on/off physical link
3881 int ice_set_link(struct ice_vsi *vsi, bool ena)
3883 struct device *dev = ice_pf_to_dev(vsi->back);
3884 struct ice_port_info *pi = vsi->port_info;
3885 struct ice_hw *hw = pi->hw;
3888 if (vsi->type != ICE_VSI_PF)
3891 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3893 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3894 * this is not a fatal error, so print a warning message and return
3895 * a success code. Return an error if FW returns an error code other
3896 * than ICE_AQ_RC_EMODE
3898 if (status == -EIO) {
3899 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3900 dev_warn(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3901 (ena ? "ON" : "OFF"), status,
3902 ice_aq_str(hw->adminq.sq_last_status));
3903 } else if (status) {
3904 dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3905 (ena ? "ON" : "OFF"), status,
3906 ice_aq_str(hw->adminq.sq_last_status));
3914 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3915 * @vsi: VSI used to add VLAN filters
3917 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3918 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3919 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3920 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3922 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3923 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3924 * traffic in SVM, since the VLAN TPID isn't part of filtering.
3926 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3927 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3928 * part of filtering.
3930 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3932 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3933 struct ice_vlan vlan;
3936 vlan = ICE_VLAN(0, 0, 0);
3937 err = vlan_ops->add_vlan(vsi, &vlan);
3938 if (err && err != -EEXIST)
3941 /* in SVM both VLAN 0 filters are identical */
3942 if (!ice_is_dvm_ena(&vsi->back->hw))
3945 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3946 err = vlan_ops->add_vlan(vsi, &vlan);
3947 if (err && err != -EEXIST)
3954 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3955 * @vsi: VSI used to add VLAN filters
3957 * Delete the VLAN 0 filters in the same manner that they were added in
3958 * ice_vsi_add_vlan_zero.
3960 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3962 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3963 struct ice_vlan vlan;
3966 vlan = ICE_VLAN(0, 0, 0);
3967 err = vlan_ops->del_vlan(vsi, &vlan);
3968 if (err && err != -EEXIST)
3971 /* in SVM both VLAN 0 filters are identical */
3972 if (!ice_is_dvm_ena(&vsi->back->hw))
3975 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3976 err = vlan_ops->del_vlan(vsi, &vlan);
3977 if (err && err != -EEXIST)
3984 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3985 * @vsi: VSI used to get the VLAN mode
3987 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3988 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3990 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3992 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2
3993 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1
3994 /* no VLAN 0 filter is created when a port VLAN is active */
3995 if (vsi->type == ICE_VSI_VF &&
3996 ice_vf_is_port_vlan_ena(&vsi->back->vf[vsi->vf_id]))
3998 if (ice_is_dvm_ena(&vsi->back->hw))
3999 return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
4001 return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
4005 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
4006 * @vsi: VSI used to determine if any non-zero VLANs have been added
4008 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
4010 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
4014 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
4015 * @vsi: VSI used to get the number of non-zero VLANs added
4017 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
4019 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
4023 * ice_is_feature_supported
4024 * @pf: pointer to the struct ice_pf instance
4025 * @f: feature enum to be checked
4027 * returns true if feature is supported, false otherwise
4029 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
4031 if (f < 0 || f >= ICE_F_MAX)
4034 return test_bit(f, pf->features);
4038 * ice_set_feature_support
4039 * @pf: pointer to the struct ice_pf instance
4040 * @f: feature enum to set
4042 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4044 if (f < 0 || f >= ICE_F_MAX)
4047 set_bit(f, pf->features);
4051 * ice_clear_feature_support
4052 * @pf: pointer to the struct ice_pf instance
4053 * @f: feature enum to clear
4055 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4057 if (f < 0 || f >= ICE_F_MAX)
4060 clear_bit(f, pf->features);
4064 * ice_init_feature_support
4065 * @pf: pointer to the struct ice_pf instance
4067 * called during init to setup supported feature
4069 void ice_init_feature_support(struct ice_pf *pf)
4071 switch (pf->hw.device_id) {
4072 case ICE_DEV_ID_E810C_BACKPLANE:
4073 case ICE_DEV_ID_E810C_QSFP:
4074 case ICE_DEV_ID_E810C_SFP:
4075 ice_set_feature_support(pf, ICE_F_DSCP);
4076 if (ice_is_e810t(&pf->hw))
4077 ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4085 * ice_vsi_update_security - update security block in VSI
4086 * @vsi: pointer to VSI structure
4087 * @fill: function pointer to fill ctx
4090 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4092 struct ice_vsi_ctx ctx = { 0 };
4094 ctx.info = vsi->info;
4095 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4098 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4101 vsi->info = ctx.info;
4106 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4107 * @ctx: pointer to VSI ctx structure
4109 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4111 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4112 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4113 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4117 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4118 * @ctx: pointer to VSI ctx structure
4120 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4122 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4123 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4124 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4128 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4129 * @ctx: pointer to VSI ctx structure
4131 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4133 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4137 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4138 * @ctx: pointer to VSI ctx structure
4140 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4142 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;