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