1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2015 Solarflare Communications Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, incorporated herein by reference.
9 #include <linux/etherdevice.h>
10 #include <linux/pci.h>
11 #include <linux/module.h>
12 #include "net_driver.h"
13 #include "ef10_sriov.h"
16 #include "mcdi_pcol.h"
18 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
21 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
22 struct efx_ef10_nic_data *nic_data = efx->nic_data;
24 MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
25 MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
26 EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
27 EVB_PORT_ASSIGN_IN_VF, vf_fn);
29 return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
33 static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
34 unsigned int vswitch_type)
36 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
39 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
40 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
41 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
42 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
43 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
45 /* Quietly try to allocate 2 VLAN tags */
46 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
49 /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
51 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
52 rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
53 sizeof(inbuf), NULL, 0, NULL);
55 efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
56 MC_CMD_VSWITCH_ALLOC_IN_LEN,
62 static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
64 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
66 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
68 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
72 static int efx_ef10_vport_alloc(struct efx_nic *efx,
73 unsigned int port_id_in,
74 unsigned int vport_type,
76 unsigned int *port_id_out)
78 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
79 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
83 EFX_WARN_ON_PARANOID(!port_id_out);
85 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
86 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
87 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
88 (vlan != EFX_EF10_NO_VLAN));
89 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
90 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
91 if (vlan != EFX_EF10_NO_VLAN)
92 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
93 VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
95 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
96 outbuf, sizeof(outbuf), &outlen);
99 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
102 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
106 static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
108 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
110 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
112 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
116 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
118 struct efx_ef10_nic_data *nic_data = efx->nic_data;
124 for (i = 0; i < efx->vf_count; i++) {
125 struct ef10_vf *vf = nic_data->vf + i;
127 /* If VF is assigned, do not free the vport */
129 vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
132 if (vf->vport_assigned) {
133 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
134 vf->vport_assigned = 0;
137 if (!is_zero_ether_addr(vf->mac)) {
138 efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
139 eth_zero_addr(vf->mac);
143 efx_ef10_vport_free(efx, vf->vport_id);
151 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
153 struct efx_ef10_nic_data *nic_data = efx->nic_data;
155 efx_ef10_sriov_free_vf_vports(efx);
160 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
163 struct efx_ef10_nic_data *nic_data = efx->nic_data;
164 struct ef10_vf *vf = nic_data->vf + vf_i;
167 if (WARN_ON_ONCE(!nic_data->vf))
170 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
171 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
172 vf->vlan, &vf->vport_id);
176 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
178 eth_zero_addr(vf->mac);
182 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
186 vf->vport_assigned = 1;
190 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
192 struct efx_ef10_nic_data *nic_data = efx->nic_data;
196 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
201 for (i = 0; i < efx->vf_count; i++) {
202 random_ether_addr(nic_data->vf[i].mac);
203 nic_data->vf[i].efx = NULL;
204 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
206 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
213 efx_ef10_sriov_free_vf_vports(efx);
219 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
224 for (i = 0; i < efx->vf_count; i++) {
225 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
232 efx_ef10_sriov_free_vf_vswitching(efx);
236 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
238 struct efx_ef10_nic_data *nic_data = efx->nic_data;
242 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
244 goto fail_vadaptor_alloc;
246 rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
247 &port_flags, NULL, NULL);
249 goto fail_vadaptor_query;
252 (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
253 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
255 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
260 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
265 /* On top of the default firmware vswitch setup, create a VEB vswitch and
266 * expansion vport for use by this function.
268 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
270 struct efx_ef10_nic_data *nic_data = efx->nic_data;
271 struct net_device *net_dev = efx->net_dev;
274 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
275 /* vswitch not needed as we have no VFs */
276 efx_ef10_vadaptor_alloc_set_features(efx);
280 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
281 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
285 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
286 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
287 EFX_EF10_NO_VLAN, &nic_data->vport_id);
291 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
294 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
296 rc = efx_ef10_vadaptor_alloc_set_features(efx);
302 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
303 eth_zero_addr(nic_data->vport_mac);
305 efx_ef10_vport_free(efx, nic_data->vport_id);
306 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
308 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
313 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
315 return efx_ef10_vadaptor_alloc_set_features(efx);
318 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
320 struct efx_ef10_nic_data *nic_data = efx->nic_data;
323 if (!nic_data->must_probe_vswitching)
326 rc = efx_ef10_vswitching_probe_pf(efx);
330 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
334 nic_data->must_probe_vswitching = false;
339 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
341 struct efx_ef10_nic_data *nic_data = efx->nic_data;
344 if (!nic_data->must_probe_vswitching)
347 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
351 nic_data->must_probe_vswitching = false;
355 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
357 struct efx_ef10_nic_data *nic_data = efx->nic_data;
359 efx_ef10_sriov_free_vf_vswitching(efx);
361 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
363 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
364 return; /* No vswitch was ever created */
366 if (!is_zero_ether_addr(nic_data->vport_mac)) {
367 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
368 efx->net_dev->dev_addr);
369 eth_zero_addr(nic_data->vport_mac);
371 efx_ef10_vport_free(efx, nic_data->vport_id);
372 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
374 /* Only free the vswitch if no VFs are assigned */
375 if (!pci_vfs_assigned(efx->pci_dev))
376 efx_ef10_vswitch_free(efx, nic_data->vport_id);
379 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
381 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
384 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
387 struct pci_dev *dev = efx->pci_dev;
389 efx->vf_count = num_vfs;
391 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
395 rc = pci_enable_sriov(dev, num_vfs);
401 efx_ef10_sriov_free_vf_vswitching(efx);
404 netif_err(efx, probe, efx->net_dev,
405 "Failed to enable SRIOV VFs\n");
409 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
411 struct pci_dev *dev = efx->pci_dev;
412 unsigned int vfs_assigned = 0;
414 vfs_assigned = pci_vfs_assigned(dev);
416 if (vfs_assigned && !force) {
417 netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
418 "please detach them before disabling SR-IOV\n");
423 pci_disable_sriov(dev);
425 efx_ef10_sriov_free_vf_vswitching(efx);
430 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
433 return efx_ef10_pci_sriov_disable(efx, false);
435 return efx_ef10_pci_sriov_enable(efx, num_vfs);
438 int efx_ef10_sriov_init(struct efx_nic *efx)
443 void efx_ef10_sriov_fini(struct efx_nic *efx)
445 struct efx_ef10_nic_data *nic_data = efx->nic_data;
450 /* Remove any un-assigned orphaned VFs */
451 if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
452 pci_disable_sriov(efx->pci_dev);
456 /* Remove any VFs in the host */
457 for (i = 0; i < efx->vf_count; ++i) {
458 struct efx_nic *vf_efx = nic_data->vf[i].efx;
461 vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
464 rc = efx_ef10_pci_sriov_disable(efx, true);
466 netif_dbg(efx, drv, efx->net_dev,
467 "Disabling SRIOV was not successful rc=%d\n", rc);
469 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
472 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
475 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
476 MCDI_DECLARE_BUF_ERR(outbuf);
480 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
481 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
483 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
484 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
489 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
491 struct efx_ef10_nic_data *nic_data = efx->nic_data;
498 if (vf_i >= efx->vf_count)
500 vf = nic_data->vf + vf_i;
503 efx_device_detach_sync(vf->efx);
504 efx_net_stop(vf->efx->net_dev);
506 down_write(&vf->efx->filter_sem);
507 vf->efx->type->filter_table_remove(vf->efx);
509 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
511 up_write(&vf->efx->filter_sem);
516 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
520 if (!is_zero_ether_addr(vf->mac)) {
521 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
526 if (!is_zero_ether_addr(mac)) {
527 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
529 eth_zero_addr(vf->mac);
533 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
536 ether_addr_copy(vf->mac, mac);
538 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
543 /* VF cannot use the vport_id that the PF created */
544 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
546 up_write(&vf->efx->filter_sem);
549 vf->efx->type->filter_table_probe(vf->efx);
550 up_write(&vf->efx->filter_sem);
551 efx_net_open(vf->efx->net_dev);
552 efx_device_attach_if_not_resetting(vf->efx);
558 eth_zero_addr(vf->mac);
562 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
565 struct efx_ef10_nic_data *nic_data = efx->nic_data;
567 u16 old_vlan, new_vlan;
570 if (vf_i >= efx->vf_count)
575 vf = nic_data->vf + vf_i;
577 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
578 if (new_vlan == vf->vlan)
582 efx_device_detach_sync(vf->efx);
583 efx_net_stop(vf->efx->net_dev);
585 mutex_lock(&vf->efx->mac_lock);
586 down_write(&vf->efx->filter_sem);
587 vf->efx->type->filter_table_remove(vf->efx);
589 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
591 goto restore_filters;
594 if (vf->vport_assigned) {
595 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
597 netif_warn(efx, drv, efx->net_dev,
598 "Failed to change vlan on VF %d.\n", vf_i);
599 netif_warn(efx, drv, efx->net_dev,
600 "This is likely because the VF is bound to a driver in a VM.\n");
601 netif_warn(efx, drv, efx->net_dev,
602 "Please unload the driver in the VM.\n");
603 goto restore_vadaptor;
605 vf->vport_assigned = 0;
608 if (!is_zero_ether_addr(vf->mac)) {
609 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
611 goto restore_evb_port;
615 rc = efx_ef10_vport_free(efx, vf->vport_id);
621 /* Do the actual vlan change */
625 /* Restore everything in reverse order */
626 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
627 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
628 vf->vlan, &vf->vport_id);
630 goto reset_nic_up_write;
633 if (!is_zero_ether_addr(vf->mac)) {
634 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
636 eth_zero_addr(vf->mac);
637 goto reset_nic_up_write;
642 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
644 goto reset_nic_up_write;
646 vf->vport_assigned = 1;
650 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
652 goto reset_nic_up_write;
657 rc2 = vf->efx->type->filter_table_probe(vf->efx);
659 goto reset_nic_up_write;
661 up_write(&vf->efx->filter_sem);
662 mutex_unlock(&vf->efx->mac_lock);
664 up_write(&vf->efx->filter_sem);
666 rc2 = efx_net_open(vf->efx->net_dev);
670 efx_device_attach_if_not_resetting(vf->efx);
676 up_write(&vf->efx->filter_sem);
677 mutex_unlock(&vf->efx->mac_lock);
681 netif_err(efx, drv, efx->net_dev,
682 "Failed to restore VF - scheduling reset.\n");
683 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
685 netif_err(efx, drv, efx->net_dev,
686 "Failed to restore the VF and cannot reset the VF "
687 "- VF is not functional.\n");
688 netif_err(efx, drv, efx->net_dev,
689 "Please reload the driver attached to the VF.\n");
692 return rc ? rc : rc2;
695 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
698 return spoofchk ? -EOPNOTSUPP : 0;
701 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
704 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
705 struct efx_ef10_nic_data *nic_data = efx->nic_data;
707 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
708 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
709 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
710 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
711 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
712 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
713 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
714 LINK_STATE_MODE_IN_FUNCTION_PF,
716 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
717 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
718 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
719 NULL, 0, NULL); /* don't care what old mode was */
722 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
723 struct ifla_vf_info *ivf)
725 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
726 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
728 struct efx_ef10_nic_data *nic_data = efx->nic_data;
733 if (vf_i >= efx->vf_count)
739 vf = nic_data->vf + vf_i;
742 ivf->min_tx_rate = 0;
743 ivf->max_tx_rate = 0;
744 ether_addr_copy(ivf->mac, vf->mac);
745 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
748 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
749 LINK_STATE_MODE_IN_FUNCTION_PF,
751 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
752 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
753 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
754 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
755 outbuf, sizeof(outbuf), &outlen);
758 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
760 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);