Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2015 Solarflare Communications Inc.
0005  */
0006 #include <linux/etherdevice.h>
0007 #include <linux/pci.h>
0008 #include <linux/module.h>
0009 #include "net_driver.h"
0010 #include "ef10_sriov.h"
0011 #include "efx.h"
0012 #include "nic.h"
0013 #include "mcdi_pcol.h"
0014 
0015 static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
0016                     unsigned int vf_fn)
0017 {
0018     MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
0019     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0020 
0021     MCDI_SET_DWORD(inbuf, EVB_PORT_ASSIGN_IN_PORT_ID, port_id);
0022     MCDI_POPULATE_DWORD_2(inbuf, EVB_PORT_ASSIGN_IN_FUNCTION,
0023                   EVB_PORT_ASSIGN_IN_PF, nic_data->pf_index,
0024                   EVB_PORT_ASSIGN_IN_VF, vf_fn);
0025 
0026     return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
0027                 NULL, 0, NULL);
0028 }
0029 
0030 static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
0031                   unsigned int vswitch_type)
0032 {
0033     MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
0034     int rc;
0035 
0036     MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
0037     MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_TYPE, vswitch_type);
0038     MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 2);
0039     MCDI_POPULATE_DWORD_1(inbuf, VSWITCH_ALLOC_IN_FLAGS,
0040                   VSWITCH_ALLOC_IN_FLAG_AUTO_PORT, 0);
0041 
0042     /* Quietly try to allocate 2 VLAN tags */
0043     rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VSWITCH_ALLOC, inbuf, sizeof(inbuf),
0044                 NULL, 0, NULL);
0045 
0046     /* If 2 VLAN tags is too many, revert to trying with 1 VLAN tags */
0047     if (rc == -EPROTO) {
0048         MCDI_SET_DWORD(inbuf, VSWITCH_ALLOC_IN_NUM_VLAN_TAGS, 1);
0049         rc = efx_mcdi_rpc(efx, MC_CMD_VSWITCH_ALLOC, inbuf,
0050                   sizeof(inbuf), NULL, 0, NULL);
0051     } else if (rc) {
0052         efx_mcdi_display_error(efx, MC_CMD_VSWITCH_ALLOC,
0053                        MC_CMD_VSWITCH_ALLOC_IN_LEN,
0054                        NULL, 0, rc);
0055     }
0056     return rc;
0057 }
0058 
0059 static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
0060 {
0061     MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
0062 
0063     MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
0064 
0065     return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
0066                 NULL, 0, NULL);
0067 }
0068 
0069 static int efx_ef10_vport_alloc(struct efx_nic *efx,
0070                 unsigned int port_id_in,
0071                 unsigned int vport_type,
0072                 u16 vlan,
0073                 unsigned int *port_id_out)
0074 {
0075     MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ALLOC_IN_LEN);
0076     MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_ALLOC_OUT_LEN);
0077     size_t outlen;
0078     int rc;
0079 
0080     EFX_WARN_ON_PARANOID(!port_id_out);
0081 
0082     MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_UPSTREAM_PORT_ID, port_id_in);
0083     MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_TYPE, vport_type);
0084     MCDI_SET_DWORD(inbuf, VPORT_ALLOC_IN_NUM_VLAN_TAGS,
0085                (vlan != EFX_EF10_NO_VLAN));
0086     MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_FLAGS,
0087                   VPORT_ALLOC_IN_FLAG_AUTO_PORT, 0);
0088     if (vlan != EFX_EF10_NO_VLAN)
0089         MCDI_POPULATE_DWORD_1(inbuf, VPORT_ALLOC_IN_VLAN_TAGS,
0090                       VPORT_ALLOC_IN_VLAN_TAG_0, vlan);
0091 
0092     rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_ALLOC, inbuf, sizeof(inbuf),
0093               outbuf, sizeof(outbuf), &outlen);
0094     if (rc)
0095         return rc;
0096     if (outlen < MC_CMD_VPORT_ALLOC_OUT_LEN)
0097         return -EIO;
0098 
0099     *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
0100     return 0;
0101 }
0102 
0103 static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
0104 {
0105     MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
0106 
0107     MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
0108 
0109     return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
0110                 NULL, 0, NULL);
0111 }
0112 
0113 static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
0114 {
0115     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0116     int i;
0117 
0118     if (!nic_data->vf)
0119         return;
0120 
0121     for (i = 0; i < efx->vf_count; i++) {
0122         struct ef10_vf *vf = nic_data->vf + i;
0123 
0124         /* If VF is assigned, do not free the vport  */
0125         if (vf->pci_dev && pci_is_dev_assigned(vf->pci_dev))
0126             continue;
0127 
0128         if (vf->vport_assigned) {
0129             efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
0130             vf->vport_assigned = 0;
0131         }
0132 
0133         if (!is_zero_ether_addr(vf->mac)) {
0134             efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
0135             eth_zero_addr(vf->mac);
0136         }
0137 
0138         if (vf->vport_id) {
0139             efx_ef10_vport_free(efx, vf->vport_id);
0140             vf->vport_id = 0;
0141         }
0142 
0143         vf->efx = NULL;
0144     }
0145 }
0146 
0147 static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
0148 {
0149     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0150 
0151     efx_ef10_sriov_free_vf_vports(efx);
0152     kfree(nic_data->vf);
0153     nic_data->vf = NULL;
0154 }
0155 
0156 static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
0157                       unsigned int vf_i)
0158 {
0159     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0160     struct ef10_vf *vf = nic_data->vf + vf_i;
0161     int rc;
0162 
0163     if (WARN_ON_ONCE(!nic_data->vf))
0164         return -EOPNOTSUPP;
0165 
0166     rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
0167                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
0168                   vf->vlan, &vf->vport_id);
0169     if (rc)
0170         return rc;
0171 
0172     rc = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
0173     if (rc) {
0174         eth_zero_addr(vf->mac);
0175         return rc;
0176     }
0177 
0178     rc =  efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
0179     if (rc)
0180         return rc;
0181 
0182     vf->vport_assigned = 1;
0183     return 0;
0184 }
0185 
0186 static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
0187 {
0188     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0189     unsigned int i;
0190     int rc;
0191 
0192     nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
0193                    GFP_KERNEL);
0194     if (!nic_data->vf)
0195         return -ENOMEM;
0196 
0197     for (i = 0; i < efx->vf_count; i++) {
0198         eth_random_addr(nic_data->vf[i].mac);
0199         nic_data->vf[i].efx = NULL;
0200         nic_data->vf[i].vlan = EFX_EF10_NO_VLAN;
0201 
0202         rc = efx_ef10_sriov_assign_vf_vport(efx, i);
0203         if (rc)
0204             goto fail;
0205     }
0206 
0207     return 0;
0208 fail:
0209     efx_ef10_sriov_free_vf_vswitching(efx);
0210     return rc;
0211 }
0212 
0213 static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
0214 {
0215     unsigned int i;
0216     int rc;
0217 
0218     for (i = 0; i < efx->vf_count; i++) {
0219         rc = efx_ef10_sriov_assign_vf_vport(efx, i);
0220         if (rc)
0221             goto fail;
0222     }
0223 
0224     return 0;
0225 fail:
0226     efx_ef10_sriov_free_vf_vswitching(efx);
0227     return rc;
0228 }
0229 
0230 static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
0231 {
0232     u32 port_flags;
0233     int rc;
0234 
0235     rc = efx_ef10_vadaptor_alloc(efx, efx->vport_id);
0236     if (rc)
0237         goto fail_vadaptor_alloc;
0238 
0239     rc = efx_ef10_vadaptor_query(efx, efx->vport_id,
0240                      &port_flags, NULL, NULL);
0241     if (rc)
0242         goto fail_vadaptor_query;
0243 
0244     if (port_flags &
0245         (1 << MC_CMD_VPORT_ALLOC_IN_FLAG_VLAN_RESTRICT_LBN))
0246         efx->fixed_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
0247     else
0248         efx->fixed_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
0249 
0250     return 0;
0251 
0252 fail_vadaptor_query:
0253     efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
0254 fail_vadaptor_alloc:
0255     return rc;
0256 }
0257 
0258 /* On top of the default firmware vswitch setup, create a VEB vswitch and
0259  * expansion vport for use by this function.
0260  */
0261 int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
0262 {
0263     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0264     struct net_device *net_dev = efx->net_dev;
0265     int rc;
0266 
0267     if (pci_sriov_get_totalvfs(efx->pci_dev) <= 0) {
0268         /* vswitch not needed as we have no VFs */
0269         efx_ef10_vadaptor_alloc_set_features(efx);
0270         return 0;
0271     }
0272 
0273     rc = efx_ef10_vswitch_alloc(efx, EVB_PORT_ID_ASSIGNED,
0274                     MC_CMD_VSWITCH_ALLOC_IN_VSWITCH_TYPE_VEB);
0275     if (rc)
0276         goto fail1;
0277 
0278     rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
0279                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
0280                   EFX_EF10_NO_VLAN, &efx->vport_id);
0281     if (rc)
0282         goto fail2;
0283 
0284     rc = efx_ef10_vport_add_mac(efx, efx->vport_id, net_dev->dev_addr);
0285     if (rc)
0286         goto fail3;
0287     ether_addr_copy(nic_data->vport_mac, net_dev->dev_addr);
0288 
0289     rc = efx_ef10_vadaptor_alloc_set_features(efx);
0290     if (rc)
0291         goto fail4;
0292 
0293     return 0;
0294 fail4:
0295     efx_ef10_vport_del_mac(efx, efx->vport_id, nic_data->vport_mac);
0296     eth_zero_addr(nic_data->vport_mac);
0297 fail3:
0298     efx_ef10_vport_free(efx, efx->vport_id);
0299     efx->vport_id = EVB_PORT_ID_ASSIGNED;
0300 fail2:
0301     efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
0302 fail1:
0303     return rc;
0304 }
0305 
0306 int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
0307 {
0308     return efx_ef10_vadaptor_alloc_set_features(efx);
0309 }
0310 
0311 int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
0312 {
0313     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0314     int rc;
0315 
0316     if (!nic_data->must_probe_vswitching)
0317         return 0;
0318 
0319     rc = efx_ef10_vswitching_probe_pf(efx);
0320     if (rc)
0321         goto fail;
0322 
0323     rc = efx_ef10_sriov_restore_vf_vswitching(efx);
0324     if (rc)
0325         goto fail;
0326 
0327     nic_data->must_probe_vswitching = false;
0328 fail:
0329     return rc;
0330 }
0331 
0332 int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
0333 {
0334     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0335     int rc;
0336 
0337     if (!nic_data->must_probe_vswitching)
0338         return 0;
0339 
0340     rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
0341     if (rc)
0342         return rc;
0343 
0344     nic_data->must_probe_vswitching = false;
0345     return 0;
0346 }
0347 
0348 void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
0349 {
0350     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0351 
0352     efx_ef10_sriov_free_vf_vswitching(efx);
0353 
0354     efx_ef10_vadaptor_free(efx, efx->vport_id);
0355 
0356     if (efx->vport_id == EVB_PORT_ID_ASSIGNED)
0357         return; /* No vswitch was ever created */
0358 
0359     if (!is_zero_ether_addr(nic_data->vport_mac)) {
0360         efx_ef10_vport_del_mac(efx, efx->vport_id,
0361                        efx->net_dev->dev_addr);
0362         eth_zero_addr(nic_data->vport_mac);
0363     }
0364     efx_ef10_vport_free(efx, efx->vport_id);
0365     efx->vport_id = EVB_PORT_ID_ASSIGNED;
0366 
0367     /* Only free the vswitch if no VFs are assigned */
0368     if (!pci_vfs_assigned(efx->pci_dev))
0369         efx_ef10_vswitch_free(efx, efx->vport_id);
0370 }
0371 
0372 void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
0373 {
0374     efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
0375 }
0376 
0377 static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
0378 {
0379     int rc = 0;
0380     struct pci_dev *dev = efx->pci_dev;
0381 
0382     efx->vf_count = num_vfs;
0383 
0384     rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
0385     if (rc)
0386         goto fail1;
0387 
0388     rc = pci_enable_sriov(dev, num_vfs);
0389     if (rc)
0390         goto fail2;
0391 
0392     return 0;
0393 fail2:
0394     efx_ef10_sriov_free_vf_vswitching(efx);
0395 fail1:
0396     efx->vf_count = 0;
0397     netif_err(efx, probe, efx->net_dev,
0398           "Failed to enable SRIOV VFs\n");
0399     return rc;
0400 }
0401 
0402 /* Disable SRIOV and remove VFs
0403  * If some VFs are attached to a guest (using Xen, only) nothing is
0404  * done if force=false, and vports are freed if force=true (for the non
0405  * attachedc ones, only) but SRIOV is not disabled and VFs are not
0406  * removed in either case.
0407  */
0408 static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
0409 {
0410     struct pci_dev *dev = efx->pci_dev;
0411     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0412     unsigned int vfs_assigned = pci_vfs_assigned(dev);
0413     int i, rc = 0;
0414 
0415     if (vfs_assigned && !force) {
0416         netif_info(efx, drv, efx->net_dev, "VFs are assigned to guests; "
0417                "please detach them before disabling SR-IOV\n");
0418         return -EBUSY;
0419     }
0420 
0421     if (!vfs_assigned) {
0422         for (i = 0; i < efx->vf_count; i++)
0423             nic_data->vf[i].pci_dev = NULL;
0424         pci_disable_sriov(dev);
0425     } else {
0426         rc = -EBUSY;
0427     }
0428 
0429     efx_ef10_sriov_free_vf_vswitching(efx);
0430     efx->vf_count = 0;
0431     return rc;
0432 }
0433 
0434 int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
0435 {
0436     if (num_vfs == 0)
0437         return efx_ef10_pci_sriov_disable(efx, false);
0438     else
0439         return efx_ef10_pci_sriov_enable(efx, num_vfs);
0440 }
0441 
0442 int efx_ef10_sriov_init(struct efx_nic *efx)
0443 {
0444     return 0;
0445 }
0446 
0447 void efx_ef10_sriov_fini(struct efx_nic *efx)
0448 {
0449     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0450     int rc;
0451 
0452     if (!nic_data->vf) {
0453         /* Remove any un-assigned orphaned VFs. This can happen if the PF driver
0454          * was unloaded while any VF was assigned to a guest (using Xen, only).
0455          */
0456         if (pci_num_vf(efx->pci_dev) && !pci_vfs_assigned(efx->pci_dev))
0457             pci_disable_sriov(efx->pci_dev);
0458         return;
0459     }
0460 
0461     /* Disable SRIOV and remove any VFs in the host */
0462     rc = efx_ef10_pci_sriov_disable(efx, true);
0463     if (rc)
0464         netif_dbg(efx, drv, efx->net_dev,
0465               "Disabling SRIOV was not successful rc=%d\n", rc);
0466     else
0467         netif_dbg(efx, drv, efx->net_dev, "SRIOV disabled\n");
0468 }
0469 
0470 static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
0471                      u8 *mac)
0472 {
0473     MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
0474     MCDI_DECLARE_BUF_ERR(outbuf);
0475     size_t outlen;
0476     int rc;
0477 
0478     MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
0479     ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
0480 
0481     rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
0482               sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
0483 
0484     return rc;
0485 }
0486 
0487 int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, const u8 *mac)
0488 {
0489     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0490     struct ef10_vf *vf;
0491     int rc;
0492 
0493     if (!nic_data->vf)
0494         return -EOPNOTSUPP;
0495 
0496     if (vf_i >= efx->vf_count)
0497         return -EINVAL;
0498     vf = nic_data->vf + vf_i;
0499 
0500     if (vf->efx) {
0501         efx_device_detach_sync(vf->efx);
0502         efx_net_stop(vf->efx->net_dev);
0503 
0504         vf->efx->type->filter_table_remove(vf->efx);
0505 
0506         rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
0507         if (rc)
0508             return rc;
0509     }
0510 
0511     rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
0512     if (rc)
0513         return rc;
0514 
0515     if (!is_zero_ether_addr(vf->mac)) {
0516         rc = efx_ef10_vport_del_vf_mac(efx, vf->vport_id, vf->mac);
0517         if (rc)
0518             return rc;
0519     }
0520 
0521     if (!is_zero_ether_addr(mac)) {
0522         rc = efx_ef10_vport_add_mac(efx, vf->vport_id, mac);
0523         if (rc)
0524             goto fail;
0525 
0526         if (vf->efx)
0527             eth_hw_addr_set(vf->efx->net_dev, mac);
0528     }
0529 
0530     ether_addr_copy(vf->mac, mac);
0531 
0532     rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
0533     if (rc)
0534         goto fail;
0535 
0536     if (vf->efx) {
0537         /* VF cannot use the vport_id that the PF created */
0538         rc = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
0539         if (rc)
0540             return rc;
0541         vf->efx->type->filter_table_probe(vf->efx);
0542         efx_net_open(vf->efx->net_dev);
0543         efx_device_attach_if_not_resetting(vf->efx);
0544     }
0545 
0546     return 0;
0547 
0548 fail:
0549     eth_zero_addr(vf->mac);
0550     return rc;
0551 }
0552 
0553 int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
0554                    u8 qos)
0555 {
0556     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0557     struct ef10_vf *vf;
0558     u16 new_vlan;
0559     int rc = 0, rc2 = 0;
0560 
0561     if (vf_i >= efx->vf_count)
0562         return -EINVAL;
0563     if (qos != 0)
0564         return -EINVAL;
0565 
0566     vf = nic_data->vf + vf_i;
0567 
0568     new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
0569     if (new_vlan == vf->vlan)
0570         return 0;
0571 
0572     if (vf->efx) {
0573         efx_device_detach_sync(vf->efx);
0574         efx_net_stop(vf->efx->net_dev);
0575 
0576         mutex_lock(&vf->efx->mac_lock);
0577         vf->efx->type->filter_table_remove(vf->efx);
0578 
0579         rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
0580         if (rc)
0581             goto restore_filters;
0582     }
0583 
0584     if (vf->vport_assigned) {
0585         rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
0586         if (rc) {
0587             netif_warn(efx, drv, efx->net_dev,
0588                    "Failed to change vlan on VF %d.\n", vf_i);
0589             netif_warn(efx, drv, efx->net_dev,
0590                    "This is likely because the VF is bound to a driver in a VM.\n");
0591             netif_warn(efx, drv, efx->net_dev,
0592                    "Please unload the driver in the VM.\n");
0593             goto restore_vadaptor;
0594         }
0595         vf->vport_assigned = 0;
0596     }
0597 
0598     if (!is_zero_ether_addr(vf->mac)) {
0599         rc = efx_ef10_vport_del_mac(efx, vf->vport_id, vf->mac);
0600         if (rc)
0601             goto restore_evb_port;
0602     }
0603 
0604     if (vf->vport_id) {
0605         rc = efx_ef10_vport_free(efx, vf->vport_id);
0606         if (rc)
0607             goto restore_mac;
0608         vf->vport_id = 0;
0609     }
0610 
0611     /* Do the actual vlan change */
0612     vf->vlan = new_vlan;
0613 
0614     /* Restore everything in reverse order */
0615     rc = efx_ef10_vport_alloc(efx, EVB_PORT_ID_ASSIGNED,
0616                   MC_CMD_VPORT_ALLOC_IN_VPORT_TYPE_NORMAL,
0617                   vf->vlan, &vf->vport_id);
0618     if (rc)
0619         goto reset_nic_up_write;
0620 
0621 restore_mac:
0622     if (!is_zero_ether_addr(vf->mac)) {
0623         rc2 = efx_ef10_vport_add_mac(efx, vf->vport_id, vf->mac);
0624         if (rc2) {
0625             eth_zero_addr(vf->mac);
0626             goto reset_nic_up_write;
0627         }
0628     }
0629 
0630 restore_evb_port:
0631     rc2 = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
0632     if (rc2)
0633         goto reset_nic_up_write;
0634     else
0635         vf->vport_assigned = 1;
0636 
0637 restore_vadaptor:
0638     if (vf->efx) {
0639         rc2 = efx_ef10_vadaptor_alloc(vf->efx, EVB_PORT_ID_ASSIGNED);
0640         if (rc2)
0641             goto reset_nic_up_write;
0642     }
0643 
0644 restore_filters:
0645     if (vf->efx) {
0646         rc2 = vf->efx->type->filter_table_probe(vf->efx);
0647         if (rc2)
0648             goto reset_nic_up_write;
0649 
0650         mutex_unlock(&vf->efx->mac_lock);
0651 
0652         rc2 = efx_net_open(vf->efx->net_dev);
0653         if (rc2)
0654             goto reset_nic;
0655 
0656         efx_device_attach_if_not_resetting(vf->efx);
0657     }
0658     return rc;
0659 
0660 reset_nic_up_write:
0661     if (vf->efx)
0662         mutex_unlock(&vf->efx->mac_lock);
0663 reset_nic:
0664     if (vf->efx) {
0665         netif_err(efx, drv, efx->net_dev,
0666               "Failed to restore VF - scheduling reset.\n");
0667         efx_schedule_reset(vf->efx, RESET_TYPE_DATAPATH);
0668     } else {
0669         netif_err(efx, drv, efx->net_dev,
0670               "Failed to restore the VF and cannot reset the VF "
0671               "- VF is not functional.\n");
0672         netif_err(efx, drv, efx->net_dev,
0673               "Please reload the driver attached to the VF.\n");
0674     }
0675 
0676     return rc ? rc : rc2;
0677 }
0678 
0679 static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
0680                          u32 mask, u32 value)
0681 {
0682     MCDI_DECLARE_BUF(pm_outbuf, MC_CMD_PRIVILEGE_MASK_OUT_LEN);
0683     MCDI_DECLARE_BUF(pm_inbuf, MC_CMD_PRIVILEGE_MASK_IN_LEN);
0684     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0685     u32 old_mask, new_mask;
0686     size_t outlen;
0687     int rc;
0688 
0689     EFX_WARN_ON_PARANOID((value & ~mask) != 0);
0690 
0691     /* Get privilege mask */
0692     MCDI_POPULATE_DWORD_2(pm_inbuf, PRIVILEGE_MASK_IN_FUNCTION,
0693                   PRIVILEGE_MASK_IN_FUNCTION_PF, nic_data->pf_index,
0694                   PRIVILEGE_MASK_IN_FUNCTION_VF, vf_i);
0695 
0696     rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
0697               pm_inbuf, sizeof(pm_inbuf),
0698               pm_outbuf, sizeof(pm_outbuf), &outlen);
0699 
0700     if (rc != 0)
0701         return rc;
0702     if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
0703         return -EIO;
0704 
0705     old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
0706 
0707     new_mask = old_mask & ~mask;
0708     new_mask |= value;
0709 
0710     if (new_mask == old_mask)
0711         return 0;
0712 
0713     new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
0714 
0715     /* Set privilege mask */
0716     MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
0717 
0718     rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
0719               pm_inbuf, sizeof(pm_inbuf),
0720               pm_outbuf, sizeof(pm_outbuf), &outlen);
0721 
0722     if (rc != 0)
0723         return rc;
0724     if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
0725         return -EIO;
0726 
0727     return 0;
0728 }
0729 
0730 int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
0731 {
0732     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0733 
0734     /* Can't enable spoofchk if firmware doesn't support it. */
0735     if (!(nic_data->datapath_caps &
0736           BIT(MC_CMD_GET_CAPABILITIES_OUT_TX_MAC_SECURITY_FILTERING_LBN)) &&
0737         spoofchk)
0738         return -EOPNOTSUPP;
0739 
0740     return efx_ef10_sriov_set_privilege_mask(efx, vf_i,
0741         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX,
0742         spoofchk ? 0 : MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX);
0743 }
0744 
0745 int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
0746                      int link_state)
0747 {
0748     MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
0749     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0750 
0751     BUILD_BUG_ON(IFLA_VF_LINK_STATE_AUTO !=
0752              MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_AUTO);
0753     BUILD_BUG_ON(IFLA_VF_LINK_STATE_ENABLE !=
0754              MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_UP);
0755     BUILD_BUG_ON(IFLA_VF_LINK_STATE_DISABLE !=
0756              MC_CMD_LINK_STATE_MODE_IN_LINK_STATE_DOWN);
0757     MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
0758                   LINK_STATE_MODE_IN_FUNCTION_PF,
0759                   nic_data->pf_index,
0760                   LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
0761     MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE, link_state);
0762     return efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
0763                 NULL, 0, NULL); /* don't care what old mode was */
0764 }
0765 
0766 int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
0767                  struct ifla_vf_info *ivf)
0768 {
0769     MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
0770     MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
0771 
0772     struct efx_ef10_nic_data *nic_data = efx->nic_data;
0773     struct ef10_vf *vf;
0774     size_t outlen;
0775     int rc;
0776 
0777     if (vf_i >= efx->vf_count)
0778         return -EINVAL;
0779 
0780     if (!nic_data->vf)
0781         return -EOPNOTSUPP;
0782 
0783     vf = nic_data->vf + vf_i;
0784 
0785     ivf->vf = vf_i;
0786     ivf->min_tx_rate = 0;
0787     ivf->max_tx_rate = 0;
0788     ether_addr_copy(ivf->mac, vf->mac);
0789     ivf->vlan = (vf->vlan == EFX_EF10_NO_VLAN) ? 0 : vf->vlan;
0790     ivf->qos = 0;
0791 
0792     MCDI_POPULATE_DWORD_2(inbuf, LINK_STATE_MODE_IN_FUNCTION,
0793                   LINK_STATE_MODE_IN_FUNCTION_PF,
0794                   nic_data->pf_index,
0795                   LINK_STATE_MODE_IN_FUNCTION_VF, vf_i);
0796     MCDI_SET_DWORD(inbuf, LINK_STATE_MODE_IN_NEW_MODE,
0797                MC_CMD_LINK_STATE_MODE_IN_DO_NOT_CHANGE);
0798     rc = efx_mcdi_rpc(efx, MC_CMD_LINK_STATE_MODE, inbuf, sizeof(inbuf),
0799               outbuf, sizeof(outbuf), &outlen);
0800     if (rc)
0801         return rc;
0802     if (outlen < MC_CMD_LINK_STATE_MODE_OUT_LEN)
0803         return -EIO;
0804     ivf->linkstate = MCDI_DWORD(outbuf, LINK_STATE_MODE_OUT_OLD_MODE);
0805 
0806     return 0;
0807 }