blob: aa7094f230179d608fd94a6fbfbcb655325d0219 [file] [log] [blame]
Rusty Russelle467cde2007-10-22 11:03:38 +10001//#define DEBUG
2#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10004#include <linux/blkdev.h>
5#include <linux/hdreg.h>
Paul Gortmaker0c8d44f2011-07-01 15:56:05 -04006#include <linux/module.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10007#include <linux/virtio.h>
8#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +02009#include <linux/scatterlist.h>
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010010#include <linux/string_helpers.h>
Liu Yuan6917f832011-04-24 02:49:26 +080011#include <scsi/scsi_cmnd.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020012
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010013#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100014
Christian Borntraegerd50ed902008-02-01 09:05:00 +010015static int major, index;
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010016struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010017
Rusty Russelle467cde2007-10-22 11:03:38 +100018struct virtio_blk
19{
20 spinlock_t lock;
21
22 struct virtio_device *vdev;
23 struct virtqueue *vq;
24
25 /* The disk structure for the kernel. */
26 struct gendisk *disk;
27
28 /* Request tracking. */
29 struct list_head reqs;
30
31 mempool_t *pool;
32
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010033 /* Process context for config space updates */
34 struct work_struct config_work;
35
Rusty Russell0864b792008-12-30 09:26:05 -060036 /* What host tells us, plus 2 for header & tailer. */
37 unsigned int sg_elems;
38
Rusty Russelle467cde2007-10-22 11:03:38 +100039 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060040 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100041};
42
43struct virtblk_req
44{
45 struct list_head list;
46 struct request *req;
47 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020048 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050049 u8 status;
Rusty Russelle467cde2007-10-22 11:03:38 +100050};
51
Rusty Russell18445c42008-02-04 23:49:57 -050052static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100053{
54 struct virtio_blk *vblk = vq->vdev->priv;
55 struct virtblk_req *vbr;
56 unsigned int len;
57 unsigned long flags;
58
59 spin_lock_irqsave(&vblk->lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +030060 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Kiyoshi Ueda83169822008-10-01 10:11:20 -040061 int error;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020062
Rusty Russellcb38fa22008-05-02 21:50:45 -050063 switch (vbr->status) {
Rusty Russelle467cde2007-10-22 11:03:38 +100064 case VIRTIO_BLK_S_OK:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040065 error = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100066 break;
67 case VIRTIO_BLK_S_UNSUPP:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040068 error = -ENOTTY;
Rusty Russelle467cde2007-10-22 11:03:38 +100069 break;
70 default:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040071 error = -EIO;
Rusty Russelle467cde2007-10-22 11:03:38 +100072 break;
73 }
74
Christoph Hellwig33659eb2010-08-07 18:17:56 +020075 switch (vbr->req->cmd_type) {
76 case REQ_TYPE_BLOCK_PC:
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020077 vbr->req->resid_len = vbr->in_hdr.residual;
78 vbr->req->sense_len = vbr->in_hdr.sense_len;
79 vbr->req->errors = vbr->in_hdr.errors;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020080 break;
81 case REQ_TYPE_SPECIAL:
john cooper4cb2ea22010-03-25 01:33:33 -040082 vbr->req->errors = (error != 0);
Christoph Hellwig33659eb2010-08-07 18:17:56 +020083 break;
Jens Axboe15fa6e82010-06-18 12:10:18 +020084 default:
85 break;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020086 }
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020087
Tejun Heo40cbbb72009-04-23 11:05:19 +090088 __blk_end_request_all(vbr->req, error);
Rusty Russelle467cde2007-10-22 11:03:38 +100089 list_del(&vbr->list);
90 mempool_free(vbr, vblk->pool);
91 }
92 /* In case queue is stopped waiting for more buffers. */
93 blk_start_queue(vblk->disk->queue);
94 spin_unlock_irqrestore(&vblk->lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +100095}
96
97static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
98 struct request *req)
99{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200100 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000101 struct virtblk_req *vbr;
102
103 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
104 if (!vbr)
105 /* When another request finishes we'll try again. */
106 return false;
107
108 vbr->req = req;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900109
110 if (req->cmd_flags & REQ_FLUSH) {
111 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000112 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200113 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900114 } else {
115 switch (req->cmd_type) {
116 case REQ_TYPE_FS:
117 vbr->out_hdr.type = 0;
118 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
119 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
120 break;
121 case REQ_TYPE_BLOCK_PC:
122 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200123 vbr->out_hdr.sector = 0;
124 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
125 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900126 case REQ_TYPE_SPECIAL:
127 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
128 vbr->out_hdr.sector = 0;
129 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
130 break;
131 default:
132 /* We don't put anything else in the queue. */
133 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200134 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000135 }
136
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200137 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000138
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200139 /*
140 * If this is a packet command we need a couple of additional headers.
141 * Behind the normal outhdr we put a segment with the scsi command
142 * block, and before the normal inhdr we put the sense data and the
143 * inhdr with additional status information before the normal inhdr.
144 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200145 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200146 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
147
148 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
149
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200150 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800151 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200152 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
153 sizeof(vbr->in_hdr));
154 }
155
156 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
157 sizeof(vbr->status));
158
159 if (num) {
160 if (rq_data_dir(vbr->req) == WRITE) {
161 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
162 out += num;
163 } else {
164 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
165 in += num;
166 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000167 }
168
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300169 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000170 mempool_free(vbr, vblk->pool);
171 return false;
172 }
173
174 list_add_tail(&vbr->list, &vblk->reqs);
175 return true;
176}
177
178static void do_virtblk_request(struct request_queue *q)
179{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200180 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000181 struct request *req;
182 unsigned int issued = 0;
183
Tejun Heo9934c8c2009-05-08 11:54:16 +0900184 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600185 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000186
187 /* If this request fails, stop queue and wait for something to
188 finish to restart it. */
189 if (!do_req(q, vblk, req)) {
190 blk_stop_queue(q);
191 break;
192 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900193 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000194 issued++;
195 }
196
197 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300198 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000199}
200
john cooper4cb2ea22010-03-25 01:33:33 -0400201/* return id (s/n) string for *disk to *id_str
202 */
203static int virtblk_get_id(struct gendisk *disk, char *id_str)
204{
205 struct virtio_blk *vblk = disk->private_data;
206 struct request *req;
207 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030208 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400209
210 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
211 GFP_KERNEL);
212 if (IS_ERR(bio))
213 return PTR_ERR(bio);
214
215 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
216 if (IS_ERR(req)) {
217 bio_put(bio);
218 return PTR_ERR(req);
219 }
220
221 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030222 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
223 blk_put_request(req);
224
225 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400226}
227
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200228static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
229 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000230{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200231 struct gendisk *disk = bdev->bd_disk;
232 struct virtio_blk *vblk = disk->private_data;
233
234 /*
235 * Only allow the generic SCSI ioctls if the host can support it.
236 */
237 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200238 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200239
Rusty Russell3225bea2009-10-22 16:39:28 -0600240 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd,
241 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000242}
243
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100244/* We provide getgeo only to please some old bootloader/partitioning tools */
245static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
246{
Ryan Harper48e40432008-04-16 13:56:37 -0500247 struct virtio_blk *vblk = bd->bd_disk->private_data;
248 struct virtio_blk_geometry vgeo;
249 int err;
250
251 /* see if the host passed in geometry config */
252 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
253 offsetof(struct virtio_blk_config, geometry),
254 &vgeo);
255
256 if (!err) {
257 geo->heads = vgeo.heads;
258 geo->sectors = vgeo.sectors;
259 geo->cylinders = vgeo.cylinders;
260 } else {
261 /* some standard values, similar to sd */
262 geo->heads = 1 << 6;
263 geo->sectors = 1 << 5;
264 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
265 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100266 return 0;
267}
268
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700269static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200270 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100271 .owner = THIS_MODULE,
272 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000273};
274
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100275static int index_to_minor(int index)
276{
277 return index << PART_BITS;
278}
279
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500280static ssize_t virtblk_serial_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
283 struct gendisk *disk = dev_to_disk(dev);
284 int err;
285
286 /* sysfs gives us a PAGE_SIZE buffer */
287 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
288
289 buf[VIRTIO_BLK_ID_BYTES] = '\0';
290 err = virtblk_get_id(disk, buf);
291 if (!err)
292 return strlen(buf);
293
294 if (err == -EIO) /* Unsupported? Make it empty. */
295 return 0;
296
297 return err;
298}
299DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
300
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100301static void virtblk_config_changed_work(struct work_struct *work)
302{
303 struct virtio_blk *vblk =
304 container_of(work, struct virtio_blk, config_work);
305 struct virtio_device *vdev = vblk->vdev;
306 struct request_queue *q = vblk->disk->queue;
307 char cap_str_2[10], cap_str_10[10];
308 u64 capacity, size;
309
310 /* Host must always specify the capacity. */
311 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
312 &capacity, sizeof(capacity));
313
314 /* If capacity is too big, truncate with warning. */
315 if ((sector_t)capacity != capacity) {
316 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
317 (unsigned long long)capacity);
318 capacity = (sector_t)-1;
319 }
320
321 size = capacity * queue_logical_block_size(q);
322 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
323 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
324
325 dev_notice(&vdev->dev,
326 "new size: %llu %d-byte logical blocks (%s/%s)\n",
327 (unsigned long long)capacity,
328 queue_logical_block_size(q),
329 cap_str_10, cap_str_2);
330
331 set_capacity(vblk->disk, capacity);
332}
333
334static void virtblk_config_changed(struct virtio_device *vdev)
335{
336 struct virtio_blk *vblk = vdev->priv;
337
338 queue_work(virtblk_wq, &vblk->config_work);
339}
340
Mike Frysinger98e94442009-05-18 03:39:09 -0400341static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000342{
343 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600344 struct request_queue *q;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100345 int err;
Rusty Russelle467cde2007-10-22 11:03:38 +1000346 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600347 u32 v, blk_size, sg_elems, opt_io_size;
348 u16 min_io_size;
349 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000350
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100351 if (index_to_minor(index) >= 1 << MINORBITS)
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100352 return -ENOSPC;
353
Rusty Russell0864b792008-12-30 09:26:05 -0600354 /* We need to know how many segments before we allocate. */
355 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
356 offsetof(struct virtio_blk_config, seg_max),
357 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200358
359 /* We need at least one SG element, whatever they say. */
360 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600361 sg_elems = 1;
362
363 /* We need an extra sg elements at head and tail. */
364 sg_elems += 2;
365 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
366 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000367 if (!vblk) {
368 err = -ENOMEM;
369 goto out;
370 }
371
372 INIT_LIST_HEAD(&vblk->reqs);
373 spin_lock_init(&vblk->lock);
374 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600375 vblk->sg_elems = sg_elems;
376 sg_init_table(vblk->sg, vblk->sg_elems);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100377 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000378
379 /* We expect one virtqueue, for output. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600380 vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests");
Rusty Russelle467cde2007-10-22 11:03:38 +1000381 if (IS_ERR(vblk->vq)) {
382 err = PTR_ERR(vblk->vq);
383 goto out_free_vblk;
384 }
385
386 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
387 if (!vblk->pool) {
388 err = -ENOMEM;
389 goto out_free_vq;
390 }
391
Rusty Russelle467cde2007-10-22 11:03:38 +1000392 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100393 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000394 if (!vblk->disk) {
395 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100396 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000397 }
398
Christoph Hellwig69740c82010-02-24 14:22:25 -0600399 q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
400 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000401 err = -ENOMEM;
402 goto out_put_disk;
403 }
404
Christoph Hellwig69740c82010-02-24 14:22:25 -0600405 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900406
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100407 if (index < 26) {
408 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
409 } else if (index < (26 + 1) * 26) {
410 sprintf(vblk->disk->disk_name, "vd%c%c",
411 'a' + index / 26 - 1, 'a' + index % 26);
412 } else {
413 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
414 const unsigned int m2 = (index / 26 - 1) % 26;
415 const unsigned int m3 = index % 26;
416 sprintf(vblk->disk->disk_name, "vd%c%c%c",
417 'a' + m1, 'a' + m2, 'a' + m3);
418 }
419
Rusty Russelle467cde2007-10-22 11:03:38 +1000420 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100421 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000422 vblk->disk->private_data = vblk;
423 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500424 vblk->disk->driverfs_dev = &vdev->dev;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100425 index++;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100426
Tejun Heo02c42b72010-09-03 11:56:18 +0200427 /* configure queue flush support */
Tejun Heo4913efe2010-09-03 11:56:16 +0200428 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
429 blk_queue_flush(q, REQ_FLUSH);
Rusty Russelle467cde2007-10-22 11:03:38 +1000430
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200431 /* If disk is read-only in the host, the guest should obey */
432 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
433 set_disk_ro(vblk->disk, 1);
434
Rusty Russella586d4f2008-02-04 23:49:56 -0500435 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500436 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
437 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000438
439 /* If capacity is too big, truncate with warning. */
440 if ((sector_t)cap != cap) {
441 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
442 (unsigned long long)cap);
443 cap = (sector_t)-1;
444 }
445 set_capacity(vblk->disk, cap);
446
Rusty Russell0864b792008-12-30 09:26:05 -0600447 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500448 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600449
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600450 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600451 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600452
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600453 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500454 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600455
Rusty Russella586d4f2008-02-04 23:49:56 -0500456 /* Host can optionally specify maximum segment size and number of
457 * segments. */
458 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
459 offsetof(struct virtio_blk_config, size_max),
460 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000461 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600462 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600463 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600464 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000465
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200466 /* Host can optionally specify the block size of the device */
467 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
468 offsetof(struct virtio_blk_config, blk_size),
469 &blk_size);
470 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600471 blk_queue_logical_block_size(q, blk_size);
472 else
473 blk_size = queue_logical_block_size(q);
474
475 /* Use topology information if available */
476 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
477 offsetof(struct virtio_blk_config, physical_block_exp),
478 &physical_block_exp);
479 if (!err && physical_block_exp)
480 blk_queue_physical_block_size(q,
481 blk_size * (1 << physical_block_exp));
482
483 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
484 offsetof(struct virtio_blk_config, alignment_offset),
485 &alignment_offset);
486 if (!err && alignment_offset)
487 blk_queue_alignment_offset(q, blk_size * alignment_offset);
488
489 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
490 offsetof(struct virtio_blk_config, min_io_size),
491 &min_io_size);
492 if (!err && min_io_size)
493 blk_queue_io_min(q, blk_size * min_io_size);
494
495 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
496 offsetof(struct virtio_blk_config, opt_io_size),
497 &opt_io_size);
498 if (!err && opt_io_size)
499 blk_queue_io_opt(q, blk_size * opt_io_size);
500
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200501
Rusty Russelle467cde2007-10-22 11:03:38 +1000502 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500503 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
504 if (err)
505 goto out_del_disk;
506
Rusty Russelle467cde2007-10-22 11:03:38 +1000507 return 0;
508
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500509out_del_disk:
510 del_gendisk(vblk->disk);
511 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000512out_put_disk:
513 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000514out_mempool:
515 mempool_destroy(vblk->pool);
516out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600517 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000518out_free_vblk:
519 kfree(vblk);
520out:
521 return err;
522}
523
Mike Frysinger98e94442009-05-18 03:39:09 -0400524static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000525{
526 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000527
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100528 flush_work(&vblk->config_work);
529
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500530 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000531 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500532
533 /* Stop all the virtqueues. */
534 vdev->config->reset(vdev);
535
Chris Lalancetteac9d4632008-05-30 15:09:41 -0500536 del_gendisk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000537 blk_cleanup_queue(vblk->disk->queue);
538 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000539 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600540 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000541 kfree(vblk);
542}
543
Márton Németh47483e22010-01-10 13:40:02 +0100544static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000545 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
546 { 0 },
547};
548
Rusty Russellc45a6812008-05-02 21:50:50 -0500549static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200550 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
551 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
552 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
Rusty Russellc45a6812008-05-02 21:50:50 -0500553};
554
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600555/*
556 * virtio_blk causes spurious section mismatch warning by
557 * simultaneously referring to a __devinit and a __devexit function.
558 * Use __refdata to avoid this warning.
559 */
560static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100561 .feature_table = features,
562 .feature_table_size = ARRAY_SIZE(features),
563 .driver.name = KBUILD_MODNAME,
564 .driver.owner = THIS_MODULE,
565 .id_table = id_table,
566 .probe = virtblk_probe,
567 .remove = __devexit_p(virtblk_remove),
568 .config_changed = virtblk_config_changed,
Rusty Russelle467cde2007-10-22 11:03:38 +1000569};
570
571static int __init init(void)
572{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100573 int error;
574
575 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
576 if (!virtblk_wq)
577 return -ENOMEM;
578
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100579 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100580 if (major < 0) {
581 error = major;
582 goto out_destroy_workqueue;
583 }
584
585 error = register_virtio_driver(&virtio_blk);
586 if (error)
587 goto out_unregister_blkdev;
588 return 0;
589
590out_unregister_blkdev:
591 unregister_blkdev(major, "virtblk");
592out_destroy_workqueue:
593 destroy_workqueue(virtblk_wq);
594 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000595}
596
597static void __exit fini(void)
598{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100599 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000600 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100601 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000602}
603module_init(init);
604module_exit(fini);
605
606MODULE_DEVICE_TABLE(virtio, id_table);
607MODULE_DESCRIPTION("Virtio block driver");
608MODULE_LICENSE("GPL");