2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/callout.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
43 #include <machine/atomic.h>
44 #include <machine/stdarg.h>
46 #include <dev/hyperv/include/hyperv_busdma.h>
47 #include <dev/hyperv/include/vmbus_xact.h>
48 #include <dev/hyperv/vmbus/hyperv_var.h>
49 #include <dev/hyperv/vmbus/vmbus_reg.h>
50 #include <dev/hyperv/vmbus/vmbus_var.h>
51 #include <dev/hyperv/vmbus/vmbus_brvar.h>
52 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
54 struct vmbus_chan_pollarg {
55 struct vmbus_channel *poll_chan;
59 static void vmbus_chan_update_evtflagcnt(
61 const struct vmbus_channel *);
62 static int vmbus_chan_close_internal(
63 struct vmbus_channel *);
64 static int vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS);
65 static void vmbus_chan_sysctl_create(
66 struct vmbus_channel *);
67 static struct vmbus_channel *vmbus_chan_alloc(struct vmbus_softc *);
68 static void vmbus_chan_free(struct vmbus_channel *);
69 static int vmbus_chan_add(struct vmbus_channel *);
70 static void vmbus_chan_cpu_default(struct vmbus_channel *);
71 static int vmbus_chan_release(struct vmbus_channel *);
72 static void vmbus_chan_set_chmap(struct vmbus_channel *);
73 static void vmbus_chan_clear_chmap(struct vmbus_channel *);
74 static void vmbus_chan_detach(struct vmbus_channel *);
75 static bool vmbus_chan_wait_revoke(
76 const struct vmbus_channel *, bool);
77 static void vmbus_chan_poll_timeout(void *);
78 static bool vmbus_chan_poll_cancel_intq(
79 struct vmbus_channel *);
80 static void vmbus_chan_poll_cancel(struct vmbus_channel *);
82 static void vmbus_chan_ins_prilist(struct vmbus_softc *,
83 struct vmbus_channel *);
84 static void vmbus_chan_rem_prilist(struct vmbus_softc *,
85 struct vmbus_channel *);
86 static void vmbus_chan_ins_list(struct vmbus_softc *,
87 struct vmbus_channel *);
88 static void vmbus_chan_rem_list(struct vmbus_softc *,
89 struct vmbus_channel *);
90 static void vmbus_chan_ins_sublist(struct vmbus_channel *,
91 struct vmbus_channel *);
92 static void vmbus_chan_rem_sublist(struct vmbus_channel *,
93 struct vmbus_channel *);
95 static void vmbus_chan_task(void *, int);
96 static void vmbus_chan_task_nobatch(void *, int);
97 static void vmbus_chan_poll_task(void *, int);
98 static void vmbus_chan_clrchmap_task(void *, int);
99 static void vmbus_chan_pollcfg_task(void *, int);
100 static void vmbus_chan_polldis_task(void *, int);
101 static void vmbus_chan_poll_cancel_task(void *, int);
102 static void vmbus_prichan_attach_task(void *, int);
103 static void vmbus_subchan_attach_task(void *, int);
104 static void vmbus_prichan_detach_task(void *, int);
105 static void vmbus_subchan_detach_task(void *, int);
107 static void vmbus_chan_msgproc_choffer(struct vmbus_softc *,
108 const struct vmbus_message *);
109 static void vmbus_chan_msgproc_chrescind(
110 struct vmbus_softc *,
111 const struct vmbus_message *);
113 static int vmbus_chan_printf(const struct vmbus_channel *,
114 const char *, ...) __printflike(2, 3);
117 * Vmbus channel message processing.
119 static const vmbus_chanmsg_proc_t
120 vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = {
121 VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer),
122 VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind),
124 VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP),
125 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP),
126 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP)
130 * Notify host that there are data pending on our TX bufring.
133 vmbus_chan_signal_tx(const struct vmbus_channel *chan)
135 atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask);
136 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
137 atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask);
139 hypercall_signal_event(chan->ch_monprm_dma.hv_paddr);
143 vmbus_chan_ins_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
146 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
147 if (atomic_testandset_int(&chan->ch_stflags,
148 VMBUS_CHAN_ST_ONPRIL_SHIFT))
149 panic("channel is already on the prilist");
150 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, chan, ch_prilink);
154 vmbus_chan_rem_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
157 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
158 if (atomic_testandclear_int(&chan->ch_stflags,
159 VMBUS_CHAN_ST_ONPRIL_SHIFT) == 0)
160 panic("channel is not on the prilist");
161 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink);
165 vmbus_chan_ins_sublist(struct vmbus_channel *prichan,
166 struct vmbus_channel *chan)
169 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
171 if (atomic_testandset_int(&chan->ch_stflags,
172 VMBUS_CHAN_ST_ONSUBL_SHIFT))
173 panic("channel is already on the sublist");
174 TAILQ_INSERT_TAIL(&prichan->ch_subchans, chan, ch_sublink);
176 /* Bump sub-channel count. */
177 prichan->ch_subchan_cnt++;
181 vmbus_chan_rem_sublist(struct vmbus_channel *prichan,
182 struct vmbus_channel *chan)
185 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
187 KASSERT(prichan->ch_subchan_cnt > 0,
188 ("invalid subchan_cnt %d", prichan->ch_subchan_cnt));
189 prichan->ch_subchan_cnt--;
191 if (atomic_testandclear_int(&chan->ch_stflags,
192 VMBUS_CHAN_ST_ONSUBL_SHIFT) == 0)
193 panic("channel is not on the sublist");
194 TAILQ_REMOVE(&prichan->ch_subchans, chan, ch_sublink);
198 vmbus_chan_ins_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
201 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
202 if (atomic_testandset_int(&chan->ch_stflags,
203 VMBUS_CHAN_ST_ONLIST_SHIFT))
204 panic("channel is already on the list");
205 TAILQ_INSERT_TAIL(&sc->vmbus_chans, chan, ch_link);
209 vmbus_chan_rem_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
212 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
213 if (atomic_testandclear_int(&chan->ch_stflags,
214 VMBUS_CHAN_ST_ONLIST_SHIFT) == 0)
215 panic("channel is not on the list");
216 TAILQ_REMOVE(&sc->vmbus_chans, chan, ch_link);
220 vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS)
222 struct vmbus_channel *chan = arg1;
225 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
227 return sysctl_handle_int(oidp, &mnf, 0, req);
231 vmbus_chan_sysctl_create(struct vmbus_channel *chan)
233 struct sysctl_oid *ch_tree, *chid_tree, *br_tree;
234 struct sysctl_ctx_list *ctx;
239 * Add sysctl nodes related to this channel to this
240 * channel's sysctl ctx, so that they can be destroyed
241 * independently upon close of this channel, which can
242 * happen even if the device is not detached.
244 ctx = &chan->ch_sysctl_ctx;
245 sysctl_ctx_init(ctx);
248 * Create dev.NAME.UNIT.channel tree.
250 ch_tree = SYSCTL_ADD_NODE(ctx,
251 SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)),
252 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
257 * Create dev.NAME.UNIT.channel.CHANID tree.
259 if (VMBUS_CHAN_ISPRIMARY(chan))
262 ch_id = chan->ch_prichan->ch_id;
263 snprintf(name, sizeof(name), "%d", ch_id);
264 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
265 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
266 if (chid_tree == NULL)
269 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
271 * Create dev.NAME.UNIT.channel.CHANID.sub tree.
273 ch_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree),
274 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
279 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree.
282 * chid_tree is changed to this new sysctl tree.
284 snprintf(name, sizeof(name), "%d", chan->ch_subidx);
285 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
286 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
287 if (chid_tree == NULL)
290 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
291 "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id");
294 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
295 "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id");
296 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
297 "mnf", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
298 chan, 0, vmbus_chan_sysctl_mnf, "I",
299 "has monitor notification facilities");
301 br_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
302 "br", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
303 if (br_tree != NULL) {
305 * Create sysctl tree for RX bufring.
307 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr.rxbr, "rx");
309 * Create sysctl tree for TX bufring.
311 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr.txbr, "tx");
316 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size,
317 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
319 struct vmbus_chan_br cbr;
323 * Allocate the TX+RX bufrings.
325 KASSERT(chan->ch_bufring == NULL, ("bufrings are allocated"));
326 chan->ch_bufring = hyperv_dmamem_alloc(bus_get_dma_tag(chan->ch_dev),
327 PAGE_SIZE, 0, txbr_size + rxbr_size, &chan->ch_bufring_dma,
329 if (chan->ch_bufring == NULL) {
330 vmbus_chan_printf(chan, "bufring allocation failed\n");
334 cbr.cbr = chan->ch_bufring;
335 cbr.cbr_paddr = chan->ch_bufring_dma.hv_paddr;
336 cbr.cbr_txsz = txbr_size;
337 cbr.cbr_rxsz = rxbr_size;
339 error = vmbus_chan_open_br(chan, &cbr, udata, udlen, cb, cbarg);
341 if (error == EISCONN) {
344 * The bufring GPADL is still connected; abandon
345 * this bufring, instead of having mysterious
346 * crash or trashed data later on.
348 vmbus_chan_printf(chan, "chan%u bufring GPADL "
349 "is still connected upon channel open error; "
350 "leak %d bytes memory\n", chan->ch_id,
351 txbr_size + rxbr_size);
353 hyperv_dmamem_free(&chan->ch_bufring_dma,
356 chan->ch_bufring = NULL;
362 vmbus_chan_open_br(struct vmbus_channel *chan, const struct vmbus_chan_br *cbr,
363 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
365 struct vmbus_softc *sc = chan->ch_vmbus;
366 const struct vmbus_message *msg;
367 struct vmbus_chanmsg_chopen *req;
368 struct vmbus_msghc *mh;
370 int error, txbr_size, rxbr_size;
374 if (udlen > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
375 vmbus_chan_printf(chan,
376 "invalid udata len %d for chan%u\n", udlen, chan->ch_id);
381 txbr_size = cbr->cbr_txsz;
382 rxbr_size = cbr->cbr_rxsz;
383 KASSERT((txbr_size & PAGE_MASK) == 0,
384 ("send bufring size is not multiple page"));
385 KASSERT((rxbr_size & PAGE_MASK) == 0,
386 ("recv bufring size is not multiple page"));
387 KASSERT((cbr->cbr_paddr & PAGE_MASK) == 0,
388 ("bufring is not page aligned"));
391 * Zero out the TX/RX bufrings, in case that they were used before.
393 memset(br, 0, txbr_size + rxbr_size);
395 if (atomic_testandset_int(&chan->ch_stflags,
396 VMBUS_CHAN_ST_OPENED_SHIFT))
397 panic("double-open chan%u", chan->ch_id);
400 chan->ch_cbarg = cbarg;
402 vmbus_chan_update_evtflagcnt(sc, chan);
404 chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid);
405 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
406 task_fn = vmbus_chan_task;
408 task_fn = vmbus_chan_task_nobatch;
409 TASK_INIT(&chan->ch_task, 0, task_fn, chan);
411 /* TX bufring comes first */
412 vmbus_txbr_setup(&chan->ch_txbr, br, txbr_size);
413 /* RX bufring immediately follows TX bufring */
414 vmbus_rxbr_setup(&chan->ch_rxbr, br + txbr_size, rxbr_size);
416 /* Create sysctl tree for this channel */
417 vmbus_chan_sysctl_create(chan);
420 * Connect the bufrings, both RX and TX, to this channel.
422 error = vmbus_chan_gpadl_connect(chan, cbr->cbr_paddr,
423 txbr_size + rxbr_size, &chan->ch_bufring_gpadl);
425 vmbus_chan_printf(chan,
426 "failed to connect bufring GPADL to chan%u\n", chan->ch_id);
431 * Install this channel, before it is opened, but after everything
432 * else has been setup.
434 vmbus_chan_set_chmap(chan);
437 * Open channel w/ the bufring GPADL on the target CPU.
439 mh = vmbus_msghc_get(sc, sizeof(*req));
441 vmbus_chan_printf(chan,
442 "can not get msg hypercall for chopen(chan%u)\n",
448 req = vmbus_msghc_dataptr(mh);
449 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
450 req->chm_chanid = chan->ch_id;
451 req->chm_openid = chan->ch_id;
452 req->chm_gpadl = chan->ch_bufring_gpadl;
453 req->chm_vcpuid = chan->ch_vcpuid;
454 req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT;
456 memcpy(req->chm_udata, udata, udlen);
458 error = vmbus_msghc_exec(sc, mh);
460 vmbus_chan_printf(chan,
461 "chopen(chan%u) msg hypercall exec failed: %d\n",
463 vmbus_msghc_put(sc, mh);
468 msg = vmbus_msghc_poll_result(sc, mh);
471 if (vmbus_chan_is_revoked(chan)) {
476 * Hypervisor does _not_ send response CHOPEN to
479 vmbus_chan_printf(chan,
480 "chan%u is revoked, when it is being opened\n",
485 * Add extra delay before cancel the hypercall
486 * execution; mainly to close any possible
487 * CHRESCIND and CHOPEN_RESP races on the
490 #define REVOKE_LINGER 100
491 for (i = 0; i < REVOKE_LINGER; ++i) {
492 msg = vmbus_msghc_poll_result(sc, mh);
499 vmbus_msghc_exec_cancel(sc, mh);
505 status = ((const struct vmbus_chanmsg_chopen_resp *)
506 msg->msg_data)->chm_status;
508 /* XXX any non-0 value is ok here. */
512 vmbus_msghc_put(sc, mh);
516 vmbus_chan_printf(chan, "chan%u opened\n", chan->ch_id);
520 vmbus_chan_printf(chan, "failed to open chan%u\n", chan->ch_id);
524 sysctl_ctx_free(&chan->ch_sysctl_ctx);
525 vmbus_chan_clear_chmap(chan);
526 if (chan->ch_bufring_gpadl != 0) {
529 error1 = vmbus_chan_gpadl_disconnect(chan,
530 chan->ch_bufring_gpadl);
533 * Give caller a hint that the bufring GPADL is still
538 chan->ch_bufring_gpadl = 0;
540 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
545 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr,
546 int size, uint32_t *gpadl0)
548 struct vmbus_softc *sc = chan->ch_vmbus;
549 struct vmbus_msghc *mh;
550 struct vmbus_chanmsg_gpadl_conn *req;
551 const struct vmbus_message *msg;
553 uint32_t gpadl, status;
554 int page_count, range_len, i, cnt, error;
557 KASSERT(*gpadl0 == 0, ("GPADL is not zero"));
560 * Preliminary checks.
563 KASSERT((size & PAGE_MASK) == 0,
564 ("invalid GPA size %d, not multiple page size", size));
565 page_count = size >> PAGE_SHIFT;
567 KASSERT((paddr & PAGE_MASK) == 0,
568 ("GPA is not page aligned %jx", (uintmax_t)paddr));
569 page_id = paddr >> PAGE_SHIFT;
571 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
573 * We don't support multiple GPA ranges.
575 if (range_len > UINT16_MAX) {
576 vmbus_chan_printf(chan, "GPA too large, %d pages\n",
584 gpadl = vmbus_gpadl_alloc(sc);
587 * Connect this GPADL to the target channel.
590 * Since each message can only hold small set of page
591 * addresses, several messages may be required to
592 * complete the connection.
594 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
595 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
600 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
601 chm_range.gpa_page[cnt]);
602 mh = vmbus_msghc_get(sc, reqsz);
604 vmbus_chan_printf(chan,
605 "can not get msg hypercall for gpadl_conn(chan%u)\n",
610 req = vmbus_msghc_dataptr(mh);
611 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
612 req->chm_chanid = chan->ch_id;
613 req->chm_gpadl = gpadl;
614 req->chm_range_len = range_len;
615 req->chm_range_cnt = 1;
616 req->chm_range.gpa_len = size;
617 req->chm_range.gpa_ofs = 0;
618 for (i = 0; i < cnt; ++i)
619 req->chm_range.gpa_page[i] = page_id++;
621 error = vmbus_msghc_exec(sc, mh);
623 vmbus_chan_printf(chan,
624 "gpadl_conn(chan%u) msg hypercall exec failed: %d\n",
626 vmbus_msghc_put(sc, mh);
630 while (page_count > 0) {
631 struct vmbus_chanmsg_gpadl_subconn *subreq;
633 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
634 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
639 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
641 vmbus_msghc_reset(mh, reqsz);
643 subreq = vmbus_msghc_dataptr(mh);
644 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
645 subreq->chm_gpadl = gpadl;
646 for (i = 0; i < cnt; ++i)
647 subreq->chm_gpa_page[i] = page_id++;
649 vmbus_msghc_exec_noresult(mh);
651 KASSERT(page_count == 0, ("invalid page count %d", page_count));
653 msg = vmbus_msghc_wait_result(sc, mh);
654 status = ((const struct vmbus_chanmsg_gpadl_connresp *)
655 msg->msg_data)->chm_status;
657 vmbus_msghc_put(sc, mh);
660 vmbus_chan_printf(chan, "gpadl_conn(chan%u) failed: %u\n",
661 chan->ch_id, status);
665 /* Done; commit the GPADL id. */
668 vmbus_chan_printf(chan, "gpadl_conn(chan%u) succeeded\n",
675 vmbus_chan_wait_revoke(const struct vmbus_channel *chan, bool can_sleep)
677 #define WAIT_COUNT 200 /* 200ms */
681 for (i = 0; i < WAIT_COUNT; ++i) {
682 if (vmbus_chan_is_revoked(chan))
695 * Disconnect the GPA from the target channel
698 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl)
700 struct vmbus_softc *sc = chan->ch_vmbus;
701 struct vmbus_msghc *mh;
702 struct vmbus_chanmsg_gpadl_disconn *req;
705 KASSERT(gpadl != 0, ("GPADL is zero"));
707 mh = vmbus_msghc_get(sc, sizeof(*req));
709 vmbus_chan_printf(chan,
710 "can not get msg hypercall for gpadl_disconn(chan%u)\n",
715 req = vmbus_msghc_dataptr(mh);
716 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
717 req->chm_chanid = chan->ch_id;
718 req->chm_gpadl = gpadl;
720 error = vmbus_msghc_exec(sc, mh);
722 vmbus_msghc_put(sc, mh);
724 if (vmbus_chan_wait_revoke(chan, true)) {
726 * Error is benign; this channel is revoked,
727 * so this GPADL will not be touched anymore.
729 vmbus_chan_printf(chan,
730 "gpadl_disconn(revoked chan%u) msg hypercall "
731 "exec failed: %d\n", chan->ch_id, error);
734 vmbus_chan_printf(chan,
735 "gpadl_disconn(chan%u) msg hypercall exec failed: %d\n",
740 vmbus_msghc_wait_result(sc, mh);
741 /* Discard result; no useful information */
742 vmbus_msghc_put(sc, mh);
748 vmbus_chan_detach(struct vmbus_channel *chan)
752 KASSERT(chan->ch_refs > 0, ("chan%u: invalid refcnt %d",
753 chan->ch_id, chan->ch_refs));
754 refs = atomic_fetchadd_int(&chan->ch_refs, -1);
756 if (VMBUS_CHAN_ISPRIMARY(chan)) {
757 KASSERT(refs == 1, ("chan%u: invalid refcnt %d for prichan",
758 chan->ch_id, refs + 1));
763 * Detach the target channel.
766 vmbus_chan_printf(chan, "chan%u detached\n",
769 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
774 vmbus_chan_clrchmap_task(void *xchan, int pending __unused)
776 struct vmbus_channel *chan = xchan;
778 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
782 vmbus_chan_clear_chmap(struct vmbus_channel *chan)
784 struct task chmap_task;
786 TASK_INIT(&chmap_task, 0, vmbus_chan_clrchmap_task, chan);
787 vmbus_chan_run_task(chan, &chmap_task);
791 vmbus_chan_set_chmap(struct vmbus_channel *chan)
794 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
798 vmbus_chan_poll_cancel_task(void *xchan, int pending __unused)
801 vmbus_chan_poll_cancel_intq(xchan);
805 vmbus_chan_poll_cancel(struct vmbus_channel *chan)
807 struct task poll_cancel;
809 TASK_INIT(&poll_cancel, 0, vmbus_chan_poll_cancel_task, chan);
810 vmbus_chan_run_task(chan, &poll_cancel);
814 vmbus_chan_close_internal(struct vmbus_channel *chan)
816 struct vmbus_softc *sc = chan->ch_vmbus;
817 struct vmbus_msghc *mh;
818 struct vmbus_chanmsg_chclose *req;
819 uint32_t old_stflags;
824 * Sub-channels are closed upon their primary channel closing,
825 * so they can be closed even before they are opened.
828 old_stflags = chan->ch_stflags;
829 if (atomic_cmpset_int(&chan->ch_stflags, old_stflags,
830 old_stflags & ~VMBUS_CHAN_ST_OPENED))
833 if ((old_stflags & VMBUS_CHAN_ST_OPENED) == 0) {
834 /* Not opened yet; done */
836 vmbus_chan_printf(chan, "chan%u not opened\n",
843 * Free this channel's sysctl tree attached to its device's
846 sysctl_ctx_free(&chan->ch_sysctl_ctx);
849 * Cancel polling, if it is enabled.
851 vmbus_chan_poll_cancel(chan);
855 * Order is critical. This channel _must_ be uninstalled first,
856 * else the channel task may be enqueued by the IDT after it has
859 vmbus_chan_clear_chmap(chan);
860 taskqueue_drain(chan->ch_tq, &chan->ch_task);
864 * Close this channel.
866 mh = vmbus_msghc_get(sc, sizeof(*req));
868 vmbus_chan_printf(chan,
869 "can not get msg hypercall for chclose(chan%u)\n",
875 req = vmbus_msghc_dataptr(mh);
876 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
877 req->chm_chanid = chan->ch_id;
879 error = vmbus_msghc_exec_noresult(mh);
880 vmbus_msghc_put(sc, mh);
883 vmbus_chan_printf(chan,
884 "chclose(chan%u) msg hypercall exec failed: %d\n",
890 vmbus_chan_printf(chan, "chan%u closed\n", chan->ch_id);
894 * Disconnect the TX+RX bufrings from this channel.
896 if (chan->ch_bufring_gpadl != 0) {
899 error1 = vmbus_chan_gpadl_disconnect(chan,
900 chan->ch_bufring_gpadl);
904 * The bufring GPADL is still connected; abandon
905 * this bufring, instead of having mysterious
906 * crash or trashed data later on.
908 vmbus_chan_printf(chan, "chan%u bufring GPADL "
909 "is still connected after close\n", chan->ch_id);
910 chan->ch_bufring = NULL;
912 * Give caller a hint that the bufring GPADL is
917 chan->ch_bufring_gpadl = 0;
921 * Destroy the TX+RX bufrings.
923 if (chan->ch_bufring != NULL) {
924 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring);
925 chan->ch_bufring = NULL;
931 vmbus_chan_close_direct(struct vmbus_channel *chan)
936 if (VMBUS_CHAN_ISPRIMARY(chan)) {
937 struct vmbus_channel *subchan;
940 * All sub-channels _must_ have been closed, or are _not_
943 mtx_lock(&chan->ch_subchan_lock);
944 TAILQ_FOREACH(subchan, &chan->ch_subchans, ch_sublink) {
946 (subchan->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0,
947 ("chan%u: subchan%u is still opened",
948 chan->ch_id, subchan->ch_subidx));
950 mtx_unlock(&chan->ch_subchan_lock);
954 error = vmbus_chan_close_internal(chan);
955 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
957 * This sub-channel is referenced, when it is linked to
958 * the primary channel; drop that reference now.
960 vmbus_chan_detach(chan);
966 * Caller should make sure that all sub-channels have
967 * been added to 'chan' and all to-be-closed channels
968 * are not being opened.
971 vmbus_chan_close(struct vmbus_channel *chan)
975 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
977 * Sub-channel is closed when its primary channel
984 * Close all sub-channels, if any.
986 subchan_cnt = chan->ch_subchan_cnt;
987 if (subchan_cnt > 0) {
988 struct vmbus_channel **subchan;
991 subchan = vmbus_subchan_get(chan, subchan_cnt);
992 for (i = 0; i < subchan_cnt; ++i) {
993 vmbus_chan_close_internal(subchan[i]);
995 * This sub-channel is referenced, when it is
996 * linked to the primary channel; drop that
999 vmbus_chan_detach(subchan[i]);
1001 vmbus_subchan_rel(subchan, subchan_cnt);
1004 /* Then close the primary channel. */
1005 vmbus_chan_close_internal(chan);
1009 vmbus_chan_intr_drain(struct vmbus_channel *chan)
1012 taskqueue_drain(chan->ch_tq, &chan->ch_task);
1016 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags,
1017 void *data, int dlen, uint64_t xactid)
1019 struct vmbus_chanpkt pkt;
1020 int pktlen, pad_pktlen, hlen, error;
1022 struct iovec iov[3];
1026 pktlen = hlen + dlen;
1027 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1028 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1029 ("invalid packet size %d", pad_pktlen));
1031 pkt.cp_hdr.cph_type = type;
1032 pkt.cp_hdr.cph_flags = flags;
1033 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1034 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1035 pkt.cp_hdr.cph_xactid = xactid;
1037 iov[0].iov_base = &pkt;
1038 iov[0].iov_len = hlen;
1039 iov[1].iov_base = data;
1040 iov[1].iov_len = dlen;
1041 iov[2].iov_base = &pad;
1042 iov[2].iov_len = pad_pktlen - pktlen;
1044 error = vmbus_txbr_write(&chan->ch_txbr, iov, 3, &send_evt);
1045 if (!error && send_evt)
1046 vmbus_chan_signal_tx(chan);
1051 vmbus_chan_send_sglist(struct vmbus_channel *chan,
1052 struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid)
1054 struct vmbus_chanpkt_sglist pkt;
1055 int pktlen, pad_pktlen, hlen, error;
1056 struct iovec iov[4];
1060 hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]);
1061 pktlen = hlen + dlen;
1062 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1063 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1064 ("invalid packet size %d", pad_pktlen));
1066 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1067 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1068 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1069 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1070 pkt.cp_hdr.cph_xactid = xactid;
1072 pkt.cp_gpa_cnt = sglen;
1074 iov[0].iov_base = &pkt;
1075 iov[0].iov_len = sizeof(pkt);
1076 iov[1].iov_base = sg;
1077 iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen;
1078 iov[2].iov_base = data;
1079 iov[2].iov_len = dlen;
1080 iov[3].iov_base = &pad;
1081 iov[3].iov_len = pad_pktlen - pktlen;
1083 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1084 if (!error && send_evt)
1085 vmbus_chan_signal_tx(chan);
1090 vmbus_chan_send_prplist(struct vmbus_channel *chan,
1091 struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen,
1094 struct vmbus_chanpkt_prplist pkt;
1095 int pktlen, pad_pktlen, hlen, error;
1096 struct iovec iov[4];
1100 hlen = __offsetof(struct vmbus_chanpkt_prplist,
1101 cp_range[0].gpa_page[prp_cnt]);
1102 pktlen = hlen + dlen;
1103 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
1104 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1105 ("invalid packet size %d", pad_pktlen));
1107 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
1108 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
1109 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
1110 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
1111 pkt.cp_hdr.cph_xactid = xactid;
1113 pkt.cp_range_cnt = 1;
1115 iov[0].iov_base = &pkt;
1116 iov[0].iov_len = sizeof(pkt);
1117 iov[1].iov_base = prp;
1118 iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]);
1119 iov[2].iov_base = data;
1120 iov[2].iov_len = dlen;
1121 iov[3].iov_base = &pad;
1122 iov[3].iov_len = pad_pktlen - pktlen;
1124 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1125 if (!error && send_evt)
1126 vmbus_chan_signal_tx(chan);
1131 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0,
1134 struct vmbus_chanpkt_hdr pkt;
1135 int error, dlen, hlen;
1137 error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
1141 if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1142 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt.cph_hlen);
1143 /* XXX this channel is dead actually. */
1146 if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) {
1147 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1148 pkt.cph_hlen, pkt.cph_tlen);
1149 /* XXX this channel is dead actually. */
1153 hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen);
1154 dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
1156 if (*dlen0 < dlen) {
1157 /* Return the size of this packet's data. */
1162 *xactid = pkt.cph_xactid;
1165 /* Skip packet header */
1166 error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen);
1167 KASSERT(!error, ("vmbus_rxbr_read failed"));
1173 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
1174 struct vmbus_chanpkt_hdr *pkt, int *pktlen0)
1176 int error, pktlen, pkt_hlen;
1178 pkt_hlen = sizeof(*pkt);
1179 error = vmbus_rxbr_peek(&chan->ch_rxbr, pkt, pkt_hlen);
1183 if (__predict_false(pkt->cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1184 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt->cph_hlen);
1185 /* XXX this channel is dead actually. */
1188 if (__predict_false(pkt->cph_hlen > pkt->cph_tlen)) {
1189 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1190 pkt->cph_hlen, pkt->cph_tlen);
1191 /* XXX this channel is dead actually. */
1195 pktlen = VMBUS_CHANPKT_GETLEN(pkt->cph_tlen);
1196 if (*pktlen0 < pktlen) {
1197 /* Return the size of this packet. */
1204 * Skip the fixed-size packet header, which has been filled
1205 * by the above vmbus_rxbr_peek().
1207 error = vmbus_rxbr_read(&chan->ch_rxbr, pkt + 1,
1208 pktlen - pkt_hlen, pkt_hlen);
1209 KASSERT(!error, ("vmbus_rxbr_read failed"));
1215 vmbus_chan_task(void *xchan, int pending __unused)
1217 struct vmbus_channel *chan = xchan;
1218 vmbus_chan_callback_t cb = chan->ch_cb;
1219 void *cbarg = chan->ch_cbarg;
1221 KASSERT(chan->ch_poll_intvl == 0,
1222 ("chan%u: interrupted in polling mode", chan->ch_id));
1225 * Optimize host to guest signaling by ensuring:
1226 * 1. While reading the channel, we disable interrupts from
1228 * 2. Ensure that we process all posted messages from the host
1229 * before returning from this callback.
1230 * 3. Once we return, enable signaling from the host. Once this
1231 * state is set we check to see if additional packets are
1232 * available to read. In this case we repeat the process.
1234 * NOTE: Interrupt has been disabled in the ISR.
1241 left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1243 /* No more data in RX bufring; done */
1246 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1251 vmbus_chan_task_nobatch(void *xchan, int pending __unused)
1253 struct vmbus_channel *chan = xchan;
1255 KASSERT(chan->ch_poll_intvl == 0,
1256 ("chan%u: interrupted in polling mode", chan->ch_id));
1257 chan->ch_cb(chan, chan->ch_cbarg);
1261 vmbus_chan_poll_timeout(void *xchan)
1263 struct vmbus_channel *chan = xchan;
1265 KASSERT(chan->ch_poll_intvl != 0,
1266 ("chan%u: polling timeout in interrupt mode", chan->ch_id));
1267 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1271 vmbus_chan_poll_task(void *xchan, int pending __unused)
1273 struct vmbus_channel *chan = xchan;
1275 KASSERT(chan->ch_poll_intvl != 0,
1276 ("chan%u: polling in interrupt mode", chan->ch_id));
1277 callout_reset_sbt_curcpu(&chan->ch_poll_timeo, chan->ch_poll_intvl, 0,
1278 vmbus_chan_poll_timeout, chan, chan->ch_poll_flags);
1279 chan->ch_cb(chan, chan->ch_cbarg);
1283 vmbus_chan_pollcfg_task(void *xarg, int pending __unused)
1285 const struct vmbus_chan_pollarg *arg = xarg;
1286 struct vmbus_channel *chan = arg->poll_chan;
1291 * Save polling interval.
1293 intvl = SBT_1S / arg->poll_hz;
1296 if (intvl == chan->ch_poll_intvl) {
1297 /* Nothing changes; done */
1300 chan->ch_poll_intvl = intvl;
1302 /* Adjust callout flags. */
1303 poll_flags = C_DIRECT_EXEC;
1304 if (arg->poll_hz <= hz)
1305 poll_flags |= C_HARDCLOCK;
1306 chan->ch_poll_flags = poll_flags;
1309 * Disconnect this channel from the channel map to make sure that
1310 * the RX bufring interrupt enabling bit can not be touched, and
1311 * ISR can not enqueue this channel task anymore. THEN, disable
1312 * interrupt from the RX bufring (TX bufring does not generate
1315 * NOTE: order is critical.
1317 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
1318 __compiler_membar();
1319 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1323 * At this point, this channel task will not be enqueued by
1324 * the ISR anymore, time to cancel the pending one.
1326 taskqueue_cancel(chan->ch_tq, &chan->ch_task, NULL);
1329 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1333 vmbus_chan_poll_cancel_intq(struct vmbus_channel *chan)
1336 if (chan->ch_poll_intvl == 0) {
1342 * Stop polling callout, so that channel polling task
1343 * will not be enqueued anymore.
1345 callout_drain(&chan->ch_poll_timeo);
1348 * Disable polling by resetting polling interval.
1351 * The polling interval resetting MUST be conducted
1352 * after the callout is drained; mainly to keep the
1353 * proper assertion in place.
1355 chan->ch_poll_intvl = 0;
1359 * At this point, this channel polling task will not be
1360 * enqueued by the callout anymore, time to cancel the
1363 taskqueue_cancel(chan->ch_tq, &chan->ch_poll_task, NULL);
1365 /* Polling was enabled. */
1370 vmbus_chan_polldis_task(void *xchan, int pending __unused)
1372 struct vmbus_channel *chan = xchan;
1374 if (!vmbus_chan_poll_cancel_intq(chan)) {
1375 /* Already disabled; done. */
1380 * Plug this channel back to the channel map and unmask
1381 * the RX bufring interrupt.
1383 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
1384 __compiler_membar();
1385 vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1388 * Kick start the interrupt task, just in case unmasking
1389 * interrupt races ISR.
1391 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1394 static __inline void
1395 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
1400 for (f = 0; f < flag_cnt; ++f) {
1405 if (event_flags[f] == 0)
1408 flags = atomic_swap_long(&event_flags[f], 0);
1409 chid_base = f << VMBUS_EVTFLAG_SHIFT;
1411 while ((chid_ofs = ffsl(flags)) != 0) {
1412 struct vmbus_channel *chan;
1414 --chid_ofs; /* NOTE: ffsl is 1-based */
1415 flags &= ~(1UL << chid_ofs);
1417 chan = sc->vmbus_chmap[chid_base + chid_ofs];
1418 if (__predict_false(chan == NULL)) {
1419 /* Channel is closed. */
1422 __compiler_membar();
1424 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
1425 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1426 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1432 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
1434 struct vmbus_evtflags *eventf;
1437 * On Host with Win8 or above, the event page can be checked directly
1438 * to get the id of the channel that has the pending interrupt.
1440 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1441 vmbus_event_flags_proc(sc, eventf->evt_flags,
1442 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
1446 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
1448 struct vmbus_evtflags *eventf;
1450 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
1451 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
1452 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
1453 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
1458 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
1459 const struct vmbus_channel *chan)
1461 volatile int *flag_cnt_ptr;
1464 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
1465 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
1470 old_flag_cnt = *flag_cnt_ptr;
1471 if (old_flag_cnt >= flag_cnt)
1473 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
1475 vmbus_chan_printf(chan,
1476 "chan%u update cpu%d flag_cnt to %d\n",
1477 chan->ch_id, chan->ch_cpuid, flag_cnt);
1484 static struct vmbus_channel *
1485 vmbus_chan_alloc(struct vmbus_softc *sc)
1487 struct vmbus_channel *chan;
1489 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
1491 chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev),
1492 HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param),
1493 &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
1494 if (chan->ch_monprm == NULL) {
1495 device_printf(sc->vmbus_dev, "monprm alloc failed\n");
1496 free(chan, M_DEVBUF);
1501 chan->ch_vmbus = sc;
1502 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
1503 sx_init(&chan->ch_orphan_lock, "vmbus chorphan");
1504 TAILQ_INIT(&chan->ch_subchans);
1505 vmbus_rxbr_init(&chan->ch_rxbr);
1506 vmbus_txbr_init(&chan->ch_txbr);
1508 TASK_INIT(&chan->ch_poll_task, 0, vmbus_chan_poll_task, chan);
1509 callout_init(&chan->ch_poll_timeo, 1);
1515 vmbus_chan_free(struct vmbus_channel *chan)
1518 KASSERT(TAILQ_EMPTY(&chan->ch_subchans) && chan->ch_subchan_cnt == 0,
1519 ("still owns sub-channels"));
1520 KASSERT((chan->ch_stflags &
1521 (VMBUS_CHAN_ST_OPENED |
1522 VMBUS_CHAN_ST_ONPRIL |
1523 VMBUS_CHAN_ST_ONSUBL |
1524 VMBUS_CHAN_ST_ONLIST)) == 0, ("free busy channel"));
1525 KASSERT(chan->ch_orphan_xact == NULL,
1526 ("still has orphan xact installed"));
1527 KASSERT(chan->ch_refs == 0, ("chan%u: invalid refcnt %d",
1528 chan->ch_id, chan->ch_refs));
1529 KASSERT(chan->ch_poll_intvl == 0, ("chan%u: polling is activated",
1532 hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm);
1533 mtx_destroy(&chan->ch_subchan_lock);
1534 sx_destroy(&chan->ch_orphan_lock);
1535 vmbus_rxbr_deinit(&chan->ch_rxbr);
1536 vmbus_txbr_deinit(&chan->ch_txbr);
1537 free(chan, M_DEVBUF);
1541 vmbus_chan_add(struct vmbus_channel *newchan)
1543 struct vmbus_softc *sc = newchan->ch_vmbus;
1544 struct vmbus_channel *prichan;
1546 if (newchan->ch_id == 0) {
1549 * Chan0 will neither be processed nor should be offered;
1552 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
1554 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
1555 device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
1560 mtx_lock(&sc->vmbus_prichan_lock);
1561 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
1563 * Sub-channel will have the same type GUID and instance
1564 * GUID as its primary channel.
1566 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
1567 sizeof(struct hyperv_guid)) == 0 &&
1568 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
1569 sizeof(struct hyperv_guid)) == 0)
1572 if (VMBUS_CHAN_ISPRIMARY(newchan)) {
1573 if (prichan == NULL) {
1574 /* Install the new primary channel */
1575 vmbus_chan_ins_prilist(sc, newchan);
1576 mtx_unlock(&sc->vmbus_prichan_lock);
1579 mtx_unlock(&sc->vmbus_prichan_lock);
1580 device_printf(sc->vmbus_dev,
1581 "duplicated primary chan%u\n", newchan->ch_id);
1584 } else { /* Sub-channel */
1585 if (prichan == NULL) {
1586 mtx_unlock(&sc->vmbus_prichan_lock);
1587 device_printf(sc->vmbus_dev,
1588 "no primary chan for chan%u\n", newchan->ch_id);
1592 * Found the primary channel for this sub-channel and
1595 * XXX refcnt prichan
1598 mtx_unlock(&sc->vmbus_prichan_lock);
1601 * This is a sub-channel; link it with the primary channel.
1603 KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan),
1604 ("new channel is not sub-channel"));
1605 KASSERT(prichan != NULL, ("no primary channel"));
1608 * Reference count this sub-channel; it will be dereferenced
1609 * when this sub-channel is closed.
1611 KASSERT(newchan->ch_refs == 1, ("chan%u: invalid refcnt %d",
1612 newchan->ch_id, newchan->ch_refs));
1613 atomic_add_int(&newchan->ch_refs, 1);
1615 newchan->ch_prichan = prichan;
1616 newchan->ch_dev = prichan->ch_dev;
1618 mtx_lock(&prichan->ch_subchan_lock);
1619 vmbus_chan_ins_sublist(prichan, newchan);
1620 mtx_unlock(&prichan->ch_subchan_lock);
1622 * Notify anyone that is interested in this sub-channel,
1623 * after this sub-channel is setup.
1628 * Hook this channel up for later revocation.
1630 mtx_lock(&sc->vmbus_chan_lock);
1631 vmbus_chan_ins_list(sc, newchan);
1632 mtx_unlock(&sc->vmbus_chan_lock);
1635 vmbus_chan_printf(newchan, "chan%u subidx%u offer\n",
1636 newchan->ch_id, newchan->ch_subidx);
1639 /* Select default cpu for this channel. */
1640 vmbus_chan_cpu_default(newchan);
1646 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1648 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
1650 if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1651 chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1652 /* Only cpu0 is supported */
1656 chan->ch_cpuid = cpu;
1657 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1660 vmbus_chan_printf(chan,
1661 "chan%u assigned to cpu%u [vcpu%u]\n",
1662 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1667 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1669 static uint32_t vmbus_chan_nextcpu;
1672 cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
1673 vmbus_chan_cpu_set(chan, cpu);
1677 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1680 * By default, pin the channel to cpu0. Devices having
1681 * special channel-cpu mapping requirement should call
1682 * vmbus_chan_cpu_{set,rr}().
1684 vmbus_chan_cpu_set(chan, 0);
1688 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc,
1689 const struct vmbus_message *msg)
1691 const struct vmbus_chanmsg_choffer *offer;
1692 struct vmbus_channel *chan;
1693 task_fn_t *detach_fn, *attach_fn;
1696 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1698 chan = vmbus_chan_alloc(sc);
1700 device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1705 chan->ch_id = offer->chm_chanid;
1706 chan->ch_subidx = offer->chm_subidx;
1707 chan->ch_guid_type = offer->chm_chtype;
1708 chan->ch_guid_inst = offer->chm_chinst;
1710 /* Batch reading is on by default */
1711 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1713 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1714 if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1715 chan->ch_monprm->mp_connid = offer->chm_connid;
1717 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1723 chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1725 trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1726 if (trig_idx >= VMBUS_MONTRIGS_MAX)
1727 panic("invalid monitor trigger %u", offer->chm_montrig);
1729 &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1731 chan->ch_montrig_mask =
1732 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1739 &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1740 chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1743 * Setup attach and detach tasks.
1745 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1746 chan->ch_mgmt_tq = sc->vmbus_devtq;
1747 attach_fn = vmbus_prichan_attach_task;
1748 detach_fn = vmbus_prichan_detach_task;
1750 chan->ch_mgmt_tq = sc->vmbus_subchtq;
1751 attach_fn = vmbus_subchan_attach_task;
1752 detach_fn = vmbus_subchan_detach_task;
1754 TASK_INIT(&chan->ch_attach_task, 0, attach_fn, chan);
1755 TASK_INIT(&chan->ch_detach_task, 0, detach_fn, chan);
1757 error = vmbus_chan_add(chan);
1759 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1760 chan->ch_id, error);
1761 atomic_subtract_int(&chan->ch_refs, 1);
1762 vmbus_chan_free(chan);
1765 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_attach_task);
1769 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc,
1770 const struct vmbus_message *msg)
1772 const struct vmbus_chanmsg_chrescind *note;
1773 struct vmbus_channel *chan;
1775 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1776 if (note->chm_chanid > VMBUS_CHAN_MAX) {
1777 device_printf(sc->vmbus_dev, "invalid revoked chan%u\n",
1783 * Find and remove the target channel from the channel list.
1785 mtx_lock(&sc->vmbus_chan_lock);
1786 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
1787 if (chan->ch_id == note->chm_chanid)
1791 mtx_unlock(&sc->vmbus_chan_lock);
1792 device_printf(sc->vmbus_dev, "chan%u is not offered\n",
1796 vmbus_chan_rem_list(sc, chan);
1797 mtx_unlock(&sc->vmbus_chan_lock);
1799 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1801 * The target channel is a primary channel; remove the
1802 * target channel from the primary channel list now,
1803 * instead of later, so that it will not be found by
1804 * other sub-channel offers, which are processed in
1807 mtx_lock(&sc->vmbus_prichan_lock);
1808 vmbus_chan_rem_prilist(sc, chan);
1809 mtx_unlock(&sc->vmbus_prichan_lock);
1814 * The following processing order is critical:
1815 * Set the REVOKED state flag before orphaning the installed xact.
1818 if (atomic_testandset_int(&chan->ch_stflags,
1819 VMBUS_CHAN_ST_REVOKED_SHIFT))
1820 panic("channel has already been revoked");
1822 sx_xlock(&chan->ch_orphan_lock);
1823 if (chan->ch_orphan_xact != NULL)
1824 vmbus_xact_ctx_orphan(chan->ch_orphan_xact);
1825 sx_xunlock(&chan->ch_orphan_lock);
1828 vmbus_chan_printf(chan, "chan%u revoked\n", note->chm_chanid);
1829 vmbus_chan_detach(chan);
1833 vmbus_chan_release(struct vmbus_channel *chan)
1835 struct vmbus_softc *sc = chan->ch_vmbus;
1836 struct vmbus_chanmsg_chfree *req;
1837 struct vmbus_msghc *mh;
1840 mh = vmbus_msghc_get(sc, sizeof(*req));
1842 vmbus_chan_printf(chan,
1843 "can not get msg hypercall for chfree(chan%u)\n",
1848 req = vmbus_msghc_dataptr(mh);
1849 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
1850 req->chm_chanid = chan->ch_id;
1852 error = vmbus_msghc_exec_noresult(mh);
1853 vmbus_msghc_put(sc, mh);
1856 vmbus_chan_printf(chan,
1857 "chfree(chan%u) msg hypercall exec failed: %d\n",
1858 chan->ch_id, error);
1861 vmbus_chan_printf(chan, "chan%u freed\n", chan->ch_id);
1867 vmbus_prichan_detach_task(void *xchan, int pending __unused)
1869 struct vmbus_channel *chan = xchan;
1871 KASSERT(VMBUS_CHAN_ISPRIMARY(chan),
1872 ("chan%u is not primary channel", chan->ch_id));
1874 /* Delete and detach the device associated with this channel. */
1875 vmbus_delete_child(chan);
1877 /* Release this channel (back to vmbus). */
1878 vmbus_chan_release(chan);
1880 /* Free this channel's resource. */
1881 vmbus_chan_free(chan);
1885 vmbus_subchan_detach_task(void *xchan, int pending __unused)
1887 struct vmbus_channel *chan = xchan;
1888 struct vmbus_channel *pri_chan = chan->ch_prichan;
1890 KASSERT(!VMBUS_CHAN_ISPRIMARY(chan),
1891 ("chan%u is primary channel", chan->ch_id));
1893 /* Release this channel (back to vmbus). */
1894 vmbus_chan_release(chan);
1896 /* Unlink from its primary channel's sub-channel list. */
1897 mtx_lock(&pri_chan->ch_subchan_lock);
1898 vmbus_chan_rem_sublist(pri_chan, chan);
1899 mtx_unlock(&pri_chan->ch_subchan_lock);
1900 /* Notify anyone that is waiting for this sub-channel to vanish. */
1903 /* Free this channel's resource. */
1904 vmbus_chan_free(chan);
1908 vmbus_prichan_attach_task(void *xchan, int pending __unused)
1912 * Add device for this primary channel.
1914 vmbus_add_child(xchan);
1918 vmbus_subchan_attach_task(void *xchan __unused, int pending __unused)
1925 vmbus_chan_destroy_all(struct vmbus_softc *sc)
1929 * Detach all devices and destroy the corresponding primary
1933 struct vmbus_channel *chan;
1935 mtx_lock(&sc->vmbus_chan_lock);
1936 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
1937 if (VMBUS_CHAN_ISPRIMARY(chan))
1941 /* No more primary channels; done. */
1942 mtx_unlock(&sc->vmbus_chan_lock);
1945 vmbus_chan_rem_list(sc, chan);
1946 mtx_unlock(&sc->vmbus_chan_lock);
1948 mtx_lock(&sc->vmbus_prichan_lock);
1949 vmbus_chan_rem_prilist(sc, chan);
1950 mtx_unlock(&sc->vmbus_prichan_lock);
1952 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
1956 struct vmbus_channel **
1957 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt)
1959 struct vmbus_channel **ret, *chan;
1962 KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt));
1964 ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP,
1967 mtx_lock(&pri_chan->ch_subchan_lock);
1969 while (pri_chan->ch_subchan_cnt < subchan_cnt)
1970 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
1973 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
1974 /* TODO: refcnt chan */
1978 if (i == subchan_cnt)
1981 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
1982 pri_chan->ch_subchan_cnt, subchan_cnt));
1984 mtx_unlock(&pri_chan->ch_subchan_lock);
1990 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused)
1993 free(subchan, M_TEMP);
1997 vmbus_subchan_drain(struct vmbus_channel *pri_chan)
1999 mtx_lock(&pri_chan->ch_subchan_lock);
2000 while (pri_chan->ch_subchan_cnt > 0)
2001 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
2002 mtx_unlock(&pri_chan->ch_subchan_lock);
2006 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
2008 vmbus_chanmsg_proc_t msg_proc;
2011 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
2012 KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX,
2013 ("invalid message type %u", msg_type));
2015 msg_proc = vmbus_chan_msgprocs[msg_type];
2016 if (msg_proc != NULL)
2021 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
2024 chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
2026 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
2030 vmbus_chan_id(const struct vmbus_channel *chan)
2036 vmbus_chan_subidx(const struct vmbus_channel *chan)
2038 return chan->ch_subidx;
2042 vmbus_chan_is_primary(const struct vmbus_channel *chan)
2044 if (VMBUS_CHAN_ISPRIMARY(chan))
2050 const struct hyperv_guid *
2051 vmbus_chan_guid_inst(const struct vmbus_channel *chan)
2053 return &chan->ch_guid_inst;
2057 vmbus_chan_prplist_nelem(int br_size, int prpcnt_max, int dlen_max)
2061 elem_size = __offsetof(struct vmbus_chanpkt_prplist,
2062 cp_range[0].gpa_page[prpcnt_max]);
2063 elem_size += dlen_max;
2064 elem_size = VMBUS_CHANPKT_TOTLEN(elem_size);
2066 return (vmbus_br_nelem(br_size, elem_size));
2070 vmbus_chan_tx_empty(const struct vmbus_channel *chan)
2073 return (vmbus_txbr_empty(&chan->ch_txbr));
2077 vmbus_chan_rx_empty(const struct vmbus_channel *chan)
2080 return (vmbus_rxbr_empty(&chan->ch_rxbr));
2084 vmbus_chan_printf(const struct vmbus_channel *chan, const char *fmt, ...)
2090 if (chan->ch_dev == NULL || !device_is_alive(chan->ch_dev))
2091 dev = chan->ch_vmbus->vmbus_dev;
2095 retval = device_print_prettyname(dev);
2097 retval += vprintf(fmt, ap);
2104 vmbus_chan_run_task(struct vmbus_channel *chan, struct task *task)
2107 taskqueue_enqueue(chan->ch_tq, task);
2108 taskqueue_drain(chan->ch_tq, task);
2112 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan)
2115 return (chan->ch_mgmt_tq);
2119 vmbus_chan_is_revoked(const struct vmbus_channel *chan)
2122 if (chan->ch_stflags & VMBUS_CHAN_ST_REVOKED)
2128 vmbus_chan_set_orphan(struct vmbus_channel *chan, struct vmbus_xact_ctx *xact)
2131 sx_xlock(&chan->ch_orphan_lock);
2132 chan->ch_orphan_xact = xact;
2133 sx_xunlock(&chan->ch_orphan_lock);
2137 vmbus_chan_unset_orphan(struct vmbus_channel *chan)
2140 sx_xlock(&chan->ch_orphan_lock);
2141 chan->ch_orphan_xact = NULL;
2142 sx_xunlock(&chan->ch_orphan_lock);
2146 vmbus_chan_xact_wait(const struct vmbus_channel *chan,
2147 struct vmbus_xact *xact, size_t *resp_len, bool can_sleep)
2152 ret = vmbus_xact_wait(xact, resp_len);
2154 ret = vmbus_xact_busywait(xact, resp_len);
2155 if (vmbus_chan_is_revoked(chan)) {
2157 * This xact probably is interrupted, and the
2158 * interruption can race the reply reception,
2159 * so we have to make sure that there are nothing
2160 * left on the RX bufring, i.e. this xact will
2161 * not be touched, once this function returns.
2163 * Since the hypervisor will not put more data
2164 * onto the RX bufring once the channel is revoked,
2165 * the following loop will be terminated, once all
2166 * data are drained by the driver's channel
2169 while (!vmbus_chan_rx_empty(chan)) {
2180 vmbus_chan_poll_enable(struct vmbus_channel *chan, u_int pollhz)
2182 struct vmbus_chan_pollarg arg;
2183 struct task poll_cfg;
2185 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2186 ("enable polling on non-batch chan%u", chan->ch_id));
2187 KASSERT(pollhz >= VMBUS_CHAN_POLLHZ_MIN &&
2188 pollhz <= VMBUS_CHAN_POLLHZ_MAX, ("invalid pollhz %u", pollhz));
2190 arg.poll_chan = chan;
2191 arg.poll_hz = pollhz;
2192 TASK_INIT(&poll_cfg, 0, vmbus_chan_pollcfg_task, &arg);
2193 vmbus_chan_run_task(chan, &poll_cfg);
2197 vmbus_chan_poll_disable(struct vmbus_channel *chan)
2199 struct task poll_dis;
2201 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2202 ("disable polling on non-batch chan%u", chan->ch_id));
2204 TASK_INIT(&poll_dis, 0, vmbus_chan_polldis_task, chan);
2205 vmbus_chan_run_task(chan, &poll_dis);