]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ethernet/sfc/ef10_sriov.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / sfc / ef10_sriov.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
834e23dd
SS
2/****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2015 Solarflare Communications Inc.
834e23dd 5 */
4b5b1ac5 6#include <linux/etherdevice.h>
834e23dd
SS
7#include <linux/pci.h>
8#include <linux/module.h>
9#include "net_driver.h"
10#include "ef10_sriov.h"
11#include "efx.h"
12#include "nic.h"
13#include "mcdi_pcol.h"
14
3c5eb876
SS
15static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
16 unsigned int vf_fn)
834e23dd 17{
3c5eb876
SS
18 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
19 struct efx_ef10_nic_data *nic_data = efx->nic_data;
834e23dd 20
3c5eb876
SS
21 MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
22 MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
23 EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
24 EVB_PORT_ASSIGN_IN_VF, vf_fn);
25
26 return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
27 NULL, 0, NULL);
834e23dd
SS
28}
29
6d8aaaf6
DP
30static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
31 unsigned int vswitch_type)
32{
33 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
2d432f20 34 int rc;
6d8aaaf6
DP
35
36 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
37 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
2d432f20 38 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
6d8aaaf6
DP
39 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
40 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
41
2d432f20
SS
42 /* Quietly try to allocate 2 VLAN tags */
43 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
44 NULL, 0, NULL);
45
46 /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
47 if (rc == -EPROTO) {
48 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
49 rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
50 sizeof(inbuf), NULL, 0, NULL);
51 } else if (rc) {
52 efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
53 MC_CMD_VSWITCH_ALLOC_IN_LEN,
54 NULL, 0, rc);
55 }
56 return rc;
6d8aaaf6
DP
57}
58
59static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
60{
61 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
62
63 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
64
65 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
66 NULL, 0, NULL);
67}
68
69static int efx_ef10_vport_alloc(struct efx_nic *efx,
70 unsigned int port_id_in,
71 unsigned int vport_type,
2d432f20 72 u16 vlan,
6d8aaaf6
DP
73 unsigned int *port_id_out)
74{
75 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
76 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
77 size_t outlen;
78 int rc;
79
80 EFX_WARN_ON_PARANOID(!port_id_out);
81
82 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
83 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
2d432f20
SS
84 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
85 (vlan != EFX_EF10_NO_VLAN));
6d8aaaf6
DP
86 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
87 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
2d432f20
SS
88 if (vlan != EFX_EF10_NO_VLAN)
89 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
90 VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
6d8aaaf6
DP
91
92 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
93 outbuf, sizeof(outbuf), &outlen);
94 if (rc)
95 return rc;
96 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
97 return -EIO;
98
99 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
100 return 0;
101}
102
103static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
104{
105 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
106
107 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
108
109 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
110 NULL, 0, NULL);
111}
112
3c5eb876
SS
113static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
114{
115 struct efx_ef10_nic_data *nic_data = efx->nic_data;
116 int i;
117
118 if (!nic_data->vf)
119 return;
120
121 for (i = 0; i < efx->vf_count; i++) {
122 struct ef10_vf *vf = nic_data->vf + i;
123
6598dad2
DP
124 /* If VF is assigned, do not free the vport */
125 if (vf->pci_dev &&
126 vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
127 continue;
128
3c5eb876
SS
129 if (vf->vport_assigned) {
130 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
131 vf->vport_assigned = 0;
132 }
133
134 if (!is_zero_ether_addr(vf->mac)) {
135 efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
136 eth_zero_addr(vf->mac);
137 }
138
139 if (vf->vport_id) {
140 efx_ef10_vport_free(efx, vf->vport_id);
141 vf->vport_id = 0;
142 }
f1122a34
SS
143
144 vf->efx = NULL;
3c5eb876
SS
145 }
146}
147
148static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
149{
150 struct efx_ef10_nic_data *nic_data = efx->nic_data;
151
152 efx_ef10_sriov_free_vf_vports(efx);
153 kfree(nic_data->vf);
154 nic_data->vf = NULL;
155}
156
157static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
158 unsigned int vf_i)
159{
160 struct efx_ef10_nic_data *nic_data = efx->nic_data;
161 struct ef10_vf *vf = nic_data->vf + vf_i;
162 int rc;
163
164 if (WARN_ON_ONCE(!nic_data->vf))
165 return -EOPNOTSUPP;
166
167 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
168 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
2d432f20 169 vf->vlan, &vf->vport_id);
3c5eb876
SS
170 if (rc)
171 return rc;
172
173 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
174 if (rc) {
175 eth_zero_addr(vf->mac);
176 return rc;
177 }
178
179 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
180 if (rc)
181 return rc;
182
183 vf->vport_assigned = 1;
184 return 0;
185}
186
187static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
188{
189 struct efx_ef10_nic_data *nic_data = efx->nic_data;
190 unsigned int i;
191 int rc;
192
193 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
194 GFP_KERNEL);
195 if (!nic_data->vf)
196 return -ENOMEM;
197
198 for (i = 0; i < efx->vf_count; i++) {
6c1f0a1f 199 eth_random_addr(nic_data->vf[i].mac);
f1122a34 200 nic_data->vf[i].efx = NULL;
2d432f20 201 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
3c5eb876
SS
202
203 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
204 if (rc)
205 goto fail;
206 }
207
208 return 0;
209fail:
210 efx_ef10_sriov_free_vf_vports(efx);
211 kfree(nic_data->vf);
212 nic_data->vf = NULL;
213 return rc;
214}
215
216static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
217{
218 unsigned int i;
219 int rc;
220
221 for (i = 0; i < efx->vf_count; i++) {
222 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
223 if (rc)
224 goto fail;
225 }
226
227 return 0;
228fail:
229 efx_ef10_sriov_free_vf_vswitching(efx);
230 return rc;
231}
232
38d27f38
AR
233static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
234{
235 struct efx_ef10_nic_data *nic_data = efx->nic_data;
236 u32 port_flags;
237 int rc;
238
239 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
240 if (rc)
241 goto fail_vadaptor_alloc;
242
243 rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
244 &port_flags, NULL, NULL);
245 if (rc)
246 goto fail_vadaptor_query;
247
248 if (port_flags &
249 (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
250 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
251 else
252 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
253
254 return 0;
255
256fail_vadaptor_query:
257 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
258fail_vadaptor_alloc:
259 return rc;
260}
261
6d8aaaf6
DP
262/* On top of the default firmware vswitch setup, create a VEB vswitch and
263 * expansion vport for use by this function.
264 */
7b8c7b54 265int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
6d8aaaf6
DP
266{
267 struct efx_ef10_nic_data *nic_data = efx->nic_data;
3c5eb876 268 struct net_device *net_dev = efx->net_dev;
6d8aaaf6
DP
269 int rc;
270
7b8c7b54
SS
271 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
272 /* vswitch not needed as we have no VFs */
38d27f38 273 efx_ef10_vadaptor_alloc_set_features(efx);
7b8c7b54
SS
274 return 0;
275 }
6d8aaaf6
DP
276
277 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
278 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
279 if (rc)
280 goto fail1;
281
282 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
283 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
2d432f20 284 EFX_EF10_NO_VLAN, &nic_data->vport_id);
6d8aaaf6
DP
285 if (rc)
286 goto fail2;
287
3c5eb876
SS
288 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
289 if (rc)
290 goto fail3;
3c5eb876
SS
291 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
292
38d27f38 293 rc = efx_ef10_vadaptor_alloc_set_features(efx);
7b8c7b54
SS
294 if (rc)
295 goto fail4;
296
6d8aaaf6 297 return 0;
7b8c7b54
SS
298fail4:
299 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
300 eth_zero_addr(nic_data->vport_mac);
3c5eb876
SS
301fail3:
302 efx_ef10_vport_free(efx, nic_data->vport_id);
303 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
6d8aaaf6
DP
304fail2:
305 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
306fail1:
307 return rc;
308}
309
7b8c7b54
SS
310int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
311{
38d27f38 312 return efx_ef10_vadaptor_alloc_set_features(efx);
7b8c7b54
SS
313}
314
315int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
6d8aaaf6
DP
316{
317 struct efx_ef10_nic_data *nic_data = efx->nic_data;
318 int rc;
319
320 if (!nic_data->must_probe_vswitching)
321 return 0;
322
7b8c7b54 323 rc = efx_ef10_vswitching_probe_pf(efx);
3c5eb876
SS
324 if (rc)
325 goto fail;
326
327 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
328 if (rc)
329 goto fail;
6d8aaaf6 330
3c5eb876
SS
331 nic_data->must_probe_vswitching = false;
332fail:
6d8aaaf6
DP
333 return rc;
334}
335
7b8c7b54
SS
336int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
337{
338 struct efx_ef10_nic_data *nic_data = efx->nic_data;
339 int rc;
340
341 if (!nic_data->must_probe_vswitching)
342 return 0;
343
344 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
345 if (rc)
346 return rc;
347
348 nic_data->must_probe_vswitching = false;
349 return 0;
350}
351
352void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
6d8aaaf6
DP
353{
354 struct efx_ef10_nic_data *nic_data = efx->nic_data;
355
3c5eb876
SS
356 efx_ef10_sriov_free_vf_vswitching(efx);
357
7b8c7b54
SS
358 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
359
6d8aaaf6
DP
360 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
361 return; /* No vswitch was ever created */
362
3c5eb876
SS
363 if (!is_zero_ether_addr(nic_data->vport_mac)) {
364 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
365 efx->net_dev->dev_addr);
366 eth_zero_addr(nic_data->vport_mac);
367 }
6d8aaaf6
DP
368 efx_ef10_vport_free(efx, nic_data->vport_id);
369 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
370
6598dad2
DP
371 /* Only free the vswitch if no VFs are assigned */
372 if (!pci_vfs_assigned(efx->pci_dev))
373 efx_ef10_vswitch_free(efx, nic_data->vport_id);
6d8aaaf6 374}
3c5eb876 375
7b8c7b54
SS
376void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
377{
378 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
379}
380
3c5eb876
SS
381static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
382{
383 int rc = 0;
384 struct pci_dev *dev = efx->pci_dev;
385
386 efx->vf_count = num_vfs;
387
388 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
389 if (rc)
390 goto fail1;
391
392 rc = pci_enable_sriov(dev, num_vfs);
393 if (rc)
394 goto fail2;
395
396 return 0;
397fail2:
398 efx_ef10_sriov_free_vf_vswitching(efx);
399fail1:
400 efx->vf_count = 0;
401 netif_err(efx, probe, efx->net_dev,
402 "Failed to enable SRIOV VFs\n");
403 return rc;
404}
405
6598dad2 406static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
3c5eb876
SS
407{
408 struct pci_dev *dev = efx->pci_dev;
6598dad2 409 unsigned int vfs_assigned = 0;
3c5eb876 410
6598dad2 411 vfs_assigned = pci_vfs_assigned(dev);
71158bf2 412
6598dad2
DP
413 if (vfs_assigned && !force) {
414 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
415 "please detach them before disabling SR-IOV\n");
71158bf2
DP
416 return -EBUSY;
417 }
418
6598dad2
DP
419 if (!vfs_assigned)
420 pci_disable_sriov(dev);
421
3c5eb876
SS
422 efx_ef10_sriov_free_vf_vswitching(efx);
423 efx->vf_count = 0;
424 return 0;
425}
426
427int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
428{
429 if (num_vfs == 0)
6598dad2 430 return efx_ef10_pci_sriov_disable(efx, false);
3c5eb876
SS
431 else
432 return efx_ef10_pci_sriov_enable(efx, num_vfs);
433}
434
435int efx_ef10_sriov_init(struct efx_nic *efx)
436{
437 return 0;
438}
439
440void efx_ef10_sriov_fini(struct efx_nic *efx)
441{
442 struct efx_ef10_nic_data *nic_data = efx->nic_data;
2a3fc311 443 unsigned int i;
3c5eb876
SS
444 int rc;
445
6598dad2
DP
446 if (!nic_data->vf) {
447 /* Remove any un-assigned orphaned VFs */
448 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
449 pci_disable_sriov(efx->pci_dev);
3c5eb876 450 return;
6598dad2 451 }
3c5eb876 452
2a3fc311
DP
453 /* Remove any VFs in the host */
454 for (i = 0; i < efx->vf_count; ++i) {
455 struct efx_nic *vf_efx = nic_data->vf[i].efx;
456
457 if (vf_efx)
458 vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
459 }
460
6598dad2 461 rc = efx_ef10_pci_sriov_disable(efx, true);
3c5eb876
SS
462 if (rc)
463 netif_dbg(efx, drv, efx->net_dev,
464 "Disabling SRIOV was not successful rc=%d\n", rc);
465 else
466 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
467}
e340be92
SS
468
469static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
470 u8 *mac)
471{
472 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
473 MCDI_DECLARE_BUF_ERR(outbuf);
474 size_t outlen;
475 int rc;
476
477 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
478 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
479
480 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
481 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
482
483 return rc;
484}
485
486int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
487{
488 struct efx_ef10_nic_data *nic_data = efx->nic_data;
489 struct ef10_vf *vf;
490 int rc;
491
492 if (!nic_data->vf)
493 return -EOPNOTSUPP;
494
495 if (vf_i >= efx->vf_count)
496 return -EINVAL;
497 vf = nic_data->vf + vf_i;
498
499 if (vf->efx) {
500 efx_device_detach_sync(vf->efx);
501 efx_net_stop(vf->efx->net_dev);
502
503 down_write(&vf->efx->filter_sem);
504 vf->efx->type->filter_table_remove(vf->efx);
505
506 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
507 if (rc) {
508 up_write(&vf->efx->filter_sem);
509 return rc;
510 }
511 }
512
513 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
514 if (rc)
515 return rc;
516
517 if (!is_zero_ether_addr(vf->mac)) {
518 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
519 if (rc)
520 return rc;
521 }
522
523 if (!is_zero_ether_addr(mac)) {
524 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
525 if (rc) {
526 eth_zero_addr(vf->mac);
527 goto fail;
528 }
529 if (vf->efx)
530 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
531 }
532
533 ether_addr_copy(vf->mac, mac);
534
535 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
536 if (rc)
537 goto fail;
538
539 if (vf->efx) {
540 /* VF cannot use the vport_id that the PF created */
541 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
542 if (rc) {
543 up_write(&vf->efx->filter_sem);
544 return rc;
545 }
546 vf->efx->type->filter_table_probe(vf->efx);
547 up_write(&vf->efx->filter_sem);
548 efx_net_open(vf->efx->net_dev);
9c568fd8 549 efx_device_attach_if_not_resetting(vf->efx);
e340be92
SS
550 }
551
552 return 0;
553
554fail:
4b5b1ac5 555 eth_zero_addr(vf->mac);
e340be92
SS
556 return rc;
557}
b9af9049 558
2d432f20
SS
559int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
560 u8 qos)
561{
562 struct efx_ef10_nic_data *nic_data = efx->nic_data;
563 struct ef10_vf *vf;
2e6dde5c 564 u16 new_vlan;
2d432f20
SS
565 int rc = 0, rc2 = 0;
566
567 if (vf_i >= efx->vf_count)
568 return -EINVAL;
569 if (qos != 0)
570 return -EINVAL;
571
572 vf = nic_data->vf + vf_i;
573
574 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
575 if (new_vlan == vf->vlan)
576 return 0;
577
578 if (vf->efx) {
579 efx_device_detach_sync(vf->efx);
580 efx_net_stop(vf->efx->net_dev);
581
d248953a 582 mutex_lock(&vf->efx->mac_lock);
2d432f20
SS
583 down_write(&vf->efx->filter_sem);
584 vf->efx->type->filter_table_remove(vf->efx);
585
586 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
587 if (rc)
588 goto restore_filters;
589 }
590
591 if (vf->vport_assigned) {
592 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
593 if (rc) {
594 netif_warn(efx, drv, efx->net_dev,
595 "Failed to change vlan on VF %d.\n", vf_i);
596 netif_warn(efx, drv, efx->net_dev,
597 "This is likely because the VF is bound to a driver in a VM.\n");
598 netif_warn(efx, drv, efx->net_dev,
599 "Please unload the driver in the VM.\n");
600 goto restore_vadaptor;
601 }
602 vf->vport_assigned = 0;
603 }
604
605 if (!is_zero_ether_addr(vf->mac)) {
606 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
607 if (rc)
608 goto restore_evb_port;
609 }
610
611 if (vf->vport_id) {
612 rc = efx_ef10_vport_free(efx, vf->vport_id);
613 if (rc)
614 goto restore_mac;
615 vf->vport_id = 0;
616 }
617
618 /* Do the actual vlan change */
2d432f20
SS
619 vf->vlan = new_vlan;
620
621 /* Restore everything in reverse order */
622 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
623 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
624 vf->vlan, &vf->vport_id);
625 if (rc)
671b53ee 626 goto reset_nic_up_write;
2d432f20
SS
627
628restore_mac:
629 if (!is_zero_ether_addr(vf->mac)) {
630 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
631 if (rc2) {
632 eth_zero_addr(vf->mac);
671b53ee 633 goto reset_nic_up_write;
2d432f20
SS
634 }
635 }
636
637restore_evb_port:
638 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
639 if (rc2)
671b53ee 640 goto reset_nic_up_write;
2d432f20
SS
641 else
642 vf->vport_assigned = 1;
643
644restore_vadaptor:
645 if (vf->efx) {
646 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
647 if (rc2)
671b53ee 648 goto reset_nic_up_write;
2d432f20
SS
649 }
650
651restore_filters:
652 if (vf->efx) {
653 rc2 = vf->efx->type->filter_table_probe(vf->efx);
654 if (rc2)
671b53ee
SS
655 goto reset_nic_up_write;
656
657 up_write(&vf->efx->filter_sem);
d248953a 658 mutex_unlock(&vf->efx->mac_lock);
2d432f20 659
2d432f20
SS
660 rc2 = efx_net_open(vf->efx->net_dev);
661 if (rc2)
662 goto reset_nic;
663
9c568fd8 664 efx_device_attach_if_not_resetting(vf->efx);
2d432f20
SS
665 }
666 return rc;
667
671b53ee 668reset_nic_up_write:
d248953a 669 if (vf->efx) {
671b53ee 670 up_write(&vf->efx->filter_sem);
d248953a
MH
671 mutex_unlock(&vf->efx->mac_lock);
672 }
2d432f20
SS
673reset_nic:
674 if (vf->efx) {
2d432f20
SS
675 netif_err(efx, drv, efx->net_dev,
676 "Failed to restore VF - scheduling reset.\n");
677 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
678 } else {
679 netif_err(efx, drv, efx->net_dev,
680 "Failed to restore the VF and cannot reset the VF "
681 "- VF is not functional.\n");
682 netif_err(efx, drv, efx->net_dev,
683 "Please reload the driver attached to the VF.\n");
684 }
685
686 return rc ? rc : rc2;
687}
688
860d2ffa
SS
689int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
690 bool spoofchk)
691{
692 return spoofchk ? -EOPNOTSUPP : 0;
693}
694
4392dc69
EC
695int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
696 int link_state)
697{
698 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
699 struct efx_ef10_nic_data *nic_data = efx->nic_data;
700
701 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
702 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
703 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
704 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
705 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
706 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
707 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
708 LINK_STATE_MODE_IN_FUNCTION_PF,
709 nic_data->pf_index,
710 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
711 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
712 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
713 NULL, 0, NULL); /* don't care what old mode was */
714}
715
b9af9049
SS
716int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
717 struct ifla_vf_info *ivf)
718{
4392dc69
EC
719 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
720 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
721
b9af9049
SS
722 struct efx_ef10_nic_data *nic_data = efx->nic_data;
723 struct ef10_vf *vf;
4392dc69
EC
724 size_t outlen;
725 int rc;
b9af9049
SS
726
727 if (vf_i >= efx->vf_count)
728 return -EINVAL;
729
730 if (!nic_data->vf)
731 return -EOPNOTSUPP;
732
733 vf = nic_data->vf + vf_i;
734
735 ivf->vf = vf_i;
736 ivf->min_tx_rate = 0;
737 ivf->max_tx_rate = 0;
738 ether_addr_copy(ivf->mac, vf->mac);
2d432f20 739 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
b9af9049
SS
740 ivf->qos = 0;
741
4392dc69
EC
742 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
743 LINK_STATE_MODE_IN_FUNCTION_PF,
744 nic_data->pf_index,
745 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
746 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
747 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
748 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
749 outbuf, sizeof(outbuf), &outlen);
750 if (rc)
751 return rc;
752 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
753 return -EIO;
754 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
755
b9af9049
SS
756 return 0;
757}