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