virtio-blk: Add bio-based IO path for virtio-blk
[deliverable/linux.git] / drivers / block / virtio_blk.c
CommitLineData
e467cde2
RR
1//#define DEBUG
2#include <linux/spinlock.h>
5a0e3ad6 3#include <linux/slab.h>
e467cde2
RR
4#include <linux/blkdev.h>
5#include <linux/hdreg.h>
0c8d44f2 6#include <linux/module.h>
4678d6f9 7#include <linux/mutex.h>
e467cde2
RR
8#include <linux/virtio.h>
9#include <linux/virtio_blk.h>
3d1266c7 10#include <linux/scatterlist.h>
7a7c924c 11#include <linux/string_helpers.h>
6917f83f 12#include <scsi/scsi_cmnd.h>
5087a50e 13#include <linux/idr.h>
3d1266c7 14
4f3bf19c 15#define PART_BITS 4
e467cde2 16
a98755c5
AH
17static bool use_bio;
18module_param(use_bio, bool, S_IRUGO);
19
5087a50e
MT
20static int major;
21static DEFINE_IDA(vd_index_ida);
22
7a7c924c 23struct workqueue_struct *virtblk_wq;
4f3bf19c 24
e467cde2
RR
25struct virtio_blk
26{
e467cde2
RR
27 struct virtio_device *vdev;
28 struct virtqueue *vq;
a98755c5 29 wait_queue_head_t queue_wait;
e467cde2
RR
30
31 /* The disk structure for the kernel. */
32 struct gendisk *disk;
33
e467cde2
RR
34 mempool_t *pool;
35
7a7c924c
CH
36 /* Process context for config space updates */
37 struct work_struct config_work;
38
4678d6f9
MT
39 /* Lock for config space updates */
40 struct mutex config_lock;
41
42 /* enable config space updates */
43 bool config_enable;
44
0864b79a
RR
45 /* What host tells us, plus 2 for header & tailer. */
46 unsigned int sg_elems;
47
5087a50e
MT
48 /* Ida index - used to track minor number allocations. */
49 int index;
50
e467cde2 51 /* Scatterlist: can be too big for stack. */
0864b79a 52 struct scatterlist sg[/*sg_elems*/];
e467cde2
RR
53};
54
55struct virtblk_req
56{
e467cde2 57 struct request *req;
a98755c5 58 struct bio *bio;
e467cde2 59 struct virtio_blk_outhdr out_hdr;
1cde26f9 60 struct virtio_scsi_inhdr in_hdr;
cb38fa23 61 u8 status;
a98755c5 62 struct scatterlist sg[];
e467cde2
RR
63};
64
a98755c5
AH
65static inline int virtblk_result(struct virtblk_req *vbr)
66{
67 switch (vbr->status) {
68 case VIRTIO_BLK_S_OK:
69 return 0;
70 case VIRTIO_BLK_S_UNSUPP:
71 return -ENOTTY;
72 default:
73 return -EIO;
74 }
75}
76
77static inline void virtblk_request_done(struct virtio_blk *vblk,
78 struct virtblk_req *vbr)
79{
80 struct request *req = vbr->req;
81 int error = virtblk_result(vbr);
82
83 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
84 req->resid_len = vbr->in_hdr.residual;
85 req->sense_len = vbr->in_hdr.sense_len;
86 req->errors = vbr->in_hdr.errors;
87 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
88 req->errors = (error != 0);
89 }
90
91 __blk_end_request_all(req, error);
92 mempool_free(vbr, vblk->pool);
93}
94
95static inline void virtblk_bio_done(struct virtio_blk *vblk,
96 struct virtblk_req *vbr)
97{
98 bio_endio(vbr->bio, virtblk_result(vbr));
99 mempool_free(vbr, vblk->pool);
100}
101
102static void virtblk_done(struct virtqueue *vq)
e467cde2
RR
103{
104 struct virtio_blk *vblk = vq->vdev->priv;
a98755c5 105 unsigned long bio_done = 0, req_done = 0;
e467cde2 106 struct virtblk_req *vbr;
e467cde2 107 unsigned long flags;
a98755c5 108 unsigned int len;
e467cde2 109
2c95a329 110 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
09ec6b69 111 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
a98755c5
AH
112 if (vbr->bio) {
113 virtblk_bio_done(vblk, vbr);
114 bio_done++;
115 } else {
116 virtblk_request_done(vblk, vbr);
117 req_done++;
33659ebb 118 }
e467cde2
RR
119 }
120 /* In case queue is stopped waiting for more buffers. */
a98755c5
AH
121 if (req_done)
122 blk_start_queue(vblk->disk->queue);
2c95a329 123 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
a98755c5
AH
124
125 if (bio_done)
126 wake_up(&vblk->queue_wait);
127}
128
129static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
130 gfp_t gfp_mask)
131{
132 struct virtblk_req *vbr;
133
134 vbr = mempool_alloc(vblk->pool, gfp_mask);
135 if (vbr && use_bio)
136 sg_init_table(vbr->sg, vblk->sg_elems);
137
138 return vbr;
e467cde2
RR
139}
140
141static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
142 struct request *req)
143{
1cde26f9 144 unsigned long num, out = 0, in = 0;
e467cde2
RR
145 struct virtblk_req *vbr;
146
a98755c5 147 vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
e467cde2
RR
148 if (!vbr)
149 /* When another request finishes we'll try again. */
150 return false;
151
152 vbr->req = req;
a98755c5 153 vbr->bio = NULL;
dd40e456
FT
154 if (req->cmd_flags & REQ_FLUSH) {
155 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
4cb2ea28 156 vbr->out_hdr.sector = 0;
157 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
dd40e456
FT
158 } else {
159 switch (req->cmd_type) {
160 case REQ_TYPE_FS:
161 vbr->out_hdr.type = 0;
162 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
163 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
164 break;
165 case REQ_TYPE_BLOCK_PC:
166 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
f1b0ef06
CH
167 vbr->out_hdr.sector = 0;
168 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
169 break;
dd40e456
FT
170 case REQ_TYPE_SPECIAL:
171 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
172 vbr->out_hdr.sector = 0;
173 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
174 break;
175 default:
176 /* We don't put anything else in the queue. */
177 BUG();
f1b0ef06 178 }
e467cde2
RR
179 }
180
1cde26f9 181 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
e467cde2 182
1cde26f9
HR
183 /*
184 * If this is a packet command we need a couple of additional headers.
185 * Behind the normal outhdr we put a segment with the scsi command
186 * block, and before the normal inhdr we put the sense data and the
187 * inhdr with additional status information before the normal inhdr.
188 */
33659ebb 189 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
1cde26f9
HR
190 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
191
192 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
193
33659ebb 194 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
6917f83f 195 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
1cde26f9
HR
196 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
197 sizeof(vbr->in_hdr));
198 }
199
200 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
201 sizeof(vbr->status));
202
203 if (num) {
204 if (rq_data_dir(vbr->req) == WRITE) {
205 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
206 out += num;
207 } else {
208 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
209 in += num;
210 }
e467cde2
RR
211 }
212
a98755c5
AH
213 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
214 GFP_ATOMIC) < 0) {
e467cde2
RR
215 mempool_free(vbr, vblk->pool);
216 return false;
217 }
218
e467cde2
RR
219 return true;
220}
221
a98755c5 222static void virtblk_request(struct request_queue *q)
e467cde2 223{
6c3b46f7 224 struct virtio_blk *vblk = q->queuedata;
e467cde2
RR
225 struct request *req;
226 unsigned int issued = 0;
227
9934c8c0 228 while ((req = blk_peek_request(q)) != NULL) {
0864b79a 229 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
e467cde2
RR
230
231 /* If this request fails, stop queue and wait for something to
232 finish to restart it. */
233 if (!do_req(q, vblk, req)) {
234 blk_stop_queue(q);
235 break;
236 }
9934c8c0 237 blk_start_request(req);
e467cde2
RR
238 issued++;
239 }
240
241 if (issued)
09ec6b69 242 virtqueue_kick(vblk->vq);
e467cde2
RR
243}
244
a98755c5
AH
245static void virtblk_add_buf_wait(struct virtio_blk *vblk,
246 struct virtblk_req *vbr,
247 unsigned long out,
248 unsigned long in)
249{
250 DEFINE_WAIT(wait);
251
252 for (;;) {
253 prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
254 TASK_UNINTERRUPTIBLE);
255
256 spin_lock_irq(vblk->disk->queue->queue_lock);
257 if (virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
258 GFP_ATOMIC) < 0) {
259 spin_unlock_irq(vblk->disk->queue->queue_lock);
260 io_schedule();
261 } else {
262 virtqueue_kick(vblk->vq);
263 spin_unlock_irq(vblk->disk->queue->queue_lock);
264 break;
265 }
266
267 }
268
269 finish_wait(&vblk->queue_wait, &wait);
270}
271
272static void virtblk_make_request(struct request_queue *q, struct bio *bio)
273{
274 struct virtio_blk *vblk = q->queuedata;
275 unsigned int num, out = 0, in = 0;
276 struct virtblk_req *vbr;
277
278 BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
279 BUG_ON(bio->bi_rw & (REQ_FLUSH | REQ_FUA));
280
281 vbr = virtblk_alloc_req(vblk, GFP_NOIO);
282 if (!vbr) {
283 bio_endio(bio, -ENOMEM);
284 return;
285 }
286
287 vbr->bio = bio;
288 vbr->req = NULL;
289 vbr->out_hdr.type = 0;
290 vbr->out_hdr.sector = bio->bi_sector;
291 vbr->out_hdr.ioprio = bio_prio(bio);
292
293 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
294
295 num = blk_bio_map_sg(q, bio, vbr->sg + out);
296
297 sg_set_buf(&vbr->sg[num + out + in++], &vbr->status,
298 sizeof(vbr->status));
299
300 if (num) {
301 if (bio->bi_rw & REQ_WRITE) {
302 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
303 out += num;
304 } else {
305 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
306 in += num;
307 }
308 }
309
310 spin_lock_irq(vblk->disk->queue->queue_lock);
311 if (unlikely(virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
312 GFP_ATOMIC) < 0)) {
313 spin_unlock_irq(vblk->disk->queue->queue_lock);
314 virtblk_add_buf_wait(vblk, vbr, out, in);
315 return;
316 }
317 virtqueue_kick(vblk->vq);
318 spin_unlock_irq(vblk->disk->queue->queue_lock);
319}
320
4cb2ea28 321/* return id (s/n) string for *disk to *id_str
322 */
323static int virtblk_get_id(struct gendisk *disk, char *id_str)
324{
325 struct virtio_blk *vblk = disk->private_data;
326 struct request *req;
327 struct bio *bio;
e4c4776d 328 int err;
4cb2ea28 329
330 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
331 GFP_KERNEL);
332 if (IS_ERR(bio))
333 return PTR_ERR(bio);
334
335 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
336 if (IS_ERR(req)) {
337 bio_put(bio);
338 return PTR_ERR(req);
339 }
340
341 req->cmd_type = REQ_TYPE_SPECIAL;
e4c4776d
MS
342 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
343 blk_put_request(req);
344
345 return err;
4cb2ea28 346}
347
fe5a50a1
CH
348static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
349 unsigned int cmd, unsigned long data)
e467cde2 350{
1cde26f9
HR
351 struct gendisk *disk = bdev->bd_disk;
352 struct virtio_blk *vblk = disk->private_data;
353
354 /*
355 * Only allow the generic SCSI ioctls if the host can support it.
356 */
357 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
d9ecdea7 358 return -ENOTTY;
1cde26f9 359
577ebb37
PB
360 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
361 (void __user *)data);
e467cde2
RR
362}
363
135da0b0
CB
364/* We provide getgeo only to please some old bootloader/partitioning tools */
365static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
366{
48e4043d
RH
367 struct virtio_blk *vblk = bd->bd_disk->private_data;
368 struct virtio_blk_geometry vgeo;
369 int err;
370
371 /* see if the host passed in geometry config */
372 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
373 offsetof(struct virtio_blk_config, geometry),
374 &vgeo);
375
376 if (!err) {
377 geo->heads = vgeo.heads;
378 geo->sectors = vgeo.sectors;
379 geo->cylinders = vgeo.cylinders;
380 } else {
381 /* some standard values, similar to sd */
382 geo->heads = 1 << 6;
383 geo->sectors = 1 << 5;
384 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
385 }
135da0b0
CB
386 return 0;
387}
388
83d5cde4 389static const struct block_device_operations virtblk_fops = {
8a6cfeb6 390 .ioctl = virtblk_ioctl,
135da0b0
CB
391 .owner = THIS_MODULE,
392 .getgeo = virtblk_getgeo,
e467cde2
RR
393};
394
d50ed907
CB
395static int index_to_minor(int index)
396{
397 return index << PART_BITS;
398}
399
5087a50e
MT
400static int minor_to_index(int minor)
401{
402 return minor >> PART_BITS;
403}
404
a5eb9e4f
RH
405static ssize_t virtblk_serial_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct gendisk *disk = dev_to_disk(dev);
409 int err;
410
411 /* sysfs gives us a PAGE_SIZE buffer */
412 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
413
414 buf[VIRTIO_BLK_ID_BYTES] = '\0';
415 err = virtblk_get_id(disk, buf);
416 if (!err)
417 return strlen(buf);
418
419 if (err == -EIO) /* Unsupported? Make it empty. */
420 return 0;
421
422 return err;
423}
424DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
425
7a7c924c
CH
426static void virtblk_config_changed_work(struct work_struct *work)
427{
428 struct virtio_blk *vblk =
429 container_of(work, struct virtio_blk, config_work);
430 struct virtio_device *vdev = vblk->vdev;
431 struct request_queue *q = vblk->disk->queue;
432 char cap_str_2[10], cap_str_10[10];
433 u64 capacity, size;
434
4678d6f9
MT
435 mutex_lock(&vblk->config_lock);
436 if (!vblk->config_enable)
437 goto done;
438
7a7c924c
CH
439 /* Host must always specify the capacity. */
440 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
441 &capacity, sizeof(capacity));
442
443 /* If capacity is too big, truncate with warning. */
444 if ((sector_t)capacity != capacity) {
445 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
446 (unsigned long long)capacity);
447 capacity = (sector_t)-1;
448 }
449
450 size = capacity * queue_logical_block_size(q);
451 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
452 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
453
454 dev_notice(&vdev->dev,
455 "new size: %llu %d-byte logical blocks (%s/%s)\n",
456 (unsigned long long)capacity,
457 queue_logical_block_size(q),
458 cap_str_10, cap_str_2);
459
460 set_capacity(vblk->disk, capacity);
e9986f30 461 revalidate_disk(vblk->disk);
4678d6f9
MT
462done:
463 mutex_unlock(&vblk->config_lock);
7a7c924c
CH
464}
465
466static void virtblk_config_changed(struct virtio_device *vdev)
467{
468 struct virtio_blk *vblk = vdev->priv;
469
470 queue_work(virtblk_wq, &vblk->config_work);
471}
472
6abd6e5a
AS
473static int init_vq(struct virtio_blk *vblk)
474{
475 int err = 0;
476
477 /* We expect one virtqueue, for output. */
a98755c5 478 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
6abd6e5a
AS
479 if (IS_ERR(vblk->vq))
480 err = PTR_ERR(vblk->vq);
481
482 return err;
483}
484
c0aa3e09
RM
485/*
486 * Legacy naming scheme used for virtio devices. We are stuck with it for
487 * virtio blk but don't ever use it for any new driver.
488 */
489static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
490{
491 const int base = 'z' - 'a' + 1;
492 char *begin = buf + strlen(prefix);
493 char *end = buf + buflen;
494 char *p;
495 int unit;
496
497 p = end - 1;
498 *p = '\0';
499 unit = base;
500 do {
501 if (p == begin)
502 return -EINVAL;
503 *--p = 'a' + (index % unit);
504 index = (index / unit) - 1;
505 } while (index >= 0);
506
507 memmove(begin, p, end - p);
508 memcpy(buf, prefix, strlen(prefix));
509
510 return 0;
511}
512
cd5d5038
PB
513static int virtblk_get_cache_mode(struct virtio_device *vdev)
514{
515 u8 writeback;
516 int err;
517
518 err = virtio_config_val(vdev, VIRTIO_BLK_F_CONFIG_WCE,
519 offsetof(struct virtio_blk_config, wce),
520 &writeback);
521 if (err)
522 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
523
524 return writeback;
525}
526
527static void virtblk_update_cache_mode(struct virtio_device *vdev)
528{
529 u8 writeback = virtblk_get_cache_mode(vdev);
530 struct virtio_blk *vblk = vdev->priv;
531
a98755c5 532 if (writeback && !use_bio)
cd5d5038
PB
533 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
534 else
535 blk_queue_flush(vblk->disk->queue, 0);
536
537 revalidate_disk(vblk->disk);
538}
539
540static const char *const virtblk_cache_types[] = {
541 "write through", "write back"
542};
543
544static ssize_t
545virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
547{
548 struct gendisk *disk = dev_to_disk(dev);
549 struct virtio_blk *vblk = disk->private_data;
550 struct virtio_device *vdev = vblk->vdev;
551 int i;
552 u8 writeback;
553
554 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
555 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
556 if (sysfs_streq(buf, virtblk_cache_types[i]))
557 break;
558
559 if (i < 0)
560 return -EINVAL;
561
562 writeback = i;
563 vdev->config->set(vdev,
564 offsetof(struct virtio_blk_config, wce),
565 &writeback, sizeof(writeback));
566
567 virtblk_update_cache_mode(vdev);
568 return count;
569}
570
571static ssize_t
572virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
573 char *buf)
574{
575 struct gendisk *disk = dev_to_disk(dev);
576 struct virtio_blk *vblk = disk->private_data;
577 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
578
579 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
580 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
581}
582
583static const struct device_attribute dev_attr_cache_type_ro =
584 __ATTR(cache_type, S_IRUGO,
585 virtblk_cache_type_show, NULL);
586static const struct device_attribute dev_attr_cache_type_rw =
587 __ATTR(cache_type, S_IRUGO|S_IWUSR,
588 virtblk_cache_type_show, virtblk_cache_type_store);
589
98e94444 590static int __devinit virtblk_probe(struct virtio_device *vdev)
e467cde2
RR
591{
592 struct virtio_blk *vblk;
69740c8b 593 struct request_queue *q;
5087a50e 594 int err, index;
a98755c5
AH
595 int pool_size;
596
e467cde2 597 u64 cap;
69740c8b
CH
598 u32 v, blk_size, sg_elems, opt_io_size;
599 u16 min_io_size;
600 u8 physical_block_exp, alignment_offset;
e467cde2 601
5087a50e
MT
602 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
603 GFP_KERNEL);
604 if (err < 0)
605 goto out;
606 index = err;
4f3bf19c 607
0864b79a
RR
608 /* We need to know how many segments before we allocate. */
609 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
610 offsetof(struct virtio_blk_config, seg_max),
611 &sg_elems);
a5b365a6
CH
612
613 /* We need at least one SG element, whatever they say. */
614 if (err || !sg_elems)
0864b79a
RR
615 sg_elems = 1;
616
617 /* We need an extra sg elements at head and tail. */
618 sg_elems += 2;
619 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
620 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
e467cde2
RR
621 if (!vblk) {
622 err = -ENOMEM;
5087a50e 623 goto out_free_index;
e467cde2
RR
624 }
625
a98755c5 626 init_waitqueue_head(&vblk->queue_wait);
e467cde2 627 vblk->vdev = vdev;
0864b79a
RR
628 vblk->sg_elems = sg_elems;
629 sg_init_table(vblk->sg, vblk->sg_elems);
4678d6f9 630 mutex_init(&vblk->config_lock);
a98755c5 631
7a7c924c 632 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
4678d6f9 633 vblk->config_enable = true;
e467cde2 634
6abd6e5a
AS
635 err = init_vq(vblk);
636 if (err)
e467cde2 637 goto out_free_vblk;
e467cde2 638
a98755c5
AH
639 pool_size = sizeof(struct virtblk_req);
640 if (use_bio)
641 pool_size += sizeof(struct scatterlist) * sg_elems;
642 vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
e467cde2
RR
643 if (!vblk->pool) {
644 err = -ENOMEM;
645 goto out_free_vq;
646 }
647
e467cde2 648 /* FIXME: How many partitions? How long is a piece of string? */
4f3bf19c 649 vblk->disk = alloc_disk(1 << PART_BITS);
e467cde2
RR
650 if (!vblk->disk) {
651 err = -ENOMEM;
4f3bf19c 652 goto out_mempool;
e467cde2
RR
653 }
654
a98755c5 655 q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
69740c8b 656 if (!q) {
e467cde2
RR
657 err = -ENOMEM;
658 goto out_put_disk;
659 }
660
a98755c5
AH
661 if (use_bio)
662 blk_queue_make_request(q, virtblk_make_request);
69740c8b 663 q->queuedata = vblk;
7d116b62 664
c0aa3e09 665 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
d50ed907 666
e467cde2 667 vblk->disk->major = major;
d50ed907 668 vblk->disk->first_minor = index_to_minor(index);
e467cde2
RR
669 vblk->disk->private_data = vblk;
670 vblk->disk->fops = &virtblk_fops;
c4839346 671 vblk->disk->driverfs_dev = &vdev->dev;
5087a50e 672 vblk->index = index;
4f3bf19c 673
02c42b7a 674 /* configure queue flush support */
cd5d5038 675 virtblk_update_cache_mode(vdev);
e467cde2 676
3ef53609
CB
677 /* If disk is read-only in the host, the guest should obey */
678 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
679 set_disk_ro(vblk->disk, 1);
680
a586d4f6 681 /* Host must always specify the capacity. */
72e61eb4
RR
682 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
683 &cap, sizeof(cap));
e467cde2
RR
684
685 /* If capacity is too big, truncate with warning. */
686 if ((sector_t)cap != cap) {
687 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
688 (unsigned long long)cap);
689 cap = (sector_t)-1;
690 }
691 set_capacity(vblk->disk, cap);
692
0864b79a 693 /* We can handle whatever the host told us to handle. */
ee714f2d 694 blk_queue_max_segments(q, vblk->sg_elems-2);
0864b79a 695
4eff3cae 696 /* No need to bounce any requests */
69740c8b 697 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
4eff3cae 698
4b7f7e20 699 /* No real sector limit. */
ee714f2d 700 blk_queue_max_hw_sectors(q, -1U);
4b7f7e20 701
a586d4f6
RR
702 /* Host can optionally specify maximum segment size and number of
703 * segments. */
704 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
705 offsetof(struct virtio_blk_config, size_max),
706 &v);
e467cde2 707 if (!err)
69740c8b 708 blk_queue_max_segment_size(q, v);
4b7f7e20 709 else
69740c8b 710 blk_queue_max_segment_size(q, -1U);
e467cde2 711
066f4d82
CB
712 /* Host can optionally specify the block size of the device */
713 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
714 offsetof(struct virtio_blk_config, blk_size),
715 &blk_size);
716 if (!err)
69740c8b
CH
717 blk_queue_logical_block_size(q, blk_size);
718 else
719 blk_size = queue_logical_block_size(q);
720
721 /* Use topology information if available */
722 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
723 offsetof(struct virtio_blk_config, physical_block_exp),
724 &physical_block_exp);
725 if (!err && physical_block_exp)
726 blk_queue_physical_block_size(q,
727 blk_size * (1 << physical_block_exp));
728
729 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
730 offsetof(struct virtio_blk_config, alignment_offset),
731 &alignment_offset);
732 if (!err && alignment_offset)
733 blk_queue_alignment_offset(q, blk_size * alignment_offset);
734
735 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
736 offsetof(struct virtio_blk_config, min_io_size),
737 &min_io_size);
738 if (!err && min_io_size)
739 blk_queue_io_min(q, blk_size * min_io_size);
740
741 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
742 offsetof(struct virtio_blk_config, opt_io_size),
743 &opt_io_size);
744 if (!err && opt_io_size)
745 blk_queue_io_opt(q, blk_size * opt_io_size);
746
e467cde2 747 add_disk(vblk->disk);
a5eb9e4f
RH
748 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
749 if (err)
750 goto out_del_disk;
751
cd5d5038
PB
752 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
753 err = device_create_file(disk_to_dev(vblk->disk),
754 &dev_attr_cache_type_rw);
755 else
756 err = device_create_file(disk_to_dev(vblk->disk),
757 &dev_attr_cache_type_ro);
758 if (err)
759 goto out_del_disk;
e467cde2
RR
760 return 0;
761
a5eb9e4f
RH
762out_del_disk:
763 del_gendisk(vblk->disk);
764 blk_cleanup_queue(vblk->disk->queue);
e467cde2
RR
765out_put_disk:
766 put_disk(vblk->disk);
e467cde2
RR
767out_mempool:
768 mempool_destroy(vblk->pool);
769out_free_vq:
d2a7ddda 770 vdev->config->del_vqs(vdev);
e467cde2
RR
771out_free_vblk:
772 kfree(vblk);
5087a50e
MT
773out_free_index:
774 ida_simple_remove(&vd_index_ida, index);
e467cde2
RR
775out:
776 return err;
777}
778
98e94444 779static void __devexit virtblk_remove(struct virtio_device *vdev)
e467cde2
RR
780{
781 struct virtio_blk *vblk = vdev->priv;
5087a50e 782 int index = vblk->index;
e467cde2 783
4678d6f9
MT
784 /* Prevent config work handler from accessing the device. */
785 mutex_lock(&vblk->config_lock);
786 vblk->config_enable = false;
787 mutex_unlock(&vblk->config_lock);
7a7c924c 788
02e2b124 789 del_gendisk(vblk->disk);
483001c7 790 blk_cleanup_queue(vblk->disk->queue);
02e2b124 791
6e5aa7ef
RR
792 /* Stop all the virtqueues. */
793 vdev->config->reset(vdev);
794
4678d6f9
MT
795 flush_work(&vblk->config_work);
796
e467cde2 797 put_disk(vblk->disk);
e467cde2 798 mempool_destroy(vblk->pool);
d2a7ddda 799 vdev->config->del_vqs(vdev);
e467cde2 800 kfree(vblk);
5087a50e 801 ida_simple_remove(&vd_index_ida, index);
e467cde2
RR
802}
803
f8fb5bc2
AS
804#ifdef CONFIG_PM
805static int virtblk_freeze(struct virtio_device *vdev)
806{
807 struct virtio_blk *vblk = vdev->priv;
808
809 /* Ensure we don't receive any more interrupts */
810 vdev->config->reset(vdev);
811
812 /* Prevent config work handler from accessing the device. */
813 mutex_lock(&vblk->config_lock);
814 vblk->config_enable = false;
815 mutex_unlock(&vblk->config_lock);
816
817 flush_work(&vblk->config_work);
818
819 spin_lock_irq(vblk->disk->queue->queue_lock);
820 blk_stop_queue(vblk->disk->queue);
821 spin_unlock_irq(vblk->disk->queue->queue_lock);
822 blk_sync_queue(vblk->disk->queue);
823
824 vdev->config->del_vqs(vdev);
825 return 0;
826}
827
828static int virtblk_restore(struct virtio_device *vdev)
829{
830 struct virtio_blk *vblk = vdev->priv;
831 int ret;
832
833 vblk->config_enable = true;
834 ret = init_vq(vdev->priv);
835 if (!ret) {
836 spin_lock_irq(vblk->disk->queue->queue_lock);
837 blk_start_queue(vblk->disk->queue);
838 spin_unlock_irq(vblk->disk->queue->queue_lock);
839 }
840 return ret;
841}
842#endif
843
47483e25 844static const struct virtio_device_id id_table[] = {
e467cde2
RR
845 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
846 { 0 },
847};
848
c45a6816 849static unsigned int features[] = {
02c42b7a
TH
850 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
851 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
cd5d5038 852 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
c45a6816
RR
853};
854
4fbfff76
RM
855/*
856 * virtio_blk causes spurious section mismatch warning by
857 * simultaneously referring to a __devinit and a __devexit function.
858 * Use __refdata to avoid this warning.
859 */
860static struct virtio_driver __refdata virtio_blk = {
7a7c924c
CH
861 .feature_table = features,
862 .feature_table_size = ARRAY_SIZE(features),
863 .driver.name = KBUILD_MODNAME,
864 .driver.owner = THIS_MODULE,
865 .id_table = id_table,
866 .probe = virtblk_probe,
867 .remove = __devexit_p(virtblk_remove),
868 .config_changed = virtblk_config_changed,
f8fb5bc2
AS
869#ifdef CONFIG_PM
870 .freeze = virtblk_freeze,
871 .restore = virtblk_restore,
872#endif
e467cde2
RR
873};
874
875static int __init init(void)
876{
7a7c924c
CH
877 int error;
878
879 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
880 if (!virtblk_wq)
881 return -ENOMEM;
882
4f3bf19c 883 major = register_blkdev(0, "virtblk");
7a7c924c
CH
884 if (major < 0) {
885 error = major;
886 goto out_destroy_workqueue;
887 }
888
889 error = register_virtio_driver(&virtio_blk);
890 if (error)
891 goto out_unregister_blkdev;
892 return 0;
893
894out_unregister_blkdev:
895 unregister_blkdev(major, "virtblk");
896out_destroy_workqueue:
897 destroy_workqueue(virtblk_wq);
898 return error;
e467cde2
RR
899}
900
901static void __exit fini(void)
902{
4f3bf19c 903 unregister_blkdev(major, "virtblk");
e467cde2 904 unregister_virtio_driver(&virtio_blk);
7a7c924c 905 destroy_workqueue(virtblk_wq);
e467cde2
RR
906}
907module_init(init);
908module_exit(fini);
909
910MODULE_DEVICE_TABLE(virtio, id_table);
911MODULE_DESCRIPTION("Virtio block driver");
912MODULE_LICENSE("GPL");
This page took 0.42392 seconds and 5 git commands to generate.