xref: /freebsd/sys/dev/hyperv/vmbus/vmbus_chan.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * Copyright (c) 2009-2012,2016 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 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/callout.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/smp.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 
41 #include <machine/atomic.h>
42 #include <machine/stdarg.h>
43 
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_extern.h>
47 
48 #include <dev/hyperv/include/vmbus_xact.h>
49 #include <dev/hyperv/vmbus/hyperv_var.h>
50 #include <dev/hyperv/vmbus/vmbus_reg.h>
51 #include <dev/hyperv/vmbus/vmbus_var.h>
52 #include <dev/hyperv/vmbus/vmbus_brvar.h>
53 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
54 
55 struct vmbus_chan_pollarg {
56 	struct vmbus_channel	*poll_chan;
57 	u_int			poll_hz;
58 };
59 
60 static void			vmbus_chan_update_evtflagcnt(
61 				    struct vmbus_softc *,
62 				    const struct vmbus_channel *);
63 static int			vmbus_chan_close_internal(
64 				    struct vmbus_channel *);
65 static int			vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS);
66 static void			vmbus_chan_sysctl_create(
67 				    struct vmbus_channel *);
68 static struct vmbus_channel	*vmbus_chan_alloc(struct vmbus_softc *);
69 static void			vmbus_chan_free(struct vmbus_channel *);
70 static int			vmbus_chan_add(struct vmbus_channel *);
71 static void			vmbus_chan_cpu_default(struct vmbus_channel *);
72 static int			vmbus_chan_release(struct vmbus_channel *);
73 static void			vmbus_chan_set_chmap(struct vmbus_channel *);
74 static void			vmbus_chan_clear_chmap(struct vmbus_channel *);
75 static void			vmbus_chan_detach(struct vmbus_channel *);
76 static bool			vmbus_chan_wait_revoke(
77 				    const struct vmbus_channel *, bool);
78 static void			vmbus_chan_poll_timeout(void *);
79 static bool			vmbus_chan_poll_cancel_intq(
80 				    struct vmbus_channel *);
81 static void			vmbus_chan_poll_cancel(struct vmbus_channel *);
82 
83 static void			vmbus_chan_ins_prilist(struct vmbus_softc *,
84 				    struct vmbus_channel *);
85 static void			vmbus_chan_rem_prilist(struct vmbus_softc *,
86 				    struct vmbus_channel *);
87 static void			vmbus_chan_ins_list(struct vmbus_softc *,
88 				    struct vmbus_channel *);
89 static void			vmbus_chan_rem_list(struct vmbus_softc *,
90 				    struct vmbus_channel *);
91 static void			vmbus_chan_ins_sublist(struct vmbus_channel *,
92 				    struct vmbus_channel *);
93 static void			vmbus_chan_rem_sublist(struct vmbus_channel *,
94 				    struct vmbus_channel *);
95 
96 static void			vmbus_chan_task(void *, int);
97 static void			vmbus_chan_task_nobatch(void *, int);
98 static void			vmbus_chan_poll_task(void *, int);
99 static void			vmbus_chan_clrchmap_task(void *, int);
100 static void			vmbus_chan_pollcfg_task(void *, int);
101 static void			vmbus_chan_polldis_task(void *, int);
102 static void			vmbus_chan_poll_cancel_task(void *, int);
103 static void			vmbus_prichan_attach_task(void *, int);
104 static void			vmbus_subchan_attach_task(void *, int);
105 static void			vmbus_prichan_detach_task(void *, int);
106 static void			vmbus_subchan_detach_task(void *, int);
107 
108 static void			vmbus_chan_msgproc_choffer(struct vmbus_softc *,
109 				    const struct vmbus_message *);
110 static void			vmbus_chan_msgproc_chrescind(
111 				    struct vmbus_softc *,
112 				    const struct vmbus_message *);
113 
114 static int			vmbus_chan_printf(const struct vmbus_channel *,
115 				    const char *, ...) __printflike(2, 3);
116 
117 /*
118  * Vmbus channel message processing.
119  */
120 static const vmbus_chanmsg_proc_t
121 vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = {
122 	VMBUS_CHANMSG_PROC(CHOFFER,	vmbus_chan_msgproc_choffer),
123 	VMBUS_CHANMSG_PROC(CHRESCIND,	vmbus_chan_msgproc_chrescind),
124 
125 	VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP),
126 	VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP),
127 	VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP)
128 };
129 
130 /*
131  * Notify host that there are data pending on our TX bufring or
132  * we have put some data on the TX bufring.
133  */
134 static __inline void
135 vmbus_chan_signal(const struct vmbus_channel *chan)
136 {
137 	atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask);
138 	if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
139 		atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask);
140 	else
141 		hypercall_signal_event(pmap_kextract(
142 		    (vm_offset_t)chan->ch_monprm));
143 }
144 
145 static __inline void
146 vmbus_chan_signal_tx(struct vmbus_channel *chan)
147 {
148 	chan->ch_txbr.txbr_intrcnt ++;
149 
150 	vmbus_chan_signal(chan);
151 }
152 
153 static __inline void
154 vmbus_chan_signal_rx(struct vmbus_channel *chan)
155 {
156 	chan->ch_rxbr.rxbr_intrcnt ++;
157 
158 	vmbus_chan_signal(chan);
159 }
160 
161 static void
162 vmbus_chan_ins_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
163 {
164 
165 	mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
166 	if (atomic_testandset_int(&chan->ch_stflags,
167 	    VMBUS_CHAN_ST_ONPRIL_SHIFT))
168 		panic("channel is already on the prilist");
169 	TAILQ_INSERT_TAIL(&sc->vmbus_prichans, chan, ch_prilink);
170 }
171 
172 static void
173 vmbus_chan_rem_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
174 {
175 
176 	mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
177 	if (atomic_testandclear_int(&chan->ch_stflags,
178 	    VMBUS_CHAN_ST_ONPRIL_SHIFT) == 0)
179 		panic("channel is not on the prilist");
180 	TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink);
181 }
182 
183 static void
184 vmbus_chan_ins_sublist(struct vmbus_channel *prichan,
185     struct vmbus_channel *chan)
186 {
187 
188 	mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
189 
190 	if (atomic_testandset_int(&chan->ch_stflags,
191 	    VMBUS_CHAN_ST_ONSUBL_SHIFT))
192 		panic("channel is already on the sublist");
193 	TAILQ_INSERT_TAIL(&prichan->ch_subchans, chan, ch_sublink);
194 
195 	/* Bump sub-channel count. */
196 	prichan->ch_subchan_cnt++;
197 }
198 
199 static void
200 vmbus_chan_rem_sublist(struct vmbus_channel *prichan,
201     struct vmbus_channel *chan)
202 {
203 
204 	mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
205 
206 	KASSERT(prichan->ch_subchan_cnt > 0,
207 	    ("invalid subchan_cnt %d", prichan->ch_subchan_cnt));
208 	prichan->ch_subchan_cnt--;
209 
210 	if (atomic_testandclear_int(&chan->ch_stflags,
211 	    VMBUS_CHAN_ST_ONSUBL_SHIFT) == 0)
212 		panic("channel is not on the sublist");
213 	TAILQ_REMOVE(&prichan->ch_subchans, chan, ch_sublink);
214 }
215 
216 static void
217 vmbus_chan_ins_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
218 {
219 
220 	mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
221 	if (atomic_testandset_int(&chan->ch_stflags,
222 	    VMBUS_CHAN_ST_ONLIST_SHIFT))
223 		panic("channel is already on the list");
224 	TAILQ_INSERT_TAIL(&sc->vmbus_chans, chan, ch_link);
225 }
226 
227 static void
228 vmbus_chan_rem_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
229 {
230 
231 	mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
232 	if (atomic_testandclear_int(&chan->ch_stflags,
233 	    VMBUS_CHAN_ST_ONLIST_SHIFT) == 0)
234 		panic("channel is not on the list");
235 	TAILQ_REMOVE(&sc->vmbus_chans, chan, ch_link);
236 }
237 
238 static int
239 vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS)
240 {
241 	struct vmbus_channel *chan = arg1;
242 	int mnf = 0;
243 
244 	if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
245 		mnf = 1;
246 	return sysctl_handle_int(oidp, &mnf, 0, req);
247 }
248 
249 static void
250 vmbus_chan_sysctl_create(struct vmbus_channel *chan)
251 {
252 	struct sysctl_oid *ch_tree, *chid_tree, *br_tree;
253 	struct sysctl_ctx_list *ctx;
254 	uint32_t ch_id;
255 	char name[16];
256 
257 	/*
258 	 * Add sysctl nodes related to this channel to this
259 	 * channel's sysctl ctx, so that they can be destroyed
260 	 * independently upon close of this channel, which can
261 	 * happen even if the device is not detached.
262 	 */
263 	ctx = &chan->ch_sysctl_ctx;
264 	sysctl_ctx_init(ctx);
265 
266 	/*
267 	 * Create dev.NAME.UNIT.channel tree.
268 	 */
269 	ch_tree = SYSCTL_ADD_NODE(ctx,
270 	    SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)),
271 	    OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
272 	if (ch_tree == NULL)
273 		return;
274 
275 	/*
276 	 * Create dev.NAME.UNIT.channel.CHANID tree.
277 	 */
278 	if (VMBUS_CHAN_ISPRIMARY(chan))
279 		ch_id = chan->ch_id;
280 	else
281 		ch_id = chan->ch_prichan->ch_id;
282 	snprintf(name, sizeof(name), "%d", ch_id);
283 	chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
284 	    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
285 	if (chid_tree == NULL)
286 		return;
287 
288 	if (!VMBUS_CHAN_ISPRIMARY(chan)) {
289 		/*
290 		 * Create dev.NAME.UNIT.channel.CHANID.sub tree.
291 		 */
292 		ch_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree),
293 		    OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
294 		if (ch_tree == NULL)
295 			return;
296 
297 		/*
298 		 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree.
299 		 *
300 		 * NOTE:
301 		 * chid_tree is changed to this new sysctl tree.
302 		 */
303 		snprintf(name, sizeof(name), "%d", chan->ch_subidx);
304 		chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
305 		    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
306 		if (chid_tree == NULL)
307 			return;
308 
309 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
310 		    "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id");
311 	}
312 
313 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
314 	    "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id");
315 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
316 	    "mnf", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
317 	    chan, 0, vmbus_chan_sysctl_mnf, "I",
318 	    "has monitor notification facilities");
319 
320 	br_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
321 	    "br", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
322 	if (br_tree != NULL) {
323 		/*
324 		 * Create sysctl tree for RX bufring.
325 		 */
326 		vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr.rxbr, "rx");
327 		/*
328 		 * Create sysctl tree for TX bufring.
329 		 */
330 		vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr.txbr, "tx");
331 	}
332 }
333 
334 int
335 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size,
336     const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
337 {
338 	struct vmbus_chan_br cbr;
339 	int error;
340 
341 	/*
342 	 * Allocate the TX+RX bufrings.
343 	 */
344 	KASSERT(chan->ch_bufring == NULL, ("bufrings are allocated"));
345 	chan->ch_bufring_size = txbr_size + rxbr_size;
346 	chan->ch_bufring = contigmalloc(chan->ch_bufring_size, M_DEVBUF,
347 	    M_WAITOK | M_ZERO, 0ul, ~0ul, PAGE_SIZE, 0);
348 	if (chan->ch_bufring == NULL) {
349 		vmbus_chan_printf(chan, "bufring allocation failed\n");
350 		return (ENOMEM);
351 	}
352 
353 	cbr.cbr = chan->ch_bufring;
354 	cbr.cbr_paddr = pmap_kextract((vm_offset_t)chan->ch_bufring);
355 	cbr.cbr_txsz = txbr_size;
356 	cbr.cbr_rxsz = rxbr_size;
357 
358 	error = vmbus_chan_open_br(chan, &cbr, udata, udlen, cb, cbarg);
359 	if (error) {
360 		if (error == EISCONN) {
361 			/*
362 			 * XXX
363 			 * The bufring GPADL is still connected; abandon
364 			 * this bufring, instead of having mysterious
365 			 * crash or trashed data later on.
366 			 */
367 			vmbus_chan_printf(chan, "chan%u bufring GPADL "
368 			    "is still connected upon channel open error; "
369 			    "leak %d bytes memory\n", chan->ch_id,
370 			    txbr_size + rxbr_size);
371 		} else {
372 			contigfree(chan->ch_bufring, chan->ch_bufring_size,
373 			    M_DEVBUF);
374 		}
375 		chan->ch_bufring = NULL;
376 	}
377 	return (error);
378 }
379 
380 int
381 vmbus_chan_open_br(struct vmbus_channel *chan, const struct vmbus_chan_br *cbr,
382     const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
383 {
384 	struct vmbus_softc *sc = chan->ch_vmbus;
385 	const struct vmbus_message *msg;
386 	struct vmbus_chanmsg_chopen *req;
387 	struct vmbus_msghc *mh;
388 	uint32_t status;
389 	int error, txbr_size, rxbr_size;
390 	task_fn_t *task_fn;
391 	uint8_t *br;
392 
393 	if (udlen > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
394 		vmbus_chan_printf(chan,
395 		    "invalid udata len %d for chan%u\n", udlen, chan->ch_id);
396 		return (EINVAL);
397 	}
398 
399 	br = cbr->cbr;
400 	txbr_size = cbr->cbr_txsz;
401 	rxbr_size = cbr->cbr_rxsz;
402 	KASSERT((txbr_size & PAGE_MASK) == 0,
403 	    ("send bufring size is not multiple page"));
404 	KASSERT((rxbr_size & PAGE_MASK) == 0,
405 	    ("recv bufring size is not multiple page"));
406 	KASSERT((cbr->cbr_paddr & PAGE_MASK) == 0,
407 	    ("bufring is not page aligned"));
408 
409 	/*
410 	 * Zero out the TX/RX bufrings, in case that they were used before.
411 	 */
412 	memset(br, 0, txbr_size + rxbr_size);
413 
414 	if (atomic_testandset_int(&chan->ch_stflags,
415 	    VMBUS_CHAN_ST_OPENED_SHIFT))
416 		panic("double-open chan%u", chan->ch_id);
417 
418 	chan->ch_cb = cb;
419 	chan->ch_cbarg = cbarg;
420 
421 	vmbus_chan_update_evtflagcnt(sc, chan);
422 
423 	chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid);
424 	if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
425 		task_fn = vmbus_chan_task;
426 	else
427 		task_fn = vmbus_chan_task_nobatch;
428 	TASK_INIT(&chan->ch_task, 0, task_fn, chan);
429 
430 	/* TX bufring comes first */
431 	vmbus_txbr_setup(&chan->ch_txbr, br, txbr_size);
432 	/* RX bufring immediately follows TX bufring */
433 	vmbus_rxbr_setup(&chan->ch_rxbr, br + txbr_size, rxbr_size);
434 
435 	/* Create sysctl tree for this channel */
436 	vmbus_chan_sysctl_create(chan);
437 
438 	/*
439 	 * Connect the bufrings, both RX and TX, to this channel.
440 	 */
441 	error = vmbus_chan_gpadl_connect(chan, cbr->cbr_paddr,
442 	    txbr_size + rxbr_size, &chan->ch_bufring_gpadl);
443 	if (error) {
444 		vmbus_chan_printf(chan,
445 		    "failed to connect bufring GPADL to chan%u\n", chan->ch_id);
446 		goto failed;
447 	}
448 
449 	/*
450 	 * Install this channel, before it is opened, but after everything
451 	 * else has been setup.
452 	 */
453 	vmbus_chan_set_chmap(chan);
454 
455 	/*
456 	 * Open channel w/ the bufring GPADL on the target CPU.
457 	 */
458 	mh = vmbus_msghc_get(sc, sizeof(*req));
459 	if (mh == NULL) {
460 		vmbus_chan_printf(chan,
461 		    "can not get msg hypercall for chopen(chan%u)\n",
462 		    chan->ch_id);
463 		error = ENXIO;
464 		goto failed;
465 	}
466 
467 	req = vmbus_msghc_dataptr(mh);
468 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
469 	req->chm_chanid = chan->ch_id;
470 	req->chm_openid = chan->ch_id;
471 	req->chm_gpadl = chan->ch_bufring_gpadl;
472 	req->chm_vcpuid = chan->ch_vcpuid;
473 	req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT;
474 	if (udlen > 0)
475 		memcpy(req->chm_udata, udata, udlen);
476 
477 	error = vmbus_msghc_exec(sc, mh);
478 	if (error) {
479 		vmbus_chan_printf(chan,
480 		    "chopen(chan%u) msg hypercall exec failed: %d\n",
481 		    chan->ch_id, error);
482 		vmbus_msghc_put(sc, mh);
483 		goto failed;
484 	}
485 
486 	for (;;) {
487 		msg = vmbus_msghc_poll_result(sc, mh);
488 		if (msg != NULL)
489 			break;
490 		if (vmbus_chan_is_revoked(chan)) {
491 			int i;
492 
493 			/*
494 			 * NOTE:
495 			 * Hypervisor does _not_ send response CHOPEN to
496 			 * a revoked channel.
497 			 */
498 			vmbus_chan_printf(chan,
499 			    "chan%u is revoked, when it is being opened\n",
500 			    chan->ch_id);
501 
502 			/*
503 			 * XXX
504 			 * Add extra delay before cancel the hypercall
505 			 * execution; mainly to close any possible
506 			 * CHRESCIND and CHOPEN_RESP races on the
507 			 * hypervisor side.
508 			 */
509 #define REVOKE_LINGER	100
510 			for (i = 0; i < REVOKE_LINGER; ++i) {
511 				msg = vmbus_msghc_poll_result(sc, mh);
512 				if (msg != NULL)
513 					break;
514 				pause("rchopen", 1);
515 			}
516 #undef REVOKE_LINGER
517 			if (msg == NULL)
518 				vmbus_msghc_exec_cancel(sc, mh);
519 			break;
520 		}
521 		pause("chopen", 1);
522 	}
523 	if (msg != NULL) {
524 		status = ((const struct vmbus_chanmsg_chopen_resp *)
525 		    msg->msg_data)->chm_status;
526 	} else {
527 		/* XXX any non-0 value is ok here. */
528 		status = 0xff;
529 	}
530 
531 	vmbus_msghc_put(sc, mh);
532 
533 	if (status == 0) {
534 		if (bootverbose)
535 			vmbus_chan_printf(chan, "chan%u opened\n", chan->ch_id);
536 		return (0);
537 	}
538 
539 	vmbus_chan_printf(chan, "failed to open chan%u\n", chan->ch_id);
540 	error = ENXIO;
541 
542 failed:
543 	sysctl_ctx_free(&chan->ch_sysctl_ctx);
544 	vmbus_chan_clear_chmap(chan);
545 	if (chan->ch_bufring_gpadl != 0) {
546 		int error1;
547 
548 		error1 = vmbus_chan_gpadl_disconnect(chan,
549 		    chan->ch_bufring_gpadl);
550 		if (error1) {
551 			/*
552 			 * Give caller a hint that the bufring GPADL is still
553 			 * connected.
554 			 */
555 			error = EISCONN;
556 		}
557 		chan->ch_bufring_gpadl = 0;
558 	}
559 	atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
560 	return (error);
561 }
562 
563 int
564 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr,
565     int size, uint32_t *gpadl0)
566 {
567 	struct vmbus_softc *sc = chan->ch_vmbus;
568 	struct vmbus_msghc *mh;
569 	struct vmbus_chanmsg_gpadl_conn *req;
570 	const struct vmbus_message *msg;
571 	size_t reqsz;
572 	uint32_t gpadl, status;
573 	int page_count, range_len, i, cnt, error;
574 	uint64_t page_id;
575 
576 	KASSERT(*gpadl0 == 0, ("GPADL is not zero"));
577 
578 	/*
579 	 * Preliminary checks.
580 	 */
581 
582 	KASSERT((size & PAGE_MASK) == 0,
583 	    ("invalid GPA size %d, not multiple page size", size));
584 	page_count = size >> PAGE_SHIFT;
585 
586 	KASSERT((paddr & PAGE_MASK) == 0,
587 	    ("GPA is not page aligned %jx", (uintmax_t)paddr));
588 	page_id = paddr >> PAGE_SHIFT;
589 
590 	range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
591 	/*
592 	 * We don't support multiple GPA ranges.
593 	 */
594 	if (range_len > UINT16_MAX) {
595 		vmbus_chan_printf(chan, "GPA too large, %d pages\n",
596 		    page_count);
597 		return EOPNOTSUPP;
598 	}
599 
600 	/*
601 	 * Allocate GPADL id.
602 	 */
603 	gpadl = vmbus_gpadl_alloc(sc);
604 
605 	/*
606 	 * Connect this GPADL to the target channel.
607 	 *
608 	 * NOTE:
609 	 * Since each message can only hold small set of page
610 	 * addresses, several messages may be required to
611 	 * complete the connection.
612 	 */
613 	if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
614 		cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
615 	else
616 		cnt = page_count;
617 	page_count -= cnt;
618 
619 	reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
620 	    chm_range.gpa_page[cnt]);
621 	mh = vmbus_msghc_get(sc, reqsz);
622 	if (mh == NULL) {
623 		vmbus_chan_printf(chan,
624 		    "can not get msg hypercall for gpadl_conn(chan%u)\n",
625 		    chan->ch_id);
626 		return EIO;
627 	}
628 
629 	req = vmbus_msghc_dataptr(mh);
630 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
631 	req->chm_chanid = chan->ch_id;
632 	req->chm_gpadl = gpadl;
633 	req->chm_range_len = range_len;
634 	req->chm_range_cnt = 1;
635 	req->chm_range.gpa_len = size;
636 	req->chm_range.gpa_ofs = 0;
637 	for (i = 0; i < cnt; ++i)
638 		req->chm_range.gpa_page[i] = page_id++;
639 
640 	error = vmbus_msghc_exec(sc, mh);
641 	if (error) {
642 		vmbus_chan_printf(chan,
643 		    "gpadl_conn(chan%u) msg hypercall exec failed: %d\n",
644 		    chan->ch_id, error);
645 		vmbus_msghc_put(sc, mh);
646 		return error;
647 	}
648 
649 	while (page_count > 0) {
650 		struct vmbus_chanmsg_gpadl_subconn *subreq;
651 
652 		if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
653 			cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
654 		else
655 			cnt = page_count;
656 		page_count -= cnt;
657 
658 		reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
659 		    chm_gpa_page[cnt]);
660 		vmbus_msghc_reset(mh, reqsz);
661 
662 		subreq = vmbus_msghc_dataptr(mh);
663 		subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
664 		subreq->chm_gpadl = gpadl;
665 		for (i = 0; i < cnt; ++i)
666 			subreq->chm_gpa_page[i] = page_id++;
667 
668 		vmbus_msghc_exec_noresult(mh);
669 	}
670 	KASSERT(page_count == 0, ("invalid page count %d", page_count));
671 
672 	msg = vmbus_msghc_wait_result(sc, mh);
673 	status = ((const struct vmbus_chanmsg_gpadl_connresp *)
674 	    msg->msg_data)->chm_status;
675 
676 	vmbus_msghc_put(sc, mh);
677 
678 	if (status != 0) {
679 		vmbus_chan_printf(chan, "gpadl_conn(chan%u) failed: %u\n",
680 		    chan->ch_id, status);
681 		return EIO;
682 	}
683 
684 	/* Done; commit the GPADL id. */
685 	*gpadl0 = gpadl;
686 	if (bootverbose) {
687 		vmbus_chan_printf(chan, "gpadl_conn(chan%u) succeeded\n",
688 		    chan->ch_id);
689 	}
690 	return 0;
691 }
692 
693 static bool
694 vmbus_chan_wait_revoke(const struct vmbus_channel *chan, bool can_sleep)
695 {
696 #define WAIT_COUNT	200	/* 200ms */
697 
698 	int i;
699 
700 	for (i = 0; i < WAIT_COUNT; ++i) {
701 		if (vmbus_chan_is_revoked(chan))
702 			return (true);
703 		if (can_sleep)
704 			pause("wchrev", 1);
705 		else
706 			DELAY(1000);
707 	}
708 	return (false);
709 
710 #undef WAIT_COUNT
711 }
712 
713 /*
714  * Disconnect the GPA from the target channel
715  */
716 int
717 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl)
718 {
719 	struct vmbus_softc *sc = chan->ch_vmbus;
720 	struct vmbus_msghc *mh;
721 	struct vmbus_chanmsg_gpadl_disconn *req;
722 	int error;
723 
724 	KASSERT(gpadl != 0, ("GPADL is zero"));
725 
726 	mh = vmbus_msghc_get(sc, sizeof(*req));
727 	if (mh == NULL) {
728 		vmbus_chan_printf(chan,
729 		    "can not get msg hypercall for gpadl_disconn(chan%u)\n",
730 		    chan->ch_id);
731 		return (EBUSY);
732 	}
733 
734 	req = vmbus_msghc_dataptr(mh);
735 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
736 	req->chm_chanid = chan->ch_id;
737 	req->chm_gpadl = gpadl;
738 
739 	error = vmbus_msghc_exec(sc, mh);
740 	if (error) {
741 		vmbus_msghc_put(sc, mh);
742 
743 		if (vmbus_chan_wait_revoke(chan, true)) {
744 			/*
745 			 * Error is benign; this channel is revoked,
746 			 * so this GPADL will not be touched anymore.
747 			 */
748 			vmbus_chan_printf(chan,
749 			    "gpadl_disconn(revoked chan%u) msg hypercall "
750 			    "exec failed: %d\n", chan->ch_id, error);
751 			return (0);
752 		}
753 		vmbus_chan_printf(chan,
754 		    "gpadl_disconn(chan%u) msg hypercall exec failed: %d\n",
755 		    chan->ch_id, error);
756 		return (error);
757 	}
758 
759 	vmbus_msghc_wait_result(sc, mh);
760 	/* Discard result; no useful information */
761 	vmbus_msghc_put(sc, mh);
762 
763 	return (0);
764 }
765 
766 static void
767 vmbus_chan_detach(struct vmbus_channel *chan)
768 {
769 	int refs;
770 
771 	KASSERT(chan->ch_refs > 0, ("chan%u: invalid refcnt %d",
772 	    chan->ch_id, chan->ch_refs));
773 	refs = atomic_fetchadd_int(&chan->ch_refs, -1);
774 #ifdef INVARIANTS
775 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
776 		KASSERT(refs == 1, ("chan%u: invalid refcnt %d for prichan",
777 		    chan->ch_id, refs + 1));
778 	}
779 #endif
780 	if (refs == 1) {
781 		/*
782 		 * Detach the target channel.
783 		 */
784 		if (bootverbose) {
785 			vmbus_chan_printf(chan, "chan%u detached\n",
786 			    chan->ch_id);
787 		}
788 		taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
789 	}
790 }
791 
792 static void
793 vmbus_chan_clrchmap_task(void *xchan, int pending __unused)
794 {
795 	struct vmbus_channel *chan = xchan;
796 
797 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
798 }
799 
800 static void
801 vmbus_chan_clear_chmap(struct vmbus_channel *chan)
802 {
803 	struct task chmap_task;
804 
805 	TASK_INIT(&chmap_task, 0, vmbus_chan_clrchmap_task, chan);
806 	vmbus_chan_run_task(chan, &chmap_task);
807 }
808 
809 static void
810 vmbus_chan_set_chmap(struct vmbus_channel *chan)
811 {
812 	__compiler_membar();
813 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
814 }
815 
816 static void
817 vmbus_chan_poll_cancel_task(void *xchan, int pending __unused)
818 {
819 
820 	vmbus_chan_poll_cancel_intq(xchan);
821 }
822 
823 static void
824 vmbus_chan_poll_cancel(struct vmbus_channel *chan)
825 {
826 	struct task poll_cancel;
827 
828 	TASK_INIT(&poll_cancel, 0, vmbus_chan_poll_cancel_task, chan);
829 	vmbus_chan_run_task(chan, &poll_cancel);
830 }
831 
832 static int
833 vmbus_chan_close_internal(struct vmbus_channel *chan)
834 {
835 	struct vmbus_softc *sc = chan->ch_vmbus;
836 	struct vmbus_msghc *mh;
837 	struct vmbus_chanmsg_chclose *req;
838 	uint32_t old_stflags;
839 	int error;
840 
841 	/*
842 	 * NOTE:
843 	 * Sub-channels are closed upon their primary channel closing,
844 	 * so they can be closed even before they are opened.
845 	 */
846 	for (;;) {
847 		old_stflags = chan->ch_stflags;
848 		if (atomic_cmpset_int(&chan->ch_stflags, old_stflags,
849 		    old_stflags & ~VMBUS_CHAN_ST_OPENED))
850 			break;
851 	}
852 	if ((old_stflags & VMBUS_CHAN_ST_OPENED) == 0) {
853 		/* Not opened yet; done */
854 		if (bootverbose) {
855 			vmbus_chan_printf(chan, "chan%u not opened\n",
856 			    chan->ch_id);
857 		}
858 		return (0);
859 	}
860 
861 	/*
862 	 * Free this channel's sysctl tree attached to its device's
863 	 * sysctl tree.
864 	 */
865 	sysctl_ctx_free(&chan->ch_sysctl_ctx);
866 
867 	/*
868 	 * Cancel polling, if it is enabled.
869 	 */
870 	vmbus_chan_poll_cancel(chan);
871 
872 	/*
873 	 * NOTE:
874 	 * Order is critical.  This channel _must_ be uninstalled first,
875 	 * else the channel task may be enqueued by the IDT after it has
876 	 * been drained.
877 	 */
878 	vmbus_chan_clear_chmap(chan);
879 	taskqueue_drain(chan->ch_tq, &chan->ch_task);
880 	chan->ch_tq = NULL;
881 
882 	/*
883 	 * Close this channel.
884 	 */
885 	mh = vmbus_msghc_get(sc, sizeof(*req));
886 	if (mh == NULL) {
887 		vmbus_chan_printf(chan,
888 		    "can not get msg hypercall for chclose(chan%u)\n",
889 		    chan->ch_id);
890 		error = ENXIO;
891 		goto disconnect;
892 	}
893 
894 	req = vmbus_msghc_dataptr(mh);
895 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
896 	req->chm_chanid = chan->ch_id;
897 
898 	error = vmbus_msghc_exec_noresult(mh);
899 	vmbus_msghc_put(sc, mh);
900 
901 	if (error) {
902 		vmbus_chan_printf(chan,
903 		    "chclose(chan%u) msg hypercall exec failed: %d\n",
904 		    chan->ch_id, error);
905 		goto disconnect;
906 	}
907 
908 	if (bootverbose)
909 		vmbus_chan_printf(chan, "chan%u closed\n", chan->ch_id);
910 
911 disconnect:
912 	/*
913 	 * Disconnect the TX+RX bufrings from this channel.
914 	 */
915 	if (chan->ch_bufring_gpadl != 0) {
916 		int error1;
917 
918 		error1 = vmbus_chan_gpadl_disconnect(chan,
919 		    chan->ch_bufring_gpadl);
920 		if (error1) {
921 			/*
922 			 * XXX
923 			 * The bufring GPADL is still connected; abandon
924 			 * this bufring, instead of having mysterious
925 			 * crash or trashed data later on.
926 			 */
927 			vmbus_chan_printf(chan, "chan%u bufring GPADL "
928 			    "is still connected after close\n", chan->ch_id);
929 			chan->ch_bufring = NULL;
930 			/*
931 			 * Give caller a hint that the bufring GPADL is
932 			 * still connected.
933 			 */
934 			error = EISCONN;
935 		}
936 		chan->ch_bufring_gpadl = 0;
937 	}
938 
939 	/*
940 	 * Destroy the TX+RX bufrings.
941 	 */
942 	if (chan->ch_bufring != NULL) {
943 		contigfree(chan->ch_bufring, chan->ch_bufring_size, M_DEVBUF);
944 		chan->ch_bufring = NULL;
945 	}
946 	return (error);
947 }
948 
949 int
950 vmbus_chan_close_direct(struct vmbus_channel *chan)
951 {
952 	int error;
953 
954 #ifdef INVARIANTS
955 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
956 		struct vmbus_channel *subchan;
957 
958 		/*
959 		 * All sub-channels _must_ have been closed, or are _not_
960 		 * opened at all.
961 		 */
962 		mtx_lock(&chan->ch_subchan_lock);
963 		TAILQ_FOREACH(subchan, &chan->ch_subchans, ch_sublink) {
964 			KASSERT(
965 			   (subchan->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0,
966 			   ("chan%u: subchan%u is still opened",
967 			    chan->ch_id, subchan->ch_subidx));
968 		}
969 		mtx_unlock(&chan->ch_subchan_lock);
970 	}
971 #endif
972 
973 	error = vmbus_chan_close_internal(chan);
974 	if (!VMBUS_CHAN_ISPRIMARY(chan)) {
975 		/*
976 		 * This sub-channel is referenced, when it is linked to
977 		 * the primary channel; drop that reference now.
978 		 */
979 		vmbus_chan_detach(chan);
980 	}
981 	return (error);
982 }
983 
984 /*
985  * Caller should make sure that all sub-channels have
986  * been added to 'chan' and all to-be-closed channels
987  * are not being opened.
988  */
989 void
990 vmbus_chan_close(struct vmbus_channel *chan)
991 {
992 	int subchan_cnt;
993 
994 	if (!VMBUS_CHAN_ISPRIMARY(chan)) {
995 		/*
996 		 * Sub-channel is closed when its primary channel
997 		 * is closed; done.
998 		 */
999 		return;
1000 	}
1001 
1002 	/*
1003 	 * Close all sub-channels, if any.
1004 	 */
1005 	subchan_cnt = chan->ch_subchan_cnt;
1006 	if (subchan_cnt > 0) {
1007 		struct vmbus_channel **subchan;
1008 		int i;
1009 
1010 		subchan = vmbus_subchan_get(chan, subchan_cnt);
1011 		for (i = 0; i < subchan_cnt; ++i) {
1012 			vmbus_chan_close_internal(subchan[i]);
1013 			/*
1014 			 * This sub-channel is referenced, when it is
1015 			 * linked to the primary channel; drop that
1016 			 * reference now.
1017 			 */
1018 			vmbus_chan_detach(subchan[i]);
1019 		}
1020 		vmbus_subchan_rel(subchan, subchan_cnt);
1021 	}
1022 
1023 	/* Then close the primary channel. */
1024 	vmbus_chan_close_internal(chan);
1025 }
1026 
1027 void
1028 vmbus_chan_intr_drain(struct vmbus_channel *chan)
1029 {
1030 
1031 	taskqueue_drain(chan->ch_tq, &chan->ch_task);
1032 }
1033 
1034 uint32_t
1035 vmbus_chan_write_available(struct vmbus_channel *chan)
1036 {
1037 	return (vmbus_txbr_available(&chan->ch_txbr));
1038 }
1039 
1040 bool
1041 vmbus_chan_write_signal(struct vmbus_channel *chan,
1042     int32_t min_signal_size)
1043 {
1044 	if (min_signal_size >= 0 &&
1045 	    vmbus_chan_write_available(chan) > min_signal_size) {
1046 		return false;
1047 	}
1048 
1049 	if (!vmbus_txbr_get_imask(&chan->ch_txbr)) {
1050 		/* txbr imask is not set, signal the reader */
1051 		vmbus_chan_signal_tx(chan);
1052 		return true;
1053 	}
1054 
1055 	return false;
1056 }
1057 
1058 void
1059 vmbus_chan_set_pending_send_size(struct vmbus_channel *chan,
1060     uint32_t size)
1061 {
1062 	if (chan)
1063 		vmbus_txbr_set_pending_snd_sz(&chan->ch_txbr, size);
1064 }
1065 
1066 int
1067 vmbus_chan_iov_send(struct vmbus_channel *chan,
1068     const struct iovec iov[], int iovlen,
1069     vmbus_br_copy_callback_t cb, void *cbarg)
1070 {
1071 	int error;
1072 	boolean_t send_evt;
1073 
1074 	if (iovlen == 0)
1075 		return (0);
1076 
1077 	error = vmbus_txbr_write_call(&chan->ch_txbr, iov, iovlen,
1078 	    cb, cbarg, &send_evt);
1079 
1080 	if (!error && send_evt) {
1081 		vmbus_chan_signal_tx(chan);
1082 	}
1083 
1084 	return error;
1085 }
1086 
1087 int
1088 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags,
1089     void *data, int dlen, uint64_t xactid)
1090 {
1091 	struct vmbus_chanpkt pkt;
1092 	int pktlen, pad_pktlen, hlen, error;
1093 	uint64_t pad = 0;
1094 	struct iovec iov[3];
1095 	boolean_t send_evt;
1096 
1097 	hlen = sizeof(pkt);
1098 	pktlen = hlen + dlen;
1099 	pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1100 	KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1101 	    ("invalid packet size %d", pad_pktlen));
1102 
1103 	pkt.cp_hdr.cph_type = type;
1104 	pkt.cp_hdr.cph_flags = flags;
1105 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1106 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1107 	pkt.cp_hdr.cph_xactid = xactid;
1108 
1109 	iov[0].iov_base = &pkt;
1110 	iov[0].iov_len = hlen;
1111 	iov[1].iov_base = data;
1112 	iov[1].iov_len = dlen;
1113 	iov[2].iov_base = &pad;
1114 	iov[2].iov_len = pad_pktlen - pktlen;
1115 
1116 	error = vmbus_txbr_write(&chan->ch_txbr, iov, 3, &send_evt);
1117 	if (!error && send_evt)
1118 		vmbus_chan_signal_tx(chan);
1119 	return error;
1120 }
1121 
1122 int
1123 vmbus_chan_send_sglist(struct vmbus_channel *chan,
1124     struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid)
1125 {
1126 	struct vmbus_chanpkt_sglist pkt;
1127 	int pktlen, pad_pktlen, hlen, error;
1128 	struct iovec iov[4];
1129 	boolean_t send_evt;
1130 	uint64_t pad = 0;
1131 
1132 	hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]);
1133 	pktlen = hlen + dlen;
1134 	pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1135 	KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1136 	    ("invalid packet size %d", pad_pktlen));
1137 
1138 	pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1139 	pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1140 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1141 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1142 	pkt.cp_hdr.cph_xactid = xactid;
1143 	pkt.cp_rsvd = 0;
1144 	pkt.cp_gpa_cnt = sglen;
1145 
1146 	iov[0].iov_base = &pkt;
1147 	iov[0].iov_len = sizeof(pkt);
1148 	iov[1].iov_base = sg;
1149 	iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen;
1150 	iov[2].iov_base = data;
1151 	iov[2].iov_len = dlen;
1152 	iov[3].iov_base = &pad;
1153 	iov[3].iov_len = pad_pktlen - pktlen;
1154 
1155 	error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1156 	if (!error && send_evt)
1157 		vmbus_chan_signal_tx(chan);
1158 	return error;
1159 }
1160 
1161 int
1162 vmbus_chan_send_prplist(struct vmbus_channel *chan,
1163     struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen,
1164     uint64_t xactid)
1165 {
1166 	struct vmbus_chanpkt_prplist pkt;
1167 	int pktlen, pad_pktlen, hlen, error;
1168 	struct iovec iov[4];
1169 	boolean_t send_evt;
1170 	uint64_t pad = 0;
1171 
1172 	hlen = __offsetof(struct vmbus_chanpkt_prplist,
1173 	    cp_range[0].gpa_page[prp_cnt]);
1174 	pktlen = hlen + dlen;
1175 	pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1176 	KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1177 	    ("invalid packet size %d", pad_pktlen));
1178 
1179 	pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1180 	pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1181 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1182 	VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1183 	pkt.cp_hdr.cph_xactid = xactid;
1184 	pkt.cp_rsvd = 0;
1185 	pkt.cp_range_cnt = 1;
1186 
1187 	iov[0].iov_base = &pkt;
1188 	iov[0].iov_len = sizeof(pkt);
1189 	iov[1].iov_base = prp;
1190 	iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]);
1191 	iov[2].iov_base = data;
1192 	iov[2].iov_len = dlen;
1193 	iov[3].iov_base = &pad;
1194 	iov[3].iov_len = pad_pktlen - pktlen;
1195 
1196 	error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1197 	if (!error && send_evt)
1198 		vmbus_chan_signal_tx(chan);
1199 	return error;
1200 }
1201 
1202 int
1203 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0,
1204     uint64_t *xactid)
1205 {
1206 	struct vmbus_chanpkt_hdr pkt;
1207 	int error, dlen, hlen;
1208 
1209 	error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
1210 	if (error)
1211 		return (error);
1212 
1213 	if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1214 		vmbus_chan_printf(chan, "invalid hlen %u\n", pkt.cph_hlen);
1215 		/* XXX this channel is dead actually. */
1216 		return (EIO);
1217 	}
1218 	if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) {
1219 		vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1220 		    pkt.cph_hlen, pkt.cph_tlen);
1221 		/* XXX this channel is dead actually. */
1222 		return (EIO);
1223 	}
1224 
1225 	hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen);
1226 	dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
1227 
1228 	if (*dlen0 < dlen) {
1229 		/* Return the size of this packet's data. */
1230 		*dlen0 = dlen;
1231 		return (ENOBUFS);
1232 	}
1233 
1234 	*xactid = pkt.cph_xactid;
1235 	*dlen0 = dlen;
1236 
1237 	/* Skip packet header */
1238 	error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen);
1239 	KASSERT(!error, ("vmbus_rxbr_read failed"));
1240 
1241 	return (0);
1242 }
1243 
1244 int
1245 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
1246     struct vmbus_chanpkt_hdr *pkt, int *pktlen0)
1247 {
1248 	int error, pktlen, pkt_hlen;
1249 
1250 	pkt_hlen = sizeof(*pkt);
1251 	error = vmbus_rxbr_peek(&chan->ch_rxbr, pkt, pkt_hlen);
1252 	if (error)
1253 		return (error);
1254 
1255 	if (__predict_false(pkt->cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1256 		vmbus_chan_printf(chan, "invalid hlen %u\n", pkt->cph_hlen);
1257 		/* XXX this channel is dead actually. */
1258 		return (EIO);
1259 	}
1260 	if (__predict_false(pkt->cph_hlen > pkt->cph_tlen)) {
1261 		vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1262 		    pkt->cph_hlen, pkt->cph_tlen);
1263 		/* XXX this channel is dead actually. */
1264 		return (EIO);
1265 	}
1266 
1267 	pktlen = VMBUS_CHANPKT_GETLEN(pkt->cph_tlen);
1268 	if (*pktlen0 < pktlen) {
1269 		/* Return the size of this packet. */
1270 		*pktlen0 = pktlen;
1271 		return (ENOBUFS);
1272 	}
1273 	*pktlen0 = pktlen;
1274 
1275 	/*
1276 	 * Skip the fixed-size packet header, which has been filled
1277 	 * by the above vmbus_rxbr_peek().
1278 	 */
1279 	error = vmbus_rxbr_read(&chan->ch_rxbr, pkt + 1,
1280 	    pktlen - pkt_hlen, pkt_hlen);
1281 	KASSERT(!error, ("vmbus_rxbr_read failed"));
1282 
1283 	return (0);
1284 }
1285 
1286 uint32_t
1287 vmbus_chan_read_available(struct vmbus_channel *chan)
1288 {
1289 	return (vmbus_rxbr_available(&chan->ch_rxbr));
1290 }
1291 
1292 /*
1293  * This routine does:
1294  *     - Advance the channel read index for 'advance' bytes
1295  *     - Copy data_len bytes in to the buffer pointed by 'data'
1296  * Return 0 if operation succeed. EAGAIN if operations if failed.
1297  * If failed, the buffer pointed by 'data' is intact, and the
1298  * channel read index is not advanced at all.
1299  */
1300 int
1301 vmbus_chan_recv_peek(struct vmbus_channel *chan,
1302     void *data, int data_len, uint32_t advance)
1303 {
1304 	int error;
1305 	boolean_t sig_event;
1306 
1307 	if (data == NULL || data_len <= 0)
1308 		return (EINVAL);
1309 
1310 	error = vmbus_rxbr_idxadv_peek(&chan->ch_rxbr,
1311 	    data, data_len, advance, &sig_event);
1312 
1313 	if (!error && sig_event) {
1314 		vmbus_chan_signal_rx(chan);
1315 	}
1316 
1317 	return (error);
1318 }
1319 
1320 /*
1321  * This routine does:
1322  *     - Advance the channel read index for 'advance' bytes
1323  */
1324 int
1325 vmbus_chan_recv_idxadv(struct vmbus_channel *chan, uint32_t advance)
1326 {
1327 	int error;
1328 	boolean_t sig_event;
1329 
1330 	if (advance == 0)
1331 		return (EINVAL);
1332 
1333 	error = vmbus_rxbr_idxadv(&chan->ch_rxbr, advance, &sig_event);
1334 
1335 	if (!error && sig_event) {
1336 		vmbus_chan_signal_rx(chan);
1337 	}
1338 
1339 	return (error);
1340 }
1341 
1342 
1343 /*
1344  * Caller should hold its own lock to serialize the ring buffer
1345  * copy.
1346  */
1347 int
1348 vmbus_chan_recv_peek_call(struct vmbus_channel *chan, int data_len,
1349     uint32_t skip, vmbus_br_copy_callback_t cb, void *cbarg)
1350 {
1351 	if (!chan || data_len <= 0 || cb == NULL)
1352 		return (EINVAL);
1353 
1354 	return (vmbus_rxbr_peek_call(&chan->ch_rxbr, data_len, skip,
1355 	    cb, cbarg));
1356 }
1357 
1358 static void
1359 vmbus_chan_task(void *xchan, int pending __unused)
1360 {
1361 	struct vmbus_channel *chan = xchan;
1362 	vmbus_chan_callback_t cb = chan->ch_cb;
1363 	void *cbarg = chan->ch_cbarg;
1364 
1365 	KASSERT(chan->ch_poll_intvl == 0,
1366 	    ("chan%u: interrupted in polling mode", chan->ch_id));
1367 
1368 	/*
1369 	 * Optimize host to guest signaling by ensuring:
1370 	 * 1. While reading the channel, we disable interrupts from
1371 	 *    host.
1372 	 * 2. Ensure that we process all posted messages from the host
1373 	 *    before returning from this callback.
1374 	 * 3. Once we return, enable signaling from the host. Once this
1375 	 *    state is set we check to see if additional packets are
1376 	 *    available to read. In this case we repeat the process.
1377 	 *
1378 	 * NOTE: Interrupt has been disabled in the ISR.
1379 	 */
1380 	for (;;) {
1381 		uint32_t left;
1382 
1383 		cb(chan, cbarg);
1384 
1385 		left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1386 		if (left == 0) {
1387 			/* No more data in RX bufring; done */
1388 			break;
1389 		}
1390 		vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1391 	}
1392 }
1393 
1394 static void
1395 vmbus_chan_task_nobatch(void *xchan, int pending __unused)
1396 {
1397 	struct vmbus_channel *chan = xchan;
1398 
1399 	KASSERT(chan->ch_poll_intvl == 0,
1400 	    ("chan%u: interrupted in polling mode", chan->ch_id));
1401 	chan->ch_cb(chan, chan->ch_cbarg);
1402 }
1403 
1404 static void
1405 vmbus_chan_poll_timeout(void *xchan)
1406 {
1407 	struct vmbus_channel *chan = xchan;
1408 
1409 	KASSERT(chan->ch_poll_intvl != 0,
1410 	    ("chan%u: polling timeout in interrupt mode", chan->ch_id));
1411 	taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1412 }
1413 
1414 static void
1415 vmbus_chan_poll_task(void *xchan, int pending __unused)
1416 {
1417 	struct vmbus_channel *chan = xchan;
1418 
1419 	KASSERT(chan->ch_poll_intvl != 0,
1420 	    ("chan%u: polling in interrupt mode", chan->ch_id));
1421 	callout_reset_sbt_curcpu(&chan->ch_poll_timeo, chan->ch_poll_intvl, 0,
1422 	    vmbus_chan_poll_timeout, chan, chan->ch_poll_flags);
1423 	chan->ch_cb(chan, chan->ch_cbarg);
1424 }
1425 
1426 static void
1427 vmbus_chan_pollcfg_task(void *xarg, int pending __unused)
1428 {
1429 	const struct vmbus_chan_pollarg *arg = xarg;
1430 	struct vmbus_channel *chan = arg->poll_chan;
1431 	sbintime_t intvl;
1432 	int poll_flags;
1433 
1434 	/*
1435 	 * Save polling interval.
1436 	 */
1437 	intvl = SBT_1S / arg->poll_hz;
1438 	if (intvl == 0)
1439 		intvl = 1;
1440 	if (intvl == chan->ch_poll_intvl) {
1441 		/* Nothing changes; done */
1442 		return;
1443 	}
1444 	chan->ch_poll_intvl = intvl;
1445 
1446 	/* Adjust callout flags. */
1447 	poll_flags = C_DIRECT_EXEC;
1448 	if (arg->poll_hz <= hz)
1449 		poll_flags |= C_HARDCLOCK;
1450 	chan->ch_poll_flags = poll_flags;
1451 
1452 	/*
1453 	 * Disconnect this channel from the channel map to make sure that
1454 	 * the RX bufring interrupt enabling bit can not be touched, and
1455 	 * ISR can not enqueue this channel task anymore.  THEN, disable
1456 	 * interrupt from the RX bufring (TX bufring does not generate
1457 	 * interrupt to VM).
1458 	 *
1459 	 * NOTE: order is critical.
1460 	 */
1461 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
1462 	__compiler_membar();
1463 	vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1464 
1465 	/*
1466 	 * NOTE:
1467 	 * At this point, this channel task will not be enqueued by
1468 	 * the ISR anymore, time to cancel the pending one.
1469 	 */
1470 	taskqueue_cancel(chan->ch_tq, &chan->ch_task, NULL);
1471 
1472 	/* Kick start! */
1473 	taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1474 }
1475 
1476 static bool
1477 vmbus_chan_poll_cancel_intq(struct vmbus_channel *chan)
1478 {
1479 
1480 	if (chan->ch_poll_intvl == 0) {
1481 		/* Not enabled. */
1482 		return (false);
1483 	}
1484 
1485 	/*
1486 	 * Stop polling callout, so that channel polling task
1487 	 * will not be enqueued anymore.
1488 	 */
1489 	callout_drain(&chan->ch_poll_timeo);
1490 
1491 	/*
1492 	 * Disable polling by resetting polling interval.
1493 	 *
1494 	 * NOTE:
1495 	 * The polling interval resetting MUST be conducted
1496 	 * after the callout is drained; mainly to keep the
1497 	 * proper assertion in place.
1498 	 */
1499 	chan->ch_poll_intvl = 0;
1500 
1501 	/*
1502 	 * NOTE:
1503 	 * At this point, this channel polling task will not be
1504 	 * enqueued by the callout anymore, time to cancel the
1505 	 * pending one.
1506 	 */
1507 	taskqueue_cancel(chan->ch_tq, &chan->ch_poll_task, NULL);
1508 
1509 	/* Polling was enabled. */
1510 	return (true);
1511 }
1512 
1513 static void
1514 vmbus_chan_polldis_task(void *xchan, int pending __unused)
1515 {
1516 	struct vmbus_channel *chan = xchan;
1517 
1518 	if (!vmbus_chan_poll_cancel_intq(chan)) {
1519 		/* Already disabled; done. */
1520 		return;
1521 	}
1522 
1523 	/*
1524 	 * Plug this channel back to the channel map and unmask
1525 	 * the RX bufring interrupt.
1526 	 */
1527 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
1528 	__compiler_membar();
1529 	vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1530 
1531 	/*
1532 	 * Kick start the interrupt task, just in case unmasking
1533 	 * interrupt races ISR.
1534 	 */
1535 	taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1536 }
1537 
1538 static __inline void
1539 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
1540     int flag_cnt)
1541 {
1542 	int f;
1543 
1544 	for (f = 0; f < flag_cnt; ++f) {
1545 		uint32_t chid_base;
1546 		u_long flags;
1547 		int chid_ofs;
1548 
1549 		if (event_flags[f] == 0)
1550 			continue;
1551 
1552 		flags = atomic_swap_long(&event_flags[f], 0);
1553 		chid_base = f << VMBUS_EVTFLAG_SHIFT;
1554 
1555 		while ((chid_ofs = ffsl(flags)) != 0) {
1556 			struct vmbus_channel *chan;
1557 
1558 			--chid_ofs; /* NOTE: ffsl is 1-based */
1559 			flags &= ~(1UL << chid_ofs);
1560 
1561 			chan = sc->vmbus_chmap[chid_base + chid_ofs];
1562 			if (__predict_false(chan == NULL)) {
1563 				/* Channel is closed. */
1564 				continue;
1565 			}
1566 			__compiler_membar();
1567 
1568 			if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
1569 				vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1570 			taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1571 		}
1572 	}
1573 }
1574 
1575 void
1576 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
1577 {
1578 	struct vmbus_evtflags *eventf;
1579 
1580 	/*
1581 	 * On Host with Win8 or above, the event page can be checked directly
1582 	 * to get the id of the channel that has the pending interrupt.
1583 	 */
1584 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1585 	vmbus_event_flags_proc(sc, eventf->evt_flags,
1586 	    VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
1587 }
1588 
1589 void
1590 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
1591 {
1592 	struct vmbus_evtflags *eventf;
1593 
1594 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1595 	if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
1596 		vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
1597 		    VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
1598 	}
1599 }
1600 
1601 static void
1602 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
1603     const struct vmbus_channel *chan)
1604 {
1605 	volatile int *flag_cnt_ptr;
1606 	int flag_cnt;
1607 
1608 	flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
1609 	flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
1610 
1611 	for (;;) {
1612 		int old_flag_cnt;
1613 
1614 		old_flag_cnt = *flag_cnt_ptr;
1615 		if (old_flag_cnt >= flag_cnt)
1616 			break;
1617 		if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
1618 			if (bootverbose) {
1619 				vmbus_chan_printf(chan,
1620 				    "chan%u update cpu%d flag_cnt to %d\n",
1621 				    chan->ch_id, chan->ch_cpuid, flag_cnt);
1622 			}
1623 			break;
1624 		}
1625 	}
1626 }
1627 
1628 static struct vmbus_channel *
1629 vmbus_chan_alloc(struct vmbus_softc *sc)
1630 {
1631 	struct vmbus_channel *chan;
1632 
1633 	chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
1634 
1635 	chan->ch_monprm = contigmalloc(sizeof(struct hyperv_mon_param),
1636 	    M_DEVBUF, M_WAITOK | M_ZERO, 0ul, ~0ul, HYPERCALL_PARAM_ALIGN, 0);
1637 	if (chan->ch_monprm == NULL) {
1638 		device_printf(sc->vmbus_dev, "monprm alloc failed\n");
1639 		free(chan, M_DEVBUF);
1640 		return NULL;
1641 	}
1642 
1643 	chan->ch_refs = 1;
1644 	chan->ch_vmbus = sc;
1645 	mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
1646 	sx_init(&chan->ch_orphan_lock, "vmbus chorphan");
1647 	TAILQ_INIT(&chan->ch_subchans);
1648 	vmbus_rxbr_init(&chan->ch_rxbr);
1649 	vmbus_txbr_init(&chan->ch_txbr);
1650 
1651 	TASK_INIT(&chan->ch_poll_task, 0, vmbus_chan_poll_task, chan);
1652 	callout_init(&chan->ch_poll_timeo, 1);
1653 
1654 	return chan;
1655 }
1656 
1657 static void
1658 vmbus_chan_free(struct vmbus_channel *chan)
1659 {
1660 
1661 	KASSERT(TAILQ_EMPTY(&chan->ch_subchans) && chan->ch_subchan_cnt == 0,
1662 	    ("still owns sub-channels"));
1663 	KASSERT((chan->ch_stflags &
1664 	    (VMBUS_CHAN_ST_OPENED |
1665 	     VMBUS_CHAN_ST_ONPRIL |
1666 	     VMBUS_CHAN_ST_ONSUBL |
1667 	     VMBUS_CHAN_ST_ONLIST)) == 0, ("free busy channel"));
1668 	KASSERT(chan->ch_orphan_xact == NULL,
1669 	    ("still has orphan xact installed"));
1670 	KASSERT(chan->ch_refs == 0, ("chan%u: invalid refcnt %d",
1671 	    chan->ch_id, chan->ch_refs));
1672 	KASSERT(chan->ch_poll_intvl == 0, ("chan%u: polling is activated",
1673 	    chan->ch_id));
1674 
1675 	contigfree(chan->ch_monprm, sizeof(struct hyperv_mon_param), M_DEVBUF);
1676 	mtx_destroy(&chan->ch_subchan_lock);
1677 	sx_destroy(&chan->ch_orphan_lock);
1678 	vmbus_rxbr_deinit(&chan->ch_rxbr);
1679 	vmbus_txbr_deinit(&chan->ch_txbr);
1680 	free(chan, M_DEVBUF);
1681 }
1682 
1683 static int
1684 vmbus_chan_add(struct vmbus_channel *newchan)
1685 {
1686 	struct vmbus_softc *sc = newchan->ch_vmbus;
1687 	struct vmbus_channel *prichan;
1688 
1689 	if (newchan->ch_id == 0) {
1690 		/*
1691 		 * XXX
1692 		 * Chan0 will neither be processed nor should be offered;
1693 		 * skip it.
1694 		 */
1695 		device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
1696 		return EINVAL;
1697 	} else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
1698 		device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
1699 		    newchan->ch_id);
1700 		return EINVAL;
1701 	}
1702 
1703 	mtx_lock(&sc->vmbus_prichan_lock);
1704 	TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
1705 		/*
1706 		 * Sub-channel will have the same type GUID and instance
1707 		 * GUID as its primary channel.
1708 		 */
1709 		if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
1710 		    sizeof(struct hyperv_guid)) == 0 &&
1711 		    memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
1712 		    sizeof(struct hyperv_guid)) == 0)
1713 			break;
1714 	}
1715 	if (VMBUS_CHAN_ISPRIMARY(newchan)) {
1716 		if (prichan == NULL) {
1717 			/* Install the new primary channel */
1718 			vmbus_chan_ins_prilist(sc, newchan);
1719 			mtx_unlock(&sc->vmbus_prichan_lock);
1720 			goto done;
1721 		} else {
1722 			mtx_unlock(&sc->vmbus_prichan_lock);
1723 			device_printf(sc->vmbus_dev,
1724 			    "duplicated primary chan%u\n", newchan->ch_id);
1725 			return EINVAL;
1726 		}
1727 	} else { /* Sub-channel */
1728 		if (prichan == NULL) {
1729 			mtx_unlock(&sc->vmbus_prichan_lock);
1730 			device_printf(sc->vmbus_dev,
1731 			    "no primary chan for chan%u\n", newchan->ch_id);
1732 			return EINVAL;
1733 		}
1734 		/*
1735 		 * Found the primary channel for this sub-channel and
1736 		 * move on.
1737 		 *
1738 		 * XXX refcnt prichan
1739 		 */
1740 	}
1741 	mtx_unlock(&sc->vmbus_prichan_lock);
1742 
1743 	/*
1744 	 * This is a sub-channel; link it with the primary channel.
1745 	 */
1746 	KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan),
1747 	    ("new channel is not sub-channel"));
1748 	KASSERT(prichan != NULL, ("no primary channel"));
1749 
1750 	/*
1751 	 * Reference count this sub-channel; it will be dereferenced
1752 	 * when this sub-channel is closed.
1753 	 */
1754 	KASSERT(newchan->ch_refs == 1, ("chan%u: invalid refcnt %d",
1755 	    newchan->ch_id, newchan->ch_refs));
1756 	atomic_add_int(&newchan->ch_refs, 1);
1757 
1758 	newchan->ch_prichan = prichan;
1759 	newchan->ch_dev = prichan->ch_dev;
1760 
1761 	mtx_lock(&prichan->ch_subchan_lock);
1762 	vmbus_chan_ins_sublist(prichan, newchan);
1763 	mtx_unlock(&prichan->ch_subchan_lock);
1764 	/*
1765 	 * Notify anyone that is interested in this sub-channel,
1766 	 * after this sub-channel is setup.
1767 	 */
1768 	wakeup(prichan);
1769 done:
1770 	/*
1771 	 * Hook this channel up for later revocation.
1772 	 */
1773 	mtx_lock(&sc->vmbus_chan_lock);
1774 	vmbus_chan_ins_list(sc, newchan);
1775 	mtx_unlock(&sc->vmbus_chan_lock);
1776 
1777 	if (bootverbose) {
1778 		vmbus_chan_printf(newchan, "chan%u subidx%u offer\n",
1779 		    newchan->ch_id, newchan->ch_subidx);
1780 	}
1781 
1782 	/* Select default cpu for this channel. */
1783 	vmbus_chan_cpu_default(newchan);
1784 
1785 	return 0;
1786 }
1787 
1788 void
1789 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1790 {
1791 	KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
1792 
1793 	if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1794 	    chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1795 		/* Only cpu0 is supported */
1796 		cpu = 0;
1797 	}
1798 
1799 	chan->ch_cpuid = cpu;
1800 	chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1801 
1802 	if (bootverbose) {
1803 		vmbus_chan_printf(chan,
1804 		    "chan%u assigned to cpu%u [vcpu%u]\n",
1805 		    chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1806 	}
1807 }
1808 
1809 void
1810 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1811 {
1812 	static uint32_t vmbus_chan_nextcpu;
1813 	int cpu;
1814 
1815 	cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
1816 	vmbus_chan_cpu_set(chan, cpu);
1817 }
1818 
1819 static void
1820 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1821 {
1822 	/*
1823 	 * By default, pin the channel to cpu0.  Devices having
1824 	 * special channel-cpu mapping requirement should call
1825 	 * vmbus_chan_cpu_{set,rr}().
1826 	 */
1827 	vmbus_chan_cpu_set(chan, 0);
1828 }
1829 
1830 static void
1831 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc,
1832     const struct vmbus_message *msg)
1833 {
1834 	const struct vmbus_chanmsg_choffer *offer;
1835 	struct vmbus_channel *chan;
1836 	task_fn_t *detach_fn, *attach_fn;
1837 	int error;
1838 
1839 	offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1840 
1841 	chan = vmbus_chan_alloc(sc);
1842 	if (chan == NULL) {
1843 		device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1844 		    offer->chm_chanid);
1845 		return;
1846 	}
1847 
1848 	chan->ch_id = offer->chm_chanid;
1849 	chan->ch_subidx = offer->chm_subidx;
1850 	chan->ch_guid_type = offer->chm_chtype;
1851 	chan->ch_guid_inst = offer->chm_chinst;
1852 
1853 	/* Batch reading is on by default */
1854 	chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1855 
1856 	chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1857 	if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1858 		chan->ch_monprm->mp_connid = offer->chm_connid;
1859 
1860 	if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1861 		int trig_idx;
1862 
1863 		/*
1864 		 * Setup MNF stuffs.
1865 		 */
1866 		chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1867 
1868 		trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1869 		if (trig_idx >= VMBUS_MONTRIGS_MAX)
1870 			panic("invalid monitor trigger %u", offer->chm_montrig);
1871 		chan->ch_montrig =
1872 		    &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1873 
1874 		chan->ch_montrig_mask =
1875 		    1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1876 	}
1877 
1878 	if (offer->chm_chflags & VMBUS_CHAN_TLNPI_PROVIDER_OFFER) {
1879 		/* This is HyperV socket channel */
1880 		chan->ch_is_hvs = true;
1881 		/* The first byte != 0 means the host initiated connection. */
1882 		chan->ch_hvs_conn_from_host =
1883 		    offer->chm_udata.pipe.user_def[0];
1884 
1885 		if (bootverbose) {
1886 			device_printf(sc->vmbus_dev,
1887 			    "chan%u is hyperv socket channel "
1888 			    "connected %s host\n",
1889 			    chan->ch_id,
1890 			    (chan->ch_hvs_conn_from_host != 0) ?
1891 			    "from" : "to");
1892 		}
1893 	} else {
1894 		chan->ch_is_hvs = false;
1895 	}
1896 
1897 	/*
1898 	 * Setup event flag.
1899 	 */
1900 	chan->ch_evtflag =
1901 	    &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1902 	chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1903 
1904 	/*
1905 	 * Setup attach and detach tasks.
1906 	 */
1907 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
1908 		chan->ch_mgmt_tq = sc->vmbus_devtq;
1909 		attach_fn = vmbus_prichan_attach_task;
1910 		detach_fn = vmbus_prichan_detach_task;
1911 	} else {
1912 		chan->ch_mgmt_tq = sc->vmbus_subchtq;
1913 		attach_fn = vmbus_subchan_attach_task;
1914 		detach_fn = vmbus_subchan_detach_task;
1915 	}
1916 	TASK_INIT(&chan->ch_attach_task, 0, attach_fn, chan);
1917 	TASK_INIT(&chan->ch_detach_task, 0, detach_fn, chan);
1918 
1919 	error = vmbus_chan_add(chan);
1920 	if (error) {
1921 		device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1922 		    chan->ch_id, error);
1923 		atomic_subtract_int(&chan->ch_refs, 1);
1924 		vmbus_chan_free(chan);
1925 		return;
1926 	}
1927 	taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_attach_task);
1928 }
1929 
1930 static void
1931 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc,
1932     const struct vmbus_message *msg)
1933 {
1934 	const struct vmbus_chanmsg_chrescind *note;
1935 	struct vmbus_channel *chan;
1936 
1937 	note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1938 	if (note->chm_chanid > VMBUS_CHAN_MAX) {
1939 		device_printf(sc->vmbus_dev, "invalid revoked chan%u\n",
1940 		    note->chm_chanid);
1941 		return;
1942 	}
1943 
1944 	/*
1945 	 * Find and remove the target channel from the channel list.
1946 	 */
1947 	mtx_lock(&sc->vmbus_chan_lock);
1948 	TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
1949 		if (chan->ch_id == note->chm_chanid)
1950 			break;
1951 	}
1952 	if (chan == NULL) {
1953 		mtx_unlock(&sc->vmbus_chan_lock);
1954 		device_printf(sc->vmbus_dev, "chan%u is not offered\n",
1955 		    note->chm_chanid);
1956 		return;
1957 	}
1958 	vmbus_chan_rem_list(sc, chan);
1959 	mtx_unlock(&sc->vmbus_chan_lock);
1960 
1961 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
1962 		/*
1963 		 * The target channel is a primary channel; remove the
1964 		 * target channel from the primary channel list now,
1965 		 * instead of later, so that it will not be found by
1966 		 * other sub-channel offers, which are processed in
1967 		 * this thread.
1968 		 */
1969 		mtx_lock(&sc->vmbus_prichan_lock);
1970 		vmbus_chan_rem_prilist(sc, chan);
1971 		mtx_unlock(&sc->vmbus_prichan_lock);
1972 	}
1973 
1974 	/*
1975 	 * NOTE:
1976 	 * The following processing order is critical:
1977 	 * Set the REVOKED state flag before orphaning the installed xact.
1978 	 */
1979 
1980 	if (atomic_testandset_int(&chan->ch_stflags,
1981 	    VMBUS_CHAN_ST_REVOKED_SHIFT))
1982 		panic("channel has already been revoked");
1983 
1984 	sx_xlock(&chan->ch_orphan_lock);
1985 	if (chan->ch_orphan_xact != NULL)
1986 		vmbus_xact_ctx_orphan(chan->ch_orphan_xact);
1987 	sx_xunlock(&chan->ch_orphan_lock);
1988 
1989 	if (bootverbose)
1990 		vmbus_chan_printf(chan, "chan%u revoked\n", note->chm_chanid);
1991 	vmbus_chan_detach(chan);
1992 }
1993 
1994 static int
1995 vmbus_chan_release(struct vmbus_channel *chan)
1996 {
1997 	struct vmbus_softc *sc = chan->ch_vmbus;
1998 	struct vmbus_chanmsg_chfree *req;
1999 	struct vmbus_msghc *mh;
2000 	int error;
2001 
2002 	mh = vmbus_msghc_get(sc, sizeof(*req));
2003 	if (mh == NULL) {
2004 		vmbus_chan_printf(chan,
2005 		    "can not get msg hypercall for chfree(chan%u)\n",
2006 		    chan->ch_id);
2007 		return (ENXIO);
2008 	}
2009 
2010 	req = vmbus_msghc_dataptr(mh);
2011 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
2012 	req->chm_chanid = chan->ch_id;
2013 
2014 	error = vmbus_msghc_exec_noresult(mh);
2015 	vmbus_msghc_put(sc, mh);
2016 
2017 	if (error) {
2018 		vmbus_chan_printf(chan,
2019 		    "chfree(chan%u) msg hypercall exec failed: %d\n",
2020 		    chan->ch_id, error);
2021 	} else {
2022 		if (bootverbose)
2023 			vmbus_chan_printf(chan, "chan%u freed\n", chan->ch_id);
2024 	}
2025 	return (error);
2026 }
2027 
2028 static void
2029 vmbus_prichan_detach_task(void *xchan, int pending __unused)
2030 {
2031 	struct vmbus_channel *chan = xchan;
2032 
2033 	KASSERT(VMBUS_CHAN_ISPRIMARY(chan),
2034 	    ("chan%u is not primary channel", chan->ch_id));
2035 
2036 	/* Delete and detach the device associated with this channel. */
2037 	vmbus_delete_child(chan);
2038 
2039 	/* Release this channel (back to vmbus). */
2040 	vmbus_chan_release(chan);
2041 
2042 	/* Free this channel's resource. */
2043 	vmbus_chan_free(chan);
2044 }
2045 
2046 static void
2047 vmbus_subchan_detach_task(void *xchan, int pending __unused)
2048 {
2049 	struct vmbus_channel *chan = xchan;
2050 	struct vmbus_channel *pri_chan = chan->ch_prichan;
2051 
2052 	KASSERT(!VMBUS_CHAN_ISPRIMARY(chan),
2053 	    ("chan%u is primary channel", chan->ch_id));
2054 
2055 	/* Release this channel (back to vmbus). */
2056 	vmbus_chan_release(chan);
2057 
2058 	/* Unlink from its primary channel's sub-channel list. */
2059 	mtx_lock(&pri_chan->ch_subchan_lock);
2060 	vmbus_chan_rem_sublist(pri_chan, chan);
2061 	mtx_unlock(&pri_chan->ch_subchan_lock);
2062 	/* Notify anyone that is waiting for this sub-channel to vanish. */
2063 	wakeup(pri_chan);
2064 
2065 	/* Free this channel's resource. */
2066 	vmbus_chan_free(chan);
2067 }
2068 
2069 static void
2070 vmbus_prichan_attach_task(void *xchan, int pending __unused)
2071 {
2072 
2073 	/*
2074 	 * Add device for this primary channel.
2075 	 */
2076 	vmbus_add_child(xchan);
2077 }
2078 
2079 static void
2080 vmbus_subchan_attach_task(void *xchan __unused, int pending __unused)
2081 {
2082 
2083 	/* Nothing */
2084 }
2085 
2086 void
2087 vmbus_chan_destroy_all(struct vmbus_softc *sc)
2088 {
2089 
2090 	/*
2091 	 * Detach all devices and destroy the corresponding primary
2092 	 * channels.
2093 	 */
2094 	for (;;) {
2095 		struct vmbus_channel *chan;
2096 
2097 		mtx_lock(&sc->vmbus_chan_lock);
2098 		TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
2099 			if (VMBUS_CHAN_ISPRIMARY(chan))
2100 				break;
2101 		}
2102 		if (chan == NULL) {
2103 			/* No more primary channels; done. */
2104 			mtx_unlock(&sc->vmbus_chan_lock);
2105 			break;
2106 		}
2107 		vmbus_chan_rem_list(sc, chan);
2108 		mtx_unlock(&sc->vmbus_chan_lock);
2109 
2110 		mtx_lock(&sc->vmbus_prichan_lock);
2111 		vmbus_chan_rem_prilist(sc, chan);
2112 		mtx_unlock(&sc->vmbus_prichan_lock);
2113 
2114 		taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
2115 	}
2116 }
2117 
2118 struct vmbus_channel **
2119 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt)
2120 {
2121 	struct vmbus_channel **ret, *chan;
2122 	int i;
2123 
2124 	KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt));
2125 
2126 	ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP,
2127 	    M_WAITOK);
2128 
2129 	mtx_lock(&pri_chan->ch_subchan_lock);
2130 
2131 	while (pri_chan->ch_subchan_cnt < subchan_cnt)
2132 		mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
2133 
2134 	i = 0;
2135 	TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
2136 		/* TODO: refcnt chan */
2137 		ret[i] = chan;
2138 
2139 		++i;
2140 		if (i == subchan_cnt)
2141 			break;
2142 	}
2143 	KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
2144 	    pri_chan->ch_subchan_cnt, subchan_cnt));
2145 
2146 	mtx_unlock(&pri_chan->ch_subchan_lock);
2147 
2148 	return ret;
2149 }
2150 
2151 void
2152 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused)
2153 {
2154 
2155 	free(subchan, M_TEMP);
2156 }
2157 
2158 void
2159 vmbus_subchan_drain(struct vmbus_channel *pri_chan)
2160 {
2161 	mtx_lock(&pri_chan->ch_subchan_lock);
2162 	while (pri_chan->ch_subchan_cnt > 0)
2163 		mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
2164 	mtx_unlock(&pri_chan->ch_subchan_lock);
2165 }
2166 
2167 void
2168 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
2169 {
2170 	vmbus_chanmsg_proc_t msg_proc;
2171 	uint32_t msg_type;
2172 
2173 	msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
2174 	KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX,
2175 	    ("invalid message type %u", msg_type));
2176 
2177 	msg_proc = vmbus_chan_msgprocs[msg_type];
2178 	if (msg_proc != NULL)
2179 		msg_proc(sc, msg);
2180 }
2181 
2182 void
2183 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
2184 {
2185 	if (!on)
2186 		chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
2187 	else
2188 		chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
2189 }
2190 
2191 uint32_t
2192 vmbus_chan_id(const struct vmbus_channel *chan)
2193 {
2194 	return chan->ch_id;
2195 }
2196 
2197 uint32_t
2198 vmbus_chan_subidx(const struct vmbus_channel *chan)
2199 {
2200 	return chan->ch_subidx;
2201 }
2202 
2203 bool
2204 vmbus_chan_is_primary(const struct vmbus_channel *chan)
2205 {
2206 	if (VMBUS_CHAN_ISPRIMARY(chan))
2207 		return true;
2208 	else
2209 		return false;
2210 }
2211 
2212 bool
2213 vmbus_chan_is_hvs(const struct vmbus_channel *chan)
2214 {
2215 	return chan->ch_is_hvs;
2216 }
2217 
2218 bool
2219 vmbus_chan_is_hvs_conn_from_host(const struct vmbus_channel *chan)
2220 {
2221 	KASSERT(vmbus_chan_is_hvs(chan) == true,
2222 	    ("Not a HyperV Socket channel %u", chan->ch_id));
2223 	if (chan->ch_hvs_conn_from_host != 0)
2224 		return true;
2225 	else
2226 		return false;
2227 }
2228 
2229 struct hyperv_guid *
2230 vmbus_chan_guid_type(struct vmbus_channel *chan)
2231 {
2232 	return &chan->ch_guid_type;
2233 }
2234 
2235 struct hyperv_guid *
2236 vmbus_chan_guid_inst(struct vmbus_channel *chan)
2237 {
2238 	return &chan->ch_guid_inst;
2239 }
2240 
2241 int
2242 vmbus_chan_prplist_nelem(int br_size, int prpcnt_max, int dlen_max)
2243 {
2244 	int elem_size;
2245 
2246 	elem_size = __offsetof(struct vmbus_chanpkt_prplist,
2247 	    cp_range[0].gpa_page[prpcnt_max]);
2248 	elem_size += dlen_max;
2249 	elem_size = VMBUS_CHANPKT_TOTLEN(elem_size);
2250 
2251 	return (vmbus_br_nelem(br_size, elem_size));
2252 }
2253 
2254 bool
2255 vmbus_chan_tx_empty(const struct vmbus_channel *chan)
2256 {
2257 
2258 	return (vmbus_txbr_empty(&chan->ch_txbr));
2259 }
2260 
2261 bool
2262 vmbus_chan_rx_empty(const struct vmbus_channel *chan)
2263 {
2264 
2265 	return (vmbus_rxbr_empty(&chan->ch_rxbr));
2266 }
2267 
2268 static int
2269 vmbus_chan_printf(const struct vmbus_channel *chan, const char *fmt, ...)
2270 {
2271 	va_list ap;
2272 	device_t dev;
2273 	int retval;
2274 
2275 	if (chan->ch_dev == NULL || !device_is_alive(chan->ch_dev))
2276 		dev = chan->ch_vmbus->vmbus_dev;
2277 	else
2278 		dev = chan->ch_dev;
2279 
2280 	retval = device_print_prettyname(dev);
2281 	va_start(ap, fmt);
2282 	retval += vprintf(fmt, ap);
2283 	va_end(ap);
2284 
2285 	return (retval);
2286 }
2287 
2288 void
2289 vmbus_chan_run_task(struct vmbus_channel *chan, struct task *task)
2290 {
2291 
2292 	taskqueue_enqueue(chan->ch_tq, task);
2293 	taskqueue_drain(chan->ch_tq, task);
2294 }
2295 
2296 struct taskqueue *
2297 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan)
2298 {
2299 
2300 	return (chan->ch_mgmt_tq);
2301 }
2302 
2303 bool
2304 vmbus_chan_is_revoked(const struct vmbus_channel *chan)
2305 {
2306 
2307 	if (chan->ch_stflags & VMBUS_CHAN_ST_REVOKED)
2308 		return (true);
2309 	return (false);
2310 }
2311 
2312 void
2313 vmbus_chan_set_orphan(struct vmbus_channel *chan, struct vmbus_xact_ctx *xact)
2314 {
2315 
2316 	sx_xlock(&chan->ch_orphan_lock);
2317 	chan->ch_orphan_xact = xact;
2318 	sx_xunlock(&chan->ch_orphan_lock);
2319 }
2320 
2321 void
2322 vmbus_chan_unset_orphan(struct vmbus_channel *chan)
2323 {
2324 
2325 	sx_xlock(&chan->ch_orphan_lock);
2326 	chan->ch_orphan_xact = NULL;
2327 	sx_xunlock(&chan->ch_orphan_lock);
2328 }
2329 
2330 const void *
2331 vmbus_chan_xact_wait(const struct vmbus_channel *chan,
2332     struct vmbus_xact *xact, size_t *resp_len, bool can_sleep)
2333 {
2334 	const void *ret;
2335 
2336 	if (can_sleep)
2337 		ret = vmbus_xact_wait(xact, resp_len);
2338 	else
2339 		ret = vmbus_xact_busywait(xact, resp_len);
2340 	if (vmbus_chan_is_revoked(chan)) {
2341 		/*
2342 		 * This xact probably is interrupted, and the
2343 		 * interruption can race the reply reception,
2344 		 * so we have to make sure that there are nothing
2345 		 * left on the RX bufring, i.e. this xact will
2346 		 * not be touched, once this function returns.
2347 		 *
2348 		 * Since the hypervisor will not put more data
2349 		 * onto the RX bufring once the channel is revoked,
2350 		 * the following loop will be terminated, once all
2351 		 * data are drained by the driver's channel
2352 		 * callback.
2353 		 */
2354 		while (!vmbus_chan_rx_empty(chan)) {
2355 			if (can_sleep)
2356 				pause("chxact", 1);
2357 			else
2358 				DELAY(1000);
2359 		}
2360 	}
2361 	return (ret);
2362 }
2363 
2364 void
2365 vmbus_chan_poll_enable(struct vmbus_channel *chan, u_int pollhz)
2366 {
2367 	struct vmbus_chan_pollarg arg;
2368 	struct task poll_cfg;
2369 
2370 	KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2371 	    ("enable polling on non-batch chan%u", chan->ch_id));
2372 	KASSERT(pollhz >= VMBUS_CHAN_POLLHZ_MIN &&
2373 	    pollhz <= VMBUS_CHAN_POLLHZ_MAX, ("invalid pollhz %u", pollhz));
2374 
2375 	arg.poll_chan = chan;
2376 	arg.poll_hz = pollhz;
2377 	TASK_INIT(&poll_cfg, 0, vmbus_chan_pollcfg_task, &arg);
2378 	vmbus_chan_run_task(chan, &poll_cfg);
2379 }
2380 
2381 void
2382 vmbus_chan_poll_disable(struct vmbus_channel *chan)
2383 {
2384 	struct task poll_dis;
2385 
2386 	KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2387 	    ("disable polling on non-batch chan%u", chan->ch_id));
2388 
2389 	TASK_INIT(&poll_dis, 0, vmbus_chan_polldis_task, chan);
2390 	vmbus_chan_run_task(chan, &poll_dis);
2391 }
2392