7ee7294143a295d3a790e73b1fff7e8e1e1fc6c8
[dpdk.git] / drivers / net / nfb / nfb_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Cesnet
3  * Copyright(c) 2019 Netcope Technologies, a.s. <info@netcope.com>
4  * All rights reserved.
5  */
6
7 #include <nfb/nfb.h>
8 #include <nfb/ndp.h>
9 #include <netcope/rxmac.h>
10 #include <netcope/txmac.h>
11
12 #include <rte_ethdev_pci.h>
13 #include <rte_kvargs.h>
14
15 #include "nfb_stats.h"
16 #include "nfb_rx.h"
17 #include "nfb_tx.h"
18 #include "nfb_rxmode.h"
19 #include "nfb.h"
20
21 /**
22  * Default MAC addr
23  */
24 static const struct rte_ether_addr eth_addr = {
25         .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 }
26 };
27
28 /**
29  * Open all RX DMA queues
30  *
31  * @param dev
32  *   Pointer to nfb device.
33  * @param[out] rxmac
34  *   Pointer to output array of nc_rxmac
35  * @param[out] max_rxmac
36  *   Pointer to output max index of rxmac
37  */
38 static void
39 nfb_nc_rxmac_init(struct nfb_device *nfb,
40         struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC],
41         uint16_t *max_rxmac)
42 {
43         *max_rxmac = 0;
44         while ((rxmac[*max_rxmac] = nc_rxmac_open_index(nfb, *max_rxmac)))
45                 ++(*max_rxmac);
46 }
47
48 /**
49  * Open all TX DMA queues
50  *
51  * @param dev
52  *   Pointer to nfb device.
53  * @param[out] txmac
54  *   Pointer to output array of nc_txmac
55  * @param[out] max_rxmac
56  *   Pointer to output max index of txmac
57  */
58 static void
59 nfb_nc_txmac_init(struct nfb_device *nfb,
60         struct nc_txmac *txmac[RTE_MAX_NC_TXMAC],
61         uint16_t *max_txmac)
62 {
63         *max_txmac = 0;
64         while ((txmac[*max_txmac] = nc_txmac_open_index(nfb, *max_txmac)))
65                 ++(*max_txmac);
66 }
67
68 /**
69  * Close all RX DMA queues
70  *
71  * @param rxmac
72  *   Pointer to array of nc_rxmac
73  * @param max_rxmac
74  *   Maximum index of rxmac
75  */
76 static void
77 nfb_nc_rxmac_deinit(struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC],
78         uint16_t max_rxmac)
79 {
80         for (; max_rxmac > 0; --max_rxmac) {
81                 nc_rxmac_close(rxmac[max_rxmac]);
82                 rxmac[max_rxmac] = NULL;
83         }
84 }
85
86 /**
87  * Close all TX DMA queues
88  *
89  * @param txmac
90  *   Pointer to array of nc_txmac
91  * @param max_txmac
92  *   Maximum index of txmac
93  */
94 static void
95 nfb_nc_txmac_deinit(struct nc_txmac *txmac[RTE_MAX_NC_TXMAC],
96         uint16_t max_txmac)
97 {
98         for (; max_txmac > 0; --max_txmac) {
99                 nc_txmac_close(txmac[max_txmac]);
100                 txmac[max_txmac] = NULL;
101         }
102 }
103
104 /**
105  * DPDK callback to start the device.
106  *
107  * Start device by starting all configured queues.
108  *
109  * @param dev
110  *   Pointer to Ethernet device structure.
111  *
112  * @return
113  *   0 on success, a negative errno value otherwise.
114  */
115 static int
116 nfb_eth_dev_start(struct rte_eth_dev *dev)
117 {
118         int ret;
119         uint16_t i;
120         uint16_t nb_rx = dev->data->nb_rx_queues;
121         uint16_t nb_tx = dev->data->nb_tx_queues;
122
123         for (i = 0; i < nb_rx; i++) {
124                 ret = nfb_eth_rx_queue_start(dev, i);
125                 if (ret != 0)
126                         goto err_rx;
127         }
128
129         for (i = 0; i < nb_tx; i++) {
130                 ret = nfb_eth_tx_queue_start(dev, i);
131                 if (ret != 0)
132                         goto err_tx;
133         }
134
135         return 0;
136
137 err_tx:
138         for (i = 0; i < nb_tx; i++)
139                 nfb_eth_tx_queue_stop(dev, i);
140 err_rx:
141         for (i = 0; i < nb_rx; i++)
142                 nfb_eth_rx_queue_stop(dev, i);
143         return ret;
144 }
145
146 /**
147  * DPDK callback to stop the device.
148  *
149  * Stop device by stopping all configured queues.
150  *
151  * @param dev
152  *   Pointer to Ethernet device structure.
153  */
154 static void
155 nfb_eth_dev_stop(struct rte_eth_dev *dev)
156 {
157         uint16_t i;
158         uint16_t nb_rx = dev->data->nb_rx_queues;
159         uint16_t nb_tx = dev->data->nb_tx_queues;
160
161         for (i = 0; i < nb_tx; i++)
162                 nfb_eth_tx_queue_stop(dev, i);
163
164         for (i = 0; i < nb_rx; i++)
165                 nfb_eth_rx_queue_stop(dev, i);
166 }
167
168 /**
169  * DPDK callback for Ethernet device configuration.
170  *
171  * @param dev
172  *   Pointer to Ethernet device structure.
173  *
174  * @return
175  *   0 on success, a negative errno value otherwise.
176  */
177 static int
178 nfb_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
179 {
180         return 0;
181 }
182
183 /**
184  * DPDK callback to get information about the device.
185  *
186  * @param dev
187  *   Pointer to Ethernet device structure.
188  * @param[out] info
189  *   Info structure output buffer.
190  */
191 static int
192 nfb_eth_dev_info(struct rte_eth_dev *dev,
193         struct rte_eth_dev_info *dev_info)
194 {
195         dev_info->max_mac_addrs = 1;
196         dev_info->max_rx_pktlen = (uint32_t)-1;
197         dev_info->max_rx_queues = dev->data->nb_rx_queues;
198         dev_info->max_tx_queues = dev->data->nb_tx_queues;
199         dev_info->speed_capa = ETH_LINK_SPEED_100G;
200
201         return 0;
202 }
203
204 /**
205  * DPDK callback to close the device.
206  *
207  * Destroy all queues and objects, free memory.
208  *
209  * @param dev
210  *   Pointer to Ethernet device structure.
211  */
212 static int
213 nfb_eth_dev_close(struct rte_eth_dev *dev)
214 {
215         struct pmd_internals *internals = dev->data->dev_private;
216         uint16_t i;
217         uint16_t nb_rx = dev->data->nb_rx_queues;
218         uint16_t nb_tx = dev->data->nb_tx_queues;
219
220         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
221                 return 0;
222
223         nfb_eth_dev_stop(dev);
224
225         nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac);
226         nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac);
227
228         for (i = 0; i < nb_rx; i++) {
229                 nfb_eth_rx_queue_release(dev->data->rx_queues[i]);
230                 dev->data->rx_queues[i] = NULL;
231         }
232         dev->data->nb_rx_queues = 0;
233         for (i = 0; i < nb_tx; i++) {
234                 nfb_eth_tx_queue_release(dev->data->tx_queues[i]);
235                 dev->data->tx_queues[i] = NULL;
236         }
237         dev->data->nb_tx_queues = 0;
238
239         rte_free(dev->data->mac_addrs);
240         dev->data->mac_addrs = NULL;
241
242         return 0;
243 }
244
245 /**
246  * DPDK callback to retrieve physical link information.
247  *
248  * @param dev
249  *   Pointer to Ethernet device structure.
250  * @param[out] link
251  *   Storage for current link status.
252  *
253  * @return
254  *   0 on success, a negative errno value otherwise.
255  */
256 static int
257 nfb_eth_link_update(struct rte_eth_dev *dev,
258         int wait_to_complete __rte_unused)
259 {
260         uint16_t i;
261         struct nc_rxmac_status status;
262         struct rte_eth_link link;
263         memset(&link, 0, sizeof(link));
264
265         struct pmd_internals *internals = dev->data->dev_private;
266
267         status.speed = MAC_SPEED_UNKNOWN;
268
269         link.link_speed = ETH_SPEED_NUM_NONE;
270         link.link_status = ETH_LINK_DOWN;
271         link.link_duplex = ETH_LINK_FULL_DUPLEX;
272         link.link_autoneg = ETH_LINK_SPEED_FIXED;
273
274         if (internals->rxmac[0] != NULL) {
275                 nc_rxmac_read_status(internals->rxmac[0], &status);
276
277                 switch (status.speed) {
278                 case MAC_SPEED_10G:
279                         link.link_speed = ETH_SPEED_NUM_10G;
280                         break;
281                 case MAC_SPEED_40G:
282                         link.link_speed = ETH_SPEED_NUM_40G;
283                         break;
284                 case MAC_SPEED_100G:
285                         link.link_speed = ETH_SPEED_NUM_100G;
286                         break;
287                 default:
288                         link.link_speed = ETH_SPEED_NUM_NONE;
289                         break;
290                 }
291         }
292
293         for (i = 0; i < internals->max_rxmac; ++i) {
294                 nc_rxmac_read_status(internals->rxmac[i], &status);
295
296                 if (status.enabled && status.link_up) {
297                         link.link_status = ETH_LINK_UP;
298                         break;
299                 }
300         }
301
302         rte_eth_linkstatus_set(dev, &link);
303
304         return 0;
305 }
306
307 /**
308  * DPDK callback to bring the link UP.
309  *
310  * @param dev
311  *   Pointer to Ethernet device structure.
312  *
313  * @return
314  *   0 on success, a negative errno value otherwise.
315  */
316 static int
317 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev)
318 {
319         struct pmd_internals *internals = (struct pmd_internals *)
320                 dev->data->dev_private;
321
322         uint16_t i;
323         for (i = 0; i < internals->max_rxmac; ++i)
324                 nc_rxmac_enable(internals->rxmac[i]);
325
326         for (i = 0; i < internals->max_txmac; ++i)
327                 nc_txmac_enable(internals->txmac[i]);
328
329         return 0;
330 }
331
332 /**
333  * DPDK callback to bring the link DOWN.
334  *
335  * @param dev
336  *   Pointer to Ethernet device structure.
337  *
338  * @return
339  *   0 on success, a negative errno value otherwise.
340  */
341 static int
342 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev)
343 {
344         struct pmd_internals *internals = (struct pmd_internals *)
345                 dev->data->dev_private;
346
347         uint16_t i;
348         for (i = 0; i < internals->max_rxmac; ++i)
349                 nc_rxmac_disable(internals->rxmac[i]);
350
351         for (i = 0; i < internals->max_txmac; ++i)
352                 nc_txmac_disable(internals->txmac[i]);
353
354         return 0;
355 }
356
357 /**
358  * DPDK callback to set primary MAC address.
359  *
360  * @param dev
361  *   Pointer to Ethernet device structure.
362  * @param mac_addr
363  *   MAC address to register.
364  *
365  * @return
366  *   0 on success, a negative errno value otherwise.
367  */
368 static int
369 nfb_eth_mac_addr_set(struct rte_eth_dev *dev,
370         struct rte_ether_addr *mac_addr)
371 {
372         unsigned int i;
373         uint64_t mac = 0;
374         struct rte_eth_dev_data *data = dev->data;
375         struct pmd_internals *internals = (struct pmd_internals *)
376                 data->dev_private;
377
378         if (!rte_is_valid_assigned_ether_addr(mac_addr))
379                 return -EINVAL;
380
381         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
382                 mac <<= 8;
383                 mac |= mac_addr->addr_bytes[i] & 0xFF;
384         }
385
386         for (i = 0; i < internals->max_rxmac; ++i)
387                 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1);
388
389         rte_ether_addr_copy(mac_addr, data->mac_addrs);
390         return 0;
391 }
392
393 static const struct eth_dev_ops ops = {
394         .dev_start = nfb_eth_dev_start,
395         .dev_stop = nfb_eth_dev_stop,
396         .dev_set_link_up = nfb_eth_dev_set_link_up,
397         .dev_set_link_down = nfb_eth_dev_set_link_down,
398         .dev_close = nfb_eth_dev_close,
399         .dev_configure = nfb_eth_dev_configure,
400         .dev_infos_get = nfb_eth_dev_info,
401         .promiscuous_enable = nfb_eth_promiscuous_enable,
402         .promiscuous_disable = nfb_eth_promiscuous_disable,
403         .allmulticast_enable = nfb_eth_allmulticast_enable,
404         .allmulticast_disable = nfb_eth_allmulticast_disable,
405         .rx_queue_start = nfb_eth_rx_queue_start,
406         .rx_queue_stop = nfb_eth_rx_queue_stop,
407         .tx_queue_start = nfb_eth_tx_queue_start,
408         .tx_queue_stop = nfb_eth_tx_queue_stop,
409         .rx_queue_setup = nfb_eth_rx_queue_setup,
410         .tx_queue_setup = nfb_eth_tx_queue_setup,
411         .rx_queue_release = nfb_eth_rx_queue_release,
412         .tx_queue_release = nfb_eth_tx_queue_release,
413         .link_update = nfb_eth_link_update,
414         .stats_get = nfb_eth_stats_get,
415         .stats_reset = nfb_eth_stats_reset,
416         .mac_addr_set = nfb_eth_mac_addr_set,
417 };
418
419 /**
420  * DPDK callback to initialize an ethernet device
421  *
422  * @param dev
423  *   Pointer to ethernet device structure
424  *
425  * @return
426  *   0 on success, a negative errno value otherwise.
427  */
428 static int
429 nfb_eth_dev_init(struct rte_eth_dev *dev)
430 {
431         struct rte_eth_dev_data *data = dev->data;
432         struct pmd_internals *internals = (struct pmd_internals *)
433                 data->dev_private;
434         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
435         struct rte_pci_addr *pci_addr = &pci_dev->addr;
436         struct rte_ether_addr eth_addr_init;
437         struct rte_kvargs *kvlist;
438
439         RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n",
440                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
441                 pci_addr->function);
442
443         snprintf(internals->nfb_dev, PATH_MAX,
444                 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT,
445                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
446                 pci_addr->function);
447
448         /* Check validity of device args */
449         if (dev->device->devargs != NULL &&
450                         dev->device->devargs->args != NULL &&
451                         strlen(dev->device->devargs->args) > 0) {
452                 kvlist = rte_kvargs_parse(dev->device->devargs->args,
453                                                 VALID_KEYS);
454                 if (kvlist == NULL) {
455                         RTE_LOG(ERR, PMD, "Failed to parse device arguments %s",
456                                 dev->device->devargs->args);
457                         rte_kvargs_free(kvlist);
458                         return -EINVAL;
459                 }
460                 rte_kvargs_free(kvlist);
461         }
462
463         /*
464          * Get number of available DMA RX and TX queues, which is maximum
465          * number of queues that can be created and store it in private device
466          * data structure.
467          */
468         internals->nfb = nfb_open(internals->nfb_dev);
469         if (internals->nfb == NULL) {
470                 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s",
471                         internals->nfb_dev);
472                 return -EINVAL;
473         }
474         data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb);
475         data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb);
476
477         RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n",
478                 data->nb_rx_queues, data->nb_tx_queues);
479
480         nfb_nc_rxmac_init(internals->nfb,
481                 internals->rxmac,
482                 &internals->max_rxmac);
483         nfb_nc_txmac_init(internals->nfb,
484                 internals->txmac,
485                 &internals->max_txmac);
486
487         /* Set rx, tx burst functions */
488         dev->rx_pkt_burst = nfb_eth_ndp_rx;
489         dev->tx_pkt_burst = nfb_eth_ndp_tx;
490
491         /* Set function callbacks for Ethernet API */
492         dev->dev_ops = &ops;
493
494         /* Get link state */
495         nfb_eth_link_update(dev, 0);
496
497         /* Allocate space for one mac address */
498         data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr),
499                 RTE_CACHE_LINE_SIZE);
500         if (data->mac_addrs == NULL) {
501                 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
502                 nfb_close(internals->nfb);
503                 return -EINVAL;
504         }
505
506         rte_eth_random_addr(eth_addr_init.addr_bytes);
507         eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0];
508         eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1];
509         eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2];
510
511         nfb_eth_mac_addr_set(dev, &eth_addr_init);
512
513         data->promiscuous = nfb_eth_promiscuous_get(dev);
514         data->all_multicast = nfb_eth_allmulticast_get(dev);
515         internals->rx_filter_original = data->promiscuous;
516
517         RTE_LOG(INFO, PMD, "NFB device ("
518                 PCI_PRI_FMT ") successfully initialized\n",
519                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
520                 pci_addr->function);
521
522         return 0;
523 }
524
525 /**
526  * DPDK callback to uninitialize an ethernet device
527  *
528  * @param dev
529  *   Pointer to ethernet device structure
530  *
531  * @return
532  *   0 on success, a negative errno value otherwise.
533  */
534 static int
535 nfb_eth_dev_uninit(struct rte_eth_dev *dev)
536 {
537         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
538         struct rte_pci_addr *pci_addr = &pci_dev->addr;
539
540         nfb_eth_dev_close(dev);
541
542         RTE_LOG(INFO, PMD, "NFB device ("
543                 PCI_PRI_FMT ") successfully uninitialized\n",
544                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
545                 pci_addr->function);
546
547         return 0;
548 }
549
550 static const struct rte_pci_id nfb_pci_id_table[] = {
551         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) },
552         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) },
553         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) },
554         { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) },
555         { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) },
556         { .vendor_id = 0, }
557 };
558
559 /**
560  * DPDK callback to register a PCI device.
561  *
562  * This function spawns Ethernet devices out of a given PCI device.
563  *
564  * @param[in] pci_drv
565  *   PCI driver structure (nfb_driver).
566  * @param[in] pci_dev
567  *   PCI device information.
568  *
569  * @return
570  *   0 on success, a negative errno value otherwise.
571  */
572 static int
573 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
574                 struct rte_pci_device *pci_dev)
575 {
576         return rte_eth_dev_pci_generic_probe(pci_dev,
577                 sizeof(struct pmd_internals), nfb_eth_dev_init);
578 }
579
580 /**
581  * DPDK callback to remove a PCI device.
582  *
583  * This function removes all Ethernet devices belong to a given PCI device.
584  *
585  * @param[in] pci_dev
586  *   Pointer to the PCI device.
587  *
588  * @return
589  *   0 on success, the function cannot fail.
590  */
591 static int
592 nfb_eth_pci_remove(struct rte_pci_device *pci_dev)
593 {
594         return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit);
595 }
596
597 static struct rte_pci_driver nfb_eth_driver = {
598         .id_table = nfb_pci_id_table,
599         .probe = nfb_eth_pci_probe,
600         .remove = nfb_eth_pci_remove,
601 };
602
603 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver);
604 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table);
605 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb");
606 RTE_PMD_REGISTER_PARAM_STRING(RTE_NFB_DRIVER_NAME, TIMESTAMP_ARG "=<0|1>");