1
2
3
4
5
6
7
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>
15
16#include "bnxt_hsi.h"
17#include "bnxt.h"
18#include "bnxt_vfr.h"
19#include "bnxt_devlink.h"
20#include "bnxt_tc.h"
21
22#ifdef CONFIG_BNXT_SRIOV
23
24#define CFA_HANDLE_INVALID 0xffff
25#define VF_IDX_INVALID 0xffff
26
27static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
28 u16 *tx_cfa_action, u16 *rx_cfa_code)
29{
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;
33
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);
37
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);
47 }
48
49 mutex_unlock(&bp->hwrm_cmd_lock);
50 return rc;
51}
52
53static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
54{
55 struct hwrm_cfa_vfr_free_input req = { 0 };
56 int rc;
57
58 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
59 sprintf(req.vfr_name, "vfr%d", vf_idx);
60
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;
65}
66
67static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
68 u16 *max_mtu)
69{
70 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
71 struct hwrm_func_qcfg_input req = {0};
72 u16 mtu;
73 int rc;
74
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);
77
78 mutex_lock(&bp->hwrm_cmd_lock);
79
80 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
81 if (!rc) {
82 mtu = le16_to_cpu(resp->max_mtu_configured);
83 if (!mtu)
84 *max_mtu = BNXT_MAX_MTU;
85 else
86 *max_mtu = mtu;
87 }
88 mutex_unlock(&bp->hwrm_cmd_lock);
89 return rc;
90}
91
92static int bnxt_vf_rep_open(struct net_device *dev)
93{
94 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
95 struct bnxt *bp = vf_rep->bp;
96
97
98 if (netif_running(bp->dev)) {
99 netif_carrier_on(dev);
100 netif_tx_start_all_queues(dev);
101 }
102 return 0;
103}
104
105static int bnxt_vf_rep_close(struct net_device *dev)
106{
107 netif_carrier_off(dev);
108 netif_tx_disable(dev);
109
110 return 0;
111}
112
113static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
114 struct net_device *dev)
115{
116 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
117 int rc, len = skb->len;
118
119 skb_dst_drop(skb);
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;
123
124 rc = dev_queue_xmit(skb);
125 if (!rc) {
126 vf_rep->tx_stats.packets++;
127 vf_rep->tx_stats.bytes += len;
128 }
129 return rc;
130}
131
132static void
133bnxt_vf_rep_get_stats64(struct net_device *dev,
134 struct rtnl_link_stats64 *stats)
135{
136 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
137
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;
142}
143
144static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
145 void *type_data,
146 void *cb_priv)
147{
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;
151
152 if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
153 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
154 return -EOPNOTSUPP;
155
156 switch (type) {
157 case TC_SETUP_CLSFLOWER:
158 return bnxt_tc_setup_flower(bp, vf_fid, type_data);
159 default:
160 return -EOPNOTSUPP;
161 }
162}
163
164static int bnxt_vf_rep_setup_tc_block(struct net_device *dev,
165 struct tc_block_offload *f)
166{
167 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
168
169 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
170 return -EOPNOTSUPP;
171
172 switch (f->command) {
173 case TC_BLOCK_BIND:
174 return tcf_block_cb_register(f->block,
175 bnxt_vf_rep_setup_tc_block_cb,
176 vf_rep, vf_rep);
177 case TC_BLOCK_UNBIND:
178 tcf_block_cb_unregister(f->block,
179 bnxt_vf_rep_setup_tc_block_cb, vf_rep);
180 return 0;
181 default:
182 return -EOPNOTSUPP;
183 }
184}
185
186static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
187 void *type_data)
188{
189 switch (type) {
190 case TC_SETUP_BLOCK:
191 return bnxt_vf_rep_setup_tc_block(dev, type_data);
192 default:
193 return -EOPNOTSUPP;
194 }
195}
196
197struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
198{
199 u16 vf_idx;
200
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;
205 }
206 return NULL;
207}
208
209void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
210{
211 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
212 struct bnxt_vf_rep_stats *rx_stats;
213
214 rx_stats = &vf_rep->rx_stats;
215 vf_rep->rx_stats.bytes += skb->len;
216 vf_rep->rx_stats.packets++;
217
218 netif_receive_skb(skb);
219}
220
221static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
222 size_t len)
223{
224 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
225 struct pci_dev *pf_pdev = vf_rep->bp->pdev;
226 int rc;
227
228 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
229 vf_rep->vf_idx);
230 if (rc >= len)
231 return -EOPNOTSUPP;
232 return 0;
233}
234
235static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
236 struct ethtool_drvinfo *info)
237{
238 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
239 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
240}
241
242static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
243 struct switchdev_attr *attr)
244{
245 struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
246
247
248
249
250 return bnxt_port_attr_get(vf_rep->bp, attr);
251}
252
253static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
254 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get
255};
256
257static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
258 .get_drvinfo = bnxt_vf_rep_get_drvinfo
259};
260
261static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
262 .ndo_size = sizeof(struct net_device_ops),
263 .ndo_open = bnxt_vf_rep_open,
264 .ndo_stop = bnxt_vf_rep_close,
265 .ndo_start_xmit = bnxt_vf_rep_xmit,
266 .ndo_get_stats64 = bnxt_vf_rep_get_stats64,
267 .extended.ndo_setup_tc_rh = bnxt_vf_rep_setup_tc,
268 .extended.ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
269};
270
271bool bnxt_dev_is_vf_rep(struct net_device *dev)
272{
273 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
274}
275
276
277
278
279
280
281void bnxt_vf_reps_close(struct bnxt *bp)
282{
283 struct bnxt_vf_rep *vf_rep;
284 u16 num_vfs, i;
285
286 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
287 return;
288
289 num_vfs = pci_num_vf(bp->pdev);
290 for (i = 0; i < num_vfs; i++) {
291 vf_rep = bp->vf_reps[i];
292 if (netif_running(vf_rep->dev))
293 bnxt_vf_rep_close(vf_rep->dev);
294 }
295}
296
297
298
299
300
301
302void bnxt_vf_reps_open(struct bnxt *bp)
303{
304 int i;
305
306 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
307 return;
308
309 for (i = 0; i < pci_num_vf(bp->pdev); i++)
310 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
311}
312
313static void __bnxt_vf_reps_destroy(struct bnxt *bp)
314{
315 u16 num_vfs = pci_num_vf(bp->pdev);
316 struct bnxt_vf_rep *vf_rep;
317 int i;
318
319 for (i = 0; i < num_vfs; i++) {
320 vf_rep = bp->vf_reps[i];
321 if (vf_rep) {
322 dst_release((struct dst_entry *)vf_rep->dst);
323
324 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
325 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
326
327 if (vf_rep->dev) {
328
329
330
331 if (vf_rep->dev->netdev_ops)
332 unregister_netdev(vf_rep->dev);
333 free_netdev(vf_rep->dev);
334 }
335 }
336 }
337
338 kfree(bp->vf_reps);
339 bp->vf_reps = NULL;
340}
341
342void bnxt_vf_reps_destroy(struct bnxt *bp)
343{
344 bool closed = false;
345
346 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
347 return;
348
349 if (!bp->vf_reps)
350 return;
351
352
353
354
355 rtnl_lock();
356 if (netif_running(bp->dev)) {
357 bnxt_close_nic(bp, false, false);
358 closed = true;
359 }
360
361 kfree(bp->cfa_code_map);
362 bp->cfa_code_map = NULL;
363 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
364
365 if (closed)
366 bnxt_open_nic(bp, false, false);
367 rtnl_unlock();
368
369
370
371
372 __bnxt_vf_reps_destroy(bp);
373}
374
375
376
377
378static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
379{
380 u32 addr;
381
382 ether_addr_copy(mac, src_mac);
383
384 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
385 mac[3] = (u8)(addr & 0xFF);
386 mac[4] = (u8)((addr >> 8) & 0xFF);
387 mac[5] = (u8)((addr >> 16) & 0xFF);
388}
389
390static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
391 struct net_device *dev)
392{
393 struct net_device *pf_dev = bp->dev;
394 u16 max_mtu;
395
396 dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
397 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
398 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
399
400
401
402 dev->hw_features = pf_dev->hw_features;
403 dev->gso_partial_features = pf_dev->gso_partial_features;
404 dev->vlan_features = pf_dev->vlan_features;
405 dev->hw_enc_features = pf_dev->hw_enc_features;
406 dev->features |= pf_dev->features;
407 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
408 dev->perm_addr);
409 ether_addr_copy(dev->dev_addr, dev->perm_addr);
410
411 if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu))
412 dev->extended->max_mtu = max_mtu;
413 dev->extended->min_mtu = ETH_ZLEN;
414}
415
416static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
417{
418 struct pci_dev *pdev = bp->pdev;
419 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
420 u32 dw;
421
422 if (!pos) {
423 netdev_info(bp->dev, "Unable do read adapter's DSN");
424 return -EOPNOTSUPP;
425 }
426
427
428 pos += 4;
429 pci_read_config_dword(pdev, pos, &dw);
430 put_unaligned_le32(dw, &dsn[0]);
431 pci_read_config_dword(pdev, pos + 4, &dw);
432 put_unaligned_le32(dw, &dsn[4]);
433 return 0;
434}
435
436static int bnxt_vf_reps_create(struct bnxt *bp)
437{
438 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
439 struct bnxt_vf_rep *vf_rep;
440 struct net_device *dev;
441 int rc, i;
442
443 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
444 if (!bp->vf_reps)
445 return -ENOMEM;
446
447
448 cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
449 GFP_KERNEL);
450 if (!cfa_code_map) {
451 rc = -ENOMEM;
452 goto err;
453 }
454 for (i = 0; i < MAX_CFA_CODE; i++)
455 cfa_code_map[i] = VF_IDX_INVALID;
456
457 for (i = 0; i < num_vfs; i++) {
458 dev = alloc_etherdev(sizeof(*vf_rep));
459 if (!dev) {
460 rc = -ENOMEM;
461 goto err;
462 }
463
464 vf_rep = netdev_priv(dev);
465 bp->vf_reps[i] = vf_rep;
466 vf_rep->dev = dev;
467 vf_rep->bp = bp;
468 vf_rep->vf_idx = i;
469 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
470
471
472 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
473 &vf_rep->tx_cfa_action,
474 &vf_rep->rx_cfa_code);
475 if (rc) {
476 rc = -ENOLINK;
477 goto err;
478 }
479 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
480
481 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
482 GFP_KERNEL);
483 if (!vf_rep->dst) {
484 rc = -ENOMEM;
485 goto err;
486 }
487
488 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
489 vf_rep->dst->u.port_info.lower_dev = bp->dev;
490
491 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
492 rc = register_netdev(dev);
493 if (rc) {
494
495 dev->netdev_ops = NULL;
496 goto err;
497 }
498 }
499
500
501 rc = bnxt_pcie_dsn_get(bp, bp->switch_id);
502 if (rc)
503 goto err;
504
505
506 bp->cfa_code_map = cfa_code_map;
507 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
508 netif_keep_dst(bp->dev);
509 return 0;
510
511err:
512 netdev_info(bp->dev, "%s error=%d", __func__, rc);
513 kfree(cfa_code_map);
514 __bnxt_vf_reps_destroy(bp);
515 return rc;
516}
517
518
519int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
520{
521 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
522
523 *mode = bp->eswitch_mode;
524 return 0;
525}
526
527int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
528{
529 struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
530 int rc = 0;
531
532 mutex_lock(&bp->sriov_lock);
533 if (bp->eswitch_mode == mode) {
534 netdev_info(bp->dev, "already in %s eswitch mode",
535 mode == DEVLINK_ESWITCH_MODE_LEGACY ?
536 "legacy" : "switchdev");
537 rc = -EINVAL;
538 goto done;
539 }
540
541 switch (mode) {
542 case DEVLINK_ESWITCH_MODE_LEGACY:
543 bnxt_vf_reps_destroy(bp);
544 break;
545
546 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
547 if (pci_num_vf(bp->pdev) == 0) {
548 netdev_info(bp->dev,
549 "Enable VFs before setting switchdev mode");
550 rc = -EPERM;
551 goto done;
552 }
553 rc = bnxt_vf_reps_create(bp);
554 break;
555
556 default:
557 rc = -EINVAL;
558 goto done;
559 }
560done:
561 mutex_unlock(&bp->sriov_lock);
562 return rc;
563}
564
565#endif
566