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>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <machine/atomic.h>
43 #include <dev/hyperv/include/hyperv_busdma.h>
44 #include <dev/hyperv/vmbus/hyperv_var.h>
45 #include <dev/hyperv/vmbus/vmbus_reg.h>
46 #include <dev/hyperv/vmbus/vmbus_var.h>
47 #include <dev/hyperv/vmbus/vmbus_brvar.h>
48 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
50 static void vmbus_chan_update_evtflagcnt(
52 const struct vmbus_channel *);
53 static void vmbus_chan_close_internal(
54 struct vmbus_channel *);
55 static int vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS);
56 static void vmbus_chan_sysctl_create(
57 struct vmbus_channel *);
58 static struct vmbus_channel *vmbus_chan_alloc(struct vmbus_softc *);
59 static void vmbus_chan_free(struct vmbus_channel *);
60 static int vmbus_chan_add(struct vmbus_channel *);
61 static void vmbus_chan_cpu_default(struct vmbus_channel *);
63 static void vmbus_chan_task(void *, int);
64 static void vmbus_chan_task_nobatch(void *, int);
65 static void vmbus_chan_detach_task(void *, int);
67 static void vmbus_chan_msgproc_choffer(struct vmbus_softc *,
68 const struct vmbus_message *);
69 static void vmbus_chan_msgproc_chrescind(
71 const struct vmbus_message *);
74 * Vmbus channel message processing.
76 static const vmbus_chanmsg_proc_t
77 vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = {
78 VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer),
79 VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind),
81 VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP),
82 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP),
83 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP)
87 * Notify host that there are data pending on our TX bufring.
90 vmbus_chan_signal_tx(const struct vmbus_channel *chan)
92 atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask);
93 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
94 atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask);
96 hypercall_signal_event(chan->ch_monprm_dma.hv_paddr);
100 vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS)
102 struct vmbus_channel *chan = arg1;
105 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
107 return sysctl_handle_int(oidp, &mnf, 0, req);
111 vmbus_chan_sysctl_create(struct vmbus_channel *chan)
113 struct sysctl_oid *ch_tree, *chid_tree, *br_tree;
114 struct sysctl_ctx_list *ctx;
119 * Add sysctl nodes related to this channel to this
120 * channel's sysctl ctx, so that they can be destroyed
121 * independently upon close of this channel, which can
122 * happen even if the device is not detached.
124 ctx = &chan->ch_sysctl_ctx;
125 sysctl_ctx_init(ctx);
128 * Create dev.NAME.UNIT.channel tree.
130 ch_tree = SYSCTL_ADD_NODE(ctx,
131 SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)),
132 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
137 * Create dev.NAME.UNIT.channel.CHANID tree.
139 if (VMBUS_CHAN_ISPRIMARY(chan))
142 ch_id = chan->ch_prichan->ch_id;
143 snprintf(name, sizeof(name), "%d", ch_id);
144 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
145 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
146 if (chid_tree == NULL)
149 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
151 * Create dev.NAME.UNIT.channel.CHANID.sub tree.
153 ch_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree),
154 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
159 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree.
162 * chid_tree is changed to this new sysctl tree.
164 snprintf(name, sizeof(name), "%d", chan->ch_subidx);
165 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
166 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
167 if (chid_tree == NULL)
170 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
171 "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id");
174 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
175 "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id");
176 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
177 "mnf", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
178 chan, 0, vmbus_chan_sysctl_mnf, "I",
179 "has monitor notification facilities");
181 br_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
182 "br", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
183 if (br_tree != NULL) {
185 * Create sysctl tree for RX bufring.
187 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr.rxbr, "rx");
189 * Create sysctl tree for TX bufring.
191 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr.txbr, "tx");
196 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size,
197 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
199 struct vmbus_chan_br cbr;
203 * Allocate the TX+RX bufrings.
205 KASSERT(chan->ch_bufring == NULL, ("bufrings are allocated"));
206 chan->ch_bufring = hyperv_dmamem_alloc(bus_get_dma_tag(chan->ch_dev),
207 PAGE_SIZE, 0, txbr_size + rxbr_size, &chan->ch_bufring_dma,
209 if (chan->ch_bufring == NULL) {
210 device_printf(chan->ch_dev, "bufring allocation failed\n");
214 cbr.cbr = chan->ch_bufring;
215 cbr.cbr_paddr = chan->ch_bufring_dma.hv_paddr;
216 cbr.cbr_txsz = txbr_size;
217 cbr.cbr_rxsz = rxbr_size;
219 error = vmbus_chan_open_br(chan, &cbr, udata, udlen, cb, cbarg);
221 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring);
222 chan->ch_bufring = NULL;
228 vmbus_chan_open_br(struct vmbus_channel *chan, const struct vmbus_chan_br *cbr,
229 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg)
231 struct vmbus_softc *sc = chan->ch_vmbus;
232 const struct vmbus_chanmsg_chopen_resp *resp;
233 const struct vmbus_message *msg;
234 struct vmbus_chanmsg_chopen *req;
235 struct vmbus_msghc *mh;
237 int error, txbr_size, rxbr_size;
240 if (udlen > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
241 device_printf(sc->vmbus_dev,
242 "invalid udata len %d for chan%u\n", udlen, chan->ch_id);
247 txbr_size = cbr->cbr_txsz;
248 rxbr_size = cbr->cbr_rxsz;
249 KASSERT((txbr_size & PAGE_MASK) == 0,
250 ("send bufring size is not multiple page"));
251 KASSERT((rxbr_size & PAGE_MASK) == 0,
252 ("recv bufring size is not multiple page"));
253 KASSERT((cbr->cbr_paddr & PAGE_MASK) == 0,
254 ("bufring is not page aligned"));
257 * Zero out the TX/RX bufrings, in case that they were used before.
259 memset(br, 0, txbr_size + rxbr_size);
261 if (atomic_testandset_int(&chan->ch_stflags,
262 VMBUS_CHAN_ST_OPENED_SHIFT))
263 panic("double-open chan%u", chan->ch_id);
266 chan->ch_cbarg = cbarg;
268 vmbus_chan_update_evtflagcnt(sc, chan);
270 chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid);
271 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
272 TASK_INIT(&chan->ch_task, 0, vmbus_chan_task, chan);
274 TASK_INIT(&chan->ch_task, 0, vmbus_chan_task_nobatch, chan);
276 /* TX bufring comes first */
277 vmbus_txbr_setup(&chan->ch_txbr, br, txbr_size);
278 /* RX bufring immediately follows TX bufring */
279 vmbus_rxbr_setup(&chan->ch_rxbr, br + txbr_size, rxbr_size);
281 /* Create sysctl tree for this channel */
282 vmbus_chan_sysctl_create(chan);
285 * Connect the bufrings, both RX and TX, to this channel.
287 error = vmbus_chan_gpadl_connect(chan, cbr->cbr_paddr,
288 txbr_size + rxbr_size, &chan->ch_bufring_gpadl);
290 device_printf(sc->vmbus_dev,
291 "failed to connect bufring GPADL to chan%u\n", chan->ch_id);
296 * Open channel w/ the bufring GPADL on the target CPU.
298 mh = vmbus_msghc_get(sc, sizeof(*req));
300 device_printf(sc->vmbus_dev,
301 "can not get msg hypercall for chopen(chan%u)\n",
307 req = vmbus_msghc_dataptr(mh);
308 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
309 req->chm_chanid = chan->ch_id;
310 req->chm_openid = chan->ch_id;
311 req->chm_gpadl = chan->ch_bufring_gpadl;
312 req->chm_vcpuid = chan->ch_vcpuid;
313 req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT;
315 memcpy(req->chm_udata, udata, udlen);
317 error = vmbus_msghc_exec(sc, mh);
319 device_printf(sc->vmbus_dev,
320 "chopen(chan%u) msg hypercall exec failed: %d\n",
322 vmbus_msghc_put(sc, mh);
326 msg = vmbus_msghc_wait_result(sc, mh);
327 resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data;
328 status = resp->chm_status;
330 vmbus_msghc_put(sc, mh);
334 device_printf(sc->vmbus_dev, "chan%u opened\n",
340 device_printf(sc->vmbus_dev, "failed to open chan%u\n", chan->ch_id);
344 if (chan->ch_bufring_gpadl) {
345 vmbus_chan_gpadl_disconnect(chan, chan->ch_bufring_gpadl);
346 chan->ch_bufring_gpadl = 0;
348 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
353 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr,
354 int size, uint32_t *gpadl0)
356 struct vmbus_softc *sc = chan->ch_vmbus;
357 struct vmbus_msghc *mh;
358 struct vmbus_chanmsg_gpadl_conn *req;
359 const struct vmbus_message *msg;
361 uint32_t gpadl, status;
362 int page_count, range_len, i, cnt, error;
366 * Preliminary checks.
369 KASSERT((size & PAGE_MASK) == 0,
370 ("invalid GPA size %d, not multiple page size", size));
371 page_count = size >> PAGE_SHIFT;
373 KASSERT((paddr & PAGE_MASK) == 0,
374 ("GPA is not page aligned %jx", (uintmax_t)paddr));
375 page_id = paddr >> PAGE_SHIFT;
377 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
379 * We don't support multiple GPA ranges.
381 if (range_len > UINT16_MAX) {
382 device_printf(sc->vmbus_dev, "GPA too large, %d pages\n",
390 gpadl = vmbus_gpadl_alloc(sc);
394 * Connect this GPADL to the target channel.
397 * Since each message can only hold small set of page
398 * addresses, several messages may be required to
399 * complete the connection.
401 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
402 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
407 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
408 chm_range.gpa_page[cnt]);
409 mh = vmbus_msghc_get(sc, reqsz);
411 device_printf(sc->vmbus_dev,
412 "can not get msg hypercall for gpadl->chan%u\n",
417 req = vmbus_msghc_dataptr(mh);
418 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
419 req->chm_chanid = chan->ch_id;
420 req->chm_gpadl = gpadl;
421 req->chm_range_len = range_len;
422 req->chm_range_cnt = 1;
423 req->chm_range.gpa_len = size;
424 req->chm_range.gpa_ofs = 0;
425 for (i = 0; i < cnt; ++i)
426 req->chm_range.gpa_page[i] = page_id++;
428 error = vmbus_msghc_exec(sc, mh);
430 device_printf(sc->vmbus_dev,
431 "gpadl->chan%u msg hypercall exec failed: %d\n",
433 vmbus_msghc_put(sc, mh);
437 while (page_count > 0) {
438 struct vmbus_chanmsg_gpadl_subconn *subreq;
440 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
441 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
446 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
448 vmbus_msghc_reset(mh, reqsz);
450 subreq = vmbus_msghc_dataptr(mh);
451 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
452 subreq->chm_gpadl = gpadl;
453 for (i = 0; i < cnt; ++i)
454 subreq->chm_gpa_page[i] = page_id++;
456 vmbus_msghc_exec_noresult(mh);
458 KASSERT(page_count == 0, ("invalid page count %d", page_count));
460 msg = vmbus_msghc_wait_result(sc, mh);
461 status = ((const struct vmbus_chanmsg_gpadl_connresp *)
462 msg->msg_data)->chm_status;
464 vmbus_msghc_put(sc, mh);
467 device_printf(sc->vmbus_dev, "gpadl->chan%u failed: "
468 "status %u\n", chan->ch_id, status);
472 device_printf(sc->vmbus_dev, "gpadl->chan%u "
473 "succeeded\n", chan->ch_id);
480 * Disconnect the GPA from the target channel
483 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl)
485 struct vmbus_softc *sc = chan->ch_vmbus;
486 struct vmbus_msghc *mh;
487 struct vmbus_chanmsg_gpadl_disconn *req;
490 mh = vmbus_msghc_get(sc, sizeof(*req));
492 device_printf(sc->vmbus_dev,
493 "can not get msg hypercall for gpa x->chan%u\n",
498 req = vmbus_msghc_dataptr(mh);
499 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
500 req->chm_chanid = chan->ch_id;
501 req->chm_gpadl = gpadl;
503 error = vmbus_msghc_exec(sc, mh);
505 device_printf(sc->vmbus_dev,
506 "gpa x->chan%u msg hypercall exec failed: %d\n",
508 vmbus_msghc_put(sc, mh);
512 vmbus_msghc_wait_result(sc, mh);
513 /* Discard result; no useful information */
514 vmbus_msghc_put(sc, mh);
520 vmbus_chan_close_internal(struct vmbus_channel *chan)
522 struct vmbus_softc *sc = chan->ch_vmbus;
523 struct vmbus_msghc *mh;
524 struct vmbus_chanmsg_chclose *req;
525 struct taskqueue *tq = chan->ch_tq;
528 /* TODO: stringent check */
529 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
532 * Free this channel's sysctl tree attached to its device's
535 sysctl_ctx_free(&chan->ch_sysctl_ctx);
538 * Set ch_tq to NULL to avoid more requests be scheduled.
539 * XXX pretty broken; need rework.
542 taskqueue_drain(tq, &chan->ch_task);
546 * Close this channel.
548 mh = vmbus_msghc_get(sc, sizeof(*req));
550 device_printf(sc->vmbus_dev,
551 "can not get msg hypercall for chclose(chan%u)\n",
556 req = vmbus_msghc_dataptr(mh);
557 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
558 req->chm_chanid = chan->ch_id;
560 error = vmbus_msghc_exec_noresult(mh);
561 vmbus_msghc_put(sc, mh);
564 device_printf(sc->vmbus_dev,
565 "chclose(chan%u) msg hypercall exec failed: %d\n",
568 } else if (bootverbose) {
569 device_printf(sc->vmbus_dev, "close chan%u\n", chan->ch_id);
573 * Disconnect the TX+RX bufrings from this channel.
575 if (chan->ch_bufring_gpadl) {
576 vmbus_chan_gpadl_disconnect(chan, chan->ch_bufring_gpadl);
577 chan->ch_bufring_gpadl = 0;
581 * Destroy the TX+RX bufrings.
583 if (chan->ch_bufring != NULL) {
584 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring);
585 chan->ch_bufring = NULL;
590 * Caller should make sure that all sub-channels have
591 * been added to 'chan' and all to-be-closed channels
592 * are not being opened.
595 vmbus_chan_close(struct vmbus_channel *chan)
599 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
601 * Sub-channel is closed when its primary channel
608 * Close all sub-channels, if any.
610 subchan_cnt = chan->ch_subchan_cnt;
611 if (subchan_cnt > 0) {
612 struct vmbus_channel **subchan;
615 subchan = vmbus_subchan_get(chan, subchan_cnt);
616 for (i = 0; i < subchan_cnt; ++i)
617 vmbus_chan_close_internal(subchan[i]);
618 vmbus_subchan_rel(subchan, subchan_cnt);
621 /* Then close the primary channel. */
622 vmbus_chan_close_internal(chan);
626 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags,
627 void *data, int dlen, uint64_t xactid)
629 struct vmbus_chanpkt pkt;
630 int pktlen, pad_pktlen, hlen, error;
636 pktlen = hlen + dlen;
637 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
638 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
639 ("invalid packet size %d", pad_pktlen));
641 pkt.cp_hdr.cph_type = type;
642 pkt.cp_hdr.cph_flags = flags;
643 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
644 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
645 pkt.cp_hdr.cph_xactid = xactid;
647 iov[0].iov_base = &pkt;
648 iov[0].iov_len = hlen;
649 iov[1].iov_base = data;
650 iov[1].iov_len = dlen;
651 iov[2].iov_base = &pad;
652 iov[2].iov_len = pad_pktlen - pktlen;
654 error = vmbus_txbr_write(&chan->ch_txbr, iov, 3, &send_evt);
655 if (!error && send_evt)
656 vmbus_chan_signal_tx(chan);
661 vmbus_chan_send_sglist(struct vmbus_channel *chan,
662 struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid)
664 struct vmbus_chanpkt_sglist pkt;
665 int pktlen, pad_pktlen, hlen, error;
670 hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]);
671 pktlen = hlen + dlen;
672 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
673 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
674 ("invalid packet size %d", pad_pktlen));
676 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
677 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
678 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
679 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
680 pkt.cp_hdr.cph_xactid = xactid;
682 pkt.cp_gpa_cnt = sglen;
684 iov[0].iov_base = &pkt;
685 iov[0].iov_len = sizeof(pkt);
686 iov[1].iov_base = sg;
687 iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen;
688 iov[2].iov_base = data;
689 iov[2].iov_len = dlen;
690 iov[3].iov_base = &pad;
691 iov[3].iov_len = pad_pktlen - pktlen;
693 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
694 if (!error && send_evt)
695 vmbus_chan_signal_tx(chan);
700 vmbus_chan_send_prplist(struct vmbus_channel *chan,
701 struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen,
704 struct vmbus_chanpkt_prplist pkt;
705 int pktlen, pad_pktlen, hlen, error;
710 hlen = __offsetof(struct vmbus_chanpkt_prplist,
711 cp_range[0].gpa_page[prp_cnt]);
712 pktlen = hlen + dlen;
713 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen);
714 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
715 ("invalid packet size %d", pad_pktlen));
717 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
718 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
719 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen);
720 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen);
721 pkt.cp_hdr.cph_xactid = xactid;
723 pkt.cp_range_cnt = 1;
725 iov[0].iov_base = &pkt;
726 iov[0].iov_len = sizeof(pkt);
727 iov[1].iov_base = prp;
728 iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]);
729 iov[2].iov_base = data;
730 iov[2].iov_len = dlen;
731 iov[3].iov_base = &pad;
732 iov[3].iov_len = pad_pktlen - pktlen;
734 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
735 if (!error && send_evt)
736 vmbus_chan_signal_tx(chan);
741 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0,
744 struct vmbus_chanpkt_hdr pkt;
745 int error, dlen, hlen;
747 error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
751 if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
752 device_printf(chan->ch_dev, "invalid hlen %u\n",
754 /* XXX this channel is dead actually. */
757 if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) {
758 device_printf(chan->ch_dev, "invalid hlen %u and tlen %u\n",
759 pkt.cph_hlen, pkt.cph_tlen);
760 /* XXX this channel is dead actually. */
764 hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen);
765 dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
768 /* Return the size of this packet's data. */
773 *xactid = pkt.cph_xactid;
776 /* Skip packet header */
777 error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen);
778 KASSERT(!error, ("vmbus_rxbr_read failed"));
784 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
785 struct vmbus_chanpkt_hdr *pkt0, int *pktlen0)
787 struct vmbus_chanpkt_hdr pkt;
790 error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
794 if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
795 device_printf(chan->ch_dev, "invalid hlen %u\n",
797 /* XXX this channel is dead actually. */
800 if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) {
801 device_printf(chan->ch_dev, "invalid hlen %u and tlen %u\n",
802 pkt.cph_hlen, pkt.cph_tlen);
803 /* XXX this channel is dead actually. */
807 pktlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen);
808 if (*pktlen0 < pktlen) {
809 /* Return the size of this packet. */
815 /* Include packet header */
816 error = vmbus_rxbr_read(&chan->ch_rxbr, pkt0, pktlen, 0);
817 KASSERT(!error, ("vmbus_rxbr_read failed"));
823 vmbus_chan_task(void *xchan, int pending __unused)
825 struct vmbus_channel *chan = xchan;
826 vmbus_chan_callback_t cb = chan->ch_cb;
827 void *cbarg = chan->ch_cbarg;
830 * Optimize host to guest signaling by ensuring:
831 * 1. While reading the channel, we disable interrupts from
833 * 2. Ensure that we process all posted messages from the host
834 * before returning from this callback.
835 * 3. Once we return, enable signaling from the host. Once this
836 * state is set we check to see if additional packets are
837 * available to read. In this case we repeat the process.
839 * NOTE: Interrupt has been disabled in the ISR.
846 left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
848 /* No more data in RX bufring; done */
851 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
856 vmbus_chan_task_nobatch(void *xchan, int pending __unused)
858 struct vmbus_channel *chan = xchan;
860 chan->ch_cb(chan, chan->ch_cbarg);
864 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
869 for (f = 0; f < flag_cnt; ++f) {
874 if (event_flags[f] == 0)
877 flags = atomic_swap_long(&event_flags[f], 0);
878 chid_base = f << VMBUS_EVTFLAG_SHIFT;
880 while ((chid_ofs = ffsl(flags)) != 0) {
881 struct vmbus_channel *chan;
883 --chid_ofs; /* NOTE: ffsl is 1-based */
884 flags &= ~(1UL << chid_ofs);
886 chan = sc->vmbus_chmap[chid_base + chid_ofs];
888 /* if channel is closed or closing */
889 if (chan == NULL || chan->ch_tq == NULL)
892 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
893 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
894 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
900 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
902 struct vmbus_evtflags *eventf;
905 * On Host with Win8 or above, the event page can be checked directly
906 * to get the id of the channel that has the pending interrupt.
908 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
909 vmbus_event_flags_proc(sc, eventf->evt_flags,
910 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
914 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
916 struct vmbus_evtflags *eventf;
918 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
919 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
920 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
921 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
926 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
927 const struct vmbus_channel *chan)
929 volatile int *flag_cnt_ptr;
932 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
933 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
938 old_flag_cnt = *flag_cnt_ptr;
939 if (old_flag_cnt >= flag_cnt)
941 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
943 device_printf(sc->vmbus_dev,
944 "channel%u update cpu%d flag_cnt to %d\n",
945 chan->ch_id, chan->ch_cpuid, flag_cnt);
952 static struct vmbus_channel *
953 vmbus_chan_alloc(struct vmbus_softc *sc)
955 struct vmbus_channel *chan;
957 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
959 chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev),
960 HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param),
961 &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
962 if (chan->ch_monprm == NULL) {
963 device_printf(sc->vmbus_dev, "monprm alloc failed\n");
964 free(chan, M_DEVBUF);
969 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
970 TAILQ_INIT(&chan->ch_subchans);
971 TASK_INIT(&chan->ch_detach_task, 0, vmbus_chan_detach_task, chan);
972 vmbus_rxbr_init(&chan->ch_rxbr);
973 vmbus_txbr_init(&chan->ch_txbr);
979 vmbus_chan_free(struct vmbus_channel *chan)
981 /* TODO: assert sub-channel list is empty */
982 /* TODO: asset no longer on the primary channel's sub-channel list */
983 /* TODO: asset no longer on the vmbus channel list */
984 hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm);
985 mtx_destroy(&chan->ch_subchan_lock);
986 vmbus_rxbr_deinit(&chan->ch_rxbr);
987 vmbus_txbr_deinit(&chan->ch_txbr);
988 free(chan, M_DEVBUF);
992 vmbus_chan_add(struct vmbus_channel *newchan)
994 struct vmbus_softc *sc = newchan->ch_vmbus;
995 struct vmbus_channel *prichan;
997 if (newchan->ch_id == 0) {
1000 * Chan0 will neither be processed nor should be offered;
1003 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
1005 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
1006 device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
1010 sc->vmbus_chmap[newchan->ch_id] = newchan;
1013 device_printf(sc->vmbus_dev, "chan%u subidx%u offer\n",
1014 newchan->ch_id, newchan->ch_subidx);
1017 mtx_lock(&sc->vmbus_prichan_lock);
1018 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
1020 * Sub-channel will have the same type GUID and instance
1021 * GUID as its primary channel.
1023 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
1024 sizeof(struct hyperv_guid)) == 0 &&
1025 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
1026 sizeof(struct hyperv_guid)) == 0)
1029 if (VMBUS_CHAN_ISPRIMARY(newchan)) {
1030 if (prichan == NULL) {
1031 /* Install the new primary channel */
1032 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, newchan,
1034 mtx_unlock(&sc->vmbus_prichan_lock);
1037 mtx_unlock(&sc->vmbus_prichan_lock);
1038 device_printf(sc->vmbus_dev, "duplicated primary "
1039 "chan%u\n", newchan->ch_id);
1042 } else { /* Sub-channel */
1043 if (prichan == NULL) {
1044 mtx_unlock(&sc->vmbus_prichan_lock);
1045 device_printf(sc->vmbus_dev, "no primary chan for "
1046 "chan%u\n", newchan->ch_id);
1050 * Found the primary channel for this sub-channel and
1053 * XXX refcnt prichan
1056 mtx_unlock(&sc->vmbus_prichan_lock);
1059 * This is a sub-channel; link it with the primary channel.
1061 KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan),
1062 ("new channel is not sub-channel"));
1063 KASSERT(prichan != NULL, ("no primary channel"));
1065 newchan->ch_prichan = prichan;
1066 newchan->ch_dev = prichan->ch_dev;
1068 mtx_lock(&prichan->ch_subchan_lock);
1069 TAILQ_INSERT_TAIL(&prichan->ch_subchans, newchan, ch_sublink);
1071 * Bump up sub-channel count and notify anyone that is
1072 * interested in this sub-channel, after this sub-channel
1075 prichan->ch_subchan_cnt++;
1076 mtx_unlock(&prichan->ch_subchan_lock);
1083 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1085 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
1087 if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1088 chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1089 /* Only cpu0 is supported */
1093 chan->ch_cpuid = cpu;
1094 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1097 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
1098 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1103 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1105 static uint32_t vmbus_chan_nextcpu;
1108 cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
1109 vmbus_chan_cpu_set(chan, cpu);
1113 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1116 * By default, pin the channel to cpu0. Devices having
1117 * special channel-cpu mapping requirement should call
1118 * vmbus_chan_cpu_{set,rr}().
1120 vmbus_chan_cpu_set(chan, 0);
1124 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc,
1125 const struct vmbus_message *msg)
1127 const struct vmbus_chanmsg_choffer *offer;
1128 struct vmbus_channel *chan;
1131 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1133 chan = vmbus_chan_alloc(sc);
1135 device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1140 chan->ch_id = offer->chm_chanid;
1141 chan->ch_subidx = offer->chm_subidx;
1142 chan->ch_guid_type = offer->chm_chtype;
1143 chan->ch_guid_inst = offer->chm_chinst;
1145 /* Batch reading is on by default */
1146 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1148 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1149 if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1150 chan->ch_monprm->mp_connid = offer->chm_connid;
1152 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1158 chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1160 trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1161 if (trig_idx >= VMBUS_MONTRIGS_MAX)
1162 panic("invalid monitor trigger %u", offer->chm_montrig);
1164 &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1166 chan->ch_montrig_mask =
1167 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1174 &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1175 chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1177 /* Select default cpu for this channel. */
1178 vmbus_chan_cpu_default(chan);
1180 error = vmbus_chan_add(chan);
1182 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1183 chan->ch_id, error);
1184 vmbus_chan_free(chan);
1188 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1190 * Add device for this primary channel.
1193 * Error is ignored here; don't have much to do if error
1196 vmbus_add_child(chan);
1201 * XXX pretty broken; need rework.
1204 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc,
1205 const struct vmbus_message *msg)
1207 const struct vmbus_chanmsg_chrescind *note;
1208 struct vmbus_channel *chan;
1210 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1211 if (note->chm_chanid > VMBUS_CHAN_MAX) {
1212 device_printf(sc->vmbus_dev, "invalid rescinded chan%u\n",
1218 device_printf(sc->vmbus_dev, "chan%u rescinded\n",
1222 chan = sc->vmbus_chmap[note->chm_chanid];
1225 sc->vmbus_chmap[note->chm_chanid] = NULL;
1227 taskqueue_enqueue(taskqueue_thread, &chan->ch_detach_task);
1231 vmbus_chan_detach_task(void *xchan, int pending __unused)
1233 struct vmbus_channel *chan = xchan;
1235 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1236 /* Only primary channel owns the device */
1237 vmbus_delete_child(chan);
1238 /* NOTE: DO NOT free primary channel for now */
1240 struct vmbus_softc *sc = chan->ch_vmbus;
1241 struct vmbus_channel *pri_chan = chan->ch_prichan;
1242 struct vmbus_chanmsg_chfree *req;
1243 struct vmbus_msghc *mh;
1246 mh = vmbus_msghc_get(sc, sizeof(*req));
1248 device_printf(sc->vmbus_dev,
1249 "can not get msg hypercall for chfree(chan%u)\n",
1254 req = vmbus_msghc_dataptr(mh);
1255 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
1256 req->chm_chanid = chan->ch_id;
1258 error = vmbus_msghc_exec_noresult(mh);
1259 vmbus_msghc_put(sc, mh);
1262 device_printf(sc->vmbus_dev,
1263 "chfree(chan%u) failed: %d",
1264 chan->ch_id, error);
1265 /* NOTE: Move on! */
1268 device_printf(sc->vmbus_dev, "chan%u freed\n",
1273 mtx_lock(&pri_chan->ch_subchan_lock);
1274 TAILQ_REMOVE(&pri_chan->ch_subchans, chan, ch_sublink);
1275 KASSERT(pri_chan->ch_subchan_cnt > 0,
1276 ("invalid subchan_cnt %d", pri_chan->ch_subchan_cnt));
1277 pri_chan->ch_subchan_cnt--;
1278 mtx_unlock(&pri_chan->ch_subchan_lock);
1281 vmbus_chan_free(chan);
1286 * Detach all devices and destroy the corresponding primary channels.
1289 vmbus_chan_destroy_all(struct vmbus_softc *sc)
1291 struct vmbus_channel *chan;
1293 mtx_lock(&sc->vmbus_prichan_lock);
1294 while ((chan = TAILQ_FIRST(&sc->vmbus_prichans)) != NULL) {
1295 KASSERT(VMBUS_CHAN_ISPRIMARY(chan), ("not primary channel"));
1296 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink);
1297 mtx_unlock(&sc->vmbus_prichan_lock);
1299 vmbus_delete_child(chan);
1300 vmbus_chan_free(chan);
1302 mtx_lock(&sc->vmbus_prichan_lock);
1304 bzero(sc->vmbus_chmap,
1305 sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX);
1306 mtx_unlock(&sc->vmbus_prichan_lock);
1310 * The channel whose vcpu binding is closest to the currect vcpu will
1312 * If no multi-channel, always select primary channel.
1314 struct vmbus_channel *
1315 vmbus_chan_cpu2chan(struct vmbus_channel *prichan, int cpu)
1317 struct vmbus_channel *sel, *chan;
1318 uint32_t vcpu, sel_dist;
1320 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpuid %d", cpu));
1321 if (TAILQ_EMPTY(&prichan->ch_subchans))
1324 vcpu = VMBUS_PCPU_GET(prichan->ch_vmbus, vcpuid, cpu);
1326 #define CHAN_VCPU_DIST(ch, vcpu) \
1327 (((ch)->ch_vcpuid > (vcpu)) ? \
1328 ((ch)->ch_vcpuid - (vcpu)) : ((vcpu) - (ch)->ch_vcpuid))
1330 #define CHAN_SELECT(ch) \
1333 sel_dist = CHAN_VCPU_DIST(ch, vcpu); \
1336 CHAN_SELECT(prichan);
1338 mtx_lock(&prichan->ch_subchan_lock);
1339 TAILQ_FOREACH(chan, &prichan->ch_subchans, ch_sublink) {
1342 KASSERT(chan->ch_stflags & VMBUS_CHAN_ST_OPENED,
1343 ("chan%u is not opened", chan->ch_id));
1345 if (chan->ch_vcpuid == vcpu) {
1346 /* Exact match; done */
1351 dist = CHAN_VCPU_DIST(chan, vcpu);
1352 if (sel_dist <= dist) {
1353 /* Far or same distance; skip */
1357 /* Select the closer channel. */
1360 mtx_unlock(&prichan->ch_subchan_lock);
1363 #undef CHAN_VCPU_DIST
1368 struct vmbus_channel **
1369 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt)
1371 struct vmbus_channel **ret, *chan;
1374 KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt));
1376 ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP,
1379 mtx_lock(&pri_chan->ch_subchan_lock);
1381 while (pri_chan->ch_subchan_cnt < subchan_cnt)
1382 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
1385 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
1386 /* TODO: refcnt chan */
1390 if (i == subchan_cnt)
1393 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
1394 pri_chan->ch_subchan_cnt, subchan_cnt));
1396 mtx_unlock(&pri_chan->ch_subchan_lock);
1402 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused)
1405 free(subchan, M_TEMP);
1409 vmbus_subchan_drain(struct vmbus_channel *pri_chan)
1411 mtx_lock(&pri_chan->ch_subchan_lock);
1412 while (pri_chan->ch_subchan_cnt > 0)
1413 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
1414 mtx_unlock(&pri_chan->ch_subchan_lock);
1418 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
1420 vmbus_chanmsg_proc_t msg_proc;
1423 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
1424 KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX,
1425 ("invalid message type %u", msg_type));
1427 msg_proc = vmbus_chan_msgprocs[msg_type];
1428 if (msg_proc != NULL)
1433 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
1436 chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
1438 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1442 vmbus_chan_id(const struct vmbus_channel *chan)
1448 vmbus_chan_subidx(const struct vmbus_channel *chan)
1450 return chan->ch_subidx;
1454 vmbus_chan_is_primary(const struct vmbus_channel *chan)
1456 if (VMBUS_CHAN_ISPRIMARY(chan))
1462 const struct hyperv_guid *
1463 vmbus_chan_guid_inst(const struct vmbus_channel *chan)
1465 return &chan->ch_guid_inst;
1469 vmbus_chan_prplist_nelem(int br_size, int prpcnt_max, int dlen_max)
1473 elem_size = __offsetof(struct vmbus_chanpkt_prplist,
1474 cp_range[0].gpa_page[prpcnt_max]);
1475 elem_size += dlen_max;
1476 elem_size = VMBUS_CHANPKT_TOTLEN(elem_size);
1478 return (vmbus_br_nelem(br_size, elem_size));