vmbus_chan.c revision 302881
1250199Sgrehan/*- 2298446Ssephe * Copyright (c) 2009-2012,2016 Microsoft Corp. 3250199Sgrehan * Copyright (c) 2012 NetApp Inc. 4250199Sgrehan * Copyright (c) 2012 Citrix Inc. 5250199Sgrehan * All rights reserved. 6250199Sgrehan * 7250199Sgrehan * Redistribution and use in source and binary forms, with or without 8250199Sgrehan * modification, are permitted provided that the following conditions 9250199Sgrehan * are met: 10250199Sgrehan * 1. Redistributions of source code must retain the above copyright 11250199Sgrehan * notice unmodified, this list of conditions, and the following 12250199Sgrehan * disclaimer. 13250199Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 14250199Sgrehan * notice, this list of conditions and the following disclaimer in the 15250199Sgrehan * documentation and/or other materials provided with the distribution. 16250199Sgrehan * 17250199Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18250199Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19250199Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20250199Sgrehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21250199Sgrehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22250199Sgrehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23250199Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24250199Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25250199Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26250199Sgrehan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27250199Sgrehan */ 28250199Sgrehan 29256276Sdim#include <sys/cdefs.h> 30256276Sdim__FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hv_channel.c 302881 2016-07-15 07:07:17Z sephe $"); 31256276Sdim 32250199Sgrehan#include <sys/param.h> 33296028Ssephe#include <sys/kernel.h> 34250199Sgrehan#include <sys/malloc.h> 35250199Sgrehan#include <sys/systm.h> 36250199Sgrehan#include <sys/mbuf.h> 37250199Sgrehan#include <sys/lock.h> 38250199Sgrehan#include <sys/mutex.h> 39296181Ssephe#include <sys/sysctl.h> 40301588Ssephe 41301588Ssephe#include <machine/atomic.h> 42250199Sgrehan#include <machine/bus.h> 43301588Ssephe 44250199Sgrehan#include <vm/vm.h> 45250199Sgrehan#include <vm/vm_param.h> 46250199Sgrehan#include <vm/pmap.h> 47250199Sgrehan 48302872Ssephe#include <dev/hyperv/include/hyperv_busdma.h> 49300102Ssephe#include <dev/hyperv/vmbus/hv_vmbus_priv.h> 50302619Ssephe#include <dev/hyperv/vmbus/hyperv_var.h> 51301588Ssephe#include <dev/hyperv/vmbus/vmbus_reg.h> 52300102Ssephe#include <dev/hyperv/vmbus/vmbus_var.h> 53250199Sgrehan 54302731Ssephestatic void vmbus_chan_send_event(hv_vmbus_channel* channel); 55302692Ssephestatic void vmbus_chan_update_evtflagcnt(struct vmbus_softc *, 56302692Ssephe const struct hv_vmbus_channel *); 57302864Ssephe 58302713Ssephestatic void vmbus_chan_task(void *, int); 59302713Ssephestatic void vmbus_chan_task_nobatch(void *, int); 60302864Ssephestatic void vmbus_chan_detach_task(void *, int); 61250199Sgrehan 62302864Ssephestatic void vmbus_chan_msgproc_choffer(struct vmbus_softc *, 63302864Ssephe const struct vmbus_message *); 64302864Ssephestatic void vmbus_chan_msgproc_chrescind(struct vmbus_softc *, 65302864Ssephe const struct vmbus_message *); 66302864Ssephe 67302864Ssephe/* 68302864Ssephe * Vmbus channel message processing. 69302864Ssephe */ 70302864Ssephestatic const vmbus_chanmsg_proc_t 71302864Ssephevmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = { 72302864Ssephe VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer), 73302864Ssephe VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind), 74302864Ssephe 75302864Ssephe VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP), 76302864Ssephe VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP), 77302864Ssephe VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP) 78302864Ssephe}; 79302864Ssephe 80250199Sgrehan/** 81250199Sgrehan * @brief Trigger an event notification on the specified channel 82250199Sgrehan */ 83250199Sgrehanstatic void 84302731Ssephevmbus_chan_send_event(hv_vmbus_channel *channel) 85250199Sgrehan{ 86302618Ssephe struct vmbus_softc *sc = channel->vmbus_sc; 87302693Ssephe uint32_t chanid = channel->ch_id; 88302618Ssephe 89302618Ssephe atomic_set_long(&sc->vmbus_tx_evtflags[chanid >> VMBUS_EVTFLAG_SHIFT], 90302618Ssephe 1UL << (chanid & VMBUS_EVTFLAG_MASK)); 91302618Ssephe 92302695Ssephe if (channel->ch_flags & VMBUS_CHAN_FLAG_HASMNF) { 93302731Ssephe atomic_set_int( 94302731Ssephe &sc->vmbus_mnf2->mnf_trigs[channel->ch_montrig_idx].mt_pending, 95302731Ssephe channel->ch_montrig_mask); 96250199Sgrehan } else { 97302726Ssephe hypercall_signal_event(channel->ch_monprm_dma.hv_paddr); 98250199Sgrehan } 99250199Sgrehan} 100250199Sgrehan 101296289Ssephestatic int 102296289Ssephevmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS) 103296289Ssephe{ 104296289Ssephe struct hv_vmbus_channel *chan = arg1; 105296289Ssephe int alloc = 0; 106296289Ssephe 107302695Ssephe if (chan->ch_flags & VMBUS_CHAN_FLAG_HASMNF) 108296289Ssephe alloc = 1; 109296289Ssephe return sysctl_handle_int(oidp, &alloc, 0, req); 110296289Ssephe} 111296289Ssephe 112296181Ssephestatic void 113296290Ssephevmbus_channel_sysctl_create(hv_vmbus_channel* channel) 114296181Ssephe{ 115296181Ssephe device_t dev; 116296181Ssephe struct sysctl_oid *devch_sysctl; 117296181Ssephe struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl; 118296181Ssephe struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl; 119296181Ssephe struct sysctl_ctx_list *ctx; 120296181Ssephe uint32_t ch_id; 121296181Ssephe uint16_t sub_ch_id; 122296181Ssephe char name[16]; 123296181Ssephe 124302819Ssephe hv_vmbus_channel* primary_ch = channel->ch_prichan; 125296181Ssephe 126296181Ssephe if (primary_ch == NULL) { 127302706Ssephe dev = channel->ch_dev; 128302693Ssephe ch_id = channel->ch_id; 129296181Ssephe } else { 130302706Ssephe dev = primary_ch->ch_dev; 131302693Ssephe ch_id = primary_ch->ch_id; 132302694Ssephe sub_ch_id = channel->ch_subidx; 133296181Ssephe } 134302633Ssephe ctx = &channel->ch_sysctl_ctx; 135302633Ssephe sysctl_ctx_init(ctx); 136296181Ssephe /* This creates dev.DEVNAME.DEVUNIT.channel tree */ 137296181Ssephe devch_sysctl = SYSCTL_ADD_NODE(ctx, 138296181Ssephe SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 139298693Ssephe OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 140296181Ssephe /* This creates dev.DEVNAME.DEVUNIT.channel.CHANID tree */ 141296181Ssephe snprintf(name, sizeof(name), "%d", ch_id); 142296181Ssephe devch_id_sysctl = SYSCTL_ADD_NODE(ctx, 143296181Ssephe SYSCTL_CHILDREN(devch_sysctl), 144298693Ssephe OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 145296181Ssephe 146296181Ssephe if (primary_ch != NULL) { 147296181Ssephe devch_sub_sysctl = SYSCTL_ADD_NODE(ctx, 148296181Ssephe SYSCTL_CHILDREN(devch_id_sysctl), 149298693Ssephe OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 150296181Ssephe snprintf(name, sizeof(name), "%d", sub_ch_id); 151296181Ssephe devch_id_sysctl = SYSCTL_ADD_NODE(ctx, 152296181Ssephe SYSCTL_CHILDREN(devch_sub_sysctl), 153298693Ssephe OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 154296188Ssephe 155296188Ssephe SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), 156296188Ssephe OID_AUTO, "chanid", CTLFLAG_RD, 157302693Ssephe &channel->ch_id, 0, "channel id"); 158296181Ssephe } 159296188Ssephe SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO, 160302873Ssephe "cpu", CTLFLAG_RD, &channel->ch_cpuid, 0, "owner CPU id"); 161296289Ssephe SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO, 162298693Ssephe "monitor_allocated", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 163298693Ssephe channel, 0, vmbus_channel_sysctl_monalloc, "I", 164296289Ssephe "is monitor allocated to this channel"); 165296188Ssephe 166296181Ssephe devch_id_in_sysctl = SYSCTL_ADD_NODE(ctx, 167296181Ssephe SYSCTL_CHILDREN(devch_id_sysctl), 168296181Ssephe OID_AUTO, 169296181Ssephe "in", 170298693Ssephe CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 171296181Ssephe devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx, 172296181Ssephe SYSCTL_CHILDREN(devch_id_sysctl), 173296181Ssephe OID_AUTO, 174296181Ssephe "out", 175298693Ssephe CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 176296181Ssephe hv_ring_buffer_stat(ctx, 177296181Ssephe SYSCTL_CHILDREN(devch_id_in_sysctl), 178296181Ssephe &(channel->inbound), 179296181Ssephe "inbound ring buffer stats"); 180296181Ssephe hv_ring_buffer_stat(ctx, 181296181Ssephe SYSCTL_CHILDREN(devch_id_out_sysctl), 182296181Ssephe &(channel->outbound), 183296181Ssephe "outbound ring buffer stats"); 184296181Ssephe} 185296290Ssephe 186250199Sgrehan/** 187250199Sgrehan * @brief Open the specified channel 188250199Sgrehan */ 189250199Sgrehanint 190250199Sgrehanhv_vmbus_channel_open( 191250199Sgrehan hv_vmbus_channel* new_channel, 192250199Sgrehan uint32_t send_ring_buffer_size, 193250199Sgrehan uint32_t recv_ring_buffer_size, 194250199Sgrehan void* user_data, 195250199Sgrehan uint32_t user_data_len, 196302874Ssephe vmbus_chan_callback_t cb, 197302874Ssephe void *cbarg) 198250199Sgrehan{ 199302607Ssephe struct vmbus_softc *sc = new_channel->vmbus_sc; 200302607Ssephe const struct vmbus_chanmsg_chopen_resp *resp; 201302607Ssephe const struct vmbus_message *msg; 202302607Ssephe struct vmbus_chanmsg_chopen *req; 203302607Ssephe struct vmbus_msghc *mh; 204302607Ssephe uint32_t status; 205250199Sgrehan int ret = 0; 206302872Ssephe uint8_t *br; 207250199Sgrehan 208302607Ssephe if (user_data_len > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) { 209302607Ssephe device_printf(sc->vmbus_dev, 210302607Ssephe "invalid udata len %u for chan%u\n", 211302693Ssephe user_data_len, new_channel->ch_id); 212302607Ssephe return EINVAL; 213302607Ssephe } 214302872Ssephe KASSERT((send_ring_buffer_size & PAGE_MASK) == 0, 215302872Ssephe ("send bufring size is not multiple page")); 216302872Ssephe KASSERT((recv_ring_buffer_size & PAGE_MASK) == 0, 217302872Ssephe ("recv bufring size is not multiple page")); 218302607Ssephe 219302812Ssephe if (atomic_testandset_int(&new_channel->ch_stflags, 220302812Ssephe VMBUS_CHAN_ST_OPENED_SHIFT)) 221302812Ssephe panic("double-open chan%u", new_channel->ch_id); 222282212Swhu 223302874Ssephe new_channel->ch_cb = cb; 224302874Ssephe new_channel->ch_cbarg = cbarg; 225250199Sgrehan 226302692Ssephe vmbus_chan_update_evtflagcnt(sc, new_channel); 227300102Ssephe 228302874Ssephe new_channel->ch_tq = VMBUS_PCPU_GET(new_channel->vmbus_sc, event_tq, 229302873Ssephe new_channel->ch_cpuid); 230302713Ssephe if (new_channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) { 231302874Ssephe TASK_INIT(&new_channel->ch_task, 0, vmbus_chan_task, 232302874Ssephe new_channel); 233302713Ssephe } else { 234302874Ssephe TASK_INIT(&new_channel->ch_task, 0, vmbus_chan_task_nobatch, 235302874Ssephe new_channel); 236302713Ssephe } 237294886Ssephe 238302872Ssephe /* 239302872Ssephe * Allocate the TX+RX bufrings. 240302872Ssephe * XXX should use ch_dev dtag 241302872Ssephe */ 242302872Ssephe br = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 243302872Ssephe PAGE_SIZE, 0, send_ring_buffer_size + recv_ring_buffer_size, 244302872Ssephe &new_channel->ch_bufring_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 245302872Ssephe if (br == NULL) { 246302872Ssephe device_printf(sc->vmbus_dev, "bufring allocation failed\n"); 247302812Ssephe ret = ENOMEM; 248302812Ssephe goto failed; 249302812Ssephe } 250302872Ssephe new_channel->ch_bufring = br; 251250199Sgrehan 252302872Ssephe /* TX bufring comes first */ 253302872Ssephe hv_vmbus_ring_buffer_init(&new_channel->outbound, 254302872Ssephe br, send_ring_buffer_size); 255302872Ssephe /* RX bufring immediately follows TX bufring */ 256302872Ssephe hv_vmbus_ring_buffer_init(&new_channel->inbound, 257302872Ssephe br + send_ring_buffer_size, recv_ring_buffer_size); 258250199Sgrehan 259296290Ssephe /* Create sysctl tree for this channel */ 260296290Ssephe vmbus_channel_sysctl_create(new_channel); 261296181Ssephe 262302872Ssephe /* 263302872Ssephe * Connect the bufrings, both RX and TX, to this channel. 264250199Sgrehan */ 265302872Ssephe ret = vmbus_chan_gpadl_connect(new_channel, 266302872Ssephe new_channel->ch_bufring_dma.hv_paddr, 267250199Sgrehan send_ring_buffer_size + recv_ring_buffer_size, 268302872Ssephe &new_channel->ch_bufring_gpadl); 269302872Ssephe if (ret != 0) { 270302872Ssephe device_printf(sc->vmbus_dev, 271302872Ssephe "failed to connect bufring GPADL to chan%u\n", 272302872Ssephe new_channel->ch_id); 273302872Ssephe goto failed; 274302872Ssephe } 275250199Sgrehan 276302607Ssephe /* 277302607Ssephe * Open channel w/ the bufring GPADL on the target CPU. 278250199Sgrehan */ 279302607Ssephe mh = vmbus_msghc_get(sc, sizeof(*req)); 280302607Ssephe if (mh == NULL) { 281302607Ssephe device_printf(sc->vmbus_dev, 282302607Ssephe "can not get msg hypercall for chopen(chan%u)\n", 283302693Ssephe new_channel->ch_id); 284302812Ssephe ret = ENXIO; 285302812Ssephe goto failed; 286302607Ssephe } 287250199Sgrehan 288302607Ssephe req = vmbus_msghc_dataptr(mh); 289302607Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN; 290302693Ssephe req->chm_chanid = new_channel->ch_id; 291302693Ssephe req->chm_openid = new_channel->ch_id; 292302872Ssephe req->chm_gpadl = new_channel->ch_bufring_gpadl; 293302873Ssephe req->chm_vcpuid = new_channel->ch_vcpuid; 294302607Ssephe req->chm_rxbr_pgofs = send_ring_buffer_size >> PAGE_SHIFT; 295250199Sgrehan if (user_data_len) 296302607Ssephe memcpy(req->chm_udata, user_data, user_data_len); 297250199Sgrehan 298302607Ssephe ret = vmbus_msghc_exec(sc, mh); 299302607Ssephe if (ret != 0) { 300302607Ssephe device_printf(sc->vmbus_dev, 301302607Ssephe "chopen(chan%u) msg hypercall exec failed: %d\n", 302302693Ssephe new_channel->ch_id, ret); 303302607Ssephe vmbus_msghc_put(sc, mh); 304302812Ssephe goto failed; 305302607Ssephe } 306250199Sgrehan 307302607Ssephe msg = vmbus_msghc_wait_result(sc, mh); 308302607Ssephe resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data; 309302607Ssephe status = resp->chm_status; 310250199Sgrehan 311302607Ssephe vmbus_msghc_put(sc, mh); 312250199Sgrehan 313302607Ssephe if (status == 0) { 314302607Ssephe if (bootverbose) { 315302607Ssephe device_printf(sc->vmbus_dev, "chan%u opened\n", 316302693Ssephe new_channel->ch_id); 317302607Ssephe } 318302812Ssephe return 0; 319250199Sgrehan } 320302812Ssephe 321302812Ssephe device_printf(sc->vmbus_dev, "failed to open chan%u\n", 322302812Ssephe new_channel->ch_id); 323302812Ssephe ret = ENXIO; 324302812Ssephe 325302812Ssephefailed: 326302872Ssephe if (new_channel->ch_bufring_gpadl) { 327302872Ssephe hv_vmbus_channel_teardown_gpdal(new_channel, 328302872Ssephe new_channel->ch_bufring_gpadl); 329302872Ssephe new_channel->ch_bufring_gpadl = 0; 330302872Ssephe } 331302872Ssephe if (new_channel->ch_bufring != NULL) { 332302872Ssephe hyperv_dmamem_free(&new_channel->ch_bufring_dma, 333302872Ssephe new_channel->ch_bufring); 334302872Ssephe new_channel->ch_bufring = NULL; 335302872Ssephe } 336302812Ssephe atomic_clear_int(&new_channel->ch_stflags, VMBUS_CHAN_ST_OPENED); 337302812Ssephe return ret; 338250199Sgrehan} 339250199Sgrehan 340250199Sgrehan/** 341302609Ssephe * @brief Establish a GPADL for the specified buffer 342250199Sgrehan */ 343302609Ssepheint 344302609Ssephehv_vmbus_channel_establish_gpadl(struct hv_vmbus_channel *channel, 345302871Ssephe void *contig_buffer, uint32_t size, uint32_t *gpadl) 346250199Sgrehan{ 347302871Ssephe return vmbus_chan_gpadl_connect(channel, 348302871Ssephe hv_get_phys_addr(contig_buffer), size, gpadl); 349302871Ssephe} 350302871Ssephe 351302871Ssepheint 352302871Ssephevmbus_chan_gpadl_connect(struct hv_vmbus_channel *chan, bus_addr_t paddr, 353302871Ssephe int size, uint32_t *gpadl0) 354302871Ssephe{ 355302871Ssephe struct vmbus_softc *sc = chan->vmbus_sc; 356302609Ssephe struct vmbus_msghc *mh; 357302609Ssephe struct vmbus_chanmsg_gpadl_conn *req; 358302609Ssephe const struct vmbus_message *msg; 359302609Ssephe size_t reqsz; 360302609Ssephe uint32_t gpadl, status; 361302609Ssephe int page_count, range_len, i, cnt, error; 362302871Ssephe uint64_t page_id; 363250199Sgrehan 364302609Ssephe /* 365302609Ssephe * Preliminary checks. 366302609Ssephe */ 367250199Sgrehan 368302609Ssephe KASSERT((size & PAGE_MASK) == 0, 369302871Ssephe ("invalid GPA size %d, not multiple page size", size)); 370250199Sgrehan page_count = size >> PAGE_SHIFT; 371250199Sgrehan 372302609Ssephe KASSERT((paddr & PAGE_MASK) == 0, 373302609Ssephe ("GPA is not page aligned %jx", (uintmax_t)paddr)); 374302609Ssephe page_id = paddr >> PAGE_SHIFT; 375250199Sgrehan 376302609Ssephe range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]); 377302609Ssephe /* 378302609Ssephe * We don't support multiple GPA ranges. 379302609Ssephe */ 380302609Ssephe if (range_len > UINT16_MAX) { 381302609Ssephe device_printf(sc->vmbus_dev, "GPA too large, %d pages\n", 382302609Ssephe page_count); 383302609Ssephe return EOPNOTSUPP; 384250199Sgrehan } 385250199Sgrehan 386302609Ssephe /* 387302609Ssephe * Allocate GPADL id. 388302609Ssephe */ 389302630Ssephe gpadl = vmbus_gpadl_alloc(sc); 390302609Ssephe *gpadl0 = gpadl; 391250199Sgrehan 392302609Ssephe /* 393302609Ssephe * Connect this GPADL to the target channel. 394302609Ssephe * 395302609Ssephe * NOTE: 396302609Ssephe * Since each message can only hold small set of page 397302609Ssephe * addresses, several messages may be required to 398302609Ssephe * complete the connection. 399302609Ssephe */ 400302609Ssephe if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX) 401302609Ssephe cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX; 402302609Ssephe else 403302609Ssephe cnt = page_count; 404302609Ssephe page_count -= cnt; 405250199Sgrehan 406302609Ssephe reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn, 407302609Ssephe chm_range.gpa_page[cnt]); 408302609Ssephe mh = vmbus_msghc_get(sc, reqsz); 409302609Ssephe if (mh == NULL) { 410302609Ssephe device_printf(sc->vmbus_dev, 411302609Ssephe "can not get msg hypercall for gpadl->chan%u\n", 412302871Ssephe chan->ch_id); 413302609Ssephe return EIO; 414250199Sgrehan } 415250199Sgrehan 416302609Ssephe req = vmbus_msghc_dataptr(mh); 417302609Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN; 418302871Ssephe req->chm_chanid = chan->ch_id; 419302609Ssephe req->chm_gpadl = gpadl; 420302609Ssephe req->chm_range_len = range_len; 421302609Ssephe req->chm_range_cnt = 1; 422302609Ssephe req->chm_range.gpa_len = size; 423302609Ssephe req->chm_range.gpa_ofs = 0; 424302609Ssephe for (i = 0; i < cnt; ++i) 425302609Ssephe req->chm_range.gpa_page[i] = page_id++; 426250199Sgrehan 427302609Ssephe error = vmbus_msghc_exec(sc, mh); 428302609Ssephe if (error) { 429302609Ssephe device_printf(sc->vmbus_dev, 430302609Ssephe "gpadl->chan%u msg hypercall exec failed: %d\n", 431302871Ssephe chan->ch_id, error); 432302609Ssephe vmbus_msghc_put(sc, mh); 433302609Ssephe return error; 434302609Ssephe } 435250199Sgrehan 436302609Ssephe while (page_count > 0) { 437302609Ssephe struct vmbus_chanmsg_gpadl_subconn *subreq; 438250199Sgrehan 439302609Ssephe if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX) 440302609Ssephe cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX; 441302609Ssephe else 442302609Ssephe cnt = page_count; 443302609Ssephe page_count -= cnt; 444250199Sgrehan 445302609Ssephe reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn, 446302609Ssephe chm_gpa_page[cnt]); 447302609Ssephe vmbus_msghc_reset(mh, reqsz); 448250199Sgrehan 449302609Ssephe subreq = vmbus_msghc_dataptr(mh); 450302609Ssephe subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN; 451302609Ssephe subreq->chm_gpadl = gpadl; 452302609Ssephe for (i = 0; i < cnt; ++i) 453302609Ssephe subreq->chm_gpa_page[i] = page_id++; 454250199Sgrehan 455302609Ssephe vmbus_msghc_exec_noresult(mh); 456250199Sgrehan } 457302609Ssephe KASSERT(page_count == 0, ("invalid page count %d", page_count)); 458250199Sgrehan 459302609Ssephe msg = vmbus_msghc_wait_result(sc, mh); 460302609Ssephe status = ((const struct vmbus_chanmsg_gpadl_connresp *) 461302609Ssephe msg->msg_data)->chm_status; 462250199Sgrehan 463302609Ssephe vmbus_msghc_put(sc, mh); 464250199Sgrehan 465302609Ssephe if (status != 0) { 466302609Ssephe device_printf(sc->vmbus_dev, "gpadl->chan%u failed: " 467302871Ssephe "status %u\n", chan->ch_id, status); 468302609Ssephe return EIO; 469302632Ssephe } else { 470302632Ssephe if (bootverbose) { 471302632Ssephe device_printf(sc->vmbus_dev, "gpadl->chan%u " 472302871Ssephe "succeeded\n", chan->ch_id); 473302632Ssephe } 474302609Ssephe } 475302609Ssephe return 0; 476250199Sgrehan} 477250199Sgrehan 478302611Ssephe/* 479302611Ssephe * Disconnect the GPA from the target channel 480250199Sgrehan */ 481250199Sgrehanint 482302611Ssephehv_vmbus_channel_teardown_gpdal(struct hv_vmbus_channel *chan, uint32_t gpadl) 483250199Sgrehan{ 484302611Ssephe struct vmbus_softc *sc = chan->vmbus_sc; 485302611Ssephe struct vmbus_msghc *mh; 486302611Ssephe struct vmbus_chanmsg_gpadl_disconn *req; 487302611Ssephe int error; 488250199Sgrehan 489302611Ssephe mh = vmbus_msghc_get(sc, sizeof(*req)); 490302611Ssephe if (mh == NULL) { 491302611Ssephe device_printf(sc->vmbus_dev, 492302611Ssephe "can not get msg hypercall for gpa x->chan%u\n", 493302693Ssephe chan->ch_id); 494302611Ssephe return EBUSY; 495250199Sgrehan } 496250199Sgrehan 497302611Ssephe req = vmbus_msghc_dataptr(mh); 498302611Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN; 499302693Ssephe req->chm_chanid = chan->ch_id; 500302611Ssephe req->chm_gpadl = gpadl; 501250199Sgrehan 502302611Ssephe error = vmbus_msghc_exec(sc, mh); 503302611Ssephe if (error) { 504302611Ssephe device_printf(sc->vmbus_dev, 505302611Ssephe "gpa x->chan%u msg hypercall exec failed: %d\n", 506302693Ssephe chan->ch_id, error); 507302611Ssephe vmbus_msghc_put(sc, mh); 508302611Ssephe return error; 509302611Ssephe } 510250199Sgrehan 511302611Ssephe vmbus_msghc_wait_result(sc, mh); 512302611Ssephe /* Discard result; no useful information */ 513302611Ssephe vmbus_msghc_put(sc, mh); 514250199Sgrehan 515302611Ssephe return 0; 516250199Sgrehan} 517250199Sgrehan 518282212Swhustatic void 519282212Swhuhv_vmbus_channel_close_internal(hv_vmbus_channel *channel) 520250199Sgrehan{ 521302610Ssephe struct vmbus_softc *sc = channel->vmbus_sc; 522302610Ssephe struct vmbus_msghc *mh; 523302610Ssephe struct vmbus_chanmsg_chclose *req; 524302874Ssephe struct taskqueue *tq = channel->ch_tq; 525302610Ssephe int error; 526250199Sgrehan 527302812Ssephe /* TODO: stringent check */ 528302812Ssephe atomic_clear_int(&channel->ch_stflags, VMBUS_CHAN_ST_OPENED); 529302812Ssephe 530302633Ssephe sysctl_ctx_free(&channel->ch_sysctl_ctx); 531282212Swhu 532282212Swhu /* 533302874Ssephe * Set ch_tq to NULL to avoid more requests be scheduled 534294886Ssephe */ 535302874Ssephe channel->ch_tq = NULL; 536302874Ssephe taskqueue_drain(tq, &channel->ch_task); 537302874Ssephe channel->ch_cb = NULL; 538250199Sgrehan 539250199Sgrehan /** 540250199Sgrehan * Send a closing message 541250199Sgrehan */ 542250199Sgrehan 543302610Ssephe mh = vmbus_msghc_get(sc, sizeof(*req)); 544302610Ssephe if (mh == NULL) { 545302610Ssephe device_printf(sc->vmbus_dev, 546302610Ssephe "can not get msg hypercall for chclose(chan%u)\n", 547302693Ssephe channel->ch_id); 548302610Ssephe return; 549302610Ssephe } 550250199Sgrehan 551302610Ssephe req = vmbus_msghc_dataptr(mh); 552302610Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE; 553302693Ssephe req->chm_chanid = channel->ch_id; 554250199Sgrehan 555302610Ssephe error = vmbus_msghc_exec_noresult(mh); 556302610Ssephe vmbus_msghc_put(sc, mh); 557302610Ssephe 558302610Ssephe if (error) { 559302610Ssephe device_printf(sc->vmbus_dev, 560302610Ssephe "chclose(chan%u) msg hypercall exec failed: %d\n", 561302693Ssephe channel->ch_id, error); 562302610Ssephe return; 563302610Ssephe } else if (bootverbose) { 564302610Ssephe device_printf(sc->vmbus_dev, "close chan%u\n", 565302693Ssephe channel->ch_id); 566302610Ssephe } 567302610Ssephe 568250199Sgrehan /* Tear down the gpadl for the channel's ring buffer */ 569302872Ssephe if (channel->ch_bufring_gpadl) { 570250199Sgrehan hv_vmbus_channel_teardown_gpdal(channel, 571302872Ssephe channel->ch_bufring_gpadl); 572302872Ssephe channel->ch_bufring_gpadl = 0; 573250199Sgrehan } 574250199Sgrehan 575250199Sgrehan /* TODO: Send a msg to release the childRelId */ 576250199Sgrehan 577250199Sgrehan /* cleanup the ring buffers for this channel */ 578250199Sgrehan hv_ring_buffer_cleanup(&channel->outbound); 579250199Sgrehan hv_ring_buffer_cleanup(&channel->inbound); 580250199Sgrehan 581302872Ssephe if (channel->ch_bufring != NULL) { 582302872Ssephe hyperv_dmamem_free(&channel->ch_bufring_dma, 583302872Ssephe channel->ch_bufring); 584302872Ssephe channel->ch_bufring = NULL; 585302872Ssephe } 586282212Swhu} 587250199Sgrehan 588302818Ssephe/* 589302818Ssephe * Caller should make sure that all sub-channels have 590302818Ssephe * been added to 'chan' and all to-be-closed channels 591302818Ssephe * are not being opened. 592282212Swhu */ 593282212Swhuvoid 594302818Ssephehv_vmbus_channel_close(struct hv_vmbus_channel *chan) 595282212Swhu{ 596302818Ssephe int subchan_cnt; 597282212Swhu 598302818Ssephe if (!VMBUS_CHAN_ISPRIMARY(chan)) { 599282212Swhu /* 600302818Ssephe * Sub-channel is closed when its primary channel 601302818Ssephe * is closed; done. 602282212Swhu */ 603282212Swhu return; 604282212Swhu } 605282212Swhu 606250199Sgrehan /* 607302818Ssephe * Close all sub-channels, if any. 608250199Sgrehan */ 609302819Ssephe subchan_cnt = chan->ch_subchan_cnt; 610302818Ssephe if (subchan_cnt > 0) { 611302818Ssephe struct hv_vmbus_channel **subchan; 612302818Ssephe int i; 613302818Ssephe 614302818Ssephe subchan = vmbus_get_subchan(chan, subchan_cnt); 615302818Ssephe for (i = 0; i < subchan_cnt; ++i) 616302818Ssephe hv_vmbus_channel_close_internal(subchan[i]); 617302818Ssephe vmbus_rel_subchan(subchan, subchan_cnt); 618250199Sgrehan } 619302818Ssephe 620302818Ssephe /* Then close the primary channel. */ 621302818Ssephe hv_vmbus_channel_close_internal(chan); 622250199Sgrehan} 623250199Sgrehan 624250199Sgrehanint 625302881Ssephehv_vmbus_channel_send_packet(struct hv_vmbus_channel *chan, 626302881Ssephe void *data, uint32_t dlen, uint64_t xactid, uint16_t type, uint16_t flags) 627250199Sgrehan{ 628302875Ssephe struct vmbus_chanpkt pkt; 629302881Ssephe int pktlen, pad_pktlen, hlen, error; 630302881Ssephe uint64_t pad = 0; 631302881Ssephe struct iovec iov[3]; 632302881Ssephe boolean_t send_evt; 633250199Sgrehan 634302881Ssephe hlen = sizeof(pkt); 635302881Ssephe pktlen = hlen + dlen; 636302881Ssephe pad_pktlen = roundup2(pktlen, VMBUS_CHANPKT_SIZE_ALIGN); 637250199Sgrehan 638302875Ssephe pkt.cp_hdr.cph_type = type; 639302875Ssephe pkt.cp_hdr.cph_flags = flags; 640302881Ssephe pkt.cp_hdr.cph_data_ofs = hlen >> VMBUS_CHANPKT_SIZE_SHIFT; 641302881Ssephe pkt.cp_hdr.cph_len = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT; 642302881Ssephe pkt.cp_hdr.cph_xactid = xactid; 643250199Sgrehan 644302875Ssephe iov[0].iov_base = &pkt; 645302881Ssephe iov[0].iov_len = hlen; 646302881Ssephe iov[1].iov_base = data; 647302881Ssephe iov[1].iov_len = dlen; 648302881Ssephe iov[2].iov_base = &pad; 649302881Ssephe iov[2].iov_len = pad_pktlen - pktlen; 650250199Sgrehan 651302881Ssephe error = hv_ring_buffer_write(&chan->outbound, iov, 3, &send_evt); 652302881Ssephe if (!error && send_evt) 653302881Ssephe vmbus_chan_send_event(chan); 654302881Ssephe return error; 655250199Sgrehan} 656250199Sgrehan 657250199Sgrehanint 658302876Ssephevmbus_chan_send_sglist(struct hv_vmbus_channel *chan, 659302876Ssephe struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid) 660250199Sgrehan{ 661302876Ssephe struct vmbus_chanpkt_sglist pkt; 662302876Ssephe int pktlen, pad_pktlen, hlen, error; 663302876Ssephe struct iovec iov[4]; 664302876Ssephe boolean_t send_evt; 665302876Ssephe uint64_t pad = 0; 666250199Sgrehan 667302876Ssephe KASSERT(sglen < VMBUS_CHAN_SGLIST_MAX, 668302876Ssephe ("invalid sglist len %d", sglen)); 669250199Sgrehan 670302876Ssephe hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]); 671302876Ssephe pktlen = hlen + dlen; 672302876Ssephe pad_pktlen = roundup2(pktlen, VMBUS_CHANPKT_SIZE_ALIGN); 673250199Sgrehan 674302880Ssephe pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 675302879Ssephe pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 676302876Ssephe pkt.cp_hdr.cph_data_ofs = hlen >> VMBUS_CHANPKT_SIZE_SHIFT; 677302876Ssephe pkt.cp_hdr.cph_len = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT; 678302876Ssephe pkt.cp_hdr.cph_xactid = xactid; 679302876Ssephe pkt.cp_rsvd = 0; 680302876Ssephe pkt.cp_gpa_cnt = sglen; 681250199Sgrehan 682302876Ssephe iov[0].iov_base = &pkt; 683302876Ssephe iov[0].iov_len = sizeof(pkt); 684302876Ssephe iov[1].iov_base = sg; 685302876Ssephe iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen; 686302876Ssephe iov[2].iov_base = data; 687302876Ssephe iov[2].iov_len = dlen; 688302876Ssephe iov[3].iov_base = &pad; 689302876Ssephe iov[3].iov_len = pad_pktlen - pktlen; 690250199Sgrehan 691302876Ssephe error = hv_ring_buffer_write(&chan->outbound, iov, 4, &send_evt); 692302876Ssephe if (!error && send_evt) 693302876Ssephe vmbus_chan_send_event(chan); 694302876Ssephe return error; 695250199Sgrehan} 696250199Sgrehan 697250199Sgrehanint 698302878Ssephevmbus_chan_send_prplist(struct hv_vmbus_channel *chan, 699302878Ssephe struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen, 700302878Ssephe uint64_t xactid) 701250199Sgrehan{ 702302878Ssephe struct vmbus_chanpkt_prplist pkt; 703302878Ssephe int pktlen, pad_pktlen, hlen, error; 704302878Ssephe struct iovec iov[4]; 705302878Ssephe boolean_t send_evt; 706302878Ssephe uint64_t pad = 0; 707250199Sgrehan 708302878Ssephe KASSERT(prp_cnt < VMBUS_CHAN_PRPLIST_MAX, 709302878Ssephe ("invalid prplist entry count %d", prp_cnt)); 710250199Sgrehan 711302878Ssephe hlen = __offsetof(struct vmbus_chanpkt_prplist, 712302878Ssephe cp_range[0].gpa_page[prp_cnt]); 713302878Ssephe pktlen = hlen + dlen; 714302878Ssephe pad_pktlen = roundup2(pktlen, VMBUS_CHANPKT_SIZE_ALIGN); 715250199Sgrehan 716302880Ssephe pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 717302879Ssephe pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 718302878Ssephe pkt.cp_hdr.cph_data_ofs = hlen >> VMBUS_CHANPKT_SIZE_SHIFT; 719302878Ssephe pkt.cp_hdr.cph_len = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT; 720302878Ssephe pkt.cp_hdr.cph_xactid = xactid; 721302878Ssephe pkt.cp_rsvd = 0; 722302878Ssephe pkt.cp_range_cnt = 1; 723250199Sgrehan 724302878Ssephe iov[0].iov_base = &pkt; 725302878Ssephe iov[0].iov_len = sizeof(pkt); 726302878Ssephe iov[1].iov_base = prp; 727302878Ssephe iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]); 728302878Ssephe iov[2].iov_base = data; 729302878Ssephe iov[2].iov_len = dlen; 730302878Ssephe iov[3].iov_base = &pad; 731302878Ssephe iov[3].iov_len = pad_pktlen - pktlen; 732250199Sgrehan 733302878Ssephe error = hv_ring_buffer_write(&chan->outbound, iov, 4, &send_evt); 734302878Ssephe if (!error && send_evt) 735302878Ssephe vmbus_chan_send_event(chan); 736302878Ssephe return error; 737250199Sgrehan} 738250199Sgrehan 739250199Sgrehan/** 740250199Sgrehan * @brief Retrieve the user packet on the specified channel 741250199Sgrehan */ 742250199Sgrehanint 743250199Sgrehanhv_vmbus_channel_recv_packet( 744250199Sgrehan hv_vmbus_channel* channel, 745250199Sgrehan void* Buffer, 746250199Sgrehan uint32_t buffer_len, 747250199Sgrehan uint32_t* buffer_actual_len, 748250199Sgrehan uint64_t* request_id) 749250199Sgrehan{ 750250199Sgrehan int ret; 751250199Sgrehan uint32_t user_len; 752250199Sgrehan uint32_t packet_len; 753250199Sgrehan hv_vm_packet_descriptor desc; 754250199Sgrehan 755250199Sgrehan *buffer_actual_len = 0; 756250199Sgrehan *request_id = 0; 757250199Sgrehan 758250199Sgrehan ret = hv_ring_buffer_peek(&channel->inbound, &desc, 759250199Sgrehan sizeof(hv_vm_packet_descriptor)); 760250199Sgrehan if (ret != 0) 761250199Sgrehan return (0); 762250199Sgrehan 763250199Sgrehan packet_len = desc.length8 << 3; 764250199Sgrehan user_len = packet_len - (desc.data_offset8 << 3); 765250199Sgrehan 766250199Sgrehan *buffer_actual_len = user_len; 767250199Sgrehan 768250199Sgrehan if (user_len > buffer_len) 769250199Sgrehan return (EINVAL); 770250199Sgrehan 771250199Sgrehan *request_id = desc.transaction_id; 772250199Sgrehan 773250199Sgrehan /* Copy over the packet to the user buffer */ 774250199Sgrehan ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len, 775250199Sgrehan (desc.data_offset8 << 3)); 776250199Sgrehan 777250199Sgrehan return (0); 778250199Sgrehan} 779250199Sgrehan 780250199Sgrehan/** 781250199Sgrehan * @brief Retrieve the raw packet on the specified channel 782250199Sgrehan */ 783250199Sgrehanint 784250199Sgrehanhv_vmbus_channel_recv_packet_raw( 785250199Sgrehan hv_vmbus_channel* channel, 786250199Sgrehan void* buffer, 787250199Sgrehan uint32_t buffer_len, 788250199Sgrehan uint32_t* buffer_actual_len, 789250199Sgrehan uint64_t* request_id) 790250199Sgrehan{ 791250199Sgrehan int ret; 792250199Sgrehan uint32_t packetLen; 793250199Sgrehan hv_vm_packet_descriptor desc; 794250199Sgrehan 795250199Sgrehan *buffer_actual_len = 0; 796250199Sgrehan *request_id = 0; 797250199Sgrehan 798250199Sgrehan ret = hv_ring_buffer_peek( 799250199Sgrehan &channel->inbound, &desc, 800250199Sgrehan sizeof(hv_vm_packet_descriptor)); 801250199Sgrehan 802250199Sgrehan if (ret != 0) 803250199Sgrehan return (0); 804250199Sgrehan 805250199Sgrehan packetLen = desc.length8 << 3; 806250199Sgrehan *buffer_actual_len = packetLen; 807250199Sgrehan 808250199Sgrehan if (packetLen > buffer_len) 809250199Sgrehan return (ENOBUFS); 810250199Sgrehan 811250199Sgrehan *request_id = desc.transaction_id; 812250199Sgrehan 813250199Sgrehan /* Copy over the entire packet to the user buffer */ 814250199Sgrehan ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0); 815250199Sgrehan 816250199Sgrehan return (0); 817250199Sgrehan} 818294886Ssephe 819294886Ssephestatic void 820302713Ssephevmbus_chan_task(void *xchan, int pending __unused) 821294886Ssephe{ 822302713Ssephe struct hv_vmbus_channel *chan = xchan; 823302874Ssephe vmbus_chan_callback_t cb = chan->ch_cb; 824302874Ssephe void *cbarg = chan->ch_cbarg; 825294886Ssephe 826302710Ssephe /* 827302710Ssephe * Optimize host to guest signaling by ensuring: 828302710Ssephe * 1. While reading the channel, we disable interrupts from 829302710Ssephe * host. 830302710Ssephe * 2. Ensure that we process all posted messages from the host 831302710Ssephe * before returning from this callback. 832302710Ssephe * 3. Once we return, enable signaling from the host. Once this 833302710Ssephe * state is set we check to see if additional packets are 834302710Ssephe * available to read. In this case we repeat the process. 835302713Ssephe * 836302713Ssephe * NOTE: Interrupt has been disabled in the ISR. 837302710Ssephe */ 838302713Ssephe for (;;) { 839302713Ssephe uint32_t left; 840294886Ssephe 841302874Ssephe cb(cbarg); 842294886Ssephe 843302713Ssephe left = hv_ring_buffer_read_end(&chan->inbound); 844302713Ssephe if (left == 0) { 845302713Ssephe /* No more data in RX bufring; done */ 846302713Ssephe break; 847302713Ssephe } 848302713Ssephe hv_ring_buffer_read_begin(&chan->inbound); 849302713Ssephe } 850294886Ssephe} 851302692Ssephe 852302713Ssephestatic void 853302713Ssephevmbus_chan_task_nobatch(void *xchan, int pending __unused) 854302713Ssephe{ 855302713Ssephe struct hv_vmbus_channel *chan = xchan; 856302713Ssephe 857302874Ssephe chan->ch_cb(chan->ch_cbarg); 858302713Ssephe} 859302713Ssephe 860302692Ssephestatic __inline void 861302692Ssephevmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags, 862302692Ssephe int flag_cnt) 863302692Ssephe{ 864302692Ssephe int f; 865302692Ssephe 866302692Ssephe for (f = 0; f < flag_cnt; ++f) { 867302806Ssephe uint32_t chid_base; 868302692Ssephe u_long flags; 869302806Ssephe int chid_ofs; 870302692Ssephe 871302692Ssephe if (event_flags[f] == 0) 872302692Ssephe continue; 873302692Ssephe 874302692Ssephe flags = atomic_swap_long(&event_flags[f], 0); 875302806Ssephe chid_base = f << VMBUS_EVTFLAG_SHIFT; 876302692Ssephe 877302806Ssephe while ((chid_ofs = ffsl(flags)) != 0) { 878302692Ssephe struct hv_vmbus_channel *channel; 879302692Ssephe 880302806Ssephe --chid_ofs; /* NOTE: ffsl is 1-based */ 881302806Ssephe flags &= ~(1UL << chid_ofs); 882302692Ssephe 883302806Ssephe channel = sc->vmbus_chmap[chid_base + chid_ofs]; 884302692Ssephe 885302692Ssephe /* if channel is closed or closing */ 886302874Ssephe if (channel == NULL || channel->ch_tq == NULL) 887302692Ssephe continue; 888302692Ssephe 889302709Ssephe if (channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) 890302692Ssephe hv_ring_buffer_read_begin(&channel->inbound); 891302874Ssephe taskqueue_enqueue(channel->ch_tq, &channel->ch_task); 892302692Ssephe } 893302692Ssephe } 894302692Ssephe} 895302692Ssephe 896302692Ssephevoid 897302692Ssephevmbus_event_proc(struct vmbus_softc *sc, int cpu) 898302692Ssephe{ 899302692Ssephe struct vmbus_evtflags *eventf; 900302692Ssephe 901302692Ssephe /* 902302692Ssephe * On Host with Win8 or above, the event page can be checked directly 903302692Ssephe * to get the id of the channel that has the pending interrupt. 904302692Ssephe */ 905302692Ssephe eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 906302692Ssephe vmbus_event_flags_proc(sc, eventf->evt_flags, 907302692Ssephe VMBUS_PCPU_GET(sc, event_flags_cnt, cpu)); 908302692Ssephe} 909302692Ssephe 910302692Ssephevoid 911302692Ssephevmbus_event_proc_compat(struct vmbus_softc *sc, int cpu) 912302692Ssephe{ 913302692Ssephe struct vmbus_evtflags *eventf; 914302692Ssephe 915302692Ssephe eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 916302692Ssephe if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) { 917302692Ssephe vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags, 918302692Ssephe VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT); 919302692Ssephe } 920302692Ssephe} 921302692Ssephe 922302692Ssephestatic void 923302692Ssephevmbus_chan_update_evtflagcnt(struct vmbus_softc *sc, 924302692Ssephe const struct hv_vmbus_channel *chan) 925302692Ssephe{ 926302692Ssephe volatile int *flag_cnt_ptr; 927302692Ssephe int flag_cnt; 928302692Ssephe 929302693Ssephe flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1; 930302873Ssephe flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid); 931302692Ssephe 932302692Ssephe for (;;) { 933302692Ssephe int old_flag_cnt; 934302692Ssephe 935302692Ssephe old_flag_cnt = *flag_cnt_ptr; 936302692Ssephe if (old_flag_cnt >= flag_cnt) 937302692Ssephe break; 938302692Ssephe if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) { 939302692Ssephe if (bootverbose) { 940302692Ssephe device_printf(sc->vmbus_dev, 941302692Ssephe "channel%u update cpu%d flag_cnt to %d\n", 942302873Ssephe chan->ch_id, chan->ch_cpuid, flag_cnt); 943302692Ssephe } 944302692Ssephe break; 945302692Ssephe } 946302692Ssephe } 947302692Ssephe} 948302864Ssephe 949302864Ssephestatic struct hv_vmbus_channel * 950302864Ssephevmbus_chan_alloc(struct vmbus_softc *sc) 951302864Ssephe{ 952302864Ssephe struct hv_vmbus_channel *chan; 953302864Ssephe 954302864Ssephe chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO); 955302864Ssephe 956302864Ssephe chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 957302864Ssephe HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param), 958302864Ssephe &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 959302864Ssephe if (chan->ch_monprm == NULL) { 960302864Ssephe device_printf(sc->vmbus_dev, "monprm alloc failed\n"); 961302864Ssephe free(chan, M_DEVBUF); 962302864Ssephe return NULL; 963302864Ssephe } 964302864Ssephe 965302864Ssephe chan->vmbus_sc = sc; 966302864Ssephe mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF); 967302864Ssephe TAILQ_INIT(&chan->ch_subchans); 968302864Ssephe TASK_INIT(&chan->ch_detach_task, 0, vmbus_chan_detach_task, chan); 969302864Ssephe 970302864Ssephe return chan; 971302864Ssephe} 972302864Ssephe 973302864Ssephestatic void 974302864Ssephevmbus_chan_free(struct hv_vmbus_channel *chan) 975302864Ssephe{ 976302864Ssephe /* TODO: assert sub-channel list is empty */ 977302864Ssephe /* TODO: asset no longer on the primary channel's sub-channel list */ 978302864Ssephe /* TODO: asset no longer on the vmbus channel list */ 979302864Ssephe hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm); 980302864Ssephe mtx_destroy(&chan->ch_subchan_lock); 981302864Ssephe free(chan, M_DEVBUF); 982302864Ssephe} 983302864Ssephe 984302864Ssephestatic int 985302864Ssephevmbus_chan_add(struct hv_vmbus_channel *newchan) 986302864Ssephe{ 987302864Ssephe struct vmbus_softc *sc = newchan->vmbus_sc; 988302864Ssephe struct hv_vmbus_channel *prichan; 989302864Ssephe 990302864Ssephe if (newchan->ch_id == 0) { 991302864Ssephe /* 992302864Ssephe * XXX 993302864Ssephe * Chan0 will neither be processed nor should be offered; 994302864Ssephe * skip it. 995302864Ssephe */ 996302864Ssephe device_printf(sc->vmbus_dev, "got chan0 offer, discard\n"); 997302864Ssephe return EINVAL; 998302864Ssephe } else if (newchan->ch_id >= VMBUS_CHAN_MAX) { 999302864Ssephe device_printf(sc->vmbus_dev, "invalid chan%u offer\n", 1000302864Ssephe newchan->ch_id); 1001302864Ssephe return EINVAL; 1002302864Ssephe } 1003302864Ssephe sc->vmbus_chmap[newchan->ch_id] = newchan; 1004302864Ssephe 1005302864Ssephe if (bootverbose) { 1006302864Ssephe device_printf(sc->vmbus_dev, "chan%u subidx%u offer\n", 1007302864Ssephe newchan->ch_id, newchan->ch_subidx); 1008302864Ssephe } 1009302864Ssephe 1010302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1011302864Ssephe TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) { 1012302864Ssephe /* 1013302864Ssephe * Sub-channel will have the same type GUID and instance 1014302864Ssephe * GUID as its primary channel. 1015302864Ssephe */ 1016302864Ssephe if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type, 1017302864Ssephe sizeof(struct hyperv_guid)) == 0 && 1018302864Ssephe memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst, 1019302864Ssephe sizeof(struct hyperv_guid)) == 0) 1020302864Ssephe break; 1021302864Ssephe } 1022302864Ssephe if (VMBUS_CHAN_ISPRIMARY(newchan)) { 1023302864Ssephe if (prichan == NULL) { 1024302864Ssephe /* Install the new primary channel */ 1025302864Ssephe TAILQ_INSERT_TAIL(&sc->vmbus_prichans, newchan, 1026302864Ssephe ch_prilink); 1027302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1028302864Ssephe return 0; 1029302864Ssephe } else { 1030302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1031302864Ssephe device_printf(sc->vmbus_dev, "duplicated primary " 1032302864Ssephe "chan%u\n", newchan->ch_id); 1033302864Ssephe return EINVAL; 1034302864Ssephe } 1035302864Ssephe } else { /* Sub-channel */ 1036302864Ssephe if (prichan == NULL) { 1037302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1038302864Ssephe device_printf(sc->vmbus_dev, "no primary chan for " 1039302864Ssephe "chan%u\n", newchan->ch_id); 1040302864Ssephe return EINVAL; 1041302864Ssephe } 1042302864Ssephe /* 1043302864Ssephe * Found the primary channel for this sub-channel and 1044302864Ssephe * move on. 1045302864Ssephe * 1046302864Ssephe * XXX refcnt prichan 1047302864Ssephe */ 1048302864Ssephe } 1049302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1050302864Ssephe 1051302864Ssephe /* 1052302864Ssephe * This is a sub-channel; link it with the primary channel. 1053302864Ssephe */ 1054302864Ssephe KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan), 1055302864Ssephe ("new channel is not sub-channel")); 1056302864Ssephe KASSERT(prichan != NULL, ("no primary channel")); 1057302864Ssephe 1058302864Ssephe newchan->ch_prichan = prichan; 1059302864Ssephe newchan->ch_dev = prichan->ch_dev; 1060302864Ssephe 1061302864Ssephe mtx_lock(&prichan->ch_subchan_lock); 1062302864Ssephe TAILQ_INSERT_TAIL(&prichan->ch_subchans, newchan, ch_sublink); 1063302864Ssephe /* 1064302864Ssephe * Bump up sub-channel count and notify anyone that is 1065302864Ssephe * interested in this sub-channel, after this sub-channel 1066302864Ssephe * is setup. 1067302864Ssephe */ 1068302864Ssephe prichan->ch_subchan_cnt++; 1069302864Ssephe mtx_unlock(&prichan->ch_subchan_lock); 1070302864Ssephe wakeup(prichan); 1071302864Ssephe 1072302864Ssephe return 0; 1073302864Ssephe} 1074302864Ssephe 1075302864Ssephevoid 1076302864Ssephevmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu) 1077302864Ssephe{ 1078302864Ssephe KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu)); 1079302864Ssephe 1080302864Ssephe if (chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WS2008 || 1081302864Ssephe chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WIN7) { 1082302864Ssephe /* Only cpu0 is supported */ 1083302864Ssephe cpu = 0; 1084302864Ssephe } 1085302864Ssephe 1086302873Ssephe chan->ch_cpuid = cpu; 1087302873Ssephe chan->ch_vcpuid = VMBUS_PCPU_GET(chan->vmbus_sc, vcpuid, cpu); 1088302864Ssephe 1089302864Ssephe if (bootverbose) { 1090302864Ssephe printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n", 1091302873Ssephe chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid); 1092302864Ssephe } 1093302864Ssephe} 1094302864Ssephe 1095302864Ssephevoid 1096302864Ssephevmbus_channel_cpu_rr(struct hv_vmbus_channel *chan) 1097302864Ssephe{ 1098302864Ssephe static uint32_t vmbus_chan_nextcpu; 1099302864Ssephe int cpu; 1100302864Ssephe 1101302864Ssephe cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus; 1102302864Ssephe vmbus_channel_cpu_set(chan, cpu); 1103302864Ssephe} 1104302864Ssephe 1105302864Ssephestatic void 1106302864Ssephevmbus_chan_cpu_default(struct hv_vmbus_channel *chan) 1107302864Ssephe{ 1108302864Ssephe /* 1109302864Ssephe * By default, pin the channel to cpu0. Devices having 1110302864Ssephe * special channel-cpu mapping requirement should call 1111302864Ssephe * vmbus_channel_cpu_{set,rr}(). 1112302864Ssephe */ 1113302864Ssephe vmbus_channel_cpu_set(chan, 0); 1114302864Ssephe} 1115302864Ssephe 1116302864Ssephestatic void 1117302864Ssephevmbus_chan_msgproc_choffer(struct vmbus_softc *sc, 1118302864Ssephe const struct vmbus_message *msg) 1119302864Ssephe{ 1120302864Ssephe const struct vmbus_chanmsg_choffer *offer; 1121302864Ssephe struct hv_vmbus_channel *chan; 1122302864Ssephe int error; 1123302864Ssephe 1124302864Ssephe offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data; 1125302864Ssephe 1126302864Ssephe chan = vmbus_chan_alloc(sc); 1127302864Ssephe if (chan == NULL) { 1128302864Ssephe device_printf(sc->vmbus_dev, "allocate chan%u failed\n", 1129302864Ssephe offer->chm_chanid); 1130302864Ssephe return; 1131302864Ssephe } 1132302864Ssephe 1133302864Ssephe chan->ch_id = offer->chm_chanid; 1134302864Ssephe chan->ch_subidx = offer->chm_subidx; 1135302864Ssephe chan->ch_guid_type = offer->chm_chtype; 1136302864Ssephe chan->ch_guid_inst = offer->chm_chinst; 1137302864Ssephe 1138302864Ssephe /* Batch reading is on by default */ 1139302864Ssephe chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD; 1140302864Ssephe 1141302864Ssephe chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT; 1142302864Ssephe if (sc->vmbus_version != VMBUS_VERSION_WS2008) 1143302864Ssephe chan->ch_monprm->mp_connid = offer->chm_connid; 1144302864Ssephe 1145302864Ssephe if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) { 1146302864Ssephe /* 1147302864Ssephe * Setup MNF stuffs. 1148302864Ssephe */ 1149302864Ssephe chan->ch_flags |= VMBUS_CHAN_FLAG_HASMNF; 1150302864Ssephe chan->ch_montrig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN; 1151302864Ssephe if (chan->ch_montrig_idx >= VMBUS_MONTRIGS_MAX) 1152302864Ssephe panic("invalid monitor trigger %u", offer->chm_montrig); 1153302864Ssephe chan->ch_montrig_mask = 1154302864Ssephe 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN); 1155302864Ssephe } 1156302864Ssephe 1157302864Ssephe /* Select default cpu for this channel. */ 1158302864Ssephe vmbus_chan_cpu_default(chan); 1159302864Ssephe 1160302864Ssephe error = vmbus_chan_add(chan); 1161302864Ssephe if (error) { 1162302864Ssephe device_printf(sc->vmbus_dev, "add chan%u failed: %d\n", 1163302864Ssephe chan->ch_id, error); 1164302864Ssephe vmbus_chan_free(chan); 1165302864Ssephe return; 1166302864Ssephe } 1167302864Ssephe 1168302864Ssephe if (VMBUS_CHAN_ISPRIMARY(chan)) { 1169302864Ssephe /* 1170302864Ssephe * Add device for this primary channel. 1171302864Ssephe * 1172302864Ssephe * NOTE: 1173302864Ssephe * Error is ignored here; don't have much to do if error 1174302864Ssephe * really happens. 1175302864Ssephe */ 1176302868Ssephe vmbus_add_child(chan); 1177302864Ssephe } 1178302864Ssephe} 1179302864Ssephe 1180302864Ssephe/* 1181302864Ssephe * XXX pretty broken; need rework. 1182302864Ssephe */ 1183302864Ssephestatic void 1184302864Ssephevmbus_chan_msgproc_chrescind(struct vmbus_softc *sc, 1185302864Ssephe const struct vmbus_message *msg) 1186302864Ssephe{ 1187302864Ssephe const struct vmbus_chanmsg_chrescind *note; 1188302864Ssephe struct hv_vmbus_channel *chan; 1189302864Ssephe 1190302864Ssephe note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data; 1191302864Ssephe if (note->chm_chanid > VMBUS_CHAN_MAX) { 1192302864Ssephe device_printf(sc->vmbus_dev, "invalid rescinded chan%u\n", 1193302864Ssephe note->chm_chanid); 1194302864Ssephe return; 1195302864Ssephe } 1196302864Ssephe 1197302864Ssephe if (bootverbose) { 1198302864Ssephe device_printf(sc->vmbus_dev, "chan%u rescinded\n", 1199302864Ssephe note->chm_chanid); 1200302864Ssephe } 1201302864Ssephe 1202302864Ssephe chan = sc->vmbus_chmap[note->chm_chanid]; 1203302864Ssephe if (chan == NULL) 1204302864Ssephe return; 1205302864Ssephe sc->vmbus_chmap[note->chm_chanid] = NULL; 1206302864Ssephe 1207302864Ssephe taskqueue_enqueue(taskqueue_thread, &chan->ch_detach_task); 1208302864Ssephe} 1209302864Ssephe 1210302864Ssephestatic void 1211302864Ssephevmbus_chan_detach_task(void *xchan, int pending __unused) 1212302864Ssephe{ 1213302864Ssephe struct hv_vmbus_channel *chan = xchan; 1214302864Ssephe 1215302864Ssephe if (VMBUS_CHAN_ISPRIMARY(chan)) { 1216302864Ssephe /* Only primary channel owns the device */ 1217302868Ssephe vmbus_delete_child(chan); 1218302864Ssephe /* NOTE: DO NOT free primary channel for now */ 1219302864Ssephe } else { 1220302864Ssephe struct vmbus_softc *sc = chan->vmbus_sc; 1221302864Ssephe struct hv_vmbus_channel *pri_chan = chan->ch_prichan; 1222302864Ssephe struct vmbus_chanmsg_chfree *req; 1223302864Ssephe struct vmbus_msghc *mh; 1224302864Ssephe int error; 1225302864Ssephe 1226302864Ssephe mh = vmbus_msghc_get(sc, sizeof(*req)); 1227302864Ssephe if (mh == NULL) { 1228302864Ssephe device_printf(sc->vmbus_dev, 1229302864Ssephe "can not get msg hypercall for chfree(chan%u)\n", 1230302864Ssephe chan->ch_id); 1231302864Ssephe goto remove; 1232302864Ssephe } 1233302864Ssephe 1234302864Ssephe req = vmbus_msghc_dataptr(mh); 1235302864Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE; 1236302864Ssephe req->chm_chanid = chan->ch_id; 1237302864Ssephe 1238302864Ssephe error = vmbus_msghc_exec_noresult(mh); 1239302864Ssephe vmbus_msghc_put(sc, mh); 1240302864Ssephe 1241302864Ssephe if (error) { 1242302864Ssephe device_printf(sc->vmbus_dev, 1243302864Ssephe "chfree(chan%u) failed: %d", 1244302864Ssephe chan->ch_id, error); 1245302864Ssephe /* NOTE: Move on! */ 1246302864Ssephe } else { 1247302864Ssephe if (bootverbose) { 1248302864Ssephe device_printf(sc->vmbus_dev, "chan%u freed\n", 1249302864Ssephe chan->ch_id); 1250302864Ssephe } 1251302864Ssephe } 1252302864Ssepheremove: 1253302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1254302864Ssephe TAILQ_REMOVE(&pri_chan->ch_subchans, chan, ch_sublink); 1255302864Ssephe KASSERT(pri_chan->ch_subchan_cnt > 0, 1256302864Ssephe ("invalid subchan_cnt %d", pri_chan->ch_subchan_cnt)); 1257302864Ssephe pri_chan->ch_subchan_cnt--; 1258302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1259302864Ssephe wakeup(pri_chan); 1260302864Ssephe 1261302864Ssephe vmbus_chan_free(chan); 1262302864Ssephe } 1263302864Ssephe} 1264302864Ssephe 1265302864Ssephe/* 1266302864Ssephe * Detach all devices and destroy the corresponding primary channels. 1267302864Ssephe */ 1268302864Ssephevoid 1269302864Ssephevmbus_chan_destroy_all(struct vmbus_softc *sc) 1270302864Ssephe{ 1271302864Ssephe struct hv_vmbus_channel *chan; 1272302864Ssephe 1273302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1274302864Ssephe while ((chan = TAILQ_FIRST(&sc->vmbus_prichans)) != NULL) { 1275302864Ssephe KASSERT(VMBUS_CHAN_ISPRIMARY(chan), ("not primary channel")); 1276302864Ssephe TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink); 1277302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1278302864Ssephe 1279302868Ssephe vmbus_delete_child(chan); 1280302864Ssephe vmbus_chan_free(chan); 1281302864Ssephe 1282302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1283302864Ssephe } 1284302864Ssephe bzero(sc->vmbus_chmap, 1285302864Ssephe sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX); 1286302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1287302864Ssephe} 1288302864Ssephe 1289302864Ssephe/** 1290302864Ssephe * @brief Select the best outgoing channel 1291302864Ssephe * 1292302864Ssephe * The channel whose vcpu binding is closest to the currect vcpu will 1293302864Ssephe * be selected. 1294302864Ssephe * If no multi-channel, always select primary channel 1295302864Ssephe * 1296302864Ssephe * @param primary - primary channel 1297302864Ssephe */ 1298302864Ssephestruct hv_vmbus_channel * 1299302864Ssephevmbus_select_outgoing_channel(struct hv_vmbus_channel *primary) 1300302864Ssephe{ 1301302864Ssephe hv_vmbus_channel *new_channel = NULL; 1302302864Ssephe hv_vmbus_channel *outgoing_channel = primary; 1303302864Ssephe int old_cpu_distance = 0; 1304302864Ssephe int new_cpu_distance = 0; 1305302864Ssephe int cur_vcpu = 0; 1306302864Ssephe int smp_pro_id = PCPU_GET(cpuid); 1307302864Ssephe 1308302864Ssephe if (TAILQ_EMPTY(&primary->ch_subchans)) { 1309302864Ssephe return outgoing_channel; 1310302864Ssephe } 1311302864Ssephe 1312302864Ssephe if (smp_pro_id >= MAXCPU) { 1313302864Ssephe return outgoing_channel; 1314302864Ssephe } 1315302864Ssephe 1316302864Ssephe cur_vcpu = VMBUS_PCPU_GET(primary->vmbus_sc, vcpuid, smp_pro_id); 1317302864Ssephe 1318302864Ssephe /* XXX need lock */ 1319302864Ssephe TAILQ_FOREACH(new_channel, &primary->ch_subchans, ch_sublink) { 1320302864Ssephe if ((new_channel->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0) { 1321302864Ssephe continue; 1322302864Ssephe } 1323302864Ssephe 1324302873Ssephe if (new_channel->ch_vcpuid == cur_vcpu){ 1325302864Ssephe return new_channel; 1326302864Ssephe } 1327302864Ssephe 1328302873Ssephe old_cpu_distance = ((outgoing_channel->ch_vcpuid > cur_vcpu) ? 1329302873Ssephe (outgoing_channel->ch_vcpuid - cur_vcpu) : 1330302873Ssephe (cur_vcpu - outgoing_channel->ch_vcpuid)); 1331302864Ssephe 1332302873Ssephe new_cpu_distance = ((new_channel->ch_vcpuid > cur_vcpu) ? 1333302873Ssephe (new_channel->ch_vcpuid - cur_vcpu) : 1334302873Ssephe (cur_vcpu - new_channel->ch_vcpuid)); 1335302864Ssephe 1336302864Ssephe if (old_cpu_distance < new_cpu_distance) { 1337302864Ssephe continue; 1338302864Ssephe } 1339302864Ssephe 1340302864Ssephe outgoing_channel = new_channel; 1341302864Ssephe } 1342302864Ssephe 1343302864Ssephe return(outgoing_channel); 1344302864Ssephe} 1345302864Ssephe 1346302864Ssephestruct hv_vmbus_channel ** 1347302864Ssephevmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt) 1348302864Ssephe{ 1349302864Ssephe struct hv_vmbus_channel **ret, *chan; 1350302864Ssephe int i; 1351302864Ssephe 1352302864Ssephe ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP, 1353302864Ssephe M_WAITOK); 1354302864Ssephe 1355302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1356302864Ssephe 1357302864Ssephe while (pri_chan->ch_subchan_cnt < subchan_cnt) 1358302864Ssephe mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0); 1359302864Ssephe 1360302864Ssephe i = 0; 1361302864Ssephe TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) { 1362302864Ssephe /* TODO: refcnt chan */ 1363302864Ssephe ret[i] = chan; 1364302864Ssephe 1365302864Ssephe ++i; 1366302864Ssephe if (i == subchan_cnt) 1367302864Ssephe break; 1368302864Ssephe } 1369302864Ssephe KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d", 1370302864Ssephe pri_chan->ch_subchan_cnt, subchan_cnt)); 1371302864Ssephe 1372302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1373302864Ssephe 1374302864Ssephe return ret; 1375302864Ssephe} 1376302864Ssephe 1377302864Ssephevoid 1378302864Ssephevmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused) 1379302864Ssephe{ 1380302864Ssephe 1381302864Ssephe free(subchan, M_TEMP); 1382302864Ssephe} 1383302864Ssephe 1384302864Ssephevoid 1385302864Ssephevmbus_drain_subchan(struct hv_vmbus_channel *pri_chan) 1386302864Ssephe{ 1387302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1388302864Ssephe while (pri_chan->ch_subchan_cnt > 0) 1389302864Ssephe mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0); 1390302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1391302864Ssephe} 1392302864Ssephe 1393302864Ssephevoid 1394302864Ssephevmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg) 1395302864Ssephe{ 1396302864Ssephe vmbus_chanmsg_proc_t msg_proc; 1397302864Ssephe uint32_t msg_type; 1398302864Ssephe 1399302864Ssephe msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type; 1400302864Ssephe KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX, 1401302864Ssephe ("invalid message type %u", msg_type)); 1402302864Ssephe 1403302864Ssephe msg_proc = vmbus_chan_msgprocs[msg_type]; 1404302864Ssephe if (msg_proc != NULL) 1405302864Ssephe msg_proc(sc, msg); 1406302864Ssephe} 1407