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>
44 #include <sys/sysctl.h>
46 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/vmparam.h>
51 #include <sys/bus_dma.h>
53 #include <machine/_inttypes.h>
54 #include <machine/xen/xen-os.h>
55 #include <machine/xen/xenvar.h>
56 #include <machine/xen/xenfunc.h>
58 #include <xen/hypervisor.h>
59 #include <xen/xen_intr.h>
60 #include <xen/evtchn.h>
61 #include <xen/gnttab.h>
62 #include <xen/interface/grant_table.h>
63 #include <xen/interface/io/protocols.h>
64 #include <xen/xenbus/xenbusvar.h>
66 #include <geom/geom_disk.h>
68 #include <dev/xen/blkfront/block.h>
70 #include "xenbus_if.h"
73 static void xb_free_command(struct xb_command *cm);
74 static void xb_startio(struct xb_softc *sc);
75 static void blkfront_connect(struct xb_softc *);
76 static void blkfront_closing(device_t);
77 static int blkfront_detach(device_t);
78 static int setup_blkring(struct xb_softc *);
79 static void blkif_int(void *);
80 static void blkfront_initialize(struct xb_softc *);
81 static int blkif_completion(struct xb_command *);
82 static void blkif_free(struct xb_softc *);
83 static void blkif_queue_cb(void *, bus_dma_segment_t *, int, int);
85 static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
87 #define GRANT_INVALID_REF 0
89 /* Control whether runtime update of vbds is enabled. */
90 #define ENABLE_VBD_UPDATE 0
93 static void vbd_update(void);
96 #define BLKIF_STATE_DISCONNECTED 0
97 #define BLKIF_STATE_CONNECTED 1
98 #define BLKIF_STATE_SUSPENDED 2
101 static char *blkif_state_name[] = {
102 [BLKIF_STATE_DISCONNECTED] = "disconnected",
103 [BLKIF_STATE_CONNECTED] = "connected",
104 [BLKIF_STATE_SUSPENDED] = "closed",
107 static char * blkif_status_name[] = {
108 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
109 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
110 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
111 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
116 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
118 #define DPRINTK(fmt, args...)
121 static int blkif_open(struct disk *dp);
122 static int blkif_close(struct disk *dp);
123 static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
124 static int blkif_queue_request(struct xb_softc *sc, struct xb_command *cm);
125 static void xb_strategy(struct bio *bp);
127 // In order to quiesce the device during kernel dumps, outstanding requests to
128 // DOM0 for disk reads/writes need to be accounted for.
129 static int xb_dump(void *, void *, vm_offset_t, off_t, size_t);
131 /* XXX move to xb_vbd.c when VBD update support is added */
134 #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
135 #define XBD_SECTOR_SHFT 9
138 * Translate Linux major/minor to an appropriate name and unit
139 * number. For HVM guests, this allows us to use the same drive names
140 * with blkfront as the emulated drives, easing transition slightly.
143 blkfront_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
145 static struct vdev_info {
151 {3, 6, 0, "ada"}, /* ide0 */
152 {22, 6, 2, "ada"}, /* ide1 */
153 {33, 6, 4, "ada"}, /* ide2 */
154 {34, 6, 6, "ada"}, /* ide3 */
155 {56, 6, 8, "ada"}, /* ide4 */
156 {57, 6, 10, "ada"}, /* ide5 */
157 {88, 6, 12, "ada"}, /* ide6 */
158 {89, 6, 14, "ada"}, /* ide7 */
159 {90, 6, 16, "ada"}, /* ide8 */
160 {91, 6, 18, "ada"}, /* ide9 */
162 {8, 4, 0, "da"}, /* scsi disk0 */
163 {65, 4, 16, "da"}, /* scsi disk1 */
164 {66, 4, 32, "da"}, /* scsi disk2 */
165 {67, 4, 48, "da"}, /* scsi disk3 */
166 {68, 4, 64, "da"}, /* scsi disk4 */
167 {69, 4, 80, "da"}, /* scsi disk5 */
168 {70, 4, 96, "da"}, /* scsi disk6 */
169 {71, 4, 112, "da"}, /* scsi disk7 */
170 {128, 4, 128, "da"}, /* scsi disk8 */
171 {129, 4, 144, "da"}, /* scsi disk9 */
172 {130, 4, 160, "da"}, /* scsi disk10 */
173 {131, 4, 176, "da"}, /* scsi disk11 */
174 {132, 4, 192, "da"}, /* scsi disk12 */
175 {133, 4, 208, "da"}, /* scsi disk13 */
176 {134, 4, 224, "da"}, /* scsi disk14 */
177 {135, 4, 240, "da"}, /* scsi disk15 */
179 {202, 4, 0, "xbd"}, /* xbd */
183 int major = vdevice >> 8;
184 int minor = vdevice & 0xff;
187 if (vdevice & (1 << 28)) {
188 *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);
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 xb_setup_sysctl(struct xb_softc *xb)
415 struct sysctl_ctx_list *sysctl_ctx = NULL;
416 struct sysctl_oid *sysctl_tree = NULL;
418 sysctl_ctx = device_get_sysctl_ctx(xb->xb_dev);
419 if (sysctl_ctx == NULL)
422 sysctl_tree = device_get_sysctl_tree(xb->xb_dev);
423 if (sysctl_tree == NULL)
426 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
427 "max_requests", CTLFLAG_RD, &xb->max_requests, -1,
428 "maximum outstanding requests (negotiated)");
430 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
431 "max_request_segments", CTLFLAG_RD,
432 &xb->max_request_segments, 0,
433 "maximum number of pages per requests (negotiated)");
435 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
436 "max_request_size", CTLFLAG_RD,
437 &xb->max_request_size, 0,
438 "maximum size in bytes of a request (negotiated)");
440 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
441 "ring_pages", CTLFLAG_RD,
443 "communication channel pages (negotiated)");
447 * Setup supplies the backend dir, virtual device. We place an event
448 * channel and shared frame entries. We watch backend to wait if it's
452 blkfront_attach(device_t dev)
461 /* FIXME: Use dynamic device id if this is not set. */
462 error = xs_scanf(XST_NIL, xenbus_get_node(dev),
463 "virtual-device", NULL, "%" PRIu32, &vdevice);
465 xenbus_dev_fatal(dev, error, "reading virtual-device");
466 device_printf(dev, "Couldn't determine virtual device.\n");
470 blkfront_vdevice_to_unit(vdevice, &unit, &name);
471 if (!strcmp(name, "xbd"))
472 device_set_unit(dev, unit);
474 sc = device_get_softc(dev);
475 mtx_init(&sc->xb_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
479 xb_initq_complete(sc);
481 for (i = 0; i < XBF_MAX_RING_PAGES; i++)
482 sc->ring_ref[i] = GRANT_INVALID_REF;
485 sc->vdevice = vdevice;
486 sc->connected = BLKIF_STATE_DISCONNECTED;
490 /* Wait for backend device to publish its protocol capabilities. */
491 xenbus_set_state(dev, XenbusStateInitialising);
497 blkfront_suspend(device_t dev)
499 struct xb_softc *sc = device_get_softc(dev);
503 /* Prevent new requests being issued until we fix things up. */
504 mtx_lock(&sc->xb_io_lock);
505 saved_state = sc->connected;
506 sc->connected = BLKIF_STATE_SUSPENDED;
508 /* Wait for outstanding I/O to drain. */
510 while (TAILQ_EMPTY(&sc->cm_busy) == 0) {
511 if (msleep(&sc->cm_busy, &sc->xb_io_lock,
512 PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
517 mtx_unlock(&sc->xb_io_lock);
520 sc->connected = saved_state;
526 blkfront_resume(device_t dev)
528 struct xb_softc *sc = device_get_softc(dev);
530 DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev));
533 blkfront_initialize(sc);
538 blkfront_initialize(struct xb_softc *sc)
540 const char *otherend_path;
541 const char *node_path;
542 uint32_t max_ring_page_order;
546 if (xenbus_get_state(sc->xb_dev) != XenbusStateInitialising) {
547 /* Initialization has already been performed. */
552 * Protocol defaults valid even if negotiation for a
555 max_ring_page_order = 0;
557 sc->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
558 sc->max_request_size = XBF_SEGS_TO_SIZE(sc->max_request_segments);
559 sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments);
562 * Protocol negotiation.
564 * \note xs_gather() returns on the first encountered error, so
565 * we must use independant calls in order to guarantee
566 * we don't miss information in a sparsly populated back-end
569 * \note xs_scanf() does not update variables for unmatched
572 otherend_path = xenbus_get_otherend_path(sc->xb_dev);
573 node_path = xenbus_get_node(sc->xb_dev);
575 /* Support both backend schemes for relaying ring page limits. */
576 (void)xs_scanf(XST_NIL, otherend_path,
577 "max-ring-page-order", NULL, "%" PRIu32,
578 &max_ring_page_order);
579 sc->ring_pages = 1 << max_ring_page_order;
580 (void)xs_scanf(XST_NIL, otherend_path,
581 "max-ring-pages", NULL, "%" PRIu32,
583 if (sc->ring_pages < 1)
586 sc->max_requests = BLKIF_MAX_RING_REQUESTS(sc->ring_pages * PAGE_SIZE);
587 (void)xs_scanf(XST_NIL, otherend_path,
588 "max-requests", NULL, "%" PRIu32,
591 (void)xs_scanf(XST_NIL, otherend_path,
592 "max-request-segments", NULL, "%" PRIu32,
593 &sc->max_request_segments);
595 (void)xs_scanf(XST_NIL, otherend_path,
596 "max-request-size", NULL, "%" PRIu32,
597 &sc->max_request_size);
599 if (sc->ring_pages > XBF_MAX_RING_PAGES) {
600 device_printf(sc->xb_dev, "Back-end specified ring-pages of "
601 "%u limited to front-end limit of %zu.\n",
602 sc->ring_pages, XBF_MAX_RING_PAGES);
603 sc->ring_pages = XBF_MAX_RING_PAGES;
606 if (powerof2(sc->ring_pages) == 0) {
607 uint32_t new_page_limit;
609 new_page_limit = 0x01 << (fls(sc->ring_pages) - 1);
610 device_printf(sc->xb_dev, "Back-end specified ring-pages of "
611 "%u is not a power of 2. Limited to %u.\n",
612 sc->ring_pages, new_page_limit);
613 sc->ring_pages = new_page_limit;
616 if (sc->max_requests > XBF_MAX_REQUESTS) {
617 device_printf(sc->xb_dev, "Back-end specified max_requests of "
618 "%u limited to front-end limit of %u.\n",
619 sc->max_requests, XBF_MAX_REQUESTS);
620 sc->max_requests = XBF_MAX_REQUESTS;
623 if (sc->max_request_segments > XBF_MAX_SEGMENTS_PER_REQUEST) {
624 device_printf(sc->xb_dev, "Back-end specified "
625 "max_request_segments of %u limited to "
626 "front-end limit of %u.\n",
627 sc->max_request_segments,
628 XBF_MAX_SEGMENTS_PER_REQUEST);
629 sc->max_request_segments = XBF_MAX_SEGMENTS_PER_REQUEST;
632 if (sc->max_request_size > XBF_MAX_REQUEST_SIZE) {
633 device_printf(sc->xb_dev, "Back-end specified "
634 "max_request_size of %u limited to front-end "
635 "limit of %u.\n", sc->max_request_size,
636 XBF_MAX_REQUEST_SIZE);
637 sc->max_request_size = XBF_MAX_REQUEST_SIZE;
640 if (sc->max_request_size > XBF_SEGS_TO_SIZE(sc->max_request_segments)) {
641 device_printf(sc->xb_dev, "Back-end specified "
642 "max_request_size of %u limited to front-end "
643 "limit of %u. (Too few segments.)\n",
644 sc->max_request_size,
645 XBF_SEGS_TO_SIZE(sc->max_request_segments));
646 sc->max_request_size =
647 XBF_SEGS_TO_SIZE(sc->max_request_segments);
650 sc->max_request_blocks = BLKIF_SEGS_TO_BLOCKS(sc->max_request_segments);
652 /* Allocate datastructures based on negotiated values. */
653 error = bus_dma_tag_create(bus_get_dma_tag(sc->xb_dev), /* parent */
654 512, PAGE_SIZE, /* algnmnt, boundary */
655 BUS_SPACE_MAXADDR, /* lowaddr */
656 BUS_SPACE_MAXADDR, /* highaddr */
657 NULL, NULL, /* filter, filterarg */
658 sc->max_request_size,
659 sc->max_request_segments,
660 PAGE_SIZE, /* maxsegsize */
661 BUS_DMA_ALLOCNOW, /* flags */
662 busdma_lock_mutex, /* lockfunc */
663 &sc->xb_io_lock, /* lockarg */
666 xenbus_dev_fatal(sc->xb_dev, error,
667 "Cannot allocate parent DMA tag\n");
671 /* Per-transaction data allocation. */
672 sc->shadow = malloc(sizeof(*sc->shadow) * sc->max_requests,
673 M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
674 if (sc->shadow == NULL) {
675 bus_dma_tag_destroy(sc->xb_io_dmat);
676 xenbus_dev_fatal(sc->xb_dev, error,
677 "Cannot allocate request structures\n");
681 for (i = 0; i < sc->max_requests; i++) {
682 struct xb_command *cm;
685 cm->sg_refs = malloc(sizeof(grant_ref_t)
686 * sc->max_request_segments,
687 M_XENBLOCKFRONT, M_NOWAIT);
688 if (cm->sg_refs == NULL)
692 if (bus_dmamap_create(sc->xb_io_dmat, 0, &cm->map) != 0)
697 if (setup_blkring(sc) != 0)
700 /* Support both backend schemes for relaying ring page limits. */
701 if (sc->ring_pages > 1) {
702 error = xs_printf(XST_NIL, node_path,
703 "num-ring-pages","%u", sc->ring_pages);
705 xenbus_dev_fatal(sc->xb_dev, error,
706 "writing %s/num-ring-pages",
711 error = xs_printf(XST_NIL, node_path,
712 "ring-page-order", "%u",
713 fls(sc->ring_pages) - 1);
715 xenbus_dev_fatal(sc->xb_dev, error,
716 "writing %s/ring-page-order",
722 error = xs_printf(XST_NIL, node_path,
723 "max-requests","%u", sc->max_requests);
725 xenbus_dev_fatal(sc->xb_dev, error,
726 "writing %s/max-requests",
731 error = xs_printf(XST_NIL, node_path,
732 "max-request-segments","%u", sc->max_request_segments);
734 xenbus_dev_fatal(sc->xb_dev, error,
735 "writing %s/max-request-segments",
740 error = xs_printf(XST_NIL, node_path,
741 "max-request-size","%u", sc->max_request_size);
743 xenbus_dev_fatal(sc->xb_dev, error,
744 "writing %s/max-request-size",
749 error = xs_printf(XST_NIL, node_path, "event-channel",
750 "%u", irq_to_evtchn_port(sc->irq));
752 xenbus_dev_fatal(sc->xb_dev, error,
753 "writing %s/event-channel",
758 error = xs_printf(XST_NIL, node_path,
759 "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE);
761 xenbus_dev_fatal(sc->xb_dev, error,
762 "writing %s/protocol",
767 xenbus_set_state(sc->xb_dev, XenbusStateInitialised);
771 setup_blkring(struct xb_softc *sc)
773 blkif_sring_t *sring;
774 uintptr_t sring_page_addr;
778 sring = malloc(sc->ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
781 xenbus_dev_fatal(sc->xb_dev, ENOMEM, "allocating shared ring");
784 SHARED_RING_INIT(sring);
785 FRONT_RING_INIT(&sc->ring, sring, sc->ring_pages * PAGE_SIZE);
787 for (i = 0, sring_page_addr = (uintptr_t)sring;
789 i++, sring_page_addr += PAGE_SIZE) {
791 error = xenbus_grant_ring(sc->xb_dev,
792 (vtomach(sring_page_addr) >> PAGE_SHIFT), &sc->ring_ref[i]);
794 xenbus_dev_fatal(sc->xb_dev, error,
795 "granting ring_ref(%d)", i);
799 if (sc->ring_pages == 1) {
800 error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev),
801 "ring-ref", "%u", sc->ring_ref[0]);
803 xenbus_dev_fatal(sc->xb_dev, error,
804 "writing %s/ring-ref",
805 xenbus_get_node(sc->xb_dev));
809 for (i = 0; i < sc->ring_pages; i++) {
810 char ring_ref_name[]= "ring_refXX";
812 snprintf(ring_ref_name, sizeof(ring_ref_name),
814 error = xs_printf(XST_NIL, xenbus_get_node(sc->xb_dev),
815 ring_ref_name, "%u", sc->ring_ref[i]);
817 xenbus_dev_fatal(sc->xb_dev, error,
819 xenbus_get_node(sc->xb_dev),
826 error = bind_listening_port_to_irqhandler(
827 xenbus_get_otherend_id(sc->xb_dev),
828 "xbd", (driver_intr_t *)blkif_int, sc,
829 INTR_TYPE_BIO | INTR_MPSAFE, &sc->irq);
831 xenbus_dev_fatal(sc->xb_dev, error,
832 "bind_evtchn_to_irqhandler failed");
840 * Callback received when the backend's state changes.
843 blkfront_backend_changed(device_t dev, XenbusState backend_state)
845 struct xb_softc *sc = device_get_softc(dev);
847 DPRINTK("backend_state=%d\n", backend_state);
849 switch (backend_state) {
850 case XenbusStateUnknown:
851 case XenbusStateInitialising:
852 case XenbusStateReconfigured:
853 case XenbusStateReconfiguring:
854 case XenbusStateClosed:
857 case XenbusStateInitWait:
858 case XenbusStateInitialised:
859 blkfront_initialize(sc);
862 case XenbusStateConnected:
863 blkfront_initialize(sc);
864 blkfront_connect(sc);
867 case XenbusStateClosing:
869 xenbus_dev_error(dev, -EBUSY,
870 "Device in use; refusing to close");
872 blkfront_closing(dev);
878 ** Invoked when the backend is finally 'ready' (and has published
879 ** the details about the physical device - #sectors, size, etc).
882 blkfront_connect(struct xb_softc *sc)
884 device_t dev = sc->xb_dev;
885 unsigned long sectors, sector_size;
887 int err, feature_barrier;
889 if( (sc->connected == BLKIF_STATE_CONNECTED) ||
890 (sc->connected == BLKIF_STATE_SUSPENDED) )
893 DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
895 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
896 "sectors", "%lu", §ors,
897 "info", "%u", &binfo,
898 "sector-size", "%lu", §or_size,
901 xenbus_dev_fatal(dev, err,
902 "reading backend fields at %s",
903 xenbus_get_otherend_path(dev));
906 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
907 "feature-barrier", "%lu", &feature_barrier,
909 if (!err || feature_barrier)
910 sc->xb_flags |= XB_BARRIER;
912 if (sc->xb_disk == NULL) {
913 device_printf(dev, "%juMB <%s> at %s",
914 (uintmax_t) sectors / (1048576 / sector_size),
915 device_get_desc(dev),
916 xenbus_get_node(dev));
917 bus_print_child_footer(device_get_parent(dev), dev);
919 xlvbd_add(sc, sectors, sc->vdevice, binfo, sector_size);
922 (void)xenbus_set_state(dev, XenbusStateConnected);
924 /* Kick pending requests. */
925 mtx_lock(&sc->xb_io_lock);
926 sc->connected = BLKIF_STATE_CONNECTED;
928 sc->xb_flags |= XB_READY;
929 mtx_unlock(&sc->xb_io_lock);
933 * Handle the change of state of the backend to Closing. We must delete our
934 * device-layer structures now, to ensure that writes are flushed through to
935 * the backend. Once this is done, we can switch to Closed in
939 blkfront_closing(device_t dev)
941 struct xb_softc *sc = device_get_softc(dev);
943 xenbus_set_state(dev, XenbusStateClosing);
945 DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev));
947 if (sc->xb_disk != NULL) {
948 disk_destroy(sc->xb_disk);
952 xenbus_set_state(dev, XenbusStateClosed);
957 blkfront_detach(device_t dev)
959 struct xb_softc *sc = device_get_softc(dev);
961 DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev));
964 mtx_destroy(&sc->xb_io_lock);
971 flush_requests(struct xb_softc *sc)
975 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->ring, notify);
978 notify_remote_via_irq(sc->irq);
982 blkif_restart_queue_callback(void *arg)
984 struct xb_softc *sc = arg;
986 mtx_lock(&sc->xb_io_lock);
990 mtx_unlock(&sc->xb_io_lock);
994 blkif_open(struct disk *dp)
996 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
999 printf("xb%d: not found", sc->xb_unit);
1003 sc->xb_flags |= XB_OPEN;
1009 blkif_close(struct disk *dp)
1011 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
1015 sc->xb_flags &= ~XB_OPEN;
1016 if (--(sc->users) == 0) {
1018 * Check whether we have been instructed to close. We will
1019 * have ignored this request initially, as the device was
1022 if (xenbus_get_otherend_state(sc->xb_dev) == XenbusStateClosing)
1023 blkfront_closing(sc->xb_dev);
1029 blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
1031 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
1040 xb_free_command(struct xb_command *cm)
1043 KASSERT((cm->cm_flags & XB_ON_XBQ_MASK) == 0,
1044 ("Freeing command that is still on a queue\n"));
1048 cm->cm_complete = NULL;
1049 xb_enqueue_free(cm);
1053 * blkif_queue_request
1057 * id: for guest use only.
1058 * operation: BLKIF_OP_{READ,WRITE,PROBE}
1059 * buffer: buffer to read/write into. this should be a
1060 * virtual address in the guest os.
1062 static struct xb_command *
1063 xb_bio_command(struct xb_softc *sc)
1065 struct xb_command *cm;
1068 if (unlikely(sc->connected != BLKIF_STATE_CONNECTED))
1071 bp = xb_dequeue_bio(sc);
1075 if ((cm = xb_dequeue_free(sc)) == NULL) {
1076 xb_requeue_bio(sc, bp);
1080 if (gnttab_alloc_grant_references(sc->max_request_segments,
1081 &cm->gref_head) != 0) {
1082 gnttab_request_free_callback(&sc->callback,
1083 blkif_restart_queue_callback, sc,
1084 sc->max_request_segments);
1085 xb_requeue_bio(sc, bp);
1086 xb_enqueue_free(cm);
1087 sc->xb_flags |= XB_FROZEN;
1092 cm->data = bp->bio_data;
1093 cm->datalen = bp->bio_bcount;
1094 cm->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ :
1096 cm->sector_number = (blkif_sector_t)bp->bio_pblkno;
1102 blkif_queue_request(struct xb_softc *sc, struct xb_command *cm)
1106 error = bus_dmamap_load(sc->xb_io_dmat, cm->map, cm->data, cm->datalen,
1107 blkif_queue_cb, cm, 0);
1108 if (error == EINPROGRESS) {
1109 printf("EINPROGRESS\n");
1110 sc->xb_flags |= XB_FROZEN;
1111 cm->cm_flags |= XB_CMD_FROZEN;
1119 blkif_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1121 struct xb_softc *sc;
1122 struct xb_command *cm;
1123 blkif_request_t *ring_req;
1124 struct blkif_request_segment *sg;
1125 struct blkif_request_segment *last_block_sg;
1126 grant_ref_t *sg_ref;
1127 vm_paddr_t buffer_ma;
1128 uint64_t fsect, lsect;
1136 //printf("%s: Start\n", __func__);
1138 printf("error %d in blkif_queue_cb\n", error);
1139 cm->bp->bio_error = EIO;
1141 xb_free_command(cm);
1145 /* Fill out a communications ring structure. */
1146 ring_req = RING_GET_REQUEST(&sc->ring, sc->ring.req_prod_pvt);
1147 sc->ring.req_prod_pvt++;
1148 ring_req->id = cm->id;
1149 ring_req->operation = cm->operation;
1150 ring_req->sector_number = cm->sector_number;
1151 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk;
1152 ring_req->nr_segments = nsegs;
1155 block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
1157 last_block_sg = sg + block_segs;
1158 sg_ref = cm->sg_refs;
1162 while (sg < last_block_sg) {
1163 buffer_ma = segs->ds_addr;
1164 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
1165 lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1;
1167 KASSERT(lsect <= 7, ("XEN disk driver data cannot "
1168 "cross a page boundary"));
1170 /* install a grant reference. */
1171 ref = gnttab_claim_grant_reference(&cm->gref_head);
1174 * GNTTAB_LIST_END == 0xffffffff, but it is private
1177 KASSERT(ref != ~0, ("grant_reference failed"));
1179 gnttab_grant_foreign_access_ref(
1181 xenbus_get_otherend_id(sc->xb_dev),
1182 buffer_ma >> PAGE_SHIFT,
1183 ring_req->operation == BLKIF_OP_WRITE);
1186 *sg = (struct blkif_request_segment) {
1188 .first_sect = fsect,
1189 .last_sect = lsect };
1195 block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
1196 if (block_segs == 0)
1199 sg = BLKRING_GET_SEG_BLOCK(&sc->ring, sc->ring.req_prod_pvt);
1200 sc->ring.req_prod_pvt++;
1201 last_block_sg = sg + block_segs;
1204 if (cm->operation == BLKIF_OP_READ)
1205 op = BUS_DMASYNC_PREREAD;
1206 else if (cm->operation == BLKIF_OP_WRITE)
1207 op = BUS_DMASYNC_PREWRITE;
1210 bus_dmamap_sync(sc->xb_io_dmat, cm->map, op);
1212 gnttab_free_grant_references(cm->gref_head);
1214 xb_enqueue_busy(cm);
1217 * This flag means that we're probably executing in the busdma swi
1218 * instead of in the startio context, so an explicit flush is needed.
1220 if (cm->cm_flags & XB_CMD_FROZEN)
1223 //printf("%s: Done\n", __func__);
1228 * Dequeue buffers and place them in the shared communication ring.
1229 * Return when no more requests can be accepted or all buffers have
1232 * Signal XEN once the ring has been filled out.
1235 xb_startio(struct xb_softc *sc)
1237 struct xb_command *cm;
1238 int error, queued = 0;
1240 mtx_assert(&sc->xb_io_lock, MA_OWNED);
1242 if (sc->connected != BLKIF_STATE_CONNECTED)
1245 while (RING_FREE_REQUESTS(&sc->ring) >= sc->max_request_blocks) {
1246 if (sc->xb_flags & XB_FROZEN)
1249 cm = xb_dequeue_ready(sc);
1252 cm = xb_bio_command(sc);
1257 if ((error = blkif_queue_request(sc, cm)) != 0) {
1258 printf("blkif_queue_request returned %d\n", error);
1269 blkif_int(void *xsc)
1271 struct xb_softc *sc = xsc;
1272 struct xb_command *cm;
1273 blkif_response_t *bret;
1277 mtx_lock(&sc->xb_io_lock);
1279 if (unlikely(sc->connected == BLKIF_STATE_DISCONNECTED)) {
1280 mtx_unlock(&sc->xb_io_lock);
1285 rp = sc->ring.sring->rsp_prod;
1286 rmb(); /* Ensure we see queued responses up to 'rp'. */
1288 for (i = sc->ring.rsp_cons; i != rp;) {
1289 bret = RING_GET_RESPONSE(&sc->ring, i);
1290 cm = &sc->shadow[bret->id];
1293 i += blkif_completion(cm);
1295 if (cm->operation == BLKIF_OP_READ)
1296 op = BUS_DMASYNC_POSTREAD;
1297 else if (cm->operation == BLKIF_OP_WRITE)
1298 op = BUS_DMASYNC_POSTWRITE;
1301 bus_dmamap_sync(sc->xb_io_dmat, cm->map, op);
1302 bus_dmamap_unload(sc->xb_io_dmat, cm->map);
1305 * If commands are completing then resources are probably
1306 * being freed as well. It's a cheap assumption even when
1309 sc->xb_flags &= ~XB_FROZEN;
1312 * Directly call the i/o complete routine to save an
1313 * an indirection in the common case.
1315 cm->status = bret->status;
1317 xb_bio_complete(sc, cm);
1318 else if (cm->cm_complete)
1319 (cm->cm_complete)(cm);
1321 xb_free_command(cm);
1324 sc->ring.rsp_cons = i;
1326 if (i != sc->ring.req_prod_pvt) {
1328 RING_FINAL_CHECK_FOR_RESPONSES(&sc->ring, more_to_do);
1332 sc->ring.sring->rsp_event = i + 1;
1337 if (unlikely(sc->connected == BLKIF_STATE_SUSPENDED))
1338 wakeup(&sc->cm_busy);
1340 mtx_unlock(&sc->xb_io_lock);
1344 blkif_free(struct xb_softc *sc)
1346 uint8_t *sring_page_ptr;
1349 /* Prevent new requests being issued until we fix things up. */
1350 mtx_lock(&sc->xb_io_lock);
1351 sc->connected = BLKIF_STATE_DISCONNECTED;
1352 mtx_unlock(&sc->xb_io_lock);
1354 /* Free resources associated with old device channel. */
1355 if (sc->ring.sring != NULL) {
1356 sring_page_ptr = (uint8_t *)sc->ring.sring;
1357 for (i = 0; i < sc->ring_pages; i++) {
1358 if (sc->ring_ref[i] != GRANT_INVALID_REF) {
1359 gnttab_end_foreign_access_ref(sc->ring_ref[i]);
1360 sc->ring_ref[i] = GRANT_INVALID_REF;
1362 sring_page_ptr += PAGE_SIZE;
1364 free(sc->ring.sring, M_XENBLOCKFRONT);
1365 sc->ring.sring = NULL;
1370 for (i = 0; i < sc->max_requests; i++) {
1371 struct xb_command *cm;
1373 cm = &sc->shadow[i];
1374 if (cm->sg_refs != NULL) {
1375 free(cm->sg_refs, M_XENBLOCKFRONT);
1379 bus_dmamap_destroy(sc->xb_io_dmat, cm->map);
1381 free(sc->shadow, M_XENBLOCKFRONT);
1384 bus_dma_tag_destroy(sc->xb_io_dmat);
1388 xb_initq_complete(sc);
1392 unbind_from_irqhandler(sc->irq);
1398 blkif_completion(struct xb_command *s)
1400 //printf("%s: Req %p(%d)\n", __func__, s, s->nseg);
1401 gnttab_end_foreign_access_references(s->nseg, s->sg_refs);
1402 return (BLKIF_SEGS_TO_BLOCKS(s->nseg));
1405 /* ** Driver registration ** */
1406 static device_method_t blkfront_methods[] = {
1407 /* Device interface */
1408 DEVMETHOD(device_probe, blkfront_probe),
1409 DEVMETHOD(device_attach, blkfront_attach),
1410 DEVMETHOD(device_detach, blkfront_detach),
1411 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1412 DEVMETHOD(device_suspend, blkfront_suspend),
1413 DEVMETHOD(device_resume, blkfront_resume),
1415 /* Xenbus interface */
1416 DEVMETHOD(xenbus_otherend_changed, blkfront_backend_changed),
1421 static driver_t blkfront_driver = {
1424 sizeof(struct xb_softc),
1426 devclass_t blkfront_devclass;
1428 DRIVER_MODULE(xbd, xenbusb_front, blkfront_driver, blkfront_devclass, 0, 0);