1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 #include "ice_dcb_lib.h"
10 #include "ice_devlink.h"
13 * ice_vsi_type_str - maps VSI type enum to string equivalents
14 * @vsi_type: VSI type enum
16 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
24 return "ICE_VSI_CTRL";
33 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
34 * @vsi: the VSI being configured
35 * @ena: start or stop the Rx rings
37 * First enable/disable all of the Rx rings, flush any remaining writes, and
38 * then verify that they have all been enabled/disabled successfully. This will
39 * let all of the register writes complete when enabling/disabling the Rx rings
40 * before waiting for the change in hardware to complete.
42 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
47 for (i = 0; i < vsi->num_rxq; i++)
48 ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
50 ice_flush(&vsi->back->hw);
52 for (i = 0; i < vsi->num_rxq; i++) {
53 ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
62 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
65 * On error: returns error code (negative)
66 * On success: returns 0
68 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
70 struct ice_pf *pf = vsi->back;
73 dev = ice_pf_to_dev(pf);
75 /* allocate memory for both Tx and Rx ring pointers */
76 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
77 sizeof(*vsi->tx_rings), GFP_KERNEL);
81 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
82 sizeof(*vsi->rx_rings), GFP_KERNEL);
86 /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
87 vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
88 sizeof(*vsi->txq_map), GFP_KERNEL);
93 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
94 sizeof(*vsi->rxq_map), GFP_KERNEL);
98 /* There is no need to allocate q_vectors for a loopback VSI. */
99 if (vsi->type == ICE_VSI_LB)
102 /* allocate memory for q_vector pointers */
103 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
104 sizeof(*vsi->q_vectors), GFP_KERNEL);
111 devm_kfree(dev, vsi->rxq_map);
113 devm_kfree(dev, vsi->txq_map);
115 devm_kfree(dev, vsi->rx_rings);
117 devm_kfree(dev, vsi->tx_rings);
122 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
123 * @vsi: the VSI being configured
125 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
131 /* a user could change the values of num_[tr]x_desc using
132 * ethtool -G so we should keep those values instead of
133 * overwriting them with the defaults.
135 if (!vsi->num_rx_desc)
136 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
137 if (!vsi->num_tx_desc)
138 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
141 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
148 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
149 * @vsi: the VSI being configured
150 * @vf_id: ID of the VF being configured
152 * Return 0 on success and a negative value on error
154 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
156 struct ice_pf *pf = vsi->back;
157 struct ice_vf *vf = NULL;
159 if (vsi->type == ICE_VSI_VF)
162 vsi->vf_id = ICE_INVAL_VFID;
166 vsi->alloc_txq = min3(pf->num_lan_msix,
167 ice_get_avail_txq_count(pf),
168 (u16)num_online_cpus());
170 vsi->alloc_txq = vsi->req_txq;
171 vsi->num_txq = vsi->req_txq;
174 pf->num_lan_tx = vsi->alloc_txq;
176 /* only 1 Rx queue unless RSS is enabled */
177 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
180 vsi->alloc_rxq = min3(pf->num_lan_msix,
181 ice_get_avail_rxq_count(pf),
182 (u16)num_online_cpus());
184 vsi->alloc_rxq = vsi->req_rxq;
185 vsi->num_rxq = vsi->req_rxq;
189 pf->num_lan_rx = vsi->alloc_rxq;
191 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
192 max_t(int, vsi->alloc_rxq,
196 vf = &pf->vf[vsi->vf_id];
197 vsi->alloc_txq = vf->num_vf_qs;
198 vsi->alloc_rxq = vf->num_vf_qs;
199 /* pf->num_msix_per_vf includes (VF miscellaneous vector +
200 * data queue interrupts). Since vsi->num_q_vectors is number
201 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
202 * original vector count
204 vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
209 vsi->num_q_vectors = 1;
216 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
220 ice_vsi_set_num_desc(vsi);
224 * ice_get_free_slot - get the next non-NULL location index in array
225 * @array: array to search
226 * @size: size of the array
227 * @curr: last known occupied index to be used as a search hint
229 * void * is being used to keep the functionality generic. This lets us use this
230 * function on any array of pointers.
232 static int ice_get_free_slot(void *array, int size, int curr)
234 int **tmp_array = (int **)array;
237 if (curr < (size - 1) && !tmp_array[curr + 1]) {
242 while ((i < size) && (tmp_array[i]))
253 * ice_vsi_delete - delete a VSI from the switch
254 * @vsi: pointer to VSI being removed
256 static void ice_vsi_delete(struct ice_vsi *vsi)
258 struct ice_pf *pf = vsi->back;
259 struct ice_vsi_ctx *ctxt;
260 enum ice_status status;
262 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
266 if (vsi->type == ICE_VSI_VF)
267 ctxt->vf_num = vsi->vf_id;
268 ctxt->vsi_num = vsi->vsi_num;
270 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
272 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
274 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
275 vsi->vsi_num, ice_stat_str(status));
281 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
282 * @vsi: pointer to VSI being cleared
284 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
286 struct ice_pf *pf = vsi->back;
289 dev = ice_pf_to_dev(pf);
291 /* free the ring and vector containers */
292 if (vsi->q_vectors) {
293 devm_kfree(dev, vsi->q_vectors);
294 vsi->q_vectors = NULL;
297 devm_kfree(dev, vsi->tx_rings);
298 vsi->tx_rings = NULL;
301 devm_kfree(dev, vsi->rx_rings);
302 vsi->rx_rings = NULL;
305 devm_kfree(dev, vsi->txq_map);
309 devm_kfree(dev, vsi->rxq_map);
315 * ice_vsi_clear - clean up and deallocate the provided VSI
316 * @vsi: pointer to VSI being cleared
318 * This deallocates the VSI's queue resources, removes it from the PF's
319 * VSI array if necessary, and deallocates the VSI
321 * Returns 0 on success, negative on failure
323 static int ice_vsi_clear(struct ice_vsi *vsi)
325 struct ice_pf *pf = NULL;
335 dev = ice_pf_to_dev(pf);
337 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
338 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
342 mutex_lock(&pf->sw_mutex);
343 /* updates the PF for this cleared VSI */
345 pf->vsi[vsi->idx] = NULL;
346 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
347 pf->next_vsi = vsi->idx;
348 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
349 vsi->vf_id != ICE_INVAL_VFID)
350 pf->next_vsi = vsi->idx;
352 ice_vsi_free_arrays(vsi);
353 mutex_unlock(&pf->sw_mutex);
354 devm_kfree(dev, vsi);
360 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
361 * @irq: interrupt number
362 * @data: pointer to a q_vector
364 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
366 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
368 if (!q_vector->tx.ring)
371 #define FDIR_RX_DESC_CLEAN_BUDGET 64
372 ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
373 ice_clean_ctrl_tx_irq(q_vector->tx.ring);
379 * ice_msix_clean_rings - MSIX mode Interrupt Handler
380 * @irq: interrupt number
381 * @data: pointer to a q_vector
383 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
385 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
387 if (!q_vector->tx.ring && !q_vector->rx.ring)
390 q_vector->total_events++;
392 napi_schedule(&q_vector->napi);
398 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
399 * @pf: board private structure
400 * @vsi_type: type of VSI
401 * @vf_id: ID of the VF being configured
403 * returns a pointer to a VSI on success, NULL on failure.
405 static struct ice_vsi *
406 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
408 struct device *dev = ice_pf_to_dev(pf);
409 struct ice_vsi *vsi = NULL;
411 /* Need to protect the allocation of the VSIs at the PF level */
412 mutex_lock(&pf->sw_mutex);
414 /* If we have already allocated our maximum number of VSIs,
415 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
416 * is available to be populated
418 if (pf->next_vsi == ICE_NO_VSI) {
419 dev_dbg(dev, "out of VSI slots!\n");
423 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
427 vsi->type = vsi_type;
429 set_bit(ICE_VSI_DOWN, vsi->state);
431 if (vsi_type == ICE_VSI_VF)
432 ice_vsi_set_num_qs(vsi, vf_id);
434 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
438 if (ice_vsi_alloc_arrays(vsi))
441 /* Setup default MSIX irq handler for VSI */
442 vsi->irq_handler = ice_msix_clean_rings;
445 if (ice_vsi_alloc_arrays(vsi))
448 /* Setup ctrl VSI MSIX irq handler */
449 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
452 if (ice_vsi_alloc_arrays(vsi))
456 if (ice_vsi_alloc_arrays(vsi))
460 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
464 if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
465 /* Use the last VSI slot as the index for PF control VSI */
466 vsi->idx = pf->num_alloc_vsi - 1;
467 pf->ctrl_vsi_idx = vsi->idx;
468 pf->vsi[vsi->idx] = vsi;
470 /* fill slot and make note of the index */
471 vsi->idx = pf->next_vsi;
472 pf->vsi[pf->next_vsi] = vsi;
474 /* prepare pf->next_vsi for next use */
475 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
479 if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
480 pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
484 devm_kfree(dev, vsi);
487 mutex_unlock(&pf->sw_mutex);
492 * ice_alloc_fd_res - Allocate FD resource for a VSI
493 * @vsi: pointer to the ice_vsi
495 * This allocates the FD resources
497 * Returns 0 on success, -EPERM on no-op or -EIO on failure
499 static int ice_alloc_fd_res(struct ice_vsi *vsi)
501 struct ice_pf *pf = vsi->back;
504 /* Flow Director filters are only allocated/assigned to the PF VSI which
505 * passes the traffic. The CTRL VSI is only used to add/delete filters
506 * so we don't allocate resources to it
509 /* FD filters from guaranteed pool per VSI */
510 g_val = pf->hw.func_caps.fd_fltr_guar;
514 /* FD filters from best effort pool */
515 b_val = pf->hw.func_caps.fd_fltr_best_effort;
519 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
522 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
525 vsi->num_gfltr = g_val / pf->num_alloc_vsi;
527 /* each VSI gets same "best_effort" quota */
528 vsi->num_bfltr = b_val;
530 if (vsi->type == ICE_VSI_VF) {
533 /* each VSI gets same "best_effort" quota */
534 vsi->num_bfltr = b_val;
541 * ice_vsi_get_qs - Assign queues from PF to VSI
542 * @vsi: the VSI to assign queues to
544 * Returns 0 on success and a negative value on error
546 static int ice_vsi_get_qs(struct ice_vsi *vsi)
548 struct ice_pf *pf = vsi->back;
549 struct ice_qs_cfg tx_qs_cfg = {
550 .qs_mutex = &pf->avail_q_mutex,
551 .pf_map = pf->avail_txqs,
552 .pf_map_size = pf->max_pf_txqs,
553 .q_count = vsi->alloc_txq,
554 .scatter_count = ICE_MAX_SCATTER_TXQS,
555 .vsi_map = vsi->txq_map,
557 .mapping_mode = ICE_VSI_MAP_CONTIG
559 struct ice_qs_cfg rx_qs_cfg = {
560 .qs_mutex = &pf->avail_q_mutex,
561 .pf_map = pf->avail_rxqs,
562 .pf_map_size = pf->max_pf_rxqs,
563 .q_count = vsi->alloc_rxq,
564 .scatter_count = ICE_MAX_SCATTER_RXQS,
565 .vsi_map = vsi->rxq_map,
567 .mapping_mode = ICE_VSI_MAP_CONTIG
571 ret = __ice_vsi_get_qs(&tx_qs_cfg);
574 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
576 ret = __ice_vsi_get_qs(&rx_qs_cfg);
579 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
585 * ice_vsi_put_qs - Release queues from VSI to PF
586 * @vsi: the VSI that is going to release queues
588 static void ice_vsi_put_qs(struct ice_vsi *vsi)
590 struct ice_pf *pf = vsi->back;
593 mutex_lock(&pf->avail_q_mutex);
595 for (i = 0; i < vsi->alloc_txq; i++) {
596 clear_bit(vsi->txq_map[i], pf->avail_txqs);
597 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
600 for (i = 0; i < vsi->alloc_rxq; i++) {
601 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
602 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
605 mutex_unlock(&pf->avail_q_mutex);
610 * @pf: pointer to the PF struct
612 * returns true if driver is in safe mode, false otherwise
614 bool ice_is_safe_mode(struct ice_pf *pf)
616 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
620 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
621 * @vsi: the VSI being cleaned up
623 * This function deletes RSS input set for all flows that were configured
626 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
628 struct ice_pf *pf = vsi->back;
629 enum ice_status status;
631 if (ice_is_safe_mode(pf))
634 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
636 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
637 vsi->vsi_num, ice_stat_str(status));
641 * ice_rss_clean - Delete RSS related VSI structures and configuration
642 * @vsi: the VSI being removed
644 static void ice_rss_clean(struct ice_vsi *vsi)
646 struct ice_pf *pf = vsi->back;
649 dev = ice_pf_to_dev(pf);
651 if (vsi->rss_hkey_user)
652 devm_kfree(dev, vsi->rss_hkey_user);
653 if (vsi->rss_lut_user)
654 devm_kfree(dev, vsi->rss_lut_user);
656 ice_vsi_clean_rss_flow_fld(vsi);
657 /* remove RSS replay list */
658 if (!ice_is_safe_mode(pf))
659 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
663 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
664 * @vsi: the VSI being configured
666 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
668 struct ice_hw_common_caps *cap;
669 struct ice_pf *pf = vsi->back;
671 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
676 cap = &pf->hw.func_caps.common_cap;
679 /* PF VSI will inherit RSS instance of PF */
680 vsi->rss_table_size = (u16)cap->rss_table_size;
681 vsi->rss_size = min_t(u16, num_online_cpus(),
682 BIT(cap->rss_table_entry_width));
683 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
686 /* VF VSI will get a small RSS table.
687 * For VSI_LUT, LUT size should be set to 64 bytes.
689 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
690 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
691 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
696 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
697 ice_vsi_type_str(vsi->type));
703 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
704 * @ctxt: the VSI context being set
706 * This initializes a default VSI context for all sections except the Queues.
708 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
712 memset(&ctxt->info, 0, sizeof(ctxt->info));
713 /* VSI's should be allocated from shared pool */
714 ctxt->alloc_from_pool = true;
715 /* Src pruning enabled by default */
716 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
717 /* Traffic from VSI can be sent to LAN */
718 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
719 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
720 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
721 * packets untagged/tagged.
723 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
724 ICE_AQ_VSI_VLAN_MODE_M) >>
725 ICE_AQ_VSI_VLAN_MODE_S);
726 /* Have 1:1 UP mapping for both ingress/egress tables */
727 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
728 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
729 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
730 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
731 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
732 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
733 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
734 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
735 ctxt->info.ingress_table = cpu_to_le32(table);
736 ctxt->info.egress_table = cpu_to_le32(table);
737 /* Have 1:1 UP mapping for outer to inner UP table */
738 ctxt->info.outer_up_table = cpu_to_le32(table);
739 /* No Outer tag support outer_tag_flags remains to zero */
743 * ice_vsi_setup_q_map - Setup a VSI queue map
744 * @vsi: the VSI being configured
745 * @ctxt: VSI context structure
747 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
749 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
750 u16 num_txq_per_tc, num_rxq_per_tc;
751 u16 qcount_tx = vsi->alloc_txq;
752 u16 qcount_rx = vsi->alloc_rxq;
753 bool ena_tc0 = false;
757 /* at least TC0 should be enabled by default */
758 if (vsi->tc_cfg.numtc) {
759 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
767 vsi->tc_cfg.ena_tc |= 1;
770 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
773 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
777 /* find the (rounded up) power-of-2 of qcount */
778 pow = (u16)order_base_2(num_rxq_per_tc);
780 /* TC mapping is a function of the number of Rx queues assigned to the
781 * VSI for each traffic class and the offset of these queues.
782 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
783 * queues allocated to TC0. No:of queues is a power-of-2.
785 * If TC is not enabled, the queue offset is set to 0, and allocate one
786 * queue, this way, traffic for the given TC will be sent to the default
789 * Setup number and offset of Rx queues for all TCs for the VSI
791 ice_for_each_traffic_class(i) {
792 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
793 /* TC is not enabled */
794 vsi->tc_cfg.tc_info[i].qoffset = 0;
795 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
796 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
797 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
798 ctxt->info.tc_mapping[i] = 0;
803 vsi->tc_cfg.tc_info[i].qoffset = offset;
804 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
805 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
806 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
808 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
809 ICE_AQ_VSI_TC_Q_OFFSET_M) |
810 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
811 ICE_AQ_VSI_TC_Q_NUM_M);
812 offset += num_rxq_per_tc;
813 tx_count += num_txq_per_tc;
814 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
817 /* if offset is non-zero, means it is calculated correctly based on
818 * enabled TCs for a given VSI otherwise qcount_rx will always
819 * be correct and non-zero because it is based off - VSI's
820 * allocated Rx queues which is at least 1 (hence qcount_tx will be
824 vsi->num_rxq = offset;
826 vsi->num_rxq = num_rxq_per_tc;
828 vsi->num_txq = tx_count;
830 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
831 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
832 /* since there is a chance that num_rxq could have been changed
833 * in the above for loop, make num_txq equal to num_rxq.
835 vsi->num_txq = vsi->num_rxq;
838 /* Rx queue mapping */
839 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
840 /* q_mapping buffer holds the info for the first queue allocated for
841 * this VSI in the PF space and also the number of queues associated
844 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
845 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
849 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
850 * @ctxt: the VSI context being set
851 * @vsi: the VSI being configured
853 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
855 u8 dflt_q_group, dflt_q_prio;
856 u16 dflt_q, report_q, val;
858 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
859 vsi->type != ICE_VSI_VF)
862 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
863 ctxt->info.valid_sections |= cpu_to_le16(val);
869 /* enable flow director filtering/programming */
870 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
871 ctxt->info.fd_options = cpu_to_le16(val);
872 /* max of allocated flow director filters */
873 ctxt->info.max_fd_fltr_dedicated =
874 cpu_to_le16(vsi->num_gfltr);
875 /* max of shared flow director filters any VSI may program */
876 ctxt->info.max_fd_fltr_shared =
877 cpu_to_le16(vsi->num_bfltr);
878 /* default queue index within the VSI of the default FD */
879 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
880 ICE_AQ_VSI_FD_DEF_Q_M);
881 /* target queue or queue group to the FD filter */
882 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
883 ICE_AQ_VSI_FD_DEF_GRP_M);
884 ctxt->info.fd_def_q = cpu_to_le16(val);
885 /* queue index on which FD filter completion is reported */
886 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
887 ICE_AQ_VSI_FD_REPORT_Q_M);
888 /* priority of the default qindex action */
889 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
890 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
891 ctxt->info.fd_report_opt = cpu_to_le16(val);
895 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
896 * @ctxt: the VSI context being set
897 * @vsi: the VSI being configured
899 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
901 u8 lut_type, hash_type;
906 dev = ice_pf_to_dev(pf);
910 /* PF VSI will inherit RSS instance of PF */
911 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
912 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
915 /* VF VSI will gets a small RSS table which is a VSI LUT type */
916 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
917 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
920 dev_dbg(dev, "Unsupported VSI type %s\n",
921 ice_vsi_type_str(vsi->type));
925 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
926 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
927 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
928 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
932 * ice_vsi_init - Create and initialize a VSI
933 * @vsi: the VSI being configured
934 * @init_vsi: is this call creating a VSI
936 * This initializes a VSI context depending on the VSI type to be added and
937 * passes it down to the add_vsi aq command to create a new VSI.
939 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
941 struct ice_pf *pf = vsi->back;
942 struct ice_hw *hw = &pf->hw;
943 struct ice_vsi_ctx *ctxt;
947 dev = ice_pf_to_dev(pf);
948 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
956 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
959 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
960 /* VF number here is the absolute VF number (0-255) */
961 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
968 ice_set_dflt_vsi_ctx(ctxt);
969 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
970 ice_set_fd_vsi_ctx(ctxt, vsi);
971 /* if the switch is in VEB mode, allow VSI loopback */
972 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
973 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
975 /* Set LUT type and HASH type if RSS is enabled */
976 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
977 vsi->type != ICE_VSI_CTRL) {
978 ice_set_rss_vsi_ctx(ctxt, vsi);
979 /* if updating VSI context, make sure to set valid_section:
980 * to indicate which section of VSI context being updated
983 ctxt->info.valid_sections |=
984 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
987 ctxt->info.sw_id = vsi->port_info->sw_id;
988 ice_vsi_setup_q_map(vsi, ctxt);
989 if (!init_vsi) /* means VSI being updated */
990 /* must to indicate which section of VSI context are
993 ctxt->info.valid_sections |=
994 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
996 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
999 if (vsi->type == ICE_VSI_VF) {
1000 ctxt->info.valid_sections |=
1001 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1002 if (pf->vf[vsi->vf_id].spoofchk) {
1003 ctxt->info.sec_flags |=
1004 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1005 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1006 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1008 ctxt->info.sec_flags &=
1009 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1010 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1011 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1015 /* Allow control frames out of main VSI */
1016 if (vsi->type == ICE_VSI_PF) {
1017 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1018 ctxt->info.valid_sections |=
1019 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1023 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1025 dev_err(dev, "Add VSI failed, err %d\n", ret);
1030 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1032 dev_err(dev, "Update VSI failed, err %d\n", ret);
1038 /* keep context for update VSI operations */
1039 vsi->info = ctxt->info;
1041 /* record VSI number returned */
1042 vsi->vsi_num = ctxt->vsi_num;
1050 * ice_free_res - free a block of resources
1051 * @res: pointer to the resource
1052 * @index: starting index previously returned by ice_get_res
1053 * @id: identifier to track owner
1055 * Returns number of resources freed
1057 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1062 if (!res || index >= res->end)
1065 id |= ICE_RES_VALID_BIT;
1066 for (i = index; i < res->end && res->list[i] == id; i++) {
1075 * ice_search_res - Search the tracker for a block of resources
1076 * @res: pointer to the resource
1077 * @needed: size of the block needed
1078 * @id: identifier to track owner
1080 * Returns the base item index of the block, or -ENOMEM for error
1082 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1084 u16 start = 0, end = 0;
1086 if (needed > res->end)
1089 id |= ICE_RES_VALID_BIT;
1092 /* skip already allocated entries */
1093 if (res->list[end++] & ICE_RES_VALID_BIT) {
1095 if ((start + needed) > res->end)
1099 if (end == (start + needed)) {
1102 /* there was enough, so assign it to the requestor */
1104 res->list[i++] = id;
1108 } while (end < res->end);
1114 * ice_get_free_res_count - Get free count from a resource tracker
1115 * @res: Resource tracker instance
1117 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1121 for (i = 0; i < res->end; i++)
1122 if (!(res->list[i] & ICE_RES_VALID_BIT))
1129 * ice_get_res - get a block of resources
1130 * @pf: board private structure
1131 * @res: pointer to the resource
1132 * @needed: size of the block needed
1133 * @id: identifier to track owner
1135 * Returns the base item index of the block, or negative for error
1138 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1143 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1144 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1145 needed, res->num_entries, id);
1149 return ice_search_res(res, needed, id);
1153 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1154 * @vsi: ptr to the VSI
1156 * This should only be called after ice_vsi_alloc() which allocates the
1157 * corresponding SW VSI structure and initializes num_queue_pairs for the
1158 * newly allocated VSI.
1160 * Returns 0 on success or negative on failure
1162 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1164 struct ice_pf *pf = vsi->back;
1169 dev = ice_pf_to_dev(pf);
1170 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1171 if (vsi->type == ICE_VSI_VF)
1174 if (vsi->base_vector) {
1175 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1176 vsi->vsi_num, vsi->base_vector);
1180 num_q_vectors = vsi->num_q_vectors;
1181 /* reserve slots from OS requested IRQs */
1182 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1186 ice_for_each_vf(pf, i) {
1188 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1189 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1193 if (i == pf->num_alloc_vfs)
1194 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1195 ICE_RES_VF_CTRL_VEC_ID);
1197 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1202 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1203 ice_get_free_res_count(pf->irq_tracker),
1204 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1207 vsi->base_vector = (u16)base;
1208 pf->num_avail_sw_msix -= num_q_vectors;
1214 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1215 * @vsi: the VSI having rings deallocated
1217 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1221 /* Avoid stale references by clearing map from vector to ring */
1222 if (vsi->q_vectors) {
1223 ice_for_each_q_vector(vsi, i) {
1224 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1227 q_vector->tx.ring = NULL;
1228 q_vector->rx.ring = NULL;
1233 if (vsi->tx_rings) {
1234 for (i = 0; i < vsi->alloc_txq; i++) {
1235 if (vsi->tx_rings[i]) {
1236 kfree_rcu(vsi->tx_rings[i], rcu);
1237 WRITE_ONCE(vsi->tx_rings[i], NULL);
1241 if (vsi->rx_rings) {
1242 for (i = 0; i < vsi->alloc_rxq; i++) {
1243 if (vsi->rx_rings[i]) {
1244 kfree_rcu(vsi->rx_rings[i], rcu);
1245 WRITE_ONCE(vsi->rx_rings[i], NULL);
1252 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1253 * @vsi: VSI which is having rings allocated
1255 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1257 struct ice_pf *pf = vsi->back;
1261 dev = ice_pf_to_dev(pf);
1262 /* Allocate Tx rings */
1263 for (i = 0; i < vsi->alloc_txq; i++) {
1264 struct ice_ring *ring;
1266 /* allocate with kzalloc(), free with kfree_rcu() */
1267 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1273 ring->reg_idx = vsi->txq_map[i];
1274 ring->ring_active = false;
1277 ring->count = vsi->num_tx_desc;
1278 WRITE_ONCE(vsi->tx_rings[i], ring);
1281 /* Allocate Rx rings */
1282 for (i = 0; i < vsi->alloc_rxq; i++) {
1283 struct ice_ring *ring;
1285 /* allocate with kzalloc(), free with kfree_rcu() */
1286 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1291 ring->reg_idx = vsi->rxq_map[i];
1292 ring->ring_active = false;
1294 ring->netdev = vsi->netdev;
1296 ring->count = vsi->num_rx_desc;
1297 WRITE_ONCE(vsi->rx_rings[i], ring);
1303 ice_vsi_clear_rings(vsi);
1308 * ice_vsi_manage_rss_lut - disable/enable RSS
1309 * @vsi: the VSI being changed
1310 * @ena: boolean value indicating if this is an enable or disable request
1312 * In the event of disable request for RSS, this function will zero out RSS
1313 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1316 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1321 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1326 if (vsi->rss_lut_user)
1327 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1329 ice_fill_rss_lut(lut, vsi->rss_table_size,
1333 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1339 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1340 * @vsi: VSI to be configured
1342 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1344 struct ice_pf *pf = vsi->back;
1349 dev = ice_pf_to_dev(pf);
1350 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1352 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1356 if (vsi->rss_lut_user)
1357 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1359 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1361 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1363 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1364 goto ice_vsi_cfg_rss_exit;
1367 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1370 goto ice_vsi_cfg_rss_exit;
1373 if (vsi->rss_hkey_user)
1374 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1376 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1378 err = ice_set_rss_key(vsi, key);
1380 dev_err(dev, "set_rss_key failed, error %d\n", err);
1383 ice_vsi_cfg_rss_exit:
1389 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1390 * @vsi: VSI to be configured
1392 * This function will only be called during the VF VSI setup. Upon successful
1393 * completion of package download, this function will configure default RSS
1394 * input sets for VF VSI.
1396 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1398 struct ice_pf *pf = vsi->back;
1399 enum ice_status status;
1402 dev = ice_pf_to_dev(pf);
1403 if (ice_is_safe_mode(pf)) {
1404 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1409 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1411 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1412 vsi->vsi_num, ice_stat_str(status));
1416 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1417 * @vsi: VSI to be configured
1419 * This function will only be called after successful download package call
1420 * during initialization of PF. Since the downloaded package will erase the
1421 * RSS section, this function will configure RSS input sets for different
1422 * flow types. The last profile added has the highest priority, therefore 2
1423 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1424 * (i.e. IPv4 src/dst TCP src/dst port).
1426 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1428 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1429 struct ice_pf *pf = vsi->back;
1430 struct ice_hw *hw = &pf->hw;
1431 enum ice_status status;
1434 dev = ice_pf_to_dev(pf);
1435 if (ice_is_safe_mode(pf)) {
1436 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1440 /* configure RSS for IPv4 with input set IP src/dst */
1441 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1442 ICE_FLOW_SEG_HDR_IPV4);
1444 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1445 vsi_num, ice_stat_str(status));
1447 /* configure RSS for IPv6 with input set IPv6 src/dst */
1448 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1449 ICE_FLOW_SEG_HDR_IPV6);
1451 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1452 vsi_num, ice_stat_str(status));
1454 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1455 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1456 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1458 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1459 vsi_num, ice_stat_str(status));
1461 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1462 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1463 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1465 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1466 vsi_num, ice_stat_str(status));
1468 /* configure RSS for sctp4 with input set IP src/dst */
1469 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1470 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1472 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1473 vsi_num, ice_stat_str(status));
1475 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1476 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1477 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1479 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1480 vsi_num, ice_stat_str(status));
1482 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1483 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1484 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1486 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1487 vsi_num, ice_stat_str(status));
1489 /* configure RSS for sctp6 with input set IPv6 src/dst */
1490 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1491 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1493 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1494 vsi_num, ice_stat_str(status));
1498 * ice_pf_state_is_nominal - checks the PF for nominal state
1499 * @pf: pointer to PF to check
1501 * Check the PF's state for a collection of bits that would indicate
1502 * the PF is in a state that would inhibit normal operation for
1503 * driver functionality.
1505 * Returns true if PF is in a nominal state, false otherwise
1507 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1509 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1514 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1515 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1522 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1523 * @vsi: the VSI to be updated
1525 void ice_update_eth_stats(struct ice_vsi *vsi)
1527 struct ice_eth_stats *prev_es, *cur_es;
1528 struct ice_hw *hw = &vsi->back->hw;
1529 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1531 prev_es = &vsi->eth_stats_prev;
1532 cur_es = &vsi->eth_stats;
1534 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1535 &prev_es->rx_bytes, &cur_es->rx_bytes);
1537 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1538 &prev_es->rx_unicast, &cur_es->rx_unicast);
1540 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1541 &prev_es->rx_multicast, &cur_es->rx_multicast);
1543 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1544 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1546 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1547 &prev_es->rx_discards, &cur_es->rx_discards);
1549 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1550 &prev_es->tx_bytes, &cur_es->tx_bytes);
1552 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1553 &prev_es->tx_unicast, &cur_es->tx_unicast);
1555 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1556 &prev_es->tx_multicast, &cur_es->tx_multicast);
1558 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1559 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1561 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1562 &prev_es->tx_errors, &cur_es->tx_errors);
1564 vsi->stat_offsets_loaded = true;
1568 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1569 * @vsi: the VSI being configured
1570 * @vid: VLAN ID to be added
1571 * @action: filter action to be performed on match
1574 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1576 struct ice_pf *pf = vsi->back;
1580 dev = ice_pf_to_dev(pf);
1582 if (!ice_fltr_add_vlan(vsi, vid, action)) {
1586 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1594 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1595 * @vsi: the VSI being configured
1596 * @vid: VLAN ID to be removed
1598 * Returns 0 on success and negative on failure
1600 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1602 struct ice_pf *pf = vsi->back;
1603 enum ice_status status;
1607 dev = ice_pf_to_dev(pf);
1609 status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1612 } else if (status == ICE_ERR_DOES_NOT_EXIST) {
1613 dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1614 vid, vsi->vsi_num, ice_stat_str(status));
1616 dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1617 vid, vsi->vsi_num, ice_stat_str(status));
1625 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1628 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1630 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1631 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1632 vsi->rx_buf_len = ICE_RXBUF_2048;
1633 #if (PAGE_SIZE < 8192)
1634 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1635 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1636 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1637 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1640 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1641 #if (PAGE_SIZE < 8192)
1642 vsi->rx_buf_len = ICE_RXBUF_3072;
1644 vsi->rx_buf_len = ICE_RXBUF_2048;
1650 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1652 * @pf_q: index of the Rx queue in the PF's queue space
1653 * @rxdid: flexible descriptor RXDID
1654 * @prio: priority for the RXDID for this queue
1657 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio)
1659 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1661 /* clear any previous values */
1662 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1663 QRXFLXP_CNTXT_RXDID_PRIO_M |
1664 QRXFLXP_CNTXT_TS_M);
1666 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1667 QRXFLXP_CNTXT_RXDID_IDX_M;
1669 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1670 QRXFLXP_CNTXT_RXDID_PRIO_M;
1672 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1676 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1677 * @vsi: the VSI being configured
1679 * Return 0 on success and a negative value on error
1680 * Configure the Rx VSI for operation.
1682 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1686 if (vsi->type == ICE_VSI_VF)
1689 ice_vsi_cfg_frame_size(vsi);
1691 /* set up individual rings */
1692 for (i = 0; i < vsi->num_rxq; i++) {
1695 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1697 dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1707 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1708 * @vsi: the VSI being configured
1709 * @rings: Tx ring array to be configured
1711 * Return 0 on success and a negative value on error
1712 * Configure the Tx VSI for operation.
1715 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1717 struct ice_aqc_add_tx_qgrp *qg_buf;
1721 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1725 qg_buf->num_txqs = 1;
1727 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1728 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1739 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1740 * @vsi: the VSI being configured
1742 * Return 0 on success and a negative value on error
1743 * Configure the Tx VSI for operation.
1745 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1747 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1751 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1752 * @vsi: the VSI being configured
1754 * Return 0 on success and a negative value on error
1755 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1757 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1762 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1766 for (i = 0; i < vsi->num_xdp_txq; i++)
1767 vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1773 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1774 * @intrl: interrupt rate limit in usecs
1775 * @gran: interrupt rate limit granularity in usecs
1777 * This function converts a decimal interrupt rate limit in usecs to the format
1778 * expected by firmware.
1780 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1782 u32 val = intrl / gran;
1785 return val | GLINT_RATE_INTRL_ENA_M;
1790 * ice_write_intrl - write throttle rate limit to interrupt specific register
1791 * @q_vector: pointer to interrupt specific structure
1792 * @intrl: throttle rate limit in microseconds to write
1794 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1796 struct ice_hw *hw = &q_vector->vsi->back->hw;
1798 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1799 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1803 * __ice_write_itr - write throttle rate to register
1804 * @q_vector: pointer to interrupt data structure
1805 * @rc: pointer to ring container
1806 * @itr: throttle rate in microseconds to write
1808 static void __ice_write_itr(struct ice_q_vector *q_vector,
1809 struct ice_ring_container *rc, u16 itr)
1811 struct ice_hw *hw = &q_vector->vsi->back->hw;
1813 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1814 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1818 * ice_write_itr - write throttle rate to queue specific register
1819 * @rc: pointer to ring container
1820 * @itr: throttle rate in microseconds to write
1822 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1824 struct ice_q_vector *q_vector;
1829 q_vector = rc->ring->q_vector;
1831 __ice_write_itr(q_vector, rc, itr);
1835 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1836 * @vsi: the VSI being configured
1838 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1841 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1843 struct ice_pf *pf = vsi->back;
1844 struct ice_hw *hw = &pf->hw;
1845 u16 txq = 0, rxq = 0;
1848 for (i = 0; i < vsi->num_q_vectors; i++) {
1849 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1850 u16 reg_idx = q_vector->reg_idx;
1852 ice_cfg_itr(hw, q_vector);
1854 /* Both Transmit Queue Interrupt Cause Control register
1855 * and Receive Queue Interrupt Cause control register
1856 * expects MSIX_INDX field to be the vector index
1857 * within the function space and not the absolute
1858 * vector index across PF or across device.
1859 * For SR-IOV VF VSIs queue vector index always starts
1860 * with 1 since first vector index(0) is used for OICR
1861 * in VF space. Since VMDq and other PF VSIs are within
1862 * the PF function space, use the vector index that is
1863 * tracked for this PF.
1865 for (q = 0; q < q_vector->num_ring_tx; q++) {
1866 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1867 q_vector->tx.itr_idx);
1871 for (q = 0; q < q_vector->num_ring_rx; q++) {
1872 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1873 q_vector->rx.itr_idx);
1880 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1881 * @vsi: the VSI being changed
1883 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1885 struct ice_hw *hw = &vsi->back->hw;
1886 struct ice_vsi_ctx *ctxt;
1887 enum ice_status status;
1890 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1894 /* Here we are configuring the VSI to let the driver add VLAN tags by
1895 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1896 * insertion happens in the Tx hot path, in ice_tx_map.
1898 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1900 /* Preserve existing VLAN strip setting */
1901 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1902 ICE_AQ_VSI_VLAN_EMOD_M);
1904 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1906 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1908 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1909 ice_stat_str(status),
1910 ice_aq_str(hw->adminq.sq_last_status));
1915 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1922 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1923 * @vsi: the VSI being changed
1924 * @ena: boolean value indicating if this is a enable or disable request
1926 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1928 struct ice_hw *hw = &vsi->back->hw;
1929 struct ice_vsi_ctx *ctxt;
1930 enum ice_status status;
1933 /* do not allow modifying VLAN stripping when a port VLAN is configured
1939 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1943 /* Here we are configuring what the VSI should do with the VLAN tag in
1944 * the Rx packet. We can either leave the tag in the packet or put it in
1945 * the Rx descriptor.
1948 /* Strip VLAN tag from Rx packet and put it in the desc */
1949 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1951 /* Disable stripping. Leave tag in packet */
1952 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1954 /* Allow all packets untagged/tagged */
1955 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1957 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1959 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1961 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1962 ena, ice_stat_str(status),
1963 ice_aq_str(hw->adminq.sq_last_status));
1968 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1975 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1976 * @vsi: the VSI whose rings are to be enabled
1978 * Returns 0 on success and a negative value on error
1980 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1982 return ice_vsi_ctrl_all_rx_rings(vsi, true);
1986 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1987 * @vsi: the VSI whose rings are to be disabled
1989 * Returns 0 on success and a negative value on error
1991 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1993 return ice_vsi_ctrl_all_rx_rings(vsi, false);
1997 * ice_vsi_stop_tx_rings - Disable Tx rings
1998 * @vsi: the VSI being configured
1999 * @rst_src: reset source
2000 * @rel_vmvf_num: Relative ID of VF/VM
2001 * @rings: Tx ring array to be stopped
2004 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2005 u16 rel_vmvf_num, struct ice_ring **rings)
2009 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2012 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
2013 struct ice_txq_meta txq_meta = { };
2016 if (!rings || !rings[q_idx])
2019 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2020 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2021 rings[q_idx], &txq_meta);
2031 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2032 * @vsi: the VSI being configured
2033 * @rst_src: reset source
2034 * @rel_vmvf_num: Relative ID of VF/VM
2037 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2040 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
2044 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2045 * @vsi: the VSI being configured
2047 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2049 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
2053 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2054 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2056 * returns true if Rx VLAN pruning is enabled and false otherwise.
2058 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2063 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2067 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2068 * @vsi: VSI to enable or disable VLAN pruning on
2069 * @ena: set to true to enable VLAN pruning and false to disable it
2070 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2072 * returns 0 if VSI is updated, negative otherwise
2074 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2076 struct ice_vsi_ctx *ctxt;
2083 /* Don't enable VLAN pruning if the netdev is currently in promiscuous
2084 * mode. VLAN pruning will be enabled when the interface exits
2085 * promiscuous mode if any VLAN filters are active.
2087 if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2091 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2095 ctxt->info = vsi->info;
2098 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2100 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2103 ctxt->info.valid_sections =
2104 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2106 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2108 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2109 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2110 ice_stat_str(status),
2111 ice_aq_str(pf->hw.adminq.sq_last_status));
2115 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2125 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2127 struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2129 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2130 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2134 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2135 * @vsi: VSI to set the q_vectors register index on
2138 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2142 if (!vsi || !vsi->q_vectors)
2145 ice_for_each_q_vector(vsi, i) {
2146 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2149 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2154 if (vsi->type == ICE_VSI_VF) {
2155 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2157 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2160 q_vector->v_idx + vsi->base_vector;
2167 ice_for_each_q_vector(vsi, i) {
2168 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2171 q_vector->reg_idx = 0;
2178 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2179 * @vsi: the VSI being configured
2180 * @tx: bool to determine Tx or Rx rule
2181 * @create: bool to determine create or remove Rule
2183 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2185 enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2186 enum ice_sw_fwd_act_type act);
2187 struct ice_pf *pf = vsi->back;
2188 enum ice_status status;
2191 dev = ice_pf_to_dev(pf);
2192 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2195 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2198 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2199 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2202 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2208 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2209 create ? "adding" : "removing", tx ? "TX" : "RX",
2210 vsi->vsi_num, ice_stat_str(status));
2214 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2215 * @vsi: pointer to the VSI
2217 * This function will allocate new scheduler aggregator now if needed and will
2218 * move specified VSI into it.
2220 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2222 struct device *dev = ice_pf_to_dev(vsi->back);
2223 struct ice_agg_node *agg_node_iter = NULL;
2224 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2225 struct ice_agg_node *agg_node = NULL;
2226 int node_offset, max_agg_nodes = 0;
2227 struct ice_port_info *port_info;
2228 struct ice_pf *pf = vsi->back;
2229 u32 agg_node_id_start = 0;
2230 enum ice_status status;
2232 /* create (as needed) scheduler aggregator node and move VSI into
2233 * corresponding aggregator node
2234 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2235 * - VF aggregator nodes will contain VF VSI
2237 port_info = pf->hw.port_info;
2241 switch (vsi->type) {
2245 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2246 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2247 agg_node_iter = &pf->pf_agg_node[0];
2250 /* user can create 'n' VFs on a given PF, but since max children
2251 * per aggregator node can be only 64. Following code handles
2252 * aggregator(s) for VF VSIs, either selects a agg_node which
2253 * was already created provided num_vsis < 64, otherwise
2254 * select next available node, which will be created
2256 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2257 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2258 agg_node_iter = &pf->vf_agg_node[0];
2261 /* other VSI type, handle later if needed */
2262 dev_dbg(dev, "unexpected VSI type %s\n",
2263 ice_vsi_type_str(vsi->type));
2267 /* find the appropriate aggregator node */
2268 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2269 /* see if we can find space in previously created
2270 * node if num_vsis < 64, otherwise skip
2272 if (agg_node_iter->num_vsis &&
2273 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2278 if (agg_node_iter->valid &&
2279 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2280 agg_id = agg_node_iter->agg_id;
2281 agg_node = agg_node_iter;
2285 /* find unclaimed agg_id */
2286 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2287 agg_id = node_offset + agg_node_id_start;
2288 agg_node = agg_node_iter;
2291 /* move to next agg_node */
2298 /* if selected aggregator node was not created, create it */
2299 if (!agg_node->valid) {
2300 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2301 (u8)vsi->tc_cfg.ena_tc);
2303 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2307 /* aggregator node is created, store the neeeded info */
2308 agg_node->valid = true;
2309 agg_node->agg_id = agg_id;
2312 /* move VSI to corresponding aggregator node */
2313 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2314 (u8)vsi->tc_cfg.ena_tc);
2316 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2321 /* keep active children count for aggregator node */
2322 agg_node->num_vsis++;
2324 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2325 * to aggregator node
2327 vsi->agg_node = agg_node;
2328 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2329 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2330 vsi->agg_node->num_vsis);
2334 * ice_vsi_setup - Set up a VSI by a given type
2335 * @pf: board private structure
2336 * @pi: pointer to the port_info instance
2337 * @vsi_type: VSI type
2338 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2339 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2340 * fill-in ICE_INVAL_VFID as input.
2342 * This allocates the sw VSI structure and its queue resources.
2344 * Returns pointer to the successfully allocated and configured VSI sw struct on
2345 * success, NULL on failure.
2348 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2349 enum ice_vsi_type vsi_type, u16 vf_id)
2351 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2352 struct device *dev = ice_pf_to_dev(pf);
2353 enum ice_status status;
2354 struct ice_vsi *vsi;
2357 if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2358 vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2360 vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2363 dev_err(dev, "could not allocate VSI\n");
2367 vsi->port_info = pi;
2368 vsi->vsw = pf->first_sw;
2369 if (vsi->type == ICE_VSI_PF)
2370 vsi->ethtype = ETH_P_PAUSE;
2372 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2375 ice_alloc_fd_res(vsi);
2377 if (ice_vsi_get_qs(vsi)) {
2378 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2380 goto unroll_vsi_alloc;
2383 /* set RSS capabilities */
2384 ice_vsi_set_rss_params(vsi);
2386 /* set TC configuration */
2387 ice_vsi_set_tc_cfg(vsi);
2389 /* create the VSI */
2390 ret = ice_vsi_init(vsi, true);
2394 switch (vsi->type) {
2397 ret = ice_vsi_alloc_q_vectors(vsi);
2399 goto unroll_vsi_init;
2401 ret = ice_vsi_setup_vector_base(vsi);
2403 goto unroll_alloc_q_vector;
2405 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2407 goto unroll_vector_base;
2409 ret = ice_vsi_alloc_rings(vsi);
2411 goto unroll_vector_base;
2413 /* Always add VLAN ID 0 switch rule by default. This is needed
2414 * in order to allow all untagged and 0 tagged priority traffic
2415 * if Rx VLAN pruning is enabled. Also there are cases where we
2416 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2417 * so this handles those cases (i.e. adding the PF to a bridge
2418 * without the 8021q module loaded).
2420 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2422 goto unroll_clear_rings;
2424 ice_vsi_map_rings_to_vectors(vsi);
2426 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2427 if (vsi->type != ICE_VSI_CTRL)
2428 /* Do not exit if configuring RSS had an issue, at
2429 * least receive traffic on first queue. Hence no
2430 * need to capture return value
2432 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2433 ice_vsi_cfg_rss_lut_key(vsi);
2434 ice_vsi_set_rss_flow_fld(vsi);
2439 /* VF driver will take care of creating netdev for this type and
2440 * map queues to vectors through Virtchnl, PF driver only
2441 * creates a VSI and corresponding structures for bookkeeping
2444 ret = ice_vsi_alloc_q_vectors(vsi);
2446 goto unroll_vsi_init;
2448 ret = ice_vsi_alloc_rings(vsi);
2450 goto unroll_alloc_q_vector;
2452 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2454 goto unroll_vector_base;
2456 /* Do not exit if configuring RSS had an issue, at least
2457 * receive traffic on first queue. Hence no need to capture
2460 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2461 ice_vsi_cfg_rss_lut_key(vsi);
2462 ice_vsi_set_vf_rss_flow_fld(vsi);
2466 ret = ice_vsi_alloc_rings(vsi);
2468 goto unroll_vsi_init;
2471 /* clean up the resources and exit */
2472 goto unroll_vsi_init;
2475 /* configure VSI nodes based on number of queues and TC's */
2476 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2477 max_txqs[i] = vsi->alloc_txq;
2479 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2482 dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2483 vsi->vsi_num, ice_stat_str(status));
2484 goto unroll_clear_rings;
2487 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2488 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2489 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2490 * The rule is added once for PF VSI in order to create appropriate
2491 * recipe, since VSI/VSI list is ignored with drop action...
2492 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2493 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2494 * settings in the HW.
2496 if (!ice_is_safe_mode(pf))
2497 if (vsi->type == ICE_VSI_PF) {
2498 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2500 ice_cfg_sw_lldp(vsi, true, true);
2504 ice_set_agg_vsi(vsi);
2508 ice_vsi_clear_rings(vsi);
2510 /* reclaim SW interrupts back to the common pool */
2511 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2512 pf->num_avail_sw_msix += vsi->num_q_vectors;
2513 unroll_alloc_q_vector:
2514 ice_vsi_free_q_vectors(vsi);
2516 ice_vsi_delete(vsi);
2518 ice_vsi_put_qs(vsi);
2520 if (vsi_type == ICE_VSI_VF)
2521 ice_enable_lag(pf->lag);
2528 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2529 * @vsi: the VSI being cleaned up
2531 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2533 struct ice_pf *pf = vsi->back;
2534 struct ice_hw *hw = &pf->hw;
2539 for (i = 0; i < vsi->num_q_vectors; i++) {
2540 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2542 ice_write_intrl(q_vector, 0);
2543 for (q = 0; q < q_vector->num_ring_tx; q++) {
2544 ice_write_itr(&q_vector->tx, 0);
2545 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2546 if (ice_is_xdp_ena_vsi(vsi)) {
2547 u32 xdp_txq = txq + vsi->num_xdp_txq;
2549 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2554 for (q = 0; q < q_vector->num_ring_rx; q++) {
2555 ice_write_itr(&q_vector->rx, 0);
2556 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2565 * ice_vsi_free_irq - Free the IRQ association with the OS
2566 * @vsi: the VSI being configured
2568 void ice_vsi_free_irq(struct ice_vsi *vsi)
2570 struct ice_pf *pf = vsi->back;
2571 int base = vsi->base_vector;
2574 if (!vsi->q_vectors || !vsi->irqs_ready)
2577 ice_vsi_release_msix(vsi);
2578 if (vsi->type == ICE_VSI_VF)
2581 vsi->irqs_ready = false;
2582 ice_for_each_q_vector(vsi, i) {
2583 u16 vector = i + base;
2586 irq_num = pf->msix_entries[vector].vector;
2588 /* free only the irqs that were actually requested */
2589 if (!vsi->q_vectors[i] ||
2590 !(vsi->q_vectors[i]->num_ring_tx ||
2591 vsi->q_vectors[i]->num_ring_rx))
2594 /* clear the affinity notifier in the IRQ descriptor */
2595 irq_set_affinity_notifier(irq_num, NULL);
2597 /* clear the affinity_mask in the IRQ descriptor */
2598 irq_set_affinity_hint(irq_num, NULL);
2599 synchronize_irq(irq_num);
2600 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2605 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2606 * @vsi: the VSI having resources freed
2608 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2615 ice_for_each_txq(vsi, i)
2616 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2617 ice_free_tx_ring(vsi->tx_rings[i]);
2621 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2622 * @vsi: the VSI having resources freed
2624 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2631 ice_for_each_rxq(vsi, i)
2632 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2633 ice_free_rx_ring(vsi->rx_rings[i]);
2637 * ice_vsi_close - Shut down a VSI
2638 * @vsi: the VSI being shut down
2640 void ice_vsi_close(struct ice_vsi *vsi)
2642 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2645 ice_vsi_free_irq(vsi);
2646 ice_vsi_free_tx_rings(vsi);
2647 ice_vsi_free_rx_rings(vsi);
2651 * ice_ena_vsi - resume a VSI
2652 * @vsi: the VSI being resume
2653 * @locked: is the rtnl_lock already held
2655 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2659 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2662 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2664 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2665 if (netif_running(vsi->netdev)) {
2669 err = ice_open_internal(vsi->netdev);
2674 } else if (vsi->type == ICE_VSI_CTRL) {
2675 err = ice_vsi_open_ctrl(vsi);
2682 * ice_dis_vsi - pause a VSI
2683 * @vsi: the VSI being paused
2684 * @locked: is the rtnl_lock already held
2686 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2688 if (test_bit(ICE_VSI_DOWN, vsi->state))
2691 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2693 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2694 if (netif_running(vsi->netdev)) {
2705 } else if (vsi->type == ICE_VSI_CTRL) {
2711 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2712 * @vsi: the VSI being un-configured
2714 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2716 int base = vsi->base_vector;
2717 struct ice_pf *pf = vsi->back;
2718 struct ice_hw *hw = &pf->hw;
2722 /* disable interrupt causation from each queue */
2723 if (vsi->tx_rings) {
2724 ice_for_each_txq(vsi, i) {
2725 if (vsi->tx_rings[i]) {
2728 reg = vsi->tx_rings[i]->reg_idx;
2729 val = rd32(hw, QINT_TQCTL(reg));
2730 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2731 wr32(hw, QINT_TQCTL(reg), val);
2736 if (vsi->rx_rings) {
2737 ice_for_each_rxq(vsi, i) {
2738 if (vsi->rx_rings[i]) {
2741 reg = vsi->rx_rings[i]->reg_idx;
2742 val = rd32(hw, QINT_RQCTL(reg));
2743 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2744 wr32(hw, QINT_RQCTL(reg), val);
2749 /* disable each interrupt */
2750 ice_for_each_q_vector(vsi, i) {
2751 if (!vsi->q_vectors[i])
2753 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2758 /* don't call synchronize_irq() for VF's from the host */
2759 if (vsi->type == ICE_VSI_VF)
2762 ice_for_each_q_vector(vsi, i)
2763 synchronize_irq(pf->msix_entries[i + base].vector);
2767 * ice_napi_del - Remove NAPI handler for the VSI
2768 * @vsi: VSI for which NAPI handler is to be removed
2770 void ice_napi_del(struct ice_vsi *vsi)
2777 ice_for_each_q_vector(vsi, v_idx)
2778 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2782 * ice_vsi_release - Delete a VSI and free its resources
2783 * @vsi: the VSI being removed
2785 * Returns 0 on success or < 0 on error
2787 int ice_vsi_release(struct ice_vsi *vsi)
2795 /* do not unregister while driver is in the reset recovery pending
2796 * state. Since reset/rebuild happens through PF service task workqueue,
2797 * it's not a good idea to unregister netdev that is associated to the
2798 * PF that is running the work queue items currently. This is done to
2799 * avoid check_flush_dependency() warning on this wq
2801 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2802 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2803 unregister_netdev(vsi->netdev);
2804 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2807 ice_devlink_destroy_port(vsi);
2809 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2812 /* Disable VSI and free resources */
2813 if (vsi->type != ICE_VSI_LB)
2814 ice_vsi_dis_irq(vsi);
2817 /* SR-IOV determines needed MSIX resources all at once instead of per
2818 * VSI since when VFs are spawned we know how many VFs there are and how
2819 * many interrupts each VF needs. SR-IOV MSIX resources are also
2820 * cleared in the same manner.
2822 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2826 ice_for_each_vf(pf, i) {
2828 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2831 if (i == pf->num_alloc_vfs) {
2832 /* No other VFs left that have control VSI, reclaim SW
2833 * interrupts back to the common pool
2835 ice_free_res(pf->irq_tracker, vsi->base_vector,
2836 ICE_RES_VF_CTRL_VEC_ID);
2837 pf->num_avail_sw_msix += vsi->num_q_vectors;
2839 } else if (vsi->type != ICE_VSI_VF) {
2840 /* reclaim SW interrupts back to the common pool */
2841 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2842 pf->num_avail_sw_msix += vsi->num_q_vectors;
2845 if (!ice_is_safe_mode(pf)) {
2846 if (vsi->type == ICE_VSI_PF) {
2847 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2849 ice_cfg_sw_lldp(vsi, true, false);
2850 /* The Rx rule will only exist to remove if the LLDP FW
2851 * engine is currently stopped
2853 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2854 ice_cfg_sw_lldp(vsi, false, false);
2858 ice_fltr_remove_all(vsi);
2859 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2860 ice_vsi_delete(vsi);
2861 ice_vsi_free_q_vectors(vsi);
2864 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2865 unregister_netdev(vsi->netdev);
2866 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2868 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2869 free_netdev(vsi->netdev);
2871 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2875 if (vsi->type == ICE_VSI_VF &&
2876 vsi->agg_node && vsi->agg_node->valid)
2877 vsi->agg_node->num_vsis--;
2878 ice_vsi_clear_rings(vsi);
2880 ice_vsi_put_qs(vsi);
2882 /* retain SW VSI data structure since it is needed to unregister and
2883 * free VSI netdev when PF is not in reset recovery pending state,\
2884 * for ex: during rmmod.
2886 if (!ice_is_reset_in_progress(pf->state))
2893 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2894 * @vsi: VSI connected with q_vectors
2895 * @coalesce: array of struct with stored coalesce
2897 * Returns array size.
2900 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2901 struct ice_coalesce_stored *coalesce)
2905 ice_for_each_q_vector(vsi, i) {
2906 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2908 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2909 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2910 coalesce[i].intrl = q_vector->intrl;
2912 if (i < vsi->num_txq)
2913 coalesce[i].tx_valid = true;
2914 if (i < vsi->num_rxq)
2915 coalesce[i].rx_valid = true;
2918 return vsi->num_q_vectors;
2922 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2923 * @vsi: VSI connected with q_vectors
2924 * @coalesce: pointer to array of struct with stored coalesce
2925 * @size: size of coalesce array
2927 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2928 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2932 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2933 struct ice_coalesce_stored *coalesce, int size)
2935 struct ice_ring_container *rc;
2938 if ((size && !coalesce) || !vsi)
2941 /* There are a couple of cases that have to be handled here:
2942 * 1. The case where the number of queue vectors stays the same, but
2943 * the number of Tx or Rx rings changes (the first for loop)
2944 * 2. The case where the number of queue vectors increased (the
2947 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2948 /* There are 2 cases to handle here and they are the same for
2950 * if the entry was valid previously (coalesce[i].[tr]x_valid
2951 * and the loop variable is less than the number of rings
2952 * allocated, then write the previous values
2954 * if the entry was not valid previously, but the number of
2955 * rings is less than are allocated (this means the number of
2956 * rings increased from previously), then write out the
2957 * values in the first element
2959 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
2960 * as there is no harm because the dynamic algorithm
2961 * will just overwrite.
2963 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2964 rc = &vsi->q_vectors[i]->rx;
2965 rc->itr_setting = coalesce[i].itr_rx;
2966 ice_write_itr(rc, rc->itr_setting);
2967 } else if (i < vsi->alloc_rxq) {
2968 rc = &vsi->q_vectors[i]->rx;
2969 rc->itr_setting = coalesce[0].itr_rx;
2970 ice_write_itr(rc, rc->itr_setting);
2973 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2974 rc = &vsi->q_vectors[i]->tx;
2975 rc->itr_setting = coalesce[i].itr_tx;
2976 ice_write_itr(rc, rc->itr_setting);
2977 } else if (i < vsi->alloc_txq) {
2978 rc = &vsi->q_vectors[i]->tx;
2979 rc->itr_setting = coalesce[0].itr_tx;
2980 ice_write_itr(rc, rc->itr_setting);
2983 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2984 ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
2987 /* the number of queue vectors increased so write whatever is in
2990 for (; i < vsi->num_q_vectors; i++) {
2992 rc = &vsi->q_vectors[i]->tx;
2993 rc->itr_setting = coalesce[0].itr_tx;
2994 ice_write_itr(rc, rc->itr_setting);
2997 rc = &vsi->q_vectors[i]->rx;
2998 rc->itr_setting = coalesce[0].itr_rx;
2999 ice_write_itr(rc, rc->itr_setting);
3001 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3002 ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3007 * ice_vsi_rebuild - Rebuild VSI after reset
3008 * @vsi: VSI to be rebuild
3009 * @init_vsi: is this an initialization or a reconfigure of the VSI
3011 * Returns 0 on success and negative value on failure
3013 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3015 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3016 struct ice_coalesce_stored *coalesce;
3017 int prev_num_q_vectors = 0;
3018 struct ice_vf *vf = NULL;
3019 enum ice_vsi_type vtype;
3020 enum ice_status status;
3029 if (vtype == ICE_VSI_VF)
3030 vf = &pf->vf[vsi->vf_id];
3032 coalesce = kcalloc(vsi->num_q_vectors,
3033 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3037 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3039 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3040 ice_vsi_free_q_vectors(vsi);
3042 /* SR-IOV determines needed MSIX resources all at once instead of per
3043 * VSI since when VFs are spawned we know how many VFs there are and how
3044 * many interrupts each VF needs. SR-IOV MSIX resources are also
3045 * cleared in the same manner.
3047 if (vtype != ICE_VSI_VF) {
3048 /* reclaim SW interrupts back to the common pool */
3049 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3050 pf->num_avail_sw_msix += vsi->num_q_vectors;
3051 vsi->base_vector = 0;
3054 if (ice_is_xdp_ena_vsi(vsi))
3055 /* return value check can be skipped here, it always returns
3056 * 0 if reset is in progress
3058 ice_destroy_xdp_rings(vsi);
3059 ice_vsi_put_qs(vsi);
3060 ice_vsi_clear_rings(vsi);
3061 ice_vsi_free_arrays(vsi);
3062 if (vtype == ICE_VSI_VF)
3063 ice_vsi_set_num_qs(vsi, vf->vf_id);
3065 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3067 ret = ice_vsi_alloc_arrays(vsi);
3071 ice_vsi_get_qs(vsi);
3073 ice_alloc_fd_res(vsi);
3074 ice_vsi_set_tc_cfg(vsi);
3076 /* Initialize VSI struct elements and create VSI in FW */
3077 ret = ice_vsi_init(vsi, init_vsi);
3084 ret = ice_vsi_alloc_q_vectors(vsi);
3088 ret = ice_vsi_setup_vector_base(vsi);
3092 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3096 ret = ice_vsi_alloc_rings(vsi);
3100 ice_vsi_map_rings_to_vectors(vsi);
3101 if (ice_is_xdp_ena_vsi(vsi)) {
3102 vsi->num_xdp_txq = vsi->alloc_rxq;
3103 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3107 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3108 if (vtype != ICE_VSI_CTRL)
3109 /* Do not exit if configuring RSS had an issue, at
3110 * least receive traffic on first queue. Hence no
3111 * need to capture return value
3113 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3114 ice_vsi_cfg_rss_lut_key(vsi);
3117 ret = ice_vsi_alloc_q_vectors(vsi);
3121 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3125 ret = ice_vsi_alloc_rings(vsi);
3134 /* configure VSI nodes based on number of queues and TC's */
3135 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3136 max_txqs[i] = vsi->alloc_txq;
3138 if (ice_is_xdp_ena_vsi(vsi))
3139 max_txqs[i] += vsi->num_xdp_txq;
3142 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3145 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3146 vsi->vsi_num, ice_stat_str(status));
3151 return ice_schedule_reset(pf, ICE_RESET_PFR);
3154 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3160 ice_vsi_free_q_vectors(vsi);
3163 vsi->current_netdev_flags = 0;
3164 unregister_netdev(vsi->netdev);
3165 free_netdev(vsi->netdev);
3170 set_bit(ICE_RESET_FAILED, pf->state);
3176 * ice_is_reset_in_progress - check for a reset in progress
3177 * @state: PF state field
3179 bool ice_is_reset_in_progress(unsigned long *state)
3181 return test_bit(ICE_RESET_OICR_RECV, state) ||
3182 test_bit(ICE_PFR_REQ, state) ||
3183 test_bit(ICE_CORER_REQ, state) ||
3184 test_bit(ICE_GLOBR_REQ, state);
3189 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3190 * @vsi: VSI being configured
3191 * @ctx: the context buffer returned from AQ VSI update command
3193 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3195 vsi->info.mapping_flags = ctx->info.mapping_flags;
3196 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3197 sizeof(vsi->info.q_mapping));
3198 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3199 sizeof(vsi->info.tc_mapping));
3203 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3204 * @vsi: VSI to be configured
3205 * @ena_tc: TC bitmap
3207 * VSI queues expected to be quiesced before calling this function
3209 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3211 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3212 struct ice_pf *pf = vsi->back;
3213 struct ice_vsi_ctx *ctx;
3214 enum ice_status status;
3219 dev = ice_pf_to_dev(pf);
3221 ice_for_each_traffic_class(i) {
3222 /* build bitmap of enabled TCs */
3223 if (ena_tc & BIT(i))
3225 /* populate max_txqs per TC */
3226 max_txqs[i] = vsi->alloc_txq;
3229 vsi->tc_cfg.ena_tc = ena_tc;
3230 vsi->tc_cfg.numtc = num_tc;
3232 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3237 ctx->info = vsi->info;
3239 ice_vsi_setup_q_map(vsi, ctx);
3241 /* must to indicate which section of VSI context are being modified */
3242 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3243 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3245 dev_info(dev, "Failed VSI Update\n");
3250 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3254 dev_err(dev, "VSI %d failed TC config, error %s\n",
3255 vsi->vsi_num, ice_stat_str(status));
3259 ice_vsi_update_q_map(vsi, ctx);
3260 vsi->info.valid_sections = 0;
3262 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3267 #endif /* CONFIG_DCB */
3270 * ice_update_ring_stats - Update ring statistics
3271 * @ring: ring to update
3272 * @pkts: number of processed packets
3273 * @bytes: number of processed bytes
3275 * This function assumes that caller has acquired a u64_stats_sync lock.
3277 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3279 ring->stats.bytes += bytes;
3280 ring->stats.pkts += pkts;
3284 * ice_update_tx_ring_stats - Update Tx ring specific counters
3285 * @tx_ring: ring to update
3286 * @pkts: number of processed packets
3287 * @bytes: number of processed bytes
3289 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3291 u64_stats_update_begin(&tx_ring->syncp);
3292 ice_update_ring_stats(tx_ring, pkts, bytes);
3293 u64_stats_update_end(&tx_ring->syncp);
3297 * ice_update_rx_ring_stats - Update Rx ring specific counters
3298 * @rx_ring: ring to update
3299 * @pkts: number of processed packets
3300 * @bytes: number of processed bytes
3302 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3304 u64_stats_update_begin(&rx_ring->syncp);
3305 ice_update_ring_stats(rx_ring, pkts, bytes);
3306 u64_stats_update_end(&rx_ring->syncp);
3310 * ice_status_to_errno - convert from enum ice_status to Linux errno
3311 * @err: ice_status value to convert
3313 int ice_status_to_errno(enum ice_status err)
3318 case ICE_ERR_DOES_NOT_EXIST:
3320 case ICE_ERR_OUT_OF_RANGE:
3324 case ICE_ERR_NO_MEMORY:
3326 case ICE_ERR_MAX_LIMIT:
3334 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3335 * @sw: switch to check if its default forwarding VSI is free
3337 * Return true if the default forwarding VSI is already being used, else returns
3338 * false signalling that it's available to use.
3340 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3342 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3346 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3347 * @sw: switch for the default forwarding VSI to compare against
3348 * @vsi: VSI to compare against default forwarding VSI
3350 * If this VSI passed in is the default forwarding VSI then return true, else
3353 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3355 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3359 * ice_set_dflt_vsi - set the default forwarding VSI
3360 * @sw: switch used to assign the default forwarding VSI
3361 * @vsi: VSI getting set as the default forwarding VSI on the switch
3363 * If the VSI passed in is already the default VSI and it's enabled just return
3366 * If there is already a default VSI on the switch and it's enabled then return
3367 * -EEXIST since there can only be one default VSI per switch.
3369 * Otherwise try to set the VSI passed in as the switch's default VSI and
3370 * return the result.
3372 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3374 enum ice_status status;
3380 dev = ice_pf_to_dev(vsi->back);
3382 /* the VSI passed in is already the default VSI */
3383 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3384 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3389 /* another VSI is already the default VSI for this switch */
3390 if (ice_is_dflt_vsi_in_use(sw)) {
3391 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3392 sw->dflt_vsi->vsi_num);
3396 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3398 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3399 vsi->vsi_num, ice_stat_str(status));
3404 sw->dflt_vsi_ena = true;
3410 * ice_clear_dflt_vsi - clear the default forwarding VSI
3411 * @sw: switch used to clear the default VSI
3413 * If the switch has no default VSI or it's not enabled then return error.
3415 * Otherwise try to clear the default VSI and return the result.
3417 int ice_clear_dflt_vsi(struct ice_sw *sw)
3419 struct ice_vsi *dflt_vsi;
3420 enum ice_status status;
3426 dev = ice_pf_to_dev(sw->pf);
3428 dflt_vsi = sw->dflt_vsi;
3430 /* there is no default VSI configured */
3431 if (!ice_is_dflt_vsi_in_use(sw))
3434 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3437 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3438 dflt_vsi->vsi_num, ice_stat_str(status));
3442 sw->dflt_vsi = NULL;
3443 sw->dflt_vsi_ena = false;
3449 * ice_set_link - turn on/off physical link
3450 * @vsi: VSI to modify physical link on
3451 * @ena: turn on/off physical link
3453 int ice_set_link(struct ice_vsi *vsi, bool ena)
3455 struct device *dev = ice_pf_to_dev(vsi->back);
3456 struct ice_port_info *pi = vsi->port_info;
3457 struct ice_hw *hw = pi->hw;
3458 enum ice_status status;
3460 if (vsi->type != ICE_VSI_PF)
3463 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3465 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3466 * this is not a fatal error, so print a warning message and return
3467 * a success code. Return an error if FW returns an error code other
3468 * than ICE_AQ_RC_EMODE
3470 if (status == ICE_ERR_AQ_ERROR) {
3471 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3472 dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3473 (ena ? "ON" : "OFF"), ice_stat_str(status),
3474 ice_aq_str(hw->adminq.sq_last_status));
3475 } else if (status) {
3476 dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3477 (ena ? "ON" : "OFF"), ice_stat_str(status),
3478 ice_aq_str(hw->adminq.sq_last_status));