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