xref: /dpdk/drivers/net/octeontx/octeontx_ethdev.c (revision 56139e85abec19ac9e91aed610abdb0c3629a638)
1aaf4363eSJerin Jacob /* SPDX-License-Identifier: BSD-3-Clause
2aaf4363eSJerin Jacob  * Copyright(c) 2017 Cavium, Inc
3f7be70e5SJerin Jacob  */
4aaf4363eSJerin Jacob 
5f7be70e5SJerin Jacob #include <stdio.h>
6f7be70e5SJerin Jacob #include <stdarg.h>
7f7be70e5SJerin Jacob #include <stdbool.h>
8f7be70e5SJerin Jacob #include <stdint.h>
9f7be70e5SJerin Jacob #include <string.h>
10f7be70e5SJerin Jacob #include <unistd.h>
11f7be70e5SJerin Jacob 
12f7be70e5SJerin Jacob #include <rte_alarm.h>
13f7be70e5SJerin Jacob #include <rte_branch_prediction.h>
14f7be70e5SJerin Jacob #include <rte_debug.h>
15f7be70e5SJerin Jacob #include <rte_devargs.h>
16f7be70e5SJerin Jacob #include <rte_dev.h>
17f7be70e5SJerin Jacob #include <rte_kvargs.h>
18f7be70e5SJerin Jacob #include <rte_malloc.h>
19dfb0c75bSPavan Nikhilesh #include <rte_mbuf_pool_ops.h>
20f7be70e5SJerin Jacob #include <rte_prefetch.h>
21d4a586d2SJianfeng Tan #include <rte_bus_vdev.h>
22f7be70e5SJerin Jacob 
23f7be70e5SJerin Jacob #include "octeontx_ethdev.h"
2420186d43SJerin Jacob #include "octeontx_rxtx.h"
25f7be70e5SJerin Jacob #include "octeontx_logs.h"
26f7be70e5SJerin Jacob 
2785221a0cSHarman Kalra struct evdev_priv_data {
2885221a0cSHarman Kalra 	OFFLOAD_FLAGS; /*Sequence should not be changed */
2985221a0cSHarman Kalra } __rte_cache_aligned;
3085221a0cSHarman Kalra 
31f7be70e5SJerin Jacob struct octeontx_vdev_init_params {
32f7be70e5SJerin Jacob 	uint8_t	nr_port;
33f7be70e5SJerin Jacob };
34f7be70e5SJerin Jacob 
35989d4926SPavan Nikhilesh uint16_t
36989d4926SPavan Nikhilesh rte_octeontx_pchan_map[OCTEONTX_MAX_BGX_PORTS][OCTEONTX_MAX_LMAC_PER_BGX];
37989d4926SPavan Nikhilesh 
384fac7c0aSJerin Jacob enum octeontx_link_speed {
394fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_SGMII,
404fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_XAUI,
414fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_RXAUI,
424fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_10G_R,
434fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_40G_R,
444fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_RESERVE1,
454fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_QSGMII,
464fac7c0aSJerin Jacob 	OCTEONTX_LINK_SPEED_RESERVE2
474fac7c0aSJerin Jacob };
484fac7c0aSJerin Jacob 
494d35a276SPavan Nikhilesh int otx_net_logtype_mbox;
504d35a276SPavan Nikhilesh int otx_net_logtype_init;
514d35a276SPavan Nikhilesh int otx_net_logtype_driver;
524d35a276SPavan Nikhilesh 
53f8e99896SThomas Monjalon RTE_INIT(otx_net_init_log)
544d35a276SPavan Nikhilesh {
551a0eab3aSHarry van Haaren 	otx_net_logtype_mbox = rte_log_register("pmd.net.octeontx.mbox");
564d35a276SPavan Nikhilesh 	if (otx_net_logtype_mbox >= 0)
574d35a276SPavan Nikhilesh 		rte_log_set_level(otx_net_logtype_mbox, RTE_LOG_NOTICE);
584d35a276SPavan Nikhilesh 
591a0eab3aSHarry van Haaren 	otx_net_logtype_init = rte_log_register("pmd.net.octeontx.init");
604d35a276SPavan Nikhilesh 	if (otx_net_logtype_init >= 0)
614d35a276SPavan Nikhilesh 		rte_log_set_level(otx_net_logtype_init, RTE_LOG_NOTICE);
624d35a276SPavan Nikhilesh 
631a0eab3aSHarry van Haaren 	otx_net_logtype_driver = rte_log_register("pmd.net.octeontx.driver");
644d35a276SPavan Nikhilesh 	if (otx_net_logtype_driver >= 0)
654d35a276SPavan Nikhilesh 		rte_log_set_level(otx_net_logtype_driver, RTE_LOG_NOTICE);
664d35a276SPavan Nikhilesh }
674d35a276SPavan Nikhilesh 
68f7be70e5SJerin Jacob /* Parse integer from integer argument */
69f7be70e5SJerin Jacob static int
70f7be70e5SJerin Jacob parse_integer_arg(const char *key __rte_unused,
71f7be70e5SJerin Jacob 		const char *value, void *extra_args)
72f7be70e5SJerin Jacob {
73f7be70e5SJerin Jacob 	int *i = (int *)extra_args;
74f7be70e5SJerin Jacob 
75f7be70e5SJerin Jacob 	*i = atoi(value);
76f7be70e5SJerin Jacob 	if (*i < 0) {
77f7be70e5SJerin Jacob 		octeontx_log_err("argument has to be positive.");
78f7be70e5SJerin Jacob 		return -1;
79f7be70e5SJerin Jacob 	}
80f7be70e5SJerin Jacob 
81f7be70e5SJerin Jacob 	return 0;
82f7be70e5SJerin Jacob }
83f7be70e5SJerin Jacob 
84f7be70e5SJerin Jacob static int
85f7be70e5SJerin Jacob octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
86f7be70e5SJerin Jacob 				struct rte_vdev_device *dev)
87f7be70e5SJerin Jacob {
88f7be70e5SJerin Jacob 	struct rte_kvargs *kvlist = NULL;
89f7be70e5SJerin Jacob 	int ret = 0;
90f7be70e5SJerin Jacob 
91f7be70e5SJerin Jacob 	static const char * const octeontx_vdev_valid_params[] = {
92f7be70e5SJerin Jacob 		OCTEONTX_VDEV_NR_PORT_ARG,
93f7be70e5SJerin Jacob 		NULL
94f7be70e5SJerin Jacob 	};
95f7be70e5SJerin Jacob 
96f7be70e5SJerin Jacob 	const char *input_args = rte_vdev_device_args(dev);
97f7be70e5SJerin Jacob 	if (params == NULL)
98f7be70e5SJerin Jacob 		return -EINVAL;
99f7be70e5SJerin Jacob 
100f7be70e5SJerin Jacob 
101f7be70e5SJerin Jacob 	if (input_args) {
102f7be70e5SJerin Jacob 		kvlist = rte_kvargs_parse(input_args,
103f7be70e5SJerin Jacob 				octeontx_vdev_valid_params);
104f7be70e5SJerin Jacob 		if (kvlist == NULL)
105f7be70e5SJerin Jacob 			return -1;
106f7be70e5SJerin Jacob 
107f7be70e5SJerin Jacob 		ret = rte_kvargs_process(kvlist,
108f7be70e5SJerin Jacob 					OCTEONTX_VDEV_NR_PORT_ARG,
109f7be70e5SJerin Jacob 					&parse_integer_arg,
110f7be70e5SJerin Jacob 					&params->nr_port);
111f7be70e5SJerin Jacob 		if (ret < 0)
112f7be70e5SJerin Jacob 			goto free_kvlist;
113f7be70e5SJerin Jacob 	}
114f7be70e5SJerin Jacob 
115f7be70e5SJerin Jacob free_kvlist:
116f7be70e5SJerin Jacob 	rte_kvargs_free(kvlist);
117f7be70e5SJerin Jacob 	return ret;
118f7be70e5SJerin Jacob }
119f7be70e5SJerin Jacob 
120f18b146cSJerin Jacob static int
121f18b146cSJerin Jacob octeontx_port_open(struct octeontx_nic *nic)
122f18b146cSJerin Jacob {
123f18b146cSJerin Jacob 	octeontx_mbox_bgx_port_conf_t bgx_port_conf;
124f18b146cSJerin Jacob 	int res;
125f18b146cSJerin Jacob 
126f18b146cSJerin Jacob 	res = 0;
127a371fd79SSantosh Shukla 	memset(&bgx_port_conf, 0x0, sizeof(bgx_port_conf));
128f18b146cSJerin Jacob 	PMD_INIT_FUNC_TRACE();
129f18b146cSJerin Jacob 
130f18b146cSJerin Jacob 	res = octeontx_bgx_port_open(nic->port_id, &bgx_port_conf);
131f18b146cSJerin Jacob 	if (res < 0) {
132f18b146cSJerin Jacob 		octeontx_log_err("failed to open port %d", res);
133f18b146cSJerin Jacob 		return res;
134f18b146cSJerin Jacob 	}
135f18b146cSJerin Jacob 
136f18b146cSJerin Jacob 	nic->node = bgx_port_conf.node;
137f18b146cSJerin Jacob 	nic->port_ena = bgx_port_conf.enable;
138f18b146cSJerin Jacob 	nic->base_ichan = bgx_port_conf.base_chan;
139f18b146cSJerin Jacob 	nic->base_ochan = bgx_port_conf.base_chan;
140f18b146cSJerin Jacob 	nic->num_ichans = bgx_port_conf.num_chans;
141f18b146cSJerin Jacob 	nic->num_ochans = bgx_port_conf.num_chans;
1423151e6a6SHarman Kalra 	nic->bgx_mtu = bgx_port_conf.mtu;
143f18b146cSJerin Jacob 	nic->bpen = bgx_port_conf.bpen;
144f18b146cSJerin Jacob 	nic->fcs_strip = bgx_port_conf.fcs_strip;
145f18b146cSJerin Jacob 	nic->bcast_mode = bgx_port_conf.bcast_mode;
146f18b146cSJerin Jacob 	nic->mcast_mode = bgx_port_conf.mcast_mode;
147f18b146cSJerin Jacob 	nic->speed	= bgx_port_conf.mode;
148f18b146cSJerin Jacob 
14935b2d13fSOlivier Matz 	memcpy(&nic->mac_addr[0], &bgx_port_conf.macaddr[0],
15035b2d13fSOlivier Matz 		RTE_ETHER_ADDR_LEN);
151f18b146cSJerin Jacob 
152f18b146cSJerin Jacob 	octeontx_log_dbg("port opened %d", nic->port_id);
153f18b146cSJerin Jacob 	return res;
154f18b146cSJerin Jacob }
155f18b146cSJerin Jacob 
156f18b146cSJerin Jacob static void
157f18b146cSJerin Jacob octeontx_port_close(struct octeontx_nic *nic)
158f18b146cSJerin Jacob {
159f18b146cSJerin Jacob 	PMD_INIT_FUNC_TRACE();
160f18b146cSJerin Jacob 
161f18b146cSJerin Jacob 	octeontx_bgx_port_close(nic->port_id);
162f18b146cSJerin Jacob 	octeontx_log_dbg("port closed %d", nic->port_id);
163f18b146cSJerin Jacob }
164f18b146cSJerin Jacob 
1657fe7c98fSJerin Jacob static int
166da6c6874SJerin Jacob octeontx_port_start(struct octeontx_nic *nic)
167da6c6874SJerin Jacob {
168da6c6874SJerin Jacob 	PMD_INIT_FUNC_TRACE();
169da6c6874SJerin Jacob 
170da6c6874SJerin Jacob 	return octeontx_bgx_port_start(nic->port_id);
171da6c6874SJerin Jacob }
172da6c6874SJerin Jacob 
173da6c6874SJerin Jacob static int
1747fe7c98fSJerin Jacob octeontx_port_stop(struct octeontx_nic *nic)
1757fe7c98fSJerin Jacob {
1767fe7c98fSJerin Jacob 	PMD_INIT_FUNC_TRACE();
1777fe7c98fSJerin Jacob 
1787fe7c98fSJerin Jacob 	return octeontx_bgx_port_stop(nic->port_id);
1797fe7c98fSJerin Jacob }
1807fe7c98fSJerin Jacob 
1819039c812SAndrew Rybchenko static int
18215bce35bSJerin Jacob octeontx_port_promisc_set(struct octeontx_nic *nic, int en)
18315bce35bSJerin Jacob {
18415bce35bSJerin Jacob 	struct rte_eth_dev *dev;
18515bce35bSJerin Jacob 	int res;
18615bce35bSJerin Jacob 
18715bce35bSJerin Jacob 	res = 0;
18815bce35bSJerin Jacob 	PMD_INIT_FUNC_TRACE();
18915bce35bSJerin Jacob 	dev = nic->dev;
19015bce35bSJerin Jacob 
19115bce35bSJerin Jacob 	res = octeontx_bgx_port_promisc_set(nic->port_id, en);
1929039c812SAndrew Rybchenko 	if (res < 0) {
19315bce35bSJerin Jacob 		octeontx_log_err("failed to set promiscuous mode %d",
19415bce35bSJerin Jacob 				nic->port_id);
1959039c812SAndrew Rybchenko 		return res;
1969039c812SAndrew Rybchenko 	}
19715bce35bSJerin Jacob 
19815bce35bSJerin Jacob 	/* Set proper flag for the mode */
19915bce35bSJerin Jacob 	dev->data->promiscuous = (en != 0) ? 1 : 0;
20015bce35bSJerin Jacob 
20115bce35bSJerin Jacob 	octeontx_log_dbg("port %d : promiscuous mode %s",
20215bce35bSJerin Jacob 			nic->port_id, en ? "set" : "unset");
2039039c812SAndrew Rybchenko 
2049039c812SAndrew Rybchenko 	return 0;
20515bce35bSJerin Jacob }
20615bce35bSJerin Jacob 
207d5b0924bSMatan Azrad static int
20855389909SJerin Jacob octeontx_port_stats(struct octeontx_nic *nic, struct rte_eth_stats *stats)
20955389909SJerin Jacob {
21055389909SJerin Jacob 	octeontx_mbox_bgx_port_stats_t bgx_stats;
21155389909SJerin Jacob 	int res;
21255389909SJerin Jacob 
21355389909SJerin Jacob 	PMD_INIT_FUNC_TRACE();
21455389909SJerin Jacob 
21555389909SJerin Jacob 	res = octeontx_bgx_port_stats(nic->port_id, &bgx_stats);
216d5b0924bSMatan Azrad 	if (res < 0) {
21755389909SJerin Jacob 		octeontx_log_err("failed to get port stats %d", nic->port_id);
218d5b0924bSMatan Azrad 		return res;
219d5b0924bSMatan Azrad 	}
22055389909SJerin Jacob 
22155389909SJerin Jacob 	stats->ipackets = bgx_stats.rx_packets;
22255389909SJerin Jacob 	stats->ibytes = bgx_stats.rx_bytes;
22355389909SJerin Jacob 	stats->imissed = bgx_stats.rx_dropped;
22455389909SJerin Jacob 	stats->ierrors = bgx_stats.rx_errors;
22555389909SJerin Jacob 	stats->opackets = bgx_stats.tx_packets;
22655389909SJerin Jacob 	stats->obytes = bgx_stats.tx_bytes;
22755389909SJerin Jacob 	stats->oerrors = bgx_stats.tx_errors;
22855389909SJerin Jacob 
22955389909SJerin Jacob 	octeontx_log_dbg("port%d stats inpkts=%" PRIx64 " outpkts=%" PRIx64 "",
23055389909SJerin Jacob 			nic->port_id, stats->ipackets, stats->opackets);
231d5b0924bSMatan Azrad 
232d5b0924bSMatan Azrad 	return 0;
23355389909SJerin Jacob }
23455389909SJerin Jacob 
2359970a9adSIgor Romanov static int
23655389909SJerin Jacob octeontx_port_stats_clr(struct octeontx_nic *nic)
23755389909SJerin Jacob {
23855389909SJerin Jacob 	PMD_INIT_FUNC_TRACE();
23955389909SJerin Jacob 
2409970a9adSIgor Romanov 	return octeontx_bgx_port_stats_clr(nic->port_id);
24155389909SJerin Jacob }
24255389909SJerin Jacob 
243f7be70e5SJerin Jacob static inline void
244f7be70e5SJerin Jacob devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
245f7be70e5SJerin Jacob 				struct rte_event_dev_info *info)
246f7be70e5SJerin Jacob {
247f7be70e5SJerin Jacob 	memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
248f7be70e5SJerin Jacob 	dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
249f7be70e5SJerin Jacob 
250f7be70e5SJerin Jacob 	dev_conf->nb_event_ports = info->max_event_ports;
251f7be70e5SJerin Jacob 	dev_conf->nb_event_queues = info->max_event_queues;
252f7be70e5SJerin Jacob 
253f7be70e5SJerin Jacob 	dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
254f7be70e5SJerin Jacob 	dev_conf->nb_event_port_dequeue_depth =
255f7be70e5SJerin Jacob 			info->max_event_port_dequeue_depth;
256f7be70e5SJerin Jacob 	dev_conf->nb_event_port_enqueue_depth =
257f7be70e5SJerin Jacob 			info->max_event_port_enqueue_depth;
258f7be70e5SJerin Jacob 	dev_conf->nb_event_port_enqueue_depth =
259f7be70e5SJerin Jacob 			info->max_event_port_enqueue_depth;
260f7be70e5SJerin Jacob 	dev_conf->nb_events_limit =
261f7be70e5SJerin Jacob 			info->max_num_events;
262f7be70e5SJerin Jacob }
263f7be70e5SJerin Jacob 
26485221a0cSHarman Kalra static uint16_t
26585221a0cSHarman Kalra octeontx_tx_offload_flags(struct rte_eth_dev *eth_dev)
26685221a0cSHarman Kalra {
26785221a0cSHarman Kalra 	struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
26885221a0cSHarman Kalra 	uint16_t flags = 0;
26985221a0cSHarman Kalra 
2705cbe1848SHarman Kalra 	if (!(nic->tx_offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE))
2715cbe1848SHarman Kalra 		flags |= OCCTX_TX_OFFLOAD_MBUF_NOFF_F;
2725cbe1848SHarman Kalra 
27385221a0cSHarman Kalra 	if (nic->tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
27485221a0cSHarman Kalra 		flags |= OCCTX_TX_MULTI_SEG_F;
27585221a0cSHarman Kalra 
27685221a0cSHarman Kalra 	return flags;
27785221a0cSHarman Kalra }
27885221a0cSHarman Kalra 
27985221a0cSHarman Kalra static uint16_t
28085221a0cSHarman Kalra octeontx_rx_offload_flags(struct rte_eth_dev *eth_dev)
28185221a0cSHarman Kalra {
28285221a0cSHarman Kalra 	struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
28385221a0cSHarman Kalra 	struct rte_eth_dev_data *data = eth_dev->data;
28485221a0cSHarman Kalra 	struct rte_eth_conf *conf = &data->dev_conf;
28585221a0cSHarman Kalra 	struct rte_eth_rxmode *rxmode = &conf->rxmode;
28685221a0cSHarman Kalra 	uint16_t flags = 0;
28785221a0cSHarman Kalra 
28885221a0cSHarman Kalra 	if (rxmode->mq_mode == ETH_MQ_RX_RSS)
28985221a0cSHarman Kalra 		flags |= OCCTX_RX_OFFLOAD_RSS_F;
29085221a0cSHarman Kalra 
29185221a0cSHarman Kalra 	if (nic->rx_offloads & DEV_RX_OFFLOAD_SCATTER) {
29285221a0cSHarman Kalra 		flags |= OCCTX_RX_MULTI_SEG_F;
29385221a0cSHarman Kalra 		eth_dev->data->scattered_rx = 1;
29485221a0cSHarman Kalra 		/* If scatter mode is enabled, TX should also be in multi
29585221a0cSHarman Kalra 		 * seg mode, else memory leak will occur
29685221a0cSHarman Kalra 		 */
29785221a0cSHarman Kalra 		nic->tx_offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
29885221a0cSHarman Kalra 	}
29985221a0cSHarman Kalra 
30085221a0cSHarman Kalra 	return flags;
30185221a0cSHarman Kalra }
30285221a0cSHarman Kalra 
3037742c55aSJerin Jacob static int
3047742c55aSJerin Jacob octeontx_dev_configure(struct rte_eth_dev *dev)
3057742c55aSJerin Jacob {
3067742c55aSJerin Jacob 	struct rte_eth_dev_data *data = dev->data;
3077742c55aSJerin Jacob 	struct rte_eth_conf *conf = &data->dev_conf;
3087742c55aSJerin Jacob 	struct rte_eth_rxmode *rxmode = &conf->rxmode;
3097742c55aSJerin Jacob 	struct rte_eth_txmode *txmode = &conf->txmode;
3107742c55aSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
3117742c55aSJerin Jacob 	int ret;
3127742c55aSJerin Jacob 
3137742c55aSJerin Jacob 	PMD_INIT_FUNC_TRACE();
3147742c55aSJerin Jacob 	RTE_SET_USED(conf);
3157742c55aSJerin Jacob 
3167742c55aSJerin Jacob 	if (!rte_eal_has_hugepages()) {
3177742c55aSJerin Jacob 		octeontx_log_err("huge page is not configured");
3187742c55aSJerin Jacob 		return -EINVAL;
3197742c55aSJerin Jacob 	}
3207742c55aSJerin Jacob 
3217742c55aSJerin Jacob 	if (txmode->mq_mode) {
3227742c55aSJerin Jacob 		octeontx_log_err("tx mq_mode DCB or VMDq not supported");
3237742c55aSJerin Jacob 		return -EINVAL;
3247742c55aSJerin Jacob 	}
3257742c55aSJerin Jacob 
3267742c55aSJerin Jacob 	if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
3277742c55aSJerin Jacob 		rxmode->mq_mode != ETH_MQ_RX_RSS) {
3287742c55aSJerin Jacob 		octeontx_log_err("unsupported rx qmode %d", rxmode->mq_mode);
3297742c55aSJerin Jacob 		return -EINVAL;
3307742c55aSJerin Jacob 	}
3317742c55aSJerin Jacob 
332a4996bd8SWei Dai 	if (!(txmode->offloads & DEV_TX_OFFLOAD_MT_LOCKFREE)) {
333a9287089SPavan Nikhilesh 		PMD_INIT_LOG(NOTICE, "cant disable lockfree tx");
334a4996bd8SWei Dai 		txmode->offloads |= DEV_TX_OFFLOAD_MT_LOCKFREE;
3357742c55aSJerin Jacob 	}
3367742c55aSJerin Jacob 
3377742c55aSJerin Jacob 	if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
3387742c55aSJerin Jacob 		octeontx_log_err("setting link speed/duplex not supported");
3397742c55aSJerin Jacob 		return -EINVAL;
3407742c55aSJerin Jacob 	}
3417742c55aSJerin Jacob 
3427742c55aSJerin Jacob 	if (conf->dcb_capability_en) {
3437742c55aSJerin Jacob 		octeontx_log_err("DCB enable not supported");
3447742c55aSJerin Jacob 		return -EINVAL;
3457742c55aSJerin Jacob 	}
3467742c55aSJerin Jacob 
3477742c55aSJerin Jacob 	if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
3487742c55aSJerin Jacob 		octeontx_log_err("flow director not supported");
3497742c55aSJerin Jacob 		return -EINVAL;
3507742c55aSJerin Jacob 	}
3517742c55aSJerin Jacob 
3527742c55aSJerin Jacob 	nic->num_tx_queues = dev->data->nb_tx_queues;
3537742c55aSJerin Jacob 
354a6d6f0afSPavan Nikhilesh 	ret = octeontx_pko_channel_open(nic->pko_vfid * PKO_VF_NUM_DQ,
3557742c55aSJerin Jacob 					nic->num_tx_queues,
3567742c55aSJerin Jacob 					nic->base_ochan);
3577742c55aSJerin Jacob 	if (ret) {
3587742c55aSJerin Jacob 		octeontx_log_err("failed to open channel %d no-of-txq %d",
3597742c55aSJerin Jacob 			   nic->base_ochan, nic->num_tx_queues);
3607742c55aSJerin Jacob 		return -EFAULT;
3617742c55aSJerin Jacob 	}
3627742c55aSJerin Jacob 
363*56139e85SVamsi Attunuru 	ret = octeontx_dev_vlan_offload_init(dev);
364*56139e85SVamsi Attunuru 	if (ret) {
365*56139e85SVamsi Attunuru 		octeontx_log_err("failed to initialize vlan offload");
366*56139e85SVamsi Attunuru 		return -EFAULT;
367*56139e85SVamsi Attunuru 	}
368*56139e85SVamsi Attunuru 
3697742c55aSJerin Jacob 	nic->pki.classifier_enable = false;
3707742c55aSJerin Jacob 	nic->pki.hash_enable = true;
3717742c55aSJerin Jacob 	nic->pki.initialized = false;
3727742c55aSJerin Jacob 
37385221a0cSHarman Kalra 	nic->rx_offloads |= rxmode->offloads;
37485221a0cSHarman Kalra 	nic->tx_offloads |= txmode->offloads;
37585221a0cSHarman Kalra 	nic->rx_offload_flags |= octeontx_rx_offload_flags(dev);
37685221a0cSHarman Kalra 	nic->tx_offload_flags |= octeontx_tx_offload_flags(dev);
37785221a0cSHarman Kalra 
3787742c55aSJerin Jacob 	return 0;
3797742c55aSJerin Jacob }
3807742c55aSJerin Jacob 
38115bce35bSJerin Jacob static void
382da6c6874SJerin Jacob octeontx_dev_close(struct rte_eth_dev *dev)
383da6c6874SJerin Jacob {
384da6c6874SJerin Jacob 	struct octeontx_txq *txq = NULL;
385da6c6874SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
386da6c6874SJerin Jacob 	unsigned int i;
387da6c6874SJerin Jacob 	int ret;
388da6c6874SJerin Jacob 
389da6c6874SJerin Jacob 	PMD_INIT_FUNC_TRACE();
390da6c6874SJerin Jacob 
391da6c6874SJerin Jacob 	rte_event_dev_close(nic->evdev);
392da6c6874SJerin Jacob 
393*56139e85SVamsi Attunuru 	octeontx_dev_vlan_offload_fini(dev);
394*56139e85SVamsi Attunuru 
395da6c6874SJerin Jacob 	ret = octeontx_pko_channel_close(nic->base_ochan);
396da6c6874SJerin Jacob 	if (ret < 0) {
397da6c6874SJerin Jacob 		octeontx_log_err("failed to close channel %d VF%d %d %d",
398da6c6874SJerin Jacob 			     nic->base_ochan, nic->port_id, nic->num_tx_queues,
399da6c6874SJerin Jacob 			     ret);
400da6c6874SJerin Jacob 	}
401da6c6874SJerin Jacob 	/* Free txq resources for this port */
402da6c6874SJerin Jacob 	for (i = 0; i < nic->num_tx_queues; i++) {
403da6c6874SJerin Jacob 		txq = dev->data->tx_queues[i];
404da6c6874SJerin Jacob 		if (!txq)
405da6c6874SJerin Jacob 			continue;
406da6c6874SJerin Jacob 
407da6c6874SJerin Jacob 		rte_free(txq);
408da6c6874SJerin Jacob 	}
409efb9dd14SPavan Nikhilesh 
4109e399b88SSunil Kumar Kori 	/* Free MAC address table */
4119e399b88SSunil Kumar Kori 	rte_free(dev->data->mac_addrs);
4129e399b88SSunil Kumar Kori 	dev->data->mac_addrs = NULL;
4139e399b88SSunil Kumar Kori 
414efb9dd14SPavan Nikhilesh 	dev->tx_pkt_burst = NULL;
415efb9dd14SPavan Nikhilesh 	dev->rx_pkt_burst = NULL;
416da6c6874SJerin Jacob }
417da6c6874SJerin Jacob 
418da6c6874SJerin Jacob static int
4193151e6a6SHarman Kalra octeontx_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
4203151e6a6SHarman Kalra {
4213151e6a6SHarman Kalra 	uint32_t buffsz, frame_size = mtu + OCCTX_L2_OVERHEAD;
4223151e6a6SHarman Kalra 	struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
4233151e6a6SHarman Kalra 	struct rte_eth_dev_data *data = eth_dev->data;
4243151e6a6SHarman Kalra 	int rc = 0;
4253151e6a6SHarman Kalra 
4263151e6a6SHarman Kalra 	/* Check if MTU is within the allowed range */
4273151e6a6SHarman Kalra 	if (frame_size < OCCTX_MIN_FRS || frame_size > OCCTX_MAX_FRS)
4283151e6a6SHarman Kalra 		return -EINVAL;
4293151e6a6SHarman Kalra 
4303151e6a6SHarman Kalra 	buffsz = data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
4313151e6a6SHarman Kalra 
4323151e6a6SHarman Kalra 	/* Refuse MTU that requires the support of scattered packets
4333151e6a6SHarman Kalra 	 * when this feature has not been enabled before.
4343151e6a6SHarman Kalra 	 */
4353151e6a6SHarman Kalra 	if (data->dev_started && frame_size > buffsz &&
4363151e6a6SHarman Kalra 	    !(nic->rx_offloads & DEV_RX_OFFLOAD_SCATTER)) {
4373151e6a6SHarman Kalra 		octeontx_log_err("Scatter mode is disabled");
4383151e6a6SHarman Kalra 		return -EINVAL;
4393151e6a6SHarman Kalra 	}
4403151e6a6SHarman Kalra 
4413151e6a6SHarman Kalra 	/* Check <seg size> * <max_seg>  >= max_frame */
4423151e6a6SHarman Kalra 	if ((nic->rx_offloads & DEV_RX_OFFLOAD_SCATTER)	&&
4433151e6a6SHarman Kalra 	    (frame_size > buffsz * OCCTX_RX_NB_SEG_MAX))
4443151e6a6SHarman Kalra 		return -EINVAL;
4453151e6a6SHarman Kalra 
4463151e6a6SHarman Kalra 	rc = octeontx_pko_send_mtu(nic->port_id, frame_size);
4473151e6a6SHarman Kalra 	if (rc)
4483151e6a6SHarman Kalra 		return rc;
4493151e6a6SHarman Kalra 
4503151e6a6SHarman Kalra 	rc = octeontx_bgx_port_mtu_set(nic->port_id, frame_size);
4513151e6a6SHarman Kalra 	if (rc)
4523151e6a6SHarman Kalra 		return rc;
4533151e6a6SHarman Kalra 
4543151e6a6SHarman Kalra 	if (frame_size > RTE_ETHER_MAX_LEN)
4553151e6a6SHarman Kalra 		nic->rx_offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
4563151e6a6SHarman Kalra 	else
4573151e6a6SHarman Kalra 		nic->rx_offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
4583151e6a6SHarman Kalra 
4593151e6a6SHarman Kalra 	/* Update max_rx_pkt_len */
4603151e6a6SHarman Kalra 	data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
4613151e6a6SHarman Kalra 	octeontx_log_info("Received pkt beyond  maxlen %d will be dropped",
4623151e6a6SHarman Kalra 			  frame_size);
4633151e6a6SHarman Kalra 
4643151e6a6SHarman Kalra 	return rc;
4653151e6a6SHarman Kalra }
4663151e6a6SHarman Kalra 
4673151e6a6SHarman Kalra static int
46885221a0cSHarman Kalra octeontx_recheck_rx_offloads(struct octeontx_rxq *rxq)
46985221a0cSHarman Kalra {
47085221a0cSHarman Kalra 	struct rte_eth_dev *eth_dev = rxq->eth_dev;
47185221a0cSHarman Kalra 	struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
47285221a0cSHarman Kalra 	struct rte_eth_dev_data *data = eth_dev->data;
47385221a0cSHarman Kalra 	struct rte_pktmbuf_pool_private *mbp_priv;
47485221a0cSHarman Kalra 	struct evdev_priv_data *evdev_priv;
47585221a0cSHarman Kalra 	struct rte_eventdev *dev;
47685221a0cSHarman Kalra 	uint32_t buffsz;
47785221a0cSHarman Kalra 
47885221a0cSHarman Kalra 	/* Get rx buffer size */
47985221a0cSHarman Kalra 	mbp_priv = rte_mempool_get_priv(rxq->pool);
48085221a0cSHarman Kalra 	buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
48185221a0cSHarman Kalra 
48285221a0cSHarman Kalra 	/* Setup scatter mode if needed by jumbo */
48385221a0cSHarman Kalra 	if (data->dev_conf.rxmode.max_rx_pkt_len > buffsz) {
48485221a0cSHarman Kalra 		nic->rx_offloads |= DEV_RX_OFFLOAD_SCATTER;
48585221a0cSHarman Kalra 		nic->rx_offload_flags |= octeontx_rx_offload_flags(eth_dev);
48685221a0cSHarman Kalra 		nic->tx_offload_flags |= octeontx_tx_offload_flags(eth_dev);
48785221a0cSHarman Kalra 	}
48885221a0cSHarman Kalra 
48985221a0cSHarman Kalra 	/* Sharing offload flags via eventdev priv region */
49085221a0cSHarman Kalra 	dev = &rte_eventdevs[rxq->evdev];
49185221a0cSHarman Kalra 	evdev_priv = dev->data->dev_private;
49285221a0cSHarman Kalra 	evdev_priv->rx_offload_flags = nic->rx_offload_flags;
49385221a0cSHarman Kalra 	evdev_priv->tx_offload_flags = nic->tx_offload_flags;
49485221a0cSHarman Kalra 
4953151e6a6SHarman Kalra 	/* Setup MTU based on max_rx_pkt_len */
4963151e6a6SHarman Kalra 	nic->mtu = data->dev_conf.rxmode.max_rx_pkt_len - OCCTX_L2_OVERHEAD;
4973151e6a6SHarman Kalra 
49885221a0cSHarman Kalra 	return 0;
49985221a0cSHarman Kalra }
50085221a0cSHarman Kalra 
50185221a0cSHarman Kalra static int
502da6c6874SJerin Jacob octeontx_dev_start(struct rte_eth_dev *dev)
503da6c6874SJerin Jacob {
504da6c6874SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
5057f4116bdSHarman Kalra 	struct octeontx_rxq *rxq;
5067f4116bdSHarman Kalra 	int ret = 0, i;
507da6c6874SJerin Jacob 
5083151e6a6SHarman Kalra 	PMD_INIT_FUNC_TRACE();
5097f4116bdSHarman Kalra 	/* Rechecking if any new offload set to update
5107f4116bdSHarman Kalra 	 * rx/tx burst function pointer accordingly.
5117f4116bdSHarman Kalra 	 */
5127f4116bdSHarman Kalra 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
5137f4116bdSHarman Kalra 		rxq = dev->data->rx_queues[i];
5147f4116bdSHarman Kalra 		octeontx_recheck_rx_offloads(rxq);
5157f4116bdSHarman Kalra 	}
516da6c6874SJerin Jacob 
5173151e6a6SHarman Kalra 	/* Setting up the mtu based on max_rx_pkt_len */
5183151e6a6SHarman Kalra 	ret = octeontx_dev_mtu_set(dev, nic->mtu);
5193151e6a6SHarman Kalra 	if (ret) {
5203151e6a6SHarman Kalra 		octeontx_log_err("Failed to set default MTU size %d", ret);
5213151e6a6SHarman Kalra 		goto error;
5223151e6a6SHarman Kalra 	}
5233151e6a6SHarman Kalra 
524da6c6874SJerin Jacob 	/*
525da6c6874SJerin Jacob 	 * Tx start
526da6c6874SJerin Jacob 	 */
52785221a0cSHarman Kalra 	octeontx_set_tx_function(dev);
528da6c6874SJerin Jacob 	ret = octeontx_pko_channel_start(nic->base_ochan);
529da6c6874SJerin Jacob 	if (ret < 0) {
530da6c6874SJerin Jacob 		octeontx_log_err("fail to conf VF%d no. txq %d chan %d ret %d",
531da6c6874SJerin Jacob 			   nic->port_id, nic->num_tx_queues, nic->base_ochan,
532da6c6874SJerin Jacob 			   ret);
533da6c6874SJerin Jacob 		goto error;
534da6c6874SJerin Jacob 	}
535da6c6874SJerin Jacob 
536da6c6874SJerin Jacob 	/*
537da6c6874SJerin Jacob 	 * Rx start
538da6c6874SJerin Jacob 	 */
539da6c6874SJerin Jacob 	dev->rx_pkt_burst = octeontx_recv_pkts;
540da6c6874SJerin Jacob 	ret = octeontx_pki_port_start(nic->port_id);
541da6c6874SJerin Jacob 	if (ret < 0) {
542da6c6874SJerin Jacob 		octeontx_log_err("fail to start Rx on port %d", nic->port_id);
543da6c6874SJerin Jacob 		goto channel_stop_error;
544da6c6874SJerin Jacob 	}
545da6c6874SJerin Jacob 
546da6c6874SJerin Jacob 	/*
547da6c6874SJerin Jacob 	 * Start port
548da6c6874SJerin Jacob 	 */
549da6c6874SJerin Jacob 	ret = octeontx_port_start(nic);
550da6c6874SJerin Jacob 	if (ret < 0) {
551da6c6874SJerin Jacob 		octeontx_log_err("failed start port %d", ret);
552da6c6874SJerin Jacob 		goto pki_port_stop_error;
553da6c6874SJerin Jacob 	}
554da6c6874SJerin Jacob 
555da6c6874SJerin Jacob 	PMD_TX_LOG(DEBUG, "pko: start channel %d no.of txq %d port %d",
556da6c6874SJerin Jacob 			nic->base_ochan, nic->num_tx_queues, nic->port_id);
557da6c6874SJerin Jacob 
558da6c6874SJerin Jacob 	ret = rte_event_dev_start(nic->evdev);
559da6c6874SJerin Jacob 	if (ret < 0) {
560da6c6874SJerin Jacob 		octeontx_log_err("failed to start evdev: ret (%d)", ret);
561da6c6874SJerin Jacob 		goto pki_port_stop_error;
562da6c6874SJerin Jacob 	}
563da6c6874SJerin Jacob 
564da6c6874SJerin Jacob 	/* Success */
565da6c6874SJerin Jacob 	return ret;
566da6c6874SJerin Jacob 
567da6c6874SJerin Jacob pki_port_stop_error:
568da6c6874SJerin Jacob 	octeontx_pki_port_stop(nic->port_id);
569da6c6874SJerin Jacob channel_stop_error:
570da6c6874SJerin Jacob 	octeontx_pko_channel_stop(nic->base_ochan);
571da6c6874SJerin Jacob error:
572da6c6874SJerin Jacob 	return ret;
573da6c6874SJerin Jacob }
574da6c6874SJerin Jacob 
575da6c6874SJerin Jacob static void
576da6c6874SJerin Jacob octeontx_dev_stop(struct rte_eth_dev *dev)
577da6c6874SJerin Jacob {
578da6c6874SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
579da6c6874SJerin Jacob 	int ret;
580da6c6874SJerin Jacob 
581da6c6874SJerin Jacob 	PMD_INIT_FUNC_TRACE();
582da6c6874SJerin Jacob 
583da6c6874SJerin Jacob 	rte_event_dev_stop(nic->evdev);
584da6c6874SJerin Jacob 
585da6c6874SJerin Jacob 	ret = octeontx_port_stop(nic);
586da6c6874SJerin Jacob 	if (ret < 0) {
587da6c6874SJerin Jacob 		octeontx_log_err("failed to req stop port %d res=%d",
588da6c6874SJerin Jacob 					nic->port_id, ret);
589da6c6874SJerin Jacob 		return;
590da6c6874SJerin Jacob 	}
591da6c6874SJerin Jacob 
592da6c6874SJerin Jacob 	ret = octeontx_pki_port_stop(nic->port_id);
593da6c6874SJerin Jacob 	if (ret < 0) {
594da6c6874SJerin Jacob 		octeontx_log_err("failed to stop pki port %d res=%d",
595da6c6874SJerin Jacob 					nic->port_id, ret);
596da6c6874SJerin Jacob 		return;
597da6c6874SJerin Jacob 	}
598da6c6874SJerin Jacob 
599da6c6874SJerin Jacob 	ret = octeontx_pko_channel_stop(nic->base_ochan);
600da6c6874SJerin Jacob 	if (ret < 0) {
601da6c6874SJerin Jacob 		octeontx_log_err("failed to stop channel %d VF%d %d %d",
602da6c6874SJerin Jacob 			     nic->base_ochan, nic->port_id, nic->num_tx_queues,
603da6c6874SJerin Jacob 			     ret);
604da6c6874SJerin Jacob 		return;
605da6c6874SJerin Jacob 	}
606da6c6874SJerin Jacob }
607da6c6874SJerin Jacob 
6089039c812SAndrew Rybchenko static int
60915bce35bSJerin Jacob octeontx_dev_promisc_enable(struct rte_eth_dev *dev)
61015bce35bSJerin Jacob {
61115bce35bSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
61215bce35bSJerin Jacob 
61315bce35bSJerin Jacob 	PMD_INIT_FUNC_TRACE();
6149039c812SAndrew Rybchenko 	return octeontx_port_promisc_set(nic, 1);
61515bce35bSJerin Jacob }
61615bce35bSJerin Jacob 
6179039c812SAndrew Rybchenko static int
61815bce35bSJerin Jacob octeontx_dev_promisc_disable(struct rte_eth_dev *dev)
61915bce35bSJerin Jacob {
62015bce35bSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
62115bce35bSJerin Jacob 
62215bce35bSJerin Jacob 	PMD_INIT_FUNC_TRACE();
6239039c812SAndrew Rybchenko 	return octeontx_port_promisc_set(nic, 0);
62415bce35bSJerin Jacob }
62515bce35bSJerin Jacob 
6264fac7c0aSJerin Jacob static int
6274fac7c0aSJerin Jacob octeontx_port_link_status(struct octeontx_nic *nic)
6284fac7c0aSJerin Jacob {
6294fac7c0aSJerin Jacob 	int res;
6304fac7c0aSJerin Jacob 
6314fac7c0aSJerin Jacob 	PMD_INIT_FUNC_TRACE();
6324fac7c0aSJerin Jacob 	res = octeontx_bgx_port_link_status(nic->port_id);
6334fac7c0aSJerin Jacob 	if (res < 0) {
6344fac7c0aSJerin Jacob 		octeontx_log_err("failed to get port %d link status",
6354fac7c0aSJerin Jacob 				nic->port_id);
6364fac7c0aSJerin Jacob 		return res;
6374fac7c0aSJerin Jacob 	}
6384fac7c0aSJerin Jacob 
6394fac7c0aSJerin Jacob 	nic->link_up = (uint8_t)res;
6404fac7c0aSJerin Jacob 	octeontx_log_dbg("port %d link status %d", nic->port_id, nic->link_up);
6414fac7c0aSJerin Jacob 
6424fac7c0aSJerin Jacob 	return res;
6434fac7c0aSJerin Jacob }
6444fac7c0aSJerin Jacob 
6454fac7c0aSJerin Jacob /*
6464fac7c0aSJerin Jacob  * Return 0 means link status changed, -1 means not changed
6474fac7c0aSJerin Jacob  */
6484fac7c0aSJerin Jacob static int
6494fac7c0aSJerin Jacob octeontx_dev_link_update(struct rte_eth_dev *dev,
6504fac7c0aSJerin Jacob 			 int wait_to_complete __rte_unused)
6514fac7c0aSJerin Jacob {
6524fac7c0aSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
6534fac7c0aSJerin Jacob 	struct rte_eth_link link;
6544fac7c0aSJerin Jacob 	int res;
6554fac7c0aSJerin Jacob 
6564fac7c0aSJerin Jacob 	PMD_INIT_FUNC_TRACE();
6574fac7c0aSJerin Jacob 
6584fac7c0aSJerin Jacob 	res = octeontx_port_link_status(nic);
6594fac7c0aSJerin Jacob 	if (res < 0) {
6604fac7c0aSJerin Jacob 		octeontx_log_err("failed to request link status %d", res);
6614fac7c0aSJerin Jacob 		return res;
6624fac7c0aSJerin Jacob 	}
6634fac7c0aSJerin Jacob 
6644fac7c0aSJerin Jacob 	link.link_status = nic->link_up;
6654fac7c0aSJerin Jacob 
6664fac7c0aSJerin Jacob 	switch (nic->speed) {
6674fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_SGMII:
6684fac7c0aSJerin Jacob 		link.link_speed = ETH_SPEED_NUM_1G;
6694fac7c0aSJerin Jacob 		break;
6704fac7c0aSJerin Jacob 
6714fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_XAUI:
6724fac7c0aSJerin Jacob 		link.link_speed = ETH_SPEED_NUM_10G;
6734fac7c0aSJerin Jacob 		break;
6744fac7c0aSJerin Jacob 
6754fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_RXAUI:
6764fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_10G_R:
6774fac7c0aSJerin Jacob 		link.link_speed = ETH_SPEED_NUM_10G;
6784fac7c0aSJerin Jacob 		break;
6794fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_QSGMII:
6804fac7c0aSJerin Jacob 		link.link_speed = ETH_SPEED_NUM_5G;
6814fac7c0aSJerin Jacob 		break;
6824fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_40G_R:
6834fac7c0aSJerin Jacob 		link.link_speed = ETH_SPEED_NUM_40G;
6844fac7c0aSJerin Jacob 		break;
6854fac7c0aSJerin Jacob 
6864fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_RESERVE1:
6874fac7c0aSJerin Jacob 	case OCTEONTX_LINK_SPEED_RESERVE2:
6884fac7c0aSJerin Jacob 	default:
6893a4b87c8SStephen Hemminger 		link.link_speed = ETH_SPEED_NUM_NONE;
6904fac7c0aSJerin Jacob 		octeontx_log_err("incorrect link speed %d", nic->speed);
6914fac7c0aSJerin Jacob 		break;
6924fac7c0aSJerin Jacob 	}
6934fac7c0aSJerin Jacob 
6941e3a958fSThomas Monjalon 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
6951e3a958fSThomas Monjalon 	link.link_autoneg = ETH_LINK_AUTONEG;
6964fac7c0aSJerin Jacob 
6972b4ab422SStephen Hemminger 	return rte_eth_linkstatus_set(dev, &link);
6984fac7c0aSJerin Jacob }
6994fac7c0aSJerin Jacob 
700d5b0924bSMatan Azrad static int
70155389909SJerin Jacob octeontx_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
70255389909SJerin Jacob {
70355389909SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
70455389909SJerin Jacob 
70555389909SJerin Jacob 	PMD_INIT_FUNC_TRACE();
706d5b0924bSMatan Azrad 	return octeontx_port_stats(nic, stats);
70755389909SJerin Jacob }
70855389909SJerin Jacob 
7099970a9adSIgor Romanov static int
71055389909SJerin Jacob octeontx_dev_stats_reset(struct rte_eth_dev *dev)
71155389909SJerin Jacob {
71255389909SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
71355389909SJerin Jacob 
71455389909SJerin Jacob 	PMD_INIT_FUNC_TRACE();
7159970a9adSIgor Romanov 	return octeontx_port_stats_clr(nic);
71655389909SJerin Jacob }
71755389909SJerin Jacob 
718e4373bf1SSunil Kumar Kori static void
719e4373bf1SSunil Kumar Kori octeontx_dev_mac_addr_del(struct rte_eth_dev *dev, uint32_t index)
720e4373bf1SSunil Kumar Kori {
721e4373bf1SSunil Kumar Kori 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
722e4373bf1SSunil Kumar Kori 	int ret;
723e4373bf1SSunil Kumar Kori 
724e4373bf1SSunil Kumar Kori 	ret = octeontx_bgx_port_mac_del(nic->port_id, index);
725e4373bf1SSunil Kumar Kori 	if (ret != 0)
726e4373bf1SSunil Kumar Kori 		octeontx_log_err("failed to del MAC address filter on port %d",
727e4373bf1SSunil Kumar Kori 				 nic->port_id);
728e4373bf1SSunil Kumar Kori }
729e4373bf1SSunil Kumar Kori 
730e4373bf1SSunil Kumar Kori static int
731e4373bf1SSunil Kumar Kori octeontx_dev_mac_addr_add(struct rte_eth_dev *dev,
732e4373bf1SSunil Kumar Kori 			  struct rte_ether_addr *mac_addr,
7339614459bSSunil Kumar Kori 			  uint32_t index,
734e4373bf1SSunil Kumar Kori 			  __rte_unused uint32_t vmdq)
735e4373bf1SSunil Kumar Kori {
736e4373bf1SSunil Kumar Kori 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
737e4373bf1SSunil Kumar Kori 	int ret;
738e4373bf1SSunil Kumar Kori 
7399614459bSSunil Kumar Kori 	ret = octeontx_bgx_port_mac_add(nic->port_id, mac_addr->addr_bytes,
7409614459bSSunil Kumar Kori 					index);
741e4373bf1SSunil Kumar Kori 	if (ret < 0) {
742e4373bf1SSunil Kumar Kori 		octeontx_log_err("failed to add MAC address filter on port %d",
743e4373bf1SSunil Kumar Kori 				 nic->port_id);
744e4373bf1SSunil Kumar Kori 		return ret;
745e4373bf1SSunil Kumar Kori 	}
746e4373bf1SSunil Kumar Kori 
747e4373bf1SSunil Kumar Kori 	return 0;
748e4373bf1SSunil Kumar Kori }
749e4373bf1SSunil Kumar Kori 
750caccf8b3SOlivier Matz static int
751ef7308fcSJerin Jacob octeontx_dev_default_mac_addr_set(struct rte_eth_dev *dev,
7526d13ea8eSOlivier Matz 					struct rte_ether_addr *addr)
753ef7308fcSJerin Jacob {
754ef7308fcSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
755ef7308fcSJerin Jacob 	int ret;
756ef7308fcSJerin Jacob 
757ef7308fcSJerin Jacob 	ret = octeontx_bgx_port_mac_set(nic->port_id, addr->addr_bytes);
7589614459bSSunil Kumar Kori 	if (ret == 0) {
7599614459bSSunil Kumar Kori 		/* Update same mac address to BGX CAM table */
7609614459bSSunil Kumar Kori 		ret = octeontx_bgx_port_mac_add(nic->port_id, addr->addr_bytes,
7619614459bSSunil Kumar Kori 						0);
7629614459bSSunil Kumar Kori 	}
7639614459bSSunil Kumar Kori 	if (ret < 0) {
764ef7308fcSJerin Jacob 		octeontx_log_err("failed to set MAC address on port %d",
765ef7308fcSJerin Jacob 				 nic->port_id);
7669614459bSSunil Kumar Kori 	}
767caccf8b3SOlivier Matz 
768caccf8b3SOlivier Matz 	return ret;
769ef7308fcSJerin Jacob }
770ef7308fcSJerin Jacob 
771bdad90d1SIvan Ilchenko static int
7727c0347a2SJerin Jacob octeontx_dev_info(struct rte_eth_dev *dev,
7737c0347a2SJerin Jacob 		struct rte_eth_dev_info *dev_info)
7747c0347a2SJerin Jacob {
775e4373bf1SSunil Kumar Kori 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
7767c0347a2SJerin Jacob 
7777c0347a2SJerin Jacob 	/* Autonegotiation may be disabled */
7787c0347a2SJerin Jacob 	dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
7797c0347a2SJerin Jacob 	dev_info->speed_capa |= ETH_LINK_SPEED_10M | ETH_LINK_SPEED_100M |
7807c0347a2SJerin Jacob 			ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
7817c0347a2SJerin Jacob 			ETH_LINK_SPEED_40G;
7827c0347a2SJerin Jacob 
7833151e6a6SHarman Kalra 	/* Min/Max MTU supported */
7843151e6a6SHarman Kalra 	dev_info->min_rx_bufsize = OCCTX_MIN_FRS;
7853151e6a6SHarman Kalra 	dev_info->max_rx_pktlen = OCCTX_MAX_FRS;
7863151e6a6SHarman Kalra 	dev_info->max_mtu = dev_info->max_rx_pktlen - OCCTX_L2_OVERHEAD;
7873151e6a6SHarman Kalra 	dev_info->min_mtu = dev_info->min_rx_bufsize - OCCTX_L2_OVERHEAD;
7883151e6a6SHarman Kalra 
789e4373bf1SSunil Kumar Kori 	dev_info->max_mac_addrs =
790e4373bf1SSunil Kumar Kori 				octeontx_bgx_port_mac_entries_get(nic->port_id);
7917c0347a2SJerin Jacob 	dev_info->max_rx_pktlen = PKI_MAX_PKTLEN;
7927c0347a2SJerin Jacob 	dev_info->max_rx_queues = 1;
7937c0347a2SJerin Jacob 	dev_info->max_tx_queues = PKO_MAX_NUM_DQ;
7947c0347a2SJerin Jacob 	dev_info->min_rx_bufsize = 0;
7957c0347a2SJerin Jacob 
7967c0347a2SJerin Jacob 	dev_info->default_rxconf = (struct rte_eth_rxconf) {
7977c0347a2SJerin Jacob 		.rx_free_thresh = 0,
7987c0347a2SJerin Jacob 		.rx_drop_en = 0,
799a9287089SPavan Nikhilesh 		.offloads = OCTEONTX_RX_OFFLOADS,
8007c0347a2SJerin Jacob 	};
8017c0347a2SJerin Jacob 
8027c0347a2SJerin Jacob 	dev_info->default_txconf = (struct rte_eth_txconf) {
8037c0347a2SJerin Jacob 		.tx_free_thresh = 0,
8042fd41a15SPavan Nikhilesh 		.offloads = OCTEONTX_TX_OFFLOADS,
8057c0347a2SJerin Jacob 	};
8067c0347a2SJerin Jacob 
807a9287089SPavan Nikhilesh 	dev_info->rx_offload_capa = OCTEONTX_RX_OFFLOADS;
808a9287089SPavan Nikhilesh 	dev_info->tx_offload_capa = OCTEONTX_TX_OFFLOADS;
80979c25ae2SPavan Nikhilesh 	dev_info->rx_queue_offload_capa = OCTEONTX_RX_OFFLOADS;
81079c25ae2SPavan Nikhilesh 	dev_info->tx_queue_offload_capa = OCTEONTX_TX_OFFLOADS;
811bdad90d1SIvan Ilchenko 
812bdad90d1SIvan Ilchenko 	return 0;
8137c0347a2SJerin Jacob }
8147c0347a2SJerin Jacob 
8157fe7c98fSJerin Jacob static void
8167fe7c98fSJerin Jacob octeontx_dq_info_getter(octeontx_dq_t *dq, void *out)
8177fe7c98fSJerin Jacob {
8187fe7c98fSJerin Jacob 	((octeontx_dq_t *)out)->lmtline_va = dq->lmtline_va;
8197fe7c98fSJerin Jacob 	((octeontx_dq_t *)out)->ioreg_va = dq->ioreg_va;
8207fe7c98fSJerin Jacob 	((octeontx_dq_t *)out)->fc_status_va = dq->fc_status_va;
8217fe7c98fSJerin Jacob }
8227fe7c98fSJerin Jacob 
8237fe7c98fSJerin Jacob static int
8247fe7c98fSJerin Jacob octeontx_vf_start_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
8257fe7c98fSJerin Jacob 				uint16_t qidx)
8267fe7c98fSJerin Jacob {
8277fe7c98fSJerin Jacob 	struct octeontx_txq *txq;
8287fe7c98fSJerin Jacob 	int res;
8297fe7c98fSJerin Jacob 
8307fe7c98fSJerin Jacob 	PMD_INIT_FUNC_TRACE();
8317fe7c98fSJerin Jacob 
8327fe7c98fSJerin Jacob 	if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
8337fe7c98fSJerin Jacob 		return 0;
8347fe7c98fSJerin Jacob 
8357fe7c98fSJerin Jacob 	txq = dev->data->tx_queues[qidx];
8367fe7c98fSJerin Jacob 
8377fe7c98fSJerin Jacob 	res = octeontx_pko_channel_query_dqs(nic->base_ochan,
8387fe7c98fSJerin Jacob 						&txq->dq,
8397fe7c98fSJerin Jacob 						sizeof(octeontx_dq_t),
8407fe7c98fSJerin Jacob 						txq->queue_id,
8417fe7c98fSJerin Jacob 						octeontx_dq_info_getter);
8427fe7c98fSJerin Jacob 	if (res < 0) {
8437fe7c98fSJerin Jacob 		res = -EFAULT;
8447fe7c98fSJerin Jacob 		goto close_port;
8457fe7c98fSJerin Jacob 	}
8467fe7c98fSJerin Jacob 
8477fe7c98fSJerin Jacob 	dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
8487fe7c98fSJerin Jacob 	return res;
8497fe7c98fSJerin Jacob 
8507fe7c98fSJerin Jacob close_port:
8517fe7c98fSJerin Jacob 	(void)octeontx_port_stop(nic);
8527fe7c98fSJerin Jacob 	octeontx_pko_channel_stop(nic->base_ochan);
8537fe7c98fSJerin Jacob 	octeontx_pko_channel_close(nic->base_ochan);
8547fe7c98fSJerin Jacob 	dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
8557fe7c98fSJerin Jacob 	return res;
8567fe7c98fSJerin Jacob }
8577fe7c98fSJerin Jacob 
8587fe7c98fSJerin Jacob static int
8597fe7c98fSJerin Jacob octeontx_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t qidx)
8607fe7c98fSJerin Jacob {
8617fe7c98fSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
8627fe7c98fSJerin Jacob 
8637fe7c98fSJerin Jacob 	PMD_INIT_FUNC_TRACE();
8647fe7c98fSJerin Jacob 	qidx = qidx % PKO_VF_NUM_DQ;
8657fe7c98fSJerin Jacob 	return octeontx_vf_start_tx_queue(dev, nic, qidx);
8667fe7c98fSJerin Jacob }
8677fe7c98fSJerin Jacob 
8687fe7c98fSJerin Jacob static inline int
8697fe7c98fSJerin Jacob octeontx_vf_stop_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
8707fe7c98fSJerin Jacob 			  uint16_t qidx)
8717fe7c98fSJerin Jacob {
8727fe7c98fSJerin Jacob 	int ret = 0;
8737fe7c98fSJerin Jacob 
8747fe7c98fSJerin Jacob 	RTE_SET_USED(nic);
8757fe7c98fSJerin Jacob 	PMD_INIT_FUNC_TRACE();
8767fe7c98fSJerin Jacob 
8777fe7c98fSJerin Jacob 	if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
8787fe7c98fSJerin Jacob 		return 0;
8797fe7c98fSJerin Jacob 
8807fe7c98fSJerin Jacob 	dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
8817fe7c98fSJerin Jacob 	return ret;
8827fe7c98fSJerin Jacob }
8837fe7c98fSJerin Jacob 
8847fe7c98fSJerin Jacob static int
8857fe7c98fSJerin Jacob octeontx_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx)
8867fe7c98fSJerin Jacob {
8877fe7c98fSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
8887fe7c98fSJerin Jacob 
8897fe7c98fSJerin Jacob 	PMD_INIT_FUNC_TRACE();
8907fe7c98fSJerin Jacob 	qidx = qidx % PKO_VF_NUM_DQ;
8917fe7c98fSJerin Jacob 
8927fe7c98fSJerin Jacob 	return octeontx_vf_stop_tx_queue(dev, nic, qidx);
8937fe7c98fSJerin Jacob }
8947fe7c98fSJerin Jacob 
895150cbc84SJerin Jacob static void
896150cbc84SJerin Jacob octeontx_dev_tx_queue_release(void *tx_queue)
897150cbc84SJerin Jacob {
898150cbc84SJerin Jacob 	struct octeontx_txq *txq = tx_queue;
899150cbc84SJerin Jacob 	int res;
900150cbc84SJerin Jacob 
901150cbc84SJerin Jacob 	PMD_INIT_FUNC_TRACE();
902150cbc84SJerin Jacob 
903150cbc84SJerin Jacob 	if (txq) {
904150cbc84SJerin Jacob 		res = octeontx_dev_tx_queue_stop(txq->eth_dev, txq->queue_id);
905150cbc84SJerin Jacob 		if (res < 0)
906150cbc84SJerin Jacob 			octeontx_log_err("failed stop tx_queue(%d)\n",
907150cbc84SJerin Jacob 				   txq->queue_id);
908150cbc84SJerin Jacob 
909150cbc84SJerin Jacob 		rte_free(txq);
910150cbc84SJerin Jacob 	}
911150cbc84SJerin Jacob }
912150cbc84SJerin Jacob 
913150cbc84SJerin Jacob static int
914150cbc84SJerin Jacob octeontx_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
915150cbc84SJerin Jacob 			    uint16_t nb_desc, unsigned int socket_id,
916a4996bd8SWei Dai 			    const struct rte_eth_txconf *tx_conf __rte_unused)
917150cbc84SJerin Jacob {
918150cbc84SJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
919150cbc84SJerin Jacob 	struct octeontx_txq *txq = NULL;
920150cbc84SJerin Jacob 	uint16_t dq_num;
921150cbc84SJerin Jacob 	int res = 0;
922150cbc84SJerin Jacob 
923150cbc84SJerin Jacob 	RTE_SET_USED(nb_desc);
924150cbc84SJerin Jacob 	RTE_SET_USED(socket_id);
925150cbc84SJerin Jacob 
926a6d6f0afSPavan Nikhilesh 	dq_num = (nic->pko_vfid * PKO_VF_NUM_DQ) + qidx;
927150cbc84SJerin Jacob 
928150cbc84SJerin Jacob 	/* Socket id check */
929150cbc84SJerin Jacob 	if (socket_id != (unsigned int)SOCKET_ID_ANY &&
930150cbc84SJerin Jacob 			socket_id != (unsigned int)nic->node)
931150cbc84SJerin Jacob 		PMD_TX_LOG(INFO, "socket_id expected %d, configured %d",
932150cbc84SJerin Jacob 						socket_id, nic->node);
933150cbc84SJerin Jacob 
934150cbc84SJerin Jacob 	/* Free memory prior to re-allocation if needed. */
935150cbc84SJerin Jacob 	if (dev->data->tx_queues[qidx] != NULL) {
936150cbc84SJerin Jacob 		PMD_TX_LOG(DEBUG, "freeing memory prior to re-allocation %d",
937150cbc84SJerin Jacob 				qidx);
938150cbc84SJerin Jacob 		octeontx_dev_tx_queue_release(dev->data->tx_queues[qidx]);
939150cbc84SJerin Jacob 		dev->data->tx_queues[qidx] = NULL;
940150cbc84SJerin Jacob 	}
941150cbc84SJerin Jacob 
942150cbc84SJerin Jacob 	/* Allocating tx queue data structure */
943150cbc84SJerin Jacob 	txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct octeontx_txq),
944150cbc84SJerin Jacob 				 RTE_CACHE_LINE_SIZE, nic->node);
945150cbc84SJerin Jacob 	if (txq == NULL) {
946150cbc84SJerin Jacob 		octeontx_log_err("failed to allocate txq=%d", qidx);
947150cbc84SJerin Jacob 		res = -ENOMEM;
948150cbc84SJerin Jacob 		goto err;
949150cbc84SJerin Jacob 	}
950150cbc84SJerin Jacob 
951150cbc84SJerin Jacob 	txq->eth_dev = dev;
952150cbc84SJerin Jacob 	txq->queue_id = dq_num;
953150cbc84SJerin Jacob 	dev->data->tx_queues[qidx] = txq;
954150cbc84SJerin Jacob 	dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
955150cbc84SJerin Jacob 
956150cbc84SJerin Jacob 	res = octeontx_pko_channel_query_dqs(nic->base_ochan,
957150cbc84SJerin Jacob 						&txq->dq,
958150cbc84SJerin Jacob 						sizeof(octeontx_dq_t),
959150cbc84SJerin Jacob 						txq->queue_id,
960150cbc84SJerin Jacob 						octeontx_dq_info_getter);
961150cbc84SJerin Jacob 	if (res < 0) {
962150cbc84SJerin Jacob 		res = -EFAULT;
963150cbc84SJerin Jacob 		goto err;
964150cbc84SJerin Jacob 	}
965150cbc84SJerin Jacob 
966150cbc84SJerin Jacob 	PMD_TX_LOG(DEBUG, "[%d]:[%d] txq=%p nb_desc=%d lmtline=%p ioreg_va=%p fc_status_va=%p",
967150cbc84SJerin Jacob 			qidx, txq->queue_id, txq, nb_desc, txq->dq.lmtline_va,
968150cbc84SJerin Jacob 			txq->dq.ioreg_va,
969150cbc84SJerin Jacob 			txq->dq.fc_status_va);
970150cbc84SJerin Jacob 
971150cbc84SJerin Jacob 	return res;
972150cbc84SJerin Jacob 
973150cbc84SJerin Jacob err:
974150cbc84SJerin Jacob 	if (txq)
975150cbc84SJerin Jacob 		rte_free(txq);
976150cbc84SJerin Jacob 
977150cbc84SJerin Jacob 	return res;
978150cbc84SJerin Jacob }
979150cbc84SJerin Jacob 
980197438eeSJerin Jacob static int
981197438eeSJerin Jacob octeontx_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
982197438eeSJerin Jacob 				uint16_t nb_desc, unsigned int socket_id,
983197438eeSJerin Jacob 				const struct rte_eth_rxconf *rx_conf,
984197438eeSJerin Jacob 				struct rte_mempool *mb_pool)
985197438eeSJerin Jacob {
986197438eeSJerin Jacob 	struct octeontx_nic *nic = octeontx_pmd_priv(dev);
987197438eeSJerin Jacob 	struct rte_mempool_ops *mp_ops = NULL;
988197438eeSJerin Jacob 	struct octeontx_rxq *rxq = NULL;
989197438eeSJerin Jacob 	pki_pktbuf_cfg_t pktbuf_conf;
990197438eeSJerin Jacob 	pki_hash_cfg_t pki_hash;
991197438eeSJerin Jacob 	pki_qos_cfg_t pki_qos;
992197438eeSJerin Jacob 	uintptr_t pool;
993197438eeSJerin Jacob 	int ret, port;
994179c7e89SPavan Nikhilesh 	uint16_t gaura;
995197438eeSJerin Jacob 	unsigned int ev_queues = (nic->ev_queues * nic->port_id) + qidx;
996197438eeSJerin Jacob 	unsigned int ev_ports = (nic->ev_ports * nic->port_id) + qidx;
997197438eeSJerin Jacob 
998197438eeSJerin Jacob 	RTE_SET_USED(nb_desc);
999197438eeSJerin Jacob 
1000197438eeSJerin Jacob 	memset(&pktbuf_conf, 0, sizeof(pktbuf_conf));
1001197438eeSJerin Jacob 	memset(&pki_hash, 0, sizeof(pki_hash));
1002197438eeSJerin Jacob 	memset(&pki_qos, 0, sizeof(pki_qos));
1003197438eeSJerin Jacob 
1004197438eeSJerin Jacob 	mp_ops = rte_mempool_get_ops(mb_pool->ops_index);
1005197438eeSJerin Jacob 	if (strcmp(mp_ops->name, "octeontx_fpavf")) {
1006197438eeSJerin Jacob 		octeontx_log_err("failed to find octeontx_fpavf mempool");
1007197438eeSJerin Jacob 		return -ENOTSUP;
1008197438eeSJerin Jacob 	}
1009197438eeSJerin Jacob 
1010197438eeSJerin Jacob 	/* Handle forbidden configurations */
1011197438eeSJerin Jacob 	if (nic->pki.classifier_enable) {
1012197438eeSJerin Jacob 		octeontx_log_err("cannot setup queue %d. "
1013197438eeSJerin Jacob 					"Classifier option unsupported", qidx);
1014197438eeSJerin Jacob 		return -EINVAL;
1015197438eeSJerin Jacob 	}
1016197438eeSJerin Jacob 
1017197438eeSJerin Jacob 	port = nic->port_id;
1018197438eeSJerin Jacob 
1019197438eeSJerin Jacob 	/* Rx deferred start is not supported */
1020197438eeSJerin Jacob 	if (rx_conf->rx_deferred_start) {
1021197438eeSJerin Jacob 		octeontx_log_err("rx deferred start not supported");
1022197438eeSJerin Jacob 		return -EINVAL;
1023197438eeSJerin Jacob 	}
1024197438eeSJerin Jacob 
1025197438eeSJerin Jacob 	/* Verify queue index */
1026197438eeSJerin Jacob 	if (qidx >= dev->data->nb_rx_queues) {
1027197438eeSJerin Jacob 		octeontx_log_err("QID %d not supporteded (0 - %d available)\n",
1028197438eeSJerin Jacob 				qidx, (dev->data->nb_rx_queues - 1));
1029197438eeSJerin Jacob 		return -ENOTSUP;
1030197438eeSJerin Jacob 	}
1031197438eeSJerin Jacob 
1032197438eeSJerin Jacob 	/* Socket id check */
1033197438eeSJerin Jacob 	if (socket_id != (unsigned int)SOCKET_ID_ANY &&
1034197438eeSJerin Jacob 			socket_id != (unsigned int)nic->node)
1035197438eeSJerin Jacob 		PMD_RX_LOG(INFO, "socket_id expected %d, configured %d",
1036197438eeSJerin Jacob 						socket_id, nic->node);
1037197438eeSJerin Jacob 
1038197438eeSJerin Jacob 	/* Allocating rx queue data structure */
1039197438eeSJerin Jacob 	rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct octeontx_rxq),
1040197438eeSJerin Jacob 				 RTE_CACHE_LINE_SIZE, nic->node);
1041197438eeSJerin Jacob 	if (rxq == NULL) {
1042197438eeSJerin Jacob 		octeontx_log_err("failed to allocate rxq=%d", qidx);
1043197438eeSJerin Jacob 		return -ENOMEM;
1044197438eeSJerin Jacob 	}
1045197438eeSJerin Jacob 
1046197438eeSJerin Jacob 	if (!nic->pki.initialized) {
1047197438eeSJerin Jacob 		pktbuf_conf.port_type = 0;
1048197438eeSJerin Jacob 		pki_hash.port_type = 0;
1049197438eeSJerin Jacob 		pki_qos.port_type = 0;
1050197438eeSJerin Jacob 
1051197438eeSJerin Jacob 		pktbuf_conf.mmask.f_wqe_skip = 1;
1052197438eeSJerin Jacob 		pktbuf_conf.mmask.f_first_skip = 1;
1053197438eeSJerin Jacob 		pktbuf_conf.mmask.f_later_skip = 1;
1054197438eeSJerin Jacob 		pktbuf_conf.mmask.f_mbuff_size = 1;
1055197438eeSJerin Jacob 		pktbuf_conf.mmask.f_cache_mode = 1;
1056197438eeSJerin Jacob 
1057197438eeSJerin Jacob 		pktbuf_conf.wqe_skip = OCTTX_PACKET_WQE_SKIP;
1058679dfdc9SNitin Saxena 		pktbuf_conf.first_skip = OCTTX_PACKET_FIRST_SKIP(mb_pool);
1059197438eeSJerin Jacob 		pktbuf_conf.later_skip = OCTTX_PACKET_LATER_SKIP;
1060197438eeSJerin Jacob 		pktbuf_conf.mbuff_size = (mb_pool->elt_size -
1061197438eeSJerin Jacob 					RTE_PKTMBUF_HEADROOM -
1062679dfdc9SNitin Saxena 					rte_pktmbuf_priv_size(mb_pool) -
1063197438eeSJerin Jacob 					sizeof(struct rte_mbuf));
1064197438eeSJerin Jacob 
1065197438eeSJerin Jacob 		pktbuf_conf.cache_mode = PKI_OPC_MODE_STF2_STT;
1066197438eeSJerin Jacob 
1067197438eeSJerin Jacob 		ret = octeontx_pki_port_pktbuf_config(port, &pktbuf_conf);
1068197438eeSJerin Jacob 		if (ret != 0) {
1069197438eeSJerin Jacob 			octeontx_log_err("fail to configure pktbuf for port %d",
1070197438eeSJerin Jacob 					port);
1071197438eeSJerin Jacob 			rte_free(rxq);
1072197438eeSJerin Jacob 			return ret;
1073197438eeSJerin Jacob 		}
1074197438eeSJerin Jacob 		PMD_RX_LOG(DEBUG, "Port %d Rx pktbuf configured:\n"
1075197438eeSJerin Jacob 				"\tmbuf_size:\t0x%0x\n"
1076197438eeSJerin Jacob 				"\twqe_skip:\t0x%0x\n"
1077197438eeSJerin Jacob 				"\tfirst_skip:\t0x%0x\n"
1078197438eeSJerin Jacob 				"\tlater_skip:\t0x%0x\n"
1079197438eeSJerin Jacob 				"\tcache_mode:\t%s\n",
1080197438eeSJerin Jacob 				port,
1081197438eeSJerin Jacob 				pktbuf_conf.mbuff_size,
1082197438eeSJerin Jacob 				pktbuf_conf.wqe_skip,
1083197438eeSJerin Jacob 				pktbuf_conf.first_skip,
1084197438eeSJerin Jacob 				pktbuf_conf.later_skip,
1085197438eeSJerin Jacob 				(pktbuf_conf.cache_mode ==
1086197438eeSJerin Jacob 						PKI_OPC_MODE_STT) ?
1087197438eeSJerin Jacob 				"STT" :
1088197438eeSJerin Jacob 				(pktbuf_conf.cache_mode ==
1089197438eeSJerin Jacob 						PKI_OPC_MODE_STF) ?
1090197438eeSJerin Jacob 				"STF" :
1091197438eeSJerin Jacob 				(pktbuf_conf.cache_mode ==
1092197438eeSJerin Jacob 						PKI_OPC_MODE_STF1_STT) ?
1093197438eeSJerin Jacob 				"STF1_STT" : "STF2_STT");
1094197438eeSJerin Jacob 
1095197438eeSJerin Jacob 		if (nic->pki.hash_enable) {
1096197438eeSJerin Jacob 			pki_hash.tag_dlc = 1;
1097197438eeSJerin Jacob 			pki_hash.tag_slc = 1;
1098197438eeSJerin Jacob 			pki_hash.tag_dlf = 1;
1099197438eeSJerin Jacob 			pki_hash.tag_slf = 1;
1100d0d65498SPavan Nikhilesh 			pki_hash.tag_prt = 1;
1101197438eeSJerin Jacob 			octeontx_pki_port_hash_config(port, &pki_hash);
1102197438eeSJerin Jacob 		}
1103197438eeSJerin Jacob 
1104197438eeSJerin Jacob 		pool = (uintptr_t)mb_pool->pool_id;
1105197438eeSJerin Jacob 
1106179c7e89SPavan Nikhilesh 		/* Get the gaura Id */
1107179c7e89SPavan Nikhilesh 		gaura = octeontx_fpa_bufpool_gaura(pool);
1108197438eeSJerin Jacob 
1109197438eeSJerin Jacob 		pki_qos.qpg_qos = PKI_QPG_QOS_NONE;
1110197438eeSJerin Jacob 		pki_qos.num_entry = 1;
1111197438eeSJerin Jacob 		pki_qos.drop_policy = 0;
1112d0d65498SPavan Nikhilesh 		pki_qos.tag_type = 0L;
1113197438eeSJerin Jacob 		pki_qos.qos_entry[0].port_add = 0;
1114197438eeSJerin Jacob 		pki_qos.qos_entry[0].gaura = gaura;
1115197438eeSJerin Jacob 		pki_qos.qos_entry[0].ggrp_ok = ev_queues;
1116197438eeSJerin Jacob 		pki_qos.qos_entry[0].ggrp_bad = ev_queues;
1117197438eeSJerin Jacob 		pki_qos.qos_entry[0].grptag_bad = 0;
1118197438eeSJerin Jacob 		pki_qos.qos_entry[0].grptag_ok = 0;
1119197438eeSJerin Jacob 
1120197438eeSJerin Jacob 		ret = octeontx_pki_port_create_qos(port, &pki_qos);
1121197438eeSJerin Jacob 		if (ret < 0) {
1122197438eeSJerin Jacob 			octeontx_log_err("failed to create QOS port=%d, q=%d",
1123197438eeSJerin Jacob 					port, qidx);
1124197438eeSJerin Jacob 			rte_free(rxq);
1125197438eeSJerin Jacob 			return ret;
1126197438eeSJerin Jacob 		}
1127197438eeSJerin Jacob 		nic->pki.initialized = true;
1128197438eeSJerin Jacob 	}
1129197438eeSJerin Jacob 
1130197438eeSJerin Jacob 	rxq->port_id = nic->port_id;
1131197438eeSJerin Jacob 	rxq->eth_dev = dev;
1132197438eeSJerin Jacob 	rxq->queue_id = qidx;
1133197438eeSJerin Jacob 	rxq->evdev = nic->evdev;
1134197438eeSJerin Jacob 	rxq->ev_queues = ev_queues;
1135197438eeSJerin Jacob 	rxq->ev_ports = ev_ports;
113685221a0cSHarman Kalra 	rxq->pool = mb_pool;
1137197438eeSJerin Jacob 
113885221a0cSHarman Kalra 	octeontx_recheck_rx_offloads(rxq);
1139197438eeSJerin Jacob 	dev->data->rx_queues[qidx] = rxq;
1140197438eeSJerin Jacob 	dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1141197438eeSJerin Jacob 	return 0;
1142197438eeSJerin Jacob }
1143197438eeSJerin Jacob 
1144197438eeSJerin Jacob static void
1145197438eeSJerin Jacob octeontx_dev_rx_queue_release(void *rxq)
1146197438eeSJerin Jacob {
1147197438eeSJerin Jacob 	rte_free(rxq);
1148197438eeSJerin Jacob }
1149197438eeSJerin Jacob 
115020186d43SJerin Jacob static const uint32_t *
115120186d43SJerin Jacob octeontx_dev_supported_ptypes_get(struct rte_eth_dev *dev)
115220186d43SJerin Jacob {
115320186d43SJerin Jacob 	static const uint32_t ptypes[] = {
115420186d43SJerin Jacob 		RTE_PTYPE_L3_IPV4,
115520186d43SJerin Jacob 		RTE_PTYPE_L3_IPV4_EXT,
115620186d43SJerin Jacob 		RTE_PTYPE_L3_IPV6,
115720186d43SJerin Jacob 		RTE_PTYPE_L3_IPV6_EXT,
115820186d43SJerin Jacob 		RTE_PTYPE_L4_TCP,
115920186d43SJerin Jacob 		RTE_PTYPE_L4_UDP,
116020186d43SJerin Jacob 		RTE_PTYPE_L4_FRAG,
116120186d43SJerin Jacob 		RTE_PTYPE_UNKNOWN
116220186d43SJerin Jacob 	};
116320186d43SJerin Jacob 
116420186d43SJerin Jacob 	if (dev->rx_pkt_burst == octeontx_recv_pkts)
116520186d43SJerin Jacob 		return ptypes;
116620186d43SJerin Jacob 
116720186d43SJerin Jacob 	return NULL;
116820186d43SJerin Jacob }
116920186d43SJerin Jacob 
11705452a17dSPavan Nikhilesh static int
11715452a17dSPavan Nikhilesh octeontx_pool_ops(struct rte_eth_dev *dev, const char *pool)
11725452a17dSPavan Nikhilesh {
11735452a17dSPavan Nikhilesh 	RTE_SET_USED(dev);
11745452a17dSPavan Nikhilesh 
11755452a17dSPavan Nikhilesh 	if (!strcmp(pool, "octeontx_fpavf"))
11765452a17dSPavan Nikhilesh 		return 0;
11775452a17dSPavan Nikhilesh 
11785452a17dSPavan Nikhilesh 	return -ENOTSUP;
11795452a17dSPavan Nikhilesh }
11805452a17dSPavan Nikhilesh 
1181f18b146cSJerin Jacob /* Initialize and register driver with DPDK Application */
1182f18b146cSJerin Jacob static const struct eth_dev_ops octeontx_dev_ops = {
11837742c55aSJerin Jacob 	.dev_configure		 = octeontx_dev_configure,
11847c0347a2SJerin Jacob 	.dev_infos_get		 = octeontx_dev_info,
1185da6c6874SJerin Jacob 	.dev_close		 = octeontx_dev_close,
1186da6c6874SJerin Jacob 	.dev_start		 = octeontx_dev_start,
1187da6c6874SJerin Jacob 	.dev_stop		 = octeontx_dev_stop,
118815bce35bSJerin Jacob 	.promiscuous_enable	 = octeontx_dev_promisc_enable,
118915bce35bSJerin Jacob 	.promiscuous_disable	 = octeontx_dev_promisc_disable,
11904fac7c0aSJerin Jacob 	.link_update		 = octeontx_dev_link_update,
119155389909SJerin Jacob 	.stats_get		 = octeontx_dev_stats_get,
119255389909SJerin Jacob 	.stats_reset		 = octeontx_dev_stats_reset,
1193e4373bf1SSunil Kumar Kori 	.mac_addr_remove	 = octeontx_dev_mac_addr_del,
1194e4373bf1SSunil Kumar Kori 	.mac_addr_add		 = octeontx_dev_mac_addr_add,
1195ef7308fcSJerin Jacob 	.mac_addr_set		 = octeontx_dev_default_mac_addr_set,
1196*56139e85SVamsi Attunuru 	.vlan_offload_set	 = octeontx_dev_vlan_offload_set,
1197*56139e85SVamsi Attunuru 	.vlan_filter_set	 = octeontx_dev_vlan_filter_set,
11987fe7c98fSJerin Jacob 	.tx_queue_start		 = octeontx_dev_tx_queue_start,
11997fe7c98fSJerin Jacob 	.tx_queue_stop		 = octeontx_dev_tx_queue_stop,
1200150cbc84SJerin Jacob 	.tx_queue_setup		 = octeontx_dev_tx_queue_setup,
1201150cbc84SJerin Jacob 	.tx_queue_release	 = octeontx_dev_tx_queue_release,
1202197438eeSJerin Jacob 	.rx_queue_setup		 = octeontx_dev_rx_queue_setup,
1203197438eeSJerin Jacob 	.rx_queue_release	 = octeontx_dev_rx_queue_release,
120420186d43SJerin Jacob 	.dev_supported_ptypes_get = octeontx_dev_supported_ptypes_get,
12053151e6a6SHarman Kalra 	.mtu_set                 = octeontx_dev_mtu_set,
12065452a17dSPavan Nikhilesh 	.pool_ops_supported      = octeontx_pool_ops,
1207f18b146cSJerin Jacob };
1208f18b146cSJerin Jacob 
1209f7be70e5SJerin Jacob /* Create Ethdev interface per BGX LMAC ports */
1210f7be70e5SJerin Jacob static int
1211f7be70e5SJerin Jacob octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
1212f7be70e5SJerin Jacob 			int socket_id)
1213f7be70e5SJerin Jacob {
1214f18b146cSJerin Jacob 	int res;
1215a6d6f0afSPavan Nikhilesh 	size_t pko_vfid;
1216f18b146cSJerin Jacob 	char octtx_name[OCTEONTX_MAX_NAME_LEN];
1217f18b146cSJerin Jacob 	struct octeontx_nic *nic = NULL;
1218f18b146cSJerin Jacob 	struct rte_eth_dev *eth_dev = NULL;
12195f19dee6SJianfeng Tan 	struct rte_eth_dev_data *data;
1220f18b146cSJerin Jacob 	const char *name = rte_vdev_device_name(dev);
1221e4373bf1SSunil Kumar Kori 	int max_entries;
1222f7be70e5SJerin Jacob 
1223f18b146cSJerin Jacob 	PMD_INIT_FUNC_TRACE();
1224f18b146cSJerin Jacob 
1225f18b146cSJerin Jacob 	sprintf(octtx_name, "%s_%d", name, port);
1226f18b146cSJerin Jacob 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1227f18b146cSJerin Jacob 		eth_dev = rte_eth_dev_attach_secondary(octtx_name);
1228f18b146cSJerin Jacob 		if (eth_dev == NULL)
1229f7be70e5SJerin Jacob 			return -ENODEV;
1230f18b146cSJerin Jacob 
1231d1c3ab22SFerruh Yigit 		eth_dev->dev_ops = &octeontx_dev_ops;
1232d1c3ab22SFerruh Yigit 		eth_dev->device = &dev->device;
12337f4116bdSHarman Kalra 		octeontx_set_tx_function(eth_dev);
1234da6c6874SJerin Jacob 		eth_dev->rx_pkt_burst = octeontx_recv_pkts;
1235fbe90cddSThomas Monjalon 		rte_eth_dev_probing_finish(eth_dev);
1236f18b146cSJerin Jacob 		return 0;
1237f18b146cSJerin Jacob 	}
1238f18b146cSJerin Jacob 
1239e16adf08SThomas Monjalon 	/* Reserve an ethdev entry */
1240e16adf08SThomas Monjalon 	eth_dev = rte_eth_dev_allocate(octtx_name);
1241e16adf08SThomas Monjalon 	if (eth_dev == NULL) {
1242e16adf08SThomas Monjalon 		octeontx_log_err("failed to allocate rte_eth_dev");
1243e16adf08SThomas Monjalon 		res = -ENOMEM;
1244e16adf08SThomas Monjalon 		goto err;
1245e16adf08SThomas Monjalon 	}
1246e16adf08SThomas Monjalon 	data = eth_dev->data;
1247e16adf08SThomas Monjalon 
1248f18b146cSJerin Jacob 	nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
1249f18b146cSJerin Jacob 	if (nic == NULL) {
1250f18b146cSJerin Jacob 		octeontx_log_err("failed to allocate nic structure");
1251f18b146cSJerin Jacob 		res = -ENOMEM;
1252f18b146cSJerin Jacob 		goto err;
1253f18b146cSJerin Jacob 	}
1254e16adf08SThomas Monjalon 	data->dev_private = nic;
1255a6d6f0afSPavan Nikhilesh 	pko_vfid = octeontx_pko_get_vfid();
1256f18b146cSJerin Jacob 
1257a6d6f0afSPavan Nikhilesh 	if (pko_vfid == SIZE_MAX) {
1258a6d6f0afSPavan Nikhilesh 		octeontx_log_err("failed to get pko vfid");
1259a6d6f0afSPavan Nikhilesh 		res = -ENODEV;
1260a6d6f0afSPavan Nikhilesh 		goto err;
1261a6d6f0afSPavan Nikhilesh 	}
1262a6d6f0afSPavan Nikhilesh 
1263a6d6f0afSPavan Nikhilesh 	nic->pko_vfid = pko_vfid;
1264f18b146cSJerin Jacob 	nic->port_id = port;
1265f18b146cSJerin Jacob 	nic->evdev = evdev;
1266f18b146cSJerin Jacob 
1267f18b146cSJerin Jacob 	res = octeontx_port_open(nic);
1268f18b146cSJerin Jacob 	if (res < 0)
1269f18b146cSJerin Jacob 		goto err;
1270f18b146cSJerin Jacob 
1271f18b146cSJerin Jacob 	/* Rx side port configuration */
1272f18b146cSJerin Jacob 	res = octeontx_pki_port_open(port);
1273f18b146cSJerin Jacob 	if (res != 0) {
1274f18b146cSJerin Jacob 		octeontx_log_err("failed to open PKI port %d", port);
1275f18b146cSJerin Jacob 		res = -ENODEV;
1276f18b146cSJerin Jacob 		goto err;
1277f18b146cSJerin Jacob 	}
1278f18b146cSJerin Jacob 
1279f18b146cSJerin Jacob 	eth_dev->device = &dev->device;
1280f18b146cSJerin Jacob 	eth_dev->intr_handle = NULL;
1281f18b146cSJerin Jacob 	eth_dev->data->kdrv = RTE_KDRV_NONE;
1282f18b146cSJerin Jacob 	eth_dev->data->numa_node = dev->device.numa_node;
1283f18b146cSJerin Jacob 
1284f18b146cSJerin Jacob 	data->port_id = eth_dev->data->port_id;
1285f18b146cSJerin Jacob 
1286f18b146cSJerin Jacob 	nic->ev_queues = 1;
1287f18b146cSJerin Jacob 	nic->ev_ports = 1;
1288f18b146cSJerin Jacob 
1289f18b146cSJerin Jacob 	data->dev_link.link_status = ETH_LINK_DOWN;
1290f18b146cSJerin Jacob 	data->dev_started = 0;
1291f18b146cSJerin Jacob 	data->promiscuous = 0;
1292f18b146cSJerin Jacob 	data->all_multicast = 0;
1293f18b146cSJerin Jacob 	data->scattered_rx = 0;
1294f18b146cSJerin Jacob 
1295e4373bf1SSunil Kumar Kori 	/* Get maximum number of supported MAC entries */
1296e4373bf1SSunil Kumar Kori 	max_entries = octeontx_bgx_port_mac_entries_get(nic->port_id);
1297e4373bf1SSunil Kumar Kori 	if (max_entries < 0) {
1298e4373bf1SSunil Kumar Kori 		octeontx_log_err("Failed to get max entries for mac addr");
1299e4373bf1SSunil Kumar Kori 		res = -ENOTSUP;
1300e4373bf1SSunil Kumar Kori 		goto err;
1301e4373bf1SSunil Kumar Kori 	}
1302e4373bf1SSunil Kumar Kori 
1303e4373bf1SSunil Kumar Kori 	data->mac_addrs = rte_zmalloc_socket(octtx_name, max_entries *
1304e4373bf1SSunil Kumar Kori 					     RTE_ETHER_ADDR_LEN, 0,
1305f18b146cSJerin Jacob 							socket_id);
1306f18b146cSJerin Jacob 	if (data->mac_addrs == NULL) {
1307f18b146cSJerin Jacob 		octeontx_log_err("failed to allocate memory for mac_addrs");
1308f18b146cSJerin Jacob 		res = -ENOMEM;
1309f18b146cSJerin Jacob 		goto err;
1310f18b146cSJerin Jacob 	}
1311f18b146cSJerin Jacob 
1312f18b146cSJerin Jacob 	eth_dev->dev_ops = &octeontx_dev_ops;
1313f18b146cSJerin Jacob 
1314f18b146cSJerin Jacob 	/* Finally save ethdev pointer to the NIC structure */
1315f18b146cSJerin Jacob 	nic->dev = eth_dev;
1316f18b146cSJerin Jacob 
1317f18b146cSJerin Jacob 	if (nic->port_id != data->port_id) {
1318f18b146cSJerin Jacob 		octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
1319f18b146cSJerin Jacob 				data->port_id, nic->port_id);
1320f18b146cSJerin Jacob 		res = -EINVAL;
13219e399b88SSunil Kumar Kori 		goto free_mac_addrs;
1322f18b146cSJerin Jacob 	}
1323f18b146cSJerin Jacob 
1324f18b146cSJerin Jacob 	/* Update port_id mac to eth_dev */
132535b2d13fSOlivier Matz 	memcpy(data->mac_addrs, nic->mac_addr, RTE_ETHER_ADDR_LEN);
1326f18b146cSJerin Jacob 
13279614459bSSunil Kumar Kori 	/* Update same mac address to BGX CAM table at index 0 */
13289614459bSSunil Kumar Kori 	octeontx_bgx_port_mac_add(nic->port_id, nic->mac_addr, 0);
13299614459bSSunil Kumar Kori 
1330f18b146cSJerin Jacob 	PMD_INIT_LOG(DEBUG, "ethdev info: ");
1331f18b146cSJerin Jacob 	PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
1332f18b146cSJerin Jacob 				nic->port_id, nic->port_ena,
1333f18b146cSJerin Jacob 				nic->base_ochan, nic->num_ochans,
1334f18b146cSJerin Jacob 				nic->num_tx_queues);
13353151e6a6SHarman Kalra 	PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->bgx_mtu);
1336f18b146cSJerin Jacob 
1337989d4926SPavan Nikhilesh 	rte_octeontx_pchan_map[(nic->base_ochan >> 8) & 0x7]
1338989d4926SPavan Nikhilesh 		[(nic->base_ochan >> 4) & 0xF] = data->port_id;
1339989d4926SPavan Nikhilesh 
1340fbe90cddSThomas Monjalon 	rte_eth_dev_probing_finish(eth_dev);
1341f18b146cSJerin Jacob 	return data->port_id;
1342f18b146cSJerin Jacob 
13439e399b88SSunil Kumar Kori free_mac_addrs:
13449e399b88SSunil Kumar Kori 	rte_free(data->mac_addrs);
1345f18b146cSJerin Jacob err:
1346a98122efSSantosh Shukla 	if (nic)
1347f18b146cSJerin Jacob 		octeontx_port_close(nic);
1348f18b146cSJerin Jacob 
1349f18b146cSJerin Jacob 	rte_eth_dev_release_port(eth_dev);
1350f18b146cSJerin Jacob 
1351f18b146cSJerin Jacob 	return res;
1352f7be70e5SJerin Jacob }
1353f7be70e5SJerin Jacob 
1354f7be70e5SJerin Jacob /* Un initialize octeontx device */
1355f7be70e5SJerin Jacob static int
1356f7be70e5SJerin Jacob octeontx_remove(struct rte_vdev_device *dev)
1357f7be70e5SJerin Jacob {
1358f7be70e5SJerin Jacob 	char octtx_name[OCTEONTX_MAX_NAME_LEN];
1359f7be70e5SJerin Jacob 	struct rte_eth_dev *eth_dev = NULL;
1360f7be70e5SJerin Jacob 	struct octeontx_nic *nic = NULL;
1361f7be70e5SJerin Jacob 	int i;
1362f7be70e5SJerin Jacob 
1363f7be70e5SJerin Jacob 	if (dev == NULL)
1364f7be70e5SJerin Jacob 		return -EINVAL;
1365f7be70e5SJerin Jacob 
1366f7be70e5SJerin Jacob 	for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
1367f7be70e5SJerin Jacob 		sprintf(octtx_name, "eth_octeontx_%d", i);
1368f7be70e5SJerin Jacob 
1369f7be70e5SJerin Jacob 		/* reserve an ethdev entry */
1370f7be70e5SJerin Jacob 		eth_dev = rte_eth_dev_allocated(octtx_name);
1371f7be70e5SJerin Jacob 		if (eth_dev == NULL)
1372f7be70e5SJerin Jacob 			return -ENODEV;
1373f7be70e5SJerin Jacob 
13744852aa8fSQi Zhang 		if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1375662dbc32SThomas Monjalon 			rte_eth_dev_release_port(eth_dev);
13764852aa8fSQi Zhang 			continue;
13774852aa8fSQi Zhang 		}
13784852aa8fSQi Zhang 
1379f7be70e5SJerin Jacob 		nic = octeontx_pmd_priv(eth_dev);
1380f7be70e5SJerin Jacob 		rte_event_dev_stop(nic->evdev);
1381f7be70e5SJerin Jacob 		PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
1382f7be70e5SJerin Jacob 
1383f7be70e5SJerin Jacob 		rte_eth_dev_release_port(eth_dev);
1384f7be70e5SJerin Jacob 		rte_event_dev_close(nic->evdev);
1385f7be70e5SJerin Jacob 	}
1386f7be70e5SJerin Jacob 
13874852aa8fSQi Zhang 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
13884852aa8fSQi Zhang 		return 0;
13894852aa8fSQi Zhang 
1390f7be70e5SJerin Jacob 	/* Free FC resource */
1391f7be70e5SJerin Jacob 	octeontx_pko_fc_free();
1392f7be70e5SJerin Jacob 
1393f7be70e5SJerin Jacob 	return 0;
1394f7be70e5SJerin Jacob }
1395f7be70e5SJerin Jacob 
1396f7be70e5SJerin Jacob /* Initialize octeontx device */
1397f7be70e5SJerin Jacob static int
1398f7be70e5SJerin Jacob octeontx_probe(struct rte_vdev_device *dev)
1399f7be70e5SJerin Jacob {
1400f7be70e5SJerin Jacob 	const char *dev_name;
1401f7be70e5SJerin Jacob 	static int probe_once;
1402f7be70e5SJerin Jacob 	uint8_t socket_id, qlist;
1403f7be70e5SJerin Jacob 	int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
1404f7be70e5SJerin Jacob 	struct rte_event_dev_config dev_conf;
1405f7be70e5SJerin Jacob 	const char *eventdev_name = "event_octeontx";
1406f7be70e5SJerin Jacob 	struct rte_event_dev_info info;
1407ee27edbeSJianfeng Tan 	struct rte_eth_dev *eth_dev;
1408f7be70e5SJerin Jacob 
1409f7be70e5SJerin Jacob 	struct octeontx_vdev_init_params init_params = {
1410f7be70e5SJerin Jacob 		OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
1411f7be70e5SJerin Jacob 	};
1412f7be70e5SJerin Jacob 
1413f7be70e5SJerin Jacob 	dev_name = rte_vdev_device_name(dev);
1414ee27edbeSJianfeng Tan 
1415ee27edbeSJianfeng Tan 	if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1416ee27edbeSJianfeng Tan 	    strlen(rte_vdev_device_args(dev)) == 0) {
1417ee27edbeSJianfeng Tan 		eth_dev = rte_eth_dev_attach_secondary(dev_name);
1418ee27edbeSJianfeng Tan 		if (!eth_dev) {
1419e6da1f00SStephen Hemminger 			PMD_INIT_LOG(ERR, "Failed to probe %s", dev_name);
1420ee27edbeSJianfeng Tan 			return -1;
1421ee27edbeSJianfeng Tan 		}
1422ee27edbeSJianfeng Tan 		/* TODO: request info from primary to set up Rx and Tx */
1423ee27edbeSJianfeng Tan 		eth_dev->dev_ops = &octeontx_dev_ops;
1424d1c3ab22SFerruh Yigit 		eth_dev->device = &dev->device;
1425fbe90cddSThomas Monjalon 		rte_eth_dev_probing_finish(eth_dev);
1426ee27edbeSJianfeng Tan 		return 0;
1427ee27edbeSJianfeng Tan 	}
1428ee27edbeSJianfeng Tan 
1429f7be70e5SJerin Jacob 	res = octeontx_parse_vdev_init_params(&init_params, dev);
1430f7be70e5SJerin Jacob 	if (res < 0)
1431f7be70e5SJerin Jacob 		return -EINVAL;
1432f7be70e5SJerin Jacob 
1433f7be70e5SJerin Jacob 	if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
1434f7be70e5SJerin Jacob 		octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
1435f7be70e5SJerin Jacob 				OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
1436f7be70e5SJerin Jacob 		return -ENOTSUP;
1437f7be70e5SJerin Jacob 	}
1438f7be70e5SJerin Jacob 
1439f7be70e5SJerin Jacob 	PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
1440f7be70e5SJerin Jacob 
1441f7be70e5SJerin Jacob 	socket_id = rte_socket_id();
1442f7be70e5SJerin Jacob 
1443f7be70e5SJerin Jacob 	tx_vfcnt = octeontx_pko_vf_count();
1444f7be70e5SJerin Jacob 
1445f7be70e5SJerin Jacob 	if (tx_vfcnt < init_params.nr_port) {
1446f7be70e5SJerin Jacob 		octeontx_log_err("not enough PKO (%d) for port number (%d)",
1447f7be70e5SJerin Jacob 				tx_vfcnt, init_params.nr_port);
1448f7be70e5SJerin Jacob 		return -EINVAL;
1449f7be70e5SJerin Jacob 	}
1450f7be70e5SJerin Jacob 	evdev = rte_event_dev_get_dev_id(eventdev_name);
1451f7be70e5SJerin Jacob 	if (evdev < 0) {
1452f7be70e5SJerin Jacob 		octeontx_log_err("eventdev %s not found", eventdev_name);
1453f7be70e5SJerin Jacob 		return -ENODEV;
1454f7be70e5SJerin Jacob 	}
1455f7be70e5SJerin Jacob 
1456f7be70e5SJerin Jacob 	res = rte_event_dev_info_get(evdev, &info);
1457f7be70e5SJerin Jacob 	if (res < 0) {
1458f7be70e5SJerin Jacob 		octeontx_log_err("failed to eventdev info %d", res);
1459f7be70e5SJerin Jacob 		return -EINVAL;
1460f7be70e5SJerin Jacob 	}
1461f7be70e5SJerin Jacob 
1462f7be70e5SJerin Jacob 	PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
1463f7be70e5SJerin Jacob 			info.max_event_queues, info.max_event_ports);
1464f7be70e5SJerin Jacob 
1465f7be70e5SJerin Jacob 	if (octeontx_pko_init_fc(tx_vfcnt))
1466f7be70e5SJerin Jacob 		return -ENOMEM;
1467f7be70e5SJerin Jacob 
1468f7be70e5SJerin Jacob 	devconf_set_default_sane_values(&dev_conf, &info);
1469f7be70e5SJerin Jacob 	res = rte_event_dev_configure(evdev, &dev_conf);
1470f7be70e5SJerin Jacob 	if (res < 0)
1471f7be70e5SJerin Jacob 		goto parse_error;
1472f7be70e5SJerin Jacob 
1473f7be70e5SJerin Jacob 	rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
1474f7be70e5SJerin Jacob 			(uint32_t *)&pnum);
1475f7be70e5SJerin Jacob 	rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
1476f7be70e5SJerin Jacob 			(uint32_t *)&qnum);
1477f7be70e5SJerin Jacob 	if (pnum < qnum) {
1478f7be70e5SJerin Jacob 		octeontx_log_err("too few event ports (%d) for event_q(%d)",
1479f7be70e5SJerin Jacob 				pnum, qnum);
1480f7be70e5SJerin Jacob 		res = -EINVAL;
1481f7be70e5SJerin Jacob 		goto parse_error;
1482f7be70e5SJerin Jacob 	}
14837efd5202SAnoob Joseph 
14847efd5202SAnoob Joseph 	/* Enable all queues available */
1485f7be70e5SJerin Jacob 	for (i = 0; i < qnum; i++) {
1486f7be70e5SJerin Jacob 		res = rte_event_queue_setup(evdev, i, NULL);
1487f7be70e5SJerin Jacob 		if (res < 0) {
1488f7be70e5SJerin Jacob 			octeontx_log_err("failed to setup event_q(%d): res %d",
1489f7be70e5SJerin Jacob 					i, res);
1490f7be70e5SJerin Jacob 			goto parse_error;
1491f7be70e5SJerin Jacob 		}
1492f7be70e5SJerin Jacob 	}
1493f7be70e5SJerin Jacob 
14947efd5202SAnoob Joseph 	/* Enable all ports available */
1495f7be70e5SJerin Jacob 	for (i = 0; i < pnum; i++) {
1496f7be70e5SJerin Jacob 		res = rte_event_port_setup(evdev, i, NULL);
1497f7be70e5SJerin Jacob 		if (res < 0) {
1498f7be70e5SJerin Jacob 			res = -ENODEV;
1499f7be70e5SJerin Jacob 			octeontx_log_err("failed to setup ev port(%d) res=%d",
1500f7be70e5SJerin Jacob 						i, res);
1501f7be70e5SJerin Jacob 			goto parse_error;
1502f7be70e5SJerin Jacob 		}
15037efd5202SAnoob Joseph 	}
15047efd5202SAnoob Joseph 
15057efd5202SAnoob Joseph 	/*
15067efd5202SAnoob Joseph 	 * Do 1:1 links for ports & queues. All queues would be mapped to
15077efd5202SAnoob Joseph 	 * one port. If there are more ports than queues, then some ports
15087efd5202SAnoob Joseph 	 * won't be linked to any queue.
15097efd5202SAnoob Joseph 	 */
15107efd5202SAnoob Joseph 	for (i = 0; i < qnum; i++) {
1511f7be70e5SJerin Jacob 		/* Link one queue to one event port */
1512f7be70e5SJerin Jacob 		qlist = i;
1513f7be70e5SJerin Jacob 		res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
1514f7be70e5SJerin Jacob 		if (res < 0) {
1515f7be70e5SJerin Jacob 			res = -ENODEV;
1516f7be70e5SJerin Jacob 			octeontx_log_err("failed to link port (%d): res=%d",
1517f7be70e5SJerin Jacob 					i, res);
1518f7be70e5SJerin Jacob 			goto parse_error;
1519f7be70e5SJerin Jacob 		}
1520f7be70e5SJerin Jacob 	}
1521f7be70e5SJerin Jacob 
1522f7be70e5SJerin Jacob 	/* Create ethdev interface */
1523f7be70e5SJerin Jacob 	for (i = 0; i < init_params.nr_port; i++) {
1524f7be70e5SJerin Jacob 		port_id = octeontx_create(dev, i, evdev, socket_id);
1525f7be70e5SJerin Jacob 		if (port_id < 0) {
1526f7be70e5SJerin Jacob 			octeontx_log_err("failed to create device %s",
1527f7be70e5SJerin Jacob 					dev_name);
1528f7be70e5SJerin Jacob 			res = -ENODEV;
1529f7be70e5SJerin Jacob 			goto parse_error;
1530f7be70e5SJerin Jacob 		}
1531f7be70e5SJerin Jacob 
1532f7be70e5SJerin Jacob 		PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
1533f7be70e5SJerin Jacob 					port_id);
1534f7be70e5SJerin Jacob 	}
1535f7be70e5SJerin Jacob 
1536f7be70e5SJerin Jacob 	if (probe_once) {
1537f7be70e5SJerin Jacob 		octeontx_log_err("interface %s not supported", dev_name);
1538f7be70e5SJerin Jacob 		octeontx_remove(dev);
1539f7be70e5SJerin Jacob 		res = -ENOTSUP;
1540f7be70e5SJerin Jacob 		goto parse_error;
1541f7be70e5SJerin Jacob 	}
1542dfb0c75bSPavan Nikhilesh 	rte_mbuf_set_platform_mempool_ops("octeontx_fpavf");
1543f7be70e5SJerin Jacob 	probe_once = 1;
1544f7be70e5SJerin Jacob 
1545f7be70e5SJerin Jacob 	return 0;
1546f7be70e5SJerin Jacob 
1547f7be70e5SJerin Jacob parse_error:
1548f7be70e5SJerin Jacob 	octeontx_pko_fc_free();
1549f7be70e5SJerin Jacob 	return res;
1550f7be70e5SJerin Jacob }
1551f7be70e5SJerin Jacob 
1552f7be70e5SJerin Jacob static struct rte_vdev_driver octeontx_pmd_drv = {
1553f7be70e5SJerin Jacob 	.probe = octeontx_probe,
1554f7be70e5SJerin Jacob 	.remove = octeontx_remove,
1555f7be70e5SJerin Jacob };
1556f7be70e5SJerin Jacob 
1557f7be70e5SJerin Jacob RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
1558f7be70e5SJerin Jacob RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
1559f7be70e5SJerin Jacob RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");
1560