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