linux/drivers/net/ethernet/netronome/nfp/nfp_net_sriov.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
   2/* Copyright (C) 2017 Netronome Systems, Inc. */
   3
   4#include <linux/bitfield.h>
   5#include <linux/errno.h>
   6#include <linux/etherdevice.h>
   7#include <linux/if_link.h>
   8#include <linux/if_ether.h>
   9
  10#include "nfpcore/nfp_cpp.h"
  11#include "nfp_app.h"
  12#include "nfp_main.h"
  13#include "nfp_net_ctrl.h"
  14#include "nfp_net.h"
  15#include "nfp_net_sriov.h"
  16
  17static int
  18nfp_net_sriov_check(struct nfp_app *app, int vf, u16 cap, const char *msg)
  19{
  20        u16 cap_vf;
  21
  22        if (!app || !app->pf->vfcfg_tbl2)
  23                return -EOPNOTSUPP;
  24
  25        cap_vf = readw(app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_CAP);
  26        if ((cap_vf & cap) != cap) {
  27                nfp_warn(app->pf->cpp, "ndo_set_vf_%s not supported\n", msg);
  28                return -EOPNOTSUPP;
  29        }
  30
  31        if (vf < 0 || vf >= app->pf->num_vfs) {
  32                nfp_warn(app->pf->cpp, "invalid VF id %d\n", vf);
  33                return -EINVAL;
  34        }
  35
  36        return 0;
  37}
  38
  39static int
  40nfp_net_sriov_update(struct nfp_app *app, int vf, u16 update, const char *msg)
  41{
  42        struct nfp_net *nn;
  43        int ret;
  44
  45        /* Write update info to mailbox in VF config symbol */
  46        writeb(vf, app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_VF_NUM);
  47        writew(update, app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_UPD);
  48
  49        nn = list_first_entry(&app->pf->vnics, struct nfp_net, vnic_list);
  50        /* Signal VF reconfiguration */
  51        ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VF);
  52        if (ret)
  53                return ret;
  54
  55        ret = readw(app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_RET);
  56        if (ret)
  57                nfp_warn(app->pf->cpp,
  58                         "FW refused VF %s update with errno: %d\n", msg, ret);
  59        return -ret;
  60}
  61
  62int nfp_app_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  63{
  64        struct nfp_app *app = nfp_app_from_netdev(netdev);
  65        unsigned int vf_offset;
  66        int err;
  67
  68        err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_MAC, "mac");
  69        if (err)
  70                return err;
  71
  72        if (is_multicast_ether_addr(mac)) {
  73                nfp_warn(app->pf->cpp,
  74                         "invalid Ethernet address %pM for VF id %d\n",
  75                         mac, vf);
  76                return -EINVAL;
  77        }
  78
  79        /* Write MAC to VF entry in VF config symbol */
  80        vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
  81        writel(get_unaligned_be32(mac), app->pf->vfcfg_tbl2 + vf_offset);
  82        writew(get_unaligned_be16(mac + 4),
  83               app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_MAC_LO);
  84
  85        err = nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_MAC, "MAC");
  86        if (!err)
  87                nfp_info(app->pf->cpp,
  88                         "MAC %pM set on VF %d, reload the VF driver to make this change effective.\n",
  89                         mac, vf);
  90
  91        return err;
  92}
  93
  94int nfp_app_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
  95                        __be16 vlan_proto)
  96{
  97        struct nfp_app *app = nfp_app_from_netdev(netdev);
  98        unsigned int vf_offset;
  99        u16 vlan_tci;
 100        int err;
 101
 102        err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_VLAN, "vlan");
 103        if (err)
 104                return err;
 105
 106        if (vlan_proto != htons(ETH_P_8021Q))
 107                return -EOPNOTSUPP;
 108
 109        if (vlan > 4095 || qos > 7) {
 110                nfp_warn(app->pf->cpp,
 111                         "invalid vlan id or qos for VF id %d\n", vf);
 112                return -EINVAL;
 113        }
 114
 115        /* Write VLAN tag to VF entry in VF config symbol */
 116        vlan_tci = FIELD_PREP(NFP_NET_VF_CFG_VLAN_VID, vlan) |
 117                FIELD_PREP(NFP_NET_VF_CFG_VLAN_QOS, qos);
 118        vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
 119        writew(vlan_tci, app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_VLAN);
 120
 121        return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_VLAN,
 122                                    "vlan");
 123}
 124
 125int nfp_app_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable)
 126{
 127        struct nfp_app *app = nfp_app_from_netdev(netdev);
 128        unsigned int vf_offset;
 129        u8 vf_ctrl;
 130        int err;
 131
 132        err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_SPOOF,
 133                                  "spoofchk");
 134        if (err)
 135                return err;
 136
 137        /* Write spoof check control bit to VF entry in VF config symbol */
 138        vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ +
 139                NFP_NET_VF_CFG_CTRL;
 140        vf_ctrl = readb(app->pf->vfcfg_tbl2 + vf_offset);
 141        vf_ctrl &= ~NFP_NET_VF_CFG_CTRL_SPOOF;
 142        vf_ctrl |= FIELD_PREP(NFP_NET_VF_CFG_CTRL_SPOOF, enable);
 143        writeb(vf_ctrl, app->pf->vfcfg_tbl2 + vf_offset);
 144
 145        return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_SPOOF,
 146                                    "spoofchk");
 147}
 148
 149int nfp_app_set_vf_link_state(struct net_device *netdev, int vf,
 150                              int link_state)
 151{
 152        struct nfp_app *app = nfp_app_from_netdev(netdev);
 153        unsigned int vf_offset;
 154        u8 vf_ctrl;
 155        int err;
 156
 157        err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_LINK_STATE,
 158                                  "link_state");
 159        if (err)
 160                return err;
 161
 162        switch (link_state) {
 163        case IFLA_VF_LINK_STATE_AUTO:
 164        case IFLA_VF_LINK_STATE_ENABLE:
 165        case IFLA_VF_LINK_STATE_DISABLE:
 166                break;
 167        default:
 168                return -EINVAL;
 169        }
 170
 171        /* Write link state to VF entry in VF config symbol */
 172        vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ +
 173                NFP_NET_VF_CFG_CTRL;
 174        vf_ctrl = readb(app->pf->vfcfg_tbl2 + vf_offset);
 175        vf_ctrl &= ~NFP_NET_VF_CFG_CTRL_LINK_STATE;
 176        vf_ctrl |= FIELD_PREP(NFP_NET_VF_CFG_CTRL_LINK_STATE, link_state);
 177        writeb(vf_ctrl, app->pf->vfcfg_tbl2 + vf_offset);
 178
 179        return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_LINK_STATE,
 180                                    "link state");
 181}
 182
 183int nfp_app_get_vf_config(struct net_device *netdev, int vf,
 184                          struct ifla_vf_info *ivi)
 185{
 186        struct nfp_app *app = nfp_app_from_netdev(netdev);
 187        unsigned int vf_offset;
 188        u16 vlan_tci;
 189        u32 mac_hi;
 190        u16 mac_lo;
 191        u8 flags;
 192        int err;
 193
 194        err = nfp_net_sriov_check(app, vf, 0, "");
 195        if (err)
 196                return err;
 197
 198        vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
 199
 200        mac_hi = readl(app->pf->vfcfg_tbl2 + vf_offset);
 201        mac_lo = readw(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_MAC_LO);
 202
 203        flags = readb(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_CTRL);
 204        vlan_tci = readw(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_VLAN);
 205
 206        memset(ivi, 0, sizeof(*ivi));
 207        ivi->vf = vf;
 208
 209        put_unaligned_be32(mac_hi, &ivi->mac[0]);
 210        put_unaligned_be16(mac_lo, &ivi->mac[4]);
 211
 212        ivi->vlan = FIELD_GET(NFP_NET_VF_CFG_VLAN_VID, vlan_tci);
 213        ivi->qos = FIELD_GET(NFP_NET_VF_CFG_VLAN_QOS, vlan_tci);
 214
 215        ivi->spoofchk = FIELD_GET(NFP_NET_VF_CFG_CTRL_SPOOF, flags);
 216        ivi->linkstate = FIELD_GET(NFP_NET_VF_CFG_CTRL_LINK_STATE, flags);
 217
 218        return 0;
 219}
 220