xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision bace76f6)
15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
25566a3e3SBruce Richardson  * Copyright(c) 2010-2016 Intel Corporation
36c3169a3SBruce Richardson  */
46c3169a3SBruce Richardson 
56c3169a3SBruce Richardson #include <stdint.h>
66c3169a3SBruce Richardson #include <string.h>
76c3169a3SBruce Richardson #include <stdio.h>
86c3169a3SBruce Richardson #include <errno.h>
96c3169a3SBruce Richardson #include <unistd.h>
106c3169a3SBruce Richardson 
11ffc905f3SFerruh Yigit #include <rte_ethdev_driver.h>
12fdf91e0fSJan Blunck #include <rte_ethdev_pci.h>
136c3169a3SBruce Richardson #include <rte_memcpy.h>
146c3169a3SBruce Richardson #include <rte_string_fns.h>
156c3169a3SBruce Richardson #include <rte_memzone.h>
166c3169a3SBruce Richardson #include <rte_malloc.h>
176c3169a3SBruce Richardson #include <rte_atomic.h>
186c3169a3SBruce Richardson #include <rte_branch_prediction.h>
196c3169a3SBruce Richardson #include <rte_pci.h>
20c752998bSGaetan Rivet #include <rte_bus_pci.h>
216c3169a3SBruce Richardson #include <rte_ether.h>
227365504fSXiao Wang #include <rte_ip.h>
237365504fSXiao Wang #include <rte_arp.h>
246c3169a3SBruce Richardson #include <rte_common.h>
25abf4c84bSBernard Iremonger #include <rte_errno.h>
264819eae8SOlivier Matz #include <rte_cpuflags.h>
276c3169a3SBruce Richardson 
286c3169a3SBruce Richardson #include <rte_memory.h>
296c3169a3SBruce Richardson #include <rte_eal.h>
306c3169a3SBruce Richardson #include <rte_dev.h>
311978a9dcSXiao Wang #include <rte_cycles.h>
326c3169a3SBruce Richardson 
336c3169a3SBruce Richardson #include "virtio_ethdev.h"
346c3169a3SBruce Richardson #include "virtio_pci.h"
356c3169a3SBruce Richardson #include "virtio_logs.h"
366c3169a3SBruce Richardson #include "virtqueue.h"
37cab04612SHuawei Xie #include "virtio_rxtx.h"
386c3169a3SBruce Richardson 
39abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
406c3169a3SBruce Richardson static int  virtio_dev_configure(struct rte_eth_dev *dev);
416c3169a3SBruce Richardson static int  virtio_dev_start(struct rte_eth_dev *dev);
426c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev);
436c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
446c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
456c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
466c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
476c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev,
486c3169a3SBruce Richardson 				struct rte_eth_dev_info *dev_info);
496c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev,
50dd2c630aSFerruh Yigit 	int wait_to_complete);
51289ba0c0SDavid Harton static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
526c3169a3SBruce Richardson 
536c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw);
546c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw);
556c3169a3SBruce Richardson 
56d5b0924bSMatan Azrad static int virtio_dev_stats_get(struct rte_eth_dev *dev,
5776d4c652SHarry van Haaren 				 struct rte_eth_stats *stats);
5876d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
59e2aae1c1SRemy Horton 				 struct rte_eth_xstat *xstats, unsigned n);
60baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
61baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
62baf91c39SRemy Horton 				       unsigned limit);
636c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
646c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
656c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
666c3169a3SBruce Richardson 				uint16_t vlan_id, int on);
676d01e580SWei Dai static int virtio_mac_addr_add(struct rte_eth_dev *dev,
686c3169a3SBruce Richardson 				struct ether_addr *mac_addr,
69dd2c630aSFerruh Yigit 				uint32_t index, uint32_t vmdq);
706c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
716c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev,
726c3169a3SBruce Richardson 				struct ether_addr *mac_addr);
736c3169a3SBruce Richardson 
74fe19d49cSZhiyong Yang static int virtio_intr_enable(struct rte_eth_dev *dev);
75fe19d49cSZhiyong Yang static int virtio_intr_disable(struct rte_eth_dev *dev);
76fe19d49cSZhiyong Yang 
776c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set(
78dd2c630aSFerruh Yigit 	struct rte_eth_dev *eth_dev,
79dd2c630aSFerruh Yigit 	uint16_t queue_id,
80dd2c630aSFerruh Yigit 	uint8_t stat_idx,
81dd2c630aSFerruh Yigit 	uint8_t is_rx);
826c3169a3SBruce Richardson 
8300628188SStephen Hemminger int virtio_logtype_init;
8400628188SStephen Hemminger int virtio_logtype_driver;
8500628188SStephen Hemminger 
867365504fSXiao Wang static void virtio_notify_peers(struct rte_eth_dev *dev);
877365504fSXiao Wang static void virtio_ack_link_announce(struct rte_eth_dev *dev);
887365504fSXiao Wang 
896c3169a3SBruce Richardson /*
906c3169a3SBruce Richardson  * The set of PCI devices this driver supports
916c3169a3SBruce Richardson  */
926c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = {
934c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
944c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
956c3169a3SBruce Richardson 	{ .vendor_id = 0, /* sentinel */ },
966c3169a3SBruce Richardson };
976c3169a3SBruce Richardson 
9876d4c652SHarry van Haaren struct rte_virtio_xstats_name_off {
9976d4c652SHarry van Haaren 	char name[RTE_ETH_XSTATS_NAME_SIZE];
10076d4c652SHarry van Haaren 	unsigned offset;
10176d4c652SHarry van Haaren };
10276d4c652SHarry van Haaren 
10376d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */
10401ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
10501ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
10601ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
10701ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_rx, stats.errors)},
10801ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
10901ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
11001ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
11101ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
11201ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
11301ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
11401ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
11501ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
11681f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
11781f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
11876d4c652SHarry van Haaren };
11976d4c652SHarry van Haaren 
12001ad44fdSHuawei Xie /* [rt]x_qX_ is prepended to the name string here */
12101ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
12201ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
12301ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
12401ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_tx, stats.errors)},
12501ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
12601ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
12701ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
12801ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
12901ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
13001ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
13101ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
13201ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
13381f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
13481f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
13501ad44fdSHuawei Xie };
13601ad44fdSHuawei Xie 
13701ad44fdSHuawei Xie #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
13801ad44fdSHuawei Xie 			    sizeof(rte_virtio_rxq_stat_strings[0]))
13901ad44fdSHuawei Xie #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
14001ad44fdSHuawei Xie 			    sizeof(rte_virtio_txq_stat_strings[0]))
14176d4c652SHarry van Haaren 
142553f4593SYuanhan Liu struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
143553f4593SYuanhan Liu 
1446c3169a3SBruce Richardson static int
14501ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
1466c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1476c3169a3SBruce Richardson {
1489e71668bSDamjan Marion 	uint32_t head, i;
1496c3169a3SBruce Richardson 	int k, sum = 0;
1506c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
151da4f2e4bSZhiyong Yang 	struct virtio_pmd_ctrl *result;
15201ad44fdSHuawei Xie 	struct virtqueue *vq;
1536c3169a3SBruce Richardson 
1546c3169a3SBruce Richardson 	ctrl->status = status;
1556c3169a3SBruce Richardson 
1563bd60a27SJianfeng Tan 	if (!cvq || !cvq->vq) {
1570bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "Control queue is not supported.");
1586c3169a3SBruce Richardson 		return -1;
1596c3169a3SBruce Richardson 	}
160a2ffb87bSXiao Wang 
161a2ffb87bSXiao Wang 	rte_spinlock_lock(&cvq->lock);
16201ad44fdSHuawei Xie 	vq = cvq->vq;
1639e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1646c3169a3SBruce Richardson 
1656c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1666c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1676c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1686c3169a3SBruce Richardson 
169a2ffb87bSXiao Wang 	if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
170a2ffb87bSXiao Wang 		rte_spinlock_unlock(&cvq->lock);
1716c3169a3SBruce Richardson 		return -1;
172a2ffb87bSXiao Wang 	}
1736c3169a3SBruce Richardson 
17401ad44fdSHuawei Xie 	memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
1756c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1766c3169a3SBruce Richardson 
1776c3169a3SBruce Richardson 	/*
1786c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1796c3169a3SBruce Richardson 	 * One TX packet for header;
1806c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1816c3169a3SBruce Richardson 	 * One RX packet for ACK.
1826c3169a3SBruce Richardson 	 */
1836c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
184f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
1856c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1866c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1876c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
1886c3169a3SBruce Richardson 
1896c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
1906c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
191f24f8f9fSJianfeng Tan 		vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
1926c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
1936c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
1946c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
1956c3169a3SBruce Richardson 		sum += dlen[k];
1966c3169a3SBruce Richardson 		vq->vq_free_cnt--;
1976c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
1986c3169a3SBruce Richardson 	}
1996c3169a3SBruce Richardson 
2006c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
201f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2026c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
2036c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
2046c3169a3SBruce Richardson 	vq->vq_free_cnt--;
2056c3169a3SBruce Richardson 
2066c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
2076c3169a3SBruce Richardson 
2086c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
2096c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
2106c3169a3SBruce Richardson 
2116c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
2126c3169a3SBruce Richardson 
2136c3169a3SBruce Richardson 	virtqueue_notify(vq);
2146c3169a3SBruce Richardson 
2156c3169a3SBruce Richardson 	rte_rmb();
216b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq) == 0) {
2176c3169a3SBruce Richardson 		rte_rmb();
2186c3169a3SBruce Richardson 		usleep(100);
2196c3169a3SBruce Richardson 	}
2206c3169a3SBruce Richardson 
221b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq)) {
2226c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
2236c3169a3SBruce Richardson 		struct vring_used_elem *uep;
2246c3169a3SBruce Richardson 
2256c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
2266c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
2276c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
2286c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
2296c3169a3SBruce Richardson 		desc_idx = idx;
2306c3169a3SBruce Richardson 
2316c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
2326c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
2336c3169a3SBruce Richardson 			vq->vq_free_cnt++;
2346c3169a3SBruce Richardson 		}
2356c3169a3SBruce Richardson 
2366c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2376c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2386c3169a3SBruce Richardson 
2396c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2406c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2416c3169a3SBruce Richardson 	}
2426c3169a3SBruce Richardson 
2436c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2446c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2456c3169a3SBruce Richardson 
246da4f2e4bSZhiyong Yang 	result = cvq->virtio_net_hdr_mz->addr;
2476c3169a3SBruce Richardson 
248a2ffb87bSXiao Wang 	rte_spinlock_unlock(&cvq->lock);
249da4f2e4bSZhiyong Yang 	return result->status;
2506c3169a3SBruce Richardson }
2516c3169a3SBruce Richardson 
2526c3169a3SBruce Richardson static int
2536c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2546c3169a3SBruce Richardson {
2556c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2566c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2576c3169a3SBruce Richardson 	int dlen[1];
2586c3169a3SBruce Richardson 	int ret;
2596c3169a3SBruce Richardson 
2606c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2616c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2626c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2636c3169a3SBruce Richardson 
2646c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2656c3169a3SBruce Richardson 
2666c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2676c3169a3SBruce Richardson 	if (ret) {
2686c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2696c3169a3SBruce Richardson 			  "failed, this is too late now...");
2706c3169a3SBruce Richardson 		return -EINVAL;
2716c3169a3SBruce Richardson 	}
2726c3169a3SBruce Richardson 
2736c3169a3SBruce Richardson 	return 0;
2746c3169a3SBruce Richardson }
2756c3169a3SBruce Richardson 
27669c80d4eSYuanhan Liu static void
27769c80d4eSYuanhan Liu virtio_dev_queue_release(void *queue __rte_unused)
2782963d99aSJianfeng Tan {
27969c80d4eSYuanhan Liu 	/* do nothing */
2805382b188SBernard Iremonger }
2815382b188SBernard Iremonger 
28269c80d4eSYuanhan Liu static uint16_t
28369c80d4eSYuanhan Liu virtio_get_nr_vq(struct virtio_hw *hw)
28469c80d4eSYuanhan Liu {
28569c80d4eSYuanhan Liu 	uint16_t nr_vq = hw->max_queue_pairs * 2;
28669c80d4eSYuanhan Liu 
28769c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
28869c80d4eSYuanhan Liu 		nr_vq += 1;
28969c80d4eSYuanhan Liu 
29069c80d4eSYuanhan Liu 	return nr_vq;
29169c80d4eSYuanhan Liu }
29269c80d4eSYuanhan Liu 
293f4d1ad15SYuanhan Liu static void
294f4d1ad15SYuanhan Liu virtio_init_vring(struct virtqueue *vq)
295f4d1ad15SYuanhan Liu {
296f4d1ad15SYuanhan Liu 	int size = vq->vq_nentries;
297f4d1ad15SYuanhan Liu 	struct vring *vr = &vq->vq_ring;
298f4d1ad15SYuanhan Liu 	uint8_t *ring_mem = vq->vq_ring_virt_mem;
299f4d1ad15SYuanhan Liu 
300f4d1ad15SYuanhan Liu 	PMD_INIT_FUNC_TRACE();
301f4d1ad15SYuanhan Liu 
302f4d1ad15SYuanhan Liu 	/*
303f4d1ad15SYuanhan Liu 	 * Reinitialise since virtio port might have been stopped and restarted
304f4d1ad15SYuanhan Liu 	 */
305f4d1ad15SYuanhan Liu 	memset(ring_mem, 0, vq->vq_ring_size);
306f4d1ad15SYuanhan Liu 	vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
307f4d1ad15SYuanhan Liu 	vq->vq_used_cons_idx = 0;
308f4d1ad15SYuanhan Liu 	vq->vq_desc_head_idx = 0;
309f4d1ad15SYuanhan Liu 	vq->vq_avail_idx = 0;
310f4d1ad15SYuanhan Liu 	vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
311f4d1ad15SYuanhan Liu 	vq->vq_free_cnt = vq->vq_nentries;
312f4d1ad15SYuanhan Liu 	memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
313f4d1ad15SYuanhan Liu 
314f4d1ad15SYuanhan Liu 	vring_desc_init(vr->desc, size);
315f4d1ad15SYuanhan Liu 
316f4d1ad15SYuanhan Liu 	/*
317f4d1ad15SYuanhan Liu 	 * Disable device(host) interrupting guest
318f4d1ad15SYuanhan Liu 	 */
319f4d1ad15SYuanhan Liu 	virtqueue_disable_intr(vq);
320f4d1ad15SYuanhan Liu }
321f4d1ad15SYuanhan Liu 
32269c80d4eSYuanhan Liu static int
32369c80d4eSYuanhan Liu virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
3246c3169a3SBruce Richardson {
3256c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
32601ad44fdSHuawei Xie 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
32701ad44fdSHuawei Xie 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
3281e7bd238SStephen Hemminger 	unsigned int vq_size, size;
3296c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
330f24f8f9fSJianfeng Tan 	struct virtnet_rx *rxvq = NULL;
331f24f8f9fSJianfeng Tan 	struct virtnet_tx *txvq = NULL;
332f24f8f9fSJianfeng Tan 	struct virtnet_ctl *cvq = NULL;
33301ad44fdSHuawei Xie 	struct virtqueue *vq;
334905a2469SYuanhan Liu 	size_t sz_hdr_mz = 0;
33501ad44fdSHuawei Xie 	void *sw_ring = NULL;
33669c80d4eSYuanhan Liu 	int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
33701ad44fdSHuawei Xie 	int ret;
3386c3169a3SBruce Richardson 
339d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
3406c3169a3SBruce Richardson 
3416c3169a3SBruce Richardson 	/*
3426c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
3436c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
3446c3169a3SBruce Richardson 	 */
345553f4593SYuanhan Liu 	vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
34669c80d4eSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
3476c3169a3SBruce Richardson 	if (vq_size == 0) {
3480bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
3496c3169a3SBruce Richardson 		return -EINVAL;
350d78deadaSStephen Hemminger 	}
351d78deadaSStephen Hemminger 
352d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3530bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
3546c3169a3SBruce Richardson 		return -EINVAL;
355d78deadaSStephen Hemminger 	}
356d78deadaSStephen Hemminger 
35773d017ddSYuanhan Liu 	snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
35873d017ddSYuanhan Liu 		 dev->data->port_id, vtpci_queue_idx);
35901ad44fdSHuawei Xie 
360905a2469SYuanhan Liu 	size = RTE_ALIGN_CEIL(sizeof(*vq) +
3616c3169a3SBruce Richardson 				vq_size * sizeof(struct vq_desc_extra),
3626c3169a3SBruce Richardson 				RTE_CACHE_LINE_SIZE);
363905a2469SYuanhan Liu 	if (queue_type == VTNET_TQ) {
36401ad44fdSHuawei Xie 		/*
36501ad44fdSHuawei Xie 		 * For each xmit packet, allocate a virtio_net_hdr
36601ad44fdSHuawei Xie 		 * and indirect ring elements
36701ad44fdSHuawei Xie 		 */
36801ad44fdSHuawei Xie 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
36901ad44fdSHuawei Xie 	} else if (queue_type == VTNET_CQ) {
37001ad44fdSHuawei Xie 		/* Allocate a page for control vq command, data and status */
37101ad44fdSHuawei Xie 		sz_hdr_mz = PAGE_SIZE;
37201ad44fdSHuawei Xie 	}
3734166bbf6SJianfeng Tan 
37469c80d4eSYuanhan Liu 	vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
37569c80d4eSYuanhan Liu 				SOCKET_ID_ANY);
37601ad44fdSHuawei Xie 	if (vq == NULL) {
37701ad44fdSHuawei Xie 		PMD_INIT_LOG(ERR, "can not allocate vq");
378cab04612SHuawei Xie 		return -ENOMEM;
379cab04612SHuawei Xie 	}
38069c80d4eSYuanhan Liu 	hw->vqs[vtpci_queue_idx] = vq;
38169c80d4eSYuanhan Liu 
3826c3169a3SBruce Richardson 	vq->hw = hw;
3836c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
3846c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
3856c3169a3SBruce Richardson 
3866c3169a3SBruce Richardson 	/*
3876c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
3886c3169a3SBruce Richardson 	 */
3896c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
3906c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
39101ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
39201ad44fdSHuawei Xie 		     size, vq->vq_ring_size);
3936c3169a3SBruce Richardson 
39469c80d4eSYuanhan Liu 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
39569c80d4eSYuanhan Liu 					 SOCKET_ID_ANY,
39601ad44fdSHuawei Xie 					 0, VIRTIO_PCI_VRING_ALIGN);
3976c3169a3SBruce Richardson 	if (mz == NULL) {
398abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
399abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
400abf4c84bSBernard Iremonger 		if (mz == NULL) {
40101ad44fdSHuawei Xie 			ret = -ENOMEM;
40201ad44fdSHuawei Xie 			goto fail_q_alloc;
4036c3169a3SBruce Richardson 		}
404abf4c84bSBernard Iremonger 	}
4056c3169a3SBruce Richardson 
4065bdd24e4STiwei Bie 	memset(mz->addr, 0, mz->len);
40701ad44fdSHuawei Xie 
408f17ca787SThomas Monjalon 	vq->vq_ring_mem = mz->iova;
4096c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
41001ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
411f17ca787SThomas Monjalon 		     (uint64_t)mz->iova);
41201ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
41301ad44fdSHuawei Xie 		     (uint64_t)(uintptr_t)mz->addr);
4146c3169a3SBruce Richardson 
415f4d1ad15SYuanhan Liu 	virtio_init_vring(vq);
416f4d1ad15SYuanhan Liu 
41701ad44fdSHuawei Xie 	if (sz_hdr_mz) {
41873d017ddSYuanhan Liu 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
41973d017ddSYuanhan Liu 			 dev->data->port_id, vtpci_queue_idx);
42001ad44fdSHuawei Xie 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
42169c80d4eSYuanhan Liu 						     SOCKET_ID_ANY, 0,
4226dc5de3aSStephen Hemminger 						     RTE_CACHE_LINE_SIZE);
4236dc5de3aSStephen Hemminger 		if (hdr_mz == NULL) {
424abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
42501ad44fdSHuawei Xie 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
4266dc5de3aSStephen Hemminger 			if (hdr_mz == NULL) {
42701ad44fdSHuawei Xie 				ret = -ENOMEM;
42801ad44fdSHuawei Xie 				goto fail_q_alloc;
4296c3169a3SBruce Richardson 			}
430abf4c84bSBernard Iremonger 		}
43101ad44fdSHuawei Xie 	}
43201ad44fdSHuawei Xie 
43301ad44fdSHuawei Xie 	if (queue_type == VTNET_RQ) {
43401ad44fdSHuawei Xie 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
43501ad44fdSHuawei Xie 			       sizeof(vq->sw_ring[0]);
43601ad44fdSHuawei Xie 
43701ad44fdSHuawei Xie 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
43869c80d4eSYuanhan Liu 				RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
43901ad44fdSHuawei Xie 		if (!sw_ring) {
44001ad44fdSHuawei Xie 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
44101ad44fdSHuawei Xie 			ret = -ENOMEM;
44201ad44fdSHuawei Xie 			goto fail_q_alloc;
44301ad44fdSHuawei Xie 		}
44401ad44fdSHuawei Xie 
44501ad44fdSHuawei Xie 		vq->sw_ring = sw_ring;
446905a2469SYuanhan Liu 		rxvq = &vq->rxq;
44701ad44fdSHuawei Xie 		rxvq->vq = vq;
44801ad44fdSHuawei Xie 		rxvq->port_id = dev->data->port_id;
44901ad44fdSHuawei Xie 		rxvq->mz = mz;
45001ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
451905a2469SYuanhan Liu 		txvq = &vq->txq;
45201ad44fdSHuawei Xie 		txvq->vq = vq;
45301ad44fdSHuawei Xie 		txvq->port_id = dev->data->port_id;
45401ad44fdSHuawei Xie 		txvq->mz = mz;
45501ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mz = hdr_mz;
456f17ca787SThomas Monjalon 		txvq->virtio_net_hdr_mem = hdr_mz->iova;
457f24f8f9fSJianfeng Tan 	} else if (queue_type == VTNET_CQ) {
458905a2469SYuanhan Liu 		cvq = &vq->cq;
459f24f8f9fSJianfeng Tan 		cvq->vq = vq;
460f24f8f9fSJianfeng Tan 		cvq->mz = mz;
461f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mz = hdr_mz;
462f17ca787SThomas Monjalon 		cvq->virtio_net_hdr_mem = hdr_mz->iova;
463f24f8f9fSJianfeng Tan 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
46469c80d4eSYuanhan Liu 
46569c80d4eSYuanhan Liu 		hw->cvq = cvq;
466f24f8f9fSJianfeng Tan 	}
467f24f8f9fSJianfeng Tan 
4686a7c0dfcSJan Blunck 	/* For virtio_user case (that is when hw->dev is NULL), we use
469f24f8f9fSJianfeng Tan 	 * virtual address. And we need properly set _offset_, please see
47025f80d10SOlivier Matz 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
471f24f8f9fSJianfeng Tan 	 */
4729470427cSYuanhan Liu 	if (!hw->virtio_user_dev)
473455da545SSantosh Shukla 		vq->offset = offsetof(struct rte_mbuf, buf_iova);
474f24f8f9fSJianfeng Tan 	else {
475f24f8f9fSJianfeng Tan 		vq->vq_ring_mem = (uintptr_t)mz->addr;
476f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
477f24f8f9fSJianfeng Tan 		if (queue_type == VTNET_TQ)
478f24f8f9fSJianfeng Tan 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
479f24f8f9fSJianfeng Tan 		else if (queue_type == VTNET_CQ)
480f24f8f9fSJianfeng Tan 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
481f24f8f9fSJianfeng Tan 	}
482f24f8f9fSJianfeng Tan 
483f24f8f9fSJianfeng Tan 	if (queue_type == VTNET_TQ) {
484f24f8f9fSJianfeng Tan 		struct virtio_tx_region *txr;
485f24f8f9fSJianfeng Tan 		unsigned int i;
486f24f8f9fSJianfeng Tan 
4876dc5de3aSStephen Hemminger 		txr = hdr_mz->addr;
4886dc5de3aSStephen Hemminger 		memset(txr, 0, vq_size * sizeof(*txr));
4896dc5de3aSStephen Hemminger 		for (i = 0; i < vq_size; i++) {
4906dc5de3aSStephen Hemminger 			struct vring_desc *start_dp = txr[i].tx_indir;
4916dc5de3aSStephen Hemminger 
4926dc5de3aSStephen Hemminger 			vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
4936dc5de3aSStephen Hemminger 
4946dc5de3aSStephen Hemminger 			/* first indirect descriptor is always the tx header */
49501ad44fdSHuawei Xie 			start_dp->addr = txvq->virtio_net_hdr_mem
4966dc5de3aSStephen Hemminger 				+ i * sizeof(*txr)
4976dc5de3aSStephen Hemminger 				+ offsetof(struct virtio_tx_region, tx_hdr);
4986dc5de3aSStephen Hemminger 
49901ad44fdSHuawei Xie 			start_dp->len = hw->vtnet_hdr_size;
5006dc5de3aSStephen Hemminger 			start_dp->flags = VRING_DESC_F_NEXT;
5016dc5de3aSStephen Hemminger 		}
5026c3169a3SBruce Richardson 	}
5036c3169a3SBruce Richardson 
504553f4593SYuanhan Liu 	if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
505595454c5SJianfeng Tan 		PMD_INIT_LOG(ERR, "setup_queue failed");
506595454c5SJianfeng Tan 		return -EINVAL;
507595454c5SJianfeng Tan 	}
508595454c5SJianfeng Tan 
5096c3169a3SBruce Richardson 	return 0;
51001ad44fdSHuawei Xie 
51101ad44fdSHuawei Xie fail_q_alloc:
51201ad44fdSHuawei Xie 	rte_free(sw_ring);
51301ad44fdSHuawei Xie 	rte_memzone_free(hdr_mz);
51401ad44fdSHuawei Xie 	rte_memzone_free(mz);
51501ad44fdSHuawei Xie 	rte_free(vq);
51601ad44fdSHuawei Xie 
51701ad44fdSHuawei Xie 	return ret;
5186c3169a3SBruce Richardson }
5196c3169a3SBruce Richardson 
52069c80d4eSYuanhan Liu static void
52169c80d4eSYuanhan Liu virtio_free_queues(struct virtio_hw *hw)
5226c3169a3SBruce Richardson {
52369c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
52469c80d4eSYuanhan Liu 	struct virtqueue *vq;
52569c80d4eSYuanhan Liu 	int queue_type;
52669c80d4eSYuanhan Liu 	uint16_t i;
5276c3169a3SBruce Richardson 
5280e78cfddSHuanle Han 	if (hw->vqs == NULL)
5290e78cfddSHuanle Han 		return;
5300e78cfddSHuanle Han 
53169c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
53269c80d4eSYuanhan Liu 		vq = hw->vqs[i];
53369c80d4eSYuanhan Liu 		if (!vq)
53469c80d4eSYuanhan Liu 			continue;
53569c80d4eSYuanhan Liu 
53669c80d4eSYuanhan Liu 		queue_type = virtio_get_queue_type(hw, i);
53769c80d4eSYuanhan Liu 		if (queue_type == VTNET_RQ) {
53869c80d4eSYuanhan Liu 			rte_free(vq->sw_ring);
53969c80d4eSYuanhan Liu 			rte_memzone_free(vq->rxq.mz);
54069c80d4eSYuanhan Liu 		} else if (queue_type == VTNET_TQ) {
54169c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.mz);
54269c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.virtio_net_hdr_mz);
54369c80d4eSYuanhan Liu 		} else {
54469c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.mz);
54569c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.virtio_net_hdr_mz);
54669c80d4eSYuanhan Liu 		}
54769c80d4eSYuanhan Liu 
54869c80d4eSYuanhan Liu 		rte_free(vq);
5490e78cfddSHuanle Han 		hw->vqs[i] = NULL;
55069c80d4eSYuanhan Liu 	}
55169c80d4eSYuanhan Liu 
55269c80d4eSYuanhan Liu 	rte_free(hw->vqs);
5530e78cfddSHuanle Han 	hw->vqs = NULL;
55469c80d4eSYuanhan Liu }
55569c80d4eSYuanhan Liu 
55669c80d4eSYuanhan Liu static int
55769c80d4eSYuanhan Liu virtio_alloc_queues(struct rte_eth_dev *dev)
55869c80d4eSYuanhan Liu {
55969c80d4eSYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
56069c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
56169c80d4eSYuanhan Liu 	uint16_t i;
56269c80d4eSYuanhan Liu 	int ret;
56369c80d4eSYuanhan Liu 
56469c80d4eSYuanhan Liu 	hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
56569c80d4eSYuanhan Liu 	if (!hw->vqs) {
56669c80d4eSYuanhan Liu 		PMD_INIT_LOG(ERR, "failed to allocate vqs");
56769c80d4eSYuanhan Liu 		return -ENOMEM;
56869c80d4eSYuanhan Liu 	}
56969c80d4eSYuanhan Liu 
57069c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
57169c80d4eSYuanhan Liu 		ret = virtio_init_queue(dev, i);
5726c3169a3SBruce Richardson 		if (ret < 0) {
57369c80d4eSYuanhan Liu 			virtio_free_queues(hw);
5746c3169a3SBruce Richardson 			return ret;
5756c3169a3SBruce Richardson 		}
5766c3169a3SBruce Richardson 	}
5776c3169a3SBruce Richardson 
57869c80d4eSYuanhan Liu 	return 0;
579941d64b5SBernard Iremonger }
580941d64b5SBernard Iremonger 
5819ebdeefeSJianfeng Tan static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
5829ebdeefeSJianfeng Tan 
583941d64b5SBernard Iremonger static void
5846c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
5856c3169a3SBruce Richardson {
5866c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5879ebdeefeSJianfeng Tan 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
5886c3169a3SBruce Richardson 
5896c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
5906c3169a3SBruce Richardson 
5916c3169a3SBruce Richardson 	/* reset the NIC */
59262a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
593981e61f5SJianfeng Tan 		VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
5949ebdeefeSJianfeng Tan 	if (intr_conf->rxq)
5959ebdeefeSJianfeng Tan 		virtio_queues_unbind_intr(dev);
5969ebdeefeSJianfeng Tan 
5979ebdeefeSJianfeng Tan 	if (intr_conf->lsc || intr_conf->rxq) {
598fe19d49cSZhiyong Yang 		virtio_intr_disable(dev);
5999ebdeefeSJianfeng Tan 		rte_intr_efd_disable(dev->intr_handle);
6009ebdeefeSJianfeng Tan 		rte_free(dev->intr_handle->intr_vec);
6019ebdeefeSJianfeng Tan 		dev->intr_handle->intr_vec = NULL;
6029ebdeefeSJianfeng Tan 	}
6039ebdeefeSJianfeng Tan 
6046c3169a3SBruce Richardson 	vtpci_reset(hw);
6056c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
60669c80d4eSYuanhan Liu 	virtio_free_queues(hw);
6076c3169a3SBruce Richardson }
6086c3169a3SBruce Richardson 
6096c3169a3SBruce Richardson static void
6106c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
6116c3169a3SBruce Richardson {
6126c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6136c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6146c3169a3SBruce Richardson 	int dlen[1];
6156c3169a3SBruce Richardson 	int ret;
6166c3169a3SBruce Richardson 
617e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
618f2462150SFerruh Yigit 		PMD_INIT_LOG(INFO, "host does not support rx control");
619e9e414a4SStephen Hemminger 		return;
620e9e414a4SStephen Hemminger 	}
621e9e414a4SStephen Hemminger 
6226c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6236c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6246c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6256c3169a3SBruce Richardson 	dlen[0] = 1;
6266c3169a3SBruce Richardson 
6276c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6286c3169a3SBruce Richardson 	if (ret)
6296c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
6306c3169a3SBruce Richardson }
6316c3169a3SBruce Richardson 
6326c3169a3SBruce Richardson static void
6336c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
6346c3169a3SBruce Richardson {
6356c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6366c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6376c3169a3SBruce Richardson 	int dlen[1];
6386c3169a3SBruce Richardson 	int ret;
6396c3169a3SBruce Richardson 
640e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
641f2462150SFerruh Yigit 		PMD_INIT_LOG(INFO, "host does not support rx control");
642e9e414a4SStephen Hemminger 		return;
643e9e414a4SStephen Hemminger 	}
644e9e414a4SStephen Hemminger 
6456c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6466c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6476c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6486c3169a3SBruce Richardson 	dlen[0] = 1;
6496c3169a3SBruce Richardson 
6506c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6516c3169a3SBruce Richardson 	if (ret)
6526c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
6536c3169a3SBruce Richardson }
6546c3169a3SBruce Richardson 
6556c3169a3SBruce Richardson static void
6566c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
6576c3169a3SBruce Richardson {
6586c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6596c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6606c3169a3SBruce Richardson 	int dlen[1];
6616c3169a3SBruce Richardson 	int ret;
6626c3169a3SBruce Richardson 
663e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
664f2462150SFerruh Yigit 		PMD_INIT_LOG(INFO, "host does not support rx control");
665e9e414a4SStephen Hemminger 		return;
666e9e414a4SStephen Hemminger 	}
667e9e414a4SStephen Hemminger 
6686c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6696c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6706c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6716c3169a3SBruce Richardson 	dlen[0] = 1;
6726c3169a3SBruce Richardson 
6736c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6746c3169a3SBruce Richardson 	if (ret)
6756c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
6766c3169a3SBruce Richardson }
6776c3169a3SBruce Richardson 
6786c3169a3SBruce Richardson static void
6796c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
6806c3169a3SBruce Richardson {
6816c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6826c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6836c3169a3SBruce Richardson 	int dlen[1];
6846c3169a3SBruce Richardson 	int ret;
6856c3169a3SBruce Richardson 
686e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
687f2462150SFerruh Yigit 		PMD_INIT_LOG(INFO, "host does not support rx control");
688e9e414a4SStephen Hemminger 		return;
689e9e414a4SStephen Hemminger 	}
690e9e414a4SStephen Hemminger 
6916c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6926c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6936c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6946c3169a3SBruce Richardson 	dlen[0] = 1;
6956c3169a3SBruce Richardson 
6966c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6976c3169a3SBruce Richardson 	if (ret)
6986c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
6996c3169a3SBruce Richardson }
7006c3169a3SBruce Richardson 
7014ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
7024ec2424aSSouvik Dey static int
7034ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
7044ec2424aSSouvik Dey {
7054ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
7064ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
7074ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
7084ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
70949d26d9eSMaxime Coquelin 	uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
7104ec2424aSSouvik Dey 
71149d26d9eSMaxime Coquelin 	max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
71249d26d9eSMaxime Coquelin 
71349d26d9eSMaxime Coquelin 	if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
714f2462150SFerruh Yigit 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
71549d26d9eSMaxime Coquelin 			ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
7164ec2424aSSouvik Dey 		return -EINVAL;
7174ec2424aSSouvik Dey 	}
7184ec2424aSSouvik Dey 	return 0;
7194ec2424aSSouvik Dey }
7204ec2424aSSouvik Dey 
721c056be23SJianfeng Tan static int
722c056be23SJianfeng Tan virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
723c056be23SJianfeng Tan {
724c056be23SJianfeng Tan 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
725c056be23SJianfeng Tan 	struct virtqueue *vq = rxvq->vq;
726c056be23SJianfeng Tan 
727c056be23SJianfeng Tan 	virtqueue_enable_intr(vq);
728c056be23SJianfeng Tan 	return 0;
729c056be23SJianfeng Tan }
730c056be23SJianfeng Tan 
731c056be23SJianfeng Tan static int
732c056be23SJianfeng Tan virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
733c056be23SJianfeng Tan {
734c056be23SJianfeng Tan 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
735c056be23SJianfeng Tan 	struct virtqueue *vq = rxvq->vq;
736c056be23SJianfeng Tan 
737c056be23SJianfeng Tan 	virtqueue_disable_intr(vq);
738c056be23SJianfeng Tan 	return 0;
739c056be23SJianfeng Tan }
740c056be23SJianfeng Tan 
7416c3169a3SBruce Richardson /*
7426c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
7436c3169a3SBruce Richardson  */
7446c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
7456c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
7466c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
7476c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
7486c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
7496c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
7506c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
7516c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
7526c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
7534ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
7546c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
7556c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
75676d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
757baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
7586c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
75976d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
7606c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
761289ba0c0SDavid Harton 	.vlan_offload_set        = virtio_dev_vlan_offload_set,
7626c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
763c056be23SJianfeng Tan 	.rx_queue_intr_enable    = virtio_dev_rx_queue_intr_enable,
764c056be23SJianfeng Tan 	.rx_queue_intr_disable   = virtio_dev_rx_queue_intr_disable,
76569c80d4eSYuanhan Liu 	.rx_queue_release        = virtio_dev_queue_release,
766b0caba1aSJianfeng Tan 	.rx_descriptor_done      = virtio_dev_rx_queue_done,
7676c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
76869c80d4eSYuanhan Liu 	.tx_queue_release        = virtio_dev_queue_release,
7696c3169a3SBruce Richardson 	/* collect stats per queue */
7706c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7716c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7726c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7736c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7746c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7756c3169a3SBruce Richardson };
7766c3169a3SBruce Richardson 
7776c3169a3SBruce Richardson static inline int
7786c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
7796c3169a3SBruce Richardson 				struct rte_eth_link *link)
7806c3169a3SBruce Richardson {
7816c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
7826c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
7836c3169a3SBruce Richardson 
7846c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7856c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
7866c3169a3SBruce Richardson 		return -1;
7876c3169a3SBruce Richardson 
7886c3169a3SBruce Richardson 	return 0;
7896c3169a3SBruce Richardson }
7906c3169a3SBruce Richardson 
7916c3169a3SBruce Richardson /**
7926c3169a3SBruce Richardson  * Atomically writes the link status information into global
7936c3169a3SBruce Richardson  * structure rte_eth_dev.
7946c3169a3SBruce Richardson  *
7956c3169a3SBruce Richardson  * @param dev
7966c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
7976c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
7986c3169a3SBruce Richardson  *
7996c3169a3SBruce Richardson  * @return
8006c3169a3SBruce Richardson  *   - On success, zero.
8016c3169a3SBruce Richardson  *   - On failure, negative value.
8026c3169a3SBruce Richardson  */
8036c3169a3SBruce Richardson static inline int
8046c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
8056c3169a3SBruce Richardson 		struct rte_eth_link *link)
8066c3169a3SBruce Richardson {
8076c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
8086c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
8096c3169a3SBruce Richardson 
8106c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
8116c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
8126c3169a3SBruce Richardson 		return -1;
8136c3169a3SBruce Richardson 
8146c3169a3SBruce Richardson 	return 0;
8156c3169a3SBruce Richardson }
8166c3169a3SBruce Richardson 
8176c3169a3SBruce Richardson static void
81876d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
8196c3169a3SBruce Richardson {
8206c3169a3SBruce Richardson 	unsigned i;
8216c3169a3SBruce Richardson 
8226c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
82301ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
8246c3169a3SBruce Richardson 		if (txvq == NULL)
8256c3169a3SBruce Richardson 			continue;
8266c3169a3SBruce Richardson 
82701ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
82801ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
82901ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
8306c3169a3SBruce Richardson 
8316c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
83201ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
83301ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
8346c3169a3SBruce Richardson 		}
8356c3169a3SBruce Richardson 	}
8366c3169a3SBruce Richardson 
8376c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
83801ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
8396c3169a3SBruce Richardson 		if (rxvq == NULL)
8406c3169a3SBruce Richardson 			continue;
8416c3169a3SBruce Richardson 
84201ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
84301ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
84401ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
8456c3169a3SBruce Richardson 
8466c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
84701ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
84801ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
8496c3169a3SBruce Richardson 		}
8506c3169a3SBruce Richardson 	}
8516c3169a3SBruce Richardson 
8526c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
8536c3169a3SBruce Richardson }
8546c3169a3SBruce Richardson 
855baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
856baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
857baf91c39SRemy Horton 				       __rte_unused unsigned limit)
858baf91c39SRemy Horton {
859baf91c39SRemy Horton 	unsigned i;
860baf91c39SRemy Horton 	unsigned count = 0;
861baf91c39SRemy Horton 	unsigned t;
862baf91c39SRemy Horton 
86301ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
86401ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
865baf91c39SRemy Horton 
8667e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
867baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
868baf91c39SRemy Horton 
869baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
87043ec842cSDidier Pallard 			struct virtnet_rx *rxvq = dev->data->rx_queues[i];
871baf91c39SRemy Horton 			if (rxvq == NULL)
872baf91c39SRemy Horton 				continue;
87301ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
874baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
875baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
876baf91c39SRemy Horton 					"rx_q%u_%s", i,
87701ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
878baf91c39SRemy Horton 				count++;
879baf91c39SRemy Horton 			}
880baf91c39SRemy Horton 		}
881baf91c39SRemy Horton 
882baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
88343ec842cSDidier Pallard 			struct virtnet_tx *txvq = dev->data->tx_queues[i];
884baf91c39SRemy Horton 			if (txvq == NULL)
885baf91c39SRemy Horton 				continue;
88601ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
887baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
888baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
889baf91c39SRemy Horton 					"tx_q%u_%s", i,
89001ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
891baf91c39SRemy Horton 				count++;
892baf91c39SRemy Horton 			}
893baf91c39SRemy Horton 		}
894baf91c39SRemy Horton 		return count;
895baf91c39SRemy Horton 	}
896baf91c39SRemy Horton 	return nstats;
897baf91c39SRemy Horton }
898baf91c39SRemy Horton 
89976d4c652SHarry van Haaren static int
900e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
90176d4c652SHarry van Haaren 		      unsigned n)
90276d4c652SHarry van Haaren {
90376d4c652SHarry van Haaren 	unsigned i;
90476d4c652SHarry van Haaren 	unsigned count = 0;
90576d4c652SHarry van Haaren 
90601ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
90701ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
90876d4c652SHarry van Haaren 
90976d4c652SHarry van Haaren 	if (n < nstats)
91076d4c652SHarry van Haaren 		return nstats;
91176d4c652SHarry van Haaren 
91276d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
91301ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
91476d4c652SHarry van Haaren 
91576d4c652SHarry van Haaren 		if (rxvq == NULL)
91676d4c652SHarry van Haaren 			continue;
91776d4c652SHarry van Haaren 
91876d4c652SHarry van Haaren 		unsigned t;
91976d4c652SHarry van Haaren 
92001ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
92176d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
92201ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
923513c78aeSOlivier Matz 			xstats[count].id = count;
92476d4c652SHarry van Haaren 			count++;
92576d4c652SHarry van Haaren 		}
92676d4c652SHarry van Haaren 	}
92776d4c652SHarry van Haaren 
92876d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
92901ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
93076d4c652SHarry van Haaren 
93176d4c652SHarry van Haaren 		if (txvq == NULL)
93276d4c652SHarry van Haaren 			continue;
93376d4c652SHarry van Haaren 
93476d4c652SHarry van Haaren 		unsigned t;
93576d4c652SHarry van Haaren 
93601ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
93776d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
93801ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
939513c78aeSOlivier Matz 			xstats[count].id = count;
94076d4c652SHarry van Haaren 			count++;
94176d4c652SHarry van Haaren 		}
94276d4c652SHarry van Haaren 	}
94376d4c652SHarry van Haaren 
94476d4c652SHarry van Haaren 	return count;
94576d4c652SHarry van Haaren }
94676d4c652SHarry van Haaren 
947d5b0924bSMatan Azrad static int
94876d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
94976d4c652SHarry van Haaren {
95076d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
951d5b0924bSMatan Azrad 
952d5b0924bSMatan Azrad 	return 0;
95376d4c652SHarry van Haaren }
95476d4c652SHarry van Haaren 
9556c3169a3SBruce Richardson static void
9566c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
9576c3169a3SBruce Richardson {
9586c3169a3SBruce Richardson 	unsigned int i;
9596c3169a3SBruce Richardson 
9606c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
96101ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
9626c3169a3SBruce Richardson 		if (txvq == NULL)
9636c3169a3SBruce Richardson 			continue;
9646c3169a3SBruce Richardson 
96501ad44fdSHuawei Xie 		txvq->stats.packets = 0;
96601ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
96701ad44fdSHuawei Xie 		txvq->stats.errors = 0;
96801ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
96901ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
97001ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
97101ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
9726c3169a3SBruce Richardson 	}
9736c3169a3SBruce Richardson 
9746c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
97501ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9766c3169a3SBruce Richardson 		if (rxvq == NULL)
9776c3169a3SBruce Richardson 			continue;
9786c3169a3SBruce Richardson 
97901ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
98001ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
98101ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
98201ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
98301ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
98401ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
98501ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
9866c3169a3SBruce Richardson 	}
9876c3169a3SBruce Richardson }
9886c3169a3SBruce Richardson 
9896c3169a3SBruce Richardson static void
9906c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
9916c3169a3SBruce Richardson {
9926c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
9936c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9946c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9956c3169a3SBruce Richardson }
9966c3169a3SBruce Richardson 
9976c3169a3SBruce Richardson static void
9986c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
9996c3169a3SBruce Richardson {
10006c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
10016c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
10026c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
10036c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
10046c3169a3SBruce Richardson 	} else {
10056c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
10066c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10076c3169a3SBruce Richardson 	}
10086c3169a3SBruce Richardson }
10096c3169a3SBruce Richardson 
10106d01e580SWei Dai static int
10116c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
10126c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
10136c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
10146c3169a3SBruce Richardson {
10156c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10166c3169a3SBruce Richardson 	int err, len[2];
10176c3169a3SBruce Richardson 
101827046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1019e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
10206d01e580SWei Dai 		return -1;
102127046236SStephen Hemminger 	}
102227046236SStephen Hemminger 
10236c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10246c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
10256c3169a3SBruce Richardson 
10266c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
10276c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
10286c3169a3SBruce Richardson 
10296c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
10306c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
10316c3169a3SBruce Richardson 
10326c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
10336c3169a3SBruce Richardson 	if (err != 0)
10346c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
10356d01e580SWei Dai 	return err;
10366c3169a3SBruce Richardson }
10376c3169a3SBruce Richardson 
10386d01e580SWei Dai static int
10396c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
10406c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
10416c3169a3SBruce Richardson {
10426c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10436c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
10446c3169a3SBruce Richardson 	unsigned int i;
10456c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10466c3169a3SBruce Richardson 
10476c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10486c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10496d01e580SWei Dai 		return -EINVAL;
10506c3169a3SBruce Richardson 	}
10516c3169a3SBruce Richardson 
10526c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10536c3169a3SBruce Richardson 	uc->entries = 0;
10546c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10556c3169a3SBruce Richardson 	mc->entries = 0;
10566c3169a3SBruce Richardson 
10576c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10586c3169a3SBruce Richardson 		const struct ether_addr *addr
10596c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
10606c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
10616c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
10626c3169a3SBruce Richardson 
10636c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
10646c3169a3SBruce Richardson 	}
10656c3169a3SBruce Richardson 
10666d01e580SWei Dai 	return virtio_mac_table_set(hw, uc, mc);
10676c3169a3SBruce Richardson }
10686c3169a3SBruce Richardson 
10696c3169a3SBruce Richardson static void
10706c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
10716c3169a3SBruce Richardson {
10726c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10736c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
10746c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10756c3169a3SBruce Richardson 	unsigned int i;
10766c3169a3SBruce Richardson 
10776c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10786c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10796c3169a3SBruce Richardson 		return;
10806c3169a3SBruce Richardson 	}
10816c3169a3SBruce Richardson 
10826c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10836c3169a3SBruce Richardson 	uc->entries = 0;
10846c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10856c3169a3SBruce Richardson 	mc->entries = 0;
10866c3169a3SBruce Richardson 
10876c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10886c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
10896c3169a3SBruce Richardson 
10906c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
10916c3169a3SBruce Richardson 			continue;
10926c3169a3SBruce Richardson 
10936c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
10946c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
10956c3169a3SBruce Richardson 	}
10966c3169a3SBruce Richardson 
10976c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10986c3169a3SBruce Richardson }
10996c3169a3SBruce Richardson 
11006c3169a3SBruce Richardson static void
11016c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
11026c3169a3SBruce Richardson {
11036c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11046c3169a3SBruce Richardson 
11056c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
11066c3169a3SBruce Richardson 
11076c3169a3SBruce Richardson 	/* Use atomic update if available */
11086c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
11096c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
11106c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
11116c3169a3SBruce Richardson 
11126c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
11136c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
11146c3169a3SBruce Richardson 
11156c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
11166c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
11176c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
11186c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
11196c3169a3SBruce Richardson }
11206c3169a3SBruce Richardson 
11216c3169a3SBruce Richardson static int
11226c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
11236c3169a3SBruce Richardson {
11246c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11256c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
11266c3169a3SBruce Richardson 	int len;
11276c3169a3SBruce Richardson 
11286c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
11296c3169a3SBruce Richardson 		return -ENOTSUP;
11306c3169a3SBruce Richardson 
11316c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
11326c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
11336c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
11346c3169a3SBruce Richardson 	len = sizeof(vlan_id);
11356c3169a3SBruce Richardson 
11366c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
11376c3169a3SBruce Richardson }
11386c3169a3SBruce Richardson 
11396ba1f63bSYuanhan Liu static int
1140fe19d49cSZhiyong Yang virtio_intr_enable(struct rte_eth_dev *dev)
1141fe19d49cSZhiyong Yang {
1142fe19d49cSZhiyong Yang 	struct virtio_hw *hw = dev->data->dev_private;
1143fe19d49cSZhiyong Yang 
1144fe19d49cSZhiyong Yang 	if (rte_intr_enable(dev->intr_handle) < 0)
1145fe19d49cSZhiyong Yang 		return -1;
1146fe19d49cSZhiyong Yang 
1147fe19d49cSZhiyong Yang 	if (!hw->virtio_user_dev)
1148fe19d49cSZhiyong Yang 		hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1149fe19d49cSZhiyong Yang 
1150fe19d49cSZhiyong Yang 	return 0;
1151fe19d49cSZhiyong Yang }
1152fe19d49cSZhiyong Yang 
1153fe19d49cSZhiyong Yang static int
1154fe19d49cSZhiyong Yang virtio_intr_disable(struct rte_eth_dev *dev)
1155fe19d49cSZhiyong Yang {
1156fe19d49cSZhiyong Yang 	struct virtio_hw *hw = dev->data->dev_private;
1157fe19d49cSZhiyong Yang 
1158fe19d49cSZhiyong Yang 	if (rte_intr_disable(dev->intr_handle) < 0)
1159fe19d49cSZhiyong Yang 		return -1;
1160fe19d49cSZhiyong Yang 
1161fe19d49cSZhiyong Yang 	if (!hw->virtio_user_dev)
1162fe19d49cSZhiyong Yang 		hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1163fe19d49cSZhiyong Yang 
1164fe19d49cSZhiyong Yang 	return 0;
1165fe19d49cSZhiyong Yang }
1166fe19d49cSZhiyong Yang 
1167fe19d49cSZhiyong Yang static int
116860e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
11696c3169a3SBruce Richardson {
11703891f233SYuanhan Liu 	uint64_t host_features;
11716c3169a3SBruce Richardson 
11726c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
11733891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
117460e6f470SOlivier Matz 		req_features);
11756c3169a3SBruce Richardson 
11766c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1177553f4593SYuanhan Liu 	host_features = VTPCI_OPS(hw)->get_features(hw);
11783891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
11796c3169a3SBruce Richardson 		host_features);
11806c3169a3SBruce Richardson 
118149d26d9eSMaxime Coquelin 	/* If supported, ensure MTU value is valid before acknowledging it. */
118249d26d9eSMaxime Coquelin 	if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
118349d26d9eSMaxime Coquelin 		struct virtio_net_config config;
118449d26d9eSMaxime Coquelin 
118549d26d9eSMaxime Coquelin 		vtpci_read_dev_config(hw,
118649d26d9eSMaxime Coquelin 			offsetof(struct virtio_net_config, mtu),
118749d26d9eSMaxime Coquelin 			&config.mtu, sizeof(config.mtu));
118849d26d9eSMaxime Coquelin 
118949d26d9eSMaxime Coquelin 		if (config.mtu < ETHER_MIN_MTU)
119049d26d9eSMaxime Coquelin 			req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
119149d26d9eSMaxime Coquelin 	}
119249d26d9eSMaxime Coquelin 
11936c3169a3SBruce Richardson 	/*
11946c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
11956c3169a3SBruce Richardson 	 * guest feature bits.
11966c3169a3SBruce Richardson 	 */
119760e6f470SOlivier Matz 	hw->guest_features = req_features;
11986c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
11993891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
12006c3169a3SBruce Richardson 		hw->guest_features);
12016ba1f63bSYuanhan Liu 
12026ba1f63bSYuanhan Liu 	if (hw->modern) {
12036ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
12046ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
12056ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
12066ba1f63bSYuanhan Liu 			return -1;
12076ba1f63bSYuanhan Liu 		}
12086ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
12096ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
12106ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
12116ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
12126ba1f63bSYuanhan Liu 			return -1;
12136ba1f63bSYuanhan Liu 		}
12146ba1f63bSYuanhan Liu 	}
12156ba1f63bSYuanhan Liu 
121660e6f470SOlivier Matz 	hw->req_guest_features = req_features;
121760e6f470SOlivier Matz 
12186ba1f63bSYuanhan Liu 	return 0;
12196c3169a3SBruce Richardson }
12206c3169a3SBruce Richardson 
12211978a9dcSXiao Wang int
12221978a9dcSXiao Wang virtio_dev_pause(struct rte_eth_dev *dev)
12231978a9dcSXiao Wang {
12241978a9dcSXiao Wang 	struct virtio_hw *hw = dev->data->dev_private;
12251978a9dcSXiao Wang 
12261978a9dcSXiao Wang 	rte_spinlock_lock(&hw->state_lock);
12271978a9dcSXiao Wang 
12281978a9dcSXiao Wang 	if (hw->started == 0) {
12291978a9dcSXiao Wang 		/* Device is just stopped. */
12301978a9dcSXiao Wang 		rte_spinlock_unlock(&hw->state_lock);
12311978a9dcSXiao Wang 		return -1;
12321978a9dcSXiao Wang 	}
12331978a9dcSXiao Wang 	hw->started = 0;
12341978a9dcSXiao Wang 	/*
12351978a9dcSXiao Wang 	 * Prevent the worker threads from touching queues to avoid contention,
12361978a9dcSXiao Wang 	 * 1 ms should be enough for the ongoing Tx function to finish.
12371978a9dcSXiao Wang 	 */
12381978a9dcSXiao Wang 	rte_delay_ms(1);
12391978a9dcSXiao Wang 	return 0;
12401978a9dcSXiao Wang }
12411978a9dcSXiao Wang 
12421978a9dcSXiao Wang /*
12431978a9dcSXiao Wang  * Recover hw state to let the worker threads continue.
12441978a9dcSXiao Wang  */
12451978a9dcSXiao Wang void
12461978a9dcSXiao Wang virtio_dev_resume(struct rte_eth_dev *dev)
12471978a9dcSXiao Wang {
12481978a9dcSXiao Wang 	struct virtio_hw *hw = dev->data->dev_private;
12491978a9dcSXiao Wang 
12501978a9dcSXiao Wang 	hw->started = 1;
12511978a9dcSXiao Wang 	rte_spinlock_unlock(&hw->state_lock);
12521978a9dcSXiao Wang }
12531978a9dcSXiao Wang 
12541978a9dcSXiao Wang /*
12551978a9dcSXiao Wang  * Should be called only after device is paused.
12561978a9dcSXiao Wang  */
12571978a9dcSXiao Wang int
12581978a9dcSXiao Wang virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
12591978a9dcSXiao Wang 		int nb_pkts)
12601978a9dcSXiao Wang {
12611978a9dcSXiao Wang 	struct virtio_hw *hw = dev->data->dev_private;
12621978a9dcSXiao Wang 	struct virtnet_tx *txvq = dev->data->tx_queues[0];
12631978a9dcSXiao Wang 	int ret;
12641978a9dcSXiao Wang 
12651978a9dcSXiao Wang 	hw->inject_pkts = tx_pkts;
12661978a9dcSXiao Wang 	ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
12671978a9dcSXiao Wang 	hw->inject_pkts = NULL;
12681978a9dcSXiao Wang 
12691978a9dcSXiao Wang 	return ret;
12701978a9dcSXiao Wang }
12711978a9dcSXiao Wang 
12727365504fSXiao Wang static void
12737365504fSXiao Wang virtio_notify_peers(struct rte_eth_dev *dev)
12747365504fSXiao Wang {
12757365504fSXiao Wang 	struct virtio_hw *hw = dev->data->dev_private;
12767365504fSXiao Wang 	struct virtnet_rx *rxvq = dev->data->rx_queues[0];
12777365504fSXiao Wang 	struct rte_mbuf *rarp_mbuf;
12787365504fSXiao Wang 
12797365504fSXiao Wang 	rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
12807365504fSXiao Wang 			(struct ether_addr *)hw->mac_addr);
12817365504fSXiao Wang 	if (rarp_mbuf == NULL) {
12827365504fSXiao Wang 		PMD_DRV_LOG(ERR, "failed to make RARP packet.");
12837365504fSXiao Wang 		return;
12847365504fSXiao Wang 	}
12857365504fSXiao Wang 
12867365504fSXiao Wang 	/* If virtio port just stopped, no need to send RARP */
12877365504fSXiao Wang 	if (virtio_dev_pause(dev) < 0) {
12887365504fSXiao Wang 		rte_pktmbuf_free(rarp_mbuf);
12897365504fSXiao Wang 		return;
12907365504fSXiao Wang 	}
12917365504fSXiao Wang 
12927365504fSXiao Wang 	virtio_inject_pkts(dev, &rarp_mbuf, 1);
12937365504fSXiao Wang 	virtio_dev_resume(dev);
12947365504fSXiao Wang }
12957365504fSXiao Wang 
12967365504fSXiao Wang static void
12977365504fSXiao Wang virtio_ack_link_announce(struct rte_eth_dev *dev)
12987365504fSXiao Wang {
12997365504fSXiao Wang 	struct virtio_hw *hw = dev->data->dev_private;
13007365504fSXiao Wang 	struct virtio_pmd_ctrl ctrl;
13017365504fSXiao Wang 
13027365504fSXiao Wang 	ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
13037365504fSXiao Wang 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
13047365504fSXiao Wang 
13057365504fSXiao Wang 	virtio_send_command(hw->cvq, &ctrl, NULL, 0);
13067365504fSXiao Wang }
13077365504fSXiao Wang 
13086c3169a3SBruce Richardson /*
13097365504fSXiao Wang  * Process virtio config changed interrupt. Call the callback
13107365504fSXiao Wang  * if link state changed, generate gratuitous RARP packet if
13117365504fSXiao Wang  * the status indicates an ANNOUNCE.
13126c3169a3SBruce Richardson  */
1313ef53b603SJianfeng Tan void
1314c23a1a30SQi Zhang virtio_interrupt_handler(void *param)
13156c3169a3SBruce Richardson {
13166c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
13176c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
13186c3169a3SBruce Richardson 	uint8_t isr;
13196c3169a3SBruce Richardson 
13206c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
13216c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
13226c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
13236c3169a3SBruce Richardson 
1324fe19d49cSZhiyong Yang 	if (virtio_intr_enable(dev) < 0)
13256c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
13266c3169a3SBruce Richardson 
13276c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
13286c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
13296c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1330d6af1a13SBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC,
1331cebe3d7bSThomas Monjalon 						      NULL);
13326c3169a3SBruce Richardson 	}
13336c3169a3SBruce Richardson 
13347365504fSXiao Wang 	if (isr & VIRTIO_NET_S_ANNOUNCE) {
13357365504fSXiao Wang 		virtio_notify_peers(dev);
13367365504fSXiao Wang 		virtio_ack_link_announce(dev);
13377365504fSXiao Wang 	}
13386c3169a3SBruce Richardson }
13396c3169a3SBruce Richardson 
13404819eae8SOlivier Matz /* set rx and tx handlers according to what is supported */
13416c3169a3SBruce Richardson static void
13424819eae8SOlivier Matz set_rxtx_funcs(struct rte_eth_dev *eth_dev)
13436c3169a3SBruce Richardson {
13446c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
13454819eae8SOlivier Matz 
134609649363SOlivier Matz 	if (hw->use_simple_rx) {
13474819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
13484819eae8SOlivier Matz 			eth_dev->data->port_id);
13494819eae8SOlivier Matz 		eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
13504819eae8SOlivier Matz 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
13514819eae8SOlivier Matz 		PMD_INIT_LOG(INFO,
13524819eae8SOlivier Matz 			"virtio: using mergeable buffer Rx path on port %u",
13534819eae8SOlivier Matz 			eth_dev->data->port_id);
13546c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
13554819eae8SOlivier Matz 	} else {
13564819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
13574819eae8SOlivier Matz 			eth_dev->data->port_id);
13586c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
13596c3169a3SBruce Richardson 	}
13606c3169a3SBruce Richardson 
136109649363SOlivier Matz 	if (hw->use_simple_tx) {
13624819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
13634819eae8SOlivier Matz 			eth_dev->data->port_id);
13644819eae8SOlivier Matz 		eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
13654819eae8SOlivier Matz 	} else {
13664819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
13674819eae8SOlivier Matz 			eth_dev->data->port_id);
13684819eae8SOlivier Matz 		eth_dev->tx_pkt_burst = virtio_xmit_pkts;
13694819eae8SOlivier Matz 	}
13704819eae8SOlivier Matz }
13714819eae8SOlivier Matz 
137226b683b4SJianfeng Tan /* Only support 1:1 queue/interrupt mapping so far.
137326b683b4SJianfeng Tan  * TODO: support n:1 queue/interrupt mapping when there are limited number of
137426b683b4SJianfeng Tan  * interrupt vectors (<N+1).
137526b683b4SJianfeng Tan  */
137626b683b4SJianfeng Tan static int
137726b683b4SJianfeng Tan virtio_queues_bind_intr(struct rte_eth_dev *dev)
137826b683b4SJianfeng Tan {
137926b683b4SJianfeng Tan 	uint32_t i;
138026b683b4SJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
138126b683b4SJianfeng Tan 
1382f2462150SFerruh Yigit 	PMD_INIT_LOG(INFO, "queue/interrupt binding");
138326b683b4SJianfeng Tan 	for (i = 0; i < dev->data->nb_rx_queues; ++i) {
138426b683b4SJianfeng Tan 		dev->intr_handle->intr_vec[i] = i + 1;
138526b683b4SJianfeng Tan 		if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
138626b683b4SJianfeng Tan 						 VIRTIO_MSI_NO_VECTOR) {
138726b683b4SJianfeng Tan 			PMD_DRV_LOG(ERR, "failed to set queue vector");
138826b683b4SJianfeng Tan 			return -EBUSY;
138926b683b4SJianfeng Tan 		}
139026b683b4SJianfeng Tan 	}
139126b683b4SJianfeng Tan 
139226b683b4SJianfeng Tan 	return 0;
139326b683b4SJianfeng Tan }
139426b683b4SJianfeng Tan 
13959ebdeefeSJianfeng Tan static void
13969ebdeefeSJianfeng Tan virtio_queues_unbind_intr(struct rte_eth_dev *dev)
13979ebdeefeSJianfeng Tan {
13989ebdeefeSJianfeng Tan 	uint32_t i;
13999ebdeefeSJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
14009ebdeefeSJianfeng Tan 
1401f2462150SFerruh Yigit 	PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
14029ebdeefeSJianfeng Tan 	for (i = 0; i < dev->data->nb_rx_queues; ++i)
14039ebdeefeSJianfeng Tan 		VTPCI_OPS(hw)->set_queue_irq(hw,
14049ebdeefeSJianfeng Tan 					     hw->vqs[i * VTNET_CQ],
14059ebdeefeSJianfeng Tan 					     VIRTIO_MSI_NO_VECTOR);
14069ebdeefeSJianfeng Tan }
14079ebdeefeSJianfeng Tan 
140826b683b4SJianfeng Tan static int
140926b683b4SJianfeng Tan virtio_configure_intr(struct rte_eth_dev *dev)
141026b683b4SJianfeng Tan {
141126b683b4SJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
141226b683b4SJianfeng Tan 
141326b683b4SJianfeng Tan 	if (!rte_intr_cap_multiple(dev->intr_handle)) {
141426b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
141526b683b4SJianfeng Tan 		return -ENOTSUP;
141626b683b4SJianfeng Tan 	}
141726b683b4SJianfeng Tan 
141826b683b4SJianfeng Tan 	if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
141926b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Fail to create eventfd");
142026b683b4SJianfeng Tan 		return -1;
142126b683b4SJianfeng Tan 	}
142226b683b4SJianfeng Tan 
142326b683b4SJianfeng Tan 	if (!dev->intr_handle->intr_vec) {
142426b683b4SJianfeng Tan 		dev->intr_handle->intr_vec =
142526b683b4SJianfeng Tan 			rte_zmalloc("intr_vec",
142626b683b4SJianfeng Tan 				    hw->max_queue_pairs * sizeof(int), 0);
142726b683b4SJianfeng Tan 		if (!dev->intr_handle->intr_vec) {
142826b683b4SJianfeng Tan 			PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
142926b683b4SJianfeng Tan 				     hw->max_queue_pairs);
143026b683b4SJianfeng Tan 			return -ENOMEM;
143126b683b4SJianfeng Tan 		}
143226b683b4SJianfeng Tan 	}
143326b683b4SJianfeng Tan 
143426b683b4SJianfeng Tan 	/* Re-register callback to update max_intr */
143526b683b4SJianfeng Tan 	rte_intr_callback_unregister(dev->intr_handle,
143626b683b4SJianfeng Tan 				     virtio_interrupt_handler,
143726b683b4SJianfeng Tan 				     dev);
143826b683b4SJianfeng Tan 	rte_intr_callback_register(dev->intr_handle,
143926b683b4SJianfeng Tan 				   virtio_interrupt_handler,
144026b683b4SJianfeng Tan 				   dev);
144126b683b4SJianfeng Tan 
144226b683b4SJianfeng Tan 	/* DO NOT try to remove this! This function will enable msix, or QEMU
144326b683b4SJianfeng Tan 	 * will encounter SIGSEGV when DRIVER_OK is sent.
144426b683b4SJianfeng Tan 	 * And for legacy devices, this should be done before queue/vec binding
144526b683b4SJianfeng Tan 	 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
144626b683b4SJianfeng Tan 	 * (22) will be ignored.
144726b683b4SJianfeng Tan 	 */
1448fe19d49cSZhiyong Yang 	if (virtio_intr_enable(dev) < 0) {
144926b683b4SJianfeng Tan 		PMD_DRV_LOG(ERR, "interrupt enable failed");
145026b683b4SJianfeng Tan 		return -1;
145126b683b4SJianfeng Tan 	}
145226b683b4SJianfeng Tan 
145326b683b4SJianfeng Tan 	if (virtio_queues_bind_intr(dev) < 0) {
145426b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
145526b683b4SJianfeng Tan 		return -1;
145626b683b4SJianfeng Tan 	}
145726b683b4SJianfeng Tan 
145826b683b4SJianfeng Tan 	return 0;
145926b683b4SJianfeng Tan }
146026b683b4SJianfeng Tan 
146160e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1462198ab336SOlivier Matz static int
146360e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
14646c3169a3SBruce Richardson {
14656c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
14666c3169a3SBruce Richardson 	struct virtio_net_config *config;
14676c3169a3SBruce Richardson 	struct virtio_net_config local_config;
14689470427cSYuanhan Liu 	struct rte_pci_device *pci_dev = NULL;
146969c80d4eSYuanhan Liu 	int ret;
14706c3169a3SBruce Richardson 
14716c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
14726c3169a3SBruce Richardson 	vtpci_reset(hw);
14736c3169a3SBruce Richardson 
14743669a1afSOlivier Matz 	if (hw->vqs) {
14753669a1afSOlivier Matz 		virtio_dev_free_mbufs(eth_dev);
14763669a1afSOlivier Matz 		virtio_free_queues(hw);
14773669a1afSOlivier Matz 	}
14783669a1afSOlivier Matz 
14796c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
14806c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
14816c3169a3SBruce Richardson 
14826c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
14836c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
148460e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
14856ba1f63bSYuanhan Liu 		return -1;
14866c3169a3SBruce Richardson 
14872c2201fbSJianfeng Tan 	if (!hw->virtio_user_dev) {
1488c0802544SFerruh Yigit 		pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1489f229eb41SJianfeng Tan 		rte_eth_copy_pci_info(eth_dev, pci_dev);
1490f229eb41SJianfeng Tan 	}
1491f229eb41SJianfeng Tan 
1492a5ed8448SMatt Peters 	/* If host does not support both status and MSI-X then disable LSC */
1493fe19d49cSZhiyong Yang 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1494fe19d49cSZhiyong Yang 	    hw->use_msix != VIRTIO_MSIX_NONE)
1495198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1496a5ed8448SMatt Peters 	else
1497a5ed8448SMatt Peters 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1498954ea115SStephen Hemminger 
14996c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
15006ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
15016ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
15026c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
15036c3169a3SBruce Richardson 	else
15046c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
15056c3169a3SBruce Richardson 
15066c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
15076c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
15086c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
15096c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
15106c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
15116c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
15126c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
15136c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
15146c3169a3SBruce Richardson 
15156c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
15166c3169a3SBruce Richardson 		config = &local_config;
15176c3169a3SBruce Richardson 
15186d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
15196d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
15206d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
15216d7740e2SChangchun Ouyang 
15226c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
15236d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
15246d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
15256d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
15266c3169a3SBruce Richardson 		} else {
15276c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
15286c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
15296c3169a3SBruce Richardson 			config->status = 0;
15306c3169a3SBruce Richardson 		}
15316c3169a3SBruce Richardson 
15326c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
15336d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
15346d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
15356d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
15366d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
15376c3169a3SBruce Richardson 		} else {
15386c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
15396c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
15406c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
15416c3169a3SBruce Richardson 		}
15426c3169a3SBruce Richardson 
154345e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
15446c3169a3SBruce Richardson 
154549d26d9eSMaxime Coquelin 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
154649d26d9eSMaxime Coquelin 			vtpci_read_dev_config(hw,
154749d26d9eSMaxime Coquelin 				offsetof(struct virtio_net_config, mtu),
154849d26d9eSMaxime Coquelin 				&config->mtu,
154949d26d9eSMaxime Coquelin 				sizeof(config->mtu));
155049d26d9eSMaxime Coquelin 
155149d26d9eSMaxime Coquelin 			/*
155249d26d9eSMaxime Coquelin 			 * MTU value has already been checked at negotiation
155349d26d9eSMaxime Coquelin 			 * time, but check again in case it has changed since
155449d26d9eSMaxime Coquelin 			 * then, which should not happen.
155549d26d9eSMaxime Coquelin 			 */
155649d26d9eSMaxime Coquelin 			if (config->mtu < ETHER_MIN_MTU) {
155749d26d9eSMaxime Coquelin 				PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
155849d26d9eSMaxime Coquelin 						config->mtu);
155949d26d9eSMaxime Coquelin 				return -1;
156049d26d9eSMaxime Coquelin 			}
156149d26d9eSMaxime Coquelin 
156249d26d9eSMaxime Coquelin 			hw->max_mtu = config->mtu;
156349d26d9eSMaxime Coquelin 			/* Set initial MTU to maximum one supported by vhost */
156449d26d9eSMaxime Coquelin 			eth_dev->data->mtu = config->mtu;
156549d26d9eSMaxime Coquelin 
156649d26d9eSMaxime Coquelin 		} else {
156749d26d9eSMaxime Coquelin 			hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
156849d26d9eSMaxime Coquelin 				VLAN_TAG_LEN - hw->vtnet_hdr_size;
156949d26d9eSMaxime Coquelin 		}
157049d26d9eSMaxime Coquelin 
15716c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
15726c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
15736c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
15746c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15756c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
15766c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
15776c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
15786c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
15796c3169a3SBruce Richardson 	} else {
158045e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
158145e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
1582240da8b2SZhike Wang 		hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1583240da8b2SZhike Wang 			VLAN_TAG_LEN - hw->vtnet_hdr_size;
15846c3169a3SBruce Richardson 	}
15856c3169a3SBruce Richardson 
158669c80d4eSYuanhan Liu 	ret = virtio_alloc_queues(eth_dev);
158769c80d4eSYuanhan Liu 	if (ret < 0)
158869c80d4eSYuanhan Liu 		return ret;
158926b683b4SJianfeng Tan 
159026b683b4SJianfeng Tan 	if (eth_dev->data->dev_conf.intr_conf.rxq) {
159126b683b4SJianfeng Tan 		if (virtio_configure_intr(eth_dev) < 0) {
159226b683b4SJianfeng Tan 			PMD_INIT_LOG(ERR, "failed to configure interrupt");
159326b683b4SJianfeng Tan 			return -1;
159426b683b4SJianfeng Tan 		}
159526b683b4SJianfeng Tan 	}
159626b683b4SJianfeng Tan 
1597ee85024cSYuanhan Liu 	vtpci_reinit_complete(hw);
159869c80d4eSYuanhan Liu 
1599ce2eabddSJianfeng Tan 	if (pci_dev)
16006c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
16016c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
16026c3169a3SBruce Richardson 			pci_dev->id.device_id);
16036c3169a3SBruce Richardson 
1604198ab336SOlivier Matz 	return 0;
1605198ab336SOlivier Matz }
1606198ab336SOlivier Matz 
1607198ab336SOlivier Matz /*
16086d890f8aSYuanhan Liu  * Remap the PCI device again (IO port map for legacy device and
16096d890f8aSYuanhan Liu  * memory map for modern device), so that the secondary process
16106d890f8aSYuanhan Liu  * could have the PCI initiated correctly.
16116d890f8aSYuanhan Liu  */
16126d890f8aSYuanhan Liu static int
16136d890f8aSYuanhan Liu virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
16146d890f8aSYuanhan Liu {
16156d890f8aSYuanhan Liu 	if (hw->modern) {
16166d890f8aSYuanhan Liu 		/*
16176d890f8aSYuanhan Liu 		 * We don't have to re-parse the PCI config space, since
16183dcfe039SThomas Monjalon 		 * rte_pci_map_device() makes sure the mapped address
16196d890f8aSYuanhan Liu 		 * in secondary process would equal to the one mapped in
16206d890f8aSYuanhan Liu 		 * the primary process: error will be returned if that
16216d890f8aSYuanhan Liu 		 * requirement is not met.
16226d890f8aSYuanhan Liu 		 *
16236d890f8aSYuanhan Liu 		 * That said, we could simply reuse all cap pointers
16246d890f8aSYuanhan Liu 		 * (such as dev_cfg, common_cfg, etc.) parsed from the
16256d890f8aSYuanhan Liu 		 * primary process, which is stored in shared memory.
16266d890f8aSYuanhan Liu 		 */
16273dcfe039SThomas Monjalon 		if (rte_pci_map_device(pci_dev)) {
16286d890f8aSYuanhan Liu 			PMD_INIT_LOG(DEBUG, "failed to map pci device!");
16296d890f8aSYuanhan Liu 			return -1;
16306d890f8aSYuanhan Liu 		}
16316d890f8aSYuanhan Liu 	} else {
16323dcfe039SThomas Monjalon 		if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
16336d890f8aSYuanhan Liu 			return -1;
16346d890f8aSYuanhan Liu 	}
16356d890f8aSYuanhan Liu 
16366d890f8aSYuanhan Liu 	return 0;
16376d890f8aSYuanhan Liu }
16386d890f8aSYuanhan Liu 
16396d890f8aSYuanhan Liu static void
16406d890f8aSYuanhan Liu virtio_set_vtpci_ops(struct virtio_hw *hw)
16416d890f8aSYuanhan Liu {
1642e754c959SThomas Monjalon #ifdef RTE_VIRTIO_USER
16436d890f8aSYuanhan Liu 	if (hw->virtio_user_dev)
16446d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &virtio_user_ops;
1645e754c959SThomas Monjalon 	else
1646e754c959SThomas Monjalon #endif
1647e754c959SThomas Monjalon 	if (hw->modern)
16486d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &modern_ops;
16496d890f8aSYuanhan Liu 	else
16506d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &legacy_ops;
16516d890f8aSYuanhan Liu }
16526d890f8aSYuanhan Liu 
16536d890f8aSYuanhan Liu /*
1654198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1655198ab336SOlivier Matz  * It returns 0 on success.
1656198ab336SOlivier Matz  */
1657198ab336SOlivier Matz int
1658198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1659198ab336SOlivier Matz {
1660198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1661198ab336SOlivier Matz 	int ret;
1662198ab336SOlivier Matz 
1663198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1664198ab336SOlivier Matz 
1665198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1666198ab336SOlivier Matz 
1667198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
16686d890f8aSYuanhan Liu 		if (!hw->virtio_user_dev) {
1669c0802544SFerruh Yigit 			ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
16706d890f8aSYuanhan Liu 			if (ret)
16716d890f8aSYuanhan Liu 				return ret;
16726d890f8aSYuanhan Liu 		}
16736d890f8aSYuanhan Liu 
16746d890f8aSYuanhan Liu 		virtio_set_vtpci_ops(hw);
16754819eae8SOlivier Matz 		set_rxtx_funcs(eth_dev);
16764819eae8SOlivier Matz 
1677198ab336SOlivier Matz 		return 0;
1678198ab336SOlivier Matz 	}
1679198ab336SOlivier Matz 
1680198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1681198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1682198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1683198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1684198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1685198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1686198ab336SOlivier Matz 		return -ENOMEM;
1687198ab336SOlivier Matz 	}
1688198ab336SOlivier Matz 
168976873125SYuanhan Liu 	hw->port_id = eth_dev->data->port_id;
16906a7c0dfcSJan Blunck 	/* For virtio_user case the hw->virtio_user_dev is populated by
16916a7c0dfcSJan Blunck 	 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
16926a7c0dfcSJan Blunck 	 */
16936a7c0dfcSJan Blunck 	if (!hw->virtio_user_dev) {
1694c0802544SFerruh Yigit 		ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1695198ab336SOlivier Matz 		if (ret)
169643d18765SPengzhen Liu 			goto out;
1697198ab336SOlivier Matz 	}
1698198ab336SOlivier Matz 
169960e6f470SOlivier Matz 	/* reset device and negotiate default features */
170096cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1701198ab336SOlivier Matz 	if (ret < 0)
170243d18765SPengzhen Liu 		goto out;
1703198ab336SOlivier Matz 
17046c3169a3SBruce Richardson 	/* Setup interrupt callback  */
170562a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
170661e3ee17SYuanhan Liu 		rte_intr_callback_register(eth_dev->intr_handle,
17076c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
17086c3169a3SBruce Richardson 
17096c3169a3SBruce Richardson 	return 0;
171043d18765SPengzhen Liu 
171143d18765SPengzhen Liu out:
171243d18765SPengzhen Liu 	rte_free(eth_dev->data->mac_addrs);
171343d18765SPengzhen Liu 	return ret;
17146c3169a3SBruce Richardson }
17156c3169a3SBruce Richardson 
1716abf4c84bSBernard Iremonger static int
1717abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1718abf4c84bSBernard Iremonger {
1719abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1720abf4c84bSBernard Iremonger 
1721abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1722abf4c84bSBernard Iremonger 		return -EPERM;
1723abf4c84bSBernard Iremonger 
17243f3b3d54SYuanhan Liu 	virtio_dev_stop(eth_dev);
1725abf4c84bSBernard Iremonger 	virtio_dev_close(eth_dev);
1726abf4c84bSBernard Iremonger 
1727abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1728abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1729abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1730abf4c84bSBernard Iremonger 
1731abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1732abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1733abf4c84bSBernard Iremonger 
1734abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
173562a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
173661e3ee17SYuanhan Liu 		rte_intr_callback_unregister(eth_dev->intr_handle,
1737abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1738abf4c84bSBernard Iremonger 						eth_dev);
17399470427cSYuanhan Liu 	if (eth_dev->device)
1740c0802544SFerruh Yigit 		rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1741abf4c84bSBernard Iremonger 
1742abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1743abf4c84bSBernard Iremonger 
1744abf4c84bSBernard Iremonger 	return 0;
1745abf4c84bSBernard Iremonger }
1746abf4c84bSBernard Iremonger 
1747fdf91e0fSJan Blunck static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1748fdf91e0fSJan Blunck 	struct rte_pci_device *pci_dev)
1749fdf91e0fSJan Blunck {
1750fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1751fdf91e0fSJan Blunck 		eth_virtio_dev_init);
1752fdf91e0fSJan Blunck }
1753fdf91e0fSJan Blunck 
1754fdf91e0fSJan Blunck static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1755fdf91e0fSJan Blunck {
1756fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1757fdf91e0fSJan Blunck }
1758fdf91e0fSJan Blunck 
1759fdf91e0fSJan Blunck static struct rte_pci_driver rte_virtio_pmd = {
1760cd433ce5SDavid Marchand 	.driver = {
1761cd433ce5SDavid Marchand 		.name = "net_virtio",
1762cd433ce5SDavid Marchand 	},
17636c3169a3SBruce Richardson 	.id_table = pci_id_virtio_map,
176422dda618SBen Walker 	.drv_flags = 0,
1765fdf91e0fSJan Blunck 	.probe = eth_virtio_pci_probe,
1766fdf91e0fSJan Blunck 	.remove = eth_virtio_pci_remove,
17676c3169a3SBruce Richardson };
17686c3169a3SBruce Richardson 
1769c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1770c830cb29SDavid Marchand static void
1771c830cb29SDavid Marchand rte_virtio_pmd_init(void)
17726c3169a3SBruce Richardson {
17736c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
17746c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1775c830cb29SDavid Marchand 		return;
17766c3169a3SBruce Richardson 	}
17776c3169a3SBruce Richardson 
17783dcfe039SThomas Monjalon 	rte_pci_register(&rte_virtio_pmd);
17796c3169a3SBruce Richardson }
17806c3169a3SBruce Richardson 
17816c3169a3SBruce Richardson /*
17826c3169a3SBruce Richardson  * Configure virtio device
17836c3169a3SBruce Richardson  * It returns 0 on success.
17846c3169a3SBruce Richardson  */
17856c3169a3SBruce Richardson static int
17866c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
17876c3169a3SBruce Richardson {
17886c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
17896c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
1790ec9f3d12SOlivier Matz 	uint64_t req_features;
1791e7b9d1d2SJiayu Hu 	int ret;
17926c3169a3SBruce Richardson 
17936c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
1794ec9f3d12SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
17954dab342bSStephen Hemminger 
1796e7b9d1d2SJiayu Hu 	if (dev->data->dev_conf.intr_conf.rxq) {
1797e7b9d1d2SJiayu Hu 		ret = virtio_init_device(dev, hw->req_guest_features);
1798e7b9d1d2SJiayu Hu 		if (ret < 0)
1799e7b9d1d2SJiayu Hu 			return ret;
1800e7b9d1d2SJiayu Hu 	}
1801e7b9d1d2SJiayu Hu 
1802d67d86ceSOlivier Matz 	/* The name hw_ip_checksum is a bit confusing since it can be
1803d67d86ceSOlivier Matz 	 * set by the application to request L3 and/or L4 checksums. In
1804d67d86ceSOlivier Matz 	 * case of virtio, only L4 checksum is supported.
1805d67d86ceSOlivier Matz 	 */
1806d67d86ceSOlivier Matz 	if (rxmode->hw_ip_checksum)
1807d67d86ceSOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1808d67d86ceSOlivier Matz 
1809ec9f3d12SOlivier Matz 	if (rxmode->enable_lro)
1810ec9f3d12SOlivier Matz 		req_features |=
1811ec9f3d12SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1812ec9f3d12SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
18136c3169a3SBruce Richardson 
1814ec9f3d12SOlivier Matz 	/* if request features changed, reinit the device */
1815ec9f3d12SOlivier Matz 	if (req_features != hw->req_guest_features) {
1816ec9f3d12SOlivier Matz 		ret = virtio_init_device(dev, req_features);
1817ec9f3d12SOlivier Matz 		if (ret < 0)
1818ec9f3d12SOlivier Matz 			return ret;
1819ec9f3d12SOlivier Matz 	}
1820ec9f3d12SOlivier Matz 
1821d67d86ceSOlivier Matz 	if (rxmode->hw_ip_checksum &&
1822d67d86ceSOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
182378fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
1824d67d86ceSOlivier Matz 			"rx checksum not available on this host");
1825d67d86ceSOlivier Matz 		return -ENOTSUP;
1826d67d86ceSOlivier Matz 	}
1827d67d86ceSOlivier Matz 
1828ec9f3d12SOlivier Matz 	if (rxmode->enable_lro &&
1829ec9f3d12SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1830b628cb24STiwei Bie 		 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
183178fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
1832ec9f3d12SOlivier Matz 			"Large Receive Offload not available on this host");
183386d59b21SOlivier Matz 		return -ENOTSUP;
183486d59b21SOlivier Matz 	}
183586d59b21SOlivier Matz 
183669c80d4eSYuanhan Liu 	/* start control queue */
183769c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
183845e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
183945e4acd4SOlivier Matz 
18406c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
18416c3169a3SBruce Richardson 
18426c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
18436c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
184478fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
18456c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
18466c3169a3SBruce Richardson 		return -ENOTSUP;
18476c3169a3SBruce Richardson 	}
18486c3169a3SBruce Richardson 
184962a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1850981e61f5SJianfeng Tan 		/* Enable vector (0) for Link State Intrerrupt */
1851981e61f5SJianfeng Tan 		if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1852981e61f5SJianfeng Tan 				VIRTIO_MSI_NO_VECTOR) {
18536c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
18546c3169a3SBruce Richardson 			return -EBUSY;
18556c3169a3SBruce Richardson 		}
18566c3169a3SBruce Richardson 
18571978a9dcSXiao Wang 	rte_spinlock_init(&hw->state_lock);
18581978a9dcSXiao Wang 
185909649363SOlivier Matz 	hw->use_simple_rx = 1;
186009649363SOlivier Matz 	hw->use_simple_tx = 1;
18614819eae8SOlivier Matz 
18628f3d451dSSamuel Gauthier #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
186309649363SOlivier Matz 	if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
186409649363SOlivier Matz 		hw->use_simple_rx = 0;
186509649363SOlivier Matz 		hw->use_simple_tx = 0;
186609649363SOlivier Matz 	}
18674819eae8SOlivier Matz #endif
186809649363SOlivier Matz 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
186909649363SOlivier Matz 		hw->use_simple_rx = 0;
187009649363SOlivier Matz 		hw->use_simple_tx = 0;
187109649363SOlivier Matz 	}
18724819eae8SOlivier Matz 
187316e48c9eSOlivier Matz 	if (rxmode->hw_ip_checksum)
187416e48c9eSOlivier Matz 		hw->use_simple_rx = 0;
187516e48c9eSOlivier Matz 
18766c3169a3SBruce Richardson 	return 0;
18776c3169a3SBruce Richardson }
18786c3169a3SBruce Richardson 
18796c3169a3SBruce Richardson 
18806c3169a3SBruce Richardson static int
18816c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
18826c3169a3SBruce Richardson {
18836c3169a3SBruce Richardson 	uint16_t nb_queues, i;
188401ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
188501ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
1886663c7679SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
1887efc83a1eSOlivier Matz 	int ret;
1888efc83a1eSOlivier Matz 
1889efc83a1eSOlivier Matz 	/* Finish the initialization of the queues */
1890efc83a1eSOlivier Matz 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1891efc83a1eSOlivier Matz 		ret = virtio_dev_rx_queue_setup_finish(dev, i);
1892efc83a1eSOlivier Matz 		if (ret < 0)
1893efc83a1eSOlivier Matz 			return ret;
1894efc83a1eSOlivier Matz 	}
1895efc83a1eSOlivier Matz 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1896efc83a1eSOlivier Matz 		ret = virtio_dev_tx_queue_setup_finish(dev, i);
1897efc83a1eSOlivier Matz 		if (ret < 0)
1898efc83a1eSOlivier Matz 			return ret;
1899efc83a1eSOlivier Matz 	}
19006c3169a3SBruce Richardson 
19016c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1902954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
190362a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
19046c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
19056c3169a3SBruce Richardson 			return -ENOTSUP;
19066c3169a3SBruce Richardson 		}
1907349a447bSJianfeng Tan 	}
1908349a447bSJianfeng Tan 
1909349a447bSJianfeng Tan 	/* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1910349a447bSJianfeng Tan 	 * in device configure, but it could be unmapped  when device is
1911349a447bSJianfeng Tan 	 * stopped.
1912349a447bSJianfeng Tan 	 */
1913349a447bSJianfeng Tan 	if (dev->data->dev_conf.intr_conf.lsc ||
1914349a447bSJianfeng Tan 	    dev->data->dev_conf.intr_conf.rxq) {
1915fe19d49cSZhiyong Yang 		virtio_intr_disable(dev);
19166c3169a3SBruce Richardson 
1917fe19d49cSZhiyong Yang 		if (virtio_intr_enable(dev) < 0) {
19186c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
19196c3169a3SBruce Richardson 			return -EIO;
19206c3169a3SBruce Richardson 		}
19216c3169a3SBruce Richardson 	}
19226c3169a3SBruce Richardson 
19236c3169a3SBruce Richardson 	/*Notify the backend
19246c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
19256c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
19266c3169a3SBruce Richardson 	 */
192760d4a353SYuanhan Liu 	nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1928663c7679SYuanhan Liu 	if (hw->max_queue_pairs > 1) {
19296c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
19306c3169a3SBruce Richardson 			return -EINVAL;
19316c3169a3SBruce Richardson 	}
19326c3169a3SBruce Richardson 
19336c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
19346c3169a3SBruce Richardson 
193560d4a353SYuanhan Liu 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
193601ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
1937d8227497STiwei Bie 		/* Flush the old packets */
1938bcf55c93STiwei Bie 		virtqueue_rxvq_flush(rxvq->vq);
193901ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
194001ad44fdSHuawei Xie 	}
19416c3169a3SBruce Richardson 
1942924da8f1SSteven Luong 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1943924da8f1SSteven Luong 		txvq = dev->data->tx_queues[i];
1944924da8f1SSteven Luong 		virtqueue_notify(txvq->vq);
1945924da8f1SSteven Luong 	}
1946924da8f1SSteven Luong 
19476c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
19486c3169a3SBruce Richardson 
194901ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
195001ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
195101ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
195201ad44fdSHuawei Xie 	}
19536c3169a3SBruce Richardson 
195401ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
195501ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
195601ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
195701ad44fdSHuawei Xie 	}
195858d9fe40SJianfeng Tan 
19594819eae8SOlivier Matz 	set_rxtx_funcs(dev);
1960aa9f0606SYuanhan Liu 	hw->started = 1;
19616c3169a3SBruce Richardson 
196258d9fe40SJianfeng Tan 	/* Initialize Link state */
196358d9fe40SJianfeng Tan 	virtio_dev_link_update(dev, 0);
196458d9fe40SJianfeng Tan 
19656c3169a3SBruce Richardson 	return 0;
19666c3169a3SBruce Richardson }
19676c3169a3SBruce Richardson 
19686c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
19696c3169a3SBruce Richardson {
1970bdb32afbSOlivier Matz 	struct virtio_hw *hw = dev->data->dev_private;
1971bdb32afbSOlivier Matz 	uint16_t nr_vq = virtio_get_nr_vq(hw);
1972bdb32afbSOlivier Matz 	const char *type __rte_unused;
1973bdb32afbSOlivier Matz 	unsigned int i, mbuf_num = 0;
1974bdb32afbSOlivier Matz 	struct virtqueue *vq;
19756c3169a3SBruce Richardson 	struct rte_mbuf *buf;
1976bdb32afbSOlivier Matz 	int queue_type;
19776c3169a3SBruce Richardson 
1978bdb32afbSOlivier Matz 	for (i = 0; i < nr_vq; i++) {
1979bdb32afbSOlivier Matz 		vq = hw->vqs[i];
1980bdb32afbSOlivier Matz 		if (!vq)
1981bdb32afbSOlivier Matz 			continue;
198201ad44fdSHuawei Xie 
1983bdb32afbSOlivier Matz 		queue_type = virtio_get_queue_type(hw, i);
1984bdb32afbSOlivier Matz 		if (queue_type == VTNET_RQ)
1985bdb32afbSOlivier Matz 			type = "rxq";
1986bdb32afbSOlivier Matz 		else if (queue_type == VTNET_TQ)
1987bdb32afbSOlivier Matz 			type = "txq";
1988bdb32afbSOlivier Matz 		else
19893669a1afSOlivier Matz 			continue;
19903669a1afSOlivier Matz 
19916c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
1992bdb32afbSOlivier Matz 			"Before freeing %s[%d] used and unused buf",
1993bdb32afbSOlivier Matz 			type, i);
1994bdb32afbSOlivier Matz 		VIRTQUEUE_DUMP(vq);
19956c3169a3SBruce Richardson 
1996727411f5SOlivier Matz 		while ((buf = virtqueue_detach_unused(vq)) != NULL) {
19976c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
19986c3169a3SBruce Richardson 			mbuf_num++;
19996c3169a3SBruce Richardson 		}
20006c3169a3SBruce Richardson 
20016c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
2002bdb32afbSOlivier Matz 			"After freeing %s[%d] used and unused buf",
2003bdb32afbSOlivier Matz 			type, i);
2004bdb32afbSOlivier Matz 		VIRTQUEUE_DUMP(vq);
20056c3169a3SBruce Richardson 	}
20066c3169a3SBruce Richardson 
2007bdb32afbSOlivier Matz 	PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
20086c3169a3SBruce Richardson }
20096c3169a3SBruce Richardson 
20106c3169a3SBruce Richardson /*
20116c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
20126c3169a3SBruce Richardson  */
20136c3169a3SBruce Richardson static void
20146c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
20156c3169a3SBruce Richardson {
2016aa9f0606SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
20176c3169a3SBruce Richardson 	struct rte_eth_link link;
2018349a447bSJianfeng Tan 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
20196c3169a3SBruce Richardson 
20206c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
20216c3169a3SBruce Richardson 
20221978a9dcSXiao Wang 	rte_spinlock_lock(&hw->state_lock);
2023349a447bSJianfeng Tan 	if (intr_conf->lsc || intr_conf->rxq)
2024fe19d49cSZhiyong Yang 		virtio_intr_disable(dev);
20256c3169a3SBruce Richardson 
2026aa9f0606SYuanhan Liu 	hw->started = 0;
20276c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
20286c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
20291978a9dcSXiao Wang 	rte_spinlock_unlock(&hw->state_lock);
20306c3169a3SBruce Richardson }
20316c3169a3SBruce Richardson 
20326c3169a3SBruce Richardson static int
20336c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
20346c3169a3SBruce Richardson {
20356c3169a3SBruce Richardson 	struct rte_eth_link link, old;
20366c3169a3SBruce Richardson 	uint16_t status;
20376c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
20386c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
20396c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
20406c3169a3SBruce Richardson 	old = link;
204111319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
204267090011SZhiyong Yang 	link.link_speed  = ETH_SPEED_NUM_10G;
20436c3169a3SBruce Richardson 
2044aa9f0606SYuanhan Liu 	if (hw->started == 0) {
2045aa9f0606SYuanhan Liu 		link.link_status = ETH_LINK_DOWN;
2046aa9f0606SYuanhan Liu 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
20476c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
20486c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
20496c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
20506c3169a3SBruce Richardson 				&status, sizeof(status));
20516c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
205209419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
20536c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
20546c3169a3SBruce Richardson 				     dev->data->port_id);
20556c3169a3SBruce Richardson 		} else {
205609419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
20576c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
20586c3169a3SBruce Richardson 				     dev->data->port_id);
20596c3169a3SBruce Richardson 		}
20606c3169a3SBruce Richardson 	} else {
206109419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
20626c3169a3SBruce Richardson 	}
20636c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
20646c3169a3SBruce Richardson 
20656c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
20666c3169a3SBruce Richardson }
20676c3169a3SBruce Richardson 
2068289ba0c0SDavid Harton static int
2069289ba0c0SDavid Harton virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2070289ba0c0SDavid Harton {
2071289ba0c0SDavid Harton 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2072289ba0c0SDavid Harton 	struct virtio_hw *hw = dev->data->dev_private;
2073289ba0c0SDavid Harton 
2074289ba0c0SDavid Harton 	if (mask & ETH_VLAN_FILTER_MASK) {
2075289ba0c0SDavid Harton 		if (rxmode->hw_vlan_filter &&
2076289ba0c0SDavid Harton 				!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2077289ba0c0SDavid Harton 
2078289ba0c0SDavid Harton 			PMD_DRV_LOG(NOTICE,
2079289ba0c0SDavid Harton 				"vlan filtering not available on this host");
2080289ba0c0SDavid Harton 
2081289ba0c0SDavid Harton 			return -ENOTSUP;
2082289ba0c0SDavid Harton 		}
2083289ba0c0SDavid Harton 	}
2084289ba0c0SDavid Harton 
2085289ba0c0SDavid Harton 	if (mask & ETH_VLAN_STRIP_MASK)
2086289ba0c0SDavid Harton 		hw->vlan_strip = rxmode->hw_vlan_strip;
2087289ba0c0SDavid Harton 
2088289ba0c0SDavid Harton 	return 0;
2089289ba0c0SDavid Harton }
2090289ba0c0SDavid Harton 
20916c3169a3SBruce Richardson static void
20926c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
20936c3169a3SBruce Richardson {
2094c1e55ed3SOlivier Matz 	uint64_t tso_mask, host_features;
20956c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
20966c3169a3SBruce Richardson 
2097b392e987SIdo Barnea 	dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2098b392e987SIdo Barnea 
2099c0802544SFerruh Yigit 	dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
210045e4acd4SOlivier Matz 	dev_info->max_rx_queues =
210145e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
210245e4acd4SOlivier Matz 	dev_info->max_tx_queues =
210345e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
21046c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
21056c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
21066c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
21076c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
21086c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
21096c3169a3SBruce Richardson 	};
211058169a9cSOlivier Matz 
2111c1e55ed3SOlivier Matz 	host_features = VTPCI_OPS(hw)->get_features(hw);
2112c1e55ed3SOlivier Matz 	dev_info->rx_offload_capa = 0;
2113c1e55ed3SOlivier Matz 	if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2114c1e55ed3SOlivier Matz 		dev_info->rx_offload_capa |=
2115c1e55ed3SOlivier Matz 			DEV_RX_OFFLOAD_TCP_CKSUM |
2116c1e55ed3SOlivier Matz 			DEV_RX_OFFLOAD_UDP_CKSUM;
2117c1e55ed3SOlivier Matz 	}
2118c1e55ed3SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2119c1e55ed3SOlivier Matz 		(1ULL << VIRTIO_NET_F_GUEST_TSO6);
2120ec9f3d12SOlivier Matz 	if ((host_features & tso_mask) == tso_mask)
2121ec9f3d12SOlivier Matz 		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2122c1e55ed3SOlivier Matz 
2123c1e55ed3SOlivier Matz 	dev_info->tx_offload_capa = 0;
212458169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
212558169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
212658169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
212758169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
212858169a9cSOlivier Matz 	}
212969657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
213069657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
213169657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
213269657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
21336c3169a3SBruce Richardson }
21346c3169a3SBruce Richardson 
21356c3169a3SBruce Richardson /*
21366c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
21376c3169a3SBruce Richardson  */
21386c3169a3SBruce Richardson static int
21396c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
21406c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
21416c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
21426c3169a3SBruce Richardson {
21436c3169a3SBruce Richardson 	return 0;
21446c3169a3SBruce Richardson }
21456c3169a3SBruce Richardson 
214601f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
214701f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
214806e81dc9SDavid Marchand RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
214900628188SStephen Hemminger 
215000628188SStephen Hemminger RTE_INIT(virtio_init_log);
215100628188SStephen Hemminger static void
215200628188SStephen Hemminger virtio_init_log(void)
215300628188SStephen Hemminger {
2154*bace76f6SHarry van Haaren 	virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
215500628188SStephen Hemminger 	if (virtio_logtype_init >= 0)
215600628188SStephen Hemminger 		rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2157*bace76f6SHarry van Haaren 	virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
215800628188SStephen Hemminger 	if (virtio_logtype_driver >= 0)
215900628188SStephen Hemminger 		rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);
216000628188SStephen Hemminger }
2161