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