linux/drivers/net/ethernet/sfc/ef10_sriov.c
<<
>>
Prefs
   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"
  14
  15static int efx_ef10_evb_port_assign(struct efx_nic *efx, unsigned int port_id,
  16                                    unsigned int vf_fn)
  17{
  18        MCDI_DECLARE_BUF(inbuf, MC_CMD_EVB_PORT_ASSIGN_IN_LEN);
  19        struct efx_ef10_nic_data *nic_data = efx->nic_data;
  20
  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);
  25
  26        return efx_mcdi_rpc(efx, MC_CMD_EVB_PORT_ASSIGN, inbuf, sizeof(inbuf),
  27                            NULL, 0, NULL);
  28}
  29
  30static int efx_ef10_vswitch_alloc(struct efx_nic *efx, unsigned int port_id,
  31                                  unsigned int vswitch_type)
  32{
  33        MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_ALLOC_IN_LEN);
  34        int rc;
  35
  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);
  41
  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);
  45
  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);
  55        }
  56        return rc;
  57}
  58
  59static int efx_ef10_vswitch_free(struct efx_nic *efx, unsigned int port_id)
  60{
  61        MCDI_DECLARE_BUF(inbuf, MC_CMD_VSWITCH_FREE_IN_LEN);
  62
  63        MCDI_SET_DWORD(inbuf, VSWITCH_FREE_IN_UPSTREAM_PORT_ID, port_id);
  64
  65        return efx_mcdi_rpc(efx, MC_CMD_VSWITCH_FREE, inbuf, sizeof(inbuf),
  66                            NULL, 0, NULL);
  67}
  68
  69static 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)
  74{
  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;
  79
  80        EFX_WARN_ON_PARANOID(!port_id_out);
  81
  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);
  91
  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;
  98
  99        *port_id_out = MCDI_DWORD(outbuf, VPORT_ALLOC_OUT_VPORT_ID);
 100        return 0;
 101}
 102
 103static int efx_ef10_vport_free(struct efx_nic *efx, unsigned int port_id)
 104{
 105        MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_FREE_IN_LEN);
 106
 107        MCDI_SET_DWORD(inbuf, VPORT_FREE_IN_VPORT_ID, port_id);
 108
 109        return efx_mcdi_rpc(efx, MC_CMD_VPORT_FREE, inbuf, sizeof(inbuf),
 110                            NULL, 0, NULL);
 111}
 112
 113static void efx_ef10_sriov_free_vf_vports(struct efx_nic *efx)
 114{
 115        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 116        int i;
 117
 118        if (!nic_data->vf)
 119                return;
 120
 121        for (i = 0; i < efx->vf_count; i++) {
 122                struct ef10_vf *vf = nic_data->vf + i;
 123
 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;
 128
 129                if (vf->vport_assigned) {
 130                        efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, i);
 131                        vf->vport_assigned = 0;
 132                }
 133
 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);
 137                }
 138
 139                if (vf->vport_id) {
 140                        efx_ef10_vport_free(efx, vf->vport_id);
 141                        vf->vport_id = 0;
 142                }
 143
 144                vf->efx = NULL;
 145        }
 146}
 147
 148static void efx_ef10_sriov_free_vf_vswitching(struct efx_nic *efx)
 149{
 150        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 151
 152        efx_ef10_sriov_free_vf_vports(efx);
 153        kfree(nic_data->vf);
 154        nic_data->vf = NULL;
 155}
 156
 157static int efx_ef10_sriov_assign_vf_vport(struct efx_nic *efx,
 158                                          unsigned int vf_i)
 159{
 160        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 161        struct ef10_vf *vf = nic_data->vf + vf_i;
 162        int rc;
 163
 164        if (WARN_ON_ONCE(!nic_data->vf))
 165                return -EOPNOTSUPP;
 166
 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;
 172
 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;
 177        }
 178
 179        rc =  efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
 180        if (rc)
 181                return rc;
 182
 183        vf->vport_assigned = 1;
 184        return 0;
 185}
 186
 187static int efx_ef10_sriov_alloc_vf_vswitching(struct efx_nic *efx)
 188{
 189        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 190        unsigned int i;
 191        int rc;
 192
 193        nic_data->vf = kcalloc(efx->vf_count, sizeof(struct ef10_vf),
 194                               GFP_KERNEL);
 195        if (!nic_data->vf)
 196                return -ENOMEM;
 197
 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;
 202
 203                rc = efx_ef10_sriov_assign_vf_vport(efx, i);
 204                if (rc)
 205                        goto fail;
 206        }
 207
 208        return 0;
 209fail:
 210        efx_ef10_sriov_free_vf_vports(efx);
 211        kfree(nic_data->vf);
 212        nic_data->vf = NULL;
 213        return rc;
 214}
 215
 216static int efx_ef10_sriov_restore_vf_vswitching(struct efx_nic *efx)
 217{
 218        unsigned int i;
 219        int rc;
 220
 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;
 225        }
 226
 227        return 0;
 228fail:
 229        efx_ef10_sriov_free_vf_vswitching(efx);
 230        return rc;
 231}
 232
 233static int efx_ef10_vadaptor_alloc_set_features(struct efx_nic *efx)
 234{
 235        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 236        u32 port_flags;
 237        int rc;
 238
 239        rc = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
 240        if (rc)
 241                goto fail_vadaptor_alloc;
 242
 243        rc = efx_ef10_vadaptor_query(efx, nic_data->vport_id,
 244                                     &port_flags, NULL, NULL);
 245        if (rc)
 246                goto fail_vadaptor_query;
 247
 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;
 253
 254        return 0;
 255
 256fail_vadaptor_query:
 257        efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
 258fail_vadaptor_alloc:
 259        return rc;
 260}
 261
 262/* On top of the default firmware vswitch setup, create a VEB vswitch and
 263 * expansion vport for use by this function.
 264 */
 265int efx_ef10_vswitching_probe_pf(struct efx_nic *efx)
 266{
 267        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 268        struct net_device *net_dev = efx->net_dev;
 269        int rc;
 270
 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;
 275        }
 276
 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;
 281
 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;
 287
 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);
 292
 293        rc = efx_ef10_vadaptor_alloc_set_features(efx);
 294        if (rc)
 295                goto fail4;
 296
 297        return 0;
 298fail4:
 299        efx_ef10_vport_del_mac(efx, nic_data->vport_id, nic_data->vport_mac);
 300        eth_zero_addr(nic_data->vport_mac);
 301fail3:
 302        efx_ef10_vport_free(efx, nic_data->vport_id);
 303        nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
 304fail2:
 305        efx_ef10_vswitch_free(efx, EVB_PORT_ID_ASSIGNED);
 306fail1:
 307        return rc;
 308}
 309
 310int efx_ef10_vswitching_probe_vf(struct efx_nic *efx)
 311{
 312        return efx_ef10_vadaptor_alloc_set_features(efx);
 313}
 314
 315int efx_ef10_vswitching_restore_pf(struct efx_nic *efx)
 316{
 317        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 318        int rc;
 319
 320        if (!nic_data->must_probe_vswitching)
 321                return 0;
 322
 323        rc = efx_ef10_vswitching_probe_pf(efx);
 324        if (rc)
 325                goto fail;
 326
 327        rc = efx_ef10_sriov_restore_vf_vswitching(efx);
 328        if (rc)
 329                goto fail;
 330
 331        nic_data->must_probe_vswitching = false;
 332fail:
 333        return rc;
 334}
 335
 336int efx_ef10_vswitching_restore_vf(struct efx_nic *efx)
 337{
 338        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 339        int rc;
 340
 341        if (!nic_data->must_probe_vswitching)
 342                return 0;
 343
 344        rc = efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
 345        if (rc)
 346                return rc;
 347
 348        nic_data->must_probe_vswitching = false;
 349        return 0;
 350}
 351
 352void efx_ef10_vswitching_remove_pf(struct efx_nic *efx)
 353{
 354        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 355
 356        efx_ef10_sriov_free_vf_vswitching(efx);
 357
 358        efx_ef10_vadaptor_free(efx, nic_data->vport_id);
 359
 360        if (nic_data->vport_id == EVB_PORT_ID_ASSIGNED)
 361                return; /* No vswitch was ever created */
 362
 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);
 367        }
 368        efx_ef10_vport_free(efx, nic_data->vport_id);
 369        nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
 370
 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);
 374}
 375
 376void efx_ef10_vswitching_remove_vf(struct efx_nic *efx)
 377{
 378        efx_ef10_vadaptor_free(efx, EVB_PORT_ID_ASSIGNED);
 379}
 380
 381static int efx_ef10_pci_sriov_enable(struct efx_nic *efx, int num_vfs)
 382{
 383        int rc = 0;
 384        struct pci_dev *dev = efx->pci_dev;
 385
 386        efx->vf_count = num_vfs;
 387
 388        rc = efx_ef10_sriov_alloc_vf_vswitching(efx);
 389        if (rc)
 390                goto fail1;
 391
 392        rc = pci_enable_sriov(dev, num_vfs);
 393        if (rc)
 394                goto fail2;
 395
 396        return 0;
 397fail2:
 398        efx_ef10_sriov_free_vf_vswitching(efx);
 399fail1:
 400        efx->vf_count = 0;
 401        netif_err(efx, probe, efx->net_dev,
 402                  "Failed to enable SRIOV VFs\n");
 403        return rc;
 404}
 405
 406static int efx_ef10_pci_sriov_disable(struct efx_nic *efx, bool force)
 407{
 408        struct pci_dev *dev = efx->pci_dev;
 409        unsigned int vfs_assigned = 0;
 410
 411        vfs_assigned = pci_vfs_assigned(dev);
 412
 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;
 417        }
 418
 419        if (!vfs_assigned)
 420                pci_disable_sriov(dev);
 421
 422        efx_ef10_sriov_free_vf_vswitching(efx);
 423        efx->vf_count = 0;
 424        return 0;
 425}
 426
 427int efx_ef10_sriov_configure(struct efx_nic *efx, int num_vfs)
 428{
 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);
 433}
 434
 435int efx_ef10_sriov_init(struct efx_nic *efx)
 436{
 437        return 0;
 438}
 439
 440void efx_ef10_sriov_fini(struct efx_nic *efx)
 441{
 442        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 443        unsigned int i;
 444        int rc;
 445
 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;
 451        }
 452
 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;
 456
 457                if (vf_efx)
 458                        vf_efx->pci_dev->driver->remove(vf_efx->pci_dev);
 459        }
 460
 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");
 467}
 468
 469static int efx_ef10_vport_del_vf_mac(struct efx_nic *efx, unsigned int port_id,
 470                                     u8 *mac)
 471{
 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;
 476
 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);
 479
 480        rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
 481                          sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
 482
 483        return rc;
 484}
 485
 486int efx_ef10_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
 487{
 488        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 489        struct ef10_vf *vf;
 490        int rc;
 491
 492        if (!nic_data->vf)
 493                return -EOPNOTSUPP;
 494
 495        if (vf_i >= efx->vf_count)
 496                return -EINVAL;
 497        vf = nic_data->vf + vf_i;
 498
 499        if (vf->efx) {
 500                efx_device_detach_sync(vf->efx);
 501                efx_net_stop(vf->efx->net_dev);
 502
 503                down_write(&vf->efx->filter_sem);
 504                vf->efx->type->filter_table_remove(vf->efx);
 505
 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;
 510                }
 511        }
 512
 513        rc = efx_ef10_evb_port_assign(efx, EVB_PORT_ID_NULL, vf_i);
 514        if (rc)
 515                return rc;
 516
 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;
 521        }
 522
 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;
 527
 528                if (vf->efx)
 529                        ether_addr_copy(vf->efx->net_dev->dev_addr, mac);
 530        }
 531
 532        ether_addr_copy(vf->mac, mac);
 533
 534        rc = efx_ef10_evb_port_assign(efx, vf->vport_id, vf_i);
 535        if (rc)
 536                goto fail;
 537
 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;
 544                }
 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);
 549        }
 550
 551        return 0;
 552
 553fail:
 554        eth_zero_addr(vf->mac);
 555        return rc;
 556}
 557
 558int efx_ef10_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i, u16 vlan,
 559                               u8 qos)
 560{
 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;
 565
 566        if (vf_i >= efx->vf_count)
 567                return -EINVAL;
 568        if (qos != 0)
 569                return -EINVAL;
 570
 571        vf = nic_data->vf + vf_i;
 572
 573        new_vlan = (vlan == 0) ? EFX_EF10_NO_VLAN : vlan;
 574        if (new_vlan == vf->vlan)
 575                return 0;
 576
 577        if (vf->efx) {
 578                efx_device_detach_sync(vf->efx);
 579                efx_net_stop(vf->efx->net_dev);
 580
 581                mutex_lock(&vf->efx->mac_lock);
 582                down_write(&vf->efx->filter_sem);
 583                vf->efx->type->filter_table_remove(vf->efx);
 584
 585                rc = efx_ef10_vadaptor_free(vf->efx, EVB_PORT_ID_ASSIGNED);
 586                if (rc)
 587                        goto restore_filters;
 588        }
 589
 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;
 600                }
 601                vf->vport_assigned = 0;
 602        }
 603
 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;
 608        }
 609
 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;
 615        }
 616
 617        /* Do the actual vlan change */
 618        vf->vlan = new_vlan;
 619
 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;
 626
 627restore_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;
 633                }
 634        }
 635
 636restore_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;
 642
 643restore_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;
 648        }
 649
 650restore_filters:
 651        if (vf->efx) {
 652                rc2 = vf->efx->type->filter_table_probe(vf->efx);
 653                if (rc2)
 654                        goto reset_nic_up_write;
 655
 656                up_write(&vf->efx->filter_sem);
 657                mutex_unlock(&vf->efx->mac_lock);
 658
 659                rc2 = efx_net_open(vf->efx->net_dev);
 660                if (rc2)
 661                        goto reset_nic;
 662
 663                efx_device_attach_if_not_resetting(vf->efx);
 664        }
 665        return rc;
 666
 667reset_nic_up_write:
 668        if (vf->efx) {
 669                up_write(&vf->efx->filter_sem);
 670                mutex_unlock(&vf->efx->mac_lock);
 671        }
 672reset_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");
 683        }
 684
 685        return rc ? rc : rc2;
 686}
 687
 688static int efx_ef10_sriov_set_privilege_mask(struct efx_nic *efx, int vf_i,
 689                                             u32 mask, u32 value)
 690{
 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;
 697
 698        EFX_WARN_ON_PARANOID((value & ~mask) != 0);
 699
 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);
 704
 705        rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
 706                          pm_inbuf, sizeof(pm_inbuf),
 707                          pm_outbuf, sizeof(pm_outbuf), &outlen);
 708
 709        if (rc != 0)
 710                return rc;
 711        if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
 712                return -EIO;
 713
 714        old_mask = MCDI_DWORD(pm_outbuf, PRIVILEGE_MASK_OUT_OLD_MASK);
 715
 716        new_mask = old_mask & ~mask;
 717        new_mask |= value;
 718
 719        if (new_mask == old_mask)
 720                return 0;
 721
 722        new_mask |= MC_CMD_PRIVILEGE_MASK_IN_DO_CHANGE;
 723
 724        /* Set privilege mask */
 725        MCDI_SET_DWORD(pm_inbuf, PRIVILEGE_MASK_IN_NEW_MASK, new_mask);
 726
 727        rc = efx_mcdi_rpc(efx, MC_CMD_PRIVILEGE_MASK,
 728                          pm_inbuf, sizeof(pm_inbuf),
 729                          pm_outbuf, sizeof(pm_outbuf), &outlen);
 730
 731        if (rc != 0)
 732                return rc;
 733        if (outlen != MC_CMD_PRIVILEGE_MASK_OUT_LEN)
 734                return -EIO;
 735
 736        return 0;
 737}
 738
 739int efx_ef10_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i, bool spoofchk)
 740{
 741        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 742
 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;
 748
 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);
 752}
 753
 754int efx_ef10_sriov_set_vf_link_state(struct efx_nic *efx, int vf_i,
 755                                     int link_state)
 756{
 757        MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
 758        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 759
 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 */
 773}
 774
 775int efx_ef10_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
 776                                 struct ifla_vf_info *ivf)
 777{
 778        MCDI_DECLARE_BUF(inbuf, MC_CMD_LINK_STATE_MODE_IN_LEN);
 779        MCDI_DECLARE_BUF(outbuf, MC_CMD_LINK_STATE_MODE_OUT_LEN);
 780
 781        struct efx_ef10_nic_data *nic_data = efx->nic_data;
 782        struct ef10_vf *vf;
 783        size_t outlen;
 784        int rc;
 785
 786        if (vf_i >= efx->vf_count)
 787                return -EINVAL;
 788
 789        if (!nic_data->vf)
 790                return -EOPNOTSUPP;
 791
 792        vf = nic_data->vf + vf_i;
 793
 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;
 800
 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);
 814
 815        return 0;
 816}
 817