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