xref: /dpdk/drivers/net/netvsc/hn_ethdev.c (revision bc8e32473cc3978d763a1387eaa8244bcf75e77d)
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 			    DEV_RX_OFFLOAD_RSS_HASH)
47 
48 #define NETVSC_ARG_LATENCY "latency"
49 #define NETVSC_ARG_RXBREAK "rx_copybreak"
50 #define NETVSC_ARG_TXBREAK "tx_copybreak"
51 #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable"
52 
53 struct hn_xstats_name_off {
54 	char name[RTE_ETH_XSTATS_NAME_SIZE];
55 	unsigned int offset;
56 };
57 
58 static const struct hn_xstats_name_off hn_stat_strings[] = {
59 	{ "good_packets",           offsetof(struct hn_stats, packets) },
60 	{ "good_bytes",             offsetof(struct hn_stats, bytes) },
61 	{ "errors",                 offsetof(struct hn_stats, errors) },
62 	{ "ring full",              offsetof(struct hn_stats, ring_full) },
63 	{ "channel full",           offsetof(struct hn_stats, channel_full) },
64 	{ "multicast_packets",      offsetof(struct hn_stats, multicast) },
65 	{ "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
66 	{ "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
67 	{ "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
68 	{ "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
69 	{ "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
70 	{ "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
71 	{ "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
72 	{ "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
73 	{ "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
74 };
75 
76 /* The default RSS key.
77  * This value is the same as MLX5 so that flows will be
78  * received on same path for both VF and synthetic NIC.
79  */
80 static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
81 	0x2c, 0xc6, 0x81, 0xd1,	0x5b, 0xdb, 0xf4, 0xf7,
82 	0xfc, 0xa2, 0x83, 0x19,	0xdb, 0x1a, 0x3e, 0x94,
83 	0x6b, 0x9e, 0x38, 0xd9,	0x2c, 0x9c, 0x03, 0xd1,
84 	0xad, 0x99, 0x44, 0xa7,	0xd9, 0x56, 0x3d, 0x59,
85 	0x06, 0x3c, 0x25, 0xf3,	0xfc, 0x1f, 0xdc, 0x2a,
86 };
87 
88 static struct rte_eth_dev *
89 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
90 {
91 	struct rte_eth_dev *eth_dev;
92 	const char *name;
93 
94 	if (!dev)
95 		return NULL;
96 
97 	name = dev->device.name;
98 
99 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
100 		eth_dev = rte_eth_dev_allocate(name);
101 		if (!eth_dev) {
102 			PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
103 			return NULL;
104 		}
105 
106 		if (private_data_size) {
107 			eth_dev->data->dev_private =
108 				rte_zmalloc_socket(name, private_data_size,
109 						     RTE_CACHE_LINE_SIZE, dev->device.numa_node);
110 			if (!eth_dev->data->dev_private) {
111 				PMD_DRV_LOG(NOTICE, "can not allocate driver data");
112 				rte_eth_dev_release_port(eth_dev);
113 				return NULL;
114 			}
115 		}
116 	} else {
117 		eth_dev = rte_eth_dev_attach_secondary(name);
118 		if (!eth_dev) {
119 			PMD_DRV_LOG(NOTICE, "can not attach secondary");
120 			return NULL;
121 		}
122 	}
123 
124 	eth_dev->device = &dev->device;
125 
126 	/* interrupt is simulated */
127 	dev->intr_handle.type = RTE_INTR_HANDLE_EXT;
128 	eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
129 	eth_dev->intr_handle = &dev->intr_handle;
130 
131 	return eth_dev;
132 }
133 
134 static void
135 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
136 {
137 	/* free ether device */
138 	rte_eth_dev_release_port(eth_dev);
139 
140 	eth_dev->device = NULL;
141 	eth_dev->intr_handle = NULL;
142 }
143 
144 static int hn_set_parameter(const char *key, const char *value, void *opaque)
145 {
146 	struct hn_data *hv = opaque;
147 	char *endp = NULL;
148 	unsigned long v;
149 
150 	v = strtoul(value, &endp, 0);
151 	if (*value == '\0' || *endp != '\0') {
152 		PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
153 		return -EINVAL;
154 	}
155 
156 	if (!strcmp(key, NETVSC_ARG_LATENCY)) {
157 		/* usec to nsec */
158 		hv->latency = v * 1000;
159 		PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency);
160 	} else if (!strcmp(key, NETVSC_ARG_RXBREAK)) {
161 		hv->rx_copybreak = v;
162 		PMD_DRV_LOG(DEBUG, "rx copy break set to %u",
163 			    hv->rx_copybreak);
164 	} else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
165 		hv->tx_copybreak = v;
166 		PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
167 			    hv->tx_copybreak);
168 	} else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) {
169 		hv->rx_extmbuf_enable = v;
170 		PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u",
171 			    hv->rx_extmbuf_enable);
172 	}
173 
174 	return 0;
175 }
176 
177 /* Parse device arguments */
178 static int hn_parse_args(const struct rte_eth_dev *dev)
179 {
180 	struct hn_data *hv = dev->data->dev_private;
181 	struct rte_devargs *devargs = dev->device->devargs;
182 	static const char * const valid_keys[] = {
183 		NETVSC_ARG_LATENCY,
184 		NETVSC_ARG_RXBREAK,
185 		NETVSC_ARG_TXBREAK,
186 		NETVSC_ARG_RX_EXTMBUF_ENABLE,
187 		NULL
188 	};
189 	struct rte_kvargs *kvlist;
190 	int ret;
191 
192 	if (!devargs)
193 		return 0;
194 
195 	PMD_INIT_LOG(DEBUG, "device args %s %s",
196 		     devargs->name, devargs->args);
197 
198 	kvlist = rte_kvargs_parse(devargs->args, valid_keys);
199 	if (!kvlist) {
200 		PMD_DRV_LOG(ERR, "invalid parameters");
201 		return -EINVAL;
202 	}
203 
204 	ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
205 	rte_kvargs_free(kvlist);
206 
207 	return ret;
208 }
209 
210 /* Update link status.
211  * Note: the DPDK definition of "wait_to_complete"
212  *   means block this call until link is up.
213  *   which is not worth supporting.
214  */
215 int
216 hn_dev_link_update(struct rte_eth_dev *dev,
217 		   int wait_to_complete __rte_unused)
218 {
219 	struct hn_data *hv = dev->data->dev_private;
220 	struct rte_eth_link link, old;
221 	int error;
222 
223 	old = dev->data->dev_link;
224 
225 	error = hn_rndis_get_linkstatus(hv);
226 	if (error)
227 		return error;
228 
229 	hn_rndis_get_linkspeed(hv);
230 
231 	link = (struct rte_eth_link) {
232 		.link_duplex = ETH_LINK_FULL_DUPLEX,
233 		.link_autoneg = ETH_LINK_SPEED_FIXED,
234 		.link_speed = hv->link_speed / 10000,
235 	};
236 
237 	if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
238 		link.link_status = ETH_LINK_UP;
239 	else
240 		link.link_status = ETH_LINK_DOWN;
241 
242 	if (old.link_status == link.link_status)
243 		return 0;
244 
245 	PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
246 		     (link.link_status == ETH_LINK_UP) ? "up" : "down");
247 
248 	return rte_eth_linkstatus_set(dev, &link);
249 }
250 
251 static int hn_dev_info_get(struct rte_eth_dev *dev,
252 			   struct rte_eth_dev_info *dev_info)
253 {
254 	struct hn_data *hv = dev->data->dev_private;
255 	int rc;
256 
257 	dev_info->speed_capa = ETH_LINK_SPEED_10G;
258 	dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
259 	dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
260 	dev_info->max_mac_addrs  = 1;
261 
262 	dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
263 	dev_info->flow_type_rss_offloads = hv->rss_offloads;
264 	dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
265 
266 	dev_info->max_rx_queues = hv->max_queues;
267 	dev_info->max_tx_queues = hv->max_queues;
268 
269 	dev_info->tx_desc_lim.nb_min = 1;
270 	dev_info->tx_desc_lim.nb_max = 4096;
271 
272 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
273 		return 0;
274 
275 	/* fills in rx and tx offload capability */
276 	rc = hn_rndis_get_offload(hv, dev_info);
277 	if (rc != 0)
278 		return rc;
279 
280 	/* merges the offload and queues of vf */
281 	return hn_vf_info_get(hv, dev_info);
282 }
283 
284 static int hn_rss_reta_update(struct rte_eth_dev *dev,
285 			      struct rte_eth_rss_reta_entry64 *reta_conf,
286 			      uint16_t reta_size)
287 {
288 	struct hn_data *hv = dev->data->dev_private;
289 	unsigned int i;
290 	int err;
291 
292 	PMD_INIT_FUNC_TRACE();
293 
294 	if (reta_size != NDIS_HASH_INDCNT) {
295 		PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
296 		return -EINVAL;
297 	}
298 
299 	for (i = 0; i < NDIS_HASH_INDCNT; i++) {
300 		uint16_t idx = i / RTE_RETA_GROUP_SIZE;
301 		uint16_t shift = i % RTE_RETA_GROUP_SIZE;
302 		uint64_t mask = (uint64_t)1 << shift;
303 
304 		if (reta_conf[idx].mask & mask)
305 			hv->rss_ind[i] = reta_conf[idx].reta[shift];
306 	}
307 
308 	err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
309 	if (err) {
310 		PMD_DRV_LOG(NOTICE,
311 			"rss disable failed");
312 		return err;
313 	}
314 
315 	err = hn_rndis_conf_rss(hv, 0);
316 	if (err) {
317 		PMD_DRV_LOG(NOTICE,
318 			    "reta reconfig failed");
319 		return err;
320 	}
321 
322 	return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
323 }
324 
325 static int hn_rss_reta_query(struct rte_eth_dev *dev,
326 			     struct rte_eth_rss_reta_entry64 *reta_conf,
327 			     uint16_t reta_size)
328 {
329 	struct hn_data *hv = dev->data->dev_private;
330 	unsigned int i;
331 
332 	PMD_INIT_FUNC_TRACE();
333 
334 	if (reta_size != NDIS_HASH_INDCNT) {
335 		PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
336 		return -EINVAL;
337 	}
338 
339 	for (i = 0; i < NDIS_HASH_INDCNT; i++) {
340 		uint16_t idx = i / RTE_RETA_GROUP_SIZE;
341 		uint16_t shift = i % RTE_RETA_GROUP_SIZE;
342 		uint64_t mask = (uint64_t)1 << shift;
343 
344 		if (reta_conf[idx].mask & mask)
345 			reta_conf[idx].reta[shift] = hv->rss_ind[i];
346 	}
347 	return 0;
348 }
349 
350 static void hn_rss_hash_init(struct hn_data *hv,
351 			     const struct rte_eth_rss_conf *rss_conf)
352 {
353 	/* Convert from DPDK RSS hash flags to NDIS hash flags */
354 	hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
355 
356 	if (rss_conf->rss_hf & ETH_RSS_IPV4)
357 		hv->rss_hash |= NDIS_HASH_IPV4;
358 	if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
359 		hv->rss_hash |= NDIS_HASH_TCP_IPV4;
360 	if (rss_conf->rss_hf & ETH_RSS_IPV6)
361 		hv->rss_hash |=  NDIS_HASH_IPV6;
362 	if (rss_conf->rss_hf & ETH_RSS_IPV6_EX)
363 		hv->rss_hash |=  NDIS_HASH_IPV6_EX;
364 	if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
365 		hv->rss_hash |= NDIS_HASH_TCP_IPV6;
366 	if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX)
367 		hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
368 
369 	memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
370 	       NDIS_HASH_KEYSIZE_TOEPLITZ);
371 }
372 
373 static int hn_rss_hash_update(struct rte_eth_dev *dev,
374 			      struct rte_eth_rss_conf *rss_conf)
375 {
376 	struct hn_data *hv = dev->data->dev_private;
377 	int err;
378 
379 	PMD_INIT_FUNC_TRACE();
380 
381 	err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
382 	if (err) {
383 		PMD_DRV_LOG(NOTICE,
384 			    "rss disable failed");
385 		return err;
386 	}
387 
388 	hn_rss_hash_init(hv, rss_conf);
389 
390 	if (rss_conf->rss_hf != 0) {
391 		err = hn_rndis_conf_rss(hv, 0);
392 		if (err) {
393 			PMD_DRV_LOG(NOTICE,
394 				    "rss reconfig failed (RSS disabled)");
395 			return err;
396 		}
397 	}
398 
399 	return hn_vf_rss_hash_update(dev, rss_conf);
400 }
401 
402 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
403 				struct rte_eth_rss_conf *rss_conf)
404 {
405 	struct hn_data *hv = dev->data->dev_private;
406 
407 	PMD_INIT_FUNC_TRACE();
408 
409 	if (hv->ndis_ver < NDIS_VERSION_6_20) {
410 		PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
411 		return -EOPNOTSUPP;
412 	}
413 
414 	rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
415 	if (rss_conf->rss_key)
416 		memcpy(rss_conf->rss_key, hv->rss_key,
417 		       NDIS_HASH_KEYSIZE_TOEPLITZ);
418 
419 	rss_conf->rss_hf = 0;
420 	if (hv->rss_hash & NDIS_HASH_IPV4)
421 		rss_conf->rss_hf |= ETH_RSS_IPV4;
422 
423 	if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
424 		rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
425 
426 	if (hv->rss_hash & NDIS_HASH_IPV6)
427 		rss_conf->rss_hf |= ETH_RSS_IPV6;
428 
429 	if (hv->rss_hash & NDIS_HASH_IPV6_EX)
430 		rss_conf->rss_hf |= ETH_RSS_IPV6_EX;
431 
432 	if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
433 		rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
434 
435 	if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
436 		rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX;
437 
438 	return 0;
439 }
440 
441 static int
442 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
443 {
444 	struct hn_data *hv = dev->data->dev_private;
445 
446 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
447 	return hn_vf_promiscuous_enable(dev);
448 }
449 
450 static int
451 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
452 {
453 	struct hn_data *hv = dev->data->dev_private;
454 	uint32_t filter;
455 
456 	filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
457 	if (dev->data->all_multicast)
458 		filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
459 	hn_rndis_set_rxfilter(hv, filter);
460 	return hn_vf_promiscuous_disable(dev);
461 }
462 
463 static int
464 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
465 {
466 	struct hn_data *hv = dev->data->dev_private;
467 
468 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
469 			      NDIS_PACKET_TYPE_ALL_MULTICAST |
470 			NDIS_PACKET_TYPE_BROADCAST);
471 	return hn_vf_allmulticast_enable(dev);
472 }
473 
474 static int
475 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
476 {
477 	struct hn_data *hv = dev->data->dev_private;
478 
479 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
480 			     NDIS_PACKET_TYPE_BROADCAST);
481 	return hn_vf_allmulticast_disable(dev);
482 }
483 
484 static int
485 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
486 		     struct rte_ether_addr *mc_addr_set,
487 		     uint32_t nb_mc_addr)
488 {
489 	/* No filtering on the synthetic path, but can do it on VF */
490 	return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
491 }
492 
493 /* Setup shared rx/tx queue data */
494 static int hn_subchan_configure(struct hn_data *hv,
495 				uint32_t subchan)
496 {
497 	struct vmbus_channel *primary = hn_primary_chan(hv);
498 	int err;
499 	unsigned int retry = 0;
500 
501 	PMD_DRV_LOG(DEBUG,
502 		    "open %u subchannels", subchan);
503 
504 	/* Send create sub channels command */
505 	err = hn_nvs_alloc_subchans(hv, &subchan);
506 	if (err)
507 		return  err;
508 
509 	while (subchan > 0) {
510 		struct vmbus_channel *new_sc;
511 		uint16_t chn_index;
512 
513 		err = rte_vmbus_subchan_open(primary, &new_sc);
514 		if (err == -ENOENT && ++retry < 1000) {
515 			/* This can happen if not ready yet */
516 			rte_delay_ms(10);
517 			continue;
518 		}
519 
520 		if (err) {
521 			PMD_DRV_LOG(ERR,
522 				    "open subchannel failed: %d", err);
523 			return err;
524 		}
525 
526 		rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
527 
528 		retry = 0;
529 		chn_index = rte_vmbus_sub_channel_index(new_sc);
530 		if (chn_index == 0 || chn_index > hv->max_queues) {
531 			PMD_DRV_LOG(ERR,
532 				    "Invalid subchannel offermsg channel %u",
533 				    chn_index);
534 			return -EIO;
535 		}
536 
537 		PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
538 		hv->channels[chn_index] = new_sc;
539 		--subchan;
540 	}
541 
542 	return err;
543 }
544 
545 static int hn_dev_configure(struct rte_eth_dev *dev)
546 {
547 	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
548 	struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
549 	const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
550 	const struct rte_eth_txmode *txmode = &dev_conf->txmode;
551 	struct hn_data *hv = dev->data->dev_private;
552 	uint64_t unsupported;
553 	int i, err, subchan;
554 
555 	PMD_INIT_FUNC_TRACE();
556 
557 	if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
558 		dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
559 
560 	unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
561 	if (unsupported) {
562 		PMD_DRV_LOG(NOTICE,
563 			    "unsupported TX offload: %#" PRIx64,
564 			    unsupported);
565 		return -EINVAL;
566 	}
567 
568 	unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
569 	if (unsupported) {
570 		PMD_DRV_LOG(NOTICE,
571 			    "unsupported RX offload: %#" PRIx64,
572 			    rxmode->offloads);
573 		return -EINVAL;
574 	}
575 
576 	hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
577 
578 	err = hn_rndis_conf_offload(hv, txmode->offloads,
579 				    rxmode->offloads);
580 	if (err) {
581 		PMD_DRV_LOG(NOTICE,
582 			    "offload configure failed");
583 		return err;
584 	}
585 
586 	hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
587 				 dev->data->nb_tx_queues);
588 
589 	for (i = 0; i < NDIS_HASH_INDCNT; i++)
590 		hv->rss_ind[i] = i % dev->data->nb_rx_queues;
591 
592 	hn_rss_hash_init(hv, rss_conf);
593 
594 	subchan = hv->num_queues - 1;
595 	if (subchan > 0) {
596 		err = hn_subchan_configure(hv, subchan);
597 		if (err) {
598 			PMD_DRV_LOG(NOTICE,
599 				    "subchannel configuration failed");
600 			return err;
601 		}
602 
603 		err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
604 		if (err) {
605 			PMD_DRV_LOG(NOTICE,
606 				"rss disable failed");
607 			return err;
608 		}
609 
610 		if (rss_conf->rss_hf != 0) {
611 			err = hn_rndis_conf_rss(hv, 0);
612 			if (err) {
613 				PMD_DRV_LOG(NOTICE,
614 					    "initial RSS config failed");
615 				return err;
616 			}
617 		}
618 	}
619 
620 	return hn_vf_configure(dev, dev_conf);
621 }
622 
623 static int hn_dev_stats_get(struct rte_eth_dev *dev,
624 			    struct rte_eth_stats *stats)
625 {
626 	unsigned int i;
627 
628 	hn_vf_stats_get(dev, stats);
629 
630 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
631 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
632 
633 		if (!txq)
634 			continue;
635 
636 		stats->opackets += txq->stats.packets;
637 		stats->obytes += txq->stats.bytes;
638 		stats->oerrors += txq->stats.errors;
639 
640 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
641 			stats->q_opackets[i] = txq->stats.packets;
642 			stats->q_obytes[i] = txq->stats.bytes;
643 		}
644 	}
645 
646 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
647 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
648 
649 		if (!rxq)
650 			continue;
651 
652 		stats->ipackets += rxq->stats.packets;
653 		stats->ibytes += rxq->stats.bytes;
654 		stats->ierrors += rxq->stats.errors;
655 		stats->imissed += rxq->stats.ring_full;
656 
657 		if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
658 			stats->q_ipackets[i] = rxq->stats.packets;
659 			stats->q_ibytes[i] = rxq->stats.bytes;
660 		}
661 	}
662 
663 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
664 	return 0;
665 }
666 
667 static int
668 hn_dev_stats_reset(struct rte_eth_dev *dev)
669 {
670 	unsigned int i;
671 
672 	PMD_INIT_FUNC_TRACE();
673 
674 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
675 		struct hn_tx_queue *txq = dev->data->tx_queues[i];
676 
677 		if (!txq)
678 			continue;
679 		memset(&txq->stats, 0, sizeof(struct hn_stats));
680 	}
681 
682 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
683 		struct hn_rx_queue *rxq = dev->data->rx_queues[i];
684 
685 		if (!rxq)
686 			continue;
687 
688 		memset(&rxq->stats, 0, sizeof(struct hn_stats));
689 	}
690 
691 	return 0;
692 }
693 
694 static int
695 hn_dev_xstats_reset(struct rte_eth_dev *dev)
696 {
697 	int ret;
698 
699 	ret = hn_dev_stats_reset(dev);
700 	if (ret != 0)
701 		return 0;
702 
703 	return hn_vf_xstats_reset(dev);
704 }
705 
706 static int
707 hn_dev_xstats_count(struct rte_eth_dev *dev)
708 {
709 	int ret, count;
710 
711 	count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
712 	count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
713 
714 	ret = hn_vf_xstats_get_names(dev, NULL, 0);
715 	if (ret < 0)
716 		return ret;
717 
718 	return count + ret;
719 }
720 
721 static int
722 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
723 			struct rte_eth_xstat_name *xstats_names,
724 			unsigned int limit)
725 {
726 	unsigned int i, t, count = 0;
727 	int ret;
728 
729 	if (!xstats_names)
730 		return hn_dev_xstats_count(dev);
731 
732 	/* Note: limit checked in rte_eth_xstats_names() */
733 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
734 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
735 
736 		if (!txq)
737 			continue;
738 
739 		if (count >= limit)
740 			break;
741 
742 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
743 			snprintf(xstats_names[count++].name,
744 				 RTE_ETH_XSTATS_NAME_SIZE,
745 				 "tx_q%u_%s", i, hn_stat_strings[t].name);
746 	}
747 
748 	for (i = 0; i < dev->data->nb_rx_queues; i++)  {
749 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
750 
751 		if (!rxq)
752 			continue;
753 
754 		if (count >= limit)
755 			break;
756 
757 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
758 			snprintf(xstats_names[count++].name,
759 				 RTE_ETH_XSTATS_NAME_SIZE,
760 				 "rx_q%u_%s", i,
761 				 hn_stat_strings[t].name);
762 	}
763 
764 	ret = hn_vf_xstats_get_names(dev, xstats_names + count,
765 				     limit - count);
766 	if (ret < 0)
767 		return ret;
768 
769 	return count + ret;
770 }
771 
772 static int
773 hn_dev_xstats_get(struct rte_eth_dev *dev,
774 		  struct rte_eth_xstat *xstats,
775 		  unsigned int n)
776 {
777 	unsigned int i, t, count = 0;
778 	const unsigned int nstats = hn_dev_xstats_count(dev);
779 	const char *stats;
780 	int ret;
781 
782 	PMD_INIT_FUNC_TRACE();
783 
784 	if (n < nstats)
785 		return nstats;
786 
787 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
788 		const struct hn_tx_queue *txq = dev->data->tx_queues[i];
789 
790 		if (!txq)
791 			continue;
792 
793 		stats = (const char *)&txq->stats;
794 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
795 			xstats[count].id = count;
796 			xstats[count].value = *(const uint64_t *)
797 				(stats + hn_stat_strings[t].offset);
798 		}
799 	}
800 
801 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
802 		const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
803 
804 		if (!rxq)
805 			continue;
806 
807 		stats = (const char *)&rxq->stats;
808 		for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
809 			xstats[count].id = count;
810 			xstats[count].value = *(const uint64_t *)
811 				(stats + hn_stat_strings[t].offset);
812 		}
813 	}
814 
815 	ret = hn_vf_xstats_get(dev, xstats, count, n);
816 	if (ret < 0)
817 		return ret;
818 
819 	return count + ret;
820 }
821 
822 static int
823 hn_dev_start(struct rte_eth_dev *dev)
824 {
825 	struct hn_data *hv = dev->data->dev_private;
826 	int error;
827 
828 	PMD_INIT_FUNC_TRACE();
829 
830 	error = hn_rndis_set_rxfilter(hv,
831 				      NDIS_PACKET_TYPE_BROADCAST |
832 				      NDIS_PACKET_TYPE_ALL_MULTICAST |
833 				      NDIS_PACKET_TYPE_DIRECTED);
834 	if (error)
835 		return error;
836 
837 	error = hn_vf_start(dev);
838 	if (error)
839 		hn_rndis_set_rxfilter(hv, 0);
840 
841 	/* Initialize Link state */
842 	if (error == 0)
843 		hn_dev_link_update(dev, 0);
844 
845 	return error;
846 }
847 
848 static int
849 hn_dev_stop(struct rte_eth_dev *dev)
850 {
851 	struct hn_data *hv = dev->data->dev_private;
852 
853 	PMD_INIT_FUNC_TRACE();
854 	dev->data->dev_started = 0;
855 
856 	hn_rndis_set_rxfilter(hv, 0);
857 	return hn_vf_stop(dev);
858 }
859 
860 static int
861 hn_dev_close(struct rte_eth_dev *dev)
862 {
863 	int ret;
864 
865 	PMD_INIT_FUNC_TRACE();
866 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
867 		return 0;
868 
869 	ret = hn_vf_close(dev);
870 	hn_dev_free_queues(dev);
871 
872 	return ret;
873 }
874 
875 static const struct eth_dev_ops hn_eth_dev_ops = {
876 	.dev_configure		= hn_dev_configure,
877 	.dev_start		= hn_dev_start,
878 	.dev_stop		= hn_dev_stop,
879 	.dev_close		= hn_dev_close,
880 	.dev_infos_get		= hn_dev_info_get,
881 	.txq_info_get		= hn_dev_tx_queue_info,
882 	.rxq_info_get		= hn_dev_rx_queue_info,
883 	.dev_supported_ptypes_get = hn_vf_supported_ptypes,
884 	.promiscuous_enable     = hn_dev_promiscuous_enable,
885 	.promiscuous_disable    = hn_dev_promiscuous_disable,
886 	.allmulticast_enable    = hn_dev_allmulticast_enable,
887 	.allmulticast_disable   = hn_dev_allmulticast_disable,
888 	.set_mc_addr_list	= hn_dev_mc_addr_list,
889 	.reta_update		= hn_rss_reta_update,
890 	.reta_query             = hn_rss_reta_query,
891 	.rss_hash_update	= hn_rss_hash_update,
892 	.rss_hash_conf_get      = hn_rss_hash_conf_get,
893 	.tx_queue_setup		= hn_dev_tx_queue_setup,
894 	.tx_queue_release	= hn_dev_tx_queue_release,
895 	.tx_done_cleanup        = hn_dev_tx_done_cleanup,
896 	.rx_queue_setup		= hn_dev_rx_queue_setup,
897 	.rx_queue_release	= hn_dev_rx_queue_release,
898 	.link_update		= hn_dev_link_update,
899 	.stats_get		= hn_dev_stats_get,
900 	.stats_reset            = hn_dev_stats_reset,
901 	.xstats_get		= hn_dev_xstats_get,
902 	.xstats_get_names	= hn_dev_xstats_get_names,
903 	.xstats_reset		= hn_dev_xstats_reset,
904 };
905 
906 /*
907  * Setup connection between PMD and kernel.
908  */
909 static int
910 hn_attach(struct hn_data *hv, unsigned int mtu)
911 {
912 	int error;
913 
914 	/* Attach NVS */
915 	error = hn_nvs_attach(hv, mtu);
916 	if (error)
917 		goto failed_nvs;
918 
919 	/* Attach RNDIS */
920 	error = hn_rndis_attach(hv);
921 	if (error)
922 		goto failed_rndis;
923 
924 	/*
925 	 * NOTE:
926 	 * Under certain conditions on certain versions of Hyper-V,
927 	 * the RNDIS rxfilter is _not_ zero on the hypervisor side
928 	 * after the successful RNDIS initialization.
929 	 */
930 	hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
931 	return 0;
932 failed_rndis:
933 	hn_nvs_detach(hv);
934 failed_nvs:
935 	return error;
936 }
937 
938 static void
939 hn_detach(struct hn_data *hv)
940 {
941 	hn_nvs_detach(hv);
942 	hn_rndis_detach(hv);
943 }
944 
945 static int
946 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
947 {
948 	struct hn_data *hv = eth_dev->data->dev_private;
949 	struct rte_device *device = eth_dev->device;
950 	struct rte_vmbus_device *vmbus;
951 	unsigned int rxr_cnt;
952 	int err, max_chan;
953 
954 	PMD_INIT_FUNC_TRACE();
955 
956 	vmbus = container_of(device, struct rte_vmbus_device, device);
957 	eth_dev->dev_ops = &hn_eth_dev_ops;
958 	eth_dev->rx_queue_count = hn_dev_rx_queue_count;
959 	eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
960 	eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
961 	eth_dev->tx_pkt_burst = &hn_xmit_pkts;
962 	eth_dev->rx_pkt_burst = &hn_recv_pkts;
963 
964 	/*
965 	 * for secondary processes, we don't initialize any further as primary
966 	 * has already done this work.
967 	 */
968 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
969 		return 0;
970 
971 	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
972 
973 	/* Since Hyper-V only supports one MAC address */
974 	eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
975 					      sizeof(struct rte_ether_addr), 0);
976 	if (eth_dev->data->mac_addrs == NULL) {
977 		PMD_INIT_LOG(ERR,
978 			     "Failed to allocate memory store MAC addresses");
979 		return -ENOMEM;
980 	}
981 
982 	hv->vmbus = vmbus;
983 	hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
984 	hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
985 	hv->port_id = eth_dev->data->port_id;
986 	hv->latency = HN_CHAN_LATENCY_NS;
987 	hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
988 	hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
989 	hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
990 	hv->max_queues = 1;
991 
992 	rte_rwlock_init(&hv->vf_lock);
993 	hv->vf_port = HN_INVALID_PORT;
994 
995 	err = hn_parse_args(eth_dev);
996 	if (err)
997 		return err;
998 
999 	strlcpy(hv->owner.name, eth_dev->device->name,
1000 		RTE_ETH_MAX_OWNER_NAME_LEN);
1001 	err = rte_eth_dev_owner_new(&hv->owner.id);
1002 	if (err) {
1003 		PMD_INIT_LOG(ERR, "Can not get owner id");
1004 		return err;
1005 	}
1006 
1007 	/* Initialize primary channel input for control operations */
1008 	err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
1009 	if (err)
1010 		return err;
1011 
1012 	rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1013 
1014 	hv->primary = hn_rx_queue_alloc(hv, 0,
1015 					eth_dev->device->numa_node);
1016 
1017 	if (!hv->primary)
1018 		return -ENOMEM;
1019 
1020 	err = hn_attach(hv, RTE_ETHER_MTU);
1021 	if  (err)
1022 		goto failed;
1023 
1024 	err = hn_chim_init(eth_dev);
1025 	if (err)
1026 		goto failed;
1027 
1028 	err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
1029 	if (err)
1030 		goto failed;
1031 
1032 	/* Multi queue requires later versions of windows server */
1033 	if (hv->nvs_ver < NVS_VERSION_5)
1034 		return 0;
1035 
1036 	max_chan = rte_vmbus_max_channels(vmbus);
1037 	PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1038 	if (max_chan <= 0)
1039 		goto failed;
1040 
1041 	if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1042 		rxr_cnt = 1;
1043 
1044 	hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1045 
1046 	/* If VF was reported but not added, do it now */
1047 	if (hv->vf_present && !hn_vf_attached(hv)) {
1048 		PMD_INIT_LOG(DEBUG, "Adding VF device");
1049 
1050 		err = hn_vf_add(eth_dev, hv);
1051 		if (err)
1052 			hv->vf_present = 0;
1053 	}
1054 
1055 	return 0;
1056 
1057 failed:
1058 	PMD_INIT_LOG(NOTICE, "device init failed");
1059 
1060 	hn_chim_uninit(eth_dev);
1061 	hn_detach(hv);
1062 	return err;
1063 }
1064 
1065 static int
1066 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1067 {
1068 	struct hn_data *hv = eth_dev->data->dev_private;
1069 	int ret, ret_stop;
1070 
1071 	PMD_INIT_FUNC_TRACE();
1072 
1073 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1074 		return 0;
1075 
1076 	ret_stop = hn_dev_stop(eth_dev);
1077 	hn_dev_close(eth_dev);
1078 
1079 	hn_detach(hv);
1080 	hn_chim_uninit(eth_dev);
1081 	rte_vmbus_chan_close(hv->primary->chan);
1082 	rte_free(hv->primary);
1083 	ret = rte_eth_dev_owner_delete(hv->owner.id);
1084 	if (ret != 0)
1085 		return ret;
1086 
1087 	return ret_stop;
1088 }
1089 
1090 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1091 			struct rte_vmbus_device *dev)
1092 {
1093 	struct rte_eth_dev *eth_dev;
1094 	int ret;
1095 
1096 	PMD_INIT_FUNC_TRACE();
1097 
1098 	eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1099 	if (!eth_dev)
1100 		return -ENOMEM;
1101 
1102 	ret = eth_hn_dev_init(eth_dev);
1103 	if (ret)
1104 		eth_dev_vmbus_release(eth_dev);
1105 	else
1106 		rte_eth_dev_probing_finish(eth_dev);
1107 
1108 	return ret;
1109 }
1110 
1111 static int eth_hn_remove(struct rte_vmbus_device *dev)
1112 {
1113 	struct rte_eth_dev *eth_dev;
1114 	int ret;
1115 
1116 	PMD_INIT_FUNC_TRACE();
1117 
1118 	eth_dev = rte_eth_dev_allocated(dev->device.name);
1119 	if (!eth_dev)
1120 		return 0; /* port already released */
1121 
1122 	ret = eth_hn_dev_uninit(eth_dev);
1123 	if (ret)
1124 		return ret;
1125 
1126 	eth_dev_vmbus_release(eth_dev);
1127 	return 0;
1128 }
1129 
1130 /* Network device GUID */
1131 static const rte_uuid_t hn_net_ids[] = {
1132 	/*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
1133 	RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1134 	{ 0 }
1135 };
1136 
1137 static struct rte_vmbus_driver rte_netvsc_pmd = {
1138 	.id_table = hn_net_ids,
1139 	.probe = eth_hn_probe,
1140 	.remove = eth_hn_remove,
1141 };
1142 
1143 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1144 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1145 RTE_LOG_REGISTER(hn_logtype_init, pmd.net.netvsc.init, NOTICE);
1146 RTE_LOG_REGISTER(hn_logtype_driver, pmd.net.netvsc.driver, NOTICE);
1147 RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
1148 			      NETVSC_ARG_LATENCY "=<uint32> "
1149 			      NETVSC_ARG_RXBREAK "=<uint32> "
1150 			      NETVSC_ARG_TXBREAK "=<uint32> "
1151 			      NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");
1152