blob: 0cfbe8c594a55c208a7727cc20a86db0e33ba704 [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
9#define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS)
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010010#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100011
Christian Borntraegerd50ed902008-02-01 09:05:00 +010012static int major, index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010013
Rusty Russelle467cde2007-10-22 11:03:38 +100014struct virtio_blk
15{
16 spinlock_t lock;
17
18 struct virtio_device *vdev;
19 struct virtqueue *vq;
20
21 /* The disk structure for the kernel. */
22 struct gendisk *disk;
23
24 /* Request tracking. */
25 struct list_head reqs;
26
27 mempool_t *pool;
28
29 /* Scatterlist: can be too big for stack. */
Jens Axboe3d1266c2007-10-24 13:21:21 +020030 struct scatterlist sg[VIRTIO_MAX_SG];
Rusty Russelle467cde2007-10-22 11:03:38 +100031};
32
33struct virtblk_req
34{
35 struct list_head list;
36 struct request *req;
37 struct virtio_blk_outhdr out_hdr;
38 struct virtio_blk_inhdr in_hdr;
39};
40
Rusty Russell18445c42008-02-04 23:49:57 -050041static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100042{
43 struct virtio_blk *vblk = vq->vdev->priv;
44 struct virtblk_req *vbr;
45 unsigned int len;
46 unsigned long flags;
47
48 spin_lock_irqsave(&vblk->lock, flags);
49 while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
50 int uptodate;
51 switch (vbr->in_hdr.status) {
52 case VIRTIO_BLK_S_OK:
53 uptodate = 1;
54 break;
55 case VIRTIO_BLK_S_UNSUPP:
56 uptodate = -ENOTTY;
57 break;
58 default:
59 uptodate = 0;
60 break;
61 }
62
63 end_dequeued_request(vbr->req, uptodate);
64 list_del(&vbr->list);
65 mempool_free(vbr, vblk->pool);
66 }
67 /* In case queue is stopped waiting for more buffers. */
68 blk_start_queue(vblk->disk->queue);
69 spin_unlock_irqrestore(&vblk->lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +100070}
71
72static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
73 struct request *req)
74{
75 unsigned long num, out, in;
76 struct virtblk_req *vbr;
77
78 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
79 if (!vbr)
80 /* When another request finishes we'll try again. */
81 return false;
82
83 vbr->req = req;
84 if (blk_fs_request(vbr->req)) {
85 vbr->out_hdr.type = 0;
86 vbr->out_hdr.sector = vbr->req->sector;
87 vbr->out_hdr.ioprio = vbr->req->ioprio;
88 } else if (blk_pc_request(vbr->req)) {
89 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
90 vbr->out_hdr.sector = 0;
91 vbr->out_hdr.ioprio = vbr->req->ioprio;
92 } else {
93 /* We don't put anything else in the queue. */
94 BUG();
95 }
96
97 if (blk_barrier_rq(vbr->req))
98 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
99
Jens Axboe3d1266c2007-10-24 13:21:21 +0200100 /* This init could be done at vblk creation time */
101 sg_init_table(vblk->sg, VIRTIO_MAX_SG);
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);
104 sg_set_buf(&vblk->sg[num+1], &vbr->in_hdr, sizeof(vbr->in_hdr));
105
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;
133 BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
134
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
149static int virtblk_ioctl(struct inode *inode, struct file *filp,
150 unsigned cmd, unsigned long data)
151{
152 return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue,
153 inode->i_bdev->bd_disk, cmd,
154 (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{
160 /* some standard values, similar to sd */
161 geo->heads = 1 << 6;
162 geo->sectors = 1 << 5;
163 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
164 return 0;
165}
166
Rusty Russelle467cde2007-10-22 11:03:38 +1000167static struct block_device_operations virtblk_fops = {
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100168 .ioctl = virtblk_ioctl,
169 .owner = THIS_MODULE,
170 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000171};
172
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100173static int index_to_minor(int index)
174{
175 return index << PART_BITS;
176}
177
Rusty Russelle467cde2007-10-22 11:03:38 +1000178static int virtblk_probe(struct virtio_device *vdev)
179{
180 struct virtio_blk *vblk;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100181 int err;
Rusty Russelle467cde2007-10-22 11:03:38 +1000182 u64 cap;
183 u32 v;
184
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100185 if (index_to_minor(index) >= 1 << MINORBITS)
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100186 return -ENOSPC;
187
Rusty Russelle467cde2007-10-22 11:03:38 +1000188 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
189 if (!vblk) {
190 err = -ENOMEM;
191 goto out;
192 }
193
194 INIT_LIST_HEAD(&vblk->reqs);
195 spin_lock_init(&vblk->lock);
196 vblk->vdev = vdev;
197
198 /* We expect one virtqueue, for output. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500199 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
Rusty Russelle467cde2007-10-22 11:03:38 +1000200 if (IS_ERR(vblk->vq)) {
201 err = PTR_ERR(vblk->vq);
202 goto out_free_vblk;
203 }
204
205 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
206 if (!vblk->pool) {
207 err = -ENOMEM;
208 goto out_free_vq;
209 }
210
Rusty Russelle467cde2007-10-22 11:03:38 +1000211 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100212 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000213 if (!vblk->disk) {
214 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100215 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000216 }
217
218 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
219 if (!vblk->disk->queue) {
220 err = -ENOMEM;
221 goto out_put_disk;
222 }
223
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100224 if (index < 26) {
225 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
226 } else if (index < (26 + 1) * 26) {
227 sprintf(vblk->disk->disk_name, "vd%c%c",
228 'a' + index / 26 - 1, 'a' + index % 26);
229 } else {
230 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
231 const unsigned int m2 = (index / 26 - 1) % 26;
232 const unsigned int m3 = index % 26;
233 sprintf(vblk->disk->disk_name, "vd%c%c%c",
234 'a' + m1, 'a' + m2, 'a' + m3);
235 }
236
Rusty Russelle467cde2007-10-22 11:03:38 +1000237 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100238 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000239 vblk->disk->private_data = vblk;
240 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500241 vblk->disk->driverfs_dev = &vdev->dev;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100242 index++;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100243
Rusty Russelle467cde2007-10-22 11:03:38 +1000244 /* If barriers are supported, tell block layer that queue is ordered */
Rusty Russella586d4f2008-02-04 23:49:56 -0500245 if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER))
Rusty Russelle467cde2007-10-22 11:03:38 +1000246 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
247
Rusty Russella586d4f2008-02-04 23:49:56 -0500248 /* Host must always specify the capacity. */
249 __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity),
250 &cap);
Rusty Russelle467cde2007-10-22 11:03:38 +1000251
252 /* If capacity is too big, truncate with warning. */
253 if ((sector_t)cap != cap) {
254 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
255 (unsigned long long)cap);
256 cap = (sector_t)-1;
257 }
258 set_capacity(vblk->disk, cap);
259
Rusty Russella586d4f2008-02-04 23:49:56 -0500260 /* Host can optionally specify maximum segment size and number of
261 * segments. */
262 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
263 offsetof(struct virtio_blk_config, size_max),
264 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000265 if (!err)
266 blk_queue_max_segment_size(vblk->disk->queue, v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000267
Rusty Russella586d4f2008-02-04 23:49:56 -0500268 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
269 offsetof(struct virtio_blk_config, seg_max),
270 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000271 if (!err)
272 blk_queue_max_hw_segments(vblk->disk->queue, v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000273
274 add_disk(vblk->disk);
275 return 0;
276
277out_put_disk:
278 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000279out_mempool:
280 mempool_destroy(vblk->pool);
281out_free_vq:
282 vdev->config->del_vq(vblk->vq);
283out_free_vblk:
284 kfree(vblk);
285out:
286 return err;
287}
288
289static void virtblk_remove(struct virtio_device *vdev)
290{
291 struct virtio_blk *vblk = vdev->priv;
292 int major = vblk->disk->major;
293
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500294 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000295 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500296
297 /* Stop all the virtqueues. */
298 vdev->config->reset(vdev);
299
Rusty Russelle467cde2007-10-22 11:03:38 +1000300 blk_cleanup_queue(vblk->disk->queue);
301 put_disk(vblk->disk);
302 unregister_blkdev(major, "virtblk");
303 mempool_destroy(vblk->pool);
Rusty Russell74b25532007-11-19 11:20:42 -0500304 vdev->config->del_vq(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000305 kfree(vblk);
306}
307
308static struct virtio_device_id id_table[] = {
309 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
310 { 0 },
311};
312
313static struct virtio_driver virtio_blk = {
314 .driver.name = KBUILD_MODNAME,
315 .driver.owner = THIS_MODULE,
316 .id_table = id_table,
317 .probe = virtblk_probe,
318 .remove = __devexit_p(virtblk_remove),
319};
320
321static int __init init(void)
322{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100323 major = register_blkdev(0, "virtblk");
324 if (major < 0)
325 return major;
Rusty Russelle467cde2007-10-22 11:03:38 +1000326 return register_virtio_driver(&virtio_blk);
327}
328
329static void __exit fini(void)
330{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100331 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000332 unregister_virtio_driver(&virtio_blk);
333}
334module_init(init);
335module_exit(fini);
336
337MODULE_DEVICE_TABLE(virtio, id_table);
338MODULE_DESCRIPTION("Virtio block driver");
339MODULE_LICENSE("GPL");