xref: /titanic_41/usr/src/uts/common/xen/io/xdf.h (revision 269552cd714f2f1e308fdde0fc69b18a7f1142b2)
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 	ulong_t		xdf_vd_exclopen;
218 	kmutex_t	xdf_iostat_lk; /* muxes lock for the iostat ptr */
219 	kmutex_t	xdf_dev_lk; /* mutex lock for I/O path */
220 	kmutex_t	xdf_cb_lk; /* mutex lock for event handling path */
221 	kcondvar_t	xdf_dev_cv; /* cv used in I/O path */
222 	uint_t		xdf_dinfo; /* disk info from backend xenstore */
223 	diskaddr_t	xdf_xdev_nblocks; /* total size in block */
224 	cmlb_geom_t	xdf_pgeom;
225 	boolean_t	xdf_pgeom_set;
226 	boolean_t	xdf_pgeom_fixed;
227 	kstat_t		*xdf_xdev_iostat;
228 	cmlb_handle_t	xdf_vd_lbl;
229 	ddi_softintr_t	xdf_softintr_id;
230 	timeout_id_t	xdf_timeout_id;
231 	struct gnttab_free_callback xdf_gnt_callback;
232 	boolean_t	xdf_feature_barrier;
233 	boolean_t	xdf_flush_supported;
234 	boolean_t	xdf_media_req_supported;
235 	boolean_t	xdf_wce;
236 	boolean_t	xdf_cmbl_reattach;
237 	char		*xdf_flush_mem;
238 	char		*xdf_cache_flush_block;
239 	int		xdf_evtchn;
240 	enum dkio_state	xdf_mstate;
241 	kcondvar_t	xdf_mstate_cv;
242 	kcondvar_t	xdf_hp_status_cv;
243 	struct buf	*xdf_ready_bp;
244 	ddi_taskq_t	*xdf_ready_tq;
245 	kthread_t	*xdf_ready_tq_thread;
246 	struct buf	*xdf_ready_tq_bp;
247 #ifdef	DEBUG
248 	int		xdf_dmacallback_num;
249 	kthread_t	*xdf_oe_change_thread;
250 #endif
251 } xdf_t;
252 
253 /*
254  * VBD I/O requests must be aligned on a 512-byte boundary and specify
255  * a transfer size which is a mutiple of 512-bytes
256  */
257 #define	ALIGNED_XFER(bp) \
258 	((((uintptr_t)((bp)->b_un.b_addr) & XB_BMASK) == 0) && \
259 	(((bp)->b_bcount & XB_BMASK) == 0))
260 
261 #define	U_INVAL(u)	(((u)->uio_loffset & (offset_t)(XB_BMASK)) || \
262 	((u)->uio_iov->iov_len & (offset_t)(XB_BMASK)))
263 
264 /* wrap pa_to_ma() for xdf to run in dom0 */
265 #define	PATOMA(addr)	(DOMAIN_IS_INITDOMAIN(xen_info) ? addr : pa_to_ma(addr))
266 
267 #define	XD_IS_RO(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_READONLY)
268 #define	XD_IS_CD(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_CDROM)
269 #define	XD_IS_RM(vbd)	VOID2BOOLEAN((vbd)->xdf_dinfo & VDISK_REMOVABLE)
270 #define	IS_READ(bp)	VOID2BOOLEAN((bp)->b_flags & B_READ)
271 #define	IS_ERROR(bp)	VOID2BOOLEAN((bp)->b_flags & B_ERROR)
272 
273 #define	XDF_UPDATE_IO_STAT(vdp, bp)					\
274 	{								\
275 		kstat_io_t *kip = KSTAT_IO_PTR((vdp)->xdf_xdev_iostat);	\
276 		size_t n_done = (bp)->b_bcount - (bp)->b_resid;		\
277 		if ((bp)->b_flags & B_READ) {				\
278 			kip->reads++;					\
279 			kip->nread += n_done;				\
280 		} else {                                                \
281 			kip->writes++;					\
282 			kip->nwritten += n_done;			\
283 		}							\
284 	}
285 
286 #ifdef DEBUG
287 #define	DPRINTF(flag, args)	{if (xdf_debug & (flag)) prom_printf args; }
288 #define	SETDMACBON(vbd)		{(vbd)->xdf_dmacallback_num++; }
289 #define	SETDMACBOFF(vbd)	{(vbd)->xdf_dmacallback_num--; }
290 #define	ISDMACBON(vbd)		((vbd)->xdf_dmacallback_num > 0)
291 #else
292 #define	DPRINTF(flag, args)
293 #define	SETDMACBON(vbd)
294 #define	SETDMACBOFF(vbd)
295 #define	ISDMACBON(vbd)
296 #endif /* DEBUG */
297 
298 #define	DDI_DBG		0x1
299 #define	DMA_DBG		0x2
300 #define	INTR_DBG	0x8
301 #define	IO_DBG		0x10
302 #define	IOCTL_DBG	0x20
303 #define	SUSRES_DBG	0x40
304 #define	LBL_DBG		0x80
305 
306 #if defined(XPV_HVM_DRIVER)
307 extern int xdf_lb_getinfo(dev_info_t *, int, void *, void *);
308 extern int xdf_lb_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t,
309     void *);
310 extern void xdfmin(struct buf *bp);
311 extern dev_info_t *xdf_hvm_hold(const char *);
312 extern boolean_t xdf_hvm_connect(dev_info_t *);
313 extern int xdf_hvm_setpgeom(dev_info_t *, cmlb_geom_t *);
314 extern int xdf_kstat_create(dev_info_t *, char *, int);
315 extern void xdf_kstat_delete(dev_info_t *);
316 extern boolean_t xdf_is_cd(dev_info_t *);
317 extern boolean_t xdf_is_rm(dev_info_t *);
318 extern boolean_t xdf_media_req_supported(dev_info_t *);
319 #endif /* XPV_HVM_DRIVER */
320 
321 #ifdef __cplusplus
322 }
323 #endif
324 
325 #endif	/* _SYS_XDF_H */
326