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;
173 vsi->alloc_txq = vsi->req_txq;
174 vsi->num_txq = vsi->req_txq;
176 vsi->alloc_txq = min3(pf->num_lan_msix,
177 ice_get_avail_txq_count(pf),
178 (u16)num_online_cpus());
181 pf->num_lan_tx = vsi->alloc_txq;
183 /* only 1 Rx queue unless RSS is enabled */
184 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
188 vsi->alloc_rxq = vsi->req_rxq;
189 vsi->num_rxq = vsi->req_rxq;
191 vsi->alloc_rxq = min3(pf->num_lan_msix,
192 ice_get_avail_rxq_count(pf),
193 (u16)num_online_cpus());
197 pf->num_lan_rx = vsi->alloc_rxq;
199 vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
200 max_t(int, vsi->alloc_rxq,
204 vf = &pf->vf[vsi->vf_id];
206 vf->num_vf_qs = vf->num_req_qs;
207 vsi->alloc_txq = vf->num_vf_qs;
208 vsi->alloc_rxq = vf->num_vf_qs;
209 /* pf->num_msix_per_vf includes (VF miscellaneous vector +
210 * data queue interrupts). Since vsi->num_q_vectors is number
211 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
212 * original vector count
214 vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
219 vsi->num_q_vectors = 1;
226 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
230 ice_vsi_set_num_desc(vsi);
234 * ice_get_free_slot - get the next non-NULL location index in array
235 * @array: array to search
236 * @size: size of the array
237 * @curr: last known occupied index to be used as a search hint
239 * void * is being used to keep the functionality generic. This lets us use this
240 * function on any array of pointers.
242 static int ice_get_free_slot(void *array, int size, int curr)
244 int **tmp_array = (int **)array;
247 if (curr < (size - 1) && !tmp_array[curr + 1]) {
252 while ((i < size) && (tmp_array[i]))
263 * ice_vsi_delete - delete a VSI from the switch
264 * @vsi: pointer to VSI being removed
266 static void ice_vsi_delete(struct ice_vsi *vsi)
268 struct ice_pf *pf = vsi->back;
269 struct ice_vsi_ctx *ctxt;
270 enum ice_status status;
272 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
276 if (vsi->type == ICE_VSI_VF)
277 ctxt->vf_num = vsi->vf_id;
278 ctxt->vsi_num = vsi->vsi_num;
280 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
282 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
284 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
285 vsi->vsi_num, ice_stat_str(status));
291 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
292 * @vsi: pointer to VSI being cleared
294 static void ice_vsi_free_arrays(struct ice_vsi *vsi)
296 struct ice_pf *pf = vsi->back;
299 dev = ice_pf_to_dev(pf);
301 if (vsi->af_xdp_zc_qps) {
302 bitmap_free(vsi->af_xdp_zc_qps);
303 vsi->af_xdp_zc_qps = NULL;
305 /* free the ring and vector containers */
306 if (vsi->q_vectors) {
307 devm_kfree(dev, vsi->q_vectors);
308 vsi->q_vectors = NULL;
311 devm_kfree(dev, vsi->tx_rings);
312 vsi->tx_rings = NULL;
315 devm_kfree(dev, vsi->rx_rings);
316 vsi->rx_rings = NULL;
319 devm_kfree(dev, vsi->txq_map);
323 devm_kfree(dev, vsi->rxq_map);
329 * ice_vsi_clear - clean up and deallocate the provided VSI
330 * @vsi: pointer to VSI being cleared
332 * This deallocates the VSI's queue resources, removes it from the PF's
333 * VSI array if necessary, and deallocates the VSI
335 * Returns 0 on success, negative on failure
337 static int ice_vsi_clear(struct ice_vsi *vsi)
339 struct ice_pf *pf = NULL;
349 dev = ice_pf_to_dev(pf);
351 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
352 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
356 mutex_lock(&pf->sw_mutex);
357 /* updates the PF for this cleared VSI */
359 pf->vsi[vsi->idx] = NULL;
360 if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
361 pf->next_vsi = vsi->idx;
362 if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
363 vsi->vf_id != ICE_INVAL_VFID)
364 pf->next_vsi = vsi->idx;
366 ice_vsi_free_arrays(vsi);
367 mutex_unlock(&pf->sw_mutex);
368 devm_kfree(dev, vsi);
374 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
375 * @irq: interrupt number
376 * @data: pointer to a q_vector
378 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
380 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
382 if (!q_vector->tx.ring)
385 #define FDIR_RX_DESC_CLEAN_BUDGET 64
386 ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
387 ice_clean_ctrl_tx_irq(q_vector->tx.ring);
393 * ice_msix_clean_rings - MSIX mode Interrupt Handler
394 * @irq: interrupt number
395 * @data: pointer to a q_vector
397 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
399 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
401 if (!q_vector->tx.ring && !q_vector->rx.ring)
404 q_vector->total_events++;
406 napi_schedule(&q_vector->napi);
412 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
413 * @pf: board private structure
414 * @vsi_type: type of VSI
415 * @vf_id: ID of the VF being configured
417 * returns a pointer to a VSI on success, NULL on failure.
419 static struct ice_vsi *
420 ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
422 struct device *dev = ice_pf_to_dev(pf);
423 struct ice_vsi *vsi = NULL;
425 /* Need to protect the allocation of the VSIs at the PF level */
426 mutex_lock(&pf->sw_mutex);
428 /* If we have already allocated our maximum number of VSIs,
429 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
430 * is available to be populated
432 if (pf->next_vsi == ICE_NO_VSI) {
433 dev_dbg(dev, "out of VSI slots!\n");
437 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
441 vsi->type = vsi_type;
443 set_bit(ICE_VSI_DOWN, vsi->state);
445 if (vsi_type == ICE_VSI_VF)
446 ice_vsi_set_num_qs(vsi, vf_id);
448 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
452 if (ice_vsi_alloc_arrays(vsi))
455 /* Setup default MSIX irq handler for VSI */
456 vsi->irq_handler = ice_msix_clean_rings;
459 if (ice_vsi_alloc_arrays(vsi))
462 /* Setup ctrl VSI MSIX irq handler */
463 vsi->irq_handler = ice_msix_clean_ctrl_vsi;
466 if (ice_vsi_alloc_arrays(vsi))
470 if (ice_vsi_alloc_arrays(vsi))
474 dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
478 if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
479 /* Use the last VSI slot as the index for PF control VSI */
480 vsi->idx = pf->num_alloc_vsi - 1;
481 pf->ctrl_vsi_idx = vsi->idx;
482 pf->vsi[vsi->idx] = vsi;
484 /* fill slot and make note of the index */
485 vsi->idx = pf->next_vsi;
486 pf->vsi[pf->next_vsi] = vsi;
488 /* prepare pf->next_vsi for next use */
489 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
493 if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
494 pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
498 devm_kfree(dev, vsi);
501 mutex_unlock(&pf->sw_mutex);
506 * ice_alloc_fd_res - Allocate FD resource for a VSI
507 * @vsi: pointer to the ice_vsi
509 * This allocates the FD resources
511 * Returns 0 on success, -EPERM on no-op or -EIO on failure
513 static int ice_alloc_fd_res(struct ice_vsi *vsi)
515 struct ice_pf *pf = vsi->back;
518 /* Flow Director filters are only allocated/assigned to the PF VSI which
519 * passes the traffic. The CTRL VSI is only used to add/delete filters
520 * so we don't allocate resources to it
523 /* FD filters from guaranteed pool per VSI */
524 g_val = pf->hw.func_caps.fd_fltr_guar;
528 /* FD filters from best effort pool */
529 b_val = pf->hw.func_caps.fd_fltr_best_effort;
533 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
536 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
539 vsi->num_gfltr = g_val / pf->num_alloc_vsi;
541 /* each VSI gets same "best_effort" quota */
542 vsi->num_bfltr = b_val;
544 if (vsi->type == ICE_VSI_VF) {
547 /* each VSI gets same "best_effort" quota */
548 vsi->num_bfltr = b_val;
555 * ice_vsi_get_qs - Assign queues from PF to VSI
556 * @vsi: the VSI to assign queues to
558 * Returns 0 on success and a negative value on error
560 static int ice_vsi_get_qs(struct ice_vsi *vsi)
562 struct ice_pf *pf = vsi->back;
563 struct ice_qs_cfg tx_qs_cfg = {
564 .qs_mutex = &pf->avail_q_mutex,
565 .pf_map = pf->avail_txqs,
566 .pf_map_size = pf->max_pf_txqs,
567 .q_count = vsi->alloc_txq,
568 .scatter_count = ICE_MAX_SCATTER_TXQS,
569 .vsi_map = vsi->txq_map,
571 .mapping_mode = ICE_VSI_MAP_CONTIG
573 struct ice_qs_cfg rx_qs_cfg = {
574 .qs_mutex = &pf->avail_q_mutex,
575 .pf_map = pf->avail_rxqs,
576 .pf_map_size = pf->max_pf_rxqs,
577 .q_count = vsi->alloc_rxq,
578 .scatter_count = ICE_MAX_SCATTER_RXQS,
579 .vsi_map = vsi->rxq_map,
581 .mapping_mode = ICE_VSI_MAP_CONTIG
585 ret = __ice_vsi_get_qs(&tx_qs_cfg);
588 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
590 ret = __ice_vsi_get_qs(&rx_qs_cfg);
593 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
599 * ice_vsi_put_qs - Release queues from VSI to PF
600 * @vsi: the VSI that is going to release queues
602 static void ice_vsi_put_qs(struct ice_vsi *vsi)
604 struct ice_pf *pf = vsi->back;
607 mutex_lock(&pf->avail_q_mutex);
609 for (i = 0; i < vsi->alloc_txq; i++) {
610 clear_bit(vsi->txq_map[i], pf->avail_txqs);
611 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
614 for (i = 0; i < vsi->alloc_rxq; i++) {
615 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
616 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
619 mutex_unlock(&pf->avail_q_mutex);
624 * @pf: pointer to the PF struct
626 * returns true if driver is in safe mode, false otherwise
628 bool ice_is_safe_mode(struct ice_pf *pf)
630 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
635 * @pf: pointer to the PF struct
637 * returns true if AUX devices/drivers are supported, false otherwise
639 bool ice_is_aux_ena(struct ice_pf *pf)
641 return test_bit(ICE_FLAG_AUX_ENA, pf->flags);
645 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
646 * @vsi: the VSI being cleaned up
648 * This function deletes RSS input set for all flows that were configured
651 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
653 struct ice_pf *pf = vsi->back;
654 enum ice_status status;
656 if (ice_is_safe_mode(pf))
659 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
661 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
662 vsi->vsi_num, ice_stat_str(status));
666 * ice_rss_clean - Delete RSS related VSI structures and configuration
667 * @vsi: the VSI being removed
669 static void ice_rss_clean(struct ice_vsi *vsi)
671 struct ice_pf *pf = vsi->back;
674 dev = ice_pf_to_dev(pf);
676 if (vsi->rss_hkey_user)
677 devm_kfree(dev, vsi->rss_hkey_user);
678 if (vsi->rss_lut_user)
679 devm_kfree(dev, vsi->rss_lut_user);
681 ice_vsi_clean_rss_flow_fld(vsi);
682 /* remove RSS replay list */
683 if (!ice_is_safe_mode(pf))
684 ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
688 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
689 * @vsi: the VSI being configured
691 static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
693 struct ice_hw_common_caps *cap;
694 struct ice_pf *pf = vsi->back;
696 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
701 cap = &pf->hw.func_caps.common_cap;
704 /* PF VSI will inherit RSS instance of PF */
705 vsi->rss_table_size = (u16)cap->rss_table_size;
706 vsi->rss_size = min_t(u16, num_online_cpus(),
707 BIT(cap->rss_table_entry_width));
708 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
711 /* VF VSI will get a small RSS table.
712 * For VSI_LUT, LUT size should be set to 64 bytes.
714 vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
715 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
716 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
721 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
722 ice_vsi_type_str(vsi->type));
728 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
729 * @ctxt: the VSI context being set
731 * This initializes a default VSI context for all sections except the Queues.
733 static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
737 memset(&ctxt->info, 0, sizeof(ctxt->info));
738 /* VSI's should be allocated from shared pool */
739 ctxt->alloc_from_pool = true;
740 /* Src pruning enabled by default */
741 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
742 /* Traffic from VSI can be sent to LAN */
743 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
744 /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
745 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
746 * packets untagged/tagged.
748 ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
749 ICE_AQ_VSI_VLAN_MODE_M) >>
750 ICE_AQ_VSI_VLAN_MODE_S);
751 /* Have 1:1 UP mapping for both ingress/egress tables */
752 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
753 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
754 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
755 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
756 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
757 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
758 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
759 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
760 ctxt->info.ingress_table = cpu_to_le32(table);
761 ctxt->info.egress_table = cpu_to_le32(table);
762 /* Have 1:1 UP mapping for outer to inner UP table */
763 ctxt->info.outer_up_table = cpu_to_le32(table);
764 /* No Outer tag support outer_tag_flags remains to zero */
768 * ice_vsi_setup_q_map - Setup a VSI queue map
769 * @vsi: the VSI being configured
770 * @ctxt: VSI context structure
772 static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
774 u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
775 u16 num_txq_per_tc, num_rxq_per_tc;
776 u16 qcount_tx = vsi->alloc_txq;
777 u16 qcount_rx = vsi->alloc_rxq;
778 bool ena_tc0 = false;
782 /* at least TC0 should be enabled by default */
783 if (vsi->tc_cfg.numtc) {
784 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
792 vsi->tc_cfg.ena_tc |= 1;
795 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
798 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
802 /* find the (rounded up) power-of-2 of qcount */
803 pow = (u16)order_base_2(num_rxq_per_tc);
805 /* TC mapping is a function of the number of Rx queues assigned to the
806 * VSI for each traffic class and the offset of these queues.
807 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
808 * queues allocated to TC0. No:of queues is a power-of-2.
810 * If TC is not enabled, the queue offset is set to 0, and allocate one
811 * queue, this way, traffic for the given TC will be sent to the default
814 * Setup number and offset of Rx queues for all TCs for the VSI
816 ice_for_each_traffic_class(i) {
817 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
818 /* TC is not enabled */
819 vsi->tc_cfg.tc_info[i].qoffset = 0;
820 vsi->tc_cfg.tc_info[i].qcount_rx = 1;
821 vsi->tc_cfg.tc_info[i].qcount_tx = 1;
822 vsi->tc_cfg.tc_info[i].netdev_tc = 0;
823 ctxt->info.tc_mapping[i] = 0;
828 vsi->tc_cfg.tc_info[i].qoffset = offset;
829 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
830 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
831 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
833 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
834 ICE_AQ_VSI_TC_Q_OFFSET_M) |
835 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
836 ICE_AQ_VSI_TC_Q_NUM_M);
837 offset += num_rxq_per_tc;
838 tx_count += num_txq_per_tc;
839 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
842 /* if offset is non-zero, means it is calculated correctly based on
843 * enabled TCs for a given VSI otherwise qcount_rx will always
844 * be correct and non-zero because it is based off - VSI's
845 * allocated Rx queues which is at least 1 (hence qcount_tx will be
849 vsi->num_rxq = offset;
851 vsi->num_rxq = num_rxq_per_tc;
853 vsi->num_txq = tx_count;
855 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
856 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
857 /* since there is a chance that num_rxq could have been changed
858 * in the above for loop, make num_txq equal to num_rxq.
860 vsi->num_txq = vsi->num_rxq;
863 /* Rx queue mapping */
864 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
865 /* q_mapping buffer holds the info for the first queue allocated for
866 * this VSI in the PF space and also the number of queues associated
869 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
870 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
874 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
875 * @ctxt: the VSI context being set
876 * @vsi: the VSI being configured
878 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
880 u8 dflt_q_group, dflt_q_prio;
881 u16 dflt_q, report_q, val;
883 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
884 vsi->type != ICE_VSI_VF)
887 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
888 ctxt->info.valid_sections |= cpu_to_le16(val);
894 /* enable flow director filtering/programming */
895 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
896 ctxt->info.fd_options = cpu_to_le16(val);
897 /* max of allocated flow director filters */
898 ctxt->info.max_fd_fltr_dedicated =
899 cpu_to_le16(vsi->num_gfltr);
900 /* max of shared flow director filters any VSI may program */
901 ctxt->info.max_fd_fltr_shared =
902 cpu_to_le16(vsi->num_bfltr);
903 /* default queue index within the VSI of the default FD */
904 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
905 ICE_AQ_VSI_FD_DEF_Q_M);
906 /* target queue or queue group to the FD filter */
907 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
908 ICE_AQ_VSI_FD_DEF_GRP_M);
909 ctxt->info.fd_def_q = cpu_to_le16(val);
910 /* queue index on which FD filter completion is reported */
911 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
912 ICE_AQ_VSI_FD_REPORT_Q_M);
913 /* priority of the default qindex action */
914 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
915 ICE_AQ_VSI_FD_DEF_PRIORITY_M);
916 ctxt->info.fd_report_opt = cpu_to_le16(val);
920 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
921 * @ctxt: the VSI context being set
922 * @vsi: the VSI being configured
924 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
926 u8 lut_type, hash_type;
931 dev = ice_pf_to_dev(pf);
935 /* PF VSI will inherit RSS instance of PF */
936 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
937 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
940 /* VF VSI will gets a small RSS table which is a VSI LUT type */
941 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
942 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
945 dev_dbg(dev, "Unsupported VSI type %s\n",
946 ice_vsi_type_str(vsi->type));
950 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
951 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
952 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
953 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
957 * ice_vsi_init - Create and initialize a VSI
958 * @vsi: the VSI being configured
959 * @init_vsi: is this call creating a VSI
961 * This initializes a VSI context depending on the VSI type to be added and
962 * passes it down to the add_vsi aq command to create a new VSI.
964 static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
966 struct ice_pf *pf = vsi->back;
967 struct ice_hw *hw = &pf->hw;
968 struct ice_vsi_ctx *ctxt;
972 dev = ice_pf_to_dev(pf);
973 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
981 ctxt->flags = ICE_AQ_VSI_TYPE_PF;
984 ctxt->flags = ICE_AQ_VSI_TYPE_VF;
985 /* VF number here is the absolute VF number (0-255) */
986 ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
993 ice_set_dflt_vsi_ctx(ctxt);
994 if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
995 ice_set_fd_vsi_ctx(ctxt, vsi);
996 /* if the switch is in VEB mode, allow VSI loopback */
997 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
998 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1000 /* Set LUT type and HASH type if RSS is enabled */
1001 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1002 vsi->type != ICE_VSI_CTRL) {
1003 ice_set_rss_vsi_ctx(ctxt, vsi);
1004 /* if updating VSI context, make sure to set valid_section:
1005 * to indicate which section of VSI context being updated
1008 ctxt->info.valid_sections |=
1009 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1012 ctxt->info.sw_id = vsi->port_info->sw_id;
1013 ice_vsi_setup_q_map(vsi, ctxt);
1014 if (!init_vsi) /* means VSI being updated */
1015 /* must to indicate which section of VSI context are
1018 ctxt->info.valid_sections |=
1019 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1021 /* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
1024 if (vsi->type == ICE_VSI_VF) {
1025 ctxt->info.valid_sections |=
1026 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1027 if (pf->vf[vsi->vf_id].spoofchk) {
1028 ctxt->info.sec_flags |=
1029 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1030 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1031 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1033 ctxt->info.sec_flags &=
1034 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1035 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1036 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1040 /* Allow control frames out of main VSI */
1041 if (vsi->type == ICE_VSI_PF) {
1042 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1043 ctxt->info.valid_sections |=
1044 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1048 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1050 dev_err(dev, "Add VSI failed, err %d\n", ret);
1055 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1057 dev_err(dev, "Update VSI failed, err %d\n", ret);
1063 /* keep context for update VSI operations */
1064 vsi->info = ctxt->info;
1066 /* record VSI number returned */
1067 vsi->vsi_num = ctxt->vsi_num;
1075 * ice_free_res - free a block of resources
1076 * @res: pointer to the resource
1077 * @index: starting index previously returned by ice_get_res
1078 * @id: identifier to track owner
1080 * Returns number of resources freed
1082 int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1087 if (!res || index >= res->end)
1090 id |= ICE_RES_VALID_BIT;
1091 for (i = index; i < res->end && res->list[i] == id; i++) {
1100 * ice_search_res - Search the tracker for a block of resources
1101 * @res: pointer to the resource
1102 * @needed: size of the block needed
1103 * @id: identifier to track owner
1105 * Returns the base item index of the block, or -ENOMEM for error
1107 static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1109 u16 start = 0, end = 0;
1111 if (needed > res->end)
1114 id |= ICE_RES_VALID_BIT;
1117 /* skip already allocated entries */
1118 if (res->list[end++] & ICE_RES_VALID_BIT) {
1120 if ((start + needed) > res->end)
1124 if (end == (start + needed)) {
1127 /* there was enough, so assign it to the requestor */
1129 res->list[i++] = id;
1133 } while (end < res->end);
1139 * ice_get_free_res_count - Get free count from a resource tracker
1140 * @res: Resource tracker instance
1142 static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1146 for (i = 0; i < res->end; i++)
1147 if (!(res->list[i] & ICE_RES_VALID_BIT))
1154 * ice_get_res - get a block of resources
1155 * @pf: board private structure
1156 * @res: pointer to the resource
1157 * @needed: size of the block needed
1158 * @id: identifier to track owner
1160 * Returns the base item index of the block, or negative for error
1163 ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1168 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1169 dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1170 needed, res->num_entries, id);
1174 return ice_search_res(res, needed, id);
1178 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1179 * @vsi: ptr to the VSI
1181 * This should only be called after ice_vsi_alloc() which allocates the
1182 * corresponding SW VSI structure and initializes num_queue_pairs for the
1183 * newly allocated VSI.
1185 * Returns 0 on success or negative on failure
1187 static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1189 struct ice_pf *pf = vsi->back;
1194 dev = ice_pf_to_dev(pf);
1195 /* SRIOV doesn't grab irq_tracker entries for each VSI */
1196 if (vsi->type == ICE_VSI_VF)
1199 if (vsi->base_vector) {
1200 dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1201 vsi->vsi_num, vsi->base_vector);
1205 num_q_vectors = vsi->num_q_vectors;
1206 /* reserve slots from OS requested IRQs */
1207 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1210 ice_for_each_vf(pf, i) {
1211 struct ice_vf *vf = &pf->vf[i];
1213 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1214 base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1218 if (i == pf->num_alloc_vfs)
1219 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1220 ICE_RES_VF_CTRL_VEC_ID);
1222 base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1227 dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1228 ice_get_free_res_count(pf->irq_tracker),
1229 ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1232 vsi->base_vector = (u16)base;
1233 pf->num_avail_sw_msix -= num_q_vectors;
1239 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1240 * @vsi: the VSI having rings deallocated
1242 static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1246 /* Avoid stale references by clearing map from vector to ring */
1247 if (vsi->q_vectors) {
1248 ice_for_each_q_vector(vsi, i) {
1249 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1252 q_vector->tx.ring = NULL;
1253 q_vector->rx.ring = NULL;
1258 if (vsi->tx_rings) {
1259 for (i = 0; i < vsi->alloc_txq; i++) {
1260 if (vsi->tx_rings[i]) {
1261 kfree_rcu(vsi->tx_rings[i], rcu);
1262 WRITE_ONCE(vsi->tx_rings[i], NULL);
1266 if (vsi->rx_rings) {
1267 for (i = 0; i < vsi->alloc_rxq; i++) {
1268 if (vsi->rx_rings[i]) {
1269 kfree_rcu(vsi->rx_rings[i], rcu);
1270 WRITE_ONCE(vsi->rx_rings[i], NULL);
1277 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1278 * @vsi: VSI which is having rings allocated
1280 static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1282 struct ice_pf *pf = vsi->back;
1286 dev = ice_pf_to_dev(pf);
1287 /* Allocate Tx rings */
1288 for (i = 0; i < vsi->alloc_txq; i++) {
1289 struct ice_ring *ring;
1291 /* allocate with kzalloc(), free with kfree_rcu() */
1292 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1298 ring->reg_idx = vsi->txq_map[i];
1299 ring->ring_active = false;
1301 ring->tx_tstamps = &pf->ptp.port.tx;
1303 ring->count = vsi->num_tx_desc;
1304 WRITE_ONCE(vsi->tx_rings[i], ring);
1307 /* Allocate Rx rings */
1308 for (i = 0; i < vsi->alloc_rxq; i++) {
1309 struct ice_ring *ring;
1311 /* allocate with kzalloc(), free with kfree_rcu() */
1312 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1317 ring->reg_idx = vsi->rxq_map[i];
1318 ring->ring_active = false;
1320 ring->netdev = vsi->netdev;
1322 ring->count = vsi->num_rx_desc;
1323 WRITE_ONCE(vsi->rx_rings[i], ring);
1329 ice_vsi_clear_rings(vsi);
1334 * ice_vsi_manage_rss_lut - disable/enable RSS
1335 * @vsi: the VSI being changed
1336 * @ena: boolean value indicating if this is an enable or disable request
1338 * In the event of disable request for RSS, this function will zero out RSS
1339 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1342 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1346 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1351 if (vsi->rss_lut_user)
1352 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1354 ice_fill_rss_lut(lut, vsi->rss_table_size,
1358 ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1363 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1364 * @vsi: VSI to be configured
1366 static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1368 struct ice_pf *pf = vsi->back;
1373 dev = ice_pf_to_dev(pf);
1374 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1376 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1380 if (vsi->rss_lut_user)
1381 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1383 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1385 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1387 dev_err(dev, "set_rss_lut failed, error %d\n", err);
1388 goto ice_vsi_cfg_rss_exit;
1391 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1394 goto ice_vsi_cfg_rss_exit;
1397 if (vsi->rss_hkey_user)
1398 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1400 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1402 err = ice_set_rss_key(vsi, key);
1404 dev_err(dev, "set_rss_key failed, error %d\n", err);
1407 ice_vsi_cfg_rss_exit:
1413 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1414 * @vsi: VSI to be configured
1416 * This function will only be called during the VF VSI setup. Upon successful
1417 * completion of package download, this function will configure default RSS
1418 * input sets for VF VSI.
1420 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1422 struct ice_pf *pf = vsi->back;
1423 enum ice_status status;
1426 dev = ice_pf_to_dev(pf);
1427 if (ice_is_safe_mode(pf)) {
1428 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1433 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1435 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1436 vsi->vsi_num, ice_stat_str(status));
1440 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1441 * @vsi: VSI to be configured
1443 * This function will only be called after successful download package call
1444 * during initialization of PF. Since the downloaded package will erase the
1445 * RSS section, this function will configure RSS input sets for different
1446 * flow types. The last profile added has the highest priority, therefore 2
1447 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1448 * (i.e. IPv4 src/dst TCP src/dst port).
1450 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1452 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1453 struct ice_pf *pf = vsi->back;
1454 struct ice_hw *hw = &pf->hw;
1455 enum ice_status status;
1458 dev = ice_pf_to_dev(pf);
1459 if (ice_is_safe_mode(pf)) {
1460 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1464 /* configure RSS for IPv4 with input set IP src/dst */
1465 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1466 ICE_FLOW_SEG_HDR_IPV4);
1468 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1469 vsi_num, ice_stat_str(status));
1471 /* configure RSS for IPv6 with input set IPv6 src/dst */
1472 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1473 ICE_FLOW_SEG_HDR_IPV6);
1475 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1476 vsi_num, ice_stat_str(status));
1478 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1479 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1480 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1482 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1483 vsi_num, ice_stat_str(status));
1485 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1486 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1487 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1489 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1490 vsi_num, ice_stat_str(status));
1492 /* configure RSS for sctp4 with input set IP src/dst */
1493 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1494 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1496 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1497 vsi_num, ice_stat_str(status));
1499 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1500 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1501 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1503 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1504 vsi_num, ice_stat_str(status));
1506 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1507 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1508 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1510 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1511 vsi_num, ice_stat_str(status));
1513 /* configure RSS for sctp6 with input set IPv6 src/dst */
1514 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1515 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1517 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1518 vsi_num, ice_stat_str(status));
1522 * ice_pf_state_is_nominal - checks the PF for nominal state
1523 * @pf: pointer to PF to check
1525 * Check the PF's state for a collection of bits that would indicate
1526 * the PF is in a state that would inhibit normal operation for
1527 * driver functionality.
1529 * Returns true if PF is in a nominal state, false otherwise
1531 bool ice_pf_state_is_nominal(struct ice_pf *pf)
1533 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1538 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1539 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1546 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1547 * @vsi: the VSI to be updated
1549 void ice_update_eth_stats(struct ice_vsi *vsi)
1551 struct ice_eth_stats *prev_es, *cur_es;
1552 struct ice_hw *hw = &vsi->back->hw;
1553 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
1555 prev_es = &vsi->eth_stats_prev;
1556 cur_es = &vsi->eth_stats;
1558 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1559 &prev_es->rx_bytes, &cur_es->rx_bytes);
1561 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1562 &prev_es->rx_unicast, &cur_es->rx_unicast);
1564 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1565 &prev_es->rx_multicast, &cur_es->rx_multicast);
1567 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1568 &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1570 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1571 &prev_es->rx_discards, &cur_es->rx_discards);
1573 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1574 &prev_es->tx_bytes, &cur_es->tx_bytes);
1576 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1577 &prev_es->tx_unicast, &cur_es->tx_unicast);
1579 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1580 &prev_es->tx_multicast, &cur_es->tx_multicast);
1582 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1583 &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1585 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1586 &prev_es->tx_errors, &cur_es->tx_errors);
1588 vsi->stat_offsets_loaded = true;
1592 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1593 * @vsi: the VSI being configured
1594 * @vid: VLAN ID to be added
1595 * @action: filter action to be performed on match
1598 ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1600 struct ice_pf *pf = vsi->back;
1604 dev = ice_pf_to_dev(pf);
1606 if (!ice_fltr_add_vlan(vsi, vid, action)) {
1610 dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1618 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1619 * @vsi: the VSI being configured
1620 * @vid: VLAN ID to be removed
1622 * Returns 0 on success and negative on failure
1624 int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1626 struct ice_pf *pf = vsi->back;
1627 enum ice_status status;
1631 dev = ice_pf_to_dev(pf);
1633 status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1636 } else if (status == ICE_ERR_DOES_NOT_EXIST) {
1637 dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1638 vid, vsi->vsi_num, ice_stat_str(status));
1640 dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1641 vid, vsi->vsi_num, ice_stat_str(status));
1649 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1652 void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1654 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1655 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1656 vsi->rx_buf_len = ICE_RXBUF_2048;
1657 #if (PAGE_SIZE < 8192)
1658 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1659 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1660 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1661 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1664 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1665 #if (PAGE_SIZE < 8192)
1666 vsi->rx_buf_len = ICE_RXBUF_3072;
1668 vsi->rx_buf_len = ICE_RXBUF_2048;
1674 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1676 * @pf_q: index of the Rx queue in the PF's queue space
1677 * @rxdid: flexible descriptor RXDID
1678 * @prio: priority for the RXDID for this queue
1679 * @ena_ts: true to enable timestamp and false to disable timestamp
1682 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1685 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1687 /* clear any previous values */
1688 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1689 QRXFLXP_CNTXT_RXDID_PRIO_M |
1690 QRXFLXP_CNTXT_TS_M);
1692 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1693 QRXFLXP_CNTXT_RXDID_IDX_M;
1695 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1696 QRXFLXP_CNTXT_RXDID_PRIO_M;
1699 /* Enable TimeSync on this queue */
1700 regval |= QRXFLXP_CNTXT_TS_M;
1702 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1705 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1707 if (q_idx >= vsi->num_rxq)
1710 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1713 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_ring **tx_rings, u16 q_idx)
1715 struct ice_aqc_add_tx_qgrp *qg_buf;
1718 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1721 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1725 qg_buf->num_txqs = 1;
1727 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1733 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1734 * @vsi: the VSI being configured
1736 * Return 0 on success and a negative value on error
1737 * Configure the Rx VSI for operation.
1739 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1743 if (vsi->type == ICE_VSI_VF)
1746 ice_vsi_cfg_frame_size(vsi);
1748 /* set up individual rings */
1749 ice_for_each_rxq(vsi, i) {
1750 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1760 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1761 * @vsi: the VSI being configured
1762 * @rings: Tx ring array to be configured
1763 * @count: number of Tx ring array elements
1765 * Return 0 on success and a negative value on error
1766 * Configure the Tx VSI for operation.
1769 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, u16 count)
1771 struct ice_aqc_add_tx_qgrp *qg_buf;
1775 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1779 qg_buf->num_txqs = 1;
1781 for (q_idx = 0; q_idx < count; q_idx++) {
1782 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1793 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1794 * @vsi: the VSI being configured
1796 * Return 0 on success and a negative value on error
1797 * Configure the Tx VSI for operation.
1799 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1801 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1805 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1806 * @vsi: the VSI being configured
1808 * Return 0 on success and a negative value on error
1809 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1811 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1816 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1820 for (i = 0; i < vsi->num_xdp_txq; i++)
1821 vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1827 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1828 * @intrl: interrupt rate limit in usecs
1829 * @gran: interrupt rate limit granularity in usecs
1831 * This function converts a decimal interrupt rate limit in usecs to the format
1832 * expected by firmware.
1834 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1836 u32 val = intrl / gran;
1839 return val | GLINT_RATE_INTRL_ENA_M;
1844 * ice_write_intrl - write throttle rate limit to interrupt specific register
1845 * @q_vector: pointer to interrupt specific structure
1846 * @intrl: throttle rate limit in microseconds to write
1848 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1850 struct ice_hw *hw = &q_vector->vsi->back->hw;
1852 wr32(hw, GLINT_RATE(q_vector->reg_idx),
1853 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1857 * __ice_write_itr - write throttle rate to register
1858 * @q_vector: pointer to interrupt data structure
1859 * @rc: pointer to ring container
1860 * @itr: throttle rate in microseconds to write
1862 static void __ice_write_itr(struct ice_q_vector *q_vector,
1863 struct ice_ring_container *rc, u16 itr)
1865 struct ice_hw *hw = &q_vector->vsi->back->hw;
1867 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1868 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1872 * ice_write_itr - write throttle rate to queue specific register
1873 * @rc: pointer to ring container
1874 * @itr: throttle rate in microseconds to write
1876 void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1878 struct ice_q_vector *q_vector;
1883 q_vector = rc->ring->q_vector;
1885 __ice_write_itr(q_vector, rc, itr);
1889 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1890 * @vsi: the VSI being configured
1892 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1895 void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1897 struct ice_pf *pf = vsi->back;
1898 struct ice_hw *hw = &pf->hw;
1899 u16 txq = 0, rxq = 0;
1902 for (i = 0; i < vsi->num_q_vectors; i++) {
1903 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1904 u16 reg_idx = q_vector->reg_idx;
1906 ice_cfg_itr(hw, q_vector);
1908 /* Both Transmit Queue Interrupt Cause Control register
1909 * and Receive Queue Interrupt Cause control register
1910 * expects MSIX_INDX field to be the vector index
1911 * within the function space and not the absolute
1912 * vector index across PF or across device.
1913 * For SR-IOV VF VSIs queue vector index always starts
1914 * with 1 since first vector index(0) is used for OICR
1915 * in VF space. Since VMDq and other PF VSIs are within
1916 * the PF function space, use the vector index that is
1917 * tracked for this PF.
1919 for (q = 0; q < q_vector->num_ring_tx; q++) {
1920 ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1921 q_vector->tx.itr_idx);
1925 for (q = 0; q < q_vector->num_ring_rx; q++) {
1926 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1927 q_vector->rx.itr_idx);
1934 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1935 * @vsi: the VSI being changed
1937 int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1939 struct ice_hw *hw = &vsi->back->hw;
1940 struct ice_vsi_ctx *ctxt;
1941 enum ice_status status;
1944 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1948 /* Here we are configuring the VSI to let the driver add VLAN tags by
1949 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1950 * insertion happens in the Tx hot path, in ice_tx_map.
1952 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1954 /* Preserve existing VLAN strip setting */
1955 ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1956 ICE_AQ_VSI_VLAN_EMOD_M);
1958 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1960 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1962 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1963 ice_stat_str(status),
1964 ice_aq_str(hw->adminq.sq_last_status));
1969 vsi->info.vlan_flags = ctxt->info.vlan_flags;
1976 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1977 * @vsi: the VSI being changed
1978 * @ena: boolean value indicating if this is a enable or disable request
1980 int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1982 struct ice_hw *hw = &vsi->back->hw;
1983 struct ice_vsi_ctx *ctxt;
1984 enum ice_status status;
1987 /* do not allow modifying VLAN stripping when a port VLAN is configured
1993 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1997 /* Here we are configuring what the VSI should do with the VLAN tag in
1998 * the Rx packet. We can either leave the tag in the packet or put it in
1999 * the Rx descriptor.
2002 /* Strip VLAN tag from Rx packet and put it in the desc */
2003 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
2005 /* Disable stripping. Leave tag in packet */
2006 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2008 /* Allow all packets untagged/tagged */
2009 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
2011 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2013 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2015 dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
2016 ena, ice_stat_str(status),
2017 ice_aq_str(hw->adminq.sq_last_status));
2022 vsi->info.vlan_flags = ctxt->info.vlan_flags;
2029 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2030 * @vsi: the VSI whose rings are to be enabled
2032 * Returns 0 on success and a negative value on error
2034 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2036 return ice_vsi_ctrl_all_rx_rings(vsi, true);
2040 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2041 * @vsi: the VSI whose rings are to be disabled
2043 * Returns 0 on success and a negative value on error
2045 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2047 return ice_vsi_ctrl_all_rx_rings(vsi, false);
2051 * ice_vsi_stop_tx_rings - Disable Tx rings
2052 * @vsi: the VSI being configured
2053 * @rst_src: reset source
2054 * @rel_vmvf_num: Relative ID of VF/VM
2055 * @rings: Tx ring array to be stopped
2056 * @count: number of Tx ring array elements
2059 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2060 u16 rel_vmvf_num, struct ice_ring **rings, u16 count)
2064 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2067 for (q_idx = 0; q_idx < count; q_idx++) {
2068 struct ice_txq_meta txq_meta = { };
2071 if (!rings || !rings[q_idx])
2074 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2075 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2076 rings[q_idx], &txq_meta);
2086 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2087 * @vsi: the VSI being configured
2088 * @rst_src: reset source
2089 * @rel_vmvf_num: Relative ID of VF/VM
2092 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2095 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2099 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2100 * @vsi: the VSI being configured
2102 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2104 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2108 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2109 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2111 * returns true if Rx VLAN pruning is enabled and false otherwise.
2113 bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2118 return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2122 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2123 * @vsi: VSI to enable or disable VLAN pruning on
2124 * @ena: set to true to enable VLAN pruning and false to disable it
2125 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2127 * returns 0 if VSI is updated, negative otherwise
2129 int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2131 struct ice_vsi_ctx *ctxt;
2138 /* Don't enable VLAN pruning if the netdev is currently in promiscuous
2139 * mode. VLAN pruning will be enabled when the interface exits
2140 * promiscuous mode if any VLAN filters are active.
2142 if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2146 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2150 ctxt->info = vsi->info;
2153 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2155 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2158 ctxt->info.valid_sections =
2159 cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2161 status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2163 netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2164 ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2165 ice_stat_str(status),
2166 ice_aq_str(pf->hw.adminq.sq_last_status));
2170 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2180 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2182 struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2184 vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2185 vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2189 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2190 * @vsi: VSI to set the q_vectors register index on
2193 ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2197 if (!vsi || !vsi->q_vectors)
2200 ice_for_each_q_vector(vsi, i) {
2201 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2204 dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2209 if (vsi->type == ICE_VSI_VF) {
2210 struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2212 q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2215 q_vector->v_idx + vsi->base_vector;
2222 ice_for_each_q_vector(vsi, i) {
2223 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2226 q_vector->reg_idx = 0;
2233 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2234 * @vsi: the VSI being configured
2235 * @tx: bool to determine Tx or Rx rule
2236 * @create: bool to determine create or remove Rule
2238 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2240 enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2241 enum ice_sw_fwd_act_type act);
2242 struct ice_pf *pf = vsi->back;
2243 enum ice_status status;
2246 dev = ice_pf_to_dev(pf);
2247 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2250 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2253 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2254 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2257 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2263 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2264 create ? "adding" : "removing", tx ? "TX" : "RX",
2265 vsi->vsi_num, ice_stat_str(status));
2269 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2270 * @vsi: pointer to the VSI
2272 * This function will allocate new scheduler aggregator now if needed and will
2273 * move specified VSI into it.
2275 static void ice_set_agg_vsi(struct ice_vsi *vsi)
2277 struct device *dev = ice_pf_to_dev(vsi->back);
2278 struct ice_agg_node *agg_node_iter = NULL;
2279 u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2280 struct ice_agg_node *agg_node = NULL;
2281 int node_offset, max_agg_nodes = 0;
2282 struct ice_port_info *port_info;
2283 struct ice_pf *pf = vsi->back;
2284 u32 agg_node_id_start = 0;
2285 enum ice_status status;
2287 /* create (as needed) scheduler aggregator node and move VSI into
2288 * corresponding aggregator node
2289 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2290 * - VF aggregator nodes will contain VF VSI
2292 port_info = pf->hw.port_info;
2296 switch (vsi->type) {
2300 max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2301 agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2302 agg_node_iter = &pf->pf_agg_node[0];
2305 /* user can create 'n' VFs on a given PF, but since max children
2306 * per aggregator node can be only 64. Following code handles
2307 * aggregator(s) for VF VSIs, either selects a agg_node which
2308 * was already created provided num_vsis < 64, otherwise
2309 * select next available node, which will be created
2311 max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2312 agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2313 agg_node_iter = &pf->vf_agg_node[0];
2316 /* other VSI type, handle later if needed */
2317 dev_dbg(dev, "unexpected VSI type %s\n",
2318 ice_vsi_type_str(vsi->type));
2322 /* find the appropriate aggregator node */
2323 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2324 /* see if we can find space in previously created
2325 * node if num_vsis < 64, otherwise skip
2327 if (agg_node_iter->num_vsis &&
2328 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2333 if (agg_node_iter->valid &&
2334 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2335 agg_id = agg_node_iter->agg_id;
2336 agg_node = agg_node_iter;
2340 /* find unclaimed agg_id */
2341 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2342 agg_id = node_offset + agg_node_id_start;
2343 agg_node = agg_node_iter;
2346 /* move to next agg_node */
2353 /* if selected aggregator node was not created, create it */
2354 if (!agg_node->valid) {
2355 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2356 (u8)vsi->tc_cfg.ena_tc);
2358 dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2362 /* aggregator node is created, store the neeeded info */
2363 agg_node->valid = true;
2364 agg_node->agg_id = agg_id;
2367 /* move VSI to corresponding aggregator node */
2368 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2369 (u8)vsi->tc_cfg.ena_tc);
2371 dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2376 /* keep active children count for aggregator node */
2377 agg_node->num_vsis++;
2379 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2380 * to aggregator node
2382 vsi->agg_node = agg_node;
2383 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2384 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2385 vsi->agg_node->num_vsis);
2389 * ice_vsi_setup - Set up a VSI by a given type
2390 * @pf: board private structure
2391 * @pi: pointer to the port_info instance
2392 * @vsi_type: VSI type
2393 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2394 * used only for ICE_VSI_VF VSI type. For other VSI types, should
2395 * fill-in ICE_INVAL_VFID as input.
2397 * This allocates the sw VSI structure and its queue resources.
2399 * Returns pointer to the successfully allocated and configured VSI sw struct on
2400 * success, NULL on failure.
2403 ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2404 enum ice_vsi_type vsi_type, u16 vf_id)
2406 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2407 struct device *dev = ice_pf_to_dev(pf);
2408 enum ice_status status;
2409 struct ice_vsi *vsi;
2412 if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2413 vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2415 vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2418 dev_err(dev, "could not allocate VSI\n");
2422 vsi->port_info = pi;
2423 vsi->vsw = pf->first_sw;
2424 if (vsi->type == ICE_VSI_PF)
2425 vsi->ethtype = ETH_P_PAUSE;
2427 if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2430 ice_alloc_fd_res(vsi);
2432 if (ice_vsi_get_qs(vsi)) {
2433 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2435 goto unroll_vsi_alloc;
2438 /* set RSS capabilities */
2439 ice_vsi_set_rss_params(vsi);
2441 /* set TC configuration */
2442 ice_vsi_set_tc_cfg(vsi);
2444 /* create the VSI */
2445 ret = ice_vsi_init(vsi, true);
2449 switch (vsi->type) {
2452 ret = ice_vsi_alloc_q_vectors(vsi);
2454 goto unroll_vsi_init;
2456 ret = ice_vsi_setup_vector_base(vsi);
2458 goto unroll_alloc_q_vector;
2460 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2462 goto unroll_vector_base;
2464 ret = ice_vsi_alloc_rings(vsi);
2466 goto unroll_vector_base;
2468 /* Always add VLAN ID 0 switch rule by default. This is needed
2469 * in order to allow all untagged and 0 tagged priority traffic
2470 * if Rx VLAN pruning is enabled. Also there are cases where we
2471 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2472 * so this handles those cases (i.e. adding the PF to a bridge
2473 * without the 8021q module loaded).
2475 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2477 goto unroll_clear_rings;
2479 ice_vsi_map_rings_to_vectors(vsi);
2481 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2482 if (vsi->type != ICE_VSI_CTRL)
2483 /* Do not exit if configuring RSS had an issue, at
2484 * least receive traffic on first queue. Hence no
2485 * need to capture return value
2487 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2488 ice_vsi_cfg_rss_lut_key(vsi);
2489 ice_vsi_set_rss_flow_fld(vsi);
2494 /* VF driver will take care of creating netdev for this type and
2495 * map queues to vectors through Virtchnl, PF driver only
2496 * creates a VSI and corresponding structures for bookkeeping
2499 ret = ice_vsi_alloc_q_vectors(vsi);
2501 goto unroll_vsi_init;
2503 ret = ice_vsi_alloc_rings(vsi);
2505 goto unroll_alloc_q_vector;
2507 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2509 goto unroll_vector_base;
2511 /* Do not exit if configuring RSS had an issue, at least
2512 * receive traffic on first queue. Hence no need to capture
2515 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2516 ice_vsi_cfg_rss_lut_key(vsi);
2517 ice_vsi_set_vf_rss_flow_fld(vsi);
2521 ret = ice_vsi_alloc_rings(vsi);
2523 goto unroll_vsi_init;
2526 /* clean up the resources and exit */
2527 goto unroll_vsi_init;
2530 /* configure VSI nodes based on number of queues and TC's */
2531 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2532 max_txqs[i] = vsi->alloc_txq;
2534 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2537 dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2538 vsi->vsi_num, ice_stat_str(status));
2539 goto unroll_clear_rings;
2542 /* Add switch rule to drop all Tx Flow Control Frames, of look up
2543 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2544 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2545 * The rule is added once for PF VSI in order to create appropriate
2546 * recipe, since VSI/VSI list is ignored with drop action...
2547 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to
2548 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2549 * settings in the HW.
2551 if (!ice_is_safe_mode(pf))
2552 if (vsi->type == ICE_VSI_PF) {
2553 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2555 ice_cfg_sw_lldp(vsi, true, true);
2559 ice_set_agg_vsi(vsi);
2563 ice_vsi_clear_rings(vsi);
2565 /* reclaim SW interrupts back to the common pool */
2566 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2567 pf->num_avail_sw_msix += vsi->num_q_vectors;
2568 unroll_alloc_q_vector:
2569 ice_vsi_free_q_vectors(vsi);
2571 ice_vsi_delete(vsi);
2573 ice_vsi_put_qs(vsi);
2575 if (vsi_type == ICE_VSI_VF)
2576 ice_enable_lag(pf->lag);
2583 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2584 * @vsi: the VSI being cleaned up
2586 static void ice_vsi_release_msix(struct ice_vsi *vsi)
2588 struct ice_pf *pf = vsi->back;
2589 struct ice_hw *hw = &pf->hw;
2594 for (i = 0; i < vsi->num_q_vectors; i++) {
2595 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2597 ice_write_intrl(q_vector, 0);
2598 for (q = 0; q < q_vector->num_ring_tx; q++) {
2599 ice_write_itr(&q_vector->tx, 0);
2600 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2601 if (ice_is_xdp_ena_vsi(vsi)) {
2602 u32 xdp_txq = txq + vsi->num_xdp_txq;
2604 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2609 for (q = 0; q < q_vector->num_ring_rx; q++) {
2610 ice_write_itr(&q_vector->rx, 0);
2611 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2620 * ice_vsi_free_irq - Free the IRQ association with the OS
2621 * @vsi: the VSI being configured
2623 void ice_vsi_free_irq(struct ice_vsi *vsi)
2625 struct ice_pf *pf = vsi->back;
2626 int base = vsi->base_vector;
2629 if (!vsi->q_vectors || !vsi->irqs_ready)
2632 ice_vsi_release_msix(vsi);
2633 if (vsi->type == ICE_VSI_VF)
2636 vsi->irqs_ready = false;
2637 ice_for_each_q_vector(vsi, i) {
2638 u16 vector = i + base;
2641 irq_num = pf->msix_entries[vector].vector;
2643 /* free only the irqs that were actually requested */
2644 if (!vsi->q_vectors[i] ||
2645 !(vsi->q_vectors[i]->num_ring_tx ||
2646 vsi->q_vectors[i]->num_ring_rx))
2649 /* clear the affinity notifier in the IRQ descriptor */
2650 irq_set_affinity_notifier(irq_num, NULL);
2652 /* clear the affinity_mask in the IRQ descriptor */
2653 irq_set_affinity_hint(irq_num, NULL);
2654 synchronize_irq(irq_num);
2655 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2660 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2661 * @vsi: the VSI having resources freed
2663 void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2670 ice_for_each_txq(vsi, i)
2671 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2672 ice_free_tx_ring(vsi->tx_rings[i]);
2676 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2677 * @vsi: the VSI having resources freed
2679 void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2686 ice_for_each_rxq(vsi, i)
2687 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2688 ice_free_rx_ring(vsi->rx_rings[i]);
2692 * ice_vsi_close - Shut down a VSI
2693 * @vsi: the VSI being shut down
2695 void ice_vsi_close(struct ice_vsi *vsi)
2697 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2700 ice_vsi_free_irq(vsi);
2701 ice_vsi_free_tx_rings(vsi);
2702 ice_vsi_free_rx_rings(vsi);
2706 * ice_ena_vsi - resume a VSI
2707 * @vsi: the VSI being resume
2708 * @locked: is the rtnl_lock already held
2710 int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2714 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2717 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2719 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
2720 if (netif_running(vsi->netdev)) {
2724 err = ice_open_internal(vsi->netdev);
2729 } else if (vsi->type == ICE_VSI_CTRL) {
2730 err = ice_vsi_open_ctrl(vsi);
2737 * ice_dis_vsi - pause a VSI
2738 * @vsi: the VSI being paused
2739 * @locked: is the rtnl_lock already held
2741 void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2743 if (test_bit(ICE_VSI_DOWN, vsi->state))
2746 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2748 if (vsi->type == ICE_VSI_PF && vsi->netdev) {
2749 if (netif_running(vsi->netdev)) {
2760 } else if (vsi->type == ICE_VSI_CTRL) {
2766 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2767 * @vsi: the VSI being un-configured
2769 void ice_vsi_dis_irq(struct ice_vsi *vsi)
2771 int base = vsi->base_vector;
2772 struct ice_pf *pf = vsi->back;
2773 struct ice_hw *hw = &pf->hw;
2777 /* disable interrupt causation from each queue */
2778 if (vsi->tx_rings) {
2779 ice_for_each_txq(vsi, i) {
2780 if (vsi->tx_rings[i]) {
2783 reg = vsi->tx_rings[i]->reg_idx;
2784 val = rd32(hw, QINT_TQCTL(reg));
2785 val &= ~QINT_TQCTL_CAUSE_ENA_M;
2786 wr32(hw, QINT_TQCTL(reg), val);
2791 if (vsi->rx_rings) {
2792 ice_for_each_rxq(vsi, i) {
2793 if (vsi->rx_rings[i]) {
2796 reg = vsi->rx_rings[i]->reg_idx;
2797 val = rd32(hw, QINT_RQCTL(reg));
2798 val &= ~QINT_RQCTL_CAUSE_ENA_M;
2799 wr32(hw, QINT_RQCTL(reg), val);
2804 /* disable each interrupt */
2805 ice_for_each_q_vector(vsi, i) {
2806 if (!vsi->q_vectors[i])
2808 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2813 /* don't call synchronize_irq() for VF's from the host */
2814 if (vsi->type == ICE_VSI_VF)
2817 ice_for_each_q_vector(vsi, i)
2818 synchronize_irq(pf->msix_entries[i + base].vector);
2822 * ice_napi_del - Remove NAPI handler for the VSI
2823 * @vsi: VSI for which NAPI handler is to be removed
2825 void ice_napi_del(struct ice_vsi *vsi)
2832 ice_for_each_q_vector(vsi, v_idx)
2833 netif_napi_del(&vsi->q_vectors[v_idx]->napi);
2837 * ice_vsi_release - Delete a VSI and free its resources
2838 * @vsi: the VSI being removed
2840 * Returns 0 on success or < 0 on error
2842 int ice_vsi_release(struct ice_vsi *vsi)
2850 /* do not unregister while driver is in the reset recovery pending
2851 * state. Since reset/rebuild happens through PF service task workqueue,
2852 * it's not a good idea to unregister netdev that is associated to the
2853 * PF that is running the work queue items currently. This is done to
2854 * avoid check_flush_dependency() warning on this wq
2856 if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2857 (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2858 unregister_netdev(vsi->netdev);
2859 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2862 ice_devlink_destroy_port(vsi);
2864 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2867 /* Disable VSI and free resources */
2868 if (vsi->type != ICE_VSI_LB)
2869 ice_vsi_dis_irq(vsi);
2872 /* SR-IOV determines needed MSIX resources all at once instead of per
2873 * VSI since when VFs are spawned we know how many VFs there are and how
2874 * many interrupts each VF needs. SR-IOV MSIX resources are also
2875 * cleared in the same manner.
2877 if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2880 ice_for_each_vf(pf, i) {
2881 struct ice_vf *vf = &pf->vf[i];
2883 if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2886 if (i == pf->num_alloc_vfs) {
2887 /* No other VFs left that have control VSI, reclaim SW
2888 * interrupts back to the common pool
2890 ice_free_res(pf->irq_tracker, vsi->base_vector,
2891 ICE_RES_VF_CTRL_VEC_ID);
2892 pf->num_avail_sw_msix += vsi->num_q_vectors;
2894 } else if (vsi->type != ICE_VSI_VF) {
2895 /* reclaim SW interrupts back to the common pool */
2896 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2897 pf->num_avail_sw_msix += vsi->num_q_vectors;
2900 if (!ice_is_safe_mode(pf)) {
2901 if (vsi->type == ICE_VSI_PF) {
2902 ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2904 ice_cfg_sw_lldp(vsi, true, false);
2905 /* The Rx rule will only exist to remove if the LLDP FW
2906 * engine is currently stopped
2908 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2909 ice_cfg_sw_lldp(vsi, false, false);
2913 ice_fltr_remove_all(vsi);
2914 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2915 ice_vsi_delete(vsi);
2916 ice_vsi_free_q_vectors(vsi);
2919 if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2920 unregister_netdev(vsi->netdev);
2921 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2923 if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2924 free_netdev(vsi->netdev);
2926 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2930 if (vsi->type == ICE_VSI_VF &&
2931 vsi->agg_node && vsi->agg_node->valid)
2932 vsi->agg_node->num_vsis--;
2933 ice_vsi_clear_rings(vsi);
2935 ice_vsi_put_qs(vsi);
2937 /* retain SW VSI data structure since it is needed to unregister and
2938 * free VSI netdev when PF is not in reset recovery pending state,\
2939 * for ex: during rmmod.
2941 if (!ice_is_reset_in_progress(pf->state))
2948 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2949 * @vsi: VSI connected with q_vectors
2950 * @coalesce: array of struct with stored coalesce
2952 * Returns array size.
2955 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2956 struct ice_coalesce_stored *coalesce)
2960 ice_for_each_q_vector(vsi, i) {
2961 struct ice_q_vector *q_vector = vsi->q_vectors[i];
2963 coalesce[i].itr_tx = q_vector->tx.itr_setting;
2964 coalesce[i].itr_rx = q_vector->rx.itr_setting;
2965 coalesce[i].intrl = q_vector->intrl;
2967 if (i < vsi->num_txq)
2968 coalesce[i].tx_valid = true;
2969 if (i < vsi->num_rxq)
2970 coalesce[i].rx_valid = true;
2973 return vsi->num_q_vectors;
2977 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2978 * @vsi: VSI connected with q_vectors
2979 * @coalesce: pointer to array of struct with stored coalesce
2980 * @size: size of coalesce array
2982 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2983 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2987 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2988 struct ice_coalesce_stored *coalesce, int size)
2990 struct ice_ring_container *rc;
2993 if ((size && !coalesce) || !vsi)
2996 /* There are a couple of cases that have to be handled here:
2997 * 1. The case where the number of queue vectors stays the same, but
2998 * the number of Tx or Rx rings changes (the first for loop)
2999 * 2. The case where the number of queue vectors increased (the
3002 for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3003 /* There are 2 cases to handle here and they are the same for
3005 * if the entry was valid previously (coalesce[i].[tr]x_valid
3006 * and the loop variable is less than the number of rings
3007 * allocated, then write the previous values
3009 * if the entry was not valid previously, but the number of
3010 * rings is less than are allocated (this means the number of
3011 * rings increased from previously), then write out the
3012 * values in the first element
3014 * Also, always write the ITR, even if in ITR_IS_DYNAMIC
3015 * as there is no harm because the dynamic algorithm
3016 * will just overwrite.
3018 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3019 rc = &vsi->q_vectors[i]->rx;
3020 rc->itr_setting = coalesce[i].itr_rx;
3021 ice_write_itr(rc, rc->itr_setting);
3022 } else if (i < vsi->alloc_rxq) {
3023 rc = &vsi->q_vectors[i]->rx;
3024 rc->itr_setting = coalesce[0].itr_rx;
3025 ice_write_itr(rc, rc->itr_setting);
3028 if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3029 rc = &vsi->q_vectors[i]->tx;
3030 rc->itr_setting = coalesce[i].itr_tx;
3031 ice_write_itr(rc, rc->itr_setting);
3032 } else if (i < vsi->alloc_txq) {
3033 rc = &vsi->q_vectors[i]->tx;
3034 rc->itr_setting = coalesce[0].itr_tx;
3035 ice_write_itr(rc, rc->itr_setting);
3038 vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3039 ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
3042 /* the number of queue vectors increased so write whatever is in
3045 for (; i < vsi->num_q_vectors; i++) {
3047 rc = &vsi->q_vectors[i]->tx;
3048 rc->itr_setting = coalesce[0].itr_tx;
3049 ice_write_itr(rc, rc->itr_setting);
3052 rc = &vsi->q_vectors[i]->rx;
3053 rc->itr_setting = coalesce[0].itr_rx;
3054 ice_write_itr(rc, rc->itr_setting);
3056 vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3057 ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3062 * ice_vsi_rebuild - Rebuild VSI after reset
3063 * @vsi: VSI to be rebuild
3064 * @init_vsi: is this an initialization or a reconfigure of the VSI
3066 * Returns 0 on success and negative value on failure
3068 int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3070 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3071 struct ice_coalesce_stored *coalesce;
3072 int prev_num_q_vectors = 0;
3073 struct ice_vf *vf = NULL;
3074 enum ice_vsi_type vtype;
3075 enum ice_status status;
3084 if (vtype == ICE_VSI_VF)
3085 vf = &pf->vf[vsi->vf_id];
3087 coalesce = kcalloc(vsi->num_q_vectors,
3088 sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3092 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
3094 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3095 ice_vsi_free_q_vectors(vsi);
3097 /* SR-IOV determines needed MSIX resources all at once instead of per
3098 * VSI since when VFs are spawned we know how many VFs there are and how
3099 * many interrupts each VF needs. SR-IOV MSIX resources are also
3100 * cleared in the same manner.
3102 if (vtype != ICE_VSI_VF) {
3103 /* reclaim SW interrupts back to the common pool */
3104 ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3105 pf->num_avail_sw_msix += vsi->num_q_vectors;
3106 vsi->base_vector = 0;
3109 if (ice_is_xdp_ena_vsi(vsi))
3110 /* return value check can be skipped here, it always returns
3111 * 0 if reset is in progress
3113 ice_destroy_xdp_rings(vsi);
3114 ice_vsi_put_qs(vsi);
3115 ice_vsi_clear_rings(vsi);
3116 ice_vsi_free_arrays(vsi);
3117 if (vtype == ICE_VSI_VF)
3118 ice_vsi_set_num_qs(vsi, vf->vf_id);
3120 ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3122 ret = ice_vsi_alloc_arrays(vsi);
3126 ice_vsi_get_qs(vsi);
3128 ice_alloc_fd_res(vsi);
3129 ice_vsi_set_tc_cfg(vsi);
3131 /* Initialize VSI struct elements and create VSI in FW */
3132 ret = ice_vsi_init(vsi, init_vsi);
3139 ret = ice_vsi_alloc_q_vectors(vsi);
3143 ret = ice_vsi_setup_vector_base(vsi);
3147 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3151 ret = ice_vsi_alloc_rings(vsi);
3155 ice_vsi_map_rings_to_vectors(vsi);
3156 if (ice_is_xdp_ena_vsi(vsi)) {
3157 vsi->num_xdp_txq = vsi->alloc_rxq;
3158 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3162 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3163 if (vtype != ICE_VSI_CTRL)
3164 /* Do not exit if configuring RSS had an issue, at
3165 * least receive traffic on first queue. Hence no
3166 * need to capture return value
3168 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3169 ice_vsi_cfg_rss_lut_key(vsi);
3172 ret = ice_vsi_alloc_q_vectors(vsi);
3176 ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3180 ret = ice_vsi_alloc_rings(vsi);
3189 /* configure VSI nodes based on number of queues and TC's */
3190 for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3191 max_txqs[i] = vsi->alloc_txq;
3193 if (ice_is_xdp_ena_vsi(vsi))
3194 max_txqs[i] += vsi->num_xdp_txq;
3197 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3200 dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3201 vsi->vsi_num, ice_stat_str(status));
3206 return ice_schedule_reset(pf, ICE_RESET_PFR);
3209 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3215 ice_vsi_free_q_vectors(vsi);
3218 vsi->current_netdev_flags = 0;
3219 unregister_netdev(vsi->netdev);
3220 free_netdev(vsi->netdev);
3225 set_bit(ICE_RESET_FAILED, pf->state);
3231 * ice_is_reset_in_progress - check for a reset in progress
3232 * @state: PF state field
3234 bool ice_is_reset_in_progress(unsigned long *state)
3236 return test_bit(ICE_RESET_OICR_RECV, state) ||
3237 test_bit(ICE_PFR_REQ, state) ||
3238 test_bit(ICE_CORER_REQ, state) ||
3239 test_bit(ICE_GLOBR_REQ, state);
3243 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3244 * @pf: pointer to the PF structure
3245 * @timeout: length of time to wait, in jiffies
3247 * Wait (sleep) for a short time until the driver finishes cleaning up from
3248 * a device reset. The caller must be able to sleep. Use this to delay
3249 * operations that could fail while the driver is cleaning up after a device
3252 * Returns 0 on success, -EBUSY if the reset is not finished within the
3253 * timeout, and -ERESTARTSYS if the thread was interrupted.
3255 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3259 ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3260 !ice_is_reset_in_progress(pf->state),
3272 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3273 * @vsi: VSI being configured
3274 * @ctx: the context buffer returned from AQ VSI update command
3276 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3278 vsi->info.mapping_flags = ctx->info.mapping_flags;
3279 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3280 sizeof(vsi->info.q_mapping));
3281 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3282 sizeof(vsi->info.tc_mapping));
3286 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3287 * @vsi: VSI to be configured
3288 * @ena_tc: TC bitmap
3290 * VSI queues expected to be quiesced before calling this function
3292 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3294 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3295 struct ice_pf *pf = vsi->back;
3296 struct ice_vsi_ctx *ctx;
3297 enum ice_status status;
3302 dev = ice_pf_to_dev(pf);
3304 ice_for_each_traffic_class(i) {
3305 /* build bitmap of enabled TCs */
3306 if (ena_tc & BIT(i))
3308 /* populate max_txqs per TC */
3309 max_txqs[i] = vsi->alloc_txq;
3312 vsi->tc_cfg.ena_tc = ena_tc;
3313 vsi->tc_cfg.numtc = num_tc;
3315 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3320 ctx->info = vsi->info;
3322 ice_vsi_setup_q_map(vsi, ctx);
3324 /* must to indicate which section of VSI context are being modified */
3325 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3326 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3328 dev_info(dev, "Failed VSI Update\n");
3333 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3337 dev_err(dev, "VSI %d failed TC config, error %s\n",
3338 vsi->vsi_num, ice_stat_str(status));
3342 ice_vsi_update_q_map(vsi, ctx);
3343 vsi->info.valid_sections = 0;
3345 ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3350 #endif /* CONFIG_DCB */
3353 * ice_update_ring_stats - Update ring statistics
3354 * @ring: ring to update
3355 * @pkts: number of processed packets
3356 * @bytes: number of processed bytes
3358 * This function assumes that caller has acquired a u64_stats_sync lock.
3360 static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3362 ring->stats.bytes += bytes;
3363 ring->stats.pkts += pkts;
3367 * ice_update_tx_ring_stats - Update Tx ring specific counters
3368 * @tx_ring: ring to update
3369 * @pkts: number of processed packets
3370 * @bytes: number of processed bytes
3372 void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3374 u64_stats_update_begin(&tx_ring->syncp);
3375 ice_update_ring_stats(tx_ring, pkts, bytes);
3376 u64_stats_update_end(&tx_ring->syncp);
3380 * ice_update_rx_ring_stats - Update Rx ring specific counters
3381 * @rx_ring: ring to update
3382 * @pkts: number of processed packets
3383 * @bytes: number of processed bytes
3385 void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3387 u64_stats_update_begin(&rx_ring->syncp);
3388 ice_update_ring_stats(rx_ring, pkts, bytes);
3389 u64_stats_update_end(&rx_ring->syncp);
3393 * ice_status_to_errno - convert from enum ice_status to Linux errno
3394 * @err: ice_status value to convert
3396 int ice_status_to_errno(enum ice_status err)
3401 case ICE_ERR_DOES_NOT_EXIST:
3403 case ICE_ERR_OUT_OF_RANGE:
3404 case ICE_ERR_AQ_ERROR:
3405 case ICE_ERR_AQ_TIMEOUT:
3406 case ICE_ERR_AQ_EMPTY:
3407 case ICE_ERR_AQ_FW_CRITICAL:
3410 case ICE_ERR_INVAL_SIZE:
3412 case ICE_ERR_NO_MEMORY:
3414 case ICE_ERR_MAX_LIMIT:
3416 case ICE_ERR_RESET_ONGOING:
3418 case ICE_ERR_AQ_FULL:
3426 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3427 * @sw: switch to check if its default forwarding VSI is free
3429 * Return true if the default forwarding VSI is already being used, else returns
3430 * false signalling that it's available to use.
3432 bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3434 return (sw->dflt_vsi && sw->dflt_vsi_ena);
3438 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3439 * @sw: switch for the default forwarding VSI to compare against
3440 * @vsi: VSI to compare against default forwarding VSI
3442 * If this VSI passed in is the default forwarding VSI then return true, else
3445 bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3447 return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3451 * ice_set_dflt_vsi - set the default forwarding VSI
3452 * @sw: switch used to assign the default forwarding VSI
3453 * @vsi: VSI getting set as the default forwarding VSI on the switch
3455 * If the VSI passed in is already the default VSI and it's enabled just return
3458 * If there is already a default VSI on the switch and it's enabled then return
3459 * -EEXIST since there can only be one default VSI per switch.
3461 * Otherwise try to set the VSI passed in as the switch's default VSI and
3462 * return the result.
3464 int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3466 enum ice_status status;
3472 dev = ice_pf_to_dev(vsi->back);
3474 /* the VSI passed in is already the default VSI */
3475 if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3476 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3481 /* another VSI is already the default VSI for this switch */
3482 if (ice_is_dflt_vsi_in_use(sw)) {
3483 dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3484 sw->dflt_vsi->vsi_num);
3488 status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3490 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3491 vsi->vsi_num, ice_stat_str(status));
3496 sw->dflt_vsi_ena = true;
3502 * ice_clear_dflt_vsi - clear the default forwarding VSI
3503 * @sw: switch used to clear the default VSI
3505 * If the switch has no default VSI or it's not enabled then return error.
3507 * Otherwise try to clear the default VSI and return the result.
3509 int ice_clear_dflt_vsi(struct ice_sw *sw)
3511 struct ice_vsi *dflt_vsi;
3512 enum ice_status status;
3518 dev = ice_pf_to_dev(sw->pf);
3520 dflt_vsi = sw->dflt_vsi;
3522 /* there is no default VSI configured */
3523 if (!ice_is_dflt_vsi_in_use(sw))
3526 status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3529 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3530 dflt_vsi->vsi_num, ice_stat_str(status));
3534 sw->dflt_vsi = NULL;
3535 sw->dflt_vsi_ena = false;
3541 * ice_set_link - turn on/off physical link
3542 * @vsi: VSI to modify physical link on
3543 * @ena: turn on/off physical link
3545 int ice_set_link(struct ice_vsi *vsi, bool ena)
3547 struct device *dev = ice_pf_to_dev(vsi->back);
3548 struct ice_port_info *pi = vsi->port_info;
3549 struct ice_hw *hw = pi->hw;
3550 enum ice_status status;
3552 if (vsi->type != ICE_VSI_PF)
3555 status = ice_aq_set_link_restart_an(pi, ena, NULL);
3557 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3558 * this is not a fatal error, so print a warning message and return
3559 * a success code. Return an error if FW returns an error code other
3560 * than ICE_AQ_RC_EMODE
3562 if (status == ICE_ERR_AQ_ERROR) {
3563 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3564 dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3565 (ena ? "ON" : "OFF"), ice_stat_str(status),
3566 ice_aq_str(hw->adminq.sq_last_status));
3567 } else if (status) {
3568 dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3569 (ena ? "ON" : "OFF"), ice_stat_str(status),
3570 ice_aq_str(hw->adminq.sq_last_status));