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