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