xref: /freebsd/sys/dev/xen/blkfront/block.h (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 
31 #ifndef __XEN_BLKFRONT_BLOCK_H__
32 #define __XEN_BLKFRONT_BLOCK_H__
33 #include <xen/blkif.h>
34 
35 /**
36  * Given a number of blkif segments, compute the maximum I/O size supported.
37  *
38  * \note This calculation assumes that all but the first and last segments
39  *       of the I/O are fully utilized.
40  *
41  * \note We reserve a segment from the maximum supported by the transport to
42  *       guarantee we can handle an unaligned transfer without the need to
43  *       use a bounce buffer.
44  */
45 #define	XBD_SEGS_TO_SIZE(segs)						\
46 	(((segs) - 1) * PAGE_SIZE)
47 
48 /**
49  * Compute the maximum number of blkif segments requried to represent
50  * an I/O of the given size.
51  *
52  * \note This calculation assumes that all but the first and last segments
53  *       of the I/O are fully utilized.
54  *
55  * \note We reserve a segment to guarantee we can handle an unaligned
56  *       transfer without the need to use a bounce buffer.
57  */
58 #define	XBD_SIZE_TO_SEGS(size)						\
59 	((size / PAGE_SIZE) + 1)
60 
61 /**
62  * The maximum number of shared memory ring pages we will allow in a
63  * negotiated block-front/back communication channel.  Allow enough
64  * ring space for all requests to be  XBD_MAX_REQUEST_SIZE'd.
65  */
66 #define XBD_MAX_RING_PAGES		32
67 
68 /**
69  * The maximum number of outstanding requests we will allow in a negotiated
70  * block-front/back communication channel.
71  */
72 #define XBD_MAX_REQUESTS						\
73 	__CONST_RING_SIZE(blkif, PAGE_SIZE * XBD_MAX_RING_PAGES)
74 
75 /**
76  * The maximum number of blkif segments which can be provided per indirect
77  * page in an indirect request.
78  */
79 #define XBD_MAX_SEGMENTS_PER_PAGE					\
80 	(PAGE_SIZE / sizeof(struct blkif_request_segment))
81 
82 /**
83  * The maximum number of blkif segments which can be provided in an indirect
84  * request.
85  */
86 #define XBD_MAX_INDIRECT_SEGMENTS					\
87 	(BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST * XBD_MAX_SEGMENTS_PER_PAGE)
88 
89 /**
90  * Compute the number of indirect segment pages required for an I/O with the
91  * specified number of indirect segments.
92  */
93 #define XBD_INDIRECT_SEGS_TO_PAGES(segs)				\
94 	((segs + XBD_MAX_SEGMENTS_PER_PAGE - 1) / XBD_MAX_SEGMENTS_PER_PAGE)
95 
96 typedef enum {
97 	XBDCF_Q_MASK		= 0xFF,
98 	/* This command has contributed to xbd_qfrozen_cnt. */
99 	XBDCF_FROZEN		= 1<<8,
100 	/* Freeze the command queue on dispatch (i.e. single step command). */
101 	XBDCF_Q_FREEZE		= 1<<9,
102 	/* Bus DMA returned EINPROGRESS for this command. */
103 	XBDCF_ASYNC_MAPPING	= 1<<10,
104 	XBDCF_INITIALIZER	= XBDCF_Q_MASK
105 } xbdc_flag_t;
106 
107 struct xbd_command;
108 typedef void xbd_cbcf_t(struct xbd_command *);
109 
110 struct xbd_command {
111 	TAILQ_ENTRY(xbd_command) cm_link;
112 	struct xbd_softc	*cm_sc;
113 	xbdc_flag_t		 cm_flags;
114 	bus_dmamap_t		 cm_map;
115 	uint64_t		 cm_id;
116 	grant_ref_t		*cm_sg_refs;
117 	struct bio		*cm_bp;
118 	grant_ref_t		 cm_gref_head;
119 	void			*cm_data;
120 	size_t			 cm_datalen;
121 	u_int			 cm_nseg;
122 	int			 cm_operation;
123 	blkif_sector_t		 cm_sector_number;
124 	int			 cm_status;
125 	xbd_cbcf_t		*cm_complete;
126 	void			*cm_indirectionpages;
127 	grant_ref_t		 cm_indirectionrefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
128 };
129 
130 typedef enum {
131 	XBD_Q_FREE,
132 	XBD_Q_READY,
133 	XBD_Q_BUSY,
134 	XBD_Q_COMPLETE,
135 	XBD_Q_BIO,
136 	XBD_Q_COUNT,
137 	XBD_Q_NONE = XBDCF_Q_MASK
138 } xbd_q_index_t;
139 
140 typedef struct xbd_cm_q {
141 	TAILQ_HEAD(, xbd_command) q_tailq;
142 	uint32_t		  q_length;
143 	uint32_t		  q_max;
144 } xbd_cm_q_t;
145 
146 typedef enum {
147 	XBD_STATE_DISCONNECTED,
148 	XBD_STATE_CONNECTED,
149 	XBD_STATE_SUSPENDED
150 } xbd_state_t;
151 
152 typedef enum {
153 	XBDF_NONE	  = 0,
154 	XBDF_OPEN	  = 1 << 0, /* drive is open (can't shut down) */
155 	XBDF_BARRIER	  = 1 << 1, /* backend supports barriers */
156 	XBDF_FLUSH	  = 1 << 2, /* backend supports flush */
157 	XBDF_READY	  = 1 << 3, /* Is ready */
158 	XBDF_CM_SHORTAGE  = 1 << 4, /* Free cm resource shortage active. */
159 	XBDF_GNT_SHORTAGE = 1 << 5, /* Grant ref resource shortage active */
160 	XBDF_WAIT_IDLE	  = 1 << 6,  /*
161 				     * No new work until outstanding work
162 				     * completes.
163 				     */
164 	XBDF_DISCARD	  = 1 << 7, /* backend supports discard */
165 	XBDF_PERSISTENT	  = 1 << 8  /* backend supports persistent grants */
166 } xbd_flag_t;
167 
168 /*
169  * We have one of these per vbd, whether ide, scsi or 'other'.
170  */
171 struct xbd_softc {
172 	device_t			 xbd_dev;
173 	struct disk			*xbd_disk;	/* disk params */
174 	struct bio_queue_head 		 xbd_bioq;	/* sort queue */
175 	int				 xbd_unit;
176 	xbd_flag_t			 xbd_flags;
177 	int				 xbd_qfrozen_cnt;
178 	int				 xbd_vdevice;
179 	xbd_state_t			 xbd_state;
180 	u_int				 xbd_ring_pages;
181 	uint32_t			 xbd_max_requests;
182 	uint32_t			 xbd_max_request_segments;
183 	uint32_t			 xbd_max_request_size;
184 	uint32_t			 xbd_max_request_indirectpages;
185 	grant_ref_t			 xbd_ring_ref[XBD_MAX_RING_PAGES];
186 	blkif_front_ring_t		 xbd_ring;
187 	xen_intr_handle_t		 xen_intr_handle;
188 	struct gnttab_free_callback	 xbd_callback;
189 	xbd_cm_q_t			 xbd_cm_q[XBD_Q_COUNT];
190 	bus_dma_tag_t			 xbd_io_dmat;
191 
192 	/**
193 	 * The number of people holding this device open.  We won't allow a
194 	 * hot-unplug unless this is 0.
195 	 */
196 	int				 xbd_users;
197 	struct mtx			 xbd_io_lock;
198 
199 	struct xbd_command		*xbd_shadow;
200 };
201 
202 int xbd_instance_create(struct xbd_softc *, blkif_sector_t sectors, int device,
203 			uint16_t vdisk_info, unsigned long sector_size,
204 			unsigned long phys_sector_size);
205 
206 static inline void
207 xbd_added_qentry(struct xbd_softc *sc, xbd_q_index_t index)
208 {
209 	struct xbd_cm_q *cmq;
210 
211 	cmq = &sc->xbd_cm_q[index];
212 	cmq->q_length++;
213 	if (cmq->q_length > cmq->q_max)
214 		cmq->q_max = cmq->q_length;
215 }
216 
217 static inline void
218 xbd_removed_qentry(struct xbd_softc *sc, xbd_q_index_t index)
219 {
220 	sc->xbd_cm_q[index].q_length--;
221 }
222 
223 static inline uint32_t
224 xbd_queue_length(struct xbd_softc *sc, xbd_q_index_t index)
225 {
226 	return (sc->xbd_cm_q[index].q_length);
227 }
228 
229 static inline void
230 xbd_initq_cm(struct xbd_softc *sc, xbd_q_index_t index)
231 {
232 	struct xbd_cm_q *cmq;
233 
234 	cmq = &sc->xbd_cm_q[index];
235 	TAILQ_INIT(&cmq->q_tailq);
236 	cmq->q_length = 0;
237 	cmq->q_max = 0;
238 }
239 
240 static inline void
241 xbd_enqueue_cm(struct xbd_command *cm, xbd_q_index_t index)
242 {
243 	KASSERT(index != XBD_Q_BIO,
244 	    ("%s: Commands cannot access the bio queue.", __func__));
245 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
246 		panic("%s: command %p is already on queue %d.",
247 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
248 	TAILQ_INSERT_TAIL(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
249 	cm->cm_flags &= ~XBDCF_Q_MASK;
250 	cm->cm_flags |= index;
251 	xbd_added_qentry(cm->cm_sc, index);
252 }
253 
254 static inline void
255 xbd_requeue_cm(struct xbd_command *cm, xbd_q_index_t index)
256 {
257 	KASSERT(index != XBD_Q_BIO,
258 	    ("%s: Commands cannot access the bio queue.", __func__));
259 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
260 		panic("%s: command %p is already on queue %d.",
261 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
262 	TAILQ_INSERT_HEAD(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
263 	cm->cm_flags &= ~XBDCF_Q_MASK;
264 	cm->cm_flags |= index;
265 	xbd_added_qentry(cm->cm_sc, index);
266 }
267 
268 static inline struct xbd_command *
269 xbd_dequeue_cm(struct xbd_softc *sc, xbd_q_index_t index)
270 {
271 	struct xbd_command *cm;
272 
273 	KASSERT(index != XBD_Q_BIO,
274 	    ("%s: Commands cannot access the bio queue.", __func__));
275 
276 	if ((cm = TAILQ_FIRST(&sc->xbd_cm_q[index].q_tailq)) != NULL) {
277 		if ((cm->cm_flags & XBDCF_Q_MASK) != index) {
278 			panic("%s: command %p is on queue %d, "
279 			    "not specified queue %d",
280 			    __func__, cm,
281 			    cm->cm_flags & XBDCF_Q_MASK,
282 			    index);
283 		}
284 		TAILQ_REMOVE(&sc->xbd_cm_q[index].q_tailq, cm, cm_link);
285 		cm->cm_flags &= ~XBDCF_Q_MASK;
286 		cm->cm_flags |= XBD_Q_NONE;
287 		xbd_removed_qentry(cm->cm_sc, index);
288 	}
289 	return (cm);
290 }
291 
292 static inline void
293 xbd_remove_cm(struct xbd_command *cm, xbd_q_index_t expected_index)
294 {
295 	xbd_q_index_t index;
296 
297 	index = cm->cm_flags & XBDCF_Q_MASK;
298 
299 	KASSERT(index != XBD_Q_BIO,
300 	    ("%s: Commands cannot access the bio queue.", __func__));
301 
302 	if (index != expected_index) {
303 		panic("%s: command %p is on queue %d, not specified queue %d",
304 		    __func__, cm, index, expected_index);
305 	}
306 	TAILQ_REMOVE(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
307 	cm->cm_flags &= ~XBDCF_Q_MASK;
308 	cm->cm_flags |= XBD_Q_NONE;
309 	xbd_removed_qentry(cm->cm_sc, index);
310 }
311 
312 static inline void
313 xbd_initq_bio(struct xbd_softc *sc)
314 {
315 	bioq_init(&sc->xbd_bioq);
316 }
317 
318 static inline void
319 xbd_enqueue_bio(struct xbd_softc *sc, struct bio *bp)
320 {
321 	bioq_insert_tail(&sc->xbd_bioq, bp);
322 	xbd_added_qentry(sc, XBD_Q_BIO);
323 }
324 
325 static inline void
326 xbd_requeue_bio(struct xbd_softc *sc, struct bio *bp)
327 {
328 	bioq_insert_head(&sc->xbd_bioq, bp);
329 	xbd_added_qentry(sc, XBD_Q_BIO);
330 }
331 
332 static inline struct bio *
333 xbd_dequeue_bio(struct xbd_softc *sc)
334 {
335 	struct bio *bp;
336 
337 	if ((bp = bioq_first(&sc->xbd_bioq)) != NULL) {
338 		bioq_remove(&sc->xbd_bioq, bp);
339 		xbd_removed_qentry(sc, XBD_Q_BIO);
340 	}
341 	return (bp);
342 }
343 
344 static inline void
345 xbd_initqs(struct xbd_softc *sc)
346 {
347 	u_int index;
348 
349 	for (index = 0; index < XBD_Q_COUNT; index++)
350 		xbd_initq_cm(sc, index);
351 
352 	xbd_initq_bio(sc);
353 }
354 
355 #endif /* __XEN_BLKFRONT_BLOCK_H__ */
356