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>
49 #include <asm/maddr.h>
51 #define BLKIF_STATE_DISCONNECTED 0
52 #define BLKIF_STATE_CONNECTED 1
53 #define BLKIF_STATE_SUSPENDED 2
55 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
56 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
57 #define GRANT_INVALID_REF 0
59 static void connect(struct blkfront_info *);
60 static void blkfront_closing(struct xenbus_device *);
61 static int blkfront_remove(struct xenbus_device *);
62 static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
63 static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
65 static void kick_pending_request_queues(struct blkfront_info *);
67 static irqreturn_t blkif_int(int irq, void *dev_id);
68 static void blkif_restart_queue(struct work_struct *work);
69 static void blkif_recover(struct blkfront_info *);
70 static void blkif_completion(struct blk_shadow *);
71 static void blkif_free(struct blkfront_info *, int);
75 * Entry point to this code when a new device is created. Allocate the basic
76 * structures and the ring buffer for communication with the backend, and
77 * inform the backend of the appropriate details for those. Switch to
80 static int blkfront_probe(struct xenbus_device *dev,
81 const struct xenbus_device_id *id)
84 struct blkfront_info *info;
86 /* FIXME: Use dynamic device id if this is not set. */
87 err = xenbus_scanf(XBT_NIL, dev->nodename,
88 "virtual-device", "%i", &vdevice);
90 xenbus_dev_fatal(dev, err, "reading virtual-device");
94 info = kzalloc(sizeof(*info), GFP_KERNEL);
96 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
101 info->vdevice = vdevice;
102 info->connected = BLKIF_STATE_DISCONNECTED;
103 INIT_WORK(&info->work, blkif_restart_queue);
105 for (i = 0; i < BLK_RING_SIZE; i++)
106 info->shadow[i].req.id = i+1;
107 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
109 /* Front end dir is a number, which is used as the id. */
110 info->handle = simple_strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
111 dev->dev.driver_data = info;
113 err = talk_to_backend(dev, info);
116 dev->dev.driver_data = NULL;
125 * We are reconnecting to the backend, due to a suspend/resume, or a backend
126 * driver restart. We tear down our blkif structure and recreate it, but
127 * leave the device-layer structures intact so that this is transparent to the
128 * rest of the kernel.
130 static int blkfront_resume(struct xenbus_device *dev)
132 struct blkfront_info *info = dev->dev.driver_data;
135 DPRINTK("blkfront_resume: %s\n", dev->nodename);
139 err = talk_to_backend(dev, info);
147 /* Common code used when first setting up, and when resuming. */
148 static int talk_to_backend(struct xenbus_device *dev,
149 struct blkfront_info *info)
151 const char *message = NULL;
152 struct xenbus_transaction xbt;
155 /* Create shared ring, alloc event channel. */
156 err = setup_blkring(dev, info);
161 err = xenbus_transaction_start(&xbt);
163 xenbus_dev_fatal(dev, err, "starting transaction");
164 goto destroy_blkring;
167 err = xenbus_printf(xbt, dev->nodename,
168 "ring-ref","%u", info->ring_ref);
170 message = "writing ring-ref";
171 goto abort_transaction;
173 err = xenbus_printf(xbt, dev->nodename,
174 "event-channel", "%u", info->evtchn);
176 message = "writing event-channel";
177 goto abort_transaction;
180 err = xenbus_transaction_end(xbt, 0);
184 xenbus_dev_fatal(dev, err, "completing transaction");
185 goto destroy_blkring;
188 xenbus_switch_state(dev, XenbusStateInitialised);
193 xenbus_transaction_end(xbt, 1);
195 xenbus_dev_fatal(dev, err, "%s", message);
203 static int setup_blkring(struct xenbus_device *dev,
204 struct blkfront_info *info)
206 blkif_sring_t *sring;
209 info->ring_ref = GRANT_INVALID_REF;
211 sring = (blkif_sring_t *)__get_free_page(GFP_KERNEL);
213 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
216 SHARED_RING_INIT(sring);
217 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
219 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
221 free_page((unsigned long)sring);
222 info->ring.sring = NULL;
225 info->ring_ref = err;
227 err = xenbus_alloc_evtchn(dev, &info->evtchn);
231 err = bind_evtchn_to_irqhandler(
232 info->evtchn, blkif_int, SA_SAMPLE_RANDOM, "blkif", info);
234 xenbus_dev_fatal(dev, err,
235 "bind_evtchn_to_irqhandler failed");
248 * Callback received when the backend's state changes.
250 static void backend_changed(struct xenbus_device *dev,
251 enum xenbus_state backend_state)
253 struct blkfront_info *info = dev->dev.driver_data;
254 struct block_device *bd;
256 DPRINTK("blkfront:backend_changed.\n");
258 switch (backend_state) {
259 case XenbusStateInitialising:
260 case XenbusStateInitWait:
261 case XenbusStateInitialised:
262 case XenbusStateUnknown:
263 case XenbusStateClosed:
266 case XenbusStateConnected:
270 case XenbusStateClosing:
271 bd = bdget(info->dev);
273 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
275 mutex_lock(&bd->bd_mutex);
277 xenbus_dev_error(dev, -EBUSY,
278 "Device in use; refusing to close");
280 blkfront_closing(dev);
281 mutex_unlock(&bd->bd_mutex);
288 /* ** Connection ** */
292 * Invoked when the backend is finally 'ready' (and has told produced
293 * the details about the physical device - #sectors, size, etc).
295 static void connect(struct blkfront_info *info)
297 unsigned long long sectors;
298 unsigned long sector_size;
302 if ((info->connected == BLKIF_STATE_CONNECTED) ||
303 (info->connected == BLKIF_STATE_SUSPENDED) )
306 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
308 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
309 "sectors", "%llu", §ors,
310 "info", "%u", &binfo,
311 "sector-size", "%lu", §or_size,
314 xenbus_dev_fatal(info->xbdev, err,
315 "reading backend fields at %s",
316 info->xbdev->otherend);
320 err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
322 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
323 info->xbdev->otherend);
327 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
329 /* Kick pending requests. */
330 spin_lock_irq(&blkif_io_lock);
331 info->connected = BLKIF_STATE_CONNECTED;
332 kick_pending_request_queues(info);
333 spin_unlock_irq(&blkif_io_lock);
339 * Handle the change of state of the backend to Closing. We must delete our
340 * device-layer structures now, to ensure that writes are flushed through to
341 * the backend. Once is this done, we can switch to Closed in
344 static void blkfront_closing(struct xenbus_device *dev)
346 struct blkfront_info *info = dev->dev.driver_data;
349 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
351 if (info->rq == NULL)
354 spin_lock_irqsave(&blkif_io_lock, flags);
355 /* No more blkif_request(). */
356 blk_stop_queue(info->rq);
357 /* No more gnttab callback work. */
358 gnttab_cancel_free_callback(&info->callback);
359 spin_unlock_irqrestore(&blkif_io_lock, flags);
361 /* Flush gnttab callback work. Must be done with no locks held. */
362 flush_scheduled_work();
367 xenbus_frontend_closed(dev);
371 static int blkfront_remove(struct xenbus_device *dev)
373 struct blkfront_info *info = dev->dev.driver_data;
375 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
385 static inline int GET_ID_FROM_FREELIST(
386 struct blkfront_info *info)
388 unsigned long free = info->shadow_free;
389 BUG_ON(free > BLK_RING_SIZE);
390 info->shadow_free = info->shadow[free].req.id;
391 info->shadow[free].req.id = 0x0fffffee; /* debug */
395 static inline void ADD_ID_TO_FREELIST(
396 struct blkfront_info *info, unsigned long id)
398 info->shadow[id].req.id = info->shadow_free;
399 info->shadow[id].request = 0;
400 info->shadow_free = id;
403 static inline void flush_requests(struct blkfront_info *info)
407 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
410 notify_remote_via_irq(info->irq);
413 static void kick_pending_request_queues(struct blkfront_info *info)
415 if (!RING_FULL(&info->ring)) {
416 /* Re-enable calldowns. */
417 blk_start_queue(info->rq);
418 /* Kick things off immediately. */
419 do_blkif_request(info->rq);
423 static void blkif_restart_queue(struct work_struct *work)
425 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
427 spin_lock_irq(&blkif_io_lock);
428 if (info->connected == BLKIF_STATE_CONNECTED)
429 kick_pending_request_queues(info);
430 spin_unlock_irq(&blkif_io_lock);
433 static void blkif_restart_queue_callback(void *arg)
435 struct blkfront_info *info = (struct blkfront_info *)arg;
436 schedule_work(&info->work);
439 int blkif_open(struct inode *inode, struct file *filep)
441 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
447 int blkif_release(struct inode *inode, struct file *filep)
449 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
451 if (info->users == 0) {
452 /* Check whether we have been instructed to close. We will
453 have ignored this request initially, as the device was
455 struct xenbus_device * dev = info->xbdev;
456 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
458 if (state == XenbusStateClosing)
459 blkfront_closing(dev);
465 int blkif_ioctl(struct inode *inode, struct file *filep,
466 unsigned command, unsigned long argument)
470 DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
471 command, (long)argument, inode->i_rdev);
474 case CDROMMULTISESSION:
475 DPRINTK("FIXME: support multisession CDs later\n");
476 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
477 if (put_user(0, (char __user *)(argument + i)))
482 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
484 return -EINVAL; /* same return as native Linux */
491 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
493 /* We don't have real geometry info, but let's at least return
494 values consistent with the size of the device */
495 sector_t nsect = get_capacity(bd->bd_disk);
496 sector_t cylinders = nsect;
500 sector_div(cylinders, hg->heads * hg->sectors);
501 hg->cylinders = cylinders;
502 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
503 hg->cylinders = 0xffff;
509 * blkif_queue_request
513 * id: for guest use only.
514 * operation: BLKIF_OP_{READ,WRITE,PROBE}
515 * buffer: buffer to read/write into. this should be a
516 * virtual address in the guest os.
518 static int blkif_queue_request(struct request *req)
520 struct blkfront_info *info = req->rq_disk->private_data;
521 unsigned long buffer_mfn;
522 blkif_request_t *ring_req;
524 struct bio_vec *bvec;
527 unsigned int fsect, lsect;
529 grant_ref_t gref_head;
531 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
534 if (gnttab_alloc_grant_references(
535 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
536 gnttab_request_free_callback(
538 blkif_restart_queue_callback,
540 BLKIF_MAX_SEGMENTS_PER_REQUEST);
544 /* Fill out a communications ring structure. */
545 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
546 id = GET_ID_FROM_FREELIST(info);
547 info->shadow[id].request = (unsigned long)req;
550 ring_req->operation = rq_data_dir(req) ?
551 BLKIF_OP_WRITE : BLKIF_OP_READ;
552 ring_req->sector_number = (blkif_sector_t)req->sector;
553 ring_req->handle = info->handle;
555 ring_req->nr_segments = 0;
556 rq_for_each_bio (bio, req) {
557 bio_for_each_segment (bvec, bio, idx) {
558 BUG_ON(ring_req->nr_segments
559 == BLKIF_MAX_SEGMENTS_PER_REQUEST);
560 buffer_mfn = page_to_phys(bvec->bv_page) >> PAGE_SHIFT;
561 fsect = bvec->bv_offset >> 9;
562 lsect = fsect + (bvec->bv_len >> 9) - 1;
563 /* install a grant reference. */
564 ref = gnttab_claim_grant_reference(&gref_head);
565 BUG_ON(ref == -ENOSPC);
567 gnttab_grant_foreign_access_ref(
569 info->xbdev->otherend_id,
573 info->shadow[id].frame[ring_req->nr_segments] =
574 mfn_to_pfn(buffer_mfn);
576 ring_req->seg[ring_req->nr_segments] =
577 (struct blkif_request_segment) {
580 .last_sect = lsect };
582 ring_req->nr_segments++;
586 info->ring.req_prod_pvt++;
588 /* Keep a private copy so we can reissue requests when recovering. */
589 info->shadow[id].req = *ring_req;
591 gnttab_free_grant_references(gref_head);
598 * read a block; request is in a request queue
600 void do_blkif_request(request_queue_t *rq)
602 struct blkfront_info *info = NULL;
606 DPRINTK("Entered do_blkif_request\n");
610 while ((req = elv_next_request(rq)) != NULL) {
611 info = req->rq_disk->private_data;
612 if (!blk_fs_request(req)) {
617 if (RING_FULL(&info->ring))
620 DPRINTK("do_blk_req %p: cmd %p, sec %llx, "
621 "(%u/%li) buffer:%p [%s]\n",
622 req, req->cmd, (u64)req->sector, req->current_nr_sectors,
623 req->nr_sectors, req->buffer,
624 rq_data_dir(req) ? "write" : "read");
627 blkdev_dequeue_request(req);
628 if (blkif_queue_request(req)) {
629 blk_requeue_request(rq, req);
631 /* Avoid pointless unplugs. */
640 flush_requests(info);
644 static irqreturn_t blkif_int(int irq, void *dev_id)
647 blkif_response_t *bret;
650 struct blkfront_info *info = (struct blkfront_info *)dev_id;
652 spin_lock_irqsave(&blkif_io_lock, flags);
654 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
655 spin_unlock_irqrestore(&blkif_io_lock, flags);
660 rp = info->ring.sring->rsp_prod;
661 rmb(); /* Ensure we see queued responses up to 'rp'. */
663 for (i = info->ring.rsp_cons; i != rp; i++) {
667 bret = RING_GET_RESPONSE(&info->ring, i);
669 req = (struct request *)info->shadow[id].request;
671 blkif_completion(&info->shadow[id]);
673 ADD_ID_TO_FREELIST(info, id);
675 switch (bret->operation) {
678 if (unlikely(bret->status != BLKIF_RSP_OKAY))
679 DPRINTK("Bad return from blkdev data "
680 "request: %x\n", bret->status);
682 ret = end_that_request_first(
683 req, (bret->status == BLKIF_RSP_OKAY),
684 req->hard_nr_sectors);
686 end_that_request_last(
687 req, (bret->status == BLKIF_RSP_OKAY));
694 info->ring.rsp_cons = i;
696 if (i != info->ring.req_prod_pvt) {
698 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
702 info->ring.sring->rsp_event = i + 1;
704 kick_pending_request_queues(info);
706 spin_unlock_irqrestore(&blkif_io_lock, flags);
711 static void blkif_free(struct blkfront_info *info, int suspend)
713 /* Prevent new requests being issued until we fix things up. */
714 spin_lock_irq(&blkif_io_lock);
715 info->connected = suspend ?
716 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
717 /* No more blkif_request(). */
719 blk_stop_queue(info->rq);
720 /* No more gnttab callback work. */
721 gnttab_cancel_free_callback(&info->callback);
722 spin_unlock_irq(&blkif_io_lock);
724 /* Flush gnttab callback work. Must be done with no locks held. */
725 flush_scheduled_work();
727 /* Free resources associated with old device channel. */
728 if (info->ring_ref != GRANT_INVALID_REF) {
729 gnttab_end_foreign_access(info->ring_ref, 0,
730 (unsigned long)info->ring.sring);
731 info->ring_ref = GRANT_INVALID_REF;
732 info->ring.sring = NULL;
735 unbind_from_irqhandler(info->irq, info);
736 info->evtchn = info->irq = 0;
740 static void blkif_completion(struct blk_shadow *s)
743 for (i = 0; i < s->req.nr_segments; i++)
744 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
747 static void blkif_recover(struct blkfront_info *info)
750 blkif_request_t *req;
751 struct blk_shadow *copy;
754 /* Stage 1: Make a safe copy of the shadow state. */
755 copy = kmalloc(sizeof(info->shadow), GFP_KERNEL | __GFP_NOFAIL);
756 memcpy(copy, info->shadow, sizeof(info->shadow));
758 /* Stage 2: Set up free list. */
759 memset(&info->shadow, 0, sizeof(info->shadow));
760 for (i = 0; i < BLK_RING_SIZE; i++)
761 info->shadow[i].req.id = i+1;
762 info->shadow_free = info->ring.req_prod_pvt;
763 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
765 /* Stage 3: Find pending requests and requeue them. */
766 for (i = 0; i < BLK_RING_SIZE; i++) {
768 if (copy[i].request == 0)
771 /* Grab a request slot and copy shadow state into it. */
772 req = RING_GET_REQUEST(
773 &info->ring, info->ring.req_prod_pvt);
776 /* We get a new request id, and must reset the shadow state. */
777 req->id = GET_ID_FROM_FREELIST(info);
778 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
780 /* Rewrite any grant references invalidated by susp/resume. */
781 for (j = 0; j < req->nr_segments; j++)
782 gnttab_grant_foreign_access_ref(
784 info->xbdev->otherend_id,
785 pfn_to_mfn(info->shadow[req->id].frame[j]),
788 info->shadow[req->id].request));
789 info->shadow[req->id].req = *req;
791 info->ring.req_prod_pvt++;
796 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
798 spin_lock_irq(&blkif_io_lock);
800 /* Now safe for us to use the shared ring */
801 info->connected = BLKIF_STATE_CONNECTED;
803 /* Send off requeued requests */
804 flush_requests(info);
806 /* Kick any other new requests queued since we resumed */
807 kick_pending_request_queues(info);
809 spin_unlock_irq(&blkif_io_lock);
813 /* ** Driver Registration ** */
816 static struct xenbus_device_id blkfront_ids[] = {
822 static struct xenbus_driver blkfront = {
824 .owner = THIS_MODULE,
826 .probe = blkfront_probe,
827 .remove = blkfront_remove,
828 .resume = blkfront_resume,
829 .otherend_changed = backend_changed,
833 static int __init xlblk_init(void)
835 if (!is_running_on_xen())
838 return xenbus_register_frontend(&blkfront);
840 module_init(xlblk_init);
843 static void xlblk_exit(void)
845 return xenbus_unregister_driver(&blkfront);
847 module_exit(xlblk_exit);
849 MODULE_LICENSE("Dual BSD/GPL");