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 54ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h> 5589e0f4d2SKip Macy #include <machine/xen/xen-os.h> 562913e88cSRobert Watson #include <machine/xen/xenvar.h> 5712678024SDoug Rabson #include <machine/xen/xenfunc.h> 58ff662b5cSJustin T. Gibbs 5912678024SDoug Rabson #include <xen/hypervisor.h> 603a6d1fcfSKip Macy #include <xen/xen_intr.h> 613a6d1fcfSKip Macy #include <xen/evtchn.h> 6212678024SDoug Rabson #include <xen/gnttab.h> 6389e0f4d2SKip Macy #include <xen/interface/grant_table.h> 6423dc5621SKip Macy #include <xen/interface/io/protocols.h> 6523dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 6689e0f4d2SKip Macy 6789e0f4d2SKip Macy #include <geom/geom_disk.h> 6889e0f4d2SKip Macy 6989e0f4d2SKip Macy #include <dev/xen/blkfront/block.h> 7089e0f4d2SKip Macy 7123dc5621SKip Macy #include "xenbus_if.h" 7223dc5621SKip Macy 73*fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/ 7433eebb6aSJustin T. Gibbs static void xbd_closing(device_t); 75*fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc); 7689e0f4d2SKip Macy 77*fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/ 78*fac3fd80SJustin T. Gibbs #if 0 79*fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 80*fac3fd80SJustin T. Gibbs #else 81*fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) 82*fac3fd80SJustin T. Gibbs #endif 83*fac3fd80SJustin T. Gibbs 84*fac3fd80SJustin T. Gibbs /* XXX move to xbd_vbd.c when VBD update support is added */ 85*fac3fd80SJustin T. Gibbs #define MAX_VBDS 64 86*fac3fd80SJustin T. Gibbs 87*fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */ 88*fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT 9 89ff662b5cSJustin T. Gibbs 9089e0f4d2SKip Macy #define GRANT_INVALID_REF 0 9189e0f4d2SKip Macy 9289e0f4d2SKip Macy /* Control whether runtime update of vbds is enabled. */ 9389e0f4d2SKip Macy #define ENABLE_VBD_UPDATE 0 9489e0f4d2SKip Macy 9589e0f4d2SKip Macy #if ENABLE_VBD_UPDATE 9689e0f4d2SKip Macy static void vbd_update(void); 9789e0f4d2SKip Macy #endif 9889e0f4d2SKip Macy 9933eebb6aSJustin T. Gibbs #define XBD_STATE_DISCONNECTED 0 10033eebb6aSJustin T. Gibbs #define XBD_STATE_CONNECTED 1 10133eebb6aSJustin T. Gibbs #define XBD_STATE_SUSPENDED 2 10289e0f4d2SKip Macy 103*fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/ 104*fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 10589e0f4d2SKip Macy 106*fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/ 107*fac3fd80SJustin T. Gibbs static inline void 108*fac3fd80SJustin T. Gibbs flush_requests(struct xbd_softc *sc) 109*fac3fd80SJustin T. Gibbs { 110*fac3fd80SJustin T. Gibbs int notify; 111e4808c4bSKip Macy 112*fac3fd80SJustin T. Gibbs RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify); 11389e0f4d2SKip Macy 114*fac3fd80SJustin T. Gibbs if (notify) 115*fac3fd80SJustin T. Gibbs notify_remote_via_irq(sc->xbd_irq); 116*fac3fd80SJustin T. Gibbs } 11789e0f4d2SKip Macy 11823dc5621SKip Macy static void 119*fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm) 12023dc5621SKip Macy { 12123dc5621SKip Macy 122*fac3fd80SJustin T. Gibbs KASSERT((cm->cm_flags & XBD_ON_XBDQ_MASK) == 0, 123*fac3fd80SJustin T. Gibbs ("Freeing command that is still on a queue\n")); 12423dc5621SKip Macy 125*fac3fd80SJustin T. Gibbs cm->cm_flags = 0; 126*fac3fd80SJustin T. Gibbs cm->cm_bp = NULL; 127*fac3fd80SJustin T. Gibbs cm->cm_complete = NULL; 128*fac3fd80SJustin T. Gibbs xbd_enqueue_free(cm); 12923dc5621SKip Macy } 13023dc5621SKip Macy 13189e0f4d2SKip Macy static void 132*fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 13389e0f4d2SKip Macy { 134*fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 135*fac3fd80SJustin T. Gibbs struct xbd_command *cm; 136*fac3fd80SJustin T. Gibbs blkif_request_t *ring_req; 137*fac3fd80SJustin T. Gibbs struct blkif_request_segment *sg; 138*fac3fd80SJustin T. Gibbs struct blkif_request_segment *last_block_sg; 139*fac3fd80SJustin T. Gibbs grant_ref_t *sg_ref; 140*fac3fd80SJustin T. Gibbs vm_paddr_t buffer_ma; 141*fac3fd80SJustin T. Gibbs uint64_t fsect, lsect; 142*fac3fd80SJustin T. Gibbs int ref; 143*fac3fd80SJustin T. Gibbs int op; 144*fac3fd80SJustin T. Gibbs int block_segs; 14589e0f4d2SKip Macy 146*fac3fd80SJustin T. Gibbs cm = arg; 147*fac3fd80SJustin T. Gibbs sc = cm->cm_sc; 148*fac3fd80SJustin T. Gibbs 149*fac3fd80SJustin T. Gibbs if (error) { 150*fac3fd80SJustin T. Gibbs printf("error %d in xbd_queue_cb\n", error); 151*fac3fd80SJustin T. Gibbs cm->cm_bp->bio_error = EIO; 152*fac3fd80SJustin T. Gibbs biodone(cm->cm_bp); 153*fac3fd80SJustin T. Gibbs xbd_free_command(cm); 15489e0f4d2SKip Macy return; 15589e0f4d2SKip Macy } 15689e0f4d2SKip Macy 157*fac3fd80SJustin T. Gibbs /* Fill out a communications ring structure. */ 158*fac3fd80SJustin T. Gibbs ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 159*fac3fd80SJustin T. Gibbs sc->xbd_ring.req_prod_pvt++; 160*fac3fd80SJustin T. Gibbs ring_req->id = cm->cm_id; 161*fac3fd80SJustin T. Gibbs ring_req->operation = cm->cm_operation; 162*fac3fd80SJustin T. Gibbs ring_req->sector_number = cm->cm_sector_number; 163*fac3fd80SJustin T. Gibbs ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 164*fac3fd80SJustin T. Gibbs ring_req->nr_segments = nsegs; 165*fac3fd80SJustin T. Gibbs cm->cm_nseg = nsegs; 166*fac3fd80SJustin T. Gibbs 167*fac3fd80SJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK); 168*fac3fd80SJustin T. Gibbs sg = ring_req->seg; 169*fac3fd80SJustin T. Gibbs last_block_sg = sg + block_segs; 170*fac3fd80SJustin T. Gibbs sg_ref = cm->cm_sg_refs; 171*fac3fd80SJustin T. Gibbs 172*fac3fd80SJustin T. Gibbs while (1) { 173*fac3fd80SJustin T. Gibbs 174*fac3fd80SJustin T. Gibbs while (sg < last_block_sg) { 175*fac3fd80SJustin T. Gibbs buffer_ma = segs->ds_addr; 176*fac3fd80SJustin T. Gibbs fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 177*fac3fd80SJustin T. Gibbs lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 178*fac3fd80SJustin T. Gibbs 179*fac3fd80SJustin T. Gibbs KASSERT(lsect <= 7, ("XEN disk driver data cannot " 180*fac3fd80SJustin T. Gibbs "cross a page boundary")); 181*fac3fd80SJustin T. Gibbs 182*fac3fd80SJustin T. Gibbs /* install a grant reference. */ 183*fac3fd80SJustin T. Gibbs ref = gnttab_claim_grant_reference(&cm->cm_gref_head); 184*fac3fd80SJustin T. Gibbs 185e4808c4bSKip Macy /* 186*fac3fd80SJustin T. Gibbs * GNTTAB_LIST_END == 0xffffffff, but it is private 187*fac3fd80SJustin T. Gibbs * to gnttab.c. 188e4808c4bSKip Macy */ 189*fac3fd80SJustin T. Gibbs KASSERT(ref != ~0, ("grant_reference failed")); 190*fac3fd80SJustin T. Gibbs 191*fac3fd80SJustin T. Gibbs gnttab_grant_foreign_access_ref( 192*fac3fd80SJustin T. Gibbs ref, 193*fac3fd80SJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), 194*fac3fd80SJustin T. Gibbs buffer_ma >> PAGE_SHIFT, 195*fac3fd80SJustin T. Gibbs ring_req->operation == BLKIF_OP_WRITE); 196*fac3fd80SJustin T. Gibbs 197*fac3fd80SJustin T. Gibbs *sg_ref = ref; 198*fac3fd80SJustin T. Gibbs *sg = (struct blkif_request_segment) { 199*fac3fd80SJustin T. Gibbs .gref = ref, 200*fac3fd80SJustin T. Gibbs .first_sect = fsect, 201*fac3fd80SJustin T. Gibbs .last_sect = lsect 202*fac3fd80SJustin T. Gibbs }; 203*fac3fd80SJustin T. Gibbs sg++; 204*fac3fd80SJustin T. Gibbs sg_ref++; 205*fac3fd80SJustin T. Gibbs segs++; 206*fac3fd80SJustin T. Gibbs nsegs--; 207*fac3fd80SJustin T. Gibbs } 208*fac3fd80SJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK); 209*fac3fd80SJustin T. Gibbs if (block_segs == 0) 210*fac3fd80SJustin T. Gibbs break; 211*fac3fd80SJustin T. Gibbs 212*fac3fd80SJustin T. Gibbs sg = BLKRING_GET_SEG_BLOCK(&sc->xbd_ring, 213*fac3fd80SJustin T. Gibbs sc->xbd_ring.req_prod_pvt); 214*fac3fd80SJustin T. Gibbs sc->xbd_ring.req_prod_pvt++; 215*fac3fd80SJustin T. Gibbs last_block_sg = sg + block_segs; 216*fac3fd80SJustin T. Gibbs } 217*fac3fd80SJustin T. Gibbs 218*fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 219*fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREREAD; 220*fac3fd80SJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE) 221*fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREWRITE; 222*fac3fd80SJustin T. Gibbs else 223*fac3fd80SJustin T. Gibbs op = 0; 224*fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 225*fac3fd80SJustin T. Gibbs 226*fac3fd80SJustin T. Gibbs gnttab_free_grant_references(cm->cm_gref_head); 227*fac3fd80SJustin T. Gibbs 228*fac3fd80SJustin T. Gibbs xbd_enqueue_busy(cm); 229*fac3fd80SJustin T. Gibbs 230*fac3fd80SJustin T. Gibbs /* 231*fac3fd80SJustin T. Gibbs * This flag means that we're probably executing in the busdma swi 232*fac3fd80SJustin T. Gibbs * instead of in the startio context, so an explicit flush is needed. 233*fac3fd80SJustin T. Gibbs */ 234*fac3fd80SJustin T. Gibbs if (cm->cm_flags & XBD_CMD_FROZEN) 235*fac3fd80SJustin T. Gibbs flush_requests(sc); 236*fac3fd80SJustin T. Gibbs 237*fac3fd80SJustin T. Gibbs return; 238*fac3fd80SJustin T. Gibbs } 239*fac3fd80SJustin T. Gibbs 240*fac3fd80SJustin T. Gibbs static int 241*fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm) 242*fac3fd80SJustin T. Gibbs { 243*fac3fd80SJustin T. Gibbs int error; 244*fac3fd80SJustin T. Gibbs 245*fac3fd80SJustin T. Gibbs error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data, 246*fac3fd80SJustin T. Gibbs cm->cm_datalen, xbd_queue_cb, cm, 0); 247*fac3fd80SJustin T. Gibbs if (error == EINPROGRESS) { 248*fac3fd80SJustin T. Gibbs printf("EINPROGRESS\n"); 249*fac3fd80SJustin T. Gibbs sc->xbd_flags |= XBD_FROZEN; 250*fac3fd80SJustin T. Gibbs cm->cm_flags |= XBD_CMD_FROZEN; 251*fac3fd80SJustin T. Gibbs return (0); 252*fac3fd80SJustin T. Gibbs } 253*fac3fd80SJustin T. Gibbs 254*fac3fd80SJustin T. Gibbs return (error); 255*fac3fd80SJustin T. Gibbs } 256*fac3fd80SJustin T. Gibbs 257*fac3fd80SJustin T. Gibbs static void 258*fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg) 259*fac3fd80SJustin T. Gibbs { 260*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = arg; 261*fac3fd80SJustin T. Gibbs 26233eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 263e4808c4bSKip Macy 26433eebb6aSJustin T. Gibbs xbd_startio(sc); 265e4808c4bSKip Macy 26633eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 267*fac3fd80SJustin T. Gibbs } 268*fac3fd80SJustin T. Gibbs 269*fac3fd80SJustin T. Gibbs static struct xbd_command * 270*fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc) 271*fac3fd80SJustin T. Gibbs { 272*fac3fd80SJustin T. Gibbs struct xbd_command *cm; 273*fac3fd80SJustin T. Gibbs struct bio *bp; 274*fac3fd80SJustin T. Gibbs 275*fac3fd80SJustin T. Gibbs if (unlikely(sc->xbd_connected != XBD_STATE_CONNECTED)) 276*fac3fd80SJustin T. Gibbs return (NULL); 277*fac3fd80SJustin T. Gibbs 278*fac3fd80SJustin T. Gibbs bp = xbd_dequeue_bio(sc); 279*fac3fd80SJustin T. Gibbs if (bp == NULL) 280*fac3fd80SJustin T. Gibbs return (NULL); 281*fac3fd80SJustin T. Gibbs 282*fac3fd80SJustin T. Gibbs if ((cm = xbd_dequeue_free(sc)) == NULL) { 283*fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 284*fac3fd80SJustin T. Gibbs return (NULL); 285*fac3fd80SJustin T. Gibbs } 286*fac3fd80SJustin T. Gibbs 287*fac3fd80SJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 288*fac3fd80SJustin T. Gibbs &cm->cm_gref_head) != 0) { 289*fac3fd80SJustin T. Gibbs gnttab_request_free_callback(&sc->xbd_callback, 290*fac3fd80SJustin T. Gibbs xbd_restart_queue_callback, sc, 291*fac3fd80SJustin T. Gibbs sc->xbd_max_request_segments); 292*fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 293*fac3fd80SJustin T. Gibbs xbd_enqueue_free(cm); 294*fac3fd80SJustin T. Gibbs sc->xbd_flags |= XBD_FROZEN; 295*fac3fd80SJustin T. Gibbs return (NULL); 296*fac3fd80SJustin T. Gibbs } 297*fac3fd80SJustin T. Gibbs 298*fac3fd80SJustin T. Gibbs cm->cm_bp = bp; 299*fac3fd80SJustin T. Gibbs cm->cm_data = bp->bio_data; 300*fac3fd80SJustin T. Gibbs cm->cm_datalen = bp->bio_bcount; 301*fac3fd80SJustin T. Gibbs cm->cm_operation = (bp->bio_cmd == BIO_READ) ? 302*fac3fd80SJustin T. Gibbs BLKIF_OP_READ : BLKIF_OP_WRITE; 303*fac3fd80SJustin T. Gibbs cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno; 304*fac3fd80SJustin T. Gibbs 305*fac3fd80SJustin T. Gibbs return (cm); 306*fac3fd80SJustin T. Gibbs } 307*fac3fd80SJustin T. Gibbs 308*fac3fd80SJustin T. Gibbs /* 309*fac3fd80SJustin T. Gibbs * Dequeue buffers and place them in the shared communication ring. 310*fac3fd80SJustin T. Gibbs * Return when no more requests can be accepted or all buffers have 311*fac3fd80SJustin T. Gibbs * been queued. 312*fac3fd80SJustin T. Gibbs * 313*fac3fd80SJustin T. Gibbs * Signal XEN once the ring has been filled out. 314*fac3fd80SJustin T. Gibbs */ 315*fac3fd80SJustin T. Gibbs static void 316*fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc) 317*fac3fd80SJustin T. Gibbs { 318*fac3fd80SJustin T. Gibbs struct xbd_command *cm; 319*fac3fd80SJustin T. Gibbs int error, queued = 0; 320*fac3fd80SJustin T. Gibbs 321*fac3fd80SJustin T. Gibbs mtx_assert(&sc->xbd_io_lock, MA_OWNED); 322*fac3fd80SJustin T. Gibbs 323*fac3fd80SJustin T. Gibbs if (sc->xbd_connected != XBD_STATE_CONNECTED) 324e4808c4bSKip Macy return; 325*fac3fd80SJustin T. Gibbs 326*fac3fd80SJustin T. Gibbs while (RING_FREE_REQUESTS(&sc->xbd_ring) >= 327*fac3fd80SJustin T. Gibbs sc->xbd_max_request_blocks) { 328*fac3fd80SJustin T. Gibbs if (sc->xbd_flags & XBD_FROZEN) 329*fac3fd80SJustin T. Gibbs break; 330*fac3fd80SJustin T. Gibbs 331*fac3fd80SJustin T. Gibbs cm = xbd_dequeue_ready(sc); 332*fac3fd80SJustin T. Gibbs 333*fac3fd80SJustin T. Gibbs if (cm == NULL) 334*fac3fd80SJustin T. Gibbs cm = xbd_bio_command(sc); 335*fac3fd80SJustin T. Gibbs 336*fac3fd80SJustin T. Gibbs if (cm == NULL) 337*fac3fd80SJustin T. Gibbs break; 338*fac3fd80SJustin T. Gibbs 339*fac3fd80SJustin T. Gibbs if ((error = xbd_queue_request(sc, cm)) != 0) { 340*fac3fd80SJustin T. Gibbs printf("xbd_queue_request returned %d\n", error); 341*fac3fd80SJustin T. Gibbs break; 342*fac3fd80SJustin T. Gibbs } 343*fac3fd80SJustin T. Gibbs queued++; 344*fac3fd80SJustin T. Gibbs } 345*fac3fd80SJustin T. Gibbs 346*fac3fd80SJustin T. Gibbs if (queued != 0) 347*fac3fd80SJustin T. Gibbs flush_requests(sc); 348e4808c4bSKip Macy } 349e4808c4bSKip Macy 350e4808c4bSKip Macy static void 35133eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm) 352e4808c4bSKip Macy { 353e4808c4bSKip Macy struct bio *bp; 354e4808c4bSKip Macy 35533eebb6aSJustin T. Gibbs bp = cm->cm_bp; 356e4808c4bSKip Macy 35733eebb6aSJustin T. Gibbs if (unlikely(cm->cm_status != BLKIF_RSP_OKAY)) { 358e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 35933eebb6aSJustin T. Gibbs printf(" status: %x\n", cm->cm_status); 360e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 361e4808c4bSKip Macy } 362e4808c4bSKip Macy 363e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 364e4808c4bSKip Macy bp->bio_error = EIO; 365e4808c4bSKip Macy else 366e4808c4bSKip Macy bp->bio_resid = 0; 367e4808c4bSKip Macy 36833eebb6aSJustin T. Gibbs xbd_free_command(cm); 369e4808c4bSKip Macy biodone(bp); 370e4808c4bSKip Macy } 371e4808c4bSKip Macy 372*fac3fd80SJustin T. Gibbs static int 373*fac3fd80SJustin T. Gibbs xbd_completion(struct xbd_command *cm) 374*fac3fd80SJustin T. Gibbs { 375*fac3fd80SJustin T. Gibbs gnttab_end_foreign_access_references(cm->cm_nseg, cm->cm_sg_refs); 376*fac3fd80SJustin T. Gibbs return (BLKIF_SEGS_TO_BLOCKS(cm->cm_nseg)); 377*fac3fd80SJustin T. Gibbs } 378*fac3fd80SJustin T. Gibbs 379*fac3fd80SJustin T. Gibbs static void 380*fac3fd80SJustin T. Gibbs xbd_int(void *xsc) 381*fac3fd80SJustin T. Gibbs { 382*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = xsc; 383*fac3fd80SJustin T. Gibbs struct xbd_command *cm; 384*fac3fd80SJustin T. Gibbs blkif_response_t *bret; 385*fac3fd80SJustin T. Gibbs RING_IDX i, rp; 386*fac3fd80SJustin T. Gibbs int op; 387*fac3fd80SJustin T. Gibbs 388*fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 389*fac3fd80SJustin T. Gibbs 390*fac3fd80SJustin T. Gibbs if (unlikely(sc->xbd_connected == XBD_STATE_DISCONNECTED)) { 391*fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 392*fac3fd80SJustin T. Gibbs return; 393*fac3fd80SJustin T. Gibbs } 394*fac3fd80SJustin T. Gibbs 395*fac3fd80SJustin T. Gibbs again: 396*fac3fd80SJustin T. Gibbs rp = sc->xbd_ring.sring->rsp_prod; 397*fac3fd80SJustin T. Gibbs rmb(); /* Ensure we see queued responses up to 'rp'. */ 398*fac3fd80SJustin T. Gibbs 399*fac3fd80SJustin T. Gibbs for (i = sc->xbd_ring.rsp_cons; i != rp;) { 400*fac3fd80SJustin T. Gibbs bret = RING_GET_RESPONSE(&sc->xbd_ring, i); 401*fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[bret->id]; 402*fac3fd80SJustin T. Gibbs 403*fac3fd80SJustin T. Gibbs xbd_remove_busy(cm); 404*fac3fd80SJustin T. Gibbs i += xbd_completion(cm); 405*fac3fd80SJustin T. Gibbs 406*fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 407*fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTREAD; 408*fac3fd80SJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE) 409*fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTWRITE; 410*fac3fd80SJustin T. Gibbs else 411*fac3fd80SJustin T. Gibbs op = 0; 412*fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 413*fac3fd80SJustin T. Gibbs bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map); 414*fac3fd80SJustin T. Gibbs 415*fac3fd80SJustin T. Gibbs /* 416*fac3fd80SJustin T. Gibbs * If commands are completing then resources are probably 417*fac3fd80SJustin T. Gibbs * being freed as well. It's a cheap assumption even when 418*fac3fd80SJustin T. Gibbs * wrong. 419*fac3fd80SJustin T. Gibbs */ 420*fac3fd80SJustin T. Gibbs sc->xbd_flags &= ~XBD_FROZEN; 421*fac3fd80SJustin T. Gibbs 422*fac3fd80SJustin T. Gibbs /* 423*fac3fd80SJustin T. Gibbs * Directly call the i/o complete routine to save an 424*fac3fd80SJustin T. Gibbs * an indirection in the common case. 425*fac3fd80SJustin T. Gibbs */ 426*fac3fd80SJustin T. Gibbs cm->cm_status = bret->status; 427*fac3fd80SJustin T. Gibbs if (cm->cm_bp) 428*fac3fd80SJustin T. Gibbs xbd_bio_complete(sc, cm); 429*fac3fd80SJustin T. Gibbs else if (cm->cm_complete != NULL) 430*fac3fd80SJustin T. Gibbs cm->cm_complete(cm); 431*fac3fd80SJustin T. Gibbs else 432*fac3fd80SJustin T. Gibbs xbd_free_command(cm); 433*fac3fd80SJustin T. Gibbs } 434*fac3fd80SJustin T. Gibbs 435*fac3fd80SJustin T. Gibbs sc->xbd_ring.rsp_cons = i; 436*fac3fd80SJustin T. Gibbs 437*fac3fd80SJustin T. Gibbs if (i != sc->xbd_ring.req_prod_pvt) { 438*fac3fd80SJustin T. Gibbs int more_to_do; 439*fac3fd80SJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do); 440*fac3fd80SJustin T. Gibbs if (more_to_do) 441*fac3fd80SJustin T. Gibbs goto again; 442*fac3fd80SJustin T. Gibbs } else { 443*fac3fd80SJustin T. Gibbs sc->xbd_ring.sring->rsp_event = i + 1; 444*fac3fd80SJustin T. Gibbs } 445*fac3fd80SJustin T. Gibbs 446*fac3fd80SJustin T. Gibbs xbd_startio(sc); 447*fac3fd80SJustin T. Gibbs 448*fac3fd80SJustin T. Gibbs if (unlikely(sc->xbd_connected == XBD_STATE_SUSPENDED)) 449*fac3fd80SJustin T. Gibbs wakeup(&sc->xbd_cm_busy); 450*fac3fd80SJustin T. Gibbs 451*fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 452*fac3fd80SJustin T. Gibbs } 453*fac3fd80SJustin T. Gibbs 454*fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/ 455*fac3fd80SJustin T. Gibbs /** 456*fac3fd80SJustin T. Gibbs * Quiesce the disk writes for a dump file before allowing the next buffer. 457*fac3fd80SJustin T. Gibbs */ 4588698b76cSKip Macy static void 45933eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc) 4608698b76cSKip Macy { 4618698b76cSKip Macy int mtd; 4628698b76cSKip Macy 4638698b76cSKip Macy // While there are outstanding requests 46433eebb6aSJustin T. Gibbs while (!TAILQ_EMPTY(&sc->xbd_cm_busy)) { 46533eebb6aSJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd); 4668698b76cSKip Macy if (mtd) { 467e4808c4bSKip Macy /* Recieved request completions, update queue. */ 46833eebb6aSJustin T. Gibbs xbd_int(sc); 4698698b76cSKip Macy } 47033eebb6aSJustin T. Gibbs if (!TAILQ_EMPTY(&sc->xbd_cm_busy)) { 471e4808c4bSKip Macy /* 472e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 473e4808c4bSKip Macy * to complete. 474e4808c4bSKip Macy */ 475be7747b4SKip Macy HYPERVISOR_yield(); 4768698b76cSKip Macy } 4778698b76cSKip Macy } 4788698b76cSKip Macy } 4798698b76cSKip Macy 480e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 481e4808c4bSKip Macy static void 48233eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm) 483e4808c4bSKip Macy { 4848698b76cSKip Macy 48533eebb6aSJustin T. Gibbs xbd_enqueue_complete(cm); 486e4808c4bSKip Macy } 487e4808c4bSKip Macy 4888698b76cSKip Macy static int 48933eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 4908698b76cSKip Macy size_t length) 4918698b76cSKip Macy { 4928698b76cSKip Macy struct disk *dp = arg; 49333eebb6aSJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 49433eebb6aSJustin T. Gibbs struct xbd_command *cm; 495e4808c4bSKip Macy size_t chunk; 496e4808c4bSKip Macy int sbp; 4978698b76cSKip Macy int rc = 0; 4988698b76cSKip Macy 499e4808c4bSKip Macy if (length <= 0) 500e4808c4bSKip Macy return (rc); 5018698b76cSKip Macy 50233eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quiet on the western front. */ 503e4808c4bSKip Macy 504e4808c4bSKip Macy /* 505e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 506e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 507e4808c4bSKip Macy */ 50833eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 509e4808c4bSKip Macy 510e4808c4bSKip Macy /* Split the 64KB block as needed */ 511e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 51233eebb6aSJustin T. Gibbs cm = xbd_dequeue_free(sc); 513e4808c4bSKip Macy if (cm == NULL) { 51433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 51533eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "dump: no more commands?\n"); 516e4808c4bSKip Macy return (EBUSY); 517e4808c4bSKip Macy } 518e4808c4bSKip Macy 51933eebb6aSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 52033eebb6aSJustin T. Gibbs &cm->cm_gref_head) != 0) { 52133eebb6aSJustin T. Gibbs xbd_free_command(cm); 52233eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 52333eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "no more grant allocs?\n"); 524e4808c4bSKip Macy return (EBUSY); 525e4808c4bSKip Macy } 526e4808c4bSKip Macy 52733eebb6aSJustin T. Gibbs chunk = length > sc->xbd_max_request_size ? 52833eebb6aSJustin T. Gibbs sc->xbd_max_request_size : length; 52933eebb6aSJustin T. Gibbs cm->cm_data = virtual; 53033eebb6aSJustin T. Gibbs cm->cm_datalen = chunk; 53133eebb6aSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE; 53233eebb6aSJustin T. Gibbs cm->cm_sector_number = offset / dp->d_sectorsize; 53333eebb6aSJustin T. Gibbs cm->cm_complete = xbd_dump_complete; 534e4808c4bSKip Macy 53533eebb6aSJustin T. Gibbs xbd_enqueue_ready(cm); 5368698b76cSKip Macy 5378698b76cSKip Macy length -= chunk; 5388698b76cSKip Macy offset += chunk; 5398698b76cSKip Macy virtual = (char *) virtual + chunk; 5408698b76cSKip Macy } 5418698b76cSKip Macy 542e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 54333eebb6aSJustin T. Gibbs xbd_startio(sc); 54433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 545e4808c4bSKip Macy 546e4808c4bSKip Macy /* Poll for the completion. */ 54733eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quite on the eastern front */ 548e4808c4bSKip Macy 549e4808c4bSKip Macy /* If there were any errors, bail out... */ 55033eebb6aSJustin T. Gibbs while ((cm = xbd_dequeue_complete(sc)) != NULL) { 55133eebb6aSJustin T. Gibbs if (cm->cm_status != BLKIF_RSP_OKAY) { 55233eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 553e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 55433eebb6aSJustin T. Gibbs cm->cm_sector_number); 555e4808c4bSKip Macy rc = EIO; 5568698b76cSKip Macy } 55733eebb6aSJustin T. Gibbs xbd_free_command(cm); 5588698b76cSKip Macy } 559e4808c4bSKip Macy 5608698b76cSKip Macy return (rc); 5618698b76cSKip Macy } 5628698b76cSKip Macy 563*fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/ 56423dc5621SKip Macy static int 565*fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp) 56689e0f4d2SKip Macy { 567*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 56823dc5621SKip Macy 569*fac3fd80SJustin T. Gibbs if (sc == NULL) { 570*fac3fd80SJustin T. Gibbs printf("xb%d: not found", sc->xbd_unit); 57123dc5621SKip Macy return (ENXIO); 57223dc5621SKip Macy } 57323dc5621SKip Macy 574*fac3fd80SJustin T. Gibbs sc->xbd_flags |= XBD_OPEN; 575*fac3fd80SJustin T. Gibbs sc->xbd_users++; 576*fac3fd80SJustin T. Gibbs return (0); 577*fac3fd80SJustin T. Gibbs } 578*fac3fd80SJustin T. Gibbs 579*fac3fd80SJustin T. Gibbs static int 580*fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp) 581*fac3fd80SJustin T. Gibbs { 582*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 583*fac3fd80SJustin T. Gibbs 584*fac3fd80SJustin T. Gibbs if (sc == NULL) 585*fac3fd80SJustin T. Gibbs return (ENXIO); 586*fac3fd80SJustin T. Gibbs sc->xbd_flags &= ~XBD_OPEN; 587*fac3fd80SJustin T. Gibbs if (--(sc->xbd_users) == 0) { 588*fac3fd80SJustin T. Gibbs /* 589*fac3fd80SJustin T. Gibbs * Check whether we have been instructed to close. We will 590*fac3fd80SJustin T. Gibbs * have ignored this request initially, as the device was 591*fac3fd80SJustin T. Gibbs * still mounted. 592*fac3fd80SJustin T. Gibbs */ 593*fac3fd80SJustin T. Gibbs if (xenbus_get_otherend_state(sc->xbd_dev) == 594*fac3fd80SJustin T. Gibbs XenbusStateClosing) 595*fac3fd80SJustin T. Gibbs xbd_closing(sc->xbd_dev); 596*fac3fd80SJustin T. Gibbs } 597*fac3fd80SJustin T. Gibbs return (0); 598*fac3fd80SJustin T. Gibbs } 599*fac3fd80SJustin T. Gibbs 600*fac3fd80SJustin T. Gibbs static int 601*fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 602*fac3fd80SJustin T. Gibbs { 603*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 604*fac3fd80SJustin T. Gibbs 605*fac3fd80SJustin T. Gibbs if (sc == NULL) 606*fac3fd80SJustin T. Gibbs return (ENXIO); 607*fac3fd80SJustin T. Gibbs 608*fac3fd80SJustin T. Gibbs return (ENOTTY); 609*fac3fd80SJustin T. Gibbs } 610*fac3fd80SJustin T. Gibbs 611*fac3fd80SJustin T. Gibbs /* 612*fac3fd80SJustin T. Gibbs * Read/write routine for a buffer. Finds the proper unit, place it on 613*fac3fd80SJustin T. Gibbs * the sortq and kick the controller. 614*fac3fd80SJustin T. Gibbs */ 615*fac3fd80SJustin T. Gibbs static void 616*fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp) 617*fac3fd80SJustin T. Gibbs { 618*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = bp->bio_disk->d_drv1; 619*fac3fd80SJustin T. Gibbs 620*fac3fd80SJustin T. Gibbs /* bogus disk? */ 621*fac3fd80SJustin T. Gibbs if (sc == NULL) { 622*fac3fd80SJustin T. Gibbs bp->bio_error = EINVAL; 623*fac3fd80SJustin T. Gibbs bp->bio_flags |= BIO_ERROR; 624*fac3fd80SJustin T. Gibbs bp->bio_resid = bp->bio_bcount; 625*fac3fd80SJustin T. Gibbs biodone(bp); 626*fac3fd80SJustin T. Gibbs return; 627*fac3fd80SJustin T. Gibbs } 628*fac3fd80SJustin T. Gibbs 629*fac3fd80SJustin T. Gibbs /* 630*fac3fd80SJustin T. Gibbs * Place it in the queue of disk activities for this disk 631*fac3fd80SJustin T. Gibbs */ 632*fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 633*fac3fd80SJustin T. Gibbs 634*fac3fd80SJustin T. Gibbs xbd_enqueue_bio(sc, bp); 635*fac3fd80SJustin T. Gibbs xbd_startio(sc); 636*fac3fd80SJustin T. Gibbs 637*fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 638*fac3fd80SJustin T. Gibbs return; 639*fac3fd80SJustin T. Gibbs } 640*fac3fd80SJustin T. Gibbs 641*fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/ 642*fac3fd80SJustin T. Gibbs static int 643*fac3fd80SJustin T. Gibbs xbd_setup_ring(struct xbd_softc *sc) 644*fac3fd80SJustin T. Gibbs { 645*fac3fd80SJustin T. Gibbs blkif_sring_t *sring; 646*fac3fd80SJustin T. Gibbs uintptr_t sring_page_addr; 647*fac3fd80SJustin T. Gibbs int error; 648*fac3fd80SJustin T. Gibbs int i; 649*fac3fd80SJustin T. Gibbs 650*fac3fd80SJustin T. Gibbs sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 651*fac3fd80SJustin T. Gibbs M_NOWAIT|M_ZERO); 652*fac3fd80SJustin T. Gibbs if (sring == NULL) { 653*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring"); 654*fac3fd80SJustin T. Gibbs return (ENOMEM); 655*fac3fd80SJustin T. Gibbs } 656*fac3fd80SJustin T. Gibbs SHARED_RING_INIT(sring); 657*fac3fd80SJustin T. Gibbs FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE); 658*fac3fd80SJustin T. Gibbs 659*fac3fd80SJustin T. Gibbs for (i = 0, sring_page_addr = (uintptr_t)sring; 660*fac3fd80SJustin T. Gibbs i < sc->xbd_ring_pages; 661*fac3fd80SJustin T. Gibbs i++, sring_page_addr += PAGE_SIZE) { 662*fac3fd80SJustin T. Gibbs 663*fac3fd80SJustin T. Gibbs error = xenbus_grant_ring(sc->xbd_dev, 664*fac3fd80SJustin T. Gibbs (vtomach(sring_page_addr) >> PAGE_SHIFT), 665*fac3fd80SJustin T. Gibbs &sc->xbd_ring_ref[i]); 666*fac3fd80SJustin T. Gibbs if (error) { 667*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 668*fac3fd80SJustin T. Gibbs "granting ring_ref(%d)", i); 669*fac3fd80SJustin T. Gibbs return (error); 670*fac3fd80SJustin T. Gibbs } 671*fac3fd80SJustin T. Gibbs } 672*fac3fd80SJustin T. Gibbs if (sc->xbd_ring_pages == 1) { 673*fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 674*fac3fd80SJustin T. Gibbs "ring-ref", "%u", sc->xbd_ring_ref[0]); 675*fac3fd80SJustin T. Gibbs if (error) { 676*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 677*fac3fd80SJustin T. Gibbs "writing %s/ring-ref", 678*fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev)); 679*fac3fd80SJustin T. Gibbs return (error); 680*fac3fd80SJustin T. Gibbs } 681*fac3fd80SJustin T. Gibbs } else { 682*fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 683*fac3fd80SJustin T. Gibbs char ring_ref_name[]= "ring_refXX"; 684*fac3fd80SJustin T. Gibbs 685*fac3fd80SJustin T. Gibbs snprintf(ring_ref_name, sizeof(ring_ref_name), 686*fac3fd80SJustin T. Gibbs "ring-ref%u", i); 687*fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 688*fac3fd80SJustin T. Gibbs ring_ref_name, "%u", sc->xbd_ring_ref[i]); 689*fac3fd80SJustin T. Gibbs if (error) { 690*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 691*fac3fd80SJustin T. Gibbs "writing %s/%s", 692*fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev), 693*fac3fd80SJustin T. Gibbs ring_ref_name); 694*fac3fd80SJustin T. Gibbs return (error); 695*fac3fd80SJustin T. Gibbs } 696*fac3fd80SJustin T. Gibbs } 697*fac3fd80SJustin T. Gibbs } 698*fac3fd80SJustin T. Gibbs 699*fac3fd80SJustin T. Gibbs error = bind_listening_port_to_irqhandler( 700*fac3fd80SJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), 701*fac3fd80SJustin T. Gibbs "xbd", (driver_intr_t *)xbd_int, sc, 702*fac3fd80SJustin T. Gibbs INTR_TYPE_BIO | INTR_MPSAFE, &sc->xbd_irq); 703*fac3fd80SJustin T. Gibbs if (error) { 704*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 705*fac3fd80SJustin T. Gibbs "bind_evtchn_to_irqhandler failed"); 706*fac3fd80SJustin T. Gibbs return (error); 707*fac3fd80SJustin T. Gibbs } 708*fac3fd80SJustin T. Gibbs 709*fac3fd80SJustin T. Gibbs return (0); 710*fac3fd80SJustin T. Gibbs } 711*fac3fd80SJustin T. Gibbs 712*fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/ 7138b8bfa35SJustin T. Gibbs static void 71433eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd) 7158b8bfa35SJustin T. Gibbs { 7168b8bfa35SJustin T. Gibbs struct sysctl_ctx_list *sysctl_ctx = NULL; 7178b8bfa35SJustin T. Gibbs struct sysctl_oid *sysctl_tree = NULL; 7188b8bfa35SJustin T. Gibbs 71933eebb6aSJustin T. Gibbs sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev); 7208b8bfa35SJustin T. Gibbs if (sysctl_ctx == NULL) 7218b8bfa35SJustin T. Gibbs return; 7228b8bfa35SJustin T. Gibbs 72333eebb6aSJustin T. Gibbs sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev); 7248b8bfa35SJustin T. Gibbs if (sysctl_tree == NULL) 7258b8bfa35SJustin T. Gibbs return; 7268b8bfa35SJustin T. Gibbs 7278b8bfa35SJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, 72833eebb6aSJustin T. Gibbs "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1, 7298b8bfa35SJustin T. Gibbs "maximum outstanding requests (negotiated)"); 7308b8bfa35SJustin T. Gibbs 7318b8bfa35SJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, 7328b8bfa35SJustin T. Gibbs "max_request_segments", CTLFLAG_RD, 73333eebb6aSJustin T. Gibbs &xbd->xbd_max_request_segments, 0, 7348b8bfa35SJustin T. Gibbs "maximum number of pages per requests (negotiated)"); 7358b8bfa35SJustin T. Gibbs 7368b8bfa35SJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, 73733eebb6aSJustin T. Gibbs "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0, 7388b8bfa35SJustin T. Gibbs "maximum size in bytes of a request (negotiated)"); 7398b8bfa35SJustin T. Gibbs 7408b8bfa35SJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO, 74133eebb6aSJustin T. Gibbs "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0, 7428b8bfa35SJustin T. Gibbs "communication channel pages (negotiated)"); 7438b8bfa35SJustin T. Gibbs } 7448b8bfa35SJustin T. Gibbs 74523dc5621SKip Macy /* 746*fac3fd80SJustin T. Gibbs * Translate Linux major/minor to an appropriate name and unit 747*fac3fd80SJustin T. Gibbs * number. For HVM guests, this allows us to use the same drive names 748*fac3fd80SJustin T. Gibbs * with blkfront as the emulated drives, easing transition slightly. 74923dc5621SKip Macy */ 750*fac3fd80SJustin T. Gibbs static void 751*fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name) 75223dc5621SKip Macy { 753*fac3fd80SJustin T. Gibbs static struct vdev_info { 754*fac3fd80SJustin T. Gibbs int major; 755*fac3fd80SJustin T. Gibbs int shift; 756*fac3fd80SJustin T. Gibbs int base; 75723dc5621SKip Macy const char *name; 758*fac3fd80SJustin T. Gibbs } info[] = { 759*fac3fd80SJustin T. Gibbs {3, 6, 0, "ada"}, /* ide0 */ 760*fac3fd80SJustin T. Gibbs {22, 6, 2, "ada"}, /* ide1 */ 761*fac3fd80SJustin T. Gibbs {33, 6, 4, "ada"}, /* ide2 */ 762*fac3fd80SJustin T. Gibbs {34, 6, 6, "ada"}, /* ide3 */ 763*fac3fd80SJustin T. Gibbs {56, 6, 8, "ada"}, /* ide4 */ 764*fac3fd80SJustin T. Gibbs {57, 6, 10, "ada"}, /* ide5 */ 765*fac3fd80SJustin T. Gibbs {88, 6, 12, "ada"}, /* ide6 */ 766*fac3fd80SJustin T. Gibbs {89, 6, 14, "ada"}, /* ide7 */ 767*fac3fd80SJustin T. Gibbs {90, 6, 16, "ada"}, /* ide8 */ 768*fac3fd80SJustin T. Gibbs {91, 6, 18, "ada"}, /* ide9 */ 76989e0f4d2SKip Macy 770*fac3fd80SJustin T. Gibbs {8, 4, 0, "da"}, /* scsi disk0 */ 771*fac3fd80SJustin T. Gibbs {65, 4, 16, "da"}, /* scsi disk1 */ 772*fac3fd80SJustin T. Gibbs {66, 4, 32, "da"}, /* scsi disk2 */ 773*fac3fd80SJustin T. Gibbs {67, 4, 48, "da"}, /* scsi disk3 */ 774*fac3fd80SJustin T. Gibbs {68, 4, 64, "da"}, /* scsi disk4 */ 775*fac3fd80SJustin T. Gibbs {69, 4, 80, "da"}, /* scsi disk5 */ 776*fac3fd80SJustin T. Gibbs {70, 4, 96, "da"}, /* scsi disk6 */ 777*fac3fd80SJustin T. Gibbs {71, 4, 112, "da"}, /* scsi disk7 */ 778*fac3fd80SJustin T. Gibbs {128, 4, 128, "da"}, /* scsi disk8 */ 779*fac3fd80SJustin T. Gibbs {129, 4, 144, "da"}, /* scsi disk9 */ 780*fac3fd80SJustin T. Gibbs {130, 4, 160, "da"}, /* scsi disk10 */ 781*fac3fd80SJustin T. Gibbs {131, 4, 176, "da"}, /* scsi disk11 */ 782*fac3fd80SJustin T. Gibbs {132, 4, 192, "da"}, /* scsi disk12 */ 783*fac3fd80SJustin T. Gibbs {133, 4, 208, "da"}, /* scsi disk13 */ 784*fac3fd80SJustin T. Gibbs {134, 4, 224, "da"}, /* scsi disk14 */ 785*fac3fd80SJustin T. Gibbs {135, 4, 240, "da"}, /* scsi disk15 */ 786*fac3fd80SJustin T. Gibbs 787*fac3fd80SJustin T. Gibbs {202, 4, 0, "xbd"}, /* xbd */ 788*fac3fd80SJustin T. Gibbs 789*fac3fd80SJustin T. Gibbs {0, 0, 0, NULL}, 790*fac3fd80SJustin T. Gibbs }; 791*fac3fd80SJustin T. Gibbs int major = vdevice >> 8; 792*fac3fd80SJustin T. Gibbs int minor = vdevice & 0xff; 793*fac3fd80SJustin T. Gibbs int i; 794*fac3fd80SJustin T. Gibbs 795*fac3fd80SJustin T. Gibbs if (vdevice & (1 << 28)) { 796*fac3fd80SJustin T. Gibbs *unit = (vdevice & ((1 << 28) - 1)) >> 8; 797*fac3fd80SJustin T. Gibbs *name = "xbd"; 798*fac3fd80SJustin T. Gibbs return; 79989e0f4d2SKip Macy } 80089e0f4d2SKip Macy 801*fac3fd80SJustin T. Gibbs for (i = 0; info[i].major; i++) { 802*fac3fd80SJustin T. Gibbs if (info[i].major == major) { 803*fac3fd80SJustin T. Gibbs *unit = info[i].base + (minor >> info[i].shift); 804*fac3fd80SJustin T. Gibbs *name = info[i].name; 805*fac3fd80SJustin T. Gibbs return; 806*fac3fd80SJustin T. Gibbs } 807*fac3fd80SJustin T. Gibbs } 808*fac3fd80SJustin T. Gibbs 809*fac3fd80SJustin T. Gibbs *unit = minor >> 4; 810*fac3fd80SJustin T. Gibbs *name = "xbd"; 811*fac3fd80SJustin T. Gibbs } 812*fac3fd80SJustin T. Gibbs 813*fac3fd80SJustin T. Gibbs int 814*fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors, 815*fac3fd80SJustin T. Gibbs int vdevice, uint16_t vdisk_info, unsigned long sector_size) 816*fac3fd80SJustin T. Gibbs { 817*fac3fd80SJustin T. Gibbs int unit, error = 0; 818*fac3fd80SJustin T. Gibbs const char *name; 819*fac3fd80SJustin T. Gibbs 82033eebb6aSJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 82123dc5621SKip Macy 822*fac3fd80SJustin T. Gibbs sc->xbd_unit = unit; 82389e0f4d2SKip Macy 824*fac3fd80SJustin T. Gibbs if (strcmp(name, "xbd")) 825*fac3fd80SJustin T. Gibbs device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit); 82689e0f4d2SKip Macy 827*fac3fd80SJustin T. Gibbs sc->xbd_disk = disk_alloc(); 828*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_unit = sc->xbd_unit; 829*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_open = xbd_open; 830*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_close = xbd_close; 831*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_ioctl = xbd_ioctl; 832*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_strategy = xbd_strategy; 833*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_dump = xbd_dump; 834*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_name = name; 835*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_drv1 = sc; 836*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_sectorsize = sector_size; 8378b8bfa35SJustin T. Gibbs 838*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_mediasize = sectors * sector_size; 839*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_maxsize = sc->xbd_max_request_size; 840*fac3fd80SJustin T. Gibbs sc->xbd_disk->d_flags = 0; 841*fac3fd80SJustin T. Gibbs disk_create(sc->xbd_disk, DISK_VERSION); 84212678024SDoug Rabson 843*fac3fd80SJustin T. Gibbs return error; 84412678024SDoug Rabson } 84512678024SDoug Rabson 846*fac3fd80SJustin T. Gibbs static void 847*fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc) 84812678024SDoug Rabson { 849*fac3fd80SJustin T. Gibbs uint8_t *sring_page_ptr; 850*fac3fd80SJustin T. Gibbs int i; 85112678024SDoug Rabson 85212678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 85333eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 854*fac3fd80SJustin T. Gibbs sc->xbd_connected = XBD_STATE_DISCONNECTED; 85533eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 85689e0f4d2SKip Macy 857*fac3fd80SJustin T. Gibbs /* Free resources associated with old device channel. */ 858*fac3fd80SJustin T. Gibbs if (sc->xbd_ring.sring != NULL) { 859*fac3fd80SJustin T. Gibbs sring_page_ptr = (uint8_t *)sc->xbd_ring.sring; 860*fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 861*fac3fd80SJustin T. Gibbs grant_ref_t *ref; 86206a630f6SJustin T. Gibbs 863*fac3fd80SJustin T. Gibbs ref = &sc->xbd_ring_ref[i]; 864*fac3fd80SJustin T. Gibbs if (*ref != GRANT_INVALID_REF) { 865*fac3fd80SJustin T. Gibbs gnttab_end_foreign_access_ref(*ref); 866*fac3fd80SJustin T. Gibbs *ref = GRANT_INVALID_REF; 867*fac3fd80SJustin T. Gibbs } 868*fac3fd80SJustin T. Gibbs sring_page_ptr += PAGE_SIZE; 869*fac3fd80SJustin T. Gibbs } 870*fac3fd80SJustin T. Gibbs free(sc->xbd_ring.sring, M_XENBLOCKFRONT); 871*fac3fd80SJustin T. Gibbs sc->xbd_ring.sring = NULL; 87289e0f4d2SKip Macy } 87389e0f4d2SKip Macy 874*fac3fd80SJustin T. Gibbs if (sc->xbd_shadow) { 87589e0f4d2SKip Macy 876*fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_max_requests; i++) { 877*fac3fd80SJustin T. Gibbs struct xbd_command *cm; 87889e0f4d2SKip Macy 879*fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[i]; 880*fac3fd80SJustin T. Gibbs if (cm->cm_sg_refs != NULL) { 881*fac3fd80SJustin T. Gibbs free(cm->cm_sg_refs, M_XENBLOCKFRONT); 882*fac3fd80SJustin T. Gibbs cm->cm_sg_refs = NULL; 88389e0f4d2SKip Macy } 88489e0f4d2SKip Macy 885*fac3fd80SJustin T. Gibbs bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map); 886*fac3fd80SJustin T. Gibbs } 887*fac3fd80SJustin T. Gibbs free(sc->xbd_shadow, M_XENBLOCKFRONT); 888*fac3fd80SJustin T. Gibbs sc->xbd_shadow = NULL; 889*fac3fd80SJustin T. Gibbs 890*fac3fd80SJustin T. Gibbs bus_dma_tag_destroy(sc->xbd_io_dmat); 891*fac3fd80SJustin T. Gibbs 892*fac3fd80SJustin T. Gibbs xbd_initq_free(sc); 893*fac3fd80SJustin T. Gibbs xbd_initq_ready(sc); 894*fac3fd80SJustin T. Gibbs xbd_initq_complete(sc); 895*fac3fd80SJustin T. Gibbs } 896*fac3fd80SJustin T. Gibbs 897*fac3fd80SJustin T. Gibbs if (sc->xbd_irq) { 898*fac3fd80SJustin T. Gibbs unbind_from_irqhandler(sc->xbd_irq); 899*fac3fd80SJustin T. Gibbs sc->xbd_irq = 0; 900*fac3fd80SJustin T. Gibbs } 901*fac3fd80SJustin T. Gibbs } 902*fac3fd80SJustin T. Gibbs 903*fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/ 904ff662b5cSJustin T. Gibbs static void 90533eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc) 90689e0f4d2SKip Macy { 907ff662b5cSJustin T. Gibbs const char *otherend_path; 908ff662b5cSJustin T. Gibbs const char *node_path; 9098b8bfa35SJustin T. Gibbs uint32_t max_ring_page_order; 910ff662b5cSJustin T. Gibbs int error; 911ff662b5cSJustin T. Gibbs int i; 91289e0f4d2SKip Macy 91333eebb6aSJustin T. Gibbs if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) { 91406a630f6SJustin T. Gibbs /* Initialization has already been performed. */ 915ff662b5cSJustin T. Gibbs return; 91606a630f6SJustin T. Gibbs } 91789e0f4d2SKip Macy 918ff662b5cSJustin T. Gibbs /* 919ff662b5cSJustin T. Gibbs * Protocol defaults valid even if negotiation for a 920ff662b5cSJustin T. Gibbs * setting fails. 921ff662b5cSJustin T. Gibbs */ 9228b8bfa35SJustin T. Gibbs max_ring_page_order = 0; 92333eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 92433eebb6aSJustin T. Gibbs sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK; 92533eebb6aSJustin T. Gibbs sc->xbd_max_request_size = 92633eebb6aSJustin T. Gibbs XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments); 92733eebb6aSJustin T. Gibbs sc->xbd_max_request_blocks = 92833eebb6aSJustin T. Gibbs BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments); 929ff662b5cSJustin T. Gibbs 930ff662b5cSJustin T. Gibbs /* 931ff662b5cSJustin T. Gibbs * Protocol negotiation. 932ff662b5cSJustin T. Gibbs * 933ff662b5cSJustin T. Gibbs * \note xs_gather() returns on the first encountered error, so 934ff662b5cSJustin T. Gibbs * we must use independant calls in order to guarantee 935ff662b5cSJustin T. Gibbs * we don't miss information in a sparsly populated back-end 936ff662b5cSJustin T. Gibbs * tree. 9378b8bfa35SJustin T. Gibbs * 9388b8bfa35SJustin T. Gibbs * \note xs_scanf() does not update variables for unmatched 9398b8bfa35SJustin T. Gibbs * fields. 940ff662b5cSJustin T. Gibbs */ 94133eebb6aSJustin T. Gibbs otherend_path = xenbus_get_otherend_path(sc->xbd_dev); 94233eebb6aSJustin T. Gibbs node_path = xenbus_get_node(sc->xbd_dev); 9438b8bfa35SJustin T. Gibbs 9448b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 9458b8bfa35SJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 9468b8bfa35SJustin T. Gibbs "max-ring-page-order", NULL, "%" PRIu32, 9478b8bfa35SJustin T. Gibbs &max_ring_page_order); 94833eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1 << max_ring_page_order; 949ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 950ff662b5cSJustin T. Gibbs "max-ring-pages", NULL, "%" PRIu32, 95133eebb6aSJustin T. Gibbs &sc->xbd_ring_pages); 95233eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages < 1) 95333eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 954ff662b5cSJustin T. Gibbs 95533eebb6aSJustin T. Gibbs sc->xbd_max_requests = 95633eebb6aSJustin T. Gibbs BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE); 957ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 958ff662b5cSJustin T. Gibbs "max-requests", NULL, "%" PRIu32, 95933eebb6aSJustin T. Gibbs &sc->xbd_max_requests); 960ff662b5cSJustin T. Gibbs 961ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 962ff662b5cSJustin T. Gibbs "max-request-segments", NULL, "%" PRIu32, 96333eebb6aSJustin T. Gibbs &sc->xbd_max_request_segments); 964ff662b5cSJustin T. Gibbs 965ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 966ff662b5cSJustin T. Gibbs "max-request-size", NULL, "%" PRIu32, 96733eebb6aSJustin T. Gibbs &sc->xbd_max_request_size); 968ff662b5cSJustin T. Gibbs 96933eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) { 97033eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 97133eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 97233eebb6aSJustin T. Gibbs "limited to front-end limit of %zu.\n", 97333eebb6aSJustin T. Gibbs sc->xbd_ring_pages, XBD_MAX_RING_PAGES); 97433eebb6aSJustin T. Gibbs sc->xbd_ring_pages = XBD_MAX_RING_PAGES; 97589e0f4d2SKip Macy } 97689e0f4d2SKip Macy 97733eebb6aSJustin T. Gibbs if (powerof2(sc->xbd_ring_pages) == 0) { 9788b8bfa35SJustin T. Gibbs uint32_t new_page_limit; 9798b8bfa35SJustin T. Gibbs 98033eebb6aSJustin T. Gibbs new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1); 98133eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 98233eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 98333eebb6aSJustin T. Gibbs "is not a power of 2. Limited to %u.\n", 98433eebb6aSJustin T. Gibbs sc->xbd_ring_pages, new_page_limit); 98533eebb6aSJustin T. Gibbs sc->xbd_ring_pages = new_page_limit; 9868b8bfa35SJustin T. Gibbs } 9878b8bfa35SJustin T. Gibbs 98833eebb6aSJustin T. Gibbs if (sc->xbd_max_requests > XBD_MAX_REQUESTS) { 98933eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 99033eebb6aSJustin T. Gibbs "Back-end specified max_requests of %u " 99133eebb6aSJustin T. Gibbs "limited to front-end limit of %u.\n", 99233eebb6aSJustin T. Gibbs sc->xbd_max_requests, XBD_MAX_REQUESTS); 99333eebb6aSJustin T. Gibbs sc->xbd_max_requests = XBD_MAX_REQUESTS; 99489e0f4d2SKip Macy } 995ff662b5cSJustin T. Gibbs 99633eebb6aSJustin T. Gibbs if (sc->xbd_max_request_segments > XBD_MAX_SEGMENTS_PER_REQUEST) { 99733eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 99833eebb6aSJustin T. Gibbs "Back-end specified max_request_segments of %u " 99933eebb6aSJustin T. Gibbs "limited to front-end limit of %u.\n", 100033eebb6aSJustin T. Gibbs sc->xbd_max_request_segments, 100133eebb6aSJustin T. Gibbs XBD_MAX_SEGMENTS_PER_REQUEST); 100233eebb6aSJustin T. Gibbs sc->xbd_max_request_segments = XBD_MAX_SEGMENTS_PER_REQUEST; 100389e0f4d2SKip Macy } 1004ff662b5cSJustin T. Gibbs 100533eebb6aSJustin T. Gibbs if (sc->xbd_max_request_size > XBD_MAX_REQUEST_SIZE) { 100633eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 100733eebb6aSJustin T. Gibbs "Back-end specified max_request_size of %u " 100833eebb6aSJustin T. Gibbs "limited to front-end limit of %u.\n", 100933eebb6aSJustin T. Gibbs sc->xbd_max_request_size, 101033eebb6aSJustin T. Gibbs XBD_MAX_REQUEST_SIZE); 101133eebb6aSJustin T. Gibbs sc->xbd_max_request_size = XBD_MAX_REQUEST_SIZE; 1012ff662b5cSJustin T. Gibbs } 1013443cc4d4SJustin T. Gibbs 101433eebb6aSJustin T. Gibbs if (sc->xbd_max_request_size > 101533eebb6aSJustin T. Gibbs XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)) { 101633eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 101733eebb6aSJustin T. Gibbs "Back-end specified max_request_size of %u " 101833eebb6aSJustin T. Gibbs "limited to front-end limit of %u. (Too few segments.)\n", 101933eebb6aSJustin T. Gibbs sc->xbd_max_request_size, 102033eebb6aSJustin T. Gibbs XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)); 102133eebb6aSJustin T. Gibbs sc->xbd_max_request_size = 102233eebb6aSJustin T. Gibbs XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments); 1023443cc4d4SJustin T. Gibbs } 1024443cc4d4SJustin T. Gibbs 102533eebb6aSJustin T. Gibbs sc->xbd_max_request_blocks = 102633eebb6aSJustin T. Gibbs BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments); 1027ff662b5cSJustin T. Gibbs 1028ff662b5cSJustin T. Gibbs /* Allocate datastructures based on negotiated values. */ 102933eebb6aSJustin T. Gibbs error = bus_dma_tag_create( 103033eebb6aSJustin T. Gibbs bus_get_dma_tag(sc->xbd_dev), /* parent */ 1031ff662b5cSJustin T. Gibbs 512, PAGE_SIZE, /* algnmnt, boundary */ 1032ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* lowaddr */ 1033ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* highaddr */ 1034ff662b5cSJustin T. Gibbs NULL, NULL, /* filter, filterarg */ 103533eebb6aSJustin T. Gibbs sc->xbd_max_request_size, 103633eebb6aSJustin T. Gibbs sc->xbd_max_request_segments, 1037ff662b5cSJustin T. Gibbs PAGE_SIZE, /* maxsegsize */ 1038ff662b5cSJustin T. Gibbs BUS_DMA_ALLOCNOW, /* flags */ 1039ff662b5cSJustin T. Gibbs busdma_lock_mutex, /* lockfunc */ 104033eebb6aSJustin T. Gibbs &sc->xbd_io_lock, /* lockarg */ 104133eebb6aSJustin T. Gibbs &sc->xbd_io_dmat); 1042ff662b5cSJustin T. Gibbs if (error != 0) { 104333eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1044ff662b5cSJustin T. Gibbs "Cannot allocate parent DMA tag\n"); 1045ff662b5cSJustin T. Gibbs return; 1046ff662b5cSJustin T. Gibbs } 1047ff662b5cSJustin T. Gibbs 1048ff662b5cSJustin T. Gibbs /* Per-transaction data allocation. */ 104933eebb6aSJustin T. Gibbs sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests, 1050ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 105133eebb6aSJustin T. Gibbs if (sc->xbd_shadow == NULL) { 105233eebb6aSJustin T. Gibbs bus_dma_tag_destroy(sc->xbd_io_dmat); 105333eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1054ff662b5cSJustin T. Gibbs "Cannot allocate request structures\n"); 105506a630f6SJustin T. Gibbs return; 1056ff662b5cSJustin T. Gibbs } 1057ff662b5cSJustin T. Gibbs 105833eebb6aSJustin T. Gibbs for (i = 0; i < sc->xbd_max_requests; i++) { 105933eebb6aSJustin T. Gibbs struct xbd_command *cm; 1060ff662b5cSJustin T. Gibbs 106133eebb6aSJustin T. Gibbs cm = &sc->xbd_shadow[i]; 106233eebb6aSJustin T. Gibbs cm->cm_sg_refs = malloc( 106333eebb6aSJustin T. Gibbs sizeof(grant_ref_t) * sc->xbd_max_request_segments, 1064ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT); 106533eebb6aSJustin T. Gibbs if (cm->cm_sg_refs == NULL) 1066ff662b5cSJustin T. Gibbs break; 106733eebb6aSJustin T. Gibbs cm->cm_id = i; 1068ff662b5cSJustin T. Gibbs cm->cm_sc = sc; 106933eebb6aSJustin T. Gibbs if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0) 1070ff662b5cSJustin T. Gibbs break; 107133eebb6aSJustin T. Gibbs xbd_free_command(cm); 1072ff662b5cSJustin T. Gibbs } 1073ff662b5cSJustin T. Gibbs 107433eebb6aSJustin T. Gibbs if (xbd_setup_ring(sc) != 0) 1075ff662b5cSJustin T. Gibbs return; 1076ff662b5cSJustin T. Gibbs 10778b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 107833eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > 1) { 1079ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 108033eebb6aSJustin T. Gibbs "num-ring-pages","%u", 108133eebb6aSJustin T. Gibbs sc->xbd_ring_pages); 1082ff662b5cSJustin T. Gibbs if (error) { 108333eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 10848b8bfa35SJustin T. Gibbs "writing %s/num-ring-pages", 10858b8bfa35SJustin T. Gibbs node_path); 10868b8bfa35SJustin T. Gibbs return; 10878b8bfa35SJustin T. Gibbs } 10880d172324SJustin T. Gibbs 10898b8bfa35SJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 10900d172324SJustin T. Gibbs "ring-page-order", "%u", 109133eebb6aSJustin T. Gibbs fls(sc->xbd_ring_pages) - 1); 10928b8bfa35SJustin T. Gibbs if (error) { 109333eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 10948b8bfa35SJustin T. Gibbs "writing %s/ring-page-order", 1095ff662b5cSJustin T. Gibbs node_path); 1096ff662b5cSJustin T. Gibbs return; 1097ff662b5cSJustin T. Gibbs } 10980d172324SJustin T. Gibbs } 1099ff662b5cSJustin T. Gibbs 1100ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 110133eebb6aSJustin T. Gibbs "max-requests","%u", 110233eebb6aSJustin T. Gibbs sc->xbd_max_requests); 1103ff662b5cSJustin T. Gibbs if (error) { 110433eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1105ff662b5cSJustin T. Gibbs "writing %s/max-requests", 1106ff662b5cSJustin T. Gibbs node_path); 1107ff662b5cSJustin T. Gibbs return; 1108ff662b5cSJustin T. Gibbs } 1109ff662b5cSJustin T. Gibbs 1110ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 111133eebb6aSJustin T. Gibbs "max-request-segments","%u", 111233eebb6aSJustin T. Gibbs sc->xbd_max_request_segments); 1113ff662b5cSJustin T. Gibbs if (error) { 111433eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1115ff662b5cSJustin T. Gibbs "writing %s/max-request-segments", 1116ff662b5cSJustin T. Gibbs node_path); 1117ff662b5cSJustin T. Gibbs return; 1118ff662b5cSJustin T. Gibbs } 1119ff662b5cSJustin T. Gibbs 1120ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 112133eebb6aSJustin T. Gibbs "max-request-size","%u", 112233eebb6aSJustin T. Gibbs sc->xbd_max_request_size); 1123ff662b5cSJustin T. Gibbs if (error) { 112433eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1125ff662b5cSJustin T. Gibbs "writing %s/max-request-size", 1126ff662b5cSJustin T. Gibbs node_path); 1127ff662b5cSJustin T. Gibbs return; 1128ff662b5cSJustin T. Gibbs } 1129ff662b5cSJustin T. Gibbs 1130ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "event-channel", 113133eebb6aSJustin T. Gibbs "%u", irq_to_evtchn_port(sc->xbd_irq)); 1132ff662b5cSJustin T. Gibbs if (error) { 113333eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1134ff662b5cSJustin T. Gibbs "writing %s/event-channel", 1135ff662b5cSJustin T. Gibbs node_path); 1136ff662b5cSJustin T. Gibbs return; 1137ff662b5cSJustin T. Gibbs } 1138ff662b5cSJustin T. Gibbs 113933eebb6aSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "protocol", 114033eebb6aSJustin T. Gibbs "%s", XEN_IO_PROTO_ABI_NATIVE); 1141ff662b5cSJustin T. Gibbs if (error) { 114233eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1143ff662b5cSJustin T. Gibbs "writing %s/protocol", 1144ff662b5cSJustin T. Gibbs node_path); 1145ff662b5cSJustin T. Gibbs return; 114623dc5621SKip Macy } 114712678024SDoug Rabson 114833eebb6aSJustin T. Gibbs xenbus_set_state(sc->xbd_dev, XenbusStateInitialised); 114989e0f4d2SKip Macy } 115089e0f4d2SKip Macy 115189e0f4d2SKip Macy /* 115233eebb6aSJustin T. Gibbs * Invoked when the backend is finally 'ready' (and has published 115333eebb6aSJustin T. Gibbs * the details about the physical device - #sectors, size, etc). 115489e0f4d2SKip Macy */ 115589e0f4d2SKip Macy static void 115633eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc) 115789e0f4d2SKip Macy { 115833eebb6aSJustin T. Gibbs device_t dev = sc->xbd_dev; 115989e0f4d2SKip Macy unsigned long sectors, sector_size; 116089e0f4d2SKip Macy unsigned int binfo; 1161e4808c4bSKip Macy int err, feature_barrier; 116289e0f4d2SKip Macy 116333eebb6aSJustin T. Gibbs if ((sc->xbd_connected == XBD_STATE_CONNECTED) || 116433eebb6aSJustin T. Gibbs (sc->xbd_connected == XBD_STATE_SUSPENDED)) 116589e0f4d2SKip Macy return; 116689e0f4d2SKip Macy 116723dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 116889e0f4d2SKip Macy 1169ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 117089e0f4d2SKip Macy "sectors", "%lu", §ors, 117189e0f4d2SKip Macy "info", "%u", &binfo, 117289e0f4d2SKip Macy "sector-size", "%lu", §or_size, 117389e0f4d2SKip Macy NULL); 117489e0f4d2SKip Macy if (err) { 117523dc5621SKip Macy xenbus_dev_fatal(dev, err, 117689e0f4d2SKip Macy "reading backend fields at %s", 117723dc5621SKip Macy xenbus_get_otherend_path(dev)); 117889e0f4d2SKip Macy return; 117989e0f4d2SKip Macy } 1180ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1181e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 118289e0f4d2SKip Macy NULL); 1183e4808c4bSKip Macy if (!err || feature_barrier) 118433eebb6aSJustin T. Gibbs sc->xbd_flags |= XBD_BARRIER; 118589e0f4d2SKip Macy 118633eebb6aSJustin T. Gibbs if (sc->xbd_disk == NULL) { 118723dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 118823dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 118923dc5621SKip Macy device_get_desc(dev), 119023dc5621SKip Macy xenbus_get_node(dev)); 119123dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 119289e0f4d2SKip Macy 119333eebb6aSJustin T. Gibbs xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo, 119433eebb6aSJustin T. Gibbs sector_size); 119506a630f6SJustin T. Gibbs } 119623dc5621SKip Macy 119723dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 119889e0f4d2SKip Macy 119989e0f4d2SKip Macy /* Kick pending requests. */ 120033eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 120133eebb6aSJustin T. Gibbs sc->xbd_connected = XBD_STATE_CONNECTED; 120233eebb6aSJustin T. Gibbs xbd_startio(sc); 120333eebb6aSJustin T. Gibbs sc->xbd_flags |= XBD_READY; 120433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 120589e0f4d2SKip Macy } 120689e0f4d2SKip Macy 120789e0f4d2SKip Macy /** 120889e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 120989e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 1210a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 121189e0f4d2SKip Macy * acknowledgement. 121289e0f4d2SKip Macy */ 121323dc5621SKip Macy static void 121433eebb6aSJustin T. Gibbs xbd_closing(device_t dev) 121589e0f4d2SKip Macy { 121633eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 121789e0f4d2SKip Macy 1218ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateClosing); 1219ff662b5cSJustin T. Gibbs 122033eebb6aSJustin T. Gibbs DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev)); 122189e0f4d2SKip Macy 122233eebb6aSJustin T. Gibbs if (sc->xbd_disk != NULL) { 122333eebb6aSJustin T. Gibbs disk_destroy(sc->xbd_disk); 122433eebb6aSJustin T. Gibbs sc->xbd_disk = NULL; 122589e0f4d2SKip Macy } 122689e0f4d2SKip Macy 122723dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 122889e0f4d2SKip Macy } 122989e0f4d2SKip Macy 1230*fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/ 1231*fac3fd80SJustin T. Gibbs static int 1232*fac3fd80SJustin T. Gibbs xbd_probe(device_t dev) 1233*fac3fd80SJustin T. Gibbs { 1234*fac3fd80SJustin T. Gibbs 1235*fac3fd80SJustin T. Gibbs if (!strcmp(xenbus_get_type(dev), "vbd")) { 1236*fac3fd80SJustin T. Gibbs device_set_desc(dev, "Virtual Block Device"); 1237*fac3fd80SJustin T. Gibbs device_quiet(dev); 1238*fac3fd80SJustin T. Gibbs return (0); 1239*fac3fd80SJustin T. Gibbs } 1240*fac3fd80SJustin T. Gibbs 1241*fac3fd80SJustin T. Gibbs return (ENXIO); 1242*fac3fd80SJustin T. Gibbs } 1243*fac3fd80SJustin T. Gibbs 1244*fac3fd80SJustin T. Gibbs /* 1245*fac3fd80SJustin T. Gibbs * Setup supplies the backend dir, virtual device. We place an event 1246*fac3fd80SJustin T. Gibbs * channel and shared frame entries. We watch backend to wait if it's 1247*fac3fd80SJustin T. Gibbs * ok. 1248*fac3fd80SJustin T. Gibbs */ 1249*fac3fd80SJustin T. Gibbs static int 1250*fac3fd80SJustin T. Gibbs xbd_attach(device_t dev) 1251*fac3fd80SJustin T. Gibbs { 1252*fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 1253*fac3fd80SJustin T. Gibbs const char *name; 1254*fac3fd80SJustin T. Gibbs uint32_t vdevice; 1255*fac3fd80SJustin T. Gibbs int error; 1256*fac3fd80SJustin T. Gibbs int i; 1257*fac3fd80SJustin T. Gibbs int unit; 1258*fac3fd80SJustin T. Gibbs 1259*fac3fd80SJustin T. Gibbs /* FIXME: Use dynamic device id if this is not set. */ 1260*fac3fd80SJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_node(dev), 1261*fac3fd80SJustin T. Gibbs "virtual-device", NULL, "%" PRIu32, &vdevice); 1262*fac3fd80SJustin T. Gibbs if (error) { 1263*fac3fd80SJustin T. Gibbs xenbus_dev_fatal(dev, error, "reading virtual-device"); 1264*fac3fd80SJustin T. Gibbs device_printf(dev, "Couldn't determine virtual device.\n"); 1265*fac3fd80SJustin T. Gibbs return (error); 1266*fac3fd80SJustin T. Gibbs } 1267*fac3fd80SJustin T. Gibbs 1268*fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 1269*fac3fd80SJustin T. Gibbs if (!strcmp(name, "xbd")) 1270*fac3fd80SJustin T. Gibbs device_set_unit(dev, unit); 1271*fac3fd80SJustin T. Gibbs 1272*fac3fd80SJustin T. Gibbs sc = device_get_softc(dev); 1273*fac3fd80SJustin T. Gibbs mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 1274*fac3fd80SJustin T. Gibbs xbd_initq_free(sc); 1275*fac3fd80SJustin T. Gibbs xbd_initq_busy(sc); 1276*fac3fd80SJustin T. Gibbs xbd_initq_ready(sc); 1277*fac3fd80SJustin T. Gibbs xbd_initq_complete(sc); 1278*fac3fd80SJustin T. Gibbs xbd_initq_bio(sc); 1279*fac3fd80SJustin T. Gibbs for (i = 0; i < XBD_MAX_RING_PAGES; i++) 1280*fac3fd80SJustin T. Gibbs sc->xbd_ring_ref[i] = GRANT_INVALID_REF; 1281*fac3fd80SJustin T. Gibbs 1282*fac3fd80SJustin T. Gibbs sc->xbd_dev = dev; 1283*fac3fd80SJustin T. Gibbs sc->xbd_vdevice = vdevice; 1284*fac3fd80SJustin T. Gibbs sc->xbd_connected = XBD_STATE_DISCONNECTED; 1285*fac3fd80SJustin T. Gibbs 1286*fac3fd80SJustin T. Gibbs xbd_setup_sysctl(sc); 1287*fac3fd80SJustin T. Gibbs 1288*fac3fd80SJustin T. Gibbs /* Wait for backend device to publish its protocol capabilities. */ 1289*fac3fd80SJustin T. Gibbs xenbus_set_state(dev, XenbusStateInitialising); 1290*fac3fd80SJustin T. Gibbs 1291*fac3fd80SJustin T. Gibbs return (0); 1292*fac3fd80SJustin T. Gibbs } 129389e0f4d2SKip Macy 129423dc5621SKip Macy static int 129533eebb6aSJustin T. Gibbs xbd_detach(device_t dev) 129689e0f4d2SKip Macy { 129733eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 129889e0f4d2SKip Macy 129933eebb6aSJustin T. Gibbs DPRINTK("xbd_remove: %s removed\n", xenbus_get_node(dev)); 130089e0f4d2SKip Macy 130133eebb6aSJustin T. Gibbs xbd_free(sc); 130233eebb6aSJustin T. Gibbs mtx_destroy(&sc->xbd_io_lock); 130389e0f4d2SKip Macy 130489e0f4d2SKip Macy return 0; 130589e0f4d2SKip Macy } 130689e0f4d2SKip Macy 130789e0f4d2SKip Macy static int 1308*fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev) 130989e0f4d2SKip Macy { 1310*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1311*fac3fd80SJustin T. Gibbs int retval; 1312*fac3fd80SJustin T. Gibbs int saved_state; 131389e0f4d2SKip Macy 131489e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 131533eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1316*fac3fd80SJustin T. Gibbs saved_state = sc->xbd_connected; 1317*fac3fd80SJustin T. Gibbs sc->xbd_connected = XBD_STATE_SUSPENDED; 1318*fac3fd80SJustin T. Gibbs 1319*fac3fd80SJustin T. Gibbs /* Wait for outstanding I/O to drain. */ 1320*fac3fd80SJustin T. Gibbs retval = 0; 1321*fac3fd80SJustin T. Gibbs while (TAILQ_EMPTY(&sc->xbd_cm_busy) == 0) { 1322*fac3fd80SJustin T. Gibbs if (msleep(&sc->xbd_cm_busy, &sc->xbd_io_lock, 1323*fac3fd80SJustin T. Gibbs PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) { 1324*fac3fd80SJustin T. Gibbs retval = EBUSY; 1325*fac3fd80SJustin T. Gibbs break; 1326*fac3fd80SJustin T. Gibbs } 1327*fac3fd80SJustin T. Gibbs } 132833eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 132989e0f4d2SKip Macy 1330*fac3fd80SJustin T. Gibbs if (retval != 0) 1331*fac3fd80SJustin T. Gibbs sc->xbd_connected = saved_state; 133233eebb6aSJustin T. Gibbs 1333*fac3fd80SJustin T. Gibbs return (retval); 133489e0f4d2SKip Macy } 133589e0f4d2SKip Macy 1336ff662b5cSJustin T. Gibbs static int 1337*fac3fd80SJustin T. Gibbs xbd_resume(device_t dev) 133889e0f4d2SKip Macy { 1339*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1340*fac3fd80SJustin T. Gibbs 1341*fac3fd80SJustin T. Gibbs DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev)); 1342*fac3fd80SJustin T. Gibbs 1343*fac3fd80SJustin T. Gibbs xbd_free(sc); 1344*fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1345*fac3fd80SJustin T. Gibbs return (0); 134689e0f4d2SKip Macy } 134789e0f4d2SKip Macy 1348*fac3fd80SJustin T. Gibbs /** 1349*fac3fd80SJustin T. Gibbs * Callback received when the backend's state changes. 1350*fac3fd80SJustin T. Gibbs */ 1351*fac3fd80SJustin T. Gibbs static void 1352*fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state) 1353*fac3fd80SJustin T. Gibbs { 1354*fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1355*fac3fd80SJustin T. Gibbs 1356*fac3fd80SJustin T. Gibbs DPRINTK("backend_state=%d\n", backend_state); 1357*fac3fd80SJustin T. Gibbs 1358*fac3fd80SJustin T. Gibbs switch (backend_state) { 1359*fac3fd80SJustin T. Gibbs case XenbusStateUnknown: 1360*fac3fd80SJustin T. Gibbs case XenbusStateInitialising: 1361*fac3fd80SJustin T. Gibbs case XenbusStateReconfigured: 1362*fac3fd80SJustin T. Gibbs case XenbusStateReconfiguring: 1363*fac3fd80SJustin T. Gibbs case XenbusStateClosed: 1364*fac3fd80SJustin T. Gibbs break; 1365*fac3fd80SJustin T. Gibbs 1366*fac3fd80SJustin T. Gibbs case XenbusStateInitWait: 1367*fac3fd80SJustin T. Gibbs case XenbusStateInitialised: 1368*fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1369*fac3fd80SJustin T. Gibbs break; 1370*fac3fd80SJustin T. Gibbs 1371*fac3fd80SJustin T. Gibbs case XenbusStateConnected: 1372*fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1373*fac3fd80SJustin T. Gibbs xbd_connect(sc); 1374*fac3fd80SJustin T. Gibbs break; 1375*fac3fd80SJustin T. Gibbs 1376*fac3fd80SJustin T. Gibbs case XenbusStateClosing: 1377*fac3fd80SJustin T. Gibbs if (sc->xbd_users > 0) 1378*fac3fd80SJustin T. Gibbs xenbus_dev_error(dev, -EBUSY, 1379*fac3fd80SJustin T. Gibbs "Device in use; refusing to close"); 1380*fac3fd80SJustin T. Gibbs else 1381*fac3fd80SJustin T. Gibbs xbd_closing(dev); 1382*fac3fd80SJustin T. Gibbs break; 1383*fac3fd80SJustin T. Gibbs } 1384*fac3fd80SJustin T. Gibbs } 1385*fac3fd80SJustin T. Gibbs 1386*fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/ 138733eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = { 138823dc5621SKip Macy /* Device interface */ 138933eebb6aSJustin T. Gibbs DEVMETHOD(device_probe, xbd_probe), 139033eebb6aSJustin T. Gibbs DEVMETHOD(device_attach, xbd_attach), 139133eebb6aSJustin T. Gibbs DEVMETHOD(device_detach, xbd_detach), 139223dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 139333eebb6aSJustin T. Gibbs DEVMETHOD(device_suspend, xbd_suspend), 139433eebb6aSJustin T. Gibbs DEVMETHOD(device_resume, xbd_resume), 139589e0f4d2SKip Macy 139623dc5621SKip Macy /* Xenbus interface */ 139733eebb6aSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed), 139889e0f4d2SKip Macy 139923dc5621SKip Macy { 0, 0 } 140089e0f4d2SKip Macy }; 140189e0f4d2SKip Macy 140233eebb6aSJustin T. Gibbs static driver_t xbd_driver = { 140323dc5621SKip Macy "xbd", 140433eebb6aSJustin T. Gibbs xbd_methods, 140533eebb6aSJustin T. Gibbs sizeof(struct xbd_softc), 140689e0f4d2SKip Macy }; 140733eebb6aSJustin T. Gibbs devclass_t xbd_devclass; 140889e0f4d2SKip Macy 140933eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0); 1410