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