xref: /freebsd/sys/dev/hyperv/vmbus/vmbus_chanvar.h (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
162549472SSepherosa Ziehau /*-
262549472SSepherosa Ziehau  * Copyright (c) 2016 Microsoft Corp.
362549472SSepherosa Ziehau  * All rights reserved.
462549472SSepherosa Ziehau  *
562549472SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
662549472SSepherosa Ziehau  * modification, are permitted provided that the following conditions
762549472SSepherosa Ziehau  * are met:
862549472SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
962549472SSepherosa Ziehau  *    notice unmodified, this list of conditions, and the following
1062549472SSepherosa Ziehau  *    disclaimer.
1162549472SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
1262549472SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in the
1362549472SSepherosa Ziehau  *    documentation and/or other materials provided with the distribution.
1462549472SSepherosa Ziehau  *
1562549472SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1662549472SSepherosa Ziehau  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1762549472SSepherosa Ziehau  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1862549472SSepherosa Ziehau  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1962549472SSepherosa Ziehau  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2062549472SSepherosa Ziehau  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2162549472SSepherosa Ziehau  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2262549472SSepherosa Ziehau  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2362549472SSepherosa Ziehau  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2462549472SSepherosa Ziehau  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2562549472SSepherosa Ziehau  */
2662549472SSepherosa Ziehau 
2762549472SSepherosa Ziehau #ifndef _VMBUS_CHANVAR_H_
2862549472SSepherosa Ziehau #define _VMBUS_CHANVAR_H_
2962549472SSepherosa Ziehau 
3062549472SSepherosa Ziehau #include <sys/param.h>
31b99113a1SSepherosa Ziehau #include <sys/callout.h>
3262549472SSepherosa Ziehau #include <sys/lock.h>
3362549472SSepherosa Ziehau #include <sys/mutex.h>
3462549472SSepherosa Ziehau #include <sys/queue.h>
3562549472SSepherosa Ziehau #include <sys/sysctl.h>
365ab84aafSSepherosa Ziehau #include <sys/sx.h>
375ab84aafSSepherosa Ziehau #include <sys/taskqueue.h>
3862549472SSepherosa Ziehau 
3962549472SSepherosa Ziehau #include <dev/hyperv/include/hyperv.h>
4062549472SSepherosa Ziehau #include <dev/hyperv/include/vmbus.h>
41afffab7eSSepherosa Ziehau #include <dev/hyperv/vmbus/vmbus_brvar.h>
4262549472SSepherosa Ziehau 
4382b8a879SSepherosa Ziehau struct vmbus_channel {
443fcf36f4SSepherosa Ziehau 	/*
453fcf36f4SSepherosa Ziehau 	 * NOTE:
463fcf36f4SSepherosa Ziehau 	 * Fields before ch_txbr are only accessed on this channel's
473fcf36f4SSepherosa Ziehau 	 * target CPU.
483fcf36f4SSepherosa Ziehau 	 */
4962549472SSepherosa Ziehau 	uint32_t			ch_flags;	/* VMBUS_CHAN_FLAG_ */
50b99113a1SSepherosa Ziehau 	int				ch_poll_flags;	/* callout flags */
5162549472SSepherosa Ziehau 
5262549472SSepherosa Ziehau 	/*
5362549472SSepherosa Ziehau 	 * RX bufring; immediately following ch_txbr.
5462549472SSepherosa Ziehau 	 */
55ea06b5d3SSepherosa Ziehau 	struct vmbus_rxbr		ch_rxbr;
5662549472SSepherosa Ziehau 
5762549472SSepherosa Ziehau 	struct taskqueue		*ch_tq;
5862549472SSepherosa Ziehau 	struct task			ch_task;
59b99113a1SSepherosa Ziehau 	struct task			ch_poll_task;
60b99113a1SSepherosa Ziehau 	sbintime_t			ch_poll_intvl;
61b99113a1SSepherosa Ziehau 	struct callout			ch_poll_timeo;
6262549472SSepherosa Ziehau 	vmbus_chan_callback_t		ch_cb;
6362549472SSepherosa Ziehau 	void				*ch_cbarg;
6462549472SSepherosa Ziehau 
653fcf36f4SSepherosa Ziehau 	/*
663fcf36f4SSepherosa Ziehau 	 * TX bufring; at the beginning of ch_bufring.
673fcf36f4SSepherosa Ziehau 	 *
683fcf36f4SSepherosa Ziehau 	 * NOTE:
693fcf36f4SSepherosa Ziehau 	 * Put TX bufring and the following MNF/evtflag to a new
703fcf36f4SSepherosa Ziehau 	 * cacheline, since they will be accessed on all CPUs by
713fcf36f4SSepherosa Ziehau 	 * locking ch_txbr first.
723fcf36f4SSepherosa Ziehau 	 *
733fcf36f4SSepherosa Ziehau 	 * XXX
743fcf36f4SSepherosa Ziehau 	 * TX bufring and following MNF/evtflags do _not_ fit in
753fcf36f4SSepherosa Ziehau 	 * one 64B cacheline.
763fcf36f4SSepherosa Ziehau 	 */
77ea06b5d3SSepherosa Ziehau 	struct vmbus_txbr		ch_txbr __aligned(CACHE_LINE_SIZE);
783fcf36f4SSepherosa Ziehau 	uint32_t			ch_txflags;	/* VMBUS_CHAN_TXF_ */
793fcf36f4SSepherosa Ziehau 
803fcf36f4SSepherosa Ziehau 	/*
813fcf36f4SSepherosa Ziehau 	 * These are based on the vmbus_chanmsg_choffer.chm_montrig.
823fcf36f4SSepherosa Ziehau 	 * Save it here for easy access.
833fcf36f4SSepherosa Ziehau 	 */
843fcf36f4SSepherosa Ziehau 	uint32_t			ch_montrig_mask;/* MNF trig mask */
853fcf36f4SSepherosa Ziehau 	volatile uint32_t		*ch_montrig;	/* MNF trigger loc. */
863fcf36f4SSepherosa Ziehau 
873fcf36f4SSepherosa Ziehau 	/*
883fcf36f4SSepherosa Ziehau 	 * These are based on the vmbus_chanmsg_choffer.chm_chanid.
893fcf36f4SSepherosa Ziehau 	 * Save it here for easy access.
903fcf36f4SSepherosa Ziehau 	 */
913fcf36f4SSepherosa Ziehau 	u_long				ch_evtflag_mask;/* event flag */
923fcf36f4SSepherosa Ziehau 	volatile u_long			*ch_evtflag;	/* event flag loc. */
933fcf36f4SSepherosa Ziehau 
943fcf36f4SSepherosa Ziehau 	/*
953fcf36f4SSepherosa Ziehau 	 * Rarely used fields.
963fcf36f4SSepherosa Ziehau 	 */
973fcf36f4SSepherosa Ziehau 
9862549472SSepherosa Ziehau 	struct hyperv_mon_param		*ch_monprm;
9962549472SSepherosa Ziehau 
1003fcf36f4SSepherosa Ziehau 	uint32_t			ch_id;		/* channel id */
1013fcf36f4SSepherosa Ziehau 	device_t			ch_dev;
1023fcf36f4SSepherosa Ziehau 	struct vmbus_softc		*ch_vmbus;
1033fcf36f4SSepherosa Ziehau 
10462549472SSepherosa Ziehau 	int				ch_cpuid;	/* owner cpu */
10562549472SSepherosa Ziehau 	/*
10662549472SSepherosa Ziehau 	 * Virtual cpuid for ch_cpuid; it is used to communicate cpuid
10762549472SSepherosa Ziehau 	 * related information w/ Hyper-V.  If MSR_HV_VP_INDEX does not
10862549472SSepherosa Ziehau 	 * exist, ch_vcpuid will always be 0 for compatibility.
10962549472SSepherosa Ziehau 	 */
11062549472SSepherosa Ziehau 	uint32_t			ch_vcpuid;
11162549472SSepherosa Ziehau 
11262549472SSepherosa Ziehau 	/*
11362549472SSepherosa Ziehau 	 * If this is a primary channel, ch_subchan* fields
11462549472SSepherosa Ziehau 	 * contain sub-channels belonging to this primary
11562549472SSepherosa Ziehau 	 * channel.
11662549472SSepherosa Ziehau 	 */
11762549472SSepherosa Ziehau 	struct mtx			ch_subchan_lock;
11882b8a879SSepherosa Ziehau 	TAILQ_HEAD(, vmbus_channel)	ch_subchans;
11962549472SSepherosa Ziehau 	int				ch_subchan_cnt;
12062549472SSepherosa Ziehau 
12162549472SSepherosa Ziehau 	/* If this is a sub-channel */
12282b8a879SSepherosa Ziehau 	TAILQ_ENTRY(vmbus_channel)	ch_sublink;	/* sub-channel link */
12382b8a879SSepherosa Ziehau 	struct vmbus_channel		*ch_prichan;	/* owner primary chan */
12462549472SSepherosa Ziehau 
12562549472SSepherosa Ziehau 	void				*ch_bufring;	/* TX+RX bufrings */
12662549472SSepherosa Ziehau 	uint32_t			ch_bufring_gpadl;
12762549472SSepherosa Ziehau 
1289f06037bSSepherosa Ziehau 	struct task			ch_attach_task;	/* run in ch_mgmt_tq */
1299f06037bSSepherosa Ziehau 	struct task			ch_detach_task;	/* run in ch_mgmt_tq */
1309f06037bSSepherosa Ziehau 	struct taskqueue		*ch_mgmt_tq;
1319f06037bSSepherosa Ziehau 
1329f06037bSSepherosa Ziehau 	/* If this is a primary channel */
13382b8a879SSepherosa Ziehau 	TAILQ_ENTRY(vmbus_channel)	ch_prilink;	/* primary chan link */
1349f06037bSSepherosa Ziehau 
1359f06037bSSepherosa Ziehau 	TAILQ_ENTRY(vmbus_channel)	ch_link;	/* channel link */
13662549472SSepherosa Ziehau 	uint32_t			ch_subidx;	/* subchan index */
13762549472SSepherosa Ziehau 	volatile uint32_t		ch_stflags;	/* atomic-op */
13862549472SSepherosa Ziehau 							/* VMBUS_CHAN_ST_ */
13962549472SSepherosa Ziehau 	struct hyperv_guid		ch_guid_type;
14062549472SSepherosa Ziehau 	struct hyperv_guid		ch_guid_inst;
14162549472SSepherosa Ziehau 
1425ab84aafSSepherosa Ziehau 	struct sx			ch_orphan_lock;
1435ab84aafSSepherosa Ziehau 	struct vmbus_xact_ctx		*ch_orphan_xact;
1445ab84aafSSepherosa Ziehau 
14591955558SSepherosa Ziehau 	int				ch_refs;
14691955558SSepherosa Ziehau 
147*a560f3ebSWei Hu 	/*
148*a560f3ebSWei Hu 	 * These are for HyperV socket channel only
149*a560f3ebSWei Hu 	 */
150*a560f3ebSWei Hu 	bool				ch_is_hvs;
151*a560f3ebSWei Hu 	uint8_t				ch_hvs_conn_from_host;
152*a560f3ebSWei Hu 
15362549472SSepherosa Ziehau 	struct sysctl_ctx_list		ch_sysctl_ctx;
15482b8a879SSepherosa Ziehau } __aligned(CACHE_LINE_SIZE);
15562549472SSepherosa Ziehau 
15662549472SSepherosa Ziehau #define VMBUS_CHAN_ISPRIMARY(chan)	((chan)->ch_subidx == 0)
15762549472SSepherosa Ziehau 
15862549472SSepherosa Ziehau /*
15962549472SSepherosa Ziehau  * If this flag is set, this channel's interrupt will be masked in ISR,
16062549472SSepherosa Ziehau  * and the RX bufring will be drained before this channel's interrupt is
16162549472SSepherosa Ziehau  * unmasked.
16262549472SSepherosa Ziehau  *
16362549472SSepherosa Ziehau  * This flag is turned on by default.  Drivers can turn it off according
16462549472SSepherosa Ziehau  * to their own requirement.
16562549472SSepherosa Ziehau  */
16662549472SSepherosa Ziehau #define VMBUS_CHAN_FLAG_BATCHREAD	0x0002
16762549472SSepherosa Ziehau 
1683fcf36f4SSepherosa Ziehau #define VMBUS_CHAN_TXF_HASMNF		0x0001
1693fcf36f4SSepherosa Ziehau 
17062549472SSepherosa Ziehau #define VMBUS_CHAN_ST_OPENED_SHIFT	0
1719f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONPRIL_SHIFT	1
1729f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONSUBL_SHIFT	2
1739f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONLIST_SHIFT	3
174fb6216bdSSepherosa Ziehau #define VMBUS_CHAN_ST_REVOKED_SHIFT	4	/* sticky */
17562549472SSepherosa Ziehau #define VMBUS_CHAN_ST_OPENED		(1 << VMBUS_CHAN_ST_OPENED_SHIFT)
1769f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONPRIL		(1 << VMBUS_CHAN_ST_ONPRIL_SHIFT)
1779f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONSUBL		(1 << VMBUS_CHAN_ST_ONSUBL_SHIFT)
1789f06037bSSepherosa Ziehau #define VMBUS_CHAN_ST_ONLIST		(1 << VMBUS_CHAN_ST_ONLIST_SHIFT)
179fb6216bdSSepherosa Ziehau #define VMBUS_CHAN_ST_REVOKED		(1 << VMBUS_CHAN_ST_REVOKED_SHIFT)
18062549472SSepherosa Ziehau 
181f62b3ae3SSepherosa Ziehau struct vmbus_softc;
182f62b3ae3SSepherosa Ziehau struct vmbus_message;
183f62b3ae3SSepherosa Ziehau 
184f62b3ae3SSepherosa Ziehau void		vmbus_event_proc(struct vmbus_softc *, int);
185f62b3ae3SSepherosa Ziehau void		vmbus_event_proc_compat(struct vmbus_softc *, int);
1868018156fSSepherosa Ziehau void		vmbus_chan_msgproc(struct vmbus_softc *,
1878018156fSSepherosa Ziehau 		    const struct vmbus_message *);
188f62b3ae3SSepherosa Ziehau void		vmbus_chan_destroy_all(struct vmbus_softc *);
189f62b3ae3SSepherosa Ziehau 
19062549472SSepherosa Ziehau #endif	/* !_VMBUS_CHANVAR_H_ */
191