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