xref: /dpdk/drivers/net/netvsc/hn_ethdev.c (revision d9ddc004e6968baece016124cc2d000c55afc8aa)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Microsoft Corporation
3  * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
4  * All rights reserved.
5  */
6 
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <errno.h>
11 #include <unistd.h>
12 
13 #include <rte_ethdev.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_memzone.h>
17 #include <rte_malloc.h>
18 #include <rte_atomic.h>
19 #include <rte_branch_prediction.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev_driver.h>
22 #include <rte_cycles.h>
23 #include <rte_errno.h>
24 #include <rte_memory.h>
25 #include <rte_eal.h>
26 #include <rte_dev.h>
27 #include <rte_bus_vmbus.h>
28 
29 #include "hn_logs.h"
30 #include "hn_var.h"
31 #include "hn_rndis.h"
32 #include "hn_nvs.h"
33 #include "ndis.h"
34 
35 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
36 			    DEV_TX_OFFLOAD_TCP_CKSUM  | \
37 			    DEV_TX_OFFLOAD_UDP_CKSUM  | \
38 			    DEV_TX_OFFLOAD_TCP_TSO    | \
39 			    DEV_TX_OFFLOAD_MULTI_SEGS | \
40 			    DEV_TX_OFFLOAD_VLAN_INSERT)
41 
42 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \
43 			    DEV_RX_OFFLOAD_VLAN_STRIP | \
44 			    DEV_RX_OFFLOAD_CRC_STRIP)
45 
46 int hn_logtype_init;
47 int hn_logtype_driver;
48 
49 struct hn_xstats_name_off {
50 	char name[RTE_ETH_XSTATS_NAME_SIZE];
51 	unsigned int offset;
52 };
53 
54 static const struct hn_xstats_name_off hn_stat_strings[] = {
55 	{ "good_packets",           offsetof(struct hn_stats, packets) },
56 	{ "good_bytes",             offsetof(struct hn_stats, bytes) },
57 	{ "errors",                 offsetof(struct hn_stats, errors) },
58 	{ "allocation_failed",      offsetof(struct hn_stats, nomemory) },
59 	{ "multicast_packets",      offsetof(struct hn_stats, multicast) },
60 	{ "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
61 	{ "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
62 	{ "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
63 	{ "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
64 	{ "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
65 	{ "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
66 	{ "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
67 	{ "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
68 	{ "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
69 };
70 
71 static struct rte_eth_dev *
72 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
73 {
74 	struct rte_eth_dev *eth_dev;
75 	const char *name;
76 
77 	if (!dev)
78 		return NULL;
79 
80 	name = dev->device.name;
81 
82 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
83 		eth_dev = rte_eth_dev_allocate(name);
84 		if (!eth_dev) {
85 			PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
86 			return NULL;
87 		}
88 
89 		if (private_data_size) {
90 			eth_dev->data->dev_private =
91 				rte_zmalloc_socket(name, private_data_size,
92 						     RTE_CACHE_LINE_SIZE, dev->device.numa_node);
93 			if (!eth_dev->data->dev_private) {
94 				PMD_DRV_LOG(NOTICE, "can not allocate driver data");
95 				rte_eth_dev_release_port(eth_dev);
96 				return NULL;
97 			}
98 		}
99 	} else {
100 		eth_dev = rte_eth_dev_attach_secondary(name);
101 		if (!eth_dev) {
102 			PMD_DRV_LOG(NOTICE, "can not attach secondary");
103 			return NULL;
104 		}
105 	}
106 
107 	eth_dev->device = &dev->device;
108 	eth_dev->intr_handle = &dev->intr_handle;
109 
110 	return eth_dev;
111 }
112 
113 static void
114 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
115 {
116 	/* free ether device */
117 	rte_eth_dev_release_port(eth_dev);
118 
119 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
120 		rte_free(eth_dev->data->dev_private);
121 
122 	eth_dev->data->dev_private = NULL;
123 
124 	/*
125 	 * Secondary process will check the name to attach.
126 	 * Clear this field to avoid attaching a released ports.
127 	 */
128 	eth_dev->data->name[0] = '\0';
129 
130 	eth_dev->device = NULL;
131 	eth_dev->intr_handle = NULL;
132 }
133 
134 /* Update link status.
135  * Note: the DPDK definition of "wait_to_complete"
136  *   means block this call until link is up.
137  *   which is not worth supporting.
138  */
139 static int
140 hn_dev_link_update(struct rte_eth_dev *dev,
141 		   __rte_unused int wait_to_complete)
142 {
143 	struct hn_data *hv = dev->data->dev_private;
144 	struct rte_eth_link link, old;
145 	int error;
146 
147 	old = dev->data->dev_link;
148 
149 	error = hn_rndis_get_linkstatus(hv);
150 	if (error)
151 		return error;
152 
153 	hn_rndis_get_linkspeed(hv);
154 
155 	link = (struct rte_eth_link) {
156 		.link_duplex = ETH_LINK_FULL_DUPLEX,
157 		.link_autoneg = ETH_LINK_SPEED_FIXED,
158 		.link_speed = hv->link_speed / 10000,
159 	};
160 
161 	if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
162 		link.link_status = ETH_LINK_UP;
163 	else
164 		link.link_status = ETH_LINK_DOWN;
165 
166 	if (old.link_status == link.link_status)
167 		return 0;
168 
169 	PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
170 		     (link.link_status == ETH_LINK_UP) ? "up" : "down");
171 
172 	return rte_eth_linkstatus_set(dev, &link);
173 }
174 
175 static void hn_dev_info_get(struct rte_eth_dev *dev,
176 			    struct rte_eth_dev_info *dev_info)
177 {
178 	struct hn_data *hv = dev->data->dev_private;
179 
180 	dev_info->speed_capa = ETH_LINK_SPEED_10G;
181 	dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
182 	dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
183 	dev_info->max_mac_addrs  = 1;
184 
185 	dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
186 	dev_info->flow_type_rss_offloads =
187 		ETH_RSS_IPV4 | ETH_RSS_IPV6 | ETH_RSS_TCP | ETH_RSS_UDP;
188 
189 	dev_info->max_rx_queues = hv->max_queues;
190 	dev_info->max_tx_queues = hv->max_queues;
191 
192 	hn_rndis_get_offload(hv, dev_info);
193 }
194 
195 static void
196 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
197 {
198 	struct hn_data *hv = dev->data->dev_private;
199 
200 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
201 }
202 
203 static void
204 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
205 {
206 	struct hn_data *hv = dev->data->dev_private;
207 	uint32_t filter;
208 
209 	filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
210 	if (dev->data->all_multicast)
211 		filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
212 	hn_rndis_set_rxfilter(hv, filter);
213 }
214 
215 static void
216 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
217 {
218 	struct hn_data *hv = dev->data->dev_private;
219 
220 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
221 			      NDIS_PACKET_TYPE_ALL_MULTICAST |
222 			NDIS_PACKET_TYPE_BROADCAST);
223 }
224 
225 static void
226 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
227 {
228 	struct hn_data *hv = dev->data->dev_private;
229 
230 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
231 			     NDIS_PACKET_TYPE_BROADCAST);
232 }
233 
234 /* Setup shared rx/tx queue data */
235 static int hn_subchan_configure(struct hn_data *hv,
236 				uint32_t subchan)
237 {
238 	struct vmbus_channel *primary = hn_primary_chan(hv);
239 	int err;
240 	unsigned int retry = 0;
241 
242 	PMD_DRV_LOG(DEBUG,
243 		    "open %u subchannels", subchan);
244 
245 	/* Send create sub channels command */
246 	err = hn_nvs_alloc_subchans(hv, &subchan);
247 	if (err)
248 		return  err;
249 
250 	while (subchan > 0) {
251 		struct vmbus_channel *new_sc;
252 		uint16_t chn_index;
253 
254 		err = rte_vmbus_subchan_open(primary, &new_sc);
255 		if (err == -ENOENT && ++retry < 1000) {
256 			/* This can happen if not ready yet */
257 			rte_delay_ms(10);
258 			continue;
259 		}
260 
261 		if (err) {
262 			PMD_DRV_LOG(ERR,
263 				    "open subchannel failed: %d", err);
264 			return err;
265 		}
266 
267 		retry = 0;
268 		chn_index = rte_vmbus_sub_channel_index(new_sc);
269 		if (chn_index == 0 || chn_index > hv->max_queues) {
270 			PMD_DRV_LOG(ERR,
271 				    "Invalid subchannel offermsg channel %u",
272 				    chn_index);
273 			return -EIO;
274 		}
275 
276 		PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
277 		hv->channels[chn_index] = new_sc;
278 		--subchan;
279 	}
280 
281 	return err;
282 }
283 
284 static int hn_dev_configure(struct rte_eth_dev *dev)
285 {
286 	const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
287 	const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
288 	const struct rte_eth_txmode *txmode = &dev_conf->txmode;
289 
290 	const struct rte_eth_rss_conf *rss_conf =
291 		&dev_conf->rx_adv_conf.rss_conf;
292 	struct hn_data *hv = dev->data->dev_private;
293 	uint64_t unsupported;
294 	int err, subchan;
295 
296 	PMD_INIT_FUNC_TRACE();
297 
298 	unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
299 	if (unsupported) {
300 		PMD_DRV_LOG(NOTICE,
301 			    "unsupported TX offload: %#" PRIx64,
302 			    unsupported);
303 		return -EINVAL;
304 	}
305 
306 	unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
307 	if (unsupported) {
308 		PMD_DRV_LOG(NOTICE,
309 			    "unsupported RX offload: %#" PRIx64,
310 			    rxmode->offloads);
311 		return -EINVAL;
312 	}
313 
314 	err = hn_rndis_conf_offload(hv, txmode->offloads,
315 				    rxmode->offloads);
316 	if (err) {
317 		PMD_DRV_LOG(NOTICE,
318 			    "offload configure failed");
319 		return err;
320 	}
321 
322 	hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
323 				 dev->data->nb_tx_queues);
324 	subchan = hv->num_queues - 1;
325 	if (subchan > 0) {
326 		err = hn_subchan_configure(hv, subchan);
327 		if (err) {
328 			PMD_DRV_LOG(NOTICE,
329 				    "subchannel configuration failed");
330 			return err;
331 		}
332 
333 		err = hn_rndis_conf_rss(hv, rss_conf);
334 		if (err) {
335 			PMD_DRV_LOG(NOTICE,
336 				    "rss configuration failed");
337 			return err;
338 		}
339 	}
340 
341 	return 0;
342 }
343 
344 static int hn_dev_stats_get(struct rte_eth_dev *dev,
345 			    struct rte_eth_stats *stats)
346 {
347 	unsigned int i;
348 
349 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
350 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
351 
352 		if (!txq)
353 			continue;
354 
355 		stats->opackets += txq->stats.packets;
356 		stats->obytes += txq->stats.bytes;
357 		stats->oerrors += txq->stats.errors + txq->stats.nomemory;
358 
359 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
360 			stats->q_opackets[i] = txq->stats.packets;
361 			stats->q_obytes[i] = txq->stats.bytes;
362 		}
363 	}
364 
365 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
366 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
367 
368 		if (!rxq)
369 			continue;
370 
371 		stats->ipackets += rxq->stats.packets;
372 		stats->ibytes += rxq->stats.bytes;
373 		stats->ierrors += rxq->stats.errors;
374 		stats->imissed += rxq->ring_full;
375 
376 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
377 			stats->q_ipackets[i] = rxq->stats.packets;
378 			stats->q_ibytes[i] = rxq->stats.bytes;
379 		}
380 	}
381 
382 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
383 	return 0;
384 }
385 
386 static void
387 hn_dev_stats_reset(struct rte_eth_dev *dev)
388 {
389 	unsigned int i;
390 
391 	PMD_INIT_FUNC_TRACE();
392 
393 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
394 		struct hn_tx_queue *txq = dev->data->tx_queues[i];
395 
396 		if (!txq)
397 			continue;
398 		memset(&txq->stats, 0, sizeof(struct hn_stats));
399 	}
400 
401 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
402 		struct hn_rx_queue *rxq = dev->data->rx_queues[i];
403 
404 		if (!rxq)
405 			continue;
406 
407 		memset(&rxq->stats, 0, sizeof(struct hn_stats));
408 		rxq->ring_full = 0;
409 	}
410 }
411 
412 static int
413 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
414 			struct rte_eth_xstat_name *xstats_names,
415 			__rte_unused unsigned int limit)
416 {
417 	unsigned int i, t, count = 0;
418 
419 	PMD_INIT_FUNC_TRACE();
420 
421 	if (!xstats_names)
422 		return dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
423 			+ dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
424 
425 	/* Note: limit checked in rte_eth_xstats_names() */
426 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
427 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
428 
429 		if (!txq)
430 			continue;
431 
432 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
433 			snprintf(xstats_names[count++].name,
434 				 RTE_ETH_XSTATS_NAME_SIZE,
435 				 "tx_q%u_%s", i, hn_stat_strings[t].name);
436 	}
437 
438 	for (i = 0; i < dev->data->nb_rx_queues; i++)  {
439 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
440 
441 		if (!rxq)
442 			continue;
443 
444 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
445 			snprintf(xstats_names[count++].name,
446 				 RTE_ETH_XSTATS_NAME_SIZE,
447 				 "rx_q%u_%s", i,
448 				 hn_stat_strings[t].name);
449 	}
450 
451 	return count;
452 }
453 
454 static int
455 hn_dev_xstats_get(struct rte_eth_dev *dev,
456 		  struct rte_eth_xstat *xstats,
457 		  unsigned int n)
458 {
459 	unsigned int i, t, count = 0;
460 
461 	const unsigned int nstats =
462 		dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
463 		+ dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
464 	const char *stats;
465 
466 	PMD_INIT_FUNC_TRACE();
467 
468 	if (n < nstats)
469 		return nstats;
470 
471 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
472 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
473 
474 		if (!txq)
475 			continue;
476 
477 		stats = (const char *)&txq->stats;
478 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
479 			xstats[count++].value = *(const uint64_t *)
480 				(stats + hn_stat_strings[t].offset);
481 	}
482 
483 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
484 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
485 
486 		if (!rxq)
487 			continue;
488 
489 		stats = (const char *)&rxq->stats;
490 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
491 			xstats[count++].value = *(const uint64_t *)
492 				(stats + hn_stat_strings[t].offset);
493 	}
494 
495 	return count;
496 }
497 
498 static int
499 hn_dev_start(struct rte_eth_dev *dev)
500 {
501 	struct hn_data *hv = dev->data->dev_private;
502 
503 	PMD_INIT_FUNC_TRACE();
504 
505 	/* check if lsc interrupt feature is enabled */
506 	if (dev->data->dev_conf.intr_conf.lsc) {
507 		PMD_DRV_LOG(ERR, "link status not supported yet");
508 		return -ENOTSUP;
509 	}
510 
511 	return hn_rndis_set_rxfilter(hv,
512 				     NDIS_PACKET_TYPE_BROADCAST |
513 				     NDIS_PACKET_TYPE_ALL_MULTICAST |
514 				     NDIS_PACKET_TYPE_DIRECTED);
515 }
516 
517 static void
518 hn_dev_stop(struct rte_eth_dev *dev)
519 {
520 	struct hn_data *hv = dev->data->dev_private;
521 
522 	PMD_INIT_FUNC_TRACE();
523 
524 	hn_rndis_set_rxfilter(hv, 0);
525 }
526 
527 static void
528 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
529 {
530 	PMD_INIT_LOG(DEBUG, "close");
531 }
532 
533 static const struct eth_dev_ops hn_eth_dev_ops = {
534 	.dev_configure		= hn_dev_configure,
535 	.dev_start		= hn_dev_start,
536 	.dev_stop		= hn_dev_stop,
537 	.dev_close		= hn_dev_close,
538 	.dev_infos_get		= hn_dev_info_get,
539 	.promiscuous_enable     = hn_dev_promiscuous_enable,
540 	.promiscuous_disable    = hn_dev_promiscuous_disable,
541 	.allmulticast_enable    = hn_dev_allmulticast_enable,
542 	.allmulticast_disable   = hn_dev_allmulticast_disable,
543 	.tx_queue_setup		= hn_dev_tx_queue_setup,
544 	.tx_queue_release	= hn_dev_tx_queue_release,
545 	.rx_queue_setup		= hn_dev_rx_queue_setup,
546 	.rx_queue_release	= hn_dev_rx_queue_release,
547 	.link_update		= hn_dev_link_update,
548 	.stats_get		= hn_dev_stats_get,
549 	.xstats_get		= hn_dev_xstats_get,
550 	.xstats_get_names	= hn_dev_xstats_get_names,
551 	.stats_reset            = hn_dev_stats_reset,
552 	.xstats_reset		= hn_dev_stats_reset,
553 };
554 
555 /*
556  * Setup connection between PMD and kernel.
557  */
558 static int
559 hn_attach(struct hn_data *hv, unsigned int mtu)
560 {
561 	int error;
562 
563 	/* Attach NVS */
564 	error = hn_nvs_attach(hv, mtu);
565 	if (error)
566 		goto failed_nvs;
567 
568 	/* Attach RNDIS */
569 	error = hn_rndis_attach(hv);
570 	if (error)
571 		goto failed_rndis;
572 
573 	/*
574 	 * NOTE:
575 	 * Under certain conditions on certain versions of Hyper-V,
576 	 * the RNDIS rxfilter is _not_ zero on the hypervisor side
577 	 * after the successful RNDIS initialization.
578 	 */
579 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
580 	return 0;
581 failed_rndis:
582 	hn_nvs_detach(hv);
583 failed_nvs:
584 	return error;
585 }
586 
587 static void
588 hn_detach(struct hn_data *hv)
589 {
590 	hn_nvs_detach(hv);
591 	hn_rndis_detach(hv);
592 }
593 
594 static int
595 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
596 {
597 	struct hn_data *hv = eth_dev->data->dev_private;
598 	struct rte_device *device = eth_dev->device;
599 	struct rte_vmbus_device *vmbus;
600 	unsigned int rxr_cnt;
601 	int err, max_chan;
602 
603 	PMD_INIT_FUNC_TRACE();
604 
605 	vmbus = container_of(device, struct rte_vmbus_device, device);
606 	eth_dev->dev_ops = &hn_eth_dev_ops;
607 	eth_dev->tx_pkt_burst = &hn_xmit_pkts;
608 	eth_dev->rx_pkt_burst = &hn_recv_pkts;
609 
610 	/*
611 	 * for secondary processes, we don't initialize any further as primary
612 	 * has already done this work.
613 	 */
614 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
615 		return 0;
616 
617 	/* Since Hyper-V only supports one MAC address, just use local data */
618 	eth_dev->data->mac_addrs = &hv->mac_addr;
619 
620 	hv->vmbus = vmbus;
621 	hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
622 	hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
623 	hv->port_id = eth_dev->data->port_id;
624 
625 	/* Initialize primary channel input for control operations */
626 	err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
627 	if (err)
628 		return err;
629 
630 	hv->primary = hn_rx_queue_alloc(hv, 0,
631 					eth_dev->device->numa_node);
632 
633 	if (!hv->primary)
634 		return -ENOMEM;
635 
636 	err = hn_attach(hv, ETHER_MTU);
637 	if  (err)
638 		goto failed;
639 
640 	err = hn_tx_pool_init(eth_dev);
641 	if (err)
642 		goto failed;
643 
644 	err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
645 	if (err)
646 		goto failed;
647 
648 	max_chan = rte_vmbus_max_channels(vmbus);
649 	PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
650 	if (max_chan <= 0)
651 		goto failed;
652 
653 	if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
654 		rxr_cnt = 1;
655 
656 	hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
657 
658 	return 0;
659 
660 failed:
661 	PMD_INIT_LOG(NOTICE, "device init failed");
662 
663 	hn_detach(hv);
664 	return err;
665 }
666 
667 static int
668 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
669 {
670 	struct hn_data *hv = eth_dev->data->dev_private;
671 
672 	PMD_INIT_FUNC_TRACE();
673 
674 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
675 		return 0;
676 
677 	hn_dev_stop(eth_dev);
678 	hn_dev_close(eth_dev);
679 
680 	eth_dev->dev_ops = NULL;
681 	eth_dev->tx_pkt_burst = NULL;
682 	eth_dev->rx_pkt_burst = NULL;
683 
684 	hn_detach(hv);
685 	rte_vmbus_chan_close(hv->primary->chan);
686 	rte_free(hv->primary);
687 
688 	eth_dev->data->mac_addrs = NULL;
689 
690 	return 0;
691 }
692 
693 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
694 			struct rte_vmbus_device *dev)
695 {
696 	struct rte_eth_dev *eth_dev;
697 	int ret;
698 
699 	PMD_INIT_FUNC_TRACE();
700 
701 	eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
702 	if (!eth_dev)
703 		return -ENOMEM;
704 
705 	ret = eth_hn_dev_init(eth_dev);
706 	if (ret)
707 		eth_dev_vmbus_release(eth_dev);
708 	else
709 		rte_eth_dev_probing_finish(eth_dev);
710 
711 	return ret;
712 }
713 
714 static int eth_hn_remove(struct rte_vmbus_device *dev)
715 {
716 	struct rte_eth_dev *eth_dev;
717 	int ret;
718 
719 	PMD_INIT_FUNC_TRACE();
720 
721 	eth_dev = rte_eth_dev_allocated(dev->device.name);
722 	if (!eth_dev)
723 		return -ENODEV;
724 
725 	ret = eth_hn_dev_uninit(eth_dev);
726 	if (ret)
727 		return ret;
728 
729 	eth_dev_vmbus_release(eth_dev);
730 	return 0;
731 }
732 
733 /* Network device GUID */
734 static const rte_uuid_t hn_net_ids[] = {
735 	/*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
736 	RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
737 	{ 0 }
738 };
739 
740 static struct rte_vmbus_driver rte_netvsc_pmd = {
741 	.id_table = hn_net_ids,
742 	.probe = eth_hn_probe,
743 	.remove = eth_hn_remove,
744 };
745 
746 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
747 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
748 
749 RTE_INIT(hn_init_log);
750 static void
751 hn_init_log(void)
752 {
753 	hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
754 	if (hn_logtype_init >= 0)
755 		rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
756 	hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
757 	if (hn_logtype_driver >= 0)
758 		rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
759 }
760