2 * XenBSD block device driver
4 * Copyright (c) 2009 Scott Long, Yahoo!
5 * Copyright (c) 2009 Frank Suchomel, Citrix
6 * Copyright (c) 2009 Doug F. Rabson, Citrix
7 * Copyright (c) 2005 Kip Macy
8 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
9 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to
14 * deal in the Software without restriction, including without limitation the
15 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
33 #ifndef __XEN_DRIVERS_BLOCK_H__
34 #define __XEN_DRIVERS_BLOCK_H__
35 #include <xen/blkif.h>
38 * The maximum number of outstanding requests blocks (request headers plus
39 * additional segment blocks) we will allow in a negotiated block-front/back
40 * communication channel.
42 #define XBF_MAX_REQUESTS 256
45 * The maximum mapped region size per request we will allow in a negotiated
46 * block-front/back communication channel.
48 * \note We reserve a segement from the maximum supported by the transport to
49 * guarantee we can handle an unaligned transfer without the need to
50 * use a bounce buffer..
52 #define XBF_MAX_REQUEST_SIZE \
53 MIN(MAXPHYS, (BLKIF_MAX_SEGMENTS_PER_REQUEST - 1) * PAGE_SIZE)
56 * The maximum number of segments (within a request header and accompanying
57 * segment blocks) per request we will allow in a negotiated block-front/back
58 * communication channel.
60 #define XBF_MAX_SEGMENTS_PER_REQUEST \
61 (MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST, \
62 (XBF_MAX_REQUEST_SIZE / PAGE_SIZE) + 1))
65 * The maximum number of shared memory ring pages we will allow in a
66 * negotiated block-front/back communication channel. Allow enough
67 * ring space for all requests to be XBF_MAX_REQUEST_SIZE'd.
69 #define XBF_MAX_RING_PAGES \
70 BLKIF_RING_PAGES(BLKIF_SEGS_TO_BLOCKS(XBF_MAX_SEGMENTS_PER_REQUEST) \
81 struct xlbd_major_info
86 struct xlbd_type_info *type;
90 TAILQ_ENTRY(xb_command) cm_link;
91 struct xb_softc *cm_sc;
93 #define XB_CMD_FROZEN (1<<0)
94 #define XB_CMD_POLLED (1<<1)
95 #define XB_ON_XBQ_FREE (1<<2)
96 #define XB_ON_XBQ_READY (1<<3)
97 #define XB_ON_XBQ_BUSY (1<<4)
98 #define XB_ON_XBQ_COMPLETE (1<<5)
99 #define XB_ON_XBQ_MASK ((1<<2)|(1<<3)|(1<<4)|(1<<5))
102 grant_ref_t *sg_refs;
104 grant_ref_t gref_head;
109 blkif_sector_t sector_number;
111 void (* cm_complete)(struct xb_command *);
118 #define XBQ_COMPLETE 4
126 union xb_statrequest {
128 struct xb_qstat ms_qstat;
132 * We have one of these per vbd, whether ide, scsi or 'other'.
136 struct disk *xb_disk; /* disk params */
137 struct bio_queue_head xb_bioq; /* sort queue */
140 #define XB_OPEN (1<<0) /* drive is open (can't shut down) */
141 #define XB_BARRIER (1 << 1) /* backend supports barriers */
142 #define XB_READY (1 << 2) /* Is ready */
143 #define XB_FROZEN (1 << 3) /* Waiting for resources */
147 uint32_t max_requests;
148 uint32_t max_request_segments;
149 uint32_t max_request_blocks;
150 uint32_t max_request_size;
151 grant_ref_t ring_ref[XBF_MAX_RING_PAGES];
152 blkif_front_ring_t ring;
154 struct gnttab_free_callback callback;
155 TAILQ_HEAD(,xb_command) cm_free;
156 TAILQ_HEAD(,xb_command) cm_ready;
157 TAILQ_HEAD(,xb_command) cm_busy;
158 TAILQ_HEAD(,xb_command) cm_complete;
159 struct xb_qstat xb_qstat[XBQ_COUNT];
160 bus_dma_tag_t xb_io_dmat;
163 * The number of people holding this device open. We won't allow a
164 * hot-unplug unless this is 0.
167 struct mtx xb_io_lock;
169 struct xb_command *shadow;
172 int xlvbd_add(struct xb_softc *, blkif_sector_t sectors, int device,
173 uint16_t vdisk_info, unsigned long sector_size);
174 void xlvbd_del(struct xb_softc *);
176 #define XBQ_ADD(sc, qname) \
178 struct xb_qstat *qs; \
180 qs = &(sc)->xb_qstat[qname]; \
182 if (qs->q_length > qs->q_max) \
183 qs->q_max = qs->q_length; \
186 #define XBQ_REMOVE(sc, qname) (sc)->xb_qstat[qname].q_length--
188 #define XBQ_INIT(sc, qname) \
190 sc->xb_qstat[qname].q_length = 0; \
191 sc->xb_qstat[qname].q_max = 0; \
194 #define XBQ_COMMAND_QUEUE(name, index) \
195 static __inline void \
196 xb_initq_ ## name (struct xb_softc *sc) \
198 TAILQ_INIT(&sc->cm_ ## name); \
199 XBQ_INIT(sc, index); \
201 static __inline void \
202 xb_enqueue_ ## name (struct xb_command *cm) \
204 if ((cm->cm_flags & XB_ON_XBQ_MASK) != 0) { \
205 printf("command %p is on another queue, " \
206 "flags = %#x\n", cm, cm->cm_flags); \
207 panic("command is on another queue"); \
209 TAILQ_INSERT_TAIL(&cm->cm_sc->cm_ ## name, cm, cm_link); \
210 cm->cm_flags |= XB_ON_ ## index; \
211 XBQ_ADD(cm->cm_sc, index); \
213 static __inline void \
214 xb_requeue_ ## name (struct xb_command *cm) \
216 if ((cm->cm_flags & XB_ON_XBQ_MASK) != 0) { \
217 printf("command %p is on another queue, " \
218 "flags = %#x\n", cm, cm->cm_flags); \
219 panic("command is on another queue"); \
221 TAILQ_INSERT_HEAD(&cm->cm_sc->cm_ ## name, cm, cm_link); \
222 cm->cm_flags |= XB_ON_ ## index; \
223 XBQ_ADD(cm->cm_sc, index); \
225 static __inline struct xb_command * \
226 xb_dequeue_ ## name (struct xb_softc *sc) \
228 struct xb_command *cm; \
230 if ((cm = TAILQ_FIRST(&sc->cm_ ## name)) != NULL) { \
231 if ((cm->cm_flags & XB_ON_XBQ_MASK) != \
233 printf("command %p not in queue, " \
234 "flags = %#x, bit = %#x\n", cm, \
235 cm->cm_flags, XB_ON_ ## index); \
236 panic("command not in queue"); \
238 TAILQ_REMOVE(&sc->cm_ ## name, cm, cm_link); \
239 cm->cm_flags &= ~XB_ON_ ## index; \
240 XBQ_REMOVE(sc, index); \
244 static __inline void \
245 xb_remove_ ## name (struct xb_command *cm) \
247 if ((cm->cm_flags & XB_ON_XBQ_MASK) != XB_ON_ ## index){\
248 printf("command %p not in queue, flags = %#x, " \
249 "bit = %#x\n", cm, cm->cm_flags, \
251 panic("command not in queue"); \
253 TAILQ_REMOVE(&cm->cm_sc->cm_ ## name, cm, cm_link); \
254 cm->cm_flags &= ~XB_ON_ ## index; \
255 XBQ_REMOVE(cm->cm_sc, index); \
259 XBQ_COMMAND_QUEUE(free, XBQ_FREE);
260 XBQ_COMMAND_QUEUE(ready, XBQ_READY);
261 XBQ_COMMAND_QUEUE(busy, XBQ_BUSY);
262 XBQ_COMMAND_QUEUE(complete, XBQ_COMPLETE);
265 xb_initq_bio(struct xb_softc *sc)
267 bioq_init(&sc->xb_bioq);
268 XBQ_INIT(sc, XBQ_BIO);
272 xb_enqueue_bio(struct xb_softc *sc, struct bio *bp)
274 bioq_insert_tail(&sc->xb_bioq, bp);
275 XBQ_ADD(sc, XBQ_BIO);
279 xb_requeue_bio(struct xb_softc *sc, struct bio *bp)
281 bioq_insert_head(&sc->xb_bioq, bp);
282 XBQ_ADD(sc, XBQ_BIO);
285 static __inline struct bio *
286 xb_dequeue_bio(struct xb_softc *sc)
290 if ((bp = bioq_first(&sc->xb_bioq)) != NULL) {
291 bioq_remove(&sc->xb_bioq, bp);
292 XBQ_REMOVE(sc, XBQ_BIO);
297 #endif /* __XEN_DRIVERS_BLOCK_H__ */