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