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