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