1 /*- 2 * Copyright (c) 2009-2012,2016 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /** 32 * HyperV definitions for messages that are sent between instances of the 33 * Channel Management Library in separate partitions, or in some cases, 34 * back to itself. 35 */ 36 37 #ifndef __HYPERV_H__ 38 #define __HYPERV_H__ 39 40 #include <sys/param.h> 41 #include <sys/mbuf.h> 42 #include <sys/queue.h> 43 #include <sys/malloc.h> 44 #include <sys/kthread.h> 45 #include <sys/taskqueue.h> 46 #include <sys/systm.h> 47 #include <sys/lock.h> 48 #include <sys/sema.h> 49 #include <sys/smp.h> 50 #include <sys/mutex.h> 51 #include <sys/bus.h> 52 #include <sys/sysctl.h> 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #include <vm/pmap.h> 56 57 #include <amd64/include/xen/synch_bitops.h> 58 #include <amd64/include/atomic.h> 59 #include <dev/hyperv/include/hyperv_busdma.h> 60 61 #define HV_S_OK 0x00000000 62 #define HV_E_FAIL 0x80004005 63 #define HV_ERROR_NOT_SUPPORTED 0x80070032 64 #define HV_ERROR_MACHINE_LOCKED 0x800704F7 65 66 /* 67 * VMBUS version is 32 bit, upper 16 bit for major_number and lower 68 * 16 bit for minor_number. 69 * 70 * 0.13 -- Windows Server 2008 71 * 1.1 -- Windows 7 72 * 2.4 -- Windows 8 73 * 3.0 -- Windows 8.1 74 */ 75 #define VMBUS_VERSION_WS2008 ((0 << 16) | (13)) 76 #define VMBUS_VERSION_WIN7 ((1 << 16) | (1)) 77 #define VMBUS_VERSION_WIN8 ((2 << 16) | (4)) 78 #define VMBUS_VERSION_WIN8_1 ((3 << 16) | (0)) 79 80 #define VMBUS_VERSION_MAJOR(ver) (((uint32_t)(ver)) >> 16) 81 #define VMBUS_VERSION_MINOR(ver) (((uint32_t)(ver)) & 0xffff) 82 83 struct hyperv_guid { 84 uint8_t hv_guid[16]; 85 } __packed; 86 87 #define HYPERV_GUID_STRLEN 40 88 89 int hyperv_guid2str(const struct hyperv_guid *, char *, size_t); 90 91 /* 92 * Common defines for Hyper-V ICs 93 */ 94 #define HV_ICMSGTYPE_NEGOTIATE 0 95 #define HV_ICMSGTYPE_HEARTBEAT 1 96 #define HV_ICMSGTYPE_KVPEXCHANGE 2 97 #define HV_ICMSGTYPE_SHUTDOWN 3 98 #define HV_ICMSGTYPE_TIMESYNC 4 99 #define HV_ICMSGTYPE_VSS 5 100 101 #define HV_ICMSGHDRFLAG_TRANSACTION 1 102 #define HV_ICMSGHDRFLAG_REQUEST 2 103 #define HV_ICMSGHDRFLAG_RESPONSE 4 104 105 typedef struct hv_vmbus_pipe_hdr { 106 uint32_t flags; 107 uint32_t msgsize; 108 } __packed hv_vmbus_pipe_hdr; 109 110 typedef struct hv_vmbus_ic_version { 111 uint16_t major; 112 uint16_t minor; 113 } __packed hv_vmbus_ic_version; 114 115 typedef struct hv_vmbus_icmsg_hdr { 116 hv_vmbus_ic_version icverframe; 117 uint16_t icmsgtype; 118 hv_vmbus_ic_version icvermsg; 119 uint16_t icmsgsize; 120 uint32_t status; 121 uint8_t ictransaction_id; 122 uint8_t icflags; 123 uint8_t reserved[2]; 124 } __packed hv_vmbus_icmsg_hdr; 125 126 typedef struct hv_vmbus_icmsg_negotiate { 127 uint16_t icframe_vercnt; 128 uint16_t icmsg_vercnt; 129 uint32_t reserved; 130 hv_vmbus_ic_version icversion_data[1]; /* any size array */ 131 } __packed hv_vmbus_icmsg_negotiate; 132 133 typedef struct hv_vmbus_shutdown_msg_data { 134 uint32_t reason_code; 135 uint32_t timeout_seconds; 136 uint32_t flags; 137 uint8_t display_message[2048]; 138 } __packed hv_vmbus_shutdown_msg_data; 139 140 typedef struct hv_vmbus_heartbeat_msg_data { 141 uint64_t seq_num; 142 uint32_t reserved[8]; 143 } __packed hv_vmbus_heartbeat_msg_data; 144 145 typedef struct { 146 /* 147 * offset in bytes from the start of ring data below 148 */ 149 volatile uint32_t write_index; 150 /* 151 * offset in bytes from the start of ring data below 152 */ 153 volatile uint32_t read_index; 154 /* 155 * NOTE: The interrupt_mask field is used only for channels, but 156 * vmbus connection also uses this data structure 157 */ 158 volatile uint32_t interrupt_mask; 159 /* pad it to PAGE_SIZE so that data starts on a page */ 160 uint8_t reserved[4084]; 161 162 /* 163 * WARNING: Ring data starts here 164 * !!! DO NOT place any fields below this !!! 165 */ 166 uint8_t buffer[0]; /* doubles as interrupt mask */ 167 } __packed hv_vmbus_ring_buffer; 168 169 typedef struct { 170 hv_vmbus_ring_buffer* ring_buffer; 171 struct mtx ring_lock; 172 uint32_t ring_data_size; /* ring_size */ 173 } hv_vmbus_ring_buffer_info; 174 175 typedef void (*vmbus_chan_callback_t)(void *); 176 177 typedef struct hv_vmbus_channel { 178 device_t ch_dev; 179 struct vmbus_softc *vmbus_sc; 180 uint32_t ch_flags; /* VMBUS_CHAN_FLAG_ */ 181 uint32_t ch_id; /* channel id */ 182 183 /* 184 * These are based on the offer_msg.monitor_id. 185 * Save it here for easy access. 186 */ 187 int ch_montrig_idx; /* MNF trig index */ 188 uint32_t ch_montrig_mask;/* MNF trig mask */ 189 190 /* 191 * send to parent 192 */ 193 hv_vmbus_ring_buffer_info outbound; 194 /* 195 * receive from parent 196 */ 197 hv_vmbus_ring_buffer_info inbound; 198 199 struct taskqueue *ch_tq; 200 struct task ch_task; 201 vmbus_chan_callback_t ch_cb; 202 void *ch_cbarg; 203 204 struct hyperv_mon_param *ch_monprm; 205 struct hyperv_dma ch_monprm_dma; 206 207 int ch_cpuid; /* owner cpu */ 208 /* 209 * Virtual cpuid for ch_cpuid; it is used to communicate cpuid 210 * related information w/ Hyper-V. If MSR_HV_VP_INDEX does not 211 * exist, ch_vcpuid will always be 0 for compatibility. 212 */ 213 uint32_t ch_vcpuid; 214 215 /* 216 * If this is a primary channel, ch_subchan* fields 217 * contain sub-channels belonging to this primary 218 * channel. 219 */ 220 struct mtx ch_subchan_lock; 221 TAILQ_HEAD(, hv_vmbus_channel) ch_subchans; 222 int ch_subchan_cnt; 223 224 /* If this is a sub-channel */ 225 TAILQ_ENTRY(hv_vmbus_channel) ch_sublink; /* sub-channel link */ 226 struct hv_vmbus_channel *ch_prichan; /* owner primary chan */ 227 228 /* 229 * Driver private data 230 */ 231 void *hv_chan_priv1; 232 void *hv_chan_priv2; 233 void *hv_chan_priv3; 234 235 void *ch_bufring; /* TX+RX bufrings */ 236 struct hyperv_dma ch_bufring_dma; 237 uint32_t ch_bufring_gpadl; 238 239 struct task ch_detach_task; 240 TAILQ_ENTRY(hv_vmbus_channel) ch_prilink; /* primary chan link */ 241 uint32_t ch_subidx; /* subchan index */ 242 volatile uint32_t ch_stflags; /* atomic-op */ 243 /* VMBUS_CHAN_ST_ */ 244 struct hyperv_guid ch_guid_type; 245 struct hyperv_guid ch_guid_inst; 246 247 struct sysctl_ctx_list ch_sysctl_ctx; 248 } hv_vmbus_channel; 249 250 #define VMBUS_CHAN_ISPRIMARY(chan) ((chan)->ch_subidx == 0) 251 252 #define VMBUS_CHAN_FLAG_HASMNF 0x0001 253 /* 254 * If this flag is set, this channel's interrupt will be masked in ISR, 255 * and the RX bufring will be drained before this channel's interrupt is 256 * unmasked. 257 * 258 * This flag is turned on by default. Drivers can turn it off according 259 * to their own requirement. 260 */ 261 #define VMBUS_CHAN_FLAG_BATCHREAD 0x0002 262 263 #define VMBUS_CHAN_ST_OPENED_SHIFT 0 264 #define VMBUS_CHAN_ST_OPENED (1 << VMBUS_CHAN_ST_OPENED_SHIFT) 265 266 /** 267 * @brief Get physical address from virtual 268 */ 269 static inline unsigned long 270 hv_get_phys_addr(void *virt) 271 { 272 unsigned long ret; 273 ret = (vtophys(virt) | ((vm_offset_t) virt & PAGE_MASK)); 274 return (ret); 275 } 276 277 static __inline struct hv_vmbus_channel * 278 vmbus_get_channel(device_t dev) 279 { 280 return device_get_ivars(dev); 281 } 282 283 #endif /* __HYPERV_H__ */ 284