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