1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2016-2017 Broadcom Limited
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.
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>
19 #include "bnxt_devlink.h"
22 #ifdef CONFIG_BNXT_SRIOV
24 #define CFA_HANDLE_INVALID 0xffff
25 #define VF_IDX_INVALID 0xffff
27 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
28 u16 *tx_cfa_action, u16 *rx_cfa_code)
30 struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr;
31 struct hwrm_cfa_vfr_alloc_input req = { 0 };
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);
38 mutex_lock(&bp->hwrm_cmd_lock);
39 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
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);
46 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
49 mutex_unlock(&bp->hwrm_cmd_lock);
53 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
55 struct hwrm_cfa_vfr_free_input req = { 0 };
58 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
59 sprintf(req.vfr_name, "vfr%d", vf_idx);
61 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
63 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
67 static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
70 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
71 struct hwrm_func_qcfg_input req = {0};
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);
78 mutex_lock(&bp->hwrm_cmd_lock);
80 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
82 mtu = le16_to_cpu(resp->max_mtu_configured);
84 *max_mtu = BNXT_MAX_MTU;
88 mutex_unlock(&bp->hwrm_cmd_lock);
92 static int bnxt_vf_rep_open(struct net_device *dev)
94 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
95 struct bnxt *bp = vf_rep->bp;
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);
105 static int bnxt_vf_rep_close(struct net_device *dev)
107 netif_carrier_off(dev);
108 netif_tx_disable(dev);
113 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
114 struct net_device *dev)
116 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
117 int rc, len = skb->len;
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;
124 rc = dev_queue_xmit(skb);
126 vf_rep->tx_stats.packets++;
127 vf_rep->tx_stats.bytes += len;
133 bnxt_vf_rep_get_stats64(struct net_device *dev,
134 struct rtnl_link_stats64 *stats)
136 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
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;
144 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
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;
152 if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
153 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
157 case TC_SETUP_CLSFLOWER:
158 return bnxt_tc_setup_flower(bp, vf_fid, type_data);
164 static int bnxt_vf_rep_setup_tc_block(struct net_device *dev,
165 struct tc_block_offload *f)
167 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
169 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
172 switch (f->command) {
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);
186 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
191 return bnxt_vf_rep_setup_tc_block(dev, type_data);
197 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
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;
209 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
211 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
213 vf_rep->rx_stats.bytes += skb->len;
214 vf_rep->rx_stats.packets++;
216 netif_receive_skb(skb);
219 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
222 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
223 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
226 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
233 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
234 struct ethtool_drvinfo *info)
236 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
237 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
240 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
241 struct switchdev_attr *attr)
243 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
245 /* as only PORT_PARENT_ID is supported currently use common code
246 * between PF and VF-rep for now.
248 return bnxt_port_attr_get(vf_rep->bp, attr);
251 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
252 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get
255 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
256 .get_drvinfo = bnxt_vf_rep_get_drvinfo
259 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
260 .ndo_open = bnxt_vf_rep_open,
261 .ndo_stop = bnxt_vf_rep_close,
262 .ndo_start_xmit = bnxt_vf_rep_xmit,
263 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
264 .ndo_setup_tc = bnxt_vf_rep_setup_tc,
265 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
268 bool bnxt_dev_is_vf_rep(struct net_device *dev)
270 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
273 /* Called when the parent PF interface is closed:
274 * As the mode transition from SWITCHDEV to LEGACY
275 * happens under the rtnl_lock() this routine is safe
276 * under the rtnl_lock()
278 void bnxt_vf_reps_close(struct bnxt *bp)
280 struct bnxt_vf_rep *vf_rep;
283 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
286 num_vfs = pci_num_vf(bp->pdev);
287 for (i = 0; i < num_vfs; i++) {
288 vf_rep = bp->vf_reps[i];
289 if (netif_running(vf_rep->dev))
290 bnxt_vf_rep_close(vf_rep->dev);
294 /* Called when the parent PF interface is opened (re-opened):
295 * As the mode transition from SWITCHDEV to LEGACY
296 * happen under the rtnl_lock() this routine is safe
297 * under the rtnl_lock()
299 void bnxt_vf_reps_open(struct bnxt *bp)
303 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
306 for (i = 0; i < pci_num_vf(bp->pdev); i++)
307 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
310 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
312 u16 num_vfs = pci_num_vf(bp->pdev);
313 struct bnxt_vf_rep *vf_rep;
316 for (i = 0; i < num_vfs; i++) {
317 vf_rep = bp->vf_reps[i];
319 dst_release((struct dst_entry *)vf_rep->dst);
321 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
322 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
325 /* if register_netdev failed, then netdev_ops
326 * would have been set to NULL
328 if (vf_rep->dev->netdev_ops)
329 unregister_netdev(vf_rep->dev);
330 free_netdev(vf_rep->dev);
339 void bnxt_vf_reps_destroy(struct bnxt *bp)
343 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
349 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
350 * before proceeding with VF-rep cleanup.
353 if (netif_running(bp->dev)) {
354 bnxt_close_nic(bp, false, false);
357 /* un-publish cfa_code_map so that RX path can't see it anymore */
358 kfree(bp->cfa_code_map);
359 bp->cfa_code_map = NULL;
360 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
363 bnxt_open_nic(bp, false, false);
366 /* Need to call vf_reps_destroy() outside of rntl_lock
367 * as unregister_netdev takes rtnl_lock
369 __bnxt_vf_reps_destroy(bp);
372 /* Use the OUI of the PF's perm addr and report the same mac addr
373 * for the same VF-rep each time
375 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
379 ether_addr_copy(mac, src_mac);
381 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
382 mac[3] = (u8)(addr & 0xFF);
383 mac[4] = (u8)((addr >> 8) & 0xFF);
384 mac[5] = (u8)((addr >> 16) & 0xFF);
387 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
388 struct net_device *dev)
390 struct net_device *pf_dev = bp->dev;
393 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
394 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
395 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
396 /* Just inherit all the featues of the parent PF as the VF-R
397 * uses the RX/TX rings of the parent PF
399 dev->hw_features = pf_dev->hw_features;
400 dev->gso_partial_features = pf_dev->gso_partial_features;
401 dev->vlan_features = pf_dev->vlan_features;
402 dev->hw_enc_features = pf_dev->hw_enc_features;
403 dev->features |= pf_dev->features;
404 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
406 ether_addr_copy(dev->dev_addr, dev->perm_addr);
407 /* Set VF-Rep's max-mtu to the corresponding VF's max-mtu */
408 if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu))
409 dev->max_mtu = max_mtu;
410 dev->min_mtu = ETH_ZLEN;
413 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
415 struct pci_dev *pdev = bp->pdev;
416 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
420 netdev_info(bp->dev, "Unable do read adapter's DSN");
424 /* DSN (two dw) is at an offset of 4 from the cap pos */
426 pci_read_config_dword(pdev, pos, &dw);
427 put_unaligned_le32(dw, &dsn[0]);
428 pci_read_config_dword(pdev, pos + 4, &dw);
429 put_unaligned_le32(dw, &dsn[4]);
433 static int bnxt_vf_reps_create(struct bnxt *bp)
435 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
436 struct bnxt_vf_rep *vf_rep;
437 struct net_device *dev;
440 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
444 /* storage for cfa_code to vf-idx mapping */
445 cfa_code_map = kmalloc_array(MAX_CFA_CODE, sizeof(*bp->cfa_code_map),
451 for (i = 0; i < MAX_CFA_CODE; i++)
452 cfa_code_map[i] = VF_IDX_INVALID;
454 for (i = 0; i < num_vfs; i++) {
455 dev = alloc_etherdev(sizeof(*vf_rep));
461 vf_rep = netdev_priv(dev);
462 bp->vf_reps[i] = vf_rep;
466 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
468 /* get cfa handles from FW */
469 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
470 &vf_rep->tx_cfa_action,
471 &vf_rep->rx_cfa_code);
476 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
478 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
484 /* only cfa_action is needed to mux a packet while TXing */
485 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
486 vf_rep->dst->u.port_info.lower_dev = bp->dev;
488 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
489 rc = register_netdev(dev);
491 /* no need for unregister_netdev in cleanup */
492 dev->netdev_ops = NULL;
497 /* Read the adapter's DSN to use as the eswitch switch_id */
498 rc = bnxt_pcie_dsn_get(bp, bp->switch_id);
502 /* publish cfa_code_map only after all VF-reps have been initialized */
503 bp->cfa_code_map = cfa_code_map;
504 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
505 netif_keep_dst(bp->dev);
509 netdev_info(bp->dev, "%s error=%d", __func__, rc);
511 __bnxt_vf_reps_destroy(bp);
515 /* Devlink related routines */
516 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
518 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
520 *mode = bp->eswitch_mode;
524 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode,
525 struct netlink_ext_ack *extack)
527 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
530 mutex_lock(&bp->sriov_lock);
531 if (bp->eswitch_mode == mode) {
532 netdev_info(bp->dev, "already in %s eswitch mode",
533 mode == DEVLINK_ESWITCH_MODE_LEGACY ?
534 "legacy" : "switchdev");
540 case DEVLINK_ESWITCH_MODE_LEGACY:
541 bnxt_vf_reps_destroy(bp);
544 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
545 if (bp->hwrm_spec_code < 0x10803) {
546 netdev_warn(bp->dev, "FW does not support SRIOV E-Switch SWITCHDEV mode\n");
551 if (pci_num_vf(bp->pdev) == 0) {
552 netdev_info(bp->dev, "Enable VFs before setting switchdev mode");
556 rc = bnxt_vf_reps_create(bp);
564 mutex_unlock(&bp->sriov_lock);