blob: 7b9b38a12d257d40f3842f8ee4d6e2d3b6cdecc9 [file] [log] [blame]
Rusty Russelle467cde2007-10-22 11:03:38 +10001//#define DEBUG
2#include <linux/spinlock.h>
3#include <linux/blkdev.h>
4#include <linux/hdreg.h>
5#include <linux/virtio.h>
6#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +02007#include <linux/scatterlist.h>
8
Christian Borntraeger4f3bf192008-01-31 15:53:53 +01009#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100010
Christian Borntraegerd50ed902008-02-01 09:05:00 +010011static int major, index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010012
Rusty Russelle467cde2007-10-22 11:03:38 +100013struct virtio_blk
14{
15 spinlock_t lock;
16
17 struct virtio_device *vdev;
18 struct virtqueue *vq;
19
20 /* The disk structure for the kernel. */
21 struct gendisk *disk;
22
23 /* Request tracking. */
24 struct list_head reqs;
25
26 mempool_t *pool;
27
Rusty Russell0864b792008-12-30 09:26:05 -060028 /* What host tells us, plus 2 for header & tailer. */
29 unsigned int sg_elems;
30
Rusty Russelle467cde2007-10-22 11:03:38 +100031 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060032 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100033};
34
35struct virtblk_req
36{
37 struct list_head list;
38 struct request *req;
39 struct virtio_blk_outhdr out_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050040 u8 status;
Rusty Russelle467cde2007-10-22 11:03:38 +100041};
42
Rusty Russell18445c42008-02-04 23:49:57 -050043static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100044{
45 struct virtio_blk *vblk = vq->vdev->priv;
46 struct virtblk_req *vbr;
47 unsigned int len;
48 unsigned long flags;
49
50 spin_lock_irqsave(&vblk->lock, flags);
51 while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
Kiyoshi Ueda83169822008-10-01 10:11:20 -040052 int error;
Rusty Russellcb38fa22008-05-02 21:50:45 -050053 switch (vbr->status) {
Rusty Russelle467cde2007-10-22 11:03:38 +100054 case VIRTIO_BLK_S_OK:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040055 error = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100056 break;
57 case VIRTIO_BLK_S_UNSUPP:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040058 error = -ENOTTY;
Rusty Russelle467cde2007-10-22 11:03:38 +100059 break;
60 default:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040061 error = -EIO;
Rusty Russelle467cde2007-10-22 11:03:38 +100062 break;
63 }
64
Kiyoshi Ueda83169822008-10-01 10:11:20 -040065 __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req));
Rusty Russelle467cde2007-10-22 11:03:38 +100066 list_del(&vbr->list);
67 mempool_free(vbr, vblk->pool);
68 }
69 /* In case queue is stopped waiting for more buffers. */
70 blk_start_queue(vblk->disk->queue);
71 spin_unlock_irqrestore(&vblk->lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +100072}
73
74static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
75 struct request *req)
76{
77 unsigned long num, out, in;
78 struct virtblk_req *vbr;
79
80 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
81 if (!vbr)
82 /* When another request finishes we'll try again. */
83 return false;
84
85 vbr->req = req;
86 if (blk_fs_request(vbr->req)) {
87 vbr->out_hdr.type = 0;
88 vbr->out_hdr.sector = vbr->req->sector;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +020089 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
Rusty Russelle467cde2007-10-22 11:03:38 +100090 } else if (blk_pc_request(vbr->req)) {
91 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
92 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +020093 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
Rusty Russelle467cde2007-10-22 11:03:38 +100094 } else {
95 /* We don't put anything else in the queue. */
96 BUG();
97 }
98
99 if (blk_barrier_rq(vbr->req))
100 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
101
Rusty Russelle467cde2007-10-22 11:03:38 +1000102 sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
103 num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
Rusty Russellcb38fa22008-05-02 21:50:45 -0500104 sg_set_buf(&vblk->sg[num+1], &vbr->status, sizeof(vbr->status));
Rusty Russelle467cde2007-10-22 11:03:38 +1000105
106 if (rq_data_dir(vbr->req) == WRITE) {
107 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
108 out = 1 + num;
109 in = 1;
110 } else {
111 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
112 out = 1;
113 in = 1 + num;
114 }
115
116 if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
117 mempool_free(vbr, vblk->pool);
118 return false;
119 }
120
121 list_add_tail(&vbr->list, &vblk->reqs);
122 return true;
123}
124
125static void do_virtblk_request(struct request_queue *q)
126{
127 struct virtio_blk *vblk = NULL;
128 struct request *req;
129 unsigned int issued = 0;
130
131 while ((req = elv_next_request(q)) != NULL) {
132 vblk = req->rq_disk->private_data;
Rusty Russell0864b792008-12-30 09:26:05 -0600133 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000134
135 /* If this request fails, stop queue and wait for something to
136 finish to restart it. */
137 if (!do_req(q, vblk, req)) {
138 blk_stop_queue(q);
139 break;
140 }
141 blkdev_dequeue_request(req);
142 issued++;
143 }
144
145 if (issued)
146 vblk->vq->vq_ops->kick(vblk->vq);
147}
148
Al Viro4e109852008-03-02 10:22:33 -0500149static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
Rusty Russelle467cde2007-10-22 11:03:38 +1000150 unsigned cmd, unsigned long data)
151{
Al Viro4e109852008-03-02 10:22:33 -0500152 return scsi_cmd_ioctl(bdev->bd_disk->queue,
153 bdev->bd_disk, mode, cmd,
Rusty Russelle467cde2007-10-22 11:03:38 +1000154 (void __user *)data);
155}
156
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100157/* We provide getgeo only to please some old bootloader/partitioning tools */
158static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
159{
Ryan Harper48e40432008-04-16 13:56:37 -0500160 struct virtio_blk *vblk = bd->bd_disk->private_data;
161 struct virtio_blk_geometry vgeo;
162 int err;
163
164 /* see if the host passed in geometry config */
165 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
166 offsetof(struct virtio_blk_config, geometry),
167 &vgeo);
168
169 if (!err) {
170 geo->heads = vgeo.heads;
171 geo->sectors = vgeo.sectors;
172 geo->cylinders = vgeo.cylinders;
173 } else {
174 /* some standard values, similar to sd */
175 geo->heads = 1 << 6;
176 geo->sectors = 1 << 5;
177 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
178 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100179 return 0;
180}
181
Rusty Russelle467cde2007-10-22 11:03:38 +1000182static struct block_device_operations virtblk_fops = {
Al Viro4e109852008-03-02 10:22:33 -0500183 .locked_ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100184 .owner = THIS_MODULE,
185 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000186};
187
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100188static int index_to_minor(int index)
189{
190 return index << PART_BITS;
191}
192
Rusty Russelle467cde2007-10-22 11:03:38 +1000193static int virtblk_probe(struct virtio_device *vdev)
194{
195 struct virtio_blk *vblk;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100196 int err;
Rusty Russelle467cde2007-10-22 11:03:38 +1000197 u64 cap;
198 u32 v;
Rusty Russell0864b792008-12-30 09:26:05 -0600199 u32 blk_size, sg_elems;
Rusty Russelle467cde2007-10-22 11:03:38 +1000200
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100201 if (index_to_minor(index) >= 1 << MINORBITS)
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100202 return -ENOSPC;
203
Rusty Russell0864b792008-12-30 09:26:05 -0600204 /* We need to know how many segments before we allocate. */
205 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
206 offsetof(struct virtio_blk_config, seg_max),
207 &sg_elems);
208 if (err)
209 sg_elems = 1;
210
211 /* We need an extra sg elements at head and tail. */
212 sg_elems += 2;
213 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
214 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000215 if (!vblk) {
216 err = -ENOMEM;
217 goto out;
218 }
219
220 INIT_LIST_HEAD(&vblk->reqs);
221 spin_lock_init(&vblk->lock);
222 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600223 vblk->sg_elems = sg_elems;
224 sg_init_table(vblk->sg, vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000225
226 /* We expect one virtqueue, for output. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500227 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
Rusty Russelle467cde2007-10-22 11:03:38 +1000228 if (IS_ERR(vblk->vq)) {
229 err = PTR_ERR(vblk->vq);
230 goto out_free_vblk;
231 }
232
233 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
234 if (!vblk->pool) {
235 err = -ENOMEM;
236 goto out_free_vq;
237 }
238
Rusty Russelle467cde2007-10-22 11:03:38 +1000239 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100240 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000241 if (!vblk->disk) {
242 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100243 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000244 }
245
246 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
247 if (!vblk->disk->queue) {
248 err = -ENOMEM;
249 goto out_put_disk;
250 }
251
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100252 if (index < 26) {
253 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
254 } else if (index < (26 + 1) * 26) {
255 sprintf(vblk->disk->disk_name, "vd%c%c",
256 'a' + index / 26 - 1, 'a' + index % 26);
257 } else {
258 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
259 const unsigned int m2 = (index / 26 - 1) % 26;
260 const unsigned int m3 = index % 26;
261 sprintf(vblk->disk->disk_name, "vd%c%c%c",
262 'a' + m1, 'a' + m2, 'a' + m3);
263 }
264
Rusty Russelle467cde2007-10-22 11:03:38 +1000265 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100266 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000267 vblk->disk->private_data = vblk;
268 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500269 vblk->disk->driverfs_dev = &vdev->dev;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100270 index++;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100271
Rusty Russelle467cde2007-10-22 11:03:38 +1000272 /* If barriers are supported, tell block layer that queue is ordered */
Rusty Russellc45a6812008-05-02 21:50:50 -0500273 if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
Rusty Russelle467cde2007-10-22 11:03:38 +1000274 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
275
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200276 /* If disk is read-only in the host, the guest should obey */
277 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
278 set_disk_ro(vblk->disk, 1);
279
Rusty Russella586d4f2008-02-04 23:49:56 -0500280 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500281 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
282 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000283
284 /* If capacity is too big, truncate with warning. */
285 if ((sector_t)cap != cap) {
286 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
287 (unsigned long long)cap);
288 cap = (sector_t)-1;
289 }
290 set_capacity(vblk->disk, cap);
291
Rusty Russell0864b792008-12-30 09:26:05 -0600292 /* We can handle whatever the host told us to handle. */
293 blk_queue_max_phys_segments(vblk->disk->queue, vblk->sg_elems-2);
294 blk_queue_max_hw_segments(vblk->disk->queue, vblk->sg_elems-2);
295
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600296 /* No real sector limit. */
297 blk_queue_max_sectors(vblk->disk->queue, -1U);
298
Rusty Russella586d4f2008-02-04 23:49:56 -0500299 /* Host can optionally specify maximum segment size and number of
300 * segments. */
301 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
302 offsetof(struct virtio_blk_config, size_max),
303 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000304 if (!err)
305 blk_queue_max_segment_size(vblk->disk->queue, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600306 else
307 blk_queue_max_segment_size(vblk->disk->queue, -1UL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000308
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200309 /* Host can optionally specify the block size of the device */
310 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
311 offsetof(struct virtio_blk_config, blk_size),
312 &blk_size);
313 if (!err)
314 blk_queue_hardsect_size(vblk->disk->queue, blk_size);
315
Rusty Russelle467cde2007-10-22 11:03:38 +1000316 add_disk(vblk->disk);
317 return 0;
318
319out_put_disk:
320 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000321out_mempool:
322 mempool_destroy(vblk->pool);
323out_free_vq:
324 vdev->config->del_vq(vblk->vq);
325out_free_vblk:
326 kfree(vblk);
327out:
328 return err;
329}
330
331static void virtblk_remove(struct virtio_device *vdev)
332{
333 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000334
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500335 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000336 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500337
338 /* Stop all the virtqueues. */
339 vdev->config->reset(vdev);
340
Chris Lalancetteac9d4632008-05-30 15:09:41 -0500341 del_gendisk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000342 blk_cleanup_queue(vblk->disk->queue);
343 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000344 mempool_destroy(vblk->pool);
Rusty Russell74b25532007-11-19 11:20:42 -0500345 vdev->config->del_vq(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000346 kfree(vblk);
347}
348
349static struct virtio_device_id id_table[] = {
350 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
351 { 0 },
352};
353
Rusty Russellc45a6812008-05-02 21:50:50 -0500354static unsigned int features[] = {
355 VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200356 VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Rusty Russellc45a6812008-05-02 21:50:50 -0500357};
358
Rusty Russelle467cde2007-10-22 11:03:38 +1000359static struct virtio_driver virtio_blk = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500360 .feature_table = features,
361 .feature_table_size = ARRAY_SIZE(features),
Rusty Russelle467cde2007-10-22 11:03:38 +1000362 .driver.name = KBUILD_MODNAME,
363 .driver.owner = THIS_MODULE,
364 .id_table = id_table,
365 .probe = virtblk_probe,
366 .remove = __devexit_p(virtblk_remove),
367};
368
369static int __init init(void)
370{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100371 major = register_blkdev(0, "virtblk");
372 if (major < 0)
373 return major;
Rusty Russelle467cde2007-10-22 11:03:38 +1000374 return register_virtio_driver(&virtio_blk);
375}
376
377static void __exit fini(void)
378{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100379 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000380 unregister_virtio_driver(&virtio_blk);
381}
382module_init(init);
383module_exit(fini);
384
385MODULE_DEVICE_TABLE(virtio, id_table);
386MODULE_DESCRIPTION("Virtio block driver");
387MODULE_LICENSE("GPL");