Home | History | Annotate | Line # | Download | only in hyperv
vmbus.c revision 1.3.2.2
      1  1.3.2.2  christos /*	$NetBSD: vmbus.c,v 1.3.2.2 2019/06/10 22:07:09 christos Exp $	*/
      2  1.3.2.2  christos /*	$OpenBSD: hyperv.c,v 1.43 2017/06/27 13:56:15 mikeb Exp $	*/
      3  1.3.2.2  christos 
      4  1.3.2.2  christos /*-
      5  1.3.2.2  christos  * Copyright (c) 2009-2012 Microsoft Corp.
      6  1.3.2.2  christos  * Copyright (c) 2012 NetApp Inc.
      7  1.3.2.2  christos  * Copyright (c) 2012 Citrix Inc.
      8  1.3.2.2  christos  * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
      9  1.3.2.2  christos  * All rights reserved.
     10  1.3.2.2  christos  *
     11  1.3.2.2  christos  * Redistribution and use in source and binary forms, with or without
     12  1.3.2.2  christos  * modification, are permitted provided that the following conditions
     13  1.3.2.2  christos  * are met:
     14  1.3.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.3.2.2  christos  *    notice unmodified, this list of conditions, and the following
     16  1.3.2.2  christos  *    disclaimer.
     17  1.3.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.3.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     19  1.3.2.2  christos  *    documentation and/or other materials provided with the distribution.
     20  1.3.2.2  christos  *
     21  1.3.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.3.2.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.3.2.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.3.2.2  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.3.2.2  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.3.2.2  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.3.2.2  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.3.2.2  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.3.2.2  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.3.2.2  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.3.2.2  christos  */
     32  1.3.2.2  christos 
     33  1.3.2.2  christos /*
     34  1.3.2.2  christos  * The OpenBSD port was done under funding by Esdenera Networks GmbH.
     35  1.3.2.2  christos  */
     36  1.3.2.2  christos 
     37  1.3.2.2  christos #include <sys/cdefs.h>
     38  1.3.2.2  christos __KERNEL_RCSID(0, "$NetBSD: vmbus.c,v 1.3.2.2 2019/06/10 22:07:09 christos Exp $");
     39  1.3.2.2  christos 
     40  1.3.2.2  christos #include <sys/param.h>
     41  1.3.2.2  christos #include <sys/systm.h>
     42  1.3.2.2  christos #include <sys/device.h>
     43  1.3.2.2  christos #include <sys/atomic.h>
     44  1.3.2.2  christos #include <sys/bitops.h>
     45  1.3.2.2  christos #include <sys/bus.h>
     46  1.3.2.2  christos #include <sys/cpu.h>
     47  1.3.2.2  christos #include <sys/intr.h>
     48  1.3.2.2  christos #include <sys/kmem.h>
     49  1.3.2.2  christos #include <sys/module.h>
     50  1.3.2.2  christos #include <sys/mutex.h>
     51  1.3.2.2  christos #include <sys/xcall.h>
     52  1.3.2.2  christos 
     53  1.3.2.2  christos #include <uvm/uvm_extern.h>
     54  1.3.2.2  christos 
     55  1.3.2.2  christos #include <dev/hyperv/vmbusvar.h>
     56  1.3.2.2  christos 
     57  1.3.2.2  christos #define VMBUS_GPADL_START		0xffff /* 0x10000 effectively */
     58  1.3.2.2  christos 
     59  1.3.2.2  christos /* Command submission flags */
     60  1.3.2.2  christos #define HCF_SLEEPOK	0x0000
     61  1.3.2.2  christos #define HCF_NOSLEEP	0x0002	/* M_NOWAIT */
     62  1.3.2.2  christos #define HCF_NOREPLY	0x0004
     63  1.3.2.2  christos 
     64  1.3.2.2  christos static void	vmbus_attach_deferred(device_t);
     65  1.3.2.2  christos static int	vmbus_alloc_dma(struct vmbus_softc *);
     66  1.3.2.2  christos static void	vmbus_free_dma(struct vmbus_softc *);
     67  1.3.2.2  christos static int	vmbus_init_interrupts(struct vmbus_softc *);
     68  1.3.2.2  christos static void	vmbus_deinit_interrupts(struct vmbus_softc *);
     69  1.3.2.2  christos static void	vmbus_init_synic(void *, void *);
     70  1.3.2.2  christos static void	vmbus_deinit_synic(void *, void *);
     71  1.3.2.2  christos 
     72  1.3.2.2  christos static int	vmbus_connect(struct vmbus_softc *);
     73  1.3.2.2  christos static int	vmbus_cmd(struct vmbus_softc *, void *, size_t, void *, size_t,
     74  1.3.2.2  christos 		    int);
     75  1.3.2.2  christos static int	vmbus_start(struct vmbus_softc *, struct vmbus_msg *, paddr_t);
     76  1.3.2.2  christos static int	vmbus_reply(struct vmbus_softc *, struct vmbus_msg *);
     77  1.3.2.2  christos static void	vmbus_wait(struct vmbus_softc *,
     78  1.3.2.2  christos 		    int (*done)(struct vmbus_softc *, struct vmbus_msg *),
     79  1.3.2.2  christos 		    struct vmbus_msg *, void *, const char *);
     80  1.3.2.2  christos static uint16_t vmbus_intr_signal(struct vmbus_softc *, paddr_t);
     81  1.3.2.2  christos static void	vmbus_event_proc(void *, struct cpu_info *);
     82  1.3.2.2  christos static void	vmbus_event_proc_compat(void *, struct cpu_info *);
     83  1.3.2.2  christos static void	vmbus_message_proc(void *, struct cpu_info *);
     84  1.3.2.2  christos static void	vmbus_message_softintr(void *);
     85  1.3.2.2  christos static void	vmbus_channel_response(struct vmbus_softc *,
     86  1.3.2.2  christos 		    struct vmbus_chanmsg_hdr *);
     87  1.3.2.2  christos static void	vmbus_channel_offer(struct vmbus_softc *,
     88  1.3.2.2  christos 		    struct vmbus_chanmsg_hdr *);
     89  1.3.2.2  christos static void	vmbus_channel_rescind(struct vmbus_softc *,
     90  1.3.2.2  christos 		    struct vmbus_chanmsg_hdr *);
     91  1.3.2.2  christos static void	vmbus_channel_delivered(struct vmbus_softc *,
     92  1.3.2.2  christos 		    struct vmbus_chanmsg_hdr *);
     93  1.3.2.2  christos static int	vmbus_channel_scan(struct vmbus_softc *);
     94  1.3.2.2  christos static void	vmbus_channel_cpu_default(struct vmbus_channel *);
     95  1.3.2.2  christos static void	vmbus_process_offer(struct vmbus_softc *, struct vmbus_offer *);
     96  1.3.2.2  christos static struct vmbus_channel *
     97  1.3.2.2  christos 		vmbus_channel_lookup(struct vmbus_softc *, uint32_t);
     98  1.3.2.2  christos static int	vmbus_channel_ring_create(struct vmbus_channel *, uint32_t);
     99  1.3.2.2  christos static void	vmbus_channel_ring_destroy(struct vmbus_channel *);
    100  1.3.2.2  christos static void	vmbus_channel_pause(struct vmbus_channel *);
    101  1.3.2.2  christos static uint32_t	vmbus_channel_unpause(struct vmbus_channel *);
    102  1.3.2.2  christos static uint32_t	vmbus_channel_ready(struct vmbus_channel *);
    103  1.3.2.2  christos static int	vmbus_attach_icdevs(struct vmbus_softc *);
    104  1.3.2.2  christos static int	vmbus_attach_devices(struct vmbus_softc *);
    105  1.3.2.2  christos 
    106  1.3.2.2  christos static struct vmbus_softc *vmbus_sc;
    107  1.3.2.2  christos 
    108  1.3.2.2  christos static const struct {
    109  1.3.2.2  christos 	int	hmd_response;
    110  1.3.2.2  christos 	int	hmd_request;
    111  1.3.2.2  christos 	void	(*hmd_handler)(struct vmbus_softc *,
    112  1.3.2.2  christos 		    struct vmbus_chanmsg_hdr *);
    113  1.3.2.2  christos } vmbus_msg_dispatch[] = {
    114  1.3.2.2  christos 	{ 0,					0, NULL },
    115  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHOFFER,		0, vmbus_channel_offer },
    116  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHRESCIND,		0, vmbus_channel_rescind },
    117  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHREQUEST,		VMBUS_CHANMSG_CHOFFER, NULL },
    118  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHOFFER_DONE,		0, vmbus_channel_delivered },
    119  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHOPEN,			0, NULL },
    120  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHOPEN_RESP,		VMBUS_CHANMSG_CHOPEN,
    121  1.3.2.2  christos 	  vmbus_channel_response },
    122  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHCLOSE,		0, NULL },
    123  1.3.2.2  christos 	{ VMBUS_CHANMSG_GPADL_CONN,		0, NULL },
    124  1.3.2.2  christos 	{ VMBUS_CHANMSG_GPADL_SUBCONN,		0, NULL },
    125  1.3.2.2  christos 	{ VMBUS_CHANMSG_GPADL_CONNRESP,		VMBUS_CHANMSG_GPADL_CONN,
    126  1.3.2.2  christos 	  vmbus_channel_response },
    127  1.3.2.2  christos 	{ VMBUS_CHANMSG_GPADL_DISCONN,		0, NULL },
    128  1.3.2.2  christos 	{ VMBUS_CHANMSG_GPADL_DISCONNRESP,	VMBUS_CHANMSG_GPADL_DISCONN,
    129  1.3.2.2  christos 	  vmbus_channel_response },
    130  1.3.2.2  christos 	{ VMBUS_CHANMSG_CHFREE,			0, NULL },
    131  1.3.2.2  christos 	{ VMBUS_CHANMSG_CONNECT,		0, NULL },
    132  1.3.2.2  christos 	{ VMBUS_CHANMSG_CONNECT_RESP,		VMBUS_CHANMSG_CONNECT,
    133  1.3.2.2  christos 	  vmbus_channel_response },
    134  1.3.2.2  christos 	{ VMBUS_CHANMSG_DISCONNECT,		0, NULL },
    135  1.3.2.2  christos };
    136  1.3.2.2  christos 
    137  1.3.2.2  christos const struct hyperv_guid hyperv_guid_network = {
    138  1.3.2.2  christos 	{ 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46,
    139  1.3.2.2  christos 	  0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e }
    140  1.3.2.2  christos };
    141  1.3.2.2  christos 
    142  1.3.2.2  christos const struct hyperv_guid hyperv_guid_ide = {
    143  1.3.2.2  christos 	{ 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
    144  1.3.2.2  christos 	  0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 }
    145  1.3.2.2  christos };
    146  1.3.2.2  christos 
    147  1.3.2.2  christos const struct hyperv_guid hyperv_guid_scsi = {
    148  1.3.2.2  christos 	{ 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
    149  1.3.2.2  christos 	  0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f }
    150  1.3.2.2  christos };
    151  1.3.2.2  christos 
    152  1.3.2.2  christos const struct hyperv_guid hyperv_guid_shutdown = {
    153  1.3.2.2  christos 	{ 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49,
    154  1.3.2.2  christos 	  0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb }
    155  1.3.2.2  christos };
    156  1.3.2.2  christos 
    157  1.3.2.2  christos const struct hyperv_guid hyperv_guid_timesync = {
    158  1.3.2.2  christos 	{ 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
    159  1.3.2.2  christos 	  0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf }
    160  1.3.2.2  christos };
    161  1.3.2.2  christos 
    162  1.3.2.2  christos const struct hyperv_guid hyperv_guid_heartbeat = {
    163  1.3.2.2  christos 	{ 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
    164  1.3.2.2  christos 	  0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d }
    165  1.3.2.2  christos };
    166  1.3.2.2  christos 
    167  1.3.2.2  christos const struct hyperv_guid hyperv_guid_kvp = {
    168  1.3.2.2  christos 	{ 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
    169  1.3.2.2  christos 	  0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6 }
    170  1.3.2.2  christos };
    171  1.3.2.2  christos 
    172  1.3.2.2  christos const struct hyperv_guid hyperv_guid_vss = {
    173  1.3.2.2  christos 	{ 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42,
    174  1.3.2.2  christos 	  0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 }
    175  1.3.2.2  christos };
    176  1.3.2.2  christos 
    177  1.3.2.2  christos const struct hyperv_guid hyperv_guid_dynmem = {
    178  1.3.2.2  christos 	{ 0xdc, 0x74, 0x50, 0x52, 0x85, 0x89, 0xe2, 0x46,
    179  1.3.2.2  christos 	  0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 }
    180  1.3.2.2  christos };
    181  1.3.2.2  christos 
    182  1.3.2.2  christos const struct hyperv_guid hyperv_guid_mouse = {
    183  1.3.2.2  christos 	{ 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c,
    184  1.3.2.2  christos 	  0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a }
    185  1.3.2.2  christos };
    186  1.3.2.2  christos 
    187  1.3.2.2  christos const struct hyperv_guid hyperv_guid_kbd = {
    188  1.3.2.2  christos 	{ 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
    189  1.3.2.2  christos 	  0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84 }
    190  1.3.2.2  christos };
    191  1.3.2.2  christos 
    192  1.3.2.2  christos const struct hyperv_guid hyperv_guid_video = {
    193  1.3.2.2  christos 	{ 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a,
    194  1.3.2.2  christos 	  0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 }
    195  1.3.2.2  christos };
    196  1.3.2.2  christos 
    197  1.3.2.2  christos const struct hyperv_guid hyperv_guid_fc = {
    198  1.3.2.2  christos 	{ 0x4a, 0xcc, 0x9b, 0x2f, 0x69, 0x00, 0xf3, 0x4a,
    199  1.3.2.2  christos 	  0xb7, 0x6b, 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda }
    200  1.3.2.2  christos };
    201  1.3.2.2  christos 
    202  1.3.2.2  christos const struct hyperv_guid hyperv_guid_fcopy = {
    203  1.3.2.2  christos 	{ 0xe3, 0x4b, 0xd1, 0x34, 0xe4, 0xde, 0xc8, 0x41,
    204  1.3.2.2  christos 	  0x9a, 0xe7, 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92 }
    205  1.3.2.2  christos };
    206  1.3.2.2  christos 
    207  1.3.2.2  christos const struct hyperv_guid hyperv_guid_pcie = {
    208  1.3.2.2  christos 	{ 0x1d, 0xf6, 0xc4, 0x44, 0x44, 0x44, 0x00, 0x44,
    209  1.3.2.2  christos 	  0x9d, 0x52, 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f }
    210  1.3.2.2  christos };
    211  1.3.2.2  christos 
    212  1.3.2.2  christos const struct hyperv_guid hyperv_guid_netdir = {
    213  1.3.2.2  christos 	{ 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b,
    214  1.3.2.2  christos 	  0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 }
    215  1.3.2.2  christos };
    216  1.3.2.2  christos 
    217  1.3.2.2  christos const struct hyperv_guid hyperv_guid_rdesktop = {
    218  1.3.2.2  christos 	{ 0xf4, 0xac, 0x6a, 0x27, 0x15, 0xac, 0x6c, 0x42,
    219  1.3.2.2  christos 	  0x98, 0xdd, 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe }
    220  1.3.2.2  christos };
    221  1.3.2.2  christos 
    222  1.3.2.2  christos /* Automatic Virtual Machine Activation (AVMA) Services */
    223  1.3.2.2  christos const struct hyperv_guid hyperv_guid_avma1 = {
    224  1.3.2.2  christos 	{ 0x55, 0xb2, 0x87, 0x44, 0x8c, 0xb8, 0x3f, 0x40,
    225  1.3.2.2  christos 	  0xbb, 0x51, 0xd1, 0xf6, 0x9c, 0xf1, 0x7f, 0x87 }
    226  1.3.2.2  christos };
    227  1.3.2.2  christos 
    228  1.3.2.2  christos const struct hyperv_guid hyperv_guid_avma2 = {
    229  1.3.2.2  christos 	{ 0xf4, 0xba, 0x75, 0x33, 0x15, 0x9e, 0x30, 0x4b,
    230  1.3.2.2  christos 	  0xb7, 0x65, 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b }
    231  1.3.2.2  christos };
    232  1.3.2.2  christos 
    233  1.3.2.2  christos const struct hyperv_guid hyperv_guid_avma3 = {
    234  1.3.2.2  christos 	{ 0xa0, 0x1f, 0x22, 0x99, 0xad, 0x24, 0xe2, 0x11,
    235  1.3.2.2  christos 	  0xbe, 0x98, 0x00, 0x1a, 0xa0, 0x1b, 0xbf, 0x6e }
    236  1.3.2.2  christos };
    237  1.3.2.2  christos 
    238  1.3.2.2  christos const struct hyperv_guid hyperv_guid_avma4 = {
    239  1.3.2.2  christos 	{ 0x16, 0x57, 0xe6, 0xf8, 0xb3, 0x3c, 0x06, 0x4a,
    240  1.3.2.2  christos 	  0x9a, 0x60, 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5 }
    241  1.3.2.2  christos };
    242  1.3.2.2  christos 
    243  1.3.2.2  christos int
    244  1.3.2.2  christos vmbus_match(device_t parent, cfdata_t cf, void *aux)
    245  1.3.2.2  christos {
    246  1.3.2.2  christos 
    247  1.3.2.2  christos 	if (cf->cf_unit != 0 ||
    248  1.3.2.2  christos 	    !hyperv_hypercall_enabled() ||
    249  1.3.2.2  christos 	    !hyperv_synic_supported())
    250  1.3.2.2  christos 		return 0;
    251  1.3.2.2  christos 
    252  1.3.2.2  christos 	return 1;
    253  1.3.2.2  christos }
    254  1.3.2.2  christos 
    255  1.3.2.2  christos int
    256  1.3.2.2  christos vmbus_attach(struct vmbus_softc *sc)
    257  1.3.2.2  christos {
    258  1.3.2.2  christos 
    259  1.3.2.2  christos 	aprint_naive("\n");
    260  1.3.2.2  christos 	aprint_normal(": Hyper-V VMBus\n");
    261  1.3.2.2  christos 
    262  1.3.2.2  christos 	vmbus_sc = sc;
    263  1.3.2.2  christos 
    264  1.3.2.2  christos 	sc->sc_msgpool = pool_cache_init(sizeof(struct vmbus_msg), 8, 0, 0,
    265  1.3.2.2  christos 	    "hvmsg", NULL, IPL_NET, NULL, NULL, NULL);
    266  1.3.2.2  christos 	hyperv_set_message_proc(vmbus_message_proc, sc);
    267  1.3.2.2  christos 
    268  1.3.2.2  christos 	if (vmbus_alloc_dma(sc))
    269  1.3.2.2  christos 		goto cleanup;
    270  1.3.2.2  christos 
    271  1.3.2.2  christos 	if (vmbus_init_interrupts(sc))
    272  1.3.2.2  christos 		goto cleanup;
    273  1.3.2.2  christos 
    274  1.3.2.2  christos 	if (vmbus_connect(sc))
    275  1.3.2.2  christos 		goto cleanup;
    276  1.3.2.2  christos 
    277  1.3.2.2  christos 	aprint_normal_dev(sc->sc_dev, "protocol %d.%d\n",
    278  1.3.2.2  christos 	    VMBUS_VERSION_MAJOR(sc->sc_proto),
    279  1.3.2.2  christos 	    VMBUS_VERSION_MINOR(sc->sc_proto));
    280  1.3.2.2  christos 
    281  1.3.2.2  christos 	if (sc->sc_proto == VMBUS_VERSION_WS2008 ||
    282  1.3.2.2  christos 	    sc->sc_proto == VMBUS_VERSION_WIN7) {
    283  1.3.2.2  christos 		hyperv_set_event_proc(vmbus_event_proc_compat, sc);
    284  1.3.2.2  christos 		sc->sc_channel_max = VMBUS_CHAN_MAX_COMPAT;
    285  1.3.2.2  christos 	} else {
    286  1.3.2.2  christos 		hyperv_set_event_proc(vmbus_event_proc, sc);
    287  1.3.2.2  christos 		sc->sc_channel_max = VMBUS_CHAN_MAX;
    288  1.3.2.2  christos 	}
    289  1.3.2.2  christos 
    290  1.3.2.2  christos 	if (vmbus_channel_scan(sc))
    291  1.3.2.2  christos 		goto cleanup;
    292  1.3.2.2  christos 
    293  1.3.2.2  christos 	/* Attach heartbeat, KVP and other "internal" services */
    294  1.3.2.2  christos 	vmbus_attach_icdevs(sc);
    295  1.3.2.2  christos 
    296  1.3.2.2  christos 	/* Attach devices with external drivers */
    297  1.3.2.2  christos 	vmbus_attach_devices(sc);
    298  1.3.2.2  christos 
    299  1.3.2.2  christos 	config_interrupts(sc->sc_dev, vmbus_attach_deferred);
    300  1.3.2.2  christos 
    301  1.3.2.2  christos 	return 0;
    302  1.3.2.2  christos 
    303  1.3.2.2  christos cleanup:
    304  1.3.2.2  christos 	vmbus_deinit_interrupts(sc);
    305  1.3.2.2  christos 	vmbus_free_dma(sc);
    306  1.3.2.2  christos 	return -1;
    307  1.3.2.2  christos }
    308  1.3.2.2  christos 
    309  1.3.2.2  christos static void
    310  1.3.2.2  christos vmbus_attach_deferred(device_t self)
    311  1.3.2.2  christos {
    312  1.3.2.2  christos 	struct vmbus_softc *sc = device_private(self);
    313  1.3.2.2  christos 
    314  1.3.2.2  christos 	xc_wait(xc_broadcast(0, vmbus_init_synic, sc, NULL));
    315  1.3.2.2  christos }
    316  1.3.2.2  christos 
    317  1.3.2.2  christos int
    318  1.3.2.2  christos vmbus_detach(struct vmbus_softc *sc, int flags)
    319  1.3.2.2  christos {
    320  1.3.2.2  christos 
    321  1.3.2.2  christos 	vmbus_deinit_interrupts(sc);
    322  1.3.2.2  christos 	vmbus_free_dma(sc);
    323  1.3.2.2  christos 
    324  1.3.2.2  christos 	return 0;
    325  1.3.2.2  christos }
    326  1.3.2.2  christos 
    327  1.3.2.2  christos static int
    328  1.3.2.2  christos vmbus_alloc_dma(struct vmbus_softc *sc)
    329  1.3.2.2  christos {
    330  1.3.2.2  christos 	CPU_INFO_ITERATOR cii;
    331  1.3.2.2  christos 	struct cpu_info *ci;
    332  1.3.2.2  christos 	struct vmbus_percpu_data *pd;
    333  1.3.2.2  christos 	int i;
    334  1.3.2.2  christos 
    335  1.3.2.2  christos 	/*
    336  1.3.2.2  christos 	 * Per-CPU messages and event flags.
    337  1.3.2.2  christos 	 */
    338  1.3.2.2  christos 	for (CPU_INFO_FOREACH(cii, ci)) {
    339  1.3.2.2  christos 		pd = &sc->sc_percpu[cpu_index(ci)];
    340  1.3.2.2  christos 
    341  1.3.2.2  christos 		pd->simp = hyperv_dma_alloc(sc->sc_dmat, &pd->simp_dma,
    342  1.3.2.2  christos 		    PAGE_SIZE, PAGE_SIZE, 0, 1);
    343  1.3.2.2  christos 		if (pd->simp == NULL)
    344  1.3.2.2  christos 			return ENOMEM;
    345  1.3.2.2  christos 
    346  1.3.2.2  christos 		pd->siep = hyperv_dma_alloc(sc->sc_dmat, &pd->siep_dma,
    347  1.3.2.2  christos 		    PAGE_SIZE, PAGE_SIZE, 0, 1);
    348  1.3.2.2  christos 		if (pd->siep == NULL)
    349  1.3.2.2  christos 			return ENOMEM;
    350  1.3.2.2  christos 	}
    351  1.3.2.2  christos 
    352  1.3.2.2  christos 	sc->sc_events = hyperv_dma_alloc(sc->sc_dmat, &sc->sc_events_dma,
    353  1.3.2.2  christos 	    PAGE_SIZE, PAGE_SIZE, 0, 1);
    354  1.3.2.2  christos 	if (sc->sc_events == NULL)
    355  1.3.2.2  christos 		return ENOMEM;
    356  1.3.2.2  christos 	sc->sc_wevents = (u_long *)sc->sc_events;
    357  1.3.2.2  christos 	sc->sc_revents = (u_long *)((uint8_t *)sc->sc_events + (PAGE_SIZE / 2));
    358  1.3.2.2  christos 
    359  1.3.2.2  christos 	for (i = 0; i < __arraycount(sc->sc_monitor); i++) {
    360  1.3.2.2  christos 		sc->sc_monitor[i] = hyperv_dma_alloc(sc->sc_dmat,
    361  1.3.2.2  christos 		    &sc->sc_monitor_dma[i], PAGE_SIZE, PAGE_SIZE, 0, 1);
    362  1.3.2.2  christos 		if (sc->sc_monitor[i] == NULL)
    363  1.3.2.2  christos 			return ENOMEM;
    364  1.3.2.2  christos 	}
    365  1.3.2.2  christos 
    366  1.3.2.2  christos 	return 0;
    367  1.3.2.2  christos }
    368  1.3.2.2  christos 
    369  1.3.2.2  christos static void
    370  1.3.2.2  christos vmbus_free_dma(struct vmbus_softc *sc)
    371  1.3.2.2  christos {
    372  1.3.2.2  christos 	CPU_INFO_ITERATOR cii;
    373  1.3.2.2  christos 	struct cpu_info *ci;
    374  1.3.2.2  christos 	int i;
    375  1.3.2.2  christos 
    376  1.3.2.2  christos 	if (sc->sc_events != NULL) {
    377  1.3.2.2  christos 		sc->sc_events = sc->sc_wevents = sc->sc_revents = NULL;
    378  1.3.2.2  christos 		hyperv_dma_free(sc->sc_dmat, &sc->sc_events_dma);
    379  1.3.2.2  christos 	}
    380  1.3.2.2  christos 
    381  1.3.2.2  christos 	for (i = 0; i < __arraycount(sc->sc_monitor); i++) {
    382  1.3.2.2  christos 		sc->sc_monitor[i] = NULL;
    383  1.3.2.2  christos 		hyperv_dma_free(sc->sc_dmat, &sc->sc_monitor_dma[i]);
    384  1.3.2.2  christos 	}
    385  1.3.2.2  christos 
    386  1.3.2.2  christos 	for (CPU_INFO_FOREACH(cii, ci)) {
    387  1.3.2.2  christos 		struct vmbus_percpu_data *pd = &sc->sc_percpu[cpu_index(ci)];
    388  1.3.2.2  christos 
    389  1.3.2.2  christos 		if (pd->simp != NULL) {
    390  1.3.2.2  christos 			pd->simp = NULL;
    391  1.3.2.2  christos 			hyperv_dma_free(sc->sc_dmat, &pd->simp_dma);
    392  1.3.2.2  christos 		}
    393  1.3.2.2  christos 		if (pd->siep != NULL) {
    394  1.3.2.2  christos 			pd->siep = NULL;
    395  1.3.2.2  christos 			hyperv_dma_free(sc->sc_dmat, &pd->siep_dma);
    396  1.3.2.2  christos 		}
    397  1.3.2.2  christos 	}
    398  1.3.2.2  christos }
    399  1.3.2.2  christos 
    400  1.3.2.2  christos static int
    401  1.3.2.2  christos vmbus_init_interrupts(struct vmbus_softc *sc)
    402  1.3.2.2  christos {
    403  1.3.2.2  christos 
    404  1.3.2.2  christos 	TAILQ_INIT(&sc->sc_reqs);
    405  1.3.2.2  christos 	mutex_init(&sc->sc_req_lock, MUTEX_DEFAULT, IPL_NET);
    406  1.3.2.2  christos 
    407  1.3.2.2  christos 	TAILQ_INIT(&sc->sc_rsps);
    408  1.3.2.2  christos 	mutex_init(&sc->sc_rsp_lock, MUTEX_DEFAULT, IPL_NET);
    409  1.3.2.2  christos 
    410  1.3.2.2  christos 	sc->sc_proto = VMBUS_VERSION_WS2008;
    411  1.3.2.2  christos 
    412  1.3.2.2  christos 	/* XXX event_tq */
    413  1.3.2.2  christos 
    414  1.3.2.2  christos 	sc->sc_msg_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    415  1.3.2.2  christos 	    vmbus_message_softintr, sc);
    416  1.3.2.2  christos 	if (sc->sc_msg_sih == NULL)
    417  1.3.2.2  christos 		return -1;
    418  1.3.2.2  christos 
    419  1.3.2.2  christos 	vmbus_init_interrupts_md(sc);
    420  1.3.2.2  christos 
    421  1.3.2.2  christos 	kcpuset_create(&sc->sc_intr_cpuset, true);
    422  1.3.2.2  christos 	if (cold) {
    423  1.3.2.2  christos 		/* Initialize other CPUs later. */
    424  1.3.2.2  christos 		vmbus_init_synic(sc, NULL);
    425  1.3.2.2  christos 	} else
    426  1.3.2.2  christos 		xc_wait(xc_broadcast(0, vmbus_init_synic, sc, NULL));
    427  1.3.2.2  christos 	atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_SYNIC);
    428  1.3.2.2  christos 
    429  1.3.2.2  christos 	return 0;
    430  1.3.2.2  christos }
    431  1.3.2.2  christos 
    432  1.3.2.2  christos static void
    433  1.3.2.2  christos vmbus_deinit_interrupts(struct vmbus_softc *sc)
    434  1.3.2.2  christos {
    435  1.3.2.2  christos 
    436  1.3.2.2  christos 	if (ISSET(sc->sc_flags, VMBUS_SCFLAG_SYNIC)) {
    437  1.3.2.2  christos 		if (cold)
    438  1.3.2.2  christos 			vmbus_deinit_synic(sc, NULL);
    439  1.3.2.2  christos 		else
    440  1.3.2.2  christos 			xc_wait(xc_broadcast(0, vmbus_deinit_synic, sc, NULL));
    441  1.3.2.2  christos 		atomic_and_32(&sc->sc_flags, (uint32_t)~VMBUS_SCFLAG_SYNIC);
    442  1.3.2.2  christos 	}
    443  1.3.2.2  christos 
    444  1.3.2.2  christos 	/* XXX event_tq */
    445  1.3.2.2  christos 
    446  1.3.2.2  christos 	if (sc->sc_msg_sih != NULL) {
    447  1.3.2.2  christos 		softint_disestablish(sc->sc_msg_sih);
    448  1.3.2.2  christos 		sc->sc_msg_sih = NULL;
    449  1.3.2.2  christos 	}
    450  1.3.2.2  christos 
    451  1.3.2.2  christos 	vmbus_deinit_interrupts_md(sc);
    452  1.3.2.2  christos }
    453  1.3.2.2  christos 
    454  1.3.2.2  christos static void
    455  1.3.2.2  christos vmbus_init_synic(void *arg1, void *arg2)
    456  1.3.2.2  christos {
    457  1.3.2.2  christos 	struct vmbus_softc *sc = arg1;
    458  1.3.2.2  christos 	cpuid_t cpu;
    459  1.3.2.2  christos 	int s;
    460  1.3.2.2  christos 
    461  1.3.2.2  christos 	s = splhigh();
    462  1.3.2.2  christos 
    463  1.3.2.2  christos 	cpu = cpu_index(curcpu());
    464  1.3.2.2  christos 	if (!kcpuset_isset(sc->sc_intr_cpuset, cpu)) {
    465  1.3.2.2  christos 		kcpuset_atomic_set(sc->sc_intr_cpuset, cpu);
    466  1.3.2.2  christos 		vmbus_init_synic_md(sc, cpu);
    467  1.3.2.2  christos 	}
    468  1.3.2.2  christos 
    469  1.3.2.2  christos 	splx(s);
    470  1.3.2.2  christos }
    471  1.3.2.2  christos 
    472  1.3.2.2  christos static void
    473  1.3.2.2  christos vmbus_deinit_synic(void *arg1, void *arg2)
    474  1.3.2.2  christos {
    475  1.3.2.2  christos 	struct vmbus_softc *sc = arg1;
    476  1.3.2.2  christos 	cpuid_t cpu;
    477  1.3.2.2  christos 	int s;
    478  1.3.2.2  christos 
    479  1.3.2.2  christos 	s = splhigh();
    480  1.3.2.2  christos 
    481  1.3.2.2  christos 	cpu = cpu_index(curcpu());
    482  1.3.2.2  christos 	if (kcpuset_isset(sc->sc_intr_cpuset, cpu)) {
    483  1.3.2.2  christos 		vmbus_deinit_synic_md(sc, cpu);
    484  1.3.2.2  christos 		kcpuset_atomic_clear(sc->sc_intr_cpuset, cpu);
    485  1.3.2.2  christos 	}
    486  1.3.2.2  christos 
    487  1.3.2.2  christos 	splx(s);
    488  1.3.2.2  christos }
    489  1.3.2.2  christos 
    490  1.3.2.2  christos static int
    491  1.3.2.2  christos vmbus_connect(struct vmbus_softc *sc)
    492  1.3.2.2  christos {
    493  1.3.2.2  christos 	static const uint32_t versions[] = {
    494  1.3.2.2  christos 		VMBUS_VERSION_WIN8_1,
    495  1.3.2.2  christos 		VMBUS_VERSION_WIN8,
    496  1.3.2.2  christos 		VMBUS_VERSION_WIN7,
    497  1.3.2.2  christos 		VMBUS_VERSION_WS2008
    498  1.3.2.2  christos 	};
    499  1.3.2.2  christos 	struct vmbus_chanmsg_connect cmd;
    500  1.3.2.2  christos 	struct vmbus_chanmsg_connect_resp rsp;
    501  1.3.2.2  christos 	int i, rv;
    502  1.3.2.2  christos 
    503  1.3.2.2  christos 	memset(&cmd, 0, sizeof(cmd));
    504  1.3.2.2  christos 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CONNECT;
    505  1.3.2.2  christos 	cmd.chm_evtflags = hyperv_dma_get_paddr(&sc->sc_events_dma);
    506  1.3.2.2  christos 	cmd.chm_mnf1 = hyperv_dma_get_paddr(&sc->sc_monitor_dma[0]);
    507  1.3.2.2  christos 	cmd.chm_mnf2 = hyperv_dma_get_paddr(&sc->sc_monitor_dma[1]);
    508  1.3.2.2  christos 
    509  1.3.2.2  christos 	memset(&rsp, 0, sizeof(rsp));
    510  1.3.2.2  christos 
    511  1.3.2.2  christos 	for (i = 0; i < __arraycount(versions); i++) {
    512  1.3.2.2  christos 		cmd.chm_ver = versions[i];
    513  1.3.2.2  christos 		rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
    514  1.3.2.2  christos 		    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
    515  1.3.2.2  christos 		if (rv) {
    516  1.3.2.2  christos 			DPRINTF("%s: CONNECT failed\n",
    517  1.3.2.2  christos 			    device_xname(sc->sc_dev));
    518  1.3.2.2  christos 			return rv;
    519  1.3.2.2  christos 		}
    520  1.3.2.2  christos 		if (rsp.chm_done) {
    521  1.3.2.2  christos 			atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_CONNECTED);
    522  1.3.2.2  christos 			sc->sc_proto = versions[i];
    523  1.3.2.2  christos 			sc->sc_handle = VMBUS_GPADL_START;
    524  1.3.2.2  christos 			break;
    525  1.3.2.2  christos 		}
    526  1.3.2.2  christos 	}
    527  1.3.2.2  christos 	if (i == __arraycount(versions)) {
    528  1.3.2.2  christos 		device_printf(sc->sc_dev,
    529  1.3.2.2  christos 		    "failed to negotiate protocol version\n");
    530  1.3.2.2  christos 		return ENXIO;
    531  1.3.2.2  christos 	}
    532  1.3.2.2  christos 
    533  1.3.2.2  christos 	return 0;
    534  1.3.2.2  christos }
    535  1.3.2.2  christos 
    536  1.3.2.2  christos static int
    537  1.3.2.2  christos vmbus_cmd(struct vmbus_softc *sc, void *cmd, size_t cmdlen, void *rsp,
    538  1.3.2.2  christos     size_t rsplen, int flags)
    539  1.3.2.2  christos {
    540  1.3.2.2  christos 	const int prflags = cold ? PR_NOWAIT : PR_WAITOK;
    541  1.3.2.2  christos 	struct vmbus_msg *msg;
    542  1.3.2.2  christos 	paddr_t pa;
    543  1.3.2.2  christos 	int rv;
    544  1.3.2.2  christos 
    545  1.3.2.2  christos 	if (cmdlen > VMBUS_MSG_DSIZE_MAX) {
    546  1.3.2.2  christos 		device_printf(sc->sc_dev, "payload too large (%zu)\n",
    547  1.3.2.2  christos 		    cmdlen);
    548  1.3.2.2  christos 		return EMSGSIZE;
    549  1.3.2.2  christos 	}
    550  1.3.2.2  christos 
    551  1.3.2.2  christos 	msg = pool_cache_get_paddr(sc->sc_msgpool, prflags, &pa);
    552  1.3.2.2  christos 	if (msg == NULL) {
    553  1.3.2.2  christos 		device_printf(sc->sc_dev, "couldn't get msgpool\n");
    554  1.3.2.2  christos 		return ENOMEM;
    555  1.3.2.2  christos 	}
    556  1.3.2.2  christos 	memset(msg, 0, sizeof(*msg));
    557  1.3.2.2  christos 	msg->msg_req.hc_dsize = cmdlen;
    558  1.3.2.2  christos 	memcpy(msg->msg_req.hc_data, cmd, cmdlen);
    559  1.3.2.2  christos 
    560  1.3.2.2  christos 	if (!(flags & HCF_NOREPLY)) {
    561  1.3.2.2  christos 		msg->msg_rsp = rsp;
    562  1.3.2.2  christos 		msg->msg_rsplen = rsplen;
    563  1.3.2.2  christos 	} else
    564  1.3.2.2  christos 		msg->msg_flags |= MSGF_NOQUEUE;
    565  1.3.2.2  christos 
    566  1.3.2.2  christos 	if (flags & HCF_NOSLEEP)
    567  1.3.2.2  christos 		msg->msg_flags |= MSGF_NOSLEEP;
    568  1.3.2.2  christos 
    569  1.3.2.2  christos 	rv = vmbus_start(sc, msg, pa);
    570  1.3.2.2  christos 	if (rv == 0)
    571  1.3.2.2  christos 		rv = vmbus_reply(sc, msg);
    572  1.3.2.2  christos 	pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
    573  1.3.2.2  christos 	return rv;
    574  1.3.2.2  christos }
    575  1.3.2.2  christos 
    576  1.3.2.2  christos static int
    577  1.3.2.2  christos vmbus_start(struct vmbus_softc *sc, struct vmbus_msg *msg, paddr_t msg_pa)
    578  1.3.2.2  christos {
    579  1.3.2.2  christos 	static const int delays[] = {
    580  1.3.2.2  christos 		100, 100, 100, 500, 500, 5000, 5000, 5000
    581  1.3.2.2  christos 	};
    582  1.3.2.2  christos 	const char *wchan = "hvstart";
    583  1.3.2.2  christos 	uint16_t status;
    584  1.3.2.2  christos 	int i, s;
    585  1.3.2.2  christos 
    586  1.3.2.2  christos 	msg->msg_req.hc_connid = VMBUS_CONNID_MESSAGE;
    587  1.3.2.2  christos 	msg->msg_req.hc_msgtype = 1;
    588  1.3.2.2  christos 
    589  1.3.2.2  christos 	if (!(msg->msg_flags & MSGF_NOQUEUE)) {
    590  1.3.2.2  christos 		mutex_enter(&sc->sc_req_lock);
    591  1.3.2.2  christos 		TAILQ_INSERT_TAIL(&sc->sc_reqs, msg, msg_entry);
    592  1.3.2.2  christos 		mutex_exit(&sc->sc_req_lock);
    593  1.3.2.2  christos 	}
    594  1.3.2.2  christos 
    595  1.3.2.2  christos 	for (i = 0; i < __arraycount(delays); i++) {
    596  1.3.2.2  christos 		status = hyperv_hypercall_post_message(
    597  1.3.2.2  christos 		    msg_pa + offsetof(struct vmbus_msg, msg_req));
    598  1.3.2.2  christos 		if (status == HYPERCALL_STATUS_SUCCESS)
    599  1.3.2.2  christos 			break;
    600  1.3.2.2  christos 
    601  1.3.2.2  christos 		if (msg->msg_flags & MSGF_NOSLEEP) {
    602  1.3.2.2  christos 			delay(delays[i]);
    603  1.3.2.2  christos 			s = splnet();
    604  1.3.2.2  christos 			hyperv_intr();
    605  1.3.2.2  christos 			splx(s);
    606  1.3.2.2  christos 		} else
    607  1.3.2.2  christos 			tsleep(wchan, PRIBIO, wchan, 1);
    608  1.3.2.2  christos 	}
    609  1.3.2.2  christos 	if (status != HYPERCALL_STATUS_SUCCESS) {
    610  1.3.2.2  christos 		device_printf(sc->sc_dev,
    611  1.3.2.2  christos 		    "posting vmbus message failed with %d\n", status);
    612  1.3.2.2  christos 		if (!(msg->msg_flags & MSGF_NOQUEUE)) {
    613  1.3.2.2  christos 			mutex_enter(&sc->sc_req_lock);
    614  1.3.2.2  christos 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
    615  1.3.2.2  christos 			mutex_exit(&sc->sc_req_lock);
    616  1.3.2.2  christos 		}
    617  1.3.2.2  christos 		return EIO;
    618  1.3.2.2  christos 	}
    619  1.3.2.2  christos 
    620  1.3.2.2  christos 	return 0;
    621  1.3.2.2  christos }
    622  1.3.2.2  christos 
    623  1.3.2.2  christos static int
    624  1.3.2.2  christos vmbus_reply_done(struct vmbus_softc *sc, struct vmbus_msg *msg)
    625  1.3.2.2  christos {
    626  1.3.2.2  christos 	struct vmbus_msg *m;
    627  1.3.2.2  christos 
    628  1.3.2.2  christos 	mutex_enter(&sc->sc_rsp_lock);
    629  1.3.2.2  christos 	TAILQ_FOREACH(m, &sc->sc_rsps, msg_entry) {
    630  1.3.2.2  christos 		if (m == msg) {
    631  1.3.2.2  christos 			mutex_exit(&sc->sc_rsp_lock);
    632  1.3.2.2  christos 			return 1;
    633  1.3.2.2  christos 		}
    634  1.3.2.2  christos 	}
    635  1.3.2.2  christos 	mutex_exit(&sc->sc_rsp_lock);
    636  1.3.2.2  christos 	return 0;
    637  1.3.2.2  christos }
    638  1.3.2.2  christos 
    639  1.3.2.2  christos static int
    640  1.3.2.2  christos vmbus_reply(struct vmbus_softc *sc, struct vmbus_msg *msg)
    641  1.3.2.2  christos {
    642  1.3.2.2  christos 
    643  1.3.2.2  christos 	if (msg->msg_flags & MSGF_NOQUEUE)
    644  1.3.2.2  christos 		return 0;
    645  1.3.2.2  christos 
    646  1.3.2.2  christos 	vmbus_wait(sc, vmbus_reply_done, msg, msg, "hvreply");
    647  1.3.2.2  christos 
    648  1.3.2.2  christos 	mutex_enter(&sc->sc_rsp_lock);
    649  1.3.2.2  christos 	TAILQ_REMOVE(&sc->sc_rsps, msg, msg_entry);
    650  1.3.2.2  christos 	mutex_exit(&sc->sc_rsp_lock);
    651  1.3.2.2  christos 
    652  1.3.2.2  christos 	return 0;
    653  1.3.2.2  christos }
    654  1.3.2.2  christos 
    655  1.3.2.2  christos static void
    656  1.3.2.2  christos vmbus_wait(struct vmbus_softc *sc,
    657  1.3.2.2  christos     int (*cond)(struct vmbus_softc *, struct vmbus_msg *),
    658  1.3.2.2  christos     struct vmbus_msg *msg, void *wchan, const char *wmsg)
    659  1.3.2.2  christos {
    660  1.3.2.2  christos 	int s;
    661  1.3.2.2  christos 
    662  1.3.2.2  christos 	while (!cond(sc, msg)) {
    663  1.3.2.2  christos 		if (msg->msg_flags & MSGF_NOSLEEP) {
    664  1.3.2.2  christos 			delay(1000);
    665  1.3.2.2  christos 			s = splnet();
    666  1.3.2.2  christos 			hyperv_intr();
    667  1.3.2.2  christos 			splx(s);
    668  1.3.2.2  christos 		} else
    669  1.3.2.2  christos 			tsleep(wchan, PRIBIO, wmsg ? wmsg : "hvwait", 1);
    670  1.3.2.2  christos 	}
    671  1.3.2.2  christos }
    672  1.3.2.2  christos 
    673  1.3.2.2  christos static uint16_t
    674  1.3.2.2  christos vmbus_intr_signal(struct vmbus_softc *sc, paddr_t con_pa)
    675  1.3.2.2  christos {
    676  1.3.2.2  christos 	uint64_t status;
    677  1.3.2.2  christos 
    678  1.3.2.2  christos 	status = hyperv_hypercall_signal_event(con_pa);
    679  1.3.2.2  christos 	return (uint16_t)status;
    680  1.3.2.2  christos }
    681  1.3.2.2  christos 
    682  1.3.2.2  christos #if LONG_BIT == 64
    683  1.3.2.2  christos #define ffsl(v)	ffs64(v)
    684  1.3.2.2  christos #elif LONG_BIT == 32
    685  1.3.2.2  christos #define ffsl(v)	ffs32(v)
    686  1.3.2.2  christos #else
    687  1.3.2.2  christos #error unsupport LONG_BIT
    688  1.3.2.2  christos #endif	/* LONG_BIT */
    689  1.3.2.2  christos 
    690  1.3.2.2  christos static void
    691  1.3.2.2  christos vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *revents,
    692  1.3.2.2  christos     int maxrow)
    693  1.3.2.2  christos {
    694  1.3.2.2  christos 	struct vmbus_channel *ch;
    695  1.3.2.2  christos 	u_long pending;
    696  1.3.2.2  christos 	uint32_t chanid, chanid_base;
    697  1.3.2.2  christos 	int row, chanid_ofs;
    698  1.3.2.2  christos 
    699  1.3.2.2  christos 	for (row = 0; row < maxrow; row++) {
    700  1.3.2.2  christos 		if (revents[row] == 0)
    701  1.3.2.2  christos 			continue;
    702  1.3.2.2  christos 
    703  1.3.2.2  christos 		pending = atomic_swap_ulong(&revents[row], 0);
    704  1.3.2.2  christos 		chanid_base = row * LONG_BIT;
    705  1.3.2.2  christos 
    706  1.3.2.2  christos 		while ((chanid_ofs = ffsl(pending)) != 0) {
    707  1.3.2.2  christos 			chanid_ofs--;	/* NOTE: ffs is 1-based */
    708  1.3.2.2  christos 			pending &= ~(1UL << chanid_ofs);
    709  1.3.2.2  christos 
    710  1.3.2.2  christos 			chanid = chanid_base + chanid_ofs;
    711  1.3.2.2  christos 			/* vmbus channel protocol message */
    712  1.3.2.2  christos 			if (chanid == 0)
    713  1.3.2.2  christos 				continue;
    714  1.3.2.2  christos 
    715  1.3.2.2  christos 			ch = vmbus_channel_lookup(sc, chanid);
    716  1.3.2.2  christos 			if (ch == NULL) {
    717  1.3.2.2  christos 				device_printf(sc->sc_dev,
    718  1.3.2.2  christos 				    "unhandled event on %d\n", chanid);
    719  1.3.2.2  christos 				continue;
    720  1.3.2.2  christos 			}
    721  1.3.2.2  christos 			if (ch->ch_state != VMBUS_CHANSTATE_OPENED) {
    722  1.3.2.2  christos 				device_printf(sc->sc_dev,
    723  1.3.2.2  christos 				    "channel %d is not active\n", chanid);
    724  1.3.2.2  christos 				continue;
    725  1.3.2.2  christos 			}
    726  1.3.2.2  christos 			ch->ch_evcnt.ev_count++;
    727  1.3.2.2  christos 			vmbus_channel_schedule(ch);
    728  1.3.2.2  christos 		}
    729  1.3.2.2  christos 	}
    730  1.3.2.2  christos }
    731  1.3.2.2  christos 
    732  1.3.2.2  christos static void
    733  1.3.2.2  christos vmbus_event_proc(void *arg, struct cpu_info *ci)
    734  1.3.2.2  christos {
    735  1.3.2.2  christos 	struct vmbus_softc *sc = arg;
    736  1.3.2.2  christos 	struct vmbus_evtflags *evt;
    737  1.3.2.2  christos 
    738  1.3.2.2  christos 	/*
    739  1.3.2.2  christos 	 * On Host with Win8 or above, the event page can be
    740  1.3.2.2  christos 	 * checked directly to get the id of the channel
    741  1.3.2.2  christos 	 * that has the pending interrupt.
    742  1.3.2.2  christos 	 */
    743  1.3.2.2  christos 	evt = (struct vmbus_evtflags *)sc->sc_percpu[cpu_index(ci)].siep +
    744  1.3.2.2  christos 	    VMBUS_SINT_MESSAGE;
    745  1.3.2.2  christos 
    746  1.3.2.2  christos 	vmbus_event_flags_proc(sc, evt->evt_flags,
    747  1.3.2.2  christos 	    __arraycount(evt->evt_flags));
    748  1.3.2.2  christos }
    749  1.3.2.2  christos 
    750  1.3.2.2  christos static void
    751  1.3.2.2  christos vmbus_event_proc_compat(void *arg, struct cpu_info *ci)
    752  1.3.2.2  christos {
    753  1.3.2.2  christos 	struct vmbus_softc *sc = arg;
    754  1.3.2.2  christos 	struct vmbus_evtflags *evt;
    755  1.3.2.2  christos 
    756  1.3.2.2  christos 	evt = (struct vmbus_evtflags *)sc->sc_percpu[cpu_index(ci)].siep +
    757  1.3.2.2  christos 	    VMBUS_SINT_MESSAGE;
    758  1.3.2.2  christos 
    759  1.3.2.2  christos 	if (test_bit(0, &evt->evt_flags[0])) {
    760  1.3.2.2  christos 		clear_bit(0, &evt->evt_flags[0]);
    761  1.3.2.2  christos 		/*
    762  1.3.2.2  christos 		 * receive size is 1/2 page and divide that by 4 bytes
    763  1.3.2.2  christos 		 */
    764  1.3.2.2  christos 		vmbus_event_flags_proc(sc, sc->sc_revents,
    765  1.3.2.2  christos 		    VMBUS_CHAN_MAX_COMPAT / VMBUS_EVTFLAG_LEN);
    766  1.3.2.2  christos 	}
    767  1.3.2.2  christos }
    768  1.3.2.2  christos 
    769  1.3.2.2  christos static void
    770  1.3.2.2  christos vmbus_message_proc(void *arg, struct cpu_info *ci)
    771  1.3.2.2  christos {
    772  1.3.2.2  christos 	struct vmbus_softc *sc = arg;
    773  1.3.2.2  christos 	struct vmbus_message *msg;
    774  1.3.2.2  christos 
    775  1.3.2.2  christos 	msg = (struct vmbus_message *)sc->sc_percpu[cpu_index(ci)].simp +
    776  1.3.2.2  christos 	    VMBUS_SINT_MESSAGE;
    777  1.3.2.2  christos 	if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
    778  1.3.2.2  christos 		if (__predict_true(!cold))
    779  1.3.2.2  christos 			softint_schedule_cpu(sc->sc_msg_sih, ci);
    780  1.3.2.2  christos 		else
    781  1.3.2.2  christos 			vmbus_message_softintr(sc);
    782  1.3.2.2  christos 	}
    783  1.3.2.2  christos }
    784  1.3.2.2  christos 
    785  1.3.2.2  christos static void
    786  1.3.2.2  christos vmbus_message_softintr(void *arg)
    787  1.3.2.2  christos {
    788  1.3.2.2  christos 	struct vmbus_softc *sc = arg;
    789  1.3.2.2  christos 	struct vmbus_message *msg;
    790  1.3.2.2  christos 	struct vmbus_chanmsg_hdr *hdr;
    791  1.3.2.2  christos 	uint32_t type;
    792  1.3.2.2  christos 	cpuid_t cpu;
    793  1.3.2.2  christos 
    794  1.3.2.2  christos 	cpu = cpu_index(curcpu());
    795  1.3.2.2  christos 
    796  1.3.2.2  christos 	for (;;) {
    797  1.3.2.2  christos 		msg = (struct vmbus_message *)sc->sc_percpu[cpu].simp +
    798  1.3.2.2  christos 		    VMBUS_SINT_MESSAGE;
    799  1.3.2.2  christos 		if (msg->msg_type == HYPERV_MSGTYPE_NONE)
    800  1.3.2.2  christos 			break;
    801  1.3.2.2  christos 
    802  1.3.2.2  christos 		hdr = (struct vmbus_chanmsg_hdr *)msg->msg_data;
    803  1.3.2.2  christos 		type = hdr->chm_type;
    804  1.3.2.2  christos 		if (type >= VMBUS_CHANMSG_COUNT) {
    805  1.3.2.2  christos 			device_printf(sc->sc_dev,
    806  1.3.2.2  christos 			    "unhandled message type %u flags %#x\n", type,
    807  1.3.2.2  christos 			    msg->msg_flags);
    808  1.3.2.2  christos 		} else {
    809  1.3.2.2  christos 			if (vmbus_msg_dispatch[type].hmd_handler) {
    810  1.3.2.2  christos 				vmbus_msg_dispatch[type].hmd_handler(sc, hdr);
    811  1.3.2.2  christos 			} else {
    812  1.3.2.2  christos 				device_printf(sc->sc_dev,
    813  1.3.2.2  christos 				    "unhandled message type %u\n", type);
    814  1.3.2.2  christos 			}
    815  1.3.2.2  christos 		}
    816  1.3.2.2  christos 
    817  1.3.2.2  christos 		msg->msg_type = HYPERV_MSGTYPE_NONE;
    818  1.3.2.2  christos 		membar_sync();
    819  1.3.2.2  christos 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING)
    820  1.3.2.2  christos 			hyperv_send_eom();
    821  1.3.2.2  christos 	}
    822  1.3.2.2  christos }
    823  1.3.2.2  christos 
    824  1.3.2.2  christos static void
    825  1.3.2.2  christos vmbus_channel_response(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *rsphdr)
    826  1.3.2.2  christos {
    827  1.3.2.2  christos 	struct vmbus_msg *msg;
    828  1.3.2.2  christos 	struct vmbus_chanmsg_hdr *reqhdr;
    829  1.3.2.2  christos 	int req;
    830  1.3.2.2  christos 
    831  1.3.2.2  christos 	req = vmbus_msg_dispatch[rsphdr->chm_type].hmd_request;
    832  1.3.2.2  christos 	mutex_enter(&sc->sc_req_lock);
    833  1.3.2.2  christos 	TAILQ_FOREACH(msg, &sc->sc_reqs, msg_entry) {
    834  1.3.2.2  christos 		reqhdr = (struct vmbus_chanmsg_hdr *)&msg->msg_req.hc_data;
    835  1.3.2.2  christos 		if (reqhdr->chm_type == req) {
    836  1.3.2.2  christos 			TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry);
    837  1.3.2.2  christos 			break;
    838  1.3.2.2  christos 		}
    839  1.3.2.2  christos 	}
    840  1.3.2.2  christos 	mutex_exit(&sc->sc_req_lock);
    841  1.3.2.2  christos 	if (msg != NULL) {
    842  1.3.2.2  christos 		memcpy(msg->msg_rsp, rsphdr, msg->msg_rsplen);
    843  1.3.2.2  christos 		mutex_enter(&sc->sc_rsp_lock);
    844  1.3.2.2  christos 		TAILQ_INSERT_TAIL(&sc->sc_rsps, msg, msg_entry);
    845  1.3.2.2  christos 		mutex_exit(&sc->sc_rsp_lock);
    846  1.3.2.2  christos 		wakeup(msg);
    847  1.3.2.2  christos 	}
    848  1.3.2.2  christos }
    849  1.3.2.2  christos 
    850  1.3.2.2  christos static void
    851  1.3.2.2  christos vmbus_channel_offer(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    852  1.3.2.2  christos {
    853  1.3.2.2  christos 	struct vmbus_offer *co;
    854  1.3.2.2  christos 
    855  1.3.2.2  christos 	co = kmem_intr_zalloc(sizeof(*co), KM_NOSLEEP);
    856  1.3.2.2  christos 	if (co == NULL) {
    857  1.3.2.2  christos 		device_printf(sc->sc_dev, "couldn't allocate offer\n");
    858  1.3.2.2  christos 		return;
    859  1.3.2.2  christos 	}
    860  1.3.2.2  christos 
    861  1.3.2.2  christos 	memcpy(&co->co_chan, hdr, sizeof(co->co_chan));
    862  1.3.2.2  christos 
    863  1.3.2.2  christos 	mutex_enter(&sc->sc_offer_lock);
    864  1.3.2.2  christos 	SIMPLEQ_INSERT_TAIL(&sc->sc_offers, co, co_entry);
    865  1.3.2.2  christos 	mutex_exit(&sc->sc_offer_lock);
    866  1.3.2.2  christos }
    867  1.3.2.2  christos 
    868  1.3.2.2  christos static void
    869  1.3.2.2  christos vmbus_channel_rescind(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    870  1.3.2.2  christos {
    871  1.3.2.2  christos 	const struct vmbus_chanmsg_chrescind *cmd;
    872  1.3.2.2  christos 
    873  1.3.2.2  christos 	cmd = (const struct vmbus_chanmsg_chrescind *)hdr;
    874  1.3.2.2  christos 	device_printf(sc->sc_dev, "revoking channel %u\n", cmd->chm_chanid);
    875  1.3.2.2  christos }
    876  1.3.2.2  christos 
    877  1.3.2.2  christos static void
    878  1.3.2.2  christos vmbus_channel_delivered(struct vmbus_softc *sc, struct vmbus_chanmsg_hdr *hdr)
    879  1.3.2.2  christos {
    880  1.3.2.2  christos 
    881  1.3.2.2  christos 	atomic_or_32(&sc->sc_flags, VMBUS_SCFLAG_OFFERS_DELIVERED);
    882  1.3.2.2  christos 	wakeup(&sc->sc_offers);
    883  1.3.2.2  christos }
    884  1.3.2.2  christos 
    885  1.3.2.2  christos static void
    886  1.3.2.2  christos hyperv_guid_sprint(struct hyperv_guid *guid, char *str, size_t size)
    887  1.3.2.2  christos {
    888  1.3.2.2  christos 	static const struct {
    889  1.3.2.2  christos 		const struct hyperv_guid *guid;
    890  1.3.2.2  christos 		const char *ident;
    891  1.3.2.2  christos 	} map[] = {
    892  1.3.2.2  christos 		{ &hyperv_guid_network,		"network" },
    893  1.3.2.2  christos 		{ &hyperv_guid_ide,		"ide" },
    894  1.3.2.2  christos 		{ &hyperv_guid_scsi,		"scsi" },
    895  1.3.2.2  christos 		{ &hyperv_guid_shutdown,	"shutdown" },
    896  1.3.2.2  christos 		{ &hyperv_guid_timesync,	"timesync" },
    897  1.3.2.2  christos 		{ &hyperv_guid_heartbeat,	"heartbeat" },
    898  1.3.2.2  christos 		{ &hyperv_guid_kvp,		"kvp" },
    899  1.3.2.2  christos 		{ &hyperv_guid_vss,		"vss" },
    900  1.3.2.2  christos 		{ &hyperv_guid_dynmem,		"dynamic-memory" },
    901  1.3.2.2  christos 		{ &hyperv_guid_mouse,		"mouse" },
    902  1.3.2.2  christos 		{ &hyperv_guid_kbd,		"keyboard" },
    903  1.3.2.2  christos 		{ &hyperv_guid_video,		"video" },
    904  1.3.2.2  christos 		{ &hyperv_guid_fc,		"fiber-channel" },
    905  1.3.2.2  christos 		{ &hyperv_guid_fcopy,		"file-copy" },
    906  1.3.2.2  christos 		{ &hyperv_guid_pcie,		"pcie-passthrough" },
    907  1.3.2.2  christos 		{ &hyperv_guid_netdir,		"network-direct" },
    908  1.3.2.2  christos 		{ &hyperv_guid_rdesktop,	"remote-desktop" },
    909  1.3.2.2  christos 		{ &hyperv_guid_avma1,		"avma-1" },
    910  1.3.2.2  christos 		{ &hyperv_guid_avma2,		"avma-2" },
    911  1.3.2.2  christos 		{ &hyperv_guid_avma3,		"avma-3" },
    912  1.3.2.2  christos 		{ &hyperv_guid_avma4,		"avma-4" },
    913  1.3.2.2  christos 	};
    914  1.3.2.2  christos 	int i;
    915  1.3.2.2  christos 
    916  1.3.2.2  christos 	for (i = 0; i < __arraycount(map); i++) {
    917  1.3.2.2  christos 		if (memcmp(guid, map[i].guid, sizeof(*guid)) == 0) {
    918  1.3.2.2  christos 			strlcpy(str, map[i].ident, size);
    919  1.3.2.2  christos 			return;
    920  1.3.2.2  christos 		}
    921  1.3.2.2  christos 	}
    922  1.3.2.2  christos 	hyperv_guid2str(guid, str, size);
    923  1.3.2.2  christos }
    924  1.3.2.2  christos 
    925  1.3.2.2  christos static int
    926  1.3.2.2  christos vmbus_channel_scan_done(struct vmbus_softc *sc, struct vmbus_msg *msg __unused)
    927  1.3.2.2  christos {
    928  1.3.2.2  christos 
    929  1.3.2.2  christos 	return ISSET(sc->sc_flags, VMBUS_SCFLAG_OFFERS_DELIVERED);
    930  1.3.2.2  christos }
    931  1.3.2.2  christos 
    932  1.3.2.2  christos static int
    933  1.3.2.2  christos vmbus_channel_scan(struct vmbus_softc *sc)
    934  1.3.2.2  christos {
    935  1.3.2.2  christos 	struct vmbus_chanmsg_hdr hdr;
    936  1.3.2.2  christos 	struct vmbus_chanmsg_choffer rsp;
    937  1.3.2.2  christos 	struct vmbus_offer *co;
    938  1.3.2.2  christos 
    939  1.3.2.2  christos 	SIMPLEQ_INIT(&sc->sc_offers);
    940  1.3.2.2  christos 	mutex_init(&sc->sc_offer_lock, MUTEX_DEFAULT, IPL_NET);
    941  1.3.2.2  christos 
    942  1.3.2.2  christos 	memset(&hdr, 0, sizeof(hdr));
    943  1.3.2.2  christos 	hdr.chm_type = VMBUS_CHANMSG_CHREQUEST;
    944  1.3.2.2  christos 
    945  1.3.2.2  christos 	if (vmbus_cmd(sc, &hdr, sizeof(hdr), &rsp, sizeof(rsp),
    946  1.3.2.2  christos 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK))) {
    947  1.3.2.2  christos 		DPRINTF("%s: CHREQUEST failed\n", device_xname(sc->sc_dev));
    948  1.3.2.2  christos 		return -1;
    949  1.3.2.2  christos 	}
    950  1.3.2.2  christos 
    951  1.3.2.2  christos 	vmbus_wait(sc, vmbus_channel_scan_done, (struct vmbus_msg *)&hdr,
    952  1.3.2.2  christos 	    &sc->sc_offers, "hvscan");
    953  1.3.2.2  christos 
    954  1.3.2.2  christos 	TAILQ_INIT(&sc->sc_channels);
    955  1.3.2.2  christos 	mutex_init(&sc->sc_channel_lock, MUTEX_DEFAULT, IPL_NET);
    956  1.3.2.2  christos 
    957  1.3.2.2  christos 	mutex_enter(&sc->sc_offer_lock);
    958  1.3.2.2  christos 	while (!SIMPLEQ_EMPTY(&sc->sc_offers)) {
    959  1.3.2.2  christos 		co = SIMPLEQ_FIRST(&sc->sc_offers);
    960  1.3.2.2  christos 		SIMPLEQ_REMOVE_HEAD(&sc->sc_offers, co_entry);
    961  1.3.2.2  christos 		mutex_exit(&sc->sc_offer_lock);
    962  1.3.2.2  christos 
    963  1.3.2.2  christos 		vmbus_process_offer(sc, co);
    964  1.3.2.2  christos 		kmem_free(co, sizeof(*co));
    965  1.3.2.2  christos 
    966  1.3.2.2  christos 		mutex_enter(&sc->sc_offer_lock);
    967  1.3.2.2  christos 	}
    968  1.3.2.2  christos 	mutex_exit(&sc->sc_offer_lock);
    969  1.3.2.2  christos 
    970  1.3.2.2  christos 	return 0;
    971  1.3.2.2  christos }
    972  1.3.2.2  christos 
    973  1.3.2.2  christos static struct vmbus_channel *
    974  1.3.2.2  christos vmbus_channel_alloc(struct vmbus_softc *sc)
    975  1.3.2.2  christos {
    976  1.3.2.2  christos 	struct vmbus_channel *ch;
    977  1.3.2.2  christos 
    978  1.3.2.2  christos 	ch = kmem_zalloc(sizeof(*ch), cold ? KM_NOSLEEP : KM_SLEEP);
    979  1.3.2.2  christos 
    980  1.3.2.2  christos 	ch->ch_monprm = hyperv_dma_alloc(sc->sc_dmat, &ch->ch_monprm_dma,
    981  1.3.2.2  christos 	    sizeof(*ch->ch_monprm), 8, 0, 1);
    982  1.3.2.2  christos 	if (ch->ch_monprm == NULL) {
    983  1.3.2.2  christos 		device_printf(sc->sc_dev, "monprm alloc failed\n");
    984  1.3.2.2  christos 		kmem_free(ch, sizeof(*ch));
    985  1.3.2.2  christos 		return NULL;
    986  1.3.2.2  christos 	}
    987  1.3.2.2  christos 	memset(ch->ch_monprm, 0, sizeof(*ch->ch_monprm));
    988  1.3.2.2  christos 
    989  1.3.2.2  christos 	ch->ch_refs = 1;
    990  1.3.2.2  christos 	ch->ch_sc = sc;
    991  1.3.2.2  christos 	mutex_init(&ch->ch_subchannel_lock, MUTEX_DEFAULT, IPL_NET);
    992  1.3.2.2  christos 	TAILQ_INIT(&ch->ch_subchannels);
    993  1.3.2.2  christos 
    994  1.3.2.2  christos 	ch->ch_state = VMBUS_CHANSTATE_CLOSED;
    995  1.3.2.2  christos 
    996  1.3.2.2  christos 	return ch;
    997  1.3.2.2  christos }
    998  1.3.2.2  christos 
    999  1.3.2.2  christos static void
   1000  1.3.2.2  christos vmbus_channel_free(struct vmbus_channel *ch)
   1001  1.3.2.2  christos {
   1002  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1003  1.3.2.2  christos 
   1004  1.3.2.2  christos 	KASSERTMSG(TAILQ_EMPTY(&ch->ch_subchannels) &&
   1005  1.3.2.2  christos 	    ch->ch_subchannel_count == 0, "still owns sub-channels");
   1006  1.3.2.2  christos 	KASSERTMSG(ch->ch_state == 0 || ch->ch_state == VMBUS_CHANSTATE_CLOSED,
   1007  1.3.2.2  christos 	    "free busy channel");
   1008  1.3.2.2  christos 	KASSERTMSG(ch->ch_refs == 0, "channel %u: invalid refcnt %d",
   1009  1.3.2.2  christos 	    ch->ch_id, ch->ch_refs);
   1010  1.3.2.2  christos 
   1011  1.3.2.2  christos 	hyperv_dma_free(sc->sc_dmat, &ch->ch_monprm_dma);
   1012  1.3.2.2  christos 	mutex_destroy(&ch->ch_subchannel_lock);
   1013  1.3.2.2  christos 	/* XXX ch_evcnt */
   1014  1.3.2.2  christos 	softint_disestablish(ch->ch_taskq);
   1015  1.3.2.2  christos 	kmem_free(ch, sizeof(*ch));
   1016  1.3.2.2  christos }
   1017  1.3.2.2  christos 
   1018  1.3.2.2  christos static int
   1019  1.3.2.2  christos vmbus_channel_add(struct vmbus_channel *nch)
   1020  1.3.2.2  christos {
   1021  1.3.2.2  christos 	struct vmbus_softc *sc = nch->ch_sc;
   1022  1.3.2.2  christos 	struct vmbus_channel *ch;
   1023  1.3.2.2  christos 	u_int refs __diagused;
   1024  1.3.2.2  christos 
   1025  1.3.2.2  christos 	if (nch->ch_id == 0) {
   1026  1.3.2.2  christos 		device_printf(sc->sc_dev, "got channel 0 offer, discard\n");
   1027  1.3.2.2  christos 		return EINVAL;
   1028  1.3.2.2  christos 	} else if (nch->ch_id >= sc->sc_channel_max) {
   1029  1.3.2.2  christos 		device_printf(sc->sc_dev, "invalid channel %u offer\n",
   1030  1.3.2.2  christos 		    nch->ch_id);
   1031  1.3.2.2  christos 		return EINVAL;
   1032  1.3.2.2  christos 	}
   1033  1.3.2.2  christos 
   1034  1.3.2.2  christos 	mutex_enter(&sc->sc_channel_lock);
   1035  1.3.2.2  christos 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   1036  1.3.2.2  christos 		if (!memcmp(&ch->ch_type, &nch->ch_type, sizeof(ch->ch_type)) &&
   1037  1.3.2.2  christos 		    !memcmp(&ch->ch_inst, &nch->ch_inst, sizeof(ch->ch_inst)))
   1038  1.3.2.2  christos 			break;
   1039  1.3.2.2  christos 	}
   1040  1.3.2.2  christos 	if (VMBUS_CHAN_ISPRIMARY(nch)) {
   1041  1.3.2.2  christos 		if (ch == NULL) {
   1042  1.3.2.2  christos 			TAILQ_INSERT_TAIL(&sc->sc_channels, nch, ch_entry);
   1043  1.3.2.2  christos 			mutex_exit(&sc->sc_channel_lock);
   1044  1.3.2.2  christos 			goto done;
   1045  1.3.2.2  christos 		} else {
   1046  1.3.2.2  christos 			mutex_exit(&sc->sc_channel_lock);
   1047  1.3.2.2  christos 			device_printf(sc->sc_dev,
   1048  1.3.2.2  christos 			    "duplicated primary channel%u\n", nch->ch_id);
   1049  1.3.2.2  christos 			return EINVAL;
   1050  1.3.2.2  christos 		}
   1051  1.3.2.2  christos 	} else {
   1052  1.3.2.2  christos 		if (ch == NULL) {
   1053  1.3.2.2  christos 			mutex_exit(&sc->sc_channel_lock);
   1054  1.3.2.2  christos 			device_printf(sc->sc_dev, "no primary channel%u\n",
   1055  1.3.2.2  christos 			    nch->ch_id);
   1056  1.3.2.2  christos 			return EINVAL;
   1057  1.3.2.2  christos 		}
   1058  1.3.2.2  christos 	}
   1059  1.3.2.2  christos 	mutex_exit(&sc->sc_channel_lock);
   1060  1.3.2.2  christos 
   1061  1.3.2.2  christos 	KASSERT(!VMBUS_CHAN_ISPRIMARY(nch));
   1062  1.3.2.2  christos 	KASSERT(ch != NULL);
   1063  1.3.2.2  christos 
   1064  1.3.2.2  christos 	refs = atomic_add_int_nv(&nch->ch_refs, 1);
   1065  1.3.2.2  christos 	KASSERT(refs == 1);
   1066  1.3.2.2  christos 
   1067  1.3.2.2  christos 	nch->ch_primary_channel = ch;
   1068  1.3.2.2  christos 	nch->ch_dev = ch->ch_dev;
   1069  1.3.2.2  christos 
   1070  1.3.2.2  christos 	mutex_enter(&ch->ch_subchannel_lock);
   1071  1.3.2.2  christos 	TAILQ_INSERT_TAIL(&ch->ch_subchannels, nch, ch_subentry);
   1072  1.3.2.2  christos 	ch->ch_subchannel_count++;
   1073  1.3.2.2  christos 	mutex_exit(&ch->ch_subchannel_lock);
   1074  1.3.2.2  christos 	wakeup(ch);
   1075  1.3.2.2  christos 
   1076  1.3.2.2  christos done:
   1077  1.3.2.2  christos 	vmbus_channel_cpu_default(nch);
   1078  1.3.2.2  christos 
   1079  1.3.2.2  christos 	return 0;
   1080  1.3.2.2  christos }
   1081  1.3.2.2  christos 
   1082  1.3.2.2  christos void
   1083  1.3.2.2  christos vmbus_channel_cpu_set(struct vmbus_channel *ch, int cpu)
   1084  1.3.2.2  christos {
   1085  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1086  1.3.2.2  christos 
   1087  1.3.2.2  christos 	KASSERTMSG(cpu >= 0 && cpu < ncpu, "invalid cpu %d", cpu);
   1088  1.3.2.2  christos 
   1089  1.3.2.2  christos 	if (sc->sc_proto == VMBUS_VERSION_WS2008 ||
   1090  1.3.2.2  christos 	    sc->sc_proto == VMBUS_VERSION_WIN7) {
   1091  1.3.2.2  christos 		/* Only cpu0 is supported */
   1092  1.3.2.2  christos 		cpu = 0;
   1093  1.3.2.2  christos 	}
   1094  1.3.2.2  christos 
   1095  1.3.2.2  christos 	ch->ch_cpuid = cpu;
   1096  1.3.2.2  christos 	ch->ch_vcpu = sc->sc_percpu[cpu].vcpuid;
   1097  1.3.2.2  christos }
   1098  1.3.2.2  christos 
   1099  1.3.2.2  christos void
   1100  1.3.2.2  christos vmbus_channel_cpu_rr(struct vmbus_channel *ch)
   1101  1.3.2.2  christos {
   1102  1.3.2.2  christos 	static uint32_t vmbus_channel_nextcpu;
   1103  1.3.2.2  christos 	int cpu;
   1104  1.3.2.2  christos 
   1105  1.3.2.2  christos 	cpu = atomic_add_32_nv(&vmbus_channel_nextcpu, 1) % ncpu;
   1106  1.3.2.2  christos 	vmbus_channel_cpu_set(ch, cpu);
   1107  1.3.2.2  christos }
   1108  1.3.2.2  christos 
   1109  1.3.2.2  christos static void
   1110  1.3.2.2  christos vmbus_channel_cpu_default(struct vmbus_channel *ch)
   1111  1.3.2.2  christos {
   1112  1.3.2.2  christos 
   1113  1.3.2.2  christos         /*
   1114  1.3.2.2  christos 	 * By default, pin the channel to cpu0.  Devices having
   1115  1.3.2.2  christos 	 * special channel-cpu mapping requirement should call
   1116  1.3.2.2  christos 	 * vmbus_channel_cpu_{set,rr}().
   1117  1.3.2.2  christos 	 */
   1118  1.3.2.2  christos 	vmbus_channel_cpu_set(ch, 0);
   1119  1.3.2.2  christos }
   1120  1.3.2.2  christos 
   1121  1.3.2.2  christos static void
   1122  1.3.2.2  christos vmbus_process_offer(struct vmbus_softc *sc, struct vmbus_offer *co)
   1123  1.3.2.2  christos {
   1124  1.3.2.2  christos 	struct vmbus_channel *ch;
   1125  1.3.2.2  christos 
   1126  1.3.2.2  christos 	ch = vmbus_channel_alloc(sc);
   1127  1.3.2.2  christos 	if (ch == NULL) {
   1128  1.3.2.2  christos 		device_printf(sc->sc_dev, "allocate channel %u failed\n",
   1129  1.3.2.2  christos 		    co->co_chan.chm_chanid);
   1130  1.3.2.2  christos 		return;
   1131  1.3.2.2  christos 	}
   1132  1.3.2.2  christos 
   1133  1.3.2.2  christos 	/*
   1134  1.3.2.2  christos 	 * By default we setup state to enable batched reading.
   1135  1.3.2.2  christos 	 * A specific service can choose to disable this prior
   1136  1.3.2.2  christos 	 * to opening the channel.
   1137  1.3.2.2  christos 	 */
   1138  1.3.2.2  christos 	ch->ch_flags |= CHF_BATCHED;
   1139  1.3.2.2  christos 
   1140  1.3.2.2  christos 	hyperv_guid_sprint(&co->co_chan.chm_chtype, ch->ch_ident,
   1141  1.3.2.2  christos 	    sizeof(ch->ch_ident));
   1142  1.3.2.2  christos 
   1143  1.3.2.2  christos 	ch->ch_monprm->mp_connid = VMBUS_CONNID_EVENT;
   1144  1.3.2.2  christos 	if (sc->sc_proto > VMBUS_VERSION_WS2008)
   1145  1.3.2.2  christos 		ch->ch_monprm->mp_connid = co->co_chan.chm_connid;
   1146  1.3.2.2  christos 
   1147  1.3.2.2  christos 	if (co->co_chan.chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) {
   1148  1.3.2.2  christos 		ch->ch_mgroup = co->co_chan.chm_montrig / VMBUS_MONTRIG_LEN;
   1149  1.3.2.2  christos 		ch->ch_mindex = co->co_chan.chm_montrig % VMBUS_MONTRIG_LEN;
   1150  1.3.2.2  christos 		ch->ch_flags |= CHF_MONITOR;
   1151  1.3.2.2  christos 	}
   1152  1.3.2.2  christos 
   1153  1.3.2.2  christos 	ch->ch_id = co->co_chan.chm_chanid;
   1154  1.3.2.2  christos 	ch->ch_subidx = co->co_chan.chm_subidx;
   1155  1.3.2.2  christos 
   1156  1.3.2.2  christos 	memcpy(&ch->ch_type, &co->co_chan.chm_chtype, sizeof(ch->ch_type));
   1157  1.3.2.2  christos 	memcpy(&ch->ch_inst, &co->co_chan.chm_chinst, sizeof(ch->ch_inst));
   1158  1.3.2.2  christos 
   1159  1.3.2.2  christos 	if (VMBUS_CHAN_ISPRIMARY(ch)) {
   1160  1.3.2.2  christos 		/* set primary channel mgmt wq */
   1161  1.3.2.2  christos 	} else {
   1162  1.3.2.2  christos 		/* set sub channel mgmt wq */
   1163  1.3.2.2  christos 	}
   1164  1.3.2.2  christos 
   1165  1.3.2.2  christos 	if (vmbus_channel_add(ch) != 0) {
   1166  1.3.2.2  christos 		vmbus_channel_free(ch);
   1167  1.3.2.2  christos 		return;
   1168  1.3.2.2  christos 	}
   1169  1.3.2.2  christos 
   1170  1.3.2.2  christos 	ch->ch_state = VMBUS_CHANSTATE_OFFERED;
   1171  1.3.2.2  christos 
   1172  1.3.2.2  christos #ifdef HYPERV_DEBUG
   1173  1.3.2.2  christos 	printf("%s: channel %u: \"%s\"", device_xname(sc->sc_dev), ch->ch_id,
   1174  1.3.2.2  christos 	    ch->ch_ident);
   1175  1.3.2.2  christos 	if (ch->ch_flags & CHF_MONITOR)
   1176  1.3.2.2  christos 		printf(", monitor %u\n", co->co_chan.chm_montrig);
   1177  1.3.2.2  christos 	else
   1178  1.3.2.2  christos 		printf("\n");
   1179  1.3.2.2  christos #endif
   1180  1.3.2.2  christos }
   1181  1.3.2.2  christos 
   1182  1.3.2.2  christos static int
   1183  1.3.2.2  christos vmbus_channel_release(struct vmbus_channel *ch)
   1184  1.3.2.2  christos {
   1185  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1186  1.3.2.2  christos 	struct vmbus_chanmsg_chfree cmd;
   1187  1.3.2.2  christos 	int rv;
   1188  1.3.2.2  christos 
   1189  1.3.2.2  christos 	memset(&cmd, 0, sizeof(cmd));
   1190  1.3.2.2  christos 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHFREE;
   1191  1.3.2.2  christos 	cmd.chm_chanid = ch->ch_id;
   1192  1.3.2.2  christos 
   1193  1.3.2.2  christos 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), NULL, 0,
   1194  1.3.2.2  christos 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK));
   1195  1.3.2.2  christos 	if (rv) {
   1196  1.3.2.2  christos 		DPRINTF("%s: CHFREE failed with %d\n", device_xname(sc->sc_dev),
   1197  1.3.2.2  christos 		    rv);
   1198  1.3.2.2  christos 	}
   1199  1.3.2.2  christos 	return rv;
   1200  1.3.2.2  christos }
   1201  1.3.2.2  christos 
   1202  1.3.2.2  christos struct vmbus_channel **
   1203  1.3.2.2  christos vmbus_subchannel_get(struct vmbus_channel *prich, int cnt)
   1204  1.3.2.2  christos {
   1205  1.3.2.2  christos 	struct vmbus_channel **ret, *ch;
   1206  1.3.2.2  christos 	int i;
   1207  1.3.2.2  christos 
   1208  1.3.2.2  christos 	KASSERT(cnt > 0);
   1209  1.3.2.2  christos 
   1210  1.3.2.2  christos 	ret = kmem_alloc(sizeof(struct vmbus_channel *) * cnt,
   1211  1.3.2.2  christos 	    cold ? KM_NOSLEEP : KM_SLEEP);
   1212  1.3.2.2  christos 
   1213  1.3.2.2  christos 	mutex_enter(&prich->ch_subchannel_lock);
   1214  1.3.2.2  christos 
   1215  1.3.2.2  christos 	while (prich->ch_subchannel_count < cnt)
   1216  1.3.2.2  christos 		/* XXX use condvar(9) instead of mtsleep */
   1217  1.3.2.2  christos 		mtsleep(prich, PRIBIO, "hvvmsubch", 0,
   1218  1.3.2.2  christos 		    &prich->ch_subchannel_lock);
   1219  1.3.2.2  christos 
   1220  1.3.2.2  christos 	i = 0;
   1221  1.3.2.2  christos 	TAILQ_FOREACH(ch, &prich->ch_subchannels, ch_subentry) {
   1222  1.3.2.2  christos 		ret[i] = ch;	/* XXX inc refs */
   1223  1.3.2.2  christos 
   1224  1.3.2.2  christos 		if (++i == cnt)
   1225  1.3.2.2  christos 			break;
   1226  1.3.2.2  christos 	}
   1227  1.3.2.2  christos 
   1228  1.3.2.2  christos 	mutex_exit(&prich->ch_subchannel_lock);
   1229  1.3.2.2  christos 
   1230  1.3.2.2  christos 	return ret;
   1231  1.3.2.2  christos }
   1232  1.3.2.2  christos 
   1233  1.3.2.2  christos void
   1234  1.3.2.2  christos vmbus_subchannel_put(struct vmbus_channel **subch, int cnt)
   1235  1.3.2.2  christos {
   1236  1.3.2.2  christos 
   1237  1.3.2.2  christos 	kmem_free(subch, sizeof(struct vmbus_channel *) * cnt);
   1238  1.3.2.2  christos }
   1239  1.3.2.2  christos 
   1240  1.3.2.2  christos static struct vmbus_channel *
   1241  1.3.2.2  christos vmbus_channel_lookup(struct vmbus_softc *sc, uint32_t relid)
   1242  1.3.2.2  christos {
   1243  1.3.2.2  christos 	struct vmbus_channel *ch;
   1244  1.3.2.2  christos 
   1245  1.3.2.2  christos 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   1246  1.3.2.2  christos 		if (ch->ch_id == relid)
   1247  1.3.2.2  christos 			return ch;
   1248  1.3.2.2  christos 	}
   1249  1.3.2.2  christos 	return NULL;
   1250  1.3.2.2  christos }
   1251  1.3.2.2  christos 
   1252  1.3.2.2  christos static int
   1253  1.3.2.2  christos vmbus_channel_ring_create(struct vmbus_channel *ch, uint32_t buflen)
   1254  1.3.2.2  christos {
   1255  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1256  1.3.2.2  christos 
   1257  1.3.2.2  christos 	buflen = roundup(buflen, PAGE_SIZE) + sizeof(struct vmbus_bufring);
   1258  1.3.2.2  christos 	ch->ch_ring_size = 2 * buflen;
   1259  1.3.2.2  christos 	ch->ch_ring = hyperv_dma_alloc(sc->sc_dmat, &ch->ch_ring_dma,
   1260  1.3.2.2  christos 	    ch->ch_ring_size, PAGE_SIZE, 0, 1);	/* page aligned memory */
   1261  1.3.2.2  christos 	if (ch->ch_ring == NULL) {
   1262  1.3.2.2  christos 		device_printf(sc->sc_dev,
   1263  1.3.2.2  christos 		    "failed to allocate channel ring\n");
   1264  1.3.2.2  christos 		return ENOMEM;
   1265  1.3.2.2  christos 	}
   1266  1.3.2.2  christos 
   1267  1.3.2.2  christos 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
   1268  1.3.2.2  christos 	ch->ch_wrd.rd_ring = (struct vmbus_bufring *)ch->ch_ring;
   1269  1.3.2.2  christos 	ch->ch_wrd.rd_size = buflen;
   1270  1.3.2.2  christos 	ch->ch_wrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
   1271  1.3.2.2  christos 	mutex_init(&ch->ch_wrd.rd_lock, MUTEX_DEFAULT, IPL_NET);
   1272  1.3.2.2  christos 
   1273  1.3.2.2  christos 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
   1274  1.3.2.2  christos 	ch->ch_rrd.rd_ring = (struct vmbus_bufring *)((uint8_t *)ch->ch_ring +
   1275  1.3.2.2  christos 	    buflen);
   1276  1.3.2.2  christos 	ch->ch_rrd.rd_size = buflen;
   1277  1.3.2.2  christos 	ch->ch_rrd.rd_dsize = buflen - sizeof(struct vmbus_bufring);
   1278  1.3.2.2  christos 	mutex_init(&ch->ch_rrd.rd_lock, MUTEX_DEFAULT, IPL_NET);
   1279  1.3.2.2  christos 
   1280  1.3.2.2  christos 	if (vmbus_handle_alloc(ch, &ch->ch_ring_dma, ch->ch_ring_size,
   1281  1.3.2.2  christos 	    &ch->ch_ring_gpadl)) {
   1282  1.3.2.2  christos 		device_printf(sc->sc_dev,
   1283  1.3.2.2  christos 		    "failed to obtain a PA handle for the ring\n");
   1284  1.3.2.2  christos 		vmbus_channel_ring_destroy(ch);
   1285  1.3.2.2  christos 		return ENOMEM;
   1286  1.3.2.2  christos 	}
   1287  1.3.2.2  christos 
   1288  1.3.2.2  christos 	return 0;
   1289  1.3.2.2  christos }
   1290  1.3.2.2  christos 
   1291  1.3.2.2  christos static void
   1292  1.3.2.2  christos vmbus_channel_ring_destroy(struct vmbus_channel *ch)
   1293  1.3.2.2  christos {
   1294  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1295  1.3.2.2  christos 
   1296  1.3.2.2  christos 	hyperv_dma_free(sc->sc_dmat, &ch->ch_ring_dma);
   1297  1.3.2.2  christos 	ch->ch_ring = NULL;
   1298  1.3.2.2  christos 	vmbus_handle_free(ch, ch->ch_ring_gpadl);
   1299  1.3.2.2  christos 
   1300  1.3.2.2  christos 	mutex_destroy(&ch->ch_wrd.rd_lock);
   1301  1.3.2.2  christos 	memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd));
   1302  1.3.2.2  christos 	mutex_destroy(&ch->ch_rrd.rd_lock);
   1303  1.3.2.2  christos 	memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd));
   1304  1.3.2.2  christos }
   1305  1.3.2.2  christos 
   1306  1.3.2.2  christos int
   1307  1.3.2.2  christos vmbus_channel_open(struct vmbus_channel *ch, size_t buflen, void *udata,
   1308  1.3.2.2  christos     size_t udatalen, void (*handler)(void *), void *arg)
   1309  1.3.2.2  christos {
   1310  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1311  1.3.2.2  christos 	struct vmbus_chanmsg_chopen cmd;
   1312  1.3.2.2  christos 	struct vmbus_chanmsg_chopen_resp rsp;
   1313  1.3.2.2  christos 	int rv = EINVAL;
   1314  1.3.2.2  christos 
   1315  1.3.2.2  christos 	if (ch->ch_ring == NULL &&
   1316  1.3.2.2  christos 	    (rv = vmbus_channel_ring_create(ch, buflen))) {
   1317  1.3.2.2  christos 		DPRINTF("%s: failed to create channel ring\n",
   1318  1.3.2.2  christos 		    device_xname(sc->sc_dev));
   1319  1.3.2.2  christos 		return rv;
   1320  1.3.2.2  christos 	}
   1321  1.3.2.2  christos 
   1322  1.3.2.2  christos 	memset(&cmd, 0, sizeof(cmd));
   1323  1.3.2.2  christos 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHOPEN;
   1324  1.3.2.2  christos 	cmd.chm_openid = ch->ch_id;
   1325  1.3.2.2  christos 	cmd.chm_chanid = ch->ch_id;
   1326  1.3.2.2  christos 	cmd.chm_gpadl = ch->ch_ring_gpadl;
   1327  1.3.2.2  christos 	cmd.chm_txbr_pgcnt = atop(ch->ch_wrd.rd_size);
   1328  1.3.2.2  christos 	cmd.chm_vcpuid = ch->ch_vcpu;
   1329  1.3.2.2  christos 	if (udata && udatalen > 0)
   1330  1.3.2.2  christos 		memcpy(cmd.chm_udata, udata, udatalen);
   1331  1.3.2.2  christos 
   1332  1.3.2.2  christos 	memset(&rsp, 0, sizeof(rsp));
   1333  1.3.2.2  christos 
   1334  1.3.2.2  christos 	ch->ch_handler = handler;
   1335  1.3.2.2  christos 	ch->ch_ctx = arg;
   1336  1.3.2.2  christos 	ch->ch_state = VMBUS_CHANSTATE_OPENED;
   1337  1.3.2.2  christos 
   1338  1.3.2.2  christos 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
   1339  1.3.2.2  christos 	    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
   1340  1.3.2.2  christos 	if (rv) {
   1341  1.3.2.2  christos 		vmbus_channel_ring_destroy(ch);
   1342  1.3.2.2  christos 		DPRINTF("%s: CHOPEN failed with %d\n", device_xname(sc->sc_dev),
   1343  1.3.2.2  christos 		    rv);
   1344  1.3.2.2  christos 		ch->ch_handler = NULL;
   1345  1.3.2.2  christos 		ch->ch_ctx = NULL;
   1346  1.3.2.2  christos 		ch->ch_state = VMBUS_CHANSTATE_OFFERED;
   1347  1.3.2.2  christos 		return rv;
   1348  1.3.2.2  christos 	}
   1349  1.3.2.2  christos 	return 0;
   1350  1.3.2.2  christos }
   1351  1.3.2.2  christos 
   1352  1.3.2.2  christos static void
   1353  1.3.2.2  christos vmbus_channel_detach(struct vmbus_channel *ch)
   1354  1.3.2.2  christos {
   1355  1.3.2.2  christos 	u_int refs;
   1356  1.3.2.2  christos 
   1357  1.3.2.2  christos 	refs = atomic_add_int_nv(&ch->ch_refs, -1);
   1358  1.3.2.2  christos 	if (refs == 1) {
   1359  1.3.2.2  christos 		/* XXX on workqueue? */
   1360  1.3.2.2  christos 		if (VMBUS_CHAN_ISPRIMARY(ch)) {
   1361  1.3.2.2  christos 			vmbus_channel_release(ch);
   1362  1.3.2.2  christos 			vmbus_channel_free(ch);
   1363  1.3.2.2  christos 		} else {
   1364  1.3.2.2  christos 			struct vmbus_channel *prich = ch->ch_primary_channel;
   1365  1.3.2.2  christos 
   1366  1.3.2.2  christos 			vmbus_channel_release(ch);
   1367  1.3.2.2  christos 
   1368  1.3.2.2  christos 			mutex_enter(&prich->ch_subchannel_lock);
   1369  1.3.2.2  christos 			TAILQ_REMOVE(&prich->ch_subchannels, ch, ch_subentry);
   1370  1.3.2.2  christos 			prich->ch_subchannel_count--;
   1371  1.3.2.2  christos 			mutex_exit(&prich->ch_subchannel_lock);
   1372  1.3.2.2  christos 			wakeup(prich);
   1373  1.3.2.2  christos 
   1374  1.3.2.2  christos 			vmbus_channel_free(ch);
   1375  1.3.2.2  christos 		}
   1376  1.3.2.2  christos 	}
   1377  1.3.2.2  christos }
   1378  1.3.2.2  christos 
   1379  1.3.2.2  christos static int
   1380  1.3.2.2  christos vmbus_channel_close_internal(struct vmbus_channel *ch)
   1381  1.3.2.2  christos {
   1382  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1383  1.3.2.2  christos 	struct vmbus_chanmsg_chclose cmd;
   1384  1.3.2.2  christos 	int rv;
   1385  1.3.2.2  christos 
   1386  1.3.2.2  christos 	memset(&cmd, 0, sizeof(cmd));
   1387  1.3.2.2  christos 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHCLOSE;
   1388  1.3.2.2  christos 	cmd.chm_chanid = ch->ch_id;
   1389  1.3.2.2  christos 
   1390  1.3.2.2  christos 	ch->ch_state = VMBUS_CHANSTATE_CLOSING;
   1391  1.3.2.2  christos 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), NULL, 0,
   1392  1.3.2.2  christos 	    HCF_NOREPLY | (cold ? HCF_NOSLEEP : HCF_SLEEPOK));
   1393  1.3.2.2  christos 	if (rv) {
   1394  1.3.2.2  christos 		DPRINTF("%s: CHCLOSE failed with %d\n",
   1395  1.3.2.2  christos 		    device_xname(sc->sc_dev), rv);
   1396  1.3.2.2  christos 		return rv;
   1397  1.3.2.2  christos 	}
   1398  1.3.2.2  christos 	ch->ch_state = VMBUS_CHANSTATE_CLOSED;
   1399  1.3.2.2  christos 	vmbus_channel_ring_destroy(ch);
   1400  1.3.2.2  christos 	return 0;
   1401  1.3.2.2  christos }
   1402  1.3.2.2  christos 
   1403  1.3.2.2  christos int
   1404  1.3.2.2  christos vmbus_channel_close_direct(struct vmbus_channel *ch)
   1405  1.3.2.2  christos {
   1406  1.3.2.2  christos 	int rv;
   1407  1.3.2.2  christos 
   1408  1.3.2.2  christos 	rv = vmbus_channel_close_internal(ch);
   1409  1.3.2.2  christos 	if (!VMBUS_CHAN_ISPRIMARY(ch))
   1410  1.3.2.2  christos 		vmbus_channel_detach(ch);
   1411  1.3.2.2  christos 	return rv;
   1412  1.3.2.2  christos }
   1413  1.3.2.2  christos 
   1414  1.3.2.2  christos int
   1415  1.3.2.2  christos vmbus_channel_close(struct vmbus_channel *ch)
   1416  1.3.2.2  christos {
   1417  1.3.2.2  christos 	struct vmbus_channel **subch;
   1418  1.3.2.2  christos 	int i, cnt, rv;
   1419  1.3.2.2  christos 
   1420  1.3.2.2  christos 	if (!VMBUS_CHAN_ISPRIMARY(ch))
   1421  1.3.2.2  christos 		return 0;
   1422  1.3.2.2  christos 
   1423  1.3.2.2  christos 	cnt = ch->ch_subchannel_count;
   1424  1.3.2.2  christos 	if (cnt > 0) {
   1425  1.3.2.2  christos 		subch = vmbus_subchannel_get(ch, cnt);
   1426  1.3.2.2  christos 		for (i = 0; i < ch->ch_subchannel_count; i++) {
   1427  1.3.2.2  christos 			rv = vmbus_channel_close_internal(subch[i]);
   1428  1.3.2.2  christos 			(void) rv;	/* XXX */
   1429  1.3.2.2  christos 			vmbus_channel_detach(ch);
   1430  1.3.2.2  christos 		}
   1431  1.3.2.2  christos 		vmbus_subchannel_put(subch, cnt);
   1432  1.3.2.2  christos 	}
   1433  1.3.2.2  christos 
   1434  1.3.2.2  christos 	return vmbus_channel_close_internal(ch);
   1435  1.3.2.2  christos }
   1436  1.3.2.2  christos 
   1437  1.3.2.2  christos static inline void
   1438  1.3.2.2  christos vmbus_channel_setevent(struct vmbus_softc *sc, struct vmbus_channel *ch)
   1439  1.3.2.2  christos {
   1440  1.3.2.2  christos 	struct vmbus_mon_trig *mtg;
   1441  1.3.2.2  christos 
   1442  1.3.2.2  christos 	/* Each uint32_t represents 32 channels */
   1443  1.3.2.2  christos 	set_bit(ch->ch_id, sc->sc_wevents);
   1444  1.3.2.2  christos 	if (ch->ch_flags & CHF_MONITOR) {
   1445  1.3.2.2  christos 		mtg = &sc->sc_monitor[1]->mnf_trigs[ch->ch_mgroup];
   1446  1.3.2.2  christos 		set_bit(ch->ch_mindex, &mtg->mt_pending);
   1447  1.3.2.2  christos 	} else
   1448  1.3.2.2  christos 		vmbus_intr_signal(sc, hyperv_dma_get_paddr(&ch->ch_monprm_dma));
   1449  1.3.2.2  christos }
   1450  1.3.2.2  christos 
   1451  1.3.2.2  christos static void
   1452  1.3.2.2  christos vmbus_channel_intr(void *arg)
   1453  1.3.2.2  christos {
   1454  1.3.2.2  christos 	struct vmbus_channel *ch = arg;
   1455  1.3.2.2  christos 
   1456  1.3.2.2  christos 	if (vmbus_channel_ready(ch))
   1457  1.3.2.2  christos 		ch->ch_handler(ch->ch_ctx);
   1458  1.3.2.2  christos 
   1459  1.3.2.2  christos 	if (vmbus_channel_unpause(ch) == 0)
   1460  1.3.2.2  christos 		return;
   1461  1.3.2.2  christos 
   1462  1.3.2.2  christos 	vmbus_channel_pause(ch);
   1463  1.3.2.2  christos 	vmbus_channel_schedule(ch);
   1464  1.3.2.2  christos }
   1465  1.3.2.2  christos 
   1466  1.3.2.2  christos int
   1467  1.3.2.2  christos vmbus_channel_setdeferred(struct vmbus_channel *ch, const char *name)
   1468  1.3.2.2  christos {
   1469  1.3.2.2  christos 
   1470  1.3.2.2  christos 	ch->ch_taskq = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1471  1.3.2.2  christos 	    vmbus_channel_intr, ch);
   1472  1.3.2.2  christos 	if (ch->ch_taskq == NULL)
   1473  1.3.2.2  christos 		return -1;
   1474  1.3.2.2  christos 	return 0;
   1475  1.3.2.2  christos }
   1476  1.3.2.2  christos 
   1477  1.3.2.2  christos void
   1478  1.3.2.2  christos vmbus_channel_schedule(struct vmbus_channel *ch)
   1479  1.3.2.2  christos {
   1480  1.3.2.2  christos 
   1481  1.3.2.2  christos 	if (ch->ch_handler) {
   1482  1.3.2.2  christos 		if (!cold && (ch->ch_flags & CHF_BATCHED)) {
   1483  1.3.2.2  christos 			vmbus_channel_pause(ch);
   1484  1.3.2.2  christos 			softint_schedule(ch->ch_taskq);
   1485  1.3.2.2  christos 		} else
   1486  1.3.2.2  christos 			ch->ch_handler(ch->ch_ctx);
   1487  1.3.2.2  christos 	}
   1488  1.3.2.2  christos }
   1489  1.3.2.2  christos 
   1490  1.3.2.2  christos static __inline void
   1491  1.3.2.2  christos vmbus_ring_put(struct vmbus_ring_data *wrd, uint8_t *data, uint32_t datalen)
   1492  1.3.2.2  christos {
   1493  1.3.2.2  christos 	int left = MIN(datalen, wrd->rd_dsize - wrd->rd_prod);
   1494  1.3.2.2  christos 
   1495  1.3.2.2  christos 	memcpy(&wrd->rd_ring->br_data[wrd->rd_prod], data, left);
   1496  1.3.2.2  christos 	memcpy(&wrd->rd_ring->br_data[0], data + left, datalen - left);
   1497  1.3.2.2  christos 	wrd->rd_prod += datalen;
   1498  1.3.2.2  christos 	if (wrd->rd_prod >= wrd->rd_dsize)
   1499  1.3.2.2  christos 		wrd->rd_prod -= wrd->rd_dsize;
   1500  1.3.2.2  christos }
   1501  1.3.2.2  christos 
   1502  1.3.2.2  christos static inline void
   1503  1.3.2.2  christos vmbus_ring_get(struct vmbus_ring_data *rrd, uint8_t *data, uint32_t datalen,
   1504  1.3.2.2  christos     int peek)
   1505  1.3.2.2  christos {
   1506  1.3.2.2  christos 	int left = MIN(datalen, rrd->rd_dsize - rrd->rd_cons);
   1507  1.3.2.2  christos 
   1508  1.3.2.2  christos 	memcpy(data, &rrd->rd_ring->br_data[rrd->rd_cons], left);
   1509  1.3.2.2  christos 	memcpy(data + left, &rrd->rd_ring->br_data[0], datalen - left);
   1510  1.3.2.2  christos 	if (!peek) {
   1511  1.3.2.2  christos 		rrd->rd_cons += datalen;
   1512  1.3.2.2  christos 		if (rrd->rd_cons >= rrd->rd_dsize)
   1513  1.3.2.2  christos 			rrd->rd_cons -= rrd->rd_dsize;
   1514  1.3.2.2  christos 	}
   1515  1.3.2.2  christos }
   1516  1.3.2.2  christos 
   1517  1.3.2.2  christos static __inline void
   1518  1.3.2.2  christos vmbus_ring_avail(struct vmbus_ring_data *rd, uint32_t *towrite,
   1519  1.3.2.2  christos     uint32_t *toread)
   1520  1.3.2.2  christos {
   1521  1.3.2.2  christos 	uint32_t ridx = rd->rd_ring->br_rindex;
   1522  1.3.2.2  christos 	uint32_t widx = rd->rd_ring->br_windex;
   1523  1.3.2.2  christos 	uint32_t r, w;
   1524  1.3.2.2  christos 
   1525  1.3.2.2  christos 	if (widx >= ridx)
   1526  1.3.2.2  christos 		w = rd->rd_dsize - (widx - ridx);
   1527  1.3.2.2  christos 	else
   1528  1.3.2.2  christos 		w = ridx - widx;
   1529  1.3.2.2  christos 	r = rd->rd_dsize - w;
   1530  1.3.2.2  christos 	if (towrite)
   1531  1.3.2.2  christos 		*towrite = w;
   1532  1.3.2.2  christos 	if (toread)
   1533  1.3.2.2  christos 		*toread = r;
   1534  1.3.2.2  christos }
   1535  1.3.2.2  christos 
   1536  1.3.2.2  christos static int
   1537  1.3.2.2  christos vmbus_ring_write(struct vmbus_ring_data *wrd, struct iovec *iov, int iov_cnt,
   1538  1.3.2.2  christos     int *needsig)
   1539  1.3.2.2  christos {
   1540  1.3.2.2  christos 	uint64_t indices = 0;
   1541  1.3.2.2  christos 	uint32_t avail, oprod, datalen = sizeof(indices);
   1542  1.3.2.2  christos 	int i;
   1543  1.3.2.2  christos 
   1544  1.3.2.2  christos 	for (i = 0; i < iov_cnt; i++)
   1545  1.3.2.2  christos 		datalen += iov[i].iov_len;
   1546  1.3.2.2  christos 
   1547  1.3.2.2  christos 	KASSERT(datalen <= wrd->rd_dsize);
   1548  1.3.2.2  christos 
   1549  1.3.2.2  christos 	vmbus_ring_avail(wrd, &avail, NULL);
   1550  1.3.2.2  christos 	if (avail <= datalen) {
   1551  1.3.2.2  christos 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
   1552  1.3.2.2  christos 		return EAGAIN;
   1553  1.3.2.2  christos 	}
   1554  1.3.2.2  christos 
   1555  1.3.2.2  christos 	oprod = wrd->rd_prod;
   1556  1.3.2.2  christos 
   1557  1.3.2.2  christos 	for (i = 0; i < iov_cnt; i++)
   1558  1.3.2.2  christos 		vmbus_ring_put(wrd, iov[i].iov_base, iov[i].iov_len);
   1559  1.3.2.2  christos 
   1560  1.3.2.2  christos 	indices = (uint64_t)oprod << 32;
   1561  1.3.2.2  christos 	vmbus_ring_put(wrd, (uint8_t *)&indices, sizeof(indices));
   1562  1.3.2.2  christos 
   1563  1.3.2.2  christos 	membar_sync();
   1564  1.3.2.2  christos 	wrd->rd_ring->br_windex = wrd->rd_prod;
   1565  1.3.2.2  christos 	membar_sync();
   1566  1.3.2.2  christos 
   1567  1.3.2.2  christos 	/* Signal when the ring transitions from being empty to non-empty */
   1568  1.3.2.2  christos 	if (wrd->rd_ring->br_imask == 0 &&
   1569  1.3.2.2  christos 	    wrd->rd_ring->br_rindex == oprod)
   1570  1.3.2.2  christos 		*needsig = 1;
   1571  1.3.2.2  christos 	else
   1572  1.3.2.2  christos 		*needsig = 0;
   1573  1.3.2.2  christos 
   1574  1.3.2.2  christos 	return 0;
   1575  1.3.2.2  christos }
   1576  1.3.2.2  christos 
   1577  1.3.2.2  christos int
   1578  1.3.2.2  christos vmbus_channel_send(struct vmbus_channel *ch, void *data, uint32_t datalen,
   1579  1.3.2.2  christos     uint64_t rid, int type, uint32_t flags)
   1580  1.3.2.2  christos {
   1581  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1582  1.3.2.2  christos 	struct vmbus_chanpkt cp;
   1583  1.3.2.2  christos 	struct iovec iov[3];
   1584  1.3.2.2  christos 	uint32_t pktlen, pktlen_aligned;
   1585  1.3.2.2  christos 	uint64_t zeropad = 0;
   1586  1.3.2.2  christos 	int rv, needsig = 0;
   1587  1.3.2.2  christos 
   1588  1.3.2.2  christos 	pktlen = sizeof(cp) + datalen;
   1589  1.3.2.2  christos 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1590  1.3.2.2  christos 
   1591  1.3.2.2  christos 	cp.cp_hdr.cph_type = type;
   1592  1.3.2.2  christos 	cp.cp_hdr.cph_flags = flags;
   1593  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp));
   1594  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1595  1.3.2.2  christos 	cp.cp_hdr.cph_tid = rid;
   1596  1.3.2.2  christos 
   1597  1.3.2.2  christos 	iov[0].iov_base = &cp;
   1598  1.3.2.2  christos 	iov[0].iov_len = sizeof(cp);
   1599  1.3.2.2  christos 
   1600  1.3.2.2  christos 	iov[1].iov_base = data;
   1601  1.3.2.2  christos 	iov[1].iov_len = datalen;
   1602  1.3.2.2  christos 
   1603  1.3.2.2  christos 	iov[2].iov_base = &zeropad;
   1604  1.3.2.2  christos 	iov[2].iov_len = pktlen_aligned - pktlen;
   1605  1.3.2.2  christos 
   1606  1.3.2.2  christos 	mutex_enter(&ch->ch_wrd.rd_lock);
   1607  1.3.2.2  christos 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 3, &needsig);
   1608  1.3.2.2  christos 	mutex_exit(&ch->ch_wrd.rd_lock);
   1609  1.3.2.2  christos 	if (rv == 0 && needsig)
   1610  1.3.2.2  christos 		vmbus_channel_setevent(sc, ch);
   1611  1.3.2.2  christos 
   1612  1.3.2.2  christos 	return rv;
   1613  1.3.2.2  christos }
   1614  1.3.2.2  christos 
   1615  1.3.2.2  christos int
   1616  1.3.2.2  christos vmbus_channel_send_sgl(struct vmbus_channel *ch, struct vmbus_gpa *sgl,
   1617  1.3.2.2  christos     uint32_t nsge, void *data, uint32_t datalen, uint64_t rid)
   1618  1.3.2.2  christos {
   1619  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1620  1.3.2.2  christos 	struct vmbus_chanpkt_sglist cp;
   1621  1.3.2.2  christos 	struct iovec iov[4];
   1622  1.3.2.2  christos 	uint32_t buflen, pktlen, pktlen_aligned;
   1623  1.3.2.2  christos 	uint64_t zeropad = 0;
   1624  1.3.2.2  christos 	int rv, needsig = 0;
   1625  1.3.2.2  christos 
   1626  1.3.2.2  christos 	buflen = sizeof(struct vmbus_gpa) * nsge;
   1627  1.3.2.2  christos 	pktlen = sizeof(cp) + datalen + buflen;
   1628  1.3.2.2  christos 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1629  1.3.2.2  christos 
   1630  1.3.2.2  christos 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
   1631  1.3.2.2  christos 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
   1632  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
   1633  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1634  1.3.2.2  christos 	cp.cp_hdr.cph_tid = rid;
   1635  1.3.2.2  christos 	cp.cp_gpa_cnt = nsge;
   1636  1.3.2.2  christos 	cp.cp_rsvd = 0;
   1637  1.3.2.2  christos 
   1638  1.3.2.2  christos 	iov[0].iov_base = &cp;
   1639  1.3.2.2  christos 	iov[0].iov_len = sizeof(cp);
   1640  1.3.2.2  christos 
   1641  1.3.2.2  christos 	iov[1].iov_base = sgl;
   1642  1.3.2.2  christos 	iov[1].iov_len = buflen;
   1643  1.3.2.2  christos 
   1644  1.3.2.2  christos 	iov[2].iov_base = data;
   1645  1.3.2.2  christos 	iov[2].iov_len = datalen;
   1646  1.3.2.2  christos 
   1647  1.3.2.2  christos 	iov[3].iov_base = &zeropad;
   1648  1.3.2.2  christos 	iov[3].iov_len = pktlen_aligned - pktlen;
   1649  1.3.2.2  christos 
   1650  1.3.2.2  christos 	mutex_enter(&ch->ch_wrd.rd_lock);
   1651  1.3.2.2  christos 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 4, &needsig);
   1652  1.3.2.2  christos 	mutex_exit(&ch->ch_wrd.rd_lock);
   1653  1.3.2.2  christos 	if (rv == 0 && needsig)
   1654  1.3.2.2  christos 		vmbus_channel_setevent(sc, ch);
   1655  1.3.2.2  christos 
   1656  1.3.2.2  christos 	return rv;
   1657  1.3.2.2  christos }
   1658  1.3.2.2  christos 
   1659  1.3.2.2  christos int
   1660  1.3.2.2  christos vmbus_channel_send_prpl(struct vmbus_channel *ch, struct vmbus_gpa_range *prpl,
   1661  1.3.2.2  christos     uint32_t nprp, void *data, uint32_t datalen, uint64_t rid)
   1662  1.3.2.2  christos {
   1663  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1664  1.3.2.2  christos 	struct vmbus_chanpkt_prplist cp;
   1665  1.3.2.2  christos 	struct iovec iov[4];
   1666  1.3.2.2  christos 	uint32_t buflen, pktlen, pktlen_aligned;
   1667  1.3.2.2  christos 	uint64_t zeropad = 0;
   1668  1.3.2.2  christos 	int rv, needsig = 0;
   1669  1.3.2.2  christos 
   1670  1.3.2.2  christos 	buflen = sizeof(struct vmbus_gpa_range) * (nprp + 1);
   1671  1.3.2.2  christos 	pktlen = sizeof(cp) + datalen + buflen;
   1672  1.3.2.2  christos 	pktlen_aligned = roundup(pktlen, sizeof(uint64_t));
   1673  1.3.2.2  christos 
   1674  1.3.2.2  christos 	cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA;
   1675  1.3.2.2  christos 	cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC;
   1676  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen);
   1677  1.3.2.2  christos 	VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned);
   1678  1.3.2.2  christos 	cp.cp_hdr.cph_tid = rid;
   1679  1.3.2.2  christos 	cp.cp_range_cnt = 1;
   1680  1.3.2.2  christos 	cp.cp_rsvd = 0;
   1681  1.3.2.2  christos 
   1682  1.3.2.2  christos 	iov[0].iov_base = &cp;
   1683  1.3.2.2  christos 	iov[0].iov_len = sizeof(cp);
   1684  1.3.2.2  christos 
   1685  1.3.2.2  christos 	iov[1].iov_base = prpl;
   1686  1.3.2.2  christos 	iov[1].iov_len = buflen;
   1687  1.3.2.2  christos 
   1688  1.3.2.2  christos 	iov[2].iov_base = data;
   1689  1.3.2.2  christos 	iov[2].iov_len = datalen;
   1690  1.3.2.2  christos 
   1691  1.3.2.2  christos 	iov[3].iov_base = &zeropad;
   1692  1.3.2.2  christos 	iov[3].iov_len = pktlen_aligned - pktlen;
   1693  1.3.2.2  christos 
   1694  1.3.2.2  christos 	mutex_enter(&ch->ch_wrd.rd_lock);
   1695  1.3.2.2  christos 	rv = vmbus_ring_write(&ch->ch_wrd, iov, 4, &needsig);
   1696  1.3.2.2  christos 	mutex_exit(&ch->ch_wrd.rd_lock);
   1697  1.3.2.2  christos 	if (rv == 0 && needsig)
   1698  1.3.2.2  christos 		vmbus_channel_setevent(sc, ch);
   1699  1.3.2.2  christos 
   1700  1.3.2.2  christos 	return rv;
   1701  1.3.2.2  christos }
   1702  1.3.2.2  christos 
   1703  1.3.2.2  christos static int
   1704  1.3.2.2  christos vmbus_ring_peek(struct vmbus_ring_data *rrd, void *data, uint32_t datalen)
   1705  1.3.2.2  christos {
   1706  1.3.2.2  christos 	uint32_t avail;
   1707  1.3.2.2  christos 
   1708  1.3.2.2  christos 	KASSERT(datalen <= rrd->rd_dsize);
   1709  1.3.2.2  christos 
   1710  1.3.2.2  christos 	vmbus_ring_avail(rrd, NULL, &avail);
   1711  1.3.2.2  christos 	if (avail < datalen)
   1712  1.3.2.2  christos 		return EAGAIN;
   1713  1.3.2.2  christos 
   1714  1.3.2.2  christos 	vmbus_ring_get(rrd, (uint8_t *)data, datalen, 1);
   1715  1.3.2.2  christos 	return 0;
   1716  1.3.2.2  christos }
   1717  1.3.2.2  christos 
   1718  1.3.2.2  christos static int
   1719  1.3.2.2  christos vmbus_ring_read(struct vmbus_ring_data *rrd, void *data, uint32_t datalen,
   1720  1.3.2.2  christos     uint32_t offset)
   1721  1.3.2.2  christos {
   1722  1.3.2.2  christos 	uint64_t indices;
   1723  1.3.2.2  christos 	uint32_t avail;
   1724  1.3.2.2  christos 
   1725  1.3.2.2  christos 	KASSERT(datalen <= rrd->rd_dsize);
   1726  1.3.2.2  christos 
   1727  1.3.2.2  christos 	vmbus_ring_avail(rrd, NULL, &avail);
   1728  1.3.2.2  christos 	if (avail < datalen) {
   1729  1.3.2.2  christos 		DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen);
   1730  1.3.2.2  christos 		return EAGAIN;
   1731  1.3.2.2  christos 	}
   1732  1.3.2.2  christos 
   1733  1.3.2.2  christos 	if (offset) {
   1734  1.3.2.2  christos 		rrd->rd_cons += offset;
   1735  1.3.2.2  christos 		if (rrd->rd_cons >= rrd->rd_dsize)
   1736  1.3.2.2  christos 			rrd->rd_cons -= rrd->rd_dsize;
   1737  1.3.2.2  christos 	}
   1738  1.3.2.2  christos 
   1739  1.3.2.2  christos 	vmbus_ring_get(rrd, (uint8_t *)data, datalen, 0);
   1740  1.3.2.2  christos 	vmbus_ring_get(rrd, (uint8_t *)&indices, sizeof(indices), 0);
   1741  1.3.2.2  christos 
   1742  1.3.2.2  christos 	membar_sync();
   1743  1.3.2.2  christos 	rrd->rd_ring->br_rindex = rrd->rd_cons;
   1744  1.3.2.2  christos 
   1745  1.3.2.2  christos 	return 0;
   1746  1.3.2.2  christos }
   1747  1.3.2.2  christos 
   1748  1.3.2.2  christos int
   1749  1.3.2.2  christos vmbus_channel_recv(struct vmbus_channel *ch, void *data, uint32_t datalen,
   1750  1.3.2.2  christos     uint32_t *rlen, uint64_t *rid, int raw)
   1751  1.3.2.2  christos {
   1752  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1753  1.3.2.2  christos 	struct vmbus_chanpkt_hdr cph;
   1754  1.3.2.2  christos 	uint32_t offset, pktlen;
   1755  1.3.2.2  christos 	int rv;
   1756  1.3.2.2  christos 
   1757  1.3.2.2  christos 	*rlen = 0;
   1758  1.3.2.2  christos 
   1759  1.3.2.2  christos 	mutex_enter(&ch->ch_rrd.rd_lock);
   1760  1.3.2.2  christos 
   1761  1.3.2.2  christos 	if ((rv = vmbus_ring_peek(&ch->ch_rrd, &cph, sizeof(cph))) != 0) {
   1762  1.3.2.2  christos 		mutex_exit(&ch->ch_rrd.rd_lock);
   1763  1.3.2.2  christos 		return rv;
   1764  1.3.2.2  christos 	}
   1765  1.3.2.2  christos 
   1766  1.3.2.2  christos 	offset = raw ? 0 : VMBUS_CHANPKT_GETLEN(cph.cph_hlen);
   1767  1.3.2.2  christos 	pktlen = VMBUS_CHANPKT_GETLEN(cph.cph_tlen) - offset;
   1768  1.3.2.2  christos 	if (pktlen > datalen) {
   1769  1.3.2.2  christos 		mutex_exit(&ch->ch_rrd.rd_lock);
   1770  1.3.2.2  christos 		device_printf(sc->sc_dev, "%s: pktlen %u datalen %u\n",
   1771  1.3.2.2  christos 		    __func__, pktlen, datalen);
   1772  1.3.2.2  christos 		return EINVAL;
   1773  1.3.2.2  christos 	}
   1774  1.3.2.2  christos 
   1775  1.3.2.2  christos 	rv = vmbus_ring_read(&ch->ch_rrd, data, pktlen, offset);
   1776  1.3.2.2  christos 	if (rv == 0) {
   1777  1.3.2.2  christos 		*rlen = pktlen;
   1778  1.3.2.2  christos 		*rid = cph.cph_tid;
   1779  1.3.2.2  christos 	}
   1780  1.3.2.2  christos 
   1781  1.3.2.2  christos 	mutex_exit(&ch->ch_rrd.rd_lock);
   1782  1.3.2.2  christos 
   1783  1.3.2.2  christos 	return rv;
   1784  1.3.2.2  christos }
   1785  1.3.2.2  christos 
   1786  1.3.2.2  christos static inline void
   1787  1.3.2.2  christos vmbus_ring_mask(struct vmbus_ring_data *rd)
   1788  1.3.2.2  christos {
   1789  1.3.2.2  christos 
   1790  1.3.2.2  christos 	membar_sync();
   1791  1.3.2.2  christos 	rd->rd_ring->br_imask = 1;
   1792  1.3.2.2  christos 	membar_sync();
   1793  1.3.2.2  christos }
   1794  1.3.2.2  christos 
   1795  1.3.2.2  christos static inline void
   1796  1.3.2.2  christos vmbus_ring_unmask(struct vmbus_ring_data *rd)
   1797  1.3.2.2  christos {
   1798  1.3.2.2  christos 
   1799  1.3.2.2  christos 	membar_sync();
   1800  1.3.2.2  christos 	rd->rd_ring->br_imask = 0;
   1801  1.3.2.2  christos 	membar_sync();
   1802  1.3.2.2  christos }
   1803  1.3.2.2  christos 
   1804  1.3.2.2  christos static void
   1805  1.3.2.2  christos vmbus_channel_pause(struct vmbus_channel *ch)
   1806  1.3.2.2  christos {
   1807  1.3.2.2  christos 
   1808  1.3.2.2  christos 	vmbus_ring_mask(&ch->ch_rrd);
   1809  1.3.2.2  christos }
   1810  1.3.2.2  christos 
   1811  1.3.2.2  christos static uint32_t
   1812  1.3.2.2  christos vmbus_channel_unpause(struct vmbus_channel *ch)
   1813  1.3.2.2  christos {
   1814  1.3.2.2  christos 	uint32_t avail;
   1815  1.3.2.2  christos 
   1816  1.3.2.2  christos 	vmbus_ring_unmask(&ch->ch_rrd);
   1817  1.3.2.2  christos 	vmbus_ring_avail(&ch->ch_rrd, NULL, &avail);
   1818  1.3.2.2  christos 
   1819  1.3.2.2  christos 	return avail;
   1820  1.3.2.2  christos }
   1821  1.3.2.2  christos 
   1822  1.3.2.2  christos static uint32_t
   1823  1.3.2.2  christos vmbus_channel_ready(struct vmbus_channel *ch)
   1824  1.3.2.2  christos {
   1825  1.3.2.2  christos 	uint32_t avail;
   1826  1.3.2.2  christos 
   1827  1.3.2.2  christos 	vmbus_ring_avail(&ch->ch_rrd, NULL, &avail);
   1828  1.3.2.2  christos 
   1829  1.3.2.2  christos 	return avail;
   1830  1.3.2.2  christos }
   1831  1.3.2.2  christos 
   1832  1.3.2.2  christos /* How many PFNs can be referenced by the header */
   1833  1.3.2.2  christos #define VMBUS_NPFNHDR	((VMBUS_MSG_DSIZE_MAX -	\
   1834  1.3.2.2  christos 	  sizeof(struct vmbus_chanmsg_gpadl_conn)) / sizeof(uint64_t))
   1835  1.3.2.2  christos 
   1836  1.3.2.2  christos /* How many PFNs can be referenced by the body */
   1837  1.3.2.2  christos #define VMBUS_NPFNBODY	((VMBUS_MSG_DSIZE_MAX -	\
   1838  1.3.2.2  christos 	  sizeof(struct vmbus_chanmsg_gpadl_subconn)) / sizeof(uint64_t))
   1839  1.3.2.2  christos 
   1840  1.3.2.2  christos int
   1841  1.3.2.2  christos vmbus_handle_alloc(struct vmbus_channel *ch, const struct hyperv_dma *dma,
   1842  1.3.2.2  christos     uint32_t buflen, uint32_t *handle)
   1843  1.3.2.2  christos {
   1844  1.3.2.2  christos 	const int prflags = cold ? PR_NOWAIT : PR_WAITOK;
   1845  1.3.2.2  christos 	const int kmemflags = cold ? KM_NOSLEEP : KM_SLEEP;
   1846  1.3.2.2  christos 	const int msgflags = cold ? MSGF_NOSLEEP : 0;
   1847  1.3.2.2  christos 	const int hcflags = cold ? HCF_NOSLEEP : HCF_SLEEPOK;
   1848  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1849  1.3.2.2  christos 	struct vmbus_chanmsg_gpadl_conn *hdr;
   1850  1.3.2.2  christos 	struct vmbus_chanmsg_gpadl_subconn *cmd;
   1851  1.3.2.2  christos 	struct vmbus_chanmsg_gpadl_connresp rsp;
   1852  1.3.2.2  christos 	struct vmbus_msg *msg;
   1853  1.3.2.2  christos 	int i, j, last, left, rv;
   1854  1.3.2.2  christos 	int bodylen = 0, ncmds = 0, pfn = 0;
   1855  1.3.2.2  christos 	uint64_t *frames;
   1856  1.3.2.2  christos 	paddr_t pa;
   1857  1.3.2.2  christos 	uint8_t *body;
   1858  1.3.2.2  christos 	/* Total number of pages to reference */
   1859  1.3.2.2  christos 	int total = atop(buflen);
   1860  1.3.2.2  christos 	/* Number of pages that will fit the header */
   1861  1.3.2.2  christos 	int inhdr = MIN(total, VMBUS_NPFNHDR);
   1862  1.3.2.2  christos 
   1863  1.3.2.2  christos 	KASSERT((buflen & PAGE_MASK) == 0);
   1864  1.3.2.2  christos 	KASSERT(buflen == (uint32_t)dma->map->dm_mapsize);
   1865  1.3.2.2  christos 
   1866  1.3.2.2  christos 	msg = pool_cache_get_paddr(sc->sc_msgpool, prflags, &pa);
   1867  1.3.2.2  christos 	if (msg == NULL)
   1868  1.3.2.2  christos 		return ENOMEM;
   1869  1.3.2.2  christos 
   1870  1.3.2.2  christos 	/* Prepare array of frame addresses */
   1871  1.3.2.2  christos 	frames = kmem_zalloc(total * sizeof(*frames), kmemflags);
   1872  1.3.2.2  christos 	if (frames == NULL) {
   1873  1.3.2.2  christos 		pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1874  1.3.2.2  christos 		return ENOMEM;
   1875  1.3.2.2  christos 	}
   1876  1.3.2.2  christos 	for (i = 0, j = 0; i < dma->map->dm_nsegs && j < total; i++) {
   1877  1.3.2.2  christos 		bus_dma_segment_t *seg = &dma->map->dm_segs[i];
   1878  1.3.2.2  christos 		bus_addr_t addr = seg->ds_addr;
   1879  1.3.2.2  christos 
   1880  1.3.2.2  christos 		KASSERT((addr & PAGE_MASK) == 0);
   1881  1.3.2.2  christos 		KASSERT((seg->ds_len & PAGE_MASK) == 0);
   1882  1.3.2.2  christos 
   1883  1.3.2.2  christos 		while (addr < seg->ds_addr + seg->ds_len && j < total) {
   1884  1.3.2.2  christos 			frames[j++] = atop(addr);
   1885  1.3.2.2  christos 			addr += PAGE_SIZE;
   1886  1.3.2.2  christos 		}
   1887  1.3.2.2  christos 	}
   1888  1.3.2.2  christos 
   1889  1.3.2.2  christos 	memset(msg, 0, sizeof(*msg));
   1890  1.3.2.2  christos 	msg->msg_req.hc_dsize = sizeof(struct vmbus_chanmsg_gpadl_conn) +
   1891  1.3.2.2  christos 	    inhdr * sizeof(uint64_t);
   1892  1.3.2.2  christos 	hdr = (struct vmbus_chanmsg_gpadl_conn *)msg->msg_req.hc_data;
   1893  1.3.2.2  christos 	msg->msg_rsp = &rsp;
   1894  1.3.2.2  christos 	msg->msg_rsplen = sizeof(rsp);
   1895  1.3.2.2  christos 	msg->msg_flags = msgflags;
   1896  1.3.2.2  christos 
   1897  1.3.2.2  christos 	left = total - inhdr;
   1898  1.3.2.2  christos 
   1899  1.3.2.2  christos 	/* Allocate additional gpadl_body structures if required */
   1900  1.3.2.2  christos 	if (left > 0) {
   1901  1.3.2.2  christos 		ncmds = MAX(1, left / VMBUS_NPFNBODY + left % VMBUS_NPFNBODY);
   1902  1.3.2.2  christos 		bodylen = ncmds * VMBUS_MSG_DSIZE_MAX;
   1903  1.3.2.2  christos 		body = kmem_zalloc(bodylen, kmemflags);
   1904  1.3.2.2  christos 		if (body == NULL) {
   1905  1.3.2.2  christos 			kmem_free(frames, total * sizeof(*frames));
   1906  1.3.2.2  christos 			pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1907  1.3.2.2  christos 			return ENOMEM;
   1908  1.3.2.2  christos 		}
   1909  1.3.2.2  christos 	}
   1910  1.3.2.2  christos 
   1911  1.3.2.2  christos 	*handle = atomic_add_int_nv(&sc->sc_handle, 1);
   1912  1.3.2.2  christos 
   1913  1.3.2.2  christos 	hdr->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_CONN;
   1914  1.3.2.2  christos 	hdr->chm_chanid = ch->ch_id;
   1915  1.3.2.2  christos 	hdr->chm_gpadl = *handle;
   1916  1.3.2.2  christos 
   1917  1.3.2.2  christos 	/* Single range for a contiguous buffer */
   1918  1.3.2.2  christos 	hdr->chm_range_cnt = 1;
   1919  1.3.2.2  christos 	hdr->chm_range_len = sizeof(struct vmbus_gpa_range) + total *
   1920  1.3.2.2  christos 	    sizeof(uint64_t);
   1921  1.3.2.2  christos 	hdr->chm_range.gpa_ofs = 0;
   1922  1.3.2.2  christos 	hdr->chm_range.gpa_len = buflen;
   1923  1.3.2.2  christos 
   1924  1.3.2.2  christos 	/* Fit as many pages as possible into the header */
   1925  1.3.2.2  christos 	for (i = 0; i < inhdr; i++)
   1926  1.3.2.2  christos 		hdr->chm_range.gpa_page[i] = frames[pfn++];
   1927  1.3.2.2  christos 
   1928  1.3.2.2  christos 	for (i = 0; i < ncmds; i++) {
   1929  1.3.2.2  christos 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
   1930  1.3.2.2  christos 		    VMBUS_MSG_DSIZE_MAX * i);
   1931  1.3.2.2  christos 		cmd->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_SUBCONN;
   1932  1.3.2.2  christos 		cmd->chm_gpadl = *handle;
   1933  1.3.2.2  christos 		last = MIN(left, VMBUS_NPFNBODY);
   1934  1.3.2.2  christos 		for (j = 0; j < last; j++)
   1935  1.3.2.2  christos 			cmd->chm_gpa_page[j] = frames[pfn++];
   1936  1.3.2.2  christos 		left -= last;
   1937  1.3.2.2  christos 	}
   1938  1.3.2.2  christos 
   1939  1.3.2.2  christos 	rv = vmbus_start(sc, msg, pa);
   1940  1.3.2.2  christos 	if (rv != 0) {
   1941  1.3.2.2  christos 		DPRINTF("%s: GPADL_CONN failed\n", device_xname(sc->sc_dev));
   1942  1.3.2.2  christos 		goto out;
   1943  1.3.2.2  christos 	}
   1944  1.3.2.2  christos 	for (i = 0; i < ncmds; i++) {
   1945  1.3.2.2  christos 		int cmdlen = sizeof(*cmd);
   1946  1.3.2.2  christos 		cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body +
   1947  1.3.2.2  christos 		    VMBUS_MSG_DSIZE_MAX * i);
   1948  1.3.2.2  christos 		/* Last element can be short */
   1949  1.3.2.2  christos 		if (i == ncmds - 1)
   1950  1.3.2.2  christos 			cmdlen += last * sizeof(uint64_t);
   1951  1.3.2.2  christos 		else
   1952  1.3.2.2  christos 			cmdlen += VMBUS_NPFNBODY * sizeof(uint64_t);
   1953  1.3.2.2  christos 		rv = vmbus_cmd(sc, cmd, cmdlen, NULL, 0, HCF_NOREPLY | hcflags);
   1954  1.3.2.2  christos 		if (rv != 0) {
   1955  1.3.2.2  christos 			DPRINTF("%s: GPADL_SUBCONN (iteration %d/%d) failed "
   1956  1.3.2.2  christos 			    "with %d\n", device_xname(sc->sc_dev), i, ncmds,
   1957  1.3.2.2  christos 			    rv);
   1958  1.3.2.2  christos 			goto out;
   1959  1.3.2.2  christos 		}
   1960  1.3.2.2  christos 	}
   1961  1.3.2.2  christos 	rv = vmbus_reply(sc, msg);
   1962  1.3.2.2  christos 	if (rv != 0) {
   1963  1.3.2.2  christos 		DPRINTF("%s: GPADL allocation failed with %d\n",
   1964  1.3.2.2  christos 		    device_xname(sc->sc_dev), rv);
   1965  1.3.2.2  christos 	}
   1966  1.3.2.2  christos 
   1967  1.3.2.2  christos  out:
   1968  1.3.2.2  christos 	if (bodylen > 0)
   1969  1.3.2.2  christos 		kmem_free(body, bodylen);
   1970  1.3.2.2  christos 	kmem_free(frames, total * sizeof(*frames));
   1971  1.3.2.2  christos 	pool_cache_put_paddr(sc->sc_msgpool, msg, pa);
   1972  1.3.2.2  christos 	if (rv)
   1973  1.3.2.2  christos 		return rv;
   1974  1.3.2.2  christos 
   1975  1.3.2.2  christos 	KASSERT(*handle == rsp.chm_gpadl);
   1976  1.3.2.2  christos 
   1977  1.3.2.2  christos 	return 0;
   1978  1.3.2.2  christos }
   1979  1.3.2.2  christos 
   1980  1.3.2.2  christos void
   1981  1.3.2.2  christos vmbus_handle_free(struct vmbus_channel *ch, uint32_t handle)
   1982  1.3.2.2  christos {
   1983  1.3.2.2  christos 	struct vmbus_softc *sc = ch->ch_sc;
   1984  1.3.2.2  christos 	struct vmbus_chanmsg_gpadl_disconn cmd;
   1985  1.3.2.2  christos 	struct vmbus_chanmsg_gpadl_disconn rsp;
   1986  1.3.2.2  christos 	int rv;
   1987  1.3.2.2  christos 
   1988  1.3.2.2  christos 	memset(&cmd, 0, sizeof(cmd));
   1989  1.3.2.2  christos 	cmd.chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_DISCONN;
   1990  1.3.2.2  christos 	cmd.chm_chanid = ch->ch_id;
   1991  1.3.2.2  christos 	cmd.chm_gpadl = handle;
   1992  1.3.2.2  christos 
   1993  1.3.2.2  christos 	rv = vmbus_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp),
   1994  1.3.2.2  christos 	    cold ? HCF_NOSLEEP : HCF_SLEEPOK);
   1995  1.3.2.2  christos 	if (rv) {
   1996  1.3.2.2  christos 		DPRINTF("%s: GPADL_DISCONN failed with %d\n",
   1997  1.3.2.2  christos 		    device_xname(sc->sc_dev), rv);
   1998  1.3.2.2  christos 	}
   1999  1.3.2.2  christos }
   2000  1.3.2.2  christos 
   2001  1.3.2.2  christos static int
   2002  1.3.2.2  christos vmbus_attach_print(void *aux, const char *name)
   2003  1.3.2.2  christos {
   2004  1.3.2.2  christos 	struct vmbus_attach_args *aa = aux;
   2005  1.3.2.2  christos 
   2006  1.3.2.2  christos 	if (name)
   2007  1.3.2.2  christos 		printf("\"%s\" at %s", aa->aa_ident, name);
   2008  1.3.2.2  christos 
   2009  1.3.2.2  christos 	return UNCONF;
   2010  1.3.2.2  christos }
   2011  1.3.2.2  christos 
   2012  1.3.2.2  christos static int
   2013  1.3.2.2  christos vmbus_attach_icdevs(struct vmbus_softc *sc)
   2014  1.3.2.2  christos {
   2015  1.3.2.2  christos 	struct vmbus_dev *dv;
   2016  1.3.2.2  christos 	struct vmbus_channel *ch;
   2017  1.3.2.2  christos 
   2018  1.3.2.2  christos 	SLIST_INIT(&sc->sc_icdevs);
   2019  1.3.2.2  christos 	mutex_init(&sc->sc_icdev_lock, MUTEX_DEFAULT, IPL_NET);
   2020  1.3.2.2  christos 
   2021  1.3.2.2  christos 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   2022  1.3.2.2  christos 		if (ch->ch_state != VMBUS_CHANSTATE_OFFERED)
   2023  1.3.2.2  christos 			continue;
   2024  1.3.2.2  christos 		if (ch->ch_flags & CHF_MONITOR)
   2025  1.3.2.2  christos 			continue;
   2026  1.3.2.2  christos 
   2027  1.3.2.2  christos 		dv = kmem_zalloc(sizeof(*dv), cold ? KM_NOSLEEP : KM_SLEEP);
   2028  1.3.2.2  christos 		if (dv == NULL) {
   2029  1.3.2.2  christos 			device_printf(sc->sc_dev,
   2030  1.3.2.2  christos 			    "failed to allocate ic device object\n");
   2031  1.3.2.2  christos 			return ENOMEM;
   2032  1.3.2.2  christos 		}
   2033  1.3.2.2  christos 		dv->dv_aa.aa_type = &ch->ch_type;
   2034  1.3.2.2  christos 		dv->dv_aa.aa_inst = &ch->ch_inst;
   2035  1.3.2.2  christos 		dv->dv_aa.aa_ident = ch->ch_ident;
   2036  1.3.2.2  christos 		dv->dv_aa.aa_chan = ch;
   2037  1.3.2.2  christos 		dv->dv_aa.aa_iot = sc->sc_iot;
   2038  1.3.2.2  christos 		dv->dv_aa.aa_memt = sc->sc_memt;
   2039  1.3.2.2  christos 		mutex_enter(&sc->sc_icdev_lock);
   2040  1.3.2.2  christos 		SLIST_INSERT_HEAD(&sc->sc_icdevs, dv, dv_entry);
   2041  1.3.2.2  christos 		mutex_exit(&sc->sc_icdev_lock);
   2042  1.3.2.2  christos 		ch->ch_dev = config_found_ia(sc->sc_dev, "hypervvmbus",
   2043  1.3.2.2  christos 		    &dv->dv_aa, vmbus_attach_print);
   2044  1.3.2.2  christos 	}
   2045  1.3.2.2  christos 	return 0;
   2046  1.3.2.2  christos }
   2047  1.3.2.2  christos 
   2048  1.3.2.2  christos static int
   2049  1.3.2.2  christos vmbus_attach_devices(struct vmbus_softc *sc)
   2050  1.3.2.2  christos {
   2051  1.3.2.2  christos 	struct vmbus_dev *dv;
   2052  1.3.2.2  christos 	struct vmbus_channel *ch;
   2053  1.3.2.2  christos 
   2054  1.3.2.2  christos 	SLIST_INIT(&sc->sc_devs);
   2055  1.3.2.2  christos 	mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NET);
   2056  1.3.2.2  christos 
   2057  1.3.2.2  christos 	TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
   2058  1.3.2.2  christos 		if (ch->ch_state != VMBUS_CHANSTATE_OFFERED)
   2059  1.3.2.2  christos 			continue;
   2060  1.3.2.2  christos 		if (!(ch->ch_flags & CHF_MONITOR))
   2061  1.3.2.2  christos 			continue;
   2062  1.3.2.2  christos 
   2063  1.3.2.2  christos 		dv = kmem_zalloc(sizeof(*dv), cold ? KM_NOSLEEP : KM_SLEEP);
   2064  1.3.2.2  christos 		if (dv == NULL) {
   2065  1.3.2.2  christos 			device_printf(sc->sc_dev,
   2066  1.3.2.2  christos 			    "failed to allocate device object\n");
   2067  1.3.2.2  christos 			return ENOMEM;
   2068  1.3.2.2  christos 		}
   2069  1.3.2.2  christos 		dv->dv_aa.aa_type = &ch->ch_type;
   2070  1.3.2.2  christos 		dv->dv_aa.aa_inst = &ch->ch_inst;
   2071  1.3.2.2  christos 		dv->dv_aa.aa_ident = ch->ch_ident;
   2072  1.3.2.2  christos 		dv->dv_aa.aa_chan = ch;
   2073  1.3.2.2  christos 		dv->dv_aa.aa_iot = sc->sc_iot;
   2074  1.3.2.2  christos 		dv->dv_aa.aa_memt = sc->sc_memt;
   2075  1.3.2.2  christos 		mutex_enter(&sc->sc_dev_lock);
   2076  1.3.2.2  christos 		SLIST_INSERT_HEAD(&sc->sc_devs, dv, dv_entry);
   2077  1.3.2.2  christos 		mutex_exit(&sc->sc_dev_lock);
   2078  1.3.2.2  christos 		ch->ch_dev = config_found_ia(sc->sc_dev, "hypervvmbus",
   2079  1.3.2.2  christos 		    &dv->dv_aa, vmbus_attach_print);
   2080  1.3.2.2  christos 	}
   2081  1.3.2.2  christos 	return 0;
   2082  1.3.2.2  christos }
   2083  1.3.2.2  christos 
   2084  1.3.2.2  christos MODULE(MODULE_CLASS_DRIVER, vmbus, "hyperv");
   2085  1.3.2.2  christos 
   2086  1.3.2.2  christos #ifdef _MODULE
   2087  1.3.2.2  christos #include "ioconf.c"
   2088  1.3.2.2  christos #endif
   2089  1.3.2.2  christos 
   2090  1.3.2.2  christos static int
   2091  1.3.2.2  christos vmbus_modcmd(modcmd_t cmd, void *aux)
   2092  1.3.2.2  christos {
   2093  1.3.2.2  christos 	int rv = 0;
   2094  1.3.2.2  christos 
   2095  1.3.2.2  christos 	switch (cmd) {
   2096  1.3.2.2  christos 	case MODULE_CMD_INIT:
   2097  1.3.2.2  christos #ifdef _MODULE
   2098  1.3.2.2  christos 		rv = config_init_component(cfdriver_ioconf_vmbus,
   2099  1.3.2.2  christos 		    cfattach_ioconf_vmbus, cfdata_ioconf_vmbus);
   2100  1.3.2.2  christos #endif
   2101  1.3.2.2  christos 		break;
   2102  1.3.2.2  christos 
   2103  1.3.2.2  christos 	case MODULE_CMD_FINI:
   2104  1.3.2.2  christos #ifdef _MODULE
   2105  1.3.2.2  christos 		rv = config_fini_component(cfdriver_ioconf_vmbus,
   2106  1.3.2.2  christos 		    cfattach_ioconf_vmbus, cfdata_ioconf_vmbus);
   2107  1.3.2.2  christos #endif
   2108  1.3.2.2  christos 		break;
   2109  1.3.2.2  christos 
   2110  1.3.2.2  christos 	default:
   2111  1.3.2.2  christos 		rv = ENOTTY;
   2112  1.3.2.2  christos 		break;
   2113  1.3.2.2  christos 	}
   2114  1.3.2.2  christos 
   2115  1.3.2.2  christos 	return rv;
   2116  1.3.2.2  christos }
   2117