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