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