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 #include <machine/xen/xenvar.h> 6476acc41fSJustin T. Gibbs 6589e0f4d2SKip Macy #include <geom/geom_disk.h> 6689e0f4d2SKip Macy 6789e0f4d2SKip Macy #include <dev/xen/blkfront/block.h> 6889e0f4d2SKip Macy 6923dc5621SKip Macy #include "xenbus_if.h" 7023dc5621SKip Macy 71fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/ 7233eebb6aSJustin T. Gibbs static void xbd_closing(device_t); 73fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc); 7489e0f4d2SKip Macy 75fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/ 76fac3fd80SJustin T. Gibbs #if 0 77fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 78fac3fd80SJustin T. Gibbs #else 79fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) 80fac3fd80SJustin T. Gibbs #endif 81fac3fd80SJustin T. Gibbs 82fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT 9 83ff662b5cSJustin T. Gibbs 84fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/ 85fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 8689e0f4d2SKip Macy 87fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/ 88127a9483SJustin T. Gibbs static void 89127a9483SJustin T. Gibbs xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag) 90127a9483SJustin T. Gibbs { 91127a9483SJustin T. Gibbs if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0) 92127a9483SJustin T. Gibbs return; 93127a9483SJustin T. Gibbs 94127a9483SJustin T. Gibbs sc->xbd_flags |= xbd_flag; 95127a9483SJustin T. Gibbs sc->xbd_qfrozen_cnt++; 96127a9483SJustin T. Gibbs } 97127a9483SJustin T. Gibbs 98127a9483SJustin T. Gibbs static void 99127a9483SJustin T. Gibbs xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag) 100127a9483SJustin T. Gibbs { 101127a9483SJustin T. Gibbs if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0) 102127a9483SJustin T. Gibbs return; 103127a9483SJustin T. Gibbs 104b834eea6SJustin T. Gibbs if (sc->xbd_qfrozen_cnt == 0) 105127a9483SJustin T. Gibbs panic("%s: Thaw with flag 0x%x while not frozen.", 106127a9483SJustin T. Gibbs __func__, xbd_flag); 107127a9483SJustin T. Gibbs 108127a9483SJustin T. Gibbs sc->xbd_flags &= ~xbd_flag; 109127a9483SJustin T. Gibbs sc->xbd_qfrozen_cnt--; 110127a9483SJustin T. Gibbs } 111127a9483SJustin T. Gibbs 1129985113bSJustin T. Gibbs static void 1139985113bSJustin T. Gibbs xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag) 1149985113bSJustin T. Gibbs { 1159985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_FROZEN) != 0) 1169985113bSJustin T. Gibbs return; 1179985113bSJustin T. Gibbs 1189985113bSJustin T. Gibbs cm->cm_flags |= XBDCF_FROZEN|cm_flag; 1199985113bSJustin T. Gibbs xbd_freeze(sc, XBDF_NONE); 1209985113bSJustin T. Gibbs } 1219985113bSJustin T. Gibbs 1229985113bSJustin T. Gibbs static void 1239985113bSJustin T. Gibbs xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm) 1249985113bSJustin T. Gibbs { 1259985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_FROZEN) == 0) 1269985113bSJustin T. Gibbs return; 1279985113bSJustin T. Gibbs 1289985113bSJustin T. Gibbs cm->cm_flags &= ~XBDCF_FROZEN; 1299985113bSJustin T. Gibbs xbd_thaw(sc, XBDF_NONE); 1309985113bSJustin T. Gibbs } 1319985113bSJustin T. Gibbs 132fac3fd80SJustin T. Gibbs static inline void 133cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc) 134fac3fd80SJustin T. Gibbs { 135fac3fd80SJustin T. Gibbs int notify; 136e4808c4bSKip Macy 137fac3fd80SJustin T. Gibbs RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify); 13889e0f4d2SKip Macy 139fac3fd80SJustin T. Gibbs if (notify) 14076acc41fSJustin T. Gibbs xen_intr_signal(sc->xen_intr_handle); 141fac3fd80SJustin T. Gibbs } 14289e0f4d2SKip Macy 14323dc5621SKip Macy static void 144fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm) 14523dc5621SKip Macy { 14623dc5621SKip Macy 147e2c1fe90SJustin T. Gibbs KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE, 148e2c1fe90SJustin T. Gibbs ("Freeing command that is still on queue %d.", 149e2c1fe90SJustin T. Gibbs cm->cm_flags & XBDCF_Q_MASK)); 15023dc5621SKip Macy 151e2c1fe90SJustin T. Gibbs cm->cm_flags = XBDCF_INITIALIZER; 152fac3fd80SJustin T. Gibbs cm->cm_bp = NULL; 153fac3fd80SJustin T. Gibbs cm->cm_complete = NULL; 154e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_FREE); 155127a9483SJustin T. Gibbs xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE); 15623dc5621SKip Macy } 15723dc5621SKip Macy 15889e0f4d2SKip Macy static void 159*d0ecc14dSColin Percival mksegarray(bus_dma_segment_t *segs, int nsegs, 160*d0ecc14dSColin Percival grant_ref_t * gref_head, int otherend_id, int readonly, 161*d0ecc14dSColin Percival grant_ref_t * sg_ref, blkif_request_segment_t * sg) 162*d0ecc14dSColin Percival { 163*d0ecc14dSColin Percival struct blkif_request_segment *last_block_sg = sg + nsegs; 164*d0ecc14dSColin Percival vm_paddr_t buffer_ma; 165*d0ecc14dSColin Percival uint64_t fsect, lsect; 166*d0ecc14dSColin Percival int ref; 167*d0ecc14dSColin Percival 168*d0ecc14dSColin Percival while (sg < last_block_sg) { 169*d0ecc14dSColin Percival buffer_ma = segs->ds_addr; 170*d0ecc14dSColin Percival fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 171*d0ecc14dSColin Percival lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 172*d0ecc14dSColin Percival 173*d0ecc14dSColin Percival KASSERT(lsect <= 7, ("XEN disk driver data cannot " 174*d0ecc14dSColin Percival "cross a page boundary")); 175*d0ecc14dSColin Percival 176*d0ecc14dSColin Percival /* install a grant reference. */ 177*d0ecc14dSColin Percival ref = gnttab_claim_grant_reference(gref_head); 178*d0ecc14dSColin Percival 179*d0ecc14dSColin Percival /* 180*d0ecc14dSColin Percival * GNTTAB_LIST_END == 0xffffffff, but it is private 181*d0ecc14dSColin Percival * to gnttab.c. 182*d0ecc14dSColin Percival */ 183*d0ecc14dSColin Percival KASSERT(ref != ~0, ("grant_reference failed")); 184*d0ecc14dSColin Percival 185*d0ecc14dSColin Percival gnttab_grant_foreign_access_ref( 186*d0ecc14dSColin Percival ref, 187*d0ecc14dSColin Percival otherend_id, 188*d0ecc14dSColin Percival buffer_ma >> PAGE_SHIFT, 189*d0ecc14dSColin Percival readonly); 190*d0ecc14dSColin Percival 191*d0ecc14dSColin Percival *sg_ref = ref; 192*d0ecc14dSColin Percival *sg = (struct blkif_request_segment) { 193*d0ecc14dSColin Percival .gref = ref, 194*d0ecc14dSColin Percival .first_sect = fsect, 195*d0ecc14dSColin Percival .last_sect = lsect 196*d0ecc14dSColin Percival }; 197*d0ecc14dSColin Percival sg++; 198*d0ecc14dSColin Percival sg_ref++; 199*d0ecc14dSColin Percival segs++; 200*d0ecc14dSColin Percival } 201*d0ecc14dSColin Percival } 202*d0ecc14dSColin Percival 203*d0ecc14dSColin Percival static void 204fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 20589e0f4d2SKip Macy { 206fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 207fac3fd80SJustin T. Gibbs struct xbd_command *cm; 208fac3fd80SJustin T. Gibbs blkif_request_t *ring_req; 209fac3fd80SJustin T. Gibbs int op; 21089e0f4d2SKip Macy 211fac3fd80SJustin T. Gibbs cm = arg; 212fac3fd80SJustin T. Gibbs sc = cm->cm_sc; 213fac3fd80SJustin T. Gibbs 214fac3fd80SJustin T. Gibbs if (error) { 215fac3fd80SJustin T. Gibbs cm->cm_bp->bio_error = EIO; 216fac3fd80SJustin T. Gibbs biodone(cm->cm_bp); 217fac3fd80SJustin T. Gibbs xbd_free_command(cm); 21889e0f4d2SKip Macy return; 21989e0f4d2SKip Macy } 22089e0f4d2SKip Macy 221d33a1217SColin Percival KASSERT(nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST, 222d33a1217SColin Percival ("Too many segments in a blkfront I/O")); 223d33a1217SColin Percival 224fac3fd80SJustin T. Gibbs /* Fill out a communications ring structure. */ 225fac3fd80SJustin T. Gibbs ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 226fac3fd80SJustin T. Gibbs sc->xbd_ring.req_prod_pvt++; 227fac3fd80SJustin T. Gibbs ring_req->id = cm->cm_id; 228fac3fd80SJustin T. Gibbs ring_req->operation = cm->cm_operation; 229fac3fd80SJustin T. Gibbs ring_req->sector_number = cm->cm_sector_number; 230fac3fd80SJustin T. Gibbs ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 231fac3fd80SJustin T. Gibbs ring_req->nr_segments = nsegs; 232fac3fd80SJustin T. Gibbs cm->cm_nseg = nsegs; 233*d0ecc14dSColin Percival mksegarray(segs, nsegs, &cm->cm_gref_head, 234fac3fd80SJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), 235*d0ecc14dSColin Percival cm->cm_operation == BLKIF_OP_WRITE, 236*d0ecc14dSColin Percival cm->cm_sg_refs, ring_req->seg); 237fac3fd80SJustin T. Gibbs 238fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 239fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREREAD; 240fac3fd80SJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE) 241fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_PREWRITE; 242fac3fd80SJustin T. Gibbs else 243fac3fd80SJustin T. Gibbs op = 0; 244fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 245fac3fd80SJustin T. Gibbs 246fac3fd80SJustin T. Gibbs gnttab_free_grant_references(cm->cm_gref_head); 247fac3fd80SJustin T. Gibbs 248e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_BUSY); 249fac3fd80SJustin T. Gibbs 250fac3fd80SJustin T. Gibbs /* 251127a9483SJustin T. Gibbs * If bus dma had to asynchronously call us back to dispatch 252127a9483SJustin T. Gibbs * this command, we are no longer executing in the context of 253127a9483SJustin T. Gibbs * xbd_startio(). Thus we cannot rely on xbd_startio()'s call to 254127a9483SJustin T. Gibbs * xbd_flush_requests() to publish this command to the backend 255127a9483SJustin T. Gibbs * along with any other commands that it could batch. 256fac3fd80SJustin T. Gibbs */ 257127a9483SJustin T. Gibbs if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0) 258cdf5d66fSJustin T. Gibbs xbd_flush_requests(sc); 259fac3fd80SJustin T. Gibbs 260fac3fd80SJustin T. Gibbs return; 261fac3fd80SJustin T. Gibbs } 262fac3fd80SJustin T. Gibbs 263fac3fd80SJustin T. Gibbs static int 264fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm) 265fac3fd80SJustin T. Gibbs { 266fac3fd80SJustin T. Gibbs int error; 267fac3fd80SJustin T. Gibbs 26877b6916dSRoger Pau Monné error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data, 26977b6916dSRoger Pau Monné cm->cm_datalen, xbd_queue_cb, cm, 0); 270fac3fd80SJustin T. Gibbs if (error == EINPROGRESS) { 271127a9483SJustin T. Gibbs /* 272127a9483SJustin T. Gibbs * Maintain queuing order by freezing the queue. The next 273127a9483SJustin T. Gibbs * command may not require as many resources as the command 274127a9483SJustin T. Gibbs * we just attempted to map, so we can't rely on bus dma 275127a9483SJustin T. Gibbs * blocking for it too. 276127a9483SJustin T. Gibbs */ 2779985113bSJustin T. Gibbs xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING); 278fac3fd80SJustin T. Gibbs return (0); 279fac3fd80SJustin T. Gibbs } 280fac3fd80SJustin T. Gibbs 281fac3fd80SJustin T. Gibbs return (error); 282fac3fd80SJustin T. Gibbs } 283fac3fd80SJustin T. Gibbs 284fac3fd80SJustin T. Gibbs static void 285fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg) 286fac3fd80SJustin T. Gibbs { 287fac3fd80SJustin T. Gibbs struct xbd_softc *sc = arg; 288fac3fd80SJustin T. Gibbs 28933eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 290e4808c4bSKip Macy 291127a9483SJustin T. Gibbs xbd_thaw(sc, XBDF_GNT_SHORTAGE); 292127a9483SJustin T. Gibbs 29333eebb6aSJustin T. Gibbs xbd_startio(sc); 294e4808c4bSKip Macy 29533eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 296fac3fd80SJustin T. Gibbs } 297fac3fd80SJustin T. Gibbs 298fac3fd80SJustin T. Gibbs static struct xbd_command * 299fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc) 300fac3fd80SJustin T. Gibbs { 301fac3fd80SJustin T. Gibbs struct xbd_command *cm; 302fac3fd80SJustin T. Gibbs struct bio *bp; 303fac3fd80SJustin T. Gibbs 30476acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED)) 305fac3fd80SJustin T. Gibbs return (NULL); 306fac3fd80SJustin T. Gibbs 307fac3fd80SJustin T. Gibbs bp = xbd_dequeue_bio(sc); 308fac3fd80SJustin T. Gibbs if (bp == NULL) 309fac3fd80SJustin T. Gibbs return (NULL); 310fac3fd80SJustin T. Gibbs 311e2c1fe90SJustin T. Gibbs if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) { 312127a9483SJustin T. Gibbs xbd_freeze(sc, XBDF_CM_SHORTAGE); 313fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 314fac3fd80SJustin T. Gibbs return (NULL); 315fac3fd80SJustin T. Gibbs } 316fac3fd80SJustin T. Gibbs 317fac3fd80SJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 318fac3fd80SJustin T. Gibbs &cm->cm_gref_head) != 0) { 319fac3fd80SJustin T. Gibbs gnttab_request_free_callback(&sc->xbd_callback, 320fac3fd80SJustin T. Gibbs xbd_restart_queue_callback, sc, 321fac3fd80SJustin T. Gibbs sc->xbd_max_request_segments); 322127a9483SJustin T. Gibbs xbd_freeze(sc, XBDF_GNT_SHORTAGE); 323fac3fd80SJustin T. Gibbs xbd_requeue_bio(sc, bp); 324e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_FREE); 325fac3fd80SJustin T. Gibbs return (NULL); 326fac3fd80SJustin T. Gibbs } 327fac3fd80SJustin T. Gibbs 328fac3fd80SJustin T. Gibbs cm->cm_bp = bp; 32977b6916dSRoger Pau Monné cm->cm_data = bp->bio_data; 33077b6916dSRoger Pau Monné cm->cm_datalen = bp->bio_bcount; 331fac3fd80SJustin T. Gibbs cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno; 332fac3fd80SJustin T. Gibbs 3339985113bSJustin T. Gibbs switch (bp->bio_cmd) { 3349985113bSJustin T. Gibbs case BIO_READ: 3359985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_READ; 3369985113bSJustin T. Gibbs break; 3379985113bSJustin T. Gibbs case BIO_WRITE: 3389985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE; 3399985113bSJustin T. Gibbs if ((bp->bio_flags & BIO_ORDERED) != 0) { 3409985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 3419985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 3429985113bSJustin T. Gibbs } else { 3439985113bSJustin T. Gibbs /* 3449985113bSJustin T. Gibbs * Single step this command. 3459985113bSJustin T. Gibbs */ 3469985113bSJustin T. Gibbs cm->cm_flags |= XBDCF_Q_FREEZE; 3479985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 3489985113bSJustin T. Gibbs /* 3499985113bSJustin T. Gibbs * Wait for in-flight requests to 3509985113bSJustin T. Gibbs * finish. 3519985113bSJustin T. Gibbs */ 3529985113bSJustin T. Gibbs xbd_freeze(sc, XBDF_WAIT_IDLE); 3539985113bSJustin T. Gibbs xbd_requeue_cm(cm, XBD_Q_READY); 3549985113bSJustin T. Gibbs return (NULL); 3559985113bSJustin T. Gibbs } 3569985113bSJustin T. Gibbs } 3579985113bSJustin T. Gibbs } 3589985113bSJustin T. Gibbs break; 3599985113bSJustin T. Gibbs case BIO_FLUSH: 3609985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_FLUSH) != 0) 3619985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE; 3629985113bSJustin T. Gibbs else if ((sc->xbd_flags & XBDF_BARRIER) != 0) 3639985113bSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 3649985113bSJustin T. Gibbs else 3659985113bSJustin T. Gibbs panic("flush request, but no flush support available"); 3669985113bSJustin T. Gibbs break; 3679985113bSJustin T. Gibbs default: 3689985113bSJustin T. Gibbs panic("unknown bio command %d", bp->bio_cmd); 3699985113bSJustin T. Gibbs } 3709985113bSJustin T. Gibbs 371fac3fd80SJustin T. Gibbs return (cm); 372fac3fd80SJustin T. Gibbs } 373fac3fd80SJustin T. Gibbs 374fac3fd80SJustin T. Gibbs /* 375fac3fd80SJustin T. Gibbs * Dequeue buffers and place them in the shared communication ring. 376fac3fd80SJustin T. Gibbs * Return when no more requests can be accepted or all buffers have 377fac3fd80SJustin T. Gibbs * been queued. 378fac3fd80SJustin T. Gibbs * 379fac3fd80SJustin T. Gibbs * Signal XEN once the ring has been filled out. 380fac3fd80SJustin T. Gibbs */ 381fac3fd80SJustin T. Gibbs static void 382fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc) 383fac3fd80SJustin T. Gibbs { 384fac3fd80SJustin T. Gibbs struct xbd_command *cm; 385fac3fd80SJustin T. Gibbs int error, queued = 0; 386fac3fd80SJustin T. Gibbs 387fac3fd80SJustin T. Gibbs mtx_assert(&sc->xbd_io_lock, MA_OWNED); 388fac3fd80SJustin T. Gibbs 389e2c1fe90SJustin T. Gibbs if (sc->xbd_state != XBD_STATE_CONNECTED) 390e4808c4bSKip Macy return; 391fac3fd80SJustin T. Gibbs 392112cacaeSRoger Pau Monné while (!RING_FULL(&sc->xbd_ring)) { 393112cacaeSRoger Pau Monné 394127a9483SJustin T. Gibbs if (sc->xbd_qfrozen_cnt != 0) 395fac3fd80SJustin T. Gibbs break; 396fac3fd80SJustin T. Gibbs 397e2c1fe90SJustin T. Gibbs cm = xbd_dequeue_cm(sc, XBD_Q_READY); 398fac3fd80SJustin T. Gibbs 399fac3fd80SJustin T. Gibbs if (cm == NULL) 400fac3fd80SJustin T. Gibbs cm = xbd_bio_command(sc); 401fac3fd80SJustin T. Gibbs 402fac3fd80SJustin T. Gibbs if (cm == NULL) 403fac3fd80SJustin T. Gibbs break; 404fac3fd80SJustin T. Gibbs 4059985113bSJustin T. Gibbs if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) { 4069985113bSJustin T. Gibbs /* 4079985113bSJustin T. Gibbs * Single step command. Future work is 4089985113bSJustin T. Gibbs * held off until this command completes. 4099985113bSJustin T. Gibbs */ 4109985113bSJustin T. Gibbs xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE); 4119985113bSJustin T. Gibbs } 4129985113bSJustin T. Gibbs 413fac3fd80SJustin T. Gibbs if ((error = xbd_queue_request(sc, cm)) != 0) { 414fac3fd80SJustin T. Gibbs printf("xbd_queue_request returned %d\n", error); 415fac3fd80SJustin T. Gibbs break; 416fac3fd80SJustin T. Gibbs } 417fac3fd80SJustin T. Gibbs queued++; 418fac3fd80SJustin T. Gibbs } 419fac3fd80SJustin T. Gibbs 420fac3fd80SJustin T. Gibbs if (queued != 0) 421cdf5d66fSJustin T. Gibbs xbd_flush_requests(sc); 422e4808c4bSKip Macy } 423e4808c4bSKip Macy 424e4808c4bSKip Macy static void 42533eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm) 426e4808c4bSKip Macy { 427e4808c4bSKip Macy struct bio *bp; 428e4808c4bSKip Macy 42933eebb6aSJustin T. Gibbs bp = cm->cm_bp; 430e4808c4bSKip Macy 43176acc41fSJustin T. Gibbs if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) { 432e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 43333eebb6aSJustin T. Gibbs printf(" status: %x\n", cm->cm_status); 434e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 435e4808c4bSKip Macy } 436e4808c4bSKip Macy 437e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 438e4808c4bSKip Macy bp->bio_error = EIO; 439e4808c4bSKip Macy else 440e4808c4bSKip Macy bp->bio_resid = 0; 441e4808c4bSKip Macy 44233eebb6aSJustin T. Gibbs xbd_free_command(cm); 443e4808c4bSKip Macy biodone(bp); 444e4808c4bSKip Macy } 445e4808c4bSKip Macy 446fac3fd80SJustin T. Gibbs static void 447fac3fd80SJustin T. Gibbs xbd_int(void *xsc) 448fac3fd80SJustin T. Gibbs { 449fac3fd80SJustin T. Gibbs struct xbd_softc *sc = xsc; 450fac3fd80SJustin T. Gibbs struct xbd_command *cm; 451fac3fd80SJustin T. Gibbs blkif_response_t *bret; 452fac3fd80SJustin T. Gibbs RING_IDX i, rp; 453fac3fd80SJustin T. Gibbs int op; 454fac3fd80SJustin T. Gibbs 455fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 456fac3fd80SJustin T. Gibbs 45776acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) { 458fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 459fac3fd80SJustin T. Gibbs return; 460fac3fd80SJustin T. Gibbs } 461fac3fd80SJustin T. Gibbs 462fac3fd80SJustin T. Gibbs again: 463fac3fd80SJustin T. Gibbs rp = sc->xbd_ring.sring->rsp_prod; 464fac3fd80SJustin T. Gibbs rmb(); /* Ensure we see queued responses up to 'rp'. */ 465fac3fd80SJustin T. Gibbs 466fac3fd80SJustin T. Gibbs for (i = sc->xbd_ring.rsp_cons; i != rp;) { 467fac3fd80SJustin T. Gibbs bret = RING_GET_RESPONSE(&sc->xbd_ring, i); 468fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[bret->id]; 469fac3fd80SJustin T. Gibbs 470e2c1fe90SJustin T. Gibbs xbd_remove_cm(cm, XBD_Q_BUSY); 471112cacaeSRoger Pau Monné gnttab_end_foreign_access_references(cm->cm_nseg, 472112cacaeSRoger Pau Monné cm->cm_sg_refs); 473112cacaeSRoger Pau Monné i++; 474fac3fd80SJustin T. Gibbs 475fac3fd80SJustin T. Gibbs if (cm->cm_operation == BLKIF_OP_READ) 476fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTREAD; 4779985113bSJustin T. Gibbs else if (cm->cm_operation == BLKIF_OP_WRITE || 4789985113bSJustin T. Gibbs cm->cm_operation == BLKIF_OP_WRITE_BARRIER) 479fac3fd80SJustin T. Gibbs op = BUS_DMASYNC_POSTWRITE; 480fac3fd80SJustin T. Gibbs else 481fac3fd80SJustin T. Gibbs op = 0; 482fac3fd80SJustin T. Gibbs bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 483fac3fd80SJustin T. Gibbs bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map); 484fac3fd80SJustin T. Gibbs 485fac3fd80SJustin T. Gibbs /* 486127a9483SJustin T. Gibbs * Release any hold this command has on future command 487127a9483SJustin T. Gibbs * dispatch. 488fac3fd80SJustin T. Gibbs */ 4899985113bSJustin T. Gibbs xbd_cm_thaw(sc, cm); 490fac3fd80SJustin T. Gibbs 491fac3fd80SJustin T. Gibbs /* 492fac3fd80SJustin T. Gibbs * Directly call the i/o complete routine to save an 493fac3fd80SJustin T. Gibbs * an indirection in the common case. 494fac3fd80SJustin T. Gibbs */ 495fac3fd80SJustin T. Gibbs cm->cm_status = bret->status; 496fac3fd80SJustin T. Gibbs if (cm->cm_bp) 497fac3fd80SJustin T. Gibbs xbd_bio_complete(sc, cm); 498fac3fd80SJustin T. Gibbs else if (cm->cm_complete != NULL) 499fac3fd80SJustin T. Gibbs cm->cm_complete(cm); 500fac3fd80SJustin T. Gibbs else 501fac3fd80SJustin T. Gibbs xbd_free_command(cm); 502fac3fd80SJustin T. Gibbs } 503fac3fd80SJustin T. Gibbs 504fac3fd80SJustin T. Gibbs sc->xbd_ring.rsp_cons = i; 505fac3fd80SJustin T. Gibbs 506fac3fd80SJustin T. Gibbs if (i != sc->xbd_ring.req_prod_pvt) { 507fac3fd80SJustin T. Gibbs int more_to_do; 508fac3fd80SJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do); 509fac3fd80SJustin T. Gibbs if (more_to_do) 510fac3fd80SJustin T. Gibbs goto again; 511fac3fd80SJustin T. Gibbs } else { 512fac3fd80SJustin T. Gibbs sc->xbd_ring.sring->rsp_event = i + 1; 513fac3fd80SJustin T. Gibbs } 514fac3fd80SJustin T. Gibbs 5159985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) == 0) 5169985113bSJustin T. Gibbs xbd_thaw(sc, XBDF_WAIT_IDLE); 5179985113bSJustin T. Gibbs 518fac3fd80SJustin T. Gibbs xbd_startio(sc); 519fac3fd80SJustin T. Gibbs 52076acc41fSJustin T. Gibbs if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED)) 521e2c1fe90SJustin T. Gibbs wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]); 522fac3fd80SJustin T. Gibbs 523fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 524fac3fd80SJustin T. Gibbs } 525fac3fd80SJustin T. Gibbs 526fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/ 527fac3fd80SJustin T. Gibbs /** 528fac3fd80SJustin T. Gibbs * Quiesce the disk writes for a dump file before allowing the next buffer. 529fac3fd80SJustin T. Gibbs */ 5308698b76cSKip Macy static void 53133eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc) 5328698b76cSKip Macy { 5338698b76cSKip Macy int mtd; 5348698b76cSKip Macy 5358698b76cSKip Macy // While there are outstanding requests 5369985113bSJustin T. Gibbs while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 53733eebb6aSJustin T. Gibbs RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd); 5388698b76cSKip Macy if (mtd) { 539e4808c4bSKip Macy /* Recieved request completions, update queue. */ 54033eebb6aSJustin T. Gibbs xbd_int(sc); 5418698b76cSKip Macy } 5429985113bSJustin T. Gibbs if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 543e4808c4bSKip Macy /* 544e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 545e4808c4bSKip Macy * to complete. 546e4808c4bSKip Macy */ 547be7747b4SKip Macy HYPERVISOR_yield(); 5488698b76cSKip Macy } 5498698b76cSKip Macy } 5508698b76cSKip Macy } 5518698b76cSKip Macy 552e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 553e4808c4bSKip Macy static void 55433eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm) 555e4808c4bSKip Macy { 5568698b76cSKip Macy 557e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_COMPLETE); 558e4808c4bSKip Macy } 559e4808c4bSKip Macy 5608698b76cSKip Macy static int 56133eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 5628698b76cSKip Macy size_t length) 5638698b76cSKip Macy { 5648698b76cSKip Macy struct disk *dp = arg; 56533eebb6aSJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 56633eebb6aSJustin T. Gibbs struct xbd_command *cm; 567e4808c4bSKip Macy size_t chunk; 568e4808c4bSKip Macy int sbp; 5698698b76cSKip Macy int rc = 0; 5708698b76cSKip Macy 571e4808c4bSKip Macy if (length <= 0) 572e4808c4bSKip Macy return (rc); 5738698b76cSKip Macy 57433eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quiet on the western front. */ 575e4808c4bSKip Macy 576e4808c4bSKip Macy /* 577e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 578e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 579e4808c4bSKip Macy */ 58033eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 581e4808c4bSKip Macy 582e4808c4bSKip Macy /* Split the 64KB block as needed */ 583e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 584e2c1fe90SJustin T. Gibbs cm = xbd_dequeue_cm(sc, XBD_Q_FREE); 585e4808c4bSKip Macy if (cm == NULL) { 58633eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 58733eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "dump: no more commands?\n"); 588e4808c4bSKip Macy return (EBUSY); 589e4808c4bSKip Macy } 590e4808c4bSKip Macy 59133eebb6aSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 59233eebb6aSJustin T. Gibbs &cm->cm_gref_head) != 0) { 59333eebb6aSJustin T. Gibbs xbd_free_command(cm); 59433eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 59533eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, "no more grant allocs?\n"); 596e4808c4bSKip Macy return (EBUSY); 597e4808c4bSKip Macy } 598e4808c4bSKip Macy 59933eebb6aSJustin T. Gibbs chunk = length > sc->xbd_max_request_size ? 60033eebb6aSJustin T. Gibbs sc->xbd_max_request_size : length; 60133eebb6aSJustin T. Gibbs cm->cm_data = virtual; 60233eebb6aSJustin T. Gibbs cm->cm_datalen = chunk; 60333eebb6aSJustin T. Gibbs cm->cm_operation = BLKIF_OP_WRITE; 60433eebb6aSJustin T. Gibbs cm->cm_sector_number = offset / dp->d_sectorsize; 60533eebb6aSJustin T. Gibbs cm->cm_complete = xbd_dump_complete; 606e4808c4bSKip Macy 607e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(cm, XBD_Q_READY); 6088698b76cSKip Macy 6098698b76cSKip Macy length -= chunk; 6108698b76cSKip Macy offset += chunk; 6118698b76cSKip Macy virtual = (char *) virtual + chunk; 6128698b76cSKip Macy } 6138698b76cSKip Macy 614e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 61533eebb6aSJustin T. Gibbs xbd_startio(sc); 61633eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 617e4808c4bSKip Macy 618e4808c4bSKip Macy /* Poll for the completion. */ 61933eebb6aSJustin T. Gibbs xbd_quiesce(sc); /* All quite on the eastern front */ 620e4808c4bSKip Macy 621e4808c4bSKip Macy /* If there were any errors, bail out... */ 622e2c1fe90SJustin T. Gibbs while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) { 62333eebb6aSJustin T. Gibbs if (cm->cm_status != BLKIF_RSP_OKAY) { 62433eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 625e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 62633eebb6aSJustin T. Gibbs cm->cm_sector_number); 627e4808c4bSKip Macy rc = EIO; 6288698b76cSKip Macy } 62933eebb6aSJustin T. Gibbs xbd_free_command(cm); 6308698b76cSKip Macy } 631e4808c4bSKip Macy 6328698b76cSKip Macy return (rc); 6338698b76cSKip Macy } 6348698b76cSKip Macy 635fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/ 63623dc5621SKip Macy static int 637fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp) 63889e0f4d2SKip Macy { 639fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 64023dc5621SKip Macy 641fac3fd80SJustin T. Gibbs if (sc == NULL) { 642fac3fd80SJustin T. Gibbs printf("xb%d: not found", sc->xbd_unit); 64323dc5621SKip Macy return (ENXIO); 64423dc5621SKip Macy } 64523dc5621SKip Macy 646e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_OPEN; 647fac3fd80SJustin T. Gibbs sc->xbd_users++; 648fac3fd80SJustin T. Gibbs return (0); 649fac3fd80SJustin T. Gibbs } 650fac3fd80SJustin T. Gibbs 651fac3fd80SJustin T. Gibbs static int 652fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp) 653fac3fd80SJustin T. Gibbs { 654fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 655fac3fd80SJustin T. Gibbs 656fac3fd80SJustin T. Gibbs if (sc == NULL) 657fac3fd80SJustin T. Gibbs return (ENXIO); 658e2c1fe90SJustin T. Gibbs sc->xbd_flags &= ~XBDF_OPEN; 659fac3fd80SJustin T. Gibbs if (--(sc->xbd_users) == 0) { 660fac3fd80SJustin T. Gibbs /* 661fac3fd80SJustin T. Gibbs * Check whether we have been instructed to close. We will 662fac3fd80SJustin T. Gibbs * have ignored this request initially, as the device was 663fac3fd80SJustin T. Gibbs * still mounted. 664fac3fd80SJustin T. Gibbs */ 665fac3fd80SJustin T. Gibbs if (xenbus_get_otherend_state(sc->xbd_dev) == 666fac3fd80SJustin T. Gibbs XenbusStateClosing) 667fac3fd80SJustin T. Gibbs xbd_closing(sc->xbd_dev); 668fac3fd80SJustin T. Gibbs } 669fac3fd80SJustin T. Gibbs return (0); 670fac3fd80SJustin T. Gibbs } 671fac3fd80SJustin T. Gibbs 672fac3fd80SJustin T. Gibbs static int 673fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 674fac3fd80SJustin T. Gibbs { 675fac3fd80SJustin T. Gibbs struct xbd_softc *sc = dp->d_drv1; 676fac3fd80SJustin T. Gibbs 677fac3fd80SJustin T. Gibbs if (sc == NULL) 678fac3fd80SJustin T. Gibbs return (ENXIO); 679fac3fd80SJustin T. Gibbs 680fac3fd80SJustin T. Gibbs return (ENOTTY); 681fac3fd80SJustin T. Gibbs } 682fac3fd80SJustin T. Gibbs 683fac3fd80SJustin T. Gibbs /* 684fac3fd80SJustin T. Gibbs * Read/write routine for a buffer. Finds the proper unit, place it on 685fac3fd80SJustin T. Gibbs * the sortq and kick the controller. 686fac3fd80SJustin T. Gibbs */ 687fac3fd80SJustin T. Gibbs static void 688fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp) 689fac3fd80SJustin T. Gibbs { 690fac3fd80SJustin T. Gibbs struct xbd_softc *sc = bp->bio_disk->d_drv1; 691fac3fd80SJustin T. Gibbs 692fac3fd80SJustin T. Gibbs /* bogus disk? */ 693fac3fd80SJustin T. Gibbs if (sc == NULL) { 694fac3fd80SJustin T. Gibbs bp->bio_error = EINVAL; 695fac3fd80SJustin T. Gibbs bp->bio_flags |= BIO_ERROR; 696fac3fd80SJustin T. Gibbs bp->bio_resid = bp->bio_bcount; 697fac3fd80SJustin T. Gibbs biodone(bp); 698fac3fd80SJustin T. Gibbs return; 699fac3fd80SJustin T. Gibbs } 700fac3fd80SJustin T. Gibbs 701fac3fd80SJustin T. Gibbs /* 702fac3fd80SJustin T. Gibbs * Place it in the queue of disk activities for this disk 703fac3fd80SJustin T. Gibbs */ 704fac3fd80SJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 705fac3fd80SJustin T. Gibbs 706fac3fd80SJustin T. Gibbs xbd_enqueue_bio(sc, bp); 707fac3fd80SJustin T. Gibbs xbd_startio(sc); 708fac3fd80SJustin T. Gibbs 709fac3fd80SJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 710fac3fd80SJustin T. Gibbs return; 711fac3fd80SJustin T. Gibbs } 712fac3fd80SJustin T. Gibbs 713fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/ 714fac3fd80SJustin T. Gibbs static int 715cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc) 716fac3fd80SJustin T. Gibbs { 717fac3fd80SJustin T. Gibbs blkif_sring_t *sring; 718fac3fd80SJustin T. Gibbs uintptr_t sring_page_addr; 719fac3fd80SJustin T. Gibbs int error; 720fac3fd80SJustin T. Gibbs int i; 721fac3fd80SJustin T. Gibbs 722fac3fd80SJustin T. Gibbs sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 723fac3fd80SJustin T. Gibbs M_NOWAIT|M_ZERO); 724fac3fd80SJustin T. Gibbs if (sring == NULL) { 725fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring"); 726fac3fd80SJustin T. Gibbs return (ENOMEM); 727fac3fd80SJustin T. Gibbs } 728fac3fd80SJustin T. Gibbs SHARED_RING_INIT(sring); 729fac3fd80SJustin T. Gibbs FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE); 730fac3fd80SJustin T. Gibbs 731fac3fd80SJustin T. Gibbs for (i = 0, sring_page_addr = (uintptr_t)sring; 732fac3fd80SJustin T. Gibbs i < sc->xbd_ring_pages; 733fac3fd80SJustin T. Gibbs i++, sring_page_addr += PAGE_SIZE) { 734fac3fd80SJustin T. Gibbs 735fac3fd80SJustin T. Gibbs error = xenbus_grant_ring(sc->xbd_dev, 736fac3fd80SJustin T. Gibbs (vtomach(sring_page_addr) >> PAGE_SHIFT), 737fac3fd80SJustin T. Gibbs &sc->xbd_ring_ref[i]); 738fac3fd80SJustin T. Gibbs if (error) { 739fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 740fac3fd80SJustin T. Gibbs "granting ring_ref(%d)", i); 741fac3fd80SJustin T. Gibbs return (error); 742fac3fd80SJustin T. Gibbs } 743fac3fd80SJustin T. Gibbs } 744fac3fd80SJustin T. Gibbs if (sc->xbd_ring_pages == 1) { 745fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 746fac3fd80SJustin T. Gibbs "ring-ref", "%u", sc->xbd_ring_ref[0]); 747fac3fd80SJustin T. Gibbs if (error) { 748fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 749fac3fd80SJustin T. Gibbs "writing %s/ring-ref", 750fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev)); 751fac3fd80SJustin T. Gibbs return (error); 752fac3fd80SJustin T. Gibbs } 753fac3fd80SJustin T. Gibbs } else { 754fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 755fac3fd80SJustin T. Gibbs char ring_ref_name[]= "ring_refXX"; 756fac3fd80SJustin T. Gibbs 757fac3fd80SJustin T. Gibbs snprintf(ring_ref_name, sizeof(ring_ref_name), 758fac3fd80SJustin T. Gibbs "ring-ref%u", i); 759fac3fd80SJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 760fac3fd80SJustin T. Gibbs ring_ref_name, "%u", sc->xbd_ring_ref[i]); 761fac3fd80SJustin T. Gibbs if (error) { 762fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 763fac3fd80SJustin T. Gibbs "writing %s/%s", 764fac3fd80SJustin T. Gibbs xenbus_get_node(sc->xbd_dev), 765fac3fd80SJustin T. Gibbs ring_ref_name); 766fac3fd80SJustin T. Gibbs return (error); 767fac3fd80SJustin T. Gibbs } 768fac3fd80SJustin T. Gibbs } 769fac3fd80SJustin T. Gibbs } 770fac3fd80SJustin T. Gibbs 77176acc41fSJustin T. Gibbs error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev, 77276acc41fSJustin T. Gibbs xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc, 77376acc41fSJustin T. Gibbs INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle); 774fac3fd80SJustin T. Gibbs if (error) { 775fac3fd80SJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 77676acc41fSJustin T. Gibbs "xen_intr_alloc_and_bind_local_port failed"); 777fac3fd80SJustin T. Gibbs return (error); 778fac3fd80SJustin T. Gibbs } 779fac3fd80SJustin T. Gibbs 780fac3fd80SJustin T. Gibbs return (0); 781fac3fd80SJustin T. Gibbs } 782fac3fd80SJustin T. Gibbs 783d9fab01dSJustin T. Gibbs static void 784d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc) 785d9fab01dSJustin T. Gibbs { 786d9fab01dSJustin T. Gibbs int i; 787d9fab01dSJustin T. Gibbs 788d9fab01dSJustin T. Gibbs if (sc->xbd_ring.sring == NULL) 789d9fab01dSJustin T. Gibbs return; 790d9fab01dSJustin T. Gibbs 791d9fab01dSJustin T. Gibbs for (i = 0; i < sc->xbd_ring_pages; i++) { 792d9fab01dSJustin T. Gibbs if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) { 793d9fab01dSJustin T. Gibbs gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]); 794d9fab01dSJustin T. Gibbs sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 795d9fab01dSJustin T. Gibbs } 796d9fab01dSJustin T. Gibbs } 797d9fab01dSJustin T. Gibbs free(sc->xbd_ring.sring, M_XENBLOCKFRONT); 798d9fab01dSJustin T. Gibbs sc->xbd_ring.sring = NULL; 799d9fab01dSJustin T. Gibbs } 800d9fab01dSJustin T. Gibbs 801fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/ 8029985113bSJustin T. Gibbs static int 8039985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len) 8049985113bSJustin T. Gibbs { 8059985113bSJustin T. Gibbs struct sbuf sb; 8069985113bSJustin T. Gibbs int feature_cnt; 8079985113bSJustin T. Gibbs 8089985113bSJustin T. Gibbs sbuf_new(&sb, features, len, SBUF_FIXEDLEN); 8099985113bSJustin T. Gibbs 8109985113bSJustin T. Gibbs feature_cnt = 0; 8119985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_FLUSH) != 0) { 8129985113bSJustin T. Gibbs sbuf_printf(&sb, "flush"); 8139985113bSJustin T. Gibbs feature_cnt++; 8149985113bSJustin T. Gibbs } 8159985113bSJustin T. Gibbs 8169985113bSJustin T. Gibbs if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 8179985113bSJustin T. Gibbs if (feature_cnt != 0) 8189985113bSJustin T. Gibbs sbuf_printf(&sb, ", "); 8199985113bSJustin T. Gibbs sbuf_printf(&sb, "write_barrier"); 8209985113bSJustin T. Gibbs feature_cnt++; 8219985113bSJustin T. Gibbs } 8229985113bSJustin T. Gibbs 8239985113bSJustin T. Gibbs (void) sbuf_finish(&sb); 8249985113bSJustin T. Gibbs return (sbuf_len(&sb)); 8259985113bSJustin T. Gibbs } 8269985113bSJustin T. Gibbs 8279985113bSJustin T. Gibbs static int 8289985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS) 8299985113bSJustin T. Gibbs { 8309985113bSJustin T. Gibbs char features[80]; 8319985113bSJustin T. Gibbs struct xbd_softc *sc = arg1; 8329985113bSJustin T. Gibbs int error; 8339985113bSJustin T. Gibbs int len; 8349985113bSJustin T. Gibbs 8359985113bSJustin T. Gibbs error = sysctl_wire_old_buffer(req, 0); 8369985113bSJustin T. Gibbs if (error != 0) 8379985113bSJustin T. Gibbs return (error); 8389985113bSJustin T. Gibbs 8399985113bSJustin T. Gibbs len = xbd_feature_string(sc, features, sizeof(features)); 8409985113bSJustin T. Gibbs 8419985113bSJustin T. Gibbs /* len is -1 on error, which will make the SYSCTL_OUT a no-op. */ 8429985113bSJustin T. Gibbs return (SYSCTL_OUT(req, features, len + 1/*NUL*/)); 8439985113bSJustin T. Gibbs } 8449985113bSJustin T. Gibbs 8458b8bfa35SJustin T. Gibbs static void 84633eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd) 8478b8bfa35SJustin T. Gibbs { 8488b8bfa35SJustin T. Gibbs struct sysctl_ctx_list *sysctl_ctx = NULL; 8498b8bfa35SJustin T. Gibbs struct sysctl_oid *sysctl_tree = NULL; 8509985113bSJustin T. Gibbs struct sysctl_oid_list *children; 8518b8bfa35SJustin T. Gibbs 85233eebb6aSJustin T. Gibbs sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev); 8538b8bfa35SJustin T. Gibbs if (sysctl_ctx == NULL) 8548b8bfa35SJustin T. Gibbs return; 8558b8bfa35SJustin T. Gibbs 85633eebb6aSJustin T. Gibbs sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev); 8578b8bfa35SJustin T. Gibbs if (sysctl_tree == NULL) 8588b8bfa35SJustin T. Gibbs return; 8598b8bfa35SJustin T. Gibbs 8609985113bSJustin T. Gibbs children = SYSCTL_CHILDREN(sysctl_tree); 8619985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 86233eebb6aSJustin T. Gibbs "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1, 8638b8bfa35SJustin T. Gibbs "maximum outstanding requests (negotiated)"); 8648b8bfa35SJustin T. Gibbs 8659985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 8668b8bfa35SJustin T. Gibbs "max_request_segments", CTLFLAG_RD, 86733eebb6aSJustin T. Gibbs &xbd->xbd_max_request_segments, 0, 8688b8bfa35SJustin T. Gibbs "maximum number of pages per requests (negotiated)"); 8698b8bfa35SJustin T. Gibbs 8709985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 87133eebb6aSJustin T. Gibbs "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0, 8728b8bfa35SJustin T. Gibbs "maximum size in bytes of a request (negotiated)"); 8738b8bfa35SJustin T. Gibbs 8749985113bSJustin T. Gibbs SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 87533eebb6aSJustin T. Gibbs "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0, 8768b8bfa35SJustin T. Gibbs "communication channel pages (negotiated)"); 8779985113bSJustin T. Gibbs 8789985113bSJustin T. Gibbs SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO, 8799985113bSJustin T. Gibbs "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0, 8809985113bSJustin T. Gibbs xbd_sysctl_features, "A", "protocol features (negotiated)"); 8818b8bfa35SJustin T. Gibbs } 8828b8bfa35SJustin T. Gibbs 88323dc5621SKip Macy /* 884fac3fd80SJustin T. Gibbs * Translate Linux major/minor to an appropriate name and unit 885fac3fd80SJustin T. Gibbs * number. For HVM guests, this allows us to use the same drive names 886fac3fd80SJustin T. Gibbs * with blkfront as the emulated drives, easing transition slightly. 88723dc5621SKip Macy */ 888fac3fd80SJustin T. Gibbs static void 889fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name) 89023dc5621SKip Macy { 891fac3fd80SJustin T. Gibbs static struct vdev_info { 892fac3fd80SJustin T. Gibbs int major; 893fac3fd80SJustin T. Gibbs int shift; 894fac3fd80SJustin T. Gibbs int base; 89523dc5621SKip Macy const char *name; 896fac3fd80SJustin T. Gibbs } info[] = { 897fac3fd80SJustin T. Gibbs {3, 6, 0, "ada"}, /* ide0 */ 898fac3fd80SJustin T. Gibbs {22, 6, 2, "ada"}, /* ide1 */ 899fac3fd80SJustin T. Gibbs {33, 6, 4, "ada"}, /* ide2 */ 900fac3fd80SJustin T. Gibbs {34, 6, 6, "ada"}, /* ide3 */ 901fac3fd80SJustin T. Gibbs {56, 6, 8, "ada"}, /* ide4 */ 902fac3fd80SJustin T. Gibbs {57, 6, 10, "ada"}, /* ide5 */ 903fac3fd80SJustin T. Gibbs {88, 6, 12, "ada"}, /* ide6 */ 904fac3fd80SJustin T. Gibbs {89, 6, 14, "ada"}, /* ide7 */ 905fac3fd80SJustin T. Gibbs {90, 6, 16, "ada"}, /* ide8 */ 906fac3fd80SJustin T. Gibbs {91, 6, 18, "ada"}, /* ide9 */ 90789e0f4d2SKip Macy 908fac3fd80SJustin T. Gibbs {8, 4, 0, "da"}, /* scsi disk0 */ 909fac3fd80SJustin T. Gibbs {65, 4, 16, "da"}, /* scsi disk1 */ 910fac3fd80SJustin T. Gibbs {66, 4, 32, "da"}, /* scsi disk2 */ 911fac3fd80SJustin T. Gibbs {67, 4, 48, "da"}, /* scsi disk3 */ 912fac3fd80SJustin T. Gibbs {68, 4, 64, "da"}, /* scsi disk4 */ 913fac3fd80SJustin T. Gibbs {69, 4, 80, "da"}, /* scsi disk5 */ 914fac3fd80SJustin T. Gibbs {70, 4, 96, "da"}, /* scsi disk6 */ 915fac3fd80SJustin T. Gibbs {71, 4, 112, "da"}, /* scsi disk7 */ 916fac3fd80SJustin T. Gibbs {128, 4, 128, "da"}, /* scsi disk8 */ 917fac3fd80SJustin T. Gibbs {129, 4, 144, "da"}, /* scsi disk9 */ 918fac3fd80SJustin T. Gibbs {130, 4, 160, "da"}, /* scsi disk10 */ 919fac3fd80SJustin T. Gibbs {131, 4, 176, "da"}, /* scsi disk11 */ 920fac3fd80SJustin T. Gibbs {132, 4, 192, "da"}, /* scsi disk12 */ 921fac3fd80SJustin T. Gibbs {133, 4, 208, "da"}, /* scsi disk13 */ 922fac3fd80SJustin T. Gibbs {134, 4, 224, "da"}, /* scsi disk14 */ 923fac3fd80SJustin T. Gibbs {135, 4, 240, "da"}, /* scsi disk15 */ 924fac3fd80SJustin T. Gibbs 925fac3fd80SJustin T. Gibbs {202, 4, 0, "xbd"}, /* xbd */ 926fac3fd80SJustin T. Gibbs 927fac3fd80SJustin T. Gibbs {0, 0, 0, NULL}, 928fac3fd80SJustin T. Gibbs }; 929fac3fd80SJustin T. Gibbs int major = vdevice >> 8; 930fac3fd80SJustin T. Gibbs int minor = vdevice & 0xff; 931fac3fd80SJustin T. Gibbs int i; 932fac3fd80SJustin T. Gibbs 933fac3fd80SJustin T. Gibbs if (vdevice & (1 << 28)) { 934fac3fd80SJustin T. Gibbs *unit = (vdevice & ((1 << 28) - 1)) >> 8; 935fac3fd80SJustin T. Gibbs *name = "xbd"; 936fac3fd80SJustin T. Gibbs return; 93789e0f4d2SKip Macy } 93889e0f4d2SKip Macy 939fac3fd80SJustin T. Gibbs for (i = 0; info[i].major; i++) { 940fac3fd80SJustin T. Gibbs if (info[i].major == major) { 941fac3fd80SJustin T. Gibbs *unit = info[i].base + (minor >> info[i].shift); 942fac3fd80SJustin T. Gibbs *name = info[i].name; 943fac3fd80SJustin T. Gibbs return; 944fac3fd80SJustin T. Gibbs } 945fac3fd80SJustin T. Gibbs } 946fac3fd80SJustin T. Gibbs 947fac3fd80SJustin T. Gibbs *unit = minor >> 4; 948fac3fd80SJustin T. Gibbs *name = "xbd"; 949fac3fd80SJustin T. Gibbs } 950fac3fd80SJustin T. Gibbs 951fac3fd80SJustin T. Gibbs int 952fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors, 953fac3fd80SJustin T. Gibbs int vdevice, uint16_t vdisk_info, unsigned long sector_size) 954fac3fd80SJustin T. Gibbs { 9559985113bSJustin T. Gibbs char features[80]; 956fac3fd80SJustin T. Gibbs int unit, error = 0; 957fac3fd80SJustin T. Gibbs const char *name; 958fac3fd80SJustin T. Gibbs 95933eebb6aSJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 96023dc5621SKip Macy 961fac3fd80SJustin T. Gibbs sc->xbd_unit = unit; 96289e0f4d2SKip Macy 9639985113bSJustin T. Gibbs if (strcmp(name, "xbd") != 0) 964fac3fd80SJustin T. Gibbs device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit); 96589e0f4d2SKip Macy 9669985113bSJustin T. Gibbs if (xbd_feature_string(sc, features, sizeof(features)) > 0) { 9679985113bSJustin T. Gibbs device_printf(sc->xbd_dev, "features: %s\n", 9689985113bSJustin T. Gibbs features); 9699985113bSJustin T. Gibbs } 9709985113bSJustin T. Gibbs 971fac3fd80SJustin T. Gibbs sc->xbd_disk = disk_alloc(); 972fac3fd80SJustin T. Gibbs sc->xbd_disk->d_unit = sc->xbd_unit; 973fac3fd80SJustin T. Gibbs sc->xbd_disk->d_open = xbd_open; 974fac3fd80SJustin T. Gibbs sc->xbd_disk->d_close = xbd_close; 975fac3fd80SJustin T. Gibbs sc->xbd_disk->d_ioctl = xbd_ioctl; 976fac3fd80SJustin T. Gibbs sc->xbd_disk->d_strategy = xbd_strategy; 977fac3fd80SJustin T. Gibbs sc->xbd_disk->d_dump = xbd_dump; 978fac3fd80SJustin T. Gibbs sc->xbd_disk->d_name = name; 979fac3fd80SJustin T. Gibbs sc->xbd_disk->d_drv1 = sc; 980fac3fd80SJustin T. Gibbs sc->xbd_disk->d_sectorsize = sector_size; 9818b8bfa35SJustin T. Gibbs 982fac3fd80SJustin T. Gibbs sc->xbd_disk->d_mediasize = sectors * sector_size; 983fac3fd80SJustin T. Gibbs sc->xbd_disk->d_maxsize = sc->xbd_max_request_size; 98477b6916dSRoger Pau Monné sc->xbd_disk->d_flags = 0; 9859985113bSJustin T. Gibbs if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) { 9869985113bSJustin T. Gibbs sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 9879985113bSJustin T. Gibbs device_printf(sc->xbd_dev, 9889985113bSJustin T. Gibbs "synchronize cache commands enabled.\n"); 9899985113bSJustin T. Gibbs } 990fac3fd80SJustin T. Gibbs disk_create(sc->xbd_disk, DISK_VERSION); 99112678024SDoug Rabson 992fac3fd80SJustin T. Gibbs return error; 99312678024SDoug Rabson } 99412678024SDoug Rabson 995fac3fd80SJustin T. Gibbs static void 996fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc) 99712678024SDoug Rabson { 998fac3fd80SJustin T. Gibbs int i; 99912678024SDoug Rabson 100012678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 100133eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1002e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_DISCONNECTED; 100333eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 100489e0f4d2SKip Macy 1005fac3fd80SJustin T. Gibbs /* Free resources associated with old device channel. */ 1006d9fab01dSJustin T. Gibbs xbd_free_ring(sc); 1007fac3fd80SJustin T. Gibbs if (sc->xbd_shadow) { 100889e0f4d2SKip Macy 1009fac3fd80SJustin T. Gibbs for (i = 0; i < sc->xbd_max_requests; i++) { 1010fac3fd80SJustin T. Gibbs struct xbd_command *cm; 101189e0f4d2SKip Macy 1012fac3fd80SJustin T. Gibbs cm = &sc->xbd_shadow[i]; 1013fac3fd80SJustin T. Gibbs if (cm->cm_sg_refs != NULL) { 1014fac3fd80SJustin T. Gibbs free(cm->cm_sg_refs, M_XENBLOCKFRONT); 1015fac3fd80SJustin T. Gibbs cm->cm_sg_refs = NULL; 101689e0f4d2SKip Macy } 101789e0f4d2SKip Macy 1018fac3fd80SJustin T. Gibbs bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map); 1019fac3fd80SJustin T. Gibbs } 1020fac3fd80SJustin T. Gibbs free(sc->xbd_shadow, M_XENBLOCKFRONT); 1021fac3fd80SJustin T. Gibbs sc->xbd_shadow = NULL; 1022fac3fd80SJustin T. Gibbs 1023fac3fd80SJustin T. Gibbs bus_dma_tag_destroy(sc->xbd_io_dmat); 1024fac3fd80SJustin T. Gibbs 1025e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_FREE); 1026e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_READY); 1027e2c1fe90SJustin T. Gibbs xbd_initq_cm(sc, XBD_Q_COMPLETE); 1028fac3fd80SJustin T. Gibbs } 1029fac3fd80SJustin T. Gibbs 103076acc41fSJustin T. Gibbs xen_intr_unbind(&sc->xen_intr_handle); 103176acc41fSJustin T. Gibbs 1032fac3fd80SJustin T. Gibbs } 1033fac3fd80SJustin T. Gibbs 1034fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/ 1035ff662b5cSJustin T. Gibbs static void 103633eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc) 103789e0f4d2SKip Macy { 1038ff662b5cSJustin T. Gibbs const char *otherend_path; 1039ff662b5cSJustin T. Gibbs const char *node_path; 10408b8bfa35SJustin T. Gibbs uint32_t max_ring_page_order; 1041ff662b5cSJustin T. Gibbs int error; 1042ff662b5cSJustin T. Gibbs int i; 104389e0f4d2SKip Macy 104433eebb6aSJustin T. Gibbs if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) { 104506a630f6SJustin T. Gibbs /* Initialization has already been performed. */ 1046ff662b5cSJustin T. Gibbs return; 104706a630f6SJustin T. Gibbs } 104889e0f4d2SKip Macy 1049ff662b5cSJustin T. Gibbs /* 1050ff662b5cSJustin T. Gibbs * Protocol defaults valid even if negotiation for a 1051ff662b5cSJustin T. Gibbs * setting fails. 1052ff662b5cSJustin T. Gibbs */ 10538b8bfa35SJustin T. Gibbs max_ring_page_order = 0; 105433eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 1055112cacaeSRoger Pau Monné sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; 105633eebb6aSJustin T. Gibbs sc->xbd_max_request_size = 105733eebb6aSJustin T. Gibbs XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments); 1058ff662b5cSJustin T. Gibbs 1059ff662b5cSJustin T. Gibbs /* 1060ff662b5cSJustin T. Gibbs * Protocol negotiation. 1061ff662b5cSJustin T. Gibbs * 1062ff662b5cSJustin T. Gibbs * \note xs_gather() returns on the first encountered error, so 1063ff662b5cSJustin T. Gibbs * we must use independant calls in order to guarantee 1064ff662b5cSJustin T. Gibbs * we don't miss information in a sparsly populated back-end 1065ff662b5cSJustin T. Gibbs * tree. 10668b8bfa35SJustin T. Gibbs * 10678b8bfa35SJustin T. Gibbs * \note xs_scanf() does not update variables for unmatched 10688b8bfa35SJustin T. Gibbs * fields. 1069ff662b5cSJustin T. Gibbs */ 107033eebb6aSJustin T. Gibbs otherend_path = xenbus_get_otherend_path(sc->xbd_dev); 107133eebb6aSJustin T. Gibbs node_path = xenbus_get_node(sc->xbd_dev); 10728b8bfa35SJustin T. Gibbs 10738b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 10748b8bfa35SJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 10758b8bfa35SJustin T. Gibbs "max-ring-page-order", NULL, "%" PRIu32, 10768b8bfa35SJustin T. Gibbs &max_ring_page_order); 107733eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1 << max_ring_page_order; 1078ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 1079ff662b5cSJustin T. Gibbs "max-ring-pages", NULL, "%" PRIu32, 108033eebb6aSJustin T. Gibbs &sc->xbd_ring_pages); 108133eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages < 1) 108233eebb6aSJustin T. Gibbs sc->xbd_ring_pages = 1; 1083ff662b5cSJustin T. Gibbs 108433eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) { 108533eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 108633eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 1087112cacaeSRoger Pau Monné "limited to front-end limit of %u.\n", 108833eebb6aSJustin T. Gibbs sc->xbd_ring_pages, XBD_MAX_RING_PAGES); 108933eebb6aSJustin T. Gibbs sc->xbd_ring_pages = XBD_MAX_RING_PAGES; 109089e0f4d2SKip Macy } 109189e0f4d2SKip Macy 109233eebb6aSJustin T. Gibbs if (powerof2(sc->xbd_ring_pages) == 0) { 10938b8bfa35SJustin T. Gibbs uint32_t new_page_limit; 10948b8bfa35SJustin T. Gibbs 109533eebb6aSJustin T. Gibbs new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1); 109633eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 109733eebb6aSJustin T. Gibbs "Back-end specified ring-pages of %u " 109833eebb6aSJustin T. Gibbs "is not a power of 2. Limited to %u.\n", 109933eebb6aSJustin T. Gibbs sc->xbd_ring_pages, new_page_limit); 110033eebb6aSJustin T. Gibbs sc->xbd_ring_pages = new_page_limit; 11018b8bfa35SJustin T. Gibbs } 11028b8bfa35SJustin T. Gibbs 1103112cacaeSRoger Pau Monné sc->xbd_max_requests = 1104112cacaeSRoger Pau Monné BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE); 110533eebb6aSJustin T. Gibbs if (sc->xbd_max_requests > XBD_MAX_REQUESTS) { 110633eebb6aSJustin T. Gibbs device_printf(sc->xbd_dev, 110733eebb6aSJustin T. Gibbs "Back-end specified max_requests of %u " 1108112cacaeSRoger Pau Monné "limited to front-end limit of %zu.\n", 110933eebb6aSJustin T. Gibbs sc->xbd_max_requests, XBD_MAX_REQUESTS); 111033eebb6aSJustin T. Gibbs sc->xbd_max_requests = XBD_MAX_REQUESTS; 111189e0f4d2SKip Macy } 1112ff662b5cSJustin T. Gibbs 1113ff662b5cSJustin T. Gibbs /* Allocate datastructures based on negotiated values. */ 111433eebb6aSJustin T. Gibbs error = bus_dma_tag_create( 111533eebb6aSJustin T. Gibbs bus_get_dma_tag(sc->xbd_dev), /* parent */ 1116ff662b5cSJustin T. Gibbs 512, PAGE_SIZE, /* algnmnt, boundary */ 1117ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* lowaddr */ 1118ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* highaddr */ 1119ff662b5cSJustin T. Gibbs NULL, NULL, /* filter, filterarg */ 112033eebb6aSJustin T. Gibbs sc->xbd_max_request_size, 112133eebb6aSJustin T. Gibbs sc->xbd_max_request_segments, 1122ff662b5cSJustin T. Gibbs PAGE_SIZE, /* maxsegsize */ 1123ff662b5cSJustin T. Gibbs BUS_DMA_ALLOCNOW, /* flags */ 1124ff662b5cSJustin T. Gibbs busdma_lock_mutex, /* lockfunc */ 112533eebb6aSJustin T. Gibbs &sc->xbd_io_lock, /* lockarg */ 112633eebb6aSJustin T. Gibbs &sc->xbd_io_dmat); 1127ff662b5cSJustin T. Gibbs if (error != 0) { 112833eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1129ff662b5cSJustin T. Gibbs "Cannot allocate parent DMA tag\n"); 1130ff662b5cSJustin T. Gibbs return; 1131ff662b5cSJustin T. Gibbs } 1132ff662b5cSJustin T. Gibbs 1133ff662b5cSJustin T. Gibbs /* Per-transaction data allocation. */ 113433eebb6aSJustin T. Gibbs sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests, 1135ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 113633eebb6aSJustin T. Gibbs if (sc->xbd_shadow == NULL) { 113733eebb6aSJustin T. Gibbs bus_dma_tag_destroy(sc->xbd_io_dmat); 113833eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1139ff662b5cSJustin T. Gibbs "Cannot allocate request structures\n"); 114006a630f6SJustin T. Gibbs return; 1141ff662b5cSJustin T. Gibbs } 1142ff662b5cSJustin T. Gibbs 114333eebb6aSJustin T. Gibbs for (i = 0; i < sc->xbd_max_requests; i++) { 114433eebb6aSJustin T. Gibbs struct xbd_command *cm; 1145ff662b5cSJustin T. Gibbs 114633eebb6aSJustin T. Gibbs cm = &sc->xbd_shadow[i]; 114733eebb6aSJustin T. Gibbs cm->cm_sg_refs = malloc( 114833eebb6aSJustin T. Gibbs sizeof(grant_ref_t) * sc->xbd_max_request_segments, 1149ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT); 115033eebb6aSJustin T. Gibbs if (cm->cm_sg_refs == NULL) 1151ff662b5cSJustin T. Gibbs break; 115233eebb6aSJustin T. Gibbs cm->cm_id = i; 1153e2c1fe90SJustin T. Gibbs cm->cm_flags = XBDCF_INITIALIZER; 1154ff662b5cSJustin T. Gibbs cm->cm_sc = sc; 115533eebb6aSJustin T. Gibbs if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0) 1156ff662b5cSJustin T. Gibbs break; 115733eebb6aSJustin T. Gibbs xbd_free_command(cm); 1158ff662b5cSJustin T. Gibbs } 1159ff662b5cSJustin T. Gibbs 1160cdf5d66fSJustin T. Gibbs if (xbd_alloc_ring(sc) != 0) 1161ff662b5cSJustin T. Gibbs return; 1162ff662b5cSJustin T. Gibbs 11638b8bfa35SJustin T. Gibbs /* Support both backend schemes for relaying ring page limits. */ 116433eebb6aSJustin T. Gibbs if (sc->xbd_ring_pages > 1) { 1165ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 116633eebb6aSJustin T. Gibbs "num-ring-pages","%u", 116733eebb6aSJustin T. Gibbs sc->xbd_ring_pages); 1168ff662b5cSJustin T. Gibbs if (error) { 116933eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 11708b8bfa35SJustin T. Gibbs "writing %s/num-ring-pages", 11718b8bfa35SJustin T. Gibbs node_path); 11728b8bfa35SJustin T. Gibbs return; 11738b8bfa35SJustin T. Gibbs } 11740d172324SJustin T. Gibbs 11758b8bfa35SJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 11760d172324SJustin T. Gibbs "ring-page-order", "%u", 117733eebb6aSJustin T. Gibbs fls(sc->xbd_ring_pages) - 1); 11788b8bfa35SJustin T. Gibbs if (error) { 117933eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 11808b8bfa35SJustin T. Gibbs "writing %s/ring-page-order", 1181ff662b5cSJustin T. Gibbs node_path); 1182ff662b5cSJustin T. Gibbs return; 1183ff662b5cSJustin T. Gibbs } 11840d172324SJustin T. Gibbs } 1185ff662b5cSJustin T. Gibbs 1186ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "event-channel", 118776acc41fSJustin T. Gibbs "%u", xen_intr_port(sc->xen_intr_handle)); 1188ff662b5cSJustin T. Gibbs if (error) { 118933eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1190ff662b5cSJustin T. Gibbs "writing %s/event-channel", 1191ff662b5cSJustin T. Gibbs node_path); 1192ff662b5cSJustin T. Gibbs return; 1193ff662b5cSJustin T. Gibbs } 1194ff662b5cSJustin T. Gibbs 119533eebb6aSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "protocol", 119633eebb6aSJustin T. Gibbs "%s", XEN_IO_PROTO_ABI_NATIVE); 1197ff662b5cSJustin T. Gibbs if (error) { 119833eebb6aSJustin T. Gibbs xenbus_dev_fatal(sc->xbd_dev, error, 1199ff662b5cSJustin T. Gibbs "writing %s/protocol", 1200ff662b5cSJustin T. Gibbs node_path); 1201ff662b5cSJustin T. Gibbs return; 120223dc5621SKip Macy } 120312678024SDoug Rabson 120433eebb6aSJustin T. Gibbs xenbus_set_state(sc->xbd_dev, XenbusStateInitialised); 120589e0f4d2SKip Macy } 120689e0f4d2SKip Macy 120789e0f4d2SKip Macy /* 120833eebb6aSJustin T. Gibbs * Invoked when the backend is finally 'ready' (and has published 120933eebb6aSJustin T. Gibbs * the details about the physical device - #sectors, size, etc). 121089e0f4d2SKip Macy */ 121189e0f4d2SKip Macy static void 121233eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc) 121389e0f4d2SKip Macy { 121433eebb6aSJustin T. Gibbs device_t dev = sc->xbd_dev; 121589e0f4d2SKip Macy unsigned long sectors, sector_size; 121689e0f4d2SKip Macy unsigned int binfo; 12179985113bSJustin T. Gibbs int err, feature_barrier, feature_flush; 121889e0f4d2SKip Macy 1219e2c1fe90SJustin T. Gibbs if (sc->xbd_state == XBD_STATE_CONNECTED || 1220e2c1fe90SJustin T. Gibbs sc->xbd_state == XBD_STATE_SUSPENDED) 122189e0f4d2SKip Macy return; 122289e0f4d2SKip Macy 122323dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 122489e0f4d2SKip Macy 1225ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 122689e0f4d2SKip Macy "sectors", "%lu", §ors, 122789e0f4d2SKip Macy "info", "%u", &binfo, 122889e0f4d2SKip Macy "sector-size", "%lu", §or_size, 122989e0f4d2SKip Macy NULL); 123089e0f4d2SKip Macy if (err) { 123123dc5621SKip Macy xenbus_dev_fatal(dev, err, 123289e0f4d2SKip Macy "reading backend fields at %s", 123323dc5621SKip Macy xenbus_get_otherend_path(dev)); 123489e0f4d2SKip Macy return; 123589e0f4d2SKip Macy } 1236ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1237e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 123889e0f4d2SKip Macy NULL); 12399985113bSJustin T. Gibbs if (err == 0 && feature_barrier != 0) 1240e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_BARRIER; 124189e0f4d2SKip Macy 12429985113bSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 12439985113bSJustin T. Gibbs "feature-flush-cache", "%lu", &feature_flush, 12449985113bSJustin T. Gibbs NULL); 12459985113bSJustin T. Gibbs if (err == 0 && feature_flush != 0) 12469985113bSJustin T. Gibbs sc->xbd_flags |= XBDF_FLUSH; 12479985113bSJustin T. Gibbs 124833eebb6aSJustin T. Gibbs if (sc->xbd_disk == NULL) { 124923dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 125023dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 125123dc5621SKip Macy device_get_desc(dev), 125223dc5621SKip Macy xenbus_get_node(dev)); 125323dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 125489e0f4d2SKip Macy 125533eebb6aSJustin T. Gibbs xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo, 125633eebb6aSJustin T. Gibbs sector_size); 125706a630f6SJustin T. Gibbs } 125823dc5621SKip Macy 125923dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 126089e0f4d2SKip Macy 126189e0f4d2SKip Macy /* Kick pending requests. */ 126233eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1263e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_CONNECTED; 126433eebb6aSJustin T. Gibbs xbd_startio(sc); 1265e2c1fe90SJustin T. Gibbs sc->xbd_flags |= XBDF_READY; 126633eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 126789e0f4d2SKip Macy } 126889e0f4d2SKip Macy 126989e0f4d2SKip Macy /** 127089e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 127189e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 1272a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 127389e0f4d2SKip Macy * acknowledgement. 127489e0f4d2SKip Macy */ 127523dc5621SKip Macy static void 127633eebb6aSJustin T. Gibbs xbd_closing(device_t dev) 127789e0f4d2SKip Macy { 127833eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 127989e0f4d2SKip Macy 1280ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateClosing); 1281ff662b5cSJustin T. Gibbs 128233eebb6aSJustin T. Gibbs DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev)); 128389e0f4d2SKip Macy 128433eebb6aSJustin T. Gibbs if (sc->xbd_disk != NULL) { 128533eebb6aSJustin T. Gibbs disk_destroy(sc->xbd_disk); 128633eebb6aSJustin T. Gibbs sc->xbd_disk = NULL; 128789e0f4d2SKip Macy } 128889e0f4d2SKip Macy 128923dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 129089e0f4d2SKip Macy } 129189e0f4d2SKip Macy 1292fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/ 1293fac3fd80SJustin T. Gibbs static int 1294fac3fd80SJustin T. Gibbs xbd_probe(device_t dev) 1295fac3fd80SJustin T. Gibbs { 1296a371f519SJustin T. Gibbs if (strcmp(xenbus_get_type(dev), "vbd") != 0) 1297a371f519SJustin T. Gibbs return (ENXIO); 1298fac3fd80SJustin T. Gibbs 1299a371f519SJustin T. Gibbs if (xen_hvm_domain()) { 1300a371f519SJustin T. Gibbs int error; 1301a371f519SJustin T. Gibbs char *type; 1302a371f519SJustin T. Gibbs 1303a371f519SJustin T. Gibbs /* 1304a371f519SJustin T. Gibbs * When running in an HVM domain, IDE disk emulation is 1305a371f519SJustin T. Gibbs * disabled early in boot so that native drivers will 1306a371f519SJustin T. Gibbs * not see emulated hardware. However, CDROM device 1307a371f519SJustin T. Gibbs * emulation cannot be disabled. 1308a371f519SJustin T. Gibbs * 1309a371f519SJustin T. Gibbs * Through use of FreeBSD's vm_guest and xen_hvm_domain() 1310a371f519SJustin T. Gibbs * APIs, we could modify the native CDROM driver to fail its 1311a371f519SJustin T. Gibbs * probe when running under Xen. Unfortunatlely, the PV 1312a371f519SJustin T. Gibbs * CDROM support in XenServer (up through at least version 1313a371f519SJustin T. Gibbs * 6.2) isn't functional, so we instead rely on the emulated 1314a371f519SJustin T. Gibbs * CDROM instance, and fail to attach the PV one here in 1315a371f519SJustin T. Gibbs * the blkfront driver. 1316a371f519SJustin T. Gibbs */ 1317a371f519SJustin T. Gibbs error = xs_read(XST_NIL, xenbus_get_node(dev), 1318a371f519SJustin T. Gibbs "device-type", NULL, (void **) &type); 1319a371f519SJustin T. Gibbs if (error) 1320a371f519SJustin T. Gibbs return (ENXIO); 1321a371f519SJustin T. Gibbs 1322a371f519SJustin T. Gibbs if (strncmp(type, "cdrom", 5) == 0) { 1323a371f519SJustin T. Gibbs free(type, M_XENSTORE); 1324a371f519SJustin T. Gibbs return (ENXIO); 1325a371f519SJustin T. Gibbs } 1326a371f519SJustin T. Gibbs free(type, M_XENSTORE); 1327a371f519SJustin T. Gibbs } 1328a371f519SJustin T. Gibbs 1329fac3fd80SJustin T. Gibbs device_set_desc(dev, "Virtual Block Device"); 1330fac3fd80SJustin T. Gibbs device_quiet(dev); 1331fac3fd80SJustin T. Gibbs return (0); 1332fac3fd80SJustin T. Gibbs } 1333fac3fd80SJustin T. Gibbs 1334fac3fd80SJustin T. Gibbs /* 1335fac3fd80SJustin T. Gibbs * Setup supplies the backend dir, virtual device. We place an event 1336fac3fd80SJustin T. Gibbs * channel and shared frame entries. We watch backend to wait if it's 1337fac3fd80SJustin T. Gibbs * ok. 1338fac3fd80SJustin T. Gibbs */ 1339fac3fd80SJustin T. Gibbs static int 1340fac3fd80SJustin T. Gibbs xbd_attach(device_t dev) 1341fac3fd80SJustin T. Gibbs { 1342fac3fd80SJustin T. Gibbs struct xbd_softc *sc; 1343fac3fd80SJustin T. Gibbs const char *name; 1344fac3fd80SJustin T. Gibbs uint32_t vdevice; 1345fac3fd80SJustin T. Gibbs int error; 1346fac3fd80SJustin T. Gibbs int i; 1347fac3fd80SJustin T. Gibbs int unit; 1348fac3fd80SJustin T. Gibbs 1349fac3fd80SJustin T. Gibbs /* FIXME: Use dynamic device id if this is not set. */ 1350fac3fd80SJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_node(dev), 1351fac3fd80SJustin T. Gibbs "virtual-device", NULL, "%" PRIu32, &vdevice); 135248a1ceedSColin Percival if (error) 135348a1ceedSColin Percival error = xs_scanf(XST_NIL, xenbus_get_node(dev), 135448a1ceedSColin Percival "virtual-device-ext", NULL, "%" PRIu32, &vdevice); 1355fac3fd80SJustin T. Gibbs if (error) { 1356fac3fd80SJustin T. Gibbs xenbus_dev_fatal(dev, error, "reading virtual-device"); 1357fac3fd80SJustin T. Gibbs device_printf(dev, "Couldn't determine virtual device.\n"); 1358fac3fd80SJustin T. Gibbs return (error); 1359fac3fd80SJustin T. Gibbs } 1360fac3fd80SJustin T. Gibbs 1361fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(vdevice, &unit, &name); 1362fac3fd80SJustin T. Gibbs if (!strcmp(name, "xbd")) 1363fac3fd80SJustin T. Gibbs device_set_unit(dev, unit); 1364fac3fd80SJustin T. Gibbs 1365fac3fd80SJustin T. Gibbs sc = device_get_softc(dev); 1366fac3fd80SJustin T. Gibbs mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 1367e2c1fe90SJustin T. Gibbs xbd_initqs(sc); 1368fac3fd80SJustin T. Gibbs for (i = 0; i < XBD_MAX_RING_PAGES; i++) 1369d9fab01dSJustin T. Gibbs sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 1370fac3fd80SJustin T. Gibbs 1371fac3fd80SJustin T. Gibbs sc->xbd_dev = dev; 1372fac3fd80SJustin T. Gibbs sc->xbd_vdevice = vdevice; 1373e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_DISCONNECTED; 1374fac3fd80SJustin T. Gibbs 1375fac3fd80SJustin T. Gibbs xbd_setup_sysctl(sc); 1376fac3fd80SJustin T. Gibbs 1377fac3fd80SJustin T. Gibbs /* Wait for backend device to publish its protocol capabilities. */ 1378fac3fd80SJustin T. Gibbs xenbus_set_state(dev, XenbusStateInitialising); 1379fac3fd80SJustin T. Gibbs 1380fac3fd80SJustin T. Gibbs return (0); 1381fac3fd80SJustin T. Gibbs } 138289e0f4d2SKip Macy 138323dc5621SKip Macy static int 138433eebb6aSJustin T. Gibbs xbd_detach(device_t dev) 138589e0f4d2SKip Macy { 138633eebb6aSJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 138789e0f4d2SKip Macy 1388e2c1fe90SJustin T. Gibbs DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev)); 138989e0f4d2SKip Macy 139033eebb6aSJustin T. Gibbs xbd_free(sc); 139133eebb6aSJustin T. Gibbs mtx_destroy(&sc->xbd_io_lock); 139289e0f4d2SKip Macy 139389e0f4d2SKip Macy return 0; 139489e0f4d2SKip Macy } 139589e0f4d2SKip Macy 139689e0f4d2SKip Macy static int 1397fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev) 139889e0f4d2SKip Macy { 1399fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1400fac3fd80SJustin T. Gibbs int retval; 1401fac3fd80SJustin T. Gibbs int saved_state; 140289e0f4d2SKip Macy 140389e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 140433eebb6aSJustin T. Gibbs mtx_lock(&sc->xbd_io_lock); 1405e2c1fe90SJustin T. Gibbs saved_state = sc->xbd_state; 1406e2c1fe90SJustin T. Gibbs sc->xbd_state = XBD_STATE_SUSPENDED; 1407fac3fd80SJustin T. Gibbs 1408fac3fd80SJustin T. Gibbs /* Wait for outstanding I/O to drain. */ 1409fac3fd80SJustin T. Gibbs retval = 0; 14109985113bSJustin T. Gibbs while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 1411e2c1fe90SJustin T. Gibbs if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock, 1412fac3fd80SJustin T. Gibbs PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) { 1413fac3fd80SJustin T. Gibbs retval = EBUSY; 1414fac3fd80SJustin T. Gibbs break; 1415fac3fd80SJustin T. Gibbs } 1416fac3fd80SJustin T. Gibbs } 141733eebb6aSJustin T. Gibbs mtx_unlock(&sc->xbd_io_lock); 141889e0f4d2SKip Macy 1419fac3fd80SJustin T. Gibbs if (retval != 0) 1420e2c1fe90SJustin T. Gibbs sc->xbd_state = saved_state; 142133eebb6aSJustin T. Gibbs 1422fac3fd80SJustin T. Gibbs return (retval); 142389e0f4d2SKip Macy } 142489e0f4d2SKip Macy 1425ff662b5cSJustin T. Gibbs static int 1426fac3fd80SJustin T. Gibbs xbd_resume(device_t dev) 142789e0f4d2SKip Macy { 1428fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1429fac3fd80SJustin T. Gibbs 1430fac3fd80SJustin T. Gibbs DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev)); 1431fac3fd80SJustin T. Gibbs 1432fac3fd80SJustin T. Gibbs xbd_free(sc); 1433fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1434fac3fd80SJustin T. Gibbs return (0); 143589e0f4d2SKip Macy } 143689e0f4d2SKip Macy 1437fac3fd80SJustin T. Gibbs /** 1438fac3fd80SJustin T. Gibbs * Callback received when the backend's state changes. 1439fac3fd80SJustin T. Gibbs */ 1440fac3fd80SJustin T. Gibbs static void 1441fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state) 1442fac3fd80SJustin T. Gibbs { 1443fac3fd80SJustin T. Gibbs struct xbd_softc *sc = device_get_softc(dev); 1444fac3fd80SJustin T. Gibbs 1445fac3fd80SJustin T. Gibbs DPRINTK("backend_state=%d\n", backend_state); 1446fac3fd80SJustin T. Gibbs 1447fac3fd80SJustin T. Gibbs switch (backend_state) { 1448fac3fd80SJustin T. Gibbs case XenbusStateUnknown: 1449fac3fd80SJustin T. Gibbs case XenbusStateInitialising: 1450fac3fd80SJustin T. Gibbs case XenbusStateReconfigured: 1451fac3fd80SJustin T. Gibbs case XenbusStateReconfiguring: 1452fac3fd80SJustin T. Gibbs case XenbusStateClosed: 1453fac3fd80SJustin T. Gibbs break; 1454fac3fd80SJustin T. Gibbs 1455fac3fd80SJustin T. Gibbs case XenbusStateInitWait: 1456fac3fd80SJustin T. Gibbs case XenbusStateInitialised: 1457fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1458fac3fd80SJustin T. Gibbs break; 1459fac3fd80SJustin T. Gibbs 1460fac3fd80SJustin T. Gibbs case XenbusStateConnected: 1461fac3fd80SJustin T. Gibbs xbd_initialize(sc); 1462fac3fd80SJustin T. Gibbs xbd_connect(sc); 1463fac3fd80SJustin T. Gibbs break; 1464fac3fd80SJustin T. Gibbs 1465fac3fd80SJustin T. Gibbs case XenbusStateClosing: 1466fac3fd80SJustin T. Gibbs if (sc->xbd_users > 0) 1467fac3fd80SJustin T. Gibbs xenbus_dev_error(dev, -EBUSY, 1468fac3fd80SJustin T. Gibbs "Device in use; refusing to close"); 1469fac3fd80SJustin T. Gibbs else 1470fac3fd80SJustin T. Gibbs xbd_closing(dev); 1471fac3fd80SJustin T. Gibbs break; 1472fac3fd80SJustin T. Gibbs } 1473fac3fd80SJustin T. Gibbs } 1474fac3fd80SJustin T. Gibbs 1475fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/ 147633eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = { 147723dc5621SKip Macy /* Device interface */ 147833eebb6aSJustin T. Gibbs DEVMETHOD(device_probe, xbd_probe), 147933eebb6aSJustin T. Gibbs DEVMETHOD(device_attach, xbd_attach), 148033eebb6aSJustin T. Gibbs DEVMETHOD(device_detach, xbd_detach), 148123dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 148233eebb6aSJustin T. Gibbs DEVMETHOD(device_suspend, xbd_suspend), 148333eebb6aSJustin T. Gibbs DEVMETHOD(device_resume, xbd_resume), 148489e0f4d2SKip Macy 148523dc5621SKip Macy /* Xenbus interface */ 148633eebb6aSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed), 148789e0f4d2SKip Macy 148823dc5621SKip Macy { 0, 0 } 148989e0f4d2SKip Macy }; 149089e0f4d2SKip Macy 149133eebb6aSJustin T. Gibbs static driver_t xbd_driver = { 149223dc5621SKip Macy "xbd", 149333eebb6aSJustin T. Gibbs xbd_methods, 149433eebb6aSJustin T. Gibbs sizeof(struct xbd_softc), 149589e0f4d2SKip Macy }; 149633eebb6aSJustin T. Gibbs devclass_t xbd_devclass; 149789e0f4d2SKip Macy 149833eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0); 1499