xref: /freebsd/sys/dev/hyperv/vmbus/vmbus_chan.c (revision 83823d063ab57db8d3954c1530d036f1ccdceb41)
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 	boolean_t sig_event;
1209 
1210 	error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
1211 	if (error)
1212 		return (error);
1213 
1214 	if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1215 		vmbus_chan_printf(chan, "invalid hlen %u\n", pkt.cph_hlen);
1216 		/* XXX this channel is dead actually. */
1217 		return (EIO);
1218 	}
1219 	if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) {
1220 		vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1221 		    pkt.cph_hlen, pkt.cph_tlen);
1222 		/* XXX this channel is dead actually. */
1223 		return (EIO);
1224 	}
1225 
1226 	hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen);
1227 	dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
1228 
1229 	if (*dlen0 < dlen) {
1230 		/* Return the size of this packet's data. */
1231 		*dlen0 = dlen;
1232 		return (ENOBUFS);
1233 	}
1234 
1235 	*xactid = pkt.cph_xactid;
1236 	*dlen0 = dlen;
1237 
1238 	/* Skip packet header */
1239 	error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen, &sig_event);
1240 	KASSERT(!error, ("vmbus_rxbr_read failed"));
1241 
1242 	if (!error && sig_event)
1243 		vmbus_chan_signal_rx(chan);
1244 
1245 	return (0);
1246 }
1247 
1248 int
1249 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
1250     struct vmbus_chanpkt_hdr *pkt, int *pktlen0)
1251 {
1252 	int error, pktlen, pkt_hlen;
1253 	boolean_t sig_event;
1254 
1255 	pkt_hlen = sizeof(*pkt);
1256 	error = vmbus_rxbr_peek(&chan->ch_rxbr, pkt, pkt_hlen);
1257 	if (error)
1258 		return (error);
1259 
1260 	if (__predict_false(pkt->cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1261 		vmbus_chan_printf(chan, "invalid hlen %u\n", pkt->cph_hlen);
1262 		/* XXX this channel is dead actually. */
1263 		return (EIO);
1264 	}
1265 	if (__predict_false(pkt->cph_hlen > pkt->cph_tlen)) {
1266 		vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1267 		    pkt->cph_hlen, pkt->cph_tlen);
1268 		/* XXX this channel is dead actually. */
1269 		return (EIO);
1270 	}
1271 
1272 	pktlen = VMBUS_CHANPKT_GETLEN(pkt->cph_tlen);
1273 	if (*pktlen0 < pktlen) {
1274 		/* Return the size of this packet. */
1275 		*pktlen0 = pktlen;
1276 		return (ENOBUFS);
1277 	}
1278 	*pktlen0 = pktlen;
1279 
1280 	/*
1281 	 * Skip the fixed-size packet header, which has been filled
1282 	 * by the above vmbus_rxbr_peek().
1283 	 */
1284 	error = vmbus_rxbr_read(&chan->ch_rxbr, pkt + 1,
1285 	    pktlen - pkt_hlen, pkt_hlen, &sig_event);
1286 	KASSERT(!error, ("vmbus_rxbr_read failed"));
1287 
1288 	if (!error && sig_event)
1289 		vmbus_chan_signal_rx(chan);
1290 
1291 	return (0);
1292 }
1293 
1294 uint32_t
1295 vmbus_chan_read_available(struct vmbus_channel *chan)
1296 {
1297 	return (vmbus_rxbr_available(&chan->ch_rxbr));
1298 }
1299 
1300 /*
1301  * This routine does:
1302  *     - Advance the channel read index for 'advance' bytes
1303  *     - Copy data_len bytes in to the buffer pointed by 'data'
1304  * Return 0 if operation succeed. EAGAIN if operations if failed.
1305  * If failed, the buffer pointed by 'data' is intact, and the
1306  * channel read index is not advanced at all.
1307  */
1308 int
1309 vmbus_chan_recv_peek(struct vmbus_channel *chan,
1310     void *data, int data_len, uint32_t advance)
1311 {
1312 	int error;
1313 	boolean_t sig_event;
1314 
1315 	if (data == NULL || data_len <= 0)
1316 		return (EINVAL);
1317 
1318 	error = vmbus_rxbr_idxadv_peek(&chan->ch_rxbr,
1319 	    data, data_len, advance, &sig_event);
1320 
1321 	if (!error && sig_event) {
1322 		vmbus_chan_signal_rx(chan);
1323 	}
1324 
1325 	return (error);
1326 }
1327 
1328 /*
1329  * This routine does:
1330  *     - Advance the channel read index for 'advance' bytes
1331  */
1332 int
1333 vmbus_chan_recv_idxadv(struct vmbus_channel *chan, uint32_t advance)
1334 {
1335 	int error;
1336 	boolean_t sig_event;
1337 
1338 	if (advance == 0)
1339 		return (EINVAL);
1340 
1341 	error = vmbus_rxbr_idxadv(&chan->ch_rxbr, advance, &sig_event);
1342 
1343 	if (!error && sig_event) {
1344 		vmbus_chan_signal_rx(chan);
1345 	}
1346 
1347 	return (error);
1348 }
1349 
1350 
1351 /*
1352  * Caller should hold its own lock to serialize the ring buffer
1353  * copy.
1354  */
1355 int
1356 vmbus_chan_recv_peek_call(struct vmbus_channel *chan, int data_len,
1357     uint32_t skip, vmbus_br_copy_callback_t cb, void *cbarg)
1358 {
1359 	if (!chan || data_len <= 0 || cb == NULL)
1360 		return (EINVAL);
1361 
1362 	return (vmbus_rxbr_peek_call(&chan->ch_rxbr, data_len, skip,
1363 	    cb, cbarg));
1364 }
1365 
1366 static void
1367 vmbus_chan_task(void *xchan, int pending __unused)
1368 {
1369 	struct vmbus_channel *chan = xchan;
1370 	vmbus_chan_callback_t cb = chan->ch_cb;
1371 	void *cbarg = chan->ch_cbarg;
1372 
1373 	KASSERT(chan->ch_poll_intvl == 0,
1374 	    ("chan%u: interrupted in polling mode", chan->ch_id));
1375 
1376 	/*
1377 	 * Optimize host to guest signaling by ensuring:
1378 	 * 1. While reading the channel, we disable interrupts from
1379 	 *    host.
1380 	 * 2. Ensure that we process all posted messages from the host
1381 	 *    before returning from this callback.
1382 	 * 3. Once we return, enable signaling from the host. Once this
1383 	 *    state is set we check to see if additional packets are
1384 	 *    available to read. In this case we repeat the process.
1385 	 *
1386 	 * NOTE: Interrupt has been disabled in the ISR.
1387 	 */
1388 	for (;;) {
1389 		uint32_t left;
1390 
1391 		cb(chan, cbarg);
1392 
1393 		left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1394 		if (left == 0) {
1395 			/* No more data in RX bufring; done */
1396 			break;
1397 		}
1398 		vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1399 	}
1400 }
1401 
1402 static void
1403 vmbus_chan_task_nobatch(void *xchan, int pending __unused)
1404 {
1405 	struct vmbus_channel *chan = xchan;
1406 
1407 	KASSERT(chan->ch_poll_intvl == 0,
1408 	    ("chan%u: interrupted in polling mode", chan->ch_id));
1409 	chan->ch_cb(chan, chan->ch_cbarg);
1410 }
1411 
1412 static void
1413 vmbus_chan_poll_timeout(void *xchan)
1414 {
1415 	struct vmbus_channel *chan = xchan;
1416 
1417 	KASSERT(chan->ch_poll_intvl != 0,
1418 	    ("chan%u: polling timeout in interrupt mode", chan->ch_id));
1419 	taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1420 }
1421 
1422 static void
1423 vmbus_chan_poll_task(void *xchan, int pending __unused)
1424 {
1425 	struct vmbus_channel *chan = xchan;
1426 
1427 	KASSERT(chan->ch_poll_intvl != 0,
1428 	    ("chan%u: polling in interrupt mode", chan->ch_id));
1429 	callout_reset_sbt_curcpu(&chan->ch_poll_timeo, chan->ch_poll_intvl, 0,
1430 	    vmbus_chan_poll_timeout, chan, chan->ch_poll_flags);
1431 	chan->ch_cb(chan, chan->ch_cbarg);
1432 }
1433 
1434 static void
1435 vmbus_chan_pollcfg_task(void *xarg, int pending __unused)
1436 {
1437 	const struct vmbus_chan_pollarg *arg = xarg;
1438 	struct vmbus_channel *chan = arg->poll_chan;
1439 	sbintime_t intvl;
1440 	int poll_flags;
1441 
1442 	/*
1443 	 * Save polling interval.
1444 	 */
1445 	intvl = SBT_1S / arg->poll_hz;
1446 	if (intvl == 0)
1447 		intvl = 1;
1448 	if (intvl == chan->ch_poll_intvl) {
1449 		/* Nothing changes; done */
1450 		return;
1451 	}
1452 	chan->ch_poll_intvl = intvl;
1453 
1454 	/* Adjust callout flags. */
1455 	poll_flags = C_DIRECT_EXEC;
1456 	if (arg->poll_hz <= hz)
1457 		poll_flags |= C_HARDCLOCK;
1458 	chan->ch_poll_flags = poll_flags;
1459 
1460 	/*
1461 	 * Disconnect this channel from the channel map to make sure that
1462 	 * the RX bufring interrupt enabling bit can not be touched, and
1463 	 * ISR can not enqueue this channel task anymore.  THEN, disable
1464 	 * interrupt from the RX bufring (TX bufring does not generate
1465 	 * interrupt to VM).
1466 	 *
1467 	 * NOTE: order is critical.
1468 	 */
1469 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
1470 	__compiler_membar();
1471 	vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1472 
1473 	/*
1474 	 * NOTE:
1475 	 * At this point, this channel task will not be enqueued by
1476 	 * the ISR anymore, time to cancel the pending one.
1477 	 */
1478 	taskqueue_cancel(chan->ch_tq, &chan->ch_task, NULL);
1479 
1480 	/* Kick start! */
1481 	taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1482 }
1483 
1484 static bool
1485 vmbus_chan_poll_cancel_intq(struct vmbus_channel *chan)
1486 {
1487 
1488 	if (chan->ch_poll_intvl == 0) {
1489 		/* Not enabled. */
1490 		return (false);
1491 	}
1492 
1493 	/*
1494 	 * Stop polling callout, so that channel polling task
1495 	 * will not be enqueued anymore.
1496 	 */
1497 	callout_drain(&chan->ch_poll_timeo);
1498 
1499 	/*
1500 	 * Disable polling by resetting polling interval.
1501 	 *
1502 	 * NOTE:
1503 	 * The polling interval resetting MUST be conducted
1504 	 * after the callout is drained; mainly to keep the
1505 	 * proper assertion in place.
1506 	 */
1507 	chan->ch_poll_intvl = 0;
1508 
1509 	/*
1510 	 * NOTE:
1511 	 * At this point, this channel polling task will not be
1512 	 * enqueued by the callout anymore, time to cancel the
1513 	 * pending one.
1514 	 */
1515 	taskqueue_cancel(chan->ch_tq, &chan->ch_poll_task, NULL);
1516 
1517 	/* Polling was enabled. */
1518 	return (true);
1519 }
1520 
1521 static void
1522 vmbus_chan_polldis_task(void *xchan, int pending __unused)
1523 {
1524 	struct vmbus_channel *chan = xchan;
1525 
1526 	if (!vmbus_chan_poll_cancel_intq(chan)) {
1527 		/* Already disabled; done. */
1528 		return;
1529 	}
1530 
1531 	/*
1532 	 * Plug this channel back to the channel map and unmask
1533 	 * the RX bufring interrupt.
1534 	 */
1535 	chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
1536 	__compiler_membar();
1537 	vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1538 
1539 	/*
1540 	 * Kick start the interrupt task, just in case unmasking
1541 	 * interrupt races ISR.
1542 	 */
1543 	taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1544 }
1545 
1546 static __inline void
1547 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
1548     int flag_cnt)
1549 {
1550 	int f;
1551 
1552 	for (f = 0; f < flag_cnt; ++f) {
1553 		uint32_t chid_base;
1554 		u_long flags;
1555 		int chid_ofs;
1556 
1557 		if (event_flags[f] == 0)
1558 			continue;
1559 
1560 		flags = atomic_swap_long(&event_flags[f], 0);
1561 		chid_base = f << VMBUS_EVTFLAG_SHIFT;
1562 
1563 		while ((chid_ofs = ffsl(flags)) != 0) {
1564 			struct vmbus_channel *chan;
1565 
1566 			--chid_ofs; /* NOTE: ffsl is 1-based */
1567 			flags &= ~(1UL << chid_ofs);
1568 
1569 			chan = sc->vmbus_chmap[chid_base + chid_ofs];
1570 			if (__predict_false(chan == NULL)) {
1571 				/* Channel is closed. */
1572 				continue;
1573 			}
1574 			__compiler_membar();
1575 
1576 			if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
1577 				vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1578 			taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1579 		}
1580 	}
1581 }
1582 
1583 void
1584 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
1585 {
1586 	struct vmbus_evtflags *eventf;
1587 
1588 	/*
1589 	 * On Host with Win8 or above, the event page can be checked directly
1590 	 * to get the id of the channel that has the pending interrupt.
1591 	 */
1592 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1593 	vmbus_event_flags_proc(sc, eventf->evt_flags,
1594 	    VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
1595 }
1596 
1597 void
1598 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
1599 {
1600 	struct vmbus_evtflags *eventf;
1601 
1602 	eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1603 	if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
1604 		vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
1605 		    VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
1606 	}
1607 }
1608 
1609 static void
1610 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
1611     const struct vmbus_channel *chan)
1612 {
1613 	volatile int *flag_cnt_ptr;
1614 	int flag_cnt;
1615 
1616 	flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
1617 	flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
1618 
1619 	for (;;) {
1620 		int old_flag_cnt;
1621 
1622 		old_flag_cnt = *flag_cnt_ptr;
1623 		if (old_flag_cnt >= flag_cnt)
1624 			break;
1625 		if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
1626 			if (bootverbose) {
1627 				vmbus_chan_printf(chan,
1628 				    "chan%u update cpu%d flag_cnt to %d\n",
1629 				    chan->ch_id, chan->ch_cpuid, flag_cnt);
1630 			}
1631 			break;
1632 		}
1633 	}
1634 }
1635 
1636 static struct vmbus_channel *
1637 vmbus_chan_alloc(struct vmbus_softc *sc)
1638 {
1639 	struct vmbus_channel *chan;
1640 
1641 	chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
1642 
1643 	chan->ch_monprm = contigmalloc(sizeof(struct hyperv_mon_param),
1644 	    M_DEVBUF, M_WAITOK | M_ZERO, 0ul, ~0ul, HYPERCALL_PARAM_ALIGN, 0);
1645 	if (chan->ch_monprm == NULL) {
1646 		device_printf(sc->vmbus_dev, "monprm alloc failed\n");
1647 		free(chan, M_DEVBUF);
1648 		return NULL;
1649 	}
1650 
1651 	chan->ch_refs = 1;
1652 	chan->ch_vmbus = sc;
1653 	mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
1654 	sx_init(&chan->ch_orphan_lock, "vmbus chorphan");
1655 	TAILQ_INIT(&chan->ch_subchans);
1656 	vmbus_rxbr_init(&chan->ch_rxbr);
1657 	vmbus_txbr_init(&chan->ch_txbr);
1658 
1659 	TASK_INIT(&chan->ch_poll_task, 0, vmbus_chan_poll_task, chan);
1660 	callout_init(&chan->ch_poll_timeo, 1);
1661 
1662 	return chan;
1663 }
1664 
1665 static void
1666 vmbus_chan_free(struct vmbus_channel *chan)
1667 {
1668 
1669 	KASSERT(TAILQ_EMPTY(&chan->ch_subchans) && chan->ch_subchan_cnt == 0,
1670 	    ("still owns sub-channels"));
1671 	KASSERT((chan->ch_stflags &
1672 	    (VMBUS_CHAN_ST_OPENED |
1673 	     VMBUS_CHAN_ST_ONPRIL |
1674 	     VMBUS_CHAN_ST_ONSUBL |
1675 	     VMBUS_CHAN_ST_ONLIST)) == 0, ("free busy channel"));
1676 	KASSERT(chan->ch_orphan_xact == NULL,
1677 	    ("still has orphan xact installed"));
1678 	KASSERT(chan->ch_refs == 0, ("chan%u: invalid refcnt %d",
1679 	    chan->ch_id, chan->ch_refs));
1680 	KASSERT(chan->ch_poll_intvl == 0, ("chan%u: polling is activated",
1681 	    chan->ch_id));
1682 
1683 	contigfree(chan->ch_monprm, sizeof(struct hyperv_mon_param), M_DEVBUF);
1684 	mtx_destroy(&chan->ch_subchan_lock);
1685 	sx_destroy(&chan->ch_orphan_lock);
1686 	vmbus_rxbr_deinit(&chan->ch_rxbr);
1687 	vmbus_txbr_deinit(&chan->ch_txbr);
1688 	free(chan, M_DEVBUF);
1689 }
1690 
1691 static int
1692 vmbus_chan_add(struct vmbus_channel *newchan)
1693 {
1694 	struct vmbus_softc *sc = newchan->ch_vmbus;
1695 	struct vmbus_channel *prichan;
1696 
1697 	if (newchan->ch_id == 0) {
1698 		/*
1699 		 * XXX
1700 		 * Chan0 will neither be processed nor should be offered;
1701 		 * skip it.
1702 		 */
1703 		device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
1704 		return EINVAL;
1705 	} else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
1706 		device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
1707 		    newchan->ch_id);
1708 		return EINVAL;
1709 	}
1710 
1711 	mtx_lock(&sc->vmbus_prichan_lock);
1712 	TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
1713 		/*
1714 		 * Sub-channel will have the same type GUID and instance
1715 		 * GUID as its primary channel.
1716 		 */
1717 		if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
1718 		    sizeof(struct hyperv_guid)) == 0 &&
1719 		    memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
1720 		    sizeof(struct hyperv_guid)) == 0)
1721 			break;
1722 	}
1723 	if (VMBUS_CHAN_ISPRIMARY(newchan)) {
1724 		if (prichan == NULL) {
1725 			/* Install the new primary channel */
1726 			vmbus_chan_ins_prilist(sc, newchan);
1727 			mtx_unlock(&sc->vmbus_prichan_lock);
1728 			goto done;
1729 		} else {
1730 			mtx_unlock(&sc->vmbus_prichan_lock);
1731 			device_printf(sc->vmbus_dev,
1732 			    "duplicated primary chan%u\n", newchan->ch_id);
1733 			return EINVAL;
1734 		}
1735 	} else { /* Sub-channel */
1736 		if (prichan == NULL) {
1737 			mtx_unlock(&sc->vmbus_prichan_lock);
1738 			device_printf(sc->vmbus_dev,
1739 			    "no primary chan for chan%u\n", newchan->ch_id);
1740 			return EINVAL;
1741 		}
1742 		/*
1743 		 * Found the primary channel for this sub-channel and
1744 		 * move on.
1745 		 *
1746 		 * XXX refcnt prichan
1747 		 */
1748 	}
1749 	mtx_unlock(&sc->vmbus_prichan_lock);
1750 
1751 	/*
1752 	 * This is a sub-channel; link it with the primary channel.
1753 	 */
1754 	KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan),
1755 	    ("new channel is not sub-channel"));
1756 	KASSERT(prichan != NULL, ("no primary channel"));
1757 
1758 	/*
1759 	 * Reference count this sub-channel; it will be dereferenced
1760 	 * when this sub-channel is closed.
1761 	 */
1762 	KASSERT(newchan->ch_refs == 1, ("chan%u: invalid refcnt %d",
1763 	    newchan->ch_id, newchan->ch_refs));
1764 	atomic_add_int(&newchan->ch_refs, 1);
1765 
1766 	newchan->ch_prichan = prichan;
1767 	newchan->ch_dev = prichan->ch_dev;
1768 
1769 	mtx_lock(&prichan->ch_subchan_lock);
1770 	vmbus_chan_ins_sublist(prichan, newchan);
1771 	mtx_unlock(&prichan->ch_subchan_lock);
1772 	/*
1773 	 * Notify anyone that is interested in this sub-channel,
1774 	 * after this sub-channel is setup.
1775 	 */
1776 	wakeup(prichan);
1777 done:
1778 	/*
1779 	 * Hook this channel up for later revocation.
1780 	 */
1781 	mtx_lock(&sc->vmbus_chan_lock);
1782 	vmbus_chan_ins_list(sc, newchan);
1783 	mtx_unlock(&sc->vmbus_chan_lock);
1784 
1785 	if (bootverbose) {
1786 		vmbus_chan_printf(newchan, "chan%u subidx%u offer\n",
1787 		    newchan->ch_id, newchan->ch_subidx);
1788 	}
1789 
1790 	/* Select default cpu for this channel. */
1791 	vmbus_chan_cpu_default(newchan);
1792 
1793 	return 0;
1794 }
1795 
1796 void
1797 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1798 {
1799 	KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
1800 
1801 	if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1802 	    chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1803 		/* Only cpu0 is supported */
1804 		cpu = 0;
1805 	}
1806 
1807 	chan->ch_cpuid = cpu;
1808 	chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1809 
1810 	if (bootverbose) {
1811 		vmbus_chan_printf(chan,
1812 		    "chan%u assigned to cpu%u [vcpu%u]\n",
1813 		    chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1814 	}
1815 }
1816 
1817 void
1818 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1819 {
1820 	static uint32_t vmbus_chan_nextcpu;
1821 	int cpu;
1822 
1823 	cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
1824 	vmbus_chan_cpu_set(chan, cpu);
1825 }
1826 
1827 static void
1828 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1829 {
1830 	/*
1831 	 * By default, pin the channel to cpu0.  Devices having
1832 	 * special channel-cpu mapping requirement should call
1833 	 * vmbus_chan_cpu_{set,rr}().
1834 	 */
1835 	vmbus_chan_cpu_set(chan, 0);
1836 }
1837 
1838 static void
1839 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc,
1840     const struct vmbus_message *msg)
1841 {
1842 	const struct vmbus_chanmsg_choffer *offer;
1843 	struct vmbus_channel *chan;
1844 	task_fn_t *detach_fn, *attach_fn;
1845 	int error;
1846 
1847 	offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1848 
1849 	chan = vmbus_chan_alloc(sc);
1850 	if (chan == NULL) {
1851 		device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1852 		    offer->chm_chanid);
1853 		return;
1854 	}
1855 
1856 	chan->ch_id = offer->chm_chanid;
1857 	chan->ch_subidx = offer->chm_subidx;
1858 	chan->ch_guid_type = offer->chm_chtype;
1859 	chan->ch_guid_inst = offer->chm_chinst;
1860 
1861 	/* Batch reading is on by default */
1862 	chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1863 
1864 	chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1865 	if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1866 		chan->ch_monprm->mp_connid = offer->chm_connid;
1867 
1868 	if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1869 		int trig_idx;
1870 
1871 		/*
1872 		 * Setup MNF stuffs.
1873 		 */
1874 		chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1875 
1876 		trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1877 		if (trig_idx >= VMBUS_MONTRIGS_MAX)
1878 			panic("invalid monitor trigger %u", offer->chm_montrig);
1879 		chan->ch_montrig =
1880 		    &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1881 
1882 		chan->ch_montrig_mask =
1883 		    1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1884 	}
1885 
1886 	if (offer->chm_chflags & VMBUS_CHAN_TLNPI_PROVIDER_OFFER) {
1887 		/* This is HyperV socket channel */
1888 		chan->ch_is_hvs = true;
1889 		/* The first byte != 0 means the host initiated connection. */
1890 		chan->ch_hvs_conn_from_host =
1891 		    offer->chm_udata.pipe.user_def[0];
1892 
1893 		if (bootverbose) {
1894 			device_printf(sc->vmbus_dev,
1895 			    "chan%u is hyperv socket channel "
1896 			    "connected %s host\n",
1897 			    chan->ch_id,
1898 			    (chan->ch_hvs_conn_from_host != 0) ?
1899 			    "from" : "to");
1900 		}
1901 	} else {
1902 		chan->ch_is_hvs = false;
1903 	}
1904 
1905 	/*
1906 	 * Setup event flag.
1907 	 */
1908 	chan->ch_evtflag =
1909 	    &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1910 	chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1911 
1912 	/*
1913 	 * Setup attach and detach tasks.
1914 	 */
1915 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
1916 		chan->ch_mgmt_tq = sc->vmbus_devtq;
1917 		attach_fn = vmbus_prichan_attach_task;
1918 		detach_fn = vmbus_prichan_detach_task;
1919 	} else {
1920 		chan->ch_mgmt_tq = sc->vmbus_subchtq;
1921 		attach_fn = vmbus_subchan_attach_task;
1922 		detach_fn = vmbus_subchan_detach_task;
1923 	}
1924 	TASK_INIT(&chan->ch_attach_task, 0, attach_fn, chan);
1925 	TASK_INIT(&chan->ch_detach_task, 0, detach_fn, chan);
1926 
1927 	error = vmbus_chan_add(chan);
1928 	if (error) {
1929 		device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1930 		    chan->ch_id, error);
1931 		atomic_subtract_int(&chan->ch_refs, 1);
1932 		vmbus_chan_free(chan);
1933 		return;
1934 	}
1935 	taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_attach_task);
1936 }
1937 
1938 static void
1939 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc,
1940     const struct vmbus_message *msg)
1941 {
1942 	const struct vmbus_chanmsg_chrescind *note;
1943 	struct vmbus_channel *chan;
1944 
1945 	note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1946 	if (note->chm_chanid > VMBUS_CHAN_MAX) {
1947 		device_printf(sc->vmbus_dev, "invalid revoked chan%u\n",
1948 		    note->chm_chanid);
1949 		return;
1950 	}
1951 
1952 	/*
1953 	 * Find and remove the target channel from the channel list.
1954 	 */
1955 	mtx_lock(&sc->vmbus_chan_lock);
1956 	TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
1957 		if (chan->ch_id == note->chm_chanid)
1958 			break;
1959 	}
1960 	if (chan == NULL) {
1961 		mtx_unlock(&sc->vmbus_chan_lock);
1962 		device_printf(sc->vmbus_dev, "chan%u is not offered\n",
1963 		    note->chm_chanid);
1964 		return;
1965 	}
1966 	vmbus_chan_rem_list(sc, chan);
1967 	mtx_unlock(&sc->vmbus_chan_lock);
1968 
1969 	if (VMBUS_CHAN_ISPRIMARY(chan)) {
1970 		/*
1971 		 * The target channel is a primary channel; remove the
1972 		 * target channel from the primary channel list now,
1973 		 * instead of later, so that it will not be found by
1974 		 * other sub-channel offers, which are processed in
1975 		 * this thread.
1976 		 */
1977 		mtx_lock(&sc->vmbus_prichan_lock);
1978 		vmbus_chan_rem_prilist(sc, chan);
1979 		mtx_unlock(&sc->vmbus_prichan_lock);
1980 	}
1981 
1982 	/*
1983 	 * NOTE:
1984 	 * The following processing order is critical:
1985 	 * Set the REVOKED state flag before orphaning the installed xact.
1986 	 */
1987 
1988 	if (atomic_testandset_int(&chan->ch_stflags,
1989 	    VMBUS_CHAN_ST_REVOKED_SHIFT))
1990 		panic("channel has already been revoked");
1991 
1992 	sx_xlock(&chan->ch_orphan_lock);
1993 	if (chan->ch_orphan_xact != NULL)
1994 		vmbus_xact_ctx_orphan(chan->ch_orphan_xact);
1995 	sx_xunlock(&chan->ch_orphan_lock);
1996 
1997 	if (bootverbose)
1998 		vmbus_chan_printf(chan, "chan%u revoked\n", note->chm_chanid);
1999 	vmbus_chan_detach(chan);
2000 }
2001 
2002 static int
2003 vmbus_chan_release(struct vmbus_channel *chan)
2004 {
2005 	struct vmbus_softc *sc = chan->ch_vmbus;
2006 	struct vmbus_chanmsg_chfree *req;
2007 	struct vmbus_msghc *mh;
2008 	int error;
2009 
2010 	mh = vmbus_msghc_get(sc, sizeof(*req));
2011 	if (mh == NULL) {
2012 		vmbus_chan_printf(chan,
2013 		    "can not get msg hypercall for chfree(chan%u)\n",
2014 		    chan->ch_id);
2015 		return (ENXIO);
2016 	}
2017 
2018 	req = vmbus_msghc_dataptr(mh);
2019 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
2020 	req->chm_chanid = chan->ch_id;
2021 
2022 	error = vmbus_msghc_exec_noresult(mh);
2023 	vmbus_msghc_put(sc, mh);
2024 
2025 	if (error) {
2026 		vmbus_chan_printf(chan,
2027 		    "chfree(chan%u) msg hypercall exec failed: %d\n",
2028 		    chan->ch_id, error);
2029 	} else {
2030 		if (bootverbose)
2031 			vmbus_chan_printf(chan, "chan%u freed\n", chan->ch_id);
2032 	}
2033 	return (error);
2034 }
2035 
2036 static void
2037 vmbus_prichan_detach_task(void *xchan, int pending __unused)
2038 {
2039 	struct vmbus_channel *chan = xchan;
2040 
2041 	KASSERT(VMBUS_CHAN_ISPRIMARY(chan),
2042 	    ("chan%u is not primary channel", chan->ch_id));
2043 
2044 	/* Delete and detach the device associated with this channel. */
2045 	vmbus_delete_child(chan);
2046 
2047 	/* Release this channel (back to vmbus). */
2048 	vmbus_chan_release(chan);
2049 
2050 	/* Free this channel's resource. */
2051 	vmbus_chan_free(chan);
2052 }
2053 
2054 static void
2055 vmbus_subchan_detach_task(void *xchan, int pending __unused)
2056 {
2057 	struct vmbus_channel *chan = xchan;
2058 	struct vmbus_channel *pri_chan = chan->ch_prichan;
2059 
2060 	KASSERT(!VMBUS_CHAN_ISPRIMARY(chan),
2061 	    ("chan%u is primary channel", chan->ch_id));
2062 
2063 	/* Release this channel (back to vmbus). */
2064 	vmbus_chan_release(chan);
2065 
2066 	/* Unlink from its primary channel's sub-channel list. */
2067 	mtx_lock(&pri_chan->ch_subchan_lock);
2068 	vmbus_chan_rem_sublist(pri_chan, chan);
2069 	mtx_unlock(&pri_chan->ch_subchan_lock);
2070 	/* Notify anyone that is waiting for this sub-channel to vanish. */
2071 	wakeup(pri_chan);
2072 
2073 	/* Free this channel's resource. */
2074 	vmbus_chan_free(chan);
2075 }
2076 
2077 static void
2078 vmbus_prichan_attach_task(void *xchan, int pending __unused)
2079 {
2080 
2081 	/*
2082 	 * Add device for this primary channel.
2083 	 */
2084 	vmbus_add_child(xchan);
2085 }
2086 
2087 static void
2088 vmbus_subchan_attach_task(void *xchan __unused, int pending __unused)
2089 {
2090 
2091 	/* Nothing */
2092 }
2093 
2094 void
2095 vmbus_chan_destroy_all(struct vmbus_softc *sc)
2096 {
2097 
2098 	/*
2099 	 * Detach all devices and destroy the corresponding primary
2100 	 * channels.
2101 	 */
2102 	for (;;) {
2103 		struct vmbus_channel *chan;
2104 
2105 		mtx_lock(&sc->vmbus_chan_lock);
2106 		TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
2107 			if (VMBUS_CHAN_ISPRIMARY(chan))
2108 				break;
2109 		}
2110 		if (chan == NULL) {
2111 			/* No more primary channels; done. */
2112 			mtx_unlock(&sc->vmbus_chan_lock);
2113 			break;
2114 		}
2115 		vmbus_chan_rem_list(sc, chan);
2116 		mtx_unlock(&sc->vmbus_chan_lock);
2117 
2118 		mtx_lock(&sc->vmbus_prichan_lock);
2119 		vmbus_chan_rem_prilist(sc, chan);
2120 		mtx_unlock(&sc->vmbus_prichan_lock);
2121 
2122 		taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
2123 	}
2124 }
2125 
2126 struct vmbus_channel **
2127 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt)
2128 {
2129 	struct vmbus_channel **ret, *chan;
2130 	int i;
2131 
2132 	KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt));
2133 
2134 	ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP,
2135 	    M_WAITOK);
2136 
2137 	mtx_lock(&pri_chan->ch_subchan_lock);
2138 
2139 	while (pri_chan->ch_subchan_cnt < subchan_cnt)
2140 		mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
2141 
2142 	i = 0;
2143 	TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
2144 		/* TODO: refcnt chan */
2145 		ret[i] = chan;
2146 
2147 		++i;
2148 		if (i == subchan_cnt)
2149 			break;
2150 	}
2151 	KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
2152 	    pri_chan->ch_subchan_cnt, subchan_cnt));
2153 
2154 	mtx_unlock(&pri_chan->ch_subchan_lock);
2155 
2156 	return ret;
2157 }
2158 
2159 void
2160 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused)
2161 {
2162 
2163 	free(subchan, M_TEMP);
2164 }
2165 
2166 void
2167 vmbus_subchan_drain(struct vmbus_channel *pri_chan)
2168 {
2169 	mtx_lock(&pri_chan->ch_subchan_lock);
2170 	while (pri_chan->ch_subchan_cnt > 0)
2171 		mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
2172 	mtx_unlock(&pri_chan->ch_subchan_lock);
2173 }
2174 
2175 void
2176 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
2177 {
2178 	vmbus_chanmsg_proc_t msg_proc;
2179 	uint32_t msg_type;
2180 
2181 	msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
2182 	KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX,
2183 	    ("invalid message type %u", msg_type));
2184 
2185 	msg_proc = vmbus_chan_msgprocs[msg_type];
2186 	if (msg_proc != NULL)
2187 		msg_proc(sc, msg);
2188 }
2189 
2190 void
2191 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
2192 {
2193 	if (!on)
2194 		chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
2195 	else
2196 		chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
2197 }
2198 
2199 uint32_t
2200 vmbus_chan_id(const struct vmbus_channel *chan)
2201 {
2202 	return chan->ch_id;
2203 }
2204 
2205 uint32_t
2206 vmbus_chan_subidx(const struct vmbus_channel *chan)
2207 {
2208 	return chan->ch_subidx;
2209 }
2210 
2211 bool
2212 vmbus_chan_is_primary(const struct vmbus_channel *chan)
2213 {
2214 	if (VMBUS_CHAN_ISPRIMARY(chan))
2215 		return true;
2216 	else
2217 		return false;
2218 }
2219 
2220 bool
2221 vmbus_chan_is_hvs(const struct vmbus_channel *chan)
2222 {
2223 	return chan->ch_is_hvs;
2224 }
2225 
2226 bool
2227 vmbus_chan_is_hvs_conn_from_host(const struct vmbus_channel *chan)
2228 {
2229 	KASSERT(vmbus_chan_is_hvs(chan) == true,
2230 	    ("Not a HyperV Socket channel %u", chan->ch_id));
2231 	if (chan->ch_hvs_conn_from_host != 0)
2232 		return true;
2233 	else
2234 		return false;
2235 }
2236 
2237 struct hyperv_guid *
2238 vmbus_chan_guid_type(struct vmbus_channel *chan)
2239 {
2240 	return &chan->ch_guid_type;
2241 }
2242 
2243 struct hyperv_guid *
2244 vmbus_chan_guid_inst(struct vmbus_channel *chan)
2245 {
2246 	return &chan->ch_guid_inst;
2247 }
2248 
2249 int
2250 vmbus_chan_prplist_nelem(int br_size, int prpcnt_max, int dlen_max)
2251 {
2252 	int elem_size;
2253 
2254 	elem_size = __offsetof(struct vmbus_chanpkt_prplist,
2255 	    cp_range[0].gpa_page[prpcnt_max]);
2256 	elem_size += dlen_max;
2257 	elem_size = VMBUS_CHANPKT_TOTLEN(elem_size);
2258 
2259 	return (vmbus_br_nelem(br_size, elem_size));
2260 }
2261 
2262 bool
2263 vmbus_chan_tx_empty(const struct vmbus_channel *chan)
2264 {
2265 
2266 	return (vmbus_txbr_empty(&chan->ch_txbr));
2267 }
2268 
2269 bool
2270 vmbus_chan_rx_empty(const struct vmbus_channel *chan)
2271 {
2272 
2273 	return (vmbus_rxbr_empty(&chan->ch_rxbr));
2274 }
2275 
2276 static int
2277 vmbus_chan_printf(const struct vmbus_channel *chan, const char *fmt, ...)
2278 {
2279 	va_list ap;
2280 	device_t dev;
2281 	int retval;
2282 
2283 	if (chan->ch_dev == NULL || !device_is_alive(chan->ch_dev))
2284 		dev = chan->ch_vmbus->vmbus_dev;
2285 	else
2286 		dev = chan->ch_dev;
2287 
2288 	retval = device_print_prettyname(dev);
2289 	va_start(ap, fmt);
2290 	retval += vprintf(fmt, ap);
2291 	va_end(ap);
2292 
2293 	return (retval);
2294 }
2295 
2296 void
2297 vmbus_chan_run_task(struct vmbus_channel *chan, struct task *task)
2298 {
2299 
2300 	taskqueue_enqueue(chan->ch_tq, task);
2301 	taskqueue_drain(chan->ch_tq, task);
2302 }
2303 
2304 struct taskqueue *
2305 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan)
2306 {
2307 
2308 	return (chan->ch_mgmt_tq);
2309 }
2310 
2311 bool
2312 vmbus_chan_is_revoked(const struct vmbus_channel *chan)
2313 {
2314 
2315 	if (chan->ch_stflags & VMBUS_CHAN_ST_REVOKED)
2316 		return (true);
2317 	return (false);
2318 }
2319 
2320 void
2321 vmbus_chan_set_orphan(struct vmbus_channel *chan, struct vmbus_xact_ctx *xact)
2322 {
2323 
2324 	sx_xlock(&chan->ch_orphan_lock);
2325 	chan->ch_orphan_xact = xact;
2326 	sx_xunlock(&chan->ch_orphan_lock);
2327 }
2328 
2329 void
2330 vmbus_chan_unset_orphan(struct vmbus_channel *chan)
2331 {
2332 
2333 	sx_xlock(&chan->ch_orphan_lock);
2334 	chan->ch_orphan_xact = NULL;
2335 	sx_xunlock(&chan->ch_orphan_lock);
2336 }
2337 
2338 const void *
2339 vmbus_chan_xact_wait(const struct vmbus_channel *chan,
2340     struct vmbus_xact *xact, size_t *resp_len, bool can_sleep)
2341 {
2342 	const void *ret;
2343 
2344 	if (can_sleep)
2345 		ret = vmbus_xact_wait(xact, resp_len);
2346 	else
2347 		ret = vmbus_xact_busywait(xact, resp_len);
2348 	if (vmbus_chan_is_revoked(chan)) {
2349 		/*
2350 		 * This xact probably is interrupted, and the
2351 		 * interruption can race the reply reception,
2352 		 * so we have to make sure that there are nothing
2353 		 * left on the RX bufring, i.e. this xact will
2354 		 * not be touched, once this function returns.
2355 		 *
2356 		 * Since the hypervisor will not put more data
2357 		 * onto the RX bufring once the channel is revoked,
2358 		 * the following loop will be terminated, once all
2359 		 * data are drained by the driver's channel
2360 		 * callback.
2361 		 */
2362 		while (!vmbus_chan_rx_empty(chan)) {
2363 			if (can_sleep)
2364 				pause("chxact", 1);
2365 			else
2366 				DELAY(1000);
2367 		}
2368 	}
2369 	return (ret);
2370 }
2371 
2372 void
2373 vmbus_chan_poll_enable(struct vmbus_channel *chan, u_int pollhz)
2374 {
2375 	struct vmbus_chan_pollarg arg;
2376 	struct task poll_cfg;
2377 
2378 	KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2379 	    ("enable polling on non-batch chan%u", chan->ch_id));
2380 	KASSERT(pollhz >= VMBUS_CHAN_POLLHZ_MIN &&
2381 	    pollhz <= VMBUS_CHAN_POLLHZ_MAX, ("invalid pollhz %u", pollhz));
2382 
2383 	arg.poll_chan = chan;
2384 	arg.poll_hz = pollhz;
2385 	TASK_INIT(&poll_cfg, 0, vmbus_chan_pollcfg_task, &arg);
2386 	vmbus_chan_run_task(chan, &poll_cfg);
2387 }
2388 
2389 void
2390 vmbus_chan_poll_disable(struct vmbus_channel *chan)
2391 {
2392 	struct task poll_dis;
2393 
2394 	KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2395 	    ("disable polling on non-batch chan%u", chan->ch_id));
2396 
2397 	TASK_INIT(&poll_dis, 0, vmbus_chan_polldis_task, chan);
2398 	vmbus_chan_run_task(chan, &poll_dis);
2399 }
2400