xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision 455da545)
16c3169a3SBruce Richardson /*-
26c3169a3SBruce Richardson  *   BSD LICENSE
36c3169a3SBruce Richardson  *
4baf91c39SRemy Horton  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
56c3169a3SBruce Richardson  *   All rights reserved.
66c3169a3SBruce Richardson  *
76c3169a3SBruce Richardson  *   Redistribution and use in source and binary forms, with or without
86c3169a3SBruce Richardson  *   modification, are permitted provided that the following conditions
96c3169a3SBruce Richardson  *   are met:
106c3169a3SBruce Richardson  *
116c3169a3SBruce Richardson  *     * Redistributions of source code must retain the above copyright
126c3169a3SBruce Richardson  *       notice, this list of conditions and the following disclaimer.
136c3169a3SBruce Richardson  *     * Redistributions in binary form must reproduce the above copyright
146c3169a3SBruce Richardson  *       notice, this list of conditions and the following disclaimer in
156c3169a3SBruce Richardson  *       the documentation and/or other materials provided with the
166c3169a3SBruce Richardson  *       distribution.
176c3169a3SBruce Richardson  *     * Neither the name of Intel Corporation nor the names of its
186c3169a3SBruce Richardson  *       contributors may be used to endorse or promote products derived
196c3169a3SBruce Richardson  *       from this software without specific prior written permission.
206c3169a3SBruce Richardson  *
216c3169a3SBruce Richardson  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
226c3169a3SBruce Richardson  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
236c3169a3SBruce Richardson  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
246c3169a3SBruce Richardson  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
256c3169a3SBruce Richardson  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
266c3169a3SBruce Richardson  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
276c3169a3SBruce Richardson  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
286c3169a3SBruce Richardson  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
296c3169a3SBruce Richardson  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
306c3169a3SBruce Richardson  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
316c3169a3SBruce Richardson  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326c3169a3SBruce Richardson  */
336c3169a3SBruce Richardson 
346c3169a3SBruce Richardson #include <stdint.h>
356c3169a3SBruce Richardson #include <string.h>
366c3169a3SBruce Richardson #include <stdio.h>
376c3169a3SBruce Richardson #include <errno.h>
386c3169a3SBruce Richardson #include <unistd.h>
396c3169a3SBruce Richardson 
406c3169a3SBruce Richardson #include <rte_ethdev.h>
41fdf91e0fSJan Blunck #include <rte_ethdev_pci.h>
426c3169a3SBruce Richardson #include <rte_memcpy.h>
436c3169a3SBruce Richardson #include <rte_string_fns.h>
446c3169a3SBruce Richardson #include <rte_memzone.h>
456c3169a3SBruce Richardson #include <rte_malloc.h>
466c3169a3SBruce Richardson #include <rte_atomic.h>
476c3169a3SBruce Richardson #include <rte_branch_prediction.h>
486c3169a3SBruce Richardson #include <rte_pci.h>
49c752998bSGaetan Rivet #include <rte_bus_pci.h>
506c3169a3SBruce Richardson #include <rte_ether.h>
516c3169a3SBruce Richardson #include <rte_common.h>
52abf4c84bSBernard Iremonger #include <rte_errno.h>
534819eae8SOlivier Matz #include <rte_cpuflags.h>
546c3169a3SBruce Richardson 
556c3169a3SBruce Richardson #include <rte_memory.h>
566c3169a3SBruce Richardson #include <rte_eal.h>
576c3169a3SBruce Richardson #include <rte_dev.h>
586c3169a3SBruce Richardson 
596c3169a3SBruce Richardson #include "virtio_ethdev.h"
606c3169a3SBruce Richardson #include "virtio_pci.h"
616c3169a3SBruce Richardson #include "virtio_logs.h"
626c3169a3SBruce Richardson #include "virtqueue.h"
63cab04612SHuawei Xie #include "virtio_rxtx.h"
646c3169a3SBruce Richardson 
65abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
666c3169a3SBruce Richardson static int  virtio_dev_configure(struct rte_eth_dev *dev);
676c3169a3SBruce Richardson static int  virtio_dev_start(struct rte_eth_dev *dev);
686c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev);
696c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
706c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
716c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
726c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
736c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev,
746c3169a3SBruce Richardson 				struct rte_eth_dev_info *dev_info);
756c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev,
76dd2c630aSFerruh Yigit 	int wait_to_complete);
77289ba0c0SDavid Harton static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
786c3169a3SBruce Richardson 
796c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw);
806c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw);
816c3169a3SBruce Richardson 
82d5b0924bSMatan Azrad static int virtio_dev_stats_get(struct rte_eth_dev *dev,
8376d4c652SHarry van Haaren 				 struct rte_eth_stats *stats);
8476d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
85e2aae1c1SRemy Horton 				 struct rte_eth_xstat *xstats, unsigned n);
86baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
87baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
88baf91c39SRemy Horton 				       unsigned limit);
896c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
906c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
916c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
926c3169a3SBruce Richardson 				uint16_t vlan_id, int on);
936d01e580SWei Dai static int virtio_mac_addr_add(struct rte_eth_dev *dev,
946c3169a3SBruce Richardson 				struct ether_addr *mac_addr,
95dd2c630aSFerruh Yigit 				uint32_t index, uint32_t vmdq);
966c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
976c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev,
986c3169a3SBruce Richardson 				struct ether_addr *mac_addr);
996c3169a3SBruce Richardson 
1006c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set(
101dd2c630aSFerruh Yigit 	struct rte_eth_dev *eth_dev,
102dd2c630aSFerruh Yigit 	uint16_t queue_id,
103dd2c630aSFerruh Yigit 	uint8_t stat_idx,
104dd2c630aSFerruh Yigit 	uint8_t is_rx);
1056c3169a3SBruce Richardson 
1066c3169a3SBruce Richardson /*
1076c3169a3SBruce Richardson  * The set of PCI devices this driver supports
1086c3169a3SBruce Richardson  */
1096c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = {
1104c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
1114c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
1126c3169a3SBruce Richardson 	{ .vendor_id = 0, /* sentinel */ },
1136c3169a3SBruce Richardson };
1146c3169a3SBruce Richardson 
11576d4c652SHarry van Haaren struct rte_virtio_xstats_name_off {
11676d4c652SHarry van Haaren 	char name[RTE_ETH_XSTATS_NAME_SIZE];
11776d4c652SHarry van Haaren 	unsigned offset;
11876d4c652SHarry van Haaren };
11976d4c652SHarry van Haaren 
12076d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */
12101ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
12201ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
12301ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
12401ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_rx, stats.errors)},
12501ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
12601ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
12701ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
12801ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
12901ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
13001ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
13101ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
13201ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
13381f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
13481f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
13576d4c652SHarry van Haaren };
13676d4c652SHarry van Haaren 
13701ad44fdSHuawei Xie /* [rt]x_qX_ is prepended to the name string here */
13801ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
13901ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
14001ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
14101ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_tx, stats.errors)},
14201ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
14301ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
14401ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
14501ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
14601ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
14701ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
14801ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
14901ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
15081f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
15181f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
15201ad44fdSHuawei Xie };
15301ad44fdSHuawei Xie 
15401ad44fdSHuawei Xie #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
15501ad44fdSHuawei Xie 			    sizeof(rte_virtio_rxq_stat_strings[0]))
15601ad44fdSHuawei Xie #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
15701ad44fdSHuawei Xie 			    sizeof(rte_virtio_txq_stat_strings[0]))
15876d4c652SHarry van Haaren 
159553f4593SYuanhan Liu struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
160553f4593SYuanhan Liu 
1616c3169a3SBruce Richardson static int
16201ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
1636c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1646c3169a3SBruce Richardson {
1659e71668bSDamjan Marion 	uint32_t head, i;
1666c3169a3SBruce Richardson 	int k, sum = 0;
1676c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
168da4f2e4bSZhiyong Yang 	struct virtio_pmd_ctrl *result;
16901ad44fdSHuawei Xie 	struct virtqueue *vq;
1706c3169a3SBruce Richardson 
1716c3169a3SBruce Richardson 	ctrl->status = status;
1726c3169a3SBruce Richardson 
1733bd60a27SJianfeng Tan 	if (!cvq || !cvq->vq) {
1740bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "Control queue is not supported.");
1756c3169a3SBruce Richardson 		return -1;
1766c3169a3SBruce Richardson 	}
17701ad44fdSHuawei Xie 	vq = cvq->vq;
1789e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1796c3169a3SBruce Richardson 
1806c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1816c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1826c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1836c3169a3SBruce Richardson 
1846c3169a3SBruce Richardson 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
1856c3169a3SBruce Richardson 		return -1;
1866c3169a3SBruce Richardson 
18701ad44fdSHuawei Xie 	memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
1886c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1896c3169a3SBruce Richardson 
1906c3169a3SBruce Richardson 	/*
1916c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1926c3169a3SBruce Richardson 	 * One TX packet for header;
1936c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1946c3169a3SBruce Richardson 	 * One RX packet for ACK.
1956c3169a3SBruce Richardson 	 */
1966c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
197f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
1986c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1996c3169a3SBruce Richardson 	vq->vq_free_cnt--;
2006c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
2016c3169a3SBruce Richardson 
2026c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
2036c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
204f24f8f9fSJianfeng Tan 		vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2056c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
2066c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
2076c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
2086c3169a3SBruce Richardson 		sum += dlen[k];
2096c3169a3SBruce Richardson 		vq->vq_free_cnt--;
2106c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
2116c3169a3SBruce Richardson 	}
2126c3169a3SBruce Richardson 
2136c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
214f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2156c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
2166c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
2176c3169a3SBruce Richardson 	vq->vq_free_cnt--;
2186c3169a3SBruce Richardson 
2196c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
2206c3169a3SBruce Richardson 
2216c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
2226c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
2236c3169a3SBruce Richardson 
2246c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
2256c3169a3SBruce Richardson 
2266c3169a3SBruce Richardson 	virtqueue_notify(vq);
2276c3169a3SBruce Richardson 
2286c3169a3SBruce Richardson 	rte_rmb();
229b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq) == 0) {
2306c3169a3SBruce Richardson 		rte_rmb();
2316c3169a3SBruce Richardson 		usleep(100);
2326c3169a3SBruce Richardson 	}
2336c3169a3SBruce Richardson 
234b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq)) {
2356c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
2366c3169a3SBruce Richardson 		struct vring_used_elem *uep;
2376c3169a3SBruce Richardson 
2386c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
2396c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
2406c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
2416c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
2426c3169a3SBruce Richardson 		desc_idx = idx;
2436c3169a3SBruce Richardson 
2446c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
2456c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
2466c3169a3SBruce Richardson 			vq->vq_free_cnt++;
2476c3169a3SBruce Richardson 		}
2486c3169a3SBruce Richardson 
2496c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2506c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2516c3169a3SBruce Richardson 
2526c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2536c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2546c3169a3SBruce Richardson 	}
2556c3169a3SBruce Richardson 
2566c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2576c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2586c3169a3SBruce Richardson 
259da4f2e4bSZhiyong Yang 	result = cvq->virtio_net_hdr_mz->addr;
2606c3169a3SBruce Richardson 
261da4f2e4bSZhiyong Yang 	return result->status;
2626c3169a3SBruce Richardson }
2636c3169a3SBruce Richardson 
2646c3169a3SBruce Richardson static int
2656c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2666c3169a3SBruce Richardson {
2676c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2686c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2696c3169a3SBruce Richardson 	int dlen[1];
2706c3169a3SBruce Richardson 	int ret;
2716c3169a3SBruce Richardson 
2726c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2736c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2746c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2756c3169a3SBruce Richardson 
2766c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2776c3169a3SBruce Richardson 
2786c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2796c3169a3SBruce Richardson 	if (ret) {
2806c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2816c3169a3SBruce Richardson 			  "failed, this is too late now...");
2826c3169a3SBruce Richardson 		return -EINVAL;
2836c3169a3SBruce Richardson 	}
2846c3169a3SBruce Richardson 
2856c3169a3SBruce Richardson 	return 0;
2866c3169a3SBruce Richardson }
2876c3169a3SBruce Richardson 
28869c80d4eSYuanhan Liu static void
28969c80d4eSYuanhan Liu virtio_dev_queue_release(void *queue __rte_unused)
2902963d99aSJianfeng Tan {
29169c80d4eSYuanhan Liu 	/* do nothing */
2925382b188SBernard Iremonger }
2935382b188SBernard Iremonger 
29469c80d4eSYuanhan Liu static int
29569c80d4eSYuanhan Liu virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
29669c80d4eSYuanhan Liu {
29769c80d4eSYuanhan Liu 	if (vtpci_queue_idx == hw->max_queue_pairs * 2)
29869c80d4eSYuanhan Liu 		return VTNET_CQ;
29969c80d4eSYuanhan Liu 	else if (vtpci_queue_idx % 2 == 0)
30069c80d4eSYuanhan Liu 		return VTNET_RQ;
30169c80d4eSYuanhan Liu 	else
30269c80d4eSYuanhan Liu 		return VTNET_TQ;
30369c80d4eSYuanhan Liu }
30469c80d4eSYuanhan Liu 
30569c80d4eSYuanhan Liu static uint16_t
30669c80d4eSYuanhan Liu virtio_get_nr_vq(struct virtio_hw *hw)
30769c80d4eSYuanhan Liu {
30869c80d4eSYuanhan Liu 	uint16_t nr_vq = hw->max_queue_pairs * 2;
30969c80d4eSYuanhan Liu 
31069c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
31169c80d4eSYuanhan Liu 		nr_vq += 1;
31269c80d4eSYuanhan Liu 
31369c80d4eSYuanhan Liu 	return nr_vq;
31469c80d4eSYuanhan Liu }
31569c80d4eSYuanhan Liu 
316f4d1ad15SYuanhan Liu static void
317f4d1ad15SYuanhan Liu virtio_init_vring(struct virtqueue *vq)
318f4d1ad15SYuanhan Liu {
319f4d1ad15SYuanhan Liu 	int size = vq->vq_nentries;
320f4d1ad15SYuanhan Liu 	struct vring *vr = &vq->vq_ring;
321f4d1ad15SYuanhan Liu 	uint8_t *ring_mem = vq->vq_ring_virt_mem;
322f4d1ad15SYuanhan Liu 
323f4d1ad15SYuanhan Liu 	PMD_INIT_FUNC_TRACE();
324f4d1ad15SYuanhan Liu 
325f4d1ad15SYuanhan Liu 	/*
326f4d1ad15SYuanhan Liu 	 * Reinitialise since virtio port might have been stopped and restarted
327f4d1ad15SYuanhan Liu 	 */
328f4d1ad15SYuanhan Liu 	memset(ring_mem, 0, vq->vq_ring_size);
329f4d1ad15SYuanhan Liu 	vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
330f4d1ad15SYuanhan Liu 	vq->vq_used_cons_idx = 0;
331f4d1ad15SYuanhan Liu 	vq->vq_desc_head_idx = 0;
332f4d1ad15SYuanhan Liu 	vq->vq_avail_idx = 0;
333f4d1ad15SYuanhan Liu 	vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
334f4d1ad15SYuanhan Liu 	vq->vq_free_cnt = vq->vq_nentries;
335f4d1ad15SYuanhan Liu 	memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
336f4d1ad15SYuanhan Liu 
337f4d1ad15SYuanhan Liu 	vring_desc_init(vr->desc, size);
338f4d1ad15SYuanhan Liu 
339f4d1ad15SYuanhan Liu 	/*
340f4d1ad15SYuanhan Liu 	 * Disable device(host) interrupting guest
341f4d1ad15SYuanhan Liu 	 */
342f4d1ad15SYuanhan Liu 	virtqueue_disable_intr(vq);
343f4d1ad15SYuanhan Liu }
344f4d1ad15SYuanhan Liu 
34569c80d4eSYuanhan Liu static int
34669c80d4eSYuanhan Liu virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
3476c3169a3SBruce Richardson {
3486c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
34901ad44fdSHuawei Xie 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
35001ad44fdSHuawei Xie 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
3511e7bd238SStephen Hemminger 	unsigned int vq_size, size;
3526c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
353f24f8f9fSJianfeng Tan 	struct virtnet_rx *rxvq = NULL;
354f24f8f9fSJianfeng Tan 	struct virtnet_tx *txvq = NULL;
355f24f8f9fSJianfeng Tan 	struct virtnet_ctl *cvq = NULL;
35601ad44fdSHuawei Xie 	struct virtqueue *vq;
357905a2469SYuanhan Liu 	size_t sz_hdr_mz = 0;
35801ad44fdSHuawei Xie 	void *sw_ring = NULL;
35969c80d4eSYuanhan Liu 	int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
36001ad44fdSHuawei Xie 	int ret;
3616c3169a3SBruce Richardson 
362d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
3636c3169a3SBruce Richardson 
3646c3169a3SBruce Richardson 	/*
3656c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
3666c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
3676c3169a3SBruce Richardson 	 */
368553f4593SYuanhan Liu 	vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
36969c80d4eSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
3706c3169a3SBruce Richardson 	if (vq_size == 0) {
3710bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
3726c3169a3SBruce Richardson 		return -EINVAL;
373d78deadaSStephen Hemminger 	}
374d78deadaSStephen Hemminger 
375d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3760bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
3776c3169a3SBruce Richardson 		return -EINVAL;
378d78deadaSStephen Hemminger 	}
379d78deadaSStephen Hemminger 
38073d017ddSYuanhan Liu 	snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
38173d017ddSYuanhan Liu 		 dev->data->port_id, vtpci_queue_idx);
38201ad44fdSHuawei Xie 
383905a2469SYuanhan Liu 	size = RTE_ALIGN_CEIL(sizeof(*vq) +
3846c3169a3SBruce Richardson 				vq_size * sizeof(struct vq_desc_extra),
3856c3169a3SBruce Richardson 				RTE_CACHE_LINE_SIZE);
386905a2469SYuanhan Liu 	if (queue_type == VTNET_TQ) {
38701ad44fdSHuawei Xie 		/*
38801ad44fdSHuawei Xie 		 * For each xmit packet, allocate a virtio_net_hdr
38901ad44fdSHuawei Xie 		 * and indirect ring elements
39001ad44fdSHuawei Xie 		 */
39101ad44fdSHuawei Xie 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
39201ad44fdSHuawei Xie 	} else if (queue_type == VTNET_CQ) {
39301ad44fdSHuawei Xie 		/* Allocate a page for control vq command, data and status */
39401ad44fdSHuawei Xie 		sz_hdr_mz = PAGE_SIZE;
39501ad44fdSHuawei Xie 	}
3964166bbf6SJianfeng Tan 
39769c80d4eSYuanhan Liu 	vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
39869c80d4eSYuanhan Liu 				SOCKET_ID_ANY);
39901ad44fdSHuawei Xie 	if (vq == NULL) {
40001ad44fdSHuawei Xie 		PMD_INIT_LOG(ERR, "can not allocate vq");
401cab04612SHuawei Xie 		return -ENOMEM;
402cab04612SHuawei Xie 	}
40369c80d4eSYuanhan Liu 	hw->vqs[vtpci_queue_idx] = vq;
40469c80d4eSYuanhan Liu 
4056c3169a3SBruce Richardson 	vq->hw = hw;
4066c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
4076c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
4086c3169a3SBruce Richardson 
4096c3169a3SBruce Richardson 	/*
4106c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
4116c3169a3SBruce Richardson 	 */
4126c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
4136c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
41401ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
41501ad44fdSHuawei Xie 		     size, vq->vq_ring_size);
4166c3169a3SBruce Richardson 
41769c80d4eSYuanhan Liu 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
41869c80d4eSYuanhan Liu 					 SOCKET_ID_ANY,
41901ad44fdSHuawei Xie 					 0, VIRTIO_PCI_VRING_ALIGN);
4206c3169a3SBruce Richardson 	if (mz == NULL) {
421abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
422abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
423abf4c84bSBernard Iremonger 		if (mz == NULL) {
42401ad44fdSHuawei Xie 			ret = -ENOMEM;
42501ad44fdSHuawei Xie 			goto fail_q_alloc;
4266c3169a3SBruce Richardson 		}
427abf4c84bSBernard Iremonger 	}
4286c3169a3SBruce Richardson 
4295bdd24e4STiwei Bie 	memset(mz->addr, 0, mz->len);
43001ad44fdSHuawei Xie 
431f17ca787SThomas Monjalon 	vq->vq_ring_mem = mz->iova;
4326c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
43301ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
434f17ca787SThomas Monjalon 		     (uint64_t)mz->iova);
43501ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
43601ad44fdSHuawei Xie 		     (uint64_t)(uintptr_t)mz->addr);
4376c3169a3SBruce Richardson 
438f4d1ad15SYuanhan Liu 	virtio_init_vring(vq);
439f4d1ad15SYuanhan Liu 
44001ad44fdSHuawei Xie 	if (sz_hdr_mz) {
44173d017ddSYuanhan Liu 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
44273d017ddSYuanhan Liu 			 dev->data->port_id, vtpci_queue_idx);
44301ad44fdSHuawei Xie 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
44469c80d4eSYuanhan Liu 						     SOCKET_ID_ANY, 0,
4456dc5de3aSStephen Hemminger 						     RTE_CACHE_LINE_SIZE);
4466dc5de3aSStephen Hemminger 		if (hdr_mz == NULL) {
447abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
44801ad44fdSHuawei Xie 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
4496dc5de3aSStephen Hemminger 			if (hdr_mz == NULL) {
45001ad44fdSHuawei Xie 				ret = -ENOMEM;
45101ad44fdSHuawei Xie 				goto fail_q_alloc;
4526c3169a3SBruce Richardson 			}
453abf4c84bSBernard Iremonger 		}
45401ad44fdSHuawei Xie 	}
45501ad44fdSHuawei Xie 
45601ad44fdSHuawei Xie 	if (queue_type == VTNET_RQ) {
45701ad44fdSHuawei Xie 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
45801ad44fdSHuawei Xie 			       sizeof(vq->sw_ring[0]);
45901ad44fdSHuawei Xie 
46001ad44fdSHuawei Xie 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
46169c80d4eSYuanhan Liu 				RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
46201ad44fdSHuawei Xie 		if (!sw_ring) {
46301ad44fdSHuawei Xie 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
46401ad44fdSHuawei Xie 			ret = -ENOMEM;
46501ad44fdSHuawei Xie 			goto fail_q_alloc;
46601ad44fdSHuawei Xie 		}
46701ad44fdSHuawei Xie 
46801ad44fdSHuawei Xie 		vq->sw_ring = sw_ring;
469905a2469SYuanhan Liu 		rxvq = &vq->rxq;
47001ad44fdSHuawei Xie 		rxvq->vq = vq;
47101ad44fdSHuawei Xie 		rxvq->port_id = dev->data->port_id;
47201ad44fdSHuawei Xie 		rxvq->mz = mz;
47301ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
474905a2469SYuanhan Liu 		txvq = &vq->txq;
47501ad44fdSHuawei Xie 		txvq->vq = vq;
47601ad44fdSHuawei Xie 		txvq->port_id = dev->data->port_id;
47701ad44fdSHuawei Xie 		txvq->mz = mz;
47801ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mz = hdr_mz;
479f17ca787SThomas Monjalon 		txvq->virtio_net_hdr_mem = hdr_mz->iova;
480f24f8f9fSJianfeng Tan 	} else if (queue_type == VTNET_CQ) {
481905a2469SYuanhan Liu 		cvq = &vq->cq;
482f24f8f9fSJianfeng Tan 		cvq->vq = vq;
483f24f8f9fSJianfeng Tan 		cvq->mz = mz;
484f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mz = hdr_mz;
485f17ca787SThomas Monjalon 		cvq->virtio_net_hdr_mem = hdr_mz->iova;
486f24f8f9fSJianfeng Tan 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
48769c80d4eSYuanhan Liu 
48869c80d4eSYuanhan Liu 		hw->cvq = cvq;
489f24f8f9fSJianfeng Tan 	}
490f24f8f9fSJianfeng Tan 
4916a7c0dfcSJan Blunck 	/* For virtio_user case (that is when hw->dev is NULL), we use
492f24f8f9fSJianfeng Tan 	 * virtual address. And we need properly set _offset_, please see
49325f80d10SOlivier Matz 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
494f24f8f9fSJianfeng Tan 	 */
4959470427cSYuanhan Liu 	if (!hw->virtio_user_dev)
496*455da545SSantosh Shukla 		vq->offset = offsetof(struct rte_mbuf, buf_iova);
497f24f8f9fSJianfeng Tan 	else {
498f24f8f9fSJianfeng Tan 		vq->vq_ring_mem = (uintptr_t)mz->addr;
499f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
500f24f8f9fSJianfeng Tan 		if (queue_type == VTNET_TQ)
501f24f8f9fSJianfeng Tan 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
502f24f8f9fSJianfeng Tan 		else if (queue_type == VTNET_CQ)
503f24f8f9fSJianfeng Tan 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
504f24f8f9fSJianfeng Tan 	}
505f24f8f9fSJianfeng Tan 
506f24f8f9fSJianfeng Tan 	if (queue_type == VTNET_TQ) {
507f24f8f9fSJianfeng Tan 		struct virtio_tx_region *txr;
508f24f8f9fSJianfeng Tan 		unsigned int i;
509f24f8f9fSJianfeng Tan 
5106dc5de3aSStephen Hemminger 		txr = hdr_mz->addr;
5116dc5de3aSStephen Hemminger 		memset(txr, 0, vq_size * sizeof(*txr));
5126dc5de3aSStephen Hemminger 		for (i = 0; i < vq_size; i++) {
5136dc5de3aSStephen Hemminger 			struct vring_desc *start_dp = txr[i].tx_indir;
5146dc5de3aSStephen Hemminger 
5156dc5de3aSStephen Hemminger 			vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
5166dc5de3aSStephen Hemminger 
5176dc5de3aSStephen Hemminger 			/* first indirect descriptor is always the tx header */
51801ad44fdSHuawei Xie 			start_dp->addr = txvq->virtio_net_hdr_mem
5196dc5de3aSStephen Hemminger 				+ i * sizeof(*txr)
5206dc5de3aSStephen Hemminger 				+ offsetof(struct virtio_tx_region, tx_hdr);
5216dc5de3aSStephen Hemminger 
52201ad44fdSHuawei Xie 			start_dp->len = hw->vtnet_hdr_size;
5236dc5de3aSStephen Hemminger 			start_dp->flags = VRING_DESC_F_NEXT;
5246dc5de3aSStephen Hemminger 		}
5256c3169a3SBruce Richardson 	}
5266c3169a3SBruce Richardson 
527553f4593SYuanhan Liu 	if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
528595454c5SJianfeng Tan 		PMD_INIT_LOG(ERR, "setup_queue failed");
529595454c5SJianfeng Tan 		return -EINVAL;
530595454c5SJianfeng Tan 	}
531595454c5SJianfeng Tan 
5326c3169a3SBruce Richardson 	return 0;
53301ad44fdSHuawei Xie 
53401ad44fdSHuawei Xie fail_q_alloc:
53501ad44fdSHuawei Xie 	rte_free(sw_ring);
53601ad44fdSHuawei Xie 	rte_memzone_free(hdr_mz);
53701ad44fdSHuawei Xie 	rte_memzone_free(mz);
53801ad44fdSHuawei Xie 	rte_free(vq);
53901ad44fdSHuawei Xie 
54001ad44fdSHuawei Xie 	return ret;
5416c3169a3SBruce Richardson }
5426c3169a3SBruce Richardson 
54369c80d4eSYuanhan Liu static void
54469c80d4eSYuanhan Liu virtio_free_queues(struct virtio_hw *hw)
5456c3169a3SBruce Richardson {
54669c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
54769c80d4eSYuanhan Liu 	struct virtqueue *vq;
54869c80d4eSYuanhan Liu 	int queue_type;
54969c80d4eSYuanhan Liu 	uint16_t i;
5506c3169a3SBruce Richardson 
5510e78cfddSHuanle Han 	if (hw->vqs == NULL)
5520e78cfddSHuanle Han 		return;
5530e78cfddSHuanle Han 
55469c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
55569c80d4eSYuanhan Liu 		vq = hw->vqs[i];
55669c80d4eSYuanhan Liu 		if (!vq)
55769c80d4eSYuanhan Liu 			continue;
55869c80d4eSYuanhan Liu 
55969c80d4eSYuanhan Liu 		queue_type = virtio_get_queue_type(hw, i);
56069c80d4eSYuanhan Liu 		if (queue_type == VTNET_RQ) {
56169c80d4eSYuanhan Liu 			rte_free(vq->sw_ring);
56269c80d4eSYuanhan Liu 			rte_memzone_free(vq->rxq.mz);
56369c80d4eSYuanhan Liu 		} else if (queue_type == VTNET_TQ) {
56469c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.mz);
56569c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.virtio_net_hdr_mz);
56669c80d4eSYuanhan Liu 		} else {
56769c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.mz);
56869c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.virtio_net_hdr_mz);
56969c80d4eSYuanhan Liu 		}
57069c80d4eSYuanhan Liu 
57169c80d4eSYuanhan Liu 		rte_free(vq);
5720e78cfddSHuanle Han 		hw->vqs[i] = NULL;
57369c80d4eSYuanhan Liu 	}
57469c80d4eSYuanhan Liu 
57569c80d4eSYuanhan Liu 	rte_free(hw->vqs);
5760e78cfddSHuanle Han 	hw->vqs = NULL;
57769c80d4eSYuanhan Liu }
57869c80d4eSYuanhan Liu 
57969c80d4eSYuanhan Liu static int
58069c80d4eSYuanhan Liu virtio_alloc_queues(struct rte_eth_dev *dev)
58169c80d4eSYuanhan Liu {
58269c80d4eSYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
58369c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
58469c80d4eSYuanhan Liu 	uint16_t i;
58569c80d4eSYuanhan Liu 	int ret;
58669c80d4eSYuanhan Liu 
58769c80d4eSYuanhan Liu 	hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
58869c80d4eSYuanhan Liu 	if (!hw->vqs) {
58969c80d4eSYuanhan Liu 		PMD_INIT_LOG(ERR, "failed to allocate vqs");
59069c80d4eSYuanhan Liu 		return -ENOMEM;
59169c80d4eSYuanhan Liu 	}
59269c80d4eSYuanhan Liu 
59369c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
59469c80d4eSYuanhan Liu 		ret = virtio_init_queue(dev, i);
5956c3169a3SBruce Richardson 		if (ret < 0) {
59669c80d4eSYuanhan Liu 			virtio_free_queues(hw);
5976c3169a3SBruce Richardson 			return ret;
5986c3169a3SBruce Richardson 		}
5996c3169a3SBruce Richardson 	}
6006c3169a3SBruce Richardson 
60169c80d4eSYuanhan Liu 	return 0;
602941d64b5SBernard Iremonger }
603941d64b5SBernard Iremonger 
6049ebdeefeSJianfeng Tan static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
6059ebdeefeSJianfeng Tan 
606941d64b5SBernard Iremonger static void
6076c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
6086c3169a3SBruce Richardson {
6096c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6109ebdeefeSJianfeng Tan 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
6116c3169a3SBruce Richardson 
6126c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
6136c3169a3SBruce Richardson 
6146c3169a3SBruce Richardson 	/* reset the NIC */
61562a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
616981e61f5SJianfeng Tan 		VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
6179ebdeefeSJianfeng Tan 	if (intr_conf->rxq)
6189ebdeefeSJianfeng Tan 		virtio_queues_unbind_intr(dev);
6199ebdeefeSJianfeng Tan 
6209ebdeefeSJianfeng Tan 	if (intr_conf->lsc || intr_conf->rxq) {
6219ebdeefeSJianfeng Tan 		rte_intr_disable(dev->intr_handle);
6229ebdeefeSJianfeng Tan 		rte_intr_efd_disable(dev->intr_handle);
6239ebdeefeSJianfeng Tan 		rte_free(dev->intr_handle->intr_vec);
6249ebdeefeSJianfeng Tan 		dev->intr_handle->intr_vec = NULL;
6259ebdeefeSJianfeng Tan 	}
6269ebdeefeSJianfeng Tan 
6276c3169a3SBruce Richardson 	vtpci_reset(hw);
6286c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
62969c80d4eSYuanhan Liu 	virtio_free_queues(hw);
6306c3169a3SBruce Richardson }
6316c3169a3SBruce Richardson 
6326c3169a3SBruce Richardson static void
6336c3169a3SBruce Richardson virtio_dev_promiscuous_enable(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] = 1;
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 enable promisc");
6536c3169a3SBruce Richardson }
6546c3169a3SBruce Richardson 
6556c3169a3SBruce Richardson static void
6566c3169a3SBruce Richardson virtio_dev_promiscuous_disable(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_PROMISC;
6706c3169a3SBruce Richardson 	ctrl.data[0] = 0;
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 disable promisc");
6766c3169a3SBruce Richardson }
6776c3169a3SBruce Richardson 
6786c3169a3SBruce Richardson static void
6796c3169a3SBruce Richardson virtio_dev_allmulticast_enable(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] = 1;
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 enable allmulticast");
6996c3169a3SBruce Richardson }
7006c3169a3SBruce Richardson 
7016c3169a3SBruce Richardson static void
7026c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
7036c3169a3SBruce Richardson {
7046c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
7056c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
7066c3169a3SBruce Richardson 	int dlen[1];
7076c3169a3SBruce Richardson 	int ret;
7086c3169a3SBruce Richardson 
709e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
710f2462150SFerruh Yigit 		PMD_INIT_LOG(INFO, "host does not support rx control");
711e9e414a4SStephen Hemminger 		return;
712e9e414a4SStephen Hemminger 	}
713e9e414a4SStephen Hemminger 
7146c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
7156c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
7166c3169a3SBruce Richardson 	ctrl.data[0] = 0;
7176c3169a3SBruce Richardson 	dlen[0] = 1;
7186c3169a3SBruce Richardson 
7196c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
7206c3169a3SBruce Richardson 	if (ret)
7216c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
7226c3169a3SBruce Richardson }
7236c3169a3SBruce Richardson 
7244ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
7254ec2424aSSouvik Dey static int
7264ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
7274ec2424aSSouvik Dey {
7284ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
7294ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
7304ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
7314ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
73249d26d9eSMaxime Coquelin 	uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
7334ec2424aSSouvik Dey 
73449d26d9eSMaxime Coquelin 	max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
73549d26d9eSMaxime Coquelin 
73649d26d9eSMaxime Coquelin 	if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
737f2462150SFerruh Yigit 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
73849d26d9eSMaxime Coquelin 			ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
7394ec2424aSSouvik Dey 		return -EINVAL;
7404ec2424aSSouvik Dey 	}
7414ec2424aSSouvik Dey 	return 0;
7424ec2424aSSouvik Dey }
7434ec2424aSSouvik Dey 
744c056be23SJianfeng Tan static int
745c056be23SJianfeng Tan virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
746c056be23SJianfeng Tan {
747c056be23SJianfeng Tan 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
748c056be23SJianfeng Tan 	struct virtqueue *vq = rxvq->vq;
749c056be23SJianfeng Tan 
750c056be23SJianfeng Tan 	virtqueue_enable_intr(vq);
751c056be23SJianfeng Tan 	return 0;
752c056be23SJianfeng Tan }
753c056be23SJianfeng Tan 
754c056be23SJianfeng Tan static int
755c056be23SJianfeng Tan virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
756c056be23SJianfeng Tan {
757c056be23SJianfeng Tan 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
758c056be23SJianfeng Tan 	struct virtqueue *vq = rxvq->vq;
759c056be23SJianfeng Tan 
760c056be23SJianfeng Tan 	virtqueue_disable_intr(vq);
761c056be23SJianfeng Tan 	return 0;
762c056be23SJianfeng Tan }
763c056be23SJianfeng Tan 
7646c3169a3SBruce Richardson /*
7656c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
7666c3169a3SBruce Richardson  */
7676c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
7686c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
7696c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
7706c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
7716c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
7726c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
7736c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
7746c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
7756c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
7764ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
7776c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
7786c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
77976d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
780baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
7816c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
78276d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
7836c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
784289ba0c0SDavid Harton 	.vlan_offload_set        = virtio_dev_vlan_offload_set,
7856c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
786c056be23SJianfeng Tan 	.rx_queue_intr_enable    = virtio_dev_rx_queue_intr_enable,
787c056be23SJianfeng Tan 	.rx_queue_intr_disable   = virtio_dev_rx_queue_intr_disable,
78869c80d4eSYuanhan Liu 	.rx_queue_release        = virtio_dev_queue_release,
789b0caba1aSJianfeng Tan 	.rx_descriptor_done      = virtio_dev_rx_queue_done,
7906c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
79169c80d4eSYuanhan Liu 	.tx_queue_release        = virtio_dev_queue_release,
7926c3169a3SBruce Richardson 	/* collect stats per queue */
7936c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7946c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7956c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7966c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7976c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7986c3169a3SBruce Richardson };
7996c3169a3SBruce Richardson 
8006c3169a3SBruce Richardson static inline int
8016c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
8026c3169a3SBruce Richardson 				struct rte_eth_link *link)
8036c3169a3SBruce Richardson {
8046c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
8056c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
8066c3169a3SBruce Richardson 
8076c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
8086c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
8096c3169a3SBruce Richardson 		return -1;
8106c3169a3SBruce Richardson 
8116c3169a3SBruce Richardson 	return 0;
8126c3169a3SBruce Richardson }
8136c3169a3SBruce Richardson 
8146c3169a3SBruce Richardson /**
8156c3169a3SBruce Richardson  * Atomically writes the link status information into global
8166c3169a3SBruce Richardson  * structure rte_eth_dev.
8176c3169a3SBruce Richardson  *
8186c3169a3SBruce Richardson  * @param dev
8196c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
8206c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
8216c3169a3SBruce Richardson  *
8226c3169a3SBruce Richardson  * @return
8236c3169a3SBruce Richardson  *   - On success, zero.
8246c3169a3SBruce Richardson  *   - On failure, negative value.
8256c3169a3SBruce Richardson  */
8266c3169a3SBruce Richardson static inline int
8276c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
8286c3169a3SBruce Richardson 		struct rte_eth_link *link)
8296c3169a3SBruce Richardson {
8306c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
8316c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
8326c3169a3SBruce Richardson 
8336c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
8346c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
8356c3169a3SBruce Richardson 		return -1;
8366c3169a3SBruce Richardson 
8376c3169a3SBruce Richardson 	return 0;
8386c3169a3SBruce Richardson }
8396c3169a3SBruce Richardson 
8406c3169a3SBruce Richardson static void
84176d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
8426c3169a3SBruce Richardson {
8436c3169a3SBruce Richardson 	unsigned i;
8446c3169a3SBruce Richardson 
8456c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
84601ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
8476c3169a3SBruce Richardson 		if (txvq == NULL)
8486c3169a3SBruce Richardson 			continue;
8496c3169a3SBruce Richardson 
85001ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
85101ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
85201ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
8536c3169a3SBruce Richardson 
8546c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
85501ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
85601ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
8576c3169a3SBruce Richardson 		}
8586c3169a3SBruce Richardson 	}
8596c3169a3SBruce Richardson 
8606c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
86101ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
8626c3169a3SBruce Richardson 		if (rxvq == NULL)
8636c3169a3SBruce Richardson 			continue;
8646c3169a3SBruce Richardson 
86501ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
86601ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
86701ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
8686c3169a3SBruce Richardson 
8696c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
87001ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
87101ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
8726c3169a3SBruce Richardson 		}
8736c3169a3SBruce Richardson 	}
8746c3169a3SBruce Richardson 
8756c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
8766c3169a3SBruce Richardson }
8776c3169a3SBruce Richardson 
878baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
879baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
880baf91c39SRemy Horton 				       __rte_unused unsigned limit)
881baf91c39SRemy Horton {
882baf91c39SRemy Horton 	unsigned i;
883baf91c39SRemy Horton 	unsigned count = 0;
884baf91c39SRemy Horton 	unsigned t;
885baf91c39SRemy Horton 
88601ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
88701ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
888baf91c39SRemy Horton 
8897e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
890baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
891baf91c39SRemy Horton 
892baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
893baf91c39SRemy Horton 			struct virtqueue *rxvq = dev->data->rx_queues[i];
894baf91c39SRemy Horton 			if (rxvq == NULL)
895baf91c39SRemy Horton 				continue;
89601ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
897baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
898baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
899baf91c39SRemy Horton 					"rx_q%u_%s", i,
90001ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
901baf91c39SRemy Horton 				count++;
902baf91c39SRemy Horton 			}
903baf91c39SRemy Horton 		}
904baf91c39SRemy Horton 
905baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
906baf91c39SRemy Horton 			struct virtqueue *txvq = dev->data->tx_queues[i];
907baf91c39SRemy Horton 			if (txvq == NULL)
908baf91c39SRemy Horton 				continue;
90901ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
910baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
911baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
912baf91c39SRemy Horton 					"tx_q%u_%s", i,
91301ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
914baf91c39SRemy Horton 				count++;
915baf91c39SRemy Horton 			}
916baf91c39SRemy Horton 		}
917baf91c39SRemy Horton 		return count;
918baf91c39SRemy Horton 	}
919baf91c39SRemy Horton 	return nstats;
920baf91c39SRemy Horton }
921baf91c39SRemy Horton 
92276d4c652SHarry van Haaren static int
923e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
92476d4c652SHarry van Haaren 		      unsigned n)
92576d4c652SHarry van Haaren {
92676d4c652SHarry van Haaren 	unsigned i;
92776d4c652SHarry van Haaren 	unsigned count = 0;
92876d4c652SHarry van Haaren 
92901ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
93001ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
93176d4c652SHarry van Haaren 
93276d4c652SHarry van Haaren 	if (n < nstats)
93376d4c652SHarry van Haaren 		return nstats;
93476d4c652SHarry van Haaren 
93576d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
93601ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
93776d4c652SHarry van Haaren 
93876d4c652SHarry van Haaren 		if (rxvq == NULL)
93976d4c652SHarry van Haaren 			continue;
94076d4c652SHarry van Haaren 
94176d4c652SHarry van Haaren 		unsigned t;
94276d4c652SHarry van Haaren 
94301ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
94476d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
94501ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
946513c78aeSOlivier Matz 			xstats[count].id = count;
94776d4c652SHarry van Haaren 			count++;
94876d4c652SHarry van Haaren 		}
94976d4c652SHarry van Haaren 	}
95076d4c652SHarry van Haaren 
95176d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
95201ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
95376d4c652SHarry van Haaren 
95476d4c652SHarry van Haaren 		if (txvq == NULL)
95576d4c652SHarry van Haaren 			continue;
95676d4c652SHarry van Haaren 
95776d4c652SHarry van Haaren 		unsigned t;
95876d4c652SHarry van Haaren 
95901ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
96076d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
96101ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
962513c78aeSOlivier Matz 			xstats[count].id = count;
96376d4c652SHarry van Haaren 			count++;
96476d4c652SHarry van Haaren 		}
96576d4c652SHarry van Haaren 	}
96676d4c652SHarry van Haaren 
96776d4c652SHarry van Haaren 	return count;
96876d4c652SHarry van Haaren }
96976d4c652SHarry van Haaren 
970d5b0924bSMatan Azrad static int
97176d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
97276d4c652SHarry van Haaren {
97376d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
974d5b0924bSMatan Azrad 
975d5b0924bSMatan Azrad 	return 0;
97676d4c652SHarry van Haaren }
97776d4c652SHarry van Haaren 
9786c3169a3SBruce Richardson static void
9796c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
9806c3169a3SBruce Richardson {
9816c3169a3SBruce Richardson 	unsigned int i;
9826c3169a3SBruce Richardson 
9836c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
98401ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
9856c3169a3SBruce Richardson 		if (txvq == NULL)
9866c3169a3SBruce Richardson 			continue;
9876c3169a3SBruce Richardson 
98801ad44fdSHuawei Xie 		txvq->stats.packets = 0;
98901ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
99001ad44fdSHuawei Xie 		txvq->stats.errors = 0;
99101ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
99201ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
99301ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
99401ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
9956c3169a3SBruce Richardson 	}
9966c3169a3SBruce Richardson 
9976c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
99801ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9996c3169a3SBruce Richardson 		if (rxvq == NULL)
10006c3169a3SBruce Richardson 			continue;
10016c3169a3SBruce Richardson 
100201ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
100301ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
100401ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
100501ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
100601ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
100701ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
100801ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
10096c3169a3SBruce Richardson 	}
10106c3169a3SBruce Richardson }
10116c3169a3SBruce Richardson 
10126c3169a3SBruce Richardson static void
10136c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
10146c3169a3SBruce Richardson {
10156c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
10166c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
10176c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
10186c3169a3SBruce Richardson }
10196c3169a3SBruce Richardson 
10206c3169a3SBruce Richardson static void
10216c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
10226c3169a3SBruce Richardson {
10236c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
10246c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
10256c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
10266c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
10276c3169a3SBruce Richardson 	} else {
10286c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
10296c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10306c3169a3SBruce Richardson 	}
10316c3169a3SBruce Richardson }
10326c3169a3SBruce Richardson 
10336d01e580SWei Dai static int
10346c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
10356c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
10366c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
10376c3169a3SBruce Richardson {
10386c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10396c3169a3SBruce Richardson 	int err, len[2];
10406c3169a3SBruce Richardson 
104127046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1042e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
10436d01e580SWei Dai 		return -1;
104427046236SStephen Hemminger 	}
104527046236SStephen Hemminger 
10466c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10476c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
10486c3169a3SBruce Richardson 
10496c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
10506c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
10516c3169a3SBruce Richardson 
10526c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
10536c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
10546c3169a3SBruce Richardson 
10556c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
10566c3169a3SBruce Richardson 	if (err != 0)
10576c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
10586d01e580SWei Dai 	return err;
10596c3169a3SBruce Richardson }
10606c3169a3SBruce Richardson 
10616d01e580SWei Dai static int
10626c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
10636c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
10646c3169a3SBruce Richardson {
10656c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10666c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
10676c3169a3SBruce Richardson 	unsigned int i;
10686c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10696c3169a3SBruce Richardson 
10706c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10716c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10726d01e580SWei Dai 		return -EINVAL;
10736c3169a3SBruce Richardson 	}
10746c3169a3SBruce Richardson 
10756c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10766c3169a3SBruce Richardson 	uc->entries = 0;
10776c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10786c3169a3SBruce Richardson 	mc->entries = 0;
10796c3169a3SBruce Richardson 
10806c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10816c3169a3SBruce Richardson 		const struct ether_addr *addr
10826c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
10836c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
10846c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
10856c3169a3SBruce Richardson 
10866c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
10876c3169a3SBruce Richardson 	}
10886c3169a3SBruce Richardson 
10896d01e580SWei Dai 	return virtio_mac_table_set(hw, uc, mc);
10906c3169a3SBruce Richardson }
10916c3169a3SBruce Richardson 
10926c3169a3SBruce Richardson static void
10936c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
10946c3169a3SBruce Richardson {
10956c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10966c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
10976c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10986c3169a3SBruce Richardson 	unsigned int i;
10996c3169a3SBruce Richardson 
11006c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
11016c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
11026c3169a3SBruce Richardson 		return;
11036c3169a3SBruce Richardson 	}
11046c3169a3SBruce Richardson 
11056c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
11066c3169a3SBruce Richardson 	uc->entries = 0;
11076c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
11086c3169a3SBruce Richardson 	mc->entries = 0;
11096c3169a3SBruce Richardson 
11106c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
11116c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
11126c3169a3SBruce Richardson 
11136c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
11146c3169a3SBruce Richardson 			continue;
11156c3169a3SBruce Richardson 
11166c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
11176c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
11186c3169a3SBruce Richardson 	}
11196c3169a3SBruce Richardson 
11206c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
11216c3169a3SBruce Richardson }
11226c3169a3SBruce Richardson 
11236c3169a3SBruce Richardson static void
11246c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
11256c3169a3SBruce Richardson {
11266c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11276c3169a3SBruce Richardson 
11286c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
11296c3169a3SBruce Richardson 
11306c3169a3SBruce Richardson 	/* Use atomic update if available */
11316c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
11326c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
11336c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
11346c3169a3SBruce Richardson 
11356c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
11366c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
11376c3169a3SBruce Richardson 
11386c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
11396c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
11406c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
11416c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
11426c3169a3SBruce Richardson }
11436c3169a3SBruce Richardson 
11446c3169a3SBruce Richardson static int
11456c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
11466c3169a3SBruce Richardson {
11476c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11486c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
11496c3169a3SBruce Richardson 	int len;
11506c3169a3SBruce Richardson 
11516c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
11526c3169a3SBruce Richardson 		return -ENOTSUP;
11536c3169a3SBruce Richardson 
11546c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
11556c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
11566c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
11576c3169a3SBruce Richardson 	len = sizeof(vlan_id);
11586c3169a3SBruce Richardson 
11596c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
11606c3169a3SBruce Richardson }
11616c3169a3SBruce Richardson 
11626ba1f63bSYuanhan Liu static int
116360e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
11646c3169a3SBruce Richardson {
11653891f233SYuanhan Liu 	uint64_t host_features;
11666c3169a3SBruce Richardson 
11676c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
11683891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
116960e6f470SOlivier Matz 		req_features);
11706c3169a3SBruce Richardson 
11716c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1172553f4593SYuanhan Liu 	host_features = VTPCI_OPS(hw)->get_features(hw);
11733891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
11746c3169a3SBruce Richardson 		host_features);
11756c3169a3SBruce Richardson 
117649d26d9eSMaxime Coquelin 	/* If supported, ensure MTU value is valid before acknowledging it. */
117749d26d9eSMaxime Coquelin 	if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
117849d26d9eSMaxime Coquelin 		struct virtio_net_config config;
117949d26d9eSMaxime Coquelin 
118049d26d9eSMaxime Coquelin 		vtpci_read_dev_config(hw,
118149d26d9eSMaxime Coquelin 			offsetof(struct virtio_net_config, mtu),
118249d26d9eSMaxime Coquelin 			&config.mtu, sizeof(config.mtu));
118349d26d9eSMaxime Coquelin 
118449d26d9eSMaxime Coquelin 		if (config.mtu < ETHER_MIN_MTU)
118549d26d9eSMaxime Coquelin 			req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
118649d26d9eSMaxime Coquelin 	}
118749d26d9eSMaxime Coquelin 
11886c3169a3SBruce Richardson 	/*
11896c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
11906c3169a3SBruce Richardson 	 * guest feature bits.
11916c3169a3SBruce Richardson 	 */
119260e6f470SOlivier Matz 	hw->guest_features = req_features;
11936c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
11943891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
11956c3169a3SBruce Richardson 		hw->guest_features);
11966ba1f63bSYuanhan Liu 
11976ba1f63bSYuanhan Liu 	if (hw->modern) {
11986ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
11996ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
12006ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
12016ba1f63bSYuanhan Liu 			return -1;
12026ba1f63bSYuanhan Liu 		}
12036ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
12046ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
12056ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
12066ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
12076ba1f63bSYuanhan Liu 			return -1;
12086ba1f63bSYuanhan Liu 		}
12096ba1f63bSYuanhan Liu 	}
12106ba1f63bSYuanhan Liu 
121160e6f470SOlivier Matz 	hw->req_guest_features = req_features;
121260e6f470SOlivier Matz 
12136ba1f63bSYuanhan Liu 	return 0;
12146c3169a3SBruce Richardson }
12156c3169a3SBruce Richardson 
12166c3169a3SBruce Richardson /*
12176c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
12186c3169a3SBruce Richardson  * if link state changed.
12196c3169a3SBruce Richardson  */
1220ef53b603SJianfeng Tan void
1221c23a1a30SQi Zhang virtio_interrupt_handler(void *param)
12226c3169a3SBruce Richardson {
12236c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
12246c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
12256c3169a3SBruce Richardson 	uint8_t isr;
12266c3169a3SBruce Richardson 
12276c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
12286c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
12296c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
12306c3169a3SBruce Richardson 
1231c23a1a30SQi Zhang 	if (rte_intr_enable(dev->intr_handle) < 0)
12326c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
12336c3169a3SBruce Richardson 
12346c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
12356c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
12366c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1237d6af1a13SBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC,
1238d6af1a13SBernard Iremonger 						      NULL, NULL);
12396c3169a3SBruce Richardson 	}
12406c3169a3SBruce Richardson 
12416c3169a3SBruce Richardson }
12426c3169a3SBruce Richardson 
12434819eae8SOlivier Matz /* set rx and tx handlers according to what is supported */
12446c3169a3SBruce Richardson static void
12454819eae8SOlivier Matz set_rxtx_funcs(struct rte_eth_dev *eth_dev)
12466c3169a3SBruce Richardson {
12476c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
12484819eae8SOlivier Matz 
124909649363SOlivier Matz 	if (hw->use_simple_rx) {
12504819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
12514819eae8SOlivier Matz 			eth_dev->data->port_id);
12524819eae8SOlivier Matz 		eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
12534819eae8SOlivier Matz 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
12544819eae8SOlivier Matz 		PMD_INIT_LOG(INFO,
12554819eae8SOlivier Matz 			"virtio: using mergeable buffer Rx path on port %u",
12564819eae8SOlivier Matz 			eth_dev->data->port_id);
12576c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
12584819eae8SOlivier Matz 	} else {
12594819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
12604819eae8SOlivier Matz 			eth_dev->data->port_id);
12616c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
12626c3169a3SBruce Richardson 	}
12636c3169a3SBruce Richardson 
126409649363SOlivier Matz 	if (hw->use_simple_tx) {
12654819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u",
12664819eae8SOlivier Matz 			eth_dev->data->port_id);
12674819eae8SOlivier Matz 		eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
12684819eae8SOlivier Matz 	} else {
12694819eae8SOlivier Matz 		PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
12704819eae8SOlivier Matz 			eth_dev->data->port_id);
12714819eae8SOlivier Matz 		eth_dev->tx_pkt_burst = virtio_xmit_pkts;
12724819eae8SOlivier Matz 	}
12734819eae8SOlivier Matz }
12744819eae8SOlivier Matz 
127526b683b4SJianfeng Tan /* Only support 1:1 queue/interrupt mapping so far.
127626b683b4SJianfeng Tan  * TODO: support n:1 queue/interrupt mapping when there are limited number of
127726b683b4SJianfeng Tan  * interrupt vectors (<N+1).
127826b683b4SJianfeng Tan  */
127926b683b4SJianfeng Tan static int
128026b683b4SJianfeng Tan virtio_queues_bind_intr(struct rte_eth_dev *dev)
128126b683b4SJianfeng Tan {
128226b683b4SJianfeng Tan 	uint32_t i;
128326b683b4SJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
128426b683b4SJianfeng Tan 
1285f2462150SFerruh Yigit 	PMD_INIT_LOG(INFO, "queue/interrupt binding");
128626b683b4SJianfeng Tan 	for (i = 0; i < dev->data->nb_rx_queues; ++i) {
128726b683b4SJianfeng Tan 		dev->intr_handle->intr_vec[i] = i + 1;
128826b683b4SJianfeng Tan 		if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
128926b683b4SJianfeng Tan 						 VIRTIO_MSI_NO_VECTOR) {
129026b683b4SJianfeng Tan 			PMD_DRV_LOG(ERR, "failed to set queue vector");
129126b683b4SJianfeng Tan 			return -EBUSY;
129226b683b4SJianfeng Tan 		}
129326b683b4SJianfeng Tan 	}
129426b683b4SJianfeng Tan 
129526b683b4SJianfeng Tan 	return 0;
129626b683b4SJianfeng Tan }
129726b683b4SJianfeng Tan 
12989ebdeefeSJianfeng Tan static void
12999ebdeefeSJianfeng Tan virtio_queues_unbind_intr(struct rte_eth_dev *dev)
13009ebdeefeSJianfeng Tan {
13019ebdeefeSJianfeng Tan 	uint32_t i;
13029ebdeefeSJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
13039ebdeefeSJianfeng Tan 
1304f2462150SFerruh Yigit 	PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
13059ebdeefeSJianfeng Tan 	for (i = 0; i < dev->data->nb_rx_queues; ++i)
13069ebdeefeSJianfeng Tan 		VTPCI_OPS(hw)->set_queue_irq(hw,
13079ebdeefeSJianfeng Tan 					     hw->vqs[i * VTNET_CQ],
13089ebdeefeSJianfeng Tan 					     VIRTIO_MSI_NO_VECTOR);
13099ebdeefeSJianfeng Tan }
13109ebdeefeSJianfeng Tan 
131126b683b4SJianfeng Tan static int
131226b683b4SJianfeng Tan virtio_configure_intr(struct rte_eth_dev *dev)
131326b683b4SJianfeng Tan {
131426b683b4SJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
131526b683b4SJianfeng Tan 
131626b683b4SJianfeng Tan 	if (!rte_intr_cap_multiple(dev->intr_handle)) {
131726b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
131826b683b4SJianfeng Tan 		return -ENOTSUP;
131926b683b4SJianfeng Tan 	}
132026b683b4SJianfeng Tan 
132126b683b4SJianfeng Tan 	if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
132226b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Fail to create eventfd");
132326b683b4SJianfeng Tan 		return -1;
132426b683b4SJianfeng Tan 	}
132526b683b4SJianfeng Tan 
132626b683b4SJianfeng Tan 	if (!dev->intr_handle->intr_vec) {
132726b683b4SJianfeng Tan 		dev->intr_handle->intr_vec =
132826b683b4SJianfeng Tan 			rte_zmalloc("intr_vec",
132926b683b4SJianfeng Tan 				    hw->max_queue_pairs * sizeof(int), 0);
133026b683b4SJianfeng Tan 		if (!dev->intr_handle->intr_vec) {
133126b683b4SJianfeng Tan 			PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
133226b683b4SJianfeng Tan 				     hw->max_queue_pairs);
133326b683b4SJianfeng Tan 			return -ENOMEM;
133426b683b4SJianfeng Tan 		}
133526b683b4SJianfeng Tan 	}
133626b683b4SJianfeng Tan 
133726b683b4SJianfeng Tan 	/* Re-register callback to update max_intr */
133826b683b4SJianfeng Tan 	rte_intr_callback_unregister(dev->intr_handle,
133926b683b4SJianfeng Tan 				     virtio_interrupt_handler,
134026b683b4SJianfeng Tan 				     dev);
134126b683b4SJianfeng Tan 	rte_intr_callback_register(dev->intr_handle,
134226b683b4SJianfeng Tan 				   virtio_interrupt_handler,
134326b683b4SJianfeng Tan 				   dev);
134426b683b4SJianfeng Tan 
134526b683b4SJianfeng Tan 	/* DO NOT try to remove this! This function will enable msix, or QEMU
134626b683b4SJianfeng Tan 	 * will encounter SIGSEGV when DRIVER_OK is sent.
134726b683b4SJianfeng Tan 	 * And for legacy devices, this should be done before queue/vec binding
134826b683b4SJianfeng Tan 	 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
134926b683b4SJianfeng Tan 	 * (22) will be ignored.
135026b683b4SJianfeng Tan 	 */
135126b683b4SJianfeng Tan 	if (rte_intr_enable(dev->intr_handle) < 0) {
135226b683b4SJianfeng Tan 		PMD_DRV_LOG(ERR, "interrupt enable failed");
135326b683b4SJianfeng Tan 		return -1;
135426b683b4SJianfeng Tan 	}
135526b683b4SJianfeng Tan 
135626b683b4SJianfeng Tan 	if (virtio_queues_bind_intr(dev) < 0) {
135726b683b4SJianfeng Tan 		PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
135826b683b4SJianfeng Tan 		return -1;
135926b683b4SJianfeng Tan 	}
136026b683b4SJianfeng Tan 
136126b683b4SJianfeng Tan 	return 0;
136226b683b4SJianfeng Tan }
136326b683b4SJianfeng Tan 
136460e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1365198ab336SOlivier Matz static int
136660e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
13676c3169a3SBruce Richardson {
13686c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
13696c3169a3SBruce Richardson 	struct virtio_net_config *config;
13706c3169a3SBruce Richardson 	struct virtio_net_config local_config;
13719470427cSYuanhan Liu 	struct rte_pci_device *pci_dev = NULL;
137269c80d4eSYuanhan Liu 	int ret;
13736c3169a3SBruce Richardson 
13746c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
13756c3169a3SBruce Richardson 	vtpci_reset(hw);
13766c3169a3SBruce Richardson 
13776c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
13786c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
13796c3169a3SBruce Richardson 
13806c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
13816c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
138260e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
13836ba1f63bSYuanhan Liu 		return -1;
13846c3169a3SBruce Richardson 
13852c2201fbSJianfeng Tan 	if (!hw->virtio_user_dev) {
1386c0802544SFerruh Yigit 		pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1387f229eb41SJianfeng Tan 		rte_eth_copy_pci_info(eth_dev, pci_dev);
1388f229eb41SJianfeng Tan 	}
1389f229eb41SJianfeng Tan 
1390a5ed8448SMatt Peters 	/* If host does not support both status and MSI-X then disable LSC */
1391a5ed8448SMatt Peters 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->use_msix)
1392198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1393a5ed8448SMatt Peters 	else
1394a5ed8448SMatt Peters 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1395954ea115SStephen Hemminger 
13966c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
13976ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
13986ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
13996c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
14006c3169a3SBruce Richardson 	else
14016c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
14026c3169a3SBruce Richardson 
14036c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
14046c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
14056c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
14066c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
14076c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
14086c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
14096c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
14106c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
14116c3169a3SBruce Richardson 
14126c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
14136c3169a3SBruce Richardson 		config = &local_config;
14146c3169a3SBruce Richardson 
14156d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
14166d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
14176d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
14186d7740e2SChangchun Ouyang 
14196c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
14206d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
14216d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
14226d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
14236c3169a3SBruce Richardson 		} else {
14246c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
14256c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
14266c3169a3SBruce Richardson 			config->status = 0;
14276c3169a3SBruce Richardson 		}
14286c3169a3SBruce Richardson 
14296c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
14306d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
14316d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
14326d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
14336d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
14346c3169a3SBruce Richardson 		} else {
14356c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
14366c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
14376c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
14386c3169a3SBruce Richardson 		}
14396c3169a3SBruce Richardson 
144045e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
14416c3169a3SBruce Richardson 
144249d26d9eSMaxime Coquelin 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
144349d26d9eSMaxime Coquelin 			vtpci_read_dev_config(hw,
144449d26d9eSMaxime Coquelin 				offsetof(struct virtio_net_config, mtu),
144549d26d9eSMaxime Coquelin 				&config->mtu,
144649d26d9eSMaxime Coquelin 				sizeof(config->mtu));
144749d26d9eSMaxime Coquelin 
144849d26d9eSMaxime Coquelin 			/*
144949d26d9eSMaxime Coquelin 			 * MTU value has already been checked at negotiation
145049d26d9eSMaxime Coquelin 			 * time, but check again in case it has changed since
145149d26d9eSMaxime Coquelin 			 * then, which should not happen.
145249d26d9eSMaxime Coquelin 			 */
145349d26d9eSMaxime Coquelin 			if (config->mtu < ETHER_MIN_MTU) {
145449d26d9eSMaxime Coquelin 				PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
145549d26d9eSMaxime Coquelin 						config->mtu);
145649d26d9eSMaxime Coquelin 				return -1;
145749d26d9eSMaxime Coquelin 			}
145849d26d9eSMaxime Coquelin 
145949d26d9eSMaxime Coquelin 			hw->max_mtu = config->mtu;
146049d26d9eSMaxime Coquelin 			/* Set initial MTU to maximum one supported by vhost */
146149d26d9eSMaxime Coquelin 			eth_dev->data->mtu = config->mtu;
146249d26d9eSMaxime Coquelin 
146349d26d9eSMaxime Coquelin 		} else {
146449d26d9eSMaxime Coquelin 			hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
146549d26d9eSMaxime Coquelin 				VLAN_TAG_LEN - hw->vtnet_hdr_size;
146649d26d9eSMaxime Coquelin 		}
146749d26d9eSMaxime Coquelin 
14686c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
14696c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
14706c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
14716c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
14726c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
14736c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
14746c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
14756c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
14766c3169a3SBruce Richardson 	} else {
147745e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
147845e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
14796c3169a3SBruce Richardson 	}
14806c3169a3SBruce Richardson 
148169c80d4eSYuanhan Liu 	ret = virtio_alloc_queues(eth_dev);
148269c80d4eSYuanhan Liu 	if (ret < 0)
148369c80d4eSYuanhan Liu 		return ret;
148426b683b4SJianfeng Tan 
148526b683b4SJianfeng Tan 	if (eth_dev->data->dev_conf.intr_conf.rxq) {
148626b683b4SJianfeng Tan 		if (virtio_configure_intr(eth_dev) < 0) {
148726b683b4SJianfeng Tan 			PMD_INIT_LOG(ERR, "failed to configure interrupt");
148826b683b4SJianfeng Tan 			return -1;
148926b683b4SJianfeng Tan 		}
149026b683b4SJianfeng Tan 	}
149126b683b4SJianfeng Tan 
1492ee85024cSYuanhan Liu 	vtpci_reinit_complete(hw);
149369c80d4eSYuanhan Liu 
1494ce2eabddSJianfeng Tan 	if (pci_dev)
14956c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
14966c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
14976c3169a3SBruce Richardson 			pci_dev->id.device_id);
14986c3169a3SBruce Richardson 
1499198ab336SOlivier Matz 	return 0;
1500198ab336SOlivier Matz }
1501198ab336SOlivier Matz 
1502198ab336SOlivier Matz /*
15036d890f8aSYuanhan Liu  * Remap the PCI device again (IO port map for legacy device and
15046d890f8aSYuanhan Liu  * memory map for modern device), so that the secondary process
15056d890f8aSYuanhan Liu  * could have the PCI initiated correctly.
15066d890f8aSYuanhan Liu  */
15076d890f8aSYuanhan Liu static int
15086d890f8aSYuanhan Liu virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
15096d890f8aSYuanhan Liu {
15106d890f8aSYuanhan Liu 	if (hw->modern) {
15116d890f8aSYuanhan Liu 		/*
15126d890f8aSYuanhan Liu 		 * We don't have to re-parse the PCI config space, since
15133dcfe039SThomas Monjalon 		 * rte_pci_map_device() makes sure the mapped address
15146d890f8aSYuanhan Liu 		 * in secondary process would equal to the one mapped in
15156d890f8aSYuanhan Liu 		 * the primary process: error will be returned if that
15166d890f8aSYuanhan Liu 		 * requirement is not met.
15176d890f8aSYuanhan Liu 		 *
15186d890f8aSYuanhan Liu 		 * That said, we could simply reuse all cap pointers
15196d890f8aSYuanhan Liu 		 * (such as dev_cfg, common_cfg, etc.) parsed from the
15206d890f8aSYuanhan Liu 		 * primary process, which is stored in shared memory.
15216d890f8aSYuanhan Liu 		 */
15223dcfe039SThomas Monjalon 		if (rte_pci_map_device(pci_dev)) {
15236d890f8aSYuanhan Liu 			PMD_INIT_LOG(DEBUG, "failed to map pci device!");
15246d890f8aSYuanhan Liu 			return -1;
15256d890f8aSYuanhan Liu 		}
15266d890f8aSYuanhan Liu 	} else {
15273dcfe039SThomas Monjalon 		if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
15286d890f8aSYuanhan Liu 			return -1;
15296d890f8aSYuanhan Liu 	}
15306d890f8aSYuanhan Liu 
15316d890f8aSYuanhan Liu 	return 0;
15326d890f8aSYuanhan Liu }
15336d890f8aSYuanhan Liu 
15346d890f8aSYuanhan Liu static void
15356d890f8aSYuanhan Liu virtio_set_vtpci_ops(struct virtio_hw *hw)
15366d890f8aSYuanhan Liu {
1537e754c959SThomas Monjalon #ifdef RTE_VIRTIO_USER
15386d890f8aSYuanhan Liu 	if (hw->virtio_user_dev)
15396d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &virtio_user_ops;
1540e754c959SThomas Monjalon 	else
1541e754c959SThomas Monjalon #endif
1542e754c959SThomas Monjalon 	if (hw->modern)
15436d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &modern_ops;
15446d890f8aSYuanhan Liu 	else
15456d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &legacy_ops;
15466d890f8aSYuanhan Liu }
15476d890f8aSYuanhan Liu 
15486d890f8aSYuanhan Liu /*
1549198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1550198ab336SOlivier Matz  * It returns 0 on success.
1551198ab336SOlivier Matz  */
1552198ab336SOlivier Matz int
1553198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1554198ab336SOlivier Matz {
1555198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1556198ab336SOlivier Matz 	int ret;
1557198ab336SOlivier Matz 
1558198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1559198ab336SOlivier Matz 
1560198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1561198ab336SOlivier Matz 
1562198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
15636d890f8aSYuanhan Liu 		if (!hw->virtio_user_dev) {
1564c0802544SFerruh Yigit 			ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
15656d890f8aSYuanhan Liu 			if (ret)
15666d890f8aSYuanhan Liu 				return ret;
15676d890f8aSYuanhan Liu 		}
15686d890f8aSYuanhan Liu 
15696d890f8aSYuanhan Liu 		virtio_set_vtpci_ops(hw);
15704819eae8SOlivier Matz 		set_rxtx_funcs(eth_dev);
15714819eae8SOlivier Matz 
1572198ab336SOlivier Matz 		return 0;
1573198ab336SOlivier Matz 	}
1574198ab336SOlivier Matz 
1575198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1576198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1577198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1578198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1579198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1580198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1581198ab336SOlivier Matz 		return -ENOMEM;
1582198ab336SOlivier Matz 	}
1583198ab336SOlivier Matz 
158476873125SYuanhan Liu 	hw->port_id = eth_dev->data->port_id;
15856a7c0dfcSJan Blunck 	/* For virtio_user case the hw->virtio_user_dev is populated by
15866a7c0dfcSJan Blunck 	 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
15876a7c0dfcSJan Blunck 	 */
15886a7c0dfcSJan Blunck 	if (!hw->virtio_user_dev) {
1589c0802544SFerruh Yigit 		ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1590198ab336SOlivier Matz 		if (ret)
1591198ab336SOlivier Matz 			return ret;
1592198ab336SOlivier Matz 	}
1593198ab336SOlivier Matz 
159460e6f470SOlivier Matz 	/* reset device and negotiate default features */
159596cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1596198ab336SOlivier Matz 	if (ret < 0)
1597198ab336SOlivier Matz 		return ret;
1598198ab336SOlivier Matz 
15996c3169a3SBruce Richardson 	/* Setup interrupt callback  */
160062a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
160161e3ee17SYuanhan Liu 		rte_intr_callback_register(eth_dev->intr_handle,
16026c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
16036c3169a3SBruce Richardson 
16046c3169a3SBruce Richardson 	return 0;
16056c3169a3SBruce Richardson }
16066c3169a3SBruce Richardson 
1607abf4c84bSBernard Iremonger static int
1608abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1609abf4c84bSBernard Iremonger {
1610abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1611abf4c84bSBernard Iremonger 
1612abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1613abf4c84bSBernard Iremonger 		return -EPERM;
1614abf4c84bSBernard Iremonger 
16153f3b3d54SYuanhan Liu 	virtio_dev_stop(eth_dev);
1616abf4c84bSBernard Iremonger 	virtio_dev_close(eth_dev);
1617abf4c84bSBernard Iremonger 
1618abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1619abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1620abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1621abf4c84bSBernard Iremonger 
1622abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1623abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1624abf4c84bSBernard Iremonger 
1625abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
162662a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
162761e3ee17SYuanhan Liu 		rte_intr_callback_unregister(eth_dev->intr_handle,
1628abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1629abf4c84bSBernard Iremonger 						eth_dev);
16309470427cSYuanhan Liu 	if (eth_dev->device)
1631c0802544SFerruh Yigit 		rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1632abf4c84bSBernard Iremonger 
1633abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1634abf4c84bSBernard Iremonger 
1635abf4c84bSBernard Iremonger 	return 0;
1636abf4c84bSBernard Iremonger }
1637abf4c84bSBernard Iremonger 
1638fdf91e0fSJan Blunck static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1639fdf91e0fSJan Blunck 	struct rte_pci_device *pci_dev)
1640fdf91e0fSJan Blunck {
1641fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1642fdf91e0fSJan Blunck 		eth_virtio_dev_init);
1643fdf91e0fSJan Blunck }
1644fdf91e0fSJan Blunck 
1645fdf91e0fSJan Blunck static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1646fdf91e0fSJan Blunck {
1647fdf91e0fSJan Blunck 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1648fdf91e0fSJan Blunck }
1649fdf91e0fSJan Blunck 
1650fdf91e0fSJan Blunck static struct rte_pci_driver rte_virtio_pmd = {
1651cd433ce5SDavid Marchand 	.driver = {
1652cd433ce5SDavid Marchand 		.name = "net_virtio",
1653cd433ce5SDavid Marchand 	},
16546c3169a3SBruce Richardson 	.id_table = pci_id_virtio_map,
165522dda618SBen Walker 	.drv_flags = 0,
1656fdf91e0fSJan Blunck 	.probe = eth_virtio_pci_probe,
1657fdf91e0fSJan Blunck 	.remove = eth_virtio_pci_remove,
16586c3169a3SBruce Richardson };
16596c3169a3SBruce Richardson 
1660c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1661c830cb29SDavid Marchand static void
1662c830cb29SDavid Marchand rte_virtio_pmd_init(void)
16636c3169a3SBruce Richardson {
16646c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
16656c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1666c830cb29SDavid Marchand 		return;
16676c3169a3SBruce Richardson 	}
16686c3169a3SBruce Richardson 
16693dcfe039SThomas Monjalon 	rte_pci_register(&rte_virtio_pmd);
16706c3169a3SBruce Richardson }
16716c3169a3SBruce Richardson 
16726c3169a3SBruce Richardson /*
16736c3169a3SBruce Richardson  * Configure virtio device
16746c3169a3SBruce Richardson  * It returns 0 on success.
16756c3169a3SBruce Richardson  */
16766c3169a3SBruce Richardson static int
16776c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
16786c3169a3SBruce Richardson {
16796c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
16806c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
1681ec9f3d12SOlivier Matz 	uint64_t req_features;
1682e7b9d1d2SJiayu Hu 	int ret;
16836c3169a3SBruce Richardson 
16846c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
1685ec9f3d12SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
16864dab342bSStephen Hemminger 
1687e7b9d1d2SJiayu Hu 	if (dev->data->dev_conf.intr_conf.rxq) {
1688e7b9d1d2SJiayu Hu 		ret = virtio_init_device(dev, hw->req_guest_features);
1689e7b9d1d2SJiayu Hu 		if (ret < 0)
1690e7b9d1d2SJiayu Hu 			return ret;
1691e7b9d1d2SJiayu Hu 	}
1692e7b9d1d2SJiayu Hu 
1693d67d86ceSOlivier Matz 	/* The name hw_ip_checksum is a bit confusing since it can be
1694d67d86ceSOlivier Matz 	 * set by the application to request L3 and/or L4 checksums. In
1695d67d86ceSOlivier Matz 	 * case of virtio, only L4 checksum is supported.
1696d67d86ceSOlivier Matz 	 */
1697d67d86ceSOlivier Matz 	if (rxmode->hw_ip_checksum)
1698d67d86ceSOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
1699d67d86ceSOlivier Matz 
1700ec9f3d12SOlivier Matz 	if (rxmode->enable_lro)
1701ec9f3d12SOlivier Matz 		req_features |=
1702ec9f3d12SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
1703ec9f3d12SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
17046c3169a3SBruce Richardson 
1705ec9f3d12SOlivier Matz 	/* if request features changed, reinit the device */
1706ec9f3d12SOlivier Matz 	if (req_features != hw->req_guest_features) {
1707ec9f3d12SOlivier Matz 		ret = virtio_init_device(dev, req_features);
1708ec9f3d12SOlivier Matz 		if (ret < 0)
1709ec9f3d12SOlivier Matz 			return ret;
1710ec9f3d12SOlivier Matz 	}
1711ec9f3d12SOlivier Matz 
1712d67d86ceSOlivier Matz 	if (rxmode->hw_ip_checksum &&
1713d67d86ceSOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
171478fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
1715d67d86ceSOlivier Matz 			"rx checksum not available on this host");
1716d67d86ceSOlivier Matz 		return -ENOTSUP;
1717d67d86ceSOlivier Matz 	}
1718d67d86ceSOlivier Matz 
1719ec9f3d12SOlivier Matz 	if (rxmode->enable_lro &&
1720ec9f3d12SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1721ec9f3d12SOlivier Matz 			!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
172278fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
1723ec9f3d12SOlivier Matz 			"Large Receive Offload not available on this host");
172486d59b21SOlivier Matz 		return -ENOTSUP;
172586d59b21SOlivier Matz 	}
172686d59b21SOlivier Matz 
172769c80d4eSYuanhan Liu 	/* start control queue */
172869c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
172945e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
173045e4acd4SOlivier Matz 
17316c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
17326c3169a3SBruce Richardson 
17336c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
17346c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
173578fd97c3SOlivier Matz 		PMD_DRV_LOG(ERR,
17366c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
17376c3169a3SBruce Richardson 		return -ENOTSUP;
17386c3169a3SBruce Richardson 	}
17396c3169a3SBruce Richardson 
174062a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1741981e61f5SJianfeng Tan 		/* Enable vector (0) for Link State Intrerrupt */
1742981e61f5SJianfeng Tan 		if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1743981e61f5SJianfeng Tan 				VIRTIO_MSI_NO_VECTOR) {
17446c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
17456c3169a3SBruce Richardson 			return -EBUSY;
17466c3169a3SBruce Richardson 		}
17476c3169a3SBruce Richardson 
174809649363SOlivier Matz 	hw->use_simple_rx = 1;
174909649363SOlivier Matz 	hw->use_simple_tx = 1;
17504819eae8SOlivier Matz 
175102dd0e21SOlivier Matz #if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
175209649363SOlivier Matz 	if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
175309649363SOlivier Matz 		hw->use_simple_rx = 0;
175409649363SOlivier Matz 		hw->use_simple_tx = 0;
175509649363SOlivier Matz 	}
17564819eae8SOlivier Matz #endif
175709649363SOlivier Matz 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
175809649363SOlivier Matz 		hw->use_simple_rx = 0;
175909649363SOlivier Matz 		hw->use_simple_tx = 0;
176009649363SOlivier Matz 	}
17614819eae8SOlivier Matz 
176216e48c9eSOlivier Matz 	if (rxmode->hw_ip_checksum)
176316e48c9eSOlivier Matz 		hw->use_simple_rx = 0;
176416e48c9eSOlivier Matz 
17656c3169a3SBruce Richardson 	return 0;
17666c3169a3SBruce Richardson }
17676c3169a3SBruce Richardson 
17686c3169a3SBruce Richardson 
17696c3169a3SBruce Richardson static int
17706c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
17716c3169a3SBruce Richardson {
17726c3169a3SBruce Richardson 	uint16_t nb_queues, i;
177301ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
177401ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
1775663c7679SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
1776efc83a1eSOlivier Matz 	int ret;
1777efc83a1eSOlivier Matz 
1778efc83a1eSOlivier Matz 	/* Finish the initialization of the queues */
1779efc83a1eSOlivier Matz 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1780efc83a1eSOlivier Matz 		ret = virtio_dev_rx_queue_setup_finish(dev, i);
1781efc83a1eSOlivier Matz 		if (ret < 0)
1782efc83a1eSOlivier Matz 			return ret;
1783efc83a1eSOlivier Matz 	}
1784efc83a1eSOlivier Matz 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1785efc83a1eSOlivier Matz 		ret = virtio_dev_tx_queue_setup_finish(dev, i);
1786efc83a1eSOlivier Matz 		if (ret < 0)
1787efc83a1eSOlivier Matz 			return ret;
1788efc83a1eSOlivier Matz 	}
17896c3169a3SBruce Richardson 
17906c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1791954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
179262a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
17936c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
17946c3169a3SBruce Richardson 			return -ENOTSUP;
17956c3169a3SBruce Richardson 		}
1796349a447bSJianfeng Tan 	}
1797349a447bSJianfeng Tan 
1798349a447bSJianfeng Tan 	/* Enable uio/vfio intr/eventfd mapping: althrough we already did that
1799349a447bSJianfeng Tan 	 * in device configure, but it could be unmapped  when device is
1800349a447bSJianfeng Tan 	 * stopped.
1801349a447bSJianfeng Tan 	 */
1802349a447bSJianfeng Tan 	if (dev->data->dev_conf.intr_conf.lsc ||
1803349a447bSJianfeng Tan 	    dev->data->dev_conf.intr_conf.rxq) {
1804349a447bSJianfeng Tan 		rte_intr_disable(dev->intr_handle);
18056c3169a3SBruce Richardson 
180661e3ee17SYuanhan Liu 		if (rte_intr_enable(dev->intr_handle) < 0) {
18076c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
18086c3169a3SBruce Richardson 			return -EIO;
18096c3169a3SBruce Richardson 		}
18106c3169a3SBruce Richardson 	}
18116c3169a3SBruce Richardson 
18126c3169a3SBruce Richardson 	/*Notify the backend
18136c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
18146c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
18156c3169a3SBruce Richardson 	 */
181660d4a353SYuanhan Liu 	nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1817663c7679SYuanhan Liu 	if (hw->max_queue_pairs > 1) {
18186c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
18196c3169a3SBruce Richardson 			return -EINVAL;
18206c3169a3SBruce Richardson 	}
18216c3169a3SBruce Richardson 
18226c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
18236c3169a3SBruce Richardson 
182460d4a353SYuanhan Liu 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
182501ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
1826d8227497STiwei Bie 		/* Flush the old packets */
1827d8227497STiwei Bie 		virtqueue_flush(rxvq->vq);
182801ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
182901ad44fdSHuawei Xie 	}
18306c3169a3SBruce Richardson 
1831924da8f1SSteven Luong 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1832924da8f1SSteven Luong 		txvq = dev->data->tx_queues[i];
1833924da8f1SSteven Luong 		virtqueue_notify(txvq->vq);
1834924da8f1SSteven Luong 	}
1835924da8f1SSteven Luong 
18366c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
18376c3169a3SBruce Richardson 
183801ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
183901ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
184001ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
184101ad44fdSHuawei Xie 	}
18426c3169a3SBruce Richardson 
184301ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
184401ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
184501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
184601ad44fdSHuawei Xie 	}
184758d9fe40SJianfeng Tan 
18484819eae8SOlivier Matz 	set_rxtx_funcs(dev);
1849aa9f0606SYuanhan Liu 	hw->started = 1;
18506c3169a3SBruce Richardson 
185158d9fe40SJianfeng Tan 	/* Initialize Link state */
185258d9fe40SJianfeng Tan 	virtio_dev_link_update(dev, 0);
185358d9fe40SJianfeng Tan 
18546c3169a3SBruce Richardson 	return 0;
18556c3169a3SBruce Richardson }
18566c3169a3SBruce Richardson 
18576c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
18586c3169a3SBruce Richardson {
18596c3169a3SBruce Richardson 	struct rte_mbuf *buf;
18606c3169a3SBruce Richardson 	int i, mbuf_num = 0;
18616c3169a3SBruce Richardson 
18626c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
186301ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
186401ad44fdSHuawei Xie 
18656c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
18666c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
186701ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
18686c3169a3SBruce Richardson 
186901ad44fdSHuawei Xie 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
187001ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
18716c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
18726c3169a3SBruce Richardson 			mbuf_num++;
18736c3169a3SBruce Richardson 		}
18746c3169a3SBruce Richardson 
18756c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
18766c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
18776c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
187801ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
18796c3169a3SBruce Richardson 	}
18806c3169a3SBruce Richardson 
18816c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
188201ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
188301ad44fdSHuawei Xie 
18846c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
18856c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
18866c3169a3SBruce Richardson 			     i);
188701ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
18886c3169a3SBruce Richardson 
18896c3169a3SBruce Richardson 		mbuf_num = 0;
189001ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
18916c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
18926c3169a3SBruce Richardson 			mbuf_num++;
18936c3169a3SBruce Richardson 		}
18946c3169a3SBruce Richardson 
18956c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
18966c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
18976c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
189801ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
18996c3169a3SBruce Richardson 	}
19006c3169a3SBruce Richardson }
19016c3169a3SBruce Richardson 
19026c3169a3SBruce Richardson /*
19036c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
19046c3169a3SBruce Richardson  */
19056c3169a3SBruce Richardson static void
19066c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
19076c3169a3SBruce Richardson {
1908aa9f0606SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
19096c3169a3SBruce Richardson 	struct rte_eth_link link;
1910349a447bSJianfeng Tan 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
19116c3169a3SBruce Richardson 
19126c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
19136c3169a3SBruce Richardson 
1914349a447bSJianfeng Tan 	if (intr_conf->lsc || intr_conf->rxq)
191561e3ee17SYuanhan Liu 		rte_intr_disable(dev->intr_handle);
19166c3169a3SBruce Richardson 
1917aa9f0606SYuanhan Liu 	hw->started = 0;
19186c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
19196c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
19206c3169a3SBruce Richardson }
19216c3169a3SBruce Richardson 
19226c3169a3SBruce Richardson static int
19236c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
19246c3169a3SBruce Richardson {
19256c3169a3SBruce Richardson 	struct rte_eth_link link, old;
19266c3169a3SBruce Richardson 	uint16_t status;
19276c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
19286c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
19296c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
19306c3169a3SBruce Richardson 	old = link;
193111319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
19326c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
19336c3169a3SBruce Richardson 
1934aa9f0606SYuanhan Liu 	if (hw->started == 0) {
1935aa9f0606SYuanhan Liu 		link.link_status = ETH_LINK_DOWN;
1936aa9f0606SYuanhan Liu 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
19376c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
19386c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
19396c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
19406c3169a3SBruce Richardson 				&status, sizeof(status));
19416c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
194209419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
19436c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
19446c3169a3SBruce Richardson 				     dev->data->port_id);
19456c3169a3SBruce Richardson 		} else {
194609419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
19476c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
19486c3169a3SBruce Richardson 				     dev->data->port_id);
19496c3169a3SBruce Richardson 		}
19506c3169a3SBruce Richardson 	} else {
195109419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
19526c3169a3SBruce Richardson 	}
19536c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
19546c3169a3SBruce Richardson 
19556c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
19566c3169a3SBruce Richardson }
19576c3169a3SBruce Richardson 
1958289ba0c0SDavid Harton static int
1959289ba0c0SDavid Harton virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1960289ba0c0SDavid Harton {
1961289ba0c0SDavid Harton 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1962289ba0c0SDavid Harton 	struct virtio_hw *hw = dev->data->dev_private;
1963289ba0c0SDavid Harton 
1964289ba0c0SDavid Harton 	if (mask & ETH_VLAN_FILTER_MASK) {
1965289ba0c0SDavid Harton 		if (rxmode->hw_vlan_filter &&
1966289ba0c0SDavid Harton 				!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1967289ba0c0SDavid Harton 
1968289ba0c0SDavid Harton 			PMD_DRV_LOG(NOTICE,
1969289ba0c0SDavid Harton 				"vlan filtering not available on this host");
1970289ba0c0SDavid Harton 
1971289ba0c0SDavid Harton 			return -ENOTSUP;
1972289ba0c0SDavid Harton 		}
1973289ba0c0SDavid Harton 	}
1974289ba0c0SDavid Harton 
1975289ba0c0SDavid Harton 	if (mask & ETH_VLAN_STRIP_MASK)
1976289ba0c0SDavid Harton 		hw->vlan_strip = rxmode->hw_vlan_strip;
1977289ba0c0SDavid Harton 
1978289ba0c0SDavid Harton 	return 0;
1979289ba0c0SDavid Harton }
1980289ba0c0SDavid Harton 
19816c3169a3SBruce Richardson static void
19826c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
19836c3169a3SBruce Richardson {
1984c1e55ed3SOlivier Matz 	uint64_t tso_mask, host_features;
19856c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
19866c3169a3SBruce Richardson 
1987b392e987SIdo Barnea 	dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
1988b392e987SIdo Barnea 
1989c0802544SFerruh Yigit 	dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL;
199045e4acd4SOlivier Matz 	dev_info->max_rx_queues =
199145e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
199245e4acd4SOlivier Matz 	dev_info->max_tx_queues =
199345e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
19946c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
19956c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
19966c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
19976c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
19986c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
19996c3169a3SBruce Richardson 	};
200058169a9cSOlivier Matz 
2001c1e55ed3SOlivier Matz 	host_features = VTPCI_OPS(hw)->get_features(hw);
2002c1e55ed3SOlivier Matz 	dev_info->rx_offload_capa = 0;
2003c1e55ed3SOlivier Matz 	if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2004c1e55ed3SOlivier Matz 		dev_info->rx_offload_capa |=
2005c1e55ed3SOlivier Matz 			DEV_RX_OFFLOAD_TCP_CKSUM |
2006c1e55ed3SOlivier Matz 			DEV_RX_OFFLOAD_UDP_CKSUM;
2007c1e55ed3SOlivier Matz 	}
2008c1e55ed3SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2009c1e55ed3SOlivier Matz 		(1ULL << VIRTIO_NET_F_GUEST_TSO6);
2010ec9f3d12SOlivier Matz 	if ((host_features & tso_mask) == tso_mask)
2011ec9f3d12SOlivier Matz 		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2012c1e55ed3SOlivier Matz 
2013c1e55ed3SOlivier Matz 	dev_info->tx_offload_capa = 0;
201458169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
201558169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
201658169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
201758169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
201858169a9cSOlivier Matz 	}
201969657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
202069657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
202169657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
202269657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
20236c3169a3SBruce Richardson }
20246c3169a3SBruce Richardson 
20256c3169a3SBruce Richardson /*
20266c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
20276c3169a3SBruce Richardson  */
20286c3169a3SBruce Richardson static int
20296c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
20306c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
20316c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
20326c3169a3SBruce Richardson {
20336c3169a3SBruce Richardson 	return 0;
20346c3169a3SBruce Richardson }
20356c3169a3SBruce Richardson 
203601f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
203701f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
203806e81dc9SDavid Marchand RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2039