vmbus_chan.c revision 302878
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 302878 2016-07-15 06:40:59Z 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 624250199Sgrehan/** 625250199Sgrehan * @brief Send the specified buffer on the given channel 626250199Sgrehan */ 627250199Sgrehanint 628250199Sgrehanhv_vmbus_channel_send_packet( 629250199Sgrehan hv_vmbus_channel* channel, 630250199Sgrehan void* buffer, 631250199Sgrehan uint32_t buffer_len, 632250199Sgrehan uint64_t request_id, 633250199Sgrehan hv_vmbus_packet_type type, 634250199Sgrehan uint32_t flags) 635250199Sgrehan{ 636250199Sgrehan int ret = 0; 637302875Ssephe struct vmbus_chanpkt pkt; 638250199Sgrehan uint32_t packet_len; 639250199Sgrehan uint64_t aligned_data; 640250199Sgrehan uint32_t packet_len_aligned; 641282212Swhu boolean_t need_sig; 642302870Ssephe struct iovec iov[3]; 643250199Sgrehan 644302875Ssephe packet_len = sizeof(pkt) + buffer_len; 645302875Ssephe packet_len_aligned = roundup2(packet_len, VMBUS_CHANPKT_SIZE_ALIGN); 646250199Sgrehan aligned_data = 0; 647250199Sgrehan 648302875Ssephe /* 649302875Ssephe * Setup channel packet. 650302875Ssephe */ 651302875Ssephe pkt.cp_hdr.cph_type = type; 652302875Ssephe pkt.cp_hdr.cph_flags = flags; 653302875Ssephe pkt.cp_hdr.cph_data_ofs = sizeof(pkt) >> VMBUS_CHANPKT_SIZE_SHIFT; 654302875Ssephe pkt.cp_hdr.cph_len = packet_len_aligned >> VMBUS_CHANPKT_SIZE_SHIFT; 655302875Ssephe pkt.cp_hdr.cph_xactid = request_id; 656250199Sgrehan 657302875Ssephe iov[0].iov_base = &pkt; 658302875Ssephe iov[0].iov_len = sizeof(pkt); 659250199Sgrehan 660302870Ssephe iov[1].iov_base = buffer; 661302870Ssephe iov[1].iov_len = buffer_len; 662250199Sgrehan 663302870Ssephe iov[2].iov_base = &aligned_data; 664302870Ssephe iov[2].iov_len = packet_len_aligned - packet_len; 665250199Sgrehan 666302870Ssephe ret = hv_ring_buffer_write(&channel->outbound, iov, 3, &need_sig); 667250199Sgrehan 668250199Sgrehan /* TODO: We should determine if this is optional */ 669302731Ssephe if (ret == 0 && need_sig) 670302731Ssephe vmbus_chan_send_event(channel); 671250199Sgrehan 672250199Sgrehan return (ret); 673250199Sgrehan} 674250199Sgrehan 675250199Sgrehanint 676302876Ssephevmbus_chan_send_sglist(struct hv_vmbus_channel *chan, 677302876Ssephe struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid) 678250199Sgrehan{ 679302876Ssephe struct vmbus_chanpkt_sglist pkt; 680302876Ssephe int pktlen, pad_pktlen, hlen, error; 681302876Ssephe struct iovec iov[4]; 682302876Ssephe boolean_t send_evt; 683302876Ssephe uint64_t pad = 0; 684250199Sgrehan 685302876Ssephe KASSERT(sglen < VMBUS_CHAN_SGLIST_MAX, 686302876Ssephe ("invalid sglist len %d", sglen)); 687250199Sgrehan 688302876Ssephe hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]); 689302876Ssephe pktlen = hlen + dlen; 690302876Ssephe pad_pktlen = roundup2(pktlen, VMBUS_CHANPKT_SIZE_ALIGN); 691250199Sgrehan 692302876Ssephe pkt.cp_hdr.cph_type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 693302876Ssephe pkt.cp_hdr.cph_flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 694302876Ssephe pkt.cp_hdr.cph_data_ofs = hlen >> VMBUS_CHANPKT_SIZE_SHIFT; 695302876Ssephe pkt.cp_hdr.cph_len = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT; 696302876Ssephe pkt.cp_hdr.cph_xactid = xactid; 697302876Ssephe pkt.cp_rsvd = 0; 698302876Ssephe pkt.cp_gpa_cnt = sglen; 699250199Sgrehan 700302876Ssephe iov[0].iov_base = &pkt; 701302876Ssephe iov[0].iov_len = sizeof(pkt); 702302876Ssephe iov[1].iov_base = sg; 703302876Ssephe iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen; 704302876Ssephe iov[2].iov_base = data; 705302876Ssephe iov[2].iov_len = dlen; 706302876Ssephe iov[3].iov_base = &pad; 707302876Ssephe iov[3].iov_len = pad_pktlen - pktlen; 708250199Sgrehan 709302876Ssephe error = hv_ring_buffer_write(&chan->outbound, iov, 4, &send_evt); 710302876Ssephe if (!error && send_evt) 711302876Ssephe vmbus_chan_send_event(chan); 712302876Ssephe return error; 713250199Sgrehan} 714250199Sgrehan 715250199Sgrehanint 716302878Ssephevmbus_chan_send_prplist(struct hv_vmbus_channel *chan, 717302878Ssephe struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen, 718302878Ssephe uint64_t xactid) 719250199Sgrehan{ 720302878Ssephe struct vmbus_chanpkt_prplist pkt; 721302878Ssephe int pktlen, pad_pktlen, hlen, error; 722302878Ssephe struct iovec iov[4]; 723302878Ssephe boolean_t send_evt; 724302878Ssephe uint64_t pad = 0; 725250199Sgrehan 726302878Ssephe KASSERT(prp_cnt < VMBUS_CHAN_PRPLIST_MAX, 727302878Ssephe ("invalid prplist entry count %d", prp_cnt)); 728250199Sgrehan 729302878Ssephe hlen = __offsetof(struct vmbus_chanpkt_prplist, 730302878Ssephe cp_range[0].gpa_page[prp_cnt]); 731302878Ssephe pktlen = hlen + dlen; 732302878Ssephe pad_pktlen = roundup2(pktlen, VMBUS_CHANPKT_SIZE_ALIGN); 733250199Sgrehan 734302878Ssephe pkt.cp_hdr.cph_type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 735302878Ssephe pkt.cp_hdr.cph_flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 736302878Ssephe pkt.cp_hdr.cph_data_ofs = hlen >> VMBUS_CHANPKT_SIZE_SHIFT; 737302878Ssephe pkt.cp_hdr.cph_len = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT; 738302878Ssephe pkt.cp_hdr.cph_xactid = xactid; 739302878Ssephe pkt.cp_rsvd = 0; 740302878Ssephe pkt.cp_range_cnt = 1; 741250199Sgrehan 742302878Ssephe iov[0].iov_base = &pkt; 743302878Ssephe iov[0].iov_len = sizeof(pkt); 744302878Ssephe iov[1].iov_base = prp; 745302878Ssephe iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]); 746302878Ssephe iov[2].iov_base = data; 747302878Ssephe iov[2].iov_len = dlen; 748302878Ssephe iov[3].iov_base = &pad; 749302878Ssephe iov[3].iov_len = pad_pktlen - pktlen; 750250199Sgrehan 751302878Ssephe error = hv_ring_buffer_write(&chan->outbound, iov, 4, &send_evt); 752302878Ssephe if (!error && send_evt) 753302878Ssephe vmbus_chan_send_event(chan); 754302878Ssephe return error; 755250199Sgrehan} 756250199Sgrehan 757250199Sgrehan/** 758250199Sgrehan * @brief Retrieve the user packet on the specified channel 759250199Sgrehan */ 760250199Sgrehanint 761250199Sgrehanhv_vmbus_channel_recv_packet( 762250199Sgrehan hv_vmbus_channel* channel, 763250199Sgrehan void* Buffer, 764250199Sgrehan uint32_t buffer_len, 765250199Sgrehan uint32_t* buffer_actual_len, 766250199Sgrehan uint64_t* request_id) 767250199Sgrehan{ 768250199Sgrehan int ret; 769250199Sgrehan uint32_t user_len; 770250199Sgrehan uint32_t packet_len; 771250199Sgrehan hv_vm_packet_descriptor desc; 772250199Sgrehan 773250199Sgrehan *buffer_actual_len = 0; 774250199Sgrehan *request_id = 0; 775250199Sgrehan 776250199Sgrehan ret = hv_ring_buffer_peek(&channel->inbound, &desc, 777250199Sgrehan sizeof(hv_vm_packet_descriptor)); 778250199Sgrehan if (ret != 0) 779250199Sgrehan return (0); 780250199Sgrehan 781250199Sgrehan packet_len = desc.length8 << 3; 782250199Sgrehan user_len = packet_len - (desc.data_offset8 << 3); 783250199Sgrehan 784250199Sgrehan *buffer_actual_len = user_len; 785250199Sgrehan 786250199Sgrehan if (user_len > buffer_len) 787250199Sgrehan return (EINVAL); 788250199Sgrehan 789250199Sgrehan *request_id = desc.transaction_id; 790250199Sgrehan 791250199Sgrehan /* Copy over the packet to the user buffer */ 792250199Sgrehan ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len, 793250199Sgrehan (desc.data_offset8 << 3)); 794250199Sgrehan 795250199Sgrehan return (0); 796250199Sgrehan} 797250199Sgrehan 798250199Sgrehan/** 799250199Sgrehan * @brief Retrieve the raw packet on the specified channel 800250199Sgrehan */ 801250199Sgrehanint 802250199Sgrehanhv_vmbus_channel_recv_packet_raw( 803250199Sgrehan hv_vmbus_channel* channel, 804250199Sgrehan void* buffer, 805250199Sgrehan uint32_t buffer_len, 806250199Sgrehan uint32_t* buffer_actual_len, 807250199Sgrehan uint64_t* request_id) 808250199Sgrehan{ 809250199Sgrehan int ret; 810250199Sgrehan uint32_t packetLen; 811250199Sgrehan hv_vm_packet_descriptor desc; 812250199Sgrehan 813250199Sgrehan *buffer_actual_len = 0; 814250199Sgrehan *request_id = 0; 815250199Sgrehan 816250199Sgrehan ret = hv_ring_buffer_peek( 817250199Sgrehan &channel->inbound, &desc, 818250199Sgrehan sizeof(hv_vm_packet_descriptor)); 819250199Sgrehan 820250199Sgrehan if (ret != 0) 821250199Sgrehan return (0); 822250199Sgrehan 823250199Sgrehan packetLen = desc.length8 << 3; 824250199Sgrehan *buffer_actual_len = packetLen; 825250199Sgrehan 826250199Sgrehan if (packetLen > buffer_len) 827250199Sgrehan return (ENOBUFS); 828250199Sgrehan 829250199Sgrehan *request_id = desc.transaction_id; 830250199Sgrehan 831250199Sgrehan /* Copy over the entire packet to the user buffer */ 832250199Sgrehan ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0); 833250199Sgrehan 834250199Sgrehan return (0); 835250199Sgrehan} 836294886Ssephe 837294886Ssephestatic void 838302713Ssephevmbus_chan_task(void *xchan, int pending __unused) 839294886Ssephe{ 840302713Ssephe struct hv_vmbus_channel *chan = xchan; 841302874Ssephe vmbus_chan_callback_t cb = chan->ch_cb; 842302874Ssephe void *cbarg = chan->ch_cbarg; 843294886Ssephe 844302710Ssephe /* 845302710Ssephe * Optimize host to guest signaling by ensuring: 846302710Ssephe * 1. While reading the channel, we disable interrupts from 847302710Ssephe * host. 848302710Ssephe * 2. Ensure that we process all posted messages from the host 849302710Ssephe * before returning from this callback. 850302710Ssephe * 3. Once we return, enable signaling from the host. Once this 851302710Ssephe * state is set we check to see if additional packets are 852302710Ssephe * available to read. In this case we repeat the process. 853302713Ssephe * 854302713Ssephe * NOTE: Interrupt has been disabled in the ISR. 855302710Ssephe */ 856302713Ssephe for (;;) { 857302713Ssephe uint32_t left; 858294886Ssephe 859302874Ssephe cb(cbarg); 860294886Ssephe 861302713Ssephe left = hv_ring_buffer_read_end(&chan->inbound); 862302713Ssephe if (left == 0) { 863302713Ssephe /* No more data in RX bufring; done */ 864302713Ssephe break; 865302713Ssephe } 866302713Ssephe hv_ring_buffer_read_begin(&chan->inbound); 867302713Ssephe } 868294886Ssephe} 869302692Ssephe 870302713Ssephestatic void 871302713Ssephevmbus_chan_task_nobatch(void *xchan, int pending __unused) 872302713Ssephe{ 873302713Ssephe struct hv_vmbus_channel *chan = xchan; 874302713Ssephe 875302874Ssephe chan->ch_cb(chan->ch_cbarg); 876302713Ssephe} 877302713Ssephe 878302692Ssephestatic __inline void 879302692Ssephevmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags, 880302692Ssephe int flag_cnt) 881302692Ssephe{ 882302692Ssephe int f; 883302692Ssephe 884302692Ssephe for (f = 0; f < flag_cnt; ++f) { 885302806Ssephe uint32_t chid_base; 886302692Ssephe u_long flags; 887302806Ssephe int chid_ofs; 888302692Ssephe 889302692Ssephe if (event_flags[f] == 0) 890302692Ssephe continue; 891302692Ssephe 892302692Ssephe flags = atomic_swap_long(&event_flags[f], 0); 893302806Ssephe chid_base = f << VMBUS_EVTFLAG_SHIFT; 894302692Ssephe 895302806Ssephe while ((chid_ofs = ffsl(flags)) != 0) { 896302692Ssephe struct hv_vmbus_channel *channel; 897302692Ssephe 898302806Ssephe --chid_ofs; /* NOTE: ffsl is 1-based */ 899302806Ssephe flags &= ~(1UL << chid_ofs); 900302692Ssephe 901302806Ssephe channel = sc->vmbus_chmap[chid_base + chid_ofs]; 902302692Ssephe 903302692Ssephe /* if channel is closed or closing */ 904302874Ssephe if (channel == NULL || channel->ch_tq == NULL) 905302692Ssephe continue; 906302692Ssephe 907302709Ssephe if (channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) 908302692Ssephe hv_ring_buffer_read_begin(&channel->inbound); 909302874Ssephe taskqueue_enqueue(channel->ch_tq, &channel->ch_task); 910302692Ssephe } 911302692Ssephe } 912302692Ssephe} 913302692Ssephe 914302692Ssephevoid 915302692Ssephevmbus_event_proc(struct vmbus_softc *sc, int cpu) 916302692Ssephe{ 917302692Ssephe struct vmbus_evtflags *eventf; 918302692Ssephe 919302692Ssephe /* 920302692Ssephe * On Host with Win8 or above, the event page can be checked directly 921302692Ssephe * to get the id of the channel that has the pending interrupt. 922302692Ssephe */ 923302692Ssephe eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 924302692Ssephe vmbus_event_flags_proc(sc, eventf->evt_flags, 925302692Ssephe VMBUS_PCPU_GET(sc, event_flags_cnt, cpu)); 926302692Ssephe} 927302692Ssephe 928302692Ssephevoid 929302692Ssephevmbus_event_proc_compat(struct vmbus_softc *sc, int cpu) 930302692Ssephe{ 931302692Ssephe struct vmbus_evtflags *eventf; 932302692Ssephe 933302692Ssephe eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 934302692Ssephe if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) { 935302692Ssephe vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags, 936302692Ssephe VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT); 937302692Ssephe } 938302692Ssephe} 939302692Ssephe 940302692Ssephestatic void 941302692Ssephevmbus_chan_update_evtflagcnt(struct vmbus_softc *sc, 942302692Ssephe const struct hv_vmbus_channel *chan) 943302692Ssephe{ 944302692Ssephe volatile int *flag_cnt_ptr; 945302692Ssephe int flag_cnt; 946302692Ssephe 947302693Ssephe flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1; 948302873Ssephe flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid); 949302692Ssephe 950302692Ssephe for (;;) { 951302692Ssephe int old_flag_cnt; 952302692Ssephe 953302692Ssephe old_flag_cnt = *flag_cnt_ptr; 954302692Ssephe if (old_flag_cnt >= flag_cnt) 955302692Ssephe break; 956302692Ssephe if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) { 957302692Ssephe if (bootverbose) { 958302692Ssephe device_printf(sc->vmbus_dev, 959302692Ssephe "channel%u update cpu%d flag_cnt to %d\n", 960302873Ssephe chan->ch_id, chan->ch_cpuid, flag_cnt); 961302692Ssephe } 962302692Ssephe break; 963302692Ssephe } 964302692Ssephe } 965302692Ssephe} 966302864Ssephe 967302864Ssephestatic struct hv_vmbus_channel * 968302864Ssephevmbus_chan_alloc(struct vmbus_softc *sc) 969302864Ssephe{ 970302864Ssephe struct hv_vmbus_channel *chan; 971302864Ssephe 972302864Ssephe chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO); 973302864Ssephe 974302864Ssephe chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 975302864Ssephe HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param), 976302864Ssephe &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 977302864Ssephe if (chan->ch_monprm == NULL) { 978302864Ssephe device_printf(sc->vmbus_dev, "monprm alloc failed\n"); 979302864Ssephe free(chan, M_DEVBUF); 980302864Ssephe return NULL; 981302864Ssephe } 982302864Ssephe 983302864Ssephe chan->vmbus_sc = sc; 984302864Ssephe mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF); 985302864Ssephe TAILQ_INIT(&chan->ch_subchans); 986302864Ssephe TASK_INIT(&chan->ch_detach_task, 0, vmbus_chan_detach_task, chan); 987302864Ssephe 988302864Ssephe return chan; 989302864Ssephe} 990302864Ssephe 991302864Ssephestatic void 992302864Ssephevmbus_chan_free(struct hv_vmbus_channel *chan) 993302864Ssephe{ 994302864Ssephe /* TODO: assert sub-channel list is empty */ 995302864Ssephe /* TODO: asset no longer on the primary channel's sub-channel list */ 996302864Ssephe /* TODO: asset no longer on the vmbus channel list */ 997302864Ssephe hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm); 998302864Ssephe mtx_destroy(&chan->ch_subchan_lock); 999302864Ssephe free(chan, M_DEVBUF); 1000302864Ssephe} 1001302864Ssephe 1002302864Ssephestatic int 1003302864Ssephevmbus_chan_add(struct hv_vmbus_channel *newchan) 1004302864Ssephe{ 1005302864Ssephe struct vmbus_softc *sc = newchan->vmbus_sc; 1006302864Ssephe struct hv_vmbus_channel *prichan; 1007302864Ssephe 1008302864Ssephe if (newchan->ch_id == 0) { 1009302864Ssephe /* 1010302864Ssephe * XXX 1011302864Ssephe * Chan0 will neither be processed nor should be offered; 1012302864Ssephe * skip it. 1013302864Ssephe */ 1014302864Ssephe device_printf(sc->vmbus_dev, "got chan0 offer, discard\n"); 1015302864Ssephe return EINVAL; 1016302864Ssephe } else if (newchan->ch_id >= VMBUS_CHAN_MAX) { 1017302864Ssephe device_printf(sc->vmbus_dev, "invalid chan%u offer\n", 1018302864Ssephe newchan->ch_id); 1019302864Ssephe return EINVAL; 1020302864Ssephe } 1021302864Ssephe sc->vmbus_chmap[newchan->ch_id] = newchan; 1022302864Ssephe 1023302864Ssephe if (bootverbose) { 1024302864Ssephe device_printf(sc->vmbus_dev, "chan%u subidx%u offer\n", 1025302864Ssephe newchan->ch_id, newchan->ch_subidx); 1026302864Ssephe } 1027302864Ssephe 1028302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1029302864Ssephe TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) { 1030302864Ssephe /* 1031302864Ssephe * Sub-channel will have the same type GUID and instance 1032302864Ssephe * GUID as its primary channel. 1033302864Ssephe */ 1034302864Ssephe if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type, 1035302864Ssephe sizeof(struct hyperv_guid)) == 0 && 1036302864Ssephe memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst, 1037302864Ssephe sizeof(struct hyperv_guid)) == 0) 1038302864Ssephe break; 1039302864Ssephe } 1040302864Ssephe if (VMBUS_CHAN_ISPRIMARY(newchan)) { 1041302864Ssephe if (prichan == NULL) { 1042302864Ssephe /* Install the new primary channel */ 1043302864Ssephe TAILQ_INSERT_TAIL(&sc->vmbus_prichans, newchan, 1044302864Ssephe ch_prilink); 1045302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1046302864Ssephe return 0; 1047302864Ssephe } else { 1048302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1049302864Ssephe device_printf(sc->vmbus_dev, "duplicated primary " 1050302864Ssephe "chan%u\n", newchan->ch_id); 1051302864Ssephe return EINVAL; 1052302864Ssephe } 1053302864Ssephe } else { /* Sub-channel */ 1054302864Ssephe if (prichan == NULL) { 1055302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1056302864Ssephe device_printf(sc->vmbus_dev, "no primary chan for " 1057302864Ssephe "chan%u\n", newchan->ch_id); 1058302864Ssephe return EINVAL; 1059302864Ssephe } 1060302864Ssephe /* 1061302864Ssephe * Found the primary channel for this sub-channel and 1062302864Ssephe * move on. 1063302864Ssephe * 1064302864Ssephe * XXX refcnt prichan 1065302864Ssephe */ 1066302864Ssephe } 1067302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1068302864Ssephe 1069302864Ssephe /* 1070302864Ssephe * This is a sub-channel; link it with the primary channel. 1071302864Ssephe */ 1072302864Ssephe KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan), 1073302864Ssephe ("new channel is not sub-channel")); 1074302864Ssephe KASSERT(prichan != NULL, ("no primary channel")); 1075302864Ssephe 1076302864Ssephe newchan->ch_prichan = prichan; 1077302864Ssephe newchan->ch_dev = prichan->ch_dev; 1078302864Ssephe 1079302864Ssephe mtx_lock(&prichan->ch_subchan_lock); 1080302864Ssephe TAILQ_INSERT_TAIL(&prichan->ch_subchans, newchan, ch_sublink); 1081302864Ssephe /* 1082302864Ssephe * Bump up sub-channel count and notify anyone that is 1083302864Ssephe * interested in this sub-channel, after this sub-channel 1084302864Ssephe * is setup. 1085302864Ssephe */ 1086302864Ssephe prichan->ch_subchan_cnt++; 1087302864Ssephe mtx_unlock(&prichan->ch_subchan_lock); 1088302864Ssephe wakeup(prichan); 1089302864Ssephe 1090302864Ssephe return 0; 1091302864Ssephe} 1092302864Ssephe 1093302864Ssephevoid 1094302864Ssephevmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu) 1095302864Ssephe{ 1096302864Ssephe KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu)); 1097302864Ssephe 1098302864Ssephe if (chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WS2008 || 1099302864Ssephe chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WIN7) { 1100302864Ssephe /* Only cpu0 is supported */ 1101302864Ssephe cpu = 0; 1102302864Ssephe } 1103302864Ssephe 1104302873Ssephe chan->ch_cpuid = cpu; 1105302873Ssephe chan->ch_vcpuid = VMBUS_PCPU_GET(chan->vmbus_sc, vcpuid, cpu); 1106302864Ssephe 1107302864Ssephe if (bootverbose) { 1108302864Ssephe printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n", 1109302873Ssephe chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid); 1110302864Ssephe } 1111302864Ssephe} 1112302864Ssephe 1113302864Ssephevoid 1114302864Ssephevmbus_channel_cpu_rr(struct hv_vmbus_channel *chan) 1115302864Ssephe{ 1116302864Ssephe static uint32_t vmbus_chan_nextcpu; 1117302864Ssephe int cpu; 1118302864Ssephe 1119302864Ssephe cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus; 1120302864Ssephe vmbus_channel_cpu_set(chan, cpu); 1121302864Ssephe} 1122302864Ssephe 1123302864Ssephestatic void 1124302864Ssephevmbus_chan_cpu_default(struct hv_vmbus_channel *chan) 1125302864Ssephe{ 1126302864Ssephe /* 1127302864Ssephe * By default, pin the channel to cpu0. Devices having 1128302864Ssephe * special channel-cpu mapping requirement should call 1129302864Ssephe * vmbus_channel_cpu_{set,rr}(). 1130302864Ssephe */ 1131302864Ssephe vmbus_channel_cpu_set(chan, 0); 1132302864Ssephe} 1133302864Ssephe 1134302864Ssephestatic void 1135302864Ssephevmbus_chan_msgproc_choffer(struct vmbus_softc *sc, 1136302864Ssephe const struct vmbus_message *msg) 1137302864Ssephe{ 1138302864Ssephe const struct vmbus_chanmsg_choffer *offer; 1139302864Ssephe struct hv_vmbus_channel *chan; 1140302864Ssephe int error; 1141302864Ssephe 1142302864Ssephe offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data; 1143302864Ssephe 1144302864Ssephe chan = vmbus_chan_alloc(sc); 1145302864Ssephe if (chan == NULL) { 1146302864Ssephe device_printf(sc->vmbus_dev, "allocate chan%u failed\n", 1147302864Ssephe offer->chm_chanid); 1148302864Ssephe return; 1149302864Ssephe } 1150302864Ssephe 1151302864Ssephe chan->ch_id = offer->chm_chanid; 1152302864Ssephe chan->ch_subidx = offer->chm_subidx; 1153302864Ssephe chan->ch_guid_type = offer->chm_chtype; 1154302864Ssephe chan->ch_guid_inst = offer->chm_chinst; 1155302864Ssephe 1156302864Ssephe /* Batch reading is on by default */ 1157302864Ssephe chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD; 1158302864Ssephe 1159302864Ssephe chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT; 1160302864Ssephe if (sc->vmbus_version != VMBUS_VERSION_WS2008) 1161302864Ssephe chan->ch_monprm->mp_connid = offer->chm_connid; 1162302864Ssephe 1163302864Ssephe if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) { 1164302864Ssephe /* 1165302864Ssephe * Setup MNF stuffs. 1166302864Ssephe */ 1167302864Ssephe chan->ch_flags |= VMBUS_CHAN_FLAG_HASMNF; 1168302864Ssephe chan->ch_montrig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN; 1169302864Ssephe if (chan->ch_montrig_idx >= VMBUS_MONTRIGS_MAX) 1170302864Ssephe panic("invalid monitor trigger %u", offer->chm_montrig); 1171302864Ssephe chan->ch_montrig_mask = 1172302864Ssephe 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN); 1173302864Ssephe } 1174302864Ssephe 1175302864Ssephe /* Select default cpu for this channel. */ 1176302864Ssephe vmbus_chan_cpu_default(chan); 1177302864Ssephe 1178302864Ssephe error = vmbus_chan_add(chan); 1179302864Ssephe if (error) { 1180302864Ssephe device_printf(sc->vmbus_dev, "add chan%u failed: %d\n", 1181302864Ssephe chan->ch_id, error); 1182302864Ssephe vmbus_chan_free(chan); 1183302864Ssephe return; 1184302864Ssephe } 1185302864Ssephe 1186302864Ssephe if (VMBUS_CHAN_ISPRIMARY(chan)) { 1187302864Ssephe /* 1188302864Ssephe * Add device for this primary channel. 1189302864Ssephe * 1190302864Ssephe * NOTE: 1191302864Ssephe * Error is ignored here; don't have much to do if error 1192302864Ssephe * really happens. 1193302864Ssephe */ 1194302868Ssephe vmbus_add_child(chan); 1195302864Ssephe } 1196302864Ssephe} 1197302864Ssephe 1198302864Ssephe/* 1199302864Ssephe * XXX pretty broken; need rework. 1200302864Ssephe */ 1201302864Ssephestatic void 1202302864Ssephevmbus_chan_msgproc_chrescind(struct vmbus_softc *sc, 1203302864Ssephe const struct vmbus_message *msg) 1204302864Ssephe{ 1205302864Ssephe const struct vmbus_chanmsg_chrescind *note; 1206302864Ssephe struct hv_vmbus_channel *chan; 1207302864Ssephe 1208302864Ssephe note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data; 1209302864Ssephe if (note->chm_chanid > VMBUS_CHAN_MAX) { 1210302864Ssephe device_printf(sc->vmbus_dev, "invalid rescinded chan%u\n", 1211302864Ssephe note->chm_chanid); 1212302864Ssephe return; 1213302864Ssephe } 1214302864Ssephe 1215302864Ssephe if (bootverbose) { 1216302864Ssephe device_printf(sc->vmbus_dev, "chan%u rescinded\n", 1217302864Ssephe note->chm_chanid); 1218302864Ssephe } 1219302864Ssephe 1220302864Ssephe chan = sc->vmbus_chmap[note->chm_chanid]; 1221302864Ssephe if (chan == NULL) 1222302864Ssephe return; 1223302864Ssephe sc->vmbus_chmap[note->chm_chanid] = NULL; 1224302864Ssephe 1225302864Ssephe taskqueue_enqueue(taskqueue_thread, &chan->ch_detach_task); 1226302864Ssephe} 1227302864Ssephe 1228302864Ssephestatic void 1229302864Ssephevmbus_chan_detach_task(void *xchan, int pending __unused) 1230302864Ssephe{ 1231302864Ssephe struct hv_vmbus_channel *chan = xchan; 1232302864Ssephe 1233302864Ssephe if (VMBUS_CHAN_ISPRIMARY(chan)) { 1234302864Ssephe /* Only primary channel owns the device */ 1235302868Ssephe vmbus_delete_child(chan); 1236302864Ssephe /* NOTE: DO NOT free primary channel for now */ 1237302864Ssephe } else { 1238302864Ssephe struct vmbus_softc *sc = chan->vmbus_sc; 1239302864Ssephe struct hv_vmbus_channel *pri_chan = chan->ch_prichan; 1240302864Ssephe struct vmbus_chanmsg_chfree *req; 1241302864Ssephe struct vmbus_msghc *mh; 1242302864Ssephe int error; 1243302864Ssephe 1244302864Ssephe mh = vmbus_msghc_get(sc, sizeof(*req)); 1245302864Ssephe if (mh == NULL) { 1246302864Ssephe device_printf(sc->vmbus_dev, 1247302864Ssephe "can not get msg hypercall for chfree(chan%u)\n", 1248302864Ssephe chan->ch_id); 1249302864Ssephe goto remove; 1250302864Ssephe } 1251302864Ssephe 1252302864Ssephe req = vmbus_msghc_dataptr(mh); 1253302864Ssephe req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE; 1254302864Ssephe req->chm_chanid = chan->ch_id; 1255302864Ssephe 1256302864Ssephe error = vmbus_msghc_exec_noresult(mh); 1257302864Ssephe vmbus_msghc_put(sc, mh); 1258302864Ssephe 1259302864Ssephe if (error) { 1260302864Ssephe device_printf(sc->vmbus_dev, 1261302864Ssephe "chfree(chan%u) failed: %d", 1262302864Ssephe chan->ch_id, error); 1263302864Ssephe /* NOTE: Move on! */ 1264302864Ssephe } else { 1265302864Ssephe if (bootverbose) { 1266302864Ssephe device_printf(sc->vmbus_dev, "chan%u freed\n", 1267302864Ssephe chan->ch_id); 1268302864Ssephe } 1269302864Ssephe } 1270302864Ssepheremove: 1271302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1272302864Ssephe TAILQ_REMOVE(&pri_chan->ch_subchans, chan, ch_sublink); 1273302864Ssephe KASSERT(pri_chan->ch_subchan_cnt > 0, 1274302864Ssephe ("invalid subchan_cnt %d", pri_chan->ch_subchan_cnt)); 1275302864Ssephe pri_chan->ch_subchan_cnt--; 1276302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1277302864Ssephe wakeup(pri_chan); 1278302864Ssephe 1279302864Ssephe vmbus_chan_free(chan); 1280302864Ssephe } 1281302864Ssephe} 1282302864Ssephe 1283302864Ssephe/* 1284302864Ssephe * Detach all devices and destroy the corresponding primary channels. 1285302864Ssephe */ 1286302864Ssephevoid 1287302864Ssephevmbus_chan_destroy_all(struct vmbus_softc *sc) 1288302864Ssephe{ 1289302864Ssephe struct hv_vmbus_channel *chan; 1290302864Ssephe 1291302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1292302864Ssephe while ((chan = TAILQ_FIRST(&sc->vmbus_prichans)) != NULL) { 1293302864Ssephe KASSERT(VMBUS_CHAN_ISPRIMARY(chan), ("not primary channel")); 1294302864Ssephe TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink); 1295302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1296302864Ssephe 1297302868Ssephe vmbus_delete_child(chan); 1298302864Ssephe vmbus_chan_free(chan); 1299302864Ssephe 1300302864Ssephe mtx_lock(&sc->vmbus_prichan_lock); 1301302864Ssephe } 1302302864Ssephe bzero(sc->vmbus_chmap, 1303302864Ssephe sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX); 1304302864Ssephe mtx_unlock(&sc->vmbus_prichan_lock); 1305302864Ssephe} 1306302864Ssephe 1307302864Ssephe/** 1308302864Ssephe * @brief Select the best outgoing channel 1309302864Ssephe * 1310302864Ssephe * The channel whose vcpu binding is closest to the currect vcpu will 1311302864Ssephe * be selected. 1312302864Ssephe * If no multi-channel, always select primary channel 1313302864Ssephe * 1314302864Ssephe * @param primary - primary channel 1315302864Ssephe */ 1316302864Ssephestruct hv_vmbus_channel * 1317302864Ssephevmbus_select_outgoing_channel(struct hv_vmbus_channel *primary) 1318302864Ssephe{ 1319302864Ssephe hv_vmbus_channel *new_channel = NULL; 1320302864Ssephe hv_vmbus_channel *outgoing_channel = primary; 1321302864Ssephe int old_cpu_distance = 0; 1322302864Ssephe int new_cpu_distance = 0; 1323302864Ssephe int cur_vcpu = 0; 1324302864Ssephe int smp_pro_id = PCPU_GET(cpuid); 1325302864Ssephe 1326302864Ssephe if (TAILQ_EMPTY(&primary->ch_subchans)) { 1327302864Ssephe return outgoing_channel; 1328302864Ssephe } 1329302864Ssephe 1330302864Ssephe if (smp_pro_id >= MAXCPU) { 1331302864Ssephe return outgoing_channel; 1332302864Ssephe } 1333302864Ssephe 1334302864Ssephe cur_vcpu = VMBUS_PCPU_GET(primary->vmbus_sc, vcpuid, smp_pro_id); 1335302864Ssephe 1336302864Ssephe /* XXX need lock */ 1337302864Ssephe TAILQ_FOREACH(new_channel, &primary->ch_subchans, ch_sublink) { 1338302864Ssephe if ((new_channel->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0) { 1339302864Ssephe continue; 1340302864Ssephe } 1341302864Ssephe 1342302873Ssephe if (new_channel->ch_vcpuid == cur_vcpu){ 1343302864Ssephe return new_channel; 1344302864Ssephe } 1345302864Ssephe 1346302873Ssephe old_cpu_distance = ((outgoing_channel->ch_vcpuid > cur_vcpu) ? 1347302873Ssephe (outgoing_channel->ch_vcpuid - cur_vcpu) : 1348302873Ssephe (cur_vcpu - outgoing_channel->ch_vcpuid)); 1349302864Ssephe 1350302873Ssephe new_cpu_distance = ((new_channel->ch_vcpuid > cur_vcpu) ? 1351302873Ssephe (new_channel->ch_vcpuid - cur_vcpu) : 1352302873Ssephe (cur_vcpu - new_channel->ch_vcpuid)); 1353302864Ssephe 1354302864Ssephe if (old_cpu_distance < new_cpu_distance) { 1355302864Ssephe continue; 1356302864Ssephe } 1357302864Ssephe 1358302864Ssephe outgoing_channel = new_channel; 1359302864Ssephe } 1360302864Ssephe 1361302864Ssephe return(outgoing_channel); 1362302864Ssephe} 1363302864Ssephe 1364302864Ssephestruct hv_vmbus_channel ** 1365302864Ssephevmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt) 1366302864Ssephe{ 1367302864Ssephe struct hv_vmbus_channel **ret, *chan; 1368302864Ssephe int i; 1369302864Ssephe 1370302864Ssephe ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP, 1371302864Ssephe M_WAITOK); 1372302864Ssephe 1373302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1374302864Ssephe 1375302864Ssephe while (pri_chan->ch_subchan_cnt < subchan_cnt) 1376302864Ssephe mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0); 1377302864Ssephe 1378302864Ssephe i = 0; 1379302864Ssephe TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) { 1380302864Ssephe /* TODO: refcnt chan */ 1381302864Ssephe ret[i] = chan; 1382302864Ssephe 1383302864Ssephe ++i; 1384302864Ssephe if (i == subchan_cnt) 1385302864Ssephe break; 1386302864Ssephe } 1387302864Ssephe KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d", 1388302864Ssephe pri_chan->ch_subchan_cnt, subchan_cnt)); 1389302864Ssephe 1390302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1391302864Ssephe 1392302864Ssephe return ret; 1393302864Ssephe} 1394302864Ssephe 1395302864Ssephevoid 1396302864Ssephevmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused) 1397302864Ssephe{ 1398302864Ssephe 1399302864Ssephe free(subchan, M_TEMP); 1400302864Ssephe} 1401302864Ssephe 1402302864Ssephevoid 1403302864Ssephevmbus_drain_subchan(struct hv_vmbus_channel *pri_chan) 1404302864Ssephe{ 1405302864Ssephe mtx_lock(&pri_chan->ch_subchan_lock); 1406302864Ssephe while (pri_chan->ch_subchan_cnt > 0) 1407302864Ssephe mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0); 1408302864Ssephe mtx_unlock(&pri_chan->ch_subchan_lock); 1409302864Ssephe} 1410302864Ssephe 1411302864Ssephevoid 1412302864Ssephevmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg) 1413302864Ssephe{ 1414302864Ssephe vmbus_chanmsg_proc_t msg_proc; 1415302864Ssephe uint32_t msg_type; 1416302864Ssephe 1417302864Ssephe msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type; 1418302864Ssephe KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX, 1419302864Ssephe ("invalid message type %u", msg_type)); 1420302864Ssephe 1421302864Ssephe msg_proc = vmbus_chan_msgprocs[msg_type]; 1422302864Ssephe if (msg_proc != NULL) 1423302864Ssephe msg_proc(sc, msg); 1424302864Ssephe} 1425