189e0f4d2SKip Macy /* 28698b76cSKip Macy * XenBSD block device driver 38698b76cSKip Macy * 4e4808c4bSKip Macy * Copyright (c) 2009 Scott Long, Yahoo! 58698b76cSKip Macy * Copyright (c) 2009 Frank Suchomel, Citrix 69999d2cbSKip Macy * Copyright (c) 2009 Doug F. Rabson, Citrix 79999d2cbSKip Macy * Copyright (c) 2005 Kip Macy 89999d2cbSKip Macy * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 99999d2cbSKip Macy * Modifications by Mark A. Williamson are (c) Intel Research Cambridge 109999d2cbSKip Macy * 119999d2cbSKip Macy * 129999d2cbSKip Macy * Permission is hereby granted, free of charge, to any person obtaining a copy 139999d2cbSKip Macy * of this software and associated documentation files (the "Software"), to 149999d2cbSKip Macy * deal in the Software without restriction, including without limitation the 159999d2cbSKip Macy * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 169999d2cbSKip Macy * sell copies of the Software, and to permit persons to whom the Software is 179999d2cbSKip Macy * furnished to do so, subject to the following conditions: 189999d2cbSKip Macy * 199999d2cbSKip Macy * The above copyright notice and this permission notice shall be included in 209999d2cbSKip Macy * all copies or substantial portions of the Software. 219999d2cbSKip Macy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 229999d2cbSKip Macy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 239999d2cbSKip Macy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 249999d2cbSKip Macy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 259999d2cbSKip Macy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 269999d2cbSKip Macy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 279999d2cbSKip Macy * DEALINGS IN THE SOFTWARE. 2889e0f4d2SKip Macy */ 2989e0f4d2SKip Macy 3089e0f4d2SKip Macy #include <sys/cdefs.h> 3189e0f4d2SKip Macy __FBSDID("$FreeBSD$"); 3289e0f4d2SKip Macy 3389e0f4d2SKip Macy #include <sys/param.h> 3489e0f4d2SKip Macy #include <sys/systm.h> 3589e0f4d2SKip Macy #include <sys/malloc.h> 3689e0f4d2SKip Macy #include <sys/kernel.h> 3789e0f4d2SKip Macy #include <vm/vm.h> 3889e0f4d2SKip Macy #include <vm/pmap.h> 3989e0f4d2SKip Macy 4089e0f4d2SKip Macy #include <sys/bio.h> 4189e0f4d2SKip Macy #include <sys/bus.h> 4289e0f4d2SKip Macy #include <sys/conf.h> 4389e0f4d2SKip Macy #include <sys/module.h> 4489e0f4d2SKip Macy 4589e0f4d2SKip Macy #include <machine/bus.h> 4689e0f4d2SKip Macy #include <sys/rman.h> 4789e0f4d2SKip Macy #include <machine/resource.h> 4889e0f4d2SKip Macy #include <machine/intr_machdep.h> 4989e0f4d2SKip Macy #include <machine/vmparam.h> 50e4808c4bSKip Macy #include <sys/bus_dma.h> 5189e0f4d2SKip Macy 52ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h> 5389e0f4d2SKip Macy #include <machine/xen/xen-os.h> 542913e88cSRobert Watson #include <machine/xen/xenvar.h> 5512678024SDoug Rabson #include <machine/xen/xenfunc.h> 56ff662b5cSJustin T. Gibbs 5712678024SDoug Rabson #include <xen/hypervisor.h> 583a6d1fcfSKip Macy #include <xen/xen_intr.h> 593a6d1fcfSKip Macy #include <xen/evtchn.h> 6012678024SDoug Rabson #include <xen/gnttab.h> 6189e0f4d2SKip Macy #include <xen/interface/grant_table.h> 6223dc5621SKip Macy #include <xen/interface/io/protocols.h> 6323dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 6489e0f4d2SKip Macy 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 7189e0f4d2SKip Macy /* prototypes */ 72e4808c4bSKip Macy static void xb_free_command(struct xb_command *cm); 7389e0f4d2SKip Macy static void xb_startio(struct xb_softc *sc); 74ff662b5cSJustin T. Gibbs static void blkfront_connect(struct xb_softc *); 7523dc5621SKip Macy static void blkfront_closing(device_t); 7623dc5621SKip Macy static int blkfront_detach(device_t); 77e4808c4bSKip Macy static int setup_blkring(struct xb_softc *); 7889e0f4d2SKip Macy static void blkif_int(void *); 79ff662b5cSJustin T. Gibbs static void blkfront_initialize(struct xb_softc *); 80ff662b5cSJustin T. Gibbs #if 0 81e4808c4bSKip Macy static void blkif_recover(struct xb_softc *); 82ff662b5cSJustin T. Gibbs #endif 83ff662b5cSJustin T. Gibbs static int blkif_completion(struct xb_command *); 84e4808c4bSKip Macy static void blkif_free(struct xb_softc *, int); 85e4808c4bSKip Macy static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int); 8689e0f4d2SKip Macy 87ff662b5cSJustin T. Gibbs MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 88ff662b5cSJustin T. Gibbs 8989e0f4d2SKip Macy #define GRANT_INVALID_REF 0 9089e0f4d2SKip Macy 9189e0f4d2SKip Macy /* Control whether runtime update of vbds is enabled. */ 9289e0f4d2SKip Macy #define ENABLE_VBD_UPDATE 0 9389e0f4d2SKip Macy 9489e0f4d2SKip Macy #if ENABLE_VBD_UPDATE 9589e0f4d2SKip Macy static void vbd_update(void); 9689e0f4d2SKip Macy #endif 9789e0f4d2SKip Macy 9889e0f4d2SKip Macy #define BLKIF_STATE_DISCONNECTED 0 9989e0f4d2SKip Macy #define BLKIF_STATE_CONNECTED 1 10089e0f4d2SKip Macy #define BLKIF_STATE_SUSPENDED 2 10189e0f4d2SKip Macy 10289e0f4d2SKip Macy #ifdef notyet 10389e0f4d2SKip Macy static char *blkif_state_name[] = { 10489e0f4d2SKip Macy [BLKIF_STATE_DISCONNECTED] = "disconnected", 10589e0f4d2SKip Macy [BLKIF_STATE_CONNECTED] = "connected", 10689e0f4d2SKip Macy [BLKIF_STATE_SUSPENDED] = "closed", 10789e0f4d2SKip Macy }; 10889e0f4d2SKip Macy 10989e0f4d2SKip Macy static char * blkif_status_name[] = { 11089e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CLOSED] = "closed", 11189e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected", 11289e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected", 11389e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CHANGED] = "changed", 11489e0f4d2SKip Macy }; 11589e0f4d2SKip Macy #endif 116e4808c4bSKip Macy 11789e0f4d2SKip Macy #if 0 11812678024SDoug Rabson #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 11989e0f4d2SKip Macy #else 12089e0f4d2SKip Macy #define DPRINTK(fmt, args...) 12189e0f4d2SKip Macy #endif 12289e0f4d2SKip Macy 12389e0f4d2SKip Macy static int blkif_open(struct disk *dp); 12489e0f4d2SKip Macy static int blkif_close(struct disk *dp); 12589e0f4d2SKip Macy static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td); 126e4808c4bSKip Macy static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm); 12789e0f4d2SKip Macy static void xb_strategy(struct bio *bp); 12889e0f4d2SKip Macy 1298698b76cSKip Macy // In order to quiesce the device during kernel dumps, outstanding requests to 1308698b76cSKip Macy // DOM0 for disk reads/writes need to be accounted for. 1318698b76cSKip Macy static int xb_dump(void *, void *, vm_offset_t, off_t, size_t); 13289e0f4d2SKip Macy 13389e0f4d2SKip Macy /* XXX move to xb_vbd.c when VBD update support is added */ 13489e0f4d2SKip Macy #define MAX_VBDS 64 13589e0f4d2SKip Macy 13689e0f4d2SKip Macy #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */ 13789e0f4d2SKip Macy #define XBD_SECTOR_SHFT 9 13889e0f4d2SKip Macy 13923dc5621SKip Macy /* 14023dc5621SKip Macy * Translate Linux major/minor to an appropriate name and unit 14123dc5621SKip Macy * number. For HVM guests, this allows us to use the same drive names 14223dc5621SKip Macy * with blkfront as the emulated drives, easing transition slightly. 14323dc5621SKip Macy */ 14423dc5621SKip Macy static void 14523dc5621SKip Macy blkfront_vdevice_to_unit(int vdevice, int *unit, const char **name) 14623dc5621SKip Macy { 14723dc5621SKip Macy static struct vdev_info { 14823dc5621SKip Macy int major; 14923dc5621SKip Macy int shift; 15023dc5621SKip Macy int base; 15123dc5621SKip Macy const char *name; 15223dc5621SKip Macy } info[] = { 15323dc5621SKip Macy {3, 6, 0, "ad"}, /* ide0 */ 15423dc5621SKip Macy {22, 6, 2, "ad"}, /* ide1 */ 15523dc5621SKip Macy {33, 6, 4, "ad"}, /* ide2 */ 15623dc5621SKip Macy {34, 6, 6, "ad"}, /* ide3 */ 15723dc5621SKip Macy {56, 6, 8, "ad"}, /* ide4 */ 15823dc5621SKip Macy {57, 6, 10, "ad"}, /* ide5 */ 15923dc5621SKip Macy {88, 6, 12, "ad"}, /* ide6 */ 16023dc5621SKip Macy {89, 6, 14, "ad"}, /* ide7 */ 16123dc5621SKip Macy {90, 6, 16, "ad"}, /* ide8 */ 16223dc5621SKip Macy {91, 6, 18, "ad"}, /* ide9 */ 16323dc5621SKip Macy 16423dc5621SKip Macy {8, 4, 0, "da"}, /* scsi disk0 */ 16523dc5621SKip Macy {65, 4, 16, "da"}, /* scsi disk1 */ 16623dc5621SKip Macy {66, 4, 32, "da"}, /* scsi disk2 */ 16723dc5621SKip Macy {67, 4, 48, "da"}, /* scsi disk3 */ 16823dc5621SKip Macy {68, 4, 64, "da"}, /* scsi disk4 */ 16923dc5621SKip Macy {69, 4, 80, "da"}, /* scsi disk5 */ 17023dc5621SKip Macy {70, 4, 96, "da"}, /* scsi disk6 */ 17123dc5621SKip Macy {71, 4, 112, "da"}, /* scsi disk7 */ 17223dc5621SKip Macy {128, 4, 128, "da"}, /* scsi disk8 */ 17323dc5621SKip Macy {129, 4, 144, "da"}, /* scsi disk9 */ 17423dc5621SKip Macy {130, 4, 160, "da"}, /* scsi disk10 */ 17523dc5621SKip Macy {131, 4, 176, "da"}, /* scsi disk11 */ 17623dc5621SKip Macy {132, 4, 192, "da"}, /* scsi disk12 */ 17723dc5621SKip Macy {133, 4, 208, "da"}, /* scsi disk13 */ 17823dc5621SKip Macy {134, 4, 224, "da"}, /* scsi disk14 */ 17923dc5621SKip Macy {135, 4, 240, "da"}, /* scsi disk15 */ 18023dc5621SKip Macy 18123dc5621SKip Macy {202, 4, 0, "xbd"}, /* xbd */ 18223dc5621SKip Macy 18323dc5621SKip Macy {0, 0, 0, NULL}, 18423dc5621SKip Macy }; 18523dc5621SKip Macy int major = vdevice >> 8; 18623dc5621SKip Macy int minor = vdevice & 0xff; 18723dc5621SKip Macy int i; 18823dc5621SKip Macy 18923dc5621SKip Macy if (vdevice & (1 << 28)) { 19023dc5621SKip Macy *unit = (vdevice & ((1 << 28) - 1)) >> 8; 19123dc5621SKip Macy *name = "xbd"; 19223dc5621SKip Macy } 19323dc5621SKip Macy 19423dc5621SKip Macy for (i = 0; info[i].major; i++) { 19523dc5621SKip Macy if (info[i].major == major) { 19623dc5621SKip Macy *unit = info[i].base + (minor >> info[i].shift); 19723dc5621SKip Macy *name = info[i].name; 19823dc5621SKip Macy return; 19923dc5621SKip Macy } 20023dc5621SKip Macy } 20123dc5621SKip Macy 20223dc5621SKip Macy *unit = minor >> 4; 20323dc5621SKip Macy *name = "xbd"; 20423dc5621SKip Macy } 20523dc5621SKip Macy 20689e0f4d2SKip Macy int 207ff662b5cSJustin T. Gibbs xlvbd_add(struct xb_softc *sc, blkif_sector_t sectors, 208ff662b5cSJustin T. Gibbs int vdevice, uint16_t vdisk_info, unsigned long sector_size) 20989e0f4d2SKip Macy { 21023dc5621SKip Macy int unit, error = 0; 21123dc5621SKip Macy const char *name; 21223dc5621SKip Macy 21323dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 21489e0f4d2SKip Macy 21523dc5621SKip Macy sc->xb_unit = unit; 21689e0f4d2SKip Macy 21723dc5621SKip Macy if (strcmp(name, "xbd")) 218e4808c4bSKip Macy device_printf(sc->xb_dev, "attaching as %s%d\n", name, unit); 21923dc5621SKip Macy 22089e0f4d2SKip Macy sc->xb_disk = disk_alloc(); 2213a6d1fcfSKip Macy sc->xb_disk->d_unit = sc->xb_unit; 22289e0f4d2SKip Macy sc->xb_disk->d_open = blkif_open; 22389e0f4d2SKip Macy sc->xb_disk->d_close = blkif_close; 22489e0f4d2SKip Macy sc->xb_disk->d_ioctl = blkif_ioctl; 22589e0f4d2SKip Macy sc->xb_disk->d_strategy = xb_strategy; 2268698b76cSKip Macy sc->xb_disk->d_dump = xb_dump; 2273a6d1fcfSKip Macy sc->xb_disk->d_name = name; 22889e0f4d2SKip Macy sc->xb_disk->d_drv1 = sc; 22989e0f4d2SKip Macy sc->xb_disk->d_sectorsize = sector_size; 23089e0f4d2SKip Macy 231ff662b5cSJustin T. Gibbs sc->xb_disk->d_mediasize = sectors * sector_size; 232ff662b5cSJustin T. Gibbs sc->xb_disk->d_maxsize = sc->max_request_size; 23389e0f4d2SKip Macy sc->xb_disk->d_flags = 0; 23489e0f4d2SKip Macy disk_create(sc->xb_disk, DISK_VERSION_00); 23589e0f4d2SKip Macy 23689e0f4d2SKip Macy return error; 23789e0f4d2SKip Macy } 23889e0f4d2SKip Macy 23989e0f4d2SKip Macy /************************ end VBD support *****************/ 24089e0f4d2SKip Macy 24189e0f4d2SKip Macy /* 24289e0f4d2SKip Macy * Read/write routine for a buffer. Finds the proper unit, place it on 24389e0f4d2SKip Macy * the sortq and kick the controller. 24489e0f4d2SKip Macy */ 24589e0f4d2SKip Macy static void 24689e0f4d2SKip Macy xb_strategy(struct bio *bp) 24789e0f4d2SKip Macy { 24889e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1; 24989e0f4d2SKip Macy 25089e0f4d2SKip Macy /* bogus disk? */ 25189e0f4d2SKip Macy if (sc == NULL) { 25289e0f4d2SKip Macy bp->bio_error = EINVAL; 25389e0f4d2SKip Macy bp->bio_flags |= BIO_ERROR; 25489e0f4d2SKip Macy bp->bio_resid = bp->bio_bcount; 25589e0f4d2SKip Macy biodone(bp); 25689e0f4d2SKip Macy return; 25789e0f4d2SKip Macy } 25889e0f4d2SKip Macy 259e4808c4bSKip Macy /* 260e4808c4bSKip Macy * Place it in the queue of disk activities for this disk 261e4808c4bSKip Macy */ 262e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 263e4808c4bSKip Macy 264e4808c4bSKip Macy xb_enqueue_bio(sc, bp); 265e4808c4bSKip Macy xb_startio(sc); 266e4808c4bSKip Macy 267e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 268e4808c4bSKip Macy return; 269e4808c4bSKip Macy } 270e4808c4bSKip Macy 271e4808c4bSKip Macy static void 272e4808c4bSKip Macy xb_bio_complete(struct xb_softc *sc, struct xb_command *cm) 273e4808c4bSKip Macy { 274e4808c4bSKip Macy struct bio *bp; 275e4808c4bSKip Macy 276e4808c4bSKip Macy bp = cm->bp; 277e4808c4bSKip Macy 278e4808c4bSKip Macy if ( unlikely(cm->status != BLKIF_RSP_OKAY) ) { 279e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 280e4808c4bSKip Macy printf(" status: %x\n", cm->status); 281e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 282e4808c4bSKip Macy } 283e4808c4bSKip Macy 284e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 285e4808c4bSKip Macy bp->bio_error = EIO; 286e4808c4bSKip Macy else 287e4808c4bSKip Macy bp->bio_resid = 0; 288e4808c4bSKip Macy 289e4808c4bSKip Macy xb_free_command(cm); 290e4808c4bSKip Macy biodone(bp); 291e4808c4bSKip Macy } 292e4808c4bSKip Macy 2938698b76cSKip Macy // Quiesce the disk writes for a dump file before allowing the next buffer. 2948698b76cSKip Macy static void 295e4808c4bSKip Macy xb_quiesce(struct xb_softc *sc) 2968698b76cSKip Macy { 2978698b76cSKip Macy int mtd; 2988698b76cSKip Macy 2998698b76cSKip Macy // While there are outstanding requests 300e4808c4bSKip Macy while (!TAILQ_EMPTY(&sc->cm_busy)) { 301e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, mtd); 3028698b76cSKip Macy if (mtd) { 303e4808c4bSKip Macy /* Recieved request completions, update queue. */ 304e4808c4bSKip Macy blkif_int(sc); 3058698b76cSKip Macy } 306e4808c4bSKip Macy if (!TAILQ_EMPTY(&sc->cm_busy)) { 307e4808c4bSKip Macy /* 308e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 309e4808c4bSKip Macy * to complete. 310e4808c4bSKip Macy */ 311be7747b4SKip Macy HYPERVISOR_yield(); 3128698b76cSKip Macy } 3138698b76cSKip Macy } 3148698b76cSKip Macy } 3158698b76cSKip Macy 316e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 317e4808c4bSKip Macy static void 318e4808c4bSKip Macy xb_dump_complete(struct xb_command *cm) 319e4808c4bSKip Macy { 3208698b76cSKip Macy 321e4808c4bSKip Macy xb_enqueue_complete(cm); 322e4808c4bSKip Macy } 323e4808c4bSKip Macy 3248698b76cSKip Macy static int 3258698b76cSKip Macy xb_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 3268698b76cSKip Macy size_t length) 3278698b76cSKip Macy { 3288698b76cSKip Macy struct disk *dp = arg; 3298698b76cSKip Macy struct xb_softc *sc = (struct xb_softc *) dp->d_drv1; 330e4808c4bSKip Macy struct xb_command *cm; 331e4808c4bSKip Macy size_t chunk; 332e4808c4bSKip Macy int sbp; 3338698b76cSKip Macy int rc = 0; 3348698b76cSKip Macy 335e4808c4bSKip Macy if (length <= 0) 336e4808c4bSKip Macy return (rc); 3378698b76cSKip Macy 338e4808c4bSKip Macy xb_quiesce(sc); /* All quiet on the western front. */ 339e4808c4bSKip Macy 340e4808c4bSKip Macy /* 341e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 342e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 343e4808c4bSKip Macy */ 344e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 345e4808c4bSKip Macy 346e4808c4bSKip Macy /* Split the 64KB block as needed */ 347e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 348e4808c4bSKip Macy cm = xb_dequeue_free(sc); 349e4808c4bSKip Macy if (cm == NULL) { 350e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 351e4808c4bSKip Macy device_printf(sc->xb_dev, "dump: no more commands?\n"); 352e4808c4bSKip Macy return (EBUSY); 353e4808c4bSKip Macy } 354e4808c4bSKip Macy 355ff662b5cSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->max_request_segments, 356ff662b5cSJustin T. Gibbs &cm->gref_head) != 0) { 357e4808c4bSKip Macy xb_free_command(cm); 358e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 359e4808c4bSKip Macy device_printf(sc->xb_dev, "no more grant allocs?\n"); 360e4808c4bSKip Macy return (EBUSY); 361e4808c4bSKip Macy } 362e4808c4bSKip Macy 363ff662b5cSJustin T. Gibbs chunk = length > sc->max_request_size 364ff662b5cSJustin T. Gibbs ? sc->max_request_size : length; 365e4808c4bSKip Macy cm->data = virtual; 366e4808c4bSKip Macy cm->datalen = chunk; 367e4808c4bSKip Macy cm->operation = BLKIF_OP_WRITE; 368e4808c4bSKip Macy cm->sector_number = offset / dp->d_sectorsize; 369e4808c4bSKip Macy cm->cm_complete = xb_dump_complete; 370e4808c4bSKip Macy 371e4808c4bSKip Macy xb_enqueue_ready(cm); 3728698b76cSKip Macy 3738698b76cSKip Macy length -= chunk; 3748698b76cSKip Macy offset += chunk; 3758698b76cSKip Macy virtual = (char *) virtual + chunk; 3768698b76cSKip Macy } 3778698b76cSKip Macy 378e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 379e4808c4bSKip Macy xb_startio(sc); 380e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 381e4808c4bSKip Macy 382e4808c4bSKip Macy /* Poll for the completion. */ 383e4808c4bSKip Macy xb_quiesce(sc); /* All quite on the eastern front */ 384e4808c4bSKip Macy 385e4808c4bSKip Macy /* If there were any errors, bail out... */ 386e4808c4bSKip Macy while ((cm = xb_dequeue_complete(sc)) != NULL) { 387e4808c4bSKip Macy if (cm->status != BLKIF_RSP_OKAY) { 388e4808c4bSKip Macy device_printf(sc->xb_dev, 389e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 390e4808c4bSKip Macy cm->sector_number); 391e4808c4bSKip Macy rc = EIO; 3928698b76cSKip Macy } 393e4808c4bSKip Macy xb_free_command(cm); 3948698b76cSKip Macy } 395e4808c4bSKip Macy 3968698b76cSKip Macy return (rc); 3978698b76cSKip Macy } 3988698b76cSKip Macy 3998698b76cSKip Macy 40023dc5621SKip Macy static int 40123dc5621SKip Macy blkfront_probe(device_t dev) 40289e0f4d2SKip Macy { 40323dc5621SKip Macy 40423dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vbd")) { 40523dc5621SKip Macy device_set_desc(dev, "Virtual Block Device"); 40623dc5621SKip Macy device_quiet(dev); 40723dc5621SKip Macy return (0); 40823dc5621SKip Macy } 40923dc5621SKip Macy 41023dc5621SKip Macy return (ENXIO); 41123dc5621SKip Macy } 41223dc5621SKip Macy 41323dc5621SKip Macy /* 41423dc5621SKip Macy * Setup supplies the backend dir, virtual device. We place an event 41523dc5621SKip Macy * channel and shared frame entries. We watch backend to wait if it's 41623dc5621SKip Macy * ok. 41723dc5621SKip Macy */ 41823dc5621SKip Macy static int 41923dc5621SKip Macy blkfront_attach(device_t dev) 42023dc5621SKip Macy { 421e4808c4bSKip Macy struct xb_softc *sc; 42223dc5621SKip Macy const char *name; 423ff662b5cSJustin T. Gibbs int error; 424ff662b5cSJustin T. Gibbs int vdevice; 425ff662b5cSJustin T. Gibbs int i; 426ff662b5cSJustin T. Gibbs int unit; 42789e0f4d2SKip Macy 42889e0f4d2SKip Macy /* FIXME: Use dynamic device id if this is not set. */ 429ff662b5cSJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_node(dev), 4303a6d1fcfSKip Macy "virtual-device", NULL, "%i", &vdevice); 43112678024SDoug Rabson if (error) { 43212678024SDoug Rabson xenbus_dev_fatal(dev, error, "reading virtual-device"); 433ff662b5cSJustin T. Gibbs device_printf(dev, "Couldn't determine virtual device.\n"); 43412678024SDoug Rabson return (error); 43589e0f4d2SKip Macy } 43689e0f4d2SKip Macy 43723dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 43823dc5621SKip Macy if (!strcmp(name, "xbd")) 43923dc5621SKip Macy device_set_unit(dev, unit); 44023dc5621SKip Macy 441e4808c4bSKip Macy sc = device_get_softc(dev); 442e4808c4bSKip Macy mtx_init(&sc->xb_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 443e4808c4bSKip Macy xb_initq_free(sc); 444e4808c4bSKip Macy xb_initq_busy(sc); 445e4808c4bSKip Macy xb_initq_ready(sc); 446e4808c4bSKip Macy xb_initq_complete(sc); 447e4808c4bSKip Macy xb_initq_bio(sc); 448ff662b5cSJustin T. Gibbs for (i = 0; i < XBF_MAX_RING_PAGES; i++) 449ff662b5cSJustin T. Gibbs sc->ring_ref[i] = GRANT_INVALID_REF; 45089e0f4d2SKip Macy 451e4808c4bSKip Macy sc->xb_dev = dev; 452e4808c4bSKip Macy sc->vdevice = vdevice; 453e4808c4bSKip Macy sc->connected = BLKIF_STATE_DISCONNECTED; 45489e0f4d2SKip Macy 45589e0f4d2SKip Macy /* Front end dir is a number, which is used as the id. */ 456e4808c4bSKip Macy sc->handle = strtoul(strrchr(xenbus_get_node(dev),'/')+1, NULL, 0); 45789e0f4d2SKip Macy 458ff662b5cSJustin T. Gibbs /* Wait for backend device to publish its protocol capabilities. */ 459ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateInitialising); 46012678024SDoug Rabson 46112678024SDoug Rabson return (0); 46212678024SDoug Rabson } 46312678024SDoug Rabson 46412678024SDoug Rabson static int 46512678024SDoug Rabson blkfront_suspend(device_t dev) 46612678024SDoug Rabson { 467e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 46812678024SDoug Rabson 46912678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 470e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 471e4808c4bSKip Macy sc->connected = BLKIF_STATE_SUSPENDED; 472e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 47389e0f4d2SKip Macy 47423dc5621SKip Macy return (0); 47589e0f4d2SKip Macy } 47689e0f4d2SKip Macy 47723dc5621SKip Macy static int 47823dc5621SKip Macy blkfront_resume(device_t dev) 47989e0f4d2SKip Macy { 480ff662b5cSJustin T. Gibbs #if 0 481e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 48289e0f4d2SKip Macy 48312678024SDoug Rabson DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev)); 48489e0f4d2SKip Macy 485ff662b5cSJustin T. Gibbs /* XXX This can't work!!! */ 486e4808c4bSKip Macy blkif_free(sc, 1); 487ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 488ff662b5cSJustin T. Gibbs if (sc->connected == BLKIF_STATE_SUSPENDED) 489e4808c4bSKip Macy blkif_recover(sc); 490ff662b5cSJustin T. Gibbs #endif 491ff662b5cSJustin T. Gibbs return (0); 49289e0f4d2SKip Macy } 49389e0f4d2SKip Macy 494ff662b5cSJustin T. Gibbs static void 495ff662b5cSJustin T. Gibbs blkfront_initialize(struct xb_softc *sc) 49689e0f4d2SKip Macy { 497ff662b5cSJustin T. Gibbs const char *otherend_path; 498ff662b5cSJustin T. Gibbs const char *node_path; 499ff662b5cSJustin T. Gibbs int error; 500ff662b5cSJustin T. Gibbs int i; 50189e0f4d2SKip Macy 502ff662b5cSJustin T. Gibbs if (xenbus_get_state(sc->xb_dev) != XenbusStateInitialising) 503ff662b5cSJustin T. Gibbs return; 50489e0f4d2SKip Macy 505ff662b5cSJustin T. Gibbs /* 506ff662b5cSJustin T. Gibbs * Protocol defaults valid even if negotiation for a 507ff662b5cSJustin T. Gibbs * setting fails. 508ff662b5cSJustin T. Gibbs */ 509ff662b5cSJustin T. Gibbs sc->ring_pages = 1; 510ff662b5cSJustin T. Gibbs sc->max_requests = BLKIF_MAX_RING_REQUESTS(PAGE_SIZE); 511ff662b5cSJustin T. Gibbs sc->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK; 512518c8243SColin Percival sc->max_request_size = (sc->max_request_segments - 1) * PAGE_SIZE; 513ff662b5cSJustin T. Gibbs sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments); 514ff662b5cSJustin T. Gibbs 515ff662b5cSJustin T. Gibbs /* 516ff662b5cSJustin T. Gibbs * Protocol negotiation. 517ff662b5cSJustin T. Gibbs * 518ff662b5cSJustin T. Gibbs * \note xs_gather() returns on the first encountered error, so 519ff662b5cSJustin T. Gibbs * we must use independant calls in order to guarantee 520ff662b5cSJustin T. Gibbs * we don't miss information in a sparsly populated back-end 521ff662b5cSJustin T. Gibbs * tree. 522ff662b5cSJustin T. Gibbs */ 523ff662b5cSJustin T. Gibbs otherend_path = xenbus_get_otherend_path(sc->xb_dev); 524ff662b5cSJustin T. Gibbs node_path = xenbus_get_node(sc->xb_dev); 525ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 526ff662b5cSJustin T. Gibbs "max-ring-pages", NULL, "%" PRIu32, 527ff662b5cSJustin T. Gibbs &sc->ring_pages); 528ff662b5cSJustin T. Gibbs 529ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 530ff662b5cSJustin T. Gibbs "max-requests", NULL, "%" PRIu32, 531ff662b5cSJustin T. Gibbs &sc->max_requests); 532ff662b5cSJustin T. Gibbs 533ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 534ff662b5cSJustin T. Gibbs "max-request-segments", NULL, "%" PRIu32, 535ff662b5cSJustin T. Gibbs &sc->max_request_segments); 536ff662b5cSJustin T. Gibbs 537ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 538ff662b5cSJustin T. Gibbs "max-request-size", NULL, "%" PRIu32, 539ff662b5cSJustin T. Gibbs &sc->max_request_size); 540ff662b5cSJustin T. Gibbs 541ff662b5cSJustin T. Gibbs if (sc->ring_pages > XBF_MAX_RING_PAGES) { 542ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specified ring-pages of " 543ff662b5cSJustin T. Gibbs "%u limited to front-end limit of %zu.\n", 544ff662b5cSJustin T. Gibbs sc->ring_pages, XBF_MAX_RING_PAGES); 545ff662b5cSJustin T. Gibbs sc->ring_pages = XBF_MAX_RING_PAGES; 54689e0f4d2SKip Macy } 54789e0f4d2SKip Macy 548ff662b5cSJustin T. Gibbs if (sc->max_requests > XBF_MAX_REQUESTS) { 549ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specified max_requests of " 550ff662b5cSJustin T. Gibbs "%u limited to front-end limit of %u.\n", 551ff662b5cSJustin T. Gibbs sc->max_requests, XBF_MAX_REQUESTS); 552ff662b5cSJustin T. Gibbs sc->max_requests = XBF_MAX_REQUESTS; 55389e0f4d2SKip Macy } 554ff662b5cSJustin T. Gibbs 555ff662b5cSJustin T. Gibbs if (sc->max_request_segments > XBF_MAX_SEGMENTS_PER_REQUEST) { 556ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specificed " 557ff662b5cSJustin T. Gibbs "max_requests_segments of %u limited to " 558ff662b5cSJustin T. Gibbs "front-end limit of %u.\n", 559ff662b5cSJustin T. Gibbs sc->max_request_segments, 560ff662b5cSJustin T. Gibbs XBF_MAX_SEGMENTS_PER_REQUEST); 561ff662b5cSJustin T. Gibbs sc->max_request_segments = XBF_MAX_SEGMENTS_PER_REQUEST; 56289e0f4d2SKip Macy } 563ff662b5cSJustin T. Gibbs 564ff662b5cSJustin T. Gibbs if (sc->max_request_size > XBF_MAX_REQUEST_SIZE) { 565ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specificed " 566ff662b5cSJustin T. Gibbs "max_request_size of %u limited to front-end " 567ff662b5cSJustin T. Gibbs "limit of %u.\n", sc->max_request_size, 568ff662b5cSJustin T. Gibbs XBF_MAX_REQUEST_SIZE); 569ff662b5cSJustin T. Gibbs sc->max_request_size = XBF_MAX_REQUEST_SIZE; 570ff662b5cSJustin T. Gibbs } 571ff662b5cSJustin T. Gibbs sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments); 572ff662b5cSJustin T. Gibbs 573ff662b5cSJustin T. Gibbs /* Allocate datastructures based on negotiated values. */ 574ff662b5cSJustin T. Gibbs error = bus_dma_tag_create(NULL, /* parent */ 575ff662b5cSJustin T. Gibbs 512, PAGE_SIZE, /* algnmnt, boundary */ 576ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* lowaddr */ 577ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* highaddr */ 578ff662b5cSJustin T. Gibbs NULL, NULL, /* filter, filterarg */ 579ff662b5cSJustin T. Gibbs sc->max_request_size, 580ff662b5cSJustin T. Gibbs sc->max_request_segments, 581ff662b5cSJustin T. Gibbs PAGE_SIZE, /* maxsegsize */ 582ff662b5cSJustin T. Gibbs BUS_DMA_ALLOCNOW, /* flags */ 583ff662b5cSJustin T. Gibbs busdma_lock_mutex, /* lockfunc */ 584ff662b5cSJustin T. Gibbs &sc->xb_io_lock, /* lockarg */ 585ff662b5cSJustin T. Gibbs &sc->xb_io_dmat); 586ff662b5cSJustin T. Gibbs if (error != 0) { 587ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 588ff662b5cSJustin T. Gibbs "Cannot allocate parent DMA tag\n"); 589ff662b5cSJustin T. Gibbs return; 590ff662b5cSJustin T. Gibbs } 591ff662b5cSJustin T. Gibbs 592ff662b5cSJustin T. Gibbs /* Per-transaction data allocation. */ 593ff662b5cSJustin T. Gibbs sc->shadow = malloc(sizeof(*sc->shadow) * sc->max_requests, 594ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 595ff662b5cSJustin T. Gibbs if (sc->shadow == NULL) { 596ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 597ff662b5cSJustin T. Gibbs "Cannot allocate request structures\n"); 598ff662b5cSJustin T. Gibbs } 599ff662b5cSJustin T. Gibbs 600ff662b5cSJustin T. Gibbs for (i = 0; i < sc->max_requests; i++) { 601ff662b5cSJustin T. Gibbs struct xb_command *cm; 602ff662b5cSJustin T. Gibbs 603ff662b5cSJustin T. Gibbs cm = &sc->shadow[i]; 604ff662b5cSJustin T. Gibbs cm->sg_refs = malloc(sizeof(grant_ref_t) 605ff662b5cSJustin T. Gibbs * sc->max_request_segments, 606ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT); 607ff662b5cSJustin T. Gibbs if (cm->sg_refs == NULL) 608ff662b5cSJustin T. Gibbs break; 609ff662b5cSJustin T. Gibbs cm->id = i; 610ff662b5cSJustin T. Gibbs cm->cm_sc = sc; 611ff662b5cSJustin T. Gibbs if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0) 612ff662b5cSJustin T. Gibbs break; 613ff662b5cSJustin T. Gibbs xb_free_command(cm); 614ff662b5cSJustin T. Gibbs } 615ff662b5cSJustin T. Gibbs 616ff662b5cSJustin T. Gibbs if (setup_blkring(sc) != 0) 617ff662b5cSJustin T. Gibbs return; 618ff662b5cSJustin T. Gibbs 619ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 620ff662b5cSJustin T. Gibbs "ring-pages","%u", sc->ring_pages); 621ff662b5cSJustin T. Gibbs if (error) { 622ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 623ff662b5cSJustin T. Gibbs "writing %s/ring-pages", 624ff662b5cSJustin T. Gibbs node_path); 625ff662b5cSJustin T. Gibbs return; 626ff662b5cSJustin T. Gibbs } 627ff662b5cSJustin T. Gibbs 628ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 629ff662b5cSJustin T. Gibbs "max-requests","%u", sc->max_requests); 630ff662b5cSJustin T. Gibbs if (error) { 631ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 632ff662b5cSJustin T. Gibbs "writing %s/max-requests", 633ff662b5cSJustin T. Gibbs node_path); 634ff662b5cSJustin T. Gibbs return; 635ff662b5cSJustin T. Gibbs } 636ff662b5cSJustin T. Gibbs 637ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 638ff662b5cSJustin T. Gibbs "max-request-segments","%u", sc->max_request_segments); 639ff662b5cSJustin T. Gibbs if (error) { 640ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 641ff662b5cSJustin T. Gibbs "writing %s/max-request-segments", 642ff662b5cSJustin T. Gibbs node_path); 643ff662b5cSJustin T. Gibbs return; 644ff662b5cSJustin T. Gibbs } 645ff662b5cSJustin T. Gibbs 646ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 647ff662b5cSJustin T. Gibbs "max-request-size","%u", sc->max_request_size); 648ff662b5cSJustin T. Gibbs if (error) { 649ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 650ff662b5cSJustin T. Gibbs "writing %s/max-request-size", 651ff662b5cSJustin T. Gibbs node_path); 652ff662b5cSJustin T. Gibbs return; 653ff662b5cSJustin T. Gibbs } 654ff662b5cSJustin T. Gibbs 655ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, "event-channel", 656ff662b5cSJustin T. Gibbs "%u", irq_to_evtchn_port(sc->irq)); 657ff662b5cSJustin T. Gibbs if (error) { 658ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 659ff662b5cSJustin T. Gibbs "writing %s/event-channel", 660ff662b5cSJustin T. Gibbs node_path); 661ff662b5cSJustin T. Gibbs return; 662ff662b5cSJustin T. Gibbs } 663ff662b5cSJustin T. Gibbs 664ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 66523dc5621SKip Macy "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE); 666ff662b5cSJustin T. Gibbs if (error) { 667ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 668ff662b5cSJustin T. Gibbs "writing %s/protocol", 669ff662b5cSJustin T. Gibbs node_path); 670ff662b5cSJustin T. Gibbs return; 67123dc5621SKip Macy } 67212678024SDoug Rabson 673ff662b5cSJustin T. Gibbs xenbus_set_state(sc->xb_dev, XenbusStateInitialised); 67489e0f4d2SKip Macy } 67589e0f4d2SKip Macy 67689e0f4d2SKip Macy static int 677e4808c4bSKip Macy setup_blkring(struct xb_softc *sc) 67889e0f4d2SKip Macy { 67989e0f4d2SKip Macy blkif_sring_t *sring; 680ff662b5cSJustin T. Gibbs uintptr_t sring_page_addr; 6813a6d1fcfSKip Macy int error; 682ff662b5cSJustin T. Gibbs int i; 68389e0f4d2SKip Macy 684ff662b5cSJustin T. Gibbs sring = malloc(sc->ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 685ff662b5cSJustin T. Gibbs M_NOWAIT|M_ZERO); 68689e0f4d2SKip Macy if (sring == NULL) { 687e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, ENOMEM, "allocating shared ring"); 688ff662b5cSJustin T. Gibbs return (ENOMEM); 68989e0f4d2SKip Macy } 69089e0f4d2SKip Macy SHARED_RING_INIT(sring); 691ff662b5cSJustin T. Gibbs FRONT_RING_INIT(&sc->ring, sring, sc->ring_pages * PAGE_SIZE); 692ff662b5cSJustin T. Gibbs 693ff662b5cSJustin T. Gibbs for (i = 0, sring_page_addr = (uintptr_t)sring; 694ff662b5cSJustin T. Gibbs i < sc->ring_pages; 695ff662b5cSJustin T. Gibbs i++, sring_page_addr += PAGE_SIZE) { 69689e0f4d2SKip Macy 697e4808c4bSKip Macy error = xenbus_grant_ring(sc->xb_dev, 698ff662b5cSJustin T. Gibbs (vtomach(sring_page_addr) >> PAGE_SHIFT), &sc->ring_ref[i]); 6993a6d1fcfSKip Macy if (error) { 700ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 701ff662b5cSJustin T. Gibbs "granting ring_ref(%d)", i); 702ff662b5cSJustin T. Gibbs return (error); 703ff662b5cSJustin T. Gibbs } 704ff662b5cSJustin T. Gibbs } 705ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev), 706ff662b5cSJustin T. Gibbs "ring-ref","%u", sc->ring_ref[0]); 707ff662b5cSJustin T. Gibbs if (error) { 708ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, "writing %s/ring-ref", 709ff662b5cSJustin T. Gibbs xenbus_get_node(sc->xb_dev)); 710ff662b5cSJustin T. Gibbs return (error); 711ff662b5cSJustin T. Gibbs } 712ff662b5cSJustin T. Gibbs for (i = 1; i < sc->ring_pages; i++) { 713ff662b5cSJustin T. Gibbs char ring_ref_name[]= "ring_refXX"; 714ff662b5cSJustin T. Gibbs 715ff662b5cSJustin T. Gibbs snprintf(ring_ref_name, sizeof(ring_ref_name), "ring-ref%u", i); 716ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev), 717ff662b5cSJustin T. Gibbs ring_ref_name, "%u", sc->ring_ref[i]); 718ff662b5cSJustin T. Gibbs if (error) { 719ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, "writing %s/%s", 720ff662b5cSJustin T. Gibbs xenbus_get_node(sc->xb_dev), 721ff662b5cSJustin T. Gibbs ring_ref_name); 722ff662b5cSJustin T. Gibbs return (error); 723ff662b5cSJustin T. Gibbs } 72489e0f4d2SKip Macy } 72589e0f4d2SKip Macy 726ff662b5cSJustin T. Gibbs error = bind_listening_port_to_irqhandler( 727ff662b5cSJustin T. Gibbs xenbus_get_otherend_id(sc->xb_dev), 728e4808c4bSKip Macy "xbd", (driver_intr_t *)blkif_int, sc, 729e4808c4bSKip Macy INTR_TYPE_BIO | INTR_MPSAFE, &sc->irq); 7303a6d1fcfSKip Macy if (error) { 731e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, error, 73289e0f4d2SKip Macy "bind_evtchn_to_irqhandler failed"); 7333a6d1fcfSKip Macy return (error); 73489e0f4d2SKip Macy } 73589e0f4d2SKip Macy 736ff662b5cSJustin T. Gibbs return (0); 737ff662b5cSJustin T. Gibbs } 73889e0f4d2SKip Macy 73989e0f4d2SKip Macy /** 74089e0f4d2SKip Macy * Callback received when the backend's state changes. 74189e0f4d2SKip Macy */ 742*283d6f72SJustin T. Gibbs static void 74323dc5621SKip Macy blkfront_backend_changed(device_t dev, XenbusState backend_state) 74489e0f4d2SKip Macy { 745e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 74689e0f4d2SKip Macy 74712678024SDoug Rabson DPRINTK("backend_state=%d\n", backend_state); 74889e0f4d2SKip Macy 74989e0f4d2SKip Macy switch (backend_state) { 75089e0f4d2SKip Macy case XenbusStateUnknown: 75189e0f4d2SKip Macy case XenbusStateInitialising: 752920ba15bSKip Macy case XenbusStateReconfigured: 753920ba15bSKip Macy case XenbusStateReconfiguring: 754ff662b5cSJustin T. Gibbs case XenbusStateClosed: 75589e0f4d2SKip Macy break; 75689e0f4d2SKip Macy 757ff662b5cSJustin T. Gibbs case XenbusStateInitWait: 758ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 759ff662b5cSJustin T. Gibbs break; 760ff662b5cSJustin T. Gibbs 761ff662b5cSJustin T. Gibbs case XenbusStateInitialised: 76289e0f4d2SKip Macy case XenbusStateConnected: 763ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 764ff662b5cSJustin T. Gibbs blkfront_connect(sc); 76589e0f4d2SKip Macy break; 76689e0f4d2SKip Macy 76789e0f4d2SKip Macy case XenbusStateClosing: 768e4808c4bSKip Macy if (sc->users > 0) 76989e0f4d2SKip Macy xenbus_dev_error(dev, -EBUSY, 77089e0f4d2SKip Macy "Device in use; refusing to close"); 77189e0f4d2SKip Macy else 77289e0f4d2SKip Macy blkfront_closing(dev); 773ff662b5cSJustin T. Gibbs break; 77489e0f4d2SKip Macy } 77589e0f4d2SKip Macy } 77689e0f4d2SKip Macy 77789e0f4d2SKip Macy /* 77889e0f4d2SKip Macy ** Invoked when the backend is finally 'ready' (and has told produced 77989e0f4d2SKip Macy ** the details about the physical device - #sectors, size, etc). 78089e0f4d2SKip Macy */ 78189e0f4d2SKip Macy static void 782ff662b5cSJustin T. Gibbs blkfront_connect(struct xb_softc *sc) 78389e0f4d2SKip Macy { 784e4808c4bSKip Macy device_t dev = sc->xb_dev; 78589e0f4d2SKip Macy unsigned long sectors, sector_size; 78689e0f4d2SKip Macy unsigned int binfo; 787e4808c4bSKip Macy int err, feature_barrier; 78889e0f4d2SKip Macy 789e4808c4bSKip Macy if( (sc->connected == BLKIF_STATE_CONNECTED) || 790e4808c4bSKip Macy (sc->connected == BLKIF_STATE_SUSPENDED) ) 79189e0f4d2SKip Macy return; 79289e0f4d2SKip Macy 79323dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 79489e0f4d2SKip Macy 795ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 79689e0f4d2SKip Macy "sectors", "%lu", §ors, 79789e0f4d2SKip Macy "info", "%u", &binfo, 79889e0f4d2SKip Macy "sector-size", "%lu", §or_size, 79989e0f4d2SKip Macy NULL); 80089e0f4d2SKip Macy if (err) { 80123dc5621SKip Macy xenbus_dev_fatal(dev, err, 80289e0f4d2SKip Macy "reading backend fields at %s", 80323dc5621SKip Macy xenbus_get_otherend_path(dev)); 80489e0f4d2SKip Macy return; 80589e0f4d2SKip Macy } 806ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 807e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 80889e0f4d2SKip Macy NULL); 809e4808c4bSKip Macy if (!err || feature_barrier) 810e4808c4bSKip Macy sc->xb_flags |= XB_BARRIER; 81189e0f4d2SKip Macy 81223dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 81323dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 81423dc5621SKip Macy device_get_desc(dev), 81523dc5621SKip Macy xenbus_get_node(dev)); 81623dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 81789e0f4d2SKip Macy 818e4808c4bSKip Macy xlvbd_add(sc, sectors, sc->vdevice, binfo, sector_size); 81923dc5621SKip Macy 82023dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 82189e0f4d2SKip Macy 82289e0f4d2SKip Macy /* Kick pending requests. */ 823e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 824e4808c4bSKip Macy sc->connected = BLKIF_STATE_CONNECTED; 825e4808c4bSKip Macy xb_startio(sc); 826e4808c4bSKip Macy sc->xb_flags |= XB_READY; 827e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 82889e0f4d2SKip Macy 82989e0f4d2SKip Macy } 83089e0f4d2SKip Macy 83189e0f4d2SKip Macy /** 83289e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 83389e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 834a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 83589e0f4d2SKip Macy * acknowledgement. 83689e0f4d2SKip Macy */ 83723dc5621SKip Macy static void 83823dc5621SKip Macy blkfront_closing(device_t dev) 83989e0f4d2SKip Macy { 840e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 84189e0f4d2SKip Macy 842ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateClosing); 843ff662b5cSJustin T. Gibbs 84423dc5621SKip Macy DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev)); 84589e0f4d2SKip Macy 846ff662b5cSJustin T. Gibbs if (sc->xb_disk != NULL) { 847ff662b5cSJustin T. Gibbs disk_destroy(sc->xb_disk); 848ff662b5cSJustin T. Gibbs sc->xb_disk = NULL; 84989e0f4d2SKip Macy } 85089e0f4d2SKip Macy 85123dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 85289e0f4d2SKip Macy } 85389e0f4d2SKip Macy 85489e0f4d2SKip Macy 85523dc5621SKip Macy static int 85623dc5621SKip Macy blkfront_detach(device_t dev) 85789e0f4d2SKip Macy { 858e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 85989e0f4d2SKip Macy 86023dc5621SKip Macy DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev)); 86189e0f4d2SKip Macy 862e4808c4bSKip Macy blkif_free(sc, 0); 863e4808c4bSKip Macy mtx_destroy(&sc->xb_io_lock); 86489e0f4d2SKip Macy 86589e0f4d2SKip Macy return 0; 86689e0f4d2SKip Macy } 86789e0f4d2SKip Macy 86889e0f4d2SKip Macy 86989e0f4d2SKip Macy static inline void 870e4808c4bSKip Macy flush_requests(struct xb_softc *sc) 87189e0f4d2SKip Macy { 87289e0f4d2SKip Macy int notify; 87389e0f4d2SKip Macy 874e4808c4bSKip Macy RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->ring, notify); 87589e0f4d2SKip Macy 87689e0f4d2SKip Macy if (notify) 877e4808c4bSKip Macy notify_remote_via_irq(sc->irq); 87889e0f4d2SKip Macy } 87989e0f4d2SKip Macy 880ff662b5cSJustin T. Gibbs static void 881ff662b5cSJustin T. Gibbs blkif_restart_queue_callback(void *arg) 88289e0f4d2SKip Macy { 883e4808c4bSKip Macy struct xb_softc *sc = arg; 88489e0f4d2SKip Macy 885ff662b5cSJustin T. Gibbs mtx_lock(&sc->xb_io_lock); 886ff662b5cSJustin T. Gibbs 887e4808c4bSKip Macy xb_startio(sc); 888ff662b5cSJustin T. Gibbs 889ff662b5cSJustin T. Gibbs mtx_unlock(&sc->xb_io_lock); 89089e0f4d2SKip Macy } 89189e0f4d2SKip Macy 89289e0f4d2SKip Macy static int 89389e0f4d2SKip Macy blkif_open(struct disk *dp) 89489e0f4d2SKip Macy { 89589e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 89689e0f4d2SKip Macy 89789e0f4d2SKip Macy if (sc == NULL) { 89812678024SDoug Rabson printf("xb%d: not found", sc->xb_unit); 89989e0f4d2SKip Macy return (ENXIO); 90089e0f4d2SKip Macy } 90189e0f4d2SKip Macy 90289e0f4d2SKip Macy sc->xb_flags |= XB_OPEN; 903e4808c4bSKip Macy sc->users++; 90489e0f4d2SKip Macy return (0); 90589e0f4d2SKip Macy } 90689e0f4d2SKip Macy 90789e0f4d2SKip Macy static int 90889e0f4d2SKip Macy blkif_close(struct disk *dp) 90989e0f4d2SKip Macy { 91089e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 91189e0f4d2SKip Macy 91289e0f4d2SKip Macy if (sc == NULL) 91389e0f4d2SKip Macy return (ENXIO); 91489e0f4d2SKip Macy sc->xb_flags &= ~XB_OPEN; 915e4808c4bSKip Macy if (--(sc->users) == 0) { 91689e0f4d2SKip Macy /* Check whether we have been instructed to close. We will 91789e0f4d2SKip Macy have ignored this request initially, as the device was 91889e0f4d2SKip Macy still mounted. */ 919e4808c4bSKip Macy device_t dev = sc->xb_dev; 92023dc5621SKip Macy XenbusState state = 92123dc5621SKip Macy xenbus_read_driver_state(xenbus_get_otherend_path(dev)); 92289e0f4d2SKip Macy 92389e0f4d2SKip Macy if (state == XenbusStateClosing) 92489e0f4d2SKip Macy blkfront_closing(dev); 92589e0f4d2SKip Macy } 92689e0f4d2SKip Macy return (0); 92789e0f4d2SKip Macy } 92889e0f4d2SKip Macy 92989e0f4d2SKip Macy static int 93089e0f4d2SKip Macy blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 93189e0f4d2SKip Macy { 93289e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 93389e0f4d2SKip Macy 93489e0f4d2SKip Macy if (sc == NULL) 93589e0f4d2SKip Macy return (ENXIO); 93689e0f4d2SKip Macy 93789e0f4d2SKip Macy return (ENOTTY); 93889e0f4d2SKip Macy } 93989e0f4d2SKip Macy 940e4808c4bSKip Macy static void 941e4808c4bSKip Macy xb_free_command(struct xb_command *cm) 942e4808c4bSKip Macy { 943e4808c4bSKip Macy 944e4808c4bSKip Macy KASSERT((cm->cm_flags & XB_ON_XBQ_MASK) == 0, 945e4808c4bSKip Macy ("Freeing command that is still on a queue\n")); 946e4808c4bSKip Macy 947e4808c4bSKip Macy cm->cm_flags = 0; 948e4808c4bSKip Macy cm->bp = NULL; 949e4808c4bSKip Macy cm->cm_complete = NULL; 950e4808c4bSKip Macy xb_enqueue_free(cm); 951e4808c4bSKip Macy } 95289e0f4d2SKip Macy 95389e0f4d2SKip Macy /* 95489e0f4d2SKip Macy * blkif_queue_request 95589e0f4d2SKip Macy * 95689e0f4d2SKip Macy * request block io 95789e0f4d2SKip Macy * 95889e0f4d2SKip Macy * id: for guest use only. 95989e0f4d2SKip Macy * operation: BLKIF_OP_{READ,WRITE,PROBE} 96089e0f4d2SKip Macy * buffer: buffer to read/write into. this should be a 96189e0f4d2SKip Macy * virtual address in the guest os. 96289e0f4d2SKip Macy */ 963e4808c4bSKip Macy static struct xb_command * 964e4808c4bSKip Macy xb_bio_command(struct xb_softc *sc) 96589e0f4d2SKip Macy { 966e4808c4bSKip Macy struct xb_command *cm; 967e4808c4bSKip Macy struct bio *bp; 96889e0f4d2SKip Macy 969e4808c4bSKip Macy if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) 970e4808c4bSKip Macy return (NULL); 97189e0f4d2SKip Macy 972e4808c4bSKip Macy bp = xb_dequeue_bio(sc); 973e4808c4bSKip Macy if (bp == NULL) 974e4808c4bSKip Macy return (NULL); 975e4808c4bSKip Macy 976e4808c4bSKip Macy if ((cm = xb_dequeue_free(sc)) == NULL) { 977e4808c4bSKip Macy xb_requeue_bio(sc, bp); 978e4808c4bSKip Macy return (NULL); 97989e0f4d2SKip Macy } 98089e0f4d2SKip Macy 981ff662b5cSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->max_request_segments, 982ff662b5cSJustin T. Gibbs &cm->gref_head) != 0) { 983e4808c4bSKip Macy gnttab_request_free_callback(&sc->callback, 984e4808c4bSKip Macy blkif_restart_queue_callback, sc, 985ff662b5cSJustin T. Gibbs sc->max_request_segments); 986e4808c4bSKip Macy xb_requeue_bio(sc, bp); 987e4808c4bSKip Macy xb_enqueue_free(cm); 988e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 989e4808c4bSKip Macy return (NULL); 990e4808c4bSKip Macy } 99189e0f4d2SKip Macy 992e4808c4bSKip Macy cm->bp = bp; 993e4808c4bSKip Macy cm->data = bp->bio_data; 994e4808c4bSKip Macy cm->datalen = bp->bio_bcount; 995e4808c4bSKip Macy cm->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ : 996e4808c4bSKip Macy BLKIF_OP_WRITE; 997e4808c4bSKip Macy cm->sector_number = (blkif_sector_t)bp->bio_pblkno; 99889e0f4d2SKip Macy 999e4808c4bSKip Macy return (cm); 1000e4808c4bSKip Macy } 100189e0f4d2SKip Macy 1002e4808c4bSKip Macy static int 1003e4808c4bSKip Macy blkif_queue_request(struct xb_softc *sc, struct xb_command *cm) 1004e4808c4bSKip Macy { 1005e4808c4bSKip Macy int error; 1006e4808c4bSKip Macy 1007e4808c4bSKip Macy error = bus_dmamap_load(sc->xb_io_dmat, cm->map, cm->data, cm->datalen, 1008e4808c4bSKip Macy blkif_queue_cb, cm, 0); 1009e4808c4bSKip Macy if (error == EINPROGRESS) { 1010e4808c4bSKip Macy printf("EINPROGRESS\n"); 1011e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 1012e4808c4bSKip Macy cm->cm_flags |= XB_CMD_FROZEN; 1013e4808c4bSKip Macy return (0); 1014e4808c4bSKip Macy } 1015e4808c4bSKip Macy 1016e4808c4bSKip Macy return (error); 1017e4808c4bSKip Macy } 1018e4808c4bSKip Macy 1019e4808c4bSKip Macy static void 1020e4808c4bSKip Macy blkif_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1021e4808c4bSKip Macy { 1022e4808c4bSKip Macy struct xb_softc *sc; 1023e4808c4bSKip Macy struct xb_command *cm; 1024e4808c4bSKip Macy blkif_request_t *ring_req; 1025ff662b5cSJustin T. Gibbs struct blkif_request_segment *sg; 1026ff662b5cSJustin T. Gibbs struct blkif_request_segment *last_block_sg; 1027ff662b5cSJustin T. Gibbs grant_ref_t *sg_ref; 1028e4808c4bSKip Macy vm_paddr_t buffer_ma; 1029e4808c4bSKip Macy uint64_t fsect, lsect; 1030ff662b5cSJustin T. Gibbs int ref; 1031ff662b5cSJustin T. Gibbs int op; 1032ff662b5cSJustin T. Gibbs int block_segs; 1033e4808c4bSKip Macy 1034e4808c4bSKip Macy cm = arg; 1035e4808c4bSKip Macy sc = cm->cm_sc; 1036e4808c4bSKip Macy 1037ff662b5cSJustin T. Gibbs //printf("%s: Start\n", __func__); 1038e4808c4bSKip Macy if (error) { 1039e4808c4bSKip Macy printf("error %d in blkif_queue_cb\n", error); 1040e4808c4bSKip Macy cm->bp->bio_error = EIO; 1041e4808c4bSKip Macy biodone(cm->bp); 1042e4808c4bSKip Macy xb_free_command(cm); 1043e4808c4bSKip Macy return; 1044e4808c4bSKip Macy } 104589e0f4d2SKip Macy 104689e0f4d2SKip Macy /* Fill out a communications ring structure. */ 1047e4808c4bSKip Macy ring_req = RING_GET_REQUEST(&sc->ring, sc->ring.req_prod_pvt); 1048ff662b5cSJustin T. Gibbs sc->ring.req_prod_pvt++; 1049ff662b5cSJustin T. Gibbs ring_req->id = cm->id; 1050e4808c4bSKip Macy ring_req->operation = cm->operation; 1051e4808c4bSKip Macy ring_req->sector_number = cm->sector_number; 105289e0f4d2SKip Macy ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk; 1053e4808c4bSKip Macy ring_req->nr_segments = nsegs; 1054ff662b5cSJustin T. Gibbs cm->nseg = nsegs; 105589e0f4d2SKip Macy 1056ff662b5cSJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK); 1057ff662b5cSJustin T. Gibbs sg = ring_req->seg; 1058ff662b5cSJustin T. Gibbs last_block_sg = sg + block_segs; 1059ff662b5cSJustin T. Gibbs sg_ref = cm->sg_refs; 1060ff662b5cSJustin T. Gibbs 1061ff662b5cSJustin T. Gibbs while (1) { 1062ff662b5cSJustin T. Gibbs 1063ff662b5cSJustin T. Gibbs while (sg < last_block_sg) { 1064ff662b5cSJustin T. Gibbs buffer_ma = segs->ds_addr; 106589e0f4d2SKip Macy fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 1066ff662b5cSJustin T. Gibbs lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 1067e4808c4bSKip Macy 1068ff662b5cSJustin T. Gibbs KASSERT(lsect <= 7, ("XEN disk driver data cannot " 1069ff662b5cSJustin T. Gibbs "cross a page boundary")); 1070e4808c4bSKip Macy 107189e0f4d2SKip Macy /* install a grant reference. */ 1072e4808c4bSKip Macy ref = gnttab_claim_grant_reference(&cm->gref_head); 1073ff662b5cSJustin T. Gibbs 1074ff662b5cSJustin T. Gibbs /* 1075ff662b5cSJustin T. Gibbs * GNTTAB_LIST_END == 0xffffffff, but it is private 1076ff662b5cSJustin T. Gibbs * to gnttab.c. 1077ff662b5cSJustin T. Gibbs */ 1078ff662b5cSJustin T. Gibbs KASSERT(ref != ~0, ("grant_reference failed")); 107989e0f4d2SKip Macy 108089e0f4d2SKip Macy gnttab_grant_foreign_access_ref( 108189e0f4d2SKip Macy ref, 1082e4808c4bSKip Macy xenbus_get_otherend_id(sc->xb_dev), 108389e0f4d2SKip Macy buffer_ma >> PAGE_SHIFT, 1084ff662b5cSJustin T. Gibbs ring_req->operation == BLKIF_OP_WRITE); 108589e0f4d2SKip Macy 1086ff662b5cSJustin T. Gibbs *sg_ref = ref; 1087ff662b5cSJustin T. Gibbs *sg = (struct blkif_request_segment) { 108889e0f4d2SKip Macy .gref = ref, 108989e0f4d2SKip Macy .first_sect = fsect, 109089e0f4d2SKip Macy .last_sect = lsect }; 1091ff662b5cSJustin T. Gibbs sg++; 1092ff662b5cSJustin T. Gibbs sg_ref++; 1093ff662b5cSJustin T. Gibbs segs++; 1094ff662b5cSJustin T. Gibbs nsegs--; 109589e0f4d2SKip Macy } 1096ff662b5cSJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK); 1097ff662b5cSJustin T. Gibbs if (block_segs == 0) 1098ff662b5cSJustin T. Gibbs break; 109989e0f4d2SKip Macy 1100ff662b5cSJustin T. Gibbs sg = BLKRING_GET_SG_REQUEST(&sc->ring, sc->ring.req_prod_pvt); 1101ff662b5cSJustin T. Gibbs sc->ring.req_prod_pvt++; 1102ff662b5cSJustin T. Gibbs last_block_sg = sg + block_segs; 1103ff662b5cSJustin T. Gibbs } 110489e0f4d2SKip Macy 1105e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 1106e4808c4bSKip Macy op = BUS_DMASYNC_PREREAD; 1107e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 1108e4808c4bSKip Macy op = BUS_DMASYNC_PREWRITE; 1109e4808c4bSKip Macy else 1110e4808c4bSKip Macy op = 0; 1111e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 1112e4808c4bSKip Macy 1113ff662b5cSJustin T. Gibbs gnttab_free_grant_references(cm->gref_head); 1114e4808c4bSKip Macy 1115e4808c4bSKip Macy xb_enqueue_busy(cm); 1116e4808c4bSKip Macy 1117e4808c4bSKip Macy /* 1118e4808c4bSKip Macy * This flag means that we're probably executing in the busdma swi 1119e4808c4bSKip Macy * instead of in the startio context, so an explicit flush is needed. 1120e4808c4bSKip Macy */ 1121e4808c4bSKip Macy if (cm->cm_flags & XB_CMD_FROZEN) 1122e4808c4bSKip Macy flush_requests(sc); 1123e4808c4bSKip Macy 1124ff662b5cSJustin T. Gibbs //printf("%s: Done\n", __func__); 1125e4808c4bSKip Macy return; 1126e4808c4bSKip Macy } 112789e0f4d2SKip Macy 112889e0f4d2SKip Macy /* 112989e0f4d2SKip Macy * Dequeue buffers and place them in the shared communication ring. 113089e0f4d2SKip Macy * Return when no more requests can be accepted or all buffers have 113189e0f4d2SKip Macy * been queued. 113289e0f4d2SKip Macy * 113389e0f4d2SKip Macy * Signal XEN once the ring has been filled out. 113489e0f4d2SKip Macy */ 113589e0f4d2SKip Macy static void 113689e0f4d2SKip Macy xb_startio(struct xb_softc *sc) 113789e0f4d2SKip Macy { 1138e4808c4bSKip Macy struct xb_command *cm; 1139e4808c4bSKip Macy int error, queued = 0; 114089e0f4d2SKip Macy 1141e4808c4bSKip Macy mtx_assert(&sc->xb_io_lock, MA_OWNED); 114289e0f4d2SKip Macy 1143ff662b5cSJustin T. Gibbs while (RING_FREE_REQUESTS(&sc->ring) >= sc->max_request_blocks) { 1144e4808c4bSKip Macy if (sc->xb_flags & XB_FROZEN) 1145e4808c4bSKip Macy break; 114689e0f4d2SKip Macy 1147e4808c4bSKip Macy cm = xb_dequeue_ready(sc); 114889e0f4d2SKip Macy 1149e4808c4bSKip Macy if (cm == NULL) 1150e4808c4bSKip Macy cm = xb_bio_command(sc); 1151e4808c4bSKip Macy 1152e4808c4bSKip Macy if (cm == NULL) 1153e4808c4bSKip Macy break; 1154e4808c4bSKip Macy 1155e4808c4bSKip Macy if ((error = blkif_queue_request(sc, cm)) != 0) { 1156e4808c4bSKip Macy printf("blkif_queue_request returned %d\n", error); 115789e0f4d2SKip Macy break; 115889e0f4d2SKip Macy } 115989e0f4d2SKip Macy queued++; 116089e0f4d2SKip Macy } 116189e0f4d2SKip Macy 116289e0f4d2SKip Macy if (queued != 0) 1163e4808c4bSKip Macy flush_requests(sc); 116489e0f4d2SKip Macy } 116589e0f4d2SKip Macy 116689e0f4d2SKip Macy static void 116789e0f4d2SKip Macy blkif_int(void *xsc) 116889e0f4d2SKip Macy { 1169e4808c4bSKip Macy struct xb_softc *sc = xsc; 1170e4808c4bSKip Macy struct xb_command *cm; 117189e0f4d2SKip Macy blkif_response_t *bret; 117289e0f4d2SKip Macy RING_IDX i, rp; 1173e4808c4bSKip Macy int op; 117489e0f4d2SKip Macy 1175e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 117689e0f4d2SKip Macy 1177e4808c4bSKip Macy if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) { 1178e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 117989e0f4d2SKip Macy return; 118089e0f4d2SKip Macy } 118189e0f4d2SKip Macy 118289e0f4d2SKip Macy again: 1183e4808c4bSKip Macy rp = sc->ring.sring->rsp_prod; 118489e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 118589e0f4d2SKip Macy 1186ff662b5cSJustin T. Gibbs for (i = sc->ring.rsp_cons; i != rp;) { 1187e4808c4bSKip Macy bret = RING_GET_RESPONSE(&sc->ring, i); 1188e4808c4bSKip Macy cm = &sc->shadow[bret->id]; 118989e0f4d2SKip Macy 1190e4808c4bSKip Macy xb_remove_busy(cm); 1191ff662b5cSJustin T. Gibbs i += blkif_completion(cm); 119289e0f4d2SKip Macy 1193e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 1194e4808c4bSKip Macy op = BUS_DMASYNC_POSTREAD; 1195e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 1196e4808c4bSKip Macy op = BUS_DMASYNC_POSTWRITE; 119789e0f4d2SKip Macy else 1198e4808c4bSKip Macy op = 0; 1199e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 1200e4808c4bSKip Macy bus_dmamap_unload(sc->xb_io_dmat, cm->map); 120189e0f4d2SKip Macy 1202e4808c4bSKip Macy /* 1203e4808c4bSKip Macy * If commands are completing then resources are probably 1204e4808c4bSKip Macy * being freed as well. It's a cheap assumption even when 1205e4808c4bSKip Macy * wrong. 1206e4808c4bSKip Macy */ 1207e4808c4bSKip Macy sc->xb_flags &= ~XB_FROZEN; 1208e4808c4bSKip Macy 1209e4808c4bSKip Macy /* 1210e4808c4bSKip Macy * Directly call the i/o complete routine to save an 1211e4808c4bSKip Macy * an indirection in the common case. 1212e4808c4bSKip Macy */ 1213e4808c4bSKip Macy cm->status = bret->status; 1214e4808c4bSKip Macy if (cm->bp) 1215e4808c4bSKip Macy xb_bio_complete(sc, cm); 1216e4808c4bSKip Macy else if (cm->cm_complete) 1217e4808c4bSKip Macy (cm->cm_complete)(cm); 1218e4808c4bSKip Macy else 1219e4808c4bSKip Macy xb_free_command(cm); 122089e0f4d2SKip Macy } 122189e0f4d2SKip Macy 1222e4808c4bSKip Macy sc->ring.rsp_cons = i; 122389e0f4d2SKip Macy 1224e4808c4bSKip Macy if (i != sc->ring.req_prod_pvt) { 122589e0f4d2SKip Macy int more_to_do; 1226e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, more_to_do); 122789e0f4d2SKip Macy if (more_to_do) 122889e0f4d2SKip Macy goto again; 122989e0f4d2SKip Macy } else { 1230e4808c4bSKip Macy sc->ring.sring->rsp_event = i + 1; 123189e0f4d2SKip Macy } 123289e0f4d2SKip Macy 1233e4808c4bSKip Macy xb_startio(sc); 123489e0f4d2SKip Macy 1235e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 123689e0f4d2SKip Macy } 123789e0f4d2SKip Macy 123889e0f4d2SKip Macy static void 1239e4808c4bSKip Macy blkif_free(struct xb_softc *sc, int suspend) 124089e0f4d2SKip Macy { 1241ff662b5cSJustin T. Gibbs uint8_t *sring_page_ptr; 1242ff662b5cSJustin T. Gibbs int i; 124389e0f4d2SKip Macy 124489e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 1245e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 1246e4808c4bSKip Macy sc->connected = suspend ? 124789e0f4d2SKip Macy BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; 1248e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 124989e0f4d2SKip Macy 125089e0f4d2SKip Macy /* Free resources associated with old device channel. */ 1251ff662b5cSJustin T. Gibbs if (sc->ring.sring != NULL) { 1252ff662b5cSJustin T. Gibbs sring_page_ptr = (uint8_t *)sc->ring.sring; 1253ff662b5cSJustin T. Gibbs for (i = 0; i < sc->ring_pages; i++) { 1254ff662b5cSJustin T. Gibbs if (sc->ring_ref[i] != GRANT_INVALID_REF) { 1255ff662b5cSJustin T. Gibbs gnttab_end_foreign_access_ref(sc->ring_ref[i]); 1256ff662b5cSJustin T. Gibbs sc->ring_ref[i] = GRANT_INVALID_REF; 1257ff662b5cSJustin T. Gibbs } 1258ff662b5cSJustin T. Gibbs sring_page_ptr += PAGE_SIZE; 1259ff662b5cSJustin T. Gibbs } 1260ff662b5cSJustin T. Gibbs free(sc->ring.sring, M_XENBLOCKFRONT); 1261e4808c4bSKip Macy sc->ring.sring = NULL; 126289e0f4d2SKip Macy } 1263ff662b5cSJustin T. Gibbs 1264ff662b5cSJustin T. Gibbs if (sc->shadow) { 1265ff662b5cSJustin T. Gibbs 1266ff662b5cSJustin T. Gibbs for (i = 0; i < sc->max_requests; i++) { 1267ff662b5cSJustin T. Gibbs struct xb_command *cm; 1268ff662b5cSJustin T. Gibbs 1269ff662b5cSJustin T. Gibbs cm = &sc->shadow[i]; 1270ff662b5cSJustin T. Gibbs if (cm->sg_refs != NULL) { 1271ff662b5cSJustin T. Gibbs free(cm->sg_refs, M_XENBLOCKFRONT); 1272ff662b5cSJustin T. Gibbs cm->sg_refs = NULL; 1273ff662b5cSJustin T. Gibbs } 1274ff662b5cSJustin T. Gibbs 1275ff662b5cSJustin T. Gibbs bus_dmamap_destroy(sc->xb_io_dmat, cm->map); 1276ff662b5cSJustin T. Gibbs } 1277ff662b5cSJustin T. Gibbs free(sc->shadow, M_XENBLOCKFRONT); 1278ff662b5cSJustin T. Gibbs sc->shadow = NULL; 1279ff662b5cSJustin T. Gibbs } 1280ff662b5cSJustin T. Gibbs 1281ff662b5cSJustin T. Gibbs if (sc->irq) { 1282e4808c4bSKip Macy unbind_from_irqhandler(sc->irq); 1283e4808c4bSKip Macy sc->irq = 0; 1284ff662b5cSJustin T. Gibbs } 128589e0f4d2SKip Macy } 128689e0f4d2SKip Macy 1287ff662b5cSJustin T. Gibbs static int 1288e4808c4bSKip Macy blkif_completion(struct xb_command *s) 128989e0f4d2SKip Macy { 1290ff662b5cSJustin T. Gibbs //printf("%s: Req %p(%d)\n", __func__, s, s->nseg); 1291ff662b5cSJustin T. Gibbs gnttab_end_foreign_access_references(s->nseg, s->sg_refs); 1292ff662b5cSJustin T. Gibbs return (BLKIF_SEGS_TO_BLOCKS(s->nseg)); 129389e0f4d2SKip Macy } 129489e0f4d2SKip Macy 1295ff662b5cSJustin T. Gibbs #if 0 129689e0f4d2SKip Macy static void 1297e4808c4bSKip Macy blkif_recover(struct xb_softc *sc) 129889e0f4d2SKip Macy { 1299e4808c4bSKip Macy /* 1300e4808c4bSKip Macy * XXX The whole concept of not quiescing and completing all i/o 1301e4808c4bSKip Macy * during suspend, and then hoping to recover and replay the 1302e4808c4bSKip Macy * resulting abandoned I/O during resume, is laughable. At best, 1303e4808c4bSKip Macy * it invalidates the i/o ordering rules required by just about 1304e4808c4bSKip Macy * every filesystem, and at worst it'll corrupt data. The code 1305e4808c4bSKip Macy * has been removed until further notice. 1306e4808c4bSKip Macy */ 130789e0f4d2SKip Macy } 1308ff662b5cSJustin T. Gibbs #endif 130989e0f4d2SKip Macy 131023dc5621SKip Macy /* ** Driver registration ** */ 131123dc5621SKip Macy static device_method_t blkfront_methods[] = { 131223dc5621SKip Macy /* Device interface */ 131323dc5621SKip Macy DEVMETHOD(device_probe, blkfront_probe), 131423dc5621SKip Macy DEVMETHOD(device_attach, blkfront_attach), 131523dc5621SKip Macy DEVMETHOD(device_detach, blkfront_detach), 131623dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 131712678024SDoug Rabson DEVMETHOD(device_suspend, blkfront_suspend), 131823dc5621SKip Macy DEVMETHOD(device_resume, blkfront_resume), 131989e0f4d2SKip Macy 132023dc5621SKip Macy /* Xenbus interface */ 1321ff662b5cSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, blkfront_backend_changed), 132289e0f4d2SKip Macy 132323dc5621SKip Macy { 0, 0 } 132489e0f4d2SKip Macy }; 132589e0f4d2SKip Macy 132623dc5621SKip Macy static driver_t blkfront_driver = { 132723dc5621SKip Macy "xbd", 132823dc5621SKip Macy blkfront_methods, 1329e4808c4bSKip Macy sizeof(struct xb_softc), 133089e0f4d2SKip Macy }; 133123dc5621SKip Macy devclass_t blkfront_devclass; 133289e0f4d2SKip Macy 1333ff662b5cSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, blkfront_driver, blkfront_devclass, 0, 0); 1334