virtio-blk: Add bio-based IO path for virtio-blk
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / block / virtio_blk.c
1 //#define DEBUG
2 #include <linux/spinlock.h>
3 #include <linux/slab.h>
4 #include <linux/blkdev.h>
5 #include <linux/hdreg.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/virtio.h>
9 #include <linux/virtio_blk.h>
10 #include <linux/scatterlist.h>
11 #include <linux/string_helpers.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <linux/idr.h>
14
15 #define PART_BITS 4
16
17 static bool use_bio;
18 module_param(use_bio, bool, S_IRUGO);
19
20 static int major;
21 static DEFINE_IDA(vd_index_ida);
22
23 struct workqueue_struct *virtblk_wq;
24
25 struct virtio_blk
26 {
27         struct virtio_device *vdev;
28         struct virtqueue *vq;
29         wait_queue_head_t queue_wait;
30
31         /* The disk structure for the kernel. */
32         struct gendisk *disk;
33
34         mempool_t *pool;
35
36         /* Process context for config space updates */
37         struct work_struct config_work;
38
39         /* Lock for config space updates */
40         struct mutex config_lock;
41
42         /* enable config space updates */
43         bool config_enable;
44
45         /* What host tells us, plus 2 for header & tailer. */
46         unsigned int sg_elems;
47
48         /* Ida index - used to track minor number allocations. */
49         int index;
50
51         /* Scatterlist: can be too big for stack. */
52         struct scatterlist sg[/*sg_elems*/];
53 };
54
55 struct virtblk_req
56 {
57         struct request *req;
58         struct bio *bio;
59         struct virtio_blk_outhdr out_hdr;
60         struct virtio_scsi_inhdr in_hdr;
61         u8 status;
62         struct scatterlist sg[];
63 };
64
65 static 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
77 static 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
95 static 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
102 static void virtblk_done(struct virtqueue *vq)
103 {
104         struct virtio_blk *vblk = vq->vdev->priv;
105         unsigned long bio_done = 0, req_done = 0;
106         struct virtblk_req *vbr;
107         unsigned long flags;
108         unsigned int len;
109
110         spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
111         while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
112                 if (vbr->bio) {
113                         virtblk_bio_done(vblk, vbr);
114                         bio_done++;
115                 } else {
116                         virtblk_request_done(vblk, vbr);
117                         req_done++;
118                 }
119         }
120         /* In case queue is stopped waiting for more buffers. */
121         if (req_done)
122                 blk_start_queue(vblk->disk->queue);
123         spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
124
125         if (bio_done)
126                 wake_up(&vblk->queue_wait);
127 }
128
129 static 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;
139 }
140
141 static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
142                    struct request *req)
143 {
144         unsigned long num, out = 0, in = 0;
145         struct virtblk_req *vbr;
146
147         vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
148         if (!vbr)
149                 /* When another request finishes we'll try again. */
150                 return false;
151
152         vbr->req = req;
153         vbr->bio = NULL;
154         if (req->cmd_flags & REQ_FLUSH) {
155                 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
156                 vbr->out_hdr.sector = 0;
157                 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
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;
167                         vbr->out_hdr.sector = 0;
168                         vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
169                         break;
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();
178                 }
179         }
180
181         sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
182
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          */
189         if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
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
194         if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
195                 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
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                 }
211         }
212
213         if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
214                               GFP_ATOMIC) < 0) {
215                 mempool_free(vbr, vblk->pool);
216                 return false;
217         }
218
219         return true;
220 }
221
222 static void virtblk_request(struct request_queue *q)
223 {
224         struct virtio_blk *vblk = q->queuedata;
225         struct request *req;
226         unsigned int issued = 0;
227
228         while ((req = blk_peek_request(q)) != NULL) {
229                 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
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                 }
237                 blk_start_request(req);
238                 issued++;
239         }
240
241         if (issued)
242                 virtqueue_kick(vblk->vq);
243 }
244
245 static 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
272 static 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
321 /* return id (s/n) string for *disk to *id_str
322  */
323 static 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;
328         int err;
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;
342         err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
343         blk_put_request(req);
344
345         return err;
346 }
347
348 static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
349                              unsigned int cmd, unsigned long data)
350 {
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))
358                 return -ENOTTY;
359
360         return scsi_cmd_blk_ioctl(bdev, mode, cmd,
361                                   (void __user *)data);
362 }
363
364 /* We provide getgeo only to please some old bootloader/partitioning tools */
365 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
366 {
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         }
386         return 0;
387 }
388
389 static const struct block_device_operations virtblk_fops = {
390         .ioctl  = virtblk_ioctl,
391         .owner  = THIS_MODULE,
392         .getgeo = virtblk_getgeo,
393 };
394
395 static int index_to_minor(int index)
396 {
397         return index << PART_BITS;
398 }
399
400 static int minor_to_index(int minor)
401 {
402         return minor >> PART_BITS;
403 }
404
405 static 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 }
424 DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
425
426 static 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
435         mutex_lock(&vblk->config_lock);
436         if (!vblk->config_enable)
437                 goto done;
438
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);
461         revalidate_disk(vblk->disk);
462 done:
463         mutex_unlock(&vblk->config_lock);
464 }
465
466 static 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
473 static int init_vq(struct virtio_blk *vblk)
474 {
475         int err = 0;
476
477         /* We expect one virtqueue, for output. */
478         vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
479         if (IS_ERR(vblk->vq))
480                 err = PTR_ERR(vblk->vq);
481
482         return err;
483 }
484
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  */
489 static 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
513 static 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
527 static 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
532         if (writeback && !use_bio)
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
540 static const char *const virtblk_cache_types[] = {
541         "write through", "write back"
542 };
543
544 static ssize_t
545 virtblk_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
571 static ssize_t
572 virtblk_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
583 static const struct device_attribute dev_attr_cache_type_ro =
584         __ATTR(cache_type, S_IRUGO,
585                virtblk_cache_type_show, NULL);
586 static 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
590 static int __devinit virtblk_probe(struct virtio_device *vdev)
591 {
592         struct virtio_blk *vblk;
593         struct request_queue *q;
594         int err, index;
595         int pool_size;
596
597         u64 cap;
598         u32 v, blk_size, sg_elems, opt_io_size;
599         u16 min_io_size;
600         u8 physical_block_exp, alignment_offset;
601
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;
607
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);
612
613         /* We need at least one SG element, whatever they say. */
614         if (err || !sg_elems)
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);
621         if (!vblk) {
622                 err = -ENOMEM;
623                 goto out_free_index;
624         }
625
626         init_waitqueue_head(&vblk->queue_wait);
627         vblk->vdev = vdev;
628         vblk->sg_elems = sg_elems;
629         sg_init_table(vblk->sg, vblk->sg_elems);
630         mutex_init(&vblk->config_lock);
631
632         INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
633         vblk->config_enable = true;
634
635         err = init_vq(vblk);
636         if (err)
637                 goto out_free_vblk;
638
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);
643         if (!vblk->pool) {
644                 err = -ENOMEM;
645                 goto out_free_vq;
646         }
647
648         /* FIXME: How many partitions?  How long is a piece of string? */
649         vblk->disk = alloc_disk(1 << PART_BITS);
650         if (!vblk->disk) {
651                 err = -ENOMEM;
652                 goto out_mempool;
653         }
654
655         q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
656         if (!q) {
657                 err = -ENOMEM;
658                 goto out_put_disk;
659         }
660
661         if (use_bio)
662                 blk_queue_make_request(q, virtblk_make_request);
663         q->queuedata = vblk;
664
665         virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
666
667         vblk->disk->major = major;
668         vblk->disk->first_minor = index_to_minor(index);
669         vblk->disk->private_data = vblk;
670         vblk->disk->fops = &virtblk_fops;
671         vblk->disk->driverfs_dev = &vdev->dev;
672         vblk->index = index;
673
674         /* configure queue flush support */
675         virtblk_update_cache_mode(vdev);
676
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
681         /* Host must always specify the capacity. */
682         vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
683                           &cap, sizeof(cap));
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
693         /* We can handle whatever the host told us to handle. */
694         blk_queue_max_segments(q, vblk->sg_elems-2);
695
696         /* No need to bounce any requests */
697         blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
698
699         /* No real sector limit. */
700         blk_queue_max_hw_sectors(q, -1U);
701
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);
707         if (!err)
708                 blk_queue_max_segment_size(q, v);
709         else
710                 blk_queue_max_segment_size(q, -1U);
711
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)
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
747         add_disk(vblk->disk);
748         err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
749         if (err)
750                 goto out_del_disk;
751
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;
760         return 0;
761
762 out_del_disk:
763         del_gendisk(vblk->disk);
764         blk_cleanup_queue(vblk->disk->queue);
765 out_put_disk:
766         put_disk(vblk->disk);
767 out_mempool:
768         mempool_destroy(vblk->pool);
769 out_free_vq:
770         vdev->config->del_vqs(vdev);
771 out_free_vblk:
772         kfree(vblk);
773 out_free_index:
774         ida_simple_remove(&vd_index_ida, index);
775 out:
776         return err;
777 }
778
779 static void __devexit virtblk_remove(struct virtio_device *vdev)
780 {
781         struct virtio_blk *vblk = vdev->priv;
782         int index = vblk->index;
783
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);
788
789         del_gendisk(vblk->disk);
790         blk_cleanup_queue(vblk->disk->queue);
791
792         /* Stop all the virtqueues. */
793         vdev->config->reset(vdev);
794
795         flush_work(&vblk->config_work);
796
797         put_disk(vblk->disk);
798         mempool_destroy(vblk->pool);
799         vdev->config->del_vqs(vdev);
800         kfree(vblk);
801         ida_simple_remove(&vd_index_ida, index);
802 }
803
804 #ifdef CONFIG_PM
805 static 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
828 static 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
844 static const struct virtio_device_id id_table[] = {
845         { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
846         { 0 },
847 };
848
849 static unsigned int features[] = {
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,
852         VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
853 };
854
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  */
860 static struct virtio_driver __refdata virtio_blk = {
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,
869 #ifdef CONFIG_PM
870         .freeze                 = virtblk_freeze,
871         .restore                = virtblk_restore,
872 #endif
873 };
874
875 static int __init init(void)
876 {
877         int error;
878
879         virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
880         if (!virtblk_wq)
881                 return -ENOMEM;
882
883         major = register_blkdev(0, "virtblk");
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
894 out_unregister_blkdev:
895         unregister_blkdev(major, "virtblk");
896 out_destroy_workqueue:
897         destroy_workqueue(virtblk_wq);
898         return error;
899 }
900
901 static void __exit fini(void)
902 {
903         unregister_blkdev(major, "virtblk");
904         unregister_virtio_driver(&virtio_blk);
905         destroy_workqueue(virtblk_wq);
906 }
907 module_init(init);
908 module_exit(fini);
909
910 MODULE_DEVICE_TABLE(virtio, id_table);
911 MODULE_DESCRIPTION("Virtio block driver");
912 MODULE_LICENSE("GPL");