1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
8 #include "ice_dcb_lib.h"
11 * ice_vsi_type_str - maps VSI type enum to string equivalents
12 * @type: VSI type enum
14 const char *ice_vsi_type_str(enum ice_vsi_type type)
29 * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
30 * @vsi: the VSI being configured
31 * @ena: start or stop the Rx rings
33 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
37 for (i = 0; i < vsi->num_rxq; i++) {
38 ret = ice_vsi_ctrl_rx_ring(vsi, ena, i);
47 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
50 * On error: returns error code (negative)
51 * On success: returns 0
53 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
55 struct ice_pf *pf = vsi->back;
58 dev = ice_pf_to_dev(pf);
60 /* allocate memory for both Tx and Rx ring pointers */
61 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
62 sizeof(*vsi->tx_rings), GFP_KERNEL);
66 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
67 sizeof(*vsi->rx_rings), GFP_KERNEL);
71 /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
72 vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
73 sizeof(*vsi->txq_map), GFP_KERNEL);
78 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
79 sizeof(*vsi->rxq_map), GFP_KERNEL);
83 /* There is no need to allocate q_vectors for a loopback VSI. */
84 if (vsi->type == ICE_VSI_LB)
87 /* allocate memory for q_vector pointers */
88 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
89 sizeof(*vsi->q_vectors), GFP_KERNEL);
96 devm_kfree(dev, vsi->rxq_map);
98 devm_kfree(dev, vsi->txq_map);
100 devm_kfree(dev, vsi->rx_rings);
102 devm_kfree(dev, vsi->tx_rings);
107 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
108 * @vsi: the VSI being configured
110 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
116 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
117 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
120 dev_dbg(&vsi->back->pdev->dev,
121 "Not setting number of Tx/Rx descriptors for VSI type %d\n",
128 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
129 * @vsi: the VSI being configured
130 * @vf_id: ID of the VF being configured
132 * Return 0 on success and a negative value on error
134 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
136 struct ice_pf *pf = vsi->back;
137 struct ice_vf *vf = NULL;
139 if (vsi->type == ICE_VSI_VF)
144 vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
147 vsi->alloc_txq = vsi->req_txq;
148 vsi->num_txq = vsi->req_txq;
151 pf->num_lan_tx = vsi->alloc_txq;
153 /* only 1 Rx queue unless RSS is enabled */
154 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
157 vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
160 vsi->alloc_rxq = vsi->req_rxq;
161 vsi->num_rxq = vsi->req_rxq;
165 pf->num_lan_rx = vsi->alloc_rxq;
167 vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
170 vf = &pf->vf[vsi->vf_id];
171 vsi->alloc_txq = vf->num_vf_qs;
172 vsi->alloc_rxq = vf->num_vf_qs;
173 /* pf->num_vf_msix includes (VF miscellaneous vector +
174 * data queue interrupts). Since vsi->num_q_vectors is number
175 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
176 * original vector count
178 vsi->num_q_vectors = pf->num_vf_msix - ICE_NONQ_VECS_VF;
185 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
189 ice_vsi_set_num_desc(vsi);
193 * ice_get_free_slot - get the next non-NULL location index in array
194 * @array: array to search
195 * @size: size of the array
196 * @curr: last known occupied index to be used as a search hint
198 * void * is being used to keep the functionality generic. This lets us use this
199 * function on any array of pointers.
201 static int ice_get_free_slot(void *array, int size, int curr)
203 int **tmp_array = (int **)array;
206 if (curr < (size - 1) && !tmp_array[curr + 1]) {
211 while ((i < size) && (tmp_array[i]))
222 * ice_vsi_delete - delete a VSI from the switch
223 * @vsi: pointer to VSI being removed
225 void ice_vsi_delete(struct ice_vsi *vsi)
227 struct ice_pf *pf = vsi->back;
228 struct ice_vsi_ctx *ctxt;
229 enum ice_status status;
231 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
235 if (vsi->type == ICE_VSI_VF)
236 ctxt->vf_num = vsi->vf_id;
237 ctxt->vsi_num = vsi->vsi_num;
239 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
241 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
243 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
244 vsi->vsi_num, status);
250 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
251 * @vsi: pointer to VSI being cleared
253 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
255 struct ice_pf *pf = vsi->back;
258 dev = ice_pf_to_dev(pf);
260 /* free the ring and vector containers */
261 if (vsi->q_vectors) {
262 devm_kfree(dev, vsi->q_vectors);
263 vsi->q_vectors = NULL;
266 devm_kfree(dev, vsi->tx_rings);
267 vsi->tx_rings = NULL;
270 devm_kfree(dev, vsi->rx_rings);
271 vsi->rx_rings = NULL;
274 devm_kfree(dev, vsi->txq_map);
278 devm_kfree(dev, vsi->rxq_map);
284 * ice_vsi_clear - clean up and deallocate the provided VSI
285 * @vsi: pointer to VSI being cleared
287 * This deallocates the VSI's queue resources, removes it from the PF's
288 * VSI array if necessary, and deallocates the VSI
290 * Returns 0 on success, negative on failure
292 int ice_vsi_clear(struct ice_vsi *vsi)
294 struct ice_pf *pf = NULL;
304 dev = ice_pf_to_dev(pf);
306 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
307 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
311 mutex_lock(&pf->sw_mutex);
312 /* updates the PF for this cleared VSI */
314 pf->vsi[vsi->idx] = NULL;
315 if (vsi->idx < pf->next_vsi)
316 pf->next_vsi = vsi->idx;
318 ice_vsi_free_arrays(vsi);
319 mutex_unlock(&pf->sw_mutex);
320 devm_kfree(dev, vsi);
326 * ice_msix_clean_rings - MSIX mode Interrupt Handler
327 * @irq: interrupt number
328 * @data: pointer to a q_vector
330 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
332 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
334 if (!q_vector->tx.ring && !q_vector->rx.ring)
337 napi_schedule(&q_vector->napi);
343 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
344 * @pf: board private structure
346 * @vf_id: ID of the VF being configured
348 * returns a pointer to a VSI on success, NULL on failure.
350 static struct ice_vsi *
351 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type, u16 vf_id)
353 struct device *dev = ice_pf_to_dev(pf);
354 struct ice_vsi *vsi = NULL;
356 /* Need to protect the allocation of the VSIs at the PF level */
357 mutex_lock(&pf->sw_mutex);
359 /* If we have already allocated our maximum number of VSIs,
360 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
361 * is available to be populated
363 if (pf->next_vsi == ICE_NO_VSI) {
364 dev_dbg(dev, "out of VSI slots!\n");
368 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
374 set_bit(__ICE_DOWN, vsi->state);
376 vsi->idx = pf->next_vsi;
378 if (type == ICE_VSI_VF)
379 ice_vsi_set_num_qs(vsi, vf_id);
381 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
385 if (ice_vsi_alloc_arrays(vsi))
388 /* Setup default MSIX irq handler for VSI */
389 vsi->irq_handler = ice_msix_clean_rings;
392 if (ice_vsi_alloc_arrays(vsi))
396 if (ice_vsi_alloc_arrays(vsi))
400 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
404 /* fill VSI slot in the PF struct */
405 pf->vsi[pf->next_vsi] = vsi;
407 /* prepare pf->next_vsi for next use */
408 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
413 devm_kfree(dev, vsi);
416 mutex_unlock(&pf->sw_mutex);
421 * ice_vsi_get_qs - Assign queues from PF to VSI
422 * @vsi: the VSI to assign queues to
424 * Returns 0 on success and a negative value on error
426 static int ice_vsi_get_qs(struct ice_vsi *vsi)
428 struct ice_pf *pf = vsi->back;
429 struct ice_qs_cfg tx_qs_cfg = {
430 .qs_mutex = &pf->avail_q_mutex,
431 .pf_map = pf->avail_txqs,
432 .pf_map_size = pf->max_pf_txqs,
433 .q_count = vsi->alloc_txq,
434 .scatter_count = ICE_MAX_SCATTER_TXQS,
435 .vsi_map = vsi->txq_map,
437 .mapping_mode = vsi->tx_mapping_mode
439 struct ice_qs_cfg rx_qs_cfg = {
440 .qs_mutex = &pf->avail_q_mutex,
441 .pf_map = pf->avail_rxqs,
442 .pf_map_size = pf->max_pf_rxqs,
443 .q_count = vsi->alloc_rxq,
444 .scatter_count = ICE_MAX_SCATTER_RXQS,
445 .vsi_map = vsi->rxq_map,
447 .mapping_mode = vsi->rx_mapping_mode
451 vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
452 vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
454 ret = __ice_vsi_get_qs(&tx_qs_cfg);
456 ret = __ice_vsi_get_qs(&rx_qs_cfg);
462 * ice_vsi_put_qs - Release queues from VSI to PF
463 * @vsi: the VSI that is going to release queues
465 void ice_vsi_put_qs(struct ice_vsi *vsi)
467 struct ice_pf *pf = vsi->back;
470 mutex_lock(&pf->avail_q_mutex);
472 for (i = 0; i < vsi->alloc_txq; i++) {
473 clear_bit(vsi->txq_map[i], pf->avail_txqs);
474 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
477 for (i = 0; i < vsi->alloc_rxq; i++) {
478 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
479 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
482 mutex_unlock(&pf->avail_q_mutex);
487 * @pf: pointer to the PF struct
489 * returns true if driver is in safe mode, false otherwise
491 bool ice_is_safe_mode(struct ice_pf *pf)
493 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
497 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
498 * @vsi: the VSI being cleaned up
500 * This function deletes RSS input set for all flows that were configured
503 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
505 struct ice_pf *pf = vsi->back;
506 enum ice_status status;
508 if (ice_is_safe_mode(pf))
511 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
513 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
514 vsi->vsi_num, status);
518 * ice_rss_clean - Delete RSS related VSI structures and configuration
519 * @vsi: the VSI being removed
521 static void ice_rss_clean(struct ice_vsi *vsi)
523 struct ice_pf *pf = vsi->back;
526 dev = ice_pf_to_dev(pf);
528 if (vsi->rss_hkey_user)
529 devm_kfree(dev, vsi->rss_hkey_user);
530 if (vsi->rss_lut_user)
531 devm_kfree(dev, vsi->rss_lut_user);
533 ice_vsi_clean_rss_flow_fld(vsi);
534 /* remove RSS replay list */
535 if (!ice_is_safe_mode(pf))
536 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
540 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
541 * @vsi: the VSI being configured
543 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
545 struct ice_hw_common_caps *cap;
546 struct ice_pf *pf = vsi->back;
548 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
553 cap = &pf->hw.func_caps.common_cap;
556 /* PF VSI will inherit RSS instance of PF */
557 vsi->rss_table_size = cap->rss_table_size;
558 vsi->rss_size = min_t(int, num_online_cpus(),
559 BIT(cap->rss_table_entry_width));
560 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
563 /* VF VSI will gets a small RSS table
564 * For VSI_LUT, LUT size should be set to 64 bytes
566 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
567 vsi->rss_size = min_t(int, num_online_cpus(),
568 BIT(cap->rss_table_entry_width));
569 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
574 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n",
581 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
582 * @ctxt: the VSI context being set
584 * This initializes a default VSI context for all sections except the Queues.
586 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
590 memset(&ctxt->info, 0, sizeof(ctxt->info));
591 /* VSI's should be allocated from shared pool */
592 ctxt->alloc_from_pool = true;
593 /* Src pruning enabled by default */
594 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
595 /* Traffic from VSI can be sent to LAN */
596 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
597 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
598 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
599 * packets untagged/tagged.
601 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
602 ICE_AQ_VSI_VLAN_MODE_M) >>
603 ICE_AQ_VSI_VLAN_MODE_S);
604 /* Have 1:1 UP mapping for both ingress/egress tables */
605 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
606 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
607 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
608 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
609 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
610 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
611 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
612 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
613 ctxt->info.ingress_table = cpu_to_le32(table);
614 ctxt->info.egress_table = cpu_to_le32(table);
615 /* Have 1:1 UP mapping for outer to inner UP table */
616 ctxt->info.outer_up_table = cpu_to_le32(table);
617 /* No Outer tag support outer_tag_flags remains to zero */
621 * ice_vsi_setup_q_map - Setup a VSI queue map
622 * @vsi: the VSI being configured
623 * @ctxt: VSI context structure
625 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
627 u16 offset = 0, qmap = 0, tx_count = 0;
628 u16 qcount_tx = vsi->alloc_txq;
629 u16 qcount_rx = vsi->alloc_rxq;
630 u16 tx_numq_tc, rx_numq_tc;
631 u16 pow = 0, max_rss = 0;
632 bool ena_tc0 = false;
636 /* at least TC0 should be enabled by default */
637 if (vsi->tc_cfg.numtc) {
638 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
646 vsi->tc_cfg.ena_tc |= 1;
649 rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
652 tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
656 /* TC mapping is a function of the number of Rx queues assigned to the
657 * VSI for each traffic class and the offset of these queues.
658 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
659 * queues allocated to TC0. No:of queues is a power-of-2.
661 * If TC is not enabled, the queue offset is set to 0, and allocate one
662 * queue, this way, traffic for the given TC will be sent to the default
665 * Setup number and offset of Rx queues for all TCs for the VSI
668 qcount_rx = rx_numq_tc;
670 /* qcount will change if RSS is enabled */
671 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
672 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
673 if (vsi->type == ICE_VSI_PF)
674 max_rss = ICE_MAX_LG_RSS_QS;
676 max_rss = ICE_MAX_SMALL_RSS_QS;
677 qcount_rx = min_t(int, rx_numq_tc, max_rss);
679 qcount_rx = min_t(int, qcount_rx,
684 /* find the (rounded up) power-of-2 of qcount */
685 pow = order_base_2(qcount_rx);
687 ice_for_each_traffic_class(i) {
688 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
689 /* TC is not enabled */
690 vsi->tc_cfg.tc_info[i].qoffset = 0;
691 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
692 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
693 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
694 ctxt->info.tc_mapping[i] = 0;
699 vsi->tc_cfg.tc_info[i].qoffset = offset;
700 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
701 vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
702 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
704 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
705 ICE_AQ_VSI_TC_Q_OFFSET_M) |
706 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
707 ICE_AQ_VSI_TC_Q_NUM_M);
709 tx_count += tx_numq_tc;
710 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
713 /* if offset is non-zero, means it is calculated correctly based on
714 * enabled TCs for a given VSI otherwise qcount_rx will always
715 * be correct and non-zero because it is based off - VSI's
716 * allocated Rx queues which is at least 1 (hence qcount_tx will be
720 vsi->num_rxq = offset;
722 vsi->num_rxq = qcount_rx;
724 vsi->num_txq = tx_count;
726 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
727 dev_dbg(&vsi->back->pdev->dev, "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
728 /* since there is a chance that num_rxq could have been changed
729 * in the above for loop, make num_txq equal to num_rxq.
731 vsi->num_txq = vsi->num_rxq;
734 /* Rx queue mapping */
735 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
736 /* q_mapping buffer holds the info for the first queue allocated for
737 * this VSI in the PF space and also the number of queues associated
740 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
741 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
745 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
746 * @ctxt: the VSI context being set
747 * @vsi: the VSI being configured
749 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
751 u8 lut_type, hash_type;
756 dev = ice_pf_to_dev(pf);
760 /* PF VSI will inherit RSS instance of PF */
761 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
762 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
765 /* VF VSI will gets a small RSS table which is a VSI LUT type */
766 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
767 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
770 dev_dbg(dev, "Unsupported VSI type %s\n",
771 ice_vsi_type_str(vsi->type));
774 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
778 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
779 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
780 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
781 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
785 * ice_vsi_init - Create and initialize a VSI
786 * @vsi: the VSI being configured
787 * @init_vsi: is this call creating a VSI
789 * This initializes a VSI context depending on the VSI type to be added and
790 * passes it down to the add_vsi aq command to create a new VSI.
792 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
794 struct ice_pf *pf = vsi->back;
795 struct ice_hw *hw = &pf->hw;
796 struct ice_vsi_ctx *ctxt;
800 dev = ice_pf_to_dev(pf);
801 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
805 ctxt->info = vsi->info;
810 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
813 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
814 /* VF number here is the absolute VF number (0-255) */
815 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
822 ice_set_dflt_vsi_ctx(ctxt);
823 /* if the switch is in VEB mode, allow VSI loopback */
824 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
825 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
827 /* Set LUT type and HASH type if RSS is enabled */
828 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
829 ice_set_rss_vsi_ctx(ctxt, vsi);
830 /* if updating VSI context, make sure to set valid_section:
831 * to indicate which section of VSI context being updated
834 ctxt->info.valid_sections |=
835 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
838 ctxt->info.sw_id = vsi->port_info->sw_id;
839 ice_vsi_setup_q_map(vsi, ctxt);
840 if (!init_vsi) /* means VSI being updated */
841 /* must to indicate which section of VSI context are
844 ctxt->info.valid_sections |=
845 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
847 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
850 if (vsi->type == ICE_VSI_VF) {
851 ctxt->info.valid_sections |=
852 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
853 if (pf->vf[vsi->vf_id].spoofchk) {
854 ctxt->info.sec_flags |=
855 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
856 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
857 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
859 ctxt->info.sec_flags &=
860 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
861 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
862 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
866 /* Allow control frames out of main VSI */
867 if (vsi->type == ICE_VSI_PF) {
868 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
869 ctxt->info.valid_sections |=
870 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
874 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
876 dev_err(dev, "Add VSI failed, err %d\n", ret);
881 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
883 dev_err(dev, "Update VSI failed, err %d\n", ret);
889 /* keep context for update VSI operations */
890 vsi->info = ctxt->info;
892 /* record VSI number returned */
893 vsi->vsi_num = ctxt->vsi_num;
901 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
902 * @vsi: ptr to the VSI
904 * This should only be called after ice_vsi_alloc() which allocates the
905 * corresponding SW VSI structure and initializes num_queue_pairs for the
906 * newly allocated VSI.
908 * Returns 0 on success or negative on failure
910 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
912 struct ice_pf *pf = vsi->back;
916 dev = ice_pf_to_dev(pf);
917 /* SRIOV doesn't grab irq_tracker entries for each VSI */
918 if (vsi->type == ICE_VSI_VF)
921 if (vsi->base_vector) {
922 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
923 vsi->vsi_num, vsi->base_vector);
927 num_q_vectors = vsi->num_q_vectors;
928 /* reserve slots from OS requested IRQs */
929 vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
931 if (vsi->base_vector < 0) {
933 "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
934 num_q_vectors, vsi->vsi_num, vsi->base_vector);
937 pf->num_avail_sw_msix -= num_q_vectors;
943 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
944 * @vsi: the VSI having rings deallocated
946 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
951 for (i = 0; i < vsi->alloc_txq; i++) {
952 if (vsi->tx_rings[i]) {
953 kfree_rcu(vsi->tx_rings[i], rcu);
954 vsi->tx_rings[i] = NULL;
959 for (i = 0; i < vsi->alloc_rxq; i++) {
960 if (vsi->rx_rings[i]) {
961 kfree_rcu(vsi->rx_rings[i], rcu);
962 vsi->rx_rings[i] = NULL;
969 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
970 * @vsi: VSI which is having rings allocated
972 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
974 struct ice_pf *pf = vsi->back;
978 dev = ice_pf_to_dev(pf);
979 /* Allocate Tx rings */
980 for (i = 0; i < vsi->alloc_txq; i++) {
981 struct ice_ring *ring;
983 /* allocate with kzalloc(), free with kfree_rcu() */
984 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
990 ring->reg_idx = vsi->txq_map[i];
991 ring->ring_active = false;
994 ring->count = vsi->num_tx_desc;
995 vsi->tx_rings[i] = ring;
998 /* Allocate Rx rings */
999 for (i = 0; i < vsi->alloc_rxq; i++) {
1000 struct ice_ring *ring;
1002 /* allocate with kzalloc(), free with kfree_rcu() */
1003 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1008 ring->reg_idx = vsi->rxq_map[i];
1009 ring->ring_active = false;
1011 ring->netdev = vsi->netdev;
1013 ring->count = vsi->num_rx_desc;
1014 vsi->rx_rings[i] = ring;
1020 ice_vsi_clear_rings(vsi);
1025 * ice_vsi_manage_rss_lut - disable/enable RSS
1026 * @vsi: the VSI being changed
1027 * @ena: boolean value indicating if this is an enable or disable request
1029 * In the event of disable request for RSS, this function will zero out RSS
1030 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1033 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1038 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1043 if (vsi->rss_lut_user)
1044 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1046 ice_fill_rss_lut(lut, vsi->rss_table_size,
1050 err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1056 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1057 * @vsi: VSI to be configured
1059 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1061 struct ice_aqc_get_set_rss_keys *key;
1062 struct ice_pf *pf = vsi->back;
1063 enum ice_status status;
1068 dev = ice_pf_to_dev(pf);
1069 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1071 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1075 if (vsi->rss_lut_user)
1076 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1078 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1080 status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1081 vsi->rss_table_size);
1084 dev_err(dev, "set_rss_lut failed, error %d\n", status);
1086 goto ice_vsi_cfg_rss_exit;
1089 key = kzalloc(sizeof(*key), GFP_KERNEL);
1092 goto ice_vsi_cfg_rss_exit;
1095 if (vsi->rss_hkey_user)
1097 (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1098 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1100 netdev_rss_key_fill((void *)key,
1101 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1103 status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1106 dev_err(dev, "set_rss_key failed, error %d\n", status);
1111 ice_vsi_cfg_rss_exit:
1117 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1118 * @vsi: VSI to be configured
1120 * This function will only be called during the VF VSI setup. Upon successful
1121 * completion of package download, this function will configure default RSS
1122 * input sets for VF VSI.
1124 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1126 struct ice_pf *pf = vsi->back;
1127 enum ice_status status;
1130 dev = ice_pf_to_dev(pf);
1131 if (ice_is_safe_mode(pf)) {
1132 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1137 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1139 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1140 vsi->vsi_num, status);
1144 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1145 * @vsi: VSI to be configured
1147 * This function will only be called after successful download package call
1148 * during initialization of PF. Since the downloaded package will erase the
1149 * RSS section, this function will configure RSS input sets for different
1150 * flow types. The last profile added has the highest priority, therefore 2
1151 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1152 * (i.e. IPv4 src/dst TCP src/dst port).
1154 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1156 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1157 struct ice_pf *pf = vsi->back;
1158 struct ice_hw *hw = &pf->hw;
1159 enum ice_status status;
1162 dev = ice_pf_to_dev(pf);
1163 if (ice_is_safe_mode(pf)) {
1164 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1168 /* configure RSS for IPv4 with input set IP src/dst */
1169 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1170 ICE_FLOW_SEG_HDR_IPV4);
1172 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1175 /* configure RSS for IPv6 with input set IPv6 src/dst */
1176 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1177 ICE_FLOW_SEG_HDR_IPV6);
1179 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1182 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1183 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1184 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1186 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1189 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1190 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1191 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1193 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1196 /* configure RSS for sctp4 with input set IP src/dst */
1197 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1198 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1200 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1203 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1204 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1205 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1207 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1210 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1211 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1212 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1214 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1217 /* configure RSS for sctp6 with input set IPv6 src/dst */
1218 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1219 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1221 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1226 * ice_add_mac_to_list - Add a MAC address filter entry to the list
1227 * @vsi: the VSI to be forwarded to
1228 * @add_list: pointer to the list which contains MAC filter entries
1229 * @macaddr: the MAC address to be added.
1231 * Adds MAC address filter entry to the temp list
1233 * Returns 0 on success or ENOMEM on failure.
1235 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1238 struct ice_fltr_list_entry *tmp;
1239 struct ice_pf *pf = vsi->back;
1241 tmp = devm_kzalloc(ice_pf_to_dev(pf), sizeof(*tmp), GFP_ATOMIC);
1245 tmp->fltr_info.flag = ICE_FLTR_TX;
1246 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1247 tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1248 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1249 tmp->fltr_info.vsi_handle = vsi->idx;
1250 ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1252 INIT_LIST_HEAD(&tmp->list_entry);
1253 list_add(&tmp->list_entry, add_list);
1259 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1260 * @vsi: the VSI to be updated
1262 void ice_update_eth_stats(struct ice_vsi *vsi)
1264 struct ice_eth_stats *prev_es, *cur_es;
1265 struct ice_hw *hw = &vsi->back->hw;
1266 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1268 prev_es = &vsi->eth_stats_prev;
1269 cur_es = &vsi->eth_stats;
1271 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1272 &prev_es->rx_bytes, &cur_es->rx_bytes);
1274 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1275 &prev_es->rx_unicast, &cur_es->rx_unicast);
1277 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1278 &prev_es->rx_multicast, &cur_es->rx_multicast);
1280 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1281 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1283 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1284 &prev_es->rx_discards, &cur_es->rx_discards);
1286 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1287 &prev_es->tx_bytes, &cur_es->tx_bytes);
1289 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1290 &prev_es->tx_unicast, &cur_es->tx_unicast);
1292 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1293 &prev_es->tx_multicast, &cur_es->tx_multicast);
1295 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1296 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1298 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1299 &prev_es->tx_errors, &cur_es->tx_errors);
1301 vsi->stat_offsets_loaded = true;
1305 * ice_free_fltr_list - free filter lists helper
1306 * @dev: pointer to the device struct
1307 * @h: pointer to the list head to be freed
1309 * Helper function to free filter lists previously created using
1310 * ice_add_mac_to_list
1312 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1314 struct ice_fltr_list_entry *e, *tmp;
1316 list_for_each_entry_safe(e, tmp, h, list_entry) {
1317 list_del(&e->list_entry);
1323 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1324 * @vsi: the VSI being configured
1325 * @vid: VLAN ID to be added
1327 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1329 struct ice_fltr_list_entry *tmp;
1330 struct ice_pf *pf = vsi->back;
1331 LIST_HEAD(tmp_add_list);
1332 enum ice_status status;
1336 dev = ice_pf_to_dev(pf);
1337 tmp = devm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
1341 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1342 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1343 tmp->fltr_info.flag = ICE_FLTR_TX;
1344 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1345 tmp->fltr_info.vsi_handle = vsi->idx;
1346 tmp->fltr_info.l_data.vlan.vlan_id = vid;
1348 INIT_LIST_HEAD(&tmp->list_entry);
1349 list_add(&tmp->list_entry, &tmp_add_list);
1351 status = ice_add_vlan(&pf->hw, &tmp_add_list);
1354 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1358 ice_free_fltr_list(dev, &tmp_add_list);
1363 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1364 * @vsi: the VSI being configured
1365 * @vid: VLAN ID to be removed
1367 * Returns 0 on success and negative on failure
1369 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1371 struct ice_fltr_list_entry *list;
1372 struct ice_pf *pf = vsi->back;
1373 LIST_HEAD(tmp_add_list);
1374 enum ice_status status;
1378 dev = ice_pf_to_dev(pf);
1379 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1383 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1384 list->fltr_info.vsi_handle = vsi->idx;
1385 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1386 list->fltr_info.l_data.vlan.vlan_id = vid;
1387 list->fltr_info.flag = ICE_FLTR_TX;
1388 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1390 INIT_LIST_HEAD(&list->list_entry);
1391 list_add(&list->list_entry, &tmp_add_list);
1393 status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1394 if (status == ICE_ERR_DOES_NOT_EXIST) {
1396 "Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1397 vid, vsi->vsi_num, status);
1398 } else if (status) {
1400 "Error removing VLAN %d on vsi %i error: %d\n",
1401 vid, vsi->vsi_num, status);
1405 ice_free_fltr_list(dev, &tmp_add_list);
1410 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1413 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1415 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1416 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1417 vsi->rx_buf_len = ICE_RXBUF_2048;
1418 #if (PAGE_SIZE < 8192)
1419 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1420 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1421 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1422 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1425 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1426 #if (PAGE_SIZE < 8192)
1427 vsi->rx_buf_len = ICE_RXBUF_3072;
1429 vsi->rx_buf_len = ICE_RXBUF_2048;
1435 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1436 * @vsi: the VSI being configured
1438 * Return 0 on success and a negative value on error
1439 * Configure the Rx VSI for operation.
1441 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1445 if (vsi->type == ICE_VSI_VF)
1448 ice_vsi_cfg_frame_size(vsi);
1450 /* set up individual rings */
1451 for (i = 0; i < vsi->num_rxq; i++) {
1454 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1456 dev_err(&vsi->back->pdev->dev,
1457 "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1467 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1468 * @vsi: the VSI being configured
1469 * @rings: Tx ring array to be configured
1471 * Return 0 on success and a negative value on error
1472 * Configure the Tx VSI for operation.
1475 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1477 struct ice_aqc_add_tx_qgrp *qg_buf;
1481 qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1485 qg_buf->num_txqs = 1;
1487 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1488 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1499 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1500 * @vsi: the VSI being configured
1502 * Return 0 on success and a negative value on error
1503 * Configure the Tx VSI for operation.
1505 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1507 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1511 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1512 * @vsi: the VSI being configured
1514 * Return 0 on success and a negative value on error
1515 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1517 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1522 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1526 for (i = 0; i < vsi->num_xdp_txq; i++)
1527 vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1533 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1534 * @intrl: interrupt rate limit in usecs
1535 * @gran: interrupt rate limit granularity in usecs
1537 * This function converts a decimal interrupt rate limit in usecs to the format
1538 * expected by firmware.
1540 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1542 u32 val = intrl / gran;
1545 return val | GLINT_RATE_INTRL_ENA_M;
1550 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1551 * @vsi: the VSI being configured
1553 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1556 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1558 struct ice_pf *pf = vsi->back;
1559 struct ice_hw *hw = &pf->hw;
1560 u32 txq = 0, rxq = 0;
1563 for (i = 0; i < vsi->num_q_vectors; i++) {
1564 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1565 u16 reg_idx = q_vector->reg_idx;
1567 ice_cfg_itr(hw, q_vector);
1569 wr32(hw, GLINT_RATE(reg_idx),
1570 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1572 /* Both Transmit Queue Interrupt Cause Control register
1573 * and Receive Queue Interrupt Cause control register
1574 * expects MSIX_INDX field to be the vector index
1575 * within the function space and not the absolute
1576 * vector index across PF or across device.
1577 * For SR-IOV VF VSIs queue vector index always starts
1578 * with 1 since first vector index(0) is used for OICR
1579 * in VF space. Since VMDq and other PF VSIs are within
1580 * the PF function space, use the vector index that is
1581 * tracked for this PF.
1583 for (q = 0; q < q_vector->num_ring_tx; q++) {
1584 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1585 q_vector->tx.itr_idx);
1589 for (q = 0; q < q_vector->num_ring_rx; q++) {
1590 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1591 q_vector->rx.itr_idx);
1598 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1599 * @vsi: the VSI being changed
1601 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1603 struct ice_hw *hw = &vsi->back->hw;
1604 struct ice_vsi_ctx *ctxt;
1605 enum ice_status status;
1608 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1612 /* Here we are configuring the VSI to let the driver add VLAN tags by
1613 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1614 * insertion happens in the Tx hot path, in ice_tx_map.
1616 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1618 /* Preserve existing VLAN strip setting */
1619 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1620 ICE_AQ_VSI_VLAN_EMOD_M);
1622 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1624 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1626 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1627 status, hw->adminq.sq_last_status);
1632 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1639 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1640 * @vsi: the VSI being changed
1641 * @ena: boolean value indicating if this is a enable or disable request
1643 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1645 struct ice_hw *hw = &vsi->back->hw;
1646 struct ice_vsi_ctx *ctxt;
1647 enum ice_status status;
1650 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1654 /* Here we are configuring what the VSI should do with the VLAN tag in
1655 * the Rx packet. We can either leave the tag in the packet or put it in
1656 * the Rx descriptor.
1659 /* Strip VLAN tag from Rx packet and put it in the desc */
1660 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1662 /* Disable stripping. Leave tag in packet */
1663 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1665 /* Allow all packets untagged/tagged */
1666 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1668 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1670 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1672 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1673 ena, status, hw->adminq.sq_last_status);
1678 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1685 * ice_vsi_start_rx_rings - start VSI's Rx rings
1686 * @vsi: the VSI whose rings are to be started
1688 * Returns 0 on success and a negative value on error
1690 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1692 return ice_vsi_ctrl_rx_rings(vsi, true);
1696 * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1699 * Returns 0 on success and a negative value on error
1701 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1703 return ice_vsi_ctrl_rx_rings(vsi, false);
1707 * ice_vsi_stop_tx_rings - Disable Tx rings
1708 * @vsi: the VSI being configured
1709 * @rst_src: reset source
1710 * @rel_vmvf_num: Relative ID of VF/VM
1711 * @rings: Tx ring array to be stopped
1714 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1715 u16 rel_vmvf_num, struct ice_ring **rings)
1719 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1722 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1723 struct ice_txq_meta txq_meta = { };
1726 if (!rings || !rings[q_idx])
1729 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1730 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1731 rings[q_idx], &txq_meta);
1741 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1742 * @vsi: the VSI being configured
1743 * @rst_src: reset source
1744 * @rel_vmvf_num: Relative ID of VF/VM
1747 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1750 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1754 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1755 * @vsi: the VSI being configured
1757 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1759 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1763 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1764 * @vsi: VSI to enable or disable VLAN pruning on
1765 * @ena: set to true to enable VLAN pruning and false to disable it
1766 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1768 * returns 0 if VSI is updated, negative otherwise
1770 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1772 struct ice_vsi_ctx *ctxt;
1780 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1784 ctxt->info = vsi->info;
1787 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1789 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1792 ctxt->info.valid_sections =
1793 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
1795 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1797 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1798 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
1799 pf->hw.adminq.sq_last_status);
1803 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1813 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
1815 struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
1817 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
1818 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
1822 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
1823 * @vsi: VSI to set the q_vectors register index on
1826 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
1830 if (!vsi || !vsi->q_vectors)
1833 ice_for_each_q_vector(vsi, i) {
1834 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1837 dev_err(&vsi->back->pdev->dev,
1838 "Failed to set reg_idx on q_vector %d VSI %d\n",
1843 if (vsi->type == ICE_VSI_VF) {
1844 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
1846 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
1849 q_vector->v_idx + vsi->base_vector;
1856 ice_for_each_q_vector(vsi, i) {
1857 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1860 q_vector->reg_idx = 0;
1867 * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
1868 * @vsi: the VSI being configured
1869 * @add_rule: boolean value to add or remove ethertype filter rule
1872 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
1874 struct ice_fltr_list_entry *list;
1875 struct ice_pf *pf = vsi->back;
1876 LIST_HEAD(tmp_add_list);
1877 enum ice_status status;
1880 dev = ice_pf_to_dev(pf);
1881 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1885 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1886 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1887 list->fltr_info.flag = ICE_FLTR_TX;
1888 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1889 list->fltr_info.vsi_handle = vsi->idx;
1890 list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
1892 INIT_LIST_HEAD(&list->list_entry);
1893 list_add(&list->list_entry, &tmp_add_list);
1896 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1898 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1902 "Failure Adding or Removing Ethertype on VSI %i error: %d\n",
1903 vsi->vsi_num, status);
1905 ice_free_fltr_list(dev, &tmp_add_list);
1909 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
1910 * @vsi: the VSI being configured
1911 * @tx: bool to determine Tx or Rx rule
1912 * @create: bool to determine create or remove Rule
1914 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
1916 struct ice_fltr_list_entry *list;
1917 struct ice_pf *pf = vsi->back;
1918 LIST_HEAD(tmp_add_list);
1919 enum ice_status status;
1922 dev = ice_pf_to_dev(pf);
1923 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1927 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1928 list->fltr_info.vsi_handle = vsi->idx;
1929 list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
1932 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1933 list->fltr_info.flag = ICE_FLTR_TX;
1934 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1936 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1937 list->fltr_info.flag = ICE_FLTR_RX;
1938 list->fltr_info.src_id = ICE_SRC_ID_LPORT;
1941 INIT_LIST_HEAD(&list->list_entry);
1942 list_add(&list->list_entry, &tmp_add_list);
1945 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1947 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1950 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
1951 create ? "adding" : "removing", tx ? "TX" : "RX",
1952 vsi->vsi_num, status);
1954 ice_free_fltr_list(dev, &tmp_add_list);
1958 * ice_vsi_setup - Set up a VSI by a given type
1959 * @pf: board private structure
1960 * @pi: pointer to the port_info instance
1962 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1963 * used only for ICE_VSI_VF VSI type. For other VSI types, should
1964 * fill-in ICE_INVAL_VFID as input.
1966 * This allocates the sw VSI structure and its queue resources.
1968 * Returns pointer to the successfully allocated and configured VSI sw struct on
1969 * success, NULL on failure.
1972 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1973 enum ice_vsi_type type, u16 vf_id)
1975 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1976 struct device *dev = ice_pf_to_dev(pf);
1977 enum ice_status status;
1978 struct ice_vsi *vsi;
1981 if (type == ICE_VSI_VF)
1982 vsi = ice_vsi_alloc(pf, type, vf_id);
1984 vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
1987 dev_err(dev, "could not allocate VSI\n");
1991 vsi->port_info = pi;
1992 vsi->vsw = pf->first_sw;
1993 if (vsi->type == ICE_VSI_PF)
1994 vsi->ethtype = ETH_P_PAUSE;
1996 if (vsi->type == ICE_VSI_VF)
1999 if (ice_vsi_get_qs(vsi)) {
2000 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2005 /* set RSS capabilities */
2006 ice_vsi_set_rss_params(vsi);
2008 /* set TC configuration */
2009 ice_vsi_set_tc_cfg(vsi);
2011 /* create the VSI */
2012 ret = ice_vsi_init(vsi, true);
2016 switch (vsi->type) {
2018 ret = ice_vsi_alloc_q_vectors(vsi);
2020 goto unroll_vsi_init;
2022 ret = ice_vsi_setup_vector_base(vsi);
2024 goto unroll_alloc_q_vector;
2026 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2028 goto unroll_vector_base;
2030 ret = ice_vsi_alloc_rings(vsi);
2032 goto unroll_vector_base;
2034 ice_vsi_map_rings_to_vectors(vsi);
2036 /* Do not exit if configuring RSS had an issue, at least
2037 * receive traffic on first queue. Hence no need to capture
2040 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2041 ice_vsi_cfg_rss_lut_key(vsi);
2042 ice_vsi_set_rss_flow_fld(vsi);
2046 /* VF driver will take care of creating netdev for this type and
2047 * map queues to vectors through Virtchnl, PF driver only
2048 * creates a VSI and corresponding structures for bookkeeping
2051 ret = ice_vsi_alloc_q_vectors(vsi);
2053 goto unroll_vsi_init;
2055 ret = ice_vsi_alloc_rings(vsi);
2057 goto unroll_alloc_q_vector;
2059 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2061 goto unroll_vector_base;
2063 /* Do not exit if configuring RSS had an issue, at least
2064 * receive traffic on first queue. Hence no need to capture
2067 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2068 ice_vsi_cfg_rss_lut_key(vsi);
2069 ice_vsi_set_vf_rss_flow_fld(vsi);
2073 ret = ice_vsi_alloc_rings(vsi);
2075 goto unroll_vsi_init;
2078 /* clean up the resources and exit */
2079 goto unroll_vsi_init;
2082 /* configure VSI nodes based on number of queues and TC's */
2083 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2084 max_txqs[i] = vsi->alloc_txq;
2086 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2089 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2090 vsi->vsi_num, status);
2091 goto unroll_vector_base;
2094 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2095 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2096 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2097 * The rule is added once for PF VSI in order to create appropriate
2098 * recipe, since VSI/VSI list is ignored with drop action...
2099 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2100 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2101 * settings in the HW.
2103 if (!ice_is_safe_mode(pf))
2104 if (vsi->type == ICE_VSI_PF) {
2105 ice_vsi_add_rem_eth_mac(vsi, true);
2107 /* Tx LLDP packets */
2108 ice_cfg_sw_lldp(vsi, true, true);
2114 /* reclaim SW interrupts back to the common pool */
2115 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2116 pf->num_avail_sw_msix += vsi->num_q_vectors;
2117 unroll_alloc_q_vector:
2118 ice_vsi_free_q_vectors(vsi);
2120 ice_vsi_delete(vsi);
2122 ice_vsi_put_qs(vsi);
2129 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2130 * @vsi: the VSI being cleaned up
2132 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2134 struct ice_pf *pf = vsi->back;
2135 struct ice_hw *hw = &pf->hw;
2140 for (i = 0; i < vsi->num_q_vectors; i++) {
2141 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2142 u16 reg_idx = q_vector->reg_idx;
2144 wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2145 wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2146 for (q = 0; q < q_vector->num_ring_tx; q++) {
2147 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2148 if (ice_is_xdp_ena_vsi(vsi)) {
2149 u32 xdp_txq = txq + vsi->num_xdp_txq;
2151 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2156 for (q = 0; q < q_vector->num_ring_rx; q++) {
2157 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2166 * ice_vsi_free_irq - Free the IRQ association with the OS
2167 * @vsi: the VSI being configured
2169 void ice_vsi_free_irq(struct ice_vsi *vsi)
2171 struct ice_pf *pf = vsi->back;
2172 int base = vsi->base_vector;
2175 if (!vsi->q_vectors || !vsi->irqs_ready)
2178 ice_vsi_release_msix(vsi);
2179 if (vsi->type == ICE_VSI_VF)
2182 vsi->irqs_ready = false;
2183 ice_for_each_q_vector(vsi, i) {
2184 u16 vector = i + base;
2187 irq_num = pf->msix_entries[vector].vector;
2189 /* free only the irqs that were actually requested */
2190 if (!vsi->q_vectors[i] ||
2191 !(vsi->q_vectors[i]->num_ring_tx ||
2192 vsi->q_vectors[i]->num_ring_rx))
2195 /* clear the affinity notifier in the IRQ descriptor */
2196 irq_set_affinity_notifier(irq_num, NULL);
2198 /* clear the affinity_mask in the IRQ descriptor */
2199 irq_set_affinity_hint(irq_num, NULL);
2200 synchronize_irq(irq_num);
2201 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2206 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2207 * @vsi: the VSI having resources freed
2209 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2216 ice_for_each_txq(vsi, i)
2217 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2218 ice_free_tx_ring(vsi->tx_rings[i]);
2222 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2223 * @vsi: the VSI having resources freed
2225 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2232 ice_for_each_rxq(vsi, i)
2233 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2234 ice_free_rx_ring(vsi->rx_rings[i]);
2238 * ice_vsi_close - Shut down a VSI
2239 * @vsi: the VSI being shut down
2241 void ice_vsi_close(struct ice_vsi *vsi)
2243 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2246 ice_vsi_free_irq(vsi);
2247 ice_vsi_free_tx_rings(vsi);
2248 ice_vsi_free_rx_rings(vsi);
2252 * ice_ena_vsi - resume a VSI
2253 * @vsi: the VSI being resume
2254 * @locked: is the rtnl_lock already held
2256 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2260 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2263 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2265 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2266 if (netif_running(vsi->netdev)) {
2270 err = ice_open(vsi->netdev);
2281 * ice_dis_vsi - pause a VSI
2282 * @vsi: the VSI being paused
2283 * @locked: is the rtnl_lock already held
2285 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2287 if (test_bit(__ICE_DOWN, vsi->state))
2290 set_bit(__ICE_NEEDS_RESTART, vsi->state);
2292 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2293 if (netif_running(vsi->netdev)) {
2297 ice_stop(vsi->netdev);
2308 * ice_free_res - free a block of resources
2309 * @res: pointer to the resource
2310 * @index: starting index previously returned by ice_get_res
2311 * @id: identifier to track owner
2313 * Returns number of resources freed
2315 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2320 if (!res || index >= res->end)
2323 id |= ICE_RES_VALID_BIT;
2324 for (i = index; i < res->end && res->list[i] == id; i++) {
2333 * ice_search_res - Search the tracker for a block of resources
2334 * @res: pointer to the resource
2335 * @needed: size of the block needed
2336 * @id: identifier to track owner
2338 * Returns the base item index of the block, or -ENOMEM for error
2340 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2342 int start = 0, end = 0;
2344 if (needed > res->end)
2347 id |= ICE_RES_VALID_BIT;
2350 /* skip already allocated entries */
2351 if (res->list[end++] & ICE_RES_VALID_BIT) {
2353 if ((start + needed) > res->end)
2357 if (end == (start + needed)) {
2360 /* there was enough, so assign it to the requestor */
2362 res->list[i++] = id;
2366 } while (end < res->end);
2372 * ice_get_res - get a block of resources
2373 * @pf: board private structure
2374 * @res: pointer to the resource
2375 * @needed: size of the block needed
2376 * @id: identifier to track owner
2378 * Returns the base item index of the block, or negative for error
2381 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2386 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2387 dev_err(ice_pf_to_dev(pf),
2388 "param err: needed=%d, num_entries = %d id=0x%04x\n",
2389 needed, res->num_entries, id);
2393 return ice_search_res(res, needed, id);
2397 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2398 * @vsi: the VSI being un-configured
2400 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2402 int base = vsi->base_vector;
2403 struct ice_pf *pf = vsi->back;
2404 struct ice_hw *hw = &pf->hw;
2408 /* disable interrupt causation from each queue */
2409 if (vsi->tx_rings) {
2410 ice_for_each_txq(vsi, i) {
2411 if (vsi->tx_rings[i]) {
2414 reg = vsi->tx_rings[i]->reg_idx;
2415 val = rd32(hw, QINT_TQCTL(reg));
2416 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2417 wr32(hw, QINT_TQCTL(reg), val);
2422 if (vsi->rx_rings) {
2423 ice_for_each_rxq(vsi, i) {
2424 if (vsi->rx_rings[i]) {
2427 reg = vsi->rx_rings[i]->reg_idx;
2428 val = rd32(hw, QINT_RQCTL(reg));
2429 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2430 wr32(hw, QINT_RQCTL(reg), val);
2435 /* disable each interrupt */
2436 ice_for_each_q_vector(vsi, i) {
2437 if (!vsi->q_vectors[i])
2439 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2444 /* don't call synchronize_irq() for VF's from the host */
2445 if (vsi->type == ICE_VSI_VF)
2448 ice_for_each_q_vector(vsi, i)
2449 synchronize_irq(pf->msix_entries[i + base].vector);
2453 * ice_napi_del - Remove NAPI handler for the VSI
2454 * @vsi: VSI for which NAPI handler is to be removed
2456 void ice_napi_del(struct ice_vsi *vsi)
2463 ice_for_each_q_vector(vsi, v_idx)
2464 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2468 * ice_vsi_release - Delete a VSI and free its resources
2469 * @vsi: the VSI being removed
2471 * Returns 0 on success or < 0 on error
2473 int ice_vsi_release(struct ice_vsi *vsi)
2481 /* do not unregister while driver is in the reset recovery pending
2482 * state. Since reset/rebuild happens through PF service task workqueue,
2483 * it's not a good idea to unregister netdev that is associated to the
2484 * PF that is running the work queue items currently. This is done to
2485 * avoid check_flush_dependency() warning on this wq
2487 if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2488 unregister_netdev(vsi->netdev);
2490 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2493 /* Disable VSI and free resources */
2494 if (vsi->type != ICE_VSI_LB)
2495 ice_vsi_dis_irq(vsi);
2498 /* SR-IOV determines needed MSIX resources all at once instead of per
2499 * VSI since when VFs are spawned we know how many VFs there are and how
2500 * many interrupts each VF needs. SR-IOV MSIX resources are also
2501 * cleared in the same manner.
2503 if (vsi->type != ICE_VSI_VF) {
2504 /* reclaim SW interrupts back to the common pool */
2505 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2506 pf->num_avail_sw_msix += vsi->num_q_vectors;
2509 if (!ice_is_safe_mode(pf)) {
2510 if (vsi->type == ICE_VSI_PF) {
2511 ice_vsi_add_rem_eth_mac(vsi, false);
2512 ice_cfg_sw_lldp(vsi, true, false);
2513 /* The Rx rule will only exist to remove if the LLDP FW
2514 * engine is currently stopped
2516 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2517 ice_cfg_sw_lldp(vsi, false, false);
2521 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2522 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2523 ice_vsi_delete(vsi);
2524 ice_vsi_free_q_vectors(vsi);
2526 /* make sure unregister_netdev() was called by checking __ICE_DOWN */
2527 if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2528 free_netdev(vsi->netdev);
2532 ice_vsi_clear_rings(vsi);
2534 ice_vsi_put_qs(vsi);
2536 /* retain SW VSI data structure since it is needed to unregister and
2537 * free VSI netdev when PF is not in reset recovery pending state,\
2538 * for ex: during rmmod.
2540 if (!ice_is_reset_in_progress(pf->state))
2547 * ice_vsi_rebuild_update_coalesce - set coalesce for a q_vector
2548 * @q_vector: pointer to q_vector which is being updated
2549 * @coalesce: pointer to array of struct with stored coalesce
2551 * Set coalesce param in q_vector and update these parameters in HW.
2554 ice_vsi_rebuild_update_coalesce(struct ice_q_vector *q_vector,
2555 struct ice_coalesce_stored *coalesce)
2557 struct ice_ring_container *rx_rc = &q_vector->rx;
2558 struct ice_ring_container *tx_rc = &q_vector->tx;
2559 struct ice_hw *hw = &q_vector->vsi->back->hw;
2561 tx_rc->itr_setting = coalesce->itr_tx;
2562 rx_rc->itr_setting = coalesce->itr_rx;
2564 /* dynamic ITR values will be updated during Tx/Rx */
2565 if (!ITR_IS_DYNAMIC(tx_rc->itr_setting))
2566 wr32(hw, GLINT_ITR(tx_rc->itr_idx, q_vector->reg_idx),
2567 ITR_REG_ALIGN(tx_rc->itr_setting) >>
2569 if (!ITR_IS_DYNAMIC(rx_rc->itr_setting))
2570 wr32(hw, GLINT_ITR(rx_rc->itr_idx, q_vector->reg_idx),
2571 ITR_REG_ALIGN(rx_rc->itr_setting) >>
2574 q_vector->intrl = coalesce->intrl;
2575 wr32(hw, GLINT_RATE(q_vector->reg_idx),
2576 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
2580 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2581 * @vsi: VSI connected with q_vectors
2582 * @coalesce: array of struct with stored coalesce
2584 * Returns array size.
2587 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2588 struct ice_coalesce_stored *coalesce)
2592 ice_for_each_q_vector(vsi, i) {
2593 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2595 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2596 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2597 coalesce[i].intrl = q_vector->intrl;
2600 return vsi->num_q_vectors;
2604 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2605 * @vsi: VSI connected with q_vectors
2606 * @coalesce: pointer to array of struct with stored coalesce
2607 * @size: size of coalesce array
2609 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2610 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2614 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2615 struct ice_coalesce_stored *coalesce, int size)
2619 if ((size && !coalesce) || !vsi)
2622 for (i = 0; i < size && i < vsi->num_q_vectors; i++)
2623 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2626 for (; i < vsi->num_q_vectors; i++) {
2627 struct ice_coalesce_stored coalesce_dflt = {
2628 .itr_tx = ICE_DFLT_TX_ITR,
2629 .itr_rx = ICE_DFLT_RX_ITR,
2632 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2638 * ice_vsi_rebuild - Rebuild VSI after reset
2639 * @vsi: VSI to be rebuild
2640 * @init_vsi: is this an initialization or a reconfigure of the VSI
2642 * Returns 0 on success and negative value on failure
2644 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2646 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2647 struct ice_coalesce_stored *coalesce;
2648 int prev_num_q_vectors = 0;
2649 struct ice_vf *vf = NULL;
2650 enum ice_status status;
2658 if (vsi->type == ICE_VSI_VF)
2659 vf = &pf->vf[vsi->vf_id];
2661 coalesce = kcalloc(vsi->num_q_vectors,
2662 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
2664 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi,
2666 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2667 ice_vsi_free_q_vectors(vsi);
2669 /* SR-IOV determines needed MSIX resources all at once instead of per
2670 * VSI since when VFs are spawned we know how many VFs there are and how
2671 * many interrupts each VF needs. SR-IOV MSIX resources are also
2672 * cleared in the same manner.
2674 if (vsi->type != ICE_VSI_VF) {
2675 /* reclaim SW interrupts back to the common pool */
2676 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2677 pf->num_avail_sw_msix += vsi->num_q_vectors;
2678 vsi->base_vector = 0;
2681 if (ice_is_xdp_ena_vsi(vsi))
2682 /* return value check can be skipped here, it always returns
2683 * 0 if reset is in progress
2685 ice_destroy_xdp_rings(vsi);
2686 ice_vsi_put_qs(vsi);
2687 ice_vsi_clear_rings(vsi);
2688 ice_vsi_free_arrays(vsi);
2689 if (vsi->type == ICE_VSI_VF)
2690 ice_vsi_set_num_qs(vsi, vf->vf_id);
2692 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2694 ret = ice_vsi_alloc_arrays(vsi);
2698 ice_vsi_get_qs(vsi);
2699 ice_vsi_set_tc_cfg(vsi);
2701 /* Initialize VSI struct elements and create VSI in FW */
2702 ret = ice_vsi_init(vsi, init_vsi);
2706 switch (vsi->type) {
2708 ret = ice_vsi_alloc_q_vectors(vsi);
2712 ret = ice_vsi_setup_vector_base(vsi);
2716 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2720 ret = ice_vsi_alloc_rings(vsi);
2724 ice_vsi_map_rings_to_vectors(vsi);
2725 if (ice_is_xdp_ena_vsi(vsi)) {
2726 vsi->num_xdp_txq = vsi->alloc_txq;
2727 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2731 /* Do not exit if configuring RSS had an issue, at least
2732 * receive traffic on first queue. Hence no need to capture
2735 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2736 ice_vsi_cfg_rss_lut_key(vsi);
2739 ret = ice_vsi_alloc_q_vectors(vsi);
2743 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2747 ret = ice_vsi_alloc_rings(vsi);
2756 /* configure VSI nodes based on number of queues and TC's */
2757 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2758 max_txqs[i] = vsi->alloc_txq;
2760 if (ice_is_xdp_ena_vsi(vsi))
2761 max_txqs[i] += vsi->num_xdp_txq;
2764 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2767 dev_err(ice_pf_to_dev(pf),
2768 "VSI %d failed lan queue config, error %d\n",
2769 vsi->vsi_num, status);
2774 return ice_schedule_reset(pf, ICE_RESET_PFR);
2777 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
2783 ice_vsi_free_q_vectors(vsi);
2786 vsi->current_netdev_flags = 0;
2787 unregister_netdev(vsi->netdev);
2788 free_netdev(vsi->netdev);
2793 set_bit(__ICE_RESET_FAILED, pf->state);
2799 * ice_is_reset_in_progress - check for a reset in progress
2800 * @state: PF state field
2802 bool ice_is_reset_in_progress(unsigned long *state)
2804 return test_bit(__ICE_RESET_OICR_RECV, state) ||
2805 test_bit(__ICE_DCBNL_DEVRESET, state) ||
2806 test_bit(__ICE_PFR_REQ, state) ||
2807 test_bit(__ICE_CORER_REQ, state) ||
2808 test_bit(__ICE_GLOBR_REQ, state);
2813 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2814 * @vsi: VSI being configured
2815 * @ctx: the context buffer returned from AQ VSI update command
2817 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2819 vsi->info.mapping_flags = ctx->info.mapping_flags;
2820 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2821 sizeof(vsi->info.q_mapping));
2822 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2823 sizeof(vsi->info.tc_mapping));
2827 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2828 * @vsi: VSI to be configured
2829 * @ena_tc: TC bitmap
2831 * VSI queues expected to be quiesced before calling this function
2833 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2835 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2836 struct ice_vsi_ctx *ctx;
2837 struct ice_pf *pf = vsi->back;
2838 enum ice_status status;
2843 dev = ice_pf_to_dev(pf);
2845 ice_for_each_traffic_class(i) {
2846 /* build bitmap of enabled TCs */
2847 if (ena_tc & BIT(i))
2849 /* populate max_txqs per TC */
2850 max_txqs[i] = vsi->alloc_txq;
2853 vsi->tc_cfg.ena_tc = ena_tc;
2854 vsi->tc_cfg.numtc = num_tc;
2856 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2861 ctx->info = vsi->info;
2863 ice_vsi_setup_q_map(vsi, ctx);
2865 /* must to indicate which section of VSI context are being modified */
2866 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2867 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2869 dev_info(dev, "Failed VSI Update\n");
2874 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2878 dev_err(dev, "VSI %d failed TC config, error %d\n",
2879 vsi->vsi_num, status);
2883 ice_vsi_update_q_map(vsi, ctx);
2884 vsi->info.valid_sections = 0;
2886 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2891 #endif /* CONFIG_DCB */
2894 * ice_nvm_version_str - format the NVM version strings
2895 * @hw: ptr to the hardware info
2897 char *ice_nvm_version_str(struct ice_hw *hw)
2899 u8 oem_ver, oem_patch, ver_hi, ver_lo;
2900 static char buf[ICE_NVM_VER_LEN];
2903 ice_get_nvm_version(hw, &oem_ver, &oem_build, &oem_patch, &ver_hi,
2906 snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d", ver_hi, ver_lo,
2907 hw->nvm.eetrack, oem_ver, oem_build, oem_patch);
2913 * ice_update_ring_stats - Update ring statistics
2914 * @ring: ring to update
2915 * @cont: used to increment per-vector counters
2916 * @pkts: number of processed packets
2917 * @bytes: number of processed bytes
2919 * This function assumes that caller has acquired a u64_stats_sync lock.
2922 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2923 u64 pkts, u64 bytes)
2925 ring->stats.bytes += bytes;
2926 ring->stats.pkts += pkts;
2927 cont->total_bytes += bytes;
2928 cont->total_pkts += pkts;
2932 * ice_update_tx_ring_stats - Update Tx ring specific counters
2933 * @tx_ring: ring to update
2934 * @pkts: number of processed packets
2935 * @bytes: number of processed bytes
2937 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
2939 u64_stats_update_begin(&tx_ring->syncp);
2940 ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
2941 u64_stats_update_end(&tx_ring->syncp);
2945 * ice_update_rx_ring_stats - Update Rx ring specific counters
2946 * @rx_ring: ring to update
2947 * @pkts: number of processed packets
2948 * @bytes: number of processed bytes
2950 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
2952 u64_stats_update_begin(&rx_ring->syncp);
2953 ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
2954 u64_stats_update_end(&rx_ring->syncp);
2958 * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
2959 * @vsi: the VSI being configured MAC filter
2960 * @macaddr: the MAC address to be added.
2961 * @set: Add or delete a MAC filter
2963 * Adds or removes MAC address filter entry for VF VSI
2966 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
2968 LIST_HEAD(tmp_add_list);
2969 enum ice_status status;
2971 /* Update MAC filter list to be added or removed for a VSI */
2972 if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
2973 status = ICE_ERR_NO_MEMORY;
2974 goto cfg_mac_fltr_exit;
2978 status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
2980 status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
2983 ice_free_fltr_list(&vsi->back->pdev->dev, &tmp_add_list);
2988 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
2989 * @sw: switch to check if its default forwarding VSI is free
2991 * Return true if the default forwarding VSI is already being used, else returns
2992 * false signalling that it's available to use.
2994 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
2996 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3000 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3001 * @sw: switch for the default forwarding VSI to compare against
3002 * @vsi: VSI to compare against default forwarding VSI
3004 * If this VSI passed in is the default forwarding VSI then return true, else
3007 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3009 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3013 * ice_set_dflt_vsi - set the default forwarding VSI
3014 * @sw: switch used to assign the default forwarding VSI
3015 * @vsi: VSI getting set as the default forwarding VSI on the switch
3017 * If the VSI passed in is already the default VSI and it's enabled just return
3020 * If there is already a default VSI on the switch and it's enabled then return
3021 * -EEXIST since there can only be one default VSI per switch.
3023 * Otherwise try to set the VSI passed in as the switch's default VSI and
3024 * return the result.
3026 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3028 enum ice_status status;
3034 dev = ice_pf_to_dev(vsi->back);
3036 /* the VSI passed in is already the default VSI */
3037 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3038 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3043 /* another VSI is already the default VSI for this switch */
3044 if (ice_is_dflt_vsi_in_use(sw)) {
3046 "Default forwarding VSI %d already in use, disable it and try again\n",
3047 sw->dflt_vsi->vsi_num);
3051 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3054 "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3055 vsi->vsi_num, status);
3060 sw->dflt_vsi_ena = true;
3066 * ice_clear_dflt_vsi - clear the default forwarding VSI
3067 * @sw: switch used to clear the default VSI
3069 * If the switch has no default VSI or it's not enabled then return error.
3071 * Otherwise try to clear the default VSI and return the result.
3073 int ice_clear_dflt_vsi(struct ice_sw *sw)
3075 struct ice_vsi *dflt_vsi;
3076 enum ice_status status;
3082 dev = ice_pf_to_dev(sw->pf);
3084 dflt_vsi = sw->dflt_vsi;
3086 /* there is no default VSI configured */
3087 if (!ice_is_dflt_vsi_in_use(sw))
3090 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3094 "Failed to clear the default forwarding VSI %d, error %d\n",
3095 dflt_vsi->vsi_num, status);
3099 sw->dflt_vsi = NULL;
3100 sw->dflt_vsi_ena = false;