xen/blkfront: change blk_shadow.request to proper pointer
[linux-2.6-block.git] / drivers / block / xen-blkfront.c
CommitLineData
9f27ee59
JF
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/interrupt.h>
39#include <linux/blkdev.h>
597592d9 40#include <linux/hdreg.h>
440a01a7 41#include <linux/cdrom.h>
9f27ee59 42#include <linux/module.h>
5a0e3ad6 43#include <linux/slab.h>
2a48fc0a 44#include <linux/mutex.h>
9e973e64 45#include <linux/scatterlist.h>
9f27ee59 46
1ccbf534 47#include <xen/xen.h>
9f27ee59
JF
48#include <xen/xenbus.h>
49#include <xen/grant_table.h>
50#include <xen/events.h>
51#include <xen/page.h>
c1c5413a 52#include <xen/platform_pci.h>
9f27ee59
JF
53
54#include <xen/interface/grant_table.h>
55#include <xen/interface/io/blkif.h>
3e334239 56#include <xen/interface/io/protocols.h>
9f27ee59
JF
57
58#include <asm/xen/hypervisor.h>
59
60enum blkif_state {
61 BLKIF_STATE_DISCONNECTED,
62 BLKIF_STATE_CONNECTED,
63 BLKIF_STATE_SUSPENDED,
64};
65
66struct blk_shadow {
67 struct blkif_request req;
a945b980 68 struct request *request;
9f27ee59
JF
69 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
70};
71
2a48fc0a 72static DEFINE_MUTEX(blkfront_mutex);
83d5cde4 73static const struct block_device_operations xlvbd_block_fops;
9f27ee59
JF
74
75#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
76
77/*
78 * We have one of these per vbd, whether ide, scsi or 'other'. They
79 * hang in private_data off the gendisk structure. We may end up
80 * putting all kinds of interesting stuff here :-)
81 */
82struct blkfront_info
83{
b70f5fa0 84 struct mutex mutex;
9f27ee59 85 struct xenbus_device *xbdev;
9f27ee59
JF
86 struct gendisk *gd;
87 int vdevice;
88 blkif_vdev_t handle;
89 enum blkif_state connected;
90 int ring_ref;
91 struct blkif_front_ring ring;
9e973e64 92 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
9f27ee59
JF
93 unsigned int evtchn, irq;
94 struct request_queue *rq;
95 struct work_struct work;
96 struct gnttab_free_callback callback;
97 struct blk_shadow shadow[BLK_RING_SIZE];
98 unsigned long shadow_free;
4913efe4 99 unsigned int feature_flush;
1d78d705 100 int is_ready;
9f27ee59
JF
101};
102
103static DEFINE_SPINLOCK(blkif_io_lock);
104
0e345826
JB
105static unsigned int nr_minors;
106static unsigned long *minors;
107static DEFINE_SPINLOCK(minor_lock);
108
9f27ee59
JF
109#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
110 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
111#define GRANT_INVALID_REF 0
112
113#define PARTS_PER_DISK 16
9246b5f0 114#define PARTS_PER_EXT_DISK 256
9f27ee59
JF
115
116#define BLKIF_MAJOR(dev) ((dev)>>8)
117#define BLKIF_MINOR(dev) ((dev) & 0xff)
118
9246b5f0
CL
119#define EXT_SHIFT 28
120#define EXTENDED (1<<EXT_SHIFT)
121#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
122#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
9f27ee59 123
9246b5f0 124#define DEV_NAME "xvd" /* name in /dev */
9f27ee59
JF
125
126static int get_id_from_freelist(struct blkfront_info *info)
127{
128 unsigned long free = info->shadow_free;
b9ed7252 129 BUG_ON(free >= BLK_RING_SIZE);
9f27ee59
JF
130 info->shadow_free = info->shadow[free].req.id;
131 info->shadow[free].req.id = 0x0fffffee; /* debug */
132 return free;
133}
134
135static void add_id_to_freelist(struct blkfront_info *info,
136 unsigned long id)
137{
138 info->shadow[id].req.id = info->shadow_free;
a945b980 139 info->shadow[id].request = NULL;
9f27ee59
JF
140 info->shadow_free = id;
141}
142
0e345826
JB
143static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
144{
145 unsigned int end = minor + nr;
146 int rc;
147
148 if (end > nr_minors) {
149 unsigned long *bitmap, *old;
150
151 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
152 GFP_KERNEL);
153 if (bitmap == NULL)
154 return -ENOMEM;
155
156 spin_lock(&minor_lock);
157 if (end > nr_minors) {
158 old = minors;
159 memcpy(bitmap, minors,
160 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
161 minors = bitmap;
162 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
163 } else
164 old = bitmap;
165 spin_unlock(&minor_lock);
166 kfree(old);
167 }
168
169 spin_lock(&minor_lock);
170 if (find_next_bit(minors, end, minor) >= end) {
171 for (; minor < end; ++minor)
172 __set_bit(minor, minors);
173 rc = 0;
174 } else
175 rc = -EBUSY;
176 spin_unlock(&minor_lock);
177
178 return rc;
179}
180
181static void xlbd_release_minors(unsigned int minor, unsigned int nr)
182{
183 unsigned int end = minor + nr;
184
185 BUG_ON(end > nr_minors);
186 spin_lock(&minor_lock);
187 for (; minor < end; ++minor)
188 __clear_bit(minor, minors);
189 spin_unlock(&minor_lock);
190}
191
9f27ee59
JF
192static void blkif_restart_queue_callback(void *arg)
193{
194 struct blkfront_info *info = (struct blkfront_info *)arg;
195 schedule_work(&info->work);
196}
197
afe42d7d 198static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
597592d9
IC
199{
200 /* We don't have real geometry info, but let's at least return
201 values consistent with the size of the device */
202 sector_t nsect = get_capacity(bd->bd_disk);
203 sector_t cylinders = nsect;
204
205 hg->heads = 0xff;
206 hg->sectors = 0x3f;
207 sector_div(cylinders, hg->heads * hg->sectors);
208 hg->cylinders = cylinders;
209 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
210 hg->cylinders = 0xffff;
211 return 0;
212}
213
a63c848b 214static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
62aa0054 215 unsigned command, unsigned long argument)
440a01a7 216{
a63c848b 217 struct blkfront_info *info = bdev->bd_disk->private_data;
440a01a7
CL
218 int i;
219
220 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
221 command, (long)argument);
222
223 switch (command) {
224 case CDROMMULTISESSION:
225 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
226 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
227 if (put_user(0, (char __user *)(argument + i)))
228 return -EFAULT;
229 return 0;
230
231 case CDROM_GET_CAPABILITY: {
232 struct gendisk *gd = info->gd;
233 if (gd->flags & GENHD_FL_CD)
234 return 0;
235 return -EINVAL;
236 }
237
238 default:
239 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
240 command);*/
241 return -EINVAL; /* same return as native Linux */
242 }
243
244 return 0;
245}
246
9f27ee59 247/*
c64e38ea
JF
248 * Generate a Xen blkfront IO request from a blk layer request. Reads
249 * and writes are handled as expected. Since we lack a loose flush
250 * request, we map flushes into a full ordered barrier.
9f27ee59 251 *
c64e38ea 252 * @req: a request struct
9f27ee59
JF
253 */
254static int blkif_queue_request(struct request *req)
255{
256 struct blkfront_info *info = req->rq_disk->private_data;
257 unsigned long buffer_mfn;
258 struct blkif_request *ring_req;
9f27ee59
JF
259 unsigned long id;
260 unsigned int fsect, lsect;
9e973e64 261 int i, ref;
9f27ee59 262 grant_ref_t gref_head;
9e973e64 263 struct scatterlist *sg;
9f27ee59
JF
264
265 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
266 return 1;
267
268 if (gnttab_alloc_grant_references(
269 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
270 gnttab_request_free_callback(
271 &info->callback,
272 blkif_restart_queue_callback,
273 info,
274 BLKIF_MAX_SEGMENTS_PER_REQUEST);
275 return 1;
276 }
277
278 /* Fill out a communications ring structure. */
279 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
280 id = get_id_from_freelist(info);
a945b980 281 info->shadow[id].request = req;
9f27ee59
JF
282
283 ring_req->id = id;
83096ebf 284 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
9f27ee59
JF
285 ring_req->handle = info->handle;
286
287 ring_req->operation = rq_data_dir(req) ?
288 BLKIF_OP_WRITE : BLKIF_OP_READ;
c64e38ea 289 if (req->cmd_flags & REQ_FLUSH)
9f27ee59
JF
290 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
291
9e973e64
JA
292 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
293 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
294
295 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
296 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
297 fsect = sg->offset >> 9;
298 lsect = fsect + (sg->length >> 9) - 1;
6c92e699
JA
299 /* install a grant reference. */
300 ref = gnttab_claim_grant_reference(&gref_head);
301 BUG_ON(ref == -ENOSPC);
302
303 gnttab_grant_foreign_access_ref(
9f27ee59
JF
304 ref,
305 info->xbdev->otherend_id,
306 buffer_mfn,
307 rq_data_dir(req) );
308
9e973e64
JA
309 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
310 ring_req->seg[i] =
9f27ee59
JF
311 (struct blkif_request_segment) {
312 .gref = ref,
313 .first_sect = fsect,
314 .last_sect = lsect };
9f27ee59
JF
315 }
316
317 info->ring.req_prod_pvt++;
318
319 /* Keep a private copy so we can reissue requests when recovering. */
320 info->shadow[id].req = *ring_req;
321
322 gnttab_free_grant_references(gref_head);
323
324 return 0;
325}
326
327
328static inline void flush_requests(struct blkfront_info *info)
329{
330 int notify;
331
332 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
333
334 if (notify)
335 notify_remote_via_irq(info->irq);
336}
337
338/*
339 * do_blkif_request
340 * read a block; request is in a request queue
341 */
165125e1 342static void do_blkif_request(struct request_queue *rq)
9f27ee59
JF
343{
344 struct blkfront_info *info = NULL;
345 struct request *req;
346 int queued;
347
348 pr_debug("Entered do_blkif_request\n");
349
350 queued = 0;
351
9934c8c0 352 while ((req = blk_peek_request(rq)) != NULL) {
9f27ee59 353 info = req->rq_disk->private_data;
9f27ee59
JF
354
355 if (RING_FULL(&info->ring))
356 goto wait;
357
9934c8c0 358 blk_start_request(req);
296b2f6a 359
33659ebb 360 if (req->cmd_type != REQ_TYPE_FS) {
296b2f6a
TH
361 __blk_end_request_all(req, -EIO);
362 continue;
363 }
364
9f27ee59 365 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
83096ebf
TH
366 "(%u/%u) buffer:%p [%s]\n",
367 req, req->cmd, (unsigned long)blk_rq_pos(req),
368 blk_rq_cur_sectors(req), blk_rq_sectors(req),
369 req->buffer, rq_data_dir(req) ? "write" : "read");
9f27ee59 370
9f27ee59
JF
371 if (blkif_queue_request(req)) {
372 blk_requeue_request(rq, req);
373wait:
374 /* Avoid pointless unplugs. */
375 blk_stop_queue(rq);
376 break;
377 }
378
379 queued++;
380 }
381
382 if (queued != 0)
383 flush_requests(info);
384}
385
386static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
387{
165125e1 388 struct request_queue *rq;
9f27ee59
JF
389
390 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
391 if (rq == NULL)
392 return -1;
393
66d352e1 394 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
9f27ee59
JF
395
396 /* Hard sector size and max sectors impersonate the equiv. hardware. */
e1defc4f 397 blk_queue_logical_block_size(rq, sector_size);
086fa5ff 398 blk_queue_max_hw_sectors(rq, 512);
9f27ee59
JF
399
400 /* Each segment in a request is up to an aligned page in size. */
401 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
402 blk_queue_max_segment_size(rq, PAGE_SIZE);
403
404 /* Ensure a merged request will fit in a single I/O ring slot. */
8a78362c 405 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
9f27ee59
JF
406
407 /* Make sure buffer addresses are sector-aligned. */
408 blk_queue_dma_alignment(rq, 511);
409
1c91fe1a
IC
410 /* Make sure we don't use bounce buffers. */
411 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
412
9f27ee59
JF
413 gd->queue = rq;
414
415 return 0;
416}
417
418
4913efe4 419static void xlvbd_flush(struct blkfront_info *info)
9f27ee59 420{
4913efe4 421 blk_queue_flush(info->rq, info->feature_flush);
9f27ee59 422 printk(KERN_INFO "blkfront: %s: barriers %s\n",
4913efe4
TH
423 info->gd->disk_name,
424 info->feature_flush ? "enabled" : "disabled");
9f27ee59
JF
425}
426
427
9246b5f0
CL
428static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
429 struct blkfront_info *info,
430 u16 vdisk_info, u16 sector_size)
9f27ee59
JF
431{
432 struct gendisk *gd;
433 int nr_minors = 1;
434 int err = -ENODEV;
9246b5f0
CL
435 unsigned int offset;
436 int minor;
437 int nr_parts;
9f27ee59
JF
438
439 BUG_ON(info->gd != NULL);
440 BUG_ON(info->rq != NULL);
441
9246b5f0
CL
442 if ((info->vdevice>>EXT_SHIFT) > 1) {
443 /* this is above the extended range; something is wrong */
444 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
445 return -ENODEV;
446 }
447
448 if (!VDEV_IS_EXTENDED(info->vdevice)) {
449 minor = BLKIF_MINOR(info->vdevice);
450 nr_parts = PARTS_PER_DISK;
451 } else {
452 minor = BLKIF_MINOR_EXT(info->vdevice);
453 nr_parts = PARTS_PER_EXT_DISK;
454 }
455
456 if ((minor % nr_parts) == 0)
457 nr_minors = nr_parts;
9f27ee59 458
0e345826
JB
459 err = xlbd_reserve_minors(minor, nr_minors);
460 if (err)
461 goto out;
462 err = -ENODEV;
463
9f27ee59
JF
464 gd = alloc_disk(nr_minors);
465 if (gd == NULL)
0e345826 466 goto release;
9f27ee59 467
9246b5f0
CL
468 offset = minor / nr_parts;
469
470 if (nr_minors > 1) {
471 if (offset < 26)
472 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
473 else
474 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
475 'a' + ((offset / 26)-1), 'a' + (offset % 26));
476 } else {
477 if (offset < 26)
478 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
479 'a' + offset,
480 minor & (nr_parts - 1));
481 else
482 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
483 'a' + ((offset / 26) - 1),
484 'a' + (offset % 26),
485 minor & (nr_parts - 1));
486 }
9f27ee59
JF
487
488 gd->major = XENVBD_MAJOR;
489 gd->first_minor = minor;
490 gd->fops = &xlvbd_block_fops;
491 gd->private_data = info;
492 gd->driverfs_dev = &(info->xbdev->dev);
493 set_capacity(gd, capacity);
494
495 if (xlvbd_init_blk_queue(gd, sector_size)) {
496 del_gendisk(gd);
0e345826 497 goto release;
9f27ee59
JF
498 }
499
500 info->rq = gd->queue;
501 info->gd = gd;
502
4913efe4 503 xlvbd_flush(info);
9f27ee59
JF
504
505 if (vdisk_info & VDISK_READONLY)
506 set_disk_ro(gd, 1);
507
508 if (vdisk_info & VDISK_REMOVABLE)
509 gd->flags |= GENHD_FL_REMOVABLE;
510
511 if (vdisk_info & VDISK_CDROM)
512 gd->flags |= GENHD_FL_CD;
513
514 return 0;
515
0e345826
JB
516 release:
517 xlbd_release_minors(minor, nr_minors);
9f27ee59
JF
518 out:
519 return err;
520}
521
a66b5aeb
DS
522static void xlvbd_release_gendisk(struct blkfront_info *info)
523{
524 unsigned int minor, nr_minors;
525 unsigned long flags;
526
527 if (info->rq == NULL)
528 return;
529
530 spin_lock_irqsave(&blkif_io_lock, flags);
531
532 /* No more blkif_request(). */
533 blk_stop_queue(info->rq);
534
535 /* No more gnttab callback work. */
536 gnttab_cancel_free_callback(&info->callback);
537 spin_unlock_irqrestore(&blkif_io_lock, flags);
538
539 /* Flush gnttab callback work. Must be done with no locks held. */
540 flush_scheduled_work();
541
542 del_gendisk(info->gd);
543
544 minor = info->gd->first_minor;
545 nr_minors = info->gd->minors;
546 xlbd_release_minors(minor, nr_minors);
547
548 blk_cleanup_queue(info->rq);
549 info->rq = NULL;
550
551 put_disk(info->gd);
552 info->gd = NULL;
553}
554
9f27ee59
JF
555static void kick_pending_request_queues(struct blkfront_info *info)
556{
557 if (!RING_FULL(&info->ring)) {
558 /* Re-enable calldowns. */
559 blk_start_queue(info->rq);
560 /* Kick things off immediately. */
561 do_blkif_request(info->rq);
562 }
563}
564
565static void blkif_restart_queue(struct work_struct *work)
566{
567 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
568
569 spin_lock_irq(&blkif_io_lock);
570 if (info->connected == BLKIF_STATE_CONNECTED)
571 kick_pending_request_queues(info);
572 spin_unlock_irq(&blkif_io_lock);
573}
574
575static void blkif_free(struct blkfront_info *info, int suspend)
576{
577 /* Prevent new requests being issued until we fix things up. */
578 spin_lock_irq(&blkif_io_lock);
579 info->connected = suspend ?
580 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
581 /* No more blkif_request(). */
582 if (info->rq)
583 blk_stop_queue(info->rq);
584 /* No more gnttab callback work. */
585 gnttab_cancel_free_callback(&info->callback);
586 spin_unlock_irq(&blkif_io_lock);
587
588 /* Flush gnttab callback work. Must be done with no locks held. */
589 flush_scheduled_work();
590
591 /* Free resources associated with old device channel. */
592 if (info->ring_ref != GRANT_INVALID_REF) {
593 gnttab_end_foreign_access(info->ring_ref, 0,
594 (unsigned long)info->ring.sring);
595 info->ring_ref = GRANT_INVALID_REF;
596 info->ring.sring = NULL;
597 }
598 if (info->irq)
599 unbind_from_irqhandler(info->irq, info);
600 info->evtchn = info->irq = 0;
601
602}
603
604static void blkif_completion(struct blk_shadow *s)
605{
606 int i;
607 for (i = 0; i < s->req.nr_segments; i++)
608 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
609}
610
611static irqreturn_t blkif_interrupt(int irq, void *dev_id)
612{
613 struct request *req;
614 struct blkif_response *bret;
615 RING_IDX i, rp;
616 unsigned long flags;
617 struct blkfront_info *info = (struct blkfront_info *)dev_id;
f530f036 618 int error;
9f27ee59
JF
619
620 spin_lock_irqsave(&blkif_io_lock, flags);
621
622 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
623 spin_unlock_irqrestore(&blkif_io_lock, flags);
624 return IRQ_HANDLED;
625 }
626
627 again:
628 rp = info->ring.sring->rsp_prod;
629 rmb(); /* Ensure we see queued responses up to 'rp'. */
630
631 for (i = info->ring.rsp_cons; i != rp; i++) {
632 unsigned long id;
9f27ee59
JF
633
634 bret = RING_GET_RESPONSE(&info->ring, i);
635 id = bret->id;
a945b980 636 req = info->shadow[id].request;
9f27ee59
JF
637
638 blkif_completion(&info->shadow[id]);
639
640 add_id_to_freelist(info, id);
641
f530f036 642 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
9f27ee59
JF
643 switch (bret->operation) {
644 case BLKIF_OP_WRITE_BARRIER:
645 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
646 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
647 info->gd->disk_name);
f530f036 648 error = -EOPNOTSUPP;
4913efe4
TH
649 info->feature_flush = 0;
650 xlvbd_flush(info);
9f27ee59
JF
651 }
652 /* fall through */
653 case BLKIF_OP_READ:
654 case BLKIF_OP_WRITE:
655 if (unlikely(bret->status != BLKIF_RSP_OKAY))
656 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
657 "request: %x\n", bret->status);
658
40cbbb78 659 __blk_end_request_all(req, error);
9f27ee59
JF
660 break;
661 default:
662 BUG();
663 }
664 }
665
666 info->ring.rsp_cons = i;
667
668 if (i != info->ring.req_prod_pvt) {
669 int more_to_do;
670 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
671 if (more_to_do)
672 goto again;
673 } else
674 info->ring.sring->rsp_event = i + 1;
675
676 kick_pending_request_queues(info);
677
678 spin_unlock_irqrestore(&blkif_io_lock, flags);
679
680 return IRQ_HANDLED;
681}
682
683
684static int setup_blkring(struct xenbus_device *dev,
685 struct blkfront_info *info)
686{
687 struct blkif_sring *sring;
688 int err;
689
690 info->ring_ref = GRANT_INVALID_REF;
691
a144ff09 692 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
9f27ee59
JF
693 if (!sring) {
694 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
695 return -ENOMEM;
696 }
697 SHARED_RING_INIT(sring);
698 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
9e973e64
JA
699
700 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
9f27ee59
JF
701
702 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
703 if (err < 0) {
704 free_page((unsigned long)sring);
705 info->ring.sring = NULL;
706 goto fail;
707 }
708 info->ring_ref = err;
709
710 err = xenbus_alloc_evtchn(dev, &info->evtchn);
711 if (err)
712 goto fail;
713
714 err = bind_evtchn_to_irqhandler(info->evtchn,
715 blkif_interrupt,
716 IRQF_SAMPLE_RANDOM, "blkif", info);
717 if (err <= 0) {
718 xenbus_dev_fatal(dev, err,
719 "bind_evtchn_to_irqhandler failed");
720 goto fail;
721 }
722 info->irq = err;
723
724 return 0;
725fail:
726 blkif_free(info, 0);
727 return err;
728}
729
730
731/* Common code used when first setting up, and when resuming. */
203fd61f 732static int talk_to_blkback(struct xenbus_device *dev,
9f27ee59
JF
733 struct blkfront_info *info)
734{
735 const char *message = NULL;
736 struct xenbus_transaction xbt;
737 int err;
738
739 /* Create shared ring, alloc event channel. */
740 err = setup_blkring(dev, info);
741 if (err)
742 goto out;
743
744again:
745 err = xenbus_transaction_start(&xbt);
746 if (err) {
747 xenbus_dev_fatal(dev, err, "starting transaction");
748 goto destroy_blkring;
749 }
750
751 err = xenbus_printf(xbt, dev->nodename,
752 "ring-ref", "%u", info->ring_ref);
753 if (err) {
754 message = "writing ring-ref";
755 goto abort_transaction;
756 }
757 err = xenbus_printf(xbt, dev->nodename,
758 "event-channel", "%u", info->evtchn);
759 if (err) {
760 message = "writing event-channel";
761 goto abort_transaction;
762 }
3e334239
MA
763 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
764 XEN_IO_PROTO_ABI_NATIVE);
765 if (err) {
766 message = "writing protocol";
767 goto abort_transaction;
768 }
9f27ee59
JF
769
770 err = xenbus_transaction_end(xbt, 0);
771 if (err) {
772 if (err == -EAGAIN)
773 goto again;
774 xenbus_dev_fatal(dev, err, "completing transaction");
775 goto destroy_blkring;
776 }
777
778 xenbus_switch_state(dev, XenbusStateInitialised);
779
780 return 0;
781
782 abort_transaction:
783 xenbus_transaction_end(xbt, 1);
784 if (message)
785 xenbus_dev_fatal(dev, err, "%s", message);
786 destroy_blkring:
787 blkif_free(info, 0);
788 out:
789 return err;
790}
791
9f27ee59
JF
792/**
793 * Entry point to this code when a new device is created. Allocate the basic
794 * structures and the ring buffer for communication with the backend, and
795 * inform the backend of the appropriate details for those. Switch to
796 * Initialised state.
797 */
798static int blkfront_probe(struct xenbus_device *dev,
799 const struct xenbus_device_id *id)
800{
801 int err, vdevice, i;
802 struct blkfront_info *info;
803
804 /* FIXME: Use dynamic device id if this is not set. */
805 err = xenbus_scanf(XBT_NIL, dev->nodename,
806 "virtual-device", "%i", &vdevice);
807 if (err != 1) {
9246b5f0
CL
808 /* go looking in the extended area instead */
809 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
810 "%i", &vdevice);
811 if (err != 1) {
812 xenbus_dev_fatal(dev, err, "reading virtual-device");
813 return err;
814 }
9f27ee59
JF
815 }
816
b98a409b
SS
817 if (xen_hvm_domain()) {
818 char *type;
819 int len;
820 /* no unplug has been done: do not hook devices != xen vbds */
1dc7ce99 821 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
b98a409b
SS
822 int major;
823
824 if (!VDEV_IS_EXTENDED(vdevice))
825 major = BLKIF_MAJOR(vdevice);
826 else
827 major = XENVBD_MAJOR;
828
829 if (major != XENVBD_MAJOR) {
830 printk(KERN_INFO
831 "%s: HVM does not support vbd %d as xen block device\n",
832 __FUNCTION__, vdevice);
833 return -ENODEV;
834 }
835 }
836 /* do not create a PV cdrom device if we are an HVM guest */
837 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
838 if (IS_ERR(type))
839 return -ENODEV;
840 if (strncmp(type, "cdrom", 5) == 0) {
841 kfree(type);
c1c5413a
SS
842 return -ENODEV;
843 }
b98a409b 844 kfree(type);
c1c5413a 845 }
9f27ee59
JF
846 info = kzalloc(sizeof(*info), GFP_KERNEL);
847 if (!info) {
848 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
849 return -ENOMEM;
850 }
851
b70f5fa0 852 mutex_init(&info->mutex);
9f27ee59
JF
853 info->xbdev = dev;
854 info->vdevice = vdevice;
855 info->connected = BLKIF_STATE_DISCONNECTED;
856 INIT_WORK(&info->work, blkif_restart_queue);
857
858 for (i = 0; i < BLK_RING_SIZE; i++)
859 info->shadow[i].req.id = i+1;
860 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
861
862 /* Front end dir is a number, which is used as the id. */
863 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
a1b4b12b 864 dev_set_drvdata(&dev->dev, info);
9f27ee59 865
203fd61f 866 err = talk_to_blkback(dev, info);
9f27ee59
JF
867 if (err) {
868 kfree(info);
a1b4b12b 869 dev_set_drvdata(&dev->dev, NULL);
9f27ee59
JF
870 return err;
871 }
872
873 return 0;
874}
875
876
877static int blkif_recover(struct blkfront_info *info)
878{
879 int i;
880 struct blkif_request *req;
881 struct blk_shadow *copy;
882 int j;
883
884 /* Stage 1: Make a safe copy of the shadow state. */
a144ff09
IC
885 copy = kmalloc(sizeof(info->shadow),
886 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
9f27ee59
JF
887 if (!copy)
888 return -ENOMEM;
889 memcpy(copy, info->shadow, sizeof(info->shadow));
890
891 /* Stage 2: Set up free list. */
892 memset(&info->shadow, 0, sizeof(info->shadow));
893 for (i = 0; i < BLK_RING_SIZE; i++)
894 info->shadow[i].req.id = i+1;
895 info->shadow_free = info->ring.req_prod_pvt;
896 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
897
898 /* Stage 3: Find pending requests and requeue them. */
899 for (i = 0; i < BLK_RING_SIZE; i++) {
900 /* Not in use? */
a945b980 901 if (!copy[i].request)
9f27ee59
JF
902 continue;
903
904 /* Grab a request slot and copy shadow state into it. */
905 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
906 *req = copy[i].req;
907
908 /* We get a new request id, and must reset the shadow state. */
909 req->id = get_id_from_freelist(info);
910 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
911
912 /* Rewrite any grant references invalidated by susp/resume. */
913 for (j = 0; j < req->nr_segments; j++)
914 gnttab_grant_foreign_access_ref(
915 req->seg[j].gref,
916 info->xbdev->otherend_id,
917 pfn_to_mfn(info->shadow[req->id].frame[j]),
a945b980 918 rq_data_dir(info->shadow[req->id].request));
9f27ee59
JF
919 info->shadow[req->id].req = *req;
920
921 info->ring.req_prod_pvt++;
922 }
923
924 kfree(copy);
925
926 xenbus_switch_state(info->xbdev, XenbusStateConnected);
927
928 spin_lock_irq(&blkif_io_lock);
929
930 /* Now safe for us to use the shared ring */
931 info->connected = BLKIF_STATE_CONNECTED;
932
933 /* Send off requeued requests */
934 flush_requests(info);
935
936 /* Kick any other new requests queued since we resumed */
937 kick_pending_request_queues(info);
938
939 spin_unlock_irq(&blkif_io_lock);
940
941 return 0;
942}
943
944/**
945 * We are reconnecting to the backend, due to a suspend/resume, or a backend
946 * driver restart. We tear down our blkif structure and recreate it, but
947 * leave the device-layer structures intact so that this is transparent to the
948 * rest of the kernel.
949 */
950static int blkfront_resume(struct xenbus_device *dev)
951{
a1b4b12b 952 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59
JF
953 int err;
954
955 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
956
957 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
958
203fd61f 959 err = talk_to_blkback(dev, info);
9f27ee59
JF
960 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
961 err = blkif_recover(info);
962
963 return err;
964}
965
b70f5fa0
DS
966static void
967blkfront_closing(struct blkfront_info *info)
968{
969 struct xenbus_device *xbdev = info->xbdev;
970 struct block_device *bdev = NULL;
971
972 mutex_lock(&info->mutex);
973
974 if (xbdev->state == XenbusStateClosing) {
975 mutex_unlock(&info->mutex);
976 return;
977 }
978
979 if (info->gd)
980 bdev = bdget_disk(info->gd, 0);
981
982 mutex_unlock(&info->mutex);
983
984 if (!bdev) {
985 xenbus_frontend_closed(xbdev);
986 return;
987 }
988
989 mutex_lock(&bdev->bd_mutex);
990
7b32d104 991 if (bdev->bd_openers) {
b70f5fa0
DS
992 xenbus_dev_error(xbdev, -EBUSY,
993 "Device in use; refusing to close");
994 xenbus_switch_state(xbdev, XenbusStateClosing);
995 } else {
996 xlvbd_release_gendisk(info);
997 xenbus_frontend_closed(xbdev);
998 }
999
1000 mutex_unlock(&bdev->bd_mutex);
1001 bdput(bdev);
1002}
9f27ee59
JF
1003
1004/*
1005 * Invoked when the backend is finally 'ready' (and has told produced
1006 * the details about the physical device - #sectors, size, etc).
1007 */
1008static void blkfront_connect(struct blkfront_info *info)
1009{
1010 unsigned long long sectors;
1011 unsigned long sector_size;
1012 unsigned int binfo;
1013 int err;
7901d141 1014 int barrier;
9f27ee59 1015
1fa73be6
S
1016 switch (info->connected) {
1017 case BLKIF_STATE_CONNECTED:
1018 /*
1019 * Potentially, the back-end may be signalling
1020 * a capacity change; update the capacity.
1021 */
1022 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1023 "sectors", "%Lu", &sectors);
1024 if (XENBUS_EXIST_ERR(err))
1025 return;
1026 printk(KERN_INFO "Setting capacity to %Lu\n",
1027 sectors);
1028 set_capacity(info->gd, sectors);
2def141e 1029 revalidate_disk(info->gd);
1fa73be6
S
1030
1031 /* fall through */
1032 case BLKIF_STATE_SUSPENDED:
9f27ee59
JF
1033 return;
1034
b4dddb49
JF
1035 default:
1036 break;
1fa73be6 1037 }
9f27ee59
JF
1038
1039 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1040 __func__, info->xbdev->otherend);
1041
1042 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1043 "sectors", "%llu", &sectors,
1044 "info", "%u", &binfo,
1045 "sector-size", "%lu", &sector_size,
1046 NULL);
1047 if (err) {
1048 xenbus_dev_fatal(info->xbdev, err,
1049 "reading backend fields at %s",
1050 info->xbdev->otherend);
1051 return;
1052 }
1053
1054 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
7901d141 1055 "feature-barrier", "%lu", &barrier,
9f27ee59 1056 NULL);
7901d141
JF
1057
1058 /*
1059 * If there's no "feature-barrier" defined, then it means
1060 * we're dealing with a very old backend which writes
4913efe4 1061 * synchronously; nothing to do.
7901d141 1062 *
6958f145 1063 * If there are barriers, then we use flush.
7901d141 1064 */
4913efe4 1065 info->feature_flush = 0;
005a1d15 1066
4913efe4
TH
1067 if (!err && barrier)
1068 info->feature_flush = REQ_FLUSH;
9f27ee59 1069
9246b5f0 1070 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
9f27ee59
JF
1071 if (err) {
1072 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1073 info->xbdev->otherend);
1074 return;
1075 }
1076
1077 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1078
1079 /* Kick pending requests. */
1080 spin_lock_irq(&blkif_io_lock);
1081 info->connected = BLKIF_STATE_CONNECTED;
1082 kick_pending_request_queues(info);
1083 spin_unlock_irq(&blkif_io_lock);
1084
1085 add_disk(info->gd);
1d78d705
CL
1086
1087 info->is_ready = 1;
9f27ee59
JF
1088}
1089
9f27ee59
JF
1090/**
1091 * Callback received when the backend's state changes.
1092 */
203fd61f 1093static void blkback_changed(struct xenbus_device *dev,
9f27ee59
JF
1094 enum xenbus_state backend_state)
1095{
a1b4b12b 1096 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59 1097
203fd61f 1098 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
9f27ee59
JF
1099
1100 switch (backend_state) {
1101 case XenbusStateInitialising:
1102 case XenbusStateInitWait:
1103 case XenbusStateInitialised:
b78c9512
NI
1104 case XenbusStateReconfiguring:
1105 case XenbusStateReconfigured:
9f27ee59
JF
1106 case XenbusStateUnknown:
1107 case XenbusStateClosed:
1108 break;
1109
1110 case XenbusStateConnected:
1111 blkfront_connect(info);
1112 break;
1113
1114 case XenbusStateClosing:
b70f5fa0 1115 blkfront_closing(info);
9f27ee59
JF
1116 break;
1117 }
1118}
1119
fa1bd359 1120static int blkfront_remove(struct xenbus_device *xbdev)
9f27ee59 1121{
fa1bd359
DS
1122 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1123 struct block_device *bdev = NULL;
1124 struct gendisk *disk;
9f27ee59 1125
fa1bd359 1126 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
9f27ee59
JF
1127
1128 blkif_free(info, 0);
1129
fa1bd359
DS
1130 mutex_lock(&info->mutex);
1131
1132 disk = info->gd;
1133 if (disk)
1134 bdev = bdget_disk(disk, 0);
1135
1136 info->xbdev = NULL;
1137 mutex_unlock(&info->mutex);
1138
1139 if (!bdev) {
1140 kfree(info);
1141 return 0;
1142 }
1143
1144 /*
1145 * The xbdev was removed before we reached the Closed
1146 * state. See if it's safe to remove the disk. If the bdev
1147 * isn't closed yet, we let release take care of it.
1148 */
1149
1150 mutex_lock(&bdev->bd_mutex);
1151 info = disk->private_data;
1152
d54142c7
DS
1153 dev_warn(disk_to_dev(disk),
1154 "%s was hot-unplugged, %d stale handles\n",
1155 xbdev->nodename, bdev->bd_openers);
1156
7b32d104 1157 if (info && !bdev->bd_openers) {
fa1bd359
DS
1158 xlvbd_release_gendisk(info);
1159 disk->private_data = NULL;
0e345826 1160 kfree(info);
fa1bd359
DS
1161 }
1162
1163 mutex_unlock(&bdev->bd_mutex);
1164 bdput(bdev);
9f27ee59
JF
1165
1166 return 0;
1167}
1168
1d78d705
CL
1169static int blkfront_is_ready(struct xenbus_device *dev)
1170{
a1b4b12b 1171 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1d78d705 1172
5d7ed20e 1173 return info->is_ready && info->xbdev;
1d78d705
CL
1174}
1175
a63c848b 1176static int blkif_open(struct block_device *bdev, fmode_t mode)
9f27ee59 1177{
13961743
DS
1178 struct gendisk *disk = bdev->bd_disk;
1179 struct blkfront_info *info;
1180 int err = 0;
6e9624b8 1181
2a48fc0a 1182 mutex_lock(&blkfront_mutex);
6e9624b8 1183
13961743
DS
1184 info = disk->private_data;
1185 if (!info) {
1186 /* xbdev gone */
1187 err = -ERESTARTSYS;
1188 goto out;
1189 }
1190
1191 mutex_lock(&info->mutex);
1192
1193 if (!info->gd)
1194 /* xbdev is closed */
1195 err = -ERESTARTSYS;
1196
1197 mutex_unlock(&info->mutex);
1198
13961743 1199out:
2a48fc0a 1200 mutex_unlock(&blkfront_mutex);
13961743 1201 return err;
9f27ee59
JF
1202}
1203
a63c848b 1204static int blkif_release(struct gendisk *disk, fmode_t mode)
9f27ee59 1205{
a63c848b 1206 struct blkfront_info *info = disk->private_data;
7fd152f4
DS
1207 struct block_device *bdev;
1208 struct xenbus_device *xbdev;
1209
2a48fc0a 1210 mutex_lock(&blkfront_mutex);
7fd152f4
DS
1211
1212 bdev = bdget_disk(disk, 0);
1213 bdput(bdev);
1214
acfca3c6
DS
1215 if (bdev->bd_openers)
1216 goto out;
1217
7fd152f4
DS
1218 /*
1219 * Check if we have been instructed to close. We will have
1220 * deferred this request, because the bdev was still open.
1221 */
1222
1223 mutex_lock(&info->mutex);
1224 xbdev = info->xbdev;
1225
1226 if (xbdev && xbdev->state == XenbusStateClosing) {
1227 /* pending switch to state closed */
d54142c7 1228 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
1229 xlvbd_release_gendisk(info);
1230 xenbus_frontend_closed(info->xbdev);
1231 }
1232
1233 mutex_unlock(&info->mutex);
1234
1235 if (!xbdev) {
1236 /* sudden device removal */
d54142c7 1237 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
1238 xlvbd_release_gendisk(info);
1239 disk->private_data = NULL;
1240 kfree(info);
9f27ee59 1241 }
7fd152f4 1242
a4cc14ec 1243out:
2a48fc0a 1244 mutex_unlock(&blkfront_mutex);
9f27ee59
JF
1245 return 0;
1246}
1247
83d5cde4 1248static const struct block_device_operations xlvbd_block_fops =
9f27ee59
JF
1249{
1250 .owner = THIS_MODULE,
a63c848b
AV
1251 .open = blkif_open,
1252 .release = blkif_release,
597592d9 1253 .getgeo = blkif_getgeo,
8a6cfeb6 1254 .ioctl = blkif_ioctl,
9f27ee59
JF
1255};
1256
1257
ec9c42ec 1258static const struct xenbus_device_id blkfront_ids[] = {
9f27ee59
JF
1259 { "vbd" },
1260 { "" }
1261};
1262
1263static struct xenbus_driver blkfront = {
1264 .name = "vbd",
1265 .owner = THIS_MODULE,
1266 .ids = blkfront_ids,
1267 .probe = blkfront_probe,
1268 .remove = blkfront_remove,
1269 .resume = blkfront_resume,
203fd61f 1270 .otherend_changed = blkback_changed,
1d78d705 1271 .is_ready = blkfront_is_ready,
9f27ee59
JF
1272};
1273
1274static int __init xlblk_init(void)
1275{
6e833587 1276 if (!xen_domain())
9f27ee59
JF
1277 return -ENODEV;
1278
1279 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1280 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1281 XENVBD_MAJOR, DEV_NAME);
1282 return -ENODEV;
1283 }
1284
1285 return xenbus_register_frontend(&blkfront);
1286}
1287module_init(xlblk_init);
1288
1289
5a60d0cd 1290static void __exit xlblk_exit(void)
9f27ee59
JF
1291{
1292 return xenbus_unregister_driver(&blkfront);
1293}
1294module_exit(xlblk_exit);
1295
1296MODULE_DESCRIPTION("Xen virtual block device frontend");
1297MODULE_LICENSE("GPL");
1298MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
d2f0c52b 1299MODULE_ALIAS("xen:vbd");
4f93f09b 1300MODULE_ALIAS("xenblk");