net/nfb: support timestamp
[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 void
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
202 /**
203  * DPDK callback to close the device.
204  *
205  * Destroy all queues and objects, free memory.
206  *
207  * @param dev
208  *   Pointer to Ethernet device structure.
209  */
210 static void
211 nfb_eth_dev_close(struct rte_eth_dev *dev)
212 {
213         uint16_t i;
214         uint16_t nb_rx = dev->data->nb_rx_queues;
215         uint16_t nb_tx = dev->data->nb_tx_queues;
216
217         nfb_eth_dev_stop(dev);
218
219         for (i = 0; i < nb_rx; i++) {
220                 nfb_eth_rx_queue_release(dev->data->rx_queues[i]);
221                 dev->data->rx_queues[i] = NULL;
222         }
223         dev->data->nb_rx_queues = 0;
224         for (i = 0; i < nb_tx; i++) {
225                 nfb_eth_tx_queue_release(dev->data->tx_queues[i]);
226                 dev->data->tx_queues[i] = NULL;
227         }
228         dev->data->nb_tx_queues = 0;
229 }
230
231 /**
232  * DPDK callback to retrieve physical link information.
233  *
234  * @param dev
235  *   Pointer to Ethernet device structure.
236  * @param[out] link
237  *   Storage for current link status.
238  *
239  * @return
240  *   0 on success, a negative errno value otherwise.
241  */
242 static int
243 nfb_eth_link_update(struct rte_eth_dev *dev,
244         int wait_to_complete __rte_unused)
245 {
246         uint16_t i;
247         struct nc_rxmac_status status;
248         struct rte_eth_link link;
249         memset(&link, 0, sizeof(link));
250
251         struct pmd_internals *internals = dev->data->dev_private;
252
253         status.speed = MAC_SPEED_UNKNOWN;
254
255         link.link_speed = ETH_SPEED_NUM_NONE;
256         link.link_status = ETH_LINK_DOWN;
257         link.link_duplex = ETH_LINK_FULL_DUPLEX;
258         link.link_autoneg = ETH_LINK_SPEED_FIXED;
259
260         if (internals->rxmac[0] != NULL) {
261                 nc_rxmac_read_status(internals->rxmac[0], &status);
262
263                 switch (status.speed) {
264                 case MAC_SPEED_10G:
265                         link.link_speed = ETH_SPEED_NUM_10G;
266                         break;
267                 case MAC_SPEED_40G:
268                         link.link_speed = ETH_SPEED_NUM_40G;
269                         break;
270                 case MAC_SPEED_100G:
271                         link.link_speed = ETH_SPEED_NUM_100G;
272                         break;
273                 default:
274                         link.link_speed = ETH_SPEED_NUM_NONE;
275                         break;
276                 }
277         }
278
279         for (i = 0; i < internals->max_rxmac; ++i) {
280                 nc_rxmac_read_status(internals->rxmac[i], &status);
281
282                 if (status.enabled && status.link_up) {
283                         link.link_status = ETH_LINK_UP;
284                         break;
285                 }
286         }
287
288         rte_eth_linkstatus_set(dev, &link);
289
290         return 0;
291 }
292
293 /**
294  * DPDK callback to bring the link UP.
295  *
296  * @param dev
297  *   Pointer to Ethernet device structure.
298  *
299  * @return
300  *   0 on success, a negative errno value otherwise.
301  */
302 static int
303 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev)
304 {
305         struct pmd_internals *internals = (struct pmd_internals *)
306                 dev->data->dev_private;
307
308         uint16_t i;
309         for (i = 0; i < internals->max_rxmac; ++i)
310                 nc_rxmac_enable(internals->rxmac[i]);
311
312         for (i = 0; i < internals->max_txmac; ++i)
313                 nc_txmac_enable(internals->txmac[i]);
314
315         return 0;
316 }
317
318 /**
319  * DPDK callback to bring the link DOWN.
320  *
321  * @param dev
322  *   Pointer to Ethernet device structure.
323  *
324  * @return
325  *   0 on success, a negative errno value otherwise.
326  */
327 static int
328 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev)
329 {
330         struct pmd_internals *internals = (struct pmd_internals *)
331                 dev->data->dev_private;
332
333         uint16_t i;
334         for (i = 0; i < internals->max_rxmac; ++i)
335                 nc_rxmac_disable(internals->rxmac[i]);
336
337         for (i = 0; i < internals->max_txmac; ++i)
338                 nc_txmac_disable(internals->txmac[i]);
339
340         return 0;
341 }
342
343 /**
344  * DPDK callback to set primary MAC address.
345  *
346  * @param dev
347  *   Pointer to Ethernet device structure.
348  * @param mac_addr
349  *   MAC address to register.
350  *
351  * @return
352  *   0 on success, a negative errno value otherwise.
353  */
354 static int
355 nfb_eth_mac_addr_set(struct rte_eth_dev *dev,
356         struct rte_ether_addr *mac_addr)
357 {
358         unsigned int i;
359         uint64_t mac = 0;
360         struct rte_eth_dev_data *data = dev->data;
361         struct pmd_internals *internals = (struct pmd_internals *)
362                 data->dev_private;
363
364         if (!rte_is_valid_assigned_ether_addr(mac_addr))
365                 return -EINVAL;
366
367         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
368                 mac <<= 8;
369                 mac |= mac_addr->addr_bytes[i] & 0xFF;
370         }
371
372         for (i = 0; i < internals->max_rxmac; ++i)
373                 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1);
374
375         rte_ether_addr_copy(mac_addr, data->mac_addrs);
376         return 0;
377 }
378
379 static const struct eth_dev_ops ops = {
380         .dev_start = nfb_eth_dev_start,
381         .dev_stop = nfb_eth_dev_stop,
382         .dev_set_link_up = nfb_eth_dev_set_link_up,
383         .dev_set_link_down = nfb_eth_dev_set_link_down,
384         .dev_close = nfb_eth_dev_close,
385         .dev_configure = nfb_eth_dev_configure,
386         .dev_infos_get = nfb_eth_dev_info,
387         .promiscuous_enable = nfb_eth_promiscuous_enable,
388         .promiscuous_disable = nfb_eth_promiscuous_disable,
389         .allmulticast_enable = nfb_eth_allmulticast_enable,
390         .allmulticast_disable = nfb_eth_allmulticast_disable,
391         .rx_queue_start = nfb_eth_rx_queue_start,
392         .rx_queue_stop = nfb_eth_rx_queue_stop,
393         .tx_queue_start = nfb_eth_tx_queue_start,
394         .tx_queue_stop = nfb_eth_tx_queue_stop,
395         .rx_queue_setup = nfb_eth_rx_queue_setup,
396         .tx_queue_setup = nfb_eth_tx_queue_setup,
397         .rx_queue_release = nfb_eth_rx_queue_release,
398         .tx_queue_release = nfb_eth_tx_queue_release,
399         .link_update = nfb_eth_link_update,
400         .stats_get = nfb_eth_stats_get,
401         .stats_reset = nfb_eth_stats_reset,
402         .mac_addr_set = nfb_eth_mac_addr_set,
403 };
404
405 /**
406  * DPDK callback to initialize an ethernet device
407  *
408  * @param dev
409  *   Pointer to ethernet device structure
410  *
411  * @return
412  *   0 on success, a negative errno value otherwise.
413  */
414 static int
415 nfb_eth_dev_init(struct rte_eth_dev *dev)
416 {
417         struct rte_eth_dev_data *data = dev->data;
418         struct pmd_internals *internals = (struct pmd_internals *)
419                 data->dev_private;
420         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
421         struct rte_pci_addr *pci_addr = &pci_dev->addr;
422         struct rte_ether_addr eth_addr_init;
423         struct rte_kvargs *kvlist;
424
425         RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n",
426                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
427                 pci_addr->function);
428
429         snprintf(internals->nfb_dev, PATH_MAX,
430                 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT,
431                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
432                 pci_addr->function);
433
434         /* Check validity of device args */
435         if (dev->device->devargs != NULL &&
436                         dev->device->devargs->args != NULL &&
437                         strlen(dev->device->devargs->args) > 0) {
438                 kvlist = rte_kvargs_parse(dev->device->devargs->args,
439                                                 VALID_KEYS);
440                 if (kvlist == NULL) {
441                         RTE_LOG(ERR, PMD, "Failed to parse device arguments %s",
442                                 dev->device->devargs->args);
443                         rte_kvargs_free(kvlist);
444                         return -EINVAL;
445                 }
446                 rte_kvargs_free(kvlist);
447         }
448
449         /*
450          * Get number of available DMA RX and TX queues, which is maximum
451          * number of queues that can be created and store it in private device
452          * data structure.
453          */
454         internals->nfb = nfb_open(internals->nfb_dev);
455         if (internals->nfb == NULL) {
456                 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s",
457                         internals->nfb_dev);
458                 return -EINVAL;
459         }
460         data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb);
461         data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb);
462
463         RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n",
464                 data->nb_rx_queues, data->nb_tx_queues);
465
466         nfb_nc_rxmac_init(internals->nfb,
467                 internals->rxmac,
468                 &internals->max_rxmac);
469         nfb_nc_txmac_init(internals->nfb,
470                 internals->txmac,
471                 &internals->max_txmac);
472
473         /* Set rx, tx burst functions */
474         dev->rx_pkt_burst = nfb_eth_ndp_rx;
475         dev->tx_pkt_burst = nfb_eth_ndp_tx;
476
477         /* Set function callbacks for Ethernet API */
478         dev->dev_ops = &ops;
479
480         /* Get link state */
481         nfb_eth_link_update(dev, 0);
482
483         /* Allocate space for one mac address */
484         data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr),
485                 RTE_CACHE_LINE_SIZE);
486         if (data->mac_addrs == NULL) {
487                 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
488                 nfb_close(internals->nfb);
489                 return -EINVAL;
490         }
491
492         rte_eth_random_addr(eth_addr_init.addr_bytes);
493         eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0];
494         eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1];
495         eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2];
496
497         nfb_eth_mac_addr_set(dev, &eth_addr_init);
498
499         data->promiscuous = nfb_eth_promiscuous_get(dev);
500         data->all_multicast = nfb_eth_allmulticast_get(dev);
501         internals->rx_filter_original = data->promiscuous;
502
503         RTE_LOG(INFO, PMD, "NFB device ("
504                 PCI_PRI_FMT ") successfully initialized\n",
505                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
506                 pci_addr->function);
507
508         return 0;
509 }
510
511 /**
512  * DPDK callback to uninitialize an ethernet device
513  *
514  * @param dev
515  *   Pointer to ethernet device structure
516  *
517  * @return
518  *   0 on success, a negative errno value otherwise.
519  */
520 static int
521 nfb_eth_dev_uninit(struct rte_eth_dev *dev)
522 {
523         struct rte_eth_dev_data *data = dev->data;
524         struct pmd_internals *internals = (struct pmd_internals *)
525                 data->dev_private;
526
527         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
528         struct rte_pci_addr *pci_addr = &pci_dev->addr;
529
530         nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac);
531         nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac);
532
533         RTE_LOG(INFO, PMD, "NFB device ("
534                 PCI_PRI_FMT ") successfully uninitialized\n",
535                 pci_addr->domain, pci_addr->bus, pci_addr->devid,
536                 pci_addr->function);
537
538         return 0;
539 }
540
541 static const struct rte_pci_id nfb_pci_id_table[] = {
542         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) },
543         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) },
544         { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) },
545         { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) },
546         { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) },
547         { .vendor_id = 0, }
548 };
549
550 /**
551  * DPDK callback to register a PCI device.
552  *
553  * This function spawns Ethernet devices out of a given PCI device.
554  *
555  * @param[in] pci_drv
556  *   PCI driver structure (nfb_driver).
557  * @param[in] pci_dev
558  *   PCI device information.
559  *
560  * @return
561  *   0 on success, a negative errno value otherwise.
562  */
563 static int
564 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
565                 struct rte_pci_device *pci_dev)
566 {
567         return rte_eth_dev_pci_generic_probe(pci_dev,
568                 sizeof(struct pmd_internals), nfb_eth_dev_init);
569 }
570
571 /**
572  * DPDK callback to remove a PCI device.
573  *
574  * This function removes all Ethernet devices belong to a given PCI device.
575  *
576  * @param[in] pci_dev
577  *   Pointer to the PCI device.
578  *
579  * @return
580  *   0 on success, the function cannot fail.
581  */
582 static int
583 nfb_eth_pci_remove(struct rte_pci_device *pci_dev)
584 {
585         return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit);
586 }
587
588 static struct rte_pci_driver nfb_eth_driver = {
589         .id_table = nfb_pci_id_table,
590         .probe = nfb_eth_pci_probe,
591         .remove = nfb_eth_pci_remove,
592 };
593
594 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver);
595 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table);
596 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb");
597 RTE_PMD_REGISTER_PARAM_STRING(RTE_NFB_DRIVER_NAME, TIMESTAMP_ARG "=<0|1>");