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)
12 static unsigned char virtblk_index = 'a';
13 static int major, minor;
19 struct virtio_device *vdev;
22 /* The disk structure for the kernel. */
25 /* Request tracking. */
26 struct list_head reqs;
30 /* Scatterlist: can be too big for stack. */
31 struct scatterlist sg[VIRTIO_MAX_SG];
36 struct list_head list;
38 struct virtio_blk_outhdr out_hdr;
39 struct virtio_blk_inhdr in_hdr;
42 static void blk_done(struct virtqueue *vq)
44 struct virtio_blk *vblk = vq->vdev->priv;
45 struct virtblk_req *vbr;
49 spin_lock_irqsave(&vblk->lock, flags);
50 while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
52 switch (vbr->in_hdr.status) {
56 case VIRTIO_BLK_S_UNSUPP:
64 end_dequeued_request(vbr->req, uptodate);
66 mempool_free(vbr, vblk->pool);
68 /* In case queue is stopped waiting for more buffers. */
69 blk_start_queue(vblk->disk->queue);
70 spin_unlock_irqrestore(&vblk->lock, flags);
73 static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
76 unsigned long num, out, in;
77 struct virtblk_req *vbr;
79 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
81 /* When another request finishes we'll try again. */
85 if (blk_fs_request(vbr->req)) {
86 vbr->out_hdr.type = 0;
87 vbr->out_hdr.sector = vbr->req->sector;
88 vbr->out_hdr.ioprio = vbr->req->ioprio;
89 } else if (blk_pc_request(vbr->req)) {
90 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
91 vbr->out_hdr.sector = 0;
92 vbr->out_hdr.ioprio = vbr->req->ioprio;
94 /* We don't put anything else in the queue. */
98 if (blk_barrier_rq(vbr->req))
99 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
101 /* This init could be done at vblk creation time */
102 sg_init_table(vblk->sg, VIRTIO_MAX_SG);
103 sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
104 num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
105 sg_set_buf(&vblk->sg[num+1], &vbr->in_hdr, sizeof(vbr->in_hdr));
107 if (rq_data_dir(vbr->req) == WRITE) {
108 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
112 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
117 if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
118 mempool_free(vbr, vblk->pool);
122 list_add_tail(&vbr->list, &vblk->reqs);
126 static void do_virtblk_request(struct request_queue *q)
128 struct virtio_blk *vblk = NULL;
130 unsigned int issued = 0;
132 while ((req = elv_next_request(q)) != NULL) {
133 vblk = req->rq_disk->private_data;
134 BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
136 /* If this request fails, stop queue and wait for something to
137 finish to restart it. */
138 if (!do_req(q, vblk, req)) {
142 blkdev_dequeue_request(req);
147 vblk->vq->vq_ops->kick(vblk->vq);
150 static int virtblk_ioctl(struct inode *inode, struct file *filp,
151 unsigned cmd, unsigned long data)
153 return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue,
154 inode->i_bdev->bd_disk, cmd,
155 (void __user *)data);
158 /* We provide getgeo only to please some old bootloader/partitioning tools */
159 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
161 /* some standard values, similar to sd */
163 geo->sectors = 1 << 5;
164 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
168 static struct block_device_operations virtblk_fops = {
169 .ioctl = virtblk_ioctl,
170 .owner = THIS_MODULE,
171 .getgeo = virtblk_getgeo,
174 static int virtblk_probe(struct virtio_device *vdev)
176 struct virtio_blk *vblk;
181 if (minor >= 1 << MINORBITS)
184 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
190 INIT_LIST_HEAD(&vblk->reqs);
191 spin_lock_init(&vblk->lock);
194 /* We expect one virtqueue, for output. */
195 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
196 if (IS_ERR(vblk->vq)) {
197 err = PTR_ERR(vblk->vq);
201 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
207 /* FIXME: How many partitions? How long is a piece of string? */
208 vblk->disk = alloc_disk(1 << PART_BITS);
214 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
215 if (!vblk->disk->queue) {
220 sprintf(vblk->disk->disk_name, "vd%c", virtblk_index++);
221 vblk->disk->major = major;
222 vblk->disk->first_minor = minor;
223 vblk->disk->private_data = vblk;
224 vblk->disk->fops = &virtblk_fops;
226 minor += 1 << PART_BITS;
228 /* If barriers are supported, tell block layer that queue is ordered */
229 if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER))
230 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
232 /* Host must always specify the capacity. */
233 __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity),
236 /* If capacity is too big, truncate with warning. */
237 if ((sector_t)cap != cap) {
238 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
239 (unsigned long long)cap);
242 set_capacity(vblk->disk, cap);
244 /* Host can optionally specify maximum segment size and number of
246 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
247 offsetof(struct virtio_blk_config, size_max),
250 blk_queue_max_segment_size(vblk->disk->queue, v);
252 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
253 offsetof(struct virtio_blk_config, seg_max),
256 blk_queue_max_hw_segments(vblk->disk->queue, v);
258 add_disk(vblk->disk);
262 put_disk(vblk->disk);
264 mempool_destroy(vblk->pool);
266 vdev->config->del_vq(vblk->vq);
273 static void virtblk_remove(struct virtio_device *vdev)
275 struct virtio_blk *vblk = vdev->priv;
276 int major = vblk->disk->major;
278 /* Nothing should be pending. */
279 BUG_ON(!list_empty(&vblk->reqs));
281 /* Stop all the virtqueues. */
282 vdev->config->reset(vdev);
284 blk_cleanup_queue(vblk->disk->queue);
285 put_disk(vblk->disk);
286 unregister_blkdev(major, "virtblk");
287 mempool_destroy(vblk->pool);
288 vdev->config->del_vq(vblk->vq);
292 static struct virtio_device_id id_table[] = {
293 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
297 static struct virtio_driver virtio_blk = {
298 .driver.name = KBUILD_MODNAME,
299 .driver.owner = THIS_MODULE,
300 .id_table = id_table,
301 .probe = virtblk_probe,
302 .remove = __devexit_p(virtblk_remove),
305 static int __init init(void)
307 major = register_blkdev(0, "virtblk");
310 return register_virtio_driver(&virtio_blk);
313 static void __exit fini(void)
315 unregister_blkdev(major, "virtblk");
316 unregister_virtio_driver(&virtio_blk);
321 MODULE_DEVICE_TABLE(virtio, id_table);
322 MODULE_DESCRIPTION("Virtio block driver");
323 MODULE_LICENSE("GPL");