blob: 95cfeeda4f3a3af201c829d19372a19a64da2d19 [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. Tsirkin4678d6f92012-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
Asias Hea98755c2012-08-08 16:07:04 +080017static bool use_bio;
18module_param(use_bio, bool, S_IRUGO);
19
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020020static int major;
21static DEFINE_IDA(vd_index_ida);
22
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010023struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010024
Rusty Russelle467cde2007-10-22 11:03:38 +100025struct virtio_blk
26{
Rusty Russelle467cde2007-10-22 11:03:38 +100027 struct virtio_device *vdev;
28 struct virtqueue *vq;
Asias Hea98755c2012-08-08 16:07:04 +080029 wait_queue_head_t queue_wait;
Rusty Russelle467cde2007-10-22 11:03:38 +100030
31 /* The disk structure for the kernel. */
32 struct gendisk *disk;
33
Rusty Russelle467cde2007-10-22 11:03:38 +100034 mempool_t *pool;
35
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010036 /* Process context for config space updates */
37 struct work_struct config_work;
38
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +103039 /* Lock for config space updates */
40 struct mutex config_lock;
41
42 /* enable config space updates */
43 bool config_enable;
44
Rusty Russell0864b792008-12-30 09:26:05 -060045 /* What host tells us, plus 2 for header & tailer. */
46 unsigned int sg_elems;
47
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020048 /* Ida index - used to track minor number allocations. */
49 int index;
50
Rusty Russelle467cde2007-10-22 11:03:38 +100051 /* Scatterlist: can be too big for stack. */
Rusty Russell0864b792008-12-30 09:26:05 -060052 struct scatterlist sg[/*sg_elems*/];
Rusty Russelle467cde2007-10-22 11:03:38 +100053};
54
55struct virtblk_req
56{
Rusty Russelle467cde2007-10-22 11:03:38 +100057 struct request *req;
Asias Hea98755c2012-08-08 16:07:04 +080058 struct bio *bio;
Rusty Russelle467cde2007-10-22 11:03:38 +100059 struct virtio_blk_outhdr out_hdr;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +020060 struct virtio_scsi_inhdr in_hdr;
Rusty Russellcb38fa22008-05-02 21:50:45 -050061 u8 status;
Asias Hea98755c2012-08-08 16:07:04 +080062 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +100063};
64
Asias Hea98755c2012-08-08 16:07:04 +080065static inline int virtblk_result(struct virtblk_req *vbr)
66{
67 switch (vbr->status) {
68 case VIRTIO_BLK_S_OK:
69 return 0;
70 case VIRTIO_BLK_S_UNSUPP:
71 return -ENOTTY;
72 default:
73 return -EIO;
74 }
75}
76
77static inline void virtblk_request_done(struct virtio_blk *vblk,
78 struct virtblk_req *vbr)
79{
80 struct request *req = vbr->req;
81 int error = virtblk_result(vbr);
82
83 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
84 req->resid_len = vbr->in_hdr.residual;
85 req->sense_len = vbr->in_hdr.sense_len;
86 req->errors = vbr->in_hdr.errors;
87 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
88 req->errors = (error != 0);
89 }
90
91 __blk_end_request_all(req, error);
92 mempool_free(vbr, vblk->pool);
93}
94
95static inline void virtblk_bio_done(struct virtio_blk *vblk,
96 struct virtblk_req *vbr)
97{
98 bio_endio(vbr->bio, virtblk_result(vbr));
99 mempool_free(vbr, vblk->pool);
100}
101
102static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000103{
104 struct virtio_blk *vblk = vq->vdev->priv;
Asias Hea98755c2012-08-08 16:07:04 +0800105 unsigned long bio_done = 0, req_done = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000106 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000107 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800108 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000109
Asias He2c95a322012-05-25 16:03:27 +0800110 spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300111 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
Asias Hea98755c2012-08-08 16:07:04 +0800112 if (vbr->bio) {
113 virtblk_bio_done(vblk, vbr);
114 bio_done++;
115 } else {
116 virtblk_request_done(vblk, vbr);
117 req_done++;
Rusty Russelle467cde2007-10-22 11:03:38 +1000118 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000119 }
120 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800121 if (req_done)
122 blk_start_queue(vblk->disk->queue);
Asias He2c95a322012-05-25 16:03:27 +0800123 spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800124
125 if (bio_done)
126 wake_up(&vblk->queue_wait);
127}
128
129static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
130 gfp_t gfp_mask)
131{
132 struct virtblk_req *vbr;
133
134 vbr = mempool_alloc(vblk->pool, gfp_mask);
135 if (vbr && use_bio)
136 sg_init_table(vbr->sg, vblk->sg_elems);
137
138 return vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000139}
140
141static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
142 struct request *req)
143{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200144 unsigned long num, out = 0, in = 0;
Rusty Russelle467cde2007-10-22 11:03:38 +1000145 struct virtblk_req *vbr;
146
Asias Hea98755c2012-08-08 16:07:04 +0800147 vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
Rusty Russelle467cde2007-10-22 11:03:38 +1000148 if (!vbr)
149 /* When another request finishes we'll try again. */
150 return false;
151
152 vbr->req = req;
Asias Hea98755c2012-08-08 16:07:04 +0800153 vbr->bio = NULL;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900154 if (req->cmd_flags & REQ_FLUSH) {
155 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
Rusty Russelle467cde2007-10-22 11:03:38 +1000156 vbr->out_hdr.sector = 0;
Fernando Luis Vázquez Cao766ca442008-08-14 09:59:13 +0200157 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900158 } else {
159 switch (req->cmd_type) {
160 case REQ_TYPE_FS:
161 vbr->out_hdr.type = 0;
162 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
163 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
164 break;
165 case REQ_TYPE_BLOCK_PC:
166 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200167 vbr->out_hdr.sector = 0;
168 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
169 break;
FUJITA Tomonoridd40e452010-07-03 17:45:38 +0900170 case REQ_TYPE_SPECIAL:
171 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
172 vbr->out_hdr.sector = 0;
173 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
174 break;
175 default:
176 /* We don't put anything else in the queue. */
177 BUG();
Christoph Hellwigf1b0ef062009-09-17 19:57:42 +0200178 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000179 }
180
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200181 sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
Rusty Russelle467cde2007-10-22 11:03:38 +1000182
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200183 /*
184 * If this is a packet command we need a couple of additional headers.
185 * Behind the normal outhdr we put a segment with the scsi command
186 * block, and before the normal inhdr we put the sense data and the
187 * inhdr with additional status information before the normal inhdr.
188 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200189 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200190 sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
191
192 num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
193
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200194 if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
Liu Yuan6917f832011-04-24 02:49:26 +0800195 sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200196 sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
197 sizeof(vbr->in_hdr));
198 }
199
200 sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
201 sizeof(vbr->status));
202
203 if (num) {
204 if (rq_data_dir(vbr->req) == WRITE) {
205 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
206 out += num;
207 } else {
208 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
209 in += num;
210 }
Rusty Russelle467cde2007-10-22 11:03:38 +1000211 }
212
Asias Hea98755c2012-08-08 16:07:04 +0800213 if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
214 GFP_ATOMIC) < 0) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000215 mempool_free(vbr, vblk->pool);
216 return false;
217 }
218
Rusty Russelle467cde2007-10-22 11:03:38 +1000219 return true;
220}
221
Asias Hea98755c2012-08-08 16:07:04 +0800222static void virtblk_request(struct request_queue *q)
Rusty Russelle467cde2007-10-22 11:03:38 +1000223{
Christoph Hellwig6c3b46f2009-05-18 14:38:28 +0200224 struct virtio_blk *vblk = q->queuedata;
Rusty Russelle467cde2007-10-22 11:03:38 +1000225 struct request *req;
226 unsigned int issued = 0;
227
Tejun Heo9934c8c2009-05-08 11:54:16 +0900228 while ((req = blk_peek_request(q)) != NULL) {
Rusty Russell0864b792008-12-30 09:26:05 -0600229 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000230
231 /* If this request fails, stop queue and wait for something to
232 finish to restart it. */
233 if (!do_req(q, vblk, req)) {
234 blk_stop_queue(q);
235 break;
236 }
Tejun Heo9934c8c2009-05-08 11:54:16 +0900237 blk_start_request(req);
Rusty Russelle467cde2007-10-22 11:03:38 +1000238 issued++;
239 }
240
241 if (issued)
Michael S. Tsirkin09ec6b62010-04-12 16:18:36 +0300242 virtqueue_kick(vblk->vq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000243}
244
Asias Hea98755c2012-08-08 16:07:04 +0800245static void virtblk_add_buf_wait(struct virtio_blk *vblk,
246 struct virtblk_req *vbr,
247 unsigned long out,
248 unsigned long in)
249{
250 DEFINE_WAIT(wait);
251
252 for (;;) {
253 prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
254 TASK_UNINTERRUPTIBLE);
255
256 spin_lock_irq(vblk->disk->queue->queue_lock);
257 if (virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
258 GFP_ATOMIC) < 0) {
259 spin_unlock_irq(vblk->disk->queue->queue_lock);
260 io_schedule();
261 } else {
262 virtqueue_kick(vblk->vq);
263 spin_unlock_irq(vblk->disk->queue->queue_lock);
264 break;
265 }
266
267 }
268
269 finish_wait(&vblk->queue_wait, &wait);
270}
271
272static void virtblk_make_request(struct request_queue *q, struct bio *bio)
273{
274 struct virtio_blk *vblk = q->queuedata;
275 unsigned int num, out = 0, in = 0;
276 struct virtblk_req *vbr;
277
278 BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
279 BUG_ON(bio->bi_rw & (REQ_FLUSH | REQ_FUA));
280
281 vbr = virtblk_alloc_req(vblk, GFP_NOIO);
282 if (!vbr) {
283 bio_endio(bio, -ENOMEM);
284 return;
285 }
286
287 vbr->bio = bio;
288 vbr->req = NULL;
289 vbr->out_hdr.type = 0;
290 vbr->out_hdr.sector = bio->bi_sector;
291 vbr->out_hdr.ioprio = bio_prio(bio);
292
293 sg_set_buf(&vbr->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
294
295 num = blk_bio_map_sg(q, bio, vbr->sg + out);
296
297 sg_set_buf(&vbr->sg[num + out + in++], &vbr->status,
298 sizeof(vbr->status));
299
300 if (num) {
301 if (bio->bi_rw & REQ_WRITE) {
302 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
303 out += num;
304 } else {
305 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
306 in += num;
307 }
308 }
309
310 spin_lock_irq(vblk->disk->queue->queue_lock);
311 if (unlikely(virtqueue_add_buf(vblk->vq, vbr->sg, out, in, vbr,
312 GFP_ATOMIC) < 0)) {
313 spin_unlock_irq(vblk->disk->queue->queue_lock);
314 virtblk_add_buf_wait(vblk, vbr, out, in);
315 return;
316 }
317 virtqueue_kick(vblk->vq);
318 spin_unlock_irq(vblk->disk->queue->queue_lock);
319}
320
john cooper4cb2ea22010-03-25 01:33:33 -0400321/* return id (s/n) string for *disk to *id_str
322 */
323static int virtblk_get_id(struct gendisk *disk, char *id_str)
324{
325 struct virtio_blk *vblk = disk->private_data;
326 struct request *req;
327 struct bio *bio;
Mike Snitzere4c47762010-10-09 12:12:13 +1030328 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400329
330 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
331 GFP_KERNEL);
332 if (IS_ERR(bio))
333 return PTR_ERR(bio);
334
335 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
336 if (IS_ERR(req)) {
337 bio_put(bio);
338 return PTR_ERR(req);
339 }
340
341 req->cmd_type = REQ_TYPE_SPECIAL;
Mike Snitzere4c47762010-10-09 12:12:13 +1030342 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
343 blk_put_request(req);
344
345 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400346}
347
Christoph Hellwigfe5a50a2010-09-15 01:27:23 +0200348static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
349 unsigned int cmd, unsigned long data)
Rusty Russelle467cde2007-10-22 11:03:38 +1000350{
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200351 struct gendisk *disk = bdev->bd_disk;
352 struct virtio_blk *vblk = disk->private_data;
353
354 /*
355 * Only allow the generic SCSI ioctls if the host can support it.
356 */
357 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
Christoph Hellwigd9ecdea2009-06-20 21:29:41 +0200358 return -ENOTTY;
Hannes Reinecke1cde26f2009-05-18 14:41:30 +0200359
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100360 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
361 (void __user *)data);
Rusty Russelle467cde2007-10-22 11:03:38 +1000362}
363
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100364/* We provide getgeo only to please some old bootloader/partitioning tools */
365static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
366{
Ryan Harper48e40432008-04-16 13:56:37 -0500367 struct virtio_blk *vblk = bd->bd_disk->private_data;
368 struct virtio_blk_geometry vgeo;
369 int err;
370
371 /* see if the host passed in geometry config */
372 err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
373 offsetof(struct virtio_blk_config, geometry),
374 &vgeo);
375
376 if (!err) {
377 geo->heads = vgeo.heads;
378 geo->sectors = vgeo.sectors;
379 geo->cylinders = vgeo.cylinders;
380 } else {
381 /* some standard values, similar to sd */
382 geo->heads = 1 << 6;
383 geo->sectors = 1 << 5;
384 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
385 }
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100386 return 0;
387}
388
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700389static const struct block_device_operations virtblk_fops = {
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200390 .ioctl = virtblk_ioctl,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100391 .owner = THIS_MODULE,
392 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000393};
394
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100395static int index_to_minor(int index)
396{
397 return index << PART_BITS;
398}
399
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200400static int minor_to_index(int minor)
401{
402 return minor >> PART_BITS;
403}
404
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500405static ssize_t virtblk_serial_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct gendisk *disk = dev_to_disk(dev);
409 int err;
410
411 /* sysfs gives us a PAGE_SIZE buffer */
412 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
413
414 buf[VIRTIO_BLK_ID_BYTES] = '\0';
415 err = virtblk_get_id(disk, buf);
416 if (!err)
417 return strlen(buf);
418
419 if (err == -EIO) /* Unsupported? Make it empty. */
420 return 0;
421
422 return err;
423}
424DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
425
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100426static void virtblk_config_changed_work(struct work_struct *work)
427{
428 struct virtio_blk *vblk =
429 container_of(work, struct virtio_blk, config_work);
430 struct virtio_device *vdev = vblk->vdev;
431 struct request_queue *q = vblk->disk->queue;
432 char cap_str_2[10], cap_str_10[10];
433 u64 capacity, size;
434
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030435 mutex_lock(&vblk->config_lock);
436 if (!vblk->config_enable)
437 goto done;
438
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100439 /* Host must always specify the capacity. */
440 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
441 &capacity, sizeof(capacity));
442
443 /* If capacity is too big, truncate with warning. */
444 if ((sector_t)capacity != capacity) {
445 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
446 (unsigned long long)capacity);
447 capacity = (sector_t)-1;
448 }
449
450 size = capacity * queue_logical_block_size(q);
451 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
452 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
453
454 dev_notice(&vdev->dev,
455 "new size: %llu %d-byte logical blocks (%s/%s)\n",
456 (unsigned long long)capacity,
457 queue_logical_block_size(q),
458 cap_str_10, cap_str_2);
459
460 set_capacity(vblk->disk, capacity);
Vivek Goyale9986f32012-03-29 10:09:44 +0200461 revalidate_disk(vblk->disk);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030462done:
463 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100464}
465
466static void virtblk_config_changed(struct virtio_device *vdev)
467{
468 struct virtio_blk *vblk = vdev->priv;
469
470 queue_work(virtblk_wq, &vblk->config_work);
471}
472
Amit Shah6abd6e52011-12-22 16:58:29 +0530473static int init_vq(struct virtio_blk *vblk)
474{
475 int err = 0;
476
477 /* We expect one virtqueue, for output. */
Asias Hea98755c2012-08-08 16:07:04 +0800478 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
Amit Shah6abd6e52011-12-22 16:58:29 +0530479 if (IS_ERR(vblk->vq))
480 err = PTR_ERR(vblk->vq);
481
482 return err;
483}
484
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800485/*
486 * Legacy naming scheme used for virtio devices. We are stuck with it for
487 * virtio blk but don't ever use it for any new driver.
488 */
489static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
490{
491 const int base = 'z' - 'a' + 1;
492 char *begin = buf + strlen(prefix);
493 char *end = buf + buflen;
494 char *p;
495 int unit;
496
497 p = end - 1;
498 *p = '\0';
499 unit = base;
500 do {
501 if (p == begin)
502 return -EINVAL;
503 *--p = 'a' + (index % unit);
504 index = (index / unit) - 1;
505 } while (index >= 0);
506
507 memmove(begin, p, end - p);
508 memcpy(buf, prefix, strlen(prefix));
509
510 return 0;
511}
512
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200513static int virtblk_get_cache_mode(struct virtio_device *vdev)
514{
515 u8 writeback;
516 int err;
517
518 err = virtio_config_val(vdev, VIRTIO_BLK_F_CONFIG_WCE,
519 offsetof(struct virtio_blk_config, wce),
520 &writeback);
521 if (err)
522 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
523
524 return writeback;
525}
526
527static void virtblk_update_cache_mode(struct virtio_device *vdev)
528{
529 u8 writeback = virtblk_get_cache_mode(vdev);
530 struct virtio_blk *vblk = vdev->priv;
531
Asias Hea98755c2012-08-08 16:07:04 +0800532 if (writeback && !use_bio)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200533 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
534 else
535 blk_queue_flush(vblk->disk->queue, 0);
536
537 revalidate_disk(vblk->disk);
538}
539
540static const char *const virtblk_cache_types[] = {
541 "write through", "write back"
542};
543
544static ssize_t
545virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
547{
548 struct gendisk *disk = dev_to_disk(dev);
549 struct virtio_blk *vblk = disk->private_data;
550 struct virtio_device *vdev = vblk->vdev;
551 int i;
552 u8 writeback;
553
554 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
555 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
556 if (sysfs_streq(buf, virtblk_cache_types[i]))
557 break;
558
559 if (i < 0)
560 return -EINVAL;
561
562 writeback = i;
563 vdev->config->set(vdev,
564 offsetof(struct virtio_blk_config, wce),
565 &writeback, sizeof(writeback));
566
567 virtblk_update_cache_mode(vdev);
568 return count;
569}
570
571static ssize_t
572virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
573 char *buf)
574{
575 struct gendisk *disk = dev_to_disk(dev);
576 struct virtio_blk *vblk = disk->private_data;
577 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
578
579 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
580 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
581}
582
583static const struct device_attribute dev_attr_cache_type_ro =
584 __ATTR(cache_type, S_IRUGO,
585 virtblk_cache_type_show, NULL);
586static const struct device_attribute dev_attr_cache_type_rw =
587 __ATTR(cache_type, S_IRUGO|S_IWUSR,
588 virtblk_cache_type_show, virtblk_cache_type_store);
589
Mike Frysinger98e94442009-05-18 03:39:09 -0400590static int __devinit virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000591{
592 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600593 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200594 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800595 int pool_size;
596
Rusty Russelle467cde2007-10-22 11:03:38 +1000597 u64 cap;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600598 u32 v, blk_size, sg_elems, opt_io_size;
599 u16 min_io_size;
600 u8 physical_block_exp, alignment_offset;
Rusty Russelle467cde2007-10-22 11:03:38 +1000601
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200602 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
603 GFP_KERNEL);
604 if (err < 0)
605 goto out;
606 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100607
Rusty Russell0864b792008-12-30 09:26:05 -0600608 /* We need to know how many segments before we allocate. */
609 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
610 offsetof(struct virtio_blk_config, seg_max),
611 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200612
613 /* We need at least one SG element, whatever they say. */
614 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600615 sg_elems = 1;
616
617 /* We need an extra sg elements at head and tail. */
618 sg_elems += 2;
619 vdev->priv = vblk = kmalloc(sizeof(*vblk) +
620 sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000621 if (!vblk) {
622 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200623 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000624 }
625
Asias Hea98755c2012-08-08 16:07:04 +0800626 init_waitqueue_head(&vblk->queue_wait);
Rusty Russelle467cde2007-10-22 11:03:38 +1000627 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600628 vblk->sg_elems = sg_elems;
629 sg_init_table(vblk->sg, vblk->sg_elems);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030630 mutex_init(&vblk->config_lock);
Asias Hea98755c2012-08-08 16:07:04 +0800631
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100632 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030633 vblk->config_enable = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000634
Amit Shah6abd6e52011-12-22 16:58:29 +0530635 err = init_vq(vblk);
636 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000637 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000638
Asias Hea98755c2012-08-08 16:07:04 +0800639 pool_size = sizeof(struct virtblk_req);
640 if (use_bio)
641 pool_size += sizeof(struct scatterlist) * sg_elems;
642 vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000643 if (!vblk->pool) {
644 err = -ENOMEM;
645 goto out_free_vq;
646 }
647
Rusty Russelle467cde2007-10-22 11:03:38 +1000648 /* FIXME: How many partitions? How long is a piece of string? */
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100649 vblk->disk = alloc_disk(1 << PART_BITS);
Rusty Russelle467cde2007-10-22 11:03:38 +1000650 if (!vblk->disk) {
651 err = -ENOMEM;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100652 goto out_mempool;
Rusty Russelle467cde2007-10-22 11:03:38 +1000653 }
654
Asias Hea98755c2012-08-08 16:07:04 +0800655 q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600656 if (!q) {
Rusty Russelle467cde2007-10-22 11:03:38 +1000657 err = -ENOMEM;
658 goto out_put_disk;
659 }
660
Asias Hea98755c2012-08-08 16:07:04 +0800661 if (use_bio)
662 blk_queue_make_request(q, virtblk_make_request);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600663 q->queuedata = vblk;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900664
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800665 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100666
Rusty Russelle467cde2007-10-22 11:03:38 +1000667 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100668 vblk->disk->first_minor = index_to_minor(index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000669 vblk->disk->private_data = vblk;
670 vblk->disk->fops = &virtblk_fops;
Jeremy Katzc4839342008-03-02 17:00:15 -0500671 vblk->disk->driverfs_dev = &vdev->dev;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200672 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100673
Tejun Heo02c42b72010-09-03 11:56:18 +0200674 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200675 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000676
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200677 /* If disk is read-only in the host, the guest should obey */
678 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
679 set_disk_ro(vblk->disk, 1);
680
Rusty Russella586d4f2008-02-04 23:49:56 -0500681 /* Host must always specify the capacity. */
Rusty Russell72e61eb2008-05-02 21:50:49 -0500682 vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
683 &cap, sizeof(cap));
Rusty Russelle467cde2007-10-22 11:03:38 +1000684
685 /* If capacity is too big, truncate with warning. */
686 if ((sector_t)cap != cap) {
687 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
688 (unsigned long long)cap);
689 cap = (sector_t)-1;
690 }
691 set_capacity(vblk->disk, cap);
692
Rusty Russell0864b792008-12-30 09:26:05 -0600693 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500694 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600695
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600696 /* No need to bounce any requests */
Christoph Hellwig69740c82010-02-24 14:22:25 -0600697 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Christoph Hellwig4eff3ca2009-07-17 21:47:45 -0600698
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600699 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500700 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600701
Rusty Russella586d4f2008-02-04 23:49:56 -0500702 /* Host can optionally specify maximum segment size and number of
703 * segments. */
704 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
705 offsetof(struct virtio_blk_config, size_max),
706 &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000707 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600708 blk_queue_max_segment_size(q, v);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600709 else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600710 blk_queue_max_segment_size(q, -1U);
Rusty Russelle467cde2007-10-22 11:03:38 +1000711
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200712 /* Host can optionally specify the block size of the device */
713 err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
714 offsetof(struct virtio_blk_config, blk_size),
715 &blk_size);
716 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600717 blk_queue_logical_block_size(q, blk_size);
718 else
719 blk_size = queue_logical_block_size(q);
720
721 /* Use topology information if available */
722 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
723 offsetof(struct virtio_blk_config, physical_block_exp),
724 &physical_block_exp);
725 if (!err && physical_block_exp)
726 blk_queue_physical_block_size(q,
727 blk_size * (1 << physical_block_exp));
728
729 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
730 offsetof(struct virtio_blk_config, alignment_offset),
731 &alignment_offset);
732 if (!err && alignment_offset)
733 blk_queue_alignment_offset(q, blk_size * alignment_offset);
734
735 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
736 offsetof(struct virtio_blk_config, min_io_size),
737 &min_io_size);
738 if (!err && min_io_size)
739 blk_queue_io_min(q, blk_size * min_io_size);
740
741 err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
742 offsetof(struct virtio_blk_config, opt_io_size),
743 &opt_io_size);
744 if (!err && opt_io_size)
745 blk_queue_io_opt(q, blk_size * opt_io_size);
746
Rusty Russelle467cde2007-10-22 11:03:38 +1000747 add_disk(vblk->disk);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500748 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
749 if (err)
750 goto out_del_disk;
751
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200752 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
753 err = device_create_file(disk_to_dev(vblk->disk),
754 &dev_attr_cache_type_rw);
755 else
756 err = device_create_file(disk_to_dev(vblk->disk),
757 &dev_attr_cache_type_ro);
758 if (err)
759 goto out_del_disk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000760 return 0;
761
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500762out_del_disk:
763 del_gendisk(vblk->disk);
764 blk_cleanup_queue(vblk->disk->queue);
Rusty Russelle467cde2007-10-22 11:03:38 +1000765out_put_disk:
766 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000767out_mempool:
768 mempool_destroy(vblk->pool);
769out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600770 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000771out_free_vblk:
772 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200773out_free_index:
774 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000775out:
776 return err;
777}
778
Mike Frysinger98e94442009-05-18 03:39:09 -0400779static void __devexit virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000780{
781 struct virtio_blk *vblk = vdev->priv;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200782 int index = vblk->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000783
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030784 /* Prevent config work handler from accessing the device. */
785 mutex_lock(&vblk->config_lock);
786 vblk->config_enable = false;
787 mutex_unlock(&vblk->config_lock);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100788
Asias He02e2b122012-05-25 10:34:47 +0800789 del_gendisk(vblk->disk);
Asias He483001c2012-05-25 10:34:48 +0800790 blk_cleanup_queue(vblk->disk->queue);
Asias He02e2b122012-05-25 10:34:47 +0800791
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500792 /* Stop all the virtqueues. */
793 vdev->config->reset(vdev);
794
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +1030795 flush_work(&vblk->config_work);
796
Rusty Russelle467cde2007-10-22 11:03:38 +1000797 put_disk(vblk->disk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000798 mempool_destroy(vblk->pool);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600799 vdev->config->del_vqs(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000800 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200801 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000802}
803
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530804#ifdef CONFIG_PM
805static int virtblk_freeze(struct virtio_device *vdev)
806{
807 struct virtio_blk *vblk = vdev->priv;
808
809 /* Ensure we don't receive any more interrupts */
810 vdev->config->reset(vdev);
811
812 /* Prevent config work handler from accessing the device. */
813 mutex_lock(&vblk->config_lock);
814 vblk->config_enable = false;
815 mutex_unlock(&vblk->config_lock);
816
817 flush_work(&vblk->config_work);
818
819 spin_lock_irq(vblk->disk->queue->queue_lock);
820 blk_stop_queue(vblk->disk->queue);
821 spin_unlock_irq(vblk->disk->queue->queue_lock);
822 blk_sync_queue(vblk->disk->queue);
823
824 vdev->config->del_vqs(vdev);
825 return 0;
826}
827
828static int virtblk_restore(struct virtio_device *vdev)
829{
830 struct virtio_blk *vblk = vdev->priv;
831 int ret;
832
833 vblk->config_enable = true;
834 ret = init_vq(vdev->priv);
835 if (!ret) {
836 spin_lock_irq(vblk->disk->queue->queue_lock);
837 blk_start_queue(vblk->disk->queue);
838 spin_unlock_irq(vblk->disk->queue->queue_lock);
839 }
840 return ret;
841}
842#endif
843
Márton Németh47483e22010-01-10 13:40:02 +0100844static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +1000845 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
846 { 0 },
847};
848
Rusty Russellc45a6812008-05-02 21:50:50 -0500849static unsigned int features[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +0200850 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
851 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200852 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
Rusty Russellc45a6812008-05-02 21:50:50 -0500853};
854
Rakib Mullick4fbfff762009-07-17 20:13:22 +0600855/*
856 * virtio_blk causes spurious section mismatch warning by
857 * simultaneously referring to a __devinit and a __devexit function.
858 * Use __refdata to avoid this warning.
859 */
860static struct virtio_driver __refdata virtio_blk = {
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100861 .feature_table = features,
862 .feature_table_size = ARRAY_SIZE(features),
863 .driver.name = KBUILD_MODNAME,
864 .driver.owner = THIS_MODULE,
865 .id_table = id_table,
866 .probe = virtblk_probe,
867 .remove = __devexit_p(virtblk_remove),
868 .config_changed = virtblk_config_changed,
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530869#ifdef CONFIG_PM
870 .freeze = virtblk_freeze,
871 .restore = virtblk_restore,
872#endif
Rusty Russelle467cde2007-10-22 11:03:38 +1000873};
874
875static int __init init(void)
876{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100877 int error;
878
879 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
880 if (!virtblk_wq)
881 return -ENOMEM;
882
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100883 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100884 if (major < 0) {
885 error = major;
886 goto out_destroy_workqueue;
887 }
888
889 error = register_virtio_driver(&virtio_blk);
890 if (error)
891 goto out_unregister_blkdev;
892 return 0;
893
894out_unregister_blkdev:
895 unregister_blkdev(major, "virtblk");
896out_destroy_workqueue:
897 destroy_workqueue(virtblk_wq);
898 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +1000899}
900
901static void __exit fini(void)
902{
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100903 unregister_blkdev(major, "virtblk");
Rusty Russelle467cde2007-10-22 11:03:38 +1000904 unregister_virtio_driver(&virtio_blk);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100905 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +1000906}
907module_init(init);
908module_exit(fini);
909
910MODULE_DEVICE_TABLE(virtio, id_table);
911MODULE_DESCRIPTION("Virtio block driver");
912MODULE_LICENSE("GPL");