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