146b249cffb07541efafc4a7374af459fbafdf98
[dpdk.git] / drivers / net / mvneta / mvneta_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Marvell International Ltd.
3  * Copyright(c) 2018 Semihalf.
4  * All rights reserved.
5  */
6
7 #include <rte_ethdev_driver.h>
8 #include <rte_kvargs.h>
9 #include <rte_bus_vdev.h>
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <linux/ethtool.h>
14 #include <linux/sockios.h>
15 #include <net/if.h>
16 #include <net/if_arp.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <rte_mvep_common.h>
23
24 #include "mvneta_rxtx.h"
25
26
27 #define MVNETA_IFACE_NAME_ARG "iface"
28
29 #define MVNETA_RX_OFFLOADS (DEV_RX_OFFLOAD_JUMBO_FRAME | \
30                           DEV_RX_OFFLOAD_CHECKSUM)
31
32 /** Port Tx offloads capabilities */
33 #define MVNETA_TX_OFFLOADS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
34                           DEV_TX_OFFLOAD_UDP_CKSUM | \
35                           DEV_TX_OFFLOAD_TCP_CKSUM | \
36                           DEV_TX_OFFLOAD_MULTI_SEGS)
37
38 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
39 #define MVNETA_DEFAULT_MTU 1500
40
41 #define MVNETA_MAC_ADDRS_MAX 256 /*16 UC, 256 IP, 256 MC/BC */
42 /** Maximum length of a match string */
43 #define MVNETA_MATCH_LEN 16
44
45 int mvneta_logtype;
46
47 static const char * const valid_args[] = {
48         MVNETA_IFACE_NAME_ARG,
49         NULL
50 };
51
52 struct mvneta_ifnames {
53         const char *names[NETA_NUM_ETH_PPIO];
54         int idx;
55 };
56
57 static int mvneta_dev_num;
58
59 /**
60  * Deinitialize packet processor.
61  */
62 static void
63 mvneta_neta_deinit(void)
64 {
65         neta_deinit();
66 }
67
68 /**
69  * Initialize packet processor.
70  *
71  * @return
72  *   0 on success, negative error value otherwise.
73  */
74 static int
75 mvneta_neta_init(void)
76 {
77         return neta_init();
78 }
79
80 /**
81  * Callback used by rte_kvargs_process() during argument parsing.
82  *
83  * @param key
84  *   Pointer to the parsed key (unused).
85  * @param value
86  *   Pointer to the parsed value.
87  * @param extra_args
88  *   Pointer to the extra arguments which contains address of the
89  *   table of pointers to parsed interface names.
90  *
91  * @return
92  *   Always 0.
93  */
94 static int
95 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
96                  void *extra_args)
97 {
98         struct mvneta_ifnames *ifnames = extra_args;
99
100         ifnames->names[ifnames->idx++] = value;
101
102         return 0;
103 }
104
105 /**
106  * Ethernet device configuration.
107  *
108  * Prepare the driver for a given number of TX and RX queues and
109  * configure RSS if supported.
110  *
111  * @param dev
112  *   Pointer to Ethernet device structure.
113  *
114  * @return
115  *   0 on success, negative error value otherwise.
116  */
117 static int
118 mvneta_dev_configure(struct rte_eth_dev *dev)
119 {
120         struct mvneta_priv *priv = dev->data->dev_private;
121         struct neta_ppio_params *ppio_params;
122
123         if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
124                 MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
125                         dev->data->dev_conf.rxmode.mq_mode);
126                 if (dev->data->nb_rx_queues > 1)
127                         return -EINVAL;
128         }
129
130         if (dev->data->dev_conf.rxmode.split_hdr_size) {
131                 MVNETA_LOG(INFO, "Split headers not supported");
132                 return -EINVAL;
133         }
134
135         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
136                 dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
137                                  MRVL_NETA_ETH_HDRS_LEN;
138
139         if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
140                 priv->multiseg = 1;
141
142         ppio_params = &priv->ppio_params;
143         ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
144         /* Default: 1 TC, no QoS supported. */
145         ppio_params->inqs_params.num_tcs = 1;
146         ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
147         priv->ppio_id = dev->data->port_id;
148
149         return 0;
150 }
151
152 /**
153  * DPDK callback to get information about the device.
154  *
155  * @param dev
156  *   Pointer to Ethernet device structure (unused).
157  * @param info
158  *   Info structure output buffer.
159  */
160 static void
161 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
162                    struct rte_eth_dev_info *info)
163 {
164         info->speed_capa = ETH_LINK_SPEED_10M |
165                            ETH_LINK_SPEED_100M |
166                            ETH_LINK_SPEED_1G |
167                            ETH_LINK_SPEED_2_5G;
168
169         info->max_rx_queues = MRVL_NETA_RXQ_MAX;
170         info->max_tx_queues = MRVL_NETA_TXQ_MAX;
171         info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;
172
173         info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
174         info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
175         info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;
176
177         info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
178         info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
179         info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;
180
181         info->rx_offload_capa = MVNETA_RX_OFFLOADS;
182         info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
183
184         info->tx_offload_capa =  MVNETA_TX_OFFLOADS;
185         info->tx_queue_offload_capa =  MVNETA_TX_OFFLOADS;
186
187         /* By default packets are dropped if no descriptors are available */
188         info->default_rxconf.rx_drop_en = 1;
189         /* Deferred tx queue start is not supported */
190         info->default_txconf.tx_deferred_start = 0;
191         info->default_txconf.offloads = 0;
192
193         info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
194 }
195
196 /**
197  * Return supported packet types.
198  *
199  * @param dev
200  *   Pointer to Ethernet device structure (unused).
201  *
202  * @return
203  *   Const pointer to the table with supported packet types.
204  */
205 static const uint32_t *
206 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
207 {
208         static const uint32_t ptypes[] = {
209                 RTE_PTYPE_L2_ETHER,
210                 RTE_PTYPE_L2_ETHER_VLAN,
211                 RTE_PTYPE_L3_IPV4,
212                 RTE_PTYPE_L3_IPV6,
213                 RTE_PTYPE_L4_TCP,
214                 RTE_PTYPE_L4_UDP
215         };
216
217         return ptypes;
218 }
219
220 /**
221  * DPDK callback to bring the link up.
222  *
223  * @param dev
224  *   Pointer to Ethernet device structure.
225  *
226  * @return
227  *   0 on success, negative error value otherwise.
228  */
229 static int
230 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
231 {
232         struct mvneta_priv *priv = dev->data->dev_private;
233
234         if (!priv->ppio)
235                 return 0;
236
237         return neta_ppio_enable(priv->ppio);
238 }
239
240 /**
241  * DPDK callback to bring the link down.
242  *
243  * @param dev
244  *   Pointer to Ethernet device structure.
245  *
246  * @return
247  *   0 on success, negative error value otherwise.
248  */
249 static int
250 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
251 {
252         struct mvneta_priv *priv = dev->data->dev_private;
253
254         if (!priv->ppio)
255                 return 0;
256
257         return neta_ppio_disable(priv->ppio);
258 }
259
260 /**
261  * DPDK callback to start the device.
262  *
263  * @param dev
264  *   Pointer to Ethernet device structure.
265  *
266  * @return
267  *   0 on success, negative errno value on failure.
268  */
269 static int
270 mvneta_dev_start(struct rte_eth_dev *dev)
271 {
272         struct mvneta_priv *priv = dev->data->dev_private;
273         char match[MVNETA_MATCH_LEN];
274         int ret = 0, i;
275
276         if (priv->ppio)
277                 return mvneta_dev_set_link_up(dev);
278
279         snprintf(match, sizeof(match), "%s", dev->data->name);
280         priv->ppio_params.match = match;
281         priv->ppio_params.inqs_params.mtu = dev->data->mtu;
282
283         ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
284         if (ret) {
285                 MVNETA_LOG(ERR, "Failed to init ppio");
286                 return ret;
287         }
288         priv->ppio_id = priv->ppio->port_id;
289
290         /*
291          * In case there are some some stale uc/mc mac addresses flush them
292          * here. It cannot be done during mvneta_dev_close() as port information
293          * is already gone at that point (due to neta_ppio_deinit() in
294          * mvneta_dev_stop()).
295          */
296         if (!priv->uc_mc_flushed) {
297                 ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
298                 if (ret) {
299                         MVNETA_LOG(ERR,
300                                 "Failed to flush uc/mc filter list");
301                         goto out;
302                 }
303                 priv->uc_mc_flushed = 1;
304         }
305
306         ret = mvneta_alloc_rx_bufs(dev);
307         if (ret)
308                 goto out;
309
310         ret = mvneta_dev_set_link_up(dev);
311         if (ret) {
312                 MVNETA_LOG(ERR, "Failed to set link up");
313                 goto out;
314         }
315
316         /* start tx queues */
317         for (i = 0; i < dev->data->nb_tx_queues; i++)
318                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
319
320         mvneta_set_tx_function(dev);
321
322         return 0;
323
324 out:
325         MVNETA_LOG(ERR, "Failed to start device");
326         neta_ppio_deinit(priv->ppio);
327         return ret;
328 }
329
330 /**
331  * DPDK callback to stop the device.
332  *
333  * @param dev
334  *   Pointer to Ethernet device structure.
335  */
336 static void
337 mvneta_dev_stop(struct rte_eth_dev *dev)
338 {
339         struct mvneta_priv *priv = dev->data->dev_private;
340
341         if (!priv->ppio)
342                 return;
343
344         mvneta_dev_set_link_down(dev);
345         mvneta_flush_queues(dev);
346         neta_ppio_deinit(priv->ppio);
347
348         priv->ppio = NULL;
349 }
350
351 /**
352  * DPDK callback to close the device.
353  *
354  * @param dev
355  *   Pointer to Ethernet device structure.
356  */
357 static void
358 mvneta_dev_close(struct rte_eth_dev *dev)
359 {
360         struct mvneta_priv *priv = dev->data->dev_private;
361         int i;
362
363         if (priv->ppio)
364                 mvneta_dev_stop(dev);
365
366         for (i = 0; i < dev->data->nb_rx_queues; i++) {
367                 mvneta_rx_queue_release(dev->data->rx_queues[i]);
368                 dev->data->rx_queues[i] = NULL;
369         }
370
371         for (i = 0; i < dev->data->nb_tx_queues; i++) {
372                 mvneta_tx_queue_release(dev->data->tx_queues[i]);
373                 dev->data->tx_queues[i] = NULL;
374         }
375 }
376
377 /**
378  * DPDK callback to set the primary MAC address.
379  *
380  * @param dev
381  *   Pointer to Ethernet device structure.
382  * @param mac_addr
383  *   MAC address to register.
384  */
385 static int
386 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
387 {
388         struct mvneta_priv *priv = dev->data->dev_private;
389         int ret;
390
391         if (!priv->ppio)
392                 return -EINVAL;
393
394         ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
395         if (ret) {
396                 char buf[ETHER_ADDR_FMT_SIZE];
397                 ether_format_addr(buf, sizeof(buf), mac_addr);
398                 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
399         }
400         return 0;
401 }
402
403 static const struct eth_dev_ops mvneta_ops = {
404         .dev_configure = mvneta_dev_configure,
405         .dev_start = mvneta_dev_start,
406         .dev_stop = mvneta_dev_stop,
407         .dev_set_link_up = mvneta_dev_set_link_up,
408         .dev_set_link_down = mvneta_dev_set_link_down,
409         .dev_close = mvneta_dev_close,
410         .mac_addr_set = mvneta_mac_addr_set,
411         .dev_infos_get = mvneta_dev_infos_get,
412         .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
413         .rxq_info_get = mvneta_rxq_info_get,
414         .txq_info_get = mvneta_txq_info_get,
415         .rx_queue_setup = mvneta_rx_queue_setup,
416         .rx_queue_release = mvneta_rx_queue_release,
417         .tx_queue_setup = mvneta_tx_queue_setup,
418         .tx_queue_release = mvneta_tx_queue_release,
419 };
420
421 /**
422  * Create device representing Ethernet port.
423  *
424  * @param name
425  *   Pointer to the port's name.
426  *
427  * @return
428  *   0 on success, negative error value otherwise.
429  */
430 static int
431 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
432 {
433         int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
434         struct rte_eth_dev *eth_dev;
435         struct mvneta_priv *priv;
436         struct ifreq req;
437
438         eth_dev = rte_eth_dev_allocate(name);
439         if (!eth_dev)
440                 return -ENOMEM;
441
442         priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
443         if (!priv) {
444                 ret = -ENOMEM;
445                 goto out_free_dev;
446         }
447
448         eth_dev->data->mac_addrs =
449                 rte_zmalloc("mac_addrs",
450                             ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
451         if (!eth_dev->data->mac_addrs) {
452                 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
453                 ret = -ENOMEM;
454                 goto out_free_priv;
455         }
456
457         memset(&req, 0, sizeof(req));
458         strcpy(req.ifr_name, name);
459         ret = ioctl(fd, SIOCGIFHWADDR, &req);
460         if (ret)
461                 goto out_free_mac;
462
463         memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
464                req.ifr_addr.sa_data, ETHER_ADDR_LEN);
465
466         eth_dev->data->kdrv = RTE_KDRV_NONE;
467         eth_dev->data->dev_private = priv;
468         eth_dev->device = &vdev->device;
469         eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
470         mvneta_set_tx_function(eth_dev);
471         eth_dev->dev_ops = &mvneta_ops;
472
473         rte_eth_dev_probing_finish(eth_dev);
474         return 0;
475 out_free_mac:
476         rte_free(eth_dev->data->mac_addrs);
477 out_free_priv:
478         rte_free(priv);
479 out_free_dev:
480         rte_eth_dev_release_port(eth_dev);
481
482         return ret;
483 }
484
485 /**
486  * Cleanup previously created device representing Ethernet port.
487  *
488  * @param eth_dev
489  *   Pointer to the corresponding rte_eth_dev structure.
490  */
491 static void
492 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
493 {
494         rte_free(eth_dev->data->dev_private);
495         rte_free(eth_dev->data->mac_addrs);
496         rte_eth_dev_release_port(eth_dev);
497 }
498
499 /**
500  * Cleanup previously created device representing Ethernet port.
501  *
502  * @param name
503  *   Pointer to the port name.
504  */
505 static void
506 mvneta_eth_dev_destroy_name(const char *name)
507 {
508         struct rte_eth_dev *eth_dev;
509
510         eth_dev = rte_eth_dev_allocated(name);
511         if (!eth_dev)
512                 return;
513
514         mvneta_eth_dev_destroy(eth_dev);
515 }
516
517 /**
518  * DPDK callback to register the virtual device.
519  *
520  * @param vdev
521  *   Pointer to the virtual device.
522  *
523  * @return
524  *   0 on success, negative error value otherwise.
525  */
526 static int
527 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
528 {
529         struct rte_kvargs *kvlist;
530         struct mvneta_ifnames ifnames;
531         int ret = -EINVAL;
532         uint32_t i, ifnum;
533         const char *params;
534
535         params = rte_vdev_device_args(vdev);
536         if (!params)
537                 return -EINVAL;
538
539         kvlist = rte_kvargs_parse(params, valid_args);
540         if (!kvlist)
541                 return -EINVAL;
542
543         ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
544         if (ifnum > RTE_DIM(ifnames.names))
545                 goto out_free_kvlist;
546
547         ifnames.idx = 0;
548         rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
549                            mvneta_ifnames_get, &ifnames);
550
551         /*
552          * The below system initialization should be done only once,
553          * on the first provided configuration file
554          */
555         if (mvneta_dev_num)
556                 goto init_devices;
557
558         MVNETA_LOG(INFO, "Perform MUSDK initializations");
559
560         ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
561         if (ret)
562                 goto out_free_kvlist;
563
564         ret = mvneta_neta_init();
565         if (ret) {
566                 MVNETA_LOG(ERR, "Failed to init NETA!");
567                 rte_mvep_deinit(MVEP_MOD_T_NETA);
568                 goto out_free_kvlist;
569         }
570
571 init_devices:
572         for (i = 0; i < ifnum; i++) {
573                 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
574                 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
575                 if (ret)
576                         goto out_cleanup;
577         }
578         mvneta_dev_num += ifnum;
579
580         rte_kvargs_free(kvlist);
581
582         return 0;
583 out_cleanup:
584         for (; i > 0; i--)
585                 mvneta_eth_dev_destroy_name(ifnames.names[i]);
586
587         if (mvneta_dev_num == 0) {
588                 mvneta_neta_deinit();
589                 rte_mvep_deinit(MVEP_MOD_T_NETA);
590         }
591 out_free_kvlist:
592         rte_kvargs_free(kvlist);
593
594         return ret;
595 }
596
597 /**
598  * DPDK callback to remove virtual device.
599  *
600  * @param vdev
601  *   Pointer to the removed virtual device.
602  *
603  * @return
604  *   0 on success, negative error value otherwise.
605  */
606 static int
607 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
608 {
609         int i;
610         const char *name;
611
612         name = rte_vdev_device_name(vdev);
613         if (!name)
614                 return -EINVAL;
615
616         MVNETA_LOG(INFO, "Removing %s", name);
617
618         RTE_ETH_FOREACH_DEV(i) {
619                 if (rte_eth_devices[i].device != &vdev->device)
620                         continue;
621
622                 mvneta_eth_dev_destroy(&rte_eth_devices[i]);
623                 mvneta_dev_num--;
624         }
625
626         if (mvneta_dev_num == 0) {
627                 MVNETA_LOG(INFO, "Perform MUSDK deinit");
628                 mvneta_neta_deinit();
629                 rte_mvep_deinit(MVEP_MOD_T_NETA);
630         }
631
632         return 0;
633 }
634
635 static struct rte_vdev_driver pmd_mvneta_drv = {
636         .probe = rte_pmd_mvneta_probe,
637         .remove = rte_pmd_mvneta_remove,
638 };
639
640 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
641 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
642
643 RTE_INIT(mvneta_init_log)
644 {
645         mvneta_logtype = rte_log_register("pmd.net.mvneta");
646         if (mvneta_logtype >= 0)
647                 rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);
648 }