Lines Matching +full:chan +full:- +full:name

1 /*-
2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
134 vmbus_chan_signal(const struct vmbus_channel *chan)
136 atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask);
137 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
138 atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask);
141 (vm_offset_t)chan->ch_monprm));
145 vmbus_chan_signal_tx(struct vmbus_channel *chan)
147 chan->ch_txbr.txbr_intrcnt ++;
149 vmbus_chan_signal(chan);
153 vmbus_chan_signal_rx(struct vmbus_channel *chan)
155 chan->ch_rxbr.rxbr_intrcnt ++;
157 vmbus_chan_signal(chan);
161 vmbus_chan_ins_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
164 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
165 if (atomic_testandset_int(&chan->ch_stflags,
168 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, chan, ch_prilink);
172 vmbus_chan_rem_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan)
175 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED);
176 if (atomic_testandclear_int(&chan->ch_stflags,
179 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink);
184 struct vmbus_channel *chan)
187 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
189 if (atomic_testandset_int(&chan->ch_stflags,
192 TAILQ_INSERT_TAIL(&prichan->ch_subchans, chan, ch_sublink);
194 /* Bump sub-channel count. */
195 prichan->ch_subchan_cnt++;
200 struct vmbus_channel *chan)
203 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED);
205 KASSERT(prichan->ch_subchan_cnt > 0,
206 ("invalid subchan_cnt %d", prichan->ch_subchan_cnt));
207 prichan->ch_subchan_cnt--;
209 if (atomic_testandclear_int(&chan->ch_stflags,
212 TAILQ_REMOVE(&prichan->ch_subchans, chan, ch_sublink);
216 vmbus_chan_ins_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
219 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
220 if (atomic_testandset_int(&chan->ch_stflags,
223 TAILQ_INSERT_TAIL(&sc->vmbus_chans, chan, ch_link);
227 vmbus_chan_rem_list(struct vmbus_softc *sc, struct vmbus_channel *chan)
230 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED);
231 if (atomic_testandclear_int(&chan->ch_stflags,
234 TAILQ_REMOVE(&sc->vmbus_chans, chan, ch_link);
240 struct vmbus_channel *chan = arg1;
243 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF)
249 vmbus_chan_sysctl_create(struct vmbus_channel *chan)
254 char name[16];
262 ctx = &chan->ch_sysctl_ctx;
266 * Create dev.NAME.UNIT.channel tree.
269 SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)),
275 * Create dev.NAME.UNIT.channel.CHANID tree.
277 if (VMBUS_CHAN_ISPRIMARY(chan))
278 ch_id = chan->ch_id;
280 ch_id = chan->ch_prichan->ch_id;
281 snprintf(name, sizeof(name), "%d", ch_id);
283 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
287 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
289 * Create dev.NAME.UNIT.channel.CHANID.sub tree.
297 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree.
302 snprintf(name, sizeof(name), "%d", chan->ch_subidx);
304 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
309 "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id");
313 "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id");
316 chan, 0, vmbus_chan_sysctl_mnf, "I",
325 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr.rxbr, "rx");
329 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr.txbr, "tx");
334 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size,
343 KASSERT(chan->ch_bufring == NULL, ("bufrings are allocated"));
344 chan->ch_bufring = contigmalloc(txbr_size + rxbr_size, M_DEVBUF,
346 if (chan->ch_bufring == NULL) {
347 vmbus_chan_printf(chan, "bufring allocation failed\n");
351 cbr.cbr = chan->ch_bufring;
352 cbr.cbr_paddr = pmap_kextract((vm_offset_t)chan->ch_bufring);
356 error = vmbus_chan_open_br(chan, &cbr, udata, udlen, cb, cbarg);
365 vmbus_chan_printf(chan, "chan%u bufring GPADL "
367 "leak %d bytes memory\n", chan->ch_id,
370 free(chan->ch_bufring, M_DEVBUF);
372 chan->ch_bufring = NULL;
378 vmbus_chan_open_br(struct vmbus_channel *chan, const struct vmbus_chan_br *cbr,
381 struct vmbus_softc *sc = chan->ch_vmbus;
391 vmbus_chan_printf(chan,
392 "invalid udata len %d for chan%u\n", udlen, chan->ch_id);
396 br = cbr->cbr;
397 txbr_size = cbr->cbr_txsz;
398 rxbr_size = cbr->cbr_rxsz;
403 KASSERT((cbr->cbr_paddr & PAGE_MASK) == 0,
411 if (atomic_testandset_int(&chan->ch_stflags,
413 panic("double-open chan%u", chan->ch_id);
415 chan->ch_cb = cb;
416 chan->ch_cbarg = cbarg;
418 vmbus_chan_update_evtflagcnt(sc, chan);
420 chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid);
421 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
425 TASK_INIT(&chan->ch_task, 0, task_fn, chan);
428 vmbus_txbr_setup(&chan->ch_txbr, br, txbr_size);
430 vmbus_rxbr_setup(&chan->ch_rxbr, br + txbr_size, rxbr_size);
433 vmbus_chan_sysctl_create(chan);
438 error = vmbus_chan_gpadl_connect(chan, cbr->cbr_paddr,
439 txbr_size + rxbr_size, &chan->ch_bufring_gpadl);
441 vmbus_chan_printf(chan,
442 "failed to connect bufring GPADL to chan%u\n", chan->ch_id);
450 vmbus_chan_set_chmap(chan);
457 vmbus_chan_printf(chan,
458 "can not get msg hypercall for chopen(chan%u)\n",
459 chan->ch_id);
465 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
466 req->chm_chanid = chan->ch_id;
467 req->chm_openid = chan->ch_id;
468 req->chm_gpadl = chan->ch_bufring_gpadl;
469 req->chm_vcpuid = chan->ch_vcpuid;
470 req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT;
472 memcpy(req->chm_udata, udata, udlen);
476 vmbus_chan_printf(chan,
477 "chopen(chan%u) msg hypercall exec failed: %d\n",
478 chan->ch_id, error);
487 if (vmbus_chan_is_revoked(chan)) {
495 vmbus_chan_printf(chan,
496 "chan%u is revoked, when it is being opened\n",
497 chan->ch_id);
522 msg->msg_data)->chm_status;
524 /* XXX any non-0 value is ok here. */
532 vmbus_chan_printf(chan, "chan%u opened\n", chan->ch_id);
536 vmbus_chan_printf(chan, "failed to open chan%u\n", chan->ch_id);
540 sysctl_ctx_free(&chan->ch_sysctl_ctx);
541 vmbus_chan_clear_chmap(chan);
542 if (chan->ch_bufring_gpadl != 0) {
545 error1 = vmbus_chan_gpadl_disconnect(chan,
546 chan->ch_bufring_gpadl);
554 chan->ch_bufring_gpadl = 0;
556 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED);
561 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr,
564 struct vmbus_softc *sc = chan->ch_vmbus;
592 vmbus_chan_printf(chan, "GPA too large, %d pages\n",
614 page_count -= cnt;
620 vmbus_chan_printf(chan,
621 "can not get msg hypercall for gpadl_conn(chan%u)\n",
622 chan->ch_id);
627 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
628 req->chm_chanid = chan->ch_id;
629 req->chm_gpadl = gpadl;
630 req->chm_range_len = range_len;
631 req->chm_range_cnt = 1;
632 req->chm_range.gpa_len = size;
633 req->chm_range.gpa_ofs = 0;
635 req->chm_range.gpa_page[i] = page_id++;
639 vmbus_chan_printf(chan,
640 "gpadl_conn(chan%u) msg hypercall exec failed: %d\n",
641 chan->ch_id, error);
653 page_count -= cnt;
660 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
661 subreq->chm_gpadl = gpadl;
663 subreq->chm_gpa_page[i] = page_id++;
671 msg->msg_data)->chm_status;
676 vmbus_chan_printf(chan, "gpadl_conn(chan%u) failed: %u\n",
677 chan->ch_id, status);
684 vmbus_chan_printf(chan, "gpadl_conn(chan%u) succeeded\n",
685 chan->ch_id);
691 vmbus_chan_wait_revoke(const struct vmbus_channel *chan, bool can_sleep)
698 if (vmbus_chan_is_revoked(chan))
714 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl)
716 struct vmbus_softc *sc = chan->ch_vmbus;
725 vmbus_chan_printf(chan,
726 "can not get msg hypercall for gpadl_disconn(chan%u)\n",
727 chan->ch_id);
732 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
733 req->chm_chanid = chan->ch_id;
734 req->chm_gpadl = gpadl;
740 if (vmbus_chan_wait_revoke(chan, true)) {
745 vmbus_chan_printf(chan,
746 "gpadl_disconn(revoked chan%u) msg hypercall "
747 "exec failed: %d\n", chan->ch_id, error);
750 vmbus_chan_printf(chan,
751 "gpadl_disconn(chan%u) msg hypercall exec failed: %d\n",
752 chan->ch_id, error);
764 vmbus_chan_detach(struct vmbus_channel *chan)
768 KASSERT(chan->ch_refs > 0, ("chan%u: invalid refcnt %d",
769 chan->ch_id, chan->ch_refs));
770 refs = atomic_fetchadd_int(&chan->ch_refs, -1);
772 if (VMBUS_CHAN_ISPRIMARY(chan)) {
773 KASSERT(refs == 1, ("chan%u: invalid refcnt %d for prichan",
774 chan->ch_id, refs + 1));
782 vmbus_chan_printf(chan, "chan%u detached\n",
783 chan->ch_id);
785 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
792 struct vmbus_channel *chan = xchan;
794 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
798 vmbus_chan_clear_chmap(struct vmbus_channel *chan)
802 TASK_INIT(&chmap_task, 0, vmbus_chan_clrchmap_task, chan);
803 vmbus_chan_run_task(chan, &chmap_task);
807 vmbus_chan_set_chmap(struct vmbus_channel *chan)
810 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
821 vmbus_chan_poll_cancel(struct vmbus_channel *chan)
825 TASK_INIT(&poll_cancel, 0, vmbus_chan_poll_cancel_task, chan);
826 vmbus_chan_run_task(chan, &poll_cancel);
830 vmbus_chan_close_internal(struct vmbus_channel *chan)
832 struct vmbus_softc *sc = chan->ch_vmbus;
840 * Sub-channels are closed upon their primary channel closing,
844 old_stflags = chan->ch_stflags;
845 if (atomic_cmpset_int(&chan->ch_stflags, old_stflags,
852 vmbus_chan_printf(chan, "chan%u not opened\n",
853 chan->ch_id);
862 sysctl_ctx_free(&chan->ch_sysctl_ctx);
867 vmbus_chan_poll_cancel(chan);
875 vmbus_chan_clear_chmap(chan);
876 taskqueue_drain(chan->ch_tq, &chan->ch_task);
877 chan->ch_tq = NULL;
884 vmbus_chan_printf(chan,
885 "can not get msg hypercall for chclose(chan%u)\n",
886 chan->ch_id);
892 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
893 req->chm_chanid = chan->ch_id;
899 vmbus_chan_printf(chan,
900 "chclose(chan%u) msg hypercall exec failed: %d\n",
901 chan->ch_id, error);
906 vmbus_chan_printf(chan, "chan%u closed\n", chan->ch_id);
912 if (chan->ch_bufring_gpadl != 0) {
915 error1 = vmbus_chan_gpadl_disconnect(chan,
916 chan->ch_bufring_gpadl);
924 vmbus_chan_printf(chan, "chan%u bufring GPADL "
925 "is still connected after close\n", chan->ch_id);
926 chan->ch_bufring = NULL;
933 chan->ch_bufring_gpadl = 0;
939 if (chan->ch_bufring != NULL) {
940 free(chan->ch_bufring, M_DEVBUF);
941 chan->ch_bufring = NULL;
947 vmbus_chan_close_direct(struct vmbus_channel *chan)
952 if (VMBUS_CHAN_ISPRIMARY(chan)) {
956 * All sub-channels _must_ have been closed, or are _not_
959 mtx_lock(&chan->ch_subchan_lock);
960 TAILQ_FOREACH(subchan, &chan->ch_subchans, ch_sublink) {
962 (subchan->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0,
963 ("chan%u: subchan%u is still opened",
964 chan->ch_id, subchan->ch_subidx));
966 mtx_unlock(&chan->ch_subchan_lock);
970 error = vmbus_chan_close_internal(chan);
971 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
973 * This sub-channel is referenced, when it is linked to
976 vmbus_chan_detach(chan);
982 * Caller should make sure that all sub-channels have
983 * been added to 'chan' and all to-be-closed channels
987 vmbus_chan_close(struct vmbus_channel *chan)
991 if (!VMBUS_CHAN_ISPRIMARY(chan)) {
993 * Sub-channel is closed when its primary channel
1000 * Close all sub-channels, if any.
1002 subchan_cnt = chan->ch_subchan_cnt;
1007 subchan = vmbus_subchan_get(chan, subchan_cnt);
1011 * This sub-channel is referenced, when it is
1021 vmbus_chan_close_internal(chan);
1025 vmbus_chan_intr_drain(struct vmbus_channel *chan)
1028 taskqueue_drain(chan->ch_tq, &chan->ch_task);
1032 vmbus_chan_write_available(struct vmbus_channel *chan)
1034 return (vmbus_txbr_available(&chan->ch_txbr));
1038 vmbus_chan_write_signal(struct vmbus_channel *chan,
1042 vmbus_chan_write_available(chan) > min_signal_size) {
1046 if (!vmbus_txbr_get_imask(&chan->ch_txbr)) {
1048 vmbus_chan_signal_tx(chan);
1056 vmbus_chan_set_pending_send_size(struct vmbus_channel *chan,
1059 if (chan)
1060 vmbus_txbr_set_pending_snd_sz(&chan->ch_txbr, size);
1064 vmbus_chan_iov_send(struct vmbus_channel *chan,
1074 error = vmbus_txbr_write_call(&chan->ch_txbr, iov, iovlen,
1078 vmbus_chan_signal_tx(chan);
1085 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags,
1097 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1111 iov[2].iov_len = pad_pktlen - pktlen;
1113 error = vmbus_txbr_write(&chan->ch_txbr, iov, 3, &send_evt);
1115 vmbus_chan_signal_tx(chan);
1120 vmbus_chan_send_sglist(struct vmbus_channel *chan,
1132 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1150 iov[3].iov_len = pad_pktlen - pktlen;
1152 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1154 vmbus_chan_signal_tx(chan);
1159 vmbus_chan_send_prplist(struct vmbus_channel *chan,
1173 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr),
1191 iov[3].iov_len = pad_pktlen - pktlen;
1193 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt);
1195 vmbus_chan_signal_tx(chan);
1200 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0,
1207 error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt));
1212 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt.cph_hlen);
1217 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1224 dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen;
1236 error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen, &sig_event);
1240 vmbus_chan_signal_rx(chan);
1246 vmbus_chan_recv_pkt(struct vmbus_channel *chan,
1253 error = vmbus_rxbr_peek(&chan->ch_rxbr, pkt, pkt_hlen);
1257 if (__predict_false(pkt->cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) {
1258 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt->cph_hlen);
1262 if (__predict_false(pkt->cph_hlen > pkt->cph_tlen)) {
1263 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n",
1264 pkt->cph_hlen, pkt->cph_tlen);
1269 pktlen = VMBUS_CHANPKT_GETLEN(pkt->cph_tlen);
1278 * Skip the fixed-size packet header, which has been filled
1281 error = vmbus_rxbr_read(&chan->ch_rxbr, pkt + 1,
1282 pktlen - pkt_hlen, pkt_hlen, &sig_event);
1286 vmbus_chan_signal_rx(chan);
1292 vmbus_chan_read_available(struct vmbus_channel *chan)
1294 return (vmbus_rxbr_available(&chan->ch_rxbr));
1299 * - Advance the channel read index for 'advance' bytes
1300 * - Copy data_len bytes in to the buffer pointed by 'data'
1306 vmbus_chan_recv_peek(struct vmbus_channel *chan,
1315 error = vmbus_rxbr_idxadv_peek(&chan->ch_rxbr,
1319 vmbus_chan_signal_rx(chan);
1327 * - Advance the channel read index for 'advance' bytes
1330 vmbus_chan_recv_idxadv(struct vmbus_channel *chan, uint32_t advance)
1338 error = vmbus_rxbr_idxadv(&chan->ch_rxbr, advance, &sig_event);
1341 vmbus_chan_signal_rx(chan);
1353 vmbus_chan_recv_peek_call(struct vmbus_channel *chan, int data_len,
1356 if (!chan || data_len <= 0 || cb == NULL)
1359 return (vmbus_rxbr_peek_call(&chan->ch_rxbr, data_len, skip,
1366 struct vmbus_channel *chan = xchan;
1367 vmbus_chan_callback_t cb = chan->ch_cb;
1368 void *cbarg = chan->ch_cbarg;
1370 KASSERT(chan->ch_poll_intvl == 0,
1371 ("chan%u: interrupted in polling mode", chan->ch_id));
1388 cb(chan, cbarg);
1390 left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1395 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1402 struct vmbus_channel *chan = xchan;
1404 KASSERT(chan->ch_poll_intvl == 0,
1405 ("chan%u: interrupted in polling mode", chan->ch_id));
1406 chan->ch_cb(chan, chan->ch_cbarg);
1412 struct vmbus_channel *chan = xchan;
1414 KASSERT(chan->ch_poll_intvl != 0,
1415 ("chan%u: polling timeout in interrupt mode", chan->ch_id));
1416 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1422 struct vmbus_channel *chan = xchan;
1424 KASSERT(chan->ch_poll_intvl != 0,
1425 ("chan%u: polling in interrupt mode", chan->ch_id));
1426 callout_reset_sbt_curcpu(&chan->ch_poll_timeo, chan->ch_poll_intvl, 0,
1427 vmbus_chan_poll_timeout, chan, chan->ch_poll_flags);
1428 chan->ch_cb(chan, chan->ch_cbarg);
1435 struct vmbus_channel *chan = arg->poll_chan;
1442 intvl = SBT_1S / arg->poll_hz;
1445 if (intvl == chan->ch_poll_intvl) {
1449 chan->ch_poll_intvl = intvl;
1453 if (arg->poll_hz <= hz)
1455 chan->ch_poll_flags = poll_flags;
1466 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL;
1468 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1475 taskqueue_cancel(chan->ch_tq, &chan->ch_task, NULL);
1478 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task);
1482 vmbus_chan_poll_cancel_intq(struct vmbus_channel *chan)
1485 if (chan->ch_poll_intvl == 0) {
1494 callout_drain(&chan->ch_poll_timeo);
1504 chan->ch_poll_intvl = 0;
1512 taskqueue_cancel(chan->ch_tq, &chan->ch_poll_task, NULL);
1521 struct vmbus_channel *chan = xchan;
1523 if (!vmbus_chan_poll_cancel_intq(chan)) {
1532 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan;
1534 vmbus_rxbr_intr_unmask(&chan->ch_rxbr);
1540 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1561 struct vmbus_channel *chan;
1563 --chid_ofs; /* NOTE: ffsl is 1-based */
1566 chan = sc->vmbus_chmap[chid_base + chid_ofs];
1567 if (__predict_false(chan == NULL)) {
1573 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD)
1574 vmbus_rxbr_intr_mask(&chan->ch_rxbr);
1575 taskqueue_enqueue(chan->ch_tq, &chan->ch_task);
1590 vmbus_event_flags_proc(sc, eventf->evt_flags,
1600 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
1601 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
1608 const struct vmbus_channel *chan)
1613 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
1614 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid);
1624 vmbus_chan_printf(chan,
1625 "chan%u update cpu%d flag_cnt to %d\n",
1626 chan->ch_id, chan->ch_cpuid, flag_cnt);
1636 struct vmbus_channel *chan;
1638 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO);
1640 chan->ch_monprm = contigmalloc(sizeof(struct hyperv_mon_param),
1642 if (chan->ch_monprm == NULL) {
1643 device_printf(sc->vmbus_dev, "monprm alloc failed\n");
1644 free(chan, M_DEVBUF);
1648 chan->ch_refs = 1;
1649 chan->ch_vmbus = sc;
1650 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF);
1651 sx_init(&chan->ch_orphan_lock, "vmbus chorphan");
1652 TAILQ_INIT(&chan->ch_subchans);
1653 vmbus_rxbr_init(&chan->ch_rxbr);
1654 vmbus_txbr_init(&chan->ch_txbr);
1656 TASK_INIT(&chan->ch_poll_task, 0, vmbus_chan_poll_task, chan);
1657 callout_init(&chan->ch_poll_timeo, 1);
1659 return chan;
1663 vmbus_chan_free(struct vmbus_channel *chan)
1666 KASSERT(TAILQ_EMPTY(&chan->ch_subchans) && chan->ch_subchan_cnt == 0,
1667 ("still owns sub-channels"));
1668 KASSERT((chan->ch_stflags &
1673 KASSERT(chan->ch_orphan_xact == NULL,
1675 KASSERT(chan->ch_refs == 0, ("chan%u: invalid refcnt %d",
1676 chan->ch_id, chan->ch_refs));
1677 KASSERT(chan->ch_poll_intvl == 0, ("chan%u: polling is activated",
1678 chan->ch_id));
1680 free(chan->ch_monprm, M_DEVBUF);
1681 mtx_destroy(&chan->ch_subchan_lock);
1682 sx_destroy(&chan->ch_orphan_lock);
1683 vmbus_rxbr_deinit(&chan->ch_rxbr);
1684 vmbus_txbr_deinit(&chan->ch_txbr);
1685 free(chan, M_DEVBUF);
1691 struct vmbus_softc *sc = newchan->ch_vmbus;
1694 if (newchan->ch_id == 0) {
1700 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n");
1702 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) {
1703 device_printf(sc->vmbus_dev, "invalid chan%u offer\n",
1704 newchan->ch_id);
1708 mtx_lock(&sc->vmbus_prichan_lock);
1709 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) {
1711 * Sub-channel will have the same type GUID and instance
1714 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type,
1716 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst,
1724 mtx_unlock(&sc->vmbus_prichan_lock);
1727 mtx_unlock(&sc->vmbus_prichan_lock);
1728 device_printf(sc->vmbus_dev,
1729 "duplicated primary chan%u\n", newchan->ch_id);
1732 } else { /* Sub-channel */
1734 mtx_unlock(&sc->vmbus_prichan_lock);
1735 device_printf(sc->vmbus_dev,
1736 "no primary chan for chan%u\n", newchan->ch_id);
1740 * Found the primary channel for this sub-channel and
1746 mtx_unlock(&sc->vmbus_prichan_lock);
1749 * This is a sub-channel; link it with the primary channel.
1752 ("new channel is not sub-channel"));
1756 * Reference count this sub-channel; it will be dereferenced
1757 * when this sub-channel is closed.
1759 KASSERT(newchan->ch_refs == 1, ("chan%u: invalid refcnt %d",
1760 newchan->ch_id, newchan->ch_refs));
1761 atomic_add_int(&newchan->ch_refs, 1);
1763 newchan->ch_prichan = prichan;
1764 newchan->ch_dev = prichan->ch_dev;
1766 mtx_lock(&prichan->ch_subchan_lock);
1768 mtx_unlock(&prichan->ch_subchan_lock);
1770 * Notify anyone that is interested in this sub-channel,
1771 * after this sub-channel is setup.
1778 mtx_lock(&sc->vmbus_chan_lock);
1780 mtx_unlock(&sc->vmbus_chan_lock);
1783 vmbus_chan_printf(newchan, "chan%u subidx%u offer\n",
1784 newchan->ch_id, newchan->ch_subidx);
1794 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu)
1798 if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 ||
1799 chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) {
1804 chan->ch_cpuid = cpu;
1805 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu);
1808 vmbus_chan_printf(chan,
1809 "chan%u assigned to cpu%u [vcpu%u]\n",
1810 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid);
1815 vmbus_chan_cpu_rr(struct vmbus_channel *chan)
1821 vmbus_chan_cpu_set(chan, cpu);
1825 vmbus_chan_cpu_default(struct vmbus_channel *chan)
1829 * special channel-cpu mapping requirement should call
1832 vmbus_chan_cpu_set(chan, 0);
1840 struct vmbus_channel *chan;
1844 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data;
1846 chan = vmbus_chan_alloc(sc);
1847 if (chan == NULL) {
1848 device_printf(sc->vmbus_dev, "allocate chan%u failed\n",
1849 offer->chm_chanid);
1853 chan->ch_id = offer->chm_chanid;
1854 chan->ch_subidx = offer->chm_subidx;
1855 chan->ch_guid_type = offer->chm_chtype;
1856 chan->ch_guid_inst = offer->chm_chinst;
1859 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
1861 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
1862 if (sc->vmbus_version != VMBUS_VERSION_WS2008)
1863 chan->ch_monprm->mp_connid = offer->chm_connid;
1865 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
1871 chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF;
1873 trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN;
1875 panic("invalid monitor trigger %u", offer->chm_montrig);
1876 chan->ch_montrig =
1877 &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending;
1879 chan->ch_montrig_mask =
1880 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN);
1883 if (offer->chm_chflags & VMBUS_CHAN_TLNPI_PROVIDER_OFFER) {
1885 chan->ch_is_hvs = true;
1887 chan->ch_hvs_conn_from_host =
1888 offer->chm_udata.pipe.user_def[0];
1891 device_printf(sc->vmbus_dev,
1892 "chan%u is hyperv socket channel "
1894 chan->ch_id,
1895 (chan->ch_hvs_conn_from_host != 0) ?
1899 chan->ch_is_hvs = false;
1905 chan->ch_evtflag =
1906 &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT];
1907 chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK);
1912 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1913 chan->ch_mgmt_tq = sc->vmbus_devtq;
1917 chan->ch_mgmt_tq = sc->vmbus_subchtq;
1921 TASK_INIT(&chan->ch_attach_task, 0, attach_fn, chan);
1922 TASK_INIT(&chan->ch_detach_task, 0, detach_fn, chan);
1924 error = vmbus_chan_add(chan);
1926 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n",
1927 chan->ch_id, error);
1928 atomic_subtract_int(&chan->ch_refs, 1);
1929 vmbus_chan_free(chan);
1932 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_attach_task);
1940 struct vmbus_channel *chan;
1942 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data;
1943 if (note->chm_chanid > VMBUS_CHAN_MAX) {
1944 device_printf(sc->vmbus_dev, "invalid revoked chan%u\n",
1945 note->chm_chanid);
1952 mtx_lock(&sc->vmbus_chan_lock);
1953 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
1954 if (chan->ch_id == note->chm_chanid)
1957 if (chan == NULL) {
1958 mtx_unlock(&sc->vmbus_chan_lock);
1959 device_printf(sc->vmbus_dev, "chan%u is not offered\n",
1960 note->chm_chanid);
1963 vmbus_chan_rem_list(sc, chan);
1964 mtx_unlock(&sc->vmbus_chan_lock);
1966 if (VMBUS_CHAN_ISPRIMARY(chan)) {
1971 * other sub-channel offers, which are processed in
1974 mtx_lock(&sc->vmbus_prichan_lock);
1975 vmbus_chan_rem_prilist(sc, chan);
1976 mtx_unlock(&sc->vmbus_prichan_lock);
1985 if (atomic_testandset_int(&chan->ch_stflags,
1989 sx_xlock(&chan->ch_orphan_lock);
1990 if (chan->ch_orphan_xact != NULL)
1991 vmbus_xact_ctx_orphan(chan->ch_orphan_xact);
1992 sx_xunlock(&chan->ch_orphan_lock);
1995 vmbus_chan_printf(chan, "chan%u revoked\n", note->chm_chanid);
1996 vmbus_chan_detach(chan);
2000 vmbus_chan_release(struct vmbus_channel *chan)
2002 struct vmbus_softc *sc = chan->ch_vmbus;
2009 vmbus_chan_printf(chan,
2010 "can not get msg hypercall for chfree(chan%u)\n",
2011 chan->ch_id);
2016 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
2017 req->chm_chanid = chan->ch_id;
2023 vmbus_chan_printf(chan,
2024 "chfree(chan%u) msg hypercall exec failed: %d\n",
2025 chan->ch_id, error);
2028 vmbus_chan_printf(chan, "chan%u freed\n", chan->ch_id);
2036 struct vmbus_channel *chan = xchan;
2038 KASSERT(VMBUS_CHAN_ISPRIMARY(chan),
2039 ("chan%u is not primary channel", chan->ch_id));
2042 vmbus_delete_child(chan);
2045 vmbus_chan_release(chan);
2048 vmbus_chan_free(chan);
2054 struct vmbus_channel *chan = xchan;
2055 struct vmbus_channel *pri_chan = chan->ch_prichan;
2057 KASSERT(!VMBUS_CHAN_ISPRIMARY(chan),
2058 ("chan%u is primary channel", chan->ch_id));
2061 vmbus_chan_release(chan);
2063 /* Unlink from its primary channel's sub-channel list. */
2064 mtx_lock(&pri_chan->ch_subchan_lock);
2065 vmbus_chan_rem_sublist(pri_chan, chan);
2066 mtx_unlock(&pri_chan->ch_subchan_lock);
2067 /* Notify anyone that is waiting for this sub-channel to vanish. */
2071 vmbus_chan_free(chan);
2100 struct vmbus_channel *chan;
2102 mtx_lock(&sc->vmbus_chan_lock);
2103 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) {
2104 if (VMBUS_CHAN_ISPRIMARY(chan))
2107 if (chan == NULL) {
2109 mtx_unlock(&sc->vmbus_chan_lock);
2112 vmbus_chan_rem_list(sc, chan);
2113 mtx_unlock(&sc->vmbus_chan_lock);
2115 mtx_lock(&sc->vmbus_prichan_lock);
2116 vmbus_chan_rem_prilist(sc, chan);
2117 mtx_unlock(&sc->vmbus_prichan_lock);
2119 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task);
2126 struct vmbus_channel **ret, *chan;
2129 KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt));
2134 mtx_lock(&pri_chan->ch_subchan_lock);
2136 while (pri_chan->ch_subchan_cnt < subchan_cnt)
2137 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0);
2140 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) {
2141 /* TODO: refcnt chan */
2142 ret[i] = chan;
2149 pri_chan->ch_subchan_cnt, subchan_cnt));
2151 mtx_unlock(&pri_chan->ch_subchan_lock);
2166 mtx_lock(&pri_chan->ch_subchan_lock);
2167 while (pri_chan->ch_subchan_cnt > 0)
2168 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0);
2169 mtx_unlock(&pri_chan->ch_subchan_lock);
2178 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
2188 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on)
2191 chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD;
2193 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD;
2197 vmbus_chan_id(const struct vmbus_channel *chan)
2199 return chan->ch_id;
2203 vmbus_chan_subidx(const struct vmbus_channel *chan)
2205 return chan->ch_subidx;
2209 vmbus_chan_is_primary(const struct vmbus_channel *chan)
2211 if (VMBUS_CHAN_ISPRIMARY(chan))
2218 vmbus_chan_is_hvs(const struct vmbus_channel *chan)
2220 return chan->ch_is_hvs;
2224 vmbus_chan_is_hvs_conn_from_host(const struct vmbus_channel *chan)
2226 KASSERT(vmbus_chan_is_hvs(chan) == true,
2227 ("Not a HyperV Socket channel %u", chan->ch_id));
2228 if (chan->ch_hvs_conn_from_host != 0)
2235 vmbus_chan_guid_type(struct vmbus_channel *chan)
2237 return &chan->ch_guid_type;
2241 vmbus_chan_guid_inst(struct vmbus_channel *chan)
2243 return &chan->ch_guid_inst;
2260 vmbus_chan_tx_empty(const struct vmbus_channel *chan)
2263 return (vmbus_txbr_empty(&chan->ch_txbr));
2267 vmbus_chan_rx_empty(const struct vmbus_channel *chan)
2270 return (vmbus_rxbr_empty(&chan->ch_rxbr));
2274 vmbus_chan_printf(const struct vmbus_channel *chan, const char *fmt, ...)
2280 if (chan->ch_dev == NULL || !device_is_alive(chan->ch_dev))
2281 dev = chan->ch_vmbus->vmbus_dev;
2283 dev = chan->ch_dev;
2294 vmbus_chan_run_task(struct vmbus_channel *chan, struct task *task)
2297 taskqueue_enqueue(chan->ch_tq, task);
2298 taskqueue_drain(chan->ch_tq, task);
2302 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan)
2305 return (chan->ch_mgmt_tq);
2309 vmbus_chan_is_revoked(const struct vmbus_channel *chan)
2312 if (chan->ch_stflags & VMBUS_CHAN_ST_REVOKED)
2318 vmbus_chan_set_orphan(struct vmbus_channel *chan, struct vmbus_xact_ctx *xact)
2321 sx_xlock(&chan->ch_orphan_lock);
2322 chan->ch_orphan_xact = xact;
2323 sx_xunlock(&chan->ch_orphan_lock);
2327 vmbus_chan_unset_orphan(struct vmbus_channel *chan)
2330 sx_xlock(&chan->ch_orphan_lock);
2331 chan->ch_orphan_xact = NULL;
2332 sx_xunlock(&chan->ch_orphan_lock);
2336 vmbus_chan_xact_wait(const struct vmbus_channel *chan,
2345 if (vmbus_chan_is_revoked(chan)) {
2359 while (!vmbus_chan_rx_empty(chan)) {
2370 vmbus_chan_poll_enable(struct vmbus_channel *chan, u_int pollhz)
2375 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2376 ("enable polling on non-batch chan%u", chan->ch_id));
2380 arg.poll_chan = chan;
2383 vmbus_chan_run_task(chan, &poll_cfg);
2387 vmbus_chan_poll_disable(struct vmbus_channel *chan)
2391 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD,
2392 ("disable polling on non-batch chan%u", chan->ch_id));
2394 TASK_INIT(&poll_dis, 0, vmbus_chan_polldis_task, chan);
2395 vmbus_chan_run_task(chan, &poll_dis);