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