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