xen-blkfront: handle bvecs with partial data
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
34ae2e47 46#include <linux/bitmap.h>
0a8704a5 47#include <linux/llist.h>
9f27ee59 48
1ccbf534 49#include <xen/xen.h>
9f27ee59
JF
50#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
c1c5413a 54#include <xen/platform_pci.h>
9f27ee59
JF
55
56#include <xen/interface/grant_table.h>
57#include <xen/interface/io/blkif.h>
3e334239 58#include <xen/interface/io/protocols.h>
9f27ee59
JF
59
60#include <asm/xen/hypervisor.h>
61
62enum blkif_state {
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
66};
67
0a8704a5
RPM
68struct grant {
69 grant_ref_t gref;
70 unsigned long pfn;
71 struct llist_node node;
72};
73
9f27ee59
JF
74struct blk_shadow {
75 struct blkif_request req;
a945b980 76 struct request *request;
9f27ee59 77 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
0a8704a5 78 struct grant *grants_used[BLKIF_MAX_SEGMENTS_PER_REQUEST];
9f27ee59
JF
79};
80
2a48fc0a 81static DEFINE_MUTEX(blkfront_mutex);
83d5cde4 82static const struct block_device_operations xlvbd_block_fops;
9f27ee59 83
667c78af 84#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
9f27ee59
JF
85
86/*
87 * We have one of these per vbd, whether ide, scsi or 'other'. They
88 * hang in private_data off the gendisk structure. We may end up
89 * putting all kinds of interesting stuff here :-)
90 */
91struct blkfront_info
92{
3467811e 93 spinlock_t io_lock;
b70f5fa0 94 struct mutex mutex;
9f27ee59 95 struct xenbus_device *xbdev;
9f27ee59
JF
96 struct gendisk *gd;
97 int vdevice;
98 blkif_vdev_t handle;
99 enum blkif_state connected;
100 int ring_ref;
101 struct blkif_front_ring ring;
9e973e64 102 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
9f27ee59
JF
103 unsigned int evtchn, irq;
104 struct request_queue *rq;
105 struct work_struct work;
106 struct gnttab_free_callback callback;
107 struct blk_shadow shadow[BLK_RING_SIZE];
0a8704a5
RPM
108 struct llist_head persistent_gnts;
109 unsigned int persistent_gnts_c;
9f27ee59 110 unsigned long shadow_free;
4913efe4 111 unsigned int feature_flush;
edf6ef59 112 unsigned int flush_op;
5ea42986
KRW
113 unsigned int feature_discard:1;
114 unsigned int feature_secdiscard:1;
ed30bf31
LD
115 unsigned int discard_granularity;
116 unsigned int discard_alignment;
0a8704a5 117 unsigned int feature_persistent:1;
1d78d705 118 int is_ready;
9f27ee59
JF
119};
120
0e345826
JB
121static unsigned int nr_minors;
122static unsigned long *minors;
123static DEFINE_SPINLOCK(minor_lock);
124
9f27ee59
JF
125#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
126 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
127#define GRANT_INVALID_REF 0
128
129#define PARTS_PER_DISK 16
9246b5f0 130#define PARTS_PER_EXT_DISK 256
9f27ee59
JF
131
132#define BLKIF_MAJOR(dev) ((dev)>>8)
133#define BLKIF_MINOR(dev) ((dev) & 0xff)
134
9246b5f0
CL
135#define EXT_SHIFT 28
136#define EXTENDED (1<<EXT_SHIFT)
137#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
138#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
c80a4209
SS
139#define EMULATED_HD_DISK_MINOR_OFFSET (0)
140#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
196cfe2a
SB
141#define EMULATED_SD_DISK_MINOR_OFFSET (0)
142#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
9f27ee59 143
9246b5f0 144#define DEV_NAME "xvd" /* name in /dev */
9f27ee59
JF
145
146static int get_id_from_freelist(struct blkfront_info *info)
147{
148 unsigned long free = info->shadow_free;
b9ed7252 149 BUG_ON(free >= BLK_RING_SIZE);
97e36834
KRW
150 info->shadow_free = info->shadow[free].req.u.rw.id;
151 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
9f27ee59
JF
152 return free;
153}
154
6878c32e 155static int add_id_to_freelist(struct blkfront_info *info,
9f27ee59
JF
156 unsigned long id)
157{
6878c32e
KRW
158 if (info->shadow[id].req.u.rw.id != id)
159 return -EINVAL;
160 if (info->shadow[id].request == NULL)
161 return -EINVAL;
97e36834 162 info->shadow[id].req.u.rw.id = info->shadow_free;
a945b980 163 info->shadow[id].request = NULL;
9f27ee59 164 info->shadow_free = id;
6878c32e 165 return 0;
9f27ee59
JF
166}
167
6878c32e
KRW
168static const char *op_name(int op)
169{
170 static const char *const names[] = {
171 [BLKIF_OP_READ] = "read",
172 [BLKIF_OP_WRITE] = "write",
173 [BLKIF_OP_WRITE_BARRIER] = "barrier",
174 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
175 [BLKIF_OP_DISCARD] = "discard" };
176
177 if (op < 0 || op >= ARRAY_SIZE(names))
178 return "unknown";
179
180 if (!names[op])
181 return "reserved";
182
183 return names[op];
184}
0e345826
JB
185static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
186{
187 unsigned int end = minor + nr;
188 int rc;
189
190 if (end > nr_minors) {
191 unsigned long *bitmap, *old;
192
f094148a 193 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
0e345826
JB
194 GFP_KERNEL);
195 if (bitmap == NULL)
196 return -ENOMEM;
197
198 spin_lock(&minor_lock);
199 if (end > nr_minors) {
200 old = minors;
201 memcpy(bitmap, minors,
202 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
203 minors = bitmap;
204 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
205 } else
206 old = bitmap;
207 spin_unlock(&minor_lock);
208 kfree(old);
209 }
210
211 spin_lock(&minor_lock);
212 if (find_next_bit(minors, end, minor) >= end) {
34ae2e47 213 bitmap_set(minors, minor, nr);
0e345826
JB
214 rc = 0;
215 } else
216 rc = -EBUSY;
217 spin_unlock(&minor_lock);
218
219 return rc;
220}
221
222static void xlbd_release_minors(unsigned int minor, unsigned int nr)
223{
224 unsigned int end = minor + nr;
225
226 BUG_ON(end > nr_minors);
227 spin_lock(&minor_lock);
34ae2e47 228 bitmap_clear(minors, minor, nr);
0e345826
JB
229 spin_unlock(&minor_lock);
230}
231
9f27ee59
JF
232static void blkif_restart_queue_callback(void *arg)
233{
234 struct blkfront_info *info = (struct blkfront_info *)arg;
235 schedule_work(&info->work);
236}
237
afe42d7d 238static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
597592d9
IC
239{
240 /* We don't have real geometry info, but let's at least return
241 values consistent with the size of the device */
242 sector_t nsect = get_capacity(bd->bd_disk);
243 sector_t cylinders = nsect;
244
245 hg->heads = 0xff;
246 hg->sectors = 0x3f;
247 sector_div(cylinders, hg->heads * hg->sectors);
248 hg->cylinders = cylinders;
249 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
250 hg->cylinders = 0xffff;
251 return 0;
252}
253
a63c848b 254static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
62aa0054 255 unsigned command, unsigned long argument)
440a01a7 256{
a63c848b 257 struct blkfront_info *info = bdev->bd_disk->private_data;
440a01a7
CL
258 int i;
259
260 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
261 command, (long)argument);
262
263 switch (command) {
264 case CDROMMULTISESSION:
265 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
266 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
267 if (put_user(0, (char __user *)(argument + i)))
268 return -EFAULT;
269 return 0;
270
271 case CDROM_GET_CAPABILITY: {
272 struct gendisk *gd = info->gd;
273 if (gd->flags & GENHD_FL_CD)
274 return 0;
275 return -EINVAL;
276 }
277
278 default:
279 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
280 command);*/
281 return -EINVAL; /* same return as native Linux */
282 }
283
284 return 0;
285}
286
9f27ee59 287/*
c64e38ea 288 * Generate a Xen blkfront IO request from a blk layer request. Reads
edf6ef59 289 * and writes are handled as expected.
9f27ee59 290 *
c64e38ea 291 * @req: a request struct
9f27ee59
JF
292 */
293static int blkif_queue_request(struct request *req)
294{
295 struct blkfront_info *info = req->rq_disk->private_data;
296 unsigned long buffer_mfn;
297 struct blkif_request *ring_req;
9f27ee59
JF
298 unsigned long id;
299 unsigned int fsect, lsect;
9e973e64 300 int i, ref;
0a8704a5
RPM
301
302 /*
303 * Used to store if we are able to queue the request by just using
304 * existing persistent grants, or if we have to get new grants,
305 * as there are not sufficiently many free.
306 */
307 bool new_persistent_gnts;
9f27ee59 308 grant_ref_t gref_head;
0a8704a5
RPM
309 struct page *granted_page;
310 struct grant *gnt_list_entry = NULL;
9e973e64 311 struct scatterlist *sg;
9f27ee59
JF
312
313 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
314 return 1;
315
0a8704a5
RPM
316 /* Check if we have enought grants to allocate a requests */
317 if (info->persistent_gnts_c < BLKIF_MAX_SEGMENTS_PER_REQUEST) {
318 new_persistent_gnts = 1;
319 if (gnttab_alloc_grant_references(
320 BLKIF_MAX_SEGMENTS_PER_REQUEST - info->persistent_gnts_c,
321 &gref_head) < 0) {
322 gnttab_request_free_callback(
323 &info->callback,
324 blkif_restart_queue_callback,
325 info,
326 BLKIF_MAX_SEGMENTS_PER_REQUEST);
327 return 1;
328 }
329 } else
330 new_persistent_gnts = 0;
9f27ee59
JF
331
332 /* Fill out a communications ring structure. */
333 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
334 id = get_id_from_freelist(info);
a945b980 335 info->shadow[id].request = req;
9f27ee59 336
97e36834 337 ring_req->u.rw.id = id;
51de6952 338 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
97e36834 339 ring_req->u.rw.handle = info->handle;
9f27ee59
JF
340
341 ring_req->operation = rq_data_dir(req) ?
342 BLKIF_OP_WRITE : BLKIF_OP_READ;
be2f8373
JF
343
344 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
345 /*
edf6ef59
KRW
346 * Ideally we can do an unordered flush-to-disk. In case the
347 * backend onlysupports barriers, use that. A barrier request
be2f8373
JF
348 * a superset of FUA, so we can implement it the same
349 * way. (It's also a FLUSH+FUA, since it is
350 * guaranteed ordered WRT previous writes.)
351 */
edf6ef59 352 ring_req->operation = info->flush_op;
be2f8373 353 }
9f27ee59 354
5ea42986 355 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
ed30bf31
LD
356 /* id, sector_number and handle are set above. */
357 ring_req->operation = BLKIF_OP_DISCARD;
ed30bf31 358 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
5ea42986
KRW
359 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
360 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
361 else
362 ring_req->u.discard.flag = 0;
ed30bf31 363 } else {
97e36834
KRW
364 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
365 info->sg);
366 BUG_ON(ring_req->u.rw.nr_segments >
367 BLKIF_MAX_SEGMENTS_PER_REQUEST);
9e973e64 368
97e36834 369 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
ed30bf31
LD
370 fsect = sg->offset >> 9;
371 lsect = fsect + (sg->length >> 9) - 1;
6c92e699 372
0a8704a5
RPM
373 if (info->persistent_gnts_c) {
374 BUG_ON(llist_empty(&info->persistent_gnts));
375 gnt_list_entry = llist_entry(
376 llist_del_first(&info->persistent_gnts),
377 struct grant, node);
378
379 ref = gnt_list_entry->gref;
380 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
381 info->persistent_gnts_c--;
382 } else {
383 ref = gnttab_claim_grant_reference(&gref_head);
384 BUG_ON(ref == -ENOSPC);
385
386 gnt_list_entry =
387 kmalloc(sizeof(struct grant),
388 GFP_ATOMIC);
389 if (!gnt_list_entry)
390 return -ENOMEM;
391
392 granted_page = alloc_page(GFP_ATOMIC);
393 if (!granted_page) {
394 kfree(gnt_list_entry);
395 return -ENOMEM;
396 }
397
398 gnt_list_entry->pfn =
399 page_to_pfn(granted_page);
400 gnt_list_entry->gref = ref;
401
402 buffer_mfn = pfn_to_mfn(page_to_pfn(
403 granted_page));
404 gnttab_grant_foreign_access_ref(ref,
ed30bf31 405 info->xbdev->otherend_id,
0a8704a5
RPM
406 buffer_mfn, 0);
407 }
408
409 info->shadow[id].grants_used[i] = gnt_list_entry;
410
411 if (rq_data_dir(req)) {
412 char *bvec_data;
413 void *shared_data;
414
415 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
416
417 shared_data = kmap_atomic(
418 pfn_to_page(gnt_list_entry->pfn));
419 bvec_data = kmap_atomic(sg_page(sg));
420
421 /*
422 * this does not wipe data stored outside the
423 * range sg->offset..sg->offset+sg->length.
424 * Therefore, blkback *could* see data from
425 * previous requests. This is OK as long as
426 * persistent grants are shared with just one
427 * domain. It may need refactoring if this
428 * changes
429 */
430 memcpy(shared_data + sg->offset,
431 bvec_data + sg->offset,
432 sg->length);
433
434 kunmap_atomic(bvec_data);
435 kunmap_atomic(shared_data);
436 }
ed30bf31
LD
437
438 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
439 ring_req->u.rw.seg[i] =
440 (struct blkif_request_segment) {
441 .gref = ref,
442 .first_sect = fsect,
443 .last_sect = lsect };
444 }
9f27ee59
JF
445 }
446
447 info->ring.req_prod_pvt++;
448
449 /* Keep a private copy so we can reissue requests when recovering. */
450 info->shadow[id].req = *ring_req;
451
0a8704a5
RPM
452 if (new_persistent_gnts)
453 gnttab_free_grant_references(gref_head);
9f27ee59
JF
454
455 return 0;
456}
457
458
459static inline void flush_requests(struct blkfront_info *info)
460{
461 int notify;
462
463 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
464
465 if (notify)
466 notify_remote_via_irq(info->irq);
467}
468
469/*
470 * do_blkif_request
471 * read a block; request is in a request queue
472 */
165125e1 473static void do_blkif_request(struct request_queue *rq)
9f27ee59
JF
474{
475 struct blkfront_info *info = NULL;
476 struct request *req;
477 int queued;
478
479 pr_debug("Entered do_blkif_request\n");
480
481 queued = 0;
482
9934c8c0 483 while ((req = blk_peek_request(rq)) != NULL) {
9f27ee59 484 info = req->rq_disk->private_data;
9f27ee59
JF
485
486 if (RING_FULL(&info->ring))
487 goto wait;
488
9934c8c0 489 blk_start_request(req);
296b2f6a 490
d11e6158
KRW
491 if ((req->cmd_type != REQ_TYPE_FS) ||
492 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
493 !info->flush_op)) {
296b2f6a
TH
494 __blk_end_request_all(req, -EIO);
495 continue;
496 }
497
9f27ee59 498 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
83096ebf
TH
499 "(%u/%u) buffer:%p [%s]\n",
500 req, req->cmd, (unsigned long)blk_rq_pos(req),
501 blk_rq_cur_sectors(req), blk_rq_sectors(req),
502 req->buffer, rq_data_dir(req) ? "write" : "read");
9f27ee59 503
9f27ee59
JF
504 if (blkif_queue_request(req)) {
505 blk_requeue_request(rq, req);
506wait:
507 /* Avoid pointless unplugs. */
508 blk_stop_queue(rq);
509 break;
510 }
511
512 queued++;
513 }
514
515 if (queued != 0)
516 flush_requests(info);
517}
518
519static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
520{
165125e1 521 struct request_queue *rq;
ed30bf31 522 struct blkfront_info *info = gd->private_data;
9f27ee59 523
3467811e 524 rq = blk_init_queue(do_blkif_request, &info->io_lock);
9f27ee59
JF
525 if (rq == NULL)
526 return -1;
527
66d352e1 528 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
9f27ee59 529
ed30bf31
LD
530 if (info->feature_discard) {
531 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
532 blk_queue_max_discard_sectors(rq, get_capacity(gd));
533 rq->limits.discard_granularity = info->discard_granularity;
534 rq->limits.discard_alignment = info->discard_alignment;
5ea42986
KRW
535 if (info->feature_secdiscard)
536 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31
LD
537 }
538
9f27ee59 539 /* Hard sector size and max sectors impersonate the equiv. hardware. */
e1defc4f 540 blk_queue_logical_block_size(rq, sector_size);
086fa5ff 541 blk_queue_max_hw_sectors(rq, 512);
9f27ee59
JF
542
543 /* Each segment in a request is up to an aligned page in size. */
544 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
545 blk_queue_max_segment_size(rq, PAGE_SIZE);
546
547 /* Ensure a merged request will fit in a single I/O ring slot. */
8a78362c 548 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
9f27ee59
JF
549
550 /* Make sure buffer addresses are sector-aligned. */
551 blk_queue_dma_alignment(rq, 511);
552
1c91fe1a
IC
553 /* Make sure we don't use bounce buffers. */
554 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
555
9f27ee59
JF
556 gd->queue = rq;
557
558 return 0;
559}
560
561
4913efe4 562static void xlvbd_flush(struct blkfront_info *info)
9f27ee59 563{
4913efe4 564 blk_queue_flush(info->rq, info->feature_flush);
0a8704a5 565 printk(KERN_INFO "blkfront: %s: %s: %s %s\n",
4913efe4 566 info->gd->disk_name,
edf6ef59
KRW
567 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
568 "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
569 "flush diskcache" : "barrier or flush"),
0a8704a5
RPM
570 info->feature_flush ? "enabled" : "disabled",
571 info->feature_persistent ? "using persistent grants" : "");
9f27ee59
JF
572}
573
c80a4209
SS
574static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
575{
576 int major;
577 major = BLKIF_MAJOR(vdevice);
578 *minor = BLKIF_MINOR(vdevice);
579 switch (major) {
580 case XEN_IDE0_MAJOR:
581 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
582 *minor = ((*minor / 64) * PARTS_PER_DISK) +
583 EMULATED_HD_DISK_MINOR_OFFSET;
584 break;
585 case XEN_IDE1_MAJOR:
586 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
587 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
588 EMULATED_HD_DISK_MINOR_OFFSET;
589 break;
590 case XEN_SCSI_DISK0_MAJOR:
591 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
592 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
593 break;
594 case XEN_SCSI_DISK1_MAJOR:
595 case XEN_SCSI_DISK2_MAJOR:
596 case XEN_SCSI_DISK3_MAJOR:
597 case XEN_SCSI_DISK4_MAJOR:
598 case XEN_SCSI_DISK5_MAJOR:
599 case XEN_SCSI_DISK6_MAJOR:
600 case XEN_SCSI_DISK7_MAJOR:
601 *offset = (*minor / PARTS_PER_DISK) +
602 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
603 EMULATED_SD_DISK_NAME_OFFSET;
604 *minor = *minor +
605 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
606 EMULATED_SD_DISK_MINOR_OFFSET;
607 break;
608 case XEN_SCSI_DISK8_MAJOR:
609 case XEN_SCSI_DISK9_MAJOR:
610 case XEN_SCSI_DISK10_MAJOR:
611 case XEN_SCSI_DISK11_MAJOR:
612 case XEN_SCSI_DISK12_MAJOR:
613 case XEN_SCSI_DISK13_MAJOR:
614 case XEN_SCSI_DISK14_MAJOR:
615 case XEN_SCSI_DISK15_MAJOR:
616 *offset = (*minor / PARTS_PER_DISK) +
617 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
618 EMULATED_SD_DISK_NAME_OFFSET;
619 *minor = *minor +
620 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
621 EMULATED_SD_DISK_MINOR_OFFSET;
622 break;
623 case XENVBD_MAJOR:
624 *offset = *minor / PARTS_PER_DISK;
625 break;
626 default:
627 printk(KERN_WARNING "blkfront: your disk configuration is "
628 "incorrect, please use an xvd device instead\n");
629 return -ENODEV;
630 }
631 return 0;
632}
9f27ee59 633
e77c78c0
JB
634static char *encode_disk_name(char *ptr, unsigned int n)
635{
636 if (n >= 26)
637 ptr = encode_disk_name(ptr, n / 26 - 1);
638 *ptr = 'a' + n % 26;
639 return ptr + 1;
640}
641
9246b5f0
CL
642static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
643 struct blkfront_info *info,
644 u16 vdisk_info, u16 sector_size)
9f27ee59
JF
645{
646 struct gendisk *gd;
647 int nr_minors = 1;
c80a4209 648 int err;
9246b5f0
CL
649 unsigned int offset;
650 int minor;
651 int nr_parts;
e77c78c0 652 char *ptr;
9f27ee59
JF
653
654 BUG_ON(info->gd != NULL);
655 BUG_ON(info->rq != NULL);
656
9246b5f0
CL
657 if ((info->vdevice>>EXT_SHIFT) > 1) {
658 /* this is above the extended range; something is wrong */
659 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
660 return -ENODEV;
661 }
662
663 if (!VDEV_IS_EXTENDED(info->vdevice)) {
c80a4209
SS
664 err = xen_translate_vdev(info->vdevice, &minor, &offset);
665 if (err)
666 return err;
667 nr_parts = PARTS_PER_DISK;
9246b5f0
CL
668 } else {
669 minor = BLKIF_MINOR_EXT(info->vdevice);
670 nr_parts = PARTS_PER_EXT_DISK;
c80a4209 671 offset = minor / nr_parts;
89153b5c 672 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
c80a4209
SS
673 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
674 "emulated IDE disks,\n\t choose an xvd device name"
675 "from xvde on\n", info->vdevice);
9246b5f0 676 }
e77c78c0
JB
677 if (minor >> MINORBITS) {
678 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
679 info->vdevice, minor);
680 return -ENODEV;
681 }
9246b5f0
CL
682
683 if ((minor % nr_parts) == 0)
684 nr_minors = nr_parts;
9f27ee59 685
0e345826
JB
686 err = xlbd_reserve_minors(minor, nr_minors);
687 if (err)
688 goto out;
689 err = -ENODEV;
690
9f27ee59
JF
691 gd = alloc_disk(nr_minors);
692 if (gd == NULL)
0e345826 693 goto release;
9f27ee59 694
e77c78c0
JB
695 strcpy(gd->disk_name, DEV_NAME);
696 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
697 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
698 if (nr_minors > 1)
699 *ptr = 0;
700 else
701 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
702 "%d", minor & (nr_parts - 1));
9f27ee59
JF
703
704 gd->major = XENVBD_MAJOR;
705 gd->first_minor = minor;
706 gd->fops = &xlvbd_block_fops;
707 gd->private_data = info;
708 gd->driverfs_dev = &(info->xbdev->dev);
709 set_capacity(gd, capacity);
710
711 if (xlvbd_init_blk_queue(gd, sector_size)) {
712 del_gendisk(gd);
0e345826 713 goto release;
9f27ee59
JF
714 }
715
716 info->rq = gd->queue;
717 info->gd = gd;
718
4913efe4 719 xlvbd_flush(info);
9f27ee59
JF
720
721 if (vdisk_info & VDISK_READONLY)
722 set_disk_ro(gd, 1);
723
724 if (vdisk_info & VDISK_REMOVABLE)
725 gd->flags |= GENHD_FL_REMOVABLE;
726
727 if (vdisk_info & VDISK_CDROM)
728 gd->flags |= GENHD_FL_CD;
729
730 return 0;
731
0e345826
JB
732 release:
733 xlbd_release_minors(minor, nr_minors);
9f27ee59
JF
734 out:
735 return err;
736}
737
a66b5aeb
DS
738static void xlvbd_release_gendisk(struct blkfront_info *info)
739{
740 unsigned int minor, nr_minors;
741 unsigned long flags;
742
743 if (info->rq == NULL)
744 return;
745
3467811e 746 spin_lock_irqsave(&info->io_lock, flags);
a66b5aeb
DS
747
748 /* No more blkif_request(). */
749 blk_stop_queue(info->rq);
750
751 /* No more gnttab callback work. */
752 gnttab_cancel_free_callback(&info->callback);
3467811e 753 spin_unlock_irqrestore(&info->io_lock, flags);
a66b5aeb
DS
754
755 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 756 flush_work(&info->work);
a66b5aeb
DS
757
758 del_gendisk(info->gd);
759
760 minor = info->gd->first_minor;
761 nr_minors = info->gd->minors;
762 xlbd_release_minors(minor, nr_minors);
763
764 blk_cleanup_queue(info->rq);
765 info->rq = NULL;
766
767 put_disk(info->gd);
768 info->gd = NULL;
769}
770
9f27ee59
JF
771static void kick_pending_request_queues(struct blkfront_info *info)
772{
773 if (!RING_FULL(&info->ring)) {
774 /* Re-enable calldowns. */
775 blk_start_queue(info->rq);
776 /* Kick things off immediately. */
777 do_blkif_request(info->rq);
778 }
779}
780
781static void blkif_restart_queue(struct work_struct *work)
782{
783 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
784
3467811e 785 spin_lock_irq(&info->io_lock);
9f27ee59
JF
786 if (info->connected == BLKIF_STATE_CONNECTED)
787 kick_pending_request_queues(info);
3467811e 788 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
789}
790
791static void blkif_free(struct blkfront_info *info, int suspend)
792{
0a8704a5
RPM
793 struct llist_node *all_gnts;
794 struct grant *persistent_gnt;
ebb351cf 795 struct llist_node *n;
0a8704a5 796
9f27ee59 797 /* Prevent new requests being issued until we fix things up. */
3467811e 798 spin_lock_irq(&info->io_lock);
9f27ee59
JF
799 info->connected = suspend ?
800 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
801 /* No more blkif_request(). */
802 if (info->rq)
803 blk_stop_queue(info->rq);
0a8704a5
RPM
804
805 /* Remove all persistent grants */
806 if (info->persistent_gnts_c) {
807 all_gnts = llist_del_all(&info->persistent_gnts);
ebb351cf 808 llist_for_each_entry_safe(persistent_gnt, n, all_gnts, node) {
0a8704a5 809 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
07c540a0 810 __free_page(pfn_to_page(persistent_gnt->pfn));
0a8704a5
RPM
811 kfree(persistent_gnt);
812 }
813 info->persistent_gnts_c = 0;
814 }
815
9f27ee59
JF
816 /* No more gnttab callback work. */
817 gnttab_cancel_free_callback(&info->callback);
3467811e 818 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
819
820 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 821 flush_work(&info->work);
9f27ee59
JF
822
823 /* Free resources associated with old device channel. */
824 if (info->ring_ref != GRANT_INVALID_REF) {
825 gnttab_end_foreign_access(info->ring_ref, 0,
826 (unsigned long)info->ring.sring);
827 info->ring_ref = GRANT_INVALID_REF;
828 info->ring.sring = NULL;
829 }
830 if (info->irq)
831 unbind_from_irqhandler(info->irq, info);
832 info->evtchn = info->irq = 0;
833
834}
835
0a8704a5
RPM
836static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
837 struct blkif_response *bret)
9f27ee59 838{
d62f6918 839 int i = 0;
0a8704a5
RPM
840 struct bio_vec *bvec;
841 struct req_iterator iter;
842 unsigned long flags;
843 char *bvec_data;
844 void *shared_data;
845 unsigned int offset = 0;
846
847 if (bret->operation == BLKIF_OP_READ) {
848 /*
849 * Copy the data received from the backend into the bvec.
850 * Since bv_offset can be different than 0, and bv_len different
851 * than PAGE_SIZE, we have to keep track of the current offset,
852 * to be sure we are copying the data from the right shared page.
853 */
854 rq_for_each_segment(bvec, s->request, iter) {
855 BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
d62f6918
RPM
856 if (bvec->bv_offset < offset)
857 i++;
cb5bd4d1 858 BUG_ON(i >= s->req.u.rw.nr_segments);
0a8704a5
RPM
859 shared_data = kmap_atomic(
860 pfn_to_page(s->grants_used[i]->pfn));
861 bvec_data = bvec_kmap_irq(bvec, &flags);
862 memcpy(bvec_data, shared_data + bvec->bv_offset,
863 bvec->bv_len);
864 bvec_kunmap_irq(bvec_data, &flags);
865 kunmap_atomic(shared_data);
d62f6918 866 offset = bvec->bv_offset + bvec->bv_len;
0a8704a5
RPM
867 }
868 }
869 /* Add the persistent grant into the list of free grants */
870 for (i = 0; i < s->req.u.rw.nr_segments; i++) {
871 llist_add(&s->grants_used[i]->node, &info->persistent_gnts);
872 info->persistent_gnts_c++;
873 }
9f27ee59
JF
874}
875
876static irqreturn_t blkif_interrupt(int irq, void *dev_id)
877{
878 struct request *req;
879 struct blkif_response *bret;
880 RING_IDX i, rp;
881 unsigned long flags;
882 struct blkfront_info *info = (struct blkfront_info *)dev_id;
f530f036 883 int error;
9f27ee59 884
3467811e 885 spin_lock_irqsave(&info->io_lock, flags);
9f27ee59
JF
886
887 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
3467811e 888 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
889 return IRQ_HANDLED;
890 }
891
892 again:
893 rp = info->ring.sring->rsp_prod;
894 rmb(); /* Ensure we see queued responses up to 'rp'. */
895
896 for (i = info->ring.rsp_cons; i != rp; i++) {
897 unsigned long id;
9f27ee59
JF
898
899 bret = RING_GET_RESPONSE(&info->ring, i);
900 id = bret->id;
6878c32e
KRW
901 /*
902 * The backend has messed up and given us an id that we would
903 * never have given to it (we stamp it up to BLK_RING_SIZE -
904 * look in get_id_from_freelist.
905 */
906 if (id >= BLK_RING_SIZE) {
907 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
908 info->gd->disk_name, op_name(bret->operation), id);
909 /* We can't safely get the 'struct request' as
910 * the id is busted. */
911 continue;
912 }
a945b980 913 req = info->shadow[id].request;
9f27ee59 914
5ea42986 915 if (bret->operation != BLKIF_OP_DISCARD)
0a8704a5 916 blkif_completion(&info->shadow[id], info, bret);
9f27ee59 917
6878c32e
KRW
918 if (add_id_to_freelist(info, id)) {
919 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
920 info->gd->disk_name, op_name(bret->operation), id);
921 continue;
922 }
9f27ee59 923
f530f036 924 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
9f27ee59 925 switch (bret->operation) {
ed30bf31
LD
926 case BLKIF_OP_DISCARD:
927 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
928 struct request_queue *rq = info->rq;
6878c32e
KRW
929 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
930 info->gd->disk_name, op_name(bret->operation));
ed30bf31
LD
931 error = -EOPNOTSUPP;
932 info->feature_discard = 0;
5ea42986 933 info->feature_secdiscard = 0;
ed30bf31 934 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
5ea42986 935 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31
LD
936 }
937 __blk_end_request_all(req, error);
938 break;
edf6ef59 939 case BLKIF_OP_FLUSH_DISKCACHE:
9f27ee59
JF
940 case BLKIF_OP_WRITE_BARRIER:
941 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
6878c32e
KRW
942 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
943 info->gd->disk_name, op_name(bret->operation));
f530f036 944 error = -EOPNOTSUPP;
dcb8baec
JF
945 }
946 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
97e36834 947 info->shadow[id].req.u.rw.nr_segments == 0)) {
6878c32e
KRW
948 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
949 info->gd->disk_name, op_name(bret->operation));
dcb8baec
JF
950 error = -EOPNOTSUPP;
951 }
952 if (unlikely(error)) {
953 if (error == -EOPNOTSUPP)
954 error = 0;
4913efe4 955 info->feature_flush = 0;
edf6ef59 956 info->flush_op = 0;
4913efe4 957 xlvbd_flush(info);
9f27ee59
JF
958 }
959 /* fall through */
960 case BLKIF_OP_READ:
961 case BLKIF_OP_WRITE:
962 if (unlikely(bret->status != BLKIF_RSP_OKAY))
963 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
964 "request: %x\n", bret->status);
965
40cbbb78 966 __blk_end_request_all(req, error);
9f27ee59
JF
967 break;
968 default:
969 BUG();
970 }
971 }
972
973 info->ring.rsp_cons = i;
974
975 if (i != info->ring.req_prod_pvt) {
976 int more_to_do;
977 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
978 if (more_to_do)
979 goto again;
980 } else
981 info->ring.sring->rsp_event = i + 1;
982
983 kick_pending_request_queues(info);
984
3467811e 985 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
986
987 return IRQ_HANDLED;
988}
989
990
991static int setup_blkring(struct xenbus_device *dev,
992 struct blkfront_info *info)
993{
994 struct blkif_sring *sring;
995 int err;
996
997 info->ring_ref = GRANT_INVALID_REF;
998
a144ff09 999 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
9f27ee59
JF
1000 if (!sring) {
1001 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1002 return -ENOMEM;
1003 }
1004 SHARED_RING_INIT(sring);
1005 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
9e973e64
JA
1006
1007 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
9f27ee59
JF
1008
1009 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
1010 if (err < 0) {
1011 free_page((unsigned long)sring);
1012 info->ring.sring = NULL;
1013 goto fail;
1014 }
1015 info->ring_ref = err;
1016
1017 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1018 if (err)
1019 goto fail;
1020
89c30f16
TT
1021 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1022 "blkif", info);
9f27ee59
JF
1023 if (err <= 0) {
1024 xenbus_dev_fatal(dev, err,
1025 "bind_evtchn_to_irqhandler failed");
1026 goto fail;
1027 }
1028 info->irq = err;
1029
1030 return 0;
1031fail:
1032 blkif_free(info, 0);
1033 return err;
1034}
1035
1036
1037/* Common code used when first setting up, and when resuming. */
203fd61f 1038static int talk_to_blkback(struct xenbus_device *dev,
9f27ee59
JF
1039 struct blkfront_info *info)
1040{
1041 const char *message = NULL;
1042 struct xenbus_transaction xbt;
1043 int err;
1044
1045 /* Create shared ring, alloc event channel. */
1046 err = setup_blkring(dev, info);
1047 if (err)
1048 goto out;
1049
1050again:
1051 err = xenbus_transaction_start(&xbt);
1052 if (err) {
1053 xenbus_dev_fatal(dev, err, "starting transaction");
1054 goto destroy_blkring;
1055 }
1056
1057 err = xenbus_printf(xbt, dev->nodename,
1058 "ring-ref", "%u", info->ring_ref);
1059 if (err) {
1060 message = "writing ring-ref";
1061 goto abort_transaction;
1062 }
1063 err = xenbus_printf(xbt, dev->nodename,
1064 "event-channel", "%u", info->evtchn);
1065 if (err) {
1066 message = "writing event-channel";
1067 goto abort_transaction;
1068 }
3e334239
MA
1069 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1070 XEN_IO_PROTO_ABI_NATIVE);
1071 if (err) {
1072 message = "writing protocol";
1073 goto abort_transaction;
1074 }
0a8704a5 1075 err = xenbus_printf(xbt, dev->nodename,
cb5bd4d1 1076 "feature-persistent", "%u", 1);
0a8704a5
RPM
1077 if (err)
1078 dev_warn(&dev->dev,
1079 "writing persistent grants feature to xenbus");
9f27ee59
JF
1080
1081 err = xenbus_transaction_end(xbt, 0);
1082 if (err) {
1083 if (err == -EAGAIN)
1084 goto again;
1085 xenbus_dev_fatal(dev, err, "completing transaction");
1086 goto destroy_blkring;
1087 }
1088
1089 xenbus_switch_state(dev, XenbusStateInitialised);
1090
1091 return 0;
1092
1093 abort_transaction:
1094 xenbus_transaction_end(xbt, 1);
1095 if (message)
1096 xenbus_dev_fatal(dev, err, "%s", message);
1097 destroy_blkring:
1098 blkif_free(info, 0);
1099 out:
1100 return err;
1101}
1102
9f27ee59
JF
1103/**
1104 * Entry point to this code when a new device is created. Allocate the basic
1105 * structures and the ring buffer for communication with the backend, and
1106 * inform the backend of the appropriate details for those. Switch to
1107 * Initialised state.
1108 */
1109static int blkfront_probe(struct xenbus_device *dev,
1110 const struct xenbus_device_id *id)
1111{
1112 int err, vdevice, i;
1113 struct blkfront_info *info;
1114
1115 /* FIXME: Use dynamic device id if this is not set. */
1116 err = xenbus_scanf(XBT_NIL, dev->nodename,
1117 "virtual-device", "%i", &vdevice);
1118 if (err != 1) {
9246b5f0
CL
1119 /* go looking in the extended area instead */
1120 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1121 "%i", &vdevice);
1122 if (err != 1) {
1123 xenbus_dev_fatal(dev, err, "reading virtual-device");
1124 return err;
1125 }
9f27ee59
JF
1126 }
1127
b98a409b
SS
1128 if (xen_hvm_domain()) {
1129 char *type;
1130 int len;
1131 /* no unplug has been done: do not hook devices != xen vbds */
1dc7ce99 1132 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
b98a409b
SS
1133 int major;
1134
1135 if (!VDEV_IS_EXTENDED(vdevice))
1136 major = BLKIF_MAJOR(vdevice);
1137 else
1138 major = XENVBD_MAJOR;
1139
1140 if (major != XENVBD_MAJOR) {
1141 printk(KERN_INFO
1142 "%s: HVM does not support vbd %d as xen block device\n",
1143 __FUNCTION__, vdevice);
1144 return -ENODEV;
1145 }
1146 }
1147 /* do not create a PV cdrom device if we are an HVM guest */
1148 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1149 if (IS_ERR(type))
1150 return -ENODEV;
1151 if (strncmp(type, "cdrom", 5) == 0) {
1152 kfree(type);
c1c5413a
SS
1153 return -ENODEV;
1154 }
b98a409b 1155 kfree(type);
c1c5413a 1156 }
9f27ee59
JF
1157 info = kzalloc(sizeof(*info), GFP_KERNEL);
1158 if (!info) {
1159 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1160 return -ENOMEM;
1161 }
1162
b70f5fa0 1163 mutex_init(&info->mutex);
3467811e 1164 spin_lock_init(&info->io_lock);
9f27ee59
JF
1165 info->xbdev = dev;
1166 info->vdevice = vdevice;
0a8704a5
RPM
1167 init_llist_head(&info->persistent_gnts);
1168 info->persistent_gnts_c = 0;
9f27ee59
JF
1169 info->connected = BLKIF_STATE_DISCONNECTED;
1170 INIT_WORK(&info->work, blkif_restart_queue);
1171
1172 for (i = 0; i < BLK_RING_SIZE; i++)
97e36834
KRW
1173 info->shadow[i].req.u.rw.id = i+1;
1174 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
9f27ee59
JF
1175
1176 /* Front end dir is a number, which is used as the id. */
1177 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
a1b4b12b 1178 dev_set_drvdata(&dev->dev, info);
9f27ee59 1179
203fd61f 1180 err = talk_to_blkback(dev, info);
9f27ee59
JF
1181 if (err) {
1182 kfree(info);
a1b4b12b 1183 dev_set_drvdata(&dev->dev, NULL);
9f27ee59
JF
1184 return err;
1185 }
1186
1187 return 0;
1188}
1189
1190
1191static int blkif_recover(struct blkfront_info *info)
1192{
1193 int i;
1194 struct blkif_request *req;
1195 struct blk_shadow *copy;
1196 int j;
1197
1198 /* Stage 1: Make a safe copy of the shadow state. */
a144ff09
IC
1199 copy = kmalloc(sizeof(info->shadow),
1200 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
9f27ee59
JF
1201 if (!copy)
1202 return -ENOMEM;
1203 memcpy(copy, info->shadow, sizeof(info->shadow));
1204
1205 /* Stage 2: Set up free list. */
1206 memset(&info->shadow, 0, sizeof(info->shadow));
1207 for (i = 0; i < BLK_RING_SIZE; i++)
97e36834 1208 info->shadow[i].req.u.rw.id = i+1;
9f27ee59 1209 info->shadow_free = info->ring.req_prod_pvt;
97e36834 1210 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
9f27ee59
JF
1211
1212 /* Stage 3: Find pending requests and requeue them. */
1213 for (i = 0; i < BLK_RING_SIZE; i++) {
1214 /* Not in use? */
a945b980 1215 if (!copy[i].request)
9f27ee59
JF
1216 continue;
1217
1218 /* Grab a request slot and copy shadow state into it. */
1219 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
1220 *req = copy[i].req;
1221
1222 /* We get a new request id, and must reset the shadow state. */
97e36834
KRW
1223 req->u.rw.id = get_id_from_freelist(info);
1224 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
9f27ee59 1225
5ea42986 1226 if (req->operation != BLKIF_OP_DISCARD) {
9f27ee59 1227 /* Rewrite any grant references invalidated by susp/resume. */
5ea42986
KRW
1228 for (j = 0; j < req->u.rw.nr_segments; j++)
1229 gnttab_grant_foreign_access_ref(
1230 req->u.rw.seg[j].gref,
1231 info->xbdev->otherend_id,
1232 pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
0a8704a5 1233 0);
5ea42986 1234 }
97e36834 1235 info->shadow[req->u.rw.id].req = *req;
9f27ee59
JF
1236
1237 info->ring.req_prod_pvt++;
1238 }
1239
1240 kfree(copy);
1241
1242 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1243
3467811e 1244 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1245
1246 /* Now safe for us to use the shared ring */
1247 info->connected = BLKIF_STATE_CONNECTED;
1248
1249 /* Send off requeued requests */
1250 flush_requests(info);
1251
1252 /* Kick any other new requests queued since we resumed */
1253 kick_pending_request_queues(info);
1254
3467811e 1255 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1256
1257 return 0;
1258}
1259
1260/**
1261 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1262 * driver restart. We tear down our blkif structure and recreate it, but
1263 * leave the device-layer structures intact so that this is transparent to the
1264 * rest of the kernel.
1265 */
1266static int blkfront_resume(struct xenbus_device *dev)
1267{
a1b4b12b 1268 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59
JF
1269 int err;
1270
1271 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1272
1273 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1274
203fd61f 1275 err = talk_to_blkback(dev, info);
9f27ee59
JF
1276 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
1277 err = blkif_recover(info);
1278
1279 return err;
1280}
1281
b70f5fa0
DS
1282static void
1283blkfront_closing(struct blkfront_info *info)
1284{
1285 struct xenbus_device *xbdev = info->xbdev;
1286 struct block_device *bdev = NULL;
1287
1288 mutex_lock(&info->mutex);
1289
1290 if (xbdev->state == XenbusStateClosing) {
1291 mutex_unlock(&info->mutex);
1292 return;
1293 }
1294
1295 if (info->gd)
1296 bdev = bdget_disk(info->gd, 0);
1297
1298 mutex_unlock(&info->mutex);
1299
1300 if (!bdev) {
1301 xenbus_frontend_closed(xbdev);
1302 return;
1303 }
1304
1305 mutex_lock(&bdev->bd_mutex);
1306
7b32d104 1307 if (bdev->bd_openers) {
b70f5fa0
DS
1308 xenbus_dev_error(xbdev, -EBUSY,
1309 "Device in use; refusing to close");
1310 xenbus_switch_state(xbdev, XenbusStateClosing);
1311 } else {
1312 xlvbd_release_gendisk(info);
1313 xenbus_frontend_closed(xbdev);
1314 }
1315
1316 mutex_unlock(&bdev->bd_mutex);
1317 bdput(bdev);
1318}
9f27ee59 1319
ed30bf31
LD
1320static void blkfront_setup_discard(struct blkfront_info *info)
1321{
1322 int err;
1323 char *type;
1324 unsigned int discard_granularity;
1325 unsigned int discard_alignment;
5ea42986 1326 unsigned int discard_secure;
ed30bf31
LD
1327
1328 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1329 if (IS_ERR(type))
1330 return;
1331
5ea42986 1332 info->feature_secdiscard = 0;
ed30bf31
LD
1333 if (strncmp(type, "phy", 3) == 0) {
1334 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1335 "discard-granularity", "%u", &discard_granularity,
1336 "discard-alignment", "%u", &discard_alignment,
1337 NULL);
1338 if (!err) {
1339 info->feature_discard = 1;
1340 info->discard_granularity = discard_granularity;
1341 info->discard_alignment = discard_alignment;
1342 }
5ea42986
KRW
1343 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1344 "discard-secure", "%d", &discard_secure,
1345 NULL);
1346 if (!err)
1347 info->feature_secdiscard = discard_secure;
1348
ed30bf31
LD
1349 } else if (strncmp(type, "file", 4) == 0)
1350 info->feature_discard = 1;
1351
1352 kfree(type);
1353}
1354
9f27ee59
JF
1355/*
1356 * Invoked when the backend is finally 'ready' (and has told produced
1357 * the details about the physical device - #sectors, size, etc).
1358 */
1359static void blkfront_connect(struct blkfront_info *info)
1360{
1361 unsigned long long sectors;
1362 unsigned long sector_size;
1363 unsigned int binfo;
1364 int err;
0a8704a5 1365 int barrier, flush, discard, persistent;
9f27ee59 1366
1fa73be6
S
1367 switch (info->connected) {
1368 case BLKIF_STATE_CONNECTED:
1369 /*
1370 * Potentially, the back-end may be signalling
1371 * a capacity change; update the capacity.
1372 */
1373 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1374 "sectors", "%Lu", &sectors);
1375 if (XENBUS_EXIST_ERR(err))
1376 return;
1377 printk(KERN_INFO "Setting capacity to %Lu\n",
1378 sectors);
1379 set_capacity(info->gd, sectors);
2def141e 1380 revalidate_disk(info->gd);
1fa73be6
S
1381
1382 /* fall through */
1383 case BLKIF_STATE_SUSPENDED:
9f27ee59
JF
1384 return;
1385
b4dddb49
JF
1386 default:
1387 break;
1fa73be6 1388 }
9f27ee59
JF
1389
1390 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1391 __func__, info->xbdev->otherend);
1392
1393 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1394 "sectors", "%llu", &sectors,
1395 "info", "%u", &binfo,
1396 "sector-size", "%lu", &sector_size,
1397 NULL);
1398 if (err) {
1399 xenbus_dev_fatal(info->xbdev, err,
1400 "reading backend fields at %s",
1401 info->xbdev->otherend);
1402 return;
1403 }
1404
edf6ef59
KRW
1405 info->feature_flush = 0;
1406 info->flush_op = 0;
1407
9f27ee59 1408 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
4352b47a 1409 "feature-barrier", "%d", &barrier,
9f27ee59 1410 NULL);
7901d141
JF
1411
1412 /*
1413 * If there's no "feature-barrier" defined, then it means
1414 * we're dealing with a very old backend which writes
4913efe4 1415 * synchronously; nothing to do.
7901d141 1416 *
6958f145 1417 * If there are barriers, then we use flush.
7901d141 1418 */
edf6ef59 1419 if (!err && barrier) {
be2f8373 1420 info->feature_flush = REQ_FLUSH | REQ_FUA;
edf6ef59
KRW
1421 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1422 }
1423 /*
1424 * And if there is "feature-flush-cache" use that above
1425 * barriers.
1426 */
1427 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1428 "feature-flush-cache", "%d", &flush,
1429 NULL);
9f27ee59 1430
edf6ef59
KRW
1431 if (!err && flush) {
1432 info->feature_flush = REQ_FLUSH;
1433 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1434 }
ed30bf31
LD
1435
1436 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1437 "feature-discard", "%d", &discard,
1438 NULL);
1439
1440 if (!err && discard)
1441 blkfront_setup_discard(info);
1442
0a8704a5
RPM
1443 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1444 "feature-persistent", "%u", &persistent,
1445 NULL);
1446 if (err)
1447 info->feature_persistent = 0;
1448 else
1449 info->feature_persistent = persistent;
1450
9246b5f0 1451 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
9f27ee59
JF
1452 if (err) {
1453 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1454 info->xbdev->otherend);
1455 return;
1456 }
1457
1458 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1459
1460 /* Kick pending requests. */
3467811e 1461 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1462 info->connected = BLKIF_STATE_CONNECTED;
1463 kick_pending_request_queues(info);
3467811e 1464 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1465
1466 add_disk(info->gd);
1d78d705
CL
1467
1468 info->is_ready = 1;
9f27ee59
JF
1469}
1470
9f27ee59
JF
1471/**
1472 * Callback received when the backend's state changes.
1473 */
203fd61f 1474static void blkback_changed(struct xenbus_device *dev,
9f27ee59
JF
1475 enum xenbus_state backend_state)
1476{
a1b4b12b 1477 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59 1478
203fd61f 1479 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
9f27ee59
JF
1480
1481 switch (backend_state) {
1482 case XenbusStateInitialising:
1483 case XenbusStateInitWait:
1484 case XenbusStateInitialised:
b78c9512
NI
1485 case XenbusStateReconfiguring:
1486 case XenbusStateReconfigured:
9f27ee59
JF
1487 case XenbusStateUnknown:
1488 case XenbusStateClosed:
1489 break;
1490
1491 case XenbusStateConnected:
1492 blkfront_connect(info);
1493 break;
1494
1495 case XenbusStateClosing:
b70f5fa0 1496 blkfront_closing(info);
9f27ee59
JF
1497 break;
1498 }
1499}
1500
fa1bd359 1501static int blkfront_remove(struct xenbus_device *xbdev)
9f27ee59 1502{
fa1bd359
DS
1503 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1504 struct block_device *bdev = NULL;
1505 struct gendisk *disk;
9f27ee59 1506
fa1bd359 1507 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
9f27ee59
JF
1508
1509 blkif_free(info, 0);
1510
fa1bd359
DS
1511 mutex_lock(&info->mutex);
1512
1513 disk = info->gd;
1514 if (disk)
1515 bdev = bdget_disk(disk, 0);
1516
1517 info->xbdev = NULL;
1518 mutex_unlock(&info->mutex);
1519
1520 if (!bdev) {
1521 kfree(info);
1522 return 0;
1523 }
1524
1525 /*
1526 * The xbdev was removed before we reached the Closed
1527 * state. See if it's safe to remove the disk. If the bdev
1528 * isn't closed yet, we let release take care of it.
1529 */
1530
1531 mutex_lock(&bdev->bd_mutex);
1532 info = disk->private_data;
1533
d54142c7
DS
1534 dev_warn(disk_to_dev(disk),
1535 "%s was hot-unplugged, %d stale handles\n",
1536 xbdev->nodename, bdev->bd_openers);
1537
7b32d104 1538 if (info && !bdev->bd_openers) {
fa1bd359
DS
1539 xlvbd_release_gendisk(info);
1540 disk->private_data = NULL;
0e345826 1541 kfree(info);
fa1bd359
DS
1542 }
1543
1544 mutex_unlock(&bdev->bd_mutex);
1545 bdput(bdev);
9f27ee59
JF
1546
1547 return 0;
1548}
1549
1d78d705
CL
1550static int blkfront_is_ready(struct xenbus_device *dev)
1551{
a1b4b12b 1552 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1d78d705 1553
5d7ed20e 1554 return info->is_ready && info->xbdev;
1d78d705
CL
1555}
1556
a63c848b 1557static int blkif_open(struct block_device *bdev, fmode_t mode)
9f27ee59 1558{
13961743
DS
1559 struct gendisk *disk = bdev->bd_disk;
1560 struct blkfront_info *info;
1561 int err = 0;
6e9624b8 1562
2a48fc0a 1563 mutex_lock(&blkfront_mutex);
6e9624b8 1564
13961743
DS
1565 info = disk->private_data;
1566 if (!info) {
1567 /* xbdev gone */
1568 err = -ERESTARTSYS;
1569 goto out;
1570 }
1571
1572 mutex_lock(&info->mutex);
1573
1574 if (!info->gd)
1575 /* xbdev is closed */
1576 err = -ERESTARTSYS;
1577
1578 mutex_unlock(&info->mutex);
1579
13961743 1580out:
2a48fc0a 1581 mutex_unlock(&blkfront_mutex);
13961743 1582 return err;
9f27ee59
JF
1583}
1584
a63c848b 1585static int blkif_release(struct gendisk *disk, fmode_t mode)
9f27ee59 1586{
a63c848b 1587 struct blkfront_info *info = disk->private_data;
7fd152f4
DS
1588 struct block_device *bdev;
1589 struct xenbus_device *xbdev;
1590
2a48fc0a 1591 mutex_lock(&blkfront_mutex);
7fd152f4
DS
1592
1593 bdev = bdget_disk(disk, 0);
7fd152f4 1594
acfca3c6
DS
1595 if (bdev->bd_openers)
1596 goto out;
1597
7fd152f4
DS
1598 /*
1599 * Check if we have been instructed to close. We will have
1600 * deferred this request, because the bdev was still open.
1601 */
1602
1603 mutex_lock(&info->mutex);
1604 xbdev = info->xbdev;
1605
1606 if (xbdev && xbdev->state == XenbusStateClosing) {
1607 /* pending switch to state closed */
d54142c7 1608 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
1609 xlvbd_release_gendisk(info);
1610 xenbus_frontend_closed(info->xbdev);
1611 }
1612
1613 mutex_unlock(&info->mutex);
1614
1615 if (!xbdev) {
1616 /* sudden device removal */
d54142c7 1617 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
1618 xlvbd_release_gendisk(info);
1619 disk->private_data = NULL;
1620 kfree(info);
9f27ee59 1621 }
7fd152f4 1622
a4cc14ec 1623out:
dad5cf65 1624 bdput(bdev);
2a48fc0a 1625 mutex_unlock(&blkfront_mutex);
9f27ee59
JF
1626 return 0;
1627}
1628
83d5cde4 1629static const struct block_device_operations xlvbd_block_fops =
9f27ee59
JF
1630{
1631 .owner = THIS_MODULE,
a63c848b
AV
1632 .open = blkif_open,
1633 .release = blkif_release,
597592d9 1634 .getgeo = blkif_getgeo,
8a6cfeb6 1635 .ioctl = blkif_ioctl,
9f27ee59
JF
1636};
1637
1638
ec9c42ec 1639static const struct xenbus_device_id blkfront_ids[] = {
9f27ee59
JF
1640 { "vbd" },
1641 { "" }
1642};
1643
73db144b 1644static DEFINE_XENBUS_DRIVER(blkfront, ,
9f27ee59
JF
1645 .probe = blkfront_probe,
1646 .remove = blkfront_remove,
1647 .resume = blkfront_resume,
203fd61f 1648 .otherend_changed = blkback_changed,
1d78d705 1649 .is_ready = blkfront_is_ready,
73db144b 1650);
9f27ee59
JF
1651
1652static int __init xlblk_init(void)
1653{
469738e6
LE
1654 int ret;
1655
6e833587 1656 if (!xen_domain())
9f27ee59
JF
1657 return -ENODEV;
1658
e95ae5a4 1659 if (xen_hvm_domain() && !xen_platform_pci_unplug)
b9136d20
IM
1660 return -ENODEV;
1661
9f27ee59
JF
1662 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1663 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1664 XENVBD_MAJOR, DEV_NAME);
1665 return -ENODEV;
1666 }
1667
73db144b 1668 ret = xenbus_register_frontend(&blkfront_driver);
469738e6
LE
1669 if (ret) {
1670 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1671 return ret;
1672 }
1673
1674 return 0;
9f27ee59
JF
1675}
1676module_init(xlblk_init);
1677
1678
5a60d0cd 1679static void __exit xlblk_exit(void)
9f27ee59 1680{
8605067f
JB
1681 xenbus_unregister_driver(&blkfront_driver);
1682 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1683 kfree(minors);
9f27ee59
JF
1684}
1685module_exit(xlblk_exit);
1686
1687MODULE_DESCRIPTION("Xen virtual block device frontend");
1688MODULE_LICENSE("GPL");
1689MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
d2f0c52b 1690MODULE_ALIAS("xen:vbd");
4f93f09b 1691MODULE_ALIAS("xenblk");