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
1720 * @count: number of Tx ring array elements
1722 * Return 0 on success and a negative value on error
1723 * Configure the Tx VSI for operation.
1726 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, u16 count)
1728 struct ice_aqc_add_tx_qgrp *qg_buf;
1732 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1736 qg_buf->num_txqs = 1;
1738 for (q_idx = 0; q_idx < count; q_idx++) {
1739 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1750 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1751 * @vsi: the VSI being configured
1753 * Return 0 on success and a negative value on error
1754 * Configure the Tx VSI for operation.
1756 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1758 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1762 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1763 * @vsi: the VSI being configured
1765 * Return 0 on success and a negative value on error
1766 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1768 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1773 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1777 for (i = 0; i < vsi->num_xdp_txq; i++)
1778 vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1784 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1785 * @intrl: interrupt rate limit in usecs
1786 * @gran: interrupt rate limit granularity in usecs
1788 * This function converts a decimal interrupt rate limit in usecs to the format
1789 * expected by firmware.
1791 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1793 u32 val = intrl / gran;
1796 return val | GLINT_RATE_INTRL_ENA_M;
1801 * ice_write_intrl - write throttle rate limit to interrupt specific register
1802 * @q_vector: pointer to interrupt specific structure
1803 * @intrl: throttle rate limit in microseconds to write
1805 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1807 struct ice_hw *hw = &q_vector->vsi->back->hw;
1809 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1810 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1814 * __ice_write_itr - write throttle rate to register
1815 * @q_vector: pointer to interrupt data structure
1816 * @rc: pointer to ring container
1817 * @itr: throttle rate in microseconds to write
1819 static void __ice_write_itr(struct ice_q_vector *q_vector,
1820 struct ice_ring_container *rc, u16 itr)
1822 struct ice_hw *hw = &q_vector->vsi->back->hw;
1824 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1825 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1829 * ice_write_itr - write throttle rate to queue specific register
1830 * @rc: pointer to ring container
1831 * @itr: throttle rate in microseconds to write
1833 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1835 struct ice_q_vector *q_vector;
1840 q_vector = rc->ring->q_vector;
1842 __ice_write_itr(q_vector, rc, itr);
1846 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1847 * @vsi: the VSI being configured
1849 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1852 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1854 struct ice_pf *pf = vsi->back;
1855 struct ice_hw *hw = &pf->hw;
1856 u16 txq = 0, rxq = 0;
1859 for (i = 0; i < vsi->num_q_vectors; i++) {
1860 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1861 u16 reg_idx = q_vector->reg_idx;
1863 ice_cfg_itr(hw, q_vector);
1865 /* Both Transmit Queue Interrupt Cause Control register
1866 * and Receive Queue Interrupt Cause control register
1867 * expects MSIX_INDX field to be the vector index
1868 * within the function space and not the absolute
1869 * vector index across PF or across device.
1870 * For SR-IOV VF VSIs queue vector index always starts
1871 * with 1 since first vector index(0) is used for OICR
1872 * in VF space. Since VMDq and other PF VSIs are within
1873 * the PF function space, use the vector index that is
1874 * tracked for this PF.
1876 for (q = 0; q < q_vector->num_ring_tx; q++) {
1877 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1878 q_vector->tx.itr_idx);
1882 for (q = 0; q < q_vector->num_ring_rx; q++) {
1883 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1884 q_vector->rx.itr_idx);
1891 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1892 * @vsi: the VSI being changed
1894 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1896 struct ice_hw *hw = &vsi->back->hw;
1897 struct ice_vsi_ctx *ctxt;
1898 enum ice_status status;
1901 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1905 /* Here we are configuring the VSI to let the driver add VLAN tags by
1906 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1907 * insertion happens in the Tx hot path, in ice_tx_map.
1909 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1911 /* Preserve existing VLAN strip setting */
1912 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1913 ICE_AQ_VSI_VLAN_EMOD_M);
1915 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1917 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1919 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1920 ice_stat_str(status),
1921 ice_aq_str(hw->adminq.sq_last_status));
1926 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1933 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1934 * @vsi: the VSI being changed
1935 * @ena: boolean value indicating if this is a enable or disable request
1937 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1939 struct ice_hw *hw = &vsi->back->hw;
1940 struct ice_vsi_ctx *ctxt;
1941 enum ice_status status;
1944 /* do not allow modifying VLAN stripping when a port VLAN is configured
1950 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1954 /* Here we are configuring what the VSI should do with the VLAN tag in
1955 * the Rx packet. We can either leave the tag in the packet or put it in
1956 * the Rx descriptor.
1959 /* Strip VLAN tag from Rx packet and put it in the desc */
1960 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1962 /* Disable stripping. Leave tag in packet */
1963 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1965 /* Allow all packets untagged/tagged */
1966 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1968 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1970 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1972 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1973 ena, ice_stat_str(status),
1974 ice_aq_str(hw->adminq.sq_last_status));
1979 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1986 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1987 * @vsi: the VSI whose rings are to be enabled
1989 * Returns 0 on success and a negative value on error
1991 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1993 return ice_vsi_ctrl_all_rx_rings(vsi, true);
1997 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1998 * @vsi: the VSI whose rings are to be disabled
2000 * Returns 0 on success and a negative value on error
2002 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2004 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2008 * ice_vsi_stop_tx_rings - Disable Tx rings
2009 * @vsi: the VSI being configured
2010 * @rst_src: reset source
2011 * @rel_vmvf_num: Relative ID of VF/VM
2012 * @rings: Tx ring array to be stopped
2013 * @count: number of Tx ring array elements
2016 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2017 u16 rel_vmvf_num, struct ice_ring **rings, u16 count)
2021 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2024 for (q_idx = 0; q_idx < count; q_idx++) {
2025 struct ice_txq_meta txq_meta = { };
2028 if (!rings || !rings[q_idx])
2031 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2032 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2033 rings[q_idx], &txq_meta);
2043 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2044 * @vsi: the VSI being configured
2045 * @rst_src: reset source
2046 * @rel_vmvf_num: Relative ID of VF/VM
2049 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2052 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2056 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2057 * @vsi: the VSI being configured
2059 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2061 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2065 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2066 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2068 * returns true if Rx VLAN pruning is enabled and false otherwise.
2070 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2075 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2079 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2080 * @vsi: VSI to enable or disable VLAN pruning on
2081 * @ena: set to true to enable VLAN pruning and false to disable it
2082 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2084 * returns 0 if VSI is updated, negative otherwise
2086 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2088 struct ice_vsi_ctx *ctxt;
2095 /* Don't enable VLAN pruning if the netdev is currently in promiscuous
2096 * mode. VLAN pruning will be enabled when the interface exits
2097 * promiscuous mode if any VLAN filters are active.
2099 if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2103 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2107 ctxt->info = vsi->info;
2110 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2112 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2115 ctxt->info.valid_sections =
2116 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2118 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2120 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2121 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2122 ice_stat_str(status),
2123 ice_aq_str(pf->hw.adminq.sq_last_status));
2127 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2137 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2139 struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2141 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2142 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2146 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2147 * @vsi: VSI to set the q_vectors register index on
2150 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2154 if (!vsi || !vsi->q_vectors)
2157 ice_for_each_q_vector(vsi, i) {
2158 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2161 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2166 if (vsi->type == ICE_VSI_VF) {
2167 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2169 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2172 q_vector->v_idx + vsi->base_vector;
2179 ice_for_each_q_vector(vsi, i) {
2180 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2183 q_vector->reg_idx = 0;
2190 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2191 * @vsi: the VSI being configured
2192 * @tx: bool to determine Tx or Rx rule
2193 * @create: bool to determine create or remove Rule
2195 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2197 enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2198 enum ice_sw_fwd_act_type act);
2199 struct ice_pf *pf = vsi->back;
2200 enum ice_status status;
2203 dev = ice_pf_to_dev(pf);
2204 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2207 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2210 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2211 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2214 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2220 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2221 create ? "adding" : "removing", tx ? "TX" : "RX",
2222 vsi->vsi_num, ice_stat_str(status));
2226 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2227 * @vsi: pointer to the VSI
2229 * This function will allocate new scheduler aggregator now if needed and will
2230 * move specified VSI into it.
2232 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2234 struct device *dev = ice_pf_to_dev(vsi->back);
2235 struct ice_agg_node *agg_node_iter = NULL;
2236 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2237 struct ice_agg_node *agg_node = NULL;
2238 int node_offset, max_agg_nodes = 0;
2239 struct ice_port_info *port_info;
2240 struct ice_pf *pf = vsi->back;
2241 u32 agg_node_id_start = 0;
2242 enum ice_status status;
2244 /* create (as needed) scheduler aggregator node and move VSI into
2245 * corresponding aggregator node
2246 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2247 * - VF aggregator nodes will contain VF VSI
2249 port_info = pf->hw.port_info;
2253 switch (vsi->type) {
2257 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2258 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2259 agg_node_iter = &pf->pf_agg_node[0];
2262 /* user can create 'n' VFs on a given PF, but since max children
2263 * per aggregator node can be only 64. Following code handles
2264 * aggregator(s) for VF VSIs, either selects a agg_node which
2265 * was already created provided num_vsis < 64, otherwise
2266 * select next available node, which will be created
2268 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2269 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2270 agg_node_iter = &pf->vf_agg_node[0];
2273 /* other VSI type, handle later if needed */
2274 dev_dbg(dev, "unexpected VSI type %s\n",
2275 ice_vsi_type_str(vsi->type));
2279 /* find the appropriate aggregator node */
2280 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2281 /* see if we can find space in previously created
2282 * node if num_vsis < 64, otherwise skip
2284 if (agg_node_iter->num_vsis &&
2285 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2290 if (agg_node_iter->valid &&
2291 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2292 agg_id = agg_node_iter->agg_id;
2293 agg_node = agg_node_iter;
2297 /* find unclaimed agg_id */
2298 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2299 agg_id = node_offset + agg_node_id_start;
2300 agg_node = agg_node_iter;
2303 /* move to next agg_node */
2310 /* if selected aggregator node was not created, create it */
2311 if (!agg_node->valid) {
2312 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2313 (u8)vsi->tc_cfg.ena_tc);
2315 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2319 /* aggregator node is created, store the neeeded info */
2320 agg_node->valid = true;
2321 agg_node->agg_id = agg_id;
2324 /* move VSI to corresponding aggregator node */
2325 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2326 (u8)vsi->tc_cfg.ena_tc);
2328 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2333 /* keep active children count for aggregator node */
2334 agg_node->num_vsis++;
2336 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2337 * to aggregator node
2339 vsi->agg_node = agg_node;
2340 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2341 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2342 vsi->agg_node->num_vsis);
2346 * ice_vsi_setup - Set up a VSI by a given type
2347 * @pf: board private structure
2348 * @pi: pointer to the port_info instance
2349 * @vsi_type: VSI type
2350 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2351 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2352 * fill-in ICE_INVAL_VFID as input.
2354 * This allocates the sw VSI structure and its queue resources.
2356 * Returns pointer to the successfully allocated and configured VSI sw struct on
2357 * success, NULL on failure.
2360 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2361 enum ice_vsi_type vsi_type, u16 vf_id)
2363 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2364 struct device *dev = ice_pf_to_dev(pf);
2365 enum ice_status status;
2366 struct ice_vsi *vsi;
2369 if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2370 vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2372 vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2375 dev_err(dev, "could not allocate VSI\n");
2379 vsi->port_info = pi;
2380 vsi->vsw = pf->first_sw;
2381 if (vsi->type == ICE_VSI_PF)
2382 vsi->ethtype = ETH_P_PAUSE;
2384 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2387 ice_alloc_fd_res(vsi);
2389 if (ice_vsi_get_qs(vsi)) {
2390 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2392 goto unroll_vsi_alloc;
2395 /* set RSS capabilities */
2396 ice_vsi_set_rss_params(vsi);
2398 /* set TC configuration */
2399 ice_vsi_set_tc_cfg(vsi);
2401 /* create the VSI */
2402 ret = ice_vsi_init(vsi, true);
2406 switch (vsi->type) {
2409 ret = ice_vsi_alloc_q_vectors(vsi);
2411 goto unroll_vsi_init;
2413 ret = ice_vsi_setup_vector_base(vsi);
2415 goto unroll_alloc_q_vector;
2417 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2419 goto unroll_vector_base;
2421 ret = ice_vsi_alloc_rings(vsi);
2423 goto unroll_vector_base;
2425 /* Always add VLAN ID 0 switch rule by default. This is needed
2426 * in order to allow all untagged and 0 tagged priority traffic
2427 * if Rx VLAN pruning is enabled. Also there are cases where we
2428 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2429 * so this handles those cases (i.e. adding the PF to a bridge
2430 * without the 8021q module loaded).
2432 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2434 goto unroll_clear_rings;
2436 ice_vsi_map_rings_to_vectors(vsi);
2438 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2439 if (vsi->type != ICE_VSI_CTRL)
2440 /* Do not exit if configuring RSS had an issue, at
2441 * least receive traffic on first queue. Hence no
2442 * need to capture return value
2444 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2445 ice_vsi_cfg_rss_lut_key(vsi);
2446 ice_vsi_set_rss_flow_fld(vsi);
2451 /* VF driver will take care of creating netdev for this type and
2452 * map queues to vectors through Virtchnl, PF driver only
2453 * creates a VSI and corresponding structures for bookkeeping
2456 ret = ice_vsi_alloc_q_vectors(vsi);
2458 goto unroll_vsi_init;
2460 ret = ice_vsi_alloc_rings(vsi);
2462 goto unroll_alloc_q_vector;
2464 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2466 goto unroll_vector_base;
2468 /* Do not exit if configuring RSS had an issue, at least
2469 * receive traffic on first queue. Hence no need to capture
2472 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2473 ice_vsi_cfg_rss_lut_key(vsi);
2474 ice_vsi_set_vf_rss_flow_fld(vsi);
2478 ret = ice_vsi_alloc_rings(vsi);
2480 goto unroll_vsi_init;
2483 /* clean up the resources and exit */
2484 goto unroll_vsi_init;
2487 /* configure VSI nodes based on number of queues and TC's */
2488 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2489 max_txqs[i] = vsi->alloc_txq;
2491 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2494 dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2495 vsi->vsi_num, ice_stat_str(status));
2496 goto unroll_clear_rings;
2499 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2500 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2501 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2502 * The rule is added once for PF VSI in order to create appropriate
2503 * recipe, since VSI/VSI list is ignored with drop action...
2504 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2505 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2506 * settings in the HW.
2508 if (!ice_is_safe_mode(pf))
2509 if (vsi->type == ICE_VSI_PF) {
2510 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2512 ice_cfg_sw_lldp(vsi, true, true);
2516 ice_set_agg_vsi(vsi);
2520 ice_vsi_clear_rings(vsi);
2522 /* reclaim SW interrupts back to the common pool */
2523 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2524 pf->num_avail_sw_msix += vsi->num_q_vectors;
2525 unroll_alloc_q_vector:
2526 ice_vsi_free_q_vectors(vsi);
2528 ice_vsi_delete(vsi);
2530 ice_vsi_put_qs(vsi);
2532 if (vsi_type == ICE_VSI_VF)
2533 ice_enable_lag(pf->lag);
2540 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2541 * @vsi: the VSI being cleaned up
2543 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2545 struct ice_pf *pf = vsi->back;
2546 struct ice_hw *hw = &pf->hw;
2551 for (i = 0; i < vsi->num_q_vectors; i++) {
2552 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2554 ice_write_intrl(q_vector, 0);
2555 for (q = 0; q < q_vector->num_ring_tx; q++) {
2556 ice_write_itr(&q_vector->tx, 0);
2557 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2558 if (ice_is_xdp_ena_vsi(vsi)) {
2559 u32 xdp_txq = txq + vsi->num_xdp_txq;
2561 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2566 for (q = 0; q < q_vector->num_ring_rx; q++) {
2567 ice_write_itr(&q_vector->rx, 0);
2568 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2577 * ice_vsi_free_irq - Free the IRQ association with the OS
2578 * @vsi: the VSI being configured
2580 void ice_vsi_free_irq(struct ice_vsi *vsi)
2582 struct ice_pf *pf = vsi->back;
2583 int base = vsi->base_vector;
2586 if (!vsi->q_vectors || !vsi->irqs_ready)
2589 ice_vsi_release_msix(vsi);
2590 if (vsi->type == ICE_VSI_VF)
2593 vsi->irqs_ready = false;
2594 ice_for_each_q_vector(vsi, i) {
2595 u16 vector = i + base;
2598 irq_num = pf->msix_entries[vector].vector;
2600 /* free only the irqs that were actually requested */
2601 if (!vsi->q_vectors[i] ||
2602 !(vsi->q_vectors[i]->num_ring_tx ||
2603 vsi->q_vectors[i]->num_ring_rx))
2606 /* clear the affinity notifier in the IRQ descriptor */
2607 irq_set_affinity_notifier(irq_num, NULL);
2609 /* clear the affinity_mask in the IRQ descriptor */
2610 irq_set_affinity_hint(irq_num, NULL);
2611 synchronize_irq(irq_num);
2612 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2617 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2618 * @vsi: the VSI having resources freed
2620 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2627 ice_for_each_txq(vsi, i)
2628 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2629 ice_free_tx_ring(vsi->tx_rings[i]);
2633 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2634 * @vsi: the VSI having resources freed
2636 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2643 ice_for_each_rxq(vsi, i)
2644 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2645 ice_free_rx_ring(vsi->rx_rings[i]);
2649 * ice_vsi_close - Shut down a VSI
2650 * @vsi: the VSI being shut down
2652 void ice_vsi_close(struct ice_vsi *vsi)
2654 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2657 ice_vsi_free_irq(vsi);
2658 ice_vsi_free_tx_rings(vsi);
2659 ice_vsi_free_rx_rings(vsi);
2663 * ice_ena_vsi - resume a VSI
2664 * @vsi: the VSI being resume
2665 * @locked: is the rtnl_lock already held
2667 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2671 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2674 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2676 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2677 if (netif_running(vsi->netdev)) {
2681 err = ice_open_internal(vsi->netdev);
2686 } else if (vsi->type == ICE_VSI_CTRL) {
2687 err = ice_vsi_open_ctrl(vsi);
2694 * ice_dis_vsi - pause a VSI
2695 * @vsi: the VSI being paused
2696 * @locked: is the rtnl_lock already held
2698 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2700 if (test_bit(ICE_VSI_DOWN, vsi->state))
2703 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2705 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2706 if (netif_running(vsi->netdev)) {
2717 } else if (vsi->type == ICE_VSI_CTRL) {
2723 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2724 * @vsi: the VSI being un-configured
2726 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2728 int base = vsi->base_vector;
2729 struct ice_pf *pf = vsi->back;
2730 struct ice_hw *hw = &pf->hw;
2734 /* disable interrupt causation from each queue */
2735 if (vsi->tx_rings) {
2736 ice_for_each_txq(vsi, i) {
2737 if (vsi->tx_rings[i]) {
2740 reg = vsi->tx_rings[i]->reg_idx;
2741 val = rd32(hw, QINT_TQCTL(reg));
2742 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2743 wr32(hw, QINT_TQCTL(reg), val);
2748 if (vsi->rx_rings) {
2749 ice_for_each_rxq(vsi, i) {
2750 if (vsi->rx_rings[i]) {
2753 reg = vsi->rx_rings[i]->reg_idx;
2754 val = rd32(hw, QINT_RQCTL(reg));
2755 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2756 wr32(hw, QINT_RQCTL(reg), val);
2761 /* disable each interrupt */
2762 ice_for_each_q_vector(vsi, i) {
2763 if (!vsi->q_vectors[i])
2765 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2770 /* don't call synchronize_irq() for VF's from the host */
2771 if (vsi->type == ICE_VSI_VF)
2774 ice_for_each_q_vector(vsi, i)
2775 synchronize_irq(pf->msix_entries[i + base].vector);
2779 * ice_napi_del - Remove NAPI handler for the VSI
2780 * @vsi: VSI for which NAPI handler is to be removed
2782 void ice_napi_del(struct ice_vsi *vsi)
2789 ice_for_each_q_vector(vsi, v_idx)
2790 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2794 * ice_vsi_release - Delete a VSI and free its resources
2795 * @vsi: the VSI being removed
2797 * Returns 0 on success or < 0 on error
2799 int ice_vsi_release(struct ice_vsi *vsi)
2807 /* do not unregister while driver is in the reset recovery pending
2808 * state. Since reset/rebuild happens through PF service task workqueue,
2809 * it's not a good idea to unregister netdev that is associated to the
2810 * PF that is running the work queue items currently. This is done to
2811 * avoid check_flush_dependency() warning on this wq
2813 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2814 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2815 unregister_netdev(vsi->netdev);
2816 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2819 ice_devlink_destroy_port(vsi);
2821 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2824 /* Disable VSI and free resources */
2825 if (vsi->type != ICE_VSI_LB)
2826 ice_vsi_dis_irq(vsi);
2829 /* SR-IOV determines needed MSIX resources all at once instead of per
2830 * VSI since when VFs are spawned we know how many VFs there are and how
2831 * many interrupts each VF needs. SR-IOV MSIX resources are also
2832 * cleared in the same manner.
2834 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2838 ice_for_each_vf(pf, i) {
2840 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2843 if (i == pf->num_alloc_vfs) {
2844 /* No other VFs left that have control VSI, reclaim SW
2845 * interrupts back to the common pool
2847 ice_free_res(pf->irq_tracker, vsi->base_vector,
2848 ICE_RES_VF_CTRL_VEC_ID);
2849 pf->num_avail_sw_msix += vsi->num_q_vectors;
2851 } else if (vsi->type != ICE_VSI_VF) {
2852 /* reclaim SW interrupts back to the common pool */
2853 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2854 pf->num_avail_sw_msix += vsi->num_q_vectors;
2857 if (!ice_is_safe_mode(pf)) {
2858 if (vsi->type == ICE_VSI_PF) {
2859 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2861 ice_cfg_sw_lldp(vsi, true, false);
2862 /* The Rx rule will only exist to remove if the LLDP FW
2863 * engine is currently stopped
2865 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2866 ice_cfg_sw_lldp(vsi, false, false);
2870 ice_fltr_remove_all(vsi);
2871 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2872 ice_vsi_delete(vsi);
2873 ice_vsi_free_q_vectors(vsi);
2876 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2877 unregister_netdev(vsi->netdev);
2878 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2880 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2881 free_netdev(vsi->netdev);
2883 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2887 if (vsi->type == ICE_VSI_VF &&
2888 vsi->agg_node && vsi->agg_node->valid)
2889 vsi->agg_node->num_vsis--;
2890 ice_vsi_clear_rings(vsi);
2892 ice_vsi_put_qs(vsi);
2894 /* retain SW VSI data structure since it is needed to unregister and
2895 * free VSI netdev when PF is not in reset recovery pending state,\
2896 * for ex: during rmmod.
2898 if (!ice_is_reset_in_progress(pf->state))
2905 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2906 * @vsi: VSI connected with q_vectors
2907 * @coalesce: array of struct with stored coalesce
2909 * Returns array size.
2912 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2913 struct ice_coalesce_stored *coalesce)
2917 ice_for_each_q_vector(vsi, i) {
2918 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2920 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2921 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2922 coalesce[i].intrl = q_vector->intrl;
2924 if (i < vsi->num_txq)
2925 coalesce[i].tx_valid = true;
2926 if (i < vsi->num_rxq)
2927 coalesce[i].rx_valid = true;
2930 return vsi->num_q_vectors;
2934 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2935 * @vsi: VSI connected with q_vectors
2936 * @coalesce: pointer to array of struct with stored coalesce
2937 * @size: size of coalesce array
2939 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2940 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2944 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2945 struct ice_coalesce_stored *coalesce, int size)
2947 struct ice_ring_container *rc;
2950 if ((size && !coalesce) || !vsi)
2953 /* There are a couple of cases that have to be handled here:
2954 * 1. The case where the number of queue vectors stays the same, but
2955 * the number of Tx or Rx rings changes (the first for loop)
2956 * 2. The case where the number of queue vectors increased (the
2959 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2960 /* There are 2 cases to handle here and they are the same for
2962 * if the entry was valid previously (coalesce[i].[tr]x_valid
2963 * and the loop variable is less than the number of rings
2964 * allocated, then write the previous values
2966 * if the entry was not valid previously, but the number of
2967 * rings is less than are allocated (this means the number of
2968 * rings increased from previously), then write out the
2969 * values in the first element
2971 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
2972 * as there is no harm because the dynamic algorithm
2973 * will just overwrite.
2975 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2976 rc = &vsi->q_vectors[i]->rx;
2977 rc->itr_setting = coalesce[i].itr_rx;
2978 ice_write_itr(rc, rc->itr_setting);
2979 } else if (i < vsi->alloc_rxq) {
2980 rc = &vsi->q_vectors[i]->rx;
2981 rc->itr_setting = coalesce[0].itr_rx;
2982 ice_write_itr(rc, rc->itr_setting);
2985 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2986 rc = &vsi->q_vectors[i]->tx;
2987 rc->itr_setting = coalesce[i].itr_tx;
2988 ice_write_itr(rc, rc->itr_setting);
2989 } else if (i < vsi->alloc_txq) {
2990 rc = &vsi->q_vectors[i]->tx;
2991 rc->itr_setting = coalesce[0].itr_tx;
2992 ice_write_itr(rc, rc->itr_setting);
2995 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2996 ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
2999 /* the number of queue vectors increased so write whatever is in
3002 for (; i < vsi->num_q_vectors; i++) {
3004 rc = &vsi->q_vectors[i]->tx;
3005 rc->itr_setting = coalesce[0].itr_tx;
3006 ice_write_itr(rc, rc->itr_setting);
3009 rc = &vsi->q_vectors[i]->rx;
3010 rc->itr_setting = coalesce[0].itr_rx;
3011 ice_write_itr(rc, rc->itr_setting);
3013 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3014 ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3019 * ice_vsi_rebuild - Rebuild VSI after reset
3020 * @vsi: VSI to be rebuild
3021 * @init_vsi: is this an initialization or a reconfigure of the VSI
3023 * Returns 0 on success and negative value on failure
3025 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3027 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3028 struct ice_coalesce_stored *coalesce;
3029 int prev_num_q_vectors = 0;
3030 struct ice_vf *vf = NULL;
3031 enum ice_vsi_type vtype;
3032 enum ice_status status;
3041 if (vtype == ICE_VSI_VF)
3042 vf = &pf->vf[vsi->vf_id];
3044 coalesce = kcalloc(vsi->num_q_vectors,
3045 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3049 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3051 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3052 ice_vsi_free_q_vectors(vsi);
3054 /* SR-IOV determines needed MSIX resources all at once instead of per
3055 * VSI since when VFs are spawned we know how many VFs there are and how
3056 * many interrupts each VF needs. SR-IOV MSIX resources are also
3057 * cleared in the same manner.
3059 if (vtype != ICE_VSI_VF) {
3060 /* reclaim SW interrupts back to the common pool */
3061 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3062 pf->num_avail_sw_msix += vsi->num_q_vectors;
3063 vsi->base_vector = 0;
3066 if (ice_is_xdp_ena_vsi(vsi))
3067 /* return value check can be skipped here, it always returns
3068 * 0 if reset is in progress
3070 ice_destroy_xdp_rings(vsi);
3071 ice_vsi_put_qs(vsi);
3072 ice_vsi_clear_rings(vsi);
3073 ice_vsi_free_arrays(vsi);
3074 if (vtype == ICE_VSI_VF)
3075 ice_vsi_set_num_qs(vsi, vf->vf_id);
3077 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3079 ret = ice_vsi_alloc_arrays(vsi);
3083 ice_vsi_get_qs(vsi);
3085 ice_alloc_fd_res(vsi);
3086 ice_vsi_set_tc_cfg(vsi);
3088 /* Initialize VSI struct elements and create VSI in FW */
3089 ret = ice_vsi_init(vsi, init_vsi);
3096 ret = ice_vsi_alloc_q_vectors(vsi);
3100 ret = ice_vsi_setup_vector_base(vsi);
3104 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3108 ret = ice_vsi_alloc_rings(vsi);
3112 ice_vsi_map_rings_to_vectors(vsi);
3113 if (ice_is_xdp_ena_vsi(vsi)) {
3114 vsi->num_xdp_txq = vsi->alloc_rxq;
3115 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3119 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3120 if (vtype != ICE_VSI_CTRL)
3121 /* Do not exit if configuring RSS had an issue, at
3122 * least receive traffic on first queue. Hence no
3123 * need to capture return value
3125 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3126 ice_vsi_cfg_rss_lut_key(vsi);
3129 ret = ice_vsi_alloc_q_vectors(vsi);
3133 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3137 ret = ice_vsi_alloc_rings(vsi);
3146 /* configure VSI nodes based on number of queues and TC's */
3147 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3148 max_txqs[i] = vsi->alloc_txq;
3150 if (ice_is_xdp_ena_vsi(vsi))
3151 max_txqs[i] += vsi->num_xdp_txq;
3154 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3157 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3158 vsi->vsi_num, ice_stat_str(status));
3163 return ice_schedule_reset(pf, ICE_RESET_PFR);
3166 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3172 ice_vsi_free_q_vectors(vsi);
3175 vsi->current_netdev_flags = 0;
3176 unregister_netdev(vsi->netdev);
3177 free_netdev(vsi->netdev);
3182 set_bit(ICE_RESET_FAILED, pf->state);
3188 * ice_is_reset_in_progress - check for a reset in progress
3189 * @state: PF state field
3191 bool ice_is_reset_in_progress(unsigned long *state)
3193 return test_bit(ICE_RESET_OICR_RECV, state) ||
3194 test_bit(ICE_PFR_REQ, state) ||
3195 test_bit(ICE_CORER_REQ, state) ||
3196 test_bit(ICE_GLOBR_REQ, state);
3201 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3202 * @vsi: VSI being configured
3203 * @ctx: the context buffer returned from AQ VSI update command
3205 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3207 vsi->info.mapping_flags = ctx->info.mapping_flags;
3208 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3209 sizeof(vsi->info.q_mapping));
3210 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3211 sizeof(vsi->info.tc_mapping));
3215 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3216 * @vsi: VSI to be configured
3217 * @ena_tc: TC bitmap
3219 * VSI queues expected to be quiesced before calling this function
3221 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3223 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3224 struct ice_pf *pf = vsi->back;
3225 struct ice_vsi_ctx *ctx;
3226 enum ice_status status;
3231 dev = ice_pf_to_dev(pf);
3233 ice_for_each_traffic_class(i) {
3234 /* build bitmap of enabled TCs */
3235 if (ena_tc & BIT(i))
3237 /* populate max_txqs per TC */
3238 max_txqs[i] = vsi->alloc_txq;
3241 vsi->tc_cfg.ena_tc = ena_tc;
3242 vsi->tc_cfg.numtc = num_tc;
3244 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3249 ctx->info = vsi->info;
3251 ice_vsi_setup_q_map(vsi, ctx);
3253 /* must to indicate which section of VSI context are being modified */
3254 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3255 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3257 dev_info(dev, "Failed VSI Update\n");
3262 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3266 dev_err(dev, "VSI %d failed TC config, error %s\n",
3267 vsi->vsi_num, ice_stat_str(status));
3271 ice_vsi_update_q_map(vsi, ctx);
3272 vsi->info.valid_sections = 0;
3274 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3279 #endif /* CONFIG_DCB */
3282 * ice_update_ring_stats - Update ring statistics
3283 * @ring: ring to update
3284 * @pkts: number of processed packets
3285 * @bytes: number of processed bytes
3287 * This function assumes that caller has acquired a u64_stats_sync lock.
3289 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3291 ring->stats.bytes += bytes;
3292 ring->stats.pkts += pkts;
3296 * ice_update_tx_ring_stats - Update Tx ring specific counters
3297 * @tx_ring: ring to update
3298 * @pkts: number of processed packets
3299 * @bytes: number of processed bytes
3301 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3303 u64_stats_update_begin(&tx_ring->syncp);
3304 ice_update_ring_stats(tx_ring, pkts, bytes);
3305 u64_stats_update_end(&tx_ring->syncp);
3309 * ice_update_rx_ring_stats - Update Rx ring specific counters
3310 * @rx_ring: ring to update
3311 * @pkts: number of processed packets
3312 * @bytes: number of processed bytes
3314 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3316 u64_stats_update_begin(&rx_ring->syncp);
3317 ice_update_ring_stats(rx_ring, pkts, bytes);
3318 u64_stats_update_end(&rx_ring->syncp);
3322 * ice_status_to_errno - convert from enum ice_status to Linux errno
3323 * @err: ice_status value to convert
3325 int ice_status_to_errno(enum ice_status err)
3330 case ICE_ERR_DOES_NOT_EXIST:
3332 case ICE_ERR_OUT_OF_RANGE:
3336 case ICE_ERR_NO_MEMORY:
3338 case ICE_ERR_MAX_LIMIT:
3346 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3347 * @sw: switch to check if its default forwarding VSI is free
3349 * Return true if the default forwarding VSI is already being used, else returns
3350 * false signalling that it's available to use.
3352 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3354 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3358 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3359 * @sw: switch for the default forwarding VSI to compare against
3360 * @vsi: VSI to compare against default forwarding VSI
3362 * If this VSI passed in is the default forwarding VSI then return true, else
3365 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3367 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3371 * ice_set_dflt_vsi - set the default forwarding VSI
3372 * @sw: switch used to assign the default forwarding VSI
3373 * @vsi: VSI getting set as the default forwarding VSI on the switch
3375 * If the VSI passed in is already the default VSI and it's enabled just return
3378 * If there is already a default VSI on the switch and it's enabled then return
3379 * -EEXIST since there can only be one default VSI per switch.
3381 * Otherwise try to set the VSI passed in as the switch's default VSI and
3382 * return the result.
3384 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3386 enum ice_status status;
3392 dev = ice_pf_to_dev(vsi->back);
3394 /* the VSI passed in is already the default VSI */
3395 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3396 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3401 /* another VSI is already the default VSI for this switch */
3402 if (ice_is_dflt_vsi_in_use(sw)) {
3403 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3404 sw->dflt_vsi->vsi_num);
3408 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3410 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3411 vsi->vsi_num, ice_stat_str(status));
3416 sw->dflt_vsi_ena = true;
3422 * ice_clear_dflt_vsi - clear the default forwarding VSI
3423 * @sw: switch used to clear the default VSI
3425 * If the switch has no default VSI or it's not enabled then return error.
3427 * Otherwise try to clear the default VSI and return the result.
3429 int ice_clear_dflt_vsi(struct ice_sw *sw)
3431 struct ice_vsi *dflt_vsi;
3432 enum ice_status status;
3438 dev = ice_pf_to_dev(sw->pf);
3440 dflt_vsi = sw->dflt_vsi;
3442 /* there is no default VSI configured */
3443 if (!ice_is_dflt_vsi_in_use(sw))
3446 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3449 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3450 dflt_vsi->vsi_num, ice_stat_str(status));
3454 sw->dflt_vsi = NULL;
3455 sw->dflt_vsi_ena = false;
3461 * ice_set_link - turn on/off physical link
3462 * @vsi: VSI to modify physical link on
3463 * @ena: turn on/off physical link
3465 int ice_set_link(struct ice_vsi *vsi, bool ena)
3467 struct device *dev = ice_pf_to_dev(vsi->back);
3468 struct ice_port_info *pi = vsi->port_info;
3469 struct ice_hw *hw = pi->hw;
3470 enum ice_status status;
3472 if (vsi->type != ICE_VSI_PF)
3475 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3477 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3478 * this is not a fatal error, so print a warning message and return
3479 * a success code. Return an error if FW returns an error code other
3480 * than ICE_AQ_RC_EMODE
3482 if (status == ICE_ERR_AQ_ERROR) {
3483 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3484 dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3485 (ena ? "ON" : "OFF"), ice_stat_str(status),
3486 ice_aq_str(hw->adminq.sq_last_status));
3487 } else if (status) {
3488 dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3489 (ena ? "ON" : "OFF"), ice_stat_str(status),
3490 ice_aq_str(hw->adminq.sq_last_status));