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