189e0f4d2SKip Macy /* 28698b76cSKip Macy * XenBSD block device driver 38698b76cSKip Macy * 433eebb6aSJustin T. Gibbs * Copyright (c) 2010-2013 Spectra Logic Corporation 5e4808c4bSKip Macy * Copyright (c) 2009 Scott Long, Yahoo! 68698b76cSKip Macy * Copyright (c) 2009 Frank Suchomel, Citrix 79999d2cbSKip Macy * Copyright (c) 2009 Doug F. Rabson, Citrix 89999d2cbSKip Macy * Copyright (c) 2005 Kip Macy 99999d2cbSKip Macy * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 109999d2cbSKip Macy * Modifications by Mark A. Williamson are (c) Intel Research Cambridge 119999d2cbSKip Macy * 129999d2cbSKip Macy * 139999d2cbSKip Macy * Permission is hereby granted, free of charge, to any person obtaining a copy 149999d2cbSKip Macy * of this software and associated documentation files (the "Software"), to 159999d2cbSKip Macy * deal in the Software without restriction, including without limitation the 169999d2cbSKip Macy * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 179999d2cbSKip Macy * sell copies of the Software, and to permit persons to whom the Software is 189999d2cbSKip Macy * furnished to do so, subject to the following conditions: 199999d2cbSKip Macy * 209999d2cbSKip Macy * The above copyright notice and this permission notice shall be included in 219999d2cbSKip Macy * all copies or substantial portions of the Software. 229999d2cbSKip Macy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 239999d2cbSKip Macy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 249999d2cbSKip Macy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 259999d2cbSKip Macy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 269999d2cbSKip Macy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 279999d2cbSKip Macy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 289999d2cbSKip Macy * DEALINGS IN THE SOFTWARE. 2989e0f4d2SKip Macy */ 3089e0f4d2SKip Macy 3189e0f4d2SKip Macy #include <sys/cdefs.h> 3289e0f4d2SKip Macy __FBSDID("$FreeBSD$"); 3389e0f4d2SKip Macy 3489e0f4d2SKip Macy #include <sys/param.h> 3589e0f4d2SKip Macy #include <sys/systm.h> 3689e0f4d2SKip Macy #include <sys/malloc.h> 3789e0f4d2SKip Macy #include <sys/kernel.h> 3889e0f4d2SKip Macy #include <vm/vm.h> 3989e0f4d2SKip Macy #include <vm/pmap.h> 4089e0f4d2SKip Macy 4189e0f4d2SKip Macy #include <sys/bio.h> 4289e0f4d2SKip Macy #include <sys/bus.h> 4389e0f4d2SKip Macy #include <sys/conf.h> 4489e0f4d2SKip Macy #include <sys/module.h> 458b8bfa35SJustin T. Gibbs #include <sys/sysctl.h> 4689e0f4d2SKip Macy 4789e0f4d2SKip Macy #include <machine/bus.h> 4889e0f4d2SKip Macy #include <sys/rman.h> 4989e0f4d2SKip Macy #include <machine/resource.h> 5089e0f4d2SKip Macy #include <machine/intr_machdep.h> 5189e0f4d2SKip Macy #include <machine/vmparam.h> 52e4808c4bSKip Macy #include <sys/bus_dma.h> 5389e0f4d2SKip Macy 5476acc41fSJustin T. Gibbs #include <xen/xen-os.h> 5512678024SDoug Rabson #include <xen/hypervisor.h> 563a6d1fcfSKip Macy #include <xen/xen_intr.h> 5712678024SDoug Rabson #include <xen/gnttab.h> 5889e0f4d2SKip Macy #include <xen/interface/grant_table.h> 5923dc5621SKip Macy #include <xen/interface/io/protocols.h> 6023dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 6189e0f4d2SKip Macy 6276acc41fSJustin T. Gibbs #include <machine/_inttypes.h> 6376acc41fSJustin T. Gibbs 6489e0f4d2SKip Macy #include <geom/geom_disk.h> 6589e0f4d2SKip Macy 6689e0f4d2SKip Macy #include <dev/xen/blkfront/block.h> 6789e0f4d2SKip Macy 6823dc5621SKip Macy #include "xenbus_if.h" 6923dc5621SKip Macy 70fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/ 7133eebb6aSJustin T. Gibbs static void xbd_closing(device_t); 72fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc); 7389e0f4d2SKip Macy 74fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/ 75fac3fd80SJustin T. Gibbs #if 0 76fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 77fac3fd80SJustin T. Gibbs #else 78fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) 79fac3fd80SJustin T. Gibbs #endif 80fac3fd80SJustin T. Gibbs 81fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT 9 82ff662b5cSJustin T. Gibbs 83fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/ 84fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 8589e0f4d2SKip Macy 86aaebf690SColin Percival static int xbd_enable_indirect = 1; 87aaebf690SColin Percival SYSCTL_NODE(_hw, OID_AUTO, xbd, CTLFLAG_RD, 0, "xbd driver parameters"); 88aaebf690SColin Percival SYSCTL_INT(_hw_xbd, OID_AUTO, xbd_enable_indirect, CTLFLAG_RDTUN, 89aaebf690SColin Percival &xbd_enable_indirect, 0, "Enable xbd indirect segments"); 90aaebf690SColin Percival 91fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/ 92127a9483SJustin T. Gibbs static void 93127a9483SJustin T. Gibbs xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag) 94127a9483SJustin T. Gibbs { 95127a9483SJustin T. Gibbs if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0) 96127a9483SJustin T. Gibbs return; 97127a9483SJustin T. Gibbs 98127a9483SJustin T. Gibbs sc->xbd_flags |= xbd_flag; 99127a9483SJustin T. Gibbs sc->xbd_qfrozen_cnt++; 100127a9483SJustin T. Gibbs } 101127a9483SJustin T. Gibbs 102127a9483SJustin T. Gibbs static void 103127a9483SJustin T. Gibbs xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag) 104127a9483SJustin T. Gibbs { 105127a9483SJustin T. Gibbs if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0) 106127a9483SJustin T. Gibbs return; 107127a9483SJustin T. Gibbs 108b834eea6SJustin T. Gibbs if (sc->xbd_qfrozen_cnt == 0) 109127a9483SJustin T. Gibbs panic("%s: Thaw with flag 0x%x while not frozen.", 110127a9483SJustin T. Gibbs __func__, xbd_flag); 111127a9483SJustin T. Gibbs 112127a9483SJustin T. Gibbs sc->xbd_flags &= ~xbd_flag; 113127a9483SJustin T. Gibbs sc->xbd_qfrozen_cnt--; 114127a9483SJustin T. Gibbs } 115127a9483SJustin T. Gibbs 1169985113bSJustin T. Gibbs static void 1179985113bSJustin T. Gibbs xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag) 1189985113bSJustin T. Gibbs { 1199985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_FROZEN) != 0) 1209985113bSJustin T. Gibbs return; 1219985113bSJustin T. Gibbs 1229985113bSJustin T. Gibbs cm->cm_flags |= XBDCF_FROZEN|cm_flag; 1239985113bSJustin T. Gibbs xbd_freeze(sc, XBDF_NONE); 1249985113bSJustin T. Gibbs } 1259985113bSJustin T. Gibbs 1269985113bSJustin T. Gibbs static void 1279985113bSJustin T. Gibbs xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm) 1289985113bSJustin T. Gibbs { 1299985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_FROZEN) == 0) 1309985113bSJustin T. Gibbs return; 1319985113bSJustin T. Gibbs 1329985113bSJustin T. Gibbs cm->cm_flags &= ~XBDCF_FROZEN; 1339985113bSJustin T. Gibbs xbd_thaw(sc, XBDF_NONE); 1349985113bSJustin T. Gibbs } 1359985113bSJustin T. Gibbs 136fac3fd80SJustin T. Gibbs static inline void 137cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc) 138fac3fd80SJustin T. Gibbs { 139fac3fd80SJustin T. Gibbs int notify; 140e4808c4bSKip Macy 141fac3fd80SJustin T. Gibbs RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify); 14289e0f4d2SKip Macy 143fac3fd80SJustin T. Gibbs if (notify) 14476acc41fSJustin T. Gibbs xen_intr_signal(sc->xen_intr_handle); 145fac3fd80SJustin T. Gibbs } 14689e0f4d2SKip Macy 14723dc5621SKip Macy static void 148fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm) 14923dc5621SKip Macy { 15023dc5621SKip Macy 151e2c1fe90SJustin T. Gibbs KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE, 152e2c1fe90SJustin T. Gibbs ("Freeing command that is still on queue %d.", 153e2c1fe90SJustin T. Gibbs cm->cm_flags & XBDCF_Q_MASK)); 15423dc5621SKip Macy 155e2c1fe90SJustin T. Gibbs cm->cm_flags = XBDCF_INITIALIZER; 156fac3fd80SJustin T. Gibbs cm->cm_bp = NULL; 157fac3fd80SJustin T. Gibbs cm->cm_complete = NULL; 158e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_FREE); 159127a9483SJustin T. Gibbs xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE); 16023dc5621SKip Macy } 16123dc5621SKip Macy 16289e0f4d2SKip Macy static void 1637a70e152SColin Percival xbd_mksegarray(bus_dma_segment_t *segs, int nsegs, 164d0ecc14dSColin Percival grant_ref_t * gref_head, int otherend_id, int readonly, 165d0ecc14dSColin Percival grant_ref_t * sg_ref, blkif_request_segment_t * sg) 166d0ecc14dSColin Percival { 167d0ecc14dSColin Percival struct blkif_request_segment *last_block_sg = sg + nsegs; 168d0ecc14dSColin Percival vm_paddr_t buffer_ma; 169d0ecc14dSColin Percival uint64_t fsect, lsect; 170d0ecc14dSColin Percival int ref; 171d0ecc14dSColin Percival 172d0ecc14dSColin Percival while (sg < last_block_sg) { 173d0ecc14dSColin Percival buffer_ma = segs->ds_addr; 174d0ecc14dSColin Percival fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 175d0ecc14dSColin Percival lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 176d0ecc14dSColin Percival 177d0ecc14dSColin Percival KASSERT(lsect <= 7, ("XEN disk driver data cannot " 178d0ecc14dSColin Percival "cross a page boundary")); 179d0ecc14dSColin Percival 180d0ecc14dSColin Percival /* install a grant reference. */ 181d0ecc14dSColin Percival ref = gnttab_claim_grant_reference(gref_head); 182d0ecc14dSColin Percival 183d0ecc14dSColin Percival /* 184d0ecc14dSColin Percival * GNTTAB_LIST_END == 0xffffffff, but it is private 185d0ecc14dSColin Percival * to gnttab.c. 186d0ecc14dSColin Percival */ 187d0ecc14dSColin Percival KASSERT(ref != ~0, ("grant_reference failed")); 188d0ecc14dSColin Percival 189d0ecc14dSColin Percival gnttab_grant_foreign_access_ref( 190d0ecc14dSColin Percival ref, 191d0ecc14dSColin Percival otherend_id, 192d0ecc14dSColin Percival buffer_ma >> PAGE_SHIFT, 193d0ecc14dSColin Percival readonly); 194d0ecc14dSColin Percival 195d0ecc14dSColin Percival *sg_ref = ref; 196d0ecc14dSColin Percival *sg = (struct blkif_request_segment) { 197d0ecc14dSColin Percival .gref = ref, 198d0ecc14dSColin Percival .first_sect = fsect, 199d0ecc14dSColin Percival .last_sect = lsect 200d0ecc14dSColin Percival }; 201d0ecc14dSColin Percival sg++; 202d0ecc14dSColin Percival sg_ref++; 203d0ecc14dSColin Percival segs++; 204d0ecc14dSColin Percival } 205d0ecc14dSColin Percival } 206d0ecc14dSColin Percival 207d0ecc14dSColin Percival static void 208fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 20989e0f4d2SKip Macy { 210fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 211fac3fd80SJustin T. Gibbs struct xbd_command *cm; 212fac3fd80SJustin T. Gibbs int op; 21389e0f4d2SKip Macy 214fac3fd80SJustin T. Gibbs cm = arg; 215fac3fd80SJustin T. Gibbs sc = cm->cm_sc; 216fac3fd80SJustin T. Gibbs 217fac3fd80SJustin T. Gibbs if (error) { 218fac3fd80SJustin T. Gibbs cm->cm_bp->bio_error = EIO; 219fac3fd80SJustin T. Gibbs biodone(cm->cm_bp); 220fac3fd80SJustin T. Gibbs xbd_free_command(cm); 22189e0f4d2SKip Macy return; 22289e0f4d2SKip Macy } 22389e0f4d2SKip Macy 224aaebf690SColin Percival KASSERT(nsegs <= sc->xbd_max_request_segments, 225d33a1217SColin Percival ("Too many segments in a blkfront I/O")); 226d33a1217SColin Percival 227aaebf690SColin Percival if (nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST) { 228aaebf690SColin Percival blkif_request_t *ring_req; 229aaebf690SColin Percival 230aaebf690SColin Percival /* Fill out a blkif_request_t structure. */ 231aaebf690SColin Percival ring_req = (blkif_request_t *) 232aaebf690SColin Percival RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 233fac3fd80SJustin T. Gibbs sc->xbd_ring.req_prod_pvt++; 234fac3fd80SJustin T. Gibbs ring_req->id = cm->cm_id; 235fac3fd80SJustin T. Gibbs ring_req->operation = cm->cm_operation; 236fac3fd80SJustin T. Gibbs ring_req->sector_number = cm->cm_sector_number; 237fac3fd80SJustin T. Gibbs ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 238fac3fd80SJustin T. Gibbs ring_req->nr_segments = nsegs; 239fac3fd80SJustin T. Gibbs cm->cm_nseg = nsegs; 2407a70e152SColin Percival xbd_mksegarray(segs, nsegs, &cm->cm_gref_head, 241fac3fd80SJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), 242d0ecc14dSColin Percival cm->cm_operation == BLKIF_OP_WRITE, 243d0ecc14dSColin Percival cm->cm_sg_refs, ring_req->seg); 244aaebf690SColin Percival } else { 245aaebf690SColin Percival blkif_request_indirect_t *ring_req; 246aaebf690SColin Percival 247aaebf690SColin Percival /* Fill out a blkif_request_indirect_t structure. */ 248aaebf690SColin Percival ring_req = (blkif_request_indirect_t *) 249aaebf690SColin Percival RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 250aaebf690SColin Percival sc->xbd_ring.req_prod_pvt++; 251aaebf690SColin Percival ring_req->id = cm->cm_id; 252aaebf690SColin Percival ring_req->operation = BLKIF_OP_INDIRECT; 253aaebf690SColin Percival ring_req->indirect_op = cm->cm_operation; 254aaebf690SColin Percival ring_req->sector_number = cm->cm_sector_number; 255aaebf690SColin Percival ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 256aaebf690SColin Percival ring_req->nr_segments = nsegs; 257aaebf690SColin Percival cm->cm_nseg = nsegs; 258aaebf690SColin Percival xbd_mksegarray(segs, nsegs, &cm->cm_gref_head, 259aaebf690SColin Percival xenbus_get_otherend_id(sc->xbd_dev), 260aaebf690SColin Percival cm->cm_operation == BLKIF_OP_WRITE, 261aaebf690SColin Percival cm->cm_sg_refs, cm->cm_indirectionpages); 262aaebf690SColin Percival memcpy(ring_req->indirect_grefs, &cm->cm_indirectionrefs, 263aaebf690SColin Percival sizeof(grant_ref_t) * sc->xbd_max_request_indirectpages); 264aaebf690SColin Percival } 265fac3fd80SJustin T. Gibbs 266fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 267fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREREAD; 268fac3fd80SJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE) 269fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREWRITE; 270fac3fd80SJustin T. Gibbs else 271fac3fd80SJustin T. Gibbs op = 0; 272fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 273fac3fd80SJustin T. Gibbs 274fac3fd80SJustin T. Gibbs gnttab_free_grant_references(cm->cm_gref_head); 275fac3fd80SJustin T. Gibbs 276e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_BUSY); 277fac3fd80SJustin T. Gibbs 278fac3fd80SJustin T. Gibbs /* 279127a9483SJustin T. Gibbs * If bus dma had to asynchronously call us back to dispatch 280127a9483SJustin T. Gibbs * this command, we are no longer executing in the context of 281127a9483SJustin T. Gibbs * xbd_startio(). Thus we cannot rely on xbd_startio()'s call to 282127a9483SJustin T. Gibbs * xbd_flush_requests() to publish this command to the backend 283127a9483SJustin T. Gibbs * along with any other commands that it could batch. 284fac3fd80SJustin T. Gibbs */ 285127a9483SJustin T. Gibbs if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0) 286cdf5d66fSJustin T. Gibbs xbd_flush_requests(sc); 287fac3fd80SJustin T. Gibbs 288fac3fd80SJustin T. Gibbs return; 289fac3fd80SJustin T. Gibbs } 290fac3fd80SJustin T. Gibbs 291fac3fd80SJustin T. Gibbs static int 292fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm) 293fac3fd80SJustin T. Gibbs { 294fac3fd80SJustin T. Gibbs int error; 295fac3fd80SJustin T. Gibbs 29677b6916dSRoger Pau Monné error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data, 29777b6916dSRoger Pau Monné cm->cm_datalen, xbd_queue_cb, cm, 0); 298fac3fd80SJustin T. Gibbs if (error == EINPROGRESS) { 299127a9483SJustin T. Gibbs /* 300127a9483SJustin T. Gibbs * Maintain queuing order by freezing the queue. The next 301127a9483SJustin T. Gibbs * command may not require as many resources as the command 302127a9483SJustin T. Gibbs * we just attempted to map, so we can't rely on bus dma 303127a9483SJustin T. Gibbs * blocking for it too. 304127a9483SJustin T. Gibbs */ 3059985113bSJustin T. Gibbs xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING); 306fac3fd80SJustin T. Gibbs return (0); 307fac3fd80SJustin T. Gibbs } 308fac3fd80SJustin T. Gibbs 309fac3fd80SJustin T. Gibbs return (error); 310fac3fd80SJustin T. Gibbs } 311fac3fd80SJustin T. Gibbs 312fac3fd80SJustin T. Gibbs static void 313fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg) 314fac3fd80SJustin T. Gibbs { 315fac3fd80SJustin T. Gibbs struct xbd_softc *sc = arg; 316fac3fd80SJustin T. Gibbs 31733eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 318e4808c4bSKip Macy 319127a9483SJustin T. Gibbs xbd_thaw(sc, XBDF_GNT_SHORTAGE); 320127a9483SJustin T. Gibbs 32133eebb6aSJustin T. Gibbs xbd_startio(sc); 322e4808c4bSKip Macy 32333eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 324fac3fd80SJustin T. Gibbs } 325fac3fd80SJustin T. Gibbs 326fac3fd80SJustin T. Gibbs static struct xbd_command * 327fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc) 328fac3fd80SJustin T. Gibbs { 329fac3fd80SJustin T. Gibbs struct xbd_command *cm; 330fac3fd80SJustin T. Gibbs struct bio *bp; 331fac3fd80SJustin T. Gibbs 33276acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED)) 333fac3fd80SJustin T. Gibbs return (NULL); 334fac3fd80SJustin T. Gibbs 335fac3fd80SJustin T. Gibbs bp = xbd_dequeue_bio(sc); 336fac3fd80SJustin T. Gibbs if (bp == NULL) 337fac3fd80SJustin T. Gibbs return (NULL); 338fac3fd80SJustin T. Gibbs 339e2c1fe90SJustin T. Gibbs if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) { 340127a9483SJustin T. Gibbs xbd_freeze(sc, XBDF_CM_SHORTAGE); 341fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 342fac3fd80SJustin T. Gibbs return (NULL); 343fac3fd80SJustin T. Gibbs } 344fac3fd80SJustin T. Gibbs 345fac3fd80SJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 346fac3fd80SJustin T. Gibbs &cm->cm_gref_head) != 0) { 347fac3fd80SJustin T. Gibbs gnttab_request_free_callback(&sc->xbd_callback, 348fac3fd80SJustin T. Gibbs xbd_restart_queue_callback, sc, 349fac3fd80SJustin T. Gibbs sc->xbd_max_request_segments); 350127a9483SJustin T. Gibbs xbd_freeze(sc, XBDF_GNT_SHORTAGE); 351fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 352e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_FREE); 353fac3fd80SJustin T. Gibbs return (NULL); 354fac3fd80SJustin T. Gibbs } 355fac3fd80SJustin T. Gibbs 356fac3fd80SJustin T. Gibbs cm->cm_bp = bp; 35777b6916dSRoger Pau Monné cm->cm_data = bp->bio_data; 35877b6916dSRoger Pau Monné cm->cm_datalen = bp->bio_bcount; 359fac3fd80SJustin T. Gibbs cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno; 360fac3fd80SJustin T. Gibbs 3619985113bSJustin T. Gibbs switch (bp->bio_cmd) { 3629985113bSJustin T. Gibbs case BIO_READ: 3639985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_READ; 3649985113bSJustin T. Gibbs break; 3659985113bSJustin T. Gibbs case BIO_WRITE: 3669985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE; 3679985113bSJustin T. Gibbs if ((bp->bio_flags & BIO_ORDERED) != 0) { 3689985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 3699985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 3709985113bSJustin T. Gibbs } else { 3719985113bSJustin T. Gibbs /* 3729985113bSJustin T. Gibbs * Single step this command. 3739985113bSJustin T. Gibbs */ 3749985113bSJustin T. Gibbs cm->cm_flags |= XBDCF_Q_FREEZE; 3759985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 3769985113bSJustin T. Gibbs /* 3779985113bSJustin T. Gibbs * Wait for in-flight requests to 3789985113bSJustin T. Gibbs * finish. 3799985113bSJustin T. Gibbs */ 3809985113bSJustin T. Gibbs xbd_freeze(sc, XBDF_WAIT_IDLE); 3819985113bSJustin T. Gibbs xbd_requeue_cm(cm, XBD_Q_READY); 3829985113bSJustin T. Gibbs return (NULL); 3839985113bSJustin T. Gibbs } 3849985113bSJustin T. Gibbs } 3859985113bSJustin T. Gibbs } 3869985113bSJustin T. Gibbs break; 3879985113bSJustin T. Gibbs case BIO_FLUSH: 3889985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_FLUSH) != 0) 3899985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE; 3909985113bSJustin T. Gibbs else if ((sc->xbd_flags & XBDF_BARRIER) != 0) 3919985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 3929985113bSJustin T. Gibbs else 3939985113bSJustin T. Gibbs panic("flush request, but no flush support available"); 3949985113bSJustin T. Gibbs break; 3959985113bSJustin T. Gibbs default: 3969985113bSJustin T. Gibbs panic("unknown bio command %d", bp->bio_cmd); 3979985113bSJustin T. Gibbs } 3989985113bSJustin T. Gibbs 399fac3fd80SJustin T. Gibbs return (cm); 400fac3fd80SJustin T. Gibbs } 401fac3fd80SJustin T. Gibbs 402fac3fd80SJustin T. Gibbs /* 403fac3fd80SJustin T. Gibbs * Dequeue buffers and place them in the shared communication ring. 404fac3fd80SJustin T. Gibbs * Return when no more requests can be accepted or all buffers have 405fac3fd80SJustin T. Gibbs * been queued. 406fac3fd80SJustin T. Gibbs * 407fac3fd80SJustin T. Gibbs * Signal XEN once the ring has been filled out. 408fac3fd80SJustin T. Gibbs */ 409fac3fd80SJustin T. Gibbs static void 410fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc) 411fac3fd80SJustin T. Gibbs { 412fac3fd80SJustin T. Gibbs struct xbd_command *cm; 413fac3fd80SJustin T. Gibbs int error, queued = 0; 414fac3fd80SJustin T. Gibbs 415fac3fd80SJustin T. Gibbs mtx_assert(&sc->xbd_io_lock, MA_OWNED); 416fac3fd80SJustin T. Gibbs 417e2c1fe90SJustin T. Gibbs if (sc->xbd_state != XBD_STATE_CONNECTED) 418e4808c4bSKip Macy return; 419fac3fd80SJustin T. Gibbs 420112cacaeSRoger Pau Monné while (!RING_FULL(&sc->xbd_ring)) { 421112cacaeSRoger Pau Monné 422127a9483SJustin T. Gibbs if (sc->xbd_qfrozen_cnt != 0) 423fac3fd80SJustin T. Gibbs break; 424fac3fd80SJustin T. Gibbs 425e2c1fe90SJustin T. Gibbs cm = xbd_dequeue_cm(sc, XBD_Q_READY); 426fac3fd80SJustin T. Gibbs 427fac3fd80SJustin T. Gibbs if (cm == NULL) 428fac3fd80SJustin T. Gibbs cm = xbd_bio_command(sc); 429fac3fd80SJustin T. Gibbs 430fac3fd80SJustin T. Gibbs if (cm == NULL) 431fac3fd80SJustin T. Gibbs break; 432fac3fd80SJustin T. Gibbs 4339985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) { 4349985113bSJustin T. Gibbs /* 4359985113bSJustin T. Gibbs * Single step command. Future work is 4369985113bSJustin T. Gibbs * held off until this command completes. 4379985113bSJustin T. Gibbs */ 4389985113bSJustin T. Gibbs xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE); 4399985113bSJustin T. Gibbs } 4409985113bSJustin T. Gibbs 441fac3fd80SJustin T. Gibbs if ((error = xbd_queue_request(sc, cm)) != 0) { 442fac3fd80SJustin T. Gibbs printf("xbd_queue_request returned %d\n", error); 443fac3fd80SJustin T. Gibbs break; 444fac3fd80SJustin T. Gibbs } 445fac3fd80SJustin T. Gibbs queued++; 446fac3fd80SJustin T. Gibbs } 447fac3fd80SJustin T. Gibbs 448fac3fd80SJustin T. Gibbs if (queued != 0) 449cdf5d66fSJustin T. Gibbs xbd_flush_requests(sc); 450e4808c4bSKip Macy } 451e4808c4bSKip Macy 452e4808c4bSKip Macy static void 45333eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm) 454e4808c4bSKip Macy { 455e4808c4bSKip Macy struct bio *bp; 456e4808c4bSKip Macy 45733eebb6aSJustin T. Gibbs bp = cm->cm_bp; 458e4808c4bSKip Macy 45976acc41fSJustin T. Gibbs if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) { 460e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 46133eebb6aSJustin T. Gibbs printf(" status: %x\n", cm->cm_status); 462e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 463e4808c4bSKip Macy } 464e4808c4bSKip Macy 465e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 466e4808c4bSKip Macy bp->bio_error = EIO; 467e4808c4bSKip Macy else 468e4808c4bSKip Macy bp->bio_resid = 0; 469e4808c4bSKip Macy 47033eebb6aSJustin T. Gibbs xbd_free_command(cm); 471e4808c4bSKip Macy biodone(bp); 472e4808c4bSKip Macy } 473e4808c4bSKip Macy 474fac3fd80SJustin T. Gibbs static void 475fac3fd80SJustin T. Gibbs xbd_int(void *xsc) 476fac3fd80SJustin T. Gibbs { 477fac3fd80SJustin T. Gibbs struct xbd_softc *sc = xsc; 478fac3fd80SJustin T. Gibbs struct xbd_command *cm; 479fac3fd80SJustin T. Gibbs blkif_response_t *bret; 480fac3fd80SJustin T. Gibbs RING_IDX i, rp; 481fac3fd80SJustin T. Gibbs int op; 482fac3fd80SJustin T. Gibbs 483fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 484fac3fd80SJustin T. Gibbs 48576acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) { 486fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 487fac3fd80SJustin T. Gibbs return; 488fac3fd80SJustin T. Gibbs } 489fac3fd80SJustin T. Gibbs 490fac3fd80SJustin T. Gibbs again: 491fac3fd80SJustin T. Gibbs rp = sc->xbd_ring.sring->rsp_prod; 492fac3fd80SJustin T. Gibbs rmb(); /* Ensure we see queued responses up to 'rp'. */ 493fac3fd80SJustin T. Gibbs 494fac3fd80SJustin T. Gibbs for (i = sc->xbd_ring.rsp_cons; i != rp;) { 495fac3fd80SJustin T. Gibbs bret = RING_GET_RESPONSE(&sc->xbd_ring, i); 496fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[bret->id]; 497fac3fd80SJustin T. Gibbs 498e2c1fe90SJustin T. Gibbs xbd_remove_cm(cm, XBD_Q_BUSY); 499112cacaeSRoger Pau Monné gnttab_end_foreign_access_references(cm->cm_nseg, 500112cacaeSRoger Pau Monné cm->cm_sg_refs); 501112cacaeSRoger Pau Monné i++; 502fac3fd80SJustin T. Gibbs 503fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 504fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTREAD; 5059985113bSJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE || 5069985113bSJustin T. Gibbs cm->cm_operation == BLKIF_OP_WRITE_BARRIER) 507fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTWRITE; 508fac3fd80SJustin T. Gibbs else 509fac3fd80SJustin T. Gibbs op = 0; 510fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 511fac3fd80SJustin T. Gibbs bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map); 512fac3fd80SJustin T. Gibbs 513fac3fd80SJustin T. Gibbs /* 514127a9483SJustin T. Gibbs * Release any hold this command has on future command 515127a9483SJustin T. Gibbs * dispatch. 516fac3fd80SJustin T. Gibbs */ 5179985113bSJustin T. Gibbs xbd_cm_thaw(sc, cm); 518fac3fd80SJustin T. Gibbs 519fac3fd80SJustin T. Gibbs /* 520fac3fd80SJustin T. Gibbs * Directly call the i/o complete routine to save an 521fac3fd80SJustin T. Gibbs * an indirection in the common case. 522fac3fd80SJustin T. Gibbs */ 523fac3fd80SJustin T. Gibbs cm->cm_status = bret->status; 524fac3fd80SJustin T. Gibbs if (cm->cm_bp) 525fac3fd80SJustin T. Gibbs xbd_bio_complete(sc, cm); 526fac3fd80SJustin T. Gibbs else if (cm->cm_complete != NULL) 527fac3fd80SJustin T. Gibbs cm->cm_complete(cm); 528fac3fd80SJustin T. Gibbs else 529fac3fd80SJustin T. Gibbs xbd_free_command(cm); 530fac3fd80SJustin T. Gibbs } 531fac3fd80SJustin T. Gibbs 532fac3fd80SJustin T. Gibbs sc->xbd_ring.rsp_cons = i; 533fac3fd80SJustin T. Gibbs 534fac3fd80SJustin T. Gibbs if (i != sc->xbd_ring.req_prod_pvt) { 535fac3fd80SJustin T. Gibbs int more_to_do; 536fac3fd80SJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do); 537fac3fd80SJustin T. Gibbs if (more_to_do) 538fac3fd80SJustin T. Gibbs goto again; 539fac3fd80SJustin T. Gibbs } else { 540fac3fd80SJustin T. Gibbs sc->xbd_ring.sring->rsp_event = i + 1; 541fac3fd80SJustin T. Gibbs } 542fac3fd80SJustin T. Gibbs 5439985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) == 0) 5449985113bSJustin T. Gibbs xbd_thaw(sc, XBDF_WAIT_IDLE); 5459985113bSJustin T. Gibbs 546fac3fd80SJustin T. Gibbs xbd_startio(sc); 547fac3fd80SJustin T. Gibbs 54876acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED)) 549e2c1fe90SJustin T. Gibbs wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]); 550fac3fd80SJustin T. Gibbs 551fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 552fac3fd80SJustin T. Gibbs } 553fac3fd80SJustin T. Gibbs 554fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/ 555fac3fd80SJustin T. Gibbs /** 556fac3fd80SJustin T. Gibbs * Quiesce the disk writes for a dump file before allowing the next buffer. 557fac3fd80SJustin T. Gibbs */ 5588698b76cSKip Macy static void 55933eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc) 5608698b76cSKip Macy { 5618698b76cSKip Macy int mtd; 5628698b76cSKip Macy 5638698b76cSKip Macy // While there are outstanding requests 5649985113bSJustin T. Gibbs while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 56533eebb6aSJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd); 5668698b76cSKip Macy if (mtd) { 567e4808c4bSKip Macy /* Recieved request completions, update queue. */ 56833eebb6aSJustin T. Gibbs xbd_int(sc); 5698698b76cSKip Macy } 5709985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 571e4808c4bSKip Macy /* 572e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 573e4808c4bSKip Macy * to complete. 574e4808c4bSKip Macy */ 575be7747b4SKip Macy HYPERVISOR_yield(); 5768698b76cSKip Macy } 5778698b76cSKip Macy } 5788698b76cSKip Macy } 5798698b76cSKip Macy 580e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 581e4808c4bSKip Macy static void 58233eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm) 583e4808c4bSKip Macy { 5848698b76cSKip Macy 585e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_COMPLETE); 586e4808c4bSKip Macy } 587e4808c4bSKip Macy 5888698b76cSKip Macy static int 58933eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 5908698b76cSKip Macy size_t length) 5918698b76cSKip Macy { 5928698b76cSKip Macy struct disk *dp = arg; 59333eebb6aSJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 59433eebb6aSJustin T. Gibbs struct xbd_command *cm; 595e4808c4bSKip Macy size_t chunk; 596e4808c4bSKip Macy int sbp; 5978698b76cSKip Macy int rc = 0; 5988698b76cSKip Macy 599e4808c4bSKip Macy if (length <= 0) 600e4808c4bSKip Macy return (rc); 6018698b76cSKip Macy 60233eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quiet on the western front. */ 603e4808c4bSKip Macy 604e4808c4bSKip Macy /* 605e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 606e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 607e4808c4bSKip Macy */ 60833eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 609e4808c4bSKip Macy 610e4808c4bSKip Macy /* Split the 64KB block as needed */ 611e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 612e2c1fe90SJustin T. Gibbs cm = xbd_dequeue_cm(sc, XBD_Q_FREE); 613e4808c4bSKip Macy if (cm == NULL) { 61433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 61533eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "dump: no more commands?\n"); 616e4808c4bSKip Macy return (EBUSY); 617e4808c4bSKip Macy } 618e4808c4bSKip Macy 61933eebb6aSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 62033eebb6aSJustin T. Gibbs &cm->cm_gref_head) != 0) { 62133eebb6aSJustin T. Gibbs xbd_free_command(cm); 62233eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 62333eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "no more grant allocs?\n"); 624e4808c4bSKip Macy return (EBUSY); 625e4808c4bSKip Macy } 626e4808c4bSKip Macy 62733eebb6aSJustin T. Gibbs chunk = length > sc->xbd_max_request_size ? 62833eebb6aSJustin T. Gibbs sc->xbd_max_request_size : length; 62933eebb6aSJustin T. Gibbs cm->cm_data = virtual; 63033eebb6aSJustin T. Gibbs cm->cm_datalen = chunk; 63133eebb6aSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE; 63233eebb6aSJustin T. Gibbs cm->cm_sector_number = offset / dp->d_sectorsize; 63333eebb6aSJustin T. Gibbs cm->cm_complete = xbd_dump_complete; 634e4808c4bSKip Macy 635e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_READY); 6368698b76cSKip Macy 6378698b76cSKip Macy length -= chunk; 6388698b76cSKip Macy offset += chunk; 6398698b76cSKip Macy virtual = (char *) virtual + chunk; 6408698b76cSKip Macy } 6418698b76cSKip Macy 642e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 64333eebb6aSJustin T. Gibbs xbd_startio(sc); 64433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 645e4808c4bSKip Macy 646e4808c4bSKip Macy /* Poll for the completion. */ 64733eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quite on the eastern front */ 648e4808c4bSKip Macy 649e4808c4bSKip Macy /* If there were any errors, bail out... */ 650e2c1fe90SJustin T. Gibbs while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) { 65133eebb6aSJustin T. Gibbs if (cm->cm_status != BLKIF_RSP_OKAY) { 65233eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 653e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 65433eebb6aSJustin T. Gibbs cm->cm_sector_number); 655e4808c4bSKip Macy rc = EIO; 6568698b76cSKip Macy } 65733eebb6aSJustin T. Gibbs xbd_free_command(cm); 6588698b76cSKip Macy } 659e4808c4bSKip Macy 6608698b76cSKip Macy return (rc); 6618698b76cSKip Macy } 6628698b76cSKip Macy 663fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/ 66423dc5621SKip Macy static int 665fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp) 66689e0f4d2SKip Macy { 667fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 66823dc5621SKip Macy 669fac3fd80SJustin T. Gibbs if (sc == NULL) { 670fac3fd80SJustin T. Gibbs printf("xb%d: not found", sc->xbd_unit); 67123dc5621SKip Macy return (ENXIO); 67223dc5621SKip Macy } 67323dc5621SKip Macy 674e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_OPEN; 675fac3fd80SJustin T. Gibbs sc->xbd_users++; 676fac3fd80SJustin T. Gibbs return (0); 677fac3fd80SJustin T. Gibbs } 678fac3fd80SJustin T. Gibbs 679fac3fd80SJustin T. Gibbs static int 680fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp) 681fac3fd80SJustin T. Gibbs { 682fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 683fac3fd80SJustin T. Gibbs 684fac3fd80SJustin T. Gibbs if (sc == NULL) 685fac3fd80SJustin T. Gibbs return (ENXIO); 686e2c1fe90SJustin T. Gibbs sc->xbd_flags &= ~XBDF_OPEN; 687fac3fd80SJustin T. Gibbs if (--(sc->xbd_users) == 0) { 688fac3fd80SJustin T. Gibbs /* 689fac3fd80SJustin T. Gibbs * Check whether we have been instructed to close. We will 690fac3fd80SJustin T. Gibbs * have ignored this request initially, as the device was 691fac3fd80SJustin T. Gibbs * still mounted. 692fac3fd80SJustin T. Gibbs */ 693fac3fd80SJustin T. Gibbs if (xenbus_get_otherend_state(sc->xbd_dev) == 694fac3fd80SJustin T. Gibbs XenbusStateClosing) 695fac3fd80SJustin T. Gibbs xbd_closing(sc->xbd_dev); 696fac3fd80SJustin T. Gibbs } 697fac3fd80SJustin T. Gibbs return (0); 698fac3fd80SJustin T. Gibbs } 699fac3fd80SJustin T. Gibbs 700fac3fd80SJustin T. Gibbs static int 701fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 702fac3fd80SJustin T. Gibbs { 703fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 704fac3fd80SJustin T. Gibbs 705fac3fd80SJustin T. Gibbs if (sc == NULL) 706fac3fd80SJustin T. Gibbs return (ENXIO); 707fac3fd80SJustin T. Gibbs 708fac3fd80SJustin T. Gibbs return (ENOTTY); 709fac3fd80SJustin T. Gibbs } 710fac3fd80SJustin T. Gibbs 711fac3fd80SJustin T. Gibbs /* 712fac3fd80SJustin T. Gibbs * Read/write routine for a buffer. Finds the proper unit, place it on 713fac3fd80SJustin T. Gibbs * the sortq and kick the controller. 714fac3fd80SJustin T. Gibbs */ 715fac3fd80SJustin T. Gibbs static void 716fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp) 717fac3fd80SJustin T. Gibbs { 718fac3fd80SJustin T. Gibbs struct xbd_softc *sc = bp->bio_disk->d_drv1; 719fac3fd80SJustin T. Gibbs 720fac3fd80SJustin T. Gibbs /* bogus disk? */ 721fac3fd80SJustin T. Gibbs if (sc == NULL) { 722fac3fd80SJustin T. Gibbs bp->bio_error = EINVAL; 723fac3fd80SJustin T. Gibbs bp->bio_flags |= BIO_ERROR; 724fac3fd80SJustin T. Gibbs bp->bio_resid = bp->bio_bcount; 725fac3fd80SJustin T. Gibbs biodone(bp); 726fac3fd80SJustin T. Gibbs return; 727fac3fd80SJustin T. Gibbs } 728fac3fd80SJustin T. Gibbs 729fac3fd80SJustin T. Gibbs /* 730fac3fd80SJustin T. Gibbs * Place it in the queue of disk activities for this disk 731fac3fd80SJustin T. Gibbs */ 732fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 733fac3fd80SJustin T. Gibbs 734fac3fd80SJustin T. Gibbs xbd_enqueue_bio(sc, bp); 735fac3fd80SJustin T. Gibbs xbd_startio(sc); 736fac3fd80SJustin T. Gibbs 737fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 738fac3fd80SJustin T. Gibbs return; 739fac3fd80SJustin T. Gibbs } 740fac3fd80SJustin T. Gibbs 741fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/ 742fac3fd80SJustin T. Gibbs static int 743cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc) 744fac3fd80SJustin T. Gibbs { 745fac3fd80SJustin T. Gibbs blkif_sring_t *sring; 746fac3fd80SJustin T. Gibbs uintptr_t sring_page_addr; 747fac3fd80SJustin T. Gibbs int error; 748fac3fd80SJustin T. Gibbs int i; 749fac3fd80SJustin T. Gibbs 750fac3fd80SJustin T. Gibbs sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 751fac3fd80SJustin T. Gibbs M_NOWAIT|M_ZERO); 752fac3fd80SJustin T. Gibbs if (sring == NULL) { 753fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring"); 754fac3fd80SJustin T. Gibbs return (ENOMEM); 755fac3fd80SJustin T. Gibbs } 756fac3fd80SJustin T. Gibbs SHARED_RING_INIT(sring); 757fac3fd80SJustin T. Gibbs FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE); 758fac3fd80SJustin T. Gibbs 759fac3fd80SJustin T. Gibbs for (i = 0, sring_page_addr = (uintptr_t)sring; 760fac3fd80SJustin T. Gibbs i < sc->xbd_ring_pages; 761fac3fd80SJustin T. Gibbs i++, sring_page_addr += PAGE_SIZE) { 762fac3fd80SJustin T. Gibbs 763fac3fd80SJustin T. Gibbs error = xenbus_grant_ring(sc->xbd_dev, 764*3c790178SJohn Baldwin (vtophys(sring_page_addr) >> PAGE_SHIFT), 765fac3fd80SJustin T. Gibbs &sc->xbd_ring_ref[i]); 766fac3fd80SJustin T. Gibbs if (error) { 767fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 768fac3fd80SJustin T. Gibbs "granting ring_ref(%d)", i); 769fac3fd80SJustin T. Gibbs return (error); 770fac3fd80SJustin T. Gibbs } 771fac3fd80SJustin T. Gibbs } 772fac3fd80SJustin T. Gibbs if (sc->xbd_ring_pages == 1) { 773fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 774fac3fd80SJustin T. Gibbs "ring-ref", "%u", sc->xbd_ring_ref[0]); 775fac3fd80SJustin T. Gibbs if (error) { 776fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 777fac3fd80SJustin T. Gibbs "writing %s/ring-ref", 778fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev)); 779fac3fd80SJustin T. Gibbs return (error); 780fac3fd80SJustin T. Gibbs } 781fac3fd80SJustin T. Gibbs } else { 782fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 783fac3fd80SJustin T. Gibbs char ring_ref_name[]= "ring_refXX"; 784fac3fd80SJustin T. Gibbs 785fac3fd80SJustin T. Gibbs snprintf(ring_ref_name, sizeof(ring_ref_name), 786fac3fd80SJustin T. Gibbs "ring-ref%u", i); 787fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 788fac3fd80SJustin T. Gibbs ring_ref_name, "%u", sc->xbd_ring_ref[i]); 789fac3fd80SJustin T. Gibbs if (error) { 790fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 791fac3fd80SJustin T. Gibbs "writing %s/%s", 792fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev), 793fac3fd80SJustin T. Gibbs ring_ref_name); 794fac3fd80SJustin T. Gibbs return (error); 795fac3fd80SJustin T. Gibbs } 796fac3fd80SJustin T. Gibbs } 797fac3fd80SJustin T. Gibbs } 798fac3fd80SJustin T. Gibbs 79976acc41fSJustin T. Gibbs error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev, 80076acc41fSJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc, 80176acc41fSJustin T. Gibbs INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle); 802fac3fd80SJustin T. Gibbs if (error) { 803fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 80476acc41fSJustin T. Gibbs "xen_intr_alloc_and_bind_local_port failed"); 805fac3fd80SJustin T. Gibbs return (error); 806fac3fd80SJustin T. Gibbs } 807fac3fd80SJustin T. Gibbs 808fac3fd80SJustin T. Gibbs return (0); 809fac3fd80SJustin T. Gibbs } 810fac3fd80SJustin T. Gibbs 811d9fab01dSJustin T. Gibbs static void 812d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc) 813d9fab01dSJustin T. Gibbs { 814d9fab01dSJustin T. Gibbs int i; 815d9fab01dSJustin T. Gibbs 816d9fab01dSJustin T. Gibbs if (sc->xbd_ring.sring == NULL) 817d9fab01dSJustin T. Gibbs return; 818d9fab01dSJustin T. Gibbs 819d9fab01dSJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 820d9fab01dSJustin T. Gibbs if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) { 821d9fab01dSJustin T. Gibbs gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]); 822d9fab01dSJustin T. Gibbs sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 823d9fab01dSJustin T. Gibbs } 824d9fab01dSJustin T. Gibbs } 825d9fab01dSJustin T. Gibbs free(sc->xbd_ring.sring, M_XENBLOCKFRONT); 826d9fab01dSJustin T. Gibbs sc->xbd_ring.sring = NULL; 827d9fab01dSJustin T. Gibbs } 828d9fab01dSJustin T. Gibbs 829fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/ 8309985113bSJustin T. Gibbs static int 8319985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len) 8329985113bSJustin T. Gibbs { 8339985113bSJustin T. Gibbs struct sbuf sb; 8349985113bSJustin T. Gibbs int feature_cnt; 8359985113bSJustin T. Gibbs 8369985113bSJustin T. Gibbs sbuf_new(&sb, features, len, SBUF_FIXEDLEN); 8379985113bSJustin T. Gibbs 8389985113bSJustin T. Gibbs feature_cnt = 0; 8399985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_FLUSH) != 0) { 8409985113bSJustin T. Gibbs sbuf_printf(&sb, "flush"); 8419985113bSJustin T. Gibbs feature_cnt++; 8429985113bSJustin T. Gibbs } 8439985113bSJustin T. Gibbs 8449985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 8459985113bSJustin T. Gibbs if (feature_cnt != 0) 8469985113bSJustin T. Gibbs sbuf_printf(&sb, ", "); 8479985113bSJustin T. Gibbs sbuf_printf(&sb, "write_barrier"); 8489985113bSJustin T. Gibbs feature_cnt++; 8499985113bSJustin T. Gibbs } 8509985113bSJustin T. Gibbs 8519985113bSJustin T. Gibbs (void) sbuf_finish(&sb); 8529985113bSJustin T. Gibbs return (sbuf_len(&sb)); 8539985113bSJustin T. Gibbs } 8549985113bSJustin T. Gibbs 8559985113bSJustin T. Gibbs static int 8569985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS) 8579985113bSJustin T. Gibbs { 8589985113bSJustin T. Gibbs char features[80]; 8599985113bSJustin T. Gibbs struct xbd_softc *sc = arg1; 8609985113bSJustin T. Gibbs int error; 8619985113bSJustin T. Gibbs int len; 8629985113bSJustin T. Gibbs 8639985113bSJustin T. Gibbs error = sysctl_wire_old_buffer(req, 0); 8649985113bSJustin T. Gibbs if (error != 0) 8659985113bSJustin T. Gibbs return (error); 8669985113bSJustin T. Gibbs 8679985113bSJustin T. Gibbs len = xbd_feature_string(sc, features, sizeof(features)); 8689985113bSJustin T. Gibbs 8699985113bSJustin T. Gibbs /* len is -1 on error, which will make the SYSCTL_OUT a no-op. */ 8709985113bSJustin T. Gibbs return (SYSCTL_OUT(req, features, len + 1/*NUL*/)); 8719985113bSJustin T. Gibbs } 8729985113bSJustin T. Gibbs 8738b8bfa35SJustin T. Gibbs static void 87433eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd) 8758b8bfa35SJustin T. Gibbs { 8768b8bfa35SJustin T. Gibbs struct sysctl_ctx_list *sysctl_ctx = NULL; 8778b8bfa35SJustin T. Gibbs struct sysctl_oid *sysctl_tree = NULL; 8789985113bSJustin T. Gibbs struct sysctl_oid_list *children; 8798b8bfa35SJustin T. Gibbs 88033eebb6aSJustin T. Gibbs sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev); 8818b8bfa35SJustin T. Gibbs if (sysctl_ctx == NULL) 8828b8bfa35SJustin T. Gibbs return; 8838b8bfa35SJustin T. Gibbs 88433eebb6aSJustin T. Gibbs sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev); 8858b8bfa35SJustin T. Gibbs if (sysctl_tree == NULL) 8868b8bfa35SJustin T. Gibbs return; 8878b8bfa35SJustin T. Gibbs 8889985113bSJustin T. Gibbs children = SYSCTL_CHILDREN(sysctl_tree); 8899985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 89033eebb6aSJustin T. Gibbs "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1, 8918b8bfa35SJustin T. Gibbs "maximum outstanding requests (negotiated)"); 8928b8bfa35SJustin T. Gibbs 8939985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 8948b8bfa35SJustin T. Gibbs "max_request_segments", CTLFLAG_RD, 89533eebb6aSJustin T. Gibbs &xbd->xbd_max_request_segments, 0, 8968b8bfa35SJustin T. Gibbs "maximum number of pages per requests (negotiated)"); 8978b8bfa35SJustin T. Gibbs 8989985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 89933eebb6aSJustin T. Gibbs "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0, 9008b8bfa35SJustin T. Gibbs "maximum size in bytes of a request (negotiated)"); 9018b8bfa35SJustin T. Gibbs 9029985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 90333eebb6aSJustin T. Gibbs "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0, 9048b8bfa35SJustin T. Gibbs "communication channel pages (negotiated)"); 9059985113bSJustin T. Gibbs 9069985113bSJustin T. Gibbs SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO, 9079985113bSJustin T. Gibbs "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0, 9089985113bSJustin T. Gibbs xbd_sysctl_features, "A", "protocol features (negotiated)"); 9098b8bfa35SJustin T. Gibbs } 9108b8bfa35SJustin T. Gibbs 91123dc5621SKip Macy /* 912fac3fd80SJustin T. Gibbs * Translate Linux major/minor to an appropriate name and unit 913fac3fd80SJustin T. Gibbs * number. For HVM guests, this allows us to use the same drive names 914fac3fd80SJustin T. Gibbs * with blkfront as the emulated drives, easing transition slightly. 91523dc5621SKip Macy */ 916fac3fd80SJustin T. Gibbs static void 917fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name) 91823dc5621SKip Macy { 919fac3fd80SJustin T. Gibbs static struct vdev_info { 920fac3fd80SJustin T. Gibbs int major; 921fac3fd80SJustin T. Gibbs int shift; 922fac3fd80SJustin T. Gibbs int base; 92323dc5621SKip Macy const char *name; 924fac3fd80SJustin T. Gibbs } info[] = { 925fac3fd80SJustin T. Gibbs {3, 6, 0, "ada"}, /* ide0 */ 926fac3fd80SJustin T. Gibbs {22, 6, 2, "ada"}, /* ide1 */ 927fac3fd80SJustin T. Gibbs {33, 6, 4, "ada"}, /* ide2 */ 928fac3fd80SJustin T. Gibbs {34, 6, 6, "ada"}, /* ide3 */ 929fac3fd80SJustin T. Gibbs {56, 6, 8, "ada"}, /* ide4 */ 930fac3fd80SJustin T. Gibbs {57, 6, 10, "ada"}, /* ide5 */ 931fac3fd80SJustin T. Gibbs {88, 6, 12, "ada"}, /* ide6 */ 932fac3fd80SJustin T. Gibbs {89, 6, 14, "ada"}, /* ide7 */ 933fac3fd80SJustin T. Gibbs {90, 6, 16, "ada"}, /* ide8 */ 934fac3fd80SJustin T. Gibbs {91, 6, 18, "ada"}, /* ide9 */ 93589e0f4d2SKip Macy 936fac3fd80SJustin T. Gibbs {8, 4, 0, "da"}, /* scsi disk0 */ 937fac3fd80SJustin T. Gibbs {65, 4, 16, "da"}, /* scsi disk1 */ 938fac3fd80SJustin T. Gibbs {66, 4, 32, "da"}, /* scsi disk2 */ 939fac3fd80SJustin T. Gibbs {67, 4, 48, "da"}, /* scsi disk3 */ 940fac3fd80SJustin T. Gibbs {68, 4, 64, "da"}, /* scsi disk4 */ 941fac3fd80SJustin T. Gibbs {69, 4, 80, "da"}, /* scsi disk5 */ 942fac3fd80SJustin T. Gibbs {70, 4, 96, "da"}, /* scsi disk6 */ 943fac3fd80SJustin T. Gibbs {71, 4, 112, "da"}, /* scsi disk7 */ 944fac3fd80SJustin T. Gibbs {128, 4, 128, "da"}, /* scsi disk8 */ 945fac3fd80SJustin T. Gibbs {129, 4, 144, "da"}, /* scsi disk9 */ 946fac3fd80SJustin T. Gibbs {130, 4, 160, "da"}, /* scsi disk10 */ 947fac3fd80SJustin T. Gibbs {131, 4, 176, "da"}, /* scsi disk11 */ 948fac3fd80SJustin T. Gibbs {132, 4, 192, "da"}, /* scsi disk12 */ 949fac3fd80SJustin T. Gibbs {133, 4, 208, "da"}, /* scsi disk13 */ 950fac3fd80SJustin T. Gibbs {134, 4, 224, "da"}, /* scsi disk14 */ 951fac3fd80SJustin T. Gibbs {135, 4, 240, "da"}, /* scsi disk15 */ 952fac3fd80SJustin T. Gibbs 953fac3fd80SJustin T. Gibbs {202, 4, 0, "xbd"}, /* xbd */ 954fac3fd80SJustin T. Gibbs 955fac3fd80SJustin T. Gibbs {0, 0, 0, NULL}, 956fac3fd80SJustin T. Gibbs }; 957fac3fd80SJustin T. Gibbs int major = vdevice >> 8; 958fac3fd80SJustin T. Gibbs int minor = vdevice & 0xff; 959fac3fd80SJustin T. Gibbs int i; 960fac3fd80SJustin T. Gibbs 961fac3fd80SJustin T. Gibbs if (vdevice & (1 << 28)) { 962fac3fd80SJustin T. Gibbs *unit = (vdevice & ((1 << 28) - 1)) >> 8; 963fac3fd80SJustin T. Gibbs *name = "xbd"; 964fac3fd80SJustin T. Gibbs return; 96589e0f4d2SKip Macy } 96689e0f4d2SKip Macy 967fac3fd80SJustin T. Gibbs for (i = 0; info[i].major; i++) { 968fac3fd80SJustin T. Gibbs if (info[i].major == major) { 969fac3fd80SJustin T. Gibbs *unit = info[i].base + (minor >> info[i].shift); 970fac3fd80SJustin T. Gibbs *name = info[i].name; 971fac3fd80SJustin T. Gibbs return; 972fac3fd80SJustin T. Gibbs } 973fac3fd80SJustin T. Gibbs } 974fac3fd80SJustin T. Gibbs 975fac3fd80SJustin T. Gibbs *unit = minor >> 4; 976fac3fd80SJustin T. Gibbs *name = "xbd"; 977fac3fd80SJustin T. Gibbs } 978fac3fd80SJustin T. Gibbs 979fac3fd80SJustin T. Gibbs int 980fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors, 981fac3fd80SJustin T. Gibbs int vdevice, uint16_t vdisk_info, unsigned long sector_size) 982fac3fd80SJustin T. Gibbs { 9839985113bSJustin T. Gibbs char features[80]; 984fac3fd80SJustin T. Gibbs int unit, error = 0; 985fac3fd80SJustin T. Gibbs const char *name; 986fac3fd80SJustin T. Gibbs 98733eebb6aSJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 98823dc5621SKip Macy 989fac3fd80SJustin T. Gibbs sc->xbd_unit = unit; 99089e0f4d2SKip Macy 9919985113bSJustin T. Gibbs if (strcmp(name, "xbd") != 0) 992fac3fd80SJustin T. Gibbs device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit); 99389e0f4d2SKip Macy 9949985113bSJustin T. Gibbs if (xbd_feature_string(sc, features, sizeof(features)) > 0) { 9959985113bSJustin T. Gibbs device_printf(sc->xbd_dev, "features: %s\n", 9969985113bSJustin T. Gibbs features); 9979985113bSJustin T. Gibbs } 9989985113bSJustin T. Gibbs 999fac3fd80SJustin T. Gibbs sc->xbd_disk = disk_alloc(); 1000fac3fd80SJustin T. Gibbs sc->xbd_disk->d_unit = sc->xbd_unit; 1001fac3fd80SJustin T. Gibbs sc->xbd_disk->d_open = xbd_open; 1002fac3fd80SJustin T. Gibbs sc->xbd_disk->d_close = xbd_close; 1003fac3fd80SJustin T. Gibbs sc->xbd_disk->d_ioctl = xbd_ioctl; 1004fac3fd80SJustin T. Gibbs sc->xbd_disk->d_strategy = xbd_strategy; 1005fac3fd80SJustin T. Gibbs sc->xbd_disk->d_dump = xbd_dump; 1006fac3fd80SJustin T. Gibbs sc->xbd_disk->d_name = name; 1007fac3fd80SJustin T. Gibbs sc->xbd_disk->d_drv1 = sc; 1008fac3fd80SJustin T. Gibbs sc->xbd_disk->d_sectorsize = sector_size; 10098b8bfa35SJustin T. Gibbs 1010fac3fd80SJustin T. Gibbs sc->xbd_disk->d_mediasize = sectors * sector_size; 1011fac3fd80SJustin T. Gibbs sc->xbd_disk->d_maxsize = sc->xbd_max_request_size; 101277b6916dSRoger Pau Monné sc->xbd_disk->d_flags = 0; 10139985113bSJustin T. Gibbs if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) { 10149985113bSJustin T. Gibbs sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 10159985113bSJustin T. Gibbs device_printf(sc->xbd_dev, 10169985113bSJustin T. Gibbs "synchronize cache commands enabled.\n"); 10179985113bSJustin T. Gibbs } 1018fac3fd80SJustin T. Gibbs disk_create(sc->xbd_disk, DISK_VERSION); 101912678024SDoug Rabson 1020fac3fd80SJustin T. Gibbs return error; 102112678024SDoug Rabson } 102212678024SDoug Rabson 1023fac3fd80SJustin T. Gibbs static void 1024fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc) 102512678024SDoug Rabson { 1026fac3fd80SJustin T. Gibbs int i; 102712678024SDoug Rabson 102812678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 102933eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1030e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_DISCONNECTED; 103133eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 103289e0f4d2SKip Macy 1033fac3fd80SJustin T. Gibbs /* Free resources associated with old device channel. */ 1034d9fab01dSJustin T. Gibbs xbd_free_ring(sc); 1035fac3fd80SJustin T. Gibbs if (sc->xbd_shadow) { 103689e0f4d2SKip Macy 1037fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_max_requests; i++) { 1038fac3fd80SJustin T. Gibbs struct xbd_command *cm; 103989e0f4d2SKip Macy 1040fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[i]; 1041fac3fd80SJustin T. Gibbs if (cm->cm_sg_refs != NULL) { 1042fac3fd80SJustin T. Gibbs free(cm->cm_sg_refs, M_XENBLOCKFRONT); 1043fac3fd80SJustin T. Gibbs cm->cm_sg_refs = NULL; 104489e0f4d2SKip Macy } 104589e0f4d2SKip Macy 1046aaebf690SColin Percival if (cm->cm_indirectionpages != NULL) { 1047aaebf690SColin Percival gnttab_end_foreign_access_references( 1048aaebf690SColin Percival sc->xbd_max_request_indirectpages, 1049aaebf690SColin Percival &cm->cm_indirectionrefs[0]); 1050aaebf690SColin Percival contigfree(cm->cm_indirectionpages, PAGE_SIZE * 1051aaebf690SColin Percival sc->xbd_max_request_indirectpages, 1052aaebf690SColin Percival M_XENBLOCKFRONT); 1053aaebf690SColin Percival cm->cm_indirectionpages = NULL; 1054aaebf690SColin Percival } 1055aaebf690SColin Percival 1056fac3fd80SJustin T. Gibbs bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map); 1057fac3fd80SJustin T. Gibbs } 1058fac3fd80SJustin T. Gibbs free(sc->xbd_shadow, M_XENBLOCKFRONT); 1059fac3fd80SJustin T. Gibbs sc->xbd_shadow = NULL; 1060fac3fd80SJustin T. Gibbs 1061fac3fd80SJustin T. Gibbs bus_dma_tag_destroy(sc->xbd_io_dmat); 1062fac3fd80SJustin T. Gibbs 1063e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_FREE); 1064e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_READY); 1065e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_COMPLETE); 1066fac3fd80SJustin T. Gibbs } 1067fac3fd80SJustin T. Gibbs 106876acc41fSJustin T. Gibbs xen_intr_unbind(&sc->xen_intr_handle); 106976acc41fSJustin T. Gibbs 1070fac3fd80SJustin T. Gibbs } 1071fac3fd80SJustin T. Gibbs 1072fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/ 1073ff662b5cSJustin T. Gibbs static void 107433eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc) 107589e0f4d2SKip Macy { 1076ff662b5cSJustin T. Gibbs const char *otherend_path; 1077ff662b5cSJustin T. Gibbs const char *node_path; 10788b8bfa35SJustin T. Gibbs uint32_t max_ring_page_order; 1079ff662b5cSJustin T. Gibbs int error; 108089e0f4d2SKip Macy 108133eebb6aSJustin T. Gibbs if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) { 108206a630f6SJustin T. Gibbs /* Initialization has already been performed. */ 1083ff662b5cSJustin T. Gibbs return; 108406a630f6SJustin T. Gibbs } 108589e0f4d2SKip Macy 1086ff662b5cSJustin T. Gibbs /* 1087ff662b5cSJustin T. Gibbs * Protocol defaults valid even if negotiation for a 1088ff662b5cSJustin T. Gibbs * setting fails. 1089ff662b5cSJustin T. Gibbs */ 10908b8bfa35SJustin T. Gibbs max_ring_page_order = 0; 109133eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 1092ff662b5cSJustin T. Gibbs 1093ff662b5cSJustin T. Gibbs /* 1094ff662b5cSJustin T. Gibbs * Protocol negotiation. 1095ff662b5cSJustin T. Gibbs * 1096ff662b5cSJustin T. Gibbs * \note xs_gather() returns on the first encountered error, so 1097ff662b5cSJustin T. Gibbs * we must use independant calls in order to guarantee 1098ff662b5cSJustin T. Gibbs * we don't miss information in a sparsly populated back-end 1099ff662b5cSJustin T. Gibbs * tree. 11008b8bfa35SJustin T. Gibbs * 11018b8bfa35SJustin T. Gibbs * \note xs_scanf() does not update variables for unmatched 11028b8bfa35SJustin T. Gibbs * fields. 1103ff662b5cSJustin T. Gibbs */ 110433eebb6aSJustin T. Gibbs otherend_path = xenbus_get_otherend_path(sc->xbd_dev); 110533eebb6aSJustin T. Gibbs node_path = xenbus_get_node(sc->xbd_dev); 11068b8bfa35SJustin T. Gibbs 11078b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 11088b8bfa35SJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 11098b8bfa35SJustin T. Gibbs "max-ring-page-order", NULL, "%" PRIu32, 11108b8bfa35SJustin T. Gibbs &max_ring_page_order); 111133eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1 << max_ring_page_order; 1112ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 1113ff662b5cSJustin T. Gibbs "max-ring-pages", NULL, "%" PRIu32, 111433eebb6aSJustin T. Gibbs &sc->xbd_ring_pages); 111533eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages < 1) 111633eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 1117ff662b5cSJustin T. Gibbs 111833eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) { 111933eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 112033eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 1121112cacaeSRoger Pau Monné "limited to front-end limit of %u.\n", 112233eebb6aSJustin T. Gibbs sc->xbd_ring_pages, XBD_MAX_RING_PAGES); 112333eebb6aSJustin T. Gibbs sc->xbd_ring_pages = XBD_MAX_RING_PAGES; 112489e0f4d2SKip Macy } 112589e0f4d2SKip Macy 112633eebb6aSJustin T. Gibbs if (powerof2(sc->xbd_ring_pages) == 0) { 11278b8bfa35SJustin T. Gibbs uint32_t new_page_limit; 11288b8bfa35SJustin T. Gibbs 112933eebb6aSJustin T. Gibbs new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1); 113033eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 113133eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 113233eebb6aSJustin T. Gibbs "is not a power of 2. Limited to %u.\n", 113333eebb6aSJustin T. Gibbs sc->xbd_ring_pages, new_page_limit); 113433eebb6aSJustin T. Gibbs sc->xbd_ring_pages = new_page_limit; 11358b8bfa35SJustin T. Gibbs } 11368b8bfa35SJustin T. Gibbs 1137112cacaeSRoger Pau Monné sc->xbd_max_requests = 1138112cacaeSRoger Pau Monné BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE); 113933eebb6aSJustin T. Gibbs if (sc->xbd_max_requests > XBD_MAX_REQUESTS) { 114033eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 114133eebb6aSJustin T. Gibbs "Back-end specified max_requests of %u " 1142112cacaeSRoger Pau Monné "limited to front-end limit of %zu.\n", 114333eebb6aSJustin T. Gibbs sc->xbd_max_requests, XBD_MAX_REQUESTS); 114433eebb6aSJustin T. Gibbs sc->xbd_max_requests = XBD_MAX_REQUESTS; 114589e0f4d2SKip Macy } 1146ff662b5cSJustin T. Gibbs 1147cdf5d66fSJustin T. Gibbs if (xbd_alloc_ring(sc) != 0) 1148ff662b5cSJustin T. Gibbs return; 1149ff662b5cSJustin T. Gibbs 11508b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 115133eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > 1) { 1152ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 115333eebb6aSJustin T. Gibbs "num-ring-pages","%u", 115433eebb6aSJustin T. Gibbs sc->xbd_ring_pages); 1155ff662b5cSJustin T. Gibbs if (error) { 115633eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 11578b8bfa35SJustin T. Gibbs "writing %s/num-ring-pages", 11588b8bfa35SJustin T. Gibbs node_path); 11598b8bfa35SJustin T. Gibbs return; 11608b8bfa35SJustin T. Gibbs } 11610d172324SJustin T. Gibbs 11628b8bfa35SJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 11630d172324SJustin T. Gibbs "ring-page-order", "%u", 116433eebb6aSJustin T. Gibbs fls(sc->xbd_ring_pages) - 1); 11658b8bfa35SJustin T. Gibbs if (error) { 116633eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 11678b8bfa35SJustin T. Gibbs "writing %s/ring-page-order", 1168ff662b5cSJustin T. Gibbs node_path); 1169ff662b5cSJustin T. Gibbs return; 1170ff662b5cSJustin T. Gibbs } 11710d172324SJustin T. Gibbs } 1172ff662b5cSJustin T. Gibbs 1173ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "event-channel", 117476acc41fSJustin T. Gibbs "%u", xen_intr_port(sc->xen_intr_handle)); 1175ff662b5cSJustin T. Gibbs if (error) { 117633eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1177ff662b5cSJustin T. Gibbs "writing %s/event-channel", 1178ff662b5cSJustin T. Gibbs node_path); 1179ff662b5cSJustin T. Gibbs return; 1180ff662b5cSJustin T. Gibbs } 1181ff662b5cSJustin T. Gibbs 118233eebb6aSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "protocol", 118333eebb6aSJustin T. Gibbs "%s", XEN_IO_PROTO_ABI_NATIVE); 1184ff662b5cSJustin T. Gibbs if (error) { 118533eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1186ff662b5cSJustin T. Gibbs "writing %s/protocol", 1187ff662b5cSJustin T. Gibbs node_path); 1188ff662b5cSJustin T. Gibbs return; 118923dc5621SKip Macy } 119012678024SDoug Rabson 119133eebb6aSJustin T. Gibbs xenbus_set_state(sc->xbd_dev, XenbusStateInitialised); 119289e0f4d2SKip Macy } 119389e0f4d2SKip Macy 119489e0f4d2SKip Macy /* 119533eebb6aSJustin T. Gibbs * Invoked when the backend is finally 'ready' (and has published 119633eebb6aSJustin T. Gibbs * the details about the physical device - #sectors, size, etc). 119789e0f4d2SKip Macy */ 119889e0f4d2SKip Macy static void 119933eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc) 120089e0f4d2SKip Macy { 120133eebb6aSJustin T. Gibbs device_t dev = sc->xbd_dev; 120289e0f4d2SKip Macy unsigned long sectors, sector_size; 120389e0f4d2SKip Macy unsigned int binfo; 12049985113bSJustin T. Gibbs int err, feature_barrier, feature_flush; 1205aaebf690SColin Percival int i, j; 120689e0f4d2SKip Macy 1207e2c1fe90SJustin T. Gibbs if (sc->xbd_state == XBD_STATE_CONNECTED || 1208e2c1fe90SJustin T. Gibbs sc->xbd_state == XBD_STATE_SUSPENDED) 120989e0f4d2SKip Macy return; 121089e0f4d2SKip Macy 121123dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 121289e0f4d2SKip Macy 1213ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 121489e0f4d2SKip Macy "sectors", "%lu", §ors, 121589e0f4d2SKip Macy "info", "%u", &binfo, 121689e0f4d2SKip Macy "sector-size", "%lu", §or_size, 121789e0f4d2SKip Macy NULL); 121889e0f4d2SKip Macy if (err) { 121923dc5621SKip Macy xenbus_dev_fatal(dev, err, 122089e0f4d2SKip Macy "reading backend fields at %s", 122123dc5621SKip Macy xenbus_get_otherend_path(dev)); 122289e0f4d2SKip Macy return; 122389e0f4d2SKip Macy } 1224ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1225e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 122689e0f4d2SKip Macy NULL); 12279985113bSJustin T. Gibbs if (err == 0 && feature_barrier != 0) 1228e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_BARRIER; 122989e0f4d2SKip Macy 12309985113bSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 12319985113bSJustin T. Gibbs "feature-flush-cache", "%lu", &feature_flush, 12329985113bSJustin T. Gibbs NULL); 12339985113bSJustin T. Gibbs if (err == 0 && feature_flush != 0) 12349985113bSJustin T. Gibbs sc->xbd_flags |= XBDF_FLUSH; 12359985113bSJustin T. Gibbs 1236aaebf690SColin Percival err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1237aaebf690SColin Percival "feature-max-indirect-segments", "%" PRIu32, 1238aaebf690SColin Percival &sc->xbd_max_request_segments, NULL); 1239aaebf690SColin Percival if ((err != 0) || (xbd_enable_indirect == 0)) 1240aaebf690SColin Percival sc->xbd_max_request_segments = 0; 1241aaebf690SColin Percival if (sc->xbd_max_request_segments > XBD_MAX_INDIRECT_SEGMENTS) 1242aaebf690SColin Percival sc->xbd_max_request_segments = XBD_MAX_INDIRECT_SEGMENTS; 1243aaebf690SColin Percival if (sc->xbd_max_request_segments > XBD_SIZE_TO_SEGS(MAXPHYS)) 1244aaebf690SColin Percival sc->xbd_max_request_segments = XBD_SIZE_TO_SEGS(MAXPHYS); 1245aaebf690SColin Percival sc->xbd_max_request_indirectpages = 1246aaebf690SColin Percival XBD_INDIRECT_SEGS_TO_PAGES(sc->xbd_max_request_segments); 1247aaebf690SColin Percival if (sc->xbd_max_request_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) 1248aaebf690SColin Percival sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; 1249aaebf690SColin Percival sc->xbd_max_request_size = 1250aaebf690SColin Percival XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments); 1251aaebf690SColin Percival 125291fb36cfSColin Percival /* Allocate datastructures based on negotiated values. */ 125391fb36cfSColin Percival err = bus_dma_tag_create( 125491fb36cfSColin Percival bus_get_dma_tag(sc->xbd_dev), /* parent */ 125591fb36cfSColin Percival 512, PAGE_SIZE, /* algnmnt, boundary */ 125691fb36cfSColin Percival BUS_SPACE_MAXADDR, /* lowaddr */ 125791fb36cfSColin Percival BUS_SPACE_MAXADDR, /* highaddr */ 125891fb36cfSColin Percival NULL, NULL, /* filter, filterarg */ 125991fb36cfSColin Percival sc->xbd_max_request_size, 126091fb36cfSColin Percival sc->xbd_max_request_segments, 126191fb36cfSColin Percival PAGE_SIZE, /* maxsegsize */ 126291fb36cfSColin Percival BUS_DMA_ALLOCNOW, /* flags */ 126391fb36cfSColin Percival busdma_lock_mutex, /* lockfunc */ 126491fb36cfSColin Percival &sc->xbd_io_lock, /* lockarg */ 126591fb36cfSColin Percival &sc->xbd_io_dmat); 126691fb36cfSColin Percival if (err != 0) { 126791fb36cfSColin Percival xenbus_dev_fatal(sc->xbd_dev, err, 126891fb36cfSColin Percival "Cannot allocate parent DMA tag\n"); 126991fb36cfSColin Percival return; 127091fb36cfSColin Percival } 127191fb36cfSColin Percival 127291fb36cfSColin Percival /* Per-transaction data allocation. */ 127391fb36cfSColin Percival sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests, 127491fb36cfSColin Percival M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 127591fb36cfSColin Percival if (sc->xbd_shadow == NULL) { 127691fb36cfSColin Percival bus_dma_tag_destroy(sc->xbd_io_dmat); 127791fb36cfSColin Percival xenbus_dev_fatal(sc->xbd_dev, ENOMEM, 127891fb36cfSColin Percival "Cannot allocate request structures\n"); 127991fb36cfSColin Percival return; 128091fb36cfSColin Percival } 128191fb36cfSColin Percival 128291fb36cfSColin Percival for (i = 0; i < sc->xbd_max_requests; i++) { 128391fb36cfSColin Percival struct xbd_command *cm; 1284aaebf690SColin Percival void * indirectpages; 128591fb36cfSColin Percival 128691fb36cfSColin Percival cm = &sc->xbd_shadow[i]; 128791fb36cfSColin Percival cm->cm_sg_refs = malloc( 128891fb36cfSColin Percival sizeof(grant_ref_t) * sc->xbd_max_request_segments, 128991fb36cfSColin Percival M_XENBLOCKFRONT, M_NOWAIT); 129091fb36cfSColin Percival if (cm->cm_sg_refs == NULL) 129191fb36cfSColin Percival break; 129291fb36cfSColin Percival cm->cm_id = i; 129391fb36cfSColin Percival cm->cm_flags = XBDCF_INITIALIZER; 129491fb36cfSColin Percival cm->cm_sc = sc; 129591fb36cfSColin Percival if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0) 129691fb36cfSColin Percival break; 1297aaebf690SColin Percival if (sc->xbd_max_request_indirectpages > 0) { 1298aaebf690SColin Percival indirectpages = contigmalloc( 1299aaebf690SColin Percival PAGE_SIZE * sc->xbd_max_request_indirectpages, 1300aaebf690SColin Percival M_XENBLOCKFRONT, M_ZERO, 0, ~0, PAGE_SIZE, 0); 1301aaebf690SColin Percival } else { 1302aaebf690SColin Percival indirectpages = NULL; 1303aaebf690SColin Percival } 1304aaebf690SColin Percival for (j = 0; j < sc->xbd_max_request_indirectpages; j++) { 1305aaebf690SColin Percival if (gnttab_grant_foreign_access( 1306aaebf690SColin Percival xenbus_get_otherend_id(sc->xbd_dev), 1307*3c790178SJohn Baldwin (vtophys(indirectpages) >> PAGE_SHIFT) + j, 1308aaebf690SColin Percival 1 /* grant read-only access */, 1309aaebf690SColin Percival &cm->cm_indirectionrefs[j])) 1310aaebf690SColin Percival break; 1311aaebf690SColin Percival } 1312aaebf690SColin Percival if (j < sc->xbd_max_request_indirectpages) 1313aaebf690SColin Percival break; 1314aaebf690SColin Percival cm->cm_indirectionpages = indirectpages; 131591fb36cfSColin Percival xbd_free_command(cm); 131691fb36cfSColin Percival } 131791fb36cfSColin Percival 131833eebb6aSJustin T. Gibbs if (sc->xbd_disk == NULL) { 131923dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 132023dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 132123dc5621SKip Macy device_get_desc(dev), 132223dc5621SKip Macy xenbus_get_node(dev)); 132323dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 132489e0f4d2SKip Macy 132533eebb6aSJustin T. Gibbs xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo, 132633eebb6aSJustin T. Gibbs sector_size); 132706a630f6SJustin T. Gibbs } 132823dc5621SKip Macy 132923dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 133089e0f4d2SKip Macy 133189e0f4d2SKip Macy /* Kick pending requests. */ 133233eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1333e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_CONNECTED; 133433eebb6aSJustin T. Gibbs xbd_startio(sc); 1335e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_READY; 133633eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 133789e0f4d2SKip Macy } 133889e0f4d2SKip Macy 133989e0f4d2SKip Macy /** 134089e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 134189e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 1342a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 134389e0f4d2SKip Macy * acknowledgement. 134489e0f4d2SKip Macy */ 134523dc5621SKip Macy static void 134633eebb6aSJustin T. Gibbs xbd_closing(device_t dev) 134789e0f4d2SKip Macy { 134833eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 134989e0f4d2SKip Macy 1350ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateClosing); 1351ff662b5cSJustin T. Gibbs 135233eebb6aSJustin T. Gibbs DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev)); 135389e0f4d2SKip Macy 135433eebb6aSJustin T. Gibbs if (sc->xbd_disk != NULL) { 135533eebb6aSJustin T. Gibbs disk_destroy(sc->xbd_disk); 135633eebb6aSJustin T. Gibbs sc->xbd_disk = NULL; 135789e0f4d2SKip Macy } 135889e0f4d2SKip Macy 135923dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 136089e0f4d2SKip Macy } 136189e0f4d2SKip Macy 1362fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/ 1363fac3fd80SJustin T. Gibbs static int 1364fac3fd80SJustin T. Gibbs xbd_probe(device_t dev) 1365fac3fd80SJustin T. Gibbs { 1366a371f519SJustin T. Gibbs if (strcmp(xenbus_get_type(dev), "vbd") != 0) 1367a371f519SJustin T. Gibbs return (ENXIO); 1368fac3fd80SJustin T. Gibbs 1369a371f519SJustin T. Gibbs if (xen_hvm_domain()) { 1370a371f519SJustin T. Gibbs int error; 1371a371f519SJustin T. Gibbs char *type; 1372a371f519SJustin T. Gibbs 1373a371f519SJustin T. Gibbs /* 1374a371f519SJustin T. Gibbs * When running in an HVM domain, IDE disk emulation is 1375a371f519SJustin T. Gibbs * disabled early in boot so that native drivers will 1376a371f519SJustin T. Gibbs * not see emulated hardware. However, CDROM device 1377a371f519SJustin T. Gibbs * emulation cannot be disabled. 1378a371f519SJustin T. Gibbs * 1379a371f519SJustin T. Gibbs * Through use of FreeBSD's vm_guest and xen_hvm_domain() 1380a371f519SJustin T. Gibbs * APIs, we could modify the native CDROM driver to fail its 1381a371f519SJustin T. Gibbs * probe when running under Xen. Unfortunatlely, the PV 1382a371f519SJustin T. Gibbs * CDROM support in XenServer (up through at least version 1383a371f519SJustin T. Gibbs * 6.2) isn't functional, so we instead rely on the emulated 1384a371f519SJustin T. Gibbs * CDROM instance, and fail to attach the PV one here in 1385a371f519SJustin T. Gibbs * the blkfront driver. 1386a371f519SJustin T. Gibbs */ 1387a371f519SJustin T. Gibbs error = xs_read(XST_NIL, xenbus_get_node(dev), 1388a371f519SJustin T. Gibbs "device-type", NULL, (void **) &type); 1389a371f519SJustin T. Gibbs if (error) 1390a371f519SJustin T. Gibbs return (ENXIO); 1391a371f519SJustin T. Gibbs 1392a371f519SJustin T. Gibbs if (strncmp(type, "cdrom", 5) == 0) { 1393a371f519SJustin T. Gibbs free(type, M_XENSTORE); 1394a371f519SJustin T. Gibbs return (ENXIO); 1395a371f519SJustin T. Gibbs } 1396a371f519SJustin T. Gibbs free(type, M_XENSTORE); 1397a371f519SJustin T. Gibbs } 1398a371f519SJustin T. Gibbs 1399fac3fd80SJustin T. Gibbs device_set_desc(dev, "Virtual Block Device"); 1400fac3fd80SJustin T. Gibbs device_quiet(dev); 1401fac3fd80SJustin T. Gibbs return (0); 1402fac3fd80SJustin T. Gibbs } 1403fac3fd80SJustin T. Gibbs 1404fac3fd80SJustin T. Gibbs /* 1405fac3fd80SJustin T. Gibbs * Setup supplies the backend dir, virtual device. We place an event 1406fac3fd80SJustin T. Gibbs * channel and shared frame entries. We watch backend to wait if it's 1407fac3fd80SJustin T. Gibbs * ok. 1408fac3fd80SJustin T. Gibbs */ 1409fac3fd80SJustin T. Gibbs static int 1410fac3fd80SJustin T. Gibbs xbd_attach(device_t dev) 1411fac3fd80SJustin T. Gibbs { 1412fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 1413fac3fd80SJustin T. Gibbs const char *name; 1414fac3fd80SJustin T. Gibbs uint32_t vdevice; 1415fac3fd80SJustin T. Gibbs int error; 1416fac3fd80SJustin T. Gibbs int i; 1417fac3fd80SJustin T. Gibbs int unit; 1418fac3fd80SJustin T. Gibbs 1419fac3fd80SJustin T. Gibbs /* FIXME: Use dynamic device id if this is not set. */ 1420fac3fd80SJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_node(dev), 1421fac3fd80SJustin T. Gibbs "virtual-device", NULL, "%" PRIu32, &vdevice); 142248a1ceedSColin Percival if (error) 142348a1ceedSColin Percival error = xs_scanf(XST_NIL, xenbus_get_node(dev), 142448a1ceedSColin Percival "virtual-device-ext", NULL, "%" PRIu32, &vdevice); 1425fac3fd80SJustin T. Gibbs if (error) { 1426fac3fd80SJustin T. Gibbs xenbus_dev_fatal(dev, error, "reading virtual-device"); 1427fac3fd80SJustin T. Gibbs device_printf(dev, "Couldn't determine virtual device.\n"); 1428fac3fd80SJustin T. Gibbs return (error); 1429fac3fd80SJustin T. Gibbs } 1430fac3fd80SJustin T. Gibbs 1431fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 1432fac3fd80SJustin T. Gibbs if (!strcmp(name, "xbd")) 1433fac3fd80SJustin T. Gibbs device_set_unit(dev, unit); 1434fac3fd80SJustin T. Gibbs 1435fac3fd80SJustin T. Gibbs sc = device_get_softc(dev); 1436fac3fd80SJustin T. Gibbs mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 1437e2c1fe90SJustin T. Gibbs xbd_initqs(sc); 1438fac3fd80SJustin T. Gibbs for (i = 0; i < XBD_MAX_RING_PAGES; i++) 1439d9fab01dSJustin T. Gibbs sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 1440fac3fd80SJustin T. Gibbs 1441fac3fd80SJustin T. Gibbs sc->xbd_dev = dev; 1442fac3fd80SJustin T. Gibbs sc->xbd_vdevice = vdevice; 1443e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_DISCONNECTED; 1444fac3fd80SJustin T. Gibbs 1445fac3fd80SJustin T. Gibbs xbd_setup_sysctl(sc); 1446fac3fd80SJustin T. Gibbs 1447fac3fd80SJustin T. Gibbs /* Wait for backend device to publish its protocol capabilities. */ 1448fac3fd80SJustin T. Gibbs xenbus_set_state(dev, XenbusStateInitialising); 1449fac3fd80SJustin T. Gibbs 1450fac3fd80SJustin T. Gibbs return (0); 1451fac3fd80SJustin T. Gibbs } 145289e0f4d2SKip Macy 145323dc5621SKip Macy static int 145433eebb6aSJustin T. Gibbs xbd_detach(device_t dev) 145589e0f4d2SKip Macy { 145633eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 145789e0f4d2SKip Macy 1458e2c1fe90SJustin T. Gibbs DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev)); 145989e0f4d2SKip Macy 146033eebb6aSJustin T. Gibbs xbd_free(sc); 146133eebb6aSJustin T. Gibbs mtx_destroy(&sc->xbd_io_lock); 146289e0f4d2SKip Macy 146389e0f4d2SKip Macy return 0; 146489e0f4d2SKip Macy } 146589e0f4d2SKip Macy 146689e0f4d2SKip Macy static int 1467fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev) 146889e0f4d2SKip Macy { 1469fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1470fac3fd80SJustin T. Gibbs int retval; 1471fac3fd80SJustin T. Gibbs int saved_state; 147289e0f4d2SKip Macy 147389e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 147433eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1475e2c1fe90SJustin T. Gibbs saved_state = sc->xbd_state; 1476e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_SUSPENDED; 1477fac3fd80SJustin T. Gibbs 1478fac3fd80SJustin T. Gibbs /* Wait for outstanding I/O to drain. */ 1479fac3fd80SJustin T. Gibbs retval = 0; 14809985113bSJustin T. Gibbs while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 1481e2c1fe90SJustin T. Gibbs if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock, 1482fac3fd80SJustin T. Gibbs PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) { 1483fac3fd80SJustin T. Gibbs retval = EBUSY; 1484fac3fd80SJustin T. Gibbs break; 1485fac3fd80SJustin T. Gibbs } 1486fac3fd80SJustin T. Gibbs } 148733eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 148889e0f4d2SKip Macy 1489fac3fd80SJustin T. Gibbs if (retval != 0) 1490e2c1fe90SJustin T. Gibbs sc->xbd_state = saved_state; 149133eebb6aSJustin T. Gibbs 1492fac3fd80SJustin T. Gibbs return (retval); 149389e0f4d2SKip Macy } 149489e0f4d2SKip Macy 1495ff662b5cSJustin T. Gibbs static int 1496fac3fd80SJustin T. Gibbs xbd_resume(device_t dev) 149789e0f4d2SKip Macy { 1498fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1499fac3fd80SJustin T. Gibbs 1500fac3fd80SJustin T. Gibbs DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev)); 1501fac3fd80SJustin T. Gibbs 1502fac3fd80SJustin T. Gibbs xbd_free(sc); 1503fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1504fac3fd80SJustin T. Gibbs return (0); 150589e0f4d2SKip Macy } 150689e0f4d2SKip Macy 1507fac3fd80SJustin T. Gibbs /** 1508fac3fd80SJustin T. Gibbs * Callback received when the backend's state changes. 1509fac3fd80SJustin T. Gibbs */ 1510fac3fd80SJustin T. Gibbs static void 1511fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state) 1512fac3fd80SJustin T. Gibbs { 1513fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1514fac3fd80SJustin T. Gibbs 1515fac3fd80SJustin T. Gibbs DPRINTK("backend_state=%d\n", backend_state); 1516fac3fd80SJustin T. Gibbs 1517fac3fd80SJustin T. Gibbs switch (backend_state) { 1518fac3fd80SJustin T. Gibbs case XenbusStateUnknown: 1519fac3fd80SJustin T. Gibbs case XenbusStateInitialising: 1520fac3fd80SJustin T. Gibbs case XenbusStateReconfigured: 1521fac3fd80SJustin T. Gibbs case XenbusStateReconfiguring: 1522fac3fd80SJustin T. Gibbs case XenbusStateClosed: 1523fac3fd80SJustin T. Gibbs break; 1524fac3fd80SJustin T. Gibbs 1525fac3fd80SJustin T. Gibbs case XenbusStateInitWait: 1526fac3fd80SJustin T. Gibbs case XenbusStateInitialised: 1527fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1528fac3fd80SJustin T. Gibbs break; 1529fac3fd80SJustin T. Gibbs 1530fac3fd80SJustin T. Gibbs case XenbusStateConnected: 1531fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1532fac3fd80SJustin T. Gibbs xbd_connect(sc); 1533fac3fd80SJustin T. Gibbs break; 1534fac3fd80SJustin T. Gibbs 1535fac3fd80SJustin T. Gibbs case XenbusStateClosing: 1536fac3fd80SJustin T. Gibbs if (sc->xbd_users > 0) 1537fac3fd80SJustin T. Gibbs xenbus_dev_error(dev, -EBUSY, 1538fac3fd80SJustin T. Gibbs "Device in use; refusing to close"); 1539fac3fd80SJustin T. Gibbs else 1540fac3fd80SJustin T. Gibbs xbd_closing(dev); 1541fac3fd80SJustin T. Gibbs break; 1542fac3fd80SJustin T. Gibbs } 1543fac3fd80SJustin T. Gibbs } 1544fac3fd80SJustin T. Gibbs 1545fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/ 154633eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = { 154723dc5621SKip Macy /* Device interface */ 154833eebb6aSJustin T. Gibbs DEVMETHOD(device_probe, xbd_probe), 154933eebb6aSJustin T. Gibbs DEVMETHOD(device_attach, xbd_attach), 155033eebb6aSJustin T. Gibbs DEVMETHOD(device_detach, xbd_detach), 155123dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 155233eebb6aSJustin T. Gibbs DEVMETHOD(device_suspend, xbd_suspend), 155333eebb6aSJustin T. Gibbs DEVMETHOD(device_resume, xbd_resume), 155489e0f4d2SKip Macy 155523dc5621SKip Macy /* Xenbus interface */ 155633eebb6aSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed), 155789e0f4d2SKip Macy 155823dc5621SKip Macy { 0, 0 } 155989e0f4d2SKip Macy }; 156089e0f4d2SKip Macy 156133eebb6aSJustin T. Gibbs static driver_t xbd_driver = { 156223dc5621SKip Macy "xbd", 156333eebb6aSJustin T. Gibbs xbd_methods, 156433eebb6aSJustin T. Gibbs sizeof(struct xbd_softc), 156589e0f4d2SKip Macy }; 156633eebb6aSJustin T. Gibbs devclass_t xbd_devclass; 156789e0f4d2SKip Macy 156833eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0); 1569