1 /******************************************************************************
4 * XenLinux virtual block-device driver.
6 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
7 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
8 * Copyright (c) 2004, Christian Limpach
9 * Copyright (c) 2004, Andrew Warfield
10 * Copyright (c) 2005, Christopher Clark
11 * Copyright (c) 2005, XenSource Ltd
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation; or, when distributed
16 * separately from the Linux kernel or incorporated into other
17 * software packages, subject to the following license:
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this source file (the "Software"), to deal in the Software without
21 * restriction, including without limitation the rights to use, copy, modify,
22 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
23 * and to permit persons to whom the Software is furnished to do so, subject to
24 * the following conditions:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
38 #include <linux/version.h>
40 #include <linux/cdrom.h>
41 #include <linux/sched.h>
42 #include <linux/interrupt.h>
43 #include <scsi/scsi.h>
44 #include <xen/evtchn.h>
45 #include <xen/xenbus.h>
46 #include <xen/interface/grant_table.h>
47 #include <xen/gnttab.h>
48 #include <asm/hypervisor.h>
50 #define BLKIF_STATE_DISCONNECTED 0
51 #define BLKIF_STATE_CONNECTED 1
52 #define BLKIF_STATE_SUSPENDED 2
54 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
55 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
56 #define GRANT_INVALID_REF 0
58 static void connect(struct blkfront_info *);
59 static void blkfront_closing(struct xenbus_device *);
60 static int blkfront_remove(struct xenbus_device *);
61 static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
62 static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
64 static void kick_pending_request_queues(struct blkfront_info *);
66 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs);
67 static void blkif_restart_queue(void *arg);
68 static void blkif_recover(struct blkfront_info *);
69 static void blkif_completion(struct blk_shadow *);
70 static void blkif_free(struct blkfront_info *, int);
74 * Entry point to this code when a new device is created. Allocate the basic
75 * structures and the ring buffer for communication with the backend, and
76 * inform the backend of the appropriate details for those. Switch to
79 static int blkfront_probe(struct xenbus_device *dev,
80 const struct xenbus_device_id *id)
83 struct blkfront_info *info;
85 /* FIXME: Use dynamic device id if this is not set. */
86 err = xenbus_scanf(XBT_NIL, dev->nodename,
87 "virtual-device", "%i", &vdevice);
89 xenbus_dev_fatal(dev, err, "reading virtual-device");
93 info = kzalloc(sizeof(*info), GFP_KERNEL);
95 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
100 info->vdevice = vdevice;
101 info->connected = BLKIF_STATE_DISCONNECTED;
102 INIT_WORK(&info->work, blkif_restart_queue, (void *)info);
104 for (i = 0; i < BLK_RING_SIZE; i++)
105 info->shadow[i].req.id = i+1;
106 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
108 /* Front end dir is a number, which is used as the id. */
109 info->handle = simple_strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
110 dev->dev.driver_data = info;
112 err = talk_to_backend(dev, info);
115 dev->dev.driver_data = NULL;
124 * We are reconnecting to the backend, due to a suspend/resume, or a backend
125 * driver restart. We tear down our blkif structure and recreate it, but
126 * leave the device-layer structures intact so that this is transparent to the
127 * rest of the kernel.
129 static int blkfront_resume(struct xenbus_device *dev)
131 struct blkfront_info *info = dev->dev.driver_data;
134 DPRINTK("blkfront_resume: %s\n", dev->nodename);
138 err = talk_to_backend(dev, info);
146 /* Common code used when first setting up, and when resuming. */
147 static int talk_to_backend(struct xenbus_device *dev,
148 struct blkfront_info *info)
150 const char *message = NULL;
151 struct xenbus_transaction xbt;
154 /* Create shared ring, alloc event channel. */
155 err = setup_blkring(dev, info);
160 err = xenbus_transaction_start(&xbt);
162 xenbus_dev_fatal(dev, err, "starting transaction");
163 goto destroy_blkring;
166 err = xenbus_printf(xbt, dev->nodename,
167 "ring-ref","%u", info->ring_ref);
169 message = "writing ring-ref";
170 goto abort_transaction;
172 err = xenbus_printf(xbt, dev->nodename,
173 "event-channel", "%u", info->evtchn);
175 message = "writing event-channel";
176 goto abort_transaction;
179 err = xenbus_transaction_end(xbt, 0);
183 xenbus_dev_fatal(dev, err, "completing transaction");
184 goto destroy_blkring;
187 xenbus_switch_state(dev, XenbusStateInitialised);
192 xenbus_transaction_end(xbt, 1);
194 xenbus_dev_fatal(dev, err, "%s", message);
202 static int setup_blkring(struct xenbus_device *dev,
203 struct blkfront_info *info)
205 blkif_sring_t *sring;
208 info->ring_ref = GRANT_INVALID_REF;
210 sring = (blkif_sring_t *)__get_free_page(GFP_KERNEL);
212 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
215 SHARED_RING_INIT(sring);
216 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
218 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
220 free_page((unsigned long)sring);
221 info->ring.sring = NULL;
224 info->ring_ref = err;
226 err = xenbus_alloc_evtchn(dev, &info->evtchn);
230 err = bind_evtchn_to_irqhandler(
231 info->evtchn, blkif_int, SA_SAMPLE_RANDOM, "blkif", info);
233 xenbus_dev_fatal(dev, err,
234 "bind_evtchn_to_irqhandler failed");
247 * Callback received when the backend's state changes.
249 static void backend_changed(struct xenbus_device *dev,
250 enum xenbus_state backend_state)
252 struct blkfront_info *info = dev->dev.driver_data;
253 struct block_device *bd;
255 DPRINTK("blkfront:backend_changed.\n");
257 switch (backend_state) {
258 case XenbusStateUnknown:
259 case XenbusStateInitialising:
260 case XenbusStateInitWait:
261 case XenbusStateInitialised:
262 case XenbusStateClosed:
265 case XenbusStateConnected:
269 case XenbusStateClosing:
270 bd = bdget(info->dev);
272 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
274 mutex_lock(&bd->bd_mutex);
276 xenbus_dev_error(dev, -EBUSY,
277 "Device in use; refusing to close");
279 blkfront_closing(dev);
280 mutex_unlock(&bd->bd_mutex);
287 /* ** Connection ** */
291 * Invoked when the backend is finally 'ready' (and has told produced
292 * the details about the physical device - #sectors, size, etc).
294 static void connect(struct blkfront_info *info)
296 unsigned long sectors, sector_size;
300 if ((info->connected == BLKIF_STATE_CONNECTED) ||
301 (info->connected == BLKIF_STATE_SUSPENDED) )
304 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
306 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
307 "sectors", "%lu", §ors,
308 "info", "%u", &binfo,
309 "sector-size", "%lu", §or_size,
312 xenbus_dev_fatal(info->xbdev, err,
313 "reading backend fields at %s",
314 info->xbdev->otherend);
318 err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
320 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
321 info->xbdev->otherend);
325 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
327 /* Kick pending requests. */
328 spin_lock_irq(&blkif_io_lock);
329 info->connected = BLKIF_STATE_CONNECTED;
330 kick_pending_request_queues(info);
331 spin_unlock_irq(&blkif_io_lock);
337 * Handle the change of state of the backend to Closing. We must delete our
338 * device-layer structures now, to ensure that writes are flushed through to
339 * the backend. Once is this done, we can switch to Closed in
342 static void blkfront_closing(struct xenbus_device *dev)
344 struct blkfront_info *info = dev->dev.driver_data;
347 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
349 if (info->rq == NULL)
352 spin_lock_irqsave(&blkif_io_lock, flags);
353 /* No more blkif_request(). */
354 blk_stop_queue(info->rq);
355 /* No more gnttab callback work. */
356 gnttab_cancel_free_callback(&info->callback);
357 flush_scheduled_work();
358 spin_unlock_irqrestore(&blkif_io_lock, flags);
362 xenbus_switch_state(dev, XenbusStateClosed);
366 static int blkfront_remove(struct xenbus_device *dev)
368 struct blkfront_info *info = dev->dev.driver_data;
370 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
380 static inline int GET_ID_FROM_FREELIST(
381 struct blkfront_info *info)
383 unsigned long free = info->shadow_free;
384 BUG_ON(free > BLK_RING_SIZE);
385 info->shadow_free = info->shadow[free].req.id;
386 info->shadow[free].req.id = 0x0fffffee; /* debug */
390 static inline void ADD_ID_TO_FREELIST(
391 struct blkfront_info *info, unsigned long id)
393 info->shadow[id].req.id = info->shadow_free;
394 info->shadow[id].request = 0;
395 info->shadow_free = id;
398 static inline void flush_requests(struct blkfront_info *info)
402 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
405 notify_remote_via_irq(info->irq);
408 static void kick_pending_request_queues(struct blkfront_info *info)
410 if (!RING_FULL(&info->ring)) {
411 /* Re-enable calldowns. */
412 blk_start_queue(info->rq);
413 /* Kick things off immediately. */
414 do_blkif_request(info->rq);
418 static void blkif_restart_queue(void *arg)
420 struct blkfront_info *info = (struct blkfront_info *)arg;
421 spin_lock_irq(&blkif_io_lock);
422 if (info->connected == BLKIF_STATE_CONNECTED)
423 kick_pending_request_queues(info);
424 spin_unlock_irq(&blkif_io_lock);
427 static void blkif_restart_queue_callback(void *arg)
429 struct blkfront_info *info = (struct blkfront_info *)arg;
430 schedule_work(&info->work);
433 int blkif_open(struct inode *inode, struct file *filep)
435 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
441 int blkif_release(struct inode *inode, struct file *filep)
443 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
445 if (info->users == 0) {
446 /* Check whether we have been instructed to close. We will
447 have ignored this request initially, as the device was
449 struct xenbus_device * dev = info->xbdev;
450 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
452 if (state == XenbusStateClosing)
453 blkfront_closing(dev);
459 int blkif_ioctl(struct inode *inode, struct file *filep,
460 unsigned command, unsigned long argument)
464 DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
465 command, (long)argument, inode->i_rdev);
468 case CDROMMULTISESSION:
469 DPRINTK("FIXME: support multisession CDs later\n");
470 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
471 if (put_user(0, (char __user *)(argument + i)))
476 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
478 return -EINVAL; /* same return as native Linux */
485 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
487 /* We don't have real geometry info, but let's at least return
488 values consistent with the size of the device */
489 sector_t nsect = get_capacity(bd->bd_disk);
490 sector_t cylinders = nsect;
494 sector_div(cylinders, hg->heads * hg->sectors);
495 hg->cylinders = cylinders;
496 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
497 hg->cylinders = 0xffff;
503 * blkif_queue_request
507 * id: for guest use only.
508 * operation: BLKIF_OP_{READ,WRITE,PROBE}
509 * buffer: buffer to read/write into. this should be a
510 * virtual address in the guest os.
512 static int blkif_queue_request(struct request *req)
514 struct blkfront_info *info = req->rq_disk->private_data;
515 unsigned long buffer_mfn;
516 blkif_request_t *ring_req;
518 struct bio_vec *bvec;
521 unsigned int fsect, lsect;
523 grant_ref_t gref_head;
525 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
528 if (gnttab_alloc_grant_references(
529 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
530 gnttab_request_free_callback(
532 blkif_restart_queue_callback,
534 BLKIF_MAX_SEGMENTS_PER_REQUEST);
538 /* Fill out a communications ring structure. */
539 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
540 id = GET_ID_FROM_FREELIST(info);
541 info->shadow[id].request = (unsigned long)req;
544 ring_req->operation = rq_data_dir(req) ?
545 BLKIF_OP_WRITE : BLKIF_OP_READ;
546 ring_req->sector_number = (blkif_sector_t)req->sector;
547 ring_req->handle = info->handle;
549 ring_req->nr_segments = 0;
550 rq_for_each_bio (bio, req) {
551 bio_for_each_segment (bvec, bio, idx) {
552 BUG_ON(ring_req->nr_segments
553 == BLKIF_MAX_SEGMENTS_PER_REQUEST);
554 buffer_mfn = page_to_phys(bvec->bv_page) >> PAGE_SHIFT;
555 fsect = bvec->bv_offset >> 9;
556 lsect = fsect + (bvec->bv_len >> 9) - 1;
557 /* install a grant reference. */
558 ref = gnttab_claim_grant_reference(&gref_head);
559 BUG_ON(ref == -ENOSPC);
561 gnttab_grant_foreign_access_ref(
563 info->xbdev->otherend_id,
567 info->shadow[id].frame[ring_req->nr_segments] =
568 mfn_to_pfn(buffer_mfn);
570 ring_req->seg[ring_req->nr_segments] =
571 (struct blkif_request_segment) {
574 .last_sect = lsect };
576 ring_req->nr_segments++;
580 info->ring.req_prod_pvt++;
582 /* Keep a private copy so we can reissue requests when recovering. */
583 info->shadow[id].req = *ring_req;
585 gnttab_free_grant_references(gref_head);
592 * read a block; request is in a request queue
594 void do_blkif_request(request_queue_t *rq)
596 struct blkfront_info *info = NULL;
600 DPRINTK("Entered do_blkif_request\n");
604 while ((req = elv_next_request(rq)) != NULL) {
605 info = req->rq_disk->private_data;
606 if (!blk_fs_request(req)) {
611 if (RING_FULL(&info->ring))
614 DPRINTK("do_blk_req %p: cmd %p, sec %lx, "
615 "(%u/%li) buffer:%p [%s]\n",
616 req, req->cmd, req->sector, req->current_nr_sectors,
617 req->nr_sectors, req->buffer,
618 rq_data_dir(req) ? "write" : "read");
621 blkdev_dequeue_request(req);
622 if (blkif_queue_request(req)) {
623 blk_requeue_request(rq, req);
625 /* Avoid pointless unplugs. */
634 flush_requests(info);
638 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs)
641 blkif_response_t *bret;
644 struct blkfront_info *info = (struct blkfront_info *)dev_id;
646 spin_lock_irqsave(&blkif_io_lock, flags);
648 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
649 spin_unlock_irqrestore(&blkif_io_lock, flags);
654 rp = info->ring.sring->rsp_prod;
655 rmb(); /* Ensure we see queued responses up to 'rp'. */
657 for (i = info->ring.rsp_cons; i != rp; i++) {
661 bret = RING_GET_RESPONSE(&info->ring, i);
663 req = (struct request *)info->shadow[id].request;
665 blkif_completion(&info->shadow[id]);
667 ADD_ID_TO_FREELIST(info, id);
669 switch (bret->operation) {
672 if (unlikely(bret->status != BLKIF_RSP_OKAY))
673 DPRINTK("Bad return from blkdev data "
674 "request: %x\n", bret->status);
676 ret = end_that_request_first(
677 req, (bret->status == BLKIF_RSP_OKAY),
678 req->hard_nr_sectors);
680 end_that_request_last(
681 req, (bret->status == BLKIF_RSP_OKAY));
688 info->ring.rsp_cons = i;
690 if (i != info->ring.req_prod_pvt) {
692 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
696 info->ring.sring->rsp_event = i + 1;
698 kick_pending_request_queues(info);
700 spin_unlock_irqrestore(&blkif_io_lock, flags);
705 static void blkif_free(struct blkfront_info *info, int suspend)
707 /* Prevent new requests being issued until we fix things up. */
708 spin_lock_irq(&blkif_io_lock);
709 info->connected = suspend ?
710 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
711 /* No more blkif_request(). */
713 blk_stop_queue(info->rq);
714 /* No more gnttab callback work. */
715 gnttab_cancel_free_callback(&info->callback);
716 flush_scheduled_work();
717 spin_unlock_irq(&blkif_io_lock);
719 /* Free resources associated with old device channel. */
720 if (info->ring_ref != GRANT_INVALID_REF) {
721 gnttab_end_foreign_access(info->ring_ref, 0,
722 (unsigned long)info->ring.sring);
723 info->ring_ref = GRANT_INVALID_REF;
724 info->ring.sring = NULL;
727 unbind_from_irqhandler(info->irq, info);
728 info->evtchn = info->irq = 0;
732 static void blkif_completion(struct blk_shadow *s)
735 for (i = 0; i < s->req.nr_segments; i++)
736 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
739 static void blkif_recover(struct blkfront_info *info)
742 blkif_request_t *req;
743 struct blk_shadow *copy;
746 /* Stage 1: Make a safe copy of the shadow state. */
747 copy = kmalloc(sizeof(info->shadow), GFP_KERNEL | __GFP_NOFAIL);
748 memcpy(copy, info->shadow, sizeof(info->shadow));
750 /* Stage 2: Set up free list. */
751 memset(&info->shadow, 0, sizeof(info->shadow));
752 for (i = 0; i < BLK_RING_SIZE; i++)
753 info->shadow[i].req.id = i+1;
754 info->shadow_free = info->ring.req_prod_pvt;
755 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
757 /* Stage 3: Find pending requests and requeue them. */
758 for (i = 0; i < BLK_RING_SIZE; i++) {
760 if (copy[i].request == 0)
763 /* Grab a request slot and copy shadow state into it. */
764 req = RING_GET_REQUEST(
765 &info->ring, info->ring.req_prod_pvt);
768 /* We get a new request id, and must reset the shadow state. */
769 req->id = GET_ID_FROM_FREELIST(info);
770 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
772 /* Rewrite any grant references invalidated by susp/resume. */
773 for (j = 0; j < req->nr_segments; j++)
774 gnttab_grant_foreign_access_ref(
776 info->xbdev->otherend_id,
777 pfn_to_mfn(info->shadow[req->id].frame[j]),
780 info->shadow[req->id].request));
781 info->shadow[req->id].req = *req;
783 info->ring.req_prod_pvt++;
788 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
790 spin_lock_irq(&blkif_io_lock);
792 /* Now safe for us to use the shared ring */
793 info->connected = BLKIF_STATE_CONNECTED;
795 /* Send off requeued requests */
796 flush_requests(info);
798 /* Kick any other new requests queued since we resumed */
799 kick_pending_request_queues(info);
801 spin_unlock_irq(&blkif_io_lock);
805 /* ** Driver Registration ** */
808 static struct xenbus_device_id blkfront_ids[] = {
814 static struct xenbus_driver blkfront = {
816 .owner = THIS_MODULE,
818 .probe = blkfront_probe,
819 .remove = blkfront_remove,
820 .resume = blkfront_resume,
821 .otherend_changed = backend_changed,
825 static int __init xlblk_init(void)
827 if (!is_running_on_xen())
830 return xenbus_register_frontend(&blkfront);
832 module_init(xlblk_init);
835 static void xlblk_exit(void)
837 return xenbus_unregister_driver(&blkfront);
839 module_exit(xlblk_exit);
841 MODULE_LICENSE("Dual BSD/GPL");