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>
21 #ifdef CONFIG_BNXT_SRIOV
23 #define CFA_HANDLE_INVALID 0xffff
24 #define VF_IDX_INVALID 0xffff
26 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
27 u16 *tx_cfa_action, u16 *rx_cfa_code)
29 struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr;
30 struct hwrm_cfa_vfr_alloc_input req = { 0 };
33 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_ALLOC, -1, -1);
34 req.vf_id = cpu_to_le16(vf_idx);
35 sprintf(req.vfr_name, "vfr%d", vf_idx);
37 mutex_lock(&bp->hwrm_cmd_lock);
38 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
40 *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action);
41 *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code);
42 netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
43 *tx_cfa_action, *rx_cfa_code);
45 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
48 mutex_unlock(&bp->hwrm_cmd_lock);
52 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
54 struct hwrm_cfa_vfr_free_input req = { 0 };
57 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
58 sprintf(req.vfr_name, "vfr%d", vf_idx);
60 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
62 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
66 static int bnxt_vf_rep_open(struct net_device *dev)
68 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
69 struct bnxt *bp = vf_rep->bp;
71 /* Enable link and TX only if the parent PF is open. */
72 if (netif_running(bp->dev)) {
73 netif_carrier_on(dev);
74 netif_tx_start_all_queues(dev);
79 static int bnxt_vf_rep_close(struct net_device *dev)
81 netif_carrier_off(dev);
82 netif_tx_disable(dev);
87 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
88 struct net_device *dev)
90 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
91 int rc, len = skb->len;
94 dst_hold((struct dst_entry *)vf_rep->dst);
95 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
96 skb->dev = vf_rep->dst->u.port_info.lower_dev;
98 rc = dev_queue_xmit(skb);
100 vf_rep->tx_stats.packets++;
101 vf_rep->tx_stats.bytes += len;
107 bnxt_vf_rep_get_stats64(struct net_device *dev,
108 struct rtnl_link_stats64 *stats)
110 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
112 stats->rx_packets = vf_rep->rx_stats.packets;
113 stats->rx_bytes = vf_rep->rx_stats.bytes;
114 stats->tx_packets = vf_rep->tx_stats.packets;
115 stats->tx_bytes = vf_rep->tx_stats.bytes;
118 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
121 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
122 struct bnxt *bp = vf_rep->bp;
123 int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
126 case TC_SETUP_CLSFLOWER:
127 return bnxt_tc_setup_flower(bp, vf_fid, type_data);
133 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
137 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
138 vf_idx = bp->cfa_code_map[cfa_code];
139 if (vf_idx != VF_IDX_INVALID)
140 return bp->vf_reps[vf_idx]->dev;
145 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
147 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
148 struct bnxt_vf_rep_stats *rx_stats;
150 rx_stats = &vf_rep->rx_stats;
151 vf_rep->rx_stats.bytes += skb->len;
152 vf_rep->rx_stats.packets++;
154 netif_receive_skb(skb);
157 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
160 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
161 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
164 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
171 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
172 struct ethtool_drvinfo *info)
174 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
175 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
178 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
179 struct switchdev_attr *attr)
181 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
183 /* as only PORT_PARENT_ID is supported currently use common code
184 * between PF and VF-rep for now.
186 return bnxt_port_attr_get(vf_rep->bp, attr);
189 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
190 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get
193 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
194 .get_drvinfo = bnxt_vf_rep_get_drvinfo
197 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
198 .ndo_open = bnxt_vf_rep_open,
199 .ndo_stop = bnxt_vf_rep_close,
200 .ndo_start_xmit = bnxt_vf_rep_xmit,
201 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
202 .ndo_setup_tc = bnxt_vf_rep_setup_tc,
203 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
206 /* Called when the parent PF interface is closed:
207 * As the mode transition from SWITCHDEV to LEGACY
208 * happens under the rtnl_lock() this routine is safe
209 * under the rtnl_lock()
211 void bnxt_vf_reps_close(struct bnxt *bp)
213 struct bnxt_vf_rep *vf_rep;
216 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
219 num_vfs = pci_num_vf(bp->pdev);
220 for (i = 0; i < num_vfs; i++) {
221 vf_rep = bp->vf_reps[i];
222 if (netif_running(vf_rep->dev))
223 bnxt_vf_rep_close(vf_rep->dev);
227 /* Called when the parent PF interface is opened (re-opened):
228 * As the mode transition from SWITCHDEV to LEGACY
229 * happen under the rtnl_lock() this routine is safe
230 * under the rtnl_lock()
232 void bnxt_vf_reps_open(struct bnxt *bp)
236 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
239 for (i = 0; i < pci_num_vf(bp->pdev); i++)
240 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
243 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
245 u16 num_vfs = pci_num_vf(bp->pdev);
246 struct bnxt_vf_rep *vf_rep;
249 for (i = 0; i < num_vfs; i++) {
250 vf_rep = bp->vf_reps[i];
252 dst_release((struct dst_entry *)vf_rep->dst);
254 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
255 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
258 /* if register_netdev failed, then netdev_ops
259 * would have been set to NULL
261 if (vf_rep->dev->netdev_ops)
262 unregister_netdev(vf_rep->dev);
263 free_netdev(vf_rep->dev);
272 void bnxt_vf_reps_destroy(struct bnxt *bp)
276 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
282 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
283 * before proceeding with VF-rep cleanup.
286 if (netif_running(bp->dev)) {
287 bnxt_close_nic(bp, false, false);
290 /* un-publish cfa_code_map so that RX path can't see it anymore */
291 kfree(bp->cfa_code_map);
292 bp->cfa_code_map = NULL;
293 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
296 bnxt_open_nic(bp, false, false);
299 /* Need to call vf_reps_destroy() outside of rntl_lock
300 * as unregister_netdev takes rtnl_lock
302 __bnxt_vf_reps_destroy(bp);
305 /* Use the OUI of the PF's perm addr and report the same mac addr
306 * for the same VF-rep each time
308 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
312 ether_addr_copy(mac, src_mac);
314 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
315 mac[3] = (u8)(addr & 0xFF);
316 mac[4] = (u8)((addr >> 8) & 0xFF);
317 mac[5] = (u8)((addr >> 16) & 0xFF);
320 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
321 struct net_device *dev)
323 struct net_device *pf_dev = bp->dev;
325 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
326 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
327 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
328 /* Just inherit all the featues of the parent PF as the VF-R
329 * uses the RX/TX rings of the parent PF
331 dev->hw_features = pf_dev->hw_features;
332 dev->gso_partial_features = pf_dev->gso_partial_features;
333 dev->vlan_features = pf_dev->vlan_features;
334 dev->hw_enc_features = pf_dev->hw_enc_features;
335 dev->features |= pf_dev->features;
336 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
338 ether_addr_copy(dev->dev_addr, dev->perm_addr);
341 static int bnxt_vf_reps_create(struct bnxt *bp)
343 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
344 struct bnxt_vf_rep *vf_rep;
345 struct net_device *dev;
348 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
352 /* storage for cfa_code to vf-idx mapping */
353 cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
359 for (i = 0; i < MAX_CFA_CODE; i++)
360 cfa_code_map[i] = VF_IDX_INVALID;
362 for (i = 0; i < num_vfs; i++) {
363 dev = alloc_etherdev(sizeof(*vf_rep));
369 vf_rep = netdev_priv(dev);
370 bp->vf_reps[i] = vf_rep;
374 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
376 /* get cfa handles from FW */
377 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
378 &vf_rep->tx_cfa_action,
379 &vf_rep->rx_cfa_code);
384 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
386 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
392 /* only cfa_action is needed to mux a packet while TXing */
393 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
394 vf_rep->dst->u.port_info.lower_dev = bp->dev;
396 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
397 rc = register_netdev(dev);
399 /* no need for unregister_netdev in cleanup */
400 dev->netdev_ops = NULL;
405 /* publish cfa_code_map only after all VF-reps have been initialized */
406 bp->cfa_code_map = cfa_code_map;
407 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
408 netif_keep_dst(bp->dev);
412 netdev_info(bp->dev, "%s error=%d", __func__, rc);
414 __bnxt_vf_reps_destroy(bp);
418 /* Devlink related routines */
419 static int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
421 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
423 *mode = bp->eswitch_mode;
427 static int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
429 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
432 mutex_lock(&bp->sriov_lock);
433 if (bp->eswitch_mode == mode) {
434 netdev_info(bp->dev, "already in %s eswitch mode",
435 mode == DEVLINK_ESWITCH_MODE_LEGACY ?
436 "legacy" : "switchdev");
442 case DEVLINK_ESWITCH_MODE_LEGACY:
443 bnxt_vf_reps_destroy(bp);
446 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
447 if (pci_num_vf(bp->pdev) == 0) {
449 "Enable VFs before setting switchdev mode");
453 rc = bnxt_vf_reps_create(bp);
461 mutex_unlock(&bp->sriov_lock);
465 static const struct devlink_ops bnxt_dl_ops = {
466 .eswitch_mode_set = bnxt_dl_eswitch_mode_set,
467 .eswitch_mode_get = bnxt_dl_eswitch_mode_get
470 int bnxt_dl_register(struct bnxt *bp)
475 if (!pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV))
478 if (bp->hwrm_spec_code < 0x10800) {
479 netdev_warn(bp->dev, "Firmware does not support SR-IOV E-Switch SWITCHDEV mode.\n");
483 dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
485 netdev_warn(bp->dev, "devlink_alloc failed");
489 bnxt_link_bp_to_dl(bp, dl);
490 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
491 rc = devlink_register(dl, &bp->pdev->dev);
493 bnxt_link_bp_to_dl(bp, NULL);
495 netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
502 void bnxt_dl_unregister(struct bnxt *bp)
504 struct devlink *dl = bp->dl;
509 devlink_unregister(dl);