Home | History | Annotate | Line # | Download | only in hyperv
vmbusvar.h revision 1.1
      1 /*	$NetBSD: vmbusvar.h,v 1.1 2019/02/15 08:54:02 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 (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 };
    137 
    138 struct vmbus_dev {
    139 	struct vmbus_attach_args	dv_aa;
    140 	SLIST_ENTRY(vmbus_dev)		dv_entry;
    141 };
    142 SLIST_HEAD(vmbus_devices, vmbus_dev);
    143 
    144 struct vmbus_percpu_data {
    145 	void			*simp;	/* Synthetic Interrupt Message Page */
    146 	void			*siep;	/* Synthetic Interrupt Event Flags Page */
    147 	uint32_t		vcpuid;	/* Virtual cpuid */
    148 
    149 	/* Rarely used fields */
    150 	struct hyperv_dma	simp_dma;
    151 	struct hyperv_dma	siep_dma;
    152 } __aligned(CACHE_LINE_SIZE);
    153 
    154 struct vmbus_softc {
    155 	device_t		sc_dev;
    156 	bus_dma_tag_t		sc_dmat;
    157 
    158 	pool_cache_t		sc_msgpool;
    159 
    160 	void			*sc_msg_sih;
    161 
    162 	u_long			*sc_wevents;	/* Write events */
    163 	u_long			*sc_revents;	/* Read events */
    164 	struct vmbus_mnf	*sc_monitor[2];
    165 	struct vmbus_percpu_data sc_percpu[MAXCPUS];
    166 
    167 	/*
    168 	 * Rarely used fields
    169 	 */
    170 	uint32_t		sc_flags;
    171 #define  VMBUS_SCFLAG_SYNIC		__BIT(0)
    172 #define  VMBUS_SCFLAG_CONNECTED		__BIT(1)
    173 #define  VMBUS_SCFLAG_OFFERS_DELIVERED	__BIT(2)
    174 	uint32_t		sc_proto;
    175 	int			sc_channel_max;
    176 
    177 	kcpuset_t		*sc_intr_cpuset;
    178 
    179 	/* Shared memory for Write/Read events */
    180 	void			*sc_events;
    181 	struct hyperv_dma	sc_events_dma;
    182 
    183 	struct hyperv_dma	sc_monitor_dma[2];
    184 
    185 	struct vmbus_queue 	sc_reqs;	/* Request queue */
    186 	kmutex_t		sc_req_lock;
    187 	struct vmbus_queue 	sc_rsps;	/* Response queue */
    188 	kmutex_t		sc_rsp_lock;
    189 
    190 	struct vmbus_offers	sc_offers;
    191 	kmutex_t		sc_offer_lock;
    192 
    193 	struct vmbus_channels	sc_channels;
    194 	kmutex_t		sc_channel_lock;
    195 
    196 	volatile uint32_t	sc_handle;
    197 
    198 	struct vmbus_devices	sc_icdevs;
    199 	kmutex_t		sc_icdev_lock;
    200 
    201 	struct vmbus_devices	sc_devs;
    202 	kmutex_t		sc_dev_lock;
    203 };
    204 
    205 static __inline void
    206 clear_bit(u_int b, volatile void *p)
    207 {
    208 	atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f)));
    209 }
    210 
    211 static __inline void
    212 set_bit(u_int b, volatile void *p)
    213 {
    214 	atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
    215 }
    216 
    217 static __inline int
    218 test_bit(u_int b, volatile void *p)
    219 {
    220 	return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
    221 }
    222 
    223 extern const struct hyperv_guid hyperv_guid_network;
    224 extern const struct hyperv_guid hyperv_guid_ide;
    225 extern const struct hyperv_guid hyperv_guid_scsi;
    226 extern const struct hyperv_guid hyperv_guid_shutdown;
    227 extern const struct hyperv_guid hyperv_guid_timesync;
    228 extern const struct hyperv_guid hyperv_guid_heartbeat;
    229 extern const struct hyperv_guid hyperv_guid_kvp;
    230 extern const struct hyperv_guid hyperv_guid_vss;
    231 extern const struct hyperv_guid hyperv_guid_dynmem;
    232 extern const struct hyperv_guid hyperv_guid_mouse;
    233 extern const struct hyperv_guid hyperv_guid_kbd;
    234 extern const struct hyperv_guid hyperv_guid_video;
    235 extern const struct hyperv_guid hyperv_guid_fc;
    236 extern const struct hyperv_guid hyperv_guid_fcopy;
    237 extern const struct hyperv_guid hyperv_guid_pcie;
    238 extern const struct hyperv_guid hyperv_guid_netdir;
    239 extern const struct hyperv_guid hyperv_guid_rdesktop;
    240 extern const struct hyperv_guid hyperv_guid_avma1;
    241 extern const struct hyperv_guid hyperv_guid_avma2;
    242 extern const struct hyperv_guid hyperv_guid_avma3;
    243 extern const struct hyperv_guid hyperv_guid_avma4;
    244 
    245 int	vmbus_match(device_t, cfdata_t, void *);
    246 int	vmbus_attach(struct vmbus_softc *);
    247 int	vmbus_detach(struct vmbus_softc *, int);
    248 
    249 int	vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *,
    250 	    uint32_t, uint32_t *);
    251 void	vmbus_handle_free(struct vmbus_channel *, uint32_t);
    252 int	vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t,
    253 	    vmbus_channel_callback_t, void *);
    254 int	vmbus_channel_close(struct vmbus_channel *);
    255 int	vmbus_channel_close_direct(struct vmbus_channel *);
    256 int	vmbus_channel_setdeferred(struct vmbus_channel *, const char *);
    257 void	vmbus_channel_schedule(struct vmbus_channel *);
    258 int	vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t,
    259 	    int, uint32_t);
    260 int	vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *,
    261 	    uint32_t, void *, uint32_t, uint64_t);
    262 int	vmbus_channel_send_prpl(struct vmbus_channel *,
    263 	    struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t);
    264 int	vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *,
    265 	    uint64_t *, int);
    266 void	vmbus_channel_cpu_set(struct vmbus_channel *, int);
    267 void	vmbus_channel_cpu_rr(struct vmbus_channel *);
    268 
    269 struct vmbus_channel **
    270 	vmbus_subchannel_get(struct vmbus_channel *, int);
    271 void	vmbus_subchannel_put(struct vmbus_channel **, int);
    272 
    273 #endif	/* _VMBUSVAR_H_ */
    274