4 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
5 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
8 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 * XenoBSD block device driver
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/malloc.h>
28 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <machine/intr_machdep.h>
41 #include <machine/vmparam.h>
43 #include <machine/xen/xen-os.h>
44 #include <machine/xen/xenfunc.h>
45 #include <xen/hypervisor.h>
46 #include <xen/xen_intr.h>
47 #include <xen/evtchn.h>
48 #include <xen/gnttab.h>
49 #include <xen/interface/grant_table.h>
50 #include <xen/interface/io/protocols.h>
51 #include <xen/xenbus/xenbusvar.h>
53 #include <geom/geom_disk.h>
55 #include <dev/xen/blkfront/block.h>
57 #include "xenbus_if.h"
59 #define ASSERT(S) KASSERT(S, (#S))
62 static void xb_startio(struct xb_softc *sc);
63 static void connect(device_t, struct blkfront_info *);
64 static void blkfront_closing(device_t);
65 static int blkfront_detach(device_t);
66 static int talk_to_backend(device_t, struct blkfront_info *);
67 static int setup_blkring(device_t, struct blkfront_info *);
68 static void blkif_int(void *);
70 static void blkif_restart_queue(void *arg);
72 static void blkif_recover(struct blkfront_info *);
73 static void blkif_completion(struct blk_shadow *);
74 static void blkif_free(struct blkfront_info *, int);
76 #define GRANT_INVALID_REF 0
77 #define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
79 LIST_HEAD(xb_softc_list_head, xb_softc) xbsl_head;
81 /* Control whether runtime update of vbds is enabled. */
82 #define ENABLE_VBD_UPDATE 0
85 static void vbd_update(void);
89 #define BLKIF_STATE_DISCONNECTED 0
90 #define BLKIF_STATE_CONNECTED 1
91 #define BLKIF_STATE_SUSPENDED 2
94 static char *blkif_state_name[] = {
95 [BLKIF_STATE_DISCONNECTED] = "disconnected",
96 [BLKIF_STATE_CONNECTED] = "connected",
97 [BLKIF_STATE_SUSPENDED] = "closed",
100 static char * blkif_status_name[] = {
101 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
102 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
103 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
104 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
107 #define WPRINTK(fmt, args...) printf("[XEN] " fmt, ##args)
109 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
111 #define DPRINTK(fmt, args...)
114 static grant_ref_t gref_head;
115 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
116 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
118 static void kick_pending_request_queues(struct blkfront_info *);
119 static int blkif_open(struct disk *dp);
120 static int blkif_close(struct disk *dp);
121 static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
122 static int blkif_queue_request(struct bio *bp);
123 static void xb_strategy(struct bio *bp);
127 /* XXX move to xb_vbd.c when VBD update support is added */
130 #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
131 #define XBD_SECTOR_SHFT 9
133 static struct mtx blkif_io_lock;
136 pfn_to_mfn(vm_paddr_t pfn)
138 return (phystomach(pfn << PAGE_SHIFT) >> PAGE_SHIFT);
142 * Translate Linux major/minor to an appropriate name and unit
143 * number. For HVM guests, this allows us to use the same drive names
144 * with blkfront as the emulated drives, easing transition slightly.
147 blkfront_vdevice_to_unit(int vdevice, int *unit, const char **name)
149 static struct vdev_info {
155 {3, 6, 0, "ad"}, /* ide0 */
156 {22, 6, 2, "ad"}, /* ide1 */
157 {33, 6, 4, "ad"}, /* ide2 */
158 {34, 6, 6, "ad"}, /* ide3 */
159 {56, 6, 8, "ad"}, /* ide4 */
160 {57, 6, 10, "ad"}, /* ide5 */
161 {88, 6, 12, "ad"}, /* ide6 */
162 {89, 6, 14, "ad"}, /* ide7 */
163 {90, 6, 16, "ad"}, /* ide8 */
164 {91, 6, 18, "ad"}, /* ide9 */
166 {8, 4, 0, "da"}, /* scsi disk0 */
167 {65, 4, 16, "da"}, /* scsi disk1 */
168 {66, 4, 32, "da"}, /* scsi disk2 */
169 {67, 4, 48, "da"}, /* scsi disk3 */
170 {68, 4, 64, "da"}, /* scsi disk4 */
171 {69, 4, 80, "da"}, /* scsi disk5 */
172 {70, 4, 96, "da"}, /* scsi disk6 */
173 {71, 4, 112, "da"}, /* scsi disk7 */
174 {128, 4, 128, "da"}, /* scsi disk8 */
175 {129, 4, 144, "da"}, /* scsi disk9 */
176 {130, 4, 160, "da"}, /* scsi disk10 */
177 {131, 4, 176, "da"}, /* scsi disk11 */
178 {132, 4, 192, "da"}, /* scsi disk12 */
179 {133, 4, 208, "da"}, /* scsi disk13 */
180 {134, 4, 224, "da"}, /* scsi disk14 */
181 {135, 4, 240, "da"}, /* scsi disk15 */
183 {202, 4, 0, "xbd"}, /* xbd */
187 int major = vdevice >> 8;
188 int minor = vdevice & 0xff;
191 if (vdevice & (1 << 28)) {
192 *unit = (vdevice & ((1 << 28) - 1)) >> 8;
196 for (i = 0; info[i].major; i++) {
197 if (info[i].major == major) {
198 *unit = info[i].base + (minor >> info[i].shift);
199 *name = info[i].name;
209 xlvbd_add(device_t dev, blkif_sector_t capacity,
210 int vdevice, uint16_t vdisk_info, uint16_t sector_size,
211 struct blkfront_info *info)
217 blkfront_vdevice_to_unit(vdevice, &unit, &name);
219 sc = (struct xb_softc *)malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
224 if (strcmp(name, "xbd"))
225 device_printf(dev, "attaching as %s%d\n", name, unit);
227 memset(&sc->xb_disk, 0, sizeof(sc->xb_disk));
228 sc->xb_disk = disk_alloc();
229 sc->xb_disk->d_unit = sc->xb_unit;
230 sc->xb_disk->d_open = blkif_open;
231 sc->xb_disk->d_close = blkif_close;
232 sc->xb_disk->d_ioctl = blkif_ioctl;
233 sc->xb_disk->d_strategy = xb_strategy;
234 sc->xb_disk->d_name = name;
235 sc->xb_disk->d_drv1 = sc;
236 sc->xb_disk->d_sectorsize = sector_size;
239 sc->xb_disk->d_mediasize = capacity << XBD_SECTOR_SHFT;
241 sc->xb_disk->d_maxsize = DFLTPHYS;
242 #else /* XXX: xen can't handle large single i/o requests */
243 sc->xb_disk->d_maxsize = 4096;
246 XENPRINTF("attaching device 0x%x unit %d capacity %llu\n",
247 xb_diskinfo[sc->xb_unit].device, sc->xb_unit,
248 sc->xb_disk->d_mediasize);
250 sc->xb_disk->d_flags = 0;
251 disk_create(sc->xb_disk, DISK_VERSION_00);
252 bioq_init(&sc->xb_bioq);
258 xlvbd_del(struct blkfront_info *info)
263 disk_destroy(sc->xb_disk);
265 /************************ end VBD support *****************/
268 * Read/write routine for a buffer. Finds the proper unit, place it on
269 * the sortq and kick the controller.
272 xb_strategy(struct bio *bp)
274 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
278 bp->bio_error = EINVAL;
279 bp->bio_flags |= BIO_ERROR;
286 * Place it in the queue of disk activities for this disk
288 mtx_lock(&blkif_io_lock);
289 bioq_disksort(&sc->xb_bioq, bp);
292 mtx_unlock(&blkif_io_lock);
297 * Correctly set the bio to indicate a failed tranfer.
299 bp->bio_resid = bp->bio_bcount;
305 blkfront_probe(device_t dev)
308 if (!strcmp(xenbus_get_type(dev), "vbd")) {
309 device_set_desc(dev, "Virtual Block Device");
318 * Setup supplies the backend dir, virtual device. We place an event
319 * channel and shared frame entries. We watch backend to wait if it's
323 blkfront_attach(device_t dev)
325 int error, vdevice, i, unit;
326 struct blkfront_info *info;
329 /* FIXME: Use dynamic device id if this is not set. */
330 error = xenbus_scanf(XBT_NIL, xenbus_get_node(dev),
331 "virtual-device", NULL, "%i", &vdevice);
333 xenbus_dev_fatal(dev, error, "reading virtual-device");
334 printf("couldn't find virtual device");
338 blkfront_vdevice_to_unit(vdevice, &unit, &name);
339 if (!strcmp(name, "xbd"))
340 device_set_unit(dev, unit);
342 info = device_get_softc(dev);
347 for (i = 0; i < sizeof(*info); i++)
348 if (((uint8_t *)info)[i] != 0)
349 panic("non-null memory");
351 info->shadow_free = 0;
353 info->vdevice = vdevice;
354 info->connected = BLKIF_STATE_DISCONNECTED;
356 /* work queue needed ? */
357 for (i = 0; i < BLK_RING_SIZE; i++)
358 info->shadow[i].req.id = i+1;
359 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
361 /* Front end dir is a number, which is used as the id. */
362 info->handle = strtoul(strrchr(xenbus_get_node(dev),'/')+1, NULL, 0);
364 error = talk_to_backend(dev, info);
372 blkfront_suspend(device_t dev)
374 struct blkfront_info *info = device_get_softc(dev);
376 /* Prevent new requests being issued until we fix things up. */
377 mtx_lock(&blkif_io_lock);
378 info->connected = BLKIF_STATE_SUSPENDED;
379 mtx_unlock(&blkif_io_lock);
385 blkfront_resume(device_t dev)
387 struct blkfront_info *info = device_get_softc(dev);
390 DPRINTK("blkfront_resume: %s\n", xenbus_get_node(dev));
393 err = talk_to_backend(dev, info);
394 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
400 /* Common code used when first setting up, and when resuming. */
402 talk_to_backend(device_t dev, struct blkfront_info *info)
404 const char *message = NULL;
405 struct xenbus_transaction xbt;
408 /* Create shared ring, alloc event channel. */
409 err = setup_blkring(dev, info);
414 err = xenbus_transaction_start(&xbt);
416 xenbus_dev_fatal(dev, err, "starting transaction");
417 goto destroy_blkring;
420 err = xenbus_printf(xbt, xenbus_get_node(dev),
421 "ring-ref","%u", info->ring_ref);
423 message = "writing ring-ref";
424 goto abort_transaction;
426 err = xenbus_printf(xbt, xenbus_get_node(dev),
427 "event-channel", "%u", irq_to_evtchn_port(info->irq));
429 message = "writing event-channel";
430 goto abort_transaction;
432 err = xenbus_printf(xbt, xenbus_get_node(dev),
433 "protocol", "%s", XEN_IO_PROTO_ABI_NATIVE);
435 message = "writing protocol";
436 goto abort_transaction;
439 err = xenbus_transaction_end(xbt, 0);
443 xenbus_dev_fatal(dev, err, "completing transaction");
444 goto destroy_blkring;
446 xenbus_set_state(dev, XenbusStateInitialised);
451 xenbus_transaction_end(xbt, 1);
453 xenbus_dev_fatal(dev, err, "%s", message);
461 setup_blkring(device_t dev, struct blkfront_info *info)
463 blkif_sring_t *sring;
466 info->ring_ref = GRANT_INVALID_REF;
468 sring = (blkif_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
470 xenbus_dev_fatal(dev, ENOMEM, "allocating shared ring");
473 SHARED_RING_INIT(sring);
474 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
476 error = xenbus_grant_ring(dev,
477 (vtomach(info->ring.sring) >> PAGE_SHIFT), &info->ring_ref);
479 free(sring, M_DEVBUF);
480 info->ring.sring = NULL;
484 error = bind_listening_port_to_irqhandler(xenbus_get_otherend_id(dev),
485 "xbd", (driver_intr_t *)blkif_int, info,
486 INTR_TYPE_BIO | INTR_MPSAFE, &info->irq);
488 xenbus_dev_fatal(dev, error,
489 "bind_evtchn_to_irqhandler failed");
501 * Callback received when the backend's state changes.
504 blkfront_backend_changed(device_t dev, XenbusState backend_state)
506 struct blkfront_info *info = device_get_softc(dev);
508 DPRINTK("backend_state=%d\n", backend_state);
510 switch (backend_state) {
511 case XenbusStateUnknown:
512 case XenbusStateInitialising:
513 case XenbusStateInitWait:
514 case XenbusStateInitialised:
515 case XenbusStateClosed:
516 case XenbusStateReconfigured:
517 case XenbusStateReconfiguring:
520 case XenbusStateConnected:
524 case XenbusStateClosing:
526 xenbus_dev_error(dev, -EBUSY,
527 "Device in use; refusing to close");
529 blkfront_closing(dev);
531 bd = bdget(info->dev);
533 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
537 xenbus_dev_error(dev, -EBUSY,
538 "Device in use; refusing to close");
540 blkfront_closing(dev);
550 ** Invoked when the backend is finally 'ready' (and has told produced
551 ** the details about the physical device - #sectors, size, etc).
554 connect(device_t dev, struct blkfront_info *info)
556 unsigned long sectors, sector_size;
560 if( (info->connected == BLKIF_STATE_CONNECTED) ||
561 (info->connected == BLKIF_STATE_SUSPENDED) )
564 DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
566 err = xenbus_gather(XBT_NIL, xenbus_get_otherend_path(dev),
567 "sectors", "%lu", §ors,
568 "info", "%u", &binfo,
569 "sector-size", "%lu", §or_size,
572 xenbus_dev_fatal(dev, err,
573 "reading backend fields at %s",
574 xenbus_get_otherend_path(dev));
577 err = xenbus_gather(XBT_NIL, xenbus_get_otherend_path(dev),
578 "feature-barrier", "%lu", &info->feature_barrier,
581 info->feature_barrier = 0;
583 device_printf(dev, "%juMB <%s> at %s",
584 (uintmax_t) sectors / (1048576 / sector_size),
585 device_get_desc(dev),
586 xenbus_get_node(dev));
587 bus_print_child_footer(device_get_parent(dev), dev);
589 xlvbd_add(dev, sectors, info->vdevice, binfo, sector_size, info);
591 (void)xenbus_set_state(dev, XenbusStateConnected);
593 /* Kick pending requests. */
594 mtx_lock(&blkif_io_lock);
595 info->connected = BLKIF_STATE_CONNECTED;
596 kick_pending_request_queues(info);
597 mtx_unlock(&blkif_io_lock);
606 * Handle the change of state of the backend to Closing. We must delete our
607 * device-layer structures now, to ensure that writes are flushed through to
608 * the backend. Once is this done, we can switch to Closed in
612 blkfront_closing(device_t dev)
614 struct blkfront_info *info = device_get_softc(dev);
616 DPRINTK("blkfront_closing: %s removed\n", xenbus_get_node(dev));
619 DPRINTK("Calling xlvbd_del\n");
624 xenbus_set_state(dev, XenbusStateClosed);
629 blkfront_detach(device_t dev)
631 struct blkfront_info *info = device_get_softc(dev);
633 DPRINTK("blkfront_remove: %s removed\n", xenbus_get_node(dev));
642 GET_ID_FROM_FREELIST(struct blkfront_info *info)
644 unsigned long nfree = info->shadow_free;
646 KASSERT(nfree <= BLK_RING_SIZE, ("free %lu > RING_SIZE", nfree));
647 info->shadow_free = info->shadow[nfree].req.id;
648 info->shadow[nfree].req.id = 0x0fffffee; /* debug */
653 ADD_ID_TO_FREELIST(struct blkfront_info *info, unsigned long id)
655 info->shadow[id].req.id = info->shadow_free;
656 info->shadow[id].request = 0;
657 info->shadow_free = id;
661 flush_requests(struct blkfront_info *info)
665 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
668 notify_remote_via_irq(info->irq);
672 kick_pending_request_queues(struct blkfront_info *info)
674 /* XXX check if we can't simplify */
676 if (!RING_FULL(&info->ring)) {
677 /* Re-enable calldowns. */
678 blk_start_queue(info->rq);
679 /* Kick things off immediately. */
680 do_blkif_request(info->rq);
683 if (!RING_FULL(&info->ring)) {
685 sc = LIST_FIRST(&xbsl_head);
686 LIST_REMOVE(sc, entry);
687 /* Re-enable calldowns. */
688 blk_start_queue(di->rq);
690 /* Kick things off immediately. */
691 xb_startio(info->sc);
697 static void blkif_restart_queue(void *arg)
699 struct blkfront_info *info = (struct blkfront_info *)arg;
701 mtx_lock(&blkif_io_lock);
702 kick_pending_request_queues(info);
703 mtx_unlock(&blkif_io_lock);
707 static void blkif_restart_queue_callback(void *arg)
710 struct blkfront_info *info = (struct blkfront_info *)arg;
711 /* XXX BSD equiv ? */
713 schedule_work(&info->work);
718 blkif_open(struct disk *dp)
720 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
723 printf("xb%d: not found", sc->xb_unit);
727 sc->xb_flags |= XB_OPEN;
728 sc->xb_info->users++;
733 blkif_close(struct disk *dp)
735 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
739 sc->xb_flags &= ~XB_OPEN;
740 if (--(sc->xb_info->users) == 0) {
741 /* Check whether we have been instructed to close. We will
742 have ignored this request initially, as the device was
744 device_t dev = sc->xb_info->xbdev;
746 xenbus_read_driver_state(xenbus_get_otherend_path(dev));
748 if (state == XenbusStateClosing)
749 blkfront_closing(dev);
755 blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
757 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
767 * blkif_queue_request
771 * id: for guest use only.
772 * operation: BLKIF_OP_{READ,WRITE,PROBE}
773 * buffer: buffer to read/write into. this should be a
774 * virtual address in the guest os.
776 static int blkif_queue_request(struct bio *bp)
779 vm_paddr_t buffer_ma;
780 blkif_request_t *ring_req;
782 uint64_t fsect, lsect;
783 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
784 struct blkfront_info *info = sc->xb_info;
787 if (unlikely(sc->xb_info->connected != BLKIF_STATE_CONNECTED))
790 if (gnttab_alloc_grant_references(
791 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
792 gnttab_request_free_callback(
794 blkif_restart_queue_callback,
796 BLKIF_MAX_SEGMENTS_PER_REQUEST);
800 /* Check if the buffer is properly aligned */
801 if ((vm_offset_t)bp->bio_data & PAGE_MASK) {
802 int align = (bp->bio_bcount < PAGE_SIZE/2) ? XBD_SECTOR_SIZE :
804 caddr_t newbuf = malloc(bp->bio_bcount + align, M_DEVBUF,
807 alignbuf = (char *)roundup2((u_long)newbuf, align);
809 /* save a copy of the current buffer */
810 bp->bio_driver1 = newbuf;
811 bp->bio_driver2 = alignbuf;
813 /* Copy the data for a write */
814 if (bp->bio_cmd == BIO_WRITE)
815 bcopy(bp->bio_data, alignbuf, bp->bio_bcount);
817 alignbuf = bp->bio_data;
819 /* Fill out a communications ring structure. */
820 ring_req = RING_GET_REQUEST(&info->ring,
821 info->ring.req_prod_pvt);
822 id = GET_ID_FROM_FREELIST(info);
823 info->shadow[id].request = (unsigned long)bp;
826 ring_req->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ :
829 ring_req->sector_number= (blkif_sector_t)bp->bio_pblkno;
830 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk;
832 ring_req->nr_segments = 0; /* XXX not doing scatter/gather since buffer
833 * chaining is not supported.
836 buffer_ma = vtomach(alignbuf);
837 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
838 lsect = fsect + (bp->bio_bcount >> XBD_SECTOR_SHFT) - 1;
839 /* install a grant reference. */
840 ref = gnttab_claim_grant_reference(&gref_head);
841 KASSERT( ref != -ENOSPC, ("grant_reference failed") );
843 gnttab_grant_foreign_access_ref(
845 xenbus_get_otherend_id(info->xbdev),
846 buffer_ma >> PAGE_SHIFT,
847 ring_req->operation & 1 ); /* ??? */
848 info->shadow[id].frame[ring_req->nr_segments] =
849 buffer_ma >> PAGE_SHIFT;
851 ring_req->seg[ring_req->nr_segments] =
852 (struct blkif_request_segment) {
855 .last_sect = lsect };
857 ring_req->nr_segments++;
858 KASSERT((buffer_ma & (XBD_SECTOR_SIZE-1)) == 0,
859 ("XEN buffer must be sector aligned"));
861 ("XEN disk driver data cannot cross a page boundary"));
863 buffer_ma &= ~PAGE_MASK;
865 info->ring.req_prod_pvt++;
867 /* Keep a private copy so we can reissue requests when recovering. */
868 info->shadow[id].req = *ring_req;
870 gnttab_free_grant_references(gref_head);
878 * Dequeue buffers and place them in the shared communication ring.
879 * Return when no more requests can be accepted or all buffers have
882 * Signal XEN once the ring has been filled out.
885 xb_startio(struct xb_softc *sc)
889 struct blkfront_info *info = sc->xb_info;
892 mtx_assert(&blkif_io_lock, MA_OWNED);
894 while ((bp = bioq_takefirst(&sc->xb_bioq)) != NULL) {
896 if (RING_FULL(&info->ring))
899 if (blkif_queue_request(bp)) {
901 bioq_insert_head(&sc->xb_bioq, bp);
908 flush_requests(sc->xb_info);
914 struct xb_softc *sc = NULL;
916 blkif_response_t *bret;
918 struct blkfront_info *info = xsc;
923 mtx_lock(&blkif_io_lock);
925 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
926 mtx_unlock(&blkif_io_lock);
931 rp = info->ring.sring->rsp_prod;
932 rmb(); /* Ensure we see queued responses up to 'rp'. */
934 for (i = info->ring.rsp_cons; i != rp; i++) {
937 bret = RING_GET_RESPONSE(&info->ring, i);
939 bp = (struct bio *)info->shadow[id].request;
941 blkif_completion(&info->shadow[id]);
943 ADD_ID_TO_FREELIST(info, id);
945 switch (bret->operation) {
947 /* had an unaligned buffer that needs to be copied */
949 bcopy(bp->bio_driver2, bp->bio_data, bp->bio_bcount);
953 /* free the copy buffer */
954 if (bp->bio_driver1) {
955 free(bp->bio_driver1, M_DEVBUF);
956 bp->bio_driver1 = NULL;
959 if ( unlikely(bret->status != BLKIF_RSP_OKAY) ) {
960 printf("Bad return from blkdev data request: %x\n",
962 bp->bio_flags |= BIO_ERROR;
965 sc = (struct xb_softc *)bp->bio_disk->d_drv1;
967 if (bp->bio_flags & BIO_ERROR)
975 panic("received invalid operation");
980 info->ring.rsp_cons = i;
982 if (i != info->ring.req_prod_pvt) {
984 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
988 info->ring.sring->rsp_event = i + 1;
991 kick_pending_request_queues(info);
993 mtx_unlock(&blkif_io_lock);
997 blkif_free(struct blkfront_info *info, int suspend)
1000 /* Prevent new requests being issued until we fix things up. */
1001 mtx_lock(&blkif_io_lock);
1002 info->connected = suspend ?
1003 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1004 mtx_unlock(&blkif_io_lock);
1006 /* Free resources associated with old device channel. */
1007 if (info->ring_ref != GRANT_INVALID_REF) {
1008 gnttab_end_foreign_access(info->ring_ref,
1010 info->ring_ref = GRANT_INVALID_REF;
1011 info->ring.sring = NULL;
1014 unbind_from_irqhandler(info->irq);
1020 blkif_completion(struct blk_shadow *s)
1024 for (i = 0; i < s->req.nr_segments; i++)
1025 gnttab_end_foreign_access(s->req.seg[i].gref, 0UL);
1029 blkif_recover(struct blkfront_info *info)
1032 blkif_request_t *req;
1033 struct blk_shadow *copy;
1038 /* Stage 1: Make a safe copy of the shadow state. */
1039 copy = (struct blk_shadow *)malloc(sizeof(info->shadow), M_DEVBUF, M_NOWAIT|M_ZERO);
1040 memcpy(copy, info->shadow, sizeof(info->shadow));
1042 /* Stage 2: Set up free list. */
1043 memset(&info->shadow, 0, sizeof(info->shadow));
1044 for (i = 0; i < BLK_RING_SIZE; i++)
1045 info->shadow[i].req.id = i+1;
1046 info->shadow_free = info->ring.req_prod_pvt;
1047 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
1049 /* Stage 3: Find pending requests and requeue them. */
1050 for (i = 0; i < BLK_RING_SIZE; i++) {
1052 if (copy[i].request == 0)
1055 /* Grab a request slot and copy shadow state into it. */
1056 req = RING_GET_REQUEST(
1057 &info->ring, info->ring.req_prod_pvt);
1060 /* We get a new request id, and must reset the shadow state. */
1061 req->id = GET_ID_FROM_FREELIST(info);
1062 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
1064 /* Rewrite any grant references invalidated by suspend/resume. */
1065 for (j = 0; j < req->nr_segments; j++)
1066 gnttab_grant_foreign_access_ref(
1068 xenbus_get_otherend_id(info->xbdev),
1069 pfn_to_mfn(info->shadow[req->id].frame[j]),
1070 0 /* assume not readonly */);
1072 info->shadow[req->id].req = *req;
1074 info->ring.req_prod_pvt++;
1077 free(copy, M_DEVBUF);
1079 xenbus_set_state(info->xbdev, XenbusStateConnected);
1081 /* Now safe for us to use the shared ring */
1082 mtx_lock(&blkif_io_lock);
1083 info->connected = BLKIF_STATE_CONNECTED;
1084 mtx_unlock(&blkif_io_lock);
1086 /* Send off requeued requests */
1087 mtx_lock(&blkif_io_lock);
1088 flush_requests(info);
1090 /* Kick any other new requests queued since we resumed */
1091 kick_pending_request_queues(info);
1092 mtx_unlock(&blkif_io_lock);
1095 /* ** Driver registration ** */
1096 static device_method_t blkfront_methods[] = {
1097 /* Device interface */
1098 DEVMETHOD(device_probe, blkfront_probe),
1099 DEVMETHOD(device_attach, blkfront_attach),
1100 DEVMETHOD(device_detach, blkfront_detach),
1101 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1102 DEVMETHOD(device_suspend, blkfront_suspend),
1103 DEVMETHOD(device_resume, blkfront_resume),
1105 /* Xenbus interface */
1106 DEVMETHOD(xenbus_backend_changed, blkfront_backend_changed),
1111 static driver_t blkfront_driver = {
1114 sizeof(struct blkfront_info),
1116 devclass_t blkfront_devclass;
1118 DRIVER_MODULE(xbd, xenbus, blkfront_driver, blkfront_devclass, 0, 0);
1120 MTX_SYSINIT(ioreq, &blkif_io_lock, "BIO LOCK", MTX_NOWITNESS); /* XXX how does one enroll a lock? */