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 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/linker.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/smp.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/taskqueue.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/pmap.h>
49
50 #include <machine/bus.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/metadata.h>
53 #include <machine/md_var.h>
54 #include <machine/resource.h>
55 #include <x86/include/apicvar.h>
56
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <dev/acpica/acpivar.h>
59
60 #include <dev/hyperv/include/hyperv.h>
61 #include <dev/hyperv/include/vmbus_xact.h>
62 #include <dev/hyperv/vmbus/hyperv_reg.h>
63 #include <dev/hyperv/vmbus/hyperv_var.h>
64 #include <dev/hyperv/vmbus/vmbus_reg.h>
65 #include <dev/hyperv/vmbus/vmbus_var.h>
66 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
67
68 #include "acpi_if.h"
69 #include "pcib_if.h"
70 #include "vmbus_if.h"
71
72 #define VMBUS_GPADL_START 0xe1e10
73
74 struct vmbus_msghc {
75 struct vmbus_xact *mh_xact;
76 struct hypercall_postmsg_in mh_inprm_save;
77 };
78
79 static void vmbus_identify(driver_t *, device_t);
80 static int vmbus_probe(device_t);
81 static int vmbus_attach(device_t);
82 static int vmbus_detach(device_t);
83 static int vmbus_read_ivar(device_t, device_t, int,
84 uintptr_t *);
85 static int vmbus_child_pnpinfo_str(device_t, device_t,
86 char *, size_t);
87 static struct resource *vmbus_alloc_resource(device_t dev,
88 device_t child, int type, int *rid,
89 rman_res_t start, rman_res_t end,
90 rman_res_t count, u_int flags);
91 static int vmbus_alloc_msi(device_t bus, device_t dev,
92 int count, int maxcount, int *irqs);
93 static int vmbus_release_msi(device_t bus, device_t dev,
94 int count, int *irqs);
95 static int vmbus_alloc_msix(device_t bus, device_t dev,
96 int *irq);
97 static int vmbus_release_msix(device_t bus, device_t dev,
98 int irq);
99 static int vmbus_map_msi(device_t bus, device_t dev,
100 int irq, uint64_t *addr, uint32_t *data);
101 static uint32_t vmbus_get_version_method(device_t, device_t);
102 static int vmbus_probe_guid_method(device_t, device_t,
103 const struct hyperv_guid *);
104 static uint32_t vmbus_get_vcpu_id_method(device_t bus,
105 device_t dev, int cpu);
106 static struct taskqueue *vmbus_get_eventtq_method(device_t, device_t,
107 int);
108 #ifdef EARLY_AP_STARTUP
109 static void vmbus_intrhook(void *);
110 #endif
111
112 static int vmbus_init(struct vmbus_softc *);
113 static int vmbus_connect(struct vmbus_softc *, uint32_t);
114 static int vmbus_req_channels(struct vmbus_softc *sc);
115 static void vmbus_disconnect(struct vmbus_softc *);
116 static int vmbus_scan(struct vmbus_softc *);
117 static void vmbus_scan_teardown(struct vmbus_softc *);
118 static void vmbus_scan_done(struct vmbus_softc *,
119 const struct vmbus_message *);
120 static void vmbus_chanmsg_handle(struct vmbus_softc *,
121 const struct vmbus_message *);
122 static void vmbus_msg_task(void *, int);
123 static void vmbus_synic_setup(void *);
124 static void vmbus_synic_teardown(void *);
125 static int vmbus_sysctl_version(SYSCTL_HANDLER_ARGS);
126 static int vmbus_dma_alloc(struct vmbus_softc *);
127 static void vmbus_dma_free(struct vmbus_softc *);
128 static int vmbus_intr_setup(struct vmbus_softc *);
129 static void vmbus_intr_teardown(struct vmbus_softc *);
130 static int vmbus_doattach(struct vmbus_softc *);
131 static void vmbus_event_proc_dummy(struct vmbus_softc *,
132 int);
133
134 static struct vmbus_softc *vmbus_sc;
135
136 SYSCTL_NODE(_hw, OID_AUTO, vmbus, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
137 "Hyper-V vmbus");
138
139 static int vmbus_pin_evttask = 1;
140 SYSCTL_INT(_hw_vmbus, OID_AUTO, pin_evttask, CTLFLAG_RDTUN,
141 &vmbus_pin_evttask, 0, "Pin event tasks to their respective CPU");
142
143 extern inthand_t IDTVEC(vmbus_isr), IDTVEC(vmbus_isr_pti);
144 #define VMBUS_ISR_ADDR trunc_page((uintptr_t)IDTVEC(vmbus_isr_pti))
145
146 uint32_t vmbus_current_version;
147
148 static const uint32_t vmbus_version[] = {
149 VMBUS_VERSION_WIN10,
150 VMBUS_VERSION_WIN8_1,
151 VMBUS_VERSION_WIN8,
152 VMBUS_VERSION_WIN7,
153 VMBUS_VERSION_WS2008
154 };
155
156 static const vmbus_chanmsg_proc_t
157 vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = {
158 VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done),
159 VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP)
160 };
161
162 static device_method_t vmbus_methods[] = {
163 /* Device interface */
164 DEVMETHOD(device_identify, vmbus_identify),
165 DEVMETHOD(device_probe, vmbus_probe),
166 DEVMETHOD(device_attach, vmbus_attach),
167 DEVMETHOD(device_detach, vmbus_detach),
168 DEVMETHOD(device_shutdown, bus_generic_shutdown),
169 DEVMETHOD(device_suspend, bus_generic_suspend),
170 DEVMETHOD(device_resume, bus_generic_resume),
171
172 /* Bus interface */
173 DEVMETHOD(bus_add_child, bus_generic_add_child),
174 DEVMETHOD(bus_print_child, bus_generic_print_child),
175 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
176 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str),
177 DEVMETHOD(bus_alloc_resource, vmbus_alloc_resource),
178 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
179 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
180 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
181 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
182 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
183 #if __FreeBSD_version >= 1100000
184 DEVMETHOD(bus_get_cpus, bus_generic_get_cpus),
185 #endif
186
187 /* pcib interface */
188 DEVMETHOD(pcib_alloc_msi, vmbus_alloc_msi),
189 DEVMETHOD(pcib_release_msi, vmbus_release_msi),
190 DEVMETHOD(pcib_alloc_msix, vmbus_alloc_msix),
191 DEVMETHOD(pcib_release_msix, vmbus_release_msix),
192 DEVMETHOD(pcib_map_msi, vmbus_map_msi),
193
194 /* Vmbus interface */
195 DEVMETHOD(vmbus_get_version, vmbus_get_version_method),
196 DEVMETHOD(vmbus_probe_guid, vmbus_probe_guid_method),
197 DEVMETHOD(vmbus_get_vcpu_id, vmbus_get_vcpu_id_method),
198 DEVMETHOD(vmbus_get_event_taskq, vmbus_get_eventtq_method),
199
200 DEVMETHOD_END
201 };
202
203 static driver_t vmbus_driver = {
204 "vmbus",
205 vmbus_methods,
206 sizeof(struct vmbus_softc)
207 };
208
209 static devclass_t vmbus_devclass;
210
211 DRIVER_MODULE(vmbus, pcib, vmbus_driver, vmbus_devclass, NULL, NULL);
212 DRIVER_MODULE(vmbus, acpi_syscontainer, vmbus_driver, vmbus_devclass,
213 NULL, NULL);
214
215 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
216 MODULE_DEPEND(vmbus, pci, 1, 1, 1);
217 MODULE_VERSION(vmbus, 1);
218
219 static __inline struct vmbus_softc *
vmbus_get_softc(void)220 vmbus_get_softc(void)
221 {
222 return vmbus_sc;
223 }
224
225 void
vmbus_msghc_reset(struct vmbus_msghc * mh,size_t dsize)226 vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize)
227 {
228 struct hypercall_postmsg_in *inprm;
229
230 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
231 panic("invalid data size %zu", dsize);
232
233 inprm = vmbus_xact_req_data(mh->mh_xact);
234 memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE);
235 inprm->hc_connid = VMBUS_CONNID_MESSAGE;
236 inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL;
237 inprm->hc_dsize = dsize;
238 }
239
240 struct vmbus_msghc *
vmbus_msghc_get(struct vmbus_softc * sc,size_t dsize)241 vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize)
242 {
243 struct vmbus_msghc *mh;
244 struct vmbus_xact *xact;
245
246 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
247 panic("invalid data size %zu", dsize);
248
249 xact = vmbus_xact_get(sc->vmbus_xc,
250 dsize + __offsetof(struct hypercall_postmsg_in, hc_data[0]));
251 if (xact == NULL)
252 return (NULL);
253
254 mh = vmbus_xact_priv(xact, sizeof(*mh));
255 mh->mh_xact = xact;
256
257 vmbus_msghc_reset(mh, dsize);
258 return (mh);
259 }
260
261 void
vmbus_msghc_put(struct vmbus_softc * sc __unused,struct vmbus_msghc * mh)262 vmbus_msghc_put(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
263 {
264
265 vmbus_xact_put(mh->mh_xact);
266 }
267
268 void *
vmbus_msghc_dataptr(struct vmbus_msghc * mh)269 vmbus_msghc_dataptr(struct vmbus_msghc *mh)
270 {
271 struct hypercall_postmsg_in *inprm;
272
273 inprm = vmbus_xact_req_data(mh->mh_xact);
274 return (inprm->hc_data);
275 }
276
277 int
vmbus_msghc_exec_noresult(struct vmbus_msghc * mh)278 vmbus_msghc_exec_noresult(struct vmbus_msghc *mh)
279 {
280 sbintime_t time = SBT_1MS;
281 struct hypercall_postmsg_in *inprm;
282 bus_addr_t inprm_paddr;
283 int i;
284
285 inprm = vmbus_xact_req_data(mh->mh_xact);
286 inprm_paddr = vmbus_xact_req_paddr(mh->mh_xact);
287
288 /*
289 * Save the input parameter so that we could restore the input
290 * parameter if the Hypercall failed.
291 *
292 * XXX
293 * Is this really necessary?! i.e. Will the Hypercall ever
294 * overwrite the input parameter?
295 */
296 memcpy(&mh->mh_inprm_save, inprm, HYPERCALL_POSTMSGIN_SIZE);
297
298 /*
299 * In order to cope with transient failures, e.g. insufficient
300 * resources on host side, we retry the post message Hypercall
301 * several times. 20 retries seem sufficient.
302 */
303 #define HC_RETRY_MAX 20
304
305 for (i = 0; i < HC_RETRY_MAX; ++i) {
306 uint64_t status;
307
308 status = hypercall_post_message(inprm_paddr);
309 if (status == HYPERCALL_STATUS_SUCCESS)
310 return 0;
311
312 pause_sbt("hcpmsg", time, 0, C_HARDCLOCK);
313 if (time < SBT_1S * 2)
314 time *= 2;
315
316 /* Restore input parameter and try again */
317 memcpy(inprm, &mh->mh_inprm_save, HYPERCALL_POSTMSGIN_SIZE);
318 }
319
320 #undef HC_RETRY_MAX
321
322 return EIO;
323 }
324
325 int
vmbus_msghc_exec(struct vmbus_softc * sc __unused,struct vmbus_msghc * mh)326 vmbus_msghc_exec(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
327 {
328 int error;
329
330 vmbus_xact_activate(mh->mh_xact);
331 error = vmbus_msghc_exec_noresult(mh);
332 if (error)
333 vmbus_xact_deactivate(mh->mh_xact);
334 return error;
335 }
336
337 void
vmbus_msghc_exec_cancel(struct vmbus_softc * sc __unused,struct vmbus_msghc * mh)338 vmbus_msghc_exec_cancel(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
339 {
340
341 vmbus_xact_deactivate(mh->mh_xact);
342 }
343
344 const struct vmbus_message *
vmbus_msghc_wait_result(struct vmbus_softc * sc __unused,struct vmbus_msghc * mh)345 vmbus_msghc_wait_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
346 {
347 size_t resp_len;
348
349 return (vmbus_xact_wait(mh->mh_xact, &resp_len));
350 }
351
352 const struct vmbus_message *
vmbus_msghc_poll_result(struct vmbus_softc * sc __unused,struct vmbus_msghc * mh)353 vmbus_msghc_poll_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
354 {
355 size_t resp_len;
356
357 return (vmbus_xact_poll(mh->mh_xact, &resp_len));
358 }
359
360 void
vmbus_msghc_wakeup(struct vmbus_softc * sc,const struct vmbus_message * msg)361 vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg)
362 {
363
364 vmbus_xact_ctx_wakeup(sc->vmbus_xc, msg, sizeof(*msg));
365 }
366
367 uint32_t
vmbus_gpadl_alloc(struct vmbus_softc * sc)368 vmbus_gpadl_alloc(struct vmbus_softc *sc)
369 {
370 uint32_t gpadl;
371
372 again:
373 gpadl = atomic_fetchadd_int(&sc->vmbus_gpadl, 1);
374 if (gpadl == 0)
375 goto again;
376 return (gpadl);
377 }
378
379 /* Used for Hyper-V socket when guest client connects to host */
380 int
vmbus_req_tl_connect(struct hyperv_guid * guest_srv_id,struct hyperv_guid * host_srv_id)381 vmbus_req_tl_connect(struct hyperv_guid *guest_srv_id,
382 struct hyperv_guid *host_srv_id)
383 {
384 struct vmbus_softc *sc = vmbus_get_softc();
385 struct vmbus_chanmsg_tl_connect *req;
386 struct vmbus_msghc *mh;
387 int error;
388
389 if (!sc)
390 return ENXIO;
391
392 mh = vmbus_msghc_get(sc, sizeof(*req));
393 if (mh == NULL) {
394 device_printf(sc->vmbus_dev,
395 "can not get msg hypercall for tl connect\n");
396 return ENXIO;
397 }
398
399 req = vmbus_msghc_dataptr(mh);
400 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_TL_CONN;
401 req->guest_endpoint_id = *guest_srv_id;
402 req->host_service_id = *host_srv_id;
403
404 error = vmbus_msghc_exec_noresult(mh);
405 vmbus_msghc_put(sc, mh);
406
407 if (error) {
408 device_printf(sc->vmbus_dev,
409 "tl connect msg hypercall failed\n");
410 }
411
412 return error;
413 }
414
415 static int
vmbus_connect(struct vmbus_softc * sc,uint32_t version)416 vmbus_connect(struct vmbus_softc *sc, uint32_t version)
417 {
418 struct vmbus_chanmsg_connect *req;
419 const struct vmbus_message *msg;
420 struct vmbus_msghc *mh;
421 int error, done = 0;
422
423 mh = vmbus_msghc_get(sc, sizeof(*req));
424 if (mh == NULL)
425 return ENXIO;
426
427 req = vmbus_msghc_dataptr(mh);
428 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT;
429 req->chm_ver = version;
430 req->chm_evtflags = sc->vmbus_evtflags_dma.hv_paddr;
431 req->chm_mnf1 = sc->vmbus_mnf1_dma.hv_paddr;
432 req->chm_mnf2 = sc->vmbus_mnf2_dma.hv_paddr;
433
434 error = vmbus_msghc_exec(sc, mh);
435 if (error) {
436 vmbus_msghc_put(sc, mh);
437 return error;
438 }
439
440 msg = vmbus_msghc_wait_result(sc, mh);
441 done = ((const struct vmbus_chanmsg_connect_resp *)
442 msg->msg_data)->chm_done;
443
444 vmbus_msghc_put(sc, mh);
445
446 return (done ? 0 : EOPNOTSUPP);
447 }
448
449 static int
vmbus_init(struct vmbus_softc * sc)450 vmbus_init(struct vmbus_softc *sc)
451 {
452 int i;
453
454 for (i = 0; i < nitems(vmbus_version); ++i) {
455 int error;
456
457 error = vmbus_connect(sc, vmbus_version[i]);
458 if (!error) {
459 vmbus_current_version = vmbus_version[i];
460 sc->vmbus_version = vmbus_version[i];
461 device_printf(sc->vmbus_dev, "version %u.%u\n",
462 VMBUS_VERSION_MAJOR(sc->vmbus_version),
463 VMBUS_VERSION_MINOR(sc->vmbus_version));
464 return 0;
465 }
466 }
467 return ENXIO;
468 }
469
470 static void
vmbus_disconnect(struct vmbus_softc * sc)471 vmbus_disconnect(struct vmbus_softc *sc)
472 {
473 struct vmbus_chanmsg_disconnect *req;
474 struct vmbus_msghc *mh;
475 int error;
476
477 mh = vmbus_msghc_get(sc, sizeof(*req));
478 if (mh == NULL) {
479 device_printf(sc->vmbus_dev,
480 "can not get msg hypercall for disconnect\n");
481 return;
482 }
483
484 req = vmbus_msghc_dataptr(mh);
485 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT;
486
487 error = vmbus_msghc_exec_noresult(mh);
488 vmbus_msghc_put(sc, mh);
489
490 if (error) {
491 device_printf(sc->vmbus_dev,
492 "disconnect msg hypercall failed\n");
493 }
494 }
495
496 static int
vmbus_req_channels(struct vmbus_softc * sc)497 vmbus_req_channels(struct vmbus_softc *sc)
498 {
499 struct vmbus_chanmsg_chrequest *req;
500 struct vmbus_msghc *mh;
501 int error;
502
503 mh = vmbus_msghc_get(sc, sizeof(*req));
504 if (mh == NULL)
505 return ENXIO;
506
507 req = vmbus_msghc_dataptr(mh);
508 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST;
509
510 error = vmbus_msghc_exec_noresult(mh);
511 vmbus_msghc_put(sc, mh);
512
513 return error;
514 }
515
516 static void
vmbus_scan_done_task(void * xsc,int pending __unused)517 vmbus_scan_done_task(void *xsc, int pending __unused)
518 {
519 struct vmbus_softc *sc = xsc;
520
521 mtx_lock(&Giant);
522 sc->vmbus_scandone = true;
523 mtx_unlock(&Giant);
524 wakeup(&sc->vmbus_scandone);
525 }
526
527 static void
vmbus_scan_done(struct vmbus_softc * sc,const struct vmbus_message * msg __unused)528 vmbus_scan_done(struct vmbus_softc *sc,
529 const struct vmbus_message *msg __unused)
530 {
531
532 taskqueue_enqueue(sc->vmbus_devtq, &sc->vmbus_scandone_task);
533 }
534
535 static int
vmbus_scan(struct vmbus_softc * sc)536 vmbus_scan(struct vmbus_softc *sc)
537 {
538 int error;
539
540 /*
541 * Identify, probe and attach for non-channel devices.
542 */
543 bus_generic_probe(sc->vmbus_dev);
544 bus_generic_attach(sc->vmbus_dev);
545
546 /*
547 * This taskqueue serializes vmbus devices' attach and detach
548 * for channel offer and rescind messages.
549 */
550 sc->vmbus_devtq = taskqueue_create("vmbus dev", M_WAITOK,
551 taskqueue_thread_enqueue, &sc->vmbus_devtq);
552 taskqueue_start_threads(&sc->vmbus_devtq, 1, PI_NET, "vmbusdev");
553 TASK_INIT(&sc->vmbus_scandone_task, 0, vmbus_scan_done_task, sc);
554
555 /*
556 * This taskqueue handles sub-channel detach, so that vmbus
557 * device's detach running in vmbus_devtq can drain its sub-
558 * channels.
559 */
560 sc->vmbus_subchtq = taskqueue_create("vmbus subch", M_WAITOK,
561 taskqueue_thread_enqueue, &sc->vmbus_subchtq);
562 taskqueue_start_threads(&sc->vmbus_subchtq, 1, PI_NET, "vmbussch");
563
564 /*
565 * Start vmbus scanning.
566 */
567 error = vmbus_req_channels(sc);
568 if (error) {
569 device_printf(sc->vmbus_dev, "channel request failed: %d\n",
570 error);
571 return (error);
572 }
573
574 /*
575 * Wait for all vmbus devices from the initial channel offers to be
576 * attached.
577 */
578 GIANT_REQUIRED;
579 while (!sc->vmbus_scandone)
580 mtx_sleep(&sc->vmbus_scandone, &Giant, 0, "vmbusdev", 0);
581
582 if (bootverbose) {
583 device_printf(sc->vmbus_dev, "device scan, probe and attach "
584 "done\n");
585 }
586 return (0);
587 }
588
589 static void
vmbus_scan_teardown(struct vmbus_softc * sc)590 vmbus_scan_teardown(struct vmbus_softc *sc)
591 {
592
593 GIANT_REQUIRED;
594 if (sc->vmbus_devtq != NULL) {
595 mtx_unlock(&Giant);
596 taskqueue_free(sc->vmbus_devtq);
597 mtx_lock(&Giant);
598 sc->vmbus_devtq = NULL;
599 }
600 if (sc->vmbus_subchtq != NULL) {
601 mtx_unlock(&Giant);
602 taskqueue_free(sc->vmbus_subchtq);
603 mtx_lock(&Giant);
604 sc->vmbus_subchtq = NULL;
605 }
606 }
607
608 static void
vmbus_chanmsg_handle(struct vmbus_softc * sc,const struct vmbus_message * msg)609 vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg)
610 {
611 vmbus_chanmsg_proc_t msg_proc;
612 uint32_t msg_type;
613
614 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
615 if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) {
616 device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
617 msg_type);
618 return;
619 }
620
621 msg_proc = vmbus_chanmsg_handlers[msg_type];
622 if (msg_proc != NULL)
623 msg_proc(sc, msg);
624
625 /* Channel specific processing */
626 vmbus_chan_msgproc(sc, msg);
627 }
628
629 static void
vmbus_msg_task(void * xsc,int pending __unused)630 vmbus_msg_task(void *xsc, int pending __unused)
631 {
632 struct vmbus_softc *sc = xsc;
633 volatile struct vmbus_message *msg;
634
635 msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE;
636 for (;;) {
637 if (msg->msg_type == HYPERV_MSGTYPE_NONE) {
638 /* No message */
639 break;
640 } else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) {
641 /* Channel message */
642 vmbus_chanmsg_handle(sc,
643 __DEVOLATILE(const struct vmbus_message *, msg));
644 }
645
646 msg->msg_type = HYPERV_MSGTYPE_NONE;
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 static __inline int
vmbus_handle_intr1(struct vmbus_softc * sc,struct trapframe * frame,int cpu)670 vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu)
671 {
672 volatile struct vmbus_message *msg;
673 struct vmbus_message *msg_base;
674
675 msg_base = VMBUS_PCPU_GET(sc, message, cpu);
676
677 /*
678 * Check event timer.
679 *
680 * TODO: move this to independent IDT vector.
681 */
682 msg = msg_base + VMBUS_SINT_TIMER;
683 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
684 msg->msg_type = HYPERV_MSGTYPE_NONE;
685
686 vmbus_et_intr(frame);
687
688 /*
689 * Make sure the write to msg_type (i.e. set to
690 * HYPERV_MSGTYPE_NONE) happens before we read the
691 * msg_flags and EOMing. Otherwise, the EOMing will
692 * not deliver any more messages since there is no
693 * empty slot
694 *
695 * NOTE:
696 * mb() is used here, since atomic_thread_fence_seq_cst()
697 * will become compiler fence on UP kernel.
698 */
699 mb();
700 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
701 /*
702 * This will cause message queue rescan to possibly
703 * deliver another msg from the hypervisor
704 */
705 wrmsr(MSR_HV_EOM, 0);
706 }
707 }
708
709 /*
710 * Check events. Hot path for network and storage I/O data; high rate.
711 *
712 * NOTE:
713 * As recommended by the Windows guest fellows, we check events before
714 * checking messages.
715 */
716 sc->vmbus_event_proc(sc, cpu);
717
718 /*
719 * Check messages. Mainly management stuffs; ultra low rate.
720 */
721 msg = msg_base + VMBUS_SINT_MESSAGE;
722 if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
723 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
724 VMBUS_PCPU_PTR(sc, message_task, cpu));
725 }
726
727 return (FILTER_HANDLED);
728 }
729
730 void
vmbus_handle_intr(struct trapframe * trap_frame)731 vmbus_handle_intr(struct trapframe *trap_frame)
732 {
733 struct vmbus_softc *sc = vmbus_get_softc();
734 int cpu = curcpu;
735
736 /*
737 * Disable preemption.
738 */
739 critical_enter();
740
741 /*
742 * Do a little interrupt counting.
743 */
744 (*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++;
745
746 vmbus_handle_intr1(sc, trap_frame, cpu);
747
748 /*
749 * Enable preemption.
750 */
751 critical_exit();
752 }
753
754 static void
vmbus_synic_setup(void * xsc)755 vmbus_synic_setup(void *xsc)
756 {
757 struct vmbus_softc *sc = xsc;
758 int cpu = curcpu;
759 uint64_t val, orig;
760 uint32_t sint;
761
762 if (hyperv_features & CPUID_HV_MSR_VP_INDEX) {
763 /* Save virtual processor id. */
764 VMBUS_PCPU_GET(sc, vcpuid, cpu) = rdmsr(MSR_HV_VP_INDEX);
765 } else {
766 /* Set virtual processor id to 0 for compatibility. */
767 VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0;
768 }
769
770 /*
771 * Setup the SynIC message.
772 */
773 orig = rdmsr(MSR_HV_SIMP);
774 val = MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK) |
775 ((VMBUS_PCPU_GET(sc, message_dma.hv_paddr, cpu) >> PAGE_SHIFT) <<
776 MSR_HV_SIMP_PGSHIFT);
777 wrmsr(MSR_HV_SIMP, val);
778
779 /*
780 * Setup the SynIC event flags.
781 */
782 orig = rdmsr(MSR_HV_SIEFP);
783 val = MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK) |
784 ((VMBUS_PCPU_GET(sc, event_flags_dma.hv_paddr, cpu)
785 >> PAGE_SHIFT) << MSR_HV_SIEFP_PGSHIFT);
786 wrmsr(MSR_HV_SIEFP, val);
787
788
789 /*
790 * Configure and unmask SINT for message and event flags.
791 */
792 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
793 orig = rdmsr(sint);
794 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
795 (orig & MSR_HV_SINT_RSVD_MASK);
796 wrmsr(sint, val);
797
798 /*
799 * Configure and unmask SINT for timer.
800 */
801 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
802 orig = rdmsr(sint);
803 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
804 (orig & MSR_HV_SINT_RSVD_MASK);
805 wrmsr(sint, val);
806
807 /*
808 * All done; enable SynIC.
809 */
810 orig = rdmsr(MSR_HV_SCONTROL);
811 val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK);
812 wrmsr(MSR_HV_SCONTROL, val);
813 }
814
815 static void
vmbus_synic_teardown(void * arg)816 vmbus_synic_teardown(void *arg)
817 {
818 uint64_t orig;
819 uint32_t sint;
820
821 /*
822 * Disable SynIC.
823 */
824 orig = rdmsr(MSR_HV_SCONTROL);
825 wrmsr(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK));
826
827 /*
828 * Mask message and event flags SINT.
829 */
830 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
831 orig = rdmsr(sint);
832 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
833
834 /*
835 * Mask timer SINT.
836 */
837 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
838 orig = rdmsr(sint);
839 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
840
841 /*
842 * Teardown SynIC message.
843 */
844 orig = rdmsr(MSR_HV_SIMP);
845 wrmsr(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK));
846
847 /*
848 * Teardown SynIC event flags.
849 */
850 orig = rdmsr(MSR_HV_SIEFP);
851 wrmsr(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK));
852 }
853
854 static int
vmbus_dma_alloc(struct vmbus_softc * sc)855 vmbus_dma_alloc(struct vmbus_softc *sc)
856 {
857 bus_dma_tag_t parent_dtag;
858 uint8_t *evtflags;
859 int cpu;
860
861 parent_dtag = bus_get_dma_tag(sc->vmbus_dev);
862 CPU_FOREACH(cpu) {
863 void *ptr;
864
865 /*
866 * Per-cpu messages and event flags.
867 */
868 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
869 PAGE_SIZE, VMBUS_PCPU_PTR(sc, message_dma, cpu),
870 BUS_DMA_WAITOK | BUS_DMA_ZERO);
871 if (ptr == NULL)
872 return ENOMEM;
873 VMBUS_PCPU_GET(sc, message, cpu) = ptr;
874
875 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
876 PAGE_SIZE, VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
877 BUS_DMA_WAITOK | BUS_DMA_ZERO);
878 if (ptr == NULL)
879 return ENOMEM;
880 VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr;
881 }
882
883 evtflags = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
884 PAGE_SIZE, &sc->vmbus_evtflags_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
885 if (evtflags == NULL)
886 return ENOMEM;
887 sc->vmbus_rx_evtflags = (u_long *)evtflags;
888 sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2));
889 sc->vmbus_evtflags = evtflags;
890
891 sc->vmbus_mnf1 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
892 PAGE_SIZE, &sc->vmbus_mnf1_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
893 if (sc->vmbus_mnf1 == NULL)
894 return ENOMEM;
895
896 sc->vmbus_mnf2 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
897 sizeof(struct vmbus_mnf), &sc->vmbus_mnf2_dma,
898 BUS_DMA_WAITOK | BUS_DMA_ZERO);
899 if (sc->vmbus_mnf2 == NULL)
900 return ENOMEM;
901
902 return 0;
903 }
904
905 static void
vmbus_dma_free(struct vmbus_softc * sc)906 vmbus_dma_free(struct vmbus_softc *sc)
907 {
908 int cpu;
909
910 if (sc->vmbus_evtflags != NULL) {
911 hyperv_dmamem_free(&sc->vmbus_evtflags_dma, sc->vmbus_evtflags);
912 sc->vmbus_evtflags = NULL;
913 sc->vmbus_rx_evtflags = NULL;
914 sc->vmbus_tx_evtflags = NULL;
915 }
916 if (sc->vmbus_mnf1 != NULL) {
917 hyperv_dmamem_free(&sc->vmbus_mnf1_dma, sc->vmbus_mnf1);
918 sc->vmbus_mnf1 = NULL;
919 }
920 if (sc->vmbus_mnf2 != NULL) {
921 hyperv_dmamem_free(&sc->vmbus_mnf2_dma, sc->vmbus_mnf2);
922 sc->vmbus_mnf2 = NULL;
923 }
924
925 CPU_FOREACH(cpu) {
926 if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) {
927 hyperv_dmamem_free(
928 VMBUS_PCPU_PTR(sc, message_dma, cpu),
929 VMBUS_PCPU_GET(sc, message, cpu));
930 VMBUS_PCPU_GET(sc, message, cpu) = NULL;
931 }
932 if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) {
933 hyperv_dmamem_free(
934 VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
935 VMBUS_PCPU_GET(sc, event_flags, cpu));
936 VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL;
937 }
938 }
939 }
940
941 static int
vmbus_intr_setup(struct vmbus_softc * sc)942 vmbus_intr_setup(struct vmbus_softc *sc)
943 {
944 int cpu;
945
946 CPU_FOREACH(cpu) {
947 char buf[MAXCOMLEN + 1];
948 cpuset_t cpu_mask;
949
950 /* Allocate an interrupt counter for Hyper-V interrupt */
951 snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu);
952 intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu));
953
954 /*
955 * Setup taskqueue to handle events. Task will be per-
956 * channel.
957 */
958 VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast(
959 "hyperv event", M_WAITOK, taskqueue_thread_enqueue,
960 VMBUS_PCPU_PTR(sc, event_tq, cpu));
961 if (vmbus_pin_evttask) {
962 CPU_SETOF(cpu, &cpu_mask);
963 taskqueue_start_threads_cpuset(
964 VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET,
965 &cpu_mask, "hvevent%d", cpu);
966 } else {
967 taskqueue_start_threads(
968 VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET,
969 "hvevent%d", cpu);
970 }
971
972 /*
973 * Setup tasks and taskqueues to handle messages.
974 */
975 VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast(
976 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
977 VMBUS_PCPU_PTR(sc, message_tq, cpu));
978 CPU_SETOF(cpu, &cpu_mask);
979 taskqueue_start_threads_cpuset(
980 VMBUS_PCPU_PTR(sc, message_tq, cpu), 1, PI_NET, &cpu_mask,
981 "hvmsg%d", cpu);
982 TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0,
983 vmbus_msg_task, sc);
984 }
985
986 #if defined(__amd64__) && defined(KLD_MODULE)
987 pmap_pti_add_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE, true);
988 #endif
989
990 /*
991 * All Hyper-V ISR required resources are setup, now let's find a
992 * free IDT vector for Hyper-V ISR and set it up.
993 */
994 sc->vmbus_idtvec = lapic_ipi_alloc(pti ? IDTVEC(vmbus_isr_pti) :
995 IDTVEC(vmbus_isr));
996 if (sc->vmbus_idtvec < 0) {
997 #if defined(__amd64__) && defined(KLD_MODULE)
998 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
999 #endif
1000 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
1001 return ENXIO;
1002 }
1003 if (bootverbose) {
1004 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
1005 sc->vmbus_idtvec);
1006 }
1007 return 0;
1008 }
1009
1010 static void
vmbus_intr_teardown(struct vmbus_softc * sc)1011 vmbus_intr_teardown(struct vmbus_softc *sc)
1012 {
1013 int cpu;
1014
1015 if (sc->vmbus_idtvec >= 0) {
1016 lapic_ipi_free(sc->vmbus_idtvec);
1017 sc->vmbus_idtvec = -1;
1018 }
1019
1020 #if defined(__amd64__) && defined(KLD_MODULE)
1021 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
1022 #endif
1023
1024 CPU_FOREACH(cpu) {
1025 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
1026 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
1027 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
1028 }
1029 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
1030 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
1031 VMBUS_PCPU_PTR(sc, message_task, cpu));
1032 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
1033 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
1034 }
1035 }
1036 }
1037
1038 static int
vmbus_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)1039 vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
1040 {
1041 return (ENOENT);
1042 }
1043
1044 static int
vmbus_child_pnpinfo_str(device_t dev,device_t child,char * buf,size_t buflen)1045 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
1046 {
1047 const struct vmbus_channel *chan;
1048 char guidbuf[HYPERV_GUID_STRLEN];
1049
1050 chan = vmbus_get_channel(child);
1051 if (chan == NULL) {
1052 /* Event timer device, which does not belong to a channel */
1053 return (0);
1054 }
1055
1056 strlcat(buf, "classid=", buflen);
1057 hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf));
1058 strlcat(buf, guidbuf, buflen);
1059
1060 strlcat(buf, " deviceid=", buflen);
1061 hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf));
1062 strlcat(buf, guidbuf, buflen);
1063
1064 return (0);
1065 }
1066
1067 int
vmbus_add_child(struct vmbus_channel * chan)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
vmbus_delete_child(struct vmbus_channel * chan)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
vmbus_sysctl_version(SYSCTL_HANDLER_ARGS)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 *
vmbus_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)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
vmbus_alloc_msi(device_t bus,device_t dev,int count,int maxcount,int * irqs)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
vmbus_release_msi(device_t bus,device_t dev,int count,int * irqs)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
vmbus_alloc_msix(device_t bus,device_t dev,int * irq)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
vmbus_release_msix(device_t bus,device_t dev,int irq)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
vmbus_map_msi(device_t bus,device_t dev,int irq,uint64_t * addr,uint32_t * data)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
vmbus_get_version_method(device_t bus,device_t dev)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
vmbus_probe_guid_method(device_t bus,device_t dev,const struct hyperv_guid * guid)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
vmbus_get_vcpu_id_method(device_t bus,device_t dev,int cpu)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 *
vmbus_get_eventtq_method(device_t bus,device_t dev __unused,int cpu)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
parse_crs(ACPI_RESOURCE * res,void * ctx)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
vmbus_get_crs(device_t dev,device_t vmbus_dev,enum parse_pass pass)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
vmbus_get_mmio_res_pass(device_t dev,enum parse_pass pass)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
vmbus_get_mmio_res(device_t dev)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
vmbus_fb_mmio_res(device_t dev)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
vmbus_free_mmio_res(device_t dev)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
vmbus_identify(driver_t * driver,device_t parent)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
vmbus_probe(device_t dev)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
vmbus_doattach(struct vmbus_softc * sc)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
vmbus_event_proc_dummy(struct vmbus_softc * sc __unused,int cpu __unused)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
vmbus_intrhook(void * xsc)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
vmbus_attach(device_t dev)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
vmbus_detach(device_t dev)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
vmbus_sysinit(void * arg __unused)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