[PATCH] switch scsi_cmd_ioctl() to passing fmode_t
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / block / virtio_blk.c
CommitLineData
e467cde2
RR
1//#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>
3d1266c7
JA
7#include <linux/scatterlist.h>
8
9#define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS)
4f3bf19c 10#define PART_BITS 4
e467cde2 11
d50ed907 12static int major, index;
4f3bf19c 13
e467cde2
RR
14struct 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. */
3d1266c7 30 struct scatterlist sg[VIRTIO_MAX_SG];
e467cde2
RR
31};
32
33struct virtblk_req
34{
35 struct list_head list;
36 struct request *req;
37 struct virtio_blk_outhdr out_hdr;
cb38fa23 38 u8 status;
e467cde2
RR
39};
40
18445c4d 41static void blk_done(struct virtqueue *vq)
e467cde2
RR
42{
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) {
8316982a 50 int error;
cb38fa23 51 switch (vbr->status) {
e467cde2 52 case VIRTIO_BLK_S_OK:
8316982a 53 error = 0;
e467cde2
RR
54 break;
55 case VIRTIO_BLK_S_UNSUPP:
8316982a 56 error = -ENOTTY;
e467cde2
RR
57 break;
58 default:
8316982a 59 error = -EIO;
e467cde2
RR
60 break;
61 }
62
8316982a 63 __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req));
e467cde2
RR
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);
e467cde2
RR
70}
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;
766ca442 87 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
e467cde2
RR
88 } else if (blk_pc_request(vbr->req)) {
89 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
90 vbr->out_hdr.sector = 0;
766ca442 91 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
e467cde2
RR
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
3d1266c7
JA
100 /* This init could be done at vblk creation time */
101 sg_init_table(vblk->sg, VIRTIO_MAX_SG);
e467cde2
RR
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);
cb38fa23 104 sg_set_buf(&vblk->sg[num+1], &vbr->status, sizeof(vbr->status));
e467cde2
RR
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{
74f3c8af
AV
152 return scsi_cmd_ioctl(inode->i_bdev->bd_disk->queue,
153 inode->i_bdev->bd_disk, filp->f_mode, cmd,
e467cde2
RR
154 (void __user *)data);
155}
156
135da0b0
CB
157/* We provide getgeo only to please some old bootloader/partitioning tools */
158static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
159{
48e4043d
RH
160 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 }
135da0b0
CB
179 return 0;
180}
181
e467cde2 182static struct block_device_operations virtblk_fops = {
135da0b0
CB
183 .ioctl = virtblk_ioctl,
184 .owner = THIS_MODULE,
185 .getgeo = virtblk_getgeo,
e467cde2
RR
186};
187
d50ed907
CB
188static int index_to_minor(int index)
189{
190 return index << PART_BITS;
191}
192
e467cde2
RR
193static int virtblk_probe(struct virtio_device *vdev)
194{
195 struct virtio_blk *vblk;
4f3bf19c 196 int err;
e467cde2
RR
197 u64 cap;
198 u32 v;
066f4d82 199 u32 blk_size;
e467cde2 200
d50ed907 201 if (index_to_minor(index) >= 1 << MINORBITS)
4f3bf19c
CB
202 return -ENOSPC;
203
e467cde2
RR
204 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
205 if (!vblk) {
206 err = -ENOMEM;
207 goto out;
208 }
209
210 INIT_LIST_HEAD(&vblk->reqs);
211 spin_lock_init(&vblk->lock);
212 vblk->vdev = vdev;
213
214 /* We expect one virtqueue, for output. */
a586d4f6 215 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
e467cde2
RR
216 if (IS_ERR(vblk->vq)) {
217 err = PTR_ERR(vblk->vq);
218 goto out_free_vblk;
219 }
220
221 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
222 if (!vblk->pool) {
223 err = -ENOMEM;
224 goto out_free_vq;
225 }
226
e467cde2 227 /* FIXME: How many partitions? How long is a piece of string? */
4f3bf19c 228 vblk->disk = alloc_disk(1 << PART_BITS);
e467cde2
RR
229 if (!vblk->disk) {
230 err = -ENOMEM;
4f3bf19c 231 goto out_mempool;
e467cde2
RR
232 }
233
234 vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
235 if (!vblk->disk->queue) {
236 err = -ENOMEM;
237 goto out_put_disk;
238 }
239
d50ed907
CB
240 if (index < 26) {
241 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
242 } else if (index < (26 + 1) * 26) {
243 sprintf(vblk->disk->disk_name, "vd%c%c",
244 'a' + index / 26 - 1, 'a' + index % 26);
245 } else {
246 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
247 const unsigned int m2 = (index / 26 - 1) % 26;
248 const unsigned int m3 = index % 26;
249 sprintf(vblk->disk->disk_name, "vd%c%c%c",
250 'a' + m1, 'a' + m2, 'a' + m3);
251 }
252
e467cde2 253 vblk->disk->major = major;
d50ed907 254 vblk->disk->first_minor = index_to_minor(index);
e467cde2
RR
255 vblk->disk->private_data = vblk;
256 vblk->disk->fops = &virtblk_fops;
c4839346 257 vblk->disk->driverfs_dev = &vdev->dev;
d50ed907 258 index++;
4f3bf19c 259
e467cde2 260 /* If barriers are supported, tell block layer that queue is ordered */
c45a6816 261 if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
e467cde2
RR
262 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
263
3ef53609
CB
264 /* If disk is read-only in the host, the guest should obey */
265 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
266 set_disk_ro(vblk->disk, 1);
267
a586d4f6 268 /* Host must always specify the capacity. */
72e61eb4
RR
269 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
270 &cap, sizeof(cap));
e467cde2
RR
271
272 /* If capacity is too big, truncate with warning. */
273 if ((sector_t)cap != cap) {
274 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
275 (unsigned long long)cap);
276 cap = (sector_t)-1;
277 }
278 set_capacity(vblk->disk, cap);
279
a586d4f6
RR
280 /* Host can optionally specify maximum segment size and number of
281 * segments. */
282 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
283 offsetof(struct virtio_blk_config, size_max),
284 &v);
e467cde2
RR
285 if (!err)
286 blk_queue_max_segment_size(vblk->disk->queue, v);
e467cde2 287
a586d4f6
RR
288 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
289 offsetof(struct virtio_blk_config, seg_max),
290 &v);
e467cde2
RR
291 if (!err)
292 blk_queue_max_hw_segments(vblk->disk->queue, v);
e467cde2 293
066f4d82
CB
294 /* Host can optionally specify the block size of the device */
295 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
296 offsetof(struct virtio_blk_config, blk_size),
297 &blk_size);
298 if (!err)
299 blk_queue_hardsect_size(vblk->disk->queue, blk_size);
300
e467cde2
RR
301 add_disk(vblk->disk);
302 return 0;
303
304out_put_disk:
305 put_disk(vblk->disk);
e467cde2
RR
306out_mempool:
307 mempool_destroy(vblk->pool);
308out_free_vq:
309 vdev->config->del_vq(vblk->vq);
310out_free_vblk:
311 kfree(vblk);
312out:
313 return err;
314}
315
316static void virtblk_remove(struct virtio_device *vdev)
317{
318 struct virtio_blk *vblk = vdev->priv;
e467cde2 319
6e5aa7ef 320 /* Nothing should be pending. */
e467cde2 321 BUG_ON(!list_empty(&vblk->reqs));
6e5aa7ef
RR
322
323 /* Stop all the virtqueues. */
324 vdev->config->reset(vdev);
325
ac9d463a 326 del_gendisk(vblk->disk);
e467cde2
RR
327 blk_cleanup_queue(vblk->disk->queue);
328 put_disk(vblk->disk);
e467cde2 329 mempool_destroy(vblk->pool);
74b2553f 330 vdev->config->del_vq(vblk->vq);
e467cde2
RR
331 kfree(vblk);
332}
333
334static struct virtio_device_id id_table[] = {
335 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
336 { 0 },
337};
338
c45a6816
RR
339static unsigned int features[] = {
340 VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
066f4d82 341 VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
c45a6816
RR
342};
343
e467cde2 344static struct virtio_driver virtio_blk = {
c45a6816
RR
345 .feature_table = features,
346 .feature_table_size = ARRAY_SIZE(features),
e467cde2
RR
347 .driver.name = KBUILD_MODNAME,
348 .driver.owner = THIS_MODULE,
349 .id_table = id_table,
350 .probe = virtblk_probe,
351 .remove = __devexit_p(virtblk_remove),
352};
353
354static int __init init(void)
355{
4f3bf19c
CB
356 major = register_blkdev(0, "virtblk");
357 if (major < 0)
358 return major;
e467cde2
RR
359 return register_virtio_driver(&virtio_blk);
360}
361
362static void __exit fini(void)
363{
4f3bf19c 364 unregister_blkdev(major, "virtblk");
e467cde2
RR
365 unregister_virtio_driver(&virtio_blk);
366}
367module_init(init);
368module_exit(fini);
369
370MODULE_DEVICE_TABLE(virtio, id_table);
371MODULE_DESCRIPTION("Virtio block driver");
372MODULE_LICENSE("GPL");