Home | History | Annotate | Line # | Download | only in hyperv
vmbus.c revision 1.2.2.6
      1 /*	$NetBSD: vmbus.c,v 1.2.2.6 2021/01/29 18:18:25 martin Exp $	*/
      2 /*	$OpenBSD: hyperv.c,v 1.43 2017/06/27 13:56:15 mikeb Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2009-2012 Microsoft Corp.
      6  * Copyright (c) 2012 NetApp Inc.
      7  * Copyright (c) 2012 Citrix Inc.
      8  * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice unmodified, this list of conditions, and the following
     16  *    disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * The OpenBSD port was done under funding by Esdenera Networks GmbH.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: vmbus.c,v 1.2.2.6 2021/01/29 18:18:25 martin Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/atomic.h>
     44 #include <sys/bitops.h>
     45 #include <sys/bus.h>
     46 #include <sys/cpu.h>
     47 #include <sys/intr.h>
     48 #include <sys/kmem.h>
     49 #include <sys/module.h>
     50 #include <sys/mutex.h>
     51 #include <sys/xcall.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <dev/hyperv/vmbusvar.h>
     56 
     57 #define VMBUS_GPADL_START		0xffff /* 0x10000 effectively */
     58 
     59 /* Command submission flags */
     60 #define HCF_SLEEPOK	0x0000
     61 #define HCF_NOSLEEP	0x0002	/* M_NOWAIT */
     62 #define HCF_NOREPLY	0x0004
     63 
     64 static void	vmbus_attach_deferred(device_t);
     65 static int	vmbus_alloc_dma(struct vmbus_softc *);
     66 static void	vmbus_free_dma(struct vmbus_softc *);
     67 static int	vmbus_init_interrupts(struct vmbus_softc *);
     68 static void	vmbus_deinit_interrupts(struct vmbus_softc *);
     69 static void	vmbus_init_synic(void *, void *);
     70 static void	vmbus_deinit_synic(void *, void *);
     71 
     72 static int	vmbus_connect(struct vmbus_softc *);
     73 static int	vmbus_cmd(struct vmbus_softc *, void *, size_t, void *, size_t,
     74 		    int);
     75 static int	vmbus_start(struct vmbus_softc *, struct vmbus_msg *, paddr_t);
     76 static int	vmbus_reply(struct vmbus_softc *, struct vmbus_msg *);
     77 static void	vmbus_wait(struct vmbus_softc *,
     78 		    int (*done)(struct vmbus_softc *, struct vmbus_msg *),
     79 		    struct vmbus_msg *, void *, const char *);
     80 static uint16_t vmbus_intr_signal(struct vmbus_softc *, paddr_t);
     81 static void	vmbus_event_proc(void *, struct cpu_info *);
     82 static void	vmbus_event_proc_compat(void *, struct cpu_info *);
     83 static void	vmbus_message_proc(void *, struct cpu_info *);
     84 static void	vmbus_message_softintr(void *);
     85 static void	vmbus_channel_response(struct vmbus_softc *,
     86 		    struct vmbus_chanmsg_hdr *);
     87 static void	vmbus_channel_offer(struct vmbus_softc *,
     88 		    struct vmbus_chanmsg_hdr *);
     89 static void	vmbus_channel_rescind(struct vmbus_softc *,
     90 		    struct vmbus_chanmsg_hdr *);
     91 static void	vmbus_channel_delivered(struct vmbus_softc *,
     92 		    struct vmbus_chanmsg_hdr *);
     93 static int	vmbus_channel_scan(struct vmbus_softc *);
     94 static void	vmbus_channel_cpu_default(struct vmbus_channel *);
     95 static void	vmbus_process_offer(struct vmbus_softc *, struct vmbus_offer *);
     96 static struct vmbus_channel *
     97 		vmbus_channel_lookup(struct vmbus_softc *, uint32_t);
     98 static int	vmbus_channel_ring_create(struct vmbus_channel *, uint32_t);
     99 static void	vmbus_channel_ring_destroy(struct vmbus_channel *);
    100 static void	vmbus_channel_pause(struct vmbus_channel *);
    101 static uint32_t	vmbus_channel_unpause(struct vmbus_channel *);
    102 static uint32_t	vmbus_channel_ready(struct vmbus_channel *);
    103 static int	vmbus_attach_icdevs(struct vmbus_softc *);
    104 static int	vmbus_attach_devices(struct vmbus_softc *);
    105 
    106 static struct vmbus_softc *vmbus_sc;
    107 
    108 static const struct {
    109 	int	hmd_response;
    110 	int	hmd_request;
    111 	void	(*hmd_handler)(struct vmbus_softc *,
    112 		    struct vmbus_chanmsg_hdr *);
    113 } vmbus_msg_dispatch[] = {
    114 	{ 0,					0, NULL },
    115 	{ VMBUS_CHANMSG_CHOFFER,		0, vmbus_channel_offer },
    116 	{ VMBUS_CHANMSG_CHRESCIND,		0, vmbus_channel_rescind },
    117 	{ VMBUS_CHANMSG_CHREQUEST,		VMBUS_CHANMSG_CHOFFER, NULL },
    118 	{ VMBUS_CHANMSG_CHOFFER_DONE,		0, vmbus_channel_delivered },
    119 	{ VMBUS_CHANMSG_CHOPEN,			0, NULL },
    120 	{ VMBUS_CHANMSG_CHOPEN_RESP,		VMBUS_CHANMSG_CHOPEN,
    121 	  vmbus_channel_response },
    122 	{ VMBUS_CHANMSG_CHCLOSE,		0, NULL },
    123 	{ VMBUS_CHANMSG_GPADL_CONN,		0, NULL },
    124 	{ VMBUS_CHANMSG_GPADL_SUBCONN,		0, NULL },
    125 	{ VMBUS_CHANMSG_GPADL_CONNRESP,		VMBUS_CHANMSG_GPADL_CONN,
    126 	  vmbus_channel_response },
    127 	{ VMBUS_CHANMSG_GPADL_DISCONN,		0, NULL },
    128 	{ VMBUS_CHANMSG_GPADL_DISCONNRESP,	VMBUS_CHANMSG_GPADL_DISCONN,
    129 	  vmbus_channel_response },
    130 	{ VMBUS_CHANMSG_CHFREE,			0, NULL },
    131 	{ VMBUS_CHANMSG_CONNECT,		0, NULL },
    132 	{ VMBUS_CHANMSG_CONNECT_RESP,		VMBUS_CHANMSG_CONNECT,
    133 	  vmbus_channel_response },
    134 	{ VMBUS_CHANMSG_DISCONNECT,		0, NULL },
    135 };
    136 
    137 const struct hyperv_guid hyperv_guid_network = {
    138 	{ 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46,
    139 	  0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e }
    140 };
    141 
    142 const struct hyperv_guid hyperv_guid_ide = {
    143 	{ 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
    144 	  0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 }
    145 };
    146 
    147 const struct hyperv_guid hyperv_guid_scsi = {
    148 	{ 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
    149 	  0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f }
    150 };
    151 
    152 const struct hyperv_guid hyperv_guid_shutdown = {
    153 	{ 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
    154 	  0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb }
    155 };
    156 
    157 const struct hyperv_guid hyperv_guid_timesync = {
    158 	{ 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
    159 	  0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf }
    160 };
    161 
    162 const struct hyperv_guid hyperv_guid_heartbeat = {
    163 	{ 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
    164 	  0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d }
    165 };
    166 
    167 const struct hyperv_guid hyperv_guid_kvp = {
    168 	{ 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
    169 	  0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6 }
    170 };
    171 
    172 const struct hyperv_guid hyperv_guid_vss = {
    173 	{ 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42,
    174 	  0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 }
    175 };
    176 
    177 const struct hyperv_guid hyperv_guid_dynmem = {
    178 	{ 0xdc, 0x74, 0x50, 0x52, 0x85, 0x89, 0xe2, 0x46,
    179 	  0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 }
    180 };
    181 
    182 const struct hyperv_guid hyperv_guid_mouse = {
    183 	{ 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c,
    184 	  0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a }
    185 };
    186 
    187 const struct hyperv_guid hyperv_guid_kbd = {
    188 	{ 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
    189 	  0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84 }
    190 };
    191 
    192 const struct hyperv_guid hyperv_guid_video = {
    193 	{ 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a,
    194 	  0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 }
    195 };
    196 
    197 const struct hyperv_guid hyperv_guid_fc = {
    198 	{ 0x4a, 0xcc, 0x9b, 0x2f, 0x69, 0x00, 0xf3, 0x4a,
    199 	  0xb7, 0x6b, 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda }
    200 };
    201 
    202 const struct hyperv_guid hyperv_guid_fcopy = {
    203 	{ 0xe3, 0x4b, 0xd1, 0x34, 0xe4, 0xde, 0xc8, 0x41,
    204 	  0x9a, 0xe7, 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92 }
    205 };
    206 
    207 const struct hyperv_guid hyperv_guid_pcie = {
    208 	{ 0x1d, 0xf6, 0xc4, 0x44, 0x44, 0x44, 0x00, 0x44,
    209 	  0x9d, 0x52, 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f }
    210 };
    211 
    212 const struct hyperv_guid hyperv_guid_netdir = {
    213 	{ 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b,
    214 	  0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 }
    215 };
    216 
    217 const struct hyperv_guid hyperv_guid_rdesktop = {
    218 	{ 0xf4, 0xac, 0x6a, 0x27, 0x15, 0xac, 0x6c, 0x42,
    219 	  0x98, 0xdd, 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe }
    220 };
    221 
    222 /* Automatic Virtual Machine Activation (AVMA) Services */
    223 const struct hyperv_guid hyperv_guid_avma1 = {
    224 	{ 0x55, 0xb2, 0x87, 0x44, 0x8c, 0xb8, 0x3f, 0x40,
    225 	  0xbb, 0x51, 0xd1, 0xf6, 0x9c, 0xf1, 0x7f, 0x87 }
    226 };
    227 
    228 const struct hyperv_guid hyperv_guid_avma2 = {
    229 	{ 0xf4, 0xba, 0x75, 0x33, 0x15, 0x9e, 0x30, 0x4b,
    230 	  0xb7, 0x65, 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b }
    231 };
    232 
    233 const struct hyperv_guid hyperv_guid_avma3 = {
    234 	{ 0xa0, 0x1f, 0x22, 0x99, 0xad, 0x24, 0xe2, 0x11,
    235 	  0xbe, 0x98, 0x00, 0x1a, 0xa0, 0x1b, 0xbf, 0x6e }
    236 };
    237 
    238 const struct hyperv_guid hyperv_guid_avma4 = {
    239 	{ 0x16, 0x57, 0xe6, 0xf8, 0xb3, 0x3c, 0x06, 0x4a,
    240 	  0x9a, 0x60, 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5 }
    241 };
    242 
    243 int
    244 vmbus_match(device_t parent, cfdata_t cf, void *aux)
    245 {
    246 
    247 	if (cf->cf_unit != 0 ||
    248 	    !hyperv_hypercall_enabled() ||
    249 	    !hyperv_synic_supported())
    250 		return 0;
    251 
    252 	return 1;
    253 }
    254 
    255 int
    256 vmbus_attach(struct vmbus_softc *sc)
    257 {
    258 
    259 	aprint_naive("\n");
    260 	aprint_normal(": Hyper-V VMBus\n");
    261 
    262 	vmbus_sc = sc;
    263 
    264 	sc->sc_msgpool = pool_cache_init(sizeof(struct vmbus_msg), 8, 0, 0,
    265 	    "hvmsg", NULL, IPL_NET, NULL, NULL, NULL);
    266 	hyperv_set_message_proc(vmbus_message_proc, sc);
    267 
    268 	if (vmbus_alloc_dma(sc))
    269 		goto cleanup;
    270 
    271 	if (vmbus_init_interrupts(sc))
    272 		goto cleanup;
    273 
    274 	if (vmbus_connect(sc))
    275 		goto cleanup;
    276 
    277 	aprint_normal_dev(sc->sc_dev, "protocol %d.%d\n",
    278 	    VMBUS_VERSION_MAJOR(sc->sc_proto),
    279 	    VMBUS_VERSION_MINOR(sc->sc_proto));
    280 
    281 	if (sc->sc_proto == VMBUS_VERSION_WS2008 ||
    282 	    sc->sc_proto == VMBUS_VERSION_WIN7) {
    283 		hyperv_set_event_proc(vmbus_event_proc_compat, sc);
    284 		sc->sc_channel_max = VMBUS_CHAN_MAX_COMPAT;
    285 	} else {
    286 		hyperv_set_event_proc(vmbus_event_proc, sc);
    287 		sc->sc_channel_max = VMBUS_CHAN_MAX;
    288 	}
    289 
    290 	if (vmbus_channel_scan(sc))
    291 		goto cleanup;
    292 
    293 	/* Attach heartbeat, KVP and other "internal" services */
    294 	vmbus_attach_icdevs(sc);
    295 
    296 	/* Attach devices with external drivers */
    297 	vmbus_attach_devices(sc);
    298 
    299 	config_interrupts(sc->sc_dev, vmbus_attach_deferred);
    300 
    301 	return 0;
    302 
    303 cleanup:
    304 	vmbus_deinit_interrupts(sc);
    305 	vmbus_free_dma(sc);
    306 	return -1;
    307 }
    308 
    309 static void
    310 vmbus_attach_deferred(device_t self)
    311 {
    312 	struct vmbus_softc *sc = device_private(self);
    313 
    314 	xc_wait(xc_broadcast(0, vmbus_init_synic, sc, NULL));
    315 }
    316 
    317 int
    318 vmbus_detach(struct vmbus_softc *sc, int flags)
    319 {
    320 
    321 	vmbus_deinit_interrupts(sc);
    322 	vmbus_free_dma(sc);
    323 
    324 	return 0;
    325 }
    326 
    327 static int
    328 vmbus_alloc_dma(struct vmbus_softc *sc)
    329 {
    330 	CPU_INFO_ITERATOR cii;
    331 	struct cpu_info *ci;
    332 	struct vmbus_percpu_data *pd;
    333 	int i;
    334 
    335 	/*
    336 	 * Per-CPU messages and event flags.
    337 	 */
    338 	for (CPU_INFO_FOREACH(cii, ci)) {
    339 		pd = &sc->sc_percpu[cpu_index(ci)];
    340 
    341 		pd->simp = hyperv_dma_alloc(sc->sc_dmat, &pd->simp_dma,
    342 		    PAGE_SIZE, PAGE_SIZE, 0, 1);
    343 		if (pd->simp == NULL)
    344 			return ENOMEM;
    345 
    346 		pd->siep = hyperv_dma_alloc(sc->sc_dmat, &pd->siep_dma,
    347 		    PAGE_SIZE, PAGE_SIZE, 0, 1);
    348 		if (pd->siep == NULL)
    349 			return ENOMEM;
    350 	}
    351 
    352 	sc->sc_events = hyperv_dma_alloc(sc->sc_dmat, &sc->sc_events_dma,
    353 	    PAGE_SIZE, PAGE_SIZE, 0, 1);
    354 	if (sc->sc_events == NULL)
    355 		return ENOMEM;
    356 	sc->sc_wevents = (u_long *)sc->sc_events;
    357 	sc->sc_revents = (u_long *)((uint8_t *)sc->sc_events + (PAGE_SIZE / 2));
    358 
    359 	for (i = 0; i < __arraycount(sc->sc_monitor); i++) {
    360 		sc->sc_monitor[i] = hyperv_dma_alloc(sc->sc_dmat,
    361 		    &sc->sc_monitor_dma[i], PAGE_SIZE, PAGE_SIZE, 0, 1);
    362 		if (sc->sc_monitor[i] == NULL)
    363 			return ENOMEM;
    364 	}
    365 
    366 	return 0;
    367 }
    368 
    369 static void
    370 vmbus_free_dma(struct vmbus_softc *sc)
    371 {
    372 	CPU_INFO_ITERATOR cii;
    373 	struct cpu_info *ci;
    374 	int i;
    375 
    376 	if (sc->sc_events != NULL) {
    377 		sc->sc_events = sc->sc_wevents = sc->sc_revents = NULL;
    378 		hyperv_dma_free(sc->sc_dmat, &sc->sc_events_dma);
    379 	}
    380 
    381 	for (i = 0; i < __arraycount(sc->sc_monitor); i++) {
    382 		sc->sc_monitor[i] = NULL;
    383 		hyperv_dma_free(sc->sc_dmat, &sc->sc_monitor_dma[i]);
    384 	}
    385 
    386 	for (CPU_INFO_FOREACH(cii, ci)) {
    387 		struct vmbus_percpu_data *pd = &sc->sc_percpu[cpu_index(ci)];
    388 
    389 		if (pd->simp != NULL) {
    390 			pd->simp = NULL;
    391 			hyperv_dma_free(sc->sc_dmat, &pd->simp_dma);
    392 		}
    393 		if (pd->siep != NULL) {
    394 			pd->siep = NULL;
    395 			hyperv_dma_free(sc->sc_dmat, &pd->siep_dma);
    396 		}
    397 	}
    398 }
    399 
    400 static int
    401 vmbus_init_interrupts(struct vmbus_softc *sc)
    402 {
    403 
    404 	TAILQ_INIT(&sc->sc_reqs);
    405 	mutex_init(&sc->sc_req_lock, MUTEX_DEFAULT, IPL_NET);
    406 
    407 	TAILQ_INIT(&sc->sc_rsps);
    408 	mutex_init(&sc->sc_rsp_lock, MUTEX_DEFAULT, IPL_NET);
    409 
    410 	sc->sc_proto = VMBUS_VERSION_WS2008;
    411 
    412 	/* XXX event_tq */
    413 
    414 	sc->sc_msg_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    415 	    vmbus_message_softintr, sc);
    416 	if (sc->sc_msg_sih == NULL)
    417 		return -1;
    418 
    419 	vmbus_init_interrupts_md(sc);
    420 
    421 	kcpuset_create(&sc->sc_intr_cpuset, true);
    422 	if (cold) {
    423 		/* Initialize other CPUs later. */
    424 		vmbus_init_synic(sc, NULL);
    425 	} else
    426 		xc_wait(xc_broadcast(0, vmbus_init_synic, sc, NULL));
    427 	atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_SYNIC);
    428 
    429 	return 0;
    430 }
    431 
    432 static void
    433 vmbus_deinit_interrupts(struct vmbus_softc *sc)
    434 {
    435 
    436 	if (ISSET(sc->sc_flags, VMBUS_SCFLAG_SYNIC)) {
    437 		if (cold)
    438 			vmbus_deinit_synic(sc, NULL);
    439 		else
    440 			xc_wait(xc_broadcast(0, vmbus_deinit_synic, sc, NULL));
    441 		atomic_and_32(&sc->sc_flags, (uint32_t)~VMBUS_SCFLAG_SYNIC);
    442 	}
    443 
    444 	/* XXX event_tq */
    445 
    446 	if (sc->sc_msg_sih != NULL) {
    447 		softint_disestablish(sc->sc_msg_sih);
    448 		sc->sc_msg_sih = NULL;
    449 	}
    450 
    451 	vmbus_deinit_interrupts_md(sc);
    452 }
    453 
    454 static void
    455 vmbus_init_synic(void *arg1, void *arg2)
    456 {
    457 	struct vmbus_softc *sc = arg1;
    458 	cpuid_t cpu;
    459 	int s;
    460 
    461 	s = splhigh();
    462 
    463 	cpu = cpu_index(curcpu());
    464 	if (!kcpuset_isset(sc->sc_intr_cpuset, cpu)) {
    465 		kcpuset_atomic_set(sc->sc_intr_cpuset, cpu);
    466 		vmbus_init_synic_md(sc, cpu);
    467 	}
    468 
    469 	splx(s);
    470 }
    471 
    472 static void
    473 vmbus_deinit_synic(void *arg1, void *arg2)
    474 {
    475 	struct vmbus_softc *sc = arg1;
    476 	cpuid_t cpu;
    477 	int s;
    478 
    479 	s = splhigh();
    480 
    481 	cpu = cpu_index(curcpu());
    482 	if (kcpuset_isset(sc->sc_intr_cpuset, cpu)) {
    483 		vmbus_deinit_synic_md(sc, cpu);
    484 		kcpuset_atomic_clear(sc->sc_intr_cpuset, cpu);
    485 	}
    486 
    487 	splx(s);
    488 }
    489 
    490 static int
    491 vmbus_connect(struct vmbus_softc *sc)
    492 {
    493 	static const uint32_t versions[] = {
    494 		VMBUS_VERSION_WIN8_1,
    495 		VMBUS_VERSION_WIN8,
    496 		VMBUS_VERSION_WIN7,
    497 		VMBUS_VERSION_WS2008
    498 	};
    499 	struct vmbus_chanmsg_connect cmd;
    500 	struct vmbus_chanmsg_connect_resp rsp;
    501 	int i, rv;
    502 
    503 	memset(&cmd, 0, sizeof(cmd));
    504 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CONNECT;
    505 	cmd.chm_evtflags = hyperv_dma_get_paddr(&sc->sc_events_dma);
    506 	cmd.chm_mnf1 = hyperv_dma_get_paddr(&sc->sc_monitor_dma[0]);
    507 	cmd.chm_mnf2 = hyperv_dma_get_paddr(&sc->sc_monitor_dma[1]);
    508 
    509 	memset(&rsp, 0, sizeof(rsp));
    510 
    511 	for (i = 0; i < __arraycount(versions); i++) {
    512 		cmd.chm_ver = versions[i];
    513 		rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
    514 		    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
    515 		if (rv) {
    516 			DPRINTF("%s: CONNECT failed\n",
    517 			    device_xname(sc->sc_dev));
    518 			return rv;
    519 		}
    520 		if (rsp.chm_done) {
    521 			atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_CONNECTED);
    522 			sc->sc_proto = versions[i];
    523 			sc->sc_handle = VMBUS_GPADL_START;
    524 			break;
    525 		}
    526 	}
    527 	if (i == __arraycount(versions)) {
    528 		device_printf(sc->sc_dev,
    529 		    "failed to negotiate protocol version\n");
    530 		return ENXIO;
    531 	}
    532 
    533 	return 0;
    534 }
    535 
    536 static int
    537 vmbus_cmd(struct vmbus_softc *sc, void *cmd, size_t cmdlen, void *rsp,
    538     size_t rsplen, int flags)
    539 {
    540 	const int prflags = cold ? PR_NOWAIT : PR_WAITOK;
    541 	struct vmbus_msg *msg;
    542 	paddr_t pa;
    543 	int rv;
    544 
    545 	if (cmdlen > VMBUS_MSG_DSIZE_MAX) {
    546 		device_printf(sc->sc_dev, "payload too large (%zu)\n",
    547 		    cmdlen);
    548 		return EMSGSIZE;
    549 	}
    550 
    551 	msg = pool_cache_get_paddr(sc->sc_msgpool, prflags, &pa);
    552 	if (msg == NULL) {
    553 		device_printf(sc->sc_dev, "couldn't get msgpool\n");
    554 		return ENOMEM;
    555 	}
    556 	memset(msg, 0, sizeof(*msg));
    557 	msg->msg_req.hc_dsize = cmdlen;
    558 	memcpy(msg->msg_req.hc_data, cmd, cmdlen);
    559 
    560 	if (!(flags & HCF_NOREPLY)) {
    561 		msg->msg_rsp = rsp;
    562 		msg->msg_rsplen = rsplen;
    563 	} else
    564 		msg->msg_flags |= MSGF_NOQUEUE;
    565 
    566 	if (flags & HCF_NOSLEEP)
    567 		msg->msg_flags |= MSGF_NOSLEEP;
    568 
    569 	rv = vmbus_start(sc, msg, pa);
    570 	if (rv == 0)
    571 		rv = vmbus_reply(sc, msg);
    572 	pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
    573 	return rv;
    574 }
    575 
    576 static int
    577 vmbus_start(struct vmbus_softc *sc, struct vmbus_msg *msg, paddr_t msg_pa)
    578 {
    579 	static const int delays[] = {
    580 		100, 100, 100, 500, 500, 5000, 5000, 5000
    581 	};
    582 	const char *wchan = "hvstart";
    583 	uint16_t status;
    584 	int i, s;
    585 
    586 	msg->msg_req.hc_connid = VMBUS_CONNID_MESSAGE;
    587 	msg->msg_req.hc_msgtype = 1;
    588 
    589 	if (!(msg->msg_flags & MSGF_NOQUEUE)) {
    590 		mutex_enter(&sc->sc_req_lock);
    591 		TAILQ_INSERT_TAIL(&sc->sc_reqs, msg, msg_entry);
    592 		mutex_exit(&sc->sc_req_lock);
    593 	}
    594 
    595 	for (i = 0; i < __arraycount(delays); i++) {
    596 		status = hyperv_hypercall_post_message(
    597 		    msg_pa + offsetof(struct vmbus_msg, msg_req));
    598 		if (status == HYPERCALL_STATUS_SUCCESS)
    599 			break;
    600 
    601 		if (msg->msg_flags & MSGF_NOSLEEP) {
    602 			delay(delays[i]);
    603 			s = splnet();
    604 			hyperv_intr();
    605 			splx(s);
    606 		} else
    607 			tsleep(wchan, PRIBIO, wchan,
    608 			    max(1, mstohz(delays[i] / 1000)));
    609 	}
    610 	if (status != HYPERCALL_STATUS_SUCCESS) {
    611 		device_printf(sc->sc_dev,
    612 		    "posting vmbus message failed with %d\n", status);
    613 		if (!(msg->msg_flags & MSGF_NOQUEUE)) {
    614 			mutex_enter(&sc->sc_req_lock);
    615 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
    616 			mutex_exit(&sc->sc_req_lock);
    617 		}
    618 		return EIO;
    619 	}
    620 
    621 	return 0;
    622 }
    623 
    624 static int
    625 vmbus_reply_done(struct vmbus_softc *sc, struct vmbus_msg *msg)
    626 {
    627 	struct vmbus_msg *m;
    628 
    629 	mutex_enter(&sc->sc_rsp_lock);
    630 	TAILQ_FOREACH(m, &sc->sc_rsps, msg_entry) {
    631 		if (m == msg) {
    632 			mutex_exit(&sc->sc_rsp_lock);
    633 			return 1;
    634 		}
    635 	}
    636 	mutex_exit(&sc->sc_rsp_lock);
    637 	return 0;
    638 }
    639 
    640 static int
    641 vmbus_reply(struct vmbus_softc *sc, struct vmbus_msg *msg)
    642 {
    643 
    644 	if (msg->msg_flags & MSGF_NOQUEUE)
    645 		return 0;
    646 
    647 	vmbus_wait(sc, vmbus_reply_done, msg, msg, "hvreply");
    648 
    649 	mutex_enter(&sc->sc_rsp_lock);
    650 	TAILQ_REMOVE(&sc->sc_rsps, msg, msg_entry);
    651 	mutex_exit(&sc->sc_rsp_lock);
    652 
    653 	return 0;
    654 }
    655 
    656 static void
    657 vmbus_wait(struct vmbus_softc *sc,
    658     int (*cond)(struct vmbus_softc *, struct vmbus_msg *),
    659     struct vmbus_msg *msg, void *wchan, const char *wmsg)
    660 {
    661 	int s;
    662 
    663 	while (!cond(sc, msg)) {
    664 		if (msg->msg_flags & MSGF_NOSLEEP) {
    665 			delay(1000);
    666 			s = splnet();
    667 			hyperv_intr();
    668 			splx(s);
    669 		} else
    670 			tsleep(wchan, PRIBIO, wmsg ? wmsg : "hvwait",
    671 			    max(1, mstohz(1)));
    672 	}
    673 }
    674 
    675 static uint16_t
    676 vmbus_intr_signal(struct vmbus_softc *sc, paddr_t con_pa)
    677 {
    678 	uint64_t status;
    679 
    680 	status = hyperv_hypercall_signal_event(con_pa);
    681 	return (uint16_t)status;
    682 }
    683 
    684 #if LONG_BIT == 64
    685 #define ffsl(v)	ffs64(v)
    686 #elif LONG_BIT == 32
    687 #define ffsl(v)	ffs32(v)
    688 #else
    689 #error unsupport LONG_BIT
    690 #endif	/* LONG_BIT */
    691 
    692 static void
    693 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *revents,
    694     int maxrow)
    695 {
    696 	struct vmbus_channel *ch;
    697 	u_long pending;
    698 	uint32_t chanid, chanid_base;
    699 	int row, chanid_ofs;
    700 
    701 	for (row = 0; row < maxrow; row++) {
    702 		if (revents[row] == 0)
    703 			continue;
    704 
    705 		pending = atomic_swap_ulong(&revents[row], 0);
    706 		chanid_base = row * LONG_BIT;
    707 
    708 		while ((chanid_ofs = ffsl(pending)) != 0) {
    709 			chanid_ofs--;	/* NOTE: ffs is 1-based */
    710 			pending &= ~(1UL << chanid_ofs);
    711 
    712 			chanid = chanid_base + chanid_ofs;
    713 			/* vmbus channel protocol message */
    714 			if (chanid == 0)
    715 				continue;
    716 
    717 			ch = vmbus_channel_lookup(sc, chanid);
    718 			if (ch == NULL) {
    719 				device_printf(sc->sc_dev,
    720 				    "unhandled event on %d\n", chanid);
    721 				continue;
    722 			}
    723 			if (ch->ch_state != VMBUS_CHANSTATE_OPENED) {
    724 				device_printf(sc->sc_dev,
    725 				    "channel %d is not active\n", chanid);
    726 				continue;
    727 			}
    728 			ch->ch_evcnt.ev_count++;
    729 			vmbus_channel_schedule(ch);
    730 		}
    731 	}
    732 }
    733 
    734 static void
    735 vmbus_event_proc(void *arg, struct cpu_info *ci)
    736 {
    737 	struct vmbus_softc *sc = arg;
    738 	struct vmbus_evtflags *evt;
    739 
    740 	/*
    741 	 * On Host with Win8 or above, the event page can be
    742 	 * checked directly to get the id of the channel
    743 	 * that has the pending interrupt.
    744 	 */
    745 	evt = (struct vmbus_evtflags *)sc->sc_percpu[cpu_index(ci)].siep +
    746 	    VMBUS_SINT_MESSAGE;
    747 
    748 	vmbus_event_flags_proc(sc, evt->evt_flags,
    749 	    __arraycount(evt->evt_flags));
    750 }
    751 
    752 static void
    753 vmbus_event_proc_compat(void *arg, struct cpu_info *ci)
    754 {
    755 	struct vmbus_softc *sc = arg;
    756 	struct vmbus_evtflags *evt;
    757 
    758 	evt = (struct vmbus_evtflags *)sc->sc_percpu[cpu_index(ci)].siep +
    759 	    VMBUS_SINT_MESSAGE;
    760 
    761 	if (test_bit(0, &evt->evt_flags[0])) {
    762 		clear_bit(0, &evt->evt_flags[0]);
    763 		/*
    764 		 * receive size is 1/2 page and divide that by 4 bytes
    765 		 */
    766 		vmbus_event_flags_proc(sc, sc->sc_revents,
    767 		    VMBUS_CHAN_MAX_COMPAT / VMBUS_EVTFLAG_LEN);
    768 	}
    769 }
    770 
    771 static void
    772 vmbus_message_proc(void *arg, struct cpu_info *ci)
    773 {
    774 	struct vmbus_softc *sc = arg;
    775 	struct vmbus_message *msg;
    776 
    777 	msg = (struct vmbus_message *)sc->sc_percpu[cpu_index(ci)].simp +
    778 	    VMBUS_SINT_MESSAGE;
    779 	if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
    780 		if (__predict_true(!cold))
    781 			softint_schedule_cpu(sc->sc_msg_sih, ci);
    782 		else
    783 			vmbus_message_softintr(sc);
    784 	}
    785 }
    786 
    787 static void
    788 vmbus_message_softintr(void *arg)
    789 {
    790 	struct vmbus_softc *sc = arg;
    791 	struct vmbus_message *msg;
    792 	struct vmbus_chanmsg_hdr *hdr;
    793 	uint32_t type;
    794 	cpuid_t cpu;
    795 
    796 	cpu = cpu_index(curcpu());
    797 
    798 	for (;;) {
    799 		msg = (struct vmbus_message *)sc->sc_percpu[cpu].simp +
    800 		    VMBUS_SINT_MESSAGE;
    801 		if (msg->msg_type == HYPERV_MSGTYPE_NONE)
    802 			break;
    803 
    804 		hdr = (struct vmbus_chanmsg_hdr *)msg->msg_data;
    805 		type = hdr->chm_type;
    806 		if (type >= VMBUS_CHANMSG_COUNT) {
    807 			device_printf(sc->sc_dev,
    808 			    "unhandled message type %u flags %#x\n", type,
    809 			    msg->msg_flags);
    810 		} else {
    811 			if (vmbus_msg_dispatch[type].hmd_handler) {
    812 				vmbus_msg_dispatch[type].hmd_handler(sc, hdr);
    813 			} else {
    814 				device_printf(sc->sc_dev,
    815 				    "unhandled message type %u\n", type);
    816 			}
    817 		}
    818 
    819 		msg->msg_type = HYPERV_MSGTYPE_NONE;
    820 		membar_sync();
    821 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING)
    822 			hyperv_send_eom();
    823 	}
    824 }
    825 
    826 static void
    827 vmbus_channel_response(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *rsphdr)
    828 {
    829 	struct vmbus_msg *msg;
    830 	struct vmbus_chanmsg_hdr *reqhdr;
    831 	int req;
    832 
    833 	req = vmbus_msg_dispatch[rsphdr->chm_type].hmd_request;
    834 	mutex_enter(&sc->sc_req_lock);
    835 	TAILQ_FOREACH(msg, &sc->sc_reqs, msg_entry) {
    836 		reqhdr = (struct vmbus_chanmsg_hdr *)&msg->msg_req.hc_data;
    837 		if (reqhdr->chm_type == req) {
    838 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
    839 			break;
    840 		}
    841 	}
    842 	mutex_exit(&sc->sc_req_lock);
    843 	if (msg != NULL) {
    844 		memcpy(msg->msg_rsp, rsphdr, msg->msg_rsplen);
    845 		mutex_enter(&sc->sc_rsp_lock);
    846 		TAILQ_INSERT_TAIL(&sc->sc_rsps, msg, msg_entry);
    847 		mutex_exit(&sc->sc_rsp_lock);
    848 		wakeup(msg);
    849 	}
    850 }
    851 
    852 static void
    853 vmbus_channel_offer(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    854 {
    855 	struct vmbus_offer *co;
    856 
    857 	co = kmem_intr_zalloc(sizeof(*co), KM_NOSLEEP);
    858 	if (co == NULL) {
    859 		device_printf(sc->sc_dev, "couldn't allocate offer\n");
    860 		return;
    861 	}
    862 
    863 	memcpy(&co->co_chan, hdr, sizeof(co->co_chan));
    864 
    865 	mutex_enter(&sc->sc_offer_lock);
    866 	SIMPLEQ_INSERT_TAIL(&sc->sc_offers, co, co_entry);
    867 	mutex_exit(&sc->sc_offer_lock);
    868 }
    869 
    870 static void
    871 vmbus_channel_rescind(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    872 {
    873 	const struct vmbus_chanmsg_chrescind *cmd;
    874 
    875 	cmd = (const struct vmbus_chanmsg_chrescind *)hdr;
    876 	device_printf(sc->sc_dev, "revoking channel %u\n", cmd->chm_chanid);
    877 }
    878 
    879 static void
    880 vmbus_channel_delivered(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    881 {
    882 
    883 	atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_OFFERS_DELIVERED);
    884 	wakeup(&sc->sc_offers);
    885 }
    886 
    887 static void
    888 hyperv_guid_sprint(struct hyperv_guid *guid, char *str, size_t size)
    889 {
    890 	static const struct {
    891 		const struct hyperv_guid *guid;
    892 		const char *ident;
    893 	} map[] = {
    894 		{ &hyperv_guid_network,		"network" },
    895 		{ &hyperv_guid_ide,		"ide" },
    896 		{ &hyperv_guid_scsi,		"scsi" },
    897 		{ &hyperv_guid_shutdown,	"shutdown" },
    898 		{ &hyperv_guid_timesync,	"timesync" },
    899 		{ &hyperv_guid_heartbeat,	"heartbeat" },
    900 		{ &hyperv_guid_kvp,		"kvp" },
    901 		{ &hyperv_guid_vss,		"vss" },
    902 		{ &hyperv_guid_dynmem,		"dynamic-memory" },
    903 		{ &hyperv_guid_mouse,		"mouse" },
    904 		{ &hyperv_guid_kbd,		"keyboard" },
    905 		{ &hyperv_guid_video,		"video" },
    906 		{ &hyperv_guid_fc,		"fiber-channel" },
    907 		{ &hyperv_guid_fcopy,		"file-copy" },
    908 		{ &hyperv_guid_pcie,		"pcie-passthrough" },
    909 		{ &hyperv_guid_netdir,		"network-direct" },
    910 		{ &hyperv_guid_rdesktop,	"remote-desktop" },
    911 		{ &hyperv_guid_avma1,		"avma-1" },
    912 		{ &hyperv_guid_avma2,		"avma-2" },
    913 		{ &hyperv_guid_avma3,		"avma-3" },
    914 		{ &hyperv_guid_avma4,		"avma-4" },
    915 	};
    916 	int i;
    917 
    918 	for (i = 0; i < __arraycount(map); i++) {
    919 		if (memcmp(guid, map[i].guid, sizeof(*guid)) == 0) {
    920 			strlcpy(str, map[i].ident, size);
    921 			return;
    922 		}
    923 	}
    924 	hyperv_guid2str(guid, str, size);
    925 }
    926 
    927 static int
    928 vmbus_channel_scan_done(struct vmbus_softc *sc, struct vmbus_msg *msg __unused)
    929 {
    930 
    931 	return ISSET(sc->sc_flags, VMBUS_SCFLAG_OFFERS_DELIVERED);
    932 }
    933 
    934 static int
    935 vmbus_channel_scan(struct vmbus_softc *sc)
    936 {
    937 	struct vmbus_chanmsg_hdr hdr;
    938 	struct vmbus_chanmsg_choffer rsp;
    939 	struct vmbus_offer *co;
    940 
    941 	SIMPLEQ_INIT(&sc->sc_offers);
    942 	mutex_init(&sc->sc_offer_lock, MUTEX_DEFAULT, IPL_NET);
    943 
    944 	memset(&hdr, 0, sizeof(hdr));
    945 	hdr.chm_type = VMBUS_CHANMSG_CHREQUEST;
    946 
    947 	if (vmbus_cmd(sc, &hdr, sizeof(hdr), &rsp, sizeof(rsp),
    948 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK))) {
    949 		DPRINTF("%s: CHREQUEST failed\n", device_xname(sc->sc_dev));
    950 		return -1;
    951 	}
    952 
    953 	vmbus_wait(sc, vmbus_channel_scan_done, (struct vmbus_msg *)&hdr,
    954 	    &sc->sc_offers, "hvscan");
    955 
    956 	TAILQ_INIT(&sc->sc_channels);
    957 	mutex_init(&sc->sc_channel_lock, MUTEX_DEFAULT, IPL_NET);
    958 
    959 	mutex_enter(&sc->sc_offer_lock);
    960 	while (!SIMPLEQ_EMPTY(&sc->sc_offers)) {
    961 		co = SIMPLEQ_FIRST(&sc->sc_offers);
    962 		SIMPLEQ_REMOVE_HEAD(&sc->sc_offers, co_entry);
    963 		mutex_exit(&sc->sc_offer_lock);
    964 
    965 		vmbus_process_offer(sc, co);
    966 		kmem_free(co, sizeof(*co));
    967 
    968 		mutex_enter(&sc->sc_offer_lock);
    969 	}
    970 	mutex_exit(&sc->sc_offer_lock);
    971 
    972 	return 0;
    973 }
    974 
    975 static struct vmbus_channel *
    976 vmbus_channel_alloc(struct vmbus_softc *sc)
    977 {
    978 	struct vmbus_channel *ch;
    979 
    980 	ch = kmem_zalloc(sizeof(*ch), cold ? KM_NOSLEEP : KM_SLEEP);
    981 
    982 	ch->ch_monprm = hyperv_dma_alloc(sc->sc_dmat, &ch->ch_monprm_dma,
    983 	    sizeof(*ch->ch_monprm), 8, 0, 1);
    984 	if (ch->ch_monprm == NULL) {
    985 		device_printf(sc->sc_dev, "monprm alloc failed\n");
    986 		kmem_free(ch, sizeof(*ch));
    987 		return NULL;
    988 	}
    989 	memset(ch->ch_monprm, 0, sizeof(*ch->ch_monprm));
    990 
    991 	ch->ch_refs = 1;
    992 	ch->ch_sc = sc;
    993 	mutex_init(&ch->ch_subchannel_lock, MUTEX_DEFAULT, IPL_NET);
    994 	TAILQ_INIT(&ch->ch_subchannels);
    995 
    996 	ch->ch_state = VMBUS_CHANSTATE_CLOSED;
    997 
    998 	return ch;
    999 }
   1000 
   1001 static void
   1002 vmbus_channel_free(struct vmbus_channel *ch)
   1003 {
   1004 	struct vmbus_softc *sc = ch->ch_sc;
   1005 
   1006 	KASSERTMSG(TAILQ_EMPTY(&ch->ch_subchannels) &&
   1007 	    ch->ch_subchannel_count == 0, "still owns sub-channels");
   1008 	KASSERTMSG(ch->ch_state == 0 || ch->ch_state == VMBUS_CHANSTATE_CLOSED,
   1009 	    "free busy channel");
   1010 	KASSERTMSG(ch->ch_refs == 0, "channel %u: invalid refcnt %d",
   1011 	    ch->ch_id, ch->ch_refs);
   1012 
   1013 	hyperv_dma_free(sc->sc_dmat, &ch->ch_monprm_dma);
   1014 	mutex_destroy(&ch->ch_subchannel_lock);
   1015 	/* XXX ch_evcnt */
   1016 	softint_disestablish(ch->ch_taskq);
   1017 	kmem_free(ch, sizeof(*ch));
   1018 }
   1019 
   1020 static int
   1021 vmbus_channel_add(struct vmbus_channel *nch)
   1022 {
   1023 	struct vmbus_softc *sc = nch->ch_sc;
   1024 	struct vmbus_channel *ch;
   1025 	u_int refs __diagused;
   1026 
   1027 	if (nch->ch_id == 0) {
   1028 		device_printf(sc->sc_dev, "got channel 0 offer, discard\n");
   1029 		return EINVAL;
   1030 	} else if (nch->ch_id >= sc->sc_channel_max) {
   1031 		device_printf(sc->sc_dev, "invalid channel %u offer\n",
   1032 		    nch->ch_id);
   1033 		return EINVAL;
   1034 	}
   1035 
   1036 	mutex_enter(&sc->sc_channel_lock);
   1037 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   1038 		if (!memcmp(&ch->ch_type, &nch->ch_type, sizeof(ch->ch_type)) &&
   1039 		    !memcmp(&ch->ch_inst, &nch->ch_inst, sizeof(ch->ch_inst)))
   1040 			break;
   1041 	}
   1042 	if (VMBUS_CHAN_ISPRIMARY(nch)) {
   1043 		if (ch == NULL) {
   1044 			TAILQ_INSERT_TAIL(&sc->sc_channels, nch, ch_entry);
   1045 			mutex_exit(&sc->sc_channel_lock);
   1046 			goto done;
   1047 		} else {
   1048 			mutex_exit(&sc->sc_channel_lock);
   1049 			device_printf(sc->sc_dev,
   1050 			    "duplicated primary channel%u\n", nch->ch_id);
   1051 			return EINVAL;
   1052 		}
   1053 	} else {
   1054 		if (ch == NULL) {
   1055 			mutex_exit(&sc->sc_channel_lock);
   1056 			device_printf(sc->sc_dev, "no primary channel%u\n",
   1057 			    nch->ch_id);
   1058 			return EINVAL;
   1059 		}
   1060 	}
   1061 	mutex_exit(&sc->sc_channel_lock);
   1062 
   1063 	KASSERT(!VMBUS_CHAN_ISPRIMARY(nch));
   1064 	KASSERT(ch != NULL);
   1065 
   1066 	refs = atomic_add_int_nv(&nch->ch_refs, 1);
   1067 	KASSERT(refs == 1);
   1068 
   1069 	nch->ch_primary_channel = ch;
   1070 	nch->ch_dev = ch->ch_dev;
   1071 
   1072 	mutex_enter(&ch->ch_subchannel_lock);
   1073 	TAILQ_INSERT_TAIL(&ch->ch_subchannels, nch, ch_subentry);
   1074 	ch->ch_subchannel_count++;
   1075 	mutex_exit(&ch->ch_subchannel_lock);
   1076 	wakeup(ch);
   1077 
   1078 done:
   1079 	vmbus_channel_cpu_default(nch);
   1080 
   1081 	return 0;
   1082 }
   1083 
   1084 void
   1085 vmbus_channel_cpu_set(struct vmbus_channel *ch, int cpu)
   1086 {
   1087 	struct vmbus_softc *sc = ch->ch_sc;
   1088 
   1089 	KASSERTMSG(cpu >= 0 && cpu < ncpu, "invalid cpu %d", cpu);
   1090 
   1091 	if (sc->sc_proto == VMBUS_VERSION_WS2008 ||
   1092 	    sc->sc_proto == VMBUS_VERSION_WIN7) {
   1093 		/* Only cpu0 is supported */
   1094 		cpu = 0;
   1095 	}
   1096 
   1097 	ch->ch_cpuid = cpu;
   1098 	ch->ch_vcpu = sc->sc_percpu[cpu].vcpuid;
   1099 }
   1100 
   1101 void
   1102 vmbus_channel_cpu_rr(struct vmbus_channel *ch)
   1103 {
   1104 	static uint32_t vmbus_channel_nextcpu;
   1105 	int cpu;
   1106 
   1107 	cpu = atomic_add_32_nv(&vmbus_channel_nextcpu, 1) % ncpu;
   1108 	vmbus_channel_cpu_set(ch, cpu);
   1109 }
   1110 
   1111 static void
   1112 vmbus_channel_cpu_default(struct vmbus_channel *ch)
   1113 {
   1114 
   1115         /*
   1116 	 * By default, pin the channel to cpu0.  Devices having
   1117 	 * special channel-cpu mapping requirement should call
   1118 	 * vmbus_channel_cpu_{set,rr}().
   1119 	 */
   1120 	vmbus_channel_cpu_set(ch, 0);
   1121 }
   1122 
   1123 static void
   1124 vmbus_process_offer(struct vmbus_softc *sc, struct vmbus_offer *co)
   1125 {
   1126 	struct vmbus_channel *ch;
   1127 
   1128 	ch = vmbus_channel_alloc(sc);
   1129 	if (ch == NULL) {
   1130 		device_printf(sc->sc_dev, "allocate channel %u failed\n",
   1131 		    co->co_chan.chm_chanid);
   1132 		return;
   1133 	}
   1134 
   1135 	/*
   1136 	 * By default we setup state to enable batched reading.
   1137 	 * A specific service can choose to disable this prior
   1138 	 * to opening the channel.
   1139 	 */
   1140 	ch->ch_flags |= CHF_BATCHED;
   1141 
   1142 	hyperv_guid_sprint(&co->co_chan.chm_chtype, ch->ch_ident,
   1143 	    sizeof(ch->ch_ident));
   1144 
   1145 	ch->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
   1146 	if (sc->sc_proto > VMBUS_VERSION_WS2008)
   1147 		ch->ch_monprm->mp_connid = co->co_chan.chm_connid;
   1148 
   1149 	if (co->co_chan.chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
   1150 		ch->ch_mgroup = co->co_chan.chm_montrig / VMBUS_MONTRIG_LEN;
   1151 		ch->ch_mindex = co->co_chan.chm_montrig % VMBUS_MONTRIG_LEN;
   1152 		ch->ch_flags |= CHF_MONITOR;
   1153 	}
   1154 
   1155 	ch->ch_id = co->co_chan.chm_chanid;
   1156 	ch->ch_subidx = co->co_chan.chm_subidx;
   1157 
   1158 	memcpy(&ch->ch_type, &co->co_chan.chm_chtype, sizeof(ch->ch_type));
   1159 	memcpy(&ch->ch_inst, &co->co_chan.chm_chinst, sizeof(ch->ch_inst));
   1160 
   1161 	if (VMBUS_CHAN_ISPRIMARY(ch)) {
   1162 		/* set primary channel mgmt wq */
   1163 	} else {
   1164 		/* set sub channel mgmt wq */
   1165 	}
   1166 
   1167 	if (vmbus_channel_add(ch) != 0) {
   1168 		vmbus_channel_free(ch);
   1169 		return;
   1170 	}
   1171 
   1172 	ch->ch_state = VMBUS_CHANSTATE_OFFERED;
   1173 
   1174 #ifdef HYPERV_DEBUG
   1175 	printf("%s: channel %u: \"%s\"", device_xname(sc->sc_dev), ch->ch_id,
   1176 	    ch->ch_ident);
   1177 	if (ch->ch_flags & CHF_MONITOR)
   1178 		printf(", monitor %u\n", co->co_chan.chm_montrig);
   1179 	else
   1180 		printf("\n");
   1181 #endif
   1182 }
   1183 
   1184 static int
   1185 vmbus_channel_release(struct vmbus_channel *ch)
   1186 {
   1187 	struct vmbus_softc *sc = ch->ch_sc;
   1188 	struct vmbus_chanmsg_chfree cmd;
   1189 	int rv;
   1190 
   1191 	memset(&cmd, 0, sizeof(cmd));
   1192 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHFREE;
   1193 	cmd.chm_chanid = ch->ch_id;
   1194 
   1195 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), NULL, 0,
   1196 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK));
   1197 	if (rv) {
   1198 		DPRINTF("%s: CHFREE failed with %d\n", device_xname(sc->sc_dev),
   1199 		    rv);
   1200 	}
   1201 	return rv;
   1202 }
   1203 
   1204 struct vmbus_channel **
   1205 vmbus_subchannel_get(struct vmbus_channel *prich, int cnt)
   1206 {
   1207 	struct vmbus_channel **ret, *ch;
   1208 	int i;
   1209 
   1210 	KASSERT(cnt > 0);
   1211 
   1212 	ret = kmem_alloc(sizeof(struct vmbus_channel *) * cnt,
   1213 	    cold ? KM_NOSLEEP : KM_SLEEP);
   1214 
   1215 	mutex_enter(&prich->ch_subchannel_lock);
   1216 
   1217 	while (prich->ch_subchannel_count < cnt)
   1218 		/* XXX use condvar(9) instead of mtsleep */
   1219 		mtsleep(prich, PRIBIO, "hvvmsubch", 0,
   1220 		    &prich->ch_subchannel_lock);
   1221 
   1222 	i = 0;
   1223 	TAILQ_FOREACH(ch, &prich->ch_subchannels, ch_subentry) {
   1224 		ret[i] = ch;	/* XXX inc refs */
   1225 
   1226 		if (++i == cnt)
   1227 			break;
   1228 	}
   1229 
   1230 	mutex_exit(&prich->ch_subchannel_lock);
   1231 
   1232 	return ret;
   1233 }
   1234 
   1235 void
   1236 vmbus_subchannel_put(struct vmbus_channel **subch, int cnt)
   1237 {
   1238 
   1239 	kmem_free(subch, sizeof(struct vmbus_channel *) * cnt);
   1240 }
   1241 
   1242 static struct vmbus_channel *
   1243 vmbus_channel_lookup(struct vmbus_softc *sc, uint32_t relid)
   1244 {
   1245 	struct vmbus_channel *ch;
   1246 
   1247 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   1248 		if (ch->ch_id == relid)
   1249 			return ch;
   1250 	}
   1251 	return NULL;
   1252 }
   1253 
   1254 static int
   1255 vmbus_channel_ring_create(struct vmbus_channel *ch, uint32_t buflen)
   1256 {
   1257 	struct vmbus_softc *sc = ch->ch_sc;
   1258 
   1259 	buflen = roundup(buflen, PAGE_SIZE) + sizeof(struct vmbus_bufring);
   1260 	ch->ch_ring_size = 2 * buflen;
   1261 	ch->ch_ring = hyperv_dma_alloc(sc->sc_dmat, &ch->ch_ring_dma,
   1262 	    ch->ch_ring_size, PAGE_SIZE, 0, 1);	/* page aligned memory */
   1263 	if (ch->ch_ring == NULL) {
   1264 		device_printf(sc->sc_dev,
   1265 		    "failed to allocate channel ring\n");
   1266 		return ENOMEM;
   1267 	}
   1268 	memset(ch->ch_ring, 0, ch->ch_ring_size);
   1269 
   1270 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
   1271 	ch->ch_wrd.rd_ring = (struct vmbus_bufring *)ch->ch_ring;
   1272 	ch->ch_wrd.rd_size = buflen;
   1273 	ch->ch_wrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
   1274 	mutex_init(&ch->ch_wrd.rd_lock, MUTEX_DEFAULT, IPL_NET);
   1275 
   1276 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
   1277 	ch->ch_rrd.rd_ring = (struct vmbus_bufring *)((uint8_t *)ch->ch_ring +
   1278 	    buflen);
   1279 	ch->ch_rrd.rd_size = buflen;
   1280 	ch->ch_rrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
   1281 	mutex_init(&ch->ch_rrd.rd_lock, MUTEX_DEFAULT, IPL_NET);
   1282 
   1283 	if (vmbus_handle_alloc(ch, &ch->ch_ring_dma, ch->ch_ring_size,
   1284 	    &ch->ch_ring_gpadl)) {
   1285 		device_printf(sc->sc_dev,
   1286 		    "failed to obtain a PA handle for the ring\n");
   1287 		vmbus_channel_ring_destroy(ch);
   1288 		return ENOMEM;
   1289 	}
   1290 
   1291 	return 0;
   1292 }
   1293 
   1294 static void
   1295 vmbus_channel_ring_destroy(struct vmbus_channel *ch)
   1296 {
   1297 	struct vmbus_softc *sc = ch->ch_sc;
   1298 
   1299 	hyperv_dma_free(sc->sc_dmat, &ch->ch_ring_dma);
   1300 	ch->ch_ring = NULL;
   1301 	vmbus_handle_free(ch, ch->ch_ring_gpadl);
   1302 
   1303 	mutex_destroy(&ch->ch_wrd.rd_lock);
   1304 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
   1305 	mutex_destroy(&ch->ch_rrd.rd_lock);
   1306 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
   1307 }
   1308 
   1309 int
   1310 vmbus_channel_open(struct vmbus_channel *ch, size_t buflen, void *udata,
   1311     size_t udatalen, void (*handler)(void *), void *arg)
   1312 {
   1313 	struct vmbus_softc *sc = ch->ch_sc;
   1314 	struct vmbus_chanmsg_chopen cmd;
   1315 	struct vmbus_chanmsg_chopen_resp rsp;
   1316 	int rv = EINVAL;
   1317 
   1318 	if (ch->ch_ring == NULL &&
   1319 	    (rv = vmbus_channel_ring_create(ch, buflen))) {
   1320 		DPRINTF("%s: failed to create channel ring\n",
   1321 		    device_xname(sc->sc_dev));
   1322 		return rv;
   1323 	}
   1324 
   1325 	memset(&cmd, 0, sizeof(cmd));
   1326 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHOPEN;
   1327 	cmd.chm_openid = ch->ch_id;
   1328 	cmd.chm_chanid = ch->ch_id;
   1329 	cmd.chm_gpadl = ch->ch_ring_gpadl;
   1330 	cmd.chm_txbr_pgcnt = atop(ch->ch_wrd.rd_size);
   1331 	cmd.chm_vcpuid = ch->ch_vcpu;
   1332 	if (udata && udatalen > 0)
   1333 		memcpy(cmd.chm_udata, udata, udatalen);
   1334 
   1335 	memset(&rsp, 0, sizeof(rsp));
   1336 
   1337 	ch->ch_handler = handler;
   1338 	ch->ch_ctx = arg;
   1339 	ch->ch_state = VMBUS_CHANSTATE_OPENED;
   1340 
   1341 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
   1342 	    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
   1343 	if (rv) {
   1344 		vmbus_channel_ring_destroy(ch);
   1345 		DPRINTF("%s: CHOPEN failed with %d\n", device_xname(sc->sc_dev),
   1346 		    rv);
   1347 		ch->ch_handler = NULL;
   1348 		ch->ch_ctx = NULL;
   1349 		ch->ch_state = VMBUS_CHANSTATE_OFFERED;
   1350 		return rv;
   1351 	}
   1352 	return 0;
   1353 }
   1354 
   1355 static void
   1356 vmbus_channel_detach(struct vmbus_channel *ch)
   1357 {
   1358 	u_int refs;
   1359 
   1360 	refs = atomic_add_int_nv(&ch->ch_refs, -1);
   1361 	if (refs == 1) {
   1362 		/* XXX on workqueue? */
   1363 		if (VMBUS_CHAN_ISPRIMARY(ch)) {
   1364 			vmbus_channel_release(ch);
   1365 			vmbus_channel_free(ch);
   1366 		} else {
   1367 			struct vmbus_channel *prich = ch->ch_primary_channel;
   1368 
   1369 			vmbus_channel_release(ch);
   1370 
   1371 			mutex_enter(&prich->ch_subchannel_lock);
   1372 			TAILQ_REMOVE(&prich->ch_subchannels, ch, ch_subentry);
   1373 			prich->ch_subchannel_count--;
   1374 			mutex_exit(&prich->ch_subchannel_lock);
   1375 			wakeup(prich);
   1376 
   1377 			vmbus_channel_free(ch);
   1378 		}
   1379 	}
   1380 }
   1381 
   1382 static int
   1383 vmbus_channel_close_internal(struct vmbus_channel *ch)
   1384 {
   1385 	struct vmbus_softc *sc = ch->ch_sc;
   1386 	struct vmbus_chanmsg_chclose cmd;
   1387 	int rv;
   1388 
   1389 	memset(&cmd, 0, sizeof(cmd));
   1390 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHCLOSE;
   1391 	cmd.chm_chanid = ch->ch_id;
   1392 
   1393 	ch->ch_state = VMBUS_CHANSTATE_CLOSING;
   1394 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), NULL, 0,
   1395 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK));
   1396 	if (rv) {
   1397 		DPRINTF("%s: CHCLOSE failed with %d\n",
   1398 		    device_xname(sc->sc_dev), rv);
   1399 		return rv;
   1400 	}
   1401 	ch->ch_state = VMBUS_CHANSTATE_CLOSED;
   1402 	vmbus_channel_ring_destroy(ch);
   1403 	return 0;
   1404 }
   1405 
   1406 int
   1407 vmbus_channel_close_direct(struct vmbus_channel *ch)
   1408 {
   1409 	int rv;
   1410 
   1411 	rv = vmbus_channel_close_internal(ch);
   1412 	if (!VMBUS_CHAN_ISPRIMARY(ch))
   1413 		vmbus_channel_detach(ch);
   1414 	return rv;
   1415 }
   1416 
   1417 int
   1418 vmbus_channel_close(struct vmbus_channel *ch)
   1419 {
   1420 	struct vmbus_channel **subch;
   1421 	int i, cnt, rv;
   1422 
   1423 	if (!VMBUS_CHAN_ISPRIMARY(ch))
   1424 		return 0;
   1425 
   1426 	cnt = ch->ch_subchannel_count;
   1427 	if (cnt > 0) {
   1428 		subch = vmbus_subchannel_get(ch, cnt);
   1429 		for (i = 0; i < ch->ch_subchannel_count; i++) {
   1430 			rv = vmbus_channel_close_internal(subch[i]);
   1431 			(void) rv;	/* XXX */
   1432 			vmbus_channel_detach(ch);
   1433 		}
   1434 		vmbus_subchannel_put(subch, cnt);
   1435 	}
   1436 
   1437 	return vmbus_channel_close_internal(ch);
   1438 }
   1439 
   1440 static inline void
   1441 vmbus_channel_setevent(struct vmbus_softc *sc, struct vmbus_channel *ch)
   1442 {
   1443 	struct vmbus_mon_trig *mtg;
   1444 
   1445 	/* Each uint32_t represents 32 channels */
   1446 	set_bit(ch->ch_id, sc->sc_wevents);
   1447 	if (ch->ch_flags & CHF_MONITOR) {
   1448 		mtg = &sc->sc_monitor[1]->mnf_trigs[ch->ch_mgroup];
   1449 		set_bit(ch->ch_mindex, &mtg->mt_pending);
   1450 	} else
   1451 		vmbus_intr_signal(sc, hyperv_dma_get_paddr(&ch->ch_monprm_dma));
   1452 }
   1453 
   1454 static void
   1455 vmbus_channel_intr(void *arg)
   1456 {
   1457 	struct vmbus_channel *ch = arg;
   1458 
   1459 	if (vmbus_channel_ready(ch))
   1460 		ch->ch_handler(ch->ch_ctx);
   1461 
   1462 	if (vmbus_channel_unpause(ch) == 0)
   1463 		return;
   1464 
   1465 	vmbus_channel_pause(ch);
   1466 	vmbus_channel_schedule(ch);
   1467 }
   1468 
   1469 int
   1470 vmbus_channel_setdeferred(struct vmbus_channel *ch, const char *name)
   1471 {
   1472 
   1473 	ch->ch_taskq = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1474 	    vmbus_channel_intr, ch);
   1475 	if (ch->ch_taskq == NULL)
   1476 		return -1;
   1477 	return 0;
   1478 }
   1479 
   1480 void
   1481 vmbus_channel_schedule(struct vmbus_channel *ch)
   1482 {
   1483 
   1484 	if (ch->ch_handler) {
   1485 		if (!cold && (ch->ch_flags & CHF_BATCHED)) {
   1486 			vmbus_channel_pause(ch);
   1487 			softint_schedule(ch->ch_taskq);
   1488 		} else
   1489 			ch->ch_handler(ch->ch_ctx);
   1490 	}
   1491 }
   1492 
   1493 static __inline void
   1494 vmbus_ring_put(struct vmbus_ring_data *wrd, uint8_t *data, uint32_t datalen)
   1495 {
   1496 	int left = MIN(datalen, wrd->rd_dsize - wrd->rd_prod);
   1497 
   1498 	memcpy(&wrd->rd_ring->br_data[wrd->rd_prod], data, left);
   1499 	memcpy(&wrd->rd_ring->br_data[0], data + left, datalen - left);
   1500 	wrd->rd_prod += datalen;
   1501 	if (wrd->rd_prod >= wrd->rd_dsize)
   1502 		wrd->rd_prod -= wrd->rd_dsize;
   1503 }
   1504 
   1505 static inline void
   1506 vmbus_ring_get(struct vmbus_ring_data *rrd, uint8_t *data, uint32_t datalen,
   1507     int peek)
   1508 {
   1509 	int left = MIN(datalen, rrd->rd_dsize - rrd->rd_cons);
   1510 
   1511 	memcpy(data, &rrd->rd_ring->br_data[rrd->rd_cons], left);
   1512 	memcpy(data + left, &rrd->rd_ring->br_data[0], datalen - left);
   1513 	if (!peek) {
   1514 		rrd->rd_cons += datalen;
   1515 		if (rrd->rd_cons >= rrd->rd_dsize)
   1516 			rrd->rd_cons -= rrd->rd_dsize;
   1517 	}
   1518 }
   1519 
   1520 static __inline void
   1521 vmbus_ring_avail(struct vmbus_ring_data *rd, uint32_t *towrite,
   1522     uint32_t *toread)
   1523 {
   1524 	uint32_t ridx = rd->rd_ring->br_rindex;
   1525 	uint32_t widx = rd->rd_ring->br_windex;
   1526 	uint32_t r, w;
   1527 
   1528 	if (widx >= ridx)
   1529 		w = rd->rd_dsize - (widx - ridx);
   1530 	else
   1531 		w = ridx - widx;
   1532 	r = rd->rd_dsize - w;
   1533 	if (towrite)
   1534 		*towrite = w;
   1535 	if (toread)
   1536 		*toread = r;
   1537 }
   1538 
   1539 static int
   1540 vmbus_ring_write(struct vmbus_ring_data *wrd, struct iovec *iov, int iov_cnt,
   1541     int *needsig)
   1542 {
   1543 	uint64_t indices = 0;
   1544 	uint32_t avail, oprod, datalen = sizeof(indices);
   1545 	int i;
   1546 
   1547 	for (i = 0; i < iov_cnt; i++)
   1548 		datalen += iov[i].iov_len;
   1549 
   1550 	KASSERT(datalen <= wrd->rd_dsize);
   1551 
   1552 	vmbus_ring_avail(wrd, &avail, NULL);
   1553 	if (avail <= datalen) {
   1554 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
   1555 		return EAGAIN;
   1556 	}
   1557 
   1558 	oprod = wrd->rd_prod;
   1559 
   1560 	for (i = 0; i < iov_cnt; i++)
   1561 		vmbus_ring_put(wrd, iov[i].iov_base, iov[i].iov_len);
   1562 
   1563 	indices = (uint64_t)oprod << 32;
   1564 	vmbus_ring_put(wrd, (uint8_t *)&indices, sizeof(indices));
   1565 
   1566 	membar_sync();
   1567 	wrd->rd_ring->br_windex = wrd->rd_prod;
   1568 	membar_sync();
   1569 
   1570 	/* Signal when the ring transitions from being empty to non-empty */
   1571 	if (wrd->rd_ring->br_imask == 0 &&
   1572 	    wrd->rd_ring->br_rindex == oprod)
   1573 		*needsig = 1;
   1574 	else
   1575 		*needsig = 0;
   1576 
   1577 	return 0;
   1578 }
   1579 
   1580 int
   1581 vmbus_channel_send(struct vmbus_channel *ch, void *data, uint32_t datalen,
   1582     uint64_t rid, int type, uint32_t flags)
   1583 {
   1584 	struct vmbus_softc *sc = ch->ch_sc;
   1585 	struct vmbus_chanpkt cp;
   1586 	struct iovec iov[3];
   1587 	uint32_t pktlen, pktlen_aligned;
   1588 	uint64_t zeropad = 0;
   1589 	int rv, needsig = 0;
   1590 
   1591 	pktlen = sizeof(cp) + datalen;
   1592 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1593 
   1594 	cp.cp_hdr.cph_type = type;
   1595 	cp.cp_hdr.cph_flags = flags;
   1596 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp));
   1597 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1598 	cp.cp_hdr.cph_tid = rid;
   1599 
   1600 	iov[0].iov_base = &cp;
   1601 	iov[0].iov_len = sizeof(cp);
   1602 
   1603 	iov[1].iov_base = data;
   1604 	iov[1].iov_len = datalen;
   1605 
   1606 	iov[2].iov_base = &zeropad;
   1607 	iov[2].iov_len = pktlen_aligned - pktlen;
   1608 
   1609 	mutex_enter(&ch->ch_wrd.rd_lock);
   1610 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 3, &needsig);
   1611 	mutex_exit(&ch->ch_wrd.rd_lock);
   1612 	if (rv == 0 && needsig)
   1613 		vmbus_channel_setevent(sc, ch);
   1614 
   1615 	return rv;
   1616 }
   1617 
   1618 int
   1619 vmbus_channel_send_sgl(struct vmbus_channel *ch, struct vmbus_gpa *sgl,
   1620     uint32_t nsge, void *data, uint32_t datalen, uint64_t rid)
   1621 {
   1622 	struct vmbus_softc *sc = ch->ch_sc;
   1623 	struct vmbus_chanpkt_sglist cp;
   1624 	struct iovec iov[4];
   1625 	uint32_t buflen, pktlen, pktlen_aligned;
   1626 	uint64_t zeropad = 0;
   1627 	int rv, needsig = 0;
   1628 
   1629 	buflen = sizeof(struct vmbus_gpa) * nsge;
   1630 	pktlen = sizeof(cp) + datalen + buflen;
   1631 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1632 
   1633 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
   1634 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
   1635 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
   1636 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1637 	cp.cp_hdr.cph_tid = rid;
   1638 	cp.cp_gpa_cnt = nsge;
   1639 	cp.cp_rsvd = 0;
   1640 
   1641 	iov[0].iov_base = &cp;
   1642 	iov[0].iov_len = sizeof(cp);
   1643 
   1644 	iov[1].iov_base = sgl;
   1645 	iov[1].iov_len = buflen;
   1646 
   1647 	iov[2].iov_base = data;
   1648 	iov[2].iov_len = datalen;
   1649 
   1650 	iov[3].iov_base = &zeropad;
   1651 	iov[3].iov_len = pktlen_aligned - pktlen;
   1652 
   1653 	mutex_enter(&ch->ch_wrd.rd_lock);
   1654 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 4, &needsig);
   1655 	mutex_exit(&ch->ch_wrd.rd_lock);
   1656 	if (rv == 0 && needsig)
   1657 		vmbus_channel_setevent(sc, ch);
   1658 
   1659 	return rv;
   1660 }
   1661 
   1662 int
   1663 vmbus_channel_send_prpl(struct vmbus_channel *ch, struct vmbus_gpa_range *prpl,
   1664     uint32_t nprp, void *data, uint32_t datalen, uint64_t rid)
   1665 {
   1666 	struct vmbus_softc *sc = ch->ch_sc;
   1667 	struct vmbus_chanpkt_prplist cp;
   1668 	struct iovec iov[4];
   1669 	uint32_t buflen, pktlen, pktlen_aligned;
   1670 	uint64_t zeropad = 0;
   1671 	int rv, needsig = 0;
   1672 
   1673 	buflen = sizeof(struct vmbus_gpa_range) * (nprp + 1);
   1674 	pktlen = sizeof(cp) + datalen + buflen;
   1675 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1676 
   1677 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
   1678 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
   1679 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
   1680 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1681 	cp.cp_hdr.cph_tid = rid;
   1682 	cp.cp_range_cnt = 1;
   1683 	cp.cp_rsvd = 0;
   1684 
   1685 	iov[0].iov_base = &cp;
   1686 	iov[0].iov_len = sizeof(cp);
   1687 
   1688 	iov[1].iov_base = prpl;
   1689 	iov[1].iov_len = buflen;
   1690 
   1691 	iov[2].iov_base = data;
   1692 	iov[2].iov_len = datalen;
   1693 
   1694 	iov[3].iov_base = &zeropad;
   1695 	iov[3].iov_len = pktlen_aligned - pktlen;
   1696 
   1697 	mutex_enter(&ch->ch_wrd.rd_lock);
   1698 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 4, &needsig);
   1699 	mutex_exit(&ch->ch_wrd.rd_lock);
   1700 	if (rv == 0 && needsig)
   1701 		vmbus_channel_setevent(sc, ch);
   1702 
   1703 	return rv;
   1704 }
   1705 
   1706 static int
   1707 vmbus_ring_peek(struct vmbus_ring_data *rrd, void *data, uint32_t datalen)
   1708 {
   1709 	uint32_t avail;
   1710 
   1711 	KASSERT(datalen <= rrd->rd_dsize);
   1712 
   1713 	vmbus_ring_avail(rrd, NULL, &avail);
   1714 	if (avail < datalen)
   1715 		return EAGAIN;
   1716 
   1717 	vmbus_ring_get(rrd, (uint8_t *)data, datalen, 1);
   1718 	return 0;
   1719 }
   1720 
   1721 static int
   1722 vmbus_ring_read(struct vmbus_ring_data *rrd, void *data, uint32_t datalen,
   1723     uint32_t offset)
   1724 {
   1725 	uint64_t indices;
   1726 	uint32_t avail;
   1727 
   1728 	KASSERT(datalen <= rrd->rd_dsize);
   1729 
   1730 	vmbus_ring_avail(rrd, NULL, &avail);
   1731 	if (avail < datalen) {
   1732 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
   1733 		return EAGAIN;
   1734 	}
   1735 
   1736 	if (offset) {
   1737 		rrd->rd_cons += offset;
   1738 		if (rrd->rd_cons >= rrd->rd_dsize)
   1739 			rrd->rd_cons -= rrd->rd_dsize;
   1740 	}
   1741 
   1742 	vmbus_ring_get(rrd, (uint8_t *)data, datalen, 0);
   1743 	vmbus_ring_get(rrd, (uint8_t *)&indices, sizeof(indices), 0);
   1744 
   1745 	membar_sync();
   1746 	rrd->rd_ring->br_rindex = rrd->rd_cons;
   1747 
   1748 	return 0;
   1749 }
   1750 
   1751 int
   1752 vmbus_channel_recv(struct vmbus_channel *ch, void *data, uint32_t datalen,
   1753     uint32_t *rlen, uint64_t *rid, int raw)
   1754 {
   1755 	struct vmbus_softc *sc = ch->ch_sc;
   1756 	struct vmbus_chanpkt_hdr cph;
   1757 	uint32_t offset, pktlen;
   1758 	int rv;
   1759 
   1760 	*rlen = 0;
   1761 
   1762 	mutex_enter(&ch->ch_rrd.rd_lock);
   1763 
   1764 	if ((rv = vmbus_ring_peek(&ch->ch_rrd, &cph, sizeof(cph))) != 0) {
   1765 		mutex_exit(&ch->ch_rrd.rd_lock);
   1766 		return rv;
   1767 	}
   1768 
   1769 	offset = raw ? 0 : VMBUS_CHANPKT_GETLEN(cph.cph_hlen);
   1770 	pktlen = VMBUS_CHANPKT_GETLEN(cph.cph_tlen) - offset;
   1771 	if (pktlen > datalen) {
   1772 		mutex_exit(&ch->ch_rrd.rd_lock);
   1773 		device_printf(sc->sc_dev, "%s: pktlen %u datalen %u\n",
   1774 		    __func__, pktlen, datalen);
   1775 		return EINVAL;
   1776 	}
   1777 
   1778 	rv = vmbus_ring_read(&ch->ch_rrd, data, pktlen, offset);
   1779 	if (rv == 0) {
   1780 		*rlen = pktlen;
   1781 		*rid = cph.cph_tid;
   1782 	}
   1783 
   1784 	mutex_exit(&ch->ch_rrd.rd_lock);
   1785 
   1786 	return rv;
   1787 }
   1788 
   1789 static inline void
   1790 vmbus_ring_mask(struct vmbus_ring_data *rd)
   1791 {
   1792 
   1793 	membar_sync();
   1794 	rd->rd_ring->br_imask = 1;
   1795 	membar_sync();
   1796 }
   1797 
   1798 static inline void
   1799 vmbus_ring_unmask(struct vmbus_ring_data *rd)
   1800 {
   1801 
   1802 	membar_sync();
   1803 	rd->rd_ring->br_imask = 0;
   1804 	membar_sync();
   1805 }
   1806 
   1807 static void
   1808 vmbus_channel_pause(struct vmbus_channel *ch)
   1809 {
   1810 
   1811 	vmbus_ring_mask(&ch->ch_rrd);
   1812 }
   1813 
   1814 static uint32_t
   1815 vmbus_channel_unpause(struct vmbus_channel *ch)
   1816 {
   1817 	uint32_t avail;
   1818 
   1819 	vmbus_ring_unmask(&ch->ch_rrd);
   1820 	vmbus_ring_avail(&ch->ch_rrd, NULL, &avail);
   1821 
   1822 	return avail;
   1823 }
   1824 
   1825 static uint32_t
   1826 vmbus_channel_ready(struct vmbus_channel *ch)
   1827 {
   1828 	uint32_t avail;
   1829 
   1830 	vmbus_ring_avail(&ch->ch_rrd, NULL, &avail);
   1831 
   1832 	return avail;
   1833 }
   1834 
   1835 /* How many PFNs can be referenced by the header */
   1836 #define VMBUS_NPFNHDR	((VMBUS_MSG_DSIZE_MAX -	\
   1837 	  sizeof(struct vmbus_chanmsg_gpadl_conn)) / sizeof(uint64_t))
   1838 
   1839 /* How many PFNs can be referenced by the body */
   1840 #define VMBUS_NPFNBODY	((VMBUS_MSG_DSIZE_MAX -	\
   1841 	  sizeof(struct vmbus_chanmsg_gpadl_subconn)) / sizeof(uint64_t))
   1842 
   1843 int
   1844 vmbus_handle_alloc(struct vmbus_channel *ch, const struct hyperv_dma *dma,
   1845     uint32_t buflen, uint32_t *handle)
   1846 {
   1847 	const int prflags = cold ? PR_NOWAIT : PR_WAITOK;
   1848 	const int kmemflags = cold ? KM_NOSLEEP : KM_SLEEP;
   1849 	const int msgflags = cold ? MSGF_NOSLEEP : 0;
   1850 	const int hcflags = cold ? HCF_NOSLEEP : HCF_SLEEPOK;
   1851 	struct vmbus_softc *sc = ch->ch_sc;
   1852 	struct vmbus_chanmsg_gpadl_conn *hdr;
   1853 	struct vmbus_chanmsg_gpadl_subconn *cmd;
   1854 	struct vmbus_chanmsg_gpadl_connresp rsp;
   1855 	struct vmbus_msg *msg;
   1856 	int i, j, last, left, rv;
   1857 	int bodylen = 0, ncmds = 0, pfn = 0;
   1858 	uint64_t *frames;
   1859 	paddr_t pa;
   1860 	uint8_t *body;
   1861 	/* Total number of pages to reference */
   1862 	int total = atop(buflen);
   1863 	/* Number of pages that will fit the header */
   1864 	int inhdr = MIN(total, VMBUS_NPFNHDR);
   1865 
   1866 	KASSERT((buflen & PAGE_MASK) == 0);
   1867 	KASSERT(buflen == (uint32_t)dma->map->dm_mapsize);
   1868 
   1869 	msg = pool_cache_get_paddr(sc->sc_msgpool, prflags, &pa);
   1870 	if (msg == NULL)
   1871 		return ENOMEM;
   1872 
   1873 	/* Prepare array of frame addresses */
   1874 	frames = kmem_zalloc(total * sizeof(*frames), kmemflags);
   1875 	if (frames == NULL) {
   1876 		pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1877 		return ENOMEM;
   1878 	}
   1879 	for (i = 0, j = 0; i < dma->map->dm_nsegs && j < total; i++) {
   1880 		bus_dma_segment_t *seg = &dma->map->dm_segs[i];
   1881 		bus_addr_t addr = seg->ds_addr;
   1882 
   1883 		KASSERT((addr & PAGE_MASK) == 0);
   1884 		KASSERT((seg->ds_len & PAGE_MASK) == 0);
   1885 
   1886 		while (addr < seg->ds_addr + seg->ds_len && j < total) {
   1887 			frames[j++] = atop(addr);
   1888 			addr += PAGE_SIZE;
   1889 		}
   1890 	}
   1891 
   1892 	memset(msg, 0, sizeof(*msg));
   1893 	msg->msg_req.hc_dsize = sizeof(struct vmbus_chanmsg_gpadl_conn) +
   1894 	    inhdr * sizeof(uint64_t);
   1895 	hdr = (struct vmbus_chanmsg_gpadl_conn *)msg->msg_req.hc_data;
   1896 	msg->msg_rsp = &rsp;
   1897 	msg->msg_rsplen = sizeof(rsp);
   1898 	msg->msg_flags = msgflags;
   1899 
   1900 	left = total - inhdr;
   1901 
   1902 	/* Allocate additional gpadl_body structures if required */
   1903 	if (left > 0) {
   1904 		ncmds = MAX(1, left / VMBUS_NPFNBODY + left % VMBUS_NPFNBODY);
   1905 		bodylen = ncmds * VMBUS_MSG_DSIZE_MAX;
   1906 		body = kmem_zalloc(bodylen, kmemflags);
   1907 		if (body == NULL) {
   1908 			kmem_free(frames, total * sizeof(*frames));
   1909 			pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1910 			return ENOMEM;
   1911 		}
   1912 	}
   1913 
   1914 	*handle = atomic_add_int_nv(&sc->sc_handle, 1);
   1915 
   1916 	hdr->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_CONN;
   1917 	hdr->chm_chanid = ch->ch_id;
   1918 	hdr->chm_gpadl = *handle;
   1919 
   1920 	/* Single range for a contiguous buffer */
   1921 	hdr->chm_range_cnt = 1;
   1922 	hdr->chm_range_len = sizeof(struct vmbus_gpa_range) + total *
   1923 	    sizeof(uint64_t);
   1924 	hdr->chm_range.gpa_ofs = 0;
   1925 	hdr->chm_range.gpa_len = buflen;
   1926 
   1927 	/* Fit as many pages as possible into the header */
   1928 	for (i = 0; i < inhdr; i++)
   1929 		hdr->chm_range.gpa_page[i] = frames[pfn++];
   1930 
   1931 	for (i = 0; i < ncmds; i++) {
   1932 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
   1933 		    VMBUS_MSG_DSIZE_MAX * i);
   1934 		cmd->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_SUBCONN;
   1935 		cmd->chm_gpadl = *handle;
   1936 		last = MIN(left, VMBUS_NPFNBODY);
   1937 		for (j = 0; j < last; j++)
   1938 			cmd->chm_gpa_page[j] = frames[pfn++];
   1939 		left -= last;
   1940 	}
   1941 
   1942 	rv = vmbus_start(sc, msg, pa);
   1943 	if (rv != 0) {
   1944 		DPRINTF("%s: GPADL_CONN failed\n", device_xname(sc->sc_dev));
   1945 		goto out;
   1946 	}
   1947 	for (i = 0; i < ncmds; i++) {
   1948 		int cmdlen = sizeof(*cmd);
   1949 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
   1950 		    VMBUS_MSG_DSIZE_MAX * i);
   1951 		/* Last element can be short */
   1952 		if (i == ncmds - 1)
   1953 			cmdlen += last * sizeof(uint64_t);
   1954 		else
   1955 			cmdlen += VMBUS_NPFNBODY * sizeof(uint64_t);
   1956 		rv = vmbus_cmd(sc, cmd, cmdlen, NULL, 0, HCF_NOREPLY | hcflags);
   1957 		if (rv != 0) {
   1958 			DPRINTF("%s: GPADL_SUBCONN (iteration %d/%d) failed "
   1959 			    "with %d\n", device_xname(sc->sc_dev), i, ncmds,
   1960 			    rv);
   1961 			goto out;
   1962 		}
   1963 	}
   1964 	rv = vmbus_reply(sc, msg);
   1965 	if (rv != 0) {
   1966 		DPRINTF("%s: GPADL allocation failed with %d\n",
   1967 		    device_xname(sc->sc_dev), rv);
   1968 	}
   1969 
   1970  out:
   1971 	if (bodylen > 0)
   1972 		kmem_free(body, bodylen);
   1973 	kmem_free(frames, total * sizeof(*frames));
   1974 	pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1975 	if (rv)
   1976 		return rv;
   1977 
   1978 	KASSERT(*handle == rsp.chm_gpadl);
   1979 
   1980 	return 0;
   1981 }
   1982 
   1983 void
   1984 vmbus_handle_free(struct vmbus_channel *ch, uint32_t handle)
   1985 {
   1986 	struct vmbus_softc *sc = ch->ch_sc;
   1987 	struct vmbus_chanmsg_gpadl_disconn cmd;
   1988 	struct vmbus_chanmsg_gpadl_disconn rsp;
   1989 	int rv;
   1990 
   1991 	memset(&cmd, 0, sizeof(cmd));
   1992 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_DISCONN;
   1993 	cmd.chm_chanid = ch->ch_id;
   1994 	cmd.chm_gpadl = handle;
   1995 
   1996 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
   1997 	    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
   1998 	if (rv) {
   1999 		DPRINTF("%s: GPADL_DISCONN failed with %d\n",
   2000 		    device_xname(sc->sc_dev), rv);
   2001 	}
   2002 }
   2003 
   2004 static int
   2005 vmbus_attach_print(void *aux, const char *name)
   2006 {
   2007 	struct vmbus_attach_args *aa = aux;
   2008 
   2009 	if (name)
   2010 		printf("\"%s\" at %s", aa->aa_ident, name);
   2011 
   2012 	return UNCONF;
   2013 }
   2014 
   2015 static int
   2016 vmbus_attach_icdevs(struct vmbus_softc *sc)
   2017 {
   2018 	struct vmbus_dev *dv;
   2019 	struct vmbus_channel *ch;
   2020 
   2021 	SLIST_INIT(&sc->sc_icdevs);
   2022 	mutex_init(&sc->sc_icdev_lock, MUTEX_DEFAULT, IPL_NET);
   2023 
   2024 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   2025 		if (ch->ch_state != VMBUS_CHANSTATE_OFFERED)
   2026 			continue;
   2027 		if (ch->ch_flags & CHF_MONITOR)
   2028 			continue;
   2029 
   2030 		dv = kmem_zalloc(sizeof(*dv), cold ? KM_NOSLEEP : KM_SLEEP);
   2031 		if (dv == NULL) {
   2032 			device_printf(sc->sc_dev,
   2033 			    "failed to allocate ic device object\n");
   2034 			return ENOMEM;
   2035 		}
   2036 		dv->dv_aa.aa_type = &ch->ch_type;
   2037 		dv->dv_aa.aa_inst = &ch->ch_inst;
   2038 		dv->dv_aa.aa_ident = ch->ch_ident;
   2039 		dv->dv_aa.aa_chan = ch;
   2040 		dv->dv_aa.aa_iot = sc->sc_iot;
   2041 		dv->dv_aa.aa_memt = sc->sc_memt;
   2042 		mutex_enter(&sc->sc_icdev_lock);
   2043 		SLIST_INSERT_HEAD(&sc->sc_icdevs, dv, dv_entry);
   2044 		mutex_exit(&sc->sc_icdev_lock);
   2045 		ch->ch_dev = config_found_ia(sc->sc_dev, "hypervvmbus",
   2046 		    &dv->dv_aa, vmbus_attach_print);
   2047 	}
   2048 	return 0;
   2049 }
   2050 
   2051 static int
   2052 vmbus_attach_devices(struct vmbus_softc *sc)
   2053 {
   2054 	struct vmbus_dev *dv;
   2055 	struct vmbus_channel *ch;
   2056 
   2057 	SLIST_INIT(&sc->sc_devs);
   2058 	mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NET);
   2059 
   2060 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   2061 		if (ch->ch_state != VMBUS_CHANSTATE_OFFERED)
   2062 			continue;
   2063 		if (!(ch->ch_flags & CHF_MONITOR))
   2064 			continue;
   2065 
   2066 		dv = kmem_zalloc(sizeof(*dv), cold ? KM_NOSLEEP : KM_SLEEP);
   2067 		if (dv == NULL) {
   2068 			device_printf(sc->sc_dev,
   2069 			    "failed to allocate device object\n");
   2070 			return ENOMEM;
   2071 		}
   2072 		dv->dv_aa.aa_type = &ch->ch_type;
   2073 		dv->dv_aa.aa_inst = &ch->ch_inst;
   2074 		dv->dv_aa.aa_ident = ch->ch_ident;
   2075 		dv->dv_aa.aa_chan = ch;
   2076 		dv->dv_aa.aa_iot = sc->sc_iot;
   2077 		dv->dv_aa.aa_memt = sc->sc_memt;
   2078 		mutex_enter(&sc->sc_dev_lock);
   2079 		SLIST_INSERT_HEAD(&sc->sc_devs, dv, dv_entry);
   2080 		mutex_exit(&sc->sc_dev_lock);
   2081 		ch->ch_dev = config_found_ia(sc->sc_dev, "hypervvmbus",
   2082 		    &dv->dv_aa, vmbus_attach_print);
   2083 	}
   2084 	return 0;
   2085 }
   2086 
   2087 MODULE(MODULE_CLASS_DRIVER, vmbus, "hyperv");
   2088 
   2089 #ifdef _MODULE
   2090 #include "ioconf.c"
   2091 #endif
   2092 
   2093 static int
   2094 vmbus_modcmd(modcmd_t cmd, void *aux)
   2095 {
   2096 	int rv = 0;
   2097 
   2098 	switch (cmd) {
   2099 	case MODULE_CMD_INIT:
   2100 #ifdef _MODULE
   2101 		rv = config_init_component(cfdriver_ioconf_vmbus,
   2102 		    cfattach_ioconf_vmbus, cfdata_ioconf_vmbus);
   2103 #endif
   2104 		break;
   2105 
   2106 	case MODULE_CMD_FINI:
   2107 #ifdef _MODULE
   2108 		rv = config_fini_component(cfdriver_ioconf_vmbus,
   2109 		    cfattach_ioconf_vmbus, cfdata_ioconf_vmbus);
   2110 #endif
   2111 		break;
   2112 
   2113 	default:
   2114 		rv = ENOTTY;
   2115 		break;
   2116 	}
   2117 
   2118 	return rv;
   2119 }
   2120