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 */
148 uint32_t max_requests;
149 uint32_t max_request_segments;
150 uint32_t max_request_blocks;
151 uint32_t max_request_size;
152 grant_ref_t ring_ref[XBF_MAX_RING_PAGES];
153 blkif_front_ring_t ring;
155 struct gnttab_free_callback callback;
156 TAILQ_HEAD(,xb_command) cm_free;
157 TAILQ_HEAD(,xb_command) cm_ready;
158 TAILQ_HEAD(,xb_command) cm_busy;
159 TAILQ_HEAD(,xb_command) cm_complete;
160 struct xb_qstat xb_qstat[XBQ_COUNT];
161 bus_dma_tag_t xb_io_dmat;
164 * The number of people holding this device open. We won't allow a
165 * hot-unplug unless this is 0.
168 struct mtx xb_io_lock;
170 struct xb_command *shadow;
173 int xlvbd_add(struct xb_softc *, blkif_sector_t sectors, int device,
174 uint16_t vdisk_info, unsigned long sector_size);
175 void xlvbd_del(struct xb_softc *);
177 #define XBQ_ADD(sc, qname) \
179 struct xb_qstat *qs; \
181 qs = &(sc)->xb_qstat[qname]; \
183 if (qs->q_length > qs->q_max) \
184 qs->q_max = qs->q_length; \
187 #define XBQ_REMOVE(sc, qname) (sc)->xb_qstat[qname].q_length--
189 #define XBQ_INIT(sc, qname) \
191 sc->xb_qstat[qname].q_length = 0; \
192 sc->xb_qstat[qname].q_max = 0; \
195 #define XBQ_COMMAND_QUEUE(name, index) \
196 static __inline void \
197 xb_initq_ ## name (struct xb_softc *sc) \
199 TAILQ_INIT(&sc->cm_ ## name); \
200 XBQ_INIT(sc, index); \
202 static __inline void \
203 xb_enqueue_ ## name (struct xb_command *cm) \
205 if ((cm->cm_flags & XB_ON_XBQ_MASK) != 0) { \
206 printf("command %p is on another queue, " \
207 "flags = %#x\n", cm, cm->cm_flags); \
208 panic("command is on another queue"); \
210 TAILQ_INSERT_TAIL(&cm->cm_sc->cm_ ## name, cm, cm_link); \
211 cm->cm_flags |= XB_ON_ ## index; \
212 XBQ_ADD(cm->cm_sc, index); \
214 static __inline void \
215 xb_requeue_ ## name (struct xb_command *cm) \
217 if ((cm->cm_flags & XB_ON_XBQ_MASK) != 0) { \
218 printf("command %p is on another queue, " \
219 "flags = %#x\n", cm, cm->cm_flags); \
220 panic("command is on another queue"); \
222 TAILQ_INSERT_HEAD(&cm->cm_sc->cm_ ## name, cm, cm_link); \
223 cm->cm_flags |= XB_ON_ ## index; \
224 XBQ_ADD(cm->cm_sc, index); \
226 static __inline struct xb_command * \
227 xb_dequeue_ ## name (struct xb_softc *sc) \
229 struct xb_command *cm; \
231 if ((cm = TAILQ_FIRST(&sc->cm_ ## name)) != NULL) { \
232 if ((cm->cm_flags & XB_ON_XBQ_MASK) != \
234 printf("command %p not in queue, " \
235 "flags = %#x, bit = %#x\n", cm, \
236 cm->cm_flags, XB_ON_ ## index); \
237 panic("command not in queue"); \
239 TAILQ_REMOVE(&sc->cm_ ## name, cm, cm_link); \
240 cm->cm_flags &= ~XB_ON_ ## index; \
241 XBQ_REMOVE(sc, index); \
245 static __inline void \
246 xb_remove_ ## name (struct xb_command *cm) \
248 if ((cm->cm_flags & XB_ON_XBQ_MASK) != XB_ON_ ## index){\
249 printf("command %p not in queue, flags = %#x, " \
250 "bit = %#x\n", cm, cm->cm_flags, \
252 panic("command not in queue"); \
254 TAILQ_REMOVE(&cm->cm_sc->cm_ ## name, cm, cm_link); \
255 cm->cm_flags &= ~XB_ON_ ## index; \
256 XBQ_REMOVE(cm->cm_sc, index); \
260 XBQ_COMMAND_QUEUE(free, XBQ_FREE);
261 XBQ_COMMAND_QUEUE(ready, XBQ_READY);
262 XBQ_COMMAND_QUEUE(busy, XBQ_BUSY);
263 XBQ_COMMAND_QUEUE(complete, XBQ_COMPLETE);
266 xb_initq_bio(struct xb_softc *sc)
268 bioq_init(&sc->xb_bioq);
269 XBQ_INIT(sc, XBQ_BIO);
273 xb_enqueue_bio(struct xb_softc *sc, struct bio *bp)
275 bioq_insert_tail(&sc->xb_bioq, bp);
276 XBQ_ADD(sc, XBQ_BIO);
280 xb_requeue_bio(struct xb_softc *sc, struct bio *bp)
282 bioq_insert_head(&sc->xb_bioq, bp);
283 XBQ_ADD(sc, XBQ_BIO);
286 static __inline struct bio *
287 xb_dequeue_bio(struct xb_softc *sc)
291 if ((bp = bioq_first(&sc->xb_bioq)) != NULL) {
292 bioq_remove(&sc->xb_bioq, bp);
293 XBQ_REMOVE(sc, XBQ_BIO);
298 #endif /* __XEN_DRIVERS_BLOCK_H__ */