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.
10 #include <linux/module.h>
11 #include "net_driver.h"
12 #include "ef10_sriov.h"
15 #include "mcdi_pcol.h"
17 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
20 MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
21 struct efx_ef10_nic_data *nic_data = efx->nic_data;
23 MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
24 MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
25 EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
26 EVB_PORT_ASSIGN_IN_VF, vf_fn);
28 return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
32 static int efx_ef10_vport_add_mac(struct efx_nic *efx,
33 unsigned int port_id, u8 *mac)
35 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ADD_MAC_ADDRESS_IN_LEN);
37 MCDI_SET_DWORD(inbuf, VPORT_ADD_MAC_ADDRESS_IN_VPORT_ID, port_id);
38 ether_addr_copy(MCDI_PTR(inbuf, VPORT_ADD_MAC_ADDRESS_IN_MACADDR), mac);
40 return efx_mcdi_rpc(efx, MC_CMD_VPORT_ADD_MAC_ADDRESS, inbuf,
41 sizeof(inbuf), NULL, 0, NULL);
44 static int efx_ef10_vport_del_mac(struct efx_nic *efx,
45 unsigned int port_id, u8 *mac)
47 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
49 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
50 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
52 return efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
53 sizeof(inbuf), NULL, 0, NULL);
56 static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
57 unsigned int vswitch_type)
59 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
62 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
63 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
64 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
65 MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
66 VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
68 /* Quietly try to allocate 2 VLAN tags */
69 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
72 /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
74 MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
75 rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
76 sizeof(inbuf), NULL, 0, NULL);
78 efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
79 MC_CMD_VSWITCH_ALLOC_IN_LEN,
85 static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
87 MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
89 MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
91 return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
95 static int efx_ef10_vport_alloc(struct efx_nic *efx,
96 unsigned int port_id_in,
97 unsigned int vport_type,
99 unsigned int *port_id_out)
101 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
102 MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
106 EFX_WARN_ON_PARANOID(!port_id_out);
108 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
109 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
110 MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
111 (vlan != EFX_EF10_NO_VLAN));
112 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
113 VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
114 if (vlan != EFX_EF10_NO_VLAN)
115 MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
116 VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
118 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
119 outbuf, sizeof(outbuf), &outlen);
122 if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
125 *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
129 static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
131 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
133 MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
135 return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
139 static int efx_ef10_vadaptor_alloc(struct efx_nic *efx, unsigned int port_id)
141 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_ALLOC_IN_LEN);
143 MCDI_SET_DWORD(inbuf, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
144 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_ALLOC, inbuf, sizeof(inbuf),
148 static int efx_ef10_vadaptor_free(struct efx_nic *efx, unsigned int port_id)
150 MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_FREE_IN_LEN);
152 MCDI_SET_DWORD(inbuf, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
153 return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_FREE, inbuf, sizeof(inbuf),
157 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
159 struct efx_ef10_nic_data *nic_data = efx->nic_data;
165 for (i = 0; i < efx->vf_count; i++) {
166 struct ef10_vf *vf = nic_data->vf + i;
168 if (vf->vport_assigned) {
169 efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
170 vf->vport_assigned = 0;
173 if (!is_zero_ether_addr(vf->mac)) {
174 efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
175 eth_zero_addr(vf->mac);
179 efx_ef10_vport_free(efx, vf->vport_id);
187 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
189 struct efx_ef10_nic_data *nic_data = efx->nic_data;
191 efx_ef10_sriov_free_vf_vports(efx);
196 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
199 struct efx_ef10_nic_data *nic_data = efx->nic_data;
200 struct ef10_vf *vf = nic_data->vf + vf_i;
203 if (WARN_ON_ONCE(!nic_data->vf))
206 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
207 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
208 vf->vlan, &vf->vport_id);
212 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
214 eth_zero_addr(vf->mac);
218 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
222 vf->vport_assigned = 1;
226 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
228 struct efx_ef10_nic_data *nic_data = efx->nic_data;
232 nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
237 for (i = 0; i < efx->vf_count; i++) {
238 random_ether_addr(nic_data->vf[i].mac);
239 nic_data->vf[i].efx = NULL;
240 nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
242 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
249 efx_ef10_sriov_free_vf_vports(efx);
255 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
260 for (i = 0; i < efx->vf_count; i++) {
261 rc = efx_ef10_sriov_assign_vf_vport(efx, i);
268 efx_ef10_sriov_free_vf_vswitching(efx);
272 /* On top of the default firmware vswitch setup, create a VEB vswitch and
273 * expansion vport for use by this function.
275 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
277 struct efx_ef10_nic_data *nic_data = efx->nic_data;
278 struct net_device *net_dev = efx->net_dev;
281 if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
282 /* vswitch not needed as we have no VFs */
283 efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
287 rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
288 MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
292 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
293 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
294 EFX_EF10_NO_VLAN, &nic_data->vport_id);
298 rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id, net_dev->dev_addr);
301 ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
303 rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
309 efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
310 eth_zero_addr(nic_data->vport_mac);
312 efx_ef10_vport_free(efx, nic_data->vport_id);
313 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
315 efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
320 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
322 struct efx_ef10_nic_data *nic_data = efx->nic_data;
324 return efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
327 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
329 struct efx_ef10_nic_data *nic_data = efx->nic_data;
332 if (!nic_data->must_probe_vswitching)
335 rc = efx_ef10_vswitching_probe_pf(efx);
339 rc = efx_ef10_sriov_restore_vf_vswitching(efx);
343 nic_data->must_probe_vswitching = false;
348 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
350 struct efx_ef10_nic_data *nic_data = efx->nic_data;
353 if (!nic_data->must_probe_vswitching)
356 rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
360 nic_data->must_probe_vswitching = false;
364 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
366 struct efx_ef10_nic_data *nic_data = efx->nic_data;
368 efx_ef10_sriov_free_vf_vswitching(efx);
370 efx_ef10_vadaptor_free(efx, nic_data->vport_id);
372 if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
373 return; /* No vswitch was ever created */
375 if (!is_zero_ether_addr(nic_data->vport_mac)) {
376 efx_ef10_vport_del_mac(efx, nic_data->vport_id,
377 efx->net_dev->dev_addr);
378 eth_zero_addr(nic_data->vport_mac);
380 efx_ef10_vport_free(efx, nic_data->vport_id);
381 nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
383 efx_ef10_vswitch_free(efx, nic_data->vport_id);
386 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
388 efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
391 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
394 struct pci_dev *dev = efx->pci_dev;
396 efx->vf_count = num_vfs;
398 rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
402 rc = pci_enable_sriov(dev, num_vfs);
408 efx_ef10_sriov_free_vf_vswitching(efx);
411 netif_err(efx, probe, efx->net_dev,
412 "Failed to enable SRIOV VFs\n");
416 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx)
418 struct pci_dev *dev = efx->pci_dev;
420 pci_disable_sriov(dev);
421 efx_ef10_sriov_free_vf_vswitching(efx);
426 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
429 return efx_ef10_pci_sriov_disable(efx);
431 return efx_ef10_pci_sriov_enable(efx, num_vfs);
434 int efx_ef10_sriov_init(struct efx_nic *efx)
439 void efx_ef10_sriov_fini(struct efx_nic *efx)
441 struct efx_ef10_nic_data *nic_data = efx->nic_data;
447 rc = efx_ef10_pci_sriov_disable(efx);
449 netif_dbg(efx, drv, efx->net_dev,
450 "Disabling SRIOV was not successful rc=%d\n", rc);
452 netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
455 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
458 MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
459 MCDI_DECLARE_BUF_ERR(outbuf);
463 MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
464 ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
466 rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
467 sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
472 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
474 struct efx_ef10_nic_data *nic_data = efx->nic_data;
481 if (vf_i >= efx->vf_count)
483 vf = nic_data->vf + vf_i;
486 efx_device_detach_sync(vf->efx);
487 efx_net_stop(vf->efx->net_dev);
489 down_write(&vf->efx->filter_sem);
490 vf->efx->type->filter_table_remove(vf->efx);
492 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
494 up_write(&vf->efx->filter_sem);
499 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
503 if (!is_zero_ether_addr(vf->mac)) {
504 rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
509 if (!is_zero_ether_addr(mac)) {
510 rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
512 eth_zero_addr(vf->mac);
516 ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
519 ether_addr_copy(vf->mac, mac);
521 rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
526 /* VF cannot use the vport_id that the PF created */
527 rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
529 up_write(&vf->efx->filter_sem);
532 vf->efx->type->filter_table_probe(vf->efx);
533 up_write(&vf->efx->filter_sem);
534 efx_net_open(vf->efx->net_dev);
535 netif_device_attach(vf->efx->net_dev);
541 memset(vf->mac, 0, ETH_ALEN);
545 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
548 struct efx_ef10_nic_data *nic_data = efx->nic_data;
550 u16 old_vlan, new_vlan;
553 if (vf_i >= efx->vf_count)
558 vf = nic_data->vf + vf_i;
560 new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
561 if (new_vlan == vf->vlan)
565 efx_device_detach_sync(vf->efx);
566 efx_net_stop(vf->efx->net_dev);
568 down_write(&vf->efx->filter_sem);
569 vf->efx->type->filter_table_remove(vf->efx);
571 rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
573 goto restore_filters;
576 if (vf->vport_assigned) {
577 rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
579 netif_warn(efx, drv, efx->net_dev,
580 "Failed to change vlan on VF %d.\n", vf_i);
581 netif_warn(efx, drv, efx->net_dev,
582 "This is likely because the VF is bound to a driver in a VM.\n");
583 netif_warn(efx, drv, efx->net_dev,
584 "Please unload the driver in the VM.\n");
585 goto restore_vadaptor;
587 vf->vport_assigned = 0;
590 if (!is_zero_ether_addr(vf->mac)) {
591 rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
593 goto restore_evb_port;
597 rc = efx_ef10_vport_free(efx, vf->vport_id);
603 /* Do the actual vlan change */
607 /* Restore everything in reverse order */
608 rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
609 MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
610 vf->vlan, &vf->vport_id);
615 if (!is_zero_ether_addr(vf->mac)) {
616 rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
618 eth_zero_addr(vf->mac);
624 rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
628 vf->vport_assigned = 1;
632 rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
639 rc2 = vf->efx->type->filter_table_probe(vf->efx);
643 up_write(&vf->efx->filter_sem);
645 rc2 = efx_net_open(vf->efx->net_dev);
649 netif_device_attach(vf->efx->net_dev);
655 up_write(&vf->efx->filter_sem);
656 netif_err(efx, drv, efx->net_dev,
657 "Failed to restore VF - scheduling reset.\n");
658 efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
660 netif_err(efx, drv, efx->net_dev,
661 "Failed to restore the VF and cannot reset the VF "
662 "- VF is not functional.\n");
663 netif_err(efx, drv, efx->net_dev,
664 "Please reload the driver attached to the VF.\n");
667 return rc ? rc : rc2;
670 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
673 return spoofchk ? -EOPNOTSUPP : 0;
676 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
679 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
680 struct efx_ef10_nic_data *nic_data = efx->nic_data;
682 BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
683 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
684 BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
685 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
686 BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
687 MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
688 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
689 LINK_STATE_MODE_IN_FUNCTION_PF,
691 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
692 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
693 return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
694 NULL, 0, NULL); /* don't care what old mode was */
697 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
698 struct ifla_vf_info *ivf)
700 MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
701 MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
703 struct efx_ef10_nic_data *nic_data = efx->nic_data;
708 if (vf_i >= efx->vf_count)
714 vf = nic_data->vf + vf_i;
717 ivf->min_tx_rate = 0;
718 ivf->max_tx_rate = 0;
719 ether_addr_copy(ivf->mac, vf->mac);
720 ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
723 MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
724 LINK_STATE_MODE_IN_FUNCTION_PF,
726 LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
727 MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
728 MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
729 rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
730 outbuf, sizeof(outbuf), &outlen);
733 if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
735 ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);