xref: /dpdk/drivers/net/enic/enic_ethdev.c (revision 1e81dbb5321b2bcfdba41a1f237d6534faef2c6e)
12e99ea80SHyong Youb Kim /* SPDX-License-Identifier: BSD-3-Clause
22e99ea80SHyong Youb Kim  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
372f3de30SBruce Richardson  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
472f3de30SBruce Richardson  */
572f3de30SBruce Richardson 
672f3de30SBruce Richardson #include <stdio.h>
772f3de30SBruce Richardson #include <stdint.h>
872f3de30SBruce Richardson 
972f3de30SBruce Richardson #include <rte_dev.h>
1072f3de30SBruce Richardson #include <rte_pci.h>
11c752998bSGaetan Rivet #include <rte_bus_pci.h>
12ffc905f3SFerruh Yigit #include <rte_ethdev_driver.h>
13fdf91e0fSJan Blunck #include <rte_ethdev_pci.h>
1472f3de30SBruce Richardson #include <rte_string_fns.h>
1572f3de30SBruce Richardson 
1672f3de30SBruce Richardson #include "vnic_intr.h"
1772f3de30SBruce Richardson #include "vnic_cq.h"
1872f3de30SBruce Richardson #include "vnic_wq.h"
1972f3de30SBruce Richardson #include "vnic_rq.h"
2072f3de30SBruce Richardson #include "vnic_enet.h"
2172f3de30SBruce Richardson #include "enic.h"
2272f3de30SBruce Richardson 
2336efba2fSHyong Youb Kim int enicpmd_logtype_init;
2436efba2fSHyong Youb Kim int enicpmd_logtype_flow;
2536efba2fSHyong Youb Kim 
2636efba2fSHyong Youb Kim #define PMD_INIT_LOG(level, fmt, args...) \
2736efba2fSHyong Youb Kim 	rte_log(RTE_LOG_ ## level, enicpmd_logtype_init, \
2836efba2fSHyong Youb Kim 		"%s" fmt "\n", __func__, ##args)
2936efba2fSHyong Youb Kim 
3036efba2fSHyong Youb Kim #define ENICPMD_FUNC_TRACE() PMD_INIT_LOG(DEBUG, " >>")
3172f3de30SBruce Richardson 
3272f3de30SBruce Richardson /*
3372f3de30SBruce Richardson  * The set of PCI devices this driver supports
3472f3de30SBruce Richardson  */
350b6fe7bdSDavid Marchand #define CISCO_PCI_VENDOR_ID 0x1137
3672f3de30SBruce Richardson static const struct rte_pci_id pci_id_enic_map[] = {
370b6fe7bdSDavid Marchand 	{ RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET) },
380b6fe7bdSDavid Marchand 	{ RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
390b6fe7bdSDavid Marchand 	{.vendor_id = 0, /* sentinel */},
4072f3de30SBruce Richardson };
4172f3de30SBruce Richardson 
4236efba2fSHyong Youb Kim RTE_INIT(enicpmd_init_log);
4336efba2fSHyong Youb Kim static void
4436efba2fSHyong Youb Kim enicpmd_init_log(void)
4536efba2fSHyong Youb Kim {
4636efba2fSHyong Youb Kim 	enicpmd_logtype_init = rte_log_register("pmd.enic.init");
4736efba2fSHyong Youb Kim 	if (enicpmd_logtype_init >= 0)
4836efba2fSHyong Youb Kim 		rte_log_set_level(enicpmd_logtype_init, RTE_LOG_NOTICE);
4936efba2fSHyong Youb Kim 	enicpmd_logtype_flow = rte_log_register("pmd.enic.flow");
5036efba2fSHyong Youb Kim 	if (enicpmd_logtype_flow >= 0)
5136efba2fSHyong Youb Kim 		rte_log_set_level(enicpmd_logtype_flow, RTE_LOG_NOTICE);
5236efba2fSHyong Youb Kim }
5336efba2fSHyong Youb Kim 
5472f3de30SBruce Richardson static int
5572f3de30SBruce Richardson enicpmd_fdir_ctrl_func(struct rte_eth_dev *eth_dev,
5672f3de30SBruce Richardson 			enum rte_filter_op filter_op, void *arg)
5772f3de30SBruce Richardson {
5872f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
5972f3de30SBruce Richardson 	int ret = 0;
6072f3de30SBruce Richardson 
6172f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
6272f3de30SBruce Richardson 	if (filter_op == RTE_ETH_FILTER_NOP)
6372f3de30SBruce Richardson 		return 0;
6472f3de30SBruce Richardson 
6572f3de30SBruce Richardson 	if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
6672f3de30SBruce Richardson 		return -EINVAL;
6772f3de30SBruce Richardson 
6872f3de30SBruce Richardson 	switch (filter_op) {
6972f3de30SBruce Richardson 	case RTE_ETH_FILTER_ADD:
7072f3de30SBruce Richardson 	case RTE_ETH_FILTER_UPDATE:
7172f3de30SBruce Richardson 		ret = enic_fdir_add_fltr(enic,
7272f3de30SBruce Richardson 			(struct rte_eth_fdir_filter *)arg);
7372f3de30SBruce Richardson 		break;
7472f3de30SBruce Richardson 
7572f3de30SBruce Richardson 	case RTE_ETH_FILTER_DELETE:
7672f3de30SBruce Richardson 		ret = enic_fdir_del_fltr(enic,
7772f3de30SBruce Richardson 			(struct rte_eth_fdir_filter *)arg);
7872f3de30SBruce Richardson 		break;
7972f3de30SBruce Richardson 
8072f3de30SBruce Richardson 	case RTE_ETH_FILTER_STATS:
8172f3de30SBruce Richardson 		enic_fdir_stats_get(enic, (struct rte_eth_fdir_stats *)arg);
8272f3de30SBruce Richardson 		break;
8372f3de30SBruce Richardson 
8472f3de30SBruce Richardson 	case RTE_ETH_FILTER_FLUSH:
8572f3de30SBruce Richardson 		dev_warning(enic, "unsupported operation %u", filter_op);
8672f3de30SBruce Richardson 		ret = -ENOTSUP;
8772f3de30SBruce Richardson 		break;
88dfbd6a9cSJohn Daley 	case RTE_ETH_FILTER_INFO:
89dfbd6a9cSJohn Daley 		enic_fdir_info_get(enic, (struct rte_eth_fdir_info *)arg);
90dfbd6a9cSJohn Daley 		break;
9172f3de30SBruce Richardson 	default:
9272f3de30SBruce Richardson 		dev_err(enic, "unknown operation %u", filter_op);
9372f3de30SBruce Richardson 		ret = -EINVAL;
9472f3de30SBruce Richardson 		break;
9572f3de30SBruce Richardson 	}
9672f3de30SBruce Richardson 	return ret;
9772f3de30SBruce Richardson }
9872f3de30SBruce Richardson 
9972f3de30SBruce Richardson static int
10072f3de30SBruce Richardson enicpmd_dev_filter_ctrl(struct rte_eth_dev *dev,
10172f3de30SBruce Richardson 		     enum rte_filter_type filter_type,
10272f3de30SBruce Richardson 		     enum rte_filter_op filter_op,
10372f3de30SBruce Richardson 		     void *arg)
10472f3de30SBruce Richardson {
1050f766680SJohn Daley 	int ret = 0;
10672f3de30SBruce Richardson 
1070f766680SJohn Daley 	ENICPMD_FUNC_TRACE();
1080f766680SJohn Daley 
1090f766680SJohn Daley 	switch (filter_type) {
1100f766680SJohn Daley 	case RTE_ETH_FILTER_GENERIC:
1110f766680SJohn Daley 		if (filter_op != RTE_ETH_FILTER_GET)
1120f766680SJohn Daley 			return -EINVAL;
1130f766680SJohn Daley 		*(const void **)arg = &enic_flow_ops;
1140f766680SJohn Daley 		break;
1150f766680SJohn Daley 	case RTE_ETH_FILTER_FDIR:
11672f3de30SBruce Richardson 		ret = enicpmd_fdir_ctrl_func(dev, filter_op, arg);
1170f766680SJohn Daley 		break;
1180f766680SJohn Daley 	default:
11972f3de30SBruce Richardson 		dev_warning(enic, "Filter type (%d) not supported",
12072f3de30SBruce Richardson 			filter_type);
1210f766680SJohn Daley 		ret = -EINVAL;
1220f766680SJohn Daley 		break;
1230f766680SJohn Daley 	}
12472f3de30SBruce Richardson 
12572f3de30SBruce Richardson 	return ret;
12672f3de30SBruce Richardson }
12772f3de30SBruce Richardson 
12872f3de30SBruce Richardson static void enicpmd_dev_tx_queue_release(void *txq)
12972f3de30SBruce Richardson {
13072f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
1310e804034SJohn Daley 
1320e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1330e804034SJohn Daley 		return;
1340e804034SJohn Daley 
13572f3de30SBruce Richardson 	enic_free_wq(txq);
13672f3de30SBruce Richardson }
13772f3de30SBruce Richardson 
13872f3de30SBruce Richardson static int enicpmd_dev_setup_intr(struct enic *enic)
13972f3de30SBruce Richardson {
14072f3de30SBruce Richardson 	int ret;
14172f3de30SBruce Richardson 	unsigned int index;
14272f3de30SBruce Richardson 
14372f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
14472f3de30SBruce Richardson 
14572f3de30SBruce Richardson 	/* Are we done with the init of all the queues? */
14672f3de30SBruce Richardson 	for (index = 0; index < enic->cq_count; index++) {
14772f3de30SBruce Richardson 		if (!enic->cq[index].ctrl)
14872f3de30SBruce Richardson 			break;
14972f3de30SBruce Richardson 	}
15072f3de30SBruce Richardson 	if (enic->cq_count != index)
15172f3de30SBruce Richardson 		return 0;
152954828b8SJohn Daley 	for (index = 0; index < enic->wq_count; index++) {
153954828b8SJohn Daley 		if (!enic->wq[index].ctrl)
154954828b8SJohn Daley 			break;
155954828b8SJohn Daley 	}
156954828b8SJohn Daley 	if (enic->wq_count != index)
157954828b8SJohn Daley 		return 0;
158954828b8SJohn Daley 	/* check start of packet (SOP) RQs only in case scatter is disabled. */
159954828b8SJohn Daley 	for (index = 0; index < enic->rq_count; index++) {
160aa07bf8fSJohn Daley 		if (!enic->rq[enic_rte_rq_idx_to_sop_idx(index)].ctrl)
161954828b8SJohn Daley 			break;
162954828b8SJohn Daley 	}
163954828b8SJohn Daley 	if (enic->rq_count != index)
164954828b8SJohn Daley 		return 0;
16572f3de30SBruce Richardson 
16672f3de30SBruce Richardson 	ret = enic_alloc_intr_resources(enic);
16772f3de30SBruce Richardson 	if (ret) {
16872f3de30SBruce Richardson 		dev_err(enic, "alloc intr failed\n");
16972f3de30SBruce Richardson 		return ret;
17072f3de30SBruce Richardson 	}
17172f3de30SBruce Richardson 	enic_init_vnic_resources(enic);
17272f3de30SBruce Richardson 
17372f3de30SBruce Richardson 	ret = enic_setup_finish(enic);
17472f3de30SBruce Richardson 	if (ret)
17572f3de30SBruce Richardson 		dev_err(enic, "setup could not be finished\n");
17672f3de30SBruce Richardson 
17772f3de30SBruce Richardson 	return ret;
17872f3de30SBruce Richardson }
17972f3de30SBruce Richardson 
18072f3de30SBruce Richardson static int enicpmd_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
18172f3de30SBruce Richardson 	uint16_t queue_idx,
18272f3de30SBruce Richardson 	uint16_t nb_desc,
18372f3de30SBruce Richardson 	unsigned int socket_id,
18472f3de30SBruce Richardson 	__rte_unused const struct rte_eth_txconf *tx_conf)
18572f3de30SBruce Richardson {
18672f3de30SBruce Richardson 	int ret;
18772f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
18872f3de30SBruce Richardson 
1890e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1900e804034SJohn Daley 		return -E_RTE_SECONDARY;
1910e804034SJohn Daley 
19272f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
1936c45c330SHyong Youb Kim 	RTE_ASSERT(queue_idx < enic->conf_wq_count);
19472f3de30SBruce Richardson 	eth_dev->data->tx_queues[queue_idx] = (void *)&enic->wq[queue_idx];
19572f3de30SBruce Richardson 
19672f3de30SBruce Richardson 	ret = enic_alloc_wq(enic, queue_idx, socket_id, nb_desc);
19772f3de30SBruce Richardson 	if (ret) {
19872f3de30SBruce Richardson 		dev_err(enic, "error in allocating wq\n");
19972f3de30SBruce Richardson 		return ret;
20072f3de30SBruce Richardson 	}
20172f3de30SBruce Richardson 
20272f3de30SBruce Richardson 	return enicpmd_dev_setup_intr(enic);
20372f3de30SBruce Richardson }
20472f3de30SBruce Richardson 
20572f3de30SBruce Richardson static int enicpmd_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
20672f3de30SBruce Richardson 	uint16_t queue_idx)
20772f3de30SBruce Richardson {
20872f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
20972f3de30SBruce Richardson 
21072f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
21172f3de30SBruce Richardson 
21272f3de30SBruce Richardson 	enic_start_wq(enic, queue_idx);
21372f3de30SBruce Richardson 
21472f3de30SBruce Richardson 	return 0;
21572f3de30SBruce Richardson }
21672f3de30SBruce Richardson 
21772f3de30SBruce Richardson static int enicpmd_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
21872f3de30SBruce Richardson 	uint16_t queue_idx)
21972f3de30SBruce Richardson {
22072f3de30SBruce Richardson 	int ret;
22172f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
22272f3de30SBruce Richardson 
22372f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
22472f3de30SBruce Richardson 
22572f3de30SBruce Richardson 	ret = enic_stop_wq(enic, queue_idx);
22672f3de30SBruce Richardson 	if (ret)
22772f3de30SBruce Richardson 		dev_err(enic, "error in stopping wq %d\n", queue_idx);
22872f3de30SBruce Richardson 
22972f3de30SBruce Richardson 	return ret;
23072f3de30SBruce Richardson }
23172f3de30SBruce Richardson 
23272f3de30SBruce Richardson static int enicpmd_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
23372f3de30SBruce Richardson 	uint16_t queue_idx)
23472f3de30SBruce Richardson {
23572f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
23672f3de30SBruce Richardson 
23772f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
23872f3de30SBruce Richardson 
23972f3de30SBruce Richardson 	enic_start_rq(enic, queue_idx);
24072f3de30SBruce Richardson 
24172f3de30SBruce Richardson 	return 0;
24272f3de30SBruce Richardson }
24372f3de30SBruce Richardson 
24472f3de30SBruce Richardson static int enicpmd_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
24572f3de30SBruce Richardson 	uint16_t queue_idx)
24672f3de30SBruce Richardson {
24772f3de30SBruce Richardson 	int ret;
24872f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
24972f3de30SBruce Richardson 
25072f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
25172f3de30SBruce Richardson 
25272f3de30SBruce Richardson 	ret = enic_stop_rq(enic, queue_idx);
25372f3de30SBruce Richardson 	if (ret)
25472f3de30SBruce Richardson 		dev_err(enic, "error in stopping rq %d\n", queue_idx);
25572f3de30SBruce Richardson 
25672f3de30SBruce Richardson 	return ret;
25772f3de30SBruce Richardson }
25872f3de30SBruce Richardson 
25972f3de30SBruce Richardson static void enicpmd_dev_rx_queue_release(void *rxq)
26072f3de30SBruce Richardson {
26172f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
2620e804034SJohn Daley 
2630e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2640e804034SJohn Daley 		return;
2650e804034SJohn Daley 
26672f3de30SBruce Richardson 	enic_free_rq(rxq);
26772f3de30SBruce Richardson }
26872f3de30SBruce Richardson 
269a787f7e6SNelson Escobar static uint32_t enicpmd_dev_rx_queue_count(struct rte_eth_dev *dev,
270a787f7e6SNelson Escobar 					   uint16_t rx_queue_id)
271a787f7e6SNelson Escobar {
272a787f7e6SNelson Escobar 	struct enic *enic = pmd_priv(dev);
273a787f7e6SNelson Escobar 	uint32_t queue_count = 0;
274a787f7e6SNelson Escobar 	struct vnic_cq *cq;
275a787f7e6SNelson Escobar 	uint32_t cq_tail;
276a787f7e6SNelson Escobar 	uint16_t cq_idx;
277a787f7e6SNelson Escobar 	int rq_num;
278a787f7e6SNelson Escobar 
279aa07bf8fSJohn Daley 	rq_num = enic_rte_rq_idx_to_sop_idx(rx_queue_id);
280a787f7e6SNelson Escobar 	cq = &enic->cq[enic_cq_rq(enic, rq_num)];
281a787f7e6SNelson Escobar 	cq_idx = cq->to_clean;
282a787f7e6SNelson Escobar 
283a787f7e6SNelson Escobar 	cq_tail = ioread32(&cq->ctrl->cq_tail);
284a787f7e6SNelson Escobar 
285a787f7e6SNelson Escobar 	if (cq_tail < cq_idx)
286a787f7e6SNelson Escobar 		cq_tail += cq->ring.desc_count;
287a787f7e6SNelson Escobar 
288a787f7e6SNelson Escobar 	queue_count = cq_tail - cq_idx;
289a787f7e6SNelson Escobar 
290a787f7e6SNelson Escobar 	return queue_count;
291a787f7e6SNelson Escobar }
292a787f7e6SNelson Escobar 
29372f3de30SBruce Richardson static int enicpmd_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
29472f3de30SBruce Richardson 	uint16_t queue_idx,
29572f3de30SBruce Richardson 	uint16_t nb_desc,
29672f3de30SBruce Richardson 	unsigned int socket_id,
297947d860cSJohn Daley 	const struct rte_eth_rxconf *rx_conf,
29872f3de30SBruce Richardson 	struct rte_mempool *mp)
29972f3de30SBruce Richardson {
30072f3de30SBruce Richardson 	int ret;
30172f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
30272f3de30SBruce Richardson 
30372f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
3040e804034SJohn Daley 
3050e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3060e804034SJohn Daley 		return -E_RTE_SECONDARY;
3076c45c330SHyong Youb Kim 	RTE_ASSERT(enic_rte_rq_idx_to_sop_idx(queue_idx) < enic->conf_rq_count);
308856d7ba7SNelson Escobar 	eth_dev->data->rx_queues[queue_idx] =
309aa07bf8fSJohn Daley 		(void *)&enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
31072f3de30SBruce Richardson 
311ce16fd70SJohn Daley 	ret = enic_alloc_rq(enic, queue_idx, socket_id, mp, nb_desc,
312ce16fd70SJohn Daley 			    rx_conf->rx_free_thresh);
31372f3de30SBruce Richardson 	if (ret) {
31472f3de30SBruce Richardson 		dev_err(enic, "error in allocating rq\n");
31572f3de30SBruce Richardson 		return ret;
31672f3de30SBruce Richardson 	}
31772f3de30SBruce Richardson 
31872f3de30SBruce Richardson 	return enicpmd_dev_setup_intr(enic);
31972f3de30SBruce Richardson }
32072f3de30SBruce Richardson 
32172f3de30SBruce Richardson static int enicpmd_vlan_filter_set(struct rte_eth_dev *eth_dev,
32272f3de30SBruce Richardson 	uint16_t vlan_id, int on)
32372f3de30SBruce Richardson {
32472f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
3254d4a76a6SJulien Meunier 	int err;
32672f3de30SBruce Richardson 
32772f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
32872f3de30SBruce Richardson 	if (on)
3294d4a76a6SJulien Meunier 		err = enic_add_vlan(enic, vlan_id);
33072f3de30SBruce Richardson 	else
3314d4a76a6SJulien Meunier 		err = enic_del_vlan(enic, vlan_id);
3324d4a76a6SJulien Meunier 	return err;
33372f3de30SBruce Richardson }
33472f3de30SBruce Richardson 
335289ba0c0SDavid Harton static int enicpmd_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
33672f3de30SBruce Richardson {
33772f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
33872f3de30SBruce Richardson 
33972f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
34072f3de30SBruce Richardson 
34172f3de30SBruce Richardson 	if (mask & ETH_VLAN_STRIP_MASK) {
342a062bafaSHyong Youb Kim 		if (eth_dev->data->dev_conf.rxmode.offloads &
343a062bafaSHyong Youb Kim 		    DEV_RX_OFFLOAD_VLAN_STRIP)
34472f3de30SBruce Richardson 			enic->ig_vlan_strip_en = 1;
34572f3de30SBruce Richardson 		else
34672f3de30SBruce Richardson 			enic->ig_vlan_strip_en = 0;
34772f3de30SBruce Richardson 	}
34872f3de30SBruce Richardson 	enic_set_rss_nic_cfg(enic);
34972f3de30SBruce Richardson 
35072f3de30SBruce Richardson 
35172f3de30SBruce Richardson 	if (mask & ETH_VLAN_FILTER_MASK) {
35272f3de30SBruce Richardson 		dev_warning(enic,
35372f3de30SBruce Richardson 			"Configuration of VLAN filter is not supported\n");
35472f3de30SBruce Richardson 	}
35572f3de30SBruce Richardson 
35672f3de30SBruce Richardson 	if (mask & ETH_VLAN_EXTEND_MASK) {
35772f3de30SBruce Richardson 		dev_warning(enic,
35872f3de30SBruce Richardson 			"Configuration of extended VLAN is not supported\n");
35972f3de30SBruce Richardson 	}
360289ba0c0SDavid Harton 
361289ba0c0SDavid Harton 	return 0;
36272f3de30SBruce Richardson }
36372f3de30SBruce Richardson 
36472f3de30SBruce Richardson static int enicpmd_dev_configure(struct rte_eth_dev *eth_dev)
36572f3de30SBruce Richardson {
36672f3de30SBruce Richardson 	int ret;
36772f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
36872f3de30SBruce Richardson 
3690e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3700e804034SJohn Daley 		return -E_RTE_SECONDARY;
3710e804034SJohn Daley 
37272f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
37372f3de30SBruce Richardson 	ret = enic_set_vnic_res(enic);
37472f3de30SBruce Richardson 	if (ret) {
37572f3de30SBruce Richardson 		dev_err(enic, "Set vNIC resource num  failed, aborting\n");
37672f3de30SBruce Richardson 		return ret;
37772f3de30SBruce Richardson 	}
37872f3de30SBruce Richardson 
379a062bafaSHyong Youb Kim 	enic->hw_ip_checksum = !!(eth_dev->data->dev_conf.rxmode.offloads &
380a062bafaSHyong Youb Kim 				  DEV_RX_OFFLOAD_CHECKSUM);
381289ba0c0SDavid Harton 	ret = enicpmd_vlan_offload_set(eth_dev, ETH_VLAN_STRIP_MASK);
382289ba0c0SDavid Harton 
383289ba0c0SDavid Harton 	return ret;
38472f3de30SBruce Richardson }
38572f3de30SBruce Richardson 
38672f3de30SBruce Richardson /* Start the device.
38772f3de30SBruce Richardson  * It returns 0 on success.
38872f3de30SBruce Richardson  */
38972f3de30SBruce Richardson static int enicpmd_dev_start(struct rte_eth_dev *eth_dev)
39072f3de30SBruce Richardson {
39172f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
39272f3de30SBruce Richardson 
3930e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3940e804034SJohn Daley 		return -E_RTE_SECONDARY;
3950e804034SJohn Daley 
39672f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
39772f3de30SBruce Richardson 	return enic_enable(enic);
39872f3de30SBruce Richardson }
39972f3de30SBruce Richardson 
40072f3de30SBruce Richardson /*
40172f3de30SBruce Richardson  * Stop device: disable rx and tx functions to allow for reconfiguring.
40272f3de30SBruce Richardson  */
40372f3de30SBruce Richardson static void enicpmd_dev_stop(struct rte_eth_dev *eth_dev)
40472f3de30SBruce Richardson {
40572f3de30SBruce Richardson 	struct rte_eth_link link;
40672f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
40772f3de30SBruce Richardson 
4080e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
4090e804034SJohn Daley 		return;
4100e804034SJohn Daley 
41172f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
41272f3de30SBruce Richardson 	enic_disable(enic);
41372f3de30SBruce Richardson 	memset(&link, 0, sizeof(link));
41472f3de30SBruce Richardson 	rte_atomic64_cmpset((uint64_t *)&eth_dev->data->dev_link,
41572f3de30SBruce Richardson 		*(uint64_t *)&eth_dev->data->dev_link,
41672f3de30SBruce Richardson 		*(uint64_t *)&link);
41772f3de30SBruce Richardson }
41872f3de30SBruce Richardson 
41972f3de30SBruce Richardson /*
42072f3de30SBruce Richardson  * Stop device.
42172f3de30SBruce Richardson  */
42272f3de30SBruce Richardson static void enicpmd_dev_close(struct rte_eth_dev *eth_dev)
42372f3de30SBruce Richardson {
42472f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
42572f3de30SBruce Richardson 
42672f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
42772f3de30SBruce Richardson 	enic_remove(enic);
42872f3de30SBruce Richardson }
42972f3de30SBruce Richardson 
43072f3de30SBruce Richardson static int enicpmd_dev_link_update(struct rte_eth_dev *eth_dev,
43172f3de30SBruce Richardson 	__rte_unused int wait_to_complete)
43272f3de30SBruce Richardson {
43372f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
43472f3de30SBruce Richardson 
43572f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
436cf8d9826SNelson Escobar 	return enic_link_update(enic);
43772f3de30SBruce Richardson }
43872f3de30SBruce Richardson 
439d5b0924bSMatan Azrad static int enicpmd_dev_stats_get(struct rte_eth_dev *eth_dev,
44072f3de30SBruce Richardson 	struct rte_eth_stats *stats)
44172f3de30SBruce Richardson {
44272f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
44372f3de30SBruce Richardson 
44472f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
445d5b0924bSMatan Azrad 	return enic_dev_stats_get(enic, stats);
44672f3de30SBruce Richardson }
44772f3de30SBruce Richardson 
44872f3de30SBruce Richardson static void enicpmd_dev_stats_reset(struct rte_eth_dev *eth_dev)
44972f3de30SBruce Richardson {
45072f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
45172f3de30SBruce Richardson 
45272f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
45372f3de30SBruce Richardson 	enic_dev_stats_clear(enic);
45472f3de30SBruce Richardson }
45572f3de30SBruce Richardson 
45672f3de30SBruce Richardson static void enicpmd_dev_info_get(struct rte_eth_dev *eth_dev,
45772f3de30SBruce Richardson 	struct rte_eth_dev_info *device_info)
45872f3de30SBruce Richardson {
45972f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
46072f3de30SBruce Richardson 
46172f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
462c0802544SFerruh Yigit 	device_info->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
463ce93d3c3SNelson Escobar 	/* Scattered Rx uses two receive queues per rx queue exposed to dpdk */
464ce93d3c3SNelson Escobar 	device_info->max_rx_queues = enic->conf_rq_count / 2;
465ce93d3c3SNelson Escobar 	device_info->max_tx_queues = enic->conf_wq_count;
46672f3de30SBruce Richardson 	device_info->min_rx_bufsize = ENIC_MIN_MTU;
467f201fd05SJohn Daley 	device_info->max_rx_pktlen = enic->max_mtu + ETHER_HDR_LEN + 4;
468bbab3d97SJohn Daley 	device_info->max_mac_addrs = ENIC_MAX_MAC_ADDR;
46972f3de30SBruce Richardson 	device_info->rx_offload_capa =
47072f3de30SBruce Richardson 		DEV_RX_OFFLOAD_VLAN_STRIP |
47172f3de30SBruce Richardson 		DEV_RX_OFFLOAD_IPV4_CKSUM |
47272f3de30SBruce Richardson 		DEV_RX_OFFLOAD_UDP_CKSUM  |
47372f3de30SBruce Richardson 		DEV_RX_OFFLOAD_TCP_CKSUM;
47472f3de30SBruce Richardson 	device_info->tx_offload_capa =
47572f3de30SBruce Richardson 		DEV_TX_OFFLOAD_VLAN_INSERT |
47672f3de30SBruce Richardson 		DEV_TX_OFFLOAD_IPV4_CKSUM  |
47772f3de30SBruce Richardson 		DEV_TX_OFFLOAD_UDP_CKSUM   |
478026afc76SJohn Daley 		DEV_TX_OFFLOAD_TCP_CKSUM   |
479026afc76SJohn Daley 		DEV_TX_OFFLOAD_TCP_TSO;
480947d860cSJohn Daley 	device_info->default_rxconf = (struct rte_eth_rxconf) {
481947d860cSJohn Daley 		.rx_free_thresh = ENIC_DEFAULT_RX_FREE_THRESH
482947d860cSJohn Daley 	};
48372f3de30SBruce Richardson }
48472f3de30SBruce Richardson 
48578a38edfSJianfeng Tan static const uint32_t *enicpmd_dev_supported_ptypes_get(struct rte_eth_dev *dev)
48678a38edfSJianfeng Tan {
48778a38edfSJianfeng Tan 	static const uint32_t ptypes[] = {
488c6f45550SJohn Daley 		RTE_PTYPE_L2_ETHER,
489c6f45550SJohn Daley 		RTE_PTYPE_L2_ETHER_VLAN,
490097e1f1eSNelson Escobar 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
491097e1f1eSNelson Escobar 		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
492097e1f1eSNelson Escobar 		RTE_PTYPE_L4_TCP,
493097e1f1eSNelson Escobar 		RTE_PTYPE_L4_UDP,
494097e1f1eSNelson Escobar 		RTE_PTYPE_L4_FRAG,
495097e1f1eSNelson Escobar 		RTE_PTYPE_L4_NONFRAG,
49678a38edfSJianfeng Tan 		RTE_PTYPE_UNKNOWN
49778a38edfSJianfeng Tan 	};
49878a38edfSJianfeng Tan 
49978a38edfSJianfeng Tan 	if (dev->rx_pkt_burst == enic_recv_pkts)
50078a38edfSJianfeng Tan 		return ptypes;
50178a38edfSJianfeng Tan 	return NULL;
50278a38edfSJianfeng Tan }
50378a38edfSJianfeng Tan 
50472f3de30SBruce Richardson static void enicpmd_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
50572f3de30SBruce Richardson {
50672f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
50772f3de30SBruce Richardson 
5080e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5090e804034SJohn Daley 		return;
5100e804034SJohn Daley 
51172f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
5120e804034SJohn Daley 
51372f3de30SBruce Richardson 	enic->promisc = 1;
51472f3de30SBruce Richardson 	enic_add_packet_filter(enic);
51572f3de30SBruce Richardson }
51672f3de30SBruce Richardson 
51772f3de30SBruce Richardson static void enicpmd_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
51872f3de30SBruce Richardson {
51972f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
52072f3de30SBruce Richardson 
5210e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5220e804034SJohn Daley 		return;
5230e804034SJohn Daley 
52472f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
52572f3de30SBruce Richardson 	enic->promisc = 0;
52672f3de30SBruce Richardson 	enic_add_packet_filter(enic);
52772f3de30SBruce Richardson }
52872f3de30SBruce Richardson 
52972f3de30SBruce Richardson static void enicpmd_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
53072f3de30SBruce Richardson {
53172f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
53272f3de30SBruce Richardson 
5330e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5340e804034SJohn Daley 		return;
5350e804034SJohn Daley 
53672f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
53772f3de30SBruce Richardson 	enic->allmulti = 1;
53872f3de30SBruce Richardson 	enic_add_packet_filter(enic);
53972f3de30SBruce Richardson }
54072f3de30SBruce Richardson 
54172f3de30SBruce Richardson static void enicpmd_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
54272f3de30SBruce Richardson {
54372f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
54472f3de30SBruce Richardson 
5450e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5460e804034SJohn Daley 		return;
5470e804034SJohn Daley 
54872f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
54972f3de30SBruce Richardson 	enic->allmulti = 0;
55072f3de30SBruce Richardson 	enic_add_packet_filter(enic);
55172f3de30SBruce Richardson }
55272f3de30SBruce Richardson 
5536d01e580SWei Dai static int enicpmd_add_mac_addr(struct rte_eth_dev *eth_dev,
55472f3de30SBruce Richardson 	struct ether_addr *mac_addr,
55572f3de30SBruce Richardson 	__rte_unused uint32_t index, __rte_unused uint32_t pool)
55672f3de30SBruce Richardson {
55772f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
55872f3de30SBruce Richardson 
5590e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5600e804034SJohn Daley 		return -E_RTE_SECONDARY;
5610e804034SJohn Daley 
56272f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
5636d01e580SWei Dai 	return enic_set_mac_address(enic, mac_addr->addr_bytes);
56472f3de30SBruce Richardson }
56572f3de30SBruce Richardson 
566bbab3d97SJohn Daley static void enicpmd_remove_mac_addr(struct rte_eth_dev *eth_dev, uint32_t index)
56772f3de30SBruce Richardson {
56872f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
56972f3de30SBruce Richardson 
5700e804034SJohn Daley 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5710e804034SJohn Daley 		return;
5720e804034SJohn Daley 
57372f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
574bbab3d97SJohn Daley 	enic_del_mac_address(enic, index);
57572f3de30SBruce Richardson }
57672f3de30SBruce Richardson 
577396a6d71SJohn Daley static int enicpmd_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
578396a6d71SJohn Daley {
579396a6d71SJohn Daley 	struct enic *enic = pmd_priv(eth_dev);
580396a6d71SJohn Daley 
581396a6d71SJohn Daley 	ENICPMD_FUNC_TRACE();
582396a6d71SJohn Daley 	return enic_set_mtu(enic, mtu);
583396a6d71SJohn Daley }
584396a6d71SJohn Daley 
58572f3de30SBruce Richardson static const struct eth_dev_ops enicpmd_eth_dev_ops = {
58672f3de30SBruce Richardson 	.dev_configure        = enicpmd_dev_configure,
58772f3de30SBruce Richardson 	.dev_start            = enicpmd_dev_start,
58872f3de30SBruce Richardson 	.dev_stop             = enicpmd_dev_stop,
58972f3de30SBruce Richardson 	.dev_set_link_up      = NULL,
59072f3de30SBruce Richardson 	.dev_set_link_down    = NULL,
59172f3de30SBruce Richardson 	.dev_close            = enicpmd_dev_close,
59272f3de30SBruce Richardson 	.promiscuous_enable   = enicpmd_dev_promiscuous_enable,
59372f3de30SBruce Richardson 	.promiscuous_disable  = enicpmd_dev_promiscuous_disable,
59472f3de30SBruce Richardson 	.allmulticast_enable  = enicpmd_dev_allmulticast_enable,
59572f3de30SBruce Richardson 	.allmulticast_disable = enicpmd_dev_allmulticast_disable,
59672f3de30SBruce Richardson 	.link_update          = enicpmd_dev_link_update,
59772f3de30SBruce Richardson 	.stats_get            = enicpmd_dev_stats_get,
59872f3de30SBruce Richardson 	.stats_reset          = enicpmd_dev_stats_reset,
59972f3de30SBruce Richardson 	.queue_stats_mapping_set = NULL,
60072f3de30SBruce Richardson 	.dev_infos_get        = enicpmd_dev_info_get,
60178a38edfSJianfeng Tan 	.dev_supported_ptypes_get = enicpmd_dev_supported_ptypes_get,
602396a6d71SJohn Daley 	.mtu_set              = enicpmd_mtu_set,
60372f3de30SBruce Richardson 	.vlan_filter_set      = enicpmd_vlan_filter_set,
60472f3de30SBruce Richardson 	.vlan_tpid_set        = NULL,
60572f3de30SBruce Richardson 	.vlan_offload_set     = enicpmd_vlan_offload_set,
60672f3de30SBruce Richardson 	.vlan_strip_queue_set = NULL,
60772f3de30SBruce Richardson 	.rx_queue_start       = enicpmd_dev_rx_queue_start,
60872f3de30SBruce Richardson 	.rx_queue_stop        = enicpmd_dev_rx_queue_stop,
60972f3de30SBruce Richardson 	.tx_queue_start       = enicpmd_dev_tx_queue_start,
61072f3de30SBruce Richardson 	.tx_queue_stop        = enicpmd_dev_tx_queue_stop,
61172f3de30SBruce Richardson 	.rx_queue_setup       = enicpmd_dev_rx_queue_setup,
61272f3de30SBruce Richardson 	.rx_queue_release     = enicpmd_dev_rx_queue_release,
613a787f7e6SNelson Escobar 	.rx_queue_count       = enicpmd_dev_rx_queue_count,
61472f3de30SBruce Richardson 	.rx_descriptor_done   = NULL,
61572f3de30SBruce Richardson 	.tx_queue_setup       = enicpmd_dev_tx_queue_setup,
61672f3de30SBruce Richardson 	.tx_queue_release     = enicpmd_dev_tx_queue_release,
61772f3de30SBruce Richardson 	.dev_led_on           = NULL,
61872f3de30SBruce Richardson 	.dev_led_off          = NULL,
61972f3de30SBruce Richardson 	.flow_ctrl_get        = NULL,
62072f3de30SBruce Richardson 	.flow_ctrl_set        = NULL,
62172f3de30SBruce Richardson 	.priority_flow_ctrl_set = NULL,
62272f3de30SBruce Richardson 	.mac_addr_add         = enicpmd_add_mac_addr,
62372f3de30SBruce Richardson 	.mac_addr_remove      = enicpmd_remove_mac_addr,
62472f3de30SBruce Richardson 	.filter_ctrl          = enicpmd_dev_filter_ctrl,
62572f3de30SBruce Richardson };
62672f3de30SBruce Richardson 
62772f3de30SBruce Richardson struct enic *enicpmd_list_head = NULL;
62872f3de30SBruce Richardson /* Initialize the driver
62972f3de30SBruce Richardson  * It returns 0 on success.
63072f3de30SBruce Richardson  */
63172f3de30SBruce Richardson static int eth_enicpmd_dev_init(struct rte_eth_dev *eth_dev)
63272f3de30SBruce Richardson {
63372f3de30SBruce Richardson 	struct rte_pci_device *pdev;
63472f3de30SBruce Richardson 	struct rte_pci_addr *addr;
63572f3de30SBruce Richardson 	struct enic *enic = pmd_priv(eth_dev);
63672f3de30SBruce Richardson 
63772f3de30SBruce Richardson 	ENICPMD_FUNC_TRACE();
63872f3de30SBruce Richardson 
63972f3de30SBruce Richardson 	enic->port_id = eth_dev->data->port_id;
64072f3de30SBruce Richardson 	enic->rte_dev = eth_dev;
64172f3de30SBruce Richardson 	eth_dev->dev_ops = &enicpmd_eth_dev_ops;
642947d860cSJohn Daley 	eth_dev->rx_pkt_burst = &enic_recv_pkts;
643d309bdc2SJohn Daley 	eth_dev->tx_pkt_burst = &enic_xmit_pkts;
644*1e81dbb5SHyong Youb Kim 	eth_dev->tx_pkt_prepare = &enic_prep_pkts;
64572f3de30SBruce Richardson 
646c0802544SFerruh Yigit 	pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
647eeefe73fSBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pdev);
64872f3de30SBruce Richardson 	enic->pdev = pdev;
64972f3de30SBruce Richardson 	addr = &pdev->addr;
65072f3de30SBruce Richardson 
65172f3de30SBruce Richardson 	snprintf(enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
65272f3de30SBruce Richardson 		addr->domain, addr->bus, addr->devid, addr->function);
65372f3de30SBruce Richardson 
65472f3de30SBruce Richardson 	return enic_probe(enic);
65572f3de30SBruce Richardson }
65672f3de30SBruce Richardson 
657fdf91e0fSJan Blunck static int eth_enic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
658fdf91e0fSJan Blunck 	struct rte_pci_device *pci_dev)
659fdf91e0fSJan Blunck {
660fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct enic),
661fdf91e0fSJan Blunck 		eth_enicpmd_dev_init);
662fdf91e0fSJan Blunck }
663fdf91e0fSJan Blunck 
664fdf91e0fSJan Blunck static int eth_enic_pci_remove(struct rte_pci_device *pci_dev)
665fdf91e0fSJan Blunck {
666fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
667fdf91e0fSJan Blunck }
668fdf91e0fSJan Blunck 
669fdf91e0fSJan Blunck static struct rte_pci_driver rte_enic_pmd = {
67072f3de30SBruce Richardson 	.id_table = pci_id_enic_map,
67153fa8cc0SNelson Escobar 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
672fdf91e0fSJan Blunck 	.probe = eth_enic_pci_probe,
673fdf91e0fSJan Blunck 	.remove = eth_enic_pci_remove,
67472f3de30SBruce Richardson };
67572f3de30SBruce Richardson 
676fdf91e0fSJan Blunck RTE_PMD_REGISTER_PCI(net_enic, rte_enic_pmd);
67701f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_enic, pci_id_enic_map);
67806e81dc9SDavid Marchand RTE_PMD_REGISTER_KMOD_DEP(net_enic, "* igb_uio | uio_pci_generic | vfio-pci");
679