Commit | Line | Data |
---|---|---|
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 JF |
42 | #include <linux/module.h> |
43 | ||
44 | #include <xen/xenbus.h> | |
45 | #include <xen/grant_table.h> | |
46 | #include <xen/events.h> | |
47 | #include <xen/page.h> | |
48 | ||
49 | #include <xen/interface/grant_table.h> | |
50 | #include <xen/interface/io/blkif.h> | |
3e334239 | 51 | #include <xen/interface/io/protocols.h> |
9f27ee59 JF |
52 | |
53 | #include <asm/xen/hypervisor.h> | |
54 | ||
55 | enum blkif_state { | |
56 | BLKIF_STATE_DISCONNECTED, | |
57 | BLKIF_STATE_CONNECTED, | |
58 | BLKIF_STATE_SUSPENDED, | |
59 | }; | |
60 | ||
61 | struct blk_shadow { | |
62 | struct blkif_request req; | |
63 | unsigned long request; | |
64 | unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
65 | }; | |
66 | ||
67 | static struct block_device_operations xlvbd_block_fops; | |
68 | ||
69 | #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE) | |
70 | ||
71 | /* | |
72 | * We have one of these per vbd, whether ide, scsi or 'other'. They | |
73 | * hang in private_data off the gendisk structure. We may end up | |
74 | * putting all kinds of interesting stuff here :-) | |
75 | */ | |
76 | struct blkfront_info | |
77 | { | |
78 | struct xenbus_device *xbdev; | |
9f27ee59 JF |
79 | struct gendisk *gd; |
80 | int vdevice; | |
81 | blkif_vdev_t handle; | |
82 | enum blkif_state connected; | |
83 | int ring_ref; | |
84 | struct blkif_front_ring ring; | |
85 | unsigned int evtchn, irq; | |
86 | struct request_queue *rq; | |
87 | struct work_struct work; | |
88 | struct gnttab_free_callback callback; | |
89 | struct blk_shadow shadow[BLK_RING_SIZE]; | |
90 | unsigned long shadow_free; | |
91 | int feature_barrier; | |
1d78d705 | 92 | int is_ready; |
9f27ee59 JF |
93 | |
94 | /** | |
95 | * The number of people holding this device open. We won't allow a | |
96 | * hot-unplug unless this is 0. | |
97 | */ | |
98 | int users; | |
99 | }; | |
100 | ||
101 | static DEFINE_SPINLOCK(blkif_io_lock); | |
102 | ||
103 | #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ | |
104 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) | |
105 | #define GRANT_INVALID_REF 0 | |
106 | ||
107 | #define PARTS_PER_DISK 16 | |
108 | ||
109 | #define BLKIF_MAJOR(dev) ((dev)>>8) | |
110 | #define BLKIF_MINOR(dev) ((dev) & 0xff) | |
111 | ||
112 | #define DEV_NAME "xvd" /* name in /dev */ | |
113 | ||
114 | /* Information about our VBDs. */ | |
115 | #define MAX_VBDS 64 | |
116 | static LIST_HEAD(vbds_list); | |
117 | ||
118 | static int get_id_from_freelist(struct blkfront_info *info) | |
119 | { | |
120 | unsigned long free = info->shadow_free; | |
121 | BUG_ON(free > BLK_RING_SIZE); | |
122 | info->shadow_free = info->shadow[free].req.id; | |
123 | info->shadow[free].req.id = 0x0fffffee; /* debug */ | |
124 | return free; | |
125 | } | |
126 | ||
127 | static void add_id_to_freelist(struct blkfront_info *info, | |
128 | unsigned long id) | |
129 | { | |
130 | info->shadow[id].req.id = info->shadow_free; | |
131 | info->shadow[id].request = 0; | |
132 | info->shadow_free = id; | |
133 | } | |
134 | ||
135 | static void blkif_restart_queue_callback(void *arg) | |
136 | { | |
137 | struct blkfront_info *info = (struct blkfront_info *)arg; | |
138 | schedule_work(&info->work); | |
139 | } | |
140 | ||
afe42d7d | 141 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
597592d9 IC |
142 | { |
143 | /* We don't have real geometry info, but let's at least return | |
144 | values consistent with the size of the device */ | |
145 | sector_t nsect = get_capacity(bd->bd_disk); | |
146 | sector_t cylinders = nsect; | |
147 | ||
148 | hg->heads = 0xff; | |
149 | hg->sectors = 0x3f; | |
150 | sector_div(cylinders, hg->heads * hg->sectors); | |
151 | hg->cylinders = cylinders; | |
152 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) | |
153 | hg->cylinders = 0xffff; | |
154 | return 0; | |
155 | } | |
156 | ||
440a01a7 CL |
157 | int blkif_ioctl(struct inode *inode, struct file *filep, |
158 | unsigned command, unsigned long argument) | |
159 | { | |
160 | struct blkfront_info *info = | |
161 | inode->i_bdev->bd_disk->private_data; | |
162 | int i; | |
163 | ||
164 | dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n", | |
165 | command, (long)argument); | |
166 | ||
167 | switch (command) { | |
168 | case CDROMMULTISESSION: | |
169 | dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n"); | |
170 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) | |
171 | if (put_user(0, (char __user *)(argument + i))) | |
172 | return -EFAULT; | |
173 | return 0; | |
174 | ||
175 | case CDROM_GET_CAPABILITY: { | |
176 | struct gendisk *gd = info->gd; | |
177 | if (gd->flags & GENHD_FL_CD) | |
178 | return 0; | |
179 | return -EINVAL; | |
180 | } | |
181 | ||
182 | default: | |
183 | /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", | |
184 | command);*/ | |
185 | return -EINVAL; /* same return as native Linux */ | |
186 | } | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
9f27ee59 JF |
191 | /* |
192 | * blkif_queue_request | |
193 | * | |
194 | * request block io | |
195 | * | |
196 | * id: for guest use only. | |
197 | * operation: BLKIF_OP_{READ,WRITE,PROBE} | |
198 | * buffer: buffer to read/write into. this should be a | |
199 | * virtual address in the guest os. | |
200 | */ | |
201 | static int blkif_queue_request(struct request *req) | |
202 | { | |
203 | struct blkfront_info *info = req->rq_disk->private_data; | |
204 | unsigned long buffer_mfn; | |
205 | struct blkif_request *ring_req; | |
5705f702 | 206 | struct req_iterator iter; |
9f27ee59 | 207 | struct bio_vec *bvec; |
9f27ee59 JF |
208 | unsigned long id; |
209 | unsigned int fsect, lsect; | |
210 | int ref; | |
211 | grant_ref_t gref_head; | |
212 | ||
213 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) | |
214 | return 1; | |
215 | ||
216 | if (gnttab_alloc_grant_references( | |
217 | BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) { | |
218 | gnttab_request_free_callback( | |
219 | &info->callback, | |
220 | blkif_restart_queue_callback, | |
221 | info, | |
222 | BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
223 | return 1; | |
224 | } | |
225 | ||
226 | /* Fill out a communications ring structure. */ | |
227 | ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
228 | id = get_id_from_freelist(info); | |
229 | info->shadow[id].request = (unsigned long)req; | |
230 | ||
231 | ring_req->id = id; | |
232 | ring_req->sector_number = (blkif_sector_t)req->sector; | |
233 | ring_req->handle = info->handle; | |
234 | ||
235 | ring_req->operation = rq_data_dir(req) ? | |
236 | BLKIF_OP_WRITE : BLKIF_OP_READ; | |
237 | if (blk_barrier_rq(req)) | |
238 | ring_req->operation = BLKIF_OP_WRITE_BARRIER; | |
239 | ||
240 | ring_req->nr_segments = 0; | |
5705f702 | 241 | rq_for_each_segment(bvec, req, iter) { |
6c92e699 JA |
242 | BUG_ON(ring_req->nr_segments == BLKIF_MAX_SEGMENTS_PER_REQUEST); |
243 | buffer_mfn = pfn_to_mfn(page_to_pfn(bvec->bv_page)); | |
244 | fsect = bvec->bv_offset >> 9; | |
245 | lsect = fsect + (bvec->bv_len >> 9) - 1; | |
246 | /* install a grant reference. */ | |
247 | ref = gnttab_claim_grant_reference(&gref_head); | |
248 | BUG_ON(ref == -ENOSPC); | |
249 | ||
250 | gnttab_grant_foreign_access_ref( | |
9f27ee59 JF |
251 | ref, |
252 | info->xbdev->otherend_id, | |
253 | buffer_mfn, | |
254 | rq_data_dir(req) ); | |
255 | ||
6c92e699 | 256 | info->shadow[id].frame[ring_req->nr_segments] = |
9f27ee59 JF |
257 | mfn_to_pfn(buffer_mfn); |
258 | ||
6c92e699 | 259 | ring_req->seg[ring_req->nr_segments] = |
9f27ee59 JF |
260 | (struct blkif_request_segment) { |
261 | .gref = ref, | |
262 | .first_sect = fsect, | |
263 | .last_sect = lsect }; | |
264 | ||
6c92e699 | 265 | ring_req->nr_segments++; |
9f27ee59 JF |
266 | } |
267 | ||
268 | info->ring.req_prod_pvt++; | |
269 | ||
270 | /* Keep a private copy so we can reissue requests when recovering. */ | |
271 | info->shadow[id].req = *ring_req; | |
272 | ||
273 | gnttab_free_grant_references(gref_head); | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | ||
279 | static inline void flush_requests(struct blkfront_info *info) | |
280 | { | |
281 | int notify; | |
282 | ||
283 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); | |
284 | ||
285 | if (notify) | |
286 | notify_remote_via_irq(info->irq); | |
287 | } | |
288 | ||
289 | /* | |
290 | * do_blkif_request | |
291 | * read a block; request is in a request queue | |
292 | */ | |
165125e1 | 293 | static void do_blkif_request(struct request_queue *rq) |
9f27ee59 JF |
294 | { |
295 | struct blkfront_info *info = NULL; | |
296 | struct request *req; | |
297 | int queued; | |
298 | ||
299 | pr_debug("Entered do_blkif_request\n"); | |
300 | ||
301 | queued = 0; | |
302 | ||
303 | while ((req = elv_next_request(rq)) != NULL) { | |
304 | info = req->rq_disk->private_data; | |
305 | if (!blk_fs_request(req)) { | |
306 | end_request(req, 0); | |
307 | continue; | |
308 | } | |
309 | ||
310 | if (RING_FULL(&info->ring)) | |
311 | goto wait; | |
312 | ||
313 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " | |
314 | "(%u/%li) buffer:%p [%s]\n", | |
315 | req, req->cmd, (unsigned long)req->sector, | |
316 | req->current_nr_sectors, | |
317 | req->nr_sectors, req->buffer, | |
318 | rq_data_dir(req) ? "write" : "read"); | |
319 | ||
320 | ||
321 | blkdev_dequeue_request(req); | |
322 | if (blkif_queue_request(req)) { | |
323 | blk_requeue_request(rq, req); | |
324 | wait: | |
325 | /* Avoid pointless unplugs. */ | |
326 | blk_stop_queue(rq); | |
327 | break; | |
328 | } | |
329 | ||
330 | queued++; | |
331 | } | |
332 | ||
333 | if (queued != 0) | |
334 | flush_requests(info); | |
335 | } | |
336 | ||
337 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) | |
338 | { | |
165125e1 | 339 | struct request_queue *rq; |
9f27ee59 JF |
340 | |
341 | rq = blk_init_queue(do_blkif_request, &blkif_io_lock); | |
342 | if (rq == NULL) | |
343 | return -1; | |
344 | ||
345 | elevator_init(rq, "noop"); | |
346 | ||
347 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ | |
348 | blk_queue_hardsect_size(rq, sector_size); | |
349 | blk_queue_max_sectors(rq, 512); | |
350 | ||
351 | /* Each segment in a request is up to an aligned page in size. */ | |
352 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); | |
353 | blk_queue_max_segment_size(rq, PAGE_SIZE); | |
354 | ||
355 | /* Ensure a merged request will fit in a single I/O ring slot. */ | |
356 | blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
357 | blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
358 | ||
359 | /* Make sure buffer addresses are sector-aligned. */ | |
360 | blk_queue_dma_alignment(rq, 511); | |
361 | ||
1c91fe1a IC |
362 | /* Make sure we don't use bounce buffers. */ |
363 | blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY); | |
364 | ||
9f27ee59 JF |
365 | gd->queue = rq; |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
370 | ||
371 | static int xlvbd_barrier(struct blkfront_info *info) | |
372 | { | |
373 | int err; | |
374 | ||
375 | err = blk_queue_ordered(info->rq, | |
376 | info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE, | |
377 | NULL); | |
378 | ||
379 | if (err) | |
380 | return err; | |
381 | ||
382 | printk(KERN_INFO "blkfront: %s: barriers %s\n", | |
383 | info->gd->disk_name, | |
384 | info->feature_barrier ? "enabled" : "disabled"); | |
385 | return 0; | |
386 | } | |
387 | ||
388 | ||
389 | static int xlvbd_alloc_gendisk(int minor, blkif_sector_t capacity, | |
390 | int vdevice, u16 vdisk_info, u16 sector_size, | |
391 | struct blkfront_info *info) | |
392 | { | |
393 | struct gendisk *gd; | |
394 | int nr_minors = 1; | |
395 | int err = -ENODEV; | |
396 | ||
397 | BUG_ON(info->gd != NULL); | |
398 | BUG_ON(info->rq != NULL); | |
399 | ||
400 | if ((minor % PARTS_PER_DISK) == 0) | |
401 | nr_minors = PARTS_PER_DISK; | |
402 | ||
403 | gd = alloc_disk(nr_minors); | |
404 | if (gd == NULL) | |
405 | goto out; | |
406 | ||
407 | if (nr_minors > 1) | |
408 | sprintf(gd->disk_name, "%s%c", DEV_NAME, | |
409 | 'a' + minor / PARTS_PER_DISK); | |
410 | else | |
411 | sprintf(gd->disk_name, "%s%c%d", DEV_NAME, | |
412 | 'a' + minor / PARTS_PER_DISK, | |
413 | minor % PARTS_PER_DISK); | |
414 | ||
415 | gd->major = XENVBD_MAJOR; | |
416 | gd->first_minor = minor; | |
417 | gd->fops = &xlvbd_block_fops; | |
418 | gd->private_data = info; | |
419 | gd->driverfs_dev = &(info->xbdev->dev); | |
420 | set_capacity(gd, capacity); | |
421 | ||
422 | if (xlvbd_init_blk_queue(gd, sector_size)) { | |
423 | del_gendisk(gd); | |
424 | goto out; | |
425 | } | |
426 | ||
427 | info->rq = gd->queue; | |
428 | info->gd = gd; | |
429 | ||
430 | if (info->feature_barrier) | |
431 | xlvbd_barrier(info); | |
432 | ||
433 | if (vdisk_info & VDISK_READONLY) | |
434 | set_disk_ro(gd, 1); | |
435 | ||
436 | if (vdisk_info & VDISK_REMOVABLE) | |
437 | gd->flags |= GENHD_FL_REMOVABLE; | |
438 | ||
439 | if (vdisk_info & VDISK_CDROM) | |
440 | gd->flags |= GENHD_FL_CD; | |
441 | ||
442 | return 0; | |
443 | ||
444 | out: | |
445 | return err; | |
446 | } | |
447 | ||
448 | static void kick_pending_request_queues(struct blkfront_info *info) | |
449 | { | |
450 | if (!RING_FULL(&info->ring)) { | |
451 | /* Re-enable calldowns. */ | |
452 | blk_start_queue(info->rq); | |
453 | /* Kick things off immediately. */ | |
454 | do_blkif_request(info->rq); | |
455 | } | |
456 | } | |
457 | ||
458 | static void blkif_restart_queue(struct work_struct *work) | |
459 | { | |
460 | struct blkfront_info *info = container_of(work, struct blkfront_info, work); | |
461 | ||
462 | spin_lock_irq(&blkif_io_lock); | |
463 | if (info->connected == BLKIF_STATE_CONNECTED) | |
464 | kick_pending_request_queues(info); | |
465 | spin_unlock_irq(&blkif_io_lock); | |
466 | } | |
467 | ||
468 | static void blkif_free(struct blkfront_info *info, int suspend) | |
469 | { | |
470 | /* Prevent new requests being issued until we fix things up. */ | |
471 | spin_lock_irq(&blkif_io_lock); | |
472 | info->connected = suspend ? | |
473 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; | |
474 | /* No more blkif_request(). */ | |
475 | if (info->rq) | |
476 | blk_stop_queue(info->rq); | |
477 | /* No more gnttab callback work. */ | |
478 | gnttab_cancel_free_callback(&info->callback); | |
479 | spin_unlock_irq(&blkif_io_lock); | |
480 | ||
481 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
482 | flush_scheduled_work(); | |
483 | ||
484 | /* Free resources associated with old device channel. */ | |
485 | if (info->ring_ref != GRANT_INVALID_REF) { | |
486 | gnttab_end_foreign_access(info->ring_ref, 0, | |
487 | (unsigned long)info->ring.sring); | |
488 | info->ring_ref = GRANT_INVALID_REF; | |
489 | info->ring.sring = NULL; | |
490 | } | |
491 | if (info->irq) | |
492 | unbind_from_irqhandler(info->irq, info); | |
493 | info->evtchn = info->irq = 0; | |
494 | ||
495 | } | |
496 | ||
497 | static void blkif_completion(struct blk_shadow *s) | |
498 | { | |
499 | int i; | |
500 | for (i = 0; i < s->req.nr_segments; i++) | |
501 | gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL); | |
502 | } | |
503 | ||
504 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) | |
505 | { | |
506 | struct request *req; | |
507 | struct blkif_response *bret; | |
508 | RING_IDX i, rp; | |
509 | unsigned long flags; | |
510 | struct blkfront_info *info = (struct blkfront_info *)dev_id; | |
f530f036 | 511 | int error; |
9f27ee59 JF |
512 | |
513 | spin_lock_irqsave(&blkif_io_lock, flags); | |
514 | ||
515 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { | |
516 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
517 | return IRQ_HANDLED; | |
518 | } | |
519 | ||
520 | again: | |
521 | rp = info->ring.sring->rsp_prod; | |
522 | rmb(); /* Ensure we see queued responses up to 'rp'. */ | |
523 | ||
524 | for (i = info->ring.rsp_cons; i != rp; i++) { | |
525 | unsigned long id; | |
526 | int ret; | |
527 | ||
528 | bret = RING_GET_RESPONSE(&info->ring, i); | |
529 | id = bret->id; | |
530 | req = (struct request *)info->shadow[id].request; | |
531 | ||
532 | blkif_completion(&info->shadow[id]); | |
533 | ||
534 | add_id_to_freelist(info, id); | |
535 | ||
f530f036 | 536 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
9f27ee59 JF |
537 | switch (bret->operation) { |
538 | case BLKIF_OP_WRITE_BARRIER: | |
539 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { | |
540 | printk(KERN_WARNING "blkfront: %s: write barrier op failed\n", | |
541 | info->gd->disk_name); | |
f530f036 | 542 | error = -EOPNOTSUPP; |
9f27ee59 JF |
543 | info->feature_barrier = 0; |
544 | xlvbd_barrier(info); | |
545 | } | |
546 | /* fall through */ | |
547 | case BLKIF_OP_READ: | |
548 | case BLKIF_OP_WRITE: | |
549 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) | |
550 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " | |
551 | "request: %x\n", bret->status); | |
552 | ||
f530f036 | 553 | ret = __blk_end_request(req, error, blk_rq_bytes(req)); |
9f27ee59 | 554 | BUG_ON(ret); |
9f27ee59 JF |
555 | break; |
556 | default: | |
557 | BUG(); | |
558 | } | |
559 | } | |
560 | ||
561 | info->ring.rsp_cons = i; | |
562 | ||
563 | if (i != info->ring.req_prod_pvt) { | |
564 | int more_to_do; | |
565 | RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); | |
566 | if (more_to_do) | |
567 | goto again; | |
568 | } else | |
569 | info->ring.sring->rsp_event = i + 1; | |
570 | ||
571 | kick_pending_request_queues(info); | |
572 | ||
573 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
574 | ||
575 | return IRQ_HANDLED; | |
576 | } | |
577 | ||
578 | ||
579 | static int setup_blkring(struct xenbus_device *dev, | |
580 | struct blkfront_info *info) | |
581 | { | |
582 | struct blkif_sring *sring; | |
583 | int err; | |
584 | ||
585 | info->ring_ref = GRANT_INVALID_REF; | |
586 | ||
587 | sring = (struct blkif_sring *)__get_free_page(GFP_KERNEL); | |
588 | if (!sring) { | |
589 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); | |
590 | return -ENOMEM; | |
591 | } | |
592 | SHARED_RING_INIT(sring); | |
593 | FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); | |
594 | ||
595 | err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); | |
596 | if (err < 0) { | |
597 | free_page((unsigned long)sring); | |
598 | info->ring.sring = NULL; | |
599 | goto fail; | |
600 | } | |
601 | info->ring_ref = err; | |
602 | ||
603 | err = xenbus_alloc_evtchn(dev, &info->evtchn); | |
604 | if (err) | |
605 | goto fail; | |
606 | ||
607 | err = bind_evtchn_to_irqhandler(info->evtchn, | |
608 | blkif_interrupt, | |
609 | IRQF_SAMPLE_RANDOM, "blkif", info); | |
610 | if (err <= 0) { | |
611 | xenbus_dev_fatal(dev, err, | |
612 | "bind_evtchn_to_irqhandler failed"); | |
613 | goto fail; | |
614 | } | |
615 | info->irq = err; | |
616 | ||
617 | return 0; | |
618 | fail: | |
619 | blkif_free(info, 0); | |
620 | return err; | |
621 | } | |
622 | ||
623 | ||
624 | /* Common code used when first setting up, and when resuming. */ | |
625 | static int talk_to_backend(struct xenbus_device *dev, | |
626 | struct blkfront_info *info) | |
627 | { | |
628 | const char *message = NULL; | |
629 | struct xenbus_transaction xbt; | |
630 | int err; | |
631 | ||
632 | /* Create shared ring, alloc event channel. */ | |
633 | err = setup_blkring(dev, info); | |
634 | if (err) | |
635 | goto out; | |
636 | ||
637 | again: | |
638 | err = xenbus_transaction_start(&xbt); | |
639 | if (err) { | |
640 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
641 | goto destroy_blkring; | |
642 | } | |
643 | ||
644 | err = xenbus_printf(xbt, dev->nodename, | |
645 | "ring-ref", "%u", info->ring_ref); | |
646 | if (err) { | |
647 | message = "writing ring-ref"; | |
648 | goto abort_transaction; | |
649 | } | |
650 | err = xenbus_printf(xbt, dev->nodename, | |
651 | "event-channel", "%u", info->evtchn); | |
652 | if (err) { | |
653 | message = "writing event-channel"; | |
654 | goto abort_transaction; | |
655 | } | |
3e334239 MA |
656 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
657 | XEN_IO_PROTO_ABI_NATIVE); | |
658 | if (err) { | |
659 | message = "writing protocol"; | |
660 | goto abort_transaction; | |
661 | } | |
9f27ee59 JF |
662 | |
663 | err = xenbus_transaction_end(xbt, 0); | |
664 | if (err) { | |
665 | if (err == -EAGAIN) | |
666 | goto again; | |
667 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
668 | goto destroy_blkring; | |
669 | } | |
670 | ||
671 | xenbus_switch_state(dev, XenbusStateInitialised); | |
672 | ||
673 | return 0; | |
674 | ||
675 | abort_transaction: | |
676 | xenbus_transaction_end(xbt, 1); | |
677 | if (message) | |
678 | xenbus_dev_fatal(dev, err, "%s", message); | |
679 | destroy_blkring: | |
680 | blkif_free(info, 0); | |
681 | out: | |
682 | return err; | |
683 | } | |
684 | ||
685 | ||
686 | /** | |
687 | * Entry point to this code when a new device is created. Allocate the basic | |
688 | * structures and the ring buffer for communication with the backend, and | |
689 | * inform the backend of the appropriate details for those. Switch to | |
690 | * Initialised state. | |
691 | */ | |
692 | static int blkfront_probe(struct xenbus_device *dev, | |
693 | const struct xenbus_device_id *id) | |
694 | { | |
695 | int err, vdevice, i; | |
696 | struct blkfront_info *info; | |
697 | ||
698 | /* FIXME: Use dynamic device id if this is not set. */ | |
699 | err = xenbus_scanf(XBT_NIL, dev->nodename, | |
700 | "virtual-device", "%i", &vdevice); | |
701 | if (err != 1) { | |
702 | xenbus_dev_fatal(dev, err, "reading virtual-device"); | |
703 | return err; | |
704 | } | |
705 | ||
706 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
707 | if (!info) { | |
708 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); | |
709 | return -ENOMEM; | |
710 | } | |
711 | ||
712 | info->xbdev = dev; | |
713 | info->vdevice = vdevice; | |
714 | info->connected = BLKIF_STATE_DISCONNECTED; | |
715 | INIT_WORK(&info->work, blkif_restart_queue); | |
716 | ||
717 | for (i = 0; i < BLK_RING_SIZE; i++) | |
718 | info->shadow[i].req.id = i+1; | |
719 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
720 | ||
721 | /* Front end dir is a number, which is used as the id. */ | |
722 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); | |
723 | dev->dev.driver_data = info; | |
724 | ||
725 | err = talk_to_backend(dev, info); | |
726 | if (err) { | |
727 | kfree(info); | |
728 | dev->dev.driver_data = NULL; | |
729 | return err; | |
730 | } | |
731 | ||
732 | return 0; | |
733 | } | |
734 | ||
735 | ||
736 | static int blkif_recover(struct blkfront_info *info) | |
737 | { | |
738 | int i; | |
739 | struct blkif_request *req; | |
740 | struct blk_shadow *copy; | |
741 | int j; | |
742 | ||
743 | /* Stage 1: Make a safe copy of the shadow state. */ | |
744 | copy = kmalloc(sizeof(info->shadow), GFP_KERNEL); | |
745 | if (!copy) | |
746 | return -ENOMEM; | |
747 | memcpy(copy, info->shadow, sizeof(info->shadow)); | |
748 | ||
749 | /* Stage 2: Set up free list. */ | |
750 | memset(&info->shadow, 0, sizeof(info->shadow)); | |
751 | for (i = 0; i < BLK_RING_SIZE; i++) | |
752 | info->shadow[i].req.id = i+1; | |
753 | info->shadow_free = info->ring.req_prod_pvt; | |
754 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
755 | ||
756 | /* Stage 3: Find pending requests and requeue them. */ | |
757 | for (i = 0; i < BLK_RING_SIZE; i++) { | |
758 | /* Not in use? */ | |
759 | if (copy[i].request == 0) | |
760 | continue; | |
761 | ||
762 | /* Grab a request slot and copy shadow state into it. */ | |
763 | req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
764 | *req = copy[i].req; | |
765 | ||
766 | /* We get a new request id, and must reset the shadow state. */ | |
767 | req->id = get_id_from_freelist(info); | |
768 | memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); | |
769 | ||
770 | /* Rewrite any grant references invalidated by susp/resume. */ | |
771 | for (j = 0; j < req->nr_segments; j++) | |
772 | gnttab_grant_foreign_access_ref( | |
773 | req->seg[j].gref, | |
774 | info->xbdev->otherend_id, | |
775 | pfn_to_mfn(info->shadow[req->id].frame[j]), | |
776 | rq_data_dir( | |
777 | (struct request *) | |
778 | info->shadow[req->id].request)); | |
779 | info->shadow[req->id].req = *req; | |
780 | ||
781 | info->ring.req_prod_pvt++; | |
782 | } | |
783 | ||
784 | kfree(copy); | |
785 | ||
786 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
787 | ||
788 | spin_lock_irq(&blkif_io_lock); | |
789 | ||
790 | /* Now safe for us to use the shared ring */ | |
791 | info->connected = BLKIF_STATE_CONNECTED; | |
792 | ||
793 | /* Send off requeued requests */ | |
794 | flush_requests(info); | |
795 | ||
796 | /* Kick any other new requests queued since we resumed */ | |
797 | kick_pending_request_queues(info); | |
798 | ||
799 | spin_unlock_irq(&blkif_io_lock); | |
800 | ||
801 | return 0; | |
802 | } | |
803 | ||
804 | /** | |
805 | * We are reconnecting to the backend, due to a suspend/resume, or a backend | |
806 | * driver restart. We tear down our blkif structure and recreate it, but | |
807 | * leave the device-layer structures intact so that this is transparent to the | |
808 | * rest of the kernel. | |
809 | */ | |
810 | static int blkfront_resume(struct xenbus_device *dev) | |
811 | { | |
812 | struct blkfront_info *info = dev->dev.driver_data; | |
813 | int err; | |
814 | ||
815 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); | |
816 | ||
817 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); | |
818 | ||
819 | err = talk_to_backend(dev, info); | |
820 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) | |
821 | err = blkif_recover(info); | |
822 | ||
823 | return err; | |
824 | } | |
825 | ||
826 | ||
827 | /* | |
828 | * Invoked when the backend is finally 'ready' (and has told produced | |
829 | * the details about the physical device - #sectors, size, etc). | |
830 | */ | |
831 | static void blkfront_connect(struct blkfront_info *info) | |
832 | { | |
833 | unsigned long long sectors; | |
834 | unsigned long sector_size; | |
835 | unsigned int binfo; | |
836 | int err; | |
837 | ||
838 | if ((info->connected == BLKIF_STATE_CONNECTED) || | |
839 | (info->connected == BLKIF_STATE_SUSPENDED) ) | |
840 | return; | |
841 | ||
842 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", | |
843 | __func__, info->xbdev->otherend); | |
844 | ||
845 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
846 | "sectors", "%llu", §ors, | |
847 | "info", "%u", &binfo, | |
848 | "sector-size", "%lu", §or_size, | |
849 | NULL); | |
850 | if (err) { | |
851 | xenbus_dev_fatal(info->xbdev, err, | |
852 | "reading backend fields at %s", | |
853 | info->xbdev->otherend); | |
854 | return; | |
855 | } | |
856 | ||
857 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
858 | "feature-barrier", "%lu", &info->feature_barrier, | |
859 | NULL); | |
860 | if (err) | |
861 | info->feature_barrier = 0; | |
862 | ||
863 | err = xlvbd_alloc_gendisk(BLKIF_MINOR(info->vdevice), | |
864 | sectors, info->vdevice, | |
865 | binfo, sector_size, info); | |
866 | if (err) { | |
867 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", | |
868 | info->xbdev->otherend); | |
869 | return; | |
870 | } | |
871 | ||
872 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
873 | ||
874 | /* Kick pending requests. */ | |
875 | spin_lock_irq(&blkif_io_lock); | |
876 | info->connected = BLKIF_STATE_CONNECTED; | |
877 | kick_pending_request_queues(info); | |
878 | spin_unlock_irq(&blkif_io_lock); | |
879 | ||
880 | add_disk(info->gd); | |
1d78d705 CL |
881 | |
882 | info->is_ready = 1; | |
9f27ee59 JF |
883 | } |
884 | ||
885 | /** | |
886 | * Handle the change of state of the backend to Closing. We must delete our | |
887 | * device-layer structures now, to ensure that writes are flushed through to | |
888 | * the backend. Once is this done, we can switch to Closed in | |
889 | * acknowledgement. | |
890 | */ | |
891 | static void blkfront_closing(struct xenbus_device *dev) | |
892 | { | |
893 | struct blkfront_info *info = dev->dev.driver_data; | |
894 | unsigned long flags; | |
895 | ||
896 | dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename); | |
897 | ||
898 | if (info->rq == NULL) | |
899 | goto out; | |
900 | ||
901 | spin_lock_irqsave(&blkif_io_lock, flags); | |
902 | ||
903 | del_gendisk(info->gd); | |
904 | ||
905 | /* No more blkif_request(). */ | |
906 | blk_stop_queue(info->rq); | |
907 | ||
908 | /* No more gnttab callback work. */ | |
909 | gnttab_cancel_free_callback(&info->callback); | |
910 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
911 | ||
912 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
913 | flush_scheduled_work(); | |
914 | ||
915 | blk_cleanup_queue(info->rq); | |
916 | info->rq = NULL; | |
917 | ||
918 | out: | |
919 | xenbus_frontend_closed(dev); | |
920 | } | |
921 | ||
922 | /** | |
923 | * Callback received when the backend's state changes. | |
924 | */ | |
925 | static void backend_changed(struct xenbus_device *dev, | |
926 | enum xenbus_state backend_state) | |
927 | { | |
928 | struct blkfront_info *info = dev->dev.driver_data; | |
929 | struct block_device *bd; | |
930 | ||
931 | dev_dbg(&dev->dev, "blkfront:backend_changed.\n"); | |
932 | ||
933 | switch (backend_state) { | |
934 | case XenbusStateInitialising: | |
935 | case XenbusStateInitWait: | |
936 | case XenbusStateInitialised: | |
937 | case XenbusStateUnknown: | |
938 | case XenbusStateClosed: | |
939 | break; | |
940 | ||
941 | case XenbusStateConnected: | |
942 | blkfront_connect(info); | |
943 | break; | |
944 | ||
945 | case XenbusStateClosing: | |
53f0e8af | 946 | bd = bdget_disk(info->gd, 0); |
9f27ee59 JF |
947 | if (bd == NULL) |
948 | xenbus_dev_fatal(dev, -ENODEV, "bdget failed"); | |
949 | ||
950 | mutex_lock(&bd->bd_mutex); | |
951 | if (info->users > 0) | |
952 | xenbus_dev_error(dev, -EBUSY, | |
953 | "Device in use; refusing to close"); | |
954 | else | |
955 | blkfront_closing(dev); | |
956 | mutex_unlock(&bd->bd_mutex); | |
957 | bdput(bd); | |
958 | break; | |
959 | } | |
960 | } | |
961 | ||
962 | static int blkfront_remove(struct xenbus_device *dev) | |
963 | { | |
964 | struct blkfront_info *info = dev->dev.driver_data; | |
965 | ||
966 | dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename); | |
967 | ||
968 | blkif_free(info, 0); | |
969 | ||
970 | kfree(info); | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
1d78d705 CL |
975 | static int blkfront_is_ready(struct xenbus_device *dev) |
976 | { | |
977 | struct blkfront_info *info = dev->dev.driver_data; | |
978 | ||
979 | return info->is_ready; | |
980 | } | |
981 | ||
9f27ee59 JF |
982 | static int blkif_open(struct inode *inode, struct file *filep) |
983 | { | |
984 | struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; | |
985 | info->users++; | |
986 | return 0; | |
987 | } | |
988 | ||
989 | static int blkif_release(struct inode *inode, struct file *filep) | |
990 | { | |
991 | struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; | |
992 | info->users--; | |
993 | if (info->users == 0) { | |
994 | /* Check whether we have been instructed to close. We will | |
995 | have ignored this request initially, as the device was | |
996 | still mounted. */ | |
997 | struct xenbus_device *dev = info->xbdev; | |
998 | enum xenbus_state state = xenbus_read_driver_state(dev->otherend); | |
999 | ||
04c06350 | 1000 | if (state == XenbusStateClosing && info->is_ready) |
9f27ee59 JF |
1001 | blkfront_closing(dev); |
1002 | } | |
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | static struct block_device_operations xlvbd_block_fops = | |
1007 | { | |
1008 | .owner = THIS_MODULE, | |
1009 | .open = blkif_open, | |
1010 | .release = blkif_release, | |
597592d9 | 1011 | .getgeo = blkif_getgeo, |
440a01a7 | 1012 | .ioctl = blkif_ioctl, |
9f27ee59 JF |
1013 | }; |
1014 | ||
1015 | ||
1016 | static struct xenbus_device_id blkfront_ids[] = { | |
1017 | { "vbd" }, | |
1018 | { "" } | |
1019 | }; | |
1020 | ||
1021 | static struct xenbus_driver blkfront = { | |
1022 | .name = "vbd", | |
1023 | .owner = THIS_MODULE, | |
1024 | .ids = blkfront_ids, | |
1025 | .probe = blkfront_probe, | |
1026 | .remove = blkfront_remove, | |
1027 | .resume = blkfront_resume, | |
1028 | .otherend_changed = backend_changed, | |
1d78d705 | 1029 | .is_ready = blkfront_is_ready, |
9f27ee59 JF |
1030 | }; |
1031 | ||
1032 | static int __init xlblk_init(void) | |
1033 | { | |
1034 | if (!is_running_on_xen()) | |
1035 | return -ENODEV; | |
1036 | ||
1037 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { | |
1038 | printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", | |
1039 | XENVBD_MAJOR, DEV_NAME); | |
1040 | return -ENODEV; | |
1041 | } | |
1042 | ||
1043 | return xenbus_register_frontend(&blkfront); | |
1044 | } | |
1045 | module_init(xlblk_init); | |
1046 | ||
1047 | ||
1048 | static void xlblk_exit(void) | |
1049 | { | |
1050 | return xenbus_unregister_driver(&blkfront); | |
1051 | } | |
1052 | module_exit(xlblk_exit); | |
1053 | ||
1054 | MODULE_DESCRIPTION("Xen virtual block device frontend"); | |
1055 | MODULE_LICENSE("GPL"); | |
1056 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); | |
d2f0c52b | 1057 | MODULE_ALIAS("xen:vbd"); |
4f93f09b | 1058 | MODULE_ALIAS("xenblk"); |