xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision f5880e1f)
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 
1556c3169a3SBruce Richardson static int
15601ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
1576c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1586c3169a3SBruce Richardson {
1599e71668bSDamjan Marion 	uint32_t head, i;
1606c3169a3SBruce Richardson 	int k, sum = 0;
1616c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
1626c3169a3SBruce Richardson 	struct virtio_pmd_ctrl result;
16301ad44fdSHuawei Xie 	struct virtqueue *vq;
1646c3169a3SBruce Richardson 
1656c3169a3SBruce Richardson 	ctrl->status = status;
1666c3169a3SBruce Richardson 
1673bd60a27SJianfeng Tan 	if (!cvq || !cvq->vq) {
1680bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "Control queue is not supported.");
1696c3169a3SBruce Richardson 		return -1;
1706c3169a3SBruce Richardson 	}
17101ad44fdSHuawei Xie 	vq = cvq->vq;
1729e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1736c3169a3SBruce Richardson 
1746c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1756c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1766c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1776c3169a3SBruce Richardson 
1786c3169a3SBruce Richardson 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
1796c3169a3SBruce Richardson 		return -1;
1806c3169a3SBruce Richardson 
18101ad44fdSHuawei Xie 	memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
1826c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1836c3169a3SBruce Richardson 
1846c3169a3SBruce Richardson 	/*
1856c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1866c3169a3SBruce Richardson 	 * One TX packet for header;
1876c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1886c3169a3SBruce Richardson 	 * One RX packet for ACK.
1896c3169a3SBruce Richardson 	 */
1906c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
191f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem;
1926c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1936c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1946c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
1956c3169a3SBruce Richardson 
1966c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
1976c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
198f24f8f9fSJianfeng Tan 		vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
1996c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
2006c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
2016c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
2026c3169a3SBruce Richardson 		sum += dlen[k];
2036c3169a3SBruce Richardson 		vq->vq_free_cnt--;
2046c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
2056c3169a3SBruce Richardson 	}
2066c3169a3SBruce Richardson 
2076c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
208f24f8f9fSJianfeng Tan 	vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem
2096c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
2106c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
2116c3169a3SBruce Richardson 	vq->vq_free_cnt--;
2126c3169a3SBruce Richardson 
2136c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
2146c3169a3SBruce Richardson 
2156c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
2166c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
2176c3169a3SBruce Richardson 
2186c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
2196c3169a3SBruce Richardson 
2206c3169a3SBruce Richardson 	virtqueue_notify(vq);
2216c3169a3SBruce Richardson 
2226c3169a3SBruce Richardson 	rte_rmb();
223b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq) == 0) {
2246c3169a3SBruce Richardson 		rte_rmb();
2256c3169a3SBruce Richardson 		usleep(100);
2266c3169a3SBruce Richardson 	}
2276c3169a3SBruce Richardson 
228b81026f1SHuawei Xie 	while (VIRTQUEUE_NUSED(vq)) {
2296c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
2306c3169a3SBruce Richardson 		struct vring_used_elem *uep;
2316c3169a3SBruce Richardson 
2326c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
2336c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
2346c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
2356c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
2366c3169a3SBruce Richardson 		desc_idx = idx;
2376c3169a3SBruce Richardson 
2386c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
2396c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
2406c3169a3SBruce Richardson 			vq->vq_free_cnt++;
2416c3169a3SBruce Richardson 		}
2426c3169a3SBruce Richardson 
2436c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2446c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2456c3169a3SBruce Richardson 
2466c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2476c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2486c3169a3SBruce Richardson 	}
2496c3169a3SBruce Richardson 
2506c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2516c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2526c3169a3SBruce Richardson 
25301ad44fdSHuawei Xie 	memcpy(&result, cvq->virtio_net_hdr_mz->addr,
2546c3169a3SBruce Richardson 			sizeof(struct virtio_pmd_ctrl));
2556c3169a3SBruce Richardson 
2566c3169a3SBruce Richardson 	return result.status;
2576c3169a3SBruce Richardson }
2586c3169a3SBruce Richardson 
2596c3169a3SBruce Richardson static int
2606c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2616c3169a3SBruce Richardson {
2626c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2636c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2646c3169a3SBruce Richardson 	int dlen[1];
2656c3169a3SBruce Richardson 	int ret;
2666c3169a3SBruce Richardson 
2676c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2686c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2696c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2706c3169a3SBruce Richardson 
2716c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2726c3169a3SBruce Richardson 
2736c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2746c3169a3SBruce Richardson 	if (ret) {
2756c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2766c3169a3SBruce Richardson 			  "failed, this is too late now...");
2776c3169a3SBruce Richardson 		return -EINVAL;
2786c3169a3SBruce Richardson 	}
2796c3169a3SBruce Richardson 
2806c3169a3SBruce Richardson 	return 0;
2816c3169a3SBruce Richardson }
2826c3169a3SBruce Richardson 
28369c80d4eSYuanhan Liu static void
28469c80d4eSYuanhan Liu virtio_dev_queue_release(void *queue __rte_unused)
2852963d99aSJianfeng Tan {
28669c80d4eSYuanhan Liu 	/* do nothing */
2875382b188SBernard Iremonger }
2885382b188SBernard Iremonger 
28969c80d4eSYuanhan Liu static int
29069c80d4eSYuanhan Liu virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
29169c80d4eSYuanhan Liu {
29269c80d4eSYuanhan Liu 	if (vtpci_queue_idx == hw->max_queue_pairs * 2)
29369c80d4eSYuanhan Liu 		return VTNET_CQ;
29469c80d4eSYuanhan Liu 	else if (vtpci_queue_idx % 2 == 0)
29569c80d4eSYuanhan Liu 		return VTNET_RQ;
29669c80d4eSYuanhan Liu 	else
29769c80d4eSYuanhan Liu 		return VTNET_TQ;
29869c80d4eSYuanhan Liu }
29969c80d4eSYuanhan Liu 
30069c80d4eSYuanhan Liu static uint16_t
30169c80d4eSYuanhan Liu virtio_get_nr_vq(struct virtio_hw *hw)
30269c80d4eSYuanhan Liu {
30369c80d4eSYuanhan Liu 	uint16_t nr_vq = hw->max_queue_pairs * 2;
30469c80d4eSYuanhan Liu 
30569c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
30669c80d4eSYuanhan Liu 		nr_vq += 1;
30769c80d4eSYuanhan Liu 
30869c80d4eSYuanhan Liu 	return nr_vq;
30969c80d4eSYuanhan Liu }
31069c80d4eSYuanhan Liu 
311f4d1ad15SYuanhan Liu static void
312f4d1ad15SYuanhan Liu virtio_init_vring(struct virtqueue *vq)
313f4d1ad15SYuanhan Liu {
314f4d1ad15SYuanhan Liu 	int size = vq->vq_nentries;
315f4d1ad15SYuanhan Liu 	struct vring *vr = &vq->vq_ring;
316f4d1ad15SYuanhan Liu 	uint8_t *ring_mem = vq->vq_ring_virt_mem;
317f4d1ad15SYuanhan Liu 
318f4d1ad15SYuanhan Liu 	PMD_INIT_FUNC_TRACE();
319f4d1ad15SYuanhan Liu 
320f4d1ad15SYuanhan Liu 	/*
321f4d1ad15SYuanhan Liu 	 * Reinitialise since virtio port might have been stopped and restarted
322f4d1ad15SYuanhan Liu 	 */
323f4d1ad15SYuanhan Liu 	memset(ring_mem, 0, vq->vq_ring_size);
324f4d1ad15SYuanhan Liu 	vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
325f4d1ad15SYuanhan Liu 	vq->vq_used_cons_idx = 0;
326f4d1ad15SYuanhan Liu 	vq->vq_desc_head_idx = 0;
327f4d1ad15SYuanhan Liu 	vq->vq_avail_idx = 0;
328f4d1ad15SYuanhan Liu 	vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
329f4d1ad15SYuanhan Liu 	vq->vq_free_cnt = vq->vq_nentries;
330f4d1ad15SYuanhan Liu 	memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
331f4d1ad15SYuanhan Liu 
332f4d1ad15SYuanhan Liu 	vring_desc_init(vr->desc, size);
333f4d1ad15SYuanhan Liu 
334f4d1ad15SYuanhan Liu 	/*
335f4d1ad15SYuanhan Liu 	 * Disable device(host) interrupting guest
336f4d1ad15SYuanhan Liu 	 */
337f4d1ad15SYuanhan Liu 	virtqueue_disable_intr(vq);
338f4d1ad15SYuanhan Liu }
339f4d1ad15SYuanhan Liu 
34069c80d4eSYuanhan Liu static int
34169c80d4eSYuanhan Liu virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
3426c3169a3SBruce Richardson {
3436c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
34401ad44fdSHuawei Xie 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
34501ad44fdSHuawei Xie 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
3461e7bd238SStephen Hemminger 	unsigned int vq_size, size;
3476c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
348f24f8f9fSJianfeng Tan 	struct virtnet_rx *rxvq = NULL;
349f24f8f9fSJianfeng Tan 	struct virtnet_tx *txvq = NULL;
350f24f8f9fSJianfeng Tan 	struct virtnet_ctl *cvq = NULL;
35101ad44fdSHuawei Xie 	struct virtqueue *vq;
352905a2469SYuanhan Liu 	size_t sz_hdr_mz = 0;
35301ad44fdSHuawei Xie 	void *sw_ring = NULL;
35469c80d4eSYuanhan Liu 	int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
35501ad44fdSHuawei Xie 	int ret;
3566c3169a3SBruce Richardson 
357d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
3586c3169a3SBruce Richardson 
3596c3169a3SBruce Richardson 	/*
3606c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
3616c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
3626c3169a3SBruce Richardson 	 */
363d5bbeefcSYuanhan Liu 	vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
36469c80d4eSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
3656c3169a3SBruce Richardson 	if (vq_size == 0) {
3660bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
3676c3169a3SBruce Richardson 		return -EINVAL;
368d78deadaSStephen Hemminger 	}
369d78deadaSStephen Hemminger 
370d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3710bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
3726c3169a3SBruce Richardson 		return -EINVAL;
373d78deadaSStephen Hemminger 	}
374d78deadaSStephen Hemminger 
37573d017ddSYuanhan Liu 	snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
37673d017ddSYuanhan Liu 		 dev->data->port_id, vtpci_queue_idx);
37701ad44fdSHuawei Xie 
378905a2469SYuanhan Liu 	size = RTE_ALIGN_CEIL(sizeof(*vq) +
3796c3169a3SBruce Richardson 				vq_size * sizeof(struct vq_desc_extra),
3806c3169a3SBruce Richardson 				RTE_CACHE_LINE_SIZE);
381905a2469SYuanhan Liu 	if (queue_type == VTNET_TQ) {
38201ad44fdSHuawei Xie 		/*
38301ad44fdSHuawei Xie 		 * For each xmit packet, allocate a virtio_net_hdr
38401ad44fdSHuawei Xie 		 * and indirect ring elements
38501ad44fdSHuawei Xie 		 */
38601ad44fdSHuawei Xie 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
38701ad44fdSHuawei Xie 	} else if (queue_type == VTNET_CQ) {
38801ad44fdSHuawei Xie 		/* Allocate a page for control vq command, data and status */
38901ad44fdSHuawei Xie 		sz_hdr_mz = PAGE_SIZE;
39001ad44fdSHuawei Xie 	}
3914166bbf6SJianfeng Tan 
39269c80d4eSYuanhan Liu 	vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
39369c80d4eSYuanhan Liu 				SOCKET_ID_ANY);
39401ad44fdSHuawei Xie 	if (vq == NULL) {
39501ad44fdSHuawei Xie 		PMD_INIT_LOG(ERR, "can not allocate vq");
396cab04612SHuawei Xie 		return -ENOMEM;
397cab04612SHuawei Xie 	}
39869c80d4eSYuanhan Liu 	hw->vqs[vtpci_queue_idx] = vq;
39969c80d4eSYuanhan Liu 
4006c3169a3SBruce Richardson 	vq->hw = hw;
4016c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
4026c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
4036c3169a3SBruce Richardson 
4046c3169a3SBruce Richardson 	/*
4056c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
4066c3169a3SBruce Richardson 	 */
4076c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
4086c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
40901ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
41001ad44fdSHuawei Xie 		     size, vq->vq_ring_size);
4116c3169a3SBruce Richardson 
41269c80d4eSYuanhan Liu 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
41369c80d4eSYuanhan Liu 					 SOCKET_ID_ANY,
41401ad44fdSHuawei Xie 					 0, VIRTIO_PCI_VRING_ALIGN);
4156c3169a3SBruce Richardson 	if (mz == NULL) {
416abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
417abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
418abf4c84bSBernard Iremonger 		if (mz == NULL) {
41901ad44fdSHuawei Xie 			ret = -ENOMEM;
42001ad44fdSHuawei Xie 			goto fail_q_alloc;
4216c3169a3SBruce Richardson 		}
422abf4c84bSBernard Iremonger 	}
4236c3169a3SBruce Richardson 
4246c3169a3SBruce Richardson 	memset(mz->addr, 0, sizeof(mz->len));
42501ad44fdSHuawei Xie 
4266c3169a3SBruce Richardson 	vq->vq_ring_mem = mz->phys_addr;
4276c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
42801ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
42901ad44fdSHuawei Xie 		     (uint64_t)mz->phys_addr);
43001ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
43101ad44fdSHuawei Xie 		     (uint64_t)(uintptr_t)mz->addr);
4326c3169a3SBruce Richardson 
433f4d1ad15SYuanhan Liu 	virtio_init_vring(vq);
434f4d1ad15SYuanhan Liu 
43501ad44fdSHuawei Xie 	if (sz_hdr_mz) {
43673d017ddSYuanhan Liu 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
43773d017ddSYuanhan Liu 			 dev->data->port_id, vtpci_queue_idx);
43801ad44fdSHuawei Xie 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
43969c80d4eSYuanhan Liu 						     SOCKET_ID_ANY, 0,
4406dc5de3aSStephen Hemminger 						     RTE_CACHE_LINE_SIZE);
4416dc5de3aSStephen Hemminger 		if (hdr_mz == NULL) {
442abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
44301ad44fdSHuawei Xie 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
4446dc5de3aSStephen Hemminger 			if (hdr_mz == NULL) {
44501ad44fdSHuawei Xie 				ret = -ENOMEM;
44601ad44fdSHuawei Xie 				goto fail_q_alloc;
4476c3169a3SBruce Richardson 			}
448abf4c84bSBernard Iremonger 		}
44901ad44fdSHuawei Xie 	}
45001ad44fdSHuawei Xie 
45101ad44fdSHuawei Xie 	if (queue_type == VTNET_RQ) {
45201ad44fdSHuawei Xie 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
45301ad44fdSHuawei Xie 			       sizeof(vq->sw_ring[0]);
45401ad44fdSHuawei Xie 
45501ad44fdSHuawei Xie 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
45669c80d4eSYuanhan Liu 				RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
45701ad44fdSHuawei Xie 		if (!sw_ring) {
45801ad44fdSHuawei Xie 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
45901ad44fdSHuawei Xie 			ret = -ENOMEM;
46001ad44fdSHuawei Xie 			goto fail_q_alloc;
46101ad44fdSHuawei Xie 		}
46201ad44fdSHuawei Xie 
46301ad44fdSHuawei Xie 		vq->sw_ring = sw_ring;
464905a2469SYuanhan Liu 		rxvq = &vq->rxq;
46501ad44fdSHuawei Xie 		rxvq->vq = vq;
46601ad44fdSHuawei Xie 		rxvq->port_id = dev->data->port_id;
46701ad44fdSHuawei Xie 		rxvq->mz = mz;
46801ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
469905a2469SYuanhan Liu 		txvq = &vq->txq;
47001ad44fdSHuawei Xie 		txvq->vq = vq;
47101ad44fdSHuawei Xie 		txvq->port_id = dev->data->port_id;
47201ad44fdSHuawei Xie 		txvq->mz = mz;
47301ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mz = hdr_mz;
47401ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
475f24f8f9fSJianfeng Tan 	} else if (queue_type == VTNET_CQ) {
476905a2469SYuanhan Liu 		cvq = &vq->cq;
477f24f8f9fSJianfeng Tan 		cvq->vq = vq;
478f24f8f9fSJianfeng Tan 		cvq->mz = mz;
479f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mz = hdr_mz;
480f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
481f24f8f9fSJianfeng Tan 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
48269c80d4eSYuanhan Liu 
48369c80d4eSYuanhan Liu 		hw->cvq = cvq;
484f24f8f9fSJianfeng Tan 	}
485f24f8f9fSJianfeng Tan 
486e8df94b8SJianfeng Tan 	/* For virtio_user case (that is when dev->pci_dev is NULL), we use
487f24f8f9fSJianfeng Tan 	 * virtual address. And we need properly set _offset_, please see
48825f80d10SOlivier Matz 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
489f24f8f9fSJianfeng Tan 	 */
490f24f8f9fSJianfeng Tan 	if (dev->pci_dev)
491f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
492f24f8f9fSJianfeng Tan 	else {
493f24f8f9fSJianfeng Tan 		vq->vq_ring_mem = (uintptr_t)mz->addr;
494f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
495f24f8f9fSJianfeng Tan 		if (queue_type == VTNET_TQ)
496f24f8f9fSJianfeng Tan 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
497f24f8f9fSJianfeng Tan 		else if (queue_type == VTNET_CQ)
498f24f8f9fSJianfeng Tan 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
499f24f8f9fSJianfeng Tan 	}
500f24f8f9fSJianfeng Tan 
501f24f8f9fSJianfeng Tan 	if (queue_type == VTNET_TQ) {
502f24f8f9fSJianfeng Tan 		struct virtio_tx_region *txr;
503f24f8f9fSJianfeng Tan 		unsigned int i;
504f24f8f9fSJianfeng Tan 
5056dc5de3aSStephen Hemminger 		txr = hdr_mz->addr;
5066dc5de3aSStephen Hemminger 		memset(txr, 0, vq_size * sizeof(*txr));
5076dc5de3aSStephen Hemminger 		for (i = 0; i < vq_size; i++) {
5086dc5de3aSStephen Hemminger 			struct vring_desc *start_dp = txr[i].tx_indir;
5096dc5de3aSStephen Hemminger 
5106dc5de3aSStephen Hemminger 			vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
5116dc5de3aSStephen Hemminger 
5126dc5de3aSStephen Hemminger 			/* first indirect descriptor is always the tx header */
51301ad44fdSHuawei Xie 			start_dp->addr = txvq->virtio_net_hdr_mem
5146dc5de3aSStephen Hemminger 				+ i * sizeof(*txr)
5156dc5de3aSStephen Hemminger 				+ offsetof(struct virtio_tx_region, tx_hdr);
5166dc5de3aSStephen Hemminger 
51701ad44fdSHuawei Xie 			start_dp->len = hw->vtnet_hdr_size;
5186dc5de3aSStephen Hemminger 			start_dp->flags = VRING_DESC_F_NEXT;
5196dc5de3aSStephen Hemminger 		}
5206c3169a3SBruce Richardson 	}
5216c3169a3SBruce Richardson 
522595454c5SJianfeng Tan 	if (hw->vtpci_ops->setup_queue(hw, vq) < 0) {
523595454c5SJianfeng Tan 		PMD_INIT_LOG(ERR, "setup_queue failed");
524595454c5SJianfeng Tan 		return -EINVAL;
525595454c5SJianfeng Tan 	}
526595454c5SJianfeng Tan 
5276c3169a3SBruce Richardson 	return 0;
52801ad44fdSHuawei Xie 
52901ad44fdSHuawei Xie fail_q_alloc:
53001ad44fdSHuawei Xie 	rte_free(sw_ring);
53101ad44fdSHuawei Xie 	rte_memzone_free(hdr_mz);
53201ad44fdSHuawei Xie 	rte_memzone_free(mz);
53301ad44fdSHuawei Xie 	rte_free(vq);
53401ad44fdSHuawei Xie 
53501ad44fdSHuawei Xie 	return ret;
5366c3169a3SBruce Richardson }
5376c3169a3SBruce Richardson 
53869c80d4eSYuanhan Liu static void
53969c80d4eSYuanhan Liu virtio_free_queues(struct virtio_hw *hw)
5406c3169a3SBruce Richardson {
54169c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
54269c80d4eSYuanhan Liu 	struct virtqueue *vq;
54369c80d4eSYuanhan Liu 	int queue_type;
54469c80d4eSYuanhan Liu 	uint16_t i;
5456c3169a3SBruce Richardson 
54669c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
54769c80d4eSYuanhan Liu 		vq = hw->vqs[i];
54869c80d4eSYuanhan Liu 		if (!vq)
54969c80d4eSYuanhan Liu 			continue;
55069c80d4eSYuanhan Liu 
55169c80d4eSYuanhan Liu 		queue_type = virtio_get_queue_type(hw, i);
55269c80d4eSYuanhan Liu 		if (queue_type == VTNET_RQ) {
55369c80d4eSYuanhan Liu 			rte_free(vq->sw_ring);
55469c80d4eSYuanhan Liu 			rte_memzone_free(vq->rxq.mz);
55569c80d4eSYuanhan Liu 		} else if (queue_type == VTNET_TQ) {
55669c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.mz);
55769c80d4eSYuanhan Liu 			rte_memzone_free(vq->txq.virtio_net_hdr_mz);
55869c80d4eSYuanhan Liu 		} else {
55969c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.mz);
56069c80d4eSYuanhan Liu 			rte_memzone_free(vq->cq.virtio_net_hdr_mz);
56169c80d4eSYuanhan Liu 		}
56269c80d4eSYuanhan Liu 
56369c80d4eSYuanhan Liu 		rte_free(vq);
56469c80d4eSYuanhan Liu 	}
56569c80d4eSYuanhan Liu 
56669c80d4eSYuanhan Liu 	rte_free(hw->vqs);
56769c80d4eSYuanhan Liu }
56869c80d4eSYuanhan Liu 
56969c80d4eSYuanhan Liu static int
57069c80d4eSYuanhan Liu virtio_alloc_queues(struct rte_eth_dev *dev)
57169c80d4eSYuanhan Liu {
57269c80d4eSYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
57369c80d4eSYuanhan Liu 	uint16_t nr_vq = virtio_get_nr_vq(hw);
57469c80d4eSYuanhan Liu 	uint16_t i;
57569c80d4eSYuanhan Liu 	int ret;
57669c80d4eSYuanhan Liu 
57769c80d4eSYuanhan Liu 	hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
57869c80d4eSYuanhan Liu 	if (!hw->vqs) {
57969c80d4eSYuanhan Liu 		PMD_INIT_LOG(ERR, "failed to allocate vqs");
58069c80d4eSYuanhan Liu 		return -ENOMEM;
58169c80d4eSYuanhan Liu 	}
58269c80d4eSYuanhan Liu 
58369c80d4eSYuanhan Liu 	for (i = 0; i < nr_vq; i++) {
58469c80d4eSYuanhan Liu 		ret = virtio_init_queue(dev, i);
5856c3169a3SBruce Richardson 		if (ret < 0) {
58669c80d4eSYuanhan Liu 			virtio_free_queues(hw);
5876c3169a3SBruce Richardson 			return ret;
5886c3169a3SBruce Richardson 		}
5896c3169a3SBruce Richardson 	}
5906c3169a3SBruce Richardson 
59169c80d4eSYuanhan Liu 	return 0;
592941d64b5SBernard Iremonger }
593941d64b5SBernard Iremonger 
594941d64b5SBernard Iremonger static void
5956c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
5966c3169a3SBruce Richardson {
5976c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5986c3169a3SBruce Richardson 
5996c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
6006c3169a3SBruce Richardson 
6016c3169a3SBruce Richardson 	/* reset the NIC */
60262a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6036c3169a3SBruce Richardson 		vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
6046c3169a3SBruce Richardson 	vtpci_reset(hw);
6056c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
60669c80d4eSYuanhan Liu 	virtio_free_queues(hw);
6076c3169a3SBruce Richardson }
6086c3169a3SBruce Richardson 
6096c3169a3SBruce Richardson static void
6106c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
6116c3169a3SBruce Richardson {
6126c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6136c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6146c3169a3SBruce Richardson 	int dlen[1];
6156c3169a3SBruce Richardson 	int ret;
6166c3169a3SBruce Richardson 
617e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
618e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
619e9e414a4SStephen Hemminger 		return;
620e9e414a4SStephen Hemminger 	}
621e9e414a4SStephen Hemminger 
6226c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6236c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6246c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6256c3169a3SBruce Richardson 	dlen[0] = 1;
6266c3169a3SBruce Richardson 
6276c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6286c3169a3SBruce Richardson 	if (ret)
6296c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
6306c3169a3SBruce Richardson }
6316c3169a3SBruce Richardson 
6326c3169a3SBruce Richardson static void
6336c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
6346c3169a3SBruce Richardson {
6356c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6366c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6376c3169a3SBruce Richardson 	int dlen[1];
6386c3169a3SBruce Richardson 	int ret;
6396c3169a3SBruce Richardson 
640e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
641e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
642e9e414a4SStephen Hemminger 		return;
643e9e414a4SStephen Hemminger 	}
644e9e414a4SStephen Hemminger 
6456c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6466c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6476c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6486c3169a3SBruce Richardson 	dlen[0] = 1;
6496c3169a3SBruce Richardson 
6506c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6516c3169a3SBruce Richardson 	if (ret)
6526c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
6536c3169a3SBruce Richardson }
6546c3169a3SBruce Richardson 
6556c3169a3SBruce Richardson static void
6566c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
6576c3169a3SBruce Richardson {
6586c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6596c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6606c3169a3SBruce Richardson 	int dlen[1];
6616c3169a3SBruce Richardson 	int ret;
6626c3169a3SBruce Richardson 
663e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
664e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
665e9e414a4SStephen Hemminger 		return;
666e9e414a4SStephen Hemminger 	}
667e9e414a4SStephen Hemminger 
6686c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6696c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6706c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6716c3169a3SBruce Richardson 	dlen[0] = 1;
6726c3169a3SBruce Richardson 
6736c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6746c3169a3SBruce Richardson 	if (ret)
6756c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
6766c3169a3SBruce Richardson }
6776c3169a3SBruce Richardson 
6786c3169a3SBruce Richardson static void
6796c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
6806c3169a3SBruce Richardson {
6816c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6826c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6836c3169a3SBruce Richardson 	int dlen[1];
6846c3169a3SBruce Richardson 	int ret;
6856c3169a3SBruce Richardson 
686e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
687e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
688e9e414a4SStephen Hemminger 		return;
689e9e414a4SStephen Hemminger 	}
690e9e414a4SStephen Hemminger 
6916c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6926c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6936c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6946c3169a3SBruce Richardson 	dlen[0] = 1;
6956c3169a3SBruce Richardson 
6966c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6976c3169a3SBruce Richardson 	if (ret)
6986c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
6996c3169a3SBruce Richardson }
7006c3169a3SBruce Richardson 
7014ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
7024ec2424aSSouvik Dey static int
7034ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
7044ec2424aSSouvik Dey {
7054ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
7064ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
7074ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
7084ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
7094ec2424aSSouvik Dey 
7104ec2424aSSouvik Dey 	if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
7114ec2424aSSouvik Dey 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
7124ec2424aSSouvik Dey 			ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
7134ec2424aSSouvik Dey 		return -EINVAL;
7144ec2424aSSouvik Dey 	}
7154ec2424aSSouvik Dey 	return 0;
7164ec2424aSSouvik Dey }
7174ec2424aSSouvik Dey 
7186c3169a3SBruce Richardson /*
7196c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
7206c3169a3SBruce Richardson  */
7216c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
7226c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
7236c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
7246c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
7256c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
7266c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
7276c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
7286c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
7296c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
7304ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
7316c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
7326c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
73376d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
734baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
7356c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
73676d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
7376c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
7386c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
73969c80d4eSYuanhan Liu 	.rx_queue_release        = virtio_dev_queue_release,
7406c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
74169c80d4eSYuanhan Liu 	.tx_queue_release        = virtio_dev_queue_release,
7426c3169a3SBruce Richardson 	/* collect stats per queue */
7436c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7446c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7456c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7466c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7476c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7486c3169a3SBruce Richardson };
7496c3169a3SBruce Richardson 
7506c3169a3SBruce Richardson static inline int
7516c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
7526c3169a3SBruce Richardson 				struct rte_eth_link *link)
7536c3169a3SBruce Richardson {
7546c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
7556c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
7566c3169a3SBruce Richardson 
7576c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7586c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
7596c3169a3SBruce Richardson 		return -1;
7606c3169a3SBruce Richardson 
7616c3169a3SBruce Richardson 	return 0;
7626c3169a3SBruce Richardson }
7636c3169a3SBruce Richardson 
7646c3169a3SBruce Richardson /**
7656c3169a3SBruce Richardson  * Atomically writes the link status information into global
7666c3169a3SBruce Richardson  * structure rte_eth_dev.
7676c3169a3SBruce Richardson  *
7686c3169a3SBruce Richardson  * @param dev
7696c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
7706c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
7716c3169a3SBruce Richardson  *
7726c3169a3SBruce Richardson  * @return
7736c3169a3SBruce Richardson  *   - On success, zero.
7746c3169a3SBruce Richardson  *   - On failure, negative value.
7756c3169a3SBruce Richardson  */
7766c3169a3SBruce Richardson static inline int
7776c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
7786c3169a3SBruce Richardson 		struct rte_eth_link *link)
7796c3169a3SBruce Richardson {
7806c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
7816c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
7826c3169a3SBruce Richardson 
7836c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7846c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
7856c3169a3SBruce Richardson 		return -1;
7866c3169a3SBruce Richardson 
7876c3169a3SBruce Richardson 	return 0;
7886c3169a3SBruce Richardson }
7896c3169a3SBruce Richardson 
7906c3169a3SBruce Richardson static void
79176d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
7926c3169a3SBruce Richardson {
7936c3169a3SBruce Richardson 	unsigned i;
7946c3169a3SBruce Richardson 
7956c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
79601ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
7976c3169a3SBruce Richardson 		if (txvq == NULL)
7986c3169a3SBruce Richardson 			continue;
7996c3169a3SBruce Richardson 
80001ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
80101ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
80201ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
8036c3169a3SBruce Richardson 
8046c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
80501ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
80601ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
8076c3169a3SBruce Richardson 		}
8086c3169a3SBruce Richardson 	}
8096c3169a3SBruce Richardson 
8106c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
81101ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
8126c3169a3SBruce Richardson 		if (rxvq == NULL)
8136c3169a3SBruce Richardson 			continue;
8146c3169a3SBruce Richardson 
81501ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
81601ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
81701ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
8186c3169a3SBruce Richardson 
8196c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
82001ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
82101ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
8226c3169a3SBruce Richardson 		}
8236c3169a3SBruce Richardson 	}
8246c3169a3SBruce Richardson 
8256c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
8266c3169a3SBruce Richardson }
8276c3169a3SBruce Richardson 
828baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
829baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
830baf91c39SRemy Horton 				       __rte_unused unsigned limit)
831baf91c39SRemy Horton {
832baf91c39SRemy Horton 	unsigned i;
833baf91c39SRemy Horton 	unsigned count = 0;
834baf91c39SRemy Horton 	unsigned t;
835baf91c39SRemy Horton 
83601ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
83701ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
838baf91c39SRemy Horton 
8397e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
840baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
841baf91c39SRemy Horton 
842baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
843baf91c39SRemy Horton 			struct virtqueue *rxvq = dev->data->rx_queues[i];
844baf91c39SRemy Horton 			if (rxvq == NULL)
845baf91c39SRemy Horton 				continue;
84601ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
847baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
848baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
849baf91c39SRemy Horton 					"rx_q%u_%s", i,
85001ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
851baf91c39SRemy Horton 				count++;
852baf91c39SRemy Horton 			}
853baf91c39SRemy Horton 		}
854baf91c39SRemy Horton 
855baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
856baf91c39SRemy Horton 			struct virtqueue *txvq = dev->data->tx_queues[i];
857baf91c39SRemy Horton 			if (txvq == NULL)
858baf91c39SRemy Horton 				continue;
85901ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
860baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
861baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
862baf91c39SRemy Horton 					"tx_q%u_%s", i,
86301ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
864baf91c39SRemy Horton 				count++;
865baf91c39SRemy Horton 			}
866baf91c39SRemy Horton 		}
867baf91c39SRemy Horton 		return count;
868baf91c39SRemy Horton 	}
869baf91c39SRemy Horton 	return nstats;
870baf91c39SRemy Horton }
871baf91c39SRemy Horton 
87276d4c652SHarry van Haaren static int
873e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
87476d4c652SHarry van Haaren 		      unsigned n)
87576d4c652SHarry van Haaren {
87676d4c652SHarry van Haaren 	unsigned i;
87776d4c652SHarry van Haaren 	unsigned count = 0;
87876d4c652SHarry van Haaren 
87901ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
88001ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
88176d4c652SHarry van Haaren 
88276d4c652SHarry van Haaren 	if (n < nstats)
88376d4c652SHarry van Haaren 		return nstats;
88476d4c652SHarry van Haaren 
88576d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
88601ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
88776d4c652SHarry van Haaren 
88876d4c652SHarry van Haaren 		if (rxvq == NULL)
88976d4c652SHarry van Haaren 			continue;
89076d4c652SHarry van Haaren 
89176d4c652SHarry van Haaren 		unsigned t;
89276d4c652SHarry van Haaren 
89301ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
89476d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
89501ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
89676d4c652SHarry van Haaren 			count++;
89776d4c652SHarry van Haaren 		}
89876d4c652SHarry van Haaren 	}
89976d4c652SHarry van Haaren 
90076d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
90101ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
90276d4c652SHarry van Haaren 
90376d4c652SHarry van Haaren 		if (txvq == NULL)
90476d4c652SHarry van Haaren 			continue;
90576d4c652SHarry van Haaren 
90676d4c652SHarry van Haaren 		unsigned t;
90776d4c652SHarry van Haaren 
90801ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
90976d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
91001ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
91176d4c652SHarry van Haaren 			count++;
91276d4c652SHarry van Haaren 		}
91376d4c652SHarry van Haaren 	}
91476d4c652SHarry van Haaren 
91576d4c652SHarry van Haaren 	return count;
91676d4c652SHarry van Haaren }
91776d4c652SHarry van Haaren 
91876d4c652SHarry van Haaren static void
91976d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
92076d4c652SHarry van Haaren {
92176d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
92276d4c652SHarry van Haaren }
92376d4c652SHarry van Haaren 
9246c3169a3SBruce Richardson static void
9256c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
9266c3169a3SBruce Richardson {
9276c3169a3SBruce Richardson 	unsigned int i;
9286c3169a3SBruce Richardson 
9296c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
93001ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
9316c3169a3SBruce Richardson 		if (txvq == NULL)
9326c3169a3SBruce Richardson 			continue;
9336c3169a3SBruce Richardson 
93401ad44fdSHuawei Xie 		txvq->stats.packets = 0;
93501ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
93601ad44fdSHuawei Xie 		txvq->stats.errors = 0;
93701ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
93801ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
93901ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
94001ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
9416c3169a3SBruce Richardson 	}
9426c3169a3SBruce Richardson 
9436c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
94401ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9456c3169a3SBruce Richardson 		if (rxvq == NULL)
9466c3169a3SBruce Richardson 			continue;
9476c3169a3SBruce Richardson 
94801ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
94901ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
95001ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
95101ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
95201ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
95301ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
95401ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
9556c3169a3SBruce Richardson 	}
9566c3169a3SBruce Richardson }
9576c3169a3SBruce Richardson 
9586c3169a3SBruce Richardson static void
9596c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
9606c3169a3SBruce Richardson {
9616c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
9626c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9636c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9646c3169a3SBruce Richardson }
9656c3169a3SBruce Richardson 
9666c3169a3SBruce Richardson static void
9676c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
9686c3169a3SBruce Richardson {
9696c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
9706c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
9716c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9726c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9736c3169a3SBruce Richardson 	} else {
9746c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
9756c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
9766c3169a3SBruce Richardson 	}
9776c3169a3SBruce Richardson }
9786c3169a3SBruce Richardson 
97927046236SStephen Hemminger static void
9806c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
9816c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
9826c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
9836c3169a3SBruce Richardson {
9846c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
9856c3169a3SBruce Richardson 	int err, len[2];
9866c3169a3SBruce Richardson 
98727046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
988e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
98927046236SStephen Hemminger 		return;
99027046236SStephen Hemminger 	}
99127046236SStephen Hemminger 
9926c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
9936c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
9946c3169a3SBruce Richardson 
9956c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
9966c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
9976c3169a3SBruce Richardson 
9986c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
9996c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
10006c3169a3SBruce Richardson 
10016c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
10026c3169a3SBruce Richardson 	if (err != 0)
10036c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
10046c3169a3SBruce Richardson }
10056c3169a3SBruce Richardson 
10066c3169a3SBruce Richardson static void
10076c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
10086c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
10096c3169a3SBruce Richardson {
10106c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10116c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
10126c3169a3SBruce Richardson 	unsigned int i;
10136c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10146c3169a3SBruce Richardson 
10156c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10166c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10176c3169a3SBruce Richardson 		return;
10186c3169a3SBruce Richardson 	}
10196c3169a3SBruce Richardson 
10206c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10216c3169a3SBruce Richardson 	uc->entries = 0;
10226c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10236c3169a3SBruce Richardson 	mc->entries = 0;
10246c3169a3SBruce Richardson 
10256c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10266c3169a3SBruce Richardson 		const struct ether_addr *addr
10276c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
10286c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
10296c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
10306c3169a3SBruce Richardson 
10316c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
10326c3169a3SBruce Richardson 	}
10336c3169a3SBruce Richardson 
10346c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10356c3169a3SBruce Richardson }
10366c3169a3SBruce Richardson 
10376c3169a3SBruce Richardson static void
10386c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
10396c3169a3SBruce Richardson {
10406c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10416c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
10426c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10436c3169a3SBruce Richardson 	unsigned int i;
10446c3169a3SBruce Richardson 
10456c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10466c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10476c3169a3SBruce Richardson 		return;
10486c3169a3SBruce Richardson 	}
10496c3169a3SBruce Richardson 
10506c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10516c3169a3SBruce Richardson 	uc->entries = 0;
10526c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10536c3169a3SBruce Richardson 	mc->entries = 0;
10546c3169a3SBruce Richardson 
10556c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10566c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
10576c3169a3SBruce Richardson 
10586c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
10596c3169a3SBruce Richardson 			continue;
10606c3169a3SBruce Richardson 
10616c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
10626c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
10636c3169a3SBruce Richardson 	}
10646c3169a3SBruce Richardson 
10656c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10666c3169a3SBruce Richardson }
10676c3169a3SBruce Richardson 
10686c3169a3SBruce Richardson static void
10696c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
10706c3169a3SBruce Richardson {
10716c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10726c3169a3SBruce Richardson 
10736c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
10746c3169a3SBruce Richardson 
10756c3169a3SBruce Richardson 	/* Use atomic update if available */
10766c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
10776c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
10786c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
10796c3169a3SBruce Richardson 
10806c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10816c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
10826c3169a3SBruce Richardson 
10836c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
10846c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
10856c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
10866c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10876c3169a3SBruce Richardson }
10886c3169a3SBruce Richardson 
10896c3169a3SBruce Richardson static int
10906c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
10916c3169a3SBruce Richardson {
10926c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10936c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10946c3169a3SBruce Richardson 	int len;
10956c3169a3SBruce Richardson 
10966c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
10976c3169a3SBruce Richardson 		return -ENOTSUP;
10986c3169a3SBruce Richardson 
10996c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
11006c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
11016c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
11026c3169a3SBruce Richardson 	len = sizeof(vlan_id);
11036c3169a3SBruce Richardson 
11046c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
11056c3169a3SBruce Richardson }
11066c3169a3SBruce Richardson 
11076ba1f63bSYuanhan Liu static int
110860e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
11096c3169a3SBruce Richardson {
11103891f233SYuanhan Liu 	uint64_t host_features;
11116c3169a3SBruce Richardson 
11126c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
11133891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
111460e6f470SOlivier Matz 		req_features);
11156c3169a3SBruce Richardson 
11166c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1117d5bbeefcSYuanhan Liu 	host_features = hw->vtpci_ops->get_features(hw);
11183891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
11196c3169a3SBruce Richardson 		host_features);
11206c3169a3SBruce Richardson 
11216c3169a3SBruce Richardson 	/*
11226c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
11236c3169a3SBruce Richardson 	 * guest feature bits.
11246c3169a3SBruce Richardson 	 */
112560e6f470SOlivier Matz 	hw->guest_features = req_features;
11266c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
11273891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
11286c3169a3SBruce Richardson 		hw->guest_features);
11296ba1f63bSYuanhan Liu 
11306ba1f63bSYuanhan Liu 	if (hw->modern) {
11316ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
11326ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11336ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
11346ba1f63bSYuanhan Liu 			return -1;
11356ba1f63bSYuanhan Liu 		}
11366ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
11376ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
11386ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11396ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
11406ba1f63bSYuanhan Liu 			return -1;
11416ba1f63bSYuanhan Liu 		}
11426ba1f63bSYuanhan Liu 	}
11436ba1f63bSYuanhan Liu 
114460e6f470SOlivier Matz 	hw->req_guest_features = req_features;
114560e6f470SOlivier Matz 
11466ba1f63bSYuanhan Liu 	return 0;
11476c3169a3SBruce Richardson }
11486c3169a3SBruce Richardson 
11496c3169a3SBruce Richardson /*
11506c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
11516c3169a3SBruce Richardson  * if link state changed.
11526c3169a3SBruce Richardson  */
11536c3169a3SBruce Richardson static void
1154*f5880e1fSJan Blunck virtio_interrupt_handler(struct rte_intr_handle *handle,
11556c3169a3SBruce Richardson 			 void *param)
11566c3169a3SBruce Richardson {
11576c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
11586c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11596c3169a3SBruce Richardson 	uint8_t isr;
11606c3169a3SBruce Richardson 
11616c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
11626c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
11636c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
11646c3169a3SBruce Richardson 
1165*f5880e1fSJan Blunck 	if (rte_intr_enable(handle) < 0)
11666c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
11676c3169a3SBruce Richardson 
11686c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
11696c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
11706c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1171c1ceaf3aSBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC, NULL);
11726c3169a3SBruce Richardson 	}
11736c3169a3SBruce Richardson 
11746c3169a3SBruce Richardson }
11756c3169a3SBruce Richardson 
11766c3169a3SBruce Richardson static void
11776c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
11786c3169a3SBruce Richardson {
11796c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11806c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
11816c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
11826c3169a3SBruce Richardson 	else
11836c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
11846c3169a3SBruce Richardson }
11856c3169a3SBruce Richardson 
118660e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1187198ab336SOlivier Matz static int
118860e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
11896c3169a3SBruce Richardson {
11906c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11916c3169a3SBruce Richardson 	struct virtio_net_config *config;
11926c3169a3SBruce Richardson 	struct virtio_net_config local_config;
1193198ab336SOlivier Matz 	struct rte_pci_device *pci_dev = eth_dev->pci_dev;
119469c80d4eSYuanhan Liu 	int ret;
11956c3169a3SBruce Richardson 
11966c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
11976c3169a3SBruce Richardson 	vtpci_reset(hw);
11986c3169a3SBruce Richardson 
11996c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
12006c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
12016c3169a3SBruce Richardson 
12026c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
12036c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
120460e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
12056ba1f63bSYuanhan Liu 		return -1;
12066c3169a3SBruce Richardson 
1207954ea115SStephen Hemminger 	/* If host does not support status then disable LSC */
1208954ea115SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1209198ab336SOlivier Matz 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1210198ab336SOlivier Matz 	else
1211198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1212954ea115SStephen Hemminger 
1213d15339b9SBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1214d15339b9SBernard Iremonger 
12156c3169a3SBruce Richardson 	rx_func_get(eth_dev);
12166c3169a3SBruce Richardson 
12176c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
12186ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
12196ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
12206c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
12216c3169a3SBruce Richardson 	else
12226c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
12236c3169a3SBruce Richardson 
12246c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
12256c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
12266c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
12276c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
12286c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
12296c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12306c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
12316c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
12326c3169a3SBruce Richardson 
12336c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
12346c3169a3SBruce Richardson 		config = &local_config;
12356c3169a3SBruce Richardson 
12366d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
12376d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
12386d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
12396d7740e2SChangchun Ouyang 
12406c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
12416d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12426d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
12436d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
12446c3169a3SBruce Richardson 		} else {
12456c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12466c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
12476c3169a3SBruce Richardson 			config->status = 0;
12486c3169a3SBruce Richardson 		}
12496c3169a3SBruce Richardson 
12506c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
12516d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12526d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
12536d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
12546d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
12556c3169a3SBruce Richardson 		} else {
12566c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12576c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
12586c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
12596c3169a3SBruce Richardson 		}
12606c3169a3SBruce Richardson 
126145e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
12626c3169a3SBruce Richardson 
12636c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
12646c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
12656c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
12666c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
12676c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12686c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
12696c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
12706c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
12716c3169a3SBruce Richardson 	} else {
127245e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
127345e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
12746c3169a3SBruce Richardson 	}
12756c3169a3SBruce Richardson 
127669c80d4eSYuanhan Liu 	ret = virtio_alloc_queues(eth_dev);
127769c80d4eSYuanhan Liu 	if (ret < 0)
127869c80d4eSYuanhan Liu 		return ret;
1279ee85024cSYuanhan Liu 	vtpci_reinit_complete(hw);
128069c80d4eSYuanhan Liu 
1281ce2eabddSJianfeng Tan 	if (pci_dev)
12826c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
12836c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
12846c3169a3SBruce Richardson 			pci_dev->id.device_id);
12856c3169a3SBruce Richardson 
1286198ab336SOlivier Matz 	return 0;
1287198ab336SOlivier Matz }
1288198ab336SOlivier Matz 
1289198ab336SOlivier Matz /*
1290198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1291198ab336SOlivier Matz  * It returns 0 on success.
1292198ab336SOlivier Matz  */
1293198ab336SOlivier Matz int
1294198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1295198ab336SOlivier Matz {
1296198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1297198ab336SOlivier Matz 	struct rte_pci_device *pci_dev;
1298198ab336SOlivier Matz 	uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1299198ab336SOlivier Matz 	int ret;
1300198ab336SOlivier Matz 
1301198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1302198ab336SOlivier Matz 
1303198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1304198ab336SOlivier Matz 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1305198ab336SOlivier Matz 
1306198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1307198ab336SOlivier Matz 		rx_func_get(eth_dev);
1308198ab336SOlivier Matz 		return 0;
1309198ab336SOlivier Matz 	}
1310198ab336SOlivier Matz 
1311198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1312198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1313198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1314198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1315198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1316198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1317198ab336SOlivier Matz 		return -ENOMEM;
1318198ab336SOlivier Matz 	}
1319198ab336SOlivier Matz 
1320198ab336SOlivier Matz 	pci_dev = eth_dev->pci_dev;
1321198ab336SOlivier Matz 
1322198ab336SOlivier Matz 	if (pci_dev) {
1323198ab336SOlivier Matz 		ret = vtpci_init(pci_dev, hw, &dev_flags);
1324198ab336SOlivier Matz 		if (ret)
1325198ab336SOlivier Matz 			return ret;
1326198ab336SOlivier Matz 	}
1327198ab336SOlivier Matz 
1328198ab336SOlivier Matz 	eth_dev->data->dev_flags = dev_flags;
1329198ab336SOlivier Matz 
133060e6f470SOlivier Matz 	/* reset device and negotiate default features */
133196cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1332198ab336SOlivier Matz 	if (ret < 0)
1333198ab336SOlivier Matz 		return ret;
1334198ab336SOlivier Matz 
13356c3169a3SBruce Richardson 	/* Setup interrupt callback  */
133662a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1337*f5880e1fSJan Blunck 		rte_intr_callback_register(vtpci_intr_handle(hw),
13386c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
13396c3169a3SBruce Richardson 
13406c3169a3SBruce Richardson 	return 0;
13416c3169a3SBruce Richardson }
13426c3169a3SBruce Richardson 
1343abf4c84bSBernard Iremonger static int
1344abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1345abf4c84bSBernard Iremonger {
1346abf4c84bSBernard Iremonger 	struct rte_pci_device *pci_dev;
1347*f5880e1fSJan Blunck 	struct virtio_hw *hw = eth_dev->data->dev_private;
1348abf4c84bSBernard Iremonger 
1349abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1350abf4c84bSBernard Iremonger 
1351abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1352abf4c84bSBernard Iremonger 		return -EPERM;
1353abf4c84bSBernard Iremonger 
13543f3b3d54SYuanhan Liu 	virtio_dev_stop(eth_dev);
1355abf4c84bSBernard Iremonger 	virtio_dev_close(eth_dev);
1356abf4c84bSBernard Iremonger 	pci_dev = eth_dev->pci_dev;
1357abf4c84bSBernard Iremonger 
1358abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1359abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1360abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1361abf4c84bSBernard Iremonger 
1362abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1363abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1364abf4c84bSBernard Iremonger 
1365abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
136662a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1367*f5880e1fSJan Blunck 		rte_intr_callback_unregister(vtpci_intr_handle(hw),
1368abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1369abf4c84bSBernard Iremonger 						eth_dev);
13706ba1f63bSYuanhan Liu 	rte_eal_pci_unmap_device(pci_dev);
1371abf4c84bSBernard Iremonger 
1372abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1373abf4c84bSBernard Iremonger 
1374abf4c84bSBernard Iremonger 	return 0;
1375abf4c84bSBernard Iremonger }
1376abf4c84bSBernard Iremonger 
13776c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
13786c52c126SStephen Hemminger 	.pci_drv = {
1379cd433ce5SDavid Marchand 		.driver = {
1380cd433ce5SDavid Marchand 			.name = "net_virtio",
1381cd433ce5SDavid Marchand 		},
13826c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
1383abf4c84bSBernard Iremonger 		.drv_flags = RTE_PCI_DRV_DETACHABLE,
1384c830cb29SDavid Marchand 		.probe = rte_eth_dev_pci_probe,
1385c830cb29SDavid Marchand 		.remove = rte_eth_dev_pci_remove,
13866c3169a3SBruce Richardson 	},
13876c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1388abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
13896c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
13906c3169a3SBruce Richardson };
13916c3169a3SBruce Richardson 
1392c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1393c830cb29SDavid Marchand static void
1394c830cb29SDavid Marchand rte_virtio_pmd_init(void)
13956c3169a3SBruce Richardson {
13966c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
13976c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1398c830cb29SDavid Marchand 		return;
13996c3169a3SBruce Richardson 	}
14006c3169a3SBruce Richardson 
1401c830cb29SDavid Marchand 	rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
14026c3169a3SBruce Richardson }
14036c3169a3SBruce Richardson 
14046c3169a3SBruce Richardson /*
14056c3169a3SBruce Richardson  * Configure virtio device
14066c3169a3SBruce Richardson  * It returns 0 on success.
14076c3169a3SBruce Richardson  */
14086c3169a3SBruce Richardson static int
14096c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
14106c3169a3SBruce Richardson {
14116c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
14126c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
141360e6f470SOlivier Matz 	uint64_t req_features;
141445e4acd4SOlivier Matz 	int ret;
14156c3169a3SBruce Richardson 
14166c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
141796cb6711SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
141896cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum)
141996cb6711SOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
142086d59b21SOlivier Matz 	if (rxmode->enable_lro)
142186d59b21SOlivier Matz 		req_features |=
142286d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
142386d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
14246c3169a3SBruce Richardson 
142560e6f470SOlivier Matz 	/* if request features changed, reinit the device */
142660e6f470SOlivier Matz 	if (req_features != hw->req_guest_features) {
142760e6f470SOlivier Matz 		ret = virtio_init_device(dev, req_features);
142860e6f470SOlivier Matz 		if (ret < 0)
142960e6f470SOlivier Matz 			return ret;
143060e6f470SOlivier Matz 	}
143160e6f470SOlivier Matz 
143296cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum &&
143396cb6711SOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
143496cb6711SOlivier Matz 		PMD_DRV_LOG(NOTICE,
143596cb6711SOlivier Matz 			"rx ip checksum not available on this host");
143696cb6711SOlivier Matz 		return -ENOTSUP;
143796cb6711SOlivier Matz 	}
143896cb6711SOlivier Matz 
143986d59b21SOlivier Matz 	if (rxmode->enable_lro &&
144086d59b21SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
144186d59b21SOlivier Matz 			!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
144286d59b21SOlivier Matz 		PMD_DRV_LOG(NOTICE,
144386d59b21SOlivier Matz 			"lro not available on this host");
144486d59b21SOlivier Matz 		return -ENOTSUP;
144586d59b21SOlivier Matz 	}
144686d59b21SOlivier Matz 
144769c80d4eSYuanhan Liu 	/* start control queue */
144869c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
144945e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
145045e4acd4SOlivier Matz 
14516c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
14526c3169a3SBruce Richardson 
14536c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
14546c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
14556c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
14566c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
14576c3169a3SBruce Richardson 		return -ENOTSUP;
14586c3169a3SBruce Richardson 	}
14596c3169a3SBruce Richardson 
146062a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
14616c3169a3SBruce Richardson 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
14626c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
14636c3169a3SBruce Richardson 			return -EBUSY;
14646c3169a3SBruce Richardson 		}
14656c3169a3SBruce Richardson 
14666c3169a3SBruce Richardson 	return 0;
14676c3169a3SBruce Richardson }
14686c3169a3SBruce Richardson 
14696c3169a3SBruce Richardson 
14706c3169a3SBruce Richardson static int
14716c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
14726c3169a3SBruce Richardson {
14736c3169a3SBruce Richardson 	uint16_t nb_queues, i;
147401ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
147501ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
1476663c7679SYuanhan Liu 	struct virtio_hw *hw = dev->data->dev_private;
14776c3169a3SBruce Richardson 
14786c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1479954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
148062a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
14816c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
14826c3169a3SBruce Richardson 			return -ENOTSUP;
14836c3169a3SBruce Richardson 		}
14846c3169a3SBruce Richardson 
1485*f5880e1fSJan Blunck 		if (rte_intr_enable(vtpci_intr_handle(hw)) < 0) {
14866c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
14876c3169a3SBruce Richardson 			return -EIO;
14886c3169a3SBruce Richardson 		}
14896c3169a3SBruce Richardson 	}
14906c3169a3SBruce Richardson 
14916c3169a3SBruce Richardson 	/* Initialize Link state */
14926c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
14936c3169a3SBruce Richardson 
14946c3169a3SBruce Richardson 	/*Notify the backend
14956c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
14966c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
14976c3169a3SBruce Richardson 	 */
149860d4a353SYuanhan Liu 	nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1499663c7679SYuanhan Liu 	if (hw->max_queue_pairs > 1) {
15006c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
15016c3169a3SBruce Richardson 			return -EINVAL;
15026c3169a3SBruce Richardson 	}
15036c3169a3SBruce Richardson 
15046c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
15056c3169a3SBruce Richardson 
150660d4a353SYuanhan Liu 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
150701ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
150801ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
150901ad44fdSHuawei Xie 	}
15106c3169a3SBruce Richardson 
15116c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
15126c3169a3SBruce Richardson 
151301ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
151401ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
151501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
151601ad44fdSHuawei Xie 	}
15176c3169a3SBruce Richardson 
151801ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
151901ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
152001ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
152101ad44fdSHuawei Xie 	}
15226c3169a3SBruce Richardson 
15236c3169a3SBruce Richardson 	return 0;
15246c3169a3SBruce Richardson }
15256c3169a3SBruce Richardson 
15266c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
15276c3169a3SBruce Richardson {
15286c3169a3SBruce Richardson 	struct rte_mbuf *buf;
15296c3169a3SBruce Richardson 	int i, mbuf_num = 0;
15306c3169a3SBruce Richardson 
15316c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
153201ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
153301ad44fdSHuawei Xie 
15346c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15356c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
153601ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15376c3169a3SBruce Richardson 
153801ad44fdSHuawei Xie 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
153901ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
15406c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15416c3169a3SBruce Richardson 			mbuf_num++;
15426c3169a3SBruce Richardson 		}
15436c3169a3SBruce Richardson 
15446c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15456c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15466c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
154701ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15486c3169a3SBruce Richardson 	}
15496c3169a3SBruce Richardson 
15506c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
155101ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
155201ad44fdSHuawei Xie 
15536c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15546c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
15556c3169a3SBruce Richardson 			     i);
155601ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15576c3169a3SBruce Richardson 
15586c3169a3SBruce Richardson 		mbuf_num = 0;
155901ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
15606c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15616c3169a3SBruce Richardson 			mbuf_num++;
15626c3169a3SBruce Richardson 		}
15636c3169a3SBruce Richardson 
15646c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15656c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15666c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
156701ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15686c3169a3SBruce Richardson 	}
15696c3169a3SBruce Richardson }
15706c3169a3SBruce Richardson 
15716c3169a3SBruce Richardson /*
15726c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
15736c3169a3SBruce Richardson  */
15746c3169a3SBruce Richardson static void
15756c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
15766c3169a3SBruce Richardson {
1577*f5880e1fSJan Blunck 	struct virtio_hw *hw = dev->data->dev_private;
15786c3169a3SBruce Richardson 	struct rte_eth_link link;
15796c3169a3SBruce Richardson 
15806c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
15816c3169a3SBruce Richardson 
15826c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
1583*f5880e1fSJan Blunck 		rte_intr_disable(vtpci_intr_handle(hw));
15846c3169a3SBruce Richardson 
15856c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15866c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15876c3169a3SBruce Richardson }
15886c3169a3SBruce Richardson 
15896c3169a3SBruce Richardson static int
15906c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
15916c3169a3SBruce Richardson {
15926c3169a3SBruce Richardson 	struct rte_eth_link link, old;
15936c3169a3SBruce Richardson 	uint16_t status;
15946c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
15956c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15966c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
15976c3169a3SBruce Richardson 	old = link;
159811319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
15996c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
16006c3169a3SBruce Richardson 
16016c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
16026c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
16036c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
16046c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
16056c3169a3SBruce Richardson 				&status, sizeof(status));
16066c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
160709419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
16086c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
16096c3169a3SBruce Richardson 				     dev->data->port_id);
16106c3169a3SBruce Richardson 		} else {
161109419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
16126c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
16136c3169a3SBruce Richardson 				     dev->data->port_id);
16146c3169a3SBruce Richardson 		}
16156c3169a3SBruce Richardson 	} else {
161609419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
16176c3169a3SBruce Richardson 	}
16186c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
16196c3169a3SBruce Richardson 
16206c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
16216c3169a3SBruce Richardson }
16226c3169a3SBruce Richardson 
16236c3169a3SBruce Richardson static void
16246c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
16256c3169a3SBruce Richardson {
162669657304SOlivier Matz 	uint64_t tso_mask;
16276c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
16286c3169a3SBruce Richardson 
162945e4acd4SOlivier Matz 	dev_info->max_rx_queues =
163045e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
163145e4acd4SOlivier Matz 	dev_info->max_tx_queues =
163245e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
16336c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
16346c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
16356c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
16366c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
16376c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
16386c3169a3SBruce Richardson 	};
163996cb6711SOlivier Matz 	dev_info->rx_offload_capa =
164096cb6711SOlivier Matz 		DEV_RX_OFFLOAD_TCP_CKSUM |
164186d59b21SOlivier Matz 		DEV_RX_OFFLOAD_UDP_CKSUM |
164286d59b21SOlivier Matz 		DEV_RX_OFFLOAD_TCP_LRO;
164358169a9cSOlivier Matz 	dev_info->tx_offload_capa = 0;
164458169a9cSOlivier Matz 
164558169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
164658169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
164758169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
164858169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
164958169a9cSOlivier Matz 	}
165069657304SOlivier Matz 
165169657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
165269657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
165369657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
165469657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
16556c3169a3SBruce Richardson }
16566c3169a3SBruce Richardson 
16576c3169a3SBruce Richardson /*
16586c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
16596c3169a3SBruce Richardson  */
16606c3169a3SBruce Richardson static int
16616c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
16626c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
16636c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
16646c3169a3SBruce Richardson {
16656c3169a3SBruce Richardson 	return 0;
16666c3169a3SBruce Richardson }
16676c3169a3SBruce Richardson 
166801f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
166901f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
16700880c401SOlivier Matz RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio");
1671