xref: /freebsd/sys/dev/hyperv/netvsc/hn_nvs.c (revision ad9f4e6351fb23ee81bc940638d20af3ca7c278d)
1 /*-
2  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
3  * Copyright (c) 2010-2012 Citrix Inc.
4  * Copyright (c) 2012 NetApp Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Network Virtualization Service.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_inet6.h"
37 #include "opt_inet.h"
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/socket.h>
43 #include <sys/systm.h>
44 #include <sys/taskqueue.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 #include <vm/pmap.h>
49 
50 #include <net/ethernet.h>
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_media.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/tcp_lro.h>
57 
58 #include <dev/hyperv/include/hyperv.h>
59 #include <dev/hyperv/include/vmbus.h>
60 #include <dev/hyperv/include/vmbus_xact.h>
61 
62 #include <dev/hyperv/netvsc/ndis.h>
63 #include <dev/hyperv/netvsc/if_hnreg.h>
64 #include <dev/hyperv/netvsc/if_hnvar.h>
65 #include <dev/hyperv/netvsc/hn_nvs.h>
66 
67 static int			hn_nvs_conn_chim(struct hn_softc *);
68 static int			hn_nvs_conn_rxbuf(struct hn_softc *);
69 static void			hn_nvs_disconn_chim(struct hn_softc *);
70 static void			hn_nvs_disconn_rxbuf(struct hn_softc *);
71 static int			hn_nvs_conf_ndis(struct hn_softc *, int);
72 static int			hn_nvs_init_ndis(struct hn_softc *);
73 static int			hn_nvs_doinit(struct hn_softc *, uint32_t);
74 static int			hn_nvs_init(struct hn_softc *);
75 static const void		*hn_nvs_xact_execute(struct hn_softc *,
76 				    struct vmbus_xact *, void *, int,
77 				    size_t *, uint32_t);
78 static void			hn_nvs_sent_none(struct hn_nvs_sendctx *,
79 				    struct hn_softc *, struct vmbus_channel *,
80 				    const void *, int);
81 
82 struct hn_nvs_sendctx		hn_nvs_sendctx_none =
83     HN_NVS_SENDCTX_INITIALIZER(hn_nvs_sent_none, NULL);
84 
85 static const uint32_t		hn_nvs_version[] = {
86 	HN_NVS_VERSION_61,
87 	HN_NVS_VERSION_6,
88 	HN_NVS_VERSION_5,
89 	HN_NVS_VERSION_4,
90 	HN_NVS_VERSION_2,
91 	HN_NVS_VERSION_1
92 };
93 
94 static const void *
95 hn_nvs_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact,
96     void *req, int reqlen, size_t *resplen0, uint32_t type)
97 {
98 	struct hn_nvs_sendctx sndc;
99 	size_t resplen, min_resplen = *resplen0;
100 	const struct hn_nvs_hdr *hdr;
101 	int error;
102 
103 	KASSERT(min_resplen >= sizeof(*hdr),
104 	    ("invalid minimum response len %zu", min_resplen));
105 
106 	/*
107 	 * Execute the xact setup by the caller.
108 	 */
109 	hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
110 
111 	vmbus_xact_activate(xact);
112 	error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
113 	    req, reqlen, &sndc);
114 	if (error) {
115 		vmbus_xact_deactivate(xact);
116 		return (NULL);
117 	}
118 	hdr = vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen,
119 	    HN_CAN_SLEEP(sc));
120 
121 	/*
122 	 * Check this NVS response message.
123 	 */
124 	if (resplen < min_resplen) {
125 		if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
126 		return (NULL);
127 	}
128 	if (hdr->nvs_type != type) {
129 		if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
130 		    "expect 0x%08x\n", hdr->nvs_type, type);
131 		return (NULL);
132 	}
133 	/* All pass! */
134 	*resplen0 = resplen;
135 	return (hdr);
136 }
137 
138 static __inline int
139 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
140 {
141 
142 	return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
143 	    req, reqlen, &hn_nvs_sendctx_none));
144 }
145 
146 static int
147 hn_nvs_conn_rxbuf(struct hn_softc *sc)
148 {
149 	struct vmbus_xact *xact = NULL;
150 	struct hn_nvs_rxbuf_conn *conn;
151 	const struct hn_nvs_rxbuf_connresp *resp;
152 	size_t resp_len;
153 	uint32_t status;
154 	int error, rxbuf_size;
155 
156 	/*
157 	 * Limit RXBUF size for old NVS.
158 	 */
159 	if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
160 		rxbuf_size = HN_RXBUF_SIZE_COMPAT;
161 	else
162 		rxbuf_size = HN_RXBUF_SIZE;
163 
164 	/*
165 	 * Connect the RXBUF GPADL to the primary channel.
166 	 *
167 	 * NOTE:
168 	 * Only primary channel has RXBUF connected to it.  Sub-channels
169 	 * just share this RXBUF.
170 	 */
171 	error = vmbus_chan_gpadl_connect(sc->hn_prichan,
172 	    pmap_kextract((vm_offset_t)sc->hn_rxbuf), rxbuf_size,
173 	    &sc->hn_rxbuf_gpadl);
174 	if (error) {
175 		if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
176 		    error);
177 		goto cleanup;
178 	}
179 
180 	/*
181 	 * Connect RXBUF to NVS.
182 	 */
183 
184 	xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
185 	if (xact == NULL) {
186 		if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
187 		error = ENXIO;
188 		goto cleanup;
189 	}
190 	conn = vmbus_xact_req_data(xact);
191 	conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
192 	conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
193 	conn->nvs_sig = HN_NVS_RXBUF_SIG;
194 
195 	resp_len = sizeof(*resp);
196 	resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
197 	    HN_NVS_TYPE_RXBUF_CONNRESP);
198 	if (resp == NULL) {
199 		if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
200 		error = EIO;
201 		goto cleanup;
202 	}
203 
204 	status = resp->nvs_status;
205 	vmbus_xact_put(xact);
206 	xact = NULL;
207 
208 	if (status != HN_NVS_STATUS_OK) {
209 		if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
210 		error = EIO;
211 		goto cleanup;
212 	}
213 	sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
214 
215 	return (0);
216 
217 cleanup:
218 	if (xact != NULL)
219 		vmbus_xact_put(xact);
220 	hn_nvs_disconn_rxbuf(sc);
221 	return (error);
222 }
223 
224 static int
225 hn_nvs_conn_chim(struct hn_softc *sc)
226 {
227 	struct vmbus_xact *xact = NULL;
228 	struct hn_nvs_chim_conn *chim;
229 	const struct hn_nvs_chim_connresp *resp;
230 	size_t resp_len;
231 	uint32_t status, sectsz;
232 	int error;
233 
234 	/*
235 	 * Connect chimney sending buffer GPADL to the primary channel.
236 	 *
237 	 * NOTE:
238 	 * Only primary channel has chimney sending buffer connected to it.
239 	 * Sub-channels just share this chimney sending buffer.
240 	 */
241 	error = vmbus_chan_gpadl_connect(sc->hn_prichan,
242 	    pmap_kextract((vm_offset_t)sc->hn_chim), HN_CHIM_SIZE,
243 	    &sc->hn_chim_gpadl);
244 	if (error) {
245 		if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
246 		goto cleanup;
247 	}
248 
249 	/*
250 	 * Connect chimney sending buffer to NVS
251 	 */
252 
253 	xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
254 	if (xact == NULL) {
255 		if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
256 		error = ENXIO;
257 		goto cleanup;
258 	}
259 	chim = vmbus_xact_req_data(xact);
260 	chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
261 	chim->nvs_gpadl = sc->hn_chim_gpadl;
262 	chim->nvs_sig = HN_NVS_CHIM_SIG;
263 
264 	resp_len = sizeof(*resp);
265 	resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
266 	    HN_NVS_TYPE_CHIM_CONNRESP);
267 	if (resp == NULL) {
268 		if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
269 		error = EIO;
270 		goto cleanup;
271 	}
272 
273 	status = resp->nvs_status;
274 	sectsz = resp->nvs_sectsz;
275 	vmbus_xact_put(xact);
276 	xact = NULL;
277 
278 	if (status != HN_NVS_STATUS_OK) {
279 		if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
280 		error = EIO;
281 		goto cleanup;
282 	}
283 	if (sectsz == 0 || sectsz % sizeof(uint32_t) != 0) {
284 		/*
285 		 * Can't use chimney sending buffer; done!
286 		 */
287 		if (sectsz == 0) {
288 			if_printf(sc->hn_ifp, "zero chimney sending buffer "
289 			    "section size\n");
290 		} else {
291 			if_printf(sc->hn_ifp, "misaligned chimney sending "
292 			    "buffers, section size: %u\n", sectsz);
293 		}
294 		sc->hn_chim_szmax = 0;
295 		sc->hn_chim_cnt = 0;
296 		sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
297 		return (0);
298 	}
299 
300 	sc->hn_chim_szmax = sectsz;
301 	sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
302 	if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
303 		if_printf(sc->hn_ifp, "chimney sending sections are "
304 		    "not properly aligned\n");
305 	}
306 	if (sc->hn_chim_cnt % LONG_BIT != 0) {
307 		if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
308 		    sc->hn_chim_cnt % LONG_BIT);
309 	}
310 
311 	sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
312 	sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
313 	    M_DEVBUF, M_WAITOK | M_ZERO);
314 
315 	/* Done! */
316 	sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
317 	if (bootverbose) {
318 		if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
319 		    sc->hn_chim_szmax, sc->hn_chim_cnt);
320 	}
321 	return (0);
322 
323 cleanup:
324 	if (xact != NULL)
325 		vmbus_xact_put(xact);
326 	hn_nvs_disconn_chim(sc);
327 	return (error);
328 }
329 
330 static void
331 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
332 {
333 	int error;
334 
335 	if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
336 		struct hn_nvs_rxbuf_disconn disconn;
337 
338 		/*
339 		 * Disconnect RXBUF from NVS.
340 		 */
341 		memset(&disconn, 0, sizeof(disconn));
342 		disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
343 		disconn.nvs_sig = HN_NVS_RXBUF_SIG;
344 
345 		/* NOTE: No response. */
346 		error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
347 		if (error) {
348 			if_printf(sc->hn_ifp,
349 			    "send nvs rxbuf disconn failed: %d\n", error);
350 			/*
351 			 * Fine for a revoked channel, since the hypervisor
352 			 * does not drain TX bufring for a revoked channel.
353 			 */
354 			if (!vmbus_chan_is_revoked(sc->hn_prichan))
355 				sc->hn_flags |= HN_FLAG_RXBUF_REF;
356 		}
357 		sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
358 
359 		/*
360 		 * Wait for the hypervisor to receive this NVS request.
361 		 *
362 		 * NOTE:
363 		 * The TX bufring will not be drained by the hypervisor,
364 		 * if the primary channel is revoked.
365 		 */
366 		while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
367 		    !vmbus_chan_is_revoked(sc->hn_prichan))
368 			pause("waittx", 1);
369 		/*
370 		 * Linger long enough for NVS to disconnect RXBUF.
371 		 */
372 		pause("lingtx", (200 * hz) / 1000);
373 	}
374 
375 	if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_rxbuf_gpadl != 0) {
376 		/*
377 		 * Disconnect RXBUF from primary channel.
378 		 */
379 		error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
380 		    sc->hn_rxbuf_gpadl);
381 		if (error) {
382 			if_printf(sc->hn_ifp,
383 			    "rxbuf gpadl disconn failed: %d\n", error);
384 			sc->hn_flags |= HN_FLAG_RXBUF_REF;
385 		}
386 		sc->hn_rxbuf_gpadl = 0;
387 	}
388 }
389 
390 static void
391 hn_nvs_disconn_chim(struct hn_softc *sc)
392 {
393 	int error;
394 
395 	if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
396 		struct hn_nvs_chim_disconn disconn;
397 
398 		/*
399 		 * Disconnect chimney sending buffer from NVS.
400 		 */
401 		memset(&disconn, 0, sizeof(disconn));
402 		disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
403 		disconn.nvs_sig = HN_NVS_CHIM_SIG;
404 
405 		/* NOTE: No response. */
406 		error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
407 		if (error) {
408 			if_printf(sc->hn_ifp,
409 			    "send nvs chim disconn failed: %d\n", error);
410 			/*
411 			 * Fine for a revoked channel, since the hypervisor
412 			 * does not drain TX bufring for a revoked channel.
413 			 */
414 			if (!vmbus_chan_is_revoked(sc->hn_prichan))
415 				sc->hn_flags |= HN_FLAG_CHIM_REF;
416 		}
417 		sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
418 
419 		/*
420 		 * Wait for the hypervisor to receive this NVS request.
421 		 *
422 		 * NOTE:
423 		 * The TX bufring will not be drained by the hypervisor,
424 		 * if the primary channel is revoked.
425 		 */
426 		while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
427 		    !vmbus_chan_is_revoked(sc->hn_prichan))
428 			pause("waittx", 1);
429 		/*
430 		 * Linger long enough for NVS to disconnect chimney
431 		 * sending buffer.
432 		 */
433 		pause("lingtx", (200 * hz) / 1000);
434 	}
435 
436 	if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_chim_gpadl != 0) {
437 		/*
438 		 * Disconnect chimney sending buffer from primary channel.
439 		 */
440 		error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
441 		    sc->hn_chim_gpadl);
442 		if (error) {
443 			if_printf(sc->hn_ifp,
444 			    "chim gpadl disconn failed: %d\n", error);
445 			sc->hn_flags |= HN_FLAG_CHIM_REF;
446 		}
447 		sc->hn_chim_gpadl = 0;
448 	}
449 
450 	if (sc->hn_chim_bmap != NULL) {
451 		free(sc->hn_chim_bmap, M_DEVBUF);
452 		sc->hn_chim_bmap = NULL;
453 		sc->hn_chim_bmap_cnt = 0;
454 	}
455 }
456 
457 static int
458 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
459 {
460 	struct vmbus_xact *xact;
461 	struct hn_nvs_init *init;
462 	const struct hn_nvs_init_resp *resp;
463 	size_t resp_len;
464 	uint32_t status;
465 
466 	xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
467 	if (xact == NULL) {
468 		if_printf(sc->hn_ifp, "no xact for nvs init\n");
469 		return (ENXIO);
470 	}
471 	init = vmbus_xact_req_data(xact);
472 	init->nvs_type = HN_NVS_TYPE_INIT;
473 	init->nvs_ver_min = nvs_ver;
474 	init->nvs_ver_max = nvs_ver;
475 
476 	resp_len = sizeof(*resp);
477 	resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
478 	    HN_NVS_TYPE_INIT_RESP);
479 	if (resp == NULL) {
480 		if_printf(sc->hn_ifp, "exec init failed\n");
481 		vmbus_xact_put(xact);
482 		return (EIO);
483 	}
484 
485 	status = resp->nvs_status;
486 	vmbus_xact_put(xact);
487 
488 	if (status != HN_NVS_STATUS_OK) {
489 		if (bootverbose) {
490 			/*
491 			 * Caller may try another NVS version, and will log
492 			 * error if there are no more NVS versions to try,
493 			 * so don't bark out loud here.
494 			 */
495 			if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
496 			    nvs_ver);
497 		}
498 		return (EINVAL);
499 	}
500 	return (0);
501 }
502 
503 /*
504  * Configure MTU and enable VLAN.
505  */
506 static int
507 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
508 {
509 	struct hn_nvs_ndis_conf conf;
510 	int error;
511 
512 	memset(&conf, 0, sizeof(conf));
513 	conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
514 	conf.nvs_mtu = mtu + ETHER_HDR_LEN;
515 	conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
516 	if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
517 		conf.nvs_caps |= HN_NVS_NDIS_CONF_SRIOV;
518 	if (sc->hn_nvs_ver >= HN_NVS_VERSION_61)
519 		conf.nvs_caps |= HN_NVS_NDIS_CONF_RSC;
520 
521 
522 	/* NOTE: No response. */
523 	error = hn_nvs_req_send(sc, &conf, sizeof(conf));
524 	if (error) {
525 		if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
526 		return (error);
527 	}
528 
529 	if (bootverbose)
530 		if_printf(sc->hn_ifp, "nvs ndis conf done\n");
531 	sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
532 	return (0);
533 }
534 
535 static int
536 hn_nvs_init_ndis(struct hn_softc *sc)
537 {
538 	struct hn_nvs_ndis_init ndis;
539 	int error;
540 
541 	memset(&ndis, 0, sizeof(ndis));
542 	ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
543 	ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
544 	ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
545 
546 	/* NOTE: No response. */
547 	error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
548 	if (error)
549 		if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
550 	return (error);
551 }
552 
553 static int
554 hn_nvs_init(struct hn_softc *sc)
555 {
556 	int i, error;
557 
558 	if (device_is_attached(sc->hn_dev)) {
559 		/*
560 		 * NVS version and NDIS version MUST NOT be changed.
561 		 */
562 		if (bootverbose) {
563 			if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
564 			    "NDIS version %u.%u\n", sc->hn_nvs_ver,
565 			    HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
566 			    HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
567 		}
568 
569 		error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
570 		if (error) {
571 			if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
572 			    "failed: %d\n", sc->hn_nvs_ver, error);
573 			return (error);
574 		}
575 		goto done;
576 	}
577 
578 	/*
579 	 * Find the supported NVS version and set NDIS version accordingly.
580 	 */
581 	for (i = 0; i < nitems(hn_nvs_version); ++i) {
582 		error = hn_nvs_doinit(sc, hn_nvs_version[i]);
583 		if (!error) {
584 			sc->hn_nvs_ver = hn_nvs_version[i];
585 
586 			/* Set NDIS version according to NVS version. */
587 			sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
588 			if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
589 				sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
590 
591 			if (bootverbose) {
592 				if_printf(sc->hn_ifp, "NVS version 0x%x, "
593 				    "NDIS version %u.%u\n", sc->hn_nvs_ver,
594 				    HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
595 				    HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
596 			}
597 			goto done;
598 		}
599 	}
600 	if_printf(sc->hn_ifp, "no NVS available\n");
601 	return (ENXIO);
602 
603 done:
604 	if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
605 		sc->hn_caps |= HN_CAP_HASHVAL;
606 	return (0);
607 }
608 
609 int
610 hn_nvs_attach(struct hn_softc *sc, int mtu)
611 {
612 	int error;
613 
614 	if (hyperv_ver_major >= 10) {
615 		/* UDP 4-tuple hash is enforced. */
616 		sc->hn_caps |= HN_CAP_UDPHASH;
617 	}
618 
619 	/*
620 	 * Initialize NVS.
621 	 */
622 	error = hn_nvs_init(sc);
623 	if (error)
624 		return (error);
625 
626 	if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
627 		/*
628 		 * Configure NDIS before initializing it.
629 		 */
630 		error = hn_nvs_conf_ndis(sc, mtu);
631 		if (error)
632 			return (error);
633 	}
634 
635 	/*
636 	 * Initialize NDIS.
637 	 */
638 	error = hn_nvs_init_ndis(sc);
639 	if (error)
640 		return (error);
641 
642 	/*
643 	 * Connect RXBUF.
644 	 */
645 	error = hn_nvs_conn_rxbuf(sc);
646 	if (error)
647 		return (error);
648 
649 	/*
650 	 * Connect chimney sending buffer.
651 	 */
652 	error = hn_nvs_conn_chim(sc);
653 	if (error) {
654 		hn_nvs_disconn_rxbuf(sc);
655 		return (error);
656 	}
657 	return (0);
658 }
659 
660 void
661 hn_nvs_detach(struct hn_softc *sc)
662 {
663 
664 	/* NOTE: there are no requests to stop the NVS. */
665 	hn_nvs_disconn_rxbuf(sc);
666 	hn_nvs_disconn_chim(sc);
667 }
668 
669 void
670 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
671     struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
672     const void *data, int dlen)
673 {
674 
675 	vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
676 }
677 
678 static void
679 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
680     struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
681     const void *data __unused, int dlen __unused)
682 {
683 	/* EMPTY */
684 }
685 
686 int
687 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
688 {
689 	struct vmbus_xact *xact;
690 	struct hn_nvs_subch_req *req;
691 	const struct hn_nvs_subch_resp *resp;
692 	int error, nsubch_req;
693 	uint32_t nsubch;
694 	size_t resp_len;
695 
696 	nsubch_req = *nsubch0;
697 	KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
698 
699 	xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
700 	if (xact == NULL) {
701 		if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
702 		return (ENXIO);
703 	}
704 	req = vmbus_xact_req_data(xact);
705 	req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
706 	req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
707 	req->nvs_nsubch = nsubch_req;
708 
709 	resp_len = sizeof(*resp);
710 	resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
711 	    HN_NVS_TYPE_SUBCH_RESP);
712 	if (resp == NULL) {
713 		if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
714 		error = EIO;
715 		goto done;
716 	}
717 	if (resp->nvs_status != HN_NVS_STATUS_OK) {
718 		if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
719 		    resp->nvs_status);
720 		error = EIO;
721 		goto done;
722 	}
723 
724 	nsubch = resp->nvs_nsubch;
725 	if (nsubch > nsubch_req) {
726 		if_printf(sc->hn_ifp, "%u subchans are allocated, "
727 		    "requested %d\n", nsubch, nsubch_req);
728 		nsubch = nsubch_req;
729 	}
730 	*nsubch0 = nsubch;
731 	error = 0;
732 done:
733 	vmbus_xact_put(xact);
734 	return (error);
735 }
736 
737 int
738 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
739     struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
740 {
741 
742 	return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,
743 	    sndc, gpa, gpa_cnt);
744 }
745 
746 void
747 hn_nvs_set_datapath(struct hn_softc *sc, uint32_t path)
748 {
749 	struct hn_nvs_datapath dp;
750 
751 	memset(&dp, 0, sizeof(dp));
752 	dp.nvs_type = HN_NVS_TYPE_SET_DATAPATH;
753 	dp.nvs_active_path = path;
754 
755 	hn_nvs_req_send(sc, &dp, sizeof(dp));
756 }
757