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(ice_pf_to_dev(vsi->back), "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_vsi_clean_rss_flow_fld - Delete RSS configuration
497 * @vsi: the VSI being cleaned up
499 * This function deletes RSS input set for all flows that were configured
502 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
504 struct ice_pf *pf = vsi->back;
505 enum ice_status status;
507 if (ice_is_safe_mode(pf))
510 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
512 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
513 vsi->vsi_num, status);
517 * ice_rss_clean - Delete RSS related VSI structures and configuration
518 * @vsi: the VSI being removed
520 static void ice_rss_clean(struct ice_vsi *vsi)
522 struct ice_pf *pf = vsi->back;
525 dev = ice_pf_to_dev(pf);
527 if (vsi->rss_hkey_user)
528 devm_kfree(dev, vsi->rss_hkey_user);
529 if (vsi->rss_lut_user)
530 devm_kfree(dev, vsi->rss_lut_user);
532 ice_vsi_clean_rss_flow_fld(vsi);
533 /* remove RSS replay list */
534 if (!ice_is_safe_mode(pf))
535 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
539 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
540 * @vsi: the VSI being configured
542 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
544 struct ice_hw_common_caps *cap;
545 struct ice_pf *pf = vsi->back;
547 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
552 cap = &pf->hw.func_caps.common_cap;
555 /* PF VSI will inherit RSS instance of PF */
556 vsi->rss_table_size = cap->rss_table_size;
557 vsi->rss_size = min_t(int, num_online_cpus(),
558 BIT(cap->rss_table_entry_width));
559 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
562 /* VF VSI will gets a small RSS table
563 * For VSI_LUT, LUT size should be set to 64 bytes
565 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
566 vsi->rss_size = min_t(int, num_online_cpus(),
567 BIT(cap->rss_table_entry_width));
568 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
573 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n",
580 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
581 * @ctxt: the VSI context being set
583 * This initializes a default VSI context for all sections except the Queues.
585 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
589 memset(&ctxt->info, 0, sizeof(ctxt->info));
590 /* VSI's should be allocated from shared pool */
591 ctxt->alloc_from_pool = true;
592 /* Src pruning enabled by default */
593 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
594 /* Traffic from VSI can be sent to LAN */
595 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
596 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
597 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
598 * packets untagged/tagged.
600 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
601 ICE_AQ_VSI_VLAN_MODE_M) >>
602 ICE_AQ_VSI_VLAN_MODE_S);
603 /* Have 1:1 UP mapping for both ingress/egress tables */
604 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
605 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
606 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
607 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
608 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
609 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
610 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
611 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
612 ctxt->info.ingress_table = cpu_to_le32(table);
613 ctxt->info.egress_table = cpu_to_le32(table);
614 /* Have 1:1 UP mapping for outer to inner UP table */
615 ctxt->info.outer_up_table = cpu_to_le32(table);
616 /* No Outer tag support outer_tag_flags remains to zero */
620 * ice_vsi_setup_q_map - Setup a VSI queue map
621 * @vsi: the VSI being configured
622 * @ctxt: VSI context structure
624 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
626 u16 offset = 0, qmap = 0, tx_count = 0;
627 u16 qcount_tx = vsi->alloc_txq;
628 u16 qcount_rx = vsi->alloc_rxq;
629 u16 tx_numq_tc, rx_numq_tc;
630 u16 pow = 0, max_rss = 0;
631 bool ena_tc0 = false;
635 /* at least TC0 should be enabled by default */
636 if (vsi->tc_cfg.numtc) {
637 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
645 vsi->tc_cfg.ena_tc |= 1;
648 rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
651 tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
655 /* TC mapping is a function of the number of Rx queues assigned to the
656 * VSI for each traffic class and the offset of these queues.
657 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
658 * queues allocated to TC0. No:of queues is a power-of-2.
660 * If TC is not enabled, the queue offset is set to 0, and allocate one
661 * queue, this way, traffic for the given TC will be sent to the default
664 * Setup number and offset of Rx queues for all TCs for the VSI
667 qcount_rx = rx_numq_tc;
669 /* qcount will change if RSS is enabled */
670 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
671 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
672 if (vsi->type == ICE_VSI_PF)
673 max_rss = ICE_MAX_LG_RSS_QS;
675 max_rss = ICE_MAX_SMALL_RSS_QS;
676 qcount_rx = min_t(int, rx_numq_tc, max_rss);
678 qcount_rx = min_t(int, qcount_rx,
683 /* find the (rounded up) power-of-2 of qcount */
684 pow = order_base_2(qcount_rx);
686 ice_for_each_traffic_class(i) {
687 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
688 /* TC is not enabled */
689 vsi->tc_cfg.tc_info[i].qoffset = 0;
690 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
691 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
692 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
693 ctxt->info.tc_mapping[i] = 0;
698 vsi->tc_cfg.tc_info[i].qoffset = offset;
699 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
700 vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
701 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
703 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
704 ICE_AQ_VSI_TC_Q_OFFSET_M) |
705 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
706 ICE_AQ_VSI_TC_Q_NUM_M);
708 tx_count += tx_numq_tc;
709 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
712 /* if offset is non-zero, means it is calculated correctly based on
713 * enabled TCs for a given VSI otherwise qcount_rx will always
714 * be correct and non-zero because it is based off - VSI's
715 * allocated Rx queues which is at least 1 (hence qcount_tx will be
719 vsi->num_rxq = offset;
721 vsi->num_rxq = qcount_rx;
723 vsi->num_txq = tx_count;
725 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
726 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
727 /* since there is a chance that num_rxq could have been changed
728 * in the above for loop, make num_txq equal to num_rxq.
730 vsi->num_txq = vsi->num_rxq;
733 /* Rx queue mapping */
734 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
735 /* q_mapping buffer holds the info for the first queue allocated for
736 * this VSI in the PF space and also the number of queues associated
739 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
740 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
744 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
745 * @ctxt: the VSI context being set
746 * @vsi: the VSI being configured
748 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
750 u8 lut_type, hash_type;
755 dev = ice_pf_to_dev(pf);
759 /* PF VSI will inherit RSS instance of PF */
760 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
761 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
764 /* VF VSI will gets a small RSS table which is a VSI LUT type */
765 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
766 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
769 dev_dbg(dev, "Unsupported VSI type %s\n",
770 ice_vsi_type_str(vsi->type));
773 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
777 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
778 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
779 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
780 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
784 * ice_vsi_init - Create and initialize a VSI
785 * @vsi: the VSI being configured
786 * @init_vsi: is this call creating a VSI
788 * This initializes a VSI context depending on the VSI type to be added and
789 * passes it down to the add_vsi aq command to create a new VSI.
791 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
793 struct ice_pf *pf = vsi->back;
794 struct ice_hw *hw = &pf->hw;
795 struct ice_vsi_ctx *ctxt;
799 dev = ice_pf_to_dev(pf);
800 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
804 ctxt->info = vsi->info;
809 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
812 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
813 /* VF number here is the absolute VF number (0-255) */
814 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
821 ice_set_dflt_vsi_ctx(ctxt);
822 /* if the switch is in VEB mode, allow VSI loopback */
823 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
824 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
826 /* Set LUT type and HASH type if RSS is enabled */
827 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
828 ice_set_rss_vsi_ctx(ctxt, vsi);
829 /* if updating VSI context, make sure to set valid_section:
830 * to indicate which section of VSI context being updated
833 ctxt->info.valid_sections |=
834 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
837 ctxt->info.sw_id = vsi->port_info->sw_id;
838 ice_vsi_setup_q_map(vsi, ctxt);
839 if (!init_vsi) /* means VSI being updated */
840 /* must to indicate which section of VSI context are
843 ctxt->info.valid_sections |=
844 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
846 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
849 if (vsi->type == ICE_VSI_VF) {
850 ctxt->info.valid_sections |=
851 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
852 if (pf->vf[vsi->vf_id].spoofchk) {
853 ctxt->info.sec_flags |=
854 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
855 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
856 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
858 ctxt->info.sec_flags &=
859 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
860 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
861 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
865 /* Allow control frames out of main VSI */
866 if (vsi->type == ICE_VSI_PF) {
867 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
868 ctxt->info.valid_sections |=
869 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
873 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
875 dev_err(dev, "Add VSI failed, err %d\n", ret);
880 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
882 dev_err(dev, "Update VSI failed, err %d\n", ret);
888 /* keep context for update VSI operations */
889 vsi->info = ctxt->info;
891 /* record VSI number returned */
892 vsi->vsi_num = ctxt->vsi_num;
900 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
901 * @vsi: ptr to the VSI
903 * This should only be called after ice_vsi_alloc() which allocates the
904 * corresponding SW VSI structure and initializes num_queue_pairs for the
905 * newly allocated VSI.
907 * Returns 0 on success or negative on failure
909 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
911 struct ice_pf *pf = vsi->back;
915 dev = ice_pf_to_dev(pf);
916 /* SRIOV doesn't grab irq_tracker entries for each VSI */
917 if (vsi->type == ICE_VSI_VF)
920 if (vsi->base_vector) {
921 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
922 vsi->vsi_num, vsi->base_vector);
926 num_q_vectors = vsi->num_q_vectors;
927 /* reserve slots from OS requested IRQs */
928 vsi->base_vector = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
930 if (vsi->base_vector < 0) {
931 dev_err(dev, "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
932 num_q_vectors, vsi->vsi_num, vsi->base_vector);
935 pf->num_avail_sw_msix -= num_q_vectors;
941 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
942 * @vsi: the VSI having rings deallocated
944 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
949 for (i = 0; i < vsi->alloc_txq; i++) {
950 if (vsi->tx_rings[i]) {
951 kfree_rcu(vsi->tx_rings[i], rcu);
952 vsi->tx_rings[i] = NULL;
957 for (i = 0; i < vsi->alloc_rxq; i++) {
958 if (vsi->rx_rings[i]) {
959 kfree_rcu(vsi->rx_rings[i], rcu);
960 vsi->rx_rings[i] = NULL;
967 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
968 * @vsi: VSI which is having rings allocated
970 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
972 struct ice_pf *pf = vsi->back;
976 dev = ice_pf_to_dev(pf);
977 /* Allocate Tx rings */
978 for (i = 0; i < vsi->alloc_txq; i++) {
979 struct ice_ring *ring;
981 /* allocate with kzalloc(), free with kfree_rcu() */
982 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
988 ring->reg_idx = vsi->txq_map[i];
989 ring->ring_active = false;
992 ring->count = vsi->num_tx_desc;
993 vsi->tx_rings[i] = ring;
996 /* Allocate Rx rings */
997 for (i = 0; i < vsi->alloc_rxq; i++) {
998 struct ice_ring *ring;
1000 /* allocate with kzalloc(), free with kfree_rcu() */
1001 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1006 ring->reg_idx = vsi->rxq_map[i];
1007 ring->ring_active = false;
1009 ring->netdev = vsi->netdev;
1011 ring->count = vsi->num_rx_desc;
1012 vsi->rx_rings[i] = ring;
1018 ice_vsi_clear_rings(vsi);
1023 * ice_vsi_manage_rss_lut - disable/enable RSS
1024 * @vsi: the VSI being changed
1025 * @ena: boolean value indicating if this is an enable or disable request
1027 * In the event of disable request for RSS, this function will zero out RSS
1028 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1031 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1036 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1041 if (vsi->rss_lut_user)
1042 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1044 ice_fill_rss_lut(lut, vsi->rss_table_size,
1048 err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1054 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1055 * @vsi: VSI to be configured
1057 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1059 struct ice_aqc_get_set_rss_keys *key;
1060 struct ice_pf *pf = vsi->back;
1061 enum ice_status status;
1066 dev = ice_pf_to_dev(pf);
1067 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
1069 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1073 if (vsi->rss_lut_user)
1074 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1076 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1078 status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
1079 vsi->rss_table_size);
1082 dev_err(dev, "set_rss_lut failed, error %d\n", status);
1084 goto ice_vsi_cfg_rss_exit;
1087 key = kzalloc(sizeof(*key), GFP_KERNEL);
1090 goto ice_vsi_cfg_rss_exit;
1093 if (vsi->rss_hkey_user)
1095 (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
1096 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1098 netdev_rss_key_fill((void *)key,
1099 ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1101 status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1104 dev_err(dev, "set_rss_key failed, error %d\n", status);
1109 ice_vsi_cfg_rss_exit:
1115 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1116 * @vsi: VSI to be configured
1118 * This function will only be called during the VF VSI setup. Upon successful
1119 * completion of package download, this function will configure default RSS
1120 * input sets for VF VSI.
1122 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1124 struct ice_pf *pf = vsi->back;
1125 enum ice_status status;
1128 dev = ice_pf_to_dev(pf);
1129 if (ice_is_safe_mode(pf)) {
1130 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1135 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1137 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1138 vsi->vsi_num, status);
1142 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1143 * @vsi: VSI to be configured
1145 * This function will only be called after successful download package call
1146 * during initialization of PF. Since the downloaded package will erase the
1147 * RSS section, this function will configure RSS input sets for different
1148 * flow types. The last profile added has the highest priority, therefore 2
1149 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1150 * (i.e. IPv4 src/dst TCP src/dst port).
1152 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1154 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1155 struct ice_pf *pf = vsi->back;
1156 struct ice_hw *hw = &pf->hw;
1157 enum ice_status status;
1160 dev = ice_pf_to_dev(pf);
1161 if (ice_is_safe_mode(pf)) {
1162 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1166 /* configure RSS for IPv4 with input set IP src/dst */
1167 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1168 ICE_FLOW_SEG_HDR_IPV4);
1170 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n",
1173 /* configure RSS for IPv6 with input set IPv6 src/dst */
1174 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1175 ICE_FLOW_SEG_HDR_IPV6);
1177 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n",
1180 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1181 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1182 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1184 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n",
1187 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1188 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1189 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1191 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n",
1194 /* configure RSS for sctp4 with input set IP src/dst */
1195 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1196 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1198 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n",
1201 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1202 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1203 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1205 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n",
1208 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1209 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1210 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1212 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n",
1215 /* configure RSS for sctp6 with input set IPv6 src/dst */
1216 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1217 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1219 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n",
1224 * ice_add_mac_to_list - Add a MAC address filter entry to the list
1225 * @vsi: the VSI to be forwarded to
1226 * @add_list: pointer to the list which contains MAC filter entries
1227 * @macaddr: the MAC address to be added.
1229 * Adds MAC address filter entry to the temp list
1231 * Returns 0 on success or ENOMEM on failure.
1234 ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
1237 struct ice_fltr_list_entry *tmp;
1238 struct ice_pf *pf = vsi->back;
1240 tmp = devm_kzalloc(ice_pf_to_dev(pf), sizeof(*tmp), GFP_ATOMIC);
1244 tmp->fltr_info.flag = ICE_FLTR_TX;
1245 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1246 tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
1247 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1248 tmp->fltr_info.vsi_handle = vsi->idx;
1249 ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
1251 INIT_LIST_HEAD(&tmp->list_entry);
1252 list_add(&tmp->list_entry, add_list);
1258 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1259 * @vsi: the VSI to be updated
1261 void ice_update_eth_stats(struct ice_vsi *vsi)
1263 struct ice_eth_stats *prev_es, *cur_es;
1264 struct ice_hw *hw = &vsi->back->hw;
1265 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1267 prev_es = &vsi->eth_stats_prev;
1268 cur_es = &vsi->eth_stats;
1270 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1271 &prev_es->rx_bytes, &cur_es->rx_bytes);
1273 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1274 &prev_es->rx_unicast, &cur_es->rx_unicast);
1276 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1277 &prev_es->rx_multicast, &cur_es->rx_multicast);
1279 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1280 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1282 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1283 &prev_es->rx_discards, &cur_es->rx_discards);
1285 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1286 &prev_es->tx_bytes, &cur_es->tx_bytes);
1288 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1289 &prev_es->tx_unicast, &cur_es->tx_unicast);
1291 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1292 &prev_es->tx_multicast, &cur_es->tx_multicast);
1294 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1295 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1297 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1298 &prev_es->tx_errors, &cur_es->tx_errors);
1300 vsi->stat_offsets_loaded = true;
1304 * ice_free_fltr_list - free filter lists helper
1305 * @dev: pointer to the device struct
1306 * @h: pointer to the list head to be freed
1308 * Helper function to free filter lists previously created using
1309 * ice_add_mac_to_list
1311 void ice_free_fltr_list(struct device *dev, struct list_head *h)
1313 struct ice_fltr_list_entry *e, *tmp;
1315 list_for_each_entry_safe(e, tmp, h, list_entry) {
1316 list_del(&e->list_entry);
1322 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1323 * @vsi: the VSI being configured
1324 * @vid: VLAN ID to be added
1326 int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
1328 struct ice_fltr_list_entry *tmp;
1329 struct ice_pf *pf = vsi->back;
1330 LIST_HEAD(tmp_add_list);
1331 enum ice_status status;
1335 dev = ice_pf_to_dev(pf);
1336 tmp = devm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
1340 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1341 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1342 tmp->fltr_info.flag = ICE_FLTR_TX;
1343 tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
1344 tmp->fltr_info.vsi_handle = vsi->idx;
1345 tmp->fltr_info.l_data.vlan.vlan_id = vid;
1347 INIT_LIST_HEAD(&tmp->list_entry);
1348 list_add(&tmp->list_entry, &tmp_add_list);
1350 status = ice_add_vlan(&pf->hw, &tmp_add_list);
1353 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1357 ice_free_fltr_list(dev, &tmp_add_list);
1362 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1363 * @vsi: the VSI being configured
1364 * @vid: VLAN ID to be removed
1366 * Returns 0 on success and negative on failure
1368 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1370 struct ice_fltr_list_entry *list;
1371 struct ice_pf *pf = vsi->back;
1372 LIST_HEAD(tmp_add_list);
1373 enum ice_status status;
1377 dev = ice_pf_to_dev(pf);
1378 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1382 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1383 list->fltr_info.vsi_handle = vsi->idx;
1384 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1385 list->fltr_info.l_data.vlan.vlan_id = vid;
1386 list->fltr_info.flag = ICE_FLTR_TX;
1387 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1389 INIT_LIST_HEAD(&list->list_entry);
1390 list_add(&list->list_entry, &tmp_add_list);
1392 status = ice_remove_vlan(&pf->hw, &tmp_add_list);
1393 if (status == ICE_ERR_DOES_NOT_EXIST) {
1394 dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %d\n",
1395 vid, vsi->vsi_num, status);
1396 } else if (status) {
1397 dev_err(dev, "Error removing VLAN %d on vsi %i error: %d\n",
1398 vid, vsi->vsi_num, status);
1402 ice_free_fltr_list(dev, &tmp_add_list);
1407 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1410 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1412 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1413 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1414 vsi->rx_buf_len = ICE_RXBUF_2048;
1415 #if (PAGE_SIZE < 8192)
1416 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1417 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1418 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1419 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1422 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1423 #if (PAGE_SIZE < 8192)
1424 vsi->rx_buf_len = ICE_RXBUF_3072;
1426 vsi->rx_buf_len = ICE_RXBUF_2048;
1432 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1433 * @vsi: the VSI being configured
1435 * Return 0 on success and a negative value on error
1436 * Configure the Rx VSI for operation.
1438 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1442 if (vsi->type == ICE_VSI_VF)
1445 ice_vsi_cfg_frame_size(vsi);
1447 /* set up individual rings */
1448 for (i = 0; i < vsi->num_rxq; i++) {
1451 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1453 dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1463 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1464 * @vsi: the VSI being configured
1465 * @rings: Tx ring array to be configured
1467 * Return 0 on success and a negative value on error
1468 * Configure the Tx VSI for operation.
1471 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1473 struct ice_aqc_add_tx_qgrp *qg_buf;
1477 qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1481 qg_buf->num_txqs = 1;
1483 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1484 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1495 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1496 * @vsi: the VSI being configured
1498 * Return 0 on success and a negative value on error
1499 * Configure the Tx VSI for operation.
1501 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1503 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1507 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1508 * @vsi: the VSI being configured
1510 * Return 0 on success and a negative value on error
1511 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1513 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1518 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1522 for (i = 0; i < vsi->num_xdp_txq; i++)
1523 vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);
1529 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1530 * @intrl: interrupt rate limit in usecs
1531 * @gran: interrupt rate limit granularity in usecs
1533 * This function converts a decimal interrupt rate limit in usecs to the format
1534 * expected by firmware.
1536 u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1538 u32 val = intrl / gran;
1541 return val | GLINT_RATE_INTRL_ENA_M;
1546 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1547 * @vsi: the VSI being configured
1549 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1552 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1554 struct ice_pf *pf = vsi->back;
1555 struct ice_hw *hw = &pf->hw;
1556 u32 txq = 0, rxq = 0;
1559 for (i = 0; i < vsi->num_q_vectors; i++) {
1560 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1561 u16 reg_idx = q_vector->reg_idx;
1563 ice_cfg_itr(hw, q_vector);
1565 wr32(hw, GLINT_RATE(reg_idx),
1566 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1568 /* Both Transmit Queue Interrupt Cause Control register
1569 * and Receive Queue Interrupt Cause control register
1570 * expects MSIX_INDX field to be the vector index
1571 * within the function space and not the absolute
1572 * vector index across PF or across device.
1573 * For SR-IOV VF VSIs queue vector index always starts
1574 * with 1 since first vector index(0) is used for OICR
1575 * in VF space. Since VMDq and other PF VSIs are within
1576 * the PF function space, use the vector index that is
1577 * tracked for this PF.
1579 for (q = 0; q < q_vector->num_ring_tx; q++) {
1580 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1581 q_vector->tx.itr_idx);
1585 for (q = 0; q < q_vector->num_ring_rx; q++) {
1586 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1587 q_vector->rx.itr_idx);
1594 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1595 * @vsi: the VSI being changed
1597 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1599 struct ice_hw *hw = &vsi->back->hw;
1600 struct ice_vsi_ctx *ctxt;
1601 enum ice_status status;
1604 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1608 /* Here we are configuring the VSI to let the driver add VLAN tags by
1609 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1610 * insertion happens in the Tx hot path, in ice_tx_map.
1612 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1614 /* Preserve existing VLAN strip setting */
1615 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1616 ICE_AQ_VSI_VLAN_EMOD_M);
1618 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1620 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1622 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %d aq_err %d\n",
1623 status, hw->adminq.sq_last_status);
1628 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1635 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1636 * @vsi: the VSI being changed
1637 * @ena: boolean value indicating if this is a enable or disable request
1639 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1641 struct ice_hw *hw = &vsi->back->hw;
1642 struct ice_vsi_ctx *ctxt;
1643 enum ice_status status;
1646 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1650 /* Here we are configuring what the VSI should do with the VLAN tag in
1651 * the Rx packet. We can either leave the tag in the packet or put it in
1652 * the Rx descriptor.
1655 /* Strip VLAN tag from Rx packet and put it in the desc */
1656 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1658 /* Disable stripping. Leave tag in packet */
1659 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1661 /* Allow all packets untagged/tagged */
1662 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1664 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1666 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1668 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
1669 ena, status, hw->adminq.sq_last_status);
1674 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1681 * ice_vsi_start_rx_rings - start VSI's Rx rings
1682 * @vsi: the VSI whose rings are to be started
1684 * Returns 0 on success and a negative value on error
1686 int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
1688 return ice_vsi_ctrl_rx_rings(vsi, true);
1692 * ice_vsi_stop_rx_rings - stop VSI's Rx rings
1695 * Returns 0 on success and a negative value on error
1697 int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
1699 return ice_vsi_ctrl_rx_rings(vsi, false);
1703 * ice_vsi_stop_tx_rings - Disable Tx rings
1704 * @vsi: the VSI being configured
1705 * @rst_src: reset source
1706 * @rel_vmvf_num: Relative ID of VF/VM
1707 * @rings: Tx ring array to be stopped
1710 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1711 u16 rel_vmvf_num, struct ice_ring **rings)
1715 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1718 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1719 struct ice_txq_meta txq_meta = { };
1722 if (!rings || !rings[q_idx])
1725 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1726 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1727 rings[q_idx], &txq_meta);
1737 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1738 * @vsi: the VSI being configured
1739 * @rst_src: reset source
1740 * @rel_vmvf_num: Relative ID of VF/VM
1743 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1746 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1750 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
1751 * @vsi: the VSI being configured
1753 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
1755 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
1759 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
1760 * @vsi: VSI to enable or disable VLAN pruning on
1761 * @ena: set to true to enable VLAN pruning and false to disable it
1762 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1764 * returns 0 if VSI is updated, negative otherwise
1766 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1768 struct ice_vsi_ctx *ctxt;
1776 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1780 ctxt->info = vsi->info;
1783 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1785 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1788 ctxt->info.valid_sections =
1789 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
1791 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1793 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
1794 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
1795 pf->hw.adminq.sq_last_status);
1799 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
1809 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
1811 struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;
1813 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
1814 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
1818 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
1819 * @vsi: VSI to set the q_vectors register index on
1822 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
1826 if (!vsi || !vsi->q_vectors)
1829 ice_for_each_q_vector(vsi, i) {
1830 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1833 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
1838 if (vsi->type == ICE_VSI_VF) {
1839 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
1841 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
1844 q_vector->v_idx + vsi->base_vector;
1851 ice_for_each_q_vector(vsi, i) {
1852 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1855 q_vector->reg_idx = 0;
1862 * ice_vsi_add_rem_eth_mac - Program VSI ethertype based filter with rule
1863 * @vsi: the VSI being configured
1864 * @add_rule: boolean value to add or remove ethertype filter rule
1867 ice_vsi_add_rem_eth_mac(struct ice_vsi *vsi, bool add_rule)
1869 struct ice_fltr_list_entry *list;
1870 struct ice_pf *pf = vsi->back;
1871 LIST_HEAD(tmp_add_list);
1872 enum ice_status status;
1875 dev = ice_pf_to_dev(pf);
1876 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1880 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1881 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1882 list->fltr_info.flag = ICE_FLTR_TX;
1883 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1884 list->fltr_info.vsi_handle = vsi->idx;
1885 list->fltr_info.l_data.ethertype_mac.ethertype = vsi->ethtype;
1887 INIT_LIST_HEAD(&list->list_entry);
1888 list_add(&list->list_entry, &tmp_add_list);
1891 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1893 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1896 dev_err(dev, "Failure Adding or Removing Ethertype on VSI %i error: %d\n",
1897 vsi->vsi_num, status);
1899 ice_free_fltr_list(dev, &tmp_add_list);
1903 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
1904 * @vsi: the VSI being configured
1905 * @tx: bool to determine Tx or Rx rule
1906 * @create: bool to determine create or remove Rule
1908 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
1910 struct ice_fltr_list_entry *list;
1911 struct ice_pf *pf = vsi->back;
1912 LIST_HEAD(tmp_add_list);
1913 enum ice_status status;
1916 dev = ice_pf_to_dev(pf);
1917 list = devm_kzalloc(dev, sizeof(*list), GFP_KERNEL);
1921 list->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1922 list->fltr_info.vsi_handle = vsi->idx;
1923 list->fltr_info.l_data.ethertype_mac.ethertype = ETH_P_LLDP;
1926 list->fltr_info.fltr_act = ICE_DROP_PACKET;
1927 list->fltr_info.flag = ICE_FLTR_TX;
1928 list->fltr_info.src_id = ICE_SRC_ID_VSI;
1930 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1931 list->fltr_info.flag = ICE_FLTR_RX;
1932 list->fltr_info.src_id = ICE_SRC_ID_LPORT;
1935 INIT_LIST_HEAD(&list->list_entry);
1936 list_add(&list->list_entry, &tmp_add_list);
1939 status = ice_add_eth_mac(&pf->hw, &tmp_add_list);
1941 status = ice_remove_eth_mac(&pf->hw, &tmp_add_list);
1944 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
1945 create ? "adding" : "removing", tx ? "TX" : "RX",
1946 vsi->vsi_num, status);
1948 ice_free_fltr_list(dev, &tmp_add_list);
1952 * ice_vsi_setup - Set up a VSI by a given type
1953 * @pf: board private structure
1954 * @pi: pointer to the port_info instance
1956 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1957 * used only for ICE_VSI_VF VSI type. For other VSI types, should
1958 * fill-in ICE_INVAL_VFID as input.
1960 * This allocates the sw VSI structure and its queue resources.
1962 * Returns pointer to the successfully allocated and configured VSI sw struct on
1963 * success, NULL on failure.
1966 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1967 enum ice_vsi_type type, u16 vf_id)
1969 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1970 struct device *dev = ice_pf_to_dev(pf);
1971 enum ice_status status;
1972 struct ice_vsi *vsi;
1975 if (type == ICE_VSI_VF)
1976 vsi = ice_vsi_alloc(pf, type, vf_id);
1978 vsi = ice_vsi_alloc(pf, type, ICE_INVAL_VFID);
1981 dev_err(dev, "could not allocate VSI\n");
1985 vsi->port_info = pi;
1986 vsi->vsw = pf->first_sw;
1987 if (vsi->type == ICE_VSI_PF)
1988 vsi->ethtype = ETH_P_PAUSE;
1990 if (vsi->type == ICE_VSI_VF)
1993 if (ice_vsi_get_qs(vsi)) {
1994 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
1999 /* set RSS capabilities */
2000 ice_vsi_set_rss_params(vsi);
2002 /* set TC configuration */
2003 ice_vsi_set_tc_cfg(vsi);
2005 /* create the VSI */
2006 ret = ice_vsi_init(vsi, true);
2010 switch (vsi->type) {
2012 ret = ice_vsi_alloc_q_vectors(vsi);
2014 goto unroll_vsi_init;
2016 ret = ice_vsi_setup_vector_base(vsi);
2018 goto unroll_alloc_q_vector;
2020 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2022 goto unroll_vector_base;
2024 ret = ice_vsi_alloc_rings(vsi);
2026 goto unroll_vector_base;
2028 ice_vsi_map_rings_to_vectors(vsi);
2030 /* Do not exit if configuring RSS had an issue, at least
2031 * receive traffic on first queue. Hence no need to capture
2034 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2035 ice_vsi_cfg_rss_lut_key(vsi);
2036 ice_vsi_set_rss_flow_fld(vsi);
2040 /* VF driver will take care of creating netdev for this type and
2041 * map queues to vectors through Virtchnl, PF driver only
2042 * creates a VSI and corresponding structures for bookkeeping
2045 ret = ice_vsi_alloc_q_vectors(vsi);
2047 goto unroll_vsi_init;
2049 ret = ice_vsi_alloc_rings(vsi);
2051 goto unroll_alloc_q_vector;
2053 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2055 goto unroll_vector_base;
2057 /* Do not exit if configuring RSS had an issue, at least
2058 * receive traffic on first queue. Hence no need to capture
2061 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2062 ice_vsi_cfg_rss_lut_key(vsi);
2063 ice_vsi_set_vf_rss_flow_fld(vsi);
2067 ret = ice_vsi_alloc_rings(vsi);
2069 goto unroll_vsi_init;
2072 /* clean up the resources and exit */
2073 goto unroll_vsi_init;
2076 /* configure VSI nodes based on number of queues and TC's */
2077 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2078 max_txqs[i] = vsi->alloc_txq;
2080 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2083 dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2084 vsi->vsi_num, status);
2085 goto unroll_vector_base;
2088 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2089 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2090 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2091 * The rule is added once for PF VSI in order to create appropriate
2092 * recipe, since VSI/VSI list is ignored with drop action...
2093 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2094 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2095 * settings in the HW.
2097 if (!ice_is_safe_mode(pf))
2098 if (vsi->type == ICE_VSI_PF) {
2099 ice_vsi_add_rem_eth_mac(vsi, true);
2101 /* Tx LLDP packets */
2102 ice_cfg_sw_lldp(vsi, true, true);
2108 /* reclaim SW interrupts back to the common pool */
2109 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2110 pf->num_avail_sw_msix += vsi->num_q_vectors;
2111 unroll_alloc_q_vector:
2112 ice_vsi_free_q_vectors(vsi);
2114 ice_vsi_delete(vsi);
2116 ice_vsi_put_qs(vsi);
2123 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2124 * @vsi: the VSI being cleaned up
2126 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2128 struct ice_pf *pf = vsi->back;
2129 struct ice_hw *hw = &pf->hw;
2134 for (i = 0; i < vsi->num_q_vectors; i++) {
2135 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2136 u16 reg_idx = q_vector->reg_idx;
2138 wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
2139 wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2140 for (q = 0; q < q_vector->num_ring_tx; q++) {
2141 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2142 if (ice_is_xdp_ena_vsi(vsi)) {
2143 u32 xdp_txq = txq + vsi->num_xdp_txq;
2145 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2150 for (q = 0; q < q_vector->num_ring_rx; q++) {
2151 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2160 * ice_vsi_free_irq - Free the IRQ association with the OS
2161 * @vsi: the VSI being configured
2163 void ice_vsi_free_irq(struct ice_vsi *vsi)
2165 struct ice_pf *pf = vsi->back;
2166 int base = vsi->base_vector;
2169 if (!vsi->q_vectors || !vsi->irqs_ready)
2172 ice_vsi_release_msix(vsi);
2173 if (vsi->type == ICE_VSI_VF)
2176 vsi->irqs_ready = false;
2177 ice_for_each_q_vector(vsi, i) {
2178 u16 vector = i + base;
2181 irq_num = pf->msix_entries[vector].vector;
2183 /* free only the irqs that were actually requested */
2184 if (!vsi->q_vectors[i] ||
2185 !(vsi->q_vectors[i]->num_ring_tx ||
2186 vsi->q_vectors[i]->num_ring_rx))
2189 /* clear the affinity notifier in the IRQ descriptor */
2190 irq_set_affinity_notifier(irq_num, NULL);
2192 /* clear the affinity_mask in the IRQ descriptor */
2193 irq_set_affinity_hint(irq_num, NULL);
2194 synchronize_irq(irq_num);
2195 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2200 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2201 * @vsi: the VSI having resources freed
2203 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2210 ice_for_each_txq(vsi, i)
2211 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2212 ice_free_tx_ring(vsi->tx_rings[i]);
2216 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2217 * @vsi: the VSI having resources freed
2219 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2226 ice_for_each_rxq(vsi, i)
2227 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2228 ice_free_rx_ring(vsi->rx_rings[i]);
2232 * ice_vsi_close - Shut down a VSI
2233 * @vsi: the VSI being shut down
2235 void ice_vsi_close(struct ice_vsi *vsi)
2237 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
2240 ice_vsi_free_irq(vsi);
2241 ice_vsi_free_tx_rings(vsi);
2242 ice_vsi_free_rx_rings(vsi);
2246 * ice_ena_vsi - resume a VSI
2247 * @vsi: the VSI being resume
2248 * @locked: is the rtnl_lock already held
2250 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2254 if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
2257 clear_bit(__ICE_NEEDS_RESTART, vsi->state);
2259 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2260 if (netif_running(vsi->netdev)) {
2264 err = ice_open(vsi->netdev);
2275 * ice_dis_vsi - pause a VSI
2276 * @vsi: the VSI being paused
2277 * @locked: is the rtnl_lock already held
2279 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2281 if (test_bit(__ICE_DOWN, vsi->state))
2284 set_bit(__ICE_NEEDS_RESTART, vsi->state);
2286 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2287 if (netif_running(vsi->netdev)) {
2291 ice_stop(vsi->netdev);
2302 * ice_free_res - free a block of resources
2303 * @res: pointer to the resource
2304 * @index: starting index previously returned by ice_get_res
2305 * @id: identifier to track owner
2307 * Returns number of resources freed
2309 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
2314 if (!res || index >= res->end)
2317 id |= ICE_RES_VALID_BIT;
2318 for (i = index; i < res->end && res->list[i] == id; i++) {
2327 * ice_search_res - Search the tracker for a block of resources
2328 * @res: pointer to the resource
2329 * @needed: size of the block needed
2330 * @id: identifier to track owner
2332 * Returns the base item index of the block, or -ENOMEM for error
2334 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
2336 int start = 0, end = 0;
2338 if (needed > res->end)
2341 id |= ICE_RES_VALID_BIT;
2344 /* skip already allocated entries */
2345 if (res->list[end++] & ICE_RES_VALID_BIT) {
2347 if ((start + needed) > res->end)
2351 if (end == (start + needed)) {
2354 /* there was enough, so assign it to the requestor */
2356 res->list[i++] = id;
2360 } while (end < res->end);
2366 * ice_get_res - get a block of resources
2367 * @pf: board private structure
2368 * @res: pointer to the resource
2369 * @needed: size of the block needed
2370 * @id: identifier to track owner
2372 * Returns the base item index of the block, or negative for error
2375 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
2380 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
2381 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
2382 needed, res->num_entries, id);
2386 return ice_search_res(res, needed, id);
2390 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2391 * @vsi: the VSI being un-configured
2393 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2395 int base = vsi->base_vector;
2396 struct ice_pf *pf = vsi->back;
2397 struct ice_hw *hw = &pf->hw;
2401 /* disable interrupt causation from each queue */
2402 if (vsi->tx_rings) {
2403 ice_for_each_txq(vsi, i) {
2404 if (vsi->tx_rings[i]) {
2407 reg = vsi->tx_rings[i]->reg_idx;
2408 val = rd32(hw, QINT_TQCTL(reg));
2409 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2410 wr32(hw, QINT_TQCTL(reg), val);
2415 if (vsi->rx_rings) {
2416 ice_for_each_rxq(vsi, i) {
2417 if (vsi->rx_rings[i]) {
2420 reg = vsi->rx_rings[i]->reg_idx;
2421 val = rd32(hw, QINT_RQCTL(reg));
2422 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2423 wr32(hw, QINT_RQCTL(reg), val);
2428 /* disable each interrupt */
2429 ice_for_each_q_vector(vsi, i) {
2430 if (!vsi->q_vectors[i])
2432 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2437 /* don't call synchronize_irq() for VF's from the host */
2438 if (vsi->type == ICE_VSI_VF)
2441 ice_for_each_q_vector(vsi, i)
2442 synchronize_irq(pf->msix_entries[i + base].vector);
2446 * ice_napi_del - Remove NAPI handler for the VSI
2447 * @vsi: VSI for which NAPI handler is to be removed
2449 void ice_napi_del(struct ice_vsi *vsi)
2456 ice_for_each_q_vector(vsi, v_idx)
2457 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2461 * ice_vsi_release - Delete a VSI and free its resources
2462 * @vsi: the VSI being removed
2464 * Returns 0 on success or < 0 on error
2466 int ice_vsi_release(struct ice_vsi *vsi)
2474 /* do not unregister while driver is in the reset recovery pending
2475 * state. Since reset/rebuild happens through PF service task workqueue,
2476 * it's not a good idea to unregister netdev that is associated to the
2477 * PF that is running the work queue items currently. This is done to
2478 * avoid check_flush_dependency() warning on this wq
2480 if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2481 unregister_netdev(vsi->netdev);
2483 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2486 /* Disable VSI and free resources */
2487 if (vsi->type != ICE_VSI_LB)
2488 ice_vsi_dis_irq(vsi);
2491 /* SR-IOV determines needed MSIX resources all at once instead of per
2492 * VSI since when VFs are spawned we know how many VFs there are and how
2493 * many interrupts each VF needs. SR-IOV MSIX resources are also
2494 * cleared in the same manner.
2496 if (vsi->type != ICE_VSI_VF) {
2497 /* reclaim SW interrupts back to the common pool */
2498 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2499 pf->num_avail_sw_msix += vsi->num_q_vectors;
2502 if (!ice_is_safe_mode(pf)) {
2503 if (vsi->type == ICE_VSI_PF) {
2504 ice_vsi_add_rem_eth_mac(vsi, false);
2505 ice_cfg_sw_lldp(vsi, true, false);
2506 /* The Rx rule will only exist to remove if the LLDP FW
2507 * engine is currently stopped
2509 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2510 ice_cfg_sw_lldp(vsi, false, false);
2514 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
2515 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2516 ice_vsi_delete(vsi);
2517 ice_vsi_free_q_vectors(vsi);
2519 /* make sure unregister_netdev() was called by checking __ICE_DOWN */
2520 if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
2521 free_netdev(vsi->netdev);
2525 ice_vsi_clear_rings(vsi);
2527 ice_vsi_put_qs(vsi);
2529 /* retain SW VSI data structure since it is needed to unregister and
2530 * free VSI netdev when PF is not in reset recovery pending state,\
2531 * for ex: during rmmod.
2533 if (!ice_is_reset_in_progress(pf->state))
2540 * ice_vsi_rebuild_update_coalesce - set coalesce for a q_vector
2541 * @q_vector: pointer to q_vector which is being updated
2542 * @coalesce: pointer to array of struct with stored coalesce
2544 * Set coalesce param in q_vector and update these parameters in HW.
2547 ice_vsi_rebuild_update_coalesce(struct ice_q_vector *q_vector,
2548 struct ice_coalesce_stored *coalesce)
2550 struct ice_ring_container *rx_rc = &q_vector->rx;
2551 struct ice_ring_container *tx_rc = &q_vector->tx;
2552 struct ice_hw *hw = &q_vector->vsi->back->hw;
2554 tx_rc->itr_setting = coalesce->itr_tx;
2555 rx_rc->itr_setting = coalesce->itr_rx;
2557 /* dynamic ITR values will be updated during Tx/Rx */
2558 if (!ITR_IS_DYNAMIC(tx_rc->itr_setting))
2559 wr32(hw, GLINT_ITR(tx_rc->itr_idx, q_vector->reg_idx),
2560 ITR_REG_ALIGN(tx_rc->itr_setting) >>
2562 if (!ITR_IS_DYNAMIC(rx_rc->itr_setting))
2563 wr32(hw, GLINT_ITR(rx_rc->itr_idx, q_vector->reg_idx),
2564 ITR_REG_ALIGN(rx_rc->itr_setting) >>
2567 q_vector->intrl = coalesce->intrl;
2568 wr32(hw, GLINT_RATE(q_vector->reg_idx),
2569 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
2573 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2574 * @vsi: VSI connected with q_vectors
2575 * @coalesce: array of struct with stored coalesce
2577 * Returns array size.
2580 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2581 struct ice_coalesce_stored *coalesce)
2585 ice_for_each_q_vector(vsi, i) {
2586 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2588 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2589 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2590 coalesce[i].intrl = q_vector->intrl;
2593 return vsi->num_q_vectors;
2597 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2598 * @vsi: VSI connected with q_vectors
2599 * @coalesce: pointer to array of struct with stored coalesce
2600 * @size: size of coalesce array
2602 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2603 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2607 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2608 struct ice_coalesce_stored *coalesce, int size)
2612 if ((size && !coalesce) || !vsi)
2615 for (i = 0; i < size && i < vsi->num_q_vectors; i++)
2616 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2619 for (; i < vsi->num_q_vectors; i++) {
2620 struct ice_coalesce_stored coalesce_dflt = {
2621 .itr_tx = ICE_DFLT_TX_ITR,
2622 .itr_rx = ICE_DFLT_RX_ITR,
2625 ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
2631 * ice_vsi_rebuild - Rebuild VSI after reset
2632 * @vsi: VSI to be rebuild
2633 * @init_vsi: is this an initialization or a reconfigure of the VSI
2635 * Returns 0 on success and negative value on failure
2637 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2639 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2640 struct ice_coalesce_stored *coalesce;
2641 int prev_num_q_vectors = 0;
2642 struct ice_vf *vf = NULL;
2643 enum ice_status status;
2651 if (vsi->type == ICE_VSI_VF)
2652 vf = &pf->vf[vsi->vf_id];
2654 coalesce = kcalloc(vsi->num_q_vectors,
2655 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
2657 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi,
2659 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2660 ice_vsi_free_q_vectors(vsi);
2662 /* SR-IOV determines needed MSIX resources all at once instead of per
2663 * VSI since when VFs are spawned we know how many VFs there are and how
2664 * many interrupts each VF needs. SR-IOV MSIX resources are also
2665 * cleared in the same manner.
2667 if (vsi->type != ICE_VSI_VF) {
2668 /* reclaim SW interrupts back to the common pool */
2669 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2670 pf->num_avail_sw_msix += vsi->num_q_vectors;
2671 vsi->base_vector = 0;
2674 if (ice_is_xdp_ena_vsi(vsi))
2675 /* return value check can be skipped here, it always returns
2676 * 0 if reset is in progress
2678 ice_destroy_xdp_rings(vsi);
2679 ice_vsi_put_qs(vsi);
2680 ice_vsi_clear_rings(vsi);
2681 ice_vsi_free_arrays(vsi);
2682 if (vsi->type == ICE_VSI_VF)
2683 ice_vsi_set_num_qs(vsi, vf->vf_id);
2685 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2687 ret = ice_vsi_alloc_arrays(vsi);
2691 ice_vsi_get_qs(vsi);
2692 ice_vsi_set_tc_cfg(vsi);
2694 /* Initialize VSI struct elements and create VSI in FW */
2695 ret = ice_vsi_init(vsi, init_vsi);
2699 switch (vsi->type) {
2701 ret = ice_vsi_alloc_q_vectors(vsi);
2705 ret = ice_vsi_setup_vector_base(vsi);
2709 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2713 ret = ice_vsi_alloc_rings(vsi);
2717 ice_vsi_map_rings_to_vectors(vsi);
2718 if (ice_is_xdp_ena_vsi(vsi)) {
2719 vsi->num_xdp_txq = vsi->alloc_txq;
2720 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
2724 /* Do not exit if configuring RSS had an issue, at least
2725 * receive traffic on first queue. Hence no need to capture
2728 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2729 ice_vsi_cfg_rss_lut_key(vsi);
2732 ret = ice_vsi_alloc_q_vectors(vsi);
2736 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2740 ret = ice_vsi_alloc_rings(vsi);
2749 /* configure VSI nodes based on number of queues and TC's */
2750 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2751 max_txqs[i] = vsi->alloc_txq;
2753 if (ice_is_xdp_ena_vsi(vsi))
2754 max_txqs[i] += vsi->num_xdp_txq;
2757 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2760 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %d\n",
2761 vsi->vsi_num, status);
2766 return ice_schedule_reset(pf, ICE_RESET_PFR);
2769 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
2775 ice_vsi_free_q_vectors(vsi);
2778 vsi->current_netdev_flags = 0;
2779 unregister_netdev(vsi->netdev);
2780 free_netdev(vsi->netdev);
2785 set_bit(__ICE_RESET_FAILED, pf->state);
2791 * ice_is_reset_in_progress - check for a reset in progress
2792 * @state: PF state field
2794 bool ice_is_reset_in_progress(unsigned long *state)
2796 return test_bit(__ICE_RESET_OICR_RECV, state) ||
2797 test_bit(__ICE_DCBNL_DEVRESET, state) ||
2798 test_bit(__ICE_PFR_REQ, state) ||
2799 test_bit(__ICE_CORER_REQ, state) ||
2800 test_bit(__ICE_GLOBR_REQ, state);
2805 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
2806 * @vsi: VSI being configured
2807 * @ctx: the context buffer returned from AQ VSI update command
2809 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
2811 vsi->info.mapping_flags = ctx->info.mapping_flags;
2812 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
2813 sizeof(vsi->info.q_mapping));
2814 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
2815 sizeof(vsi->info.tc_mapping));
2819 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
2820 * @vsi: VSI to be configured
2821 * @ena_tc: TC bitmap
2823 * VSI queues expected to be quiesced before calling this function
2825 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
2827 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2828 struct ice_pf *pf = vsi->back;
2829 struct ice_vsi_ctx *ctx;
2830 enum ice_status status;
2835 dev = ice_pf_to_dev(pf);
2837 ice_for_each_traffic_class(i) {
2838 /* build bitmap of enabled TCs */
2839 if (ena_tc & BIT(i))
2841 /* populate max_txqs per TC */
2842 max_txqs[i] = vsi->alloc_txq;
2845 vsi->tc_cfg.ena_tc = ena_tc;
2846 vsi->tc_cfg.numtc = num_tc;
2848 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2853 ctx->info = vsi->info;
2855 ice_vsi_setup_q_map(vsi, ctx);
2857 /* must to indicate which section of VSI context are being modified */
2858 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
2859 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2861 dev_info(dev, "Failed VSI Update\n");
2866 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2870 dev_err(dev, "VSI %d failed TC config, error %d\n",
2871 vsi->vsi_num, status);
2875 ice_vsi_update_q_map(vsi, ctx);
2876 vsi->info.valid_sections = 0;
2878 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
2883 #endif /* CONFIG_DCB */
2886 * ice_update_ring_stats - Update ring statistics
2887 * @ring: ring to update
2888 * @cont: used to increment per-vector counters
2889 * @pkts: number of processed packets
2890 * @bytes: number of processed bytes
2892 * This function assumes that caller has acquired a u64_stats_sync lock.
2895 ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
2896 u64 pkts, u64 bytes)
2898 ring->stats.bytes += bytes;
2899 ring->stats.pkts += pkts;
2900 cont->total_bytes += bytes;
2901 cont->total_pkts += pkts;
2905 * ice_update_tx_ring_stats - Update Tx ring specific counters
2906 * @tx_ring: ring to update
2907 * @pkts: number of processed packets
2908 * @bytes: number of processed bytes
2910 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
2912 u64_stats_update_begin(&tx_ring->syncp);
2913 ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
2914 u64_stats_update_end(&tx_ring->syncp);
2918 * ice_update_rx_ring_stats - Update Rx ring specific counters
2919 * @rx_ring: ring to update
2920 * @pkts: number of processed packets
2921 * @bytes: number of processed bytes
2923 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
2925 u64_stats_update_begin(&rx_ring->syncp);
2926 ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
2927 u64_stats_update_end(&rx_ring->syncp);
2931 * ice_vsi_cfg_mac_fltr - Add or remove a MAC address filter for a VSI
2932 * @vsi: the VSI being configured MAC filter
2933 * @macaddr: the MAC address to be added.
2934 * @set: Add or delete a MAC filter
2936 * Adds or removes MAC address filter entry for VF VSI
2939 ice_vsi_cfg_mac_fltr(struct ice_vsi *vsi, const u8 *macaddr, bool set)
2941 LIST_HEAD(tmp_add_list);
2942 enum ice_status status;
2944 /* Update MAC filter list to be added or removed for a VSI */
2945 if (ice_add_mac_to_list(vsi, &tmp_add_list, macaddr)) {
2946 status = ICE_ERR_NO_MEMORY;
2947 goto cfg_mac_fltr_exit;
2951 status = ice_add_mac(&vsi->back->hw, &tmp_add_list);
2953 status = ice_remove_mac(&vsi->back->hw, &tmp_add_list);
2956 ice_free_fltr_list(ice_pf_to_dev(vsi->back), &tmp_add_list);
2961 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
2962 * @sw: switch to check if its default forwarding VSI is free
2964 * Return true if the default forwarding VSI is already being used, else returns
2965 * false signalling that it's available to use.
2967 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
2969 return (sw->dflt_vsi && sw->dflt_vsi_ena);
2973 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
2974 * @sw: switch for the default forwarding VSI to compare against
2975 * @vsi: VSI to compare against default forwarding VSI
2977 * If this VSI passed in is the default forwarding VSI then return true, else
2980 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
2982 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
2986 * ice_set_dflt_vsi - set the default forwarding VSI
2987 * @sw: switch used to assign the default forwarding VSI
2988 * @vsi: VSI getting set as the default forwarding VSI on the switch
2990 * If the VSI passed in is already the default VSI and it's enabled just return
2993 * If there is already a default VSI on the switch and it's enabled then return
2994 * -EEXIST since there can only be one default VSI per switch.
2996 * Otherwise try to set the VSI passed in as the switch's default VSI and
2997 * return the result.
2999 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3001 enum ice_status status;
3007 dev = ice_pf_to_dev(vsi->back);
3009 /* the VSI passed in is already the default VSI */
3010 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3011 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3016 /* another VSI is already the default VSI for this switch */
3017 if (ice_is_dflt_vsi_in_use(sw)) {
3018 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3019 sw->dflt_vsi->vsi_num);
3023 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3025 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3026 vsi->vsi_num, status);
3031 sw->dflt_vsi_ena = true;
3037 * ice_clear_dflt_vsi - clear the default forwarding VSI
3038 * @sw: switch used to clear the default VSI
3040 * If the switch has no default VSI or it's not enabled then return error.
3042 * Otherwise try to clear the default VSI and return the result.
3044 int ice_clear_dflt_vsi(struct ice_sw *sw)
3046 struct ice_vsi *dflt_vsi;
3047 enum ice_status status;
3053 dev = ice_pf_to_dev(sw->pf);
3055 dflt_vsi = sw->dflt_vsi;
3057 /* there is no default VSI configured */
3058 if (!ice_is_dflt_vsi_in_use(sw))
3061 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3064 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3065 dflt_vsi->vsi_num, status);
3069 sw->dflt_vsi = NULL;
3070 sw->dflt_vsi_ena = false;