xref: /dpdk/drivers/net/netvsc/hn_ethdev.c (revision b733c60f68f12e064359b27e630305c541a3fbdf)
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 		rte_vmbus_set_latency(hv->vmbus, new_sc,
268 				      HN_CHAN_LATENCY_NS);
269 
270 		retry = 0;
271 		chn_index = rte_vmbus_sub_channel_index(new_sc);
272 		if (chn_index == 0 || chn_index > hv->max_queues) {
273 			PMD_DRV_LOG(ERR,
274 				    "Invalid subchannel offermsg channel %u",
275 				    chn_index);
276 			return -EIO;
277 		}
278 
279 		PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
280 		hv->channels[chn_index] = new_sc;
281 		--subchan;
282 	}
283 
284 	return err;
285 }
286 
287 static int hn_dev_configure(struct rte_eth_dev *dev)
288 {
289 	const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
290 	const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
291 	const struct rte_eth_txmode *txmode = &dev_conf->txmode;
292 
293 	const struct rte_eth_rss_conf *rss_conf =
294 		&dev_conf->rx_adv_conf.rss_conf;
295 	struct hn_data *hv = dev->data->dev_private;
296 	uint64_t unsupported;
297 	int err, subchan;
298 
299 	PMD_INIT_FUNC_TRACE();
300 
301 	unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
302 	if (unsupported) {
303 		PMD_DRV_LOG(NOTICE,
304 			    "unsupported TX offload: %#" PRIx64,
305 			    unsupported);
306 		return -EINVAL;
307 	}
308 
309 	unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
310 	if (unsupported) {
311 		PMD_DRV_LOG(NOTICE,
312 			    "unsupported RX offload: %#" PRIx64,
313 			    rxmode->offloads);
314 		return -EINVAL;
315 	}
316 
317 	err = hn_rndis_conf_offload(hv, txmode->offloads,
318 				    rxmode->offloads);
319 	if (err) {
320 		PMD_DRV_LOG(NOTICE,
321 			    "offload configure failed");
322 		return err;
323 	}
324 
325 	hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
326 				 dev->data->nb_tx_queues);
327 	subchan = hv->num_queues - 1;
328 	if (subchan > 0) {
329 		err = hn_subchan_configure(hv, subchan);
330 		if (err) {
331 			PMD_DRV_LOG(NOTICE,
332 				    "subchannel configuration failed");
333 			return err;
334 		}
335 
336 		err = hn_rndis_conf_rss(hv, rss_conf);
337 		if (err) {
338 			PMD_DRV_LOG(NOTICE,
339 				    "rss configuration failed");
340 			return err;
341 		}
342 	}
343 
344 	return 0;
345 }
346 
347 static int hn_dev_stats_get(struct rte_eth_dev *dev,
348 			    struct rte_eth_stats *stats)
349 {
350 	unsigned int i;
351 
352 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
353 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
354 
355 		if (!txq)
356 			continue;
357 
358 		stats->opackets += txq->stats.packets;
359 		stats->obytes += txq->stats.bytes;
360 		stats->oerrors += txq->stats.errors + txq->stats.nomemory;
361 
362 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
363 			stats->q_opackets[i] = txq->stats.packets;
364 			stats->q_obytes[i] = txq->stats.bytes;
365 		}
366 	}
367 
368 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
369 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
370 
371 		if (!rxq)
372 			continue;
373 
374 		stats->ipackets += rxq->stats.packets;
375 		stats->ibytes += rxq->stats.bytes;
376 		stats->ierrors += rxq->stats.errors;
377 		stats->imissed += rxq->ring_full;
378 
379 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
380 			stats->q_ipackets[i] = rxq->stats.packets;
381 			stats->q_ibytes[i] = rxq->stats.bytes;
382 		}
383 	}
384 
385 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
386 	return 0;
387 }
388 
389 static void
390 hn_dev_stats_reset(struct rte_eth_dev *dev)
391 {
392 	unsigned int i;
393 
394 	PMD_INIT_FUNC_TRACE();
395 
396 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
397 		struct hn_tx_queue *txq = dev->data->tx_queues[i];
398 
399 		if (!txq)
400 			continue;
401 		memset(&txq->stats, 0, sizeof(struct hn_stats));
402 	}
403 
404 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
405 		struct hn_rx_queue *rxq = dev->data->rx_queues[i];
406 
407 		if (!rxq)
408 			continue;
409 
410 		memset(&rxq->stats, 0, sizeof(struct hn_stats));
411 		rxq->ring_full = 0;
412 	}
413 }
414 
415 static int
416 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
417 			struct rte_eth_xstat_name *xstats_names,
418 			__rte_unused unsigned int limit)
419 {
420 	unsigned int i, t, count = 0;
421 
422 	PMD_INIT_FUNC_TRACE();
423 
424 	if (!xstats_names)
425 		return dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
426 			+ dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
427 
428 	/* Note: limit checked in rte_eth_xstats_names() */
429 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
430 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
431 
432 		if (!txq)
433 			continue;
434 
435 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
436 			snprintf(xstats_names[count++].name,
437 				 RTE_ETH_XSTATS_NAME_SIZE,
438 				 "tx_q%u_%s", i, hn_stat_strings[t].name);
439 	}
440 
441 	for (i = 0; i < dev->data->nb_rx_queues; i++)  {
442 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
443 
444 		if (!rxq)
445 			continue;
446 
447 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
448 			snprintf(xstats_names[count++].name,
449 				 RTE_ETH_XSTATS_NAME_SIZE,
450 				 "rx_q%u_%s", i,
451 				 hn_stat_strings[t].name);
452 	}
453 
454 	return count;
455 }
456 
457 static int
458 hn_dev_xstats_get(struct rte_eth_dev *dev,
459 		  struct rte_eth_xstat *xstats,
460 		  unsigned int n)
461 {
462 	unsigned int i, t, count = 0;
463 
464 	const unsigned int nstats =
465 		dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
466 		+ dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
467 	const char *stats;
468 
469 	PMD_INIT_FUNC_TRACE();
470 
471 	if (n < nstats)
472 		return nstats;
473 
474 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
475 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
476 
477 		if (!txq)
478 			continue;
479 
480 		stats = (const char *)&txq->stats;
481 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
482 			xstats[count++].value = *(const uint64_t *)
483 				(stats + hn_stat_strings[t].offset);
484 	}
485 
486 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
487 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
488 
489 		if (!rxq)
490 			continue;
491 
492 		stats = (const char *)&rxq->stats;
493 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
494 			xstats[count++].value = *(const uint64_t *)
495 				(stats + hn_stat_strings[t].offset);
496 	}
497 
498 	return count;
499 }
500 
501 static int
502 hn_dev_start(struct rte_eth_dev *dev)
503 {
504 	struct hn_data *hv = dev->data->dev_private;
505 
506 	PMD_INIT_FUNC_TRACE();
507 
508 	/* check if lsc interrupt feature is enabled */
509 	if (dev->data->dev_conf.intr_conf.lsc) {
510 		PMD_DRV_LOG(ERR, "link status not supported yet");
511 		return -ENOTSUP;
512 	}
513 
514 	return hn_rndis_set_rxfilter(hv,
515 				     NDIS_PACKET_TYPE_BROADCAST |
516 				     NDIS_PACKET_TYPE_ALL_MULTICAST |
517 				     NDIS_PACKET_TYPE_DIRECTED);
518 }
519 
520 static void
521 hn_dev_stop(struct rte_eth_dev *dev)
522 {
523 	struct hn_data *hv = dev->data->dev_private;
524 
525 	PMD_INIT_FUNC_TRACE();
526 
527 	hn_rndis_set_rxfilter(hv, 0);
528 }
529 
530 static void
531 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
532 {
533 	PMD_INIT_LOG(DEBUG, "close");
534 }
535 
536 static const struct eth_dev_ops hn_eth_dev_ops = {
537 	.dev_configure		= hn_dev_configure,
538 	.dev_start		= hn_dev_start,
539 	.dev_stop		= hn_dev_stop,
540 	.dev_close		= hn_dev_close,
541 	.dev_infos_get		= hn_dev_info_get,
542 	.txq_info_get		= hn_dev_tx_queue_info,
543 	.rxq_info_get		= hn_dev_rx_queue_info,
544 	.promiscuous_enable     = hn_dev_promiscuous_enable,
545 	.promiscuous_disable    = hn_dev_promiscuous_disable,
546 	.allmulticast_enable    = hn_dev_allmulticast_enable,
547 	.allmulticast_disable   = hn_dev_allmulticast_disable,
548 	.tx_queue_setup		= hn_dev_tx_queue_setup,
549 	.tx_queue_release	= hn_dev_tx_queue_release,
550 	.tx_done_cleanup        = hn_dev_tx_done_cleanup,
551 	.rx_queue_setup		= hn_dev_rx_queue_setup,
552 	.rx_queue_release	= hn_dev_rx_queue_release,
553 	.link_update		= hn_dev_link_update,
554 	.stats_get		= hn_dev_stats_get,
555 	.xstats_get		= hn_dev_xstats_get,
556 	.xstats_get_names	= hn_dev_xstats_get_names,
557 	.stats_reset            = hn_dev_stats_reset,
558 	.xstats_reset		= hn_dev_stats_reset,
559 };
560 
561 /*
562  * Setup connection between PMD and kernel.
563  */
564 static int
565 hn_attach(struct hn_data *hv, unsigned int mtu)
566 {
567 	int error;
568 
569 	/* Attach NVS */
570 	error = hn_nvs_attach(hv, mtu);
571 	if (error)
572 		goto failed_nvs;
573 
574 	/* Attach RNDIS */
575 	error = hn_rndis_attach(hv);
576 	if (error)
577 		goto failed_rndis;
578 
579 	/*
580 	 * NOTE:
581 	 * Under certain conditions on certain versions of Hyper-V,
582 	 * the RNDIS rxfilter is _not_ zero on the hypervisor side
583 	 * after the successful RNDIS initialization.
584 	 */
585 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
586 	return 0;
587 failed_rndis:
588 	hn_nvs_detach(hv);
589 failed_nvs:
590 	return error;
591 }
592 
593 static void
594 hn_detach(struct hn_data *hv)
595 {
596 	hn_nvs_detach(hv);
597 	hn_rndis_detach(hv);
598 }
599 
600 static int
601 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
602 {
603 	struct hn_data *hv = eth_dev->data->dev_private;
604 	struct rte_device *device = eth_dev->device;
605 	struct rte_vmbus_device *vmbus;
606 	unsigned int rxr_cnt;
607 	int err, max_chan;
608 
609 	PMD_INIT_FUNC_TRACE();
610 
611 	vmbus = container_of(device, struct rte_vmbus_device, device);
612 	eth_dev->dev_ops = &hn_eth_dev_ops;
613 	eth_dev->tx_pkt_burst = &hn_xmit_pkts;
614 	eth_dev->rx_pkt_burst = &hn_recv_pkts;
615 
616 	/*
617 	 * for secondary processes, we don't initialize any further as primary
618 	 * has already done this work.
619 	 */
620 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
621 		return 0;
622 
623 	/* Since Hyper-V only supports one MAC address, just use local data */
624 	eth_dev->data->mac_addrs = &hv->mac_addr;
625 
626 	hv->vmbus = vmbus;
627 	hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
628 	hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
629 	hv->port_id = eth_dev->data->port_id;
630 
631 	/* Initialize primary channel input for control operations */
632 	err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
633 	if (err)
634 		return err;
635 
636 	rte_vmbus_set_latency(hv->vmbus, hv->channels[0],
637 			      HN_CHAN_LATENCY_NS);
638 
639 	hv->primary = hn_rx_queue_alloc(hv, 0,
640 					eth_dev->device->numa_node);
641 
642 	if (!hv->primary)
643 		return -ENOMEM;
644 
645 	err = hn_attach(hv, ETHER_MTU);
646 	if  (err)
647 		goto failed;
648 
649 	err = hn_tx_pool_init(eth_dev);
650 	if (err)
651 		goto failed;
652 
653 	err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
654 	if (err)
655 		goto failed;
656 
657 	max_chan = rte_vmbus_max_channels(vmbus);
658 	PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
659 	if (max_chan <= 0)
660 		goto failed;
661 
662 	if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
663 		rxr_cnt = 1;
664 
665 	hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
666 
667 	return 0;
668 
669 failed:
670 	PMD_INIT_LOG(NOTICE, "device init failed");
671 
672 	hn_detach(hv);
673 	return err;
674 }
675 
676 static int
677 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
678 {
679 	struct hn_data *hv = eth_dev->data->dev_private;
680 
681 	PMD_INIT_FUNC_TRACE();
682 
683 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
684 		return 0;
685 
686 	hn_dev_stop(eth_dev);
687 	hn_dev_close(eth_dev);
688 
689 	eth_dev->dev_ops = NULL;
690 	eth_dev->tx_pkt_burst = NULL;
691 	eth_dev->rx_pkt_burst = NULL;
692 
693 	hn_detach(hv);
694 	rte_vmbus_chan_close(hv->primary->chan);
695 	rte_free(hv->primary);
696 
697 	eth_dev->data->mac_addrs = NULL;
698 
699 	return 0;
700 }
701 
702 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
703 			struct rte_vmbus_device *dev)
704 {
705 	struct rte_eth_dev *eth_dev;
706 	int ret;
707 
708 	PMD_INIT_FUNC_TRACE();
709 
710 	eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
711 	if (!eth_dev)
712 		return -ENOMEM;
713 
714 	ret = eth_hn_dev_init(eth_dev);
715 	if (ret)
716 		eth_dev_vmbus_release(eth_dev);
717 	else
718 		rte_eth_dev_probing_finish(eth_dev);
719 
720 	return ret;
721 }
722 
723 static int eth_hn_remove(struct rte_vmbus_device *dev)
724 {
725 	struct rte_eth_dev *eth_dev;
726 	int ret;
727 
728 	PMD_INIT_FUNC_TRACE();
729 
730 	eth_dev = rte_eth_dev_allocated(dev->device.name);
731 	if (!eth_dev)
732 		return -ENODEV;
733 
734 	ret = eth_hn_dev_uninit(eth_dev);
735 	if (ret)
736 		return ret;
737 
738 	eth_dev_vmbus_release(eth_dev);
739 	return 0;
740 }
741 
742 /* Network device GUID */
743 static const rte_uuid_t hn_net_ids[] = {
744 	/*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
745 	RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
746 	{ 0 }
747 };
748 
749 static struct rte_vmbus_driver rte_netvsc_pmd = {
750 	.id_table = hn_net_ids,
751 	.probe = eth_hn_probe,
752 	.remove = eth_hn_remove,
753 };
754 
755 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
756 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
757 
758 RTE_INIT(hn_init_log);
759 static void
760 hn_init_log(void)
761 {
762 	hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
763 	if (hn_logtype_init >= 0)
764 		rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
765 	hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
766 	if (hn_logtype_driver >= 0)
767 		rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
768 }
769