net/af_packet: make qdisc bypass configurable
[dpdk.git] / drivers / net / ark / ark_ethdev.c
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright (c) 2015-2017 Atomic Rules LLC
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <dlfcn.h>
37
38 #include <rte_ethdev_pci.h>
39 #include <rte_kvargs.h>
40
41 #include "ark_global.h"
42 #include "ark_logs.h"
43 #include "ark_ethdev_tx.h"
44 #include "ark_ethdev_rx.h"
45 #include "ark_mpu.h"
46 #include "ark_ddm.h"
47 #include "ark_udm.h"
48 #include "ark_rqp.h"
49 #include "ark_pktdir.h"
50 #include "ark_pktgen.h"
51 #include "ark_pktchkr.h"
52
53 /*  Internal prototypes */
54 static int eth_ark_check_args(struct ark_adapter *ark, const char *params);
55 static int eth_ark_dev_init(struct rte_eth_dev *dev);
56 static int ark_config_device(struct rte_eth_dev *dev);
57 static int eth_ark_dev_uninit(struct rte_eth_dev *eth_dev);
58 static int eth_ark_dev_configure(struct rte_eth_dev *dev);
59 static int eth_ark_dev_start(struct rte_eth_dev *dev);
60 static void eth_ark_dev_stop(struct rte_eth_dev *dev);
61 static void eth_ark_dev_close(struct rte_eth_dev *dev);
62 static void eth_ark_dev_info_get(struct rte_eth_dev *dev,
63                                  struct rte_eth_dev_info *dev_info);
64 static int eth_ark_dev_link_update(struct rte_eth_dev *dev,
65                                    int wait_to_complete);
66 static int eth_ark_dev_set_link_up(struct rte_eth_dev *dev);
67 static int eth_ark_dev_set_link_down(struct rte_eth_dev *dev);
68 static void eth_ark_dev_stats_get(struct rte_eth_dev *dev,
69                                   struct rte_eth_stats *stats);
70 static void eth_ark_dev_stats_reset(struct rte_eth_dev *dev);
71 static void eth_ark_set_default_mac_addr(struct rte_eth_dev *dev,
72                                          struct ether_addr *mac_addr);
73 static int eth_ark_macaddr_add(struct rte_eth_dev *dev,
74                                struct ether_addr *mac_addr,
75                                uint32_t index,
76                                uint32_t pool);
77 static void eth_ark_macaddr_remove(struct rte_eth_dev *dev,
78                                    uint32_t index);
79 static int  eth_ark_set_mtu(struct rte_eth_dev *dev, uint16_t size);
80
81 /*
82  * The packet generator is a functional block used to generate packet
83  * patterns for testing.  It is not intended for nominal use.
84  */
85 #define ARK_PKTGEN_ARG "Pkt_gen"
86
87 /*
88  * The packet checker is a functional block used to verify packet
89  * patterns for testing.  It is not intended for nominal use.
90  */
91 #define ARK_PKTCHKR_ARG "Pkt_chkr"
92
93 /*
94  * The packet director is used to select the internal ingress and
95  * egress packets paths during testing.  It is not intended for
96  * nominal use.
97  */
98 #define ARK_PKTDIR_ARG "Pkt_dir"
99
100 /* Devinfo configurations */
101 #define ARK_RX_MAX_QUEUE (4096 * 4)
102 #define ARK_RX_MIN_QUEUE (512)
103 #define ARK_RX_MAX_PKT_LEN ((16 * 1024) - 128)
104 #define ARK_RX_MIN_BUFSIZE (1024)
105
106 #define ARK_TX_MAX_QUEUE (4096 * 4)
107 #define ARK_TX_MIN_QUEUE (256)
108
109 static const char * const valid_arguments[] = {
110         ARK_PKTGEN_ARG,
111         ARK_PKTCHKR_ARG,
112         ARK_PKTDIR_ARG,
113         NULL
114 };
115
116 static const struct rte_pci_id pci_id_ark_map[] = {
117         {RTE_PCI_DEVICE(0x1d6c, 0x100d)},
118         {RTE_PCI_DEVICE(0x1d6c, 0x100e)},
119         {.vendor_id = 0, /* sentinel */ },
120 };
121
122 static int
123 eth_ark_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
124                 struct rte_pci_device *pci_dev)
125 {
126         struct rte_eth_dev *eth_dev;
127         int ret;
128
129         eth_dev = rte_eth_dev_pci_allocate(pci_dev, sizeof(struct ark_adapter));
130
131         if (eth_dev == NULL)
132                 return -ENOMEM;
133
134         ret = eth_ark_dev_init(eth_dev);
135         if (ret)
136                 rte_eth_dev_pci_release(eth_dev);
137
138         return ret;
139 }
140
141 static int
142 eth_ark_pci_remove(struct rte_pci_device *pci_dev)
143 {
144         return rte_eth_dev_pci_generic_remove(pci_dev, eth_ark_dev_uninit);
145 }
146
147 static struct rte_pci_driver rte_ark_pmd = {
148         .id_table = pci_id_ark_map,
149         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
150         .probe = eth_ark_pci_probe,
151         .remove = eth_ark_pci_remove,
152 };
153
154 static const struct eth_dev_ops ark_eth_dev_ops = {
155         .dev_configure = eth_ark_dev_configure,
156         .dev_start = eth_ark_dev_start,
157         .dev_stop = eth_ark_dev_stop,
158         .dev_close = eth_ark_dev_close,
159
160         .dev_infos_get = eth_ark_dev_info_get,
161
162         .rx_queue_setup = eth_ark_dev_rx_queue_setup,
163         .rx_queue_count = eth_ark_dev_rx_queue_count,
164         .tx_queue_setup = eth_ark_tx_queue_setup,
165
166         .link_update = eth_ark_dev_link_update,
167         .dev_set_link_up = eth_ark_dev_set_link_up,
168         .dev_set_link_down = eth_ark_dev_set_link_down,
169
170         .rx_queue_start = eth_ark_rx_start_queue,
171         .rx_queue_stop = eth_ark_rx_stop_queue,
172
173         .tx_queue_start = eth_ark_tx_queue_start,
174         .tx_queue_stop = eth_ark_tx_queue_stop,
175
176         .stats_get = eth_ark_dev_stats_get,
177         .stats_reset = eth_ark_dev_stats_reset,
178
179         .mac_addr_add = eth_ark_macaddr_add,
180         .mac_addr_remove = eth_ark_macaddr_remove,
181         .mac_addr_set = eth_ark_set_default_mac_addr,
182
183         .mtu_set = eth_ark_set_mtu,
184 };
185
186 static int
187 check_for_ext(struct ark_adapter *ark)
188 {
189         int found = 0;
190
191         /* Get the env */
192         const char *dllpath = getenv("ARK_EXT_PATH");
193
194         if (dllpath == NULL) {
195                 PMD_DEBUG_LOG(DEBUG, "ARK EXT NO dll path specified\n");
196                 return 0;
197         }
198         PMD_DRV_LOG(INFO, "ARK EXT found dll path at %s\n", dllpath);
199
200         /* Open and load the .so */
201         ark->d_handle = dlopen(dllpath, RTLD_LOCAL | RTLD_LAZY);
202         if (ark->d_handle == NULL) {
203                 PMD_DRV_LOG(ERR, "Could not load user extension %s\n",
204                             dllpath);
205                 return -1;
206         }
207         PMD_DRV_LOG(INFO, "SUCCESS: loaded user extension %s\n",
208                             dllpath);
209
210         /* Get the entry points */
211         ark->user_ext.dev_init =
212                 (void *(*)(struct rte_eth_dev *, void *, int))
213                 dlsym(ark->d_handle, "dev_init");
214         PMD_DEBUG_LOG(DEBUG, "device ext init pointer = %p\n",
215                       ark->user_ext.dev_init);
216         ark->user_ext.dev_get_port_count =
217                 (int (*)(struct rte_eth_dev *, void *))
218                 dlsym(ark->d_handle, "dev_get_port_count");
219         ark->user_ext.dev_uninit =
220                 (void (*)(struct rte_eth_dev *, void *))
221                 dlsym(ark->d_handle, "dev_uninit");
222         ark->user_ext.dev_configure =
223                 (int (*)(struct rte_eth_dev *, void *))
224                 dlsym(ark->d_handle, "dev_configure");
225         ark->user_ext.dev_start =
226                 (int (*)(struct rte_eth_dev *, void *))
227                 dlsym(ark->d_handle, "dev_start");
228         ark->user_ext.dev_stop =
229                 (void (*)(struct rte_eth_dev *, void *))
230                 dlsym(ark->d_handle, "dev_stop");
231         ark->user_ext.dev_close =
232                 (void (*)(struct rte_eth_dev *, void *))
233                 dlsym(ark->d_handle, "dev_close");
234         ark->user_ext.link_update =
235                 (int (*)(struct rte_eth_dev *, int, void *))
236                 dlsym(ark->d_handle, "link_update");
237         ark->user_ext.dev_set_link_up =
238                 (int (*)(struct rte_eth_dev *, void *))
239                 dlsym(ark->d_handle, "dev_set_link_up");
240         ark->user_ext.dev_set_link_down =
241                 (int (*)(struct rte_eth_dev *, void *))
242                 dlsym(ark->d_handle, "dev_set_link_down");
243         ark->user_ext.stats_get =
244                 (void (*)(struct rte_eth_dev *, struct rte_eth_stats *,
245                           void *))
246                 dlsym(ark->d_handle, "stats_get");
247         ark->user_ext.stats_reset =
248                 (void (*)(struct rte_eth_dev *, void *))
249                 dlsym(ark->d_handle, "stats_reset");
250         ark->user_ext.mac_addr_add =
251                 (void (*)(struct rte_eth_dev *, struct ether_addr *, uint32_t,
252                           uint32_t, void *))
253                 dlsym(ark->d_handle, "mac_addr_add");
254         ark->user_ext.mac_addr_remove =
255                 (void (*)(struct rte_eth_dev *, uint32_t, void *))
256                 dlsym(ark->d_handle, "mac_addr_remove");
257         ark->user_ext.mac_addr_set =
258                 (void (*)(struct rte_eth_dev *, struct ether_addr *,
259                           void *))
260                 dlsym(ark->d_handle, "mac_addr_set");
261         ark->user_ext.set_mtu =
262                 (int (*)(struct rte_eth_dev *, uint16_t,
263                           void *))
264                 dlsym(ark->d_handle, "set_mtu");
265
266         return found;
267 }
268
269 static int
270 eth_ark_dev_init(struct rte_eth_dev *dev)
271 {
272         struct ark_adapter *ark =
273                 (struct ark_adapter *)dev->data->dev_private;
274         struct rte_pci_device *pci_dev;
275         int ret;
276         int port_count = 1;
277         int p;
278
279         ark->eth_dev = dev;
280
281         PMD_FUNC_LOG(DEBUG, "\n");
282
283         /* Check to see if there is an extension that we need to load */
284         ret = check_for_ext(ark);
285         if (ret)
286                 return ret;
287         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
288         rte_eth_copy_pci_info(dev, pci_dev);
289
290         /* Use dummy function until setup */
291         dev->rx_pkt_burst = &eth_ark_recv_pkts_noop;
292         dev->tx_pkt_burst = &eth_ark_xmit_pkts_noop;
293
294         ark->bar0 = (uint8_t *)pci_dev->mem_resource[0].addr;
295         ark->a_bar = (uint8_t *)pci_dev->mem_resource[2].addr;
296
297         ark->sysctrl.v  = (void *)&ark->bar0[ARK_SYSCTRL_BASE];
298         ark->mpurx.v  = (void *)&ark->bar0[ARK_MPU_RX_BASE];
299         ark->udm.v  = (void *)&ark->bar0[ARK_UDM_BASE];
300         ark->mputx.v  = (void *)&ark->bar0[ARK_MPU_TX_BASE];
301         ark->ddm.v  = (void *)&ark->bar0[ARK_DDM_BASE];
302         ark->cmac.v  = (void *)&ark->bar0[ARK_CMAC_BASE];
303         ark->external.v  = (void *)&ark->bar0[ARK_EXTERNAL_BASE];
304         ark->pktdir.v  = (void *)&ark->bar0[ARK_PKTDIR_BASE];
305         ark->pktgen.v  = (void *)&ark->bar0[ARK_PKTGEN_BASE];
306         ark->pktchkr.v  = (void *)&ark->bar0[ARK_PKTCHKR_BASE];
307
308         ark->rqpacing =
309                 (struct ark_rqpace_t *)(ark->bar0 + ARK_RCPACING_BASE);
310         ark->started = 0;
311
312         PMD_DEBUG_LOG(INFO, "Sys Ctrl Const = 0x%x  HW Commit_ID: %08x\n",
313                       ark->sysctrl.t32[4],
314                       rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
315         PMD_DRV_LOG(INFO, "Arkville HW Commit_ID: %08x\n",
316                     rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
317
318         /* If HW sanity test fails, return an error */
319         if (ark->sysctrl.t32[4] != 0xcafef00d) {
320                 PMD_DRV_LOG(ERR,
321                             "HW Sanity test has failed, expected constant"
322                             " 0x%x, read 0x%x (%s)\n",
323                             0xcafef00d,
324                             ark->sysctrl.t32[4], __func__);
325                 return -1;
326         }
327         if (ark->sysctrl.t32[3] != 0) {
328                 if (ark_rqp_lasped(ark->rqpacing)) {
329                         PMD_DRV_LOG(ERR, "Arkville Evaluation System - "
330                                     "Timer has Expired\n");
331                         return -1;
332                 }
333                 PMD_DRV_LOG(WARNING, "Arkville Evaluation System - "
334                             "Timer is Running\n");
335         }
336
337         PMD_DRV_LOG(INFO,
338                     "HW Sanity test has PASSED, expected constant"
339                     " 0x%x, read 0x%x (%s)\n",
340                     0xcafef00d, ark->sysctrl.t32[4], __func__);
341
342         /* We are a single function multi-port device. */
343         ret = ark_config_device(dev);
344         dev->dev_ops = &ark_eth_dev_ops;
345         dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
346
347         dev->data->mac_addrs = rte_zmalloc("ark", ETHER_ADDR_LEN, 0);
348         if (!dev->data->mac_addrs) {
349                 PMD_DRV_LOG(ERR,
350                             "Failed to allocated memory for storing mac address"
351                             );
352         }
353
354         if (ark->user_ext.dev_init) {
355                 ark->user_data[dev->data->port_id] =
356                         ark->user_ext.dev_init(dev, ark->a_bar, 0);
357                 if (!ark->user_data[dev->data->port_id]) {
358                         PMD_DRV_LOG(INFO,
359                                     "Failed to initialize PMD extension!"
360                                     " continuing without it\n");
361                         memset(&ark->user_ext, 0, sizeof(struct ark_user_ext));
362                         dlclose(ark->d_handle);
363                 }
364         }
365
366         if (pci_dev->device.devargs)
367                 ret = eth_ark_check_args(ark, pci_dev->device.devargs->args);
368         else
369                 PMD_DRV_LOG(INFO, "No Device args found\n");
370
371         if (ret)
372                 goto error;
373         /*
374          * We will create additional devices based on the number of requested
375          * ports
376          */
377         if (ark->user_ext.dev_get_port_count)
378                 port_count =
379                         ark->user_ext.dev_get_port_count(dev,
380                                  ark->user_data[dev->data->port_id]);
381         ark->num_ports = port_count;
382
383         for (p = 0; p < port_count; p++) {
384                 struct rte_eth_dev *eth_dev;
385                 char name[RTE_ETH_NAME_MAX_LEN];
386
387                 snprintf(name, sizeof(name), "arketh%d",
388                          dev->data->port_id + p);
389
390                 if (p == 0) {
391                         /* First port is already allocated by DPDK */
392                         eth_dev = ark->eth_dev;
393                         continue;
394                 }
395
396                 /* reserve an ethdev entry */
397                 eth_dev = rte_eth_dev_allocate(name);
398                 if (!eth_dev) {
399                         PMD_DRV_LOG(ERR,
400                                     "Could not allocate eth_dev for port %d\n",
401                                     p);
402                         goto error;
403                 }
404
405                 eth_dev->device = &pci_dev->device;
406                 eth_dev->data->dev_private = ark;
407                 eth_dev->dev_ops = ark->eth_dev->dev_ops;
408                 eth_dev->tx_pkt_burst = ark->eth_dev->tx_pkt_burst;
409                 eth_dev->rx_pkt_burst = ark->eth_dev->rx_pkt_burst;
410
411                 rte_eth_copy_pci_info(eth_dev, pci_dev);
412
413                 eth_dev->data->mac_addrs = rte_zmalloc(name, ETHER_ADDR_LEN, 0);
414                 if (!eth_dev->data->mac_addrs) {
415                         PMD_DRV_LOG(ERR,
416                                     "Memory allocation for MAC failed!"
417                                     " Exiting.\n");
418                         goto error;
419                 }
420
421                 if (ark->user_ext.dev_init) {
422                         ark->user_data[eth_dev->data->port_id] =
423                                 ark->user_ext.dev_init(dev, ark->a_bar, p);
424                 }
425         }
426
427         return ret;
428
429  error:
430         if (dev->data->mac_addrs)
431                 rte_free(dev->data->mac_addrs);
432         return -1;
433 }
434
435 /*
436  *Initial device configuration when device is opened
437  * setup the DDM, and UDM
438  * Called once per PCIE device
439  */
440 static int
441 ark_config_device(struct rte_eth_dev *dev)
442 {
443         struct ark_adapter *ark =
444                 (struct ark_adapter *)dev->data->dev_private;
445         uint16_t num_q, i;
446         struct ark_mpu_t *mpu;
447
448         /*
449          * Make sure that the packet director, generator and checker are in a
450          * known state
451          */
452         ark->start_pg = 0;
453         ark->pg = ark_pktgen_init(ark->pktgen.v, 0, 1);
454         ark_pktgen_reset(ark->pg);
455         ark->pc = ark_pktchkr_init(ark->pktchkr.v, 0, 1);
456         ark_pktchkr_stop(ark->pc);
457         ark->pd = ark_pktdir_init(ark->pktdir.v);
458
459         /* Verify HW */
460         if (ark_udm_verify(ark->udm.v))
461                 return -1;
462         if (ark_ddm_verify(ark->ddm.v))
463                 return -1;
464
465         /* UDM */
466         if (ark_udm_reset(ark->udm.v)) {
467                 PMD_DRV_LOG(ERR, "Unable to stop and reset UDM\n");
468                 return -1;
469         }
470         /* Keep in reset until the MPU are cleared */
471
472         /* MPU reset */
473         mpu = ark->mpurx.v;
474         num_q = ark_api_num_queues(mpu);
475         ark->rx_queues = num_q;
476         for (i = 0; i < num_q; i++) {
477                 ark_mpu_reset(mpu);
478                 mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
479         }
480
481         ark_udm_stop(ark->udm.v, 0);
482         ark_udm_configure(ark->udm.v,
483                           RTE_PKTMBUF_HEADROOM,
484                           RTE_MBUF_DEFAULT_DATAROOM,
485                           ARK_RX_WRITE_TIME_NS);
486         ark_udm_stats_reset(ark->udm.v);
487         ark_udm_stop(ark->udm.v, 0);
488
489         /* TX -- DDM */
490         if (ark_ddm_stop(ark->ddm.v, 1))
491                 PMD_DRV_LOG(ERR, "Unable to stop DDM\n");
492
493         mpu = ark->mputx.v;
494         num_q = ark_api_num_queues(mpu);
495         ark->tx_queues = num_q;
496         for (i = 0; i < num_q; i++) {
497                 ark_mpu_reset(mpu);
498                 mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
499         }
500
501         ark_ddm_reset(ark->ddm.v);
502         ark_ddm_stats_reset(ark->ddm.v);
503
504         ark_ddm_stop(ark->ddm.v, 0);
505         ark_rqp_stats_reset(ark->rqpacing);
506
507         return 0;
508 }
509
510 static int
511 eth_ark_dev_uninit(struct rte_eth_dev *dev)
512 {
513         struct ark_adapter *ark =
514                 (struct ark_adapter *)dev->data->dev_private;
515
516         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
517                 return 0;
518
519         if (ark->user_ext.dev_uninit)
520                 ark->user_ext.dev_uninit(dev,
521                          ark->user_data[dev->data->port_id]);
522
523         ark_pktgen_uninit(ark->pg);
524         ark_pktchkr_uninit(ark->pc);
525
526         dev->dev_ops = NULL;
527         dev->rx_pkt_burst = NULL;
528         dev->tx_pkt_burst = NULL;
529         rte_free(dev->data->mac_addrs);
530         return 0;
531 }
532
533 static int
534 eth_ark_dev_configure(struct rte_eth_dev *dev)
535 {
536         PMD_FUNC_LOG(DEBUG, "\n");
537         struct ark_adapter *ark =
538                 (struct ark_adapter *)dev->data->dev_private;
539
540         eth_ark_dev_set_link_up(dev);
541         if (ark->user_ext.dev_configure)
542                 return ark->user_ext.dev_configure(dev,
543                            ark->user_data[dev->data->port_id]);
544         return 0;
545 }
546
547 static void *
548 delay_pg_start(void *arg)
549 {
550         struct ark_adapter *ark = (struct ark_adapter *)arg;
551
552         /* This function is used exclusively for regression testing, We
553          * perform a blind sleep here to ensure that the external test
554          * application has time to setup the test before we generate packets
555          */
556         usleep(100000);
557         ark_pktgen_run(ark->pg);
558         return NULL;
559 }
560
561 static int
562 eth_ark_dev_start(struct rte_eth_dev *dev)
563 {
564         struct ark_adapter *ark =
565                 (struct ark_adapter *)dev->data->dev_private;
566         int i;
567
568         PMD_FUNC_LOG(DEBUG, "\n");
569
570         /* RX Side */
571         /* start UDM */
572         ark_udm_start(ark->udm.v);
573
574         for (i = 0; i < dev->data->nb_rx_queues; i++)
575                 eth_ark_rx_start_queue(dev, i);
576
577         /* TX Side */
578         for (i = 0; i < dev->data->nb_tx_queues; i++)
579                 eth_ark_tx_queue_start(dev, i);
580
581         /* start DDM */
582         ark_ddm_start(ark->ddm.v);
583
584         ark->started = 1;
585         /* set xmit and receive function */
586         dev->rx_pkt_burst = &eth_ark_recv_pkts;
587         dev->tx_pkt_burst = &eth_ark_xmit_pkts;
588
589         if (ark->start_pg)
590                 ark_pktchkr_run(ark->pc);
591
592         if (ark->start_pg && (dev->data->port_id == 0)) {
593                 pthread_t thread;
594
595                 /* Delay packet generatpr start allow the hardware to be ready
596                  * This is only used for sanity checking with internal generator
597                  */
598                 if (pthread_create(&thread, NULL, delay_pg_start, ark)) {
599                         PMD_DRV_LOG(ERR, "Could not create pktgen "
600                                     "starter thread\n");
601                         return -1;
602                 }
603         }
604
605         if (ark->user_ext.dev_start)
606                 ark->user_ext.dev_start(dev,
607                         ark->user_data[dev->data->port_id]);
608
609         return 0;
610 }
611
612 static void
613 eth_ark_dev_stop(struct rte_eth_dev *dev)
614 {
615         uint16_t i;
616         int status;
617         struct ark_adapter *ark =
618                 (struct ark_adapter *)dev->data->dev_private;
619         struct ark_mpu_t *mpu;
620
621         PMD_FUNC_LOG(DEBUG, "\n");
622
623         if (ark->started == 0)
624                 return;
625         ark->started = 0;
626
627         /* Stop the extension first */
628         if (ark->user_ext.dev_stop)
629                 ark->user_ext.dev_stop(dev,
630                        ark->user_data[dev->data->port_id]);
631
632         /* Stop the packet generator */
633         if (ark->start_pg)
634                 ark_pktgen_pause(ark->pg);
635
636         dev->rx_pkt_burst = &eth_ark_recv_pkts_noop;
637         dev->tx_pkt_burst = &eth_ark_xmit_pkts_noop;
638
639         /* STOP TX Side */
640         for (i = 0; i < dev->data->nb_tx_queues; i++) {
641                 status = eth_ark_tx_queue_stop(dev, i);
642                 if (status != 0) {
643                         uint8_t port = dev->data->port_id;
644                         PMD_DRV_LOG(ERR,
645                                     "tx_queue stop anomaly"
646                                     " port %u, queue %u\n",
647                                     port, i);
648                 }
649         }
650
651         /* Stop DDM */
652         /* Wait up to 0.1 second.  each stop is up to 1000 * 10 useconds */
653         for (i = 0; i < 10; i++) {
654                 status = ark_ddm_stop(ark->ddm.v, 1);
655                 if (status == 0)
656                         break;
657         }
658         if (status || i != 0) {
659                 PMD_DRV_LOG(ERR, "DDM stop anomaly. status:"
660                             " %d iter: %u. (%s)\n",
661                             status,
662                             i,
663                             __func__);
664                 ark_ddm_dump(ark->ddm.v, "Stop anomaly");
665
666                 mpu = ark->mputx.v;
667                 for (i = 0; i < ark->tx_queues; i++) {
668                         ark_mpu_dump(mpu, "DDM failure dump", i);
669                         mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
670                 }
671         }
672
673         /* STOP RX Side */
674         /* Stop UDM  multiple tries attempted */
675         for (i = 0; i < 10; i++) {
676                 status = ark_udm_stop(ark->udm.v, 1);
677                 if (status == 0)
678                         break;
679         }
680         if (status || i != 0) {
681                 PMD_DRV_LOG(ERR, "UDM stop anomaly. status %d iter: %u. (%s)\n",
682                             status, i, __func__);
683                 ark_udm_dump(ark->udm.v, "Stop anomaly");
684
685                 mpu = ark->mpurx.v;
686                 for (i = 0; i < ark->rx_queues; i++) {
687                         ark_mpu_dump(mpu, "UDM Stop anomaly", i);
688                         mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
689                 }
690         }
691
692         ark_udm_dump_stats(ark->udm.v, "Post stop");
693         ark_udm_dump_perf(ark->udm.v, "Post stop");
694
695         for (i = 0; i < dev->data->nb_tx_queues; i++)
696                 eth_ark_rx_dump_queue(dev, i, __func__);
697
698         /* Stop the packet checker if it is running */
699         if (ark->start_pg) {
700                 ark_pktchkr_dump_stats(ark->pc);
701                 ark_pktchkr_stop(ark->pc);
702         }
703 }
704
705 static void
706 eth_ark_dev_close(struct rte_eth_dev *dev)
707 {
708         struct ark_adapter *ark =
709                 (struct ark_adapter *)dev->data->dev_private;
710         uint16_t i;
711
712         if (ark->user_ext.dev_close)
713                 ark->user_ext.dev_close(dev,
714                  ark->user_data[dev->data->port_id]);
715
716         eth_ark_dev_stop(dev);
717         eth_ark_udm_force_close(dev);
718
719         /*
720          * TODO This should only be called once for the device during shutdown
721          */
722         ark_rqp_dump(ark->rqpacing);
723
724         for (i = 0; i < dev->data->nb_tx_queues; i++) {
725                 eth_ark_tx_queue_release(dev->data->tx_queues[i]);
726                 dev->data->tx_queues[i] = 0;
727         }
728
729         for (i = 0; i < dev->data->nb_rx_queues; i++) {
730                 eth_ark_dev_rx_queue_release(dev->data->rx_queues[i]);
731                 dev->data->rx_queues[i] = 0;
732         }
733 }
734
735 static void
736 eth_ark_dev_info_get(struct rte_eth_dev *dev,
737                      struct rte_eth_dev_info *dev_info)
738 {
739         struct ark_adapter *ark =
740                 (struct ark_adapter *)dev->data->dev_private;
741         struct ark_mpu_t *tx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_TX_BASE);
742         struct ark_mpu_t *rx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_RX_BASE);
743         uint16_t ports = ark->num_ports;
744
745         dev_info->max_rx_pktlen = ARK_RX_MAX_PKT_LEN;
746         dev_info->min_rx_bufsize = ARK_RX_MIN_BUFSIZE;
747
748         dev_info->max_rx_queues = ark_api_num_queues_per_port(rx_mpu, ports);
749         dev_info->max_tx_queues = ark_api_num_queues_per_port(tx_mpu, ports);
750
751         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
752                 .nb_max = ARK_RX_MAX_QUEUE,
753                 .nb_min = ARK_RX_MIN_QUEUE,
754                 .nb_align = ARK_RX_MIN_QUEUE}; /* power of 2 */
755
756         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
757                 .nb_max = ARK_TX_MAX_QUEUE,
758                 .nb_min = ARK_TX_MIN_QUEUE,
759                 .nb_align = ARK_TX_MIN_QUEUE}; /* power of 2 */
760
761         /* ARK PMD supports all line rates, how do we indicate that here ?? */
762         dev_info->speed_capa = (ETH_LINK_SPEED_1G |
763                                 ETH_LINK_SPEED_10G |
764                                 ETH_LINK_SPEED_25G |
765                                 ETH_LINK_SPEED_40G |
766                                 ETH_LINK_SPEED_50G |
767                                 ETH_LINK_SPEED_100G);
768         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
769 }
770
771 static int
772 eth_ark_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
773 {
774         PMD_DEBUG_LOG(DEBUG, "link status = %d\n",
775                         dev->data->dev_link.link_status);
776         struct ark_adapter *ark =
777                 (struct ark_adapter *)dev->data->dev_private;
778
779         if (ark->user_ext.link_update) {
780                 return ark->user_ext.link_update
781                         (dev, wait_to_complete,
782                          ark->user_data[dev->data->port_id]);
783         }
784         return 0;
785 }
786
787 static int
788 eth_ark_dev_set_link_up(struct rte_eth_dev *dev)
789 {
790         dev->data->dev_link.link_status = 1;
791         struct ark_adapter *ark =
792                 (struct ark_adapter *)dev->data->dev_private;
793
794         if (ark->user_ext.dev_set_link_up)
795                 return ark->user_ext.dev_set_link_up(dev,
796                              ark->user_data[dev->data->port_id]);
797         return 0;
798 }
799
800 static int
801 eth_ark_dev_set_link_down(struct rte_eth_dev *dev)
802 {
803         dev->data->dev_link.link_status = 0;
804         struct ark_adapter *ark =
805                 (struct ark_adapter *)dev->data->dev_private;
806
807         if (ark->user_ext.dev_set_link_down)
808                 return ark->user_ext.dev_set_link_down(dev,
809                        ark->user_data[dev->data->port_id]);
810         return 0;
811 }
812
813 static void
814 eth_ark_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
815 {
816         uint16_t i;
817         struct ark_adapter *ark =
818                 (struct ark_adapter *)dev->data->dev_private;
819
820         stats->ipackets = 0;
821         stats->ibytes = 0;
822         stats->opackets = 0;
823         stats->obytes = 0;
824         stats->imissed = 0;
825         stats->oerrors = 0;
826
827         for (i = 0; i < dev->data->nb_tx_queues; i++)
828                 eth_tx_queue_stats_get(dev->data->tx_queues[i], stats);
829         for (i = 0; i < dev->data->nb_rx_queues; i++)
830                 eth_rx_queue_stats_get(dev->data->rx_queues[i], stats);
831         if (ark->user_ext.stats_get)
832                 ark->user_ext.stats_get(dev, stats,
833                         ark->user_data[dev->data->port_id]);
834 }
835
836 static void
837 eth_ark_dev_stats_reset(struct rte_eth_dev *dev)
838 {
839         uint16_t i;
840         struct ark_adapter *ark =
841                 (struct ark_adapter *)dev->data->dev_private;
842
843         for (i = 0; i < dev->data->nb_tx_queues; i++)
844                 eth_tx_queue_stats_reset(dev->data->tx_queues[i]);
845         for (i = 0; i < dev->data->nb_rx_queues; i++)
846                 eth_rx_queue_stats_reset(dev->data->rx_queues[i]);
847         if (ark->user_ext.stats_reset)
848                 ark->user_ext.stats_reset(dev,
849                           ark->user_data[dev->data->port_id]);
850 }
851
852 static int
853 eth_ark_macaddr_add(struct rte_eth_dev *dev,
854                     struct ether_addr *mac_addr,
855                     uint32_t index,
856                     uint32_t pool)
857 {
858         struct ark_adapter *ark =
859                 (struct ark_adapter *)dev->data->dev_private;
860
861         if (ark->user_ext.mac_addr_add) {
862                 ark->user_ext.mac_addr_add(dev,
863                                            mac_addr,
864                                            index,
865                                            pool,
866                            ark->user_data[dev->data->port_id]);
867                 return 0;
868         }
869         return -ENOTSUP;
870 }
871
872 static void
873 eth_ark_macaddr_remove(struct rte_eth_dev *dev, uint32_t index)
874 {
875         struct ark_adapter *ark =
876                 (struct ark_adapter *)dev->data->dev_private;
877
878         if (ark->user_ext.mac_addr_remove)
879                 ark->user_ext.mac_addr_remove(dev, index,
880                               ark->user_data[dev->data->port_id]);
881 }
882
883 static void
884 eth_ark_set_default_mac_addr(struct rte_eth_dev *dev,
885                              struct ether_addr *mac_addr)
886 {
887         struct ark_adapter *ark =
888                 (struct ark_adapter *)dev->data->dev_private;
889
890         if (ark->user_ext.mac_addr_set)
891                 ark->user_ext.mac_addr_set(dev, mac_addr,
892                            ark->user_data[dev->data->port_id]);
893 }
894
895 static int
896 eth_ark_set_mtu(struct rte_eth_dev *dev, uint16_t  size)
897 {
898         struct ark_adapter *ark =
899                 (struct ark_adapter *)dev->data->dev_private;
900
901         if (ark->user_ext.set_mtu)
902                 return ark->user_ext.set_mtu(dev, size,
903                              ark->user_data[dev->data->port_id]);
904
905         return -ENOTSUP;
906 }
907
908 static inline int
909 process_pktdir_arg(const char *key, const char *value,
910                    void *extra_args)
911 {
912         PMD_FUNC_LOG(DEBUG, "key = %s, value = %s\n",
913                     key, value);
914         struct ark_adapter *ark =
915                 (struct ark_adapter *)extra_args;
916
917         ark->pkt_dir_v = strtol(value, NULL, 16);
918         PMD_FUNC_LOG(DEBUG, "pkt_dir_v = 0x%x\n", ark->pkt_dir_v);
919         return 0;
920 }
921
922 static inline int
923 process_file_args(const char *key, const char *value, void *extra_args)
924 {
925         PMD_FUNC_LOG(DEBUG, "key = %s, value = %s\n",
926                     key, value);
927         char *args = (char *)extra_args;
928
929         /* Open the configuration file */
930         FILE *file = fopen(value, "r");
931         char line[ARK_MAX_ARG_LEN];
932         int  size = 0;
933         int first = 1;
934
935         if (file == NULL) {
936                 PMD_DRV_LOG(ERR, "Unable to open "
937                             "config file %s\n", value);
938                 return -1;
939         }
940
941         while (fgets(line, sizeof(line), file)) {
942                 size += strlen(line);
943                 if (size >= ARK_MAX_ARG_LEN) {
944                         PMD_DRV_LOG(ERR, "Unable to parse file %s args, "
945                                     "parameter list is too long\n", value);
946                         fclose(file);
947                         return -1;
948                 }
949                 if (first) {
950                         strncpy(args, line, ARK_MAX_ARG_LEN);
951                         first = 0;
952                 } else {
953                         strncat(args, line, ARK_MAX_ARG_LEN);
954                 }
955         }
956         PMD_FUNC_LOG(DEBUG, "file = %s\n", args);
957         fclose(file);
958         return 0;
959 }
960
961 static int
962 eth_ark_check_args(struct ark_adapter *ark, const char *params)
963 {
964         struct rte_kvargs *kvlist;
965         unsigned int k_idx;
966         struct rte_kvargs_pair *pair = NULL;
967         int ret = -1;
968
969         kvlist = rte_kvargs_parse(params, valid_arguments);
970         if (kvlist == NULL)
971                 return 0;
972
973         ark->pkt_gen_args[0] = 0;
974         ark->pkt_chkr_args[0] = 0;
975
976         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
977                 pair = &kvlist->pairs[k_idx];
978                 PMD_FUNC_LOG(DEBUG, "**** Arg passed to PMD = %s:%s\n",
979                              pair->key,
980                              pair->value);
981         }
982
983         if (rte_kvargs_process(kvlist,
984                                ARK_PKTDIR_ARG,
985                                &process_pktdir_arg,
986                                ark) != 0) {
987                 PMD_DRV_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTDIR_ARG);
988                 goto free_kvlist;
989         }
990
991         if (rte_kvargs_process(kvlist,
992                                ARK_PKTGEN_ARG,
993                                &process_file_args,
994                                ark->pkt_gen_args) != 0) {
995                 PMD_DRV_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTGEN_ARG);
996                 goto free_kvlist;
997         }
998
999         if (rte_kvargs_process(kvlist,
1000                                ARK_PKTCHKR_ARG,
1001                                &process_file_args,
1002                                ark->pkt_chkr_args) != 0) {
1003                 PMD_DRV_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTCHKR_ARG);
1004                 goto free_kvlist;
1005         }
1006
1007         PMD_DRV_LOG(INFO, "packet director set to 0x%x\n", ark->pkt_dir_v);
1008         /* Setup the packet director */
1009         ark_pktdir_setup(ark->pd, ark->pkt_dir_v);
1010
1011         /* Setup the packet generator */
1012         if (ark->pkt_gen_args[0]) {
1013                 PMD_DRV_LOG(INFO, "Setting up the packet generator\n");
1014                 ark_pktgen_parse(ark->pkt_gen_args);
1015                 ark_pktgen_reset(ark->pg);
1016                 ark_pktgen_setup(ark->pg);
1017                 ark->start_pg = 1;
1018         }
1019
1020         /* Setup the packet checker */
1021         if (ark->pkt_chkr_args[0]) {
1022                 ark_pktchkr_parse(ark->pkt_chkr_args);
1023                 ark_pktchkr_setup(ark->pc);
1024         }
1025
1026         ret = 0;
1027
1028 free_kvlist:
1029         rte_kvargs_free(kvlist);
1030
1031         return ret;
1032 }
1033
1034 RTE_PMD_REGISTER_PCI(net_ark, rte_ark_pmd);
1035 RTE_PMD_REGISTER_KMOD_DEP(net_ark, "* igb_uio | uio_pci_generic ");
1036 RTE_PMD_REGISTER_PCI_TABLE(net_ark, pci_id_ark_map);
1037 RTE_PMD_REGISTER_PARAM_STRING(net_ark,
1038                               ARK_PKTGEN_ARG "=<filename> "
1039                               ARK_PKTCHKR_ARG "=<filename> "
1040                               ARK_PKTDIR_ARG "=<bitmap>");