Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_vfr.c
blob26290403f38fa321e3f7bc397f2d2916857338c7
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.
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>
16 #include "bnxt_hsi.h"
17 #include "bnxt.h"
18 #include "bnxt_vfr.h"
19 #include "bnxt_devlink.h"
20 #include "bnxt_tc.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 };
32 int rc;
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);
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);
49 mutex_unlock(&bp->hwrm_cmd_lock);
50 return rc;
53 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
55 struct hwrm_cfa_vfr_free_input req = { 0 };
56 int rc;
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);
62 if (rc)
63 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
64 return rc;
67 static int bnxt_vf_rep_open(struct net_device *dev)
69 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
70 struct bnxt *bp = vf_rep->bp;
72 /* Enable link and TX only if the parent PF is open. */
73 if (netif_running(bp->dev)) {
74 netif_carrier_on(dev);
75 netif_tx_start_all_queues(dev);
77 return 0;
80 static int bnxt_vf_rep_close(struct net_device *dev)
82 netif_carrier_off(dev);
83 netif_tx_disable(dev);
85 return 0;
88 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
89 struct net_device *dev)
91 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
92 int rc, len = skb->len;
94 skb_dst_drop(skb);
95 dst_hold((struct dst_entry *)vf_rep->dst);
96 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
97 skb->dev = vf_rep->dst->u.port_info.lower_dev;
99 rc = dev_queue_xmit(skb);
100 if (!rc) {
101 vf_rep->tx_stats.packets++;
102 vf_rep->tx_stats.bytes += len;
104 return rc;
107 static void
108 bnxt_vf_rep_get_stats64(struct net_device *dev,
109 struct rtnl_link_stats64 *stats)
111 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
113 stats->rx_packets = vf_rep->rx_stats.packets;
114 stats->rx_bytes = vf_rep->rx_stats.bytes;
115 stats->tx_packets = vf_rep->tx_stats.packets;
116 stats->tx_bytes = vf_rep->tx_stats.bytes;
119 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
120 void *type_data,
121 void *cb_priv)
123 struct bnxt_vf_rep *vf_rep = cb_priv;
124 struct bnxt *bp = vf_rep->bp;
125 int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
127 if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
128 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
129 return -EOPNOTSUPP;
131 switch (type) {
132 case TC_SETUP_CLSFLOWER:
133 return bnxt_tc_setup_flower(bp, vf_fid, type_data);
134 default:
135 return -EOPNOTSUPP;
139 static int bnxt_vf_rep_setup_tc_block(struct net_device *dev,
140 struct tc_block_offload *f)
142 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
144 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
145 return -EOPNOTSUPP;
147 switch (f->command) {
148 case TC_BLOCK_BIND:
149 return tcf_block_cb_register(f->block,
150 bnxt_vf_rep_setup_tc_block_cb,
151 vf_rep, vf_rep);
152 case TC_BLOCK_UNBIND:
153 tcf_block_cb_unregister(f->block,
154 bnxt_vf_rep_setup_tc_block_cb, vf_rep);
155 return 0;
156 default:
157 return -EOPNOTSUPP;
161 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
162 void *type_data)
164 switch (type) {
165 case TC_SETUP_BLOCK:
166 return bnxt_vf_rep_setup_tc_block(dev, type_data);
167 default:
168 return -EOPNOTSUPP;
172 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
174 u16 vf_idx;
176 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
177 vf_idx = bp->cfa_code_map[cfa_code];
178 if (vf_idx != VF_IDX_INVALID)
179 return bp->vf_reps[vf_idx]->dev;
181 return NULL;
184 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
186 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
187 struct bnxt_vf_rep_stats *rx_stats;
189 rx_stats = &vf_rep->rx_stats;
190 vf_rep->rx_stats.bytes += skb->len;
191 vf_rep->rx_stats.packets++;
193 netif_receive_skb(skb);
196 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
197 size_t len)
199 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
200 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
201 int rc;
203 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
204 vf_rep->vf_idx);
205 if (rc >= len)
206 return -EOPNOTSUPP;
207 return 0;
210 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
211 struct ethtool_drvinfo *info)
213 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
214 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
217 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
218 struct switchdev_attr *attr)
220 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
222 /* as only PORT_PARENT_ID is supported currently use common code
223 * between PF and VF-rep for now.
225 return bnxt_port_attr_get(vf_rep->bp, attr);
228 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
229 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get
232 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
233 .get_drvinfo = bnxt_vf_rep_get_drvinfo
236 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
237 .ndo_open = bnxt_vf_rep_open,
238 .ndo_stop = bnxt_vf_rep_close,
239 .ndo_start_xmit = bnxt_vf_rep_xmit,
240 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
241 .ndo_setup_tc = bnxt_vf_rep_setup_tc,
242 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
245 bool bnxt_dev_is_vf_rep(struct net_device *dev)
247 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
250 /* Called when the parent PF interface is closed:
251 * As the mode transition from SWITCHDEV to LEGACY
252 * happens under the rtnl_lock() this routine is safe
253 * under the rtnl_lock()
255 void bnxt_vf_reps_close(struct bnxt *bp)
257 struct bnxt_vf_rep *vf_rep;
258 u16 num_vfs, i;
260 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
261 return;
263 num_vfs = pci_num_vf(bp->pdev);
264 for (i = 0; i < num_vfs; i++) {
265 vf_rep = bp->vf_reps[i];
266 if (netif_running(vf_rep->dev))
267 bnxt_vf_rep_close(vf_rep->dev);
271 /* Called when the parent PF interface is opened (re-opened):
272 * As the mode transition from SWITCHDEV to LEGACY
273 * happen under the rtnl_lock() this routine is safe
274 * under the rtnl_lock()
276 void bnxt_vf_reps_open(struct bnxt *bp)
278 int i;
280 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
281 return;
283 for (i = 0; i < pci_num_vf(bp->pdev); i++)
284 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
287 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
289 u16 num_vfs = pci_num_vf(bp->pdev);
290 struct bnxt_vf_rep *vf_rep;
291 int i;
293 for (i = 0; i < num_vfs; i++) {
294 vf_rep = bp->vf_reps[i];
295 if (vf_rep) {
296 dst_release((struct dst_entry *)vf_rep->dst);
298 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
299 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
301 if (vf_rep->dev) {
302 /* if register_netdev failed, then netdev_ops
303 * would have been set to NULL
305 if (vf_rep->dev->netdev_ops)
306 unregister_netdev(vf_rep->dev);
307 free_netdev(vf_rep->dev);
312 kfree(bp->vf_reps);
313 bp->vf_reps = NULL;
316 void bnxt_vf_reps_destroy(struct bnxt *bp)
318 bool closed = false;
320 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
321 return;
323 if (!bp->vf_reps)
324 return;
326 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
327 * before proceeding with VF-rep cleanup.
329 rtnl_lock();
330 if (netif_running(bp->dev)) {
331 bnxt_close_nic(bp, false, false);
332 closed = true;
334 /* un-publish cfa_code_map so that RX path can't see it anymore */
335 kfree(bp->cfa_code_map);
336 bp->cfa_code_map = NULL;
337 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
339 if (closed)
340 bnxt_open_nic(bp, false, false);
341 rtnl_unlock();
343 /* Need to call vf_reps_destroy() outside of rntl_lock
344 * as unregister_netdev takes rtnl_lock
346 __bnxt_vf_reps_destroy(bp);
349 /* Use the OUI of the PF's perm addr and report the same mac addr
350 * for the same VF-rep each time
352 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
354 u32 addr;
356 ether_addr_copy(mac, src_mac);
358 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
359 mac[3] = (u8)(addr & 0xFF);
360 mac[4] = (u8)((addr >> 8) & 0xFF);
361 mac[5] = (u8)((addr >> 16) & 0xFF);
364 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
365 struct net_device *dev)
367 struct net_device *pf_dev = bp->dev;
369 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
370 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
371 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
372 /* Just inherit all the featues of the parent PF as the VF-R
373 * uses the RX/TX rings of the parent PF
375 dev->hw_features = pf_dev->hw_features;
376 dev->gso_partial_features = pf_dev->gso_partial_features;
377 dev->vlan_features = pf_dev->vlan_features;
378 dev->hw_enc_features = pf_dev->hw_enc_features;
379 dev->features |= pf_dev->features;
380 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
381 dev->perm_addr);
382 ether_addr_copy(dev->dev_addr, dev->perm_addr);
385 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
387 struct pci_dev *pdev = bp->pdev;
388 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
389 u32 dw;
391 if (!pos) {
392 netdev_info(bp->dev, "Unable do read adapter's DSN");
393 return -EOPNOTSUPP;
396 /* DSN (two dw) is at an offset of 4 from the cap pos */
397 pos += 4;
398 pci_read_config_dword(pdev, pos, &dw);
399 put_unaligned_le32(dw, &dsn[0]);
400 pci_read_config_dword(pdev, pos + 4, &dw);
401 put_unaligned_le32(dw, &dsn[4]);
402 return 0;
405 static int bnxt_vf_reps_create(struct bnxt *bp)
407 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
408 struct bnxt_vf_rep *vf_rep;
409 struct net_device *dev;
410 int rc, i;
412 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
413 if (!bp->vf_reps)
414 return -ENOMEM;
416 /* storage for cfa_code to vf-idx mapping */
417 cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
418 GFP_KERNEL);
419 if (!cfa_code_map) {
420 rc = -ENOMEM;
421 goto err;
423 for (i = 0; i < MAX_CFA_CODE; i++)
424 cfa_code_map[i] = VF_IDX_INVALID;
426 for (i = 0; i < num_vfs; i++) {
427 dev = alloc_etherdev(sizeof(*vf_rep));
428 if (!dev) {
429 rc = -ENOMEM;
430 goto err;
433 vf_rep = netdev_priv(dev);
434 bp->vf_reps[i] = vf_rep;
435 vf_rep->dev = dev;
436 vf_rep->bp = bp;
437 vf_rep->vf_idx = i;
438 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
440 /* get cfa handles from FW */
441 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
442 &vf_rep->tx_cfa_action,
443 &vf_rep->rx_cfa_code);
444 if (rc) {
445 rc = -ENOLINK;
446 goto err;
448 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
450 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
451 GFP_KERNEL);
452 if (!vf_rep->dst) {
453 rc = -ENOMEM;
454 goto err;
456 /* only cfa_action is needed to mux a packet while TXing */
457 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
458 vf_rep->dst->u.port_info.lower_dev = bp->dev;
460 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
461 rc = register_netdev(dev);
462 if (rc) {
463 /* no need for unregister_netdev in cleanup */
464 dev->netdev_ops = NULL;
465 goto err;
469 /* Read the adapter's DSN to use as the eswitch switch_id */
470 rc = bnxt_pcie_dsn_get(bp, bp->switch_id);
471 if (rc)
472 goto err;
474 /* publish cfa_code_map only after all VF-reps have been initialized */
475 bp->cfa_code_map = cfa_code_map;
476 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
477 netif_keep_dst(bp->dev);
478 return 0;
480 err:
481 netdev_info(bp->dev, "%s error=%d", __func__, rc);
482 kfree(cfa_code_map);
483 __bnxt_vf_reps_destroy(bp);
484 return rc;
487 /* Devlink related routines */
488 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
490 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
492 *mode = bp->eswitch_mode;
493 return 0;
496 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
498 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
499 int rc = 0;
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;
510 switch (mode) {
511 case DEVLINK_ESWITCH_MODE_LEGACY:
512 bnxt_vf_reps_destroy(bp);
513 break;
515 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
516 if (pci_num_vf(bp->pdev) == 0) {
517 netdev_info(bp->dev,
518 "Enable VFs before setting switchdev mode");
519 rc = -EPERM;
520 goto done;
522 rc = bnxt_vf_reps_create(bp);
523 break;
525 default:
526 rc = -EINVAL;
527 goto done;
529 done:
530 mutex_unlock(&bp->sriov_lock);
531 return rc;
534 #endif