1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 #include "ice_dcb_lib.h"
10 #include "ice_devlink.h"
13 * ice_vsi_type_str - maps VSI type enum to string equivalents
14 * @vsi_type: VSI type enum
16 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
24 return "ICE_VSI_CTRL";
33 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
34 * @vsi: the VSI being configured
35 * @ena: start or stop the Rx rings
37 * First enable/disable all of the Rx rings, flush any remaining writes, and
38 * then verify that they have all been enabled/disabled successfully. This will
39 * let all of the register writes complete when enabling/disabling the Rx rings
40 * before waiting for the change in hardware to complete.
42 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
47 for (i = 0; i < vsi->num_rxq; i++)
48 ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
50 ice_flush(&vsi->back->hw);
52 for (i = 0; i < vsi->num_rxq; i++) {
53 ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
62 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
65 * On error: returns error code (negative)
66 * On success: returns 0
68 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
70 struct ice_pf *pf = vsi->back;
73 dev = ice_pf_to_dev(pf);
75 /* allocate memory for both Tx and Rx ring pointers */
76 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
77 sizeof(*vsi->tx_rings), GFP_KERNEL);
81 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
82 sizeof(*vsi->rx_rings), GFP_KERNEL);
86 /* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
87 vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
88 sizeof(*vsi->txq_map), GFP_KERNEL);
93 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
94 sizeof(*vsi->rxq_map), GFP_KERNEL);
98 /* There is no need to allocate q_vectors for a loopback VSI. */
99 if (vsi->type == ICE_VSI_LB)
102 /* allocate memory for q_vector pointers */
103 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
104 sizeof(*vsi->q_vectors), GFP_KERNEL);
111 devm_kfree(dev, vsi->rxq_map);
113 devm_kfree(dev, vsi->txq_map);
115 devm_kfree(dev, vsi->rx_rings);
117 devm_kfree(dev, vsi->tx_rings);
122 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
123 * @vsi: the VSI being configured
125 static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
131 /* a user could change the values of num_[tr]x_desc using
132 * ethtool -G so we should keep those values instead of
133 * overwriting them with the defaults.
135 if (!vsi->num_rx_desc)
136 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
137 if (!vsi->num_tx_desc)
138 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
141 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
148 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
149 * @vsi: the VSI being configured
150 * @vf_id: ID of the VF being configured
152 * Return 0 on success and a negative value on error
154 static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
156 struct ice_pf *pf = vsi->back;
157 struct ice_vf *vf = NULL;
159 if (vsi->type == ICE_VSI_VF)
164 vsi->alloc_txq = min3(pf->num_lan_msix,
165 ice_get_avail_txq_count(pf),
166 (u16)num_online_cpus());
168 vsi->alloc_txq = vsi->req_txq;
169 vsi->num_txq = vsi->req_txq;
172 pf->num_lan_tx = vsi->alloc_txq;
174 /* only 1 Rx queue unless RSS is enabled */
175 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
178 vsi->alloc_rxq = min3(pf->num_lan_msix,
179 ice_get_avail_rxq_count(pf),
180 (u16)num_online_cpus());
182 vsi->alloc_rxq = vsi->req_rxq;
183 vsi->num_rxq = vsi->req_rxq;
187 pf->num_lan_rx = vsi->alloc_rxq;
189 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
190 max_t(int, vsi->alloc_rxq,
194 vf = &pf->vf[vsi->vf_id];
195 vsi->alloc_txq = vf->num_vf_qs;
196 vsi->alloc_rxq = vf->num_vf_qs;
197 /* pf->num_msix_per_vf includes (VF miscellaneous vector +
198 * data queue interrupts). Since vsi->num_q_vectors is number
199 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
200 * original vector count
202 vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
207 vsi->num_q_vectors = 1;
214 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
218 ice_vsi_set_num_desc(vsi);
222 * ice_get_free_slot - get the next non-NULL location index in array
223 * @array: array to search
224 * @size: size of the array
225 * @curr: last known occupied index to be used as a search hint
227 * void * is being used to keep the functionality generic. This lets us use this
228 * function on any array of pointers.
230 static int ice_get_free_slot(void *array, int size, int curr)
232 int **tmp_array = (int **)array;
235 if (curr < (size - 1) && !tmp_array[curr + 1]) {
240 while ((i < size) && (tmp_array[i]))
251 * ice_vsi_delete - delete a VSI from the switch
252 * @vsi: pointer to VSI being removed
254 static void ice_vsi_delete(struct ice_vsi *vsi)
256 struct ice_pf *pf = vsi->back;
257 struct ice_vsi_ctx *ctxt;
258 enum ice_status status;
260 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
264 if (vsi->type == ICE_VSI_VF)
265 ctxt->vf_num = vsi->vf_id;
266 ctxt->vsi_num = vsi->vsi_num;
268 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
270 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
272 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
273 vsi->vsi_num, ice_stat_str(status));
279 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
280 * @vsi: pointer to VSI being cleared
282 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
284 struct ice_pf *pf = vsi->back;
287 dev = ice_pf_to_dev(pf);
289 /* free the ring and vector containers */
290 if (vsi->q_vectors) {
291 devm_kfree(dev, vsi->q_vectors);
292 vsi->q_vectors = NULL;
295 devm_kfree(dev, vsi->tx_rings);
296 vsi->tx_rings = NULL;
299 devm_kfree(dev, vsi->rx_rings);
300 vsi->rx_rings = NULL;
303 devm_kfree(dev, vsi->txq_map);
307 devm_kfree(dev, vsi->rxq_map);
313 * ice_vsi_clear - clean up and deallocate the provided VSI
314 * @vsi: pointer to VSI being cleared
316 * This deallocates the VSI's queue resources, removes it from the PF's
317 * VSI array if necessary, and deallocates the VSI
319 * Returns 0 on success, negative on failure
321 static int ice_vsi_clear(struct ice_vsi *vsi)
323 struct ice_pf *pf = NULL;
333 dev = ice_pf_to_dev(pf);
335 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
336 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
340 mutex_lock(&pf->sw_mutex);
341 /* updates the PF for this cleared VSI */
343 pf->vsi[vsi->idx] = NULL;
344 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
345 pf->next_vsi = vsi->idx;
346 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
347 vsi->vf_id != ICE_INVAL_VFID)
348 pf->next_vsi = vsi->idx;
350 ice_vsi_free_arrays(vsi);
351 mutex_unlock(&pf->sw_mutex);
352 devm_kfree(dev, vsi);
358 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
359 * @irq: interrupt number
360 * @data: pointer to a q_vector
362 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
364 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
366 if (!q_vector->tx.ring)
369 #define FDIR_RX_DESC_CLEAN_BUDGET 64
370 ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
371 ice_clean_ctrl_tx_irq(q_vector->tx.ring);
377 * ice_msix_clean_rings - MSIX mode Interrupt Handler
378 * @irq: interrupt number
379 * @data: pointer to a q_vector
381 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
383 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
385 if (!q_vector->tx.ring && !q_vector->rx.ring)
388 q_vector->total_events++;
390 napi_schedule(&q_vector->napi);
396 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
397 * @pf: board private structure
398 * @vsi_type: type of VSI
399 * @vf_id: ID of the VF being configured
401 * returns a pointer to a VSI on success, NULL on failure.
403 static struct ice_vsi *
404 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
406 struct device *dev = ice_pf_to_dev(pf);
407 struct ice_vsi *vsi = NULL;
409 /* Need to protect the allocation of the VSIs at the PF level */
410 mutex_lock(&pf->sw_mutex);
412 /* If we have already allocated our maximum number of VSIs,
413 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
414 * is available to be populated
416 if (pf->next_vsi == ICE_NO_VSI) {
417 dev_dbg(dev, "out of VSI slots!\n");
421 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
425 vsi->type = vsi_type;
427 set_bit(ICE_VSI_DOWN, vsi->state);
429 if (vsi_type == ICE_VSI_VF)
430 ice_vsi_set_num_qs(vsi, vf_id);
432 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
436 if (ice_vsi_alloc_arrays(vsi))
439 /* Setup default MSIX irq handler for VSI */
440 vsi->irq_handler = ice_msix_clean_rings;
443 if (ice_vsi_alloc_arrays(vsi))
446 /* Setup ctrl VSI MSIX irq handler */
447 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
450 if (ice_vsi_alloc_arrays(vsi))
454 if (ice_vsi_alloc_arrays(vsi))
458 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
462 if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
463 /* Use the last VSI slot as the index for PF control VSI */
464 vsi->idx = pf->num_alloc_vsi - 1;
465 pf->ctrl_vsi_idx = vsi->idx;
466 pf->vsi[vsi->idx] = vsi;
468 /* fill slot and make note of the index */
469 vsi->idx = pf->next_vsi;
470 pf->vsi[pf->next_vsi] = vsi;
472 /* prepare pf->next_vsi for next use */
473 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
477 if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
478 pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
482 devm_kfree(dev, vsi);
485 mutex_unlock(&pf->sw_mutex);
490 * ice_alloc_fd_res - Allocate FD resource for a VSI
491 * @vsi: pointer to the ice_vsi
493 * This allocates the FD resources
495 * Returns 0 on success, -EPERM on no-op or -EIO on failure
497 static int ice_alloc_fd_res(struct ice_vsi *vsi)
499 struct ice_pf *pf = vsi->back;
502 /* Flow Director filters are only allocated/assigned to the PF VSI which
503 * passes the traffic. The CTRL VSI is only used to add/delete filters
504 * so we don't allocate resources to it
507 /* FD filters from guaranteed pool per VSI */
508 g_val = pf->hw.func_caps.fd_fltr_guar;
512 /* FD filters from best effort pool */
513 b_val = pf->hw.func_caps.fd_fltr_best_effort;
517 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
520 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
523 vsi->num_gfltr = g_val / pf->num_alloc_vsi;
525 /* each VSI gets same "best_effort" quota */
526 vsi->num_bfltr = b_val;
528 if (vsi->type == ICE_VSI_VF) {
531 /* each VSI gets same "best_effort" quota */
532 vsi->num_bfltr = b_val;
539 * ice_vsi_get_qs - Assign queues from PF to VSI
540 * @vsi: the VSI to assign queues to
542 * Returns 0 on success and a negative value on error
544 static int ice_vsi_get_qs(struct ice_vsi *vsi)
546 struct ice_pf *pf = vsi->back;
547 struct ice_qs_cfg tx_qs_cfg = {
548 .qs_mutex = &pf->avail_q_mutex,
549 .pf_map = pf->avail_txqs,
550 .pf_map_size = pf->max_pf_txqs,
551 .q_count = vsi->alloc_txq,
552 .scatter_count = ICE_MAX_SCATTER_TXQS,
553 .vsi_map = vsi->txq_map,
555 .mapping_mode = ICE_VSI_MAP_CONTIG
557 struct ice_qs_cfg rx_qs_cfg = {
558 .qs_mutex = &pf->avail_q_mutex,
559 .pf_map = pf->avail_rxqs,
560 .pf_map_size = pf->max_pf_rxqs,
561 .q_count = vsi->alloc_rxq,
562 .scatter_count = ICE_MAX_SCATTER_RXQS,
563 .vsi_map = vsi->rxq_map,
565 .mapping_mode = ICE_VSI_MAP_CONTIG
569 ret = __ice_vsi_get_qs(&tx_qs_cfg);
572 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
574 ret = __ice_vsi_get_qs(&rx_qs_cfg);
577 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
583 * ice_vsi_put_qs - Release queues from VSI to PF
584 * @vsi: the VSI that is going to release queues
586 static void ice_vsi_put_qs(struct ice_vsi *vsi)
588 struct ice_pf *pf = vsi->back;
591 mutex_lock(&pf->avail_q_mutex);
593 for (i = 0; i < vsi->alloc_txq; i++) {
594 clear_bit(vsi->txq_map[i], pf->avail_txqs);
595 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
598 for (i = 0; i < vsi->alloc_rxq; i++) {
599 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
600 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
603 mutex_unlock(&pf->avail_q_mutex);
608 * @pf: pointer to the PF struct
610 * returns true if driver is in safe mode, false otherwise
612 bool ice_is_safe_mode(struct ice_pf *pf)
614 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
618 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
619 * @vsi: the VSI being cleaned up
621 * This function deletes RSS input set for all flows that were configured
624 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
626 struct ice_pf *pf = vsi->back;
627 enum ice_status status;
629 if (ice_is_safe_mode(pf))
632 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
634 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
635 vsi->vsi_num, ice_stat_str(status));
639 * ice_rss_clean - Delete RSS related VSI structures and configuration
640 * @vsi: the VSI being removed
642 static void ice_rss_clean(struct ice_vsi *vsi)
644 struct ice_pf *pf = vsi->back;
647 dev = ice_pf_to_dev(pf);
649 if (vsi->rss_hkey_user)
650 devm_kfree(dev, vsi->rss_hkey_user);
651 if (vsi->rss_lut_user)
652 devm_kfree(dev, vsi->rss_lut_user);
654 ice_vsi_clean_rss_flow_fld(vsi);
655 /* remove RSS replay list */
656 if (!ice_is_safe_mode(pf))
657 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
661 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
662 * @vsi: the VSI being configured
664 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
666 struct ice_hw_common_caps *cap;
667 struct ice_pf *pf = vsi->back;
669 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
674 cap = &pf->hw.func_caps.common_cap;
677 /* PF VSI will inherit RSS instance of PF */
678 vsi->rss_table_size = (u16)cap->rss_table_size;
679 vsi->rss_size = min_t(u16, num_online_cpus(),
680 BIT(cap->rss_table_entry_width));
681 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
684 /* VF VSI will get a small RSS table.
685 * For VSI_LUT, LUT size should be set to 64 bytes.
687 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
688 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
689 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
694 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
695 ice_vsi_type_str(vsi->type));
701 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
702 * @ctxt: the VSI context being set
704 * This initializes a default VSI context for all sections except the Queues.
706 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
710 memset(&ctxt->info, 0, sizeof(ctxt->info));
711 /* VSI's should be allocated from shared pool */
712 ctxt->alloc_from_pool = true;
713 /* Src pruning enabled by default */
714 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
715 /* Traffic from VSI can be sent to LAN */
716 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
717 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
718 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
719 * packets untagged/tagged.
721 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
722 ICE_AQ_VSI_VLAN_MODE_M) >>
723 ICE_AQ_VSI_VLAN_MODE_S);
724 /* Have 1:1 UP mapping for both ingress/egress tables */
725 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
726 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
727 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
728 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
729 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
730 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
731 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
732 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
733 ctxt->info.ingress_table = cpu_to_le32(table);
734 ctxt->info.egress_table = cpu_to_le32(table);
735 /* Have 1:1 UP mapping for outer to inner UP table */
736 ctxt->info.outer_up_table = cpu_to_le32(table);
737 /* No Outer tag support outer_tag_flags remains to zero */
741 * ice_vsi_setup_q_map - Setup a VSI queue map
742 * @vsi: the VSI being configured
743 * @ctxt: VSI context structure
745 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
747 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
748 u16 num_txq_per_tc, num_rxq_per_tc;
749 u16 qcount_tx = vsi->alloc_txq;
750 u16 qcount_rx = vsi->alloc_rxq;
751 bool ena_tc0 = false;
755 /* at least TC0 should be enabled by default */
756 if (vsi->tc_cfg.numtc) {
757 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
765 vsi->tc_cfg.ena_tc |= 1;
768 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
771 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
775 /* find the (rounded up) power-of-2 of qcount */
776 pow = (u16)order_base_2(num_rxq_per_tc);
778 /* TC mapping is a function of the number of Rx queues assigned to the
779 * VSI for each traffic class and the offset of these queues.
780 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
781 * queues allocated to TC0. No:of queues is a power-of-2.
783 * If TC is not enabled, the queue offset is set to 0, and allocate one
784 * queue, this way, traffic for the given TC will be sent to the default
787 * Setup number and offset of Rx queues for all TCs for the VSI
789 ice_for_each_traffic_class(i) {
790 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
791 /* TC is not enabled */
792 vsi->tc_cfg.tc_info[i].qoffset = 0;
793 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
794 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
795 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
796 ctxt->info.tc_mapping[i] = 0;
801 vsi->tc_cfg.tc_info[i].qoffset = offset;
802 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
803 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
804 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
806 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
807 ICE_AQ_VSI_TC_Q_OFFSET_M) |
808 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
809 ICE_AQ_VSI_TC_Q_NUM_M);
810 offset += num_rxq_per_tc;
811 tx_count += num_txq_per_tc;
812 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
815 /* if offset is non-zero, means it is calculated correctly based on
816 * enabled TCs for a given VSI otherwise qcount_rx will always
817 * be correct and non-zero because it is based off - VSI's
818 * allocated Rx queues which is at least 1 (hence qcount_tx will be
822 vsi->num_rxq = offset;
824 vsi->num_rxq = num_rxq_per_tc;
826 vsi->num_txq = tx_count;
828 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
829 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
830 /* since there is a chance that num_rxq could have been changed
831 * in the above for loop, make num_txq equal to num_rxq.
833 vsi->num_txq = vsi->num_rxq;
836 /* Rx queue mapping */
837 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
838 /* q_mapping buffer holds the info for the first queue allocated for
839 * this VSI in the PF space and also the number of queues associated
842 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
843 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
847 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
848 * @ctxt: the VSI context being set
849 * @vsi: the VSI being configured
851 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
853 u8 dflt_q_group, dflt_q_prio;
854 u16 dflt_q, report_q, val;
856 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
857 vsi->type != ICE_VSI_VF)
860 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
861 ctxt->info.valid_sections |= cpu_to_le16(val);
867 /* enable flow director filtering/programming */
868 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
869 ctxt->info.fd_options = cpu_to_le16(val);
870 /* max of allocated flow director filters */
871 ctxt->info.max_fd_fltr_dedicated =
872 cpu_to_le16(vsi->num_gfltr);
873 /* max of shared flow director filters any VSI may program */
874 ctxt->info.max_fd_fltr_shared =
875 cpu_to_le16(vsi->num_bfltr);
876 /* default queue index within the VSI of the default FD */
877 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
878 ICE_AQ_VSI_FD_DEF_Q_M);
879 /* target queue or queue group to the FD filter */
880 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
881 ICE_AQ_VSI_FD_DEF_GRP_M);
882 ctxt->info.fd_def_q = cpu_to_le16(val);
883 /* queue index on which FD filter completion is reported */
884 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
885 ICE_AQ_VSI_FD_REPORT_Q_M);
886 /* priority of the default qindex action */
887 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
888 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
889 ctxt->info.fd_report_opt = cpu_to_le16(val);
893 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
894 * @ctxt: the VSI context being set
895 * @vsi: the VSI being configured
897 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
899 u8 lut_type, hash_type;
904 dev = ice_pf_to_dev(pf);
908 /* PF VSI will inherit RSS instance of PF */
909 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
910 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
913 /* VF VSI will gets a small RSS table which is a VSI LUT type */
914 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
915 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
918 dev_dbg(dev, "Unsupported VSI type %s\n",
919 ice_vsi_type_str(vsi->type));
923 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
924 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
925 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
926 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
930 * ice_vsi_init - Create and initialize a VSI
931 * @vsi: the VSI being configured
932 * @init_vsi: is this call creating a VSI
934 * This initializes a VSI context depending on the VSI type to be added and
935 * passes it down to the add_vsi aq command to create a new VSI.
937 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
939 struct ice_pf *pf = vsi->back;
940 struct ice_hw *hw = &pf->hw;
941 struct ice_vsi_ctx *ctxt;
945 dev = ice_pf_to_dev(pf);
946 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
954 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
957 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
958 /* VF number here is the absolute VF number (0-255) */
959 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
966 ice_set_dflt_vsi_ctx(ctxt);
967 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
968 ice_set_fd_vsi_ctx(ctxt, vsi);
969 /* if the switch is in VEB mode, allow VSI loopback */
970 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
971 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
973 /* Set LUT type and HASH type if RSS is enabled */
974 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
975 vsi->type != ICE_VSI_CTRL) {
976 ice_set_rss_vsi_ctx(ctxt, vsi);
977 /* if updating VSI context, make sure to set valid_section:
978 * to indicate which section of VSI context being updated
981 ctxt->info.valid_sections |=
982 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
985 ctxt->info.sw_id = vsi->port_info->sw_id;
986 ice_vsi_setup_q_map(vsi, ctxt);
987 if (!init_vsi) /* means VSI being updated */
988 /* must to indicate which section of VSI context are
991 ctxt->info.valid_sections |=
992 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
994 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
997 if (vsi->type == ICE_VSI_VF) {
998 ctxt->info.valid_sections |=
999 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1000 if (pf->vf[vsi->vf_id].spoofchk) {
1001 ctxt->info.sec_flags |=
1002 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1003 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1004 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1006 ctxt->info.sec_flags &=
1007 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1008 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1009 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1013 /* Allow control frames out of main VSI */
1014 if (vsi->type == ICE_VSI_PF) {
1015 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1016 ctxt->info.valid_sections |=
1017 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1021 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1023 dev_err(dev, "Add VSI failed, err %d\n", ret);
1028 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1030 dev_err(dev, "Update VSI failed, err %d\n", ret);
1036 /* keep context for update VSI operations */
1037 vsi->info = ctxt->info;
1039 /* record VSI number returned */
1040 vsi->vsi_num = ctxt->vsi_num;
1048 * ice_free_res - free a block of resources
1049 * @res: pointer to the resource
1050 * @index: starting index previously returned by ice_get_res
1051 * @id: identifier to track owner
1053 * Returns number of resources freed
1055 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1060 if (!res || index >= res->end)
1063 id |= ICE_RES_VALID_BIT;
1064 for (i = index; i < res->end && res->list[i] == id; i++) {
1073 * ice_search_res - Search the tracker for a block of resources
1074 * @res: pointer to the resource
1075 * @needed: size of the block needed
1076 * @id: identifier to track owner
1078 * Returns the base item index of the block, or -ENOMEM for error
1080 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1082 u16 start = 0, end = 0;
1084 if (needed > res->end)
1087 id |= ICE_RES_VALID_BIT;
1090 /* skip already allocated entries */
1091 if (res->list[end++] & ICE_RES_VALID_BIT) {
1093 if ((start + needed) > res->end)
1097 if (end == (start + needed)) {
1100 /* there was enough, so assign it to the requestor */
1102 res->list[i++] = id;
1106 } while (end < res->end);
1112 * ice_get_free_res_count - Get free count from a resource tracker
1113 * @res: Resource tracker instance
1115 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1119 for (i = 0; i < res->end; i++)
1120 if (!(res->list[i] & ICE_RES_VALID_BIT))
1127 * ice_get_res - get a block of resources
1128 * @pf: board private structure
1129 * @res: pointer to the resource
1130 * @needed: size of the block needed
1131 * @id: identifier to track owner
1133 * Returns the base item index of the block, or negative for error
1136 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1141 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1142 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1143 needed, res->num_entries, id);
1147 return ice_search_res(res, needed, id);
1151 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1152 * @vsi: ptr to the VSI
1154 * This should only be called after ice_vsi_alloc() which allocates the
1155 * corresponding SW VSI structure and initializes num_queue_pairs for the
1156 * newly allocated VSI.
1158 * Returns 0 on success or negative on failure
1160 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1162 struct ice_pf *pf = vsi->back;
1167 dev = ice_pf_to_dev(pf);
1168 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1169 if (vsi->type == ICE_VSI_VF)
1172 if (vsi->base_vector) {
1173 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1174 vsi->vsi_num, vsi->base_vector);
1178 num_q_vectors = vsi->num_q_vectors;
1179 /* reserve slots from OS requested IRQs */
1180 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1184 ice_for_each_vf(pf, i) {
1186 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1187 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1191 if (i == pf->num_alloc_vfs)
1192 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1193 ICE_RES_VF_CTRL_VEC_ID);
1195 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1200 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1201 ice_get_free_res_count(pf->irq_tracker),
1202 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1205 vsi->base_vector = (u16)base;
1206 pf->num_avail_sw_msix -= num_q_vectors;
1212 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1213 * @vsi: the VSI having rings deallocated
1215 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1219 /* Avoid stale references by clearing map from vector to ring */
1220 if (vsi->q_vectors) {
1221 ice_for_each_q_vector(vsi, i) {
1222 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1225 q_vector->tx.ring = NULL;
1226 q_vector->rx.ring = NULL;
1231 if (vsi->tx_rings) {
1232 for (i = 0; i < vsi->alloc_txq; i++) {
1233 if (vsi->tx_rings[i]) {
1234 kfree_rcu(vsi->tx_rings[i], rcu);
1235 WRITE_ONCE(vsi->tx_rings[i], NULL);
1239 if (vsi->rx_rings) {
1240 for (i = 0; i < vsi->alloc_rxq; i++) {
1241 if (vsi->rx_rings[i]) {
1242 kfree_rcu(vsi->rx_rings[i], rcu);
1243 WRITE_ONCE(vsi->rx_rings[i], NULL);
1250 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1251 * @vsi: VSI which is having rings allocated
1253 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1255 struct ice_pf *pf = vsi->back;
1259 dev = ice_pf_to_dev(pf);
1260 /* Allocate Tx rings */
1261 for (i = 0; i < vsi->alloc_txq; i++) {
1262 struct ice_ring *ring;
1264 /* allocate with kzalloc(), free with kfree_rcu() */
1265 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1271 ring->reg_idx = vsi->txq_map[i];
1272 ring->ring_active = false;
1275 ring->count = vsi->num_tx_desc;
1276 WRITE_ONCE(vsi->tx_rings[i], ring);
1279 /* Allocate Rx rings */
1280 for (i = 0; i < vsi->alloc_rxq; i++) {
1281 struct ice_ring *ring;
1283 /* allocate with kzalloc(), free with kfree_rcu() */
1284 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1289 ring->reg_idx = vsi->rxq_map[i];
1290 ring->ring_active = false;
1292 ring->netdev = vsi->netdev;
1294 ring->count = vsi->num_rx_desc;
1295 WRITE_ONCE(vsi->rx_rings[i], ring);
1301 ice_vsi_clear_rings(vsi);
1306 * ice_vsi_manage_rss_lut - disable/enable RSS
1307 * @vsi: the VSI being changed
1308 * @ena: boolean value indicating if this is an enable or disable request
1310 * In the event of disable request for RSS, this function will zero out RSS
1311 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1314 int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1319 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1324 if (vsi->rss_lut_user)
1325 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1327 ice_fill_rss_lut(lut, vsi->rss_table_size,
1331 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1337 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1338 * @vsi: VSI to be configured
1340 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1342 struct ice_pf *pf = vsi->back;
1347 dev = ice_pf_to_dev(pf);
1348 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1350 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1354 if (vsi->rss_lut_user)
1355 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1357 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1359 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1361 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1362 goto ice_vsi_cfg_rss_exit;
1365 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1368 goto ice_vsi_cfg_rss_exit;
1371 if (vsi->rss_hkey_user)
1372 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1374 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1376 err = ice_set_rss_key(vsi, key);
1378 dev_err(dev, "set_rss_key failed, error %d\n", err);
1381 ice_vsi_cfg_rss_exit:
1387 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1388 * @vsi: VSI to be configured
1390 * This function will only be called during the VF VSI setup. Upon successful
1391 * completion of package download, this function will configure default RSS
1392 * input sets for VF VSI.
1394 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1396 struct ice_pf *pf = vsi->back;
1397 enum ice_status status;
1400 dev = ice_pf_to_dev(pf);
1401 if (ice_is_safe_mode(pf)) {
1402 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1407 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1409 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1410 vsi->vsi_num, ice_stat_str(status));
1414 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1415 * @vsi: VSI to be configured
1417 * This function will only be called after successful download package call
1418 * during initialization of PF. Since the downloaded package will erase the
1419 * RSS section, this function will configure RSS input sets for different
1420 * flow types. The last profile added has the highest priority, therefore 2
1421 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1422 * (i.e. IPv4 src/dst TCP src/dst port).
1424 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1426 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1427 struct ice_pf *pf = vsi->back;
1428 struct ice_hw *hw = &pf->hw;
1429 enum ice_status status;
1432 dev = ice_pf_to_dev(pf);
1433 if (ice_is_safe_mode(pf)) {
1434 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1438 /* configure RSS for IPv4 with input set IP src/dst */
1439 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1440 ICE_FLOW_SEG_HDR_IPV4);
1442 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1443 vsi_num, ice_stat_str(status));
1445 /* configure RSS for IPv6 with input set IPv6 src/dst */
1446 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1447 ICE_FLOW_SEG_HDR_IPV6);
1449 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1450 vsi_num, ice_stat_str(status));
1452 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1453 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1454 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1456 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1457 vsi_num, ice_stat_str(status));
1459 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1460 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1461 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1463 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1464 vsi_num, ice_stat_str(status));
1466 /* configure RSS for sctp4 with input set IP src/dst */
1467 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1468 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1470 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1471 vsi_num, ice_stat_str(status));
1473 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1474 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1475 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1477 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1478 vsi_num, ice_stat_str(status));
1480 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1481 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1482 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1484 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1485 vsi_num, ice_stat_str(status));
1487 /* configure RSS for sctp6 with input set IPv6 src/dst */
1488 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1489 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1491 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1492 vsi_num, ice_stat_str(status));
1496 * ice_pf_state_is_nominal - checks the PF for nominal state
1497 * @pf: pointer to PF to check
1499 * Check the PF's state for a collection of bits that would indicate
1500 * the PF is in a state that would inhibit normal operation for
1501 * driver functionality.
1503 * Returns true if PF is in a nominal state, false otherwise
1505 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1507 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1512 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1513 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1520 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1521 * @vsi: the VSI to be updated
1523 void ice_update_eth_stats(struct ice_vsi *vsi)
1525 struct ice_eth_stats *prev_es, *cur_es;
1526 struct ice_hw *hw = &vsi->back->hw;
1527 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1529 prev_es = &vsi->eth_stats_prev;
1530 cur_es = &vsi->eth_stats;
1532 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1533 &prev_es->rx_bytes, &cur_es->rx_bytes);
1535 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1536 &prev_es->rx_unicast, &cur_es->rx_unicast);
1538 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1539 &prev_es->rx_multicast, &cur_es->rx_multicast);
1541 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1542 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1544 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1545 &prev_es->rx_discards, &cur_es->rx_discards);
1547 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1548 &prev_es->tx_bytes, &cur_es->tx_bytes);
1550 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1551 &prev_es->tx_unicast, &cur_es->tx_unicast);
1553 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1554 &prev_es->tx_multicast, &cur_es->tx_multicast);
1556 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1557 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1559 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1560 &prev_es->tx_errors, &cur_es->tx_errors);
1562 vsi->stat_offsets_loaded = true;
1566 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1567 * @vsi: the VSI being configured
1568 * @vid: VLAN ID to be added
1569 * @action: filter action to be performed on match
1572 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1574 struct ice_pf *pf = vsi->back;
1578 dev = ice_pf_to_dev(pf);
1580 if (!ice_fltr_add_vlan(vsi, vid, action)) {
1584 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1592 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1593 * @vsi: the VSI being configured
1594 * @vid: VLAN ID to be removed
1596 * Returns 0 on success and negative on failure
1598 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1600 struct ice_pf *pf = vsi->back;
1601 enum ice_status status;
1605 dev = ice_pf_to_dev(pf);
1607 status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1610 } else if (status == ICE_ERR_DOES_NOT_EXIST) {
1611 dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1612 vid, vsi->vsi_num, ice_stat_str(status));
1614 dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1615 vid, vsi->vsi_num, ice_stat_str(status));
1623 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1626 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1628 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1629 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1630 vsi->rx_buf_len = ICE_RXBUF_2048;
1631 #if (PAGE_SIZE < 8192)
1632 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1633 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1634 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1635 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1638 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1639 #if (PAGE_SIZE < 8192)
1640 vsi->rx_buf_len = ICE_RXBUF_3072;
1642 vsi->rx_buf_len = ICE_RXBUF_2048;
1648 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1650 * @pf_q: index of the Rx queue in the PF's queue space
1651 * @rxdid: flexible descriptor RXDID
1652 * @prio: priority for the RXDID for this queue
1655 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio)
1657 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1659 /* clear any previous values */
1660 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1661 QRXFLXP_CNTXT_RXDID_PRIO_M |
1662 QRXFLXP_CNTXT_TS_M);
1664 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1665 QRXFLXP_CNTXT_RXDID_IDX_M;
1667 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1668 QRXFLXP_CNTXT_RXDID_PRIO_M;
1670 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1674 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1675 * @vsi: the VSI being configured
1677 * Return 0 on success and a negative value on error
1678 * Configure the Rx VSI for operation.
1680 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1684 if (vsi->type == ICE_VSI_VF)
1687 ice_vsi_cfg_frame_size(vsi);
1689 /* set up individual rings */
1690 for (i = 0; i < vsi->num_rxq; i++) {
1693 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
1695 dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1705 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1706 * @vsi: the VSI being configured
1707 * @rings: Tx ring array to be configured
1709 * Return 0 on success and a negative value on error
1710 * Configure the Tx VSI for operation.
1713 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1715 struct ice_aqc_add_tx_qgrp *qg_buf;
1719 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1723 qg_buf->num_txqs = 1;
1725 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
1726 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1737 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1738 * @vsi: the VSI being configured
1740 * Return 0 on success and a negative value on error
1741 * Configure the Tx VSI for operation.
1743 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1745 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1749 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1750 * @vsi: the VSI being configured
1752 * Return 0 on success and a negative value on error
1753 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1755 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1760 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
1764 for (i = 0; i < vsi->num_xdp_txq; i++)
1765 vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1771 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1772 * @intrl: interrupt rate limit in usecs
1773 * @gran: interrupt rate limit granularity in usecs
1775 * This function converts a decimal interrupt rate limit in usecs to the format
1776 * expected by firmware.
1778 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1780 u32 val = intrl / gran;
1783 return val | GLINT_RATE_INTRL_ENA_M;
1788 * ice_write_intrl - write throttle rate limit to interrupt specific register
1789 * @q_vector: pointer to interrupt specific structure
1790 * @intrl: throttle rate limit in microseconds to write
1792 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1794 struct ice_hw *hw = &q_vector->vsi->back->hw;
1796 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1797 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1801 * __ice_write_itr - write throttle rate to register
1802 * @q_vector: pointer to interrupt data structure
1803 * @rc: pointer to ring container
1804 * @itr: throttle rate in microseconds to write
1806 static void __ice_write_itr(struct ice_q_vector *q_vector,
1807 struct ice_ring_container *rc, u16 itr)
1809 struct ice_hw *hw = &q_vector->vsi->back->hw;
1811 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1812 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1816 * ice_write_itr - write throttle rate to queue specific register
1817 * @rc: pointer to ring container
1818 * @itr: throttle rate in microseconds to write
1820 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1822 struct ice_q_vector *q_vector;
1827 q_vector = rc->ring->q_vector;
1829 __ice_write_itr(q_vector, rc, itr);
1833 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1834 * @vsi: the VSI being configured
1836 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1839 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1841 struct ice_pf *pf = vsi->back;
1842 struct ice_hw *hw = &pf->hw;
1843 u16 txq = 0, rxq = 0;
1846 for (i = 0; i < vsi->num_q_vectors; i++) {
1847 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1848 u16 reg_idx = q_vector->reg_idx;
1850 ice_cfg_itr(hw, q_vector);
1852 /* Both Transmit Queue Interrupt Cause Control register
1853 * and Receive Queue Interrupt Cause control register
1854 * expects MSIX_INDX field to be the vector index
1855 * within the function space and not the absolute
1856 * vector index across PF or across device.
1857 * For SR-IOV VF VSIs queue vector index always starts
1858 * with 1 since first vector index(0) is used for OICR
1859 * in VF space. Since VMDq and other PF VSIs are within
1860 * the PF function space, use the vector index that is
1861 * tracked for this PF.
1863 for (q = 0; q < q_vector->num_ring_tx; q++) {
1864 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1865 q_vector->tx.itr_idx);
1869 for (q = 0; q < q_vector->num_ring_rx; q++) {
1870 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1871 q_vector->rx.itr_idx);
1878 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1879 * @vsi: the VSI being changed
1881 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1883 struct ice_hw *hw = &vsi->back->hw;
1884 struct ice_vsi_ctx *ctxt;
1885 enum ice_status status;
1888 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1892 /* Here we are configuring the VSI to let the driver add VLAN tags by
1893 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1894 * insertion happens in the Tx hot path, in ice_tx_map.
1896 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1898 /* Preserve existing VLAN strip setting */
1899 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1900 ICE_AQ_VSI_VLAN_EMOD_M);
1902 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1904 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1906 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1907 ice_stat_str(status),
1908 ice_aq_str(hw->adminq.sq_last_status));
1913 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1920 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1921 * @vsi: the VSI being changed
1922 * @ena: boolean value indicating if this is a enable or disable request
1924 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1926 struct ice_hw *hw = &vsi->back->hw;
1927 struct ice_vsi_ctx *ctxt;
1928 enum ice_status status;
1931 /* do not allow modifying VLAN stripping when a port VLAN is configured
1937 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1941 /* Here we are configuring what the VSI should do with the VLAN tag in
1942 * the Rx packet. We can either leave the tag in the packet or put it in
1943 * the Rx descriptor.
1946 /* Strip VLAN tag from Rx packet and put it in the desc */
1947 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1949 /* Disable stripping. Leave tag in packet */
1950 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1952 /* Allow all packets untagged/tagged */
1953 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1955 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1957 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1959 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
1960 ena, ice_stat_str(status),
1961 ice_aq_str(hw->adminq.sq_last_status));
1966 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1973 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1974 * @vsi: the VSI whose rings are to be enabled
1976 * Returns 0 on success and a negative value on error
1978 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1980 return ice_vsi_ctrl_all_rx_rings(vsi, true);
1984 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1985 * @vsi: the VSI whose rings are to be disabled
1987 * Returns 0 on success and a negative value on error
1989 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1991 return ice_vsi_ctrl_all_rx_rings(vsi, false);
1995 * ice_vsi_stop_tx_rings - Disable Tx rings
1996 * @vsi: the VSI being configured
1997 * @rst_src: reset source
1998 * @rel_vmvf_num: Relative ID of VF/VM
1999 * @rings: Tx ring array to be stopped
2002 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2003 u16 rel_vmvf_num, struct ice_ring **rings)
2007 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2010 for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
2011 struct ice_txq_meta txq_meta = { };
2014 if (!rings || !rings[q_idx])
2017 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2018 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2019 rings[q_idx], &txq_meta);
2029 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2030 * @vsi: the VSI being configured
2031 * @rst_src: reset source
2032 * @rel_vmvf_num: Relative ID of VF/VM
2035 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2038 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
2042 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2043 * @vsi: the VSI being configured
2045 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2047 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
2051 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2052 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2054 * returns true if Rx VLAN pruning is enabled and false otherwise.
2056 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2061 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2065 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2066 * @vsi: VSI to enable or disable VLAN pruning on
2067 * @ena: set to true to enable VLAN pruning and false to disable it
2068 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2070 * returns 0 if VSI is updated, negative otherwise
2072 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2074 struct ice_vsi_ctx *ctxt;
2081 /* Don't enable VLAN pruning if the netdev is currently in promiscuous
2082 * mode. VLAN pruning will be enabled when the interface exits
2083 * promiscuous mode if any VLAN filters are active.
2085 if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2089 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2093 ctxt->info = vsi->info;
2096 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2098 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2101 ctxt->info.valid_sections =
2102 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2104 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2106 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2107 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2108 ice_stat_str(status),
2109 ice_aq_str(pf->hw.adminq.sq_last_status));
2113 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2123 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2125 struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2127 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2128 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2132 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2133 * @vsi: VSI to set the q_vectors register index on
2136 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2140 if (!vsi || !vsi->q_vectors)
2143 ice_for_each_q_vector(vsi, i) {
2144 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2147 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2152 if (vsi->type == ICE_VSI_VF) {
2153 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2155 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2158 q_vector->v_idx + vsi->base_vector;
2165 ice_for_each_q_vector(vsi, i) {
2166 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2169 q_vector->reg_idx = 0;
2176 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2177 * @vsi: the VSI being configured
2178 * @tx: bool to determine Tx or Rx rule
2179 * @create: bool to determine create or remove Rule
2181 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2183 enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2184 enum ice_sw_fwd_act_type act);
2185 struct ice_pf *pf = vsi->back;
2186 enum ice_status status;
2189 dev = ice_pf_to_dev(pf);
2190 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2193 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2196 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2197 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2200 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2206 dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2207 create ? "adding" : "removing", tx ? "TX" : "RX",
2208 vsi->vsi_num, ice_stat_str(status));
2212 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2213 * @vsi: pointer to the VSI
2215 * This function will allocate new scheduler aggregator now if needed and will
2216 * move specified VSI into it.
2218 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2220 struct device *dev = ice_pf_to_dev(vsi->back);
2221 struct ice_agg_node *agg_node_iter = NULL;
2222 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2223 struct ice_agg_node *agg_node = NULL;
2224 int node_offset, max_agg_nodes = 0;
2225 struct ice_port_info *port_info;
2226 struct ice_pf *pf = vsi->back;
2227 u32 agg_node_id_start = 0;
2228 enum ice_status status;
2230 /* create (as needed) scheduler aggregator node and move VSI into
2231 * corresponding aggregator node
2232 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2233 * - VF aggregator nodes will contain VF VSI
2235 port_info = pf->hw.port_info;
2239 switch (vsi->type) {
2243 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2244 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2245 agg_node_iter = &pf->pf_agg_node[0];
2248 /* user can create 'n' VFs on a given PF, but since max children
2249 * per aggregator node can be only 64. Following code handles
2250 * aggregator(s) for VF VSIs, either selects a agg_node which
2251 * was already created provided num_vsis < 64, otherwise
2252 * select next available node, which will be created
2254 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2255 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2256 agg_node_iter = &pf->vf_agg_node[0];
2259 /* other VSI type, handle later if needed */
2260 dev_dbg(dev, "unexpected VSI type %s\n",
2261 ice_vsi_type_str(vsi->type));
2265 /* find the appropriate aggregator node */
2266 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2267 /* see if we can find space in previously created
2268 * node if num_vsis < 64, otherwise skip
2270 if (agg_node_iter->num_vsis &&
2271 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2276 if (agg_node_iter->valid &&
2277 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2278 agg_id = agg_node_iter->agg_id;
2279 agg_node = agg_node_iter;
2283 /* find unclaimed agg_id */
2284 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2285 agg_id = node_offset + agg_node_id_start;
2286 agg_node = agg_node_iter;
2289 /* move to next agg_node */
2296 /* if selected aggregator node was not created, create it */
2297 if (!agg_node->valid) {
2298 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2299 (u8)vsi->tc_cfg.ena_tc);
2301 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2305 /* aggregator node is created, store the neeeded info */
2306 agg_node->valid = true;
2307 agg_node->agg_id = agg_id;
2310 /* move VSI to corresponding aggregator node */
2311 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2312 (u8)vsi->tc_cfg.ena_tc);
2314 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2319 /* keep active children count for aggregator node */
2320 agg_node->num_vsis++;
2322 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2323 * to aggregator node
2325 vsi->agg_node = agg_node;
2326 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2327 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2328 vsi->agg_node->num_vsis);
2332 * ice_vsi_setup - Set up a VSI by a given type
2333 * @pf: board private structure
2334 * @pi: pointer to the port_info instance
2335 * @vsi_type: VSI type
2336 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2337 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2338 * fill-in ICE_INVAL_VFID as input.
2340 * This allocates the sw VSI structure and its queue resources.
2342 * Returns pointer to the successfully allocated and configured VSI sw struct on
2343 * success, NULL on failure.
2346 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2347 enum ice_vsi_type vsi_type, u16 vf_id)
2349 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2350 struct device *dev = ice_pf_to_dev(pf);
2351 enum ice_status status;
2352 struct ice_vsi *vsi;
2355 if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2356 vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2358 vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2361 dev_err(dev, "could not allocate VSI\n");
2365 vsi->port_info = pi;
2366 vsi->vsw = pf->first_sw;
2367 if (vsi->type == ICE_VSI_PF)
2368 vsi->ethtype = ETH_P_PAUSE;
2370 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2373 ice_alloc_fd_res(vsi);
2375 if (ice_vsi_get_qs(vsi)) {
2376 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2378 goto unroll_vsi_alloc;
2381 /* set RSS capabilities */
2382 ice_vsi_set_rss_params(vsi);
2384 /* set TC configuration */
2385 ice_vsi_set_tc_cfg(vsi);
2387 /* create the VSI */
2388 ret = ice_vsi_init(vsi, true);
2392 switch (vsi->type) {
2395 ret = ice_vsi_alloc_q_vectors(vsi);
2397 goto unroll_vsi_init;
2399 ret = ice_vsi_setup_vector_base(vsi);
2401 goto unroll_alloc_q_vector;
2403 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2405 goto unroll_vector_base;
2407 ret = ice_vsi_alloc_rings(vsi);
2409 goto unroll_vector_base;
2411 /* Always add VLAN ID 0 switch rule by default. This is needed
2412 * in order to allow all untagged and 0 tagged priority traffic
2413 * if Rx VLAN pruning is enabled. Also there are cases where we
2414 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2415 * so this handles those cases (i.e. adding the PF to a bridge
2416 * without the 8021q module loaded).
2418 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2420 goto unroll_clear_rings;
2422 ice_vsi_map_rings_to_vectors(vsi);
2424 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2425 if (vsi->type != ICE_VSI_CTRL)
2426 /* Do not exit if configuring RSS had an issue, at
2427 * least receive traffic on first queue. Hence no
2428 * need to capture return value
2430 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2431 ice_vsi_cfg_rss_lut_key(vsi);
2432 ice_vsi_set_rss_flow_fld(vsi);
2437 /* VF driver will take care of creating netdev for this type and
2438 * map queues to vectors through Virtchnl, PF driver only
2439 * creates a VSI and corresponding structures for bookkeeping
2442 ret = ice_vsi_alloc_q_vectors(vsi);
2444 goto unroll_vsi_init;
2446 ret = ice_vsi_alloc_rings(vsi);
2448 goto unroll_alloc_q_vector;
2450 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2452 goto unroll_vector_base;
2454 /* Do not exit if configuring RSS had an issue, at least
2455 * receive traffic on first queue. Hence no need to capture
2458 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2459 ice_vsi_cfg_rss_lut_key(vsi);
2460 ice_vsi_set_vf_rss_flow_fld(vsi);
2464 ret = ice_vsi_alloc_rings(vsi);
2466 goto unroll_vsi_init;
2469 /* clean up the resources and exit */
2470 goto unroll_vsi_init;
2473 /* configure VSI nodes based on number of queues and TC's */
2474 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2475 max_txqs[i] = vsi->alloc_txq;
2477 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2480 dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2481 vsi->vsi_num, ice_stat_str(status));
2482 goto unroll_clear_rings;
2485 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2486 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2487 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2488 * The rule is added once for PF VSI in order to create appropriate
2489 * recipe, since VSI/VSI list is ignored with drop action...
2490 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2491 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2492 * settings in the HW.
2494 if (!ice_is_safe_mode(pf))
2495 if (vsi->type == ICE_VSI_PF) {
2496 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2498 ice_cfg_sw_lldp(vsi, true, true);
2502 ice_set_agg_vsi(vsi);
2506 ice_vsi_clear_rings(vsi);
2508 /* reclaim SW interrupts back to the common pool */
2509 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2510 pf->num_avail_sw_msix += vsi->num_q_vectors;
2511 unroll_alloc_q_vector:
2512 ice_vsi_free_q_vectors(vsi);
2514 ice_vsi_delete(vsi);
2516 ice_vsi_put_qs(vsi);
2518 if (vsi_type == ICE_VSI_VF)
2519 ice_enable_lag(pf->lag);
2526 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2527 * @vsi: the VSI being cleaned up
2529 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2531 struct ice_pf *pf = vsi->back;
2532 struct ice_hw *hw = &pf->hw;
2537 for (i = 0; i < vsi->num_q_vectors; i++) {
2538 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2540 ice_write_intrl(q_vector, 0);
2541 for (q = 0; q < q_vector->num_ring_tx; q++) {
2542 ice_write_itr(&q_vector->tx, 0);
2543 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2544 if (ice_is_xdp_ena_vsi(vsi)) {
2545 u32 xdp_txq = txq + vsi->num_xdp_txq;
2547 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2552 for (q = 0; q < q_vector->num_ring_rx; q++) {
2553 ice_write_itr(&q_vector->rx, 0);
2554 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2563 * ice_vsi_free_irq - Free the IRQ association with the OS
2564 * @vsi: the VSI being configured
2566 void ice_vsi_free_irq(struct ice_vsi *vsi)
2568 struct ice_pf *pf = vsi->back;
2569 int base = vsi->base_vector;
2572 if (!vsi->q_vectors || !vsi->irqs_ready)
2575 ice_vsi_release_msix(vsi);
2576 if (vsi->type == ICE_VSI_VF)
2579 vsi->irqs_ready = false;
2580 ice_for_each_q_vector(vsi, i) {
2581 u16 vector = i + base;
2584 irq_num = pf->msix_entries[vector].vector;
2586 /* free only the irqs that were actually requested */
2587 if (!vsi->q_vectors[i] ||
2588 !(vsi->q_vectors[i]->num_ring_tx ||
2589 vsi->q_vectors[i]->num_ring_rx))
2592 /* clear the affinity notifier in the IRQ descriptor */
2593 irq_set_affinity_notifier(irq_num, NULL);
2595 /* clear the affinity_mask in the IRQ descriptor */
2596 irq_set_affinity_hint(irq_num, NULL);
2597 synchronize_irq(irq_num);
2598 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2603 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2604 * @vsi: the VSI having resources freed
2606 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2613 ice_for_each_txq(vsi, i)
2614 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2615 ice_free_tx_ring(vsi->tx_rings[i]);
2619 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2620 * @vsi: the VSI having resources freed
2622 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2629 ice_for_each_rxq(vsi, i)
2630 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2631 ice_free_rx_ring(vsi->rx_rings[i]);
2635 * ice_vsi_close - Shut down a VSI
2636 * @vsi: the VSI being shut down
2638 void ice_vsi_close(struct ice_vsi *vsi)
2640 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2643 ice_vsi_free_irq(vsi);
2644 ice_vsi_free_tx_rings(vsi);
2645 ice_vsi_free_rx_rings(vsi);
2649 * ice_ena_vsi - resume a VSI
2650 * @vsi: the VSI being resume
2651 * @locked: is the rtnl_lock already held
2653 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2657 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2660 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2662 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2663 if (netif_running(vsi->netdev)) {
2667 err = ice_open_internal(vsi->netdev);
2672 } else if (vsi->type == ICE_VSI_CTRL) {
2673 err = ice_vsi_open_ctrl(vsi);
2680 * ice_dis_vsi - pause a VSI
2681 * @vsi: the VSI being paused
2682 * @locked: is the rtnl_lock already held
2684 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2686 if (test_bit(ICE_VSI_DOWN, vsi->state))
2689 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2691 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2692 if (netif_running(vsi->netdev)) {
2703 } else if (vsi->type == ICE_VSI_CTRL) {
2709 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2710 * @vsi: the VSI being un-configured
2712 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2714 int base = vsi->base_vector;
2715 struct ice_pf *pf = vsi->back;
2716 struct ice_hw *hw = &pf->hw;
2720 /* disable interrupt causation from each queue */
2721 if (vsi->tx_rings) {
2722 ice_for_each_txq(vsi, i) {
2723 if (vsi->tx_rings[i]) {
2726 reg = vsi->tx_rings[i]->reg_idx;
2727 val = rd32(hw, QINT_TQCTL(reg));
2728 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2729 wr32(hw, QINT_TQCTL(reg), val);
2734 if (vsi->rx_rings) {
2735 ice_for_each_rxq(vsi, i) {
2736 if (vsi->rx_rings[i]) {
2739 reg = vsi->rx_rings[i]->reg_idx;
2740 val = rd32(hw, QINT_RQCTL(reg));
2741 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2742 wr32(hw, QINT_RQCTL(reg), val);
2747 /* disable each interrupt */
2748 ice_for_each_q_vector(vsi, i) {
2749 if (!vsi->q_vectors[i])
2751 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2756 /* don't call synchronize_irq() for VF's from the host */
2757 if (vsi->type == ICE_VSI_VF)
2760 ice_for_each_q_vector(vsi, i)
2761 synchronize_irq(pf->msix_entries[i + base].vector);
2765 * ice_napi_del - Remove NAPI handler for the VSI
2766 * @vsi: VSI for which NAPI handler is to be removed
2768 void ice_napi_del(struct ice_vsi *vsi)
2775 ice_for_each_q_vector(vsi, v_idx)
2776 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2780 * ice_vsi_release - Delete a VSI and free its resources
2781 * @vsi: the VSI being removed
2783 * Returns 0 on success or < 0 on error
2785 int ice_vsi_release(struct ice_vsi *vsi)
2793 /* do not unregister while driver is in the reset recovery pending
2794 * state. Since reset/rebuild happens through PF service task workqueue,
2795 * it's not a good idea to unregister netdev that is associated to the
2796 * PF that is running the work queue items currently. This is done to
2797 * avoid check_flush_dependency() warning on this wq
2799 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2800 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2801 unregister_netdev(vsi->netdev);
2802 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2805 ice_devlink_destroy_port(vsi);
2807 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2810 /* Disable VSI and free resources */
2811 if (vsi->type != ICE_VSI_LB)
2812 ice_vsi_dis_irq(vsi);
2815 /* SR-IOV determines needed MSIX resources all at once instead of per
2816 * VSI since when VFs are spawned we know how many VFs there are and how
2817 * many interrupts each VF needs. SR-IOV MSIX resources are also
2818 * cleared in the same manner.
2820 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2824 ice_for_each_vf(pf, i) {
2826 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2829 if (i == pf->num_alloc_vfs) {
2830 /* No other VFs left that have control VSI, reclaim SW
2831 * interrupts back to the common pool
2833 ice_free_res(pf->irq_tracker, vsi->base_vector,
2834 ICE_RES_VF_CTRL_VEC_ID);
2835 pf->num_avail_sw_msix += vsi->num_q_vectors;
2837 } else if (vsi->type != ICE_VSI_VF) {
2838 /* reclaim SW interrupts back to the common pool */
2839 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2840 pf->num_avail_sw_msix += vsi->num_q_vectors;
2843 if (!ice_is_safe_mode(pf)) {
2844 if (vsi->type == ICE_VSI_PF) {
2845 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2847 ice_cfg_sw_lldp(vsi, true, false);
2848 /* The Rx rule will only exist to remove if the LLDP FW
2849 * engine is currently stopped
2851 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2852 ice_cfg_sw_lldp(vsi, false, false);
2856 ice_fltr_remove_all(vsi);
2857 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2858 ice_vsi_delete(vsi);
2859 ice_vsi_free_q_vectors(vsi);
2862 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2863 unregister_netdev(vsi->netdev);
2864 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2866 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2867 free_netdev(vsi->netdev);
2869 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2873 if (vsi->type == ICE_VSI_VF &&
2874 vsi->agg_node && vsi->agg_node->valid)
2875 vsi->agg_node->num_vsis--;
2876 ice_vsi_clear_rings(vsi);
2878 ice_vsi_put_qs(vsi);
2880 /* retain SW VSI data structure since it is needed to unregister and
2881 * free VSI netdev when PF is not in reset recovery pending state,\
2882 * for ex: during rmmod.
2884 if (!ice_is_reset_in_progress(pf->state))
2891 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2892 * @vsi: VSI connected with q_vectors
2893 * @coalesce: array of struct with stored coalesce
2895 * Returns array size.
2898 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2899 struct ice_coalesce_stored *coalesce)
2903 ice_for_each_q_vector(vsi, i) {
2904 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2906 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2907 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2908 coalesce[i].intrl = q_vector->intrl;
2910 if (i < vsi->num_txq)
2911 coalesce[i].tx_valid = true;
2912 if (i < vsi->num_rxq)
2913 coalesce[i].rx_valid = true;
2916 return vsi->num_q_vectors;
2920 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2921 * @vsi: VSI connected with q_vectors
2922 * @coalesce: pointer to array of struct with stored coalesce
2923 * @size: size of coalesce array
2925 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2926 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2930 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2931 struct ice_coalesce_stored *coalesce, int size)
2933 struct ice_ring_container *rc;
2936 if ((size && !coalesce) || !vsi)
2939 /* There are a couple of cases that have to be handled here:
2940 * 1. The case where the number of queue vectors stays the same, but
2941 * the number of Tx or Rx rings changes (the first for loop)
2942 * 2. The case where the number of queue vectors increased (the
2945 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2946 /* There are 2 cases to handle here and they are the same for
2948 * if the entry was valid previously (coalesce[i].[tr]x_valid
2949 * and the loop variable is less than the number of rings
2950 * allocated, then write the previous values
2952 * if the entry was not valid previously, but the number of
2953 * rings is less than are allocated (this means the number of
2954 * rings increased from previously), then write out the
2955 * values in the first element
2957 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
2958 * as there is no harm because the dynamic algorithm
2959 * will just overwrite.
2961 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2962 rc = &vsi->q_vectors[i]->rx;
2963 rc->itr_setting = coalesce[i].itr_rx;
2964 ice_write_itr(rc, rc->itr_setting);
2965 } else if (i < vsi->alloc_rxq) {
2966 rc = &vsi->q_vectors[i]->rx;
2967 rc->itr_setting = coalesce[0].itr_rx;
2968 ice_write_itr(rc, rc->itr_setting);
2971 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2972 rc = &vsi->q_vectors[i]->tx;
2973 rc->itr_setting = coalesce[i].itr_tx;
2974 ice_write_itr(rc, rc->itr_setting);
2975 } else if (i < vsi->alloc_txq) {
2976 rc = &vsi->q_vectors[i]->tx;
2977 rc->itr_setting = coalesce[0].itr_tx;
2978 ice_write_itr(rc, rc->itr_setting);
2981 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2982 ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
2985 /* the number of queue vectors increased so write whatever is in
2988 for (; i < vsi->num_q_vectors; i++) {
2990 rc = &vsi->q_vectors[i]->tx;
2991 rc->itr_setting = coalesce[0].itr_tx;
2992 ice_write_itr(rc, rc->itr_setting);
2995 rc = &vsi->q_vectors[i]->rx;
2996 rc->itr_setting = coalesce[0].itr_rx;
2997 ice_write_itr(rc, rc->itr_setting);
2999 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3000 ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3005 * ice_vsi_rebuild - Rebuild VSI after reset
3006 * @vsi: VSI to be rebuild
3007 * @init_vsi: is this an initialization or a reconfigure of the VSI
3009 * Returns 0 on success and negative value on failure
3011 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3013 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3014 struct ice_coalesce_stored *coalesce;
3015 int prev_num_q_vectors = 0;
3016 struct ice_vf *vf = NULL;
3017 enum ice_vsi_type vtype;
3018 enum ice_status status;
3027 if (vtype == ICE_VSI_VF)
3028 vf = &pf->vf[vsi->vf_id];
3030 coalesce = kcalloc(vsi->num_q_vectors,
3031 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3035 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3037 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3038 ice_vsi_free_q_vectors(vsi);
3040 /* SR-IOV determines needed MSIX resources all at once instead of per
3041 * VSI since when VFs are spawned we know how many VFs there are and how
3042 * many interrupts each VF needs. SR-IOV MSIX resources are also
3043 * cleared in the same manner.
3045 if (vtype != ICE_VSI_VF) {
3046 /* reclaim SW interrupts back to the common pool */
3047 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3048 pf->num_avail_sw_msix += vsi->num_q_vectors;
3049 vsi->base_vector = 0;
3052 if (ice_is_xdp_ena_vsi(vsi))
3053 /* return value check can be skipped here, it always returns
3054 * 0 if reset is in progress
3056 ice_destroy_xdp_rings(vsi);
3057 ice_vsi_put_qs(vsi);
3058 ice_vsi_clear_rings(vsi);
3059 ice_vsi_free_arrays(vsi);
3060 if (vtype == ICE_VSI_VF)
3061 ice_vsi_set_num_qs(vsi, vf->vf_id);
3063 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3065 ret = ice_vsi_alloc_arrays(vsi);
3069 ice_vsi_get_qs(vsi);
3071 ice_alloc_fd_res(vsi);
3072 ice_vsi_set_tc_cfg(vsi);
3074 /* Initialize VSI struct elements and create VSI in FW */
3075 ret = ice_vsi_init(vsi, init_vsi);
3082 ret = ice_vsi_alloc_q_vectors(vsi);
3086 ret = ice_vsi_setup_vector_base(vsi);
3090 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3094 ret = ice_vsi_alloc_rings(vsi);
3098 ice_vsi_map_rings_to_vectors(vsi);
3099 if (ice_is_xdp_ena_vsi(vsi)) {
3100 vsi->num_xdp_txq = vsi->alloc_rxq;
3101 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3105 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3106 if (vtype != ICE_VSI_CTRL)
3107 /* Do not exit if configuring RSS had an issue, at
3108 * least receive traffic on first queue. Hence no
3109 * need to capture return value
3111 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3112 ice_vsi_cfg_rss_lut_key(vsi);
3115 ret = ice_vsi_alloc_q_vectors(vsi);
3119 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3123 ret = ice_vsi_alloc_rings(vsi);
3132 /* configure VSI nodes based on number of queues and TC's */
3133 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3134 max_txqs[i] = vsi->alloc_txq;
3136 if (ice_is_xdp_ena_vsi(vsi))
3137 max_txqs[i] += vsi->num_xdp_txq;
3140 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3143 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3144 vsi->vsi_num, ice_stat_str(status));
3149 return ice_schedule_reset(pf, ICE_RESET_PFR);
3152 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3158 ice_vsi_free_q_vectors(vsi);
3161 vsi->current_netdev_flags = 0;
3162 unregister_netdev(vsi->netdev);
3163 free_netdev(vsi->netdev);
3168 set_bit(ICE_RESET_FAILED, pf->state);
3174 * ice_is_reset_in_progress - check for a reset in progress
3175 * @state: PF state field
3177 bool ice_is_reset_in_progress(unsigned long *state)
3179 return test_bit(ICE_RESET_OICR_RECV, state) ||
3180 test_bit(ICE_PFR_REQ, state) ||
3181 test_bit(ICE_CORER_REQ, state) ||
3182 test_bit(ICE_GLOBR_REQ, state);
3187 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3188 * @vsi: VSI being configured
3189 * @ctx: the context buffer returned from AQ VSI update command
3191 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3193 vsi->info.mapping_flags = ctx->info.mapping_flags;
3194 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3195 sizeof(vsi->info.q_mapping));
3196 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3197 sizeof(vsi->info.tc_mapping));
3201 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3202 * @vsi: VSI to be configured
3203 * @ena_tc: TC bitmap
3205 * VSI queues expected to be quiesced before calling this function
3207 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3209 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3210 struct ice_pf *pf = vsi->back;
3211 struct ice_vsi_ctx *ctx;
3212 enum ice_status status;
3217 dev = ice_pf_to_dev(pf);
3219 ice_for_each_traffic_class(i) {
3220 /* build bitmap of enabled TCs */
3221 if (ena_tc & BIT(i))
3223 /* populate max_txqs per TC */
3224 max_txqs[i] = vsi->alloc_txq;
3227 vsi->tc_cfg.ena_tc = ena_tc;
3228 vsi->tc_cfg.numtc = num_tc;
3230 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3235 ctx->info = vsi->info;
3237 ice_vsi_setup_q_map(vsi, ctx);
3239 /* must to indicate which section of VSI context are being modified */
3240 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3241 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3243 dev_info(dev, "Failed VSI Update\n");
3248 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3252 dev_err(dev, "VSI %d failed TC config, error %s\n",
3253 vsi->vsi_num, ice_stat_str(status));
3257 ice_vsi_update_q_map(vsi, ctx);
3258 vsi->info.valid_sections = 0;
3260 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3265 #endif /* CONFIG_DCB */
3268 * ice_update_ring_stats - Update ring statistics
3269 * @ring: ring to update
3270 * @pkts: number of processed packets
3271 * @bytes: number of processed bytes
3273 * This function assumes that caller has acquired a u64_stats_sync lock.
3275 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3277 ring->stats.bytes += bytes;
3278 ring->stats.pkts += pkts;
3282 * ice_update_tx_ring_stats - Update Tx ring specific counters
3283 * @tx_ring: ring to update
3284 * @pkts: number of processed packets
3285 * @bytes: number of processed bytes
3287 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3289 u64_stats_update_begin(&tx_ring->syncp);
3290 ice_update_ring_stats(tx_ring, pkts, bytes);
3291 u64_stats_update_end(&tx_ring->syncp);
3295 * ice_update_rx_ring_stats - Update Rx ring specific counters
3296 * @rx_ring: ring to update
3297 * @pkts: number of processed packets
3298 * @bytes: number of processed bytes
3300 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3302 u64_stats_update_begin(&rx_ring->syncp);
3303 ice_update_ring_stats(rx_ring, pkts, bytes);
3304 u64_stats_update_end(&rx_ring->syncp);
3308 * ice_status_to_errno - convert from enum ice_status to Linux errno
3309 * @err: ice_status value to convert
3311 int ice_status_to_errno(enum ice_status err)
3316 case ICE_ERR_DOES_NOT_EXIST:
3318 case ICE_ERR_OUT_OF_RANGE:
3322 case ICE_ERR_NO_MEMORY:
3324 case ICE_ERR_MAX_LIMIT:
3332 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3333 * @sw: switch to check if its default forwarding VSI is free
3335 * Return true if the default forwarding VSI is already being used, else returns
3336 * false signalling that it's available to use.
3338 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3340 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3344 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3345 * @sw: switch for the default forwarding VSI to compare against
3346 * @vsi: VSI to compare against default forwarding VSI
3348 * If this VSI passed in is the default forwarding VSI then return true, else
3351 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3353 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3357 * ice_set_dflt_vsi - set the default forwarding VSI
3358 * @sw: switch used to assign the default forwarding VSI
3359 * @vsi: VSI getting set as the default forwarding VSI on the switch
3361 * If the VSI passed in is already the default VSI and it's enabled just return
3364 * If there is already a default VSI on the switch and it's enabled then return
3365 * -EEXIST since there can only be one default VSI per switch.
3367 * Otherwise try to set the VSI passed in as the switch's default VSI and
3368 * return the result.
3370 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3372 enum ice_status status;
3378 dev = ice_pf_to_dev(vsi->back);
3380 /* the VSI passed in is already the default VSI */
3381 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3382 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3387 /* another VSI is already the default VSI for this switch */
3388 if (ice_is_dflt_vsi_in_use(sw)) {
3389 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3390 sw->dflt_vsi->vsi_num);
3394 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3396 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3397 vsi->vsi_num, ice_stat_str(status));
3402 sw->dflt_vsi_ena = true;
3408 * ice_clear_dflt_vsi - clear the default forwarding VSI
3409 * @sw: switch used to clear the default VSI
3411 * If the switch has no default VSI or it's not enabled then return error.
3413 * Otherwise try to clear the default VSI and return the result.
3415 int ice_clear_dflt_vsi(struct ice_sw *sw)
3417 struct ice_vsi *dflt_vsi;
3418 enum ice_status status;
3424 dev = ice_pf_to_dev(sw->pf);
3426 dflt_vsi = sw->dflt_vsi;
3428 /* there is no default VSI configured */
3429 if (!ice_is_dflt_vsi_in_use(sw))
3432 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3435 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3436 dflt_vsi->vsi_num, ice_stat_str(status));
3440 sw->dflt_vsi = NULL;
3441 sw->dflt_vsi_ena = false;
3447 * ice_set_link - turn on/off physical link
3448 * @vsi: VSI to modify physical link on
3449 * @ena: turn on/off physical link
3451 int ice_set_link(struct ice_vsi *vsi, bool ena)
3453 struct device *dev = ice_pf_to_dev(vsi->back);
3454 struct ice_port_info *pi = vsi->port_info;
3455 struct ice_hw *hw = pi->hw;
3456 enum ice_status status;
3458 if (vsi->type != ICE_VSI_PF)
3461 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3463 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3464 * this is not a fatal error, so print a warning message and return
3465 * a success code. Return an error if FW returns an error code other
3466 * than ICE_AQ_RC_EMODE
3468 if (status == ICE_ERR_AQ_ERROR) {
3469 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3470 dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3471 (ena ? "ON" : "OFF"), ice_stat_str(status),
3472 ice_aq_str(hw->adminq.sq_last_status));
3473 } else if (status) {
3474 dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3475 (ena ? "ON" : "OFF"), ice_stat_str(status),
3476 ice_aq_str(hw->adminq.sq_last_status));