xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision b0caba1a)
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>
416c3169a3SBruce Richardson #include <rte_memcpy.h>
426c3169a3SBruce Richardson #include <rte_string_fns.h>
436c3169a3SBruce Richardson #include <rte_memzone.h>
446c3169a3SBruce Richardson #include <rte_malloc.h>
456c3169a3SBruce Richardson #include <rte_atomic.h>
466c3169a3SBruce Richardson #include <rte_branch_prediction.h>
476c3169a3SBruce Richardson #include <rte_pci.h>
486c3169a3SBruce Richardson #include <rte_ether.h>
496c3169a3SBruce Richardson #include <rte_common.h>
50abf4c84bSBernard Iremonger #include <rte_errno.h>
516c3169a3SBruce Richardson 
526c3169a3SBruce Richardson #include <rte_memory.h>
536c3169a3SBruce Richardson #include <rte_eal.h>
546c3169a3SBruce Richardson #include <rte_dev.h>
556c3169a3SBruce Richardson 
566c3169a3SBruce Richardson #include "virtio_ethdev.h"
576c3169a3SBruce Richardson #include "virtio_pci.h"
586c3169a3SBruce Richardson #include "virtio_logs.h"
596c3169a3SBruce Richardson #include "virtqueue.h"
60cab04612SHuawei Xie #include "virtio_rxtx.h"
616c3169a3SBruce Richardson 
62abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
636c3169a3SBruce Richardson static int  virtio_dev_configure(struct rte_eth_dev *dev);
646c3169a3SBruce Richardson static int  virtio_dev_start(struct rte_eth_dev *dev);
656c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev);
666c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
676c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
686c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
696c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
706c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev,
716c3169a3SBruce Richardson 				struct rte_eth_dev_info *dev_info);
726c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev,
736c3169a3SBruce Richardson 	__rte_unused int wait_to_complete);
746c3169a3SBruce Richardson 
756c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw);
766c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw);
776c3169a3SBruce Richardson 
7876d4c652SHarry van Haaren static void virtio_dev_stats_get(struct rte_eth_dev *dev,
7976d4c652SHarry van Haaren 				 struct rte_eth_stats *stats);
8076d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
81e2aae1c1SRemy Horton 				 struct rte_eth_xstat *xstats, unsigned n);
82baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
83baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
84baf91c39SRemy Horton 				       unsigned limit);
856c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
866c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
876c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
886c3169a3SBruce Richardson 				uint16_t vlan_id, int on);
896c3169a3SBruce Richardson static void virtio_mac_addr_add(struct rte_eth_dev *dev,
906c3169a3SBruce Richardson 				struct ether_addr *mac_addr,
916c3169a3SBruce Richardson 				uint32_t index, uint32_t vmdq __rte_unused);
926c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
936c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev,
946c3169a3SBruce Richardson 				struct ether_addr *mac_addr);
956c3169a3SBruce Richardson 
966c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set(
976c3169a3SBruce Richardson 	__rte_unused struct rte_eth_dev *eth_dev,
986c3169a3SBruce Richardson 	__rte_unused uint16_t queue_id,
996c3169a3SBruce Richardson 	__rte_unused uint8_t stat_idx,
1006c3169a3SBruce Richardson 	__rte_unused uint8_t is_rx);
1016c3169a3SBruce Richardson 
1026c3169a3SBruce Richardson /*
1036c3169a3SBruce Richardson  * The set of PCI devices this driver supports
1046c3169a3SBruce Richardson  */
1056c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = {
1064c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
1074c790365SJason Wang 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
1086c3169a3SBruce Richardson 	{ .vendor_id = 0, /* sentinel */ },
1096c3169a3SBruce Richardson };
1106c3169a3SBruce Richardson 
11176d4c652SHarry van Haaren struct rte_virtio_xstats_name_off {
11276d4c652SHarry van Haaren 	char name[RTE_ETH_XSTATS_NAME_SIZE];
11376d4c652SHarry van Haaren 	unsigned offset;
11476d4c652SHarry van Haaren };
11576d4c652SHarry van Haaren 
11676d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */
11701ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
11801ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
11901ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
12001ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_rx, stats.errors)},
12101ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
12201ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
12301ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
12401ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
12501ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
12601ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
12701ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
12801ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
12981f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
13081f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
13176d4c652SHarry van Haaren };
13276d4c652SHarry van Haaren 
13301ad44fdSHuawei Xie /* [rt]x_qX_ is prepended to the name string here */
13401ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
13501ad44fdSHuawei Xie 	{"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
13601ad44fdSHuawei Xie 	{"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
13701ad44fdSHuawei Xie 	{"errors",                 offsetof(struct virtnet_tx, stats.errors)},
13801ad44fdSHuawei Xie 	{"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
13901ad44fdSHuawei Xie 	{"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
14001ad44fdSHuawei Xie 	{"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
14101ad44fdSHuawei Xie 	{"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
14201ad44fdSHuawei Xie 	{"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
14301ad44fdSHuawei Xie 	{"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
14401ad44fdSHuawei Xie 	{"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
14501ad44fdSHuawei Xie 	{"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
14681f7234bSZhiyong Yang 	{"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
14781f7234bSZhiyong Yang 	{"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
14801ad44fdSHuawei Xie };
14901ad44fdSHuawei Xie 
15001ad44fdSHuawei Xie #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
15101ad44fdSHuawei Xie 			    sizeof(rte_virtio_rxq_stat_strings[0]))
15201ad44fdSHuawei Xie #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
15301ad44fdSHuawei Xie 			    sizeof(rte_virtio_txq_stat_strings[0]))
15476d4c652SHarry van Haaren 
155553f4593SYuanhan Liu struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
156553f4593SYuanhan Liu 
1576c3169a3SBruce Richardson static int
15801ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
1596c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1606c3169a3SBruce Richardson {
1619e71668bSDamjan Marion 	uint32_t head, i;
1626c3169a3SBruce Richardson 	int k, sum = 0;
1636c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
1646c3169a3SBruce Richardson 	struct virtio_pmd_ctrl result;
16501ad44fdSHuawei Xie 	struct virtqueue *vq;
1666c3169a3SBruce Richardson 
1676c3169a3SBruce Richardson 	ctrl->status = status;
1686c3169a3SBruce Richardson 
1693bd60a27SJianfeng Tan 	if (!cvq || !cvq->vq) {
1700bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "Control queue is not supported.");
1716c3169a3SBruce Richardson 		return -1;
1726c3169a3SBruce Richardson 	}
17301ad44fdSHuawei Xie 	vq = cvq->vq;
1749e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1756c3169a3SBruce Richardson 
1766c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1776c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1786c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1796c3169a3SBruce Richardson 
1806c3169a3SBruce Richardson 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
1816c3169a3SBruce Richardson 		return -1;
1826c3169a3SBruce Richardson 
18301ad44fdSHuawei Xie 	memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
1846c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1856c3169a3SBruce Richardson 
1866c3169a3SBruce Richardson 	/*
1876c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1886c3169a3SBruce Richardson 	 * One TX packet for header;
1896c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1906c3169a3SBruce Richardson 	 * One RX packet for ACK.
1916c3169a3SBruce Richardson 	 */
1926c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
193f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
1946c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1956c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1966c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
1976c3169a3SBruce Richardson 
1986c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
1996c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
200f24f8f9fSJianfeng Tan 		vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2016c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
2026c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
2036c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
2046c3169a3SBruce Richardson 		sum += dlen[k];
2056c3169a3SBruce Richardson 		vq->vq_free_cnt--;
2066c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
2076c3169a3SBruce Richardson 	}
2086c3169a3SBruce Richardson 
2096c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
210f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2116c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
2126c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
2136c3169a3SBruce Richardson 	vq->vq_free_cnt--;
2146c3169a3SBruce Richardson 
2156c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
2166c3169a3SBruce Richardson 
2176c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
2186c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
2196c3169a3SBruce Richardson 
2206c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
2216c3169a3SBruce Richardson 
2226c3169a3SBruce Richardson 	virtqueue_notify(vq);
2236c3169a3SBruce Richardson 
2246c3169a3SBruce Richardson 	rte_rmb();
225b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq) == 0) {
2266c3169a3SBruce Richardson 		rte_rmb();
2276c3169a3SBruce Richardson 		usleep(100);
2286c3169a3SBruce Richardson 	}
2296c3169a3SBruce Richardson 
230b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq)) {
2316c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
2326c3169a3SBruce Richardson 		struct vring_used_elem *uep;
2336c3169a3SBruce Richardson 
2346c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
2356c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
2366c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
2376c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
2386c3169a3SBruce Richardson 		desc_idx = idx;
2396c3169a3SBruce Richardson 
2406c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
2416c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
2426c3169a3SBruce Richardson 			vq->vq_free_cnt++;
2436c3169a3SBruce Richardson 		}
2446c3169a3SBruce Richardson 
2456c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2466c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2476c3169a3SBruce Richardson 
2486c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2496c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2506c3169a3SBruce Richardson 	}
2516c3169a3SBruce Richardson 
2526c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2536c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2546c3169a3SBruce Richardson 
25501ad44fdSHuawei Xie 	memcpy(&result, cvq->virtio_net_hdr_mz->addr,
2566c3169a3SBruce Richardson 			sizeof(struct virtio_pmd_ctrl));
2576c3169a3SBruce Richardson 
2586c3169a3SBruce Richardson 	return result.status;
2596c3169a3SBruce Richardson }
2606c3169a3SBruce Richardson 
2616c3169a3SBruce Richardson static int
2626c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2636c3169a3SBruce Richardson {
2646c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2656c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2666c3169a3SBruce Richardson 	int dlen[1];
2676c3169a3SBruce Richardson 	int ret;
2686c3169a3SBruce Richardson 
2696c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2706c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2716c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2726c3169a3SBruce Richardson 
2736c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2746c3169a3SBruce Richardson 
2756c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2766c3169a3SBruce Richardson 	if (ret) {
2776c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2786c3169a3SBruce Richardson 			  "failed, this is too late now...");
2796c3169a3SBruce Richardson 		return -EINVAL;
2806c3169a3SBruce Richardson 	}
2816c3169a3SBruce Richardson 
2826c3169a3SBruce Richardson 	return 0;
2836c3169a3SBruce Richardson }
2846c3169a3SBruce Richardson 
28569c80d4eSYuanhan Liu static void
28669c80d4eSYuanhan Liu virtio_dev_queue_release(void *queue __rte_unused)
2872963d99aSJianfeng Tan {
28869c80d4eSYuanhan Liu 	/* do nothing */
2895382b188SBernard Iremonger }
2905382b188SBernard Iremonger 
29169c80d4eSYuanhan Liu static int
29269c80d4eSYuanhan Liu virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
29369c80d4eSYuanhan Liu {
29469c80d4eSYuanhan Liu 	if (vtpci_queue_idx == hw->max_queue_pairs * 2)
29569c80d4eSYuanhan Liu 		return VTNET_CQ;
29669c80d4eSYuanhan Liu 	else if (vtpci_queue_idx % 2 == 0)
29769c80d4eSYuanhan Liu 		return VTNET_RQ;
29869c80d4eSYuanhan Liu 	else
29969c80d4eSYuanhan Liu 		return VTNET_TQ;
30069c80d4eSYuanhan Liu }
30169c80d4eSYuanhan Liu 
30269c80d4eSYuanhan Liu static uint16_t
30369c80d4eSYuanhan Liu virtio_get_nr_vq(struct virtio_hw *hw)
30469c80d4eSYuanhan Liu {
30569c80d4eSYuanhan Liu 	uint16_t nr_vq = hw->max_queue_pairs * 2;
30669c80d4eSYuanhan Liu 
30769c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
30869c80d4eSYuanhan Liu 		nr_vq += 1;
30969c80d4eSYuanhan Liu 
31069c80d4eSYuanhan Liu 	return nr_vq;
31169c80d4eSYuanhan Liu }
31269c80d4eSYuanhan Liu 
313f4d1ad15SYuanhan Liu static void
314f4d1ad15SYuanhan Liu virtio_init_vring(struct virtqueue *vq)
315f4d1ad15SYuanhan Liu {
316f4d1ad15SYuanhan Liu 	int size = vq->vq_nentries;
317f4d1ad15SYuanhan Liu 	struct vring *vr = &vq->vq_ring;
318f4d1ad15SYuanhan Liu 	uint8_t *ring_mem = vq->vq_ring_virt_mem;
319f4d1ad15SYuanhan Liu 
320f4d1ad15SYuanhan Liu 	PMD_INIT_FUNC_TRACE();
321f4d1ad15SYuanhan Liu 
322f4d1ad15SYuanhan Liu 	/*
323f4d1ad15SYuanhan Liu 	 * Reinitialise since virtio port might have been stopped and restarted
324f4d1ad15SYuanhan Liu 	 */
325f4d1ad15SYuanhan Liu 	memset(ring_mem, 0, vq->vq_ring_size);
326f4d1ad15SYuanhan Liu 	vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
327f4d1ad15SYuanhan Liu 	vq->vq_used_cons_idx = 0;
328f4d1ad15SYuanhan Liu 	vq->vq_desc_head_idx = 0;
329f4d1ad15SYuanhan Liu 	vq->vq_avail_idx = 0;
330f4d1ad15SYuanhan Liu 	vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
331f4d1ad15SYuanhan Liu 	vq->vq_free_cnt = vq->vq_nentries;
332f4d1ad15SYuanhan Liu 	memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
333f4d1ad15SYuanhan Liu 
334f4d1ad15SYuanhan Liu 	vring_desc_init(vr->desc, size);
335f4d1ad15SYuanhan Liu 
336f4d1ad15SYuanhan Liu 	/*
337f4d1ad15SYuanhan Liu 	 * Disable device(host) interrupting guest
338f4d1ad15SYuanhan Liu 	 */
339f4d1ad15SYuanhan Liu 	virtqueue_disable_intr(vq);
340f4d1ad15SYuanhan Liu }
341f4d1ad15SYuanhan Liu 
34269c80d4eSYuanhan Liu static int
34369c80d4eSYuanhan Liu virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
3446c3169a3SBruce Richardson {
3456c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
34601ad44fdSHuawei Xie 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
34701ad44fdSHuawei Xie 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
3481e7bd238SStephen Hemminger 	unsigned int vq_size, size;
3496c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
350f24f8f9fSJianfeng Tan 	struct virtnet_rx *rxvq = NULL;
351f24f8f9fSJianfeng Tan 	struct virtnet_tx *txvq = NULL;
352f24f8f9fSJianfeng Tan 	struct virtnet_ctl *cvq = NULL;
35301ad44fdSHuawei Xie 	struct virtqueue *vq;
354905a2469SYuanhan Liu 	size_t sz_hdr_mz = 0;
35501ad44fdSHuawei Xie 	void *sw_ring = NULL;
35669c80d4eSYuanhan Liu 	int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
35701ad44fdSHuawei Xie 	int ret;
3586c3169a3SBruce Richardson 
359d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
3606c3169a3SBruce Richardson 
3616c3169a3SBruce Richardson 	/*
3626c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
3636c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
3646c3169a3SBruce Richardson 	 */
365553f4593SYuanhan Liu 	vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
36669c80d4eSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
3676c3169a3SBruce Richardson 	if (vq_size == 0) {
3680bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
3696c3169a3SBruce Richardson 		return -EINVAL;
370d78deadaSStephen Hemminger 	}
371d78deadaSStephen Hemminger 
372d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3730bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
3746c3169a3SBruce Richardson 		return -EINVAL;
375d78deadaSStephen Hemminger 	}
376d78deadaSStephen Hemminger 
37773d017ddSYuanhan Liu 	snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
37873d017ddSYuanhan Liu 		 dev->data->port_id, vtpci_queue_idx);
37901ad44fdSHuawei Xie 
380905a2469SYuanhan Liu 	size = RTE_ALIGN_CEIL(sizeof(*vq) +
3816c3169a3SBruce Richardson 				vq_size * sizeof(struct vq_desc_extra),
3826c3169a3SBruce Richardson 				RTE_CACHE_LINE_SIZE);
383905a2469SYuanhan Liu 	if (queue_type == VTNET_TQ) {
38401ad44fdSHuawei Xie 		/*
38501ad44fdSHuawei Xie 		 * For each xmit packet, allocate a virtio_net_hdr
38601ad44fdSHuawei Xie 		 * and indirect ring elements
38701ad44fdSHuawei Xie 		 */
38801ad44fdSHuawei Xie 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
38901ad44fdSHuawei Xie 	} else if (queue_type == VTNET_CQ) {
39001ad44fdSHuawei Xie 		/* Allocate a page for control vq command, data and status */
39101ad44fdSHuawei Xie 		sz_hdr_mz = PAGE_SIZE;
39201ad44fdSHuawei Xie 	}
3934166bbf6SJianfeng Tan 
39469c80d4eSYuanhan Liu 	vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
39569c80d4eSYuanhan Liu 				SOCKET_ID_ANY);
39601ad44fdSHuawei Xie 	if (vq == NULL) {
39701ad44fdSHuawei Xie 		PMD_INIT_LOG(ERR, "can not allocate vq");
398cab04612SHuawei Xie 		return -ENOMEM;
399cab04612SHuawei Xie 	}
40069c80d4eSYuanhan Liu 	hw->vqs[vtpci_queue_idx] = vq;
40169c80d4eSYuanhan Liu 
4026c3169a3SBruce Richardson 	vq->hw = hw;
4036c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
4046c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
4056c3169a3SBruce Richardson 
4066c3169a3SBruce Richardson 	/*
4076c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
4086c3169a3SBruce Richardson 	 */
4096c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
4106c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
41101ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
41201ad44fdSHuawei Xie 		     size, vq->vq_ring_size);
4136c3169a3SBruce Richardson 
41469c80d4eSYuanhan Liu 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
41569c80d4eSYuanhan Liu 					 SOCKET_ID_ANY,
41601ad44fdSHuawei Xie 					 0, VIRTIO_PCI_VRING_ALIGN);
4176c3169a3SBruce Richardson 	if (mz == NULL) {
418abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
419abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
420abf4c84bSBernard Iremonger 		if (mz == NULL) {
42101ad44fdSHuawei Xie 			ret = -ENOMEM;
42201ad44fdSHuawei Xie 			goto fail_q_alloc;
4236c3169a3SBruce Richardson 		}
424abf4c84bSBernard Iremonger 	}
4256c3169a3SBruce Richardson 
4266c3169a3SBruce Richardson 	memset(mz->addr, 0, sizeof(mz->len));
42701ad44fdSHuawei Xie 
4286c3169a3SBruce Richardson 	vq->vq_ring_mem = mz->phys_addr;
4296c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
43001ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
43101ad44fdSHuawei Xie 		     (uint64_t)mz->phys_addr);
43201ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
43301ad44fdSHuawei Xie 		     (uint64_t)(uintptr_t)mz->addr);
4346c3169a3SBruce Richardson 
435f4d1ad15SYuanhan Liu 	virtio_init_vring(vq);
436f4d1ad15SYuanhan Liu 
43701ad44fdSHuawei Xie 	if (sz_hdr_mz) {
43873d017ddSYuanhan Liu 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
43973d017ddSYuanhan Liu 			 dev->data->port_id, vtpci_queue_idx);
44001ad44fdSHuawei Xie 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
44169c80d4eSYuanhan Liu 						     SOCKET_ID_ANY, 0,
4426dc5de3aSStephen Hemminger 						     RTE_CACHE_LINE_SIZE);
4436dc5de3aSStephen Hemminger 		if (hdr_mz == NULL) {
444abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
44501ad44fdSHuawei Xie 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
4466dc5de3aSStephen Hemminger 			if (hdr_mz == NULL) {
44701ad44fdSHuawei Xie 				ret = -ENOMEM;
44801ad44fdSHuawei Xie 				goto fail_q_alloc;
4496c3169a3SBruce Richardson 			}
450abf4c84bSBernard Iremonger 		}
45101ad44fdSHuawei Xie 	}
45201ad44fdSHuawei Xie 
45301ad44fdSHuawei Xie 	if (queue_type == VTNET_RQ) {
45401ad44fdSHuawei Xie 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
45501ad44fdSHuawei Xie 			       sizeof(vq->sw_ring[0]);
45601ad44fdSHuawei Xie 
45701ad44fdSHuawei Xie 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
45869c80d4eSYuanhan Liu 				RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
45901ad44fdSHuawei Xie 		if (!sw_ring) {
46001ad44fdSHuawei Xie 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
46101ad44fdSHuawei Xie 			ret = -ENOMEM;
46201ad44fdSHuawei Xie 			goto fail_q_alloc;
46301ad44fdSHuawei Xie 		}
46401ad44fdSHuawei Xie 
46501ad44fdSHuawei Xie 		vq->sw_ring = sw_ring;
466905a2469SYuanhan Liu 		rxvq = &vq->rxq;
46701ad44fdSHuawei Xie 		rxvq->vq = vq;
46801ad44fdSHuawei Xie 		rxvq->port_id = dev->data->port_id;
46901ad44fdSHuawei Xie 		rxvq->mz = mz;
47001ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
471905a2469SYuanhan Liu 		txvq = &vq->txq;
47201ad44fdSHuawei Xie 		txvq->vq = vq;
47301ad44fdSHuawei Xie 		txvq->port_id = dev->data->port_id;
47401ad44fdSHuawei Xie 		txvq->mz = mz;
47501ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mz = hdr_mz;
47601ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
477f24f8f9fSJianfeng Tan 	} else if (queue_type == VTNET_CQ) {
478905a2469SYuanhan Liu 		cvq = &vq->cq;
479f24f8f9fSJianfeng Tan 		cvq->vq = vq;
480f24f8f9fSJianfeng Tan 		cvq->mz = mz;
481f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mz = hdr_mz;
482f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
483f24f8f9fSJianfeng Tan 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
48469c80d4eSYuanhan Liu 
48569c80d4eSYuanhan Liu 		hw->cvq = cvq;
486f24f8f9fSJianfeng Tan 	}
487f24f8f9fSJianfeng Tan 
4886a7c0dfcSJan Blunck 	/* For virtio_user case (that is when hw->dev is NULL), we use
489f24f8f9fSJianfeng Tan 	 * virtual address. And we need properly set _offset_, please see
49025f80d10SOlivier Matz 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
491f24f8f9fSJianfeng Tan 	 */
4929470427cSYuanhan Liu 	if (!hw->virtio_user_dev)
493f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
494f24f8f9fSJianfeng Tan 	else {
495f24f8f9fSJianfeng Tan 		vq->vq_ring_mem = (uintptr_t)mz->addr;
496f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
497f24f8f9fSJianfeng Tan 		if (queue_type == VTNET_TQ)
498f24f8f9fSJianfeng Tan 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
499f24f8f9fSJianfeng Tan 		else if (queue_type == VTNET_CQ)
500f24f8f9fSJianfeng Tan 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
501f24f8f9fSJianfeng Tan 	}
502f24f8f9fSJianfeng Tan 
503f24f8f9fSJianfeng Tan 	if (queue_type == VTNET_TQ) {
504f24f8f9fSJianfeng Tan 		struct virtio_tx_region *txr;
505f24f8f9fSJianfeng Tan 		unsigned int i;
506f24f8f9fSJianfeng Tan 
5076dc5de3aSStephen Hemminger 		txr = hdr_mz->addr;
5086dc5de3aSStephen Hemminger 		memset(txr, 0, vq_size * sizeof(*txr));
5096dc5de3aSStephen Hemminger 		for (i = 0; i < vq_size; i++) {
5106dc5de3aSStephen Hemminger 			struct vring_desc *start_dp = txr[i].tx_indir;
5116dc5de3aSStephen Hemminger 
5126dc5de3aSStephen Hemminger 			vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
5136dc5de3aSStephen Hemminger 
5146dc5de3aSStephen Hemminger 			/* first indirect descriptor is always the tx header */
51501ad44fdSHuawei Xie 			start_dp->addr = txvq->virtio_net_hdr_mem
5166dc5de3aSStephen Hemminger 				+ i * sizeof(*txr)
5176dc5de3aSStephen Hemminger 				+ offsetof(struct virtio_tx_region, tx_hdr);
5186dc5de3aSStephen Hemminger 
51901ad44fdSHuawei Xie 			start_dp->len = hw->vtnet_hdr_size;
5206dc5de3aSStephen Hemminger 			start_dp->flags = VRING_DESC_F_NEXT;
5216dc5de3aSStephen Hemminger 		}
5226c3169a3SBruce Richardson 	}
5236c3169a3SBruce Richardson 
524553f4593SYuanhan Liu 	if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
525595454c5SJianfeng Tan 		PMD_INIT_LOG(ERR, "setup_queue failed");
526595454c5SJianfeng Tan 		return -EINVAL;
527595454c5SJianfeng Tan 	}
528595454c5SJianfeng Tan 
5296c3169a3SBruce Richardson 	return 0;
53001ad44fdSHuawei Xie 
53101ad44fdSHuawei Xie fail_q_alloc:
53201ad44fdSHuawei Xie 	rte_free(sw_ring);
53301ad44fdSHuawei Xie 	rte_memzone_free(hdr_mz);
53401ad44fdSHuawei Xie 	rte_memzone_free(mz);
53501ad44fdSHuawei Xie 	rte_free(vq);
53601ad44fdSHuawei Xie 
53701ad44fdSHuawei Xie 	return ret;
5386c3169a3SBruce Richardson }
5396c3169a3SBruce Richardson 
54069c80d4eSYuanhan Liu static void
54169c80d4eSYuanhan Liu virtio_free_queues(struct virtio_hw *hw)
5426c3169a3SBruce Richardson {
54369c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
54469c80d4eSYuanhan Liu 	struct virtqueue *vq;
54569c80d4eSYuanhan Liu 	int queue_type;
54669c80d4eSYuanhan Liu 	uint16_t i;
5476c3169a3SBruce Richardson 
54869c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
54969c80d4eSYuanhan Liu 		vq = hw->vqs[i];
55069c80d4eSYuanhan Liu 		if (!vq)
55169c80d4eSYuanhan Liu 			continue;
55269c80d4eSYuanhan Liu 
55369c80d4eSYuanhan Liu 		queue_type = virtio_get_queue_type(hw, i);
55469c80d4eSYuanhan Liu 		if (queue_type == VTNET_RQ) {
55569c80d4eSYuanhan Liu 			rte_free(vq->sw_ring);
55669c80d4eSYuanhan Liu 			rte_memzone_free(vq->rxq.mz);
55769c80d4eSYuanhan Liu 		} else if (queue_type == VTNET_TQ) {
55869c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.mz);
55969c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.virtio_net_hdr_mz);
56069c80d4eSYuanhan Liu 		} else {
56169c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.mz);
56269c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.virtio_net_hdr_mz);
56369c80d4eSYuanhan Liu 		}
56469c80d4eSYuanhan Liu 
56569c80d4eSYuanhan Liu 		rte_free(vq);
56669c80d4eSYuanhan Liu 	}
56769c80d4eSYuanhan Liu 
56869c80d4eSYuanhan Liu 	rte_free(hw->vqs);
56969c80d4eSYuanhan Liu }
57069c80d4eSYuanhan Liu 
57169c80d4eSYuanhan Liu static int
57269c80d4eSYuanhan Liu virtio_alloc_queues(struct rte_eth_dev *dev)
57369c80d4eSYuanhan Liu {
57469c80d4eSYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
57569c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
57669c80d4eSYuanhan Liu 	uint16_t i;
57769c80d4eSYuanhan Liu 	int ret;
57869c80d4eSYuanhan Liu 
57969c80d4eSYuanhan Liu 	hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
58069c80d4eSYuanhan Liu 	if (!hw->vqs) {
58169c80d4eSYuanhan Liu 		PMD_INIT_LOG(ERR, "failed to allocate vqs");
58269c80d4eSYuanhan Liu 		return -ENOMEM;
58369c80d4eSYuanhan Liu 	}
58469c80d4eSYuanhan Liu 
58569c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
58669c80d4eSYuanhan Liu 		ret = virtio_init_queue(dev, i);
5876c3169a3SBruce Richardson 		if (ret < 0) {
58869c80d4eSYuanhan Liu 			virtio_free_queues(hw);
5896c3169a3SBruce Richardson 			return ret;
5906c3169a3SBruce Richardson 		}
5916c3169a3SBruce Richardson 	}
5926c3169a3SBruce Richardson 
59369c80d4eSYuanhan Liu 	return 0;
594941d64b5SBernard Iremonger }
595941d64b5SBernard Iremonger 
596941d64b5SBernard Iremonger static void
5976c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
5986c3169a3SBruce Richardson {
5996c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6006c3169a3SBruce Richardson 
6016c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
6026c3169a3SBruce Richardson 
6036c3169a3SBruce Richardson 	/* reset the NIC */
60462a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
605981e61f5SJianfeng Tan 		VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
6066c3169a3SBruce Richardson 	vtpci_reset(hw);
6076c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
60869c80d4eSYuanhan Liu 	virtio_free_queues(hw);
6096c3169a3SBruce Richardson }
6106c3169a3SBruce Richardson 
6116c3169a3SBruce Richardson static void
6126c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
6136c3169a3SBruce Richardson {
6146c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6156c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6166c3169a3SBruce Richardson 	int dlen[1];
6176c3169a3SBruce Richardson 	int ret;
6186c3169a3SBruce Richardson 
619e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
620e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
621e9e414a4SStephen Hemminger 		return;
622e9e414a4SStephen Hemminger 	}
623e9e414a4SStephen Hemminger 
6246c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6256c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6266c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6276c3169a3SBruce Richardson 	dlen[0] = 1;
6286c3169a3SBruce Richardson 
6296c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6306c3169a3SBruce Richardson 	if (ret)
6316c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
6326c3169a3SBruce Richardson }
6336c3169a3SBruce Richardson 
6346c3169a3SBruce Richardson static void
6356c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
6366c3169a3SBruce Richardson {
6376c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6386c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6396c3169a3SBruce Richardson 	int dlen[1];
6406c3169a3SBruce Richardson 	int ret;
6416c3169a3SBruce Richardson 
642e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
643e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
644e9e414a4SStephen Hemminger 		return;
645e9e414a4SStephen Hemminger 	}
646e9e414a4SStephen Hemminger 
6476c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6486c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6496c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6506c3169a3SBruce Richardson 	dlen[0] = 1;
6516c3169a3SBruce Richardson 
6526c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6536c3169a3SBruce Richardson 	if (ret)
6546c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
6556c3169a3SBruce Richardson }
6566c3169a3SBruce Richardson 
6576c3169a3SBruce Richardson static void
6586c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
6596c3169a3SBruce Richardson {
6606c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6616c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6626c3169a3SBruce Richardson 	int dlen[1];
6636c3169a3SBruce Richardson 	int ret;
6646c3169a3SBruce Richardson 
665e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
666e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
667e9e414a4SStephen Hemminger 		return;
668e9e414a4SStephen Hemminger 	}
669e9e414a4SStephen Hemminger 
6706c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6716c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6726c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6736c3169a3SBruce Richardson 	dlen[0] = 1;
6746c3169a3SBruce Richardson 
6756c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6766c3169a3SBruce Richardson 	if (ret)
6776c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
6786c3169a3SBruce Richardson }
6796c3169a3SBruce Richardson 
6806c3169a3SBruce Richardson static void
6816c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
6826c3169a3SBruce Richardson {
6836c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6846c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6856c3169a3SBruce Richardson 	int dlen[1];
6866c3169a3SBruce Richardson 	int ret;
6876c3169a3SBruce Richardson 
688e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
689e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
690e9e414a4SStephen Hemminger 		return;
691e9e414a4SStephen Hemminger 	}
692e9e414a4SStephen Hemminger 
6936c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6946c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6956c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6966c3169a3SBruce Richardson 	dlen[0] = 1;
6976c3169a3SBruce Richardson 
6986c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6996c3169a3SBruce Richardson 	if (ret)
7006c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
7016c3169a3SBruce Richardson }
7026c3169a3SBruce Richardson 
7034ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
7044ec2424aSSouvik Dey static int
7054ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
7064ec2424aSSouvik Dey {
7074ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
7084ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
7094ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
7104ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
7114ec2424aSSouvik Dey 
7124ec2424aSSouvik Dey 	if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
7134ec2424aSSouvik Dey 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
7144ec2424aSSouvik Dey 			ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
7154ec2424aSSouvik Dey 		return -EINVAL;
7164ec2424aSSouvik Dey 	}
7174ec2424aSSouvik Dey 	return 0;
7184ec2424aSSouvik Dey }
7194ec2424aSSouvik Dey 
7206c3169a3SBruce Richardson /*
7216c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
7226c3169a3SBruce Richardson  */
7236c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
7246c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
7256c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
7266c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
7276c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
7286c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
7296c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
7306c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
7316c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
7324ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
7336c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
7346c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
73576d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
736baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
7376c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
73876d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
7396c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
7406c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
74169c80d4eSYuanhan Liu 	.rx_queue_release        = virtio_dev_queue_release,
742*b0caba1aSJianfeng Tan 	.rx_descriptor_done      = virtio_dev_rx_queue_done,
7436c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
74469c80d4eSYuanhan Liu 	.tx_queue_release        = virtio_dev_queue_release,
7456c3169a3SBruce Richardson 	/* collect stats per queue */
7466c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7476c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7486c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7496c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7506c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7516c3169a3SBruce Richardson };
7526c3169a3SBruce Richardson 
7536c3169a3SBruce Richardson static inline int
7546c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
7556c3169a3SBruce Richardson 				struct rte_eth_link *link)
7566c3169a3SBruce Richardson {
7576c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
7586c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
7596c3169a3SBruce Richardson 
7606c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7616c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
7626c3169a3SBruce Richardson 		return -1;
7636c3169a3SBruce Richardson 
7646c3169a3SBruce Richardson 	return 0;
7656c3169a3SBruce Richardson }
7666c3169a3SBruce Richardson 
7676c3169a3SBruce Richardson /**
7686c3169a3SBruce Richardson  * Atomically writes the link status information into global
7696c3169a3SBruce Richardson  * structure rte_eth_dev.
7706c3169a3SBruce Richardson  *
7716c3169a3SBruce Richardson  * @param dev
7726c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
7736c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
7746c3169a3SBruce Richardson  *
7756c3169a3SBruce Richardson  * @return
7766c3169a3SBruce Richardson  *   - On success, zero.
7776c3169a3SBruce Richardson  *   - On failure, negative value.
7786c3169a3SBruce Richardson  */
7796c3169a3SBruce Richardson static inline int
7806c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
7816c3169a3SBruce Richardson 		struct rte_eth_link *link)
7826c3169a3SBruce Richardson {
7836c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
7846c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
7856c3169a3SBruce Richardson 
7866c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7876c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
7886c3169a3SBruce Richardson 		return -1;
7896c3169a3SBruce Richardson 
7906c3169a3SBruce Richardson 	return 0;
7916c3169a3SBruce Richardson }
7926c3169a3SBruce Richardson 
7936c3169a3SBruce Richardson static void
79476d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
7956c3169a3SBruce Richardson {
7966c3169a3SBruce Richardson 	unsigned i;
7976c3169a3SBruce Richardson 
7986c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
79901ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
8006c3169a3SBruce Richardson 		if (txvq == NULL)
8016c3169a3SBruce Richardson 			continue;
8026c3169a3SBruce Richardson 
80301ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
80401ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
80501ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
8066c3169a3SBruce Richardson 
8076c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
80801ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
80901ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
8106c3169a3SBruce Richardson 		}
8116c3169a3SBruce Richardson 	}
8126c3169a3SBruce Richardson 
8136c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
81401ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
8156c3169a3SBruce Richardson 		if (rxvq == NULL)
8166c3169a3SBruce Richardson 			continue;
8176c3169a3SBruce Richardson 
81801ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
81901ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
82001ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
8216c3169a3SBruce Richardson 
8226c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
82301ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
82401ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
8256c3169a3SBruce Richardson 		}
8266c3169a3SBruce Richardson 	}
8276c3169a3SBruce Richardson 
8286c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
8296c3169a3SBruce Richardson }
8306c3169a3SBruce Richardson 
831baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
832baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
833baf91c39SRemy Horton 				       __rte_unused unsigned limit)
834baf91c39SRemy Horton {
835baf91c39SRemy Horton 	unsigned i;
836baf91c39SRemy Horton 	unsigned count = 0;
837baf91c39SRemy Horton 	unsigned t;
838baf91c39SRemy Horton 
83901ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
84001ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
841baf91c39SRemy Horton 
8427e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
843baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
844baf91c39SRemy Horton 
845baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
846baf91c39SRemy Horton 			struct virtqueue *rxvq = dev->data->rx_queues[i];
847baf91c39SRemy Horton 			if (rxvq == NULL)
848baf91c39SRemy Horton 				continue;
84901ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
850baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
851baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
852baf91c39SRemy Horton 					"rx_q%u_%s", i,
85301ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
854baf91c39SRemy Horton 				count++;
855baf91c39SRemy Horton 			}
856baf91c39SRemy Horton 		}
857baf91c39SRemy Horton 
858baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
859baf91c39SRemy Horton 			struct virtqueue *txvq = dev->data->tx_queues[i];
860baf91c39SRemy Horton 			if (txvq == NULL)
861baf91c39SRemy Horton 				continue;
86201ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
863baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
864baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
865baf91c39SRemy Horton 					"tx_q%u_%s", i,
86601ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
867baf91c39SRemy Horton 				count++;
868baf91c39SRemy Horton 			}
869baf91c39SRemy Horton 		}
870baf91c39SRemy Horton 		return count;
871baf91c39SRemy Horton 	}
872baf91c39SRemy Horton 	return nstats;
873baf91c39SRemy Horton }
874baf91c39SRemy Horton 
87576d4c652SHarry van Haaren static int
876e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
87776d4c652SHarry van Haaren 		      unsigned n)
87876d4c652SHarry van Haaren {
87976d4c652SHarry van Haaren 	unsigned i;
88076d4c652SHarry van Haaren 	unsigned count = 0;
88176d4c652SHarry van Haaren 
88201ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
88301ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
88476d4c652SHarry van Haaren 
88576d4c652SHarry van Haaren 	if (n < nstats)
88676d4c652SHarry van Haaren 		return nstats;
88776d4c652SHarry van Haaren 
88876d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
88901ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
89076d4c652SHarry van Haaren 
89176d4c652SHarry van Haaren 		if (rxvq == NULL)
89276d4c652SHarry van Haaren 			continue;
89376d4c652SHarry van Haaren 
89476d4c652SHarry van Haaren 		unsigned t;
89576d4c652SHarry van Haaren 
89601ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
89776d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
89801ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
899513c78aeSOlivier Matz 			xstats[count].id = count;
90076d4c652SHarry van Haaren 			count++;
90176d4c652SHarry van Haaren 		}
90276d4c652SHarry van Haaren 	}
90376d4c652SHarry van Haaren 
90476d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
90501ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
90676d4c652SHarry van Haaren 
90776d4c652SHarry van Haaren 		if (txvq == NULL)
90876d4c652SHarry van Haaren 			continue;
90976d4c652SHarry van Haaren 
91076d4c652SHarry van Haaren 		unsigned t;
91176d4c652SHarry van Haaren 
91201ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
91376d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
91401ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
915513c78aeSOlivier Matz 			xstats[count].id = count;
91676d4c652SHarry van Haaren 			count++;
91776d4c652SHarry van Haaren 		}
91876d4c652SHarry van Haaren 	}
91976d4c652SHarry van Haaren 
92076d4c652SHarry van Haaren 	return count;
92176d4c652SHarry van Haaren }
92276d4c652SHarry van Haaren 
92376d4c652SHarry van Haaren static void
92476d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
92576d4c652SHarry van Haaren {
92676d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
92776d4c652SHarry van Haaren }
92876d4c652SHarry van Haaren 
9296c3169a3SBruce Richardson static void
9306c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
9316c3169a3SBruce Richardson {
9326c3169a3SBruce Richardson 	unsigned int i;
9336c3169a3SBruce Richardson 
9346c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
93501ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
9366c3169a3SBruce Richardson 		if (txvq == NULL)
9376c3169a3SBruce Richardson 			continue;
9386c3169a3SBruce Richardson 
93901ad44fdSHuawei Xie 		txvq->stats.packets = 0;
94001ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
94101ad44fdSHuawei Xie 		txvq->stats.errors = 0;
94201ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
94301ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
94401ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
94501ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
9466c3169a3SBruce Richardson 	}
9476c3169a3SBruce Richardson 
9486c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
94901ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9506c3169a3SBruce Richardson 		if (rxvq == NULL)
9516c3169a3SBruce Richardson 			continue;
9526c3169a3SBruce Richardson 
95301ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
95401ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
95501ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
95601ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
95701ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
95801ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
95901ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
9606c3169a3SBruce Richardson 	}
9616c3169a3SBruce Richardson }
9626c3169a3SBruce Richardson 
9636c3169a3SBruce Richardson static void
9646c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
9656c3169a3SBruce Richardson {
9666c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
9676c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9686c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9696c3169a3SBruce Richardson }
9706c3169a3SBruce Richardson 
9716c3169a3SBruce Richardson static void
9726c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
9736c3169a3SBruce Richardson {
9746c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
9756c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
9766c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9776c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9786c3169a3SBruce Richardson 	} else {
9796c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
9806c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
9816c3169a3SBruce Richardson 	}
9826c3169a3SBruce Richardson }
9836c3169a3SBruce Richardson 
98427046236SStephen Hemminger static void
9856c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
9866c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
9876c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
9886c3169a3SBruce Richardson {
9896c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
9906c3169a3SBruce Richardson 	int err, len[2];
9916c3169a3SBruce Richardson 
99227046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
993e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
99427046236SStephen Hemminger 		return;
99527046236SStephen Hemminger 	}
99627046236SStephen Hemminger 
9976c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
9986c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
9996c3169a3SBruce Richardson 
10006c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
10016c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
10026c3169a3SBruce Richardson 
10036c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
10046c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
10056c3169a3SBruce Richardson 
10066c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
10076c3169a3SBruce Richardson 	if (err != 0)
10086c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
10096c3169a3SBruce Richardson }
10106c3169a3SBruce Richardson 
10116c3169a3SBruce Richardson static void
10126c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
10136c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
10146c3169a3SBruce Richardson {
10156c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10166c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
10176c3169a3SBruce Richardson 	unsigned int i;
10186c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10196c3169a3SBruce Richardson 
10206c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10216c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10226c3169a3SBruce Richardson 		return;
10236c3169a3SBruce Richardson 	}
10246c3169a3SBruce Richardson 
10256c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10266c3169a3SBruce Richardson 	uc->entries = 0;
10276c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10286c3169a3SBruce Richardson 	mc->entries = 0;
10296c3169a3SBruce Richardson 
10306c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10316c3169a3SBruce Richardson 		const struct ether_addr *addr
10326c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
10336c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
10346c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
10356c3169a3SBruce Richardson 
10366c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
10376c3169a3SBruce Richardson 	}
10386c3169a3SBruce Richardson 
10396c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10406c3169a3SBruce Richardson }
10416c3169a3SBruce Richardson 
10426c3169a3SBruce Richardson static void
10436c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
10446c3169a3SBruce Richardson {
10456c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10466c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
10476c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10486c3169a3SBruce Richardson 	unsigned int i;
10496c3169a3SBruce Richardson 
10506c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10516c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10526c3169a3SBruce Richardson 		return;
10536c3169a3SBruce Richardson 	}
10546c3169a3SBruce Richardson 
10556c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10566c3169a3SBruce Richardson 	uc->entries = 0;
10576c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10586c3169a3SBruce Richardson 	mc->entries = 0;
10596c3169a3SBruce Richardson 
10606c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10616c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
10626c3169a3SBruce Richardson 
10636c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
10646c3169a3SBruce Richardson 			continue;
10656c3169a3SBruce Richardson 
10666c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
10676c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
10686c3169a3SBruce Richardson 	}
10696c3169a3SBruce Richardson 
10706c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10716c3169a3SBruce Richardson }
10726c3169a3SBruce Richardson 
10736c3169a3SBruce Richardson static void
10746c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
10756c3169a3SBruce Richardson {
10766c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10776c3169a3SBruce Richardson 
10786c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
10796c3169a3SBruce Richardson 
10806c3169a3SBruce Richardson 	/* Use atomic update if available */
10816c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
10826c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
10836c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
10846c3169a3SBruce Richardson 
10856c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10866c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
10876c3169a3SBruce Richardson 
10886c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
10896c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
10906c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
10916c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10926c3169a3SBruce Richardson }
10936c3169a3SBruce Richardson 
10946c3169a3SBruce Richardson static int
10956c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
10966c3169a3SBruce Richardson {
10976c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10986c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10996c3169a3SBruce Richardson 	int len;
11006c3169a3SBruce Richardson 
11016c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
11026c3169a3SBruce Richardson 		return -ENOTSUP;
11036c3169a3SBruce Richardson 
11046c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
11056c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
11066c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
11076c3169a3SBruce Richardson 	len = sizeof(vlan_id);
11086c3169a3SBruce Richardson 
11096c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
11106c3169a3SBruce Richardson }
11116c3169a3SBruce Richardson 
11126ba1f63bSYuanhan Liu static int
111360e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
11146c3169a3SBruce Richardson {
11153891f233SYuanhan Liu 	uint64_t host_features;
11166c3169a3SBruce Richardson 
11176c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
11183891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
111960e6f470SOlivier Matz 		req_features);
11206c3169a3SBruce Richardson 
11216c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1122553f4593SYuanhan Liu 	host_features = VTPCI_OPS(hw)->get_features(hw);
11233891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
11246c3169a3SBruce Richardson 		host_features);
11256c3169a3SBruce Richardson 
11266c3169a3SBruce Richardson 	/*
11276c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
11286c3169a3SBruce Richardson 	 * guest feature bits.
11296c3169a3SBruce Richardson 	 */
113060e6f470SOlivier Matz 	hw->guest_features = req_features;
11316c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
11323891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
11336c3169a3SBruce Richardson 		hw->guest_features);
11346ba1f63bSYuanhan Liu 
11356ba1f63bSYuanhan Liu 	if (hw->modern) {
11366ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
11376ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11386ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
11396ba1f63bSYuanhan Liu 			return -1;
11406ba1f63bSYuanhan Liu 		}
11416ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
11426ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
11436ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11446ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
11456ba1f63bSYuanhan Liu 			return -1;
11466ba1f63bSYuanhan Liu 		}
11476ba1f63bSYuanhan Liu 	}
11486ba1f63bSYuanhan Liu 
114960e6f470SOlivier Matz 	hw->req_guest_features = req_features;
115060e6f470SOlivier Matz 
11516ba1f63bSYuanhan Liu 	return 0;
11526c3169a3SBruce Richardson }
11536c3169a3SBruce Richardson 
11546c3169a3SBruce Richardson /*
11556c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
11566c3169a3SBruce Richardson  * if link state changed.
11576c3169a3SBruce Richardson  */
11586c3169a3SBruce Richardson static void
1159f5880e1fSJan Blunck virtio_interrupt_handler(struct rte_intr_handle *handle,
11606c3169a3SBruce Richardson 			 void *param)
11616c3169a3SBruce Richardson {
11626c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
11636c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11646c3169a3SBruce Richardson 	uint8_t isr;
11656c3169a3SBruce Richardson 
11666c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
11676c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
11686c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
11696c3169a3SBruce Richardson 
1170f5880e1fSJan Blunck 	if (rte_intr_enable(handle) < 0)
11716c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
11726c3169a3SBruce Richardson 
11736c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
11746c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
11756c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1176c1ceaf3aSBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC, NULL);
11776c3169a3SBruce Richardson 	}
11786c3169a3SBruce Richardson 
11796c3169a3SBruce Richardson }
11806c3169a3SBruce Richardson 
11816c3169a3SBruce Richardson static void
11826c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
11836c3169a3SBruce Richardson {
11846c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11856c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
11866c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
11876c3169a3SBruce Richardson 	else
11886c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
11896c3169a3SBruce Richardson }
11906c3169a3SBruce Richardson 
119160e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1192198ab336SOlivier Matz static int
119360e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
11946c3169a3SBruce Richardson {
11956c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11966c3169a3SBruce Richardson 	struct virtio_net_config *config;
11976c3169a3SBruce Richardson 	struct virtio_net_config local_config;
11989470427cSYuanhan Liu 	struct rte_pci_device *pci_dev = NULL;
119969c80d4eSYuanhan Liu 	int ret;
12006c3169a3SBruce Richardson 
12016c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
12026c3169a3SBruce Richardson 	vtpci_reset(hw);
12036c3169a3SBruce Richardson 
12046c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
12056c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
12066c3169a3SBruce Richardson 
12076c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
12086c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
120960e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
12106ba1f63bSYuanhan Liu 		return -1;
12116c3169a3SBruce Richardson 
1212f229eb41SJianfeng Tan 	if (eth_dev->device) {
1213f229eb41SJianfeng Tan 		pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
1214f229eb41SJianfeng Tan 		rte_eth_copy_pci_info(eth_dev, pci_dev);
1215f229eb41SJianfeng Tan 	}
1216f229eb41SJianfeng Tan 
1217954ea115SStephen Hemminger 	/* If host does not support status then disable LSC */
1218954ea115SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1219198ab336SOlivier Matz 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1220198ab336SOlivier Matz 	else
1221198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1222954ea115SStephen Hemminger 
12236c3169a3SBruce Richardson 	rx_func_get(eth_dev);
12246c3169a3SBruce Richardson 
12256c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
12266ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
12276ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
12286c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
12296c3169a3SBruce Richardson 	else
12306c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
12316c3169a3SBruce Richardson 
12326c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
12336c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
12346c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
12356c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
12366c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
12376c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12386c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
12396c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
12406c3169a3SBruce Richardson 
12416c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
12426c3169a3SBruce Richardson 		config = &local_config;
12436c3169a3SBruce Richardson 
12446d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
12456d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
12466d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
12476d7740e2SChangchun Ouyang 
12486c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
12496d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12506d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
12516d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
12526c3169a3SBruce Richardson 		} else {
12536c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12546c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
12556c3169a3SBruce Richardson 			config->status = 0;
12566c3169a3SBruce Richardson 		}
12576c3169a3SBruce Richardson 
12586c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
12596d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12606d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
12616d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
12626d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
12636c3169a3SBruce Richardson 		} else {
12646c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12656c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
12666c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
12676c3169a3SBruce Richardson 		}
12686c3169a3SBruce Richardson 
126945e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
12706c3169a3SBruce Richardson 
12716c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
12726c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
12736c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
12746c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
12756c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12766c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
12776c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
12786c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
12796c3169a3SBruce Richardson 	} else {
128045e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
128145e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
12826c3169a3SBruce Richardson 	}
12836c3169a3SBruce Richardson 
128469c80d4eSYuanhan Liu 	ret = virtio_alloc_queues(eth_dev);
128569c80d4eSYuanhan Liu 	if (ret < 0)
128669c80d4eSYuanhan Liu 		return ret;
1287ee85024cSYuanhan Liu 	vtpci_reinit_complete(hw);
128869c80d4eSYuanhan Liu 
1289ce2eabddSJianfeng Tan 	if (pci_dev)
12906c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
12916c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
12926c3169a3SBruce Richardson 			pci_dev->id.device_id);
12936c3169a3SBruce Richardson 
1294198ab336SOlivier Matz 	return 0;
1295198ab336SOlivier Matz }
1296198ab336SOlivier Matz 
1297198ab336SOlivier Matz /*
12986d890f8aSYuanhan Liu  * Remap the PCI device again (IO port map for legacy device and
12996d890f8aSYuanhan Liu  * memory map for modern device), so that the secondary process
13006d890f8aSYuanhan Liu  * could have the PCI initiated correctly.
13016d890f8aSYuanhan Liu  */
13026d890f8aSYuanhan Liu static int
13036d890f8aSYuanhan Liu virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
13046d890f8aSYuanhan Liu {
13056d890f8aSYuanhan Liu 	if (hw->modern) {
13066d890f8aSYuanhan Liu 		/*
13076d890f8aSYuanhan Liu 		 * We don't have to re-parse the PCI config space, since
13086d890f8aSYuanhan Liu 		 * rte_eal_pci_map_device() makes sure the mapped address
13096d890f8aSYuanhan Liu 		 * in secondary process would equal to the one mapped in
13106d890f8aSYuanhan Liu 		 * the primary process: error will be returned if that
13116d890f8aSYuanhan Liu 		 * requirement is not met.
13126d890f8aSYuanhan Liu 		 *
13136d890f8aSYuanhan Liu 		 * That said, we could simply reuse all cap pointers
13146d890f8aSYuanhan Liu 		 * (such as dev_cfg, common_cfg, etc.) parsed from the
13156d890f8aSYuanhan Liu 		 * primary process, which is stored in shared memory.
13166d890f8aSYuanhan Liu 		 */
13176d890f8aSYuanhan Liu 		if (rte_eal_pci_map_device(pci_dev)) {
13186d890f8aSYuanhan Liu 			PMD_INIT_LOG(DEBUG, "failed to map pci device!");
13196d890f8aSYuanhan Liu 			return -1;
13206d890f8aSYuanhan Liu 		}
13216d890f8aSYuanhan Liu 	} else {
13226d890f8aSYuanhan Liu 		if (rte_eal_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
13236d890f8aSYuanhan Liu 			return -1;
13246d890f8aSYuanhan Liu 	}
13256d890f8aSYuanhan Liu 
13266d890f8aSYuanhan Liu 	return 0;
13276d890f8aSYuanhan Liu }
13286d890f8aSYuanhan Liu 
13296d890f8aSYuanhan Liu static void
13306d890f8aSYuanhan Liu virtio_set_vtpci_ops(struct virtio_hw *hw)
13316d890f8aSYuanhan Liu {
13326d890f8aSYuanhan Liu 	if (hw->virtio_user_dev)
13336d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &virtio_user_ops;
13346d890f8aSYuanhan Liu 	else if (hw->modern)
13356d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &modern_ops;
13366d890f8aSYuanhan Liu 	else
13376d890f8aSYuanhan Liu 		VTPCI_OPS(hw) = &legacy_ops;
13386d890f8aSYuanhan Liu }
13396d890f8aSYuanhan Liu 
13406d890f8aSYuanhan Liu /*
1341198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1342198ab336SOlivier Matz  * It returns 0 on success.
1343198ab336SOlivier Matz  */
1344198ab336SOlivier Matz int
1345198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1346198ab336SOlivier Matz {
1347198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1348198ab336SOlivier Matz 	uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1349198ab336SOlivier Matz 	int ret;
1350198ab336SOlivier Matz 
1351198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1352198ab336SOlivier Matz 
1353198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1354198ab336SOlivier Matz 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1355198ab336SOlivier Matz 
1356198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
13576d890f8aSYuanhan Liu 		if (!hw->virtio_user_dev) {
13586d890f8aSYuanhan Liu 			ret = virtio_remap_pci(RTE_DEV_TO_PCI(eth_dev->device),
13596d890f8aSYuanhan Liu 					       hw);
13606d890f8aSYuanhan Liu 			if (ret)
13616d890f8aSYuanhan Liu 				return ret;
13626d890f8aSYuanhan Liu 		}
13636d890f8aSYuanhan Liu 
13646d890f8aSYuanhan Liu 		virtio_set_vtpci_ops(hw);
1365d4be35a9SYuanhan Liu 		if (hw->use_simple_rxtx) {
1366d4be35a9SYuanhan Liu 			eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple;
1367d4be35a9SYuanhan Liu 			eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1368d4be35a9SYuanhan Liu 		} else {
1369198ab336SOlivier Matz 			rx_func_get(eth_dev);
1370d4be35a9SYuanhan Liu 		}
1371198ab336SOlivier Matz 		return 0;
1372198ab336SOlivier Matz 	}
1373198ab336SOlivier Matz 
1374198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1375198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1376198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1377198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1378198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1379198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1380198ab336SOlivier Matz 		return -ENOMEM;
1381198ab336SOlivier Matz 	}
1382198ab336SOlivier Matz 
13836a7c0dfcSJan Blunck 	/* For virtio_user case the hw->virtio_user_dev is populated by
13846a7c0dfcSJan Blunck 	 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
13856a7c0dfcSJan Blunck 	 */
13866a7c0dfcSJan Blunck 	if (!hw->virtio_user_dev) {
1387eac901ceSJan Blunck 		ret = vtpci_init(RTE_DEV_TO_PCI(eth_dev->device), hw,
1388eac901ceSJan Blunck 				 &dev_flags);
1389198ab336SOlivier Matz 		if (ret)
1390198ab336SOlivier Matz 			return ret;
1391198ab336SOlivier Matz 	}
1392198ab336SOlivier Matz 
1393553f4593SYuanhan Liu 	hw->port_id = eth_dev->data->port_id;
1394198ab336SOlivier Matz 	eth_dev->data->dev_flags = dev_flags;
1395198ab336SOlivier Matz 
139660e6f470SOlivier Matz 	/* reset device and negotiate default features */
139796cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1398198ab336SOlivier Matz 	if (ret < 0)
1399198ab336SOlivier Matz 		return ret;
1400198ab336SOlivier Matz 
14016c3169a3SBruce Richardson 	/* Setup interrupt callback  */
140262a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
140361e3ee17SYuanhan Liu 		rte_intr_callback_register(eth_dev->intr_handle,
14046c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
14056c3169a3SBruce Richardson 
14066c3169a3SBruce Richardson 	return 0;
14076c3169a3SBruce Richardson }
14086c3169a3SBruce Richardson 
1409abf4c84bSBernard Iremonger static int
1410abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1411abf4c84bSBernard Iremonger {
1412abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1413abf4c84bSBernard Iremonger 
1414abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1415abf4c84bSBernard Iremonger 		return -EPERM;
1416abf4c84bSBernard Iremonger 
14173f3b3d54SYuanhan Liu 	virtio_dev_stop(eth_dev);
1418abf4c84bSBernard Iremonger 	virtio_dev_close(eth_dev);
1419abf4c84bSBernard Iremonger 
1420abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1421abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1422abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1423abf4c84bSBernard Iremonger 
1424abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1425abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1426abf4c84bSBernard Iremonger 
1427abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
142862a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
142961e3ee17SYuanhan Liu 		rte_intr_callback_unregister(eth_dev->intr_handle,
1430abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1431abf4c84bSBernard Iremonger 						eth_dev);
14329470427cSYuanhan Liu 	if (eth_dev->device)
14339470427cSYuanhan Liu 		rte_eal_pci_unmap_device(RTE_DEV_TO_PCI(eth_dev->device));
1434abf4c84bSBernard Iremonger 
1435abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1436abf4c84bSBernard Iremonger 
1437abf4c84bSBernard Iremonger 	return 0;
1438abf4c84bSBernard Iremonger }
1439abf4c84bSBernard Iremonger 
14406c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
14416c52c126SStephen Hemminger 	.pci_drv = {
1442cd433ce5SDavid Marchand 		.driver = {
1443cd433ce5SDavid Marchand 			.name = "net_virtio",
1444cd433ce5SDavid Marchand 		},
14456c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
144622dda618SBen Walker 		.drv_flags = 0,
1447c830cb29SDavid Marchand 		.probe = rte_eth_dev_pci_probe,
1448c830cb29SDavid Marchand 		.remove = rte_eth_dev_pci_remove,
14496c3169a3SBruce Richardson 	},
14506c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1451abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
14526c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
14536c3169a3SBruce Richardson };
14546c3169a3SBruce Richardson 
1455c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1456c830cb29SDavid Marchand static void
1457c830cb29SDavid Marchand rte_virtio_pmd_init(void)
14586c3169a3SBruce Richardson {
14596c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
14606c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1461c830cb29SDavid Marchand 		return;
14626c3169a3SBruce Richardson 	}
14636c3169a3SBruce Richardson 
1464c830cb29SDavid Marchand 	rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
14656c3169a3SBruce Richardson }
14666c3169a3SBruce Richardson 
14676c3169a3SBruce Richardson /*
14686c3169a3SBruce Richardson  * Configure virtio device
14696c3169a3SBruce Richardson  * It returns 0 on success.
14706c3169a3SBruce Richardson  */
14716c3169a3SBruce Richardson static int
14726c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
14736c3169a3SBruce Richardson {
14746c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
14756c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
147660e6f470SOlivier Matz 	uint64_t req_features;
147745e4acd4SOlivier Matz 	int ret;
14786c3169a3SBruce Richardson 
14796c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
148096cb6711SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
148196cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum)
148296cb6711SOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
148386d59b21SOlivier Matz 	if (rxmode->enable_lro)
148486d59b21SOlivier Matz 		req_features |=
148586d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
148686d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
14876c3169a3SBruce Richardson 
148860e6f470SOlivier Matz 	/* if request features changed, reinit the device */
148960e6f470SOlivier Matz 	if (req_features != hw->req_guest_features) {
149060e6f470SOlivier Matz 		ret = virtio_init_device(dev, req_features);
149160e6f470SOlivier Matz 		if (ret < 0)
149260e6f470SOlivier Matz 			return ret;
149360e6f470SOlivier Matz 	}
149460e6f470SOlivier Matz 
149596cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum &&
149696cb6711SOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
149796cb6711SOlivier Matz 		PMD_DRV_LOG(NOTICE,
149896cb6711SOlivier Matz 			"rx ip checksum not available on this host");
149996cb6711SOlivier Matz 		return -ENOTSUP;
150096cb6711SOlivier Matz 	}
150196cb6711SOlivier Matz 
150286d59b21SOlivier Matz 	if (rxmode->enable_lro &&
150386d59b21SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
150486d59b21SOlivier Matz 			!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
150586d59b21SOlivier Matz 		PMD_DRV_LOG(NOTICE,
150686d59b21SOlivier Matz 			"lro not available on this host");
150786d59b21SOlivier Matz 		return -ENOTSUP;
150886d59b21SOlivier Matz 	}
150986d59b21SOlivier Matz 
151069c80d4eSYuanhan Liu 	/* start control queue */
151169c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
151245e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
151345e4acd4SOlivier Matz 
15146c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
15156c3169a3SBruce Richardson 
15166c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
15176c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
15186c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
15196c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
15206c3169a3SBruce Richardson 		return -ENOTSUP;
15216c3169a3SBruce Richardson 	}
15226c3169a3SBruce Richardson 
152362a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1524981e61f5SJianfeng Tan 		/* Enable vector (0) for Link State Intrerrupt */
1525981e61f5SJianfeng Tan 		if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
1526981e61f5SJianfeng Tan 				VIRTIO_MSI_NO_VECTOR) {
15276c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
15286c3169a3SBruce Richardson 			return -EBUSY;
15296c3169a3SBruce Richardson 		}
15306c3169a3SBruce Richardson 
15316c3169a3SBruce Richardson 	return 0;
15326c3169a3SBruce Richardson }
15336c3169a3SBruce Richardson 
15346c3169a3SBruce Richardson 
15356c3169a3SBruce Richardson static int
15366c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
15376c3169a3SBruce Richardson {
15386c3169a3SBruce Richardson 	uint16_t nb_queues, i;
153901ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
154001ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
1541663c7679SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
15426c3169a3SBruce Richardson 
15436c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1544954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
154562a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
15466c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
15476c3169a3SBruce Richardson 			return -ENOTSUP;
15486c3169a3SBruce Richardson 		}
15496c3169a3SBruce Richardson 
155061e3ee17SYuanhan Liu 		if (rte_intr_enable(dev->intr_handle) < 0) {
15516c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
15526c3169a3SBruce Richardson 			return -EIO;
15536c3169a3SBruce Richardson 		}
15546c3169a3SBruce Richardson 	}
15556c3169a3SBruce Richardson 
15566c3169a3SBruce Richardson 	/* Initialize Link state */
15576c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
15586c3169a3SBruce Richardson 
15596c3169a3SBruce Richardson 	/*Notify the backend
15606c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
15616c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
15626c3169a3SBruce Richardson 	 */
156360d4a353SYuanhan Liu 	nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1564663c7679SYuanhan Liu 	if (hw->max_queue_pairs > 1) {
15656c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
15666c3169a3SBruce Richardson 			return -EINVAL;
15676c3169a3SBruce Richardson 	}
15686c3169a3SBruce Richardson 
15696c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
15706c3169a3SBruce Richardson 
157160d4a353SYuanhan Liu 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
157201ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
157301ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
157401ad44fdSHuawei Xie 	}
15756c3169a3SBruce Richardson 
15766c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
15776c3169a3SBruce Richardson 
157801ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
157901ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
158001ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
158101ad44fdSHuawei Xie 	}
15826c3169a3SBruce Richardson 
158301ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
158401ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
158501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
158601ad44fdSHuawei Xie 	}
15876c3169a3SBruce Richardson 
15886c3169a3SBruce Richardson 	return 0;
15896c3169a3SBruce Richardson }
15906c3169a3SBruce Richardson 
15916c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
15926c3169a3SBruce Richardson {
15936c3169a3SBruce Richardson 	struct rte_mbuf *buf;
15946c3169a3SBruce Richardson 	int i, mbuf_num = 0;
15956c3169a3SBruce Richardson 
15966c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
159701ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
159801ad44fdSHuawei Xie 
15996c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
16006c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
160101ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
16026c3169a3SBruce Richardson 
160301ad44fdSHuawei Xie 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
160401ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
16056c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
16066c3169a3SBruce Richardson 			mbuf_num++;
16076c3169a3SBruce Richardson 		}
16086c3169a3SBruce Richardson 
16096c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
16106c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
16116c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
161201ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
16136c3169a3SBruce Richardson 	}
16146c3169a3SBruce Richardson 
16156c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
161601ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
161701ad44fdSHuawei Xie 
16186c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
16196c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
16206c3169a3SBruce Richardson 			     i);
162101ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
16226c3169a3SBruce Richardson 
16236c3169a3SBruce Richardson 		mbuf_num = 0;
162401ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
16256c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
16266c3169a3SBruce Richardson 			mbuf_num++;
16276c3169a3SBruce Richardson 		}
16286c3169a3SBruce Richardson 
16296c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
16306c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
16316c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
163201ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
16336c3169a3SBruce Richardson 	}
16346c3169a3SBruce Richardson }
16356c3169a3SBruce Richardson 
16366c3169a3SBruce Richardson /*
16376c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
16386c3169a3SBruce Richardson  */
16396c3169a3SBruce Richardson static void
16406c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
16416c3169a3SBruce Richardson {
16426c3169a3SBruce Richardson 	struct rte_eth_link link;
16436c3169a3SBruce Richardson 
16446c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
16456c3169a3SBruce Richardson 
16466c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
164761e3ee17SYuanhan Liu 		rte_intr_disable(dev->intr_handle);
16486c3169a3SBruce Richardson 
16496c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
16506c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
16516c3169a3SBruce Richardson }
16526c3169a3SBruce Richardson 
16536c3169a3SBruce Richardson static int
16546c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
16556c3169a3SBruce Richardson {
16566c3169a3SBruce Richardson 	struct rte_eth_link link, old;
16576c3169a3SBruce Richardson 	uint16_t status;
16586c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
16596c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
16606c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
16616c3169a3SBruce Richardson 	old = link;
166211319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
16636c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
16646c3169a3SBruce Richardson 
16656c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
16666c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
16676c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
16686c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
16696c3169a3SBruce Richardson 				&status, sizeof(status));
16706c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
167109419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
16726c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
16736c3169a3SBruce Richardson 				     dev->data->port_id);
16746c3169a3SBruce Richardson 		} else {
167509419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
16766c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
16776c3169a3SBruce Richardson 				     dev->data->port_id);
16786c3169a3SBruce Richardson 		}
16796c3169a3SBruce Richardson 	} else {
168009419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
16816c3169a3SBruce Richardson 	}
16826c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
16836c3169a3SBruce Richardson 
16846c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
16856c3169a3SBruce Richardson }
16866c3169a3SBruce Richardson 
16876c3169a3SBruce Richardson static void
16886c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
16896c3169a3SBruce Richardson {
169069657304SOlivier Matz 	uint64_t tso_mask;
16916c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
16926c3169a3SBruce Richardson 
16939470427cSYuanhan Liu 	dev_info->pci_dev = dev->device ? RTE_DEV_TO_PCI(dev->device) : NULL;
169445e4acd4SOlivier Matz 	dev_info->max_rx_queues =
169545e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
169645e4acd4SOlivier Matz 	dev_info->max_tx_queues =
169745e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
16986c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
16996c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
17006c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
17016c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
17026c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
17036c3169a3SBruce Richardson 	};
170496cb6711SOlivier Matz 	dev_info->rx_offload_capa =
170596cb6711SOlivier Matz 		DEV_RX_OFFLOAD_TCP_CKSUM |
170686d59b21SOlivier Matz 		DEV_RX_OFFLOAD_UDP_CKSUM |
170786d59b21SOlivier Matz 		DEV_RX_OFFLOAD_TCP_LRO;
170858169a9cSOlivier Matz 	dev_info->tx_offload_capa = 0;
170958169a9cSOlivier Matz 
171058169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
171158169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
171258169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
171358169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
171458169a9cSOlivier Matz 	}
171569657304SOlivier Matz 
171669657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
171769657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
171869657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
171969657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
17206c3169a3SBruce Richardson }
17216c3169a3SBruce Richardson 
17226c3169a3SBruce Richardson /*
17236c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
17246c3169a3SBruce Richardson  */
17256c3169a3SBruce Richardson static int
17266c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
17276c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
17286c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
17296c3169a3SBruce Richardson {
17306c3169a3SBruce Richardson 	return 0;
17316c3169a3SBruce Richardson }
17326c3169a3SBruce Richardson 
173301f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
173401f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
17350880c401SOlivier Matz RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio");
1736