linux/drivers/net/ethernet/broadcom/bnxt/bnxt_vfr.c
<<
>>
Prefs
   1/* Broadcom NetXtreme-C/E network driver.
   2 *
   3 * Copyright (c) 2016-2017 Broadcom Limited
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation.
   8 */
   9#include <linux/pci.h>
  10#include <linux/netdevice.h>
  11#include <linux/etherdevice.h>
  12#include <linux/rtnetlink.h>
  13#include <linux/jhash.h>
  14#include <net/pkt_cls.h>
  15
  16#include "bnxt_hsi.h"
  17#include "bnxt.h"
  18#include "bnxt_vfr.h"
  19#include "bnxt_devlink.h"
  20#include "bnxt_tc.h"
  21
  22#ifdef CONFIG_BNXT_SRIOV
  23
  24#define CFA_HANDLE_INVALID              0xffff
  25#define VF_IDX_INVALID                  0xffff
  26
  27static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
  28                              u16 *tx_cfa_action, u16 *rx_cfa_code)
  29{
  30        struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  31        struct hwrm_cfa_vfr_alloc_input req = { 0 };
  32        int rc;
  33
  34        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_ALLOC, -1, -1);
  35        req.vf_id = cpu_to_le16(vf_idx);
  36        sprintf(req.vfr_name, "vfr%d", vf_idx);
  37
  38        mutex_lock(&bp->hwrm_cmd_lock);
  39        rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  40        if (!rc) {
  41                *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action);
  42                *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code);
  43                netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
  44                           *tx_cfa_action, *rx_cfa_code);
  45        } else {
  46                netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
  47        }
  48
  49        mutex_unlock(&bp->hwrm_cmd_lock);
  50        return rc;
  51}
  52
  53static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
  54{
  55        struct hwrm_cfa_vfr_free_input req = { 0 };
  56        int rc;
  57
  58        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
  59        sprintf(req.vfr_name, "vfr%d", vf_idx);
  60
  61        rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  62        if (rc)
  63                netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
  64        return rc;
  65}
  66
  67static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
  68                              u16 *max_mtu)
  69{
  70        struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  71        struct hwrm_func_qcfg_input req = {0};
  72        u16 mtu;
  73        int rc;
  74
  75        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  76        req.fid = cpu_to_le16(bp->pf.vf[vf_rep->vf_idx].fw_fid);
  77
  78        mutex_lock(&bp->hwrm_cmd_lock);
  79
  80        rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  81        if (!rc) {
  82                mtu = le16_to_cpu(resp->max_mtu_configured);
  83                if (!mtu)
  84                        *max_mtu = BNXT_MAX_MTU;
  85                else
  86                        *max_mtu = mtu;
  87        }
  88        mutex_unlock(&bp->hwrm_cmd_lock);
  89        return rc;
  90}
  91
  92static int bnxt_vf_rep_open(struct net_device *dev)
  93{
  94        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
  95        struct bnxt *bp = vf_rep->bp;
  96
  97        /* Enable link and TX only if the parent PF is open. */
  98        if (netif_running(bp->dev)) {
  99                netif_carrier_on(dev);
 100                netif_tx_start_all_queues(dev);
 101        }
 102        return 0;
 103}
 104
 105static int bnxt_vf_rep_close(struct net_device *dev)
 106{
 107        netif_carrier_off(dev);
 108        netif_tx_disable(dev);
 109
 110        return 0;
 111}
 112
 113static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
 114                                    struct net_device *dev)
 115{
 116        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
 117        int rc, len = skb->len;
 118
 119        skb_dst_drop(skb);
 120        dst_hold((struct dst_entry *)vf_rep->dst);
 121        skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
 122        skb->dev = vf_rep->dst->u.port_info.lower_dev;
 123
 124        rc = dev_queue_xmit(skb);
 125        if (!rc) {
 126                vf_rep->tx_stats.packets++;
 127                vf_rep->tx_stats.bytes += len;
 128        }
 129        return rc;
 130}
 131
 132static void
 133bnxt_vf_rep_get_stats64(struct net_device *dev,
 134                        struct rtnl_link_stats64 *stats)
 135{
 136        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
 137
 138        stats->rx_packets = vf_rep->rx_stats.packets;
 139        stats->rx_bytes = vf_rep->rx_stats.bytes;
 140        stats->tx_packets = vf_rep->tx_stats.packets;
 141        stats->tx_bytes = vf_rep->tx_stats.bytes;
 142}
 143
 144static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
 145                                         void *type_data,
 146                                         void *cb_priv)
 147{
 148        struct bnxt_vf_rep *vf_rep = cb_priv;
 149        struct bnxt *bp = vf_rep->bp;
 150        int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
 151
 152        if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
 153            !tc_cls_can_offload_and_chain0(bp->dev, type_data))
 154                return -EOPNOTSUPP;
 155
 156        switch (type) {
 157        case TC_SETUP_CLSFLOWER:
 158                return bnxt_tc_setup_flower(bp, vf_fid, type_data);
 159        default:
 160                return -EOPNOTSUPP;
 161        }
 162}
 163
 164static int bnxt_vf_rep_setup_tc_block(struct net_device *dev,
 165                                      struct tc_block_offload *f)
 166{
 167        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
 168
 169        if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
 170                return -EOPNOTSUPP;
 171
 172        switch (f->command) {
 173        case TC_BLOCK_BIND:
 174                return tcf_block_cb_register(f->block,
 175                                             bnxt_vf_rep_setup_tc_block_cb,
 176                                             vf_rep, vf_rep, f->extack);
 177        case TC_BLOCK_UNBIND:
 178                tcf_block_cb_unregister(f->block,
 179                                        bnxt_vf_rep_setup_tc_block_cb, vf_rep);
 180                return 0;
 181        default:
 182                return -EOPNOTSUPP;
 183        }
 184}
 185
 186static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
 187                                void *type_data)
 188{
 189        switch (type) {
 190        case TC_SETUP_BLOCK:
 191                return bnxt_vf_rep_setup_tc_block(dev, type_data);
 192        default:
 193                return -EOPNOTSUPP;
 194        }
 195}
 196
 197struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
 198{
 199        u16 vf_idx;
 200
 201        if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
 202                vf_idx = bp->cfa_code_map[cfa_code];
 203                if (vf_idx != VF_IDX_INVALID)
 204                        return bp->vf_reps[vf_idx]->dev;
 205        }
 206        return NULL;
 207}
 208
 209void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
 210{
 211        struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
 212
 213        vf_rep->rx_stats.bytes += skb->len;
 214        vf_rep->rx_stats.packets++;
 215
 216        netif_receive_skb(skb);
 217}
 218
 219static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
 220                                          size_t len)
 221{
 222        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
 223        struct pci_dev *pf_pdev = vf_rep->bp->pdev;
 224        int rc;
 225
 226        rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
 227                      vf_rep->vf_idx);
 228        if (rc >= len)
 229                return -EOPNOTSUPP;
 230        return 0;
 231}
 232
 233static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
 234                                    struct ethtool_drvinfo *info)
 235{
 236        strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
 237        strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
 238}
 239
 240static int bnxt_vf_rep_get_port_parent_id(struct net_device *dev,
 241                                          struct netdev_phys_item_id *ppid)
 242{
 243        struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
 244
 245        /* as only PORT_PARENT_ID is supported currently use common code
 246         * between PF and VF-rep for now.
 247         */
 248        return bnxt_get_port_parent_id(vf_rep->bp->dev, ppid);
 249}
 250
 251static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
 252        .get_drvinfo            = bnxt_vf_rep_get_drvinfo
 253};
 254
 255static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
 256        .ndo_open               = bnxt_vf_rep_open,
 257        .ndo_stop               = bnxt_vf_rep_close,
 258        .ndo_start_xmit         = bnxt_vf_rep_xmit,
 259        .ndo_get_stats64        = bnxt_vf_rep_get_stats64,
 260        .ndo_setup_tc           = bnxt_vf_rep_setup_tc,
 261        .ndo_get_port_parent_id = bnxt_vf_rep_get_port_parent_id,
 262        .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
 263};
 264
 265bool bnxt_dev_is_vf_rep(struct net_device *dev)
 266{
 267        return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
 268}
 269
 270/* Called when the parent PF interface is closed:
 271 * As the mode transition from SWITCHDEV to LEGACY
 272 * happens under the rtnl_lock() this routine is safe
 273 * under the rtnl_lock()
 274 */
 275void bnxt_vf_reps_close(struct bnxt *bp)
 276{
 277        struct bnxt_vf_rep *vf_rep;
 278        u16 num_vfs, i;
 279
 280        if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
 281                return;
 282
 283        num_vfs = pci_num_vf(bp->pdev);
 284        for (i = 0; i < num_vfs; i++) {
 285                vf_rep = bp->vf_reps[i];
 286                if (netif_running(vf_rep->dev))
 287                        bnxt_vf_rep_close(vf_rep->dev);
 288        }
 289}
 290
 291/* Called when the parent PF interface is opened (re-opened):
 292 * As the mode transition from SWITCHDEV to LEGACY
 293 * happen under the rtnl_lock() this routine is safe
 294 * under the rtnl_lock()
 295 */
 296void bnxt_vf_reps_open(struct bnxt *bp)
 297{
 298        int i;
 299
 300        if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
 301                return;
 302
 303        for (i = 0; i < pci_num_vf(bp->pdev); i++)
 304                bnxt_vf_rep_open(bp->vf_reps[i]->dev);
 305}
 306
 307static void __bnxt_vf_reps_destroy(struct bnxt *bp)
 308{
 309        u16 num_vfs = pci_num_vf(bp->pdev);
 310        struct bnxt_vf_rep *vf_rep;
 311        int i;
 312
 313        for (i = 0; i < num_vfs; i++) {
 314                vf_rep = bp->vf_reps[i];
 315                if (vf_rep) {
 316                        dst_release((struct dst_entry *)vf_rep->dst);
 317
 318                        if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
 319                                hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
 320
 321                        if (vf_rep->dev) {
 322                                /* if register_netdev failed, then netdev_ops
 323                                 * would have been set to NULL
 324                                 */
 325                                if (vf_rep->dev->netdev_ops)
 326                                        unregister_netdev(vf_rep->dev);
 327                                free_netdev(vf_rep->dev);
 328                        }
 329                }
 330        }
 331
 332        kfree(bp->vf_reps);
 333        bp->vf_reps = NULL;
 334}
 335
 336void bnxt_vf_reps_destroy(struct bnxt *bp)
 337{
 338        bool closed = false;
 339
 340        if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
 341                return;
 342
 343        if (!bp->vf_reps)
 344                return;
 345
 346        /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
 347         * before proceeding with VF-rep cleanup.
 348         */
 349        rtnl_lock();
 350        if (netif_running(bp->dev)) {
 351                bnxt_close_nic(bp, false, false);
 352                closed = true;
 353        }
 354        /* un-publish cfa_code_map so that RX path can't see it anymore */
 355        kfree(bp->cfa_code_map);
 356        bp->cfa_code_map = NULL;
 357        bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
 358
 359        if (closed)
 360                bnxt_open_nic(bp, false, false);
 361        rtnl_unlock();
 362
 363        /* Need to call vf_reps_destroy() outside of rntl_lock
 364         * as unregister_netdev takes rtnl_lock
 365         */
 366        __bnxt_vf_reps_destroy(bp);
 367}
 368
 369/* Use the OUI of the PF's perm addr and report the same mac addr
 370 * for the same VF-rep each time
 371 */
 372static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
 373{
 374        u32 addr;
 375
 376        ether_addr_copy(mac, src_mac);
 377
 378        addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
 379        mac[3] = (u8)(addr & 0xFF);
 380        mac[4] = (u8)((addr >> 8) & 0xFF);
 381        mac[5] = (u8)((addr >> 16) & 0xFF);
 382}
 383
 384static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
 385                                    struct net_device *dev)
 386{
 387        struct net_device *pf_dev = bp->dev;
 388        u16 max_mtu;
 389
 390        dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
 391        dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
 392        /* Just inherit all the featues of the parent PF as the VF-R
 393         * uses the RX/TX rings of the parent PF
 394         */
 395        dev->hw_features = pf_dev->hw_features;
 396        dev->gso_partial_features = pf_dev->gso_partial_features;
 397        dev->vlan_features = pf_dev->vlan_features;
 398        dev->hw_enc_features = pf_dev->hw_enc_features;
 399        dev->features |= pf_dev->features;
 400        bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
 401                                 dev->perm_addr);
 402        ether_addr_copy(dev->dev_addr, dev->perm_addr);
 403        /* Set VF-Rep's max-mtu to the corresponding VF's max-mtu */
 404        if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu))
 405                dev->max_mtu = max_mtu;
 406        dev->min_mtu = ETH_ZLEN;
 407}
 408
 409static int bnxt_vf_reps_create(struct bnxt *bp)
 410{
 411        u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
 412        struct bnxt_vf_rep *vf_rep;
 413        struct net_device *dev;
 414        int rc, i;
 415
 416        bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
 417        if (!bp->vf_reps)
 418                return -ENOMEM;
 419
 420        /* storage for cfa_code to vf-idx mapping */
 421        cfa_code_map = kmalloc_array(MAX_CFA_CODE, sizeof(*bp->cfa_code_map),
 422                                     GFP_KERNEL);
 423        if (!cfa_code_map) {
 424                rc = -ENOMEM;
 425                goto err;
 426        }
 427        for (i = 0; i < MAX_CFA_CODE; i++)
 428                cfa_code_map[i] = VF_IDX_INVALID;
 429
 430        for (i = 0; i < num_vfs; i++) {
 431                dev = alloc_etherdev(sizeof(*vf_rep));
 432                if (!dev) {
 433                        rc = -ENOMEM;
 434                        goto err;
 435                }
 436
 437                vf_rep = netdev_priv(dev);
 438                bp->vf_reps[i] = vf_rep;
 439                vf_rep->dev = dev;
 440                vf_rep->bp = bp;
 441                vf_rep->vf_idx = i;
 442                vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
 443
 444                /* get cfa handles from FW */
 445                rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
 446                                        &vf_rep->tx_cfa_action,
 447                                        &vf_rep->rx_cfa_code);
 448                if (rc) {
 449                        rc = -ENOLINK;
 450                        goto err;
 451                }
 452                cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
 453
 454                vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
 455                                                 GFP_KERNEL);
 456                if (!vf_rep->dst) {
 457                        rc = -ENOMEM;
 458                        goto err;
 459                }
 460                /* only cfa_action is needed to mux a packet while TXing */
 461                vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
 462                vf_rep->dst->u.port_info.lower_dev = bp->dev;
 463
 464                bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
 465                rc = register_netdev(dev);
 466                if (rc) {
 467                        /* no need for unregister_netdev in cleanup */
 468                        dev->netdev_ops = NULL;
 469                        goto err;
 470                }
 471        }
 472
 473        /* publish cfa_code_map only after all VF-reps have been initialized */
 474        bp->cfa_code_map = cfa_code_map;
 475        bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
 476        netif_keep_dst(bp->dev);
 477        return 0;
 478
 479err:
 480        netdev_info(bp->dev, "%s error=%d", __func__, rc);
 481        kfree(cfa_code_map);
 482        __bnxt_vf_reps_destroy(bp);
 483        return rc;
 484}
 485
 486/* Devlink related routines */
 487int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
 488{
 489        struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
 490
 491        *mode = bp->eswitch_mode;
 492        return 0;
 493}
 494
 495int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode,
 496                             struct netlink_ext_ack *extack)
 497{
 498        struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
 499        int rc = 0;
 500
 501        mutex_lock(&bp->sriov_lock);
 502        if (bp->eswitch_mode == mode) {
 503                netdev_info(bp->dev, "already in %s eswitch mode",
 504                            mode == DEVLINK_ESWITCH_MODE_LEGACY ?
 505                            "legacy" : "switchdev");
 506                rc = -EINVAL;
 507                goto done;
 508        }
 509
 510        switch (mode) {
 511        case DEVLINK_ESWITCH_MODE_LEGACY:
 512                bnxt_vf_reps_destroy(bp);
 513                break;
 514
 515        case DEVLINK_ESWITCH_MODE_SWITCHDEV:
 516                if (bp->hwrm_spec_code < 0x10803) {
 517                        netdev_warn(bp->dev, "FW does not support SRIOV E-Switch SWITCHDEV mode\n");
 518                        rc = -ENOTSUPP;
 519                        goto done;
 520                }
 521
 522                if (pci_num_vf(bp->pdev) == 0) {
 523                        netdev_info(bp->dev, "Enable VFs before setting switchdev mode");
 524                        rc = -EPERM;
 525                        goto done;
 526                }
 527                rc = bnxt_vf_reps_create(bp);
 528                break;
 529
 530        default:
 531                rc = -EINVAL;
 532                goto done;
 533        }
 534done:
 535        mutex_unlock(&bp->sriov_lock);
 536        return rc;
 537}
 538
 539#endif
 540