1/* $NetBSD: vmbusvar.h,v 1.7 2022/05/20 13:55:17 nonaka 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 44typedef void (*vmbus_channel_callback_t)(void *); 45 46struct vmbus_softc; 47struct vmbus_channel; 48 49struct 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); 60TAILQ_HEAD(vmbus_queue, vmbus_msg); 61 62struct 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}; 69SIMPLEQ_HEAD(vmbus_chevq, vmbus_chev); 70 71struct 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}; 78SIMPLEQ_HEAD(vmbus_devq, vmbus_dev); 79 80struct 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 89TAILQ_HEAD(vmbus_channels, vmbus_channel); 90 91struct 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 kmutex_t ch_event_lock; 125 kcondvar_t ch_event_cv; 126 127 uint32_t ch_flags; 128#define CHF_BATCHED __BIT(0) 129#define CHF_MONITOR __BIT(1) 130#define CHF_REVOKED __BIT(2) 131 132 uint8_t ch_mgroup; 133 uint8_t ch_mindex; 134 struct hyperv_mon_param *ch_monprm; 135 struct hyperv_dma ch_monprm_dma; 136 137 TAILQ_ENTRY(vmbus_channel) ch_prientry; 138 TAILQ_ENTRY(vmbus_channel) ch_entry; 139 140 kmutex_t ch_subchannel_lock; 141 kcondvar_t ch_subchannel_cv; 142 struct vmbus_channels ch_subchannels; 143 u_int ch_subchannel_count; 144 TAILQ_ENTRY(vmbus_channel) ch_subentry; 145 struct vmbus_channel *ch_primary_channel; 146}; 147 148#define VMBUS_CHAN_ISPRIMARY(chan) ((chan)->ch_subidx == 0) 149 150struct vmbus_attach_args { 151 struct hyperv_guid *aa_type; 152 struct hyperv_guid *aa_inst; 153 char *aa_ident; 154 struct vmbus_channel *aa_chan; 155 bus_space_tag_t aa_iot; 156 bus_space_tag_t aa_memt; 157}; 158 159struct vmbus_percpu_data { 160 void *simp; /* Synthetic Interrupt Message Page */ 161 void *siep; /* Synthetic Interrupt Event Flags Page */ 162 163 /* Rarely used fields */ 164 struct hyperv_dma simp_dma; 165 struct hyperv_dma siep_dma; 166 167 void *md_cookie; 168} __aligned(CACHE_LINE_SIZE); 169 170struct vmbus_softc { 171 device_t sc_dev; 172 bus_space_tag_t sc_iot; 173 bus_space_tag_t sc_memt; 174 bus_dma_tag_t sc_dmat; 175 176 pool_cache_t sc_msgpool; 177 178 void *sc_msg_sih; 179 180 u_long *sc_wevents; /* Write events */ 181 u_long *sc_revents; /* Read events */ 182 struct vmbus_channel * volatile *sc_chanmap; 183 volatile u_long sc_evtmask[VMBUS_EVTFLAGS_MAX]; 184 struct vmbus_mnf *sc_monitor[2]; 185 struct vmbus_percpu_data sc_percpu[MAXCPUS]; 186 187 /* 188 * Rarely used fields 189 */ 190 uint32_t sc_flags; 191#define VMBUS_SCFLAG_SYNIC __BIT(0) 192#define VMBUS_SCFLAG_CONNECTED __BIT(1) 193#define VMBUS_SCFLAG_OFFERS_DELIVERED __BIT(2) 194 uint32_t sc_proto; 195 int sc_channel_max; 196 197 kcpuset_t *sc_intr_cpuset; 198 199 /* Shared memory for Write/Read events */ 200 void *sc_events; 201 struct hyperv_dma sc_events_dma; 202 203 struct hyperv_dma sc_monitor_dma[2]; 204 205 struct vmbus_queue sc_reqs; /* Request queue */ 206 kmutex_t sc_req_lock; 207 struct vmbus_queue sc_rsps; /* Response queue */ 208 kmutex_t sc_rsp_lock; 209 210 struct vmbus_chevq sc_chevq; 211 kmutex_t sc_chevq_lock; 212 kcondvar_t sc_chevq_cv; 213 214 struct vmbus_devq sc_devq; 215 kmutex_t sc_devq_lock; 216 kcondvar_t sc_devq_cv; 217 218 struct vmbus_devq sc_subch_devq; 219 kmutex_t sc_subch_devq_lock; 220 kcondvar_t sc_subch_devq_cv; 221 222 struct vmbus_channels sc_prichans; 223 kmutex_t sc_prichan_lock; 224 225 struct vmbus_channels sc_channels; 226 kmutex_t sc_channel_lock; 227 228 volatile uint32_t sc_handle; 229}; 230 231static __inline void 232clear_bit(u_int b, volatile void *p) 233{ 234 atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f))); 235} 236 237static __inline void 238set_bit(u_int b, volatile void *p) 239{ 240 atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f)); 241} 242 243static __inline int 244test_bit(u_int b, volatile void *p) 245{ 246 return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f))); 247} 248 249extern const struct hyperv_guid hyperv_guid_network; 250extern const struct hyperv_guid hyperv_guid_ide; 251extern const struct hyperv_guid hyperv_guid_scsi; 252extern const struct hyperv_guid hyperv_guid_shutdown; 253extern const struct hyperv_guid hyperv_guid_timesync; 254extern const struct hyperv_guid hyperv_guid_heartbeat; 255extern const struct hyperv_guid hyperv_guid_kvp; 256extern const struct hyperv_guid hyperv_guid_vss; 257extern const struct hyperv_guid hyperv_guid_dynmem; 258extern const struct hyperv_guid hyperv_guid_mouse; 259extern const struct hyperv_guid hyperv_guid_kbd; 260extern const struct hyperv_guid hyperv_guid_video; 261extern const struct hyperv_guid hyperv_guid_fc; 262extern const struct hyperv_guid hyperv_guid_fcopy; 263extern const struct hyperv_guid hyperv_guid_pcie; 264extern const struct hyperv_guid hyperv_guid_netdir; 265extern const struct hyperv_guid hyperv_guid_rdesktop; 266extern const struct hyperv_guid hyperv_guid_avma1; 267extern const struct hyperv_guid hyperv_guid_avma2; 268extern const struct hyperv_guid hyperv_guid_avma3; 269extern const struct hyperv_guid hyperv_guid_avma4; 270 271int vmbus_match(device_t, cfdata_t, void *); 272int vmbus_attach(struct vmbus_softc *); 273int vmbus_detach(struct vmbus_softc *, int); 274 275int vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *, 276 uint32_t, uint32_t *); 277void vmbus_handle_free(struct vmbus_channel *, uint32_t); 278int vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t, 279 vmbus_channel_callback_t, void *); 280int vmbus_channel_close(struct vmbus_channel *); 281int vmbus_channel_close_direct(struct vmbus_channel *); 282int vmbus_channel_setdeferred(struct vmbus_channel *, const char *); 283void vmbus_channel_schedule(struct vmbus_channel *); 284int vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t, 285 int, uint32_t); 286int vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *, 287 uint32_t, void *, uint32_t, uint64_t); 288int vmbus_channel_send_prpl(struct vmbus_channel *, 289 struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t); 290int vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *, 291 uint64_t *, int); 292void vmbus_channel_cpu_set(struct vmbus_channel *, int); 293void vmbus_channel_cpu_rr(struct vmbus_channel *); 294bool vmbus_channel_is_revoked(struct vmbus_channel *); 295bool vmbus_channel_tx_empty(struct vmbus_channel *); 296bool vmbus_channel_rx_empty(struct vmbus_channel *); 297void vmbus_channel_pause(struct vmbus_channel *); 298uint32_t vmbus_channel_unpause(struct vmbus_channel *); 299uint32_t vmbus_channel_ready(struct vmbus_channel *); 300 301struct vmbus_channel ** 302 vmbus_subchannel_get(struct vmbus_channel *, int); 303void vmbus_subchannel_rel(struct vmbus_channel **, int); 304void vmbus_subchannel_drain(struct vmbus_channel *); 305 306#endif /* _VMBUSVAR_H_ */ 307