xref: /titanic_44/usr/src/uts/common/xen/io/xdf.h (revision 44e79163a6f229e84295b0d2aff268af3df641b9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #ifndef _SYS_XDF_H
29 #define	_SYS_XDF_H
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 
38 #define	BLKIF_RING_SIZE \
39 	__RING_SIZE((blkif_sring_t *)NULL, PAGESIZE)
40 #define	BLKIF_X86_32_RING_SIZE \
41 	__RING_SIZE((blkif_x86_32_sring_t *)NULL, PAGESIZE)
42 #define	BLKIF_X86_64_RING_SIZE \
43 	__RING_SIZE((blkif_x86_64_sring_t *)NULL, PAGESIZE)
44 
45 /*
46  * VBDs have standard 512 byte blocks
47  * A single blkif_request can transfer up to 11 pages of data, 1 page/segment
48  */
49 #define	XB_BSIZE	DEV_BSIZE
50 #define	XB_BMASK	(XB_BSIZE - 1)
51 #define	XB_BSHIFT	9
52 #define	XB_DTOB(bn)	((bn) << XB_BSHIFT)
53 
54 #define	XB_MAX_SEGLEN	(8 * XB_BSIZE)
55 #define	XB_SEGOFFSET	(XB_MAX_SEGLEN - 1)
56 #define	XB_MAX_XFER	(XB_MAX_SEGLEN * BLKIF_MAX_SEGMENTS_PER_REQUEST)
57 #define	XB_MAXPHYS	(XB_MAX_XFER * BLKIF_RING_SIZE)
58 
59 /*
60  * blkif status
61  */
62 enum xdf_state {
63 	/*
64 	 * initial state
65 	 */
66 	XD_UNKNOWN,
67 	/*
68 	 * ring and evtchn alloced, xenbus state changed to
69 	 * XenbusStateInitialised, wait for backend to connect
70 	 */
71 	XD_INIT,
72 	/*
73 	 * backend's xenbus state has changed to XenbusStateConnected,
74 	 * this is the only state allowing I/Os
75 	 */
76 	XD_READY,
77 	/*
78 	 * vbd interface close request received from backend, no more I/O
79 	 * requestis allowed to be put into ring buffer, while interrupt handler
80 	 * is allowed to run to finish any outstanding I/O request, disconnect
81 	 * process is kicked off by changing xenbus state to XenbusStateClosed
82 	 */
83 	XD_CLOSING,
84 	/*
85 	 * disconnection process finished, both backend and frontend's
86 	 * xenbus state has been changed to XenbusStateClosed, can be detached
87 	 */
88 	XD_CLOSED,
89 	/*
90 	 * disconnection process finished, frontend is suspended
91 	 */
92 	XD_SUSPEND
93 };
94 
95 /*
96  * 16 partitions + fdisk
97  */
98 #define	XDF_PSHIFT	6
99 #define	XDF_PMASK	((1 << XDF_PSHIFT) - 1)
100 #define	XDF_PEXT	(1 << XDF_PSHIFT)
101 #define	XDF_MINOR(i, m) (((i) << XDF_PSHIFT) | (m))
102 #define	XDF_INST(m)	((m) >> XDF_PSHIFT)
103 #define	XDF_PART(m)	((m) & XDF_PMASK)
104 
105 /*
106  * one blkif_request_t will have one corresponding ge_slot_t
107  * where we save those grant table refs used in this blkif_request_t
108  *
109  * the id of this ge_slot_t will also be put into 'id' field in
110  * each blkif_request_t when sent out to the ring buffer.
111  */
112 typedef struct ge_slot {
113 	list_node_t	link;
114 	domid_t		oeid;
115 	struct v_req	*vreq;
116 	int		isread;
117 	grant_ref_t	ghead;
118 	int		ngrefs;
119 	grant_ref_t	ge[BLKIF_MAX_SEGMENTS_PER_REQUEST];
120 } ge_slot_t;
121 
122 /*
123  * vbd I/O request
124  *
125  * An instance of this structure is bound to each buf passed to
126  * the driver's strategy by setting the pointer into bp->av_back.
127  * The id of this vreq will also be put into 'id' field in each
128  * blkif_request_t when sent out to the ring buffer for one DMA
129  * window of this buf.
130  *
131  * Vreq mainly contains DMA information for this buf. In one vreq/buf,
132  * there could be more than one DMA window, each of which will be
133  * mapped to one blkif_request_t/ge_slot_t. Ge_slot_t contains all grant
134  * table entry information for this buf. The ge_slot_t for current DMA
135  * window is pointed to by v_gs in vreq.
136  *
137  * So, grant table entries will only be alloc'ed when the DMA window is
138  * about to be transferred via blkif_request_t to the ring buffer. And
139  * they will be freed right after the blkif_response_t is seen. By this
140  * means, we can make use of grant table entries more efficiently.
141  */
142 typedef struct v_req {
143 	list_node_t	v_link;
144 	int		v_status;
145 	buf_t		*v_buf;
146 	ddi_dma_handle_t v_dmahdl;
147 	ddi_dma_cookie_t v_dmac;
148 	uint_t		v_ndmacs;
149 	uint_t		v_dmaw;
150 	uint_t		v_ndmaws;
151 	uint_t		v_nslots;
152 	ge_slot_t	*v_gs;
153 	uint64_t	v_blkno;
154 	ddi_acc_handle_t v_align;
155 	caddr_t		v_abuf;
156 	ddi_dma_handle_t v_memdmahdl;
157 	uint8_t		v_flush_diskcache;
158 } v_req_t;
159 
160 /*
161  * Status set and checked in vreq->v_status by vreq_setup()
162  *
163  * These flags will help us to continue the vreq setup work from last failure
164  * point, instead of starting from scratch after each failure.
165  */
166 #define	VREQ_INIT		0x0
167 #define	VREQ_INIT_DONE		0x1
168 #define	VREQ_DMAHDL_ALLOCED	0x2
169 #define	VREQ_MEMDMAHDL_ALLOCED	0x3
170 #define	VREQ_DMAMEM_ALLOCED	0x4
171 #define	VREQ_DMABUF_BOUND	0x5
172 #define	VREQ_GS_ALLOCED		0x6
173 #define	VREQ_DMAWIN_DONE	0x7
174 
175 /*
176  * virtual block device per-instance softstate
177  */
178 typedef struct xdf {
179 	dev_info_t	*xdf_dip;
180 	domid_t		xdf_peer; /* otherend's dom ID */
181 	xendev_ring_t	*xdf_xb_ring; /* I/O ring buffer */
182 	ddi_acc_handle_t xdf_xb_ring_hdl; /* access handler for ring buffer */
183 	list_t		xdf_vreq_act; /* active vreq list */
184 	list_t		xdf_gs_act; /* active grant table slot list */
185 	buf_t		*xdf_f_act; /* active buf list head */
186 	buf_t		*xdf_l_act; /* active buf list tail */
187 	enum xdf_state	xdf_status; /* status of this virtual disk */
188 	ulong_t		xdf_vd_open[OTYPCNT];
189 	ulong_t		xdf_vd_lyropen[XDF_PEXT];
190 	ulong_t		xdf_vd_exclopen;
191 	kmutex_t	xdf_dev_lk; /* mutex lock for I/O path */
192 	kmutex_t	xdf_cb_lk; /* mutex lock for event handling path */
193 	kcondvar_t	xdf_dev_cv; /* cv used in I/O path */
194 	uint_t		xdf_xdev_info; /* disk info from backend xenstore */
195 	diskaddr_t	xdf_xdev_nblocks; /* total size in block */
196 	kstat_t		*xdf_xdev_iostat;
197 	cmlb_handle_t	xdf_vd_lbl;
198 	ddi_softintr_t	xdf_softintr_id;
199 	timeout_id_t	xdf_timeout_id;
200 	struct gnttab_free_callback xdf_gnt_callback;
201 	int		xdf_feature_barrier;
202 	int		xdf_flush_supported;
203 	int		xdf_wce;
204 	char		*xdf_flush_mem;
205 	char		*xdf_cache_flush_block;
206 	int		xdf_evtchn;
207 #ifdef	DEBUG
208 	int		xdf_dmacallback_num;
209 #endif
210 } xdf_t;
211 
212 #define	BP2VREQ(bp)	((v_req_t *)((bp)->av_back))
213 
214 /*
215  * VBD I/O requests must be aligned on a 512-byte boundary and specify
216  * a transfer size which is a mutiple of 512-bytes
217  */
218 #define	ALIGNED_XFER(bp) \
219 	((((uintptr_t)((bp)->b_un.b_addr) & XB_BMASK) == 0) && \
220 	(((bp)->b_bcount & XB_BMASK) == 0))
221 
222 #define	U_INVAL(u)	(((u)->uio_loffset & (offset_t)(XB_BMASK)) || \
223 	((u)->uio_iov->iov_len & (offset_t)(XB_BMASK)))
224 
225 /* wrap pa_to_ma() for xdf to run in dom0 */
226 #define	PATOMA(addr)	(DOMAIN_IS_INITDOMAIN(xen_info) ? addr : pa_to_ma(addr))
227 
228 #define	XD_IS_RO(vbd)	((vbd)->xdf_xdev_info & VDISK_READONLY)
229 #define	XD_IS_CD(vbd)	((vbd)->xdf_xdev_info & VDISK_CDROM)
230 #define	XD_IS_RM(vbd)	((vbd)->xdf_xdev_info & VDISK_REMOVABLE)
231 #define	IS_READ(bp)	((bp)->b_flags & B_READ)
232 #define	IS_ERROR(bp)	((bp)->b_flags & B_ERROR)
233 
234 #define	XDF_UPDATE_IO_STAT(vdp, bp)					\
235 	if ((vdp)->xdf_xdev_iostat != NULL) {				\
236 		kstat_io_t *kip = KSTAT_IO_PTR((vdp)->xdf_xdev_iostat);	\
237 		size_t n_done = (bp)->b_bcount - (bp)->b_resid;		\
238 		if ((bp)->b_flags & B_READ) {				\
239 			kip->reads++;					\
240 			kip->nread += n_done;				\
241 		} else {                                                \
242 			kip->writes++;					\
243 			kip->nwritten += n_done;			\
244 		}							\
245 	}
246 
247 extern int xdfdebug;
248 #ifdef DEBUG
249 #define	DPRINTF(flag, args)	{if (xdfdebug & (flag)) prom_printf args; }
250 #define	SETDMACBON(vbd)		{(vbd)->xdf_dmacallback_num++; }
251 #define	SETDMACBOFF(vbd)	{(vbd)->xdf_dmacallback_num--; }
252 #define	ISDMACBON(vbd)		((vbd)->xdf_dmacallback_num > 0)
253 #else
254 #define	DPRINTF(flag, args)
255 #define	SETDMACBON(vbd)
256 #define	SETDMACBOFF(vbd)
257 #define	ISDMACBON(vbd)
258 #endif /* DEBUG */
259 
260 #define	DDI_DBG		0x1
261 #define	DMA_DBG		0x2
262 #define	INTR_DBG	0x8
263 #define	IO_DBG		0x10
264 #define	IOCTL_DBG	0x20
265 #define	SUSRES_DBG	0x40
266 #define	LBL_DBG		0x80
267 
268 #ifdef __cplusplus
269 }
270 #endif
271 
272 #endif	/* _SYS_XDF_H */
273