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