xref: /dpdk/drivers/net/virtio/virtio_ethdev.c (revision a720e6740afcd6d21955d188c6e6a7136fcc7d6c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <unistd.h>
10 
11 #include <rte_ethdev_driver.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_memcpy.h>
14 #include <rte_string_fns.h>
15 #include <rte_memzone.h>
16 #include <rte_malloc.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_pci.h>
19 #include <rte_bus_pci.h>
20 #include <rte_ether.h>
21 #include <rte_ip.h>
22 #include <rte_arp.h>
23 #include <rte_common.h>
24 #include <rte_errno.h>
25 #include <rte_cpuflags.h>
26 
27 #include <rte_memory.h>
28 #include <rte_eal.h>
29 #include <rte_dev.h>
30 #include <rte_cycles.h>
31 #include <rte_kvargs.h>
32 
33 #include "virtio_ethdev.h"
34 #include "virtio_pci.h"
35 #include "virtio_logs.h"
36 #include "virtqueue.h"
37 #include "virtio_rxtx.h"
38 
39 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
40 static int  virtio_dev_configure(struct rte_eth_dev *dev);
41 static int  virtio_dev_start(struct rte_eth_dev *dev);
42 static void virtio_dev_stop(struct rte_eth_dev *dev);
43 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
44 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
45 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
46 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
47 static void virtio_dev_info_get(struct rte_eth_dev *dev,
48 				struct rte_eth_dev_info *dev_info);
49 static int virtio_dev_link_update(struct rte_eth_dev *dev,
50 	int wait_to_complete);
51 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
52 
53 static void virtio_set_hwaddr(struct virtio_hw *hw);
54 static void virtio_get_hwaddr(struct virtio_hw *hw);
55 
56 static int virtio_dev_stats_get(struct rte_eth_dev *dev,
57 				 struct rte_eth_stats *stats);
58 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
59 				 struct rte_eth_xstat *xstats, unsigned n);
60 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
61 				       struct rte_eth_xstat_name *xstats_names,
62 				       unsigned limit);
63 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
64 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
65 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
66 				uint16_t vlan_id, int on);
67 static int virtio_mac_addr_add(struct rte_eth_dev *dev,
68 				struct ether_addr *mac_addr,
69 				uint32_t index, uint32_t vmdq);
70 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
71 static int virtio_mac_addr_set(struct rte_eth_dev *dev,
72 				struct ether_addr *mac_addr);
73 
74 static int virtio_intr_disable(struct rte_eth_dev *dev);
75 
76 static int virtio_dev_queue_stats_mapping_set(
77 	struct rte_eth_dev *eth_dev,
78 	uint16_t queue_id,
79 	uint8_t stat_idx,
80 	uint8_t is_rx);
81 
82 int virtio_logtype_init;
83 int virtio_logtype_driver;
84 
85 static void virtio_notify_peers(struct rte_eth_dev *dev);
86 static void virtio_ack_link_announce(struct rte_eth_dev *dev);
87 
88 /*
89  * The set of PCI devices this driver supports
90  */
91 static const struct rte_pci_id pci_id_virtio_map[] = {
92 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
93 	{ RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
94 	{ .vendor_id = 0, /* sentinel */ },
95 };
96 
97 struct rte_virtio_xstats_name_off {
98 	char name[RTE_ETH_XSTATS_NAME_SIZE];
99 	unsigned offset;
100 };
101 
102 /* [rt]x_qX_ is prepended to the name string here */
103 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
104 	{"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
105 	{"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
106 	{"errors",                 offsetof(struct virtnet_rx, stats.errors)},
107 	{"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
108 	{"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
109 	{"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
110 	{"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
111 	{"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
112 	{"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
113 	{"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
114 	{"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
115 	{"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
116 	{"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
117 };
118 
119 /* [rt]x_qX_ is prepended to the name string here */
120 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
121 	{"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
122 	{"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
123 	{"errors",                 offsetof(struct virtnet_tx, stats.errors)},
124 	{"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
125 	{"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
126 	{"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
127 	{"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
128 	{"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
129 	{"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
130 	{"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
131 	{"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
132 	{"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
133 	{"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
134 };
135 
136 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
137 			    sizeof(rte_virtio_rxq_stat_strings[0]))
138 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
139 			    sizeof(rte_virtio_txq_stat_strings[0]))
140 
141 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
142 
143 static struct virtio_pmd_ctrl *
144 virtio_send_command_packed(struct virtnet_ctl *cvq,
145 			   struct virtio_pmd_ctrl *ctrl,
146 			   int *dlen, int pkt_num)
147 {
148 	struct virtqueue *vq = cvq->vq;
149 	int head;
150 	struct vring_packed_desc *desc = vq->vq_packed.ring.desc;
151 	struct virtio_pmd_ctrl *result;
152 	uint16_t flags;
153 	int sum = 0;
154 	int nb_descs = 0;
155 	int k;
156 
157 	/*
158 	 * Format is enforced in qemu code:
159 	 * One TX packet for header;
160 	 * At least one TX packet per argument;
161 	 * One RX packet for ACK.
162 	 */
163 	head = vq->vq_avail_idx;
164 	flags = vq->vq_packed.cached_flags;
165 	desc[head].addr = cvq->virtio_net_hdr_mem;
166 	desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
167 	vq->vq_free_cnt--;
168 	nb_descs++;
169 	if (++vq->vq_avail_idx >= vq->vq_nentries) {
170 		vq->vq_avail_idx -= vq->vq_nentries;
171 		vq->vq_packed.cached_flags ^=
172 			VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
173 	}
174 
175 	for (k = 0; k < pkt_num; k++) {
176 		desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
177 			+ sizeof(struct virtio_net_ctrl_hdr)
178 			+ sizeof(ctrl->status) + sizeof(uint8_t) * sum;
179 		desc[vq->vq_avail_idx].len = dlen[k];
180 		desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT |
181 			vq->vq_packed.cached_flags;
182 		sum += dlen[k];
183 		vq->vq_free_cnt--;
184 		nb_descs++;
185 		if (++vq->vq_avail_idx >= vq->vq_nentries) {
186 			vq->vq_avail_idx -= vq->vq_nentries;
187 			vq->vq_packed.cached_flags ^=
188 				VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
189 		}
190 	}
191 
192 	desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem
193 		+ sizeof(struct virtio_net_ctrl_hdr);
194 	desc[vq->vq_avail_idx].len = sizeof(ctrl->status);
195 	desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE |
196 		vq->vq_packed.cached_flags;
197 	vq->vq_free_cnt--;
198 	nb_descs++;
199 	if (++vq->vq_avail_idx >= vq->vq_nentries) {
200 		vq->vq_avail_idx -= vq->vq_nentries;
201 		vq->vq_packed.cached_flags ^=
202 			VRING_DESC_F_AVAIL(1) | VRING_DESC_F_USED(1);
203 	}
204 
205 	virtio_wmb(vq->hw->weak_barriers);
206 	desc[head].flags = VRING_DESC_F_NEXT | flags;
207 
208 	virtio_wmb(vq->hw->weak_barriers);
209 	virtqueue_notify(vq);
210 
211 	/* wait for used descriptors in virtqueue */
212 	while (!desc_is_used(&desc[head], vq))
213 		usleep(100);
214 
215 	virtio_rmb(vq->hw->weak_barriers);
216 
217 	/* now get used descriptors */
218 	vq->vq_free_cnt += nb_descs;
219 	vq->vq_used_cons_idx += nb_descs;
220 	if (vq->vq_used_cons_idx >= vq->vq_nentries) {
221 		vq->vq_used_cons_idx -= vq->vq_nentries;
222 		vq->vq_packed.used_wrap_counter ^= 1;
223 	}
224 
225 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n"
226 			"vq->vq_avail_idx=%d\n"
227 			"vq->vq_used_cons_idx=%d\n"
228 			"vq->vq_packed.cached_flags=0x%x\n"
229 			"vq->vq_packed.used_wrap_counter=%d\n",
230 			vq->vq_free_cnt,
231 			vq->vq_avail_idx,
232 			vq->vq_used_cons_idx,
233 			vq->vq_packed.cached_flags,
234 			vq->vq_packed.used_wrap_counter);
235 
236 	result = cvq->virtio_net_hdr_mz->addr;
237 	return result;
238 }
239 
240 static struct virtio_pmd_ctrl *
241 virtio_send_command_split(struct virtnet_ctl *cvq,
242 			  struct virtio_pmd_ctrl *ctrl,
243 			  int *dlen, int pkt_num)
244 {
245 	struct virtio_pmd_ctrl *result;
246 	struct virtqueue *vq = cvq->vq;
247 	uint32_t head, i;
248 	int k, sum = 0;
249 
250 	head = vq->vq_desc_head_idx;
251 
252 	/*
253 	 * Format is enforced in qemu code:
254 	 * One TX packet for header;
255 	 * At least one TX packet per argument;
256 	 * One RX packet for ACK.
257 	 */
258 	vq->vq_split.ring.desc[head].flags = VRING_DESC_F_NEXT;
259 	vq->vq_split.ring.desc[head].addr = cvq->virtio_net_hdr_mem;
260 	vq->vq_split.ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
261 	vq->vq_free_cnt--;
262 	i = vq->vq_split.ring.desc[head].next;
263 
264 	for (k = 0; k < pkt_num; k++) {
265 		vq->vq_split.ring.desc[i].flags = VRING_DESC_F_NEXT;
266 		vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
267 			+ sizeof(struct virtio_net_ctrl_hdr)
268 			+ sizeof(ctrl->status) + sizeof(uint8_t)*sum;
269 		vq->vq_split.ring.desc[i].len = dlen[k];
270 		sum += dlen[k];
271 		vq->vq_free_cnt--;
272 		i = vq->vq_split.ring.desc[i].next;
273 	}
274 
275 	vq->vq_split.ring.desc[i].flags = VRING_DESC_F_WRITE;
276 	vq->vq_split.ring.desc[i].addr = cvq->virtio_net_hdr_mem
277 			+ sizeof(struct virtio_net_ctrl_hdr);
278 	vq->vq_split.ring.desc[i].len = sizeof(ctrl->status);
279 	vq->vq_free_cnt--;
280 
281 	vq->vq_desc_head_idx = vq->vq_split.ring.desc[i].next;
282 
283 	vq_update_avail_ring(vq, head);
284 	vq_update_avail_idx(vq);
285 
286 	PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
287 
288 	virtqueue_notify(vq);
289 
290 	rte_rmb();
291 	while (VIRTQUEUE_NUSED(vq) == 0) {
292 		rte_rmb();
293 		usleep(100);
294 	}
295 
296 	while (VIRTQUEUE_NUSED(vq)) {
297 		uint32_t idx, desc_idx, used_idx;
298 		struct vring_used_elem *uep;
299 
300 		used_idx = (uint32_t)(vq->vq_used_cons_idx
301 				& (vq->vq_nentries - 1));
302 		uep = &vq->vq_split.ring.used->ring[used_idx];
303 		idx = (uint32_t) uep->id;
304 		desc_idx = idx;
305 
306 		while (vq->vq_split.ring.desc[desc_idx].flags &
307 				VRING_DESC_F_NEXT) {
308 			desc_idx = vq->vq_split.ring.desc[desc_idx].next;
309 			vq->vq_free_cnt++;
310 		}
311 
312 		vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
313 		vq->vq_desc_head_idx = idx;
314 
315 		vq->vq_used_cons_idx++;
316 		vq->vq_free_cnt++;
317 	}
318 
319 	PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
320 			vq->vq_free_cnt, vq->vq_desc_head_idx);
321 
322 	result = cvq->virtio_net_hdr_mz->addr;
323 	return result;
324 }
325 
326 static int
327 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
328 		    int *dlen, int pkt_num)
329 {
330 	virtio_net_ctrl_ack status = ~0;
331 	struct virtio_pmd_ctrl *result;
332 	struct virtqueue *vq;
333 
334 	ctrl->status = status;
335 
336 	if (!cvq || !cvq->vq) {
337 		PMD_INIT_LOG(ERR, "Control queue is not supported.");
338 		return -1;
339 	}
340 
341 	rte_spinlock_lock(&cvq->lock);
342 	vq = cvq->vq;
343 
344 	PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
345 		"vq->hw->cvq = %p vq = %p",
346 		vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
347 
348 	if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) {
349 		rte_spinlock_unlock(&cvq->lock);
350 		return -1;
351 	}
352 
353 	memcpy(cvq->virtio_net_hdr_mz->addr, ctrl,
354 		sizeof(struct virtio_pmd_ctrl));
355 
356 	if (vtpci_packed_queue(vq->hw))
357 		result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num);
358 	else
359 		result = virtio_send_command_split(cvq, ctrl, dlen, pkt_num);
360 
361 	rte_spinlock_unlock(&cvq->lock);
362 	return result->status;
363 }
364 
365 static int
366 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
367 {
368 	struct virtio_hw *hw = dev->data->dev_private;
369 	struct virtio_pmd_ctrl ctrl;
370 	int dlen[1];
371 	int ret;
372 
373 	ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
374 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
375 	memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
376 
377 	dlen[0] = sizeof(uint16_t);
378 
379 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
380 	if (ret) {
381 		PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
382 			  "failed, this is too late now...");
383 		return -EINVAL;
384 	}
385 
386 	return 0;
387 }
388 
389 static void
390 virtio_dev_queue_release(void *queue __rte_unused)
391 {
392 	/* do nothing */
393 }
394 
395 static uint16_t
396 virtio_get_nr_vq(struct virtio_hw *hw)
397 {
398 	uint16_t nr_vq = hw->max_queue_pairs * 2;
399 
400 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
401 		nr_vq += 1;
402 
403 	return nr_vq;
404 }
405 
406 static void
407 virtio_init_vring(struct virtqueue *vq)
408 {
409 	int size = vq->vq_nentries;
410 	uint8_t *ring_mem = vq->vq_ring_virt_mem;
411 
412 	PMD_INIT_FUNC_TRACE();
413 
414 	memset(ring_mem, 0, vq->vq_ring_size);
415 
416 	vq->vq_used_cons_idx = 0;
417 	vq->vq_desc_head_idx = 0;
418 	vq->vq_avail_idx = 0;
419 	vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
420 	vq->vq_free_cnt = vq->vq_nentries;
421 	memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
422 	if (vtpci_packed_queue(vq->hw)) {
423 		vring_init_packed(&vq->vq_packed.ring, ring_mem,
424 				  VIRTIO_PCI_VRING_ALIGN, size);
425 		vring_desc_init_packed(vq, size);
426 	} else {
427 		struct vring *vr = &vq->vq_split.ring;
428 
429 		vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size);
430 		vring_desc_init_split(vr->desc, size);
431 	}
432 	/*
433 	 * Disable device(host) interrupting guest
434 	 */
435 	virtqueue_disable_intr(vq);
436 }
437 
438 static int
439 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx)
440 {
441 	char vq_name[VIRTQUEUE_MAX_NAME_SZ];
442 	char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ];
443 	const struct rte_memzone *mz = NULL, *hdr_mz = NULL;
444 	unsigned int vq_size, size;
445 	struct virtio_hw *hw = dev->data->dev_private;
446 	struct virtnet_rx *rxvq = NULL;
447 	struct virtnet_tx *txvq = NULL;
448 	struct virtnet_ctl *cvq = NULL;
449 	struct virtqueue *vq;
450 	size_t sz_hdr_mz = 0;
451 	void *sw_ring = NULL;
452 	int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx);
453 	int ret;
454 	int numa_node = dev->device->numa_node;
455 
456 	PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
457 			vtpci_queue_idx, numa_node);
458 
459 	/*
460 	 * Read the virtqueue size from the Queue Size field
461 	 * Always power of 2 and if 0 virtqueue does not exist
462 	 */
463 	vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
464 	PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
465 	if (vq_size == 0) {
466 		PMD_INIT_LOG(ERR, "virtqueue does not exist");
467 		return -EINVAL;
468 	}
469 
470 	if (!rte_is_power_of_2(vq_size)) {
471 		PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
472 		return -EINVAL;
473 	}
474 
475 	snprintf(vq_name, sizeof(vq_name), "port%d_vq%d",
476 		 dev->data->port_id, vtpci_queue_idx);
477 
478 	size = RTE_ALIGN_CEIL(sizeof(*vq) +
479 				vq_size * sizeof(struct vq_desc_extra),
480 				RTE_CACHE_LINE_SIZE);
481 	if (queue_type == VTNET_TQ) {
482 		/*
483 		 * For each xmit packet, allocate a virtio_net_hdr
484 		 * and indirect ring elements
485 		 */
486 		sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region);
487 	} else if (queue_type == VTNET_CQ) {
488 		/* Allocate a page for control vq command, data and status */
489 		sz_hdr_mz = PAGE_SIZE;
490 	}
491 
492 	vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
493 				numa_node);
494 	if (vq == NULL) {
495 		PMD_INIT_LOG(ERR, "can not allocate vq");
496 		return -ENOMEM;
497 	}
498 	hw->vqs[vtpci_queue_idx] = vq;
499 
500 	vq->hw = hw;
501 	vq->vq_queue_index = vtpci_queue_idx;
502 	vq->vq_nentries = vq_size;
503 	if (vtpci_packed_queue(hw)) {
504 		vq->vq_packed.used_wrap_counter = 1;
505 		vq->vq_packed.cached_flags = VRING_DESC_F_AVAIL(1);
506 		vq->vq_packed.event_flags_shadow = 0;
507 		if (queue_type == VTNET_RQ)
508 			vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
509 	}
510 
511 	/*
512 	 * Reserve a memzone for vring elements
513 	 */
514 	size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN);
515 	vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
516 	PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
517 		     size, vq->vq_ring_size);
518 
519 	mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
520 			numa_node, RTE_MEMZONE_IOVA_CONTIG,
521 			VIRTIO_PCI_VRING_ALIGN);
522 	if (mz == NULL) {
523 		if (rte_errno == EEXIST)
524 			mz = rte_memzone_lookup(vq_name);
525 		if (mz == NULL) {
526 			ret = -ENOMEM;
527 			goto fail_q_alloc;
528 		}
529 	}
530 
531 	memset(mz->addr, 0, mz->len);
532 
533 	vq->vq_ring_mem = mz->iova;
534 	vq->vq_ring_virt_mem = mz->addr;
535 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%" PRIx64,
536 		     (uint64_t)mz->iova);
537 	PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
538 		     (uint64_t)(uintptr_t)mz->addr);
539 
540 	virtio_init_vring(vq);
541 
542 	if (sz_hdr_mz) {
543 		snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr",
544 			 dev->data->port_id, vtpci_queue_idx);
545 		hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz,
546 				numa_node, RTE_MEMZONE_IOVA_CONTIG,
547 				RTE_CACHE_LINE_SIZE);
548 		if (hdr_mz == NULL) {
549 			if (rte_errno == EEXIST)
550 				hdr_mz = rte_memzone_lookup(vq_hdr_name);
551 			if (hdr_mz == NULL) {
552 				ret = -ENOMEM;
553 				goto fail_q_alloc;
554 			}
555 		}
556 	}
557 
558 	if (queue_type == VTNET_RQ) {
559 		size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
560 			       sizeof(vq->sw_ring[0]);
561 
562 		sw_ring = rte_zmalloc_socket("sw_ring", sz_sw,
563 				RTE_CACHE_LINE_SIZE, numa_node);
564 		if (!sw_ring) {
565 			PMD_INIT_LOG(ERR, "can not allocate RX soft ring");
566 			ret = -ENOMEM;
567 			goto fail_q_alloc;
568 		}
569 
570 		vq->sw_ring = sw_ring;
571 		rxvq = &vq->rxq;
572 		rxvq->vq = vq;
573 		rxvq->port_id = dev->data->port_id;
574 		rxvq->mz = mz;
575 	} else if (queue_type == VTNET_TQ) {
576 		txvq = &vq->txq;
577 		txvq->vq = vq;
578 		txvq->port_id = dev->data->port_id;
579 		txvq->mz = mz;
580 		txvq->virtio_net_hdr_mz = hdr_mz;
581 		txvq->virtio_net_hdr_mem = hdr_mz->iova;
582 	} else if (queue_type == VTNET_CQ) {
583 		cvq = &vq->cq;
584 		cvq->vq = vq;
585 		cvq->mz = mz;
586 		cvq->virtio_net_hdr_mz = hdr_mz;
587 		cvq->virtio_net_hdr_mem = hdr_mz->iova;
588 		memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
589 
590 		hw->cvq = cvq;
591 	}
592 
593 	/* For virtio_user case (that is when hw->dev is NULL), we use
594 	 * virtual address. And we need properly set _offset_, please see
595 	 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information.
596 	 */
597 	if (!hw->virtio_user_dev)
598 		vq->offset = offsetof(struct rte_mbuf, buf_iova);
599 	else {
600 		vq->vq_ring_mem = (uintptr_t)mz->addr;
601 		vq->offset = offsetof(struct rte_mbuf, buf_addr);
602 		if (queue_type == VTNET_TQ)
603 			txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
604 		else if (queue_type == VTNET_CQ)
605 			cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr;
606 	}
607 
608 	if (queue_type == VTNET_TQ) {
609 		struct virtio_tx_region *txr;
610 		unsigned int i;
611 
612 		txr = hdr_mz->addr;
613 		memset(txr, 0, vq_size * sizeof(*txr));
614 		for (i = 0; i < vq_size; i++) {
615 			struct vring_desc *start_dp = txr[i].tx_indir;
616 
617 			/* first indirect descriptor is always the tx header */
618 			if (!vtpci_packed_queue(hw)) {
619 				vring_desc_init_split(start_dp,
620 						      RTE_DIM(txr[i].tx_indir));
621 				start_dp->addr = txvq->virtio_net_hdr_mem
622 					+ i * sizeof(*txr)
623 					+ offsetof(struct virtio_tx_region,
624 						   tx_hdr);
625 				start_dp->len = hw->vtnet_hdr_size;
626 				start_dp->flags = VRING_DESC_F_NEXT;
627 			}
628 		}
629 	}
630 
631 	if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
632 		PMD_INIT_LOG(ERR, "setup_queue failed");
633 		return -EINVAL;
634 	}
635 
636 	return 0;
637 
638 fail_q_alloc:
639 	rte_free(sw_ring);
640 	rte_memzone_free(hdr_mz);
641 	rte_memzone_free(mz);
642 	rte_free(vq);
643 
644 	return ret;
645 }
646 
647 static void
648 virtio_free_queues(struct virtio_hw *hw)
649 {
650 	uint16_t nr_vq = virtio_get_nr_vq(hw);
651 	struct virtqueue *vq;
652 	int queue_type;
653 	uint16_t i;
654 
655 	if (hw->vqs == NULL)
656 		return;
657 
658 	for (i = 0; i < nr_vq; i++) {
659 		vq = hw->vqs[i];
660 		if (!vq)
661 			continue;
662 
663 		queue_type = virtio_get_queue_type(hw, i);
664 		if (queue_type == VTNET_RQ) {
665 			rte_free(vq->sw_ring);
666 			rte_memzone_free(vq->rxq.mz);
667 		} else if (queue_type == VTNET_TQ) {
668 			rte_memzone_free(vq->txq.mz);
669 			rte_memzone_free(vq->txq.virtio_net_hdr_mz);
670 		} else {
671 			rte_memzone_free(vq->cq.mz);
672 			rte_memzone_free(vq->cq.virtio_net_hdr_mz);
673 		}
674 
675 		rte_free(vq);
676 		hw->vqs[i] = NULL;
677 	}
678 
679 	rte_free(hw->vqs);
680 	hw->vqs = NULL;
681 }
682 
683 static int
684 virtio_alloc_queues(struct rte_eth_dev *dev)
685 {
686 	struct virtio_hw *hw = dev->data->dev_private;
687 	uint16_t nr_vq = virtio_get_nr_vq(hw);
688 	uint16_t i;
689 	int ret;
690 
691 	hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
692 	if (!hw->vqs) {
693 		PMD_INIT_LOG(ERR, "failed to allocate vqs");
694 		return -ENOMEM;
695 	}
696 
697 	for (i = 0; i < nr_vq; i++) {
698 		ret = virtio_init_queue(dev, i);
699 		if (ret < 0) {
700 			virtio_free_queues(hw);
701 			return ret;
702 		}
703 	}
704 
705 	return 0;
706 }
707 
708 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
709 
710 static void
711 virtio_dev_close(struct rte_eth_dev *dev)
712 {
713 	struct virtio_hw *hw = dev->data->dev_private;
714 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
715 
716 	PMD_INIT_LOG(DEBUG, "virtio_dev_close");
717 
718 	if (!hw->opened)
719 		return;
720 	hw->opened = false;
721 
722 	/* reset the NIC */
723 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
724 		VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
725 	if (intr_conf->rxq)
726 		virtio_queues_unbind_intr(dev);
727 
728 	if (intr_conf->lsc || intr_conf->rxq) {
729 		virtio_intr_disable(dev);
730 		rte_intr_efd_disable(dev->intr_handle);
731 		rte_free(dev->intr_handle->intr_vec);
732 		dev->intr_handle->intr_vec = NULL;
733 	}
734 
735 	vtpci_reset(hw);
736 	virtio_dev_free_mbufs(dev);
737 	virtio_free_queues(hw);
738 }
739 
740 static void
741 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
742 {
743 	struct virtio_hw *hw = dev->data->dev_private;
744 	struct virtio_pmd_ctrl ctrl;
745 	int dlen[1];
746 	int ret;
747 
748 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
749 		PMD_INIT_LOG(INFO, "host does not support rx control");
750 		return;
751 	}
752 
753 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
754 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
755 	ctrl.data[0] = 1;
756 	dlen[0] = 1;
757 
758 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
759 	if (ret)
760 		PMD_INIT_LOG(ERR, "Failed to enable promisc");
761 }
762 
763 static void
764 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
765 {
766 	struct virtio_hw *hw = dev->data->dev_private;
767 	struct virtio_pmd_ctrl ctrl;
768 	int dlen[1];
769 	int ret;
770 
771 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
772 		PMD_INIT_LOG(INFO, "host does not support rx control");
773 		return;
774 	}
775 
776 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
777 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
778 	ctrl.data[0] = 0;
779 	dlen[0] = 1;
780 
781 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
782 	if (ret)
783 		PMD_INIT_LOG(ERR, "Failed to disable promisc");
784 }
785 
786 static void
787 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
788 {
789 	struct virtio_hw *hw = dev->data->dev_private;
790 	struct virtio_pmd_ctrl ctrl;
791 	int dlen[1];
792 	int ret;
793 
794 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
795 		PMD_INIT_LOG(INFO, "host does not support rx control");
796 		return;
797 	}
798 
799 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
800 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
801 	ctrl.data[0] = 1;
802 	dlen[0] = 1;
803 
804 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
805 	if (ret)
806 		PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
807 }
808 
809 static void
810 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
811 {
812 	struct virtio_hw *hw = dev->data->dev_private;
813 	struct virtio_pmd_ctrl ctrl;
814 	int dlen[1];
815 	int ret;
816 
817 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
818 		PMD_INIT_LOG(INFO, "host does not support rx control");
819 		return;
820 	}
821 
822 	ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
823 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
824 	ctrl.data[0] = 0;
825 	dlen[0] = 1;
826 
827 	ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
828 	if (ret)
829 		PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
830 }
831 
832 #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
833 static int
834 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
835 {
836 	struct virtio_hw *hw = dev->data->dev_private;
837 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
838 				 hw->vtnet_hdr_size;
839 	uint32_t frame_size = mtu + ether_hdr_len;
840 	uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
841 
842 	max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
843 
844 	if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) {
845 		PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
846 			ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
847 		return -EINVAL;
848 	}
849 	return 0;
850 }
851 
852 static int
853 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
854 {
855 	struct virtio_hw *hw = dev->data->dev_private;
856 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
857 	struct virtqueue *vq = rxvq->vq;
858 
859 	virtqueue_enable_intr(vq);
860 	virtio_mb(hw->weak_barriers);
861 	return 0;
862 }
863 
864 static int
865 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
866 {
867 	struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
868 	struct virtqueue *vq = rxvq->vq;
869 
870 	virtqueue_disable_intr(vq);
871 	return 0;
872 }
873 
874 /*
875  * dev_ops for virtio, bare necessities for basic operation
876  */
877 static const struct eth_dev_ops virtio_eth_dev_ops = {
878 	.dev_configure           = virtio_dev_configure,
879 	.dev_start               = virtio_dev_start,
880 	.dev_stop                = virtio_dev_stop,
881 	.dev_close               = virtio_dev_close,
882 	.promiscuous_enable      = virtio_dev_promiscuous_enable,
883 	.promiscuous_disable     = virtio_dev_promiscuous_disable,
884 	.allmulticast_enable     = virtio_dev_allmulticast_enable,
885 	.allmulticast_disable    = virtio_dev_allmulticast_disable,
886 	.mtu_set                 = virtio_mtu_set,
887 	.dev_infos_get           = virtio_dev_info_get,
888 	.stats_get               = virtio_dev_stats_get,
889 	.xstats_get              = virtio_dev_xstats_get,
890 	.xstats_get_names        = virtio_dev_xstats_get_names,
891 	.stats_reset             = virtio_dev_stats_reset,
892 	.xstats_reset            = virtio_dev_stats_reset,
893 	.link_update             = virtio_dev_link_update,
894 	.vlan_offload_set        = virtio_dev_vlan_offload_set,
895 	.rx_queue_setup          = virtio_dev_rx_queue_setup,
896 	.rx_queue_intr_enable    = virtio_dev_rx_queue_intr_enable,
897 	.rx_queue_intr_disable   = virtio_dev_rx_queue_intr_disable,
898 	.rx_queue_release        = virtio_dev_queue_release,
899 	.rx_descriptor_done      = virtio_dev_rx_queue_done,
900 	.tx_queue_setup          = virtio_dev_tx_queue_setup,
901 	.tx_queue_release        = virtio_dev_queue_release,
902 	/* collect stats per queue */
903 	.queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
904 	.vlan_filter_set         = virtio_vlan_filter_set,
905 	.mac_addr_add            = virtio_mac_addr_add,
906 	.mac_addr_remove         = virtio_mac_addr_remove,
907 	.mac_addr_set            = virtio_mac_addr_set,
908 };
909 
910 static void
911 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
912 {
913 	unsigned i;
914 
915 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
916 		const struct virtnet_tx *txvq = dev->data->tx_queues[i];
917 		if (txvq == NULL)
918 			continue;
919 
920 		stats->opackets += txvq->stats.packets;
921 		stats->obytes += txvq->stats.bytes;
922 		stats->oerrors += txvq->stats.errors;
923 
924 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
925 			stats->q_opackets[i] = txvq->stats.packets;
926 			stats->q_obytes[i] = txvq->stats.bytes;
927 		}
928 	}
929 
930 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
931 		const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
932 		if (rxvq == NULL)
933 			continue;
934 
935 		stats->ipackets += rxvq->stats.packets;
936 		stats->ibytes += rxvq->stats.bytes;
937 		stats->ierrors += rxvq->stats.errors;
938 
939 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
940 			stats->q_ipackets[i] = rxvq->stats.packets;
941 			stats->q_ibytes[i] = rxvq->stats.bytes;
942 		}
943 	}
944 
945 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
946 }
947 
948 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
949 				       struct rte_eth_xstat_name *xstats_names,
950 				       __rte_unused unsigned limit)
951 {
952 	unsigned i;
953 	unsigned count = 0;
954 	unsigned t;
955 
956 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
957 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
958 
959 	if (xstats_names != NULL) {
960 		/* Note: limit checked in rte_eth_xstats_names() */
961 
962 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
963 			struct virtnet_rx *rxvq = dev->data->rx_queues[i];
964 			if (rxvq == NULL)
965 				continue;
966 			for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
967 				snprintf(xstats_names[count].name,
968 					sizeof(xstats_names[count].name),
969 					"rx_q%u_%s", i,
970 					rte_virtio_rxq_stat_strings[t].name);
971 				count++;
972 			}
973 		}
974 
975 		for (i = 0; i < dev->data->nb_tx_queues; i++) {
976 			struct virtnet_tx *txvq = dev->data->tx_queues[i];
977 			if (txvq == NULL)
978 				continue;
979 			for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
980 				snprintf(xstats_names[count].name,
981 					sizeof(xstats_names[count].name),
982 					"tx_q%u_%s", i,
983 					rte_virtio_txq_stat_strings[t].name);
984 				count++;
985 			}
986 		}
987 		return count;
988 	}
989 	return nstats;
990 }
991 
992 static int
993 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
994 		      unsigned n)
995 {
996 	unsigned i;
997 	unsigned count = 0;
998 
999 	unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
1000 		dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
1001 
1002 	if (n < nstats)
1003 		return nstats;
1004 
1005 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1006 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1007 
1008 		if (rxvq == NULL)
1009 			continue;
1010 
1011 		unsigned t;
1012 
1013 		for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
1014 			xstats[count].value = *(uint64_t *)(((char *)rxvq) +
1015 				rte_virtio_rxq_stat_strings[t].offset);
1016 			xstats[count].id = count;
1017 			count++;
1018 		}
1019 	}
1020 
1021 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1022 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
1023 
1024 		if (txvq == NULL)
1025 			continue;
1026 
1027 		unsigned t;
1028 
1029 		for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
1030 			xstats[count].value = *(uint64_t *)(((char *)txvq) +
1031 				rte_virtio_txq_stat_strings[t].offset);
1032 			xstats[count].id = count;
1033 			count++;
1034 		}
1035 	}
1036 
1037 	return count;
1038 }
1039 
1040 static int
1041 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1042 {
1043 	virtio_update_stats(dev, stats);
1044 
1045 	return 0;
1046 }
1047 
1048 static void
1049 virtio_dev_stats_reset(struct rte_eth_dev *dev)
1050 {
1051 	unsigned int i;
1052 
1053 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1054 		struct virtnet_tx *txvq = dev->data->tx_queues[i];
1055 		if (txvq == NULL)
1056 			continue;
1057 
1058 		txvq->stats.packets = 0;
1059 		txvq->stats.bytes = 0;
1060 		txvq->stats.errors = 0;
1061 		txvq->stats.multicast = 0;
1062 		txvq->stats.broadcast = 0;
1063 		memset(txvq->stats.size_bins, 0,
1064 		       sizeof(txvq->stats.size_bins[0]) * 8);
1065 	}
1066 
1067 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1068 		struct virtnet_rx *rxvq = dev->data->rx_queues[i];
1069 		if (rxvq == NULL)
1070 			continue;
1071 
1072 		rxvq->stats.packets = 0;
1073 		rxvq->stats.bytes = 0;
1074 		rxvq->stats.errors = 0;
1075 		rxvq->stats.multicast = 0;
1076 		rxvq->stats.broadcast = 0;
1077 		memset(rxvq->stats.size_bins, 0,
1078 		       sizeof(rxvq->stats.size_bins[0]) * 8);
1079 	}
1080 }
1081 
1082 static void
1083 virtio_set_hwaddr(struct virtio_hw *hw)
1084 {
1085 	vtpci_write_dev_config(hw,
1086 			offsetof(struct virtio_net_config, mac),
1087 			&hw->mac_addr, ETHER_ADDR_LEN);
1088 }
1089 
1090 static void
1091 virtio_get_hwaddr(struct virtio_hw *hw)
1092 {
1093 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
1094 		vtpci_read_dev_config(hw,
1095 			offsetof(struct virtio_net_config, mac),
1096 			&hw->mac_addr, ETHER_ADDR_LEN);
1097 	} else {
1098 		eth_random_addr(&hw->mac_addr[0]);
1099 		virtio_set_hwaddr(hw);
1100 	}
1101 }
1102 
1103 static int
1104 virtio_mac_table_set(struct virtio_hw *hw,
1105 		     const struct virtio_net_ctrl_mac *uc,
1106 		     const struct virtio_net_ctrl_mac *mc)
1107 {
1108 	struct virtio_pmd_ctrl ctrl;
1109 	int err, len[2];
1110 
1111 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1112 		PMD_DRV_LOG(INFO, "host does not support mac table");
1113 		return -1;
1114 	}
1115 
1116 	ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1117 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1118 
1119 	len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
1120 	memcpy(ctrl.data, uc, len[0]);
1121 
1122 	len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
1123 	memcpy(ctrl.data + len[0], mc, len[1]);
1124 
1125 	err = virtio_send_command(hw->cvq, &ctrl, len, 2);
1126 	if (err != 0)
1127 		PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
1128 	return err;
1129 }
1130 
1131 static int
1132 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1133 		    uint32_t index, uint32_t vmdq __rte_unused)
1134 {
1135 	struct virtio_hw *hw = dev->data->dev_private;
1136 	const struct ether_addr *addrs = dev->data->mac_addrs;
1137 	unsigned int i;
1138 	struct virtio_net_ctrl_mac *uc, *mc;
1139 
1140 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
1141 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1142 		return -EINVAL;
1143 	}
1144 
1145 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1146 	uc->entries = 0;
1147 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1148 	mc->entries = 0;
1149 
1150 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1151 		const struct ether_addr *addr
1152 			= (i == index) ? mac_addr : addrs + i;
1153 		struct virtio_net_ctrl_mac *tbl
1154 			= is_multicast_ether_addr(addr) ? mc : uc;
1155 
1156 		memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
1157 	}
1158 
1159 	return virtio_mac_table_set(hw, uc, mc);
1160 }
1161 
1162 static void
1163 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1164 {
1165 	struct virtio_hw *hw = dev->data->dev_private;
1166 	struct ether_addr *addrs = dev->data->mac_addrs;
1167 	struct virtio_net_ctrl_mac *uc, *mc;
1168 	unsigned int i;
1169 
1170 	if (index >= VIRTIO_MAX_MAC_ADDRS) {
1171 		PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
1172 		return;
1173 	}
1174 
1175 	uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
1176 	uc->entries = 0;
1177 	mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
1178 	mc->entries = 0;
1179 
1180 	for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
1181 		struct virtio_net_ctrl_mac *tbl;
1182 
1183 		if (i == index || is_zero_ether_addr(addrs + i))
1184 			continue;
1185 
1186 		tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
1187 		memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
1188 	}
1189 
1190 	virtio_mac_table_set(hw, uc, mc);
1191 }
1192 
1193 static int
1194 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
1195 {
1196 	struct virtio_hw *hw = dev->data->dev_private;
1197 
1198 	memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
1199 
1200 	/* Use atomic update if available */
1201 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1202 		struct virtio_pmd_ctrl ctrl;
1203 		int len = ETHER_ADDR_LEN;
1204 
1205 		ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
1206 		ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
1207 
1208 		memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
1209 		return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1210 	}
1211 
1212 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
1213 		return -ENOTSUP;
1214 
1215 	virtio_set_hwaddr(hw);
1216 	return 0;
1217 }
1218 
1219 static int
1220 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1221 {
1222 	struct virtio_hw *hw = dev->data->dev_private;
1223 	struct virtio_pmd_ctrl ctrl;
1224 	int len;
1225 
1226 	if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
1227 		return -ENOTSUP;
1228 
1229 	ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
1230 	ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
1231 	memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
1232 	len = sizeof(vlan_id);
1233 
1234 	return virtio_send_command(hw->cvq, &ctrl, &len, 1);
1235 }
1236 
1237 static int
1238 virtio_intr_enable(struct rte_eth_dev *dev)
1239 {
1240 	struct virtio_hw *hw = dev->data->dev_private;
1241 
1242 	if (rte_intr_enable(dev->intr_handle) < 0)
1243 		return -1;
1244 
1245 	if (!hw->virtio_user_dev)
1246 		hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1247 
1248 	return 0;
1249 }
1250 
1251 static int
1252 virtio_intr_disable(struct rte_eth_dev *dev)
1253 {
1254 	struct virtio_hw *hw = dev->data->dev_private;
1255 
1256 	if (rte_intr_disable(dev->intr_handle) < 0)
1257 		return -1;
1258 
1259 	if (!hw->virtio_user_dev)
1260 		hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev));
1261 
1262 	return 0;
1263 }
1264 
1265 static int
1266 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
1267 {
1268 	uint64_t host_features;
1269 
1270 	/* Prepare guest_features: feature that driver wants to support */
1271 	PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
1272 		req_features);
1273 
1274 	/* Read device(host) feature bits */
1275 	host_features = VTPCI_OPS(hw)->get_features(hw);
1276 	PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
1277 		host_features);
1278 
1279 	/* If supported, ensure MTU value is valid before acknowledging it. */
1280 	if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
1281 		struct virtio_net_config config;
1282 
1283 		vtpci_read_dev_config(hw,
1284 			offsetof(struct virtio_net_config, mtu),
1285 			&config.mtu, sizeof(config.mtu));
1286 
1287 		if (config.mtu < ETHER_MIN_MTU)
1288 			req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1289 	}
1290 
1291 	/*
1292 	 * Negotiate features: Subset of device feature bits are written back
1293 	 * guest feature bits.
1294 	 */
1295 	hw->guest_features = req_features;
1296 	hw->guest_features = vtpci_negotiate_features(hw, host_features);
1297 	PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
1298 		hw->guest_features);
1299 
1300 	if (hw->modern) {
1301 		if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
1302 			PMD_INIT_LOG(ERR,
1303 				"VIRTIO_F_VERSION_1 features is not enabled.");
1304 			return -1;
1305 		}
1306 		vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1307 		if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
1308 			PMD_INIT_LOG(ERR,
1309 				"failed to set FEATURES_OK status!");
1310 			return -1;
1311 		}
1312 	}
1313 
1314 	hw->req_guest_features = req_features;
1315 
1316 	return 0;
1317 }
1318 
1319 int
1320 virtio_dev_pause(struct rte_eth_dev *dev)
1321 {
1322 	struct virtio_hw *hw = dev->data->dev_private;
1323 
1324 	rte_spinlock_lock(&hw->state_lock);
1325 
1326 	if (hw->started == 0) {
1327 		/* Device is just stopped. */
1328 		rte_spinlock_unlock(&hw->state_lock);
1329 		return -1;
1330 	}
1331 	hw->started = 0;
1332 	/*
1333 	 * Prevent the worker threads from touching queues to avoid contention,
1334 	 * 1 ms should be enough for the ongoing Tx function to finish.
1335 	 */
1336 	rte_delay_ms(1);
1337 	return 0;
1338 }
1339 
1340 /*
1341  * Recover hw state to let the worker threads continue.
1342  */
1343 void
1344 virtio_dev_resume(struct rte_eth_dev *dev)
1345 {
1346 	struct virtio_hw *hw = dev->data->dev_private;
1347 
1348 	hw->started = 1;
1349 	rte_spinlock_unlock(&hw->state_lock);
1350 }
1351 
1352 /*
1353  * Should be called only after device is paused.
1354  */
1355 int
1356 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
1357 		int nb_pkts)
1358 {
1359 	struct virtio_hw *hw = dev->data->dev_private;
1360 	struct virtnet_tx *txvq = dev->data->tx_queues[0];
1361 	int ret;
1362 
1363 	hw->inject_pkts = tx_pkts;
1364 	ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts);
1365 	hw->inject_pkts = NULL;
1366 
1367 	return ret;
1368 }
1369 
1370 static void
1371 virtio_notify_peers(struct rte_eth_dev *dev)
1372 {
1373 	struct virtio_hw *hw = dev->data->dev_private;
1374 	struct virtnet_rx *rxvq;
1375 	struct rte_mbuf *rarp_mbuf;
1376 
1377 	if (!dev->data->rx_queues)
1378 		return;
1379 
1380 	rxvq = dev->data->rx_queues[0];
1381 	if (!rxvq)
1382 		return;
1383 
1384 	rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
1385 			(struct ether_addr *)hw->mac_addr);
1386 	if (rarp_mbuf == NULL) {
1387 		PMD_DRV_LOG(ERR, "failed to make RARP packet.");
1388 		return;
1389 	}
1390 
1391 	/* If virtio port just stopped, no need to send RARP */
1392 	if (virtio_dev_pause(dev) < 0) {
1393 		rte_pktmbuf_free(rarp_mbuf);
1394 		return;
1395 	}
1396 
1397 	virtio_inject_pkts(dev, &rarp_mbuf, 1);
1398 	virtio_dev_resume(dev);
1399 }
1400 
1401 static void
1402 virtio_ack_link_announce(struct rte_eth_dev *dev)
1403 {
1404 	struct virtio_hw *hw = dev->data->dev_private;
1405 	struct virtio_pmd_ctrl ctrl;
1406 
1407 	ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
1408 	ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
1409 
1410 	virtio_send_command(hw->cvq, &ctrl, NULL, 0);
1411 }
1412 
1413 /*
1414  * Process virtio config changed interrupt. Call the callback
1415  * if link state changed, generate gratuitous RARP packet if
1416  * the status indicates an ANNOUNCE.
1417  */
1418 void
1419 virtio_interrupt_handler(void *param)
1420 {
1421 	struct rte_eth_dev *dev = param;
1422 	struct virtio_hw *hw = dev->data->dev_private;
1423 	uint8_t isr;
1424 	uint16_t status;
1425 
1426 	/* Read interrupt status which clears interrupt */
1427 	isr = vtpci_isr(hw);
1428 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1429 
1430 	if (virtio_intr_enable(dev) < 0)
1431 		PMD_DRV_LOG(ERR, "interrupt enable failed");
1432 
1433 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
1434 		if (virtio_dev_link_update(dev, 0) == 0)
1435 			_rte_eth_dev_callback_process(dev,
1436 						      RTE_ETH_EVENT_INTR_LSC,
1437 						      NULL);
1438 
1439 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1440 			vtpci_read_dev_config(hw,
1441 				offsetof(struct virtio_net_config, status),
1442 				&status, sizeof(status));
1443 			if (status & VIRTIO_NET_S_ANNOUNCE) {
1444 				virtio_notify_peers(dev);
1445 				if (hw->cvq)
1446 					virtio_ack_link_announce(dev);
1447 			}
1448 		}
1449 	}
1450 }
1451 
1452 /* set rx and tx handlers according to what is supported */
1453 static void
1454 set_rxtx_funcs(struct rte_eth_dev *eth_dev)
1455 {
1456 	struct virtio_hw *hw = eth_dev->data->dev_private;
1457 
1458 	if (vtpci_packed_queue(hw)) {
1459 		PMD_INIT_LOG(INFO,
1460 			"virtio: using packed ring %s Tx path on port %u",
1461 			hw->use_inorder_tx ? "inorder" : "standard",
1462 			eth_dev->data->port_id);
1463 		eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
1464 	} else {
1465 		if (hw->use_inorder_tx) {
1466 			PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
1467 				eth_dev->data->port_id);
1468 			eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
1469 		} else {
1470 			PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
1471 				eth_dev->data->port_id);
1472 			eth_dev->tx_pkt_burst = virtio_xmit_pkts;
1473 		}
1474 	}
1475 
1476 	if (vtpci_packed_queue(hw)) {
1477 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1478 			PMD_INIT_LOG(INFO,
1479 				"virtio: using packed ring mergeable buffer Rx path on port %u",
1480 				eth_dev->data->port_id);
1481 			eth_dev->rx_pkt_burst =
1482 				&virtio_recv_mergeable_pkts_packed;
1483 		} else {
1484 			PMD_INIT_LOG(INFO,
1485 				"virtio: using packed ring standard Rx path on port %u",
1486 				eth_dev->data->port_id);
1487 			eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
1488 		}
1489 	} else {
1490 		if (hw->use_simple_rx) {
1491 			PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u",
1492 				eth_dev->data->port_id);
1493 			eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
1494 		} else if (hw->use_inorder_rx) {
1495 			PMD_INIT_LOG(INFO,
1496 				"virtio: using inorder Rx path on port %u",
1497 				eth_dev->data->port_id);
1498 			eth_dev->rx_pkt_burst =	&virtio_recv_pkts_inorder;
1499 		} else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
1500 			PMD_INIT_LOG(INFO,
1501 				"virtio: using mergeable buffer Rx path on port %u",
1502 				eth_dev->data->port_id);
1503 			eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1504 		} else {
1505 			PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
1506 				eth_dev->data->port_id);
1507 			eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1508 		}
1509 	}
1510 
1511 }
1512 
1513 /* Only support 1:1 queue/interrupt mapping so far.
1514  * TODO: support n:1 queue/interrupt mapping when there are limited number of
1515  * interrupt vectors (<N+1).
1516  */
1517 static int
1518 virtio_queues_bind_intr(struct rte_eth_dev *dev)
1519 {
1520 	uint32_t i;
1521 	struct virtio_hw *hw = dev->data->dev_private;
1522 
1523 	PMD_INIT_LOG(INFO, "queue/interrupt binding");
1524 	for (i = 0; i < dev->data->nb_rx_queues; ++i) {
1525 		dev->intr_handle->intr_vec[i] = i + 1;
1526 		if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
1527 						 VIRTIO_MSI_NO_VECTOR) {
1528 			PMD_DRV_LOG(ERR, "failed to set queue vector");
1529 			return -EBUSY;
1530 		}
1531 	}
1532 
1533 	return 0;
1534 }
1535 
1536 static void
1537 virtio_queues_unbind_intr(struct rte_eth_dev *dev)
1538 {
1539 	uint32_t i;
1540 	struct virtio_hw *hw = dev->data->dev_private;
1541 
1542 	PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
1543 	for (i = 0; i < dev->data->nb_rx_queues; ++i)
1544 		VTPCI_OPS(hw)->set_queue_irq(hw,
1545 					     hw->vqs[i * VTNET_CQ],
1546 					     VIRTIO_MSI_NO_VECTOR);
1547 }
1548 
1549 static int
1550 virtio_configure_intr(struct rte_eth_dev *dev)
1551 {
1552 	struct virtio_hw *hw = dev->data->dev_private;
1553 
1554 	if (!rte_intr_cap_multiple(dev->intr_handle)) {
1555 		PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
1556 		return -ENOTSUP;
1557 	}
1558 
1559 	if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) {
1560 		PMD_INIT_LOG(ERR, "Fail to create eventfd");
1561 		return -1;
1562 	}
1563 
1564 	if (!dev->intr_handle->intr_vec) {
1565 		dev->intr_handle->intr_vec =
1566 			rte_zmalloc("intr_vec",
1567 				    hw->max_queue_pairs * sizeof(int), 0);
1568 		if (!dev->intr_handle->intr_vec) {
1569 			PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
1570 				     hw->max_queue_pairs);
1571 			return -ENOMEM;
1572 		}
1573 	}
1574 
1575 	/* Re-register callback to update max_intr */
1576 	rte_intr_callback_unregister(dev->intr_handle,
1577 				     virtio_interrupt_handler,
1578 				     dev);
1579 	rte_intr_callback_register(dev->intr_handle,
1580 				   virtio_interrupt_handler,
1581 				   dev);
1582 
1583 	/* DO NOT try to remove this! This function will enable msix, or QEMU
1584 	 * will encounter SIGSEGV when DRIVER_OK is sent.
1585 	 * And for legacy devices, this should be done before queue/vec binding
1586 	 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
1587 	 * (22) will be ignored.
1588 	 */
1589 	if (virtio_intr_enable(dev) < 0) {
1590 		PMD_DRV_LOG(ERR, "interrupt enable failed");
1591 		return -1;
1592 	}
1593 
1594 	if (virtio_queues_bind_intr(dev) < 0) {
1595 		PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
1596 		return -1;
1597 	}
1598 
1599 	return 0;
1600 }
1601 
1602 /* reset device and renegotiate features if needed */
1603 static int
1604 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
1605 {
1606 	struct virtio_hw *hw = eth_dev->data->dev_private;
1607 	struct virtio_net_config *config;
1608 	struct virtio_net_config local_config;
1609 	struct rte_pci_device *pci_dev = NULL;
1610 	int ret;
1611 
1612 	/* Reset the device although not necessary at startup */
1613 	vtpci_reset(hw);
1614 
1615 	if (hw->vqs) {
1616 		virtio_dev_free_mbufs(eth_dev);
1617 		virtio_free_queues(hw);
1618 	}
1619 
1620 	/* Tell the host we've noticed this device. */
1621 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1622 
1623 	/* Tell the host we've known how to drive the device. */
1624 	vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1625 	if (virtio_negotiate_features(hw, req_features) < 0)
1626 		return -1;
1627 
1628 	hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
1629 
1630 	if (!hw->virtio_user_dev) {
1631 		pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1632 		rte_eth_copy_pci_info(eth_dev, pci_dev);
1633 	}
1634 
1635 	/* If host does not support both status and MSI-X then disable LSC */
1636 	if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) &&
1637 	    hw->use_msix != VIRTIO_MSIX_NONE)
1638 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1639 	else
1640 		eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1641 
1642 	/* Setting up rx_header size for the device */
1643 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
1644 	    vtpci_with_feature(hw, VIRTIO_F_VERSION_1) ||
1645 	    vtpci_with_feature(hw, VIRTIO_F_RING_PACKED))
1646 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1647 	else
1648 		hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1649 
1650 	/* Copy the permanent MAC address to: virtio_hw */
1651 	virtio_get_hwaddr(hw);
1652 	ether_addr_copy((struct ether_addr *) hw->mac_addr,
1653 			&eth_dev->data->mac_addrs[0]);
1654 	PMD_INIT_LOG(DEBUG,
1655 		     "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1656 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1657 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1658 
1659 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1660 		config = &local_config;
1661 
1662 		vtpci_read_dev_config(hw,
1663 			offsetof(struct virtio_net_config, mac),
1664 			&config->mac, sizeof(config->mac));
1665 
1666 		if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1667 			vtpci_read_dev_config(hw,
1668 				offsetof(struct virtio_net_config, status),
1669 				&config->status, sizeof(config->status));
1670 		} else {
1671 			PMD_INIT_LOG(DEBUG,
1672 				     "VIRTIO_NET_F_STATUS is not supported");
1673 			config->status = 0;
1674 		}
1675 
1676 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1677 			vtpci_read_dev_config(hw,
1678 				offsetof(struct virtio_net_config, max_virtqueue_pairs),
1679 				&config->max_virtqueue_pairs,
1680 				sizeof(config->max_virtqueue_pairs));
1681 		} else {
1682 			PMD_INIT_LOG(DEBUG,
1683 				     "VIRTIO_NET_F_MQ is not supported");
1684 			config->max_virtqueue_pairs = 1;
1685 		}
1686 
1687 		hw->max_queue_pairs = config->max_virtqueue_pairs;
1688 
1689 		if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) {
1690 			vtpci_read_dev_config(hw,
1691 				offsetof(struct virtio_net_config, mtu),
1692 				&config->mtu,
1693 				sizeof(config->mtu));
1694 
1695 			/*
1696 			 * MTU value has already been checked at negotiation
1697 			 * time, but check again in case it has changed since
1698 			 * then, which should not happen.
1699 			 */
1700 			if (config->mtu < ETHER_MIN_MTU) {
1701 				PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
1702 						config->mtu);
1703 				return -1;
1704 			}
1705 
1706 			hw->max_mtu = config->mtu;
1707 			/* Set initial MTU to maximum one supported by vhost */
1708 			eth_dev->data->mtu = config->mtu;
1709 
1710 		} else {
1711 			hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1712 				VLAN_TAG_LEN - hw->vtnet_hdr_size;
1713 		}
1714 
1715 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1716 				config->max_virtqueue_pairs);
1717 		PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1718 		PMD_INIT_LOG(DEBUG,
1719 				"PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1720 				config->mac[0], config->mac[1],
1721 				config->mac[2], config->mac[3],
1722 				config->mac[4], config->mac[5]);
1723 	} else {
1724 		PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
1725 		hw->max_queue_pairs = 1;
1726 		hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN -
1727 			VLAN_TAG_LEN - hw->vtnet_hdr_size;
1728 	}
1729 
1730 	ret = virtio_alloc_queues(eth_dev);
1731 	if (ret < 0)
1732 		return ret;
1733 
1734 	if (eth_dev->data->dev_conf.intr_conf.rxq) {
1735 		if (virtio_configure_intr(eth_dev) < 0) {
1736 			PMD_INIT_LOG(ERR, "failed to configure interrupt");
1737 			return -1;
1738 		}
1739 	}
1740 
1741 	vtpci_reinit_complete(hw);
1742 
1743 	if (pci_dev)
1744 		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1745 			eth_dev->data->port_id, pci_dev->id.vendor_id,
1746 			pci_dev->id.device_id);
1747 
1748 	return 0;
1749 }
1750 
1751 /*
1752  * Remap the PCI device again (IO port map for legacy device and
1753  * memory map for modern device), so that the secondary process
1754  * could have the PCI initiated correctly.
1755  */
1756 static int
1757 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw)
1758 {
1759 	if (hw->modern) {
1760 		/*
1761 		 * We don't have to re-parse the PCI config space, since
1762 		 * rte_pci_map_device() makes sure the mapped address
1763 		 * in secondary process would equal to the one mapped in
1764 		 * the primary process: error will be returned if that
1765 		 * requirement is not met.
1766 		 *
1767 		 * That said, we could simply reuse all cap pointers
1768 		 * (such as dev_cfg, common_cfg, etc.) parsed from the
1769 		 * primary process, which is stored in shared memory.
1770 		 */
1771 		if (rte_pci_map_device(pci_dev)) {
1772 			PMD_INIT_LOG(DEBUG, "failed to map pci device!");
1773 			return -1;
1774 		}
1775 	} else {
1776 		if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
1777 			return -1;
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 static void
1784 virtio_set_vtpci_ops(struct virtio_hw *hw)
1785 {
1786 #ifdef RTE_VIRTIO_USER
1787 	if (hw->virtio_user_dev)
1788 		VTPCI_OPS(hw) = &virtio_user_ops;
1789 	else
1790 #endif
1791 	if (hw->modern)
1792 		VTPCI_OPS(hw) = &modern_ops;
1793 	else
1794 		VTPCI_OPS(hw) = &legacy_ops;
1795 }
1796 
1797 /*
1798  * This function is based on probe() function in virtio_pci.c
1799  * It returns 0 on success.
1800  */
1801 int
1802 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1803 {
1804 	struct virtio_hw *hw = eth_dev->data->dev_private;
1805 	int ret;
1806 
1807 	RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf));
1808 
1809 	eth_dev->dev_ops = &virtio_eth_dev_ops;
1810 
1811 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1812 		if (!hw->virtio_user_dev) {
1813 			ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1814 			if (ret)
1815 				return ret;
1816 		}
1817 
1818 		virtio_set_vtpci_ops(hw);
1819 		set_rxtx_funcs(eth_dev);
1820 
1821 		return 0;
1822 	}
1823 
1824 	/* Allocate memory for storing MAC addresses */
1825 	eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1826 	if (eth_dev->data->mac_addrs == NULL) {
1827 		PMD_INIT_LOG(ERR,
1828 			"Failed to allocate %d bytes needed to store MAC addresses",
1829 			VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1830 		return -ENOMEM;
1831 	}
1832 
1833 	hw->port_id = eth_dev->data->port_id;
1834 	/* For virtio_user case the hw->virtio_user_dev is populated by
1835 	 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called.
1836 	 */
1837 	if (!hw->virtio_user_dev) {
1838 		ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
1839 		if (ret)
1840 			goto out;
1841 	}
1842 
1843 	/* reset device and negotiate default features */
1844 	ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
1845 	if (ret < 0)
1846 		goto out;
1847 
1848 	return 0;
1849 
1850 out:
1851 	rte_free(eth_dev->data->mac_addrs);
1852 	return ret;
1853 }
1854 
1855 static int
1856 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1857 {
1858 	PMD_INIT_FUNC_TRACE();
1859 
1860 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1861 		return 0;
1862 
1863 	virtio_dev_stop(eth_dev);
1864 	virtio_dev_close(eth_dev);
1865 
1866 	eth_dev->dev_ops = NULL;
1867 	eth_dev->tx_pkt_burst = NULL;
1868 	eth_dev->rx_pkt_burst = NULL;
1869 
1870 	if (eth_dev->device)
1871 		rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
1872 
1873 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1874 
1875 	return 0;
1876 }
1877 
1878 static int vdpa_check_handler(__rte_unused const char *key,
1879 		const char *value, __rte_unused void *opaque)
1880 {
1881 	if (strcmp(value, "1"))
1882 		return -1;
1883 
1884 	return 0;
1885 }
1886 
1887 static int
1888 vdpa_mode_selected(struct rte_devargs *devargs)
1889 {
1890 	struct rte_kvargs *kvlist;
1891 	const char *key = "vdpa";
1892 	int ret = 0;
1893 
1894 	if (devargs == NULL)
1895 		return 0;
1896 
1897 	kvlist = rte_kvargs_parse(devargs->args, NULL);
1898 	if (kvlist == NULL)
1899 		return 0;
1900 
1901 	if (!rte_kvargs_count(kvlist, key))
1902 		goto exit;
1903 
1904 	/* vdpa mode selected when there's a key-value pair: vdpa=1 */
1905 	if (rte_kvargs_process(kvlist, key,
1906 				vdpa_check_handler, NULL) < 0) {
1907 		goto exit;
1908 	}
1909 	ret = 1;
1910 
1911 exit:
1912 	rte_kvargs_free(kvlist);
1913 	return ret;
1914 }
1915 
1916 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1917 	struct rte_pci_device *pci_dev)
1918 {
1919 	if (rte_eal_iopl_init() != 0) {
1920 		PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1921 		return 1;
1922 	}
1923 
1924 	/* virtio pmd skips probe if device needs to work in vdpa mode */
1925 	if (vdpa_mode_selected(pci_dev->device.devargs))
1926 		return 1;
1927 
1928 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw),
1929 		eth_virtio_dev_init);
1930 }
1931 
1932 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
1933 {
1934 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit);
1935 }
1936 
1937 static struct rte_pci_driver rte_virtio_pmd = {
1938 	.driver = {
1939 		.name = "net_virtio",
1940 	},
1941 	.id_table = pci_id_virtio_map,
1942 	.drv_flags = 0,
1943 	.probe = eth_virtio_pci_probe,
1944 	.remove = eth_virtio_pci_remove,
1945 };
1946 
1947 RTE_INIT(rte_virtio_pmd_init)
1948 {
1949 	rte_eal_iopl_init();
1950 	rte_pci_register(&rte_virtio_pmd);
1951 }
1952 
1953 static bool
1954 rx_offload_enabled(struct virtio_hw *hw)
1955 {
1956 	return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
1957 		vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
1958 		vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
1959 }
1960 
1961 static bool
1962 tx_offload_enabled(struct virtio_hw *hw)
1963 {
1964 	return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
1965 		vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
1966 		vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
1967 }
1968 
1969 /*
1970  * Configure virtio device
1971  * It returns 0 on success.
1972  */
1973 static int
1974 virtio_dev_configure(struct rte_eth_dev *dev)
1975 {
1976 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1977 	const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1978 	struct virtio_hw *hw = dev->data->dev_private;
1979 	uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN +
1980 		hw->vtnet_hdr_size;
1981 	uint64_t rx_offloads = rxmode->offloads;
1982 	uint64_t tx_offloads = txmode->offloads;
1983 	uint64_t req_features;
1984 	int ret;
1985 
1986 	PMD_INIT_LOG(DEBUG, "configure");
1987 	req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
1988 
1989 	if (dev->data->dev_conf.intr_conf.rxq) {
1990 		ret = virtio_init_device(dev, hw->req_guest_features);
1991 		if (ret < 0)
1992 			return ret;
1993 	}
1994 
1995 	if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len)
1996 		req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
1997 
1998 	if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
1999 			   DEV_RX_OFFLOAD_TCP_CKSUM))
2000 		req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
2001 
2002 	if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
2003 		req_features |=
2004 			(1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2005 			(1ULL << VIRTIO_NET_F_GUEST_TSO6);
2006 
2007 	if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM |
2008 			   DEV_TX_OFFLOAD_TCP_CKSUM))
2009 		req_features |= (1ULL << VIRTIO_NET_F_CSUM);
2010 
2011 	if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO)
2012 		req_features |=
2013 			(1ULL << VIRTIO_NET_F_HOST_TSO4) |
2014 			(1ULL << VIRTIO_NET_F_HOST_TSO6);
2015 
2016 	/* if request features changed, reinit the device */
2017 	if (req_features != hw->req_guest_features) {
2018 		ret = virtio_init_device(dev, req_features);
2019 		if (ret < 0)
2020 			return ret;
2021 	}
2022 
2023 	if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2024 			    DEV_RX_OFFLOAD_TCP_CKSUM)) &&
2025 		!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
2026 		PMD_DRV_LOG(ERR,
2027 			"rx checksum not available on this host");
2028 		return -ENOTSUP;
2029 	}
2030 
2031 	if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) &&
2032 		(!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
2033 		 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
2034 		PMD_DRV_LOG(ERR,
2035 			"Large Receive Offload not available on this host");
2036 		return -ENOTSUP;
2037 	}
2038 
2039 	/* start control queue */
2040 	if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
2041 		virtio_dev_cq_start(dev);
2042 
2043 	if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2044 		hw->vlan_strip = 1;
2045 
2046 	if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2047 	    && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2048 		PMD_DRV_LOG(ERR,
2049 			    "vlan filtering not available on this host");
2050 		return -ENOTSUP;
2051 	}
2052 
2053 	hw->has_tx_offload = tx_offload_enabled(hw);
2054 	hw->has_rx_offload = rx_offload_enabled(hw);
2055 
2056 	if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2057 		/* Enable vector (0) for Link State Intrerrupt */
2058 		if (VTPCI_OPS(hw)->set_config_irq(hw, 0) ==
2059 				VIRTIO_MSI_NO_VECTOR) {
2060 			PMD_DRV_LOG(ERR, "failed to set config vector");
2061 			return -EBUSY;
2062 		}
2063 
2064 	rte_spinlock_init(&hw->state_lock);
2065 
2066 	hw->use_simple_rx = 1;
2067 
2068 	if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) {
2069 		hw->use_inorder_tx = 1;
2070 		hw->use_inorder_rx = 1;
2071 		hw->use_simple_rx = 0;
2072 	}
2073 
2074 	if (vtpci_packed_queue(hw)) {
2075 		hw->use_simple_rx = 0;
2076 		hw->use_inorder_rx = 0;
2077 	}
2078 
2079 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
2080 	if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
2081 		hw->use_simple_rx = 0;
2082 	}
2083 #endif
2084 	if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
2085 		 hw->use_simple_rx = 0;
2086 	}
2087 
2088 	if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM |
2089 			   DEV_RX_OFFLOAD_TCP_CKSUM |
2090 			   DEV_RX_OFFLOAD_TCP_LRO |
2091 			   DEV_RX_OFFLOAD_VLAN_STRIP))
2092 		hw->use_simple_rx = 0;
2093 
2094 	hw->opened = true;
2095 
2096 	return 0;
2097 }
2098 
2099 
2100 static int
2101 virtio_dev_start(struct rte_eth_dev *dev)
2102 {
2103 	uint16_t nb_queues, i;
2104 	struct virtnet_rx *rxvq;
2105 	struct virtnet_tx *txvq __rte_unused;
2106 	struct virtio_hw *hw = dev->data->dev_private;
2107 	int ret;
2108 
2109 	/* Finish the initialization of the queues */
2110 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2111 		ret = virtio_dev_rx_queue_setup_finish(dev, i);
2112 		if (ret < 0)
2113 			return ret;
2114 	}
2115 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2116 		ret = virtio_dev_tx_queue_setup_finish(dev, i);
2117 		if (ret < 0)
2118 			return ret;
2119 	}
2120 
2121 	/* check if lsc interrupt feature is enabled */
2122 	if (dev->data->dev_conf.intr_conf.lsc) {
2123 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
2124 			PMD_DRV_LOG(ERR, "link status not supported by host");
2125 			return -ENOTSUP;
2126 		}
2127 	}
2128 
2129 	/* Enable uio/vfio intr/eventfd mapping: althrough we already did that
2130 	 * in device configure, but it could be unmapped  when device is
2131 	 * stopped.
2132 	 */
2133 	if (dev->data->dev_conf.intr_conf.lsc ||
2134 	    dev->data->dev_conf.intr_conf.rxq) {
2135 		virtio_intr_disable(dev);
2136 
2137 		/* Setup interrupt callback  */
2138 		if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2139 			rte_intr_callback_register(dev->intr_handle,
2140 						   virtio_interrupt_handler,
2141 						   dev);
2142 
2143 		if (virtio_intr_enable(dev) < 0) {
2144 			PMD_DRV_LOG(ERR, "interrupt enable failed");
2145 			return -EIO;
2146 		}
2147 	}
2148 
2149 	/*Notify the backend
2150 	 *Otherwise the tap backend might already stop its queue due to fullness.
2151 	 *vhost backend will have no chance to be waked up
2152 	 */
2153 	nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2154 	if (hw->max_queue_pairs > 1) {
2155 		if (virtio_set_multiple_queues(dev, nb_queues) != 0)
2156 			return -EINVAL;
2157 	}
2158 
2159 	PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
2160 
2161 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2162 		rxvq = dev->data->rx_queues[i];
2163 		/* Flush the old packets */
2164 		virtqueue_rxvq_flush(rxvq->vq);
2165 		virtqueue_notify(rxvq->vq);
2166 	}
2167 
2168 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2169 		txvq = dev->data->tx_queues[i];
2170 		virtqueue_notify(txvq->vq);
2171 	}
2172 
2173 	PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
2174 
2175 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2176 		rxvq = dev->data->rx_queues[i];
2177 		VIRTQUEUE_DUMP(rxvq->vq);
2178 	}
2179 
2180 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2181 		txvq = dev->data->tx_queues[i];
2182 		VIRTQUEUE_DUMP(txvq->vq);
2183 	}
2184 
2185 	set_rxtx_funcs(dev);
2186 	hw->started = true;
2187 
2188 	/* Initialize Link state */
2189 	virtio_dev_link_update(dev, 0);
2190 
2191 	return 0;
2192 }
2193 
2194 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
2195 {
2196 	struct virtio_hw *hw = dev->data->dev_private;
2197 	uint16_t nr_vq = virtio_get_nr_vq(hw);
2198 	const char *type __rte_unused;
2199 	unsigned int i, mbuf_num = 0;
2200 	struct virtqueue *vq;
2201 	struct rte_mbuf *buf;
2202 	int queue_type;
2203 
2204 	if (hw->vqs == NULL)
2205 		return;
2206 
2207 	for (i = 0; i < nr_vq; i++) {
2208 		vq = hw->vqs[i];
2209 		if (!vq)
2210 			continue;
2211 
2212 		queue_type = virtio_get_queue_type(hw, i);
2213 		if (queue_type == VTNET_RQ)
2214 			type = "rxq";
2215 		else if (queue_type == VTNET_TQ)
2216 			type = "txq";
2217 		else
2218 			continue;
2219 
2220 		PMD_INIT_LOG(DEBUG,
2221 			"Before freeing %s[%d] used and unused buf",
2222 			type, i);
2223 		VIRTQUEUE_DUMP(vq);
2224 
2225 		while ((buf = virtqueue_detach_unused(vq)) != NULL) {
2226 			rte_pktmbuf_free(buf);
2227 			mbuf_num++;
2228 		}
2229 
2230 		PMD_INIT_LOG(DEBUG,
2231 			"After freeing %s[%d] used and unused buf",
2232 			type, i);
2233 		VIRTQUEUE_DUMP(vq);
2234 	}
2235 
2236 	PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
2237 }
2238 
2239 /*
2240  * Stop device: disable interrupt and mark link down
2241  */
2242 static void
2243 virtio_dev_stop(struct rte_eth_dev *dev)
2244 {
2245 	struct virtio_hw *hw = dev->data->dev_private;
2246 	struct rte_eth_link link;
2247 	struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
2248 
2249 	PMD_INIT_LOG(DEBUG, "stop");
2250 
2251 	rte_spinlock_lock(&hw->state_lock);
2252 	if (!hw->started)
2253 		goto out_unlock;
2254 	hw->started = false;
2255 
2256 	if (intr_conf->lsc || intr_conf->rxq) {
2257 		virtio_intr_disable(dev);
2258 
2259 		/* Reset interrupt callback  */
2260 		if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
2261 			rte_intr_callback_unregister(dev->intr_handle,
2262 						     virtio_interrupt_handler,
2263 						     dev);
2264 		}
2265 	}
2266 
2267 	memset(&link, 0, sizeof(link));
2268 	rte_eth_linkstatus_set(dev, &link);
2269 out_unlock:
2270 	rte_spinlock_unlock(&hw->state_lock);
2271 }
2272 
2273 static int
2274 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
2275 {
2276 	struct rte_eth_link link;
2277 	uint16_t status;
2278 	struct virtio_hw *hw = dev->data->dev_private;
2279 
2280 	memset(&link, 0, sizeof(link));
2281 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
2282 	link.link_speed  = ETH_SPEED_NUM_10G;
2283 	link.link_autoneg = ETH_LINK_FIXED;
2284 
2285 	if (!hw->started) {
2286 		link.link_status = ETH_LINK_DOWN;
2287 	} else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
2288 		PMD_INIT_LOG(DEBUG, "Get link status from hw");
2289 		vtpci_read_dev_config(hw,
2290 				offsetof(struct virtio_net_config, status),
2291 				&status, sizeof(status));
2292 		if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
2293 			link.link_status = ETH_LINK_DOWN;
2294 			PMD_INIT_LOG(DEBUG, "Port %d is down",
2295 				     dev->data->port_id);
2296 		} else {
2297 			link.link_status = ETH_LINK_UP;
2298 			PMD_INIT_LOG(DEBUG, "Port %d is up",
2299 				     dev->data->port_id);
2300 		}
2301 	} else {
2302 		link.link_status = ETH_LINK_UP;
2303 	}
2304 
2305 	return rte_eth_linkstatus_set(dev, &link);
2306 }
2307 
2308 static int
2309 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2310 {
2311 	const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
2312 	struct virtio_hw *hw = dev->data->dev_private;
2313 	uint64_t offloads = rxmode->offloads;
2314 
2315 	if (mask & ETH_VLAN_FILTER_MASK) {
2316 		if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) &&
2317 				!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
2318 
2319 			PMD_DRV_LOG(NOTICE,
2320 				"vlan filtering not available on this host");
2321 
2322 			return -ENOTSUP;
2323 		}
2324 	}
2325 
2326 	if (mask & ETH_VLAN_STRIP_MASK)
2327 		hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2328 
2329 	return 0;
2330 }
2331 
2332 static void
2333 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2334 {
2335 	uint64_t tso_mask, host_features;
2336 	struct virtio_hw *hw = dev->data->dev_private;
2337 
2338 	dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */
2339 
2340 	dev_info->max_rx_queues =
2341 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
2342 	dev_info->max_tx_queues =
2343 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
2344 	dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
2345 	dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
2346 	dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
2347 
2348 	host_features = VTPCI_OPS(hw)->get_features(hw);
2349 	dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
2350 	dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2351 	if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
2352 		dev_info->rx_offload_capa |=
2353 			DEV_RX_OFFLOAD_TCP_CKSUM |
2354 			DEV_RX_OFFLOAD_UDP_CKSUM;
2355 	}
2356 	if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
2357 		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER;
2358 	tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
2359 		(1ULL << VIRTIO_NET_F_GUEST_TSO6);
2360 	if ((host_features & tso_mask) == tso_mask)
2361 		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2362 
2363 	dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
2364 				    DEV_TX_OFFLOAD_VLAN_INSERT;
2365 	if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
2366 		dev_info->tx_offload_capa |=
2367 			DEV_TX_OFFLOAD_UDP_CKSUM |
2368 			DEV_TX_OFFLOAD_TCP_CKSUM;
2369 	}
2370 	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2371 		(1ULL << VIRTIO_NET_F_HOST_TSO6);
2372 	if ((host_features & tso_mask) == tso_mask)
2373 		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
2374 }
2375 
2376 /*
2377  * It enables testpmd to collect per queue stats.
2378  */
2379 static int
2380 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
2381 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
2382 __rte_unused uint8_t is_rx)
2383 {
2384 	return 0;
2385 }
2386 
2387 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
2388 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
2389 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
2390 
2391 RTE_INIT(virtio_init_log)
2392 {
2393 	virtio_logtype_init = rte_log_register("pmd.net.virtio.init");
2394 	if (virtio_logtype_init >= 0)
2395 		rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE);
2396 	virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver");
2397 	if (virtio_logtype_driver >= 0)
2398 		rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE);
2399 }
2400