xref: /freebsd/sys/dev/xen/blkfront/block.h (revision 55bce0c1203e70d8b62a3dedc9235ab39660c6f4)
1 /*
2  * XenBSD block device driver
3  *
4  * Copyright (c) 2010-2013 Spectra Logic Corporation
5  * Copyright (c) 2009 Scott Long, Yahoo!
6  * Copyright (c) 2009 Frank Suchomel, Citrix
7  * Copyright (c) 2009 Doug F. Rabson, Citrix
8  * Copyright (c) 2005 Kip Macy
9  * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10  * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
11  *
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this software and associated documentation files (the "Software"), to
15  * deal in the Software without restriction, including without limitation the
16  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17  * sell copies of the Software, and to permit persons to whom the Software is
18  * furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef __XEN_BLKFRONT_BLOCK_H__
34 #define __XEN_BLKFRONT_BLOCK_H__
35 #include <xen/blkif.h>
36 
37 /**
38  * Given a number of blkif segments, compute the maximum I/O size supported.
39  *
40  * \note This calculation assumes that all but the first and last segments
41  *       of the I/O are fully utilized.
42  *
43  * \note We reserve a segement from the maximum supported by the transport to
44  *       guarantee we can handle an unaligned transfer without the need to
45  *       use a bounce buffer.
46  */
47 #define	XBD_SEGS_TO_SIZE(segs)						\
48 	(((segs) - 1) * PAGE_SIZE)
49 
50 /**
51  * Compute the maximum number of blkif segments requried to represent
52  * an I/O of the given size.
53  *
54  * \note This calculation assumes that all but the first and last segments
55  *       of the I/O are fully utilized.
56  *
57  * \note We reserve a segement to guarantee we can handle an unaligned
58  *       transfer without the need to use a bounce buffer.
59  */
60 #define	XBD_SIZE_TO_SEGS(size)						\
61 	((size / PAGE_SIZE) + 1)
62 
63 /**
64  * The maximum number of outstanding requests blocks (request headers plus
65  * additional segment blocks) we will allow in a negotiated block-front/back
66  * communication channel.
67  */
68 #define XBD_MAX_REQUESTS		256
69 
70 /**
71  * The maximum mapped region size per request we will allow in a negotiated
72  * block-front/back communication channel.
73  */
74 #define	XBD_MAX_REQUEST_SIZE						\
75 	MIN(MAXPHYS, XBD_SEGS_TO_SIZE(BLKIF_MAX_SEGMENTS_PER_REQUEST))
76 
77 /**
78  * The maximum number of segments (within a request header and accompanying
79  * segment blocks) per request we will allow in a negotiated block-front/back
80  * communication channel.
81  */
82 #define	XBD_MAX_SEGMENTS_PER_REQUEST					\
83 	(MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST,				\
84 	     XBD_SIZE_TO_SEGS(XBD_MAX_REQUEST_SIZE)))
85 
86 /**
87  * The maximum number of shared memory ring pages we will allow in a
88  * negotiated block-front/back communication channel.  Allow enough
89  * ring space for all requests to be  XBD_MAX_REQUEST_SIZE'd.
90  */
91 #define XBD_MAX_RING_PAGES						    \
92 	BLKIF_RING_PAGES(BLKIF_SEGS_TO_BLOCKS(XBD_MAX_SEGMENTS_PER_REQUEST) \
93 		       * XBD_MAX_REQUESTS)
94 
95 typedef enum {
96 	XBDCF_Q_MASK		= 0xFF,
97 	XBDCF_FROZEN		= 1<<8,
98 	XBDCF_POLLED		= 1<<9,
99 	XBDCF_ASYNC_MAPPING	= 1<<10,
100 	XBDCF_INITIALIZER	= XBDCF_Q_MASK
101 } xbdc_flag_t;
102 
103 struct xbd_command;
104 typedef void xbd_cbcf_t(struct xbd_command *);
105 
106 struct xbd_command {
107 	TAILQ_ENTRY(xbd_command) cm_link;
108 	struct xbd_softc	*cm_sc;
109 	xbdc_flag_t		 cm_flags;
110 	bus_dmamap_t		 cm_map;
111 	uint64_t		 cm_id;
112 	grant_ref_t		*cm_sg_refs;
113 	struct bio		*cm_bp;
114 	grant_ref_t		 cm_gref_head;
115 	void			*cm_data;
116 	size_t			 cm_datalen;
117 	u_int			 cm_nseg;
118 	int			 cm_operation;
119 	blkif_sector_t		 cm_sector_number;
120 	int			 cm_status;
121 	xbd_cbcf_t		*cm_complete;
122 };
123 
124 typedef enum {
125 	XBD_Q_FREE,
126 	XBD_Q_READY,
127 	XBD_Q_BUSY,
128 	XBD_Q_COMPLETE,
129 	XBD_Q_BIO,
130 	XBD_Q_COUNT,
131 	XBD_Q_NONE = XBDCF_Q_MASK
132 } xbd_q_index_t;
133 
134 typedef struct xbd_cm_q {
135 	TAILQ_HEAD(, xbd_command) q_tailq;
136 	uint32_t		  q_length;
137 	uint32_t		  q_max;
138 } xbd_cm_q_t;
139 
140 typedef enum {
141 	XBD_STATE_DISCONNECTED,
142 	XBD_STATE_CONNECTED,
143 	XBD_STATE_SUSPENDED
144 } xbd_state_t;
145 
146 typedef enum {
147 	XBDF_NONE	  = 0,
148 	XBDF_OPEN	  = 1 << 0, /* drive is open (can't shut down) */
149 	XBDF_BARRIER	  = 1 << 1, /* backend supports barriers */
150 	XBDF_READY	  = 1 << 2, /* Is ready */
151 	XBDF_CM_SHORTAGE  = 1 << 3, /* Free cm resource shortage active. */
152 	XBDF_GNT_SHORTAGE = 1 << 4  /* Grant ref resource shortage active */
153 } xbd_flag_t;
154 
155 /*
156  * We have one of these per vbd, whether ide, scsi or 'other'.
157  */
158 struct xbd_softc {
159 	device_t			 xbd_dev;
160 	struct disk			*xbd_disk;	/* disk params */
161 	struct bio_queue_head 		 xbd_bioq;	/* sort queue */
162 	int				 xbd_unit;
163 	xbd_flag_t			 xbd_flags;
164 	int				 xbd_qfrozen_cnt;
165 	int				 xbd_vdevice;
166 	xbd_state_t			 xbd_state;
167 	u_int				 xbd_ring_pages;
168 	uint32_t			 xbd_max_requests;
169 	uint32_t			 xbd_max_request_segments;
170 	uint32_t			 xbd_max_request_blocks;
171 	uint32_t			 xbd_max_request_size;
172 	grant_ref_t			 xbd_ring_ref[XBD_MAX_RING_PAGES];
173 	blkif_front_ring_t		 xbd_ring;
174 	unsigned int			 xbd_irq;
175 	struct gnttab_free_callback	 xbd_callback;
176 	xbd_cm_q_t			 xbd_cm_q[XBD_Q_COUNT];
177 	bus_dma_tag_t			 xbd_io_dmat;
178 
179 	/**
180 	 * The number of people holding this device open.  We won't allow a
181 	 * hot-unplug unless this is 0.
182 	 */
183 	int				 xbd_users;
184 	struct mtx			 xbd_io_lock;
185 
186 	struct xbd_command		*xbd_shadow;
187 };
188 
189 int xbd_instance_create(struct xbd_softc *, blkif_sector_t sectors, int device,
190 			uint16_t vdisk_info, unsigned long sector_size);
191 
192 static inline void
193 xbd_added_qentry(struct xbd_softc *sc, xbd_q_index_t index)
194 {
195 	struct xbd_cm_q *cmq;
196 
197 	cmq = &sc->xbd_cm_q[index];
198 	cmq->q_length++;
199 	if (cmq->q_length > cmq->q_max)
200 		cmq->q_max = cmq->q_length;
201 }
202 
203 static inline void
204 xbd_removed_qentry(struct xbd_softc *sc, xbd_q_index_t index)
205 {
206 	sc->xbd_cm_q[index].q_length--;
207 }
208 
209 static inline void
210 xbd_initq_cm(struct xbd_softc *sc, xbd_q_index_t index)
211 {
212 	struct xbd_cm_q *cmq;
213 
214 	cmq = &sc->xbd_cm_q[index];
215 	TAILQ_INIT(&cmq->q_tailq);
216 	cmq->q_length = 0;
217 	cmq->q_max = 0;
218 }
219 
220 static inline void
221 xbd_enqueue_cm(struct xbd_command *cm, xbd_q_index_t index)
222 {
223 	KASSERT(index != XBD_Q_BIO,
224 	    ("%s: Commands cannot access the bio queue.", __func__));
225 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
226 		panic("%s: command %p is already on queue %d.",
227 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
228 	TAILQ_INSERT_TAIL(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
229 	cm->cm_flags &= ~XBDCF_Q_MASK;
230 	cm->cm_flags |= index;
231 	xbd_added_qentry(cm->cm_sc, index);
232 }
233 
234 static inline void
235 xbd_requeue_cm(struct xbd_command *cm, xbd_q_index_t index)
236 {
237 	KASSERT(index != XBD_Q_BIO,
238 	    ("%s: Commands cannot access the bio queue.", __func__));
239 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
240 		panic("%s: command %p is already on queue %d.",
241 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
242 	TAILQ_INSERT_HEAD(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
243 	cm->cm_flags &= ~XBDCF_Q_MASK;
244 	cm->cm_flags |= index;
245 	xbd_added_qentry(cm->cm_sc, index);
246 }
247 
248 static inline struct xbd_command *
249 xbd_dequeue_cm(struct xbd_softc *sc, xbd_q_index_t index)
250 {
251 	struct xbd_command *cm;
252 
253 	KASSERT(index != XBD_Q_BIO,
254 	    ("%s: Commands cannot access the bio queue.", __func__));
255 
256 	if ((cm = TAILQ_FIRST(&sc->xbd_cm_q[index].q_tailq)) != NULL) {
257 		if ((cm->cm_flags & XBDCF_Q_MASK) != index) {
258 			panic("%s: command %p is on queue %d, "
259 			    "not specified queue %d",
260 			    __func__, cm,
261 			    cm->cm_flags & XBDCF_Q_MASK,
262 			    index);
263 		}
264 		TAILQ_REMOVE(&sc->xbd_cm_q[index].q_tailq, cm, cm_link);
265 		cm->cm_flags &= ~XBDCF_Q_MASK;
266 		cm->cm_flags |= XBD_Q_NONE;
267 		xbd_removed_qentry(cm->cm_sc, index);
268 	}
269 	return (cm);
270 }
271 
272 static inline void
273 xbd_remove_cm(struct xbd_command *cm, xbd_q_index_t expected_index)
274 {
275 	xbd_q_index_t index;
276 
277 	index = cm->cm_flags & XBDCF_Q_MASK;
278 
279 	KASSERT(index != XBD_Q_BIO,
280 	    ("%s: Commands cannot access the bio queue.", __func__));
281 
282 	if (index != expected_index) {
283 		panic("%s: command %p is on queue %d, not specified queue %d",
284 		    __func__, cm, index, expected_index);
285 	}
286 	TAILQ_REMOVE(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
287 	cm->cm_flags &= ~XBDCF_Q_MASK;
288 	cm->cm_flags |= XBD_Q_NONE;
289 	xbd_removed_qentry(cm->cm_sc, index);
290 }
291 
292 static __inline void
293 xbd_initq_bio(struct xbd_softc *sc)
294 {
295 	bioq_init(&sc->xbd_bioq);
296 }
297 
298 static __inline void
299 xbd_enqueue_bio(struct xbd_softc *sc, struct bio *bp)
300 {
301 	bioq_insert_tail(&sc->xbd_bioq, bp);
302 	xbd_added_qentry(sc, XBD_Q_BIO);
303 }
304 
305 static __inline void
306 xbd_requeue_bio(struct xbd_softc *sc, struct bio *bp)
307 {
308 	bioq_insert_head(&sc->xbd_bioq, bp);
309 	xbd_added_qentry(sc, XBD_Q_BIO);
310 }
311 
312 static __inline struct bio *
313 xbd_dequeue_bio(struct xbd_softc *sc)
314 {
315 	struct bio *bp;
316 
317 	if ((bp = bioq_first(&sc->xbd_bioq)) != NULL) {
318 		bioq_remove(&sc->xbd_bioq, bp);
319 		xbd_removed_qentry(sc, XBD_Q_BIO);
320 	}
321 	return (bp);
322 }
323 
324 static inline void
325 xbd_initqs(struct xbd_softc *sc)
326 {
327 	u_int index;
328 
329 	for (index = 0; index < XBD_Q_COUNT; index++)
330 		xbd_initq_cm(sc, index);
331 
332 	xbd_initq_bio(sc);
333 }
334 
335 #endif /* __XEN_BLKFRONT_BLOCK_H__ */
336