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 static int blkif_completion(struct xb_command *); 81*06a630f6SJustin T. Gibbs static void blkif_free(struct xb_softc *); 82e4808c4bSKip Macy static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int); 8389e0f4d2SKip Macy 84ff662b5cSJustin T. Gibbs MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 85ff662b5cSJustin T. Gibbs 8689e0f4d2SKip Macy #define GRANT_INVALID_REF 0 8789e0f4d2SKip Macy 8889e0f4d2SKip Macy /* Control whether runtime update of vbds is enabled. */ 8989e0f4d2SKip Macy #define ENABLE_VBD_UPDATE 0 9089e0f4d2SKip Macy 9189e0f4d2SKip Macy #if ENABLE_VBD_UPDATE 9289e0f4d2SKip Macy static void vbd_update(void); 9389e0f4d2SKip Macy #endif 9489e0f4d2SKip Macy 9589e0f4d2SKip Macy #define BLKIF_STATE_DISCONNECTED 0 9689e0f4d2SKip Macy #define BLKIF_STATE_CONNECTED 1 9789e0f4d2SKip Macy #define BLKIF_STATE_SUSPENDED 2 9889e0f4d2SKip Macy 9989e0f4d2SKip Macy #ifdef notyet 10089e0f4d2SKip Macy static char *blkif_state_name[] = { 10189e0f4d2SKip Macy [BLKIF_STATE_DISCONNECTED] = "disconnected", 10289e0f4d2SKip Macy [BLKIF_STATE_CONNECTED] = "connected", 10389e0f4d2SKip Macy [BLKIF_STATE_SUSPENDED] = "closed", 10489e0f4d2SKip Macy }; 10589e0f4d2SKip Macy 10689e0f4d2SKip Macy static char * blkif_status_name[] = { 10789e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CLOSED] = "closed", 10889e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected", 10989e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected", 11089e0f4d2SKip Macy [BLKIF_INTERFACE_STATUS_CHANGED] = "changed", 11189e0f4d2SKip Macy }; 11289e0f4d2SKip Macy #endif 113e4808c4bSKip Macy 11489e0f4d2SKip Macy #if 0 11512678024SDoug Rabson #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 11689e0f4d2SKip Macy #else 11789e0f4d2SKip Macy #define DPRINTK(fmt, args...) 11889e0f4d2SKip Macy #endif 11989e0f4d2SKip Macy 12089e0f4d2SKip Macy static int blkif_open(struct disk *dp); 12189e0f4d2SKip Macy static int blkif_close(struct disk *dp); 12289e0f4d2SKip Macy static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td); 123e4808c4bSKip Macy static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm); 12489e0f4d2SKip Macy static void xb_strategy(struct bio *bp); 12589e0f4d2SKip Macy 1268698b76cSKip Macy // In order to quiesce the device during kernel dumps, outstanding requests to 1278698b76cSKip Macy // DOM0 for disk reads/writes need to be accounted for. 1288698b76cSKip Macy static int xb_dump(void *, void *, vm_offset_t, off_t, size_t); 12989e0f4d2SKip Macy 13089e0f4d2SKip Macy /* XXX move to xb_vbd.c when VBD update support is added */ 13189e0f4d2SKip Macy #define MAX_VBDS 64 13289e0f4d2SKip Macy 13389e0f4d2SKip Macy #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */ 13489e0f4d2SKip Macy #define XBD_SECTOR_SHFT 9 13589e0f4d2SKip Macy 13623dc5621SKip Macy /* 13723dc5621SKip Macy * Translate Linux major/minor to an appropriate name and unit 13823dc5621SKip Macy * number. For HVM guests, this allows us to use the same drive names 13923dc5621SKip Macy * with blkfront as the emulated drives, easing transition slightly. 14023dc5621SKip Macy */ 14123dc5621SKip Macy static void 14223dc5621SKip Macy blkfront_vdevice_to_unit(int vdevice, int *unit, const char **name) 14323dc5621SKip Macy { 14423dc5621SKip Macy static struct vdev_info { 14523dc5621SKip Macy int major; 14623dc5621SKip Macy int shift; 14723dc5621SKip Macy int base; 14823dc5621SKip Macy const char *name; 14923dc5621SKip Macy } info[] = { 15023dc5621SKip Macy {3, 6, 0, "ad"}, /* ide0 */ 15123dc5621SKip Macy {22, 6, 2, "ad"}, /* ide1 */ 15223dc5621SKip Macy {33, 6, 4, "ad"}, /* ide2 */ 15323dc5621SKip Macy {34, 6, 6, "ad"}, /* ide3 */ 15423dc5621SKip Macy {56, 6, 8, "ad"}, /* ide4 */ 15523dc5621SKip Macy {57, 6, 10, "ad"}, /* ide5 */ 15623dc5621SKip Macy {88, 6, 12, "ad"}, /* ide6 */ 15723dc5621SKip Macy {89, 6, 14, "ad"}, /* ide7 */ 15823dc5621SKip Macy {90, 6, 16, "ad"}, /* ide8 */ 15923dc5621SKip Macy {91, 6, 18, "ad"}, /* ide9 */ 16023dc5621SKip Macy 16123dc5621SKip Macy {8, 4, 0, "da"}, /* scsi disk0 */ 16223dc5621SKip Macy {65, 4, 16, "da"}, /* scsi disk1 */ 16323dc5621SKip Macy {66, 4, 32, "da"}, /* scsi disk2 */ 16423dc5621SKip Macy {67, 4, 48, "da"}, /* scsi disk3 */ 16523dc5621SKip Macy {68, 4, 64, "da"}, /* scsi disk4 */ 16623dc5621SKip Macy {69, 4, 80, "da"}, /* scsi disk5 */ 16723dc5621SKip Macy {70, 4, 96, "da"}, /* scsi disk6 */ 16823dc5621SKip Macy {71, 4, 112, "da"}, /* scsi disk7 */ 16923dc5621SKip Macy {128, 4, 128, "da"}, /* scsi disk8 */ 17023dc5621SKip Macy {129, 4, 144, "da"}, /* scsi disk9 */ 17123dc5621SKip Macy {130, 4, 160, "da"}, /* scsi disk10 */ 17223dc5621SKip Macy {131, 4, 176, "da"}, /* scsi disk11 */ 17323dc5621SKip Macy {132, 4, 192, "da"}, /* scsi disk12 */ 17423dc5621SKip Macy {133, 4, 208, "da"}, /* scsi disk13 */ 17523dc5621SKip Macy {134, 4, 224, "da"}, /* scsi disk14 */ 17623dc5621SKip Macy {135, 4, 240, "da"}, /* scsi disk15 */ 17723dc5621SKip Macy 17823dc5621SKip Macy {202, 4, 0, "xbd"}, /* xbd */ 17923dc5621SKip Macy 18023dc5621SKip Macy {0, 0, 0, NULL}, 18123dc5621SKip Macy }; 18223dc5621SKip Macy int major = vdevice >> 8; 18323dc5621SKip Macy int minor = vdevice & 0xff; 18423dc5621SKip Macy int i; 18523dc5621SKip Macy 18623dc5621SKip Macy if (vdevice & (1 << 28)) { 18723dc5621SKip Macy *unit = (vdevice & ((1 << 28) - 1)) >> 8; 18823dc5621SKip Macy *name = "xbd"; 18923dc5621SKip Macy } 19023dc5621SKip Macy 19123dc5621SKip Macy for (i = 0; info[i].major; i++) { 19223dc5621SKip Macy if (info[i].major == major) { 19323dc5621SKip Macy *unit = info[i].base + (minor >> info[i].shift); 19423dc5621SKip Macy *name = info[i].name; 19523dc5621SKip Macy return; 19623dc5621SKip Macy } 19723dc5621SKip Macy } 19823dc5621SKip Macy 19923dc5621SKip Macy *unit = minor >> 4; 20023dc5621SKip Macy *name = "xbd"; 20123dc5621SKip Macy } 20223dc5621SKip Macy 20389e0f4d2SKip Macy int 204ff662b5cSJustin T. Gibbs xlvbd_add(struct xb_softc *sc, blkif_sector_t sectors, 205ff662b5cSJustin T. Gibbs int vdevice, uint16_t vdisk_info, unsigned long sector_size) 20689e0f4d2SKip Macy { 20723dc5621SKip Macy int unit, error = 0; 20823dc5621SKip Macy const char *name; 20923dc5621SKip Macy 21023dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 21189e0f4d2SKip Macy 21223dc5621SKip Macy sc->xb_unit = unit; 21389e0f4d2SKip Macy 21423dc5621SKip Macy if (strcmp(name, "xbd")) 215e4808c4bSKip Macy device_printf(sc->xb_dev, "attaching as %s%d\n", name, unit); 21623dc5621SKip Macy 21789e0f4d2SKip Macy sc->xb_disk = disk_alloc(); 2183a6d1fcfSKip Macy sc->xb_disk->d_unit = sc->xb_unit; 21989e0f4d2SKip Macy sc->xb_disk->d_open = blkif_open; 22089e0f4d2SKip Macy sc->xb_disk->d_close = blkif_close; 22189e0f4d2SKip Macy sc->xb_disk->d_ioctl = blkif_ioctl; 22289e0f4d2SKip Macy sc->xb_disk->d_strategy = xb_strategy; 2238698b76cSKip Macy sc->xb_disk->d_dump = xb_dump; 2243a6d1fcfSKip Macy sc->xb_disk->d_name = name; 22589e0f4d2SKip Macy sc->xb_disk->d_drv1 = sc; 22689e0f4d2SKip Macy sc->xb_disk->d_sectorsize = sector_size; 22789e0f4d2SKip Macy 228ff662b5cSJustin T. Gibbs sc->xb_disk->d_mediasize = sectors * sector_size; 229ff662b5cSJustin T. Gibbs sc->xb_disk->d_maxsize = sc->max_request_size; 23089e0f4d2SKip Macy sc->xb_disk->d_flags = 0; 23189e0f4d2SKip Macy disk_create(sc->xb_disk, DISK_VERSION_00); 23289e0f4d2SKip Macy 23389e0f4d2SKip Macy return error; 23489e0f4d2SKip Macy } 23589e0f4d2SKip Macy 23689e0f4d2SKip Macy /************************ end VBD support *****************/ 23789e0f4d2SKip Macy 23889e0f4d2SKip Macy /* 23989e0f4d2SKip Macy * Read/write routine for a buffer. Finds the proper unit, place it on 24089e0f4d2SKip Macy * the sortq and kick the controller. 24189e0f4d2SKip Macy */ 24289e0f4d2SKip Macy static void 24389e0f4d2SKip Macy xb_strategy(struct bio *bp) 24489e0f4d2SKip Macy { 24589e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1; 24689e0f4d2SKip Macy 24789e0f4d2SKip Macy /* bogus disk? */ 24889e0f4d2SKip Macy if (sc == NULL) { 24989e0f4d2SKip Macy bp->bio_error = EINVAL; 25089e0f4d2SKip Macy bp->bio_flags |= BIO_ERROR; 25189e0f4d2SKip Macy bp->bio_resid = bp->bio_bcount; 25289e0f4d2SKip Macy biodone(bp); 25389e0f4d2SKip Macy return; 25489e0f4d2SKip Macy } 25589e0f4d2SKip Macy 256e4808c4bSKip Macy /* 257e4808c4bSKip Macy * Place it in the queue of disk activities for this disk 258e4808c4bSKip Macy */ 259e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 260e4808c4bSKip Macy 261e4808c4bSKip Macy xb_enqueue_bio(sc, bp); 262e4808c4bSKip Macy xb_startio(sc); 263e4808c4bSKip Macy 264e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 265e4808c4bSKip Macy return; 266e4808c4bSKip Macy } 267e4808c4bSKip Macy 268e4808c4bSKip Macy static void 269e4808c4bSKip Macy xb_bio_complete(struct xb_softc *sc, struct xb_command *cm) 270e4808c4bSKip Macy { 271e4808c4bSKip Macy struct bio *bp; 272e4808c4bSKip Macy 273e4808c4bSKip Macy bp = cm->bp; 274e4808c4bSKip Macy 275e4808c4bSKip Macy if ( unlikely(cm->status != BLKIF_RSP_OKAY) ) { 276e4808c4bSKip Macy disk_err(bp, "disk error" , -1, 0); 277e4808c4bSKip Macy printf(" status: %x\n", cm->status); 278e4808c4bSKip Macy bp->bio_flags |= BIO_ERROR; 279e4808c4bSKip Macy } 280e4808c4bSKip Macy 281e4808c4bSKip Macy if (bp->bio_flags & BIO_ERROR) 282e4808c4bSKip Macy bp->bio_error = EIO; 283e4808c4bSKip Macy else 284e4808c4bSKip Macy bp->bio_resid = 0; 285e4808c4bSKip Macy 286e4808c4bSKip Macy xb_free_command(cm); 287e4808c4bSKip Macy biodone(bp); 288e4808c4bSKip Macy } 289e4808c4bSKip Macy 2908698b76cSKip Macy // Quiesce the disk writes for a dump file before allowing the next buffer. 2918698b76cSKip Macy static void 292e4808c4bSKip Macy xb_quiesce(struct xb_softc *sc) 2938698b76cSKip Macy { 2948698b76cSKip Macy int mtd; 2958698b76cSKip Macy 2968698b76cSKip Macy // While there are outstanding requests 297e4808c4bSKip Macy while (!TAILQ_EMPTY(&sc->cm_busy)) { 298e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, mtd); 2998698b76cSKip Macy if (mtd) { 300e4808c4bSKip Macy /* Recieved request completions, update queue. */ 301e4808c4bSKip Macy blkif_int(sc); 3028698b76cSKip Macy } 303e4808c4bSKip Macy if (!TAILQ_EMPTY(&sc->cm_busy)) { 304e4808c4bSKip Macy /* 305e4808c4bSKip Macy * Still pending requests, wait for the disk i/o 306e4808c4bSKip Macy * to complete. 307e4808c4bSKip Macy */ 308be7747b4SKip Macy HYPERVISOR_yield(); 3098698b76cSKip Macy } 3108698b76cSKip Macy } 3118698b76cSKip Macy } 3128698b76cSKip Macy 313e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */ 314e4808c4bSKip Macy static void 315e4808c4bSKip Macy xb_dump_complete(struct xb_command *cm) 316e4808c4bSKip Macy { 3178698b76cSKip Macy 318e4808c4bSKip Macy xb_enqueue_complete(cm); 319e4808c4bSKip Macy } 320e4808c4bSKip Macy 3218698b76cSKip Macy static int 3228698b76cSKip Macy xb_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 3238698b76cSKip Macy size_t length) 3248698b76cSKip Macy { 3258698b76cSKip Macy struct disk *dp = arg; 3268698b76cSKip Macy struct xb_softc *sc = (struct xb_softc *) dp->d_drv1; 327e4808c4bSKip Macy struct xb_command *cm; 328e4808c4bSKip Macy size_t chunk; 329e4808c4bSKip Macy int sbp; 3308698b76cSKip Macy int rc = 0; 3318698b76cSKip Macy 332e4808c4bSKip Macy if (length <= 0) 333e4808c4bSKip Macy return (rc); 3348698b76cSKip Macy 335e4808c4bSKip Macy xb_quiesce(sc); /* All quiet on the western front. */ 336e4808c4bSKip Macy 337e4808c4bSKip Macy /* 338e4808c4bSKip Macy * If this lock is held, then this module is failing, and a 339e4808c4bSKip Macy * successful kernel dump is highly unlikely anyway. 340e4808c4bSKip Macy */ 341e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 342e4808c4bSKip Macy 343e4808c4bSKip Macy /* Split the 64KB block as needed */ 344e4808c4bSKip Macy for (sbp=0; length > 0; sbp++) { 345e4808c4bSKip Macy cm = xb_dequeue_free(sc); 346e4808c4bSKip Macy if (cm == NULL) { 347e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 348e4808c4bSKip Macy device_printf(sc->xb_dev, "dump: no more commands?\n"); 349e4808c4bSKip Macy return (EBUSY); 350e4808c4bSKip Macy } 351e4808c4bSKip Macy 352ff662b5cSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->max_request_segments, 353ff662b5cSJustin T. Gibbs &cm->gref_head) != 0) { 354e4808c4bSKip Macy xb_free_command(cm); 355e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 356e4808c4bSKip Macy device_printf(sc->xb_dev, "no more grant allocs?\n"); 357e4808c4bSKip Macy return (EBUSY); 358e4808c4bSKip Macy } 359e4808c4bSKip Macy 360ff662b5cSJustin T. Gibbs chunk = length > sc->max_request_size 361ff662b5cSJustin T. Gibbs ? sc->max_request_size : length; 362e4808c4bSKip Macy cm->data = virtual; 363e4808c4bSKip Macy cm->datalen = chunk; 364e4808c4bSKip Macy cm->operation = BLKIF_OP_WRITE; 365e4808c4bSKip Macy cm->sector_number = offset / dp->d_sectorsize; 366e4808c4bSKip Macy cm->cm_complete = xb_dump_complete; 367e4808c4bSKip Macy 368e4808c4bSKip Macy xb_enqueue_ready(cm); 3698698b76cSKip Macy 3708698b76cSKip Macy length -= chunk; 3718698b76cSKip Macy offset += chunk; 3728698b76cSKip Macy virtual = (char *) virtual + chunk; 3738698b76cSKip Macy } 3748698b76cSKip Macy 375e4808c4bSKip Macy /* Tell DOM0 to do the I/O */ 376e4808c4bSKip Macy xb_startio(sc); 377e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 378e4808c4bSKip Macy 379e4808c4bSKip Macy /* Poll for the completion. */ 380e4808c4bSKip Macy xb_quiesce(sc); /* All quite on the eastern front */ 381e4808c4bSKip Macy 382e4808c4bSKip Macy /* If there were any errors, bail out... */ 383e4808c4bSKip Macy while ((cm = xb_dequeue_complete(sc)) != NULL) { 384e4808c4bSKip Macy if (cm->status != BLKIF_RSP_OKAY) { 385e4808c4bSKip Macy device_printf(sc->xb_dev, 386e4808c4bSKip Macy "Dump I/O failed at sector %jd\n", 387e4808c4bSKip Macy cm->sector_number); 388e4808c4bSKip Macy rc = EIO; 3898698b76cSKip Macy } 390e4808c4bSKip Macy xb_free_command(cm); 3918698b76cSKip Macy } 392e4808c4bSKip Macy 3938698b76cSKip Macy return (rc); 3948698b76cSKip Macy } 3958698b76cSKip Macy 3968698b76cSKip Macy 39723dc5621SKip Macy static int 39823dc5621SKip Macy blkfront_probe(device_t dev) 39989e0f4d2SKip Macy { 40023dc5621SKip Macy 40123dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vbd")) { 40223dc5621SKip Macy device_set_desc(dev, "Virtual Block Device"); 40323dc5621SKip Macy device_quiet(dev); 40423dc5621SKip Macy return (0); 40523dc5621SKip Macy } 40623dc5621SKip Macy 40723dc5621SKip Macy return (ENXIO); 40823dc5621SKip Macy } 40923dc5621SKip Macy 41023dc5621SKip Macy /* 41123dc5621SKip Macy * Setup supplies the backend dir, virtual device. We place an event 41223dc5621SKip Macy * channel and shared frame entries. We watch backend to wait if it's 41323dc5621SKip Macy * ok. 41423dc5621SKip Macy */ 41523dc5621SKip Macy static int 41623dc5621SKip Macy blkfront_attach(device_t dev) 41723dc5621SKip Macy { 418e4808c4bSKip Macy struct xb_softc *sc; 41923dc5621SKip Macy const char *name; 420ff662b5cSJustin T. Gibbs int error; 421ff662b5cSJustin T. Gibbs int vdevice; 422ff662b5cSJustin T. Gibbs int i; 423ff662b5cSJustin T. Gibbs int unit; 42489e0f4d2SKip Macy 42589e0f4d2SKip Macy /* FIXME: Use dynamic device id if this is not set. */ 426ff662b5cSJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_node(dev), 4273a6d1fcfSKip Macy "virtual-device", NULL, "%i", &vdevice); 42812678024SDoug Rabson if (error) { 42912678024SDoug Rabson xenbus_dev_fatal(dev, error, "reading virtual-device"); 430ff662b5cSJustin T. Gibbs device_printf(dev, "Couldn't determine virtual device.\n"); 43112678024SDoug Rabson return (error); 43289e0f4d2SKip Macy } 43389e0f4d2SKip Macy 43423dc5621SKip Macy blkfront_vdevice_to_unit(vdevice, &unit, &name); 43523dc5621SKip Macy if (!strcmp(name, "xbd")) 43623dc5621SKip Macy device_set_unit(dev, unit); 43723dc5621SKip Macy 438e4808c4bSKip Macy sc = device_get_softc(dev); 439e4808c4bSKip Macy mtx_init(&sc->xb_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 440e4808c4bSKip Macy xb_initq_free(sc); 441e4808c4bSKip Macy xb_initq_busy(sc); 442e4808c4bSKip Macy xb_initq_ready(sc); 443e4808c4bSKip Macy xb_initq_complete(sc); 444e4808c4bSKip Macy xb_initq_bio(sc); 445ff662b5cSJustin T. Gibbs for (i = 0; i < XBF_MAX_RING_PAGES; i++) 446ff662b5cSJustin T. Gibbs sc->ring_ref[i] = GRANT_INVALID_REF; 44789e0f4d2SKip Macy 448e4808c4bSKip Macy sc->xb_dev = dev; 449e4808c4bSKip Macy sc->vdevice = vdevice; 450e4808c4bSKip Macy sc->connected = BLKIF_STATE_DISCONNECTED; 45189e0f4d2SKip Macy 452ff662b5cSJustin T. Gibbs /* Wait for backend device to publish its protocol capabilities. */ 453ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateInitialising); 45412678024SDoug Rabson 45512678024SDoug Rabson return (0); 45612678024SDoug Rabson } 45712678024SDoug Rabson 45812678024SDoug Rabson static int 45912678024SDoug Rabson blkfront_suspend(device_t dev) 46012678024SDoug Rabson { 461e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 462*06a630f6SJustin T. Gibbs int retval; 463*06a630f6SJustin T. Gibbs int saved_state; 46412678024SDoug Rabson 46512678024SDoug Rabson /* Prevent new requests being issued until we fix things up. */ 466e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 467*06a630f6SJustin T. Gibbs saved_state = sc->connected; 468e4808c4bSKip Macy sc->connected = BLKIF_STATE_SUSPENDED; 469*06a630f6SJustin T. Gibbs 470*06a630f6SJustin T. Gibbs /* Wait for outstanding I/O to drain. */ 471*06a630f6SJustin T. Gibbs retval = 0; 472*06a630f6SJustin T. Gibbs while (TAILQ_EMPTY(&sc->cm_busy) == 0) { 473*06a630f6SJustin T. Gibbs if (msleep(&sc->cm_busy, &sc->xb_io_lock, 474*06a630f6SJustin T. Gibbs PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) { 475*06a630f6SJustin T. Gibbs retval = EBUSY; 476*06a630f6SJustin T. Gibbs break; 477*06a630f6SJustin T. Gibbs } 478*06a630f6SJustin T. Gibbs } 479e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 48089e0f4d2SKip Macy 481*06a630f6SJustin T. Gibbs if (retval != 0) 482*06a630f6SJustin T. Gibbs sc->connected = saved_state; 483*06a630f6SJustin T. Gibbs 484*06a630f6SJustin T. Gibbs return (retval); 48589e0f4d2SKip Macy } 48689e0f4d2SKip Macy 48723dc5621SKip Macy static int 48823dc5621SKip Macy blkfront_resume(device_t dev) 48989e0f4d2SKip Macy { 490e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 49189e0f4d2SKip Macy 49212678024SDoug Rabson DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev)); 49389e0f4d2SKip Macy 494*06a630f6SJustin T. Gibbs blkif_free(sc); 495ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 496ff662b5cSJustin T. Gibbs return (0); 49789e0f4d2SKip Macy } 49889e0f4d2SKip Macy 499ff662b5cSJustin T. Gibbs static void 500ff662b5cSJustin T. Gibbs blkfront_initialize(struct xb_softc *sc) 50189e0f4d2SKip Macy { 502ff662b5cSJustin T. Gibbs const char *otherend_path; 503ff662b5cSJustin T. Gibbs const char *node_path; 504ff662b5cSJustin T. Gibbs int error; 505ff662b5cSJustin T. Gibbs int i; 50689e0f4d2SKip Macy 507*06a630f6SJustin T. Gibbs if (xenbus_get_state(sc->xb_dev) != XenbusStateInitialising) { 508*06a630f6SJustin T. Gibbs /* Initialization has already been performed. */ 509ff662b5cSJustin T. Gibbs return; 510*06a630f6SJustin T. Gibbs } 51189e0f4d2SKip Macy 512ff662b5cSJustin T. Gibbs /* 513ff662b5cSJustin T. Gibbs * Protocol defaults valid even if negotiation for a 514ff662b5cSJustin T. Gibbs * setting fails. 515ff662b5cSJustin T. Gibbs */ 516ff662b5cSJustin T. Gibbs sc->ring_pages = 1; 517ff662b5cSJustin T. Gibbs sc->max_requests = BLKIF_MAX_RING_REQUESTS(PAGE_SIZE); 518ff662b5cSJustin T. Gibbs sc->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK; 519518c8243SColin Percival sc->max_request_size = (sc->max_request_segments - 1) * PAGE_SIZE; 520ff662b5cSJustin T. Gibbs sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments); 521ff662b5cSJustin T. Gibbs 522ff662b5cSJustin T. Gibbs /* 523ff662b5cSJustin T. Gibbs * Protocol negotiation. 524ff662b5cSJustin T. Gibbs * 525ff662b5cSJustin T. Gibbs * \note xs_gather() returns on the first encountered error, so 526ff662b5cSJustin T. Gibbs * we must use independant calls in order to guarantee 527ff662b5cSJustin T. Gibbs * we don't miss information in a sparsly populated back-end 528ff662b5cSJustin T. Gibbs * tree. 529ff662b5cSJustin T. Gibbs */ 530ff662b5cSJustin T. Gibbs otherend_path = xenbus_get_otherend_path(sc->xb_dev); 531ff662b5cSJustin T. Gibbs node_path = xenbus_get_node(sc->xb_dev); 532ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 533ff662b5cSJustin T. Gibbs "max-ring-pages", NULL, "%" PRIu32, 534ff662b5cSJustin T. Gibbs &sc->ring_pages); 535ff662b5cSJustin T. Gibbs 536ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 537ff662b5cSJustin T. Gibbs "max-requests", NULL, "%" PRIu32, 538ff662b5cSJustin T. Gibbs &sc->max_requests); 539ff662b5cSJustin T. Gibbs 540ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 541ff662b5cSJustin T. Gibbs "max-request-segments", NULL, "%" PRIu32, 542ff662b5cSJustin T. Gibbs &sc->max_request_segments); 543ff662b5cSJustin T. Gibbs 544ff662b5cSJustin T. Gibbs (void)xs_scanf(XST_NIL, otherend_path, 545ff662b5cSJustin T. Gibbs "max-request-size", NULL, "%" PRIu32, 546ff662b5cSJustin T. Gibbs &sc->max_request_size); 547ff662b5cSJustin T. Gibbs 548ff662b5cSJustin T. Gibbs if (sc->ring_pages > XBF_MAX_RING_PAGES) { 549ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specified ring-pages of " 550ff662b5cSJustin T. Gibbs "%u limited to front-end limit of %zu.\n", 551ff662b5cSJustin T. Gibbs sc->ring_pages, XBF_MAX_RING_PAGES); 552ff662b5cSJustin T. Gibbs sc->ring_pages = XBF_MAX_RING_PAGES; 55389e0f4d2SKip Macy } 55489e0f4d2SKip Macy 555ff662b5cSJustin T. Gibbs if (sc->max_requests > XBF_MAX_REQUESTS) { 556ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specified max_requests of " 557ff662b5cSJustin T. Gibbs "%u limited to front-end limit of %u.\n", 558ff662b5cSJustin T. Gibbs sc->max_requests, XBF_MAX_REQUESTS); 559ff662b5cSJustin T. Gibbs sc->max_requests = XBF_MAX_REQUESTS; 56089e0f4d2SKip Macy } 561ff662b5cSJustin T. Gibbs 562ff662b5cSJustin T. Gibbs if (sc->max_request_segments > XBF_MAX_SEGMENTS_PER_REQUEST) { 563ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specificed " 564ff662b5cSJustin T. Gibbs "max_requests_segments of %u limited to " 565ff662b5cSJustin T. Gibbs "front-end limit of %u.\n", 566ff662b5cSJustin T. Gibbs sc->max_request_segments, 567ff662b5cSJustin T. Gibbs XBF_MAX_SEGMENTS_PER_REQUEST); 568ff662b5cSJustin T. Gibbs sc->max_request_segments = XBF_MAX_SEGMENTS_PER_REQUEST; 56989e0f4d2SKip Macy } 570ff662b5cSJustin T. Gibbs 571ff662b5cSJustin T. Gibbs if (sc->max_request_size > XBF_MAX_REQUEST_SIZE) { 572ff662b5cSJustin T. Gibbs device_printf(sc->xb_dev, "Back-end specificed " 573ff662b5cSJustin T. Gibbs "max_request_size of %u limited to front-end " 574ff662b5cSJustin T. Gibbs "limit of %u.\n", sc->max_request_size, 575ff662b5cSJustin T. Gibbs XBF_MAX_REQUEST_SIZE); 576ff662b5cSJustin T. Gibbs sc->max_request_size = XBF_MAX_REQUEST_SIZE; 577ff662b5cSJustin T. Gibbs } 578ff662b5cSJustin T. Gibbs sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments); 579ff662b5cSJustin T. Gibbs 580ff662b5cSJustin T. Gibbs /* Allocate datastructures based on negotiated values. */ 581ff662b5cSJustin T. Gibbs error = bus_dma_tag_create(NULL, /* parent */ 582ff662b5cSJustin T. Gibbs 512, PAGE_SIZE, /* algnmnt, boundary */ 583ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* lowaddr */ 584ff662b5cSJustin T. Gibbs BUS_SPACE_MAXADDR, /* highaddr */ 585ff662b5cSJustin T. Gibbs NULL, NULL, /* filter, filterarg */ 586ff662b5cSJustin T. Gibbs sc->max_request_size, 587ff662b5cSJustin T. Gibbs sc->max_request_segments, 588ff662b5cSJustin T. Gibbs PAGE_SIZE, /* maxsegsize */ 589ff662b5cSJustin T. Gibbs BUS_DMA_ALLOCNOW, /* flags */ 590ff662b5cSJustin T. Gibbs busdma_lock_mutex, /* lockfunc */ 591ff662b5cSJustin T. Gibbs &sc->xb_io_lock, /* lockarg */ 592ff662b5cSJustin T. Gibbs &sc->xb_io_dmat); 593ff662b5cSJustin T. Gibbs if (error != 0) { 594ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 595ff662b5cSJustin T. Gibbs "Cannot allocate parent DMA tag\n"); 596ff662b5cSJustin T. Gibbs return; 597ff662b5cSJustin T. Gibbs } 598ff662b5cSJustin T. Gibbs 599ff662b5cSJustin T. Gibbs /* Per-transaction data allocation. */ 600ff662b5cSJustin T. Gibbs sc->shadow = malloc(sizeof(*sc->shadow) * sc->max_requests, 601ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 602ff662b5cSJustin T. Gibbs if (sc->shadow == NULL) { 603*06a630f6SJustin T. Gibbs bus_dma_tag_destroy(sc->xb_io_dmat); 604ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 605ff662b5cSJustin T. Gibbs "Cannot allocate request structures\n"); 606*06a630f6SJustin T. Gibbs return; 607ff662b5cSJustin T. Gibbs } 608ff662b5cSJustin T. Gibbs 609ff662b5cSJustin T. Gibbs for (i = 0; i < sc->max_requests; i++) { 610ff662b5cSJustin T. Gibbs struct xb_command *cm; 611ff662b5cSJustin T. Gibbs 612ff662b5cSJustin T. Gibbs cm = &sc->shadow[i]; 613ff662b5cSJustin T. Gibbs cm->sg_refs = malloc(sizeof(grant_ref_t) 614ff662b5cSJustin T. Gibbs * sc->max_request_segments, 615ff662b5cSJustin T. Gibbs M_XENBLOCKFRONT, M_NOWAIT); 616ff662b5cSJustin T. Gibbs if (cm->sg_refs == NULL) 617ff662b5cSJustin T. Gibbs break; 618ff662b5cSJustin T. Gibbs cm->id = i; 619ff662b5cSJustin T. Gibbs cm->cm_sc = sc; 620ff662b5cSJustin T. Gibbs if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0) 621ff662b5cSJustin T. Gibbs break; 622ff662b5cSJustin T. Gibbs xb_free_command(cm); 623ff662b5cSJustin T. Gibbs } 624ff662b5cSJustin T. Gibbs 625ff662b5cSJustin T. Gibbs if (setup_blkring(sc) != 0) 626ff662b5cSJustin T. Gibbs return; 627ff662b5cSJustin T. Gibbs 628ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 629ff662b5cSJustin T. Gibbs "ring-pages","%u", sc->ring_pages); 630ff662b5cSJustin T. Gibbs if (error) { 631ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 632ff662b5cSJustin T. Gibbs "writing %s/ring-pages", 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-requests","%u", sc->max_requests); 639ff662b5cSJustin T. Gibbs if (error) { 640ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 641ff662b5cSJustin T. Gibbs "writing %s/max-requests", 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-segments","%u", sc->max_request_segments); 648ff662b5cSJustin T. Gibbs if (error) { 649ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 650ff662b5cSJustin T. Gibbs "writing %s/max-request-segments", 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, 656ff662b5cSJustin T. Gibbs "max-request-size","%u", sc->max_request_size); 657ff662b5cSJustin T. Gibbs if (error) { 658ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 659ff662b5cSJustin T. Gibbs "writing %s/max-request-size", 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, "event-channel", 665ff662b5cSJustin T. Gibbs "%u", irq_to_evtchn_port(sc->irq)); 666ff662b5cSJustin T. Gibbs if (error) { 667ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 668ff662b5cSJustin T. Gibbs "writing %s/event-channel", 669ff662b5cSJustin T. Gibbs node_path); 670ff662b5cSJustin T. Gibbs return; 671ff662b5cSJustin T. Gibbs } 672ff662b5cSJustin T. Gibbs 673ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, node_path, 67423dc5621SKip Macy "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE); 675ff662b5cSJustin T. Gibbs if (error) { 676ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 677ff662b5cSJustin T. Gibbs "writing %s/protocol", 678ff662b5cSJustin T. Gibbs node_path); 679ff662b5cSJustin T. Gibbs return; 68023dc5621SKip Macy } 68112678024SDoug Rabson 682ff662b5cSJustin T. Gibbs xenbus_set_state(sc->xb_dev, XenbusStateInitialised); 68389e0f4d2SKip Macy } 68489e0f4d2SKip Macy 68589e0f4d2SKip Macy static int 686e4808c4bSKip Macy setup_blkring(struct xb_softc *sc) 68789e0f4d2SKip Macy { 68889e0f4d2SKip Macy blkif_sring_t *sring; 689ff662b5cSJustin T. Gibbs uintptr_t sring_page_addr; 6903a6d1fcfSKip Macy int error; 691ff662b5cSJustin T. Gibbs int i; 69289e0f4d2SKip Macy 693ff662b5cSJustin T. Gibbs sring = malloc(sc->ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 694ff662b5cSJustin T. Gibbs M_NOWAIT|M_ZERO); 69589e0f4d2SKip Macy if (sring == NULL) { 696e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, ENOMEM, "allocating shared ring"); 697ff662b5cSJustin T. Gibbs return (ENOMEM); 69889e0f4d2SKip Macy } 69989e0f4d2SKip Macy SHARED_RING_INIT(sring); 700ff662b5cSJustin T. Gibbs FRONT_RING_INIT(&sc->ring, sring, sc->ring_pages * PAGE_SIZE); 701ff662b5cSJustin T. Gibbs 702ff662b5cSJustin T. Gibbs for (i = 0, sring_page_addr = (uintptr_t)sring; 703ff662b5cSJustin T. Gibbs i < sc->ring_pages; 704ff662b5cSJustin T. Gibbs i++, sring_page_addr += PAGE_SIZE) { 70589e0f4d2SKip Macy 706e4808c4bSKip Macy error = xenbus_grant_ring(sc->xb_dev, 707ff662b5cSJustin T. Gibbs (vtomach(sring_page_addr) >> PAGE_SHIFT), &sc->ring_ref[i]); 7083a6d1fcfSKip Macy if (error) { 709ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, 710ff662b5cSJustin T. Gibbs "granting ring_ref(%d)", i); 711ff662b5cSJustin T. Gibbs return (error); 712ff662b5cSJustin T. Gibbs } 713ff662b5cSJustin T. Gibbs } 714ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev), 715ff662b5cSJustin T. Gibbs "ring-ref","%u", sc->ring_ref[0]); 716ff662b5cSJustin T. Gibbs if (error) { 717ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, "writing %s/ring-ref", 718ff662b5cSJustin T. Gibbs xenbus_get_node(sc->xb_dev)); 719ff662b5cSJustin T. Gibbs return (error); 720ff662b5cSJustin T. Gibbs } 721ff662b5cSJustin T. Gibbs for (i = 1; i < sc->ring_pages; i++) { 722ff662b5cSJustin T. Gibbs char ring_ref_name[]= "ring_refXX"; 723ff662b5cSJustin T. Gibbs 724ff662b5cSJustin T. Gibbs snprintf(ring_ref_name, sizeof(ring_ref_name), "ring-ref%u", i); 725ff662b5cSJustin T. Gibbs error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev), 726ff662b5cSJustin T. Gibbs ring_ref_name, "%u", sc->ring_ref[i]); 727ff662b5cSJustin T. Gibbs if (error) { 728ff662b5cSJustin T. Gibbs xenbus_dev_fatal(sc->xb_dev, error, "writing %s/%s", 729ff662b5cSJustin T. Gibbs xenbus_get_node(sc->xb_dev), 730ff662b5cSJustin T. Gibbs ring_ref_name); 731ff662b5cSJustin T. Gibbs return (error); 732ff662b5cSJustin T. Gibbs } 73389e0f4d2SKip Macy } 73489e0f4d2SKip Macy 735ff662b5cSJustin T. Gibbs error = bind_listening_port_to_irqhandler( 736ff662b5cSJustin T. Gibbs xenbus_get_otherend_id(sc->xb_dev), 737e4808c4bSKip Macy "xbd", (driver_intr_t *)blkif_int, sc, 738e4808c4bSKip Macy INTR_TYPE_BIO | INTR_MPSAFE, &sc->irq); 7393a6d1fcfSKip Macy if (error) { 740e4808c4bSKip Macy xenbus_dev_fatal(sc->xb_dev, error, 74189e0f4d2SKip Macy "bind_evtchn_to_irqhandler failed"); 7423a6d1fcfSKip Macy return (error); 74389e0f4d2SKip Macy } 74489e0f4d2SKip Macy 745ff662b5cSJustin T. Gibbs return (0); 746ff662b5cSJustin T. Gibbs } 74789e0f4d2SKip Macy 74889e0f4d2SKip Macy /** 74989e0f4d2SKip Macy * Callback received when the backend's state changes. 75089e0f4d2SKip Macy */ 751283d6f72SJustin T. Gibbs static void 75223dc5621SKip Macy blkfront_backend_changed(device_t dev, XenbusState backend_state) 75389e0f4d2SKip Macy { 754e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 75589e0f4d2SKip Macy 75612678024SDoug Rabson DPRINTK("backend_state=%d\n", backend_state); 75789e0f4d2SKip Macy 75889e0f4d2SKip Macy switch (backend_state) { 75989e0f4d2SKip Macy case XenbusStateUnknown: 76089e0f4d2SKip Macy case XenbusStateInitialising: 761920ba15bSKip Macy case XenbusStateReconfigured: 762920ba15bSKip Macy case XenbusStateReconfiguring: 763ff662b5cSJustin T. Gibbs case XenbusStateClosed: 76489e0f4d2SKip Macy break; 76589e0f4d2SKip Macy 766ff662b5cSJustin T. Gibbs case XenbusStateInitWait: 767*06a630f6SJustin T. Gibbs case XenbusStateInitialised: 768ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 769ff662b5cSJustin T. Gibbs break; 770ff662b5cSJustin T. Gibbs 77189e0f4d2SKip Macy case XenbusStateConnected: 772ff662b5cSJustin T. Gibbs blkfront_initialize(sc); 773ff662b5cSJustin T. Gibbs blkfront_connect(sc); 77489e0f4d2SKip Macy break; 77589e0f4d2SKip Macy 77689e0f4d2SKip Macy case XenbusStateClosing: 777e4808c4bSKip Macy if (sc->users > 0) 77889e0f4d2SKip Macy xenbus_dev_error(dev, -EBUSY, 77989e0f4d2SKip Macy "Device in use; refusing to close"); 78089e0f4d2SKip Macy else 78189e0f4d2SKip Macy blkfront_closing(dev); 782ff662b5cSJustin T. Gibbs break; 78389e0f4d2SKip Macy } 78489e0f4d2SKip Macy } 78589e0f4d2SKip Macy 78689e0f4d2SKip Macy /* 787*06a630f6SJustin T. Gibbs ** Invoked when the backend is finally 'ready' (and has published 78889e0f4d2SKip Macy ** the details about the physical device - #sectors, size, etc). 78989e0f4d2SKip Macy */ 79089e0f4d2SKip Macy static void 791ff662b5cSJustin T. Gibbs blkfront_connect(struct xb_softc *sc) 79289e0f4d2SKip Macy { 793e4808c4bSKip Macy device_t dev = sc->xb_dev; 79489e0f4d2SKip Macy unsigned long sectors, sector_size; 79589e0f4d2SKip Macy unsigned int binfo; 796e4808c4bSKip Macy int err, feature_barrier; 79789e0f4d2SKip Macy 798e4808c4bSKip Macy if( (sc->connected == BLKIF_STATE_CONNECTED) || 799e4808c4bSKip Macy (sc->connected == BLKIF_STATE_SUSPENDED) ) 80089e0f4d2SKip Macy return; 80189e0f4d2SKip Macy 80223dc5621SKip Macy DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 80389e0f4d2SKip Macy 804ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 80589e0f4d2SKip Macy "sectors", "%lu", §ors, 80689e0f4d2SKip Macy "info", "%u", &binfo, 80789e0f4d2SKip Macy "sector-size", "%lu", §or_size, 80889e0f4d2SKip Macy NULL); 80989e0f4d2SKip Macy if (err) { 81023dc5621SKip Macy xenbus_dev_fatal(dev, err, 81189e0f4d2SKip Macy "reading backend fields at %s", 81223dc5621SKip Macy xenbus_get_otherend_path(dev)); 81389e0f4d2SKip Macy return; 81489e0f4d2SKip Macy } 815ff662b5cSJustin T. Gibbs err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 816e4808c4bSKip Macy "feature-barrier", "%lu", &feature_barrier, 81789e0f4d2SKip Macy NULL); 818e4808c4bSKip Macy if (!err || feature_barrier) 819e4808c4bSKip Macy sc->xb_flags |= XB_BARRIER; 82089e0f4d2SKip Macy 821*06a630f6SJustin T. Gibbs if (sc->xb_disk == NULL) { 82223dc5621SKip Macy device_printf(dev, "%juMB <%s> at %s", 82323dc5621SKip Macy (uintmax_t) sectors / (1048576 / sector_size), 82423dc5621SKip Macy device_get_desc(dev), 82523dc5621SKip Macy xenbus_get_node(dev)); 82623dc5621SKip Macy bus_print_child_footer(device_get_parent(dev), dev); 82789e0f4d2SKip Macy 828e4808c4bSKip Macy xlvbd_add(sc, sectors, sc->vdevice, binfo, sector_size); 829*06a630f6SJustin T. Gibbs } 83023dc5621SKip Macy 83123dc5621SKip Macy (void)xenbus_set_state(dev, XenbusStateConnected); 83289e0f4d2SKip Macy 83389e0f4d2SKip Macy /* Kick pending requests. */ 834e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 835e4808c4bSKip Macy sc->connected = BLKIF_STATE_CONNECTED; 836e4808c4bSKip Macy xb_startio(sc); 837e4808c4bSKip Macy sc->xb_flags |= XB_READY; 838e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 83989e0f4d2SKip Macy } 84089e0f4d2SKip Macy 84189e0f4d2SKip Macy /** 84289e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 84389e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 844a4660d59SJustin T. Gibbs * the backend. Once this is done, we can switch to Closed in 84589e0f4d2SKip Macy * acknowledgement. 84689e0f4d2SKip Macy */ 84723dc5621SKip Macy static void 84823dc5621SKip Macy blkfront_closing(device_t dev) 84989e0f4d2SKip Macy { 850e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 85189e0f4d2SKip Macy 852ff662b5cSJustin T. Gibbs xenbus_set_state(dev, XenbusStateClosing); 853ff662b5cSJustin T. Gibbs 85423dc5621SKip Macy DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev)); 85589e0f4d2SKip Macy 856ff662b5cSJustin T. Gibbs if (sc->xb_disk != NULL) { 857ff662b5cSJustin T. Gibbs disk_destroy(sc->xb_disk); 858ff662b5cSJustin T. Gibbs sc->xb_disk = NULL; 85989e0f4d2SKip Macy } 86089e0f4d2SKip Macy 86123dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 86289e0f4d2SKip Macy } 86389e0f4d2SKip Macy 86489e0f4d2SKip Macy 86523dc5621SKip Macy static int 86623dc5621SKip Macy blkfront_detach(device_t dev) 86789e0f4d2SKip Macy { 868e4808c4bSKip Macy struct xb_softc *sc = device_get_softc(dev); 86989e0f4d2SKip Macy 87023dc5621SKip Macy DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev)); 87189e0f4d2SKip Macy 872*06a630f6SJustin T. Gibbs blkif_free(sc); 873e4808c4bSKip Macy mtx_destroy(&sc->xb_io_lock); 87489e0f4d2SKip Macy 87589e0f4d2SKip Macy return 0; 87689e0f4d2SKip Macy } 87789e0f4d2SKip Macy 87889e0f4d2SKip Macy 87989e0f4d2SKip Macy static inline void 880e4808c4bSKip Macy flush_requests(struct xb_softc *sc) 88189e0f4d2SKip Macy { 88289e0f4d2SKip Macy int notify; 88389e0f4d2SKip Macy 884e4808c4bSKip Macy RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->ring, notify); 88589e0f4d2SKip Macy 88689e0f4d2SKip Macy if (notify) 887e4808c4bSKip Macy notify_remote_via_irq(sc->irq); 88889e0f4d2SKip Macy } 88989e0f4d2SKip Macy 890ff662b5cSJustin T. Gibbs static void 891ff662b5cSJustin T. Gibbs blkif_restart_queue_callback(void *arg) 89289e0f4d2SKip Macy { 893e4808c4bSKip Macy struct xb_softc *sc = arg; 89489e0f4d2SKip Macy 895ff662b5cSJustin T. Gibbs mtx_lock(&sc->xb_io_lock); 896ff662b5cSJustin T. Gibbs 897e4808c4bSKip Macy xb_startio(sc); 898ff662b5cSJustin T. Gibbs 899ff662b5cSJustin T. Gibbs mtx_unlock(&sc->xb_io_lock); 90089e0f4d2SKip Macy } 90189e0f4d2SKip Macy 90289e0f4d2SKip Macy static int 90389e0f4d2SKip Macy blkif_open(struct disk *dp) 90489e0f4d2SKip Macy { 90589e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 90689e0f4d2SKip Macy 90789e0f4d2SKip Macy if (sc == NULL) { 90812678024SDoug Rabson printf("xb%d: not found", sc->xb_unit); 90989e0f4d2SKip Macy return (ENXIO); 91089e0f4d2SKip Macy } 91189e0f4d2SKip Macy 91289e0f4d2SKip Macy sc->xb_flags |= XB_OPEN; 913e4808c4bSKip Macy sc->users++; 91489e0f4d2SKip Macy return (0); 91589e0f4d2SKip Macy } 91689e0f4d2SKip Macy 91789e0f4d2SKip Macy static int 91889e0f4d2SKip Macy blkif_close(struct disk *dp) 91989e0f4d2SKip Macy { 92089e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 92189e0f4d2SKip Macy 92289e0f4d2SKip Macy if (sc == NULL) 92389e0f4d2SKip Macy return (ENXIO); 92489e0f4d2SKip Macy sc->xb_flags &= ~XB_OPEN; 925e4808c4bSKip Macy if (--(sc->users) == 0) { 92689e0f4d2SKip Macy /* Check whether we have been instructed to close. We will 92789e0f4d2SKip Macy have ignored this request initially, as the device was 92889e0f4d2SKip Macy still mounted. */ 929e4808c4bSKip Macy device_t dev = sc->xb_dev; 93023dc5621SKip Macy XenbusState state = 93123dc5621SKip Macy xenbus_read_driver_state(xenbus_get_otherend_path(dev)); 93289e0f4d2SKip Macy 93389e0f4d2SKip Macy if (state == XenbusStateClosing) 93489e0f4d2SKip Macy blkfront_closing(dev); 93589e0f4d2SKip Macy } 93689e0f4d2SKip Macy return (0); 93789e0f4d2SKip Macy } 93889e0f4d2SKip Macy 93989e0f4d2SKip Macy static int 94089e0f4d2SKip Macy blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 94189e0f4d2SKip Macy { 94289e0f4d2SKip Macy struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 94389e0f4d2SKip Macy 94489e0f4d2SKip Macy if (sc == NULL) 94589e0f4d2SKip Macy return (ENXIO); 94689e0f4d2SKip Macy 94789e0f4d2SKip Macy return (ENOTTY); 94889e0f4d2SKip Macy } 94989e0f4d2SKip Macy 950e4808c4bSKip Macy static void 951e4808c4bSKip Macy xb_free_command(struct xb_command *cm) 952e4808c4bSKip Macy { 953e4808c4bSKip Macy 954e4808c4bSKip Macy KASSERT((cm->cm_flags & XB_ON_XBQ_MASK) == 0, 955e4808c4bSKip Macy ("Freeing command that is still on a queue\n")); 956e4808c4bSKip Macy 957e4808c4bSKip Macy cm->cm_flags = 0; 958e4808c4bSKip Macy cm->bp = NULL; 959e4808c4bSKip Macy cm->cm_complete = NULL; 960e4808c4bSKip Macy xb_enqueue_free(cm); 961e4808c4bSKip Macy } 96289e0f4d2SKip Macy 96389e0f4d2SKip Macy /* 96489e0f4d2SKip Macy * blkif_queue_request 96589e0f4d2SKip Macy * 96689e0f4d2SKip Macy * request block io 96789e0f4d2SKip Macy * 96889e0f4d2SKip Macy * id: for guest use only. 96989e0f4d2SKip Macy * operation: BLKIF_OP_{READ,WRITE,PROBE} 97089e0f4d2SKip Macy * buffer: buffer to read/write into. this should be a 97189e0f4d2SKip Macy * virtual address in the guest os. 97289e0f4d2SKip Macy */ 973e4808c4bSKip Macy static struct xb_command * 974e4808c4bSKip Macy xb_bio_command(struct xb_softc *sc) 97589e0f4d2SKip Macy { 976e4808c4bSKip Macy struct xb_command *cm; 977e4808c4bSKip Macy struct bio *bp; 97889e0f4d2SKip Macy 979e4808c4bSKip Macy if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) 980e4808c4bSKip Macy return (NULL); 98189e0f4d2SKip Macy 982e4808c4bSKip Macy bp = xb_dequeue_bio(sc); 983e4808c4bSKip Macy if (bp == NULL) 984e4808c4bSKip Macy return (NULL); 985e4808c4bSKip Macy 986e4808c4bSKip Macy if ((cm = xb_dequeue_free(sc)) == NULL) { 987e4808c4bSKip Macy xb_requeue_bio(sc, bp); 988e4808c4bSKip Macy return (NULL); 98989e0f4d2SKip Macy } 99089e0f4d2SKip Macy 991ff662b5cSJustin T. Gibbs if (gnttab_alloc_grant_references(sc->max_request_segments, 992ff662b5cSJustin T. Gibbs &cm->gref_head) != 0) { 993e4808c4bSKip Macy gnttab_request_free_callback(&sc->callback, 994e4808c4bSKip Macy blkif_restart_queue_callback, sc, 995ff662b5cSJustin T. Gibbs sc->max_request_segments); 996e4808c4bSKip Macy xb_requeue_bio(sc, bp); 997e4808c4bSKip Macy xb_enqueue_free(cm); 998e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 999e4808c4bSKip Macy return (NULL); 1000e4808c4bSKip Macy } 100189e0f4d2SKip Macy 1002e4808c4bSKip Macy cm->bp = bp; 1003e4808c4bSKip Macy cm->data = bp->bio_data; 1004e4808c4bSKip Macy cm->datalen = bp->bio_bcount; 1005e4808c4bSKip Macy cm->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ : 1006e4808c4bSKip Macy BLKIF_OP_WRITE; 1007e4808c4bSKip Macy cm->sector_number = (blkif_sector_t)bp->bio_pblkno; 100889e0f4d2SKip Macy 1009e4808c4bSKip Macy return (cm); 1010e4808c4bSKip Macy } 101189e0f4d2SKip Macy 1012e4808c4bSKip Macy static int 1013e4808c4bSKip Macy blkif_queue_request(struct xb_softc *sc, struct xb_command *cm) 1014e4808c4bSKip Macy { 1015e4808c4bSKip Macy int error; 1016e4808c4bSKip Macy 1017e4808c4bSKip Macy error = bus_dmamap_load(sc->xb_io_dmat, cm->map, cm->data, cm->datalen, 1018e4808c4bSKip Macy blkif_queue_cb, cm, 0); 1019e4808c4bSKip Macy if (error == EINPROGRESS) { 1020e4808c4bSKip Macy printf("EINPROGRESS\n"); 1021e4808c4bSKip Macy sc->xb_flags |= XB_FROZEN; 1022e4808c4bSKip Macy cm->cm_flags |= XB_CMD_FROZEN; 1023e4808c4bSKip Macy return (0); 1024e4808c4bSKip Macy } 1025e4808c4bSKip Macy 1026e4808c4bSKip Macy return (error); 1027e4808c4bSKip Macy } 1028e4808c4bSKip Macy 1029e4808c4bSKip Macy static void 1030e4808c4bSKip Macy blkif_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1031e4808c4bSKip Macy { 1032e4808c4bSKip Macy struct xb_softc *sc; 1033e4808c4bSKip Macy struct xb_command *cm; 1034e4808c4bSKip Macy blkif_request_t *ring_req; 1035ff662b5cSJustin T. Gibbs struct blkif_request_segment *sg; 1036ff662b5cSJustin T. Gibbs struct blkif_request_segment *last_block_sg; 1037ff662b5cSJustin T. Gibbs grant_ref_t *sg_ref; 1038e4808c4bSKip Macy vm_paddr_t buffer_ma; 1039e4808c4bSKip Macy uint64_t fsect, lsect; 1040ff662b5cSJustin T. Gibbs int ref; 1041ff662b5cSJustin T. Gibbs int op; 1042ff662b5cSJustin T. Gibbs int block_segs; 1043e4808c4bSKip Macy 1044e4808c4bSKip Macy cm = arg; 1045e4808c4bSKip Macy sc = cm->cm_sc; 1046e4808c4bSKip Macy 1047ff662b5cSJustin T. Gibbs //printf("%s: Start\n", __func__); 1048e4808c4bSKip Macy if (error) { 1049e4808c4bSKip Macy printf("error %d in blkif_queue_cb\n", error); 1050e4808c4bSKip Macy cm->bp->bio_error = EIO; 1051e4808c4bSKip Macy biodone(cm->bp); 1052e4808c4bSKip Macy xb_free_command(cm); 1053e4808c4bSKip Macy return; 1054e4808c4bSKip Macy } 105589e0f4d2SKip Macy 105689e0f4d2SKip Macy /* Fill out a communications ring structure. */ 1057e4808c4bSKip Macy ring_req = RING_GET_REQUEST(&sc->ring, sc->ring.req_prod_pvt); 1058ff662b5cSJustin T. Gibbs sc->ring.req_prod_pvt++; 1059ff662b5cSJustin T. Gibbs ring_req->id = cm->id; 1060e4808c4bSKip Macy ring_req->operation = cm->operation; 1061e4808c4bSKip Macy ring_req->sector_number = cm->sector_number; 106289e0f4d2SKip Macy ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk; 1063e4808c4bSKip Macy ring_req->nr_segments = nsegs; 1064ff662b5cSJustin T. Gibbs cm->nseg = nsegs; 106589e0f4d2SKip Macy 1066ff662b5cSJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK); 1067ff662b5cSJustin T. Gibbs sg = ring_req->seg; 1068ff662b5cSJustin T. Gibbs last_block_sg = sg + block_segs; 1069ff662b5cSJustin T. Gibbs sg_ref = cm->sg_refs; 1070ff662b5cSJustin T. Gibbs 1071ff662b5cSJustin T. Gibbs while (1) { 1072ff662b5cSJustin T. Gibbs 1073ff662b5cSJustin T. Gibbs while (sg < last_block_sg) { 1074ff662b5cSJustin T. Gibbs buffer_ma = segs->ds_addr; 107589e0f4d2SKip Macy fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 1076ff662b5cSJustin T. Gibbs lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 1077e4808c4bSKip Macy 1078ff662b5cSJustin T. Gibbs KASSERT(lsect <= 7, ("XEN disk driver data cannot " 1079ff662b5cSJustin T. Gibbs "cross a page boundary")); 1080e4808c4bSKip Macy 108189e0f4d2SKip Macy /* install a grant reference. */ 1082e4808c4bSKip Macy ref = gnttab_claim_grant_reference(&cm->gref_head); 1083ff662b5cSJustin T. Gibbs 1084ff662b5cSJustin T. Gibbs /* 1085ff662b5cSJustin T. Gibbs * GNTTAB_LIST_END == 0xffffffff, but it is private 1086ff662b5cSJustin T. Gibbs * to gnttab.c. 1087ff662b5cSJustin T. Gibbs */ 1088ff662b5cSJustin T. Gibbs KASSERT(ref != ~0, ("grant_reference failed")); 108989e0f4d2SKip Macy 109089e0f4d2SKip Macy gnttab_grant_foreign_access_ref( 109189e0f4d2SKip Macy ref, 1092e4808c4bSKip Macy xenbus_get_otherend_id(sc->xb_dev), 109389e0f4d2SKip Macy buffer_ma >> PAGE_SHIFT, 1094ff662b5cSJustin T. Gibbs ring_req->operation == BLKIF_OP_WRITE); 109589e0f4d2SKip Macy 1096ff662b5cSJustin T. Gibbs *sg_ref = ref; 1097ff662b5cSJustin T. Gibbs *sg = (struct blkif_request_segment) { 109889e0f4d2SKip Macy .gref = ref, 109989e0f4d2SKip Macy .first_sect = fsect, 110089e0f4d2SKip Macy .last_sect = lsect }; 1101ff662b5cSJustin T. Gibbs sg++; 1102ff662b5cSJustin T. Gibbs sg_ref++; 1103ff662b5cSJustin T. Gibbs segs++; 1104ff662b5cSJustin T. Gibbs nsegs--; 110589e0f4d2SKip Macy } 1106ff662b5cSJustin T. Gibbs block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK); 1107ff662b5cSJustin T. Gibbs if (block_segs == 0) 1108ff662b5cSJustin T. Gibbs break; 110989e0f4d2SKip Macy 1110ff662b5cSJustin T. Gibbs sg = BLKRING_GET_SG_REQUEST(&sc->ring, sc->ring.req_prod_pvt); 1111ff662b5cSJustin T. Gibbs sc->ring.req_prod_pvt++; 1112ff662b5cSJustin T. Gibbs last_block_sg = sg + block_segs; 1113ff662b5cSJustin T. Gibbs } 111489e0f4d2SKip Macy 1115e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 1116e4808c4bSKip Macy op = BUS_DMASYNC_PREREAD; 1117e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 1118e4808c4bSKip Macy op = BUS_DMASYNC_PREWRITE; 1119e4808c4bSKip Macy else 1120e4808c4bSKip Macy op = 0; 1121e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 1122e4808c4bSKip Macy 1123ff662b5cSJustin T. Gibbs gnttab_free_grant_references(cm->gref_head); 1124e4808c4bSKip Macy 1125e4808c4bSKip Macy xb_enqueue_busy(cm); 1126e4808c4bSKip Macy 1127e4808c4bSKip Macy /* 1128e4808c4bSKip Macy * This flag means that we're probably executing in the busdma swi 1129e4808c4bSKip Macy * instead of in the startio context, so an explicit flush is needed. 1130e4808c4bSKip Macy */ 1131e4808c4bSKip Macy if (cm->cm_flags & XB_CMD_FROZEN) 1132e4808c4bSKip Macy flush_requests(sc); 1133e4808c4bSKip Macy 1134ff662b5cSJustin T. Gibbs //printf("%s: Done\n", __func__); 1135e4808c4bSKip Macy return; 1136e4808c4bSKip Macy } 113789e0f4d2SKip Macy 113889e0f4d2SKip Macy /* 113989e0f4d2SKip Macy * Dequeue buffers and place them in the shared communication ring. 114089e0f4d2SKip Macy * Return when no more requests can be accepted or all buffers have 114189e0f4d2SKip Macy * been queued. 114289e0f4d2SKip Macy * 114389e0f4d2SKip Macy * Signal XEN once the ring has been filled out. 114489e0f4d2SKip Macy */ 114589e0f4d2SKip Macy static void 114689e0f4d2SKip Macy xb_startio(struct xb_softc *sc) 114789e0f4d2SKip Macy { 1148e4808c4bSKip Macy struct xb_command *cm; 1149e4808c4bSKip Macy int error, queued = 0; 115089e0f4d2SKip Macy 1151e4808c4bSKip Macy mtx_assert(&sc->xb_io_lock, MA_OWNED); 115289e0f4d2SKip Macy 1153*06a630f6SJustin T. Gibbs if (sc->connected != BLKIF_STATE_CONNECTED) 1154*06a630f6SJustin T. Gibbs return; 1155*06a630f6SJustin T. Gibbs 1156ff662b5cSJustin T. Gibbs while (RING_FREE_REQUESTS(&sc->ring) >= sc->max_request_blocks) { 1157e4808c4bSKip Macy if (sc->xb_flags & XB_FROZEN) 1158e4808c4bSKip Macy break; 115989e0f4d2SKip Macy 1160e4808c4bSKip Macy cm = xb_dequeue_ready(sc); 116189e0f4d2SKip Macy 1162e4808c4bSKip Macy if (cm == NULL) 1163e4808c4bSKip Macy cm = xb_bio_command(sc); 1164e4808c4bSKip Macy 1165e4808c4bSKip Macy if (cm == NULL) 1166e4808c4bSKip Macy break; 1167e4808c4bSKip Macy 1168e4808c4bSKip Macy if ((error = blkif_queue_request(sc, cm)) != 0) { 1169e4808c4bSKip Macy printf("blkif_queue_request returned %d\n", error); 117089e0f4d2SKip Macy break; 117189e0f4d2SKip Macy } 117289e0f4d2SKip Macy queued++; 117389e0f4d2SKip Macy } 117489e0f4d2SKip Macy 117589e0f4d2SKip Macy if (queued != 0) 1176e4808c4bSKip Macy flush_requests(sc); 117789e0f4d2SKip Macy } 117889e0f4d2SKip Macy 117989e0f4d2SKip Macy static void 118089e0f4d2SKip Macy blkif_int(void *xsc) 118189e0f4d2SKip Macy { 1182e4808c4bSKip Macy struct xb_softc *sc = xsc; 1183e4808c4bSKip Macy struct xb_command *cm; 118489e0f4d2SKip Macy blkif_response_t *bret; 118589e0f4d2SKip Macy RING_IDX i, rp; 1186e4808c4bSKip Macy int op; 118789e0f4d2SKip Macy 1188e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 118989e0f4d2SKip Macy 1190*06a630f6SJustin T. Gibbs if (unlikely(sc->connected == BLKIF_STATE_DISCONNECTED)) { 1191e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 119289e0f4d2SKip Macy return; 119389e0f4d2SKip Macy } 119489e0f4d2SKip Macy 119589e0f4d2SKip Macy again: 1196e4808c4bSKip Macy rp = sc->ring.sring->rsp_prod; 119789e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 119889e0f4d2SKip Macy 1199ff662b5cSJustin T. Gibbs for (i = sc->ring.rsp_cons; i != rp;) { 1200e4808c4bSKip Macy bret = RING_GET_RESPONSE(&sc->ring, i); 1201e4808c4bSKip Macy cm = &sc->shadow[bret->id]; 120289e0f4d2SKip Macy 1203e4808c4bSKip Macy xb_remove_busy(cm); 1204ff662b5cSJustin T. Gibbs i += blkif_completion(cm); 120589e0f4d2SKip Macy 1206e4808c4bSKip Macy if (cm->operation == BLKIF_OP_READ) 1207e4808c4bSKip Macy op = BUS_DMASYNC_POSTREAD; 1208e4808c4bSKip Macy else if (cm->operation == BLKIF_OP_WRITE) 1209e4808c4bSKip Macy op = BUS_DMASYNC_POSTWRITE; 121089e0f4d2SKip Macy else 1211e4808c4bSKip Macy op = 0; 1212e4808c4bSKip Macy bus_dmamap_sync(sc->xb_io_dmat, cm->map, op); 1213e4808c4bSKip Macy bus_dmamap_unload(sc->xb_io_dmat, cm->map); 121489e0f4d2SKip Macy 1215e4808c4bSKip Macy /* 1216e4808c4bSKip Macy * If commands are completing then resources are probably 1217e4808c4bSKip Macy * being freed as well. It's a cheap assumption even when 1218e4808c4bSKip Macy * wrong. 1219e4808c4bSKip Macy */ 1220e4808c4bSKip Macy sc->xb_flags &= ~XB_FROZEN; 1221e4808c4bSKip Macy 1222e4808c4bSKip Macy /* 1223e4808c4bSKip Macy * Directly call the i/o complete routine to save an 1224e4808c4bSKip Macy * an indirection in the common case. 1225e4808c4bSKip Macy */ 1226e4808c4bSKip Macy cm->status = bret->status; 1227e4808c4bSKip Macy if (cm->bp) 1228e4808c4bSKip Macy xb_bio_complete(sc, cm); 1229e4808c4bSKip Macy else if (cm->cm_complete) 1230e4808c4bSKip Macy (cm->cm_complete)(cm); 1231e4808c4bSKip Macy else 1232e4808c4bSKip Macy xb_free_command(cm); 123389e0f4d2SKip Macy } 123489e0f4d2SKip Macy 1235e4808c4bSKip Macy sc->ring.rsp_cons = i; 123689e0f4d2SKip Macy 1237e4808c4bSKip Macy if (i != sc->ring.req_prod_pvt) { 123889e0f4d2SKip Macy int more_to_do; 1239e4808c4bSKip Macy RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, more_to_do); 124089e0f4d2SKip Macy if (more_to_do) 124189e0f4d2SKip Macy goto again; 124289e0f4d2SKip Macy } else { 1243e4808c4bSKip Macy sc->ring.sring->rsp_event = i + 1; 124489e0f4d2SKip Macy } 124589e0f4d2SKip Macy 1246e4808c4bSKip Macy xb_startio(sc); 124789e0f4d2SKip Macy 1248*06a630f6SJustin T. Gibbs if (unlikely(sc->connected == BLKIF_STATE_SUSPENDED)) 1249*06a630f6SJustin T. Gibbs wakeup(&sc->cm_busy); 1250*06a630f6SJustin T. Gibbs 1251e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 125289e0f4d2SKip Macy } 125389e0f4d2SKip Macy 125489e0f4d2SKip Macy static void 1255*06a630f6SJustin T. Gibbs blkif_free(struct xb_softc *sc) 125689e0f4d2SKip Macy { 1257ff662b5cSJustin T. Gibbs uint8_t *sring_page_ptr; 1258ff662b5cSJustin T. Gibbs int i; 125989e0f4d2SKip Macy 126089e0f4d2SKip Macy /* Prevent new requests being issued until we fix things up. */ 1261e4808c4bSKip Macy mtx_lock(&sc->xb_io_lock); 1262*06a630f6SJustin T. Gibbs sc->connected = BLKIF_STATE_DISCONNECTED; 1263e4808c4bSKip Macy mtx_unlock(&sc->xb_io_lock); 126489e0f4d2SKip Macy 126589e0f4d2SKip Macy /* Free resources associated with old device channel. */ 1266ff662b5cSJustin T. Gibbs if (sc->ring.sring != NULL) { 1267ff662b5cSJustin T. Gibbs sring_page_ptr = (uint8_t *)sc->ring.sring; 1268ff662b5cSJustin T. Gibbs for (i = 0; i < sc->ring_pages; i++) { 1269ff662b5cSJustin T. Gibbs if (sc->ring_ref[i] != GRANT_INVALID_REF) { 1270ff662b5cSJustin T. Gibbs gnttab_end_foreign_access_ref(sc->ring_ref[i]); 1271ff662b5cSJustin T. Gibbs sc->ring_ref[i] = GRANT_INVALID_REF; 1272ff662b5cSJustin T. Gibbs } 1273ff662b5cSJustin T. Gibbs sring_page_ptr += PAGE_SIZE; 1274ff662b5cSJustin T. Gibbs } 1275ff662b5cSJustin T. Gibbs free(sc->ring.sring, M_XENBLOCKFRONT); 1276e4808c4bSKip Macy sc->ring.sring = NULL; 127789e0f4d2SKip Macy } 1278ff662b5cSJustin T. Gibbs 1279ff662b5cSJustin T. Gibbs if (sc->shadow) { 1280ff662b5cSJustin T. Gibbs 1281ff662b5cSJustin T. Gibbs for (i = 0; i < sc->max_requests; i++) { 1282ff662b5cSJustin T. Gibbs struct xb_command *cm; 1283ff662b5cSJustin T. Gibbs 1284ff662b5cSJustin T. Gibbs cm = &sc->shadow[i]; 1285ff662b5cSJustin T. Gibbs if (cm->sg_refs != NULL) { 1286ff662b5cSJustin T. Gibbs free(cm->sg_refs, M_XENBLOCKFRONT); 1287ff662b5cSJustin T. Gibbs cm->sg_refs = NULL; 1288ff662b5cSJustin T. Gibbs } 1289ff662b5cSJustin T. Gibbs 1290ff662b5cSJustin T. Gibbs bus_dmamap_destroy(sc->xb_io_dmat, cm->map); 1291ff662b5cSJustin T. Gibbs } 1292ff662b5cSJustin T. Gibbs free(sc->shadow, M_XENBLOCKFRONT); 1293ff662b5cSJustin T. Gibbs sc->shadow = NULL; 1294*06a630f6SJustin T. Gibbs 1295*06a630f6SJustin T. Gibbs bus_dma_tag_destroy(sc->xb_io_dmat); 1296*06a630f6SJustin T. Gibbs 1297*06a630f6SJustin T. Gibbs xb_initq_free(sc); 1298*06a630f6SJustin T. Gibbs xb_initq_ready(sc); 1299*06a630f6SJustin T. Gibbs xb_initq_complete(sc); 1300ff662b5cSJustin T. Gibbs } 1301ff662b5cSJustin T. Gibbs 1302ff662b5cSJustin T. Gibbs if (sc->irq) { 1303e4808c4bSKip Macy unbind_from_irqhandler(sc->irq); 1304e4808c4bSKip Macy sc->irq = 0; 1305ff662b5cSJustin T. Gibbs } 130689e0f4d2SKip Macy } 130789e0f4d2SKip Macy 1308ff662b5cSJustin T. Gibbs static int 1309e4808c4bSKip Macy blkif_completion(struct xb_command *s) 131089e0f4d2SKip Macy { 1311ff662b5cSJustin T. Gibbs //printf("%s: Req %p(%d)\n", __func__, s, s->nseg); 1312ff662b5cSJustin T. Gibbs gnttab_end_foreign_access_references(s->nseg, s->sg_refs); 1313ff662b5cSJustin T. Gibbs return (BLKIF_SEGS_TO_BLOCKS(s->nseg)); 131489e0f4d2SKip Macy } 131589e0f4d2SKip Macy 131623dc5621SKip Macy /* ** Driver registration ** */ 131723dc5621SKip Macy static device_method_t blkfront_methods[] = { 131823dc5621SKip Macy /* Device interface */ 131923dc5621SKip Macy DEVMETHOD(device_probe, blkfront_probe), 132023dc5621SKip Macy DEVMETHOD(device_attach, blkfront_attach), 132123dc5621SKip Macy DEVMETHOD(device_detach, blkfront_detach), 132223dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 132312678024SDoug Rabson DEVMETHOD(device_suspend, blkfront_suspend), 132423dc5621SKip Macy DEVMETHOD(device_resume, blkfront_resume), 132589e0f4d2SKip Macy 132623dc5621SKip Macy /* Xenbus interface */ 1327ff662b5cSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, blkfront_backend_changed), 132889e0f4d2SKip Macy 132923dc5621SKip Macy { 0, 0 } 133089e0f4d2SKip Macy }; 133189e0f4d2SKip Macy 133223dc5621SKip Macy static driver_t blkfront_driver = { 133323dc5621SKip Macy "xbd", 133423dc5621SKip Macy blkfront_methods, 1335e4808c4bSKip Macy sizeof(struct xb_softc), 133689e0f4d2SKip Macy }; 133723dc5621SKip Macy devclass_t blkfront_devclass; 133889e0f4d2SKip Macy 1339ff662b5cSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, blkfront_driver, blkfront_devclass, 0, 0); 1340