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>
11 static int major, index;
17 struct virtio_device *vdev;
20 /* The disk structure for the kernel. */
23 /* Request tracking. */
24 struct list_head reqs;
28 /* What host tells us, plus 2 for header & tailer. */
29 unsigned int sg_elems;
31 /* Scatterlist: can be too big for stack. */
32 struct scatterlist sg[/*sg_elems*/];
37 struct list_head list;
39 struct virtio_blk_outhdr out_hdr;
43 static void blk_done(struct virtqueue *vq)
45 struct virtio_blk *vblk = vq->vdev->priv;
46 struct virtblk_req *vbr;
50 spin_lock_irqsave(&vblk->lock, flags);
51 while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
53 switch (vbr->status) {
57 case VIRTIO_BLK_S_UNSUPP:
65 __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req));
67 mempool_free(vbr, vblk->pool);
69 /* In case queue is stopped waiting for more buffers. */
70 blk_start_queue(vblk->disk->queue);
71 spin_unlock_irqrestore(&vblk->lock, flags);
74 static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
77 unsigned long num, out, in;
78 struct virtblk_req *vbr;
80 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
82 /* When another request finishes we'll try again. */
86 if (blk_fs_request(vbr->req)) {
87 vbr->out_hdr.type = 0;
88 vbr->out_hdr.sector = vbr->req->sector;
89 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
90 } else if (blk_pc_request(vbr->req)) {
91 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
92 vbr->out_hdr.sector = 0;
93 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
95 /* We don't put anything else in the queue. */
99 if (blk_barrier_rq(vbr->req))
100 vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
102 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->status, sizeof(vbr->status));
106 if (rq_data_dir(vbr->req) == WRITE) {
107 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
111 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
116 if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
117 mempool_free(vbr, vblk->pool);
121 list_add_tail(&vbr->list, &vblk->reqs);
125 static void do_virtblk_request(struct request_queue *q)
127 struct virtio_blk *vblk = NULL;
129 unsigned int issued = 0;
131 while ((req = elv_next_request(q)) != NULL) {
132 vblk = req->rq_disk->private_data;
133 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
135 /* If this request fails, stop queue and wait for something to
136 finish to restart it. */
137 if (!do_req(q, vblk, req)) {
141 blkdev_dequeue_request(req);
146 vblk->vq->vq_ops->kick(vblk->vq);
149 static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
150 unsigned cmd, unsigned long data)
152 return scsi_cmd_ioctl(bdev->bd_disk->queue,
153 bdev->bd_disk, mode, cmd,
154 (void __user *)data);
157 /* We provide getgeo only to please some old bootloader/partitioning tools */
158 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
160 struct virtio_blk *vblk = bd->bd_disk->private_data;
161 struct virtio_blk_geometry vgeo;
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),
170 geo->heads = vgeo.heads;
171 geo->sectors = vgeo.sectors;
172 geo->cylinders = vgeo.cylinders;
174 /* some standard values, similar to sd */
176 geo->sectors = 1 << 5;
177 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
182 static struct block_device_operations virtblk_fops = {
183 .locked_ioctl = virtblk_ioctl,
184 .owner = THIS_MODULE,
185 .getgeo = virtblk_getgeo,
188 static int index_to_minor(int index)
190 return index << PART_BITS;
193 static int virtblk_probe(struct virtio_device *vdev)
195 struct virtio_blk *vblk;
199 u32 blk_size, sg_elems;
201 if (index_to_minor(index) >= 1 << MINORBITS)
204 /* 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),
211 /* We need an extra sg elements at head and tail. */
213 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
214 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
220 INIT_LIST_HEAD(&vblk->reqs);
221 spin_lock_init(&vblk->lock);
223 vblk->sg_elems = sg_elems;
224 sg_init_table(vblk->sg, vblk->sg_elems);
226 /* We expect one virtqueue, for output. */
227 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
228 if (IS_ERR(vblk->vq)) {
229 err = PTR_ERR(vblk->vq);
233 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
239 /* FIXME: How many partitions? How long is a piece of string? */
240 vblk->disk = alloc_disk(1 << PART_BITS);
246 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
247 if (!vblk->disk->queue) {
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);
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);
265 vblk->disk->major = major;
266 vblk->disk->first_minor = index_to_minor(index);
267 vblk->disk->private_data = vblk;
268 vblk->disk->fops = &virtblk_fops;
269 vblk->disk->driverfs_dev = &vdev->dev;
272 /* If barriers are supported, tell block layer that queue is ordered */
273 if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
274 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
276 /* 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);
280 /* Host must always specify the capacity. */
281 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
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);
290 set_capacity(vblk->disk, cap);
292 /* 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);
296 /* No real sector limit. */
297 blk_queue_max_sectors(vblk->disk->queue, -1U);
299 /* Host can optionally specify maximum segment size and number of
301 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
302 offsetof(struct virtio_blk_config, size_max),
305 blk_queue_max_segment_size(vblk->disk->queue, v);
307 blk_queue_max_segment_size(vblk->disk->queue, -1U);
309 /* 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),
314 blk_queue_hardsect_size(vblk->disk->queue, blk_size);
316 add_disk(vblk->disk);
320 put_disk(vblk->disk);
322 mempool_destroy(vblk->pool);
324 vdev->config->del_vq(vblk->vq);
331 static void virtblk_remove(struct virtio_device *vdev)
333 struct virtio_blk *vblk = vdev->priv;
335 /* Nothing should be pending. */
336 BUG_ON(!list_empty(&vblk->reqs));
338 /* Stop all the virtqueues. */
339 vdev->config->reset(vdev);
341 del_gendisk(vblk->disk);
342 blk_cleanup_queue(vblk->disk->queue);
343 put_disk(vblk->disk);
344 mempool_destroy(vblk->pool);
345 vdev->config->del_vq(vblk->vq);
349 static struct virtio_device_id id_table[] = {
350 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
354 static unsigned int features[] = {
355 VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
356 VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
359 static struct virtio_driver virtio_blk = {
360 .feature_table = features,
361 .feature_table_size = ARRAY_SIZE(features),
362 .driver.name = KBUILD_MODNAME,
363 .driver.owner = THIS_MODULE,
364 .id_table = id_table,
365 .probe = virtblk_probe,
366 .remove = __devexit_p(virtblk_remove),
369 static int __init init(void)
371 major = register_blkdev(0, "virtblk");
374 return register_virtio_driver(&virtio_blk);
377 static void __exit fini(void)
379 unregister_blkdev(major, "virtblk");
380 unregister_virtio_driver(&virtio_blk);
385 MODULE_DEVICE_TABLE(virtio, id_table);
386 MODULE_DESCRIPTION("Virtio block driver");
387 MODULE_LICENSE("GPL");