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>
19 #ifdef CONFIG_BNXT_SRIOV
21 #define CFA_HANDLE_INVALID 0xffff
22 #define VF_IDX_INVALID 0xffff
24 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
25 u16 *tx_cfa_action, u16 *rx_cfa_code)
27 struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr;
28 struct hwrm_cfa_vfr_alloc_input req = { 0 };
31 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_ALLOC, -1, -1);
32 req.vf_id = cpu_to_le16(vf_idx);
33 sprintf(req.vfr_name, "vfr%d", vf_idx);
35 mutex_lock(&bp->hwrm_cmd_lock);
36 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
38 *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action);
39 *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code);
40 netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
41 *tx_cfa_action, *rx_cfa_code);
43 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
46 mutex_unlock(&bp->hwrm_cmd_lock);
50 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
52 struct hwrm_cfa_vfr_free_input req = { 0 };
55 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
56 sprintf(req.vfr_name, "vfr%d", vf_idx);
58 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
60 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
64 static int bnxt_vf_rep_open(struct net_device *dev)
66 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
67 struct bnxt *bp = vf_rep->bp;
69 /* Enable link and TX only if the parent PF is open. */
70 if (netif_running(bp->dev)) {
71 netif_carrier_on(dev);
72 netif_tx_start_all_queues(dev);
77 static int bnxt_vf_rep_close(struct net_device *dev)
79 netif_carrier_off(dev);
80 netif_tx_disable(dev);
85 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
86 struct net_device *dev)
88 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
89 int rc, len = skb->len;
92 dst_hold((struct dst_entry *)vf_rep->dst);
93 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
94 skb->dev = vf_rep->dst->u.port_info.lower_dev;
96 rc = dev_queue_xmit(skb);
98 vf_rep->tx_stats.packets++;
99 vf_rep->tx_stats.bytes += len;
105 bnxt_vf_rep_get_stats64(struct net_device *dev,
106 struct rtnl_link_stats64 *stats)
108 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
110 stats->rx_packets = vf_rep->rx_stats.packets;
111 stats->rx_bytes = vf_rep->rx_stats.bytes;
112 stats->tx_packets = vf_rep->tx_stats.packets;
113 stats->tx_bytes = vf_rep->tx_stats.bytes;
116 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
120 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
121 vf_idx = bp->cfa_code_map[cfa_code];
122 if (vf_idx != VF_IDX_INVALID)
123 return bp->vf_reps[vf_idx]->dev;
128 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
130 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
131 struct bnxt_vf_rep_stats *rx_stats;
133 rx_stats = &vf_rep->rx_stats;
134 vf_rep->rx_stats.bytes += skb->len;
135 vf_rep->rx_stats.packets++;
137 netif_receive_skb(skb);
140 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
143 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
144 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
147 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
154 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
155 struct ethtool_drvinfo *info)
157 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
158 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
161 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
162 struct switchdev_attr *attr)
164 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
166 /* as only PORT_PARENT_ID is supported currently use common code
167 * between PF and VF-rep for now.
169 return bnxt_port_attr_get(vf_rep->bp, attr);
172 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
173 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get
176 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
177 .get_drvinfo = bnxt_vf_rep_get_drvinfo
180 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
181 .ndo_open = bnxt_vf_rep_open,
182 .ndo_stop = bnxt_vf_rep_close,
183 .ndo_start_xmit = bnxt_vf_rep_xmit,
184 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
185 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
188 /* Called when the parent PF interface is closed:
189 * As the mode transition from SWITCHDEV to LEGACY
190 * happens under the rtnl_lock() this routine is safe
191 * under the rtnl_lock()
193 void bnxt_vf_reps_close(struct bnxt *bp)
195 struct bnxt_vf_rep *vf_rep;
198 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
201 num_vfs = pci_num_vf(bp->pdev);
202 for (i = 0; i < num_vfs; i++) {
203 vf_rep = bp->vf_reps[i];
204 if (netif_running(vf_rep->dev))
205 bnxt_vf_rep_close(vf_rep->dev);
209 /* Called when the parent PF interface is opened (re-opened):
210 * As the mode transition from SWITCHDEV to LEGACY
211 * happen under the rtnl_lock() this routine is safe
212 * under the rtnl_lock()
214 void bnxt_vf_reps_open(struct bnxt *bp)
218 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
221 for (i = 0; i < pci_num_vf(bp->pdev); i++)
222 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
225 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
227 u16 num_vfs = pci_num_vf(bp->pdev);
228 struct bnxt_vf_rep *vf_rep;
231 for (i = 0; i < num_vfs; i++) {
232 vf_rep = bp->vf_reps[i];
234 dst_release((struct dst_entry *)vf_rep->dst);
236 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
237 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
240 /* if register_netdev failed, then netdev_ops
241 * would have been set to NULL
243 if (vf_rep->dev->netdev_ops)
244 unregister_netdev(vf_rep->dev);
245 free_netdev(vf_rep->dev);
254 void bnxt_vf_reps_destroy(struct bnxt *bp)
258 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
264 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
265 * before proceeding with VF-rep cleanup.
268 if (netif_running(bp->dev)) {
269 bnxt_close_nic(bp, false, false);
272 /* un-publish cfa_code_map so that RX path can't see it anymore */
273 kfree(bp->cfa_code_map);
274 bp->cfa_code_map = NULL;
275 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
278 bnxt_open_nic(bp, false, false);
281 /* Need to call vf_reps_destroy() outside of rntl_lock
282 * as unregister_netdev takes rtnl_lock
284 __bnxt_vf_reps_destroy(bp);
287 /* Use the OUI of the PF's perm addr and report the same mac addr
288 * for the same VF-rep each time
290 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
294 ether_addr_copy(mac, src_mac);
296 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
297 mac[3] = (u8)(addr & 0xFF);
298 mac[4] = (u8)((addr >> 8) & 0xFF);
299 mac[5] = (u8)((addr >> 16) & 0xFF);
302 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
303 struct net_device *dev)
305 struct net_device *pf_dev = bp->dev;
307 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
308 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
309 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
310 /* Just inherit all the featues of the parent PF as the VF-R
311 * uses the RX/TX rings of the parent PF
313 dev->hw_features = pf_dev->hw_features;
314 dev->gso_partial_features = pf_dev->gso_partial_features;
315 dev->vlan_features = pf_dev->vlan_features;
316 dev->hw_enc_features = pf_dev->hw_enc_features;
317 dev->features |= pf_dev->features;
318 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
320 ether_addr_copy(dev->dev_addr, dev->perm_addr);
323 static int bnxt_vf_reps_create(struct bnxt *bp)
325 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
326 struct bnxt_vf_rep *vf_rep;
327 struct net_device *dev;
330 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
334 /* storage for cfa_code to vf-idx mapping */
335 cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
341 for (i = 0; i < MAX_CFA_CODE; i++)
342 cfa_code_map[i] = VF_IDX_INVALID;
344 for (i = 0; i < num_vfs; i++) {
345 dev = alloc_etherdev(sizeof(*vf_rep));
351 vf_rep = netdev_priv(dev);
352 bp->vf_reps[i] = vf_rep;
356 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
358 /* get cfa handles from FW */
359 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
360 &vf_rep->tx_cfa_action,
361 &vf_rep->rx_cfa_code);
366 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
368 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
374 /* only cfa_action is needed to mux a packet while TXing */
375 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
376 vf_rep->dst->u.port_info.lower_dev = bp->dev;
378 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
379 rc = register_netdev(dev);
381 /* no need for unregister_netdev in cleanup */
382 dev->netdev_ops = NULL;
387 /* publish cfa_code_map only after all VF-reps have been initialized */
388 bp->cfa_code_map = cfa_code_map;
389 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
390 netif_keep_dst(bp->dev);
394 netdev_info(bp->dev, "%s error=%d", __func__, rc);
396 __bnxt_vf_reps_destroy(bp);
400 /* Devlink related routines */
401 static int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
403 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
405 *mode = bp->eswitch_mode;
409 static int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
411 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
414 mutex_lock(&bp->sriov_lock);
415 if (bp->eswitch_mode == mode) {
416 netdev_info(bp->dev, "already in %s eswitch mode",
417 mode == DEVLINK_ESWITCH_MODE_LEGACY ?
418 "legacy" : "switchdev");
424 case DEVLINK_ESWITCH_MODE_LEGACY:
425 bnxt_vf_reps_destroy(bp);
428 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
429 if (pci_num_vf(bp->pdev) == 0) {
431 "Enable VFs before setting swtichdev mode");
435 rc = bnxt_vf_reps_create(bp);
443 mutex_unlock(&bp->sriov_lock);
447 static const struct devlink_ops bnxt_dl_ops = {
448 .eswitch_mode_set = bnxt_dl_eswitch_mode_set,
449 .eswitch_mode_get = bnxt_dl_eswitch_mode_get
452 int bnxt_dl_register(struct bnxt *bp)
457 if (!pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV))
460 if (bp->hwrm_spec_code < 0x10800) {
461 netdev_warn(bp->dev, "Firmware does not support SR-IOV E-Switch SWITCHDEV mode.\n");
465 dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
467 netdev_warn(bp->dev, "devlink_alloc failed");
471 bnxt_link_bp_to_dl(dl, bp);
472 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
473 rc = devlink_register(dl, &bp->pdev->dev);
475 bnxt_link_bp_to_dl(dl, NULL);
477 netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
484 void bnxt_dl_unregister(struct bnxt *bp)
486 struct devlink *dl = bp->dl;
491 devlink_unregister(dl);