blob: 33a48a80c7e82a6a949c6191dee5321add634377 [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>
6#include <linux/virtio.h>
7#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +02008#include <linux/scatterlist.h>
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01009#include <linux/string_helpers.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020010
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010011#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100012
Christian Borntraegerd50ed902008-02-01 09:05:00 +010013static int major, index;
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010014struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010015
Rusty Russelle467cde2007-10-22 11:03:38 +100016struct virtio_blk
17{
18 spinlock_t lock;
19
20 struct virtio_device *vdev;
21 struct virtqueue *vq;
22
23 /* The disk structure for the kernel. */
24 struct gendisk *disk;
25
26 /* Request tracking. */
27 struct list_head reqs;
28
29 mempool_t *pool;
30
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010031 /* Process context for config space updates */
32 struct work_struct config_work;
33
Rusty Russell0864b792008-12-30 09:26:05 -060034 /* What host tells us, plus 2 for header & tailer. */
35 unsigned int sg_elems;
36
Rusty Russelle467cde2007-10-22 11:03:38 +100037 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060038 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100039};
40
41struct virtblk_req
42{
43 struct list_head list;
44 struct request *req;
45 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020046 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050047 u8 status;
Rusty Russelle467cde2007-10-22 11:03:38 +100048};
49
Rusty Russell18445c42008-02-04 23:49:57 -050050static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100051{
52 struct virtio_blk *vblk = vq->vdev->priv;
53 struct virtblk_req *vbr;
54 unsigned int len;
55 unsigned long flags;
56
57 spin_lock_irqsave(&vblk->lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +030058 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Kiyoshi Ueda83169822008-10-01 10:11:20 -040059 int error;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020060
Rusty Russellcb38fa22008-05-02 21:50:45 -050061 switch (vbr->status) {
Rusty Russelle467cde2007-10-22 11:03:38 +100062 case VIRTIO_BLK_S_OK:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040063 error = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100064 break;
65 case VIRTIO_BLK_S_UNSUPP:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040066 error = -ENOTTY;
Rusty Russelle467cde2007-10-22 11:03:38 +100067 break;
68 default:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040069 error = -EIO;
Rusty Russelle467cde2007-10-22 11:03:38 +100070 break;
71 }
72
Christoph Hellwig33659eb2010-08-07 18:17:56 +020073 switch (vbr->req->cmd_type) {
74 case REQ_TYPE_BLOCK_PC:
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020075 vbr->req->resid_len = vbr->in_hdr.residual;
76 vbr->req->sense_len = vbr->in_hdr.sense_len;
77 vbr->req->errors = vbr->in_hdr.errors;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020078 break;
79 case REQ_TYPE_SPECIAL:
john cooper4cb2ea22010-03-25 01:33:33 -040080 vbr->req->errors = (error != 0);
Christoph Hellwig33659eb2010-08-07 18:17:56 +020081 break;
Jens Axboe15fa6e82010-06-18 12:10:18 +020082 default:
83 break;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020084 }
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020085
Tejun Heo40cbbb72009-04-23 11:05:19 +090086 __blk_end_request_all(vbr->req, error);
Rusty Russelle467cde2007-10-22 11:03:38 +100087 list_del(&vbr->list);
88 mempool_free(vbr, vblk->pool);
89 }
90 /* In case queue is stopped waiting for more buffers. */
91 blk_start_queue(vblk->disk->queue);
92 spin_unlock_irqrestore(&vblk->lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +100093}
94
95static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
96 struct request *req)
97{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020098 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100099 struct virtblk_req *vbr;
100
101 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
102 if (!vbr)
103 /* When another request finishes we'll try again. */
104 return false;
105
106 vbr->req = req;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900107
108 if (req->cmd_flags & REQ_FLUSH) {
109 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000110 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200111 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900112 } else {
113 switch (req->cmd_type) {
114 case REQ_TYPE_FS:
115 vbr->out_hdr.type = 0;
116 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
117 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
118 break;
119 case REQ_TYPE_BLOCK_PC:
120 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200121 vbr->out_hdr.sector = 0;
122 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
123 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900124 case REQ_TYPE_SPECIAL:
125 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
126 vbr->out_hdr.sector = 0;
127 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
128 break;
129 default:
130 /* We don't put anything else in the queue. */
131 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200132 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000133 }
134
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200135 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000136
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200137 /*
138 * If this is a packet command we need a couple of additional headers.
139 * Behind the normal outhdr we put a segment with the scsi command
140 * block, and before the normal inhdr we put the sense data and the
141 * inhdr with additional status information before the normal inhdr.
142 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200143 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200144 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
145
146 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
147
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200148 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200149 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, 96);
150 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
151 sizeof(vbr->in_hdr));
152 }
153
154 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
155 sizeof(vbr->status));
156
157 if (num) {
158 if (rq_data_dir(vbr->req) == WRITE) {
159 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
160 out += num;
161 } else {
162 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
163 in += num;
164 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000165 }
166
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300167 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000168 mempool_free(vbr, vblk->pool);
169 return false;
170 }
171
172 list_add_tail(&vbr->list, &vblk->reqs);
173 return true;
174}
175
176static void do_virtblk_request(struct request_queue *q)
177{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200178 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000179 struct request *req;
180 unsigned int issued = 0;
181
Tejun Heo9934c8c2009-05-08 11:54:16 +0900182 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600183 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000184
185 /* If this request fails, stop queue and wait for something to
186 finish to restart it. */
187 if (!do_req(q, vblk, req)) {
188 blk_stop_queue(q);
189 break;
190 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900191 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000192 issued++;
193 }
194
195 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300196 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000197}
198
john cooper4cb2ea22010-03-25 01:33:33 -0400199/* return id (s/n) string for *disk to *id_str
200 */
201static int virtblk_get_id(struct gendisk *disk, char *id_str)
202{
203 struct virtio_blk *vblk = disk->private_data;
204 struct request *req;
205 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030206 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400207
208 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
209 GFP_KERNEL);
210 if (IS_ERR(bio))
211 return PTR_ERR(bio);
212
213 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
214 if (IS_ERR(req)) {
215 bio_put(bio);
216 return PTR_ERR(req);
217 }
218
219 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030220 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
221 blk_put_request(req);
222
223 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400224}
225
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200226static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
227 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000228{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200229 struct gendisk *disk = bdev->bd_disk;
230 struct virtio_blk *vblk = disk->private_data;
231
232 /*
233 * Only allow the generic SCSI ioctls if the host can support it.
234 */
235 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200236 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200237
Rusty Russell3225bea2009-10-22 16:39:28 -0600238 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd,
239 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000240}
241
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100242/* We provide getgeo only to please some old bootloader/partitioning tools */
243static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
244{
Ryan Harper48e40432008-04-16 13:56:37 -0500245 struct virtio_blk *vblk = bd->bd_disk->private_data;
246 struct virtio_blk_geometry vgeo;
247 int err;
248
249 /* see if the host passed in geometry config */
250 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
251 offsetof(struct virtio_blk_config, geometry),
252 &vgeo);
253
254 if (!err) {
255 geo->heads = vgeo.heads;
256 geo->sectors = vgeo.sectors;
257 geo->cylinders = vgeo.cylinders;
258 } else {
259 /* some standard values, similar to sd */
260 geo->heads = 1 << 6;
261 geo->sectors = 1 << 5;
262 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
263 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100264 return 0;
265}
266
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700267static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200268 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100269 .owner = THIS_MODULE,
270 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000271};
272
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100273static int index_to_minor(int index)
274{
275 return index << PART_BITS;
276}
277
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500278static ssize_t virtblk_serial_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
281 struct gendisk *disk = dev_to_disk(dev);
282 int err;
283
284 /* sysfs gives us a PAGE_SIZE buffer */
285 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
286
287 buf[VIRTIO_BLK_ID_BYTES] = '\0';
288 err = virtblk_get_id(disk, buf);
289 if (!err)
290 return strlen(buf);
291
292 if (err == -EIO) /* Unsupported? Make it empty. */
293 return 0;
294
295 return err;
296}
297DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
298
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100299static void virtblk_config_changed_work(struct work_struct *work)
300{
301 struct virtio_blk *vblk =
302 container_of(work, struct virtio_blk, config_work);
303 struct virtio_device *vdev = vblk->vdev;
304 struct request_queue *q = vblk->disk->queue;
305 char cap_str_2[10], cap_str_10[10];
306 u64 capacity, size;
307
308 /* Host must always specify the capacity. */
309 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
310 &capacity, sizeof(capacity));
311
312 /* If capacity is too big, truncate with warning. */
313 if ((sector_t)capacity != capacity) {
314 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
315 (unsigned long long)capacity);
316 capacity = (sector_t)-1;
317 }
318
319 size = capacity * queue_logical_block_size(q);
320 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
321 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
322
323 dev_notice(&vdev->dev,
324 "new size: %llu %d-byte logical blocks (%s/%s)\n",
325 (unsigned long long)capacity,
326 queue_logical_block_size(q),
327 cap_str_10, cap_str_2);
328
329 set_capacity(vblk->disk, capacity);
330}
331
332static void virtblk_config_changed(struct virtio_device *vdev)
333{
334 struct virtio_blk *vblk = vdev->priv;
335
336 queue_work(virtblk_wq, &vblk->config_work);
337}
338
Mike Frysinger98e94442009-05-18 03:39:09 -0400339static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000340{
341 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600342 struct request_queue *q;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100343 int err;
Rusty Russelle467cde2007-10-22 11:03:38 +1000344 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600345 u32 v, blk_size, sg_elems, opt_io_size;
346 u16 min_io_size;
347 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000348
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100349 if (index_to_minor(index) >= 1 << MINORBITS)
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100350 return -ENOSPC;
351
Rusty Russell0864b792008-12-30 09:26:05 -0600352 /* We need to know how many segments before we allocate. */
353 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
354 offsetof(struct virtio_blk_config, seg_max),
355 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200356
357 /* We need at least one SG element, whatever they say. */
358 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600359 sg_elems = 1;
360
361 /* We need an extra sg elements at head and tail. */
362 sg_elems += 2;
363 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
364 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000365 if (!vblk) {
366 err = -ENOMEM;
367 goto out;
368 }
369
370 INIT_LIST_HEAD(&vblk->reqs);
371 spin_lock_init(&vblk->lock);
372 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600373 vblk->sg_elems = sg_elems;
374 sg_init_table(vblk->sg, vblk->sg_elems);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100375 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000376
377 /* We expect one virtqueue, for output. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600378 vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests");
Rusty Russelle467cde2007-10-22 11:03:38 +1000379 if (IS_ERR(vblk->vq)) {
380 err = PTR_ERR(vblk->vq);
381 goto out_free_vblk;
382 }
383
384 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
385 if (!vblk->pool) {
386 err = -ENOMEM;
387 goto out_free_vq;
388 }
389
Rusty Russelle467cde2007-10-22 11:03:38 +1000390 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100391 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000392 if (!vblk->disk) {
393 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100394 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000395 }
396
Christoph Hellwig69740c82010-02-24 14:22:25 -0600397 q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
398 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000399 err = -ENOMEM;
400 goto out_put_disk;
401 }
402
Christoph Hellwig69740c82010-02-24 14:22:25 -0600403 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900404
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100405 if (index < 26) {
406 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
407 } else if (index < (26 + 1) * 26) {
408 sprintf(vblk->disk->disk_name, "vd%c%c",
409 'a' + index / 26 - 1, 'a' + index % 26);
410 } else {
411 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
412 const unsigned int m2 = (index / 26 - 1) % 26;
413 const unsigned int m3 = index % 26;
414 sprintf(vblk->disk->disk_name, "vd%c%c%c",
415 'a' + m1, 'a' + m2, 'a' + m3);
416 }
417
Rusty Russelle467cde2007-10-22 11:03:38 +1000418 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100419 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000420 vblk->disk->private_data = vblk;
421 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500422 vblk->disk->driverfs_dev = &vdev->dev;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100423 index++;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100424
Tejun Heo02c42b72010-09-03 11:56:18 +0200425 /* configure queue flush support */
Tejun Heo4913efe2010-09-03 11:56:16 +0200426 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
427 blk_queue_flush(q, REQ_FLUSH);
Rusty Russelle467cde2007-10-22 11:03:38 +1000428
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200429 /* If disk is read-only in the host, the guest should obey */
430 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
431 set_disk_ro(vblk->disk, 1);
432
Rusty Russella586d4f2008-02-04 23:49:56 -0500433 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500434 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
435 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000436
437 /* If capacity is too big, truncate with warning. */
438 if ((sector_t)cap != cap) {
439 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
440 (unsigned long long)cap);
441 cap = (sector_t)-1;
442 }
443 set_capacity(vblk->disk, cap);
444
Rusty Russell0864b792008-12-30 09:26:05 -0600445 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500446 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600447
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600448 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600449 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600450
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600451 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500452 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600453
Rusty Russella586d4f2008-02-04 23:49:56 -0500454 /* Host can optionally specify maximum segment size and number of
455 * segments. */
456 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
457 offsetof(struct virtio_blk_config, size_max),
458 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000459 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600460 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600461 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600462 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000463
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200464 /* Host can optionally specify the block size of the device */
465 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
466 offsetof(struct virtio_blk_config, blk_size),
467 &blk_size);
468 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600469 blk_queue_logical_block_size(q, blk_size);
470 else
471 blk_size = queue_logical_block_size(q);
472
473 /* Use topology information if available */
474 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
475 offsetof(struct virtio_blk_config, physical_block_exp),
476 &physical_block_exp);
477 if (!err && physical_block_exp)
478 blk_queue_physical_block_size(q,
479 blk_size * (1 << physical_block_exp));
480
481 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
482 offsetof(struct virtio_blk_config, alignment_offset),
483 &alignment_offset);
484 if (!err && alignment_offset)
485 blk_queue_alignment_offset(q, blk_size * alignment_offset);
486
487 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
488 offsetof(struct virtio_blk_config, min_io_size),
489 &min_io_size);
490 if (!err && min_io_size)
491 blk_queue_io_min(q, blk_size * min_io_size);
492
493 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
494 offsetof(struct virtio_blk_config, opt_io_size),
495 &opt_io_size);
496 if (!err && opt_io_size)
497 blk_queue_io_opt(q, blk_size * opt_io_size);
498
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200499
Rusty Russelle467cde2007-10-22 11:03:38 +1000500 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500501 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
502 if (err)
503 goto out_del_disk;
504
Rusty Russelle467cde2007-10-22 11:03:38 +1000505 return 0;
506
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500507out_del_disk:
508 del_gendisk(vblk->disk);
509 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000510out_put_disk:
511 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000512out_mempool:
513 mempool_destroy(vblk->pool);
514out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600515 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000516out_free_vblk:
517 kfree(vblk);
518out:
519 return err;
520}
521
Mike Frysinger98e94442009-05-18 03:39:09 -0400522static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000523{
524 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000525
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100526 flush_work(&vblk->config_work);
527
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500528 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000529 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500530
531 /* Stop all the virtqueues. */
532 vdev->config->reset(vdev);
533
Chris Lalancetteac9d4632008-05-30 15:09:41 -0500534 del_gendisk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000535 blk_cleanup_queue(vblk->disk->queue);
536 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000537 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600538 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000539 kfree(vblk);
540}
541
Márton Németh47483e22010-01-10 13:40:02 +0100542static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000543 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
544 { 0 },
545};
546
Rusty Russellc45a6812008-05-02 21:50:50 -0500547static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200548 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
549 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
550 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
Rusty Russellc45a6812008-05-02 21:50:50 -0500551};
552
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600553/*
554 * virtio_blk causes spurious section mismatch warning by
555 * simultaneously referring to a __devinit and a __devexit function.
556 * Use __refdata to avoid this warning.
557 */
558static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100559 .feature_table = features,
560 .feature_table_size = ARRAY_SIZE(features),
561 .driver.name = KBUILD_MODNAME,
562 .driver.owner = THIS_MODULE,
563 .id_table = id_table,
564 .probe = virtblk_probe,
565 .remove = __devexit_p(virtblk_remove),
566 .config_changed = virtblk_config_changed,
Rusty Russelle467cde2007-10-22 11:03:38 +1000567};
568
569static int __init init(void)
570{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100571 int error;
572
573 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
574 if (!virtblk_wq)
575 return -ENOMEM;
576
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100577 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100578 if (major < 0) {
579 error = major;
580 goto out_destroy_workqueue;
581 }
582
583 error = register_virtio_driver(&virtio_blk);
584 if (error)
585 goto out_unregister_blkdev;
586 return 0;
587
588out_unregister_blkdev:
589 unregister_blkdev(major, "virtblk");
590out_destroy_workqueue:
591 destroy_workqueue(virtblk_wq);
592 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000593}
594
595static void __exit fini(void)
596{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100597 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000598 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100599 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000600}
601module_init(init);
602module_exit(fini);
603
604MODULE_DEVICE_TABLE(virtio, id_table);
605MODULE_DESCRIPTION("Virtio block driver");
606MODULE_LICENSE("GPL");