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 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
298 if (vsi->type == ICE_VSI_VF)
299 ctxt->vf_num = vsi->vf->vf_id;
300 ctxt->vsi_num = vsi->vsi_num;
302 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
304 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
306 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
307 vsi->vsi_num, status);
313 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
314 * @vsi: pointer to VSI being cleared
316 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
318 struct ice_pf *pf = vsi->back;
321 dev = ice_pf_to_dev(pf);
323 if (vsi->af_xdp_zc_qps) {
324 bitmap_free(vsi->af_xdp_zc_qps);
325 vsi->af_xdp_zc_qps = NULL;
327 /* free the ring and vector containers */
328 if (vsi->q_vectors) {
329 devm_kfree(dev, vsi->q_vectors);
330 vsi->q_vectors = NULL;
333 devm_kfree(dev, vsi->tx_rings);
334 vsi->tx_rings = NULL;
337 devm_kfree(dev, vsi->rx_rings);
338 vsi->rx_rings = NULL;
341 devm_kfree(dev, vsi->txq_map);
345 devm_kfree(dev, vsi->rxq_map);
351 * ice_vsi_free_stats - Free the ring statistics structures
354 static void ice_vsi_free_stats(struct ice_vsi *vsi)
356 struct ice_vsi_stats *vsi_stat;
357 struct ice_pf *pf = vsi->back;
360 if (vsi->type == ICE_VSI_CHNL)
365 vsi_stat = pf->vsi_stats[vsi->idx];
369 ice_for_each_alloc_txq(vsi, i) {
370 if (vsi_stat->tx_ring_stats[i]) {
371 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
372 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
376 ice_for_each_alloc_rxq(vsi, i) {
377 if (vsi_stat->rx_ring_stats[i]) {
378 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
379 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
383 kfree(vsi_stat->tx_ring_stats);
384 kfree(vsi_stat->rx_ring_stats);
386 pf->vsi_stats[vsi->idx] = NULL;
390 * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
391 * @vsi: VSI which is having stats allocated
393 static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
395 struct ice_ring_stats **tx_ring_stats;
396 struct ice_ring_stats **rx_ring_stats;
397 struct ice_vsi_stats *vsi_stats;
398 struct ice_pf *pf = vsi->back;
401 vsi_stats = pf->vsi_stats[vsi->idx];
402 tx_ring_stats = vsi_stats->tx_ring_stats;
403 rx_ring_stats = vsi_stats->rx_ring_stats;
405 /* Allocate Tx ring stats */
406 ice_for_each_alloc_txq(vsi, i) {
407 struct ice_ring_stats *ring_stats;
408 struct ice_tx_ring *ring;
410 ring = vsi->tx_rings[i];
411 ring_stats = tx_ring_stats[i];
414 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
418 WRITE_ONCE(tx_ring_stats[i], ring_stats);
421 ring->ring_stats = ring_stats;
424 /* Allocate Rx ring stats */
425 ice_for_each_alloc_rxq(vsi, i) {
426 struct ice_ring_stats *ring_stats;
427 struct ice_rx_ring *ring;
429 ring = vsi->rx_rings[i];
430 ring_stats = rx_ring_stats[i];
433 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
437 WRITE_ONCE(rx_ring_stats[i], ring_stats);
440 ring->ring_stats = ring_stats;
446 ice_vsi_free_stats(vsi);
451 * ice_vsi_free - clean up and deallocate the provided VSI
452 * @vsi: pointer to VSI being cleared
454 * This deallocates the VSI's queue resources, removes it from the PF's
455 * VSI array if necessary, and deallocates the VSI
457 static void ice_vsi_free(struct ice_vsi *vsi)
459 struct ice_pf *pf = NULL;
462 if (!vsi || !vsi->back)
466 dev = ice_pf_to_dev(pf);
468 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
469 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
473 mutex_lock(&pf->sw_mutex);
474 /* updates the PF for this cleared VSI */
476 pf->vsi[vsi->idx] = NULL;
477 pf->next_vsi = vsi->idx;
479 ice_vsi_free_stats(vsi);
480 ice_vsi_free_arrays(vsi);
481 mutex_unlock(&pf->sw_mutex);
482 devm_kfree(dev, vsi);
485 void ice_vsi_delete(struct ice_vsi *vsi)
487 ice_vsi_delete_from_hw(vsi);
492 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
493 * @irq: interrupt number
494 * @data: pointer to a q_vector
496 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
498 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
500 if (!q_vector->tx.tx_ring)
503 #define FDIR_RX_DESC_CLEAN_BUDGET 64
504 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
505 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
511 * ice_msix_clean_rings - MSIX mode Interrupt Handler
512 * @irq: interrupt number
513 * @data: pointer to a q_vector
515 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
517 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
519 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
522 q_vector->total_events++;
524 napi_schedule(&q_vector->napi);
529 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
531 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
532 struct ice_pf *pf = q_vector->vsi->back;
536 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
540 ice_for_each_vf_rcu(pf, bkt, vf)
541 napi_schedule(&vf->repr->q_vector->napi);
548 * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
551 static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
553 struct ice_vsi_stats *vsi_stat;
554 struct ice_pf *pf = vsi->back;
556 if (vsi->type == ICE_VSI_CHNL)
561 if (pf->vsi_stats[vsi->idx])
562 /* realloc will happen in rebuild path */
565 vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
569 vsi_stat->tx_ring_stats =
570 kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
572 if (!vsi_stat->tx_ring_stats)
575 vsi_stat->rx_ring_stats =
576 kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
578 if (!vsi_stat->rx_ring_stats)
581 pf->vsi_stats[vsi->idx] = vsi_stat;
586 kfree(vsi_stat->rx_ring_stats);
588 kfree(vsi_stat->tx_ring_stats);
590 pf->vsi_stats[vsi->idx] = NULL;
595 * ice_vsi_alloc_def - set default values for already allocated VSI
597 * @ch: ptr to channel
600 ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
602 if (vsi->type != ICE_VSI_CHNL) {
603 ice_vsi_set_num_qs(vsi);
604 if (ice_vsi_alloc_arrays(vsi))
609 case ICE_VSI_SWITCHDEV_CTRL:
610 /* Setup eswitch MSIX irq handler for VSI */
611 vsi->irq_handler = ice_eswitch_msix_clean_rings;
614 /* Setup default MSIX irq handler for VSI */
615 vsi->irq_handler = ice_msix_clean_rings;
618 /* Setup ctrl VSI MSIX irq handler */
619 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
625 vsi->num_rxq = ch->num_rxq;
626 vsi->num_txq = ch->num_txq;
627 vsi->next_base_q = ch->base_q;
633 ice_vsi_free_arrays(vsi);
641 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
642 * @pf: board private structure
644 * Reserves a VSI index from the PF and allocates an empty VSI structure
645 * without a type. The VSI structure must later be initialized by calling
648 * returns a pointer to a VSI on success, NULL on failure.
650 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
652 struct device *dev = ice_pf_to_dev(pf);
653 struct ice_vsi *vsi = NULL;
655 /* Need to protect the allocation of the VSIs at the PF level */
656 mutex_lock(&pf->sw_mutex);
658 /* If we have already allocated our maximum number of VSIs,
659 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
660 * is available to be populated
662 if (pf->next_vsi == ICE_NO_VSI) {
663 dev_dbg(dev, "out of VSI slots!\n");
667 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
672 set_bit(ICE_VSI_DOWN, vsi->state);
674 /* fill slot and make note of the index */
675 vsi->idx = pf->next_vsi;
676 pf->vsi[pf->next_vsi] = vsi;
678 /* prepare pf->next_vsi for next use */
679 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
683 mutex_unlock(&pf->sw_mutex);
688 * ice_alloc_fd_res - Allocate FD resource for a VSI
689 * @vsi: pointer to the ice_vsi
691 * This allocates the FD resources
693 * Returns 0 on success, -EPERM on no-op or -EIO on failure
695 static int ice_alloc_fd_res(struct ice_vsi *vsi)
697 struct ice_pf *pf = vsi->back;
700 /* Flow Director filters are only allocated/assigned to the PF VSI or
701 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
702 * add/delete filters so resources are not allocated to it
704 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
707 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
708 vsi->type == ICE_VSI_CHNL))
711 /* FD filters from guaranteed pool per VSI */
712 g_val = pf->hw.func_caps.fd_fltr_guar;
716 /* FD filters from best effort pool */
717 b_val = pf->hw.func_caps.fd_fltr_best_effort;
721 /* PF main VSI gets only 64 FD resources from guaranteed pool
722 * when ADQ is configured.
724 #define ICE_PF_VSI_GFLTR 64
726 /* determine FD filter resources per VSI from shared(best effort) and
729 if (vsi->type == ICE_VSI_PF) {
730 vsi->num_gfltr = g_val;
731 /* if MQPRIO is configured, main VSI doesn't get all FD
732 * resources from guaranteed pool. PF VSI gets 64 FD resources
734 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
735 if (g_val < ICE_PF_VSI_GFLTR)
737 /* allow bare minimum entries for PF VSI */
738 vsi->num_gfltr = ICE_PF_VSI_GFLTR;
741 /* each VSI gets same "best_effort" quota */
742 vsi->num_bfltr = b_val;
743 } else if (vsi->type == ICE_VSI_VF) {
746 /* each VSI gets same "best_effort" quota */
747 vsi->num_bfltr = b_val;
749 struct ice_vsi *main_vsi;
752 main_vsi = ice_get_main_vsi(pf);
756 if (!main_vsi->all_numtc)
759 /* figure out ADQ numtc */
760 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
762 /* only one TC but still asking resources for channels,
765 if (numtc < ICE_CHNL_START_TC)
768 g_val -= ICE_PF_VSI_GFLTR;
769 /* channel VSIs gets equal share from guaranteed pool */
770 vsi->num_gfltr = g_val / numtc;
772 /* each VSI gets same "best_effort" quota */
773 vsi->num_bfltr = b_val;
780 * ice_vsi_get_qs - Assign queues from PF to VSI
781 * @vsi: the VSI to assign queues to
783 * Returns 0 on success and a negative value on error
785 static int ice_vsi_get_qs(struct ice_vsi *vsi)
787 struct ice_pf *pf = vsi->back;
788 struct ice_qs_cfg tx_qs_cfg = {
789 .qs_mutex = &pf->avail_q_mutex,
790 .pf_map = pf->avail_txqs,
791 .pf_map_size = pf->max_pf_txqs,
792 .q_count = vsi->alloc_txq,
793 .scatter_count = ICE_MAX_SCATTER_TXQS,
794 .vsi_map = vsi->txq_map,
796 .mapping_mode = ICE_VSI_MAP_CONTIG
798 struct ice_qs_cfg rx_qs_cfg = {
799 .qs_mutex = &pf->avail_q_mutex,
800 .pf_map = pf->avail_rxqs,
801 .pf_map_size = pf->max_pf_rxqs,
802 .q_count = vsi->alloc_rxq,
803 .scatter_count = ICE_MAX_SCATTER_RXQS,
804 .vsi_map = vsi->rxq_map,
806 .mapping_mode = ICE_VSI_MAP_CONTIG
810 if (vsi->type == ICE_VSI_CHNL)
813 ret = __ice_vsi_get_qs(&tx_qs_cfg);
816 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
818 ret = __ice_vsi_get_qs(&rx_qs_cfg);
821 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
827 * ice_vsi_put_qs - Release queues from VSI to PF
828 * @vsi: the VSI that is going to release queues
830 static void ice_vsi_put_qs(struct ice_vsi *vsi)
832 struct ice_pf *pf = vsi->back;
835 mutex_lock(&pf->avail_q_mutex);
837 ice_for_each_alloc_txq(vsi, i) {
838 clear_bit(vsi->txq_map[i], pf->avail_txqs);
839 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
842 ice_for_each_alloc_rxq(vsi, i) {
843 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
844 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
847 mutex_unlock(&pf->avail_q_mutex);
852 * @pf: pointer to the PF struct
854 * returns true if driver is in safe mode, false otherwise
856 bool ice_is_safe_mode(struct ice_pf *pf)
858 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
863 * @pf: pointer to the PF struct
865 * returns true if RDMA is currently supported, false otherwise
867 bool ice_is_rdma_ena(struct ice_pf *pf)
869 return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
873 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
874 * @vsi: the VSI being cleaned up
876 * This function deletes RSS input set for all flows that were configured
879 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
881 struct ice_pf *pf = vsi->back;
884 if (ice_is_safe_mode(pf))
887 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
889 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
890 vsi->vsi_num, status);
894 * ice_rss_clean - Delete RSS related VSI structures and configuration
895 * @vsi: the VSI being removed
897 static void ice_rss_clean(struct ice_vsi *vsi)
899 struct ice_pf *pf = vsi->back;
902 dev = ice_pf_to_dev(pf);
904 if (vsi->rss_hkey_user)
905 devm_kfree(dev, vsi->rss_hkey_user);
906 if (vsi->rss_lut_user)
907 devm_kfree(dev, vsi->rss_lut_user);
909 ice_vsi_clean_rss_flow_fld(vsi);
910 /* remove RSS replay list */
911 if (!ice_is_safe_mode(pf))
912 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
916 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
917 * @vsi: the VSI being configured
919 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
921 struct ice_hw_common_caps *cap;
922 struct ice_pf *pf = vsi->back;
924 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
929 cap = &pf->hw.func_caps.common_cap;
933 /* PF VSI will inherit RSS instance of PF */
934 vsi->rss_table_size = (u16)cap->rss_table_size;
935 if (vsi->type == ICE_VSI_CHNL)
936 vsi->rss_size = min_t(u16, vsi->num_rxq,
937 BIT(cap->rss_table_entry_width));
939 vsi->rss_size = min_t(u16, num_online_cpus(),
940 BIT(cap->rss_table_entry_width));
941 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
943 case ICE_VSI_SWITCHDEV_CTRL:
944 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
945 vsi->rss_size = min_t(u16, num_online_cpus(),
946 BIT(cap->rss_table_entry_width));
947 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
950 /* VF VSI will get a small RSS table.
951 * For VSI_LUT, LUT size should be set to 64 bytes.
953 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
954 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
955 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
960 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
961 ice_vsi_type_str(vsi->type));
967 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
968 * @hw: HW structure used to determine the VLAN mode of the device
969 * @ctxt: the VSI context being set
971 * This initializes a default VSI context for all sections except the Queues.
973 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
977 memset(&ctxt->info, 0, sizeof(ctxt->info));
978 /* VSI's should be allocated from shared pool */
979 ctxt->alloc_from_pool = true;
980 /* Src pruning enabled by default */
981 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
982 /* Traffic from VSI can be sent to LAN */
983 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
984 /* allow all untagged/tagged packets by default on Tx */
985 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
986 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
987 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
988 /* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
989 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
991 * DVM - leave inner VLAN in packet by default
993 if (ice_is_dvm_ena(hw)) {
994 ctxt->info.inner_vlan_flags |=
995 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
996 ctxt->info.outer_vlan_flags =
997 (ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
998 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
999 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M;
1000 ctxt->info.outer_vlan_flags |=
1001 (ICE_AQ_VSI_OUTER_TAG_VLAN_8100 <<
1002 ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
1003 ICE_AQ_VSI_OUTER_TAG_TYPE_M;
1004 ctxt->info.outer_vlan_flags |=
1005 FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
1006 ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
1008 /* Have 1:1 UP mapping for both ingress/egress tables */
1009 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1010 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1011 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1012 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1013 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1014 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1015 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1016 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1017 ctxt->info.ingress_table = cpu_to_le32(table);
1018 ctxt->info.egress_table = cpu_to_le32(table);
1019 /* Have 1:1 UP mapping for outer to inner UP table */
1020 ctxt->info.outer_up_table = cpu_to_le32(table);
1021 /* No Outer tag support outer_tag_flags remains to zero */
1025 * ice_vsi_setup_q_map - Setup a VSI queue map
1026 * @vsi: the VSI being configured
1027 * @ctxt: VSI context structure
1029 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1031 u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
1032 u16 num_txq_per_tc, num_rxq_per_tc;
1033 u16 qcount_tx = vsi->alloc_txq;
1034 u16 qcount_rx = vsi->alloc_rxq;
1038 if (!vsi->tc_cfg.numtc) {
1039 /* at least TC0 should be enabled by default */
1040 vsi->tc_cfg.numtc = 1;
1041 vsi->tc_cfg.ena_tc = 1;
1044 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
1045 if (!num_rxq_per_tc)
1047 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
1048 if (!num_txq_per_tc)
1051 /* find the (rounded up) power-of-2 of qcount */
1052 pow = (u16)order_base_2(num_rxq_per_tc);
1054 /* TC mapping is a function of the number of Rx queues assigned to the
1055 * VSI for each traffic class and the offset of these queues.
1056 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1057 * queues allocated to TC0. No:of queues is a power-of-2.
1059 * If TC is not enabled, the queue offset is set to 0, and allocate one
1060 * queue, this way, traffic for the given TC will be sent to the default
1063 * Setup number and offset of Rx queues for all TCs for the VSI
1065 ice_for_each_traffic_class(i) {
1066 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1067 /* TC is not enabled */
1068 vsi->tc_cfg.tc_info[i].qoffset = 0;
1069 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1070 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1071 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1072 ctxt->info.tc_mapping[i] = 0;
1077 vsi->tc_cfg.tc_info[i].qoffset = offset;
1078 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1079 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1080 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1082 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1083 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1084 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1085 ICE_AQ_VSI_TC_Q_NUM_M);
1086 offset += num_rxq_per_tc;
1087 tx_count += num_txq_per_tc;
1088 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1091 /* if offset is non-zero, means it is calculated correctly based on
1092 * enabled TCs for a given VSI otherwise qcount_rx will always
1093 * be correct and non-zero because it is based off - VSI's
1094 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1100 rx_count = num_rxq_per_tc;
1102 if (rx_count > vsi->alloc_rxq) {
1103 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1104 rx_count, vsi->alloc_rxq);
1108 if (tx_count > vsi->alloc_txq) {
1109 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1110 tx_count, vsi->alloc_txq);
1114 vsi->num_txq = tx_count;
1115 vsi->num_rxq = rx_count;
1117 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1118 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1119 /* since there is a chance that num_rxq could have been changed
1120 * in the above for loop, make num_txq equal to num_rxq.
1122 vsi->num_txq = vsi->num_rxq;
1125 /* Rx queue mapping */
1126 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1127 /* q_mapping buffer holds the info for the first queue allocated for
1128 * this VSI in the PF space and also the number of queues associated
1131 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1132 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1138 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1139 * @ctxt: the VSI context being set
1140 * @vsi: the VSI being configured
1142 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1144 u8 dflt_q_group, dflt_q_prio;
1145 u16 dflt_q, report_q, val;
1147 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1148 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1151 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1152 ctxt->info.valid_sections |= cpu_to_le16(val);
1158 /* enable flow director filtering/programming */
1159 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1160 ctxt->info.fd_options = cpu_to_le16(val);
1161 /* max of allocated flow director filters */
1162 ctxt->info.max_fd_fltr_dedicated =
1163 cpu_to_le16(vsi->num_gfltr);
1164 /* max of shared flow director filters any VSI may program */
1165 ctxt->info.max_fd_fltr_shared =
1166 cpu_to_le16(vsi->num_bfltr);
1167 /* default queue index within the VSI of the default FD */
1168 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
1169 ICE_AQ_VSI_FD_DEF_Q_M);
1170 /* target queue or queue group to the FD filter */
1171 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
1172 ICE_AQ_VSI_FD_DEF_GRP_M);
1173 ctxt->info.fd_def_q = cpu_to_le16(val);
1174 /* queue index on which FD filter completion is reported */
1175 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
1176 ICE_AQ_VSI_FD_REPORT_Q_M);
1177 /* priority of the default qindex action */
1178 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
1179 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
1180 ctxt->info.fd_report_opt = cpu_to_le16(val);
1184 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1185 * @ctxt: the VSI context being set
1186 * @vsi: the VSI being configured
1188 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1190 u8 lut_type, hash_type;
1195 dev = ice_pf_to_dev(pf);
1197 switch (vsi->type) {
1200 /* PF VSI will inherit RSS instance of PF */
1201 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1202 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1205 /* VF VSI will gets a small RSS table which is a VSI LUT type */
1206 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1207 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1210 dev_dbg(dev, "Unsupported VSI type %s\n",
1211 ice_vsi_type_str(vsi->type));
1215 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1216 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1217 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1218 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1222 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1224 struct ice_pf *pf = vsi->back;
1229 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1231 pow = order_base_2(qcount);
1232 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1233 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1234 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1235 ICE_AQ_VSI_TC_Q_NUM_M);
1237 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1238 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1239 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1240 ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1244 * ice_vsi_init - Create and initialize a VSI
1245 * @vsi: the VSI being configured
1246 * @vsi_flags: VSI configuration flags
1248 * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1249 * reconfigure an existing context.
1251 * This initializes a VSI context depending on the VSI type to be added and
1252 * passes it down to the add_vsi aq command to create a new VSI.
1254 static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1256 struct ice_pf *pf = vsi->back;
1257 struct ice_hw *hw = &pf->hw;
1258 struct ice_vsi_ctx *ctxt;
1262 dev = ice_pf_to_dev(pf);
1263 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1267 switch (vsi->type) {
1271 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1273 case ICE_VSI_SWITCHDEV_CTRL:
1275 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1278 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1279 /* VF number here is the absolute VF number (0-255) */
1280 ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1287 /* Handle VLAN pruning for channel VSI if main VSI has VLAN
1290 if (vsi->type == ICE_VSI_CHNL) {
1291 struct ice_vsi *main_vsi;
1293 main_vsi = ice_get_main_vsi(pf);
1294 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1295 ctxt->info.sw_flags2 |=
1296 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1298 ctxt->info.sw_flags2 &=
1299 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1302 ice_set_dflt_vsi_ctx(hw, ctxt);
1303 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1304 ice_set_fd_vsi_ctx(ctxt, vsi);
1305 /* if the switch is in VEB mode, allow VSI loopback */
1306 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1307 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1309 /* Set LUT type and HASH type if RSS is enabled */
1310 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1311 vsi->type != ICE_VSI_CTRL) {
1312 ice_set_rss_vsi_ctx(ctxt, vsi);
1313 /* if updating VSI context, make sure to set valid_section:
1314 * to indicate which section of VSI context being updated
1316 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1317 ctxt->info.valid_sections |=
1318 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1321 ctxt->info.sw_id = vsi->port_info->sw_id;
1322 if (vsi->type == ICE_VSI_CHNL) {
1323 ice_chnl_vsi_setup_q_map(vsi, ctxt);
1325 ret = ice_vsi_setup_q_map(vsi, ctxt);
1329 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1330 /* means VSI being updated */
1331 /* must to indicate which section of VSI context are
1334 ctxt->info.valid_sections |=
1335 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1338 /* Allow control frames out of main VSI */
1339 if (vsi->type == ICE_VSI_PF) {
1340 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1341 ctxt->info.valid_sections |=
1342 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1345 if (vsi_flags & ICE_VSI_FLAG_INIT) {
1346 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1348 dev_err(dev, "Add VSI failed, err %d\n", ret);
1353 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1355 dev_err(dev, "Update VSI failed, err %d\n", ret);
1361 /* keep context for update VSI operations */
1362 vsi->info = ctxt->info;
1364 /* record VSI number returned */
1365 vsi->vsi_num = ctxt->vsi_num;
1373 * ice_free_res - free a block of resources
1374 * @res: pointer to the resource
1375 * @index: starting index previously returned by ice_get_res
1376 * @id: identifier to track owner
1378 * Returns number of resources freed
1380 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1385 if (!res || index >= res->end)
1388 id |= ICE_RES_VALID_BIT;
1389 for (i = index; i < res->end && res->list[i] == id; i++) {
1398 * ice_search_res - Search the tracker for a block of resources
1399 * @res: pointer to the resource
1400 * @needed: size of the block needed
1401 * @id: identifier to track owner
1403 * Returns the base item index of the block, or -ENOMEM for error
1405 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1407 u16 start = 0, end = 0;
1409 if (needed > res->end)
1412 id |= ICE_RES_VALID_BIT;
1415 /* skip already allocated entries */
1416 if (res->list[end++] & ICE_RES_VALID_BIT) {
1418 if ((start + needed) > res->end)
1422 if (end == (start + needed)) {
1425 /* there was enough, so assign it to the requestor */
1427 res->list[i++] = id;
1431 } while (end < res->end);
1437 * ice_get_free_res_count - Get free count from a resource tracker
1438 * @res: Resource tracker instance
1440 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1444 for (i = 0; i < res->end; i++)
1445 if (!(res->list[i] & ICE_RES_VALID_BIT))
1452 * ice_get_res - get a block of resources
1453 * @pf: board private structure
1454 * @res: pointer to the resource
1455 * @needed: size of the block needed
1456 * @id: identifier to track owner
1458 * Returns the base item index of the block, or negative for error
1461 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1466 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1467 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1468 needed, res->num_entries, id);
1472 return ice_search_res(res, needed, id);
1476 * ice_get_vf_ctrl_res - Get VF control VSI resource
1477 * @pf: pointer to the PF structure
1478 * @vsi: the VSI to allocate a resource for
1480 * Look up whether another VF has already allocated the control VSI resource.
1481 * If so, re-use this resource so that we share it among all VFs.
1483 * Otherwise, allocate the resource and return it.
1485 static int ice_get_vf_ctrl_res(struct ice_pf *pf, struct ice_vsi *vsi)
1492 ice_for_each_vf_rcu(pf, bkt, vf) {
1493 if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1494 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1501 return ice_get_res(pf, pf->irq_tracker, vsi->num_q_vectors,
1502 ICE_RES_VF_CTRL_VEC_ID);
1506 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1507 * @vsi: ptr to the VSI
1509 * This should only be called after ice_vsi_alloc_def() which allocates the
1510 * corresponding SW VSI structure and initializes num_queue_pairs for the
1511 * newly allocated VSI.
1513 * Returns 0 on success or negative on failure
1515 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1517 struct ice_pf *pf = vsi->back;
1522 dev = ice_pf_to_dev(pf);
1523 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1524 if (vsi->type == ICE_VSI_VF)
1526 if (vsi->type == ICE_VSI_CHNL)
1529 if (vsi->base_vector) {
1530 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1531 vsi->vsi_num, vsi->base_vector);
1535 num_q_vectors = vsi->num_q_vectors;
1536 /* reserve slots from OS requested IRQs */
1537 if (vsi->type == ICE_VSI_CTRL && vsi->vf) {
1538 base = ice_get_vf_ctrl_res(pf, vsi);
1540 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1545 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1546 ice_get_free_res_count(pf->irq_tracker),
1547 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1550 vsi->base_vector = (u16)base;
1551 pf->num_avail_sw_msix -= num_q_vectors;
1557 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1558 * @vsi: the VSI having rings deallocated
1560 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1564 /* Avoid stale references by clearing map from vector to ring */
1565 if (vsi->q_vectors) {
1566 ice_for_each_q_vector(vsi, i) {
1567 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1570 q_vector->tx.tx_ring = NULL;
1571 q_vector->rx.rx_ring = NULL;
1576 if (vsi->tx_rings) {
1577 ice_for_each_alloc_txq(vsi, i) {
1578 if (vsi->tx_rings[i]) {
1579 kfree_rcu(vsi->tx_rings[i], rcu);
1580 WRITE_ONCE(vsi->tx_rings[i], NULL);
1584 if (vsi->rx_rings) {
1585 ice_for_each_alloc_rxq(vsi, i) {
1586 if (vsi->rx_rings[i]) {
1587 kfree_rcu(vsi->rx_rings[i], rcu);
1588 WRITE_ONCE(vsi->rx_rings[i], NULL);
1595 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1596 * @vsi: VSI which is having rings allocated
1598 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1600 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1601 struct ice_pf *pf = vsi->back;
1605 dev = ice_pf_to_dev(pf);
1606 /* Allocate Tx rings */
1607 ice_for_each_alloc_txq(vsi, i) {
1608 struct ice_tx_ring *ring;
1610 /* allocate with kzalloc(), free with kfree_rcu() */
1611 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1617 ring->reg_idx = vsi->txq_map[i];
1619 ring->tx_tstamps = &pf->ptp.port.tx;
1621 ring->count = vsi->num_tx_desc;
1622 ring->txq_teid = ICE_INVAL_TEID;
1624 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1626 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1627 WRITE_ONCE(vsi->tx_rings[i], ring);
1630 /* Allocate Rx rings */
1631 ice_for_each_alloc_rxq(vsi, i) {
1632 struct ice_rx_ring *ring;
1634 /* allocate with kzalloc(), free with kfree_rcu() */
1635 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1640 ring->reg_idx = vsi->rxq_map[i];
1642 ring->netdev = vsi->netdev;
1644 ring->count = vsi->num_rx_desc;
1645 ring->cached_phctime = pf->ptp.cached_phc_time;
1646 WRITE_ONCE(vsi->rx_rings[i], ring);
1652 ice_vsi_clear_rings(vsi);
1657 * ice_vsi_manage_rss_lut - disable/enable RSS
1658 * @vsi: the VSI being changed
1659 * @ena: boolean value indicating if this is an enable or disable request
1661 * In the event of disable request for RSS, this function will zero out RSS
1662 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1665 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1669 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1674 if (vsi->rss_lut_user)
1675 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1677 ice_fill_rss_lut(lut, vsi->rss_table_size,
1681 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1686 * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1687 * @vsi: VSI to be configured
1688 * @disable: set to true to have FCS / CRC in the frame data
1690 void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1694 ice_for_each_rxq(vsi, i)
1696 vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1698 vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1702 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1703 * @vsi: VSI to be configured
1705 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1707 struct ice_pf *pf = vsi->back;
1712 dev = ice_pf_to_dev(pf);
1713 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1714 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1715 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1717 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1719 /* If orig_rss_size is valid and it is less than determined
1720 * main VSI's rss_size, update main VSI's rss_size to be
1721 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1722 * RSS table gets programmed to be correct (whatever it was
1723 * to begin with (prior to setup-tc for ADQ config)
1725 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1726 vsi->orig_rss_size <= vsi->num_rxq) {
1727 vsi->rss_size = vsi->orig_rss_size;
1728 /* now orig_rss_size is used, reset it to zero */
1729 vsi->orig_rss_size = 0;
1733 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1737 if (vsi->rss_lut_user)
1738 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1740 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1742 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1744 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1745 goto ice_vsi_cfg_rss_exit;
1748 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1751 goto ice_vsi_cfg_rss_exit;
1754 if (vsi->rss_hkey_user)
1755 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1757 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1759 err = ice_set_rss_key(vsi, key);
1761 dev_err(dev, "set_rss_key failed, error %d\n", err);
1764 ice_vsi_cfg_rss_exit:
1770 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1771 * @vsi: VSI to be configured
1773 * This function will only be called during the VF VSI setup. Upon successful
1774 * completion of package download, this function will configure default RSS
1775 * input sets for VF VSI.
1777 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1779 struct ice_pf *pf = vsi->back;
1783 dev = ice_pf_to_dev(pf);
1784 if (ice_is_safe_mode(pf)) {
1785 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1790 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1792 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1793 vsi->vsi_num, status);
1797 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1798 * @vsi: VSI to be configured
1800 * This function will only be called after successful download package call
1801 * during initialization of PF. Since the downloaded package will erase the
1802 * RSS section, this function will configure RSS input sets for different
1803 * flow types. The last profile added has the highest priority, therefore 2
1804 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1805 * (i.e. IPv4 src/dst TCP src/dst port).
1807 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1809 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1810 struct ice_pf *pf = vsi->back;
1811 struct ice_hw *hw = &pf->hw;
1815 dev = ice_pf_to_dev(pf);
1816 if (ice_is_safe_mode(pf)) {
1817 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1821 /* configure RSS for IPv4 with input set IP src/dst */
1822 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1823 ICE_FLOW_SEG_HDR_IPV4);
1825 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1828 /* configure RSS for IPv6 with input set IPv6 src/dst */
1829 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1830 ICE_FLOW_SEG_HDR_IPV6);
1832 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1835 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1836 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1837 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1839 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1842 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1843 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1844 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1846 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1849 /* configure RSS for sctp4 with input set IP src/dst */
1850 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1851 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1853 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1856 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1857 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1858 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1860 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1863 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1864 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1865 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1867 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1870 /* configure RSS for sctp6 with input set IPv6 src/dst */
1871 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1872 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1874 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1877 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_ESP_SPI,
1878 ICE_FLOW_SEG_HDR_ESP);
1880 dev_dbg(dev, "ice_add_rss_cfg failed for esp/spi flow, vsi = %d, error = %d\n",
1885 * ice_pf_state_is_nominal - checks the PF for nominal state
1886 * @pf: pointer to PF to check
1888 * Check the PF's state for a collection of bits that would indicate
1889 * the PF is in a state that would inhibit normal operation for
1890 * driver functionality.
1892 * Returns true if PF is in a nominal state, false otherwise
1894 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1896 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1901 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1902 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1909 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1910 * @vsi: the VSI to be updated
1912 void ice_update_eth_stats(struct ice_vsi *vsi)
1914 struct ice_eth_stats *prev_es, *cur_es;
1915 struct ice_hw *hw = &vsi->back->hw;
1916 struct ice_pf *pf = vsi->back;
1917 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1919 prev_es = &vsi->eth_stats_prev;
1920 cur_es = &vsi->eth_stats;
1922 if (ice_is_reset_in_progress(pf->state))
1923 vsi->stat_offsets_loaded = false;
1925 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1926 &prev_es->rx_bytes, &cur_es->rx_bytes);
1928 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1929 &prev_es->rx_unicast, &cur_es->rx_unicast);
1931 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1932 &prev_es->rx_multicast, &cur_es->rx_multicast);
1934 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1935 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1937 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1938 &prev_es->rx_discards, &cur_es->rx_discards);
1940 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1941 &prev_es->tx_bytes, &cur_es->tx_bytes);
1943 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1944 &prev_es->tx_unicast, &cur_es->tx_unicast);
1946 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1947 &prev_es->tx_multicast, &cur_es->tx_multicast);
1949 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1950 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1952 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1953 &prev_es->tx_errors, &cur_es->tx_errors);
1955 vsi->stat_offsets_loaded = true;
1959 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1962 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1964 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1965 vsi->max_frame = ICE_MAX_FRAME_LEGACY_RX;
1966 vsi->rx_buf_len = ICE_RXBUF_1664;
1967 #if (PAGE_SIZE < 8192)
1968 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1969 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1970 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1971 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1974 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1975 vsi->rx_buf_len = ICE_RXBUF_3072;
1980 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1982 * @pf_q: index of the Rx queue in the PF's queue space
1983 * @rxdid: flexible descriptor RXDID
1984 * @prio: priority for the RXDID for this queue
1985 * @ena_ts: true to enable timestamp and false to disable timestamp
1988 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1991 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1993 /* clear any previous values */
1994 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1995 QRXFLXP_CNTXT_RXDID_PRIO_M |
1996 QRXFLXP_CNTXT_TS_M);
1998 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1999 QRXFLXP_CNTXT_RXDID_IDX_M;
2001 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
2002 QRXFLXP_CNTXT_RXDID_PRIO_M;
2005 /* Enable TimeSync on this queue */
2006 regval |= QRXFLXP_CNTXT_TS_M;
2008 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
2011 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
2013 if (q_idx >= vsi->num_rxq)
2016 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
2019 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
2021 struct ice_aqc_add_tx_qgrp *qg_buf;
2024 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
2027 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
2031 qg_buf->num_txqs = 1;
2033 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
2039 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
2040 * @vsi: the VSI being configured
2042 * Return 0 on success and a negative value on error
2043 * Configure the Rx VSI for operation.
2045 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
2049 if (vsi->type == ICE_VSI_VF)
2052 ice_vsi_cfg_frame_size(vsi);
2054 /* set up individual rings */
2055 ice_for_each_rxq(vsi, i) {
2056 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
2066 * ice_vsi_cfg_txqs - Configure the VSI for Tx
2067 * @vsi: the VSI being configured
2068 * @rings: Tx ring array to be configured
2069 * @count: number of Tx ring array elements
2071 * Return 0 on success and a negative value on error
2072 * Configure the Tx VSI for operation.
2075 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
2077 struct ice_aqc_add_tx_qgrp *qg_buf;
2081 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
2085 qg_buf->num_txqs = 1;
2087 for (q_idx = 0; q_idx < count; q_idx++) {
2088 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
2099 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
2100 * @vsi: the VSI being configured
2102 * Return 0 on success and a negative value on error
2103 * Configure the Tx VSI for operation.
2105 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
2107 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
2111 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
2112 * @vsi: the VSI being configured
2114 * Return 0 on success and a negative value on error
2115 * Configure the Tx queues dedicated for XDP in given VSI for operation.
2117 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
2122 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
2126 ice_for_each_rxq(vsi, i)
2127 ice_tx_xsk_pool(vsi, i);
2133 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
2134 * @intrl: interrupt rate limit in usecs
2135 * @gran: interrupt rate limit granularity in usecs
2137 * This function converts a decimal interrupt rate limit in usecs to the format
2138 * expected by firmware.
2140 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
2142 u32 val = intrl / gran;
2145 return val | GLINT_RATE_INTRL_ENA_M;
2150 * ice_write_intrl - write throttle rate limit to interrupt specific register
2151 * @q_vector: pointer to interrupt specific structure
2152 * @intrl: throttle rate limit in microseconds to write
2154 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
2156 struct ice_hw *hw = &q_vector->vsi->back->hw;
2158 wr32(hw, GLINT_RATE(q_vector->reg_idx),
2159 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
2162 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
2165 case ICE_RX_CONTAINER:
2167 return rc->rx_ring->q_vector;
2169 case ICE_TX_CONTAINER:
2171 return rc->tx_ring->q_vector;
2181 * __ice_write_itr - write throttle rate to register
2182 * @q_vector: pointer to interrupt data structure
2183 * @rc: pointer to ring container
2184 * @itr: throttle rate in microseconds to write
2186 static void __ice_write_itr(struct ice_q_vector *q_vector,
2187 struct ice_ring_container *rc, u16 itr)
2189 struct ice_hw *hw = &q_vector->vsi->back->hw;
2191 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
2192 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
2196 * ice_write_itr - write throttle rate to queue specific register
2197 * @rc: pointer to ring container
2198 * @itr: throttle rate in microseconds to write
2200 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
2202 struct ice_q_vector *q_vector;
2204 q_vector = ice_pull_qvec_from_rc(rc);
2208 __ice_write_itr(q_vector, rc, itr);
2212 * ice_set_q_vector_intrl - set up interrupt rate limiting
2213 * @q_vector: the vector to be configured
2215 * Interrupt rate limiting is local to the vector, not per-queue so we must
2216 * detect if either ring container has dynamic moderation enabled to decide
2217 * what to set the interrupt rate limit to via INTRL settings. In the case that
2218 * dynamic moderation is disabled on both, write the value with the cached
2219 * setting to make sure INTRL register matches the user visible value.
2221 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2223 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2224 /* in the case of dynamic enabled, cap each vector to no more
2225 * than (4 us) 250,000 ints/sec, which allows low latency
2226 * but still less than 500,000 interrupts per second, which
2227 * reduces CPU a bit in the case of the lowest latency
2228 * setting. The 4 here is a value in microseconds.
2230 ice_write_intrl(q_vector, 4);
2232 ice_write_intrl(q_vector, q_vector->intrl);
2237 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2238 * @vsi: the VSI being configured
2240 * This configures MSIX mode interrupts for the PF VSI, and should not be used
2243 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2245 struct ice_pf *pf = vsi->back;
2246 struct ice_hw *hw = &pf->hw;
2247 u16 txq = 0, rxq = 0;
2250 ice_for_each_q_vector(vsi, i) {
2251 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2252 u16 reg_idx = q_vector->reg_idx;
2254 ice_cfg_itr(hw, q_vector);
2256 /* Both Transmit Queue Interrupt Cause Control register
2257 * and Receive Queue Interrupt Cause control register
2258 * expects MSIX_INDX field to be the vector index
2259 * within the function space and not the absolute
2260 * vector index across PF or across device.
2261 * For SR-IOV VF VSIs queue vector index always starts
2262 * with 1 since first vector index(0) is used for OICR
2263 * in VF space. Since VMDq and other PF VSIs are within
2264 * the PF function space, use the vector index that is
2265 * tracked for this PF.
2267 for (q = 0; q < q_vector->num_ring_tx; q++) {
2268 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2269 q_vector->tx.itr_idx);
2273 for (q = 0; q < q_vector->num_ring_rx; q++) {
2274 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2275 q_vector->rx.itr_idx);
2282 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2283 * @vsi: the VSI whose rings are to be enabled
2285 * Returns 0 on success and a negative value on error
2287 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2289 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2293 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2294 * @vsi: the VSI whose rings are to be disabled
2296 * Returns 0 on success and a negative value on error
2298 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2300 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2304 * ice_vsi_stop_tx_rings - Disable Tx rings
2305 * @vsi: the VSI being configured
2306 * @rst_src: reset source
2307 * @rel_vmvf_num: Relative ID of VF/VM
2308 * @rings: Tx ring array to be stopped
2309 * @count: number of Tx ring array elements
2312 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2313 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2317 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2320 for (q_idx = 0; q_idx < count; q_idx++) {
2321 struct ice_txq_meta txq_meta = { };
2324 if (!rings || !rings[q_idx])
2327 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2328 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2329 rings[q_idx], &txq_meta);
2339 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2340 * @vsi: the VSI being configured
2341 * @rst_src: reset source
2342 * @rel_vmvf_num: Relative ID of VF/VM
2345 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2348 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2352 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2353 * @vsi: the VSI being configured
2355 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2357 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2361 * ice_vsi_is_rx_queue_active
2362 * @vsi: the VSI being configured
2364 * Return true if at least one queue is active.
2366 bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
2368 struct ice_pf *pf = vsi->back;
2369 struct ice_hw *hw = &pf->hw;
2372 ice_for_each_rxq(vsi, i) {
2376 pf_q = vsi->rxq_map[i];
2377 rx_reg = rd32(hw, QRX_CTRL(pf_q));
2378 if (rx_reg & QRX_CTRL_QENA_STAT_M)
2386 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2387 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2389 * returns true if Rx VLAN pruning is enabled and false otherwise.
2391 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2396 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2399 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2401 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2402 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2403 vsi->tc_cfg.numtc = 1;
2407 /* set VSI TC information based on DCB config */
2408 ice_vsi_set_dcb_tc_cfg(vsi);
2412 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2413 * @vsi: VSI to set the q_vectors register index on
2416 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2420 if (!vsi || !vsi->q_vectors)
2423 ice_for_each_q_vector(vsi, i) {
2424 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2427 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2432 if (vsi->type == ICE_VSI_VF) {
2433 struct ice_vf *vf = vsi->vf;
2435 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2438 q_vector->v_idx + vsi->base_vector;
2445 ice_for_each_q_vector(vsi, i) {
2446 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2449 q_vector->reg_idx = 0;
2456 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2457 * @vsi: the VSI being configured
2458 * @tx: bool to determine Tx or Rx rule
2459 * @create: bool to determine create or remove Rule
2461 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2463 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2464 enum ice_sw_fwd_act_type act);
2465 struct ice_pf *pf = vsi->back;
2469 dev = ice_pf_to_dev(pf);
2470 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2473 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2476 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2477 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2480 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2486 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2487 create ? "adding" : "removing", tx ? "TX" : "RX",
2488 vsi->vsi_num, status);
2492 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2493 * @vsi: pointer to the VSI
2495 * This function will allocate new scheduler aggregator now if needed and will
2496 * move specified VSI into it.
2498 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2500 struct device *dev = ice_pf_to_dev(vsi->back);
2501 struct ice_agg_node *agg_node_iter = NULL;
2502 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2503 struct ice_agg_node *agg_node = NULL;
2504 int node_offset, max_agg_nodes = 0;
2505 struct ice_port_info *port_info;
2506 struct ice_pf *pf = vsi->back;
2507 u32 agg_node_id_start = 0;
2510 /* create (as needed) scheduler aggregator node and move VSI into
2511 * corresponding aggregator node
2512 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2513 * - VF aggregator nodes will contain VF VSI
2515 port_info = pf->hw.port_info;
2519 switch (vsi->type) {
2524 case ICE_VSI_SWITCHDEV_CTRL:
2525 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2526 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2527 agg_node_iter = &pf->pf_agg_node[0];
2530 /* user can create 'n' VFs on a given PF, but since max children
2531 * per aggregator node can be only 64. Following code handles
2532 * aggregator(s) for VF VSIs, either selects a agg_node which
2533 * was already created provided num_vsis < 64, otherwise
2534 * select next available node, which will be created
2536 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2537 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2538 agg_node_iter = &pf->vf_agg_node[0];
2541 /* other VSI type, handle later if needed */
2542 dev_dbg(dev, "unexpected VSI type %s\n",
2543 ice_vsi_type_str(vsi->type));
2547 /* find the appropriate aggregator node */
2548 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2549 /* see if we can find space in previously created
2550 * node if num_vsis < 64, otherwise skip
2552 if (agg_node_iter->num_vsis &&
2553 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2558 if (agg_node_iter->valid &&
2559 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2560 agg_id = agg_node_iter->agg_id;
2561 agg_node = agg_node_iter;
2565 /* find unclaimed agg_id */
2566 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2567 agg_id = node_offset + agg_node_id_start;
2568 agg_node = agg_node_iter;
2571 /* move to next agg_node */
2578 /* if selected aggregator node was not created, create it */
2579 if (!agg_node->valid) {
2580 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2581 (u8)vsi->tc_cfg.ena_tc);
2583 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2587 /* aggregator node is created, store the needed info */
2588 agg_node->valid = true;
2589 agg_node->agg_id = agg_id;
2592 /* move VSI to corresponding aggregator node */
2593 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2594 (u8)vsi->tc_cfg.ena_tc);
2596 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2601 /* keep active children count for aggregator node */
2602 agg_node->num_vsis++;
2604 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2605 * to aggregator node
2607 vsi->agg_node = agg_node;
2608 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2609 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2610 vsi->agg_node->num_vsis);
2614 * ice_free_vf_ctrl_res - Free the VF control VSI resource
2615 * @pf: pointer to PF structure
2616 * @vsi: the VSI to free resources for
2618 * Check if the VF control VSI resource is still in use. If no VF is using it
2619 * any more, release the VSI resource. Otherwise, leave it to be cleaned up
2620 * once no other VF uses it.
2622 static void ice_free_vf_ctrl_res(struct ice_pf *pf, struct ice_vsi *vsi)
2628 ice_for_each_vf_rcu(pf, bkt, vf) {
2629 if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
2636 /* No other VFs left that have control VSI. It is now safe to reclaim
2637 * SW interrupts back to the common pool.
2639 ice_free_res(pf->irq_tracker, vsi->base_vector,
2640 ICE_RES_VF_CTRL_VEC_ID);
2641 pf->num_avail_sw_msix += vsi->num_q_vectors;
2644 static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
2646 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2647 struct device *dev = ice_pf_to_dev(pf);
2650 /* configure VSI nodes based on number of queues and TC's */
2651 ice_for_each_traffic_class(i) {
2652 if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2655 if (vsi->type == ICE_VSI_CHNL) {
2656 if (!vsi->alloc_txq && vsi->num_txq)
2657 max_txqs[i] = vsi->num_txq;
2659 max_txqs[i] = pf->num_lan_tx;
2661 max_txqs[i] = vsi->alloc_txq;
2665 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2666 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2669 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2678 * ice_vsi_cfg_def - configure default VSI based on the type
2679 * @vsi: pointer to VSI
2680 * @params: the parameters to configure this VSI with
2683 ice_vsi_cfg_def(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2685 struct device *dev = ice_pf_to_dev(vsi->back);
2686 struct ice_pf *pf = vsi->back;
2689 vsi->vsw = pf->first_sw;
2691 ret = ice_vsi_alloc_def(vsi, params->ch);
2695 /* allocate memory for Tx/Rx ring stat pointers */
2696 ret = ice_vsi_alloc_stat_arrays(vsi);
2698 goto unroll_vsi_alloc;
2700 ice_alloc_fd_res(vsi);
2702 ret = ice_vsi_get_qs(vsi);
2704 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2706 goto unroll_vsi_alloc_stat;
2709 /* set RSS capabilities */
2710 ice_vsi_set_rss_params(vsi);
2712 /* set TC configuration */
2713 ice_vsi_set_tc_cfg(vsi);
2715 /* create the VSI */
2716 ret = ice_vsi_init(vsi, params->flags);
2720 ice_vsi_init_vlan_ops(vsi);
2722 switch (vsi->type) {
2724 case ICE_VSI_SWITCHDEV_CTRL:
2726 ret = ice_vsi_alloc_q_vectors(vsi);
2728 goto unroll_vsi_init;
2730 ret = ice_vsi_setup_vector_base(vsi);
2732 goto unroll_alloc_q_vector;
2734 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2736 goto unroll_vector_base;
2738 ret = ice_vsi_alloc_rings(vsi);
2740 goto unroll_vector_base;
2742 ret = ice_vsi_alloc_ring_stats(vsi);
2744 goto unroll_vector_base;
2746 ice_vsi_map_rings_to_vectors(vsi);
2747 if (ice_is_xdp_ena_vsi(vsi)) {
2748 ret = ice_vsi_determine_xdp_res(vsi);
2750 goto unroll_vector_base;
2751 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2753 goto unroll_vector_base;
2756 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2757 if (vsi->type != ICE_VSI_CTRL)
2758 /* Do not exit if configuring RSS had an issue, at
2759 * least receive traffic on first queue. Hence no
2760 * need to capture return value
2762 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2763 ice_vsi_cfg_rss_lut_key(vsi);
2764 ice_vsi_set_rss_flow_fld(vsi);
2769 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2770 ice_vsi_cfg_rss_lut_key(vsi);
2771 ice_vsi_set_rss_flow_fld(vsi);
2775 /* VF driver will take care of creating netdev for this type and
2776 * map queues to vectors through Virtchnl, PF driver only
2777 * creates a VSI and corresponding structures for bookkeeping
2780 ret = ice_vsi_alloc_q_vectors(vsi);
2782 goto unroll_vsi_init;
2784 ret = ice_vsi_alloc_rings(vsi);
2786 goto unroll_alloc_q_vector;
2788 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2790 goto unroll_vector_base;
2792 ret = ice_vsi_alloc_ring_stats(vsi);
2794 goto unroll_vector_base;
2795 /* Do not exit if configuring RSS had an issue, at least
2796 * receive traffic on first queue. Hence no need to capture
2799 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2800 ice_vsi_cfg_rss_lut_key(vsi);
2801 ice_vsi_set_vf_rss_flow_fld(vsi);
2805 ret = ice_vsi_alloc_rings(vsi);
2807 goto unroll_vsi_init;
2809 ret = ice_vsi_alloc_ring_stats(vsi);
2811 goto unroll_vector_base;
2815 /* clean up the resources and exit */
2817 goto unroll_vsi_init;
2823 /* reclaim SW interrupts back to the common pool */
2824 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2825 pf->num_avail_sw_msix += vsi->num_q_vectors;
2826 unroll_alloc_q_vector:
2827 ice_vsi_free_q_vectors(vsi);
2829 ice_vsi_delete_from_hw(vsi);
2831 ice_vsi_put_qs(vsi);
2832 unroll_vsi_alloc_stat:
2833 ice_vsi_free_stats(vsi);
2835 ice_vsi_free_arrays(vsi);
2840 * ice_vsi_cfg - configure a previously allocated VSI
2841 * @vsi: pointer to VSI
2842 * @params: parameters used to configure this VSI
2844 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2846 struct ice_pf *pf = vsi->back;
2849 if (WARN_ON(params->type == ICE_VSI_VF && !params->vf))
2852 vsi->type = params->type;
2853 vsi->port_info = params->pi;
2855 /* For VSIs which don't have a connected VF, this will be NULL */
2856 vsi->vf = params->vf;
2858 ret = ice_vsi_cfg_def(vsi, params);
2862 ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2866 if (vsi->type == ICE_VSI_CTRL) {
2868 WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2869 vsi->vf->ctrl_vsi_idx = vsi->idx;
2871 WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2872 pf->ctrl_vsi_idx = vsi->idx;
2880 * ice_vsi_decfg - remove all VSI configuration
2881 * @vsi: pointer to VSI
2883 void ice_vsi_decfg(struct ice_vsi *vsi)
2885 struct ice_pf *pf = vsi->back;
2888 /* The Rx rule will only exist to remove if the LLDP FW
2889 * engine is currently stopped
2891 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2892 !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2893 ice_cfg_sw_lldp(vsi, false, false);
2895 ice_fltr_remove_all(vsi);
2896 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2897 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2899 dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2902 if (ice_is_xdp_ena_vsi(vsi))
2903 /* return value check can be skipped here, it always returns
2904 * 0 if reset is in progress
2906 ice_destroy_xdp_rings(vsi);
2908 ice_vsi_clear_rings(vsi);
2909 ice_vsi_free_q_vectors(vsi);
2910 ice_vsi_put_qs(vsi);
2911 ice_vsi_free_arrays(vsi);
2913 /* SR-IOV determines needed MSIX resources all at once instead of per
2914 * VSI since when VFs are spawned we know how many VFs there are and how
2915 * many interrupts each VF needs. SR-IOV MSIX resources are also
2916 * cleared in the same manner.
2918 if (vsi->type == ICE_VSI_CTRL && vsi->vf) {
2919 ice_free_vf_ctrl_res(pf, vsi);
2920 } else if (vsi->type != ICE_VSI_VF) {
2921 /* reclaim SW interrupts back to the common pool */
2922 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2923 pf->num_avail_sw_msix += vsi->num_q_vectors;
2924 vsi->base_vector = 0;
2927 if (vsi->type == ICE_VSI_VF &&
2928 vsi->agg_node && vsi->agg_node->valid)
2929 vsi->agg_node->num_vsis--;
2930 if (vsi->agg_node) {
2931 vsi->agg_node->valid = false;
2932 vsi->agg_node->agg_id = 0;
2937 * ice_vsi_setup - Set up a VSI by a given type
2938 * @pf: board private structure
2939 * @params: parameters to use when creating the VSI
2941 * This allocates the sw VSI structure and its queue resources.
2943 * Returns pointer to the successfully allocated and configured VSI sw struct on
2944 * success, NULL on failure.
2947 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2949 struct device *dev = ice_pf_to_dev(pf);
2950 struct ice_vsi *vsi;
2953 /* ice_vsi_setup can only initialize a new VSI, and we must have
2954 * a port_info structure for it.
2956 if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2957 WARN_ON(!params->pi))
2960 vsi = ice_vsi_alloc(pf);
2962 dev_err(dev, "could not allocate VSI\n");
2966 ret = ice_vsi_cfg(vsi, params);
2970 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2971 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2972 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2973 * The rule is added once for PF VSI in order to create appropriate
2974 * recipe, since VSI/VSI list is ignored with drop action...
2975 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2976 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2977 * settings in the HW.
2979 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2980 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2982 ice_cfg_sw_lldp(vsi, true, true);
2986 ice_set_agg_vsi(vsi);
2991 if (params->type == ICE_VSI_VF)
2992 ice_enable_lag(pf->lag);
2999 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
3000 * @vsi: the VSI being cleaned up
3002 static void ice_vsi_release_msix(struct ice_vsi *vsi)
3004 struct ice_pf *pf = vsi->back;
3005 struct ice_hw *hw = &pf->hw;
3010 ice_for_each_q_vector(vsi, i) {
3011 struct ice_q_vector *q_vector = vsi->q_vectors[i];
3013 ice_write_intrl(q_vector, 0);
3014 for (q = 0; q < q_vector->num_ring_tx; q++) {
3015 ice_write_itr(&q_vector->tx, 0);
3016 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
3017 if (ice_is_xdp_ena_vsi(vsi)) {
3018 u32 xdp_txq = txq + vsi->num_xdp_txq;
3020 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
3025 for (q = 0; q < q_vector->num_ring_rx; q++) {
3026 ice_write_itr(&q_vector->rx, 0);
3027 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
3036 * ice_vsi_free_irq - Free the IRQ association with the OS
3037 * @vsi: the VSI being configured
3039 void ice_vsi_free_irq(struct ice_vsi *vsi)
3041 struct ice_pf *pf = vsi->back;
3042 int base = vsi->base_vector;
3045 if (!vsi->q_vectors || !vsi->irqs_ready)
3048 ice_vsi_release_msix(vsi);
3049 if (vsi->type == ICE_VSI_VF)
3052 vsi->irqs_ready = false;
3053 ice_free_cpu_rx_rmap(vsi);
3055 ice_for_each_q_vector(vsi, i) {
3056 u16 vector = i + base;
3059 irq_num = pf->msix_entries[vector].vector;
3061 /* free only the irqs that were actually requested */
3062 if (!vsi->q_vectors[i] ||
3063 !(vsi->q_vectors[i]->num_ring_tx ||
3064 vsi->q_vectors[i]->num_ring_rx))
3067 /* clear the affinity notifier in the IRQ descriptor */
3068 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
3069 irq_set_affinity_notifier(irq_num, NULL);
3071 /* clear the affinity_mask in the IRQ descriptor */
3072 irq_set_affinity_hint(irq_num, NULL);
3073 synchronize_irq(irq_num);
3074 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
3079 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
3080 * @vsi: the VSI having resources freed
3082 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
3089 ice_for_each_txq(vsi, i)
3090 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3091 ice_free_tx_ring(vsi->tx_rings[i]);
3095 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
3096 * @vsi: the VSI having resources freed
3098 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
3105 ice_for_each_rxq(vsi, i)
3106 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3107 ice_free_rx_ring(vsi->rx_rings[i]);
3111 * ice_vsi_close - Shut down a VSI
3112 * @vsi: the VSI being shut down
3114 void ice_vsi_close(struct ice_vsi *vsi)
3116 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
3119 ice_vsi_free_irq(vsi);
3120 ice_vsi_free_tx_rings(vsi);
3121 ice_vsi_free_rx_rings(vsi);
3125 * ice_ena_vsi - resume a VSI
3126 * @vsi: the VSI being resume
3127 * @locked: is the rtnl_lock already held
3129 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
3133 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
3136 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
3138 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
3139 if (netif_running(vsi->netdev)) {
3143 err = ice_open_internal(vsi->netdev);
3148 } else if (vsi->type == ICE_VSI_CTRL) {
3149 err = ice_vsi_open_ctrl(vsi);
3156 * ice_dis_vsi - pause a VSI
3157 * @vsi: the VSI being paused
3158 * @locked: is the rtnl_lock already held
3160 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
3162 if (test_bit(ICE_VSI_DOWN, vsi->state))
3165 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
3167 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
3168 if (netif_running(vsi->netdev)) {
3179 } else if (vsi->type == ICE_VSI_CTRL ||
3180 vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
3186 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
3187 * @vsi: the VSI being un-configured
3189 void ice_vsi_dis_irq(struct ice_vsi *vsi)
3191 int base = vsi->base_vector;
3192 struct ice_pf *pf = vsi->back;
3193 struct ice_hw *hw = &pf->hw;
3197 /* disable interrupt causation from each queue */
3198 if (vsi->tx_rings) {
3199 ice_for_each_txq(vsi, i) {
3200 if (vsi->tx_rings[i]) {
3203 reg = vsi->tx_rings[i]->reg_idx;
3204 val = rd32(hw, QINT_TQCTL(reg));
3205 val &= ~QINT_TQCTL_CAUSE_ENA_M;
3206 wr32(hw, QINT_TQCTL(reg), val);
3211 if (vsi->rx_rings) {
3212 ice_for_each_rxq(vsi, i) {
3213 if (vsi->rx_rings[i]) {
3216 reg = vsi->rx_rings[i]->reg_idx;
3217 val = rd32(hw, QINT_RQCTL(reg));
3218 val &= ~QINT_RQCTL_CAUSE_ENA_M;
3219 wr32(hw, QINT_RQCTL(reg), val);
3224 /* disable each interrupt */
3225 ice_for_each_q_vector(vsi, i) {
3226 if (!vsi->q_vectors[i])
3228 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
3233 /* don't call synchronize_irq() for VF's from the host */
3234 if (vsi->type == ICE_VSI_VF)
3237 ice_for_each_q_vector(vsi, i)
3238 synchronize_irq(pf->msix_entries[i + base].vector);
3242 * ice_napi_del - Remove NAPI handler for the VSI
3243 * @vsi: VSI for which NAPI handler is to be removed
3245 void ice_napi_del(struct ice_vsi *vsi)
3252 ice_for_each_q_vector(vsi, v_idx)
3253 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
3257 * ice_vsi_release - Delete a VSI and free its resources
3258 * @vsi: the VSI being removed
3260 * Returns 0 on success or < 0 on error
3262 int ice_vsi_release(struct ice_vsi *vsi)
3270 /* do not unregister while driver is in the reset recovery pending
3271 * state. Since reset/rebuild happens through PF service task workqueue,
3272 * it's not a good idea to unregister netdev that is associated to the
3273 * PF that is running the work queue items currently. This is done to
3274 * avoid check_flush_dependency() warning on this wq
3276 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
3277 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
3278 unregister_netdev(vsi->netdev);
3279 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3282 if (vsi->type == ICE_VSI_PF)
3283 ice_devlink_destroy_pf_port(pf);
3285 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3292 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
3293 unregister_netdev(vsi->netdev);
3294 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
3296 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
3297 free_netdev(vsi->netdev);
3299 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3303 /* retain SW VSI data structure since it is needed to unregister and
3304 * free VSI netdev when PF is not in reset recovery pending state,\
3305 * for ex: during rmmod.
3307 if (!ice_is_reset_in_progress(pf->state))
3308 ice_vsi_delete(vsi);
3314 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
3315 * @vsi: VSI connected with q_vectors
3316 * @coalesce: array of struct with stored coalesce
3318 * Returns array size.
3321 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3322 struct ice_coalesce_stored *coalesce)
3326 ice_for_each_q_vector(vsi, i) {
3327 struct ice_q_vector *q_vector = vsi->q_vectors[i];
3329 coalesce[i].itr_tx = q_vector->tx.itr_settings;
3330 coalesce[i].itr_rx = q_vector->rx.itr_settings;
3331 coalesce[i].intrl = q_vector->intrl;
3333 if (i < vsi->num_txq)
3334 coalesce[i].tx_valid = true;
3335 if (i < vsi->num_rxq)
3336 coalesce[i].rx_valid = true;
3339 return vsi->num_q_vectors;
3343 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3344 * @vsi: VSI connected with q_vectors
3345 * @coalesce: pointer to array of struct with stored coalesce
3346 * @size: size of coalesce array
3348 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3349 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3353 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3354 struct ice_coalesce_stored *coalesce, int size)
3356 struct ice_ring_container *rc;
3359 if ((size && !coalesce) || !vsi)
3362 /* There are a couple of cases that have to be handled here:
3363 * 1. The case where the number of queue vectors stays the same, but
3364 * the number of Tx or Rx rings changes (the first for loop)
3365 * 2. The case where the number of queue vectors increased (the
3368 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3369 /* There are 2 cases to handle here and they are the same for
3371 * if the entry was valid previously (coalesce[i].[tr]x_valid
3372 * and the loop variable is less than the number of rings
3373 * allocated, then write the previous values
3375 * if the entry was not valid previously, but the number of
3376 * rings is less than are allocated (this means the number of
3377 * rings increased from previously), then write out the
3378 * values in the first element
3380 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3381 * as there is no harm because the dynamic algorithm
3382 * will just overwrite.
3384 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3385 rc = &vsi->q_vectors[i]->rx;
3386 rc->itr_settings = coalesce[i].itr_rx;
3387 ice_write_itr(rc, rc->itr_setting);
3388 } else if (i < vsi->alloc_rxq) {
3389 rc = &vsi->q_vectors[i]->rx;
3390 rc->itr_settings = coalesce[0].itr_rx;
3391 ice_write_itr(rc, rc->itr_setting);
3394 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3395 rc = &vsi->q_vectors[i]->tx;
3396 rc->itr_settings = coalesce[i].itr_tx;
3397 ice_write_itr(rc, rc->itr_setting);
3398 } else if (i < vsi->alloc_txq) {
3399 rc = &vsi->q_vectors[i]->tx;
3400 rc->itr_settings = coalesce[0].itr_tx;
3401 ice_write_itr(rc, rc->itr_setting);
3404 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3405 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3408 /* the number of queue vectors increased so write whatever is in
3411 for (; i < vsi->num_q_vectors; i++) {
3413 rc = &vsi->q_vectors[i]->tx;
3414 rc->itr_settings = coalesce[0].itr_tx;
3415 ice_write_itr(rc, rc->itr_setting);
3418 rc = &vsi->q_vectors[i]->rx;
3419 rc->itr_settings = coalesce[0].itr_rx;
3420 ice_write_itr(rc, rc->itr_setting);
3422 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3423 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3428 * ice_vsi_realloc_stat_arrays - Frees unused stat structures
3430 * @prev_txq: Number of Tx rings before ring reallocation
3431 * @prev_rxq: Number of Rx rings before ring reallocation
3434 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi, int prev_txq, int prev_rxq)
3436 struct ice_vsi_stats *vsi_stat;
3437 struct ice_pf *pf = vsi->back;
3440 if (!prev_txq || !prev_rxq)
3442 if (vsi->type == ICE_VSI_CHNL)
3445 vsi_stat = pf->vsi_stats[vsi->idx];
3447 if (vsi->num_txq < prev_txq) {
3448 for (i = vsi->num_txq; i < prev_txq; i++) {
3449 if (vsi_stat->tx_ring_stats[i]) {
3450 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
3451 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
3456 if (vsi->num_rxq < prev_rxq) {
3457 for (i = vsi->num_rxq; i < prev_rxq; i++) {
3458 if (vsi_stat->rx_ring_stats[i]) {
3459 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
3460 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3467 * ice_vsi_rebuild - Rebuild VSI after reset
3468 * @vsi: VSI to be rebuild
3469 * @vsi_flags: flags used for VSI rebuild flow
3471 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3472 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3474 * Returns 0 on success and negative value on failure
3476 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3478 struct ice_vsi_cfg_params params = {};
3479 struct ice_coalesce_stored *coalesce;
3480 int ret, prev_txq, prev_rxq;
3481 int prev_num_q_vectors = 0;
3487 params = ice_vsi_to_params(vsi);
3488 params.flags = vsi_flags;
3491 if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3494 coalesce = kcalloc(vsi->num_q_vectors,
3495 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3499 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3501 prev_txq = vsi->num_txq;
3502 prev_rxq = vsi->num_rxq;
3505 ret = ice_vsi_cfg_def(vsi, ¶ms);
3509 ret = ice_vsi_cfg_tc_lan(pf, vsi);
3511 if (vsi_flags & ICE_VSI_FLAG_INIT) {
3513 goto err_vsi_cfg_tc_lan;
3517 return ice_schedule_reset(pf, ICE_RESET_PFR);
3520 ice_vsi_realloc_stat_arrays(vsi, prev_txq, prev_rxq);
3522 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3535 * ice_is_reset_in_progress - check for a reset in progress
3536 * @state: PF state field
3538 bool ice_is_reset_in_progress(unsigned long *state)
3540 return test_bit(ICE_RESET_OICR_RECV, state) ||
3541 test_bit(ICE_PFR_REQ, state) ||
3542 test_bit(ICE_CORER_REQ, state) ||
3543 test_bit(ICE_GLOBR_REQ, state);
3547 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3548 * @pf: pointer to the PF structure
3549 * @timeout: length of time to wait, in jiffies
3551 * Wait (sleep) for a short time until the driver finishes cleaning up from
3552 * a device reset. The caller must be able to sleep. Use this to delay
3553 * operations that could fail while the driver is cleaning up after a device
3556 * Returns 0 on success, -EBUSY if the reset is not finished within the
3557 * timeout, and -ERESTARTSYS if the thread was interrupted.
3559 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3563 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3564 !ice_is_reset_in_progress(pf->state),
3575 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3576 * @vsi: VSI being configured
3577 * @ctx: the context buffer returned from AQ VSI update command
3579 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3581 vsi->info.mapping_flags = ctx->info.mapping_flags;
3582 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3583 sizeof(vsi->info.q_mapping));
3584 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3585 sizeof(vsi->info.tc_mapping));
3589 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3590 * @vsi: the VSI being configured
3591 * @ena_tc: TC map to be enabled
3593 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3595 struct net_device *netdev = vsi->netdev;
3596 struct ice_pf *pf = vsi->back;
3597 int numtc = vsi->tc_cfg.numtc;
3598 struct ice_dcbx_cfg *dcbcfg;
3605 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3606 if (vsi->type == ICE_VSI_CHNL)
3610 netdev_reset_tc(netdev);
3614 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3615 numtc = vsi->all_numtc;
3617 if (netdev_set_num_tc(netdev, numtc))
3620 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3622 ice_for_each_traffic_class(i)
3623 if (vsi->tc_cfg.ena_tc & BIT(i))
3624 netdev_set_tc_queue(netdev,
3625 vsi->tc_cfg.tc_info[i].netdev_tc,
3626 vsi->tc_cfg.tc_info[i].qcount_tx,
3627 vsi->tc_cfg.tc_info[i].qoffset);
3628 /* setup TC queue map for CHNL TCs */
3629 ice_for_each_chnl_tc(i) {
3630 if (!(vsi->all_enatc & BIT(i)))
3632 if (!vsi->mqprio_qopt.qopt.count[i])
3634 netdev_set_tc_queue(netdev, i,
3635 vsi->mqprio_qopt.qopt.count[i],
3636 vsi->mqprio_qopt.qopt.offset[i]);
3639 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3642 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3643 u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3645 /* Get the mapped netdev TC# for the UP */
3646 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3647 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3652 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3653 * @vsi: the VSI being configured,
3654 * @ctxt: VSI context structure
3655 * @ena_tc: number of traffic classes to enable
3657 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3660 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3663 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3664 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3665 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3666 u16 new_txq, new_rxq;
3670 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3672 pow = order_base_2(tc0_qcount);
3673 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3674 ICE_AQ_VSI_TC_Q_OFFSET_M) |
3675 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3677 ice_for_each_traffic_class(i) {
3678 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3679 /* TC is not enabled */
3680 vsi->tc_cfg.tc_info[i].qoffset = 0;
3681 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3682 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3683 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3684 ctxt->info.tc_mapping[i] = 0;
3688 offset = vsi->mqprio_qopt.qopt.offset[i];
3689 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3690 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3691 vsi->tc_cfg.tc_info[i].qoffset = offset;
3692 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3693 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3694 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3697 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3698 ice_for_each_chnl_tc(i) {
3699 if (!(vsi->all_enatc & BIT(i)))
3701 offset = vsi->mqprio_qopt.qopt.offset[i];
3702 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3703 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3707 new_txq = offset + qcount_tx;
3708 if (new_txq > vsi->alloc_txq) {
3709 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3710 new_txq, vsi->alloc_txq);
3714 new_rxq = offset + qcount_rx;
3715 if (new_rxq > vsi->alloc_rxq) {
3716 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3717 new_rxq, vsi->alloc_rxq);
3721 /* Set actual Tx/Rx queue pairs */
3722 vsi->num_txq = new_txq;
3723 vsi->num_rxq = new_rxq;
3725 /* Setup queue TC[0].qmap for given VSI context */
3726 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3727 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3728 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3730 /* Find queue count available for channel VSIs and starting offset
3733 if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3734 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3735 vsi->next_base_q = tc0_qcount;
3737 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq);
3738 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq);
3739 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3740 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3746 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3747 * @vsi: VSI to be configured
3748 * @ena_tc: TC bitmap
3750 * VSI queues expected to be quiesced before calling this function
3752 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3754 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3755 struct ice_pf *pf = vsi->back;
3756 struct ice_tc_cfg old_tc_cfg;
3757 struct ice_vsi_ctx *ctx;
3762 dev = ice_pf_to_dev(pf);
3763 if (vsi->tc_cfg.ena_tc == ena_tc &&
3764 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3767 ice_for_each_traffic_class(i) {
3768 /* build bitmap of enabled TCs */
3769 if (ena_tc & BIT(i))
3771 /* populate max_txqs per TC */
3772 max_txqs[i] = vsi->alloc_txq;
3773 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3774 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3776 if (vsi->type == ICE_VSI_CHNL &&
3777 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3778 max_txqs[i] = vsi->num_txq;
3781 memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3782 vsi->tc_cfg.ena_tc = ena_tc;
3783 vsi->tc_cfg.numtc = num_tc;
3785 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3790 ctx->info = vsi->info;
3792 if (vsi->type == ICE_VSI_PF &&
3793 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3794 ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3796 ret = ice_vsi_setup_q_map(vsi, ctx);
3799 memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3803 /* must to indicate which section of VSI context are being modified */
3804 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3805 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3807 dev_info(dev, "Failed VSI Update\n");
3811 if (vsi->type == ICE_VSI_PF &&
3812 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3813 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3815 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3816 vsi->tc_cfg.ena_tc, max_txqs);
3819 dev_err(dev, "VSI %d failed TC config, error %d\n",
3823 ice_vsi_update_q_map(vsi, ctx);
3824 vsi->info.valid_sections = 0;
3826 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3833 * ice_update_ring_stats - Update ring statistics
3834 * @stats: stats to be updated
3835 * @pkts: number of processed packets
3836 * @bytes: number of processed bytes
3838 * This function assumes that caller has acquired a u64_stats_sync lock.
3840 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3842 stats->bytes += bytes;
3843 stats->pkts += pkts;
3847 * ice_update_tx_ring_stats - Update Tx ring specific counters
3848 * @tx_ring: ring to update
3849 * @pkts: number of processed packets
3850 * @bytes: number of processed bytes
3852 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3854 u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3855 ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3856 u64_stats_update_end(&tx_ring->ring_stats->syncp);
3860 * ice_update_rx_ring_stats - Update Rx ring specific counters
3861 * @rx_ring: ring to update
3862 * @pkts: number of processed packets
3863 * @bytes: number of processed bytes
3865 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3867 u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3868 ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3869 u64_stats_update_end(&rx_ring->ring_stats->syncp);
3873 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3874 * @pi: port info of the switch with default VSI
3876 * Return true if the there is a single VSI in default forwarding VSI list
3878 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3880 bool exists = false;
3882 ice_check_if_dflt_vsi(pi, 0, &exists);
3887 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3888 * @vsi: VSI to compare against default forwarding VSI
3890 * If this VSI passed in is the default forwarding VSI then return true, else
3893 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3895 return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3899 * ice_set_dflt_vsi - set the default forwarding VSI
3900 * @vsi: VSI getting set as the default forwarding VSI on the switch
3902 * If the VSI passed in is already the default VSI and it's enabled just return
3905 * Otherwise try to set the VSI passed in as the switch's default VSI and
3906 * return the result.
3908 int ice_set_dflt_vsi(struct ice_vsi *vsi)
3916 dev = ice_pf_to_dev(vsi->back);
3918 /* the VSI passed in is already the default VSI */
3919 if (ice_is_vsi_dflt_vsi(vsi)) {
3920 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3925 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3927 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3928 vsi->vsi_num, status);
3936 * ice_clear_dflt_vsi - clear the default forwarding VSI
3937 * @vsi: VSI to remove from filter list
3939 * If the switch has no default VSI or it's not enabled then return error.
3941 * Otherwise try to clear the default VSI and return the result.
3943 int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3951 dev = ice_pf_to_dev(vsi->back);
3953 /* there is no default VSI configured */
3954 if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3957 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3960 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3961 vsi->vsi_num, status);
3969 * ice_get_link_speed_mbps - get link speed in Mbps
3970 * @vsi: the VSI whose link speed is being queried
3972 * Return current VSI link speed and 0 if the speed is unknown.
3974 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3976 unsigned int link_speed;
3978 link_speed = vsi->port_info->phy.link_info.link_speed;
3980 return (int)ice_get_link_speed(fls(link_speed) - 1);
3984 * ice_get_link_speed_kbps - get link speed in Kbps
3985 * @vsi: the VSI whose link speed is being queried
3987 * Return current VSI link speed and 0 if the speed is unknown.
3989 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3993 speed_mbps = ice_get_link_speed_mbps(vsi);
3995 return speed_mbps * 1000;
3999 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
4000 * @vsi: VSI to be configured
4001 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
4003 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
4004 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
4007 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
4009 struct ice_pf *pf = vsi->back;
4014 dev = ice_pf_to_dev(pf);
4015 if (!vsi->port_info) {
4016 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
4017 vsi->idx, vsi->type);
4021 speed = ice_get_link_speed_kbps(vsi);
4022 if (min_tx_rate > (u64)speed) {
4023 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
4024 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
4029 /* Configure min BW for VSI limit */
4031 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
4032 ICE_MIN_BW, min_tx_rate);
4034 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
4035 min_tx_rate, ice_vsi_type_str(vsi->type),
4040 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
4041 min_tx_rate, ice_vsi_type_str(vsi->type));
4043 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
4047 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
4048 ice_vsi_type_str(vsi->type), vsi->idx);
4052 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
4053 ice_vsi_type_str(vsi->type), vsi->idx);
4060 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
4061 * @vsi: VSI to be configured
4062 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
4064 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
4065 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
4068 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
4070 struct ice_pf *pf = vsi->back;
4075 dev = ice_pf_to_dev(pf);
4076 if (!vsi->port_info) {
4077 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
4078 vsi->idx, vsi->type);
4082 speed = ice_get_link_speed_kbps(vsi);
4083 if (max_tx_rate > (u64)speed) {
4084 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
4085 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
4090 /* Configure max BW for VSI limit */
4092 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
4093 ICE_MAX_BW, max_tx_rate);
4095 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
4096 max_tx_rate, ice_vsi_type_str(vsi->type),
4101 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
4102 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
4104 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
4108 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
4109 ice_vsi_type_str(vsi->type), vsi->idx);
4113 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
4114 ice_vsi_type_str(vsi->type), vsi->idx);
4121 * ice_set_link - turn on/off physical link
4122 * @vsi: VSI to modify physical link on
4123 * @ena: turn on/off physical link
4125 int ice_set_link(struct ice_vsi *vsi, bool ena)
4127 struct device *dev = ice_pf_to_dev(vsi->back);
4128 struct ice_port_info *pi = vsi->port_info;
4129 struct ice_hw *hw = pi->hw;
4132 if (vsi->type != ICE_VSI_PF)
4135 status = ice_aq_set_link_restart_an(pi, ena, NULL);
4137 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
4138 * this is not a fatal error, so print a warning message and return
4139 * a success code. Return an error if FW returns an error code other
4140 * than ICE_AQ_RC_EMODE
4142 if (status == -EIO) {
4143 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
4144 dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
4145 (ena ? "ON" : "OFF"), status,
4146 ice_aq_str(hw->adminq.sq_last_status));
4147 } else if (status) {
4148 dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
4149 (ena ? "ON" : "OFF"), status,
4150 ice_aq_str(hw->adminq.sq_last_status));
4158 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
4159 * @vsi: VSI used to add VLAN filters
4161 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
4162 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
4163 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
4164 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
4166 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
4167 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
4168 * traffic in SVM, since the VLAN TPID isn't part of filtering.
4170 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
4171 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
4172 * part of filtering.
4174 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
4176 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
4177 struct ice_vlan vlan;
4180 vlan = ICE_VLAN(0, 0, 0);
4181 err = vlan_ops->add_vlan(vsi, &vlan);
4182 if (err && err != -EEXIST)
4185 /* in SVM both VLAN 0 filters are identical */
4186 if (!ice_is_dvm_ena(&vsi->back->hw))
4189 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
4190 err = vlan_ops->add_vlan(vsi, &vlan);
4191 if (err && err != -EEXIST)
4198 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
4199 * @vsi: VSI used to add VLAN filters
4201 * Delete the VLAN 0 filters in the same manner that they were added in
4202 * ice_vsi_add_vlan_zero.
4204 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
4206 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
4207 struct ice_vlan vlan;
4210 vlan = ICE_VLAN(0, 0, 0);
4211 err = vlan_ops->del_vlan(vsi, &vlan);
4212 if (err && err != -EEXIST)
4215 /* in SVM both VLAN 0 filters are identical */
4216 if (!ice_is_dvm_ena(&vsi->back->hw))
4219 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
4220 err = vlan_ops->del_vlan(vsi, &vlan);
4221 if (err && err != -EEXIST)
4224 /* when deleting the last VLAN filter, make sure to disable the VLAN
4225 * promisc mode so the filter isn't left by accident
4227 return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
4228 ICE_MCAST_VLAN_PROMISC_BITS, 0);
4232 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
4233 * @vsi: VSI used to get the VLAN mode
4235 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
4236 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
4238 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
4240 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2
4241 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1
4242 /* no VLAN 0 filter is created when a port VLAN is active */
4243 if (vsi->type == ICE_VSI_VF) {
4244 if (WARN_ON(!vsi->vf))
4247 if (ice_vf_is_port_vlan_ena(vsi->vf))
4251 if (ice_is_dvm_ena(&vsi->back->hw))
4252 return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
4254 return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
4258 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
4259 * @vsi: VSI used to determine if any non-zero VLANs have been added
4261 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
4263 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
4267 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
4268 * @vsi: VSI used to get the number of non-zero VLANs added
4270 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
4272 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
4276 * ice_is_feature_supported
4277 * @pf: pointer to the struct ice_pf instance
4278 * @f: feature enum to be checked
4280 * returns true if feature is supported, false otherwise
4282 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
4284 if (f < 0 || f >= ICE_F_MAX)
4287 return test_bit(f, pf->features);
4291 * ice_set_feature_support
4292 * @pf: pointer to the struct ice_pf instance
4293 * @f: feature enum to set
4295 static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
4297 if (f < 0 || f >= ICE_F_MAX)
4300 set_bit(f, pf->features);
4304 * ice_clear_feature_support
4305 * @pf: pointer to the struct ice_pf instance
4306 * @f: feature enum to clear
4308 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
4310 if (f < 0 || f >= ICE_F_MAX)
4313 clear_bit(f, pf->features);
4317 * ice_init_feature_support
4318 * @pf: pointer to the struct ice_pf instance
4320 * called during init to setup supported feature
4322 void ice_init_feature_support(struct ice_pf *pf)
4324 switch (pf->hw.device_id) {
4325 case ICE_DEV_ID_E810C_BACKPLANE:
4326 case ICE_DEV_ID_E810C_QSFP:
4327 case ICE_DEV_ID_E810C_SFP:
4328 ice_set_feature_support(pf, ICE_F_DSCP);
4329 ice_set_feature_support(pf, ICE_F_PTP_EXTTS);
4330 if (ice_is_e810t(&pf->hw)) {
4331 ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4332 if (ice_gnss_is_gps_present(&pf->hw))
4333 ice_set_feature_support(pf, ICE_F_GNSS);
4342 * ice_vsi_update_security - update security block in VSI
4343 * @vsi: pointer to VSI structure
4344 * @fill: function pointer to fill ctx
4347 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4349 struct ice_vsi_ctx ctx = { 0 };
4351 ctx.info = vsi->info;
4352 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4355 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4358 vsi->info = ctx.info;
4363 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4364 * @ctx: pointer to VSI ctx structure
4366 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4368 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4369 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4370 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4374 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4375 * @ctx: pointer to VSI ctx structure
4377 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4379 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4380 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4381 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4385 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4386 * @ctx: pointer to VSI ctx structure
4388 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4390 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4394 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4395 * @ctx: pointer to VSI ctx structure
4397 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4399 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;