Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / blkfront / blkfront.c
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  */
37
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 <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>
50
51 #define BLKIF_STATE_DISCONNECTED 0
52 #define BLKIF_STATE_CONNECTED    1
53 #define BLKIF_STATE_SUSPENDED    2
54
55 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
56     (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
57 #define GRANT_INVALID_REF       0
58
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 *);
64
65 static void kick_pending_request_queues(struct blkfront_info *);
66
67 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs);
68 static void blkif_restart_queue(void *arg);
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);
72
73
74 /**
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
78  * Initialised state.
79  */
80 static int blkfront_probe(struct xenbus_device *dev,
81                           const struct xenbus_device_id *id)
82 {
83         int err, vdevice, i;
84         struct blkfront_info *info;
85
86         /* FIXME: Use dynamic device id if this is not set. */
87         err = xenbus_scanf(XBT_NIL, dev->nodename,
88                            "virtual-device", "%i", &vdevice);
89         if (err != 1) {
90                 xenbus_dev_fatal(dev, err, "reading virtual-device");
91                 return err;
92         }
93
94         info = kzalloc(sizeof(*info), GFP_KERNEL);
95         if (!info) {
96                 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
97                 return -ENOMEM;
98         }
99
100         info->xbdev = dev;
101         info->vdevice = vdevice;
102         info->connected = BLKIF_STATE_DISCONNECTED;
103         INIT_WORK(&info->work, blkif_restart_queue, (void *)info);
104
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;
108
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;
112
113         err = talk_to_backend(dev, info);
114         if (err) {
115                 kfree(info);
116                 dev->dev.driver_data = NULL;
117                 return err;
118         }
119
120         return 0;
121 }
122
123
124 /**
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.
129  */
130 static int blkfront_resume(struct xenbus_device *dev)
131 {
132         struct blkfront_info *info = dev->dev.driver_data;
133         int err;
134
135         DPRINTK("blkfront_resume: %s\n", dev->nodename);
136
137         blkif_free(info, 1);
138
139         err = talk_to_backend(dev, info);
140         if (!err)
141                 blkif_recover(info);
142
143         return err;
144 }
145
146
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)
150 {
151         const char *message = NULL;
152         struct xenbus_transaction xbt;
153         int err;
154
155         /* Create shared ring, alloc event channel. */
156         err = setup_blkring(dev, info);
157         if (err)
158                 goto out;
159
160 again:
161         err = xenbus_transaction_start(&xbt);
162         if (err) {
163                 xenbus_dev_fatal(dev, err, "starting transaction");
164                 goto destroy_blkring;
165         }
166
167         err = xenbus_printf(xbt, dev->nodename,
168                             "ring-ref","%u", info->ring_ref);
169         if (err) {
170                 message = "writing ring-ref";
171                 goto abort_transaction;
172         }
173         err = xenbus_printf(xbt, dev->nodename,
174                             "event-channel", "%u", info->evtchn);
175         if (err) {
176                 message = "writing event-channel";
177                 goto abort_transaction;
178         }
179
180         err = xenbus_transaction_end(xbt, 0);
181         if (err) {
182                 if (err == -EAGAIN)
183                         goto again;
184                 xenbus_dev_fatal(dev, err, "completing transaction");
185                 goto destroy_blkring;
186         }
187
188         xenbus_switch_state(dev, XenbusStateInitialised);
189
190         return 0;
191
192  abort_transaction:
193         xenbus_transaction_end(xbt, 1);
194         if (message)
195                 xenbus_dev_fatal(dev, err, "%s", message);
196  destroy_blkring:
197         blkif_free(info, 0);
198  out:
199         return err;
200 }
201
202
203 static int setup_blkring(struct xenbus_device *dev,
204                          struct blkfront_info *info)
205 {
206         blkif_sring_t *sring;
207         int err;
208
209         info->ring_ref = GRANT_INVALID_REF;
210
211         sring = (blkif_sring_t *)__get_free_page(GFP_KERNEL);
212         if (!sring) {
213                 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
214                 return -ENOMEM;
215         }
216         SHARED_RING_INIT(sring);
217         FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
218
219         err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
220         if (err < 0) {
221                 free_page((unsigned long)sring);
222                 info->ring.sring = NULL;
223                 goto fail;
224         }
225         info->ring_ref = err;
226
227         err = xenbus_alloc_evtchn(dev, &info->evtchn);
228         if (err)
229                 goto fail;
230
231         err = bind_evtchn_to_irqhandler(
232                 info->evtchn, blkif_int, SA_SAMPLE_RANDOM, "blkif", info);
233         if (err <= 0) {
234                 xenbus_dev_fatal(dev, err,
235                                  "bind_evtchn_to_irqhandler failed");
236                 goto fail;
237         }
238         info->irq = err;
239
240         return 0;
241 fail:
242         blkif_free(info, 0);
243         return err;
244 }
245
246
247 /**
248  * Callback received when the backend's state changes.
249  */
250 static void backend_changed(struct xenbus_device *dev,
251                             enum xenbus_state backend_state)
252 {
253         struct blkfront_info *info = dev->dev.driver_data;
254         struct block_device *bd;
255
256         DPRINTK("blkfront:backend_changed.\n");
257
258         switch (backend_state) {
259         case XenbusStateInitialising:
260         case XenbusStateInitWait:
261         case XenbusStateInitialised:
262         case XenbusStateUnknown:
263         case XenbusStateClosed:
264                 break;
265
266         case XenbusStateConnected:
267                 connect(info);
268                 break;
269
270         case XenbusStateClosing:
271                 bd = bdget(info->dev);
272                 if (bd == NULL)
273                         xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
274
275                 mutex_lock(&bd->bd_mutex);
276                 if (info->users > 0)
277                         xenbus_dev_error(dev, -EBUSY,
278                                          "Device in use; refusing to close");
279                 else
280                         blkfront_closing(dev);
281                 mutex_unlock(&bd->bd_mutex);
282                 bdput(bd);
283                 break;
284         }
285 }
286
287
288 /* ** Connection ** */
289
290
291 /*
292  * Invoked when the backend is finally 'ready' (and has told produced
293  * the details about the physical device - #sectors, size, etc).
294  */
295 static void connect(struct blkfront_info *info)
296 {
297         unsigned long sectors, sector_size;
298         unsigned int binfo;
299         int err;
300
301         if ((info->connected == BLKIF_STATE_CONNECTED) ||
302             (info->connected == BLKIF_STATE_SUSPENDED) )
303                 return;
304
305         DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
306
307         err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
308                             "sectors", "%lu", &sectors,
309                             "info", "%u", &binfo,
310                             "sector-size", "%lu", &sector_size,
311                             NULL);
312         if (err) {
313                 xenbus_dev_fatal(info->xbdev, err,
314                                  "reading backend fields at %s",
315                                  info->xbdev->otherend);
316                 return;
317         }
318
319         err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
320         if (err) {
321                 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
322                                  info->xbdev->otherend);
323                 return;
324         }
325
326         (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
327
328         /* Kick pending requests. */
329         spin_lock_irq(&blkif_io_lock);
330         info->connected = BLKIF_STATE_CONNECTED;
331         kick_pending_request_queues(info);
332         spin_unlock_irq(&blkif_io_lock);
333
334         add_disk(info->gd);
335 }
336
337 /**
338  * Handle the change of state of the backend to Closing.  We must delete our
339  * device-layer structures now, to ensure that writes are flushed through to
340  * the backend.  Once is this done, we can switch to Closed in
341  * acknowledgement.
342  */
343 static void blkfront_closing(struct xenbus_device *dev)
344 {
345         struct blkfront_info *info = dev->dev.driver_data;
346         unsigned long flags;
347
348         DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
349
350         if (info->rq == NULL)
351                 return;
352
353         spin_lock_irqsave(&blkif_io_lock, flags);
354         /* No more blkif_request(). */
355         blk_stop_queue(info->rq);
356         /* No more gnttab callback work. */
357         gnttab_cancel_free_callback(&info->callback);
358         spin_unlock_irqrestore(&blkif_io_lock, flags);
359
360         /* Flush gnttab callback work. Must be done with no locks held. */
361         flush_scheduled_work();
362
363         xlvbd_del(info);
364
365         xenbus_frontend_closed(dev);
366 }
367
368
369 static int blkfront_remove(struct xenbus_device *dev)
370 {
371         struct blkfront_info *info = dev->dev.driver_data;
372
373         DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
374
375         blkif_free(info, 0);
376
377         kfree(info);
378
379         return 0;
380 }
381
382
383 static inline int GET_ID_FROM_FREELIST(
384         struct blkfront_info *info)
385 {
386         unsigned long free = info->shadow_free;
387         BUG_ON(free > BLK_RING_SIZE);
388         info->shadow_free = info->shadow[free].req.id;
389         info->shadow[free].req.id = 0x0fffffee; /* debug */
390         return free;
391 }
392
393 static inline void ADD_ID_TO_FREELIST(
394         struct blkfront_info *info, unsigned long id)
395 {
396         info->shadow[id].req.id  = info->shadow_free;
397         info->shadow[id].request = 0;
398         info->shadow_free = id;
399 }
400
401 static inline void flush_requests(struct blkfront_info *info)
402 {
403         int notify;
404
405         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
406
407         if (notify)
408                 notify_remote_via_irq(info->irq);
409 }
410
411 static void kick_pending_request_queues(struct blkfront_info *info)
412 {
413         if (!RING_FULL(&info->ring)) {
414                 /* Re-enable calldowns. */
415                 blk_start_queue(info->rq);
416                 /* Kick things off immediately. */
417                 do_blkif_request(info->rq);
418         }
419 }
420
421 static void blkif_restart_queue(void *arg)
422 {
423         struct blkfront_info *info = (struct blkfront_info *)arg;
424         spin_lock_irq(&blkif_io_lock);
425         if (info->connected == BLKIF_STATE_CONNECTED)
426                 kick_pending_request_queues(info);
427         spin_unlock_irq(&blkif_io_lock);
428 }
429
430 static void blkif_restart_queue_callback(void *arg)
431 {
432         struct blkfront_info *info = (struct blkfront_info *)arg;
433         schedule_work(&info->work);
434 }
435
436 int blkif_open(struct inode *inode, struct file *filep)
437 {
438         struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
439         info->users++;
440         return 0;
441 }
442
443
444 int blkif_release(struct inode *inode, struct file *filep)
445 {
446         struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
447         info->users--;
448         if (info->users == 0) {
449                 /* Check whether we have been instructed to close.  We will
450                    have ignored this request initially, as the device was
451                    still mounted. */
452                 struct xenbus_device * dev = info->xbdev;
453                 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
454
455                 if (state == XenbusStateClosing)
456                         blkfront_closing(dev);
457         }
458         return 0;
459 }
460
461
462 int blkif_ioctl(struct inode *inode, struct file *filep,
463                 unsigned command, unsigned long argument)
464 {
465         int i;
466
467         DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
468                       command, (long)argument, inode->i_rdev);
469
470         switch (command) {
471         case CDROMMULTISESSION:
472                 DPRINTK("FIXME: support multisession CDs later\n");
473                 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
474                         if (put_user(0, (char __user *)(argument + i)))
475                                 return -EFAULT;
476                 return 0;
477
478         default:
479                 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
480                   command);*/
481                 return -EINVAL; /* same return as native Linux */
482         }
483
484         return 0;
485 }
486
487
488 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
489 {
490         /* We don't have real geometry info, but let's at least return
491            values consistent with the size of the device */
492         sector_t nsect = get_capacity(bd->bd_disk);
493         sector_t cylinders = nsect;
494
495         hg->heads = 0xff;
496         hg->sectors = 0x3f;
497         sector_div(cylinders, hg->heads * hg->sectors);
498         hg->cylinders = cylinders;
499         if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
500                 hg->cylinders = 0xffff;
501         return 0;
502 }
503
504
505 /*
506  * blkif_queue_request
507  *
508  * request block io
509  *
510  * id: for guest use only.
511  * operation: BLKIF_OP_{READ,WRITE,PROBE}
512  * buffer: buffer to read/write into. this should be a
513  *   virtual address in the guest os.
514  */
515 static int blkif_queue_request(struct request *req)
516 {
517         struct blkfront_info *info = req->rq_disk->private_data;
518         unsigned long buffer_mfn;
519         blkif_request_t *ring_req;
520         struct bio *bio;
521         struct bio_vec *bvec;
522         int idx;
523         unsigned long id;
524         unsigned int fsect, lsect;
525         int ref;
526         grant_ref_t gref_head;
527
528         if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
529                 return 1;
530
531         if (gnttab_alloc_grant_references(
532                 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
533                 gnttab_request_free_callback(
534                         &info->callback,
535                         blkif_restart_queue_callback,
536                         info,
537                         BLKIF_MAX_SEGMENTS_PER_REQUEST);
538                 return 1;
539         }
540
541         /* Fill out a communications ring structure. */
542         ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
543         id = GET_ID_FROM_FREELIST(info);
544         info->shadow[id].request = (unsigned long)req;
545
546         ring_req->id = id;
547         ring_req->operation = rq_data_dir(req) ?
548                 BLKIF_OP_WRITE : BLKIF_OP_READ;
549         ring_req->sector_number = (blkif_sector_t)req->sector;
550         ring_req->handle = info->handle;
551
552         ring_req->nr_segments = 0;
553         rq_for_each_bio (bio, req) {
554                 bio_for_each_segment (bvec, bio, idx) {
555                         BUG_ON(ring_req->nr_segments
556                                == BLKIF_MAX_SEGMENTS_PER_REQUEST);
557                         buffer_mfn = page_to_phys(bvec->bv_page) >> PAGE_SHIFT;
558                         fsect = bvec->bv_offset >> 9;
559                         lsect = fsect + (bvec->bv_len >> 9) - 1;
560                         /* install a grant reference. */
561                         ref = gnttab_claim_grant_reference(&gref_head);
562                         BUG_ON(ref == -ENOSPC);
563
564                         gnttab_grant_foreign_access_ref(
565                                 ref,
566                                 info->xbdev->otherend_id,
567                                 buffer_mfn,
568                                 rq_data_dir(req) );
569
570                         info->shadow[id].frame[ring_req->nr_segments] =
571                                 mfn_to_pfn(buffer_mfn);
572
573                         ring_req->seg[ring_req->nr_segments] =
574                                 (struct blkif_request_segment) {
575                                         .gref       = ref,
576                                         .first_sect = fsect,
577                                         .last_sect  = lsect };
578
579                         ring_req->nr_segments++;
580                 }
581         }
582
583         info->ring.req_prod_pvt++;
584
585         /* Keep a private copy so we can reissue requests when recovering. */
586         info->shadow[id].req = *ring_req;
587
588         gnttab_free_grant_references(gref_head);
589
590         return 0;
591 }
592
593 /*
594  * do_blkif_request
595  *  read a block; request is in a request queue
596  */
597 void do_blkif_request(request_queue_t *rq)
598 {
599         struct blkfront_info *info = NULL;
600         struct request *req;
601         int queued;
602
603         DPRINTK("Entered do_blkif_request\n");
604
605         queued = 0;
606
607         while ((req = elv_next_request(rq)) != NULL) {
608                 info = req->rq_disk->private_data;
609                 if (!blk_fs_request(req)) {
610                         end_request(req, 0);
611                         continue;
612                 }
613
614                 if (RING_FULL(&info->ring))
615                         goto wait;
616
617                 DPRINTK("do_blk_req %p: cmd %p, sec %lx, "
618                         "(%u/%li) buffer:%p [%s]\n",
619                         req, req->cmd, req->sector, req->current_nr_sectors,
620                         req->nr_sectors, req->buffer,
621                         rq_data_dir(req) ? "write" : "read");
622
623
624                 blkdev_dequeue_request(req);
625                 if (blkif_queue_request(req)) {
626                         blk_requeue_request(rq, req);
627                 wait:
628                         /* Avoid pointless unplugs. */
629                         blk_stop_queue(rq);
630                         break;
631                 }
632
633                 queued++;
634         }
635
636         if (queued != 0)
637                 flush_requests(info);
638 }
639
640
641 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs)
642 {
643         struct request *req;
644         blkif_response_t *bret;
645         RING_IDX i, rp;
646         unsigned long flags;
647         struct blkfront_info *info = (struct blkfront_info *)dev_id;
648
649         spin_lock_irqsave(&blkif_io_lock, flags);
650
651         if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
652                 spin_unlock_irqrestore(&blkif_io_lock, flags);
653                 return IRQ_HANDLED;
654         }
655
656  again:
657         rp = info->ring.sring->rsp_prod;
658         rmb(); /* Ensure we see queued responses up to 'rp'. */
659
660         for (i = info->ring.rsp_cons; i != rp; i++) {
661                 unsigned long id;
662                 int ret;
663
664                 bret = RING_GET_RESPONSE(&info->ring, i);
665                 id   = bret->id;
666                 req  = (struct request *)info->shadow[id].request;
667
668                 blkif_completion(&info->shadow[id]);
669
670                 ADD_ID_TO_FREELIST(info, id);
671
672                 switch (bret->operation) {
673                 case BLKIF_OP_READ:
674                 case BLKIF_OP_WRITE:
675                         if (unlikely(bret->status != BLKIF_RSP_OKAY))
676                                 DPRINTK("Bad return from blkdev data "
677                                         "request: %x\n", bret->status);
678
679                         ret = end_that_request_first(
680                                 req, (bret->status == BLKIF_RSP_OKAY),
681                                 req->hard_nr_sectors);
682                         BUG_ON(ret);
683                         end_that_request_last(
684                                 req, (bret->status == BLKIF_RSP_OKAY));
685                         break;
686                 default:
687                         BUG();
688                 }
689         }
690
691         info->ring.rsp_cons = i;
692
693         if (i != info->ring.req_prod_pvt) {
694                 int more_to_do;
695                 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
696                 if (more_to_do)
697                         goto again;
698         } else
699                 info->ring.sring->rsp_event = i + 1;
700
701         kick_pending_request_queues(info);
702
703         spin_unlock_irqrestore(&blkif_io_lock, flags);
704
705         return IRQ_HANDLED;
706 }
707
708 static void blkif_free(struct blkfront_info *info, int suspend)
709 {
710         /* Prevent new requests being issued until we fix things up. */
711         spin_lock_irq(&blkif_io_lock);
712         info->connected = suspend ?
713                 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
714         /* No more blkif_request(). */
715         if (info->rq)
716                 blk_stop_queue(info->rq);
717         /* No more gnttab callback work. */
718         gnttab_cancel_free_callback(&info->callback);
719         spin_unlock_irq(&blkif_io_lock);
720
721         /* Flush gnttab callback work. Must be done with no locks held. */
722         flush_scheduled_work();
723
724         /* Free resources associated with old device channel. */
725         if (info->ring_ref != GRANT_INVALID_REF) {
726                 gnttab_end_foreign_access(info->ring_ref, 0,
727                                           (unsigned long)info->ring.sring);
728                 info->ring_ref = GRANT_INVALID_REF;
729                 info->ring.sring = NULL;
730         }
731         if (info->irq)
732                 unbind_from_irqhandler(info->irq, info);
733         info->evtchn = info->irq = 0;
734
735 }
736
737 static void blkif_completion(struct blk_shadow *s)
738 {
739         int i;
740         for (i = 0; i < s->req.nr_segments; i++)
741                 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
742 }
743
744 static void blkif_recover(struct blkfront_info *info)
745 {
746         int i;
747         blkif_request_t *req;
748         struct blk_shadow *copy;
749         int j;
750
751         /* Stage 1: Make a safe copy of the shadow state. */
752         copy = kmalloc(sizeof(info->shadow), GFP_KERNEL | __GFP_NOFAIL);
753         memcpy(copy, info->shadow, sizeof(info->shadow));
754
755         /* Stage 2: Set up free list. */
756         memset(&info->shadow, 0, sizeof(info->shadow));
757         for (i = 0; i < BLK_RING_SIZE; i++)
758                 info->shadow[i].req.id = i+1;
759         info->shadow_free = info->ring.req_prod_pvt;
760         info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
761
762         /* Stage 3: Find pending requests and requeue them. */
763         for (i = 0; i < BLK_RING_SIZE; i++) {
764                 /* Not in use? */
765                 if (copy[i].request == 0)
766                         continue;
767
768                 /* Grab a request slot and copy shadow state into it. */
769                 req = RING_GET_REQUEST(
770                         &info->ring, info->ring.req_prod_pvt);
771                 *req = copy[i].req;
772
773                 /* We get a new request id, and must reset the shadow state. */
774                 req->id = GET_ID_FROM_FREELIST(info);
775                 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
776
777                 /* Rewrite any grant references invalidated by susp/resume. */
778                 for (j = 0; j < req->nr_segments; j++)
779                         gnttab_grant_foreign_access_ref(
780                                 req->seg[j].gref,
781                                 info->xbdev->otherend_id,
782                                 pfn_to_mfn(info->shadow[req->id].frame[j]),
783                                 rq_data_dir(
784                                         (struct request *)
785                                         info->shadow[req->id].request));
786                 info->shadow[req->id].req = *req;
787
788                 info->ring.req_prod_pvt++;
789         }
790
791         kfree(copy);
792
793         (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
794
795         spin_lock_irq(&blkif_io_lock);
796
797         /* Now safe for us to use the shared ring */
798         info->connected = BLKIF_STATE_CONNECTED;
799
800         /* Send off requeued requests */
801         flush_requests(info);
802
803         /* Kick any other new requests queued since we resumed */
804         kick_pending_request_queues(info);
805
806         spin_unlock_irq(&blkif_io_lock);
807 }
808
809
810 /* ** Driver Registration ** */
811
812
813 static struct xenbus_device_id blkfront_ids[] = {
814         { "vbd" },
815         { "" }
816 };
817
818
819 static struct xenbus_driver blkfront = {
820         .name = "vbd",
821         .owner = THIS_MODULE,
822         .ids = blkfront_ids,
823         .probe = blkfront_probe,
824         .remove = blkfront_remove,
825         .resume = blkfront_resume,
826         .otherend_changed = backend_changed,
827 };
828
829
830 static int __init xlblk_init(void)
831 {
832         if (!is_running_on_xen())
833                 return -ENODEV;
834
835         return xenbus_register_frontend(&blkfront);
836 }
837 module_init(xlblk_init);
838
839
840 static void xlblk_exit(void)
841 {
842         return xenbus_unregister_driver(&blkfront);
843 }
844 module_exit(xlblk_exit);
845
846 MODULE_LICENSE("Dual BSD/GPL");