ice: set vf->num_msix in ice_initialize_vf_entry()
[linux-2.6-block.git] / drivers / net / ethernet / intel / ice / ice_sriov.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_vf_lib_private.h"
6 #include "ice_base.h"
7 #include "ice_lib.h"
8 #include "ice_fltr.h"
9 #include "ice_dcb_lib.h"
10 #include "ice_flow.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"
15 #include "ice_vlan.h"
16
17 /**
18  * ice_free_vf_entries - Free all VF entries from the hash table
19  * @pf: pointer to the PF structure
20  *
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.
23  */
24 static void ice_free_vf_entries(struct ice_pf *pf)
25 {
26         struct ice_vfs *vfs = &pf->vfs;
27         struct hlist_node *tmp;
28         struct ice_vf *vf;
29         unsigned int bkt;
30
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.
34          */
35         lockdep_assert_held(&vfs->table_lock);
36
37         hash_for_each_safe(vfs->table, bkt, tmp, vf, entry) {
38                 hash_del_rcu(&vf->entry);
39                 ice_put_vf(vf);
40         }
41 }
42
43 /**
44  * ice_free_vf_res - Free a VF's resources
45  * @vf: pointer to the VF info
46  */
47 static void ice_free_vf_res(struct ice_vf *vf)
48 {
49         struct ice_pf *pf = vf->pf;
50         int i, last_vector_idx;
51
52         /* First, disable VF's configuration API to prevent OS from
53          * accessing the VF's VSI after it's freed or invalidated.
54          */
55         clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
56         ice_vf_fdir_exit(vf);
57         /* free VF control VSI */
58         if (vf->ctrl_vsi_idx != ICE_NO_VSI)
59                 ice_vf_ctrl_vsi_release(vf);
60
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);
64                 vf->num_mac = 0;
65         }
66
67         last_vector_idx = vf->first_vector_idx + vf->num_msix - 1;
68
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));
72
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);
76                 ice_flush(&pf->hw);
77         }
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);
81 }
82
83 /**
84  * ice_dis_vf_mappings
85  * @vf: pointer to the VF structure
86  */
87 static void ice_dis_vf_mappings(struct ice_vf *vf)
88 {
89         struct ice_pf *pf = vf->pf;
90         struct ice_vsi *vsi;
91         struct device *dev;
92         int first, last, v;
93         struct ice_hw *hw;
94
95         hw = &pf->hw;
96         vsi = ice_get_vf_vsi(vf);
97         if (WARN_ON(!vsi))
98                 return;
99
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);
103
104         first = vf->first_vector_idx;
105         last = first + vf->num_msix - 1;
106         for (v = first; v <= last; v++) {
107                 u32 reg;
108
109                 reg = FIELD_PREP(GLINT_VECT2FUNC_IS_PF_M, 1) |
110                       FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id);
111                 wr32(hw, GLINT_VECT2FUNC(v), reg);
112         }
113
114         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
115                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
116         else
117                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
118
119         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
120                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
121         else
122                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
123 }
124
125 /**
126  * ice_sriov_free_msix_res - Reset/free any used MSIX resources
127  * @pf: pointer to the PF structure
128  *
129  * Since no MSIX entries are taken from the pf->irq_tracker then just clear
130  * the pf->sriov_base_vector.
131  *
132  * Returns 0 on success, and -EINVAL on error.
133  */
134 static int ice_sriov_free_msix_res(struct ice_pf *pf)
135 {
136         if (!pf)
137                 return -EINVAL;
138
139         bitmap_free(pf->sriov_irq_bm);
140         pf->sriov_irq_size = 0;
141         pf->sriov_base_vector = 0;
142
143         return 0;
144 }
145
146 /**
147  * ice_free_vfs - Free all VFs
148  * @pf: pointer to the PF structure
149  */
150 void ice_free_vfs(struct ice_pf *pf)
151 {
152         struct device *dev = ice_pf_to_dev(pf);
153         struct ice_vfs *vfs = &pf->vfs;
154         struct ice_hw *hw = &pf->hw;
155         struct ice_vf *vf;
156         unsigned int bkt;
157
158         if (!ice_has_vfs(pf))
159                 return;
160
161         while (test_and_set_bit(ICE_VF_DIS, pf->state))
162                 usleep_range(1000, 2000);
163
164         /* Disable IOV before freeing resources. This lets any VF drivers
165          * running in the host get themselves cleaned up before we yank
166          * the carpet out from underneath their feet.
167          */
168         if (!pci_vfs_assigned(pf->pdev))
169                 pci_disable_sriov(pf->pdev);
170         else
171                 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
172
173         mutex_lock(&vfs->table_lock);
174
175         ice_for_each_vf(pf, bkt, vf) {
176                 mutex_lock(&vf->cfg_lock);
177
178                 ice_eswitch_detach(pf, vf);
179                 ice_dis_vf_qs(vf);
180
181                 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
182                         /* disable VF qp mappings and set VF disable state */
183                         ice_dis_vf_mappings(vf);
184                         set_bit(ICE_VF_STATE_DIS, vf->vf_states);
185                         ice_free_vf_res(vf);
186                 }
187
188                 if (!pci_vfs_assigned(pf->pdev)) {
189                         u32 reg_idx, bit_idx;
190
191                         reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
192                         bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
193                         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
194                 }
195
196                 /* clear malicious info since the VF is getting released */
197                 list_del(&vf->mbx_info.list_entry);
198
199                 mutex_unlock(&vf->cfg_lock);
200         }
201
202         if (ice_sriov_free_msix_res(pf))
203                 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
204
205         vfs->num_qps_per = 0;
206         ice_free_vf_entries(pf);
207
208         mutex_unlock(&vfs->table_lock);
209
210         clear_bit(ICE_VF_DIS, pf->state);
211         clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
212 }
213
214 /**
215  * ice_vf_vsi_setup - Set up a VF VSI
216  * @vf: VF to setup VSI for
217  *
218  * Returns pointer to the successfully allocated VSI struct on success,
219  * otherwise returns NULL on failure.
220  */
221 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
222 {
223         struct ice_vsi_cfg_params params = {};
224         struct ice_pf *pf = vf->pf;
225         struct ice_vsi *vsi;
226
227         params.type = ICE_VSI_VF;
228         params.pi = ice_vf_get_port_info(vf);
229         params.vf = vf;
230         params.flags = ICE_VSI_FLAG_INIT;
231
232         vsi = ice_vsi_setup(pf, &params);
233
234         if (!vsi) {
235                 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
236                 ice_vf_invalidate_vsi(vf);
237                 return NULL;
238         }
239
240         vf->lan_vsi_idx = vsi->idx;
241
242         return vsi;
243 }
244
245
246 /**
247  * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
248  * @vf: VF to enable MSIX mappings for
249  *
250  * Some of the registers need to be indexed/configured using hardware global
251  * device values and other registers need 0-based values, which represent PF
252  * based values.
253  */
254 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
255 {
256         int device_based_first_msix, device_based_last_msix;
257         int pf_based_first_msix, pf_based_last_msix, v;
258         struct ice_pf *pf = vf->pf;
259         int device_based_vf_id;
260         struct ice_hw *hw;
261         u32 reg;
262
263         hw = &pf->hw;
264         pf_based_first_msix = vf->first_vector_idx;
265         pf_based_last_msix = (pf_based_first_msix + vf->num_msix) - 1;
266
267         device_based_first_msix = pf_based_first_msix +
268                 pf->hw.func_caps.common_cap.msix_vector_first_id;
269         device_based_last_msix =
270                 (device_based_first_msix + vf->num_msix) - 1;
271         device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
272
273         reg = FIELD_PREP(VPINT_ALLOC_FIRST_M, device_based_first_msix) |
274               FIELD_PREP(VPINT_ALLOC_LAST_M, device_based_last_msix) |
275               VPINT_ALLOC_VALID_M;
276         wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
277
278         reg = FIELD_PREP(VPINT_ALLOC_PCI_FIRST_M, device_based_first_msix) |
279               FIELD_PREP(VPINT_ALLOC_PCI_LAST_M, device_based_last_msix) |
280               VPINT_ALLOC_PCI_VALID_M;
281         wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
282
283         /* map the interrupts to its functions */
284         for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
285                 reg = FIELD_PREP(GLINT_VECT2FUNC_VF_NUM_M, device_based_vf_id) |
286                       FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id);
287                 wr32(hw, GLINT_VECT2FUNC(v), reg);
288         }
289
290         /* Map mailbox interrupt to VF MSI-X vector 0 */
291         wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
292 }
293
294 /**
295  * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
296  * @vf: VF to enable the mappings for
297  * @max_txq: max Tx queues allowed on the VF's VSI
298  * @max_rxq: max Rx queues allowed on the VF's VSI
299  */
300 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
301 {
302         struct device *dev = ice_pf_to_dev(vf->pf);
303         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
304         struct ice_hw *hw = &vf->pf->hw;
305         u32 reg;
306
307         if (WARN_ON(!vsi))
308                 return;
309
310         /* set regardless of mapping mode */
311         wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
312
313         /* VF Tx queues allocation */
314         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
315                 /* set the VF PF Tx queue range
316                  * VFNUMQ value should be set to (number of queues - 1). A value
317                  * of 0 means 1 queue and a value of 255 means 256 queues
318                  */
319                 reg = FIELD_PREP(VPLAN_TX_QBASE_VFFIRSTQ_M, vsi->txq_map[0]) |
320                       FIELD_PREP(VPLAN_TX_QBASE_VFNUMQ_M, max_txq - 1);
321                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
322         } else {
323                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
324         }
325
326         /* set regardless of mapping mode */
327         wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
328
329         /* VF Rx queues allocation */
330         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
331                 /* set the VF PF Rx queue range
332                  * VFNUMQ value should be set to (number of queues - 1). A value
333                  * of 0 means 1 queue and a value of 255 means 256 queues
334                  */
335                 reg = FIELD_PREP(VPLAN_RX_QBASE_VFFIRSTQ_M, vsi->rxq_map[0]) |
336                       FIELD_PREP(VPLAN_RX_QBASE_VFNUMQ_M, max_rxq - 1);
337                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
338         } else {
339                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
340         }
341 }
342
343 /**
344  * ice_ena_vf_mappings - enable VF MSIX and queue mapping
345  * @vf: pointer to the VF structure
346  */
347 static void ice_ena_vf_mappings(struct ice_vf *vf)
348 {
349         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
350
351         if (WARN_ON(!vsi))
352                 return;
353
354         ice_ena_vf_msix_mappings(vf);
355         ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
356 }
357
358 /**
359  * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
360  * @vf: VF to calculate the register index for
361  * @q_vector: a q_vector associated to the VF
362  */
363 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
364 {
365         if (!vf || !q_vector)
366                 return -EINVAL;
367
368         /* always add one to account for the OICR being the first MSIX */
369         return vf->first_vector_idx + q_vector->v_idx + 1;
370 }
371
372 /**
373  * ice_sriov_set_msix_res - Set any used MSIX resources
374  * @pf: pointer to PF structure
375  * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
376  *
377  * This function allows SR-IOV resources to be taken from the end of the PF's
378  * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
379  * just set the pf->sriov_base_vector and return success.
380  *
381  * If there are not enough resources available, return an error. This should
382  * always be caught by ice_set_per_vf_res().
383  *
384  * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
385  * in the PF's space available for SR-IOV.
386  */
387 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
388 {
389         u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
390         int vectors_used = ice_get_max_used_msix_vector(pf);
391         int sriov_base_vector;
392
393         sriov_base_vector = total_vectors - num_msix_needed;
394
395         /* make sure we only grab irq_tracker entries from the list end and
396          * that we have enough available MSIX vectors
397          */
398         if (sriov_base_vector < vectors_used)
399                 return -EINVAL;
400
401         pf->sriov_base_vector = sriov_base_vector;
402
403         return 0;
404 }
405
406 /**
407  * ice_set_per_vf_res - check if vectors and queues are available
408  * @pf: pointer to the PF structure
409  * @num_vfs: the number of SR-IOV VFs being configured
410  *
411  * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
412  * get more vectors and can enable more queues per VF. Note that this does not
413  * grab any vectors from the SW pool already allocated. Also note, that all
414  * vector counts include one for each VF's miscellaneous interrupt vector
415  * (i.e. OICR).
416  *
417  * Minimum VFs - 2 vectors, 1 queue pair
418  * Small VFs - 5 vectors, 4 queue pairs
419  * Medium VFs - 17 vectors, 16 queue pairs
420  *
421  * Second, determine number of queue pairs per VF by starting with a pre-defined
422  * maximum each VF supports. If this is not possible, then we adjust based on
423  * queue pairs available on the device.
424  *
425  * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
426  * by each VF during VF initialization and reset.
427  */
428 static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs)
429 {
430         int vectors_used = ice_get_max_used_msix_vector(pf);
431         u16 num_msix_per_vf, num_txq, num_rxq, avail_qs;
432         int msix_avail_per_vf, msix_avail_for_sriov;
433         struct device *dev = ice_pf_to_dev(pf);
434         int err;
435
436         lockdep_assert_held(&pf->vfs.table_lock);
437
438         if (!num_vfs)
439                 return -EINVAL;
440
441         /* determine MSI-X resources per VF */
442         msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
443                 vectors_used;
444         msix_avail_per_vf = msix_avail_for_sriov / num_vfs;
445         if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
446                 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
447         } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
448                 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
449         } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
450                 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
451         } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
452                 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
453         } else {
454                 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",
455                         msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
456                         num_vfs);
457                 return -ENOSPC;
458         }
459
460         num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
461                         ICE_MAX_RSS_QS_PER_VF);
462         avail_qs = ice_get_avail_txq_count(pf) / num_vfs;
463         if (!avail_qs)
464                 num_txq = 0;
465         else if (num_txq > avail_qs)
466                 num_txq = rounddown_pow_of_two(avail_qs);
467
468         num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF,
469                         ICE_MAX_RSS_QS_PER_VF);
470         avail_qs = ice_get_avail_rxq_count(pf) / num_vfs;
471         if (!avail_qs)
472                 num_rxq = 0;
473         else if (num_rxq > avail_qs)
474                 num_rxq = rounddown_pow_of_two(avail_qs);
475
476         if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) {
477                 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
478                         ICE_MIN_QS_PER_VF, num_vfs);
479                 return -ENOSPC;
480         }
481
482         err = ice_sriov_set_msix_res(pf, num_msix_per_vf * num_vfs);
483         if (err) {
484                 dev_err(dev, "Unable to set MSI-X resources for %d VFs, err %d\n",
485                         num_vfs, err);
486                 return err;
487         }
488
489         /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
490         pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq);
491         pf->vfs.num_msix_per = num_msix_per_vf;
492         dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
493                  num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per);
494
495         return 0;
496 }
497
498 /**
499  * ice_sriov_get_irqs - get irqs for SR-IOV usacase
500  * @pf: pointer to PF structure
501  * @needed: number of irqs to get
502  *
503  * This returns the first MSI-X vector index in PF space that is used by this
504  * VF. This index is used when accessing PF relative registers such as
505  * GLINT_VECT2FUNC and GLINT_DYN_CTL.
506  * This will always be the OICR index in the AVF driver so any functionality
507  * using vf->first_vector_idx for queue configuration_id: id of VF which will
508  * use this irqs
509  *
510  * Only SRIOV specific vectors are tracked in sriov_irq_bm. SRIOV vectors are
511  * allocated from the end of global irq index. First bit in sriov_irq_bm means
512  * last irq index etc. It simplifies extension of SRIOV vectors.
513  * They will be always located from sriov_base_vector to the last irq
514  * index. While increasing/decreasing sriov_base_vector can be moved.
515  */
516 static int ice_sriov_get_irqs(struct ice_pf *pf, u16 needed)
517 {
518         int res = bitmap_find_next_zero_area(pf->sriov_irq_bm,
519                                              pf->sriov_irq_size, 0, needed, 0);
520         /* conversion from number in bitmap to global irq index */
521         int index = pf->sriov_irq_size - res - needed;
522
523         if (res >= pf->sriov_irq_size || index < pf->sriov_base_vector)
524                 return -ENOENT;
525
526         bitmap_set(pf->sriov_irq_bm, res, needed);
527         return index;
528 }
529
530 /**
531  * ice_sriov_free_irqs - free irqs used by the VF
532  * @pf: pointer to PF structure
533  * @vf: pointer to VF structure
534  */
535 static void ice_sriov_free_irqs(struct ice_pf *pf, struct ice_vf *vf)
536 {
537         /* Move back from first vector index to first index in bitmap */
538         int bm_i = pf->sriov_irq_size - vf->first_vector_idx - vf->num_msix;
539
540         bitmap_clear(pf->sriov_irq_bm, bm_i, vf->num_msix);
541         vf->first_vector_idx = 0;
542 }
543
544 /**
545  * ice_init_vf_vsi_res - initialize/setup VF VSI resources
546  * @vf: VF to initialize/setup the VSI for
547  *
548  * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
549  * VF VSI's broadcast filter and is only used during initial VF creation.
550  */
551 static int ice_init_vf_vsi_res(struct ice_vf *vf)
552 {
553         struct ice_pf *pf = vf->pf;
554         struct ice_vsi *vsi;
555         int err;
556
557         vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
558         if (vf->first_vector_idx < 0)
559                 return -ENOMEM;
560
561         vsi = ice_vf_vsi_setup(vf);
562         if (!vsi)
563                 return -ENOMEM;
564
565         err = ice_vf_init_host_cfg(vf, vsi);
566         if (err)
567                 goto release_vsi;
568
569         return 0;
570
571 release_vsi:
572         ice_vf_vsi_release(vf);
573         return err;
574 }
575
576 /**
577  * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
578  * @pf: PF the VFs are associated with
579  */
580 static int ice_start_vfs(struct ice_pf *pf)
581 {
582         struct ice_hw *hw = &pf->hw;
583         unsigned int bkt, it_cnt;
584         struct ice_vf *vf;
585         int retval;
586
587         lockdep_assert_held(&pf->vfs.table_lock);
588
589         it_cnt = 0;
590         ice_for_each_vf(pf, bkt, vf) {
591                 vf->vf_ops->clear_reset_trigger(vf);
592
593                 retval = ice_init_vf_vsi_res(vf);
594                 if (retval) {
595                         dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
596                                 vf->vf_id, retval);
597                         goto teardown;
598                 }
599
600                 retval = ice_eswitch_attach(pf, vf);
601                 if (retval) {
602                         dev_err(ice_pf_to_dev(pf), "Failed to attach VF %d to eswitch, error %d",
603                                 vf->vf_id, retval);
604                         ice_vf_vsi_release(vf);
605                         goto teardown;
606                 }
607
608                 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
609                 ice_ena_vf_mappings(vf);
610                 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
611                 it_cnt++;
612         }
613
614         ice_flush(hw);
615         return 0;
616
617 teardown:
618         ice_for_each_vf(pf, bkt, vf) {
619                 if (it_cnt == 0)
620                         break;
621
622                 ice_dis_vf_mappings(vf);
623                 ice_vf_vsi_release(vf);
624                 it_cnt--;
625         }
626
627         return retval;
628 }
629
630 /**
631  * ice_sriov_free_vf - Free VF memory after all references are dropped
632  * @vf: pointer to VF to free
633  *
634  * Called by ice_put_vf through ice_release_vf once the last reference to a VF
635  * structure has been dropped.
636  */
637 static void ice_sriov_free_vf(struct ice_vf *vf)
638 {
639         mutex_destroy(&vf->cfg_lock);
640
641         kfree_rcu(vf, rcu);
642 }
643
644 /**
645  * ice_sriov_clear_reset_state - clears VF Reset status register
646  * @vf: the vf to configure
647  */
648 static void ice_sriov_clear_reset_state(struct ice_vf *vf)
649 {
650         struct ice_hw *hw = &vf->pf->hw;
651
652         /* Clear the reset status register so that VF immediately sees that
653          * the device is resetting, even if hardware hasn't yet gotten around
654          * to clearing VFGEN_RSTAT for us.
655          */
656         wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_INPROGRESS);
657 }
658
659 /**
660  * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers
661  * @vf: the vf to configure
662  */
663 static void ice_sriov_clear_mbx_register(struct ice_vf *vf)
664 {
665         struct ice_pf *pf = vf->pf;
666
667         wr32(&pf->hw, VF_MBX_ARQLEN(vf->vf_id), 0);
668         wr32(&pf->hw, VF_MBX_ATQLEN(vf->vf_id), 0);
669 }
670
671 /**
672  * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF
673  * @vf: pointer to VF structure
674  * @is_vflr: true if reset occurred due to VFLR
675  *
676  * Trigger and cleanup after a VF reset for a SR-IOV VF.
677  */
678 static void ice_sriov_trigger_reset_register(struct ice_vf *vf, bool is_vflr)
679 {
680         struct ice_pf *pf = vf->pf;
681         u32 reg, reg_idx, bit_idx;
682         unsigned int vf_abs_id, i;
683         struct device *dev;
684         struct ice_hw *hw;
685
686         dev = ice_pf_to_dev(pf);
687         hw = &pf->hw;
688         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
689
690         /* In the case of a VFLR, HW has already reset the VF and we just need
691          * to clean up. Otherwise we must first trigger the reset using the
692          * VFRTRIG register.
693          */
694         if (!is_vflr) {
695                 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
696                 reg |= VPGEN_VFRTRIG_VFSWR_M;
697                 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
698         }
699
700         /* clear the VFLR bit in GLGEN_VFLRSTAT */
701         reg_idx = (vf_abs_id) / 32;
702         bit_idx = (vf_abs_id) % 32;
703         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
704         ice_flush(hw);
705
706         wr32(hw, PF_PCI_CIAA,
707              VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
708         for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
709                 reg = rd32(hw, PF_PCI_CIAD);
710                 /* no transactions pending so stop polling */
711                 if ((reg & VF_TRANS_PENDING_M) == 0)
712                         break;
713
714                 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
715                 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
716         }
717 }
718
719 /**
720  * ice_sriov_poll_reset_status - poll SRIOV VF reset status
721  * @vf: pointer to VF structure
722  *
723  * Returns true when reset is successful, else returns false
724  */
725 static bool ice_sriov_poll_reset_status(struct ice_vf *vf)
726 {
727         struct ice_pf *pf = vf->pf;
728         unsigned int i;
729         u32 reg;
730
731         for (i = 0; i < 10; i++) {
732                 /* VF reset requires driver to first reset the VF and then
733                  * poll the status register to make sure that the reset
734                  * completed successfully.
735                  */
736                 reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id));
737                 if (reg & VPGEN_VFRSTAT_VFRD_M)
738                         return true;
739
740                 /* only sleep if the reset is not done */
741                 usleep_range(10, 20);
742         }
743         return false;
744 }
745
746 /**
747  * ice_sriov_clear_reset_trigger - enable VF to access hardware
748  * @vf: VF to enabled hardware access for
749  */
750 static void ice_sriov_clear_reset_trigger(struct ice_vf *vf)
751 {
752         struct ice_hw *hw = &vf->pf->hw;
753         u32 reg;
754
755         reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
756         reg &= ~VPGEN_VFRTRIG_VFSWR_M;
757         wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
758         ice_flush(hw);
759 }
760
761 /**
762  * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
763  * @vf: VF to perform tasks on
764  */
765 static void ice_sriov_post_vsi_rebuild(struct ice_vf *vf)
766 {
767         ice_ena_vf_mappings(vf);
768         wr32(&vf->pf->hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
769 }
770
771 static const struct ice_vf_ops ice_sriov_vf_ops = {
772         .reset_type = ICE_VF_RESET,
773         .free = ice_sriov_free_vf,
774         .clear_reset_state = ice_sriov_clear_reset_state,
775         .clear_mbx_register = ice_sriov_clear_mbx_register,
776         .trigger_reset_register = ice_sriov_trigger_reset_register,
777         .poll_reset_status = ice_sriov_poll_reset_status,
778         .clear_reset_trigger = ice_sriov_clear_reset_trigger,
779         .irq_close = NULL,
780         .post_vsi_rebuild = ice_sriov_post_vsi_rebuild,
781 };
782
783 /**
784  * ice_create_vf_entries - Allocate and insert VF entries
785  * @pf: pointer to the PF structure
786  * @num_vfs: the number of VFs to allocate
787  *
788  * Allocate new VF entries and insert them into the hash table. Set some
789  * basic default fields for initializing the new VFs.
790  *
791  * After this function exits, the hash table will have num_vfs entries
792  * inserted.
793  *
794  * Returns 0 on success or an integer error code on failure.
795  */
796 static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs)
797 {
798         struct pci_dev *pdev = pf->pdev;
799         struct ice_vfs *vfs = &pf->vfs;
800         struct pci_dev *vfdev = NULL;
801         struct ice_vf *vf;
802         u16 vf_pdev_id;
803         int err, pos;
804
805         lockdep_assert_held(&vfs->table_lock);
806
807         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
808         pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_pdev_id);
809
810         for (u16 vf_id = 0; vf_id < num_vfs; vf_id++) {
811                 vf = kzalloc(sizeof(*vf), GFP_KERNEL);
812                 if (!vf) {
813                         err = -ENOMEM;
814                         goto err_free_entries;
815                 }
816                 kref_init(&vf->refcnt);
817
818                 vf->pf = pf;
819                 vf->vf_id = vf_id;
820
821                 /* set sriov vf ops for VFs created during SRIOV flow */
822                 vf->vf_ops = &ice_sriov_vf_ops;
823
824                 ice_initialize_vf_entry(vf);
825
826                 do {
827                         vfdev = pci_get_device(pdev->vendor, vf_pdev_id, vfdev);
828                 } while (vfdev && vfdev->physfn != pdev);
829                 vf->vfdev = vfdev;
830                 vf->vf_sw_id = pf->first_sw;
831
832                 pci_dev_get(vfdev);
833
834                 hash_add_rcu(vfs->table, &vf->entry, vf_id);
835         }
836
837         /* Decrement of refcount done by pci_get_device() inside the loop does
838          * not touch the last iteration's vfdev, so it has to be done manually
839          * to balance pci_dev_get() added within the loop.
840          */
841         pci_dev_put(vfdev);
842
843         return 0;
844
845 err_free_entries:
846         ice_free_vf_entries(pf);
847         return err;
848 }
849
850 /**
851  * ice_ena_vfs - enable VFs so they are ready to be used
852  * @pf: pointer to the PF structure
853  * @num_vfs: number of VFs to enable
854  */
855 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
856 {
857         int total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
858         struct device *dev = ice_pf_to_dev(pf);
859         struct ice_hw *hw = &pf->hw;
860         int ret;
861
862         pf->sriov_irq_bm = bitmap_zalloc(total_vectors, GFP_KERNEL);
863         if (!pf->sriov_irq_bm)
864                 return -ENOMEM;
865         pf->sriov_irq_size = total_vectors;
866
867         /* Disable global interrupt 0 so we don't try to handle the VFLR. */
868         wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
869              ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
870         set_bit(ICE_OICR_INTR_DIS, pf->state);
871         ice_flush(hw);
872
873         ret = pci_enable_sriov(pf->pdev, num_vfs);
874         if (ret)
875                 goto err_unroll_intr;
876
877         mutex_lock(&pf->vfs.table_lock);
878
879         ret = ice_set_per_vf_res(pf, num_vfs);
880         if (ret) {
881                 dev_err(dev, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n",
882                         num_vfs, ret);
883                 goto err_unroll_sriov;
884         }
885
886         ret = ice_create_vf_entries(pf, num_vfs);
887         if (ret) {
888                 dev_err(dev, "Failed to allocate VF entries for %d VFs\n",
889                         num_vfs);
890                 goto err_unroll_sriov;
891         }
892
893         ret = ice_start_vfs(pf);
894         if (ret) {
895                 dev_err(dev, "Failed to start %d VFs, err %d\n", num_vfs, ret);
896                 ret = -EAGAIN;
897                 goto err_unroll_vf_entries;
898         }
899
900         clear_bit(ICE_VF_DIS, pf->state);
901
902         /* rearm global interrupts */
903         if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state))
904                 ice_irq_dynamic_ena(hw, NULL, NULL);
905
906         mutex_unlock(&pf->vfs.table_lock);
907
908         return 0;
909
910 err_unroll_vf_entries:
911         ice_free_vf_entries(pf);
912 err_unroll_sriov:
913         mutex_unlock(&pf->vfs.table_lock);
914         pci_disable_sriov(pf->pdev);
915 err_unroll_intr:
916         /* rearm interrupts here */
917         ice_irq_dynamic_ena(hw, NULL, NULL);
918         clear_bit(ICE_OICR_INTR_DIS, pf->state);
919         bitmap_free(pf->sriov_irq_bm);
920         return ret;
921 }
922
923 /**
924  * ice_pci_sriov_ena - Enable or change number of VFs
925  * @pf: pointer to the PF structure
926  * @num_vfs: number of VFs to allocate
927  *
928  * Returns 0 on success and negative on failure
929  */
930 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
931 {
932         struct device *dev = ice_pf_to_dev(pf);
933         int err;
934
935         if (!num_vfs) {
936                 ice_free_vfs(pf);
937                 return 0;
938         }
939
940         if (num_vfs > pf->vfs.num_supported) {
941                 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
942                         num_vfs, pf->vfs.num_supported);
943                 return -EOPNOTSUPP;
944         }
945
946         dev_info(dev, "Enabling %d VFs\n", num_vfs);
947         err = ice_ena_vfs(pf, num_vfs);
948         if (err) {
949                 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
950                 return err;
951         }
952
953         set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
954         return 0;
955 }
956
957 /**
958  * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
959  * @pf: PF to enabled SR-IOV on
960  */
961 static int ice_check_sriov_allowed(struct ice_pf *pf)
962 {
963         struct device *dev = ice_pf_to_dev(pf);
964
965         if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
966                 dev_err(dev, "This device is not capable of SR-IOV\n");
967                 return -EOPNOTSUPP;
968         }
969
970         if (ice_is_safe_mode(pf)) {
971                 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
972                 return -EOPNOTSUPP;
973         }
974
975         if (!ice_pf_state_is_nominal(pf)) {
976                 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
977                 return -EBUSY;
978         }
979
980         return 0;
981 }
982
983 /**
984  * ice_sriov_get_vf_total_msix - return number of MSI-X used by VFs
985  * @pdev: pointer to pci_dev struct
986  *
987  * The function is called via sysfs ops
988  */
989 u32 ice_sriov_get_vf_total_msix(struct pci_dev *pdev)
990 {
991         struct ice_pf *pf = pci_get_drvdata(pdev);
992
993         return pf->sriov_irq_size - ice_get_max_used_msix_vector(pf);
994 }
995
996 static int ice_sriov_move_base_vector(struct ice_pf *pf, int move)
997 {
998         if (pf->sriov_base_vector - move < ice_get_max_used_msix_vector(pf))
999                 return -ENOMEM;
1000
1001         pf->sriov_base_vector -= move;
1002         return 0;
1003 }
1004
1005 static void ice_sriov_remap_vectors(struct ice_pf *pf, u16 restricted_id)
1006 {
1007         u16 vf_ids[ICE_MAX_SRIOV_VFS];
1008         struct ice_vf *tmp_vf;
1009         int to_remap = 0, bkt;
1010
1011         /* For better irqs usage try to remap irqs of VFs
1012          * that aren't running yet
1013          */
1014         ice_for_each_vf(pf, bkt, tmp_vf) {
1015                 /* skip VF which is changing the number of MSI-X */
1016                 if (restricted_id == tmp_vf->vf_id ||
1017                     test_bit(ICE_VF_STATE_ACTIVE, tmp_vf->vf_states))
1018                         continue;
1019
1020                 ice_dis_vf_mappings(tmp_vf);
1021                 ice_sriov_free_irqs(pf, tmp_vf);
1022
1023                 vf_ids[to_remap] = tmp_vf->vf_id;
1024                 to_remap += 1;
1025         }
1026
1027         for (int i = 0; i < to_remap; i++) {
1028                 tmp_vf = ice_get_vf_by_id(pf, vf_ids[i]);
1029                 if (!tmp_vf)
1030                         continue;
1031
1032                 tmp_vf->first_vector_idx =
1033                         ice_sriov_get_irqs(pf, tmp_vf->num_msix);
1034                 /* there is no need to rebuild VSI as we are only changing the
1035                  * vector indexes not amount of MSI-X or queues
1036                  */
1037                 ice_ena_vf_mappings(tmp_vf);
1038                 ice_put_vf(tmp_vf);
1039         }
1040 }
1041
1042 /**
1043  * ice_sriov_set_msix_vec_count
1044  * @vf_dev: pointer to pci_dev struct of VF device
1045  * @msix_vec_count: new value for MSI-X amount on this VF
1046  *
1047  * Set requested MSI-X, queues and registers for @vf_dev.
1048  *
1049  * First do some sanity checks like if there are any VFs, if the new value
1050  * is correct etc. Then disable old mapping (MSI-X and queues registers), change
1051  * MSI-X and queues, rebuild VSI and enable new mapping.
1052  *
1053  * If it is possible (driver not binded to VF) try to remap also other VFs to
1054  * linearize irqs register usage.
1055  */
1056 int ice_sriov_set_msix_vec_count(struct pci_dev *vf_dev, int msix_vec_count)
1057 {
1058         struct pci_dev *pdev = pci_physfn(vf_dev);
1059         struct ice_pf *pf = pci_get_drvdata(pdev);
1060         u16 prev_msix, prev_queues, queues;
1061         bool needs_rebuild = false;
1062         struct ice_vsi *vsi;
1063         struct ice_vf *vf;
1064         int id;
1065
1066         if (!ice_get_num_vfs(pf))
1067                 return -ENOENT;
1068
1069         if (!msix_vec_count)
1070                 return 0;
1071
1072         queues = msix_vec_count;
1073         /* add 1 MSI-X for OICR */
1074         msix_vec_count += 1;
1075
1076         if (queues > min(ice_get_avail_txq_count(pf),
1077                          ice_get_avail_rxq_count(pf)))
1078                 return -EINVAL;
1079
1080         if (msix_vec_count < ICE_MIN_INTR_PER_VF)
1081                 return -EINVAL;
1082
1083         /* Transition of PCI VF function number to function_id */
1084         for (id = 0; id < pci_num_vf(pdev); id++) {
1085                 if (vf_dev->devfn == pci_iov_virtfn_devfn(pdev, id))
1086                         break;
1087         }
1088
1089         if (id == pci_num_vf(pdev))
1090                 return -ENOENT;
1091
1092         vf = ice_get_vf_by_id(pf, id);
1093
1094         if (!vf)
1095                 return -ENOENT;
1096
1097         vsi = ice_get_vf_vsi(vf);
1098         if (!vsi)
1099                 return -ENOENT;
1100
1101         prev_msix = vf->num_msix;
1102         prev_queues = vf->num_vf_qs;
1103
1104         if (ice_sriov_move_base_vector(pf, msix_vec_count - prev_msix)) {
1105                 ice_put_vf(vf);
1106                 return -ENOSPC;
1107         }
1108
1109         ice_dis_vf_mappings(vf);
1110         ice_sriov_free_irqs(pf, vf);
1111
1112         /* Remap all VFs beside the one is now configured */
1113         ice_sriov_remap_vectors(pf, vf->vf_id);
1114
1115         vf->num_msix = msix_vec_count;
1116         vf->num_vf_qs = queues;
1117         vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
1118         if (vf->first_vector_idx < 0)
1119                 goto unroll;
1120
1121         if (ice_vf_reconfig_vsi(vf) || ice_vf_init_host_cfg(vf, vsi)) {
1122                 /* Try to rebuild with previous values */
1123                 needs_rebuild = true;
1124                 goto unroll;
1125         }
1126
1127         dev_info(ice_pf_to_dev(pf),
1128                  "Changing VF %d resources to %d vectors and %d queues\n",
1129                  vf->vf_id, vf->num_msix, vf->num_vf_qs);
1130
1131         ice_ena_vf_mappings(vf);
1132         ice_put_vf(vf);
1133
1134         return 0;
1135
1136 unroll:
1137         dev_info(ice_pf_to_dev(pf),
1138                  "Can't set %d vectors on VF %d, falling back to %d\n",
1139                  vf->num_msix, vf->vf_id, prev_msix);
1140
1141         vf->num_msix = prev_msix;
1142         vf->num_vf_qs = prev_queues;
1143         vf->first_vector_idx = ice_sriov_get_irqs(pf, vf->num_msix);
1144         if (vf->first_vector_idx < 0)
1145                 return -EINVAL;
1146
1147         if (needs_rebuild) {
1148                 ice_vf_reconfig_vsi(vf);
1149                 ice_vf_init_host_cfg(vf, vsi);
1150         }
1151
1152         ice_ena_vf_mappings(vf);
1153         ice_put_vf(vf);
1154
1155         return -EINVAL;
1156 }
1157
1158 /**
1159  * ice_sriov_configure - Enable or change number of VFs via sysfs
1160  * @pdev: pointer to a pci_dev structure
1161  * @num_vfs: number of VFs to allocate or 0 to free VFs
1162  *
1163  * This function is called when the user updates the number of VFs in sysfs. On
1164  * success return whatever num_vfs was set to by the caller. Return negative on
1165  * failure.
1166  */
1167 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1168 {
1169         struct ice_pf *pf = pci_get_drvdata(pdev);
1170         struct device *dev = ice_pf_to_dev(pf);
1171         int err;
1172
1173         err = ice_check_sriov_allowed(pf);
1174         if (err)
1175                 return err;
1176
1177         if (!num_vfs) {
1178                 if (!pci_vfs_assigned(pdev)) {
1179                         ice_free_vfs(pf);
1180                         return 0;
1181                 }
1182
1183                 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1184                 return -EBUSY;
1185         }
1186
1187         err = ice_pci_sriov_ena(pf, num_vfs);
1188         if (err)
1189                 return err;
1190
1191         return num_vfs;
1192 }
1193
1194 /**
1195  * ice_process_vflr_event - Free VF resources via IRQ calls
1196  * @pf: pointer to the PF structure
1197  *
1198  * called from the VFLR IRQ handler to
1199  * free up VF resources and state variables
1200  */
1201 void ice_process_vflr_event(struct ice_pf *pf)
1202 {
1203         struct ice_hw *hw = &pf->hw;
1204         struct ice_vf *vf;
1205         unsigned int bkt;
1206         u32 reg;
1207
1208         if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
1209             !ice_has_vfs(pf))
1210                 return;
1211
1212         mutex_lock(&pf->vfs.table_lock);
1213         ice_for_each_vf(pf, bkt, vf) {
1214                 u32 reg_idx, bit_idx;
1215
1216                 reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1217                 bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1218                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1219                 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1220                 if (reg & BIT(bit_idx))
1221                         /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1222                         ice_reset_vf(vf, ICE_VF_RESET_VFLR | ICE_VF_RESET_LOCK);
1223         }
1224         mutex_unlock(&pf->vfs.table_lock);
1225 }
1226
1227 /**
1228  * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1229  * @pf: PF used to index all VFs
1230  * @pfq: queue index relative to the PF's function space
1231  *
1232  * If no VF is found who owns the pfq then return NULL, otherwise return a
1233  * pointer to the VF who owns the pfq
1234  *
1235  * If this function returns non-NULL, it acquires a reference count of the VF
1236  * structure. The caller is responsible for calling ice_put_vf() to drop this
1237  * reference.
1238  */
1239 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1240 {
1241         struct ice_vf *vf;
1242         unsigned int bkt;
1243
1244         rcu_read_lock();
1245         ice_for_each_vf_rcu(pf, bkt, vf) {
1246                 struct ice_vsi *vsi;
1247                 u16 rxq_idx;
1248
1249                 vsi = ice_get_vf_vsi(vf);
1250                 if (!vsi)
1251                         continue;
1252
1253                 ice_for_each_rxq(vsi, rxq_idx)
1254                         if (vsi->rxq_map[rxq_idx] == pfq) {
1255                                 struct ice_vf *found;
1256
1257                                 if (kref_get_unless_zero(&vf->refcnt))
1258                                         found = vf;
1259                                 else
1260                                         found = NULL;
1261                                 rcu_read_unlock();
1262                                 return found;
1263                         }
1264         }
1265         rcu_read_unlock();
1266
1267         return NULL;
1268 }
1269
1270 /**
1271  * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1272  * @pf: PF used for conversion
1273  * @globalq: global queue index used to convert to PF space queue index
1274  */
1275 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1276 {
1277         return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1278 }
1279
1280 /**
1281  * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1282  * @pf: PF that the LAN overflow event happened on
1283  * @event: structure holding the event information for the LAN overflow event
1284  *
1285  * Determine if the LAN overflow event was caused by a VF queue. If it was not
1286  * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1287  * reset on the offending VF.
1288  */
1289 void
1290 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1291 {
1292         u32 gldcb_rtctq, queue;
1293         struct ice_vf *vf;
1294
1295         gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1296         dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1297
1298         /* event returns device global Rx queue number */
1299         queue = FIELD_GET(GLDCB_RTCTQ_RXQNUM_M, gldcb_rtctq);
1300
1301         vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1302         if (!vf)
1303                 return;
1304
1305         ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1306         ice_put_vf(vf);
1307 }
1308
1309 /**
1310  * ice_set_vf_spoofchk
1311  * @netdev: network interface device structure
1312  * @vf_id: VF identifier
1313  * @ena: flag to enable or disable feature
1314  *
1315  * Enable or disable VF spoof checking
1316  */
1317 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
1318 {
1319         struct ice_netdev_priv *np = netdev_priv(netdev);
1320         struct ice_pf *pf = np->vsi->back;
1321         struct ice_vsi *vf_vsi;
1322         struct device *dev;
1323         struct ice_vf *vf;
1324         int ret;
1325
1326         dev = ice_pf_to_dev(pf);
1327
1328         vf = ice_get_vf_by_id(pf, vf_id);
1329         if (!vf)
1330                 return -EINVAL;
1331
1332         ret = ice_check_vf_ready_for_cfg(vf);
1333         if (ret)
1334                 goto out_put_vf;
1335
1336         vf_vsi = ice_get_vf_vsi(vf);
1337         if (!vf_vsi) {
1338                 netdev_err(netdev, "VSI %d for VF %d is null\n",
1339                            vf->lan_vsi_idx, vf->vf_id);
1340                 ret = -EINVAL;
1341                 goto out_put_vf;
1342         }
1343
1344         if (vf_vsi->type != ICE_VSI_VF) {
1345                 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
1346                            vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
1347                 ret = -ENODEV;
1348                 goto out_put_vf;
1349         }
1350
1351         if (ena == vf->spoofchk) {
1352                 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
1353                 ret = 0;
1354                 goto out_put_vf;
1355         }
1356
1357         ret = ice_vsi_apply_spoofchk(vf_vsi, ena);
1358         if (ret)
1359                 dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n",
1360                         ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret);
1361         else
1362                 vf->spoofchk = ena;
1363
1364 out_put_vf:
1365         ice_put_vf(vf);
1366         return ret;
1367 }
1368
1369 /**
1370  * ice_get_vf_cfg
1371  * @netdev: network interface device structure
1372  * @vf_id: VF identifier
1373  * @ivi: VF configuration structure
1374  *
1375  * return VF configuration
1376  */
1377 int
1378 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
1379 {
1380         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1381         struct ice_vf *vf;
1382         int ret;
1383
1384         vf = ice_get_vf_by_id(pf, vf_id);
1385         if (!vf)
1386                 return -EINVAL;
1387
1388         ret = ice_check_vf_ready_for_cfg(vf);
1389         if (ret)
1390                 goto out_put_vf;
1391
1392         ivi->vf = vf_id;
1393         ether_addr_copy(ivi->mac, vf->hw_lan_addr);
1394
1395         /* VF configuration for VLAN and applicable QoS */
1396         ivi->vlan = ice_vf_get_port_vlan_id(vf);
1397         ivi->qos = ice_vf_get_port_vlan_prio(vf);
1398         if (ice_vf_is_port_vlan_ena(vf))
1399                 ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf));
1400
1401         ivi->trusted = vf->trusted;
1402         ivi->spoofchk = vf->spoofchk;
1403         if (!vf->link_forced)
1404                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
1405         else if (vf->link_up)
1406                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
1407         else
1408                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
1409         ivi->max_tx_rate = vf->max_tx_rate;
1410         ivi->min_tx_rate = vf->min_tx_rate;
1411
1412 out_put_vf:
1413         ice_put_vf(vf);
1414         return ret;
1415 }
1416
1417 /**
1418  * ice_set_vf_mac
1419  * @netdev: network interface device structure
1420  * @vf_id: VF identifier
1421  * @mac: MAC address
1422  *
1423  * program VF MAC address
1424  */
1425 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1426 {
1427         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1428         struct ice_vf *vf;
1429         int ret;
1430
1431         if (is_multicast_ether_addr(mac)) {
1432                 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
1433                 return -EINVAL;
1434         }
1435
1436         vf = ice_get_vf_by_id(pf, vf_id);
1437         if (!vf)
1438                 return -EINVAL;
1439
1440         /* nothing left to do, unicast MAC already set */
1441         if (ether_addr_equal(vf->dev_lan_addr, mac) &&
1442             ether_addr_equal(vf->hw_lan_addr, mac)) {
1443                 ret = 0;
1444                 goto out_put_vf;
1445         }
1446
1447         ret = ice_check_vf_ready_for_cfg(vf);
1448         if (ret)
1449                 goto out_put_vf;
1450
1451         mutex_lock(&vf->cfg_lock);
1452
1453         /* VF is notified of its new MAC via the PF's response to the
1454          * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
1455          */
1456         ether_addr_copy(vf->dev_lan_addr, mac);
1457         ether_addr_copy(vf->hw_lan_addr, mac);
1458         if (is_zero_ether_addr(mac)) {
1459                 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
1460                 vf->pf_set_mac = false;
1461                 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
1462                             vf->vf_id);
1463         } else {
1464                 /* PF will add MAC rule for the VF */
1465                 vf->pf_set_mac = true;
1466                 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
1467                             mac, vf_id);
1468         }
1469
1470         ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1471         mutex_unlock(&vf->cfg_lock);
1472
1473 out_put_vf:
1474         ice_put_vf(vf);
1475         return ret;
1476 }
1477
1478 /**
1479  * ice_set_vf_trust
1480  * @netdev: network interface device structure
1481  * @vf_id: VF identifier
1482  * @trusted: Boolean value to enable/disable trusted VF
1483  *
1484  * Enable or disable a given VF as trusted
1485  */
1486 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
1487 {
1488         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1489         struct ice_vf *vf;
1490         int ret;
1491
1492         vf = ice_get_vf_by_id(pf, vf_id);
1493         if (!vf)
1494                 return -EINVAL;
1495
1496         if (ice_is_eswitch_mode_switchdev(pf)) {
1497                 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n");
1498                 return -EOPNOTSUPP;
1499         }
1500
1501         ret = ice_check_vf_ready_for_cfg(vf);
1502         if (ret)
1503                 goto out_put_vf;
1504
1505         /* Check if already trusted */
1506         if (trusted == vf->trusted) {
1507                 ret = 0;
1508                 goto out_put_vf;
1509         }
1510
1511         mutex_lock(&vf->cfg_lock);
1512
1513         vf->trusted = trusted;
1514         ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1515         dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
1516                  vf_id, trusted ? "" : "un");
1517
1518         mutex_unlock(&vf->cfg_lock);
1519
1520 out_put_vf:
1521         ice_put_vf(vf);
1522         return ret;
1523 }
1524
1525 /**
1526  * ice_set_vf_link_state
1527  * @netdev: network interface device structure
1528  * @vf_id: VF identifier
1529  * @link_state: required link state
1530  *
1531  * Set VF's link state, irrespective of physical link state status
1532  */
1533 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
1534 {
1535         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1536         struct ice_vf *vf;
1537         int ret;
1538
1539         vf = ice_get_vf_by_id(pf, vf_id);
1540         if (!vf)
1541                 return -EINVAL;
1542
1543         ret = ice_check_vf_ready_for_cfg(vf);
1544         if (ret)
1545                 goto out_put_vf;
1546
1547         switch (link_state) {
1548         case IFLA_VF_LINK_STATE_AUTO:
1549                 vf->link_forced = false;
1550                 break;
1551         case IFLA_VF_LINK_STATE_ENABLE:
1552                 vf->link_forced = true;
1553                 vf->link_up = true;
1554                 break;
1555         case IFLA_VF_LINK_STATE_DISABLE:
1556                 vf->link_forced = true;
1557                 vf->link_up = false;
1558                 break;
1559         default:
1560                 ret = -EINVAL;
1561                 goto out_put_vf;
1562         }
1563
1564         ice_vc_notify_vf_link_state(vf);
1565
1566 out_put_vf:
1567         ice_put_vf(vf);
1568         return ret;
1569 }
1570
1571 /**
1572  * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs
1573  * @pf: PF associated with VFs
1574  */
1575 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf)
1576 {
1577         struct ice_vf *vf;
1578         unsigned int bkt;
1579         int rate = 0;
1580
1581         rcu_read_lock();
1582         ice_for_each_vf_rcu(pf, bkt, vf)
1583                 rate += vf->min_tx_rate;
1584         rcu_read_unlock();
1585
1586         return rate;
1587 }
1588
1589 /**
1590  * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription
1591  * @vf: VF trying to configure min_tx_rate
1592  * @min_tx_rate: min Tx rate in Mbps
1593  *
1594  * Check if the min_tx_rate being passed in will cause oversubscription of total
1595  * min_tx_rate based on the current link speed and all other VFs configured
1596  * min_tx_rate
1597  *
1598  * Return true if the passed min_tx_rate would cause oversubscription, else
1599  * return false
1600  */
1601 static bool
1602 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate)
1603 {
1604         struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1605         int all_vfs_min_tx_rate;
1606         int link_speed_mbps;
1607
1608         if (WARN_ON(!vsi))
1609                 return false;
1610
1611         link_speed_mbps = ice_get_link_speed_mbps(vsi);
1612         all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf);
1613
1614         /* this VF's previous rate is being overwritten */
1615         all_vfs_min_tx_rate -= vf->min_tx_rate;
1616
1617         if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) {
1618                 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",
1619                         min_tx_rate, vf->vf_id,
1620                         all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps,
1621                         link_speed_mbps);
1622                 return true;
1623         }
1624
1625         return false;
1626 }
1627
1628 /**
1629  * ice_set_vf_bw - set min/max VF bandwidth
1630  * @netdev: network interface device structure
1631  * @vf_id: VF identifier
1632  * @min_tx_rate: Minimum Tx rate in Mbps
1633  * @max_tx_rate: Maximum Tx rate in Mbps
1634  */
1635 int
1636 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
1637               int max_tx_rate)
1638 {
1639         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1640         struct ice_vsi *vsi;
1641         struct device *dev;
1642         struct ice_vf *vf;
1643         int ret;
1644
1645         dev = ice_pf_to_dev(pf);
1646
1647         vf = ice_get_vf_by_id(pf, vf_id);
1648         if (!vf)
1649                 return -EINVAL;
1650
1651         ret = ice_check_vf_ready_for_cfg(vf);
1652         if (ret)
1653                 goto out_put_vf;
1654
1655         vsi = ice_get_vf_vsi(vf);
1656         if (!vsi) {
1657                 ret = -EINVAL;
1658                 goto out_put_vf;
1659         }
1660
1661         if (min_tx_rate && ice_is_dcb_active(pf)) {
1662                 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n");
1663                 ret = -EOPNOTSUPP;
1664                 goto out_put_vf;
1665         }
1666
1667         if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) {
1668                 ret = -EINVAL;
1669                 goto out_put_vf;
1670         }
1671
1672         if (vf->min_tx_rate != (unsigned int)min_tx_rate) {
1673                 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000);
1674                 if (ret) {
1675                         dev_err(dev, "Unable to set min-tx-rate for VF %d\n",
1676                                 vf->vf_id);
1677                         goto out_put_vf;
1678                 }
1679
1680                 vf->min_tx_rate = min_tx_rate;
1681         }
1682
1683         if (vf->max_tx_rate != (unsigned int)max_tx_rate) {
1684                 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000);
1685                 if (ret) {
1686                         dev_err(dev, "Unable to set max-tx-rate for VF %d\n",
1687                                 vf->vf_id);
1688                         goto out_put_vf;
1689                 }
1690
1691                 vf->max_tx_rate = max_tx_rate;
1692         }
1693
1694 out_put_vf:
1695         ice_put_vf(vf);
1696         return ret;
1697 }
1698
1699 /**
1700  * ice_get_vf_stats - populate some stats for the VF
1701  * @netdev: the netdev of the PF
1702  * @vf_id: the host OS identifier (0-255)
1703  * @vf_stats: pointer to the OS memory to be initialized
1704  */
1705 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
1706                      struct ifla_vf_stats *vf_stats)
1707 {
1708         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1709         struct ice_eth_stats *stats;
1710         struct ice_vsi *vsi;
1711         struct ice_vf *vf;
1712         int ret;
1713
1714         vf = ice_get_vf_by_id(pf, vf_id);
1715         if (!vf)
1716                 return -EINVAL;
1717
1718         ret = ice_check_vf_ready_for_cfg(vf);
1719         if (ret)
1720                 goto out_put_vf;
1721
1722         vsi = ice_get_vf_vsi(vf);
1723         if (!vsi) {
1724                 ret = -EINVAL;
1725                 goto out_put_vf;
1726         }
1727
1728         ice_update_eth_stats(vsi);
1729         stats = &vsi->eth_stats;
1730
1731         memset(vf_stats, 0, sizeof(*vf_stats));
1732
1733         vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
1734                 stats->rx_multicast;
1735         vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
1736                 stats->tx_multicast;
1737         vf_stats->rx_bytes   = stats->rx_bytes;
1738         vf_stats->tx_bytes   = stats->tx_bytes;
1739         vf_stats->broadcast  = stats->rx_broadcast;
1740         vf_stats->multicast  = stats->rx_multicast;
1741         vf_stats->rx_dropped = stats->rx_discards;
1742         vf_stats->tx_dropped = stats->tx_discards;
1743
1744 out_put_vf:
1745         ice_put_vf(vf);
1746         return ret;
1747 }
1748
1749 /**
1750  * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported
1751  * @hw: hardware structure used to check the VLAN mode
1752  * @vlan_proto: VLAN TPID being checked
1753  *
1754  * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q
1755  * and ETH_P_8021AD are supported. If the device is configured in Single VLAN
1756  * Mode (SVM), then only ETH_P_8021Q is supported.
1757  */
1758 static bool
1759 ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto)
1760 {
1761         bool is_supported = false;
1762
1763         switch (vlan_proto) {
1764         case ETH_P_8021Q:
1765                 is_supported = true;
1766                 break;
1767         case ETH_P_8021AD:
1768                 if (ice_is_dvm_ena(hw))
1769                         is_supported = true;
1770                 break;
1771         }
1772
1773         return is_supported;
1774 }
1775
1776 /**
1777  * ice_set_vf_port_vlan
1778  * @netdev: network interface device structure
1779  * @vf_id: VF identifier
1780  * @vlan_id: VLAN ID being set
1781  * @qos: priority setting
1782  * @vlan_proto: VLAN protocol
1783  *
1784  * program VF Port VLAN ID and/or QoS
1785  */
1786 int
1787 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
1788                      __be16 vlan_proto)
1789 {
1790         struct ice_pf *pf = ice_netdev_to_pf(netdev);
1791         u16 local_vlan_proto = ntohs(vlan_proto);
1792         struct device *dev;
1793         struct ice_vf *vf;
1794         int ret;
1795
1796         dev = ice_pf_to_dev(pf);
1797
1798         if (vlan_id >= VLAN_N_VID || qos > 7) {
1799                 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
1800                         vf_id, vlan_id, qos);
1801                 return -EINVAL;
1802         }
1803
1804         if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) {
1805                 dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n",
1806                         local_vlan_proto);
1807                 return -EPROTONOSUPPORT;
1808         }
1809
1810         vf = ice_get_vf_by_id(pf, vf_id);
1811         if (!vf)
1812                 return -EINVAL;
1813
1814         ret = ice_check_vf_ready_for_cfg(vf);
1815         if (ret)
1816                 goto out_put_vf;
1817
1818         if (ice_vf_get_port_vlan_prio(vf) == qos &&
1819             ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto &&
1820             ice_vf_get_port_vlan_id(vf) == vlan_id) {
1821                 /* duplicate request, so just return success */
1822                 dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n",
1823                         vlan_id, qos, local_vlan_proto);
1824                 ret = 0;
1825                 goto out_put_vf;
1826         }
1827
1828         mutex_lock(&vf->cfg_lock);
1829
1830         vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos);
1831         if (ice_vf_is_port_vlan_ena(vf))
1832                 dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n",
1833                          vlan_id, qos, local_vlan_proto, vf_id);
1834         else
1835                 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
1836
1837         ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
1838         mutex_unlock(&vf->cfg_lock);
1839
1840 out_put_vf:
1841         ice_put_vf(vf);
1842         return ret;
1843 }
1844
1845 /**
1846  * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
1847  * @vf: pointer to the VF structure
1848  */
1849 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
1850 {
1851         struct ice_pf *pf = vf->pf;
1852         struct device *dev;
1853
1854         dev = ice_pf_to_dev(pf);
1855
1856         dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
1857                  vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
1858                  vf->dev_lan_addr,
1859                  test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
1860                           ? "on" : "off");
1861 }
1862
1863 /**
1864  * ice_print_vfs_mdd_events - print VFs malicious driver detect event
1865  * @pf: pointer to the PF structure
1866  *
1867  * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
1868  */
1869 void ice_print_vfs_mdd_events(struct ice_pf *pf)
1870 {
1871         struct device *dev = ice_pf_to_dev(pf);
1872         struct ice_hw *hw = &pf->hw;
1873         struct ice_vf *vf;
1874         unsigned int bkt;
1875
1876         /* check that there are pending MDD events to print */
1877         if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state))
1878                 return;
1879
1880         /* VF MDD event logs are rate limited to one second intervals */
1881         if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1))
1882                 return;
1883
1884         pf->vfs.last_printed_mdd_jiffies = jiffies;
1885
1886         mutex_lock(&pf->vfs.table_lock);
1887         ice_for_each_vf(pf, bkt, vf) {
1888                 /* only print Rx MDD event message if there are new events */
1889                 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
1890                         vf->mdd_rx_events.last_printed =
1891                                                         vf->mdd_rx_events.count;
1892                         ice_print_vf_rx_mdd_event(vf);
1893                 }
1894
1895                 /* only print Tx MDD event message if there are new events */
1896                 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
1897                         vf->mdd_tx_events.last_printed =
1898                                                         vf->mdd_tx_events.count;
1899
1900                         dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
1901                                  vf->mdd_tx_events.count, hw->pf_id, vf->vf_id,
1902                                  vf->dev_lan_addr);
1903                 }
1904         }
1905         mutex_unlock(&pf->vfs.table_lock);
1906 }
1907
1908 /**
1909  * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
1910  * @pf: pointer to the PF structure
1911  *
1912  * Called when recovering from a PF FLR to restore interrupt capability to
1913  * the VFs.
1914  */
1915 void ice_restore_all_vfs_msi_state(struct ice_pf *pf)
1916 {
1917         struct ice_vf *vf;
1918         u32 bkt;
1919
1920         ice_for_each_vf(pf, bkt, vf)
1921                 pci_restore_msi_state(vf->vfdev);
1922 }