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 + pf->vfs.num_msix_per - 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 + pf->vfs.num_msix_per - 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)
138 struct ice_res_tracker *res;
143 res = pf->irq_tracker;
147 /* give back irq_tracker resources used */
148 WARN_ON(pf->sriov_base_vector < res->num_entries);
150 pf->sriov_base_vector = 0;
156 * ice_free_vfs - Free all VFs
157 * @pf: pointer to the PF structure
159 void ice_free_vfs(struct ice_pf *pf)
161 struct device *dev = ice_pf_to_dev(pf);
162 struct ice_vfs *vfs = &pf->vfs;
163 struct ice_hw *hw = &pf->hw;
167 if (!ice_has_vfs(pf))
170 while (test_and_set_bit(ICE_VF_DIS, pf->state))
171 usleep_range(1000, 2000);
173 /* Disable IOV before freeing resources. This lets any VF drivers
174 * running in the host get themselves cleaned up before we yank
175 * the carpet out from underneath their feet.
177 if (!pci_vfs_assigned(pf->pdev))
178 pci_disable_sriov(pf->pdev);
180 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
182 mutex_lock(&vfs->table_lock);
184 ice_eswitch_release(pf);
186 ice_for_each_vf(pf, bkt, vf) {
187 mutex_lock(&vf->cfg_lock);
191 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
192 /* disable VF qp mappings and set VF disable state */
193 ice_dis_vf_mappings(vf);
194 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
198 if (!pci_vfs_assigned(pf->pdev)) {
199 u32 reg_idx, bit_idx;
201 reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
202 bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
203 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
206 /* clear malicious info since the VF is getting released */
207 list_del(&vf->mbx_info.list_entry);
209 mutex_unlock(&vf->cfg_lock);
212 if (ice_sriov_free_msix_res(pf))
213 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
215 vfs->num_qps_per = 0;
216 ice_free_vf_entries(pf);
218 mutex_unlock(&vfs->table_lock);
220 clear_bit(ICE_VF_DIS, pf->state);
221 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
225 * ice_vf_vsi_setup - Set up a VF VSI
226 * @vf: VF to setup VSI for
228 * Returns pointer to the successfully allocated VSI struct on success,
229 * otherwise returns NULL on failure.
231 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
233 struct ice_vsi_cfg_params params = {};
234 struct ice_pf *pf = vf->pf;
237 params.type = ICE_VSI_VF;
238 params.pi = ice_vf_get_port_info(vf);
240 params.flags = ICE_VSI_FLAG_INIT;
242 vsi = ice_vsi_setup(pf, ¶ms);
245 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
246 ice_vf_invalidate_vsi(vf);
250 vf->lan_vsi_idx = vsi->idx;
251 vf->lan_vsi_num = vsi->vsi_num;
257 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
258 * @pf: pointer to PF structure
259 * @vf: pointer to VF that the first MSIX vector index is being calculated for
261 * This returns the first MSIX vector index in PF space that is used by this VF.
262 * This index is used when accessing PF relative registers such as
263 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
264 * This will always be the OICR index in the AVF driver so any functionality
265 * using vf->first_vector_idx for queue configuration will have to increment by
266 * 1 to avoid meddling with the OICR index.
268 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
270 return pf->sriov_base_vector + vf->vf_id * pf->vfs.num_msix_per;
274 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
275 * @vf: VF to enable MSIX mappings for
277 * Some of the registers need to be indexed/configured using hardware global
278 * device values and other registers need 0-based values, which represent PF
281 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
283 int device_based_first_msix, device_based_last_msix;
284 int pf_based_first_msix, pf_based_last_msix, v;
285 struct ice_pf *pf = vf->pf;
286 int device_based_vf_id;
291 pf_based_first_msix = vf->first_vector_idx;
292 pf_based_last_msix = (pf_based_first_msix + pf->vfs.num_msix_per) - 1;
294 device_based_first_msix = pf_based_first_msix +
295 pf->hw.func_caps.common_cap.msix_vector_first_id;
296 device_based_last_msix =
297 (device_based_first_msix + pf->vfs.num_msix_per) - 1;
298 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
300 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
301 VPINT_ALLOC_FIRST_M) |
302 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
303 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
304 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
306 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
307 & VPINT_ALLOC_PCI_FIRST_M) |
308 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
309 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
310 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
312 /* map the interrupts to its functions */
313 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
314 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
315 GLINT_VECT2FUNC_VF_NUM_M) |
316 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
317 GLINT_VECT2FUNC_PF_NUM_M));
318 wr32(hw, GLINT_VECT2FUNC(v), reg);
321 /* Map mailbox interrupt to VF MSI-X vector 0 */
322 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
326 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
327 * @vf: VF to enable the mappings for
328 * @max_txq: max Tx queues allowed on the VF's VSI
329 * @max_rxq: max Rx queues allowed on the VF's VSI
331 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
333 struct device *dev = ice_pf_to_dev(vf->pf);
334 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
335 struct ice_hw *hw = &vf->pf->hw;
341 /* set regardless of mapping mode */
342 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
344 /* VF Tx queues allocation */
345 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
346 /* set the VF PF Tx queue range
347 * VFNUMQ value should be set to (number of queues - 1). A value
348 * of 0 means 1 queue and a value of 255 means 256 queues
350 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
351 VPLAN_TX_QBASE_VFFIRSTQ_M) |
352 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
353 VPLAN_TX_QBASE_VFNUMQ_M));
354 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
356 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
359 /* set regardless of mapping mode */
360 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
362 /* VF Rx queues allocation */
363 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
364 /* set the VF PF Rx queue range
365 * VFNUMQ value should be set to (number of queues - 1). A value
366 * of 0 means 1 queue and a value of 255 means 256 queues
368 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
369 VPLAN_RX_QBASE_VFFIRSTQ_M) |
370 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
371 VPLAN_RX_QBASE_VFNUMQ_M));
372 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
374 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
379 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
380 * @vf: pointer to the VF structure
382 static void ice_ena_vf_mappings(struct ice_vf *vf)
384 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
389 ice_ena_vf_msix_mappings(vf);
390 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
394 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
395 * @vf: VF to calculate the register index for
396 * @q_vector: a q_vector associated to the VF
398 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
402 if (!vf || !q_vector)
407 /* always add one to account for the OICR being the first MSIX */
408 return pf->sriov_base_vector + pf->vfs.num_msix_per * vf->vf_id +
413 * ice_get_max_valid_res_idx - Get the max valid resource index
414 * @res: pointer to the resource to find the max valid index for
416 * Start from the end of the ice_res_tracker and return right when we find the
417 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
418 * valid for SR-IOV because it is the only consumer that manipulates the
419 * res->end and this is always called when res->end is set to res->num_entries.
421 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
428 for (i = res->num_entries - 1; i >= 0; i--)
429 if (res->list[i] & ICE_RES_VALID_BIT)
436 * ice_sriov_set_msix_res - Set any used MSIX resources
437 * @pf: pointer to PF structure
438 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
440 * This function allows SR-IOV resources to be taken from the end of the PF's
441 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
442 * just set the pf->sriov_base_vector and return success.
444 * If there are not enough resources available, return an error. This should
445 * always be caught by ice_set_per_vf_res().
447 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
448 * in the PF's space available for SR-IOV.
450 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
452 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
453 int vectors_used = pf->irq_tracker->num_entries;
454 int sriov_base_vector;
456 sriov_base_vector = total_vectors - num_msix_needed;
458 /* make sure we only grab irq_tracker entries from the list end and
459 * that we have enough available MSIX vectors
461 if (sriov_base_vector < vectors_used)
464 pf->sriov_base_vector = sriov_base_vector;
470 * ice_set_per_vf_res - check if vectors and queues are available
471 * @pf: pointer to the PF structure
472 * @num_vfs: the number of SR-IOV VFs being configured
474 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
475 * get more vectors and can enable more queues per VF. Note that this does not
476 * grab any vectors from the SW pool already allocated. Also note, that all
477 * vector counts include one for each VF's miscellaneous interrupt vector
480 * Minimum VFs - 2 vectors, 1 queue pair
481 * Small VFs - 5 vectors, 4 queue pairs
482 * Medium VFs - 17 vectors, 16 queue pairs
484 * Second, determine number of queue pairs per VF by starting with a pre-defined
485 * maximum each VF supports. If this is not possible, then we adjust based on
486 * queue pairs available on the device.
488 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
489 * by each VF during VF initialization and reset.
491 static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs)
493 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
494 u16 num_msix_per_vf, num_txq, num_rxq, avail_qs;
495 int msix_avail_per_vf, msix_avail_for_sriov;
496 struct device *dev = ice_pf_to_dev(pf);
499 lockdep_assert_held(&pf->vfs.table_lock);
504 if (max_valid_res_idx < 0)
507 /* determine MSI-X resources per VF */
508 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
509 pf->irq_tracker->num_entries;
510 msix_avail_per_vf = msix_avail_for_sriov / num_vfs;
511 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
512 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
513 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
514 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
515 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
516 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
517 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
518 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
520 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",
521 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
526 num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
527 ICE_MAX_RSS_QS_PER_VF);
528 avail_qs = ice_get_avail_txq_count(pf) / num_vfs;
531 else if (num_txq > avail_qs)
532 num_txq = rounddown_pow_of_two(avail_qs);
534 num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
535 ICE_MAX_RSS_QS_PER_VF);
536 avail_qs = ice_get_avail_rxq_count(pf) / num_vfs;
539 else if (num_rxq > avail_qs)
540 num_rxq = rounddown_pow_of_two(avail_qs);
542 if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) {
543 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
544 ICE_MIN_QS_PER_VF, num_vfs);
548 err = ice_sriov_set_msix_res(pf, num_msix_per_vf * num_vfs);
550 dev_err(dev, "Unable to set MSI-X resources for %d VFs, err %d\n",
555 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
556 pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq);
557 pf->vfs.num_msix_per = num_msix_per_vf;
558 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
559 num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per);
565 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
566 * @vf: VF to initialize/setup the VSI for
568 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
569 * VF VSI's broadcast filter and is only used during initial VF creation.
571 static int ice_init_vf_vsi_res(struct ice_vf *vf)
573 struct ice_pf *pf = vf->pf;
577 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
579 vsi = ice_vf_vsi_setup(vf);
583 err = ice_vf_init_host_cfg(vf, vsi);
590 ice_vf_vsi_release(vf);
595 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
596 * @pf: PF the VFs are associated with
598 static int ice_start_vfs(struct ice_pf *pf)
600 struct ice_hw *hw = &pf->hw;
601 unsigned int bkt, it_cnt;
605 lockdep_assert_held(&pf->vfs.table_lock);
608 ice_for_each_vf(pf, bkt, vf) {
609 vf->vf_ops->clear_reset_trigger(vf);
611 retval = ice_init_vf_vsi_res(vf);
613 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
618 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
619 ice_ena_vf_mappings(vf);
620 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
628 ice_for_each_vf(pf, bkt, vf) {
632 ice_dis_vf_mappings(vf);
633 ice_vf_vsi_release(vf);
641 * ice_sriov_free_vf - Free VF memory after all references are dropped
642 * @vf: pointer to VF to free
644 * Called by ice_put_vf through ice_release_vf once the last reference to a VF
645 * structure has been dropped.
647 static void ice_sriov_free_vf(struct ice_vf *vf)
649 mutex_destroy(&vf->cfg_lock);
655 * ice_sriov_clear_reset_state - clears VF Reset status register
656 * @vf: the vf to configure
658 static void ice_sriov_clear_reset_state(struct ice_vf *vf)
660 struct ice_hw *hw = &vf->pf->hw;
662 /* Clear the reset status register so that VF immediately sees that
663 * the device is resetting, even if hardware hasn't yet gotten around
664 * to clearing VFGEN_RSTAT for us.
666 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_INPROGRESS);
670 * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers
671 * @vf: the vf to configure
673 static void ice_sriov_clear_mbx_register(struct ice_vf *vf)
675 struct ice_pf *pf = vf->pf;
677 wr32(&pf->hw, VF_MBX_ARQLEN(vf->vf_id), 0);
678 wr32(&pf->hw, VF_MBX_ATQLEN(vf->vf_id), 0);
682 * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF
683 * @vf: pointer to VF structure
684 * @is_vflr: true if reset occurred due to VFLR
686 * Trigger and cleanup after a VF reset for a SR-IOV VF.
688 static void ice_sriov_trigger_reset_register(struct ice_vf *vf, bool is_vflr)
690 struct ice_pf *pf = vf->pf;
691 u32 reg, reg_idx, bit_idx;
692 unsigned int vf_abs_id, i;
696 dev = ice_pf_to_dev(pf);
698 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
700 /* In the case of a VFLR, HW has already reset the VF and we just need
701 * to clean up. Otherwise we must first trigger the reset using the
705 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
706 reg |= VPGEN_VFRTRIG_VFSWR_M;
707 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
710 /* clear the VFLR bit in GLGEN_VFLRSTAT */
711 reg_idx = (vf_abs_id) / 32;
712 bit_idx = (vf_abs_id) % 32;
713 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
716 wr32(hw, PF_PCI_CIAA,
717 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
718 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
719 reg = rd32(hw, PF_PCI_CIAD);
720 /* no transactions pending so stop polling */
721 if ((reg & VF_TRANS_PENDING_M) == 0)
724 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
725 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
730 * ice_sriov_poll_reset_status - poll SRIOV VF reset status
731 * @vf: pointer to VF structure
733 * Returns true when reset is successful, else returns false
735 static bool ice_sriov_poll_reset_status(struct ice_vf *vf)
737 struct ice_pf *pf = vf->pf;
741 for (i = 0; i < 10; i++) {
742 /* VF reset requires driver to first reset the VF and then
743 * poll the status register to make sure that the reset
744 * completed successfully.
746 reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id));
747 if (reg & VPGEN_VFRSTAT_VFRD_M)
750 /* only sleep if the reset is not done */
751 usleep_range(10, 20);
757 * ice_sriov_clear_reset_trigger - enable VF to access hardware
758 * @vf: VF to enabled hardware access for
760 static void ice_sriov_clear_reset_trigger(struct ice_vf *vf)
762 struct ice_hw *hw = &vf->pf->hw;
765 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
766 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
767 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
772 * ice_sriov_create_vsi - Create a new VSI for a VF
773 * @vf: VF to create the VSI for
775 * This is called by ice_vf_recreate_vsi to create the new VSI after the old
776 * VSI has been released.
778 static int ice_sriov_create_vsi(struct ice_vf *vf)
782 vsi = ice_vf_vsi_setup(vf);
790 * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
791 * @vf: VF to perform tasks on
793 static void ice_sriov_post_vsi_rebuild(struct ice_vf *vf)
795 ice_ena_vf_mappings(vf);
796 wr32(&vf->pf->hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
799 static const struct ice_vf_ops ice_sriov_vf_ops = {
800 .reset_type = ICE_VF_RESET,
801 .free = ice_sriov_free_vf,
802 .clear_reset_state = ice_sriov_clear_reset_state,
803 .clear_mbx_register = ice_sriov_clear_mbx_register,
804 .trigger_reset_register = ice_sriov_trigger_reset_register,
805 .poll_reset_status = ice_sriov_poll_reset_status,
806 .clear_reset_trigger = ice_sriov_clear_reset_trigger,
808 .create_vsi = ice_sriov_create_vsi,
809 .post_vsi_rebuild = ice_sriov_post_vsi_rebuild,
813 * ice_create_vf_entries - Allocate and insert VF entries
814 * @pf: pointer to the PF structure
815 * @num_vfs: the number of VFs to allocate
817 * Allocate new VF entries and insert them into the hash table. Set some
818 * basic default fields for initializing the new VFs.
820 * After this function exits, the hash table will have num_vfs entries
823 * Returns 0 on success or an integer error code on failure.
825 static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs)
827 struct ice_vfs *vfs = &pf->vfs;
832 lockdep_assert_held(&vfs->table_lock);
834 for (vf_id = 0; vf_id < num_vfs; vf_id++) {
835 vf = kzalloc(sizeof(*vf), GFP_KERNEL);
838 goto err_free_entries;
840 kref_init(&vf->refcnt);
845 /* set sriov vf ops for VFs created during SRIOV flow */
846 vf->vf_ops = &ice_sriov_vf_ops;
848 ice_initialize_vf_entry(vf);
850 vf->vf_sw_id = pf->first_sw;
852 hash_add_rcu(vfs->table, &vf->entry, vf_id);
858 ice_free_vf_entries(pf);
863 * ice_ena_vfs - enable VFs so they are ready to be used
864 * @pf: pointer to the PF structure
865 * @num_vfs: number of VFs to enable
867 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
869 struct device *dev = ice_pf_to_dev(pf);
870 struct ice_hw *hw = &pf->hw;
873 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
874 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
875 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
876 set_bit(ICE_OICR_INTR_DIS, pf->state);
879 ret = pci_enable_sriov(pf->pdev, num_vfs);
881 goto err_unroll_intr;
883 mutex_lock(&pf->vfs.table_lock);
885 ret = ice_set_per_vf_res(pf, num_vfs);
887 dev_err(dev, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n",
889 goto err_unroll_sriov;
892 ret = ice_create_vf_entries(pf, num_vfs);
894 dev_err(dev, "Failed to allocate VF entries for %d VFs\n",
896 goto err_unroll_sriov;
899 ret = ice_start_vfs(pf);
901 dev_err(dev, "Failed to start %d VFs, err %d\n", num_vfs, ret);
903 goto err_unroll_vf_entries;
906 clear_bit(ICE_VF_DIS, pf->state);
908 ret = ice_eswitch_configure(pf);
910 dev_err(dev, "Failed to configure eswitch, err %d\n", ret);
911 goto err_unroll_sriov;
914 /* rearm global interrupts */
915 if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
916 ice_irq_dynamic_ena(hw, NULL, NULL);
918 mutex_unlock(&pf->vfs.table_lock);
922 err_unroll_vf_entries:
923 ice_free_vf_entries(pf);
925 mutex_unlock(&pf->vfs.table_lock);
926 pci_disable_sriov(pf->pdev);
928 /* rearm interrupts here */
929 ice_irq_dynamic_ena(hw, NULL, NULL);
930 clear_bit(ICE_OICR_INTR_DIS, pf->state);
935 * ice_pci_sriov_ena - Enable or change number of VFs
936 * @pf: pointer to the PF structure
937 * @num_vfs: number of VFs to allocate
939 * Returns 0 on success and negative on failure
941 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
943 int pre_existing_vfs = pci_num_vf(pf->pdev);
944 struct device *dev = ice_pf_to_dev(pf);
947 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
949 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
952 if (num_vfs > pf->vfs.num_supported) {
953 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
954 num_vfs, pf->vfs.num_supported);
958 dev_info(dev, "Enabling %d VFs\n", num_vfs);
959 err = ice_ena_vfs(pf, num_vfs);
961 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
965 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
970 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
971 * @pf: PF to enabled SR-IOV on
973 static int ice_check_sriov_allowed(struct ice_pf *pf)
975 struct device *dev = ice_pf_to_dev(pf);
977 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
978 dev_err(dev, "This device is not capable of SR-IOV\n");
982 if (ice_is_safe_mode(pf)) {
983 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
987 if (!ice_pf_state_is_nominal(pf)) {
988 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
996 * ice_sriov_configure - Enable or change number of VFs via sysfs
997 * @pdev: pointer to a pci_dev structure
998 * @num_vfs: number of VFs to allocate or 0 to free VFs
1000 * This function is called when the user updates the number of VFs in sysfs. On
1001 * success return whatever num_vfs was set to by the caller. Return negative on
1004 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1006 struct ice_pf *pf = pci_get_drvdata(pdev);
1007 struct device *dev = ice_pf_to_dev(pf);
1010 err = ice_check_sriov_allowed(pf);
1015 if (!pci_vfs_assigned(pdev)) {
1018 ice_enable_lag(pf->lag);
1022 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1026 err = ice_pci_sriov_ena(pf, num_vfs);
1031 ice_disable_lag(pf->lag);
1036 * ice_process_vflr_event - Free VF resources via IRQ calls
1037 * @pf: pointer to the PF structure
1039 * called from the VFLR IRQ handler to
1040 * free up VF resources and state variables
1042 void ice_process_vflr_event(struct ice_pf *pf)
1044 struct ice_hw *hw = &pf->hw;
1049 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
1053 mutex_lock(&pf->vfs.table_lock);
1054 ice_for_each_vf(pf, bkt, vf) {
1055 u32 reg_idx, bit_idx;
1057 reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1058 bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1059 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1060 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1061 if (reg & BIT(bit_idx))
1062 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1063 ice_reset_vf(vf, ICE_VF_RESET_VFLR | ICE_VF_RESET_LOCK);
1065 mutex_unlock(&pf->vfs.table_lock);
1069 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1070 * @pf: PF used to index all VFs
1071 * @pfq: queue index relative to the PF's function space
1073 * If no VF is found who owns the pfq then return NULL, otherwise return a
1074 * pointer to the VF who owns the pfq
1076 * If this function returns non-NULL, it acquires a reference count of the VF
1077 * structure. The caller is responsible for calling ice_put_vf() to drop this
1080 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1086 ice_for_each_vf_rcu(pf, bkt, vf) {
1087 struct ice_vsi *vsi;
1090 vsi = ice_get_vf_vsi(vf);
1094 ice_for_each_rxq(vsi, rxq_idx)
1095 if (vsi->rxq_map[rxq_idx] == pfq) {
1096 struct ice_vf *found;
1098 if (kref_get_unless_zero(&vf->refcnt))
1112 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1113 * @pf: PF used for conversion
1114 * @globalq: global queue index used to convert to PF space queue index
1116 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1118 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1122 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1123 * @pf: PF that the LAN overflow event happened on
1124 * @event: structure holding the event information for the LAN overflow event
1126 * Determine if the LAN overflow event was caused by a VF queue. If it was not
1127 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1128 * reset on the offending VF.
1131 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1133 u32 gldcb_rtctq, queue;
1136 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1137 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1139 /* event returns device global Rx queue number */
1140 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
1141 GLDCB_RTCTQ_RXQNUM_S;
1143 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1147 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1152 * ice_set_vf_spoofchk
1153 * @netdev: network interface device structure
1154 * @vf_id: VF identifier
1155 * @ena: flag to enable or disable feature
1157 * Enable or disable VF spoof checking
1159 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
1161 struct ice_netdev_priv *np = netdev_priv(netdev);
1162 struct ice_pf *pf = np->vsi->back;
1163 struct ice_vsi *vf_vsi;
1168 dev = ice_pf_to_dev(pf);
1170 vf = ice_get_vf_by_id(pf, vf_id);
1174 ret = ice_check_vf_ready_for_cfg(vf);
1178 vf_vsi = ice_get_vf_vsi(vf);
1180 netdev_err(netdev, "VSI %d for VF %d is null\n",
1181 vf->lan_vsi_idx, vf->vf_id);
1186 if (vf_vsi->type != ICE_VSI_VF) {
1187 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
1188 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
1193 if (ena == vf->spoofchk) {
1194 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
1199 ret = ice_vsi_apply_spoofchk(vf_vsi, ena);
1201 dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
1202 ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret);
1213 * @netdev: network interface device structure
1214 * @vf_id: VF identifier
1215 * @ivi: VF configuration structure
1217 * return VF configuration
1220 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
1222 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1226 vf = ice_get_vf_by_id(pf, vf_id);
1230 ret = ice_check_vf_ready_for_cfg(vf);
1235 ether_addr_copy(ivi->mac, vf->hw_lan_addr);
1237 /* VF configuration for VLAN and applicable QoS */
1238 ivi->vlan = ice_vf_get_port_vlan_id(vf);
1239 ivi->qos = ice_vf_get_port_vlan_prio(vf);
1240 if (ice_vf_is_port_vlan_ena(vf))
1241 ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf));
1243 ivi->trusted = vf->trusted;
1244 ivi->spoofchk = vf->spoofchk;
1245 if (!vf->link_forced)
1246 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
1247 else if (vf->link_up)
1248 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
1250 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
1251 ivi->max_tx_rate = vf->max_tx_rate;
1252 ivi->min_tx_rate = vf->min_tx_rate;
1261 * @netdev: network interface device structure
1262 * @vf_id: VF identifier
1265 * program VF MAC address
1267 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1269 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1273 if (is_multicast_ether_addr(mac)) {
1274 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
1278 vf = ice_get_vf_by_id(pf, vf_id);
1282 /* nothing left to do, unicast MAC already set */
1283 if (ether_addr_equal(vf->dev_lan_addr, mac) &&
1284 ether_addr_equal(vf->hw_lan_addr, mac)) {
1289 ret = ice_check_vf_ready_for_cfg(vf);
1293 mutex_lock(&vf->cfg_lock);
1295 /* VF is notified of its new MAC via the PF's response to the
1296 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
1298 ether_addr_copy(vf->dev_lan_addr, mac);
1299 ether_addr_copy(vf->hw_lan_addr, mac);
1300 if (is_zero_ether_addr(mac)) {
1301 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
1302 vf->pf_set_mac = false;
1303 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
1306 /* PF will add MAC rule for the VF */
1307 vf->pf_set_mac = true;
1308 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
1312 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1313 mutex_unlock(&vf->cfg_lock);
1322 * @netdev: network interface device structure
1323 * @vf_id: VF identifier
1324 * @trusted: Boolean value to enable/disable trusted VF
1326 * Enable or disable a given VF as trusted
1328 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
1330 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1334 if (ice_is_eswitch_mode_switchdev(pf)) {
1335 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
1339 vf = ice_get_vf_by_id(pf, vf_id);
1343 ret = ice_check_vf_ready_for_cfg(vf);
1347 /* Check if already trusted */
1348 if (trusted == vf->trusted) {
1353 mutex_lock(&vf->cfg_lock);
1355 vf->trusted = trusted;
1356 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1357 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
1358 vf_id, trusted ? "" : "un");
1360 mutex_unlock(&vf->cfg_lock);
1368 * ice_set_vf_link_state
1369 * @netdev: network interface device structure
1370 * @vf_id: VF identifier
1371 * @link_state: required link state
1373 * Set VF's link state, irrespective of physical link state status
1375 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
1377 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1381 vf = ice_get_vf_by_id(pf, vf_id);
1385 ret = ice_check_vf_ready_for_cfg(vf);
1389 switch (link_state) {
1390 case IFLA_VF_LINK_STATE_AUTO:
1391 vf->link_forced = false;
1393 case IFLA_VF_LINK_STATE_ENABLE:
1394 vf->link_forced = true;
1397 case IFLA_VF_LINK_STATE_DISABLE:
1398 vf->link_forced = true;
1399 vf->link_up = false;
1406 ice_vc_notify_vf_link_state(vf);
1414 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
1415 * @pf: PF associated with VFs
1417 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
1424 ice_for_each_vf_rcu(pf, bkt, vf)
1425 rate += vf->min_tx_rate;
1432 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
1433 * @vf: VF trying to configure min_tx_rate
1434 * @min_tx_rate: min Tx rate in Mbps
1436 * Check if the min_tx_rate being passed in will cause oversubscription of total
1437 * min_tx_rate based on the current link speed and all other VFs configured
1440 * Return true if the passed min_tx_rate would cause oversubscription, else
1444 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
1446 struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1447 int all_vfs_min_tx_rate;
1448 int link_speed_mbps;
1453 link_speed_mbps = ice_get_link_speed_mbps(vsi);
1454 all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
1456 /* this VF's previous rate is being overwritten */
1457 all_vfs_min_tx_rate -= vf->min_tx_rate;
1459 if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
1460 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",
1461 min_tx_rate, vf->vf_id,
1462 all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
1471 * ice_set_vf_bw - set min/max VF bandwidth
1472 * @netdev: network interface device structure
1473 * @vf_id: VF identifier
1474 * @min_tx_rate: Minimum Tx rate in Mbps
1475 * @max_tx_rate: Maximum Tx rate in Mbps
1478 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
1481 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1482 struct ice_vsi *vsi;
1487 dev = ice_pf_to_dev(pf);
1489 vf = ice_get_vf_by_id(pf, vf_id);
1493 ret = ice_check_vf_ready_for_cfg(vf);
1497 vsi = ice_get_vf_vsi(vf);
1503 if (min_tx_rate && ice_is_dcb_active(pf)) {
1504 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
1509 if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) {
1514 if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
1515 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
1517 dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
1522 vf->min_tx_rate = min_tx_rate;
1525 if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
1526 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
1528 dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
1533 vf->max_tx_rate = max_tx_rate;
1542 * ice_get_vf_stats - populate some stats for the VF
1543 * @netdev: the netdev of the PF
1544 * @vf_id: the host OS identifier (0-255)
1545 * @vf_stats: pointer to the OS memory to be initialized
1547 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
1548 struct ifla_vf_stats *vf_stats)
1550 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1551 struct ice_eth_stats *stats;
1552 struct ice_vsi *vsi;
1556 vf = ice_get_vf_by_id(pf, vf_id);
1560 ret = ice_check_vf_ready_for_cfg(vf);
1564 vsi = ice_get_vf_vsi(vf);
1570 ice_update_eth_stats(vsi);
1571 stats = &vsi->eth_stats;
1573 memset(vf_stats, 0, sizeof(*vf_stats));
1575 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
1576 stats->rx_multicast;
1577 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
1578 stats->tx_multicast;
1579 vf_stats->rx_bytes = stats->rx_bytes;
1580 vf_stats->tx_bytes = stats->tx_bytes;
1581 vf_stats->broadcast = stats->rx_broadcast;
1582 vf_stats->multicast = stats->rx_multicast;
1583 vf_stats->rx_dropped = stats->rx_discards;
1584 vf_stats->tx_dropped = stats->tx_discards;
1592 * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
1593 * @hw: hardware structure used to check the VLAN mode
1594 * @vlan_proto: VLAN TPID being checked
1596 * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
1597 * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
1598 * Mode (SVM), then only ETH_P_8021Q is supported.
1601 ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto)
1603 bool is_supported = false;
1605 switch (vlan_proto) {
1607 is_supported = true;
1610 if (ice_is_dvm_ena(hw))
1611 is_supported = true;
1615 return is_supported;
1619 * ice_set_vf_port_vlan
1620 * @netdev: network interface device structure
1621 * @vf_id: VF identifier
1622 * @vlan_id: VLAN ID being set
1623 * @qos: priority setting
1624 * @vlan_proto: VLAN protocol
1626 * program VF Port VLAN ID and/or QoS
1629 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
1632 struct ice_pf *pf = ice_netdev_to_pf(netdev);
1633 u16 local_vlan_proto = ntohs(vlan_proto);
1638 dev = ice_pf_to_dev(pf);
1640 if (vlan_id >= VLAN_N_VID || qos > 7) {
1641 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
1642 vf_id, vlan_id, qos);
1646 if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) {
1647 dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n",
1649 return -EPROTONOSUPPORT;
1652 vf = ice_get_vf_by_id(pf, vf_id);
1656 ret = ice_check_vf_ready_for_cfg(vf);
1660 if (ice_vf_get_port_vlan_prio(vf) == qos &&
1661 ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto &&
1662 ice_vf_get_port_vlan_id(vf) == vlan_id) {
1663 /* duplicate request, so just return success */
1664 dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
1665 vlan_id, qos, local_vlan_proto);
1670 mutex_lock(&vf->cfg_lock);
1672 vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos);
1673 if (ice_vf_is_port_vlan_ena(vf))
1674 dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
1675 vlan_id, qos, local_vlan_proto, vf_id);
1677 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
1679 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1680 mutex_unlock(&vf->cfg_lock);
1688 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
1689 * @vf: pointer to the VF structure
1691 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
1693 struct ice_pf *pf = vf->pf;
1696 dev = ice_pf_to_dev(pf);
1698 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
1699 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
1701 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
1706 * ice_print_vfs_mdd_events - print VFs malicious driver detect event
1707 * @pf: pointer to the PF structure
1709 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
1711 void ice_print_vfs_mdd_events(struct ice_pf *pf)
1713 struct device *dev = ice_pf_to_dev(pf);
1714 struct ice_hw *hw = &pf->hw;
1718 /* check that there are pending MDD events to print */
1719 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
1722 /* VF MDD event logs are rate limited to one second intervals */
1723 if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1))
1726 pf->vfs.last_printed_mdd_jiffies = jiffies;
1728 mutex_lock(&pf->vfs.table_lock);
1729 ice_for_each_vf(pf, bkt, vf) {
1730 /* only print Rx MDD event message if there are new events */
1731 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
1732 vf->mdd_rx_events.last_printed =
1733 vf->mdd_rx_events.count;
1734 ice_print_vf_rx_mdd_event(vf);
1737 /* only print Tx MDD event message if there are new events */
1738 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
1739 vf->mdd_tx_events.last_printed =
1740 vf->mdd_tx_events.count;
1742 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
1743 vf->mdd_tx_events.count, hw->pf_id, vf->vf_id,
1747 mutex_unlock(&pf->vfs.table_lock);
1751 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
1752 * @pdev: pointer to a pci_dev structure
1754 * Called when recovering from a PF FLR to restore interrupt capability to
1757 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
1762 if (!pci_num_vf(pdev))
1765 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1767 struct pci_dev *vfdev;
1769 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
1771 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
1773 if (vfdev->is_virtfn && vfdev->physfn == pdev)
1774 pci_restore_msi_state(vfdev);
1775 vfdev = pci_get_device(pdev->vendor, vf_id,
1782 * ice_is_malicious_vf - helper function to detect a malicious VF
1783 * @pf: ptr to struct ice_pf
1784 * @event: pointer to the AQ event
1785 * @num_msg_proc: the number of messages processed so far
1786 * @num_msg_pending: the number of messages peinding in admin queue
1789 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
1790 u16 num_msg_proc, u16 num_msg_pending)
1792 s16 vf_id = le16_to_cpu(event->desc.retval);
1793 struct device *dev = ice_pf_to_dev(pf);
1794 struct ice_mbx_data mbxdata;
1795 bool report_malvf = false;
1799 vf = ice_get_vf_by_id(pf, vf_id);
1803 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1806 mbxdata.num_msg_proc = num_msg_proc;
1807 mbxdata.num_pending_arq = num_msg_pending;
1808 mbxdata.max_num_msgs_mbx = pf->hw.mailboxq.num_rq_entries;
1809 #define ICE_MBX_OVERFLOW_WATERMARK 64
1810 mbxdata.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
1812 /* check to see if we have a newly malicious VF */
1813 status = ice_mbx_vf_state_handler(&pf->hw, &mbxdata, &vf->mbx_info,
1819 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
1820 u8 zero_addr[ETH_ALEN] = {};
1822 dev_warn(dev, "VF MAC %pM on PF MAC %pM is generating asynchronous messages and may be overflowing the PF message queue. Please see the Adapter User Guide for more information\n",
1824 pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
1830 return vf->mbx_info.malicious;