fedora core 6 1.2949 + vserver 2.2.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);
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);
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);
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 long sectors;
298         unsigned long sector_size;
299         unsigned int binfo;
300         int err;
301
302         if ((info->connected == BLKIF_STATE_CONNECTED) ||
303             (info->connected == BLKIF_STATE_SUSPENDED) )
304                 return;
305
306         DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
307
308         err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
309                             "sectors", "%llu", &sectors,
310                             "info", "%u", &binfo,
311                             "sector-size", "%lu", &sector_size,
312                             NULL);
313         if (err) {
314                 xenbus_dev_fatal(info->xbdev, err,
315                                  "reading backend fields at %s",
316                                  info->xbdev->otherend);
317                 return;
318         }
319
320         err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
321         if (err) {
322                 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
323                                  info->xbdev->otherend);
324                 return;
325         }
326
327         (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
328
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);
334
335         add_disk(info->gd);
336 }
337
338 /**
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
342  * acknowledgement.
343  */
344 static void blkfront_closing(struct xenbus_device *dev)
345 {
346         struct blkfront_info *info = dev->dev.driver_data;
347         unsigned long flags;
348
349         DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
350
351         if (info->rq == NULL)
352                 goto out;
353
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);
360
361         /* Flush gnttab callback work. Must be done with no locks held. */
362         flush_scheduled_work();
363
364         xlvbd_del(info);
365
366 out:
367         xenbus_frontend_closed(dev);
368 }
369
370
371 static int blkfront_remove(struct xenbus_device *dev)
372 {
373         struct blkfront_info *info = dev->dev.driver_data;
374
375         DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
376
377         blkif_free(info, 0);
378
379         kfree(info);
380
381         return 0;
382 }
383
384
385 static inline int GET_ID_FROM_FREELIST(
386         struct blkfront_info *info)
387 {
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 */
392         return free;
393 }
394
395 static inline void ADD_ID_TO_FREELIST(
396         struct blkfront_info *info, unsigned long id)
397 {
398         info->shadow[id].req.id  = info->shadow_free;
399         info->shadow[id].request = 0;
400         info->shadow_free = id;
401 }
402
403 static inline void flush_requests(struct blkfront_info *info)
404 {
405         int notify;
406
407         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
408
409         if (notify)
410                 notify_remote_via_irq(info->irq);
411 }
412
413 static void kick_pending_request_queues(struct blkfront_info *info)
414 {
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);
420         }
421 }
422
423 static void blkif_restart_queue(struct work_struct *work)
424 {
425         struct blkfront_info *info = container_of(work, struct blkfront_info, work);
426
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);
431 }
432
433 static void blkif_restart_queue_callback(void *arg)
434 {
435         struct blkfront_info *info = (struct blkfront_info *)arg;
436         schedule_work(&info->work);
437 }
438
439 int blkif_open(struct inode *inode, struct file *filep)
440 {
441         struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
442         info->users++;
443         return 0;
444 }
445
446
447 int blkif_release(struct inode *inode, struct file *filep)
448 {
449         struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
450         info->users--;
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
454                    still mounted. */
455                 struct xenbus_device * dev = info->xbdev;
456                 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
457
458                 if (state == XenbusStateClosing)
459                         blkfront_closing(dev);
460         }
461         return 0;
462 }
463
464
465 int blkif_ioctl(struct inode *inode, struct file *filep,
466                 unsigned command, unsigned long argument)
467 {
468         int i;
469
470         DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
471                       command, (long)argument, inode->i_rdev);
472
473         switch (command) {
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)))
478                                 return -EFAULT;
479                 return 0;
480
481         default:
482                 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
483                   command);*/
484                 return -EINVAL; /* same return as native Linux */
485         }
486
487         return 0;
488 }
489
490
491 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
492 {
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;
497
498         hg->heads = 0xff;
499         hg->sectors = 0x3f;
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;
504         return 0;
505 }
506
507
508 /*
509  * blkif_queue_request
510  *
511  * request block io
512  *
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.
517  */
518 static int blkif_queue_request(struct request *req)
519 {
520         struct blkfront_info *info = req->rq_disk->private_data;
521         unsigned long buffer_mfn;
522         blkif_request_t *ring_req;
523         struct bio *bio;
524         struct bio_vec *bvec;
525         int idx;
526         unsigned long id;
527         unsigned int fsect, lsect;
528         int ref;
529         grant_ref_t gref_head;
530
531         if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
532                 return 1;
533
534         if (gnttab_alloc_grant_references(
535                 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
536                 gnttab_request_free_callback(
537                         &info->callback,
538                         blkif_restart_queue_callback,
539                         info,
540                         BLKIF_MAX_SEGMENTS_PER_REQUEST);
541                 return 1;
542         }
543
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;
548
549         ring_req->id = id;
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;
554
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);
566
567                         gnttab_grant_foreign_access_ref(
568                                 ref,
569                                 info->xbdev->otherend_id,
570                                 buffer_mfn,
571                                 rq_data_dir(req) );
572
573                         info->shadow[id].frame[ring_req->nr_segments] =
574                                 mfn_to_pfn(buffer_mfn);
575
576                         ring_req->seg[ring_req->nr_segments] =
577                                 (struct blkif_request_segment) {
578                                         .gref       = ref,
579                                         .first_sect = fsect,
580                                         .last_sect  = lsect };
581
582                         ring_req->nr_segments++;
583                 }
584         }
585
586         info->ring.req_prod_pvt++;
587
588         /* Keep a private copy so we can reissue requests when recovering. */
589         info->shadow[id].req = *ring_req;
590
591         gnttab_free_grant_references(gref_head);
592
593         return 0;
594 }
595
596 /*
597  * do_blkif_request
598  *  read a block; request is in a request queue
599  */
600 void do_blkif_request(request_queue_t *rq)
601 {
602         struct blkfront_info *info = NULL;
603         struct request *req;
604         int queued;
605
606         DPRINTK("Entered do_blkif_request\n");
607
608         queued = 0;
609
610         while ((req = elv_next_request(rq)) != NULL) {
611                 info = req->rq_disk->private_data;
612                 if (!blk_fs_request(req)) {
613                         end_request(req, 0);
614                         continue;
615                 }
616
617                 if (RING_FULL(&info->ring))
618                         goto wait;
619
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");
625
626
627                 blkdev_dequeue_request(req);
628                 if (blkif_queue_request(req)) {
629                         blk_requeue_request(rq, req);
630                 wait:
631                         /* Avoid pointless unplugs. */
632                         blk_stop_queue(rq);
633                         break;
634                 }
635
636                 queued++;
637         }
638
639         if (queued != 0)
640                 flush_requests(info);
641 }
642
643
644 static irqreturn_t blkif_int(int irq, void *dev_id)
645 {
646         struct request *req;
647         blkif_response_t *bret;
648         RING_IDX i, rp;
649         unsigned long flags;
650         struct blkfront_info *info = (struct blkfront_info *)dev_id;
651
652         spin_lock_irqsave(&blkif_io_lock, flags);
653
654         if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
655                 spin_unlock_irqrestore(&blkif_io_lock, flags);
656                 return IRQ_HANDLED;
657         }
658
659  again:
660         rp = info->ring.sring->rsp_prod;
661         rmb(); /* Ensure we see queued responses up to 'rp'. */
662
663         for (i = info->ring.rsp_cons; i != rp; i++) {
664                 unsigned long id;
665                 int ret;
666
667                 bret = RING_GET_RESPONSE(&info->ring, i);
668                 id   = bret->id;
669                 req  = (struct request *)info->shadow[id].request;
670
671                 blkif_completion(&info->shadow[id]);
672
673                 ADD_ID_TO_FREELIST(info, id);
674
675                 switch (bret->operation) {
676                 case BLKIF_OP_READ:
677                 case BLKIF_OP_WRITE:
678                         if (unlikely(bret->status != BLKIF_RSP_OKAY))
679                                 DPRINTK("Bad return from blkdev data "
680                                         "request: %x\n", bret->status);
681
682                         ret = end_that_request_first(
683                                 req, (bret->status == BLKIF_RSP_OKAY),
684                                 req->hard_nr_sectors);
685                         BUG_ON(ret);
686                         end_that_request_last(
687                                 req, (bret->status == BLKIF_RSP_OKAY));
688                         break;
689                 default:
690                         BUG();
691                 }
692         }
693
694         info->ring.rsp_cons = i;
695
696         if (i != info->ring.req_prod_pvt) {
697                 int more_to_do;
698                 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
699                 if (more_to_do)
700                         goto again;
701         } else
702                 info->ring.sring->rsp_event = i + 1;
703
704         kick_pending_request_queues(info);
705
706         spin_unlock_irqrestore(&blkif_io_lock, flags);
707
708         return IRQ_HANDLED;
709 }
710
711 static void blkif_free(struct blkfront_info *info, int suspend)
712 {
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(). */
718         if (info->rq)
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);
723
724         /* Flush gnttab callback work. Must be done with no locks held. */
725         flush_scheduled_work();
726
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;
733         }
734         if (info->irq)
735                 unbind_from_irqhandler(info->irq, info);
736         info->evtchn = info->irq = 0;
737
738 }
739
740 static void blkif_completion(struct blk_shadow *s)
741 {
742         int i;
743         for (i = 0; i < s->req.nr_segments; i++)
744                 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
745 }
746
747 static void blkif_recover(struct blkfront_info *info)
748 {
749         int i;
750         blkif_request_t *req;
751         struct blk_shadow *copy;
752         int j;
753
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));
757
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;
764
765         /* Stage 3: Find pending requests and requeue them. */
766         for (i = 0; i < BLK_RING_SIZE; i++) {
767                 /* Not in use? */
768                 if (copy[i].request == 0)
769                         continue;
770
771                 /* Grab a request slot and copy shadow state into it. */
772                 req = RING_GET_REQUEST(
773                         &info->ring, info->ring.req_prod_pvt);
774                 *req = copy[i].req;
775
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], &copy[i], sizeof(copy[i]));
779
780                 /* Rewrite any grant references invalidated by susp/resume. */
781                 for (j = 0; j < req->nr_segments; j++)
782                         gnttab_grant_foreign_access_ref(
783                                 req->seg[j].gref,
784                                 info->xbdev->otherend_id,
785                                 pfn_to_mfn(info->shadow[req->id].frame[j]),
786                                 rq_data_dir(
787                                         (struct request *)
788                                         info->shadow[req->id].request));
789                 info->shadow[req->id].req = *req;
790
791                 info->ring.req_prod_pvt++;
792         }
793
794         kfree(copy);
795
796         (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
797
798         spin_lock_irq(&blkif_io_lock);
799
800         /* Now safe for us to use the shared ring */
801         info->connected = BLKIF_STATE_CONNECTED;
802
803         /* Send off requeued requests */
804         flush_requests(info);
805
806         /* Kick any other new requests queued since we resumed */
807         kick_pending_request_queues(info);
808
809         spin_unlock_irq(&blkif_io_lock);
810 }
811
812
813 /* ** Driver Registration ** */
814
815
816 static struct xenbus_device_id blkfront_ids[] = {
817         { "vbd" },
818         { "" }
819 };
820
821
822 static struct xenbus_driver blkfront = {
823         .name = "vbd",
824         .owner = THIS_MODULE,
825         .ids = blkfront_ids,
826         .probe = blkfront_probe,
827         .remove = blkfront_remove,
828         .resume = blkfront_resume,
829         .otherend_changed = backend_changed,
830 };
831
832
833 static int __init xlblk_init(void)
834 {
835         if (!is_running_on_xen())
836                 return -ENODEV;
837
838         return xenbus_register_frontend(&blkfront);
839 }
840 module_init(xlblk_init);
841
842
843 static void xlblk_exit(void)
844 {
845         return xenbus_unregister_driver(&blkfront);
846 }
847 module_exit(xlblk_exit);
848
849 MODULE_LICENSE("Dual BSD/GPL");