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