xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision 92ebda07ee58cf6966305ba03b50b81debfb2d98)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #ifdef RTE_EXEC_ENV_LINUXAPP
40 #include <dirent.h>
41 #include <fcntl.h>
42 #endif
43 
44 #include <rte_ethdev.h>
45 #include <rte_memcpy.h>
46 #include <rte_string_fns.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_pci.h>
52 #include <rte_ether.h>
53 #include <rte_common.h>
54 
55 #include <rte_memory.h>
56 #include <rte_eal.h>
57 #include <rte_dev.h>
58 
59 #include "virtio_ethdev.h"
60 #include "virtio_pci.h"
61 #include "virtio_logs.h"
62 #include "virtqueue.h"
63 
64 
65 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
66 static int  virtio_dev_configure(struct rte_eth_dev *dev);
67 static int  virtio_dev_start(struct rte_eth_dev *dev);
68 static void virtio_dev_stop(struct rte_eth_dev *dev);
69 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
70 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
71 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
72 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
73 static void virtio_dev_info_get(struct rte_eth_dev *dev,
74 				struct rte_eth_dev_info *dev_info);
75 static int virtio_dev_link_update(struct rte_eth_dev *dev,
76 	__rte_unused int wait_to_complete);
77 
78 static void virtio_set_hwaddr(struct virtio_hw *hw);
79 static void virtio_get_hwaddr(struct virtio_hw *hw);
80 
81 static void virtio_dev_rx_queue_release(__rte_unused void *rxq);
82 static void virtio_dev_tx_queue_release(__rte_unused void *txq);
83 
84 static void virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
88 				uint16_t vlan_id, int on);
89 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
90 				struct ether_addr *mac_addr,
91 				uint32_t index, uint32_t vmdq __rte_unused);
92 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
93 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94 				struct ether_addr *mac_addr);
95 
96 static int virtio_dev_queue_stats_mapping_set(
97 	__rte_unused struct rte_eth_dev *eth_dev,
98 	__rte_unused uint16_t queue_id,
99 	__rte_unused uint8_t stat_idx,
100 	__rte_unused uint8_t is_rx);
101 
102 /*
103  * The set of PCI devices this driver supports
104  */
105 static const struct rte_pci_id pci_id_virtio_map[] = {
106 
107 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
108 #include "rte_pci_dev_ids.h"
109 
110 { .vendor_id = 0, /* sentinel */ },
111 };
112 
113 static int
114 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
115 		int *dlen, int pkt_num)
116 {
117 	uint32_t head, i;
118 	int k, sum = 0;
119 	virtio_net_ctrl_ack status = ~0;
120 	struct virtio_pmd_ctrl result;
121 
122 	ctrl->status = status;
123 
124 	if (!(vq && vq->hw->cvq)) {
125 		PMD_INIT_LOG(ERR,
126 			     "%s(): Control queue is not supported.",
127 			     __func__);
128 		return -1;
129 	}
130 	head = vq->vq_desc_head_idx;
131 
132 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
133 		"vq->hw->cvq = %p vq = %p",
134 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
135 
136 	if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
137 		return -1;
138 
139 	memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
140 		sizeof(struct virtio_pmd_ctrl));
141 
142 	/*
143 	 * Format is enforced in qemu code:
144 	 * One TX packet for header;
145 	 * At least one TX packet per argument;
146 	 * One RX packet for ACK.
147 	 */
148 	vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
149 	vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
150 	vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
151 	vq->vq_free_cnt--;
152 	i = vq->vq_ring.desc[head].next;
153 
154 	for (k = 0; k < pkt_num; k++) {
155 		vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
156 		vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
157 			+ sizeof(struct virtio_net_ctrl_hdr)
158 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
159 		vq->vq_ring.desc[i].len = dlen[k];
160 		sum += dlen[k];
161 		vq->vq_free_cnt--;
162 		i = vq->vq_ring.desc[i].next;
163 	}
164 
165 	vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
166 	vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
167 			+ sizeof(struct virtio_net_ctrl_hdr);
168 	vq->vq_ring.desc[i].len = sizeof(ctrl->status);
169 	vq->vq_free_cnt--;
170 
171 	vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
172 
173 	vq_update_avail_ring(vq, head);
174 	vq_update_avail_idx(vq);
175 
176 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
177 
178 	virtqueue_notify(vq);
179 
180 	rte_rmb();
181 	while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
182 		rte_rmb();
183 		usleep(100);
184 	}
185 
186 	while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
187 		uint32_t idx, desc_idx, used_idx;
188 		struct vring_used_elem *uep;
189 
190 		used_idx = (uint32_t)(vq->vq_used_cons_idx
191 				& (vq->vq_nentries - 1));
192 		uep = &vq->vq_ring.used->ring[used_idx];
193 		idx = (uint32_t) uep->id;
194 		desc_idx = idx;
195 
196 		while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
197 			desc_idx = vq->vq_ring.desc[desc_idx].next;
198 			vq->vq_free_cnt++;
199 		}
200 
201 		vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
202 		vq->vq_desc_head_idx = idx;
203 
204 		vq->vq_used_cons_idx++;
205 		vq->vq_free_cnt++;
206 	}
207 
208 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
209 			vq->vq_free_cnt, vq->vq_desc_head_idx);
210 
211 	memcpy(&result, vq->virtio_net_hdr_mz->addr,
212 			sizeof(struct virtio_pmd_ctrl));
213 
214 	return result.status;
215 }
216 
217 static int
218 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
219 {
220 	struct virtio_hw *hw = dev->data->dev_private;
221 	struct virtio_pmd_ctrl ctrl;
222 	int dlen[1];
223 	int ret;
224 
225 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
226 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
227 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
228 
229 	dlen[0] = sizeof(uint16_t);
230 
231 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
232 	if (ret) {
233 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
234 			  "failed, this is too late now...");
235 		return -EINVAL;
236 	}
237 
238 	return 0;
239 }
240 
241 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
242 			int queue_type,
243 			uint16_t queue_idx,
244 			uint16_t  vtpci_queue_idx,
245 			uint16_t nb_desc,
246 			unsigned int socket_id,
247 			struct virtqueue **pvq)
248 {
249 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
250 	const struct rte_memzone *mz;
251 	uint16_t vq_size;
252 	int size;
253 	struct virtio_hw *hw = dev->data->dev_private;
254 	struct virtqueue  *vq = NULL;
255 
256 	/* Write the virtqueue index to the Queue Select Field */
257 	VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
258 	PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
259 
260 	/*
261 	 * Read the virtqueue size from the Queue Size field
262 	 * Always power of 2 and if 0 virtqueue does not exist
263 	 */
264 	vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
265 	PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
266 	if (nb_desc == 0)
267 		nb_desc = vq_size;
268 	if (vq_size == 0) {
269 		PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
270 		return -EINVAL;
271 	}
272 
273 	if (!rte_is_power_of_2(vq_size)) {
274 		PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
275 		return -EINVAL;
276 	}
277 
278 	if (nb_desc < vq_size) {
279 		if (!rte_is_power_of_2(nb_desc)) {
280 			PMD_INIT_LOG(ERR,
281 				     "nb_desc(%u) size is not powerof 2",
282 				     nb_desc);
283 			return -EINVAL;
284 		}
285 		vq_size = nb_desc;
286 	}
287 
288 	if (queue_type == VTNET_RQ) {
289 		snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
290 			dev->data->port_id, queue_idx);
291 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
292 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
293 	} else if (queue_type == VTNET_TQ) {
294 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
295 			dev->data->port_id, queue_idx);
296 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
297 			vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
298 	} else if (queue_type == VTNET_CQ) {
299 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
300 			dev->data->port_id);
301 		vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
302 			vq_size * sizeof(struct vq_desc_extra),
303 			RTE_CACHE_LINE_SIZE);
304 	}
305 	if (vq == NULL) {
306 		PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
307 		return (-ENOMEM);
308 	}
309 
310 	vq->hw = hw;
311 	vq->port_id = dev->data->port_id;
312 	vq->queue_id = queue_idx;
313 	vq->vq_queue_index = vtpci_queue_idx;
314 	vq->vq_nentries = vq_size;
315 	vq->vq_free_cnt = vq_size;
316 
317 	/*
318 	 * Reserve a memzone for vring elements
319 	 */
320 	size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
321 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
322 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
323 
324 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
325 		socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
326 	if (mz == NULL) {
327 		rte_free(vq);
328 		return -ENOMEM;
329 	}
330 
331 	/*
332 	 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
333 	 * and only accepts 32 bit page frame number.
334 	 * Check if the allocated physical memory exceeds 16TB.
335 	 */
336 	if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
337 		PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
338 		rte_free(vq);
339 		return -ENOMEM;
340 	}
341 
342 	memset(mz->addr, 0, sizeof(mz->len));
343 	vq->mz = mz;
344 	vq->vq_ring_mem = mz->phys_addr;
345 	vq->vq_ring_virt_mem = mz->addr;
346 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64, (uint64_t)mz->phys_addr);
347 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
348 	vq->virtio_net_hdr_mz  = NULL;
349 	vq->virtio_net_hdr_mem = 0;
350 
351 	if (queue_type == VTNET_TQ) {
352 		/*
353 		 * For each xmit packet, allocate a virtio_net_hdr
354 		 */
355 		snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
356 			dev->data->port_id, queue_idx);
357 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
358 			vq_size * hw->vtnet_hdr_size,
359 			socket_id, 0, RTE_CACHE_LINE_SIZE);
360 		if (vq->virtio_net_hdr_mz == NULL) {
361 			rte_free(vq);
362 			return -ENOMEM;
363 		}
364 		vq->virtio_net_hdr_mem =
365 			vq->virtio_net_hdr_mz->phys_addr;
366 		memset(vq->virtio_net_hdr_mz->addr, 0,
367 			vq_size * hw->vtnet_hdr_size);
368 	} else if (queue_type == VTNET_CQ) {
369 		/* Allocate a page for control vq command, data and status */
370 		snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
371 			dev->data->port_id);
372 		vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
373 			PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
374 		if (vq->virtio_net_hdr_mz == NULL) {
375 			rte_free(vq);
376 			return -ENOMEM;
377 		}
378 		vq->virtio_net_hdr_mem =
379 			vq->virtio_net_hdr_mz->phys_addr;
380 		memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
381 	}
382 
383 	/*
384 	 * Set guest physical address of the virtqueue
385 	 * in VIRTIO_PCI_QUEUE_PFN config register of device
386 	 */
387 	VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
388 			mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
389 	*pvq = vq;
390 	return 0;
391 }
392 
393 static int
394 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
395 		uint32_t socket_id)
396 {
397 	struct virtqueue *vq;
398 	uint16_t nb_desc = 0;
399 	int ret;
400 	struct virtio_hw *hw = dev->data->dev_private;
401 
402 	PMD_INIT_FUNC_TRACE();
403 	ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
404 			vtpci_queue_idx, nb_desc, socket_id, &vq);
405 	if (ret < 0) {
406 		PMD_INIT_LOG(ERR, "control vq initialization failed");
407 		return ret;
408 	}
409 
410 	hw->cvq = vq;
411 	return 0;
412 }
413 
414 static void
415 virtio_dev_close(struct rte_eth_dev *dev)
416 {
417 	struct virtio_hw *hw = dev->data->dev_private;
418 	struct rte_pci_device *pci_dev = dev->pci_dev;
419 
420 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
421 
422 	/* reset the NIC */
423 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
424 		vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
425 	vtpci_reset(hw);
426 	hw->started = 0;
427 	virtio_dev_free_mbufs(dev);
428 }
429 
430 static void
431 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
432 {
433 	struct virtio_hw *hw = dev->data->dev_private;
434 	struct virtio_pmd_ctrl ctrl;
435 	int dlen[1];
436 	int ret;
437 
438 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
439 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
440 		return;
441 	}
442 
443 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
444 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
445 	ctrl.data[0] = 1;
446 	dlen[0] = 1;
447 
448 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
449 	if (ret)
450 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
451 }
452 
453 static void
454 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
455 {
456 	struct virtio_hw *hw = dev->data->dev_private;
457 	struct virtio_pmd_ctrl ctrl;
458 	int dlen[1];
459 	int ret;
460 
461 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
462 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
463 		return;
464 	}
465 
466 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
467 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
468 	ctrl.data[0] = 0;
469 	dlen[0] = 1;
470 
471 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
472 	if (ret)
473 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
474 }
475 
476 static void
477 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
478 {
479 	struct virtio_hw *hw = dev->data->dev_private;
480 	struct virtio_pmd_ctrl ctrl;
481 	int dlen[1];
482 	int ret;
483 
484 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
485 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
486 		return;
487 	}
488 
489 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
490 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
491 	ctrl.data[0] = 1;
492 	dlen[0] = 1;
493 
494 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
495 	if (ret)
496 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
497 }
498 
499 static void
500 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
501 {
502 	struct virtio_hw *hw = dev->data->dev_private;
503 	struct virtio_pmd_ctrl ctrl;
504 	int dlen[1];
505 	int ret;
506 
507 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
508 		PMD_INIT_LOG(INFO, "host does not support rx control\n");
509 		return;
510 	}
511 
512 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
513 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
514 	ctrl.data[0] = 0;
515 	dlen[0] = 1;
516 
517 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
518 	if (ret)
519 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
520 }
521 
522 /*
523  * dev_ops for virtio, bare necessities for basic operation
524  */
525 static const struct eth_dev_ops virtio_eth_dev_ops = {
526 	.dev_configure           = virtio_dev_configure,
527 	.dev_start               = virtio_dev_start,
528 	.dev_stop                = virtio_dev_stop,
529 	.dev_close               = virtio_dev_close,
530 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
531 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
532 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
533 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
534 
535 	.dev_infos_get           = virtio_dev_info_get,
536 	.stats_get               = virtio_dev_stats_get,
537 	.stats_reset             = virtio_dev_stats_reset,
538 	.link_update             = virtio_dev_link_update,
539 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
540 	/* meaningfull only to multiple queue */
541 	.rx_queue_release        = virtio_dev_rx_queue_release,
542 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
543 	/* meaningfull only to multiple queue */
544 	.tx_queue_release        = virtio_dev_tx_queue_release,
545 	/* collect stats per queue */
546 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
547 	.vlan_filter_set         = virtio_vlan_filter_set,
548 	.mac_addr_add            = virtio_mac_addr_add,
549 	.mac_addr_remove         = virtio_mac_addr_remove,
550 	.mac_addr_set            = virtio_mac_addr_set,
551 };
552 
553 static inline int
554 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
555 				struct rte_eth_link *link)
556 {
557 	struct rte_eth_link *dst = link;
558 	struct rte_eth_link *src = &(dev->data->dev_link);
559 
560 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
561 			*(uint64_t *)src) == 0)
562 		return -1;
563 
564 	return 0;
565 }
566 
567 /**
568  * Atomically writes the link status information into global
569  * structure rte_eth_dev.
570  *
571  * @param dev
572  *   - Pointer to the structure rte_eth_dev to read from.
573  *   - Pointer to the buffer to be saved with the link status.
574  *
575  * @return
576  *   - On success, zero.
577  *   - On failure, negative value.
578  */
579 static inline int
580 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
581 		struct rte_eth_link *link)
582 {
583 	struct rte_eth_link *dst = &(dev->data->dev_link);
584 	struct rte_eth_link *src = link;
585 
586 	if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
587 					*(uint64_t *)src) == 0)
588 		return -1;
589 
590 	return 0;
591 }
592 
593 static void
594 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
595 {
596 	unsigned i;
597 
598 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
599 		const struct virtqueue *txvq = dev->data->tx_queues[i];
600 		if (txvq == NULL)
601 			continue;
602 
603 		stats->opackets += txvq->packets;
604 		stats->obytes += txvq->bytes;
605 		stats->oerrors += txvq->errors;
606 
607 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
608 			stats->q_opackets[i] = txvq->packets;
609 			stats->q_obytes[i] = txvq->bytes;
610 		}
611 	}
612 
613 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
614 		const struct virtqueue *rxvq = dev->data->rx_queues[i];
615 		if (rxvq == NULL)
616 			continue;
617 
618 		stats->ipackets += rxvq->packets;
619 		stats->ibytes += rxvq->bytes;
620 		stats->ierrors += rxvq->errors;
621 
622 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
623 			stats->q_ipackets[i] = rxvq->packets;
624 			stats->q_ibytes[i] = rxvq->bytes;
625 		}
626 	}
627 
628 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
629 }
630 
631 static void
632 virtio_dev_stats_reset(struct rte_eth_dev *dev)
633 {
634 	unsigned int i;
635 
636 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
637 		struct virtqueue *txvq = dev->data->tx_queues[i];
638 		if (txvq == NULL)
639 			continue;
640 
641 		txvq->packets = 0;
642 		txvq->bytes = 0;
643 		txvq->errors = 0;
644 	}
645 
646 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
647 		struct virtqueue *rxvq = dev->data->rx_queues[i];
648 		if (rxvq == NULL)
649 			continue;
650 
651 		rxvq->packets = 0;
652 		rxvq->bytes = 0;
653 		rxvq->errors = 0;
654 	}
655 
656 	dev->data->rx_mbuf_alloc_failed = 0;
657 }
658 
659 static void
660 virtio_set_hwaddr(struct virtio_hw *hw)
661 {
662 	vtpci_write_dev_config(hw,
663 			offsetof(struct virtio_net_config, mac),
664 			&hw->mac_addr, ETHER_ADDR_LEN);
665 }
666 
667 static void
668 virtio_get_hwaddr(struct virtio_hw *hw)
669 {
670 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
671 		vtpci_read_dev_config(hw,
672 			offsetof(struct virtio_net_config, mac),
673 			&hw->mac_addr, ETHER_ADDR_LEN);
674 	} else {
675 		eth_random_addr(&hw->mac_addr[0]);
676 		virtio_set_hwaddr(hw);
677 	}
678 }
679 
680 static void
681 virtio_mac_table_set(struct virtio_hw *hw,
682 		     const struct virtio_net_ctrl_mac *uc,
683 		     const struct virtio_net_ctrl_mac *mc)
684 {
685 	struct virtio_pmd_ctrl ctrl;
686 	int err, len[2];
687 
688 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
689 		PMD_DRV_LOG(INFO, "host does not support mac table\n");
690 		return;
691 	}
692 
693 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
694 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
695 
696 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
697 	memcpy(ctrl.data, uc, len[0]);
698 
699 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
700 	memcpy(ctrl.data + len[0], mc, len[1]);
701 
702 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
703 	if (err != 0)
704 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
705 }
706 
707 static void
708 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
709 		    uint32_t index, uint32_t vmdq __rte_unused)
710 {
711 	struct virtio_hw *hw = dev->data->dev_private;
712 	const struct ether_addr *addrs = dev->data->mac_addrs;
713 	unsigned int i;
714 	struct virtio_net_ctrl_mac *uc, *mc;
715 
716 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
717 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
718 		return;
719 	}
720 
721 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
722 	uc->entries = 0;
723 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
724 	mc->entries = 0;
725 
726 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
727 		const struct ether_addr *addr
728 			= (i == index) ? mac_addr : addrs + i;
729 		struct virtio_net_ctrl_mac *tbl
730 			= is_multicast_ether_addr(addr) ? mc : uc;
731 
732 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
733 	}
734 
735 	virtio_mac_table_set(hw, uc, mc);
736 }
737 
738 static void
739 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
740 {
741 	struct virtio_hw *hw = dev->data->dev_private;
742 	struct ether_addr *addrs = dev->data->mac_addrs;
743 	struct virtio_net_ctrl_mac *uc, *mc;
744 	unsigned int i;
745 
746 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
747 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
748 		return;
749 	}
750 
751 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
752 	uc->entries = 0;
753 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
754 	mc->entries = 0;
755 
756 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
757 		struct virtio_net_ctrl_mac *tbl;
758 
759 		if (i == index || is_zero_ether_addr(addrs + i))
760 			continue;
761 
762 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
763 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
764 	}
765 
766 	virtio_mac_table_set(hw, uc, mc);
767 }
768 
769 static void
770 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
771 {
772 	struct virtio_hw *hw = dev->data->dev_private;
773 
774 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
775 
776 	/* Use atomic update if available */
777 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
778 		struct virtio_pmd_ctrl ctrl;
779 		int len = ETHER_ADDR_LEN;
780 
781 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
782 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
783 
784 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
785 		virtio_send_command(hw->cvq, &ctrl, &len, 1);
786 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
787 		virtio_set_hwaddr(hw);
788 }
789 
790 static int
791 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
792 {
793 	struct virtio_hw *hw = dev->data->dev_private;
794 	struct virtio_pmd_ctrl ctrl;
795 	int len;
796 
797 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
798 		return -ENOTSUP;
799 
800 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
801 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
802 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
803 	len = sizeof(vlan_id);
804 
805 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
806 }
807 
808 static void
809 virtio_negotiate_features(struct virtio_hw *hw)
810 {
811 	uint32_t host_features;
812 
813 	/* Prepare guest_features: feature that driver wants to support */
814 	hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
815 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
816 		hw->guest_features);
817 
818 	/* Read device(host) feature bits */
819 	host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
820 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
821 		host_features);
822 
823 	/*
824 	 * Negotiate features: Subset of device feature bits are written back
825 	 * guest feature bits.
826 	 */
827 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
828 	PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
829 		hw->guest_features);
830 }
831 
832 #ifdef RTE_EXEC_ENV_LINUXAPP
833 static int
834 parse_sysfs_value(const char *filename, unsigned long *val)
835 {
836 	FILE *f;
837 	char buf[BUFSIZ];
838 	char *end = NULL;
839 
840 	f = fopen(filename, "r");
841 	if (f == NULL) {
842 		PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
843 			     __func__, filename);
844 		return -1;
845 	}
846 
847 	if (fgets(buf, sizeof(buf), f) == NULL) {
848 		PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
849 			     __func__, filename);
850 		fclose(f);
851 		return -1;
852 	}
853 	*val = strtoul(buf, &end, 0);
854 	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
855 		PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
856 			     __func__, filename);
857 		fclose(f);
858 		return -1;
859 	}
860 	fclose(f);
861 	return 0;
862 }
863 
864 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen,
865 			unsigned int *uio_num)
866 {
867 	struct dirent *e;
868 	DIR *dir;
869 	char dirname[PATH_MAX];
870 
871 	/* depending on kernel version, uio can be located in uio/uioX
872 	 * or uio:uioX */
873 	snprintf(dirname, sizeof(dirname),
874 		     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
875 		     loc->domain, loc->bus, loc->devid, loc->function);
876 	dir = opendir(dirname);
877 	if (dir == NULL) {
878 		/* retry with the parent directory */
879 		snprintf(dirname, sizeof(dirname),
880 			     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
881 			     loc->domain, loc->bus, loc->devid, loc->function);
882 		dir = opendir(dirname);
883 
884 		if (dir == NULL) {
885 			PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
886 			return -1;
887 		}
888 	}
889 
890 	/* take the first file starting with "uio" */
891 	while ((e = readdir(dir)) != NULL) {
892 		/* format could be uio%d ...*/
893 		int shortprefix_len = sizeof("uio") - 1;
894 		/* ... or uio:uio%d */
895 		int longprefix_len = sizeof("uio:uio") - 1;
896 		char *endptr;
897 
898 		if (strncmp(e->d_name, "uio", 3) != 0)
899 			continue;
900 
901 		/* first try uio%d */
902 		errno = 0;
903 		*uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
904 		if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
905 			snprintf(buf, buflen, "%s/uio%u", dirname, *uio_num);
906 			break;
907 		}
908 
909 		/* then try uio:uio%d */
910 		errno = 0;
911 		*uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
912 		if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
913 			snprintf(buf, buflen, "%s/uio:uio%u", dirname,
914 				     *uio_num);
915 			break;
916 		}
917 	}
918 	closedir(dir);
919 
920 	/* No uio resource found */
921 	if (e == NULL) {
922 		PMD_INIT_LOG(ERR, "Could not find uio resource");
923 		return -1;
924 	}
925 
926 	return 0;
927 }
928 
929 static int
930 virtio_has_msix(const struct rte_pci_addr *loc)
931 {
932 	DIR *d;
933 	char dirname[PATH_MAX];
934 
935 	snprintf(dirname, sizeof(dirname),
936 		     SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
937 		     loc->domain, loc->bus, loc->devid, loc->function);
938 
939 	d = opendir(dirname);
940 	if (d)
941 		closedir(d);
942 
943 	return (d != NULL);
944 }
945 
946 /* Extract I/O port numbers from sysfs */
947 static int virtio_resource_init_by_uio(struct rte_pci_device *pci_dev)
948 {
949 	char dirname[PATH_MAX];
950 	char filename[PATH_MAX];
951 	unsigned long start, size;
952 	unsigned int uio_num;
953 
954 	if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname), &uio_num) < 0)
955 		return -1;
956 
957 	/* get portio size */
958 	snprintf(filename, sizeof(filename),
959 		     "%s/portio/port0/size", dirname);
960 	if (parse_sysfs_value(filename, &size) < 0) {
961 		PMD_INIT_LOG(ERR, "%s(): cannot parse size",
962 			     __func__);
963 		return -1;
964 	}
965 
966 	/* get portio start */
967 	snprintf(filename, sizeof(filename),
968 		 "%s/portio/port0/start", dirname);
969 	if (parse_sysfs_value(filename, &start) < 0) {
970 		PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
971 			     __func__);
972 		return -1;
973 	}
974 	pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
975 	pci_dev->mem_resource[0].len =  (uint64_t)size;
976 	PMD_INIT_LOG(DEBUG,
977 		     "PCI Port IO found start=0x%lx with size=0x%lx",
978 		     start, size);
979 
980 	/* save fd */
981 	memset(dirname, 0, sizeof(dirname));
982 	snprintf(dirname, sizeof(dirname), "/dev/uio%u", uio_num);
983 	pci_dev->intr_handle.fd = open(dirname, O_RDWR);
984 	if (pci_dev->intr_handle.fd < 0) {
985 		PMD_INIT_LOG(ERR, "Cannot open %s: %s\n",
986 			dirname, strerror(errno));
987 		return -1;
988 	}
989 
990 	pci_dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
991 	pci_dev->driver->drv_flags |= RTE_PCI_DRV_INTR_LSC;
992 
993 	return 0;
994 }
995 
996 /* Extract port I/O numbers from proc/ioports */
997 static int virtio_resource_init_by_ioports(struct rte_pci_device *pci_dev)
998 {
999 	uint16_t start, end;
1000 	int size;
1001 	FILE *fp;
1002 	char *line = NULL;
1003 	char pci_id[16];
1004 	int found = 0;
1005 	size_t linesz;
1006 
1007 	snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
1008 		 pci_dev->addr.domain,
1009 		 pci_dev->addr.bus,
1010 		 pci_dev->addr.devid,
1011 		 pci_dev->addr.function);
1012 
1013 	fp = fopen("/proc/ioports", "r");
1014 	if (fp == NULL) {
1015 		PMD_INIT_LOG(ERR, "%s(): can't open ioports", __func__);
1016 		return -1;
1017 	}
1018 
1019 	while (getdelim(&line, &linesz, '\n', fp) > 0) {
1020 		char *ptr = line;
1021 		char *left;
1022 		int n;
1023 
1024 		n = strcspn(ptr, ":");
1025 		ptr[n] = 0;
1026 		left = &ptr[n+1];
1027 
1028 		while (*left && isspace(*left))
1029 			left++;
1030 
1031 		if (!strncmp(left, pci_id, strlen(pci_id))) {
1032 			found = 1;
1033 
1034 			while (*ptr && isspace(*ptr))
1035 				ptr++;
1036 
1037 			sscanf(ptr, "%04hx-%04hx", &start, &end);
1038 			size = end - start + 1;
1039 
1040 			break;
1041 		}
1042 	}
1043 
1044 	free(line);
1045 	fclose(fp);
1046 
1047 	if (!found)
1048 		return -1;
1049 
1050 	pci_dev->mem_resource[0].addr = (void *)(uintptr_t)(uint32_t)start;
1051 	pci_dev->mem_resource[0].len =  (uint64_t)size;
1052 	PMD_INIT_LOG(DEBUG,
1053 		"PCI Port IO found start=0x%x with size=0x%x",
1054 		start, size);
1055 
1056 	/* can't support lsc interrupt without uio */
1057 	pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1058 
1059 	return 0;
1060 }
1061 
1062 /* Extract I/O port numbers from sysfs */
1063 static int virtio_resource_init(struct rte_pci_device *pci_dev)
1064 {
1065 	if (virtio_resource_init_by_uio(pci_dev) == 0)
1066 		return 0;
1067 	else
1068 		return virtio_resource_init_by_ioports(pci_dev);
1069 }
1070 
1071 #else
1072 static int
1073 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
1074 {
1075 	/* nic_uio does not enable interrupts, return 0 (false). */
1076 	return 0;
1077 }
1078 
1079 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
1080 {
1081 	/* no setup required */
1082 	return 0;
1083 }
1084 #endif
1085 
1086 /*
1087  * Process Virtio Config changed interrupt and call the callback
1088  * if link state changed.
1089  */
1090 static void
1091 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1092 			 void *param)
1093 {
1094 	struct rte_eth_dev *dev = param;
1095 	struct virtio_hw *hw = dev->data->dev_private;
1096 	uint8_t isr;
1097 
1098 	/* Read interrupt status which clears interrupt */
1099 	isr = vtpci_isr(hw);
1100 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1101 
1102 	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1103 		PMD_DRV_LOG(ERR, "interrupt enable failed");
1104 
1105 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
1106 		if (virtio_dev_link_update(dev, 0) == 0)
1107 			_rte_eth_dev_callback_process(dev,
1108 						      RTE_ETH_EVENT_INTR_LSC);
1109 	}
1110 
1111 }
1112 
1113 static void
1114 rx_func_get(struct rte_eth_dev *eth_dev)
1115 {
1116 	struct virtio_hw *hw = eth_dev->data->dev_private;
1117 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1118 		eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1119 	else
1120 		eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1121 }
1122 
1123 /*
1124  * This function is based on probe() function in virtio_pci.c
1125  * It returns 0 on success.
1126  */
1127 static int
1128 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1129 {
1130 	struct virtio_hw *hw = eth_dev->data->dev_private;
1131 	struct virtio_net_config *config;
1132 	struct virtio_net_config local_config;
1133 	uint32_t offset_conf = sizeof(config->mac);
1134 	struct rte_pci_device *pci_dev;
1135 
1136 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
1137 
1138 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1139 	eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1140 
1141 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1142 		rx_func_get(eth_dev);
1143 		return 0;
1144 	}
1145 
1146 	/* Allocate memory for storing MAC addresses */
1147 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
1148 	if (eth_dev->data->mac_addrs == NULL) {
1149 		PMD_INIT_LOG(ERR,
1150 			"Failed to allocate %d bytes needed to store MAC addresses",
1151 			ETHER_ADDR_LEN);
1152 		return -ENOMEM;
1153 	}
1154 
1155 	pci_dev = eth_dev->pci_dev;
1156 	if (virtio_resource_init(pci_dev) < 0)
1157 		return -1;
1158 
1159 	hw->use_msix = virtio_has_msix(&pci_dev->addr);
1160 	hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
1161 
1162 	/* Reset the device although not necessary at startup */
1163 	vtpci_reset(hw);
1164 
1165 	/* Tell the host we've noticed this device. */
1166 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1167 
1168 	/* Tell the host we've known how to drive the device. */
1169 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1170 	virtio_negotiate_features(hw);
1171 
1172 	rx_func_get(eth_dev);
1173 
1174 	/* Setting up rx_header size for the device */
1175 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1176 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1177 	else
1178 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1179 
1180 	/* Copy the permanent MAC address to: virtio_hw */
1181 	virtio_get_hwaddr(hw);
1182 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
1183 			&eth_dev->data->mac_addrs[0]);
1184 	PMD_INIT_LOG(DEBUG,
1185 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1186 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1187 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1188 
1189 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1190 		config = &local_config;
1191 
1192 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1193 			offset_conf += sizeof(config->status);
1194 		} else {
1195 			PMD_INIT_LOG(DEBUG,
1196 				     "VIRTIO_NET_F_STATUS is not supported");
1197 			config->status = 0;
1198 		}
1199 
1200 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1201 			offset_conf += sizeof(config->max_virtqueue_pairs);
1202 		} else {
1203 			PMD_INIT_LOG(DEBUG,
1204 				     "VIRTIO_NET_F_MQ is not supported");
1205 			config->max_virtqueue_pairs = 1;
1206 		}
1207 
1208 		vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
1209 
1210 		hw->max_rx_queues =
1211 			(VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1212 			VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1213 		hw->max_tx_queues =
1214 			(VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1215 			VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1216 
1217 		virtio_dev_cq_queue_setup(eth_dev,
1218 					config->max_virtqueue_pairs * 2,
1219 					SOCKET_ID_ANY);
1220 
1221 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1222 				config->max_virtqueue_pairs);
1223 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1224 		PMD_INIT_LOG(DEBUG,
1225 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1226 				config->mac[0], config->mac[1],
1227 				config->mac[2], config->mac[3],
1228 				config->mac[4], config->mac[5]);
1229 	} else {
1230 		hw->max_rx_queues = 1;
1231 		hw->max_tx_queues = 1;
1232 	}
1233 
1234 	eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1235 	eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1236 
1237 	PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
1238 			hw->max_rx_queues, hw->max_tx_queues);
1239 	PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1240 			eth_dev->data->port_id, pci_dev->id.vendor_id,
1241 			pci_dev->id.device_id);
1242 
1243 	/* Setup interrupt callback  */
1244 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1245 		rte_intr_callback_register(&pci_dev->intr_handle,
1246 				   virtio_interrupt_handler, eth_dev);
1247 
1248 	virtio_dev_cq_start(eth_dev);
1249 
1250 	return 0;
1251 }
1252 
1253 static struct eth_driver rte_virtio_pmd = {
1254 	.pci_drv = {
1255 		.name = "rte_virtio_pmd",
1256 		.id_table = pci_id_virtio_map,
1257 	},
1258 	.eth_dev_init = eth_virtio_dev_init,
1259 	.dev_private_size = sizeof(struct virtio_hw),
1260 };
1261 
1262 /*
1263  * Driver initialization routine.
1264  * Invoked once at EAL init time.
1265  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1266  * Returns 0 on success.
1267  */
1268 static int
1269 rte_virtio_pmd_init(const char *name __rte_unused,
1270 		    const char *param __rte_unused)
1271 {
1272 	if (rte_eal_iopl_init() != 0) {
1273 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1274 		return -1;
1275 	}
1276 
1277 	rte_eth_driver_register(&rte_virtio_pmd);
1278 	return 0;
1279 }
1280 
1281 /*
1282  * Only 1 queue is supported, no queue release related operation
1283  */
1284 static void
1285 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1286 {
1287 }
1288 
1289 static void
1290 virtio_dev_tx_queue_release(__rte_unused void *txq)
1291 {
1292 }
1293 
1294 /*
1295  * Configure virtio device
1296  * It returns 0 on success.
1297  */
1298 static int
1299 virtio_dev_configure(struct rte_eth_dev *dev)
1300 {
1301 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1302 	struct virtio_hw *hw = dev->data->dev_private;
1303 	struct rte_pci_device *pci_dev = dev->pci_dev;
1304 
1305 	PMD_INIT_LOG(DEBUG, "configure");
1306 
1307 	if (rxmode->hw_ip_checksum) {
1308 		PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1309 		return (-EINVAL);
1310 	}
1311 
1312 	hw->vlan_strip = rxmode->hw_vlan_strip;
1313 
1314 	if (rxmode->hw_vlan_filter
1315 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1316 		PMD_DRV_LOG(NOTICE,
1317 			    "vlan filtering not available on this host");
1318 		return -ENOTSUP;
1319 	}
1320 
1321 	if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1322 		if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1323 			PMD_DRV_LOG(ERR, "failed to set config vector");
1324 			return -EBUSY;
1325 		}
1326 
1327 	return 0;
1328 }
1329 
1330 
1331 static int
1332 virtio_dev_start(struct rte_eth_dev *dev)
1333 {
1334 	uint16_t nb_queues, i;
1335 	struct virtio_hw *hw = dev->data->dev_private;
1336 	struct rte_pci_device *pci_dev = dev->pci_dev;
1337 
1338 	/* check if lsc interrupt feature is enabled */
1339 	if ((dev->data->dev_conf.intr_conf.lsc) &&
1340 		(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1341 		if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1342 			PMD_DRV_LOG(ERR, "link status not supported by host");
1343 			return -ENOTSUP;
1344 		}
1345 
1346 		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1347 			PMD_DRV_LOG(ERR, "interrupt enable failed");
1348 			return -EIO;
1349 		}
1350 	}
1351 
1352 	/* Initialize Link state */
1353 	virtio_dev_link_update(dev, 0);
1354 
1355 	/* On restart after stop do not touch queues */
1356 	if (hw->started)
1357 		return 0;
1358 
1359 	/* Do final configuration before rx/tx engine starts */
1360 	virtio_dev_rxtx_start(dev);
1361 	vtpci_reinit_complete(hw);
1362 
1363 	hw->started = 1;
1364 
1365 	/*Notify the backend
1366 	 *Otherwise the tap backend might already stop its queue due to fullness.
1367 	 *vhost backend will have no chance to be waked up
1368 	 */
1369 	nb_queues = dev->data->nb_rx_queues;
1370 	if (nb_queues > 1) {
1371 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1372 			return -EINVAL;
1373 	}
1374 
1375 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1376 
1377 	for (i = 0; i < nb_queues; i++)
1378 		virtqueue_notify(dev->data->rx_queues[i]);
1379 
1380 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1381 
1382 	for (i = 0; i < dev->data->nb_rx_queues; i++)
1383 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1384 
1385 	for (i = 0; i < dev->data->nb_tx_queues; i++)
1386 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1387 
1388 	return 0;
1389 }
1390 
1391 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1392 {
1393 	struct rte_mbuf *buf;
1394 	int i, mbuf_num = 0;
1395 
1396 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1397 		PMD_INIT_LOG(DEBUG,
1398 			     "Before freeing rxq[%d] used and unused buf", i);
1399 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1400 
1401 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1402 					dev->data->rx_queues[i])) != NULL) {
1403 			rte_pktmbuf_free(buf);
1404 			mbuf_num++;
1405 		}
1406 
1407 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1408 		PMD_INIT_LOG(DEBUG,
1409 			     "After freeing rxq[%d] used and unused buf", i);
1410 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1411 	}
1412 
1413 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1414 		PMD_INIT_LOG(DEBUG,
1415 			     "Before freeing txq[%d] used and unused bufs",
1416 			     i);
1417 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1418 
1419 		mbuf_num = 0;
1420 		while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1421 					dev->data->tx_queues[i])) != NULL) {
1422 			rte_pktmbuf_free(buf);
1423 
1424 			mbuf_num++;
1425 		}
1426 
1427 		PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1428 		PMD_INIT_LOG(DEBUG,
1429 			     "After freeing txq[%d] used and unused buf", i);
1430 		VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1431 	}
1432 }
1433 
1434 /*
1435  * Stop device: disable interrupt and mark link down
1436  */
1437 static void
1438 virtio_dev_stop(struct rte_eth_dev *dev)
1439 {
1440 	struct rte_eth_link link;
1441 
1442 	PMD_INIT_LOG(DEBUG, "stop");
1443 
1444 	if (dev->data->dev_conf.intr_conf.lsc)
1445 		rte_intr_disable(&dev->pci_dev->intr_handle);
1446 
1447 	memset(&link, 0, sizeof(link));
1448 	virtio_dev_atomic_write_link_status(dev, &link);
1449 }
1450 
1451 static int
1452 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1453 {
1454 	struct rte_eth_link link, old;
1455 	uint16_t status;
1456 	struct virtio_hw *hw = dev->data->dev_private;
1457 	memset(&link, 0, sizeof(link));
1458 	virtio_dev_atomic_read_link_status(dev, &link);
1459 	old = link;
1460 	link.link_duplex = FULL_DUPLEX;
1461 	link.link_speed  = SPEED_10G;
1462 
1463 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1464 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
1465 		vtpci_read_dev_config(hw,
1466 				offsetof(struct virtio_net_config, status),
1467 				&status, sizeof(status));
1468 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1469 			link.link_status = 0;
1470 			PMD_INIT_LOG(DEBUG, "Port %d is down",
1471 				     dev->data->port_id);
1472 		} else {
1473 			link.link_status = 1;
1474 			PMD_INIT_LOG(DEBUG, "Port %d is up",
1475 				     dev->data->port_id);
1476 		}
1477 	} else {
1478 		link.link_status = 1;   /* Link up */
1479 	}
1480 	virtio_dev_atomic_write_link_status(dev, &link);
1481 
1482 	return (old.link_status == link.link_status) ? -1 : 0;
1483 }
1484 
1485 static void
1486 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1487 {
1488 	struct virtio_hw *hw = dev->data->dev_private;
1489 
1490 	dev_info->driver_name = dev->driver->pci_drv.name;
1491 	dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1492 	dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1493 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1494 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1495 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1496 	dev_info->default_txconf = (struct rte_eth_txconf) {
1497 		.txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1498 	};
1499 }
1500 
1501 /*
1502  * It enables testpmd to collect per queue stats.
1503  */
1504 static int
1505 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1506 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1507 __rte_unused uint8_t is_rx)
1508 {
1509 	return 0;
1510 }
1511 
1512 static struct rte_driver rte_virtio_driver = {
1513 	.type = PMD_PDEV,
1514 	.init = rte_virtio_pmd_init,
1515 };
1516 
1517 PMD_REGISTER_DRIVER(rte_virtio_driver);
1518