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>
18 #include "bnxt_hwrm.h"
20 #include "bnxt_devlink.h"
23 #ifdef CONFIG_BNXT_SRIOV
25 #define CFA_HANDLE_INVALID 0xffff
26 #define VF_IDX_INVALID 0xffff
28 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
29 u16 *tx_cfa_action, u16 *rx_cfa_code)
31 struct hwrm_cfa_vfr_alloc_output *resp;
32 struct hwrm_cfa_vfr_alloc_input *req;
35 rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_ALLOC);
37 req->vf_id = cpu_to_le16(vf_idx);
38 sprintf(req->vfr_name, "vfr%d", vf_idx);
40 resp = hwrm_req_hold(bp, req);
41 rc = hwrm_req_send(bp, req);
43 *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action);
44 *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code);
45 netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
46 *tx_cfa_action, *rx_cfa_code);
48 hwrm_req_drop(bp, req);
51 netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc);
55 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
57 struct hwrm_cfa_vfr_free_input *req;
60 rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_FREE);
62 sprintf(req->vfr_name, "vfr%d", vf_idx);
63 rc = hwrm_req_send(bp, req);
66 netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc);
70 static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
73 struct hwrm_func_qcfg_output *resp;
74 struct hwrm_func_qcfg_input *req;
78 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
82 req->fid = cpu_to_le16(bp->pf.vf[vf_rep->vf_idx].fw_fid);
83 resp = hwrm_req_hold(bp, req);
84 rc = hwrm_req_send(bp, req);
86 mtu = le16_to_cpu(resp->max_mtu_configured);
88 *max_mtu = BNXT_MAX_MTU;
92 hwrm_req_drop(bp, req);
96 static int bnxt_vf_rep_open(struct net_device *dev)
98 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
99 struct bnxt *bp = vf_rep->bp;
101 /* Enable link and TX only if the parent PF is open. */
102 if (netif_running(bp->dev)) {
103 netif_carrier_on(dev);
104 netif_tx_start_all_queues(dev);
109 static int bnxt_vf_rep_close(struct net_device *dev)
111 netif_carrier_off(dev);
112 netif_tx_disable(dev);
117 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
118 struct net_device *dev)
120 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
121 int rc, len = skb->len;
124 dst_hold((struct dst_entry *)vf_rep->dst);
125 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
126 skb->dev = vf_rep->dst->u.port_info.lower_dev;
128 rc = dev_queue_xmit(skb);
130 vf_rep->tx_stats.packets++;
131 vf_rep->tx_stats.bytes += len;
137 bnxt_vf_rep_get_stats64(struct net_device *dev,
138 struct rtnl_link_stats64 *stats)
140 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
142 stats->rx_packets = vf_rep->rx_stats.packets;
143 stats->rx_bytes = vf_rep->rx_stats.bytes;
144 stats->tx_packets = vf_rep->tx_stats.packets;
145 stats->tx_bytes = vf_rep->tx_stats.bytes;
148 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
152 struct bnxt_vf_rep *vf_rep = cb_priv;
153 struct bnxt *bp = vf_rep->bp;
154 int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
156 if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
157 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
161 case TC_SETUP_CLSFLOWER:
162 return bnxt_tc_setup_flower(bp, vf_fid, type_data);
168 static LIST_HEAD(bnxt_vf_block_cb_list);
170 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
173 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
177 return flow_block_cb_setup_simple(type_data,
178 &bnxt_vf_block_cb_list,
179 bnxt_vf_rep_setup_tc_block_cb,
180 vf_rep, vf_rep, true);
186 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
190 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
191 vf_idx = bp->cfa_code_map[cfa_code];
192 if (vf_idx != VF_IDX_INVALID)
193 return bp->vf_reps[vf_idx]->dev;
198 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
200 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
202 vf_rep->rx_stats.bytes += skb->len;
203 vf_rep->rx_stats.packets++;
205 netif_receive_skb(skb);
208 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
211 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
212 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
215 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
222 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
223 struct ethtool_drvinfo *info)
225 strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
228 static int bnxt_vf_rep_get_port_parent_id(struct net_device *dev,
229 struct netdev_phys_item_id *ppid)
231 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
233 /* as only PORT_PARENT_ID is supported currently use common code
234 * between PF and VF-rep for now.
236 return bnxt_get_port_parent_id(vf_rep->bp->dev, ppid);
239 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
240 .get_drvinfo = bnxt_vf_rep_get_drvinfo
243 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
244 .ndo_open = bnxt_vf_rep_open,
245 .ndo_stop = bnxt_vf_rep_close,
246 .ndo_start_xmit = bnxt_vf_rep_xmit,
247 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
248 .ndo_setup_tc = bnxt_vf_rep_setup_tc,
249 .ndo_get_port_parent_id = bnxt_vf_rep_get_port_parent_id,
250 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
253 bool bnxt_dev_is_vf_rep(struct net_device *dev)
255 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
258 /* Called when the parent PF interface is closed:
259 * As the mode transition from SWITCHDEV to LEGACY
260 * happens under the rtnl_lock() this routine is safe
261 * under the rtnl_lock()
263 void bnxt_vf_reps_close(struct bnxt *bp)
265 struct bnxt_vf_rep *vf_rep;
268 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
271 num_vfs = pci_num_vf(bp->pdev);
272 for (i = 0; i < num_vfs; i++) {
273 vf_rep = bp->vf_reps[i];
274 if (netif_running(vf_rep->dev))
275 bnxt_vf_rep_close(vf_rep->dev);
279 /* Called when the parent PF interface is opened (re-opened):
280 * As the mode transition from SWITCHDEV to LEGACY
281 * happen under the rtnl_lock() this routine is safe
282 * under the rtnl_lock()
284 void bnxt_vf_reps_open(struct bnxt *bp)
288 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
291 for (i = 0; i < pci_num_vf(bp->pdev); i++) {
292 /* Open the VF-Rep only if it is allocated in the FW */
293 if (bp->vf_reps[i]->tx_cfa_action != CFA_HANDLE_INVALID)
294 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
298 static void __bnxt_free_one_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep)
304 dst_release((struct dst_entry *)vf_rep->dst);
307 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID) {
308 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
309 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
313 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
315 u16 num_vfs = pci_num_vf(bp->pdev);
316 struct bnxt_vf_rep *vf_rep;
319 for (i = 0; i < num_vfs; i++) {
320 vf_rep = bp->vf_reps[i];
322 __bnxt_free_one_vf_rep(bp, vf_rep);
324 /* if register_netdev failed, then netdev_ops
325 * would have been set to NULL
327 if (vf_rep->dev->netdev_ops)
328 unregister_netdev(vf_rep->dev);
329 free_netdev(vf_rep->dev);
338 void bnxt_vf_reps_destroy(struct bnxt *bp)
342 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
348 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
349 * before proceeding with VF-rep cleanup.
352 if (netif_running(bp->dev)) {
353 bnxt_close_nic(bp, false, false);
356 /* un-publish cfa_code_map so that RX path can't see it anymore */
357 kfree(bp->cfa_code_map);
358 bp->cfa_code_map = NULL;
361 /* Temporarily set legacy mode to avoid re-opening
362 * representors and restore switchdev mode after that.
364 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
365 bnxt_open_nic(bp, false, false);
366 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
370 /* Need to call vf_reps_destroy() outside of rntl_lock
371 * as unregister_netdev takes rtnl_lock
373 __bnxt_vf_reps_destroy(bp);
376 /* Free the VF-Reps in firmware, during firmware hot-reset processing.
377 * Note that the VF-Rep netdevs are still active (not unregistered) during
378 * this process. As the mode transition from SWITCHDEV to LEGACY happens
379 * under the rtnl_lock() this routine is safe under the rtnl_lock().
381 void bnxt_vf_reps_free(struct bnxt *bp)
383 u16 num_vfs = pci_num_vf(bp->pdev);
386 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
389 for (i = 0; i < num_vfs; i++)
390 __bnxt_free_one_vf_rep(bp, bp->vf_reps[i]);
393 static int bnxt_alloc_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
396 /* get cfa handles from FW */
397 if (hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx, &vf_rep->tx_cfa_action,
398 &vf_rep->rx_cfa_code))
401 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
402 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL);
406 /* only cfa_action is needed to mux a packet while TXing */
407 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
408 vf_rep->dst->u.port_info.lower_dev = bp->dev;
413 /* Allocate the VF-Reps in firmware, during firmware hot-reset processing.
414 * Note that the VF-Rep netdevs are still active (not unregistered) during
415 * this process. As the mode transition from SWITCHDEV to LEGACY happens
416 * under the rtnl_lock() this routine is safe under the rtnl_lock().
418 int bnxt_vf_reps_alloc(struct bnxt *bp)
420 u16 *cfa_code_map = bp->cfa_code_map, num_vfs = pci_num_vf(bp->pdev);
421 struct bnxt_vf_rep *vf_rep;
424 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
430 for (i = 0; i < MAX_CFA_CODE; i++)
431 cfa_code_map[i] = VF_IDX_INVALID;
433 for (i = 0; i < num_vfs; i++) {
434 vf_rep = bp->vf_reps[i];
437 rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map);
445 netdev_info(bp->dev, "%s error=%d\n", __func__, rc);
446 bnxt_vf_reps_free(bp);
450 /* Use the OUI of the PF's perm addr and report the same mac addr
451 * for the same VF-rep each time
453 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
457 ether_addr_copy(mac, src_mac);
459 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
460 mac[3] = (u8)(addr & 0xFF);
461 mac[4] = (u8)((addr >> 8) & 0xFF);
462 mac[5] = (u8)((addr >> 16) & 0xFF);
465 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
466 struct net_device *dev)
468 struct net_device *pf_dev = bp->dev;
471 SET_NETDEV_DEV(dev, &bp->pdev->dev);
472 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
473 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
474 /* Just inherit all the featues of the parent PF as the VF-R
475 * uses the RX/TX rings of the parent PF
477 dev->hw_features = pf_dev->hw_features;
478 dev->gso_partial_features = pf_dev->gso_partial_features;
479 dev->vlan_features = pf_dev->vlan_features;
480 dev->hw_enc_features = pf_dev->hw_enc_features;
481 dev->features |= pf_dev->features;
482 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
484 eth_hw_addr_set(dev, dev->perm_addr);
485 /* Set VF-Rep's max-mtu to the corresponding VF's max-mtu */
486 if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu))
487 dev->max_mtu = max_mtu;
488 dev->min_mtu = ETH_ZLEN;
491 int bnxt_vf_reps_create(struct bnxt *bp)
493 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
494 struct bnxt_vf_rep *vf_rep;
495 struct net_device *dev;
498 if (!(bp->flags & BNXT_FLAG_DSN_VALID))
501 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
505 /* storage for cfa_code to vf-idx mapping */
506 cfa_code_map = kmalloc_array(MAX_CFA_CODE, sizeof(*bp->cfa_code_map),
512 for (i = 0; i < MAX_CFA_CODE; i++)
513 cfa_code_map[i] = VF_IDX_INVALID;
515 for (i = 0; i < num_vfs; i++) {
516 dev = alloc_etherdev(sizeof(*vf_rep));
522 vf_rep = netdev_priv(dev);
523 bp->vf_reps[i] = vf_rep;
527 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
529 rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map);
533 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
534 rc = register_netdev(dev);
536 /* no need for unregister_netdev in cleanup */
537 dev->netdev_ops = NULL;
542 /* publish cfa_code_map only after all VF-reps have been initialized */
543 bp->cfa_code_map = cfa_code_map;
544 netif_keep_dst(bp->dev);
548 netdev_info(bp->dev, "%s error=%d\n", __func__, rc);
550 __bnxt_vf_reps_destroy(bp);
554 /* Devlink related routines */
555 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
557 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
559 *mode = bp->eswitch_mode;
563 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode,
564 struct netlink_ext_ack *extack)
566 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
569 if (bp->eswitch_mode == mode) {
570 netdev_info(bp->dev, "already in %s eswitch mode\n",
571 mode == DEVLINK_ESWITCH_MODE_LEGACY ?
572 "legacy" : "switchdev");
577 case DEVLINK_ESWITCH_MODE_LEGACY:
578 bnxt_vf_reps_destroy(bp);
581 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
582 if (bp->hwrm_spec_code < 0x10803) {
583 netdev_warn(bp->dev, "FW does not support SRIOV E-Switch SWITCHDEV mode\n");
587 /* Create representors for existing VFs */
588 if (pci_num_vf(bp->pdev) > 0)
589 ret = bnxt_vf_reps_create(bp);
597 bp->eswitch_mode = mode;