1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2015 Solarflare Communications Inc.
6 #include <linux/etherdevice.h>
8 #include <linux/module.h>
9 #include "net_driver.h"
10 #include "ef10_sriov.h"
13 #include "mcdi_pcol.h"
15 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
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),
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);
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),
46 /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
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);
52 efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
53 MC_CMD_VSWITCH_ALLOC_IN_LEN,
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),
69 static int efx_ef10_vport_alloc(struct efx_nic *efx,
70 unsigned int port_id_in,
71 unsigned int vport_type,
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);
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);
96 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
99 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
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),
113 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
115 struct efx_ef10_nic_data *nic_data = efx->nic_data;
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 */
126 vf->pci_dev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
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);
140 efx_ef10_vport_free(efx, vf->vport_id);
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);
157 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
160 struct efx_ef10_nic_data *nic_data = efx->nic_data;
161 struct ef10_vf *vf = nic_data->vf + vf_i;
164 if (WARN_ON_ONCE(!nic_data->vf))
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);
173 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
175 eth_zero_addr(vf->mac);
179 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
183 vf->vport_assigned = 1;
187 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
189 struct efx_ef10_nic_data *nic_data = efx->nic_data;
193 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
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);
210 efx_ef10_sriov_free_vf_vports(efx);
216 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
221 for (i = 0; i < efx->vf_count; i++) {
222 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
229 efx_ef10_sriov_free_vf_vswitching(efx);
233 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
235 struct efx_ef10_nic_data *nic_data = efx->nic_data;
239 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
241 goto fail_vadaptor_alloc;
243 rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
244 &port_flags, NULL, NULL);
246 goto fail_vadaptor_query;
249 (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
250 efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
252 efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
257 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
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;
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);
277 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
278 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
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);
288 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
291 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
293 rc = efx_ef10_vadaptor_alloc_set_features(efx);
299 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
300 eth_zero_addr(nic_data->vport_mac);
302 efx_ef10_vport_free(efx, nic_data->vport_id);
303 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
305 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
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;
320 if (!nic_data->must_probe_vswitching)
323 rc = efx_ef10_vswitching_probe_pf(efx);
327 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
331 nic_data->must_probe_vswitching = false;
336 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
338 struct efx_ef10_nic_data *nic_data = efx->nic_data;
341 if (!nic_data->must_probe_vswitching)
344 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
348 nic_data->must_probe_vswitching = false;
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)
384 struct pci_dev *dev = efx->pci_dev;
386 efx->vf_count = num_vfs;
388 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
392 rc = pci_enable_sriov(dev, num_vfs);
398 efx_ef10_sriov_free_vf_vswitching(efx);
401 netif_err(efx, probe, efx->net_dev,
402 "Failed to enable SRIOV VFs\n");
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");
420 pci_disable_sriov(dev);
422 efx_ef10_sriov_free_vf_vswitching(efx);
427 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
430 return efx_ef10_pci_sriov_disable(efx, false);
432 return efx_ef10_pci_sriov_enable(efx, num_vfs);
435 int efx_ef10_sriov_init(struct efx_nic *efx)
440 void efx_ef10_sriov_fini(struct efx_nic *efx)
442 struct efx_ef10_nic_data *nic_data = efx->nic_data;
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);
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;
458 vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
461 rc = efx_ef10_pci_sriov_disable(efx, true);
463 netif_dbg(efx, drv, efx->net_dev,
464 "Disabling SRIOV was not successful rc=%d\n", rc);
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,
472 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
473 MCDI_DECLARE_BUF_ERR(outbuf);
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);
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;
495 if (vf_i >= efx->vf_count)
497 vf = nic_data->vf + vf_i;
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);
508 up_write(&vf->efx->filter_sem);
513 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
517 if (!is_zero_ether_addr(vf->mac)) {
518 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
523 if (!is_zero_ether_addr(mac)) {
524 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
526 eth_zero_addr(vf->mac);
530 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
533 ether_addr_copy(vf->mac, mac);
535 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
540 /* VF cannot use the vport_id that the PF created */
541 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
543 up_write(&vf->efx->filter_sem);
546 vf->efx->type->filter_table_probe(vf->efx);
547 up_write(&vf->efx->filter_sem);
548 efx_net_open(vf->efx->net_dev);
549 efx_device_attach_if_not_resetting(vf->efx);
555 eth_zero_addr(vf->mac);
559 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
562 struct efx_ef10_nic_data *nic_data = efx->nic_data;
567 if (vf_i >= efx->vf_count)
572 vf = nic_data->vf + vf_i;
574 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
575 if (new_vlan == vf->vlan)
579 efx_device_detach_sync(vf->efx);
580 efx_net_stop(vf->efx->net_dev);
582 mutex_lock(&vf->efx->mac_lock);
583 down_write(&vf->efx->filter_sem);
584 vf->efx->type->filter_table_remove(vf->efx);
586 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
588 goto restore_filters;
591 if (vf->vport_assigned) {
592 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
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;
602 vf->vport_assigned = 0;
605 if (!is_zero_ether_addr(vf->mac)) {
606 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
608 goto restore_evb_port;
612 rc = efx_ef10_vport_free(efx, vf->vport_id);
618 /* Do the actual vlan change */
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);
626 goto reset_nic_up_write;
629 if (!is_zero_ether_addr(vf->mac)) {
630 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
632 eth_zero_addr(vf->mac);
633 goto reset_nic_up_write;
638 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
640 goto reset_nic_up_write;
642 vf->vport_assigned = 1;
646 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
648 goto reset_nic_up_write;
653 rc2 = vf->efx->type->filter_table_probe(vf->efx);
655 goto reset_nic_up_write;
657 up_write(&vf->efx->filter_sem);
658 mutex_unlock(&vf->efx->mac_lock);
660 rc2 = efx_net_open(vf->efx->net_dev);
664 efx_device_attach_if_not_resetting(vf->efx);
670 up_write(&vf->efx->filter_sem);
671 mutex_unlock(&vf->efx->mac_lock);
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);
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");
686 return rc ? rc : rc2;
689 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
692 return spoofchk ? -EOPNOTSUPP : 0;
695 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
698 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
699 struct efx_ef10_nic_data *nic_data = efx->nic_data;
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,
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 */
716 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
717 struct ifla_vf_info *ivf)
719 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
720 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
722 struct efx_ef10_nic_data *nic_data = efx->nic_data;
727 if (vf_i >= efx->vf_count)
733 vf = nic_data->vf + vf_i;
736 ivf->min_tx_rate = 0;
737 ivf->max_tx_rate = 0;
738 ether_addr_copy(ivf->mac, vf->mac);
739 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
742 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
743 LINK_STATE_MODE_IN_FUNCTION_PF,
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);
752 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
754 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);