Home | History | Annotate | Line # | Download | only in hyperv
vmbusvar.h revision 1.1.2.3
      1 /*	$NetBSD: vmbusvar.h,v 1.1.2.3 2019/06/12 10:17:32 martin 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 (at) 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/evcnt.h>
     28 #include <sys/kcpuset.h>
     29 #include <sys/mutex.h>
     30 #include <sys/pool.h>
     31 
     32 #include <dev/hyperv/hypervreg.h>
     33 #include <dev/hyperv/hypervvar.h>
     34 
     35 /* #define HYPERV_DEBUG */
     36 
     37 #ifdef HYPERV_DEBUG
     38 #define DPRINTF(x...)		printf(x)
     39 #else
     40 #define DPRINTF(x...)
     41 #endif
     42 
     43 typedef void (*vmbus_channel_callback_t)(void *);
     44 
     45 struct vmbus_softc;
     46 
     47 struct vmbus_msg {
     48 	uint64_t			msg_flags;
     49 #define  MSGF_NOSLEEP			__BIT(0)
     50 #define  MSGF_NOQUEUE			__BIT(1)
     51 #define  MSGF_ORPHANED			__BIT(2)
     52 	struct hyperv_hypercall_postmsg_in msg_req __aligned(8);
     53 	void				*msg_rsp;
     54 	size_t				msg_rsplen;
     55 	TAILQ_ENTRY(vmbus_msg)		msg_entry;
     56 };
     57 __CTASSERT((offsetof(struct vmbus_msg, msg_req) % 8) == 0);
     58 TAILQ_HEAD(vmbus_queue, vmbus_msg);
     59 
     60 struct vmbus_offer {
     61 	struct vmbus_chanmsg_choffer	co_chan;
     62 	SIMPLEQ_ENTRY(vmbus_offer)	co_entry;
     63 };
     64 SIMPLEQ_HEAD(vmbus_offers, vmbus_offer);
     65 
     66 struct vmbus_ring_data {
     67 	struct vmbus_bufring		*rd_ring;
     68 	uint32_t			rd_size;
     69 	kmutex_t			rd_lock;
     70 	uint32_t			rd_prod;
     71 	uint32_t			rd_cons;
     72 	uint32_t			rd_dsize;
     73 };
     74 
     75 struct vmbus_channel;
     76 TAILQ_HEAD(vmbus_channels, vmbus_channel);
     77 
     78 struct vmbus_channel {
     79 	struct vmbus_softc		*ch_sc;
     80 	device_t			ch_dev;
     81 	u_int				ch_refs;
     82 
     83 	int				ch_state;
     84 #define  VMBUS_CHANSTATE_OFFERED	1
     85 #define  VMBUS_CHANSTATE_OPENED		2
     86 #define  VMBUS_CHANSTATE_CLOSING	3
     87 #define  VMBUS_CHANSTATE_CLOSED		4
     88 	uint32_t			ch_id;
     89 	uint16_t			ch_subidx;
     90 
     91 	struct hyperv_guid		ch_type;
     92 	struct hyperv_guid		ch_inst;
     93 	char				ch_ident[38];
     94 
     95 	void				*ch_ring;
     96 	uint32_t			ch_ring_gpadl;
     97 	u_long				ch_ring_size;
     98 	struct hyperv_dma		ch_ring_dma;
     99 
    100 	struct vmbus_ring_data		ch_wrd;
    101 	struct vmbus_ring_data		ch_rrd;
    102 
    103 	int				ch_cpuid;
    104 	uint32_t			ch_vcpu;
    105 
    106 	void				(*ch_handler)(void *);
    107 	void				*ch_ctx;
    108 	struct evcnt			ch_evcnt;
    109 	void				*ch_taskq;
    110 
    111 	uint32_t			ch_flags;
    112 #define  CHF_BATCHED			__BIT(0)
    113 #define  CHF_MONITOR			__BIT(1)
    114 
    115 	uint8_t				ch_mgroup;
    116 	uint8_t				ch_mindex;
    117 	struct hyperv_mon_param		*ch_monprm;
    118 	struct hyperv_dma		ch_monprm_dma;
    119 
    120 	TAILQ_ENTRY(vmbus_channel)	ch_entry;
    121 
    122 	kmutex_t			ch_subchannel_lock;
    123 	struct vmbus_channels		ch_subchannels;
    124 	u_int				ch_subchannel_count;
    125 	TAILQ_ENTRY(vmbus_channel)	ch_subentry;
    126 	struct vmbus_channel		*ch_primary_channel;
    127 };
    128 
    129 #define VMBUS_CHAN_ISPRIMARY(chan)	((chan)->ch_subidx == 0)
    130 
    131 struct vmbus_attach_args {
    132 	struct hyperv_guid		*aa_type;
    133 	struct hyperv_guid		*aa_inst;
    134 	char				*aa_ident;
    135 	struct vmbus_channel		*aa_chan;
    136 	bus_space_tag_t			aa_iot;
    137 	bus_space_tag_t			aa_memt;
    138 };
    139 
    140 struct vmbus_dev {
    141 	struct vmbus_attach_args	dv_aa;
    142 	SLIST_ENTRY(vmbus_dev)		dv_entry;
    143 };
    144 SLIST_HEAD(vmbus_devices, vmbus_dev);
    145 
    146 struct vmbus_percpu_data {
    147 	void			*simp;	/* Synthetic Interrupt Message Page */
    148 	void			*siep;	/* Synthetic Interrupt Event Flags Page */
    149 	uint32_t		vcpuid;	/* Virtual cpuid */
    150 
    151 	/* Rarely used fields */
    152 	struct hyperv_dma	simp_dma;
    153 	struct hyperv_dma	siep_dma;
    154 } __aligned(CACHE_LINE_SIZE);
    155 
    156 struct vmbus_softc {
    157 	device_t		sc_dev;
    158 	bus_space_tag_t		sc_iot;
    159 	bus_space_tag_t		sc_memt;
    160 	bus_dma_tag_t		sc_dmat;
    161 
    162 	pool_cache_t		sc_msgpool;
    163 
    164 	void			*sc_msg_sih;
    165 
    166 	u_long			*sc_wevents;	/* Write events */
    167 	u_long			*sc_revents;	/* Read events */
    168 	struct vmbus_mnf	*sc_monitor[2];
    169 	struct vmbus_percpu_data sc_percpu[MAXCPUS];
    170 
    171 	/*
    172 	 * Rarely used fields
    173 	 */
    174 	uint32_t		sc_flags;
    175 #define  VMBUS_SCFLAG_SYNIC		__BIT(0)
    176 #define  VMBUS_SCFLAG_CONNECTED		__BIT(1)
    177 #define  VMBUS_SCFLAG_OFFERS_DELIVERED	__BIT(2)
    178 	uint32_t		sc_proto;
    179 	int			sc_channel_max;
    180 
    181 	kcpuset_t		*sc_intr_cpuset;
    182 
    183 	/* Shared memory for Write/Read events */
    184 	void			*sc_events;
    185 	struct hyperv_dma	sc_events_dma;
    186 
    187 	struct hyperv_dma	sc_monitor_dma[2];
    188 
    189 	struct vmbus_queue 	sc_reqs;	/* Request queue */
    190 	kmutex_t		sc_req_lock;
    191 	struct vmbus_queue 	sc_rsps;	/* Response queue */
    192 	kmutex_t		sc_rsp_lock;
    193 
    194 	struct vmbus_offers	sc_offers;
    195 	kmutex_t		sc_offer_lock;
    196 
    197 	struct vmbus_channels	sc_channels;
    198 	kmutex_t		sc_channel_lock;
    199 
    200 	volatile uint32_t	sc_handle;
    201 
    202 	struct vmbus_devices	sc_icdevs;
    203 	kmutex_t		sc_icdev_lock;
    204 
    205 	struct vmbus_devices	sc_devs;
    206 	kmutex_t		sc_dev_lock;
    207 };
    208 
    209 static __inline void
    210 clear_bit(u_int b, volatile void *p)
    211 {
    212 	atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f)));
    213 }
    214 
    215 static __inline void
    216 set_bit(u_int b, volatile void *p)
    217 {
    218 	atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
    219 }
    220 
    221 static __inline int
    222 test_bit(u_int b, volatile void *p)
    223 {
    224 	return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
    225 }
    226 
    227 extern const struct hyperv_guid hyperv_guid_network;
    228 extern const struct hyperv_guid hyperv_guid_ide;
    229 extern const struct hyperv_guid hyperv_guid_scsi;
    230 extern const struct hyperv_guid hyperv_guid_shutdown;
    231 extern const struct hyperv_guid hyperv_guid_timesync;
    232 extern const struct hyperv_guid hyperv_guid_heartbeat;
    233 extern const struct hyperv_guid hyperv_guid_kvp;
    234 extern const struct hyperv_guid hyperv_guid_vss;
    235 extern const struct hyperv_guid hyperv_guid_dynmem;
    236 extern const struct hyperv_guid hyperv_guid_mouse;
    237 extern const struct hyperv_guid hyperv_guid_kbd;
    238 extern const struct hyperv_guid hyperv_guid_video;
    239 extern const struct hyperv_guid hyperv_guid_fc;
    240 extern const struct hyperv_guid hyperv_guid_fcopy;
    241 extern const struct hyperv_guid hyperv_guid_pcie;
    242 extern const struct hyperv_guid hyperv_guid_netdir;
    243 extern const struct hyperv_guid hyperv_guid_rdesktop;
    244 extern const struct hyperv_guid hyperv_guid_avma1;
    245 extern const struct hyperv_guid hyperv_guid_avma2;
    246 extern const struct hyperv_guid hyperv_guid_avma3;
    247 extern const struct hyperv_guid hyperv_guid_avma4;
    248 
    249 int	vmbus_match(device_t, cfdata_t, void *);
    250 int	vmbus_attach(struct vmbus_softc *);
    251 int	vmbus_detach(struct vmbus_softc *, int);
    252 
    253 int	vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *,
    254 	    uint32_t, uint32_t *);
    255 void	vmbus_handle_free(struct vmbus_channel *, uint32_t);
    256 int	vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t,
    257 	    vmbus_channel_callback_t, void *);
    258 int	vmbus_channel_close(struct vmbus_channel *);
    259 int	vmbus_channel_close_direct(struct vmbus_channel *);
    260 int	vmbus_channel_setdeferred(struct vmbus_channel *, const char *);
    261 void	vmbus_channel_schedule(struct vmbus_channel *);
    262 int	vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t,
    263 	    int, uint32_t);
    264 int	vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *,
    265 	    uint32_t, void *, uint32_t, uint64_t);
    266 int	vmbus_channel_send_prpl(struct vmbus_channel *,
    267 	    struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t);
    268 int	vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *,
    269 	    uint64_t *, int);
    270 void	vmbus_channel_cpu_set(struct vmbus_channel *, int);
    271 void	vmbus_channel_cpu_rr(struct vmbus_channel *);
    272 
    273 struct vmbus_channel **
    274 	vmbus_subchannel_get(struct vmbus_channel *, int);
    275 void	vmbus_subchannel_put(struct vmbus_channel **, int);
    276 
    277 #endif	/* _VMBUSVAR_H_ */
    278