b05c5d0ee3f9ef732177d4c6785fddcc73cf301d
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_vfr.c
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2016-2017 Broadcom Limited
4  *
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
15 #include "bnxt_hsi.h"
16 #include "bnxt.h"
17 #include "bnxt_vfr.h"
18
19 #ifdef CONFIG_BNXT_SRIOV
20
21 #define CFA_HANDLE_INVALID              0xffff
22 #define VF_IDX_INVALID                  0xffff
23
24 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
25                               u16 *tx_cfa_action, u16 *rx_cfa_code)
26 {
27         struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr;
28         struct hwrm_cfa_vfr_alloc_input req = { 0 };
29         int rc;
30
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);
34
35         mutex_lock(&bp->hwrm_cmd_lock);
36         rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
37         if (!rc) {
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);
42         } else {
43                 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
44         }
45
46         mutex_unlock(&bp->hwrm_cmd_lock);
47         return rc;
48 }
49
50 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
51 {
52         struct hwrm_cfa_vfr_free_input req = { 0 };
53         int rc;
54
55         bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1);
56         sprintf(req.vfr_name, "vfr%d", vf_idx);
57
58         rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
59         if (rc)
60                 netdev_info(bp->dev, "%s error rc=%d", __func__, rc);
61         return rc;
62 }
63
64 static int bnxt_vf_rep_open(struct net_device *dev)
65 {
66         struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
67         struct bnxt *bp = vf_rep->bp;
68
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);
73         }
74         return 0;
75 }
76
77 static int bnxt_vf_rep_close(struct net_device *dev)
78 {
79         netif_carrier_off(dev);
80         netif_tx_disable(dev);
81
82         return 0;
83 }
84
85 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
86                                     struct net_device *dev)
87 {
88         struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
89         int rc, len = skb->len;
90
91         skb_dst_drop(skb);
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;
95
96         rc = dev_queue_xmit(skb);
97         if (!rc) {
98                 vf_rep->tx_stats.packets++;
99                 vf_rep->tx_stats.bytes += len;
100         }
101         return rc;
102 }
103
104 static void
105 bnxt_vf_rep_get_stats64(struct net_device *dev,
106                         struct rtnl_link_stats64 *stats)
107 {
108         struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
109
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;
114 }
115
116 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
117 {
118         u16 vf_idx;
119
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;
124         }
125         return NULL;
126 }
127
128 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
129 {
130         struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
131         struct bnxt_vf_rep_stats *rx_stats;
132
133         rx_stats = &vf_rep->rx_stats;
134         vf_rep->rx_stats.bytes += skb->len;
135         vf_rep->rx_stats.packets++;
136
137         netif_receive_skb(skb);
138 }
139
140 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
141                                           size_t len)
142 {
143         struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
144         struct pci_dev *pf_pdev = vf_rep->bp->pdev;
145         int rc;
146
147         rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
148                       vf_rep->vf_idx);
149         if (rc >= len)
150                 return -EOPNOTSUPP;
151         return 0;
152 }
153
154 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
155                                     struct ethtool_drvinfo *info)
156 {
157         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
158         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
159 }
160
161 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
162                                      struct switchdev_attr *attr)
163 {
164         struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
165
166         /* as only PORT_PARENT_ID is supported currently use common code
167          * between PF and VF-rep for now.
168          */
169         return bnxt_port_attr_get(vf_rep->bp, attr);
170 }
171
172 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
173         .switchdev_port_attr_get        = bnxt_vf_rep_port_attr_get
174 };
175
176 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
177         .get_drvinfo            = bnxt_vf_rep_get_drvinfo
178 };
179
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
186 };
187
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()
192  */
193 void bnxt_vf_reps_close(struct bnxt *bp)
194 {
195         struct bnxt_vf_rep *vf_rep;
196         u16 num_vfs, i;
197
198         if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
199                 return;
200
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);
206         }
207 }
208
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()
213  */
214 void bnxt_vf_reps_open(struct bnxt *bp)
215 {
216         int i;
217
218         if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
219                 return;
220
221         for (i = 0; i < pci_num_vf(bp->pdev); i++)
222                 bnxt_vf_rep_open(bp->vf_reps[i]->dev);
223 }
224
225 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
226 {
227         u16 num_vfs = pci_num_vf(bp->pdev);
228         struct bnxt_vf_rep *vf_rep;
229         int i;
230
231         for (i = 0; i < num_vfs; i++) {
232                 vf_rep = bp->vf_reps[i];
233                 if (vf_rep) {
234                         dst_release((struct dst_entry *)vf_rep->dst);
235
236                         if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
237                                 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
238
239                         if (vf_rep->dev) {
240                                 /* if register_netdev failed, then netdev_ops
241                                  * would have been set to NULL
242                                  */
243                                 if (vf_rep->dev->netdev_ops)
244                                         unregister_netdev(vf_rep->dev);
245                                 free_netdev(vf_rep->dev);
246                         }
247                 }
248         }
249
250         kfree(bp->vf_reps);
251         bp->vf_reps = NULL;
252 }
253
254 void bnxt_vf_reps_destroy(struct bnxt *bp)
255 {
256         bool closed = false;
257
258         if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
259                 return;
260
261         if (!bp->vf_reps)
262                 return;
263
264         /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
265          * before proceeding with VF-rep cleanup.
266          */
267         rtnl_lock();
268         if (netif_running(bp->dev)) {
269                 bnxt_close_nic(bp, false, false);
270                 closed = true;
271         }
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;
276
277         if (closed)
278                 bnxt_open_nic(bp, false, false);
279         rtnl_unlock();
280
281         /* Need to call vf_reps_destroy() outside of rntl_lock
282          * as unregister_netdev takes rtnl_lock
283          */
284         __bnxt_vf_reps_destroy(bp);
285 }
286
287 /* Use the OUI of the PF's perm addr and report the same mac addr
288  * for the same VF-rep each time
289  */
290 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
291 {
292         u32 addr;
293
294         ether_addr_copy(mac, src_mac);
295
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);
300 }
301
302 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
303                                     struct net_device *dev)
304 {
305         struct net_device *pf_dev = bp->dev;
306
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
312          */
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,
319                                  dev->perm_addr);
320         ether_addr_copy(dev->dev_addr, dev->perm_addr);
321 }
322
323 static int bnxt_vf_reps_create(struct bnxt *bp)
324 {
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;
328         int rc, i;
329
330         bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
331         if (!bp->vf_reps)
332                 return -ENOMEM;
333
334         /* storage for cfa_code to vf-idx mapping */
335         cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
336                                GFP_KERNEL);
337         if (!cfa_code_map) {
338                 rc = -ENOMEM;
339                 goto err;
340         }
341         for (i = 0; i < MAX_CFA_CODE; i++)
342                 cfa_code_map[i] = VF_IDX_INVALID;
343
344         for (i = 0; i < num_vfs; i++) {
345                 dev = alloc_etherdev(sizeof(*vf_rep));
346                 if (!dev) {
347                         rc = -ENOMEM;
348                         goto err;
349                 }
350
351                 vf_rep = netdev_priv(dev);
352                 bp->vf_reps[i] = vf_rep;
353                 vf_rep->dev = dev;
354                 vf_rep->bp = bp;
355                 vf_rep->vf_idx = i;
356                 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
357
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);
362                 if (rc) {
363                         rc = -ENOLINK;
364                         goto err;
365                 }
366                 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
367
368                 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
369                                                  GFP_KERNEL);
370                 if (!vf_rep->dst) {
371                         rc = -ENOMEM;
372                         goto err;
373                 }
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;
377
378                 bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
379                 rc = register_netdev(dev);
380                 if (rc) {
381                         /* no need for unregister_netdev in cleanup */
382                         dev->netdev_ops = NULL;
383                         goto err;
384                 }
385         }
386
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);
391         return 0;
392
393 err:
394         netdev_info(bp->dev, "%s error=%d", __func__, rc);
395         kfree(cfa_code_map);
396         __bnxt_vf_reps_destroy(bp);
397         return rc;
398 }
399
400 /* Devlink related routines */
401 static int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
402 {
403         struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
404
405         *mode = bp->eswitch_mode;
406         return 0;
407 }
408
409 static int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
410 {
411         struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
412         int rc = 0;
413
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");
419                 rc = -EINVAL;
420                 goto done;
421         }
422
423         switch (mode) {
424         case DEVLINK_ESWITCH_MODE_LEGACY:
425                 bnxt_vf_reps_destroy(bp);
426                 break;
427
428         case DEVLINK_ESWITCH_MODE_SWITCHDEV:
429                 if (pci_num_vf(bp->pdev) == 0) {
430                         netdev_info(bp->dev,
431                                     "Enable VFs before setting swtichdev mode");
432                         rc = -EPERM;
433                         goto done;
434                 }
435                 rc = bnxt_vf_reps_create(bp);
436                 break;
437
438         default:
439                 rc = -EINVAL;
440                 goto done;
441         }
442 done:
443         mutex_unlock(&bp->sriov_lock);
444         return rc;
445 }
446
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
450 };
451
452 int bnxt_dl_register(struct bnxt *bp)
453 {
454         struct devlink *dl;
455         int rc;
456
457         if (!pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV))
458                 return 0;
459
460         if (bp->hwrm_spec_code < 0x10800) {
461                 netdev_warn(bp->dev, "Firmware does not support SR-IOV E-Switch SWITCHDEV mode.\n");
462                 return -ENOTSUPP;
463         }
464
465         dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
466         if (!dl) {
467                 netdev_warn(bp->dev, "devlink_alloc failed");
468                 return -ENOMEM;
469         }
470
471         bnxt_link_bp_to_dl(dl, bp);
472         bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
473         rc = devlink_register(dl, &bp->pdev->dev);
474         if (rc) {
475                 bnxt_link_bp_to_dl(dl, NULL);
476                 devlink_free(dl);
477                 netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
478                 return rc;
479         }
480
481         return 0;
482 }
483
484 void bnxt_dl_unregister(struct bnxt *bp)
485 {
486         struct devlink *dl = bp->dl;
487
488         if (!dl)
489                 return;
490
491         devlink_unregister(dl);
492         devlink_free(dl);
493 }
494
495 #endif