1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
5 #include "ice_vf_lib_private.h"
9 #include "ice_dcb_lib.h"
11 #include "ice_eswitch.h"
12 #include "ice_virtchnl_allowlist.h"
13 #include "ice_flex_pipe.h"
14 #include "ice_vf_vsi_vlan_ops.h"
18 * ice_free_vf_entries - Free all VF entries from the hash table
19 * @pf: pointer to the PF structure
21 * Iterate over the VF hash table, removing and releasing all VF entries.
22 * Called during VF teardown or as cleanup during failed VF initialization.
24 static void ice_free_vf_entries(struct ice_pf
*pf
)
26 struct ice_vfs
*vfs
= &pf
->vfs
;
27 struct hlist_node
*tmp
;
31 /* Remove all VFs from the hash table and release their main
32 * reference. Once all references to the VF are dropped, ice_put_vf()
33 * will call ice_release_vf which will remove the VF memory.
35 lockdep_assert_held(&vfs
->table_lock
);
37 hash_for_each_safe(vfs
->table
, bkt
, tmp
, vf
, entry
) {
38 hash_del_rcu(&vf
->entry
);
44 * ice_free_vf_res - Free a VF's resources
45 * @vf: pointer to the VF info
47 static void ice_free_vf_res(struct ice_vf
*vf
)
49 struct ice_pf
*pf
= vf
->pf
;
50 int i
, last_vector_idx
;
52 /* First, disable VF's configuration API to prevent OS from
53 * accessing the VF's VSI after it's freed or invalidated.
55 clear_bit(ICE_VF_STATE_INIT
, vf
->vf_states
);
57 /* free VF control VSI */
58 if (vf
->ctrl_vsi_idx
!= ICE_NO_VSI
)
59 ice_vf_ctrl_vsi_release(vf
);
61 /* free VSI and disconnect it from the parent uplink */
62 if (vf
->lan_vsi_idx
!= ICE_NO_VSI
) {
63 ice_vf_vsi_release(vf
);
67 last_vector_idx
= vf
->first_vector_idx
+ vf
->num_msix
- 1;
69 /* clear VF MDD event information */
70 memset(&vf
->mdd_tx_events
, 0, sizeof(vf
->mdd_tx_events
));
71 memset(&vf
->mdd_rx_events
, 0, sizeof(vf
->mdd_rx_events
));
73 /* Disable interrupts so that VF starts in a known state */
74 for (i
= vf
->first_vector_idx
; i
<= last_vector_idx
; i
++) {
75 wr32(&pf
->hw
, GLINT_DYN_CTL(i
), GLINT_DYN_CTL_CLEARPBA_M
);
78 /* reset some of the state variables keeping track of the resources */
79 clear_bit(ICE_VF_STATE_MC_PROMISC
, vf
->vf_states
);
80 clear_bit(ICE_VF_STATE_UC_PROMISC
, vf
->vf_states
);
85 * @vf: pointer to the VF structure
87 static void ice_dis_vf_mappings(struct ice_vf
*vf
)
89 struct ice_pf
*pf
= vf
->pf
;
96 vsi
= ice_get_vf_vsi(vf
);
100 dev
= ice_pf_to_dev(pf
);
101 wr32(hw
, VPINT_ALLOC(vf
->vf_id
), 0);
102 wr32(hw
, VPINT_ALLOC_PCI(vf
->vf_id
), 0);
104 first
= vf
->first_vector_idx
;
105 last
= first
+ vf
->num_msix
- 1;
106 for (v
= first
; v
<= last
; v
++) {
109 reg
= (((1 << GLINT_VECT2FUNC_IS_PF_S
) &
110 GLINT_VECT2FUNC_IS_PF_M
) |
111 ((hw
->pf_id
<< GLINT_VECT2FUNC_PF_NUM_S
) &
112 GLINT_VECT2FUNC_PF_NUM_M
));
113 wr32(hw
, GLINT_VECT2FUNC(v
), reg
);
116 if (vsi
->tx_mapping_mode
== ICE_VSI_MAP_CONTIG
)
117 wr32(hw
, VPLAN_TX_QBASE(vf
->vf_id
), 0);
119 dev_err(dev
, "Scattered mode for VF Tx queues is not yet implemented\n");
121 if (vsi
->rx_mapping_mode
== ICE_VSI_MAP_CONTIG
)
122 wr32(hw
, VPLAN_RX_QBASE(vf
->vf_id
), 0);
124 dev_err(dev
, "Scattered mode for VF Rx queues is not yet implemented\n");
128 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
129 * @pf: pointer to the PF structure
131 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
132 * the pf->sriov_base_vector.
134 * Returns 0 on success, and -EINVAL on error.
136 static int ice_sriov_free_msix_res(struct ice_pf
*pf
)
141 bitmap_free(pf
->sriov_irq_bm
);
142 pf
->sriov_irq_size
= 0;
143 pf
->sriov_base_vector
= 0;
149 * ice_free_vfs - Free all VFs
150 * @pf: pointer to the PF structure
152 void ice_free_vfs(struct ice_pf
*pf
)
154 struct device
*dev
= ice_pf_to_dev(pf
);
155 struct ice_vfs
*vfs
= &pf
->vfs
;
156 struct ice_hw
*hw
= &pf
->hw
;
160 if (!ice_has_vfs(pf
))
163 while (test_and_set_bit(ICE_VF_DIS
, pf
->state
))
164 usleep_range(1000, 2000);
166 /* Disable IOV before freeing resources. This lets any VF drivers
167 * running in the host get themselves cleaned up before we yank
168 * the carpet out from underneath their feet.
170 if (!pci_vfs_assigned(pf
->pdev
))
171 pci_disable_sriov(pf
->pdev
);
173 dev_warn(dev
, "VFs are assigned - not disabling SR-IOV\n");
175 mutex_lock(&vfs
->table_lock
);
177 ice_eswitch_release(pf
);
179 ice_for_each_vf(pf
, bkt
, vf
) {
180 mutex_lock(&vf
->cfg_lock
);
184 if (test_bit(ICE_VF_STATE_INIT
, vf
->vf_states
)) {
185 /* disable VF qp mappings and set VF disable state */
186 ice_dis_vf_mappings(vf
);
187 set_bit(ICE_VF_STATE_DIS
, vf
->vf_states
);
191 if (!pci_vfs_assigned(pf
->pdev
)) {
192 u32 reg_idx
, bit_idx
;
194 reg_idx
= (hw
->func_caps
.vf_base_id
+ vf
->vf_id
) / 32;
195 bit_idx
= (hw
->func_caps
.vf_base_id
+ vf
->vf_id
) % 32;
196 wr32(hw
, GLGEN_VFLRSTAT(reg_idx
), BIT(bit_idx
));
199 /* clear malicious info since the VF is getting released */
200 list_del(&vf
->mbx_info
.list_entry
);
202 mutex_unlock(&vf
->cfg_lock
);
205 if (ice_sriov_free_msix_res(pf
))
206 dev_err(dev
, "Failed to free MSIX resources used by SR-IOV\n");
208 vfs
->num_qps_per
= 0;
209 ice_free_vf_entries(pf
);
211 mutex_unlock(&vfs
->table_lock
);
213 clear_bit(ICE_VF_DIS
, pf
->state
);
214 clear_bit(ICE_FLAG_SRIOV_ENA
, pf
->flags
);
218 * ice_vf_vsi_setup - Set up a VF VSI
219 * @vf: VF to setup VSI for
221 * Returns pointer to the successfully allocated VSI struct on success,
222 * otherwise returns NULL on failure.
224 static struct ice_vsi
*ice_vf_vsi_setup(struct ice_vf
*vf
)
226 struct ice_vsi_cfg_params params
= {};
227 struct ice_pf
*pf
= vf
->pf
;
230 params
.type
= ICE_VSI_VF
;
231 params
.pi
= ice_vf_get_port_info(vf
);
233 params
.flags
= ICE_VSI_FLAG_INIT
;
235 vsi
= ice_vsi_setup(pf
, ¶ms
);
238 dev_err(ice_pf_to_dev(pf
), "Failed to create VF VSI\n");
239 ice_vf_invalidate_vsi(vf
);
243 vf
->lan_vsi_idx
= vsi
->idx
;
244 vf
->lan_vsi_num
= vsi
->vsi_num
;
251 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
252 * @vf: VF to enable MSIX mappings for
254 * Some of the registers need to be indexed/configured using hardware global
255 * device values and other registers need 0-based values, which represent PF
258 static void ice_ena_vf_msix_mappings(struct ice_vf
*vf
)
260 int device_based_first_msix
, device_based_last_msix
;
261 int pf_based_first_msix
, pf_based_last_msix
, v
;
262 struct ice_pf
*pf
= vf
->pf
;
263 int device_based_vf_id
;
268 pf_based_first_msix
= vf
->first_vector_idx
;
269 pf_based_last_msix
= (pf_based_first_msix
+ vf
->num_msix
) - 1;
271 device_based_first_msix
= pf_based_first_msix
+
272 pf
->hw
.func_caps
.common_cap
.msix_vector_first_id
;
273 device_based_last_msix
=
274 (device_based_first_msix
+ vf
->num_msix
) - 1;
275 device_based_vf_id
= vf
->vf_id
+ hw
->func_caps
.vf_base_id
;
277 reg
= (((device_based_first_msix
<< VPINT_ALLOC_FIRST_S
) &
278 VPINT_ALLOC_FIRST_M
) |
279 ((device_based_last_msix
<< VPINT_ALLOC_LAST_S
) &
280 VPINT_ALLOC_LAST_M
) | VPINT_ALLOC_VALID_M
);
281 wr32(hw
, VPINT_ALLOC(vf
->vf_id
), reg
);
283 reg
= (((device_based_first_msix
<< VPINT_ALLOC_PCI_FIRST_S
)
284 & VPINT_ALLOC_PCI_FIRST_M
) |
285 ((device_based_last_msix
<< VPINT_ALLOC_PCI_LAST_S
) &
286 VPINT_ALLOC_PCI_LAST_M
) | VPINT_ALLOC_PCI_VALID_M
);
287 wr32(hw
, VPINT_ALLOC_PCI(vf
->vf_id
), reg
);
289 /* map the interrupts to its functions */
290 for (v
= pf_based_first_msix
; v
<= pf_based_last_msix
; v
++) {
291 reg
= (((device_based_vf_id
<< GLINT_VECT2FUNC_VF_NUM_S
) &
292 GLINT_VECT2FUNC_VF_NUM_M
) |
293 ((hw
->pf_id
<< GLINT_VECT2FUNC_PF_NUM_S
) &
294 GLINT_VECT2FUNC_PF_NUM_M
));
295 wr32(hw
, GLINT_VECT2FUNC(v
), reg
);
298 /* Map mailbox interrupt to VF MSI-X vector 0 */
299 wr32(hw
, VPINT_MBX_CTL(device_based_vf_id
), VPINT_MBX_CTL_CAUSE_ENA_M
);
303 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
304 * @vf: VF to enable the mappings for
305 * @max_txq: max Tx queues allowed on the VF's VSI
306 * @max_rxq: max Rx queues allowed on the VF's VSI
308 static void ice_ena_vf_q_mappings(struct ice_vf
*vf
, u16 max_txq
, u16 max_rxq
)
310 struct device
*dev
= ice_pf_to_dev(vf
->pf
);
311 struct ice_vsi
*vsi
= ice_get_vf_vsi(vf
);
312 struct ice_hw
*hw
= &vf
->pf
->hw
;
318 /* set regardless of mapping mode */
319 wr32(hw
, VPLAN_TXQ_MAPENA(vf
->vf_id
), VPLAN_TXQ_MAPENA_TX_ENA_M
);
321 /* VF Tx queues allocation */
322 if (vsi
->tx_mapping_mode
== ICE_VSI_MAP_CONTIG
) {
323 /* set the VF PF Tx queue range
324 * VFNUMQ value should be set to (number of queues - 1). A value
325 * of 0 means 1 queue and a value of 255 means 256 queues
327 reg
= (((vsi
->txq_map
[0] << VPLAN_TX_QBASE_VFFIRSTQ_S
) &
328 VPLAN_TX_QBASE_VFFIRSTQ_M
) |
329 (((max_txq
- 1) << VPLAN_TX_QBASE_VFNUMQ_S
) &
330 VPLAN_TX_QBASE_VFNUMQ_M
));
331 wr32(hw
, VPLAN_TX_QBASE(vf
->vf_id
), reg
);
333 dev_err(dev
, "Scattered mode for VF Tx queues is not yet implemented\n");
336 /* set regardless of mapping mode */
337 wr32(hw
, VPLAN_RXQ_MAPENA(vf
->vf_id
), VPLAN_RXQ_MAPENA_RX_ENA_M
);
339 /* VF Rx queues allocation */
340 if (vsi
->rx_mapping_mode
== ICE_VSI_MAP_CONTIG
) {
341 /* set the VF PF Rx queue range
342 * VFNUMQ value should be set to (number of queues - 1). A value
343 * of 0 means 1 queue and a value of 255 means 256 queues
345 reg
= (((vsi
->rxq_map
[0] << VPLAN_RX_QBASE_VFFIRSTQ_S
) &
346 VPLAN_RX_QBASE_VFFIRSTQ_M
) |
347 (((max_rxq
- 1) << VPLAN_RX_QBASE_VFNUMQ_S
) &
348 VPLAN_RX_QBASE_VFNUMQ_M
));
349 wr32(hw
, VPLAN_RX_QBASE(vf
->vf_id
), reg
);
351 dev_err(dev
, "Scattered mode for VF Rx queues is not yet implemented\n");
356 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
357 * @vf: pointer to the VF structure
359 static void ice_ena_vf_mappings(struct ice_vf
*vf
)
361 struct ice_vsi
*vsi
= ice_get_vf_vsi(vf
);
366 ice_ena_vf_msix_mappings(vf
);
367 ice_ena_vf_q_mappings(vf
, vsi
->alloc_txq
, vsi
->alloc_rxq
);
371 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
372 * @vf: VF to calculate the register index for
373 * @q_vector: a q_vector associated to the VF
375 int ice_calc_vf_reg_idx(struct ice_vf
*vf
, struct ice_q_vector
*q_vector
)
379 if (!vf
|| !q_vector
)
384 /* always add one to account for the OICR being the first MSIX */
385 return pf
->sriov_base_vector
+ pf
->vfs
.num_msix_per
* vf
->vf_id
+
390 * ice_sriov_set_msix_res - Set any used MSIX resources
391 * @pf: pointer to PF structure
392 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
394 * This function allows SR-IOV resources to be taken from the end of the PF's
395 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
396 * just set the pf->sriov_base_vector and return success.
398 * If there are not enough resources available, return an error. This should
399 * always be caught by ice_set_per_vf_res().
401 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
402 * in the PF's space available for SR-IOV.
404 static int ice_sriov_set_msix_res(struct ice_pf
*pf
, u16 num_msix_needed
)
406 u16 total_vectors
= pf
->hw
.func_caps
.common_cap
.num_msix_vectors
;
407 int vectors_used
= ice_get_max_used_msix_vector(pf
);
408 int sriov_base_vector
;
410 sriov_base_vector
= total_vectors
- num_msix_needed
;
412 /* make sure we only grab irq_tracker entries from the list end and
413 * that we have enough available MSIX vectors
415 if (sriov_base_vector
< vectors_used
)
418 pf
->sriov_base_vector
= sriov_base_vector
;
424 * ice_set_per_vf_res - check if vectors and queues are available
425 * @pf: pointer to the PF structure
426 * @num_vfs: the number of SR-IOV VFs being configured
428 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
429 * get more vectors and can enable more queues per VF. Note that this does not
430 * grab any vectors from the SW pool already allocated. Also note, that all
431 * vector counts include one for each VF's miscellaneous interrupt vector
434 * Minimum VFs - 2 vectors, 1 queue pair
435 * Small VFs - 5 vectors, 4 queue pairs
436 * Medium VFs - 17 vectors, 16 queue pairs
438 * Second, determine number of queue pairs per VF by starting with a pre-defined
439 * maximum each VF supports. If this is not possible, then we adjust based on
440 * queue pairs available on the device.
442 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
443 * by each VF during VF initialization and reset.
445 static int ice_set_per_vf_res(struct ice_pf
*pf
, u16 num_vfs
)
447 int vectors_used
= ice_get_max_used_msix_vector(pf
);
448 u16 num_msix_per_vf
, num_txq
, num_rxq
, avail_qs
;
449 int msix_avail_per_vf
, msix_avail_for_sriov
;
450 struct device
*dev
= ice_pf_to_dev(pf
);
453 lockdep_assert_held(&pf
->vfs
.table_lock
);
458 /* determine MSI-X resources per VF */
459 msix_avail_for_sriov
= pf
->hw
.func_caps
.common_cap
.num_msix_vectors
-
461 msix_avail_per_vf
= msix_avail_for_sriov
/ num_vfs
;
462 if (msix_avail_per_vf
>= ICE_NUM_VF_MSIX_MED
) {
463 num_msix_per_vf
= ICE_NUM_VF_MSIX_MED
;
464 } else if (msix_avail_per_vf
>= ICE_NUM_VF_MSIX_SMALL
) {
465 num_msix_per_vf
= ICE_NUM_VF_MSIX_SMALL
;
466 } else if (msix_avail_per_vf
>= ICE_NUM_VF_MSIX_MULTIQ_MIN
) {
467 num_msix_per_vf
= ICE_NUM_VF_MSIX_MULTIQ_MIN
;
468 } else if (msix_avail_per_vf
>= ICE_MIN_INTR_PER_VF
) {
469 num_msix_per_vf
= ICE_MIN_INTR_PER_VF
;
471 dev_err(dev
, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
472 msix_avail_for_sriov
, ICE_MIN_INTR_PER_VF
,
477 num_txq
= min_t(u16
, num_msix_per_vf
- ICE_NONQ_VECS_VF
,
478 ICE_MAX_RSS_QS_PER_VF
);
479 avail_qs
= ice_get_avail_txq_count(pf
) / num_vfs
;
482 else if (num_txq
> avail_qs
)
483 num_txq
= rounddown_pow_of_two(avail_qs
);
485 num_rxq
= min_t(u16
, num_msix_per_vf
- ICE_NONQ_VECS_VF
,
486 ICE_MAX_RSS_QS_PER_VF
);
487 avail_qs
= ice_get_avail_rxq_count(pf
) / num_vfs
;
490 else if (num_rxq
> avail_qs
)
491 num_rxq
= rounddown_pow_of_two(avail_qs
);
493 if (num_txq
< ICE_MIN_QS_PER_VF
|| num_rxq
< ICE_MIN_QS_PER_VF
) {
494 dev_err(dev
, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
495 ICE_MIN_QS_PER_VF
, num_vfs
);
499 err
= ice_sriov_set_msix_res(pf
, num_msix_per_vf
* num_vfs
);
501 dev_err(dev
, "Unable to set MSI-X resources for %d VFs, err %d\n",
506 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
507 pf
->vfs
.num_qps_per
= min_t(int, num_txq
, num_rxq
);
508 pf
->vfs
.num_msix_per
= num_msix_per_vf
;
509 dev_info(dev
, "Enabling %d VFs with %d vectors and %d queues per VF\n",
510 num_vfs
, pf
->vfs
.num_msix_per
, pf
->vfs
.num_qps_per
);
516 * ice_sriov_get_irqs - get irqs for SR-IOV usacase
517 * @pf: pointer to PF structure
518 * @needed: number of irqs to get
520 * This returns the first MSI-X vector index in PF space that is used by this
521 * VF. This index is used when accessing PF relative registers such as
522 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
523 * This will always be the OICR index in the AVF driver so any functionality
524 * using vf->first_vector_idx for queue configuration_id: id of VF which will
527 * Only SRIOV specific vectors are tracked in sriov_irq_bm. SRIOV vectors are
528 * allocated from the end of global irq index. First bit in sriov_irq_bm means
529 * last irq index etc. It simplifies extension of SRIOV vectors.
530 * They will be always located from sriov_base_vector to the last irq
531 * index. While increasing/decreasing sriov_base_vector can be moved.
533 static int ice_sriov_get_irqs(struct ice_pf
*pf
, u16 needed
)
535 int res
= bitmap_find_next_zero_area(pf
->sriov_irq_bm
,
536 pf
->sriov_irq_size
, 0, needed
, 0);
537 /* conversion from number in bitmap to global irq index */
538 int index
= pf
->sriov_irq_size
- res
- needed
;
540 if (res
>= pf
->sriov_irq_size
|| index
< pf
->sriov_base_vector
)
543 bitmap_set(pf
->sriov_irq_bm
, res
, needed
);
548 * ice_sriov_free_irqs - free irqs used by the VF
549 * @pf: pointer to PF structure
550 * @vf: pointer to VF structure
552 static void ice_sriov_free_irqs(struct ice_pf
*pf
, struct ice_vf
*vf
)
554 /* Move back from first vector index to first index in bitmap */
555 int bm_i
= pf
->sriov_irq_size
- vf
->first_vector_idx
- vf
->num_msix
;
557 bitmap_clear(pf
->sriov_irq_bm
, bm_i
, vf
->num_msix
);
558 vf
->first_vector_idx
= 0;
562 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
563 * @vf: VF to initialize/setup the VSI for
565 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
566 * VF VSI's broadcast filter and is only used during initial VF creation.
568 static int ice_init_vf_vsi_res(struct ice_vf
*vf
)
570 struct ice_pf
*pf
= vf
->pf
;
574 vf
->first_vector_idx
= ice_sriov_get_irqs(pf
, vf
->num_msix
);
575 if (vf
->first_vector_idx
< 0)
578 vsi
= ice_vf_vsi_setup(vf
);
582 err
= ice_vf_init_host_cfg(vf
, vsi
);
589 ice_vf_vsi_release(vf
);
594 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
595 * @pf: PF the VFs are associated with
597 static int ice_start_vfs(struct ice_pf
*pf
)
599 struct ice_hw
*hw
= &pf
->hw
;
600 unsigned int bkt
, it_cnt
;
604 lockdep_assert_held(&pf
->vfs
.table_lock
);
607 ice_for_each_vf(pf
, bkt
, vf
) {
608 vf
->vf_ops
->clear_reset_trigger(vf
);
610 retval
= ice_init_vf_vsi_res(vf
);
612 dev_err(ice_pf_to_dev(pf
), "Failed to initialize VSI resources for VF %d, error %d\n",
617 set_bit(ICE_VF_STATE_INIT
, vf
->vf_states
);
618 ice_ena_vf_mappings(vf
);
619 wr32(hw
, VFGEN_RSTAT(vf
->vf_id
), VIRTCHNL_VFR_VFACTIVE
);
627 ice_for_each_vf(pf
, bkt
, vf
) {
631 ice_dis_vf_mappings(vf
);
632 ice_vf_vsi_release(vf
);
640 * ice_sriov_free_vf - Free VF memory after all references are dropped
641 * @vf: pointer to VF to free
643 * Called by ice_put_vf through ice_release_vf once the last reference to a VF
644 * structure has been dropped.
646 static void ice_sriov_free_vf(struct ice_vf
*vf
)
648 mutex_destroy(&vf
->cfg_lock
);
654 * ice_sriov_clear_reset_state - clears VF Reset status register
655 * @vf: the vf to configure
657 static void ice_sriov_clear_reset_state(struct ice_vf
*vf
)
659 struct ice_hw
*hw
= &vf
->pf
->hw
;
661 /* Clear the reset status register so that VF immediately sees that
662 * the device is resetting, even if hardware hasn't yet gotten around
663 * to clearing VFGEN_RSTAT for us.
665 wr32(hw
, VFGEN_RSTAT(vf
->vf_id
), VIRTCHNL_VFR_INPROGRESS
);
669 * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers
670 * @vf: the vf to configure
672 static void ice_sriov_clear_mbx_register(struct ice_vf
*vf
)
674 struct ice_pf
*pf
= vf
->pf
;
676 wr32(&pf
->hw
, VF_MBX_ARQLEN(vf
->vf_id
), 0);
677 wr32(&pf
->hw
, VF_MBX_ATQLEN(vf
->vf_id
), 0);
681 * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF
682 * @vf: pointer to VF structure
683 * @is_vflr: true if reset occurred due to VFLR
685 * Trigger and cleanup after a VF reset for a SR-IOV VF.
687 static void ice_sriov_trigger_reset_register(struct ice_vf
*vf
, bool is_vflr
)
689 struct ice_pf
*pf
= vf
->pf
;
690 u32 reg
, reg_idx
, bit_idx
;
691 unsigned int vf_abs_id
, i
;
695 dev
= ice_pf_to_dev(pf
);
697 vf_abs_id
= vf
->vf_id
+ hw
->func_caps
.vf_base_id
;
699 /* In the case of a VFLR, HW has already reset the VF and we just need
700 * to clean up. Otherwise we must first trigger the reset using the
704 reg
= rd32(hw
, VPGEN_VFRTRIG(vf
->vf_id
));
705 reg
|= VPGEN_VFRTRIG_VFSWR_M
;
706 wr32(hw
, VPGEN_VFRTRIG(vf
->vf_id
), reg
);
709 /* clear the VFLR bit in GLGEN_VFLRSTAT */
710 reg_idx
= (vf_abs_id
) / 32;
711 bit_idx
= (vf_abs_id
) % 32;
712 wr32(hw
, GLGEN_VFLRSTAT(reg_idx
), BIT(bit_idx
));
715 wr32(hw
, PF_PCI_CIAA
,
716 VF_DEVICE_STATUS
| (vf_abs_id
<< PF_PCI_CIAA_VF_NUM_S
));
717 for (i
= 0; i
< ICE_PCI_CIAD_WAIT_COUNT
; i
++) {
718 reg
= rd32(hw
, PF_PCI_CIAD
);
719 /* no transactions pending so stop polling */
720 if ((reg
& VF_TRANS_PENDING_M
) == 0)
723 dev_err(dev
, "VF %u PCI transactions stuck\n", vf
->vf_id
);
724 udelay(ICE_PCI_CIAD_WAIT_DELAY_US
);
729 * ice_sriov_poll_reset_status - poll SRIOV VF reset status
730 * @vf: pointer to VF structure
732 * Returns true when reset is successful, else returns false
734 static bool ice_sriov_poll_reset_status(struct ice_vf
*vf
)
736 struct ice_pf
*pf
= vf
->pf
;
740 for (i
= 0; i
< 10; i
++) {
741 /* VF reset requires driver to first reset the VF and then
742 * poll the status register to make sure that the reset
743 * completed successfully.
745 reg
= rd32(&pf
->hw
, VPGEN_VFRSTAT(vf
->vf_id
));
746 if (reg
& VPGEN_VFRSTAT_VFRD_M
)
749 /* only sleep if the reset is not done */
750 usleep_range(10, 20);
756 * ice_sriov_clear_reset_trigger - enable VF to access hardware
757 * @vf: VF to enabled hardware access for
759 static void ice_sriov_clear_reset_trigger(struct ice_vf
*vf
)
761 struct ice_hw
*hw
= &vf
->pf
->hw
;
764 reg
= rd32(hw
, VPGEN_VFRTRIG(vf
->vf_id
));
765 reg
&= ~VPGEN_VFRTRIG_VFSWR_M
;
766 wr32(hw
, VPGEN_VFRTRIG(vf
->vf_id
), reg
);
771 * ice_sriov_create_vsi - Create a new VSI for a VF
772 * @vf: VF to create the VSI for
774 * This is called by ice_vf_recreate_vsi to create the new VSI after the old
775 * VSI has been released.
777 static int ice_sriov_create_vsi(struct ice_vf
*vf
)
781 vsi
= ice_vf_vsi_setup(vf
);
789 * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
790 * @vf: VF to perform tasks on
792 static void ice_sriov_post_vsi_rebuild(struct ice_vf
*vf
)
794 ice_ena_vf_mappings(vf
);
795 wr32(&vf
->pf
->hw
, VFGEN_RSTAT(vf
->vf_id
), VIRTCHNL_VFR_VFACTIVE
);
798 static const struct ice_vf_ops ice_sriov_vf_ops
= {
799 .reset_type
= ICE_VF_RESET
,
800 .free
= ice_sriov_free_vf
,
801 .clear_reset_state
= ice_sriov_clear_reset_state
,
802 .clear_mbx_register
= ice_sriov_clear_mbx_register
,
803 .trigger_reset_register
= ice_sriov_trigger_reset_register
,
804 .poll_reset_status
= ice_sriov_poll_reset_status
,
805 .clear_reset_trigger
= ice_sriov_clear_reset_trigger
,
807 .create_vsi
= ice_sriov_create_vsi
,
808 .post_vsi_rebuild
= ice_sriov_post_vsi_rebuild
,
812 * ice_create_vf_entries - Allocate and insert VF entries
813 * @pf: pointer to the PF structure
814 * @num_vfs: the number of VFs to allocate
816 * Allocate new VF entries and insert them into the hash table. Set some
817 * basic default fields for initializing the new VFs.
819 * After this function exits, the hash table will have num_vfs entries
822 * Returns 0 on success or an integer error code on failure.
824 static int ice_create_vf_entries(struct ice_pf
*pf
, u16 num_vfs
)
826 struct pci_dev
*pdev
= pf
->pdev
;
827 struct ice_vfs
*vfs
= &pf
->vfs
;
828 struct pci_dev
*vfdev
= NULL
;
833 lockdep_assert_held(&vfs
->table_lock
);
835 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_SRIOV
);
836 pci_read_config_word(pdev
, pos
+ PCI_SRIOV_VF_DID
, &vf_pdev_id
);
838 for (u16 vf_id
= 0; vf_id
< num_vfs
; vf_id
++) {
839 vf
= kzalloc(sizeof(*vf
), GFP_KERNEL
);
842 goto err_free_entries
;
844 kref_init(&vf
->refcnt
);
849 /* set sriov vf ops for VFs created during SRIOV flow */
850 vf
->vf_ops
= &ice_sriov_vf_ops
;
852 ice_initialize_vf_entry(vf
);
855 vfdev
= pci_get_device(pdev
->vendor
, vf_pdev_id
, vfdev
);
856 } while (vfdev
&& vfdev
->physfn
!= pdev
);
858 vf
->vf_sw_id
= pf
->first_sw
;
862 /* set default number of MSI-X */
863 vf
->num_msix
= pf
->vfs
.num_msix_per
;
864 vf
->num_vf_qs
= pf
->vfs
.num_qps_per
;
865 ice_vc_set_default_allowlist(vf
);
867 hash_add_rcu(vfs
->table
, &vf
->entry
, vf_id
);
870 /* Decrement of refcount done by pci_get_device() inside the loop does
871 * not touch the last iteration's vfdev, so it has to be done manually
872 * to balance pci_dev_get() added within the loop.
879 ice_free_vf_entries(pf
);
884 * ice_ena_vfs - enable VFs so they are ready to be used
885 * @pf: pointer to the PF structure
886 * @num_vfs: number of VFs to enable
888 static int ice_ena_vfs(struct ice_pf
*pf
, u16 num_vfs
)
890 int total_vectors
= pf
->hw
.func_caps
.common_cap
.num_msix_vectors
;
891 struct device
*dev
= ice_pf_to_dev(pf
);
892 struct ice_hw
*hw
= &pf
->hw
;
895 pf
->sriov_irq_bm
= bitmap_zalloc(total_vectors
, GFP_KERNEL
);
896 if (!pf
->sriov_irq_bm
)
898 pf
->sriov_irq_size
= total_vectors
;
900 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
901 wr32(hw
, GLINT_DYN_CTL(pf
->oicr_irq
.index
),
902 ICE_ITR_NONE
<< GLINT_DYN_CTL_ITR_INDX_S
);
903 set_bit(ICE_OICR_INTR_DIS
, pf
->state
);
906 ret
= pci_enable_sriov(pf
->pdev
, num_vfs
);
908 goto err_unroll_intr
;
910 mutex_lock(&pf
->vfs
.table_lock
);
912 ret
= ice_set_per_vf_res(pf
, num_vfs
);
914 dev_err(dev
, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n",
916 goto err_unroll_sriov
;
919 ret
= ice_create_vf_entries(pf
, num_vfs
);
921 dev_err(dev
, "Failed to allocate VF entries for %d VFs\n",
923 goto err_unroll_sriov
;
926 ret
= ice_start_vfs(pf
);
928 dev_err(dev
, "Failed to start %d VFs, err %d\n", num_vfs
, ret
);
930 goto err_unroll_vf_entries
;
933 clear_bit(ICE_VF_DIS
, pf
->state
);
935 ret
= ice_eswitch_configure(pf
);
937 dev_err(dev
, "Failed to configure eswitch, err %d\n", ret
);
938 goto err_unroll_sriov
;
941 /* rearm global interrupts */
942 if (test_and_clear_bit(ICE_OICR_INTR_DIS
, pf
->state
))
943 ice_irq_dynamic_ena(hw
, NULL
, NULL
);
945 mutex_unlock(&pf
->vfs
.table_lock
);
949 err_unroll_vf_entries
:
950 ice_free_vf_entries(pf
);
952 mutex_unlock(&pf
->vfs
.table_lock
);
953 pci_disable_sriov(pf
->pdev
);
955 /* rearm interrupts here */
956 ice_irq_dynamic_ena(hw
, NULL
, NULL
);
957 clear_bit(ICE_OICR_INTR_DIS
, pf
->state
);
958 bitmap_free(pf
->sriov_irq_bm
);
963 * ice_pci_sriov_ena - Enable or change number of VFs
964 * @pf: pointer to the PF structure
965 * @num_vfs: number of VFs to allocate
967 * Returns 0 on success and negative on failure
969 static int ice_pci_sriov_ena(struct ice_pf
*pf
, int num_vfs
)
971 struct device
*dev
= ice_pf_to_dev(pf
);
979 if (num_vfs
> pf
->vfs
.num_supported
) {
980 dev_err(dev
, "Can't enable %d VFs, max VFs supported is %d\n",
981 num_vfs
, pf
->vfs
.num_supported
);
985 dev_info(dev
, "Enabling %d VFs\n", num_vfs
);
986 err
= ice_ena_vfs(pf
, num_vfs
);
988 dev_err(dev
, "Failed to enable SR-IOV: %d\n", err
);
992 set_bit(ICE_FLAG_SRIOV_ENA
, pf
->flags
);
997 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
998 * @pf: PF to enabled SR-IOV on
1000 static int ice_check_sriov_allowed(struct ice_pf
*pf
)
1002 struct device
*dev
= ice_pf_to_dev(pf
);
1004 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE
, pf
->flags
)) {
1005 dev_err(dev
, "This device is not capable of SR-IOV\n");
1009 if (ice_is_safe_mode(pf
)) {
1010 dev_err(dev
, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1014 if (!ice_pf_state_is_nominal(pf
)) {
1015 dev_err(dev
, "Cannot enable SR-IOV, device not ready\n");
1023 * ice_sriov_get_vf_total_msix - return number of MSI-X used by VFs
1024 * @pdev: pointer to pci_dev struct
1026 * The function is called via sysfs ops
1028 u32
ice_sriov_get_vf_total_msix(struct pci_dev
*pdev
)
1030 struct ice_pf
*pf
= pci_get_drvdata(pdev
);
1032 return pf
->sriov_irq_size
- ice_get_max_used_msix_vector(pf
);
1035 static int ice_sriov_move_base_vector(struct ice_pf
*pf
, int move
)
1037 if (pf
->sriov_base_vector
- move
< ice_get_max_used_msix_vector(pf
))
1040 pf
->sriov_base_vector
-= move
;
1044 static void ice_sriov_remap_vectors(struct ice_pf
*pf
, u16 restricted_id
)
1046 u16 vf_ids
[ICE_MAX_SRIOV_VFS
];
1047 struct ice_vf
*tmp_vf
;
1048 int to_remap
= 0, bkt
;
1050 /* For better irqs usage try to remap irqs of VFs
1051 * that aren't running yet
1053 ice_for_each_vf(pf
, bkt
, tmp_vf
) {
1054 /* skip VF which is changing the number of MSI-X */
1055 if (restricted_id
== tmp_vf
->vf_id
||
1056 test_bit(ICE_VF_STATE_ACTIVE
, tmp_vf
->vf_states
))
1059 ice_dis_vf_mappings(tmp_vf
);
1060 ice_sriov_free_irqs(pf
, tmp_vf
);
1062 vf_ids
[to_remap
] = tmp_vf
->vf_id
;
1066 for (int i
= 0; i
< to_remap
; i
++) {
1067 tmp_vf
= ice_get_vf_by_id(pf
, vf_ids
[i
]);
1071 tmp_vf
->first_vector_idx
=
1072 ice_sriov_get_irqs(pf
, tmp_vf
->num_msix
);
1073 /* there is no need to rebuild VSI as we are only changing the
1074 * vector indexes not amount of MSI-X or queues
1076 ice_ena_vf_mappings(tmp_vf
);
1082 * ice_sriov_set_msix_vec_count
1083 * @vf_dev: pointer to pci_dev struct of VF device
1084 * @msix_vec_count: new value for MSI-X amount on this VF
1086 * Set requested MSI-X, queues and registers for @vf_dev.
1088 * First do some sanity checks like if there are any VFs, if the new value
1089 * is correct etc. Then disable old mapping (MSI-X and queues registers), change
1090 * MSI-X and queues, rebuild VSI and enable new mapping.
1092 * If it is possible (driver not binded to VF) try to remap also other VFs to
1093 * linearize irqs register usage.
1095 int ice_sriov_set_msix_vec_count(struct pci_dev
*vf_dev
, int msix_vec_count
)
1097 struct pci_dev
*pdev
= pci_physfn(vf_dev
);
1098 struct ice_pf
*pf
= pci_get_drvdata(pdev
);
1099 u16 prev_msix
, prev_queues
, queues
;
1100 bool needs_rebuild
= false;
1104 if (!ice_get_num_vfs(pf
))
1107 if (!msix_vec_count
)
1110 queues
= msix_vec_count
;
1111 /* add 1 MSI-X for OICR */
1112 msix_vec_count
+= 1;
1114 if (queues
> min(ice_get_avail_txq_count(pf
),
1115 ice_get_avail_rxq_count(pf
)))
1118 if (msix_vec_count
< ICE_MIN_INTR_PER_VF
)
1121 /* Transition of PCI VF function number to function_id */
1122 for (id
= 0; id
< pci_num_vf(pdev
); id
++) {
1123 if (vf_dev
->devfn
== pci_iov_virtfn_devfn(pdev
, id
))
1127 if (id
== pci_num_vf(pdev
))
1130 vf
= ice_get_vf_by_id(pf
, id
);
1135 prev_msix
= vf
->num_msix
;
1136 prev_queues
= vf
->num_vf_qs
;
1138 if (ice_sriov_move_base_vector(pf
, msix_vec_count
- prev_msix
)) {
1143 ice_dis_vf_mappings(vf
);
1144 ice_sriov_free_irqs(pf
, vf
);
1146 /* Remap all VFs beside the one is now configured */
1147 ice_sriov_remap_vectors(pf
, vf
->vf_id
);
1149 vf
->num_msix
= msix_vec_count
;
1150 vf
->num_vf_qs
= queues
;
1151 vf
->first_vector_idx
= ice_sriov_get_irqs(pf
, vf
->num_msix
);
1152 if (vf
->first_vector_idx
< 0)
1155 ice_vf_vsi_release(vf
);
1156 if (vf
->vf_ops
->create_vsi(vf
)) {
1157 /* Try to rebuild with previous values */
1158 needs_rebuild
= true;
1162 dev_info(ice_pf_to_dev(pf
),
1163 "Changing VF %d resources to %d vectors and %d queues\n",
1164 vf
->vf_id
, vf
->num_msix
, vf
->num_vf_qs
);
1166 ice_ena_vf_mappings(vf
);
1172 dev_info(ice_pf_to_dev(pf
),
1173 "Can't set %d vectors on VF %d, falling back to %d\n",
1174 vf
->num_msix
, vf
->vf_id
, prev_msix
);
1176 vf
->num_msix
= prev_msix
;
1177 vf
->num_vf_qs
= prev_queues
;
1178 vf
->first_vector_idx
= ice_sriov_get_irqs(pf
, vf
->num_msix
);
1179 if (vf
->first_vector_idx
< 0)
1183 vf
->vf_ops
->create_vsi(vf
);
1185 ice_ena_vf_mappings(vf
);
1192 * ice_sriov_configure - Enable or change number of VFs via sysfs
1193 * @pdev: pointer to a pci_dev structure
1194 * @num_vfs: number of VFs to allocate or 0 to free VFs
1196 * This function is called when the user updates the number of VFs in sysfs. On
1197 * success return whatever num_vfs was set to by the caller. Return negative on
1200 int ice_sriov_configure(struct pci_dev
*pdev
, int num_vfs
)
1202 struct ice_pf
*pf
= pci_get_drvdata(pdev
);
1203 struct device
*dev
= ice_pf_to_dev(pf
);
1206 err
= ice_check_sriov_allowed(pf
);
1211 if (!pci_vfs_assigned(pdev
)) {
1216 dev_err(dev
, "can't free VFs because some are assigned to VMs.\n");
1220 err
= ice_pci_sriov_ena(pf
, num_vfs
);
1228 * ice_process_vflr_event - Free VF resources via IRQ calls
1229 * @pf: pointer to the PF structure
1231 * called from the VFLR IRQ handler to
1232 * free up VF resources and state variables
1234 void ice_process_vflr_event(struct ice_pf
*pf
)
1236 struct ice_hw
*hw
= &pf
->hw
;
1241 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING
, pf
->state
) ||
1245 mutex_lock(&pf
->vfs
.table_lock
);
1246 ice_for_each_vf(pf
, bkt
, vf
) {
1247 u32 reg_idx
, bit_idx
;
1249 reg_idx
= (hw
->func_caps
.vf_base_id
+ vf
->vf_id
) / 32;
1250 bit_idx
= (hw
->func_caps
.vf_base_id
+ vf
->vf_id
) % 32;
1251 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1252 reg
= rd32(hw
, GLGEN_VFLRSTAT(reg_idx
));
1253 if (reg
& BIT(bit_idx
))
1254 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1255 ice_reset_vf(vf
, ICE_VF_RESET_VFLR
| ICE_VF_RESET_LOCK
);
1257 mutex_unlock(&pf
->vfs
.table_lock
);
1261 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1262 * @pf: PF used to index all VFs
1263 * @pfq: queue index relative to the PF's function space
1265 * If no VF is found who owns the pfq then return NULL, otherwise return a
1266 * pointer to the VF who owns the pfq
1268 * If this function returns non-NULL, it acquires a reference count of the VF
1269 * structure. The caller is responsible for calling ice_put_vf() to drop this
1272 static struct ice_vf
*ice_get_vf_from_pfq(struct ice_pf
*pf
, u16 pfq
)
1278 ice_for_each_vf_rcu(pf
, bkt
, vf
) {
1279 struct ice_vsi
*vsi
;
1282 vsi
= ice_get_vf_vsi(vf
);
1286 ice_for_each_rxq(vsi
, rxq_idx
)
1287 if (vsi
->rxq_map
[rxq_idx
] == pfq
) {
1288 struct ice_vf
*found
;
1290 if (kref_get_unless_zero(&vf
->refcnt
))
1304 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1305 * @pf: PF used for conversion
1306 * @globalq: global queue index used to convert to PF space queue index
1308 static u32
ice_globalq_to_pfq(struct ice_pf
*pf
, u32 globalq
)
1310 return globalq
- pf
->hw
.func_caps
.common_cap
.rxq_first_id
;
1314 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1315 * @pf: PF that the LAN overflow event happened on
1316 * @event: structure holding the event information for the LAN overflow event
1318 * Determine if the LAN overflow event was caused by a VF queue. If it was not
1319 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1320 * reset on the offending VF.
1323 ice_vf_lan_overflow_event(struct ice_pf
*pf
, struct ice_rq_event_info
*event
)
1325 u32 gldcb_rtctq
, queue
;
1328 gldcb_rtctq
= le32_to_cpu(event
->desc
.params
.lan_overflow
.prtdcb_ruptq
);
1329 dev_dbg(ice_pf_to_dev(pf
), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq
);
1331 /* event returns device global Rx queue number */
1332 queue
= (gldcb_rtctq
& GLDCB_RTCTQ_RXQNUM_M
) >>
1333 GLDCB_RTCTQ_RXQNUM_S
;
1335 vf
= ice_get_vf_from_pfq(pf
, ice_globalq_to_pfq(pf
, queue
));
1339 ice_reset_vf(vf
, ICE_VF_RESET_NOTIFY
| ICE_VF_RESET_LOCK
);
1344 * ice_set_vf_spoofchk
1345 * @netdev: network interface device structure
1346 * @vf_id: VF identifier
1347 * @ena: flag to enable or disable feature
1349 * Enable or disable VF spoof checking
1351 int ice_set_vf_spoofchk(struct net_device
*netdev
, int vf_id
, bool ena
)
1353 struct ice_netdev_priv
*np
= netdev_priv(netdev
);
1354 struct ice_pf
*pf
= np
->vsi
->back
;
1355 struct ice_vsi
*vf_vsi
;
1360 dev
= ice_pf_to_dev(pf
);
1362 vf
= ice_get_vf_by_id(pf
, vf_id
);
1366 ret
= ice_check_vf_ready_for_cfg(vf
);
1370 vf_vsi
= ice_get_vf_vsi(vf
);
1372 netdev_err(netdev
, "VSI %d for VF %d is null\n",
1373 vf
->lan_vsi_idx
, vf
->vf_id
);
1378 if (vf_vsi
->type
!= ICE_VSI_VF
) {
1379 netdev_err(netdev
, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
1380 vf_vsi
->type
, vf_vsi
->vsi_num
, vf
->vf_id
);
1385 if (ena
== vf
->spoofchk
) {
1386 dev_dbg(dev
, "VF spoofchk already %s\n", ena
? "ON" : "OFF");
1391 ret
= ice_vsi_apply_spoofchk(vf_vsi
, ena
);
1393 dev_err(dev
, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
1394 ena
? "ON" : "OFF", vf
->vf_id
, vf_vsi
->vsi_num
, ret
);
1405 * @netdev: network interface device structure
1406 * @vf_id: VF identifier
1407 * @ivi: VF configuration structure
1409 * return VF configuration
1412 ice_get_vf_cfg(struct net_device
*netdev
, int vf_id
, struct ifla_vf_info
*ivi
)
1414 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1418 vf
= ice_get_vf_by_id(pf
, vf_id
);
1422 ret
= ice_check_vf_ready_for_cfg(vf
);
1427 ether_addr_copy(ivi
->mac
, vf
->hw_lan_addr
);
1429 /* VF configuration for VLAN and applicable QoS */
1430 ivi
->vlan
= ice_vf_get_port_vlan_id(vf
);
1431 ivi
->qos
= ice_vf_get_port_vlan_prio(vf
);
1432 if (ice_vf_is_port_vlan_ena(vf
))
1433 ivi
->vlan_proto
= cpu_to_be16(ice_vf_get_port_vlan_tpid(vf
));
1435 ivi
->trusted
= vf
->trusted
;
1436 ivi
->spoofchk
= vf
->spoofchk
;
1437 if (!vf
->link_forced
)
1438 ivi
->linkstate
= IFLA_VF_LINK_STATE_AUTO
;
1439 else if (vf
->link_up
)
1440 ivi
->linkstate
= IFLA_VF_LINK_STATE_ENABLE
;
1442 ivi
->linkstate
= IFLA_VF_LINK_STATE_DISABLE
;
1443 ivi
->max_tx_rate
= vf
->max_tx_rate
;
1444 ivi
->min_tx_rate
= vf
->min_tx_rate
;
1453 * @netdev: network interface device structure
1454 * @vf_id: VF identifier
1457 * program VF MAC address
1459 int ice_set_vf_mac(struct net_device
*netdev
, int vf_id
, u8
*mac
)
1461 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1465 if (is_multicast_ether_addr(mac
)) {
1466 netdev_err(netdev
, "%pM not a valid unicast address\n", mac
);
1470 vf
= ice_get_vf_by_id(pf
, vf_id
);
1474 /* nothing left to do, unicast MAC already set */
1475 if (ether_addr_equal(vf
->dev_lan_addr
, mac
) &&
1476 ether_addr_equal(vf
->hw_lan_addr
, mac
)) {
1481 ret
= ice_check_vf_ready_for_cfg(vf
);
1485 mutex_lock(&vf
->cfg_lock
);
1487 /* VF is notified of its new MAC via the PF's response to the
1488 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
1490 ether_addr_copy(vf
->dev_lan_addr
, mac
);
1491 ether_addr_copy(vf
->hw_lan_addr
, mac
);
1492 if (is_zero_ether_addr(mac
)) {
1493 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
1494 vf
->pf_set_mac
= false;
1495 netdev_info(netdev
, "Removing MAC on VF %d. VF driver will be reinitialized\n",
1498 /* PF will add MAC rule for the VF */
1499 vf
->pf_set_mac
= true;
1500 netdev_info(netdev
, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
1504 ice_reset_vf(vf
, ICE_VF_RESET_NOTIFY
);
1505 mutex_unlock(&vf
->cfg_lock
);
1514 * @netdev: network interface device structure
1515 * @vf_id: VF identifier
1516 * @trusted: Boolean value to enable/disable trusted VF
1518 * Enable or disable a given VF as trusted
1520 int ice_set_vf_trust(struct net_device
*netdev
, int vf_id
, bool trusted
)
1522 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1526 vf
= ice_get_vf_by_id(pf
, vf_id
);
1530 if (ice_is_eswitch_mode_switchdev(pf
)) {
1531 dev_info(ice_pf_to_dev(pf
), "Trusted VF is forbidden in switchdev mode\n");
1535 ret
= ice_check_vf_ready_for_cfg(vf
);
1539 /* Check if already trusted */
1540 if (trusted
== vf
->trusted
) {
1545 mutex_lock(&vf
->cfg_lock
);
1547 vf
->trusted
= trusted
;
1548 ice_reset_vf(vf
, ICE_VF_RESET_NOTIFY
);
1549 dev_info(ice_pf_to_dev(pf
), "VF %u is now %strusted\n",
1550 vf_id
, trusted
? "" : "un");
1552 mutex_unlock(&vf
->cfg_lock
);
1560 * ice_set_vf_link_state
1561 * @netdev: network interface device structure
1562 * @vf_id: VF identifier
1563 * @link_state: required link state
1565 * Set VF's link state, irrespective of physical link state status
1567 int ice_set_vf_link_state(struct net_device
*netdev
, int vf_id
, int link_state
)
1569 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1573 vf
= ice_get_vf_by_id(pf
, vf_id
);
1577 ret
= ice_check_vf_ready_for_cfg(vf
);
1581 switch (link_state
) {
1582 case IFLA_VF_LINK_STATE_AUTO
:
1583 vf
->link_forced
= false;
1585 case IFLA_VF_LINK_STATE_ENABLE
:
1586 vf
->link_forced
= true;
1589 case IFLA_VF_LINK_STATE_DISABLE
:
1590 vf
->link_forced
= true;
1591 vf
->link_up
= false;
1598 ice_vc_notify_vf_link_state(vf
);
1606 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
1607 * @pf: PF associated with VFs
1609 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf
*pf
)
1616 ice_for_each_vf_rcu(pf
, bkt
, vf
)
1617 rate
+= vf
->min_tx_rate
;
1624 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
1625 * @vf: VF trying to configure min_tx_rate
1626 * @min_tx_rate: min Tx rate in Mbps
1628 * Check if the min_tx_rate being passed in will cause oversubscription of total
1629 * min_tx_rate based on the current link speed and all other VFs configured
1632 * Return true if the passed min_tx_rate would cause oversubscription, else
1636 ice_min_tx_rate_oversubscribed(struct ice_vf
*vf
, int min_tx_rate
)
1638 struct ice_vsi
*vsi
= ice_get_vf_vsi(vf
);
1639 int all_vfs_min_tx_rate
;
1640 int link_speed_mbps
;
1645 link_speed_mbps
= ice_get_link_speed_mbps(vsi
);
1646 all_vfs_min_tx_rate
= ice_calc_all_vfs_min_tx_rate(vf
->pf
);
1648 /* this VF's previous rate is being overwritten */
1649 all_vfs_min_tx_rate
-= vf
->min_tx_rate
;
1651 if (all_vfs_min_tx_rate
+ min_tx_rate
> link_speed_mbps
) {
1652 dev_err(ice_pf_to_dev(vf
->pf
), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n",
1653 min_tx_rate
, vf
->vf_id
,
1654 all_vfs_min_tx_rate
+ min_tx_rate
- link_speed_mbps
,
1663 * ice_set_vf_bw - set min/max VF bandwidth
1664 * @netdev: network interface device structure
1665 * @vf_id: VF identifier
1666 * @min_tx_rate: Minimum Tx rate in Mbps
1667 * @max_tx_rate: Maximum Tx rate in Mbps
1670 ice_set_vf_bw(struct net_device
*netdev
, int vf_id
, int min_tx_rate
,
1673 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1674 struct ice_vsi
*vsi
;
1679 dev
= ice_pf_to_dev(pf
);
1681 vf
= ice_get_vf_by_id(pf
, vf_id
);
1685 ret
= ice_check_vf_ready_for_cfg(vf
);
1689 vsi
= ice_get_vf_vsi(vf
);
1695 if (min_tx_rate
&& ice_is_dcb_active(pf
)) {
1696 dev_err(dev
, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
1701 if (ice_min_tx_rate_oversubscribed(vf
, min_tx_rate
)) {
1706 if (vf
->min_tx_rate
!= (unsigned int)min_tx_rate
) {
1707 ret
= ice_set_min_bw_limit(vsi
, (u64
)min_tx_rate
* 1000);
1709 dev_err(dev
, "Unable to set min-tx-rate for VF %d\n",
1714 vf
->min_tx_rate
= min_tx_rate
;
1717 if (vf
->max_tx_rate
!= (unsigned int)max_tx_rate
) {
1718 ret
= ice_set_max_bw_limit(vsi
, (u64
)max_tx_rate
* 1000);
1720 dev_err(dev
, "Unable to set max-tx-rate for VF %d\n",
1725 vf
->max_tx_rate
= max_tx_rate
;
1734 * ice_get_vf_stats - populate some stats for the VF
1735 * @netdev: the netdev of the PF
1736 * @vf_id: the host OS identifier (0-255)
1737 * @vf_stats: pointer to the OS memory to be initialized
1739 int ice_get_vf_stats(struct net_device
*netdev
, int vf_id
,
1740 struct ifla_vf_stats
*vf_stats
)
1742 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1743 struct ice_eth_stats
*stats
;
1744 struct ice_vsi
*vsi
;
1748 vf
= ice_get_vf_by_id(pf
, vf_id
);
1752 ret
= ice_check_vf_ready_for_cfg(vf
);
1756 vsi
= ice_get_vf_vsi(vf
);
1762 ice_update_eth_stats(vsi
);
1763 stats
= &vsi
->eth_stats
;
1765 memset(vf_stats
, 0, sizeof(*vf_stats
));
1767 vf_stats
->rx_packets
= stats
->rx_unicast
+ stats
->rx_broadcast
+
1768 stats
->rx_multicast
;
1769 vf_stats
->tx_packets
= stats
->tx_unicast
+ stats
->tx_broadcast
+
1770 stats
->tx_multicast
;
1771 vf_stats
->rx_bytes
= stats
->rx_bytes
;
1772 vf_stats
->tx_bytes
= stats
->tx_bytes
;
1773 vf_stats
->broadcast
= stats
->rx_broadcast
;
1774 vf_stats
->multicast
= stats
->rx_multicast
;
1775 vf_stats
->rx_dropped
= stats
->rx_discards
;
1776 vf_stats
->tx_dropped
= stats
->tx_discards
;
1784 * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
1785 * @hw: hardware structure used to check the VLAN mode
1786 * @vlan_proto: VLAN TPID being checked
1788 * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
1789 * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
1790 * Mode (SVM), then only ETH_P_8021Q is supported.
1793 ice_is_supported_port_vlan_proto(struct ice_hw
*hw
, u16 vlan_proto
)
1795 bool is_supported
= false;
1797 switch (vlan_proto
) {
1799 is_supported
= true;
1802 if (ice_is_dvm_ena(hw
))
1803 is_supported
= true;
1807 return is_supported
;
1811 * ice_set_vf_port_vlan
1812 * @netdev: network interface device structure
1813 * @vf_id: VF identifier
1814 * @vlan_id: VLAN ID being set
1815 * @qos: priority setting
1816 * @vlan_proto: VLAN protocol
1818 * program VF Port VLAN ID and/or QoS
1821 ice_set_vf_port_vlan(struct net_device
*netdev
, int vf_id
, u16 vlan_id
, u8 qos
,
1824 struct ice_pf
*pf
= ice_netdev_to_pf(netdev
);
1825 u16 local_vlan_proto
= ntohs(vlan_proto
);
1830 dev
= ice_pf_to_dev(pf
);
1832 if (vlan_id
>= VLAN_N_VID
|| qos
> 7) {
1833 dev_err(dev
, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
1834 vf_id
, vlan_id
, qos
);
1838 if (!ice_is_supported_port_vlan_proto(&pf
->hw
, local_vlan_proto
)) {
1839 dev_err(dev
, "VF VLAN protocol 0x%04x is not supported\n",
1841 return -EPROTONOSUPPORT
;
1844 vf
= ice_get_vf_by_id(pf
, vf_id
);
1848 ret
= ice_check_vf_ready_for_cfg(vf
);
1852 if (ice_vf_get_port_vlan_prio(vf
) == qos
&&
1853 ice_vf_get_port_vlan_tpid(vf
) == local_vlan_proto
&&
1854 ice_vf_get_port_vlan_id(vf
) == vlan_id
) {
1855 /* duplicate request, so just return success */
1856 dev_dbg(dev
, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
1857 vlan_id
, qos
, local_vlan_proto
);
1862 mutex_lock(&vf
->cfg_lock
);
1864 vf
->port_vlan_info
= ICE_VLAN(local_vlan_proto
, vlan_id
, qos
);
1865 if (ice_vf_is_port_vlan_ena(vf
))
1866 dev_info(dev
, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
1867 vlan_id
, qos
, local_vlan_proto
, vf_id
);
1869 dev_info(dev
, "Clearing port VLAN on VF %d\n", vf_id
);
1871 ice_reset_vf(vf
, ICE_VF_RESET_NOTIFY
);
1872 mutex_unlock(&vf
->cfg_lock
);
1880 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
1881 * @vf: pointer to the VF structure
1883 void ice_print_vf_rx_mdd_event(struct ice_vf
*vf
)
1885 struct ice_pf
*pf
= vf
->pf
;
1888 dev
= ice_pf_to_dev(pf
);
1890 dev_info(dev
, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
1891 vf
->mdd_rx_events
.count
, pf
->hw
.pf_id
, vf
->vf_id
,
1893 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF
, pf
->flags
)
1898 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
1899 * @pf: pointer to the PF structure
1901 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
1903 void ice_print_vfs_mdd_events(struct ice_pf
*pf
)
1905 struct device
*dev
= ice_pf_to_dev(pf
);
1906 struct ice_hw
*hw
= &pf
->hw
;
1910 /* check that there are pending MDD events to print */
1911 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING
, pf
->state
))
1914 /* VF MDD event logs are rate limited to one second intervals */
1915 if (time_is_after_jiffies(pf
->vfs
.last_printed_mdd_jiffies
+ HZ
* 1))
1918 pf
->vfs
.last_printed_mdd_jiffies
= jiffies
;
1920 mutex_lock(&pf
->vfs
.table_lock
);
1921 ice_for_each_vf(pf
, bkt
, vf
) {
1922 /* only print Rx MDD event message if there are new events */
1923 if (vf
->mdd_rx_events
.count
!= vf
->mdd_rx_events
.last_printed
) {
1924 vf
->mdd_rx_events
.last_printed
=
1925 vf
->mdd_rx_events
.count
;
1926 ice_print_vf_rx_mdd_event(vf
);
1929 /* only print Tx MDD event message if there are new events */
1930 if (vf
->mdd_tx_events
.count
!= vf
->mdd_tx_events
.last_printed
) {
1931 vf
->mdd_tx_events
.last_printed
=
1932 vf
->mdd_tx_events
.count
;
1934 dev_info(dev
, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
1935 vf
->mdd_tx_events
.count
, hw
->pf_id
, vf
->vf_id
,
1939 mutex_unlock(&pf
->vfs
.table_lock
);
1943 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
1944 * @pf: pointer to the PF structure
1946 * Called when recovering from a PF FLR to restore interrupt capability to
1949 void ice_restore_all_vfs_msi_state(struct ice_pf
*pf
)
1954 ice_for_each_vf(pf
, bkt
, vf
)
1955 pci_restore_msi_state(vf
->vfdev
);