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