ia64/linux-2.6.18-xen.hg

view drivers/xen/blkfront/blkfront.c @ 805:63be3e5e2f1a

linux/blkfront: use blk_rq_map_sg to generate ring entries

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Mar 05 14:42:00 2009 +0000 (2009-03-05)
parents 1d647ef26f3f
children 0430b1dbfb3a
line source
1 /******************************************************************************
2 * blkfront.c
3 *
4 * XenLinux virtual block-device driver.
5 *
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
12 *
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:
18 *
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:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
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
35 * IN THE SOFTWARE.
36 */
38 #include <linux/version.h>
39 #include "block.h"
40 #include <linux/cdrom.h>
41 #include <linux/sched.h>
42 #include <linux/interrupt.h>
43 #include <linux/scatterlist.h>
44 #include <scsi/scsi.h>
45 #include <xen/evtchn.h>
46 #include <xen/xenbus.h>
47 #include <xen/interface/grant_table.h>
48 #include <xen/interface/io/protocols.h>
49 #include <xen/gnttab.h>
50 #include <asm/hypervisor.h>
51 #include <asm/maddr.h>
53 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
54 #include <xen/platform-compat.h>
55 #endif
57 #define BLKIF_STATE_DISCONNECTED 0
58 #define BLKIF_STATE_CONNECTED 1
59 #define BLKIF_STATE_SUSPENDED 2
61 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
62 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
63 #define GRANT_INVALID_REF 0
65 static void connect(struct blkfront_info *);
66 static void blkfront_closing(struct xenbus_device *);
67 static int blkfront_remove(struct xenbus_device *);
68 static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
69 static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
71 static void kick_pending_request_queues(struct blkfront_info *);
73 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs);
74 static void blkif_restart_queue(void *arg);
75 static void blkif_recover(struct blkfront_info *);
76 static void blkif_completion(struct blk_shadow *);
77 static void blkif_free(struct blkfront_info *, int);
80 /**
81 * Entry point to this code when a new device is created. Allocate the basic
82 * structures and the ring buffer for communication with the backend, and
83 * inform the backend of the appropriate details for those. Switch to
84 * Initialised state.
85 */
86 static int blkfront_probe(struct xenbus_device *dev,
87 const struct xenbus_device_id *id)
88 {
89 int err, vdevice, i;
90 struct blkfront_info *info;
92 /* FIXME: Use dynamic device id if this is not set. */
93 err = xenbus_scanf(XBT_NIL, dev->nodename,
94 "virtual-device", "%i", &vdevice);
95 if (err != 1) {
96 /* go looking in the extended area instead */
97 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
98 "%i", &vdevice);
99 if (err != 1) {
100 xenbus_dev_fatal(dev, err, "reading virtual-device");
101 return err;
102 }
103 }
105 info = kzalloc(sizeof(*info), GFP_KERNEL);
106 if (!info) {
107 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
108 return -ENOMEM;
109 }
111 info->xbdev = dev;
112 info->vdevice = vdevice;
113 info->connected = BLKIF_STATE_DISCONNECTED;
114 INIT_WORK(&info->work, blkif_restart_queue, (void *)info);
116 for (i = 0; i < BLK_RING_SIZE; i++)
117 info->shadow[i].req.id = i+1;
118 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
120 /* Front end dir is a number, which is used as the id. */
121 info->handle = simple_strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
122 dev->dev.driver_data = info;
124 err = talk_to_backend(dev, info);
125 if (err) {
126 kfree(info);
127 dev->dev.driver_data = NULL;
128 return err;
129 }
131 return 0;
132 }
135 /**
136 * We are reconnecting to the backend, due to a suspend/resume, or a backend
137 * driver restart. We tear down our blkif structure and recreate it, but
138 * leave the device-layer structures intact so that this is transparent to the
139 * rest of the kernel.
140 */
141 static int blkfront_resume(struct xenbus_device *dev)
142 {
143 struct blkfront_info *info = dev->dev.driver_data;
144 int err;
146 DPRINTK("blkfront_resume: %s\n", dev->nodename);
148 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
150 err = talk_to_backend(dev, info);
151 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
152 blkif_recover(info);
154 return err;
155 }
158 /* Common code used when first setting up, and when resuming. */
159 static int talk_to_backend(struct xenbus_device *dev,
160 struct blkfront_info *info)
161 {
162 const char *message = NULL;
163 struct xenbus_transaction xbt;
164 int err;
166 /* Create shared ring, alloc event channel. */
167 err = setup_blkring(dev, info);
168 if (err)
169 goto out;
171 again:
172 err = xenbus_transaction_start(&xbt);
173 if (err) {
174 xenbus_dev_fatal(dev, err, "starting transaction");
175 goto destroy_blkring;
176 }
178 err = xenbus_printf(xbt, dev->nodename,
179 "ring-ref","%u", info->ring_ref);
180 if (err) {
181 message = "writing ring-ref";
182 goto abort_transaction;
183 }
184 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
185 irq_to_evtchn_port(info->irq));
186 if (err) {
187 message = "writing event-channel";
188 goto abort_transaction;
189 }
190 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
191 XEN_IO_PROTO_ABI_NATIVE);
192 if (err) {
193 message = "writing protocol";
194 goto abort_transaction;
195 }
197 err = xenbus_transaction_end(xbt, 0);
198 if (err) {
199 if (err == -EAGAIN)
200 goto again;
201 xenbus_dev_fatal(dev, err, "completing transaction");
202 goto destroy_blkring;
203 }
205 xenbus_switch_state(dev, XenbusStateInitialised);
207 return 0;
209 abort_transaction:
210 xenbus_transaction_end(xbt, 1);
211 if (message)
212 xenbus_dev_fatal(dev, err, "%s", message);
213 destroy_blkring:
214 blkif_free(info, 0);
215 out:
216 return err;
217 }
220 static int setup_blkring(struct xenbus_device *dev,
221 struct blkfront_info *info)
222 {
223 blkif_sring_t *sring;
224 int err;
226 info->ring_ref = GRANT_INVALID_REF;
228 sring = (blkif_sring_t *)__get_free_page(GFP_NOIO | __GFP_HIGH);
229 if (!sring) {
230 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
231 return -ENOMEM;
232 }
233 SHARED_RING_INIT(sring);
234 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
236 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
238 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
239 if (err < 0) {
240 free_page((unsigned long)sring);
241 info->ring.sring = NULL;
242 goto fail;
243 }
244 info->ring_ref = err;
246 err = bind_listening_port_to_irqhandler(
247 dev->otherend_id, blkif_int, SA_SAMPLE_RANDOM, "blkif", info);
248 if (err <= 0) {
249 xenbus_dev_fatal(dev, err,
250 "bind_listening_port_to_irqhandler");
251 goto fail;
252 }
253 info->irq = err;
255 return 0;
256 fail:
257 blkif_free(info, 0);
258 return err;
259 }
262 /**
263 * Callback received when the backend's state changes.
264 */
265 static void backend_changed(struct xenbus_device *dev,
266 enum xenbus_state backend_state)
267 {
268 struct blkfront_info *info = dev->dev.driver_data;
269 struct block_device *bd;
271 DPRINTK("blkfront:backend_changed.\n");
273 switch (backend_state) {
274 case XenbusStateInitialising:
275 case XenbusStateInitWait:
276 case XenbusStateInitialised:
277 case XenbusStateReconfiguring:
278 case XenbusStateReconfigured:
279 case XenbusStateUnknown:
280 case XenbusStateClosed:
281 break;
283 case XenbusStateConnected:
284 connect(info);
285 break;
287 case XenbusStateClosing:
288 bd = bdget(info->dev);
289 if (bd == NULL)
290 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
292 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
293 down(&bd->bd_sem);
294 #else
295 mutex_lock(&bd->bd_mutex);
296 #endif
297 if (info->users > 0)
298 xenbus_dev_error(dev, -EBUSY,
299 "Device in use; refusing to close");
300 else
301 blkfront_closing(dev);
302 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
303 up(&bd->bd_sem);
304 #else
305 mutex_unlock(&bd->bd_mutex);
306 #endif
307 bdput(bd);
308 break;
309 }
310 }
313 /* ** Connection ** */
316 /*
317 * Invoked when the backend is finally 'ready' (and has told produced
318 * the details about the physical device - #sectors, size, etc).
319 */
320 static void connect(struct blkfront_info *info)
321 {
322 unsigned long long sectors;
323 unsigned long sector_size;
324 unsigned int binfo;
325 int err;
327 if ((info->connected == BLKIF_STATE_CONNECTED) ||
328 (info->connected == BLKIF_STATE_SUSPENDED) )
329 return;
331 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
333 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
334 "sectors", "%Lu", &sectors,
335 "info", "%u", &binfo,
336 "sector-size", "%lu", &sector_size,
337 NULL);
338 if (err) {
339 xenbus_dev_fatal(info->xbdev, err,
340 "reading backend fields at %s",
341 info->xbdev->otherend);
342 return;
343 }
345 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
346 "feature-barrier", "%lu", &info->feature_barrier,
347 NULL);
348 if (err)
349 info->feature_barrier = 0;
351 err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
352 if (err) {
353 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
354 info->xbdev->otherend);
355 return;
356 }
358 err = xlvbd_sysfs_addif(info);
359 if (err) {
360 xenbus_dev_fatal(info->xbdev, err, "xlvbd_sysfs_addif at %s",
361 info->xbdev->otherend);
362 return;
363 }
365 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
367 /* Kick pending requests. */
368 spin_lock_irq(&blkif_io_lock);
369 info->connected = BLKIF_STATE_CONNECTED;
370 kick_pending_request_queues(info);
371 spin_unlock_irq(&blkif_io_lock);
373 add_disk(info->gd);
375 info->is_ready = 1;
376 }
378 /**
379 * Handle the change of state of the backend to Closing. We must delete our
380 * device-layer structures now, to ensure that writes are flushed through to
381 * the backend. Once is this done, we can switch to Closed in
382 * acknowledgement.
383 */
384 static void blkfront_closing(struct xenbus_device *dev)
385 {
386 struct blkfront_info *info = dev->dev.driver_data;
387 unsigned long flags;
389 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
391 if (info->rq == NULL)
392 goto out;
394 spin_lock_irqsave(&blkif_io_lock, flags);
395 /* No more blkif_request(). */
396 blk_stop_queue(info->rq);
397 /* No more gnttab callback work. */
398 gnttab_cancel_free_callback(&info->callback);
399 spin_unlock_irqrestore(&blkif_io_lock, flags);
401 /* Flush gnttab callback work. Must be done with no locks held. */
402 flush_scheduled_work();
404 xlvbd_sysfs_delif(info);
406 xlvbd_del(info);
408 out:
409 xenbus_frontend_closed(dev);
410 }
413 static int blkfront_remove(struct xenbus_device *dev)
414 {
415 struct blkfront_info *info = dev->dev.driver_data;
417 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
419 blkif_free(info, 0);
421 kfree(info);
423 return 0;
424 }
427 static inline int GET_ID_FROM_FREELIST(
428 struct blkfront_info *info)
429 {
430 unsigned long free = info->shadow_free;
431 BUG_ON(free > BLK_RING_SIZE);
432 info->shadow_free = info->shadow[free].req.id;
433 info->shadow[free].req.id = 0x0fffffee; /* debug */
434 return free;
435 }
437 static inline void ADD_ID_TO_FREELIST(
438 struct blkfront_info *info, unsigned long id)
439 {
440 info->shadow[id].req.id = info->shadow_free;
441 info->shadow[id].request = 0;
442 info->shadow_free = id;
443 }
445 static inline void flush_requests(struct blkfront_info *info)
446 {
447 int notify;
449 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
451 if (notify)
452 notify_remote_via_irq(info->irq);
453 }
455 static void kick_pending_request_queues(struct blkfront_info *info)
456 {
457 if (!RING_FULL(&info->ring)) {
458 /* Re-enable calldowns. */
459 blk_start_queue(info->rq);
460 /* Kick things off immediately. */
461 do_blkif_request(info->rq);
462 }
463 }
465 static void blkif_restart_queue(void *arg)
466 {
467 struct blkfront_info *info = (struct blkfront_info *)arg;
468 spin_lock_irq(&blkif_io_lock);
469 if (info->connected == BLKIF_STATE_CONNECTED)
470 kick_pending_request_queues(info);
471 spin_unlock_irq(&blkif_io_lock);
472 }
474 static void blkif_restart_queue_callback(void *arg)
475 {
476 struct blkfront_info *info = (struct blkfront_info *)arg;
477 schedule_work(&info->work);
478 }
480 int blkif_open(struct inode *inode, struct file *filep)
481 {
482 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
483 info->users++;
484 return 0;
485 }
488 int blkif_release(struct inode *inode, struct file *filep)
489 {
490 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
491 info->users--;
492 if (info->users == 0) {
493 /* Check whether we have been instructed to close. We will
494 have ignored this request initially, as the device was
495 still mounted. */
496 struct xenbus_device * dev = info->xbdev;
497 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
499 if (state == XenbusStateClosing && info->is_ready)
500 blkfront_closing(dev);
501 }
502 return 0;
503 }
506 int blkif_ioctl(struct inode *inode, struct file *filep,
507 unsigned command, unsigned long argument)
508 {
509 int i;
511 DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
512 command, (long)argument, inode->i_rdev);
514 switch (command) {
515 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
516 case HDIO_GETGEO: {
517 struct block_device *bd = inode->i_bdev;
518 struct hd_geometry geo;
519 int ret;
521 if (!argument)
522 return -EINVAL;
524 geo.start = get_start_sect(bd);
525 ret = blkif_getgeo(bd, &geo);
526 if (ret)
527 return ret;
529 if (copy_to_user((struct hd_geometry __user *)argument, &geo,
530 sizeof(geo)))
531 return -EFAULT;
533 return 0;
534 }
535 #endif
536 case CDROMMULTISESSION:
537 DPRINTK("FIXME: support multisession CDs later\n");
538 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
539 if (put_user(0, (char __user *)(argument + i)))
540 return -EFAULT;
541 return 0;
543 case CDROM_GET_CAPABILITY: {
544 struct blkfront_info *info =
545 inode->i_bdev->bd_disk->private_data;
546 struct gendisk *gd = info->gd;
547 if (gd->flags & GENHD_FL_CD)
548 return 0;
549 return -EINVAL;
550 }
551 default:
552 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
553 command);*/
554 return -EINVAL; /* same return as native Linux */
555 }
557 return 0;
558 }
561 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
562 {
563 /* We don't have real geometry info, but let's at least return
564 values consistent with the size of the device */
565 sector_t nsect = get_capacity(bd->bd_disk);
566 sector_t cylinders = nsect;
568 hg->heads = 0xff;
569 hg->sectors = 0x3f;
570 sector_div(cylinders, hg->heads * hg->sectors);
571 hg->cylinders = cylinders;
572 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
573 hg->cylinders = 0xffff;
574 return 0;
575 }
578 /*
579 * blkif_queue_request
580 *
581 * request block io
582 *
583 * id: for guest use only.
584 * operation: BLKIF_OP_{READ,WRITE,PROBE}
585 * buffer: buffer to read/write into. this should be a
586 * virtual address in the guest os.
587 */
588 static int blkif_queue_request(struct request *req)
589 {
590 struct blkfront_info *info = req->rq_disk->private_data;
591 unsigned long buffer_mfn;
592 blkif_request_t *ring_req;
593 unsigned long id;
594 unsigned int fsect, lsect;
595 int i, ref;
596 grant_ref_t gref_head;
597 struct scatterlist *sg;
599 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
600 return 1;
602 if (gnttab_alloc_grant_references(
603 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
604 gnttab_request_free_callback(
605 &info->callback,
606 blkif_restart_queue_callback,
607 info,
608 BLKIF_MAX_SEGMENTS_PER_REQUEST);
609 return 1;
610 }
612 /* Fill out a communications ring structure. */
613 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
614 id = GET_ID_FROM_FREELIST(info);
615 info->shadow[id].request = (unsigned long)req;
617 ring_req->id = id;
618 ring_req->sector_number = (blkif_sector_t)req->sector;
619 ring_req->handle = info->handle;
621 ring_req->operation = rq_data_dir(req) ?
622 BLKIF_OP_WRITE : BLKIF_OP_READ;
623 if (blk_barrier_rq(req))
624 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
626 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
627 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
628 for (i = 0; i < ring_req->nr_segments; ++i) {
629 sg = info->sg + i;
630 buffer_mfn = page_to_phys(sg->page) >> PAGE_SHIFT;
631 fsect = sg->offset >> 9;
632 lsect = fsect + (sg->length >> 9) - 1;
633 /* install a grant reference. */
634 ref = gnttab_claim_grant_reference(&gref_head);
635 BUG_ON(ref == -ENOSPC);
637 gnttab_grant_foreign_access_ref(
638 ref,
639 info->xbdev->otherend_id,
640 buffer_mfn,
641 rq_data_dir(req) ? GTF_readonly : 0 );
643 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
644 ring_req->seg[i] =
645 (struct blkif_request_segment) {
646 .gref = ref,
647 .first_sect = fsect,
648 .last_sect = lsect };
649 }
651 info->ring.req_prod_pvt++;
653 /* Keep a private copy so we can reissue requests when recovering. */
654 info->shadow[id].req = *ring_req;
656 gnttab_free_grant_references(gref_head);
658 return 0;
659 }
661 /*
662 * do_blkif_request
663 * read a block; request is in a request queue
664 */
665 void do_blkif_request(request_queue_t *rq)
666 {
667 struct blkfront_info *info = NULL;
668 struct request *req;
669 int queued;
671 DPRINTK("Entered do_blkif_request\n");
673 queued = 0;
675 while ((req = elv_next_request(rq)) != NULL) {
676 info = req->rq_disk->private_data;
677 if (!blk_fs_request(req)) {
678 end_request(req, 0);
679 continue;
680 }
682 if (RING_FULL(&info->ring))
683 goto wait;
685 DPRINTK("do_blk_req %p: cmd %p, sec %llx, "
686 "(%u/%li) buffer:%p [%s]\n",
687 req, req->cmd, (long long)req->sector,
688 req->current_nr_sectors,
689 req->nr_sectors, req->buffer,
690 rq_data_dir(req) ? "write" : "read");
693 blkdev_dequeue_request(req);
694 if (blkif_queue_request(req)) {
695 blk_requeue_request(rq, req);
696 wait:
697 /* Avoid pointless unplugs. */
698 blk_stop_queue(rq);
699 break;
700 }
702 queued++;
703 }
705 if (queued != 0)
706 flush_requests(info);
707 }
710 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs)
711 {
712 struct request *req;
713 blkif_response_t *bret;
714 RING_IDX i, rp;
715 unsigned long flags;
716 struct blkfront_info *info = (struct blkfront_info *)dev_id;
717 int uptodate;
719 spin_lock_irqsave(&blkif_io_lock, flags);
721 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
722 spin_unlock_irqrestore(&blkif_io_lock, flags);
723 return IRQ_HANDLED;
724 }
726 again:
727 rp = info->ring.sring->rsp_prod;
728 rmb(); /* Ensure we see queued responses up to 'rp'. */
730 for (i = info->ring.rsp_cons; i != rp; i++) {
731 unsigned long id;
732 int ret;
734 bret = RING_GET_RESPONSE(&info->ring, i);
735 id = bret->id;
736 req = (struct request *)info->shadow[id].request;
738 blkif_completion(&info->shadow[id]);
740 ADD_ID_TO_FREELIST(info, id);
742 uptodate = (bret->status == BLKIF_RSP_OKAY);
743 switch (bret->operation) {
744 case BLKIF_OP_WRITE_BARRIER:
745 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
746 printk("blkfront: %s: write barrier op failed\n",
747 info->gd->disk_name);
748 uptodate = -EOPNOTSUPP;
749 info->feature_barrier = 0;
750 xlvbd_barrier(info);
751 }
752 /* fall through */
753 case BLKIF_OP_READ:
754 case BLKIF_OP_WRITE:
755 if (unlikely(bret->status != BLKIF_RSP_OKAY))
756 DPRINTK("Bad return from blkdev data "
757 "request: %x\n", bret->status);
759 ret = end_that_request_first(req, uptodate,
760 req->hard_nr_sectors);
761 BUG_ON(ret);
762 end_that_request_last(req, uptodate);
763 break;
764 default:
765 BUG();
766 }
767 }
769 info->ring.rsp_cons = i;
771 if (i != info->ring.req_prod_pvt) {
772 int more_to_do;
773 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
774 if (more_to_do)
775 goto again;
776 } else
777 info->ring.sring->rsp_event = i + 1;
779 kick_pending_request_queues(info);
781 spin_unlock_irqrestore(&blkif_io_lock, flags);
783 return IRQ_HANDLED;
784 }
786 static void blkif_free(struct blkfront_info *info, int suspend)
787 {
788 /* Prevent new requests being issued until we fix things up. */
789 spin_lock_irq(&blkif_io_lock);
790 info->connected = suspend ?
791 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
792 /* No more blkif_request(). */
793 if (info->rq)
794 blk_stop_queue(info->rq);
795 /* No more gnttab callback work. */
796 gnttab_cancel_free_callback(&info->callback);
797 spin_unlock_irq(&blkif_io_lock);
799 /* Flush gnttab callback work. Must be done with no locks held. */
800 flush_scheduled_work();
802 /* Free resources associated with old device channel. */
803 if (info->ring_ref != GRANT_INVALID_REF) {
804 gnttab_end_foreign_access(info->ring_ref,
805 (unsigned long)info->ring.sring);
806 info->ring_ref = GRANT_INVALID_REF;
807 info->ring.sring = NULL;
808 }
809 if (info->irq)
810 unbind_from_irqhandler(info->irq, info);
811 info->irq = 0;
812 }
814 static void blkif_completion(struct blk_shadow *s)
815 {
816 int i;
817 for (i = 0; i < s->req.nr_segments; i++)
818 gnttab_end_foreign_access(s->req.seg[i].gref, 0UL);
819 }
821 static void blkif_recover(struct blkfront_info *info)
822 {
823 int i;
824 blkif_request_t *req;
825 struct blk_shadow *copy;
826 int j;
828 /* Stage 1: Make a safe copy of the shadow state. */
829 copy = kmalloc(sizeof(info->shadow), GFP_NOIO | __GFP_NOFAIL | __GFP_HIGH);
830 memcpy(copy, info->shadow, sizeof(info->shadow));
832 /* Stage 2: Set up free list. */
833 memset(&info->shadow, 0, sizeof(info->shadow));
834 for (i = 0; i < BLK_RING_SIZE; i++)
835 info->shadow[i].req.id = i+1;
836 info->shadow_free = info->ring.req_prod_pvt;
837 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
839 /* Stage 3: Find pending requests and requeue them. */
840 for (i = 0; i < BLK_RING_SIZE; i++) {
841 /* Not in use? */
842 if (copy[i].request == 0)
843 continue;
845 /* Grab a request slot and copy shadow state into it. */
846 req = RING_GET_REQUEST(
847 &info->ring, info->ring.req_prod_pvt);
848 *req = copy[i].req;
850 /* We get a new request id, and must reset the shadow state. */
851 req->id = GET_ID_FROM_FREELIST(info);
852 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
854 /* Rewrite any grant references invalidated by susp/resume. */
855 for (j = 0; j < req->nr_segments; j++)
856 gnttab_grant_foreign_access_ref(
857 req->seg[j].gref,
858 info->xbdev->otherend_id,
859 pfn_to_mfn(info->shadow[req->id].frame[j]),
860 rq_data_dir((struct request *)
861 info->shadow[req->id].request) ?
862 GTF_readonly : 0);
863 info->shadow[req->id].req = *req;
865 info->ring.req_prod_pvt++;
866 }
868 kfree(copy);
870 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
872 spin_lock_irq(&blkif_io_lock);
874 /* Now safe for us to use the shared ring */
875 info->connected = BLKIF_STATE_CONNECTED;
877 /* Send off requeued requests */
878 flush_requests(info);
880 /* Kick any other new requests queued since we resumed */
881 kick_pending_request_queues(info);
883 spin_unlock_irq(&blkif_io_lock);
884 }
886 int blkfront_is_ready(struct xenbus_device *dev)
887 {
888 struct blkfront_info *info = dev->dev.driver_data;
890 return info->is_ready;
891 }
894 /* ** Driver Registration ** */
897 static const struct xenbus_device_id blkfront_ids[] = {
898 { "vbd" },
899 { "" }
900 };
901 MODULE_ALIAS("xen:vbd");
903 static struct xenbus_driver blkfront = {
904 .name = "vbd",
905 .owner = THIS_MODULE,
906 .ids = blkfront_ids,
907 .probe = blkfront_probe,
908 .remove = blkfront_remove,
909 .resume = blkfront_resume,
910 .otherend_changed = backend_changed,
911 .is_ready = blkfront_is_ready,
912 };
915 static int __init xlblk_init(void)
916 {
917 if (!is_running_on_xen())
918 return -ENODEV;
920 return xenbus_register_frontend(&blkfront);
921 }
922 module_init(xlblk_init);
925 static void __exit xlblk_exit(void)
926 {
927 return xenbus_unregister_driver(&blkfront);
928 }
929 module_exit(xlblk_exit);
931 MODULE_LICENSE("Dual BSD/GPL");