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);
108 vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
109 if (!vsi->af_xdp_zc_qps)
115 devm_kfree(dev, vsi->q_vectors);
117 devm_kfree(dev, vsi->rxq_map);
119 devm_kfree(dev, vsi->txq_map);
121 devm_kfree(dev, vsi->rx_rings);
123 devm_kfree(dev, vsi->tx_rings);
128 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
129 * @vsi: the VSI being configured
131 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
137 /* a user could change the values of num_[tr]x_desc using
138 * ethtool -G so we should keep those values instead of
139 * overwriting them with the defaults.
141 if (!vsi->num_rx_desc)
142 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
143 if (!vsi->num_tx_desc)
144 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
147 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
154 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
155 * @vsi: the VSI being configured
156 * @vf_id: ID of the VF being configured
158 * Return 0 on success and a negative value on error
160 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
162 struct ice_pf *pf = vsi->back;
163 struct ice_vf *vf = NULL;
165 if (vsi->type == ICE_VSI_VF)
168 vsi->vf_id = ICE_INVAL_VFID;
172 vsi->alloc_txq = min3(pf->num_lan_msix,
173 ice_get_avail_txq_count(pf),
174 (u16)num_online_cpus());
176 vsi->alloc_txq = vsi->req_txq;
177 vsi->num_txq = vsi->req_txq;
180 pf->num_lan_tx = vsi->alloc_txq;
182 /* only 1 Rx queue unless RSS is enabled */
183 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
186 vsi->alloc_rxq = min3(pf->num_lan_msix,
187 ice_get_avail_rxq_count(pf),
188 (u16)num_online_cpus());
190 vsi->alloc_rxq = vsi->req_rxq;
191 vsi->num_rxq = vsi->req_rxq;
195 pf->num_lan_rx = vsi->alloc_rxq;
197 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
198 max_t(int, vsi->alloc_rxq,
202 vf = &pf->vf[vsi->vf_id];
204 vf->num_vf_qs = vf->num_req_qs;
205 vsi->alloc_txq = vf->num_vf_qs;
206 vsi->alloc_rxq = vf->num_vf_qs;
207 /* pf->num_msix_per_vf includes (VF miscellaneous vector +
208 * data queue interrupts). Since vsi->num_q_vectors is number
209 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
210 * original vector count
212 vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
217 vsi->num_q_vectors = 1;
224 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
228 ice_vsi_set_num_desc(vsi);
232 * ice_get_free_slot - get the next non-NULL location index in array
233 * @array: array to search
234 * @size: size of the array
235 * @curr: last known occupied index to be used as a search hint
237 * void * is being used to keep the functionality generic. This lets us use this
238 * function on any array of pointers.
240 static int ice_get_free_slot(void *array, int size, int curr)
242 int **tmp_array = (int **)array;
245 if (curr < (size - 1) && !tmp_array[curr + 1]) {
250 while ((i < size) && (tmp_array[i]))
261 * ice_vsi_delete - delete a VSI from the switch
262 * @vsi: pointer to VSI being removed
264 static void ice_vsi_delete(struct ice_vsi *vsi)
266 struct ice_pf *pf = vsi->back;
267 struct ice_vsi_ctx *ctxt;
268 enum ice_status status;
270 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
274 if (vsi->type == ICE_VSI_VF)
275 ctxt->vf_num = vsi->vf_id;
276 ctxt->vsi_num = vsi->vsi_num;
278 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
280 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
282 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
283 vsi->vsi_num, ice_stat_str(status));
289 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
290 * @vsi: pointer to VSI being cleared
292 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
294 struct ice_pf *pf = vsi->back;
297 dev = ice_pf_to_dev(pf);
299 if (vsi->af_xdp_zc_qps) {
300 bitmap_free(vsi->af_xdp_zc_qps);
301 vsi->af_xdp_zc_qps = NULL;
303 /* free the ring and vector containers */
304 if (vsi->q_vectors) {
305 devm_kfree(dev, vsi->q_vectors);
306 vsi->q_vectors = NULL;
309 devm_kfree(dev, vsi->tx_rings);
310 vsi->tx_rings = NULL;
313 devm_kfree(dev, vsi->rx_rings);
314 vsi->rx_rings = NULL;
317 devm_kfree(dev, vsi->txq_map);
321 devm_kfree(dev, vsi->rxq_map);
327 * ice_vsi_clear - clean up and deallocate the provided VSI
328 * @vsi: pointer to VSI being cleared
330 * This deallocates the VSI's queue resources, removes it from the PF's
331 * VSI array if necessary, and deallocates the VSI
333 * Returns 0 on success, negative on failure
335 static int ice_vsi_clear(struct ice_vsi *vsi)
337 struct ice_pf *pf = NULL;
347 dev = ice_pf_to_dev(pf);
349 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
350 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
354 mutex_lock(&pf->sw_mutex);
355 /* updates the PF for this cleared VSI */
357 pf->vsi[vsi->idx] = NULL;
358 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
359 pf->next_vsi = vsi->idx;
360 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
361 vsi->vf_id != ICE_INVAL_VFID)
362 pf->next_vsi = vsi->idx;
364 ice_vsi_free_arrays(vsi);
365 mutex_unlock(&pf->sw_mutex);
366 devm_kfree(dev, vsi);
372 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
373 * @irq: interrupt number
374 * @data: pointer to a q_vector
376 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
378 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
380 if (!q_vector->tx.ring)
383 #define FDIR_RX_DESC_CLEAN_BUDGET 64
384 ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
385 ice_clean_ctrl_tx_irq(q_vector->tx.ring);
391 * ice_msix_clean_rings - MSIX mode Interrupt Handler
392 * @irq: interrupt number
393 * @data: pointer to a q_vector
395 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
397 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
399 if (!q_vector->tx.ring && !q_vector->rx.ring)
402 q_vector->total_events++;
404 napi_schedule(&q_vector->napi);
410 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
411 * @pf: board private structure
412 * @vsi_type: type of VSI
413 * @vf_id: ID of the VF being configured
415 * returns a pointer to a VSI on success, NULL on failure.
417 static struct ice_vsi *
418 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
420 struct device *dev = ice_pf_to_dev(pf);
421 struct ice_vsi *vsi = NULL;
423 /* Need to protect the allocation of the VSIs at the PF level */
424 mutex_lock(&pf->sw_mutex);
426 /* If we have already allocated our maximum number of VSIs,
427 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
428 * is available to be populated
430 if (pf->next_vsi == ICE_NO_VSI) {
431 dev_dbg(dev, "out of VSI slots!\n");
435 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
439 vsi->type = vsi_type;
441 set_bit(ICE_VSI_DOWN, vsi->state);
443 if (vsi_type == ICE_VSI_VF)
444 ice_vsi_set_num_qs(vsi, vf_id);
446 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
450 if (ice_vsi_alloc_arrays(vsi))
453 /* Setup default MSIX irq handler for VSI */
454 vsi->irq_handler = ice_msix_clean_rings;
457 if (ice_vsi_alloc_arrays(vsi))
460 /* Setup ctrl VSI MSIX irq handler */
461 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
464 if (ice_vsi_alloc_arrays(vsi))
468 if (ice_vsi_alloc_arrays(vsi))
472 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
476 if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
477 /* Use the last VSI slot as the index for PF control VSI */
478 vsi->idx = pf->num_alloc_vsi - 1;
479 pf->ctrl_vsi_idx = vsi->idx;
480 pf->vsi[vsi->idx] = vsi;
482 /* fill slot and make note of the index */
483 vsi->idx = pf->next_vsi;
484 pf->vsi[pf->next_vsi] = vsi;
486 /* prepare pf->next_vsi for next use */
487 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
491 if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
492 pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
496 devm_kfree(dev, vsi);
499 mutex_unlock(&pf->sw_mutex);
504 * ice_alloc_fd_res - Allocate FD resource for a VSI
505 * @vsi: pointer to the ice_vsi
507 * This allocates the FD resources
509 * Returns 0 on success, -EPERM on no-op or -EIO on failure
511 static int ice_alloc_fd_res(struct ice_vsi *vsi)
513 struct ice_pf *pf = vsi->back;
516 /* Flow Director filters are only allocated/assigned to the PF VSI which
517 * passes the traffic. The CTRL VSI is only used to add/delete filters
518 * so we don't allocate resources to it
521 /* FD filters from guaranteed pool per VSI */
522 g_val = pf->hw.func_caps.fd_fltr_guar;
526 /* FD filters from best effort pool */
527 b_val = pf->hw.func_caps.fd_fltr_best_effort;
531 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
534 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
537 vsi->num_gfltr = g_val / pf->num_alloc_vsi;
539 /* each VSI gets same "best_effort" quota */
540 vsi->num_bfltr = b_val;
542 if (vsi->type == ICE_VSI_VF) {
545 /* each VSI gets same "best_effort" quota */
546 vsi->num_bfltr = b_val;
553 * ice_vsi_get_qs - Assign queues from PF to VSI
554 * @vsi: the VSI to assign queues to
556 * Returns 0 on success and a negative value on error
558 static int ice_vsi_get_qs(struct ice_vsi *vsi)
560 struct ice_pf *pf = vsi->back;
561 struct ice_qs_cfg tx_qs_cfg = {
562 .qs_mutex = &pf->avail_q_mutex,
563 .pf_map = pf->avail_txqs,
564 .pf_map_size = pf->max_pf_txqs,
565 .q_count = vsi->alloc_txq,
566 .scatter_count = ICE_MAX_SCATTER_TXQS,
567 .vsi_map = vsi->txq_map,
569 .mapping_mode = ICE_VSI_MAP_CONTIG
571 struct ice_qs_cfg rx_qs_cfg = {
572 .qs_mutex = &pf->avail_q_mutex,
573 .pf_map = pf->avail_rxqs,
574 .pf_map_size = pf->max_pf_rxqs,
575 .q_count = vsi->alloc_rxq,
576 .scatter_count = ICE_MAX_SCATTER_RXQS,
577 .vsi_map = vsi->rxq_map,
579 .mapping_mode = ICE_VSI_MAP_CONTIG
583 ret = __ice_vsi_get_qs(&tx_qs_cfg);
586 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
588 ret = __ice_vsi_get_qs(&rx_qs_cfg);
591 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
597 * ice_vsi_put_qs - Release queues from VSI to PF
598 * @vsi: the VSI that is going to release queues
600 static void ice_vsi_put_qs(struct ice_vsi *vsi)
602 struct ice_pf *pf = vsi->back;
605 mutex_lock(&pf->avail_q_mutex);
607 for (i = 0; i < vsi->alloc_txq; i++) {
608 clear_bit(vsi->txq_map[i], pf->avail_txqs);
609 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
612 for (i = 0; i < vsi->alloc_rxq; i++) {
613 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
614 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
617 mutex_unlock(&pf->avail_q_mutex);
622 * @pf: pointer to the PF struct
624 * returns true if driver is in safe mode, false otherwise
626 bool ice_is_safe_mode(struct ice_pf *pf)
628 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
632 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
633 * @vsi: the VSI being cleaned up
635 * This function deletes RSS input set for all flows that were configured
638 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
640 struct ice_pf *pf = vsi->back;
641 enum ice_status status;
643 if (ice_is_safe_mode(pf))
646 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
648 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
649 vsi->vsi_num, ice_stat_str(status));
653 * ice_rss_clean - Delete RSS related VSI structures and configuration
654 * @vsi: the VSI being removed
656 static void ice_rss_clean(struct ice_vsi *vsi)
658 struct ice_pf *pf = vsi->back;
661 dev = ice_pf_to_dev(pf);
663 if (vsi->rss_hkey_user)
664 devm_kfree(dev, vsi->rss_hkey_user);
665 if (vsi->rss_lut_user)
666 devm_kfree(dev, vsi->rss_lut_user);
668 ice_vsi_clean_rss_flow_fld(vsi);
669 /* remove RSS replay list */
670 if (!ice_is_safe_mode(pf))
671 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
675 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
676 * @vsi: the VSI being configured
678 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
680 struct ice_hw_common_caps *cap;
681 struct ice_pf *pf = vsi->back;
683 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
688 cap = &pf->hw.func_caps.common_cap;
691 /* PF VSI will inherit RSS instance of PF */
692 vsi->rss_table_size = (u16)cap->rss_table_size;
693 vsi->rss_size = min_t(u16, num_online_cpus(),
694 BIT(cap->rss_table_entry_width));
695 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
698 /* VF VSI will get a small RSS table.
699 * For VSI_LUT, LUT size should be set to 64 bytes.
701 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
702 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
703 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
708 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
709 ice_vsi_type_str(vsi->type));
715 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
716 * @ctxt: the VSI context being set
718 * This initializes a default VSI context for all sections except the Queues.
720 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
724 memset(&ctxt->info, 0, sizeof(ctxt->info));
725 /* VSI's should be allocated from shared pool */
726 ctxt->alloc_from_pool = true;
727 /* Src pruning enabled by default */
728 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
729 /* Traffic from VSI can be sent to LAN */
730 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
731 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
732 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
733 * packets untagged/tagged.
735 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
736 ICE_AQ_VSI_VLAN_MODE_M) >>
737 ICE_AQ_VSI_VLAN_MODE_S);
738 /* Have 1:1 UP mapping for both ingress/egress tables */
739 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
740 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
741 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
742 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
743 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
744 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
745 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
746 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
747 ctxt->info.ingress_table = cpu_to_le32(table);
748 ctxt->info.egress_table = cpu_to_le32(table);
749 /* Have 1:1 UP mapping for outer to inner UP table */
750 ctxt->info.outer_up_table = cpu_to_le32(table);
751 /* No Outer tag support outer_tag_flags remains to zero */
755 * ice_vsi_setup_q_map - Setup a VSI queue map
756 * @vsi: the VSI being configured
757 * @ctxt: VSI context structure
759 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
761 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
762 u16 num_txq_per_tc, num_rxq_per_tc;
763 u16 qcount_tx = vsi->alloc_txq;
764 u16 qcount_rx = vsi->alloc_rxq;
765 bool ena_tc0 = false;
769 /* at least TC0 should be enabled by default */
770 if (vsi->tc_cfg.numtc) {
771 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
779 vsi->tc_cfg.ena_tc |= 1;
782 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
785 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
789 /* find the (rounded up) power-of-2 of qcount */
790 pow = (u16)order_base_2(num_rxq_per_tc);
792 /* TC mapping is a function of the number of Rx queues assigned to the
793 * VSI for each traffic class and the offset of these queues.
794 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
795 * queues allocated to TC0. No:of queues is a power-of-2.
797 * If TC is not enabled, the queue offset is set to 0, and allocate one
798 * queue, this way, traffic for the given TC will be sent to the default
801 * Setup number and offset of Rx queues for all TCs for the VSI
803 ice_for_each_traffic_class(i) {
804 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
805 /* TC is not enabled */
806 vsi->tc_cfg.tc_info[i].qoffset = 0;
807 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
808 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
809 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
810 ctxt->info.tc_mapping[i] = 0;
815 vsi->tc_cfg.tc_info[i].qoffset = offset;
816 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
817 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
818 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
820 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
821 ICE_AQ_VSI_TC_Q_OFFSET_M) |
822 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
823 ICE_AQ_VSI_TC_Q_NUM_M);
824 offset += num_rxq_per_tc;
825 tx_count += num_txq_per_tc;
826 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
829 /* if offset is non-zero, means it is calculated correctly based on
830 * enabled TCs for a given VSI otherwise qcount_rx will always
831 * be correct and non-zero because it is based off - VSI's
832 * allocated Rx queues which is at least 1 (hence qcount_tx will be
836 vsi->num_rxq = offset;
838 vsi->num_rxq = num_rxq_per_tc;
840 vsi->num_txq = tx_count;
842 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
843 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
844 /* since there is a chance that num_rxq could have been changed
845 * in the above for loop, make num_txq equal to num_rxq.
847 vsi->num_txq = vsi->num_rxq;
850 /* Rx queue mapping */
851 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
852 /* q_mapping buffer holds the info for the first queue allocated for
853 * this VSI in the PF space and also the number of queues associated
856 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
857 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
861 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
862 * @ctxt: the VSI context being set
863 * @vsi: the VSI being configured
865 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
867 u8 dflt_q_group, dflt_q_prio;
868 u16 dflt_q, report_q, val;
870 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
871 vsi->type != ICE_VSI_VF)
874 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
875 ctxt->info.valid_sections |= cpu_to_le16(val);
881 /* enable flow director filtering/programming */
882 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
883 ctxt->info.fd_options = cpu_to_le16(val);
884 /* max of allocated flow director filters */
885 ctxt->info.max_fd_fltr_dedicated =
886 cpu_to_le16(vsi->num_gfltr);
887 /* max of shared flow director filters any VSI may program */
888 ctxt->info.max_fd_fltr_shared =
889 cpu_to_le16(vsi->num_bfltr);
890 /* default queue index within the VSI of the default FD */
891 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
892 ICE_AQ_VSI_FD_DEF_Q_M);
893 /* target queue or queue group to the FD filter */
894 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
895 ICE_AQ_VSI_FD_DEF_GRP_M);
896 ctxt->info.fd_def_q = cpu_to_le16(val);
897 /* queue index on which FD filter completion is reported */
898 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
899 ICE_AQ_VSI_FD_REPORT_Q_M);
900 /* priority of the default qindex action */
901 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
902 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
903 ctxt->info.fd_report_opt = cpu_to_le16(val);
907 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
908 * @ctxt: the VSI context being set
909 * @vsi: the VSI being configured
911 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
913 u8 lut_type, hash_type;
918 dev = ice_pf_to_dev(pf);
922 /* PF VSI will inherit RSS instance of PF */
923 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
924 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
927 /* VF VSI will gets a small RSS table which is a VSI LUT type */
928 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
929 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
932 dev_dbg(dev, "Unsupported VSI type %s\n",
933 ice_vsi_type_str(vsi->type));
937 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
938 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
939 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
940 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
944 * ice_vsi_init - Create and initialize a VSI
945 * @vsi: the VSI being configured
946 * @init_vsi: is this call creating a VSI
948 * This initializes a VSI context depending on the VSI type to be added and
949 * passes it down to the add_vsi aq command to create a new VSI.
951 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
953 struct ice_pf *pf = vsi->back;
954 struct ice_hw *hw = &pf->hw;
955 struct ice_vsi_ctx *ctxt;
959 dev = ice_pf_to_dev(pf);
960 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
968 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
971 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
972 /* VF number here is the absolute VF number (0-255) */
973 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
980 ice_set_dflt_vsi_ctx(ctxt);
981 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
982 ice_set_fd_vsi_ctx(ctxt, vsi);
983 /* if the switch is in VEB mode, allow VSI loopback */
984 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
985 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
987 /* Set LUT type and HASH type if RSS is enabled */
988 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
989 vsi->type != ICE_VSI_CTRL) {
990 ice_set_rss_vsi_ctx(ctxt, vsi);
991 /* if updating VSI context, make sure to set valid_section:
992 * to indicate which section of VSI context being updated
995 ctxt->info.valid_sections |=
996 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
999 ctxt->info.sw_id = vsi->port_info->sw_id;
1000 ice_vsi_setup_q_map(vsi, ctxt);
1001 if (!init_vsi) /* means VSI being updated */
1002 /* must to indicate which section of VSI context are
1005 ctxt->info.valid_sections |=
1006 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1008 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
1011 if (vsi->type == ICE_VSI_VF) {
1012 ctxt->info.valid_sections |=
1013 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1014 if (pf->vf[vsi->vf_id].spoofchk) {
1015 ctxt->info.sec_flags |=
1016 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1017 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1018 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1020 ctxt->info.sec_flags &=
1021 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1022 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1023 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1027 /* Allow control frames out of main VSI */
1028 if (vsi->type == ICE_VSI_PF) {
1029 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1030 ctxt->info.valid_sections |=
1031 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1035 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1037 dev_err(dev, "Add VSI failed, err %d\n", ret);
1042 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1044 dev_err(dev, "Update VSI failed, err %d\n", ret);
1050 /* keep context for update VSI operations */
1051 vsi->info = ctxt->info;
1053 /* record VSI number returned */
1054 vsi->vsi_num = ctxt->vsi_num;
1062 * ice_free_res - free a block of resources
1063 * @res: pointer to the resource
1064 * @index: starting index previously returned by ice_get_res
1065 * @id: identifier to track owner
1067 * Returns number of resources freed
1069 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1074 if (!res || index >= res->end)
1077 id |= ICE_RES_VALID_BIT;
1078 for (i = index; i < res->end && res->list[i] == id; i++) {
1087 * ice_search_res - Search the tracker for a block of resources
1088 * @res: pointer to the resource
1089 * @needed: size of the block needed
1090 * @id: identifier to track owner
1092 * Returns the base item index of the block, or -ENOMEM for error
1094 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1096 u16 start = 0, end = 0;
1098 if (needed > res->end)
1101 id |= ICE_RES_VALID_BIT;
1104 /* skip already allocated entries */
1105 if (res->list[end++] & ICE_RES_VALID_BIT) {
1107 if ((start + needed) > res->end)
1111 if (end == (start + needed)) {
1114 /* there was enough, so assign it to the requestor */
1116 res->list[i++] = id;
1120 } while (end < res->end);
1126 * ice_get_free_res_count - Get free count from a resource tracker
1127 * @res: Resource tracker instance
1129 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1133 for (i = 0; i < res->end; i++)
1134 if (!(res->list[i] & ICE_RES_VALID_BIT))
1141 * ice_get_res - get a block of resources
1142 * @pf: board private structure
1143 * @res: pointer to the resource
1144 * @needed: size of the block needed
1145 * @id: identifier to track owner
1147 * Returns the base item index of the block, or negative for error
1150 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1155 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1156 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1157 needed, res->num_entries, id);
1161 return ice_search_res(res, needed, id);
1165 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1166 * @vsi: ptr to the VSI
1168 * This should only be called after ice_vsi_alloc() which allocates the
1169 * corresponding SW VSI structure and initializes num_queue_pairs for the
1170 * newly allocated VSI.
1172 * Returns 0 on success or negative on failure
1174 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1176 struct ice_pf *pf = vsi->back;
1181 dev = ice_pf_to_dev(pf);
1182 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1183 if (vsi->type == ICE_VSI_VF)
1186 if (vsi->base_vector) {
1187 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1188 vsi->vsi_num, vsi->base_vector);
1192 num_q_vectors = vsi->num_q_vectors;
1193 /* reserve slots from OS requested IRQs */
1194 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1198 ice_for_each_vf(pf, i) {
1200 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1201 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1205 if (i == pf->num_alloc_vfs)
1206 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1207 ICE_RES_VF_CTRL_VEC_ID);
1209 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1214 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1215 ice_get_free_res_count(pf->irq_tracker),
1216 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1219 vsi->base_vector = (u16)base;
1220 pf->num_avail_sw_msix -= num_q_vectors;
1226 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1227 * @vsi: the VSI having rings deallocated
1229 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1233 /* Avoid stale references by clearing map from vector to ring */
1234 if (vsi->q_vectors) {
1235 ice_for_each_q_vector(vsi, i) {
1236 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1239 q_vector->tx.ring = NULL;
1240 q_vector->rx.ring = NULL;
1245 if (vsi->tx_rings) {
1246 for (i = 0; i < vsi->alloc_txq; i++) {
1247 if (vsi->tx_rings[i]) {
1248 kfree_rcu(vsi->tx_rings[i], rcu);
1249 WRITE_ONCE(vsi->tx_rings[i], NULL);
1253 if (vsi->rx_rings) {
1254 for (i = 0; i < vsi->alloc_rxq; i++) {
1255 if (vsi->rx_rings[i]) {
1256 kfree_rcu(vsi->rx_rings[i], rcu);
1257 WRITE_ONCE(vsi->rx_rings[i], NULL);
1264 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1265 * @vsi: VSI which is having rings allocated
1267 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1269 struct ice_pf *pf = vsi->back;
1273 dev = ice_pf_to_dev(pf);
1274 /* Allocate Tx rings */
1275 for (i = 0; i < vsi->alloc_txq; i++) {
1276 struct ice_ring *ring;
1278 /* allocate with kzalloc(), free with kfree_rcu() */
1279 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1285 ring->reg_idx = vsi->txq_map[i];
1286 ring->ring_active = false;
1289 ring->count = vsi->num_tx_desc;
1290 WRITE_ONCE(vsi->tx_rings[i], ring);
1293 /* Allocate Rx rings */
1294 for (i = 0; i < vsi->alloc_rxq; i++) {
1295 struct ice_ring *ring;
1297 /* allocate with kzalloc(), free with kfree_rcu() */
1298 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1303 ring->reg_idx = vsi->rxq_map[i];
1304 ring->ring_active = false;
1306 ring->netdev = vsi->netdev;
1308 ring->count = vsi->num_rx_desc;
1309 WRITE_ONCE(vsi->rx_rings[i], ring);
1315 ice_vsi_clear_rings(vsi);
1320 * ice_vsi_manage_rss_lut - disable/enable RSS
1321 * @vsi: the VSI being changed
1322 * @ena: boolean value indicating if this is an enable or disable request
1324 * In the event of disable request for RSS, this function will zero out RSS
1325 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1328 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1332 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1337 if (vsi->rss_lut_user)
1338 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1340 ice_fill_rss_lut(lut, vsi->rss_table_size,
1344 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1349 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1350 * @vsi: VSI to be configured
1352 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1354 struct ice_pf *pf = vsi->back;
1359 dev = ice_pf_to_dev(pf);
1360 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1362 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1366 if (vsi->rss_lut_user)
1367 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1369 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1371 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1373 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1374 goto ice_vsi_cfg_rss_exit;
1377 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1380 goto ice_vsi_cfg_rss_exit;
1383 if (vsi->rss_hkey_user)
1384 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1386 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1388 err = ice_set_rss_key(vsi, key);
1390 dev_err(dev, "set_rss_key failed, error %d\n", err);
1393 ice_vsi_cfg_rss_exit:
1399 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1400 * @vsi: VSI to be configured
1402 * This function will only be called during the VF VSI setup. Upon successful
1403 * completion of package download, this function will configure default RSS
1404 * input sets for VF VSI.
1406 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1408 struct ice_pf *pf = vsi->back;
1409 enum ice_status status;
1412 dev = ice_pf_to_dev(pf);
1413 if (ice_is_safe_mode(pf)) {
1414 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1419 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1421 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1422 vsi->vsi_num, ice_stat_str(status));
1426 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1427 * @vsi: VSI to be configured
1429 * This function will only be called after successful download package call
1430 * during initialization of PF. Since the downloaded package will erase the
1431 * RSS section, this function will configure RSS input sets for different
1432 * flow types. The last profile added has the highest priority, therefore 2
1433 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1434 * (i.e. IPv4 src/dst TCP src/dst port).
1436 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1438 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1439 struct ice_pf *pf = vsi->back;
1440 struct ice_hw *hw = &pf->hw;
1441 enum ice_status status;
1444 dev = ice_pf_to_dev(pf);
1445 if (ice_is_safe_mode(pf)) {
1446 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1450 /* configure RSS for IPv4 with input set IP src/dst */
1451 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1452 ICE_FLOW_SEG_HDR_IPV4);
1454 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1455 vsi_num, ice_stat_str(status));
1457 /* configure RSS for IPv6 with input set IPv6 src/dst */
1458 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1459 ICE_FLOW_SEG_HDR_IPV6);
1461 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1462 vsi_num, ice_stat_str(status));
1464 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1465 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1466 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1468 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1469 vsi_num, ice_stat_str(status));
1471 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1472 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1473 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1475 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1476 vsi_num, ice_stat_str(status));
1478 /* configure RSS for sctp4 with input set IP src/dst */
1479 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1480 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1482 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1483 vsi_num, ice_stat_str(status));
1485 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1486 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1487 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1489 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1490 vsi_num, ice_stat_str(status));
1492 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1493 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1494 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1496 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1497 vsi_num, ice_stat_str(status));
1499 /* configure RSS for sctp6 with input set IPv6 src/dst */
1500 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1501 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1503 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1504 vsi_num, ice_stat_str(status));
1508 * ice_pf_state_is_nominal - checks the PF for nominal state
1509 * @pf: pointer to PF to check
1511 * Check the PF's state for a collection of bits that would indicate
1512 * the PF is in a state that would inhibit normal operation for
1513 * driver functionality.
1515 * Returns true if PF is in a nominal state, false otherwise
1517 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1519 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1524 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1525 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1532 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1533 * @vsi: the VSI to be updated
1535 void ice_update_eth_stats(struct ice_vsi *vsi)
1537 struct ice_eth_stats *prev_es, *cur_es;
1538 struct ice_hw *hw = &vsi->back->hw;
1539 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1541 prev_es = &vsi->eth_stats_prev;
1542 cur_es = &vsi->eth_stats;
1544 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1545 &prev_es->rx_bytes, &cur_es->rx_bytes);
1547 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1548 &prev_es->rx_unicast, &cur_es->rx_unicast);
1550 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1551 &prev_es->rx_multicast, &cur_es->rx_multicast);
1553 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1554 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1556 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1557 &prev_es->rx_discards, &cur_es->rx_discards);
1559 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1560 &prev_es->tx_bytes, &cur_es->tx_bytes);
1562 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1563 &prev_es->tx_unicast, &cur_es->tx_unicast);
1565 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1566 &prev_es->tx_multicast, &cur_es->tx_multicast);
1568 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1569 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1571 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1572 &prev_es->tx_errors, &cur_es->tx_errors);
1574 vsi->stat_offsets_loaded = true;
1578 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1579 * @vsi: the VSI being configured
1580 * @vid: VLAN ID to be added
1581 * @action: filter action to be performed on match
1584 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1586 struct ice_pf *pf = vsi->back;
1590 dev = ice_pf_to_dev(pf);
1592 if (!ice_fltr_add_vlan(vsi, vid, action)) {
1596 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1604 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1605 * @vsi: the VSI being configured
1606 * @vid: VLAN ID to be removed
1608 * Returns 0 on success and negative on failure
1610 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1612 struct ice_pf *pf = vsi->back;
1613 enum ice_status status;
1617 dev = ice_pf_to_dev(pf);
1619 status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1622 } else if (status == ICE_ERR_DOES_NOT_EXIST) {
1623 dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1624 vid, vsi->vsi_num, ice_stat_str(status));
1626 dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1627 vid, vsi->vsi_num, ice_stat_str(status));
1635 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1638 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1640 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1641 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1642 vsi->rx_buf_len = ICE_RXBUF_2048;
1643 #if (PAGE_SIZE < 8192)
1644 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1645 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1646 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1647 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1650 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1651 #if (PAGE_SIZE < 8192)
1652 vsi->rx_buf_len = ICE_RXBUF_3072;
1654 vsi->rx_buf_len = ICE_RXBUF_2048;
1660 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1662 * @pf_q: index of the Rx queue in the PF's queue space
1663 * @rxdid: flexible descriptor RXDID
1664 * @prio: priority for the RXDID for this queue
1667 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio)
1669 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1671 /* clear any previous values */
1672 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1673 QRXFLXP_CNTXT_RXDID_PRIO_M |
1674 QRXFLXP_CNTXT_TS_M);
1676 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1677 QRXFLXP_CNTXT_RXDID_IDX_M;
1679 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1680 QRXFLXP_CNTXT_RXDID_PRIO_M;
1682 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1686 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1687 * @vsi: the VSI being configured
1689 * Return 0 on success and a negative value on error
1690 * Configure the Rx VSI for operation.
1692 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1696 if (vsi->type == ICE_VSI_VF)
1699 ice_vsi_cfg_frame_size(vsi);
1701 /* set up individual rings */
1702 for (i = 0; i < vsi->num_rxq; i++) {
1705 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1707 dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1717 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1718 * @vsi: the VSI being configured
1719 * @rings: Tx ring array to be configured
1721 * Return 0 on success and a negative value on error
1722 * Configure the Tx VSI for operation.
1725 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1727 struct ice_aqc_add_tx_qgrp *qg_buf;
1731 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1735 qg_buf->num_txqs = 1;
1737 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1738 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1749 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1750 * @vsi: the VSI being configured
1752 * Return 0 on success and a negative value on error
1753 * Configure the Tx VSI for operation.
1755 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1757 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1761 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1762 * @vsi: the VSI being configured
1764 * Return 0 on success and a negative value on error
1765 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1767 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1772 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1776 for (i = 0; i < vsi->num_xdp_txq; i++)
1777 vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1783 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1784 * @intrl: interrupt rate limit in usecs
1785 * @gran: interrupt rate limit granularity in usecs
1787 * This function converts a decimal interrupt rate limit in usecs to the format
1788 * expected by firmware.
1790 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1792 u32 val = intrl / gran;
1795 return val | GLINT_RATE_INTRL_ENA_M;
1800 * ice_write_intrl - write throttle rate limit to interrupt specific register
1801 * @q_vector: pointer to interrupt specific structure
1802 * @intrl: throttle rate limit in microseconds to write
1804 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1806 struct ice_hw *hw = &q_vector->vsi->back->hw;
1808 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1809 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1813 * __ice_write_itr - write throttle rate to register
1814 * @q_vector: pointer to interrupt data structure
1815 * @rc: pointer to ring container
1816 * @itr: throttle rate in microseconds to write
1818 static void __ice_write_itr(struct ice_q_vector *q_vector,
1819 struct ice_ring_container *rc, u16 itr)
1821 struct ice_hw *hw = &q_vector->vsi->back->hw;
1823 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1824 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1828 * ice_write_itr - write throttle rate to queue specific register
1829 * @rc: pointer to ring container
1830 * @itr: throttle rate in microseconds to write
1832 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1834 struct ice_q_vector *q_vector;
1839 q_vector = rc->ring->q_vector;
1841 __ice_write_itr(q_vector, rc, itr);
1845 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1846 * @vsi: the VSI being configured
1848 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1851 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1853 struct ice_pf *pf = vsi->back;
1854 struct ice_hw *hw = &pf->hw;
1855 u16 txq = 0, rxq = 0;
1858 for (i = 0; i < vsi->num_q_vectors; i++) {
1859 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1860 u16 reg_idx = q_vector->reg_idx;
1862 ice_cfg_itr(hw, q_vector);
1864 /* Both Transmit Queue Interrupt Cause Control register
1865 * and Receive Queue Interrupt Cause control register
1866 * expects MSIX_INDX field to be the vector index
1867 * within the function space and not the absolute
1868 * vector index across PF or across device.
1869 * For SR-IOV VF VSIs queue vector index always starts
1870 * with 1 since first vector index(0) is used for OICR
1871 * in VF space. Since VMDq and other PF VSIs are within
1872 * the PF function space, use the vector index that is
1873 * tracked for this PF.
1875 for (q = 0; q < q_vector->num_ring_tx; q++) {
1876 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1877 q_vector->tx.itr_idx);
1881 for (q = 0; q < q_vector->num_ring_rx; q++) {
1882 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1883 q_vector->rx.itr_idx);
1890 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1891 * @vsi: the VSI being changed
1893 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1895 struct ice_hw *hw = &vsi->back->hw;
1896 struct ice_vsi_ctx *ctxt;
1897 enum ice_status status;
1900 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1904 /* Here we are configuring the VSI to let the driver add VLAN tags by
1905 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1906 * insertion happens in the Tx hot path, in ice_tx_map.
1908 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1910 /* Preserve existing VLAN strip setting */
1911 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1912 ICE_AQ_VSI_VLAN_EMOD_M);
1914 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1916 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1918 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1919 ice_stat_str(status),
1920 ice_aq_str(hw->adminq.sq_last_status));
1925 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1932 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1933 * @vsi: the VSI being changed
1934 * @ena: boolean value indicating if this is a enable or disable request
1936 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1938 struct ice_hw *hw = &vsi->back->hw;
1939 struct ice_vsi_ctx *ctxt;
1940 enum ice_status status;
1943 /* do not allow modifying VLAN stripping when a port VLAN is configured
1949 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1953 /* Here we are configuring what the VSI should do with the VLAN tag in
1954 * the Rx packet. We can either leave the tag in the packet or put it in
1955 * the Rx descriptor.
1958 /* Strip VLAN tag from Rx packet and put it in the desc */
1959 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1961 /* Disable stripping. Leave tag in packet */
1962 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1964 /* Allow all packets untagged/tagged */
1965 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1967 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1969 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1971 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1972 ena, ice_stat_str(status),
1973 ice_aq_str(hw->adminq.sq_last_status));
1978 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1985 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1986 * @vsi: the VSI whose rings are to be enabled
1988 * Returns 0 on success and a negative value on error
1990 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1992 return ice_vsi_ctrl_all_rx_rings(vsi, true);
1996 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1997 * @vsi: the VSI whose rings are to be disabled
1999 * Returns 0 on success and a negative value on error
2001 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2003 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2007 * ice_vsi_stop_tx_rings - Disable Tx rings
2008 * @vsi: the VSI being configured
2009 * @rst_src: reset source
2010 * @rel_vmvf_num: Relative ID of VF/VM
2011 * @rings: Tx ring array to be stopped
2014 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2015 u16 rel_vmvf_num, struct ice_ring **rings)
2019 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2022 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
2023 struct ice_txq_meta txq_meta = { };
2026 if (!rings || !rings[q_idx])
2029 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2030 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2031 rings[q_idx], &txq_meta);
2041 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2042 * @vsi: the VSI being configured
2043 * @rst_src: reset source
2044 * @rel_vmvf_num: Relative ID of VF/VM
2047 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2050 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
2054 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2055 * @vsi: the VSI being configured
2057 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2059 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
2063 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2064 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2066 * returns true if Rx VLAN pruning is enabled and false otherwise.
2068 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2073 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2077 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2078 * @vsi: VSI to enable or disable VLAN pruning on
2079 * @ena: set to true to enable VLAN pruning and false to disable it
2080 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2082 * returns 0 if VSI is updated, negative otherwise
2084 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2086 struct ice_vsi_ctx *ctxt;
2093 /* Don't enable VLAN pruning if the netdev is currently in promiscuous
2094 * mode. VLAN pruning will be enabled when the interface exits
2095 * promiscuous mode if any VLAN filters are active.
2097 if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2101 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2105 ctxt->info = vsi->info;
2108 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2110 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2113 ctxt->info.valid_sections =
2114 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2116 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2118 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2119 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2120 ice_stat_str(status),
2121 ice_aq_str(pf->hw.adminq.sq_last_status));
2125 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2135 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2137 struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2139 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2140 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2144 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2145 * @vsi: VSI to set the q_vectors register index on
2148 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2152 if (!vsi || !vsi->q_vectors)
2155 ice_for_each_q_vector(vsi, i) {
2156 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2159 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2164 if (vsi->type == ICE_VSI_VF) {
2165 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2167 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2170 q_vector->v_idx + vsi->base_vector;
2177 ice_for_each_q_vector(vsi, i) {
2178 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2181 q_vector->reg_idx = 0;
2188 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2189 * @vsi: the VSI being configured
2190 * @tx: bool to determine Tx or Rx rule
2191 * @create: bool to determine create or remove Rule
2193 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2195 enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2196 enum ice_sw_fwd_act_type act);
2197 struct ice_pf *pf = vsi->back;
2198 enum ice_status status;
2201 dev = ice_pf_to_dev(pf);
2202 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2205 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2208 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2209 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2212 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2218 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2219 create ? "adding" : "removing", tx ? "TX" : "RX",
2220 vsi->vsi_num, ice_stat_str(status));
2224 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2225 * @vsi: pointer to the VSI
2227 * This function will allocate new scheduler aggregator now if needed and will
2228 * move specified VSI into it.
2230 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2232 struct device *dev = ice_pf_to_dev(vsi->back);
2233 struct ice_agg_node *agg_node_iter = NULL;
2234 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2235 struct ice_agg_node *agg_node = NULL;
2236 int node_offset, max_agg_nodes = 0;
2237 struct ice_port_info *port_info;
2238 struct ice_pf *pf = vsi->back;
2239 u32 agg_node_id_start = 0;
2240 enum ice_status status;
2242 /* create (as needed) scheduler aggregator node and move VSI into
2243 * corresponding aggregator node
2244 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2245 * - VF aggregator nodes will contain VF VSI
2247 port_info = pf->hw.port_info;
2251 switch (vsi->type) {
2255 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2256 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2257 agg_node_iter = &pf->pf_agg_node[0];
2260 /* user can create 'n' VFs on a given PF, but since max children
2261 * per aggregator node can be only 64. Following code handles
2262 * aggregator(s) for VF VSIs, either selects a agg_node which
2263 * was already created provided num_vsis < 64, otherwise
2264 * select next available node, which will be created
2266 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2267 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2268 agg_node_iter = &pf->vf_agg_node[0];
2271 /* other VSI type, handle later if needed */
2272 dev_dbg(dev, "unexpected VSI type %s\n",
2273 ice_vsi_type_str(vsi->type));
2277 /* find the appropriate aggregator node */
2278 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2279 /* see if we can find space in previously created
2280 * node if num_vsis < 64, otherwise skip
2282 if (agg_node_iter->num_vsis &&
2283 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2288 if (agg_node_iter->valid &&
2289 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2290 agg_id = agg_node_iter->agg_id;
2291 agg_node = agg_node_iter;
2295 /* find unclaimed agg_id */
2296 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2297 agg_id = node_offset + agg_node_id_start;
2298 agg_node = agg_node_iter;
2301 /* move to next agg_node */
2308 /* if selected aggregator node was not created, create it */
2309 if (!agg_node->valid) {
2310 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2311 (u8)vsi->tc_cfg.ena_tc);
2313 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2317 /* aggregator node is created, store the neeeded info */
2318 agg_node->valid = true;
2319 agg_node->agg_id = agg_id;
2322 /* move VSI to corresponding aggregator node */
2323 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2324 (u8)vsi->tc_cfg.ena_tc);
2326 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2331 /* keep active children count for aggregator node */
2332 agg_node->num_vsis++;
2334 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2335 * to aggregator node
2337 vsi->agg_node = agg_node;
2338 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2339 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2340 vsi->agg_node->num_vsis);
2344 * ice_vsi_setup - Set up a VSI by a given type
2345 * @pf: board private structure
2346 * @pi: pointer to the port_info instance
2347 * @vsi_type: VSI type
2348 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2349 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2350 * fill-in ICE_INVAL_VFID as input.
2352 * This allocates the sw VSI structure and its queue resources.
2354 * Returns pointer to the successfully allocated and configured VSI sw struct on
2355 * success, NULL on failure.
2358 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2359 enum ice_vsi_type vsi_type, u16 vf_id)
2361 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2362 struct device *dev = ice_pf_to_dev(pf);
2363 enum ice_status status;
2364 struct ice_vsi *vsi;
2367 if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2368 vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2370 vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2373 dev_err(dev, "could not allocate VSI\n");
2377 vsi->port_info = pi;
2378 vsi->vsw = pf->first_sw;
2379 if (vsi->type == ICE_VSI_PF)
2380 vsi->ethtype = ETH_P_PAUSE;
2382 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2385 ice_alloc_fd_res(vsi);
2387 if (ice_vsi_get_qs(vsi)) {
2388 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2390 goto unroll_vsi_alloc;
2393 /* set RSS capabilities */
2394 ice_vsi_set_rss_params(vsi);
2396 /* set TC configuration */
2397 ice_vsi_set_tc_cfg(vsi);
2399 /* create the VSI */
2400 ret = ice_vsi_init(vsi, true);
2404 switch (vsi->type) {
2407 ret = ice_vsi_alloc_q_vectors(vsi);
2409 goto unroll_vsi_init;
2411 ret = ice_vsi_setup_vector_base(vsi);
2413 goto unroll_alloc_q_vector;
2415 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2417 goto unroll_vector_base;
2419 ret = ice_vsi_alloc_rings(vsi);
2421 goto unroll_vector_base;
2423 /* Always add VLAN ID 0 switch rule by default. This is needed
2424 * in order to allow all untagged and 0 tagged priority traffic
2425 * if Rx VLAN pruning is enabled. Also there are cases where we
2426 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2427 * so this handles those cases (i.e. adding the PF to a bridge
2428 * without the 8021q module loaded).
2430 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2432 goto unroll_clear_rings;
2434 ice_vsi_map_rings_to_vectors(vsi);
2436 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2437 if (vsi->type != ICE_VSI_CTRL)
2438 /* Do not exit if configuring RSS had an issue, at
2439 * least receive traffic on first queue. Hence no
2440 * need to capture return value
2442 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2443 ice_vsi_cfg_rss_lut_key(vsi);
2444 ice_vsi_set_rss_flow_fld(vsi);
2449 /* VF driver will take care of creating netdev for this type and
2450 * map queues to vectors through Virtchnl, PF driver only
2451 * creates a VSI and corresponding structures for bookkeeping
2454 ret = ice_vsi_alloc_q_vectors(vsi);
2456 goto unroll_vsi_init;
2458 ret = ice_vsi_alloc_rings(vsi);
2460 goto unroll_alloc_q_vector;
2462 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2464 goto unroll_vector_base;
2466 /* Do not exit if configuring RSS had an issue, at least
2467 * receive traffic on first queue. Hence no need to capture
2470 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2471 ice_vsi_cfg_rss_lut_key(vsi);
2472 ice_vsi_set_vf_rss_flow_fld(vsi);
2476 ret = ice_vsi_alloc_rings(vsi);
2478 goto unroll_vsi_init;
2481 /* clean up the resources and exit */
2482 goto unroll_vsi_init;
2485 /* configure VSI nodes based on number of queues and TC's */
2486 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2487 max_txqs[i] = vsi->alloc_txq;
2489 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2492 dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2493 vsi->vsi_num, ice_stat_str(status));
2494 goto unroll_clear_rings;
2497 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2498 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2499 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2500 * The rule is added once for PF VSI in order to create appropriate
2501 * recipe, since VSI/VSI list is ignored with drop action...
2502 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2503 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2504 * settings in the HW.
2506 if (!ice_is_safe_mode(pf))
2507 if (vsi->type == ICE_VSI_PF) {
2508 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2510 ice_cfg_sw_lldp(vsi, true, true);
2514 ice_set_agg_vsi(vsi);
2518 ice_vsi_clear_rings(vsi);
2520 /* reclaim SW interrupts back to the common pool */
2521 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2522 pf->num_avail_sw_msix += vsi->num_q_vectors;
2523 unroll_alloc_q_vector:
2524 ice_vsi_free_q_vectors(vsi);
2526 ice_vsi_delete(vsi);
2528 ice_vsi_put_qs(vsi);
2530 if (vsi_type == ICE_VSI_VF)
2531 ice_enable_lag(pf->lag);
2538 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2539 * @vsi: the VSI being cleaned up
2541 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2543 struct ice_pf *pf = vsi->back;
2544 struct ice_hw *hw = &pf->hw;
2549 for (i = 0; i < vsi->num_q_vectors; i++) {
2550 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2552 ice_write_intrl(q_vector, 0);
2553 for (q = 0; q < q_vector->num_ring_tx; q++) {
2554 ice_write_itr(&q_vector->tx, 0);
2555 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2556 if (ice_is_xdp_ena_vsi(vsi)) {
2557 u32 xdp_txq = txq + vsi->num_xdp_txq;
2559 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2564 for (q = 0; q < q_vector->num_ring_rx; q++) {
2565 ice_write_itr(&q_vector->rx, 0);
2566 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2575 * ice_vsi_free_irq - Free the IRQ association with the OS
2576 * @vsi: the VSI being configured
2578 void ice_vsi_free_irq(struct ice_vsi *vsi)
2580 struct ice_pf *pf = vsi->back;
2581 int base = vsi->base_vector;
2584 if (!vsi->q_vectors || !vsi->irqs_ready)
2587 ice_vsi_release_msix(vsi);
2588 if (vsi->type == ICE_VSI_VF)
2591 vsi->irqs_ready = false;
2592 ice_for_each_q_vector(vsi, i) {
2593 u16 vector = i + base;
2596 irq_num = pf->msix_entries[vector].vector;
2598 /* free only the irqs that were actually requested */
2599 if (!vsi->q_vectors[i] ||
2600 !(vsi->q_vectors[i]->num_ring_tx ||
2601 vsi->q_vectors[i]->num_ring_rx))
2604 /* clear the affinity notifier in the IRQ descriptor */
2605 irq_set_affinity_notifier(irq_num, NULL);
2607 /* clear the affinity_mask in the IRQ descriptor */
2608 irq_set_affinity_hint(irq_num, NULL);
2609 synchronize_irq(irq_num);
2610 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2615 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2616 * @vsi: the VSI having resources freed
2618 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2625 ice_for_each_txq(vsi, i)
2626 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2627 ice_free_tx_ring(vsi->tx_rings[i]);
2631 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2632 * @vsi: the VSI having resources freed
2634 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2641 ice_for_each_rxq(vsi, i)
2642 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2643 ice_free_rx_ring(vsi->rx_rings[i]);
2647 * ice_vsi_close - Shut down a VSI
2648 * @vsi: the VSI being shut down
2650 void ice_vsi_close(struct ice_vsi *vsi)
2652 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2655 ice_vsi_free_irq(vsi);
2656 ice_vsi_free_tx_rings(vsi);
2657 ice_vsi_free_rx_rings(vsi);
2661 * ice_ena_vsi - resume a VSI
2662 * @vsi: the VSI being resume
2663 * @locked: is the rtnl_lock already held
2665 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2669 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2672 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2674 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2675 if (netif_running(vsi->netdev)) {
2679 err = ice_open_internal(vsi->netdev);
2684 } else if (vsi->type == ICE_VSI_CTRL) {
2685 err = ice_vsi_open_ctrl(vsi);
2692 * ice_dis_vsi - pause a VSI
2693 * @vsi: the VSI being paused
2694 * @locked: is the rtnl_lock already held
2696 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2698 if (test_bit(ICE_VSI_DOWN, vsi->state))
2701 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2703 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2704 if (netif_running(vsi->netdev)) {
2715 } else if (vsi->type == ICE_VSI_CTRL) {
2721 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2722 * @vsi: the VSI being un-configured
2724 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2726 int base = vsi->base_vector;
2727 struct ice_pf *pf = vsi->back;
2728 struct ice_hw *hw = &pf->hw;
2732 /* disable interrupt causation from each queue */
2733 if (vsi->tx_rings) {
2734 ice_for_each_txq(vsi, i) {
2735 if (vsi->tx_rings[i]) {
2738 reg = vsi->tx_rings[i]->reg_idx;
2739 val = rd32(hw, QINT_TQCTL(reg));
2740 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2741 wr32(hw, QINT_TQCTL(reg), val);
2746 if (vsi->rx_rings) {
2747 ice_for_each_rxq(vsi, i) {
2748 if (vsi->rx_rings[i]) {
2751 reg = vsi->rx_rings[i]->reg_idx;
2752 val = rd32(hw, QINT_RQCTL(reg));
2753 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2754 wr32(hw, QINT_RQCTL(reg), val);
2759 /* disable each interrupt */
2760 ice_for_each_q_vector(vsi, i) {
2761 if (!vsi->q_vectors[i])
2763 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2768 /* don't call synchronize_irq() for VF's from the host */
2769 if (vsi->type == ICE_VSI_VF)
2772 ice_for_each_q_vector(vsi, i)
2773 synchronize_irq(pf->msix_entries[i + base].vector);
2777 * ice_napi_del - Remove NAPI handler for the VSI
2778 * @vsi: VSI for which NAPI handler is to be removed
2780 void ice_napi_del(struct ice_vsi *vsi)
2787 ice_for_each_q_vector(vsi, v_idx)
2788 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2792 * ice_vsi_release - Delete a VSI and free its resources
2793 * @vsi: the VSI being removed
2795 * Returns 0 on success or < 0 on error
2797 int ice_vsi_release(struct ice_vsi *vsi)
2805 /* do not unregister while driver is in the reset recovery pending
2806 * state. Since reset/rebuild happens through PF service task workqueue,
2807 * it's not a good idea to unregister netdev that is associated to the
2808 * PF that is running the work queue items currently. This is done to
2809 * avoid check_flush_dependency() warning on this wq
2811 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2812 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2813 unregister_netdev(vsi->netdev);
2814 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2817 ice_devlink_destroy_port(vsi);
2819 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2822 /* Disable VSI and free resources */
2823 if (vsi->type != ICE_VSI_LB)
2824 ice_vsi_dis_irq(vsi);
2827 /* SR-IOV determines needed MSIX resources all at once instead of per
2828 * VSI since when VFs are spawned we know how many VFs there are and how
2829 * many interrupts each VF needs. SR-IOV MSIX resources are also
2830 * cleared in the same manner.
2832 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2836 ice_for_each_vf(pf, i) {
2838 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2841 if (i == pf->num_alloc_vfs) {
2842 /* No other VFs left that have control VSI, reclaim SW
2843 * interrupts back to the common pool
2845 ice_free_res(pf->irq_tracker, vsi->base_vector,
2846 ICE_RES_VF_CTRL_VEC_ID);
2847 pf->num_avail_sw_msix += vsi->num_q_vectors;
2849 } else if (vsi->type != ICE_VSI_VF) {
2850 /* reclaim SW interrupts back to the common pool */
2851 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2852 pf->num_avail_sw_msix += vsi->num_q_vectors;
2855 if (!ice_is_safe_mode(pf)) {
2856 if (vsi->type == ICE_VSI_PF) {
2857 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2859 ice_cfg_sw_lldp(vsi, true, false);
2860 /* The Rx rule will only exist to remove if the LLDP FW
2861 * engine is currently stopped
2863 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2864 ice_cfg_sw_lldp(vsi, false, false);
2868 ice_fltr_remove_all(vsi);
2869 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2870 ice_vsi_delete(vsi);
2871 ice_vsi_free_q_vectors(vsi);
2874 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2875 unregister_netdev(vsi->netdev);
2876 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2878 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2879 free_netdev(vsi->netdev);
2881 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2885 if (vsi->type == ICE_VSI_VF &&
2886 vsi->agg_node && vsi->agg_node->valid)
2887 vsi->agg_node->num_vsis--;
2888 ice_vsi_clear_rings(vsi);
2890 ice_vsi_put_qs(vsi);
2892 /* retain SW VSI data structure since it is needed to unregister and
2893 * free VSI netdev when PF is not in reset recovery pending state,\
2894 * for ex: during rmmod.
2896 if (!ice_is_reset_in_progress(pf->state))
2903 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2904 * @vsi: VSI connected with q_vectors
2905 * @coalesce: array of struct with stored coalesce
2907 * Returns array size.
2910 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2911 struct ice_coalesce_stored *coalesce)
2915 ice_for_each_q_vector(vsi, i) {
2916 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2918 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2919 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2920 coalesce[i].intrl = q_vector->intrl;
2922 if (i < vsi->num_txq)
2923 coalesce[i].tx_valid = true;
2924 if (i < vsi->num_rxq)
2925 coalesce[i].rx_valid = true;
2928 return vsi->num_q_vectors;
2932 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2933 * @vsi: VSI connected with q_vectors
2934 * @coalesce: pointer to array of struct with stored coalesce
2935 * @size: size of coalesce array
2937 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2938 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2942 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2943 struct ice_coalesce_stored *coalesce, int size)
2945 struct ice_ring_container *rc;
2948 if ((size && !coalesce) || !vsi)
2951 /* There are a couple of cases that have to be handled here:
2952 * 1. The case where the number of queue vectors stays the same, but
2953 * the number of Tx or Rx rings changes (the first for loop)
2954 * 2. The case where the number of queue vectors increased (the
2957 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2958 /* There are 2 cases to handle here and they are the same for
2960 * if the entry was valid previously (coalesce[i].[tr]x_valid
2961 * and the loop variable is less than the number of rings
2962 * allocated, then write the previous values
2964 * if the entry was not valid previously, but the number of
2965 * rings is less than are allocated (this means the number of
2966 * rings increased from previously), then write out the
2967 * values in the first element
2969 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
2970 * as there is no harm because the dynamic algorithm
2971 * will just overwrite.
2973 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2974 rc = &vsi->q_vectors[i]->rx;
2975 rc->itr_setting = coalesce[i].itr_rx;
2976 ice_write_itr(rc, rc->itr_setting);
2977 } else if (i < vsi->alloc_rxq) {
2978 rc = &vsi->q_vectors[i]->rx;
2979 rc->itr_setting = coalesce[0].itr_rx;
2980 ice_write_itr(rc, rc->itr_setting);
2983 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2984 rc = &vsi->q_vectors[i]->tx;
2985 rc->itr_setting = coalesce[i].itr_tx;
2986 ice_write_itr(rc, rc->itr_setting);
2987 } else if (i < vsi->alloc_txq) {
2988 rc = &vsi->q_vectors[i]->tx;
2989 rc->itr_setting = coalesce[0].itr_tx;
2990 ice_write_itr(rc, rc->itr_setting);
2993 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2994 ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
2997 /* the number of queue vectors increased so write whatever is in
3000 for (; i < vsi->num_q_vectors; i++) {
3002 rc = &vsi->q_vectors[i]->tx;
3003 rc->itr_setting = coalesce[0].itr_tx;
3004 ice_write_itr(rc, rc->itr_setting);
3007 rc = &vsi->q_vectors[i]->rx;
3008 rc->itr_setting = coalesce[0].itr_rx;
3009 ice_write_itr(rc, rc->itr_setting);
3011 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3012 ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3017 * ice_vsi_rebuild - Rebuild VSI after reset
3018 * @vsi: VSI to be rebuild
3019 * @init_vsi: is this an initialization or a reconfigure of the VSI
3021 * Returns 0 on success and negative value on failure
3023 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3025 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3026 struct ice_coalesce_stored *coalesce;
3027 int prev_num_q_vectors = 0;
3028 struct ice_vf *vf = NULL;
3029 enum ice_vsi_type vtype;
3030 enum ice_status status;
3039 if (vtype == ICE_VSI_VF)
3040 vf = &pf->vf[vsi->vf_id];
3042 coalesce = kcalloc(vsi->num_q_vectors,
3043 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3047 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3049 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3050 ice_vsi_free_q_vectors(vsi);
3052 /* SR-IOV determines needed MSIX resources all at once instead of per
3053 * VSI since when VFs are spawned we know how many VFs there are and how
3054 * many interrupts each VF needs. SR-IOV MSIX resources are also
3055 * cleared in the same manner.
3057 if (vtype != ICE_VSI_VF) {
3058 /* reclaim SW interrupts back to the common pool */
3059 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3060 pf->num_avail_sw_msix += vsi->num_q_vectors;
3061 vsi->base_vector = 0;
3064 if (ice_is_xdp_ena_vsi(vsi))
3065 /* return value check can be skipped here, it always returns
3066 * 0 if reset is in progress
3068 ice_destroy_xdp_rings(vsi);
3069 ice_vsi_put_qs(vsi);
3070 ice_vsi_clear_rings(vsi);
3071 ice_vsi_free_arrays(vsi);
3072 if (vtype == ICE_VSI_VF)
3073 ice_vsi_set_num_qs(vsi, vf->vf_id);
3075 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3077 ret = ice_vsi_alloc_arrays(vsi);
3081 ice_vsi_get_qs(vsi);
3083 ice_alloc_fd_res(vsi);
3084 ice_vsi_set_tc_cfg(vsi);
3086 /* Initialize VSI struct elements and create VSI in FW */
3087 ret = ice_vsi_init(vsi, init_vsi);
3094 ret = ice_vsi_alloc_q_vectors(vsi);
3098 ret = ice_vsi_setup_vector_base(vsi);
3102 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3106 ret = ice_vsi_alloc_rings(vsi);
3110 ice_vsi_map_rings_to_vectors(vsi);
3111 if (ice_is_xdp_ena_vsi(vsi)) {
3112 vsi->num_xdp_txq = vsi->alloc_rxq;
3113 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3117 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3118 if (vtype != ICE_VSI_CTRL)
3119 /* Do not exit if configuring RSS had an issue, at
3120 * least receive traffic on first queue. Hence no
3121 * need to capture return value
3123 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3124 ice_vsi_cfg_rss_lut_key(vsi);
3127 ret = ice_vsi_alloc_q_vectors(vsi);
3131 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3135 ret = ice_vsi_alloc_rings(vsi);
3144 /* configure VSI nodes based on number of queues and TC's */
3145 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3146 max_txqs[i] = vsi->alloc_txq;
3148 if (ice_is_xdp_ena_vsi(vsi))
3149 max_txqs[i] += vsi->num_xdp_txq;
3152 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3155 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3156 vsi->vsi_num, ice_stat_str(status));
3161 return ice_schedule_reset(pf, ICE_RESET_PFR);
3164 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3170 ice_vsi_free_q_vectors(vsi);
3173 vsi->current_netdev_flags = 0;
3174 unregister_netdev(vsi->netdev);
3175 free_netdev(vsi->netdev);
3180 set_bit(ICE_RESET_FAILED, pf->state);
3186 * ice_is_reset_in_progress - check for a reset in progress
3187 * @state: PF state field
3189 bool ice_is_reset_in_progress(unsigned long *state)
3191 return test_bit(ICE_RESET_OICR_RECV, state) ||
3192 test_bit(ICE_PFR_REQ, state) ||
3193 test_bit(ICE_CORER_REQ, state) ||
3194 test_bit(ICE_GLOBR_REQ, state);
3199 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3200 * @vsi: VSI being configured
3201 * @ctx: the context buffer returned from AQ VSI update command
3203 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3205 vsi->info.mapping_flags = ctx->info.mapping_flags;
3206 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3207 sizeof(vsi->info.q_mapping));
3208 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3209 sizeof(vsi->info.tc_mapping));
3213 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3214 * @vsi: VSI to be configured
3215 * @ena_tc: TC bitmap
3217 * VSI queues expected to be quiesced before calling this function
3219 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3221 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3222 struct ice_pf *pf = vsi->back;
3223 struct ice_vsi_ctx *ctx;
3224 enum ice_status status;
3229 dev = ice_pf_to_dev(pf);
3231 ice_for_each_traffic_class(i) {
3232 /* build bitmap of enabled TCs */
3233 if (ena_tc & BIT(i))
3235 /* populate max_txqs per TC */
3236 max_txqs[i] = vsi->alloc_txq;
3239 vsi->tc_cfg.ena_tc = ena_tc;
3240 vsi->tc_cfg.numtc = num_tc;
3242 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3247 ctx->info = vsi->info;
3249 ice_vsi_setup_q_map(vsi, ctx);
3251 /* must to indicate which section of VSI context are being modified */
3252 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3253 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3255 dev_info(dev, "Failed VSI Update\n");
3260 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3264 dev_err(dev, "VSI %d failed TC config, error %s\n",
3265 vsi->vsi_num, ice_stat_str(status));
3269 ice_vsi_update_q_map(vsi, ctx);
3270 vsi->info.valid_sections = 0;
3272 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3277 #endif /* CONFIG_DCB */
3280 * ice_update_ring_stats - Update ring statistics
3281 * @ring: ring to update
3282 * @pkts: number of processed packets
3283 * @bytes: number of processed bytes
3285 * This function assumes that caller has acquired a u64_stats_sync lock.
3287 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3289 ring->stats.bytes += bytes;
3290 ring->stats.pkts += pkts;
3294 * ice_update_tx_ring_stats - Update Tx ring specific counters
3295 * @tx_ring: ring to update
3296 * @pkts: number of processed packets
3297 * @bytes: number of processed bytes
3299 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3301 u64_stats_update_begin(&tx_ring->syncp);
3302 ice_update_ring_stats(tx_ring, pkts, bytes);
3303 u64_stats_update_end(&tx_ring->syncp);
3307 * ice_update_rx_ring_stats - Update Rx ring specific counters
3308 * @rx_ring: ring to update
3309 * @pkts: number of processed packets
3310 * @bytes: number of processed bytes
3312 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3314 u64_stats_update_begin(&rx_ring->syncp);
3315 ice_update_ring_stats(rx_ring, pkts, bytes);
3316 u64_stats_update_end(&rx_ring->syncp);
3320 * ice_status_to_errno - convert from enum ice_status to Linux errno
3321 * @err: ice_status value to convert
3323 int ice_status_to_errno(enum ice_status err)
3328 case ICE_ERR_DOES_NOT_EXIST:
3330 case ICE_ERR_OUT_OF_RANGE:
3334 case ICE_ERR_NO_MEMORY:
3336 case ICE_ERR_MAX_LIMIT:
3344 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3345 * @sw: switch to check if its default forwarding VSI is free
3347 * Return true if the default forwarding VSI is already being used, else returns
3348 * false signalling that it's available to use.
3350 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3352 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3356 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3357 * @sw: switch for the default forwarding VSI to compare against
3358 * @vsi: VSI to compare against default forwarding VSI
3360 * If this VSI passed in is the default forwarding VSI then return true, else
3363 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3365 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3369 * ice_set_dflt_vsi - set the default forwarding VSI
3370 * @sw: switch used to assign the default forwarding VSI
3371 * @vsi: VSI getting set as the default forwarding VSI on the switch
3373 * If the VSI passed in is already the default VSI and it's enabled just return
3376 * If there is already a default VSI on the switch and it's enabled then return
3377 * -EEXIST since there can only be one default VSI per switch.
3379 * Otherwise try to set the VSI passed in as the switch's default VSI and
3380 * return the result.
3382 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3384 enum ice_status status;
3390 dev = ice_pf_to_dev(vsi->back);
3392 /* the VSI passed in is already the default VSI */
3393 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3394 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3399 /* another VSI is already the default VSI for this switch */
3400 if (ice_is_dflt_vsi_in_use(sw)) {
3401 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3402 sw->dflt_vsi->vsi_num);
3406 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3408 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3409 vsi->vsi_num, ice_stat_str(status));
3414 sw->dflt_vsi_ena = true;
3420 * ice_clear_dflt_vsi - clear the default forwarding VSI
3421 * @sw: switch used to clear the default VSI
3423 * If the switch has no default VSI or it's not enabled then return error.
3425 * Otherwise try to clear the default VSI and return the result.
3427 int ice_clear_dflt_vsi(struct ice_sw *sw)
3429 struct ice_vsi *dflt_vsi;
3430 enum ice_status status;
3436 dev = ice_pf_to_dev(sw->pf);
3438 dflt_vsi = sw->dflt_vsi;
3440 /* there is no default VSI configured */
3441 if (!ice_is_dflt_vsi_in_use(sw))
3444 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3447 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3448 dflt_vsi->vsi_num, ice_stat_str(status));
3452 sw->dflt_vsi = NULL;
3453 sw->dflt_vsi_ena = false;
3459 * ice_set_link - turn on/off physical link
3460 * @vsi: VSI to modify physical link on
3461 * @ena: turn on/off physical link
3463 int ice_set_link(struct ice_vsi *vsi, bool ena)
3465 struct device *dev = ice_pf_to_dev(vsi->back);
3466 struct ice_port_info *pi = vsi->port_info;
3467 struct ice_hw *hw = pi->hw;
3468 enum ice_status status;
3470 if (vsi->type != ICE_VSI_PF)
3473 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3475 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3476 * this is not a fatal error, so print a warning message and return
3477 * a success code. Return an error if FW returns an error code other
3478 * than ICE_AQ_RC_EMODE
3480 if (status == ICE_ERR_AQ_ERROR) {
3481 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3482 dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3483 (ena ? "ON" : "OFF"), ice_stat_str(status),
3484 ice_aq_str(hw->adminq.sq_last_status));
3485 } else if (status) {
3486 dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3487 (ena ? "ON" : "OFF"), ice_stat_str(status),
3488 ice_aq_str(hw->adminq.sq_last_status));