xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision ce8e1218)
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 #ifdef RTE_EXEC_ENV_LINUXAPP
406c3169a3SBruce Richardson #include <dirent.h>
416c3169a3SBruce Richardson #include <fcntl.h>
426c3169a3SBruce Richardson #endif
436c3169a3SBruce Richardson 
446c3169a3SBruce Richardson #include <rte_ethdev.h>
456c3169a3SBruce Richardson #include <rte_memcpy.h>
466c3169a3SBruce Richardson #include <rte_string_fns.h>
476c3169a3SBruce Richardson #include <rte_memzone.h>
486c3169a3SBruce Richardson #include <rte_malloc.h>
496c3169a3SBruce Richardson #include <rte_atomic.h>
506c3169a3SBruce Richardson #include <rte_branch_prediction.h>
516c3169a3SBruce Richardson #include <rte_pci.h>
526c3169a3SBruce Richardson #include <rte_ether.h>
536c3169a3SBruce Richardson #include <rte_common.h>
54abf4c84bSBernard Iremonger #include <rte_errno.h>
556c3169a3SBruce Richardson 
566c3169a3SBruce Richardson #include <rte_memory.h>
576c3169a3SBruce Richardson #include <rte_eal.h>
586c3169a3SBruce Richardson #include <rte_dev.h>
596c3169a3SBruce Richardson 
606c3169a3SBruce Richardson #include "virtio_ethdev.h"
616c3169a3SBruce Richardson #include "virtio_pci.h"
626c3169a3SBruce Richardson #include "virtio_logs.h"
636c3169a3SBruce Richardson #include "virtqueue.h"
646c3169a3SBruce Richardson 
656c3169a3SBruce Richardson 
666c3169a3SBruce Richardson static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
67abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
686c3169a3SBruce Richardson static int  virtio_dev_configure(struct rte_eth_dev *dev);
696c3169a3SBruce Richardson static int  virtio_dev_start(struct rte_eth_dev *dev);
706c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev);
716c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
726c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
736c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
746c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
756c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev,
766c3169a3SBruce Richardson 				struct rte_eth_dev_info *dev_info);
776c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev,
786c3169a3SBruce Richardson 	__rte_unused int wait_to_complete);
796c3169a3SBruce Richardson 
806c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw);
816c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw);
826c3169a3SBruce Richardson 
836c3169a3SBruce Richardson static void virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
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 
1126c3169a3SBruce Richardson static int
1136c3169a3SBruce Richardson virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
1146c3169a3SBruce Richardson 		int *dlen, int pkt_num)
1156c3169a3SBruce Richardson {
1169e71668bSDamjan Marion 	uint32_t head, i;
1176c3169a3SBruce Richardson 	int k, sum = 0;
1186c3169a3SBruce Richardson 	virtio_net_ctrl_ack status = ~0;
1196c3169a3SBruce Richardson 	struct virtio_pmd_ctrl result;
1206c3169a3SBruce Richardson 
1216c3169a3SBruce Richardson 	ctrl->status = status;
1226c3169a3SBruce Richardson 
1239e71668bSDamjan Marion 	if (!(vq && vq->hw->cvq)) {
1246c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR,
1256c3169a3SBruce Richardson 			     "%s(): Control queue is not supported.",
1266c3169a3SBruce Richardson 			     __func__);
1276c3169a3SBruce Richardson 		return -1;
1286c3169a3SBruce Richardson 	}
1299e71668bSDamjan Marion 	head = vq->vq_desc_head_idx;
1306c3169a3SBruce Richardson 
1316c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
1326c3169a3SBruce Richardson 		"vq->hw->cvq = %p vq = %p",
1336c3169a3SBruce Richardson 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
1346c3169a3SBruce Richardson 
1356c3169a3SBruce Richardson 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
1366c3169a3SBruce Richardson 		return -1;
1376c3169a3SBruce Richardson 
1386c3169a3SBruce Richardson 	memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
1396c3169a3SBruce Richardson 		sizeof(struct virtio_pmd_ctrl));
1406c3169a3SBruce Richardson 
1416c3169a3SBruce Richardson 	/*
1426c3169a3SBruce Richardson 	 * Format is enforced in qemu code:
1436c3169a3SBruce Richardson 	 * One TX packet for header;
1446c3169a3SBruce Richardson 	 * At least one TX packet per argument;
1456c3169a3SBruce Richardson 	 * One RX packet for ACK.
1466c3169a3SBruce Richardson 	 */
1476c3169a3SBruce Richardson 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
1486c3169a3SBruce Richardson 	vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
1496c3169a3SBruce Richardson 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
1506c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1516c3169a3SBruce Richardson 	i = vq->vq_ring.desc[head].next;
1526c3169a3SBruce Richardson 
1536c3169a3SBruce Richardson 	for (k = 0; k < pkt_num; k++) {
1546c3169a3SBruce Richardson 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
1556c3169a3SBruce Richardson 		vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
1566c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr)
1576c3169a3SBruce Richardson 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
1586c3169a3SBruce Richardson 		vq->vq_ring.desc[i].len = dlen[k];
1596c3169a3SBruce Richardson 		sum += dlen[k];
1606c3169a3SBruce Richardson 		vq->vq_free_cnt--;
1616c3169a3SBruce Richardson 		i = vq->vq_ring.desc[i].next;
1626c3169a3SBruce Richardson 	}
1636c3169a3SBruce Richardson 
1646c3169a3SBruce Richardson 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
1656c3169a3SBruce Richardson 	vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
1666c3169a3SBruce Richardson 			+ sizeof(struct virtio_net_ctrl_hdr);
1676c3169a3SBruce Richardson 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
1686c3169a3SBruce Richardson 	vq->vq_free_cnt--;
1696c3169a3SBruce Richardson 
1706c3169a3SBruce Richardson 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
1716c3169a3SBruce Richardson 
1726c3169a3SBruce Richardson 	vq_update_avail_ring(vq, head);
1736c3169a3SBruce Richardson 	vq_update_avail_idx(vq);
1746c3169a3SBruce Richardson 
1756c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
1766c3169a3SBruce Richardson 
1776c3169a3SBruce Richardson 	virtqueue_notify(vq);
1786c3169a3SBruce Richardson 
1796c3169a3SBruce Richardson 	rte_rmb();
1806c3169a3SBruce Richardson 	while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
1816c3169a3SBruce Richardson 		rte_rmb();
1826c3169a3SBruce Richardson 		usleep(100);
1836c3169a3SBruce Richardson 	}
1846c3169a3SBruce Richardson 
1856c3169a3SBruce Richardson 	while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
1866c3169a3SBruce Richardson 		uint32_t idx, desc_idx, used_idx;
1876c3169a3SBruce Richardson 		struct vring_used_elem *uep;
1886c3169a3SBruce Richardson 
1896c3169a3SBruce Richardson 		used_idx = (uint32_t)(vq->vq_used_cons_idx
1906c3169a3SBruce Richardson 				& (vq->vq_nentries - 1));
1916c3169a3SBruce Richardson 		uep = &vq->vq_ring.used->ring[used_idx];
1926c3169a3SBruce Richardson 		idx = (uint32_t) uep->id;
1936c3169a3SBruce Richardson 		desc_idx = idx;
1946c3169a3SBruce Richardson 
1956c3169a3SBruce Richardson 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
1966c3169a3SBruce Richardson 			desc_idx = vq->vq_ring.desc[desc_idx].next;
1976c3169a3SBruce Richardson 			vq->vq_free_cnt++;
1986c3169a3SBruce Richardson 		}
1996c3169a3SBruce Richardson 
2006c3169a3SBruce Richardson 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
2016c3169a3SBruce Richardson 		vq->vq_desc_head_idx = idx;
2026c3169a3SBruce Richardson 
2036c3169a3SBruce Richardson 		vq->vq_used_cons_idx++;
2046c3169a3SBruce Richardson 		vq->vq_free_cnt++;
2056c3169a3SBruce Richardson 	}
2066c3169a3SBruce Richardson 
2076c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
2086c3169a3SBruce Richardson 			vq->vq_free_cnt, vq->vq_desc_head_idx);
2096c3169a3SBruce Richardson 
2106c3169a3SBruce Richardson 	memcpy(&result, vq->virtio_net_hdr_mz->addr,
2116c3169a3SBruce Richardson 			sizeof(struct virtio_pmd_ctrl));
2126c3169a3SBruce Richardson 
2136c3169a3SBruce Richardson 	return result.status;
2146c3169a3SBruce Richardson }
2156c3169a3SBruce Richardson 
2166c3169a3SBruce Richardson static int
2176c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
2186c3169a3SBruce Richardson {
2196c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2206c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
2216c3169a3SBruce Richardson 	int dlen[1];
2226c3169a3SBruce Richardson 	int ret;
2236c3169a3SBruce Richardson 
2246c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
2256c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
2266c3169a3SBruce Richardson 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
2276c3169a3SBruce Richardson 
2286c3169a3SBruce Richardson 	dlen[0] = sizeof(uint16_t);
2296c3169a3SBruce Richardson 
2306c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
2316c3169a3SBruce Richardson 	if (ret) {
2326c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
2336c3169a3SBruce Richardson 			  "failed, this is too late now...");
2346c3169a3SBruce Richardson 		return -EINVAL;
2356c3169a3SBruce Richardson 	}
2366c3169a3SBruce Richardson 
2376c3169a3SBruce Richardson 	return 0;
2386c3169a3SBruce Richardson }
2396c3169a3SBruce Richardson 
2405382b188SBernard Iremonger void
2415382b188SBernard Iremonger virtio_dev_queue_release(struct virtqueue *vq) {
242*ce8e1218SBernard Iremonger 	struct virtio_hw *hw;
2435382b188SBernard Iremonger 
2445382b188SBernard Iremonger 	if (vq) {
245*ce8e1218SBernard Iremonger 		hw = vq->hw;
2465382b188SBernard Iremonger 		/* Select and deactivate the queue */
2475382b188SBernard Iremonger 		VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vq->queue_id);
2485382b188SBernard Iremonger 		VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN, 0);
2495382b188SBernard Iremonger 
2505382b188SBernard Iremonger 		rte_free(vq);
2515382b188SBernard Iremonger 		vq = NULL;
2525382b188SBernard Iremonger 	}
2535382b188SBernard Iremonger }
2545382b188SBernard Iremonger 
2556c3169a3SBruce Richardson int virtio_dev_queue_setup(struct rte_eth_dev *dev,
2566c3169a3SBruce Richardson 			int queue_type,
2576c3169a3SBruce Richardson 			uint16_t queue_idx,
2586c3169a3SBruce Richardson 			uint16_t vtpci_queue_idx,
2596c3169a3SBruce Richardson 			uint16_t nb_desc,
2606c3169a3SBruce Richardson 			unsigned int socket_id,
2616c3169a3SBruce Richardson 			struct virtqueue **pvq)
2626c3169a3SBruce Richardson {
2636c3169a3SBruce Richardson 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
2646c3169a3SBruce Richardson 	const struct rte_memzone *mz;
2656c3169a3SBruce Richardson 	uint16_t vq_size;
2666c3169a3SBruce Richardson 	int size;
2676c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
2686c3169a3SBruce Richardson 	struct virtqueue *vq = NULL;
2696c3169a3SBruce Richardson 
2706c3169a3SBruce Richardson 	/* Write the virtqueue index to the Queue Select Field */
2716c3169a3SBruce Richardson 	VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
2726c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
2736c3169a3SBruce Richardson 
2746c3169a3SBruce Richardson 	/*
2756c3169a3SBruce Richardson 	 * Read the virtqueue size from the Queue Size field
2766c3169a3SBruce Richardson 	 * Always power of 2 and if 0 virtqueue does not exist
2776c3169a3SBruce Richardson 	 */
2786c3169a3SBruce Richardson 	vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
2796c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
2806c3169a3SBruce Richardson 	if (vq_size == 0) {
2816c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
2826c3169a3SBruce Richardson 		return -EINVAL;
283d78deadaSStephen Hemminger 	}
284d78deadaSStephen Hemminger 
285d78deadaSStephen Hemminger 	if (!rte_is_power_of_2(vq_size)) {
2866c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
2876c3169a3SBruce Richardson 		return -EINVAL;
288d78deadaSStephen Hemminger 	}
289d78deadaSStephen Hemminger 
2906c3169a3SBruce Richardson 	if (queue_type == VTNET_RQ) {
2916c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
2926c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
2936c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
2946c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
2956c3169a3SBruce Richardson 	} else if (queue_type == VTNET_TQ) {
2966c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
2976c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
2986c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
2996c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
3006c3169a3SBruce Richardson 	} else if (queue_type == VTNET_CQ) {
3016c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
3026c3169a3SBruce Richardson 			dev->data->port_id);
3036c3169a3SBruce Richardson 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
3046c3169a3SBruce Richardson 			vq_size * sizeof(struct vq_desc_extra),
3056c3169a3SBruce Richardson 			RTE_CACHE_LINE_SIZE);
3066c3169a3SBruce Richardson 	}
3076c3169a3SBruce Richardson 	if (vq == NULL) {
3086c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
3096c3169a3SBruce Richardson 		return (-ENOMEM);
3106c3169a3SBruce Richardson 	}
3116c3169a3SBruce Richardson 
3126c3169a3SBruce Richardson 	vq->hw = hw;
3136c3169a3SBruce Richardson 	vq->port_id = dev->data->port_id;
3146c3169a3SBruce Richardson 	vq->queue_id = queue_idx;
3156c3169a3SBruce Richardson 	vq->vq_queue_index = vtpci_queue_idx;
3166c3169a3SBruce Richardson 	vq->vq_nentries = vq_size;
317945884f1SStephen Hemminger 
318945884f1SStephen Hemminger 	if (nb_desc == 0 || nb_desc > vq_size)
319945884f1SStephen Hemminger 		nb_desc = vq_size;
320945884f1SStephen Hemminger 	vq->vq_free_cnt = nb_desc;
3216c3169a3SBruce Richardson 
3226c3169a3SBruce Richardson 	/*
3236c3169a3SBruce Richardson 	 * Reserve a memzone for vring elements
3246c3169a3SBruce Richardson 	 */
3256c3169a3SBruce Richardson 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
3266c3169a3SBruce Richardson 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
3276c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
3286c3169a3SBruce Richardson 
3296c3169a3SBruce Richardson 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
3306c3169a3SBruce Richardson 		socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
3316c3169a3SBruce Richardson 	if (mz == NULL) {
332abf4c84bSBernard Iremonger 		if (rte_errno == EEXIST)
333abf4c84bSBernard Iremonger 			mz = rte_memzone_lookup(vq_name);
334abf4c84bSBernard Iremonger 		if (mz == NULL) {
3356c3169a3SBruce Richardson 			rte_free(vq);
3366c3169a3SBruce Richardson 			return -ENOMEM;
3376c3169a3SBruce Richardson 		}
338abf4c84bSBernard Iremonger 	}
3396c3169a3SBruce Richardson 
3406c3169a3SBruce Richardson 	/*
3416c3169a3SBruce Richardson 	 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
3426c3169a3SBruce Richardson 	 * and only accepts 32 bit page frame number.
3436c3169a3SBruce Richardson 	 * Check if the allocated physical memory exceeds 16TB.
3446c3169a3SBruce Richardson 	 */
3456c3169a3SBruce Richardson 	if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
3466c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
3476c3169a3SBruce Richardson 		rte_free(vq);
3486c3169a3SBruce Richardson 		return -ENOMEM;
3496c3169a3SBruce Richardson 	}
3506c3169a3SBruce Richardson 
3516c3169a3SBruce Richardson 	memset(mz->addr, 0, sizeof(mz->len));
3526c3169a3SBruce Richardson 	vq->mz = mz;
3536c3169a3SBruce Richardson 	vq->vq_ring_mem = mz->phys_addr;
3546c3169a3SBruce Richardson 	vq->vq_ring_virt_mem = mz->addr;
3556c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64, (uint64_t)mz->phys_addr);
3566c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
3576c3169a3SBruce Richardson 	vq->virtio_net_hdr_mz  = NULL;
3586c3169a3SBruce Richardson 	vq->virtio_net_hdr_mem = 0;
3596c3169a3SBruce Richardson 
3606c3169a3SBruce Richardson 	if (queue_type == VTNET_TQ) {
3616c3169a3SBruce Richardson 		/*
3626c3169a3SBruce Richardson 		 * For each xmit packet, allocate a virtio_net_hdr
3636c3169a3SBruce Richardson 		 */
3646c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
3656c3169a3SBruce Richardson 			dev->data->port_id, queue_idx);
3666c3169a3SBruce Richardson 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
3676c3169a3SBruce Richardson 			vq_size * hw->vtnet_hdr_size,
3686c3169a3SBruce Richardson 			socket_id, 0, RTE_CACHE_LINE_SIZE);
3696c3169a3SBruce Richardson 		if (vq->virtio_net_hdr_mz == NULL) {
370abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
371abf4c84bSBernard Iremonger 				vq->virtio_net_hdr_mz =
372abf4c84bSBernard Iremonger 					rte_memzone_lookup(vq_name);
373abf4c84bSBernard Iremonger 			if (vq->virtio_net_hdr_mz == NULL) {
3746c3169a3SBruce Richardson 				rte_free(vq);
3756c3169a3SBruce Richardson 				return -ENOMEM;
3766c3169a3SBruce Richardson 			}
377abf4c84bSBernard Iremonger 		}
3786c3169a3SBruce Richardson 		vq->virtio_net_hdr_mem =
3796c3169a3SBruce Richardson 			vq->virtio_net_hdr_mz->phys_addr;
3806c3169a3SBruce Richardson 		memset(vq->virtio_net_hdr_mz->addr, 0,
3816c3169a3SBruce Richardson 			vq_size * hw->vtnet_hdr_size);
3826c3169a3SBruce Richardson 	} else if (queue_type == VTNET_CQ) {
3836c3169a3SBruce Richardson 		/* Allocate a page for control vq command, data and status */
3846c3169a3SBruce Richardson 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
3856c3169a3SBruce Richardson 			dev->data->port_id);
3866c3169a3SBruce Richardson 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
3876c3169a3SBruce Richardson 			PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
3886c3169a3SBruce Richardson 		if (vq->virtio_net_hdr_mz == NULL) {
389abf4c84bSBernard Iremonger 			if (rte_errno == EEXIST)
390abf4c84bSBernard Iremonger 				vq->virtio_net_hdr_mz =
391abf4c84bSBernard Iremonger 					rte_memzone_lookup(vq_name);
392abf4c84bSBernard Iremonger 			if (vq->virtio_net_hdr_mz == NULL) {
3936c3169a3SBruce Richardson 				rte_free(vq);
3946c3169a3SBruce Richardson 				return -ENOMEM;
3956c3169a3SBruce Richardson 			}
396abf4c84bSBernard Iremonger 		}
3976c3169a3SBruce Richardson 		vq->virtio_net_hdr_mem =
3986c3169a3SBruce Richardson 			vq->virtio_net_hdr_mz->phys_addr;
3996c3169a3SBruce Richardson 		memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
4006c3169a3SBruce Richardson 	}
4016c3169a3SBruce Richardson 
4026c3169a3SBruce Richardson 	/*
4036c3169a3SBruce Richardson 	 * Set guest physical address of the virtqueue
4046c3169a3SBruce Richardson 	 * in VIRTIO_PCI_QUEUE_PFN config register of device
4056c3169a3SBruce Richardson 	 */
4066c3169a3SBruce Richardson 	VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
4076c3169a3SBruce Richardson 			mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
4086c3169a3SBruce Richardson 	*pvq = vq;
4096c3169a3SBruce Richardson 	return 0;
4106c3169a3SBruce Richardson }
4116c3169a3SBruce Richardson 
4126c3169a3SBruce Richardson static int
4136c3169a3SBruce Richardson virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
4146c3169a3SBruce Richardson 		uint32_t socket_id)
4156c3169a3SBruce Richardson {
4166c3169a3SBruce Richardson 	struct virtqueue *vq;
4176c3169a3SBruce Richardson 	int ret;
4186c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4196c3169a3SBruce Richardson 
4206c3169a3SBruce Richardson 	PMD_INIT_FUNC_TRACE();
4216c3169a3SBruce Richardson 	ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
42227b31d13SStephen Hemminger 			vtpci_queue_idx, 0, socket_id, &vq);
4236c3169a3SBruce Richardson 	if (ret < 0) {
4246c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "control vq initialization failed");
4256c3169a3SBruce Richardson 		return ret;
4266c3169a3SBruce Richardson 	}
4276c3169a3SBruce Richardson 
4286c3169a3SBruce Richardson 	hw->cvq = vq;
4296c3169a3SBruce Richardson 	return 0;
4306c3169a3SBruce Richardson }
4316c3169a3SBruce Richardson 
4326c3169a3SBruce Richardson static void
433941d64b5SBernard Iremonger virtio_free_queues(struct rte_eth_dev *dev)
434941d64b5SBernard Iremonger {
435941d64b5SBernard Iremonger 	unsigned int i;
436941d64b5SBernard Iremonger 
437941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_rx_queues; i++)
438941d64b5SBernard Iremonger 		virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
439941d64b5SBernard Iremonger 
440941d64b5SBernard Iremonger 	dev->data->nb_rx_queues = 0;
441941d64b5SBernard Iremonger 
442941d64b5SBernard Iremonger 	for (i = 0; i < dev->data->nb_tx_queues; i++)
443941d64b5SBernard Iremonger 		virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
444941d64b5SBernard Iremonger 
445941d64b5SBernard Iremonger 	dev->data->nb_tx_queues = 0;
446941d64b5SBernard Iremonger }
447941d64b5SBernard Iremonger 
448941d64b5SBernard Iremonger static void
4496c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev)
4506c3169a3SBruce Richardson {
4516c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4526c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
4536c3169a3SBruce Richardson 
4546c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
4556c3169a3SBruce Richardson 
4566c3169a3SBruce Richardson 	/* reset the NIC */
4576c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
4586c3169a3SBruce Richardson 		vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
4596c3169a3SBruce Richardson 	vtpci_reset(hw);
4606c3169a3SBruce Richardson 	hw->started = 0;
4616c3169a3SBruce Richardson 	virtio_dev_free_mbufs(dev);
462941d64b5SBernard Iremonger 	virtio_free_queues(dev);
4636c3169a3SBruce Richardson }
4646c3169a3SBruce Richardson 
4656c3169a3SBruce Richardson static void
4666c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
4676c3169a3SBruce Richardson {
4686c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4696c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
4706c3169a3SBruce Richardson 	int dlen[1];
4716c3169a3SBruce Richardson 	int ret;
4726c3169a3SBruce Richardson 
473e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
474e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
475e9e414a4SStephen Hemminger 		return;
476e9e414a4SStephen Hemminger 	}
477e9e414a4SStephen Hemminger 
4786c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
4796c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
4806c3169a3SBruce Richardson 	ctrl.data[0] = 1;
4816c3169a3SBruce Richardson 	dlen[0] = 1;
4826c3169a3SBruce Richardson 
4836c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
4846c3169a3SBruce Richardson 	if (ret)
4856c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
4866c3169a3SBruce Richardson }
4876c3169a3SBruce Richardson 
4886c3169a3SBruce Richardson static void
4896c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
4906c3169a3SBruce Richardson {
4916c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
4926c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
4936c3169a3SBruce Richardson 	int dlen[1];
4946c3169a3SBruce Richardson 	int ret;
4956c3169a3SBruce Richardson 
496e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
497e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
498e9e414a4SStephen Hemminger 		return;
499e9e414a4SStephen Hemminger 	}
500e9e414a4SStephen Hemminger 
5016c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5026c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
5036c3169a3SBruce Richardson 	ctrl.data[0] = 0;
5046c3169a3SBruce Richardson 	dlen[0] = 1;
5056c3169a3SBruce Richardson 
5066c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5076c3169a3SBruce Richardson 	if (ret)
5086c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
5096c3169a3SBruce Richardson }
5106c3169a3SBruce Richardson 
5116c3169a3SBruce Richardson static void
5126c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
5136c3169a3SBruce Richardson {
5146c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5156c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5166c3169a3SBruce Richardson 	int dlen[1];
5176c3169a3SBruce Richardson 	int ret;
5186c3169a3SBruce Richardson 
519e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
520e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
521e9e414a4SStephen Hemminger 		return;
522e9e414a4SStephen Hemminger 	}
523e9e414a4SStephen Hemminger 
5246c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5256c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
5266c3169a3SBruce Richardson 	ctrl.data[0] = 1;
5276c3169a3SBruce Richardson 	dlen[0] = 1;
5286c3169a3SBruce Richardson 
5296c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5306c3169a3SBruce Richardson 	if (ret)
5316c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
5326c3169a3SBruce Richardson }
5336c3169a3SBruce Richardson 
5346c3169a3SBruce Richardson static void
5356c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
5366c3169a3SBruce Richardson {
5376c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
5386c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
5396c3169a3SBruce Richardson 	int dlen[1];
5406c3169a3SBruce Richardson 	int ret;
5416c3169a3SBruce Richardson 
542e9e414a4SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
543e9e414a4SStephen Hemminger 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
544e9e414a4SStephen Hemminger 		return;
545e9e414a4SStephen Hemminger 	}
546e9e414a4SStephen Hemminger 
5476c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
5486c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
5496c3169a3SBruce Richardson 	ctrl.data[0] = 0;
5506c3169a3SBruce Richardson 	dlen[0] = 1;
5516c3169a3SBruce Richardson 
5526c3169a3SBruce Richardson 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
5536c3169a3SBruce Richardson 	if (ret)
5546c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
5556c3169a3SBruce Richardson }
5566c3169a3SBruce Richardson 
5576c3169a3SBruce Richardson /*
5586c3169a3SBruce Richardson  * dev_ops for virtio, bare necessities for basic operation
5596c3169a3SBruce Richardson  */
5606c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = {
5616c3169a3SBruce Richardson 	.dev_configure           = virtio_dev_configure,
5626c3169a3SBruce Richardson 	.dev_start               = virtio_dev_start,
5636c3169a3SBruce Richardson 	.dev_stop                = virtio_dev_stop,
5646c3169a3SBruce Richardson 	.dev_close               = virtio_dev_close,
5656c3169a3SBruce Richardson 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
5666c3169a3SBruce Richardson 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
5676c3169a3SBruce Richardson 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
5686c3169a3SBruce Richardson 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
5696c3169a3SBruce Richardson 
5706c3169a3SBruce Richardson 	.dev_infos_get           = virtio_dev_info_get,
5716c3169a3SBruce Richardson 	.stats_get               = virtio_dev_stats_get,
5726c3169a3SBruce Richardson 	.stats_reset             = virtio_dev_stats_reset,
5736c3169a3SBruce Richardson 	.link_update             = virtio_dev_link_update,
5746c3169a3SBruce Richardson 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
5756c3169a3SBruce Richardson 	.rx_queue_release        = virtio_dev_rx_queue_release,
5766c3169a3SBruce Richardson 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
5776c3169a3SBruce Richardson 	.tx_queue_release        = virtio_dev_tx_queue_release,
5786c3169a3SBruce Richardson 	/* collect stats per queue */
5796c3169a3SBruce Richardson 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
5806c3169a3SBruce Richardson 	.vlan_filter_set         = virtio_vlan_filter_set,
5816c3169a3SBruce Richardson 	.mac_addr_add            = virtio_mac_addr_add,
5826c3169a3SBruce Richardson 	.mac_addr_remove         = virtio_mac_addr_remove,
5836c3169a3SBruce Richardson 	.mac_addr_set            = virtio_mac_addr_set,
5846c3169a3SBruce Richardson };
5856c3169a3SBruce Richardson 
5866c3169a3SBruce Richardson static inline int
5876c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
5886c3169a3SBruce Richardson 				struct rte_eth_link *link)
5896c3169a3SBruce Richardson {
5906c3169a3SBruce Richardson 	struct rte_eth_link *dst = link;
5916c3169a3SBruce Richardson 	struct rte_eth_link *src = &(dev->data->dev_link);
5926c3169a3SBruce Richardson 
5936c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
5946c3169a3SBruce Richardson 			*(uint64_t *)src) == 0)
5956c3169a3SBruce Richardson 		return -1;
5966c3169a3SBruce Richardson 
5976c3169a3SBruce Richardson 	return 0;
5986c3169a3SBruce Richardson }
5996c3169a3SBruce Richardson 
6006c3169a3SBruce Richardson /**
6016c3169a3SBruce Richardson  * Atomically writes the link status information into global
6026c3169a3SBruce Richardson  * structure rte_eth_dev.
6036c3169a3SBruce Richardson  *
6046c3169a3SBruce Richardson  * @param dev
6056c3169a3SBruce Richardson  *   - Pointer to the structure rte_eth_dev to read from.
6066c3169a3SBruce Richardson  *   - Pointer to the buffer to be saved with the link status.
6076c3169a3SBruce Richardson  *
6086c3169a3SBruce Richardson  * @return
6096c3169a3SBruce Richardson  *   - On success, zero.
6106c3169a3SBruce Richardson  *   - On failure, negative value.
6116c3169a3SBruce Richardson  */
6126c3169a3SBruce Richardson static inline int
6136c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
6146c3169a3SBruce Richardson 		struct rte_eth_link *link)
6156c3169a3SBruce Richardson {
6166c3169a3SBruce Richardson 	struct rte_eth_link *dst = &(dev->data->dev_link);
6176c3169a3SBruce Richardson 	struct rte_eth_link *src = link;
6186c3169a3SBruce Richardson 
6196c3169a3SBruce Richardson 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
6206c3169a3SBruce Richardson 					*(uint64_t *)src) == 0)
6216c3169a3SBruce Richardson 		return -1;
6226c3169a3SBruce Richardson 
6236c3169a3SBruce Richardson 	return 0;
6246c3169a3SBruce Richardson }
6256c3169a3SBruce Richardson 
6266c3169a3SBruce Richardson static void
6276c3169a3SBruce Richardson virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
6286c3169a3SBruce Richardson {
6296c3169a3SBruce Richardson 	unsigned i;
6306c3169a3SBruce Richardson 
6316c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
6326c3169a3SBruce Richardson 		const struct virtqueue *txvq = dev->data->tx_queues[i];
6336c3169a3SBruce Richardson 		if (txvq == NULL)
6346c3169a3SBruce Richardson 			continue;
6356c3169a3SBruce Richardson 
6366c3169a3SBruce Richardson 		stats->opackets += txvq->packets;
6376c3169a3SBruce Richardson 		stats->obytes += txvq->bytes;
6386c3169a3SBruce Richardson 		stats->oerrors += txvq->errors;
6396c3169a3SBruce Richardson 
6406c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
6416c3169a3SBruce Richardson 			stats->q_opackets[i] = txvq->packets;
6426c3169a3SBruce Richardson 			stats->q_obytes[i] = txvq->bytes;
6436c3169a3SBruce Richardson 		}
6446c3169a3SBruce Richardson 	}
6456c3169a3SBruce Richardson 
6466c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
6476c3169a3SBruce Richardson 		const struct virtqueue *rxvq = dev->data->rx_queues[i];
6486c3169a3SBruce Richardson 		if (rxvq == NULL)
6496c3169a3SBruce Richardson 			continue;
6506c3169a3SBruce Richardson 
6516c3169a3SBruce Richardson 		stats->ipackets += rxvq->packets;
6526c3169a3SBruce Richardson 		stats->ibytes += rxvq->bytes;
6536c3169a3SBruce Richardson 		stats->ierrors += rxvq->errors;
6546c3169a3SBruce Richardson 
6556c3169a3SBruce Richardson 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
6566c3169a3SBruce Richardson 			stats->q_ipackets[i] = rxvq->packets;
6576c3169a3SBruce Richardson 			stats->q_ibytes[i] = rxvq->bytes;
6586c3169a3SBruce Richardson 		}
6596c3169a3SBruce Richardson 	}
6606c3169a3SBruce Richardson 
6616c3169a3SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
6626c3169a3SBruce Richardson }
6636c3169a3SBruce Richardson 
6646c3169a3SBruce Richardson static void
6656c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev)
6666c3169a3SBruce Richardson {
6676c3169a3SBruce Richardson 	unsigned int i;
6686c3169a3SBruce Richardson 
6696c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
6706c3169a3SBruce Richardson 		struct virtqueue *txvq = dev->data->tx_queues[i];
6716c3169a3SBruce Richardson 		if (txvq == NULL)
6726c3169a3SBruce Richardson 			continue;
6736c3169a3SBruce Richardson 
6746c3169a3SBruce Richardson 		txvq->packets = 0;
6756c3169a3SBruce Richardson 		txvq->bytes = 0;
6766c3169a3SBruce Richardson 		txvq->errors = 0;
6776c3169a3SBruce Richardson 	}
6786c3169a3SBruce Richardson 
6796c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
6806c3169a3SBruce Richardson 		struct virtqueue *rxvq = dev->data->rx_queues[i];
6816c3169a3SBruce Richardson 		if (rxvq == NULL)
6826c3169a3SBruce Richardson 			continue;
6836c3169a3SBruce Richardson 
6846c3169a3SBruce Richardson 		rxvq->packets = 0;
6856c3169a3SBruce Richardson 		rxvq->bytes = 0;
6866c3169a3SBruce Richardson 		rxvq->errors = 0;
6876c3169a3SBruce Richardson 	}
6886c3169a3SBruce Richardson 
6896c3169a3SBruce Richardson 	dev->data->rx_mbuf_alloc_failed = 0;
6906c3169a3SBruce Richardson }
6916c3169a3SBruce Richardson 
6926c3169a3SBruce Richardson static void
6936c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw)
6946c3169a3SBruce Richardson {
6956c3169a3SBruce Richardson 	vtpci_write_dev_config(hw,
6966c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
6976c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
6986c3169a3SBruce Richardson }
6996c3169a3SBruce Richardson 
7006c3169a3SBruce Richardson static void
7016c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw)
7026c3169a3SBruce Richardson {
7036c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
7046c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
7056c3169a3SBruce Richardson 			offsetof(struct virtio_net_config, mac),
7066c3169a3SBruce Richardson 			&hw->mac_addr, ETHER_ADDR_LEN);
7076c3169a3SBruce Richardson 	} else {
7086c3169a3SBruce Richardson 		eth_random_addr(&hw->mac_addr[0]);
7096c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
7106c3169a3SBruce Richardson 	}
7116c3169a3SBruce Richardson }
7126c3169a3SBruce Richardson 
71327046236SStephen Hemminger static void
7146c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw,
7156c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *uc,
7166c3169a3SBruce Richardson 		     const struct virtio_net_ctrl_mac *mc)
7176c3169a3SBruce Richardson {
7186c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
7196c3169a3SBruce Richardson 	int err, len[2];
7206c3169a3SBruce Richardson 
72127046236SStephen Hemminger 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
72227046236SStephen Hemminger 		PMD_DRV_LOG(INFO, "host does not support mac table\n");
72327046236SStephen Hemminger 		return;
72427046236SStephen Hemminger 	}
72527046236SStephen Hemminger 
7266c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
7276c3169a3SBruce Richardson 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
7286c3169a3SBruce Richardson 
7296c3169a3SBruce Richardson 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
7306c3169a3SBruce Richardson 	memcpy(ctrl.data, uc, len[0]);
7316c3169a3SBruce Richardson 
7326c3169a3SBruce Richardson 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
7336c3169a3SBruce Richardson 	memcpy(ctrl.data + len[0], mc, len[1]);
7346c3169a3SBruce Richardson 
7356c3169a3SBruce Richardson 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
7366c3169a3SBruce Richardson 	if (err != 0)
7376c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
7386c3169a3SBruce Richardson }
7396c3169a3SBruce Richardson 
7406c3169a3SBruce Richardson static void
7416c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
7426c3169a3SBruce Richardson 		    uint32_t index, uint32_t vmdq __rte_unused)
7436c3169a3SBruce Richardson {
7446c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
7456c3169a3SBruce Richardson 	const struct ether_addr *addrs = dev->data->mac_addrs;
7466c3169a3SBruce Richardson 	unsigned int i;
7476c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
7486c3169a3SBruce Richardson 
7496c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
7506c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
7516c3169a3SBruce Richardson 		return;
7526c3169a3SBruce Richardson 	}
7536c3169a3SBruce Richardson 
7546c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
7556c3169a3SBruce Richardson 	uc->entries = 0;
7566c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
7576c3169a3SBruce Richardson 	mc->entries = 0;
7586c3169a3SBruce Richardson 
7596c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
7606c3169a3SBruce Richardson 		const struct ether_addr *addr
7616c3169a3SBruce Richardson 			= (i == index) ? mac_addr : addrs + i;
7626c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl
7636c3169a3SBruce Richardson 			= is_multicast_ether_addr(addr) ? mc : uc;
7646c3169a3SBruce Richardson 
7656c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
7666c3169a3SBruce Richardson 	}
7676c3169a3SBruce Richardson 
7686c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
7696c3169a3SBruce Richardson }
7706c3169a3SBruce Richardson 
7716c3169a3SBruce Richardson static void
7726c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
7736c3169a3SBruce Richardson {
7746c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
7756c3169a3SBruce Richardson 	struct ether_addr *addrs = dev->data->mac_addrs;
7766c3169a3SBruce Richardson 	struct virtio_net_ctrl_mac *uc, *mc;
7776c3169a3SBruce Richardson 	unsigned int i;
7786c3169a3SBruce Richardson 
7796c3169a3SBruce Richardson 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
7806c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
7816c3169a3SBruce Richardson 		return;
7826c3169a3SBruce Richardson 	}
7836c3169a3SBruce Richardson 
7846c3169a3SBruce Richardson 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
7856c3169a3SBruce Richardson 	uc->entries = 0;
7866c3169a3SBruce Richardson 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
7876c3169a3SBruce Richardson 	mc->entries = 0;
7886c3169a3SBruce Richardson 
7896c3169a3SBruce Richardson 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
7906c3169a3SBruce Richardson 		struct virtio_net_ctrl_mac *tbl;
7916c3169a3SBruce Richardson 
7926c3169a3SBruce Richardson 		if (i == index || is_zero_ether_addr(addrs + i))
7936c3169a3SBruce Richardson 			continue;
7946c3169a3SBruce Richardson 
7956c3169a3SBruce Richardson 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
7966c3169a3SBruce Richardson 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
7976c3169a3SBruce Richardson 	}
7986c3169a3SBruce Richardson 
7996c3169a3SBruce Richardson 	virtio_mac_table_set(hw, uc, mc);
8006c3169a3SBruce Richardson }
8016c3169a3SBruce Richardson 
8026c3169a3SBruce Richardson static void
8036c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
8046c3169a3SBruce Richardson {
8056c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
8066c3169a3SBruce Richardson 
8076c3169a3SBruce Richardson 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
8086c3169a3SBruce Richardson 
8096c3169a3SBruce Richardson 	/* Use atomic update if available */
8106c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
8116c3169a3SBruce Richardson 		struct virtio_pmd_ctrl ctrl;
8126c3169a3SBruce Richardson 		int len = ETHER_ADDR_LEN;
8136c3169a3SBruce Richardson 
8146c3169a3SBruce Richardson 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
8156c3169a3SBruce Richardson 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
8166c3169a3SBruce Richardson 
8176c3169a3SBruce Richardson 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
8186c3169a3SBruce Richardson 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
8196c3169a3SBruce Richardson 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
8206c3169a3SBruce Richardson 		virtio_set_hwaddr(hw);
8216c3169a3SBruce Richardson }
8226c3169a3SBruce Richardson 
8236c3169a3SBruce Richardson static int
8246c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
8256c3169a3SBruce Richardson {
8266c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
8276c3169a3SBruce Richardson 	struct virtio_pmd_ctrl ctrl;
8286c3169a3SBruce Richardson 	int len;
8296c3169a3SBruce Richardson 
8306c3169a3SBruce Richardson 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
8316c3169a3SBruce Richardson 		return -ENOTSUP;
8326c3169a3SBruce Richardson 
8336c3169a3SBruce Richardson 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
8346c3169a3SBruce Richardson 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
8356c3169a3SBruce Richardson 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
8366c3169a3SBruce Richardson 	len = sizeof(vlan_id);
8376c3169a3SBruce Richardson 
8386c3169a3SBruce Richardson 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
8396c3169a3SBruce Richardson }
8406c3169a3SBruce Richardson 
8416c3169a3SBruce Richardson static void
8426c3169a3SBruce Richardson virtio_negotiate_features(struct virtio_hw *hw)
8436c3169a3SBruce Richardson {
8444a92b671SStephen Hemminger 	uint32_t host_features;
8456c3169a3SBruce Richardson 
8466c3169a3SBruce Richardson 	/* Prepare guest_features: feature that driver wants to support */
8474a92b671SStephen Hemminger 	hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
8486c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
8496c3169a3SBruce Richardson 		hw->guest_features);
8506c3169a3SBruce Richardson 
8516c3169a3SBruce Richardson 	/* Read device(host) feature bits */
8526c3169a3SBruce Richardson 	host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
8536c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
8546c3169a3SBruce Richardson 		host_features);
8556c3169a3SBruce Richardson 
8566c3169a3SBruce Richardson 	/*
8576c3169a3SBruce Richardson 	 * Negotiate features: Subset of device feature bits are written back
8586c3169a3SBruce Richardson 	 * guest feature bits.
8596c3169a3SBruce Richardson 	 */
8606c3169a3SBruce Richardson 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
8616c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
8626c3169a3SBruce Richardson 		hw->guest_features);
8636c3169a3SBruce Richardson }
8646c3169a3SBruce Richardson 
8656c3169a3SBruce Richardson #ifdef RTE_EXEC_ENV_LINUXAPP
8666c3169a3SBruce Richardson static int
8676c3169a3SBruce Richardson parse_sysfs_value(const char *filename, unsigned long *val)
8686c3169a3SBruce Richardson {
8696c3169a3SBruce Richardson 	FILE *f;
8706c3169a3SBruce Richardson 	char buf[BUFSIZ];
8716c3169a3SBruce Richardson 	char *end = NULL;
8726c3169a3SBruce Richardson 
8736c3169a3SBruce Richardson 	f = fopen(filename, "r");
8746c3169a3SBruce Richardson 	if (f == NULL) {
8756c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
8766c3169a3SBruce Richardson 			     __func__, filename);
8776c3169a3SBruce Richardson 		return -1;
8786c3169a3SBruce Richardson 	}
8796c3169a3SBruce Richardson 
8806c3169a3SBruce Richardson 	if (fgets(buf, sizeof(buf), f) == NULL) {
8816c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
8826c3169a3SBruce Richardson 			     __func__, filename);
8836c3169a3SBruce Richardson 		fclose(f);
8846c3169a3SBruce Richardson 		return -1;
8856c3169a3SBruce Richardson 	}
8866c3169a3SBruce Richardson 	*val = strtoul(buf, &end, 0);
8876c3169a3SBruce Richardson 	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
8886c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
8896c3169a3SBruce Richardson 			     __func__, filename);
8906c3169a3SBruce Richardson 		fclose(f);
8916c3169a3SBruce Richardson 		return -1;
8926c3169a3SBruce Richardson 	}
8936c3169a3SBruce Richardson 	fclose(f);
8946c3169a3SBruce Richardson 	return 0;
8956c3169a3SBruce Richardson }
8966c3169a3SBruce Richardson 
8976c3169a3SBruce Richardson static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen,
8986c3169a3SBruce Richardson 			unsigned int *uio_num)
8996c3169a3SBruce Richardson {
9006c3169a3SBruce Richardson 	struct dirent *e;
9016c3169a3SBruce Richardson 	DIR *dir;
9026c3169a3SBruce Richardson 	char dirname[PATH_MAX];
9036c3169a3SBruce Richardson 
9046c3169a3SBruce Richardson 	/* depending on kernel version, uio can be located in uio/uioX
9056c3169a3SBruce Richardson 	 * or uio:uioX */
9066c3169a3SBruce Richardson 	snprintf(dirname, sizeof(dirname),
9076c3169a3SBruce Richardson 		     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
9086c3169a3SBruce Richardson 		     loc->domain, loc->bus, loc->devid, loc->function);
9096c3169a3SBruce Richardson 	dir = opendir(dirname);
9106c3169a3SBruce Richardson 	if (dir == NULL) {
9116c3169a3SBruce Richardson 		/* retry with the parent directory */
9126c3169a3SBruce Richardson 		snprintf(dirname, sizeof(dirname),
9136c3169a3SBruce Richardson 			     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
9146c3169a3SBruce Richardson 			     loc->domain, loc->bus, loc->devid, loc->function);
9156c3169a3SBruce Richardson 		dir = opendir(dirname);
9166c3169a3SBruce Richardson 
9176c3169a3SBruce Richardson 		if (dir == NULL) {
9186c3169a3SBruce Richardson 			PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
9196c3169a3SBruce Richardson 			return -1;
9206c3169a3SBruce Richardson 		}
9216c3169a3SBruce Richardson 	}
9226c3169a3SBruce Richardson 
9236c3169a3SBruce Richardson 	/* take the first file starting with "uio" */
9246c3169a3SBruce Richardson 	while ((e = readdir(dir)) != NULL) {
9256c3169a3SBruce Richardson 		/* format could be uio%d ...*/
9266c3169a3SBruce Richardson 		int shortprefix_len = sizeof("uio") - 1;
9276c3169a3SBruce Richardson 		/* ... or uio:uio%d */
9286c3169a3SBruce Richardson 		int longprefix_len = sizeof("uio:uio") - 1;
9296c3169a3SBruce Richardson 		char *endptr;
9306c3169a3SBruce Richardson 
9316c3169a3SBruce Richardson 		if (strncmp(e->d_name, "uio", 3) != 0)
9326c3169a3SBruce Richardson 			continue;
9336c3169a3SBruce Richardson 
9346c3169a3SBruce Richardson 		/* first try uio%d */
9356c3169a3SBruce Richardson 		errno = 0;
9366c3169a3SBruce Richardson 		*uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
9376c3169a3SBruce Richardson 		if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
9386c3169a3SBruce Richardson 			snprintf(buf, buflen, "%s/uio%u", dirname, *uio_num);
9396c3169a3SBruce Richardson 			break;
9406c3169a3SBruce Richardson 		}
9416c3169a3SBruce Richardson 
9426c3169a3SBruce Richardson 		/* then try uio:uio%d */
9436c3169a3SBruce Richardson 		errno = 0;
9446c3169a3SBruce Richardson 		*uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
9456c3169a3SBruce Richardson 		if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
9466c3169a3SBruce Richardson 			snprintf(buf, buflen, "%s/uio:uio%u", dirname,
9476c3169a3SBruce Richardson 				     *uio_num);
9486c3169a3SBruce Richardson 			break;
9496c3169a3SBruce Richardson 		}
9506c3169a3SBruce Richardson 	}
9516c3169a3SBruce Richardson 	closedir(dir);
9526c3169a3SBruce Richardson 
9536c3169a3SBruce Richardson 	/* No uio resource found */
9546c3169a3SBruce Richardson 	if (e == NULL) {
9556c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Could not find uio resource");
9566c3169a3SBruce Richardson 		return -1;
9576c3169a3SBruce Richardson 	}
9586c3169a3SBruce Richardson 
9596c3169a3SBruce Richardson 	return 0;
9606c3169a3SBruce Richardson }
9616c3169a3SBruce Richardson 
9626c3169a3SBruce Richardson static int
9636c3169a3SBruce Richardson virtio_has_msix(const struct rte_pci_addr *loc)
9646c3169a3SBruce Richardson {
9656c3169a3SBruce Richardson 	DIR *d;
9666c3169a3SBruce Richardson 	char dirname[PATH_MAX];
9676c3169a3SBruce Richardson 
9686c3169a3SBruce Richardson 	snprintf(dirname, sizeof(dirname),
9696c3169a3SBruce Richardson 		     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
9706c3169a3SBruce Richardson 		     loc->domain, loc->bus, loc->devid, loc->function);
9716c3169a3SBruce Richardson 
9726c3169a3SBruce Richardson 	d = opendir(dirname);
9736c3169a3SBruce Richardson 	if (d)
9746c3169a3SBruce Richardson 		closedir(d);
9756c3169a3SBruce Richardson 
9766c3169a3SBruce Richardson 	return (d != NULL);
9776c3169a3SBruce Richardson }
9786c3169a3SBruce Richardson 
9796c3169a3SBruce Richardson /* Extract I/O port numbers from sysfs */
9806c3169a3SBruce Richardson static int virtio_resource_init_by_uio(struct rte_pci_device *pci_dev)
9816c3169a3SBruce Richardson {
9826c3169a3SBruce Richardson 	char dirname[PATH_MAX];
9836c3169a3SBruce Richardson 	char filename[PATH_MAX];
9846c3169a3SBruce Richardson 	unsigned long start, size;
9856c3169a3SBruce Richardson 	unsigned int uio_num;
9866c3169a3SBruce Richardson 
9876c3169a3SBruce Richardson 	if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname), &uio_num) < 0)
9886c3169a3SBruce Richardson 		return -1;
9896c3169a3SBruce Richardson 
9906c3169a3SBruce Richardson 	/* get portio size */
9916c3169a3SBruce Richardson 	snprintf(filename, sizeof(filename),
9926c3169a3SBruce Richardson 		     "%s/portio/port0/size", dirname);
9936c3169a3SBruce Richardson 	if (parse_sysfs_value(filename, &size) < 0) {
9946c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): cannot parse size",
9956c3169a3SBruce Richardson 			     __func__);
9966c3169a3SBruce Richardson 		return -1;
9976c3169a3SBruce Richardson 	}
9986c3169a3SBruce Richardson 
9996c3169a3SBruce Richardson 	/* get portio start */
10006c3169a3SBruce Richardson 	snprintf(filename, sizeof(filename),
10016c3169a3SBruce Richardson 		 "%s/portio/port0/start", dirname);
10026c3169a3SBruce Richardson 	if (parse_sysfs_value(filename, &start) < 0) {
10036c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
10046c3169a3SBruce Richardson 			     __func__);
10056c3169a3SBruce Richardson 		return -1;
10066c3169a3SBruce Richardson 	}
10076c3169a3SBruce Richardson 	pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
10086c3169a3SBruce Richardson 	pci_dev->mem_resource[0].len =  (uint64_t)size;
10096c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
10106c3169a3SBruce Richardson 		     "PCI Port IO found start=0x%lx with size=0x%lx",
10116c3169a3SBruce Richardson 		     start, size);
10126c3169a3SBruce Richardson 
10136c3169a3SBruce Richardson 	/* save fd */
10146c3169a3SBruce Richardson 	memset(dirname, 0, sizeof(dirname));
10156c3169a3SBruce Richardson 	snprintf(dirname, sizeof(dirname), "/dev/uio%u", uio_num);
10166c3169a3SBruce Richardson 	pci_dev->intr_handle.fd = open(dirname, O_RDWR);
10176c3169a3SBruce Richardson 	if (pci_dev->intr_handle.fd < 0) {
10186c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "Cannot open %s: %s\n",
10196c3169a3SBruce Richardson 			dirname, strerror(errno));
10206c3169a3SBruce Richardson 		return -1;
10216c3169a3SBruce Richardson 	}
10226c3169a3SBruce Richardson 
10236c3169a3SBruce Richardson 	pci_dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
10246c3169a3SBruce Richardson 	pci_dev->driver->drv_flags |= RTE_PCI_DRV_INTR_LSC;
10256c3169a3SBruce Richardson 
10266c3169a3SBruce Richardson 	return 0;
10276c3169a3SBruce Richardson }
10286c3169a3SBruce Richardson 
10296c3169a3SBruce Richardson /* Extract port I/O numbers from proc/ioports */
10306c3169a3SBruce Richardson static int virtio_resource_init_by_ioports(struct rte_pci_device *pci_dev)
10316c3169a3SBruce Richardson {
10326c3169a3SBruce Richardson 	uint16_t start, end;
10336c3169a3SBruce Richardson 	int size;
10346c3169a3SBruce Richardson 	FILE *fp;
10356c3169a3SBruce Richardson 	char *line = NULL;
10366c3169a3SBruce Richardson 	char pci_id[16];
10376c3169a3SBruce Richardson 	int found = 0;
10386c3169a3SBruce Richardson 	size_t linesz;
10396c3169a3SBruce Richardson 
10406c3169a3SBruce Richardson 	snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
10416c3169a3SBruce Richardson 		 pci_dev->addr.domain,
10426c3169a3SBruce Richardson 		 pci_dev->addr.bus,
10436c3169a3SBruce Richardson 		 pci_dev->addr.devid,
10446c3169a3SBruce Richardson 		 pci_dev->addr.function);
10456c3169a3SBruce Richardson 
10466c3169a3SBruce Richardson 	fp = fopen("/proc/ioports", "r");
10476c3169a3SBruce Richardson 	if (fp == NULL) {
10486c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "%s(): can't open ioports", __func__);
10496c3169a3SBruce Richardson 		return -1;
10506c3169a3SBruce Richardson 	}
10516c3169a3SBruce Richardson 
10526c3169a3SBruce Richardson 	while (getdelim(&line, &linesz, '\n', fp) > 0) {
10536c3169a3SBruce Richardson 		char *ptr = line;
10546c3169a3SBruce Richardson 		char *left;
10556c3169a3SBruce Richardson 		int n;
10566c3169a3SBruce Richardson 
10576c3169a3SBruce Richardson 		n = strcspn(ptr, ":");
10586c3169a3SBruce Richardson 		ptr[n] = 0;
10596c3169a3SBruce Richardson 		left = &ptr[n+1];
10606c3169a3SBruce Richardson 
10616c3169a3SBruce Richardson 		while (*left && isspace(*left))
10626c3169a3SBruce Richardson 			left++;
10636c3169a3SBruce Richardson 
10646c3169a3SBruce Richardson 		if (!strncmp(left, pci_id, strlen(pci_id))) {
10656c3169a3SBruce Richardson 			found = 1;
10666c3169a3SBruce Richardson 
10676c3169a3SBruce Richardson 			while (*ptr && isspace(*ptr))
10686c3169a3SBruce Richardson 				ptr++;
10696c3169a3SBruce Richardson 
10706c3169a3SBruce Richardson 			sscanf(ptr, "%04hx-%04hx", &start, &end);
10716c3169a3SBruce Richardson 			size = end - start + 1;
10726c3169a3SBruce Richardson 
10736c3169a3SBruce Richardson 			break;
10746c3169a3SBruce Richardson 		}
10756c3169a3SBruce Richardson 	}
10766c3169a3SBruce Richardson 
10776c3169a3SBruce Richardson 	free(line);
10786c3169a3SBruce Richardson 	fclose(fp);
10796c3169a3SBruce Richardson 
10806c3169a3SBruce Richardson 	if (!found)
10816c3169a3SBruce Richardson 		return -1;
10826c3169a3SBruce Richardson 
10836c3169a3SBruce Richardson 	pci_dev->mem_resource[0].addr = (void *)(uintptr_t)(uint32_t)start;
10846c3169a3SBruce Richardson 	pci_dev->mem_resource[0].len =  (uint64_t)size;
10856c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
10866c3169a3SBruce Richardson 		"PCI Port IO found start=0x%x with size=0x%x",
10876c3169a3SBruce Richardson 		start, size);
10886c3169a3SBruce Richardson 
10896c3169a3SBruce Richardson 	/* can't support lsc interrupt without uio */
10906c3169a3SBruce Richardson 	pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
10916c3169a3SBruce Richardson 
10926c3169a3SBruce Richardson 	return 0;
10936c3169a3SBruce Richardson }
10946c3169a3SBruce Richardson 
10956c3169a3SBruce Richardson /* Extract I/O port numbers from sysfs */
10966c3169a3SBruce Richardson static int virtio_resource_init(struct rte_pci_device *pci_dev)
10976c3169a3SBruce Richardson {
10986c3169a3SBruce Richardson 	if (virtio_resource_init_by_uio(pci_dev) == 0)
10996c3169a3SBruce Richardson 		return 0;
11006c3169a3SBruce Richardson 	else
11016c3169a3SBruce Richardson 		return virtio_resource_init_by_ioports(pci_dev);
11026c3169a3SBruce Richardson }
11036c3169a3SBruce Richardson 
11046c3169a3SBruce Richardson #else
11056c3169a3SBruce Richardson static int
11066c3169a3SBruce Richardson virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
11076c3169a3SBruce Richardson {
11086c3169a3SBruce Richardson 	/* nic_uio does not enable interrupts, return 0 (false). */
11096c3169a3SBruce Richardson 	return 0;
11106c3169a3SBruce Richardson }
11116c3169a3SBruce Richardson 
11126c3169a3SBruce Richardson static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
11136c3169a3SBruce Richardson {
11146c3169a3SBruce Richardson 	/* no setup required */
11156c3169a3SBruce Richardson 	return 0;
11166c3169a3SBruce Richardson }
11176c3169a3SBruce Richardson #endif
11186c3169a3SBruce Richardson 
11196c3169a3SBruce Richardson /*
11206c3169a3SBruce Richardson  * Process Virtio Config changed interrupt and call the callback
11216c3169a3SBruce Richardson  * if link state changed.
11226c3169a3SBruce Richardson  */
11236c3169a3SBruce Richardson static void
11246c3169a3SBruce Richardson virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
11256c3169a3SBruce Richardson 			 void *param)
11266c3169a3SBruce Richardson {
11276c3169a3SBruce Richardson 	struct rte_eth_dev *dev = param;
11286c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
11296c3169a3SBruce Richardson 	uint8_t isr;
11306c3169a3SBruce Richardson 
11316c3169a3SBruce Richardson 	/* Read interrupt status which clears interrupt */
11326c3169a3SBruce Richardson 	isr = vtpci_isr(hw);
11336c3169a3SBruce Richardson 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
11346c3169a3SBruce Richardson 
11356c3169a3SBruce Richardson 	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
11366c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "interrupt enable failed");
11376c3169a3SBruce Richardson 
11386c3169a3SBruce Richardson 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
11396c3169a3SBruce Richardson 		if (virtio_dev_link_update(dev, 0) == 0)
11406c3169a3SBruce Richardson 			_rte_eth_dev_callback_process(dev,
11416c3169a3SBruce Richardson 						      RTE_ETH_EVENT_INTR_LSC);
11426c3169a3SBruce Richardson 	}
11436c3169a3SBruce Richardson 
11446c3169a3SBruce Richardson }
11456c3169a3SBruce Richardson 
11466c3169a3SBruce Richardson static void
11476c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev)
11486c3169a3SBruce Richardson {
11496c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11506c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
11516c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
11526c3169a3SBruce Richardson 	else
11536c3169a3SBruce Richardson 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
11546c3169a3SBruce Richardson }
11556c3169a3SBruce Richardson 
11566c3169a3SBruce Richardson /*
11576c3169a3SBruce Richardson  * This function is based on probe() function in virtio_pci.c
11586c3169a3SBruce Richardson  * It returns 0 on success.
11596c3169a3SBruce Richardson  */
11606c3169a3SBruce Richardson static int
11616c3169a3SBruce Richardson eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
11626c3169a3SBruce Richardson {
11636c3169a3SBruce Richardson 	struct virtio_hw *hw = eth_dev->data->dev_private;
11646c3169a3SBruce Richardson 	struct virtio_net_config *config;
11656c3169a3SBruce Richardson 	struct virtio_net_config local_config;
11666c3169a3SBruce Richardson 	uint32_t offset_conf = sizeof(config->mac);
11676c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev;
11686c3169a3SBruce Richardson 
11696c3169a3SBruce Richardson 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
11706c3169a3SBruce Richardson 
11716c3169a3SBruce Richardson 	eth_dev->dev_ops = &virtio_eth_dev_ops;
11726c3169a3SBruce Richardson 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
11736c3169a3SBruce Richardson 
11746c3169a3SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
11756c3169a3SBruce Richardson 		rx_func_get(eth_dev);
11766c3169a3SBruce Richardson 		return 0;
11776c3169a3SBruce Richardson 	}
11786c3169a3SBruce Richardson 
11796c3169a3SBruce Richardson 	/* Allocate memory for storing MAC addresses */
11806c3169a3SBruce Richardson 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
11816c3169a3SBruce Richardson 	if (eth_dev->data->mac_addrs == NULL) {
11826c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR,
11836c3169a3SBruce Richardson 			"Failed to allocate %d bytes needed to store MAC addresses",
11846c3169a3SBruce Richardson 			ETHER_ADDR_LEN);
11856c3169a3SBruce Richardson 		return -ENOMEM;
11866c3169a3SBruce Richardson 	}
11876c3169a3SBruce Richardson 
11886c3169a3SBruce Richardson 	pci_dev = eth_dev->pci_dev;
11896c3169a3SBruce Richardson 	if (virtio_resource_init(pci_dev) < 0)
11906c3169a3SBruce Richardson 		return -1;
11916c3169a3SBruce Richardson 
11926c3169a3SBruce Richardson 	hw->use_msix = virtio_has_msix(&pci_dev->addr);
11936c3169a3SBruce Richardson 	hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
11946c3169a3SBruce Richardson 
11956c3169a3SBruce Richardson 	/* Reset the device although not necessary at startup */
11966c3169a3SBruce Richardson 	vtpci_reset(hw);
11976c3169a3SBruce Richardson 
11986c3169a3SBruce Richardson 	/* Tell the host we've noticed this device. */
11996c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
12006c3169a3SBruce Richardson 
12016c3169a3SBruce Richardson 	/* Tell the host we've known how to drive the device. */
12026c3169a3SBruce Richardson 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
12036c3169a3SBruce Richardson 	virtio_negotiate_features(hw);
12046c3169a3SBruce Richardson 
12056c3169a3SBruce Richardson 	rx_func_get(eth_dev);
12066c3169a3SBruce Richardson 
12076c3169a3SBruce Richardson 	/* Setting up rx_header size for the device */
12086c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
12096c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
12106c3169a3SBruce Richardson 	else
12116c3169a3SBruce Richardson 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
12126c3169a3SBruce Richardson 
12136c3169a3SBruce Richardson 	/* Copy the permanent MAC address to: virtio_hw */
12146c3169a3SBruce Richardson 	virtio_get_hwaddr(hw);
12156c3169a3SBruce Richardson 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
12166c3169a3SBruce Richardson 			&eth_dev->data->mac_addrs[0]);
12176c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG,
12186c3169a3SBruce Richardson 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12196c3169a3SBruce Richardson 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
12206c3169a3SBruce Richardson 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
12216c3169a3SBruce Richardson 
12226c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
12236c3169a3SBruce Richardson 		config = &local_config;
12246c3169a3SBruce Richardson 
12256c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
12266c3169a3SBruce Richardson 			offset_conf += sizeof(config->status);
12276c3169a3SBruce Richardson 		} else {
12286c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12296c3169a3SBruce Richardson 				     "VIRTIO_NET_F_STATUS is not supported");
12306c3169a3SBruce Richardson 			config->status = 0;
12316c3169a3SBruce Richardson 		}
12326c3169a3SBruce Richardson 
12336c3169a3SBruce Richardson 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
12346c3169a3SBruce Richardson 			offset_conf += sizeof(config->max_virtqueue_pairs);
12356c3169a3SBruce Richardson 		} else {
12366c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG,
12376c3169a3SBruce Richardson 				     "VIRTIO_NET_F_MQ is not supported");
12386c3169a3SBruce Richardson 			config->max_virtqueue_pairs = 1;
12396c3169a3SBruce Richardson 		}
12406c3169a3SBruce Richardson 
12416c3169a3SBruce Richardson 		vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
12426c3169a3SBruce Richardson 
12436c3169a3SBruce Richardson 		hw->max_rx_queues =
12446c3169a3SBruce Richardson 			(VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
12456c3169a3SBruce Richardson 			VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
12466c3169a3SBruce Richardson 		hw->max_tx_queues =
12476c3169a3SBruce Richardson 			(VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
12486c3169a3SBruce Richardson 			VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
12496c3169a3SBruce Richardson 
12506c3169a3SBruce Richardson 		virtio_dev_cq_queue_setup(eth_dev,
12516c3169a3SBruce Richardson 					config->max_virtqueue_pairs * 2,
12526c3169a3SBruce Richardson 					SOCKET_ID_ANY);
12536c3169a3SBruce Richardson 
12546c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
12556c3169a3SBruce Richardson 				config->max_virtqueue_pairs);
12566c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
12576c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
12586c3169a3SBruce Richardson 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
12596c3169a3SBruce Richardson 				config->mac[0], config->mac[1],
12606c3169a3SBruce Richardson 				config->mac[2], config->mac[3],
12616c3169a3SBruce Richardson 				config->mac[4], config->mac[5]);
12626c3169a3SBruce Richardson 	} else {
12636c3169a3SBruce Richardson 		hw->max_rx_queues = 1;
12646c3169a3SBruce Richardson 		hw->max_tx_queues = 1;
12656c3169a3SBruce Richardson 	}
12666c3169a3SBruce Richardson 
12676c3169a3SBruce Richardson 	eth_dev->data->nb_rx_queues = hw->max_rx_queues;
12686c3169a3SBruce Richardson 	eth_dev->data->nb_tx_queues = hw->max_tx_queues;
12696c3169a3SBruce Richardson 
12706c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
12716c3169a3SBruce Richardson 			hw->max_rx_queues, hw->max_tx_queues);
12726c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
12736c3169a3SBruce Richardson 			eth_dev->data->port_id, pci_dev->id.vendor_id,
12746c3169a3SBruce Richardson 			pci_dev->id.device_id);
12756c3169a3SBruce Richardson 
12766c3169a3SBruce Richardson 	/* Setup interrupt callback  */
12776c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
12786c3169a3SBruce Richardson 		rte_intr_callback_register(&pci_dev->intr_handle,
12796c3169a3SBruce Richardson 				   virtio_interrupt_handler, eth_dev);
12806c3169a3SBruce Richardson 
12816c3169a3SBruce Richardson 	virtio_dev_cq_start(eth_dev);
12826c3169a3SBruce Richardson 
12836c3169a3SBruce Richardson 	return 0;
12846c3169a3SBruce Richardson }
12856c3169a3SBruce Richardson 
1286abf4c84bSBernard Iremonger static int
1287abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1288abf4c84bSBernard Iremonger {
1289abf4c84bSBernard Iremonger 	struct rte_pci_device *pci_dev;
1290abf4c84bSBernard Iremonger 	struct virtio_hw *hw = eth_dev->data->dev_private;
1291abf4c84bSBernard Iremonger 
1292abf4c84bSBernard Iremonger 	PMD_INIT_FUNC_TRACE();
1293abf4c84bSBernard Iremonger 
1294abf4c84bSBernard Iremonger 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1295abf4c84bSBernard Iremonger 		return -EPERM;
1296abf4c84bSBernard Iremonger 
1297abf4c84bSBernard Iremonger 	if (hw->started == 1) {
1298abf4c84bSBernard Iremonger 		virtio_dev_stop(eth_dev);
1299abf4c84bSBernard Iremonger 		virtio_dev_close(eth_dev);
1300abf4c84bSBernard Iremonger 	}
1301abf4c84bSBernard Iremonger 	pci_dev = eth_dev->pci_dev;
1302abf4c84bSBernard Iremonger 
1303abf4c84bSBernard Iremonger 	eth_dev->dev_ops = NULL;
1304abf4c84bSBernard Iremonger 	eth_dev->tx_pkt_burst = NULL;
1305abf4c84bSBernard Iremonger 	eth_dev->rx_pkt_burst = NULL;
1306abf4c84bSBernard Iremonger 
13075382b188SBernard Iremonger 	virtio_dev_queue_release(hw->cvq);
1308abf4c84bSBernard Iremonger 
1309abf4c84bSBernard Iremonger 	rte_free(eth_dev->data->mac_addrs);
1310abf4c84bSBernard Iremonger 	eth_dev->data->mac_addrs = NULL;
1311abf4c84bSBernard Iremonger 
1312abf4c84bSBernard Iremonger 	/* reset interrupt callback  */
1313abf4c84bSBernard Iremonger 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1314abf4c84bSBernard Iremonger 		rte_intr_callback_unregister(&pci_dev->intr_handle,
1315abf4c84bSBernard Iremonger 						virtio_interrupt_handler,
1316abf4c84bSBernard Iremonger 						eth_dev);
1317abf4c84bSBernard Iremonger 
1318abf4c84bSBernard Iremonger 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1319abf4c84bSBernard Iremonger 
1320abf4c84bSBernard Iremonger 	return 0;
1321abf4c84bSBernard Iremonger }
1322abf4c84bSBernard Iremonger 
13236c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = {
13246c52c126SStephen Hemminger 	.pci_drv = {
13256c3169a3SBruce Richardson 		.name = "rte_virtio_pmd",
13266c3169a3SBruce Richardson 		.id_table = pci_id_virtio_map,
1327abf4c84bSBernard Iremonger 		.drv_flags = RTE_PCI_DRV_DETACHABLE,
13286c3169a3SBruce Richardson 	},
13296c3169a3SBruce Richardson 	.eth_dev_init = eth_virtio_dev_init,
1330abf4c84bSBernard Iremonger 	.eth_dev_uninit = eth_virtio_dev_uninit,
13316c3169a3SBruce Richardson 	.dev_private_size = sizeof(struct virtio_hw),
13326c3169a3SBruce Richardson };
13336c3169a3SBruce Richardson 
13346c3169a3SBruce Richardson /*
13356c3169a3SBruce Richardson  * Driver initialization routine.
13366c3169a3SBruce Richardson  * Invoked once at EAL init time.
13376c3169a3SBruce Richardson  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
13386c3169a3SBruce Richardson  * Returns 0 on success.
13396c3169a3SBruce Richardson  */
13406c3169a3SBruce Richardson static int
13416c3169a3SBruce Richardson rte_virtio_pmd_init(const char *name __rte_unused,
13426c3169a3SBruce Richardson 		    const char *param __rte_unused)
13436c3169a3SBruce Richardson {
13446c3169a3SBruce Richardson 	if (rte_eal_iopl_init() != 0) {
13456c3169a3SBruce Richardson 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
13466c3169a3SBruce Richardson 		return -1;
13476c3169a3SBruce Richardson 	}
13486c3169a3SBruce Richardson 
13496c3169a3SBruce Richardson 	rte_eth_driver_register(&rte_virtio_pmd);
13506c3169a3SBruce Richardson 	return 0;
13516c3169a3SBruce Richardson }
13526c3169a3SBruce Richardson 
13536c3169a3SBruce Richardson /*
13546c3169a3SBruce Richardson  * Configure virtio device
13556c3169a3SBruce Richardson  * It returns 0 on success.
13566c3169a3SBruce Richardson  */
13576c3169a3SBruce Richardson static int
13586c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev)
13596c3169a3SBruce Richardson {
13606c3169a3SBruce Richardson 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
13616c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
13626c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
13636c3169a3SBruce Richardson 
13646c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "configure");
13656c3169a3SBruce Richardson 
13666c3169a3SBruce Richardson 	if (rxmode->hw_ip_checksum) {
13676c3169a3SBruce Richardson 		PMD_DRV_LOG(ERR, "HW IP checksum not supported");
13686c3169a3SBruce Richardson 		return (-EINVAL);
13696c3169a3SBruce Richardson 	}
13706c3169a3SBruce Richardson 
13716c3169a3SBruce Richardson 	hw->vlan_strip = rxmode->hw_vlan_strip;
13726c3169a3SBruce Richardson 
13736c3169a3SBruce Richardson 	if (rxmode->hw_vlan_filter
13746c3169a3SBruce Richardson 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
13756c3169a3SBruce Richardson 		PMD_DRV_LOG(NOTICE,
13766c3169a3SBruce Richardson 			    "vlan filtering not available on this host");
13776c3169a3SBruce Richardson 		return -ENOTSUP;
13786c3169a3SBruce Richardson 	}
13796c3169a3SBruce Richardson 
13806c3169a3SBruce Richardson 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
13816c3169a3SBruce Richardson 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
13826c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "failed to set config vector");
13836c3169a3SBruce Richardson 			return -EBUSY;
13846c3169a3SBruce Richardson 		}
13856c3169a3SBruce Richardson 
13866c3169a3SBruce Richardson 	return 0;
13876c3169a3SBruce Richardson }
13886c3169a3SBruce Richardson 
13896c3169a3SBruce Richardson 
13906c3169a3SBruce Richardson static int
13916c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev)
13926c3169a3SBruce Richardson {
13936c3169a3SBruce Richardson 	uint16_t nb_queues, i;
13946c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
13956c3169a3SBruce Richardson 	struct rte_pci_device *pci_dev = dev->pci_dev;
13966c3169a3SBruce Richardson 
13976c3169a3SBruce Richardson 	/* check if lsc interrupt feature is enabled */
13986c3169a3SBruce Richardson 	if ((dev->data->dev_conf.intr_conf.lsc) &&
13996c3169a3SBruce Richardson 		(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
14006c3169a3SBruce Richardson 		if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
14016c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "link status not supported by host");
14026c3169a3SBruce Richardson 			return -ENOTSUP;
14036c3169a3SBruce Richardson 		}
14046c3169a3SBruce Richardson 
14056c3169a3SBruce Richardson 		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
14066c3169a3SBruce Richardson 			PMD_DRV_LOG(ERR, "interrupt enable failed");
14076c3169a3SBruce Richardson 			return -EIO;
14086c3169a3SBruce Richardson 		}
14096c3169a3SBruce Richardson 	}
14106c3169a3SBruce Richardson 
14116c3169a3SBruce Richardson 	/* Initialize Link state */
14126c3169a3SBruce Richardson 	virtio_dev_link_update(dev, 0);
14136c3169a3SBruce Richardson 
14146c3169a3SBruce Richardson 	/* On restart after stop do not touch queues */
14156c3169a3SBruce Richardson 	if (hw->started)
14166c3169a3SBruce Richardson 		return 0;
14176c3169a3SBruce Richardson 
14186c3169a3SBruce Richardson 	/* Do final configuration before rx/tx engine starts */
14196c3169a3SBruce Richardson 	virtio_dev_rxtx_start(dev);
14206c3169a3SBruce Richardson 	vtpci_reinit_complete(hw);
14216c3169a3SBruce Richardson 
14226c3169a3SBruce Richardson 	hw->started = 1;
14236c3169a3SBruce Richardson 
14246c3169a3SBruce Richardson 	/*Notify the backend
14256c3169a3SBruce Richardson 	 *Otherwise the tap backend might already stop its queue due to fullness.
14266c3169a3SBruce Richardson 	 *vhost backend will have no chance to be waked up
14276c3169a3SBruce Richardson 	 */
14286c3169a3SBruce Richardson 	nb_queues = dev->data->nb_rx_queues;
14296c3169a3SBruce Richardson 	if (nb_queues > 1) {
14306c3169a3SBruce Richardson 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
14316c3169a3SBruce Richardson 			return -EINVAL;
14326c3169a3SBruce Richardson 	}
14336c3169a3SBruce Richardson 
14346c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
14356c3169a3SBruce Richardson 
14366c3169a3SBruce Richardson 	for (i = 0; i < nb_queues; i++)
14376c3169a3SBruce Richardson 		virtqueue_notify(dev->data->rx_queues[i]);
14386c3169a3SBruce Richardson 
14396c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
14406c3169a3SBruce Richardson 
14416c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++)
14426c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
14436c3169a3SBruce Richardson 
14446c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++)
14456c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
14466c3169a3SBruce Richardson 
14476c3169a3SBruce Richardson 	return 0;
14486c3169a3SBruce Richardson }
14496c3169a3SBruce Richardson 
14506c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
14516c3169a3SBruce Richardson {
14526c3169a3SBruce Richardson 	struct rte_mbuf *buf;
14536c3169a3SBruce Richardson 	int i, mbuf_num = 0;
14546c3169a3SBruce Richardson 
14556c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
14566c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
14576c3169a3SBruce Richardson 			     "Before freeing rxq[%d] used and unused buf", i);
14586c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
14596c3169a3SBruce Richardson 
1460abf4c84bSBernard Iremonger 		PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p",
1461abf4c84bSBernard Iremonger 				i, dev->data->rx_queues[i]);
14626c3169a3SBruce Richardson 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
14636c3169a3SBruce Richardson 					dev->data->rx_queues[i])) != NULL) {
14646c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
14656c3169a3SBruce Richardson 			mbuf_num++;
14666c3169a3SBruce Richardson 		}
14676c3169a3SBruce Richardson 
14686c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
14696c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
14706c3169a3SBruce Richardson 			     "After freeing rxq[%d] used and unused buf", i);
14716c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
14726c3169a3SBruce Richardson 	}
14736c3169a3SBruce Richardson 
14746c3169a3SBruce Richardson 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
14756c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
14766c3169a3SBruce Richardson 			     "Before freeing txq[%d] used and unused bufs",
14776c3169a3SBruce Richardson 			     i);
14786c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
14796c3169a3SBruce Richardson 
14806c3169a3SBruce Richardson 		mbuf_num = 0;
14816c3169a3SBruce Richardson 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
14826c3169a3SBruce Richardson 					dev->data->tx_queues[i])) != NULL) {
14836c3169a3SBruce Richardson 			rte_pktmbuf_free(buf);
14846c3169a3SBruce Richardson 
14856c3169a3SBruce Richardson 			mbuf_num++;
14866c3169a3SBruce Richardson 		}
14876c3169a3SBruce Richardson 
14886c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
14896c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG,
14906c3169a3SBruce Richardson 			     "After freeing txq[%d] used and unused buf", i);
14916c3169a3SBruce Richardson 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
14926c3169a3SBruce Richardson 	}
14936c3169a3SBruce Richardson }
14946c3169a3SBruce Richardson 
14956c3169a3SBruce Richardson /*
14966c3169a3SBruce Richardson  * Stop device: disable interrupt and mark link down
14976c3169a3SBruce Richardson  */
14986c3169a3SBruce Richardson static void
14996c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev)
15006c3169a3SBruce Richardson {
15016c3169a3SBruce Richardson 	struct rte_eth_link link;
15026c3169a3SBruce Richardson 
15036c3169a3SBruce Richardson 	PMD_INIT_LOG(DEBUG, "stop");
15046c3169a3SBruce Richardson 
15056c3169a3SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc)
15066c3169a3SBruce Richardson 		rte_intr_disable(&dev->pci_dev->intr_handle);
15076c3169a3SBruce Richardson 
15086c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15096c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15106c3169a3SBruce Richardson }
15116c3169a3SBruce Richardson 
15126c3169a3SBruce Richardson static int
15136c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
15146c3169a3SBruce Richardson {
15156c3169a3SBruce Richardson 	struct rte_eth_link link, old;
15166c3169a3SBruce Richardson 	uint16_t status;
15176c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
15186c3169a3SBruce Richardson 	memset(&link, 0, sizeof(link));
15196c3169a3SBruce Richardson 	virtio_dev_atomic_read_link_status(dev, &link);
15206c3169a3SBruce Richardson 	old = link;
15216c3169a3SBruce Richardson 	link.link_duplex = FULL_DUPLEX;
15226c3169a3SBruce Richardson 	link.link_speed  = SPEED_10G;
15236c3169a3SBruce Richardson 
15246c3169a3SBruce Richardson 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
15256c3169a3SBruce Richardson 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
15266c3169a3SBruce Richardson 		vtpci_read_dev_config(hw,
15276c3169a3SBruce Richardson 				offsetof(struct virtio_net_config, status),
15286c3169a3SBruce Richardson 				&status, sizeof(status));
15296c3169a3SBruce Richardson 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
15306c3169a3SBruce Richardson 			link.link_status = 0;
15316c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is down",
15326c3169a3SBruce Richardson 				     dev->data->port_id);
15336c3169a3SBruce Richardson 		} else {
15346c3169a3SBruce Richardson 			link.link_status = 1;
15356c3169a3SBruce Richardson 			PMD_INIT_LOG(DEBUG, "Port %d is up",
15366c3169a3SBruce Richardson 				     dev->data->port_id);
15376c3169a3SBruce Richardson 		}
15386c3169a3SBruce Richardson 	} else {
15396c3169a3SBruce Richardson 		link.link_status = 1;   /* Link up */
15406c3169a3SBruce Richardson 	}
15416c3169a3SBruce Richardson 	virtio_dev_atomic_write_link_status(dev, &link);
15426c3169a3SBruce Richardson 
15436c3169a3SBruce Richardson 	return (old.link_status == link.link_status) ? -1 : 0;
15446c3169a3SBruce Richardson }
15456c3169a3SBruce Richardson 
15466c3169a3SBruce Richardson static void
15476c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
15486c3169a3SBruce Richardson {
15496c3169a3SBruce Richardson 	struct virtio_hw *hw = dev->data->dev_private;
15506c3169a3SBruce Richardson 
15516c3169a3SBruce Richardson 	dev_info->driver_name = dev->driver->pci_drv.name;
15526c3169a3SBruce Richardson 	dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
15536c3169a3SBruce Richardson 	dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
15546c3169a3SBruce Richardson 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
15556c3169a3SBruce Richardson 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
15566c3169a3SBruce Richardson 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
15576c3169a3SBruce Richardson 	dev_info->default_txconf = (struct rte_eth_txconf) {
15586c3169a3SBruce Richardson 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
15596c3169a3SBruce Richardson 	};
15606c3169a3SBruce Richardson }
15616c3169a3SBruce Richardson 
15626c3169a3SBruce Richardson /*
15636c3169a3SBruce Richardson  * It enables testpmd to collect per queue stats.
15646c3169a3SBruce Richardson  */
15656c3169a3SBruce Richardson static int
15666c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
15676c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
15686c3169a3SBruce Richardson __rte_unused uint8_t is_rx)
15696c3169a3SBruce Richardson {
15706c3169a3SBruce Richardson 	return 0;
15716c3169a3SBruce Richardson }
15726c3169a3SBruce Richardson 
15736c3169a3SBruce Richardson static struct rte_driver rte_virtio_driver = {
15746c3169a3SBruce Richardson 	.type = PMD_PDEV,
15756c3169a3SBruce Richardson 	.init = rte_virtio_pmd_init,
15766c3169a3SBruce Richardson };
15776c3169a3SBruce Richardson 
15786c3169a3SBruce Richardson PMD_REGISTER_DRIVER(rte_virtio_driver);
1579