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 5289e0f4d2SKip Macy #include <machine/xen/xen-os.h> 5312678024SDoug Rabson #include <machine/xen/xenfunc.h> 5412678024SDoug Rabson #include <xen/hypervisor.h> 553a6d1fcfSKip Macy #include <xen/xen_intr.h> 563a6d1fcfSKip Macy #include <xen/evtchn.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 6289e0f4d2SKip Macy #include <geom/geom_disk.h> 6389e0f4d2SKip Macy 6489e0f4d2SKip Macy #include <dev/xen/blkfront/block.h> 6589e0f4d2SKip Macy 6623dc5621SKip Macy #include "xenbus_if.h" 6723dc5621SKip Macy 6889e0f4d2SKip Macy /* prototypes */ 69e4808c4bSKip Macy static void xb_free_command(struct xb_command *cm); 7089e0f4d2SKip Macy static void xb_startio(struct xb_softc *sc); 71e4808c4bSKip Macy static void connect(struct xb_softc *); 7223dc5621SKip Macy static void blkfront_closing(device_t); 7323dc5621SKip Macy static int blkfront_detach(device_t); 74e4808c4bSKip Macy static int talk_to_backend(struct xb_softc *); 75e4808c4bSKip Macy static int setup_blkring(struct xb_softc *); 7689e0f4d2SKip Macy static void blkif_int(void *); 77e4808c4bSKip Macy static void blkif_recover(struct xb_softc *); 78e4808c4bSKip Macy static void blkif_completion(struct xb_command *); 79e4808c4bSKip Macy static void blkif_free(struct xb_softc *, int); 80e4808c4bSKip Macy static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int); 8189e0f4d2SKip Macy 8289e0f4d2SKip Macy #define GRANT_INVALID_REF 0 8389e0f4d2SKip Macy 8489e0f4d2SKip Macy /* Control whether runtime update of vbds is enabled. */ 8589e0f4d2SKip Macy #define ENABLE_VBD_UPDATE 0 8689e0f4d2SKip Macy 8789e0f4d2SKip Macy #if ENABLE_VBD_UPDATE 8889e0f4d2SKip Macy static void vbd_update(void); 8989e0f4d2SKip Macy #endif 9089e0f4d2SKip Macy 9189e0f4d2SKip Macy #define BLKIF_STATE_DISCONNECTED 0 9289e0f4d2SKip Macy #define BLKIF_STATE_CONNECTED 1 9389e0f4d2SKip Macy #define BLKIF_STATE_SUSPENDED 2 9489e0f4d2SKip Macy 9589e0f4d2SKip Macy #ifdef notyet 9689e0f4d2SKip Macy static char *blkif_state_name[] = { 9789e0f4d2SKip Macy [BLKIF_STATE_DISCONNECTED] = "disconnected", 9889e0f4d2SKip Macy [BLKIF_STATE_CONNECTED] = "connected", 9989e0f4d2SKip Macy [BLKIF_STATE_SUSPENDED] = "closed", 10089e0f4d2SKip Macy }; 10189e0f4d2SKip Macy 10289e0f4d2SKip Macy static char * blkif_status_name[] = { 10389e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CLOSED] = "closed", 10489e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected", 10589e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected", 10689e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CHANGED] = "changed", 10789e0f4d2SKip Macy }; 10889e0f4d2SKip Macy #endif 109e4808c4bSKip Macy 11089e0f4d2SKip Macy #if 0 11112678024SDoug Rabson #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 11289e0f4d2SKip Macy #else 11389e0f4d2SKip Macy #define DPRINTK(fmt, args...) 11489e0f4d2SKip Macy #endif 11589e0f4d2SKip Macy 11689e0f4d2SKip Macy #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ 11789e0f4d2SKip Macy (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) 11889e0f4d2SKip Macy 119e4808c4bSKip Macy #define BLKIF_MAXIO (32 * 1024) 120e4808c4bSKip Macy 12189e0f4d2SKip Macy static int blkif_open(struct disk *dp); 12289e0f4d2SKip Macy static int blkif_close(struct disk *dp); 12389e0f4d2SKip Macy static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td); 124e4808c4bSKip Macy static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm); 12589e0f4d2SKip Macy static void xb_strategy(struct bio *bp); 12689e0f4d2SKip Macy 1278698b76cSKip Macy // In order to quiesce the device during kernel dumps, outstanding requests to 1288698b76cSKip Macy // DOM0 for disk reads/writes need to be accounted for. 1298698b76cSKip Macy static int xb_dump(void *, void *, vm_offset_t, off_t, size_t); 13089e0f4d2SKip Macy 13189e0f4d2SKip Macy /* XXX move to xb_vbd.c when VBD update support is added */ 13289e0f4d2SKip Macy #define MAX_VBDS 64 13389e0f4d2SKip Macy 13489e0f4d2SKip Macy #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */ 13589e0f4d2SKip Macy #define XBD_SECTOR_SHFT 9 13689e0f4d2SKip Macy 13723dc5621SKip Macy /* 13823dc5621SKip Macy * Translate Linux major/minor to an appropriate name and unit 13923dc5621SKip Macy * number. For HVM guests, this allows us to use the same drive names 14023dc5621SKip Macy * with blkfront as the emulated drives, easing transition slightly. 14123dc5621SKip Macy */ 14223dc5621SKip Macy static void 14323dc5621SKip Macy blkfront_vdevice_to_unit(int vdevice, int *unit, const char **name) 14423dc5621SKip Macy { 14523dc5621SKip Macy static struct vdev_info { 14623dc5621SKip Macy int major; 14723dc5621SKip Macy int shift; 14823dc5621SKip Macy int base; 14923dc5621SKip Macy const char *name; 15023dc5621SKip Macy } info[] = { 15123dc5621SKip Macy {3, 6, 0, "ad"}, /* ide0 */ 15223dc5621SKip Macy {22, 6, 2, "ad"}, /* ide1 */ 15323dc5621SKip Macy {33, 6, 4, "ad"}, /* ide2 */ 15423dc5621SKip Macy {34, 6, 6, "ad"}, /* ide3 */ 15523dc5621SKip Macy {56, 6, 8, "ad"}, /* ide4 */ 15623dc5621SKip Macy {57, 6, 10, "ad"}, /* ide5 */ 15723dc5621SKip Macy {88, 6, 12, "ad"}, /* ide6 */ 15823dc5621SKip Macy {89, 6, 14, "ad"}, /* ide7 */ 15923dc5621SKip Macy {90, 6, 16, "ad"}, /* ide8 */ 16023dc5621SKip Macy {91, 6, 18, "ad"}, /* ide9 */ 16123dc5621SKip Macy 16223dc5621SKip Macy {8, 4, 0, "da"}, /* scsi disk0 */ 16323dc5621SKip Macy {65, 4, 16, "da"}, /* scsi disk1 */ 16423dc5621SKip Macy {66, 4, 32, "da"}, /* scsi disk2 */ 16523dc5621SKip Macy {67, 4, 48, "da"}, /* scsi disk3 */ 16623dc5621SKip Macy {68, 4, 64, "da"}, /* scsi disk4 */ 16723dc5621SKip Macy {69, 4, 80, "da"}, /* scsi disk5 */ 16823dc5621SKip Macy {70, 4, 96, "da"}, /* scsi disk6 */ 16923dc5621SKip Macy {71, 4, 112, "da"}, /* scsi disk7 */ 17023dc5621SKip Macy {128, 4, 128, "da"}, /* scsi disk8 */ 17123dc5621SKip Macy {129, 4, 144, "da"}, /* scsi disk9 */ 17223dc5621SKip Macy {130, 4, 160, "da"}, /* scsi disk10 */ 17323dc5621SKip Macy {131, 4, 176, "da"}, /* scsi disk11 */ 17423dc5621SKip Macy {132, 4, 192, "da"}, /* scsi disk12 */ 17523dc5621SKip Macy {133, 4, 208, "da"}, /* scsi disk13 */ 17623dc5621SKip Macy {134, 4, 224, "da"}, /* scsi disk14 */ 17723dc5621SKip Macy {135, 4, 240, "da"}, /* scsi disk15 */ 17823dc5621SKip Macy 17923dc5621SKip Macy {202, 4, 0, "xbd"}, /* xbd */ 18023dc5621SKip Macy 18123dc5621SKip Macy {0, 0, 0, NULL}, 18223dc5621SKip Macy }; 18323dc5621SKip Macy int major = vdevice >> 8; 18423dc5621SKip Macy int minor = vdevice & 0xff; 18523dc5621SKip Macy int i; 18623dc5621SKip Macy 18723dc5621SKip Macy if (vdevice & (1 << 28)) { 18823dc5621SKip Macy *unit = (vdevice & ((1 << 28) - 1)) >> 8; 18923dc5621SKip Macy *name = "xbd"; 19023dc5621SKip Macy } 19123dc5621SKip Macy 19223dc5621SKip Macy for (i = 0; info[i].major; i++) { 19323dc5621SKip Macy if (info[i].major == major) { 19423dc5621SKip Macy *unit = info[i].base + (minor >> info[i].shift); 19523dc5621SKip Macy *name = info[i].name; 19623dc5621SKip Macy return; 19723dc5621SKip Macy } 19823dc5621SKip Macy } 19923dc5621SKip Macy 20023dc5621SKip Macy *unit = minor >> 4; 20123dc5621SKip Macy *name = "xbd"; 20223dc5621SKip Macy } 20323dc5621SKip Macy 20489e0f4d2SKip Macy int 205e4808c4bSKip Macy xlvbd_add(struct xb_softc *sc, blkif_sector_t capacity, 206e4808c4bSKip Macy int vdevice, uint16_t vdisk_info, uint16_t sector_size) 20789e0f4d2SKip Macy { 20823dc5621SKip Macy int unit, error = 0; 20923dc5621SKip Macy const char *name; 21023dc5621SKip Macy 21123dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 21289e0f4d2SKip Macy 21323dc5621SKip Macy sc->xb_unit = unit; 21489e0f4d2SKip Macy 21523dc5621SKip Macy if (strcmp(name, "xbd")) 216e4808c4bSKip Macy device_printf(sc->xb_dev, "attaching as %s%d\n", name, unit); 21723dc5621SKip Macy 21889e0f4d2SKip Macy memset(&sc->xb_disk, 0, sizeof(sc->xb_disk)); 21989e0f4d2SKip Macy sc->xb_disk = disk_alloc(); 2203a6d1fcfSKip Macy sc->xb_disk->d_unit = sc->xb_unit; 22189e0f4d2SKip Macy sc->xb_disk->d_open = blkif_open; 22289e0f4d2SKip Macy sc->xb_disk->d_close = blkif_close; 22389e0f4d2SKip Macy sc->xb_disk->d_ioctl = blkif_ioctl; 22489e0f4d2SKip Macy sc->xb_disk->d_strategy = xb_strategy; 2258698b76cSKip Macy sc->xb_disk->d_dump = xb_dump; 2263a6d1fcfSKip Macy sc->xb_disk->d_name = name; 22789e0f4d2SKip Macy sc->xb_disk->d_drv1 = sc; 22889e0f4d2SKip Macy sc->xb_disk->d_sectorsize = sector_size; 22989e0f4d2SKip Macy 23089e0f4d2SKip Macy sc->xb_disk->d_mediasize = capacity << XBD_SECTOR_SHFT; 231e4808c4bSKip Macy sc->xb_disk->d_maxsize = BLKIF_MAXIO; 23289e0f4d2SKip Macy sc->xb_disk->d_flags = 0; 23389e0f4d2SKip Macy disk_create(sc->xb_disk, DISK_VERSION_00); 23489e0f4d2SKip Macy 23589e0f4d2SKip Macy return error; 23689e0f4d2SKip Macy } 23789e0f4d2SKip Macy 23889e0f4d2SKip Macy void 239e4808c4bSKip Macy xlvbd_del(struct xb_softc *sc) 24089e0f4d2SKip Macy { 24189e0f4d2SKip Macy 24289e0f4d2SKip Macy disk_destroy(sc->xb_disk); 24389e0f4d2SKip Macy } 24489e0f4d2SKip Macy /************************ end VBD support *****************/ 24589e0f4d2SKip Macy 24689e0f4d2SKip Macy /* 24789e0f4d2SKip Macy * Read/write routine for a buffer. Finds the proper unit, place it on 24889e0f4d2SKip Macy * the sortq and kick the controller. 24989e0f4d2SKip Macy */ 25089e0f4d2SKip Macy static void 25189e0f4d2SKip Macy xb_strategy(struct bio *bp) 25289e0f4d2SKip Macy { 25389e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1; 25489e0f4d2SKip Macy 25589e0f4d2SKip Macy /* bogus disk? */ 25689e0f4d2SKip Macy if (sc == NULL) { 25789e0f4d2SKip Macy bp->bio_error = EINVAL; 25889e0f4d2SKip Macy bp->bio_flags |= BIO_ERROR; 25989e0f4d2SKip Macy bp->bio_resid = bp->bio_bcount; 26089e0f4d2SKip Macy biodone(bp); 26189e0f4d2SKip Macy return; 26289e0f4d2SKip Macy } 26389e0f4d2SKip Macy 264e4808c4bSKip Macy /* 265e4808c4bSKip Macy * Place it in the queue of disk activities for this disk 266e4808c4bSKip Macy */ 267e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 268e4808c4bSKip Macy 269e4808c4bSKip Macy xb_enqueue_bio(sc, bp); 270e4808c4bSKip Macy xb_startio(sc); 271e4808c4bSKip Macy 272e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 273e4808c4bSKip Macy return; 274e4808c4bSKip Macy } 275e4808c4bSKip Macy 276e4808c4bSKip Macy static void 277e4808c4bSKip Macy xb_bio_complete(struct xb_softc *sc, struct xb_command *cm) 278e4808c4bSKip Macy { 279e4808c4bSKip Macy struct bio *bp; 280e4808c4bSKip Macy 281e4808c4bSKip Macy bp = cm->bp; 282e4808c4bSKip Macy 283e4808c4bSKip Macy if ( unlikely(cm->status != BLKIF_RSP_OKAY) ) { 284e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 285e4808c4bSKip Macy printf(" status: %x\n", cm->status); 286e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 287e4808c4bSKip Macy } 288e4808c4bSKip Macy 289e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 290e4808c4bSKip Macy bp->bio_error = EIO; 291e4808c4bSKip Macy else 292e4808c4bSKip Macy bp->bio_resid = 0; 293e4808c4bSKip Macy 294e4808c4bSKip Macy xb_free_command(cm); 295e4808c4bSKip Macy biodone(bp); 296e4808c4bSKip Macy } 297e4808c4bSKip Macy 2988698b76cSKip Macy // Quiesce the disk writes for a dump file before allowing the next buffer. 2998698b76cSKip Macy static void 300e4808c4bSKip Macy xb_quiesce(struct xb_softc *sc) 3018698b76cSKip Macy { 3028698b76cSKip Macy int mtd; 3038698b76cSKip Macy 3048698b76cSKip Macy // While there are outstanding requests 305e4808c4bSKip Macy while (!TAILQ_EMPTY(&sc->cm_busy)) { 306e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, mtd); 3078698b76cSKip Macy if (mtd) { 308e4808c4bSKip Macy /* Recieved request completions, update queue. */ 309e4808c4bSKip Macy blkif_int(sc); 3108698b76cSKip Macy } 311e4808c4bSKip Macy if (!TAILQ_EMPTY(&sc->cm_busy)) { 312e4808c4bSKip Macy /* 313e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 314e4808c4bSKip Macy * to complete. 315e4808c4bSKip Macy */ 316be7747b4SKip Macy HYPERVISOR_yield(); 3178698b76cSKip Macy } 3188698b76cSKip Macy } 3198698b76cSKip Macy } 3208698b76cSKip Macy 321e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 322e4808c4bSKip Macy static void 323e4808c4bSKip Macy xb_dump_complete(struct xb_command *cm) 324e4808c4bSKip Macy { 3258698b76cSKip Macy 326e4808c4bSKip Macy xb_enqueue_complete(cm); 327e4808c4bSKip Macy } 328e4808c4bSKip Macy 3298698b76cSKip Macy static int 3308698b76cSKip Macy xb_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 3318698b76cSKip Macy size_t length) 3328698b76cSKip Macy { 3338698b76cSKip Macy struct disk *dp = arg; 3348698b76cSKip Macy struct xb_softc *sc = (struct xb_softc *) dp->d_drv1; 335e4808c4bSKip Macy struct xb_command *cm; 336e4808c4bSKip Macy size_t chunk; 337e4808c4bSKip Macy int sbp; 3388698b76cSKip Macy int rc = 0; 3398698b76cSKip Macy 340e4808c4bSKip Macy if (length <= 0) 341e4808c4bSKip Macy return (rc); 3428698b76cSKip Macy 343e4808c4bSKip Macy xb_quiesce(sc); /* All quiet on the western front. */ 344e4808c4bSKip Macy 345e4808c4bSKip Macy /* 346e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 347e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 348e4808c4bSKip Macy */ 349e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 350e4808c4bSKip Macy 351e4808c4bSKip Macy /* Split the 64KB block as needed */ 352e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 353e4808c4bSKip Macy cm = xb_dequeue_free(sc); 354e4808c4bSKip Macy if (cm == NULL) { 355e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 356e4808c4bSKip Macy device_printf(sc->xb_dev, "dump: no more commands?\n"); 357e4808c4bSKip Macy return (EBUSY); 358e4808c4bSKip Macy } 359e4808c4bSKip Macy 360e4808c4bSKip Macy if (gnttab_alloc_grant_references( 361e4808c4bSKip Macy BLKIF_MAX_SEGMENTS_PER_REQUEST, &cm->gref_head) < 0) { 362e4808c4bSKip Macy xb_free_command(cm); 363e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 364e4808c4bSKip Macy device_printf(sc->xb_dev, "no more grant allocs?\n"); 365e4808c4bSKip Macy return (EBUSY); 366e4808c4bSKip Macy } 367e4808c4bSKip Macy 368e4808c4bSKip Macy chunk = length > BLKIF_MAXIO ? BLKIF_MAXIO : length; 369e4808c4bSKip Macy cm->data = virtual; 370e4808c4bSKip Macy cm->datalen = chunk; 371e4808c4bSKip Macy cm->operation = BLKIF_OP_WRITE; 372e4808c4bSKip Macy cm->sector_number = offset / dp->d_sectorsize; 373e4808c4bSKip Macy cm->cm_complete = xb_dump_complete; 374e4808c4bSKip Macy 375e4808c4bSKip Macy xb_enqueue_ready(cm); 3768698b76cSKip Macy 3778698b76cSKip Macy length -= chunk; 3788698b76cSKip Macy offset += chunk; 3798698b76cSKip Macy virtual = (char *) virtual + chunk; 3808698b76cSKip Macy } 3818698b76cSKip Macy 382e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 383e4808c4bSKip Macy xb_startio(sc); 384e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 385e4808c4bSKip Macy 386e4808c4bSKip Macy /* Poll for the completion. */ 387e4808c4bSKip Macy xb_quiesce(sc); /* All quite on the eastern front */ 388e4808c4bSKip Macy 389e4808c4bSKip Macy /* If there were any errors, bail out... */ 390e4808c4bSKip Macy while ((cm = xb_dequeue_complete(sc)) != NULL) { 391e4808c4bSKip Macy if (cm->status != BLKIF_RSP_OKAY) { 392e4808c4bSKip Macy device_printf(sc->xb_dev, 393e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 394e4808c4bSKip Macy cm->sector_number); 395e4808c4bSKip Macy rc = EIO; 3968698b76cSKip Macy } 397e4808c4bSKip Macy xb_free_command(cm); 3988698b76cSKip Macy } 399e4808c4bSKip Macy 4008698b76cSKip Macy return (rc); 4018698b76cSKip Macy } 4028698b76cSKip Macy 4038698b76cSKip Macy 40423dc5621SKip Macy static int 40523dc5621SKip Macy blkfront_probe(device_t dev) 40689e0f4d2SKip Macy { 40723dc5621SKip Macy 40823dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vbd")) { 40923dc5621SKip Macy device_set_desc(dev, "Virtual Block Device"); 41023dc5621SKip Macy device_quiet(dev); 41123dc5621SKip Macy return (0); 41223dc5621SKip Macy } 41323dc5621SKip Macy 41423dc5621SKip Macy return (ENXIO); 41523dc5621SKip Macy } 41623dc5621SKip Macy 41723dc5621SKip Macy /* 41823dc5621SKip Macy * Setup supplies the backend dir, virtual device. We place an event 41923dc5621SKip Macy * channel and shared frame entries. We watch backend to wait if it's 42023dc5621SKip Macy * ok. 42123dc5621SKip Macy */ 42223dc5621SKip Macy static int 42323dc5621SKip Macy blkfront_attach(device_t dev) 42423dc5621SKip Macy { 425e4808c4bSKip Macy struct xb_softc *sc; 426e4808c4bSKip Macy struct xb_command *cm; 42723dc5621SKip Macy const char *name; 428e4808c4bSKip Macy int error, vdevice, i, unit; 42989e0f4d2SKip Macy 43089e0f4d2SKip Macy /* FIXME: Use dynamic device id if this is not set. */ 43112678024SDoug Rabson error = xenbus_scanf(XBT_NIL, xenbus_get_node(dev), 4323a6d1fcfSKip Macy "virtual-device", NULL, "%i", &vdevice); 43312678024SDoug Rabson if (error) { 43412678024SDoug Rabson xenbus_dev_fatal(dev, error, "reading virtual-device"); 4355d254c04SKip Macy printf("couldn't find virtual device"); 43612678024SDoug Rabson return (error); 43789e0f4d2SKip Macy } 43889e0f4d2SKip Macy 43923dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 44023dc5621SKip Macy if (!strcmp(name, "xbd")) 44123dc5621SKip Macy device_set_unit(dev, unit); 44223dc5621SKip Macy 443e4808c4bSKip Macy sc = device_get_softc(dev); 444e4808c4bSKip Macy mtx_init(&sc->xb_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 445e4808c4bSKip Macy xb_initq_free(sc); 446e4808c4bSKip Macy xb_initq_busy(sc); 447e4808c4bSKip Macy xb_initq_ready(sc); 448e4808c4bSKip Macy xb_initq_complete(sc); 449e4808c4bSKip Macy xb_initq_bio(sc); 45089e0f4d2SKip Macy 451e4808c4bSKip Macy /* Allocate parent DMA tag */ 452e4808c4bSKip Macy if (bus_dma_tag_create( NULL, /* parent */ 453a4660d59SJustin T. Gibbs 512, 4096, /* algnmnt, boundary */ 454e4808c4bSKip Macy BUS_SPACE_MAXADDR, /* lowaddr */ 455e4808c4bSKip Macy BUS_SPACE_MAXADDR, /* highaddr */ 456e4808c4bSKip Macy NULL, NULL, /* filter, filterarg */ 457e4808c4bSKip Macy BLKIF_MAXIO, /* maxsize */ 458e4808c4bSKip Macy BLKIF_MAX_SEGMENTS_PER_REQUEST, /* nsegments */ 459e4808c4bSKip Macy PAGE_SIZE, /* maxsegsize */ 460e4808c4bSKip Macy BUS_DMA_ALLOCNOW, /* flags */ 461e4808c4bSKip Macy busdma_lock_mutex, /* lockfunc */ 462e4808c4bSKip Macy &sc->xb_io_lock, /* lockarg */ 463e4808c4bSKip Macy &sc->xb_io_dmat)) { 464e4808c4bSKip Macy device_printf(dev, "Cannot allocate parent DMA tag\n"); 465e4808c4bSKip Macy return (ENOMEM); 466e4808c4bSKip Macy } 467e4808c4bSKip Macy #ifdef notyet 468e4808c4bSKip Macy if (bus_dma_tag_set(sc->xb_io_dmat, BUS_DMA_SET_MINSEGSZ, 469e4808c4bSKip Macy XBD_SECTOR_SIZE)) { 470e4808c4bSKip Macy device_printf(dev, "Cannot set sector size\n"); 471e4808c4bSKip Macy return (EINVAL); 472e4808c4bSKip Macy } 473e4808c4bSKip Macy #endif 47489e0f4d2SKip Macy 475e4808c4bSKip Macy sc->xb_dev = dev; 476e4808c4bSKip Macy sc->vdevice = vdevice; 477e4808c4bSKip Macy sc->connected = BLKIF_STATE_DISCONNECTED; 47889e0f4d2SKip Macy 47989e0f4d2SKip Macy /* work queue needed ? */ 480e4808c4bSKip Macy for (i = 0; i < BLK_RING_SIZE; i++) { 481e4808c4bSKip Macy cm = &sc->shadow[i]; 482e4808c4bSKip Macy cm->req.id = i; 483e4808c4bSKip Macy cm->cm_sc = sc; 484e4808c4bSKip Macy if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0) 485e4808c4bSKip Macy break; 486e4808c4bSKip Macy xb_free_command(cm); 487e4808c4bSKip Macy } 48889e0f4d2SKip Macy 48989e0f4d2SKip Macy /* Front end dir is a number, which is used as the id. */ 490e4808c4bSKip Macy sc->handle = strtoul(strrchr(xenbus_get_node(dev),'/')+1, NULL, 0); 49189e0f4d2SKip Macy 492e4808c4bSKip Macy error = talk_to_backend(sc); 49312678024SDoug Rabson if (error) 49412678024SDoug Rabson return (error); 49512678024SDoug Rabson 49612678024SDoug Rabson return (0); 49712678024SDoug Rabson } 49812678024SDoug Rabson 49912678024SDoug Rabson static int 50012678024SDoug Rabson blkfront_suspend(device_t dev) 50112678024SDoug Rabson { 502e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 50312678024SDoug Rabson 50412678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 505e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 506e4808c4bSKip Macy sc->connected = BLKIF_STATE_SUSPENDED; 507e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 50889e0f4d2SKip Macy 50923dc5621SKip Macy return (0); 51089e0f4d2SKip Macy } 51189e0f4d2SKip Macy 51223dc5621SKip Macy static int 51323dc5621SKip Macy blkfront_resume(device_t dev) 51489e0f4d2SKip Macy { 515e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 51689e0f4d2SKip Macy int err; 51789e0f4d2SKip Macy 51812678024SDoug Rabson DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev)); 51989e0f4d2SKip Macy 520e4808c4bSKip Macy blkif_free(sc, 1); 521e4808c4bSKip Macy err = talk_to_backend(sc); 522e4808c4bSKip Macy if (sc->connected == BLKIF_STATE_SUSPENDED && !err) 523e4808c4bSKip Macy blkif_recover(sc); 52489e0f4d2SKip Macy 52512678024SDoug Rabson return (err); 52689e0f4d2SKip Macy } 52789e0f4d2SKip Macy 52889e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */ 52923dc5621SKip Macy static int 530e4808c4bSKip Macy talk_to_backend(struct xb_softc *sc) 53189e0f4d2SKip Macy { 532e4808c4bSKip Macy device_t dev; 53389e0f4d2SKip Macy struct xenbus_transaction xbt; 534e4808c4bSKip Macy const char *message = NULL; 53589e0f4d2SKip Macy int err; 53689e0f4d2SKip Macy 53789e0f4d2SKip Macy /* Create shared ring, alloc event channel. */ 538e4808c4bSKip Macy dev = sc->xb_dev; 539e4808c4bSKip Macy err = setup_blkring(sc); 54089e0f4d2SKip Macy if (err) 54189e0f4d2SKip Macy goto out; 54289e0f4d2SKip Macy 54389e0f4d2SKip Macy again: 54489e0f4d2SKip Macy err = xenbus_transaction_start(&xbt); 54589e0f4d2SKip Macy if (err) { 54689e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "starting transaction"); 54789e0f4d2SKip Macy goto destroy_blkring; 54889e0f4d2SKip Macy } 54989e0f4d2SKip Macy 55023dc5621SKip Macy err = xenbus_printf(xbt, xenbus_get_node(dev), 551e4808c4bSKip Macy "ring-ref","%u", sc->ring_ref); 55289e0f4d2SKip Macy if (err) { 55389e0f4d2SKip Macy message = "writing ring-ref"; 55489e0f4d2SKip Macy goto abort_transaction; 55589e0f4d2SKip Macy } 55623dc5621SKip Macy err = xenbus_printf(xbt, xenbus_get_node(dev), 557e4808c4bSKip Macy "event-channel", "%u", irq_to_evtchn_port(sc->irq)); 55889e0f4d2SKip Macy if (err) { 55989e0f4d2SKip Macy message = "writing event-channel"; 56089e0f4d2SKip Macy goto abort_transaction; 56189e0f4d2SKip Macy } 56223dc5621SKip Macy err = xenbus_printf(xbt, xenbus_get_node(dev), 56323dc5621SKip Macy "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE); 56423dc5621SKip Macy if (err) { 56523dc5621SKip Macy message = "writing protocol"; 56623dc5621SKip Macy goto abort_transaction; 56723dc5621SKip Macy } 56812678024SDoug Rabson 56989e0f4d2SKip Macy err = xenbus_transaction_end(xbt, 0); 57089e0f4d2SKip Macy if (err) { 5713a6d1fcfSKip Macy if (err == EAGAIN) 57289e0f4d2SKip Macy goto again; 57389e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "completing transaction"); 57489e0f4d2SKip Macy goto destroy_blkring; 57589e0f4d2SKip Macy } 57623dc5621SKip Macy xenbus_set_state(dev, XenbusStateInitialised); 57789e0f4d2SKip Macy 57889e0f4d2SKip Macy return 0; 57989e0f4d2SKip Macy 58089e0f4d2SKip Macy abort_transaction: 58189e0f4d2SKip Macy xenbus_transaction_end(xbt, 1); 58289e0f4d2SKip Macy if (message) 58389e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "%s", message); 58489e0f4d2SKip Macy destroy_blkring: 585e4808c4bSKip Macy blkif_free(sc, 0); 58689e0f4d2SKip Macy out: 58789e0f4d2SKip Macy return err; 58889e0f4d2SKip Macy } 58989e0f4d2SKip Macy 59089e0f4d2SKip Macy static int 591e4808c4bSKip Macy setup_blkring(struct xb_softc *sc) 59289e0f4d2SKip Macy { 59389e0f4d2SKip Macy blkif_sring_t *sring; 5943a6d1fcfSKip Macy int error; 59589e0f4d2SKip Macy 596e4808c4bSKip Macy sc->ring_ref = GRANT_INVALID_REF; 59789e0f4d2SKip Macy 59889e0f4d2SKip Macy sring = (blkif_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO); 59989e0f4d2SKip Macy if (sring == NULL) { 600e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, ENOMEM, "allocating shared ring"); 60189e0f4d2SKip Macy return ENOMEM; 60289e0f4d2SKip Macy } 60389e0f4d2SKip Macy SHARED_RING_INIT(sring); 604e4808c4bSKip Macy FRONT_RING_INIT(&sc->ring, sring, PAGE_SIZE); 60589e0f4d2SKip Macy 606e4808c4bSKip Macy error = xenbus_grant_ring(sc->xb_dev, 607e4808c4bSKip Macy (vtomach(sc->ring.sring) >> PAGE_SHIFT), &sc->ring_ref); 6083a6d1fcfSKip Macy if (error) { 60989e0f4d2SKip Macy free(sring, M_DEVBUF); 610e4808c4bSKip Macy sc->ring.sring = NULL; 61189e0f4d2SKip Macy goto fail; 61289e0f4d2SKip Macy } 61389e0f4d2SKip Macy 614e4808c4bSKip Macy error = bind_listening_port_to_irqhandler(xenbus_get_otherend_id(sc->xb_dev), 615e4808c4bSKip Macy "xbd", (driver_intr_t *)blkif_int, sc, 616e4808c4bSKip Macy INTR_TYPE_BIO | INTR_MPSAFE, &sc->irq); 6173a6d1fcfSKip Macy if (error) { 618e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, error, 61989e0f4d2SKip Macy "bind_evtchn_to_irqhandler failed"); 62089e0f4d2SKip Macy goto fail; 62189e0f4d2SKip Macy } 62289e0f4d2SKip Macy 6233a6d1fcfSKip Macy return (0); 62489e0f4d2SKip Macy fail: 625e4808c4bSKip Macy blkif_free(sc, 0); 6263a6d1fcfSKip Macy return (error); 62789e0f4d2SKip Macy } 62889e0f4d2SKip Macy 62989e0f4d2SKip Macy 63089e0f4d2SKip Macy /** 63189e0f4d2SKip Macy * Callback received when the backend's state changes. 63289e0f4d2SKip Macy */ 633cfed3783SKip Macy static int 63423dc5621SKip Macy blkfront_backend_changed(device_t dev, XenbusState backend_state) 63589e0f4d2SKip Macy { 636e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 63789e0f4d2SKip Macy 63812678024SDoug Rabson DPRINTK("backend_state=%d\n", backend_state); 63989e0f4d2SKip Macy 64089e0f4d2SKip Macy switch (backend_state) { 64189e0f4d2SKip Macy case XenbusStateUnknown: 64289e0f4d2SKip Macy case XenbusStateInitialising: 64389e0f4d2SKip Macy case XenbusStateInitWait: 64489e0f4d2SKip Macy case XenbusStateInitialised: 64589e0f4d2SKip Macy case XenbusStateClosed: 646920ba15bSKip Macy case XenbusStateReconfigured: 647920ba15bSKip Macy case XenbusStateReconfiguring: 64889e0f4d2SKip Macy break; 64989e0f4d2SKip Macy 65089e0f4d2SKip Macy case XenbusStateConnected: 651e4808c4bSKip Macy connect(sc); 65289e0f4d2SKip Macy break; 65389e0f4d2SKip Macy 65489e0f4d2SKip Macy case XenbusStateClosing: 655e4808c4bSKip Macy if (sc->users > 0) 65689e0f4d2SKip Macy xenbus_dev_error(dev, -EBUSY, 65789e0f4d2SKip Macy "Device in use; refusing to close"); 65889e0f4d2SKip Macy else 65989e0f4d2SKip Macy blkfront_closing(dev); 66089e0f4d2SKip Macy #ifdef notyet 661e4808c4bSKip Macy bd = bdget(sc->dev); 66289e0f4d2SKip Macy if (bd == NULL) 66389e0f4d2SKip Macy xenbus_dev_fatal(dev, -ENODEV, "bdget failed"); 66489e0f4d2SKip Macy 66589e0f4d2SKip Macy down(&bd->bd_sem); 666e4808c4bSKip Macy if (sc->users > 0) 66789e0f4d2SKip Macy xenbus_dev_error(dev, -EBUSY, 66889e0f4d2SKip Macy "Device in use; refusing to close"); 66989e0f4d2SKip Macy else 67089e0f4d2SKip Macy blkfront_closing(dev); 67189e0f4d2SKip Macy up(&bd->bd_sem); 67289e0f4d2SKip Macy bdput(bd); 67389e0f4d2SKip Macy #endif 67489e0f4d2SKip Macy } 675cfed3783SKip Macy 676cfed3783SKip Macy return (0); 67789e0f4d2SKip Macy } 67889e0f4d2SKip Macy 67989e0f4d2SKip Macy /* 68089e0f4d2SKip Macy ** Invoked when the backend is finally 'ready' (and has told produced 68189e0f4d2SKip Macy ** the details about the physical device - #sectors, size, etc). 68289e0f4d2SKip Macy */ 68389e0f4d2SKip Macy static void 684e4808c4bSKip Macy connect(struct xb_softc *sc) 68589e0f4d2SKip Macy { 686e4808c4bSKip Macy device_t dev = sc->xb_dev; 68789e0f4d2SKip Macy unsigned long sectors, sector_size; 68889e0f4d2SKip Macy unsigned int binfo; 689e4808c4bSKip Macy int err, feature_barrier; 69089e0f4d2SKip Macy 691e4808c4bSKip Macy if( (sc->connected == BLKIF_STATE_CONNECTED) || 692e4808c4bSKip Macy (sc->connected == BLKIF_STATE_SUSPENDED) ) 69389e0f4d2SKip Macy return; 69489e0f4d2SKip Macy 69523dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 69689e0f4d2SKip Macy 69723dc5621SKip Macy err = xenbus_gather(XBT_NIL, xenbus_get_otherend_path(dev), 69889e0f4d2SKip Macy "sectors", "%lu", §ors, 69989e0f4d2SKip Macy "info", "%u", &binfo, 70089e0f4d2SKip Macy "sector-size", "%lu", §or_size, 70189e0f4d2SKip Macy NULL); 70289e0f4d2SKip Macy if (err) { 70323dc5621SKip Macy xenbus_dev_fatal(dev, err, 70489e0f4d2SKip Macy "reading backend fields at %s", 70523dc5621SKip Macy xenbus_get_otherend_path(dev)); 70689e0f4d2SKip Macy return; 70789e0f4d2SKip Macy } 70823dc5621SKip Macy err = xenbus_gather(XBT_NIL, xenbus_get_otherend_path(dev), 709e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 71089e0f4d2SKip Macy NULL); 711e4808c4bSKip Macy if (!err || feature_barrier) 712e4808c4bSKip Macy sc->xb_flags |= XB_BARRIER; 71389e0f4d2SKip Macy 71423dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 71523dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 71623dc5621SKip Macy device_get_desc(dev), 71723dc5621SKip Macy xenbus_get_node(dev)); 71823dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 71989e0f4d2SKip Macy 720e4808c4bSKip Macy xlvbd_add(sc, sectors, sc->vdevice, binfo, sector_size); 72123dc5621SKip Macy 72223dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 72389e0f4d2SKip Macy 72489e0f4d2SKip Macy /* Kick pending requests. */ 725e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 726e4808c4bSKip Macy sc->connected = BLKIF_STATE_CONNECTED; 727e4808c4bSKip Macy xb_startio(sc); 728e4808c4bSKip Macy sc->xb_flags |= XB_READY; 729e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 73089e0f4d2SKip Macy 73189e0f4d2SKip Macy } 73289e0f4d2SKip Macy 73389e0f4d2SKip Macy /** 73489e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 73589e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 736a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 73789e0f4d2SKip Macy * acknowledgement. 73889e0f4d2SKip Macy */ 73923dc5621SKip Macy static void 74023dc5621SKip Macy blkfront_closing(device_t dev) 74189e0f4d2SKip Macy { 742e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 74389e0f4d2SKip Macy 74423dc5621SKip Macy DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev)); 74589e0f4d2SKip Macy 746e4808c4bSKip Macy if (sc->mi) { 74789e0f4d2SKip Macy DPRINTK("Calling xlvbd_del\n"); 748e4808c4bSKip Macy xlvbd_del(sc); 749e4808c4bSKip Macy sc->mi = NULL; 75089e0f4d2SKip Macy } 75189e0f4d2SKip Macy 75223dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 75389e0f4d2SKip Macy } 75489e0f4d2SKip Macy 75589e0f4d2SKip Macy 75623dc5621SKip Macy static int 75723dc5621SKip Macy blkfront_detach(device_t dev) 75889e0f4d2SKip Macy { 759e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 76089e0f4d2SKip Macy 76123dc5621SKip Macy DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev)); 76289e0f4d2SKip Macy 763e4808c4bSKip Macy blkif_free(sc, 0); 764e4808c4bSKip Macy mtx_destroy(&sc->xb_io_lock); 76589e0f4d2SKip Macy 76689e0f4d2SKip Macy return 0; 76789e0f4d2SKip Macy } 76889e0f4d2SKip Macy 76989e0f4d2SKip Macy 77089e0f4d2SKip Macy static inline void 771e4808c4bSKip Macy flush_requests(struct xb_softc *sc) 77289e0f4d2SKip Macy { 77389e0f4d2SKip Macy int notify; 77489e0f4d2SKip Macy 775e4808c4bSKip Macy RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->ring, notify); 77689e0f4d2SKip Macy 77789e0f4d2SKip Macy if (notify) 778e4808c4bSKip Macy notify_remote_via_irq(sc->irq); 77989e0f4d2SKip Macy } 78089e0f4d2SKip Macy 78189e0f4d2SKip Macy static void blkif_restart_queue_callback(void *arg) 78289e0f4d2SKip Macy { 783e4808c4bSKip Macy struct xb_softc *sc = arg; 78489e0f4d2SKip Macy 785e4808c4bSKip Macy xb_startio(sc); 78689e0f4d2SKip Macy } 78789e0f4d2SKip Macy 78889e0f4d2SKip Macy static int 78989e0f4d2SKip Macy blkif_open(struct disk *dp) 79089e0f4d2SKip Macy { 79189e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 79289e0f4d2SKip Macy 79389e0f4d2SKip Macy if (sc == NULL) { 79412678024SDoug Rabson printf("xb%d: not found", sc->xb_unit); 79589e0f4d2SKip Macy return (ENXIO); 79689e0f4d2SKip Macy } 79789e0f4d2SKip Macy 79889e0f4d2SKip Macy sc->xb_flags |= XB_OPEN; 799e4808c4bSKip Macy sc->users++; 80089e0f4d2SKip Macy return (0); 80189e0f4d2SKip Macy } 80289e0f4d2SKip Macy 80389e0f4d2SKip Macy static int 80489e0f4d2SKip Macy blkif_close(struct disk *dp) 80589e0f4d2SKip Macy { 80689e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 80789e0f4d2SKip Macy 80889e0f4d2SKip Macy if (sc == NULL) 80989e0f4d2SKip Macy return (ENXIO); 81089e0f4d2SKip Macy sc->xb_flags &= ~XB_OPEN; 811e4808c4bSKip Macy if (--(sc->users) == 0) { 81289e0f4d2SKip Macy /* Check whether we have been instructed to close. We will 81389e0f4d2SKip Macy have ignored this request initially, as the device was 81489e0f4d2SKip Macy still mounted. */ 815e4808c4bSKip Macy device_t dev = sc->xb_dev; 81623dc5621SKip Macy XenbusState state = 81723dc5621SKip Macy xenbus_read_driver_state(xenbus_get_otherend_path(dev)); 81889e0f4d2SKip Macy 81989e0f4d2SKip Macy if (state == XenbusStateClosing) 82089e0f4d2SKip Macy blkfront_closing(dev); 82189e0f4d2SKip Macy } 82289e0f4d2SKip Macy return (0); 82389e0f4d2SKip Macy } 82489e0f4d2SKip Macy 82589e0f4d2SKip Macy static int 82689e0f4d2SKip Macy blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 82789e0f4d2SKip Macy { 82889e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 82989e0f4d2SKip Macy 83089e0f4d2SKip Macy if (sc == NULL) 83189e0f4d2SKip Macy return (ENXIO); 83289e0f4d2SKip Macy 83389e0f4d2SKip Macy return (ENOTTY); 83489e0f4d2SKip Macy } 83589e0f4d2SKip Macy 836e4808c4bSKip Macy static void 837e4808c4bSKip Macy xb_free_command(struct xb_command *cm) 838e4808c4bSKip Macy { 839e4808c4bSKip Macy 840e4808c4bSKip Macy KASSERT((cm->cm_flags & XB_ON_XBQ_MASK) == 0, 841e4808c4bSKip Macy ("Freeing command that is still on a queue\n")); 842e4808c4bSKip Macy 843e4808c4bSKip Macy cm->cm_flags = 0; 844e4808c4bSKip Macy cm->bp = NULL; 845e4808c4bSKip Macy cm->cm_complete = NULL; 846e4808c4bSKip Macy xb_enqueue_free(cm); 847e4808c4bSKip Macy } 84889e0f4d2SKip Macy 84989e0f4d2SKip Macy /* 85089e0f4d2SKip Macy * blkif_queue_request 85189e0f4d2SKip Macy * 85289e0f4d2SKip Macy * request block io 85389e0f4d2SKip Macy * 85489e0f4d2SKip Macy * id: for guest use only. 85589e0f4d2SKip Macy * operation: BLKIF_OP_{READ,WRITE,PROBE} 85689e0f4d2SKip Macy * buffer: buffer to read/write into. this should be a 85789e0f4d2SKip Macy * virtual address in the guest os. 85889e0f4d2SKip Macy */ 859e4808c4bSKip Macy static struct xb_command * 860e4808c4bSKip Macy xb_bio_command(struct xb_softc *sc) 86189e0f4d2SKip Macy { 862e4808c4bSKip Macy struct xb_command *cm; 863e4808c4bSKip Macy struct bio *bp; 86489e0f4d2SKip Macy 865e4808c4bSKip Macy if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) 866e4808c4bSKip Macy return (NULL); 86789e0f4d2SKip Macy 868e4808c4bSKip Macy bp = xb_dequeue_bio(sc); 869e4808c4bSKip Macy if (bp == NULL) 870e4808c4bSKip Macy return (NULL); 871e4808c4bSKip Macy 872e4808c4bSKip Macy if ((cm = xb_dequeue_free(sc)) == NULL) { 873e4808c4bSKip Macy xb_requeue_bio(sc, bp); 874e4808c4bSKip Macy return (NULL); 87589e0f4d2SKip Macy } 87689e0f4d2SKip Macy 877e4808c4bSKip Macy if (gnttab_alloc_grant_references(BLKIF_MAX_SEGMENTS_PER_REQUEST, 878e4808c4bSKip Macy &cm->gref_head) < 0) { 879e4808c4bSKip Macy gnttab_request_free_callback(&sc->callback, 880e4808c4bSKip Macy blkif_restart_queue_callback, sc, 881e4808c4bSKip Macy BLKIF_MAX_SEGMENTS_PER_REQUEST); 882e4808c4bSKip Macy xb_requeue_bio(sc, bp); 883e4808c4bSKip Macy xb_enqueue_free(cm); 884e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 885e4808c4bSKip Macy return (NULL); 886e4808c4bSKip Macy } 88789e0f4d2SKip Macy 888e4808c4bSKip Macy /* XXX Can we grab refs before doing the load so that the ref can 889e4808c4bSKip Macy * be filled out here? 890e4808c4bSKip Macy */ 891e4808c4bSKip Macy cm->bp = bp; 892e4808c4bSKip Macy cm->data = bp->bio_data; 893e4808c4bSKip Macy cm->datalen = bp->bio_bcount; 894e4808c4bSKip Macy cm->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ : 895e4808c4bSKip Macy BLKIF_OP_WRITE; 896e4808c4bSKip Macy cm->sector_number = (blkif_sector_t)bp->bio_pblkno; 89789e0f4d2SKip Macy 898e4808c4bSKip Macy return (cm); 899e4808c4bSKip Macy } 90089e0f4d2SKip Macy 901e4808c4bSKip Macy static int 902e4808c4bSKip Macy blkif_queue_request(struct xb_softc *sc, struct xb_command *cm) 903e4808c4bSKip Macy { 904e4808c4bSKip Macy int error; 905e4808c4bSKip Macy 906e4808c4bSKip Macy error = bus_dmamap_load(sc->xb_io_dmat, cm->map, cm->data, cm->datalen, 907e4808c4bSKip Macy blkif_queue_cb, cm, 0); 908e4808c4bSKip Macy if (error == EINPROGRESS) { 909e4808c4bSKip Macy printf("EINPROGRESS\n"); 910e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 911e4808c4bSKip Macy cm->cm_flags |= XB_CMD_FROZEN; 912e4808c4bSKip Macy return (0); 913e4808c4bSKip Macy } 914e4808c4bSKip Macy 915e4808c4bSKip Macy return (error); 916e4808c4bSKip Macy } 917e4808c4bSKip Macy 918e4808c4bSKip Macy static void 919e4808c4bSKip Macy blkif_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 920e4808c4bSKip Macy { 921e4808c4bSKip Macy struct xb_softc *sc; 922e4808c4bSKip Macy struct xb_command *cm; 923e4808c4bSKip Macy blkif_request_t *ring_req; 924e4808c4bSKip Macy vm_paddr_t buffer_ma; 925e4808c4bSKip Macy uint64_t fsect, lsect; 926e4808c4bSKip Macy int ref, i, op; 927e4808c4bSKip Macy 928e4808c4bSKip Macy cm = arg; 929e4808c4bSKip Macy sc = cm->cm_sc; 930e4808c4bSKip Macy 931e4808c4bSKip Macy if (error) { 932e4808c4bSKip Macy printf("error %d in blkif_queue_cb\n", error); 933e4808c4bSKip Macy cm->bp->bio_error = EIO; 934e4808c4bSKip Macy biodone(cm->bp); 935e4808c4bSKip Macy xb_free_command(cm); 936e4808c4bSKip Macy return; 937e4808c4bSKip Macy } 93889e0f4d2SKip Macy 93989e0f4d2SKip Macy /* Fill out a communications ring structure. */ 940e4808c4bSKip Macy ring_req = RING_GET_REQUEST(&sc->ring, sc->ring.req_prod_pvt); 941e4808c4bSKip Macy if (ring_req == NULL) { 942e4808c4bSKip Macy /* XXX Is this possible? */ 943e4808c4bSKip Macy printf("ring_req NULL, requeuing\n"); 944e4808c4bSKip Macy xb_enqueue_ready(cm); 945e4808c4bSKip Macy return; 946e4808c4bSKip Macy } 947e4808c4bSKip Macy ring_req->id = cm->req.id; 948e4808c4bSKip Macy ring_req->operation = cm->operation; 949e4808c4bSKip Macy ring_req->sector_number = cm->sector_number; 95089e0f4d2SKip Macy ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk; 951e4808c4bSKip Macy ring_req->nr_segments = nsegs; 95289e0f4d2SKip Macy 953e4808c4bSKip Macy for (i = 0; i < nsegs; i++) { 954e4808c4bSKip Macy buffer_ma = segs[i].ds_addr; 95589e0f4d2SKip Macy fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 956e4808c4bSKip Macy lsect = fsect + (segs[i].ds_len >> XBD_SECTOR_SHFT) - 1; 957e4808c4bSKip Macy 958e4808c4bSKip Macy KASSERT(lsect <= 7, 959e4808c4bSKip Macy ("XEN disk driver data cannot cross a page boundary")); 960e4808c4bSKip Macy 96189e0f4d2SKip Macy /* install a grant reference. */ 962e4808c4bSKip Macy ref = gnttab_claim_grant_reference(&cm->gref_head); 963e4808c4bSKip Macy KASSERT( ref != ENOSPC, ("grant_reference failed") ); 96489e0f4d2SKip Macy 96589e0f4d2SKip Macy gnttab_grant_foreign_access_ref( 96689e0f4d2SKip Macy ref, 967e4808c4bSKip Macy xenbus_get_otherend_id(sc->xb_dev), 96889e0f4d2SKip Macy buffer_ma >> PAGE_SHIFT, 96989e0f4d2SKip Macy ring_req->operation & 1 ); /* ??? */ 97089e0f4d2SKip Macy 971e4808c4bSKip Macy ring_req->seg[i] = 97289e0f4d2SKip Macy (struct blkif_request_segment) { 97389e0f4d2SKip Macy .gref = ref, 97489e0f4d2SKip Macy .first_sect = fsect, 97589e0f4d2SKip Macy .last_sect = lsect }; 97689e0f4d2SKip Macy } 97789e0f4d2SKip Macy 97889e0f4d2SKip Macy 979e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 980e4808c4bSKip Macy op = BUS_DMASYNC_PREREAD; 981e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 982e4808c4bSKip Macy op = BUS_DMASYNC_PREWRITE; 983e4808c4bSKip Macy else 984e4808c4bSKip Macy op = 0; 985e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 986e4808c4bSKip Macy 987e4808c4bSKip Macy sc->ring.req_prod_pvt++; 988e4808c4bSKip Macy 989e4808c4bSKip Macy /* Keep a private copy so we can reissue requests when recovering. */ 990e4808c4bSKip Macy cm->req = *ring_req; 991e4808c4bSKip Macy 992e4808c4bSKip Macy xb_enqueue_busy(cm); 993e4808c4bSKip Macy 994e4808c4bSKip Macy gnttab_free_grant_references(cm->gref_head); 995e4808c4bSKip Macy 996e4808c4bSKip Macy /* 997e4808c4bSKip Macy * This flag means that we're probably executing in the busdma swi 998e4808c4bSKip Macy * instead of in the startio context, so an explicit flush is needed. 999e4808c4bSKip Macy */ 1000e4808c4bSKip Macy if (cm->cm_flags & XB_CMD_FROZEN) 1001e4808c4bSKip Macy flush_requests(sc); 1002e4808c4bSKip Macy 1003e4808c4bSKip Macy return; 1004e4808c4bSKip Macy } 100589e0f4d2SKip Macy 100689e0f4d2SKip Macy /* 100789e0f4d2SKip Macy * Dequeue buffers and place them in the shared communication ring. 100889e0f4d2SKip Macy * Return when no more requests can be accepted or all buffers have 100989e0f4d2SKip Macy * been queued. 101089e0f4d2SKip Macy * 101189e0f4d2SKip Macy * Signal XEN once the ring has been filled out. 101289e0f4d2SKip Macy */ 101389e0f4d2SKip Macy static void 101489e0f4d2SKip Macy xb_startio(struct xb_softc *sc) 101589e0f4d2SKip Macy { 1016e4808c4bSKip Macy struct xb_command *cm; 1017e4808c4bSKip Macy int error, queued = 0; 101889e0f4d2SKip Macy 1019e4808c4bSKip Macy mtx_assert(&sc->xb_io_lock, MA_OWNED); 102089e0f4d2SKip Macy 1021e4808c4bSKip Macy while (!RING_FULL(&sc->ring)) { 1022e4808c4bSKip Macy if (sc->xb_flags & XB_FROZEN) 1023e4808c4bSKip Macy break; 102489e0f4d2SKip Macy 1025e4808c4bSKip Macy cm = xb_dequeue_ready(sc); 102689e0f4d2SKip Macy 1027e4808c4bSKip Macy if (cm == NULL) 1028e4808c4bSKip Macy cm = xb_bio_command(sc); 1029e4808c4bSKip Macy 1030e4808c4bSKip Macy if (cm == NULL) 1031e4808c4bSKip Macy break; 1032e4808c4bSKip Macy 1033e4808c4bSKip Macy if ((error = blkif_queue_request(sc, cm)) != 0) { 1034e4808c4bSKip Macy printf("blkif_queue_request returned %d\n", error); 103589e0f4d2SKip Macy break; 103689e0f4d2SKip Macy } 103789e0f4d2SKip Macy queued++; 103889e0f4d2SKip Macy } 103989e0f4d2SKip Macy 104089e0f4d2SKip Macy if (queued != 0) 1041e4808c4bSKip Macy flush_requests(sc); 104289e0f4d2SKip Macy } 104389e0f4d2SKip Macy 104489e0f4d2SKip Macy static void 104589e0f4d2SKip Macy blkif_int(void *xsc) 104689e0f4d2SKip Macy { 1047e4808c4bSKip Macy struct xb_softc *sc = xsc; 1048e4808c4bSKip Macy struct xb_command *cm; 104989e0f4d2SKip Macy blkif_response_t *bret; 105089e0f4d2SKip Macy RING_IDX i, rp; 1051e4808c4bSKip Macy int op; 105289e0f4d2SKip Macy 1053e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 105489e0f4d2SKip Macy 1055e4808c4bSKip Macy if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) { 1056e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 105789e0f4d2SKip Macy return; 105889e0f4d2SKip Macy } 105989e0f4d2SKip Macy 106089e0f4d2SKip Macy again: 1061e4808c4bSKip Macy rp = sc->ring.sring->rsp_prod; 106289e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 106389e0f4d2SKip Macy 1064e4808c4bSKip Macy for (i = sc->ring.rsp_cons; i != rp; i++) { 1065e4808c4bSKip Macy bret = RING_GET_RESPONSE(&sc->ring, i); 1066e4808c4bSKip Macy cm = &sc->shadow[bret->id]; 106789e0f4d2SKip Macy 1068e4808c4bSKip Macy xb_remove_busy(cm); 1069e4808c4bSKip Macy blkif_completion(cm); 107089e0f4d2SKip Macy 1071e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 1072e4808c4bSKip Macy op = BUS_DMASYNC_POSTREAD; 1073e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 1074e4808c4bSKip Macy op = BUS_DMASYNC_POSTWRITE; 107589e0f4d2SKip Macy else 1076e4808c4bSKip Macy op = 0; 1077e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 1078e4808c4bSKip Macy bus_dmamap_unload(sc->xb_io_dmat, cm->map); 107989e0f4d2SKip Macy 1080e4808c4bSKip Macy /* 1081e4808c4bSKip Macy * If commands are completing then resources are probably 1082e4808c4bSKip Macy * being freed as well. It's a cheap assumption even when 1083e4808c4bSKip Macy * wrong. 1084e4808c4bSKip Macy */ 1085e4808c4bSKip Macy sc->xb_flags &= ~XB_FROZEN; 1086e4808c4bSKip Macy 1087e4808c4bSKip Macy /* 1088e4808c4bSKip Macy * Directly call the i/o complete routine to save an 1089e4808c4bSKip Macy * an indirection in the common case. 1090e4808c4bSKip Macy */ 1091e4808c4bSKip Macy cm->status = bret->status; 1092e4808c4bSKip Macy if (cm->bp) 1093e4808c4bSKip Macy xb_bio_complete(sc, cm); 1094e4808c4bSKip Macy else if (cm->cm_complete) 1095e4808c4bSKip Macy (cm->cm_complete)(cm); 1096e4808c4bSKip Macy else 1097e4808c4bSKip Macy xb_free_command(cm); 109889e0f4d2SKip Macy } 109989e0f4d2SKip Macy 1100e4808c4bSKip Macy sc->ring.rsp_cons = i; 110189e0f4d2SKip Macy 1102e4808c4bSKip Macy if (i != sc->ring.req_prod_pvt) { 110389e0f4d2SKip Macy int more_to_do; 1104e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, more_to_do); 110589e0f4d2SKip Macy if (more_to_do) 110689e0f4d2SKip Macy goto again; 110789e0f4d2SKip Macy } else { 1108e4808c4bSKip Macy sc->ring.sring->rsp_event = i + 1; 110989e0f4d2SKip Macy } 111089e0f4d2SKip Macy 1111e4808c4bSKip Macy xb_startio(sc); 111289e0f4d2SKip Macy 1113e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 111489e0f4d2SKip Macy } 111589e0f4d2SKip Macy 111689e0f4d2SKip Macy static void 1117e4808c4bSKip Macy blkif_free(struct xb_softc *sc, int suspend) 111889e0f4d2SKip Macy { 111989e0f4d2SKip Macy 112089e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 1121e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 1122e4808c4bSKip Macy sc->connected = suspend ? 112389e0f4d2SKip Macy BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; 1124e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 112589e0f4d2SKip Macy 112689e0f4d2SKip Macy /* Free resources associated with old device channel. */ 1127e4808c4bSKip Macy if (sc->ring_ref != GRANT_INVALID_REF) { 1128e4808c4bSKip Macy gnttab_end_foreign_access(sc->ring_ref, 1129e4808c4bSKip Macy sc->ring.sring); 1130e4808c4bSKip Macy sc->ring_ref = GRANT_INVALID_REF; 1131e4808c4bSKip Macy sc->ring.sring = NULL; 113289e0f4d2SKip Macy } 1133e4808c4bSKip Macy if (sc->irq) 1134e4808c4bSKip Macy unbind_from_irqhandler(sc->irq); 1135e4808c4bSKip Macy sc->irq = 0; 113689e0f4d2SKip Macy 113789e0f4d2SKip Macy } 113889e0f4d2SKip Macy 113989e0f4d2SKip Macy static void 1140e4808c4bSKip Macy blkif_completion(struct xb_command *s) 114189e0f4d2SKip Macy { 114289e0f4d2SKip Macy int i; 114389e0f4d2SKip Macy 114489e0f4d2SKip Macy for (i = 0; i < s->req.nr_segments; i++) 1145920ba15bSKip Macy gnttab_end_foreign_access(s->req.seg[i].gref, 0UL); 114689e0f4d2SKip Macy } 114789e0f4d2SKip Macy 114889e0f4d2SKip Macy static void 1149e4808c4bSKip Macy blkif_recover(struct xb_softc *sc) 115089e0f4d2SKip Macy { 1151e4808c4bSKip Macy /* 1152e4808c4bSKip Macy * XXX The whole concept of not quiescing and completing all i/o 1153e4808c4bSKip Macy * during suspend, and then hoping to recover and replay the 1154e4808c4bSKip Macy * resulting abandoned I/O during resume, is laughable. At best, 1155e4808c4bSKip Macy * it invalidates the i/o ordering rules required by just about 1156e4808c4bSKip Macy * every filesystem, and at worst it'll corrupt data. The code 1157e4808c4bSKip Macy * has been removed until further notice. 1158e4808c4bSKip Macy */ 115989e0f4d2SKip Macy } 116089e0f4d2SKip Macy 116123dc5621SKip Macy /* ** Driver registration ** */ 116223dc5621SKip Macy static device_method_t blkfront_methods[] = { 116323dc5621SKip Macy /* Device interface */ 116423dc5621SKip Macy DEVMETHOD(device_probe, blkfront_probe), 116523dc5621SKip Macy DEVMETHOD(device_attach, blkfront_attach), 116623dc5621SKip Macy DEVMETHOD(device_detach, blkfront_detach), 116723dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 116812678024SDoug Rabson DEVMETHOD(device_suspend, blkfront_suspend), 116923dc5621SKip Macy DEVMETHOD(device_resume, blkfront_resume), 117089e0f4d2SKip Macy 117123dc5621SKip Macy /* Xenbus interface */ 117223dc5621SKip Macy DEVMETHOD(xenbus_backend_changed, blkfront_backend_changed), 117389e0f4d2SKip Macy 117423dc5621SKip Macy { 0, 0 } 117589e0f4d2SKip Macy }; 117689e0f4d2SKip Macy 117723dc5621SKip Macy static driver_t blkfront_driver = { 117823dc5621SKip Macy "xbd", 117923dc5621SKip Macy blkfront_methods, 1180e4808c4bSKip Macy sizeof(struct xb_softc), 118189e0f4d2SKip Macy }; 118223dc5621SKip Macy devclass_t blkfront_devclass; 118389e0f4d2SKip Macy 118423dc5621SKip Macy DRIVER_MODULE(xbd, xenbus, blkfront_driver, blkfront_devclass, 0, 0); 1185