1 /* $NetBSD: vmbusvar.h,v 1.6 2020/07/14 00:45:53 yamaguchi Exp $ */ 2 /* $OpenBSD: hypervvar.h,v 1.13 2017/06/23 19:05:42 mikeb Exp $ */ 3 4 /* 5 * Copyright (c) 2016 Mike Belopuhov <mike@esdenera.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #ifndef _VMBUSVAR_H_ 21 #define _VMBUSVAR_H_ 22 23 #include <sys/param.h> 24 #include <sys/device.h> 25 #include <sys/atomic.h> 26 #include <sys/bus.h> 27 #include <sys/condvar.h> 28 #include <sys/evcnt.h> 29 #include <sys/kcpuset.h> 30 #include <sys/mutex.h> 31 #include <sys/pool.h> 32 33 #include <dev/hyperv/hypervreg.h> 34 #include <dev/hyperv/hypervvar.h> 35 36 /* #define HYPERV_DEBUG */ 37 38 #ifdef HYPERV_DEBUG 39 #define DPRINTF(x...) printf(x) 40 #else 41 #define DPRINTF(x...) 42 #endif 43 44 typedef void (*vmbus_channel_callback_t)(void *); 45 46 struct vmbus_softc; 47 struct vmbus_channel; 48 49 struct vmbus_msg { 50 uint64_t msg_flags; 51 #define MSGF_NOSLEEP __BIT(0) 52 #define MSGF_NOQUEUE __BIT(1) 53 #define MSGF_ORPHANED __BIT(2) 54 struct hyperv_hypercall_postmsg_in msg_req __aligned(8); 55 void *msg_rsp; 56 size_t msg_rsplen; 57 TAILQ_ENTRY(vmbus_msg) msg_entry; 58 }; 59 __CTASSERT((offsetof(struct vmbus_msg, msg_req) % 8) == 0); 60 TAILQ_HEAD(vmbus_queue, vmbus_msg); 61 62 struct vmbus_chev { 63 int vce_type; 64 #define VMBUS_CHEV_TYPE_OFFER 0 65 #define VMBUS_CHEV_TYPE_RESCIND 1 66 void *vce_arg; 67 SIMPLEQ_ENTRY(vmbus_chev) vce_entry; 68 }; 69 SIMPLEQ_HEAD(vmbus_chevq, vmbus_chev); 70 71 struct vmbus_dev { 72 int vd_type; 73 #define VMBUS_DEV_TYPE_ATTACH 0 74 #define VMBUS_DEV_TYPE_DETACH 1 75 struct vmbus_channel *vd_chan; 76 SIMPLEQ_ENTRY(vmbus_dev) vd_entry; 77 }; 78 SIMPLEQ_HEAD(vmbus_devq, vmbus_dev); 79 80 struct vmbus_ring_data { 81 struct vmbus_bufring *rd_ring; 82 uint32_t rd_size; 83 kmutex_t rd_lock; 84 uint32_t rd_prod; 85 uint32_t rd_cons; 86 uint32_t rd_dsize; 87 }; 88 89 TAILQ_HEAD(vmbus_channels, vmbus_channel); 90 91 struct vmbus_channel { 92 struct vmbus_softc *ch_sc; 93 device_t ch_dev; 94 u_int ch_refs; 95 96 int ch_state; 97 #define VMBUS_CHANSTATE_OFFERED 1 98 #define VMBUS_CHANSTATE_OPENED 2 99 #define VMBUS_CHANSTATE_CLOSING 3 100 #define VMBUS_CHANSTATE_CLOSED 4 101 uint32_t ch_id; 102 uint16_t ch_subidx; 103 104 struct hyperv_guid ch_type; 105 struct hyperv_guid ch_inst; 106 char ch_ident[38]; 107 108 void *ch_ring; 109 uint32_t ch_ring_gpadl; 110 u_long ch_ring_size; 111 struct hyperv_dma ch_ring_dma; 112 113 struct vmbus_ring_data ch_wrd; 114 struct vmbus_ring_data ch_rrd; 115 116 int ch_cpuid; 117 uint32_t ch_vcpu; 118 119 void (*ch_handler)(void *); 120 void *ch_ctx; 121 struct evcnt ch_evcnt; 122 void *ch_taskq; 123 124 uint32_t ch_flags; 125 #define CHF_BATCHED __BIT(0) 126 #define CHF_MONITOR __BIT(1) 127 #define CHF_REVOKED __BIT(2) 128 129 uint8_t ch_mgroup; 130 uint8_t ch_mindex; 131 struct hyperv_mon_param *ch_monprm; 132 struct hyperv_dma ch_monprm_dma; 133 134 TAILQ_ENTRY(vmbus_channel) ch_prientry; 135 TAILQ_ENTRY(vmbus_channel) ch_entry; 136 137 kmutex_t ch_subchannel_lock; 138 struct vmbus_channels ch_subchannels; 139 u_int ch_subchannel_count; 140 TAILQ_ENTRY(vmbus_channel) ch_subentry; 141 struct vmbus_channel *ch_primary_channel; 142 }; 143 144 #define VMBUS_CHAN_ISPRIMARY(chan) ((chan)->ch_subidx == 0) 145 146 struct vmbus_attach_args { 147 struct hyperv_guid *aa_type; 148 struct hyperv_guid *aa_inst; 149 char *aa_ident; 150 struct vmbus_channel *aa_chan; 151 bus_space_tag_t aa_iot; 152 bus_space_tag_t aa_memt; 153 }; 154 155 struct vmbus_percpu_data { 156 void *simp; /* Synthetic Interrupt Message Page */ 157 void *siep; /* Synthetic Interrupt Event Flags Page */ 158 159 /* Rarely used fields */ 160 struct hyperv_dma simp_dma; 161 struct hyperv_dma siep_dma; 162 163 void *md_cookie; 164 } __aligned(CACHE_LINE_SIZE); 165 166 struct vmbus_softc { 167 device_t sc_dev; 168 bus_space_tag_t sc_iot; 169 bus_space_tag_t sc_memt; 170 bus_dma_tag_t sc_dmat; 171 172 pool_cache_t sc_msgpool; 173 174 void *sc_msg_sih; 175 176 u_long *sc_wevents; /* Write events */ 177 u_long *sc_revents; /* Read events */ 178 struct vmbus_mnf *sc_monitor[2]; 179 struct vmbus_percpu_data sc_percpu[MAXCPUS]; 180 181 /* 182 * Rarely used fields 183 */ 184 uint32_t sc_flags; 185 #define VMBUS_SCFLAG_SYNIC __BIT(0) 186 #define VMBUS_SCFLAG_CONNECTED __BIT(1) 187 #define VMBUS_SCFLAG_OFFERS_DELIVERED __BIT(2) 188 uint32_t sc_proto; 189 int sc_channel_max; 190 191 kcpuset_t *sc_intr_cpuset; 192 193 /* Shared memory for Write/Read events */ 194 void *sc_events; 195 struct hyperv_dma sc_events_dma; 196 197 struct hyperv_dma sc_monitor_dma[2]; 198 199 struct vmbus_queue sc_reqs; /* Request queue */ 200 kmutex_t sc_req_lock; 201 struct vmbus_queue sc_rsps; /* Response queue */ 202 kmutex_t sc_rsp_lock; 203 204 struct vmbus_chevq sc_chevq; 205 kmutex_t sc_chevq_lock; 206 kcondvar_t sc_chevq_cv; 207 208 struct vmbus_devq sc_devq; 209 kmutex_t sc_devq_lock; 210 kcondvar_t sc_devq_cv; 211 212 struct vmbus_devq sc_subch_devq; 213 kmutex_t sc_subch_devq_lock; 214 kcondvar_t sc_subch_devq_cv; 215 216 struct vmbus_channels sc_prichans; 217 kmutex_t sc_prichan_lock; 218 219 struct vmbus_channels sc_channels; 220 kmutex_t sc_channel_lock; 221 222 volatile uint32_t sc_handle; 223 }; 224 225 static __inline void 226 clear_bit(u_int b, volatile void *p) 227 { 228 atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f))); 229 } 230 231 static __inline void 232 set_bit(u_int b, volatile void *p) 233 { 234 atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f)); 235 } 236 237 static __inline int 238 test_bit(u_int b, volatile void *p) 239 { 240 return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f))); 241 } 242 243 extern const struct hyperv_guid hyperv_guid_network; 244 extern const struct hyperv_guid hyperv_guid_ide; 245 extern const struct hyperv_guid hyperv_guid_scsi; 246 extern const struct hyperv_guid hyperv_guid_shutdown; 247 extern const struct hyperv_guid hyperv_guid_timesync; 248 extern const struct hyperv_guid hyperv_guid_heartbeat; 249 extern const struct hyperv_guid hyperv_guid_kvp; 250 extern const struct hyperv_guid hyperv_guid_vss; 251 extern const struct hyperv_guid hyperv_guid_dynmem; 252 extern const struct hyperv_guid hyperv_guid_mouse; 253 extern const struct hyperv_guid hyperv_guid_kbd; 254 extern const struct hyperv_guid hyperv_guid_video; 255 extern const struct hyperv_guid hyperv_guid_fc; 256 extern const struct hyperv_guid hyperv_guid_fcopy; 257 extern const struct hyperv_guid hyperv_guid_pcie; 258 extern const struct hyperv_guid hyperv_guid_netdir; 259 extern const struct hyperv_guid hyperv_guid_rdesktop; 260 extern const struct hyperv_guid hyperv_guid_avma1; 261 extern const struct hyperv_guid hyperv_guid_avma2; 262 extern const struct hyperv_guid hyperv_guid_avma3; 263 extern const struct hyperv_guid hyperv_guid_avma4; 264 265 int vmbus_match(device_t, cfdata_t, void *); 266 int vmbus_attach(struct vmbus_softc *); 267 int vmbus_detach(struct vmbus_softc *, int); 268 269 int vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *, 270 uint32_t, uint32_t *); 271 void vmbus_handle_free(struct vmbus_channel *, uint32_t); 272 int vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t, 273 vmbus_channel_callback_t, void *); 274 int vmbus_channel_close(struct vmbus_channel *); 275 int vmbus_channel_close_direct(struct vmbus_channel *); 276 int vmbus_channel_setdeferred(struct vmbus_channel *, const char *); 277 void vmbus_channel_schedule(struct vmbus_channel *); 278 int vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t, 279 int, uint32_t); 280 int vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *, 281 uint32_t, void *, uint32_t, uint64_t); 282 int vmbus_channel_send_prpl(struct vmbus_channel *, 283 struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t); 284 int vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *, 285 uint64_t *, int); 286 void vmbus_channel_cpu_set(struct vmbus_channel *, int); 287 void vmbus_channel_cpu_rr(struct vmbus_channel *); 288 bool vmbus_channel_is_revoked(struct vmbus_channel *); 289 290 struct vmbus_channel ** 291 vmbus_subchannel_get(struct vmbus_channel *, int); 292 void vmbus_subchannel_put(struct vmbus_channel **, int); 293 294 #endif /* _VMBUSVAR_H_ */ 295