xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision 3f3b3d54)
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 
2835382b188SBernard Iremonger void
2842963d99aSJianfeng Tan virtio_dev_queue_release(struct virtqueue *vq)
2852963d99aSJianfeng Tan {
286ce8e1218SBernard Iremonger 	struct virtio_hw *hw;
2875382b188SBernard Iremonger 
2885382b188SBernard Iremonger 	if (vq) {
289ce8e1218SBernard Iremonger 		hw = vq->hw;
2902963d99aSJianfeng Tan 		if (vq->configured)
291d5bbeefcSYuanhan Liu 			hw->vtpci_ops->del_queue(hw, vq);
2925382b188SBernard Iremonger 
293cab04612SHuawei Xie 		rte_free(vq->sw_ring);
2945382b188SBernard Iremonger 		rte_free(vq);
2955382b188SBernard Iremonger 	}
2965382b188SBernard Iremonger }
2975382b188SBernard Iremonger 
2986c3169a3SBruce Richardson int virtio_dev_queue_setup(struct rte_eth_dev *dev,
2996c3169a3SBruce Richardson 			int queue_type,
3006c3169a3SBruce Richardson 			uint16_t queue_idx,
3016c3169a3SBruce Richardson 			uint16_t vtpci_queue_idx,
3026c3169a3SBruce Richardson 			uint16_t nb_desc,
3036c3169a3SBruce Richardson 			unsigned int socket_id,
30401ad44fdSHuawei Xie 			void **pvq)
3056c3169a3SBruce Richardson {
3066c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
30701ad44fdSHuawei Xie 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
30801ad44fdSHuawei Xie 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
3091e7bd238SStephen Hemminger 	unsigned int vq_size, size;
3106c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
311f24f8f9fSJianfeng Tan 	struct virtnet_rx *rxvq = NULL;
312f24f8f9fSJianfeng Tan 	struct virtnet_tx *txvq = NULL;
313f24f8f9fSJianfeng Tan 	struct virtnet_ctl *cvq = NULL;
31401ad44fdSHuawei Xie 	struct virtqueue *vq;
3154166bbf6SJianfeng Tan 	const char *queue_names[] = {"rvq", "txq", "cvq"};
31601ad44fdSHuawei Xie 	size_t sz_vq, sz_q = 0, sz_hdr_mz = 0;
31701ad44fdSHuawei Xie 	void *sw_ring = NULL;
31801ad44fdSHuawei Xie 	int ret;
3196c3169a3SBruce Richardson 
320d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
3216c3169a3SBruce Richardson 
3226c3169a3SBruce Richardson 	/*
3236c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
3246c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
3256c3169a3SBruce Richardson 	 */
326d5bbeefcSYuanhan Liu 	vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
3271e7bd238SStephen Hemminger 	PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
3286c3169a3SBruce Richardson 	if (vq_size == 0) {
3290bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
3306c3169a3SBruce Richardson 		return -EINVAL;
331d78deadaSStephen Hemminger 	}
332d78deadaSStephen Hemminger 
333d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3340bb159adSHuawei Xie 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
3356c3169a3SBruce Richardson 		return -EINVAL;
336d78deadaSStephen Hemminger 	}
337d78deadaSStephen Hemminger 
3384166bbf6SJianfeng Tan 	snprintf(vq_name, sizeof(vq_name), "port%d_%s%d",
3394166bbf6SJianfeng Tan 		 dev->data->port_id, queue_names[queue_type], queue_idx);
34001ad44fdSHuawei Xie 
34101ad44fdSHuawei Xie 	sz_vq = RTE_ALIGN_CEIL(sizeof(*vq) +
3426c3169a3SBruce Richardson 				vq_size * sizeof(struct vq_desc_extra),
3436c3169a3SBruce Richardson 				RTE_CACHE_LINE_SIZE);
3444166bbf6SJianfeng Tan 	if (queue_type == VTNET_RQ) {
34501ad44fdSHuawei Xie 		sz_q = sz_vq + sizeof(*rxvq);
34601ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
34701ad44fdSHuawei Xie 		sz_q = sz_vq + sizeof(*txvq);
34801ad44fdSHuawei Xie 		/*
34901ad44fdSHuawei Xie 		 * For each xmit packet, allocate a virtio_net_hdr
35001ad44fdSHuawei Xie 		 * and indirect ring elements
35101ad44fdSHuawei Xie 		 */
35201ad44fdSHuawei Xie 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
35301ad44fdSHuawei Xie 	} else if (queue_type == VTNET_CQ) {
35401ad44fdSHuawei Xie 		sz_q = sz_vq + sizeof(*cvq);
35501ad44fdSHuawei Xie 		/* Allocate a page for control vq command, data and status */
35601ad44fdSHuawei Xie 		sz_hdr_mz = PAGE_SIZE;
35701ad44fdSHuawei Xie 	}
3584166bbf6SJianfeng Tan 
35901ad44fdSHuawei Xie 	vq = rte_zmalloc_socket(vq_name, sz_q, RTE_CACHE_LINE_SIZE, socket_id);
36001ad44fdSHuawei Xie 	if (vq == NULL) {
36101ad44fdSHuawei Xie 		PMD_INIT_LOG(ERR, "can not allocate vq");
362cab04612SHuawei Xie 		return -ENOMEM;
363cab04612SHuawei Xie 	}
3646c3169a3SBruce Richardson 	vq->hw = hw;
3656c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
3666c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
367945884f1SStephen Hemminger 
368945884f1SStephen Hemminger 	if (nb_desc == 0 || nb_desc > vq_size)
369945884f1SStephen Hemminger 		nb_desc = vq_size;
370945884f1SStephen Hemminger 	vq->vq_free_cnt = nb_desc;
3716c3169a3SBruce Richardson 
3726c3169a3SBruce Richardson 	/*
3736c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
3746c3169a3SBruce Richardson 	 */
3756c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
3766c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
37701ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
37801ad44fdSHuawei Xie 		     size, vq->vq_ring_size);
3796c3169a3SBruce Richardson 
38001ad44fdSHuawei Xie 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, socket_id,
38101ad44fdSHuawei Xie 					 0, VIRTIO_PCI_VRING_ALIGN);
3826c3169a3SBruce Richardson 	if (mz == NULL) {
383abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
384abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
385abf4c84bSBernard Iremonger 		if (mz == NULL) {
38601ad44fdSHuawei Xie 			ret = -ENOMEM;
38701ad44fdSHuawei Xie 			goto fail_q_alloc;
3886c3169a3SBruce Richardson 		}
389abf4c84bSBernard Iremonger 	}
3906c3169a3SBruce Richardson 
3916c3169a3SBruce Richardson 	memset(mz->addr, 0, sizeof(mz->len));
39201ad44fdSHuawei Xie 
3936c3169a3SBruce Richardson 	vq->vq_ring_mem = mz->phys_addr;
3946c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
39501ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
39601ad44fdSHuawei Xie 		     (uint64_t)mz->phys_addr);
39701ad44fdSHuawei Xie 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
39801ad44fdSHuawei Xie 		     (uint64_t)(uintptr_t)mz->addr);
3996c3169a3SBruce Richardson 
40001ad44fdSHuawei Xie 	if (sz_hdr_mz) {
40101ad44fdSHuawei Xie 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_%s%d_hdr",
40201ad44fdSHuawei Xie 			 dev->data->port_id, queue_names[queue_type],
40301ad44fdSHuawei Xie 			 queue_idx);
40401ad44fdSHuawei Xie 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
4056dc5de3aSStephen Hemminger 						     socket_id, 0,
4066dc5de3aSStephen Hemminger 						     RTE_CACHE_LINE_SIZE);
4076dc5de3aSStephen Hemminger 		if (hdr_mz == NULL) {
408abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
40901ad44fdSHuawei Xie 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
4106dc5de3aSStephen Hemminger 			if (hdr_mz == NULL) {
41101ad44fdSHuawei Xie 				ret = -ENOMEM;
41201ad44fdSHuawei Xie 				goto fail_q_alloc;
4136c3169a3SBruce Richardson 			}
414abf4c84bSBernard Iremonger 		}
41501ad44fdSHuawei Xie 	}
41601ad44fdSHuawei Xie 
41701ad44fdSHuawei Xie 	if (queue_type == VTNET_RQ) {
41801ad44fdSHuawei Xie 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
41901ad44fdSHuawei Xie 			       sizeof(vq->sw_ring[0]);
42001ad44fdSHuawei Xie 
42101ad44fdSHuawei Xie 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
42201ad44fdSHuawei Xie 					     RTE_CACHE_LINE_SIZE, socket_id);
42301ad44fdSHuawei Xie 		if (!sw_ring) {
42401ad44fdSHuawei Xie 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
42501ad44fdSHuawei Xie 			ret = -ENOMEM;
42601ad44fdSHuawei Xie 			goto fail_q_alloc;
42701ad44fdSHuawei Xie 		}
42801ad44fdSHuawei Xie 
42901ad44fdSHuawei Xie 		vq->sw_ring = sw_ring;
43001ad44fdSHuawei Xie 		rxvq = (struct virtnet_rx *)RTE_PTR_ADD(vq, sz_vq);
43101ad44fdSHuawei Xie 		rxvq->vq = vq;
43201ad44fdSHuawei Xie 		rxvq->port_id = dev->data->port_id;
43301ad44fdSHuawei Xie 		rxvq->queue_id = queue_idx;
43401ad44fdSHuawei Xie 		rxvq->mz = mz;
43501ad44fdSHuawei Xie 		*pvq = rxvq;
43601ad44fdSHuawei Xie 	} else if (queue_type == VTNET_TQ) {
43701ad44fdSHuawei Xie 		txvq = (struct virtnet_tx *)RTE_PTR_ADD(vq, sz_vq);
43801ad44fdSHuawei Xie 		txvq->vq = vq;
43901ad44fdSHuawei Xie 		txvq->port_id = dev->data->port_id;
44001ad44fdSHuawei Xie 		txvq->queue_id = queue_idx;
44101ad44fdSHuawei Xie 		txvq->mz = mz;
44201ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mz = hdr_mz;
44301ad44fdSHuawei Xie 		txvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
4446dc5de3aSStephen Hemminger 
445f24f8f9fSJianfeng Tan 		*pvq = txvq;
446f24f8f9fSJianfeng Tan 	} else if (queue_type == VTNET_CQ) {
447f24f8f9fSJianfeng Tan 		cvq = (struct virtnet_ctl *)RTE_PTR_ADD(vq, sz_vq);
448f24f8f9fSJianfeng Tan 		cvq->vq = vq;
449f24f8f9fSJianfeng Tan 		cvq->mz = mz;
450f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mz = hdr_mz;
451f24f8f9fSJianfeng Tan 		cvq->virtio_net_hdr_mem = hdr_mz->phys_addr;
452f24f8f9fSJianfeng Tan 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
453f24f8f9fSJianfeng Tan 		*pvq = cvq;
454f24f8f9fSJianfeng Tan 	}
455f24f8f9fSJianfeng Tan 
456e8df94b8SJianfeng Tan 	/* For virtio_user case (that is when dev->pci_dev is NULL), we use
457f24f8f9fSJianfeng Tan 	 * virtual address. And we need properly set _offset_, please see
45825f80d10SOlivier Matz 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
459f24f8f9fSJianfeng Tan 	 */
460f24f8f9fSJianfeng Tan 	if (dev->pci_dev)
461f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_physaddr);
462f24f8f9fSJianfeng Tan 	else {
463f24f8f9fSJianfeng Tan 		vq->vq_ring_mem = (uintptr_t)mz->addr;
464f24f8f9fSJianfeng Tan 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
465f24f8f9fSJianfeng Tan 		if (queue_type == VTNET_TQ)
466f24f8f9fSJianfeng Tan 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
467f24f8f9fSJianfeng Tan 		else if (queue_type == VTNET_CQ)
468f24f8f9fSJianfeng Tan 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
469f24f8f9fSJianfeng Tan 	}
470f24f8f9fSJianfeng Tan 
471f24f8f9fSJianfeng Tan 	if (queue_type == VTNET_TQ) {
472f24f8f9fSJianfeng Tan 		struct virtio_tx_region *txr;
473f24f8f9fSJianfeng Tan 		unsigned int i;
474f24f8f9fSJianfeng Tan 
4756dc5de3aSStephen Hemminger 		txr = hdr_mz->addr;
4766dc5de3aSStephen Hemminger 		memset(txr, 0, vq_size * sizeof(*txr));
4776dc5de3aSStephen Hemminger 		for (i = 0; i < vq_size; i++) {
4786dc5de3aSStephen Hemminger 			struct vring_desc *start_dp = txr[i].tx_indir;
4796dc5de3aSStephen Hemminger 
4806dc5de3aSStephen Hemminger 			vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
4816dc5de3aSStephen Hemminger 
4826dc5de3aSStephen Hemminger 			/* first indirect descriptor is always the tx header */
48301ad44fdSHuawei Xie 			start_dp->addr = txvq->virtio_net_hdr_mem
4846dc5de3aSStephen Hemminger 				+ i * sizeof(*txr)
4856dc5de3aSStephen Hemminger 				+ offsetof(struct virtio_tx_region, tx_hdr);
4866dc5de3aSStephen Hemminger 
48701ad44fdSHuawei Xie 			start_dp->len = hw->vtnet_hdr_size;
4886dc5de3aSStephen Hemminger 			start_dp->flags = VRING_DESC_F_NEXT;
4896dc5de3aSStephen Hemminger 		}
4906c3169a3SBruce Richardson 	}
4916c3169a3SBruce Richardson 
492595454c5SJianfeng Tan 	if (hw->vtpci_ops->setup_queue(hw, vq) < 0) {
493595454c5SJianfeng Tan 		PMD_INIT_LOG(ERR, "setup_queue failed");
494595454c5SJianfeng Tan 		virtio_dev_queue_release(vq);
495595454c5SJianfeng Tan 		return -EINVAL;
496595454c5SJianfeng Tan 	}
497595454c5SJianfeng Tan 
4982963d99aSJianfeng Tan 	vq->configured = 1;
4996c3169a3SBruce Richardson 	return 0;
50001ad44fdSHuawei Xie 
50101ad44fdSHuawei Xie fail_q_alloc:
50201ad44fdSHuawei Xie 	rte_free(sw_ring);
50301ad44fdSHuawei Xie 	rte_memzone_free(hdr_mz);
50401ad44fdSHuawei Xie 	rte_memzone_free(mz);
50501ad44fdSHuawei Xie 	rte_free(vq);
50601ad44fdSHuawei Xie 
50701ad44fdSHuawei Xie 	return ret;
5086c3169a3SBruce Richardson }
5096c3169a3SBruce Richardson 
5106c3169a3SBruce Richardson static int
5116c3169a3SBruce Richardson virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
5126c3169a3SBruce Richardson 		uint32_t socket_id)
5136c3169a3SBruce Richardson {
51401ad44fdSHuawei Xie 	struct virtnet_ctl *cvq;
5156c3169a3SBruce Richardson 	int ret;
5166c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5176c3169a3SBruce Richardson 
5186c3169a3SBruce Richardson 	PMD_INIT_FUNC_TRACE();
5196c3169a3SBruce Richardson 	ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
52001ad44fdSHuawei Xie 			vtpci_queue_idx, 0, socket_id, (void **)&cvq);
5216c3169a3SBruce Richardson 	if (ret < 0) {
5226c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "control vq initialization failed");
5236c3169a3SBruce Richardson 		return ret;
5246c3169a3SBruce Richardson 	}
5256c3169a3SBruce Richardson 
52601ad44fdSHuawei Xie 	hw->cvq = cvq;
5276c3169a3SBruce Richardson 	return 0;
5286c3169a3SBruce Richardson }
5296c3169a3SBruce Richardson 
5306c3169a3SBruce Richardson static void
531941d64b5SBernard Iremonger virtio_free_queues(struct rte_eth_dev *dev)
532941d64b5SBernard Iremonger {
533941d64b5SBernard Iremonger 	unsigned int i;
534941d64b5SBernard Iremonger 
535941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_rx_queues; i++)
536941d64b5SBernard Iremonger 		virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
537941d64b5SBernard Iremonger 
538941d64b5SBernard Iremonger 	dev->data->nb_rx_queues = 0;
539941d64b5SBernard Iremonger 
540941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_tx_queues; i++)
541941d64b5SBernard Iremonger 		virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
542941d64b5SBernard Iremonger 
543941d64b5SBernard Iremonger 	dev->data->nb_tx_queues = 0;
544941d64b5SBernard Iremonger }
545941d64b5SBernard Iremonger 
546941d64b5SBernard Iremonger static void
5476c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
5486c3169a3SBruce Richardson {
5496c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5506c3169a3SBruce Richardson 
5516c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
5526c3169a3SBruce Richardson 
55345e4acd4SOlivier Matz 	if (hw->cvq)
55445e4acd4SOlivier Matz 		virtio_dev_queue_release(hw->cvq->vq);
55545e4acd4SOlivier Matz 
5566c3169a3SBruce Richardson 	/* reset the NIC */
55762a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
5586c3169a3SBruce Richardson 		vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
5596c3169a3SBruce Richardson 	vtpci_reset(hw);
560*3f3b3d54SYuanhan Liu 	hw->started = 0;
5616c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
562941d64b5SBernard Iremonger 	virtio_free_queues(dev);
5636c3169a3SBruce Richardson }
5646c3169a3SBruce Richardson 
5656c3169a3SBruce Richardson static void
5666c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
5676c3169a3SBruce Richardson {
5686c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5696c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5706c3169a3SBruce Richardson 	int dlen[1];
5716c3169a3SBruce Richardson 	int ret;
5726c3169a3SBruce Richardson 
573e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
574e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
575e9e414a4SStephen Hemminger 		return;
576e9e414a4SStephen Hemminger 	}
577e9e414a4SStephen Hemminger 
5786c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5796c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
5806c3169a3SBruce Richardson 	ctrl.data[0] = 1;
5816c3169a3SBruce Richardson 	dlen[0] = 1;
5826c3169a3SBruce Richardson 
5836c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5846c3169a3SBruce Richardson 	if (ret)
5856c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
5866c3169a3SBruce Richardson }
5876c3169a3SBruce Richardson 
5886c3169a3SBruce Richardson static void
5896c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
5906c3169a3SBruce Richardson {
5916c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5926c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5936c3169a3SBruce Richardson 	int dlen[1];
5946c3169a3SBruce Richardson 	int ret;
5956c3169a3SBruce Richardson 
596e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
597e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
598e9e414a4SStephen Hemminger 		return;
599e9e414a4SStephen Hemminger 	}
600e9e414a4SStephen Hemminger 
6016c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6026c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
6036c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6046c3169a3SBruce Richardson 	dlen[0] = 1;
6056c3169a3SBruce Richardson 
6066c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6076c3169a3SBruce Richardson 	if (ret)
6086c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
6096c3169a3SBruce Richardson }
6106c3169a3SBruce Richardson 
6116c3169a3SBruce Richardson static void
6126c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
6136c3169a3SBruce Richardson {
6146c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6156c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6166c3169a3SBruce Richardson 	int dlen[1];
6176c3169a3SBruce Richardson 	int ret;
6186c3169a3SBruce Richardson 
619e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
620e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
621e9e414a4SStephen Hemminger 		return;
622e9e414a4SStephen Hemminger 	}
623e9e414a4SStephen Hemminger 
6246c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6256c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6266c3169a3SBruce Richardson 	ctrl.data[0] = 1;
6276c3169a3SBruce Richardson 	dlen[0] = 1;
6286c3169a3SBruce Richardson 
6296c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6306c3169a3SBruce Richardson 	if (ret)
6316c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
6326c3169a3SBruce Richardson }
6336c3169a3SBruce Richardson 
6346c3169a3SBruce Richardson static void
6356c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
6366c3169a3SBruce Richardson {
6376c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
6386c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
6396c3169a3SBruce Richardson 	int dlen[1];
6406c3169a3SBruce Richardson 	int ret;
6416c3169a3SBruce Richardson 
642e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
643e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
644e9e414a4SStephen Hemminger 		return;
645e9e414a4SStephen Hemminger 	}
646e9e414a4SStephen Hemminger 
6476c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
6486c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
6496c3169a3SBruce Richardson 	ctrl.data[0] = 0;
6506c3169a3SBruce Richardson 	dlen[0] = 1;
6516c3169a3SBruce Richardson 
6526c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
6536c3169a3SBruce Richardson 	if (ret)
6546c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
6556c3169a3SBruce Richardson }
6566c3169a3SBruce Richardson 
6574ec2424aSSouvik Dey #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
6584ec2424aSSouvik Dey static int
6594ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
6604ec2424aSSouvik Dey {
6614ec2424aSSouvik Dey 	struct virtio_hw *hw = dev->data->dev_private;
6624ec2424aSSouvik Dey 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
6634ec2424aSSouvik Dey 				 hw->vtnet_hdr_size;
6644ec2424aSSouvik Dey 	uint32_t frame_size = mtu + ether_hdr_len;
6654ec2424aSSouvik Dey 
6664ec2424aSSouvik Dey 	if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) {
6674ec2424aSSouvik Dey 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n",
6684ec2424aSSouvik Dey 			ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len);
6694ec2424aSSouvik Dey 		return -EINVAL;
6704ec2424aSSouvik Dey 	}
6714ec2424aSSouvik Dey 	return 0;
6724ec2424aSSouvik Dey }
6734ec2424aSSouvik Dey 
6746c3169a3SBruce Richardson /*
6756c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
6766c3169a3SBruce Richardson  */
6776c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
6786c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
6796c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
6806c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
6816c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
6826c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
6836c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
6846c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
6856c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
6864ec2424aSSouvik Dey 	.mtu_set                 = virtio_mtu_set,
6876c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
6886c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
68976d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
690baf91c39SRemy Horton 	.xstats_get_names        = virtio_dev_xstats_get_names,
6916c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
69276d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
6936c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
6946c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
6956c3169a3SBruce Richardson 	.rx_queue_release        = virtio_dev_rx_queue_release,
6966c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
6976c3169a3SBruce Richardson 	.tx_queue_release        = virtio_dev_tx_queue_release,
6986c3169a3SBruce Richardson 	/* collect stats per queue */
6996c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
7006c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
7016c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
7026c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
7036c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
7046c3169a3SBruce Richardson };
7056c3169a3SBruce Richardson 
7066c3169a3SBruce Richardson static inline int
7076c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
7086c3169a3SBruce Richardson 				struct rte_eth_link *link)
7096c3169a3SBruce Richardson {
7106c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
7116c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
7126c3169a3SBruce Richardson 
7136c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7146c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
7156c3169a3SBruce Richardson 		return -1;
7166c3169a3SBruce Richardson 
7176c3169a3SBruce Richardson 	return 0;
7186c3169a3SBruce Richardson }
7196c3169a3SBruce Richardson 
7206c3169a3SBruce Richardson /**
7216c3169a3SBruce Richardson  * Atomically writes the link status information into global
7226c3169a3SBruce Richardson  * structure rte_eth_dev.
7236c3169a3SBruce Richardson  *
7246c3169a3SBruce Richardson  * @param dev
7256c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
7266c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
7276c3169a3SBruce Richardson  *
7286c3169a3SBruce Richardson  * @return
7296c3169a3SBruce Richardson  *   - On success, zero.
7306c3169a3SBruce Richardson  *   - On failure, negative value.
7316c3169a3SBruce Richardson  */
7326c3169a3SBruce Richardson static inline int
7336c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
7346c3169a3SBruce Richardson 		struct rte_eth_link *link)
7356c3169a3SBruce Richardson {
7366c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
7376c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
7386c3169a3SBruce Richardson 
7396c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
7406c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
7416c3169a3SBruce Richardson 		return -1;
7426c3169a3SBruce Richardson 
7436c3169a3SBruce Richardson 	return 0;
7446c3169a3SBruce Richardson }
7456c3169a3SBruce Richardson 
7466c3169a3SBruce Richardson static void
74776d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
7486c3169a3SBruce Richardson {
7496c3169a3SBruce Richardson 	unsigned i;
7506c3169a3SBruce Richardson 
7516c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
75201ad44fdSHuawei Xie 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
7536c3169a3SBruce Richardson 		if (txvq == NULL)
7546c3169a3SBruce Richardson 			continue;
7556c3169a3SBruce Richardson 
75601ad44fdSHuawei Xie 		stats->opackets += txvq->stats.packets;
75701ad44fdSHuawei Xie 		stats->obytes += txvq->stats.bytes;
75801ad44fdSHuawei Xie 		stats->oerrors += txvq->stats.errors;
7596c3169a3SBruce Richardson 
7606c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
76101ad44fdSHuawei Xie 			stats->q_opackets[i] = txvq->stats.packets;
76201ad44fdSHuawei Xie 			stats->q_obytes[i] = txvq->stats.bytes;
7636c3169a3SBruce Richardson 		}
7646c3169a3SBruce Richardson 	}
7656c3169a3SBruce Richardson 
7666c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
76701ad44fdSHuawei Xie 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
7686c3169a3SBruce Richardson 		if (rxvq == NULL)
7696c3169a3SBruce Richardson 			continue;
7706c3169a3SBruce Richardson 
77101ad44fdSHuawei Xie 		stats->ipackets += rxvq->stats.packets;
77201ad44fdSHuawei Xie 		stats->ibytes += rxvq->stats.bytes;
77301ad44fdSHuawei Xie 		stats->ierrors += rxvq->stats.errors;
7746c3169a3SBruce Richardson 
7756c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
77601ad44fdSHuawei Xie 			stats->q_ipackets[i] = rxvq->stats.packets;
77701ad44fdSHuawei Xie 			stats->q_ibytes[i] = rxvq->stats.bytes;
7786c3169a3SBruce Richardson 		}
7796c3169a3SBruce Richardson 	}
7806c3169a3SBruce Richardson 
7816c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
7826c3169a3SBruce Richardson }
7836c3169a3SBruce Richardson 
784baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
785baf91c39SRemy Horton 				       struct rte_eth_xstat_name *xstats_names,
786baf91c39SRemy Horton 				       __rte_unused unsigned limit)
787baf91c39SRemy Horton {
788baf91c39SRemy Horton 	unsigned i;
789baf91c39SRemy Horton 	unsigned count = 0;
790baf91c39SRemy Horton 	unsigned t;
791baf91c39SRemy Horton 
79201ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
79301ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
794baf91c39SRemy Horton 
7957e1eb993SYuanhan Liu 	if (xstats_names != NULL) {
796baf91c39SRemy Horton 		/* Note: limit checked in rte_eth_xstats_names() */
797baf91c39SRemy Horton 
798baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
799baf91c39SRemy Horton 			struct virtqueue *rxvq = dev->data->rx_queues[i];
800baf91c39SRemy Horton 			if (rxvq == NULL)
801baf91c39SRemy Horton 				continue;
80201ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
803baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
804baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
805baf91c39SRemy Horton 					"rx_q%u_%s", i,
80601ad44fdSHuawei Xie 					rte_virtio_rxq_stat_strings[t].name);
807baf91c39SRemy Horton 				count++;
808baf91c39SRemy Horton 			}
809baf91c39SRemy Horton 		}
810baf91c39SRemy Horton 
811baf91c39SRemy Horton 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
812baf91c39SRemy Horton 			struct virtqueue *txvq = dev->data->tx_queues[i];
813baf91c39SRemy Horton 			if (txvq == NULL)
814baf91c39SRemy Horton 				continue;
81501ad44fdSHuawei Xie 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
816baf91c39SRemy Horton 				snprintf(xstats_names[count].name,
817baf91c39SRemy Horton 					sizeof(xstats_names[count].name),
818baf91c39SRemy Horton 					"tx_q%u_%s", i,
81901ad44fdSHuawei Xie 					rte_virtio_txq_stat_strings[t].name);
820baf91c39SRemy Horton 				count++;
821baf91c39SRemy Horton 			}
822baf91c39SRemy Horton 		}
823baf91c39SRemy Horton 		return count;
824baf91c39SRemy Horton 	}
825baf91c39SRemy Horton 	return nstats;
826baf91c39SRemy Horton }
827baf91c39SRemy Horton 
82876d4c652SHarry van Haaren static int
829e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
83076d4c652SHarry van Haaren 		      unsigned n)
83176d4c652SHarry van Haaren {
83276d4c652SHarry van Haaren 	unsigned i;
83376d4c652SHarry van Haaren 	unsigned count = 0;
83476d4c652SHarry van Haaren 
83501ad44fdSHuawei Xie 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
83601ad44fdSHuawei Xie 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
83776d4c652SHarry van Haaren 
83876d4c652SHarry van Haaren 	if (n < nstats)
83976d4c652SHarry van Haaren 		return nstats;
84076d4c652SHarry van Haaren 
84176d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
84201ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
84376d4c652SHarry van Haaren 
84476d4c652SHarry van Haaren 		if (rxvq == NULL)
84576d4c652SHarry van Haaren 			continue;
84676d4c652SHarry van Haaren 
84776d4c652SHarry van Haaren 		unsigned t;
84876d4c652SHarry van Haaren 
84901ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
85076d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
85101ad44fdSHuawei Xie 				rte_virtio_rxq_stat_strings[t].offset);
85276d4c652SHarry van Haaren 			count++;
85376d4c652SHarry van Haaren 		}
85476d4c652SHarry van Haaren 	}
85576d4c652SHarry van Haaren 
85676d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
85701ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
85876d4c652SHarry van Haaren 
85976d4c652SHarry van Haaren 		if (txvq == NULL)
86076d4c652SHarry van Haaren 			continue;
86176d4c652SHarry van Haaren 
86276d4c652SHarry van Haaren 		unsigned t;
86376d4c652SHarry van Haaren 
86401ad44fdSHuawei Xie 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
86576d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
86601ad44fdSHuawei Xie 				rte_virtio_txq_stat_strings[t].offset);
86776d4c652SHarry van Haaren 			count++;
86876d4c652SHarry van Haaren 		}
86976d4c652SHarry van Haaren 	}
87076d4c652SHarry van Haaren 
87176d4c652SHarry van Haaren 	return count;
87276d4c652SHarry van Haaren }
87376d4c652SHarry van Haaren 
87476d4c652SHarry van Haaren static void
87576d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
87676d4c652SHarry van Haaren {
87776d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
87876d4c652SHarry van Haaren }
87976d4c652SHarry van Haaren 
8806c3169a3SBruce Richardson static void
8816c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
8826c3169a3SBruce Richardson {
8836c3169a3SBruce Richardson 	unsigned int i;
8846c3169a3SBruce Richardson 
8856c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
88601ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
8876c3169a3SBruce Richardson 		if (txvq == NULL)
8886c3169a3SBruce Richardson 			continue;
8896c3169a3SBruce Richardson 
89001ad44fdSHuawei Xie 		txvq->stats.packets = 0;
89101ad44fdSHuawei Xie 		txvq->stats.bytes = 0;
89201ad44fdSHuawei Xie 		txvq->stats.errors = 0;
89301ad44fdSHuawei Xie 		txvq->stats.multicast = 0;
89401ad44fdSHuawei Xie 		txvq->stats.broadcast = 0;
89501ad44fdSHuawei Xie 		memset(txvq->stats.size_bins, 0,
89601ad44fdSHuawei Xie 		       sizeof(txvq->stats.size_bins[0]) * 8);
8976c3169a3SBruce Richardson 	}
8986c3169a3SBruce Richardson 
8996c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
90001ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
9016c3169a3SBruce Richardson 		if (rxvq == NULL)
9026c3169a3SBruce Richardson 			continue;
9036c3169a3SBruce Richardson 
90401ad44fdSHuawei Xie 		rxvq->stats.packets = 0;
90501ad44fdSHuawei Xie 		rxvq->stats.bytes = 0;
90601ad44fdSHuawei Xie 		rxvq->stats.errors = 0;
90701ad44fdSHuawei Xie 		rxvq->stats.multicast = 0;
90801ad44fdSHuawei Xie 		rxvq->stats.broadcast = 0;
90901ad44fdSHuawei Xie 		memset(rxvq->stats.size_bins, 0,
91001ad44fdSHuawei Xie 		       sizeof(rxvq->stats.size_bins[0]) * 8);
9116c3169a3SBruce Richardson 	}
9126c3169a3SBruce Richardson }
9136c3169a3SBruce Richardson 
9146c3169a3SBruce Richardson static void
9156c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
9166c3169a3SBruce Richardson {
9176c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
9186c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9196c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9206c3169a3SBruce Richardson }
9216c3169a3SBruce Richardson 
9226c3169a3SBruce Richardson static void
9236c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
9246c3169a3SBruce Richardson {
9256c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
9266c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
9276c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
9286c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
9296c3169a3SBruce Richardson 	} else {
9306c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
9316c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
9326c3169a3SBruce Richardson 	}
9336c3169a3SBruce Richardson }
9346c3169a3SBruce Richardson 
93527046236SStephen Hemminger static void
9366c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
9376c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
9386c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
9396c3169a3SBruce Richardson {
9406c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
9416c3169a3SBruce Richardson 	int err, len[2];
9426c3169a3SBruce Richardson 
94327046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
944e9083127SJianfeng Tan 		PMD_DRV_LOG(INFO, "host does not support mac table");
94527046236SStephen Hemminger 		return;
94627046236SStephen Hemminger 	}
94727046236SStephen Hemminger 
9486c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
9496c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
9506c3169a3SBruce Richardson 
9516c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
9526c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
9536c3169a3SBruce Richardson 
9546c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
9556c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
9566c3169a3SBruce Richardson 
9576c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
9586c3169a3SBruce Richardson 	if (err != 0)
9596c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
9606c3169a3SBruce Richardson }
9616c3169a3SBruce Richardson 
9626c3169a3SBruce Richardson static void
9636c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
9646c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
9656c3169a3SBruce Richardson {
9666c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
9676c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
9686c3169a3SBruce Richardson 	unsigned int i;
9696c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
9706c3169a3SBruce Richardson 
9716c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
9726c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
9736c3169a3SBruce Richardson 		return;
9746c3169a3SBruce Richardson 	}
9756c3169a3SBruce Richardson 
9766c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
9776c3169a3SBruce Richardson 	uc->entries = 0;
9786c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
9796c3169a3SBruce Richardson 	mc->entries = 0;
9806c3169a3SBruce Richardson 
9816c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
9826c3169a3SBruce Richardson 		const struct ether_addr *addr
9836c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
9846c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
9856c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
9866c3169a3SBruce Richardson 
9876c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
9886c3169a3SBruce Richardson 	}
9896c3169a3SBruce Richardson 
9906c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
9916c3169a3SBruce Richardson }
9926c3169a3SBruce Richardson 
9936c3169a3SBruce Richardson static void
9946c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
9956c3169a3SBruce Richardson {
9966c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
9976c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
9986c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
9996c3169a3SBruce Richardson 	unsigned int i;
10006c3169a3SBruce Richardson 
10016c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
10026c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
10036c3169a3SBruce Richardson 		return;
10046c3169a3SBruce Richardson 	}
10056c3169a3SBruce Richardson 
10066c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
10076c3169a3SBruce Richardson 	uc->entries = 0;
10086c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
10096c3169a3SBruce Richardson 	mc->entries = 0;
10106c3169a3SBruce Richardson 
10116c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
10126c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
10136c3169a3SBruce Richardson 
10146c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
10156c3169a3SBruce Richardson 			continue;
10166c3169a3SBruce Richardson 
10176c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
10186c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
10196c3169a3SBruce Richardson 	}
10206c3169a3SBruce Richardson 
10216c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
10226c3169a3SBruce Richardson }
10236c3169a3SBruce Richardson 
10246c3169a3SBruce Richardson static void
10256c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
10266c3169a3SBruce Richardson {
10276c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10286c3169a3SBruce Richardson 
10296c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
10306c3169a3SBruce Richardson 
10316c3169a3SBruce Richardson 	/* Use atomic update if available */
10326c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
10336c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
10346c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
10356c3169a3SBruce Richardson 
10366c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
10376c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
10386c3169a3SBruce Richardson 
10396c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
10406c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
10416c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
10426c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
10436c3169a3SBruce Richardson }
10446c3169a3SBruce Richardson 
10456c3169a3SBruce Richardson static int
10466c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
10476c3169a3SBruce Richardson {
10486c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
10496c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
10506c3169a3SBruce Richardson 	int len;
10516c3169a3SBruce Richardson 
10526c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
10536c3169a3SBruce Richardson 		return -ENOTSUP;
10546c3169a3SBruce Richardson 
10556c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
10566c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
10576c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
10586c3169a3SBruce Richardson 	len = sizeof(vlan_id);
10596c3169a3SBruce Richardson 
10606c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
10616c3169a3SBruce Richardson }
10626c3169a3SBruce Richardson 
10636ba1f63bSYuanhan Liu static int
106460e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
10656c3169a3SBruce Richardson {
10663891f233SYuanhan Liu 	uint64_t host_features;
10676c3169a3SBruce Richardson 
10686c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
10693891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
107060e6f470SOlivier Matz 		req_features);
10716c3169a3SBruce Richardson 
10726c3169a3SBruce Richardson 	/* Read device(host) feature bits */
1073d5bbeefcSYuanhan Liu 	host_features = hw->vtpci_ops->get_features(hw);
10743891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
10756c3169a3SBruce Richardson 		host_features);
10766c3169a3SBruce Richardson 
10776c3169a3SBruce Richardson 	/*
10786c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
10796c3169a3SBruce Richardson 	 * guest feature bits.
10806c3169a3SBruce Richardson 	 */
108160e6f470SOlivier Matz 	hw->guest_features = req_features;
10826c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
10833891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
10846c3169a3SBruce Richardson 		hw->guest_features);
10856ba1f63bSYuanhan Liu 
10866ba1f63bSYuanhan Liu 	if (hw->modern) {
10876ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
10886ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
10896ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
10906ba1f63bSYuanhan Liu 			return -1;
10916ba1f63bSYuanhan Liu 		}
10926ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
10936ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
10946ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
10956ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
10966ba1f63bSYuanhan Liu 			return -1;
10976ba1f63bSYuanhan Liu 		}
10986ba1f63bSYuanhan Liu 	}
10996ba1f63bSYuanhan Liu 
110060e6f470SOlivier Matz 	hw->req_guest_features = req_features;
110160e6f470SOlivier Matz 
11026ba1f63bSYuanhan Liu 	return 0;
11036c3169a3SBruce Richardson }
11046c3169a3SBruce Richardson 
11056c3169a3SBruce Richardson /*
11066c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
11076c3169a3SBruce Richardson  * if link state changed.
11086c3169a3SBruce Richardson  */
11096c3169a3SBruce Richardson static void
11106c3169a3SBruce Richardson virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
11116c3169a3SBruce Richardson 			 void *param)
11126c3169a3SBruce Richardson {
11136c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
11146c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11156c3169a3SBruce Richardson 	uint8_t isr;
11166c3169a3SBruce Richardson 
11176c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
11186c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
11196c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
11206c3169a3SBruce Richardson 
11216c3169a3SBruce Richardson 	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
11226c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
11236c3169a3SBruce Richardson 
11246c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
11256c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
11266c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
1127c1ceaf3aSBernard Iremonger 						      RTE_ETH_EVENT_INTR_LSC, NULL);
11286c3169a3SBruce Richardson 	}
11296c3169a3SBruce Richardson 
11306c3169a3SBruce Richardson }
11316c3169a3SBruce Richardson 
11326c3169a3SBruce Richardson static void
11336c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
11346c3169a3SBruce Richardson {
11356c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11366c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
11376c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
11386c3169a3SBruce Richardson 	else
11396c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
11406c3169a3SBruce Richardson }
11416c3169a3SBruce Richardson 
114260e6f470SOlivier Matz /* reset device and renegotiate features if needed */
1143198ab336SOlivier Matz static int
114460e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
11456c3169a3SBruce Richardson {
11466c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11476c3169a3SBruce Richardson 	struct virtio_net_config *config;
11486c3169a3SBruce Richardson 	struct virtio_net_config local_config;
1149198ab336SOlivier Matz 	struct rte_pci_device *pci_dev = eth_dev->pci_dev;
11506c3169a3SBruce Richardson 
11516c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
11526c3169a3SBruce Richardson 	vtpci_reset(hw);
11536c3169a3SBruce Richardson 
11546c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
11556c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
11566c3169a3SBruce Richardson 
11576c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
11586c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
115960e6f470SOlivier Matz 	if (virtio_negotiate_features(hw, req_features) < 0)
11606ba1f63bSYuanhan Liu 		return -1;
11616c3169a3SBruce Richardson 
1162954ea115SStephen Hemminger 	/* If host does not support status then disable LSC */
1163954ea115SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1164198ab336SOlivier Matz 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1165198ab336SOlivier Matz 	else
1166198ab336SOlivier Matz 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1167954ea115SStephen Hemminger 
1168d15339b9SBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1169d15339b9SBernard Iremonger 
11706c3169a3SBruce Richardson 	rx_func_get(eth_dev);
11716c3169a3SBruce Richardson 
11726c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
11736ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
11746ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
11756c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
11766c3169a3SBruce Richardson 	else
11776c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
11786c3169a3SBruce Richardson 
11796c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
11806c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
11816c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
11826c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
11836c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
11846c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
11856c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
11866c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
11876c3169a3SBruce Richardson 
11886c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
11896c3169a3SBruce Richardson 		config = &local_config;
11906c3169a3SBruce Richardson 
11916d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
11926d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
11936d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
11946d7740e2SChangchun Ouyang 
11956c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
11966d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
11976d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
11986d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
11996c3169a3SBruce Richardson 		} else {
12006c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12016c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
12026c3169a3SBruce Richardson 			config->status = 0;
12036c3169a3SBruce Richardson 		}
12046c3169a3SBruce Richardson 
12056c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
12066d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
12076d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
12086d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
12096d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
12106c3169a3SBruce Richardson 		} else {
12116c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12126c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
12136c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
12146c3169a3SBruce Richardson 		}
12156c3169a3SBruce Richardson 
121645e4acd4SOlivier Matz 		hw->max_queue_pairs = config->max_virtqueue_pairs;
12176c3169a3SBruce Richardson 
12186c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
12196c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
12206c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
12216c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
12226c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12236c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
12246c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
12256c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
12266c3169a3SBruce Richardson 	} else {
122745e4acd4SOlivier Matz 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
122845e4acd4SOlivier Matz 		hw->max_queue_pairs = 1;
12296c3169a3SBruce Richardson 	}
12306c3169a3SBruce Richardson 
1231ce2eabddSJianfeng Tan 	if (pci_dev)
12326c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
12336c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
12346c3169a3SBruce Richardson 			pci_dev->id.device_id);
12356c3169a3SBruce Richardson 
1236198ab336SOlivier Matz 	return 0;
1237198ab336SOlivier Matz }
1238198ab336SOlivier Matz 
1239198ab336SOlivier Matz /*
1240198ab336SOlivier Matz  * This function is based on probe() function in virtio_pci.c
1241198ab336SOlivier Matz  * It returns 0 on success.
1242198ab336SOlivier Matz  */
1243198ab336SOlivier Matz int
1244198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1245198ab336SOlivier Matz {
1246198ab336SOlivier Matz 	struct virtio_hw *hw = eth_dev->data->dev_private;
1247198ab336SOlivier Matz 	struct rte_pci_device *pci_dev;
1248198ab336SOlivier Matz 	uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
1249198ab336SOlivier Matz 	int ret;
1250198ab336SOlivier Matz 
1251198ab336SOlivier Matz 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1252198ab336SOlivier Matz 
1253198ab336SOlivier Matz 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1254198ab336SOlivier Matz 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1255198ab336SOlivier Matz 
1256198ab336SOlivier Matz 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1257198ab336SOlivier Matz 		rx_func_get(eth_dev);
1258198ab336SOlivier Matz 		return 0;
1259198ab336SOlivier Matz 	}
1260198ab336SOlivier Matz 
1261198ab336SOlivier Matz 	/* Allocate memory for storing MAC addresses */
1262198ab336SOlivier Matz 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1263198ab336SOlivier Matz 	if (eth_dev->data->mac_addrs == NULL) {
1264198ab336SOlivier Matz 		PMD_INIT_LOG(ERR,
1265198ab336SOlivier Matz 			"Failed to allocate %d bytes needed to store MAC addresses",
1266198ab336SOlivier Matz 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1267198ab336SOlivier Matz 		return -ENOMEM;
1268198ab336SOlivier Matz 	}
1269198ab336SOlivier Matz 
1270198ab336SOlivier Matz 	pci_dev = eth_dev->pci_dev;
1271198ab336SOlivier Matz 
1272198ab336SOlivier Matz 	if (pci_dev) {
1273198ab336SOlivier Matz 		ret = vtpci_init(pci_dev, hw, &dev_flags);
1274198ab336SOlivier Matz 		if (ret)
1275198ab336SOlivier Matz 			return ret;
1276198ab336SOlivier Matz 	}
1277198ab336SOlivier Matz 
1278198ab336SOlivier Matz 	eth_dev->data->dev_flags = dev_flags;
1279198ab336SOlivier Matz 
128060e6f470SOlivier Matz 	/* reset device and negotiate default features */
128196cb6711SOlivier Matz 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1282198ab336SOlivier Matz 	if (ret < 0)
1283198ab336SOlivier Matz 		return ret;
1284198ab336SOlivier Matz 
12856c3169a3SBruce Richardson 	/* Setup interrupt callback  */
128662a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
12876c3169a3SBruce Richardson 		rte_intr_callback_register(&pci_dev->intr_handle,
12886c3169a3SBruce Richardson 			virtio_interrupt_handler, eth_dev);
12896c3169a3SBruce Richardson 
12906c3169a3SBruce Richardson 	return 0;
12916c3169a3SBruce Richardson }
12926c3169a3SBruce Richardson 
1293abf4c84bSBernard Iremonger static int
1294abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1295abf4c84bSBernard Iremonger {
1296abf4c84bSBernard Iremonger 	struct rte_pci_device *pci_dev;
1297*3f3b3d54SYuanhan Liu 	struct virtio_hw *hw = eth_dev->data->dev_private;
1298abf4c84bSBernard Iremonger 
1299abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1300abf4c84bSBernard Iremonger 
1301abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1302abf4c84bSBernard Iremonger 		return -EPERM;
1303abf4c84bSBernard Iremonger 
1304*3f3b3d54SYuanhan Liu 	if (hw->started == 1) {
1305*3f3b3d54SYuanhan Liu 		virtio_dev_stop(eth_dev);
1306abf4c84bSBernard Iremonger 		virtio_dev_close(eth_dev);
1307*3f3b3d54SYuanhan Liu 	}
1308abf4c84bSBernard Iremonger 	pci_dev = eth_dev->pci_dev;
1309abf4c84bSBernard Iremonger 
1310abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1311abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1312abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1313abf4c84bSBernard Iremonger 
1314abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1315abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1316abf4c84bSBernard Iremonger 
1317abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
131862a785a6SJianfeng Tan 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1319abf4c84bSBernard Iremonger 		rte_intr_callback_unregister(&pci_dev->intr_handle,
1320abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1321abf4c84bSBernard Iremonger 						eth_dev);
13226ba1f63bSYuanhan Liu 	rte_eal_pci_unmap_device(pci_dev);
1323abf4c84bSBernard Iremonger 
1324abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1325abf4c84bSBernard Iremonger 
1326abf4c84bSBernard Iremonger 	return 0;
1327abf4c84bSBernard Iremonger }
1328abf4c84bSBernard Iremonger 
13296c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
13306c52c126SStephen Hemminger 	.pci_drv = {
1331cd433ce5SDavid Marchand 		.driver = {
1332cd433ce5SDavid Marchand 			.name = "net_virtio",
1333cd433ce5SDavid Marchand 		},
13346c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
1335abf4c84bSBernard Iremonger 		.drv_flags = RTE_PCI_DRV_DETACHABLE,
1336c830cb29SDavid Marchand 		.probe = rte_eth_dev_pci_probe,
1337c830cb29SDavid Marchand 		.remove = rte_eth_dev_pci_remove,
13386c3169a3SBruce Richardson 	},
13396c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1340abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
13416c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
13426c3169a3SBruce Richardson };
13436c3169a3SBruce Richardson 
1344c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init);
1345c830cb29SDavid Marchand static void
1346c830cb29SDavid Marchand rte_virtio_pmd_init(void)
13476c3169a3SBruce Richardson {
13486c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
13496c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1350c830cb29SDavid Marchand 		return;
13516c3169a3SBruce Richardson 	}
13526c3169a3SBruce Richardson 
1353c830cb29SDavid Marchand 	rte_eal_pci_register(&rte_virtio_pmd.pci_drv);
13546c3169a3SBruce Richardson }
13556c3169a3SBruce Richardson 
13566c3169a3SBruce Richardson /*
13576c3169a3SBruce Richardson  * Configure virtio device
13586c3169a3SBruce Richardson  * It returns 0 on success.
13596c3169a3SBruce Richardson  */
13606c3169a3SBruce Richardson static int
13616c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
13626c3169a3SBruce Richardson {
13636c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
13646c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
136560e6f470SOlivier Matz 	uint64_t req_features;
136645e4acd4SOlivier Matz 	int ret;
13676c3169a3SBruce Richardson 
13686c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
136996cb6711SOlivier Matz 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
137096cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum)
137196cb6711SOlivier Matz 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
137286d59b21SOlivier Matz 	if (rxmode->enable_lro)
137386d59b21SOlivier Matz 		req_features |=
137486d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
137586d59b21SOlivier Matz 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
13766c3169a3SBruce Richardson 
137760e6f470SOlivier Matz 	/* if request features changed, reinit the device */
137860e6f470SOlivier Matz 	if (req_features != hw->req_guest_features) {
137960e6f470SOlivier Matz 		ret = virtio_init_device(dev, req_features);
138060e6f470SOlivier Matz 		if (ret < 0)
138160e6f470SOlivier Matz 			return ret;
138260e6f470SOlivier Matz 	}
138360e6f470SOlivier Matz 
138496cb6711SOlivier Matz 	if (rxmode->hw_ip_checksum &&
138596cb6711SOlivier Matz 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
138696cb6711SOlivier Matz 		PMD_DRV_LOG(NOTICE,
138796cb6711SOlivier Matz 			"rx ip checksum not available on this host");
138896cb6711SOlivier Matz 		return -ENOTSUP;
138996cb6711SOlivier Matz 	}
139096cb6711SOlivier Matz 
139186d59b21SOlivier Matz 	if (rxmode->enable_lro &&
139286d59b21SOlivier Matz 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
139386d59b21SOlivier Matz 			!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) {
139486d59b21SOlivier Matz 		PMD_DRV_LOG(NOTICE,
139586d59b21SOlivier Matz 			"lro not available on this host");
139686d59b21SOlivier Matz 		return -ENOTSUP;
139786d59b21SOlivier Matz 	}
139886d59b21SOlivier Matz 
139945e4acd4SOlivier Matz 	/* Setup and start control queue */
140045e4acd4SOlivier Matz 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
140145e4acd4SOlivier Matz 		ret = virtio_dev_cq_queue_setup(dev,
140245e4acd4SOlivier Matz 			hw->max_queue_pairs * 2,
140345e4acd4SOlivier Matz 			SOCKET_ID_ANY);
140445e4acd4SOlivier Matz 		if (ret < 0)
140545e4acd4SOlivier Matz 			return ret;
140645e4acd4SOlivier Matz 		virtio_dev_cq_start(dev);
140745e4acd4SOlivier Matz 	}
140845e4acd4SOlivier Matz 
14096c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
14106c3169a3SBruce Richardson 
14116c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
14126c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
14136c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
14146c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
14156c3169a3SBruce Richardson 		return -ENOTSUP;
14166c3169a3SBruce Richardson 	}
14176c3169a3SBruce Richardson 
141862a785a6SJianfeng Tan 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
14196c3169a3SBruce Richardson 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
14206c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
14216c3169a3SBruce Richardson 			return -EBUSY;
14226c3169a3SBruce Richardson 		}
14236c3169a3SBruce Richardson 
14246c3169a3SBruce Richardson 	return 0;
14256c3169a3SBruce Richardson }
14266c3169a3SBruce Richardson 
14276c3169a3SBruce Richardson 
14286c3169a3SBruce Richardson static int
14296c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
14306c3169a3SBruce Richardson {
14316c3169a3SBruce Richardson 	uint16_t nb_queues, i;
14326c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
143301ad44fdSHuawei Xie 	struct virtnet_rx *rxvq;
143401ad44fdSHuawei Xie 	struct virtnet_tx *txvq __rte_unused;
14356c3169a3SBruce Richardson 
14366c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1437954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
143862a785a6SJianfeng Tan 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
14396c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
14406c3169a3SBruce Richardson 			return -ENOTSUP;
14416c3169a3SBruce Richardson 		}
14426c3169a3SBruce Richardson 
14436c3169a3SBruce Richardson 		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
14446c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
14456c3169a3SBruce Richardson 			return -EIO;
14466c3169a3SBruce Richardson 		}
14476c3169a3SBruce Richardson 	}
14486c3169a3SBruce Richardson 
14496c3169a3SBruce Richardson 	/* Initialize Link state */
14506c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
14516c3169a3SBruce Richardson 
14526c3169a3SBruce Richardson 	/* On restart after stop do not touch queues */
14536c3169a3SBruce Richardson 	if (hw->started)
14546c3169a3SBruce Richardson 		return 0;
14556c3169a3SBruce Richardson 
14566c3169a3SBruce Richardson 	/* Do final configuration before rx/tx engine starts */
14576c3169a3SBruce Richardson 	virtio_dev_rxtx_start(dev);
14586c3169a3SBruce Richardson 	vtpci_reinit_complete(hw);
14596c3169a3SBruce Richardson 
14606c3169a3SBruce Richardson 	hw->started = 1;
14616c3169a3SBruce Richardson 
14626c3169a3SBruce Richardson 	/*Notify the backend
14636c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
14646c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
14656c3169a3SBruce Richardson 	 */
14666c3169a3SBruce Richardson 	nb_queues = dev->data->nb_rx_queues;
14676c3169a3SBruce Richardson 	if (nb_queues > 1) {
14686c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
14696c3169a3SBruce Richardson 			return -EINVAL;
14706c3169a3SBruce Richardson 	}
14716c3169a3SBruce Richardson 
14726c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
14736c3169a3SBruce Richardson 
147401ad44fdSHuawei Xie 	for (i = 0; i < nb_queues; i++) {
147501ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
147601ad44fdSHuawei Xie 		virtqueue_notify(rxvq->vq);
147701ad44fdSHuawei Xie 	}
14786c3169a3SBruce Richardson 
14796c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
14806c3169a3SBruce Richardson 
148101ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
148201ad44fdSHuawei Xie 		rxvq = dev->data->rx_queues[i];
148301ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
148401ad44fdSHuawei Xie 	}
14856c3169a3SBruce Richardson 
148601ad44fdSHuawei Xie 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
148701ad44fdSHuawei Xie 		txvq = dev->data->tx_queues[i];
148801ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
148901ad44fdSHuawei Xie 	}
14906c3169a3SBruce Richardson 
14916c3169a3SBruce Richardson 	return 0;
14926c3169a3SBruce Richardson }
14936c3169a3SBruce Richardson 
14946c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
14956c3169a3SBruce Richardson {
14966c3169a3SBruce Richardson 	struct rte_mbuf *buf;
14976c3169a3SBruce Richardson 	int i, mbuf_num = 0;
14986c3169a3SBruce Richardson 
14996c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
150001ad44fdSHuawei Xie 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
150101ad44fdSHuawei Xie 
15026c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15036c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
150401ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15056c3169a3SBruce Richardson 
150601ad44fdSHuawei Xie 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq);
150701ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) {
15086c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15096c3169a3SBruce Richardson 			mbuf_num++;
15106c3169a3SBruce Richardson 		}
15116c3169a3SBruce Richardson 
15126c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15136c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15146c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
151501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(rxvq->vq);
15166c3169a3SBruce Richardson 	}
15176c3169a3SBruce Richardson 
15186c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
151901ad44fdSHuawei Xie 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
152001ad44fdSHuawei Xie 
15216c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15226c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
15236c3169a3SBruce Richardson 			     i);
152401ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15256c3169a3SBruce Richardson 
15266c3169a3SBruce Richardson 		mbuf_num = 0;
152701ad44fdSHuawei Xie 		while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) {
15286c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
15296c3169a3SBruce Richardson 			mbuf_num++;
15306c3169a3SBruce Richardson 		}
15316c3169a3SBruce Richardson 
15326c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
15336c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
15346c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
153501ad44fdSHuawei Xie 		VIRTQUEUE_DUMP(txvq->vq);
15366c3169a3SBruce Richardson 	}
15376c3169a3SBruce Richardson }
15386c3169a3SBruce Richardson 
15396c3169a3SBruce Richardson /*
15406c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
15416c3169a3SBruce Richardson  */
15426c3169a3SBruce Richardson static void
15436c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
15446c3169a3SBruce Richardson {
15456c3169a3SBruce Richardson 	struct rte_eth_link link;
15466c3169a3SBruce Richardson 
15476c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
15486c3169a3SBruce Richardson 
15496c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
15506c3169a3SBruce Richardson 		rte_intr_disable(&dev->pci_dev->intr_handle);
15516c3169a3SBruce Richardson 
15526c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15536c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15546c3169a3SBruce Richardson }
15556c3169a3SBruce Richardson 
15566c3169a3SBruce Richardson static int
15576c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
15586c3169a3SBruce Richardson {
15596c3169a3SBruce Richardson 	struct rte_eth_link link, old;
15606c3169a3SBruce Richardson 	uint16_t status;
15616c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
15626c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15636c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
15646c3169a3SBruce Richardson 	old = link;
156511319000SMarc Sune 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
15666c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
15676c3169a3SBruce Richardson 
15686c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
15696c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
15706c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
15716c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
15726c3169a3SBruce Richardson 				&status, sizeof(status));
15736c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
157409419f23SThomas Monjalon 			link.link_status = ETH_LINK_DOWN;
15756c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
15766c3169a3SBruce Richardson 				     dev->data->port_id);
15776c3169a3SBruce Richardson 		} else {
157809419f23SThomas Monjalon 			link.link_status = ETH_LINK_UP;
15796c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
15806c3169a3SBruce Richardson 				     dev->data->port_id);
15816c3169a3SBruce Richardson 		}
15826c3169a3SBruce Richardson 	} else {
158309419f23SThomas Monjalon 		link.link_status = ETH_LINK_UP;
15846c3169a3SBruce Richardson 	}
15856c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15866c3169a3SBruce Richardson 
15876c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
15886c3169a3SBruce Richardson }
15896c3169a3SBruce Richardson 
15906c3169a3SBruce Richardson static void
15916c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
15926c3169a3SBruce Richardson {
159369657304SOlivier Matz 	uint64_t tso_mask;
15946c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
15956c3169a3SBruce Richardson 
1596ce2eabddSJianfeng Tan 	if (dev->pci_dev)
15972f3193cfSJan Viktorin 		dev_info->driver_name = dev->driver->pci_drv.driver.name;
1598ce2eabddSJianfeng Tan 	else
1599e8df94b8SJianfeng Tan 		dev_info->driver_name = "virtio_user PMD";
160045e4acd4SOlivier Matz 	dev_info->max_rx_queues =
160145e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
160245e4acd4SOlivier Matz 	dev_info->max_tx_queues =
160345e4acd4SOlivier Matz 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
16046c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
16056c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
16066c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
16076c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
16086c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
16096c3169a3SBruce Richardson 	};
161096cb6711SOlivier Matz 	dev_info->rx_offload_capa =
161196cb6711SOlivier Matz 		DEV_RX_OFFLOAD_TCP_CKSUM |
161286d59b21SOlivier Matz 		DEV_RX_OFFLOAD_UDP_CKSUM |
161386d59b21SOlivier Matz 		DEV_RX_OFFLOAD_TCP_LRO;
161458169a9cSOlivier Matz 	dev_info->tx_offload_capa = 0;
161558169a9cSOlivier Matz 
161658169a9cSOlivier Matz 	if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) {
161758169a9cSOlivier Matz 		dev_info->tx_offload_capa |=
161858169a9cSOlivier Matz 			DEV_TX_OFFLOAD_UDP_CKSUM |
161958169a9cSOlivier Matz 			DEV_TX_OFFLOAD_TCP_CKSUM;
162058169a9cSOlivier Matz 	}
162169657304SOlivier Matz 
162269657304SOlivier Matz 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
162369657304SOlivier Matz 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
162469657304SOlivier Matz 	if ((hw->guest_features & tso_mask) == tso_mask)
162569657304SOlivier Matz 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
16266c3169a3SBruce Richardson }
16276c3169a3SBruce Richardson 
16286c3169a3SBruce Richardson /*
16296c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
16306c3169a3SBruce Richardson  */
16316c3169a3SBruce Richardson static int
16326c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
16336c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
16346c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
16356c3169a3SBruce Richardson {
16366c3169a3SBruce Richardson 	return 0;
16376c3169a3SBruce Richardson }
16386c3169a3SBruce Richardson 
163901f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
164001f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
1641