]>
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 | ||
9 | #define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS) | |
4f3bf19c | 10 | #define PART_BITS 4 |
e467cde2 RR |
11 | |
12 | static unsigned char virtblk_index = 'a'; | |
4f3bf19c CB |
13 | static int major, minor; |
14 | ||
e467cde2 RR |
15 | struct virtio_blk |
16 | { | |
17 | spinlock_t lock; | |
18 | ||
19 | struct virtio_device *vdev; | |
20 | struct virtqueue *vq; | |
21 | ||
22 | /* The disk structure for the kernel. */ | |
23 | struct gendisk *disk; | |
24 | ||
25 | /* Request tracking. */ | |
26 | struct list_head reqs; | |
27 | ||
28 | mempool_t *pool; | |
29 | ||
30 | /* Scatterlist: can be too big for stack. */ | |
3d1266c7 | 31 | struct scatterlist sg[VIRTIO_MAX_SG]; |
e467cde2 RR |
32 | }; |
33 | ||
34 | struct virtblk_req | |
35 | { | |
36 | struct list_head list; | |
37 | struct request *req; | |
38 | struct virtio_blk_outhdr out_hdr; | |
39 | struct virtio_blk_inhdr in_hdr; | |
40 | }; | |
41 | ||
18445c4d | 42 | static void blk_done(struct virtqueue *vq) |
e467cde2 RR |
43 | { |
44 | struct virtio_blk *vblk = vq->vdev->priv; | |
45 | struct virtblk_req *vbr; | |
46 | unsigned int len; | |
47 | unsigned long flags; | |
48 | ||
49 | spin_lock_irqsave(&vblk->lock, flags); | |
50 | while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) { | |
51 | int uptodate; | |
52 | switch (vbr->in_hdr.status) { | |
53 | case VIRTIO_BLK_S_OK: | |
54 | uptodate = 1; | |
55 | break; | |
56 | case VIRTIO_BLK_S_UNSUPP: | |
57 | uptodate = -ENOTTY; | |
58 | break; | |
59 | default: | |
60 | uptodate = 0; | |
61 | break; | |
62 | } | |
63 | ||
64 | end_dequeued_request(vbr->req, uptodate); | |
65 | list_del(&vbr->list); | |
66 | mempool_free(vbr, vblk->pool); | |
67 | } | |
68 | /* In case queue is stopped waiting for more buffers. */ | |
69 | blk_start_queue(vblk->disk->queue); | |
70 | spin_unlock_irqrestore(&vblk->lock, flags); | |
e467cde2 RR |
71 | } |
72 | ||
73 | static bool do_req(struct request_queue *q, struct virtio_blk *vblk, | |
74 | struct request *req) | |
75 | { | |
76 | unsigned long num, out, in; | |
77 | struct virtblk_req *vbr; | |
78 | ||
79 | vbr = mempool_alloc(vblk->pool, GFP_ATOMIC); | |
80 | if (!vbr) | |
81 | /* When another request finishes we'll try again. */ | |
82 | return false; | |
83 | ||
84 | vbr->req = req; | |
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; | |
93 | } else { | |
94 | /* We don't put anything else in the queue. */ | |
95 | BUG(); | |
96 | } | |
97 | ||
98 | if (blk_barrier_rq(vbr->req)) | |
99 | vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER; | |
100 | ||
3d1266c7 JA |
101 | /* This init could be done at vblk creation time */ |
102 | sg_init_table(vblk->sg, VIRTIO_MAX_SG); | |
e467cde2 RR |
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)); | |
106 | ||
107 | if (rq_data_dir(vbr->req) == WRITE) { | |
108 | vbr->out_hdr.type |= VIRTIO_BLK_T_OUT; | |
109 | out = 1 + num; | |
110 | in = 1; | |
111 | } else { | |
112 | vbr->out_hdr.type |= VIRTIO_BLK_T_IN; | |
113 | out = 1; | |
114 | in = 1 + num; | |
115 | } | |
116 | ||
117 | if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) { | |
118 | mempool_free(vbr, vblk->pool); | |
119 | return false; | |
120 | } | |
121 | ||
122 | list_add_tail(&vbr->list, &vblk->reqs); | |
123 | return true; | |
124 | } | |
125 | ||
126 | static void do_virtblk_request(struct request_queue *q) | |
127 | { | |
128 | struct virtio_blk *vblk = NULL; | |
129 | struct request *req; | |
130 | unsigned int issued = 0; | |
131 | ||
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)); | |
135 | ||
136 | /* If this request fails, stop queue and wait for something to | |
137 | finish to restart it. */ | |
138 | if (!do_req(q, vblk, req)) { | |
139 | blk_stop_queue(q); | |
140 | break; | |
141 | } | |
142 | blkdev_dequeue_request(req); | |
143 | issued++; | |
144 | } | |
145 | ||
146 | if (issued) | |
147 | vblk->vq->vq_ops->kick(vblk->vq); | |
148 | } | |
149 | ||
150 | static int virtblk_ioctl(struct inode *inode, struct file *filp, | |
151 | unsigned cmd, unsigned long data) | |
152 | { | |
153 | return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue, | |
154 | inode->i_bdev->bd_disk, cmd, | |
155 | (void __user *)data); | |
156 | } | |
157 | ||
135da0b0 CB |
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) | |
160 | { | |
161 | /* some standard values, similar to sd */ | |
162 | geo->heads = 1 << 6; | |
163 | geo->sectors = 1 << 5; | |
164 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; | |
165 | return 0; | |
166 | } | |
167 | ||
e467cde2 | 168 | static struct block_device_operations virtblk_fops = { |
135da0b0 CB |
169 | .ioctl = virtblk_ioctl, |
170 | .owner = THIS_MODULE, | |
171 | .getgeo = virtblk_getgeo, | |
e467cde2 RR |
172 | }; |
173 | ||
174 | static int virtblk_probe(struct virtio_device *vdev) | |
175 | { | |
176 | struct virtio_blk *vblk; | |
4f3bf19c | 177 | int err; |
e467cde2 RR |
178 | u64 cap; |
179 | u32 v; | |
180 | ||
4f3bf19c CB |
181 | if (minor >= 1 << MINORBITS) |
182 | return -ENOSPC; | |
183 | ||
e467cde2 RR |
184 | vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL); |
185 | if (!vblk) { | |
186 | err = -ENOMEM; | |
187 | goto out; | |
188 | } | |
189 | ||
190 | INIT_LIST_HEAD(&vblk->reqs); | |
191 | spin_lock_init(&vblk->lock); | |
192 | vblk->vdev = vdev; | |
193 | ||
194 | /* We expect one virtqueue, for output. */ | |
a586d4f6 | 195 | vblk->vq = vdev->config->find_vq(vdev, 0, blk_done); |
e467cde2 RR |
196 | if (IS_ERR(vblk->vq)) { |
197 | err = PTR_ERR(vblk->vq); | |
198 | goto out_free_vblk; | |
199 | } | |
200 | ||
201 | vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req)); | |
202 | if (!vblk->pool) { | |
203 | err = -ENOMEM; | |
204 | goto out_free_vq; | |
205 | } | |
206 | ||
e467cde2 | 207 | /* FIXME: How many partitions? How long is a piece of string? */ |
4f3bf19c | 208 | vblk->disk = alloc_disk(1 << PART_BITS); |
e467cde2 RR |
209 | if (!vblk->disk) { |
210 | err = -ENOMEM; | |
4f3bf19c | 211 | goto out_mempool; |
e467cde2 RR |
212 | } |
213 | ||
214 | vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock); | |
215 | if (!vblk->disk->queue) { | |
216 | err = -ENOMEM; | |
217 | goto out_put_disk; | |
218 | } | |
219 | ||
220 | sprintf(vblk->disk->disk_name, "vd%c", virtblk_index++); | |
221 | vblk->disk->major = major; | |
4f3bf19c | 222 | vblk->disk->first_minor = minor; |
e467cde2 RR |
223 | vblk->disk->private_data = vblk; |
224 | vblk->disk->fops = &virtblk_fops; | |
225 | ||
4f3bf19c CB |
226 | minor += 1 << PART_BITS; |
227 | ||
e467cde2 | 228 | /* If barriers are supported, tell block layer that queue is ordered */ |
a586d4f6 | 229 | if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER)) |
e467cde2 RR |
230 | blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL); |
231 | ||
a586d4f6 RR |
232 | /* Host must always specify the capacity. */ |
233 | __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity), | |
234 | &cap); | |
e467cde2 RR |
235 | |
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); | |
240 | cap = (sector_t)-1; | |
241 | } | |
242 | set_capacity(vblk->disk, cap); | |
243 | ||
a586d4f6 RR |
244 | /* Host can optionally specify maximum segment size and number of |
245 | * segments. */ | |
246 | err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX, | |
247 | offsetof(struct virtio_blk_config, size_max), | |
248 | &v); | |
e467cde2 RR |
249 | if (!err) |
250 | blk_queue_max_segment_size(vblk->disk->queue, v); | |
e467cde2 | 251 | |
a586d4f6 RR |
252 | err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX, |
253 | offsetof(struct virtio_blk_config, seg_max), | |
254 | &v); | |
e467cde2 RR |
255 | if (!err) |
256 | blk_queue_max_hw_segments(vblk->disk->queue, v); | |
e467cde2 RR |
257 | |
258 | add_disk(vblk->disk); | |
259 | return 0; | |
260 | ||
261 | out_put_disk: | |
262 | put_disk(vblk->disk); | |
e467cde2 RR |
263 | out_mempool: |
264 | mempool_destroy(vblk->pool); | |
265 | out_free_vq: | |
266 | vdev->config->del_vq(vblk->vq); | |
267 | out_free_vblk: | |
268 | kfree(vblk); | |
269 | out: | |
270 | return err; | |
271 | } | |
272 | ||
273 | static void virtblk_remove(struct virtio_device *vdev) | |
274 | { | |
275 | struct virtio_blk *vblk = vdev->priv; | |
276 | int major = vblk->disk->major; | |
277 | ||
6e5aa7ef | 278 | /* Nothing should be pending. */ |
e467cde2 | 279 | BUG_ON(!list_empty(&vblk->reqs)); |
6e5aa7ef RR |
280 | |
281 | /* Stop all the virtqueues. */ | |
282 | vdev->config->reset(vdev); | |
283 | ||
e467cde2 RR |
284 | blk_cleanup_queue(vblk->disk->queue); |
285 | put_disk(vblk->disk); | |
286 | unregister_blkdev(major, "virtblk"); | |
287 | mempool_destroy(vblk->pool); | |
74b2553f | 288 | vdev->config->del_vq(vblk->vq); |
e467cde2 RR |
289 | kfree(vblk); |
290 | } | |
291 | ||
292 | static struct virtio_device_id id_table[] = { | |
293 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID }, | |
294 | { 0 }, | |
295 | }; | |
296 | ||
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), | |
303 | }; | |
304 | ||
305 | static int __init init(void) | |
306 | { | |
4f3bf19c CB |
307 | major = register_blkdev(0, "virtblk"); |
308 | if (major < 0) | |
309 | return major; | |
e467cde2 RR |
310 | return register_virtio_driver(&virtio_blk); |
311 | } | |
312 | ||
313 | static void __exit fini(void) | |
314 | { | |
4f3bf19c | 315 | unregister_blkdev(major, "virtblk"); |
e467cde2 RR |
316 | unregister_virtio_driver(&virtio_blk); |
317 | } | |
318 | module_init(init); | |
319 | module_exit(fini); | |
320 | ||
321 | MODULE_DEVICE_TABLE(virtio, id_table); | |
322 | MODULE_DESCRIPTION("Virtio block driver"); | |
323 | MODULE_LICENSE("GPL"); |