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: the VF associated with this VSI, if any
171 * Return 0 on success and a negative value on error
173 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, struct ice_vf *vf)
175 enum ice_vsi_type vsi_type = vsi->type;
176 struct ice_pf *pf = vsi->back;
178 if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
184 vsi->alloc_txq = vsi->req_txq;
185 vsi->num_txq = vsi->req_txq;
187 vsi->alloc_txq = min3(pf->num_lan_msix,
188 ice_get_avail_txq_count(pf),
189 (u16)num_online_cpus());
192 pf->num_lan_tx = vsi->alloc_txq;
194 /* only 1 Rx queue unless RSS is enabled */
195 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
199 vsi->alloc_rxq = vsi->req_rxq;
200 vsi->num_rxq = vsi->req_rxq;
202 vsi->alloc_rxq = min3(pf->num_lan_msix,
203 ice_get_avail_rxq_count(pf),
204 (u16)num_online_cpus());
208 pf->num_lan_rx = vsi->alloc_rxq;
210 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
211 max_t(int, vsi->alloc_rxq,
214 case ICE_VSI_SWITCHDEV_CTRL:
215 /* The number of queues for ctrl VSI is equal to number of VFs.
216 * Each ring is associated to the corresponding VF_PR netdev.
218 vsi->alloc_txq = ice_get_num_vfs(pf);
219 vsi->alloc_rxq = vsi->alloc_txq;
220 vsi->num_q_vectors = 1;
224 vf->num_vf_qs = vf->num_req_qs;
225 vsi->alloc_txq = vf->num_vf_qs;
226 vsi->alloc_rxq = vf->num_vf_qs;
227 /* pf->vfs.num_msix_per includes (VF miscellaneous vector +
228 * data queue interrupts). Since vsi->num_q_vectors is number
229 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
230 * original vector count
232 vsi->num_q_vectors = pf->vfs.num_msix_per - ICE_NONQ_VECS_VF;
237 vsi->num_q_vectors = 1;
248 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type);
252 ice_vsi_set_num_desc(vsi);
256 * ice_get_free_slot - get the next non-NULL location index in array
257 * @array: array to search
258 * @size: size of the array
259 * @curr: last known occupied index to be used as a search hint
261 * void * is being used to keep the functionality generic. This lets us use this
262 * function on any array of pointers.
264 static int ice_get_free_slot(void *array, int size, int curr)
266 int **tmp_array = (int **)array;
269 if (curr < (size - 1) && !tmp_array[curr + 1]) {
274 while ((i < size) && (tmp_array[i]))
285 * ice_vsi_delete - delete a VSI from the switch
286 * @vsi: pointer to VSI being removed
288 void ice_vsi_delete(struct ice_vsi *vsi)
290 struct ice_pf *pf = vsi->back;
291 struct ice_vsi_ctx *ctxt;
294 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
298 if (vsi->type == ICE_VSI_VF)
299 ctxt->vf_num = vsi->vf->vf_id;
300 ctxt->vsi_num = vsi->vsi_num;
302 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
304 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
306 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
307 vsi->vsi_num, status);
313 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
314 * @vsi: pointer to VSI being cleared
316 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
318 struct ice_pf *pf = vsi->back;
321 dev = ice_pf_to_dev(pf);
323 if (vsi->af_xdp_zc_qps) {
324 bitmap_free(vsi->af_xdp_zc_qps);
325 vsi->af_xdp_zc_qps = NULL;
327 /* free the ring and vector containers */
328 if (vsi->q_vectors) {
329 devm_kfree(dev, vsi->q_vectors);
330 vsi->q_vectors = NULL;
333 devm_kfree(dev, vsi->tx_rings);
334 vsi->tx_rings = NULL;
337 devm_kfree(dev, vsi->rx_rings);
338 vsi->rx_rings = NULL;
341 devm_kfree(dev, vsi->txq_map);
345 devm_kfree(dev, vsi->rxq_map);
351 * ice_vsi_clear - clean up and deallocate the provided VSI
352 * @vsi: pointer to VSI being cleared
354 * This deallocates the VSI's queue resources, removes it from the PF's
355 * VSI array if necessary, and deallocates the VSI
357 * Returns 0 on success, negative on failure
359 int ice_vsi_clear(struct ice_vsi *vsi)
361 struct ice_pf *pf = NULL;
371 dev = ice_pf_to_dev(pf);
373 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
374 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
378 mutex_lock(&pf->sw_mutex);
379 /* updates the PF for this cleared VSI */
381 pf->vsi[vsi->idx] = NULL;
382 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
383 pf->next_vsi = vsi->idx;
384 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL && vsi->vf)
385 pf->next_vsi = vsi->idx;
387 ice_vsi_free_arrays(vsi);
388 mutex_unlock(&pf->sw_mutex);
389 devm_kfree(dev, vsi);
395 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
396 * @irq: interrupt number
397 * @data: pointer to a q_vector
399 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
401 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
403 if (!q_vector->tx.tx_ring)
406 #define FDIR_RX_DESC_CLEAN_BUDGET 64
407 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
408 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
414 * ice_msix_clean_rings - MSIX mode Interrupt Handler
415 * @irq: interrupt number
416 * @data: pointer to a q_vector
418 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
420 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
422 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
425 q_vector->total_events++;
427 napi_schedule(&q_vector->napi);
432 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
434 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
435 struct ice_pf *pf = q_vector->vsi->back;
439 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
443 ice_for_each_vf_rcu(pf, bkt, vf)
444 napi_schedule(&vf->repr->q_vector->napi);
451 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
452 * @pf: board private structure
453 * @vsi_type: type of VSI
454 * @ch: ptr to channel
455 * @vf: VF for ICE_VSI_VF and ICE_VSI_CTRL
457 * The VF pointer is used for ICE_VSI_VF and ICE_VSI_CTRL. For ICE_VSI_CTRL,
458 * it may be NULL in the case there is no association with a VF. For
459 * ICE_VSI_VF the VF pointer *must not* be NULL.
461 * returns a pointer to a VSI on success, NULL on failure.
463 static struct ice_vsi *
464 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type,
465 struct ice_channel *ch, struct ice_vf *vf)
467 struct device *dev = ice_pf_to_dev(pf);
468 struct ice_vsi *vsi = NULL;
470 if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
473 /* Need to protect the allocation of the VSIs at the PF level */
474 mutex_lock(&pf->sw_mutex);
476 /* If we have already allocated our maximum number of VSIs,
477 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
478 * is available to be populated
480 if (pf->next_vsi == ICE_NO_VSI) {
481 dev_dbg(dev, "out of VSI slots!\n");
485 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
489 vsi->type = vsi_type;
491 set_bit(ICE_VSI_DOWN, vsi->state);
493 if (vsi_type == ICE_VSI_VF)
494 ice_vsi_set_num_qs(vsi, vf);
495 else if (vsi_type != ICE_VSI_CHNL)
496 ice_vsi_set_num_qs(vsi, NULL);
499 case ICE_VSI_SWITCHDEV_CTRL:
500 if (ice_vsi_alloc_arrays(vsi))
503 /* Setup eswitch MSIX irq handler for VSI */
504 vsi->irq_handler = ice_eswitch_msix_clean_rings;
507 if (ice_vsi_alloc_arrays(vsi))
510 /* Setup default MSIX irq handler for VSI */
511 vsi->irq_handler = ice_msix_clean_rings;
514 if (ice_vsi_alloc_arrays(vsi))
517 /* Setup ctrl VSI MSIX irq handler */
518 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
520 /* For the PF control VSI this is NULL, for the VF control VSI
521 * this will be the first VF to allocate it.
526 if (ice_vsi_alloc_arrays(vsi))
533 vsi->num_rxq = ch->num_rxq;
534 vsi->num_txq = ch->num_txq;
535 vsi->next_base_q = ch->base_q;
538 if (ice_vsi_alloc_arrays(vsi))
542 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
546 if (vsi->type == ICE_VSI_CTRL && !vf) {
547 /* Use the last VSI slot as the index for PF control VSI */
548 vsi->idx = pf->num_alloc_vsi - 1;
549 pf->ctrl_vsi_idx = vsi->idx;
550 pf->vsi[vsi->idx] = vsi;
552 /* fill slot and make note of the index */
553 vsi->idx = pf->next_vsi;
554 pf->vsi[pf->next_vsi] = vsi;
556 /* prepare pf->next_vsi for next use */
557 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
561 if (vsi->type == ICE_VSI_CTRL && vf)
562 vf->ctrl_vsi_idx = vsi->idx;
566 devm_kfree(dev, vsi);
569 mutex_unlock(&pf->sw_mutex);
574 * ice_alloc_fd_res - Allocate FD resource for a VSI
575 * @vsi: pointer to the ice_vsi
577 * This allocates the FD resources
579 * Returns 0 on success, -EPERM on no-op or -EIO on failure
581 static int ice_alloc_fd_res(struct ice_vsi *vsi)
583 struct ice_pf *pf = vsi->back;
586 /* Flow Director filters are only allocated/assigned to the PF VSI or
587 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
588 * add/delete filters so resources are not allocated to it
590 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
593 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
594 vsi->type == ICE_VSI_CHNL))
597 /* FD filters from guaranteed pool per VSI */
598 g_val = pf->hw.func_caps.fd_fltr_guar;
602 /* FD filters from best effort pool */
603 b_val = pf->hw.func_caps.fd_fltr_best_effort;
607 /* PF main VSI gets only 64 FD resources from guaranteed pool
608 * when ADQ is configured.
610 #define ICE_PF_VSI_GFLTR 64
612 /* determine FD filter resources per VSI from shared(best effort) and
615 if (vsi->type == ICE_VSI_PF) {
616 vsi->num_gfltr = g_val;
617 /* if MQPRIO is configured, main VSI doesn't get all FD
618 * resources from guaranteed pool. PF VSI gets 64 FD resources
620 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
621 if (g_val < ICE_PF_VSI_GFLTR)
623 /* allow bare minimum entries for PF VSI */
624 vsi->num_gfltr = ICE_PF_VSI_GFLTR;
627 /* each VSI gets same "best_effort" quota */
628 vsi->num_bfltr = b_val;
629 } else if (vsi->type == ICE_VSI_VF) {
632 /* each VSI gets same "best_effort" quota */
633 vsi->num_bfltr = b_val;
635 struct ice_vsi *main_vsi;
638 main_vsi = ice_get_main_vsi(pf);
642 if (!main_vsi->all_numtc)
645 /* figure out ADQ numtc */
646 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
648 /* only one TC but still asking resources for channels,
651 if (numtc < ICE_CHNL_START_TC)
654 g_val -= ICE_PF_VSI_GFLTR;
655 /* channel VSIs gets equal share from guaranteed pool */
656 vsi->num_gfltr = g_val / numtc;
658 /* each VSI gets same "best_effort" quota */
659 vsi->num_bfltr = b_val;
666 * ice_vsi_get_qs - Assign queues from PF to VSI
667 * @vsi: the VSI to assign queues to
669 * Returns 0 on success and a negative value on error
671 static int ice_vsi_get_qs(struct ice_vsi *vsi)
673 struct ice_pf *pf = vsi->back;
674 struct ice_qs_cfg tx_qs_cfg = {
675 .qs_mutex = &pf->avail_q_mutex,
676 .pf_map = pf->avail_txqs,
677 .pf_map_size = pf->max_pf_txqs,
678 .q_count = vsi->alloc_txq,
679 .scatter_count = ICE_MAX_SCATTER_TXQS,
680 .vsi_map = vsi->txq_map,
682 .mapping_mode = ICE_VSI_MAP_CONTIG
684 struct ice_qs_cfg rx_qs_cfg = {
685 .qs_mutex = &pf->avail_q_mutex,
686 .pf_map = pf->avail_rxqs,
687 .pf_map_size = pf->max_pf_rxqs,
688 .q_count = vsi->alloc_rxq,
689 .scatter_count = ICE_MAX_SCATTER_RXQS,
690 .vsi_map = vsi->rxq_map,
692 .mapping_mode = ICE_VSI_MAP_CONTIG
696 if (vsi->type == ICE_VSI_CHNL)
699 ret = __ice_vsi_get_qs(&tx_qs_cfg);
702 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
704 ret = __ice_vsi_get_qs(&rx_qs_cfg);
707 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
713 * ice_vsi_put_qs - Release queues from VSI to PF
714 * @vsi: the VSI that is going to release queues
716 static void ice_vsi_put_qs(struct ice_vsi *vsi)
718 struct ice_pf *pf = vsi->back;
721 mutex_lock(&pf->avail_q_mutex);
723 ice_for_each_alloc_txq(vsi, i) {
724 clear_bit(vsi->txq_map[i], pf->avail_txqs);
725 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
728 ice_for_each_alloc_rxq(vsi, i) {
729 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
730 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
733 mutex_unlock(&pf->avail_q_mutex);
738 * @pf: pointer to the PF struct
740 * returns true if driver is in safe mode, false otherwise
742 bool ice_is_safe_mode(struct ice_pf *pf)
744 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
749 * @pf: pointer to the PF struct
751 * returns true if RDMA is currently supported, false otherwise
753 bool ice_is_rdma_ena(struct ice_pf *pf)
755 return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
759 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
760 * @vsi: the VSI being cleaned up
762 * This function deletes RSS input set for all flows that were configured
765 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
767 struct ice_pf *pf = vsi->back;
770 if (ice_is_safe_mode(pf))
773 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
775 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
776 vsi->vsi_num, status);
780 * ice_rss_clean - Delete RSS related VSI structures and configuration
781 * @vsi: the VSI being removed
783 static void ice_rss_clean(struct ice_vsi *vsi)
785 struct ice_pf *pf = vsi->back;
788 dev = ice_pf_to_dev(pf);
790 if (vsi->rss_hkey_user)
791 devm_kfree(dev, vsi->rss_hkey_user);
792 if (vsi->rss_lut_user)
793 devm_kfree(dev, vsi->rss_lut_user);
795 ice_vsi_clean_rss_flow_fld(vsi);
796 /* remove RSS replay list */
797 if (!ice_is_safe_mode(pf))
798 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
802 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
803 * @vsi: the VSI being configured
805 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
807 struct ice_hw_common_caps *cap;
808 struct ice_pf *pf = vsi->back;
810 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
815 cap = &pf->hw.func_caps.common_cap;
819 /* PF VSI will inherit RSS instance of PF */
820 vsi->rss_table_size = (u16)cap->rss_table_size;
821 if (vsi->type == ICE_VSI_CHNL)
822 vsi->rss_size = min_t(u16, vsi->num_rxq,
823 BIT(cap->rss_table_entry_width));
825 vsi->rss_size = min_t(u16, num_online_cpus(),
826 BIT(cap->rss_table_entry_width));
827 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
829 case ICE_VSI_SWITCHDEV_CTRL:
830 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
831 vsi->rss_size = min_t(u16, num_online_cpus(),
832 BIT(cap->rss_table_entry_width));
833 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
836 /* VF VSI will get a small RSS table.
837 * For VSI_LUT, LUT size should be set to 64 bytes.
839 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
840 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
841 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
846 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
847 ice_vsi_type_str(vsi->type));
853 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
854 * @hw: HW structure used to determine the VLAN mode of the device
855 * @ctxt: the VSI context being set
857 * This initializes a default VSI context for all sections except the Queues.
859 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
863 memset(&ctxt->info, 0, sizeof(ctxt->info));
864 /* VSI's should be allocated from shared pool */
865 ctxt->alloc_from_pool = true;
866 /* Src pruning enabled by default */
867 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
868 /* Traffic from VSI can be sent to LAN */
869 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
870 /* allow all untagged/tagged packets by default on Tx */
871 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
872 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
873 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
874 /* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
875 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
877 * DVM - leave inner VLAN in packet by default
879 if (ice_is_dvm_ena(hw)) {
880 ctxt->info.inner_vlan_flags |=
881 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
882 ctxt->info.outer_vlan_flags =
883 (ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
884 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
885 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M;
886 ctxt->info.outer_vlan_flags |=
887 (ICE_AQ_VSI_OUTER_TAG_VLAN_8100 <<
888 ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
889 ICE_AQ_VSI_OUTER_TAG_TYPE_M;
891 /* Have 1:1 UP mapping for both ingress/egress tables */
892 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
893 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
894 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
895 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
896 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
897 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
898 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
899 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
900 ctxt->info.ingress_table = cpu_to_le32(table);
901 ctxt->info.egress_table = cpu_to_le32(table);
902 /* Have 1:1 UP mapping for outer to inner UP table */
903 ctxt->info.outer_up_table = cpu_to_le32(table);
904 /* No Outer tag support outer_tag_flags remains to zero */
908 * ice_vsi_setup_q_map - Setup a VSI queue map
909 * @vsi: the VSI being configured
910 * @ctxt: VSI context structure
912 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
914 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
915 u16 num_txq_per_tc, num_rxq_per_tc;
916 u16 qcount_tx = vsi->alloc_txq;
917 u16 qcount_rx = vsi->alloc_rxq;
921 if (!vsi->tc_cfg.numtc) {
922 /* at least TC0 should be enabled by default */
923 vsi->tc_cfg.numtc = 1;
924 vsi->tc_cfg.ena_tc = 1;
927 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
930 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
934 /* find the (rounded up) power-of-2 of qcount */
935 pow = (u16)order_base_2(num_rxq_per_tc);
937 /* TC mapping is a function of the number of Rx queues assigned to the
938 * VSI for each traffic class and the offset of these queues.
939 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
940 * queues allocated to TC0. No:of queues is a power-of-2.
942 * If TC is not enabled, the queue offset is set to 0, and allocate one
943 * queue, this way, traffic for the given TC will be sent to the default
946 * Setup number and offset of Rx queues for all TCs for the VSI
948 ice_for_each_traffic_class(i) {
949 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
950 /* TC is not enabled */
951 vsi->tc_cfg.tc_info[i].qoffset = 0;
952 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
953 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
954 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
955 ctxt->info.tc_mapping[i] = 0;
960 vsi->tc_cfg.tc_info[i].qoffset = offset;
961 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
962 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
963 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
965 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
966 ICE_AQ_VSI_TC_Q_OFFSET_M) |
967 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
968 ICE_AQ_VSI_TC_Q_NUM_M);
969 offset += num_rxq_per_tc;
970 tx_count += num_txq_per_tc;
971 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
974 /* if offset is non-zero, means it is calculated correctly based on
975 * enabled TCs for a given VSI otherwise qcount_rx will always
976 * be correct and non-zero because it is based off - VSI's
977 * allocated Rx queues which is at least 1 (hence qcount_tx will be
981 vsi->num_rxq = offset;
983 vsi->num_rxq = num_rxq_per_tc;
985 if (vsi->num_rxq > vsi->alloc_rxq) {
986 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
987 vsi->num_rxq, vsi->alloc_rxq);
991 vsi->num_txq = tx_count;
992 if (vsi->num_txq > vsi->alloc_txq) {
993 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
994 vsi->num_txq, vsi->alloc_txq);
998 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
999 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1000 /* since there is a chance that num_rxq could have been changed
1001 * in the above for loop, make num_txq equal to num_rxq.
1003 vsi->num_txq = vsi->num_rxq;
1006 /* Rx queue mapping */
1007 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1008 /* q_mapping buffer holds the info for the first queue allocated for
1009 * this VSI in the PF space and also the number of queues associated
1012 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1013 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1019 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1020 * @ctxt: the VSI context being set
1021 * @vsi: the VSI being configured
1023 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1025 u8 dflt_q_group, dflt_q_prio;
1026 u16 dflt_q, report_q, val;
1028 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1029 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1032 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1033 ctxt->info.valid_sections |= cpu_to_le16(val);
1039 /* enable flow director filtering/programming */
1040 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1041 ctxt->info.fd_options = cpu_to_le16(val);
1042 /* max of allocated flow director filters */
1043 ctxt->info.max_fd_fltr_dedicated =
1044 cpu_to_le16(vsi->num_gfltr);
1045 /* max of shared flow director filters any VSI may program */
1046 ctxt->info.max_fd_fltr_shared =
1047 cpu_to_le16(vsi->num_bfltr);
1048 /* default queue index within the VSI of the default FD */
1049 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
1050 ICE_AQ_VSI_FD_DEF_Q_M);
1051 /* target queue or queue group to the FD filter */
1052 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
1053 ICE_AQ_VSI_FD_DEF_GRP_M);
1054 ctxt->info.fd_def_q = cpu_to_le16(val);
1055 /* queue index on which FD filter completion is reported */
1056 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
1057 ICE_AQ_VSI_FD_REPORT_Q_M);
1058 /* priority of the default qindex action */
1059 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
1060 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
1061 ctxt->info.fd_report_opt = cpu_to_le16(val);
1065 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1066 * @ctxt: the VSI context being set
1067 * @vsi: the VSI being configured
1069 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1071 u8 lut_type, hash_type;
1076 dev = ice_pf_to_dev(pf);
1078 switch (vsi->type) {
1081 /* PF VSI will inherit RSS instance of PF */
1082 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1083 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1086 /* VF VSI will gets a small RSS table which is a VSI LUT type */
1087 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1088 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1091 dev_dbg(dev, "Unsupported VSI type %s\n",
1092 ice_vsi_type_str(vsi->type));
1096 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1097 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1098 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1099 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1103 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1105 struct ice_pf *pf = vsi->back;
1110 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1112 pow = order_base_2(qcount);
1113 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1114 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1115 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1116 ICE_AQ_VSI_TC_Q_NUM_M);
1118 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1119 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1120 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1121 ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1125 * ice_vsi_init - Create and initialize a VSI
1126 * @vsi: the VSI being configured
1127 * @init_vsi: is this call creating a VSI
1129 * This initializes a VSI context depending on the VSI type to be added and
1130 * passes it down to the add_vsi aq command to create a new VSI.
1132 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
1134 struct ice_pf *pf = vsi->back;
1135 struct ice_hw *hw = &pf->hw;
1136 struct ice_vsi_ctx *ctxt;
1140 dev = ice_pf_to_dev(pf);
1141 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1145 switch (vsi->type) {
1149 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1151 case ICE_VSI_SWITCHDEV_CTRL:
1153 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1156 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1157 /* VF number here is the absolute VF number (0-255) */
1158 ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1165 /* Handle VLAN pruning for channel VSI if main VSI has VLAN
1168 if (vsi->type == ICE_VSI_CHNL) {
1169 struct ice_vsi *main_vsi;
1171 main_vsi = ice_get_main_vsi(pf);
1172 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1173 ctxt->info.sw_flags2 |=
1174 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1176 ctxt->info.sw_flags2 &=
1177 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1180 ice_set_dflt_vsi_ctx(hw, ctxt);
1181 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1182 ice_set_fd_vsi_ctx(ctxt, vsi);
1183 /* if the switch is in VEB mode, allow VSI loopback */
1184 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1185 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1187 /* Set LUT type and HASH type if RSS is enabled */
1188 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1189 vsi->type != ICE_VSI_CTRL) {
1190 ice_set_rss_vsi_ctx(ctxt, vsi);
1191 /* if updating VSI context, make sure to set valid_section:
1192 * to indicate which section of VSI context being updated
1195 ctxt->info.valid_sections |=
1196 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1199 ctxt->info.sw_id = vsi->port_info->sw_id;
1200 if (vsi->type == ICE_VSI_CHNL) {
1201 ice_chnl_vsi_setup_q_map(vsi, ctxt);
1203 ret = ice_vsi_setup_q_map(vsi, ctxt);
1207 if (!init_vsi) /* means VSI being updated */
1208 /* must to indicate which section of VSI context are
1211 ctxt->info.valid_sections |=
1212 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1215 /* Allow control frames out of main VSI */
1216 if (vsi->type == ICE_VSI_PF) {
1217 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1218 ctxt->info.valid_sections |=
1219 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1223 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1225 dev_err(dev, "Add VSI failed, err %d\n", ret);
1230 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1232 dev_err(dev, "Update VSI failed, err %d\n", ret);
1238 /* keep context for update VSI operations */
1239 vsi->info = ctxt->info;
1241 /* record VSI number returned */
1242 vsi->vsi_num = ctxt->vsi_num;
1250 * ice_free_res - free a block of resources
1251 * @res: pointer to the resource
1252 * @index: starting index previously returned by ice_get_res
1253 * @id: identifier to track owner
1255 * Returns number of resources freed
1257 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1262 if (!res || index >= res->end)
1265 id |= ICE_RES_VALID_BIT;
1266 for (i = index; i < res->end && res->list[i] == id; i++) {
1275 * ice_search_res - Search the tracker for a block of resources
1276 * @res: pointer to the resource
1277 * @needed: size of the block needed
1278 * @id: identifier to track owner
1280 * Returns the base item index of the block, or -ENOMEM for error
1282 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1284 u16 start = 0, end = 0;
1286 if (needed > res->end)
1289 id |= ICE_RES_VALID_BIT;
1292 /* skip already allocated entries */
1293 if (res->list[end++] & ICE_RES_VALID_BIT) {
1295 if ((start + needed) > res->end)
1299 if (end == (start + needed)) {
1302 /* there was enough, so assign it to the requestor */
1304 res->list[i++] = id;
1308 } while (end < res->end);
1314 * ice_get_free_res_count - Get free count from a resource tracker
1315 * @res: Resource tracker instance
1317 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1321 for (i = 0; i < res->end; i++)
1322 if (!(res->list[i] & ICE_RES_VALID_BIT))
1329 * ice_get_res - get a block of resources
1330 * @pf: board private structure
1331 * @res: pointer to the resource
1332 * @needed: size of the block needed
1333 * @id: identifier to track owner
1335 * Returns the base item index of the block, or negative for error
1338 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1343 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1344 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1345 needed, res->num_entries, id);
1349 return ice_search_res(res, needed, id);
1353 * ice_get_vf_ctrl_res - Get VF control VSI resource
1354 * @pf: pointer to the PF structure
1355 * @vsi: the VSI to allocate a resource for
1357 * Look up whether another VF has already allocated the control VSI resource.
1358 * If so, re-use this resource so that we share it among all VFs.
1360 * Otherwise, allocate the resource and return it.
1362 static int ice_get_vf_ctrl_res(struct ice_pf *pf, struct ice_vsi *vsi)
1369 ice_for_each_vf_rcu(pf, bkt, vf) {
1370 if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1371 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1378 return ice_get_res(pf, pf->irq_tracker, vsi->num_q_vectors,
1379 ICE_RES_VF_CTRL_VEC_ID);
1383 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1384 * @vsi: ptr to the VSI
1386 * This should only be called after ice_vsi_alloc() which allocates the
1387 * corresponding SW VSI structure and initializes num_queue_pairs for the
1388 * newly allocated VSI.
1390 * Returns 0 on success or negative on failure
1392 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1394 struct ice_pf *pf = vsi->back;
1399 dev = ice_pf_to_dev(pf);
1400 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1401 if (vsi->type == ICE_VSI_VF)
1403 if (vsi->type == ICE_VSI_CHNL)
1406 if (vsi->base_vector) {
1407 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1408 vsi->vsi_num, vsi->base_vector);
1412 num_q_vectors = vsi->num_q_vectors;
1413 /* reserve slots from OS requested IRQs */
1414 if (vsi->type == ICE_VSI_CTRL && vsi->vf) {
1415 base = ice_get_vf_ctrl_res(pf, vsi);
1417 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1422 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1423 ice_get_free_res_count(pf->irq_tracker),
1424 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1427 vsi->base_vector = (u16)base;
1428 pf->num_avail_sw_msix -= num_q_vectors;
1434 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1435 * @vsi: the VSI having rings deallocated
1437 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1441 /* Avoid stale references by clearing map from vector to ring */
1442 if (vsi->q_vectors) {
1443 ice_for_each_q_vector(vsi, i) {
1444 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1447 q_vector->tx.tx_ring = NULL;
1448 q_vector->rx.rx_ring = NULL;
1453 if (vsi->tx_rings) {
1454 ice_for_each_alloc_txq(vsi, i) {
1455 if (vsi->tx_rings[i]) {
1456 kfree_rcu(vsi->tx_rings[i], rcu);
1457 WRITE_ONCE(vsi->tx_rings[i], NULL);
1461 if (vsi->rx_rings) {
1462 ice_for_each_alloc_rxq(vsi, i) {
1463 if (vsi->rx_rings[i]) {
1464 kfree_rcu(vsi->rx_rings[i], rcu);
1465 WRITE_ONCE(vsi->rx_rings[i], NULL);
1472 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1473 * @vsi: VSI which is having rings allocated
1475 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1477 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1478 struct ice_pf *pf = vsi->back;
1482 dev = ice_pf_to_dev(pf);
1483 /* Allocate Tx rings */
1484 ice_for_each_alloc_txq(vsi, i) {
1485 struct ice_tx_ring *ring;
1487 /* allocate with kzalloc(), free with kfree_rcu() */
1488 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1494 ring->reg_idx = vsi->txq_map[i];
1496 ring->tx_tstamps = &pf->ptp.port.tx;
1498 ring->count = vsi->num_tx_desc;
1499 ring->txq_teid = ICE_INVAL_TEID;
1501 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1503 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1504 WRITE_ONCE(vsi->tx_rings[i], ring);
1507 /* Allocate Rx rings */
1508 ice_for_each_alloc_rxq(vsi, i) {
1509 struct ice_rx_ring *ring;
1511 /* allocate with kzalloc(), free with kfree_rcu() */
1512 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1517 ring->reg_idx = vsi->rxq_map[i];
1519 ring->netdev = vsi->netdev;
1521 ring->count = vsi->num_rx_desc;
1522 WRITE_ONCE(vsi->rx_rings[i], ring);
1528 ice_vsi_clear_rings(vsi);
1533 * ice_vsi_manage_rss_lut - disable/enable RSS
1534 * @vsi: the VSI being changed
1535 * @ena: boolean value indicating if this is an enable or disable request
1537 * In the event of disable request for RSS, this function will zero out RSS
1538 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1541 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1545 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1550 if (vsi->rss_lut_user)
1551 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1553 ice_fill_rss_lut(lut, vsi->rss_table_size,
1557 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1562 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1563 * @vsi: VSI to be configured
1565 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1567 struct ice_pf *pf = vsi->back;
1572 dev = ice_pf_to_dev(pf);
1573 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1574 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1575 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1577 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1579 /* If orig_rss_size is valid and it is less than determined
1580 * main VSI's rss_size, update main VSI's rss_size to be
1581 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1582 * RSS table gets programmed to be correct (whatever it was
1583 * to begin with (prior to setup-tc for ADQ config)
1585 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1586 vsi->orig_rss_size <= vsi->num_rxq) {
1587 vsi->rss_size = vsi->orig_rss_size;
1588 /* now orig_rss_size is used, reset it to zero */
1589 vsi->orig_rss_size = 0;
1593 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1597 if (vsi->rss_lut_user)
1598 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1600 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1602 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1604 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1605 goto ice_vsi_cfg_rss_exit;
1608 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1611 goto ice_vsi_cfg_rss_exit;
1614 if (vsi->rss_hkey_user)
1615 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1617 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1619 err = ice_set_rss_key(vsi, key);
1621 dev_err(dev, "set_rss_key failed, error %d\n", err);
1624 ice_vsi_cfg_rss_exit:
1630 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1631 * @vsi: VSI to be configured
1633 * This function will only be called during the VF VSI setup. Upon successful
1634 * completion of package download, this function will configure default RSS
1635 * input sets for VF VSI.
1637 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1639 struct ice_pf *pf = vsi->back;
1643 dev = ice_pf_to_dev(pf);
1644 if (ice_is_safe_mode(pf)) {
1645 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1650 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1652 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1653 vsi->vsi_num, status);
1657 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1658 * @vsi: VSI to be configured
1660 * This function will only be called after successful download package call
1661 * during initialization of PF. Since the downloaded package will erase the
1662 * RSS section, this function will configure RSS input sets for different
1663 * flow types. The last profile added has the highest priority, therefore 2
1664 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1665 * (i.e. IPv4 src/dst TCP src/dst port).
1667 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1669 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1670 struct ice_pf *pf = vsi->back;
1671 struct ice_hw *hw = &pf->hw;
1675 dev = ice_pf_to_dev(pf);
1676 if (ice_is_safe_mode(pf)) {
1677 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1681 /* configure RSS for IPv4 with input set IP src/dst */
1682 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1683 ICE_FLOW_SEG_HDR_IPV4);
1685 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1688 /* configure RSS for IPv6 with input set IPv6 src/dst */
1689 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1690 ICE_FLOW_SEG_HDR_IPV6);
1692 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1695 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1696 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1697 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1699 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1702 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1703 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1704 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1706 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1709 /* configure RSS for sctp4 with input set IP src/dst */
1710 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1711 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1713 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1716 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1717 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1718 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1720 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1723 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1724 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1725 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1727 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1730 /* configure RSS for sctp6 with input set IPv6 src/dst */
1731 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1732 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1734 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1737 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_ESP_SPI,
1738 ICE_FLOW_SEG_HDR_ESP);
1740 dev_dbg(dev, "ice_add_rss_cfg failed for esp/spi flow, vsi = %d, error = %d\n",
1745 * ice_pf_state_is_nominal - checks the PF for nominal state
1746 * @pf: pointer to PF to check
1748 * Check the PF's state for a collection of bits that would indicate
1749 * the PF is in a state that would inhibit normal operation for
1750 * driver functionality.
1752 * Returns true if PF is in a nominal state, false otherwise
1754 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1756 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1761 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1762 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1769 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1770 * @vsi: the VSI to be updated
1772 void ice_update_eth_stats(struct ice_vsi *vsi)
1774 struct ice_eth_stats *prev_es, *cur_es;
1775 struct ice_hw *hw = &vsi->back->hw;
1776 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1778 prev_es = &vsi->eth_stats_prev;
1779 cur_es = &vsi->eth_stats;
1781 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1782 &prev_es->rx_bytes, &cur_es->rx_bytes);
1784 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1785 &prev_es->rx_unicast, &cur_es->rx_unicast);
1787 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1788 &prev_es->rx_multicast, &cur_es->rx_multicast);
1790 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1791 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1793 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1794 &prev_es->rx_discards, &cur_es->rx_discards);
1796 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1797 &prev_es->tx_bytes, &cur_es->tx_bytes);
1799 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1800 &prev_es->tx_unicast, &cur_es->tx_unicast);
1802 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1803 &prev_es->tx_multicast, &cur_es->tx_multicast);
1805 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1806 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1808 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1809 &prev_es->tx_errors, &cur_es->tx_errors);
1811 vsi->stat_offsets_loaded = true;
1815 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1818 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1820 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1821 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1822 vsi->rx_buf_len = ICE_RXBUF_2048;
1823 #if (PAGE_SIZE < 8192)
1824 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1825 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1826 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1827 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1830 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1831 #if (PAGE_SIZE < 8192)
1832 vsi->rx_buf_len = ICE_RXBUF_3072;
1834 vsi->rx_buf_len = ICE_RXBUF_2048;
1840 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1842 * @pf_q: index of the Rx queue in the PF's queue space
1843 * @rxdid: flexible descriptor RXDID
1844 * @prio: priority for the RXDID for this queue
1845 * @ena_ts: true to enable timestamp and false to disable timestamp
1848 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1851 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1853 /* clear any previous values */
1854 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1855 QRXFLXP_CNTXT_RXDID_PRIO_M |
1856 QRXFLXP_CNTXT_TS_M);
1858 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1859 QRXFLXP_CNTXT_RXDID_IDX_M;
1861 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1862 QRXFLXP_CNTXT_RXDID_PRIO_M;
1865 /* Enable TimeSync on this queue */
1866 regval |= QRXFLXP_CNTXT_TS_M;
1868 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1871 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1873 if (q_idx >= vsi->num_rxq)
1876 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1879 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1881 struct ice_aqc_add_tx_qgrp *qg_buf;
1884 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1887 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1891 qg_buf->num_txqs = 1;
1893 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1899 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1900 * @vsi: the VSI being configured
1902 * Return 0 on success and a negative value on error
1903 * Configure the Rx VSI for operation.
1905 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1909 if (vsi->type == ICE_VSI_VF)
1912 ice_vsi_cfg_frame_size(vsi);
1914 /* set up individual rings */
1915 ice_for_each_rxq(vsi, i) {
1916 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1926 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1927 * @vsi: the VSI being configured
1928 * @rings: Tx ring array to be configured
1929 * @count: number of Tx ring array elements
1931 * Return 0 on success and a negative value on error
1932 * Configure the Tx VSI for operation.
1935 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1937 struct ice_aqc_add_tx_qgrp *qg_buf;
1941 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1945 qg_buf->num_txqs = 1;
1947 for (q_idx = 0; q_idx < count; q_idx++) {
1948 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1959 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1960 * @vsi: the VSI being configured
1962 * Return 0 on success and a negative value on error
1963 * Configure the Tx VSI for operation.
1965 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1967 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1971 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1972 * @vsi: the VSI being configured
1974 * Return 0 on success and a negative value on error
1975 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1977 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1982 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1986 ice_for_each_xdp_txq(vsi, i)
1987 vsi->xdp_rings[i]->xsk_pool = ice_tx_xsk_pool(vsi->xdp_rings[i]);
1993 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1994 * @intrl: interrupt rate limit in usecs
1995 * @gran: interrupt rate limit granularity in usecs
1997 * This function converts a decimal interrupt rate limit in usecs to the format
1998 * expected by firmware.
2000 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
2002 u32 val = intrl / gran;
2005 return val | GLINT_RATE_INTRL_ENA_M;
2010 * ice_write_intrl - write throttle rate limit to interrupt specific register
2011 * @q_vector: pointer to interrupt specific structure
2012 * @intrl: throttle rate limit in microseconds to write
2014 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
2016 struct ice_hw *hw = &q_vector->vsi->back->hw;
2018 wr32(hw, GLINT_RATE(q_vector->reg_idx),
2019 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
2022 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
2025 case ICE_RX_CONTAINER:
2027 return rc->rx_ring->q_vector;
2029 case ICE_TX_CONTAINER:
2031 return rc->tx_ring->q_vector;
2041 * __ice_write_itr - write throttle rate to register
2042 * @q_vector: pointer to interrupt data structure
2043 * @rc: pointer to ring container
2044 * @itr: throttle rate in microseconds to write
2046 static void __ice_write_itr(struct ice_q_vector *q_vector,
2047 struct ice_ring_container *rc, u16 itr)
2049 struct ice_hw *hw = &q_vector->vsi->back->hw;
2051 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
2052 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
2056 * ice_write_itr - write throttle rate to queue specific register
2057 * @rc: pointer to ring container
2058 * @itr: throttle rate in microseconds to write
2060 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
2062 struct ice_q_vector *q_vector;
2064 q_vector = ice_pull_qvec_from_rc(rc);
2068 __ice_write_itr(q_vector, rc, itr);
2072 * ice_set_q_vector_intrl - set up interrupt rate limiting
2073 * @q_vector: the vector to be configured
2075 * Interrupt rate limiting is local to the vector, not per-queue so we must
2076 * detect if either ring container has dynamic moderation enabled to decide
2077 * what to set the interrupt rate limit to via INTRL settings. In the case that
2078 * dynamic moderation is disabled on both, write the value with the cached
2079 * setting to make sure INTRL register matches the user visible value.
2081 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2083 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2084 /* in the case of dynamic enabled, cap each vector to no more
2085 * than (4 us) 250,000 ints/sec, which allows low latency
2086 * but still less than 500,000 interrupts per second, which
2087 * reduces CPU a bit in the case of the lowest latency
2088 * setting. The 4 here is a value in microseconds.
2090 ice_write_intrl(q_vector, 4);
2092 ice_write_intrl(q_vector, q_vector->intrl);
2097 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2098 * @vsi: the VSI being configured
2100 * This configures MSIX mode interrupts for the PF VSI, and should not be used
2103 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2105 struct ice_pf *pf = vsi->back;
2106 struct ice_hw *hw = &pf->hw;
2107 u16 txq = 0, rxq = 0;
2110 ice_for_each_q_vector(vsi, i) {
2111 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2112 u16 reg_idx = q_vector->reg_idx;
2114 ice_cfg_itr(hw, q_vector);
2116 /* Both Transmit Queue Interrupt Cause Control register
2117 * and Receive Queue Interrupt Cause control register
2118 * expects MSIX_INDX field to be the vector index
2119 * within the function space and not the absolute
2120 * vector index across PF or across device.
2121 * For SR-IOV VF VSIs queue vector index always starts
2122 * with 1 since first vector index(0) is used for OICR
2123 * in VF space. Since VMDq and other PF VSIs are within
2124 * the PF function space, use the vector index that is
2125 * tracked for this PF.
2127 for (q = 0; q < q_vector->num_ring_tx; q++) {
2128 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2129 q_vector->tx.itr_idx);
2133 for (q = 0; q < q_vector->num_ring_rx; q++) {
2134 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2135 q_vector->rx.itr_idx);
2142 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2143 * @vsi: the VSI whose rings are to be enabled
2145 * Returns 0 on success and a negative value on error
2147 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2149 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2153 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2154 * @vsi: the VSI whose rings are to be disabled
2156 * Returns 0 on success and a negative value on error
2158 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2160 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2164 * ice_vsi_stop_tx_rings - Disable Tx rings
2165 * @vsi: the VSI being configured
2166 * @rst_src: reset source
2167 * @rel_vmvf_num: Relative ID of VF/VM
2168 * @rings: Tx ring array to be stopped
2169 * @count: number of Tx ring array elements
2172 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2173 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2177 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2180 for (q_idx = 0; q_idx < count; q_idx++) {
2181 struct ice_txq_meta txq_meta = { };
2184 if (!rings || !rings[q_idx])
2187 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2188 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2189 rings[q_idx], &txq_meta);
2199 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2200 * @vsi: the VSI being configured
2201 * @rst_src: reset source
2202 * @rel_vmvf_num: Relative ID of VF/VM
2205 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2208 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2212 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2213 * @vsi: the VSI being configured
2215 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2217 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2221 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2222 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2224 * returns true if Rx VLAN pruning is enabled and false otherwise.
2226 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2231 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2234 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2236 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2237 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2238 vsi->tc_cfg.numtc = 1;
2242 /* set VSI TC information based on DCB config */
2243 ice_vsi_set_dcb_tc_cfg(vsi);
2247 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2248 * @vsi: VSI to set the q_vectors register index on
2251 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2255 if (!vsi || !vsi->q_vectors)
2258 ice_for_each_q_vector(vsi, i) {
2259 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2262 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2267 if (vsi->type == ICE_VSI_VF) {
2268 struct ice_vf *vf = vsi->vf;
2270 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2273 q_vector->v_idx + vsi->base_vector;
2280 ice_for_each_q_vector(vsi, i) {
2281 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2284 q_vector->reg_idx = 0;
2291 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2292 * @vsi: the VSI being configured
2293 * @tx: bool to determine Tx or Rx rule
2294 * @create: bool to determine create or remove Rule
2296 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2298 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2299 enum ice_sw_fwd_act_type act);
2300 struct ice_pf *pf = vsi->back;
2304 dev = ice_pf_to_dev(pf);
2305 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2308 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2311 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2312 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2315 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2321 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2322 create ? "adding" : "removing", tx ? "TX" : "RX",
2323 vsi->vsi_num, status);
2327 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2328 * @vsi: pointer to the VSI
2330 * This function will allocate new scheduler aggregator now if needed and will
2331 * move specified VSI into it.
2333 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2335 struct device *dev = ice_pf_to_dev(vsi->back);
2336 struct ice_agg_node *agg_node_iter = NULL;
2337 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2338 struct ice_agg_node *agg_node = NULL;
2339 int node_offset, max_agg_nodes = 0;
2340 struct ice_port_info *port_info;
2341 struct ice_pf *pf = vsi->back;
2342 u32 agg_node_id_start = 0;
2345 /* create (as needed) scheduler aggregator node and move VSI into
2346 * corresponding aggregator node
2347 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2348 * - VF aggregator nodes will contain VF VSI
2350 port_info = pf->hw.port_info;
2354 switch (vsi->type) {
2359 case ICE_VSI_SWITCHDEV_CTRL:
2360 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2361 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2362 agg_node_iter = &pf->pf_agg_node[0];
2365 /* user can create 'n' VFs on a given PF, but since max children
2366 * per aggregator node can be only 64. Following code handles
2367 * aggregator(s) for VF VSIs, either selects a agg_node which
2368 * was already created provided num_vsis < 64, otherwise
2369 * select next available node, which will be created
2371 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2372 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2373 agg_node_iter = &pf->vf_agg_node[0];
2376 /* other VSI type, handle later if needed */
2377 dev_dbg(dev, "unexpected VSI type %s\n",
2378 ice_vsi_type_str(vsi->type));
2382 /* find the appropriate aggregator node */
2383 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2384 /* see if we can find space in previously created
2385 * node if num_vsis < 64, otherwise skip
2387 if (agg_node_iter->num_vsis &&
2388 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2393 if (agg_node_iter->valid &&
2394 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2395 agg_id = agg_node_iter->agg_id;
2396 agg_node = agg_node_iter;
2400 /* find unclaimed agg_id */
2401 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2402 agg_id = node_offset + agg_node_id_start;
2403 agg_node = agg_node_iter;
2406 /* move to next agg_node */
2413 /* if selected aggregator node was not created, create it */
2414 if (!agg_node->valid) {
2415 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2416 (u8)vsi->tc_cfg.ena_tc);
2418 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2422 /* aggregator node is created, store the neeeded info */
2423 agg_node->valid = true;
2424 agg_node->agg_id = agg_id;
2427 /* move VSI to corresponding aggregator node */
2428 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2429 (u8)vsi->tc_cfg.ena_tc);
2431 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2436 /* keep active children count for aggregator node */
2437 agg_node->num_vsis++;
2439 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2440 * to aggregator node
2442 vsi->agg_node = agg_node;
2443 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2444 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2445 vsi->agg_node->num_vsis);
2449 * ice_vsi_setup - Set up a VSI by a given type
2450 * @pf: board private structure
2451 * @pi: pointer to the port_info instance
2452 * @vsi_type: VSI type
2453 * @vf: pointer to VF to which this VSI connects. This field is used primarily
2454 * for the ICE_VSI_VF type. Other VSI types should pass NULL.
2455 * @ch: ptr to channel
2457 * This allocates the sw VSI structure and its queue resources.
2459 * Returns pointer to the successfully allocated and configured VSI sw struct on
2460 * success, NULL on failure.
2463 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2464 enum ice_vsi_type vsi_type, struct ice_vf *vf,
2465 struct ice_channel *ch)
2467 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2468 struct device *dev = ice_pf_to_dev(pf);
2469 struct ice_vsi *vsi;
2472 if (vsi_type == ICE_VSI_CHNL)
2473 vsi = ice_vsi_alloc(pf, vsi_type, ch, NULL);
2474 else if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2475 vsi = ice_vsi_alloc(pf, vsi_type, NULL, vf);
2477 vsi = ice_vsi_alloc(pf, vsi_type, NULL, NULL);
2480 dev_err(dev, "could not allocate VSI\n");
2484 vsi->port_info = pi;
2485 vsi->vsw = pf->first_sw;
2486 if (vsi->type == ICE_VSI_PF)
2487 vsi->ethtype = ETH_P_PAUSE;
2489 ice_alloc_fd_res(vsi);
2491 if (vsi_type != ICE_VSI_CHNL) {
2492 if (ice_vsi_get_qs(vsi)) {
2493 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2495 goto unroll_vsi_alloc;
2499 /* set RSS capabilities */
2500 ice_vsi_set_rss_params(vsi);
2502 /* set TC configuration */
2503 ice_vsi_set_tc_cfg(vsi);
2505 /* create the VSI */
2506 ret = ice_vsi_init(vsi, true);
2510 ice_vsi_init_vlan_ops(vsi);
2512 switch (vsi->type) {
2514 case ICE_VSI_SWITCHDEV_CTRL:
2516 ret = ice_vsi_alloc_q_vectors(vsi);
2518 goto unroll_vsi_init;
2520 ret = ice_vsi_setup_vector_base(vsi);
2522 goto unroll_alloc_q_vector;
2524 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2526 goto unroll_vector_base;
2528 ret = ice_vsi_alloc_rings(vsi);
2530 goto unroll_vector_base;
2532 ice_vsi_map_rings_to_vectors(vsi);
2534 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2535 if (vsi->type != ICE_VSI_CTRL)
2536 /* Do not exit if configuring RSS had an issue, at
2537 * least receive traffic on first queue. Hence no
2538 * need to capture return value
2540 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2541 ice_vsi_cfg_rss_lut_key(vsi);
2542 ice_vsi_set_rss_flow_fld(vsi);
2547 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2548 ice_vsi_cfg_rss_lut_key(vsi);
2549 ice_vsi_set_rss_flow_fld(vsi);
2553 /* VF driver will take care of creating netdev for this type and
2554 * map queues to vectors through Virtchnl, PF driver only
2555 * creates a VSI and corresponding structures for bookkeeping
2558 ret = ice_vsi_alloc_q_vectors(vsi);
2560 goto unroll_vsi_init;
2562 ret = ice_vsi_alloc_rings(vsi);
2564 goto unroll_alloc_q_vector;
2566 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2568 goto unroll_vector_base;
2570 /* Do not exit if configuring RSS had an issue, at least
2571 * receive traffic on first queue. Hence no need to capture
2574 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2575 ice_vsi_cfg_rss_lut_key(vsi);
2576 ice_vsi_set_vf_rss_flow_fld(vsi);
2580 ret = ice_vsi_alloc_rings(vsi);
2582 goto unroll_vsi_init;
2585 /* clean up the resources and exit */
2586 goto unroll_vsi_init;
2589 /* configure VSI nodes based on number of queues and TC's */
2590 ice_for_each_traffic_class(i) {
2591 if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2594 if (vsi->type == ICE_VSI_CHNL) {
2595 if (!vsi->alloc_txq && vsi->num_txq)
2596 max_txqs[i] = vsi->num_txq;
2598 max_txqs[i] = pf->num_lan_tx;
2600 max_txqs[i] = vsi->alloc_txq;
2604 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2605 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2608 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2610 goto unroll_clear_rings;
2613 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2614 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2615 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2616 * The rule is added once for PF VSI in order to create appropriate
2617 * recipe, since VSI/VSI list is ignored with drop action...
2618 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2619 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2620 * settings in the HW.
2622 if (!ice_is_safe_mode(pf))
2623 if (vsi->type == ICE_VSI_PF) {
2624 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2626 ice_cfg_sw_lldp(vsi, true, true);
2630 ice_set_agg_vsi(vsi);
2634 ice_vsi_clear_rings(vsi);
2636 /* reclaim SW interrupts back to the common pool */
2637 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2638 pf->num_avail_sw_msix += vsi->num_q_vectors;
2639 unroll_alloc_q_vector:
2640 ice_vsi_free_q_vectors(vsi);
2642 ice_vsi_delete(vsi);
2644 ice_vsi_put_qs(vsi);
2646 if (vsi_type == ICE_VSI_VF)
2647 ice_enable_lag(pf->lag);
2654 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2655 * @vsi: the VSI being cleaned up
2657 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2659 struct ice_pf *pf = vsi->back;
2660 struct ice_hw *hw = &pf->hw;
2665 ice_for_each_q_vector(vsi, i) {
2666 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2668 ice_write_intrl(q_vector, 0);
2669 for (q = 0; q < q_vector->num_ring_tx; q++) {
2670 ice_write_itr(&q_vector->tx, 0);
2671 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2672 if (ice_is_xdp_ena_vsi(vsi)) {
2673 u32 xdp_txq = txq + vsi->num_xdp_txq;
2675 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2680 for (q = 0; q < q_vector->num_ring_rx; q++) {
2681 ice_write_itr(&q_vector->rx, 0);
2682 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2691 * ice_vsi_free_irq - Free the IRQ association with the OS
2692 * @vsi: the VSI being configured
2694 void ice_vsi_free_irq(struct ice_vsi *vsi)
2696 struct ice_pf *pf = vsi->back;
2697 int base = vsi->base_vector;
2700 if (!vsi->q_vectors || !vsi->irqs_ready)
2703 ice_vsi_release_msix(vsi);
2704 if (vsi->type == ICE_VSI_VF)
2707 vsi->irqs_ready = false;
2708 ice_free_cpu_rx_rmap(vsi);
2710 ice_for_each_q_vector(vsi, i) {
2711 u16 vector = i + base;
2714 irq_num = pf->msix_entries[vector].vector;
2716 /* free only the irqs that were actually requested */
2717 if (!vsi->q_vectors[i] ||
2718 !(vsi->q_vectors[i]->num_ring_tx ||
2719 vsi->q_vectors[i]->num_ring_rx))
2722 /* clear the affinity notifier in the IRQ descriptor */
2723 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2724 irq_set_affinity_notifier(irq_num, NULL);
2726 /* clear the affinity_mask in the IRQ descriptor */
2727 irq_set_affinity_hint(irq_num, NULL);
2728 synchronize_irq(irq_num);
2729 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2734 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2735 * @vsi: the VSI having resources freed
2737 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2744 ice_for_each_txq(vsi, i)
2745 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2746 ice_free_tx_ring(vsi->tx_rings[i]);
2750 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2751 * @vsi: the VSI having resources freed
2753 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2760 ice_for_each_rxq(vsi, i)
2761 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2762 ice_free_rx_ring(vsi->rx_rings[i]);
2766 * ice_vsi_close - Shut down a VSI
2767 * @vsi: the VSI being shut down
2769 void ice_vsi_close(struct ice_vsi *vsi)
2771 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2774 ice_vsi_free_irq(vsi);
2775 ice_vsi_free_tx_rings(vsi);
2776 ice_vsi_free_rx_rings(vsi);
2780 * ice_ena_vsi - resume a VSI
2781 * @vsi: the VSI being resume
2782 * @locked: is the rtnl_lock already held
2784 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2788 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2791 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2793 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2794 if (netif_running(vsi->netdev)) {
2798 err = ice_open_internal(vsi->netdev);
2803 } else if (vsi->type == ICE_VSI_CTRL) {
2804 err = ice_vsi_open_ctrl(vsi);
2811 * ice_dis_vsi - pause a VSI
2812 * @vsi: the VSI being paused
2813 * @locked: is the rtnl_lock already held
2815 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2817 if (test_bit(ICE_VSI_DOWN, vsi->state))
2820 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2822 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2823 if (netif_running(vsi->netdev)) {
2834 } else if (vsi->type == ICE_VSI_CTRL ||
2835 vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2841 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2842 * @vsi: the VSI being un-configured
2844 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2846 int base = vsi->base_vector;
2847 struct ice_pf *pf = vsi->back;
2848 struct ice_hw *hw = &pf->hw;
2852 /* disable interrupt causation from each queue */
2853 if (vsi->tx_rings) {
2854 ice_for_each_txq(vsi, i) {
2855 if (vsi->tx_rings[i]) {
2858 reg = vsi->tx_rings[i]->reg_idx;
2859 val = rd32(hw, QINT_TQCTL(reg));
2860 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2861 wr32(hw, QINT_TQCTL(reg), val);
2866 if (vsi->rx_rings) {
2867 ice_for_each_rxq(vsi, i) {
2868 if (vsi->rx_rings[i]) {
2871 reg = vsi->rx_rings[i]->reg_idx;
2872 val = rd32(hw, QINT_RQCTL(reg));
2873 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2874 wr32(hw, QINT_RQCTL(reg), val);
2879 /* disable each interrupt */
2880 ice_for_each_q_vector(vsi, i) {
2881 if (!vsi->q_vectors[i])
2883 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2888 /* don't call synchronize_irq() for VF's from the host */
2889 if (vsi->type == ICE_VSI_VF)
2892 ice_for_each_q_vector(vsi, i)
2893 synchronize_irq(pf->msix_entries[i + base].vector);
2897 * ice_napi_del - Remove NAPI handler for the VSI
2898 * @vsi: VSI for which NAPI handler is to be removed
2900 void ice_napi_del(struct ice_vsi *vsi)
2907 ice_for_each_q_vector(vsi, v_idx)
2908 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2912 * ice_free_vf_ctrl_res - Free the VF control VSI resource
2913 * @pf: pointer to PF structure
2914 * @vsi: the VSI to free resources for
2916 * Check if the VF control VSI resource is still in use. If no VF is using it
2917 * any more, release the VSI resource. Otherwise, leave it to be cleaned up
2918 * once no other VF uses it.
2920 static void ice_free_vf_ctrl_res(struct ice_pf *pf, struct ice_vsi *vsi)
2926 ice_for_each_vf_rcu(pf, bkt, vf) {
2927 if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
2934 /* No other VFs left that have control VSI. It is now safe to reclaim
2935 * SW interrupts back to the common pool.
2937 ice_free_res(pf->irq_tracker, vsi->base_vector,
2938 ICE_RES_VF_CTRL_VEC_ID);
2939 pf->num_avail_sw_msix += vsi->num_q_vectors;
2943 * ice_vsi_release - Delete a VSI and free its resources
2944 * @vsi: the VSI being removed
2946 * Returns 0 on success or < 0 on error
2948 int ice_vsi_release(struct ice_vsi *vsi)
2957 /* do not unregister while driver is in the reset recovery pending
2958 * state. Since reset/rebuild happens through PF service task workqueue,
2959 * it's not a good idea to unregister netdev that is associated to the
2960 * PF that is running the work queue items currently. This is done to
2961 * avoid check_flush_dependency() warning on this wq
2963 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2964 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2965 unregister_netdev(vsi->netdev);
2966 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2969 if (vsi->type == ICE_VSI_PF)
2970 ice_devlink_destroy_pf_port(pf);
2972 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2975 /* Disable VSI and free resources */
2976 if (vsi->type != ICE_VSI_LB)
2977 ice_vsi_dis_irq(vsi);
2980 /* SR-IOV determines needed MSIX resources all at once instead of per
2981 * VSI since when VFs are spawned we know how many VFs there are and how
2982 * many interrupts each VF needs. SR-IOV MSIX resources are also
2983 * cleared in the same manner.
2985 if (vsi->type == ICE_VSI_CTRL && vsi->vf) {
2986 ice_free_vf_ctrl_res(pf, vsi);
2987 } else if (vsi->type != ICE_VSI_VF) {
2988 /* reclaim SW interrupts back to the common pool */
2989 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2990 pf->num_avail_sw_msix += vsi->num_q_vectors;
2993 if (!ice_is_safe_mode(pf)) {
2994 if (vsi->type == ICE_VSI_PF) {
2995 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2997 ice_cfg_sw_lldp(vsi, true, false);
2998 /* The Rx rule will only exist to remove if the LLDP FW
2999 * engine is currently stopped
3001 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
3002 ice_cfg_sw_lldp(vsi, false, false);
3006 if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
3007 ice_clear_dflt_vsi(pf->first_sw);
3008 ice_fltr_remove_all(vsi);
3009 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3010 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3012 dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3014 ice_vsi_delete(vsi);
3015 ice_vsi_free_q_vectors(vsi);
3018 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
3019 unregister_netdev(vsi->netdev);
3020 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3022 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
3023 free_netdev(vsi->netdev);
3025 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3029 if (vsi->type == ICE_VSI_VF &&
3030 vsi->agg_node && vsi->agg_node->valid)
3031 vsi->agg_node->num_vsis--;
3032 ice_vsi_clear_rings(vsi);
3034 ice_vsi_put_qs(vsi);
3036 /* retain SW VSI data structure since it is needed to unregister and
3037 * free VSI netdev when PF is not in reset recovery pending state,\
3038 * for ex: during rmmod.
3040 if (!ice_is_reset_in_progress(pf->state))
3047 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
3048 * @vsi: VSI connected with q_vectors
3049 * @coalesce: array of struct with stored coalesce
3051 * Returns array size.
3054 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3055 struct ice_coalesce_stored *coalesce)
3059 ice_for_each_q_vector(vsi, i) {
3060 struct ice_q_vector *q_vector = vsi->q_vectors[i];
3062 coalesce[i].itr_tx = q_vector->tx.itr_settings;
3063 coalesce[i].itr_rx = q_vector->rx.itr_settings;
3064 coalesce[i].intrl = q_vector->intrl;
3066 if (i < vsi->num_txq)
3067 coalesce[i].tx_valid = true;
3068 if (i < vsi->num_rxq)
3069 coalesce[i].rx_valid = true;
3072 return vsi->num_q_vectors;
3076 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3077 * @vsi: VSI connected with q_vectors
3078 * @coalesce: pointer to array of struct with stored coalesce
3079 * @size: size of coalesce array
3081 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3082 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3086 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3087 struct ice_coalesce_stored *coalesce, int size)
3089 struct ice_ring_container *rc;
3092 if ((size && !coalesce) || !vsi)
3095 /* There are a couple of cases that have to be handled here:
3096 * 1. The case where the number of queue vectors stays the same, but
3097 * the number of Tx or Rx rings changes (the first for loop)
3098 * 2. The case where the number of queue vectors increased (the
3101 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3102 /* There are 2 cases to handle here and they are the same for
3104 * if the entry was valid previously (coalesce[i].[tr]x_valid
3105 * and the loop variable is less than the number of rings
3106 * allocated, then write the previous values
3108 * if the entry was not valid previously, but the number of
3109 * rings is less than are allocated (this means the number of
3110 * rings increased from previously), then write out the
3111 * values in the first element
3113 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3114 * as there is no harm because the dynamic algorithm
3115 * will just overwrite.
3117 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3118 rc = &vsi->q_vectors[i]->rx;
3119 rc->itr_settings = coalesce[i].itr_rx;
3120 ice_write_itr(rc, rc->itr_setting);
3121 } else if (i < vsi->alloc_rxq) {
3122 rc = &vsi->q_vectors[i]->rx;
3123 rc->itr_settings = coalesce[0].itr_rx;
3124 ice_write_itr(rc, rc->itr_setting);
3127 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3128 rc = &vsi->q_vectors[i]->tx;
3129 rc->itr_settings = coalesce[i].itr_tx;
3130 ice_write_itr(rc, rc->itr_setting);
3131 } else if (i < vsi->alloc_txq) {
3132 rc = &vsi->q_vectors[i]->tx;
3133 rc->itr_settings = coalesce[0].itr_tx;
3134 ice_write_itr(rc, rc->itr_setting);
3137 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3138 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3141 /* the number of queue vectors increased so write whatever is in
3144 for (; i < vsi->num_q_vectors; i++) {
3146 rc = &vsi->q_vectors[i]->tx;
3147 rc->itr_settings = coalesce[0].itr_tx;
3148 ice_write_itr(rc, rc->itr_setting);
3151 rc = &vsi->q_vectors[i]->rx;
3152 rc->itr_settings = coalesce[0].itr_rx;
3153 ice_write_itr(rc, rc->itr_setting);
3155 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3156 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3161 * ice_vsi_rebuild - Rebuild VSI after reset
3162 * @vsi: VSI to be rebuild
3163 * @init_vsi: is this an initialization or a reconfigure of the VSI
3165 * Returns 0 on success and negative value on failure
3167 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3169 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3170 struct ice_coalesce_stored *coalesce;
3171 int prev_num_q_vectors = 0;
3172 enum ice_vsi_type vtype;
3181 if (WARN_ON(vtype == ICE_VSI_VF) && !vsi->vf)
3184 ice_vsi_init_vlan_ops(vsi);
3186 coalesce = kcalloc(vsi->num_q_vectors,
3187 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3191 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3193 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3194 ret = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3196 dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3198 ice_vsi_free_q_vectors(vsi);
3200 /* SR-IOV determines needed MSIX resources all at once instead of per
3201 * VSI since when VFs are spawned we know how many VFs there are and how
3202 * many interrupts each VF needs. SR-IOV MSIX resources are also
3203 * cleared in the same manner.
3205 if (vtype != ICE_VSI_VF) {
3206 /* reclaim SW interrupts back to the common pool */
3207 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3208 pf->num_avail_sw_msix += vsi->num_q_vectors;
3209 vsi->base_vector = 0;
3212 if (ice_is_xdp_ena_vsi(vsi))
3213 /* return value check can be skipped here, it always returns
3214 * 0 if reset is in progress
3216 ice_destroy_xdp_rings(vsi);
3217 ice_vsi_put_qs(vsi);
3218 ice_vsi_clear_rings(vsi);
3219 ice_vsi_free_arrays(vsi);
3220 if (vtype == ICE_VSI_VF)
3221 ice_vsi_set_num_qs(vsi, vsi->vf);
3223 ice_vsi_set_num_qs(vsi, NULL);
3225 ret = ice_vsi_alloc_arrays(vsi);
3229 ice_vsi_get_qs(vsi);
3231 ice_alloc_fd_res(vsi);
3232 ice_vsi_set_tc_cfg(vsi);
3234 /* Initialize VSI struct elements and create VSI in FW */
3235 ret = ice_vsi_init(vsi, init_vsi);
3241 case ICE_VSI_SWITCHDEV_CTRL:
3243 ret = ice_vsi_alloc_q_vectors(vsi);
3247 ret = ice_vsi_setup_vector_base(vsi);
3251 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3255 ret = ice_vsi_alloc_rings(vsi);
3259 ice_vsi_map_rings_to_vectors(vsi);
3260 if (ice_is_xdp_ena_vsi(vsi)) {
3261 ret = ice_vsi_determine_xdp_res(vsi);
3264 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3268 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3269 if (vtype != ICE_VSI_CTRL)
3270 /* Do not exit if configuring RSS had an issue, at
3271 * least receive traffic on first queue. Hence no
3272 * need to capture return value
3274 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3275 ice_vsi_cfg_rss_lut_key(vsi);
3278 ret = ice_vsi_alloc_q_vectors(vsi);
3282 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3286 ret = ice_vsi_alloc_rings(vsi);
3292 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3293 ice_vsi_cfg_rss_lut_key(vsi);
3294 ice_vsi_set_rss_flow_fld(vsi);
3301 /* configure VSI nodes based on number of queues and TC's */
3302 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3303 /* configure VSI nodes based on number of queues and TC's.
3304 * ADQ creates VSIs for each TC/Channel but doesn't
3305 * allocate queues instead it reconfigures the PF queues
3306 * as per the TC command. So max_txqs should point to the
3309 if (vtype == ICE_VSI_CHNL)
3310 max_txqs[i] = pf->num_lan_tx;
3312 max_txqs[i] = vsi->alloc_txq;
3314 if (ice_is_xdp_ena_vsi(vsi))
3315 max_txqs[i] += vsi->num_xdp_txq;
3318 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3319 /* If MQPRIO is set, means channel code path, hence for main
3320 * VSI's, use TC as 1
3322 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3324 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3325 vsi->tc_cfg.ena_tc, max_txqs);
3328 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %d\n",
3334 return ice_schedule_reset(pf, ICE_RESET_PFR);
3337 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3343 ice_vsi_free_q_vectors(vsi);
3346 vsi->current_netdev_flags = 0;
3347 unregister_netdev(vsi->netdev);
3348 free_netdev(vsi->netdev);
3353 set_bit(ICE_RESET_FAILED, pf->state);
3359 * ice_is_reset_in_progress - check for a reset in progress
3360 * @state: PF state field
3362 bool ice_is_reset_in_progress(unsigned long *state)
3364 return test_bit(ICE_RESET_OICR_RECV, state) ||
3365 test_bit(ICE_PFR_REQ, state) ||
3366 test_bit(ICE_CORER_REQ, state) ||
3367 test_bit(ICE_GLOBR_REQ, state);
3371 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3372 * @pf: pointer to the PF structure
3373 * @timeout: length of time to wait, in jiffies
3375 * Wait (sleep) for a short time until the driver finishes cleaning up from
3376 * a device reset. The caller must be able to sleep. Use this to delay
3377 * operations that could fail while the driver is cleaning up after a device
3380 * Returns 0 on success, -EBUSY if the reset is not finished within the
3381 * timeout, and -ERESTARTSYS if the thread was interrupted.
3383 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3387 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3388 !ice_is_reset_in_progress(pf->state),
3399 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3400 * @vsi: VSI being configured
3401 * @ctx: the context buffer returned from AQ VSI update command
3403 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3405 vsi->info.mapping_flags = ctx->info.mapping_flags;
3406 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3407 sizeof(vsi->info.q_mapping));
3408 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3409 sizeof(vsi->info.tc_mapping));
3413 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3414 * @vsi: the VSI being configured
3415 * @ena_tc: TC map to be enabled
3417 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3419 struct net_device *netdev = vsi->netdev;
3420 struct ice_pf *pf = vsi->back;
3421 int numtc = vsi->tc_cfg.numtc;
3422 struct ice_dcbx_cfg *dcbcfg;
3429 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3430 if (vsi->type == ICE_VSI_CHNL)
3434 netdev_reset_tc(netdev);
3438 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3439 numtc = vsi->all_numtc;
3441 if (netdev_set_num_tc(netdev, numtc))
3444 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3446 ice_for_each_traffic_class(i)
3447 if (vsi->tc_cfg.ena_tc & BIT(i))
3448 netdev_set_tc_queue(netdev,
3449 vsi->tc_cfg.tc_info[i].netdev_tc,
3450 vsi->tc_cfg.tc_info[i].qcount_tx,
3451 vsi->tc_cfg.tc_info[i].qoffset);
3452 /* setup TC queue map for CHNL TCs */
3453 ice_for_each_chnl_tc(i) {
3454 if (!(vsi->all_enatc & BIT(i)))
3456 if (!vsi->mqprio_qopt.qopt.count[i])
3458 netdev_set_tc_queue(netdev, i,
3459 vsi->mqprio_qopt.qopt.count[i],
3460 vsi->mqprio_qopt.qopt.offset[i]);
3463 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3466 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3467 u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3469 /* Get the mapped netdev TC# for the UP */
3470 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3471 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3476 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3477 * @vsi: the VSI being configured,
3478 * @ctxt: VSI context structure
3479 * @ena_tc: number of traffic classes to enable
3481 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3484 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3487 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3488 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3489 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3493 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3495 pow = order_base_2(tc0_qcount);
3496 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3497 ICE_AQ_VSI_TC_Q_OFFSET_M) |
3498 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3500 ice_for_each_traffic_class(i) {
3501 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3502 /* TC is not enabled */
3503 vsi->tc_cfg.tc_info[i].qoffset = 0;
3504 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3505 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3506 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3507 ctxt->info.tc_mapping[i] = 0;
3511 offset = vsi->mqprio_qopt.qopt.offset[i];
3512 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3513 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3514 vsi->tc_cfg.tc_info[i].qoffset = offset;
3515 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3516 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3517 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3520 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3521 ice_for_each_chnl_tc(i) {
3522 if (!(vsi->all_enatc & BIT(i)))
3524 offset = vsi->mqprio_qopt.qopt.offset[i];
3525 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3526 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3530 /* Set actual Tx/Rx queue pairs */
3531 vsi->num_txq = offset + qcount_tx;
3532 if (vsi->num_txq > vsi->alloc_txq) {
3533 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3534 vsi->num_txq, vsi->alloc_txq);
3538 vsi->num_rxq = offset + qcount_rx;
3539 if (vsi->num_rxq > vsi->alloc_rxq) {
3540 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3541 vsi->num_rxq, vsi->alloc_rxq);
3545 /* Setup queue TC[0].qmap for given VSI context */
3546 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3547 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3548 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3550 /* Find queue count available for channel VSIs and starting offset
3553 if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3554 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3555 vsi->next_base_q = tc0_qcount;
3557 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq);
3558 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq);
3559 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3560 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3566 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3567 * @vsi: VSI to be configured
3568 * @ena_tc: TC bitmap
3570 * VSI queues expected to be quiesced before calling this function
3572 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3574 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3575 struct ice_pf *pf = vsi->back;
3576 struct ice_vsi_ctx *ctx;
3581 dev = ice_pf_to_dev(pf);
3582 if (vsi->tc_cfg.ena_tc == ena_tc &&
3583 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3586 ice_for_each_traffic_class(i) {
3587 /* build bitmap of enabled TCs */
3588 if (ena_tc & BIT(i))
3590 /* populate max_txqs per TC */
3591 max_txqs[i] = vsi->alloc_txq;
3592 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3593 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3595 if (vsi->type == ICE_VSI_CHNL &&
3596 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3597 max_txqs[i] = vsi->num_txq;
3600 vsi->tc_cfg.ena_tc = ena_tc;
3601 vsi->tc_cfg.numtc = num_tc;
3603 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3608 ctx->info = vsi->info;
3610 if (vsi->type == ICE_VSI_PF &&
3611 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3612 ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3614 ret = ice_vsi_setup_q_map(vsi, ctx);
3619 /* must to indicate which section of VSI context are being modified */
3620 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3621 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3623 dev_info(dev, "Failed VSI Update\n");
3627 if (vsi->type == ICE_VSI_PF &&
3628 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3629 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3631 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3632 vsi->tc_cfg.ena_tc, max_txqs);
3635 dev_err(dev, "VSI %d failed TC config, error %d\n",
3639 ice_vsi_update_q_map(vsi, ctx);
3640 vsi->info.valid_sections = 0;
3642 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3649 * ice_update_ring_stats - Update ring statistics
3650 * @stats: stats to be updated
3651 * @pkts: number of processed packets
3652 * @bytes: number of processed bytes
3654 * This function assumes that caller has acquired a u64_stats_sync lock.
3656 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3658 stats->bytes += bytes;
3659 stats->pkts += pkts;
3663 * ice_update_tx_ring_stats - Update Tx ring specific counters
3664 * @tx_ring: ring to update
3665 * @pkts: number of processed packets
3666 * @bytes: number of processed bytes
3668 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3670 u64_stats_update_begin(&tx_ring->syncp);
3671 ice_update_ring_stats(&tx_ring->stats, pkts, bytes);
3672 u64_stats_update_end(&tx_ring->syncp);
3676 * ice_update_rx_ring_stats - Update Rx ring specific counters
3677 * @rx_ring: ring to update
3678 * @pkts: number of processed packets
3679 * @bytes: number of processed bytes
3681 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3683 u64_stats_update_begin(&rx_ring->syncp);
3684 ice_update_ring_stats(&rx_ring->stats, pkts, bytes);
3685 u64_stats_update_end(&rx_ring->syncp);
3689 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3690 * @sw: switch to check if its default forwarding VSI is free
3692 * Return true if the default forwarding VSI is already being used, else returns
3693 * false signalling that it's available to use.
3695 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3697 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3701 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3702 * @sw: switch for the default forwarding VSI to compare against
3703 * @vsi: VSI to compare against default forwarding VSI
3705 * If this VSI passed in is the default forwarding VSI then return true, else
3708 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3710 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3714 * ice_set_dflt_vsi - set the default forwarding VSI
3715 * @sw: switch used to assign the default forwarding VSI
3716 * @vsi: VSI getting set as the default forwarding VSI on the switch
3718 * If the VSI passed in is already the default VSI and it's enabled just return
3721 * If there is already a default VSI on the switch and it's enabled then return
3722 * -EEXIST since there can only be one default VSI per switch.
3724 * Otherwise try to set the VSI passed in as the switch's default VSI and
3725 * return the result.
3727 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3735 dev = ice_pf_to_dev(vsi->back);
3737 /* the VSI passed in is already the default VSI */
3738 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3739 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3744 /* another VSI is already the default VSI for this switch */
3745 if (ice_is_dflt_vsi_in_use(sw)) {
3746 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3747 sw->dflt_vsi->vsi_num);
3751 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3753 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3754 vsi->vsi_num, status);
3759 sw->dflt_vsi_ena = true;
3765 * ice_clear_dflt_vsi - clear the default forwarding VSI
3766 * @sw: switch used to clear the default VSI
3768 * If the switch has no default VSI or it's not enabled then return error.
3770 * Otherwise try to clear the default VSI and return the result.
3772 int ice_clear_dflt_vsi(struct ice_sw *sw)
3774 struct ice_vsi *dflt_vsi;
3781 dev = ice_pf_to_dev(sw->pf);
3783 dflt_vsi = sw->dflt_vsi;
3785 /* there is no default VSI configured */
3786 if (!ice_is_dflt_vsi_in_use(sw))
3789 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3792 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3793 dflt_vsi->vsi_num, status);
3797 sw->dflt_vsi = NULL;
3798 sw->dflt_vsi_ena = false;
3804 * ice_get_link_speed_mbps - get link speed in Mbps
3805 * @vsi: the VSI whose link speed is being queried
3807 * Return current VSI link speed and 0 if the speed is unknown.
3809 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3811 switch (vsi->port_info->phy.link_info.link_speed) {
3812 case ICE_AQ_LINK_SPEED_100GB:
3813 return SPEED_100000;
3814 case ICE_AQ_LINK_SPEED_50GB:
3816 case ICE_AQ_LINK_SPEED_40GB:
3818 case ICE_AQ_LINK_SPEED_25GB:
3820 case ICE_AQ_LINK_SPEED_20GB:
3822 case ICE_AQ_LINK_SPEED_10GB:
3824 case ICE_AQ_LINK_SPEED_5GB:
3826 case ICE_AQ_LINK_SPEED_2500MB:
3828 case ICE_AQ_LINK_SPEED_1000MB:
3830 case ICE_AQ_LINK_SPEED_100MB:
3832 case ICE_AQ_LINK_SPEED_10MB:
3834 case ICE_AQ_LINK_SPEED_UNKNOWN:
3841 * ice_get_link_speed_kbps - get link speed in Kbps
3842 * @vsi: the VSI whose link speed is being queried
3844 * Return current VSI link speed and 0 if the speed is unknown.
3846 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3850 speed_mbps = ice_get_link_speed_mbps(vsi);
3852 return speed_mbps * 1000;
3856 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3857 * @vsi: VSI to be configured
3858 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3860 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3861 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3864 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3866 struct ice_pf *pf = vsi->back;
3871 dev = ice_pf_to_dev(pf);
3872 if (!vsi->port_info) {
3873 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3874 vsi->idx, vsi->type);
3878 speed = ice_get_link_speed_kbps(vsi);
3879 if (min_tx_rate > (u64)speed) {
3880 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3881 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3886 /* Configure min BW for VSI limit */
3888 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3889 ICE_MIN_BW, min_tx_rate);
3891 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3892 min_tx_rate, ice_vsi_type_str(vsi->type),
3897 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3898 min_tx_rate, ice_vsi_type_str(vsi->type));
3900 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3904 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3905 ice_vsi_type_str(vsi->type), vsi->idx);
3909 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3910 ice_vsi_type_str(vsi->type), vsi->idx);
3917 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3918 * @vsi: VSI to be configured
3919 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3921 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3922 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3925 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3927 struct ice_pf *pf = vsi->back;
3932 dev = ice_pf_to_dev(pf);
3933 if (!vsi->port_info) {
3934 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3935 vsi->idx, vsi->type);
3939 speed = ice_get_link_speed_kbps(vsi);
3940 if (max_tx_rate > (u64)speed) {
3941 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3942 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3947 /* Configure max BW for VSI limit */
3949 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3950 ICE_MAX_BW, max_tx_rate);
3952 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3953 max_tx_rate, ice_vsi_type_str(vsi->type),
3958 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3959 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3961 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3965 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3966 ice_vsi_type_str(vsi->type), vsi->idx);
3970 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3971 ice_vsi_type_str(vsi->type), vsi->idx);
3978 * ice_set_link - turn on/off physical link
3979 * @vsi: VSI to modify physical link on
3980 * @ena: turn on/off physical link
3982 int ice_set_link(struct ice_vsi *vsi, bool ena)
3984 struct device *dev = ice_pf_to_dev(vsi->back);
3985 struct ice_port_info *pi = vsi->port_info;
3986 struct ice_hw *hw = pi->hw;
3989 if (vsi->type != ICE_VSI_PF)
3992 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3994 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3995 * this is not a fatal error, so print a warning message and return
3996 * a success code. Return an error if FW returns an error code other
3997 * than ICE_AQ_RC_EMODE
3999 if (status == -EIO) {
4000 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
4001 dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
4002 (ena ? "ON" : "OFF"), status,
4003 ice_aq_str(hw->adminq.sq_last_status));
4004 } else if (status) {
4005 dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
4006 (ena ? "ON" : "OFF"), status,
4007 ice_aq_str(hw->adminq.sq_last_status));
4015 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
4016 * @vsi: VSI used to add VLAN filters
4018 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
4019 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
4020 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
4021 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
4023 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
4024 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
4025 * traffic in SVM, since the VLAN TPID isn't part of filtering.
4027 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
4028 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
4029 * part of filtering.
4031 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
4033 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
4034 struct ice_vlan vlan;
4037 vlan = ICE_VLAN(0, 0, 0);
4038 err = vlan_ops->add_vlan(vsi, &vlan);
4039 if (err && err != -EEXIST)
4042 /* in SVM both VLAN 0 filters are identical */
4043 if (!ice_is_dvm_ena(&vsi->back->hw))
4046 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
4047 err = vlan_ops->add_vlan(vsi, &vlan);
4048 if (err && err != -EEXIST)
4055 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
4056 * @vsi: VSI used to add VLAN filters
4058 * Delete the VLAN 0 filters in the same manner that they were added in
4059 * ice_vsi_add_vlan_zero.
4061 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
4063 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
4064 struct ice_vlan vlan;
4067 vlan = ICE_VLAN(0, 0, 0);
4068 err = vlan_ops->del_vlan(vsi, &vlan);
4069 if (err && err != -EEXIST)
4072 /* in SVM both VLAN 0 filters are identical */
4073 if (!ice_is_dvm_ena(&vsi->back->hw))
4076 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
4077 err = vlan_ops->del_vlan(vsi, &vlan);
4078 if (err && err != -EEXIST)
4085 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
4086 * @vsi: VSI used to get the VLAN mode
4088 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
4089 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
4091 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
4093 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2
4094 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1
4095 /* no VLAN 0 filter is created when a port VLAN is active */
4096 if (vsi->type == ICE_VSI_VF) {
4097 if (WARN_ON(!vsi->vf))
4100 if (ice_vf_is_port_vlan_ena(vsi->vf))
4104 if (ice_is_dvm_ena(&vsi->back->hw))
4105 return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
4107 return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
4111 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
4112 * @vsi: VSI used to determine if any non-zero VLANs have been added
4114 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
4116 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
4120 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
4121 * @vsi: VSI used to get the number of non-zero VLANs added
4123 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
4125 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
4129 * ice_is_feature_supported
4130 * @pf: pointer to the struct ice_pf instance
4131 * @f: feature enum to be checked
4133 * returns true if feature is supported, false otherwise
4135 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
4137 if (f < 0 || f >= ICE_F_MAX)
4140 return test_bit(f, pf->features);
4144 * ice_set_feature_support
4145 * @pf: pointer to the struct ice_pf instance
4146 * @f: feature enum to set
4148 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4150 if (f < 0 || f >= ICE_F_MAX)
4153 set_bit(f, pf->features);
4157 * ice_clear_feature_support
4158 * @pf: pointer to the struct ice_pf instance
4159 * @f: feature enum to clear
4161 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4163 if (f < 0 || f >= ICE_F_MAX)
4166 clear_bit(f, pf->features);
4170 * ice_init_feature_support
4171 * @pf: pointer to the struct ice_pf instance
4173 * called during init to setup supported feature
4175 void ice_init_feature_support(struct ice_pf *pf)
4177 switch (pf->hw.device_id) {
4178 case ICE_DEV_ID_E810C_BACKPLANE:
4179 case ICE_DEV_ID_E810C_QSFP:
4180 case ICE_DEV_ID_E810C_SFP:
4181 ice_set_feature_support(pf, ICE_F_DSCP);
4182 if (ice_is_e810t(&pf->hw)) {
4183 ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4184 if (ice_gnss_is_gps_present(&pf->hw))
4185 ice_set_feature_support(pf, ICE_F_GNSS);
4194 * ice_vsi_update_security - update security block in VSI
4195 * @vsi: pointer to VSI structure
4196 * @fill: function pointer to fill ctx
4199 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4201 struct ice_vsi_ctx ctx = { 0 };
4203 ctx.info = vsi->info;
4204 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4207 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4210 vsi->info = ctx.info;
4215 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4216 * @ctx: pointer to VSI ctx structure
4218 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4220 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4221 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4222 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4226 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4227 * @ctx: pointer to VSI ctx structure
4229 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4231 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4232 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4233 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4237 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4238 * @ctx: pointer to VSI ctx structure
4240 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4242 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4246 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4247 * @ctx: pointer to VSI ctx structure
4249 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4251 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;