Merge branch 'for-3.16/core' into for-3.16/drivers
[deliverable/linux.git] / drivers / block / virtio_blk.c
CommitLineData
e467cde2
RR
1//#define DEBUG
2#include <linux/spinlock.h>
5a0e3ad6 3#include <linux/slab.h>
e467cde2
RR
4#include <linux/blkdev.h>
5#include <linux/hdreg.h>
0c8d44f2 6#include <linux/module.h>
4678d6f9 7#include <linux/mutex.h>
e467cde2
RR
8#include <linux/virtio.h>
9#include <linux/virtio_blk.h>
3d1266c7 10#include <linux/scatterlist.h>
7a7c924c 11#include <linux/string_helpers.h>
6917f83f 12#include <scsi/scsi_cmnd.h>
5087a50e 13#include <linux/idr.h>
1cf7e9c6
JA
14#include <linux/blk-mq.h>
15#include <linux/numa.h>
3d1266c7 16
4f3bf19c 17#define PART_BITS 4
e467cde2 18
5087a50e
MT
19static int major;
20static DEFINE_IDA(vd_index_ida);
21
2a647bfe 22static struct workqueue_struct *virtblk_wq;
4f3bf19c 23
e467cde2
RR
24struct virtio_blk
25{
e467cde2
RR
26 struct virtio_device *vdev;
27 struct virtqueue *vq;
1cf7e9c6 28 spinlock_t vq_lock;
e467cde2
RR
29
30 /* The disk structure for the kernel. */
31 struct gendisk *disk;
32
24d2f903
CH
33 /* Block layer tags. */
34 struct blk_mq_tag_set tag_set;
35
7a7c924c
CH
36 /* Process context for config space updates */
37 struct work_struct config_work;
38
4678d6f9
MT
39 /* Lock for config space updates */
40 struct mutex config_lock;
41
42 /* enable config space updates */
43 bool config_enable;
44
0864b79a
RR
45 /* What host tells us, plus 2 for header & tailer. */
46 unsigned int sg_elems;
47
5087a50e
MT
48 /* Ida index - used to track minor number allocations. */
49 int index;
e467cde2
RR
50};
51
52struct virtblk_req
53{
e467cde2
RR
54 struct request *req;
55 struct virtio_blk_outhdr out_hdr;
1cde26f9 56 struct virtio_scsi_inhdr in_hdr;
cb38fa23 57 u8 status;
a98755c5 58 struct scatterlist sg[];
e467cde2
RR
59};
60
a98755c5
AH
61static inline int virtblk_result(struct virtblk_req *vbr)
62{
63 switch (vbr->status) {
64 case VIRTIO_BLK_S_OK:
65 return 0;
66 case VIRTIO_BLK_S_UNSUPP:
67 return -ENOTTY;
68 default:
69 return -EIO;
70 }
71}
72
8f39db9d 73static int __virtblk_add_req(struct virtqueue *vq,
20af3cfd
PB
74 struct virtblk_req *vbr,
75 struct scatterlist *data_sg,
0a11cc36 76 bool have_data)
c85a1f91 77{
20af3cfd 78 struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
8f39db9d 79 unsigned int num_out = 0, num_in = 0;
20af3cfd 80 int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT;
8f39db9d
PB
81
82 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
83 sgs[num_out++] = &hdr;
84
20af3cfd
PB
85 /*
86 * If this is a packet command we need a couple of additional headers.
87 * Behind the normal outhdr we put a segment with the scsi command
88 * block, and before the normal inhdr we put the sense data and the
89 * inhdr with additional status information.
90 */
91 if (type == VIRTIO_BLK_T_SCSI_CMD) {
92 sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
93 sgs[num_out++] = &cmd;
94 }
95
0a11cc36 96 if (have_data) {
8f39db9d 97 if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT)
20af3cfd 98 sgs[num_out++] = data_sg;
8f39db9d 99 else
20af3cfd
PB
100 sgs[num_out + num_in++] = data_sg;
101 }
102
103 if (type == VIRTIO_BLK_T_SCSI_CMD) {
104 sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
105 sgs[num_out + num_in++] = &sense;
106 sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
107 sgs[num_out + num_in++] = &inhdr;
8f39db9d
PB
108 }
109
110 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
111 sgs[num_out + num_in++] = &status;
112
113 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
5ee21a52
PB
114}
115
5124c285 116static inline void virtblk_request_done(struct request *req)
a98755c5 117{
9d74e257 118 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
a98755c5
AH
119 int error = virtblk_result(vbr);
120
121 if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
122 req->resid_len = vbr->in_hdr.residual;
123 req->sense_len = vbr->in_hdr.sense_len;
124 req->errors = vbr->in_hdr.errors;
125 } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
126 req->errors = (error != 0);
127 }
128
1cf7e9c6 129 blk_mq_end_io(req, error);
a98755c5
AH
130}
131
132static void virtblk_done(struct virtqueue *vq)
e467cde2
RR
133{
134 struct virtio_blk *vblk = vq->vdev->priv;
1cf7e9c6 135 bool req_done = false;
e467cde2 136 struct virtblk_req *vbr;
e467cde2 137 unsigned long flags;
a98755c5 138 unsigned int len;
e467cde2 139
1cf7e9c6 140 spin_lock_irqsave(&vblk->vq_lock, flags);
bb811108
AH
141 do {
142 virtqueue_disable_cb(vq);
143 while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
5124c285 144 blk_mq_complete_request(vbr->req);
1cf7e9c6 145 req_done = true;
33659ebb 146 }
7f03b17d
HG
147 if (unlikely(virtqueue_is_broken(vq)))
148 break;
bb811108 149 } while (!virtqueue_enable_cb(vq));
1cf7e9c6 150
e467cde2 151 /* In case queue is stopped waiting for more buffers. */
a98755c5 152 if (req_done)
1b4a3258 153 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
0c29e93e 154 spin_unlock_irqrestore(&vblk->vq_lock, flags);
a98755c5
AH
155}
156
1cf7e9c6 157static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req)
e467cde2 158{
1cf7e9c6 159 struct virtio_blk *vblk = hctx->queue->queuedata;
9d74e257 160 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
1cf7e9c6 161 unsigned long flags;
20af3cfd 162 unsigned int num;
1cf7e9c6 163 const bool last = (req->cmd_flags & REQ_END) != 0;
5261b85e 164 int err;
e467cde2 165
1cf7e9c6 166 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
e467cde2
RR
167
168 vbr->req = req;
dd40e456
FT
169 if (req->cmd_flags & REQ_FLUSH) {
170 vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
4cb2ea28 171 vbr->out_hdr.sector = 0;
172 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
dd40e456
FT
173 } else {
174 switch (req->cmd_type) {
175 case REQ_TYPE_FS:
176 vbr->out_hdr.type = 0;
177 vbr->out_hdr.sector = blk_rq_pos(vbr->req);
178 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
179 break;
180 case REQ_TYPE_BLOCK_PC:
181 vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
f1b0ef06
CH
182 vbr->out_hdr.sector = 0;
183 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
184 break;
dd40e456
FT
185 case REQ_TYPE_SPECIAL:
186 vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
187 vbr->out_hdr.sector = 0;
188 vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
189 break;
190 default:
191 /* We don't put anything else in the queue. */
192 BUG();
f1b0ef06 193 }
e467cde2
RR
194 }
195
1cf7e9c6 196 num = blk_rq_map_sg(hctx->queue, vbr->req, vbr->sg);
1cde26f9 197 if (num) {
20af3cfd 198 if (rq_data_dir(vbr->req) == WRITE)
1cde26f9 199 vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
20af3cfd 200 else
1cde26f9 201 vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
e467cde2
RR
202 }
203
1cf7e9c6 204 spin_lock_irqsave(&vblk->vq_lock, flags);
5261b85e
RR
205 err = __virtblk_add_req(vblk->vq, vbr, vbr->sg, num);
206 if (err) {
f02b9ac3 207 virtqueue_kick(vblk->vq);
1cf7e9c6 208 blk_mq_stop_hw_queue(hctx);
0c29e93e 209 spin_unlock_irqrestore(&vblk->vq_lock, flags);
5261b85e
RR
210 /* Out of mem doesn't actually happen, since we fall back
211 * to direct descriptors */
212 if (err == -ENOMEM || err == -ENOSPC)
213 return BLK_MQ_RQ_QUEUE_BUSY;
214 return BLK_MQ_RQ_QUEUE_ERROR;
a98755c5
AH
215 }
216
1cf7e9c6
JA
217 if (last)
218 virtqueue_kick(vblk->vq);
f02b9ac3
SL
219
220 spin_unlock_irqrestore(&vblk->vq_lock, flags);
1cf7e9c6 221 return BLK_MQ_RQ_QUEUE_OK;
a98755c5
AH
222}
223
4cb2ea28 224/* return id (s/n) string for *disk to *id_str
225 */
226static int virtblk_get_id(struct gendisk *disk, char *id_str)
227{
228 struct virtio_blk *vblk = disk->private_data;
229 struct request *req;
230 struct bio *bio;
e4c4776d 231 int err;
4cb2ea28 232
233 bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
234 GFP_KERNEL);
235 if (IS_ERR(bio))
236 return PTR_ERR(bio);
237
238 req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
239 if (IS_ERR(req)) {
240 bio_put(bio);
241 return PTR_ERR(req);
242 }
243
244 req->cmd_type = REQ_TYPE_SPECIAL;
e4c4776d
MS
245 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
246 blk_put_request(req);
247
248 return err;
4cb2ea28 249}
250
fe5a50a1
CH
251static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
252 unsigned int cmd, unsigned long data)
e467cde2 253{
1cde26f9
HR
254 struct gendisk *disk = bdev->bd_disk;
255 struct virtio_blk *vblk = disk->private_data;
256
257 /*
258 * Only allow the generic SCSI ioctls if the host can support it.
259 */
260 if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
d9ecdea7 261 return -ENOTTY;
1cde26f9 262
577ebb37
PB
263 return scsi_cmd_blk_ioctl(bdev, mode, cmd,
264 (void __user *)data);
e467cde2
RR
265}
266
135da0b0
CB
267/* We provide getgeo only to please some old bootloader/partitioning tools */
268static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
269{
48e4043d 270 struct virtio_blk *vblk = bd->bd_disk->private_data;
48e4043d
RH
271
272 /* see if the host passed in geometry config */
855e0c52
RR
273 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
274 virtio_cread(vblk->vdev, struct virtio_blk_config,
275 geometry.cylinders, &geo->cylinders);
276 virtio_cread(vblk->vdev, struct virtio_blk_config,
277 geometry.heads, &geo->heads);
278 virtio_cread(vblk->vdev, struct virtio_blk_config,
279 geometry.sectors, &geo->sectors);
48e4043d
RH
280 } else {
281 /* some standard values, similar to sd */
282 geo->heads = 1 << 6;
283 geo->sectors = 1 << 5;
284 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
285 }
135da0b0
CB
286 return 0;
287}
288
83d5cde4 289static const struct block_device_operations virtblk_fops = {
8a6cfeb6 290 .ioctl = virtblk_ioctl,
135da0b0
CB
291 .owner = THIS_MODULE,
292 .getgeo = virtblk_getgeo,
e467cde2
RR
293};
294
d50ed907
CB
295static int index_to_minor(int index)
296{
297 return index << PART_BITS;
298}
299
5087a50e
MT
300static int minor_to_index(int minor)
301{
302 return minor >> PART_BITS;
303}
304
a5eb9e4f
RH
305static ssize_t virtblk_serial_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct gendisk *disk = dev_to_disk(dev);
309 int err;
310
311 /* sysfs gives us a PAGE_SIZE buffer */
312 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
313
314 buf[VIRTIO_BLK_ID_BYTES] = '\0';
315 err = virtblk_get_id(disk, buf);
316 if (!err)
317 return strlen(buf);
318
319 if (err == -EIO) /* Unsupported? Make it empty. */
320 return 0;
321
322 return err;
323}
324DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
325
7a7c924c
CH
326static void virtblk_config_changed_work(struct work_struct *work)
327{
328 struct virtio_blk *vblk =
329 container_of(work, struct virtio_blk, config_work);
330 struct virtio_device *vdev = vblk->vdev;
331 struct request_queue *q = vblk->disk->queue;
332 char cap_str_2[10], cap_str_10[10];
9d9598b8 333 char *envp[] = { "RESIZE=1", NULL };
7a7c924c
CH
334 u64 capacity, size;
335
4678d6f9
MT
336 mutex_lock(&vblk->config_lock);
337 if (!vblk->config_enable)
338 goto done;
339
7a7c924c 340 /* Host must always specify the capacity. */
855e0c52 341 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
7a7c924c
CH
342
343 /* If capacity is too big, truncate with warning. */
344 if ((sector_t)capacity != capacity) {
345 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
346 (unsigned long long)capacity);
347 capacity = (sector_t)-1;
348 }
349
350 size = capacity * queue_logical_block_size(q);
351 string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
352 string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
353
354 dev_notice(&vdev->dev,
355 "new size: %llu %d-byte logical blocks (%s/%s)\n",
356 (unsigned long long)capacity,
357 queue_logical_block_size(q),
358 cap_str_10, cap_str_2);
359
360 set_capacity(vblk->disk, capacity);
e9986f30 361 revalidate_disk(vblk->disk);
9d9598b8 362 kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
4678d6f9
MT
363done:
364 mutex_unlock(&vblk->config_lock);
7a7c924c
CH
365}
366
367static void virtblk_config_changed(struct virtio_device *vdev)
368{
369 struct virtio_blk *vblk = vdev->priv;
370
371 queue_work(virtblk_wq, &vblk->config_work);
372}
373
6abd6e5a
AS
374static int init_vq(struct virtio_blk *vblk)
375{
376 int err = 0;
377
378 /* We expect one virtqueue, for output. */
a98755c5 379 vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
6abd6e5a
AS
380 if (IS_ERR(vblk->vq))
381 err = PTR_ERR(vblk->vq);
382
383 return err;
384}
385
c0aa3e09
RM
386/*
387 * Legacy naming scheme used for virtio devices. We are stuck with it for
388 * virtio blk but don't ever use it for any new driver.
389 */
390static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
391{
392 const int base = 'z' - 'a' + 1;
393 char *begin = buf + strlen(prefix);
394 char *end = buf + buflen;
395 char *p;
396 int unit;
397
398 p = end - 1;
399 *p = '\0';
400 unit = base;
401 do {
402 if (p == begin)
403 return -EINVAL;
404 *--p = 'a' + (index % unit);
405 index = (index / unit) - 1;
406 } while (index >= 0);
407
408 memmove(begin, p, end - p);
409 memcpy(buf, prefix, strlen(prefix));
410
411 return 0;
412}
413
cd5d5038
PB
414static int virtblk_get_cache_mode(struct virtio_device *vdev)
415{
416 u8 writeback;
417 int err;
418
855e0c52
RR
419 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
420 struct virtio_blk_config, wce,
421 &writeback);
cd5d5038
PB
422 if (err)
423 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
424
425 return writeback;
426}
427
428static void virtblk_update_cache_mode(struct virtio_device *vdev)
429{
430 u8 writeback = virtblk_get_cache_mode(vdev);
431 struct virtio_blk *vblk = vdev->priv;
432
c85a1f91 433 if (writeback)
cd5d5038
PB
434 blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
435 else
436 blk_queue_flush(vblk->disk->queue, 0);
437
438 revalidate_disk(vblk->disk);
439}
440
441static const char *const virtblk_cache_types[] = {
442 "write through", "write back"
443};
444
445static ssize_t
446virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
447 const char *buf, size_t count)
448{
449 struct gendisk *disk = dev_to_disk(dev);
450 struct virtio_blk *vblk = disk->private_data;
451 struct virtio_device *vdev = vblk->vdev;
452 int i;
cd5d5038
PB
453
454 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
455 for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
456 if (sysfs_streq(buf, virtblk_cache_types[i]))
457 break;
458
459 if (i < 0)
460 return -EINVAL;
461
855e0c52 462 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
cd5d5038
PB
463 virtblk_update_cache_mode(vdev);
464 return count;
465}
466
467static ssize_t
468virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
469 char *buf)
470{
471 struct gendisk *disk = dev_to_disk(dev);
472 struct virtio_blk *vblk = disk->private_data;
473 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
474
475 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
476 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
477}
478
479static const struct device_attribute dev_attr_cache_type_ro =
480 __ATTR(cache_type, S_IRUGO,
481 virtblk_cache_type_show, NULL);
482static const struct device_attribute dev_attr_cache_type_rw =
483 __ATTR(cache_type, S_IRUGO|S_IWUSR,
484 virtblk_cache_type_show, virtblk_cache_type_store);
485
24d2f903
CH
486static int virtblk_init_request(void *data, struct request *rq,
487 unsigned int hctx_idx, unsigned int request_idx,
488 unsigned int numa_node)
e9b267d9
CH
489{
490 struct virtio_blk *vblk = data;
491 struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
492
493 sg_init_table(vbr->sg, vblk->sg_elems);
494 return 0;
495}
496
1cf7e9c6
JA
497static struct blk_mq_ops virtio_mq_ops = {
498 .queue_rq = virtio_queue_rq,
499 .map_queue = blk_mq_map_queue,
5124c285 500 .complete = virtblk_request_done,
24d2f903 501 .init_request = virtblk_init_request,
1cf7e9c6
JA
502};
503
24d2f903
CH
504static unsigned int virtblk_queue_depth;
505module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
1cf7e9c6 506
8d85fce7 507static int virtblk_probe(struct virtio_device *vdev)
e467cde2
RR
508{
509 struct virtio_blk *vblk;
69740c8b 510 struct request_queue *q;
5087a50e 511 int err, index;
a98755c5 512
e467cde2 513 u64 cap;
69740c8b
CH
514 u32 v, blk_size, sg_elems, opt_io_size;
515 u16 min_io_size;
516 u8 physical_block_exp, alignment_offset;
e467cde2 517
5087a50e
MT
518 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
519 GFP_KERNEL);
520 if (err < 0)
521 goto out;
522 index = err;
4f3bf19c 523
0864b79a 524 /* We need to know how many segments before we allocate. */
855e0c52
RR
525 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
526 struct virtio_blk_config, seg_max,
527 &sg_elems);
a5b365a6
CH
528
529 /* We need at least one SG element, whatever they say. */
530 if (err || !sg_elems)
0864b79a
RR
531 sg_elems = 1;
532
533 /* We need an extra sg elements at head and tail. */
534 sg_elems += 2;
1cf7e9c6 535 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
e467cde2
RR
536 if (!vblk) {
537 err = -ENOMEM;
5087a50e 538 goto out_free_index;
e467cde2
RR
539 }
540
e467cde2 541 vblk->vdev = vdev;
0864b79a 542 vblk->sg_elems = sg_elems;
4678d6f9 543 mutex_init(&vblk->config_lock);
a98755c5 544
7a7c924c 545 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
4678d6f9 546 vblk->config_enable = true;
e467cde2 547
6abd6e5a
AS
548 err = init_vq(vblk);
549 if (err)
e467cde2 550 goto out_free_vblk;
1cf7e9c6 551 spin_lock_init(&vblk->vq_lock);
e467cde2 552
e467cde2 553 /* FIXME: How many partitions? How long is a piece of string? */
4f3bf19c 554 vblk->disk = alloc_disk(1 << PART_BITS);
e467cde2
RR
555 if (!vblk->disk) {
556 err = -ENOMEM;
1cf7e9c6 557 goto out_free_vq;
e467cde2
RR
558 }
559
fc4324b4 560 /* Default queue sizing is to fill the ring. */
24d2f903
CH
561 if (!virtblk_queue_depth) {
562 virtblk_queue_depth = vblk->vq->num_free;
fc4324b4
RR
563 /* ... but without indirect descs, we use 2 descs per req */
564 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
24d2f903 565 virtblk_queue_depth /= 2;
fc4324b4 566 }
24d2f903
CH
567
568 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
569 vblk->tag_set.ops = &virtio_mq_ops;
570 vblk->tag_set.nr_hw_queues = 1;
571 vblk->tag_set.queue_depth = virtblk_queue_depth;
572 vblk->tag_set.numa_node = NUMA_NO_NODE;
573 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
574 vblk->tag_set.cmd_size =
1cf7e9c6
JA
575 sizeof(struct virtblk_req) +
576 sizeof(struct scatterlist) * sg_elems;
24d2f903 577 vblk->tag_set.driver_data = vblk;
1cf7e9c6 578
24d2f903
CH
579 err = blk_mq_alloc_tag_set(&vblk->tag_set);
580 if (err)
581 goto out_put_disk;
582
583 q = vblk->disk->queue = blk_mq_init_queue(&vblk->tag_set);
69740c8b 584 if (!q) {
e467cde2 585 err = -ENOMEM;
24d2f903 586 goto out_free_tags;
e467cde2
RR
587 }
588
69740c8b 589 q->queuedata = vblk;
7d116b62 590
c0aa3e09 591 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
d50ed907 592
e467cde2 593 vblk->disk->major = major;
d50ed907 594 vblk->disk->first_minor = index_to_minor(index);
e467cde2
RR
595 vblk->disk->private_data = vblk;
596 vblk->disk->fops = &virtblk_fops;
c4839346 597 vblk->disk->driverfs_dev = &vdev->dev;
5087a50e 598 vblk->index = index;
4f3bf19c 599
02c42b7a 600 /* configure queue flush support */
cd5d5038 601 virtblk_update_cache_mode(vdev);
e467cde2 602
3ef53609
CB
603 /* If disk is read-only in the host, the guest should obey */
604 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
605 set_disk_ro(vblk->disk, 1);
606
a586d4f6 607 /* Host must always specify the capacity. */
855e0c52 608 virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
e467cde2
RR
609
610 /* If capacity is too big, truncate with warning. */
611 if ((sector_t)cap != cap) {
612 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
613 (unsigned long long)cap);
614 cap = (sector_t)-1;
615 }
616 set_capacity(vblk->disk, cap);
617
0864b79a 618 /* We can handle whatever the host told us to handle. */
ee714f2d 619 blk_queue_max_segments(q, vblk->sg_elems-2);
0864b79a 620
4eff3cae 621 /* No need to bounce any requests */
69740c8b 622 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
4eff3cae 623
4b7f7e20 624 /* No real sector limit. */
ee714f2d 625 blk_queue_max_hw_sectors(q, -1U);
4b7f7e20 626
a586d4f6
RR
627 /* Host can optionally specify maximum segment size and number of
628 * segments. */
855e0c52
RR
629 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
630 struct virtio_blk_config, size_max, &v);
e467cde2 631 if (!err)
69740c8b 632 blk_queue_max_segment_size(q, v);
4b7f7e20 633 else
69740c8b 634 blk_queue_max_segment_size(q, -1U);
e467cde2 635
066f4d82 636 /* Host can optionally specify the block size of the device */
855e0c52
RR
637 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
638 struct virtio_blk_config, blk_size,
639 &blk_size);
066f4d82 640 if (!err)
69740c8b
CH
641 blk_queue_logical_block_size(q, blk_size);
642 else
643 blk_size = queue_logical_block_size(q);
644
645 /* Use topology information if available */
855e0c52
RR
646 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
647 struct virtio_blk_config, physical_block_exp,
648 &physical_block_exp);
69740c8b
CH
649 if (!err && physical_block_exp)
650 blk_queue_physical_block_size(q,
651 blk_size * (1 << physical_block_exp));
652
855e0c52
RR
653 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
654 struct virtio_blk_config, alignment_offset,
655 &alignment_offset);
69740c8b
CH
656 if (!err && alignment_offset)
657 blk_queue_alignment_offset(q, blk_size * alignment_offset);
658
855e0c52
RR
659 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
660 struct virtio_blk_config, min_io_size,
661 &min_io_size);
69740c8b
CH
662 if (!err && min_io_size)
663 blk_queue_io_min(q, blk_size * min_io_size);
664
855e0c52
RR
665 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
666 struct virtio_blk_config, opt_io_size,
667 &opt_io_size);
69740c8b
CH
668 if (!err && opt_io_size)
669 blk_queue_io_opt(q, blk_size * opt_io_size);
670
e467cde2 671 add_disk(vblk->disk);
a5eb9e4f
RH
672 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
673 if (err)
674 goto out_del_disk;
675
cd5d5038
PB
676 if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
677 err = device_create_file(disk_to_dev(vblk->disk),
678 &dev_attr_cache_type_rw);
679 else
680 err = device_create_file(disk_to_dev(vblk->disk),
681 &dev_attr_cache_type_ro);
682 if (err)
683 goto out_del_disk;
e467cde2
RR
684 return 0;
685
a5eb9e4f
RH
686out_del_disk:
687 del_gendisk(vblk->disk);
688 blk_cleanup_queue(vblk->disk->queue);
24d2f903
CH
689out_free_tags:
690 blk_mq_free_tag_set(&vblk->tag_set);
e467cde2
RR
691out_put_disk:
692 put_disk(vblk->disk);
e467cde2 693out_free_vq:
d2a7ddda 694 vdev->config->del_vqs(vdev);
e467cde2
RR
695out_free_vblk:
696 kfree(vblk);
5087a50e
MT
697out_free_index:
698 ida_simple_remove(&vd_index_ida, index);
e467cde2
RR
699out:
700 return err;
701}
702
8d85fce7 703static void virtblk_remove(struct virtio_device *vdev)
e467cde2
RR
704{
705 struct virtio_blk *vblk = vdev->priv;
5087a50e 706 int index = vblk->index;
f4953fe6 707 int refc;
e467cde2 708
4678d6f9
MT
709 /* Prevent config work handler from accessing the device. */
710 mutex_lock(&vblk->config_lock);
711 vblk->config_enable = false;
712 mutex_unlock(&vblk->config_lock);
7a7c924c 713
02e2b124 714 del_gendisk(vblk->disk);
483001c7 715 blk_cleanup_queue(vblk->disk->queue);
02e2b124 716
24d2f903
CH
717 blk_mq_free_tag_set(&vblk->tag_set);
718
6e5aa7ef
RR
719 /* Stop all the virtqueues. */
720 vdev->config->reset(vdev);
721
4678d6f9
MT
722 flush_work(&vblk->config_work);
723
f4953fe6 724 refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
e467cde2 725 put_disk(vblk->disk);
d2a7ddda 726 vdev->config->del_vqs(vdev);
e467cde2 727 kfree(vblk);
f4953fe6
AG
728
729 /* Only free device id if we don't have any users */
730 if (refc == 1)
731 ida_simple_remove(&vd_index_ida, index);
e467cde2
RR
732}
733
89107000 734#ifdef CONFIG_PM_SLEEP
f8fb5bc2
AS
735static int virtblk_freeze(struct virtio_device *vdev)
736{
737 struct virtio_blk *vblk = vdev->priv;
738
739 /* Ensure we don't receive any more interrupts */
740 vdev->config->reset(vdev);
741
742 /* Prevent config work handler from accessing the device. */
743 mutex_lock(&vblk->config_lock);
744 vblk->config_enable = false;
745 mutex_unlock(&vblk->config_lock);
746
747 flush_work(&vblk->config_work);
748
1cf7e9c6 749 blk_mq_stop_hw_queues(vblk->disk->queue);
f8fb5bc2
AS
750
751 vdev->config->del_vqs(vdev);
752 return 0;
753}
754
755static int virtblk_restore(struct virtio_device *vdev)
756{
757 struct virtio_blk *vblk = vdev->priv;
758 int ret;
759
760 vblk->config_enable = true;
761 ret = init_vq(vdev->priv);
1cf7e9c6 762 if (!ret)
1b4a3258 763 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
1cf7e9c6 764
f8fb5bc2
AS
765 return ret;
766}
767#endif
768
47483e25 769static const struct virtio_device_id id_table[] = {
e467cde2
RR
770 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
771 { 0 },
772};
773
c45a6816 774static unsigned int features[] = {
02c42b7a
TH
775 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
776 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
cd5d5038 777 VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
c45a6816
RR
778};
779
8d85fce7 780static struct virtio_driver virtio_blk = {
7a7c924c
CH
781 .feature_table = features,
782 .feature_table_size = ARRAY_SIZE(features),
783 .driver.name = KBUILD_MODNAME,
784 .driver.owner = THIS_MODULE,
785 .id_table = id_table,
786 .probe = virtblk_probe,
8d85fce7 787 .remove = virtblk_remove,
7a7c924c 788 .config_changed = virtblk_config_changed,
89107000 789#ifdef CONFIG_PM_SLEEP
f8fb5bc2
AS
790 .freeze = virtblk_freeze,
791 .restore = virtblk_restore,
792#endif
e467cde2
RR
793};
794
795static int __init init(void)
796{
7a7c924c
CH
797 int error;
798
799 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
800 if (!virtblk_wq)
801 return -ENOMEM;
802
4f3bf19c 803 major = register_blkdev(0, "virtblk");
7a7c924c
CH
804 if (major < 0) {
805 error = major;
806 goto out_destroy_workqueue;
807 }
808
809 error = register_virtio_driver(&virtio_blk);
810 if (error)
811 goto out_unregister_blkdev;
812 return 0;
813
814out_unregister_blkdev:
815 unregister_blkdev(major, "virtblk");
816out_destroy_workqueue:
817 destroy_workqueue(virtblk_wq);
818 return error;
e467cde2
RR
819}
820
821static void __exit fini(void)
822{
4f3bf19c 823 unregister_blkdev(major, "virtblk");
e467cde2 824 unregister_virtio_driver(&virtio_blk);
7a7c924c 825 destroy_workqueue(virtblk_wq);
e467cde2
RR
826}
827module_init(init);
828module_exit(fini);
829
830MODULE_DEVICE_TABLE(virtio, id_table);
831MODULE_DESCRIPTION("Virtio block driver");
832MODULE_LICENSE("GPL");
This page took 0.779209 seconds and 5 git commands to generate.