1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
7 #include "ice_dcb_lib.h"
10 * ice_vsi_type_str - maps VSI type enum to string equivalents
11 * @type: VSI type enum
13 const char *ice_vsi_type_str(enum ice_vsi_type type)
28 * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
29 * @vsi: the VSI being configured
30 * @ena: start or stop the Rx rings
32 static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
36 for (i = 0; i < vsi->num_rxq; i++) {
37 ret = ice_vsi_ctrl_rx_ring(vsi, ena, i);
46 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
49 * On error: returns error code (negative)
50 * On success: returns 0
52 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
54 struct ice_pf *pf = vsi->back;
57 dev = ice_pf_to_dev(pf);
59 /* allocate memory for both Tx and Rx ring pointers */
60 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
61 sizeof(*vsi->tx_rings), GFP_KERNEL);
65 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
66 sizeof(*vsi->rx_rings), GFP_KERNEL);
70 /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
71 vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
72 sizeof(*vsi->txq_map), GFP_KERNEL);
77 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
78 sizeof(*vsi->rxq_map), GFP_KERNEL);
82 /* There is no need to allocate q_vectors for a loopback VSI. */
83 if (vsi->type == ICE_VSI_LB)
86 /* allocate memory for q_vector pointers */
87 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
88 sizeof(*vsi->q_vectors), GFP_KERNEL);
95 devm_kfree(dev, vsi->rxq_map);
97 devm_kfree(dev, vsi->txq_map);
99 devm_kfree(dev, vsi->rx_rings);
101 devm_kfree(dev, vsi->tx_rings);
106 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
107 * @vsi: the VSI being configured
109 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
115 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
116 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
119 dev_dbg(&vsi->back->pdev->dev,
120 "Not setting number of Tx/Rx descriptors for VSI type %d\n",
127 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
128 * @vsi: the VSI being configured
129 * @vf_id: ID of the VF being configured
131 * Return 0 on success and a negative value on error
133 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
135 struct ice_pf *pf = vsi->back;
136 struct ice_vf *vf = NULL;
138 if (vsi->type == ICE_VSI_VF)
143 vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
146 vsi->alloc_txq = vsi->req_txq;
147 vsi->num_txq = vsi->req_txq;
150 pf->num_lan_tx = vsi->alloc_txq;
152 /* only 1 Rx queue unless RSS is enabled */
153 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
156 vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
159 vsi->alloc_rxq = vsi->req_rxq;
160 vsi->num_rxq = vsi->req_rxq;
164 pf->num_lan_rx = vsi->alloc_rxq;
166 vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
169 vf = &pf->vf[vsi->vf_id];
170 vsi->alloc_txq = vf->num_vf_qs;
171 vsi->alloc_rxq = vf->num_vf_qs;
172 /* pf->num_vf_msix includes (VF miscellaneous vector +
173 * data queue interrupts). Since vsi->num_q_vectors is number
174 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
175 * original vector count
177 vsi->num_q_vectors = pf->num_vf_msix - ICE_NONQ_VECS_VF;
184 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
188 ice_vsi_set_num_desc(vsi);
192 * ice_get_free_slot - get the next non-NULL location index in array
193 * @array: array to search
194 * @size: size of the array
195 * @curr: last known occupied index to be used as a search hint
197 * void * is being used to keep the functionality generic. This lets us use this
198 * function on any array of pointers.
200 static int ice_get_free_slot(void *array, int size, int curr)
202 int **tmp_array = (int **)array;
205 if (curr < (size - 1) && !tmp_array[curr + 1]) {
210 while ((i < size) && (tmp_array[i]))
221 * ice_vsi_delete - delete a VSI from the switch
222 * @vsi: pointer to VSI being removed
224 void ice_vsi_delete(struct ice_vsi *vsi)
226 struct ice_pf *pf = vsi->back;
227 struct ice_vsi_ctx *ctxt;
228 enum ice_status status;
230 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
234 if (vsi->type == ICE_VSI_VF)
235 ctxt->vf_num = vsi->vf_id;
236 ctxt->vsi_num = vsi->vsi_num;
238 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
240 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
242 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
243 vsi->vsi_num, status);
249 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
250 * @vsi: pointer to VSI being cleared
252 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
254 struct ice_pf *pf = vsi->back;
257 dev = ice_pf_to_dev(pf);
259 /* free the ring and vector containers */
260 if (vsi->q_vectors) {
261 devm_kfree(dev, vsi->q_vectors);
262 vsi->q_vectors = NULL;
265 devm_kfree(dev, vsi->tx_rings);
266 vsi->tx_rings = NULL;
269 devm_kfree(dev, vsi->rx_rings);
270 vsi->rx_rings = NULL;
273 devm_kfree(dev, vsi->txq_map);
277 devm_kfree(dev, vsi->rxq_map);
283 * ice_vsi_clear - clean up and deallocate the provided VSI
284 * @vsi: pointer to VSI being cleared
286 * This deallocates the VSI's queue resources, removes it from the PF's
287 * VSI array if necessary, and deallocates the VSI
289 * Returns 0 on success, negative on failure
291 int ice_vsi_clear(struct ice_vsi *vsi)
293 struct ice_pf *pf = NULL;
303 dev = ice_pf_to_dev(pf);
305 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
306 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
310 mutex_lock(&pf->sw_mutex);
311 /* updates the PF for this cleared VSI */
313 pf->vsi[vsi->idx] = NULL;
314 if (vsi->idx < pf->next_vsi)
315 pf->next_vsi = vsi->idx;
317 ice_vsi_free_arrays(vsi);
318 mutex_unlock(&pf->sw_mutex);
319 devm_kfree(dev, vsi);
325 * ice_msix_clean_rings - MSIX mode Interrupt Handler
326 * @irq: interrupt number
327 * @data: pointer to a q_vector
329 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
331 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
333 if (!q_vector->tx.ring && !q_vector->rx.ring)
336 napi_schedule(&q_vector->napi);
342 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
343 * @pf: board private structure
345 * @vf_id: ID of the VF being configured
347 * returns a pointer to a VSI on success, NULL on failure.
349 static struct ice_vsi *
350 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type, u16 vf_id)
352 struct device *dev = ice_pf_to_dev(pf);
353 struct ice_vsi *vsi = NULL;
355 /* Need to protect the allocation of the VSIs at the PF level */
356 mutex_lock(&pf->sw_mutex);
358 /* If we have already allocated our maximum number of VSIs,
359 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
360 * is available to be populated
362 if (pf->next_vsi == ICE_NO_VSI) {
363 dev_dbg(dev, "out of VSI slots!\n");
367 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
373 set_bit(__ICE_DOWN, vsi->state);
375 vsi->idx = pf->next_vsi;
377 if (type == ICE_VSI_VF)
378 ice_vsi_set_num_qs(vsi, vf_id);
380 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
384 if (ice_vsi_alloc_arrays(vsi))
387 /* Setup default MSIX irq handler for VSI */
388 vsi->irq_handler = ice_msix_clean_rings;
391 if (ice_vsi_alloc_arrays(vsi))
395 if (ice_vsi_alloc_arrays(vsi))
399 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
403 /* fill VSI slot in the PF struct */
404 pf->vsi[pf->next_vsi] = vsi;
406 /* prepare pf->next_vsi for next use */
407 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
412 devm_kfree(dev, vsi);
415 mutex_unlock(&pf->sw_mutex);
420 * ice_vsi_get_qs - Assign queues from PF to VSI
421 * @vsi: the VSI to assign queues to
423 * Returns 0 on success and a negative value on error
425 static int ice_vsi_get_qs(struct ice_vsi *vsi)
427 struct ice_pf *pf = vsi->back;
428 struct ice_qs_cfg tx_qs_cfg = {
429 .qs_mutex = &pf->avail_q_mutex,
430 .pf_map = pf->avail_txqs,
431 .pf_map_size = pf->max_pf_txqs,
432 .q_count = vsi->alloc_txq,
433 .scatter_count = ICE_MAX_SCATTER_TXQS,
434 .vsi_map = vsi->txq_map,
436 .mapping_mode = vsi->tx_mapping_mode
438 struct ice_qs_cfg rx_qs_cfg = {
439 .qs_mutex = &pf->avail_q_mutex,
440 .pf_map = pf->avail_rxqs,
441 .pf_map_size = pf->max_pf_rxqs,
442 .q_count = vsi->alloc_rxq,
443 .scatter_count = ICE_MAX_SCATTER_RXQS,
444 .vsi_map = vsi->rxq_map,
446 .mapping_mode = vsi->rx_mapping_mode
450 vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
451 vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
453 ret = __ice_vsi_get_qs(&tx_qs_cfg);
455 ret = __ice_vsi_get_qs(&rx_qs_cfg);
461 * ice_vsi_put_qs - Release queues from VSI to PF
462 * @vsi: the VSI that is going to release queues
464 void ice_vsi_put_qs(struct ice_vsi *vsi)
466 struct ice_pf *pf = vsi->back;
469 mutex_lock(&pf->avail_q_mutex);
471 for (i = 0; i < vsi->alloc_txq; i++) {
472 clear_bit(vsi->txq_map[i], pf->avail_txqs);
473 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
476 for (i = 0; i < vsi->alloc_rxq; i++) {
477 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
478 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
481 mutex_unlock(&pf->avail_q_mutex);
486 * @pf: pointer to the PF struct
488 * returns true if driver is in safe mode, false otherwise
490 bool ice_is_safe_mode(struct ice_pf *pf)
492 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
496 * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
497 * @vsi: the VSI being removed
499 static void ice_rss_clean(struct ice_vsi *vsi)
501 struct ice_pf *pf = vsi->back;
504 dev = ice_pf_to_dev(pf);
506 if (vsi->rss_hkey_user)
507 devm_kfree(dev, vsi->rss_hkey_user);
508 if (vsi->rss_lut_user)
509 devm_kfree(dev, vsi->rss_lut_user);
513 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
514 * @vsi: the VSI being configured
516 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
518 struct ice_hw_common_caps *cap;
519 struct ice_pf *pf = vsi->back;
521 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
526 cap = &pf->hw.func_caps.common_cap;
529 /* PF VSI will inherit RSS instance of PF */
530 vsi->rss_table_size = cap->rss_table_size;
531 vsi->rss_size = min_t(int, num_online_cpus(),
532 BIT(cap->rss_table_entry_width));
533 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
536 /* VF VSI will gets a small RSS table
537 * For VSI_LUT, LUT size should be set to 64 bytes
539 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
540 vsi->rss_size = min_t(int, num_online_cpus(),
541 BIT(cap->rss_table_entry_width));
542 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
547 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n",
554 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
555 * @ctxt: the VSI context being set
557 * This initializes a default VSI context for all sections except the Queues.
559 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
563 memset(&ctxt->info, 0, sizeof(ctxt->info));
564 /* VSI's should be allocated from shared pool */
565 ctxt->alloc_from_pool = true;
566 /* Src pruning enabled by default */
567 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
568 /* Traffic from VSI can be sent to LAN */
569 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
570 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
571 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
572 * packets untagged/tagged.
574 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
575 ICE_AQ_VSI_VLAN_MODE_M) >>
576 ICE_AQ_VSI_VLAN_MODE_S);
577 /* Have 1:1 UP mapping for both ingress/egress tables */
578 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
579 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
580 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
581 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
582 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
583 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
584 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
585 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
586 ctxt->info.ingress_table = cpu_to_le32(table);
587 ctxt->info.egress_table = cpu_to_le32(table);
588 /* Have 1:1 UP mapping for outer to inner UP table */
589 ctxt->info.outer_up_table = cpu_to_le32(table);
590 /* No Outer tag support outer_tag_flags remains to zero */
594 * ice_vsi_setup_q_map - Setup a VSI queue map
595 * @vsi: the VSI being configured
596 * @ctxt: VSI context structure
598 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
600 u16 offset = 0, qmap = 0, tx_count = 0;
601 u16 qcount_tx = vsi->alloc_txq;
602 u16 qcount_rx = vsi->alloc_rxq;
603 u16 tx_numq_tc, rx_numq_tc;
604 u16 pow = 0, max_rss = 0;
605 bool ena_tc0 = false;
609 /* at least TC0 should be enabled by default */
610 if (vsi->tc_cfg.numtc) {
611 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
619 vsi->tc_cfg.ena_tc |= 1;
622 rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
625 tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
629 /* TC mapping is a function of the number of Rx queues assigned to the
630 * VSI for each traffic class and the offset of these queues.
631 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
632 * queues allocated to TC0. No:of queues is a power-of-2.
634 * If TC is not enabled, the queue offset is set to 0, and allocate one
635 * queue, this way, traffic for the given TC will be sent to the default
638 * Setup number and offset of Rx queues for all TCs for the VSI
641 qcount_rx = rx_numq_tc;
643 /* qcount will change if RSS is enabled */
644 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
645 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
646 if (vsi->type == ICE_VSI_PF)
647 max_rss = ICE_MAX_LG_RSS_QS;
649 max_rss = ICE_MAX_SMALL_RSS_QS;
650 qcount_rx = min_t(int, rx_numq_tc, max_rss);
652 qcount_rx = min_t(int, qcount_rx,
657 /* find the (rounded up) power-of-2 of qcount */
658 pow = order_base_2(qcount_rx);
660 ice_for_each_traffic_class(i) {
661 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
662 /* TC is not enabled */
663 vsi->tc_cfg.tc_info[i].qoffset = 0;
664 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
665 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
666 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
667 ctxt->info.tc_mapping[i] = 0;
672 vsi->tc_cfg.tc_info[i].qoffset = offset;
673 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
674 vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
675 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
677 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
678 ICE_AQ_VSI_TC_Q_OFFSET_M) |
679 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
680 ICE_AQ_VSI_TC_Q_NUM_M);
682 tx_count += tx_numq_tc;
683 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
686 /* if offset is non-zero, means it is calculated correctly based on
687 * enabled TCs for a given VSI otherwise qcount_rx will always
688 * be correct and non-zero because it is based off - VSI's
689 * allocated Rx queues which is at least 1 (hence qcount_tx will be
693 vsi->num_rxq = offset;
695 vsi->num_rxq = qcount_rx;
697 vsi->num_txq = tx_count;
699 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
700 dev_dbg(&vsi->back->pdev->dev, "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
701 /* since there is a chance that num_rxq could have been changed
702 * in the above for loop, make num_txq equal to num_rxq.
704 vsi->num_txq = vsi->num_rxq;
707 /* Rx queue mapping */
708 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
709 /* q_mapping buffer holds the info for the first queue allocated for
710 * this VSI in the PF space and also the number of queues associated
713 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
714 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
718 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
719 * @ctxt: the VSI context being set
720 * @vsi: the VSI being configured
722 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
724 u8 lut_type, hash_type;
729 dev = ice_pf_to_dev(pf);
733 /* PF VSI will inherit RSS instance of PF */
734 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
735 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
738 /* VF VSI will gets a small RSS table which is a VSI LUT type */
739 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
740 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
743 dev_dbg(dev, "Unsupported VSI type %s\n",
744 ice_vsi_type_str(vsi->type));
747 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
751 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
752 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
753 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
754 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
758 * ice_vsi_init - Create and initialize a VSI
759 * @vsi: the VSI being configured
760 * @init_vsi: is this call creating a VSI
762 * This initializes a VSI context depending on the VSI type to be added and
763 * passes it down to the add_vsi aq command to create a new VSI.
765 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
767 struct ice_pf *pf = vsi->back;
768 struct ice_hw *hw = &pf->hw;
769 struct ice_vsi_ctx *ctxt;
773 dev = ice_pf_to_dev(pf);
774 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
778 ctxt->info = vsi->info;
783 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
786 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
787 /* VF number here is the absolute VF number (0-255) */
788 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
795 ice_set_dflt_vsi_ctx(ctxt);
796 /* if the switch is in VEB mode, allow VSI loopback */
797 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
798 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
800 /* Set LUT type and HASH type if RSS is enabled */
801 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
802 ice_set_rss_vsi_ctx(ctxt, vsi);
803 /* if updating VSI context, make sure to set valid_section:
804 * to indicate which section of VSI context being updated
807 ctxt->info.valid_sections |=
808 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
811 ctxt->info.sw_id = vsi->port_info->sw_id;
812 ice_vsi_setup_q_map(vsi, ctxt);
813 if (!init_vsi) /* means VSI being updated */
814 /* must to indicate which section of VSI context are
817 ctxt->info.valid_sections |=
818 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
820 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
823 if (vsi->type == ICE_VSI_VF) {
824 ctxt->info.valid_sections |=
825 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
826 if (pf->vf[vsi->vf_id].spoofchk) {
827 ctxt->info.sec_flags |=
828 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
829 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
830 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
832 ctxt->info.sec_flags &=
833 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
834 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
835 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
839 /* Allow control frames out of main VSI */
840 if (vsi->type == ICE_VSI_PF) {
841 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
842 ctxt->info.valid_sections |=
843 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
847 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
849 dev_err(dev, "Add VSI failed, err %d\n", ret);
854 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
856 dev_err(dev, "Update VSI failed, err %d\n", ret);
862 /* keep context for update VSI operations */
863 vsi->info = ctxt->info;
865 /* record VSI number returned */
866 vsi->vsi_num = ctxt->vsi_num;
874 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
875 * @vsi: ptr to the VSI
877 * This should only be called after ice_vsi_alloc() which allocates the
878 * corresponding SW VSI structure and initializes num_queue_pairs for the
879 * newly allocated VSI.
881 * Returns 0 on success or negative on failure
883 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
885 struct ice_pf *pf = vsi->back;
889 dev = ice_pf_to_dev(pf);
890 /* SRIOV doesn't grab irq_tracker entries for each VSI */
891 if (vsi->type == ICE_VSI_VF)
894 if (vsi->base_vector) {
895 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
896 vsi->vsi_num, vsi->base_vector);
900 num_q_vectors = vsi->num_q_vectors;
901 /* reserve slots from OS requested IRQs */
902 vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
904 if (vsi->base_vector < 0) {
906 "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
907 num_q_vectors, vsi->vsi_num, vsi->base_vector);
910 pf->num_avail_sw_msix -= num_q_vectors;
916 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
917 * @vsi: the VSI having rings deallocated
919 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
924 for (i = 0; i < vsi->alloc_txq; i++) {
925 if (vsi->tx_rings[i]) {
926 kfree_rcu(vsi->tx_rings[i], rcu);
927 vsi->tx_rings[i] = NULL;
932 for (i = 0; i < vsi->alloc_rxq; i++) {
933 if (vsi->rx_rings[i]) {
934 kfree_rcu(vsi->rx_rings[i], rcu);
935 vsi->rx_rings[i] = NULL;
942 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
943 * @vsi: VSI which is having rings allocated
945 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
947 struct ice_pf *pf = vsi->back;
951 dev = ice_pf_to_dev(pf);
952 /* Allocate Tx rings */
953 for (i = 0; i < vsi->alloc_txq; i++) {
954 struct ice_ring *ring;
956 /* allocate with kzalloc(), free with kfree_rcu() */
957 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
963 ring->reg_idx = vsi->txq_map[i];
964 ring->ring_active = false;
967 ring->count = vsi->num_tx_desc;
968 vsi->tx_rings[i] = ring;
971 /* Allocate Rx rings */
972 for (i = 0; i < vsi->alloc_rxq; i++) {
973 struct ice_ring *ring;
975 /* allocate with kzalloc(), free with kfree_rcu() */
976 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
981 ring->reg_idx = vsi->rxq_map[i];
982 ring->ring_active = false;
984 ring->netdev = vsi->netdev;
986 ring->count = vsi->num_rx_desc;
987 vsi->rx_rings[i] = ring;
993 ice_vsi_clear_rings(vsi);
998 * ice_vsi_manage_rss_lut - disable/enable RSS
999 * @vsi: the VSI being changed
1000 * @ena: boolean value indicating if this is an enable or disable request
1002 * In the event of disable request for RSS, this function will zero out RSS
1003 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1006 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1011 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1016 if (vsi->rss_lut_user)
1017 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1019 ice_fill_rss_lut(lut, vsi->rss_table_size,
1023 err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1029 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1030 * @vsi: VSI to be configured
1032 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1034 struct ice_aqc_get_set_rss_keys *key;
1035 struct ice_pf *pf = vsi->back;
1036 enum ice_status status;
1041 dev = ice_pf_to_dev(pf);
1042 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1044 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1048 if (vsi->rss_lut_user)
1049 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1051 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1053 status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1054 vsi->rss_table_size);
1057 dev_err(dev, "set_rss_lut failed, error %d\n", status);
1059 goto ice_vsi_cfg_rss_exit;
1062 key = kzalloc(sizeof(*key), GFP_KERNEL);
1065 goto ice_vsi_cfg_rss_exit;
1068 if (vsi->rss_hkey_user)
1070 (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1071 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1073 netdev_rss_key_fill((void *)key,
1074 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1076 status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1079 dev_err(dev, "set_rss_key failed, error %d\n", status);
1084 ice_vsi_cfg_rss_exit:
1090 * ice_add_mac_to_list - Add a MAC address filter entry to the list
1091 * @vsi: the VSI to be forwarded to
1092 * @add_list: pointer to the list which contains MAC filter entries
1093 * @macaddr: the MAC address to be added.
1095 * Adds MAC address filter entry to the temp list
1097 * Returns 0 on success or ENOMEM on failure.
1099 int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1102 struct ice_fltr_list_entry *tmp;
1103 struct ice_pf *pf = vsi->back;
1105 tmp = devm_kzalloc(ice_pf_to_dev(pf), sizeof(*tmp), GFP_ATOMIC);
1109 tmp->fltr_info.flag = ICE_FLTR_TX;
1110 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1111 tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1112 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1113 tmp->fltr_info.vsi_handle = vsi->idx;
1114 ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1116 INIT_LIST_HEAD(&tmp->list_entry);
1117 list_add(&tmp->list_entry, add_list);
1123 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1124 * @vsi: the VSI to be updated
1126 void ice_update_eth_stats(struct ice_vsi *vsi)
1128 struct ice_eth_stats *prev_es, *cur_es;
1129 struct ice_hw *hw = &vsi->back->hw;
1130 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1132 prev_es = &vsi->eth_stats_prev;
1133 cur_es = &vsi->eth_stats;
1135 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1136 &prev_es->rx_bytes, &cur_es->rx_bytes);
1138 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1139 &prev_es->rx_unicast, &cur_es->rx_unicast);
1141 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1142 &prev_es->rx_multicast, &cur_es->rx_multicast);
1144 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1145 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1147 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1148 &prev_es->rx_discards, &cur_es->rx_discards);
1150 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1151 &prev_es->tx_bytes, &cur_es->tx_bytes);
1153 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1154 &prev_es->tx_unicast, &cur_es->tx_unicast);
1156 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1157 &prev_es->tx_multicast, &cur_es->tx_multicast);
1159 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1160 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1162 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1163 &prev_es->tx_errors, &cur_es->tx_errors);
1165 vsi->stat_offsets_loaded = true;
1169 * ice_free_fltr_list - free filter lists helper
1170 * @dev: pointer to the device struct
1171 * @h: pointer to the list head to be freed
1173 * Helper function to free filter lists previously created using
1174 * ice_add_mac_to_list
1176 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1178 struct ice_fltr_list_entry *e, *tmp;
1180 list_for_each_entry_safe(e, tmp, h, list_entry) {
1181 list_del(&e->list_entry);
1187 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1188 * @vsi: the VSI being configured
1189 * @vid: VLAN ID to be added
1191 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1193 struct ice_fltr_list_entry *tmp;
1194 struct ice_pf *pf = vsi->back;
1195 LIST_HEAD(tmp_add_list);
1196 enum ice_status status;
1200 dev = ice_pf_to_dev(pf);
1201 tmp = devm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
1205 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1206 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1207 tmp->fltr_info.flag = ICE_FLTR_TX;
1208 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1209 tmp->fltr_info.vsi_handle = vsi->idx;
1210 tmp->fltr_info.l_data.vlan.vlan_id = vid;
1212 INIT_LIST_HEAD(&tmp->list_entry);
1213 list_add(&tmp->list_entry, &tmp_add_list);
1215 status = ice_add_vlan(&pf->hw, &tmp_add_list);
1218 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1222 ice_free_fltr_list(dev, &tmp_add_list);
1227 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1228 * @vsi: the VSI being configured
1229 * @vid: VLAN ID to be removed
1231 * Returns 0 on success and negative on failure
1233 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1235 struct ice_fltr_list_entry *list;
1236 struct ice_pf *pf = vsi->back;
1237 LIST_HEAD(tmp_add_list);
1238 enum ice_status status;
1242 dev = ice_pf_to_dev(pf);
1243 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1247 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1248 list->fltr_info.vsi_handle = vsi->idx;
1249 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1250 list->fltr_info.l_data.vlan.vlan_id = vid;
1251 list->fltr_info.flag = ICE_FLTR_TX;
1252 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1254 INIT_LIST_HEAD(&list->list_entry);
1255 list_add(&list->list_entry, &tmp_add_list);
1257 status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1258 if (status == ICE_ERR_DOES_NOT_EXIST) {
1260 "Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1261 vid, vsi->vsi_num, status);
1262 } else if (status) {
1264 "Error removing VLAN %d on vsi %i error: %d\n",
1265 vid, vsi->vsi_num, status);
1269 ice_free_fltr_list(dev, &tmp_add_list);
1274 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1277 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1279 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1280 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1281 vsi->rx_buf_len = ICE_RXBUF_2048;
1282 #if (PAGE_SIZE < 8192)
1283 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1284 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1285 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1286 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1289 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1290 #if (PAGE_SIZE < 8192)
1291 vsi->rx_buf_len = ICE_RXBUF_3072;
1293 vsi->rx_buf_len = ICE_RXBUF_2048;
1299 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1300 * @vsi: the VSI being configured
1302 * Return 0 on success and a negative value on error
1303 * Configure the Rx VSI for operation.
1305 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1309 if (vsi->type == ICE_VSI_VF)
1312 ice_vsi_cfg_frame_size(vsi);
1314 /* set up individual rings */
1315 for (i = 0; i < vsi->num_rxq; i++) {
1318 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1320 dev_err(&vsi->back->pdev->dev,
1321 "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1331 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1332 * @vsi: the VSI being configured
1333 * @rings: Tx ring array to be configured
1335 * Return 0 on success and a negative value on error
1336 * Configure the Tx VSI for operation.
1339 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1341 struct ice_aqc_add_tx_qgrp *qg_buf;
1345 qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1349 qg_buf->num_txqs = 1;
1351 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1352 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1363 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1364 * @vsi: the VSI being configured
1366 * Return 0 on success and a negative value on error
1367 * Configure the Tx VSI for operation.
1369 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1371 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1375 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1376 * @vsi: the VSI being configured
1378 * Return 0 on success and a negative value on error
1379 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1381 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1386 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1390 for (i = 0; i < vsi->num_xdp_txq; i++)
1391 vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1397 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1398 * @intrl: interrupt rate limit in usecs
1399 * @gran: interrupt rate limit granularity in usecs
1401 * This function converts a decimal interrupt rate limit in usecs to the format
1402 * expected by firmware.
1404 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1406 u32 val = intrl / gran;
1409 return val | GLINT_RATE_INTRL_ENA_M;
1414 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1415 * @vsi: the VSI being configured
1417 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1420 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1422 struct ice_pf *pf = vsi->back;
1423 struct ice_hw *hw = &pf->hw;
1424 u32 txq = 0, rxq = 0;
1427 for (i = 0; i < vsi->num_q_vectors; i++) {
1428 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1429 u16 reg_idx = q_vector->reg_idx;
1431 ice_cfg_itr(hw, q_vector);
1433 wr32(hw, GLINT_RATE(reg_idx),
1434 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1436 /* Both Transmit Queue Interrupt Cause Control register
1437 * and Receive Queue Interrupt Cause control register
1438 * expects MSIX_INDX field to be the vector index
1439 * within the function space and not the absolute
1440 * vector index across PF or across device.
1441 * For SR-IOV VF VSIs queue vector index always starts
1442 * with 1 since first vector index(0) is used for OICR
1443 * in VF space. Since VMDq and other PF VSIs are within
1444 * the PF function space, use the vector index that is
1445 * tracked for this PF.
1447 for (q = 0; q < q_vector->num_ring_tx; q++) {
1448 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1449 q_vector->tx.itr_idx);
1453 for (q = 0; q < q_vector->num_ring_rx; q++) {
1454 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1455 q_vector->rx.itr_idx);
1462 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1463 * @vsi: the VSI being changed
1465 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1467 struct ice_hw *hw = &vsi->back->hw;
1468 struct ice_vsi_ctx *ctxt;
1469 enum ice_status status;
1472 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1476 /* Here we are configuring the VSI to let the driver add VLAN tags by
1477 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1478 * insertion happens in the Tx hot path, in ice_tx_map.
1480 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1482 /* Preserve existing VLAN strip setting */
1483 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1484 ICE_AQ_VSI_VLAN_EMOD_M);
1486 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1488 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1490 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
1491 status, hw->adminq.sq_last_status);
1496 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1503 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1504 * @vsi: the VSI being changed
1505 * @ena: boolean value indicating if this is a enable or disable request
1507 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1509 struct ice_hw *hw = &vsi->back->hw;
1510 struct ice_vsi_ctx *ctxt;
1511 enum ice_status status;
1514 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1518 /* Here we are configuring what the VSI should do with the VLAN tag in
1519 * the Rx packet. We can either leave the tag in the packet or put it in
1520 * the Rx descriptor.
1523 /* Strip VLAN tag from Rx packet and put it in the desc */
1524 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1526 /* Disable stripping. Leave tag in packet */
1527 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1529 /* Allow all packets untagged/tagged */
1530 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1532 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1534 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1536 dev_err(&vsi->back->pdev->dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1537 ena, status, hw->adminq.sq_last_status);
1542 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1549 * ice_vsi_start_rx_rings - start VSI's Rx rings
1550 * @vsi: the VSI whose rings are to be started
1552 * Returns 0 on success and a negative value on error
1554 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1556 return ice_vsi_ctrl_rx_rings(vsi, true);
1560 * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1563 * Returns 0 on success and a negative value on error
1565 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1567 return ice_vsi_ctrl_rx_rings(vsi, false);
1571 * ice_vsi_stop_tx_rings - Disable Tx rings
1572 * @vsi: the VSI being configured
1573 * @rst_src: reset source
1574 * @rel_vmvf_num: Relative ID of VF/VM
1575 * @rings: Tx ring array to be stopped
1578 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1579 u16 rel_vmvf_num, struct ice_ring **rings)
1583 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1586 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1587 struct ice_txq_meta txq_meta = { };
1590 if (!rings || !rings[q_idx])
1593 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1594 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1595 rings[q_idx], &txq_meta);
1605 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1606 * @vsi: the VSI being configured
1607 * @rst_src: reset source
1608 * @rel_vmvf_num: Relative ID of VF/VM
1611 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1614 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1618 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1619 * @vsi: the VSI being configured
1621 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1623 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1627 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1628 * @vsi: VSI to enable or disable VLAN pruning on
1629 * @ena: set to true to enable VLAN pruning and false to disable it
1630 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1632 * returns 0 if VSI is updated, negative otherwise
1634 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1636 struct ice_vsi_ctx *ctxt;
1644 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1648 ctxt->info = vsi->info;
1651 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1653 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1656 ctxt->info.valid_sections =
1657 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
1659 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1661 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1662 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
1663 pf->hw.adminq.sq_last_status);
1667 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1677 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
1679 struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
1681 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
1682 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
1686 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
1687 * @vsi: VSI to set the q_vectors register index on
1690 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
1694 if (!vsi || !vsi->q_vectors)
1697 ice_for_each_q_vector(vsi, i) {
1698 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1701 dev_err(&vsi->back->pdev->dev,
1702 "Failed to set reg_idx on q_vector %d VSI %d\n",
1707 if (vsi->type == ICE_VSI_VF) {
1708 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
1710 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
1713 q_vector->v_idx + vsi->base_vector;
1720 ice_for_each_q_vector(vsi, i) {
1721 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1724 q_vector->reg_idx = 0;
1731 * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
1732 * @vsi: the VSI being configured
1733 * @add_rule: boolean value to add or remove ethertype filter rule
1736 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
1738 struct ice_fltr_list_entry *list;
1739 struct ice_pf *pf = vsi->back;
1740 LIST_HEAD(tmp_add_list);
1741 enum ice_status status;
1744 dev = ice_pf_to_dev(pf);
1745 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1749 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1750 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1751 list->fltr_info.flag = ICE_FLTR_TX;
1752 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1753 list->fltr_info.vsi_handle = vsi->idx;
1754 list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
1756 INIT_LIST_HEAD(&list->list_entry);
1757 list_add(&list->list_entry, &tmp_add_list);
1760 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1762 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1766 "Failure Adding or Removing Ethertype on VSI %i error: %d\n",
1767 vsi->vsi_num, status);
1769 ice_free_fltr_list(dev, &tmp_add_list);
1773 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
1774 * @vsi: the VSI being configured
1775 * @tx: bool to determine Tx or Rx rule
1776 * @create: bool to determine create or remove Rule
1778 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
1780 struct ice_fltr_list_entry *list;
1781 struct ice_pf *pf = vsi->back;
1782 LIST_HEAD(tmp_add_list);
1783 enum ice_status status;
1786 dev = ice_pf_to_dev(pf);
1787 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1791 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1792 list->fltr_info.vsi_handle = vsi->idx;
1793 list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
1796 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1797 list->fltr_info.flag = ICE_FLTR_TX;
1798 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1800 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1801 list->fltr_info.flag = ICE_FLTR_RX;
1802 list->fltr_info.src_id = ICE_SRC_ID_LPORT;
1805 INIT_LIST_HEAD(&list->list_entry);
1806 list_add(&list->list_entry, &tmp_add_list);
1809 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1811 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1814 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
1815 create ? "adding" : "removing", tx ? "TX" : "RX",
1816 vsi->vsi_num, status);
1818 ice_free_fltr_list(dev, &tmp_add_list);
1822 * ice_vsi_setup - Set up a VSI by a given type
1823 * @pf: board private structure
1824 * @pi: pointer to the port_info instance
1826 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1827 * used only for ICE_VSI_VF VSI type. For other VSI types, should
1828 * fill-in ICE_INVAL_VFID as input.
1830 * This allocates the sw VSI structure and its queue resources.
1832 * Returns pointer to the successfully allocated and configured VSI sw struct on
1833 * success, NULL on failure.
1836 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1837 enum ice_vsi_type type, u16 vf_id)
1839 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1840 struct device *dev = ice_pf_to_dev(pf);
1841 enum ice_status status;
1842 struct ice_vsi *vsi;
1845 if (type == ICE_VSI_VF)
1846 vsi = ice_vsi_alloc(pf, type, vf_id);
1848 vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
1851 dev_err(dev, "could not allocate VSI\n");
1855 vsi->port_info = pi;
1856 vsi->vsw = pf->first_sw;
1857 if (vsi->type == ICE_VSI_PF)
1858 vsi->ethtype = ETH_P_PAUSE;
1860 if (vsi->type == ICE_VSI_VF)
1863 if (ice_vsi_get_qs(vsi)) {
1864 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1869 /* set RSS capabilities */
1870 ice_vsi_set_rss_params(vsi);
1872 /* set TC configuration */
1873 ice_vsi_set_tc_cfg(vsi);
1875 /* create the VSI */
1876 ret = ice_vsi_init(vsi, true);
1880 switch (vsi->type) {
1882 ret = ice_vsi_alloc_q_vectors(vsi);
1884 goto unroll_vsi_init;
1886 ret = ice_vsi_setup_vector_base(vsi);
1888 goto unroll_alloc_q_vector;
1890 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1892 goto unroll_vector_base;
1894 ret = ice_vsi_alloc_rings(vsi);
1896 goto unroll_vector_base;
1898 ice_vsi_map_rings_to_vectors(vsi);
1900 /* Do not exit if configuring RSS had an issue, at least
1901 * receive traffic on first queue. Hence no need to capture
1904 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1905 ice_vsi_cfg_rss_lut_key(vsi);
1908 /* VF driver will take care of creating netdev for this type and
1909 * map queues to vectors through Virtchnl, PF driver only
1910 * creates a VSI and corresponding structures for bookkeeping
1913 ret = ice_vsi_alloc_q_vectors(vsi);
1915 goto unroll_vsi_init;
1917 ret = ice_vsi_alloc_rings(vsi);
1919 goto unroll_alloc_q_vector;
1921 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
1923 goto unroll_vector_base;
1925 /* Do not exit if configuring RSS had an issue, at least
1926 * receive traffic on first queue. Hence no need to capture
1929 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1930 ice_vsi_cfg_rss_lut_key(vsi);
1933 ret = ice_vsi_alloc_rings(vsi);
1935 goto unroll_vsi_init;
1938 /* clean up the resources and exit */
1939 goto unroll_vsi_init;
1942 /* configure VSI nodes based on number of queues and TC's */
1943 for (i = 0; i < vsi->tc_cfg.numtc; i++)
1944 max_txqs[i] = vsi->alloc_txq;
1946 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
1949 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
1950 vsi->vsi_num, status);
1951 goto unroll_vector_base;
1954 /* Add switch rule to drop all Tx Flow Control Frames, of look up
1955 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
1956 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
1957 * The rule is added once for PF VSI in order to create appropriate
1958 * recipe, since VSI/VSI list is ignored with drop action...
1959 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
1960 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
1961 * settings in the HW.
1963 if (!ice_is_safe_mode(pf))
1964 if (vsi->type == ICE_VSI_PF) {
1965 ice_vsi_add_rem_eth_mac(vsi, true);
1967 /* Tx LLDP packets */
1968 ice_cfg_sw_lldp(vsi, true, true);
1974 /* reclaim SW interrupts back to the common pool */
1975 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
1976 pf->num_avail_sw_msix += vsi->num_q_vectors;
1977 unroll_alloc_q_vector:
1978 ice_vsi_free_q_vectors(vsi);
1980 ice_vsi_delete(vsi);
1982 ice_vsi_put_qs(vsi);
1989 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1990 * @vsi: the VSI being cleaned up
1992 static void ice_vsi_release_msix(struct ice_vsi *vsi)
1994 struct ice_pf *pf = vsi->back;
1995 struct ice_hw *hw = &pf->hw;
2000 for (i = 0; i < vsi->num_q_vectors; i++) {
2001 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2002 u16 reg_idx = q_vector->reg_idx;
2004 wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2005 wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2006 for (q = 0; q < q_vector->num_ring_tx; q++) {
2007 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2008 if (ice_is_xdp_ena_vsi(vsi)) {
2009 u32 xdp_txq = txq + vsi->num_xdp_txq;
2011 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2016 for (q = 0; q < q_vector->num_ring_rx; q++) {
2017 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2026 * ice_vsi_free_irq - Free the IRQ association with the OS
2027 * @vsi: the VSI being configured
2029 void ice_vsi_free_irq(struct ice_vsi *vsi)
2031 struct ice_pf *pf = vsi->back;
2032 int base = vsi->base_vector;
2035 if (!vsi->q_vectors || !vsi->irqs_ready)
2038 ice_vsi_release_msix(vsi);
2039 if (vsi->type == ICE_VSI_VF)
2042 vsi->irqs_ready = false;
2043 ice_for_each_q_vector(vsi, i) {
2044 u16 vector = i + base;
2047 irq_num = pf->msix_entries[vector].vector;
2049 /* free only the irqs that were actually requested */
2050 if (!vsi->q_vectors[i] ||
2051 !(vsi->q_vectors[i]->num_ring_tx ||
2052 vsi->q_vectors[i]->num_ring_rx))
2055 /* clear the affinity notifier in the IRQ descriptor */
2056 irq_set_affinity_notifier(irq_num, NULL);
2058 /* clear the affinity_mask in the IRQ descriptor */
2059 irq_set_affinity_hint(irq_num, NULL);
2060 synchronize_irq(irq_num);
2061 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2066 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2067 * @vsi: the VSI having resources freed
2069 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2076 ice_for_each_txq(vsi, i)
2077 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2078 ice_free_tx_ring(vsi->tx_rings[i]);
2082 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2083 * @vsi: the VSI having resources freed
2085 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2092 ice_for_each_rxq(vsi, i)
2093 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2094 ice_free_rx_ring(vsi->rx_rings[i]);
2098 * ice_vsi_close - Shut down a VSI
2099 * @vsi: the VSI being shut down
2101 void ice_vsi_close(struct ice_vsi *vsi)
2103 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2106 ice_vsi_free_irq(vsi);
2107 ice_vsi_free_tx_rings(vsi);
2108 ice_vsi_free_rx_rings(vsi);
2112 * ice_ena_vsi - resume a VSI
2113 * @vsi: the VSI being resume
2114 * @locked: is the rtnl_lock already held
2116 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2120 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2123 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2125 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2126 if (netif_running(vsi->netdev)) {
2130 err = ice_open(vsi->netdev);
2141 * ice_dis_vsi - pause a VSI
2142 * @vsi: the VSI being paused
2143 * @locked: is the rtnl_lock already held
2145 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2147 if (test_bit(__ICE_DOWN, vsi->state))
2150 set_bit(__ICE_NEEDS_RESTART, vsi->state);
2152 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2153 if (netif_running(vsi->netdev)) {
2157 ice_stop(vsi->netdev);
2168 * ice_free_res - free a block of resources
2169 * @res: pointer to the resource
2170 * @index: starting index previously returned by ice_get_res
2171 * @id: identifier to track owner
2173 * Returns number of resources freed
2175 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2180 if (!res || index >= res->end)
2183 id |= ICE_RES_VALID_BIT;
2184 for (i = index; i < res->end && res->list[i] == id; i++) {
2193 * ice_search_res - Search the tracker for a block of resources
2194 * @res: pointer to the resource
2195 * @needed: size of the block needed
2196 * @id: identifier to track owner
2198 * Returns the base item index of the block, or -ENOMEM for error
2200 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2202 int start = 0, end = 0;
2204 if (needed > res->end)
2207 id |= ICE_RES_VALID_BIT;
2210 /* skip already allocated entries */
2211 if (res->list[end++] & ICE_RES_VALID_BIT) {
2213 if ((start + needed) > res->end)
2217 if (end == (start + needed)) {
2220 /* there was enough, so assign it to the requestor */
2222 res->list[i++] = id;
2226 } while (end < res->end);
2232 * ice_get_res - get a block of resources
2233 * @pf: board private structure
2234 * @res: pointer to the resource
2235 * @needed: size of the block needed
2236 * @id: identifier to track owner
2238 * Returns the base item index of the block, or negative for error
2241 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2246 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2247 dev_err(ice_pf_to_dev(pf),
2248 "param err: needed=%d, num_entries = %d id=0x%04x\n",
2249 needed, res->num_entries, id);
2253 return ice_search_res(res, needed, id);
2257 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2258 * @vsi: the VSI being un-configured
2260 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2262 int base = vsi->base_vector;
2263 struct ice_pf *pf = vsi->back;
2264 struct ice_hw *hw = &pf->hw;
2268 /* disable interrupt causation from each queue */
2269 if (vsi->tx_rings) {
2270 ice_for_each_txq(vsi, i) {
2271 if (vsi->tx_rings[i]) {
2274 reg = vsi->tx_rings[i]->reg_idx;
2275 val = rd32(hw, QINT_TQCTL(reg));
2276 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2277 wr32(hw, QINT_TQCTL(reg), val);
2282 if (vsi->rx_rings) {
2283 ice_for_each_rxq(vsi, i) {
2284 if (vsi->rx_rings[i]) {
2287 reg = vsi->rx_rings[i]->reg_idx;
2288 val = rd32(hw, QINT_RQCTL(reg));
2289 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2290 wr32(hw, QINT_RQCTL(reg), val);
2295 /* disable each interrupt */
2296 ice_for_each_q_vector(vsi, i) {
2297 if (!vsi->q_vectors[i])
2299 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2304 /* don't call synchronize_irq() for VF's from the host */
2305 if (vsi->type == ICE_VSI_VF)
2308 ice_for_each_q_vector(vsi, i)
2309 synchronize_irq(pf->msix_entries[i + base].vector);
2313 * ice_napi_del - Remove NAPI handler for the VSI
2314 * @vsi: VSI for which NAPI handler is to be removed
2316 void ice_napi_del(struct ice_vsi *vsi)
2323 ice_for_each_q_vector(vsi, v_idx)
2324 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2328 * ice_vsi_release - Delete a VSI and free its resources
2329 * @vsi: the VSI being removed
2331 * Returns 0 on success or < 0 on error
2333 int ice_vsi_release(struct ice_vsi *vsi)
2341 /* do not unregister while driver is in the reset recovery pending
2342 * state. Since reset/rebuild happens through PF service task workqueue,
2343 * it's not a good idea to unregister netdev that is associated to the
2344 * PF that is running the work queue items currently. This is done to
2345 * avoid check_flush_dependency() warning on this wq
2347 if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2348 unregister_netdev(vsi->netdev);
2350 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2353 /* Disable VSI and free resources */
2354 if (vsi->type != ICE_VSI_LB)
2355 ice_vsi_dis_irq(vsi);
2358 /* SR-IOV determines needed MSIX resources all at once instead of per
2359 * VSI since when VFs are spawned we know how many VFs there are and how
2360 * many interrupts each VF needs. SR-IOV MSIX resources are also
2361 * cleared in the same manner.
2363 if (vsi->type != ICE_VSI_VF) {
2364 /* reclaim SW interrupts back to the common pool */
2365 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2366 pf->num_avail_sw_msix += vsi->num_q_vectors;
2369 if (!ice_is_safe_mode(pf)) {
2370 if (vsi->type == ICE_VSI_PF) {
2371 ice_vsi_add_rem_eth_mac(vsi, false);
2372 ice_cfg_sw_lldp(vsi, true, false);
2373 /* The Rx rule will only exist to remove if the LLDP FW
2374 * engine is currently stopped
2376 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2377 ice_cfg_sw_lldp(vsi, false, false);
2381 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2382 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2383 ice_vsi_delete(vsi);
2384 ice_vsi_free_q_vectors(vsi);
2386 /* make sure unregister_netdev() was called by checking __ICE_DOWN */
2387 if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2388 free_netdev(vsi->netdev);
2392 ice_vsi_clear_rings(vsi);
2394 ice_vsi_put_qs(vsi);
2396 /* retain SW VSI data structure since it is needed to unregister and
2397 * free VSI netdev when PF is not in reset recovery pending state,\
2398 * for ex: during rmmod.
2400 if (!ice_is_reset_in_progress(pf->state))
2407 * ice_vsi_rebuild_update_coalesce - set coalesce for a q_vector
2408 * @q_vector: pointer to q_vector which is being updated
2409 * @coalesce: pointer to array of struct with stored coalesce
2411 * Set coalesce param in q_vector and update these parameters in HW.
2414 ice_vsi_rebuild_update_coalesce(struct ice_q_vector *q_vector,
2415 struct ice_coalesce_stored *coalesce)
2417 struct ice_ring_container *rx_rc = &q_vector->rx;
2418 struct ice_ring_container *tx_rc = &q_vector->tx;
2419 struct ice_hw *hw = &q_vector->vsi->back->hw;
2421 tx_rc->itr_setting = coalesce->itr_tx;
2422 rx_rc->itr_setting = coalesce->itr_rx;
2424 /* dynamic ITR values will be updated during Tx/Rx */
2425 if (!ITR_IS_DYNAMIC(tx_rc->itr_setting))
2426 wr32(hw, GLINT_ITR(tx_rc->itr_idx, q_vector->reg_idx),
2427 ITR_REG_ALIGN(tx_rc->itr_setting) >>
2429 if (!ITR_IS_DYNAMIC(rx_rc->itr_setting))
2430 wr32(hw, GLINT_ITR(rx_rc->itr_idx, q_vector->reg_idx),
2431 ITR_REG_ALIGN(rx_rc->itr_setting) >>
2434 q_vector->intrl = coalesce->intrl;
2435 wr32(hw, GLINT_RATE(q_vector->reg_idx),
2436 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
2440 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2441 * @vsi: VSI connected with q_vectors
2442 * @coalesce: array of struct with stored coalesce
2444 * Returns array size.
2447 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2448 struct ice_coalesce_stored *coalesce)
2452 ice_for_each_q_vector(vsi, i) {
2453 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2455 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2456 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2457 coalesce[i].intrl = q_vector->intrl;
2460 return vsi->num_q_vectors;
2464 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2465 * @vsi: VSI connected with q_vectors
2466 * @coalesce: pointer to array of struct with stored coalesce
2467 * @size: size of coalesce array
2469 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2470 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2474 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2475 struct ice_coalesce_stored *coalesce, int size)
2479 if ((size && !coalesce) || !vsi)
2482 for (i = 0; i < size && i < vsi->num_q_vectors; i++)
2483 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2486 for (; i < vsi->num_q_vectors; i++) {
2487 struct ice_coalesce_stored coalesce_dflt = {
2488 .itr_tx = ICE_DFLT_TX_ITR,
2489 .itr_rx = ICE_DFLT_RX_ITR,
2492 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2498 * ice_vsi_rebuild - Rebuild VSI after reset
2499 * @vsi: VSI to be rebuild
2500 * @init_vsi: is this an initialization or a reconfigure of the VSI
2502 * Returns 0 on success and negative value on failure
2504 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2506 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2507 struct ice_coalesce_stored *coalesce;
2508 int prev_num_q_vectors = 0;
2509 struct ice_vf *vf = NULL;
2510 enum ice_status status;
2518 if (vsi->type == ICE_VSI_VF)
2519 vf = &pf->vf[vsi->vf_id];
2521 coalesce = kcalloc(vsi->num_q_vectors,
2522 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
2524 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi,
2526 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2527 ice_vsi_free_q_vectors(vsi);
2529 /* SR-IOV determines needed MSIX resources all at once instead of per
2530 * VSI since when VFs are spawned we know how many VFs there are and how
2531 * many interrupts each VF needs. SR-IOV MSIX resources are also
2532 * cleared in the same manner.
2534 if (vsi->type != ICE_VSI_VF) {
2535 /* reclaim SW interrupts back to the common pool */
2536 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2537 pf->num_avail_sw_msix += vsi->num_q_vectors;
2538 vsi->base_vector = 0;
2541 if (ice_is_xdp_ena_vsi(vsi))
2542 /* return value check can be skipped here, it always returns
2543 * 0 if reset is in progress
2545 ice_destroy_xdp_rings(vsi);
2546 ice_vsi_put_qs(vsi);
2547 ice_vsi_clear_rings(vsi);
2548 ice_vsi_free_arrays(vsi);
2549 ice_dev_onetime_setup(&pf->hw);
2550 if (vsi->type == ICE_VSI_VF)
2551 ice_vsi_set_num_qs(vsi, vf->vf_id);
2553 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2555 ret = ice_vsi_alloc_arrays(vsi);
2559 ice_vsi_get_qs(vsi);
2560 ice_vsi_set_tc_cfg(vsi);
2562 /* Initialize VSI struct elements and create VSI in FW */
2563 ret = ice_vsi_init(vsi, init_vsi);
2567 switch (vsi->type) {
2569 ret = ice_vsi_alloc_q_vectors(vsi);
2573 ret = ice_vsi_setup_vector_base(vsi);
2577 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2581 ret = ice_vsi_alloc_rings(vsi);
2585 ice_vsi_map_rings_to_vectors(vsi);
2586 if (ice_is_xdp_ena_vsi(vsi)) {
2587 vsi->num_xdp_txq = vsi->alloc_txq;
2588 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2592 /* Do not exit if configuring RSS had an issue, at least
2593 * receive traffic on first queue. Hence no need to capture
2596 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2597 ice_vsi_cfg_rss_lut_key(vsi);
2600 ret = ice_vsi_alloc_q_vectors(vsi);
2604 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2608 ret = ice_vsi_alloc_rings(vsi);
2617 /* configure VSI nodes based on number of queues and TC's */
2618 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2619 max_txqs[i] = vsi->alloc_txq;
2621 if (ice_is_xdp_ena_vsi(vsi))
2622 max_txqs[i] += vsi->num_xdp_txq;
2625 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2628 dev_err(ice_pf_to_dev(pf),
2629 "VSI %d failed lan queue config, error %d\n",
2630 vsi->vsi_num, status);
2635 return ice_schedule_reset(pf, ICE_RESET_PFR);
2638 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
2644 ice_vsi_free_q_vectors(vsi);
2647 vsi->current_netdev_flags = 0;
2648 unregister_netdev(vsi->netdev);
2649 free_netdev(vsi->netdev);
2654 set_bit(__ICE_RESET_FAILED, pf->state);
2660 * ice_is_reset_in_progress - check for a reset in progress
2661 * @state: PF state field
2663 bool ice_is_reset_in_progress(unsigned long *state)
2665 return test_bit(__ICE_RESET_OICR_RECV, state) ||
2666 test_bit(__ICE_DCBNL_DEVRESET, state) ||
2667 test_bit(__ICE_PFR_REQ, state) ||
2668 test_bit(__ICE_CORER_REQ, state) ||
2669 test_bit(__ICE_GLOBR_REQ, state);
2674 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2675 * @vsi: VSI being configured
2676 * @ctx: the context buffer returned from AQ VSI update command
2678 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2680 vsi->info.mapping_flags = ctx->info.mapping_flags;
2681 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2682 sizeof(vsi->info.q_mapping));
2683 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2684 sizeof(vsi->info.tc_mapping));
2688 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2689 * @vsi: VSI to be configured
2690 * @ena_tc: TC bitmap
2692 * VSI queues expected to be quiesced before calling this function
2694 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2696 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2697 struct ice_vsi_ctx *ctx;
2698 struct ice_pf *pf = vsi->back;
2699 enum ice_status status;
2704 dev = ice_pf_to_dev(pf);
2706 ice_for_each_traffic_class(i) {
2707 /* build bitmap of enabled TCs */
2708 if (ena_tc & BIT(i))
2710 /* populate max_txqs per TC */
2711 max_txqs[i] = vsi->alloc_txq;
2714 vsi->tc_cfg.ena_tc = ena_tc;
2715 vsi->tc_cfg.numtc = num_tc;
2717 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2722 ctx->info = vsi->info;
2724 ice_vsi_setup_q_map(vsi, ctx);
2726 /* must to indicate which section of VSI context are being modified */
2727 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2728 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2730 dev_info(dev, "Failed VSI Update\n");
2735 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2739 dev_err(dev, "VSI %d failed TC config, error %d\n",
2740 vsi->vsi_num, status);
2744 ice_vsi_update_q_map(vsi, ctx);
2745 vsi->info.valid_sections = 0;
2747 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2752 #endif /* CONFIG_DCB */
2755 * ice_nvm_version_str - format the NVM version strings
2756 * @hw: ptr to the hardware info
2758 char *ice_nvm_version_str(struct ice_hw *hw)
2760 u8 oem_ver, oem_patch, ver_hi, ver_lo;
2761 static char buf[ICE_NVM_VER_LEN];
2764 ice_get_nvm_version(hw, &oem_ver, &oem_build, &oem_patch, &ver_hi,
2767 snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d", ver_hi, ver_lo,
2768 hw->nvm.eetrack, oem_ver, oem_build, oem_patch);
2774 * ice_update_ring_stats - Update ring statistics
2775 * @ring: ring to update
2776 * @cont: used to increment per-vector counters
2777 * @pkts: number of processed packets
2778 * @bytes: number of processed bytes
2780 * This function assumes that caller has acquired a u64_stats_sync lock.
2783 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2784 u64 pkts, u64 bytes)
2786 ring->stats.bytes += bytes;
2787 ring->stats.pkts += pkts;
2788 cont->total_bytes += bytes;
2789 cont->total_pkts += pkts;
2793 * ice_update_tx_ring_stats - Update Tx ring specific counters
2794 * @tx_ring: ring to update
2795 * @pkts: number of processed packets
2796 * @bytes: number of processed bytes
2798 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
2800 u64_stats_update_begin(&tx_ring->syncp);
2801 ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
2802 u64_stats_update_end(&tx_ring->syncp);
2806 * ice_update_rx_ring_stats - Update Rx ring specific counters
2807 * @rx_ring: ring to update
2808 * @pkts: number of processed packets
2809 * @bytes: number of processed bytes
2811 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
2813 u64_stats_update_begin(&rx_ring->syncp);
2814 ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
2815 u64_stats_update_end(&rx_ring->syncp);
2819 * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
2820 * @vsi: the VSI being configured MAC filter
2821 * @macaddr: the MAC address to be added.
2822 * @set: Add or delete a MAC filter
2824 * Adds or removes MAC address filter entry for VF VSI
2827 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
2829 LIST_HEAD(tmp_add_list);
2830 enum ice_status status;
2832 /* Update MAC filter list to be added or removed for a VSI */
2833 if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
2834 status = ICE_ERR_NO_MEMORY;
2835 goto cfg_mac_fltr_exit;
2839 status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
2841 status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
2844 ice_free_fltr_list(&vsi->back->pdev->dev, &tmp_add_list);
2849 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
2850 * @sw: switch to check if its default forwarding VSI is free
2852 * Return true if the default forwarding VSI is already being used, else returns
2853 * false signalling that it's available to use.
2855 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
2857 return (sw->dflt_vsi && sw->dflt_vsi_ena);
2861 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
2862 * @sw: switch for the default forwarding VSI to compare against
2863 * @vsi: VSI to compare against default forwarding VSI
2865 * If this VSI passed in is the default forwarding VSI then return true, else
2868 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
2870 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
2874 * ice_set_dflt_vsi - set the default forwarding VSI
2875 * @sw: switch used to assign the default forwarding VSI
2876 * @vsi: VSI getting set as the default forwarding VSI on the switch
2878 * If the VSI passed in is already the default VSI and it's enabled just return
2881 * If there is already a default VSI on the switch and it's enabled then return
2882 * -EEXIST since there can only be one default VSI per switch.
2884 * Otherwise try to set the VSI passed in as the switch's default VSI and
2885 * return the result.
2887 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
2889 enum ice_status status;
2895 dev = ice_pf_to_dev(vsi->back);
2897 /* the VSI passed in is already the default VSI */
2898 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
2899 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
2904 /* another VSI is already the default VSI for this switch */
2905 if (ice_is_dflt_vsi_in_use(sw)) {
2907 "Default forwarding VSI %d already in use, disable it and try again\n",
2908 sw->dflt_vsi->vsi_num);
2912 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
2915 "Failed to set VSI %d as the default forwarding VSI, error %d\n",
2916 vsi->vsi_num, status);
2921 sw->dflt_vsi_ena = true;
2927 * ice_clear_dflt_vsi - clear the default forwarding VSI
2928 * @sw: switch used to clear the default VSI
2930 * If the switch has no default VSI or it's not enabled then return error.
2932 * Otherwise try to clear the default VSI and return the result.
2934 int ice_clear_dflt_vsi(struct ice_sw *sw)
2936 struct ice_vsi *dflt_vsi;
2937 enum ice_status status;
2943 dev = ice_pf_to_dev(sw->pf);
2945 dflt_vsi = sw->dflt_vsi;
2947 /* there is no default VSI configured */
2948 if (!ice_is_dflt_vsi_in_use(sw))
2951 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
2955 "Failed to clear the default forwarding VSI %d, error %d\n",
2956 dflt_vsi->vsi_num, status);
2960 sw->dflt_vsi = NULL;
2961 sw->dflt_vsi_ena = false;