xref: /freebsd/sys/dev/hyperv/vmbus/vmbus.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix 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  * VM Bus Driver Implementation
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/smp.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/resource.h>
50 #include <x86/include/apicvar.h>
51 
52 #include <contrib/dev/acpica/include/acpi.h>
53 #include <dev/acpica/acpivar.h>
54 
55 #include <dev/hyperv/include/hyperv.h>
56 #include <dev/hyperv/include/vmbus_xact.h>
57 #include <dev/hyperv/vmbus/hyperv_reg.h>
58 #include <dev/hyperv/vmbus/hyperv_var.h>
59 #include <dev/hyperv/vmbus/vmbus_reg.h>
60 #include <dev/hyperv/vmbus/vmbus_var.h>
61 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
62 
63 #include "acpi_if.h"
64 #include "pcib_if.h"
65 #include "vmbus_if.h"
66 
67 #define VMBUS_GPADL_START		0xe1e10
68 
69 struct vmbus_msghc {
70 	struct vmbus_xact		*mh_xact;
71 	struct hypercall_postmsg_in	mh_inprm_save;
72 };
73 
74 static void			vmbus_identify(driver_t *, device_t);
75 static int			vmbus_probe(device_t);
76 static int			vmbus_attach(device_t);
77 static int			vmbus_detach(device_t);
78 static int			vmbus_read_ivar(device_t, device_t, int,
79 				    uintptr_t *);
80 static int			vmbus_child_pnpinfo_str(device_t, device_t,
81 				    char *, size_t);
82 static struct resource		*vmbus_alloc_resource(device_t dev,
83 				    device_t child, int type, int *rid,
84 				    rman_res_t start, rman_res_t end,
85 				    rman_res_t count, u_int flags);
86 static int			vmbus_alloc_msi(device_t bus, device_t dev,
87 				    int count, int maxcount, int *irqs);
88 static int			vmbus_release_msi(device_t bus, device_t dev,
89 				    int count, int *irqs);
90 static int			vmbus_alloc_msix(device_t bus, device_t dev,
91 				    int *irq);
92 static int			vmbus_release_msix(device_t bus, device_t dev,
93 				    int irq);
94 static int			vmbus_map_msi(device_t bus, device_t dev,
95 				    int irq, uint64_t *addr, uint32_t *data);
96 static uint32_t			vmbus_get_version_method(device_t, device_t);
97 static int			vmbus_probe_guid_method(device_t, device_t,
98 				    const struct hyperv_guid *);
99 static uint32_t			vmbus_get_vcpu_id_method(device_t bus,
100 				    device_t dev, int cpu);
101 static struct taskqueue		*vmbus_get_eventtq_method(device_t, device_t,
102 				    int);
103 #ifdef EARLY_AP_STARTUP
104 static void			vmbus_intrhook(void *);
105 #endif
106 
107 static int			vmbus_init(struct vmbus_softc *);
108 static int			vmbus_connect(struct vmbus_softc *, uint32_t);
109 static int			vmbus_req_channels(struct vmbus_softc *sc);
110 static void			vmbus_disconnect(struct vmbus_softc *);
111 static int			vmbus_scan(struct vmbus_softc *);
112 static void			vmbus_scan_teardown(struct vmbus_softc *);
113 static void			vmbus_scan_done(struct vmbus_softc *,
114 				    const struct vmbus_message *);
115 static void			vmbus_chanmsg_handle(struct vmbus_softc *,
116 				    const struct vmbus_message *);
117 static void			vmbus_msg_task(void *, int);
118 static void			vmbus_synic_setup(void *);
119 static void			vmbus_synic_teardown(void *);
120 static int			vmbus_sysctl_version(SYSCTL_HANDLER_ARGS);
121 static int			vmbus_dma_alloc(struct vmbus_softc *);
122 static void			vmbus_dma_free(struct vmbus_softc *);
123 static int			vmbus_intr_setup(struct vmbus_softc *);
124 static void			vmbus_intr_teardown(struct vmbus_softc *);
125 static int			vmbus_doattach(struct vmbus_softc *);
126 static void			vmbus_event_proc_dummy(struct vmbus_softc *,
127 				    int);
128 
129 static struct vmbus_softc	*vmbus_sc;
130 
131 extern inthand_t IDTVEC(vmbus_isr);
132 
133 static const uint32_t		vmbus_version[] = {
134 	VMBUS_VERSION_WIN8_1,
135 	VMBUS_VERSION_WIN8,
136 	VMBUS_VERSION_WIN7,
137 	VMBUS_VERSION_WS2008
138 };
139 
140 static const vmbus_chanmsg_proc_t
141 vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = {
142 	VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done),
143 	VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP)
144 };
145 
146 static device_method_t vmbus_methods[] = {
147 	/* Device interface */
148 	DEVMETHOD(device_identify,		vmbus_identify),
149 	DEVMETHOD(device_probe,			vmbus_probe),
150 	DEVMETHOD(device_attach,		vmbus_attach),
151 	DEVMETHOD(device_detach,		vmbus_detach),
152 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
153 	DEVMETHOD(device_suspend,		bus_generic_suspend),
154 	DEVMETHOD(device_resume,		bus_generic_resume),
155 
156 	/* Bus interface */
157 	DEVMETHOD(bus_add_child,		bus_generic_add_child),
158 	DEVMETHOD(bus_print_child,		bus_generic_print_child),
159 	DEVMETHOD(bus_read_ivar,		vmbus_read_ivar),
160 	DEVMETHOD(bus_child_pnpinfo_str,	vmbus_child_pnpinfo_str),
161 	DEVMETHOD(bus_alloc_resource,		vmbus_alloc_resource),
162 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
163 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
164 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
165 	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
166 	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),
167 #if __FreeBSD_version >= 1100000
168 	DEVMETHOD(bus_get_cpus,			bus_generic_get_cpus),
169 #endif
170 
171 	/* pcib interface */
172 	DEVMETHOD(pcib_alloc_msi,		vmbus_alloc_msi),
173 	DEVMETHOD(pcib_release_msi,		vmbus_release_msi),
174 	DEVMETHOD(pcib_alloc_msix,		vmbus_alloc_msix),
175 	DEVMETHOD(pcib_release_msix,		vmbus_release_msix),
176 	DEVMETHOD(pcib_map_msi,			vmbus_map_msi),
177 
178 	/* Vmbus interface */
179 	DEVMETHOD(vmbus_get_version,		vmbus_get_version_method),
180 	DEVMETHOD(vmbus_probe_guid,		vmbus_probe_guid_method),
181 	DEVMETHOD(vmbus_get_vcpu_id,		vmbus_get_vcpu_id_method),
182 	DEVMETHOD(vmbus_get_event_taskq,	vmbus_get_eventtq_method),
183 
184 	DEVMETHOD_END
185 };
186 
187 static driver_t vmbus_driver = {
188 	"vmbus",
189 	vmbus_methods,
190 	sizeof(struct vmbus_softc)
191 };
192 
193 static devclass_t vmbus_devclass;
194 
195 DRIVER_MODULE(vmbus, pcib, vmbus_driver, vmbus_devclass, NULL, NULL);
196 DRIVER_MODULE(vmbus, acpi_syscontainer, vmbus_driver, vmbus_devclass,
197     NULL, NULL);
198 
199 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
200 MODULE_DEPEND(vmbus, pci, 1, 1, 1);
201 MODULE_VERSION(vmbus, 1);
202 
203 static __inline struct vmbus_softc *
204 vmbus_get_softc(void)
205 {
206 	return vmbus_sc;
207 }
208 
209 void
210 vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize)
211 {
212 	struct hypercall_postmsg_in *inprm;
213 
214 	if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
215 		panic("invalid data size %zu", dsize);
216 
217 	inprm = vmbus_xact_req_data(mh->mh_xact);
218 	memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE);
219 	inprm->hc_connid = VMBUS_CONNID_MESSAGE;
220 	inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL;
221 	inprm->hc_dsize = dsize;
222 }
223 
224 struct vmbus_msghc *
225 vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize)
226 {
227 	struct vmbus_msghc *mh;
228 	struct vmbus_xact *xact;
229 
230 	if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
231 		panic("invalid data size %zu", dsize);
232 
233 	xact = vmbus_xact_get(sc->vmbus_xc,
234 	    dsize + __offsetof(struct hypercall_postmsg_in, hc_data[0]));
235 	if (xact == NULL)
236 		return (NULL);
237 
238 	mh = vmbus_xact_priv(xact, sizeof(*mh));
239 	mh->mh_xact = xact;
240 
241 	vmbus_msghc_reset(mh, dsize);
242 	return (mh);
243 }
244 
245 void
246 vmbus_msghc_put(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
247 {
248 
249 	vmbus_xact_put(mh->mh_xact);
250 }
251 
252 void *
253 vmbus_msghc_dataptr(struct vmbus_msghc *mh)
254 {
255 	struct hypercall_postmsg_in *inprm;
256 
257 	inprm = vmbus_xact_req_data(mh->mh_xact);
258 	return (inprm->hc_data);
259 }
260 
261 int
262 vmbus_msghc_exec_noresult(struct vmbus_msghc *mh)
263 {
264 	sbintime_t time = SBT_1MS;
265 	struct hypercall_postmsg_in *inprm;
266 	bus_addr_t inprm_paddr;
267 	int i;
268 
269 	inprm = vmbus_xact_req_data(mh->mh_xact);
270 	inprm_paddr = vmbus_xact_req_paddr(mh->mh_xact);
271 
272 	/*
273 	 * Save the input parameter so that we could restore the input
274 	 * parameter if the Hypercall failed.
275 	 *
276 	 * XXX
277 	 * Is this really necessary?!  i.e. Will the Hypercall ever
278 	 * overwrite the input parameter?
279 	 */
280 	memcpy(&mh->mh_inprm_save, inprm, HYPERCALL_POSTMSGIN_SIZE);
281 
282 	/*
283 	 * In order to cope with transient failures, e.g. insufficient
284 	 * resources on host side, we retry the post message Hypercall
285 	 * several times.  20 retries seem sufficient.
286 	 */
287 #define HC_RETRY_MAX	20
288 
289 	for (i = 0; i < HC_RETRY_MAX; ++i) {
290 		uint64_t status;
291 
292 		status = hypercall_post_message(inprm_paddr);
293 		if (status == HYPERCALL_STATUS_SUCCESS)
294 			return 0;
295 
296 		pause_sbt("hcpmsg", time, 0, C_HARDCLOCK);
297 		if (time < SBT_1S * 2)
298 			time *= 2;
299 
300 		/* Restore input parameter and try again */
301 		memcpy(inprm, &mh->mh_inprm_save, HYPERCALL_POSTMSGIN_SIZE);
302 	}
303 
304 #undef HC_RETRY_MAX
305 
306 	return EIO;
307 }
308 
309 int
310 vmbus_msghc_exec(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
311 {
312 	int error;
313 
314 	vmbus_xact_activate(mh->mh_xact);
315 	error = vmbus_msghc_exec_noresult(mh);
316 	if (error)
317 		vmbus_xact_deactivate(mh->mh_xact);
318 	return error;
319 }
320 
321 void
322 vmbus_msghc_exec_cancel(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
323 {
324 
325 	vmbus_xact_deactivate(mh->mh_xact);
326 }
327 
328 const struct vmbus_message *
329 vmbus_msghc_wait_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
330 {
331 	size_t resp_len;
332 
333 	return (vmbus_xact_wait(mh->mh_xact, &resp_len));
334 }
335 
336 const struct vmbus_message *
337 vmbus_msghc_poll_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
338 {
339 	size_t resp_len;
340 
341 	return (vmbus_xact_poll(mh->mh_xact, &resp_len));
342 }
343 
344 void
345 vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg)
346 {
347 
348 	vmbus_xact_ctx_wakeup(sc->vmbus_xc, msg, sizeof(*msg));
349 }
350 
351 uint32_t
352 vmbus_gpadl_alloc(struct vmbus_softc *sc)
353 {
354 	uint32_t gpadl;
355 
356 again:
357 	gpadl = atomic_fetchadd_int(&sc->vmbus_gpadl, 1);
358 	if (gpadl == 0)
359 		goto again;
360 	return (gpadl);
361 }
362 
363 static int
364 vmbus_connect(struct vmbus_softc *sc, uint32_t version)
365 {
366 	struct vmbus_chanmsg_connect *req;
367 	const struct vmbus_message *msg;
368 	struct vmbus_msghc *mh;
369 	int error, done = 0;
370 
371 	mh = vmbus_msghc_get(sc, sizeof(*req));
372 	if (mh == NULL)
373 		return ENXIO;
374 
375 	req = vmbus_msghc_dataptr(mh);
376 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT;
377 	req->chm_ver = version;
378 	req->chm_evtflags = sc->vmbus_evtflags_dma.hv_paddr;
379 	req->chm_mnf1 = sc->vmbus_mnf1_dma.hv_paddr;
380 	req->chm_mnf2 = sc->vmbus_mnf2_dma.hv_paddr;
381 
382 	error = vmbus_msghc_exec(sc, mh);
383 	if (error) {
384 		vmbus_msghc_put(sc, mh);
385 		return error;
386 	}
387 
388 	msg = vmbus_msghc_wait_result(sc, mh);
389 	done = ((const struct vmbus_chanmsg_connect_resp *)
390 	    msg->msg_data)->chm_done;
391 
392 	vmbus_msghc_put(sc, mh);
393 
394 	return (done ? 0 : EOPNOTSUPP);
395 }
396 
397 static int
398 vmbus_init(struct vmbus_softc *sc)
399 {
400 	int i;
401 
402 	for (i = 0; i < nitems(vmbus_version); ++i) {
403 		int error;
404 
405 		error = vmbus_connect(sc, vmbus_version[i]);
406 		if (!error) {
407 			sc->vmbus_version = vmbus_version[i];
408 			device_printf(sc->vmbus_dev, "version %u.%u\n",
409 			    VMBUS_VERSION_MAJOR(sc->vmbus_version),
410 			    VMBUS_VERSION_MINOR(sc->vmbus_version));
411 			return 0;
412 		}
413 	}
414 	return ENXIO;
415 }
416 
417 static void
418 vmbus_disconnect(struct vmbus_softc *sc)
419 {
420 	struct vmbus_chanmsg_disconnect *req;
421 	struct vmbus_msghc *mh;
422 	int error;
423 
424 	mh = vmbus_msghc_get(sc, sizeof(*req));
425 	if (mh == NULL) {
426 		device_printf(sc->vmbus_dev,
427 		    "can not get msg hypercall for disconnect\n");
428 		return;
429 	}
430 
431 	req = vmbus_msghc_dataptr(mh);
432 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT;
433 
434 	error = vmbus_msghc_exec_noresult(mh);
435 	vmbus_msghc_put(sc, mh);
436 
437 	if (error) {
438 		device_printf(sc->vmbus_dev,
439 		    "disconnect msg hypercall failed\n");
440 	}
441 }
442 
443 static int
444 vmbus_req_channels(struct vmbus_softc *sc)
445 {
446 	struct vmbus_chanmsg_chrequest *req;
447 	struct vmbus_msghc *mh;
448 	int error;
449 
450 	mh = vmbus_msghc_get(sc, sizeof(*req));
451 	if (mh == NULL)
452 		return ENXIO;
453 
454 	req = vmbus_msghc_dataptr(mh);
455 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST;
456 
457 	error = vmbus_msghc_exec_noresult(mh);
458 	vmbus_msghc_put(sc, mh);
459 
460 	return error;
461 }
462 
463 static void
464 vmbus_scan_done_task(void *xsc, int pending __unused)
465 {
466 	struct vmbus_softc *sc = xsc;
467 
468 	mtx_lock(&Giant);
469 	sc->vmbus_scandone = true;
470 	mtx_unlock(&Giant);
471 	wakeup(&sc->vmbus_scandone);
472 }
473 
474 static void
475 vmbus_scan_done(struct vmbus_softc *sc,
476     const struct vmbus_message *msg __unused)
477 {
478 
479 	taskqueue_enqueue(sc->vmbus_devtq, &sc->vmbus_scandone_task);
480 }
481 
482 static int
483 vmbus_scan(struct vmbus_softc *sc)
484 {
485 	int error;
486 
487 	/*
488 	 * Identify, probe and attach for non-channel devices.
489 	 */
490 	bus_generic_probe(sc->vmbus_dev);
491 	bus_generic_attach(sc->vmbus_dev);
492 
493 	/*
494 	 * This taskqueue serializes vmbus devices' attach and detach
495 	 * for channel offer and rescind messages.
496 	 */
497 	sc->vmbus_devtq = taskqueue_create("vmbus dev", M_WAITOK,
498 	    taskqueue_thread_enqueue, &sc->vmbus_devtq);
499 	taskqueue_start_threads(&sc->vmbus_devtq, 1, PI_NET, "vmbusdev");
500 	TASK_INIT(&sc->vmbus_scandone_task, 0, vmbus_scan_done_task, sc);
501 
502 	/*
503 	 * This taskqueue handles sub-channel detach, so that vmbus
504 	 * device's detach running in vmbus_devtq can drain its sub-
505 	 * channels.
506 	 */
507 	sc->vmbus_subchtq = taskqueue_create("vmbus subch", M_WAITOK,
508 	    taskqueue_thread_enqueue, &sc->vmbus_subchtq);
509 	taskqueue_start_threads(&sc->vmbus_subchtq, 1, PI_NET, "vmbussch");
510 
511 	/*
512 	 * Start vmbus scanning.
513 	 */
514 	error = vmbus_req_channels(sc);
515 	if (error) {
516 		device_printf(sc->vmbus_dev, "channel request failed: %d\n",
517 		    error);
518 		return (error);
519 	}
520 
521 	/*
522 	 * Wait for all vmbus devices from the initial channel offers to be
523 	 * attached.
524 	 */
525 	GIANT_REQUIRED;
526 	while (!sc->vmbus_scandone)
527 		mtx_sleep(&sc->vmbus_scandone, &Giant, 0, "vmbusdev", 0);
528 
529 	if (bootverbose) {
530 		device_printf(sc->vmbus_dev, "device scan, probe and attach "
531 		    "done\n");
532 	}
533 	return (0);
534 }
535 
536 static void
537 vmbus_scan_teardown(struct vmbus_softc *sc)
538 {
539 
540 	GIANT_REQUIRED;
541 	if (sc->vmbus_devtq != NULL) {
542 		mtx_unlock(&Giant);
543 		taskqueue_free(sc->vmbus_devtq);
544 		mtx_lock(&Giant);
545 		sc->vmbus_devtq = NULL;
546 	}
547 	if (sc->vmbus_subchtq != NULL) {
548 		mtx_unlock(&Giant);
549 		taskqueue_free(sc->vmbus_subchtq);
550 		mtx_lock(&Giant);
551 		sc->vmbus_subchtq = NULL;
552 	}
553 }
554 
555 static void
556 vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg)
557 {
558 	vmbus_chanmsg_proc_t msg_proc;
559 	uint32_t msg_type;
560 
561 	msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
562 	if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) {
563 		device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
564 		    msg_type);
565 		return;
566 	}
567 
568 	msg_proc = vmbus_chanmsg_handlers[msg_type];
569 	if (msg_proc != NULL)
570 		msg_proc(sc, msg);
571 
572 	/* Channel specific processing */
573 	vmbus_chan_msgproc(sc, msg);
574 }
575 
576 static void
577 vmbus_msg_task(void *xsc, int pending __unused)
578 {
579 	struct vmbus_softc *sc = xsc;
580 	volatile struct vmbus_message *msg;
581 
582 	msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE;
583 	for (;;) {
584 		if (msg->msg_type == HYPERV_MSGTYPE_NONE) {
585 			/* No message */
586 			break;
587 		} else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) {
588 			/* Channel message */
589 			vmbus_chanmsg_handle(sc,
590 			    __DEVOLATILE(const struct vmbus_message *, msg));
591 		}
592 
593 		msg->msg_type = HYPERV_MSGTYPE_NONE;
594 		/*
595 		 * Make sure the write to msg_type (i.e. set to
596 		 * HYPERV_MSGTYPE_NONE) happens before we read the
597 		 * msg_flags and EOMing. Otherwise, the EOMing will
598 		 * not deliver any more messages since there is no
599 		 * empty slot
600 		 *
601 		 * NOTE:
602 		 * mb() is used here, since atomic_thread_fence_seq_cst()
603 		 * will become compiler fence on UP kernel.
604 		 */
605 		mb();
606 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
607 			/*
608 			 * This will cause message queue rescan to possibly
609 			 * deliver another msg from the hypervisor
610 			 */
611 			wrmsr(MSR_HV_EOM, 0);
612 		}
613 	}
614 }
615 
616 static __inline int
617 vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu)
618 {
619 	volatile struct vmbus_message *msg;
620 	struct vmbus_message *msg_base;
621 
622 	msg_base = VMBUS_PCPU_GET(sc, message, cpu);
623 
624 	/*
625 	 * Check event timer.
626 	 *
627 	 * TODO: move this to independent IDT vector.
628 	 */
629 	msg = msg_base + VMBUS_SINT_TIMER;
630 	if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
631 		msg->msg_type = HYPERV_MSGTYPE_NONE;
632 
633 		vmbus_et_intr(frame);
634 
635 		/*
636 		 * Make sure the write to msg_type (i.e. set to
637 		 * HYPERV_MSGTYPE_NONE) happens before we read the
638 		 * msg_flags and EOMing. Otherwise, the EOMing will
639 		 * not deliver any more messages since there is no
640 		 * empty slot
641 		 *
642 		 * NOTE:
643 		 * mb() is used here, since atomic_thread_fence_seq_cst()
644 		 * will become compiler fence on UP kernel.
645 		 */
646 		mb();
647 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
648 			/*
649 			 * This will cause message queue rescan to possibly
650 			 * deliver another msg from the hypervisor
651 			 */
652 			wrmsr(MSR_HV_EOM, 0);
653 		}
654 	}
655 
656 	/*
657 	 * Check events.  Hot path for network and storage I/O data; high rate.
658 	 *
659 	 * NOTE:
660 	 * As recommended by the Windows guest fellows, we check events before
661 	 * checking messages.
662 	 */
663 	sc->vmbus_event_proc(sc, cpu);
664 
665 	/*
666 	 * Check messages.  Mainly management stuffs; ultra low rate.
667 	 */
668 	msg = msg_base + VMBUS_SINT_MESSAGE;
669 	if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
670 		taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
671 		    VMBUS_PCPU_PTR(sc, message_task, cpu));
672 	}
673 
674 	return (FILTER_HANDLED);
675 }
676 
677 void
678 vmbus_handle_intr(struct trapframe *trap_frame)
679 {
680 	struct vmbus_softc *sc = vmbus_get_softc();
681 	int cpu = curcpu;
682 
683 	/*
684 	 * Disable preemption.
685 	 */
686 	critical_enter();
687 
688 	/*
689 	 * Do a little interrupt counting.
690 	 */
691 	(*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++;
692 
693 	vmbus_handle_intr1(sc, trap_frame, cpu);
694 
695 	/*
696 	 * Enable preemption.
697 	 */
698 	critical_exit();
699 }
700 
701 static void
702 vmbus_synic_setup(void *xsc)
703 {
704 	struct vmbus_softc *sc = xsc;
705 	int cpu = curcpu;
706 	uint64_t val, orig;
707 	uint32_t sint;
708 
709 	if (hyperv_features & CPUID_HV_MSR_VP_INDEX) {
710 		/* Save virtual processor id. */
711 		VMBUS_PCPU_GET(sc, vcpuid, cpu) = rdmsr(MSR_HV_VP_INDEX);
712 	} else {
713 		/* Set virtual processor id to 0 for compatibility. */
714 		VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0;
715 	}
716 
717 	/*
718 	 * Setup the SynIC message.
719 	 */
720 	orig = rdmsr(MSR_HV_SIMP);
721 	val = MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK) |
722 	    ((VMBUS_PCPU_GET(sc, message_dma.hv_paddr, cpu) >> PAGE_SHIFT) <<
723 	     MSR_HV_SIMP_PGSHIFT);
724 	wrmsr(MSR_HV_SIMP, val);
725 
726 	/*
727 	 * Setup the SynIC event flags.
728 	 */
729 	orig = rdmsr(MSR_HV_SIEFP);
730 	val = MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK) |
731 	    ((VMBUS_PCPU_GET(sc, event_flags_dma.hv_paddr, cpu)
732 	      >> PAGE_SHIFT) << MSR_HV_SIEFP_PGSHIFT);
733 	wrmsr(MSR_HV_SIEFP, val);
734 
735 
736 	/*
737 	 * Configure and unmask SINT for message and event flags.
738 	 */
739 	sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
740 	orig = rdmsr(sint);
741 	val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
742 	    (orig & MSR_HV_SINT_RSVD_MASK);
743 	wrmsr(sint, val);
744 
745 	/*
746 	 * Configure and unmask SINT for timer.
747 	 */
748 	sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
749 	orig = rdmsr(sint);
750 	val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
751 	    (orig & MSR_HV_SINT_RSVD_MASK);
752 	wrmsr(sint, val);
753 
754 	/*
755 	 * All done; enable SynIC.
756 	 */
757 	orig = rdmsr(MSR_HV_SCONTROL);
758 	val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK);
759 	wrmsr(MSR_HV_SCONTROL, val);
760 }
761 
762 static void
763 vmbus_synic_teardown(void *arg)
764 {
765 	uint64_t orig;
766 	uint32_t sint;
767 
768 	/*
769 	 * Disable SynIC.
770 	 */
771 	orig = rdmsr(MSR_HV_SCONTROL);
772 	wrmsr(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK));
773 
774 	/*
775 	 * Mask message and event flags SINT.
776 	 */
777 	sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
778 	orig = rdmsr(sint);
779 	wrmsr(sint, orig | MSR_HV_SINT_MASKED);
780 
781 	/*
782 	 * Mask timer SINT.
783 	 */
784 	sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
785 	orig = rdmsr(sint);
786 	wrmsr(sint, orig | MSR_HV_SINT_MASKED);
787 
788 	/*
789 	 * Teardown SynIC message.
790 	 */
791 	orig = rdmsr(MSR_HV_SIMP);
792 	wrmsr(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK));
793 
794 	/*
795 	 * Teardown SynIC event flags.
796 	 */
797 	orig = rdmsr(MSR_HV_SIEFP);
798 	wrmsr(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK));
799 }
800 
801 static int
802 vmbus_dma_alloc(struct vmbus_softc *sc)
803 {
804 	bus_dma_tag_t parent_dtag;
805 	uint8_t *evtflags;
806 	int cpu;
807 
808 	parent_dtag = bus_get_dma_tag(sc->vmbus_dev);
809 	CPU_FOREACH(cpu) {
810 		void *ptr;
811 
812 		/*
813 		 * Per-cpu messages and event flags.
814 		 */
815 		ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
816 		    PAGE_SIZE, VMBUS_PCPU_PTR(sc, message_dma, cpu),
817 		    BUS_DMA_WAITOK | BUS_DMA_ZERO);
818 		if (ptr == NULL)
819 			return ENOMEM;
820 		VMBUS_PCPU_GET(sc, message, cpu) = ptr;
821 
822 		ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
823 		    PAGE_SIZE, VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
824 		    BUS_DMA_WAITOK | BUS_DMA_ZERO);
825 		if (ptr == NULL)
826 			return ENOMEM;
827 		VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr;
828 	}
829 
830 	evtflags = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
831 	    PAGE_SIZE, &sc->vmbus_evtflags_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
832 	if (evtflags == NULL)
833 		return ENOMEM;
834 	sc->vmbus_rx_evtflags = (u_long *)evtflags;
835 	sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2));
836 	sc->vmbus_evtflags = evtflags;
837 
838 	sc->vmbus_mnf1 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
839 	    PAGE_SIZE, &sc->vmbus_mnf1_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
840 	if (sc->vmbus_mnf1 == NULL)
841 		return ENOMEM;
842 
843 	sc->vmbus_mnf2 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
844 	    sizeof(struct vmbus_mnf), &sc->vmbus_mnf2_dma,
845 	    BUS_DMA_WAITOK | BUS_DMA_ZERO);
846 	if (sc->vmbus_mnf2 == NULL)
847 		return ENOMEM;
848 
849 	return 0;
850 }
851 
852 static void
853 vmbus_dma_free(struct vmbus_softc *sc)
854 {
855 	int cpu;
856 
857 	if (sc->vmbus_evtflags != NULL) {
858 		hyperv_dmamem_free(&sc->vmbus_evtflags_dma, sc->vmbus_evtflags);
859 		sc->vmbus_evtflags = NULL;
860 		sc->vmbus_rx_evtflags = NULL;
861 		sc->vmbus_tx_evtflags = NULL;
862 	}
863 	if (sc->vmbus_mnf1 != NULL) {
864 		hyperv_dmamem_free(&sc->vmbus_mnf1_dma, sc->vmbus_mnf1);
865 		sc->vmbus_mnf1 = NULL;
866 	}
867 	if (sc->vmbus_mnf2 != NULL) {
868 		hyperv_dmamem_free(&sc->vmbus_mnf2_dma, sc->vmbus_mnf2);
869 		sc->vmbus_mnf2 = NULL;
870 	}
871 
872 	CPU_FOREACH(cpu) {
873 		if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) {
874 			hyperv_dmamem_free(
875 			    VMBUS_PCPU_PTR(sc, message_dma, cpu),
876 			    VMBUS_PCPU_GET(sc, message, cpu));
877 			VMBUS_PCPU_GET(sc, message, cpu) = NULL;
878 		}
879 		if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) {
880 			hyperv_dmamem_free(
881 			    VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
882 			    VMBUS_PCPU_GET(sc, event_flags, cpu));
883 			VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL;
884 		}
885 	}
886 }
887 
888 static int
889 vmbus_intr_setup(struct vmbus_softc *sc)
890 {
891 	int cpu;
892 
893 	CPU_FOREACH(cpu) {
894 		char buf[MAXCOMLEN + 1];
895 		cpuset_t cpu_mask;
896 
897 		/* Allocate an interrupt counter for Hyper-V interrupt */
898 		snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu);
899 		intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu));
900 
901 		/*
902 		 * Setup taskqueue to handle events.  Task will be per-
903 		 * channel.
904 		 */
905 		VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast(
906 		    "hyperv event", M_WAITOK, taskqueue_thread_enqueue,
907 		    VMBUS_PCPU_PTR(sc, event_tq, cpu));
908 		CPU_SETOF(cpu, &cpu_mask);
909 		taskqueue_start_threads_cpuset(
910 		    VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET, &cpu_mask,
911 		    "hvevent%d", cpu);
912 
913 		/*
914 		 * Setup tasks and taskqueues to handle messages.
915 		 */
916 		VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast(
917 		    "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
918 		    VMBUS_PCPU_PTR(sc, message_tq, cpu));
919 		CPU_SETOF(cpu, &cpu_mask);
920 		taskqueue_start_threads_cpuset(
921 		    VMBUS_PCPU_PTR(sc, message_tq, cpu), 1, PI_NET, &cpu_mask,
922 		    "hvmsg%d", cpu);
923 		TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0,
924 		    vmbus_msg_task, sc);
925 	}
926 
927 	/*
928 	 * All Hyper-V ISR required resources are setup, now let's find a
929 	 * free IDT vector for Hyper-V ISR and set it up.
930 	 */
931 	sc->vmbus_idtvec = lapic_ipi_alloc(IDTVEC(vmbus_isr));
932 	if (sc->vmbus_idtvec < 0) {
933 		device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
934 		return ENXIO;
935 	}
936 	if (bootverbose) {
937 		device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
938 		    sc->vmbus_idtvec);
939 	}
940 	return 0;
941 }
942 
943 static void
944 vmbus_intr_teardown(struct vmbus_softc *sc)
945 {
946 	int cpu;
947 
948 	if (sc->vmbus_idtvec >= 0) {
949 		lapic_ipi_free(sc->vmbus_idtvec);
950 		sc->vmbus_idtvec = -1;
951 	}
952 
953 	CPU_FOREACH(cpu) {
954 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
955 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
956 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
957 		}
958 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
959 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
960 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
961 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
962 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
963 		}
964 	}
965 }
966 
967 static int
968 vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
969 {
970 	return (ENOENT);
971 }
972 
973 static int
974 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
975 {
976 	const struct vmbus_channel *chan;
977 	char guidbuf[HYPERV_GUID_STRLEN];
978 
979 	chan = vmbus_get_channel(child);
980 	if (chan == NULL) {
981 		/* Event timer device, which does not belong to a channel */
982 		return (0);
983 	}
984 
985 	strlcat(buf, "classid=", buflen);
986 	hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf));
987 	strlcat(buf, guidbuf, buflen);
988 
989 	strlcat(buf, " deviceid=", buflen);
990 	hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf));
991 	strlcat(buf, guidbuf, buflen);
992 
993 	return (0);
994 }
995 
996 int
997 vmbus_add_child(struct vmbus_channel *chan)
998 {
999 	struct vmbus_softc *sc = chan->ch_vmbus;
1000 	device_t parent = sc->vmbus_dev;
1001 
1002 	mtx_lock(&Giant);
1003 
1004 	chan->ch_dev = device_add_child(parent, NULL, -1);
1005 	if (chan->ch_dev == NULL) {
1006 		mtx_unlock(&Giant);
1007 		device_printf(parent, "device_add_child for chan%u failed\n",
1008 		    chan->ch_id);
1009 		return (ENXIO);
1010 	}
1011 	device_set_ivars(chan->ch_dev, chan);
1012 	device_probe_and_attach(chan->ch_dev);
1013 
1014 	mtx_unlock(&Giant);
1015 	return (0);
1016 }
1017 
1018 int
1019 vmbus_delete_child(struct vmbus_channel *chan)
1020 {
1021 	int error = 0;
1022 
1023 	mtx_lock(&Giant);
1024 	if (chan->ch_dev != NULL) {
1025 		error = device_delete_child(chan->ch_vmbus->vmbus_dev,
1026 		    chan->ch_dev);
1027 		chan->ch_dev = NULL;
1028 	}
1029 	mtx_unlock(&Giant);
1030 	return (error);
1031 }
1032 
1033 static int
1034 vmbus_sysctl_version(SYSCTL_HANDLER_ARGS)
1035 {
1036 	struct vmbus_softc *sc = arg1;
1037 	char verstr[16];
1038 
1039 	snprintf(verstr, sizeof(verstr), "%u.%u",
1040 	    VMBUS_VERSION_MAJOR(sc->vmbus_version),
1041 	    VMBUS_VERSION_MINOR(sc->vmbus_version));
1042 	return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
1043 }
1044 
1045 /*
1046  * We need the function to make sure the MMIO resource is allocated from the
1047  * ranges found in _CRS.
1048  *
1049  * For the release function, we can use bus_generic_release_resource().
1050  */
1051 static struct resource *
1052 vmbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
1053     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1054 {
1055 	device_t parent = device_get_parent(dev);
1056 	struct resource *res;
1057 
1058 #ifdef NEW_PCIB
1059 	if (type == SYS_RES_MEMORY) {
1060 		struct vmbus_softc *sc = device_get_softc(dev);
1061 
1062 		res = pcib_host_res_alloc(&sc->vmbus_mmio_res, child, type,
1063 		    rid, start, end, count, flags);
1064 	} else
1065 #endif
1066 	{
1067 		res = BUS_ALLOC_RESOURCE(parent, child, type, rid, start,
1068 		    end, count, flags);
1069 	}
1070 
1071 	return (res);
1072 }
1073 
1074 static int
1075 vmbus_alloc_msi(device_t bus, device_t dev, int count, int maxcount, int *irqs)
1076 {
1077 
1078 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1079 	    irqs));
1080 }
1081 
1082 static int
1083 vmbus_release_msi(device_t bus, device_t dev, int count, int *irqs)
1084 {
1085 
1086 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1087 }
1088 
1089 static int
1090 vmbus_alloc_msix(device_t bus, device_t dev, int *irq)
1091 {
1092 
1093 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1094 }
1095 
1096 static int
1097 vmbus_release_msix(device_t bus, device_t dev, int irq)
1098 {
1099 
1100 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1101 }
1102 
1103 static int
1104 vmbus_map_msi(device_t bus, device_t dev, int irq, uint64_t *addr,
1105 	uint32_t *data)
1106 {
1107 
1108 	return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
1109 }
1110 
1111 static uint32_t
1112 vmbus_get_version_method(device_t bus, device_t dev)
1113 {
1114 	struct vmbus_softc *sc = device_get_softc(bus);
1115 
1116 	return sc->vmbus_version;
1117 }
1118 
1119 static int
1120 vmbus_probe_guid_method(device_t bus, device_t dev,
1121     const struct hyperv_guid *guid)
1122 {
1123 	const struct vmbus_channel *chan = vmbus_get_channel(dev);
1124 
1125 	if (memcmp(&chan->ch_guid_type, guid, sizeof(struct hyperv_guid)) == 0)
1126 		return 0;
1127 	return ENXIO;
1128 }
1129 
1130 static uint32_t
1131 vmbus_get_vcpu_id_method(device_t bus, device_t dev, int cpu)
1132 {
1133 	const struct vmbus_softc *sc = device_get_softc(bus);
1134 
1135 	return (VMBUS_PCPU_GET(sc, vcpuid, cpu));
1136 }
1137 
1138 static struct taskqueue *
1139 vmbus_get_eventtq_method(device_t bus, device_t dev __unused, int cpu)
1140 {
1141 	const struct vmbus_softc *sc = device_get_softc(bus);
1142 
1143 	KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu%d", cpu));
1144 	return (VMBUS_PCPU_GET(sc, event_tq, cpu));
1145 }
1146 
1147 #ifdef NEW_PCIB
1148 #define VTPM_BASE_ADDR 0xfed40000
1149 #define FOUR_GB (1ULL << 32)
1150 
1151 enum parse_pass { parse_64, parse_32 };
1152 
1153 struct parse_context {
1154 	device_t vmbus_dev;
1155 	enum parse_pass pass;
1156 };
1157 
1158 static ACPI_STATUS
1159 parse_crs(ACPI_RESOURCE *res, void *ctx)
1160 {
1161 	const struct parse_context *pc = ctx;
1162 	device_t vmbus_dev = pc->vmbus_dev;
1163 
1164 	struct vmbus_softc *sc = device_get_softc(vmbus_dev);
1165 	UINT64 start, end;
1166 
1167 	switch (res->Type) {
1168 	case ACPI_RESOURCE_TYPE_ADDRESS32:
1169 		start = res->Data.Address32.Address.Minimum;
1170 		end = res->Data.Address32.Address.Maximum;
1171 		break;
1172 
1173 	case ACPI_RESOURCE_TYPE_ADDRESS64:
1174 		start = res->Data.Address64.Address.Minimum;
1175 		end = res->Data.Address64.Address.Maximum;
1176 		break;
1177 
1178 	default:
1179 		/* Unused types. */
1180 		return (AE_OK);
1181 	}
1182 
1183 	/*
1184 	 * We don't use <1MB addresses.
1185 	 */
1186 	if (end < 0x100000)
1187 		return (AE_OK);
1188 
1189 	/* Don't conflict with vTPM. */
1190 	if (end >= VTPM_BASE_ADDR && start < VTPM_BASE_ADDR)
1191 		end = VTPM_BASE_ADDR - 1;
1192 
1193 	if ((pc->pass == parse_32 && start < FOUR_GB) ||
1194 	    (pc->pass == parse_64 && start >= FOUR_GB))
1195 		pcib_host_res_decodes(&sc->vmbus_mmio_res, SYS_RES_MEMORY,
1196 		    start, end, 0);
1197 
1198 	return (AE_OK);
1199 }
1200 
1201 static void
1202 vmbus_get_crs(device_t dev, device_t vmbus_dev, enum parse_pass pass)
1203 {
1204 	struct parse_context pc;
1205 	ACPI_STATUS status;
1206 
1207 	if (bootverbose)
1208 		device_printf(dev, "walking _CRS, pass=%d\n", pass);
1209 
1210 	pc.vmbus_dev = vmbus_dev;
1211 	pc.pass = pass;
1212 	status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
1213 			parse_crs, &pc);
1214 
1215 	if (bootverbose && ACPI_FAILURE(status))
1216 		device_printf(dev, "_CRS: not found, pass=%d\n", pass);
1217 }
1218 
1219 static void
1220 vmbus_get_mmio_res_pass(device_t dev, enum parse_pass pass)
1221 {
1222 	device_t acpi0, parent;
1223 
1224 	parent = device_get_parent(dev);
1225 
1226 	acpi0 = device_get_parent(parent);
1227 	if (strcmp("acpi0", device_get_nameunit(acpi0)) == 0) {
1228 		device_t *children;
1229 		int count;
1230 
1231 		/*
1232 		 * Try to locate VMBUS resources and find _CRS on them.
1233 		 */
1234 		if (device_get_children(acpi0, &children, &count) == 0) {
1235 			int i;
1236 
1237 			for (i = 0; i < count; ++i) {
1238 				if (!device_is_attached(children[i]))
1239 					continue;
1240 
1241 				if (strcmp("vmbus_res",
1242 				    device_get_name(children[i])) == 0)
1243 					vmbus_get_crs(children[i], dev, pass);
1244 			}
1245 			free(children, M_TEMP);
1246 		}
1247 
1248 		/*
1249 		 * Try to find _CRS on acpi.
1250 		 */
1251 		vmbus_get_crs(acpi0, dev, pass);
1252 	} else {
1253 		device_printf(dev, "not grandchild of acpi\n");
1254 	}
1255 
1256 	/*
1257 	 * Try to find _CRS on parent.
1258 	 */
1259 	vmbus_get_crs(parent, dev, pass);
1260 }
1261 
1262 static void
1263 vmbus_get_mmio_res(device_t dev)
1264 {
1265 	struct vmbus_softc *sc = device_get_softc(dev);
1266 	/*
1267 	 * We walk the resources twice to make sure that: in the resource
1268 	 * list, the 32-bit resources appear behind the 64-bit resources.
1269 	 * NB: resource_list_add() uses INSERT_TAIL. This way, when we
1270 	 * iterate through the list to find a range for a 64-bit BAR in
1271 	 * vmbus_alloc_resource(), we can make sure we try to use >4GB
1272 	 * ranges first.
1273 	 */
1274 	pcib_host_res_init(dev, &sc->vmbus_mmio_res);
1275 
1276 	vmbus_get_mmio_res_pass(dev, parse_64);
1277 	vmbus_get_mmio_res_pass(dev, parse_32);
1278 }
1279 
1280 static void
1281 vmbus_free_mmio_res(device_t dev)
1282 {
1283 	struct vmbus_softc *sc = device_get_softc(dev);
1284 
1285 	pcib_host_res_free(dev, &sc->vmbus_mmio_res);
1286 }
1287 #endif	/* NEW_PCIB */
1288 
1289 static void
1290 vmbus_identify(driver_t *driver, device_t parent)
1291 {
1292 
1293 	if (device_get_unit(parent) != 0 || vm_guest != VM_GUEST_HV ||
1294 	    (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1295 		return;
1296 	device_add_child(parent, "vmbus", -1);
1297 }
1298 
1299 static int
1300 vmbus_probe(device_t dev)
1301 {
1302 
1303 	if (device_get_unit(dev) != 0 || vm_guest != VM_GUEST_HV ||
1304 	    (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1305 		return (ENXIO);
1306 
1307 	device_set_desc(dev, "Hyper-V Vmbus");
1308 	return (BUS_PROBE_DEFAULT);
1309 }
1310 
1311 /**
1312  * @brief Main vmbus driver initialization routine.
1313  *
1314  * Here, we
1315  * - initialize the vmbus driver context
1316  * - setup various driver entry points
1317  * - invoke the vmbus hv main init routine
1318  * - get the irq resource
1319  * - invoke the vmbus to add the vmbus root device
1320  * - setup the vmbus root device
1321  * - retrieve the channel offers
1322  */
1323 static int
1324 vmbus_doattach(struct vmbus_softc *sc)
1325 {
1326 	struct sysctl_oid_list *child;
1327 	struct sysctl_ctx_list *ctx;
1328 	int ret;
1329 
1330 	if (sc->vmbus_flags & VMBUS_FLAG_ATTACHED)
1331 		return (0);
1332 
1333 #ifdef NEW_PCIB
1334 	vmbus_get_mmio_res(sc->vmbus_dev);
1335 #endif
1336 
1337 	sc->vmbus_flags |= VMBUS_FLAG_ATTACHED;
1338 
1339 	sc->vmbus_gpadl = VMBUS_GPADL_START;
1340 	mtx_init(&sc->vmbus_prichan_lock, "vmbus prichan", NULL, MTX_DEF);
1341 	TAILQ_INIT(&sc->vmbus_prichans);
1342 	mtx_init(&sc->vmbus_chan_lock, "vmbus channel", NULL, MTX_DEF);
1343 	TAILQ_INIT(&sc->vmbus_chans);
1344 	sc->vmbus_chmap = malloc(
1345 	    sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX, M_DEVBUF,
1346 	    M_WAITOK | M_ZERO);
1347 
1348 	/*
1349 	 * Create context for "post message" Hypercalls
1350 	 */
1351 	sc->vmbus_xc = vmbus_xact_ctx_create(bus_get_dma_tag(sc->vmbus_dev),
1352 	    HYPERCALL_POSTMSGIN_SIZE, VMBUS_MSG_SIZE,
1353 	    sizeof(struct vmbus_msghc));
1354 	if (sc->vmbus_xc == NULL) {
1355 		ret = ENXIO;
1356 		goto cleanup;
1357 	}
1358 
1359 	/*
1360 	 * Allocate DMA stuffs.
1361 	 */
1362 	ret = vmbus_dma_alloc(sc);
1363 	if (ret != 0)
1364 		goto cleanup;
1365 
1366 	/*
1367 	 * Setup interrupt.
1368 	 */
1369 	ret = vmbus_intr_setup(sc);
1370 	if (ret != 0)
1371 		goto cleanup;
1372 
1373 	/*
1374 	 * Setup SynIC.
1375 	 */
1376 	if (bootverbose)
1377 		device_printf(sc->vmbus_dev, "smp_started = %d\n", smp_started);
1378 	smp_rendezvous(NULL, vmbus_synic_setup, NULL, sc);
1379 	sc->vmbus_flags |= VMBUS_FLAG_SYNIC;
1380 
1381 	/*
1382 	 * Initialize vmbus, e.g. connect to Hypervisor.
1383 	 */
1384 	ret = vmbus_init(sc);
1385 	if (ret != 0)
1386 		goto cleanup;
1387 
1388 	if (sc->vmbus_version == VMBUS_VERSION_WS2008 ||
1389 	    sc->vmbus_version == VMBUS_VERSION_WIN7)
1390 		sc->vmbus_event_proc = vmbus_event_proc_compat;
1391 	else
1392 		sc->vmbus_event_proc = vmbus_event_proc;
1393 
1394 	ret = vmbus_scan(sc);
1395 	if (ret != 0)
1396 		goto cleanup;
1397 
1398 	ctx = device_get_sysctl_ctx(sc->vmbus_dev);
1399 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vmbus_dev));
1400 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "version",
1401 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1402 	    vmbus_sysctl_version, "A", "vmbus version");
1403 
1404 	return (ret);
1405 
1406 cleanup:
1407 	vmbus_scan_teardown(sc);
1408 	vmbus_intr_teardown(sc);
1409 	vmbus_dma_free(sc);
1410 	if (sc->vmbus_xc != NULL) {
1411 		vmbus_xact_ctx_destroy(sc->vmbus_xc);
1412 		sc->vmbus_xc = NULL;
1413 	}
1414 	free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF);
1415 	mtx_destroy(&sc->vmbus_prichan_lock);
1416 	mtx_destroy(&sc->vmbus_chan_lock);
1417 
1418 	return (ret);
1419 }
1420 
1421 static void
1422 vmbus_event_proc_dummy(struct vmbus_softc *sc __unused, int cpu __unused)
1423 {
1424 }
1425 
1426 #ifdef EARLY_AP_STARTUP
1427 
1428 static void
1429 vmbus_intrhook(void *xsc)
1430 {
1431 	struct vmbus_softc *sc = xsc;
1432 
1433 	if (bootverbose)
1434 		device_printf(sc->vmbus_dev, "intrhook\n");
1435 	vmbus_doattach(sc);
1436 	config_intrhook_disestablish(&sc->vmbus_intrhook);
1437 }
1438 
1439 #endif	/* EARLY_AP_STARTUP */
1440 
1441 static int
1442 vmbus_attach(device_t dev)
1443 {
1444 	vmbus_sc = device_get_softc(dev);
1445 	vmbus_sc->vmbus_dev = dev;
1446 	vmbus_sc->vmbus_idtvec = -1;
1447 
1448 	/*
1449 	 * Event processing logic will be configured:
1450 	 * - After the vmbus protocol version negotiation.
1451 	 * - Before we request channel offers.
1452 	 */
1453 	vmbus_sc->vmbus_event_proc = vmbus_event_proc_dummy;
1454 
1455 #ifdef EARLY_AP_STARTUP
1456 	/*
1457 	 * Defer the real attach until the pause(9) works as expected.
1458 	 */
1459 	vmbus_sc->vmbus_intrhook.ich_func = vmbus_intrhook;
1460 	vmbus_sc->vmbus_intrhook.ich_arg = vmbus_sc;
1461 	config_intrhook_establish(&vmbus_sc->vmbus_intrhook);
1462 #else	/* !EARLY_AP_STARTUP */
1463 	/*
1464 	 * If the system has already booted and thread
1465 	 * scheduling is possible indicated by the global
1466 	 * cold set to zero, we just call the driver
1467 	 * initialization directly.
1468 	 */
1469 	if (!cold)
1470 		vmbus_doattach(vmbus_sc);
1471 #endif	/* EARLY_AP_STARTUP */
1472 
1473 	return (0);
1474 }
1475 
1476 static int
1477 vmbus_detach(device_t dev)
1478 {
1479 	struct vmbus_softc *sc = device_get_softc(dev);
1480 
1481 	bus_generic_detach(dev);
1482 	vmbus_chan_destroy_all(sc);
1483 
1484 	vmbus_scan_teardown(sc);
1485 
1486 	vmbus_disconnect(sc);
1487 
1488 	if (sc->vmbus_flags & VMBUS_FLAG_SYNIC) {
1489 		sc->vmbus_flags &= ~VMBUS_FLAG_SYNIC;
1490 		smp_rendezvous(NULL, vmbus_synic_teardown, NULL, NULL);
1491 	}
1492 
1493 	vmbus_intr_teardown(sc);
1494 	vmbus_dma_free(sc);
1495 
1496 	if (sc->vmbus_xc != NULL) {
1497 		vmbus_xact_ctx_destroy(sc->vmbus_xc);
1498 		sc->vmbus_xc = NULL;
1499 	}
1500 
1501 	free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF);
1502 	mtx_destroy(&sc->vmbus_prichan_lock);
1503 	mtx_destroy(&sc->vmbus_chan_lock);
1504 
1505 #ifdef NEW_PCIB
1506 	vmbus_free_mmio_res(dev);
1507 #endif
1508 
1509 	return (0);
1510 }
1511 
1512 #ifndef EARLY_AP_STARTUP
1513 
1514 static void
1515 vmbus_sysinit(void *arg __unused)
1516 {
1517 	struct vmbus_softc *sc = vmbus_get_softc();
1518 
1519 	if (vm_guest != VM_GUEST_HV || sc == NULL)
1520 		return;
1521 
1522 	/*
1523 	 * If the system has already booted and thread
1524 	 * scheduling is possible, as indicated by the
1525 	 * global cold set to zero, we just call the driver
1526 	 * initialization directly.
1527 	 */
1528 	if (!cold)
1529 		vmbus_doattach(sc);
1530 }
1531 /*
1532  * NOTE:
1533  * We have to start as the last step of SI_SUB_SMP, i.e. after SMP is
1534  * initialized.
1535  */
1536 SYSINIT(vmbus_initialize, SI_SUB_SMP, SI_ORDER_ANY, vmbus_sysinit, NULL);
1537 
1538 #endif	/* !EARLY_AP_STARTUP */
1539