1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 #include "ice_dcb_lib.h"
10 #include "ice_devlink.h"
11 #include "ice_vsi_vlan_ops.h"
14 * ice_vsi_type_str - maps VSI type enum to string equivalents
15 * @vsi_type: VSI type enum
17 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
25 return "ICE_VSI_CTRL";
27 return "ICE_VSI_CHNL";
30 case ICE_VSI_SWITCHDEV_CTRL:
31 return "ICE_VSI_SWITCHDEV_CTRL";
38 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
39 * @vsi: the VSI being configured
40 * @ena: start or stop the Rx rings
42 * First enable/disable all of the Rx rings, flush any remaining writes, and
43 * then verify that they have all been enabled/disabled successfully. This will
44 * let all of the register writes complete when enabling/disabling the Rx rings
45 * before waiting for the change in hardware to complete.
47 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
52 ice_for_each_rxq(vsi, i)
53 ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
55 ice_flush(&vsi->back->hw);
57 ice_for_each_rxq(vsi, i) {
58 ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
67 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
70 * On error: returns error code (negative)
71 * On success: returns 0
73 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
75 struct ice_pf *pf = vsi->back;
78 dev = ice_pf_to_dev(pf);
79 if (vsi->type == ICE_VSI_CHNL)
82 /* allocate memory for both Tx and Rx ring pointers */
83 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
84 sizeof(*vsi->tx_rings), GFP_KERNEL);
88 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
89 sizeof(*vsi->rx_rings), GFP_KERNEL);
93 /* txq_map needs to have enough space to track both Tx (stack) rings
94 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
95 * so use num_possible_cpus() as we want to always provide XDP ring
96 * per CPU, regardless of queue count settings from user that might
97 * have come from ethtool's set_channels() callback;
99 vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
100 sizeof(*vsi->txq_map), GFP_KERNEL);
105 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
106 sizeof(*vsi->rxq_map), GFP_KERNEL);
110 /* There is no need to allocate q_vectors for a loopback VSI. */
111 if (vsi->type == ICE_VSI_LB)
114 /* allocate memory for q_vector pointers */
115 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
116 sizeof(*vsi->q_vectors), GFP_KERNEL);
120 vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
121 if (!vsi->af_xdp_zc_qps)
127 devm_kfree(dev, vsi->q_vectors);
129 devm_kfree(dev, vsi->rxq_map);
131 devm_kfree(dev, vsi->txq_map);
133 devm_kfree(dev, vsi->rx_rings);
135 devm_kfree(dev, vsi->tx_rings);
140 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
141 * @vsi: the VSI being configured
143 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
147 case ICE_VSI_SWITCHDEV_CTRL:
150 /* a user could change the values of num_[tr]x_desc using
151 * ethtool -G so we should keep those values instead of
152 * overwriting them with the defaults.
154 if (!vsi->num_rx_desc)
155 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
156 if (!vsi->num_tx_desc)
157 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
160 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
167 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
168 * @vsi: the VSI being configured
170 * Return 0 on success and a negative value on error
172 static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
174 enum ice_vsi_type vsi_type = vsi->type;
175 struct ice_pf *pf = vsi->back;
176 struct ice_vf *vf = vsi->vf;
178 if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
184 vsi->alloc_txq = vsi->req_txq;
185 vsi->num_txq = vsi->req_txq;
187 vsi->alloc_txq = min3(pf->num_lan_msix,
188 ice_get_avail_txq_count(pf),
189 (u16)num_online_cpus());
192 pf->num_lan_tx = vsi->alloc_txq;
194 /* only 1 Rx queue unless RSS is enabled */
195 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
199 vsi->alloc_rxq = vsi->req_rxq;
200 vsi->num_rxq = vsi->req_rxq;
202 vsi->alloc_rxq = min3(pf->num_lan_msix,
203 ice_get_avail_rxq_count(pf),
204 (u16)num_online_cpus());
208 pf->num_lan_rx = vsi->alloc_rxq;
210 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
211 max_t(int, vsi->alloc_rxq,
214 case ICE_VSI_SWITCHDEV_CTRL:
215 /* The number of queues for ctrl VSI is equal to number of VFs.
216 * Each ring is associated to the corresponding VF_PR netdev.
218 vsi->alloc_txq = ice_get_num_vfs(pf);
219 vsi->alloc_rxq = vsi->alloc_txq;
220 vsi->num_q_vectors = 1;
224 vf->num_vf_qs = vf->num_req_qs;
225 vsi->alloc_txq = vf->num_vf_qs;
226 vsi->alloc_rxq = vf->num_vf_qs;
227 /* pf->vfs.num_msix_per includes (VF miscellaneous vector +
228 * data queue interrupts). Since vsi->num_q_vectors is number
229 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
230 * original vector count
232 vsi->num_q_vectors = pf->vfs.num_msix_per - ICE_NONQ_VECS_VF;
237 vsi->num_q_vectors = 1;
248 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type);
252 ice_vsi_set_num_desc(vsi);
256 * ice_get_free_slot - get the next non-NULL location index in array
257 * @array: array to search
258 * @size: size of the array
259 * @curr: last known occupied index to be used as a search hint
261 * void * is being used to keep the functionality generic. This lets us use this
262 * function on any array of pointers.
264 static int ice_get_free_slot(void *array, int size, int curr)
266 int **tmp_array = (int **)array;
269 if (curr < (size - 1) && !tmp_array[curr + 1]) {
274 while ((i < size) && (tmp_array[i]))
285 * ice_vsi_delete_from_hw - delete a VSI from the switch
286 * @vsi: pointer to VSI being removed
288 static void ice_vsi_delete_from_hw(struct ice_vsi *vsi)
290 struct ice_pf *pf = vsi->back;
291 struct ice_vsi_ctx *ctxt;
294 ice_fltr_remove_all(vsi);
295 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
299 if (vsi->type == ICE_VSI_VF)
300 ctxt->vf_num = vsi->vf->vf_id;
301 ctxt->vsi_num = vsi->vsi_num;
303 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
305 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
307 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
308 vsi->vsi_num, status);
314 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
315 * @vsi: pointer to VSI being cleared
317 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
319 struct ice_pf *pf = vsi->back;
322 dev = ice_pf_to_dev(pf);
324 bitmap_free(vsi->af_xdp_zc_qps);
325 vsi->af_xdp_zc_qps = NULL;
326 /* free the ring and vector containers */
327 devm_kfree(dev, vsi->q_vectors);
328 vsi->q_vectors = NULL;
329 devm_kfree(dev, vsi->tx_rings);
330 vsi->tx_rings = NULL;
331 devm_kfree(dev, vsi->rx_rings);
332 vsi->rx_rings = NULL;
333 devm_kfree(dev, vsi->txq_map);
335 devm_kfree(dev, vsi->rxq_map);
340 * ice_vsi_free_stats - Free the ring statistics structures
343 static void ice_vsi_free_stats(struct ice_vsi *vsi)
345 struct ice_vsi_stats *vsi_stat;
346 struct ice_pf *pf = vsi->back;
349 if (vsi->type == ICE_VSI_CHNL)
354 vsi_stat = pf->vsi_stats[vsi->idx];
358 ice_for_each_alloc_txq(vsi, i) {
359 if (vsi_stat->tx_ring_stats[i]) {
360 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
361 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
365 ice_for_each_alloc_rxq(vsi, i) {
366 if (vsi_stat->rx_ring_stats[i]) {
367 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
368 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
372 kfree(vsi_stat->tx_ring_stats);
373 kfree(vsi_stat->rx_ring_stats);
375 pf->vsi_stats[vsi->idx] = NULL;
379 * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
380 * @vsi: VSI which is having stats allocated
382 static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
384 struct ice_ring_stats **tx_ring_stats;
385 struct ice_ring_stats **rx_ring_stats;
386 struct ice_vsi_stats *vsi_stats;
387 struct ice_pf *pf = vsi->back;
390 vsi_stats = pf->vsi_stats[vsi->idx];
391 tx_ring_stats = vsi_stats->tx_ring_stats;
392 rx_ring_stats = vsi_stats->rx_ring_stats;
394 /* Allocate Tx ring stats */
395 ice_for_each_alloc_txq(vsi, i) {
396 struct ice_ring_stats *ring_stats;
397 struct ice_tx_ring *ring;
399 ring = vsi->tx_rings[i];
400 ring_stats = tx_ring_stats[i];
403 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
407 WRITE_ONCE(tx_ring_stats[i], ring_stats);
410 ring->ring_stats = ring_stats;
413 /* Allocate Rx ring stats */
414 ice_for_each_alloc_rxq(vsi, i) {
415 struct ice_ring_stats *ring_stats;
416 struct ice_rx_ring *ring;
418 ring = vsi->rx_rings[i];
419 ring_stats = rx_ring_stats[i];
422 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
426 WRITE_ONCE(rx_ring_stats[i], ring_stats);
429 ring->ring_stats = ring_stats;
435 ice_vsi_free_stats(vsi);
440 * ice_vsi_free - clean up and deallocate the provided VSI
441 * @vsi: pointer to VSI being cleared
443 * This deallocates the VSI's queue resources, removes it from the PF's
444 * VSI array if necessary, and deallocates the VSI
446 static void ice_vsi_free(struct ice_vsi *vsi)
448 struct ice_pf *pf = NULL;
451 if (!vsi || !vsi->back)
455 dev = ice_pf_to_dev(pf);
457 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
458 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
462 mutex_lock(&pf->sw_mutex);
463 /* updates the PF for this cleared VSI */
465 pf->vsi[vsi->idx] = NULL;
466 pf->next_vsi = vsi->idx;
468 ice_vsi_free_stats(vsi);
469 ice_vsi_free_arrays(vsi);
470 mutex_unlock(&pf->sw_mutex);
471 devm_kfree(dev, vsi);
474 void ice_vsi_delete(struct ice_vsi *vsi)
476 ice_vsi_delete_from_hw(vsi);
481 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
482 * @irq: interrupt number
483 * @data: pointer to a q_vector
485 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
487 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
489 if (!q_vector->tx.tx_ring)
492 #define FDIR_RX_DESC_CLEAN_BUDGET 64
493 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
494 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
500 * ice_msix_clean_rings - MSIX mode Interrupt Handler
501 * @irq: interrupt number
502 * @data: pointer to a q_vector
504 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
506 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
508 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
511 q_vector->total_events++;
513 napi_schedule(&q_vector->napi);
518 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
520 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
521 struct ice_pf *pf = q_vector->vsi->back;
525 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
529 ice_for_each_vf_rcu(pf, bkt, vf)
530 napi_schedule(&vf->repr->q_vector->napi);
537 * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
540 static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
542 struct ice_vsi_stats *vsi_stat;
543 struct ice_pf *pf = vsi->back;
545 if (vsi->type == ICE_VSI_CHNL)
550 if (pf->vsi_stats[vsi->idx])
551 /* realloc will happen in rebuild path */
554 vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
558 vsi_stat->tx_ring_stats =
559 kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
561 if (!vsi_stat->tx_ring_stats)
564 vsi_stat->rx_ring_stats =
565 kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
567 if (!vsi_stat->rx_ring_stats)
570 pf->vsi_stats[vsi->idx] = vsi_stat;
575 kfree(vsi_stat->rx_ring_stats);
577 kfree(vsi_stat->tx_ring_stats);
579 pf->vsi_stats[vsi->idx] = NULL;
584 * ice_vsi_alloc_def - set default values for already allocated VSI
586 * @ch: ptr to channel
589 ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
591 if (vsi->type != ICE_VSI_CHNL) {
592 ice_vsi_set_num_qs(vsi);
593 if (ice_vsi_alloc_arrays(vsi))
598 case ICE_VSI_SWITCHDEV_CTRL:
599 /* Setup eswitch MSIX irq handler for VSI */
600 vsi->irq_handler = ice_eswitch_msix_clean_rings;
603 /* Setup default MSIX irq handler for VSI */
604 vsi->irq_handler = ice_msix_clean_rings;
607 /* Setup ctrl VSI MSIX irq handler */
608 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
614 vsi->num_rxq = ch->num_rxq;
615 vsi->num_txq = ch->num_txq;
616 vsi->next_base_q = ch->base_q;
622 ice_vsi_free_arrays(vsi);
630 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
631 * @pf: board private structure
633 * Reserves a VSI index from the PF and allocates an empty VSI structure
634 * without a type. The VSI structure must later be initialized by calling
637 * returns a pointer to a VSI on success, NULL on failure.
639 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
641 struct device *dev = ice_pf_to_dev(pf);
642 struct ice_vsi *vsi = NULL;
644 /* Need to protect the allocation of the VSIs at the PF level */
645 mutex_lock(&pf->sw_mutex);
647 /* If we have already allocated our maximum number of VSIs,
648 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
649 * is available to be populated
651 if (pf->next_vsi == ICE_NO_VSI) {
652 dev_dbg(dev, "out of VSI slots!\n");
656 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
661 set_bit(ICE_VSI_DOWN, vsi->state);
663 /* fill slot and make note of the index */
664 vsi->idx = pf->next_vsi;
665 pf->vsi[pf->next_vsi] = vsi;
667 /* prepare pf->next_vsi for next use */
668 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
672 mutex_unlock(&pf->sw_mutex);
677 * ice_alloc_fd_res - Allocate FD resource for a VSI
678 * @vsi: pointer to the ice_vsi
680 * This allocates the FD resources
682 * Returns 0 on success, -EPERM on no-op or -EIO on failure
684 static int ice_alloc_fd_res(struct ice_vsi *vsi)
686 struct ice_pf *pf = vsi->back;
689 /* Flow Director filters are only allocated/assigned to the PF VSI or
690 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
691 * add/delete filters so resources are not allocated to it
693 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
696 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
697 vsi->type == ICE_VSI_CHNL))
700 /* FD filters from guaranteed pool per VSI */
701 g_val = pf->hw.func_caps.fd_fltr_guar;
705 /* FD filters from best effort pool */
706 b_val = pf->hw.func_caps.fd_fltr_best_effort;
710 /* PF main VSI gets only 64 FD resources from guaranteed pool
711 * when ADQ is configured.
713 #define ICE_PF_VSI_GFLTR 64
715 /* determine FD filter resources per VSI from shared(best effort) and
718 if (vsi->type == ICE_VSI_PF) {
719 vsi->num_gfltr = g_val;
720 /* if MQPRIO is configured, main VSI doesn't get all FD
721 * resources from guaranteed pool. PF VSI gets 64 FD resources
723 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
724 if (g_val < ICE_PF_VSI_GFLTR)
726 /* allow bare minimum entries for PF VSI */
727 vsi->num_gfltr = ICE_PF_VSI_GFLTR;
730 /* each VSI gets same "best_effort" quota */
731 vsi->num_bfltr = b_val;
732 } else if (vsi->type == ICE_VSI_VF) {
735 /* each VSI gets same "best_effort" quota */
736 vsi->num_bfltr = b_val;
738 struct ice_vsi *main_vsi;
741 main_vsi = ice_get_main_vsi(pf);
745 if (!main_vsi->all_numtc)
748 /* figure out ADQ numtc */
749 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
751 /* only one TC but still asking resources for channels,
754 if (numtc < ICE_CHNL_START_TC)
757 g_val -= ICE_PF_VSI_GFLTR;
758 /* channel VSIs gets equal share from guaranteed pool */
759 vsi->num_gfltr = g_val / numtc;
761 /* each VSI gets same "best_effort" quota */
762 vsi->num_bfltr = b_val;
769 * ice_vsi_get_qs - Assign queues from PF to VSI
770 * @vsi: the VSI to assign queues to
772 * Returns 0 on success and a negative value on error
774 static int ice_vsi_get_qs(struct ice_vsi *vsi)
776 struct ice_pf *pf = vsi->back;
777 struct ice_qs_cfg tx_qs_cfg = {
778 .qs_mutex = &pf->avail_q_mutex,
779 .pf_map = pf->avail_txqs,
780 .pf_map_size = pf->max_pf_txqs,
781 .q_count = vsi->alloc_txq,
782 .scatter_count = ICE_MAX_SCATTER_TXQS,
783 .vsi_map = vsi->txq_map,
785 .mapping_mode = ICE_VSI_MAP_CONTIG
787 struct ice_qs_cfg rx_qs_cfg = {
788 .qs_mutex = &pf->avail_q_mutex,
789 .pf_map = pf->avail_rxqs,
790 .pf_map_size = pf->max_pf_rxqs,
791 .q_count = vsi->alloc_rxq,
792 .scatter_count = ICE_MAX_SCATTER_RXQS,
793 .vsi_map = vsi->rxq_map,
795 .mapping_mode = ICE_VSI_MAP_CONTIG
799 if (vsi->type == ICE_VSI_CHNL)
802 ret = __ice_vsi_get_qs(&tx_qs_cfg);
805 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
807 ret = __ice_vsi_get_qs(&rx_qs_cfg);
810 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
816 * ice_vsi_put_qs - Release queues from VSI to PF
817 * @vsi: the VSI that is going to release queues
819 static void ice_vsi_put_qs(struct ice_vsi *vsi)
821 struct ice_pf *pf = vsi->back;
824 mutex_lock(&pf->avail_q_mutex);
826 ice_for_each_alloc_txq(vsi, i) {
827 clear_bit(vsi->txq_map[i], pf->avail_txqs);
828 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
831 ice_for_each_alloc_rxq(vsi, i) {
832 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
833 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
836 mutex_unlock(&pf->avail_q_mutex);
841 * @pf: pointer to the PF struct
843 * returns true if driver is in safe mode, false otherwise
845 bool ice_is_safe_mode(struct ice_pf *pf)
847 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
852 * @pf: pointer to the PF struct
854 * returns true if RDMA is currently supported, false otherwise
856 bool ice_is_rdma_ena(struct ice_pf *pf)
858 return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
862 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
863 * @vsi: the VSI being cleaned up
865 * This function deletes RSS input set for all flows that were configured
868 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
870 struct ice_pf *pf = vsi->back;
873 if (ice_is_safe_mode(pf))
876 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
878 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
879 vsi->vsi_num, status);
883 * ice_rss_clean - Delete RSS related VSI structures and configuration
884 * @vsi: the VSI being removed
886 static void ice_rss_clean(struct ice_vsi *vsi)
888 struct ice_pf *pf = vsi->back;
891 dev = ice_pf_to_dev(pf);
893 devm_kfree(dev, vsi->rss_hkey_user);
894 devm_kfree(dev, vsi->rss_lut_user);
896 ice_vsi_clean_rss_flow_fld(vsi);
897 /* remove RSS replay list */
898 if (!ice_is_safe_mode(pf))
899 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
903 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
904 * @vsi: the VSI being configured
906 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
908 struct ice_hw_common_caps *cap;
909 struct ice_pf *pf = vsi->back;
911 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
916 cap = &pf->hw.func_caps.common_cap;
920 /* PF VSI will inherit RSS instance of PF */
921 vsi->rss_table_size = (u16)cap->rss_table_size;
922 if (vsi->type == ICE_VSI_CHNL)
923 vsi->rss_size = min_t(u16, vsi->num_rxq,
924 BIT(cap->rss_table_entry_width));
926 vsi->rss_size = min_t(u16, num_online_cpus(),
927 BIT(cap->rss_table_entry_width));
928 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
930 case ICE_VSI_SWITCHDEV_CTRL:
931 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
932 vsi->rss_size = min_t(u16, num_online_cpus(),
933 BIT(cap->rss_table_entry_width));
934 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
937 /* VF VSI will get a small RSS table.
938 * For VSI_LUT, LUT size should be set to 64 bytes.
940 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
941 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
942 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
947 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
948 ice_vsi_type_str(vsi->type));
954 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
955 * @hw: HW structure used to determine the VLAN mode of the device
956 * @ctxt: the VSI context being set
958 * This initializes a default VSI context for all sections except the Queues.
960 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
964 memset(&ctxt->info, 0, sizeof(ctxt->info));
965 /* VSI's should be allocated from shared pool */
966 ctxt->alloc_from_pool = true;
967 /* Src pruning enabled by default */
968 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
969 /* Traffic from VSI can be sent to LAN */
970 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
971 /* allow all untagged/tagged packets by default on Tx */
972 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL &
973 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >>
974 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S);
975 /* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
976 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
978 * DVM - leave inner VLAN in packet by default
980 if (ice_is_dvm_ena(hw)) {
981 ctxt->info.inner_vlan_flags |=
982 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
983 ctxt->info.outer_vlan_flags =
984 (ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
985 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
986 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M;
987 ctxt->info.outer_vlan_flags |=
988 (ICE_AQ_VSI_OUTER_TAG_VLAN_8100 <<
989 ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
990 ICE_AQ_VSI_OUTER_TAG_TYPE_M;
991 ctxt->info.outer_vlan_flags |=
992 FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
993 ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
995 /* Have 1:1 UP mapping for both ingress/egress tables */
996 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
997 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
998 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
999 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1000 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1001 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1002 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1003 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1004 ctxt->info.ingress_table = cpu_to_le32(table);
1005 ctxt->info.egress_table = cpu_to_le32(table);
1006 /* Have 1:1 UP mapping for outer to inner UP table */
1007 ctxt->info.outer_up_table = cpu_to_le32(table);
1008 /* No Outer tag support outer_tag_flags remains to zero */
1012 * ice_vsi_setup_q_map - Setup a VSI queue map
1013 * @vsi: the VSI being configured
1014 * @ctxt: VSI context structure
1016 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1018 u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
1019 u16 num_txq_per_tc, num_rxq_per_tc;
1020 u16 qcount_tx = vsi->alloc_txq;
1021 u16 qcount_rx = vsi->alloc_rxq;
1025 if (!vsi->tc_cfg.numtc) {
1026 /* at least TC0 should be enabled by default */
1027 vsi->tc_cfg.numtc = 1;
1028 vsi->tc_cfg.ena_tc = 1;
1031 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
1032 if (!num_rxq_per_tc)
1034 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
1035 if (!num_txq_per_tc)
1038 /* find the (rounded up) power-of-2 of qcount */
1039 pow = (u16)order_base_2(num_rxq_per_tc);
1041 /* TC mapping is a function of the number of Rx queues assigned to the
1042 * VSI for each traffic class and the offset of these queues.
1043 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1044 * queues allocated to TC0. No:of queues is a power-of-2.
1046 * If TC is not enabled, the queue offset is set to 0, and allocate one
1047 * queue, this way, traffic for the given TC will be sent to the default
1050 * Setup number and offset of Rx queues for all TCs for the VSI
1052 ice_for_each_traffic_class(i) {
1053 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1054 /* TC is not enabled */
1055 vsi->tc_cfg.tc_info[i].qoffset = 0;
1056 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1057 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1058 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1059 ctxt->info.tc_mapping[i] = 0;
1064 vsi->tc_cfg.tc_info[i].qoffset = offset;
1065 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1066 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1067 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1069 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1070 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1071 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1072 ICE_AQ_VSI_TC_Q_NUM_M);
1073 offset += num_rxq_per_tc;
1074 tx_count += num_txq_per_tc;
1075 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1078 /* if offset is non-zero, means it is calculated correctly based on
1079 * enabled TCs for a given VSI otherwise qcount_rx will always
1080 * be correct and non-zero because it is based off - VSI's
1081 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1087 rx_count = num_rxq_per_tc;
1089 if (rx_count > vsi->alloc_rxq) {
1090 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1091 rx_count, vsi->alloc_rxq);
1095 if (tx_count > vsi->alloc_txq) {
1096 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1097 tx_count, vsi->alloc_txq);
1101 vsi->num_txq = tx_count;
1102 vsi->num_rxq = rx_count;
1104 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1105 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1106 /* since there is a chance that num_rxq could have been changed
1107 * in the above for loop, make num_txq equal to num_rxq.
1109 vsi->num_txq = vsi->num_rxq;
1112 /* Rx queue mapping */
1113 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1114 /* q_mapping buffer holds the info for the first queue allocated for
1115 * this VSI in the PF space and also the number of queues associated
1118 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1119 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1125 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1126 * @ctxt: the VSI context being set
1127 * @vsi: the VSI being configured
1129 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1131 u8 dflt_q_group, dflt_q_prio;
1132 u16 dflt_q, report_q, val;
1134 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1135 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1138 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1139 ctxt->info.valid_sections |= cpu_to_le16(val);
1145 /* enable flow director filtering/programming */
1146 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1147 ctxt->info.fd_options = cpu_to_le16(val);
1148 /* max of allocated flow director filters */
1149 ctxt->info.max_fd_fltr_dedicated =
1150 cpu_to_le16(vsi->num_gfltr);
1151 /* max of shared flow director filters any VSI may program */
1152 ctxt->info.max_fd_fltr_shared =
1153 cpu_to_le16(vsi->num_bfltr);
1154 /* default queue index within the VSI of the default FD */
1155 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
1156 ICE_AQ_VSI_FD_DEF_Q_M);
1157 /* target queue or queue group to the FD filter */
1158 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
1159 ICE_AQ_VSI_FD_DEF_GRP_M);
1160 ctxt->info.fd_def_q = cpu_to_le16(val);
1161 /* queue index on which FD filter completion is reported */
1162 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
1163 ICE_AQ_VSI_FD_REPORT_Q_M);
1164 /* priority of the default qindex action */
1165 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
1166 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
1167 ctxt->info.fd_report_opt = cpu_to_le16(val);
1171 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1172 * @ctxt: the VSI context being set
1173 * @vsi: the VSI being configured
1175 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1177 u8 lut_type, hash_type;
1182 dev = ice_pf_to_dev(pf);
1184 switch (vsi->type) {
1187 /* PF VSI will inherit RSS instance of PF */
1188 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1189 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1192 /* VF VSI will gets a small RSS table which is a VSI LUT type */
1193 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
1194 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1197 dev_dbg(dev, "Unsupported VSI type %s\n",
1198 ice_vsi_type_str(vsi->type));
1202 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1203 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1204 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1205 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1209 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1211 struct ice_pf *pf = vsi->back;
1216 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1218 pow = order_base_2(qcount);
1219 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1220 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1221 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1222 ICE_AQ_VSI_TC_Q_NUM_M);
1224 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1225 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1226 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1227 ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1231 * ice_vsi_init - Create and initialize a VSI
1232 * @vsi: the VSI being configured
1233 * @vsi_flags: VSI configuration flags
1235 * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1236 * reconfigure an existing context.
1238 * This initializes a VSI context depending on the VSI type to be added and
1239 * passes it down to the add_vsi aq command to create a new VSI.
1241 static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1243 struct ice_pf *pf = vsi->back;
1244 struct ice_hw *hw = &pf->hw;
1245 struct ice_vsi_ctx *ctxt;
1249 dev = ice_pf_to_dev(pf);
1250 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1254 switch (vsi->type) {
1258 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1260 case ICE_VSI_SWITCHDEV_CTRL:
1262 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1265 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1266 /* VF number here is the absolute VF number (0-255) */
1267 ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1274 /* Handle VLAN pruning for channel VSI if main VSI has VLAN
1277 if (vsi->type == ICE_VSI_CHNL) {
1278 struct ice_vsi *main_vsi;
1280 main_vsi = ice_get_main_vsi(pf);
1281 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1282 ctxt->info.sw_flags2 |=
1283 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1285 ctxt->info.sw_flags2 &=
1286 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1289 ice_set_dflt_vsi_ctx(hw, ctxt);
1290 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1291 ice_set_fd_vsi_ctx(ctxt, vsi);
1292 /* if the switch is in VEB mode, allow VSI loopback */
1293 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1294 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1296 /* Set LUT type and HASH type if RSS is enabled */
1297 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1298 vsi->type != ICE_VSI_CTRL) {
1299 ice_set_rss_vsi_ctx(ctxt, vsi);
1300 /* if updating VSI context, make sure to set valid_section:
1301 * to indicate which section of VSI context being updated
1303 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1304 ctxt->info.valid_sections |=
1305 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1308 ctxt->info.sw_id = vsi->port_info->sw_id;
1309 if (vsi->type == ICE_VSI_CHNL) {
1310 ice_chnl_vsi_setup_q_map(vsi, ctxt);
1312 ret = ice_vsi_setup_q_map(vsi, ctxt);
1316 if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1317 /* means VSI being updated */
1318 /* must to indicate which section of VSI context are
1321 ctxt->info.valid_sections |=
1322 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1325 /* Allow control frames out of main VSI */
1326 if (vsi->type == ICE_VSI_PF) {
1327 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1328 ctxt->info.valid_sections |=
1329 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1332 if (vsi_flags & ICE_VSI_FLAG_INIT) {
1333 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1335 dev_err(dev, "Add VSI failed, err %d\n", ret);
1340 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1342 dev_err(dev, "Update VSI failed, err %d\n", ret);
1348 /* keep context for update VSI operations */
1349 vsi->info = ctxt->info;
1351 /* record VSI number returned */
1352 vsi->vsi_num = ctxt->vsi_num;
1360 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1361 * @vsi: the VSI having rings deallocated
1363 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1367 /* Avoid stale references by clearing map from vector to ring */
1368 if (vsi->q_vectors) {
1369 ice_for_each_q_vector(vsi, i) {
1370 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1373 q_vector->tx.tx_ring = NULL;
1374 q_vector->rx.rx_ring = NULL;
1379 if (vsi->tx_rings) {
1380 ice_for_each_alloc_txq(vsi, i) {
1381 if (vsi->tx_rings[i]) {
1382 kfree_rcu(vsi->tx_rings[i], rcu);
1383 WRITE_ONCE(vsi->tx_rings[i], NULL);
1387 if (vsi->rx_rings) {
1388 ice_for_each_alloc_rxq(vsi, i) {
1389 if (vsi->rx_rings[i]) {
1390 kfree_rcu(vsi->rx_rings[i], rcu);
1391 WRITE_ONCE(vsi->rx_rings[i], NULL);
1398 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1399 * @vsi: VSI which is having rings allocated
1401 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1403 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1404 struct ice_pf *pf = vsi->back;
1408 dev = ice_pf_to_dev(pf);
1409 /* Allocate Tx rings */
1410 ice_for_each_alloc_txq(vsi, i) {
1411 struct ice_tx_ring *ring;
1413 /* allocate with kzalloc(), free with kfree_rcu() */
1414 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1420 ring->reg_idx = vsi->txq_map[i];
1422 ring->tx_tstamps = &pf->ptp.port.tx;
1424 ring->count = vsi->num_tx_desc;
1425 ring->txq_teid = ICE_INVAL_TEID;
1427 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1429 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1430 WRITE_ONCE(vsi->tx_rings[i], ring);
1433 /* Allocate Rx rings */
1434 ice_for_each_alloc_rxq(vsi, i) {
1435 struct ice_rx_ring *ring;
1437 /* allocate with kzalloc(), free with kfree_rcu() */
1438 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1443 ring->reg_idx = vsi->rxq_map[i];
1445 ring->netdev = vsi->netdev;
1447 ring->count = vsi->num_rx_desc;
1448 ring->cached_phctime = pf->ptp.cached_phc_time;
1449 WRITE_ONCE(vsi->rx_rings[i], ring);
1455 ice_vsi_clear_rings(vsi);
1460 * ice_vsi_manage_rss_lut - disable/enable RSS
1461 * @vsi: the VSI being changed
1462 * @ena: boolean value indicating if this is an enable or disable request
1464 * In the event of disable request for RSS, this function will zero out RSS
1465 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1468 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1472 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1477 if (vsi->rss_lut_user)
1478 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1480 ice_fill_rss_lut(lut, vsi->rss_table_size,
1484 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1489 * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1490 * @vsi: VSI to be configured
1491 * @disable: set to true to have FCS / CRC in the frame data
1493 void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1497 ice_for_each_rxq(vsi, i)
1499 vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1501 vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1505 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1506 * @vsi: VSI to be configured
1508 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1510 struct ice_pf *pf = vsi->back;
1515 dev = ice_pf_to_dev(pf);
1516 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1517 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1518 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1520 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1522 /* If orig_rss_size is valid and it is less than determined
1523 * main VSI's rss_size, update main VSI's rss_size to be
1524 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1525 * RSS table gets programmed to be correct (whatever it was
1526 * to begin with (prior to setup-tc for ADQ config)
1528 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1529 vsi->orig_rss_size <= vsi->num_rxq) {
1530 vsi->rss_size = vsi->orig_rss_size;
1531 /* now orig_rss_size is used, reset it to zero */
1532 vsi->orig_rss_size = 0;
1536 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1540 if (vsi->rss_lut_user)
1541 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1543 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1545 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1547 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1548 goto ice_vsi_cfg_rss_exit;
1551 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1554 goto ice_vsi_cfg_rss_exit;
1557 if (vsi->rss_hkey_user)
1558 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1560 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1562 err = ice_set_rss_key(vsi, key);
1564 dev_err(dev, "set_rss_key failed, error %d\n", err);
1567 ice_vsi_cfg_rss_exit:
1573 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1574 * @vsi: VSI to be configured
1576 * This function will only be called during the VF VSI setup. Upon successful
1577 * completion of package download, this function will configure default RSS
1578 * input sets for VF VSI.
1580 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1582 struct ice_pf *pf = vsi->back;
1586 dev = ice_pf_to_dev(pf);
1587 if (ice_is_safe_mode(pf)) {
1588 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1593 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1595 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1596 vsi->vsi_num, status);
1600 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1601 * @vsi: VSI to be configured
1603 * This function will only be called after successful download package call
1604 * during initialization of PF. Since the downloaded package will erase the
1605 * RSS section, this function will configure RSS input sets for different
1606 * flow types. The last profile added has the highest priority, therefore 2
1607 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1608 * (i.e. IPv4 src/dst TCP src/dst port).
1610 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1612 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1613 struct ice_pf *pf = vsi->back;
1614 struct ice_hw *hw = &pf->hw;
1618 dev = ice_pf_to_dev(pf);
1619 if (ice_is_safe_mode(pf)) {
1620 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1624 /* configure RSS for IPv4 with input set IP src/dst */
1625 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1626 ICE_FLOW_SEG_HDR_IPV4);
1628 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1631 /* configure RSS for IPv6 with input set IPv6 src/dst */
1632 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1633 ICE_FLOW_SEG_HDR_IPV6);
1635 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1638 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1639 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1640 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1642 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1645 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1646 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1647 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1649 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1652 /* configure RSS for sctp4 with input set IP src/dst */
1653 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1654 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1656 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1659 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1660 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1661 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1663 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1666 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1667 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1668 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1670 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1673 /* configure RSS for sctp6 with input set IPv6 src/dst */
1674 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1675 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1677 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1680 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_ESP_SPI,
1681 ICE_FLOW_SEG_HDR_ESP);
1683 dev_dbg(dev, "ice_add_rss_cfg failed for esp/spi flow, vsi = %d, error = %d\n",
1688 * ice_pf_state_is_nominal - checks the PF for nominal state
1689 * @pf: pointer to PF to check
1691 * Check the PF's state for a collection of bits that would indicate
1692 * the PF is in a state that would inhibit normal operation for
1693 * driver functionality.
1695 * Returns true if PF is in a nominal state, false otherwise
1697 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1699 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1704 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1705 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1712 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1713 * @vsi: the VSI to be updated
1715 void ice_update_eth_stats(struct ice_vsi *vsi)
1717 struct ice_eth_stats *prev_es, *cur_es;
1718 struct ice_hw *hw = &vsi->back->hw;
1719 struct ice_pf *pf = vsi->back;
1720 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1722 prev_es = &vsi->eth_stats_prev;
1723 cur_es = &vsi->eth_stats;
1725 if (ice_is_reset_in_progress(pf->state))
1726 vsi->stat_offsets_loaded = false;
1728 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1729 &prev_es->rx_bytes, &cur_es->rx_bytes);
1731 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1732 &prev_es->rx_unicast, &cur_es->rx_unicast);
1734 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1735 &prev_es->rx_multicast, &cur_es->rx_multicast);
1737 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1738 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1740 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1741 &prev_es->rx_discards, &cur_es->rx_discards);
1743 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1744 &prev_es->tx_bytes, &cur_es->tx_bytes);
1746 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1747 &prev_es->tx_unicast, &cur_es->tx_unicast);
1749 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1750 &prev_es->tx_multicast, &cur_es->tx_multicast);
1752 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1753 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1755 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1756 &prev_es->tx_errors, &cur_es->tx_errors);
1758 vsi->stat_offsets_loaded = true;
1762 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1765 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1767 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1768 vsi->max_frame = ICE_MAX_FRAME_LEGACY_RX;
1769 vsi->rx_buf_len = ICE_RXBUF_1664;
1770 #if (PAGE_SIZE < 8192)
1771 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1772 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1773 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1774 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1777 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1778 vsi->rx_buf_len = ICE_RXBUF_3072;
1783 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1785 * @pf_q: index of the Rx queue in the PF's queue space
1786 * @rxdid: flexible descriptor RXDID
1787 * @prio: priority for the RXDID for this queue
1788 * @ena_ts: true to enable timestamp and false to disable timestamp
1791 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1794 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1796 /* clear any previous values */
1797 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1798 QRXFLXP_CNTXT_RXDID_PRIO_M |
1799 QRXFLXP_CNTXT_TS_M);
1801 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1802 QRXFLXP_CNTXT_RXDID_IDX_M;
1804 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1805 QRXFLXP_CNTXT_RXDID_PRIO_M;
1808 /* Enable TimeSync on this queue */
1809 regval |= QRXFLXP_CNTXT_TS_M;
1811 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1814 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1816 if (q_idx >= vsi->num_rxq)
1819 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1822 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1824 struct ice_aqc_add_tx_qgrp *qg_buf;
1827 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1830 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1834 qg_buf->num_txqs = 1;
1836 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1842 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1843 * @vsi: the VSI being configured
1845 * Return 0 on success and a negative value on error
1846 * Configure the Rx VSI for operation.
1848 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1852 if (vsi->type == ICE_VSI_VF)
1855 ice_vsi_cfg_frame_size(vsi);
1857 /* set up individual rings */
1858 ice_for_each_rxq(vsi, i) {
1859 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1869 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1870 * @vsi: the VSI being configured
1871 * @rings: Tx ring array to be configured
1872 * @count: number of Tx ring array elements
1874 * Return 0 on success and a negative value on error
1875 * Configure the Tx VSI for operation.
1878 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1880 struct ice_aqc_add_tx_qgrp *qg_buf;
1884 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1888 qg_buf->num_txqs = 1;
1890 for (q_idx = 0; q_idx < count; q_idx++) {
1891 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1902 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1903 * @vsi: the VSI being configured
1905 * Return 0 on success and a negative value on error
1906 * Configure the Tx VSI for operation.
1908 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1910 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1914 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1915 * @vsi: the VSI being configured
1917 * Return 0 on success and a negative value on error
1918 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1920 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1925 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1929 ice_for_each_rxq(vsi, i)
1930 ice_tx_xsk_pool(vsi, i);
1936 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1937 * @intrl: interrupt rate limit in usecs
1938 * @gran: interrupt rate limit granularity in usecs
1940 * This function converts a decimal interrupt rate limit in usecs to the format
1941 * expected by firmware.
1943 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1945 u32 val = intrl / gran;
1948 return val | GLINT_RATE_INTRL_ENA_M;
1953 * ice_write_intrl - write throttle rate limit to interrupt specific register
1954 * @q_vector: pointer to interrupt specific structure
1955 * @intrl: throttle rate limit in microseconds to write
1957 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1959 struct ice_hw *hw = &q_vector->vsi->back->hw;
1961 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1962 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1965 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1968 case ICE_RX_CONTAINER:
1970 return rc->rx_ring->q_vector;
1972 case ICE_TX_CONTAINER:
1974 return rc->tx_ring->q_vector;
1984 * __ice_write_itr - write throttle rate to register
1985 * @q_vector: pointer to interrupt data structure
1986 * @rc: pointer to ring container
1987 * @itr: throttle rate in microseconds to write
1989 static void __ice_write_itr(struct ice_q_vector *q_vector,
1990 struct ice_ring_container *rc, u16 itr)
1992 struct ice_hw *hw = &q_vector->vsi->back->hw;
1994 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1995 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1999 * ice_write_itr - write throttle rate to queue specific register
2000 * @rc: pointer to ring container
2001 * @itr: throttle rate in microseconds to write
2003 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
2005 struct ice_q_vector *q_vector;
2007 q_vector = ice_pull_qvec_from_rc(rc);
2011 __ice_write_itr(q_vector, rc, itr);
2015 * ice_set_q_vector_intrl - set up interrupt rate limiting
2016 * @q_vector: the vector to be configured
2018 * Interrupt rate limiting is local to the vector, not per-queue so we must
2019 * detect if either ring container has dynamic moderation enabled to decide
2020 * what to set the interrupt rate limit to via INTRL settings. In the case that
2021 * dynamic moderation is disabled on both, write the value with the cached
2022 * setting to make sure INTRL register matches the user visible value.
2024 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
2026 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
2027 /* in the case of dynamic enabled, cap each vector to no more
2028 * than (4 us) 250,000 ints/sec, which allows low latency
2029 * but still less than 500,000 interrupts per second, which
2030 * reduces CPU a bit in the case of the lowest latency
2031 * setting. The 4 here is a value in microseconds.
2033 ice_write_intrl(q_vector, 4);
2035 ice_write_intrl(q_vector, q_vector->intrl);
2040 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
2041 * @vsi: the VSI being configured
2043 * This configures MSIX mode interrupts for the PF VSI, and should not be used
2046 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
2048 struct ice_pf *pf = vsi->back;
2049 struct ice_hw *hw = &pf->hw;
2050 u16 txq = 0, rxq = 0;
2053 ice_for_each_q_vector(vsi, i) {
2054 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2055 u16 reg_idx = q_vector->reg_idx;
2057 ice_cfg_itr(hw, q_vector);
2059 /* Both Transmit Queue Interrupt Cause Control register
2060 * and Receive Queue Interrupt Cause control register
2061 * expects MSIX_INDX field to be the vector index
2062 * within the function space and not the absolute
2063 * vector index across PF or across device.
2064 * For SR-IOV VF VSIs queue vector index always starts
2065 * with 1 since first vector index(0) is used for OICR
2066 * in VF space. Since VMDq and other PF VSIs are within
2067 * the PF function space, use the vector index that is
2068 * tracked for this PF.
2070 for (q = 0; q < q_vector->num_ring_tx; q++) {
2071 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
2072 q_vector->tx.itr_idx);
2076 for (q = 0; q < q_vector->num_ring_rx; q++) {
2077 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
2078 q_vector->rx.itr_idx);
2085 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2086 * @vsi: the VSI whose rings are to be enabled
2088 * Returns 0 on success and a negative value on error
2090 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2092 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2096 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2097 * @vsi: the VSI whose rings are to be disabled
2099 * Returns 0 on success and a negative value on error
2101 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2103 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2107 * ice_vsi_stop_tx_rings - Disable Tx rings
2108 * @vsi: the VSI being configured
2109 * @rst_src: reset source
2110 * @rel_vmvf_num: Relative ID of VF/VM
2111 * @rings: Tx ring array to be stopped
2112 * @count: number of Tx ring array elements
2115 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2116 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2120 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2123 for (q_idx = 0; q_idx < count; q_idx++) {
2124 struct ice_txq_meta txq_meta = { };
2127 if (!rings || !rings[q_idx])
2130 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2131 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2132 rings[q_idx], &txq_meta);
2142 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2143 * @vsi: the VSI being configured
2144 * @rst_src: reset source
2145 * @rel_vmvf_num: Relative ID of VF/VM
2148 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2151 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2155 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2156 * @vsi: the VSI being configured
2158 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2160 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2164 * ice_vsi_is_rx_queue_active
2165 * @vsi: the VSI being configured
2167 * Return true if at least one queue is active.
2169 bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
2171 struct ice_pf *pf = vsi->back;
2172 struct ice_hw *hw = &pf->hw;
2175 ice_for_each_rxq(vsi, i) {
2179 pf_q = vsi->rxq_map[i];
2180 rx_reg = rd32(hw, QRX_CTRL(pf_q));
2181 if (rx_reg & QRX_CTRL_QENA_STAT_M)
2189 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2190 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2192 * returns true if Rx VLAN pruning is enabled and false otherwise.
2194 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2199 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2202 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2204 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2205 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2206 vsi->tc_cfg.numtc = 1;
2210 /* set VSI TC information based on DCB config */
2211 ice_vsi_set_dcb_tc_cfg(vsi);
2215 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2216 * @vsi: the VSI being configured
2217 * @tx: bool to determine Tx or Rx rule
2218 * @create: bool to determine create or remove Rule
2220 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2222 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2223 enum ice_sw_fwd_act_type act);
2224 struct ice_pf *pf = vsi->back;
2228 dev = ice_pf_to_dev(pf);
2229 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2232 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2235 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2236 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2239 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2245 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2246 create ? "adding" : "removing", tx ? "TX" : "RX",
2247 vsi->vsi_num, status);
2251 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2252 * @vsi: pointer to the VSI
2254 * This function will allocate new scheduler aggregator now if needed and will
2255 * move specified VSI into it.
2257 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2259 struct device *dev = ice_pf_to_dev(vsi->back);
2260 struct ice_agg_node *agg_node_iter = NULL;
2261 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2262 struct ice_agg_node *agg_node = NULL;
2263 int node_offset, max_agg_nodes = 0;
2264 struct ice_port_info *port_info;
2265 struct ice_pf *pf = vsi->back;
2266 u32 agg_node_id_start = 0;
2269 /* create (as needed) scheduler aggregator node and move VSI into
2270 * corresponding aggregator node
2271 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2272 * - VF aggregator nodes will contain VF VSI
2274 port_info = pf->hw.port_info;
2278 switch (vsi->type) {
2283 case ICE_VSI_SWITCHDEV_CTRL:
2284 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2285 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2286 agg_node_iter = &pf->pf_agg_node[0];
2289 /* user can create 'n' VFs on a given PF, but since max children
2290 * per aggregator node can be only 64. Following code handles
2291 * aggregator(s) for VF VSIs, either selects a agg_node which
2292 * was already created provided num_vsis < 64, otherwise
2293 * select next available node, which will be created
2295 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2296 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2297 agg_node_iter = &pf->vf_agg_node[0];
2300 /* other VSI type, handle later if needed */
2301 dev_dbg(dev, "unexpected VSI type %s\n",
2302 ice_vsi_type_str(vsi->type));
2306 /* find the appropriate aggregator node */
2307 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2308 /* see if we can find space in previously created
2309 * node if num_vsis < 64, otherwise skip
2311 if (agg_node_iter->num_vsis &&
2312 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2317 if (agg_node_iter->valid &&
2318 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2319 agg_id = agg_node_iter->agg_id;
2320 agg_node = agg_node_iter;
2324 /* find unclaimed agg_id */
2325 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2326 agg_id = node_offset + agg_node_id_start;
2327 agg_node = agg_node_iter;
2330 /* move to next agg_node */
2337 /* if selected aggregator node was not created, create it */
2338 if (!agg_node->valid) {
2339 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2340 (u8)vsi->tc_cfg.ena_tc);
2342 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2346 /* aggregator node is created, store the needed info */
2347 agg_node->valid = true;
2348 agg_node->agg_id = agg_id;
2351 /* move VSI to corresponding aggregator node */
2352 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2353 (u8)vsi->tc_cfg.ena_tc);
2355 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2360 /* keep active children count for aggregator node */
2361 agg_node->num_vsis++;
2363 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2364 * to aggregator node
2366 vsi->agg_node = agg_node;
2367 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2368 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2369 vsi->agg_node->num_vsis);
2372 static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
2374 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2375 struct device *dev = ice_pf_to_dev(pf);
2378 /* configure VSI nodes based on number of queues and TC's */
2379 ice_for_each_traffic_class(i) {
2380 if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2383 if (vsi->type == ICE_VSI_CHNL) {
2384 if (!vsi->alloc_txq && vsi->num_txq)
2385 max_txqs[i] = vsi->num_txq;
2387 max_txqs[i] = pf->num_lan_tx;
2389 max_txqs[i] = vsi->alloc_txq;
2393 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2394 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2397 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2406 * ice_vsi_cfg_def - configure default VSI based on the type
2407 * @vsi: pointer to VSI
2408 * @params: the parameters to configure this VSI with
2411 ice_vsi_cfg_def(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2413 struct device *dev = ice_pf_to_dev(vsi->back);
2414 struct ice_pf *pf = vsi->back;
2417 vsi->vsw = pf->first_sw;
2419 ret = ice_vsi_alloc_def(vsi, params->ch);
2423 /* allocate memory for Tx/Rx ring stat pointers */
2424 ret = ice_vsi_alloc_stat_arrays(vsi);
2426 goto unroll_vsi_alloc;
2428 ice_alloc_fd_res(vsi);
2430 ret = ice_vsi_get_qs(vsi);
2432 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2434 goto unroll_vsi_alloc_stat;
2437 /* set RSS capabilities */
2438 ice_vsi_set_rss_params(vsi);
2440 /* set TC configuration */
2441 ice_vsi_set_tc_cfg(vsi);
2443 /* create the VSI */
2444 ret = ice_vsi_init(vsi, params->flags);
2448 ice_vsi_init_vlan_ops(vsi);
2450 switch (vsi->type) {
2452 case ICE_VSI_SWITCHDEV_CTRL:
2454 ret = ice_vsi_alloc_q_vectors(vsi);
2456 goto unroll_vsi_init;
2458 ret = ice_vsi_alloc_rings(vsi);
2460 goto unroll_vector_base;
2462 ret = ice_vsi_alloc_ring_stats(vsi);
2464 goto unroll_vector_base;
2466 ice_vsi_map_rings_to_vectors(vsi);
2467 vsi->stat_offsets_loaded = false;
2469 if (ice_is_xdp_ena_vsi(vsi)) {
2470 ret = ice_vsi_determine_xdp_res(vsi);
2472 goto unroll_vector_base;
2473 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2475 goto unroll_vector_base;
2478 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2479 if (vsi->type != ICE_VSI_CTRL)
2480 /* Do not exit if configuring RSS had an issue, at
2481 * least receive traffic on first queue. Hence no
2482 * need to capture return value
2484 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2485 ice_vsi_cfg_rss_lut_key(vsi);
2486 ice_vsi_set_rss_flow_fld(vsi);
2491 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2492 ice_vsi_cfg_rss_lut_key(vsi);
2493 ice_vsi_set_rss_flow_fld(vsi);
2497 /* VF driver will take care of creating netdev for this type and
2498 * map queues to vectors through Virtchnl, PF driver only
2499 * creates a VSI and corresponding structures for bookkeeping
2502 ret = ice_vsi_alloc_q_vectors(vsi);
2504 goto unroll_vsi_init;
2506 ret = ice_vsi_alloc_rings(vsi);
2508 goto unroll_alloc_q_vector;
2510 ret = ice_vsi_alloc_ring_stats(vsi);
2512 goto unroll_vector_base;
2514 vsi->stat_offsets_loaded = false;
2516 /* Do not exit if configuring RSS had an issue, at least
2517 * receive traffic on first queue. Hence no need to capture
2520 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2521 ice_vsi_cfg_rss_lut_key(vsi);
2522 ice_vsi_set_vf_rss_flow_fld(vsi);
2526 ret = ice_vsi_alloc_rings(vsi);
2528 goto unroll_vsi_init;
2530 ret = ice_vsi_alloc_ring_stats(vsi);
2532 goto unroll_vector_base;
2536 /* clean up the resources and exit */
2538 goto unroll_vsi_init;
2544 /* reclaim SW interrupts back to the common pool */
2545 unroll_alloc_q_vector:
2546 ice_vsi_free_q_vectors(vsi);
2548 ice_vsi_delete_from_hw(vsi);
2550 ice_vsi_put_qs(vsi);
2551 unroll_vsi_alloc_stat:
2552 ice_vsi_free_stats(vsi);
2554 ice_vsi_free_arrays(vsi);
2559 * ice_vsi_cfg - configure a previously allocated VSI
2560 * @vsi: pointer to VSI
2561 * @params: parameters used to configure this VSI
2563 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params)
2565 struct ice_pf *pf = vsi->back;
2568 if (WARN_ON(params->type == ICE_VSI_VF && !params->vf))
2571 vsi->type = params->type;
2572 vsi->port_info = params->pi;
2574 /* For VSIs which don't have a connected VF, this will be NULL */
2575 vsi->vf = params->vf;
2577 ret = ice_vsi_cfg_def(vsi, params);
2581 ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2585 if (vsi->type == ICE_VSI_CTRL) {
2587 WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2588 vsi->vf->ctrl_vsi_idx = vsi->idx;
2590 WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2591 pf->ctrl_vsi_idx = vsi->idx;
2599 * ice_vsi_decfg - remove all VSI configuration
2600 * @vsi: pointer to VSI
2602 void ice_vsi_decfg(struct ice_vsi *vsi)
2604 struct ice_pf *pf = vsi->back;
2607 /* The Rx rule will only exist to remove if the LLDP FW
2608 * engine is currently stopped
2610 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2611 !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2612 ice_cfg_sw_lldp(vsi, false, false);
2614 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2615 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2617 dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2620 if (ice_is_xdp_ena_vsi(vsi))
2621 /* return value check can be skipped here, it always returns
2622 * 0 if reset is in progress
2624 ice_destroy_xdp_rings(vsi);
2626 ice_vsi_clear_rings(vsi);
2627 ice_vsi_free_q_vectors(vsi);
2628 ice_vsi_put_qs(vsi);
2629 ice_vsi_free_arrays(vsi);
2631 /* SR-IOV determines needed MSIX resources all at once instead of per
2632 * VSI since when VFs are spawned we know how many VFs there are and how
2633 * many interrupts each VF needs. SR-IOV MSIX resources are also
2634 * cleared in the same manner.
2637 if (vsi->type == ICE_VSI_VF &&
2638 vsi->agg_node && vsi->agg_node->valid)
2639 vsi->agg_node->num_vsis--;
2640 if (vsi->agg_node) {
2641 vsi->agg_node->valid = false;
2642 vsi->agg_node->agg_id = 0;
2647 * ice_vsi_setup - Set up a VSI by a given type
2648 * @pf: board private structure
2649 * @params: parameters to use when creating the VSI
2651 * This allocates the sw VSI structure and its queue resources.
2653 * Returns pointer to the successfully allocated and configured VSI sw struct on
2654 * success, NULL on failure.
2657 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2659 struct device *dev = ice_pf_to_dev(pf);
2660 struct ice_vsi *vsi;
2663 /* ice_vsi_setup can only initialize a new VSI, and we must have
2664 * a port_info structure for it.
2666 if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2667 WARN_ON(!params->pi))
2670 vsi = ice_vsi_alloc(pf);
2672 dev_err(dev, "could not allocate VSI\n");
2676 ret = ice_vsi_cfg(vsi, params);
2680 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2681 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2682 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2683 * The rule is added once for PF VSI in order to create appropriate
2684 * recipe, since VSI/VSI list is ignored with drop action...
2685 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2686 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2687 * settings in the HW.
2689 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2690 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2692 ice_cfg_sw_lldp(vsi, true, true);
2696 ice_set_agg_vsi(vsi);
2707 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2708 * @vsi: the VSI being cleaned up
2710 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2712 struct ice_pf *pf = vsi->back;
2713 struct ice_hw *hw = &pf->hw;
2718 ice_for_each_q_vector(vsi, i) {
2719 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2721 ice_write_intrl(q_vector, 0);
2722 for (q = 0; q < q_vector->num_ring_tx; q++) {
2723 ice_write_itr(&q_vector->tx, 0);
2724 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2725 if (ice_is_xdp_ena_vsi(vsi)) {
2726 u32 xdp_txq = txq + vsi->num_xdp_txq;
2728 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2733 for (q = 0; q < q_vector->num_ring_rx; q++) {
2734 ice_write_itr(&q_vector->rx, 0);
2735 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2744 * ice_vsi_free_irq - Free the IRQ association with the OS
2745 * @vsi: the VSI being configured
2747 void ice_vsi_free_irq(struct ice_vsi *vsi)
2749 struct ice_pf *pf = vsi->back;
2752 if (!vsi->q_vectors || !vsi->irqs_ready)
2755 ice_vsi_release_msix(vsi);
2756 if (vsi->type == ICE_VSI_VF)
2759 vsi->irqs_ready = false;
2760 ice_free_cpu_rx_rmap(vsi);
2762 ice_for_each_q_vector(vsi, i) {
2765 irq_num = vsi->q_vectors[i]->irq.virq;
2767 /* free only the irqs that were actually requested */
2768 if (!vsi->q_vectors[i] ||
2769 !(vsi->q_vectors[i]->num_ring_tx ||
2770 vsi->q_vectors[i]->num_ring_rx))
2773 /* clear the affinity notifier in the IRQ descriptor */
2774 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2775 irq_set_affinity_notifier(irq_num, NULL);
2777 /* clear the affinity_mask in the IRQ descriptor */
2778 irq_set_affinity_hint(irq_num, NULL);
2779 synchronize_irq(irq_num);
2780 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2785 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2786 * @vsi: the VSI having resources freed
2788 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2795 ice_for_each_txq(vsi, i)
2796 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2797 ice_free_tx_ring(vsi->tx_rings[i]);
2801 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2802 * @vsi: the VSI having resources freed
2804 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2811 ice_for_each_rxq(vsi, i)
2812 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2813 ice_free_rx_ring(vsi->rx_rings[i]);
2817 * ice_vsi_close - Shut down a VSI
2818 * @vsi: the VSI being shut down
2820 void ice_vsi_close(struct ice_vsi *vsi)
2822 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2825 ice_vsi_free_irq(vsi);
2826 ice_vsi_free_tx_rings(vsi);
2827 ice_vsi_free_rx_rings(vsi);
2831 * ice_ena_vsi - resume a VSI
2832 * @vsi: the VSI being resume
2833 * @locked: is the rtnl_lock already held
2835 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2839 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2842 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2844 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2845 if (netif_running(vsi->netdev)) {
2849 err = ice_open_internal(vsi->netdev);
2854 } else if (vsi->type == ICE_VSI_CTRL) {
2855 err = ice_vsi_open_ctrl(vsi);
2862 * ice_dis_vsi - pause a VSI
2863 * @vsi: the VSI being paused
2864 * @locked: is the rtnl_lock already held
2866 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2868 if (test_bit(ICE_VSI_DOWN, vsi->state))
2871 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2873 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2874 if (netif_running(vsi->netdev)) {
2885 } else if (vsi->type == ICE_VSI_CTRL ||
2886 vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2892 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2893 * @vsi: the VSI being un-configured
2895 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2897 struct ice_pf *pf = vsi->back;
2898 struct ice_hw *hw = &pf->hw;
2902 /* disable interrupt causation from each queue */
2903 if (vsi->tx_rings) {
2904 ice_for_each_txq(vsi, i) {
2905 if (vsi->tx_rings[i]) {
2908 reg = vsi->tx_rings[i]->reg_idx;
2909 val = rd32(hw, QINT_TQCTL(reg));
2910 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2911 wr32(hw, QINT_TQCTL(reg), val);
2916 if (vsi->rx_rings) {
2917 ice_for_each_rxq(vsi, i) {
2918 if (vsi->rx_rings[i]) {
2921 reg = vsi->rx_rings[i]->reg_idx;
2922 val = rd32(hw, QINT_RQCTL(reg));
2923 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2924 wr32(hw, QINT_RQCTL(reg), val);
2929 /* disable each interrupt */
2930 ice_for_each_q_vector(vsi, i) {
2931 if (!vsi->q_vectors[i])
2933 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2938 /* don't call synchronize_irq() for VF's from the host */
2939 if (vsi->type == ICE_VSI_VF)
2942 ice_for_each_q_vector(vsi, i)
2943 synchronize_irq(vsi->q_vectors[i]->irq.virq);
2947 * ice_napi_del - Remove NAPI handler for the VSI
2948 * @vsi: VSI for which NAPI handler is to be removed
2950 void ice_napi_del(struct ice_vsi *vsi)
2957 ice_for_each_q_vector(vsi, v_idx)
2958 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2962 * ice_vsi_release - Delete a VSI and free its resources
2963 * @vsi: the VSI being removed
2965 * Returns 0 on success or < 0 on error
2967 int ice_vsi_release(struct ice_vsi *vsi)
2975 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2981 /* retain SW VSI data structure since it is needed to unregister and
2982 * free VSI netdev when PF is not in reset recovery pending state,\
2983 * for ex: during rmmod.
2985 if (!ice_is_reset_in_progress(pf->state))
2986 ice_vsi_delete(vsi);
2992 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2993 * @vsi: VSI connected with q_vectors
2994 * @coalesce: array of struct with stored coalesce
2996 * Returns array size.
2999 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
3000 struct ice_coalesce_stored *coalesce)
3004 ice_for_each_q_vector(vsi, i) {
3005 struct ice_q_vector *q_vector = vsi->q_vectors[i];
3007 coalesce[i].itr_tx = q_vector->tx.itr_settings;
3008 coalesce[i].itr_rx = q_vector->rx.itr_settings;
3009 coalesce[i].intrl = q_vector->intrl;
3011 if (i < vsi->num_txq)
3012 coalesce[i].tx_valid = true;
3013 if (i < vsi->num_rxq)
3014 coalesce[i].rx_valid = true;
3017 return vsi->num_q_vectors;
3021 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
3022 * @vsi: VSI connected with q_vectors
3023 * @coalesce: pointer to array of struct with stored coalesce
3024 * @size: size of coalesce array
3026 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
3027 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
3031 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
3032 struct ice_coalesce_stored *coalesce, int size)
3034 struct ice_ring_container *rc;
3037 if ((size && !coalesce) || !vsi)
3040 /* There are a couple of cases that have to be handled here:
3041 * 1. The case where the number of queue vectors stays the same, but
3042 * the number of Tx or Rx rings changes (the first for loop)
3043 * 2. The case where the number of queue vectors increased (the
3046 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3047 /* There are 2 cases to handle here and they are the same for
3049 * if the entry was valid previously (coalesce[i].[tr]x_valid
3050 * and the loop variable is less than the number of rings
3051 * allocated, then write the previous values
3053 * if the entry was not valid previously, but the number of
3054 * rings is less than are allocated (this means the number of
3055 * rings increased from previously), then write out the
3056 * values in the first element
3058 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3059 * as there is no harm because the dynamic algorithm
3060 * will just overwrite.
3062 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3063 rc = &vsi->q_vectors[i]->rx;
3064 rc->itr_settings = coalesce[i].itr_rx;
3065 ice_write_itr(rc, rc->itr_setting);
3066 } else if (i < vsi->alloc_rxq) {
3067 rc = &vsi->q_vectors[i]->rx;
3068 rc->itr_settings = coalesce[0].itr_rx;
3069 ice_write_itr(rc, rc->itr_setting);
3072 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3073 rc = &vsi->q_vectors[i]->tx;
3074 rc->itr_settings = coalesce[i].itr_tx;
3075 ice_write_itr(rc, rc->itr_setting);
3076 } else if (i < vsi->alloc_txq) {
3077 rc = &vsi->q_vectors[i]->tx;
3078 rc->itr_settings = coalesce[0].itr_tx;
3079 ice_write_itr(rc, rc->itr_setting);
3082 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3083 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3086 /* the number of queue vectors increased so write whatever is in
3089 for (; i < vsi->num_q_vectors; i++) {
3091 rc = &vsi->q_vectors[i]->tx;
3092 rc->itr_settings = coalesce[0].itr_tx;
3093 ice_write_itr(rc, rc->itr_setting);
3096 rc = &vsi->q_vectors[i]->rx;
3097 rc->itr_settings = coalesce[0].itr_rx;
3098 ice_write_itr(rc, rc->itr_setting);
3100 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3101 ice_set_q_vector_intrl(vsi->q_vectors[i]);
3106 * ice_vsi_realloc_stat_arrays - Frees unused stat structures
3108 * @prev_txq: Number of Tx rings before ring reallocation
3109 * @prev_rxq: Number of Rx rings before ring reallocation
3112 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi, int prev_txq, int prev_rxq)
3114 struct ice_vsi_stats *vsi_stat;
3115 struct ice_pf *pf = vsi->back;
3118 if (!prev_txq || !prev_rxq)
3120 if (vsi->type == ICE_VSI_CHNL)
3123 vsi_stat = pf->vsi_stats[vsi->idx];
3125 if (vsi->num_txq < prev_txq) {
3126 for (i = vsi->num_txq; i < prev_txq; i++) {
3127 if (vsi_stat->tx_ring_stats[i]) {
3128 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
3129 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
3134 if (vsi->num_rxq < prev_rxq) {
3135 for (i = vsi->num_rxq; i < prev_rxq; i++) {
3136 if (vsi_stat->rx_ring_stats[i]) {
3137 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
3138 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
3145 * ice_vsi_rebuild - Rebuild VSI after reset
3146 * @vsi: VSI to be rebuild
3147 * @vsi_flags: flags used for VSI rebuild flow
3149 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3150 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3152 * Returns 0 on success and negative value on failure
3154 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3156 struct ice_vsi_cfg_params params = {};
3157 struct ice_coalesce_stored *coalesce;
3158 int ret, prev_txq, prev_rxq;
3159 int prev_num_q_vectors = 0;
3165 params = ice_vsi_to_params(vsi);
3166 params.flags = vsi_flags;
3169 if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3172 coalesce = kcalloc(vsi->num_q_vectors,
3173 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3177 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3179 prev_txq = vsi->num_txq;
3180 prev_rxq = vsi->num_rxq;
3183 ret = ice_vsi_cfg_def(vsi, ¶ms);
3187 ret = ice_vsi_cfg_tc_lan(pf, vsi);
3189 if (vsi_flags & ICE_VSI_FLAG_INIT) {
3191 goto err_vsi_cfg_tc_lan;
3195 return ice_schedule_reset(pf, ICE_RESET_PFR);
3198 ice_vsi_realloc_stat_arrays(vsi, prev_txq, prev_rxq);
3200 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3213 * ice_is_reset_in_progress - check for a reset in progress
3214 * @state: PF state field
3216 bool ice_is_reset_in_progress(unsigned long *state)
3218 return test_bit(ICE_RESET_OICR_RECV, state) ||
3219 test_bit(ICE_PFR_REQ, state) ||
3220 test_bit(ICE_CORER_REQ, state) ||
3221 test_bit(ICE_GLOBR_REQ, state);
3225 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3226 * @pf: pointer to the PF structure
3227 * @timeout: length of time to wait, in jiffies
3229 * Wait (sleep) for a short time until the driver finishes cleaning up from
3230 * a device reset. The caller must be able to sleep. Use this to delay
3231 * operations that could fail while the driver is cleaning up after a device
3234 * Returns 0 on success, -EBUSY if the reset is not finished within the
3235 * timeout, and -ERESTARTSYS if the thread was interrupted.
3237 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3241 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3242 !ice_is_reset_in_progress(pf->state),
3253 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3254 * @vsi: VSI being configured
3255 * @ctx: the context buffer returned from AQ VSI update command
3257 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3259 vsi->info.mapping_flags = ctx->info.mapping_flags;
3260 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3261 sizeof(vsi->info.q_mapping));
3262 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3263 sizeof(vsi->info.tc_mapping));
3267 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3268 * @vsi: the VSI being configured
3269 * @ena_tc: TC map to be enabled
3271 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3273 struct net_device *netdev = vsi->netdev;
3274 struct ice_pf *pf = vsi->back;
3275 int numtc = vsi->tc_cfg.numtc;
3276 struct ice_dcbx_cfg *dcbcfg;
3283 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3284 if (vsi->type == ICE_VSI_CHNL)
3288 netdev_reset_tc(netdev);
3292 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3293 numtc = vsi->all_numtc;
3295 if (netdev_set_num_tc(netdev, numtc))
3298 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3300 ice_for_each_traffic_class(i)
3301 if (vsi->tc_cfg.ena_tc & BIT(i))
3302 netdev_set_tc_queue(netdev,
3303 vsi->tc_cfg.tc_info[i].netdev_tc,
3304 vsi->tc_cfg.tc_info[i].qcount_tx,
3305 vsi->tc_cfg.tc_info[i].qoffset);
3306 /* setup TC queue map for CHNL TCs */
3307 ice_for_each_chnl_tc(i) {
3308 if (!(vsi->all_enatc & BIT(i)))
3310 if (!vsi->mqprio_qopt.qopt.count[i])
3312 netdev_set_tc_queue(netdev, i,
3313 vsi->mqprio_qopt.qopt.count[i],
3314 vsi->mqprio_qopt.qopt.offset[i]);
3317 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3320 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3321 u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3323 /* Get the mapped netdev TC# for the UP */
3324 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3325 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3330 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3331 * @vsi: the VSI being configured,
3332 * @ctxt: VSI context structure
3333 * @ena_tc: number of traffic classes to enable
3335 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3338 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3341 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3342 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3343 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3344 u16 new_txq, new_rxq;
3348 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3350 pow = order_base_2(tc0_qcount);
3351 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
3352 ICE_AQ_VSI_TC_Q_OFFSET_M) |
3353 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M);
3355 ice_for_each_traffic_class(i) {
3356 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3357 /* TC is not enabled */
3358 vsi->tc_cfg.tc_info[i].qoffset = 0;
3359 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3360 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3361 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3362 ctxt->info.tc_mapping[i] = 0;
3366 offset = vsi->mqprio_qopt.qopt.offset[i];
3367 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3368 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3369 vsi->tc_cfg.tc_info[i].qoffset = offset;
3370 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3371 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3372 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3375 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3376 ice_for_each_chnl_tc(i) {
3377 if (!(vsi->all_enatc & BIT(i)))
3379 offset = vsi->mqprio_qopt.qopt.offset[i];
3380 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3381 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3385 new_txq = offset + qcount_tx;
3386 if (new_txq > vsi->alloc_txq) {
3387 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3388 new_txq, vsi->alloc_txq);
3392 new_rxq = offset + qcount_rx;
3393 if (new_rxq > vsi->alloc_rxq) {
3394 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3395 new_rxq, vsi->alloc_rxq);
3399 /* Set actual Tx/Rx queue pairs */
3400 vsi->num_txq = new_txq;
3401 vsi->num_rxq = new_rxq;
3403 /* Setup queue TC[0].qmap for given VSI context */
3404 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3405 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3406 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3408 /* Find queue count available for channel VSIs and starting offset
3411 if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3412 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3413 vsi->next_base_q = tc0_qcount;
3415 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq);
3416 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq);
3417 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3418 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3424 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3425 * @vsi: VSI to be configured
3426 * @ena_tc: TC bitmap
3428 * VSI queues expected to be quiesced before calling this function
3430 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3432 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3433 struct ice_pf *pf = vsi->back;
3434 struct ice_tc_cfg old_tc_cfg;
3435 struct ice_vsi_ctx *ctx;
3440 dev = ice_pf_to_dev(pf);
3441 if (vsi->tc_cfg.ena_tc == ena_tc &&
3442 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3445 ice_for_each_traffic_class(i) {
3446 /* build bitmap of enabled TCs */
3447 if (ena_tc & BIT(i))
3449 /* populate max_txqs per TC */
3450 max_txqs[i] = vsi->alloc_txq;
3451 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3452 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3454 if (vsi->type == ICE_VSI_CHNL &&
3455 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3456 max_txqs[i] = vsi->num_txq;
3459 memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3460 vsi->tc_cfg.ena_tc = ena_tc;
3461 vsi->tc_cfg.numtc = num_tc;
3463 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3468 ctx->info = vsi->info;
3470 if (vsi->type == ICE_VSI_PF &&
3471 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3472 ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3474 ret = ice_vsi_setup_q_map(vsi, ctx);
3477 memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3481 /* must to indicate which section of VSI context are being modified */
3482 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3483 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3485 dev_info(dev, "Failed VSI Update\n");
3489 if (vsi->type == ICE_VSI_PF &&
3490 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3491 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3493 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3494 vsi->tc_cfg.ena_tc, max_txqs);
3497 dev_err(dev, "VSI %d failed TC config, error %d\n",
3501 ice_vsi_update_q_map(vsi, ctx);
3502 vsi->info.valid_sections = 0;
3504 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3511 * ice_update_ring_stats - Update ring statistics
3512 * @stats: stats to be updated
3513 * @pkts: number of processed packets
3514 * @bytes: number of processed bytes
3516 * This function assumes that caller has acquired a u64_stats_sync lock.
3518 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3520 stats->bytes += bytes;
3521 stats->pkts += pkts;
3525 * ice_update_tx_ring_stats - Update Tx ring specific counters
3526 * @tx_ring: ring to update
3527 * @pkts: number of processed packets
3528 * @bytes: number of processed bytes
3530 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3532 u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3533 ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3534 u64_stats_update_end(&tx_ring->ring_stats->syncp);
3538 * ice_update_rx_ring_stats - Update Rx ring specific counters
3539 * @rx_ring: ring to update
3540 * @pkts: number of processed packets
3541 * @bytes: number of processed bytes
3543 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3545 u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3546 ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3547 u64_stats_update_end(&rx_ring->ring_stats->syncp);
3551 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3552 * @pi: port info of the switch with default VSI
3554 * Return true if the there is a single VSI in default forwarding VSI list
3556 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3558 bool exists = false;
3560 ice_check_if_dflt_vsi(pi, 0, &exists);
3565 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3566 * @vsi: VSI to compare against default forwarding VSI
3568 * If this VSI passed in is the default forwarding VSI then return true, else
3571 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3573 return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3577 * ice_set_dflt_vsi - set the default forwarding VSI
3578 * @vsi: VSI getting set as the default forwarding VSI on the switch
3580 * If the VSI passed in is already the default VSI and it's enabled just return
3583 * Otherwise try to set the VSI passed in as the switch's default VSI and
3584 * return the result.
3586 int ice_set_dflt_vsi(struct ice_vsi *vsi)
3594 dev = ice_pf_to_dev(vsi->back);
3596 /* the VSI passed in is already the default VSI */
3597 if (ice_is_vsi_dflt_vsi(vsi)) {
3598 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3603 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
3605 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3606 vsi->vsi_num, status);
3614 * ice_clear_dflt_vsi - clear the default forwarding VSI
3615 * @vsi: VSI to remove from filter list
3617 * If the switch has no default VSI or it's not enabled then return error.
3619 * Otherwise try to clear the default VSI and return the result.
3621 int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3629 dev = ice_pf_to_dev(vsi->back);
3631 /* there is no default VSI configured */
3632 if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3635 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3638 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3639 vsi->vsi_num, status);
3647 * ice_get_link_speed_mbps - get link speed in Mbps
3648 * @vsi: the VSI whose link speed is being queried
3650 * Return current VSI link speed and 0 if the speed is unknown.
3652 int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3654 unsigned int link_speed;
3656 link_speed = vsi->port_info->phy.link_info.link_speed;
3658 return (int)ice_get_link_speed(fls(link_speed) - 1);
3662 * ice_get_link_speed_kbps - get link speed in Kbps
3663 * @vsi: the VSI whose link speed is being queried
3665 * Return current VSI link speed and 0 if the speed is unknown.
3667 int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3671 speed_mbps = ice_get_link_speed_mbps(vsi);
3673 return speed_mbps * 1000;
3677 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3678 * @vsi: VSI to be configured
3679 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3681 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3682 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3685 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3687 struct ice_pf *pf = vsi->back;
3692 dev = ice_pf_to_dev(pf);
3693 if (!vsi->port_info) {
3694 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3695 vsi->idx, vsi->type);
3699 speed = ice_get_link_speed_kbps(vsi);
3700 if (min_tx_rate > (u64)speed) {
3701 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3702 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3707 /* Configure min BW for VSI limit */
3709 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3710 ICE_MIN_BW, min_tx_rate);
3712 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3713 min_tx_rate, ice_vsi_type_str(vsi->type),
3718 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3719 min_tx_rate, ice_vsi_type_str(vsi->type));
3721 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3725 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3726 ice_vsi_type_str(vsi->type), vsi->idx);
3730 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3731 ice_vsi_type_str(vsi->type), vsi->idx);
3738 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3739 * @vsi: VSI to be configured
3740 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3742 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3743 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3746 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3748 struct ice_pf *pf = vsi->back;
3753 dev = ice_pf_to_dev(pf);
3754 if (!vsi->port_info) {
3755 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3756 vsi->idx, vsi->type);
3760 speed = ice_get_link_speed_kbps(vsi);
3761 if (max_tx_rate > (u64)speed) {
3762 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3763 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3768 /* Configure max BW for VSI limit */
3770 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3771 ICE_MAX_BW, max_tx_rate);
3773 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3774 max_tx_rate, ice_vsi_type_str(vsi->type),
3779 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3780 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3782 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3786 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3787 ice_vsi_type_str(vsi->type), vsi->idx);
3791 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3792 ice_vsi_type_str(vsi->type), vsi->idx);
3799 * ice_set_link - turn on/off physical link
3800 * @vsi: VSI to modify physical link on
3801 * @ena: turn on/off physical link
3803 int ice_set_link(struct ice_vsi *vsi, bool ena)
3805 struct device *dev = ice_pf_to_dev(vsi->back);
3806 struct ice_port_info *pi = vsi->port_info;
3807 struct ice_hw *hw = pi->hw;
3810 if (vsi->type != ICE_VSI_PF)
3813 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3815 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3816 * this is not a fatal error, so print a warning message and return
3817 * a success code. Return an error if FW returns an error code other
3818 * than ICE_AQ_RC_EMODE
3820 if (status == -EIO) {
3821 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3822 dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3823 (ena ? "ON" : "OFF"), status,
3824 ice_aq_str(hw->adminq.sq_last_status));
3825 } else if (status) {
3826 dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3827 (ena ? "ON" : "OFF"), status,
3828 ice_aq_str(hw->adminq.sq_last_status));
3836 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3837 * @vsi: VSI used to add VLAN filters
3839 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3840 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3841 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3842 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3844 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3845 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3846 * traffic in SVM, since the VLAN TPID isn't part of filtering.
3848 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3849 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3850 * part of filtering.
3852 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3854 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3855 struct ice_vlan vlan;
3858 vlan = ICE_VLAN(0, 0, 0);
3859 err = vlan_ops->add_vlan(vsi, &vlan);
3860 if (err && err != -EEXIST)
3863 /* in SVM both VLAN 0 filters are identical */
3864 if (!ice_is_dvm_ena(&vsi->back->hw))
3867 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3868 err = vlan_ops->add_vlan(vsi, &vlan);
3869 if (err && err != -EEXIST)
3876 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3877 * @vsi: VSI used to add VLAN filters
3879 * Delete the VLAN 0 filters in the same manner that they were added in
3880 * ice_vsi_add_vlan_zero.
3882 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3884 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3885 struct ice_vlan vlan;
3888 vlan = ICE_VLAN(0, 0, 0);
3889 err = vlan_ops->del_vlan(vsi, &vlan);
3890 if (err && err != -EEXIST)
3893 /* in SVM both VLAN 0 filters are identical */
3894 if (!ice_is_dvm_ena(&vsi->back->hw))
3897 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3898 err = vlan_ops->del_vlan(vsi, &vlan);
3899 if (err && err != -EEXIST)
3902 /* when deleting the last VLAN filter, make sure to disable the VLAN
3903 * promisc mode so the filter isn't left by accident
3905 return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3906 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3910 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3911 * @vsi: VSI used to get the VLAN mode
3913 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3914 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3916 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3918 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2
3919 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1
3920 /* no VLAN 0 filter is created when a port VLAN is active */
3921 if (vsi->type == ICE_VSI_VF) {
3922 if (WARN_ON(!vsi->vf))
3925 if (ice_vf_is_port_vlan_ena(vsi->vf))
3929 if (ice_is_dvm_ena(&vsi->back->hw))
3930 return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3932 return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3936 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3937 * @vsi: VSI used to determine if any non-zero VLANs have been added
3939 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3941 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3945 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3946 * @vsi: VSI used to get the number of non-zero VLANs added
3948 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3950 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3954 * ice_is_feature_supported
3955 * @pf: pointer to the struct ice_pf instance
3956 * @f: feature enum to be checked
3958 * returns true if feature is supported, false otherwise
3960 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3962 if (f < 0 || f >= ICE_F_MAX)
3965 return test_bit(f, pf->features);
3969 * ice_set_feature_support
3970 * @pf: pointer to the struct ice_pf instance
3971 * @f: feature enum to set
3973 void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
3975 if (f < 0 || f >= ICE_F_MAX)
3978 set_bit(f, pf->features);
3982 * ice_clear_feature_support
3983 * @pf: pointer to the struct ice_pf instance
3984 * @f: feature enum to clear
3986 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
3988 if (f < 0 || f >= ICE_F_MAX)
3991 clear_bit(f, pf->features);
3995 * ice_init_feature_support
3996 * @pf: pointer to the struct ice_pf instance
3998 * called during init to setup supported feature
4000 void ice_init_feature_support(struct ice_pf *pf)
4002 switch (pf->hw.device_id) {
4003 case ICE_DEV_ID_E810C_BACKPLANE:
4004 case ICE_DEV_ID_E810C_QSFP:
4005 case ICE_DEV_ID_E810C_SFP:
4006 ice_set_feature_support(pf, ICE_F_DSCP);
4007 ice_set_feature_support(pf, ICE_F_PTP_EXTTS);
4008 if (ice_is_e810t(&pf->hw)) {
4009 ice_set_feature_support(pf, ICE_F_SMA_CTRL);
4010 if (ice_gnss_is_gps_present(&pf->hw))
4011 ice_set_feature_support(pf, ICE_F_GNSS);
4020 * ice_vsi_update_security - update security block in VSI
4021 * @vsi: pointer to VSI structure
4022 * @fill: function pointer to fill ctx
4025 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
4027 struct ice_vsi_ctx ctx = { 0 };
4029 ctx.info = vsi->info;
4030 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
4033 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4036 vsi->info = ctx.info;
4041 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
4042 * @ctx: pointer to VSI ctx structure
4044 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
4046 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
4047 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4048 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4052 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
4053 * @ctx: pointer to VSI ctx structure
4055 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
4057 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
4058 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4059 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4063 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
4064 * @ctx: pointer to VSI ctx structure
4066 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
4068 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4072 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
4073 * @ctx: pointer to VSI ctx structure
4075 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
4077 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
4081 * ice_vsi_update_local_lb - update sw block in VSI with local loopback bit
4082 * @vsi: pointer to VSI structure
4083 * @set: set or unset the bit
4086 ice_vsi_update_local_lb(struct ice_vsi *vsi, bool set)
4088 struct ice_vsi_ctx ctx = {
4092 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
4094 ctx.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
4096 ctx.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
4098 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
4101 vsi->info = ctx.info;