2 * XenBSD block device driver
4 * Copyright (c) 2009 Scott Long, Yahoo!
5 * Copyright (c) 2009 Frank Suchomel, Citrix
6 * Copyright (c) 2009 Doug F. Rabson, Citrix
7 * Copyright (c) 2005 Kip Macy
8 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
9 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to
14 * deal in the Software without restriction, including without limitation the
15 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/vmparam.h>
50 #include <sys/bus_dma.h>
52 #include <machine/_inttypes.h>
53 #include <machine/xen/xen-os.h>
54 #include <machine/xen/xenfunc.h>
56 #include <xen/hypervisor.h>
57 #include <xen/xen_intr.h>
58 #include <xen/evtchn.h>
59 #include <xen/gnttab.h>
60 #include <xen/interface/grant_table.h>
61 #include <xen/interface/io/protocols.h>
62 #include <xen/xenbus/xenbusvar.h>
64 #include <geom/geom_disk.h>
66 #include <dev/xen/blkfront/block.h>
68 #include "xenbus_if.h"
71 static void xb_free_command(struct xb_command *cm);
72 static void xb_startio(struct xb_softc *sc);
73 static void blkfront_connect(struct xb_softc *);
74 static void blkfront_closing(device_t);
75 static int blkfront_detach(device_t);
76 static int setup_blkring(struct xb_softc *);
77 static void blkif_int(void *);
78 static void blkfront_initialize(struct xb_softc *);
80 static void blkif_recover(struct xb_softc *);
82 static int blkif_completion(struct xb_command *);
83 static void blkif_free(struct xb_softc *, int);
84 static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int);
86 MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
88 #define GRANT_INVALID_REF 0
90 /* Control whether runtime update of vbds is enabled. */
91 #define ENABLE_VBD_UPDATE 0
94 static void vbd_update(void);
97 #define BLKIF_STATE_DISCONNECTED 0
98 #define BLKIF_STATE_CONNECTED 1
99 #define BLKIF_STATE_SUSPENDED 2
102 static char *blkif_state_name[] = {
103 [BLKIF_STATE_DISCONNECTED] = "disconnected",
104 [BLKIF_STATE_CONNECTED] = "connected",
105 [BLKIF_STATE_SUSPENDED] = "closed",
108 static char * blkif_status_name[] = {
109 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
110 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
111 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
112 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
117 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
119 #define DPRINTK(fmt, args...)
122 static int blkif_open(struct disk *dp);
123 static int blkif_close(struct disk *dp);
124 static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
125 static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm);
126 static void xb_strategy(struct bio *bp);
128 // In order to quiesce the device during kernel dumps, outstanding requests to
129 // DOM0 for disk reads/writes need to be accounted for.
130 static int xb_dump(void *, void *, vm_offset_t, off_t, size_t);
132 /* XXX move to xb_vbd.c when VBD update support is added */
135 #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
136 #define XBD_SECTOR_SHFT 9
139 * Translate Linux major/minor to an appropriate name and unit
140 * number. For HVM guests, this allows us to use the same drive names
141 * with blkfront as the emulated drives, easing transition slightly.
144 blkfront_vdevice_to_unit(int vdevice, int *unit, const char **name)
146 static struct vdev_info {
152 {3, 6, 0, "ad"}, /* ide0 */
153 {22, 6, 2, "ad"}, /* ide1 */
154 {33, 6, 4, "ad"}, /* ide2 */
155 {34, 6, 6, "ad"}, /* ide3 */
156 {56, 6, 8, "ad"}, /* ide4 */
157 {57, 6, 10, "ad"}, /* ide5 */
158 {88, 6, 12, "ad"}, /* ide6 */
159 {89, 6, 14, "ad"}, /* ide7 */
160 {90, 6, 16, "ad"}, /* ide8 */
161 {91, 6, 18, "ad"}, /* ide9 */
163 {8, 4, 0, "da"}, /* scsi disk0 */
164 {65, 4, 16, "da"}, /* scsi disk1 */
165 {66, 4, 32, "da"}, /* scsi disk2 */
166 {67, 4, 48, "da"}, /* scsi disk3 */
167 {68, 4, 64, "da"}, /* scsi disk4 */
168 {69, 4, 80, "da"}, /* scsi disk5 */
169 {70, 4, 96, "da"}, /* scsi disk6 */
170 {71, 4, 112, "da"}, /* scsi disk7 */
171 {128, 4, 128, "da"}, /* scsi disk8 */
172 {129, 4, 144, "da"}, /* scsi disk9 */
173 {130, 4, 160, "da"}, /* scsi disk10 */
174 {131, 4, 176, "da"}, /* scsi disk11 */
175 {132, 4, 192, "da"}, /* scsi disk12 */
176 {133, 4, 208, "da"}, /* scsi disk13 */
177 {134, 4, 224, "da"}, /* scsi disk14 */
178 {135, 4, 240, "da"}, /* scsi disk15 */
180 {202, 4, 0, "xbd"}, /* xbd */
184 int major = vdevice >> 8;
185 int minor = vdevice & 0xff;
188 if (vdevice & (1 << 28)) {
189 *unit = (vdevice & ((1 << 28) - 1)) >> 8;
193 for (i = 0; info[i].major; i++) {
194 if (info[i].major == major) {
195 *unit = info[i].base + (minor >> info[i].shift);
196 *name = info[i].name;
206 xlvbd_add(struct xb_softc *sc, blkif_sector_t sectors,
207 int vdevice, uint16_t vdisk_info, unsigned long sector_size)
212 blkfront_vdevice_to_unit(vdevice, &unit, &name);
216 if (strcmp(name, "xbd"))
217 device_printf(sc->xb_dev, "attaching as %s%d\n", name, unit);
219 sc->xb_disk = disk_alloc();
220 sc->xb_disk->d_unit = sc->xb_unit;
221 sc->xb_disk->d_open = blkif_open;
222 sc->xb_disk->d_close = blkif_close;
223 sc->xb_disk->d_ioctl = blkif_ioctl;
224 sc->xb_disk->d_strategy = xb_strategy;
225 sc->xb_disk->d_dump = xb_dump;
226 sc->xb_disk->d_name = name;
227 sc->xb_disk->d_drv1 = sc;
228 sc->xb_disk->d_sectorsize = sector_size;
230 sc->xb_disk->d_mediasize = sectors * sector_size;
231 sc->xb_disk->d_maxsize = sc->max_request_size;
232 sc->xb_disk->d_flags = 0;
233 disk_create(sc->xb_disk, DISK_VERSION_00);
238 /************************ end VBD support *****************/
241 * Read/write routine for a buffer. Finds the proper unit, place it on
242 * the sortq and kick the controller.
245 xb_strategy(struct bio *bp)
247 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
251 bp->bio_error = EINVAL;
252 bp->bio_flags |= BIO_ERROR;
253 bp->bio_resid = bp->bio_bcount;
259 * Place it in the queue of disk activities for this disk
261 mtx_lock(&sc->xb_io_lock);
263 xb_enqueue_bio(sc, bp);
266 mtx_unlock(&sc->xb_io_lock);
271 xb_bio_complete(struct xb_softc *sc, struct xb_command *cm)
277 if ( unlikely(cm->status != BLKIF_RSP_OKAY) ) {
278 disk_err(bp, "disk error" , -1, 0);
279 printf(" status: %x\n", cm->status);
280 bp->bio_flags |= BIO_ERROR;
283 if (bp->bio_flags & BIO_ERROR)
292 // Quiesce the disk writes for a dump file before allowing the next buffer.
294 xb_quiesce(struct xb_softc *sc)
298 // While there are outstanding requests
299 while (!TAILQ_EMPTY(&sc->cm_busy)) {
300 RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, mtd);
302 /* Recieved request completions, update queue. */
305 if (!TAILQ_EMPTY(&sc->cm_busy)) {
307 * Still pending requests, wait for the disk i/o
315 /* Kernel dump function for a paravirtualized disk device */
317 xb_dump_complete(struct xb_command *cm)
320 xb_enqueue_complete(cm);
324 xb_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
327 struct disk *dp = arg;
328 struct xb_softc *sc = (struct xb_softc *) dp->d_drv1;
329 struct xb_command *cm;
337 xb_quiesce(sc); /* All quiet on the western front. */
340 * If this lock is held, then this module is failing, and a
341 * successful kernel dump is highly unlikely anyway.
343 mtx_lock(&sc->xb_io_lock);
345 /* Split the 64KB block as needed */
346 for (sbp=0; length > 0; sbp++) {
347 cm = xb_dequeue_free(sc);
349 mtx_unlock(&sc->xb_io_lock);
350 device_printf(sc->xb_dev, "dump: no more commands?\n");
354 if (gnttab_alloc_grant_references(sc->max_request_segments,
355 &cm->gref_head) != 0) {
357 mtx_unlock(&sc->xb_io_lock);
358 device_printf(sc->xb_dev, "no more grant allocs?\n");
362 chunk = length > sc->max_request_size
363 ? sc->max_request_size : length;
366 cm->operation = BLKIF_OP_WRITE;
367 cm->sector_number = offset / dp->d_sectorsize;
368 cm->cm_complete = xb_dump_complete;
370 xb_enqueue_ready(cm);
374 virtual = (char *) virtual + chunk;
377 /* Tell DOM0 to do the I/O */
379 mtx_unlock(&sc->xb_io_lock);
381 /* Poll for the completion. */
382 xb_quiesce(sc); /* All quite on the eastern front */
384 /* If there were any errors, bail out... */
385 while ((cm = xb_dequeue_complete(sc)) != NULL) {
386 if (cm->status != BLKIF_RSP_OKAY) {
387 device_printf(sc->xb_dev,
388 "Dump I/O failed at sector %jd\n",
400 blkfront_probe(device_t dev)
403 if (!strcmp(xenbus_get_type(dev), "vbd")) {
404 device_set_desc(dev, "Virtual Block Device");
413 * Setup supplies the backend dir, virtual device. We place an event
414 * channel and shared frame entries. We watch backend to wait if it's
418 blkfront_attach(device_t dev)
427 /* FIXME: Use dynamic device id if this is not set. */
428 error = xs_scanf(XST_NIL, xenbus_get_node(dev),
429 "virtual-device", NULL, "%i", &vdevice);
431 xenbus_dev_fatal(dev, error, "reading virtual-device");
432 device_printf(dev, "Couldn't determine virtual device.\n");
436 blkfront_vdevice_to_unit(vdevice, &unit, &name);
437 if (!strcmp(name, "xbd"))
438 device_set_unit(dev, unit);
440 sc = device_get_softc(dev);
441 mtx_init(&sc->xb_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
445 xb_initq_complete(sc);
447 for (i = 0; i < XBF_MAX_RING_PAGES; i++)
448 sc->ring_ref[i] = GRANT_INVALID_REF;
451 sc->vdevice = vdevice;
452 sc->connected = BLKIF_STATE_DISCONNECTED;
454 /* Front end dir is a number, which is used as the id. */
455 sc->handle = strtoul(strrchr(xenbus_get_node(dev),'/')+1, NULL, 0);
457 /* Wait for backend device to publish its protocol capabilities. */
458 xenbus_set_state(dev, XenbusStateInitialising);
464 blkfront_suspend(device_t dev)
466 struct xb_softc *sc = device_get_softc(dev);
468 /* Prevent new requests being issued until we fix things up. */
469 mtx_lock(&sc->xb_io_lock);
470 sc->connected = BLKIF_STATE_SUSPENDED;
471 mtx_unlock(&sc->xb_io_lock);
477 blkfront_resume(device_t dev)
480 struct xb_softc *sc = device_get_softc(dev);
482 DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev));
484 /* XXX This can't work!!! */
486 blkfront_initialize(sc);
487 if (sc->connected == BLKIF_STATE_SUSPENDED)
494 blkfront_initialize(struct xb_softc *sc)
496 const char *otherend_path;
497 const char *node_path;
501 if (xenbus_get_state(sc->xb_dev) != XenbusStateInitialising)
505 * Protocol defaults valid even if negotiation for a
509 sc->max_requests = BLKIF_MAX_RING_REQUESTS(PAGE_SIZE);
510 sc->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
511 sc->max_request_size = (sc->max_request_segments - 1) * PAGE_SIZE;
512 sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments);
515 * Protocol negotiation.
517 * \note xs_gather() returns on the first encountered error, so
518 * we must use independant calls in order to guarantee
519 * we don't miss information in a sparsly populated back-end
522 otherend_path = xenbus_get_otherend_path(sc->xb_dev);
523 node_path = xenbus_get_node(sc->xb_dev);
524 (void)xs_scanf(XST_NIL, otherend_path,
525 "max-ring-pages", NULL, "%" PRIu32,
528 (void)xs_scanf(XST_NIL, otherend_path,
529 "max-requests", NULL, "%" PRIu32,
532 (void)xs_scanf(XST_NIL, otherend_path,
533 "max-request-segments", NULL, "%" PRIu32,
534 &sc->max_request_segments);
536 (void)xs_scanf(XST_NIL, otherend_path,
537 "max-request-size", NULL, "%" PRIu32,
538 &sc->max_request_size);
540 if (sc->ring_pages > XBF_MAX_RING_PAGES) {
541 device_printf(sc->xb_dev, "Back-end specified ring-pages of "
542 "%u limited to front-end limit of %zu.\n",
543 sc->ring_pages, XBF_MAX_RING_PAGES);
544 sc->ring_pages = XBF_MAX_RING_PAGES;
547 if (sc->max_requests > XBF_MAX_REQUESTS) {
548 device_printf(sc->xb_dev, "Back-end specified max_requests of "
549 "%u limited to front-end limit of %u.\n",
550 sc->max_requests, XBF_MAX_REQUESTS);
551 sc->max_requests = XBF_MAX_REQUESTS;
554 if (sc->max_request_segments > XBF_MAX_SEGMENTS_PER_REQUEST) {
555 device_printf(sc->xb_dev, "Back-end specificed "
556 "max_requests_segments of %u limited to "
557 "front-end limit of %u.\n",
558 sc->max_request_segments,
559 XBF_MAX_SEGMENTS_PER_REQUEST);
560 sc->max_request_segments = XBF_MAX_SEGMENTS_PER_REQUEST;
563 if (sc->max_request_size > XBF_MAX_REQUEST_SIZE) {
564 device_printf(sc->xb_dev, "Back-end specificed "
565 "max_request_size of %u limited to front-end "
566 "limit of %u.\n", sc->max_request_size,
567 XBF_MAX_REQUEST_SIZE);
568 sc->max_request_size = XBF_MAX_REQUEST_SIZE;
570 sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments);
572 /* Allocate datastructures based on negotiated values. */
573 error = bus_dma_tag_create(NULL, /* parent */
574 512, PAGE_SIZE, /* algnmnt, boundary */
575 BUS_SPACE_MAXADDR, /* lowaddr */
576 BUS_SPACE_MAXADDR, /* highaddr */
577 NULL, NULL, /* filter, filterarg */
578 sc->max_request_size,
579 sc->max_request_segments,
580 PAGE_SIZE, /* maxsegsize */
581 BUS_DMA_ALLOCNOW, /* flags */
582 busdma_lock_mutex, /* lockfunc */
583 &sc->xb_io_lock, /* lockarg */
586 xenbus_dev_fatal(sc->xb_dev, error,
587 "Cannot allocate parent DMA tag\n");
591 /* Per-transaction data allocation. */
592 sc->shadow = malloc(sizeof(*sc->shadow) * sc->max_requests,
593 M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
594 if (sc->shadow == NULL) {
595 xenbus_dev_fatal(sc->xb_dev, error,
596 "Cannot allocate request structures\n");
599 for (i = 0; i < sc->max_requests; i++) {
600 struct xb_command *cm;
603 cm->sg_refs = malloc(sizeof(grant_ref_t)
604 * sc->max_request_segments,
605 M_XENBLOCKFRONT, M_NOWAIT);
606 if (cm->sg_refs == NULL)
610 if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0)
615 if (setup_blkring(sc) != 0)
618 error = xs_printf(XST_NIL, node_path,
619 "ring-pages","%u", sc->ring_pages);
621 xenbus_dev_fatal(sc->xb_dev, error,
622 "writing %s/ring-pages",
627 error = xs_printf(XST_NIL, node_path,
628 "max-requests","%u", sc->max_requests);
630 xenbus_dev_fatal(sc->xb_dev, error,
631 "writing %s/max-requests",
636 error = xs_printf(XST_NIL, node_path,
637 "max-request-segments","%u", sc->max_request_segments);
639 xenbus_dev_fatal(sc->xb_dev, error,
640 "writing %s/max-request-segments",
645 error = xs_printf(XST_NIL, node_path,
646 "max-request-size","%u", sc->max_request_size);
648 xenbus_dev_fatal(sc->xb_dev, error,
649 "writing %s/max-request-size",
654 error = xs_printf(XST_NIL, node_path, "event-channel",
655 "%u", irq_to_evtchn_port(sc->irq));
657 xenbus_dev_fatal(sc->xb_dev, error,
658 "writing %s/event-channel",
663 error = xs_printf(XST_NIL, node_path,
664 "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE);
666 xenbus_dev_fatal(sc->xb_dev, error,
667 "writing %s/protocol",
672 xenbus_set_state(sc->xb_dev, XenbusStateInitialised);
676 setup_blkring(struct xb_softc *sc)
678 blkif_sring_t *sring;
679 uintptr_t sring_page_addr;
683 sring = malloc(sc->ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
686 xenbus_dev_fatal(sc->xb_dev, ENOMEM, "allocating shared ring");
689 SHARED_RING_INIT(sring);
690 FRONT_RING_INIT(&sc->ring, sring, sc->ring_pages * PAGE_SIZE);
692 for (i = 0, sring_page_addr = (uintptr_t)sring;
694 i++, sring_page_addr += PAGE_SIZE) {
696 error = xenbus_grant_ring(sc->xb_dev,
697 (vtomach(sring_page_addr) >> PAGE_SHIFT), &sc->ring_ref[i]);
699 xenbus_dev_fatal(sc->xb_dev, error,
700 "granting ring_ref(%d)", i);
704 error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev),
705 "ring-ref","%u", sc->ring_ref[0]);
707 xenbus_dev_fatal(sc->xb_dev, error, "writing %s/ring-ref",
708 xenbus_get_node(sc->xb_dev));
711 for (i = 1; i < sc->ring_pages; i++) {
712 char ring_ref_name[]= "ring_refXX";
714 snprintf(ring_ref_name, sizeof(ring_ref_name), "ring-ref%u", i);
715 error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev),
716 ring_ref_name, "%u", sc->ring_ref[i]);
718 xenbus_dev_fatal(sc->xb_dev, error, "writing %s/%s",
719 xenbus_get_node(sc->xb_dev),
725 error = bind_listening_port_to_irqhandler(
726 xenbus_get_otherend_id(sc->xb_dev),
727 "xbd", (driver_intr_t *)blkif_int, sc,
728 INTR_TYPE_BIO | INTR_MPSAFE, &sc->irq);
730 xenbus_dev_fatal(sc->xb_dev, error,
731 "bind_evtchn_to_irqhandler failed");
739 * Callback received when the backend's state changes.
742 blkfront_backend_changed(device_t dev, XenbusState backend_state)
744 struct xb_softc *sc = device_get_softc(dev);
746 DPRINTK("backend_state=%d\n", backend_state);
748 switch (backend_state) {
749 case XenbusStateUnknown:
750 case XenbusStateInitialising:
751 case XenbusStateReconfigured:
752 case XenbusStateReconfiguring:
753 case XenbusStateClosed:
756 case XenbusStateInitWait:
757 blkfront_initialize(sc);
760 case XenbusStateInitialised:
761 case XenbusStateConnected:
762 blkfront_initialize(sc);
763 blkfront_connect(sc);
766 case XenbusStateClosing:
768 xenbus_dev_error(dev, -EBUSY,
769 "Device in use; refusing to close");
771 blkfront_closing(dev);
779 ** Invoked when the backend is finally 'ready' (and has told produced
780 ** the details about the physical device - #sectors, size, etc).
783 blkfront_connect(struct xb_softc *sc)
785 device_t dev = sc->xb_dev;
786 unsigned long sectors, sector_size;
788 int err, feature_barrier;
790 if( (sc->connected == BLKIF_STATE_CONNECTED) ||
791 (sc->connected == BLKIF_STATE_SUSPENDED) )
794 DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
796 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
797 "sectors", "%lu", §ors,
798 "info", "%u", &binfo,
799 "sector-size", "%lu", §or_size,
802 xenbus_dev_fatal(dev, err,
803 "reading backend fields at %s",
804 xenbus_get_otherend_path(dev));
807 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
808 "feature-barrier", "%lu", &feature_barrier,
810 if (!err || feature_barrier)
811 sc->xb_flags |= XB_BARRIER;
813 device_printf(dev, "%juMB <%s> at %s",
814 (uintmax_t) sectors / (1048576 / sector_size),
815 device_get_desc(dev),
816 xenbus_get_node(dev));
817 bus_print_child_footer(device_get_parent(dev), dev);
819 xlvbd_add(sc, sectors, sc->vdevice, binfo, sector_size);
821 (void)xenbus_set_state(dev, XenbusStateConnected);
823 /* Kick pending requests. */
824 mtx_lock(&sc->xb_io_lock);
825 sc->connected = BLKIF_STATE_CONNECTED;
827 sc->xb_flags |= XB_READY;
828 mtx_unlock(&sc->xb_io_lock);
833 * Handle the change of state of the backend to Closing. We must delete our
834 * device-layer structures now, to ensure that writes are flushed through to
835 * the backend. Once this is done, we can switch to Closed in
839 blkfront_closing(device_t dev)
841 struct xb_softc *sc = device_get_softc(dev);
843 xenbus_set_state(dev, XenbusStateClosing);
845 DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev));
847 if (sc->xb_disk != NULL) {
848 disk_destroy(sc->xb_disk);
852 xenbus_set_state(dev, XenbusStateClosed);
857 blkfront_detach(device_t dev)
859 struct xb_softc *sc = device_get_softc(dev);
861 DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev));
864 mtx_destroy(&sc->xb_io_lock);
871 flush_requests(struct xb_softc *sc)
875 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->ring, notify);
878 notify_remote_via_irq(sc->irq);
882 blkif_restart_queue_callback(void *arg)
884 struct xb_softc *sc = arg;
886 mtx_lock(&sc->xb_io_lock);
890 mtx_unlock(&sc->xb_io_lock);
894 blkif_open(struct disk *dp)
896 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
899 printf("xb%d: not found", sc->xb_unit);
903 sc->xb_flags |= XB_OPEN;
909 blkif_close(struct disk *dp)
911 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
915 sc->xb_flags &= ~XB_OPEN;
916 if (--(sc->users) == 0) {
917 /* Check whether we have been instructed to close. We will
918 have ignored this request initially, as the device was
920 device_t dev = sc->xb_dev;
922 xenbus_read_driver_state(xenbus_get_otherend_path(dev));
924 if (state == XenbusStateClosing)
925 blkfront_closing(dev);
931 blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
933 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
942 xb_free_command(struct xb_command *cm)
945 KASSERT((cm->cm_flags & XB_ON_XBQ_MASK) == 0,
946 ("Freeing command that is still on a queue\n"));
950 cm->cm_complete = NULL;
955 * blkif_queue_request
959 * id: for guest use only.
960 * operation: BLKIF_OP_{READ,WRITE,PROBE}
961 * buffer: buffer to read/write into. this should be a
962 * virtual address in the guest os.
964 static struct xb_command *
965 xb_bio_command(struct xb_softc *sc)
967 struct xb_command *cm;
970 if (unlikely(sc->connected != BLKIF_STATE_CONNECTED))
973 bp = xb_dequeue_bio(sc);
977 if ((cm = xb_dequeue_free(sc)) == NULL) {
978 xb_requeue_bio(sc, bp);
982 if (gnttab_alloc_grant_references(sc->max_request_segments,
983 &cm->gref_head) != 0) {
984 gnttab_request_free_callback(&sc->callback,
985 blkif_restart_queue_callback, sc,
986 sc->max_request_segments);
987 xb_requeue_bio(sc, bp);
989 sc->xb_flags |= XB_FROZEN;
994 cm->data = bp->bio_data;
995 cm->datalen = bp->bio_bcount;
996 cm->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ :
998 cm->sector_number = (blkif_sector_t)bp->bio_pblkno;
1004 blkif_queue_request(struct xb_softc *sc, struct xb_command *cm)
1008 error = bus_dmamap_load(sc->xb_io_dmat, cm->map, cm->data, cm->datalen,
1009 blkif_queue_cb, cm, 0);
1010 if (error == EINPROGRESS) {
1011 printf("EINPROGRESS\n");
1012 sc->xb_flags |= XB_FROZEN;
1013 cm->cm_flags |= XB_CMD_FROZEN;
1021 blkif_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1023 struct xb_softc *sc;
1024 struct xb_command *cm;
1025 blkif_request_t *ring_req;
1026 struct blkif_request_segment *sg;
1027 struct blkif_request_segment *last_block_sg;
1028 grant_ref_t *sg_ref;
1029 vm_paddr_t buffer_ma;
1030 uint64_t fsect, lsect;
1038 //printf("%s: Start\n", __func__);
1040 printf("error %d in blkif_queue_cb\n", error);
1041 cm->bp->bio_error = EIO;
1043 xb_free_command(cm);
1047 /* Fill out a communications ring structure. */
1048 ring_req = RING_GET_REQUEST(&sc->ring, sc->ring.req_prod_pvt);
1049 sc->ring.req_prod_pvt++;
1050 ring_req->id = cm->id;
1051 ring_req->operation = cm->operation;
1052 ring_req->sector_number = cm->sector_number;
1053 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk;
1054 ring_req->nr_segments = nsegs;
1057 block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
1059 last_block_sg = sg + block_segs;
1060 sg_ref = cm->sg_refs;
1064 while (sg < last_block_sg) {
1065 buffer_ma = segs->ds_addr;
1066 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
1067 lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1;
1069 KASSERT(lsect <= 7, ("XEN disk driver data cannot "
1070 "cross a page boundary"));
1072 /* install a grant reference. */
1073 ref = gnttab_claim_grant_reference(&cm->gref_head);
1076 * GNTTAB_LIST_END == 0xffffffff, but it is private
1079 KASSERT(ref != ~0, ("grant_reference failed"));
1081 gnttab_grant_foreign_access_ref(
1083 xenbus_get_otherend_id(sc->xb_dev),
1084 buffer_ma >> PAGE_SHIFT,
1085 ring_req->operation == BLKIF_OP_WRITE);
1088 *sg = (struct blkif_request_segment) {
1090 .first_sect = fsect,
1091 .last_sect = lsect };
1097 block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
1098 if (block_segs == 0)
1101 sg = BLKRING_GET_SG_REQUEST(&sc->ring, sc->ring.req_prod_pvt);
1102 sc->ring.req_prod_pvt++;
1103 last_block_sg = sg + block_segs;
1106 if (cm->operation == BLKIF_OP_READ)
1107 op = BUS_DMASYNC_PREREAD;
1108 else if (cm->operation == BLKIF_OP_WRITE)
1109 op = BUS_DMASYNC_PREWRITE;
1112 bus_dmamap_sync(sc->xb_io_dmat, cm->map, op);
1114 gnttab_free_grant_references(cm->gref_head);
1116 xb_enqueue_busy(cm);
1119 * This flag means that we're probably executing in the busdma swi
1120 * instead of in the startio context, so an explicit flush is needed.
1122 if (cm->cm_flags & XB_CMD_FROZEN)
1125 //printf("%s: Done\n", __func__);
1130 * Dequeue buffers and place them in the shared communication ring.
1131 * Return when no more requests can be accepted or all buffers have
1134 * Signal XEN once the ring has been filled out.
1137 xb_startio(struct xb_softc *sc)
1139 struct xb_command *cm;
1140 int error, queued = 0;
1142 mtx_assert(&sc->xb_io_lock, MA_OWNED);
1144 while (RING_FREE_REQUESTS(&sc->ring) >= sc->max_request_blocks) {
1145 if (sc->xb_flags & XB_FROZEN)
1148 cm = xb_dequeue_ready(sc);
1151 cm = xb_bio_command(sc);
1156 if ((error = blkif_queue_request(sc, cm)) != 0) {
1157 printf("blkif_queue_request returned %d\n", error);
1168 blkif_int(void *xsc)
1170 struct xb_softc *sc = xsc;
1171 struct xb_command *cm;
1172 blkif_response_t *bret;
1176 mtx_lock(&sc->xb_io_lock);
1178 if (unlikely(sc->connected != BLKIF_STATE_CONNECTED)) {
1179 mtx_unlock(&sc->xb_io_lock);
1184 rp = sc->ring.sring->rsp_prod;
1185 rmb(); /* Ensure we see queued responses up to 'rp'. */
1187 for (i = sc->ring.rsp_cons; i != rp;) {
1188 bret = RING_GET_RESPONSE(&sc->ring, i);
1189 cm = &sc->shadow[bret->id];
1192 i += blkif_completion(cm);
1194 if (cm->operation == BLKIF_OP_READ)
1195 op = BUS_DMASYNC_POSTREAD;
1196 else if (cm->operation == BLKIF_OP_WRITE)
1197 op = BUS_DMASYNC_POSTWRITE;
1200 bus_dmamap_sync(sc->xb_io_dmat, cm->map, op);
1201 bus_dmamap_unload(sc->xb_io_dmat, cm->map);
1204 * If commands are completing then resources are probably
1205 * being freed as well. It's a cheap assumption even when
1208 sc->xb_flags &= ~XB_FROZEN;
1211 * Directly call the i/o complete routine to save an
1212 * an indirection in the common case.
1214 cm->status = bret->status;
1216 xb_bio_complete(sc, cm);
1217 else if (cm->cm_complete)
1218 (cm->cm_complete)(cm);
1220 xb_free_command(cm);
1223 sc->ring.rsp_cons = i;
1225 if (i != sc->ring.req_prod_pvt) {
1227 RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, more_to_do);
1231 sc->ring.sring->rsp_event = i + 1;
1236 mtx_unlock(&sc->xb_io_lock);
1240 blkif_free(struct xb_softc *sc, int suspend)
1242 uint8_t *sring_page_ptr;
1245 /* Prevent new requests being issued until we fix things up. */
1246 mtx_lock(&sc->xb_io_lock);
1247 sc->connected = suspend ?
1248 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1249 mtx_unlock(&sc->xb_io_lock);
1251 /* Free resources associated with old device channel. */
1252 if (sc->ring.sring != NULL) {
1253 sring_page_ptr = (uint8_t *)sc->ring.sring;
1254 for (i = 0; i < sc->ring_pages; i++) {
1255 if (sc->ring_ref[i] != GRANT_INVALID_REF) {
1256 gnttab_end_foreign_access_ref(sc->ring_ref[i]);
1257 sc->ring_ref[i] = GRANT_INVALID_REF;
1259 sring_page_ptr += PAGE_SIZE;
1261 free(sc->ring.sring, M_XENBLOCKFRONT);
1262 sc->ring.sring = NULL;
1267 for (i = 0; i < sc->max_requests; i++) {
1268 struct xb_command *cm;
1270 cm = &sc->shadow[i];
1271 if (cm->sg_refs != NULL) {
1272 free(cm->sg_refs, M_XENBLOCKFRONT);
1276 bus_dmamap_destroy(sc->xb_io_dmat, cm->map);
1278 free(sc->shadow, M_XENBLOCKFRONT);
1283 unbind_from_irqhandler(sc->irq);
1289 blkif_completion(struct xb_command *s)
1291 //printf("%s: Req %p(%d)\n", __func__, s, s->nseg);
1292 gnttab_end_foreign_access_references(s->nseg, s->sg_refs);
1293 return (BLKIF_SEGS_TO_BLOCKS(s->nseg));
1298 blkif_recover(struct xb_softc *sc)
1301 * XXX The whole concept of not quiescing and completing all i/o
1302 * during suspend, and then hoping to recover and replay the
1303 * resulting abandoned I/O during resume, is laughable. At best,
1304 * it invalidates the i/o ordering rules required by just about
1305 * every filesystem, and at worst it'll corrupt data. The code
1306 * has been removed until further notice.
1311 /* ** Driver registration ** */
1312 static device_method_t blkfront_methods[] = {
1313 /* Device interface */
1314 DEVMETHOD(device_probe, blkfront_probe),
1315 DEVMETHOD(device_attach, blkfront_attach),
1316 DEVMETHOD(device_detach, blkfront_detach),
1317 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1318 DEVMETHOD(device_suspend, blkfront_suspend),
1319 DEVMETHOD(device_resume, blkfront_resume),
1321 /* Xenbus interface */
1322 DEVMETHOD(xenbus_otherend_changed, blkfront_backend_changed),
1327 static driver_t blkfront_driver = {
1330 sizeof(struct xb_softc),
1332 devclass_t blkfront_devclass;
1334 DRIVER_MODULE(xbd, xenbusb_front, blkfront_driver, blkfront_devclass, 0, 0);