2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
30 * VM Bus Driver Implementation
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <sys/taskqueue.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/apicvar.h>
50 #include <machine/md_var.h>
52 #include <contrib/dev/acpica/include/acpi.h>
54 #include <dev/hyperv/include/hyperv.h>
55 #include <dev/hyperv/include/vmbus_xact.h>
56 #include <dev/hyperv/vmbus/hyperv_reg.h>
57 #include <dev/hyperv/vmbus/hyperv_var.h>
58 #include <dev/hyperv/vmbus/vmbus_reg.h>
59 #include <dev/hyperv/vmbus/vmbus_var.h>
60 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
65 #define VMBUS_GPADL_START 0xe1e10
68 struct vmbus_xact *mh_xact;
69 struct hypercall_postmsg_in mh_inprm_save;
72 static int vmbus_probe(device_t);
73 static int vmbus_attach(device_t);
74 static int vmbus_detach(device_t);
75 static int vmbus_read_ivar(device_t, device_t, int,
77 static int vmbus_child_pnpinfo_str(device_t, device_t,
79 static uint32_t vmbus_get_version_method(device_t, device_t);
80 static int vmbus_probe_guid_method(device_t, device_t,
81 const struct hyperv_guid *);
83 static int vmbus_init(struct vmbus_softc *);
84 static int vmbus_connect(struct vmbus_softc *, uint32_t);
85 static int vmbus_req_channels(struct vmbus_softc *sc);
86 static void vmbus_disconnect(struct vmbus_softc *);
87 static int vmbus_scan(struct vmbus_softc *);
88 static void vmbus_scan_wait(struct vmbus_softc *);
89 static void vmbus_scan_newchan(struct vmbus_softc *);
90 static void vmbus_scan_newdev(struct vmbus_softc *);
91 static void vmbus_scan_done(struct vmbus_softc *,
92 const struct vmbus_message *);
93 static void vmbus_chanmsg_handle(struct vmbus_softc *,
94 const struct vmbus_message *);
95 static void vmbus_msg_task(void *, int);
96 static void vmbus_synic_setup(void *);
97 static void vmbus_synic_teardown(void *);
98 static int vmbus_sysctl_version(SYSCTL_HANDLER_ARGS);
99 static int vmbus_dma_alloc(struct vmbus_softc *);
100 static void vmbus_dma_free(struct vmbus_softc *);
101 static int vmbus_intr_setup(struct vmbus_softc *);
102 static void vmbus_intr_teardown(struct vmbus_softc *);
103 static int vmbus_doattach(struct vmbus_softc *);
104 static void vmbus_event_proc_dummy(struct vmbus_softc *,
107 static struct vmbus_softc *vmbus_sc;
109 extern inthand_t IDTVEC(rsvd), IDTVEC(vmbus_isr);
111 static const uint32_t vmbus_version[] = {
112 VMBUS_VERSION_WIN8_1,
118 static const vmbus_chanmsg_proc_t
119 vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = {
120 VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done),
121 VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP)
124 static device_method_t vmbus_methods[] = {
125 /* Device interface */
126 DEVMETHOD(device_probe, vmbus_probe),
127 DEVMETHOD(device_attach, vmbus_attach),
128 DEVMETHOD(device_detach, vmbus_detach),
129 DEVMETHOD(device_shutdown, bus_generic_shutdown),
130 DEVMETHOD(device_suspend, bus_generic_suspend),
131 DEVMETHOD(device_resume, bus_generic_resume),
134 DEVMETHOD(bus_add_child, bus_generic_add_child),
135 DEVMETHOD(bus_print_child, bus_generic_print_child),
136 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
137 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str),
139 /* Vmbus interface */
140 DEVMETHOD(vmbus_get_version, vmbus_get_version_method),
141 DEVMETHOD(vmbus_probe_guid, vmbus_probe_guid_method),
146 static driver_t vmbus_driver = {
149 sizeof(struct vmbus_softc)
152 static devclass_t vmbus_devclass;
154 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, NULL, NULL);
155 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
156 MODULE_VERSION(vmbus, 1);
158 static __inline struct vmbus_softc *
159 vmbus_get_softc(void)
165 vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize)
167 struct hypercall_postmsg_in *inprm;
169 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
170 panic("invalid data size %zu", dsize);
172 inprm = vmbus_xact_req_data(mh->mh_xact);
173 memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE);
174 inprm->hc_connid = VMBUS_CONNID_MESSAGE;
175 inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL;
176 inprm->hc_dsize = dsize;
180 vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize)
182 struct vmbus_msghc *mh;
183 struct vmbus_xact *xact;
185 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
186 panic("invalid data size %zu", dsize);
188 xact = vmbus_xact_get(sc->vmbus_xc,
189 dsize + __offsetof(struct hypercall_postmsg_in, hc_data[0]));
193 mh = vmbus_xact_priv(xact, sizeof(*mh));
196 vmbus_msghc_reset(mh, dsize);
201 vmbus_msghc_put(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
204 vmbus_xact_put(mh->mh_xact);
208 vmbus_msghc_dataptr(struct vmbus_msghc *mh)
210 struct hypercall_postmsg_in *inprm;
212 inprm = vmbus_xact_req_data(mh->mh_xact);
213 return (inprm->hc_data);
217 vmbus_msghc_exec_noresult(struct vmbus_msghc *mh)
219 sbintime_t time = SBT_1MS;
220 struct hypercall_postmsg_in *inprm;
221 bus_addr_t inprm_paddr;
224 inprm = vmbus_xact_req_data(mh->mh_xact);
225 inprm_paddr = vmbus_xact_req_paddr(mh->mh_xact);
228 * Save the input parameter so that we could restore the input
229 * parameter if the Hypercall failed.
232 * Is this really necessary?! i.e. Will the Hypercall ever
233 * overwrite the input parameter?
235 memcpy(&mh->mh_inprm_save, inprm, HYPERCALL_POSTMSGIN_SIZE);
238 * In order to cope with transient failures, e.g. insufficient
239 * resources on host side, we retry the post message Hypercall
240 * several times. 20 retries seem sufficient.
242 #define HC_RETRY_MAX 20
244 for (i = 0; i < HC_RETRY_MAX; ++i) {
247 status = hypercall_post_message(inprm_paddr);
248 if (status == HYPERCALL_STATUS_SUCCESS)
251 pause_sbt("hcpmsg", time, 0, C_HARDCLOCK);
252 if (time < SBT_1S * 2)
255 /* Restore input parameter and try again */
256 memcpy(inprm, &mh->mh_inprm_save, HYPERCALL_POSTMSGIN_SIZE);
265 vmbus_msghc_exec(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
269 vmbus_xact_activate(mh->mh_xact);
270 error = vmbus_msghc_exec_noresult(mh);
272 vmbus_xact_deactivate(mh->mh_xact);
276 const struct vmbus_message *
277 vmbus_msghc_wait_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
281 return (vmbus_xact_wait(mh->mh_xact, &resp_len));
285 vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg)
288 vmbus_xact_ctx_wakeup(sc->vmbus_xc, msg, sizeof(*msg));
292 vmbus_gpadl_alloc(struct vmbus_softc *sc)
294 return atomic_fetchadd_int(&sc->vmbus_gpadl, 1);
298 vmbus_connect(struct vmbus_softc *sc, uint32_t version)
300 struct vmbus_chanmsg_connect *req;
301 const struct vmbus_message *msg;
302 struct vmbus_msghc *mh;
305 mh = vmbus_msghc_get(sc, sizeof(*req));
309 req = vmbus_msghc_dataptr(mh);
310 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT;
311 req->chm_ver = version;
312 req->chm_evtflags = sc->vmbus_evtflags_dma.hv_paddr;
313 req->chm_mnf1 = sc->vmbus_mnf1_dma.hv_paddr;
314 req->chm_mnf2 = sc->vmbus_mnf2_dma.hv_paddr;
316 error = vmbus_msghc_exec(sc, mh);
318 vmbus_msghc_put(sc, mh);
322 msg = vmbus_msghc_wait_result(sc, mh);
323 done = ((const struct vmbus_chanmsg_connect_resp *)
324 msg->msg_data)->chm_done;
326 vmbus_msghc_put(sc, mh);
328 return (done ? 0 : EOPNOTSUPP);
332 vmbus_init(struct vmbus_softc *sc)
336 for (i = 0; i < nitems(vmbus_version); ++i) {
339 error = vmbus_connect(sc, vmbus_version[i]);
341 sc->vmbus_version = vmbus_version[i];
342 device_printf(sc->vmbus_dev, "version %u.%u\n",
343 VMBUS_VERSION_MAJOR(sc->vmbus_version),
344 VMBUS_VERSION_MINOR(sc->vmbus_version));
352 vmbus_disconnect(struct vmbus_softc *sc)
354 struct vmbus_chanmsg_disconnect *req;
355 struct vmbus_msghc *mh;
358 mh = vmbus_msghc_get(sc, sizeof(*req));
360 device_printf(sc->vmbus_dev,
361 "can not get msg hypercall for disconnect\n");
365 req = vmbus_msghc_dataptr(mh);
366 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT;
368 error = vmbus_msghc_exec_noresult(mh);
369 vmbus_msghc_put(sc, mh);
372 device_printf(sc->vmbus_dev,
373 "disconnect msg hypercall failed\n");
378 vmbus_req_channels(struct vmbus_softc *sc)
380 struct vmbus_chanmsg_chrequest *req;
381 struct vmbus_msghc *mh;
384 mh = vmbus_msghc_get(sc, sizeof(*req));
388 req = vmbus_msghc_dataptr(mh);
389 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST;
391 error = vmbus_msghc_exec_noresult(mh);
392 vmbus_msghc_put(sc, mh);
398 vmbus_scan_newchan(struct vmbus_softc *sc)
400 mtx_lock(&sc->vmbus_scan_lock);
401 if ((sc->vmbus_scan_chcnt & VMBUS_SCAN_CHCNT_DONE) == 0)
402 sc->vmbus_scan_chcnt++;
403 mtx_unlock(&sc->vmbus_scan_lock);
407 vmbus_scan_done(struct vmbus_softc *sc,
408 const struct vmbus_message *msg __unused)
410 mtx_lock(&sc->vmbus_scan_lock);
411 sc->vmbus_scan_chcnt |= VMBUS_SCAN_CHCNT_DONE;
412 mtx_unlock(&sc->vmbus_scan_lock);
413 wakeup(&sc->vmbus_scan_chcnt);
417 vmbus_scan_newdev(struct vmbus_softc *sc)
419 mtx_lock(&sc->vmbus_scan_lock);
420 sc->vmbus_scan_devcnt++;
421 mtx_unlock(&sc->vmbus_scan_lock);
422 wakeup(&sc->vmbus_scan_devcnt);
426 vmbus_scan_wait(struct vmbus_softc *sc)
430 mtx_lock(&sc->vmbus_scan_lock);
431 while ((sc->vmbus_scan_chcnt & VMBUS_SCAN_CHCNT_DONE) == 0) {
432 mtx_sleep(&sc->vmbus_scan_chcnt, &sc->vmbus_scan_lock, 0,
435 chancnt = sc->vmbus_scan_chcnt & ~VMBUS_SCAN_CHCNT_DONE;
437 while (sc->vmbus_scan_devcnt != chancnt) {
438 mtx_sleep(&sc->vmbus_scan_devcnt, &sc->vmbus_scan_lock, 0,
441 mtx_unlock(&sc->vmbus_scan_lock);
445 vmbus_scan(struct vmbus_softc *sc)
450 * Start vmbus scanning.
452 error = vmbus_req_channels(sc);
454 device_printf(sc->vmbus_dev, "channel request failed: %d\n",
460 * Wait for all devices are added to vmbus.
465 * Identify, probe and attach.
467 bus_generic_probe(sc->vmbus_dev);
468 bus_generic_attach(sc->vmbus_dev);
471 device_printf(sc->vmbus_dev, "device scan, probe and attach "
478 vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg)
480 vmbus_chanmsg_proc_t msg_proc;
483 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
484 if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) {
485 device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
490 msg_proc = vmbus_chanmsg_handlers[msg_type];
491 if (msg_proc != NULL)
494 /* Channel specific processing */
495 vmbus_chan_msgproc(sc, msg);
499 vmbus_msg_task(void *xsc, int pending __unused)
501 struct vmbus_softc *sc = xsc;
502 volatile struct vmbus_message *msg;
504 msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE;
506 if (msg->msg_type == HYPERV_MSGTYPE_NONE) {
509 } else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) {
510 /* Channel message */
511 vmbus_chanmsg_handle(sc,
512 __DEVOLATILE(const struct vmbus_message *, msg));
515 msg->msg_type = HYPERV_MSGTYPE_NONE;
517 * Make sure the write to msg_type (i.e. set to
518 * HYPERV_MSGTYPE_NONE) happens before we read the
519 * msg_flags and EOMing. Otherwise, the EOMing will
520 * not deliver any more messages since there is no
524 * mb() is used here, since atomic_thread_fence_seq_cst()
525 * will become compiler fence on UP kernel.
528 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
530 * This will cause message queue rescan to possibly
531 * deliver another msg from the hypervisor
533 wrmsr(MSR_HV_EOM, 0);
539 vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu)
541 volatile struct vmbus_message *msg;
542 struct vmbus_message *msg_base;
544 msg_base = VMBUS_PCPU_GET(sc, message, cpu);
549 * TODO: move this to independent IDT vector.
551 msg = msg_base + VMBUS_SINT_TIMER;
552 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
553 msg->msg_type = HYPERV_MSGTYPE_NONE;
555 vmbus_et_intr(frame);
558 * Make sure the write to msg_type (i.e. set to
559 * HYPERV_MSGTYPE_NONE) happens before we read the
560 * msg_flags and EOMing. Otherwise, the EOMing will
561 * not deliver any more messages since there is no
565 * mb() is used here, since atomic_thread_fence_seq_cst()
566 * will become compiler fence on UP kernel.
569 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
571 * This will cause message queue rescan to possibly
572 * deliver another msg from the hypervisor
574 wrmsr(MSR_HV_EOM, 0);
579 * Check events. Hot path for network and storage I/O data; high rate.
582 * As recommended by the Windows guest fellows, we check events before
585 sc->vmbus_event_proc(sc, cpu);
588 * Check messages. Mainly management stuffs; ultra low rate.
590 msg = msg_base + VMBUS_SINT_MESSAGE;
591 if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
592 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
593 VMBUS_PCPU_PTR(sc, message_task, cpu));
596 return (FILTER_HANDLED);
600 vmbus_handle_intr(struct trapframe *trap_frame)
602 struct vmbus_softc *sc = vmbus_get_softc();
606 * Disable preemption.
611 * Do a little interrupt counting.
613 (*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++;
615 vmbus_handle_intr1(sc, trap_frame, cpu);
624 vmbus_synic_setup(void *xsc)
626 struct vmbus_softc *sc = xsc;
631 if (hyperv_features & CPUID_HV_MSR_VP_INDEX) {
632 /* Save virtual processor id. */
633 VMBUS_PCPU_GET(sc, vcpuid, cpu) = rdmsr(MSR_HV_VP_INDEX);
635 /* Set virtual processor id to 0 for compatibility. */
636 VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0;
640 * Setup the SynIC message.
642 orig = rdmsr(MSR_HV_SIMP);
643 val = MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK) |
644 ((VMBUS_PCPU_GET(sc, message_dma.hv_paddr, cpu) >> PAGE_SHIFT) <<
645 MSR_HV_SIMP_PGSHIFT);
646 wrmsr(MSR_HV_SIMP, val);
649 * Setup the SynIC event flags.
651 orig = rdmsr(MSR_HV_SIEFP);
652 val = MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK) |
653 ((VMBUS_PCPU_GET(sc, event_flags_dma.hv_paddr, cpu)
654 >> PAGE_SHIFT) << MSR_HV_SIEFP_PGSHIFT);
655 wrmsr(MSR_HV_SIEFP, val);
659 * Configure and unmask SINT for message and event flags.
661 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
663 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
664 (orig & MSR_HV_SINT_RSVD_MASK);
668 * Configure and unmask SINT for timer.
670 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
672 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
673 (orig & MSR_HV_SINT_RSVD_MASK);
677 * All done; enable SynIC.
679 orig = rdmsr(MSR_HV_SCONTROL);
680 val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK);
681 wrmsr(MSR_HV_SCONTROL, val);
685 vmbus_synic_teardown(void *arg)
693 orig = rdmsr(MSR_HV_SCONTROL);
694 wrmsr(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK));
697 * Mask message and event flags SINT.
699 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
701 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
706 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
708 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
711 * Teardown SynIC message.
713 orig = rdmsr(MSR_HV_SIMP);
714 wrmsr(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK));
717 * Teardown SynIC event flags.
719 orig = rdmsr(MSR_HV_SIEFP);
720 wrmsr(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK));
724 vmbus_dma_alloc(struct vmbus_softc *sc)
726 bus_dma_tag_t parent_dtag;
730 parent_dtag = bus_get_dma_tag(sc->vmbus_dev);
735 * Per-cpu messages and event flags.
737 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
738 PAGE_SIZE, VMBUS_PCPU_PTR(sc, message_dma, cpu),
739 BUS_DMA_WAITOK | BUS_DMA_ZERO);
742 VMBUS_PCPU_GET(sc, message, cpu) = ptr;
744 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
745 PAGE_SIZE, VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
746 BUS_DMA_WAITOK | BUS_DMA_ZERO);
749 VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr;
752 evtflags = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
753 PAGE_SIZE, &sc->vmbus_evtflags_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
754 if (evtflags == NULL)
756 sc->vmbus_rx_evtflags = (u_long *)evtflags;
757 sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2));
758 sc->vmbus_evtflags = evtflags;
760 sc->vmbus_mnf1 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
761 PAGE_SIZE, &sc->vmbus_mnf1_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
762 if (sc->vmbus_mnf1 == NULL)
765 sc->vmbus_mnf2 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
766 sizeof(struct vmbus_mnf), &sc->vmbus_mnf2_dma,
767 BUS_DMA_WAITOK | BUS_DMA_ZERO);
768 if (sc->vmbus_mnf2 == NULL)
775 vmbus_dma_free(struct vmbus_softc *sc)
779 if (sc->vmbus_evtflags != NULL) {
780 hyperv_dmamem_free(&sc->vmbus_evtflags_dma, sc->vmbus_evtflags);
781 sc->vmbus_evtflags = NULL;
782 sc->vmbus_rx_evtflags = NULL;
783 sc->vmbus_tx_evtflags = NULL;
785 if (sc->vmbus_mnf1 != NULL) {
786 hyperv_dmamem_free(&sc->vmbus_mnf1_dma, sc->vmbus_mnf1);
787 sc->vmbus_mnf1 = NULL;
789 if (sc->vmbus_mnf2 != NULL) {
790 hyperv_dmamem_free(&sc->vmbus_mnf2_dma, sc->vmbus_mnf2);
791 sc->vmbus_mnf2 = NULL;
795 if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) {
797 VMBUS_PCPU_PTR(sc, message_dma, cpu),
798 VMBUS_PCPU_GET(sc, message, cpu));
799 VMBUS_PCPU_GET(sc, message, cpu) = NULL;
801 if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) {
803 VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
804 VMBUS_PCPU_GET(sc, event_flags, cpu));
805 VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL;
811 * @brief Find a free IDT slot and setup the interrupt handler.
814 vmbus_vector_alloc(void)
818 struct gate_descriptor *ip;
821 * Search backwards form the highest IDT vector available for use
822 * as vmbus channel callback vector. We install 'vmbus_isr'
823 * handler at that vector and use it to interrupt vcpus.
825 vector = APIC_SPURIOUS_INT;
826 while (--vector >= APIC_IPI_INTS) {
828 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
829 if (func == (uintptr_t)&IDTVEC(rsvd)) {
831 setidt(vector , IDTVEC(vmbus_isr), SDT_SYS386IGT,
832 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
834 setidt(vector , IDTVEC(vmbus_isr), SDT_SYSIGT,
845 * @brief Restore the IDT slot to rsvd.
848 vmbus_vector_free(int vector)
851 struct gate_descriptor *ip;
856 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
857 ("invalid vector %d", vector));
860 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
861 KASSERT(func == (uintptr_t)&IDTVEC(vmbus_isr),
862 ("invalid vector %d", vector));
864 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
868 vmbus_cpuset_setthread_task(void *xmask, int pending __unused)
870 cpuset_t *mask = xmask;
873 error = cpuset_setthread(curthread->td_tid, mask);
875 panic("curthread=%ju: can't pin; error=%d",
876 (uintmax_t)curthread->td_tid, error);
881 vmbus_intr_setup(struct vmbus_softc *sc)
886 struct task cpuset_task;
887 char buf[MAXCOMLEN + 1];
890 /* Allocate an interrupt counter for Hyper-V interrupt */
891 snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu);
892 intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu));
895 * Setup taskqueue to handle events. Task will be per-
898 VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast(
899 "hyperv event", M_WAITOK, taskqueue_thread_enqueue,
900 VMBUS_PCPU_PTR(sc, event_tq, cpu));
901 taskqueue_start_threads(VMBUS_PCPU_PTR(sc, event_tq, cpu),
902 1, PI_NET, "hvevent%d", cpu);
904 CPU_SETOF(cpu, &cpu_mask);
905 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task,
907 taskqueue_enqueue(VMBUS_PCPU_GET(sc, event_tq, cpu),
909 taskqueue_drain(VMBUS_PCPU_GET(sc, event_tq, cpu),
913 * Setup tasks and taskqueues to handle messages.
915 VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast(
916 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
917 VMBUS_PCPU_PTR(sc, message_tq, cpu));
918 taskqueue_start_threads(VMBUS_PCPU_PTR(sc, message_tq, cpu), 1,
919 PI_NET, "hvmsg%d", cpu);
920 TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0,
923 CPU_SETOF(cpu, &cpu_mask);
924 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task,
926 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
928 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
933 * All Hyper-V ISR required resources are setup, now let's find a
934 * free IDT vector for Hyper-V ISR and set it up.
936 sc->vmbus_idtvec = vmbus_vector_alloc();
937 if (sc->vmbus_idtvec == 0) {
938 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
942 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
949 vmbus_intr_teardown(struct vmbus_softc *sc)
953 vmbus_vector_free(sc->vmbus_idtvec);
956 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
957 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
958 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
960 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
961 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
962 VMBUS_PCPU_PTR(sc, message_task, cpu));
963 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
964 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
970 vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
976 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
978 const struct vmbus_channel *chan;
979 char guidbuf[HYPERV_GUID_STRLEN];
981 chan = vmbus_get_channel(child);
983 /* Event timer device, which does not belong to a channel */
987 strlcat(buf, "classid=", buflen);
988 hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf));
989 strlcat(buf, guidbuf, buflen);
991 strlcat(buf, " deviceid=", buflen);
992 hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf));
993 strlcat(buf, guidbuf, buflen);
999 vmbus_add_child(struct vmbus_channel *chan)
1001 struct vmbus_softc *sc = chan->ch_vmbus;
1002 device_t parent = sc->vmbus_dev;
1005 /* New channel has been offered */
1006 vmbus_scan_newchan(sc);
1008 chan->ch_dev = device_add_child(parent, NULL, -1);
1009 if (chan->ch_dev == NULL) {
1010 device_printf(parent, "device_add_child for chan%u failed\n",
1015 device_set_ivars(chan->ch_dev, chan);
1018 /* New device has been/should be added to vmbus. */
1019 vmbus_scan_newdev(sc);
1024 vmbus_delete_child(struct vmbus_channel *chan)
1028 if (chan->ch_dev == NULL) {
1029 /* Failed to add a device. */
1034 * XXXKYS: Ensure that this is the opposite of
1035 * device_add_child()
1038 error = device_delete_child(chan->ch_vmbus->vmbus_dev, chan->ch_dev);
1045 vmbus_sysctl_version(SYSCTL_HANDLER_ARGS)
1047 struct vmbus_softc *sc = arg1;
1050 snprintf(verstr, sizeof(verstr), "%u.%u",
1051 VMBUS_VERSION_MAJOR(sc->vmbus_version),
1052 VMBUS_VERSION_MINOR(sc->vmbus_version));
1053 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
1057 vmbus_get_version_method(device_t bus, device_t dev)
1059 struct vmbus_softc *sc = device_get_softc(bus);
1061 return sc->vmbus_version;
1065 vmbus_probe_guid_method(device_t bus, device_t dev,
1066 const struct hyperv_guid *guid)
1068 const struct vmbus_channel *chan = vmbus_get_channel(dev);
1070 if (memcmp(&chan->ch_guid_type, guid, sizeof(struct hyperv_guid)) == 0)
1076 vmbus_probe(device_t dev)
1078 char *id[] = { "VMBUS", NULL };
1080 if (ACPI_ID_PROBE(device_get_parent(dev), dev, id) == NULL ||
1081 device_get_unit(dev) != 0 || vm_guest != VM_GUEST_HV ||
1082 (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1085 device_set_desc(dev, "Hyper-V Vmbus");
1087 return (BUS_PROBE_DEFAULT);
1091 * @brief Main vmbus driver initialization routine.
1094 * - initialize the vmbus driver context
1095 * - setup various driver entry points
1096 * - invoke the vmbus hv main init routine
1097 * - get the irq resource
1098 * - invoke the vmbus to add the vmbus root device
1099 * - setup the vmbus root device
1100 * - retrieve the channel offers
1103 vmbus_doattach(struct vmbus_softc *sc)
1105 struct sysctl_oid_list *child;
1106 struct sysctl_ctx_list *ctx;
1109 if (sc->vmbus_flags & VMBUS_FLAG_ATTACHED)
1111 sc->vmbus_flags |= VMBUS_FLAG_ATTACHED;
1113 mtx_init(&sc->vmbus_scan_lock, "vmbus scan", NULL, MTX_DEF);
1114 sc->vmbus_gpadl = VMBUS_GPADL_START;
1115 mtx_init(&sc->vmbus_prichan_lock, "vmbus prichan", NULL, MTX_DEF);
1116 TAILQ_INIT(&sc->vmbus_prichans);
1117 sc->vmbus_chmap = malloc(
1118 sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX, M_DEVBUF,
1122 * Create context for "post message" Hypercalls
1124 sc->vmbus_xc = vmbus_xact_ctx_create(bus_get_dma_tag(sc->vmbus_dev),
1125 HYPERCALL_POSTMSGIN_SIZE, VMBUS_MSG_SIZE,
1126 sizeof(struct vmbus_msghc));
1127 if (sc->vmbus_xc == NULL) {
1133 * Allocate DMA stuffs.
1135 ret = vmbus_dma_alloc(sc);
1142 ret = vmbus_intr_setup(sc);
1150 device_printf(sc->vmbus_dev, "smp_started = %d\n", smp_started);
1151 smp_rendezvous(NULL, vmbus_synic_setup, NULL, sc);
1152 sc->vmbus_flags |= VMBUS_FLAG_SYNIC;
1155 * Initialize vmbus, e.g. connect to Hypervisor.
1157 ret = vmbus_init(sc);
1161 if (sc->vmbus_version == VMBUS_VERSION_WS2008 ||
1162 sc->vmbus_version == VMBUS_VERSION_WIN7)
1163 sc->vmbus_event_proc = vmbus_event_proc_compat;
1165 sc->vmbus_event_proc = vmbus_event_proc;
1167 ret = vmbus_scan(sc);
1171 ctx = device_get_sysctl_ctx(sc->vmbus_dev);
1172 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vmbus_dev));
1173 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "version",
1174 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1175 vmbus_sysctl_version, "A", "vmbus version");
1180 vmbus_intr_teardown(sc);
1182 if (sc->vmbus_xc != NULL) {
1183 vmbus_xact_ctx_destroy(sc->vmbus_xc);
1184 sc->vmbus_xc = NULL;
1186 free(sc->vmbus_chmap, M_DEVBUF);
1187 mtx_destroy(&sc->vmbus_scan_lock);
1188 mtx_destroy(&sc->vmbus_prichan_lock);
1194 vmbus_event_proc_dummy(struct vmbus_softc *sc __unused, int cpu __unused)
1199 vmbus_attach(device_t dev)
1201 vmbus_sc = device_get_softc(dev);
1202 vmbus_sc->vmbus_dev = dev;
1205 * Event processing logic will be configured:
1206 * - After the vmbus protocol version negotiation.
1207 * - Before we request channel offers.
1209 vmbus_sc->vmbus_event_proc = vmbus_event_proc_dummy;
1212 * If the system has already booted and thread
1213 * scheduling is possible indicated by the global
1214 * cold set to zero, we just call the driver
1215 * initialization directly.
1218 vmbus_doattach(vmbus_sc);
1224 vmbus_detach(device_t dev)
1226 struct vmbus_softc *sc = device_get_softc(dev);
1228 vmbus_chan_destroy_all(sc);
1230 vmbus_disconnect(sc);
1232 if (sc->vmbus_flags & VMBUS_FLAG_SYNIC) {
1233 sc->vmbus_flags &= ~VMBUS_FLAG_SYNIC;
1234 smp_rendezvous(NULL, vmbus_synic_teardown, NULL, NULL);
1237 vmbus_intr_teardown(sc);
1240 if (sc->vmbus_xc != NULL) {
1241 vmbus_xact_ctx_destroy(sc->vmbus_xc);
1242 sc->vmbus_xc = NULL;
1245 free(sc->vmbus_chmap, M_DEVBUF);
1246 mtx_destroy(&sc->vmbus_scan_lock);
1247 mtx_destroy(&sc->vmbus_prichan_lock);
1253 vmbus_sysinit(void *arg __unused)
1255 struct vmbus_softc *sc = vmbus_get_softc();
1257 if (vm_guest != VM_GUEST_HV || sc == NULL)
1261 * If the system has already booted and thread
1262 * scheduling is possible, as indicated by the
1263 * global cold set to zero, we just call the driver
1264 * initialization directly.
1271 * We have to start as the last step of SI_SUB_SMP, i.e. after SMP is
1274 SYSINIT(vmbus_initialize, SI_SUB_SMP, SI_ORDER_ANY, vmbus_sysinit, NULL);