1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2015 Solarflare Communications Inc.
5 */
6 #include <linux/etherdevice.h>
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
efx_ef10_evb_port_assign(struct efx_nic * efx,unsigned int port_id,unsigned int vf_fn)15 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
16 unsigned int vf_fn)
17 {
18 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
19 struct efx_ef10_nic_data *nic_data = efx->nic_data;
20
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);
28 }
29
efx_ef10_vswitch_alloc(struct efx_nic * efx,unsigned int port_id,unsigned int vswitch_type)30 static 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);
34 int rc;
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);
38 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
39 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
40 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
41
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;
57 }
58
efx_ef10_vswitch_free(struct efx_nic * efx,unsigned int port_id)59 static 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
efx_ef10_vport_alloc(struct efx_nic * efx,unsigned int port_id_in,unsigned int vport_type,u16 vlan,unsigned int * port_id_out)69 static int efx_ef10_vport_alloc(struct efx_nic *efx,
70 unsigned int port_id_in,
71 unsigned int vport_type,
72 u16 vlan,
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);
84 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
85 (vlan != EFX_EF10_NO_VLAN));
86 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
87 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
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);
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
efx_ef10_vport_free(struct efx_nic * efx,unsigned int port_id)103 static 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
efx_ef10_sriov_free_vf_vports(struct efx_nic * efx)113 static 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
124 /* If VF is assigned, do not free the vport */
125 if (vf->pci_dev && pci_is_dev_assigned(vf->pci_dev))
126 continue;
127
128 if (vf->vport_assigned) {
129 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
130 vf->vport_assigned = 0;
131 }
132
133 if (!is_zero_ether_addr(vf->mac)) {
134 efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
135 eth_zero_addr(vf->mac);
136 }
137
138 if (vf->vport_id) {
139 efx_ef10_vport_free(efx, vf->vport_id);
140 vf->vport_id = 0;
141 }
142
143 vf->efx = NULL;
144 }
145 }
146
efx_ef10_sriov_free_vf_vswitching(struct efx_nic * efx)147 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
148 {
149 struct efx_ef10_nic_data *nic_data = efx->nic_data;
150
151 efx_ef10_sriov_free_vf_vports(efx);
152 kfree(nic_data->vf);
153 nic_data->vf = NULL;
154 }
155
efx_ef10_sriov_assign_vf_vport(struct efx_nic * efx,unsigned int vf_i)156 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
157 unsigned int vf_i)
158 {
159 struct efx_ef10_nic_data *nic_data = efx->nic_data;
160 struct ef10_vf *vf = nic_data->vf + vf_i;
161 int rc;
162
163 if (WARN_ON_ONCE(!nic_data->vf))
164 return -EOPNOTSUPP;
165
166 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
167 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
168 vf->vlan, &vf->vport_id);
169 if (rc)
170 return rc;
171
172 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
173 if (rc) {
174 eth_zero_addr(vf->mac);
175 return rc;
176 }
177
178 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
179 if (rc)
180 return rc;
181
182 vf->vport_assigned = 1;
183 return 0;
184 }
185
efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic * efx)186 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
187 {
188 struct efx_ef10_nic_data *nic_data = efx->nic_data;
189 unsigned int i;
190 int rc;
191
192 nic_data->vf = kzalloc_objs(struct ef10_vf, efx->vf_count);
193 if (!nic_data->vf)
194 return -ENOMEM;
195
196 for (i = 0; i < efx->vf_count; i++) {
197 eth_random_addr(nic_data->vf[i].mac);
198 nic_data->vf[i].efx = NULL;
199 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
200
201 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
202 if (rc)
203 goto fail;
204 }
205
206 return 0;
207 fail:
208 efx_ef10_sriov_free_vf_vswitching(efx);
209 return rc;
210 }
211
efx_ef10_sriov_restore_vf_vswitching(struct efx_nic * efx)212 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
213 {
214 unsigned int i;
215 int rc;
216
217 for (i = 0; i < efx->vf_count; i++) {
218 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
219 if (rc)
220 goto fail;
221 }
222
223 return 0;
224 fail:
225 efx_ef10_sriov_free_vf_vswitching(efx);
226 return rc;
227 }
228
efx_ef10_vadaptor_alloc_set_features(struct efx_nic * efx)229 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
230 {
231 u32 port_flags;
232 int rc;
233
234 rc = efx_ef10_vadaptor_alloc(efx, efx->vport_id);
235 if (rc)
236 goto fail_vadaptor_alloc;
237
238 rc = efx_ef10_vadaptor_query(efx, efx->vport_id,
239 &port_flags, NULL, NULL);
240 if (rc)
241 goto fail_vadaptor_query;
242
243 if (port_flags &
244 (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
245 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
246 else
247 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
248
249 return 0;
250
251 fail_vadaptor_query:
252 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
253 fail_vadaptor_alloc:
254 return rc;
255 }
256
257 /* On top of the default firmware vswitch setup, create a VEB vswitch and
258 * expansion vport for use by this function.
259 */
efx_ef10_vswitching_probe_pf(struct efx_nic * efx)260 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
261 {
262 struct efx_ef10_nic_data *nic_data = efx->nic_data;
263 struct net_device *net_dev = efx->net_dev;
264 int rc;
265
266 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
267 /* vswitch not needed as we have no VFs */
268 efx_ef10_vadaptor_alloc_set_features(efx);
269 return 0;
270 }
271
272 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
273 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
274 if (rc)
275 goto fail1;
276
277 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
278 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
279 EFX_EF10_NO_VLAN, &efx->vport_id);
280 if (rc)
281 goto fail2;
282
283 rc = efx_ef10_vport_add_mac(efx, efx->vport_id, net_dev->dev_addr);
284 if (rc)
285 goto fail3;
286 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
287
288 rc = efx_ef10_vadaptor_alloc_set_features(efx);
289 if (rc)
290 goto fail4;
291
292 return 0;
293 fail4:
294 efx_ef10_vport_del_mac(efx, efx->vport_id, nic_data->vport_mac);
295 eth_zero_addr(nic_data->vport_mac);
296 fail3:
297 efx_ef10_vport_free(efx, efx->vport_id);
298 efx->vport_id = EVB_PORT_ID_ASSIGNED;
299 fail2:
300 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
301 fail1:
302 return rc;
303 }
304
efx_ef10_vswitching_probe_vf(struct efx_nic * efx)305 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
306 {
307 return efx_ef10_vadaptor_alloc_set_features(efx);
308 }
309
efx_ef10_vswitching_restore_pf(struct efx_nic * efx)310 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
311 {
312 struct efx_ef10_nic_data *nic_data = efx->nic_data;
313 int rc;
314
315 if (!nic_data->must_probe_vswitching)
316 return 0;
317
318 rc = efx_ef10_vswitching_probe_pf(efx);
319 if (rc)
320 goto fail;
321
322 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
323 if (rc)
324 goto fail;
325
326 nic_data->must_probe_vswitching = false;
327 fail:
328 return rc;
329 }
330
efx_ef10_vswitching_restore_vf(struct efx_nic * efx)331 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
332 {
333 struct efx_ef10_nic_data *nic_data = efx->nic_data;
334 int rc;
335
336 if (!nic_data->must_probe_vswitching)
337 return 0;
338
339 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
340 if (rc)
341 return rc;
342
343 nic_data->must_probe_vswitching = false;
344 return 0;
345 }
346
efx_ef10_vswitching_remove_pf(struct efx_nic * efx)347 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
348 {
349 struct efx_ef10_nic_data *nic_data = efx->nic_data;
350
351 efx_ef10_sriov_free_vf_vswitching(efx);
352
353 efx_ef10_vadaptor_free(efx, efx->vport_id);
354
355 if (efx->vport_id == EVB_PORT_ID_ASSIGNED)
356 return; /* No vswitch was ever created */
357
358 if (!is_zero_ether_addr(nic_data->vport_mac)) {
359 efx_ef10_vport_del_mac(efx, efx->vport_id,
360 efx->net_dev->dev_addr);
361 eth_zero_addr(nic_data->vport_mac);
362 }
363 efx_ef10_vport_free(efx, efx->vport_id);
364 efx->vport_id = EVB_PORT_ID_ASSIGNED;
365
366 /* Only free the vswitch if no VFs are assigned */
367 if (!pci_vfs_assigned(efx->pci_dev))
368 efx_ef10_vswitch_free(efx, efx->vport_id);
369 }
370
efx_ef10_vswitching_remove_vf(struct efx_nic * efx)371 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
372 {
373 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
374 }
375
efx_ef10_pci_sriov_enable(struct efx_nic * efx,int num_vfs)376 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
377 {
378 int rc = 0;
379 struct pci_dev *dev = efx->pci_dev;
380
381 efx->vf_count = num_vfs;
382
383 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
384 if (rc)
385 goto fail1;
386
387 rc = pci_enable_sriov(dev, num_vfs);
388 if (rc)
389 goto fail2;
390
391 return 0;
392 fail2:
393 efx_ef10_sriov_free_vf_vswitching(efx);
394 fail1:
395 efx->vf_count = 0;
396 netif_err(efx, probe, efx->net_dev,
397 "Failed to enable SRIOV VFs\n");
398 return rc;
399 }
400
401 /* Disable SRIOV and remove VFs
402 * If some VFs are attached to a guest (using Xen, only) nothing is
403 * done if force=false, and vports are freed if force=true (for the non
404 * attachedc ones, only) but SRIOV is not disabled and VFs are not
405 * removed in either case.
406 */
efx_ef10_pci_sriov_disable(struct efx_nic * efx,bool force)407 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
408 {
409 struct pci_dev *dev = efx->pci_dev;
410 struct efx_ef10_nic_data *nic_data = efx->nic_data;
411 unsigned int vfs_assigned = pci_vfs_assigned(dev);
412 int i, rc = 0;
413
414 if (vfs_assigned && !force) {
415 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
416 "please detach them before disabling SR-IOV\n");
417 return -EBUSY;
418 }
419
420 if (!vfs_assigned) {
421 for (i = 0; i < efx->vf_count; i++)
422 nic_data->vf[i].pci_dev = NULL;
423 pci_disable_sriov(dev);
424 } else {
425 rc = -EBUSY;
426 }
427
428 efx_ef10_sriov_free_vf_vswitching(efx);
429 efx->vf_count = 0;
430 return rc;
431 }
432
efx_ef10_sriov_configure(struct efx_nic * efx,int num_vfs)433 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
434 {
435 if (num_vfs == 0)
436 return efx_ef10_pci_sriov_disable(efx, false);
437 else
438 return efx_ef10_pci_sriov_enable(efx, num_vfs);
439 }
440
efx_ef10_sriov_init(struct efx_nic * efx)441 int efx_ef10_sriov_init(struct efx_nic *efx)
442 {
443 return 0;
444 }
445
efx_ef10_sriov_fini(struct efx_nic * efx)446 void efx_ef10_sriov_fini(struct efx_nic *efx)
447 {
448 struct efx_ef10_nic_data *nic_data = efx->nic_data;
449 int rc;
450
451 if (!nic_data->vf) {
452 /* Remove any un-assigned orphaned VFs. This can happen if the PF driver
453 * was unloaded while any VF was assigned to a guest (using Xen, only).
454 */
455 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
456 pci_disable_sriov(efx->pci_dev);
457 return;
458 }
459
460 /* Disable SRIOV and remove any VFs in the host */
461 rc = efx_ef10_pci_sriov_disable(efx, true);
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 }
468
efx_ef10_vport_del_vf_mac(struct efx_nic * efx,unsigned int port_id,u8 * mac)469 static 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
efx_ef10_sriov_set_vf_mac(struct efx_nic * efx,int vf_i,const u8 * mac)486 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, const 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 vf->efx->type->filter_table_remove(vf->efx);
504
505 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
506 if (rc)
507 return rc;
508 }
509
510 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
511 if (rc)
512 return rc;
513
514 if (!is_zero_ether_addr(vf->mac)) {
515 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
516 if (rc)
517 return rc;
518 }
519
520 if (!is_zero_ether_addr(mac)) {
521 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
522 if (rc)
523 goto fail;
524
525 if (vf->efx)
526 eth_hw_addr_set(vf->efx->net_dev, mac);
527 }
528
529 ether_addr_copy(vf->mac, mac);
530
531 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
532 if (rc)
533 goto fail;
534
535 if (vf->efx) {
536 /* VF cannot use the vport_id that the PF created */
537 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
538 if (rc)
539 return rc;
540 vf->efx->type->filter_table_probe(vf->efx);
541 efx_net_open(vf->efx->net_dev);
542 efx_device_attach_if_not_resetting(vf->efx);
543 }
544
545 return 0;
546
547 fail:
548 eth_zero_addr(vf->mac);
549 return rc;
550 }
551
efx_ef10_sriov_set_vf_vlan(struct efx_nic * efx,int vf_i,u16 vlan,u8 qos)552 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
553 u8 qos)
554 {
555 struct efx_ef10_nic_data *nic_data = efx->nic_data;
556 struct ef10_vf *vf;
557 u16 new_vlan;
558 int rc = 0, rc2 = 0;
559
560 if (vf_i >= efx->vf_count)
561 return -EINVAL;
562 if (qos != 0)
563 return -EINVAL;
564
565 vf = nic_data->vf + vf_i;
566
567 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
568 if (new_vlan == vf->vlan)
569 return 0;
570
571 if (vf->efx) {
572 efx_device_detach_sync(vf->efx);
573 efx_net_stop(vf->efx->net_dev);
574
575 mutex_lock(&vf->efx->mac_lock);
576 vf->efx->type->filter_table_remove(vf->efx);
577
578 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
579 if (rc)
580 goto restore_filters;
581 }
582
583 if (vf->vport_assigned) {
584 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
585 if (rc) {
586 netif_warn(efx, drv, efx->net_dev,
587 "Failed to change vlan on VF %d.\n", vf_i);
588 netif_warn(efx, drv, efx->net_dev,
589 "This is likely because the VF is bound to a driver in a VM.\n");
590 netif_warn(efx, drv, efx->net_dev,
591 "Please unload the driver in the VM.\n");
592 goto restore_vadaptor;
593 }
594 vf->vport_assigned = 0;
595 }
596
597 if (!is_zero_ether_addr(vf->mac)) {
598 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
599 if (rc)
600 goto restore_evb_port;
601 }
602
603 if (vf->vport_id) {
604 rc = efx_ef10_vport_free(efx, vf->vport_id);
605 if (rc)
606 goto restore_mac;
607 vf->vport_id = 0;
608 }
609
610 /* Do the actual vlan change */
611 vf->vlan = new_vlan;
612
613 /* Restore everything in reverse order */
614 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
615 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
616 vf->vlan, &vf->vport_id);
617 if (rc)
618 goto reset_nic_up_write;
619
620 restore_mac:
621 if (!is_zero_ether_addr(vf->mac)) {
622 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
623 if (rc2) {
624 eth_zero_addr(vf->mac);
625 goto reset_nic_up_write;
626 }
627 }
628
629 restore_evb_port:
630 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
631 if (rc2)
632 goto reset_nic_up_write;
633 else
634 vf->vport_assigned = 1;
635
636 restore_vadaptor:
637 if (vf->efx) {
638 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
639 if (rc2)
640 goto reset_nic_up_write;
641 }
642
643 restore_filters:
644 if (vf->efx) {
645 rc2 = vf->efx->type->filter_table_probe(vf->efx);
646 if (rc2)
647 goto reset_nic_up_write;
648
649 mutex_unlock(&vf->efx->mac_lock);
650
651 rc2 = efx_net_open(vf->efx->net_dev);
652 if (rc2)
653 goto reset_nic;
654
655 efx_device_attach_if_not_resetting(vf->efx);
656 }
657 return rc;
658
659 reset_nic_up_write:
660 if (vf->efx)
661 mutex_unlock(&vf->efx->mac_lock);
662 reset_nic:
663 if (vf->efx) {
664 netif_err(efx, drv, efx->net_dev,
665 "Failed to restore VF - scheduling reset.\n");
666 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
667 } else {
668 netif_err(efx, drv, efx->net_dev,
669 "Failed to restore the VF and cannot reset the VF "
670 "- VF is not functional.\n");
671 netif_err(efx, drv, efx->net_dev,
672 "Please reload the driver attached to the VF.\n");
673 }
674
675 return rc ? rc : rc2;
676 }
677
efx_ef10_sriov_set_privilege_mask(struct efx_nic * efx,int vf_i,u32 mask,u32 value)678 static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
679 u32 mask, u32 value)
680 {
681 MCDI_DECLARE_BUF(pm_outbuf, MC_CMD_PRIVILEGE_MASK_OUT_LEN);
682 MCDI_DECLARE_BUF(pm_inbuf, MC_CMD_PRIVILEGE_MASK_IN_LEN);
683 struct efx_ef10_nic_data *nic_data = efx->nic_data;
684 u32 old_mask, new_mask;
685 size_t outlen;
686 int rc;
687
688 EFX_WARN_ON_PARANOID((value & ~mask) != 0);
689
690 /* Get privilege mask */
691 MCDI_POPULATE_DWORD_2(pm_inbuf, PRIVILEGE_MASK_IN_FUNCTION,
692 PRIVILEGE_MASK_IN_FUNCTION_PF, nic_data->pf_index,
693 PRIVILEGE_MASK_IN_FUNCTION_VF, vf_i);
694
695 rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
696 pm_inbuf, sizeof(pm_inbuf),
697 pm_outbuf, sizeof(pm_outbuf), &outlen);
698
699 if (rc != 0)
700 return rc;
701 if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
702 return -EIO;
703
704 old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
705
706 new_mask = old_mask & ~mask;
707 new_mask |= value;
708
709 if (new_mask == old_mask)
710 return 0;
711
712 new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
713
714 /* Set privilege mask */
715 MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
716
717 rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
718 pm_inbuf, sizeof(pm_inbuf),
719 pm_outbuf, sizeof(pm_outbuf), &outlen);
720
721 if (rc != 0)
722 return rc;
723 if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
724 return -EIO;
725
726 return 0;
727 }
728
efx_ef10_sriov_set_vf_spoofchk(struct efx_nic * efx,int vf_i,bool spoofchk)729 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
730 {
731 struct efx_ef10_nic_data *nic_data = efx->nic_data;
732
733 /* Can't enable spoofchk if firmware doesn't support it. */
734 if (!(nic_data->datapath_caps &
735 BIT(MC_CMD_GET_CAPABILITIES_OUT_TX_MAC_SECURITY_FILTERING_LBN)) &&
736 spoofchk)
737 return -EOPNOTSUPP;
738
739 return efx_ef10_sriov_set_privilege_mask(efx, vf_i,
740 MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX,
741 spoofchk ? 0 : MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX);
742 }
743
efx_ef10_sriov_set_vf_link_state(struct efx_nic * efx,int vf_i,int link_state)744 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
745 int link_state)
746 {
747 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
748 struct efx_ef10_nic_data *nic_data = efx->nic_data;
749
750 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
751 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
752 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
753 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
754 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
755 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
756 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
757 LINK_STATE_MODE_IN_FUNCTION_PF,
758 nic_data->pf_index,
759 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
760 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
761 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
762 NULL, 0, NULL); /* don't care what old mode was */
763 }
764
efx_ef10_sriov_get_vf_config(struct efx_nic * efx,int vf_i,struct ifla_vf_info * ivf)765 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
766 struct ifla_vf_info *ivf)
767 {
768 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
769 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
770
771 struct efx_ef10_nic_data *nic_data = efx->nic_data;
772 struct ef10_vf *vf;
773 size_t outlen;
774 int rc;
775
776 if (vf_i >= efx->vf_count)
777 return -EINVAL;
778
779 if (!nic_data->vf)
780 return -EOPNOTSUPP;
781
782 vf = nic_data->vf + vf_i;
783
784 ivf->vf = vf_i;
785 ivf->min_tx_rate = 0;
786 ivf->max_tx_rate = 0;
787 ether_addr_copy(ivf->mac, vf->mac);
788 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
789 ivf->qos = 0;
790
791 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
792 LINK_STATE_MODE_IN_FUNCTION_PF,
793 nic_data->pf_index,
794 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
795 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
796 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
797 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
798 outbuf, sizeof(outbuf), &outlen);
799 if (rc)
800 return rc;
801 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
802 return -EIO;
803 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
804
805 return 0;
806 }
807