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