blob: e7a5750a93d945657f0aa1e63175ad66369aeaa6 [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>
Liu Yuan6917f832011-04-24 02:49:26 +080010#include <scsi/scsi_cmnd.h>
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020011#include <linux/idr.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
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020015static int major;
16static DEFINE_IDA(vd_index_ida);
17
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010018struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010019
Rusty Russelle467cde2007-10-22 11:03:38 +100020struct virtio_blk
21{
22 spinlock_t lock;
23
24 struct virtio_device *vdev;
25 struct virtqueue *vq;
26
27 /* The disk structure for the kernel. */
28 struct gendisk *disk;
29
30 /* Request tracking. */
31 struct list_head reqs;
32
33 mempool_t *pool;
34
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010035 /* Process context for config space updates */
36 struct work_struct config_work;
37
Rusty Russell0864b792008-12-30 09:26:05 -060038 /* What host tells us, plus 2 for header & tailer. */
39 unsigned int sg_elems;
40
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020041 /* Ida index - used to track minor number allocations. */
42 int index;
43
Rusty Russelle467cde2007-10-22 11:03:38 +100044 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060045 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100046};
47
48struct virtblk_req
49{
50 struct list_head list;
51 struct request *req;
52 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020053 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050054 u8 status;
Rusty Russelle467cde2007-10-22 11:03:38 +100055};
56
Rusty Russell18445c42008-02-04 23:49:57 -050057static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100058{
59 struct virtio_blk *vblk = vq->vdev->priv;
60 struct virtblk_req *vbr;
61 unsigned int len;
62 unsigned long flags;
63
64 spin_lock_irqsave(&vblk->lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +030065 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Kiyoshi Ueda83169822008-10-01 10:11:20 -040066 int error;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020067
Rusty Russellcb38fa22008-05-02 21:50:45 -050068 switch (vbr->status) {
Rusty Russelle467cde2007-10-22 11:03:38 +100069 case VIRTIO_BLK_S_OK:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040070 error = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100071 break;
72 case VIRTIO_BLK_S_UNSUPP:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040073 error = -ENOTTY;
Rusty Russelle467cde2007-10-22 11:03:38 +100074 break;
75 default:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040076 error = -EIO;
Rusty Russelle467cde2007-10-22 11:03:38 +100077 break;
78 }
79
Christoph Hellwig33659eb2010-08-07 18:17:56 +020080 switch (vbr->req->cmd_type) {
81 case REQ_TYPE_BLOCK_PC:
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020082 vbr->req->resid_len = vbr->in_hdr.residual;
83 vbr->req->sense_len = vbr->in_hdr.sense_len;
84 vbr->req->errors = vbr->in_hdr.errors;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020085 break;
86 case REQ_TYPE_SPECIAL:
john cooper4cb2ea22010-03-25 01:33:33 -040087 vbr->req->errors = (error != 0);
Christoph Hellwig33659eb2010-08-07 18:17:56 +020088 break;
Jens Axboe15fa6e82010-06-18 12:10:18 +020089 default:
90 break;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020091 }
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020092
Tejun Heo40cbbb72009-04-23 11:05:19 +090093 __blk_end_request_all(vbr->req, error);
Rusty Russelle467cde2007-10-22 11:03:38 +100094 list_del(&vbr->list);
95 mempool_free(vbr, vblk->pool);
96 }
97 /* In case queue is stopped waiting for more buffers. */
98 blk_start_queue(vblk->disk->queue);
99 spin_unlock_irqrestore(&vblk->lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +1000100}
101
102static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
103 struct request *req)
104{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200105 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000106 struct virtblk_req *vbr;
107
108 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
109 if (!vbr)
110 /* When another request finishes we'll try again. */
111 return false;
112
113 vbr->req = req;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900114
115 if (req->cmd_flags & REQ_FLUSH) {
116 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000117 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200118 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900119 } else {
120 switch (req->cmd_type) {
121 case REQ_TYPE_FS:
122 vbr->out_hdr.type = 0;
123 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
124 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
125 break;
126 case REQ_TYPE_BLOCK_PC:
127 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200128 vbr->out_hdr.sector = 0;
129 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
130 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900131 case REQ_TYPE_SPECIAL:
132 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
133 vbr->out_hdr.sector = 0;
134 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
135 break;
136 default:
137 /* We don't put anything else in the queue. */
138 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200139 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000140 }
141
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200142 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000143
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200144 /*
145 * If this is a packet command we need a couple of additional headers.
146 * Behind the normal outhdr we put a segment with the scsi command
147 * block, and before the normal inhdr we put the sense data and the
148 * inhdr with additional status information before the normal inhdr.
149 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200150 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200151 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
152
153 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
154
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200155 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800156 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200157 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
158 sizeof(vbr->in_hdr));
159 }
160
161 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
162 sizeof(vbr->status));
163
164 if (num) {
165 if (rq_data_dir(vbr->req) == WRITE) {
166 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
167 out += num;
168 } else {
169 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
170 in += num;
171 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000172 }
173
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300174 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000175 mempool_free(vbr, vblk->pool);
176 return false;
177 }
178
179 list_add_tail(&vbr->list, &vblk->reqs);
180 return true;
181}
182
183static void do_virtblk_request(struct request_queue *q)
184{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200185 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000186 struct request *req;
187 unsigned int issued = 0;
188
Tejun Heo9934c8c2009-05-08 11:54:16 +0900189 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600190 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000191
192 /* If this request fails, stop queue and wait for something to
193 finish to restart it. */
194 if (!do_req(q, vblk, req)) {
195 blk_stop_queue(q);
196 break;
197 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900198 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000199 issued++;
200 }
201
202 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300203 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000204}
205
john cooper4cb2ea22010-03-25 01:33:33 -0400206/* return id (s/n) string for *disk to *id_str
207 */
208static int virtblk_get_id(struct gendisk *disk, char *id_str)
209{
210 struct virtio_blk *vblk = disk->private_data;
211 struct request *req;
212 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030213 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400214
215 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
216 GFP_KERNEL);
217 if (IS_ERR(bio))
218 return PTR_ERR(bio);
219
220 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
221 if (IS_ERR(req)) {
222 bio_put(bio);
223 return PTR_ERR(req);
224 }
225
226 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030227 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
228 blk_put_request(req);
229
230 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400231}
232
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200233static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
234 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000235{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200236 struct gendisk *disk = bdev->bd_disk;
237 struct virtio_blk *vblk = disk->private_data;
238
239 /*
240 * Only allow the generic SCSI ioctls if the host can support it.
241 */
242 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200243 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200244
Rusty Russell3225bea2009-10-22 16:39:28 -0600245 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd,
246 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000247}
248
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100249/* We provide getgeo only to please some old bootloader/partitioning tools */
250static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
251{
Ryan Harper48e40432008-04-16 13:56:37 -0500252 struct virtio_blk *vblk = bd->bd_disk->private_data;
253 struct virtio_blk_geometry vgeo;
254 int err;
255
256 /* see if the host passed in geometry config */
257 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
258 offsetof(struct virtio_blk_config, geometry),
259 &vgeo);
260
261 if (!err) {
262 geo->heads = vgeo.heads;
263 geo->sectors = vgeo.sectors;
264 geo->cylinders = vgeo.cylinders;
265 } else {
266 /* some standard values, similar to sd */
267 geo->heads = 1 << 6;
268 geo->sectors = 1 << 5;
269 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
270 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100271 return 0;
272}
273
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700274static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200275 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100276 .owner = THIS_MODULE,
277 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000278};
279
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100280static int index_to_minor(int index)
281{
282 return index << PART_BITS;
283}
284
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200285static int minor_to_index(int minor)
286{
287 return minor >> PART_BITS;
288}
289
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500290static ssize_t virtblk_serial_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct gendisk *disk = dev_to_disk(dev);
294 int err;
295
296 /* sysfs gives us a PAGE_SIZE buffer */
297 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
298
299 buf[VIRTIO_BLK_ID_BYTES] = '\0';
300 err = virtblk_get_id(disk, buf);
301 if (!err)
302 return strlen(buf);
303
304 if (err == -EIO) /* Unsupported? Make it empty. */
305 return 0;
306
307 return err;
308}
309DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
310
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100311static void virtblk_config_changed_work(struct work_struct *work)
312{
313 struct virtio_blk *vblk =
314 container_of(work, struct virtio_blk, config_work);
315 struct virtio_device *vdev = vblk->vdev;
316 struct request_queue *q = vblk->disk->queue;
317 char cap_str_2[10], cap_str_10[10];
318 u64 capacity, size;
319
320 /* Host must always specify the capacity. */
321 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
322 &capacity, sizeof(capacity));
323
324 /* If capacity is too big, truncate with warning. */
325 if ((sector_t)capacity != capacity) {
326 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
327 (unsigned long long)capacity);
328 capacity = (sector_t)-1;
329 }
330
331 size = capacity * queue_logical_block_size(q);
332 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
333 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
334
335 dev_notice(&vdev->dev,
336 "new size: %llu %d-byte logical blocks (%s/%s)\n",
337 (unsigned long long)capacity,
338 queue_logical_block_size(q),
339 cap_str_10, cap_str_2);
340
341 set_capacity(vblk->disk, capacity);
342}
343
344static void virtblk_config_changed(struct virtio_device *vdev)
345{
346 struct virtio_blk *vblk = vdev->priv;
347
348 queue_work(virtblk_wq, &vblk->config_work);
349}
350
Mike Frysinger98e94442009-05-18 03:39:09 -0400351static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000352{
353 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600354 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200355 int err, index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000356 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600357 u32 v, blk_size, sg_elems, opt_io_size;
358 u16 min_io_size;
359 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000360
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200361 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
362 GFP_KERNEL);
363 if (err < 0)
364 goto out;
365 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100366
Rusty Russell0864b792008-12-30 09:26:05 -0600367 /* We need to know how many segments before we allocate. */
368 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
369 offsetof(struct virtio_blk_config, seg_max),
370 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200371
372 /* We need at least one SG element, whatever they say. */
373 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600374 sg_elems = 1;
375
376 /* We need an extra sg elements at head and tail. */
377 sg_elems += 2;
378 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
379 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000380 if (!vblk) {
381 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200382 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000383 }
384
385 INIT_LIST_HEAD(&vblk->reqs);
386 spin_lock_init(&vblk->lock);
387 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600388 vblk->sg_elems = sg_elems;
389 sg_init_table(vblk->sg, vblk->sg_elems);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100390 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000391
392 /* We expect one virtqueue, for output. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600393 vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests");
Rusty Russelle467cde2007-10-22 11:03:38 +1000394 if (IS_ERR(vblk->vq)) {
395 err = PTR_ERR(vblk->vq);
396 goto out_free_vblk;
397 }
398
399 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
400 if (!vblk->pool) {
401 err = -ENOMEM;
402 goto out_free_vq;
403 }
404
Rusty Russelle467cde2007-10-22 11:03:38 +1000405 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100406 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000407 if (!vblk->disk) {
408 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100409 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000410 }
411
Christoph Hellwig69740c82010-02-24 14:22:25 -0600412 q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
413 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000414 err = -ENOMEM;
415 goto out_put_disk;
416 }
417
Christoph Hellwig69740c82010-02-24 14:22:25 -0600418 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900419
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100420 if (index < 26) {
421 sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
422 } else if (index < (26 + 1) * 26) {
423 sprintf(vblk->disk->disk_name, "vd%c%c",
424 'a' + index / 26 - 1, 'a' + index % 26);
425 } else {
426 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
427 const unsigned int m2 = (index / 26 - 1) % 26;
428 const unsigned int m3 = index % 26;
429 sprintf(vblk->disk->disk_name, "vd%c%c%c",
430 'a' + m1, 'a' + m2, 'a' + m3);
431 }
432
Rusty Russelle467cde2007-10-22 11:03:38 +1000433 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100434 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000435 vblk->disk->private_data = vblk;
436 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500437 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200438 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100439
Tejun Heo02c42b72010-09-03 11:56:18 +0200440 /* configure queue flush support */
Tejun Heo4913efe2010-09-03 11:56:16 +0200441 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
442 blk_queue_flush(q, REQ_FLUSH);
Rusty Russelle467cde2007-10-22 11:03:38 +1000443
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200444 /* If disk is read-only in the host, the guest should obey */
445 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
446 set_disk_ro(vblk->disk, 1);
447
Rusty Russella586d4f2008-02-04 23:49:56 -0500448 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500449 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
450 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000451
452 /* If capacity is too big, truncate with warning. */
453 if ((sector_t)cap != cap) {
454 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
455 (unsigned long long)cap);
456 cap = (sector_t)-1;
457 }
458 set_capacity(vblk->disk, cap);
459
Rusty Russell0864b792008-12-30 09:26:05 -0600460 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500461 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600462
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600463 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600464 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600465
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600466 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500467 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600468
Rusty Russella586d4f2008-02-04 23:49:56 -0500469 /* Host can optionally specify maximum segment size and number of
470 * segments. */
471 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
472 offsetof(struct virtio_blk_config, size_max),
473 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000474 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600475 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600476 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600477 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000478
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200479 /* Host can optionally specify the block size of the device */
480 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
481 offsetof(struct virtio_blk_config, blk_size),
482 &blk_size);
483 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600484 blk_queue_logical_block_size(q, blk_size);
485 else
486 blk_size = queue_logical_block_size(q);
487
488 /* Use topology information if available */
489 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
490 offsetof(struct virtio_blk_config, physical_block_exp),
491 &physical_block_exp);
492 if (!err && physical_block_exp)
493 blk_queue_physical_block_size(q,
494 blk_size * (1 << physical_block_exp));
495
496 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
497 offsetof(struct virtio_blk_config, alignment_offset),
498 &alignment_offset);
499 if (!err && alignment_offset)
500 blk_queue_alignment_offset(q, blk_size * alignment_offset);
501
502 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
503 offsetof(struct virtio_blk_config, min_io_size),
504 &min_io_size);
505 if (!err && min_io_size)
506 blk_queue_io_min(q, blk_size * min_io_size);
507
508 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
509 offsetof(struct virtio_blk_config, opt_io_size),
510 &opt_io_size);
511 if (!err && opt_io_size)
512 blk_queue_io_opt(q, blk_size * opt_io_size);
513
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200514
Rusty Russelle467cde2007-10-22 11:03:38 +1000515 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500516 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
517 if (err)
518 goto out_del_disk;
519
Rusty Russelle467cde2007-10-22 11:03:38 +1000520 return 0;
521
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500522out_del_disk:
523 del_gendisk(vblk->disk);
524 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000525out_put_disk:
526 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000527out_mempool:
528 mempool_destroy(vblk->pool);
529out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600530 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000531out_free_vblk:
532 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200533out_free_index:
534 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000535out:
536 return err;
537}
538
Mike Frysinger98e94442009-05-18 03:39:09 -0400539static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000540{
541 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200542 int index = vblk->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000543
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100544 flush_work(&vblk->config_work);
545
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500546 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000547 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500548
549 /* Stop all the virtqueues. */
550 vdev->config->reset(vdev);
551
Chris Lalancetteac9d4632008-05-30 15:09:41 -0500552 del_gendisk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000553 blk_cleanup_queue(vblk->disk->queue);
554 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000555 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600556 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000557 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200558 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000559}
560
Márton Németh47483e22010-01-10 13:40:02 +0100561static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000562 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
563 { 0 },
564};
565
Rusty Russellc45a6812008-05-02 21:50:50 -0500566static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200567 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
568 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
569 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
Rusty Russellc45a6812008-05-02 21:50:50 -0500570};
571
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600572/*
573 * virtio_blk causes spurious section mismatch warning by
574 * simultaneously referring to a __devinit and a __devexit function.
575 * Use __refdata to avoid this warning.
576 */
577static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100578 .feature_table = features,
579 .feature_table_size = ARRAY_SIZE(features),
580 .driver.name = KBUILD_MODNAME,
581 .driver.owner = THIS_MODULE,
582 .id_table = id_table,
583 .probe = virtblk_probe,
584 .remove = __devexit_p(virtblk_remove),
585 .config_changed = virtblk_config_changed,
Rusty Russelle467cde2007-10-22 11:03:38 +1000586};
587
588static int __init init(void)
589{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100590 int error;
591
592 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
593 if (!virtblk_wq)
594 return -ENOMEM;
595
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100596 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100597 if (major < 0) {
598 error = major;
599 goto out_destroy_workqueue;
600 }
601
602 error = register_virtio_driver(&virtio_blk);
603 if (error)
604 goto out_unregister_blkdev;
605 return 0;
606
607out_unregister_blkdev:
608 unregister_blkdev(major, "virtblk");
609out_destroy_workqueue:
610 destroy_workqueue(virtblk_wq);
611 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000612}
613
614static void __exit fini(void)
615{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100616 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000617 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100618 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000619}
620module_init(init);
621module_exit(fini);
622
623MODULE_DEVICE_TABLE(virtio, id_table);
624MODULE_DESCRIPTION("Virtio block driver");
625MODULE_LICENSE("GPL");