gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / sfc / ef10_sriov.c
blob4580b30caae1bebb6761a773955b5adc51b90881
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"
15 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
16 unsigned int vf_fn)
18 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
19 struct efx_ef10_nic_data *nic_data = efx->nic_data;
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);
26 return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
27 NULL, 0, NULL);
30 static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
31 unsigned int vswitch_type)
33 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
34 int rc;
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);
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);
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);
56 return rc;
59 static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
61 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
63 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
65 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
66 NULL, 0, NULL);
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)
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;
80 EFX_WARN_ON_PARANOID(!port_id_out);
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);
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;
99 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
100 return 0;
103 static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
105 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
107 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
109 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
110 NULL, 0, NULL);
113 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
115 struct efx_ef10_nic_data *nic_data = efx->nic_data;
116 int i;
118 if (!nic_data->vf)
119 return;
121 for (i = 0; i < efx->vf_count; i++) {
122 struct ef10_vf *vf = nic_data->vf + i;
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;
129 if (vf->vport_assigned) {
130 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
131 vf->vport_assigned = 0;
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);
139 if (vf->vport_id) {
140 efx_ef10_vport_free(efx, vf->vport_id);
141 vf->vport_id = 0;
144 vf->efx = NULL;
148 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
150 struct efx_ef10_nic_data *nic_data = efx->nic_data;
152 efx_ef10_sriov_free_vf_vports(efx);
153 kfree(nic_data->vf);
154 nic_data->vf = NULL;
157 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
158 unsigned int vf_i)
160 struct efx_ef10_nic_data *nic_data = efx->nic_data;
161 struct ef10_vf *vf = nic_data->vf + vf_i;
162 int rc;
164 if (WARN_ON_ONCE(!nic_data->vf))
165 return -EOPNOTSUPP;
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;
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;
179 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
180 if (rc)
181 return rc;
183 vf->vport_assigned = 1;
184 return 0;
187 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
189 struct efx_ef10_nic_data *nic_data = efx->nic_data;
190 unsigned int i;
191 int rc;
193 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
194 GFP_KERNEL);
195 if (!nic_data->vf)
196 return -ENOMEM;
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;
203 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
204 if (rc)
205 goto fail;
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;
216 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
218 unsigned int i;
219 int rc;
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;
227 return 0;
228 fail:
229 efx_ef10_sriov_free_vf_vswitching(efx);
230 return rc;
233 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
235 struct efx_ef10_nic_data *nic_data = efx->nic_data;
236 u32 port_flags;
237 int rc;
239 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
240 if (rc)
241 goto fail_vadaptor_alloc;
243 rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
244 &port_flags, NULL, NULL);
245 if (rc)
246 goto fail_vadaptor_query;
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;
254 return 0;
256 fail_vadaptor_query:
257 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
258 fail_vadaptor_alloc:
259 return rc;
262 /* On top of the default firmware vswitch setup, create a VEB vswitch and
263 * expansion vport for use by this function.
265 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
267 struct efx_ef10_nic_data *nic_data = efx->nic_data;
268 struct net_device *net_dev = efx->net_dev;
269 int rc;
271 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
272 /* vswitch not needed as we have no VFs */
273 efx_ef10_vadaptor_alloc_set_features(efx);
274 return 0;
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;
282 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
283 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
284 EFX_EF10_NO_VLAN, &nic_data->vport_id);
285 if (rc)
286 goto fail2;
288 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
289 if (rc)
290 goto fail3;
291 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
293 rc = efx_ef10_vadaptor_alloc_set_features(efx);
294 if (rc)
295 goto fail4;
297 return 0;
298 fail4:
299 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
300 eth_zero_addr(nic_data->vport_mac);
301 fail3:
302 efx_ef10_vport_free(efx, nic_data->vport_id);
303 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
304 fail2:
305 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
306 fail1:
307 return rc;
310 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
312 return efx_ef10_vadaptor_alloc_set_features(efx);
315 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
317 struct efx_ef10_nic_data *nic_data = efx->nic_data;
318 int rc;
320 if (!nic_data->must_probe_vswitching)
321 return 0;
323 rc = efx_ef10_vswitching_probe_pf(efx);
324 if (rc)
325 goto fail;
327 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
328 if (rc)
329 goto fail;
331 nic_data->must_probe_vswitching = false;
332 fail:
333 return rc;
336 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
338 struct efx_ef10_nic_data *nic_data = efx->nic_data;
339 int rc;
341 if (!nic_data->must_probe_vswitching)
342 return 0;
344 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
345 if (rc)
346 return rc;
348 nic_data->must_probe_vswitching = false;
349 return 0;
352 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
354 struct efx_ef10_nic_data *nic_data = efx->nic_data;
356 efx_ef10_sriov_free_vf_vswitching(efx);
358 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
360 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
361 return; /* No vswitch was ever created */
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);
368 efx_ef10_vport_free(efx, nic_data->vport_id);
369 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
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);
376 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
378 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
381 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
383 int rc = 0;
384 struct pci_dev *dev = efx->pci_dev;
386 efx->vf_count = num_vfs;
388 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
389 if (rc)
390 goto fail1;
392 rc = pci_enable_sriov(dev, num_vfs);
393 if (rc)
394 goto fail2;
396 return 0;
397 fail2:
398 efx_ef10_sriov_free_vf_vswitching(efx);
399 fail1:
400 efx->vf_count = 0;
401 netif_err(efx, probe, efx->net_dev,
402 "Failed to enable SRIOV VFs\n");
403 return rc;
406 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
408 struct pci_dev *dev = efx->pci_dev;
409 unsigned int vfs_assigned = 0;
411 vfs_assigned = pci_vfs_assigned(dev);
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");
416 return -EBUSY;
419 if (!vfs_assigned)
420 pci_disable_sriov(dev);
422 efx_ef10_sriov_free_vf_vswitching(efx);
423 efx->vf_count = 0;
424 return 0;
427 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
429 if (num_vfs == 0)
430 return efx_ef10_pci_sriov_disable(efx, false);
431 else
432 return efx_ef10_pci_sriov_enable(efx, num_vfs);
435 int efx_ef10_sriov_init(struct efx_nic *efx)
437 return 0;
440 void efx_ef10_sriov_fini(struct efx_nic *efx)
442 struct efx_ef10_nic_data *nic_data = efx->nic_data;
443 unsigned int i;
444 int rc;
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);
450 return;
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;
457 if (vf_efx)
458 vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
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");
469 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
470 u8 *mac)
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;
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);
480 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
481 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
483 return rc;
486 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
488 struct efx_ef10_nic_data *nic_data = efx->nic_data;
489 struct ef10_vf *vf;
490 int rc;
492 if (!nic_data->vf)
493 return -EOPNOTSUPP;
495 if (vf_i >= efx->vf_count)
496 return -EINVAL;
497 vf = nic_data->vf + vf_i;
499 if (vf->efx) {
500 efx_device_detach_sync(vf->efx);
501 efx_net_stop(vf->efx->net_dev);
503 down_write(&vf->efx->filter_sem);
504 vf->efx->type->filter_table_remove(vf->efx);
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;
513 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
514 if (rc)
515 return rc;
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;
523 if (!is_zero_ether_addr(mac)) {
524 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
525 if (rc)
526 goto fail;
528 if (vf->efx)
529 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
532 ether_addr_copy(vf->mac, mac);
534 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
535 if (rc)
536 goto fail;
538 if (vf->efx) {
539 /* VF cannot use the vport_id that the PF created */
540 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
541 if (rc) {
542 up_write(&vf->efx->filter_sem);
543 return rc;
545 vf->efx->type->filter_table_probe(vf->efx);
546 up_write(&vf->efx->filter_sem);
547 efx_net_open(vf->efx->net_dev);
548 efx_device_attach_if_not_resetting(vf->efx);
551 return 0;
553 fail:
554 eth_zero_addr(vf->mac);
555 return rc;
558 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
559 u8 qos)
561 struct efx_ef10_nic_data *nic_data = efx->nic_data;
562 struct ef10_vf *vf;
563 u16 new_vlan;
564 int rc = 0, rc2 = 0;
566 if (vf_i >= efx->vf_count)
567 return -EINVAL;
568 if (qos != 0)
569 return -EINVAL;
571 vf = nic_data->vf + vf_i;
573 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
574 if (new_vlan == vf->vlan)
575 return 0;
577 if (vf->efx) {
578 efx_device_detach_sync(vf->efx);
579 efx_net_stop(vf->efx->net_dev);
581 mutex_lock(&vf->efx->mac_lock);
582 down_write(&vf->efx->filter_sem);
583 vf->efx->type->filter_table_remove(vf->efx);
585 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
586 if (rc)
587 goto restore_filters;
590 if (vf->vport_assigned) {
591 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
592 if (rc) {
593 netif_warn(efx, drv, efx->net_dev,
594 "Failed to change vlan on VF %d.\n", vf_i);
595 netif_warn(efx, drv, efx->net_dev,
596 "This is likely because the VF is bound to a driver in a VM.\n");
597 netif_warn(efx, drv, efx->net_dev,
598 "Please unload the driver in the VM.\n");
599 goto restore_vadaptor;
601 vf->vport_assigned = 0;
604 if (!is_zero_ether_addr(vf->mac)) {
605 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
606 if (rc)
607 goto restore_evb_port;
610 if (vf->vport_id) {
611 rc = efx_ef10_vport_free(efx, vf->vport_id);
612 if (rc)
613 goto restore_mac;
614 vf->vport_id = 0;
617 /* Do the actual vlan change */
618 vf->vlan = new_vlan;
620 /* Restore everything in reverse order */
621 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
622 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
623 vf->vlan, &vf->vport_id);
624 if (rc)
625 goto reset_nic_up_write;
627 restore_mac:
628 if (!is_zero_ether_addr(vf->mac)) {
629 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
630 if (rc2) {
631 eth_zero_addr(vf->mac);
632 goto reset_nic_up_write;
636 restore_evb_port:
637 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
638 if (rc2)
639 goto reset_nic_up_write;
640 else
641 vf->vport_assigned = 1;
643 restore_vadaptor:
644 if (vf->efx) {
645 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
646 if (rc2)
647 goto reset_nic_up_write;
650 restore_filters:
651 if (vf->efx) {
652 rc2 = vf->efx->type->filter_table_probe(vf->efx);
653 if (rc2)
654 goto reset_nic_up_write;
656 up_write(&vf->efx->filter_sem);
657 mutex_unlock(&vf->efx->mac_lock);
659 rc2 = efx_net_open(vf->efx->net_dev);
660 if (rc2)
661 goto reset_nic;
663 efx_device_attach_if_not_resetting(vf->efx);
665 return rc;
667 reset_nic_up_write:
668 if (vf->efx) {
669 up_write(&vf->efx->filter_sem);
670 mutex_unlock(&vf->efx->mac_lock);
672 reset_nic:
673 if (vf->efx) {
674 netif_err(efx, drv, efx->net_dev,
675 "Failed to restore VF - scheduling reset.\n");
676 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
677 } else {
678 netif_err(efx, drv, efx->net_dev,
679 "Failed to restore the VF and cannot reset the VF "
680 "- VF is not functional.\n");
681 netif_err(efx, drv, efx->net_dev,
682 "Please reload the driver attached to the VF.\n");
685 return rc ? rc : rc2;
688 static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
689 u32 mask, u32 value)
691 MCDI_DECLARE_BUF(pm_outbuf, MC_CMD_PRIVILEGE_MASK_OUT_LEN);
692 MCDI_DECLARE_BUF(pm_inbuf, MC_CMD_PRIVILEGE_MASK_IN_LEN);
693 struct efx_ef10_nic_data *nic_data = efx->nic_data;
694 u32 old_mask, new_mask;
695 size_t outlen;
696 int rc;
698 EFX_WARN_ON_PARANOID((value & ~mask) != 0);
700 /* Get privilege mask */
701 MCDI_POPULATE_DWORD_2(pm_inbuf, PRIVILEGE_MASK_IN_FUNCTION,
702 PRIVILEGE_MASK_IN_FUNCTION_PF, nic_data->pf_index,
703 PRIVILEGE_MASK_IN_FUNCTION_VF, vf_i);
705 rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
706 pm_inbuf, sizeof(pm_inbuf),
707 pm_outbuf, sizeof(pm_outbuf), &outlen);
709 if (rc != 0)
710 return rc;
711 if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
712 return -EIO;
714 old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
716 new_mask = old_mask & ~mask;
717 new_mask |= value;
719 if (new_mask == old_mask)
720 return 0;
722 new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
724 /* Set privilege mask */
725 MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
727 rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
728 pm_inbuf, sizeof(pm_inbuf),
729 pm_outbuf, sizeof(pm_outbuf), &outlen);
731 if (rc != 0)
732 return rc;
733 if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
734 return -EIO;
736 return 0;
739 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
741 struct efx_ef10_nic_data *nic_data = efx->nic_data;
743 /* Can't enable spoofchk if firmware doesn't support it. */
744 if (!(nic_data->datapath_caps &
745 BIT(MC_CMD_GET_CAPABILITIES_OUT_TX_MAC_SECURITY_FILTERING_LBN)) &&
746 spoofchk)
747 return -EOPNOTSUPP;
749 return efx_ef10_sriov_set_privilege_mask(efx, vf_i,
750 MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX,
751 spoofchk ? 0 : MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX);
754 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
755 int link_state)
757 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
758 struct efx_ef10_nic_data *nic_data = efx->nic_data;
760 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
761 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
762 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
763 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
764 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
765 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
766 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
767 LINK_STATE_MODE_IN_FUNCTION_PF,
768 nic_data->pf_index,
769 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
770 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
771 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
772 NULL, 0, NULL); /* don't care what old mode was */
775 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
776 struct ifla_vf_info *ivf)
778 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
779 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
781 struct efx_ef10_nic_data *nic_data = efx->nic_data;
782 struct ef10_vf *vf;
783 size_t outlen;
784 int rc;
786 if (vf_i >= efx->vf_count)
787 return -EINVAL;
789 if (!nic_data->vf)
790 return -EOPNOTSUPP;
792 vf = nic_data->vf + vf_i;
794 ivf->vf = vf_i;
795 ivf->min_tx_rate = 0;
796 ivf->max_tx_rate = 0;
797 ether_addr_copy(ivf->mac, vf->mac);
798 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
799 ivf->qos = 0;
801 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
802 LINK_STATE_MODE_IN_FUNCTION_PF,
803 nic_data->pf_index,
804 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
805 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
806 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
807 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
808 outbuf, sizeof(outbuf), &outlen);
809 if (rc)
810 return rc;
811 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
812 return -EIO;
813 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
815 return 0;