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