xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision ee85024c)
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);
6053f3b3d54SYuanhan Liu 	hw->started = 0;
6066c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
60769c80d4eSYuanhan Liu 	virtio_free_queues(hw);
6086c3169a3SBruce Richardson }
6096c3169a3SBruce Richardson 
6106c3169a3SBruce Richardson static void
6116c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
6126c3169a3SBruce Richardson {
6136c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6146c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6156c3169a3SBruce Richardson 	int dlen[1];
6166c3169a3SBruce Richardson 	int ret;
6176c3169a3SBruce Richardson 
618e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
619e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
620e9e414a4SStephen Hemminger 		return;
621e9e414a4SStephen Hemminger 	}
622e9e414a4SStephen Hemminger 
6236c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6246c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6256c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6266c3169a3SBruce Richardson 	dlen[0] = 1;
6276c3169a3SBruce Richardson 
6286c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6296c3169a3SBruce Richardson 	if (ret)
6306c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
6316c3169a3SBruce Richardson }
6326c3169a3SBruce Richardson 
6336c3169a3SBruce Richardson static void
6346c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
6356c3169a3SBruce Richardson {
6366c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6376c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6386c3169a3SBruce Richardson 	int dlen[1];
6396c3169a3SBruce Richardson 	int ret;
6406c3169a3SBruce Richardson 
641e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
642e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
643e9e414a4SStephen Hemminger 		return;
644e9e414a4SStephen Hemminger 	}
645e9e414a4SStephen Hemminger 
6466c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6476c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6486c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6496c3169a3SBruce Richardson 	dlen[0] = 1;
6506c3169a3SBruce Richardson 
6516c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6526c3169a3SBruce Richardson 	if (ret)
6536c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
6546c3169a3SBruce Richardson }
6556c3169a3SBruce Richardson 
6566c3169a3SBruce Richardson static void
6576c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
6586c3169a3SBruce Richardson {
6596c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6606c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6616c3169a3SBruce Richardson 	int dlen[1];
6626c3169a3SBruce Richardson 	int ret;
6636c3169a3SBruce Richardson 
664e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
665e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
666e9e414a4SStephen Hemminger 		return;
667e9e414a4SStephen Hemminger 	}
668e9e414a4SStephen Hemminger 
6696c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6706c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6716c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6726c3169a3SBruce Richardson 	dlen[0] = 1;
6736c3169a3SBruce Richardson 
6746c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6756c3169a3SBruce Richardson 	if (ret)
6766c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
6776c3169a3SBruce Richardson }
6786c3169a3SBruce Richardson 
6796c3169a3SBruce Richardson static void
6806c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
6816c3169a3SBruce Richardson {
6826c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6836c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6846c3169a3SBruce Richardson 	int dlen[1];
6856c3169a3SBruce Richardson 	int ret;
6866c3169a3SBruce Richardson 
687e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
688e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
689e9e414a4SStephen Hemminger 		return;
690e9e414a4SStephen Hemminger 	}
691e9e414a4SStephen Hemminger 
6926c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6936c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6946c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6956c3169a3SBruce Richardson 	dlen[0] = 1;
6966c3169a3SBruce Richardson 
6976c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6986c3169a3SBruce Richardson 	if (ret)
6996c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
7006c3169a3SBruce Richardson }
7016c3169a3SBruce Richardson 
7024ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
7034ec2424aSSouvik Dey static int
7044ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
7054ec2424aSSouvik Dey {
7064ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
7074ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
7084ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
7094ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
7104ec2424aSSouvik Dey 
7114ec2424aSSouvik Dey 	if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
7124ec2424aSSouvik Dey 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
7134ec2424aSSouvik Dey 			ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
7144ec2424aSSouvik Dey 		return -EINVAL;
7154ec2424aSSouvik Dey 	}
7164ec2424aSSouvik Dey 	return 0;
7174ec2424aSSouvik Dey }
7184ec2424aSSouvik Dey 
7196c3169a3SBruce Richardson /*
7206c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
7216c3169a3SBruce Richardson  */
7226c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
7236c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
7246c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
7256c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
7266c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
7276c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
7286c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
7296c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
7306c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
7314ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
7326c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
7336c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
73476d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
735baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
7366c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
73776d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
7386c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
7396c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
74069c80d4eSYuanhan Liu 	.rx_queue_release        = virtio_dev_queue_release,
7416c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
74269c80d4eSYuanhan Liu 	.tx_queue_release        = virtio_dev_queue_release,
7436c3169a3SBruce Richardson 	/* collect stats per queue */
7446c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7456c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7466c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7476c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7486c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7496c3169a3SBruce Richardson };
7506c3169a3SBruce Richardson 
7516c3169a3SBruce Richardson static inline int
7526c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
7536c3169a3SBruce Richardson 				struct rte_eth_link *link)
7546c3169a3SBruce Richardson {
7556c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
7566c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
7576c3169a3SBruce Richardson 
7586c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7596c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
7606c3169a3SBruce Richardson 		return -1;
7616c3169a3SBruce Richardson 
7626c3169a3SBruce Richardson 	return 0;
7636c3169a3SBruce Richardson }
7646c3169a3SBruce Richardson 
7656c3169a3SBruce Richardson /**
7666c3169a3SBruce Richardson  * Atomically writes the link status information into global
7676c3169a3SBruce Richardson  * structure rte_eth_dev.
7686c3169a3SBruce Richardson  *
7696c3169a3SBruce Richardson  * @param dev
7706c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
7716c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
7726c3169a3SBruce Richardson  *
7736c3169a3SBruce Richardson  * @return
7746c3169a3SBruce Richardson  *   - On success, zero.
7756c3169a3SBruce Richardson  *   - On failure, negative value.
7766c3169a3SBruce Richardson  */
7776c3169a3SBruce Richardson static inline int
7786c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
7796c3169a3SBruce Richardson 		struct rte_eth_link *link)
7806c3169a3SBruce Richardson {
7816c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
7826c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
7836c3169a3SBruce Richardson 
7846c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7856c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
7866c3169a3SBruce Richardson 		return -1;
7876c3169a3SBruce Richardson 
7886c3169a3SBruce Richardson 	return 0;
7896c3169a3SBruce Richardson }
7906c3169a3SBruce Richardson 
7916c3169a3SBruce Richardson static void
79276d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
7936c3169a3SBruce Richardson {
7946c3169a3SBruce Richardson 	unsigned i;
7956c3169a3SBruce Richardson 
7966c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
79701ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
7986c3169a3SBruce Richardson 		if (txvq == NULL)
7996c3169a3SBruce Richardson 			continue;
8006c3169a3SBruce Richardson 
80101ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
80201ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
80301ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
8046c3169a3SBruce Richardson 
8056c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
80601ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
80701ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
8086c3169a3SBruce Richardson 		}
8096c3169a3SBruce Richardson 	}
8106c3169a3SBruce Richardson 
8116c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
81201ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
8136c3169a3SBruce Richardson 		if (rxvq == NULL)
8146c3169a3SBruce Richardson 			continue;
8156c3169a3SBruce Richardson 
81601ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
81701ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
81801ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
8196c3169a3SBruce Richardson 
8206c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
82101ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
82201ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
8236c3169a3SBruce Richardson 		}
8246c3169a3SBruce Richardson 	}
8256c3169a3SBruce Richardson 
8266c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
8276c3169a3SBruce Richardson }
8286c3169a3SBruce Richardson 
829baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
830baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
831baf91c39SRemy Horton 				       __rte_unused unsigned limit)
832baf91c39SRemy Horton {
833baf91c39SRemy Horton 	unsigned i;
834baf91c39SRemy Horton 	unsigned count = 0;
835baf91c39SRemy Horton 	unsigned t;
836baf91c39SRemy Horton 
83701ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
83801ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
839baf91c39SRemy Horton 
8407e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
841baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
842baf91c39SRemy Horton 
843baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
844baf91c39SRemy Horton 			struct virtqueue *rxvq = dev->data->rx_queues[i];
845baf91c39SRemy Horton 			if (rxvq == NULL)
846baf91c39SRemy Horton 				continue;
84701ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
848baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
849baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
850baf91c39SRemy Horton 					"rx_q%u_%s", i,
85101ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
852baf91c39SRemy Horton 				count++;
853baf91c39SRemy Horton 			}
854baf91c39SRemy Horton 		}
855baf91c39SRemy Horton 
856baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
857baf91c39SRemy Horton 			struct virtqueue *txvq = dev->data->tx_queues[i];
858baf91c39SRemy Horton 			if (txvq == NULL)
859baf91c39SRemy Horton 				continue;
86001ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
861baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
862baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
863baf91c39SRemy Horton 					"tx_q%u_%s", i,
86401ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
865baf91c39SRemy Horton 				count++;
866baf91c39SRemy Horton 			}
867baf91c39SRemy Horton 		}
868baf91c39SRemy Horton 		return count;
869baf91c39SRemy Horton 	}
870baf91c39SRemy Horton 	return nstats;
871baf91c39SRemy Horton }
872baf91c39SRemy Horton 
87376d4c652SHarry van Haaren static int
874e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
87576d4c652SHarry van Haaren 		      unsigned n)
87676d4c652SHarry van Haaren {
87776d4c652SHarry van Haaren 	unsigned i;
87876d4c652SHarry van Haaren 	unsigned count = 0;
87976d4c652SHarry van Haaren 
88001ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
88101ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
88276d4c652SHarry van Haaren 
88376d4c652SHarry van Haaren 	if (n < nstats)
88476d4c652SHarry van Haaren 		return nstats;
88576d4c652SHarry van Haaren 
88676d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
88701ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
88876d4c652SHarry van Haaren 
88976d4c652SHarry van Haaren 		if (rxvq == NULL)
89076d4c652SHarry van Haaren 			continue;
89176d4c652SHarry van Haaren 
89276d4c652SHarry van Haaren 		unsigned t;
89376d4c652SHarry van Haaren 
89401ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
89576d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
89601ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
89776d4c652SHarry van Haaren 			count++;
89876d4c652SHarry van Haaren 		}
89976d4c652SHarry van Haaren 	}
90076d4c652SHarry van Haaren 
90176d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
90201ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
90376d4c652SHarry van Haaren 
90476d4c652SHarry van Haaren 		if (txvq == NULL)
90576d4c652SHarry van Haaren 			continue;
90676d4c652SHarry van Haaren 
90776d4c652SHarry van Haaren 		unsigned t;
90876d4c652SHarry van Haaren 
90901ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
91076d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
91101ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
91276d4c652SHarry van Haaren 			count++;
91376d4c652SHarry van Haaren 		}
91476d4c652SHarry van Haaren 	}
91576d4c652SHarry van Haaren 
91676d4c652SHarry van Haaren 	return count;
91776d4c652SHarry van Haaren }
91876d4c652SHarry van Haaren 
91976d4c652SHarry van Haaren static void
92076d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
92176d4c652SHarry van Haaren {
92276d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
92376d4c652SHarry van Haaren }
92476d4c652SHarry van Haaren 
9256c3169a3SBruce Richardson static void
9266c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
9276c3169a3SBruce Richardson {
9286c3169a3SBruce Richardson 	unsigned int i;
9296c3169a3SBruce Richardson 
9306c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
93101ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
9326c3169a3SBruce Richardson 		if (txvq == NULL)
9336c3169a3SBruce Richardson 			continue;
9346c3169a3SBruce Richardson 
93501ad44fdSHuawei Xie 		txvq->stats.packets = 0;
93601ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
93701ad44fdSHuawei Xie 		txvq->stats.errors = 0;
93801ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
93901ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
94001ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
94101ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
9426c3169a3SBruce Richardson 	}
9436c3169a3SBruce Richardson 
9446c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
94501ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9466c3169a3SBruce Richardson 		if (rxvq == NULL)
9476c3169a3SBruce Richardson 			continue;
9486c3169a3SBruce Richardson 
94901ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
95001ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
95101ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
95201ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
95301ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
95401ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
95501ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
9566c3169a3SBruce Richardson 	}
9576c3169a3SBruce Richardson }
9586c3169a3SBruce Richardson 
9596c3169a3SBruce Richardson static void
9606c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
9616c3169a3SBruce Richardson {
9626c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
9636c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9646c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9656c3169a3SBruce Richardson }
9666c3169a3SBruce Richardson 
9676c3169a3SBruce Richardson static void
9686c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
9696c3169a3SBruce Richardson {
9706c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
9716c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
9726c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9736c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9746c3169a3SBruce Richardson 	} else {
9756c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
9766c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
9776c3169a3SBruce Richardson 	}
9786c3169a3SBruce Richardson }
9796c3169a3SBruce Richardson 
98027046236SStephen Hemminger static void
9816c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
9826c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
9836c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
9846c3169a3SBruce Richardson {
9856c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
9866c3169a3SBruce Richardson 	int err, len[2];
9876c3169a3SBruce Richardson 
98827046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
989e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
99027046236SStephen Hemminger 		return;
99127046236SStephen Hemminger 	}
99227046236SStephen Hemminger 
9936c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
9946c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
9956c3169a3SBruce Richardson 
9966c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
9976c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
9986c3169a3SBruce Richardson 
9996c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
10006c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
10016c3169a3SBruce Richardson 
10026c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
10036c3169a3SBruce Richardson 	if (err != 0)
10046c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
10056c3169a3SBruce Richardson }
10066c3169a3SBruce Richardson 
10076c3169a3SBruce Richardson static void
10086c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
10096c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
10106c3169a3SBruce Richardson {
10116c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10126c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
10136c3169a3SBruce Richardson 	unsigned int i;
10146c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10156c3169a3SBruce Richardson 
10166c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10176c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10186c3169a3SBruce Richardson 		return;
10196c3169a3SBruce Richardson 	}
10206c3169a3SBruce Richardson 
10216c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10226c3169a3SBruce Richardson 	uc->entries = 0;
10236c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10246c3169a3SBruce Richardson 	mc->entries = 0;
10256c3169a3SBruce Richardson 
10266c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10276c3169a3SBruce Richardson 		const struct ether_addr *addr
10286c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
10296c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
10306c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
10316c3169a3SBruce Richardson 
10326c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
10336c3169a3SBruce Richardson 	}
10346c3169a3SBruce Richardson 
10356c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10366c3169a3SBruce Richardson }
10376c3169a3SBruce Richardson 
10386c3169a3SBruce Richardson static void
10396c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
10406c3169a3SBruce Richardson {
10416c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10426c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
10436c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
10446c3169a3SBruce Richardson 	unsigned int i;
10456c3169a3SBruce Richardson 
10466c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10476c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10486c3169a3SBruce Richardson 		return;
10496c3169a3SBruce Richardson 	}
10506c3169a3SBruce Richardson 
10516c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10526c3169a3SBruce Richardson 	uc->entries = 0;
10536c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10546c3169a3SBruce Richardson 	mc->entries = 0;
10556c3169a3SBruce Richardson 
10566c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10576c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
10586c3169a3SBruce Richardson 
10596c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
10606c3169a3SBruce Richardson 			continue;
10616c3169a3SBruce Richardson 
10626c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
10636c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
10646c3169a3SBruce Richardson 	}
10656c3169a3SBruce Richardson 
10666c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10676c3169a3SBruce Richardson }
10686c3169a3SBruce Richardson 
10696c3169a3SBruce Richardson static void
10706c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
10716c3169a3SBruce Richardson {
10726c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10736c3169a3SBruce Richardson 
10746c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
10756c3169a3SBruce Richardson 
10766c3169a3SBruce Richardson 	/* Use atomic update if available */
10776c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
10786c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
10796c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
10806c3169a3SBruce Richardson 
10816c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10826c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
10836c3169a3SBruce Richardson 
10846c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
10856c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
10866c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
10876c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10886c3169a3SBruce Richardson }
10896c3169a3SBruce Richardson 
10906c3169a3SBruce Richardson static int
10916c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
10926c3169a3SBruce Richardson {
10936c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10946c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10956c3169a3SBruce Richardson 	int len;
10966c3169a3SBruce Richardson 
10976c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
10986c3169a3SBruce Richardson 		return -ENOTSUP;
10996c3169a3SBruce Richardson 
11006c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
11016c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
11026c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
11036c3169a3SBruce Richardson 	len = sizeof(vlan_id);
11046c3169a3SBruce Richardson 
11056c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
11066c3169a3SBruce Richardson }
11076c3169a3SBruce Richardson 
11086ba1f63bSYuanhan Liu static int
110960e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
11106c3169a3SBruce Richardson {
11113891f233SYuanhan Liu 	uint64_t host_features;
11126c3169a3SBruce Richardson 
11136c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
11143891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
111560e6f470SOlivier Matz 		req_features);
11166c3169a3SBruce Richardson 
11176c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1118d5bbeefcSYuanhan Liu 	host_features = hw->vtpci_ops->get_features(hw);
11193891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
11206c3169a3SBruce Richardson 		host_features);
11216c3169a3SBruce Richardson 
11226c3169a3SBruce Richardson 	/*
11236c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
11246c3169a3SBruce Richardson 	 * guest feature bits.
11256c3169a3SBruce Richardson 	 */
112660e6f470SOlivier Matz 	hw->guest_features = req_features;
11276c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
11283891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
11296c3169a3SBruce Richardson 		hw->guest_features);
11306ba1f63bSYuanhan Liu 
11316ba1f63bSYuanhan Liu 	if (hw->modern) {
11326ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
11336ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11346ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
11356ba1f63bSYuanhan Liu 			return -1;
11366ba1f63bSYuanhan Liu 		}
11376ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
11386ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
11396ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
11406ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
11416ba1f63bSYuanhan Liu 			return -1;
11426ba1f63bSYuanhan Liu 		}
11436ba1f63bSYuanhan Liu 	}
11446ba1f63bSYuanhan Liu 
114560e6f470SOlivier Matz 	hw->req_guest_features = req_features;
114660e6f470SOlivier Matz 
11476ba1f63bSYuanhan Liu 	return 0;
11486c3169a3SBruce Richardson }
11496c3169a3SBruce Richardson 
11506c3169a3SBruce Richardson /*
11516c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
11526c3169a3SBruce Richardson  * if link state changed.
11536c3169a3SBruce Richardson  */
11546c3169a3SBruce Richardson static void
11556c3169a3SBruce Richardson virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
11566c3169a3SBruce Richardson 			 void *param)
11576c3169a3SBruce Richardson {
11586c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
11596c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11606c3169a3SBruce Richardson 	uint8_t isr;
11616c3169a3SBruce Richardson 
11626c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
11636c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
11646c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
11656c3169a3SBruce Richardson 
11666c3169a3SBruce Richardson 	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
11676c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
11686c3169a3SBruce Richardson 
11696c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
11706c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
11716c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1172c1ceaf3aSBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC, NULL);
11736c3169a3SBruce Richardson 	}
11746c3169a3SBruce Richardson 
11756c3169a3SBruce Richardson }
11766c3169a3SBruce Richardson 
11776c3169a3SBruce Richardson static void
11786c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
11796c3169a3SBruce Richardson {
11806c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11816c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
11826c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
11836c3169a3SBruce Richardson 	else
11846c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
11856c3169a3SBruce Richardson }
11866c3169a3SBruce Richardson 
118760e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1188198ab336SOlivier Matz static int
118960e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
11906c3169a3SBruce Richardson {
11916c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11926c3169a3SBruce Richardson 	struct virtio_net_config *config;
11936c3169a3SBruce Richardson 	struct virtio_net_config local_config;
1194198ab336SOlivier Matz 	struct rte_pci_device *pci_dev = eth_dev->pci_dev;
119569c80d4eSYuanhan Liu 	int ret;
11966c3169a3SBruce Richardson 
11976c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
11986c3169a3SBruce Richardson 	vtpci_reset(hw);
11996c3169a3SBruce Richardson 
12006c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
12016c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
12026c3169a3SBruce Richardson 
12036c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
12046c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
120560e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
12066ba1f63bSYuanhan Liu 		return -1;
12076c3169a3SBruce Richardson 
1208954ea115SStephen Hemminger 	/* If host does not support status then disable LSC */
1209954ea115SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1210198ab336SOlivier Matz 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1211198ab336SOlivier Matz 	else
1212198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1213954ea115SStephen Hemminger 
1214d15339b9SBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1215d15339b9SBernard Iremonger 
12166c3169a3SBruce Richardson 	rx_func_get(eth_dev);
12176c3169a3SBruce Richardson 
12186c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
12196ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
12206ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
12216c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
12226c3169a3SBruce Richardson 	else
12236c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
12246c3169a3SBruce Richardson 
12256c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
12266c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
12276c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
12286c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
12296c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
12306c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12316c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
12326c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
12336c3169a3SBruce Richardson 
12346c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
12356c3169a3SBruce Richardson 		config = &local_config;
12366c3169a3SBruce Richardson 
12376d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
12386d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
12396d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
12406d7740e2SChangchun Ouyang 
12416c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
12426d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12436d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
12446d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
12456c3169a3SBruce Richardson 		} else {
12466c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12476c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
12486c3169a3SBruce Richardson 			config->status = 0;
12496c3169a3SBruce Richardson 		}
12506c3169a3SBruce Richardson 
12516c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
12526d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12536d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
12546d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
12556d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
12566c3169a3SBruce Richardson 		} else {
12576c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12586c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
12596c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
12606c3169a3SBruce Richardson 		}
12616c3169a3SBruce Richardson 
126245e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
12636c3169a3SBruce Richardson 
12646c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
12656c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
12666c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
12676c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
12686c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12696c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
12706c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
12716c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
12726c3169a3SBruce Richardson 	} else {
127345e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
127445e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
12756c3169a3SBruce Richardson 	}
12766c3169a3SBruce Richardson 
127769c80d4eSYuanhan Liu 	ret = virtio_alloc_queues(eth_dev);
127869c80d4eSYuanhan Liu 	if (ret < 0)
127969c80d4eSYuanhan Liu 		return ret;
1280*ee85024cSYuanhan Liu 	vtpci_reinit_complete(hw);
128169c80d4eSYuanhan Liu 
1282ce2eabddSJianfeng Tan 	if (pci_dev)
12836c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
12846c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
12856c3169a3SBruce Richardson 			pci_dev->id.device_id);
12866c3169a3SBruce Richardson 
1287198ab336SOlivier Matz 	return 0;
1288198ab336SOlivier Matz }
1289198ab336SOlivier Matz 
1290198ab336SOlivier Matz /*
1291198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1292198ab336SOlivier Matz  * It returns 0 on success.
1293198ab336SOlivier Matz  */
1294198ab336SOlivier Matz int
1295198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1296198ab336SOlivier Matz {
1297198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1298198ab336SOlivier Matz 	struct rte_pci_device *pci_dev;
1299198ab336SOlivier Matz 	uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1300198ab336SOlivier Matz 	int ret;
1301198ab336SOlivier Matz 
1302198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1303198ab336SOlivier Matz 
1304198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1305198ab336SOlivier Matz 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1306198ab336SOlivier Matz 
1307198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1308198ab336SOlivier Matz 		rx_func_get(eth_dev);
1309198ab336SOlivier Matz 		return 0;
1310198ab336SOlivier Matz 	}
1311198ab336SOlivier Matz 
1312198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1313198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1314198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1315198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1316198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1317198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1318198ab336SOlivier Matz 		return -ENOMEM;
1319198ab336SOlivier Matz 	}
1320198ab336SOlivier Matz 
1321198ab336SOlivier Matz 	pci_dev = eth_dev->pci_dev;
1322198ab336SOlivier Matz 
1323198ab336SOlivier Matz 	if (pci_dev) {
1324198ab336SOlivier Matz 		ret = vtpci_init(pci_dev, hw, &dev_flags);
1325198ab336SOlivier Matz 		if (ret)
1326198ab336SOlivier Matz 			return ret;
1327198ab336SOlivier Matz 	}
1328198ab336SOlivier Matz 
1329198ab336SOlivier Matz 	eth_dev->data->dev_flags = dev_flags;
1330198ab336SOlivier Matz 
133160e6f470SOlivier Matz 	/* reset device and negotiate default features */
133296cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1333198ab336SOlivier Matz 	if (ret < 0)
1334198ab336SOlivier Matz 		return ret;
1335198ab336SOlivier Matz 
13366c3169a3SBruce Richardson 	/* Setup interrupt callback  */
133762a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
13386c3169a3SBruce Richardson 		rte_intr_callback_register(&pci_dev->intr_handle,
13396c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
13406c3169a3SBruce Richardson 
13416c3169a3SBruce Richardson 	return 0;
13426c3169a3SBruce Richardson }
13436c3169a3SBruce Richardson 
1344abf4c84bSBernard Iremonger static int
1345abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1346abf4c84bSBernard Iremonger {
1347abf4c84bSBernard Iremonger 	struct rte_pci_device *pci_dev;
13483f3b3d54SYuanhan Liu 	struct virtio_hw *hw = eth_dev->data->dev_private;
1349abf4c84bSBernard Iremonger 
1350abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1351abf4c84bSBernard Iremonger 
1352abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1353abf4c84bSBernard Iremonger 		return -EPERM;
1354abf4c84bSBernard Iremonger 
13553f3b3d54SYuanhan Liu 	if (hw->started == 1) {
13563f3b3d54SYuanhan Liu 		virtio_dev_stop(eth_dev);
1357abf4c84bSBernard Iremonger 		virtio_dev_close(eth_dev);
13583f3b3d54SYuanhan Liu 	}
1359abf4c84bSBernard Iremonger 	pci_dev = eth_dev->pci_dev;
1360abf4c84bSBernard Iremonger 
1361abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1362abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1363abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1364abf4c84bSBernard Iremonger 
1365abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1366abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1367abf4c84bSBernard Iremonger 
1368abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
136962a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1370abf4c84bSBernard Iremonger 		rte_intr_callback_unregister(&pci_dev->intr_handle,
1371abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1372abf4c84bSBernard Iremonger 						eth_dev);
13736ba1f63bSYuanhan Liu 	rte_eal_pci_unmap_device(pci_dev);
1374abf4c84bSBernard Iremonger 
1375abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1376abf4c84bSBernard Iremonger 
1377abf4c84bSBernard Iremonger 	return 0;
1378abf4c84bSBernard Iremonger }
1379abf4c84bSBernard Iremonger 
13806c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
13816c52c126SStephen Hemminger 	.pci_drv = {
1382cd433ce5SDavid Marchand 		.driver = {
1383cd433ce5SDavid Marchand 			.name = "net_virtio",
1384cd433ce5SDavid Marchand 		},
13856c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
1386abf4c84bSBernard Iremonger 		.drv_flags = RTE_PCI_DRV_DETACHABLE,
1387c830cb29SDavid Marchand 		.probe = rte_eth_dev_pci_probe,
1388c830cb29SDavid Marchand 		.remove = rte_eth_dev_pci_remove,
13896c3169a3SBruce Richardson 	},
13906c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1391abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
13926c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
13936c3169a3SBruce Richardson };
13946c3169a3SBruce Richardson 
1395c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1396c830cb29SDavid Marchand static void
1397c830cb29SDavid Marchand rte_virtio_pmd_init(void)
13986c3169a3SBruce Richardson {
13996c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
14006c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1401c830cb29SDavid Marchand 		return;
14026c3169a3SBruce Richardson 	}
14036c3169a3SBruce Richardson 
1404c830cb29SDavid Marchand 	rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
14056c3169a3SBruce Richardson }
14066c3169a3SBruce Richardson 
14076c3169a3SBruce Richardson /*
14086c3169a3SBruce Richardson  * Configure virtio device
14096c3169a3SBruce Richardson  * It returns 0 on success.
14106c3169a3SBruce Richardson  */
14116c3169a3SBruce Richardson static int
14126c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
14136c3169a3SBruce Richardson {
14146c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
14156c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
141660e6f470SOlivier Matz 	uint64_t req_features;
141745e4acd4SOlivier Matz 	int ret;
14186c3169a3SBruce Richardson 
14196c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
142096cb6711SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
142196cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum)
142296cb6711SOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
142386d59b21SOlivier Matz 	if (rxmode->enable_lro)
142486d59b21SOlivier Matz 		req_features |=
142586d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
142686d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
14276c3169a3SBruce Richardson 
142860e6f470SOlivier Matz 	/* if request features changed, reinit the device */
142960e6f470SOlivier Matz 	if (req_features != hw->req_guest_features) {
143060e6f470SOlivier Matz 		ret = virtio_init_device(dev, req_features);
143160e6f470SOlivier Matz 		if (ret < 0)
143260e6f470SOlivier Matz 			return ret;
143360e6f470SOlivier Matz 	}
143460e6f470SOlivier Matz 
143596cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum &&
143696cb6711SOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
143796cb6711SOlivier Matz 		PMD_DRV_LOG(NOTICE,
143896cb6711SOlivier Matz 			"rx ip checksum not available on this host");
143996cb6711SOlivier Matz 		return -ENOTSUP;
144096cb6711SOlivier Matz 	}
144196cb6711SOlivier Matz 
144286d59b21SOlivier Matz 	if (rxmode->enable_lro &&
144386d59b21SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
144486d59b21SOlivier Matz 			!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
144586d59b21SOlivier Matz 		PMD_DRV_LOG(NOTICE,
144686d59b21SOlivier Matz 			"lro not available on this host");
144786d59b21SOlivier Matz 		return -ENOTSUP;
144886d59b21SOlivier Matz 	}
144986d59b21SOlivier Matz 
145069c80d4eSYuanhan Liu 	/* start control queue */
145169c80d4eSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
145245e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
145345e4acd4SOlivier Matz 
14546c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
14556c3169a3SBruce Richardson 
14566c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
14576c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
14586c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
14596c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
14606c3169a3SBruce Richardson 		return -ENOTSUP;
14616c3169a3SBruce Richardson 	}
14626c3169a3SBruce Richardson 
146362a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
14646c3169a3SBruce Richardson 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
14656c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
14666c3169a3SBruce Richardson 			return -EBUSY;
14676c3169a3SBruce Richardson 		}
14686c3169a3SBruce Richardson 
14696c3169a3SBruce Richardson 	return 0;
14706c3169a3SBruce Richardson }
14716c3169a3SBruce Richardson 
14726c3169a3SBruce Richardson 
14736c3169a3SBruce Richardson static int
14746c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
14756c3169a3SBruce Richardson {
14766c3169a3SBruce Richardson 	uint16_t nb_queues, i;
14776c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
147801ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
147901ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
14806c3169a3SBruce Richardson 
14816c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1482954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
148362a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
14846c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
14856c3169a3SBruce Richardson 			return -ENOTSUP;
14866c3169a3SBruce Richardson 		}
14876c3169a3SBruce Richardson 
14886c3169a3SBruce Richardson 		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
14896c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
14906c3169a3SBruce Richardson 			return -EIO;
14916c3169a3SBruce Richardson 		}
14926c3169a3SBruce Richardson 	}
14936c3169a3SBruce Richardson 
14946c3169a3SBruce Richardson 	/* Initialize Link state */
14956c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
14966c3169a3SBruce Richardson 
14976c3169a3SBruce Richardson 	/* On restart after stop do not touch queues */
14986c3169a3SBruce Richardson 	if (hw->started)
14996c3169a3SBruce Richardson 		return 0;
15006c3169a3SBruce Richardson 
15016c3169a3SBruce Richardson 	hw->started = 1;
15026c3169a3SBruce Richardson 
15036c3169a3SBruce Richardson 	/*Notify the backend
15046c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
15056c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
15066c3169a3SBruce Richardson 	 */
15076c3169a3SBruce Richardson 	nb_queues = dev->data->nb_rx_queues;
15086c3169a3SBruce Richardson 	if (nb_queues > 1) {
15096c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
15106c3169a3SBruce Richardson 			return -EINVAL;
15116c3169a3SBruce Richardson 	}
15126c3169a3SBruce Richardson 
15136c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
15146c3169a3SBruce Richardson 
151501ad44fdSHuawei Xie 	for (i = 0; i < nb_queues; i++) {
151601ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
151701ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
151801ad44fdSHuawei Xie 	}
15196c3169a3SBruce Richardson 
15206c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
15216c3169a3SBruce Richardson 
152201ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
152301ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
152401ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
152501ad44fdSHuawei Xie 	}
15266c3169a3SBruce Richardson 
152701ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
152801ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
152901ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
153001ad44fdSHuawei Xie 	}
15316c3169a3SBruce Richardson 
15326c3169a3SBruce Richardson 	return 0;
15336c3169a3SBruce Richardson }
15346c3169a3SBruce Richardson 
15356c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
15366c3169a3SBruce Richardson {
15376c3169a3SBruce Richardson 	struct rte_mbuf *buf;
15386c3169a3SBruce Richardson 	int i, mbuf_num = 0;
15396c3169a3SBruce Richardson 
15406c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
154101ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
154201ad44fdSHuawei Xie 
15436c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15446c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
154501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15466c3169a3SBruce Richardson 
154701ad44fdSHuawei Xie 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
154801ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
15496c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15506c3169a3SBruce Richardson 			mbuf_num++;
15516c3169a3SBruce Richardson 		}
15526c3169a3SBruce Richardson 
15536c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15546c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15556c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
155601ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15576c3169a3SBruce Richardson 	}
15586c3169a3SBruce Richardson 
15596c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
156001ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
156101ad44fdSHuawei Xie 
15626c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15636c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
15646c3169a3SBruce Richardson 			     i);
156501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15666c3169a3SBruce Richardson 
15676c3169a3SBruce Richardson 		mbuf_num = 0;
156801ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
15696c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15706c3169a3SBruce Richardson 			mbuf_num++;
15716c3169a3SBruce Richardson 		}
15726c3169a3SBruce Richardson 
15736c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15746c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15756c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
157601ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15776c3169a3SBruce Richardson 	}
15786c3169a3SBruce Richardson }
15796c3169a3SBruce Richardson 
15806c3169a3SBruce Richardson /*
15816c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
15826c3169a3SBruce Richardson  */
15836c3169a3SBruce Richardson static void
15846c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
15856c3169a3SBruce Richardson {
15866c3169a3SBruce Richardson 	struct rte_eth_link link;
15876c3169a3SBruce Richardson 
15886c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
15896c3169a3SBruce Richardson 
15906c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
15916c3169a3SBruce Richardson 		rte_intr_disable(&dev->pci_dev->intr_handle);
15926c3169a3SBruce Richardson 
15936c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15946c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15956c3169a3SBruce Richardson }
15966c3169a3SBruce Richardson 
15976c3169a3SBruce Richardson static int
15986c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
15996c3169a3SBruce Richardson {
16006c3169a3SBruce Richardson 	struct rte_eth_link link, old;
16016c3169a3SBruce Richardson 	uint16_t status;
16026c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
16036c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
16046c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
16056c3169a3SBruce Richardson 	old = link;
160611319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
16076c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
16086c3169a3SBruce Richardson 
16096c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
16106c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
16116c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
16126c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
16136c3169a3SBruce Richardson 				&status, sizeof(status));
16146c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
161509419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
16166c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
16176c3169a3SBruce Richardson 				     dev->data->port_id);
16186c3169a3SBruce Richardson 		} else {
161909419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
16206c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
16216c3169a3SBruce Richardson 				     dev->data->port_id);
16226c3169a3SBruce Richardson 		}
16236c3169a3SBruce Richardson 	} else {
162409419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
16256c3169a3SBruce Richardson 	}
16266c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
16276c3169a3SBruce Richardson 
16286c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
16296c3169a3SBruce Richardson }
16306c3169a3SBruce Richardson 
16316c3169a3SBruce Richardson static void
16326c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
16336c3169a3SBruce Richardson {
163469657304SOlivier Matz 	uint64_t tso_mask;
16356c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
16366c3169a3SBruce Richardson 
1637ce2eabddSJianfeng Tan 	if (dev->pci_dev)
16382f3193cfSJan Viktorin 		dev_info->driver_name = dev->driver->pci_drv.driver.name;
1639ce2eabddSJianfeng Tan 	else
1640e8df94b8SJianfeng Tan 		dev_info->driver_name = "virtio_user PMD";
164145e4acd4SOlivier Matz 	dev_info->max_rx_queues =
164245e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
164345e4acd4SOlivier Matz 	dev_info->max_tx_queues =
164445e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
16456c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
16466c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
16476c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
16486c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
16496c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
16506c3169a3SBruce Richardson 	};
165196cb6711SOlivier Matz 	dev_info->rx_offload_capa =
165296cb6711SOlivier Matz 		DEV_RX_OFFLOAD_TCP_CKSUM |
165386d59b21SOlivier Matz 		DEV_RX_OFFLOAD_UDP_CKSUM |
165486d59b21SOlivier Matz 		DEV_RX_OFFLOAD_TCP_LRO;
165558169a9cSOlivier Matz 	dev_info->tx_offload_capa = 0;
165658169a9cSOlivier Matz 
165758169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
165858169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
165958169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
166058169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
166158169a9cSOlivier Matz 	}
166269657304SOlivier Matz 
166369657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
166469657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
166569657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
166669657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
16676c3169a3SBruce Richardson }
16686c3169a3SBruce Richardson 
16696c3169a3SBruce Richardson /*
16706c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
16716c3169a3SBruce Richardson  */
16726c3169a3SBruce Richardson static int
16736c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
16746c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
16756c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
16766c3169a3SBruce Richardson {
16776c3169a3SBruce Richardson 	return 0;
16786c3169a3SBruce Richardson }
16796c3169a3SBruce Richardson 
168001f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
168101f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
1682