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>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
41 #include <machine/atomic.h>
42 #include <machine/bus.h>
45 #include <vm/vm_param.h>
48 #include <dev/hyperv/include/hyperv_busdma.h>
49 #include <dev/hyperv/vmbus/hv_vmbus_priv.h>
50 #include <dev/hyperv/vmbus/hyperv_var.h>
51 #include <dev/hyperv/vmbus/vmbus_reg.h>
52 #include <dev/hyperv/vmbus/vmbus_var.h>
54 static void vmbus_chan_update_evtflagcnt(struct vmbus_softc *,
55 const struct vmbus_channel *);
57 static void vmbus_chan_task(void *, int);
58 static void vmbus_chan_task_nobatch(void *, int);
59 static void vmbus_chan_detach_task(void *, int);
61 static void vmbus_chan_msgproc_choffer(struct vmbus_softc *,
62 const struct vmbus_message *);
63 static void vmbus_chan_msgproc_chrescind(struct vmbus_softc *,
64 const struct vmbus_message *);
67 * Vmbus channel message processing.
69 static const vmbus_chanmsg_proc_t
70 vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = {
71 VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer),
72 VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind),
74 VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP),
75 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP),
76 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP)
80 * Notify host that there are data pending on our TX bufring.
83 vmbus_chan_signal_tx(const struct vmbus_channel *chan)
85 atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask);
86 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
87 atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask);
89 hypercall_signal_event(chan->ch_monprm_dma.hv_paddr);
93 vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS)
95 struct vmbus_channel *chan = arg1;
98 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
100 return sysctl_handle_int(oidp, &mnf, 0, req);
104 vmbus_chan_sysctl_create(struct vmbus_channel *chan)
106 struct sysctl_oid *ch_tree, *chid_tree, *br_tree;
107 struct sysctl_ctx_list *ctx;
112 * Add sysctl nodes related to this channel to this
113 * channel's sysctl ctx, so that they can be destroyed
114 * independently upon close of this channel, which can
115 * happen even if the device is not detached.
117 ctx = &chan->ch_sysctl_ctx;
118 sysctl_ctx_init(ctx);
121 * Create dev.NAME.UNIT.channel tree.
123 ch_tree = SYSCTL_ADD_NODE(ctx,
124 SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)),
125 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
130 * Create dev.NAME.UNIT.channel.CHANID tree.
132 if (VMBUS_CHAN_ISPRIMARY(chan))
135 ch_id = chan->ch_prichan->ch_id;
136 snprintf(name, sizeof(name), "%d", ch_id);
137 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
138 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
139 if (chid_tree == NULL)
142 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
144 * Create dev.NAME.UNIT.channel.CHANID.sub tree.
146 ch_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree),
147 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
152 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree.
155 * chid_tree is changed to this new sysctl tree.
157 snprintf(name, sizeof(name), "%d", chan->ch_subidx);
158 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
159 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
160 if (chid_tree == NULL)
163 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
164 "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id");
167 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
168 "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id");
169 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
170 "mnf", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
171 chan, 0, vmbus_chan_sysctl_mnf, "I",
172 "has monitor notification facilities");
174 br_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
175 "br", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
176 if (br_tree != NULL) {
178 * Create sysctl tree for RX bufring.
180 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr, "rx");
182 * Create sysctl tree for TX bufring.
184 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr, "tx");
189 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size,
190 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
192 struct vmbus_softc *sc = chan->ch_vmbus;
193 const struct vmbus_chanmsg_chopen_resp *resp;
194 const struct vmbus_message *msg;
195 struct vmbus_chanmsg_chopen *req;
196 struct vmbus_msghc *mh;
201 if (udlen > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
202 device_printf(sc->vmbus_dev,
203 "invalid udata len %d for chan%u\n", udlen, chan->ch_id);
206 KASSERT((txbr_size & PAGE_MASK) == 0,
207 ("send bufring size is not multiple page"));
208 KASSERT((rxbr_size & PAGE_MASK) == 0,
209 ("recv bufring size is not multiple page"));
211 if (atomic_testandset_int(&chan->ch_stflags,
212 VMBUS_CHAN_ST_OPENED_SHIFT))
213 panic("double-open chan%u", chan->ch_id);
216 chan->ch_cbarg = cbarg;
218 vmbus_chan_update_evtflagcnt(sc, chan);
220 chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid);
221 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
222 TASK_INIT(&chan->ch_task, 0, vmbus_chan_task, chan);
224 TASK_INIT(&chan->ch_task, 0, vmbus_chan_task_nobatch, chan);
227 * Allocate the TX+RX bufrings.
228 * XXX should use ch_dev dtag
230 br = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev),
231 PAGE_SIZE, 0, txbr_size + rxbr_size, &chan->ch_bufring_dma,
232 BUS_DMA_WAITOK | BUS_DMA_ZERO);
234 device_printf(sc->vmbus_dev, "bufring allocation failed\n");
238 chan->ch_bufring = br;
240 /* TX bufring comes first */
241 hv_vmbus_ring_buffer_init(&chan->ch_txbr, br, txbr_size);
242 /* RX bufring immediately follows TX bufring */
243 hv_vmbus_ring_buffer_init(&chan->ch_rxbr, br + txbr_size, rxbr_size);
245 /* Create sysctl tree for this channel */
246 vmbus_chan_sysctl_create(chan);
249 * Connect the bufrings, both RX and TX, to this channel.
251 error = vmbus_chan_gpadl_connect(chan, chan->ch_bufring_dma.hv_paddr,
252 txbr_size + rxbr_size, &chan->ch_bufring_gpadl);
254 device_printf(sc->vmbus_dev,
255 "failed to connect bufring GPADL to chan%u\n", chan->ch_id);
260 * Open channel w/ the bufring GPADL on the target CPU.
262 mh = vmbus_msghc_get(sc, sizeof(*req));
264 device_printf(sc->vmbus_dev,
265 "can not get msg hypercall for chopen(chan%u)\n",
271 req = vmbus_msghc_dataptr(mh);
272 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
273 req->chm_chanid = chan->ch_id;
274 req->chm_openid = chan->ch_id;
275 req->chm_gpadl = chan->ch_bufring_gpadl;
276 req->chm_vcpuid = chan->ch_vcpuid;
277 req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT;
279 memcpy(req->chm_udata, udata, udlen);
281 error = vmbus_msghc_exec(sc, mh);
283 device_printf(sc->vmbus_dev,
284 "chopen(chan%u) msg hypercall exec failed: %d\n",
286 vmbus_msghc_put(sc, mh);
290 msg = vmbus_msghc_wait_result(sc, mh);
291 resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data;
292 status = resp->chm_status;
294 vmbus_msghc_put(sc, mh);
298 device_printf(sc->vmbus_dev, "chan%u opened\n",
304 device_printf(sc->vmbus_dev, "failed to open chan%u\n", chan->ch_id);
308 if (chan->ch_bufring_gpadl) {
309 vmbus_chan_gpadl_disconnect(chan, chan->ch_bufring_gpadl);
310 chan->ch_bufring_gpadl = 0;
312 if (chan->ch_bufring != NULL) {
313 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring);
314 chan->ch_bufring = NULL;
316 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
321 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr,
322 int size, uint32_t *gpadl0)
324 struct vmbus_softc *sc = chan->ch_vmbus;
325 struct vmbus_msghc *mh;
326 struct vmbus_chanmsg_gpadl_conn *req;
327 const struct vmbus_message *msg;
329 uint32_t gpadl, status;
330 int page_count, range_len, i, cnt, error;
334 * Preliminary checks.
337 KASSERT((size & PAGE_MASK) == 0,
338 ("invalid GPA size %d, not multiple page size", size));
339 page_count = size >> PAGE_SHIFT;
341 KASSERT((paddr & PAGE_MASK) == 0,
342 ("GPA is not page aligned %jx", (uintmax_t)paddr));
343 page_id = paddr >> PAGE_SHIFT;
345 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
347 * We don't support multiple GPA ranges.
349 if (range_len > UINT16_MAX) {
350 device_printf(sc->vmbus_dev, "GPA too large, %d pages\n",
358 gpadl = vmbus_gpadl_alloc(sc);
362 * Connect this GPADL to the target channel.
365 * Since each message can only hold small set of page
366 * addresses, several messages may be required to
367 * complete the connection.
369 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
370 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
375 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
376 chm_range.gpa_page[cnt]);
377 mh = vmbus_msghc_get(sc, reqsz);
379 device_printf(sc->vmbus_dev,
380 "can not get msg hypercall for gpadl->chan%u\n",
385 req = vmbus_msghc_dataptr(mh);
386 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
387 req->chm_chanid = chan->ch_id;
388 req->chm_gpadl = gpadl;
389 req->chm_range_len = range_len;
390 req->chm_range_cnt = 1;
391 req->chm_range.gpa_len = size;
392 req->chm_range.gpa_ofs = 0;
393 for (i = 0; i < cnt; ++i)
394 req->chm_range.gpa_page[i] = page_id++;
396 error = vmbus_msghc_exec(sc, mh);
398 device_printf(sc->vmbus_dev,
399 "gpadl->chan%u msg hypercall exec failed: %d\n",
401 vmbus_msghc_put(sc, mh);
405 while (page_count > 0) {
406 struct vmbus_chanmsg_gpadl_subconn *subreq;
408 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
409 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
414 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
416 vmbus_msghc_reset(mh, reqsz);
418 subreq = vmbus_msghc_dataptr(mh);
419 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
420 subreq->chm_gpadl = gpadl;
421 for (i = 0; i < cnt; ++i)
422 subreq->chm_gpa_page[i] = page_id++;
424 vmbus_msghc_exec_noresult(mh);
426 KASSERT(page_count == 0, ("invalid page count %d", page_count));
428 msg = vmbus_msghc_wait_result(sc, mh);
429 status = ((const struct vmbus_chanmsg_gpadl_connresp *)
430 msg->msg_data)->chm_status;
432 vmbus_msghc_put(sc, mh);
435 device_printf(sc->vmbus_dev, "gpadl->chan%u failed: "
436 "status %u\n", chan->ch_id, status);
440 device_printf(sc->vmbus_dev, "gpadl->chan%u "
441 "succeeded\n", chan->ch_id);
448 * Disconnect the GPA from the target channel
451 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl)
453 struct vmbus_softc *sc = chan->ch_vmbus;
454 struct vmbus_msghc *mh;
455 struct vmbus_chanmsg_gpadl_disconn *req;
458 mh = vmbus_msghc_get(sc, sizeof(*req));
460 device_printf(sc->vmbus_dev,
461 "can not get msg hypercall for gpa x->chan%u\n",
466 req = vmbus_msghc_dataptr(mh);
467 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
468 req->chm_chanid = chan->ch_id;
469 req->chm_gpadl = gpadl;
471 error = vmbus_msghc_exec(sc, mh);
473 device_printf(sc->vmbus_dev,
474 "gpa x->chan%u msg hypercall exec failed: %d\n",
476 vmbus_msghc_put(sc, mh);
480 vmbus_msghc_wait_result(sc, mh);
481 /* Discard result; no useful information */
482 vmbus_msghc_put(sc, mh);
488 vmbus_chan_close_internal(struct vmbus_channel *chan)
490 struct vmbus_softc *sc = chan->ch_vmbus;
491 struct vmbus_msghc *mh;
492 struct vmbus_chanmsg_chclose *req;
493 struct taskqueue *tq = chan->ch_tq;
496 /* TODO: stringent check */
497 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
500 * Free this channel's sysctl tree attached to its device's
503 sysctl_ctx_free(&chan->ch_sysctl_ctx);
506 * Set ch_tq to NULL to avoid more requests be scheduled.
507 * XXX pretty broken; need rework.
510 taskqueue_drain(tq, &chan->ch_task);
514 * Close this channel.
516 mh = vmbus_msghc_get(sc, sizeof(*req));
518 device_printf(sc->vmbus_dev,
519 "can not get msg hypercall for chclose(chan%u)\n",
524 req = vmbus_msghc_dataptr(mh);
525 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
526 req->chm_chanid = chan->ch_id;
528 error = vmbus_msghc_exec_noresult(mh);
529 vmbus_msghc_put(sc, mh);
532 device_printf(sc->vmbus_dev,
533 "chclose(chan%u) msg hypercall exec failed: %d\n",
536 } else if (bootverbose) {
537 device_printf(sc->vmbus_dev, "close chan%u\n", chan->ch_id);
541 * Disconnect the TX+RX bufrings from this channel.
543 if (chan->ch_bufring_gpadl) {
544 vmbus_chan_gpadl_disconnect(chan, chan->ch_bufring_gpadl);
545 chan->ch_bufring_gpadl = 0;
549 * Destroy the TX+RX bufrings.
551 hv_ring_buffer_cleanup(&chan->ch_txbr);
552 hv_ring_buffer_cleanup(&chan->ch_rxbr);
553 if (chan->ch_bufring != NULL) {
554 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring);
555 chan->ch_bufring = NULL;
560 * Caller should make sure that all sub-channels have
561 * been added to 'chan' and all to-be-closed channels
562 * are not being opened.
565 vmbus_chan_close(struct vmbus_channel *chan)
569 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
571 * Sub-channel is closed when its primary channel
578 * Close all sub-channels, if any.
580 subchan_cnt = chan->ch_subchan_cnt;
581 if (subchan_cnt > 0) {
582 struct vmbus_channel **subchan;
585 subchan = vmbus_subchan_get(chan, subchan_cnt);
586 for (i = 0; i < subchan_cnt; ++i)
587 vmbus_chan_close_internal(subchan[i]);
588 vmbus_subchan_rel(subchan, subchan_cnt);
591 /* Then close the primary channel. */
592 vmbus_chan_close_internal(chan);
596 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags,
597 void *data, int dlen, uint64_t xactid)
599 struct vmbus_chanpkt pkt;
600 int pktlen, pad_pktlen, hlen, error;
606 pktlen = hlen + dlen;
607 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
609 pkt.cp_hdr.cph_type = type;
610 pkt.cp_hdr.cph_flags = flags;
611 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
612 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
613 pkt.cp_hdr.cph_xactid = xactid;
615 iov[0].iov_base = &pkt;
616 iov[0].iov_len = hlen;
617 iov[1].iov_base = data;
618 iov[1].iov_len = dlen;
619 iov[2].iov_base = &pad;
620 iov[2].iov_len = pad_pktlen - pktlen;
622 error = hv_ring_buffer_write(&chan->ch_txbr, iov, 3, &send_evt);
623 if (!error && send_evt)
624 vmbus_chan_signal_tx(chan);
629 vmbus_chan_send_sglist(struct vmbus_channel *chan,
630 struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid)
632 struct vmbus_chanpkt_sglist pkt;
633 int pktlen, pad_pktlen, hlen, error;
638 KASSERT(sglen < VMBUS_CHAN_SGLIST_MAX,
639 ("invalid sglist len %d", sglen));
641 hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]);
642 pktlen = hlen + dlen;
643 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
645 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
646 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
647 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
648 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
649 pkt.cp_hdr.cph_xactid = xactid;
651 pkt.cp_gpa_cnt = sglen;
653 iov[0].iov_base = &pkt;
654 iov[0].iov_len = sizeof(pkt);
655 iov[1].iov_base = sg;
656 iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen;
657 iov[2].iov_base = data;
658 iov[2].iov_len = dlen;
659 iov[3].iov_base = &pad;
660 iov[3].iov_len = pad_pktlen - pktlen;
662 error = hv_ring_buffer_write(&chan->ch_txbr, iov, 4, &send_evt);
663 if (!error && send_evt)
664 vmbus_chan_signal_tx(chan);
669 vmbus_chan_send_prplist(struct vmbus_channel *chan,
670 struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen,
673 struct vmbus_chanpkt_prplist pkt;
674 int pktlen, pad_pktlen, hlen, error;
679 KASSERT(prp_cnt < VMBUS_CHAN_PRPLIST_MAX,
680 ("invalid prplist entry count %d", prp_cnt));
682 hlen = __offsetof(struct vmbus_chanpkt_prplist,
683 cp_range[0].gpa_page[prp_cnt]);
684 pktlen = hlen + dlen;
685 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
687 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
688 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
689 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
690 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
691 pkt.cp_hdr.cph_xactid = xactid;
693 pkt.cp_range_cnt = 1;
695 iov[0].iov_base = &pkt;
696 iov[0].iov_len = sizeof(pkt);
697 iov[1].iov_base = prp;
698 iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]);
699 iov[2].iov_base = data;
700 iov[2].iov_len = dlen;
701 iov[3].iov_base = &pad;
702 iov[3].iov_len = pad_pktlen - pktlen;
704 error = hv_ring_buffer_write(&chan->ch_txbr, iov, 4, &send_evt);
705 if (!error && send_evt)
706 vmbus_chan_signal_tx(chan);
711 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0,
714 struct vmbus_chanpkt_hdr pkt;
715 int error, dlen, hlen;
717 error = hv_ring_buffer_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
721 hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen);
722 dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
725 /* Return the size of this packet's data. */
730 *xactid = pkt.cph_xactid;
733 /* Skip packet header */
734 error = hv_ring_buffer_read(&chan->ch_rxbr, data, dlen, hlen);
735 KASSERT(!error, ("hv_ring_buffer_read failed"));
741 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
742 struct vmbus_chanpkt_hdr *pkt0, int *pktlen0)
744 struct vmbus_chanpkt_hdr pkt;
747 error = hv_ring_buffer_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
751 pktlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen);
752 if (*pktlen0 < pktlen) {
753 /* Return the size of this packet. */
759 /* Include packet header */
760 error = hv_ring_buffer_read(&chan->ch_rxbr, pkt0, pktlen, 0);
761 KASSERT(!error, ("hv_ring_buffer_read failed"));
767 vmbus_chan_task(void *xchan, int pending __unused)
769 struct vmbus_channel *chan = xchan;
770 vmbus_chan_callback_t cb = chan->ch_cb;
771 void *cbarg = chan->ch_cbarg;
774 * Optimize host to guest signaling by ensuring:
775 * 1. While reading the channel, we disable interrupts from
777 * 2. Ensure that we process all posted messages from the host
778 * before returning from this callback.
779 * 3. Once we return, enable signaling from the host. Once this
780 * state is set we check to see if additional packets are
781 * available to read. In this case we repeat the process.
783 * NOTE: Interrupt has been disabled in the ISR.
790 left = hv_ring_buffer_read_end(&chan->ch_rxbr);
792 /* No more data in RX bufring; done */
795 hv_ring_buffer_read_begin(&chan->ch_rxbr);
800 vmbus_chan_task_nobatch(void *xchan, int pending __unused)
802 struct vmbus_channel *chan = xchan;
804 chan->ch_cb(chan, chan->ch_cbarg);
808 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
813 for (f = 0; f < flag_cnt; ++f) {
818 if (event_flags[f] == 0)
821 flags = atomic_swap_long(&event_flags[f], 0);
822 chid_base = f << VMBUS_EVTFLAG_SHIFT;
824 while ((chid_ofs = ffsl(flags)) != 0) {
825 struct vmbus_channel *chan;
827 --chid_ofs; /* NOTE: ffsl is 1-based */
828 flags &= ~(1UL << chid_ofs);
830 chan = sc->vmbus_chmap[chid_base + chid_ofs];
832 /* if channel is closed or closing */
833 if (chan == NULL || chan->ch_tq == NULL)
836 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
837 hv_ring_buffer_read_begin(&chan->ch_rxbr);
838 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
844 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
846 struct vmbus_evtflags *eventf;
849 * On Host with Win8 or above, the event page can be checked directly
850 * to get the id of the channel that has the pending interrupt.
852 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
853 vmbus_event_flags_proc(sc, eventf->evt_flags,
854 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
858 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
860 struct vmbus_evtflags *eventf;
862 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
863 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
864 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
865 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
870 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
871 const struct vmbus_channel *chan)
873 volatile int *flag_cnt_ptr;
876 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
877 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
882 old_flag_cnt = *flag_cnt_ptr;
883 if (old_flag_cnt >= flag_cnt)
885 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
887 device_printf(sc->vmbus_dev,
888 "channel%u update cpu%d flag_cnt to %d\n",
889 chan->ch_id, chan->ch_cpuid, flag_cnt);
896 static struct vmbus_channel *
897 vmbus_chan_alloc(struct vmbus_softc *sc)
899 struct vmbus_channel *chan;
901 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
903 chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev),
904 HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param),
905 &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
906 if (chan->ch_monprm == NULL) {
907 device_printf(sc->vmbus_dev, "monprm alloc failed\n");
908 free(chan, M_DEVBUF);
913 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
914 TAILQ_INIT(&chan->ch_subchans);
915 TASK_INIT(&chan->ch_detach_task, 0, vmbus_chan_detach_task, chan);
921 vmbus_chan_free(struct vmbus_channel *chan)
923 /* TODO: assert sub-channel list is empty */
924 /* TODO: asset no longer on the primary channel's sub-channel list */
925 /* TODO: asset no longer on the vmbus channel list */
926 hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm);
927 mtx_destroy(&chan->ch_subchan_lock);
928 free(chan, M_DEVBUF);
932 vmbus_chan_add(struct vmbus_channel *newchan)
934 struct vmbus_softc *sc = newchan->ch_vmbus;
935 struct vmbus_channel *prichan;
937 if (newchan->ch_id == 0) {
940 * Chan0 will neither be processed nor should be offered;
943 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
945 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
946 device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
950 sc->vmbus_chmap[newchan->ch_id] = newchan;
953 device_printf(sc->vmbus_dev, "chan%u subidx%u offer\n",
954 newchan->ch_id, newchan->ch_subidx);
957 mtx_lock(&sc->vmbus_prichan_lock);
958 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
960 * Sub-channel will have the same type GUID and instance
961 * GUID as its primary channel.
963 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
964 sizeof(struct hyperv_guid)) == 0 &&
965 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
966 sizeof(struct hyperv_guid)) == 0)
969 if (VMBUS_CHAN_ISPRIMARY(newchan)) {
970 if (prichan == NULL) {
971 /* Install the new primary channel */
972 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, newchan,
974 mtx_unlock(&sc->vmbus_prichan_lock);
977 mtx_unlock(&sc->vmbus_prichan_lock);
978 device_printf(sc->vmbus_dev, "duplicated primary "
979 "chan%u\n", newchan->ch_id);
982 } else { /* Sub-channel */
983 if (prichan == NULL) {
984 mtx_unlock(&sc->vmbus_prichan_lock);
985 device_printf(sc->vmbus_dev, "no primary chan for "
986 "chan%u\n", newchan->ch_id);
990 * Found the primary channel for this sub-channel and
996 mtx_unlock(&sc->vmbus_prichan_lock);
999 * This is a sub-channel; link it with the primary channel.
1001 KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan),
1002 ("new channel is not sub-channel"));
1003 KASSERT(prichan != NULL, ("no primary channel"));
1005 newchan->ch_prichan = prichan;
1006 newchan->ch_dev = prichan->ch_dev;
1008 mtx_lock(&prichan->ch_subchan_lock);
1009 TAILQ_INSERT_TAIL(&prichan->ch_subchans, newchan, ch_sublink);
1011 * Bump up sub-channel count and notify anyone that is
1012 * interested in this sub-channel, after this sub-channel
1015 prichan->ch_subchan_cnt++;
1016 mtx_unlock(&prichan->ch_subchan_lock);
1023 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1025 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
1027 if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1028 chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1029 /* Only cpu0 is supported */
1033 chan->ch_cpuid = cpu;
1034 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1037 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
1038 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1043 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1045 static uint32_t vmbus_chan_nextcpu;
1048 cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
1049 vmbus_chan_cpu_set(chan, cpu);
1053 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1056 * By default, pin the channel to cpu0. Devices having
1057 * special channel-cpu mapping requirement should call
1058 * vmbus_chan_cpu_{set,rr}().
1060 vmbus_chan_cpu_set(chan, 0);
1064 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc,
1065 const struct vmbus_message *msg)
1067 const struct vmbus_chanmsg_choffer *offer;
1068 struct vmbus_channel *chan;
1071 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1073 chan = vmbus_chan_alloc(sc);
1075 device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1080 chan->ch_id = offer->chm_chanid;
1081 chan->ch_subidx = offer->chm_subidx;
1082 chan->ch_guid_type = offer->chm_chtype;
1083 chan->ch_guid_inst = offer->chm_chinst;
1085 /* Batch reading is on by default */
1086 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1088 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1089 if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1090 chan->ch_monprm->mp_connid = offer->chm_connid;
1092 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1098 chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1100 trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1101 if (trig_idx >= VMBUS_MONTRIGS_MAX)
1102 panic("invalid monitor trigger %u", offer->chm_montrig);
1104 &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1106 chan->ch_montrig_mask =
1107 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1114 &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1115 chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1117 /* Select default cpu for this channel. */
1118 vmbus_chan_cpu_default(chan);
1120 error = vmbus_chan_add(chan);
1122 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1123 chan->ch_id, error);
1124 vmbus_chan_free(chan);
1128 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1130 * Add device for this primary channel.
1133 * Error is ignored here; don't have much to do if error
1136 vmbus_add_child(chan);
1141 * XXX pretty broken; need rework.
1144 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc,
1145 const struct vmbus_message *msg)
1147 const struct vmbus_chanmsg_chrescind *note;
1148 struct vmbus_channel *chan;
1150 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1151 if (note->chm_chanid > VMBUS_CHAN_MAX) {
1152 device_printf(sc->vmbus_dev, "invalid rescinded chan%u\n",
1158 device_printf(sc->vmbus_dev, "chan%u rescinded\n",
1162 chan = sc->vmbus_chmap[note->chm_chanid];
1165 sc->vmbus_chmap[note->chm_chanid] = NULL;
1167 taskqueue_enqueue(taskqueue_thread, &chan->ch_detach_task);
1171 vmbus_chan_detach_task(void *xchan, int pending __unused)
1173 struct vmbus_channel *chan = xchan;
1175 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1176 /* Only primary channel owns the device */
1177 vmbus_delete_child(chan);
1178 /* NOTE: DO NOT free primary channel for now */
1180 struct vmbus_softc *sc = chan->ch_vmbus;
1181 struct vmbus_channel *pri_chan = chan->ch_prichan;
1182 struct vmbus_chanmsg_chfree *req;
1183 struct vmbus_msghc *mh;
1186 mh = vmbus_msghc_get(sc, sizeof(*req));
1188 device_printf(sc->vmbus_dev,
1189 "can not get msg hypercall for chfree(chan%u)\n",
1194 req = vmbus_msghc_dataptr(mh);
1195 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
1196 req->chm_chanid = chan->ch_id;
1198 error = vmbus_msghc_exec_noresult(mh);
1199 vmbus_msghc_put(sc, mh);
1202 device_printf(sc->vmbus_dev,
1203 "chfree(chan%u) failed: %d",
1204 chan->ch_id, error);
1205 /* NOTE: Move on! */
1208 device_printf(sc->vmbus_dev, "chan%u freed\n",
1213 mtx_lock(&pri_chan->ch_subchan_lock);
1214 TAILQ_REMOVE(&pri_chan->ch_subchans, chan, ch_sublink);
1215 KASSERT(pri_chan->ch_subchan_cnt > 0,
1216 ("invalid subchan_cnt %d", pri_chan->ch_subchan_cnt));
1217 pri_chan->ch_subchan_cnt--;
1218 mtx_unlock(&pri_chan->ch_subchan_lock);
1221 vmbus_chan_free(chan);
1226 * Detach all devices and destroy the corresponding primary channels.
1229 vmbus_chan_destroy_all(struct vmbus_softc *sc)
1231 struct vmbus_channel *chan;
1233 mtx_lock(&sc->vmbus_prichan_lock);
1234 while ((chan = TAILQ_FIRST(&sc->vmbus_prichans)) != NULL) {
1235 KASSERT(VMBUS_CHAN_ISPRIMARY(chan), ("not primary channel"));
1236 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink);
1237 mtx_unlock(&sc->vmbus_prichan_lock);
1239 vmbus_delete_child(chan);
1240 vmbus_chan_free(chan);
1242 mtx_lock(&sc->vmbus_prichan_lock);
1244 bzero(sc->vmbus_chmap,
1245 sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX);
1246 mtx_unlock(&sc->vmbus_prichan_lock);
1250 * The channel whose vcpu binding is closest to the currect vcpu will
1252 * If no multi-channel, always select primary channel.
1254 struct vmbus_channel *
1255 vmbus_chan_cpu2chan(struct vmbus_channel *prichan, int cpu)
1257 struct vmbus_channel *sel, *chan;
1258 uint32_t vcpu, sel_dist;
1260 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpuid %d", cpu));
1261 if (TAILQ_EMPTY(&prichan->ch_subchans))
1264 vcpu = VMBUS_PCPU_GET(prichan->ch_vmbus, vcpuid, cpu);
1266 #define CHAN_VCPU_DIST(ch, vcpu) \
1267 (((ch)->ch_vcpuid > (vcpu)) ? \
1268 ((ch)->ch_vcpuid - (vcpu)) : ((vcpu) - (ch)->ch_vcpuid))
1270 #define CHAN_SELECT(ch) \
1273 sel_dist = CHAN_VCPU_DIST(ch, vcpu); \
1276 CHAN_SELECT(prichan);
1278 mtx_lock(&prichan->ch_subchan_lock);
1279 TAILQ_FOREACH(chan, &prichan->ch_subchans, ch_sublink) {
1282 KASSERT(chan->ch_stflags & VMBUS_CHAN_ST_OPENED,
1283 ("chan%u is not opened", chan->ch_id));
1285 if (chan->ch_vcpuid == vcpu) {
1286 /* Exact match; done */
1291 dist = CHAN_VCPU_DIST(chan, vcpu);
1292 if (sel_dist <= dist) {
1293 /* Far or same distance; skip */
1297 /* Select the closer channel. */
1300 mtx_unlock(&prichan->ch_subchan_lock);
1303 #undef CHAN_VCPU_DIST
1308 struct vmbus_channel **
1309 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt)
1311 struct vmbus_channel **ret, *chan;
1314 ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP,
1317 mtx_lock(&pri_chan->ch_subchan_lock);
1319 while (pri_chan->ch_subchan_cnt < subchan_cnt)
1320 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
1323 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
1324 /* TODO: refcnt chan */
1328 if (i == subchan_cnt)
1331 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
1332 pri_chan->ch_subchan_cnt, subchan_cnt));
1334 mtx_unlock(&pri_chan->ch_subchan_lock);
1340 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused)
1343 free(subchan, M_TEMP);
1347 vmbus_subchan_drain(struct vmbus_channel *pri_chan)
1349 mtx_lock(&pri_chan->ch_subchan_lock);
1350 while (pri_chan->ch_subchan_cnt > 0)
1351 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
1352 mtx_unlock(&pri_chan->ch_subchan_lock);
1356 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
1358 vmbus_chanmsg_proc_t msg_proc;
1361 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
1362 KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX,
1363 ("invalid message type %u", msg_type));
1365 msg_proc = vmbus_chan_msgprocs[msg_type];
1366 if (msg_proc != NULL)
1371 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
1374 chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
1376 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1380 vmbus_chan_id(const struct vmbus_channel *chan)
1386 vmbus_chan_subidx(const struct vmbus_channel *chan)
1388 return chan->ch_subidx;
1392 vmbus_chan_is_primary(const struct vmbus_channel *chan)
1394 if (VMBUS_CHAN_ISPRIMARY(chan))
1400 const struct hyperv_guid *
1401 vmbus_chan_guid_inst(const struct vmbus_channel *chan)
1403 return &chan->ch_guid_inst;