blob: e51fdc72cdc06e216cbf886da824004c7a873488 [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>
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +10307#include <linux/mutex.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10008#include <linux/virtio.h>
9#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020010#include <linux/scatterlist.h>
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010011#include <linux/string_helpers.h>
Liu Yuan6917f832011-04-24 02:49:26 +080012#include <scsi/scsi_cmnd.h>
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020013#include <linux/idr.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020014
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010015#define PART_BITS 4
Rusty Russelle467cde2007-10-22 11:03:38 +100016
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020017static int major;
18static DEFINE_IDA(vd_index_ida);
19
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010020struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010021
Rusty Russelle467cde2007-10-22 11:03:38 +100022struct virtio_blk
23{
Rusty Russelle467cde2007-10-22 11:03:38 +100024 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
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +103038 /* Lock for config space updates */
39 struct mutex config_lock;
40
41 /* enable config space updates */
42 bool config_enable;
43
Rusty Russell0864b792008-12-30 09:26:05 -060044 /* What host tells us, plus 2 for header & tailer. */
45 unsigned int sg_elems;
46
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020047 /* Ida index - used to track minor number allocations. */
48 int index;
49
Rusty Russelle467cde2007-10-22 11:03:38 +100050 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060051 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100052};
53
54struct virtblk_req
55{
56 struct list_head list;
57 struct request *req;
58 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020059 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050060 u8 status;
Rusty Russelle467cde2007-10-22 11:03:38 +100061};
62
Rusty Russell18445c42008-02-04 23:49:57 -050063static void blk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +100064{
65 struct virtio_blk *vblk = vq->vdev->priv;
66 struct virtblk_req *vbr;
67 unsigned int len;
68 unsigned long flags;
69
Asias He374d3a42012-05-25 16:03:27 +080070 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +030071 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Kiyoshi Ueda83169822008-10-01 10:11:20 -040072 int error;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020073
Rusty Russellcb38fa22008-05-02 21:50:45 -050074 switch (vbr->status) {
Rusty Russelle467cde2007-10-22 11:03:38 +100075 case VIRTIO_BLK_S_OK:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040076 error = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +100077 break;
78 case VIRTIO_BLK_S_UNSUPP:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040079 error = -ENOTTY;
Rusty Russelle467cde2007-10-22 11:03:38 +100080 break;
81 default:
Kiyoshi Ueda83169822008-10-01 10:11:20 -040082 error = -EIO;
Rusty Russelle467cde2007-10-22 11:03:38 +100083 break;
84 }
85
Christoph Hellwig33659eb2010-08-07 18:17:56 +020086 switch (vbr->req->cmd_type) {
87 case REQ_TYPE_BLOCK_PC:
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020088 vbr->req->resid_len = vbr->in_hdr.residual;
89 vbr->req->sense_len = vbr->in_hdr.sense_len;
90 vbr->req->errors = vbr->in_hdr.errors;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020091 break;
92 case REQ_TYPE_SPECIAL:
john cooper4cb2ea22010-03-25 01:33:33 -040093 vbr->req->errors = (error != 0);
Christoph Hellwig33659eb2010-08-07 18:17:56 +020094 break;
Jens Axboe15fa6e82010-06-18 12:10:18 +020095 default:
96 break;
Christoph Hellwig33659eb2010-08-07 18:17:56 +020097 }
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020098
Tejun Heo40cbbb72009-04-23 11:05:19 +090099 __blk_end_request_all(vbr->req, error);
Rusty Russelle467cde2007-10-22 11:03:38 +1000100 list_del(&vbr->list);
101 mempool_free(vbr, vblk->pool);
102 }
103 /* In case queue is stopped waiting for more buffers. */
104 blk_start_queue(vblk->disk->queue);
Asias He374d3a42012-05-25 16:03:27 +0800105 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
Rusty Russelle467cde2007-10-22 11:03:38 +1000106}
107
108static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
109 struct request *req)
110{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200111 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000112 struct virtblk_req *vbr;
113
114 vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
115 if (!vbr)
116 /* When another request finishes we'll try again. */
117 return false;
118
119 vbr->req = req;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900120
121 if (req->cmd_flags & REQ_FLUSH) {
122 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000123 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200124 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900125 } else {
126 switch (req->cmd_type) {
127 case REQ_TYPE_FS:
128 vbr->out_hdr.type = 0;
129 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
130 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
131 break;
132 case REQ_TYPE_BLOCK_PC:
133 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200134 vbr->out_hdr.sector = 0;
135 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
136 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900137 case REQ_TYPE_SPECIAL:
138 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
139 vbr->out_hdr.sector = 0;
140 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
141 break;
142 default:
143 /* We don't put anything else in the queue. */
144 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200145 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000146 }
147
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200148 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000149
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200150 /*
151 * If this is a packet command we need a couple of additional headers.
152 * Behind the normal outhdr we put a segment with the scsi command
153 * block, and before the normal inhdr we put the sense data and the
154 * inhdr with additional status information before the normal inhdr.
155 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200156 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200157 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
158
159 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
160
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200161 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800162 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200163 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
164 sizeof(vbr->in_hdr));
165 }
166
167 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
168 sizeof(vbr->status));
169
170 if (num) {
171 if (rq_data_dir(vbr->req) == WRITE) {
172 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
173 out += num;
174 } else {
175 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
176 in += num;
177 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000178 }
179
Rusty Russellf96fde42012-01-12 15:44:42 +1030180 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr, GFP_ATOMIC)<0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000181 mempool_free(vbr, vblk->pool);
182 return false;
183 }
184
185 list_add_tail(&vbr->list, &vblk->reqs);
186 return true;
187}
188
189static void do_virtblk_request(struct request_queue *q)
190{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200191 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000192 struct request *req;
193 unsigned int issued = 0;
194
Tejun Heo9934c8c2009-05-08 11:54:16 +0900195 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600196 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000197
198 /* If this request fails, stop queue and wait for something to
199 finish to restart it. */
200 if (!do_req(q, vblk, req)) {
201 blk_stop_queue(q);
202 break;
203 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900204 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000205 issued++;
206 }
207
208 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300209 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000210}
211
john cooper4cb2ea22010-03-25 01:33:33 -0400212/* return id (s/n) string for *disk to *id_str
213 */
214static int virtblk_get_id(struct gendisk *disk, char *id_str)
215{
216 struct virtio_blk *vblk = disk->private_data;
217 struct request *req;
218 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030219 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400220
221 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
222 GFP_KERNEL);
223 if (IS_ERR(bio))
224 return PTR_ERR(bio);
225
226 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
227 if (IS_ERR(req)) {
228 bio_put(bio);
229 return PTR_ERR(req);
230 }
231
232 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030233 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
234 blk_put_request(req);
235
236 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400237}
238
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200239static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
240 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000241{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200242 struct gendisk *disk = bdev->bd_disk;
243 struct virtio_blk *vblk = disk->private_data;
244
245 /*
246 * Only allow the generic SCSI ioctls if the host can support it.
247 */
248 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200249 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200250
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100251 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
252 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000253}
254
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100255/* We provide getgeo only to please some old bootloader/partitioning tools */
256static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
257{
Ryan Harper48e40432008-04-16 13:56:37 -0500258 struct virtio_blk *vblk = bd->bd_disk->private_data;
259 struct virtio_blk_geometry vgeo;
260 int err;
261
262 /* see if the host passed in geometry config */
263 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
264 offsetof(struct virtio_blk_config, geometry),
265 &vgeo);
266
267 if (!err) {
268 geo->heads = vgeo.heads;
269 geo->sectors = vgeo.sectors;
270 geo->cylinders = vgeo.cylinders;
271 } else {
272 /* some standard values, similar to sd */
273 geo->heads = 1 << 6;
274 geo->sectors = 1 << 5;
275 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
276 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100277 return 0;
278}
279
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700280static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200281 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100282 .owner = THIS_MODULE,
283 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000284};
285
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100286static int index_to_minor(int index)
287{
288 return index << PART_BITS;
289}
290
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200291static int minor_to_index(int minor)
292{
293 return minor >> PART_BITS;
294}
295
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500296static ssize_t virtblk_serial_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
299 struct gendisk *disk = dev_to_disk(dev);
300 int err;
301
302 /* sysfs gives us a PAGE_SIZE buffer */
303 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
304
305 buf[VIRTIO_BLK_ID_BYTES] = '\0';
306 err = virtblk_get_id(disk, buf);
307 if (!err)
308 return strlen(buf);
309
310 if (err == -EIO) /* Unsupported? Make it empty. */
311 return 0;
312
313 return err;
314}
315DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
316
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100317static void virtblk_config_changed_work(struct work_struct *work)
318{
319 struct virtio_blk *vblk =
320 container_of(work, struct virtio_blk, config_work);
321 struct virtio_device *vdev = vblk->vdev;
322 struct request_queue *q = vblk->disk->queue;
323 char cap_str_2[10], cap_str_10[10];
324 u64 capacity, size;
325
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030326 mutex_lock(&vblk->config_lock);
327 if (!vblk->config_enable)
328 goto done;
329
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100330 /* Host must always specify the capacity. */
331 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
332 &capacity, sizeof(capacity));
333
334 /* If capacity is too big, truncate with warning. */
335 if ((sector_t)capacity != capacity) {
336 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
337 (unsigned long long)capacity);
338 capacity = (sector_t)-1;
339 }
340
341 size = capacity * queue_logical_block_size(q);
342 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
343 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
344
345 dev_notice(&vdev->dev,
346 "new size: %llu %d-byte logical blocks (%s/%s)\n",
347 (unsigned long long)capacity,
348 queue_logical_block_size(q),
349 cap_str_10, cap_str_2);
350
351 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200352 revalidate_disk(vblk->disk);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030353done:
354 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100355}
356
357static void virtblk_config_changed(struct virtio_device *vdev)
358{
359 struct virtio_blk *vblk = vdev->priv;
360
361 queue_work(virtblk_wq, &vblk->config_work);
362}
363
Amit Shah6abd6e52011-12-22 16:58:29 +0530364static int init_vq(struct virtio_blk *vblk)
365{
366 int err = 0;
367
368 /* We expect one virtqueue, for output. */
369 vblk->vq = virtio_find_single_vq(vblk->vdev, blk_done, "requests");
370 if (IS_ERR(vblk->vq))
371 err = PTR_ERR(vblk->vq);
372
373 return err;
374}
375
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800376/*
377 * Legacy naming scheme used for virtio devices. We are stuck with it for
378 * virtio blk but don't ever use it for any new driver.
379 */
380static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
381{
382 const int base = 'z' - 'a' + 1;
383 char *begin = buf + strlen(prefix);
384 char *end = buf + buflen;
385 char *p;
386 int unit;
387
388 p = end - 1;
389 *p = '\0';
390 unit = base;
391 do {
392 if (p == begin)
393 return -EINVAL;
394 *--p = 'a' + (index % unit);
395 index = (index / unit) - 1;
396 } while (index >= 0);
397
398 memmove(begin, p, end - p);
399 memcpy(buf, prefix, strlen(prefix));
400
401 return 0;
402}
403
Mike Frysinger98e94442009-05-18 03:39:09 -0400404static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000405{
406 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600407 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200408 int err, index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000409 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600410 u32 v, blk_size, sg_elems, opt_io_size;
411 u16 min_io_size;
412 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000413
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200414 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
415 GFP_KERNEL);
416 if (err < 0)
417 goto out;
418 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100419
Rusty Russell0864b792008-12-30 09:26:05 -0600420 /* We need to know how many segments before we allocate. */
421 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
422 offsetof(struct virtio_blk_config, seg_max),
423 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200424
425 /* We need at least one SG element, whatever they say. */
426 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600427 sg_elems = 1;
428
429 /* We need an extra sg elements at head and tail. */
430 sg_elems += 2;
431 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
432 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000433 if (!vblk) {
434 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200435 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000436 }
437
438 INIT_LIST_HEAD(&vblk->reqs);
Rusty Russelle467cde2007-10-22 11:03:38 +1000439 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600440 vblk->sg_elems = sg_elems;
441 sg_init_table(vblk->sg, vblk->sg_elems);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030442 mutex_init(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100443 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030444 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000445
Amit Shah6abd6e52011-12-22 16:58:29 +0530446 err = init_vq(vblk);
447 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000448 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000449
450 vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
451 if (!vblk->pool) {
452 err = -ENOMEM;
453 goto out_free_vq;
454 }
455
Rusty Russelle467cde2007-10-22 11:03:38 +1000456 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100457 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000458 if (!vblk->disk) {
459 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100460 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000461 }
462
Asias He374d3a42012-05-25 16:03:27 +0800463 q = vblk->disk->queue = blk_init_queue(do_virtblk_request, NULL);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600464 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000465 err = -ENOMEM;
466 goto out_put_disk;
467 }
468
Christoph Hellwig69740c82010-02-24 14:22:25 -0600469 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900470
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800471 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100472
Rusty Russelle467cde2007-10-22 11:03:38 +1000473 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100474 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000475 vblk->disk->private_data = vblk;
476 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500477 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200478 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100479
Tejun Heo02c42b72010-09-03 11:56:18 +0200480 /* configure queue flush support */
Tejun Heo4913efe2010-09-03 11:56:16 +0200481 if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
482 blk_queue_flush(q, REQ_FLUSH);
Rusty Russelle467cde2007-10-22 11:03:38 +1000483
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200484 /* If disk is read-only in the host, the guest should obey */
485 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
486 set_disk_ro(vblk->disk, 1);
487
Rusty Russella586d4f2008-02-04 23:49:56 -0500488 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500489 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
490 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000491
492 /* If capacity is too big, truncate with warning. */
493 if ((sector_t)cap != cap) {
494 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
495 (unsigned long long)cap);
496 cap = (sector_t)-1;
497 }
498 set_capacity(vblk->disk, cap);
499
Rusty Russell0864b792008-12-30 09:26:05 -0600500 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500501 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600502
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600503 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600504 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600505
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600506 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500507 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600508
Rusty Russella586d4f2008-02-04 23:49:56 -0500509 /* Host can optionally specify maximum segment size and number of
510 * segments. */
511 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
512 offsetof(struct virtio_blk_config, size_max),
513 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000514 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600515 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600516 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600517 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000518
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200519 /* Host can optionally specify the block size of the device */
520 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
521 offsetof(struct virtio_blk_config, blk_size),
522 &blk_size);
523 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600524 blk_queue_logical_block_size(q, blk_size);
525 else
526 blk_size = queue_logical_block_size(q);
527
528 /* Use topology information if available */
529 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
530 offsetof(struct virtio_blk_config, physical_block_exp),
531 &physical_block_exp);
532 if (!err && physical_block_exp)
533 blk_queue_physical_block_size(q,
534 blk_size * (1 << physical_block_exp));
535
536 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
537 offsetof(struct virtio_blk_config, alignment_offset),
538 &alignment_offset);
539 if (!err && alignment_offset)
540 blk_queue_alignment_offset(q, blk_size * alignment_offset);
541
542 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
543 offsetof(struct virtio_blk_config, min_io_size),
544 &min_io_size);
545 if (!err && min_io_size)
546 blk_queue_io_min(q, blk_size * min_io_size);
547
548 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
549 offsetof(struct virtio_blk_config, opt_io_size),
550 &opt_io_size);
551 if (!err && opt_io_size)
552 blk_queue_io_opt(q, blk_size * opt_io_size);
553
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200554
Rusty Russelle467cde2007-10-22 11:03:38 +1000555 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500556 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
557 if (err)
558 goto out_del_disk;
559
Rusty Russelle467cde2007-10-22 11:03:38 +1000560 return 0;
561
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500562out_del_disk:
563 del_gendisk(vblk->disk);
564 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000565out_put_disk:
566 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000567out_mempool:
568 mempool_destroy(vblk->pool);
569out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600570 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000571out_free_vblk:
572 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200573out_free_index:
574 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000575out:
576 return err;
577}
578
Mike Frysinger98e94442009-05-18 03:39:09 -0400579static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000580{
581 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200582 int index = vblk->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000583
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030584 /* Prevent config work handler from accessing the device. */
585 mutex_lock(&vblk->config_lock);
586 vblk->config_enable = false;
587 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100588
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500589 /* Nothing should be pending. */
Rusty Russelle467cde2007-10-22 11:03:38 +1000590 BUG_ON(!list_empty(&vblk->reqs));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500591
592 /* Stop all the virtqueues. */
593 vdev->config->reset(vdev);
594
Michael S. Tsirkin4678d6f2012-01-12 15:44:44 +1030595 flush_work(&vblk->config_work);
596
Chris Lalancetteac9d4632008-05-30 15:09:41 -0500597 del_gendisk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000598 blk_cleanup_queue(vblk->disk->queue);
599 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000600 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600601 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000602 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200603 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000604}
605
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530606#ifdef CONFIG_PM
607static int virtblk_freeze(struct virtio_device *vdev)
608{
609 struct virtio_blk *vblk = vdev->priv;
610
611 /* Ensure we don't receive any more interrupts */
612 vdev->config->reset(vdev);
613
614 /* Prevent config work handler from accessing the device. */
615 mutex_lock(&vblk->config_lock);
616 vblk->config_enable = false;
617 mutex_unlock(&vblk->config_lock);
618
619 flush_work(&vblk->config_work);
620
621 spin_lock_irq(vblk->disk->queue->queue_lock);
622 blk_stop_queue(vblk->disk->queue);
623 spin_unlock_irq(vblk->disk->queue->queue_lock);
624 blk_sync_queue(vblk->disk->queue);
625
626 vdev->config->del_vqs(vdev);
627 return 0;
628}
629
630static int virtblk_restore(struct virtio_device *vdev)
631{
632 struct virtio_blk *vblk = vdev->priv;
633 int ret;
634
635 vblk->config_enable = true;
636 ret = init_vq(vdev->priv);
637 if (!ret) {
638 spin_lock_irq(vblk->disk->queue->queue_lock);
639 blk_start_queue(vblk->disk->queue);
640 spin_unlock_irq(vblk->disk->queue->queue_lock);
641 }
642 return ret;
643}
644#endif
645
Márton Németh47483e22010-01-10 13:40:02 +0100646static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000647 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
648 { 0 },
649};
650
Rusty Russellc45a6812008-05-02 21:50:50 -0500651static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200652 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
653 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
654 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
Rusty Russellc45a6812008-05-02 21:50:50 -0500655};
656
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600657/*
658 * virtio_blk causes spurious section mismatch warning by
659 * simultaneously referring to a __devinit and a __devexit function.
660 * Use __refdata to avoid this warning.
661 */
662static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100663 .feature_table = features,
664 .feature_table_size = ARRAY_SIZE(features),
665 .driver.name = KBUILD_MODNAME,
666 .driver.owner = THIS_MODULE,
667 .id_table = id_table,
668 .probe = virtblk_probe,
669 .remove = __devexit_p(virtblk_remove),
670 .config_changed = virtblk_config_changed,
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530671#ifdef CONFIG_PM
672 .freeze = virtblk_freeze,
673 .restore = virtblk_restore,
674#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000675};
676
677static int __init init(void)
678{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100679 int error;
680
681 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
682 if (!virtblk_wq)
683 return -ENOMEM;
684
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100685 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100686 if (major < 0) {
687 error = major;
688 goto out_destroy_workqueue;
689 }
690
691 error = register_virtio_driver(&virtio_blk);
692 if (error)
693 goto out_unregister_blkdev;
694 return 0;
695
696out_unregister_blkdev:
697 unregister_blkdev(major, "virtblk");
698out_destroy_workqueue:
699 destroy_workqueue(virtblk_wq);
700 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000701}
702
703static void __exit fini(void)
704{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100705 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000706 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100707 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000708}
709module_init(init);
710module_exit(fini);
711
712MODULE_DEVICE_TABLE(virtio, id_table);
713MODULE_DESCRIPTION("Virtio block driver");
714MODULE_LICENSE("GPL");