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