xref: /openbsd-src/sys/dev/pv/hyperv.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*-
2  * Copyright (c) 2009-2012 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * Copyright (c) 2016 Mike Belopuhov <mike@esdenera.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * The OpenBSD port was done under funding by Esdenera Networks GmbH.
32  */
33 
34 #include <sys/param.h>
35 
36 /* Hyperv requires locked atomic operations */
37 #ifndef MULTIPROCESSOR
38 #define _HYPERVMPATOMICS
39 #define MULTIPROCESSOR
40 #endif
41 #include <sys/atomic.h>
42 #ifdef _HYPERVMPATOMICS
43 #undef MULTIPROCESSOR
44 #undef _HYPERVMPATOMICS
45 #endif
46 
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/signal.h>
50 #include <sys/signalvar.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/timetc.h>
55 #include <sys/task.h>
56 #include <sys/syslog.h>
57 
58 #include <machine/bus.h>
59 #include <machine/cpu.h>
60 #include <machine/cpufunc.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <machine/i82489var.h>
65 
66 #include <dev/pv/pvvar.h>
67 #include <dev/pv/pvreg.h>
68 #include <dev/pv/hypervreg.h>
69 #include <dev/pv/hypervvar.h>
70 
71 /* Command submission flags */
72 #define HCF_SLEEPOK	0x0001	/* M_WAITOK */
73 #define HCF_NOSLEEP	0x0002	/* M_NOWAIT */
74 #define HCF_NOREPLY	0x0004
75 
76 struct hv_softc *hv_sc;
77 
78 int 	hv_match(struct device *, void *, void *);
79 void	hv_attach(struct device *, struct device *, void *);
80 void	hv_set_version(struct hv_softc *);
81 u_int	hv_gettime(struct timecounter *);
82 int	hv_init_hypercall(struct hv_softc *);
83 uint64_t hv_hypercall(struct hv_softc *, uint64_t, void *, void *);
84 int	hv_init_interrupts(struct hv_softc *);
85 int	hv_init_synic(struct hv_softc *);
86 int	hv_cmd(struct hv_softc *, void *, size_t, void *, size_t, int);
87 int	hv_start(struct hv_softc *, struct hv_msg *);
88 int	hv_reply(struct hv_softc *, struct hv_msg *);
89 void	hv_wait(struct hv_softc *, int (*done)(struct hv_softc *,
90 	    struct hv_msg *), struct hv_msg *, void *, const char *);
91 uint16_t hv_intr_signal(struct hv_softc *, void *);
92 void	hv_intr(void);
93 void	hv_event_intr(struct hv_softc *);
94 void	hv_message_intr(struct hv_softc *);
95 int	hv_vmbus_connect(struct hv_softc *);
96 void	hv_channel_response(struct hv_softc *, struct vmbus_chanmsg_hdr *);
97 void	hv_channel_offer(struct hv_softc *, struct vmbus_chanmsg_hdr *);
98 void	hv_channel_rescind(struct hv_softc *, struct vmbus_chanmsg_hdr *);
99 void	hv_channel_delivered(struct hv_softc *, struct vmbus_chanmsg_hdr *);
100 int	hv_channel_scan(struct hv_softc *);
101 void	hv_process_offer(struct hv_softc *, struct hv_offer *);
102 struct hv_channel *
103 	hv_channel_lookup(struct hv_softc *, uint32_t);
104 int	hv_channel_ring_create(struct hv_channel *, uint32_t);
105 void	hv_channel_ring_destroy(struct hv_channel *);
106 void	hv_channel_pause(struct hv_channel *);
107 uint	hv_channel_unpause(struct hv_channel *);
108 uint	hv_channel_ready(struct hv_channel *);
109 extern void hv_attach_icdevs(struct hv_softc *);
110 int	hv_attach_devices(struct hv_softc *);
111 
112 struct {
113 	int		  hmd_response;
114 	int		  hmd_request;
115 	void		(*hmd_handler)(struct hv_softc *,
116 			    struct vmbus_chanmsg_hdr *);
117 } hv_msg_dispatch[] = {
118 	{ 0,					0, NULL },
119 	{ VMBUS_CHANMSG_CHOFFER,		0, hv_channel_offer },
120 	{ VMBUS_CHANMSG_CHRESCIND,		0, hv_channel_rescind },
121 	{ VMBUS_CHANMSG_CHREQUEST,		VMBUS_CHANMSG_CHOFFER,
122 	  NULL },
123 	{ VMBUS_CHANMSG_CHOFFER_DONE,		0,
124 	  hv_channel_delivered },
125 	{ VMBUS_CHANMSG_CHOPEN,			0, NULL },
126 	{ VMBUS_CHANMSG_CHOPEN_RESP,		VMBUS_CHANMSG_CHOPEN,
127 	  hv_channel_response },
128 	{ VMBUS_CHANMSG_CHCLOSE,		0, NULL },
129 	{ VMBUS_CHANMSG_GPADL_CONN,		0, NULL },
130 	{ VMBUS_CHANMSG_GPADL_SUBCONN,		0, NULL },
131 	{ VMBUS_CHANMSG_GPADL_CONNRESP,		VMBUS_CHANMSG_GPADL_CONN,
132 	  hv_channel_response },
133 	{ VMBUS_CHANMSG_GPADL_DISCONN,		0, NULL },
134 	{ VMBUS_CHANMSG_GPADL_DISCONNRESP,	VMBUS_CHANMSG_GPADL_DISCONN,
135 	  hv_channel_response },
136 	{ VMBUS_CHANMSG_CHFREE,			0, NULL },
137 	{ VMBUS_CHANMSG_CONNECT,		0, NULL },
138 	{ VMBUS_CHANMSG_CONNECT_RESP,		VMBUS_CHANMSG_CONNECT,
139 	  hv_channel_response },
140 	{ VMBUS_CHANMSG_DISCONNECT,		0, NULL },
141 };
142 
143 struct timecounter hv_timecounter = {
144 	.tc_get_timecount = hv_gettime,
145 	.tc_poll_pps = 0,
146 	.tc_counter_mask = 0xffffffff,
147 	.tc_frequency = 10000000,
148 	.tc_name = "hyperv",
149 	.tc_quality = 9001,
150 	.tc_priv = NULL,
151 	.tc_user = 0,
152 };
153 
154 struct cfdriver hyperv_cd = {
155 	NULL, "hyperv", DV_DULL
156 };
157 
158 const struct cfattach hyperv_ca = {
159 	sizeof(struct hv_softc), hv_match, hv_attach
160 };
161 
162 const struct hv_guid hv_guid_network = {
163 	{ 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46,
164 	  0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e }
165 };
166 
167 const struct hv_guid hv_guid_ide = {
168 	{ 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
169 	  0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 }
170 };
171 
172 const struct hv_guid hv_guid_scsi = {
173 	{ 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
174 	  0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f }
175 };
176 
177 const struct hv_guid hv_guid_shutdown = {
178 	{ 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
179 	  0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb }
180 };
181 
182 const struct hv_guid hv_guid_timesync = {
183 	{ 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
184 	  0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf }
185 };
186 
187 const struct hv_guid hv_guid_heartbeat = {
188 	{ 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
189 	  0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d }
190 };
191 
192 const struct hv_guid hv_guid_kvp = {
193 	{ 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
194 	  0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6 }
195 };
196 
197 #ifdef HYPERV_DEBUG
198 const struct hv_guid hv_guid_vss = {
199 	{ 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42,
200 	  0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 }
201 };
202 
203 const struct hv_guid hv_guid_dynmem = {
204 	{ 0xdc, 0x74, 0x50, 0x52, 0x85, 0x89, 0xe2, 0x46,
205 	  0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 }
206 };
207 
208 const struct hv_guid hv_guid_mouse = {
209 	{ 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c,
210 	  0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a }
211 };
212 
213 const struct hv_guid hv_guid_kbd = {
214 	{ 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
215 	  0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84 }
216 };
217 
218 const struct hv_guid hv_guid_video = {
219 	{ 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a,
220 	  0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 }
221 };
222 
223 const struct hv_guid hv_guid_fc = {
224 	{ 0x4a, 0xcc, 0x9b, 0x2f, 0x69, 0x00, 0xf3, 0x4a,
225 	  0xb7, 0x6b, 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda }
226 };
227 
228 const struct hv_guid hv_guid_fcopy = {
229 	{ 0xe3, 0x4b, 0xd1, 0x34, 0xe4, 0xde, 0xc8, 0x41,
230 	  0x9a, 0xe7, 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92 }
231 };
232 
233 const struct hv_guid hv_guid_pcie = {
234 	{ 0x1d, 0xf6, 0xc4, 0x44, 0x44, 0x44, 0x00, 0x44,
235 	  0x9d, 0x52, 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f }
236 };
237 
238 const struct hv_guid hv_guid_netdir = {
239 	{ 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b,
240 	  0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 }
241 };
242 
243 const struct hv_guid hv_guid_rdesktop = {
244 	{ 0xf4, 0xac, 0x6a, 0x27, 0x15, 0xac, 0x6c, 0x42,
245 	  0x98, 0xdd, 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe }
246 };
247 
248 /* Automatic Virtual Machine Activation (AVMA) Services */
249 const struct hv_guid hv_guid_avma1 = {
250 	{ 0x55, 0xb2, 0x87, 0x44, 0x8c, 0xb8, 0x3f, 0x40,
251 	  0xbb, 0x51, 0xd1, 0xf6, 0x9c, 0xf1, 0x7f, 0x87 }
252 };
253 
254 const struct hv_guid hv_guid_avma2 = {
255 	{ 0xf4, 0xba, 0x75, 0x33, 0x15, 0x9e, 0x30, 0x4b,
256 	  0xb7, 0x65, 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b }
257 };
258 
259 const struct hv_guid hv_guid_avma3 = {
260 	{ 0xa0, 0x1f, 0x22, 0x99, 0xad, 0x24, 0xe2, 0x11,
261 	  0xbe, 0x98, 0x00, 0x1a, 0xa0, 0x1b, 0xbf, 0x6e }
262 };
263 
264 const struct hv_guid hv_guid_avma4 = {
265 	{ 0x16, 0x57, 0xe6, 0xf8, 0xb3, 0x3c, 0x06, 0x4a,
266 	  0x9a, 0x60, 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5 }
267 };
268 #endif	/* HYPERV_DEBUG */
269 
270 int
271 hv_match(struct device *parent, void *match, void *aux)
272 {
273 	struct pv_attach_args *pva = aux;
274 	struct pvbus_hv *hv = &pva->pva_hv[PVBUS_HYPERV];
275 
276 	if ((hv->hv_major == 0 && hv->hv_minor == 0) || hv->hv_base == 0)
277 		return (0);
278 
279 	return (1);
280 }
281 
282 void
283 hv_attach(struct device *parent, struct device *self, void *aux)
284 {
285 	struct hv_softc *sc = (struct hv_softc *)self;
286 	struct pv_attach_args *pva = aux;
287 	struct pvbus_hv *hv = &pva->pva_hv[PVBUS_HYPERV];
288 
289 	sc->sc_pvbus = hv;
290 	sc->sc_dmat = pva->pva_dmat;
291 
292 	if (!(hv->hv_features & CPUID_HV_MSR_HYPERCALL) ||
293 	    !(hv->hv_features & CPUID_HV_MSR_SYNIC)) {
294 		printf(": not functional\n");
295 		return;
296 	}
297 
298 	DPRINTF("\n");
299 
300 	hv_set_version(sc);
301 
302 	if (hv->hv_features & CPUID_HV_MSR_TIME_REFCNT)
303 		tc_init(&hv_timecounter);
304 
305 	if (hv_init_hypercall(sc))
306 		return;
307 
308 	/* Wire it up to the global */
309 	hv_sc = sc;
310 
311 	if (hv_init_interrupts(sc))
312 		return;
313 
314 	if (hv_vmbus_connect(sc))
315 		return;
316 
317 	DPRINTF("%s", sc->sc_dev.dv_xname);
318 	printf(": protocol %d.%d, features %#x\n",
319 	    VMBUS_VERSION_MAJOR(sc->sc_proto),
320 	    VMBUS_VERSION_MINOR(sc->sc_proto),
321 	    hv->hv_features);
322 
323 	if (hv_channel_scan(sc))
324 		return;
325 
326 	/* Attach heartbeat, KVP and other "internal" services */
327 	hv_attach_icdevs(sc);
328 
329 	/* Attach devices with external drivers */
330 	hv_attach_devices(sc);
331 }
332 
333 void
334 hv_set_version(struct hv_softc *sc)
335 {
336 	uint64_t ver;
337 
338 	/* OpenBSD build date */
339 	ver = MSR_HV_GUESTID_OSTYPE_OPENBSD;
340 	ver |= (uint64_t)OpenBSD << MSR_HV_GUESTID_VERSION_SHIFT;
341 	wrmsr(MSR_HV_GUEST_OS_ID, ver);
342 }
343 
344 u_int
345 hv_gettime(struct timecounter *tc)
346 {
347 	u_int now = rdmsr(MSR_HV_TIME_REF_COUNT);
348 
349 	return (now);
350 }
351 
352 void
353 hv_delay(int usecs)
354 {
355 	uint64_t interval, start;
356 
357 	/* 10 MHz fixed frequency */
358 	interval = (uint64_t)usecs * 10;
359 	start = rdmsr(MSR_HV_TIME_REF_COUNT);
360 	while (rdmsr(MSR_HV_TIME_REF_COUNT) - start < interval)
361 		CPU_BUSY_CYCLE();
362 }
363 
364 int
365 hv_init_hypercall(struct hv_softc *sc)
366 {
367 	extern void *hv_hypercall_page;
368 	uint64_t msr;
369 	paddr_t pa;
370 
371 	sc->sc_hc = &hv_hypercall_page;
372 
373 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_hc, &pa)) {
374 		printf(": hypercall page PA extraction failed\n");
375 		return (-1);
376 	}
377 
378 	msr = (atop(pa) << MSR_HV_HYPERCALL_PGSHIFT) | MSR_HV_HYPERCALL_ENABLE;
379 	wrmsr(MSR_HV_HYPERCALL, msr);
380 
381 	if (!(rdmsr(MSR_HV_HYPERCALL) & MSR_HV_HYPERCALL_ENABLE)) {
382 		printf(": failed to set up a hypercall page\n");
383 		return (-1);
384 	}
385 
386 	return (0);
387 }
388 
389 uint64_t
390 hv_hypercall(struct hv_softc *sc, uint64_t control, void *input,
391     void *output)
392 {
393 	paddr_t input_pa = 0, output_pa = 0;
394 	uint64_t status = 0;
395 
396 	if (input != NULL &&
397 	    pmap_extract(pmap_kernel(), (vaddr_t)input, &input_pa) == 0) {
398 		printf("%s: hypercall input PA extraction failed\n",
399 		    sc->sc_dev.dv_xname);
400 		return (~HYPERCALL_STATUS_SUCCESS);
401 	}
402 
403 	if (output != NULL &&
404 	    pmap_extract(pmap_kernel(), (vaddr_t)output, &output_pa) == 0) {
405 		printf("%s: hypercall output PA extraction failed\n",
406 		    sc->sc_dev.dv_xname);
407 		return (~HYPERCALL_STATUS_SUCCESS);
408 	}
409 
410 #ifdef __amd64__
411 	__asm__ __volatile__ ("mov %0, %%r8" : : "r" (output_pa) : "r8");
412 	__asm__ __volatile__ ("call *%3" : "=a" (status) : "c" (control),
413 	    "d" (input_pa), "m" (sc->sc_hc));
414 #else  /* __i386__ */
415 	{
416 		uint32_t control_hi = control >> 32;
417 		uint32_t control_lo = control & 0xfffffffff;
418 		uint32_t status_hi = 1;
419 		uint32_t status_lo = 1;
420 
421 		__asm__ __volatile__ ("call *%8" :
422 		    "=d" (status_hi), "=a"(status_lo) :
423 		    "d" (control_hi), "a" (control_lo),
424 		    "b" (0), "c" (input_pa), "D" (0), "S" (output_pa),
425 		    "m" (sc->sc_hc));
426 
427 		status = status_lo | ((uint64_t)status_hi << 32);
428 	}
429 #endif	/* __amd64__ */
430 
431 	return (status);
432 }
433 
434 int
435 hv_init_interrupts(struct hv_softc *sc)
436 {
437 	struct cpu_info *ci = curcpu();
438 	int cpu = CPU_INFO_UNIT(ci);
439 
440 	sc->sc_idtvec = LAPIC_HYPERV_VECTOR;
441 
442 	TAILQ_INIT(&sc->sc_reqs);
443 	mtx_init(&sc->sc_reqlck, IPL_NET);
444 
445 	TAILQ_INIT(&sc->sc_rsps);
446 	mtx_init(&sc->sc_rsplck, IPL_NET);
447 
448 	sc->sc_simp[cpu] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait);
449 	if (sc->sc_simp[cpu] == NULL) {
450 		printf(": failed to allocate SIMP\n");
451 		return (-1);
452 	}
453 
454 	sc->sc_siep[cpu] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait);
455 	if (sc->sc_siep[cpu] == NULL) {
456 		printf(": failed to allocate SIEP\n");
457 		km_free(sc->sc_simp[cpu], PAGE_SIZE, &kv_any, &kp_zero);
458 		return (-1);
459 	}
460 
461 	sc->sc_proto = VMBUS_VERSION_WS2008;
462 
463 	return (hv_init_synic(sc));
464 }
465 
466 int
467 hv_init_synic(struct hv_softc *sc)
468 {
469 	struct cpu_info *ci = curcpu();
470 	int cpu = CPU_INFO_UNIT(ci);
471 	uint64_t simp, siefp, sctrl, sint;
472 	paddr_t pa;
473 
474 	/*
475 	 * Setup the Synic's message page
476 	 */
477 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_simp[cpu], &pa)) {
478 		printf(": SIMP PA extraction failed\n");
479 		return (-1);
480 	}
481 	simp = rdmsr(MSR_HV_SIMP);
482 	simp &= (1 << MSR_HV_SIMP_PGSHIFT) - 1;
483 	simp |= (atop(pa) << MSR_HV_SIMP_PGSHIFT);
484 	simp |= MSR_HV_SIMP_ENABLE;
485 	wrmsr(MSR_HV_SIMP, simp);
486 
487 	/*
488 	 * Setup the Synic's event page
489 	 */
490 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_siep[cpu], &pa)) {
491 		printf(": SIEP PA extraction failed\n");
492 		return (-1);
493 	}
494 	siefp = rdmsr(MSR_HV_SIEFP);
495 	siefp &= (1<<MSR_HV_SIEFP_PGSHIFT) - 1;
496 	siefp |= (atop(pa) << MSR_HV_SIEFP_PGSHIFT);
497 	siefp |= MSR_HV_SIEFP_ENABLE;
498 	wrmsr(MSR_HV_SIEFP, siefp);
499 
500 	/*
501 	 * Configure and unmask SINT for message and event flags
502 	 */
503 	sint = rdmsr(MSR_HV_SINT0 + VMBUS_SINT_MESSAGE);
504 	sint = sc->sc_idtvec | MSR_HV_SINT_AUTOEOI |
505 	    (sint & MSR_HV_SINT_RSVD_MASK);
506 	wrmsr(MSR_HV_SINT0 + VMBUS_SINT_MESSAGE, sint);
507 
508 	/* Enable the global synic bit */
509 	sctrl = rdmsr(MSR_HV_SCONTROL);
510 	sctrl |= MSR_HV_SCTRL_ENABLE;
511 	wrmsr(MSR_HV_SCONTROL, sctrl);
512 
513 	sc->sc_vcpus[cpu] = rdmsr(MSR_HV_VP_INDEX);
514 
515 	DPRINTF("vcpu%u: SIMP %#llx SIEFP %#llx SCTRL %#llx\n",
516 	    sc->sc_vcpus[cpu], simp, siefp, sctrl);
517 
518 	return (0);
519 }
520 
521 int
522 hv_cmd(struct hv_softc *sc, void *cmd, size_t cmdlen, void *rsp,
523     size_t rsplen, int flags)
524 {
525 	struct hv_msg msg;
526 	int rv;
527 
528 	if (cmdlen > VMBUS_MSG_DSIZE_MAX) {
529 		printf("%s: payload too large (%lu)\n", sc->sc_dev.dv_xname,
530 		    cmdlen);
531 		return (EMSGSIZE);
532 	}
533 
534 	memset(&msg, 0, sizeof(msg));
535 
536 	msg.msg_req.hc_dsize = cmdlen;
537 	memcpy(msg.msg_req.hc_data, cmd, cmdlen);
538 
539 	if (!(flags & HCF_NOREPLY)) {
540 		msg.msg_rsp = rsp;
541 		msg.msg_rsplen = rsplen;
542 	} else
543 		msg.msg_flags |= MSGF_NOQUEUE;
544 
545 	if (flags & HCF_NOSLEEP)
546 		msg.msg_flags |= MSGF_NOSLEEP;
547 
548 	if ((rv = hv_start(sc, &msg)) != 0)
549 		return (rv);
550 	return (hv_reply(sc, &msg));
551 }
552 
553 int
554 hv_start(struct hv_softc *sc, struct hv_msg *msg)
555 {
556 	const int delays[] = { 100, 100, 100, 500, 500, 5000, 5000, 5000 };
557 	const char *wchan = "hvstart";
558 	uint16_t status;
559 	int i, s;
560 
561 	msg->msg_req.hc_connid = VMBUS_CONNID_MESSAGE;
562 	msg->msg_req.hc_msgtype = 1;
563 
564 	if (!(msg->msg_flags & MSGF_NOQUEUE)) {
565 		mtx_enter(&sc->sc_reqlck);
566 		TAILQ_INSERT_TAIL(&sc->sc_reqs, msg, msg_entry);
567 		mtx_leave(&sc->sc_reqlck);
568 	}
569 
570 	for (i = 0; i < nitems(delays); i++) {
571 		status = hv_hypercall(sc, HYPERCALL_POST_MESSAGE,
572 		    &msg->msg_req, NULL);
573 		if (status == HYPERCALL_STATUS_SUCCESS)
574 			break;
575 		if (msg->msg_flags & MSGF_NOSLEEP) {
576 			delay(delays[i]);
577 			s = splnet();
578 			hv_intr();
579 			splx(s);
580 		} else {
581 			tsleep_nsec(wchan, PRIBIO, wchan,
582 			    USEC_TO_NSEC(delays[i]));
583 		}
584 	}
585 	if (status != 0) {
586 		printf("%s: posting vmbus message failed with %d\n",
587 		    sc->sc_dev.dv_xname, status);
588 		if (!(msg->msg_flags & MSGF_NOQUEUE)) {
589 			mtx_enter(&sc->sc_reqlck);
590 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
591 			mtx_leave(&sc->sc_reqlck);
592 		}
593 		return (EIO);
594 	}
595 
596 	return (0);
597 }
598 
599 static int
600 hv_reply_done(struct hv_softc *sc, struct hv_msg *msg)
601 {
602 	struct hv_msg *m;
603 
604 	mtx_enter(&sc->sc_rsplck);
605 	TAILQ_FOREACH(m, &sc->sc_rsps, msg_entry) {
606 		if (m == msg) {
607 			mtx_leave(&sc->sc_rsplck);
608 			return (1);
609 		}
610 	}
611 	mtx_leave(&sc->sc_rsplck);
612 	return (0);
613 }
614 
615 int
616 hv_reply(struct hv_softc *sc, struct hv_msg *msg)
617 {
618 	if (msg->msg_flags & MSGF_NOQUEUE)
619 		return (0);
620 
621 	hv_wait(sc, hv_reply_done, msg, msg, "hvreply");
622 
623 	mtx_enter(&sc->sc_rsplck);
624 	TAILQ_REMOVE(&sc->sc_rsps, msg, msg_entry);
625 	mtx_leave(&sc->sc_rsplck);
626 
627 	return (0);
628 }
629 
630 void
631 hv_wait(struct hv_softc *sc, int (*cond)(struct hv_softc *, struct hv_msg *),
632     struct hv_msg *msg, void *wchan, const char *wmsg)
633 {
634 	int s;
635 
636 	KASSERT(cold ? msg->msg_flags & MSGF_NOSLEEP : 1);
637 
638 	while (!cond(sc, msg)) {
639 		if (msg->msg_flags & MSGF_NOSLEEP) {
640 			delay(1000);
641 			s = splnet();
642 			hv_intr();
643 			splx(s);
644 		} else {
645 			tsleep_nsec(wchan, PRIBIO, wmsg ? wmsg : "hvwait",
646 			    USEC_TO_NSEC(1000));
647 		}
648 	}
649 }
650 
651 uint16_t
652 hv_intr_signal(struct hv_softc *sc, void *con)
653 {
654 	uint64_t status;
655 
656 	status = hv_hypercall(sc, HYPERCALL_SIGNAL_EVENT, con, NULL);
657 	return ((uint16_t)status);
658 }
659 
660 void
661 hv_intr(void)
662 {
663 	struct hv_softc *sc = hv_sc;
664 
665 	hv_event_intr(sc);
666 	hv_message_intr(sc);
667 }
668 
669 void
670 hv_event_intr(struct hv_softc *sc)
671 {
672 	struct vmbus_evtflags *evt;
673 	struct cpu_info *ci = curcpu();
674 	int cpu = CPU_INFO_UNIT(ci);
675 	int bit, row, maxrow, chanid;
676 	struct hv_channel *ch;
677 	u_long *revents, pending;
678 
679 	evt = (struct vmbus_evtflags *)sc->sc_siep[cpu] +
680 	    VMBUS_SINT_MESSAGE;
681 	if ((sc->sc_proto == VMBUS_VERSION_WS2008) ||
682 	    (sc->sc_proto == VMBUS_VERSION_WIN7)) {
683 		if (!test_bit(0, &evt->evt_flags[0]))
684 			return;
685 		clear_bit(0, &evt->evt_flags[0]);
686 		maxrow = VMBUS_CHAN_MAX_COMPAT / VMBUS_EVTFLAG_LEN;
687 		/*
688 		 * receive size is 1/2 page and divide that by 4 bytes
689 		 */
690 		revents = sc->sc_revents;
691 	} else {
692 		maxrow = nitems(evt->evt_flags);
693 		/*
694 		 * On Host with Win8 or above, the event page can be
695 		 * checked directly to get the id of the channel
696 		 * that has the pending interrupt.
697 		 */
698 		revents = &evt->evt_flags[0];
699 	}
700 
701 	for (row = 0; row < maxrow; row++) {
702 		if (revents[row] == 0)
703 			continue;
704 		pending = atomic_swap_ulong(&revents[row], 0);
705 		for (bit = 0; pending > 0; pending >>= 1, bit++) {
706 			if ((pending & 1) == 0)
707 				continue;
708 			chanid = (row * LONG_BIT) + bit;
709 			/* vmbus channel protocol message */
710 			if (chanid == 0)
711 				continue;
712 			ch = hv_channel_lookup(sc, chanid);
713 			if (ch == NULL) {
714 				printf("%s: unhandled event on %d\n",
715 				    sc->sc_dev.dv_xname, chanid);
716 				continue;
717 			}
718 			if (ch->ch_state != HV_CHANSTATE_OPENED) {
719 				printf("%s: channel %d is not active\n",
720 				    sc->sc_dev.dv_xname, chanid);
721 				continue;
722 			}
723 			ch->ch_evcnt.ec_count++;
724 			hv_channel_schedule(ch);
725 		}
726 	}
727 }
728 
729 void
730 hv_message_intr(struct hv_softc *sc)
731 {
732 	struct vmbus_message *msg;
733 	struct vmbus_chanmsg_hdr *hdr;
734 	struct cpu_info *ci = curcpu();
735 	int cpu = CPU_INFO_UNIT(ci);
736 
737 	for (;;) {
738 		msg = (struct vmbus_message *)sc->sc_simp[cpu] +
739 		    VMBUS_SINT_MESSAGE;
740 		if (msg->msg_type == VMBUS_MSGTYPE_NONE)
741 			break;
742 
743 		hdr = (struct vmbus_chanmsg_hdr *)msg->msg_data;
744 		if (hdr->chm_type >= VMBUS_CHANMSG_COUNT) {
745 			printf("%s: unhandled message type %u flags %#x\n",
746 			    sc->sc_dev.dv_xname, hdr->chm_type,
747 			    msg->msg_flags);
748 			goto skip;
749 		}
750 		if (hv_msg_dispatch[hdr->chm_type].hmd_handler)
751 			hv_msg_dispatch[hdr->chm_type].hmd_handler(sc, hdr);
752 		else
753 			printf("%s: unhandled message type %u\n",
754 			    sc->sc_dev.dv_xname, hdr->chm_type);
755  skip:
756 		msg->msg_type = VMBUS_MSGTYPE_NONE;
757 		virtio_membar_sync();
758 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING)
759 			wrmsr(MSR_HV_EOM, 0);
760 	}
761 }
762 
763 void
764 hv_channel_response(struct hv_softc *sc, struct vmbus_chanmsg_hdr *rsphdr)
765 {
766 	struct hv_msg *msg;
767 	struct vmbus_chanmsg_hdr *reqhdr;
768 	int req;
769 
770 	req = hv_msg_dispatch[rsphdr->chm_type].hmd_request;
771 	mtx_enter(&sc->sc_reqlck);
772 	TAILQ_FOREACH(msg, &sc->sc_reqs, msg_entry) {
773 		reqhdr = (struct vmbus_chanmsg_hdr *)&msg->msg_req.hc_data;
774 		if (reqhdr->chm_type == req) {
775 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
776 			break;
777 		}
778 	}
779 	mtx_leave(&sc->sc_reqlck);
780 	if (msg != NULL) {
781 		memcpy(msg->msg_rsp, rsphdr, msg->msg_rsplen);
782 		mtx_enter(&sc->sc_rsplck);
783 		TAILQ_INSERT_TAIL(&sc->sc_rsps, msg, msg_entry);
784 		mtx_leave(&sc->sc_rsplck);
785 		wakeup(msg);
786 	}
787 }
788 
789 void
790 hv_channel_offer(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr)
791 {
792 	struct hv_offer *co;
793 
794 	co = malloc(sizeof(*co), M_DEVBUF, M_NOWAIT | M_ZERO);
795 	if (co == NULL) {
796 		printf("%s: failed to allocate an offer object\n",
797 		    sc->sc_dev.dv_xname);
798 		return;
799 	}
800 
801 	memcpy(&co->co_chan, hdr, sizeof(co->co_chan));
802 
803 	mtx_enter(&sc->sc_offerlck);
804 	SIMPLEQ_INSERT_TAIL(&sc->sc_offers, co, co_entry);
805 	mtx_leave(&sc->sc_offerlck);
806 }
807 
808 void
809 hv_channel_rescind(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr)
810 {
811 	const struct vmbus_chanmsg_chrescind *cmd;
812 
813 	cmd = (const struct vmbus_chanmsg_chrescind *)hdr;
814 	printf("%s: revoking channel %u\n", sc->sc_dev.dv_xname,
815 	    cmd->chm_chanid);
816 }
817 
818 void
819 hv_channel_delivered(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr)
820 {
821 	atomic_setbits_int(&sc->sc_flags, HSF_OFFERS_DELIVERED);
822 	wakeup(&sc->sc_offers);
823 }
824 
825 int
826 hv_vmbus_connect(struct hv_softc *sc)
827 {
828 	const uint32_t versions[] = {
829 		VMBUS_VERSION_WIN10,
830 		VMBUS_VERSION_WIN8_1, VMBUS_VERSION_WIN8,
831 		VMBUS_VERSION_WIN7, VMBUS_VERSION_WS2008
832 	};
833 	struct vmbus_chanmsg_connect cmd;
834 	struct vmbus_chanmsg_connect_resp rsp;
835 	paddr_t epa, mpa1, mpa2;
836 	int i;
837 
838 	sc->sc_events = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait);
839 	if (sc->sc_events == NULL) {
840 		printf(": failed to allocate channel port events page\n");
841 		goto errout;
842 	}
843 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_events, &epa)) {
844 		printf(": channel port events page PA extraction failed\n");
845 		goto errout;
846 	}
847 
848 	sc->sc_wevents = (u_long *)sc->sc_events;
849 	sc->sc_revents = (u_long *)((caddr_t)sc->sc_events + (PAGE_SIZE >> 1));
850 
851 	sc->sc_monitor[0] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait);
852 	if (sc->sc_monitor[0] == NULL) {
853 		printf(": failed to allocate monitor page 1\n");
854 		goto errout;
855 	}
856 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_monitor[0], &mpa1)) {
857 		printf(": monitor page 1 PA extraction failed\n");
858 		goto errout;
859 	}
860 
861 	sc->sc_monitor[1] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait);
862 	if (sc->sc_monitor[1] == NULL) {
863 		printf(": failed to allocate monitor page 2\n");
864 		goto errout;
865 	}
866 	if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_monitor[1], &mpa2)) {
867 		printf(": monitor page 2 PA extraction failed\n");
868 		goto errout;
869 	}
870 
871 	memset(&cmd, 0, sizeof(cmd));
872 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CONNECT;
873 	cmd.chm_evtflags = (uint64_t)epa;
874 	cmd.chm_mnf1 = (uint64_t)mpa1;
875 	cmd.chm_mnf2 = (uint64_t)mpa2;
876 
877 	memset(&rsp, 0, sizeof(rsp));
878 
879 	for (i = 0; i < nitems(versions); i++) {
880 		cmd.chm_ver = versions[i];
881 		if (hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
882 		    HCF_NOSLEEP)) {
883 			DPRINTF("%s: CONNECT failed\n",
884 			    sc->sc_dev.dv_xname);
885 			goto errout;
886 		}
887 		if (rsp.chm_done) {
888 			sc->sc_flags |= HSF_CONNECTED;
889 			sc->sc_proto = versions[i];
890 			sc->sc_handle = VMBUS_GPADL_START;
891 			break;
892 		}
893 	}
894 	if (i == nitems(versions)) {
895 		printf("%s: failed to negotiate protocol version\n",
896 		    sc->sc_dev.dv_xname);
897 		goto errout;
898 	}
899 
900 	return (0);
901 
902  errout:
903 	if (sc->sc_events) {
904 		km_free(sc->sc_events, PAGE_SIZE, &kv_any, &kp_zero);
905 		sc->sc_events = NULL;
906 		sc->sc_wevents = NULL;
907 		sc->sc_revents = NULL;
908 	}
909 	if (sc->sc_monitor[0]) {
910 		km_free(sc->sc_monitor[0], PAGE_SIZE, &kv_any, &kp_zero);
911 		sc->sc_monitor[0] = NULL;
912 	}
913 	if (sc->sc_monitor[1]) {
914 		km_free(sc->sc_monitor[1], PAGE_SIZE, &kv_any, &kp_zero);
915 		sc->sc_monitor[1] = NULL;
916 	}
917 	return (-1);
918 }
919 
920 #ifdef HYPERV_DEBUG
921 static inline char *
922 guidprint(struct hv_guid *a)
923 {
924 	/* 3     0  5  4 7 6  8 9  10        15 */
925 	/* 33221100-5544-7766-9988-FFEEDDCCBBAA */
926 	static char buf[16 * 2 + 4 + 1];
927 	int i, j = 0;
928 
929 	for (i = 3; i != -1; i -= 1, j += 2)
930 		snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]);
931 	buf[j++] = '-';
932 	for (i = 5; i != 3; i -= 1, j += 2)
933 		snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]);
934 	buf[j++] = '-';
935 	for (i = 7; i != 5; i -= 1, j += 2)
936 		snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]);
937 	buf[j++] = '-';
938 	for (i = 8; i < 10; i += 1, j += 2)
939 		snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]);
940 	buf[j++] = '-';
941 	for (i = 10; i < 16; i += 1, j += 2)
942 		snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]);
943 	return (&buf[0]);
944 }
945 #endif	/* HYPERV_DEBUG */
946 
947 void
948 hv_guid_sprint(struct hv_guid *guid, char *str, size_t size)
949 {
950 	const struct {
951 		const struct hv_guid	*guid;
952 		const char		*ident;
953 	} map[] = {
954 		{ &hv_guid_network,	"network" },
955 		{ &hv_guid_ide,		"ide" },
956 		{ &hv_guid_scsi,	"scsi" },
957 		{ &hv_guid_shutdown,	"shutdown" },
958 		{ &hv_guid_timesync,	"timesync" },
959 		{ &hv_guid_heartbeat,	"heartbeat" },
960 		{ &hv_guid_kvp,		"kvp" },
961 #ifdef HYPERV_DEBUG
962 		{ &hv_guid_vss,		"vss" },
963 		{ &hv_guid_dynmem,	"dynamic-memory" },
964 		{ &hv_guid_mouse,	"mouse" },
965 		{ &hv_guid_kbd,		"keyboard" },
966 		{ &hv_guid_video,	"video" },
967 		{ &hv_guid_fc,		"fiber-channel" },
968 		{ &hv_guid_fcopy,	"file-copy" },
969 		{ &hv_guid_pcie,	"pcie-passthrough" },
970 		{ &hv_guid_netdir,	"network-direct" },
971 		{ &hv_guid_rdesktop,	"remote-desktop" },
972 		{ &hv_guid_avma1,	"avma-1" },
973 		{ &hv_guid_avma2,	"avma-2" },
974 		{ &hv_guid_avma3,	"avma-3" },
975 		{ &hv_guid_avma4,	"avma-4" },
976 #endif
977 	};
978 	int i;
979 
980 	for (i = 0; i < nitems(map); i++) {
981 		if (memcmp(guid, map[i].guid, sizeof(*guid)) == 0) {
982 			strlcpy(str, map[i].ident, size);
983 			return;
984 		}
985 	}
986 #ifdef HYPERV_DEBUG
987 	strlcpy(str, guidprint(guid), size);
988 #endif
989 }
990 
991 static int
992 hv_channel_scan_done(struct hv_softc *sc, struct hv_msg *msg __unused)
993 {
994 	return (sc->sc_flags & HSF_OFFERS_DELIVERED);
995 }
996 
997 int
998 hv_channel_scan(struct hv_softc *sc)
999 {
1000 	struct vmbus_chanmsg_hdr hdr;
1001 	struct vmbus_chanmsg_choffer rsp;
1002 	struct hv_offer *co;
1003 
1004 	SIMPLEQ_INIT(&sc->sc_offers);
1005 	mtx_init(&sc->sc_offerlck, IPL_NET);
1006 
1007 	memset(&hdr, 0, sizeof(hdr));
1008 	hdr.chm_type = VMBUS_CHANMSG_CHREQUEST;
1009 
1010 	if (hv_cmd(sc, &hdr, sizeof(hdr), &rsp, sizeof(rsp),
1011 	    HCF_NOSLEEP | HCF_NOREPLY)) {
1012 		DPRINTF("%s: CHREQUEST failed\n", sc->sc_dev.dv_xname);
1013 		return (-1);
1014 	}
1015 
1016 	hv_wait(sc, hv_channel_scan_done, (struct hv_msg *)&hdr,
1017 	    &sc->sc_offers, "hvscan");
1018 
1019 	TAILQ_INIT(&sc->sc_channels);
1020 	mtx_init(&sc->sc_channelck, IPL_NET);
1021 
1022 	mtx_enter(&sc->sc_offerlck);
1023 	while (!SIMPLEQ_EMPTY(&sc->sc_offers)) {
1024 		co = SIMPLEQ_FIRST(&sc->sc_offers);
1025 		SIMPLEQ_REMOVE_HEAD(&sc->sc_offers, co_entry);
1026 		mtx_leave(&sc->sc_offerlck);
1027 
1028 		hv_process_offer(sc, co);
1029 		free(co, M_DEVBUF, sizeof(*co));
1030 
1031 		mtx_enter(&sc->sc_offerlck);
1032 	}
1033 	mtx_leave(&sc->sc_offerlck);
1034 
1035 	return (0);
1036 }
1037 
1038 void
1039 hv_process_offer(struct hv_softc *sc, struct hv_offer *co)
1040 {
1041 	struct hv_channel *ch, *nch;
1042 
1043 	nch = malloc(sizeof(*nch), M_DEVBUF, M_ZERO | M_NOWAIT);
1044 	if (nch == NULL) {
1045 		printf("%s: failed to allocate memory for the channel\n",
1046 		    sc->sc_dev.dv_xname);
1047 		return;
1048 	}
1049 	nch->ch_sc = sc;
1050 	hv_guid_sprint(&co->co_chan.chm_chtype, nch->ch_ident,
1051 	    sizeof(nch->ch_ident));
1052 
1053 	/*
1054 	 * By default we setup state to enable batched reading.
1055 	 * A specific service can choose to disable this prior
1056 	 * to opening the channel.
1057 	 */
1058 	nch->ch_flags |= CHF_BATCHED;
1059 
1060 	KASSERT((((vaddr_t)&nch->ch_monprm) & 0x7) == 0);
1061 	memset(&nch->ch_monprm, 0, sizeof(nch->ch_monprm));
1062 	nch->ch_monprm.mp_connid = VMBUS_CONNID_EVENT;
1063 
1064 	if (sc->sc_proto != VMBUS_VERSION_WS2008)
1065 		nch->ch_monprm.mp_connid = co->co_chan.chm_connid;
1066 
1067 	if (co->co_chan.chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1068 		nch->ch_mgroup = co->co_chan.chm_montrig / VMBUS_MONTRIG_LEN;
1069 		nch->ch_mindex = co->co_chan.chm_montrig % VMBUS_MONTRIG_LEN;
1070 		nch->ch_flags |= CHF_MONITOR;
1071 	}
1072 
1073 	nch->ch_id = co->co_chan.chm_chanid;
1074 
1075 	memcpy(&nch->ch_type, &co->co_chan.chm_chtype, sizeof(ch->ch_type));
1076 	memcpy(&nch->ch_inst, &co->co_chan.chm_chinst, sizeof(ch->ch_inst));
1077 
1078 	mtx_enter(&sc->sc_channelck);
1079 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
1080 		if (!memcmp(&ch->ch_type, &nch->ch_type, sizeof(ch->ch_type)) &&
1081 		    !memcmp(&ch->ch_inst, &nch->ch_inst, sizeof(ch->ch_inst)))
1082 			break;
1083 	}
1084 	if (ch != NULL) {
1085 		if (co->co_chan.chm_subidx == 0) {
1086 			printf("%s: unknown offer \"%s\"\n",
1087 			    sc->sc_dev.dv_xname, nch->ch_ident);
1088 			mtx_leave(&sc->sc_channelck);
1089 			free(nch, M_DEVBUF, sizeof(*nch));
1090 			return;
1091 		}
1092 #ifdef HYPERV_DEBUG
1093 		printf("%s: subchannel %u for \"%s\"\n", sc->sc_dev.dv_xname,
1094 		    co->co_chan.chm_subidx, ch->ch_ident);
1095 #endif
1096 		mtx_leave(&sc->sc_channelck);
1097 		free(nch, M_DEVBUF, sizeof(*nch));
1098 		return;
1099 	}
1100 
1101 	nch->ch_state = HV_CHANSTATE_OFFERED;
1102 
1103 	TAILQ_INSERT_TAIL(&sc->sc_channels, nch, ch_entry);
1104 	mtx_leave(&sc->sc_channelck);
1105 
1106 #ifdef HYPERV_DEBUG
1107 	printf("%s: channel %u: \"%s\"", sc->sc_dev.dv_xname, nch->ch_id,
1108 	    nch->ch_ident);
1109 	if (nch->ch_flags & CHF_MONITOR)
1110 		printf(", monitor %u\n", co->co_chan.chm_montrig);
1111 	else
1112 		printf("\n");
1113 #endif
1114 }
1115 
1116 struct hv_channel *
1117 hv_channel_lookup(struct hv_softc *sc, uint32_t relid)
1118 {
1119 	struct hv_channel *ch;
1120 
1121 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
1122 		if (ch->ch_id == relid)
1123 			return (ch);
1124 	}
1125 	return (NULL);
1126 }
1127 
1128 int
1129 hv_channel_ring_create(struct hv_channel *ch, uint32_t buflen)
1130 {
1131 	struct hv_softc *sc = ch->ch_sc;
1132 
1133 	buflen = roundup(buflen, PAGE_SIZE) + sizeof(struct vmbus_bufring);
1134 	ch->ch_ring = km_alloc(2 * buflen, &kv_any, &kp_zero, cold ?
1135 	    &kd_nowait : &kd_waitok);
1136 	if (ch->ch_ring == NULL) {
1137 		printf("%s: failed to allocate channel ring\n",
1138 		    sc->sc_dev.dv_xname);
1139 		return (-1);
1140 	}
1141 	ch->ch_ring_size = 2 * buflen;
1142 
1143 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
1144 	ch->ch_wrd.rd_ring = (struct vmbus_bufring *)ch->ch_ring;
1145 	ch->ch_wrd.rd_size = buflen;
1146 	ch->ch_wrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
1147 	mtx_init(&ch->ch_wrd.rd_lock, IPL_NET);
1148 
1149 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
1150 	ch->ch_rrd.rd_ring = (struct vmbus_bufring *)((uint8_t *)ch->ch_ring +
1151 	    buflen);
1152 	ch->ch_rrd.rd_size = buflen;
1153 	ch->ch_rrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
1154 	mtx_init(&ch->ch_rrd.rd_lock, IPL_NET);
1155 
1156 	if (hv_handle_alloc(ch, ch->ch_ring, 2 * buflen, &ch->ch_ring_gpadl)) {
1157 		printf("%s: failed to obtain a PA handle for the ring\n",
1158 		    sc->sc_dev.dv_xname);
1159 		hv_channel_ring_destroy(ch);
1160 		return (-1);
1161 	}
1162 
1163 	return (0);
1164 }
1165 
1166 void
1167 hv_channel_ring_destroy(struct hv_channel *ch)
1168 {
1169 	km_free(ch->ch_ring, ch->ch_ring_size, &kv_any, &kp_zero);
1170 	ch->ch_ring = NULL;
1171 	hv_handle_free(ch, ch->ch_ring_gpadl);
1172 
1173 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
1174 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
1175 }
1176 
1177 int
1178 hv_channel_open(struct hv_channel *ch, size_t buflen, void *udata,
1179     size_t udatalen, void (*handler)(void *), void *arg)
1180 {
1181 	struct hv_softc *sc = ch->ch_sc;
1182 	struct vmbus_chanmsg_chopen cmd;
1183 	struct vmbus_chanmsg_chopen_resp rsp;
1184 	int rv;
1185 
1186 	if (ch->ch_ring == NULL &&
1187 	    hv_channel_ring_create(ch, buflen)) {
1188 		DPRINTF("%s: failed to create channel ring\n",
1189 		    sc->sc_dev.dv_xname);
1190 		return (-1);
1191 	}
1192 
1193 	memset(&cmd, 0, sizeof(cmd));
1194 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHOPEN;
1195 	cmd.chm_openid = ch->ch_id;
1196 	cmd.chm_chanid = ch->ch_id;
1197 	cmd.chm_gpadl = ch->ch_ring_gpadl;
1198 	cmd.chm_txbr_pgcnt = ch->ch_wrd.rd_size >> PAGE_SHIFT;
1199 	cmd.chm_vcpuid = ch->ch_vcpu;
1200 
1201 	if (udata && udatalen > 0)
1202 		memcpy(cmd.chm_udata, udata, udatalen);
1203 
1204 	memset(&rsp, 0, sizeof(rsp));
1205 
1206 	ch->ch_handler = handler;
1207 	ch->ch_ctx = arg;
1208 
1209 	ch->ch_state = HV_CHANSTATE_OPENED;
1210 
1211 	rv = hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
1212 	    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
1213 	if (rv) {
1214 		hv_channel_ring_destroy(ch);
1215 		DPRINTF("%s: CHOPEN failed with %d\n",
1216 		    sc->sc_dev.dv_xname, rv);
1217 		ch->ch_handler = NULL;
1218 		ch->ch_ctx = NULL;
1219 		ch->ch_state = HV_CHANSTATE_OFFERED;
1220 		return (-1);
1221 	}
1222 
1223 	return (0);
1224 }
1225 
1226 int
1227 hv_channel_close(struct hv_channel *ch)
1228 {
1229 	struct hv_softc *sc = ch->ch_sc;
1230 	struct vmbus_chanmsg_chclose cmd;
1231 	int rv;
1232 
1233 	memset(&cmd, 0, sizeof(cmd));
1234 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHCLOSE;
1235 	cmd.chm_chanid = ch->ch_id;
1236 
1237 	ch->ch_state = HV_CHANSTATE_CLOSING;
1238 	rv = hv_cmd(sc, &cmd, sizeof(cmd), NULL, 0, HCF_NOREPLY);
1239 	if (rv) {
1240 		DPRINTF("%s: CHCLOSE failed with %d\n",
1241 		    sc->sc_dev.dv_xname, rv);
1242 		return (-1);
1243 	}
1244 	ch->ch_state = HV_CHANSTATE_CLOSED;
1245 	hv_channel_ring_destroy(ch);
1246 	return (0);
1247 }
1248 
1249 static inline void
1250 hv_channel_setevent(struct hv_softc *sc, struct hv_channel *ch)
1251 {
1252 	struct vmbus_mon_trig *mtg;
1253 
1254 	/* Each uint32_t represents 32 channels */
1255 	set_bit(ch->ch_id, sc->sc_wevents);
1256 	if (ch->ch_flags & CHF_MONITOR) {
1257 		mtg = &sc->sc_monitor[1]->mnf_trigs[ch->ch_mgroup];
1258 		set_bit(ch->ch_mindex, &mtg->mt_pending);
1259 	} else
1260 		hv_intr_signal(sc, &ch->ch_monprm);
1261 }
1262 
1263 void
1264 hv_channel_intr(void *arg)
1265 {
1266 	struct hv_channel *ch = arg;
1267 
1268 	if (hv_channel_ready(ch))
1269 		ch->ch_handler(ch->ch_ctx);
1270 
1271 	if (hv_channel_unpause(ch) == 0)
1272 		return;
1273 
1274 	hv_channel_pause(ch);
1275 	hv_channel_schedule(ch);
1276 }
1277 
1278 int
1279 hv_channel_setdeferred(struct hv_channel *ch, const char *name)
1280 {
1281 	ch->ch_taskq = taskq_create(name, 1, IPL_NET, TASKQ_MPSAFE);
1282 	if (ch->ch_taskq == NULL)
1283 		return (-1);
1284 	task_set(&ch->ch_task, hv_channel_intr, ch);
1285 	return (0);
1286 }
1287 
1288 void
1289 hv_channel_schedule(struct hv_channel *ch)
1290 {
1291 	if (ch->ch_handler) {
1292 		if (!cold && (ch->ch_flags & CHF_BATCHED)) {
1293 			hv_channel_pause(ch);
1294 			task_add(ch->ch_taskq, &ch->ch_task);
1295 		} else
1296 			ch->ch_handler(ch->ch_ctx);
1297 	}
1298 }
1299 
1300 static inline void
1301 hv_ring_put(struct hv_ring_data *wrd, uint8_t *data, uint32_t datalen)
1302 {
1303 	int left = MIN(datalen, wrd->rd_dsize - wrd->rd_prod);
1304 
1305 	memcpy(&wrd->rd_ring->br_data[wrd->rd_prod], data, left);
1306 	memcpy(&wrd->rd_ring->br_data[0], data + left, datalen - left);
1307 	wrd->rd_prod += datalen;
1308 	if (wrd->rd_prod >= wrd->rd_dsize)
1309 		wrd->rd_prod -= wrd->rd_dsize;
1310 }
1311 
1312 static inline void
1313 hv_ring_get(struct hv_ring_data *rrd, uint8_t *data, uint32_t datalen,
1314     int peek)
1315 {
1316 	int left = MIN(datalen, rrd->rd_dsize - rrd->rd_cons);
1317 
1318 	memcpy(data, &rrd->rd_ring->br_data[rrd->rd_cons], left);
1319 	memcpy(data + left, &rrd->rd_ring->br_data[0], datalen - left);
1320 	if (!peek) {
1321 		rrd->rd_cons += datalen;
1322 		if (rrd->rd_cons >= rrd->rd_dsize)
1323 			rrd->rd_cons -= rrd->rd_dsize;
1324 	}
1325 }
1326 
1327 static inline void
1328 hv_ring_avail(struct hv_ring_data *rd, uint32_t *towrite, uint32_t *toread)
1329 {
1330 	uint32_t ridx = rd->rd_ring->br_rindex;
1331 	uint32_t widx = rd->rd_ring->br_windex;
1332 	uint32_t r, w;
1333 
1334 	if (widx >= ridx)
1335 		w = rd->rd_dsize - (widx - ridx);
1336 	else
1337 		w = ridx - widx;
1338 	r = rd->rd_dsize - w;
1339 	if (towrite)
1340 		*towrite = w;
1341 	if (toread)
1342 		*toread = r;
1343 }
1344 
1345 int
1346 hv_ring_write(struct hv_ring_data *wrd, struct iovec *iov, int iov_cnt,
1347     int *needsig)
1348 {
1349 	uint64_t indices = 0;
1350 	uint32_t avail, oprod, datalen = sizeof(indices);
1351 	int i;
1352 
1353 	for (i = 0; i < iov_cnt; i++)
1354 		datalen += iov[i].iov_len;
1355 
1356 	KASSERT(datalen <= wrd->rd_dsize);
1357 
1358 	hv_ring_avail(wrd, &avail, NULL);
1359 	if (avail <= datalen) {
1360 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
1361 		return (EAGAIN);
1362 	}
1363 
1364 	oprod = wrd->rd_prod;
1365 
1366 	for (i = 0; i < iov_cnt; i++)
1367 		hv_ring_put(wrd, iov[i].iov_base, iov[i].iov_len);
1368 
1369 	indices = (uint64_t)oprod << 32;
1370 	hv_ring_put(wrd, (uint8_t *)&indices, sizeof(indices));
1371 
1372 	virtio_membar_sync();
1373 	wrd->rd_ring->br_windex = wrd->rd_prod;
1374 	virtio_membar_sync();
1375 
1376 	/* Signal when the ring transitions from being empty to non-empty */
1377 	if (wrd->rd_ring->br_imask == 0 &&
1378 	    wrd->rd_ring->br_rindex == oprod)
1379 		*needsig = 1;
1380 	else
1381 		*needsig = 0;
1382 
1383 	return (0);
1384 }
1385 
1386 int
1387 hv_channel_send(struct hv_channel *ch, void *data, uint32_t datalen,
1388     uint64_t rid, int type, uint32_t flags)
1389 {
1390 	struct hv_softc *sc = ch->ch_sc;
1391 	struct vmbus_chanpkt cp;
1392 	struct iovec iov[3];
1393 	uint32_t pktlen, pktlen_aligned;
1394 	uint64_t zeropad = 0;
1395 	int rv, needsig = 0;
1396 
1397 	pktlen = sizeof(cp) + datalen;
1398 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
1399 
1400 	cp.cp_hdr.cph_type = type;
1401 	cp.cp_hdr.cph_flags = flags;
1402 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp));
1403 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
1404 	cp.cp_hdr.cph_tid = rid;
1405 
1406 	iov[0].iov_base = &cp;
1407 	iov[0].iov_len = sizeof(cp);
1408 
1409 	iov[1].iov_base = data;
1410 	iov[1].iov_len = datalen;
1411 
1412 	iov[2].iov_base = &zeropad;
1413 	iov[2].iov_len = pktlen_aligned - pktlen;
1414 
1415 	mtx_enter(&ch->ch_wrd.rd_lock);
1416 	rv = hv_ring_write(&ch->ch_wrd, iov, 3, &needsig);
1417 	mtx_leave(&ch->ch_wrd.rd_lock);
1418 	if (rv == 0 && needsig)
1419 		hv_channel_setevent(sc, ch);
1420 
1421 	return (rv);
1422 }
1423 
1424 int
1425 hv_channel_send_sgl(struct hv_channel *ch, struct vmbus_gpa *sgl,
1426     uint32_t nsge, void *data, uint32_t datalen, uint64_t rid)
1427 {
1428 	struct hv_softc *sc = ch->ch_sc;
1429 	struct vmbus_chanpkt_sglist cp;
1430 	struct iovec iov[4];
1431 	uint32_t buflen, pktlen, pktlen_aligned;
1432 	uint64_t zeropad = 0;
1433 	int rv, needsig = 0;
1434 
1435 	buflen = sizeof(struct vmbus_gpa) * nsge;
1436 	pktlen = sizeof(cp) + datalen + buflen;
1437 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
1438 
1439 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1440 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1441 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
1442 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
1443 	cp.cp_hdr.cph_tid = rid;
1444 	cp.cp_gpa_cnt = nsge;
1445 	cp.cp_rsvd = 0;
1446 
1447 	iov[0].iov_base = &cp;
1448 	iov[0].iov_len = sizeof(cp);
1449 
1450 	iov[1].iov_base = sgl;
1451 	iov[1].iov_len = buflen;
1452 
1453 	iov[2].iov_base = data;
1454 	iov[2].iov_len = datalen;
1455 
1456 	iov[3].iov_base = &zeropad;
1457 	iov[3].iov_len = pktlen_aligned - pktlen;
1458 
1459 	mtx_enter(&ch->ch_wrd.rd_lock);
1460 	rv = hv_ring_write(&ch->ch_wrd, iov, 4, &needsig);
1461 	mtx_leave(&ch->ch_wrd.rd_lock);
1462 	if (rv == 0 && needsig)
1463 		hv_channel_setevent(sc, ch);
1464 
1465 	return (rv);
1466 }
1467 
1468 int
1469 hv_channel_send_prpl(struct hv_channel *ch, struct vmbus_gpa_range *prpl,
1470     uint32_t nprp, void *data, uint32_t datalen, uint64_t rid)
1471 {
1472 	struct hv_softc *sc = ch->ch_sc;
1473 	struct vmbus_chanpkt_prplist cp;
1474 	struct iovec iov[4];
1475 	uint32_t buflen, pktlen, pktlen_aligned;
1476 	uint64_t zeropad = 0;
1477 	int rv, needsig = 0;
1478 
1479 	buflen = sizeof(struct vmbus_gpa_range) * (nprp + 1);
1480 	pktlen = sizeof(cp) + datalen + buflen;
1481 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
1482 
1483 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1484 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1485 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
1486 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
1487 	cp.cp_hdr.cph_tid = rid;
1488 	cp.cp_range_cnt = 1;
1489 	cp.cp_rsvd = 0;
1490 
1491 	iov[0].iov_base = &cp;
1492 	iov[0].iov_len = sizeof(cp);
1493 
1494 	iov[1].iov_base = prpl;
1495 	iov[1].iov_len = buflen;
1496 
1497 	iov[2].iov_base = data;
1498 	iov[2].iov_len = datalen;
1499 
1500 	iov[3].iov_base = &zeropad;
1501 	iov[3].iov_len = pktlen_aligned - pktlen;
1502 
1503 	mtx_enter(&ch->ch_wrd.rd_lock);
1504 	rv = hv_ring_write(&ch->ch_wrd, iov, 4, &needsig);
1505 	mtx_leave(&ch->ch_wrd.rd_lock);
1506 	if (rv == 0 && needsig)
1507 		hv_channel_setevent(sc, ch);
1508 
1509 	return (rv);
1510 }
1511 
1512 int
1513 hv_ring_peek(struct hv_ring_data *rrd, void *data, uint32_t datalen)
1514 {
1515 	uint32_t avail;
1516 
1517 	KASSERT(datalen <= rrd->rd_dsize);
1518 
1519 	hv_ring_avail(rrd, NULL, &avail);
1520 	if (avail < datalen)
1521 		return (EAGAIN);
1522 
1523 	hv_ring_get(rrd, (uint8_t *)data, datalen, 1);
1524 	return (0);
1525 }
1526 
1527 int
1528 hv_ring_read(struct hv_ring_data *rrd, void *data, uint32_t datalen,
1529     uint32_t offset)
1530 {
1531 	uint64_t indices;
1532 	uint32_t avail;
1533 
1534 	KASSERT(datalen <= rrd->rd_dsize);
1535 
1536 	hv_ring_avail(rrd, NULL, &avail);
1537 	if (avail < datalen) {
1538 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
1539 		return (EAGAIN);
1540 	}
1541 
1542 	if (offset) {
1543 		rrd->rd_cons += offset;
1544 		if (rrd->rd_cons >= rrd->rd_dsize)
1545 			rrd->rd_cons -= rrd->rd_dsize;
1546 	}
1547 
1548 	hv_ring_get(rrd, (uint8_t *)data, datalen, 0);
1549 	hv_ring_get(rrd, (uint8_t *)&indices, sizeof(indices), 0);
1550 
1551 	virtio_membar_sync();
1552 	rrd->rd_ring->br_rindex = rrd->rd_cons;
1553 
1554 	return (0);
1555 }
1556 
1557 int
1558 hv_channel_recv(struct hv_channel *ch, void *data, uint32_t datalen,
1559     uint32_t *rlen, uint64_t *rid, int raw)
1560 {
1561 	struct vmbus_chanpkt_hdr cph;
1562 	uint32_t offset, pktlen;
1563 	int rv;
1564 
1565 	*rlen = 0;
1566 
1567 	mtx_enter(&ch->ch_rrd.rd_lock);
1568 
1569 	if ((rv = hv_ring_peek(&ch->ch_rrd, &cph, sizeof(cph))) != 0) {
1570 		mtx_leave(&ch->ch_rrd.rd_lock);
1571 		return (rv);
1572 	}
1573 
1574 	offset = raw ? 0 : VMBUS_CHANPKT_GETLEN(cph.cph_hlen);
1575 	pktlen = VMBUS_CHANPKT_GETLEN(cph.cph_tlen) - offset;
1576 	if (pktlen > datalen) {
1577 		mtx_leave(&ch->ch_rrd.rd_lock);
1578 		printf("%s: pktlen %u datalen %u\n", __func__, pktlen, datalen);
1579 		return (EINVAL);
1580 	}
1581 
1582 	rv = hv_ring_read(&ch->ch_rrd, data, pktlen, offset);
1583 	if (rv == 0) {
1584 		*rlen = pktlen;
1585 		*rid = cph.cph_tid;
1586 	}
1587 
1588 	mtx_leave(&ch->ch_rrd.rd_lock);
1589 
1590 	return (rv);
1591 }
1592 
1593 static inline void
1594 hv_ring_mask(struct hv_ring_data *rd)
1595 {
1596 	virtio_membar_sync();
1597 	rd->rd_ring->br_imask = 1;
1598 	virtio_membar_sync();
1599 }
1600 
1601 static inline void
1602 hv_ring_unmask(struct hv_ring_data *rd)
1603 {
1604 	virtio_membar_sync();
1605 	rd->rd_ring->br_imask = 0;
1606 	virtio_membar_sync();
1607 }
1608 
1609 void
1610 hv_channel_pause(struct hv_channel *ch)
1611 {
1612 	hv_ring_mask(&ch->ch_rrd);
1613 }
1614 
1615 uint
1616 hv_channel_unpause(struct hv_channel *ch)
1617 {
1618 	uint32_t avail;
1619 
1620 	hv_ring_unmask(&ch->ch_rrd);
1621 	hv_ring_avail(&ch->ch_rrd, NULL, &avail);
1622 
1623 	return (avail);
1624 }
1625 
1626 uint
1627 hv_channel_ready(struct hv_channel *ch)
1628 {
1629 	uint32_t avail;
1630 
1631 	hv_ring_avail(&ch->ch_rrd, NULL, &avail);
1632 
1633 	return (avail);
1634 }
1635 
1636 /* How many PFNs can be referenced by the header */
1637 #define HV_NPFNHDR	((VMBUS_MSG_DSIZE_MAX -	\
1638 	  sizeof(struct vmbus_chanmsg_gpadl_conn)) / sizeof(uint64_t))
1639 
1640 /* How many PFNs can be referenced by the body */
1641 #define HV_NPFNBODY	((VMBUS_MSG_DSIZE_MAX -	\
1642 	  sizeof(struct vmbus_chanmsg_gpadl_subconn)) / sizeof(uint64_t))
1643 
1644 int
1645 hv_handle_alloc(struct hv_channel *ch, void *buffer, uint32_t buflen,
1646     uint32_t *handle)
1647 {
1648 	struct hv_softc *sc = ch->ch_sc;
1649 	struct vmbus_chanmsg_gpadl_conn *hdr;
1650 	struct vmbus_chanmsg_gpadl_subconn *cmd;
1651 	struct vmbus_chanmsg_gpadl_connresp rsp;
1652 	struct hv_msg *msg;
1653 	int i, j, last, left, rv;
1654 	int bodylen = 0, ncmds = 0, pfn = 0;
1655 	int waitflag = cold ? M_NOWAIT : M_WAITOK;
1656 	uint64_t *frames;
1657 	paddr_t pa;
1658 	caddr_t body;
1659 	/* Total number of pages to reference */
1660 	int total = atop(buflen);
1661 	/* Number of pages that will fit the header */
1662 	int inhdr = MIN(total, HV_NPFNHDR);
1663 
1664 	KASSERT((buflen & (PAGE_SIZE - 1)) == 0);
1665 
1666 	if ((msg = malloc(sizeof(*msg), M_DEVBUF, M_ZERO | waitflag)) == NULL)
1667 		return (ENOMEM);
1668 
1669 	/* Prepare array of frame addresses */
1670 	if ((frames = mallocarray(total, sizeof(*frames), M_DEVBUF, M_ZERO |
1671 	    waitflag)) == NULL) {
1672 		free(msg, M_DEVBUF, sizeof(*msg));
1673 		return (ENOMEM);
1674 	}
1675 	for (i = 0; i < total; i++) {
1676 		if (!pmap_extract(pmap_kernel(), (vaddr_t)buffer +
1677 		    PAGE_SIZE * i, &pa)) {
1678 			free(msg, M_DEVBUF, sizeof(*msg));
1679 			free(frames, M_DEVBUF, total * sizeof(*frames));
1680 			return (EFAULT);
1681 		}
1682 		frames[i] = atop(pa);
1683 	}
1684 
1685 	msg->msg_req.hc_dsize = sizeof(struct vmbus_chanmsg_gpadl_conn) +
1686 	    inhdr * sizeof(uint64_t);
1687 	hdr = (struct vmbus_chanmsg_gpadl_conn *)msg->msg_req.hc_data;
1688 	msg->msg_rsp = &rsp;
1689 	msg->msg_rsplen = sizeof(rsp);
1690 	if (waitflag == M_NOWAIT)
1691 		msg->msg_flags = MSGF_NOSLEEP;
1692 
1693 	left = total - inhdr;
1694 
1695 	/* Allocate additional gpadl_body structures if required */
1696 	if (left > 0) {
1697 		ncmds = MAX(1, left / HV_NPFNBODY + left % HV_NPFNBODY);
1698 		bodylen = ncmds * VMBUS_MSG_DSIZE_MAX;
1699 		body = malloc(bodylen, M_DEVBUF, M_ZERO | waitflag);
1700 		if (body == NULL) {
1701 			free(msg, M_DEVBUF, sizeof(*msg));
1702 			free(frames, M_DEVBUF, atop(buflen) * sizeof(*frames));
1703 			return (ENOMEM);
1704 		}
1705 	}
1706 
1707 	*handle = atomic_inc_int_nv(&sc->sc_handle);
1708 
1709 	hdr->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_CONN;
1710 	hdr->chm_chanid = ch->ch_id;
1711 	hdr->chm_gpadl = *handle;
1712 
1713 	/* Single range for a contiguous buffer */
1714 	hdr->chm_range_cnt = 1;
1715 	hdr->chm_range_len = sizeof(struct vmbus_gpa_range) + total *
1716 	    sizeof(uint64_t);
1717 	hdr->chm_range.gpa_ofs = 0;
1718 	hdr->chm_range.gpa_len = buflen;
1719 
1720 	/* Fit as many pages as possible into the header */
1721 	for (i = 0; i < inhdr; i++)
1722 		hdr->chm_range.gpa_page[i] = frames[pfn++];
1723 
1724 	for (i = 0; i < ncmds; i++) {
1725 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
1726 		    VMBUS_MSG_DSIZE_MAX * i);
1727 		cmd->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_SUBCONN;
1728 		cmd->chm_gpadl = *handle;
1729 		last = MIN(left, HV_NPFNBODY);
1730 		for (j = 0; j < last; j++)
1731 			cmd->chm_gpa_page[j] = frames[pfn++];
1732 		left -= last;
1733 	}
1734 
1735 	rv = hv_start(sc, msg);
1736 	if (rv != 0) {
1737 		DPRINTF("%s: GPADL_CONN failed\n", sc->sc_dev.dv_xname);
1738 		goto out;
1739 	}
1740 	for (i = 0; i < ncmds; i++) {
1741 		int cmdlen = sizeof(*cmd);
1742 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
1743 		    VMBUS_MSG_DSIZE_MAX * i);
1744 		/* Last element can be short */
1745 		if (i == ncmds - 1)
1746 			cmdlen += last * sizeof(uint64_t);
1747 		else
1748 			cmdlen += HV_NPFNBODY * sizeof(uint64_t);
1749 		rv = hv_cmd(sc, cmd, cmdlen, NULL, 0, waitflag | HCF_NOREPLY);
1750 		if (rv != 0) {
1751 			DPRINTF("%s: GPADL_SUBCONN (iteration %d/%d) failed "
1752 			    "with %d\n", sc->sc_dev.dv_xname, i, ncmds, rv);
1753 			goto out;
1754 		}
1755 	}
1756 	rv = hv_reply(sc, msg);
1757 	if (rv != 0)
1758 		DPRINTF("%s: GPADL allocation failed with %d\n",
1759 		    sc->sc_dev.dv_xname, rv);
1760 
1761  out:
1762 	free(msg, M_DEVBUF, sizeof(*msg));
1763 	free(frames, M_DEVBUF, total * sizeof(*frames));
1764 	if (bodylen > 0)
1765 		free(body, M_DEVBUF, bodylen);
1766 	if (rv != 0)
1767 		return (rv);
1768 
1769 	KASSERT(*handle == rsp.chm_gpadl);
1770 
1771 	return (0);
1772 }
1773 
1774 void
1775 hv_handle_free(struct hv_channel *ch, uint32_t handle)
1776 {
1777 	struct hv_softc *sc = ch->ch_sc;
1778 	struct vmbus_chanmsg_gpadl_disconn cmd;
1779 	struct vmbus_chanmsg_gpadl_disconn rsp;
1780 	int rv;
1781 
1782 	memset(&cmd, 0, sizeof(cmd));
1783 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_DISCONN;
1784 	cmd.chm_chanid = ch->ch_id;
1785 	cmd.chm_gpadl = handle;
1786 
1787 	rv = hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp), cold ?
1788 	    HCF_NOSLEEP : 0);
1789 	if (rv)
1790 		DPRINTF("%s: GPADL_DISCONN failed with %d\n",
1791 		    sc->sc_dev.dv_xname, rv);
1792 }
1793 
1794 static int
1795 hv_attach_print(void *aux, const char *name)
1796 {
1797 	struct hv_attach_args *aa = aux;
1798 
1799 	if (name)
1800 		printf("\"%s\" at %s", aa->aa_ident, name);
1801 
1802 	return (UNCONF);
1803 }
1804 
1805 int
1806 hv_attach_devices(struct hv_softc *sc)
1807 {
1808 	struct hv_dev *dv;
1809 	struct hv_channel *ch;
1810 
1811 	SLIST_INIT(&sc->sc_devs);
1812 	mtx_init(&sc->sc_devlck, IPL_NET);
1813 
1814 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
1815 		if (ch->ch_state != HV_CHANSTATE_OFFERED)
1816 			continue;
1817 		if (!(ch->ch_flags & CHF_MONITOR))
1818 			continue;
1819 		dv = malloc(sizeof(*dv), M_DEVBUF, M_ZERO | M_NOWAIT);
1820 		if (dv == NULL) {
1821 			printf("%s: failed to allocate device object\n",
1822 			    sc->sc_dev.dv_xname);
1823 			return (-1);
1824 		}
1825 		dv->dv_aa.aa_parent = sc;
1826 		dv->dv_aa.aa_type = &ch->ch_type;
1827 		dv->dv_aa.aa_inst = &ch->ch_inst;
1828 		dv->dv_aa.aa_ident = ch->ch_ident;
1829 		dv->dv_aa.aa_chan = ch;
1830 		dv->dv_aa.aa_dmat = sc->sc_dmat;
1831 		mtx_enter(&sc->sc_devlck);
1832 		SLIST_INSERT_HEAD(&sc->sc_devs, dv, dv_entry);
1833 		mtx_leave(&sc->sc_devlck);
1834 		config_found((struct device *)sc, &dv->dv_aa, hv_attach_print);
1835 	}
1836 	return (0);
1837 }
1838 
1839 void
1840 hv_evcount_attach(struct hv_channel *ch, const char *name)
1841 {
1842 	struct hv_softc *sc = ch->ch_sc;
1843 
1844 	evcount_attach(&ch->ch_evcnt, name, &sc->sc_idtvec);
1845 }
1846