xref: /dpdk/drivers/net/ark/ark_ethdev.c (revision 8f1d23ece06adff5eae9f1b4365bdbbd3abee2b2)
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, 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 	{.vendor_id = 0, /* sentinel */ },
103 };
104 
105 /*
106  * This structure is used to statically define the capabilities
107  * of supported devices.
108  * Capabilities:
109  *  rqpacing -
110  * Some HW variants require that PCIe read-requests be correctly throttled.
111  * This is called "rqpacing" and has to do with credit and flow control
112  * on certain Arkville implementations.
113  */
114 struct ark_caps {
115 	bool rqpacing;
116 	bool isvf;
117 };
118 struct ark_dev_caps {
119 	uint32_t  device_id;
120 	struct ark_caps  caps;
121 };
122 #define SET_DEV_CAPS(id, rqp, vf)			\
123 	{id, {.rqpacing = rqp, .isvf = vf} }
124 
125 static const struct ark_dev_caps
126 ark_device_caps[] = {
127 		     SET_DEV_CAPS(0x100d, true, false),
128 		     SET_DEV_CAPS(0x100e, true, false),
129 		     SET_DEV_CAPS(0x100f, true, false),
130 		     SET_DEV_CAPS(0x1010, false, false),
131 		     SET_DEV_CAPS(0x1017, true, false),
132 		     SET_DEV_CAPS(0x1018, true, false),
133 		     SET_DEV_CAPS(0x1019, true, false),
134 		     SET_DEV_CAPS(0x101a, true, false),
135 		     SET_DEV_CAPS(0x101b, true, false),
136 		     SET_DEV_CAPS(0x101c, true, true),
137 		     SET_DEV_CAPS(0x101e, false, false),
138 		     SET_DEV_CAPS(0x101f, false, false),
139 		     {.device_id = 0,}
140 };
141 
142 static int
143 eth_ark_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
144 		struct rte_pci_device *pci_dev)
145 {
146 	struct rte_eth_dev *eth_dev;
147 	int ret;
148 
149 	eth_dev = rte_eth_dev_pci_allocate(pci_dev, sizeof(struct ark_adapter));
150 
151 	if (eth_dev == NULL)
152 		return -ENOMEM;
153 
154 	ret = eth_ark_dev_init(eth_dev);
155 	if (ret)
156 		rte_eth_dev_release_port(eth_dev);
157 
158 	return ret;
159 }
160 
161 static int
162 eth_ark_pci_remove(struct rte_pci_device *pci_dev)
163 {
164 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_ark_dev_uninit);
165 }
166 
167 static struct rte_pci_driver rte_ark_pmd = {
168 	.id_table = pci_id_ark_map,
169 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
170 	.probe = eth_ark_pci_probe,
171 	.remove = eth_ark_pci_remove,
172 };
173 
174 static const struct eth_dev_ops ark_eth_dev_ops = {
175 	.dev_configure = eth_ark_dev_configure,
176 	.dev_start = eth_ark_dev_start,
177 	.dev_stop = eth_ark_dev_stop,
178 	.dev_close = eth_ark_dev_close,
179 
180 	.dev_infos_get = eth_ark_dev_info_get,
181 
182 	.rx_queue_setup = eth_ark_dev_rx_queue_setup,
183 	.tx_queue_setup = eth_ark_tx_queue_setup,
184 
185 	.link_update = eth_ark_dev_link_update,
186 	.dev_set_link_up = eth_ark_dev_set_link_up,
187 	.dev_set_link_down = eth_ark_dev_set_link_down,
188 
189 	.rx_queue_start = eth_ark_rx_start_queue,
190 	.rx_queue_stop = eth_ark_rx_stop_queue,
191 
192 	.tx_queue_start = eth_ark_tx_queue_start,
193 	.tx_queue_stop = eth_ark_tx_queue_stop,
194 
195 	.stats_get = eth_ark_dev_stats_get,
196 	.stats_reset = eth_ark_dev_stats_reset,
197 
198 	.mac_addr_add = eth_ark_macaddr_add,
199 	.mac_addr_remove = eth_ark_macaddr_remove,
200 	.mac_addr_set = eth_ark_set_default_mac_addr,
201 
202 	.mtu_set = eth_ark_set_mtu,
203 };
204 
205 static int
206 check_for_ext(struct ark_adapter *ark)
207 {
208 	int found = 0;
209 
210 	/* Get the env */
211 	const char *dllpath = getenv("ARK_EXT_PATH");
212 
213 	if (dllpath == NULL) {
214 		ARK_PMD_LOG(DEBUG, "EXT NO dll path specified\n");
215 		return 0;
216 	}
217 	ARK_PMD_LOG(NOTICE, "EXT found dll path at %s\n", dllpath);
218 
219 	/* Open and load the .so */
220 	ark->d_handle = dlopen(dllpath, RTLD_LOCAL | RTLD_LAZY);
221 	if (ark->d_handle == NULL) {
222 		ARK_PMD_LOG(ERR, "Could not load user extension %s\n",
223 			    dllpath);
224 		return -1;
225 	}
226 	ARK_PMD_LOG(DEBUG, "SUCCESS: loaded user extension %s\n",
227 			    dllpath);
228 
229 	/* Get the entry points */
230 	ark->user_ext.dev_init =
231 		(void *(*)(struct rte_eth_dev *, void *, int))
232 		dlsym(ark->d_handle, "rte_pmd_ark_dev_init");
233 	ARK_PMD_LOG(DEBUG, "device ext init pointer = %p\n",
234 		      ark->user_ext.dev_init);
235 	ark->user_ext.dev_get_port_count =
236 		(int (*)(struct rte_eth_dev *, void *))
237 		dlsym(ark->d_handle, "rte_pmd_ark_dev_get_port_count");
238 	ark->user_ext.dev_uninit =
239 		(void (*)(struct rte_eth_dev *, void *))
240 		dlsym(ark->d_handle, "rte_pmd_ark_dev_uninit");
241 	ark->user_ext.dev_configure =
242 		(int (*)(struct rte_eth_dev *, void *))
243 		dlsym(ark->d_handle, "rte_pmd_ark_dev_configure");
244 	ark->user_ext.dev_start =
245 		(int (*)(struct rte_eth_dev *, void *))
246 		dlsym(ark->d_handle, "rte_pmd_ark_dev_start");
247 	ark->user_ext.dev_stop =
248 		(void (*)(struct rte_eth_dev *, void *))
249 		dlsym(ark->d_handle, "rte_pmd_ark_dev_stop");
250 	ark->user_ext.dev_close =
251 		(void (*)(struct rte_eth_dev *, void *))
252 		dlsym(ark->d_handle, "rte_pmd_ark_dev_close");
253 	ark->user_ext.link_update =
254 		(int (*)(struct rte_eth_dev *, int, void *))
255 		dlsym(ark->d_handle, "rte_pmd_ark_link_update");
256 	ark->user_ext.dev_set_link_up =
257 		(int (*)(struct rte_eth_dev *, void *))
258 		dlsym(ark->d_handle, "rte_pmd_ark_dev_set_link_up");
259 	ark->user_ext.dev_set_link_down =
260 		(int (*)(struct rte_eth_dev *, void *))
261 		dlsym(ark->d_handle, "rte_pmd_ark_dev_set_link_down");
262 	ark->user_ext.stats_get =
263 		(int (*)(struct rte_eth_dev *, struct rte_eth_stats *,
264 			  void *))
265 		dlsym(ark->d_handle, "rte_pmd_ark_stats_get");
266 	ark->user_ext.stats_reset =
267 		(void (*)(struct rte_eth_dev *, void *))
268 		dlsym(ark->d_handle, "rte_pmd_ark_stats_reset");
269 	ark->user_ext.mac_addr_add =
270 		(void (*)(struct rte_eth_dev *, struct rte_ether_addr *,
271 			uint32_t, uint32_t, void *))
272 		dlsym(ark->d_handle, "rte_pmd_ark_mac_addr_add");
273 	ark->user_ext.mac_addr_remove =
274 		(void (*)(struct rte_eth_dev *, uint32_t, void *))
275 		dlsym(ark->d_handle, "rte_pmd_ark_mac_addr_remove");
276 	ark->user_ext.mac_addr_set =
277 		(void (*)(struct rte_eth_dev *, struct rte_ether_addr *,
278 			  void *))
279 		dlsym(ark->d_handle, "rte_pmd_ark_mac_addr_set");
280 	ark->user_ext.set_mtu =
281 		(int (*)(struct rte_eth_dev *, uint16_t,
282 			  void *))
283 		dlsym(ark->d_handle, "rte_pmd_ark_set_mtu");
284 	ark->user_ext.rx_user_meta_hook =
285 		(rx_user_meta_hook_fn)dlsym(ark->d_handle,
286 					    "rte_pmd_ark_rx_user_meta_hook");
287 	ark->user_ext.tx_user_meta_hook =
288 		(tx_user_meta_hook_fn)dlsym(ark->d_handle,
289 					    "rte_pmd_ark_tx_user_meta_hook");
290 
291 	return found;
292 }
293 
294 static int
295 eth_ark_dev_init(struct rte_eth_dev *dev)
296 {
297 	struct ark_adapter *ark = dev->data->dev_private;
298 	struct rte_pci_device *pci_dev;
299 	int ret;
300 	int port_count = 1;
301 	int p;
302 	bool rqpacing = false;
303 
304 	ark->eth_dev = dev;
305 
306 	ARK_PMD_LOG(DEBUG, "\n");
307 
308 	/* Check to see if there is an extension that we need to load */
309 	ret = check_for_ext(ark);
310 	if (ret)
311 		return ret;
312 
313 	pci_dev = RTE_ETH_DEV_TO_PCI(dev);
314 	rte_eth_copy_pci_info(dev, pci_dev);
315 	dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
316 
317 	p = 0;
318 	while (ark_device_caps[p].device_id != 0) {
319 		if (pci_dev->id.device_id == ark_device_caps[p].device_id) {
320 			rqpacing = ark_device_caps[p].caps.rqpacing;
321 			ark->isvf = ark_device_caps[p].caps.isvf;
322 			break;
323 		}
324 		p++;
325 	}
326 
327 	/* Use dummy function until setup */
328 	dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
329 	dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
330 
331 	ark->bar0 = (uint8_t *)pci_dev->mem_resource[0].addr;
332 	ark->a_bar = (uint8_t *)pci_dev->mem_resource[2].addr;
333 
334 	ark->sysctrl.v  = (void *)&ark->bar0[ARK_SYSCTRL_BASE];
335 	ark->mpurx.v  = (void *)&ark->bar0[ARK_MPU_RX_BASE];
336 	ark->udm.v  = (void *)&ark->bar0[ARK_UDM_BASE];
337 	ark->mputx.v  = (void *)&ark->bar0[ARK_MPU_TX_BASE];
338 	ark->ddm.v  = (void *)&ark->bar0[ARK_DDM_BASE];
339 	ark->cmac.v  = (void *)&ark->bar0[ARK_CMAC_BASE];
340 	ark->external.v  = (void *)&ark->bar0[ARK_EXTERNAL_BASE];
341 	ark->pktdir.v  = (void *)&ark->bar0[ARK_PKTDIR_BASE];
342 	ark->pktgen.v  = (void *)&ark->bar0[ARK_PKTGEN_BASE];
343 	ark->pktchkr.v  = (void *)&ark->bar0[ARK_PKTCHKR_BASE];
344 
345 	if (rqpacing) {
346 		ark->rqpacing =
347 			(struct ark_rqpace_t *)(ark->bar0 + ARK_RCPACING_BASE);
348 	} else {
349 		ark->rqpacing = NULL;
350 	}
351 	ark->started = 0;
352 	ark->pkt_dir_v = ARK_PKT_DIR_INIT_VAL;
353 
354 	ARK_PMD_LOG(INFO, "Sys Ctrl Const = 0x%x  HW Commit_ID: %08x\n",
355 		      ark->sysctrl.t32[4],
356 		      rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
357 	ARK_PMD_LOG(NOTICE, "Arkville HW Commit_ID: %08x\n",
358 		    rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
359 
360 	/* If HW sanity test fails, return an error */
361 	if (ark->sysctrl.t32[4] != 0xcafef00d) {
362 		ARK_PMD_LOG(ERR,
363 			    "HW Sanity test has failed, expected constant"
364 			    " 0x%x, read 0x%x (%s)\n",
365 			    0xcafef00d,
366 			    ark->sysctrl.t32[4], __func__);
367 		return -1;
368 	}
369 	if (ark->sysctrl.t32[3] != 0) {
370 		if (ark->rqpacing) {
371 			if (ark_rqp_lasped(ark->rqpacing)) {
372 				ARK_PMD_LOG(ERR, "Arkville Evaluation System - "
373 					    "Timer has Expired\n");
374 				return -1;
375 			}
376 			ARK_PMD_LOG(WARNING, "Arkville Evaluation System - "
377 				    "Timer is Running\n");
378 		}
379 	}
380 
381 	ARK_PMD_LOG(DEBUG,
382 		    "HW Sanity test has PASSED, expected constant"
383 		    " 0x%x, read 0x%x (%s)\n",
384 		    0xcafef00d, ark->sysctrl.t32[4], __func__);
385 
386 	/* We are a single function multi-port device. */
387 	ret = ark_config_device(dev);
388 	if (ret)
389 		return -1;
390 
391 	dev->dev_ops = &ark_eth_dev_ops;
392 	dev->rx_queue_count = eth_ark_dev_rx_queue_count;
393 
394 	dev->data->mac_addrs = rte_zmalloc("ark", RTE_ETHER_ADDR_LEN, 0);
395 	if (!dev->data->mac_addrs) {
396 		ARK_PMD_LOG(ERR,
397 			    "Failed to allocated memory for storing mac address"
398 			    );
399 	}
400 
401 	if (ark->user_ext.dev_init) {
402 		ark->user_data[dev->data->port_id] =
403 			ark->user_ext.dev_init(dev, ark->a_bar, 0);
404 		if (!ark->user_data[dev->data->port_id]) {
405 			ARK_PMD_LOG(WARNING,
406 				    "Failed to initialize PMD extension!"
407 				    " continuing without it\n");
408 			memset(&ark->user_ext, 0, sizeof(struct ark_user_ext));
409 			dlclose(ark->d_handle);
410 		}
411 	}
412 
413 	if (pci_dev->device.devargs)
414 		ret = eth_ark_check_args(ark, pci_dev->device.devargs->args);
415 	else
416 		ARK_PMD_LOG(INFO, "No Device args found\n");
417 
418 	if (ret)
419 		goto error;
420 	/*
421 	 * We will create additional devices based on the number of requested
422 	 * ports
423 	 */
424 	if (ark->user_ext.dev_get_port_count)
425 		port_count =
426 			ark->user_ext.dev_get_port_count(dev,
427 				 ark->user_data[dev->data->port_id]);
428 	ark->num_ports = port_count;
429 
430 	for (p = 0; p < port_count; p++) {
431 		struct rte_eth_dev *eth_dev;
432 		char name[RTE_ETH_NAME_MAX_LEN];
433 
434 		snprintf(name, sizeof(name), "arketh%d",
435 			 dev->data->port_id + p);
436 
437 		if (p == 0) {
438 			/* First port is already allocated by DPDK */
439 			eth_dev = ark->eth_dev;
440 			rte_eth_dev_probing_finish(eth_dev);
441 			continue;
442 		}
443 
444 		/* reserve an ethdev entry */
445 		eth_dev = rte_eth_dev_allocate(name);
446 		if (!eth_dev) {
447 			ARK_PMD_LOG(ERR,
448 				    "Could not allocate eth_dev for port %d\n",
449 				    p);
450 			goto error;
451 		}
452 
453 		eth_dev->device = &pci_dev->device;
454 		eth_dev->data->dev_private = ark;
455 		eth_dev->dev_ops = ark->eth_dev->dev_ops;
456 		eth_dev->tx_pkt_burst = ark->eth_dev->tx_pkt_burst;
457 		eth_dev->rx_pkt_burst = ark->eth_dev->rx_pkt_burst;
458 
459 		rte_eth_copy_pci_info(eth_dev, pci_dev);
460 		eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
461 
462 		eth_dev->data->mac_addrs = rte_zmalloc(name,
463 						RTE_ETHER_ADDR_LEN, 0);
464 		if (!eth_dev->data->mac_addrs) {
465 			ARK_PMD_LOG(ERR,
466 				    "Memory allocation for MAC failed!"
467 				    " Exiting.\n");
468 			goto error;
469 		}
470 
471 		if (ark->user_ext.dev_init) {
472 			ark->user_data[eth_dev->data->port_id] =
473 				ark->user_ext.dev_init(dev, ark->a_bar, p);
474 		}
475 
476 		rte_eth_dev_probing_finish(eth_dev);
477 	}
478 
479 	return ret;
480 
481 error:
482 	rte_free(dev->data->mac_addrs);
483 	dev->data->mac_addrs = NULL;
484 	return -1;
485 }
486 
487 /*
488  *Initial device configuration when device is opened
489  * setup the DDM, and UDM
490  * Called once per PCIE device
491  */
492 static int
493 ark_config_device(struct rte_eth_dev *dev)
494 {
495 	struct ark_adapter *ark = dev->data->dev_private;
496 	uint16_t num_q, i;
497 	struct ark_mpu_t *mpu;
498 
499 	/*
500 	 * Make sure that the packet director, generator and checker are in a
501 	 * known state
502 	 */
503 	if (!ark->isvf) {
504 		ark->start_pg = 0;
505 		ark->pg_running = 0;
506 		ark->pg = ark_pktgen_init(ark->pktgen.v, 0, 1);
507 		if (ark->pg == NULL)
508 			return -1;
509 		ark_pktgen_reset(ark->pg);
510 		ark->pc = ark_pktchkr_init(ark->pktchkr.v, 0, 1);
511 		if (ark->pc == NULL)
512 			return -1;
513 		ark_pktchkr_stop(ark->pc);
514 		ark->pd = ark_pktdir_init(ark->pktdir.v);
515 		if (ark->pd == NULL)
516 			return -1;
517 	}
518 	/* Verify HW */
519 	if (ark_udm_verify(ark->udm.v))
520 		return -1;
521 	if (ark_ddm_verify(ark->ddm.v))
522 		return -1;
523 
524 	/* MPU reset */
525 	mpu = ark->mpurx.v;
526 	num_q = ark_api_num_queues(mpu);
527 	ark->rx_queues = num_q;
528 	for (i = 0; i < num_q; i++) {
529 		mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
530 	}
531 
532 	mpu = ark->mputx.v;
533 	num_q = ark_api_num_queues(mpu);
534 	ark->tx_queues = num_q;
535 	for (i = 0; i < num_q; i++) {
536 		mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
537 	}
538 
539 	if (!ark->isvf && ark->rqpacing)
540 		ark_rqp_stats_reset(ark->rqpacing);
541 
542 	return 0;
543 }
544 
545 static int
546 eth_ark_dev_uninit(struct rte_eth_dev *dev)
547 {
548 	struct ark_adapter *ark = dev->data->dev_private;
549 
550 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
551 		return 0;
552 
553 	if (ark->user_ext.dev_uninit)
554 		ark->user_ext.dev_uninit(dev,
555 			 ark->user_data[dev->data->port_id]);
556 
557 	if (!ark->isvf) {
558 		ark_pktgen_uninit(ark->pg);
559 		ark_pktchkr_uninit(ark->pc);
560 	}
561 
562 	return 0;
563 }
564 
565 static int
566 eth_ark_dev_configure(struct rte_eth_dev *dev)
567 {
568 	struct ark_adapter *ark = dev->data->dev_private;
569 
570 	eth_ark_dev_set_link_up(dev);
571 	if (ark->user_ext.dev_configure)
572 		return ark->user_ext.dev_configure(dev,
573 			   ark->user_data[dev->data->port_id]);
574 	return 0;
575 }
576 
577 static int
578 eth_ark_dev_start(struct rte_eth_dev *dev)
579 {
580 	struct ark_adapter *ark = dev->data->dev_private;
581 	int i;
582 
583 	/* RX Side */
584 	for (i = 0; i < dev->data->nb_rx_queues; i++)
585 		eth_ark_rx_start_queue(dev, i);
586 
587 	/* TX Side */
588 	for (i = 0; i < dev->data->nb_tx_queues; i++)
589 		eth_ark_tx_queue_start(dev, i);
590 
591 	ark->started = 1;
592 	/* set xmit and receive function */
593 	dev->rx_pkt_burst = &eth_ark_recv_pkts;
594 	dev->tx_pkt_burst = &eth_ark_xmit_pkts;
595 
596 	if (!ark->isvf && ark->start_pg)
597 		ark_pktchkr_run(ark->pc);
598 
599 	if (!ark->isvf && ark->start_pg && !ark->pg_running) {
600 		pthread_t thread;
601 
602 		/* Delay packet generatpr start allow the hardware to be ready
603 		 * This is only used for sanity checking with internal generator
604 		 */
605 		char tname[32];
606 		snprintf(tname, sizeof(tname), "ark-delay-pg-%d",
607 			 dev->data->port_id);
608 
609 		if (rte_ctrl_thread_create(&thread, tname, NULL,
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