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
170 * Return 0 on success and a negative value on error
172 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
174 enum ice_vsi_type vsi_type = vsi->type;
175 struct ice_pf *pf = vsi->back;
176 struct ice_vf *vf = vsi->vf;
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_from_hw - delete a VSI from the switch
286 * @vsi: pointer to VSI being removed
288 static void ice_vsi_delete_from_hw(struct ice_vsi *vsi)
290 struct ice_pf *pf = vsi->back;
291 struct ice_vsi_ctx *ctxt;
294 ice_fltr_remove_all(vsi);
295 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
299 if (vsi->type == ICE_VSI_VF)
300 ctxt->vf_num = vsi->vf->vf_id;
301 ctxt->vsi_num = vsi->vsi_num;
303 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
305 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
307 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
308 vsi->vsi_num, status);
314 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
315 * @vsi: pointer to VSI being cleared
317 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
319 struct ice_pf *pf = vsi->back;
322 dev = ice_pf_to_dev(pf);
324 if (vsi->af_xdp_zc_qps) {
325 bitmap_free(vsi->af_xdp_zc_qps);
326 vsi->af_xdp_zc_qps = NULL;
328 /* free the ring and vector containers */
329 if (vsi->q_vectors) {
330 devm_kfree(dev, vsi->q_vectors);
331 vsi->q_vectors = NULL;
334 devm_kfree(dev, vsi->tx_rings);
335 vsi->tx_rings = NULL;
338 devm_kfree(dev, vsi->rx_rings);
339 vsi->rx_rings = NULL;
342 devm_kfree(dev, vsi->txq_map);
346 devm_kfree(dev, vsi->rxq_map);
352 * ice_vsi_free_stats - Free the ring statistics structures
355 static void ice_vsi_free_stats(struct ice_vsi *vsi)
357 struct ice_vsi_stats *vsi_stat;
358 struct ice_pf *pf = vsi->back;
361 if (vsi->type == ICE_VSI_CHNL)
366 vsi_stat = pf->vsi_stats[vsi->idx];
370 ice_for_each_alloc_txq(vsi, i) {
371 if (vsi_stat->tx_ring_stats[i]) {
372 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
373 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
377 ice_for_each_alloc_rxq(vsi, i) {
378 if (vsi_stat->rx_ring_stats[i]) {
379 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
380 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
384 kfree(vsi_stat->tx_ring_stats);
385 kfree(vsi_stat->rx_ring_stats);
387 pf->vsi_stats[vsi->idx] = NULL;
391 * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
392 * @vsi: VSI which is having stats allocated
394 static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
396 struct ice_ring_stats **tx_ring_stats;
397 struct ice_ring_stats **rx_ring_stats;
398 struct ice_vsi_stats *vsi_stats;
399 struct ice_pf *pf = vsi->back;
402 vsi_stats = pf->vsi_stats[vsi->idx];
403 tx_ring_stats = vsi_stats->tx_ring_stats;
404 rx_ring_stats = vsi_stats->rx_ring_stats;
406 /* Allocate Tx ring stats */
407 ice_for_each_alloc_txq(vsi, i) {
408 struct ice_ring_stats *ring_stats;
409 struct ice_tx_ring *ring;
411 ring = vsi->tx_rings[i];
412 ring_stats = tx_ring_stats[i];
415 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
419 WRITE_ONCE(tx_ring_stats[i], ring_stats);
422 ring->ring_stats = ring_stats;
425 /* Allocate Rx ring stats */
426 ice_for_each_alloc_rxq(vsi, i) {
427 struct ice_ring_stats *ring_stats;
428 struct ice_rx_ring *ring;
430 ring = vsi->rx_rings[i];
431 ring_stats = rx_ring_stats[i];
434 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
438 WRITE_ONCE(rx_ring_stats[i], ring_stats);
441 ring->ring_stats = ring_stats;
447 ice_vsi_free_stats(vsi);
452 * ice_vsi_free - clean up and deallocate the provided VSI
453 * @vsi: pointer to VSI being cleared
455 * This deallocates the VSI's queue resources, removes it from the PF's
456 * VSI array if necessary, and deallocates the VSI
458 static void ice_vsi_free(struct ice_vsi *vsi)
460 struct ice_pf *pf = NULL;
463 if (!vsi || !vsi->back)
467 dev = ice_pf_to_dev(pf);
469 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
470 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
474 mutex_lock(&pf->sw_mutex);
475 /* updates the PF for this cleared VSI */
477 pf->vsi[vsi->idx] = NULL;
478 pf->next_vsi = vsi->idx;
480 ice_vsi_free_stats(vsi);
481 ice_vsi_free_arrays(vsi);
482 mutex_unlock(&pf->sw_mutex);
483 devm_kfree(dev, vsi);
486 void ice_vsi_delete(struct ice_vsi *vsi)
488 ice_vsi_delete_from_hw(vsi);
493 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
494 * @irq: interrupt number
495 * @data: pointer to a q_vector
497 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
499 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
501 if (!q_vector->tx.tx_ring)
504 #define FDIR_RX_DESC_CLEAN_BUDGET 64
505 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
506 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
512 * ice_msix_clean_rings - MSIX mode Interrupt Handler
513 * @irq: interrupt number
514 * @data: pointer to a q_vector
516 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
518 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
520 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
523 q_vector->total_events++;
525 napi_schedule(&q_vector->napi);
530 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
532 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
533 struct ice_pf *pf = q_vector->vsi->back;
537 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
541 ice_for_each_vf_rcu(pf, bkt, vf)
542 napi_schedule(&vf->repr->q_vector->napi);
549 * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
552 static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
554 struct ice_vsi_stats *vsi_stat;
555 struct ice_pf *pf = vsi->back;
557 if (vsi->type == ICE_VSI_CHNL)
562 if (pf->vsi_stats[vsi->idx])
563 /* realloc will happen in rebuild path */
566 vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
570 vsi_stat->tx_ring_stats =
571 kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
573 if (!vsi_stat->tx_ring_stats)
576 vsi_stat->rx_ring_stats =
577 kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
579 if (!vsi_stat->rx_ring_stats)
582 pf->vsi_stats[vsi->idx] = vsi_stat;
587 kfree(vsi_stat->rx_ring_stats);
589 kfree(vsi_stat->tx_ring_stats);
591 pf->vsi_stats[vsi->idx] = NULL;
596 * ice_vsi_alloc_def - set default values for already allocated VSI
598 * @ch: ptr to channel
601 ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
603 if (vsi->type != ICE_VSI_CHNL) {
604 ice_vsi_set_num_qs(vsi);
605 if (ice_vsi_alloc_arrays(vsi))
610 case ICE_VSI_SWITCHDEV_CTRL:
611 /* Setup eswitch MSIX irq handler for VSI */
612 vsi->irq_handler = ice_eswitch_msix_clean_rings;
615 /* Setup default MSIX irq handler for VSI */
616 vsi->irq_handler = ice_msix_clean_rings;
619 /* Setup ctrl VSI MSIX irq handler */
620 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
626 vsi->num_rxq = ch->num_rxq;
627 vsi->num_txq = ch->num_txq;
628 vsi->next_base_q = ch->base_q;
634 ice_vsi_free_arrays(vsi);
642 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
643 * @pf: board private structure
645 * Reserves a VSI index from the PF and allocates an empty VSI structure
646 * without a type. The VSI structure must later be initialized by calling
649 * returns a pointer to a VSI on success, NULL on failure.
651 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
653 struct device *dev = ice_pf_to_dev(pf);
654 struct ice_vsi *vsi = NULL;
656 /* Need to protect the allocation of the VSIs at the PF level */
657 mutex_lock(&pf->sw_mutex);
659 /* If we have already allocated our maximum number of VSIs,
660 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
661 * is available to be populated
663 if (pf->next_vsi == ICE_NO_VSI) {
664 dev_dbg(dev, "out of VSI slots!\n");
668 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
673 set_bit(ICE_VSI_DOWN, vsi->state);
675 /* fill slot and make note of the index */
676 vsi->idx = pf->next_vsi;
677 pf->vsi[pf->next_vsi] = vsi;
679 /* prepare pf->next_vsi for next use */
680 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
684 mutex_unlock(&pf->sw_mutex);
689 * ice_alloc_fd_res - Allocate FD resource for a VSI
690 * @vsi: pointer to the ice_vsi
692 * This allocates the FD resources
694 * Returns 0 on success, -EPERM on no-op or -EIO on failure
696 static int ice_alloc_fd_res(struct ice_vsi *vsi)
698 struct ice_pf *pf = vsi->back;
701 /* Flow Director filters are only allocated/assigned to the PF VSI or
702 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
703 * add/delete filters so resources are not allocated to it
705 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
708 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
709 vsi->type == ICE_VSI_CHNL))
712 /* FD filters from guaranteed pool per VSI */
713 g_val = pf->hw.func_caps.fd_fltr_guar;
717 /* FD filters from best effort pool */
718 b_val = pf->hw.func_caps.fd_fltr_best_effort;
722 /* PF main VSI gets only 64 FD resources from guaranteed pool
723 * when ADQ is configured.
725 #define ICE_PF_VSI_GFLTR 64
727 /* determine FD filter resources per VSI from shared(best effort) and
730 if (vsi->type == ICE_VSI_PF) {
731 vsi->num_gfltr = g_val;
732 /* if MQPRIO is configured, main VSI doesn't get all FD
733 * resources from guaranteed pool. PF VSI gets 64 FD resources
735 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
736 if (g_val < ICE_PF_VSI_GFLTR)
738 /* allow bare minimum entries for PF VSI */
739 vsi->num_gfltr = ICE_PF_VSI_GFLTR;
742 /* each VSI gets same "best_effort" quota */
743 vsi->num_bfltr = b_val;
744 } else if (vsi->type == ICE_VSI_VF) {
747 /* each VSI gets same "best_effort" quota */
748 vsi->num_bfltr = b_val;
750 struct ice_vsi *main_vsi;
753 main_vsi = ice_get_main_vsi(pf);
757 if (!main_vsi->all_numtc)
760 /* figure out ADQ numtc */
761 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
763 /* only one TC but still asking resources for channels,
766 if (numtc < ICE_CHNL_START_TC)
769 g_val -= ICE_PF_VSI_GFLTR;
770 /* channel VSIs gets equal share from guaranteed pool */
771 vsi->num_gfltr = g_val / numtc;
773 /* each VSI gets same "best_effort" quota */
774 vsi->num_bfltr = b_val;
781 * ice_vsi_get_qs - Assign queues from PF to VSI
782 * @vsi: the VSI to assign queues to
784 * Returns 0 on success and a negative value on error
786 static int ice_vsi_get_qs(struct ice_vsi *vsi)
788 struct ice_pf *pf = vsi->back;
789 struct ice_qs_cfg tx_qs_cfg = {
790 .qs_mutex = &pf->avail_q_mutex,
791 .pf_map = pf->avail_txqs,
792 .pf_map_size = pf->max_pf_txqs,
793 .q_count = vsi->alloc_txq,
794 .scatter_count = ICE_MAX_SCATTER_TXQS,
795 .vsi_map = vsi->txq_map,
797 .mapping_mode = ICE_VSI_MAP_CONTIG
799 struct ice_qs_cfg rx_qs_cfg = {
800 .qs_mutex = &pf->avail_q_mutex,
801 .pf_map = pf->avail_rxqs,
802 .pf_map_size = pf->max_pf_rxqs,
803 .q_count = vsi->alloc_rxq,
804 .scatter_count = ICE_MAX_SCATTER_RXQS,
805 .vsi_map = vsi->rxq_map,
807 .mapping_mode = ICE_VSI_MAP_CONTIG
811 if (vsi->type == ICE_VSI_CHNL)
814 ret = __ice_vsi_get_qs(&tx_qs_cfg);
817 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
819 ret = __ice_vsi_get_qs(&rx_qs_cfg);
822 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
828 * ice_vsi_put_qs - Release queues from VSI to PF
829 * @vsi: the VSI that is going to release queues
831 static void ice_vsi_put_qs(struct ice_vsi *vsi)
833 struct ice_pf *pf = vsi->back;
836 mutex_lock(&pf->avail_q_mutex);
838 ice_for_each_alloc_txq(vsi, i) {
839 clear_bit(vsi->txq_map[i], pf->avail_txqs);
840 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
843 ice_for_each_alloc_rxq(vsi, i) {
844 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
845 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
848 mutex_unlock(&pf->avail_q_mutex);
853 * @pf: pointer to the PF struct
855 * returns true if driver is in safe mode, false otherwise
857 bool ice_is_safe_mode(struct ice_pf *pf)
859 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
864 * @pf: pointer to the PF struct
866 * returns true if RDMA is currently supported, false otherwise
868 bool ice_is_rdma_ena(struct ice_pf *pf)
870 return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
874 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
875 * @vsi: the VSI being cleaned up
877 * This function deletes RSS input set for all flows that were configured
880 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
882 struct ice_pf *pf = vsi->back;
885 if (ice_is_safe_mode(pf))
888 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
890 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
891 vsi->vsi_num, status);
895 * ice_rss_clean - Delete RSS related VSI structures and configuration
896 * @vsi: the VSI being removed
898 static void ice_rss_clean(struct ice_vsi *vsi)
900 struct ice_pf *pf = vsi->back;
903 dev = ice_pf_to_dev(pf);
905 if (vsi->rss_hkey_user)
906 devm_kfree(dev, vsi->rss_hkey_user);
907 if (vsi->rss_lut_user)
908 devm_kfree(dev, vsi->rss_lut_user);
910 ice_vsi_clean_rss_flow_fld(vsi);
911 /* remove RSS replay list */
912 if (!ice_is_safe_mode(pf))
913 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
917 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
918 * @vsi: the VSI being configured
920 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
922 struct ice_hw_common_caps *cap;
923 struct ice_pf *pf = vsi->back;
925 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
930 cap = &pf->hw.func_caps.common_cap;
934 /* PF VSI will inherit RSS instance of PF */
935 vsi->rss_table_size = (u16)cap->rss_table_size;
936 if (vsi->type == ICE_VSI_CHNL)
937 vsi->rss_size = min_t(u16, vsi->num_rxq,
938 BIT(cap->rss_table_entry_width));
940 vsi->rss_size = min_t(u16, num_online_cpus(),
941 BIT(cap->rss_table_entry_width));
942 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
944 case ICE_VSI_SWITCHDEV_CTRL:
945 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
946 vsi->rss_size = min_t(u16, num_online_cpus(),
947 BIT(cap->rss_table_entry_width));
948 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
951 /* VF VSI will get a small RSS table.
952 * For VSI_LUT, LUT size should be set to 64 bytes.
954 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
955 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
956 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
961 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
962 ice_vsi_type_str(vsi->type));
968 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
969 * @hw: HW structure used to determine the VLAN mode of the device
970 * @ctxt: the VSI context being set
972 * This initializes a default VSI context for all sections except the Queues.
974 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
978 memset(&ctxt->info, 0, sizeof(ctxt->info));
979 /* VSI's should be allocated from shared pool */
980 ctxt->alloc_from_pool = true;
981 /* Src pruning enabled by default */
982 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
983 /* Traffic from VSI can be sent to LAN */
984 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
985 /* allow all untagged/tagged packets by default on Tx */
986 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
987 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
988 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
989 /* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
990 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
992 * DVM - leave inner VLAN in packet by default
994 if (ice_is_dvm_ena(hw)) {
995 ctxt->info.inner_vlan_flags |=
996 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
997 ctxt->info.outer_vlan_flags =
998 (ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
999 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
1000 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M;
1001 ctxt->info.outer_vlan_flags |=
1002 (ICE_AQ_VSI_OUTER_TAG_VLAN_8100 <<
1003 ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
1004 ICE_AQ_VSI_OUTER_TAG_TYPE_M;
1005 ctxt->info.outer_vlan_flags |=
1006 FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
1007 ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
1009 /* Have 1:1 UP mapping for both ingress/egress tables */
1010 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1011 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1012 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1013 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1014 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1015 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1016 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1017 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1018 ctxt->info.ingress_table = cpu_to_le32(table);
1019 ctxt->info.egress_table = cpu_to_le32(table);
1020 /* Have 1:1 UP mapping for outer to inner UP table */
1021 ctxt->info.outer_up_table = cpu_to_le32(table);
1022 /* No Outer tag support outer_tag_flags remains to zero */
1026 * ice_vsi_setup_q_map - Setup a VSI queue map
1027 * @vsi: the VSI being configured
1028 * @ctxt: VSI context structure
1030 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1032 u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
1033 u16 num_txq_per_tc, num_rxq_per_tc;
1034 u16 qcount_tx = vsi->alloc_txq;
1035 u16 qcount_rx = vsi->alloc_rxq;
1039 if (!vsi->tc_cfg.numtc) {
1040 /* at least TC0 should be enabled by default */
1041 vsi->tc_cfg.numtc = 1;
1042 vsi->tc_cfg.ena_tc = 1;
1045 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
1046 if (!num_rxq_per_tc)
1048 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
1049 if (!num_txq_per_tc)
1052 /* find the (rounded up) power-of-2 of qcount */
1053 pow = (u16)order_base_2(num_rxq_per_tc);
1055 /* TC mapping is a function of the number of Rx queues assigned to the
1056 * VSI for each traffic class and the offset of these queues.
1057 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1058 * queues allocated to TC0. No:of queues is a power-of-2.
1060 * If TC is not enabled, the queue offset is set to 0, and allocate one
1061 * queue, this way, traffic for the given TC will be sent to the default
1064 * Setup number and offset of Rx queues for all TCs for the VSI
1066 ice_for_each_traffic_class(i) {
1067 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1068 /* TC is not enabled */
1069 vsi->tc_cfg.tc_info[i].qoffset = 0;
1070 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1071 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1072 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1073 ctxt->info.tc_mapping[i] = 0;
1078 vsi->tc_cfg.tc_info[i].qoffset = offset;
1079 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1080 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1081 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1083 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1084 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1085 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1086 ICE_AQ_VSI_TC_Q_NUM_M);
1087 offset += num_rxq_per_tc;
1088 tx_count += num_txq_per_tc;
1089 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1092 /* if offset is non-zero, means it is calculated correctly based on
1093 * enabled TCs for a given VSI otherwise qcount_rx will always
1094 * be correct and non-zero because it is based off - VSI's
1095 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1101 rx_count = num_rxq_per_tc;
1103 if (rx_count > vsi->alloc_rxq) {
1104 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1105 rx_count, vsi->alloc_rxq);
1109 if (tx_count > vsi->alloc_txq) {
1110 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1111 tx_count, vsi->alloc_txq);
1115 vsi->num_txq = tx_count;
1116 vsi->num_rxq = rx_count;
1118 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1119 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1120 /* since there is a chance that num_rxq could have been changed
1121 * in the above for loop, make num_txq equal to num_rxq.
1123 vsi->num_txq = vsi->num_rxq;
1126 /* Rx queue mapping */
1127 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1128 /* q_mapping buffer holds the info for the first queue allocated for
1129 * this VSI in the PF space and also the number of queues associated
1132 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1133 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1139 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1140 * @ctxt: the VSI context being set
1141 * @vsi: the VSI being configured
1143 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1145 u8 dflt_q_group, dflt_q_prio;
1146 u16 dflt_q, report_q, val;
1148 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1149 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1152 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1153 ctxt->info.valid_sections |= cpu_to_le16(val);
1159 /* enable flow director filtering/programming */
1160 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1161 ctxt->info.fd_options = cpu_to_le16(val);
1162 /* max of allocated flow director filters */
1163 ctxt->info.max_fd_fltr_dedicated =
1164 cpu_to_le16(vsi->num_gfltr);
1165 /* max of shared flow director filters any VSI may program */
1166 ctxt->info.max_fd_fltr_shared =
1167 cpu_to_le16(vsi->num_bfltr);
1168 /* default queue index within the VSI of the default FD */
1169 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
1170 ICE_AQ_VSI_FD_DEF_Q_M);
1171 /* target queue or queue group to the FD filter */
1172 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
1173 ICE_AQ_VSI_FD_DEF_GRP_M);
1174 ctxt->info.fd_def_q = cpu_to_le16(val);
1175 /* queue index on which FD filter completion is reported */
1176 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
1177 ICE_AQ_VSI_FD_REPORT_Q_M);
1178 /* priority of the default qindex action */
1179 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
1180 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
1181 ctxt->info.fd_report_opt = cpu_to_le16(val);
1185 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1186 * @ctxt: the VSI context being set
1187 * @vsi: the VSI being configured
1189 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1191 u8 lut_type, hash_type;
1196 dev = ice_pf_to_dev(pf);
1198 switch (vsi->type) {
1201 /* PF VSI will inherit RSS instance of PF */
1202 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1203 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1206 /* VF VSI will gets a small RSS table which is a VSI LUT type */
1207 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1208 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1211 dev_dbg(dev, "Unsupported VSI type %s\n",
1212 ice_vsi_type_str(vsi->type));
1216 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1217 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1218 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1219 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1223 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1225 struct ice_pf *pf = vsi->back;
1230 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1232 pow = order_base_2(qcount);
1233 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1234 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1235 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1236 ICE_AQ_VSI_TC_Q_NUM_M);
1238 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1239 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1240 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1241 ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1245 * ice_vsi_init - Create and initialize a VSI
1246 * @vsi: the VSI being configured
1247 * @vsi_flags: VSI configuration flags
1249 * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1250 * reconfigure an existing context.
1252 * This initializes a VSI context depending on the VSI type to be added and
1253 * passes it down to the add_vsi aq command to create a new VSI.
1255 static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1257 struct ice_pf *pf = vsi->back;
1258 struct ice_hw *hw = &pf->hw;
1259 struct ice_vsi_ctx *ctxt;
1263 dev = ice_pf_to_dev(pf);
1264 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1268 switch (vsi->type) {
1272 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1274 case ICE_VSI_SWITCHDEV_CTRL:
1276 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1279 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1280 /* VF number here is the absolute VF number (0-255) */
1281 ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1288 /* Handle VLAN pruning for channel VSI if main VSI has VLAN
1291 if (vsi->type == ICE_VSI_CHNL) {
1292 struct ice_vsi *main_vsi;
1294 main_vsi = ice_get_main_vsi(pf);
1295 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1296 ctxt->info.sw_flags2 |=
1297 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1299 ctxt->info.sw_flags2 &=
1300 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1303 ice_set_dflt_vsi_ctx(hw, ctxt);
1304 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1305 ice_set_fd_vsi_ctx(ctxt, vsi);
1306 /* if the switch is in VEB mode, allow VSI loopback */
1307 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1308 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1310 /* Set LUT type and HASH type if RSS is enabled */
1311 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1312 vsi->type != ICE_VSI_CTRL) {
1313 ice_set_rss_vsi_ctx(ctxt, vsi);
1314 /* if updating VSI context, make sure to set valid_section:
1315 * to indicate which section of VSI context being updated
1317 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1318 ctxt->info.valid_sections |=
1319 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1322 ctxt->info.sw_id = vsi->port_info->sw_id;
1323 if (vsi->type == ICE_VSI_CHNL) {
1324 ice_chnl_vsi_setup_q_map(vsi, ctxt);
1326 ret = ice_vsi_setup_q_map(vsi, ctxt);
1330 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1331 /* means VSI being updated */
1332 /* must to indicate which section of VSI context are
1335 ctxt->info.valid_sections |=
1336 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1339 /* Allow control frames out of main VSI */
1340 if (vsi->type == ICE_VSI_PF) {
1341 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1342 ctxt->info.valid_sections |=
1343 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1346 if (vsi_flags & ICE_VSI_FLAG_INIT) {
1347 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1349 dev_err(dev, "Add VSI failed, err %d\n", ret);
1354 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1356 dev_err(dev, "Update VSI failed, err %d\n", ret);
1362 /* keep context for update VSI operations */
1363 vsi->info = ctxt->info;
1365 /* record VSI number returned */
1366 vsi->vsi_num = ctxt->vsi_num;
1374 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1375 * @vsi: the VSI having rings deallocated
1377 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1381 /* Avoid stale references by clearing map from vector to ring */
1382 if (vsi->q_vectors) {
1383 ice_for_each_q_vector(vsi, i) {
1384 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1387 q_vector->tx.tx_ring = NULL;
1388 q_vector->rx.rx_ring = NULL;
1393 if (vsi->tx_rings) {
1394 ice_for_each_alloc_txq(vsi, i) {
1395 if (vsi->tx_rings[i]) {
1396 kfree_rcu(vsi->tx_rings[i], rcu);
1397 WRITE_ONCE(vsi->tx_rings[i], NULL);
1401 if (vsi->rx_rings) {
1402 ice_for_each_alloc_rxq(vsi, i) {
1403 if (vsi->rx_rings[i]) {
1404 kfree_rcu(vsi->rx_rings[i], rcu);
1405 WRITE_ONCE(vsi->rx_rings[i], NULL);
1412 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1413 * @vsi: VSI which is having rings allocated
1415 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1417 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1418 struct ice_pf *pf = vsi->back;
1422 dev = ice_pf_to_dev(pf);
1423 /* Allocate Tx rings */
1424 ice_for_each_alloc_txq(vsi, i) {
1425 struct ice_tx_ring *ring;
1427 /* allocate with kzalloc(), free with kfree_rcu() */
1428 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1434 ring->reg_idx = vsi->txq_map[i];
1436 ring->tx_tstamps = &pf->ptp.port.tx;
1438 ring->count = vsi->num_tx_desc;
1439 ring->txq_teid = ICE_INVAL_TEID;
1441 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1443 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1444 WRITE_ONCE(vsi->tx_rings[i], ring);
1447 /* Allocate Rx rings */
1448 ice_for_each_alloc_rxq(vsi, i) {
1449 struct ice_rx_ring *ring;
1451 /* allocate with kzalloc(), free with kfree_rcu() */
1452 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1457 ring->reg_idx = vsi->rxq_map[i];
1459 ring->netdev = vsi->netdev;
1461 ring->count = vsi->num_rx_desc;
1462 ring->cached_phctime = pf->ptp.cached_phc_time;
1463 WRITE_ONCE(vsi->rx_rings[i], ring);
1469 ice_vsi_clear_rings(vsi);
1474 * ice_vsi_manage_rss_lut - disable/enable RSS
1475 * @vsi: the VSI being changed
1476 * @ena: boolean value indicating if this is an enable or disable request
1478 * In the event of disable request for RSS, this function will zero out RSS
1479 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1482 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1486 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1491 if (vsi->rss_lut_user)
1492 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1494 ice_fill_rss_lut(lut, vsi->rss_table_size,
1498 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1503 * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1504 * @vsi: VSI to be configured
1505 * @disable: set to true to have FCS / CRC in the frame data
1507 void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1511 ice_for_each_rxq(vsi, i)
1513 vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1515 vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1519 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1520 * @vsi: VSI to be configured
1522 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1524 struct ice_pf *pf = vsi->back;
1529 dev = ice_pf_to_dev(pf);
1530 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1531 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1532 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1534 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1536 /* If orig_rss_size is valid and it is less than determined
1537 * main VSI's rss_size, update main VSI's rss_size to be
1538 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1539 * RSS table gets programmed to be correct (whatever it was
1540 * to begin with (prior to setup-tc for ADQ config)
1542 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1543 vsi->orig_rss_size <= vsi->num_rxq) {
1544 vsi->rss_size = vsi->orig_rss_size;
1545 /* now orig_rss_size is used, reset it to zero */
1546 vsi->orig_rss_size = 0;
1550 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1554 if (vsi->rss_lut_user)
1555 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1557 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1559 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1561 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1562 goto ice_vsi_cfg_rss_exit;
1565 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1568 goto ice_vsi_cfg_rss_exit;
1571 if (vsi->rss_hkey_user)
1572 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1574 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1576 err = ice_set_rss_key(vsi, key);
1578 dev_err(dev, "set_rss_key failed, error %d\n", err);
1581 ice_vsi_cfg_rss_exit:
1587 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1588 * @vsi: VSI to be configured
1590 * This function will only be called during the VF VSI setup. Upon successful
1591 * completion of package download, this function will configure default RSS
1592 * input sets for VF VSI.
1594 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1596 struct ice_pf *pf = vsi->back;
1600 dev = ice_pf_to_dev(pf);
1601 if (ice_is_safe_mode(pf)) {
1602 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1607 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1609 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1610 vsi->vsi_num, status);
1614 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1615 * @vsi: VSI to be configured
1617 * This function will only be called after successful download package call
1618 * during initialization of PF. Since the downloaded package will erase the
1619 * RSS section, this function will configure RSS input sets for different
1620 * flow types. The last profile added has the highest priority, therefore 2
1621 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1622 * (i.e. IPv4 src/dst TCP src/dst port).
1624 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1626 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1627 struct ice_pf *pf = vsi->back;
1628 struct ice_hw *hw = &pf->hw;
1632 dev = ice_pf_to_dev(pf);
1633 if (ice_is_safe_mode(pf)) {
1634 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1638 /* configure RSS for IPv4 with input set IP src/dst */
1639 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1640 ICE_FLOW_SEG_HDR_IPV4);
1642 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1645 /* configure RSS for IPv6 with input set IPv6 src/dst */
1646 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1647 ICE_FLOW_SEG_HDR_IPV6);
1649 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1652 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1653 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1654 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1656 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1659 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1660 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1661 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1663 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1666 /* configure RSS for sctp4 with input set IP src/dst */
1667 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1668 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1670 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1673 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1674 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1675 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1677 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1680 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1681 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1682 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1684 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1687 /* configure RSS for sctp6 with input set IPv6 src/dst */
1688 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1689 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1691 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1694 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_ESP_SPI,
1695 ICE_FLOW_SEG_HDR_ESP);
1697 dev_dbg(dev, "ice_add_rss_cfg failed for esp/spi flow, vsi = %d, error = %d\n",
1702 * ice_pf_state_is_nominal - checks the PF for nominal state
1703 * @pf: pointer to PF to check
1705 * Check the PF's state for a collection of bits that would indicate
1706 * the PF is in a state that would inhibit normal operation for
1707 * driver functionality.
1709 * Returns true if PF is in a nominal state, false otherwise
1711 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1713 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1718 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1719 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1726 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1727 * @vsi: the VSI to be updated
1729 void ice_update_eth_stats(struct ice_vsi *vsi)
1731 struct ice_eth_stats *prev_es, *cur_es;
1732 struct ice_hw *hw = &vsi->back->hw;
1733 struct ice_pf *pf = vsi->back;
1734 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1736 prev_es = &vsi->eth_stats_prev;
1737 cur_es = &vsi->eth_stats;
1739 if (ice_is_reset_in_progress(pf->state))
1740 vsi->stat_offsets_loaded = false;
1742 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1743 &prev_es->rx_bytes, &cur_es->rx_bytes);
1745 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1746 &prev_es->rx_unicast, &cur_es->rx_unicast);
1748 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1749 &prev_es->rx_multicast, &cur_es->rx_multicast);
1751 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1752 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1754 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1755 &prev_es->rx_discards, &cur_es->rx_discards);
1757 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1758 &prev_es->tx_bytes, &cur_es->tx_bytes);
1760 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1761 &prev_es->tx_unicast, &cur_es->tx_unicast);
1763 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1764 &prev_es->tx_multicast, &cur_es->tx_multicast);
1766 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1767 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1769 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1770 &prev_es->tx_errors, &cur_es->tx_errors);
1772 vsi->stat_offsets_loaded = true;
1776 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1779 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1781 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1782 vsi->max_frame = ICE_MAX_FRAME_LEGACY_RX;
1783 vsi->rx_buf_len = ICE_RXBUF_1664;
1784 #if (PAGE_SIZE < 8192)
1785 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1786 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1787 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1788 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1791 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1792 vsi->rx_buf_len = ICE_RXBUF_3072;
1797 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1799 * @pf_q: index of the Rx queue in the PF's queue space
1800 * @rxdid: flexible descriptor RXDID
1801 * @prio: priority for the RXDID for this queue
1802 * @ena_ts: true to enable timestamp and false to disable timestamp
1805 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1808 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1810 /* clear any previous values */
1811 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1812 QRXFLXP_CNTXT_RXDID_PRIO_M |
1813 QRXFLXP_CNTXT_TS_M);
1815 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1816 QRXFLXP_CNTXT_RXDID_IDX_M;
1818 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1819 QRXFLXP_CNTXT_RXDID_PRIO_M;
1822 /* Enable TimeSync on this queue */
1823 regval |= QRXFLXP_CNTXT_TS_M;
1825 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1828 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1830 if (q_idx >= vsi->num_rxq)
1833 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1836 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1838 struct ice_aqc_add_tx_qgrp *qg_buf;
1841 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1844 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1848 qg_buf->num_txqs = 1;
1850 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1856 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1857 * @vsi: the VSI being configured
1859 * Return 0 on success and a negative value on error
1860 * Configure the Rx VSI for operation.
1862 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1866 if (vsi->type == ICE_VSI_VF)
1869 ice_vsi_cfg_frame_size(vsi);
1871 /* set up individual rings */
1872 ice_for_each_rxq(vsi, i) {
1873 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1883 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1884 * @vsi: the VSI being configured
1885 * @rings: Tx ring array to be configured
1886 * @count: number of Tx ring array elements
1888 * Return 0 on success and a negative value on error
1889 * Configure the Tx VSI for operation.
1892 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1894 struct ice_aqc_add_tx_qgrp *qg_buf;
1898 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1902 qg_buf->num_txqs = 1;
1904 for (q_idx = 0; q_idx < count; q_idx++) {
1905 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1916 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1917 * @vsi: the VSI being configured
1919 * Return 0 on success and a negative value on error
1920 * Configure the Tx VSI for operation.
1922 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1924 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1928 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1929 * @vsi: the VSI being configured
1931 * Return 0 on success and a negative value on error
1932 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1934 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1939 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1943 ice_for_each_rxq(vsi, i)
1944 ice_tx_xsk_pool(vsi, i);
1950 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1951 * @intrl: interrupt rate limit in usecs
1952 * @gran: interrupt rate limit granularity in usecs
1954 * This function converts a decimal interrupt rate limit in usecs to the format
1955 * expected by firmware.
1957 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1959 u32 val = intrl / gran;
1962 return val | GLINT_RATE_INTRL_ENA_M;
1967 * ice_write_intrl - write throttle rate limit to interrupt specific register
1968 * @q_vector: pointer to interrupt specific structure
1969 * @intrl: throttle rate limit in microseconds to write
1971 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1973 struct ice_hw *hw = &q_vector->vsi->back->hw;
1975 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1976 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1979 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1982 case ICE_RX_CONTAINER:
1984 return rc->rx_ring->q_vector;
1986 case ICE_TX_CONTAINER:
1988 return rc->tx_ring->q_vector;
1998 * __ice_write_itr - write throttle rate to register
1999 * @q_vector: pointer to interrupt data structure
2000 * @rc: pointer to ring container
2001 * @itr: throttle rate in microseconds to write
2003 static void __ice_write_itr(struct ice_q_vector *q_vector,
2004 struct ice_ring_container *rc, u16 itr)
2006 struct ice_hw *hw = &q_vector->vsi->back->hw;
2008 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
2009 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
2013 * ice_write_itr - write throttle rate to queue specific register
2014 * @rc: pointer to ring container
2015 * @itr: throttle rate in microseconds to write
2017 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
2019 struct ice_q_vector *q_vector;
2021 q_vector = ice_pull_qvec_from_rc(rc);
2025 __ice_write_itr(q_vector, rc, itr);
2029 * ice_set_q_vector_intrl - set up interrupt rate limiting
2030 * @q_vector: the vector to be configured
2032 * Interrupt rate limiting is local to the vector, not per-queue so we must
2033 * detect if either ring container has dynamic moderation enabled to decide
2034 * what to set the interrupt rate limit to via INTRL settings. In the case that
2035 * dynamic moderation is disabled on both, write the value with the cached
2036 * setting to make sure INTRL register matches the user visible value.
2038 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2040 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2041 /* in the case of dynamic enabled, cap each vector to no more
2042 * than (4 us) 250,000 ints/sec, which allows low latency
2043 * but still less than 500,000 interrupts per second, which
2044 * reduces CPU a bit in the case of the lowest latency
2045 * setting. The 4 here is a value in microseconds.
2047 ice_write_intrl(q_vector, 4);
2049 ice_write_intrl(q_vector, q_vector->intrl);
2054 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2055 * @vsi: the VSI being configured
2057 * This configures MSIX mode interrupts for the PF VSI, and should not be used
2060 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2062 struct ice_pf *pf = vsi->back;
2063 struct ice_hw *hw = &pf->hw;
2064 u16 txq = 0, rxq = 0;
2067 ice_for_each_q_vector(vsi, i) {
2068 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2069 u16 reg_idx = q_vector->reg_idx;
2071 ice_cfg_itr(hw, q_vector);
2073 /* Both Transmit Queue Interrupt Cause Control register
2074 * and Receive Queue Interrupt Cause control register
2075 * expects MSIX_INDX field to be the vector index
2076 * within the function space and not the absolute
2077 * vector index across PF or across device.
2078 * For SR-IOV VF VSIs queue vector index always starts
2079 * with 1 since first vector index(0) is used for OICR
2080 * in VF space. Since VMDq and other PF VSIs are within
2081 * the PF function space, use the vector index that is
2082 * tracked for this PF.
2084 for (q = 0; q < q_vector->num_ring_tx; q++) {
2085 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2086 q_vector->tx.itr_idx);
2090 for (q = 0; q < q_vector->num_ring_rx; q++) {
2091 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2092 q_vector->rx.itr_idx);
2099 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2100 * @vsi: the VSI whose rings are to be enabled
2102 * Returns 0 on success and a negative value on error
2104 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2106 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2110 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2111 * @vsi: the VSI whose rings are to be disabled
2113 * Returns 0 on success and a negative value on error
2115 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2117 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2121 * ice_vsi_stop_tx_rings - Disable Tx rings
2122 * @vsi: the VSI being configured
2123 * @rst_src: reset source
2124 * @rel_vmvf_num: Relative ID of VF/VM
2125 * @rings: Tx ring array to be stopped
2126 * @count: number of Tx ring array elements
2129 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2130 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2134 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2137 for (q_idx = 0; q_idx < count; q_idx++) {
2138 struct ice_txq_meta txq_meta = { };
2141 if (!rings || !rings[q_idx])
2144 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2145 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2146 rings[q_idx], &txq_meta);
2156 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2157 * @vsi: the VSI being configured
2158 * @rst_src: reset source
2159 * @rel_vmvf_num: Relative ID of VF/VM
2162 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2165 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2169 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2170 * @vsi: the VSI being configured
2172 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2174 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2178 * ice_vsi_is_rx_queue_active
2179 * @vsi: the VSI being configured
2181 * Return true if at least one queue is active.
2183 bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
2185 struct ice_pf *pf = vsi->back;
2186 struct ice_hw *hw = &pf->hw;
2189 ice_for_each_rxq(vsi, i) {
2193 pf_q = vsi->rxq_map[i];
2194 rx_reg = rd32(hw, QRX_CTRL(pf_q));
2195 if (rx_reg & QRX_CTRL_QENA_STAT_M)
2203 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2204 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2206 * returns true if Rx VLAN pruning is enabled and false otherwise.
2208 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2213 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2216 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2218 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2219 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2220 vsi->tc_cfg.numtc = 1;
2224 /* set VSI TC information based on DCB config */
2225 ice_vsi_set_dcb_tc_cfg(vsi);
2229 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2230 * @vsi: the VSI being configured
2231 * @tx: bool to determine Tx or Rx rule
2232 * @create: bool to determine create or remove Rule
2234 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2236 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2237 enum ice_sw_fwd_act_type act);
2238 struct ice_pf *pf = vsi->back;
2242 dev = ice_pf_to_dev(pf);
2243 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2246 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2249 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2250 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2253 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2259 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2260 create ? "adding" : "removing", tx ? "TX" : "RX",
2261 vsi->vsi_num, status);
2265 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2266 * @vsi: pointer to the VSI
2268 * This function will allocate new scheduler aggregator now if needed and will
2269 * move specified VSI into it.
2271 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2273 struct device *dev = ice_pf_to_dev(vsi->back);
2274 struct ice_agg_node *agg_node_iter = NULL;
2275 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2276 struct ice_agg_node *agg_node = NULL;
2277 int node_offset, max_agg_nodes = 0;
2278 struct ice_port_info *port_info;
2279 struct ice_pf *pf = vsi->back;
2280 u32 agg_node_id_start = 0;
2283 /* create (as needed) scheduler aggregator node and move VSI into
2284 * corresponding aggregator node
2285 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2286 * - VF aggregator nodes will contain VF VSI
2288 port_info = pf->hw.port_info;
2292 switch (vsi->type) {
2297 case ICE_VSI_SWITCHDEV_CTRL:
2298 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2299 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2300 agg_node_iter = &pf->pf_agg_node[0];
2303 /* user can create 'n' VFs on a given PF, but since max children
2304 * per aggregator node can be only 64. Following code handles
2305 * aggregator(s) for VF VSIs, either selects a agg_node which
2306 * was already created provided num_vsis < 64, otherwise
2307 * select next available node, which will be created
2309 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2310 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2311 agg_node_iter = &pf->vf_agg_node[0];
2314 /* other VSI type, handle later if needed */
2315 dev_dbg(dev, "unexpected VSI type %s\n",
2316 ice_vsi_type_str(vsi->type));
2320 /* find the appropriate aggregator node */
2321 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2322 /* see if we can find space in previously created
2323 * node if num_vsis < 64, otherwise skip
2325 if (agg_node_iter->num_vsis &&
2326 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2331 if (agg_node_iter->valid &&
2332 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2333 agg_id = agg_node_iter->agg_id;
2334 agg_node = agg_node_iter;
2338 /* find unclaimed agg_id */
2339 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2340 agg_id = node_offset + agg_node_id_start;
2341 agg_node = agg_node_iter;
2344 /* move to next agg_node */
2351 /* if selected aggregator node was not created, create it */
2352 if (!agg_node->valid) {
2353 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2354 (u8)vsi->tc_cfg.ena_tc);
2356 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2360 /* aggregator node is created, store the needed info */
2361 agg_node->valid = true;
2362 agg_node->agg_id = agg_id;
2365 /* move VSI to corresponding aggregator node */
2366 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2367 (u8)vsi->tc_cfg.ena_tc);
2369 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2374 /* keep active children count for aggregator node */
2375 agg_node->num_vsis++;
2377 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2378 * to aggregator node
2380 vsi->agg_node = agg_node;
2381 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2382 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2383 vsi->agg_node->num_vsis);
2386 static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
2388 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2389 struct device *dev = ice_pf_to_dev(pf);
2392 /* configure VSI nodes based on number of queues and TC's */
2393 ice_for_each_traffic_class(i) {
2394 if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2397 if (vsi->type == ICE_VSI_CHNL) {
2398 if (!vsi->alloc_txq && vsi->num_txq)
2399 max_txqs[i] = vsi->num_txq;
2401 max_txqs[i] = pf->num_lan_tx;
2403 max_txqs[i] = vsi->alloc_txq;
2407 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2408 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2411 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2420 * ice_vsi_cfg_def - configure default VSI based on the type
2421 * @vsi: pointer to VSI
2422 * @params: the parameters to configure this VSI with
2425 ice_vsi_cfg_def(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2427 struct device *dev = ice_pf_to_dev(vsi->back);
2428 struct ice_pf *pf = vsi->back;
2431 vsi->vsw = pf->first_sw;
2433 ret = ice_vsi_alloc_def(vsi, params->ch);
2437 /* allocate memory for Tx/Rx ring stat pointers */
2438 ret = ice_vsi_alloc_stat_arrays(vsi);
2440 goto unroll_vsi_alloc;
2442 ice_alloc_fd_res(vsi);
2444 ret = ice_vsi_get_qs(vsi);
2446 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2448 goto unroll_vsi_alloc_stat;
2451 /* set RSS capabilities */
2452 ice_vsi_set_rss_params(vsi);
2454 /* set TC configuration */
2455 ice_vsi_set_tc_cfg(vsi);
2457 /* create the VSI */
2458 ret = ice_vsi_init(vsi, params->flags);
2462 ice_vsi_init_vlan_ops(vsi);
2464 switch (vsi->type) {
2466 case ICE_VSI_SWITCHDEV_CTRL:
2468 ret = ice_vsi_alloc_q_vectors(vsi);
2470 goto unroll_vsi_init;
2472 ret = ice_vsi_alloc_rings(vsi);
2474 goto unroll_vector_base;
2476 ret = ice_vsi_alloc_ring_stats(vsi);
2478 goto unroll_vector_base;
2480 ice_vsi_map_rings_to_vectors(vsi);
2481 if (ice_is_xdp_ena_vsi(vsi)) {
2482 ret = ice_vsi_determine_xdp_res(vsi);
2484 goto unroll_vector_base;
2485 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2487 goto unroll_vector_base;
2490 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2491 if (vsi->type != ICE_VSI_CTRL)
2492 /* Do not exit if configuring RSS had an issue, at
2493 * least receive traffic on first queue. Hence no
2494 * need to capture return value
2496 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2497 ice_vsi_cfg_rss_lut_key(vsi);
2498 ice_vsi_set_rss_flow_fld(vsi);
2503 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2504 ice_vsi_cfg_rss_lut_key(vsi);
2505 ice_vsi_set_rss_flow_fld(vsi);
2509 /* VF driver will take care of creating netdev for this type and
2510 * map queues to vectors through Virtchnl, PF driver only
2511 * creates a VSI and corresponding structures for bookkeeping
2514 ret = ice_vsi_alloc_q_vectors(vsi);
2516 goto unroll_vsi_init;
2518 ret = ice_vsi_alloc_rings(vsi);
2520 goto unroll_alloc_q_vector;
2522 ret = ice_vsi_alloc_ring_stats(vsi);
2524 goto unroll_vector_base;
2525 /* Do not exit if configuring RSS had an issue, at least
2526 * receive traffic on first queue. Hence no need to capture
2529 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2530 ice_vsi_cfg_rss_lut_key(vsi);
2531 ice_vsi_set_vf_rss_flow_fld(vsi);
2535 ret = ice_vsi_alloc_rings(vsi);
2537 goto unroll_vsi_init;
2539 ret = ice_vsi_alloc_ring_stats(vsi);
2541 goto unroll_vector_base;
2545 /* clean up the resources and exit */
2547 goto unroll_vsi_init;
2553 /* reclaim SW interrupts back to the common pool */
2554 unroll_alloc_q_vector:
2555 ice_vsi_free_q_vectors(vsi);
2557 ice_vsi_delete_from_hw(vsi);
2559 ice_vsi_put_qs(vsi);
2560 unroll_vsi_alloc_stat:
2561 ice_vsi_free_stats(vsi);
2563 ice_vsi_free_arrays(vsi);
2568 * ice_vsi_cfg - configure a previously allocated VSI
2569 * @vsi: pointer to VSI
2570 * @params: parameters used to configure this VSI
2572 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2574 struct ice_pf *pf = vsi->back;
2577 if (WARN_ON(params->type == ICE_VSI_VF && !params->vf))
2580 vsi->type = params->type;
2581 vsi->port_info = params->pi;
2583 /* For VSIs which don't have a connected VF, this will be NULL */
2584 vsi->vf = params->vf;
2586 ret = ice_vsi_cfg_def(vsi, params);
2590 ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2594 if (vsi->type == ICE_VSI_CTRL) {
2596 WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2597 vsi->vf->ctrl_vsi_idx = vsi->idx;
2599 WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2600 pf->ctrl_vsi_idx = vsi->idx;
2608 * ice_vsi_decfg - remove all VSI configuration
2609 * @vsi: pointer to VSI
2611 void ice_vsi_decfg(struct ice_vsi *vsi)
2613 struct ice_pf *pf = vsi->back;
2616 /* The Rx rule will only exist to remove if the LLDP FW
2617 * engine is currently stopped
2619 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2620 !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2621 ice_cfg_sw_lldp(vsi, false, false);
2623 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2624 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2626 dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2629 if (ice_is_xdp_ena_vsi(vsi))
2630 /* return value check can be skipped here, it always returns
2631 * 0 if reset is in progress
2633 ice_destroy_xdp_rings(vsi);
2635 ice_vsi_clear_rings(vsi);
2636 ice_vsi_free_q_vectors(vsi);
2637 ice_vsi_put_qs(vsi);
2638 ice_vsi_free_arrays(vsi);
2640 /* SR-IOV determines needed MSIX resources all at once instead of per
2641 * VSI since when VFs are spawned we know how many VFs there are and how
2642 * many interrupts each VF needs. SR-IOV MSIX resources are also
2643 * cleared in the same manner.
2646 if (vsi->type == ICE_VSI_VF &&
2647 vsi->agg_node && vsi->agg_node->valid)
2648 vsi->agg_node->num_vsis--;
2649 if (vsi->agg_node) {
2650 vsi->agg_node->valid = false;
2651 vsi->agg_node->agg_id = 0;
2656 * ice_vsi_setup - Set up a VSI by a given type
2657 * @pf: board private structure
2658 * @params: parameters to use when creating the VSI
2660 * This allocates the sw VSI structure and its queue resources.
2662 * Returns pointer to the successfully allocated and configured VSI sw struct on
2663 * success, NULL on failure.
2666 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2668 struct device *dev = ice_pf_to_dev(pf);
2669 struct ice_vsi *vsi;
2672 /* ice_vsi_setup can only initialize a new VSI, and we must have
2673 * a port_info structure for it.
2675 if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2676 WARN_ON(!params->pi))
2679 vsi = ice_vsi_alloc(pf);
2681 dev_err(dev, "could not allocate VSI\n");
2685 ret = ice_vsi_cfg(vsi, params);
2689 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2690 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2691 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2692 * The rule is added once for PF VSI in order to create appropriate
2693 * recipe, since VSI/VSI list is ignored with drop action...
2694 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2695 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2696 * settings in the HW.
2698 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2699 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2701 ice_cfg_sw_lldp(vsi, true, true);
2705 ice_set_agg_vsi(vsi);
2710 if (params->type == ICE_VSI_VF)
2711 ice_enable_lag(pf->lag);
2718 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2719 * @vsi: the VSI being cleaned up
2721 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2723 struct ice_pf *pf = vsi->back;
2724 struct ice_hw *hw = &pf->hw;
2729 ice_for_each_q_vector(vsi, i) {
2730 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2732 ice_write_intrl(q_vector, 0);
2733 for (q = 0; q < q_vector->num_ring_tx; q++) {
2734 ice_write_itr(&q_vector->tx, 0);
2735 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2736 if (ice_is_xdp_ena_vsi(vsi)) {
2737 u32 xdp_txq = txq + vsi->num_xdp_txq;
2739 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2744 for (q = 0; q < q_vector->num_ring_rx; q++) {
2745 ice_write_itr(&q_vector->rx, 0);
2746 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2755 * ice_vsi_free_irq - Free the IRQ association with the OS
2756 * @vsi: the VSI being configured
2758 void ice_vsi_free_irq(struct ice_vsi *vsi)
2760 struct ice_pf *pf = vsi->back;
2763 if (!vsi->q_vectors || !vsi->irqs_ready)
2766 ice_vsi_release_msix(vsi);
2767 if (vsi->type == ICE_VSI_VF)
2770 vsi->irqs_ready = false;
2771 ice_free_cpu_rx_rmap(vsi);
2773 ice_for_each_q_vector(vsi, i) {
2776 irq_num = vsi->q_vectors[i]->irq.virq;
2778 /* free only the irqs that were actually requested */
2779 if (!vsi->q_vectors[i] ||
2780 !(vsi->q_vectors[i]->num_ring_tx ||
2781 vsi->q_vectors[i]->num_ring_rx))
2784 /* clear the affinity notifier in the IRQ descriptor */
2785 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2786 irq_set_affinity_notifier(irq_num, NULL);
2788 /* clear the affinity_mask in the IRQ descriptor */
2789 irq_set_affinity_hint(irq_num, NULL);
2790 synchronize_irq(irq_num);
2791 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2796 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2797 * @vsi: the VSI having resources freed
2799 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2806 ice_for_each_txq(vsi, i)
2807 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2808 ice_free_tx_ring(vsi->tx_rings[i]);
2812 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2813 * @vsi: the VSI having resources freed
2815 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2822 ice_for_each_rxq(vsi, i)
2823 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2824 ice_free_rx_ring(vsi->rx_rings[i]);
2828 * ice_vsi_close - Shut down a VSI
2829 * @vsi: the VSI being shut down
2831 void ice_vsi_close(struct ice_vsi *vsi)
2833 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2836 ice_vsi_free_irq(vsi);
2837 ice_vsi_free_tx_rings(vsi);
2838 ice_vsi_free_rx_rings(vsi);
2842 * ice_ena_vsi - resume a VSI
2843 * @vsi: the VSI being resume
2844 * @locked: is the rtnl_lock already held
2846 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2850 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2853 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2855 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2856 if (netif_running(vsi->netdev)) {
2860 err = ice_open_internal(vsi->netdev);
2865 } else if (vsi->type == ICE_VSI_CTRL) {
2866 err = ice_vsi_open_ctrl(vsi);
2873 * ice_dis_vsi - pause a VSI
2874 * @vsi: the VSI being paused
2875 * @locked: is the rtnl_lock already held
2877 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2879 if (test_bit(ICE_VSI_DOWN, vsi->state))
2882 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2884 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2885 if (netif_running(vsi->netdev)) {
2896 } else if (vsi->type == ICE_VSI_CTRL ||
2897 vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2903 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2904 * @vsi: the VSI being un-configured
2906 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2908 struct ice_pf *pf = vsi->back;
2909 struct ice_hw *hw = &pf->hw;
2913 /* disable interrupt causation from each queue */
2914 if (vsi->tx_rings) {
2915 ice_for_each_txq(vsi, i) {
2916 if (vsi->tx_rings[i]) {
2919 reg = vsi->tx_rings[i]->reg_idx;
2920 val = rd32(hw, QINT_TQCTL(reg));
2921 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2922 wr32(hw, QINT_TQCTL(reg), val);
2927 if (vsi->rx_rings) {
2928 ice_for_each_rxq(vsi, i) {
2929 if (vsi->rx_rings[i]) {
2932 reg = vsi->rx_rings[i]->reg_idx;
2933 val = rd32(hw, QINT_RQCTL(reg));
2934 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2935 wr32(hw, QINT_RQCTL(reg), val);
2940 /* disable each interrupt */
2941 ice_for_each_q_vector(vsi, i) {
2942 if (!vsi->q_vectors[i])
2944 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2949 /* don't call synchronize_irq() for VF's from the host */
2950 if (vsi->type == ICE_VSI_VF)
2953 ice_for_each_q_vector(vsi, i)
2954 synchronize_irq(vsi->q_vectors[i]->irq.virq);
2958 * ice_napi_del - Remove NAPI handler for the VSI
2959 * @vsi: VSI for which NAPI handler is to be removed
2961 void ice_napi_del(struct ice_vsi *vsi)
2968 ice_for_each_q_vector(vsi, v_idx)
2969 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2973 * ice_vsi_release - Delete a VSI and free its resources
2974 * @vsi: the VSI being removed
2976 * Returns 0 on success or < 0 on error
2978 int ice_vsi_release(struct ice_vsi *vsi)
2986 /* do not unregister while driver is in the reset recovery pending
2987 * state. Since reset/rebuild happens through PF service task workqueue,
2988 * it's not a good idea to unregister netdev that is associated to the
2989 * PF that is running the work queue items currently. This is done to
2990 * avoid check_flush_dependency() warning on this wq
2992 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2993 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2994 unregister_netdev(vsi->netdev);
2995 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2998 if (vsi->type == ICE_VSI_PF)
2999 ice_devlink_destroy_pf_port(pf);
3001 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3008 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
3009 unregister_netdev(vsi->netdev);
3010 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3012 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
3013 free_netdev(vsi->netdev);
3015 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3019 /* retain SW VSI data structure since it is needed to unregister and
3020 * free VSI netdev when PF is not in reset recovery pending state,\
3021 * for ex: during rmmod.
3023 if (!ice_is_reset_in_progress(pf->state))
3024 ice_vsi_delete(vsi);
3030 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
3031 * @vsi: VSI connected with q_vectors
3032 * @coalesce: array of struct with stored coalesce
3034 * Returns array size.
3037 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3038 struct ice_coalesce_stored *coalesce)
3042 ice_for_each_q_vector(vsi, i) {
3043 struct ice_q_vector *q_vector = vsi->q_vectors[i];
3045 coalesce[i].itr_tx = q_vector->tx.itr_settings;
3046 coalesce[i].itr_rx = q_vector->rx.itr_settings;
3047 coalesce[i].intrl = q_vector->intrl;
3049 if (i < vsi->num_txq)
3050 coalesce[i].tx_valid = true;
3051 if (i < vsi->num_rxq)
3052 coalesce[i].rx_valid = true;
3055 return vsi->num_q_vectors;
3059 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3060 * @vsi: VSI connected with q_vectors
3061 * @coalesce: pointer to array of struct with stored coalesce
3062 * @size: size of coalesce array
3064 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3065 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3069 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3070 struct ice_coalesce_stored *coalesce, int size)
3072 struct ice_ring_container *rc;
3075 if ((size && !coalesce) || !vsi)
3078 /* There are a couple of cases that have to be handled here:
3079 * 1. The case where the number of queue vectors stays the same, but
3080 * the number of Tx or Rx rings changes (the first for loop)
3081 * 2. The case where the number of queue vectors increased (the
3084 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3085 /* There are 2 cases to handle here and they are the same for
3087 * if the entry was valid previously (coalesce[i].[tr]x_valid
3088 * and the loop variable is less than the number of rings
3089 * allocated, then write the previous values
3091 * if the entry was not valid previously, but the number of
3092 * rings is less than are allocated (this means the number of
3093 * rings increased from previously), then write out the
3094 * values in the first element
3096 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3097 * as there is no harm because the dynamic algorithm
3098 * will just overwrite.
3100 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3101 rc = &vsi->q_vectors[i]->rx;
3102 rc->itr_settings = coalesce[i].itr_rx;
3103 ice_write_itr(rc, rc->itr_setting);
3104 } else if (i < vsi->alloc_rxq) {
3105 rc = &vsi->q_vectors[i]->rx;
3106 rc->itr_settings = coalesce[0].itr_rx;
3107 ice_write_itr(rc, rc->itr_setting);
3110 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3111 rc = &vsi->q_vectors[i]->tx;
3112 rc->itr_settings = coalesce[i].itr_tx;
3113 ice_write_itr(rc, rc->itr_setting);
3114 } else if (i < vsi->alloc_txq) {
3115 rc = &vsi->q_vectors[i]->tx;
3116 rc->itr_settings = coalesce[0].itr_tx;
3117 ice_write_itr(rc, rc->itr_setting);
3120 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3121 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3124 /* the number of queue vectors increased so write whatever is in
3127 for (; i < vsi->num_q_vectors; i++) {
3129 rc = &vsi->q_vectors[i]->tx;
3130 rc->itr_settings = coalesce[0].itr_tx;
3131 ice_write_itr(rc, rc->itr_setting);
3134 rc = &vsi->q_vectors[i]->rx;
3135 rc->itr_settings = coalesce[0].itr_rx;
3136 ice_write_itr(rc, rc->itr_setting);
3138 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3139 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3144 * ice_vsi_realloc_stat_arrays - Frees unused stat structures
3146 * @prev_txq: Number of Tx rings before ring reallocation
3147 * @prev_rxq: Number of Rx rings before ring reallocation
3150 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi, int prev_txq, int prev_rxq)
3152 struct ice_vsi_stats *vsi_stat;
3153 struct ice_pf *pf = vsi->back;
3156 if (!prev_txq || !prev_rxq)
3158 if (vsi->type == ICE_VSI_CHNL)
3161 vsi_stat = pf->vsi_stats[vsi->idx];
3163 if (vsi->num_txq < prev_txq) {
3164 for (i = vsi->num_txq; i < prev_txq; i++) {
3165 if (vsi_stat->tx_ring_stats[i]) {
3166 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
3167 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
3172 if (vsi->num_rxq < prev_rxq) {
3173 for (i = vsi->num_rxq; i < prev_rxq; i++) {
3174 if (vsi_stat->rx_ring_stats[i]) {
3175 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
3176 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3183 * ice_vsi_rebuild - Rebuild VSI after reset
3184 * @vsi: VSI to be rebuild
3185 * @vsi_flags: flags used for VSI rebuild flow
3187 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3188 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3190 * Returns 0 on success and negative value on failure
3192 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3194 struct ice_vsi_cfg_params params = {};
3195 struct ice_coalesce_stored *coalesce;
3196 int ret, prev_txq, prev_rxq;
3197 int prev_num_q_vectors = 0;
3203 params = ice_vsi_to_params(vsi);
3204 params.flags = vsi_flags;
3207 if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3210 coalesce = kcalloc(vsi->num_q_vectors,
3211 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3215 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3217 prev_txq = vsi->num_txq;
3218 prev_rxq = vsi->num_rxq;
3221 ret = ice_vsi_cfg_def(vsi, ¶ms);
3225 ret = ice_vsi_cfg_tc_lan(pf, vsi);
3227 if (vsi_flags & ICE_VSI_FLAG_INIT) {
3229 goto err_vsi_cfg_tc_lan;
3233 return ice_schedule_reset(pf, ICE_RESET_PFR);
3236 ice_vsi_realloc_stat_arrays(vsi, prev_txq, prev_rxq);
3238 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3251 * ice_is_reset_in_progress - check for a reset in progress
3252 * @state: PF state field
3254 bool ice_is_reset_in_progress(unsigned long *state)
3256 return test_bit(ICE_RESET_OICR_RECV, state) ||
3257 test_bit(ICE_PFR_REQ, state) ||
3258 test_bit(ICE_CORER_REQ, state) ||
3259 test_bit(ICE_GLOBR_REQ, state);
3263 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3264 * @pf: pointer to the PF structure
3265 * @timeout: length of time to wait, in jiffies
3267 * Wait (sleep) for a short time until the driver finishes cleaning up from
3268 * a device reset. The caller must be able to sleep. Use this to delay
3269 * operations that could fail while the driver is cleaning up after a device
3272 * Returns 0 on success, -EBUSY if the reset is not finished within the
3273 * timeout, and -ERESTARTSYS if the thread was interrupted.
3275 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3279 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3280 !ice_is_reset_in_progress(pf->state),
3291 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3292 * @vsi: VSI being configured
3293 * @ctx: the context buffer returned from AQ VSI update command
3295 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3297 vsi->info.mapping_flags = ctx->info.mapping_flags;
3298 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3299 sizeof(vsi->info.q_mapping));
3300 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3301 sizeof(vsi->info.tc_mapping));
3305 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3306 * @vsi: the VSI being configured
3307 * @ena_tc: TC map to be enabled
3309 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3311 struct net_device *netdev = vsi->netdev;
3312 struct ice_pf *pf = vsi->back;
3313 int numtc = vsi->tc_cfg.numtc;
3314 struct ice_dcbx_cfg *dcbcfg;
3321 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3322 if (vsi->type == ICE_VSI_CHNL)
3326 netdev_reset_tc(netdev);
3330 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3331 numtc = vsi->all_numtc;
3333 if (netdev_set_num_tc(netdev, numtc))
3336 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3338 ice_for_each_traffic_class(i)
3339 if (vsi->tc_cfg.ena_tc & BIT(i))
3340 netdev_set_tc_queue(netdev,
3341 vsi->tc_cfg.tc_info[i].netdev_tc,
3342 vsi->tc_cfg.tc_info[i].qcount_tx,
3343 vsi->tc_cfg.tc_info[i].qoffset);
3344 /* setup TC queue map for CHNL TCs */
3345 ice_for_each_chnl_tc(i) {
3346 if (!(vsi->all_enatc & BIT(i)))
3348 if (!vsi->mqprio_qopt.qopt.count[i])
3350 netdev_set_tc_queue(netdev, i,
3351 vsi->mqprio_qopt.qopt.count[i],
3352 vsi->mqprio_qopt.qopt.offset[i]);
3355 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3358 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3359 u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3361 /* Get the mapped netdev TC# for the UP */
3362 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3363 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3368 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3369 * @vsi: the VSI being configured,
3370 * @ctxt: VSI context structure
3371 * @ena_tc: number of traffic classes to enable
3373 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3376 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3379 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3380 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3381 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3382 u16 new_txq, new_rxq;
3386 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3388 pow = order_base_2(tc0_qcount);
3389 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3390 ICE_AQ_VSI_TC_Q_OFFSET_M) |
3391 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3393 ice_for_each_traffic_class(i) {
3394 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3395 /* TC is not enabled */
3396 vsi->tc_cfg.tc_info[i].qoffset = 0;
3397 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3398 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3399 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3400 ctxt->info.tc_mapping[i] = 0;
3404 offset = vsi->mqprio_qopt.qopt.offset[i];
3405 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3406 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3407 vsi->tc_cfg.tc_info[i].qoffset = offset;
3408 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3409 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3410 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3413 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3414 ice_for_each_chnl_tc(i) {
3415 if (!(vsi->all_enatc & BIT(i)))
3417 offset = vsi->mqprio_qopt.qopt.offset[i];
3418 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3419 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3423 new_txq = offset + qcount_tx;
3424 if (new_txq > vsi->alloc_txq) {
3425 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3426 new_txq, vsi->alloc_txq);
3430 new_rxq = offset + qcount_rx;
3431 if (new_rxq > vsi->alloc_rxq) {
3432 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3433 new_rxq, vsi->alloc_rxq);
3437 /* Set actual Tx/Rx queue pairs */
3438 vsi->num_txq = new_txq;
3439 vsi->num_rxq = new_rxq;
3441 /* Setup queue TC[0].qmap for given VSI context */
3442 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3443 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3444 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3446 /* Find queue count available for channel VSIs and starting offset
3449 if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3450 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3451 vsi->next_base_q = tc0_qcount;
3453 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq);
3454 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq);
3455 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3456 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3462 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3463 * @vsi: VSI to be configured
3464 * @ena_tc: TC bitmap
3466 * VSI queues expected to be quiesced before calling this function
3468 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3470 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3471 struct ice_pf *pf = vsi->back;
3472 struct ice_tc_cfg old_tc_cfg;
3473 struct ice_vsi_ctx *ctx;
3478 dev = ice_pf_to_dev(pf);
3479 if (vsi->tc_cfg.ena_tc == ena_tc &&
3480 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3483 ice_for_each_traffic_class(i) {
3484 /* build bitmap of enabled TCs */
3485 if (ena_tc & BIT(i))
3487 /* populate max_txqs per TC */
3488 max_txqs[i] = vsi->alloc_txq;
3489 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3490 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3492 if (vsi->type == ICE_VSI_CHNL &&
3493 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3494 max_txqs[i] = vsi->num_txq;
3497 memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3498 vsi->tc_cfg.ena_tc = ena_tc;
3499 vsi->tc_cfg.numtc = num_tc;
3501 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3506 ctx->info = vsi->info;
3508 if (vsi->type == ICE_VSI_PF &&
3509 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3510 ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3512 ret = ice_vsi_setup_q_map(vsi, ctx);
3515 memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3519 /* must to indicate which section of VSI context are being modified */
3520 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3521 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3523 dev_info(dev, "Failed VSI Update\n");
3527 if (vsi->type == ICE_VSI_PF &&
3528 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3529 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3531 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3532 vsi->tc_cfg.ena_tc, max_txqs);
3535 dev_err(dev, "VSI %d failed TC config, error %d\n",
3539 ice_vsi_update_q_map(vsi, ctx);
3540 vsi->info.valid_sections = 0;
3542 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3549 * ice_update_ring_stats - Update ring statistics
3550 * @stats: stats to be updated
3551 * @pkts: number of processed packets
3552 * @bytes: number of processed bytes
3554 * This function assumes that caller has acquired a u64_stats_sync lock.
3556 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3558 stats->bytes += bytes;
3559 stats->pkts += pkts;
3563 * ice_update_tx_ring_stats - Update Tx ring specific counters
3564 * @tx_ring: ring to update
3565 * @pkts: number of processed packets
3566 * @bytes: number of processed bytes
3568 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3570 u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3571 ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3572 u64_stats_update_end(&tx_ring->ring_stats->syncp);
3576 * ice_update_rx_ring_stats - Update Rx ring specific counters
3577 * @rx_ring: ring to update
3578 * @pkts: number of processed packets
3579 * @bytes: number of processed bytes
3581 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3583 u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3584 ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3585 u64_stats_update_end(&rx_ring->ring_stats->syncp);
3589 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3590 * @pi: port info of the switch with default VSI
3592 * Return true if the there is a single VSI in default forwarding VSI list
3594 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3596 bool exists = false;
3598 ice_check_if_dflt_vsi(pi, 0, &exists);
3603 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3604 * @vsi: VSI to compare against default forwarding VSI
3606 * If this VSI passed in is the default forwarding VSI then return true, else
3609 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3611 return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3615 * ice_set_dflt_vsi - set the default forwarding VSI
3616 * @vsi: VSI getting set as the default forwarding VSI on the switch
3618 * If the VSI passed in is already the default VSI and it's enabled just return
3621 * Otherwise try to set the VSI passed in as the switch's default VSI and
3622 * return the result.
3624 int ice_set_dflt_vsi(struct ice_vsi *vsi)
3632 dev = ice_pf_to_dev(vsi->back);
3634 /* the VSI passed in is already the default VSI */
3635 if (ice_is_vsi_dflt_vsi(vsi)) {
3636 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3641 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3643 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3644 vsi->vsi_num, status);
3652 * ice_clear_dflt_vsi - clear the default forwarding VSI
3653 * @vsi: VSI to remove from filter list
3655 * If the switch has no default VSI or it's not enabled then return error.
3657 * Otherwise try to clear the default VSI and return the result.
3659 int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3667 dev = ice_pf_to_dev(vsi->back);
3669 /* there is no default VSI configured */
3670 if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3673 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3676 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3677 vsi->vsi_num, status);
3685 * ice_get_link_speed_mbps - get link speed in Mbps
3686 * @vsi: the VSI whose link speed is being queried
3688 * Return current VSI link speed and 0 if the speed is unknown.
3690 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3692 unsigned int link_speed;
3694 link_speed = vsi->port_info->phy.link_info.link_speed;
3696 return (int)ice_get_link_speed(fls(link_speed) - 1);
3700 * ice_get_link_speed_kbps - get link speed in Kbps
3701 * @vsi: the VSI whose link speed is being queried
3703 * Return current VSI link speed and 0 if the speed is unknown.
3705 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3709 speed_mbps = ice_get_link_speed_mbps(vsi);
3711 return speed_mbps * 1000;
3715 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3716 * @vsi: VSI to be configured
3717 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3719 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3720 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3723 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3725 struct ice_pf *pf = vsi->back;
3730 dev = ice_pf_to_dev(pf);
3731 if (!vsi->port_info) {
3732 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3733 vsi->idx, vsi->type);
3737 speed = ice_get_link_speed_kbps(vsi);
3738 if (min_tx_rate > (u64)speed) {
3739 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3740 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3745 /* Configure min BW for VSI limit */
3747 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3748 ICE_MIN_BW, min_tx_rate);
3750 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3751 min_tx_rate, ice_vsi_type_str(vsi->type),
3756 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3757 min_tx_rate, ice_vsi_type_str(vsi->type));
3759 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3763 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3764 ice_vsi_type_str(vsi->type), vsi->idx);
3768 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3769 ice_vsi_type_str(vsi->type), vsi->idx);
3776 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3777 * @vsi: VSI to be configured
3778 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3780 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3781 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3784 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3786 struct ice_pf *pf = vsi->back;
3791 dev = ice_pf_to_dev(pf);
3792 if (!vsi->port_info) {
3793 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3794 vsi->idx, vsi->type);
3798 speed = ice_get_link_speed_kbps(vsi);
3799 if (max_tx_rate > (u64)speed) {
3800 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3801 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3806 /* Configure max BW for VSI limit */
3808 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3809 ICE_MAX_BW, max_tx_rate);
3811 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3812 max_tx_rate, ice_vsi_type_str(vsi->type),
3817 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3818 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3820 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3824 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3825 ice_vsi_type_str(vsi->type), vsi->idx);
3829 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3830 ice_vsi_type_str(vsi->type), vsi->idx);
3837 * ice_set_link - turn on/off physical link
3838 * @vsi: VSI to modify physical link on
3839 * @ena: turn on/off physical link
3841 int ice_set_link(struct ice_vsi *vsi, bool ena)
3843 struct device *dev = ice_pf_to_dev(vsi->back);
3844 struct ice_port_info *pi = vsi->port_info;
3845 struct ice_hw *hw = pi->hw;
3848 if (vsi->type != ICE_VSI_PF)
3851 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3853 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3854 * this is not a fatal error, so print a warning message and return
3855 * a success code. Return an error if FW returns an error code other
3856 * than ICE_AQ_RC_EMODE
3858 if (status == -EIO) {
3859 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3860 dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3861 (ena ? "ON" : "OFF"), status,
3862 ice_aq_str(hw->adminq.sq_last_status));
3863 } else if (status) {
3864 dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3865 (ena ? "ON" : "OFF"), status,
3866 ice_aq_str(hw->adminq.sq_last_status));
3874 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3875 * @vsi: VSI used to add VLAN filters
3877 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3878 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3879 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3880 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3882 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3883 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3884 * traffic in SVM, since the VLAN TPID isn't part of filtering.
3886 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3887 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3888 * part of filtering.
3890 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3892 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3893 struct ice_vlan vlan;
3896 vlan = ICE_VLAN(0, 0, 0);
3897 err = vlan_ops->add_vlan(vsi, &vlan);
3898 if (err && err != -EEXIST)
3901 /* in SVM both VLAN 0 filters are identical */
3902 if (!ice_is_dvm_ena(&vsi->back->hw))
3905 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3906 err = vlan_ops->add_vlan(vsi, &vlan);
3907 if (err && err != -EEXIST)
3914 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3915 * @vsi: VSI used to add VLAN filters
3917 * Delete the VLAN 0 filters in the same manner that they were added in
3918 * ice_vsi_add_vlan_zero.
3920 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3922 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3923 struct ice_vlan vlan;
3926 vlan = ICE_VLAN(0, 0, 0);
3927 err = vlan_ops->del_vlan(vsi, &vlan);
3928 if (err && err != -EEXIST)
3931 /* in SVM both VLAN 0 filters are identical */
3932 if (!ice_is_dvm_ena(&vsi->back->hw))
3935 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3936 err = vlan_ops->del_vlan(vsi, &vlan);
3937 if (err && err != -EEXIST)
3940 /* when deleting the last VLAN filter, make sure to disable the VLAN
3941 * promisc mode so the filter isn't left by accident
3943 return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3944 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3948 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3949 * @vsi: VSI used to get the VLAN mode
3951 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3952 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3954 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3956 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2
3957 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1
3958 /* no VLAN 0 filter is created when a port VLAN is active */
3959 if (vsi->type == ICE_VSI_VF) {
3960 if (WARN_ON(!vsi->vf))
3963 if (ice_vf_is_port_vlan_ena(vsi->vf))
3967 if (ice_is_dvm_ena(&vsi->back->hw))
3968 return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3970 return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3974 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3975 * @vsi: VSI used to determine if any non-zero VLANs have been added
3977 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3979 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3983 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3984 * @vsi: VSI used to get the number of non-zero VLANs added
3986 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3988 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3992 * ice_is_feature_supported
3993 * @pf: pointer to the struct ice_pf instance
3994 * @f: feature enum to be checked
3996 * returns true if feature is supported, false otherwise
3998 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
4000 if (f < 0 || f >= ICE_F_MAX)
4003 return test_bit(f, pf->features);
4007 * ice_set_feature_support
4008 * @pf: pointer to the struct ice_pf instance
4009 * @f: feature enum to set
4011 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4013 if (f < 0 || f >= ICE_F_MAX)
4016 set_bit(f, pf->features);
4020 * ice_clear_feature_support
4021 * @pf: pointer to the struct ice_pf instance
4022 * @f: feature enum to clear
4024 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4026 if (f < 0 || f >= ICE_F_MAX)
4029 clear_bit(f, pf->features);
4033 * ice_init_feature_support
4034 * @pf: pointer to the struct ice_pf instance
4036 * called during init to setup supported feature
4038 void ice_init_feature_support(struct ice_pf *pf)
4040 switch (pf->hw.device_id) {
4041 case ICE_DEV_ID_E810C_BACKPLANE:
4042 case ICE_DEV_ID_E810C_QSFP:
4043 case ICE_DEV_ID_E810C_SFP:
4044 ice_set_feature_support(pf, ICE_F_DSCP);
4045 ice_set_feature_support(pf, ICE_F_PTP_EXTTS);
4046 if (ice_is_e810t(&pf->hw)) {
4047 ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4048 if (ice_gnss_is_gps_present(&pf->hw))
4049 ice_set_feature_support(pf, ICE_F_GNSS);
4058 * ice_vsi_update_security - update security block in VSI
4059 * @vsi: pointer to VSI structure
4060 * @fill: function pointer to fill ctx
4063 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4065 struct ice_vsi_ctx ctx = { 0 };
4067 ctx.info = vsi->info;
4068 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4071 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4074 vsi->info = ctx.info;
4079 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4080 * @ctx: pointer to VSI ctx structure
4082 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4084 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4085 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4086 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4090 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4091 * @ctx: pointer to VSI ctx structure
4093 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4095 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4096 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4097 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4101 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4102 * @ctx: pointer to VSI ctx structure
4104 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4106 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4110 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4111 * @ctx: pointer to VSI ctx structure
4113 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4115 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;