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>
7 #include <linux/scatterlist.h>
9 #define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS)
11 static unsigned char virtblk_index = 'a';
16 struct virtio_device *vdev;
19 /* The disk structure for the kernel. */
22 /* Request tracking. */
23 struct list_head reqs;
27 /* Scatterlist: can be too big for stack. */
28 struct scatterlist sg[VIRTIO_MAX_SG];
33 struct list_head list;
35 struct virtio_blk_outhdr out_hdr;
36 struct virtio_blk_inhdr in_hdr;
39 static bool blk_done(struct virtqueue *vq)
41 struct virtio_blk *vblk = vq->vdev->priv;
42 struct virtblk_req *vbr;
46 spin_lock_irqsave(&vblk->lock, flags);
47 while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
49 switch (vbr->in_hdr.status) {
53 case VIRTIO_BLK_S_UNSUPP:
61 end_dequeued_request(vbr->req, uptodate);
63 mempool_free(vbr, vblk->pool);
65 /* In case queue is stopped waiting for more buffers. */
66 blk_start_queue(vblk->disk->queue);
67 spin_unlock_irqrestore(&vblk->lock, flags);
71 static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
74 unsigned long num, out, in;
75 struct virtblk_req *vbr;
77 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
79 /* When another request finishes we'll try again. */
83 if (blk_fs_request(vbr->req)) {
84 vbr->out_hdr.type = 0;
85 vbr->out_hdr.sector = vbr->req->sector;
86 vbr->out_hdr.ioprio = vbr->req->ioprio;
87 } else if (blk_pc_request(vbr->req)) {
88 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
89 vbr->out_hdr.sector = 0;
90 vbr->out_hdr.ioprio = vbr->req->ioprio;
92 /* We don't put anything else in the queue. */
96 if (blk_barrier_rq(vbr->req))
97 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
99 /* This init could be done at vblk creation time */
100 sg_init_table(vblk->sg, VIRTIO_MAX_SG);
101 sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
102 num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
103 sg_set_buf(&vblk->sg[num+1], &vbr->in_hdr, sizeof(vbr->in_hdr));
105 if (rq_data_dir(vbr->req) == WRITE) {
106 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
110 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
115 if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
116 mempool_free(vbr, vblk->pool);
120 list_add_tail(&vbr->list, &vblk->reqs);
124 static void do_virtblk_request(struct request_queue *q)
126 struct virtio_blk *vblk = NULL;
128 unsigned int issued = 0;
130 while ((req = elv_next_request(q)) != NULL) {
131 vblk = req->rq_disk->private_data;
132 BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
134 /* If this request fails, stop queue and wait for something to
135 finish to restart it. */
136 if (!do_req(q, vblk, req)) {
140 blkdev_dequeue_request(req);
145 vblk->vq->vq_ops->kick(vblk->vq);
148 static int virtblk_ioctl(struct inode *inode, struct file *filp,
149 unsigned cmd, unsigned long data)
151 return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue,
152 inode->i_bdev->bd_disk, cmd,
153 (void __user *)data);
156 static struct block_device_operations virtblk_fops = {
157 .ioctl = virtblk_ioctl,
158 .owner = THIS_MODULE,
161 static int virtblk_probe(struct virtio_device *vdev)
163 struct virtio_blk *vblk;
170 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
176 INIT_LIST_HEAD(&vblk->reqs);
177 spin_lock_init(&vblk->lock);
180 /* We expect one virtqueue, for output. */
181 vblk->vq = vdev->config->find_vq(vdev, blk_done);
182 if (IS_ERR(vblk->vq)) {
183 err = PTR_ERR(vblk->vq);
187 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
193 major = register_blkdev(0, "virtblk");
199 /* FIXME: How many partitions? How long is a piece of string? */
200 vblk->disk = alloc_disk(1 << 4);
203 goto out_unregister_blkdev;
206 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
207 if (!vblk->disk->queue) {
212 sprintf(vblk->disk->disk_name, "vd%c", virtblk_index++);
213 vblk->disk->major = major;
214 vblk->disk->first_minor = 0;
215 vblk->disk->private_data = vblk;
216 vblk->disk->fops = &virtblk_fops;
218 /* If barriers are supported, tell block layer that queue is ordered */
219 token = vdev->config->find(vdev, VIRTIO_CONFIG_BLK_F, &len);
220 if (virtio_use_bit(vdev, token, len, VIRTIO_BLK_F_BARRIER))
221 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
223 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_CAPACITY, &cap);
225 dev_err(&vdev->dev, "Bad/missing capacity in config\n");
229 /* If capacity is too big, truncate with warning. */
230 if ((sector_t)cap != cap) {
231 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
232 (unsigned long long)cap);
235 set_capacity(vblk->disk, cap);
237 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SIZE_MAX, &v);
239 blk_queue_max_segment_size(vblk->disk->queue, v);
240 else if (err != -ENOENT) {
241 dev_err(&vdev->dev, "Bad SIZE_MAX in config\n");
245 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SEG_MAX, &v);
247 blk_queue_max_hw_segments(vblk->disk->queue, v);
248 else if (err != -ENOENT) {
249 dev_err(&vdev->dev, "Bad SEG_MAX in config\n");
253 add_disk(vblk->disk);
257 put_disk(vblk->disk);
258 out_unregister_blkdev:
259 unregister_blkdev(major, "virtblk");
261 mempool_destroy(vblk->pool);
263 vdev->config->del_vq(vblk->vq);
270 static void virtblk_remove(struct virtio_device *vdev)
272 struct virtio_blk *vblk = vdev->priv;
273 int major = vblk->disk->major;
275 BUG_ON(!list_empty(&vblk->reqs));
276 blk_cleanup_queue(vblk->disk->queue);
277 put_disk(vblk->disk);
278 unregister_blkdev(major, "virtblk");
279 mempool_destroy(vblk->pool);
283 static struct virtio_device_id id_table[] = {
284 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
288 static struct virtio_driver virtio_blk = {
289 .driver.name = KBUILD_MODNAME,
290 .driver.owner = THIS_MODULE,
291 .id_table = id_table,
292 .probe = virtblk_probe,
293 .remove = __devexit_p(virtblk_remove),
296 static int __init init(void)
298 return register_virtio_driver(&virtio_blk);
301 static void __exit fini(void)
303 unregister_virtio_driver(&virtio_blk);
308 MODULE_DEVICE_TABLE(virtio, id_table);
309 MODULE_DESCRIPTION("Virtio block driver");
310 MODULE_LICENSE("GPL");