Commit | Line | Data |
---|---|---|
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 | ||
4f3bf19c | 9 | #define PART_BITS 4 |
e467cde2 | 10 | |
d50ed907 | 11 | static int major, index; |
4f3bf19c | 12 | |
e467cde2 RR |
13 | struct virtio_blk |
14 | { | |
15 | spinlock_t lock; | |
16 | ||
17 | struct virtio_device *vdev; | |
18 | struct virtqueue *vq; | |
19 | ||
20 | /* The disk structure for the kernel. */ | |
21 | struct gendisk *disk; | |
22 | ||
23 | /* Request tracking. */ | |
24 | struct list_head reqs; | |
25 | ||
26 | mempool_t *pool; | |
27 | ||
0864b79a RR |
28 | /* What host tells us, plus 2 for header & tailer. */ |
29 | unsigned int sg_elems; | |
30 | ||
e467cde2 | 31 | /* Scatterlist: can be too big for stack. */ |
0864b79a | 32 | struct scatterlist sg[/*sg_elems*/]; |
e467cde2 RR |
33 | }; |
34 | ||
35 | struct virtblk_req | |
36 | { | |
37 | struct list_head list; | |
38 | struct request *req; | |
39 | struct virtio_blk_outhdr out_hdr; | |
cb38fa23 | 40 | u8 status; |
e467cde2 RR |
41 | }; |
42 | ||
18445c4d | 43 | static void blk_done(struct virtqueue *vq) |
e467cde2 RR |
44 | { |
45 | struct virtio_blk *vblk = vq->vdev->priv; | |
46 | struct virtblk_req *vbr; | |
47 | unsigned int len; | |
48 | unsigned long flags; | |
49 | ||
50 | spin_lock_irqsave(&vblk->lock, flags); | |
51 | while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) { | |
8316982a | 52 | int error; |
cb38fa23 | 53 | switch (vbr->status) { |
e467cde2 | 54 | case VIRTIO_BLK_S_OK: |
8316982a | 55 | error = 0; |
e467cde2 RR |
56 | break; |
57 | case VIRTIO_BLK_S_UNSUPP: | |
8316982a | 58 | error = -ENOTTY; |
e467cde2 RR |
59 | break; |
60 | default: | |
8316982a | 61 | error = -EIO; |
e467cde2 RR |
62 | break; |
63 | } | |
64 | ||
40cbbb78 | 65 | __blk_end_request_all(vbr->req, error); |
e467cde2 RR |
66 | list_del(&vbr->list); |
67 | mempool_free(vbr, vblk->pool); | |
68 | } | |
69 | /* In case queue is stopped waiting for more buffers. */ | |
70 | blk_start_queue(vblk->disk->queue); | |
71 | spin_unlock_irqrestore(&vblk->lock, flags); | |
e467cde2 RR |
72 | } |
73 | ||
74 | static bool do_req(struct request_queue *q, struct virtio_blk *vblk, | |
75 | struct request *req) | |
76 | { | |
77 | unsigned long num, out, in; | |
78 | struct virtblk_req *vbr; | |
79 | ||
80 | vbr = mempool_alloc(vblk->pool, GFP_ATOMIC); | |
81 | if (!vbr) | |
82 | /* When another request finishes we'll try again. */ | |
83 | return false; | |
84 | ||
85 | vbr->req = req; | |
86 | if (blk_fs_request(vbr->req)) { | |
87 | vbr->out_hdr.type = 0; | |
83096ebf | 88 | vbr->out_hdr.sector = blk_rq_pos(vbr->req); |
766ca442 | 89 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
e467cde2 RR |
90 | } else if (blk_pc_request(vbr->req)) { |
91 | vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD; | |
92 | vbr->out_hdr.sector = 0; | |
766ca442 | 93 | vbr->out_hdr.ioprio = req_get_ioprio(vbr->req); |
e467cde2 RR |
94 | } else { |
95 | /* We don't put anything else in the queue. */ | |
96 | BUG(); | |
97 | } | |
98 | ||
99 | if (blk_barrier_rq(vbr->req)) | |
100 | vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER; | |
101 | ||
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 | ||
125 | static 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 | ||
9934c8c0 | 131 | while ((req = blk_peek_request(q)) != NULL) { |
e467cde2 | 132 | vblk = req->rq_disk->private_data; |
0864b79a | 133 | BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems); |
e467cde2 RR |
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 | } | |
9934c8c0 | 141 | blk_start_request(req); |
e467cde2 RR |
142 | issued++; |
143 | } | |
144 | ||
145 | if (issued) | |
146 | vblk->vq->vq_ops->kick(vblk->vq); | |
147 | } | |
148 | ||
4e109852 | 149 | static int virtblk_ioctl(struct block_device *bdev, fmode_t mode, |
e467cde2 RR |
150 | unsigned cmd, unsigned long data) |
151 | { | |
4e109852 AV |
152 | return scsi_cmd_ioctl(bdev->bd_disk->queue, |
153 | bdev->bd_disk, mode, cmd, | |
e467cde2 RR |
154 | (void __user *)data); |
155 | } | |
156 | ||
135da0b0 CB |
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) | |
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 | 182 | static struct block_device_operations virtblk_fops = { |
4e109852 | 183 | .locked_ioctl = virtblk_ioctl, |
135da0b0 CB |
184 | .owner = THIS_MODULE, |
185 | .getgeo = virtblk_getgeo, | |
e467cde2 RR |
186 | }; |
187 | ||
d50ed907 CB |
188 | static int index_to_minor(int index) |
189 | { | |
190 | return index << PART_BITS; | |
191 | } | |
192 | ||
e467cde2 RR |
193 | static 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; | |
0864b79a | 199 | u32 blk_size, sg_elems; |
e467cde2 | 200 | |
d50ed907 | 201 | if (index_to_minor(index) >= 1 << MINORBITS) |
4f3bf19c CB |
202 | return -ENOSPC; |
203 | ||
0864b79a RR |
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), | |
207 | &sg_elems); | |
208 | if (err) | |
209 | sg_elems = 1; | |
210 | ||
211 | /* We need an extra sg elements at head and tail. */ | |
212 | sg_elems += 2; | |
213 | vdev->priv = vblk = kmalloc(sizeof(*vblk) + | |
214 | sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL); | |
e467cde2 RR |
215 | if (!vblk) { |
216 | err = -ENOMEM; | |
217 | goto out; | |
218 | } | |
219 | ||
220 | INIT_LIST_HEAD(&vblk->reqs); | |
221 | spin_lock_init(&vblk->lock); | |
222 | vblk->vdev = vdev; | |
0864b79a RR |
223 | vblk->sg_elems = sg_elems; |
224 | sg_init_table(vblk->sg, vblk->sg_elems); | |
e467cde2 RR |
225 | |
226 | /* We expect one virtqueue, for output. */ | |
a586d4f6 | 227 | vblk->vq = vdev->config->find_vq(vdev, 0, blk_done); |
e467cde2 RR |
228 | if (IS_ERR(vblk->vq)) { |
229 | err = PTR_ERR(vblk->vq); | |
230 | goto out_free_vblk; | |
231 | } | |
232 | ||
233 | vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req)); | |
234 | if (!vblk->pool) { | |
235 | err = -ENOMEM; | |
236 | goto out_free_vq; | |
237 | } | |
238 | ||
e467cde2 | 239 | /* FIXME: How many partitions? How long is a piece of string? */ |
4f3bf19c | 240 | vblk->disk = alloc_disk(1 << PART_BITS); |
e467cde2 RR |
241 | if (!vblk->disk) { |
242 | err = -ENOMEM; | |
4f3bf19c | 243 | goto out_mempool; |
e467cde2 RR |
244 | } |
245 | ||
246 | vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock); | |
247 | if (!vblk->disk->queue) { | |
248 | err = -ENOMEM; | |
249 | goto out_put_disk; | |
250 | } | |
251 | ||
7d116b62 FLVC |
252 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue); |
253 | ||
d50ed907 CB |
254 | if (index < 26) { |
255 | sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26); | |
256 | } else if (index < (26 + 1) * 26) { | |
257 | sprintf(vblk->disk->disk_name, "vd%c%c", | |
258 | 'a' + index / 26 - 1, 'a' + index % 26); | |
259 | } else { | |
260 | const unsigned int m1 = (index / 26 - 1) / 26 - 1; | |
261 | const unsigned int m2 = (index / 26 - 1) % 26; | |
262 | const unsigned int m3 = index % 26; | |
263 | sprintf(vblk->disk->disk_name, "vd%c%c%c", | |
264 | 'a' + m1, 'a' + m2, 'a' + m3); | |
265 | } | |
266 | ||
e467cde2 | 267 | vblk->disk->major = major; |
d50ed907 | 268 | vblk->disk->first_minor = index_to_minor(index); |
e467cde2 RR |
269 | vblk->disk->private_data = vblk; |
270 | vblk->disk->fops = &virtblk_fops; | |
c4839346 | 271 | vblk->disk->driverfs_dev = &vdev->dev; |
d50ed907 | 272 | index++; |
4f3bf19c | 273 | |
e467cde2 | 274 | /* If barriers are supported, tell block layer that queue is ordered */ |
c45a6816 | 275 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER)) |
e467cde2 RR |
276 | blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL); |
277 | ||
3ef53609 CB |
278 | /* If disk is read-only in the host, the guest should obey */ |
279 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO)) | |
280 | set_disk_ro(vblk->disk, 1); | |
281 | ||
a586d4f6 | 282 | /* Host must always specify the capacity. */ |
72e61eb4 RR |
283 | vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity), |
284 | &cap, sizeof(cap)); | |
e467cde2 RR |
285 | |
286 | /* If capacity is too big, truncate with warning. */ | |
287 | if ((sector_t)cap != cap) { | |
288 | dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n", | |
289 | (unsigned long long)cap); | |
290 | cap = (sector_t)-1; | |
291 | } | |
292 | set_capacity(vblk->disk, cap); | |
293 | ||
0864b79a RR |
294 | /* We can handle whatever the host told us to handle. */ |
295 | blk_queue_max_phys_segments(vblk->disk->queue, vblk->sg_elems-2); | |
296 | blk_queue_max_hw_segments(vblk->disk->queue, vblk->sg_elems-2); | |
297 | ||
4b7f7e20 RR |
298 | /* No real sector limit. */ |
299 | blk_queue_max_sectors(vblk->disk->queue, -1U); | |
300 | ||
a586d4f6 RR |
301 | /* Host can optionally specify maximum segment size and number of |
302 | * segments. */ | |
303 | err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX, | |
304 | offsetof(struct virtio_blk_config, size_max), | |
305 | &v); | |
e467cde2 RR |
306 | if (!err) |
307 | blk_queue_max_segment_size(vblk->disk->queue, v); | |
4b7f7e20 | 308 | else |
b194aee9 | 309 | blk_queue_max_segment_size(vblk->disk->queue, -1U); |
e467cde2 | 310 | |
066f4d82 CB |
311 | /* Host can optionally specify the block size of the device */ |
312 | err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE, | |
313 | offsetof(struct virtio_blk_config, blk_size), | |
314 | &blk_size); | |
315 | if (!err) | |
316 | blk_queue_hardsect_size(vblk->disk->queue, blk_size); | |
317 | ||
e467cde2 RR |
318 | add_disk(vblk->disk); |
319 | return 0; | |
320 | ||
321 | out_put_disk: | |
322 | put_disk(vblk->disk); | |
e467cde2 RR |
323 | out_mempool: |
324 | mempool_destroy(vblk->pool); | |
325 | out_free_vq: | |
326 | vdev->config->del_vq(vblk->vq); | |
327 | out_free_vblk: | |
328 | kfree(vblk); | |
329 | out: | |
330 | return err; | |
331 | } | |
332 | ||
333 | static void virtblk_remove(struct virtio_device *vdev) | |
334 | { | |
335 | struct virtio_blk *vblk = vdev->priv; | |
e467cde2 | 336 | |
6e5aa7ef | 337 | /* Nothing should be pending. */ |
e467cde2 | 338 | BUG_ON(!list_empty(&vblk->reqs)); |
6e5aa7ef RR |
339 | |
340 | /* Stop all the virtqueues. */ | |
341 | vdev->config->reset(vdev); | |
342 | ||
ac9d463a | 343 | del_gendisk(vblk->disk); |
e467cde2 RR |
344 | blk_cleanup_queue(vblk->disk->queue); |
345 | put_disk(vblk->disk); | |
e467cde2 | 346 | mempool_destroy(vblk->pool); |
74b2553f | 347 | vdev->config->del_vq(vblk->vq); |
e467cde2 RR |
348 | kfree(vblk); |
349 | } | |
350 | ||
351 | static struct virtio_device_id id_table[] = { | |
352 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, | |
353 | { 0 }, | |
354 | }; | |
355 | ||
c45a6816 RR |
356 | static unsigned int features[] = { |
357 | VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, | |
066f4d82 | 358 | VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, |
c45a6816 RR |
359 | }; |
360 | ||
e467cde2 | 361 | static struct virtio_driver virtio_blk = { |
c45a6816 RR |
362 | .feature_table = features, |
363 | .feature_table_size = ARRAY_SIZE(features), | |
e467cde2 RR |
364 | .driver.name = KBUILD_MODNAME, |
365 | .driver.owner = THIS_MODULE, | |
366 | .id_table = id_table, | |
367 | .probe = virtblk_probe, | |
368 | .remove = __devexit_p(virtblk_remove), | |
369 | }; | |
370 | ||
371 | static int __init init(void) | |
372 | { | |
4f3bf19c CB |
373 | major = register_blkdev(0, "virtblk"); |
374 | if (major < 0) | |
375 | return major; | |
e467cde2 RR |
376 | return register_virtio_driver(&virtio_blk); |
377 | } | |
378 | ||
379 | static void __exit fini(void) | |
380 | { | |
4f3bf19c | 381 | unregister_blkdev(major, "virtblk"); |
e467cde2 RR |
382 | unregister_virtio_driver(&virtio_blk); |
383 | } | |
384 | module_init(init); | |
385 | module_exit(fini); | |
386 | ||
387 | MODULE_DEVICE_TABLE(virtio, id_table); | |
388 | MODULE_DESCRIPTION("Virtio block driver"); | |
389 | MODULE_LICENSE("GPL"); |