xref: /dpdk/drivers/net/ark/ark_ethdev.c (revision f30e69b41f949cd4a9afb6ff39de196e661708e2)
1540914bcSEd Czeck /* SPDX-License-Identifier: BSD-3-Clause
2540914bcSEd Czeck  * Copyright (c) 2015-2018 Atomic Rules LLC
31131cbf0SEd Czeck  */
41131cbf0SEd Czeck 
51131cbf0SEd Czeck #include <unistd.h>
61131cbf0SEd Czeck #include <sys/stat.h>
71131cbf0SEd Czeck #include <dlfcn.h>
81131cbf0SEd Czeck 
9c752998bSGaetan Rivet #include <rte_bus_pci.h>
101131cbf0SEd Czeck #include <rte_ethdev_pci.h>
111131cbf0SEd Czeck #include <rte_kvargs.h>
121131cbf0SEd Czeck 
131131cbf0SEd Czeck #include "ark_global.h"
141131cbf0SEd Czeck #include "ark_logs.h"
15727b3fe2SEd Czeck #include "ark_ethdev_tx.h"
16727b3fe2SEd Czeck #include "ark_ethdev_rx.h"
17727b3fe2SEd Czeck #include "ark_mpu.h"
18727b3fe2SEd Czeck #include "ark_ddm.h"
19727b3fe2SEd Czeck #include "ark_udm.h"
20727b3fe2SEd Czeck #include "ark_rqp.h"
21727b3fe2SEd Czeck #include "ark_pktdir.h"
22727b3fe2SEd Czeck #include "ark_pktgen.h"
23727b3fe2SEd Czeck #include "ark_pktchkr.h"
241131cbf0SEd Czeck 
251131cbf0SEd Czeck /*  Internal prototypes */
261131cbf0SEd Czeck static int eth_ark_check_args(struct ark_adapter *ark, const char *params);
271131cbf0SEd Czeck static int eth_ark_dev_init(struct rte_eth_dev *dev);
28727b3fe2SEd Czeck static int ark_config_device(struct rte_eth_dev *dev);
291131cbf0SEd Czeck static int eth_ark_dev_uninit(struct rte_eth_dev *eth_dev);
301131cbf0SEd Czeck static int eth_ark_dev_configure(struct rte_eth_dev *dev);
31727b3fe2SEd Czeck static int eth_ark_dev_start(struct rte_eth_dev *dev);
3262024eb8SIvan Ilchenko static int eth_ark_dev_stop(struct rte_eth_dev *dev);
33b142387bSThomas Monjalon static int eth_ark_dev_close(struct rte_eth_dev *dev);
34bdad90d1SIvan Ilchenko static int eth_ark_dev_info_get(struct rte_eth_dev *dev,
351131cbf0SEd Czeck 				struct rte_eth_dev_info *dev_info);
36727b3fe2SEd Czeck static int eth_ark_dev_link_update(struct rte_eth_dev *dev,
37727b3fe2SEd Czeck 				   int wait_to_complete);
38727b3fe2SEd Czeck static int eth_ark_dev_set_link_up(struct rte_eth_dev *dev);
39727b3fe2SEd Czeck static int eth_ark_dev_set_link_down(struct rte_eth_dev *dev);
40d5b0924bSMatan Azrad static int eth_ark_dev_stats_get(struct rte_eth_dev *dev,
41727b3fe2SEd Czeck 				  struct rte_eth_stats *stats);
429970a9adSIgor Romanov static int eth_ark_dev_stats_reset(struct rte_eth_dev *dev);
43caccf8b3SOlivier Matz static int eth_ark_set_default_mac_addr(struct rte_eth_dev *dev,
446d13ea8eSOlivier Matz 					 struct rte_ether_addr *mac_addr);
456d01e580SWei Dai static int eth_ark_macaddr_add(struct rte_eth_dev *dev,
466d13ea8eSOlivier Matz 			       struct rte_ether_addr *mac_addr,
47727b3fe2SEd Czeck 			       uint32_t index,
48727b3fe2SEd Czeck 			       uint32_t pool);
49727b3fe2SEd Czeck static void eth_ark_macaddr_remove(struct rte_eth_dev *dev,
50727b3fe2SEd Czeck 				   uint32_t index);
519ae74488SJohn Miller static int  eth_ark_set_mtu(struct rte_eth_dev *dev, uint16_t size);
521131cbf0SEd Czeck 
531131cbf0SEd Czeck /*
541131cbf0SEd Czeck  * The packet generator is a functional block used to generate packet
551131cbf0SEd Czeck  * patterns for testing.  It is not intended for nominal use.
561131cbf0SEd Czeck  */
571131cbf0SEd Czeck #define ARK_PKTGEN_ARG "Pkt_gen"
581131cbf0SEd Czeck 
591131cbf0SEd Czeck /*
601131cbf0SEd Czeck  * The packet checker is a functional block used to verify packet
611131cbf0SEd Czeck  * patterns for testing.  It is not intended for nominal use.
621131cbf0SEd Czeck  */
631131cbf0SEd Czeck #define ARK_PKTCHKR_ARG "Pkt_chkr"
641131cbf0SEd Czeck 
651131cbf0SEd Czeck /*
661131cbf0SEd Czeck  * The packet director is used to select the internal ingress and
671131cbf0SEd Czeck  * egress packets paths during testing.  It is not intended for
681131cbf0SEd Czeck  * nominal use.
691131cbf0SEd Czeck  */
701131cbf0SEd Czeck #define ARK_PKTDIR_ARG "Pkt_dir"
711131cbf0SEd Czeck 
721131cbf0SEd Czeck /* Devinfo configurations */
731131cbf0SEd Czeck #define ARK_RX_MAX_QUEUE (4096 * 4)
741131cbf0SEd Czeck #define ARK_RX_MIN_QUEUE (512)
751131cbf0SEd Czeck #define ARK_RX_MAX_PKT_LEN ((16 * 1024) - 128)
761131cbf0SEd Czeck #define ARK_RX_MIN_BUFSIZE (1024)
771131cbf0SEd Czeck 
781131cbf0SEd Czeck #define ARK_TX_MAX_QUEUE (4096 * 4)
791131cbf0SEd Czeck #define ARK_TX_MIN_QUEUE (256)
801131cbf0SEd Czeck 
811131cbf0SEd Czeck static const char * const valid_arguments[] = {
821131cbf0SEd Czeck 	ARK_PKTGEN_ARG,
831131cbf0SEd Czeck 	ARK_PKTCHKR_ARG,
841131cbf0SEd Czeck 	ARK_PKTDIR_ARG,
851131cbf0SEd Czeck 	NULL
861131cbf0SEd Czeck };
871131cbf0SEd Czeck 
881131cbf0SEd Czeck static const struct rte_pci_id pci_id_ark_map[] = {
891131cbf0SEd Czeck 	{RTE_PCI_DEVICE(0x1d6c, 0x100d)},
901131cbf0SEd Czeck 	{RTE_PCI_DEVICE(0x1d6c, 0x100e)},
911131cbf0SEd Czeck 	{.vendor_id = 0, /* sentinel */ },
921131cbf0SEd Czeck };
931131cbf0SEd Czeck 
941131cbf0SEd Czeck static int
951131cbf0SEd Czeck eth_ark_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
961131cbf0SEd Czeck 		struct rte_pci_device *pci_dev)
971131cbf0SEd Czeck {
981131cbf0SEd Czeck 	struct rte_eth_dev *eth_dev;
991131cbf0SEd Czeck 	int ret;
1001131cbf0SEd Czeck 
1011131cbf0SEd Czeck 	eth_dev = rte_eth_dev_pci_allocate(pci_dev, sizeof(struct ark_adapter));
1021131cbf0SEd Czeck 
1031131cbf0SEd Czeck 	if (eth_dev == NULL)
1041131cbf0SEd Czeck 		return -ENOMEM;
1051131cbf0SEd Czeck 
1061131cbf0SEd Czeck 	ret = eth_ark_dev_init(eth_dev);
1071131cbf0SEd Czeck 	if (ret)
1082c65898bSThomas Monjalon 		rte_eth_dev_release_port(eth_dev);
1091131cbf0SEd Czeck 
1101131cbf0SEd Czeck 	return ret;
1111131cbf0SEd Czeck }
1121131cbf0SEd Czeck 
1131131cbf0SEd Czeck static int
1141131cbf0SEd Czeck eth_ark_pci_remove(struct rte_pci_device *pci_dev)
1151131cbf0SEd Czeck {
1161131cbf0SEd Czeck 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_ark_dev_uninit);
1171131cbf0SEd Czeck }
1181131cbf0SEd Czeck 
1191131cbf0SEd Czeck static struct rte_pci_driver rte_ark_pmd = {
1201131cbf0SEd Czeck 	.id_table = pci_id_ark_map,
1211131cbf0SEd Czeck 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1221131cbf0SEd Czeck 	.probe = eth_ark_pci_probe,
1231131cbf0SEd Czeck 	.remove = eth_ark_pci_remove,
1241131cbf0SEd Czeck };
1251131cbf0SEd Czeck 
1261131cbf0SEd Czeck static const struct eth_dev_ops ark_eth_dev_ops = {
1271131cbf0SEd Czeck 	.dev_configure = eth_ark_dev_configure,
128727b3fe2SEd Czeck 	.dev_start = eth_ark_dev_start,
129727b3fe2SEd Czeck 	.dev_stop = eth_ark_dev_stop,
130727b3fe2SEd Czeck 	.dev_close = eth_ark_dev_close,
131727b3fe2SEd Czeck 
1321131cbf0SEd Czeck 	.dev_infos_get = eth_ark_dev_info_get,
133727b3fe2SEd Czeck 
134727b3fe2SEd Czeck 	.rx_queue_setup = eth_ark_dev_rx_queue_setup,
135727b3fe2SEd Czeck 	.tx_queue_setup = eth_ark_tx_queue_setup,
136727b3fe2SEd Czeck 
137727b3fe2SEd Czeck 	.link_update = eth_ark_dev_link_update,
138727b3fe2SEd Czeck 	.dev_set_link_up = eth_ark_dev_set_link_up,
139727b3fe2SEd Czeck 	.dev_set_link_down = eth_ark_dev_set_link_down,
140727b3fe2SEd Czeck 
141727b3fe2SEd Czeck 	.rx_queue_start = eth_ark_rx_start_queue,
142727b3fe2SEd Czeck 	.rx_queue_stop = eth_ark_rx_stop_queue,
143727b3fe2SEd Czeck 
144727b3fe2SEd Czeck 	.tx_queue_start = eth_ark_tx_queue_start,
145727b3fe2SEd Czeck 	.tx_queue_stop = eth_ark_tx_queue_stop,
146727b3fe2SEd Czeck 
147727b3fe2SEd Czeck 	.stats_get = eth_ark_dev_stats_get,
148727b3fe2SEd Czeck 	.stats_reset = eth_ark_dev_stats_reset,
149727b3fe2SEd Czeck 
150727b3fe2SEd Czeck 	.mac_addr_add = eth_ark_macaddr_add,
151727b3fe2SEd Czeck 	.mac_addr_remove = eth_ark_macaddr_remove,
152727b3fe2SEd Czeck 	.mac_addr_set = eth_ark_set_default_mac_addr,
1539ae74488SJohn Miller 
1549ae74488SJohn Miller 	.mtu_set = eth_ark_set_mtu,
1551131cbf0SEd Czeck };
1561131cbf0SEd Czeck 
1571131cbf0SEd Czeck static int
158727b3fe2SEd Czeck check_for_ext(struct ark_adapter *ark)
159727b3fe2SEd Czeck {
160727b3fe2SEd Czeck 	int found = 0;
161727b3fe2SEd Czeck 
162727b3fe2SEd Czeck 	/* Get the env */
163727b3fe2SEd Czeck 	const char *dllpath = getenv("ARK_EXT_PATH");
164727b3fe2SEd Czeck 
165727b3fe2SEd Czeck 	if (dllpath == NULL) {
1661502d443SEd Czeck 		ARK_PMD_LOG(DEBUG, "EXT NO dll path specified\n");
167727b3fe2SEd Czeck 		return 0;
168727b3fe2SEd Czeck 	}
1691502d443SEd Czeck 	ARK_PMD_LOG(NOTICE, "EXT found dll path at %s\n", dllpath);
170727b3fe2SEd Czeck 
171727b3fe2SEd Czeck 	/* Open and load the .so */
172727b3fe2SEd Czeck 	ark->d_handle = dlopen(dllpath, RTLD_LOCAL | RTLD_LAZY);
173727b3fe2SEd Czeck 	if (ark->d_handle == NULL) {
1741502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Could not load user extension %s\n",
175727b3fe2SEd Czeck 			    dllpath);
176727b3fe2SEd Czeck 		return -1;
177727b3fe2SEd Czeck 	}
1781502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "SUCCESS: loaded user extension %s\n",
179727b3fe2SEd Czeck 			    dllpath);
180727b3fe2SEd Czeck 
181727b3fe2SEd Czeck 	/* Get the entry points */
182727b3fe2SEd Czeck 	ark->user_ext.dev_init =
183727b3fe2SEd Czeck 		(void *(*)(struct rte_eth_dev *, void *, int))
184727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_init");
1851502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "device ext init pointer = %p\n",
186727b3fe2SEd Czeck 		      ark->user_ext.dev_init);
187727b3fe2SEd Czeck 	ark->user_ext.dev_get_port_count =
188727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, void *))
189727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_get_port_count");
190727b3fe2SEd Czeck 	ark->user_ext.dev_uninit =
191727b3fe2SEd Czeck 		(void (*)(struct rte_eth_dev *, void *))
192727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_uninit");
193727b3fe2SEd Czeck 	ark->user_ext.dev_configure =
194727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, void *))
195727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_configure");
196727b3fe2SEd Czeck 	ark->user_ext.dev_start =
197727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, void *))
198727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_start");
199727b3fe2SEd Czeck 	ark->user_ext.dev_stop =
200727b3fe2SEd Czeck 		(void (*)(struct rte_eth_dev *, void *))
201727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_stop");
202727b3fe2SEd Czeck 	ark->user_ext.dev_close =
203727b3fe2SEd Czeck 		(void (*)(struct rte_eth_dev *, void *))
204727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_close");
205727b3fe2SEd Czeck 	ark->user_ext.link_update =
206727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, int, void *))
207727b3fe2SEd Czeck 		dlsym(ark->d_handle, "link_update");
208727b3fe2SEd Czeck 	ark->user_ext.dev_set_link_up =
209727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, void *))
210727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_set_link_up");
211727b3fe2SEd Czeck 	ark->user_ext.dev_set_link_down =
212727b3fe2SEd Czeck 		(int (*)(struct rte_eth_dev *, void *))
213727b3fe2SEd Czeck 		dlsym(ark->d_handle, "dev_set_link_down");
214727b3fe2SEd Czeck 	ark->user_ext.stats_get =
215d5b0924bSMatan Azrad 		(int (*)(struct rte_eth_dev *, struct rte_eth_stats *,
216727b3fe2SEd Czeck 			  void *))
217727b3fe2SEd Czeck 		dlsym(ark->d_handle, "stats_get");
218727b3fe2SEd Czeck 	ark->user_ext.stats_reset =
219727b3fe2SEd Czeck 		(void (*)(struct rte_eth_dev *, void *))
220727b3fe2SEd Czeck 		dlsym(ark->d_handle, "stats_reset");
221727b3fe2SEd Czeck 	ark->user_ext.mac_addr_add =
2226d13ea8eSOlivier Matz 		(void (*)(struct rte_eth_dev *, struct rte_ether_addr *,
2236d13ea8eSOlivier Matz 			uint32_t, uint32_t, void *))
224727b3fe2SEd Czeck 		dlsym(ark->d_handle, "mac_addr_add");
225727b3fe2SEd Czeck 	ark->user_ext.mac_addr_remove =
226727b3fe2SEd Czeck 		(void (*)(struct rte_eth_dev *, uint32_t, void *))
227727b3fe2SEd Czeck 		dlsym(ark->d_handle, "mac_addr_remove");
228727b3fe2SEd Czeck 	ark->user_ext.mac_addr_set =
2296d13ea8eSOlivier Matz 		(void (*)(struct rte_eth_dev *, struct rte_ether_addr *,
230727b3fe2SEd Czeck 			  void *))
231727b3fe2SEd Czeck 		dlsym(ark->d_handle, "mac_addr_set");
2329ae74488SJohn Miller 	ark->user_ext.set_mtu =
2339ae74488SJohn Miller 		(int (*)(struct rte_eth_dev *, uint16_t,
2349ae74488SJohn Miller 			  void *))
2359ae74488SJohn Miller 		dlsym(ark->d_handle, "set_mtu");
236727b3fe2SEd Czeck 
237727b3fe2SEd Czeck 	return found;
238727b3fe2SEd Czeck }
239727b3fe2SEd Czeck 
240727b3fe2SEd Czeck static int
2411131cbf0SEd Czeck eth_ark_dev_init(struct rte_eth_dev *dev)
2421131cbf0SEd Czeck {
2430bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
2441131cbf0SEd Czeck 	struct rte_pci_device *pci_dev;
245727b3fe2SEd Czeck 	int ret;
246727b3fe2SEd Czeck 	int port_count = 1;
247727b3fe2SEd Czeck 	int p;
2481131cbf0SEd Czeck 
2491131cbf0SEd Czeck 	ark->eth_dev = dev;
2501131cbf0SEd Czeck 
2511502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "\n");
2521131cbf0SEd Czeck 
253727b3fe2SEd Czeck 	/* Check to see if there is an extension that we need to load */
254727b3fe2SEd Czeck 	ret = check_for_ext(ark);
255727b3fe2SEd Czeck 	if (ret)
256727b3fe2SEd Czeck 		return ret;
257c0802544SFerruh Yigit 	pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2581131cbf0SEd Czeck 	rte_eth_copy_pci_info(dev, pci_dev);
259*f30e69b4SFerruh Yigit 	dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
2601131cbf0SEd Czeck 
261727b3fe2SEd Czeck 	/* Use dummy function until setup */
262727b3fe2SEd Czeck 	dev->rx_pkt_burst = &eth_ark_recv_pkts_noop;
263727b3fe2SEd Czeck 	dev->tx_pkt_burst = &eth_ark_xmit_pkts_noop;
264727b3fe2SEd Czeck 
2651131cbf0SEd Czeck 	ark->bar0 = (uint8_t *)pci_dev->mem_resource[0].addr;
2661131cbf0SEd Czeck 	ark->a_bar = (uint8_t *)pci_dev->mem_resource[2].addr;
2671131cbf0SEd Czeck 
268727b3fe2SEd Czeck 	ark->sysctrl.v  = (void *)&ark->bar0[ARK_SYSCTRL_BASE];
269727b3fe2SEd Czeck 	ark->mpurx.v  = (void *)&ark->bar0[ARK_MPU_RX_BASE];
270727b3fe2SEd Czeck 	ark->udm.v  = (void *)&ark->bar0[ARK_UDM_BASE];
271727b3fe2SEd Czeck 	ark->mputx.v  = (void *)&ark->bar0[ARK_MPU_TX_BASE];
272727b3fe2SEd Czeck 	ark->ddm.v  = (void *)&ark->bar0[ARK_DDM_BASE];
273727b3fe2SEd Czeck 	ark->cmac.v  = (void *)&ark->bar0[ARK_CMAC_BASE];
274727b3fe2SEd Czeck 	ark->external.v  = (void *)&ark->bar0[ARK_EXTERNAL_BASE];
275727b3fe2SEd Czeck 	ark->pktdir.v  = (void *)&ark->bar0[ARK_PKTDIR_BASE];
276727b3fe2SEd Czeck 	ark->pktgen.v  = (void *)&ark->bar0[ARK_PKTGEN_BASE];
277727b3fe2SEd Czeck 	ark->pktchkr.v  = (void *)&ark->bar0[ARK_PKTCHKR_BASE];
278727b3fe2SEd Czeck 
279727b3fe2SEd Czeck 	ark->rqpacing =
280727b3fe2SEd Czeck 		(struct ark_rqpace_t *)(ark->bar0 + ARK_RCPACING_BASE);
281727b3fe2SEd Czeck 	ark->started = 0;
282727b3fe2SEd Czeck 
2831502d443SEd Czeck 	ARK_PMD_LOG(INFO, "Sys Ctrl Const = 0x%x  HW Commit_ID: %08x\n",
284727b3fe2SEd Czeck 		      ark->sysctrl.t32[4],
285727b3fe2SEd Czeck 		      rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
2861502d443SEd Czeck 	ARK_PMD_LOG(NOTICE, "Arkville HW Commit_ID: %08x\n",
287727b3fe2SEd Czeck 		    rte_be_to_cpu_32(ark->sysctrl.t32[0x20 / 4]));
288727b3fe2SEd Czeck 
289727b3fe2SEd Czeck 	/* If HW sanity test fails, return an error */
290727b3fe2SEd Czeck 	if (ark->sysctrl.t32[4] != 0xcafef00d) {
2911502d443SEd Czeck 		ARK_PMD_LOG(ERR,
292727b3fe2SEd Czeck 			    "HW Sanity test has failed, expected constant"
293727b3fe2SEd Czeck 			    " 0x%x, read 0x%x (%s)\n",
294727b3fe2SEd Czeck 			    0xcafef00d,
295727b3fe2SEd Czeck 			    ark->sysctrl.t32[4], __func__);
296727b3fe2SEd Czeck 		return -1;
297727b3fe2SEd Czeck 	}
2989ff8fe95SEd Czeck 	if (ark->sysctrl.t32[3] != 0) {
2999ff8fe95SEd Czeck 		if (ark_rqp_lasped(ark->rqpacing)) {
3001502d443SEd Czeck 			ARK_PMD_LOG(ERR, "Arkville Evaluation System - "
3019ff8fe95SEd Czeck 				    "Timer has Expired\n");
3029ff8fe95SEd Czeck 			return -1;
3039ff8fe95SEd Czeck 		}
3041502d443SEd Czeck 		ARK_PMD_LOG(WARNING, "Arkville Evaluation System - "
3059ff8fe95SEd Czeck 			    "Timer is Running\n");
3069ff8fe95SEd Czeck 	}
307727b3fe2SEd Czeck 
3081502d443SEd Czeck 	ARK_PMD_LOG(DEBUG,
309727b3fe2SEd Czeck 		    "HW Sanity test has PASSED, expected constant"
310727b3fe2SEd Czeck 		    " 0x%x, read 0x%x (%s)\n",
311727b3fe2SEd Czeck 		    0xcafef00d, ark->sysctrl.t32[4], __func__);
312727b3fe2SEd Czeck 
313727b3fe2SEd Czeck 	/* We are a single function multi-port device. */
314727b3fe2SEd Czeck 	ret = ark_config_device(dev);
315589876bfSEd Czeck 	if (ret)
316589876bfSEd Czeck 		return -1;
317589876bfSEd Czeck 
3181131cbf0SEd Czeck 	dev->dev_ops = &ark_eth_dev_ops;
319cbfc6111SFerruh Yigit 	dev->rx_queue_count = eth_ark_dev_rx_queue_count;
3201131cbf0SEd Czeck 
32135b2d13fSOlivier Matz 	dev->data->mac_addrs = rte_zmalloc("ark", RTE_ETHER_ADDR_LEN, 0);
322727b3fe2SEd Czeck 	if (!dev->data->mac_addrs) {
3231502d443SEd Czeck 		ARK_PMD_LOG(ERR,
324727b3fe2SEd Czeck 			    "Failed to allocated memory for storing mac address"
325727b3fe2SEd Czeck 			    );
326727b3fe2SEd Czeck 	}
327727b3fe2SEd Czeck 
328727b3fe2SEd Czeck 	if (ark->user_ext.dev_init) {
329428046b4SJohn Miller 		ark->user_data[dev->data->port_id] =
330428046b4SJohn Miller 			ark->user_ext.dev_init(dev, ark->a_bar, 0);
331428046b4SJohn Miller 		if (!ark->user_data[dev->data->port_id]) {
3321502d443SEd Czeck 			ARK_PMD_LOG(WARNING,
333727b3fe2SEd Czeck 				    "Failed to initialize PMD extension!"
334727b3fe2SEd Czeck 				    " continuing without it\n");
335727b3fe2SEd Czeck 			memset(&ark->user_ext, 0, sizeof(struct ark_user_ext));
336727b3fe2SEd Czeck 			dlclose(ark->d_handle);
337727b3fe2SEd Czeck 		}
338727b3fe2SEd Czeck 	}
339727b3fe2SEd Czeck 
3401131cbf0SEd Czeck 	if (pci_dev->device.devargs)
3411131cbf0SEd Czeck 		ret = eth_ark_check_args(ark, pci_dev->device.devargs->args);
3421131cbf0SEd Czeck 	else
3431502d443SEd Czeck 		ARK_PMD_LOG(INFO, "No Device args found\n");
3441131cbf0SEd Czeck 
345727b3fe2SEd Czeck 	if (ret)
346727b3fe2SEd Czeck 		goto error;
347727b3fe2SEd Czeck 	/*
348727b3fe2SEd Czeck 	 * We will create additional devices based on the number of requested
349727b3fe2SEd Czeck 	 * ports
350727b3fe2SEd Czeck 	 */
351727b3fe2SEd Czeck 	if (ark->user_ext.dev_get_port_count)
352727b3fe2SEd Czeck 		port_count =
353428046b4SJohn Miller 			ark->user_ext.dev_get_port_count(dev,
354428046b4SJohn Miller 				 ark->user_data[dev->data->port_id]);
355727b3fe2SEd Czeck 	ark->num_ports = port_count;
356727b3fe2SEd Czeck 
357727b3fe2SEd Czeck 	for (p = 0; p < port_count; p++) {
358727b3fe2SEd Czeck 		struct rte_eth_dev *eth_dev;
359727b3fe2SEd Czeck 		char name[RTE_ETH_NAME_MAX_LEN];
360727b3fe2SEd Czeck 
361727b3fe2SEd Czeck 		snprintf(name, sizeof(name), "arketh%d",
362727b3fe2SEd Czeck 			 dev->data->port_id + p);
363727b3fe2SEd Czeck 
364727b3fe2SEd Czeck 		if (p == 0) {
365727b3fe2SEd Czeck 			/* First port is already allocated by DPDK */
366727b3fe2SEd Czeck 			eth_dev = ark->eth_dev;
3675bfe551dSEd Czeck 			rte_eth_dev_probing_finish(eth_dev);
368727b3fe2SEd Czeck 			continue;
369727b3fe2SEd Czeck 		}
370727b3fe2SEd Czeck 
371727b3fe2SEd Czeck 		/* reserve an ethdev entry */
372727b3fe2SEd Czeck 		eth_dev = rte_eth_dev_allocate(name);
373727b3fe2SEd Czeck 		if (!eth_dev) {
3741502d443SEd Czeck 			ARK_PMD_LOG(ERR,
375727b3fe2SEd Czeck 				    "Could not allocate eth_dev for port %d\n",
376727b3fe2SEd Czeck 				    p);
377727b3fe2SEd Czeck 			goto error;
378727b3fe2SEd Czeck 		}
379727b3fe2SEd Czeck 
380727b3fe2SEd Czeck 		eth_dev->device = &pci_dev->device;
381727b3fe2SEd Czeck 		eth_dev->data->dev_private = ark;
382727b3fe2SEd Czeck 		eth_dev->dev_ops = ark->eth_dev->dev_ops;
383727b3fe2SEd Czeck 		eth_dev->tx_pkt_burst = ark->eth_dev->tx_pkt_burst;
384727b3fe2SEd Czeck 		eth_dev->rx_pkt_burst = ark->eth_dev->rx_pkt_burst;
385727b3fe2SEd Czeck 
386727b3fe2SEd Czeck 		rte_eth_copy_pci_info(eth_dev, pci_dev);
387*f30e69b4SFerruh Yigit 		eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
388727b3fe2SEd Czeck 
38935b2d13fSOlivier Matz 		eth_dev->data->mac_addrs = rte_zmalloc(name,
39035b2d13fSOlivier Matz 						RTE_ETHER_ADDR_LEN, 0);
391727b3fe2SEd Czeck 		if (!eth_dev->data->mac_addrs) {
3921502d443SEd Czeck 			ARK_PMD_LOG(ERR,
393727b3fe2SEd Czeck 				    "Memory allocation for MAC failed!"
394727b3fe2SEd Czeck 				    " Exiting.\n");
395727b3fe2SEd Czeck 			goto error;
396727b3fe2SEd Czeck 		}
397727b3fe2SEd Czeck 
398428046b4SJohn Miller 		if (ark->user_ext.dev_init) {
399428046b4SJohn Miller 			ark->user_data[eth_dev->data->port_id] =
400727b3fe2SEd Czeck 				ark->user_ext.dev_init(dev, ark->a_bar, p);
401727b3fe2SEd Czeck 		}
402fbe90cddSThomas Monjalon 
403fbe90cddSThomas Monjalon 		rte_eth_dev_probing_finish(eth_dev);
404428046b4SJohn Miller 	}
405727b3fe2SEd Czeck 
4061131cbf0SEd Czeck 	return ret;
407727b3fe2SEd Czeck 
408727b3fe2SEd Czeck error:
409727b3fe2SEd Czeck 	rte_free(dev->data->mac_addrs);
410e7f2fa88SDavid Marchand 	dev->data->mac_addrs = NULL;
411727b3fe2SEd Czeck 	return -1;
412727b3fe2SEd Czeck }
413727b3fe2SEd Czeck 
414727b3fe2SEd Czeck /*
415727b3fe2SEd Czeck  *Initial device configuration when device is opened
416727b3fe2SEd Czeck  * setup the DDM, and UDM
417727b3fe2SEd Czeck  * Called once per PCIE device
418727b3fe2SEd Czeck  */
419727b3fe2SEd Czeck static int
420727b3fe2SEd Czeck ark_config_device(struct rte_eth_dev *dev)
421727b3fe2SEd Czeck {
4220bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
423727b3fe2SEd Czeck 	uint16_t num_q, i;
424727b3fe2SEd Czeck 	struct ark_mpu_t *mpu;
425727b3fe2SEd Czeck 
426727b3fe2SEd Czeck 	/*
427727b3fe2SEd Czeck 	 * Make sure that the packet director, generator and checker are in a
428727b3fe2SEd Czeck 	 * known state
429727b3fe2SEd Czeck 	 */
430727b3fe2SEd Czeck 	ark->start_pg = 0;
431727b3fe2SEd Czeck 	ark->pg = ark_pktgen_init(ark->pktgen.v, 0, 1);
43253a9ba13SYong Wang 	if (ark->pg == NULL)
43353a9ba13SYong Wang 		return -1;
434727b3fe2SEd Czeck 	ark_pktgen_reset(ark->pg);
435727b3fe2SEd Czeck 	ark->pc = ark_pktchkr_init(ark->pktchkr.v, 0, 1);
43653a9ba13SYong Wang 	if (ark->pc == NULL)
43753a9ba13SYong Wang 		return -1;
438727b3fe2SEd Czeck 	ark_pktchkr_stop(ark->pc);
439727b3fe2SEd Czeck 	ark->pd = ark_pktdir_init(ark->pktdir.v);
44053a9ba13SYong Wang 	if (ark->pd == NULL)
44153a9ba13SYong Wang 		return -1;
442727b3fe2SEd Czeck 
443727b3fe2SEd Czeck 	/* Verify HW */
444727b3fe2SEd Czeck 	if (ark_udm_verify(ark->udm.v))
445727b3fe2SEd Czeck 		return -1;
446727b3fe2SEd Czeck 	if (ark_ddm_verify(ark->ddm.v))
447727b3fe2SEd Czeck 		return -1;
448727b3fe2SEd Czeck 
449727b3fe2SEd Czeck 	/* UDM */
450727b3fe2SEd Czeck 	if (ark_udm_reset(ark->udm.v)) {
4511502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to stop and reset UDM\n");
452727b3fe2SEd Czeck 		return -1;
453727b3fe2SEd Czeck 	}
454727b3fe2SEd Czeck 	/* Keep in reset until the MPU are cleared */
455727b3fe2SEd Czeck 
456727b3fe2SEd Czeck 	/* MPU reset */
457727b3fe2SEd Czeck 	mpu = ark->mpurx.v;
458727b3fe2SEd Czeck 	num_q = ark_api_num_queues(mpu);
459727b3fe2SEd Czeck 	ark->rx_queues = num_q;
460727b3fe2SEd Czeck 	for (i = 0; i < num_q; i++) {
461727b3fe2SEd Czeck 		ark_mpu_reset(mpu);
462727b3fe2SEd Czeck 		mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
463727b3fe2SEd Czeck 	}
464727b3fe2SEd Czeck 
465727b3fe2SEd Czeck 	ark_udm_stop(ark->udm.v, 0);
466727b3fe2SEd Czeck 	ark_udm_configure(ark->udm.v,
467727b3fe2SEd Czeck 			  RTE_PKTMBUF_HEADROOM,
468727b3fe2SEd Czeck 			  RTE_MBUF_DEFAULT_DATAROOM,
469727b3fe2SEd Czeck 			  ARK_RX_WRITE_TIME_NS);
470727b3fe2SEd Czeck 	ark_udm_stats_reset(ark->udm.v);
471727b3fe2SEd Czeck 	ark_udm_stop(ark->udm.v, 0);
472727b3fe2SEd Czeck 
473727b3fe2SEd Czeck 	/* TX -- DDM */
474727b3fe2SEd Czeck 	if (ark_ddm_stop(ark->ddm.v, 1))
4751502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to stop DDM\n");
476727b3fe2SEd Czeck 
477727b3fe2SEd Czeck 	mpu = ark->mputx.v;
478727b3fe2SEd Czeck 	num_q = ark_api_num_queues(mpu);
479727b3fe2SEd Czeck 	ark->tx_queues = num_q;
480727b3fe2SEd Czeck 	for (i = 0; i < num_q; i++) {
481727b3fe2SEd Czeck 		ark_mpu_reset(mpu);
482727b3fe2SEd Czeck 		mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
483727b3fe2SEd Czeck 	}
484727b3fe2SEd Czeck 
485727b3fe2SEd Czeck 	ark_ddm_reset(ark->ddm.v);
486727b3fe2SEd Czeck 	ark_ddm_stats_reset(ark->ddm.v);
487727b3fe2SEd Czeck 
488727b3fe2SEd Czeck 	ark_ddm_stop(ark->ddm.v, 0);
489727b3fe2SEd Czeck 	ark_rqp_stats_reset(ark->rqpacing);
490727b3fe2SEd Czeck 
491727b3fe2SEd Czeck 	return 0;
4921131cbf0SEd Czeck }
4931131cbf0SEd Czeck 
4941131cbf0SEd Czeck static int
4951131cbf0SEd Czeck eth_ark_dev_uninit(struct rte_eth_dev *dev)
4961131cbf0SEd Czeck {
4970bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
498727b3fe2SEd Czeck 
4991131cbf0SEd Czeck 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5001131cbf0SEd Czeck 		return 0;
5011131cbf0SEd Czeck 
502727b3fe2SEd Czeck 	if (ark->user_ext.dev_uninit)
503428046b4SJohn Miller 		ark->user_ext.dev_uninit(dev,
504428046b4SJohn Miller 			 ark->user_data[dev->data->port_id]);
505727b3fe2SEd Czeck 
506727b3fe2SEd Czeck 	ark_pktgen_uninit(ark->pg);
507727b3fe2SEd Czeck 	ark_pktchkr_uninit(ark->pc);
508727b3fe2SEd Czeck 
5091131cbf0SEd Czeck 	return 0;
5101131cbf0SEd Czeck }
5111131cbf0SEd Czeck 
5121131cbf0SEd Czeck static int
513727b3fe2SEd Czeck eth_ark_dev_configure(struct rte_eth_dev *dev)
5141131cbf0SEd Czeck {
5150bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
516727b3fe2SEd Czeck 
517727b3fe2SEd Czeck 	eth_ark_dev_set_link_up(dev);
518727b3fe2SEd Czeck 	if (ark->user_ext.dev_configure)
519428046b4SJohn Miller 		return ark->user_ext.dev_configure(dev,
520428046b4SJohn Miller 			   ark->user_data[dev->data->port_id]);
5211131cbf0SEd Czeck 	return 0;
5221131cbf0SEd Czeck }
5231131cbf0SEd Czeck 
524727b3fe2SEd Czeck static void *
525727b3fe2SEd Czeck delay_pg_start(void *arg)
526727b3fe2SEd Czeck {
527727b3fe2SEd Czeck 	struct ark_adapter *ark = (struct ark_adapter *)arg;
528727b3fe2SEd Czeck 
529727b3fe2SEd Czeck 	/* This function is used exclusively for regression testing, We
530727b3fe2SEd Czeck 	 * perform a blind sleep here to ensure that the external test
531727b3fe2SEd Czeck 	 * application has time to setup the test before we generate packets
532727b3fe2SEd Czeck 	 */
533727b3fe2SEd Czeck 	usleep(100000);
534727b3fe2SEd Czeck 	ark_pktgen_run(ark->pg);
535727b3fe2SEd Czeck 	return NULL;
536727b3fe2SEd Czeck }
537727b3fe2SEd Czeck 
538727b3fe2SEd Czeck static int
539727b3fe2SEd Czeck eth_ark_dev_start(struct rte_eth_dev *dev)
540727b3fe2SEd Czeck {
5410bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
542727b3fe2SEd Czeck 	int i;
543727b3fe2SEd Czeck 
544727b3fe2SEd Czeck 	/* RX Side */
545727b3fe2SEd Czeck 	/* start UDM */
546727b3fe2SEd Czeck 	ark_udm_start(ark->udm.v);
547727b3fe2SEd Czeck 
548727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_rx_queues; i++)
549727b3fe2SEd Czeck 		eth_ark_rx_start_queue(dev, i);
550727b3fe2SEd Czeck 
551727b3fe2SEd Czeck 	/* TX Side */
552727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_tx_queues; i++)
553727b3fe2SEd Czeck 		eth_ark_tx_queue_start(dev, i);
554727b3fe2SEd Czeck 
555727b3fe2SEd Czeck 	/* start DDM */
556727b3fe2SEd Czeck 	ark_ddm_start(ark->ddm.v);
557727b3fe2SEd Czeck 
558727b3fe2SEd Czeck 	ark->started = 1;
559727b3fe2SEd Czeck 	/* set xmit and receive function */
560727b3fe2SEd Czeck 	dev->rx_pkt_burst = &eth_ark_recv_pkts;
561727b3fe2SEd Czeck 	dev->tx_pkt_burst = &eth_ark_xmit_pkts;
562727b3fe2SEd Czeck 
563727b3fe2SEd Czeck 	if (ark->start_pg)
564727b3fe2SEd Czeck 		ark_pktchkr_run(ark->pc);
565727b3fe2SEd Czeck 
566727b3fe2SEd Czeck 	if (ark->start_pg && (dev->data->port_id == 0)) {
567727b3fe2SEd Czeck 		pthread_t thread;
568727b3fe2SEd Czeck 
569727b3fe2SEd Czeck 		/* Delay packet generatpr start allow the hardware to be ready
570727b3fe2SEd Czeck 		 * This is only used for sanity checking with internal generator
571727b3fe2SEd Czeck 		 */
572d1434c04SJohn Miller 		if (pthread_create(&thread, NULL, delay_pg_start, ark)) {
5731502d443SEd Czeck 			ARK_PMD_LOG(ERR, "Could not create pktgen "
574d1434c04SJohn Miller 				    "starter thread\n");
575d1434c04SJohn Miller 			return -1;
576d1434c04SJohn Miller 		}
577727b3fe2SEd Czeck 	}
578727b3fe2SEd Czeck 
579727b3fe2SEd Czeck 	if (ark->user_ext.dev_start)
580428046b4SJohn Miller 		ark->user_ext.dev_start(dev,
581428046b4SJohn Miller 			ark->user_data[dev->data->port_id]);
582727b3fe2SEd Czeck 
583727b3fe2SEd Czeck 	return 0;
584727b3fe2SEd Czeck }
585727b3fe2SEd Czeck 
58662024eb8SIvan Ilchenko static int
587727b3fe2SEd Czeck eth_ark_dev_stop(struct rte_eth_dev *dev)
588727b3fe2SEd Czeck {
589727b3fe2SEd Czeck 	uint16_t i;
590727b3fe2SEd Czeck 	int status;
5910bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
592727b3fe2SEd Czeck 	struct ark_mpu_t *mpu;
593727b3fe2SEd Czeck 
594727b3fe2SEd Czeck 	if (ark->started == 0)
59562024eb8SIvan Ilchenko 		return 0;
596727b3fe2SEd Czeck 	ark->started = 0;
597b8f5d2aeSThomas Monjalon 	dev->data->dev_started = 0;
598727b3fe2SEd Czeck 
599727b3fe2SEd Czeck 	/* Stop the extension first */
600727b3fe2SEd Czeck 	if (ark->user_ext.dev_stop)
601428046b4SJohn Miller 		ark->user_ext.dev_stop(dev,
602428046b4SJohn Miller 		       ark->user_data[dev->data->port_id]);
603727b3fe2SEd Czeck 
604727b3fe2SEd Czeck 	/* Stop the packet generator */
605727b3fe2SEd Czeck 	if (ark->start_pg)
606727b3fe2SEd Czeck 		ark_pktgen_pause(ark->pg);
607727b3fe2SEd Czeck 
608727b3fe2SEd Czeck 	dev->rx_pkt_burst = &eth_ark_recv_pkts_noop;
609727b3fe2SEd Czeck 	dev->tx_pkt_burst = &eth_ark_xmit_pkts_noop;
610727b3fe2SEd Czeck 
611727b3fe2SEd Czeck 	/* STOP TX Side */
612727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
613727b3fe2SEd Czeck 		status = eth_ark_tx_queue_stop(dev, i);
614727b3fe2SEd Czeck 		if (status != 0) {
615f8244c63SZhiyong Yang 			uint16_t port = dev->data->port_id;
6161502d443SEd Czeck 			ARK_PMD_LOG(ERR,
617727b3fe2SEd Czeck 				    "tx_queue stop anomaly"
618727b3fe2SEd Czeck 				    " port %u, queue %u\n",
619727b3fe2SEd Czeck 				    port, i);
620727b3fe2SEd Czeck 		}
621727b3fe2SEd Czeck 	}
622727b3fe2SEd Czeck 
623727b3fe2SEd Czeck 	/* Stop DDM */
624727b3fe2SEd Czeck 	/* Wait up to 0.1 second.  each stop is up to 1000 * 10 useconds */
625727b3fe2SEd Czeck 	for (i = 0; i < 10; i++) {
626727b3fe2SEd Czeck 		status = ark_ddm_stop(ark->ddm.v, 1);
627727b3fe2SEd Czeck 		if (status == 0)
628727b3fe2SEd Czeck 			break;
629727b3fe2SEd Czeck 	}
630727b3fe2SEd Czeck 	if (status || i != 0) {
6311502d443SEd Czeck 		ARK_PMD_LOG(ERR, "DDM stop anomaly. status:"
632727b3fe2SEd Czeck 			    " %d iter: %u. (%s)\n",
633727b3fe2SEd Czeck 			    status,
634727b3fe2SEd Czeck 			    i,
635727b3fe2SEd Czeck 			    __func__);
636727b3fe2SEd Czeck 		ark_ddm_dump(ark->ddm.v, "Stop anomaly");
637727b3fe2SEd Czeck 
638727b3fe2SEd Czeck 		mpu = ark->mputx.v;
639727b3fe2SEd Czeck 		for (i = 0; i < ark->tx_queues; i++) {
640727b3fe2SEd Czeck 			ark_mpu_dump(mpu, "DDM failure dump", i);
641727b3fe2SEd Czeck 			mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
642727b3fe2SEd Czeck 		}
643727b3fe2SEd Czeck 	}
644727b3fe2SEd Czeck 
645727b3fe2SEd Czeck 	/* STOP RX Side */
646727b3fe2SEd Czeck 	/* Stop UDM  multiple tries attempted */
647727b3fe2SEd Czeck 	for (i = 0; i < 10; i++) {
648727b3fe2SEd Czeck 		status = ark_udm_stop(ark->udm.v, 1);
649727b3fe2SEd Czeck 		if (status == 0)
650727b3fe2SEd Czeck 			break;
651727b3fe2SEd Czeck 	}
652727b3fe2SEd Czeck 	if (status || i != 0) {
6531502d443SEd Czeck 		ARK_PMD_LOG(ERR, "UDM stop anomaly. status %d iter: %u. (%s)\n",
654727b3fe2SEd Czeck 			    status, i, __func__);
655727b3fe2SEd Czeck 		ark_udm_dump(ark->udm.v, "Stop anomaly");
656727b3fe2SEd Czeck 
657727b3fe2SEd Czeck 		mpu = ark->mpurx.v;
658727b3fe2SEd Czeck 		for (i = 0; i < ark->rx_queues; i++) {
659727b3fe2SEd Czeck 			ark_mpu_dump(mpu, "UDM Stop anomaly", i);
660727b3fe2SEd Czeck 			mpu = RTE_PTR_ADD(mpu, ARK_MPU_QOFFSET);
661727b3fe2SEd Czeck 		}
662727b3fe2SEd Czeck 	}
663727b3fe2SEd Czeck 
664727b3fe2SEd Czeck 	ark_udm_dump_stats(ark->udm.v, "Post stop");
665727b3fe2SEd Czeck 	ark_udm_dump_perf(ark->udm.v, "Post stop");
666727b3fe2SEd Czeck 
66722862a02SJohn Miller 	for (i = 0; i < dev->data->nb_rx_queues; i++)
668727b3fe2SEd Czeck 		eth_ark_rx_dump_queue(dev, i, __func__);
669727b3fe2SEd Czeck 
670727b3fe2SEd Czeck 	/* Stop the packet checker if it is running */
671727b3fe2SEd Czeck 	if (ark->start_pg) {
672727b3fe2SEd Czeck 		ark_pktchkr_dump_stats(ark->pc);
673727b3fe2SEd Czeck 		ark_pktchkr_stop(ark->pc);
674727b3fe2SEd Czeck 	}
67562024eb8SIvan Ilchenko 
67662024eb8SIvan Ilchenko 	return 0;
677727b3fe2SEd Czeck }
678727b3fe2SEd Czeck 
679b142387bSThomas Monjalon static int
680727b3fe2SEd Czeck eth_ark_dev_close(struct rte_eth_dev *dev)
681727b3fe2SEd Czeck {
6820bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
683727b3fe2SEd Czeck 	uint16_t i;
684727b3fe2SEd Czeck 
68530410493SThomas Monjalon 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
68630410493SThomas Monjalon 		return 0;
68730410493SThomas Monjalon 
688727b3fe2SEd Czeck 	if (ark->user_ext.dev_close)
689428046b4SJohn Miller 		ark->user_ext.dev_close(dev,
690428046b4SJohn Miller 		 ark->user_data[dev->data->port_id]);
691727b3fe2SEd Czeck 
692727b3fe2SEd Czeck 	eth_ark_dev_stop(dev);
693727b3fe2SEd Czeck 	eth_ark_udm_force_close(dev);
694727b3fe2SEd Czeck 
695727b3fe2SEd Czeck 	/*
696727b3fe2SEd Czeck 	 * TODO This should only be called once for the device during shutdown
697727b3fe2SEd Czeck 	 */
698727b3fe2SEd Czeck 	ark_rqp_dump(ark->rqpacing);
699727b3fe2SEd Czeck 
700727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
701727b3fe2SEd Czeck 		eth_ark_tx_queue_release(dev->data->tx_queues[i]);
702727b3fe2SEd Czeck 		dev->data->tx_queues[i] = 0;
703727b3fe2SEd Czeck 	}
704727b3fe2SEd Czeck 
705727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
706727b3fe2SEd Czeck 		eth_ark_dev_rx_queue_release(dev->data->rx_queues[i]);
707727b3fe2SEd Czeck 		dev->data->rx_queues[i] = 0;
708727b3fe2SEd Czeck 	}
709c5ddc9b9SEd Czeck 
710b142387bSThomas Monjalon 	return 0;
711727b3fe2SEd Czeck }
712727b3fe2SEd Czeck 
713bdad90d1SIvan Ilchenko static int
7141131cbf0SEd Czeck eth_ark_dev_info_get(struct rte_eth_dev *dev,
7151131cbf0SEd Czeck 		     struct rte_eth_dev_info *dev_info)
7161131cbf0SEd Czeck {
7170bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
718727b3fe2SEd Czeck 	struct ark_mpu_t *tx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_TX_BASE);
719727b3fe2SEd Czeck 	struct ark_mpu_t *rx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_RX_BASE);
720727b3fe2SEd Czeck 	uint16_t ports = ark->num_ports;
721727b3fe2SEd Czeck 
7221131cbf0SEd Czeck 	dev_info->max_rx_pktlen = ARK_RX_MAX_PKT_LEN;
7231131cbf0SEd Czeck 	dev_info->min_rx_bufsize = ARK_RX_MIN_BUFSIZE;
7241131cbf0SEd Czeck 
725727b3fe2SEd Czeck 	dev_info->max_rx_queues = ark_api_num_queues_per_port(rx_mpu, ports);
726727b3fe2SEd Czeck 	dev_info->max_tx_queues = ark_api_num_queues_per_port(tx_mpu, ports);
727727b3fe2SEd Czeck 
7281131cbf0SEd Czeck 	dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
7291131cbf0SEd Czeck 		.nb_max = ARK_RX_MAX_QUEUE,
7301131cbf0SEd Czeck 		.nb_min = ARK_RX_MIN_QUEUE,
7311131cbf0SEd Czeck 		.nb_align = ARK_RX_MIN_QUEUE}; /* power of 2 */
7321131cbf0SEd Czeck 
7331131cbf0SEd Czeck 	dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
7341131cbf0SEd Czeck 		.nb_max = ARK_TX_MAX_QUEUE,
7351131cbf0SEd Czeck 		.nb_min = ARK_TX_MIN_QUEUE,
7361131cbf0SEd Czeck 		.nb_align = ARK_TX_MIN_QUEUE}; /* power of 2 */
7371131cbf0SEd Czeck 
7381131cbf0SEd Czeck 	/* ARK PMD supports all line rates, how do we indicate that here ?? */
7391131cbf0SEd Czeck 	dev_info->speed_capa = (ETH_LINK_SPEED_1G |
7401131cbf0SEd Czeck 				ETH_LINK_SPEED_10G |
7411131cbf0SEd Czeck 				ETH_LINK_SPEED_25G |
7421131cbf0SEd Czeck 				ETH_LINK_SPEED_40G |
7431131cbf0SEd Czeck 				ETH_LINK_SPEED_50G |
7441131cbf0SEd Czeck 				ETH_LINK_SPEED_100G);
745bdad90d1SIvan Ilchenko 
746bdad90d1SIvan Ilchenko 	return 0;
7471131cbf0SEd Czeck }
7481131cbf0SEd Czeck 
749727b3fe2SEd Czeck static int
750727b3fe2SEd Czeck eth_ark_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
751727b3fe2SEd Czeck {
7521502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "link status = %d\n",
753727b3fe2SEd Czeck 			dev->data->dev_link.link_status);
7540bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
755727b3fe2SEd Czeck 
756727b3fe2SEd Czeck 	if (ark->user_ext.link_update) {
757727b3fe2SEd Czeck 		return ark->user_ext.link_update
758727b3fe2SEd Czeck 			(dev, wait_to_complete,
759428046b4SJohn Miller 			 ark->user_data[dev->data->port_id]);
760727b3fe2SEd Czeck 	}
761727b3fe2SEd Czeck 	return 0;
762727b3fe2SEd Czeck }
763727b3fe2SEd Czeck 
764727b3fe2SEd Czeck static int
765727b3fe2SEd Czeck eth_ark_dev_set_link_up(struct rte_eth_dev *dev)
766727b3fe2SEd Czeck {
767727b3fe2SEd Czeck 	dev->data->dev_link.link_status = 1;
7680bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
769727b3fe2SEd Czeck 
770727b3fe2SEd Czeck 	if (ark->user_ext.dev_set_link_up)
771428046b4SJohn Miller 		return ark->user_ext.dev_set_link_up(dev,
772428046b4SJohn Miller 			     ark->user_data[dev->data->port_id]);
773727b3fe2SEd Czeck 	return 0;
774727b3fe2SEd Czeck }
775727b3fe2SEd Czeck 
776727b3fe2SEd Czeck static int
777727b3fe2SEd Czeck eth_ark_dev_set_link_down(struct rte_eth_dev *dev)
778727b3fe2SEd Czeck {
779727b3fe2SEd Czeck 	dev->data->dev_link.link_status = 0;
7800bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
781727b3fe2SEd Czeck 
782727b3fe2SEd Czeck 	if (ark->user_ext.dev_set_link_down)
783428046b4SJohn Miller 		return ark->user_ext.dev_set_link_down(dev,
784428046b4SJohn Miller 		       ark->user_data[dev->data->port_id]);
785727b3fe2SEd Czeck 	return 0;
786727b3fe2SEd Czeck }
787727b3fe2SEd Czeck 
788d5b0924bSMatan Azrad static int
789727b3fe2SEd Czeck eth_ark_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
790727b3fe2SEd Czeck {
791727b3fe2SEd Czeck 	uint16_t i;
7920bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
793727b3fe2SEd Czeck 
794727b3fe2SEd Czeck 	stats->ipackets = 0;
795727b3fe2SEd Czeck 	stats->ibytes = 0;
796727b3fe2SEd Czeck 	stats->opackets = 0;
797727b3fe2SEd Czeck 	stats->obytes = 0;
798727b3fe2SEd Czeck 	stats->imissed = 0;
799727b3fe2SEd Czeck 	stats->oerrors = 0;
800727b3fe2SEd Czeck 
801727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_tx_queues; i++)
802727b3fe2SEd Czeck 		eth_tx_queue_stats_get(dev->data->tx_queues[i], stats);
803727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_rx_queues; i++)
804727b3fe2SEd Czeck 		eth_rx_queue_stats_get(dev->data->rx_queues[i], stats);
805727b3fe2SEd Czeck 	if (ark->user_ext.stats_get)
806d5b0924bSMatan Azrad 		return ark->user_ext.stats_get(dev, stats,
807428046b4SJohn Miller 			ark->user_data[dev->data->port_id]);
808d5b0924bSMatan Azrad 	return 0;
809727b3fe2SEd Czeck }
810727b3fe2SEd Czeck 
8119970a9adSIgor Romanov static int
812727b3fe2SEd Czeck eth_ark_dev_stats_reset(struct rte_eth_dev *dev)
813727b3fe2SEd Czeck {
814727b3fe2SEd Czeck 	uint16_t i;
8150bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
816727b3fe2SEd Czeck 
817727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_tx_queues; i++)
818f588d3a5SJohn Miller 		eth_tx_queue_stats_reset(dev->data->tx_queues[i]);
819727b3fe2SEd Czeck 	for (i = 0; i < dev->data->nb_rx_queues; i++)
820727b3fe2SEd Czeck 		eth_rx_queue_stats_reset(dev->data->rx_queues[i]);
821727b3fe2SEd Czeck 	if (ark->user_ext.stats_reset)
822428046b4SJohn Miller 		ark->user_ext.stats_reset(dev,
823428046b4SJohn Miller 			  ark->user_data[dev->data->port_id]);
8249970a9adSIgor Romanov 
8259970a9adSIgor Romanov 	return 0;
826727b3fe2SEd Czeck }
827727b3fe2SEd Czeck 
8286d01e580SWei Dai static int
829727b3fe2SEd Czeck eth_ark_macaddr_add(struct rte_eth_dev *dev,
8306d13ea8eSOlivier Matz 		    struct rte_ether_addr *mac_addr,
831727b3fe2SEd Czeck 		    uint32_t index,
832727b3fe2SEd Czeck 		    uint32_t pool)
833727b3fe2SEd Czeck {
8340bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
835727b3fe2SEd Czeck 
8366d01e580SWei Dai 	if (ark->user_ext.mac_addr_add) {
837727b3fe2SEd Czeck 		ark->user_ext.mac_addr_add(dev,
838727b3fe2SEd Czeck 					   mac_addr,
839727b3fe2SEd Czeck 					   index,
840727b3fe2SEd Czeck 					   pool,
841428046b4SJohn Miller 			   ark->user_data[dev->data->port_id]);
8426d01e580SWei Dai 		return 0;
8436d01e580SWei Dai 	}
8446d01e580SWei Dai 	return -ENOTSUP;
845727b3fe2SEd Czeck }
846727b3fe2SEd Czeck 
847727b3fe2SEd Czeck static void
848727b3fe2SEd Czeck eth_ark_macaddr_remove(struct rte_eth_dev *dev, uint32_t index)
849727b3fe2SEd Czeck {
8500bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
851727b3fe2SEd Czeck 
852727b3fe2SEd Czeck 	if (ark->user_ext.mac_addr_remove)
853428046b4SJohn Miller 		ark->user_ext.mac_addr_remove(dev, index,
854428046b4SJohn Miller 			      ark->user_data[dev->data->port_id]);
855727b3fe2SEd Czeck }
856727b3fe2SEd Czeck 
857caccf8b3SOlivier Matz static int
858727b3fe2SEd Czeck eth_ark_set_default_mac_addr(struct rte_eth_dev *dev,
8596d13ea8eSOlivier Matz 			     struct rte_ether_addr *mac_addr)
860727b3fe2SEd Czeck {
8610bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
862727b3fe2SEd Czeck 
863caccf8b3SOlivier Matz 	if (ark->user_ext.mac_addr_set) {
864428046b4SJohn Miller 		ark->user_ext.mac_addr_set(dev, mac_addr,
865428046b4SJohn Miller 			   ark->user_data[dev->data->port_id]);
866caccf8b3SOlivier Matz 		return 0;
867caccf8b3SOlivier Matz 	}
868caccf8b3SOlivier Matz 	return -ENOTSUP;
869727b3fe2SEd Czeck }
870727b3fe2SEd Czeck 
8719ae74488SJohn Miller static int
8729ae74488SJohn Miller eth_ark_set_mtu(struct rte_eth_dev *dev, uint16_t  size)
8739ae74488SJohn Miller {
8740bf8b0f1SStephen Hemminger 	struct ark_adapter *ark = dev->data->dev_private;
8759ae74488SJohn Miller 
8769ae74488SJohn Miller 	if (ark->user_ext.set_mtu)
8779ae74488SJohn Miller 		return ark->user_ext.set_mtu(dev, size,
8789ae74488SJohn Miller 			     ark->user_data[dev->data->port_id]);
8799ae74488SJohn Miller 
8809ae74488SJohn Miller 	return -ENOTSUP;
8819ae74488SJohn Miller }
8829ae74488SJohn Miller 
8831131cbf0SEd Czeck static inline int
8841131cbf0SEd Czeck process_pktdir_arg(const char *key, const char *value,
8851131cbf0SEd Czeck 		   void *extra_args)
8861131cbf0SEd Czeck {
8871502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "key = %s, value = %s\n",
8881131cbf0SEd Czeck 		    key, value);
8891131cbf0SEd Czeck 	struct ark_adapter *ark =
8901131cbf0SEd Czeck 		(struct ark_adapter *)extra_args;
8911131cbf0SEd Czeck 
8921131cbf0SEd Czeck 	ark->pkt_dir_v = strtol(value, NULL, 16);
8931502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "pkt_dir_v = 0x%x\n", ark->pkt_dir_v);
8941131cbf0SEd Czeck 	return 0;
8951131cbf0SEd Czeck }
8961131cbf0SEd Czeck 
8971131cbf0SEd Czeck static inline int
8981131cbf0SEd Czeck process_file_args(const char *key, const char *value, void *extra_args)
8991131cbf0SEd Czeck {
9001502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "key = %s, value = %s\n",
9011131cbf0SEd Czeck 		    key, value);
9021131cbf0SEd Czeck 	char *args = (char *)extra_args;
9031131cbf0SEd Czeck 
9041131cbf0SEd Czeck 	/* Open the configuration file */
9051131cbf0SEd Czeck 	FILE *file = fopen(value, "r");
9061131cbf0SEd Czeck 	char line[ARK_MAX_ARG_LEN];
9071131cbf0SEd Czeck 	int  size = 0;
9081131cbf0SEd Czeck 	int first = 1;
9091131cbf0SEd Czeck 
9102f3b88fbSJohn Miller 	if (file == NULL) {
9111502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to open "
9122f3b88fbSJohn Miller 			    "config file %s\n", value);
9132f3b88fbSJohn Miller 		return -1;
9142f3b88fbSJohn Miller 	}
9152f3b88fbSJohn Miller 
9161131cbf0SEd Czeck 	while (fgets(line, sizeof(line), file)) {
9171131cbf0SEd Czeck 		size += strlen(line);
9181131cbf0SEd Czeck 		if (size >= ARK_MAX_ARG_LEN) {
9191502d443SEd Czeck 			ARK_PMD_LOG(ERR, "Unable to parse file %s args, "
9201131cbf0SEd Czeck 				    "parameter list is too long\n", value);
9211131cbf0SEd Czeck 			fclose(file);
9221131cbf0SEd Czeck 			return -1;
9231131cbf0SEd Czeck 		}
9241131cbf0SEd Czeck 		if (first) {
9251131cbf0SEd Czeck 			strncpy(args, line, ARK_MAX_ARG_LEN);
9261131cbf0SEd Czeck 			first = 0;
9271131cbf0SEd Czeck 		} else {
9281131cbf0SEd Czeck 			strncat(args, line, ARK_MAX_ARG_LEN);
9291131cbf0SEd Czeck 		}
9301131cbf0SEd Czeck 	}
9311502d443SEd Czeck 	ARK_PMD_LOG(DEBUG, "file = %s\n", args);
9321131cbf0SEd Czeck 	fclose(file);
9331131cbf0SEd Czeck 	return 0;
9341131cbf0SEd Czeck }
9351131cbf0SEd Czeck 
9361131cbf0SEd Czeck static int
9371131cbf0SEd Czeck eth_ark_check_args(struct ark_adapter *ark, const char *params)
9381131cbf0SEd Czeck {
9391131cbf0SEd Czeck 	struct rte_kvargs *kvlist;
9401131cbf0SEd Czeck 	unsigned int k_idx;
9411131cbf0SEd Czeck 	struct rte_kvargs_pair *pair = NULL;
9420f31eb0cSFerruh Yigit 	int ret = -1;
9431131cbf0SEd Czeck 
9441131cbf0SEd Czeck 	kvlist = rte_kvargs_parse(params, valid_arguments);
9451131cbf0SEd Czeck 	if (kvlist == NULL)
9461131cbf0SEd Czeck 		return 0;
9471131cbf0SEd Czeck 
9481131cbf0SEd Czeck 	ark->pkt_gen_args[0] = 0;
9491131cbf0SEd Czeck 	ark->pkt_chkr_args[0] = 0;
9501131cbf0SEd Czeck 
9511131cbf0SEd Czeck 	for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
9521131cbf0SEd Czeck 		pair = &kvlist->pairs[k_idx];
9531502d443SEd Czeck 		ARK_PMD_LOG(DEBUG, "**** Arg passed to PMD = %s:%s\n",
9541131cbf0SEd Czeck 			     pair->key,
9551131cbf0SEd Czeck 			     pair->value);
9561131cbf0SEd Czeck 	}
9571131cbf0SEd Czeck 
9581131cbf0SEd Czeck 	if (rte_kvargs_process(kvlist,
9591131cbf0SEd Czeck 			       ARK_PKTDIR_ARG,
9601131cbf0SEd Czeck 			       &process_pktdir_arg,
9611131cbf0SEd Czeck 			       ark) != 0) {
9621502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTDIR_ARG);
9630f31eb0cSFerruh Yigit 		goto free_kvlist;
9641131cbf0SEd Czeck 	}
9651131cbf0SEd Czeck 
9661131cbf0SEd Czeck 	if (rte_kvargs_process(kvlist,
9671131cbf0SEd Czeck 			       ARK_PKTGEN_ARG,
9681131cbf0SEd Czeck 			       &process_file_args,
9691131cbf0SEd Czeck 			       ark->pkt_gen_args) != 0) {
9701502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTGEN_ARG);
9710f31eb0cSFerruh Yigit 		goto free_kvlist;
9721131cbf0SEd Czeck 	}
9731131cbf0SEd Czeck 
9741131cbf0SEd Czeck 	if (rte_kvargs_process(kvlist,
9751131cbf0SEd Czeck 			       ARK_PKTCHKR_ARG,
9761131cbf0SEd Czeck 			       &process_file_args,
9771131cbf0SEd Czeck 			       ark->pkt_chkr_args) != 0) {
9781502d443SEd Czeck 		ARK_PMD_LOG(ERR, "Unable to parse arg %s\n", ARK_PKTCHKR_ARG);
9790f31eb0cSFerruh Yigit 		goto free_kvlist;
9801131cbf0SEd Czeck 	}
9811131cbf0SEd Czeck 
9821502d443SEd Czeck 	ARK_PMD_LOG(INFO, "packet director set to 0x%x\n", ark->pkt_dir_v);
983727b3fe2SEd Czeck 	/* Setup the packet director */
984727b3fe2SEd Czeck 	ark_pktdir_setup(ark->pd, ark->pkt_dir_v);
985727b3fe2SEd Czeck 
986727b3fe2SEd Czeck 	/* Setup the packet generator */
987727b3fe2SEd Czeck 	if (ark->pkt_gen_args[0]) {
9881502d443SEd Czeck 		ARK_PMD_LOG(DEBUG, "Setting up the packet generator\n");
989727b3fe2SEd Czeck 		ark_pktgen_parse(ark->pkt_gen_args);
990727b3fe2SEd Czeck 		ark_pktgen_reset(ark->pg);
991727b3fe2SEd Czeck 		ark_pktgen_setup(ark->pg);
992727b3fe2SEd Czeck 		ark->start_pg = 1;
993727b3fe2SEd Czeck 	}
994727b3fe2SEd Czeck 
995727b3fe2SEd Czeck 	/* Setup the packet checker */
996727b3fe2SEd Czeck 	if (ark->pkt_chkr_args[0]) {
997727b3fe2SEd Czeck 		ark_pktchkr_parse(ark->pkt_chkr_args);
998727b3fe2SEd Czeck 		ark_pktchkr_setup(ark->pc);
999727b3fe2SEd Czeck 	}
10001131cbf0SEd Czeck 
10010f31eb0cSFerruh Yigit 	ret = 0;
10020f31eb0cSFerruh Yigit 
10030f31eb0cSFerruh Yigit free_kvlist:
10040f31eb0cSFerruh Yigit 	rte_kvargs_free(kvlist);
10050f31eb0cSFerruh Yigit 
10060f31eb0cSFerruh Yigit 	return ret;
10071131cbf0SEd Czeck }
10081131cbf0SEd Czeck 
10091131cbf0SEd Czeck RTE_PMD_REGISTER_PCI(net_ark, rte_ark_pmd);
10101131cbf0SEd Czeck RTE_PMD_REGISTER_KMOD_DEP(net_ark, "* igb_uio | uio_pci_generic ");
10111131cbf0SEd Czeck RTE_PMD_REGISTER_PCI_TABLE(net_ark, pci_id_ark_map);
10121131cbf0SEd Czeck RTE_PMD_REGISTER_PARAM_STRING(net_ark,
10131131cbf0SEd Czeck 			      ARK_PKTGEN_ARG "=<filename> "
10141131cbf0SEd Czeck 			      ARK_PKTCHKR_ARG "=<filename> "
10151131cbf0SEd Czeck 			      ARK_PKTDIR_ARG "=<bitmap>");
10169c99878aSJerin Jacob RTE_LOG_REGISTER(ark_logtype, pmd.net.ark, NOTICE);
1017