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