xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision 9a0615af)
16c3169a3SBruce Richardson /*-
26c3169a3SBruce Richardson  *   BSD LICENSE
36c3169a3SBruce Richardson  *
4abf4c84bSBernard Iremonger  *   Copyright(c) 2010-2015 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 
626c3169a3SBruce Richardson 
636c3169a3SBruce Richardson static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
64abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
656c3169a3SBruce Richardson static int  virtio_dev_configure(struct rte_eth_dev *dev);
666c3169a3SBruce Richardson static int  virtio_dev_start(struct rte_eth_dev *dev);
676c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev);
686c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
696c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
706c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
716c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
726c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev,
736c3169a3SBruce Richardson 				struct rte_eth_dev_info *dev_info);
746c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev,
756c3169a3SBruce Richardson 	__rte_unused int wait_to_complete);
766c3169a3SBruce Richardson 
776c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw);
786c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw);
796c3169a3SBruce Richardson 
8076d4c652SHarry van Haaren static void virtio_dev_stats_get(struct rte_eth_dev *dev,
8176d4c652SHarry van Haaren 				 struct rte_eth_stats *stats);
8276d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
8376d4c652SHarry van Haaren 				 struct rte_eth_xstats *xstats, unsigned n);
846c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
856c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
866c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
876c3169a3SBruce Richardson 				uint16_t vlan_id, int on);
886c3169a3SBruce Richardson static void virtio_mac_addr_add(struct rte_eth_dev *dev,
896c3169a3SBruce Richardson 				struct ether_addr *mac_addr,
906c3169a3SBruce Richardson 				uint32_t index, uint32_t vmdq __rte_unused);
916c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
926c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev,
936c3169a3SBruce Richardson 				struct ether_addr *mac_addr);
946c3169a3SBruce Richardson 
956c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set(
966c3169a3SBruce Richardson 	__rte_unused struct rte_eth_dev *eth_dev,
976c3169a3SBruce Richardson 	__rte_unused uint16_t queue_id,
986c3169a3SBruce Richardson 	__rte_unused uint8_t stat_idx,
996c3169a3SBruce Richardson 	__rte_unused uint8_t is_rx);
1006c3169a3SBruce Richardson 
1016c3169a3SBruce Richardson /*
1026c3169a3SBruce Richardson  * The set of PCI devices this driver supports
1036c3169a3SBruce Richardson  */
1046c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = {
1056c3169a3SBruce Richardson 
1066c3169a3SBruce Richardson #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
1076c3169a3SBruce Richardson #include "rte_pci_dev_ids.h"
1086c3169a3SBruce Richardson 
1096c3169a3SBruce Richardson { .vendor_id = 0, /* sentinel */ },
1106c3169a3SBruce Richardson };
1116c3169a3SBruce Richardson 
11276d4c652SHarry van Haaren struct rte_virtio_xstats_name_off {
11376d4c652SHarry van Haaren 	char name[RTE_ETH_XSTATS_NAME_SIZE];
11476d4c652SHarry van Haaren 	unsigned offset;
11576d4c652SHarry van Haaren };
11676d4c652SHarry van Haaren 
11776d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */
11876d4c652SHarry van Haaren static const struct rte_virtio_xstats_name_off rte_virtio_q_stat_strings[] = {
11976d4c652SHarry van Haaren 	{"good_packets",           offsetof(struct virtqueue, packets)},
12076d4c652SHarry van Haaren 	{"good_bytes",             offsetof(struct virtqueue, bytes)},
12176d4c652SHarry van Haaren 	{"errors",                 offsetof(struct virtqueue, errors)},
12276d4c652SHarry van Haaren 	{"multicast_packets",      offsetof(struct virtqueue, multicast)},
12376d4c652SHarry van Haaren 	{"broadcast_packets",      offsetof(struct virtqueue, broadcast)},
12476d4c652SHarry van Haaren 	{"undersize_packets",      offsetof(struct virtqueue, size_bins[0])},
12576d4c652SHarry van Haaren 	{"size_64_packets",        offsetof(struct virtqueue, size_bins[1])},
12676d4c652SHarry van Haaren 	{"size_65_127_packets",    offsetof(struct virtqueue, size_bins[2])},
12776d4c652SHarry van Haaren 	{"size_128_255_packets",   offsetof(struct virtqueue, size_bins[3])},
12876d4c652SHarry van Haaren 	{"size_256_511_packets",   offsetof(struct virtqueue, size_bins[4])},
12976d4c652SHarry van Haaren 	{"size_512_1023_packets",  offsetof(struct virtqueue, size_bins[5])},
13076d4c652SHarry van Haaren 	{"size_1024_1517_packets", offsetof(struct virtqueue, size_bins[6])},
13176d4c652SHarry van Haaren 	{"size_1518_max_packets",  offsetof(struct virtqueue, size_bins[7])},
13276d4c652SHarry van Haaren };
13376d4c652SHarry van Haaren 
13476d4c652SHarry van Haaren #define VIRTIO_NB_Q_XSTATS (sizeof(rte_virtio_q_stat_strings) / \
13576d4c652SHarry van Haaren 			    sizeof(rte_virtio_q_stat_strings[0]))
13676d4c652SHarry van Haaren 
1376c3169a3SBruce Richardson static int
1386c3169a3SBruce Richardson virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
1396c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1406c3169a3SBruce Richardson {
1419e71668bSDamjan Marion 	uint32_t head, i;
1426c3169a3SBruce Richardson 	int k, sum = 0;
1436c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
1446c3169a3SBruce Richardson 	struct virtio_pmd_ctrl result;
1456c3169a3SBruce Richardson 
1466c3169a3SBruce Richardson 	ctrl->status = status;
1476c3169a3SBruce Richardson 
1489e71668bSDamjan Marion 	if (!(vq && vq->hw->cvq)) {
1496c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR,
1506c3169a3SBruce Richardson 			     "%s(): Control queue is not supported.",
1516c3169a3SBruce Richardson 			     __func__);
1526c3169a3SBruce Richardson 		return -1;
1536c3169a3SBruce Richardson 	}
1549e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1556c3169a3SBruce Richardson 
1566c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1576c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1586c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1596c3169a3SBruce Richardson 
1606c3169a3SBruce Richardson 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
1616c3169a3SBruce Richardson 		return -1;
1626c3169a3SBruce Richardson 
1636c3169a3SBruce Richardson 	memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
1646c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1656c3169a3SBruce Richardson 
1666c3169a3SBruce Richardson 	/*
1676c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1686c3169a3SBruce Richardson 	 * One TX packet for header;
1696c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1706c3169a3SBruce Richardson 	 * One RX packet for ACK.
1716c3169a3SBruce Richardson 	 */
1726c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
1736c3169a3SBruce Richardson 	vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
1746c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1756c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1766c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
1776c3169a3SBruce Richardson 
1786c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
1796c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
1806c3169a3SBruce Richardson 		vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
1816c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
1826c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
1836c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
1846c3169a3SBruce Richardson 		sum += dlen[k];
1856c3169a3SBruce Richardson 		vq->vq_free_cnt--;
1866c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
1876c3169a3SBruce Richardson 	}
1886c3169a3SBruce Richardson 
1896c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
1906c3169a3SBruce Richardson 	vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
1916c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
1926c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
1936c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1946c3169a3SBruce Richardson 
1956c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
1966c3169a3SBruce Richardson 
1976c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
1986c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
1996c3169a3SBruce Richardson 
2006c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
2016c3169a3SBruce Richardson 
2026c3169a3SBruce Richardson 	virtqueue_notify(vq);
2036c3169a3SBruce Richardson 
2046c3169a3SBruce Richardson 	rte_rmb();
2056c3169a3SBruce Richardson 	while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
2066c3169a3SBruce Richardson 		rte_rmb();
2076c3169a3SBruce Richardson 		usleep(100);
2086c3169a3SBruce Richardson 	}
2096c3169a3SBruce Richardson 
2106c3169a3SBruce Richardson 	while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
2116c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
2126c3169a3SBruce Richardson 		struct vring_used_elem *uep;
2136c3169a3SBruce Richardson 
2146c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
2156c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
2166c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
2176c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
2186c3169a3SBruce Richardson 		desc_idx = idx;
2196c3169a3SBruce Richardson 
2206c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
2216c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
2226c3169a3SBruce Richardson 			vq->vq_free_cnt++;
2236c3169a3SBruce Richardson 		}
2246c3169a3SBruce Richardson 
2256c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2266c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2276c3169a3SBruce Richardson 
2286c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2296c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2306c3169a3SBruce Richardson 	}
2316c3169a3SBruce Richardson 
2326c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2336c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2346c3169a3SBruce Richardson 
2356c3169a3SBruce Richardson 	memcpy(&result, vq->virtio_net_hdr_mz->addr,
2366c3169a3SBruce Richardson 			sizeof(struct virtio_pmd_ctrl));
2376c3169a3SBruce Richardson 
2386c3169a3SBruce Richardson 	return result.status;
2396c3169a3SBruce Richardson }
2406c3169a3SBruce Richardson 
2416c3169a3SBruce Richardson static int
2426c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2436c3169a3SBruce Richardson {
2446c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2456c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2466c3169a3SBruce Richardson 	int dlen[1];
2476c3169a3SBruce Richardson 	int ret;
2486c3169a3SBruce Richardson 
2496c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2506c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2516c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2526c3169a3SBruce Richardson 
2536c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2546c3169a3SBruce Richardson 
2556c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2566c3169a3SBruce Richardson 	if (ret) {
2576c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2586c3169a3SBruce Richardson 			  "failed, this is too late now...");
2596c3169a3SBruce Richardson 		return -EINVAL;
2606c3169a3SBruce Richardson 	}
2616c3169a3SBruce Richardson 
2626c3169a3SBruce Richardson 	return 0;
2636c3169a3SBruce Richardson }
2646c3169a3SBruce Richardson 
2655382b188SBernard Iremonger void
2665382b188SBernard Iremonger virtio_dev_queue_release(struct virtqueue *vq) {
267ce8e1218SBernard Iremonger 	struct virtio_hw *hw;
2685382b188SBernard Iremonger 
2695382b188SBernard Iremonger 	if (vq) {
270ce8e1218SBernard Iremonger 		hw = vq->hw;
271d5bbeefcSYuanhan Liu 		hw->vtpci_ops->del_queue(hw, vq);
2725382b188SBernard Iremonger 
273cab04612SHuawei Xie 		rte_free(vq->sw_ring);
2745382b188SBernard Iremonger 		rte_free(vq);
2755382b188SBernard Iremonger 	}
2765382b188SBernard Iremonger }
2775382b188SBernard Iremonger 
2786c3169a3SBruce Richardson int virtio_dev_queue_setup(struct rte_eth_dev *dev,
2796c3169a3SBruce Richardson 			int queue_type,
2806c3169a3SBruce Richardson 			uint16_t queue_idx,
2816c3169a3SBruce Richardson 			uint16_t vtpci_queue_idx,
2826c3169a3SBruce Richardson 			uint16_t nb_desc,
2836c3169a3SBruce Richardson 			unsigned int socket_id,
2846c3169a3SBruce Richardson 			struct virtqueue **pvq)
2856c3169a3SBruce Richardson {
2866c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
2876c3169a3SBruce Richardson 	const struct rte_memzone *mz;
2881e7bd238SStephen Hemminger 	unsigned int vq_size, size;
2896c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2906c3169a3SBruce Richardson 	struct virtqueue *vq = NULL;
2916c3169a3SBruce Richardson 
292d5bbeefcSYuanhan Liu 	PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
2936c3169a3SBruce Richardson 
2946c3169a3SBruce Richardson 	/*
2956c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
2966c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
2976c3169a3SBruce Richardson 	 */
298d5bbeefcSYuanhan Liu 	vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx);
2991e7bd238SStephen Hemminger 	PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
3006c3169a3SBruce Richardson 	if (vq_size == 0) {
3016c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
3026c3169a3SBruce Richardson 		return -EINVAL;
303d78deadaSStephen Hemminger 	}
304d78deadaSStephen Hemminger 
305d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
3066c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
3076c3169a3SBruce Richardson 		return -EINVAL;
308d78deadaSStephen Hemminger 	}
309d78deadaSStephen Hemminger 
3106c3169a3SBruce Richardson 	if (queue_type == VTNET_RQ) {
3116c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
3126c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
3136c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
3146c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
315cab04612SHuawei Xie 		vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
316cab04612SHuawei Xie 			(RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
317cab04612SHuawei Xie 			sizeof(vq->sw_ring[0]), RTE_CACHE_LINE_SIZE, socket_id);
3186c3169a3SBruce Richardson 	} else if (queue_type == VTNET_TQ) {
3196c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
3206c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
3216c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
3226c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
3236c3169a3SBruce Richardson 	} else if (queue_type == VTNET_CQ) {
3246c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
3256c3169a3SBruce Richardson 			dev->data->port_id);
3266c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
3276c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra),
3286c3169a3SBruce Richardson 			RTE_CACHE_LINE_SIZE);
3296c3169a3SBruce Richardson 	}
3306c3169a3SBruce Richardson 	if (vq == NULL) {
3316c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
332693f715dSHuawei Xie 		return -ENOMEM;
3336c3169a3SBruce Richardson 	}
334cab04612SHuawei Xie 	if (queue_type == VTNET_RQ && vq->sw_ring == NULL) {
335cab04612SHuawei Xie 		PMD_INIT_LOG(ERR, "%s: Can not allocate RX soft ring",
336cab04612SHuawei Xie 			__func__);
337cab04612SHuawei Xie 		rte_free(vq);
338cab04612SHuawei Xie 		return -ENOMEM;
339cab04612SHuawei Xie 	}
3406c3169a3SBruce Richardson 
3416c3169a3SBruce Richardson 	vq->hw = hw;
3426c3169a3SBruce Richardson 	vq->port_id = dev->data->port_id;
3436c3169a3SBruce Richardson 	vq->queue_id = queue_idx;
3446c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
3456c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
346945884f1SStephen Hemminger 
347945884f1SStephen Hemminger 	if (nb_desc == 0 || nb_desc > vq_size)
348945884f1SStephen Hemminger 		nb_desc = vq_size;
349945884f1SStephen Hemminger 	vq->vq_free_cnt = nb_desc;
3506c3169a3SBruce Richardson 
3516c3169a3SBruce Richardson 	/*
3526c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
3536c3169a3SBruce Richardson 	 */
3546c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
3556c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
3566c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
3576c3169a3SBruce Richardson 
3586c3169a3SBruce Richardson 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
3596c3169a3SBruce Richardson 		socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
3606c3169a3SBruce Richardson 	if (mz == NULL) {
361abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
362abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
363abf4c84bSBernard Iremonger 		if (mz == NULL) {
3646c3169a3SBruce Richardson 			rte_free(vq);
3656c3169a3SBruce Richardson 			return -ENOMEM;
3666c3169a3SBruce Richardson 		}
367abf4c84bSBernard Iremonger 	}
3686c3169a3SBruce Richardson 
3696c3169a3SBruce Richardson 	/*
3706c3169a3SBruce Richardson 	 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
3716c3169a3SBruce Richardson 	 * and only accepts 32 bit page frame number.
3726c3169a3SBruce Richardson 	 * Check if the allocated physical memory exceeds 16TB.
3736c3169a3SBruce Richardson 	 */
3746c3169a3SBruce Richardson 	if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
3756c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
3766c3169a3SBruce Richardson 		rte_free(vq);
3776c3169a3SBruce Richardson 		return -ENOMEM;
3786c3169a3SBruce Richardson 	}
3796c3169a3SBruce Richardson 
3806c3169a3SBruce Richardson 	memset(mz->addr, 0, sizeof(mz->len));
3816c3169a3SBruce Richardson 	vq->mz = mz;
3826c3169a3SBruce Richardson 	vq->vq_ring_mem = mz->phys_addr;
3836c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
3846c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64, (uint64_t)mz->phys_addr);
3856c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
3866c3169a3SBruce Richardson 	vq->virtio_net_hdr_mz  = NULL;
3876c3169a3SBruce Richardson 	vq->virtio_net_hdr_mem = 0;
3886c3169a3SBruce Richardson 
3896c3169a3SBruce Richardson 	if (queue_type == VTNET_TQ) {
3906c3169a3SBruce Richardson 		/*
3916c3169a3SBruce Richardson 		 * For each xmit packet, allocate a virtio_net_hdr
3926c3169a3SBruce Richardson 		 */
3936c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
3946c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
3956c3169a3SBruce Richardson 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
3966c3169a3SBruce Richardson 			vq_size * hw->vtnet_hdr_size,
3976c3169a3SBruce Richardson 			socket_id, 0, RTE_CACHE_LINE_SIZE);
3986c3169a3SBruce Richardson 		if (vq->virtio_net_hdr_mz == NULL) {
399abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
400abf4c84bSBernard Iremonger 				vq->virtio_net_hdr_mz =
401abf4c84bSBernard Iremonger 					rte_memzone_lookup(vq_name);
402abf4c84bSBernard Iremonger 			if (vq->virtio_net_hdr_mz == NULL) {
4036c3169a3SBruce Richardson 				rte_free(vq);
4046c3169a3SBruce Richardson 				return -ENOMEM;
4056c3169a3SBruce Richardson 			}
406abf4c84bSBernard Iremonger 		}
4076c3169a3SBruce Richardson 		vq->virtio_net_hdr_mem =
4086c3169a3SBruce Richardson 			vq->virtio_net_hdr_mz->phys_addr;
4096c3169a3SBruce Richardson 		memset(vq->virtio_net_hdr_mz->addr, 0,
4106c3169a3SBruce Richardson 			vq_size * hw->vtnet_hdr_size);
4116c3169a3SBruce Richardson 	} else if (queue_type == VTNET_CQ) {
4126c3169a3SBruce Richardson 		/* Allocate a page for control vq command, data and status */
4136c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
4146c3169a3SBruce Richardson 			dev->data->port_id);
4156c3169a3SBruce Richardson 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
4166c3169a3SBruce Richardson 			PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
4176c3169a3SBruce Richardson 		if (vq->virtio_net_hdr_mz == NULL) {
418abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
419abf4c84bSBernard Iremonger 				vq->virtio_net_hdr_mz =
420abf4c84bSBernard Iremonger 					rte_memzone_lookup(vq_name);
421abf4c84bSBernard Iremonger 			if (vq->virtio_net_hdr_mz == NULL) {
4226c3169a3SBruce Richardson 				rte_free(vq);
4236c3169a3SBruce Richardson 				return -ENOMEM;
4246c3169a3SBruce Richardson 			}
425abf4c84bSBernard Iremonger 		}
4266c3169a3SBruce Richardson 		vq->virtio_net_hdr_mem =
4276c3169a3SBruce Richardson 			vq->virtio_net_hdr_mz->phys_addr;
4286c3169a3SBruce Richardson 		memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
4296c3169a3SBruce Richardson 	}
4306c3169a3SBruce Richardson 
431d5bbeefcSYuanhan Liu 	hw->vtpci_ops->setup_queue(hw, vq);
432d5bbeefcSYuanhan Liu 
4336c3169a3SBruce Richardson 	*pvq = vq;
4346c3169a3SBruce Richardson 	return 0;
4356c3169a3SBruce Richardson }
4366c3169a3SBruce Richardson 
4376c3169a3SBruce Richardson static int
4386c3169a3SBruce Richardson virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
4396c3169a3SBruce Richardson 		uint32_t socket_id)
4406c3169a3SBruce Richardson {
4416c3169a3SBruce Richardson 	struct virtqueue *vq;
4426c3169a3SBruce Richardson 	int ret;
4436c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4446c3169a3SBruce Richardson 
4456c3169a3SBruce Richardson 	PMD_INIT_FUNC_TRACE();
4466c3169a3SBruce Richardson 	ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
44727b31d13SStephen Hemminger 			vtpci_queue_idx, 0, socket_id, &vq);
4486c3169a3SBruce Richardson 	if (ret < 0) {
4496c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "control vq initialization failed");
4506c3169a3SBruce Richardson 		return ret;
4516c3169a3SBruce Richardson 	}
4526c3169a3SBruce Richardson 
4536c3169a3SBruce Richardson 	hw->cvq = vq;
4546c3169a3SBruce Richardson 	return 0;
4556c3169a3SBruce Richardson }
4566c3169a3SBruce Richardson 
4576c3169a3SBruce Richardson static void
458941d64b5SBernard Iremonger virtio_free_queues(struct rte_eth_dev *dev)
459941d64b5SBernard Iremonger {
460941d64b5SBernard Iremonger 	unsigned int i;
461941d64b5SBernard Iremonger 
462941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_rx_queues; i++)
463941d64b5SBernard Iremonger 		virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
464941d64b5SBernard Iremonger 
465941d64b5SBernard Iremonger 	dev->data->nb_rx_queues = 0;
466941d64b5SBernard Iremonger 
467941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_tx_queues; i++)
468941d64b5SBernard Iremonger 		virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
469941d64b5SBernard Iremonger 
470941d64b5SBernard Iremonger 	dev->data->nb_tx_queues = 0;
471941d64b5SBernard Iremonger }
472941d64b5SBernard Iremonger 
473941d64b5SBernard Iremonger static void
4746c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
4756c3169a3SBruce Richardson {
4766c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4776c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
4786c3169a3SBruce Richardson 
4796c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
4806c3169a3SBruce Richardson 
481*9a0615afSJianfeng Tan 	if (hw->started == 1)
482*9a0615afSJianfeng Tan 		virtio_dev_stop(dev);
483*9a0615afSJianfeng Tan 
4846c3169a3SBruce Richardson 	/* reset the NIC */
4856c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
4866c3169a3SBruce Richardson 		vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
4876c3169a3SBruce Richardson 	vtpci_reset(hw);
4886c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
489941d64b5SBernard Iremonger 	virtio_free_queues(dev);
4906c3169a3SBruce Richardson }
4916c3169a3SBruce Richardson 
4926c3169a3SBruce Richardson static void
4936c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
4946c3169a3SBruce Richardson {
4956c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4966c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
4976c3169a3SBruce Richardson 	int dlen[1];
4986c3169a3SBruce Richardson 	int ret;
4996c3169a3SBruce Richardson 
500e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
501e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
502e9e414a4SStephen Hemminger 		return;
503e9e414a4SStephen Hemminger 	}
504e9e414a4SStephen Hemminger 
5056c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5066c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
5076c3169a3SBruce Richardson 	ctrl.data[0] = 1;
5086c3169a3SBruce Richardson 	dlen[0] = 1;
5096c3169a3SBruce Richardson 
5106c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5116c3169a3SBruce Richardson 	if (ret)
5126c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
5136c3169a3SBruce Richardson }
5146c3169a3SBruce Richardson 
5156c3169a3SBruce Richardson static void
5166c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
5176c3169a3SBruce Richardson {
5186c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5196c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5206c3169a3SBruce Richardson 	int dlen[1];
5216c3169a3SBruce Richardson 	int ret;
5226c3169a3SBruce Richardson 
523e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
524e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
525e9e414a4SStephen Hemminger 		return;
526e9e414a4SStephen Hemminger 	}
527e9e414a4SStephen Hemminger 
5286c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5296c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
5306c3169a3SBruce Richardson 	ctrl.data[0] = 0;
5316c3169a3SBruce Richardson 	dlen[0] = 1;
5326c3169a3SBruce Richardson 
5336c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5346c3169a3SBruce Richardson 	if (ret)
5356c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
5366c3169a3SBruce Richardson }
5376c3169a3SBruce Richardson 
5386c3169a3SBruce Richardson static void
5396c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
5406c3169a3SBruce Richardson {
5416c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5426c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5436c3169a3SBruce Richardson 	int dlen[1];
5446c3169a3SBruce Richardson 	int ret;
5456c3169a3SBruce Richardson 
546e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
547e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
548e9e414a4SStephen Hemminger 		return;
549e9e414a4SStephen Hemminger 	}
550e9e414a4SStephen Hemminger 
5516c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5526c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
5536c3169a3SBruce Richardson 	ctrl.data[0] = 1;
5546c3169a3SBruce Richardson 	dlen[0] = 1;
5556c3169a3SBruce Richardson 
5566c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5576c3169a3SBruce Richardson 	if (ret)
5586c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
5596c3169a3SBruce Richardson }
5606c3169a3SBruce Richardson 
5616c3169a3SBruce Richardson static void
5626c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
5636c3169a3SBruce Richardson {
5646c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5656c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5666c3169a3SBruce Richardson 	int dlen[1];
5676c3169a3SBruce Richardson 	int ret;
5686c3169a3SBruce Richardson 
569e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
570e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
571e9e414a4SStephen Hemminger 		return;
572e9e414a4SStephen Hemminger 	}
573e9e414a4SStephen Hemminger 
5746c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5756c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
5766c3169a3SBruce Richardson 	ctrl.data[0] = 0;
5776c3169a3SBruce Richardson 	dlen[0] = 1;
5786c3169a3SBruce Richardson 
5796c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5806c3169a3SBruce Richardson 	if (ret)
5816c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
5826c3169a3SBruce Richardson }
5836c3169a3SBruce Richardson 
5846c3169a3SBruce Richardson /*
5856c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
5866c3169a3SBruce Richardson  */
5876c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
5886c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
5896c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
5906c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
5916c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
5926c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
5936c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
5946c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
5956c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
5966c3169a3SBruce Richardson 
5976c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
5986c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
59976d4c652SHarry van Haaren 	.xstats_get              = virtio_dev_xstats_get,
6006c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
60176d4c652SHarry van Haaren 	.xstats_reset            = virtio_dev_stats_reset,
6026c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
6036c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
6046c3169a3SBruce Richardson 	.rx_queue_release        = virtio_dev_rx_queue_release,
6056c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
6066c3169a3SBruce Richardson 	.tx_queue_release        = virtio_dev_tx_queue_release,
6076c3169a3SBruce Richardson 	/* collect stats per queue */
6086c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
6096c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
6106c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
6116c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
6126c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
6136c3169a3SBruce Richardson };
6146c3169a3SBruce Richardson 
6156c3169a3SBruce Richardson static inline int
6166c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
6176c3169a3SBruce Richardson 				struct rte_eth_link *link)
6186c3169a3SBruce Richardson {
6196c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
6206c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
6216c3169a3SBruce Richardson 
6226c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
6236c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
6246c3169a3SBruce Richardson 		return -1;
6256c3169a3SBruce Richardson 
6266c3169a3SBruce Richardson 	return 0;
6276c3169a3SBruce Richardson }
6286c3169a3SBruce Richardson 
6296c3169a3SBruce Richardson /**
6306c3169a3SBruce Richardson  * Atomically writes the link status information into global
6316c3169a3SBruce Richardson  * structure rte_eth_dev.
6326c3169a3SBruce Richardson  *
6336c3169a3SBruce Richardson  * @param dev
6346c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
6356c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
6366c3169a3SBruce Richardson  *
6376c3169a3SBruce Richardson  * @return
6386c3169a3SBruce Richardson  *   - On success, zero.
6396c3169a3SBruce Richardson  *   - On failure, negative value.
6406c3169a3SBruce Richardson  */
6416c3169a3SBruce Richardson static inline int
6426c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
6436c3169a3SBruce Richardson 		struct rte_eth_link *link)
6446c3169a3SBruce Richardson {
6456c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
6466c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
6476c3169a3SBruce Richardson 
6486c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
6496c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
6506c3169a3SBruce Richardson 		return -1;
6516c3169a3SBruce Richardson 
6526c3169a3SBruce Richardson 	return 0;
6536c3169a3SBruce Richardson }
6546c3169a3SBruce Richardson 
6556c3169a3SBruce Richardson static void
65676d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
6576c3169a3SBruce Richardson {
6586c3169a3SBruce Richardson 	unsigned i;
6596c3169a3SBruce Richardson 
6606c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
6616c3169a3SBruce Richardson 		const struct virtqueue *txvq = dev->data->tx_queues[i];
6626c3169a3SBruce Richardson 		if (txvq == NULL)
6636c3169a3SBruce Richardson 			continue;
6646c3169a3SBruce Richardson 
6656c3169a3SBruce Richardson 		stats->opackets += txvq->packets;
6666c3169a3SBruce Richardson 		stats->obytes += txvq->bytes;
6676c3169a3SBruce Richardson 		stats->oerrors += txvq->errors;
6686c3169a3SBruce Richardson 
6696c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
6706c3169a3SBruce Richardson 			stats->q_opackets[i] = txvq->packets;
6716c3169a3SBruce Richardson 			stats->q_obytes[i] = txvq->bytes;
6726c3169a3SBruce Richardson 		}
6736c3169a3SBruce Richardson 	}
6746c3169a3SBruce Richardson 
6756c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
6766c3169a3SBruce Richardson 		const struct virtqueue *rxvq = dev->data->rx_queues[i];
6776c3169a3SBruce Richardson 		if (rxvq == NULL)
6786c3169a3SBruce Richardson 			continue;
6796c3169a3SBruce Richardson 
6806c3169a3SBruce Richardson 		stats->ipackets += rxvq->packets;
6816c3169a3SBruce Richardson 		stats->ibytes += rxvq->bytes;
6826c3169a3SBruce Richardson 		stats->ierrors += rxvq->errors;
6836c3169a3SBruce Richardson 
6846c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
6856c3169a3SBruce Richardson 			stats->q_ipackets[i] = rxvq->packets;
6866c3169a3SBruce Richardson 			stats->q_ibytes[i] = rxvq->bytes;
6876c3169a3SBruce Richardson 		}
6886c3169a3SBruce Richardson 	}
6896c3169a3SBruce Richardson 
6906c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
6916c3169a3SBruce Richardson }
6926c3169a3SBruce Richardson 
69376d4c652SHarry van Haaren static int
69476d4c652SHarry van Haaren virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
69576d4c652SHarry van Haaren 		      unsigned n)
69676d4c652SHarry van Haaren {
69776d4c652SHarry van Haaren 	unsigned i;
69876d4c652SHarry van Haaren 	unsigned count = 0;
69976d4c652SHarry van Haaren 
70076d4c652SHarry van Haaren 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_Q_XSTATS +
70176d4c652SHarry van Haaren 		dev->data->nb_rx_queues * VIRTIO_NB_Q_XSTATS;
70276d4c652SHarry van Haaren 
70376d4c652SHarry van Haaren 	if (n < nstats)
70476d4c652SHarry van Haaren 		return nstats;
70576d4c652SHarry van Haaren 
70676d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
70776d4c652SHarry van Haaren 		struct virtqueue *rxvq = dev->data->rx_queues[i];
70876d4c652SHarry van Haaren 
70976d4c652SHarry van Haaren 		if (rxvq == NULL)
71076d4c652SHarry van Haaren 			continue;
71176d4c652SHarry van Haaren 
71276d4c652SHarry van Haaren 		unsigned t;
71376d4c652SHarry van Haaren 
71476d4c652SHarry van Haaren 		for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
71576d4c652SHarry van Haaren 			snprintf(xstats[count].name, sizeof(xstats[count].name),
71676d4c652SHarry van Haaren 				 "rx_q%u_%s", i,
71776d4c652SHarry van Haaren 				 rte_virtio_q_stat_strings[t].name);
71876d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
71976d4c652SHarry van Haaren 				rte_virtio_q_stat_strings[t].offset);
72076d4c652SHarry van Haaren 			count++;
72176d4c652SHarry van Haaren 		}
72276d4c652SHarry van Haaren 	}
72376d4c652SHarry van Haaren 
72476d4c652SHarry van Haaren 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
72576d4c652SHarry van Haaren 		struct virtqueue *txvq = dev->data->tx_queues[i];
72676d4c652SHarry van Haaren 
72776d4c652SHarry van Haaren 		if (txvq == NULL)
72876d4c652SHarry van Haaren 			continue;
72976d4c652SHarry van Haaren 
73076d4c652SHarry van Haaren 		unsigned t;
73176d4c652SHarry van Haaren 
73276d4c652SHarry van Haaren 		for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
73376d4c652SHarry van Haaren 			snprintf(xstats[count].name, sizeof(xstats[count].name),
73476d4c652SHarry van Haaren 				 "tx_q%u_%s", i,
73576d4c652SHarry van Haaren 				 rte_virtio_q_stat_strings[t].name);
73676d4c652SHarry van Haaren 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
73776d4c652SHarry van Haaren 				rte_virtio_q_stat_strings[t].offset);
73876d4c652SHarry van Haaren 			count++;
73976d4c652SHarry van Haaren 		}
74076d4c652SHarry van Haaren 	}
74176d4c652SHarry van Haaren 
74276d4c652SHarry van Haaren 	return count;
74376d4c652SHarry van Haaren }
74476d4c652SHarry van Haaren 
74576d4c652SHarry van Haaren static void
74676d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
74776d4c652SHarry van Haaren {
74876d4c652SHarry van Haaren 	virtio_update_stats(dev, stats);
74976d4c652SHarry van Haaren }
75076d4c652SHarry van Haaren 
7516c3169a3SBruce Richardson static void
7526c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
7536c3169a3SBruce Richardson {
7546c3169a3SBruce Richardson 	unsigned int i;
7556c3169a3SBruce Richardson 
7566c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
7576c3169a3SBruce Richardson 		struct virtqueue *txvq = dev->data->tx_queues[i];
7586c3169a3SBruce Richardson 		if (txvq == NULL)
7596c3169a3SBruce Richardson 			continue;
7606c3169a3SBruce Richardson 
7616c3169a3SBruce Richardson 		txvq->packets = 0;
7626c3169a3SBruce Richardson 		txvq->bytes = 0;
7636c3169a3SBruce Richardson 		txvq->errors = 0;
76476d4c652SHarry van Haaren 		txvq->multicast = 0;
76576d4c652SHarry van Haaren 		txvq->broadcast = 0;
76676d4c652SHarry van Haaren 		memset(txvq->size_bins, 0, sizeof(txvq->size_bins[0]) * 8);
7676c3169a3SBruce Richardson 	}
7686c3169a3SBruce Richardson 
7696c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
7706c3169a3SBruce Richardson 		struct virtqueue *rxvq = dev->data->rx_queues[i];
7716c3169a3SBruce Richardson 		if (rxvq == NULL)
7726c3169a3SBruce Richardson 			continue;
7736c3169a3SBruce Richardson 
7746c3169a3SBruce Richardson 		rxvq->packets = 0;
7756c3169a3SBruce Richardson 		rxvq->bytes = 0;
7766c3169a3SBruce Richardson 		rxvq->errors = 0;
77776d4c652SHarry van Haaren 		rxvq->multicast = 0;
77876d4c652SHarry van Haaren 		rxvq->broadcast = 0;
77976d4c652SHarry van Haaren 		memset(rxvq->size_bins, 0, sizeof(rxvq->size_bins[0]) * 8);
7806c3169a3SBruce Richardson 	}
7816c3169a3SBruce Richardson }
7826c3169a3SBruce Richardson 
7836c3169a3SBruce Richardson static void
7846c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
7856c3169a3SBruce Richardson {
7866c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
7876c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
7886c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
7896c3169a3SBruce Richardson }
7906c3169a3SBruce Richardson 
7916c3169a3SBruce Richardson static void
7926c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
7936c3169a3SBruce Richardson {
7946c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
7956c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
7966c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
7976c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
7986c3169a3SBruce Richardson 	} else {
7996c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
8006c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
8016c3169a3SBruce Richardson 	}
8026c3169a3SBruce Richardson }
8036c3169a3SBruce Richardson 
80427046236SStephen Hemminger static void
8056c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
8066c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
8076c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
8086c3169a3SBruce Richardson {
8096c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
8106c3169a3SBruce Richardson 	int err, len[2];
8116c3169a3SBruce Richardson 
81227046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
81327046236SStephen Hemminger 		PMD_DRV_LOG(INFO, "host does not support mac table\n");
81427046236SStephen Hemminger 		return;
81527046236SStephen Hemminger 	}
81627046236SStephen Hemminger 
8176c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
8186c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
8196c3169a3SBruce Richardson 
8206c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
8216c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
8226c3169a3SBruce Richardson 
8236c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
8246c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
8256c3169a3SBruce Richardson 
8266c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
8276c3169a3SBruce Richardson 	if (err != 0)
8286c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
8296c3169a3SBruce Richardson }
8306c3169a3SBruce Richardson 
8316c3169a3SBruce Richardson static void
8326c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
8336c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
8346c3169a3SBruce Richardson {
8356c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
8366c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
8376c3169a3SBruce Richardson 	unsigned int i;
8386c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
8396c3169a3SBruce Richardson 
8406c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
8416c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
8426c3169a3SBruce Richardson 		return;
8436c3169a3SBruce Richardson 	}
8446c3169a3SBruce Richardson 
8456c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
8466c3169a3SBruce Richardson 	uc->entries = 0;
8476c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
8486c3169a3SBruce Richardson 	mc->entries = 0;
8496c3169a3SBruce Richardson 
8506c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
8516c3169a3SBruce Richardson 		const struct ether_addr *addr
8526c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
8536c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
8546c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
8556c3169a3SBruce Richardson 
8566c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
8576c3169a3SBruce Richardson 	}
8586c3169a3SBruce Richardson 
8596c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
8606c3169a3SBruce Richardson }
8616c3169a3SBruce Richardson 
8626c3169a3SBruce Richardson static void
8636c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
8646c3169a3SBruce Richardson {
8656c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
8666c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
8676c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
8686c3169a3SBruce Richardson 	unsigned int i;
8696c3169a3SBruce Richardson 
8706c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
8716c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
8726c3169a3SBruce Richardson 		return;
8736c3169a3SBruce Richardson 	}
8746c3169a3SBruce Richardson 
8756c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
8766c3169a3SBruce Richardson 	uc->entries = 0;
8776c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
8786c3169a3SBruce Richardson 	mc->entries = 0;
8796c3169a3SBruce Richardson 
8806c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
8816c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
8826c3169a3SBruce Richardson 
8836c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
8846c3169a3SBruce Richardson 			continue;
8856c3169a3SBruce Richardson 
8866c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
8876c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
8886c3169a3SBruce Richardson 	}
8896c3169a3SBruce Richardson 
8906c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
8916c3169a3SBruce Richardson }
8926c3169a3SBruce Richardson 
8936c3169a3SBruce Richardson static void
8946c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
8956c3169a3SBruce Richardson {
8966c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
8976c3169a3SBruce Richardson 
8986c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
8996c3169a3SBruce Richardson 
9006c3169a3SBruce Richardson 	/* Use atomic update if available */
9016c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
9026c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
9036c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
9046c3169a3SBruce Richardson 
9056c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
9066c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
9076c3169a3SBruce Richardson 
9086c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
9096c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
9106c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
9116c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
9126c3169a3SBruce Richardson }
9136c3169a3SBruce Richardson 
9146c3169a3SBruce Richardson static int
9156c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
9166c3169a3SBruce Richardson {
9176c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
9186c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
9196c3169a3SBruce Richardson 	int len;
9206c3169a3SBruce Richardson 
9216c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
9226c3169a3SBruce Richardson 		return -ENOTSUP;
9236c3169a3SBruce Richardson 
9246c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
9256c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
9266c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
9276c3169a3SBruce Richardson 	len = sizeof(vlan_id);
9286c3169a3SBruce Richardson 
9296c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
9306c3169a3SBruce Richardson }
9316c3169a3SBruce Richardson 
9326ba1f63bSYuanhan Liu static int
9336c3169a3SBruce Richardson virtio_negotiate_features(struct virtio_hw *hw)
9346c3169a3SBruce Richardson {
9353891f233SYuanhan Liu 	uint64_t host_features;
9366c3169a3SBruce Richardson 
9376c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
9384a92b671SStephen Hemminger 	hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
9393891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
9406c3169a3SBruce Richardson 		hw->guest_features);
9416c3169a3SBruce Richardson 
9426c3169a3SBruce Richardson 	/* Read device(host) feature bits */
943d5bbeefcSYuanhan Liu 	host_features = hw->vtpci_ops->get_features(hw);
9443891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
9456c3169a3SBruce Richardson 		host_features);
9466c3169a3SBruce Richardson 
9476c3169a3SBruce Richardson 	/*
9486c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
9496c3169a3SBruce Richardson 	 * guest feature bits.
9506c3169a3SBruce Richardson 	 */
9516c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
9523891f233SYuanhan Liu 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
9536c3169a3SBruce Richardson 		hw->guest_features);
9546ba1f63bSYuanhan Liu 
9556ba1f63bSYuanhan Liu 	if (hw->modern) {
9566ba1f63bSYuanhan Liu 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
9576ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
9586ba1f63bSYuanhan Liu 				"VIRTIO_F_VERSION_1 features is not enabled.");
9596ba1f63bSYuanhan Liu 			return -1;
9606ba1f63bSYuanhan Liu 		}
9616ba1f63bSYuanhan Liu 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
9626ba1f63bSYuanhan Liu 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
9636ba1f63bSYuanhan Liu 			PMD_INIT_LOG(ERR,
9646ba1f63bSYuanhan Liu 				"failed to set FEATURES_OK status!");
9656ba1f63bSYuanhan Liu 			return -1;
9666ba1f63bSYuanhan Liu 		}
9676ba1f63bSYuanhan Liu 	}
9686ba1f63bSYuanhan Liu 
9696ba1f63bSYuanhan Liu 	return 0;
9706c3169a3SBruce Richardson }
9716c3169a3SBruce Richardson 
9726c3169a3SBruce Richardson /*
9736c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
9746c3169a3SBruce Richardson  * if link state changed.
9756c3169a3SBruce Richardson  */
9766c3169a3SBruce Richardson static void
9776c3169a3SBruce Richardson virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
9786c3169a3SBruce Richardson 			 void *param)
9796c3169a3SBruce Richardson {
9806c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
9816c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
9826c3169a3SBruce Richardson 	uint8_t isr;
9836c3169a3SBruce Richardson 
9846c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
9856c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
9866c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
9876c3169a3SBruce Richardson 
9886c3169a3SBruce Richardson 	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
9896c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
9906c3169a3SBruce Richardson 
9916c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
9926c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
9936c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
9946c3169a3SBruce Richardson 						      RTE_ETH_EVENT_INTR_LSC);
9956c3169a3SBruce Richardson 	}
9966c3169a3SBruce Richardson 
9976c3169a3SBruce Richardson }
9986c3169a3SBruce Richardson 
9996c3169a3SBruce Richardson static void
10006c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
10016c3169a3SBruce Richardson {
10026c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
10036c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
10046c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
10056c3169a3SBruce Richardson 	else
10066c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
10076c3169a3SBruce Richardson }
10086c3169a3SBruce Richardson 
10096c3169a3SBruce Richardson /*
10106c3169a3SBruce Richardson  * This function is based on probe() function in virtio_pci.c
10116c3169a3SBruce Richardson  * It returns 0 on success.
10126c3169a3SBruce Richardson  */
10136c3169a3SBruce Richardson static int
10146c3169a3SBruce Richardson eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
10156c3169a3SBruce Richardson {
10166c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
10176c3169a3SBruce Richardson 	struct virtio_net_config *config;
10186c3169a3SBruce Richardson 	struct virtio_net_config local_config;
10196c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev;
1020ac5e1d83SHuawei Xie 	int ret;
10216c3169a3SBruce Richardson 
10226c3169a3SBruce Richardson 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
10236c3169a3SBruce Richardson 
10246c3169a3SBruce Richardson 	eth_dev->dev_ops = &virtio_eth_dev_ops;
10256c3169a3SBruce Richardson 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
10266c3169a3SBruce Richardson 
10276c3169a3SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
10286c3169a3SBruce Richardson 		rx_func_get(eth_dev);
10296c3169a3SBruce Richardson 		return 0;
10306c3169a3SBruce Richardson 	}
10316c3169a3SBruce Richardson 
10326c3169a3SBruce Richardson 	/* Allocate memory for storing MAC addresses */
1033b5b0467cSIvan Boule 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
10346c3169a3SBruce Richardson 	if (eth_dev->data->mac_addrs == NULL) {
10356c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR,
10366c3169a3SBruce Richardson 			"Failed to allocate %d bytes needed to store MAC addresses",
1037b5b0467cSIvan Boule 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
10386c3169a3SBruce Richardson 		return -ENOMEM;
10396c3169a3SBruce Richardson 	}
10406c3169a3SBruce Richardson 
10416c3169a3SBruce Richardson 	pci_dev = eth_dev->pci_dev;
1042eeefe73fSBernard Iremonger 
1043ac5e1d83SHuawei Xie 	ret = vtpci_init(pci_dev, hw);
1044ac5e1d83SHuawei Xie 	if (ret)
1045ac5e1d83SHuawei Xie 		return ret;
10466c3169a3SBruce Richardson 
10476c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
10486c3169a3SBruce Richardson 	vtpci_reset(hw);
10496c3169a3SBruce Richardson 
10506c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
10516c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
10526c3169a3SBruce Richardson 
10536c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
10546c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
10556ba1f63bSYuanhan Liu 	if (virtio_negotiate_features(hw) < 0)
10566ba1f63bSYuanhan Liu 		return -1;
10576c3169a3SBruce Richardson 
1058954ea115SStephen Hemminger 	/* If host does not support status then disable LSC */
1059954ea115SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1060954ea115SStephen Hemminger 		pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1061954ea115SStephen Hemminger 
1062d15339b9SBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1063d15339b9SBernard Iremonger 
10646c3169a3SBruce Richardson 	rx_func_get(eth_dev);
10656c3169a3SBruce Richardson 
10666c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
10676ba1f63bSYuanhan Liu 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
10686ba1f63bSYuanhan Liu 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1))
10696c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
10706c3169a3SBruce Richardson 	else
10716c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
10726c3169a3SBruce Richardson 
10736c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
10746c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
10756c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
10766c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
10776c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
10786c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
10796c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
10806c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
10816c3169a3SBruce Richardson 
10826c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
10836c3169a3SBruce Richardson 		config = &local_config;
10846c3169a3SBruce Richardson 
10856d7740e2SChangchun Ouyang 		vtpci_read_dev_config(hw,
10866d7740e2SChangchun Ouyang 			offsetof(struct virtio_net_config, mac),
10876d7740e2SChangchun Ouyang 			&config->mac, sizeof(config->mac));
10886d7740e2SChangchun Ouyang 
10896c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
10906d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
10916d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, status),
10926d7740e2SChangchun Ouyang 				&config->status, sizeof(config->status));
10936c3169a3SBruce Richardson 		} else {
10946c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
10956c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
10966c3169a3SBruce Richardson 			config->status = 0;
10976c3169a3SBruce Richardson 		}
10986c3169a3SBruce Richardson 
10996c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
11006d7740e2SChangchun Ouyang 			vtpci_read_dev_config(hw,
11016d7740e2SChangchun Ouyang 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
11026d7740e2SChangchun Ouyang 				&config->max_virtqueue_pairs,
11036d7740e2SChangchun Ouyang 				sizeof(config->max_virtqueue_pairs));
11046c3169a3SBruce Richardson 		} else {
11056c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
11066c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
11076c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
11086c3169a3SBruce Richardson 		}
11096c3169a3SBruce Richardson 
11106c3169a3SBruce Richardson 		hw->max_rx_queues =
11116c3169a3SBruce Richardson 			(VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
11126c3169a3SBruce Richardson 			VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
11136c3169a3SBruce Richardson 		hw->max_tx_queues =
11146c3169a3SBruce Richardson 			(VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
11156c3169a3SBruce Richardson 			VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
11166c3169a3SBruce Richardson 
11176c3169a3SBruce Richardson 		virtio_dev_cq_queue_setup(eth_dev,
11186c3169a3SBruce Richardson 					config->max_virtqueue_pairs * 2,
11196c3169a3SBruce Richardson 					SOCKET_ID_ANY);
11206c3169a3SBruce Richardson 
11216c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
11226c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
11236c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
11246c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
11256c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
11266c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
11276c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
11286c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
11296c3169a3SBruce Richardson 	} else {
11306c3169a3SBruce Richardson 		hw->max_rx_queues = 1;
11316c3169a3SBruce Richardson 		hw->max_tx_queues = 1;
11326c3169a3SBruce Richardson 	}
11336c3169a3SBruce Richardson 
11346c3169a3SBruce Richardson 	eth_dev->data->nb_rx_queues = hw->max_rx_queues;
11356c3169a3SBruce Richardson 	eth_dev->data->nb_tx_queues = hw->max_tx_queues;
11366c3169a3SBruce Richardson 
11376c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
11386c3169a3SBruce Richardson 			hw->max_rx_queues, hw->max_tx_queues);
11396c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
11406c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
11416c3169a3SBruce Richardson 			pci_dev->id.device_id);
11426c3169a3SBruce Richardson 
11436c3169a3SBruce Richardson 	/* Setup interrupt callback  */
11446c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
11456c3169a3SBruce Richardson 		rte_intr_callback_register(&pci_dev->intr_handle,
11466c3169a3SBruce Richardson 				   virtio_interrupt_handler, eth_dev);
11476c3169a3SBruce Richardson 
11486c3169a3SBruce Richardson 	virtio_dev_cq_start(eth_dev);
11496c3169a3SBruce Richardson 
11506c3169a3SBruce Richardson 	return 0;
11516c3169a3SBruce Richardson }
11526c3169a3SBruce Richardson 
1153abf4c84bSBernard Iremonger static int
1154abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1155abf4c84bSBernard Iremonger {
1156abf4c84bSBernard Iremonger 	struct rte_pci_device *pci_dev;
1157abf4c84bSBernard Iremonger 	struct virtio_hw *hw = eth_dev->data->dev_private;
1158abf4c84bSBernard Iremonger 
1159abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1160abf4c84bSBernard Iremonger 
1161abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1162abf4c84bSBernard Iremonger 		return -EPERM;
1163abf4c84bSBernard Iremonger 
1164*9a0615afSJianfeng Tan 	/* Close it anyway since there's no way to know if closed */
1165abf4c84bSBernard Iremonger 	virtio_dev_close(eth_dev);
1166*9a0615afSJianfeng Tan 
1167abf4c84bSBernard Iremonger 	pci_dev = eth_dev->pci_dev;
1168abf4c84bSBernard Iremonger 
1169abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1170abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1171abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1172abf4c84bSBernard Iremonger 
11735382b188SBernard Iremonger 	virtio_dev_queue_release(hw->cvq);
1174abf4c84bSBernard Iremonger 
1175abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1176abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1177abf4c84bSBernard Iremonger 
1178abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
1179abf4c84bSBernard Iremonger 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1180abf4c84bSBernard Iremonger 		rte_intr_callback_unregister(&pci_dev->intr_handle,
1181abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1182abf4c84bSBernard Iremonger 						eth_dev);
11836ba1f63bSYuanhan Liu 	rte_eal_pci_unmap_device(pci_dev);
1184abf4c84bSBernard Iremonger 
1185abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1186abf4c84bSBernard Iremonger 
1187abf4c84bSBernard Iremonger 	return 0;
1188abf4c84bSBernard Iremonger }
1189abf4c84bSBernard Iremonger 
11906c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
11916c52c126SStephen Hemminger 	.pci_drv = {
11926c3169a3SBruce Richardson 		.name = "rte_virtio_pmd",
11936c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
1194abf4c84bSBernard Iremonger 		.drv_flags = RTE_PCI_DRV_DETACHABLE,
11956c3169a3SBruce Richardson 	},
11966c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1197abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
11986c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
11996c3169a3SBruce Richardson };
12006c3169a3SBruce Richardson 
12016c3169a3SBruce Richardson /*
12026c3169a3SBruce Richardson  * Driver initialization routine.
12036c3169a3SBruce Richardson  * Invoked once at EAL init time.
12046c3169a3SBruce Richardson  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
12056c3169a3SBruce Richardson  * Returns 0 on success.
12066c3169a3SBruce Richardson  */
12076c3169a3SBruce Richardson static int
12086c3169a3SBruce Richardson rte_virtio_pmd_init(const char *name __rte_unused,
12096c3169a3SBruce Richardson 		    const char *param __rte_unused)
12106c3169a3SBruce Richardson {
12116c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
12126c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
12136c3169a3SBruce Richardson 		return -1;
12146c3169a3SBruce Richardson 	}
12156c3169a3SBruce Richardson 
12166c3169a3SBruce Richardson 	rte_eth_driver_register(&rte_virtio_pmd);
12176c3169a3SBruce Richardson 	return 0;
12186c3169a3SBruce Richardson }
12196c3169a3SBruce Richardson 
12206c3169a3SBruce Richardson /*
12216c3169a3SBruce Richardson  * Configure virtio device
12226c3169a3SBruce Richardson  * It returns 0 on success.
12236c3169a3SBruce Richardson  */
12246c3169a3SBruce Richardson static int
12256c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
12266c3169a3SBruce Richardson {
12276c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
12286c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
12296c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
12306c3169a3SBruce Richardson 
12316c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
12326c3169a3SBruce Richardson 
12336c3169a3SBruce Richardson 	if (rxmode->hw_ip_checksum) {
12346c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1235693f715dSHuawei Xie 		return -EINVAL;
12366c3169a3SBruce Richardson 	}
12376c3169a3SBruce Richardson 
12386c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
12396c3169a3SBruce Richardson 
12406c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
12416c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
12426c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
12436c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
12446c3169a3SBruce Richardson 		return -ENOTSUP;
12456c3169a3SBruce Richardson 	}
12466c3169a3SBruce Richardson 
12476c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
12486c3169a3SBruce Richardson 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
12496c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
12506c3169a3SBruce Richardson 			return -EBUSY;
12516c3169a3SBruce Richardson 		}
12526c3169a3SBruce Richardson 
12536c3169a3SBruce Richardson 	return 0;
12546c3169a3SBruce Richardson }
12556c3169a3SBruce Richardson 
12566c3169a3SBruce Richardson 
12576c3169a3SBruce Richardson static int
12586c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
12596c3169a3SBruce Richardson {
12606c3169a3SBruce Richardson 	uint16_t nb_queues, i;
12616c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
12626c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
12636c3169a3SBruce Richardson 
12646c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
1265954ea115SStephen Hemminger 	if (dev->data->dev_conf.intr_conf.lsc) {
1266954ea115SStephen Hemminger 		if (!(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
12676c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
12686c3169a3SBruce Richardson 			return -ENOTSUP;
12696c3169a3SBruce Richardson 		}
12706c3169a3SBruce Richardson 
12716c3169a3SBruce Richardson 		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
12726c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
12736c3169a3SBruce Richardson 			return -EIO;
12746c3169a3SBruce Richardson 		}
12756c3169a3SBruce Richardson 	}
12766c3169a3SBruce Richardson 
12776c3169a3SBruce Richardson 	/* Initialize Link state */
12786c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
12796c3169a3SBruce Richardson 
12806c3169a3SBruce Richardson 	/* On restart after stop do not touch queues */
12816c3169a3SBruce Richardson 	if (hw->started)
12826c3169a3SBruce Richardson 		return 0;
12836c3169a3SBruce Richardson 
12846c3169a3SBruce Richardson 	/* Do final configuration before rx/tx engine starts */
12856c3169a3SBruce Richardson 	virtio_dev_rxtx_start(dev);
12866c3169a3SBruce Richardson 	vtpci_reinit_complete(hw);
12876c3169a3SBruce Richardson 
12886c3169a3SBruce Richardson 	hw->started = 1;
12896c3169a3SBruce Richardson 
12906c3169a3SBruce Richardson 	/*Notify the backend
12916c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
12926c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
12936c3169a3SBruce Richardson 	 */
12946c3169a3SBruce Richardson 	nb_queues = dev->data->nb_rx_queues;
12956c3169a3SBruce Richardson 	if (nb_queues > 1) {
12966c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
12976c3169a3SBruce Richardson 			return -EINVAL;
12986c3169a3SBruce Richardson 	}
12996c3169a3SBruce Richardson 
13006c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
13016c3169a3SBruce Richardson 
13026c3169a3SBruce Richardson 	for (i = 0; i < nb_queues; i++)
13036c3169a3SBruce Richardson 		virtqueue_notify(dev->data->rx_queues[i]);
13046c3169a3SBruce Richardson 
13056c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
13066c3169a3SBruce Richardson 
13076c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++)
13086c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
13096c3169a3SBruce Richardson 
13106c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++)
13116c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
13126c3169a3SBruce Richardson 
13136c3169a3SBruce Richardson 	return 0;
13146c3169a3SBruce Richardson }
13156c3169a3SBruce Richardson 
13166c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
13176c3169a3SBruce Richardson {
13186c3169a3SBruce Richardson 	struct rte_mbuf *buf;
13196c3169a3SBruce Richardson 	int i, mbuf_num = 0;
13206c3169a3SBruce Richardson 
13216c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
13226c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
13236c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
13246c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
13256c3169a3SBruce Richardson 
1326abf4c84bSBernard Iremonger 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p",
1327abf4c84bSBernard Iremonger 				i, dev->data->rx_queues[i]);
13286c3169a3SBruce Richardson 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
13296c3169a3SBruce Richardson 					dev->data->rx_queues[i])) != NULL) {
13306c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
13316c3169a3SBruce Richardson 			mbuf_num++;
13326c3169a3SBruce Richardson 		}
13336c3169a3SBruce Richardson 
13346c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
13356c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
13366c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
13376c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
13386c3169a3SBruce Richardson 	}
13396c3169a3SBruce Richardson 
13406c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
13416c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
13426c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
13436c3169a3SBruce Richardson 			     i);
13446c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
13456c3169a3SBruce Richardson 
13466c3169a3SBruce Richardson 		mbuf_num = 0;
13476c3169a3SBruce Richardson 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
13486c3169a3SBruce Richardson 					dev->data->tx_queues[i])) != NULL) {
13496c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
13506c3169a3SBruce Richardson 
13516c3169a3SBruce Richardson 			mbuf_num++;
13526c3169a3SBruce Richardson 		}
13536c3169a3SBruce Richardson 
13546c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
13556c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
13566c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
13576c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
13586c3169a3SBruce Richardson 	}
13596c3169a3SBruce Richardson }
13606c3169a3SBruce Richardson 
13616c3169a3SBruce Richardson /*
13626c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
13636c3169a3SBruce Richardson  */
13646c3169a3SBruce Richardson static void
13656c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
13666c3169a3SBruce Richardson {
13676c3169a3SBruce Richardson 	struct rte_eth_link link;
1368*9a0615afSJianfeng Tan 	struct virtio_hw *hw = dev->data->dev_private;
13696c3169a3SBruce Richardson 
13706c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
13716c3169a3SBruce Richardson 
1372*9a0615afSJianfeng Tan 	hw->started = 0;
1373*9a0615afSJianfeng Tan 
13746c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
13756c3169a3SBruce Richardson 		rte_intr_disable(&dev->pci_dev->intr_handle);
13766c3169a3SBruce Richardson 
13776c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
13786c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
13796c3169a3SBruce Richardson }
13806c3169a3SBruce Richardson 
13816c3169a3SBruce Richardson static int
13826c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
13836c3169a3SBruce Richardson {
13846c3169a3SBruce Richardson 	struct rte_eth_link link, old;
13856c3169a3SBruce Richardson 	uint16_t status;
13866c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
13876c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
13886c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
13896c3169a3SBruce Richardson 	old = link;
13906c3169a3SBruce Richardson 	link.link_duplex = FULL_DUPLEX;
13916c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
13926c3169a3SBruce Richardson 
13936c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
13946c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
13956c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
13966c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
13976c3169a3SBruce Richardson 				&status, sizeof(status));
13986c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
13996c3169a3SBruce Richardson 			link.link_status = 0;
14006c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
14016c3169a3SBruce Richardson 				     dev->data->port_id);
14026c3169a3SBruce Richardson 		} else {
14036c3169a3SBruce Richardson 			link.link_status = 1;
14046c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
14056c3169a3SBruce Richardson 				     dev->data->port_id);
14066c3169a3SBruce Richardson 		}
14076c3169a3SBruce Richardson 	} else {
14086c3169a3SBruce Richardson 		link.link_status = 1;   /* Link up */
14096c3169a3SBruce Richardson 	}
14106c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
14116c3169a3SBruce Richardson 
14126c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
14136c3169a3SBruce Richardson }
14146c3169a3SBruce Richardson 
14156c3169a3SBruce Richardson static void
14166c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
14176c3169a3SBruce Richardson {
14186c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
14196c3169a3SBruce Richardson 
14206c3169a3SBruce Richardson 	dev_info->driver_name = dev->driver->pci_drv.name;
14216c3169a3SBruce Richardson 	dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
14226c3169a3SBruce Richardson 	dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
14236c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
14246c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
14256c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
14266c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
14276c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
14286c3169a3SBruce Richardson 	};
14296c3169a3SBruce Richardson }
14306c3169a3SBruce Richardson 
14316c3169a3SBruce Richardson /*
14326c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
14336c3169a3SBruce Richardson  */
14346c3169a3SBruce Richardson static int
14356c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
14366c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
14376c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
14386c3169a3SBruce Richardson {
14396c3169a3SBruce Richardson 	return 0;
14406c3169a3SBruce Richardson }
14416c3169a3SBruce Richardson 
14426c3169a3SBruce Richardson static struct rte_driver rte_virtio_driver = {
14436c3169a3SBruce Richardson 	.type = PMD_PDEV,
14446c3169a3SBruce Richardson 	.init = rte_virtio_pmd_init,
14456c3169a3SBruce Richardson };
14466c3169a3SBruce Richardson 
14476c3169a3SBruce Richardson PMD_REGISTER_DRIVER(rte_virtio_driver);
1448