xref: /illumos-gate/usr/src/uts/common/xen/io/xdf.h (revision 5279807d7e1818eac6f90ac640b7a89cdb37522d)
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 2009 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 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/cmlb.h>
34 #include <sys/dkio.h>
35 
36 #include <sys/gnttab.h>
37 #include <xen/sys/xendev.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /*
45  * VBDs have standard 512 byte blocks
46  * A single blkif_request can transfer up to 11 pages of data, 1 page/segment
47  */
48 #define	XB_BSIZE	DEV_BSIZE
49 #define	XB_BMASK	(XB_BSIZE - 1)
50 #define	XB_BSHIFT	9
51 #define	XB_DTOB(bn)	((bn) << XB_BSHIFT)
52 
53 #define	XB_MAX_SEGLEN	(8 * XB_BSIZE)
54 #define	XB_SEGOFFSET	(XB_MAX_SEGLEN - 1)
55 #define	XB_MAX_XFER	(XB_MAX_SEGLEN * BLKIF_MAX_SEGMENTS_PER_REQUEST)
56 #define	XB_MAXPHYS	(XB_MAX_XFER * BLKIF_RING_SIZE)
57 
58 
59 /*
60  * Slice for absolute disk transaction.
61  *
62  * Hack Alert.  XB_SLICE_NONE is a magic value that can be written into the
63  * b_private field of buf structures passed to xdf_strategy().  When present
64  * it indicates that the I/O is using an absolute offset.  (ie, the I/O is
65  * not bound to any one partition.)  This magic value is currently used by
66  * the pv_cmdk driver.  This hack is shamelessly stolen from the sun4v vdc
67  * driver, another virtual disk device driver.  (Although in the case of
68  * vdc the hack is less egregious since it is self contained within the
69  * vdc driver, where as here it is used as an interface between the pv_cmdk
70  * driver and the xdf driver.)
71  */
72 #define	XB_SLICE_NONE		0xFF
73 
74 /*
75  * blkif status
76  */
77 typedef enum xdf_state {
78 	/*
79 	 * initial state
80 	 */
81 	XD_UNKNOWN = 0,
82 	/*
83 	 * ring and evtchn alloced, xenbus state changed to
84 	 * XenbusStateInitialised, wait for backend to connect
85 	 */
86 	XD_INIT = 1,
87 	/*
88 	 * backend and frontend xenbus state has changed to
89 	 * XenbusStateConnected.  IO is now allowed, but we are not still
90 	 * fully initialized.
91 	 */
92 	XD_CONNECTED = 2,
93 	/*
94 	 * We're fully initialized and allowing regular IO.
95 	 */
96 	XD_READY = 3,
97 	/*
98 	 * vbd interface close request received from backend, no more I/O
99 	 * requestis allowed to be put into ring buffer, while interrupt handler
100 	 * is allowed to run to finish any outstanding I/O request, disconnect
101 	 * process is kicked off by changing xenbus state to XenbusStateClosed
102 	 */
103 	XD_CLOSING = 4,
104 	/*
105 	 * disconnection process finished, both backend and frontend's
106 	 * xenbus state has been changed to XenbusStateClosed, can be detached
107 	 */
108 	XD_CLOSED = 5,
109 	/*
110 	 * We're either being suspended or resuming from a suspend.  If we're
111 	 * in the process of suspending, we block all new IO, but but allow
112 	 * existing IO to drain.
113 	 */
114 	XD_SUSPEND = 6
115 } xdf_state_t;
116 
117 /*
118  * 16 partitions + fdisk
119  */
120 #define	XDF_PSHIFT	6
121 #define	XDF_PMASK	((1 << XDF_PSHIFT) - 1)
122 #define	XDF_PEXT	(1 << XDF_PSHIFT)
123 #define	XDF_MINOR(i, m) (((i) << XDF_PSHIFT) | (m))
124 #define	XDF_INST(m)	((m) >> XDF_PSHIFT)
125 #define	XDF_PART(m)	((m) & XDF_PMASK)
126 
127 /*
128  * one blkif_request_t will have one corresponding ge_slot_t
129  * where we save those grant table refs used in this blkif_request_t
130  *
131  * the id of this ge_slot_t will also be put into 'id' field in
132  * each blkif_request_t when sent out to the ring buffer.
133  */
134 typedef struct ge_slot {
135 	list_node_t	gs_vreq_link;
136 	struct v_req	*gs_vreq;
137 	domid_t		gs_oeid;
138 	int		gs_isread;
139 	grant_ref_t	gs_ghead;
140 	int		gs_ngrefs;
141 	grant_ref_t	gs_ge[BLKIF_MAX_SEGMENTS_PER_REQUEST];
142 } ge_slot_t;
143 
144 /*
145  * vbd I/O request
146  *
147  * An instance of this structure is bound to each buf passed to
148  * the driver's strategy by setting the pointer into bp->av_back.
149  * The id of this vreq will also be put into 'id' field in each
150  * blkif_request_t when sent out to the ring buffer for one DMA
151  * window of this buf.
152  *
153  * Vreq mainly contains DMA information for this buf. In one vreq/buf,
154  * there could be more than one DMA window, each of which will be
155  * mapped to one blkif_request_t/ge_slot_t. Ge_slot_t contains all grant
156  * table entry information for this buf. The ge_slot_t for current DMA
157  * window is pointed to by v_gs in vreq.
158  *
159  * So, grant table entries will only be alloc'ed when the DMA window is
160  * about to be transferred via blkif_request_t to the ring buffer. And
161  * they will be freed right after the blkif_response_t is seen. By this
162  * means, we can make use of grant table entries more efficiently.
163  */
164 typedef struct v_req {
165 	list_node_t	v_link;
166 	list_t		v_gs;
167 	int		v_status;
168 	buf_t		*v_buf;
169 	uint_t		v_ndmacs;
170 	uint_t		v_dmaw;
171 	uint_t		v_ndmaws;
172 	uint_t		v_nslots;
173 	uint64_t	v_blkno;
174 	ddi_dma_handle_t v_memdmahdl;
175 	ddi_acc_handle_t v_align;
176 	ddi_dma_handle_t v_dmahdl;
177 	ddi_dma_cookie_t v_dmac;
178 	caddr_t		v_abuf;
179 	uint8_t		v_flush_diskcache;
180 	boolean_t	v_runq;
181 } v_req_t;
182 
183 /*
184  * Status set and checked in vreq->v_status by vreq_setup()
185  *
186  * These flags will help us to continue the vreq setup work from last failure
187  * point, instead of starting from scratch after each failure.
188  */
189 #define	VREQ_INIT		0x0
190 #define	VREQ_INIT_DONE		0x1
191 #define	VREQ_DMAHDL_ALLOCED	0x2
192 #define	VREQ_MEMDMAHDL_ALLOCED	0x3
193 #define	VREQ_DMAMEM_ALLOCED	0x4
194 #define	VREQ_DMABUF_BOUND	0x5
195 #define	VREQ_GS_ALLOCED		0x6
196 #define	VREQ_DMAWIN_DONE	0x7
197 
198 /*
199  * virtual block device per-instance softstate
200  */
201 typedef struct xdf {
202 	dev_info_t	*xdf_dip;
203 	char		*xdf_addr;
204 	ddi_iblock_cookie_t xdf_ibc; /* mutex iblock cookie */
205 	domid_t		xdf_peer; /* otherend's dom ID */
206 	xendev_ring_t	*xdf_xb_ring; /* I/O ring buffer */
207 	ddi_acc_handle_t xdf_xb_ring_hdl; /* access handler for ring buffer */
208 	list_t		xdf_vreq_act; /* active vreq list */
209 	buf_t		*xdf_f_act; /* active buf list head */
210 	buf_t		*xdf_l_act; /* active buf list tail */
211 	buf_t		*xdf_i_act; /* active buf list index */
212 	xdf_state_t	xdf_state; /* status of this virtual disk */
213 	boolean_t	xdf_suspending;
214 	ulong_t		xdf_vd_open[OTYPCNT];
215 	ulong_t		xdf_vd_lyropen[XDF_PEXT];
216 	ulong_t		xdf_connect_req;
217 	kthread_t	*xdf_connect_thread;
218 	ulong_t		xdf_vd_exclopen;
219 	kmutex_t	xdf_iostat_lk; /* muxes lock for the iostat ptr */
220 	kmutex_t	xdf_dev_lk; /* mutex lock for I/O path */
221 	kmutex_t	xdf_cb_lk; /* mutex lock for event handling path */
222 	kcondvar_t	xdf_dev_cv; /* cv used in I/O path */
223 	uint_t		xdf_dinfo; /* disk info from backend xenstore */
224 	diskaddr_t	xdf_xdev_nblocks; /* total size in block */
225 	cmlb_geom_t	xdf_pgeom;
226 	boolean_t	xdf_pgeom_set;
227 	boolean_t	xdf_pgeom_fixed;
228 	kstat_t		*xdf_xdev_iostat;
229 	cmlb_handle_t	xdf_vd_lbl;
230 	ddi_softintr_t	xdf_softintr_id;
231 	timeout_id_t	xdf_timeout_id;
232 	struct gnttab_free_callback xdf_gnt_callback;
233 	boolean_t	xdf_feature_barrier;
234 	boolean_t	xdf_flush_supported;
235 	boolean_t	xdf_media_req_supported;
236 	boolean_t	xdf_wce;
237 	boolean_t	xdf_cmbl_reattach;
238 	char		*xdf_flush_mem;
239 	char		*xdf_cache_flush_block;
240 	int		xdf_evtchn;
241 	enum dkio_state	xdf_mstate;
242 	kcondvar_t	xdf_mstate_cv;
243 	kcondvar_t	xdf_hp_status_cv;
244 	struct buf	*xdf_ready_bp;
245 	ddi_taskq_t	*xdf_ready_tq;
246 	kthread_t	*xdf_ready_tq_thread;
247 	struct buf	*xdf_ready_tq_bp;
248 #ifdef	DEBUG
249 	int		xdf_dmacallback_num;
250 	kthread_t	*xdf_oe_change_thread;
251 #endif
252 } xdf_t;
253 
254 /*
255  * VBD I/O requests must be aligned on a 512-byte boundary and specify
256  * a transfer size which is a mutiple of 512-bytes
257  */
258 #define	ALIGNED_XFER(bp) \
259 	((((uintptr_t)((bp)->b_un.b_addr) & XB_BMASK) == 0) && \
260 	(((bp)->b_bcount & XB_BMASK) == 0))
261 
262 #define	U_INVAL(u)	(((u)->uio_loffset & (offset_t)(XB_BMASK)) || \
263 	((u)->uio_iov->iov_len & (offset_t)(XB_BMASK)))
264 
265 /* wrap pa_to_ma() for xdf to run in dom0 */
266 #define	PATOMA(addr)	(DOMAIN_IS_INITDOMAIN(xen_info) ? addr : pa_to_ma(addr))
267 
268 #define	XD_IS_RO(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_READONLY)
269 #define	XD_IS_CD(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_CDROM)
270 #define	XD_IS_RM(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_REMOVABLE)
271 #define	IS_READ(bp)	VOID2BOOLEAN((bp)->b_flags & B_READ)
272 #define	IS_ERROR(bp)	VOID2BOOLEAN((bp)->b_flags & B_ERROR)
273 
274 #define	XDF_UPDATE_IO_STAT(vdp, bp)					\
275 	{								\
276 		kstat_io_t *kip = KSTAT_IO_PTR((vdp)->xdf_xdev_iostat);	\
277 		size_t n_done = (bp)->b_bcount - (bp)->b_resid;		\
278 		if ((bp)->b_flags & B_READ) {				\
279 			kip->reads++;					\
280 			kip->nread += n_done;				\
281 		} else {                                                \
282 			kip->writes++;					\
283 			kip->nwritten += n_done;			\
284 		}							\
285 	}
286 
287 #ifdef DEBUG
288 #define	DPRINTF(flag, args)	{if (xdf_debug & (flag)) prom_printf args; }
289 #define	SETDMACBON(vbd)		{(vbd)->xdf_dmacallback_num++; }
290 #define	SETDMACBOFF(vbd)	{(vbd)->xdf_dmacallback_num--; }
291 #define	ISDMACBON(vbd)		((vbd)->xdf_dmacallback_num > 0)
292 #else
293 #define	DPRINTF(flag, args)
294 #define	SETDMACBON(vbd)
295 #define	SETDMACBOFF(vbd)
296 #define	ISDMACBON(vbd)
297 #endif /* DEBUG */
298 
299 #define	DDI_DBG		0x1
300 #define	DMA_DBG		0x2
301 #define	INTR_DBG	0x8
302 #define	IO_DBG		0x10
303 #define	IOCTL_DBG	0x20
304 #define	SUSRES_DBG	0x40
305 #define	LBL_DBG		0x80
306 
307 #if defined(XPV_HVM_DRIVER)
308 extern int xdf_lb_getinfo(dev_info_t *, int, void *, void *);
309 extern int xdf_lb_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t,
310     void *);
311 extern void xdfmin(struct buf *bp);
312 extern dev_info_t *xdf_hvm_hold(const char *);
313 extern boolean_t xdf_hvm_connect(dev_info_t *);
314 extern int xdf_hvm_setpgeom(dev_info_t *, cmlb_geom_t *);
315 extern int xdf_kstat_create(dev_info_t *, char *, int);
316 extern void xdf_kstat_delete(dev_info_t *);
317 extern boolean_t xdf_is_cd(dev_info_t *);
318 extern boolean_t xdf_is_rm(dev_info_t *);
319 extern boolean_t xdf_media_req_supported(dev_info_t *);
320 #endif /* XPV_HVM_DRIVER */
321 
322 #ifdef __cplusplus
323 }
324 #endif
325 
326 #endif	/* _SYS_XDF_H */
327