]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/block/virtio_blk.c
ide: don't abuse cmd_type
[mirror_ubuntu-bionic-kernel.git] / drivers / block / virtio_blk.c
1 //#define DEBUG
2 #include <linux/spinlock.h>
3 #include <linux/slab.h>
4 #include <linux/blkdev.h>
5 #include <linux/hdreg.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/virtio.h>
9 #include <linux/virtio_blk.h>
10 #include <linux/scatterlist.h>
11 #include <linux/string_helpers.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <linux/idr.h>
14 #include <linux/blk-mq.h>
15 #include <linux/numa.h>
16
17 #define PART_BITS 4
18 #define VQ_NAME_LEN 16
19
20 static int major;
21 static DEFINE_IDA(vd_index_ida);
22
23 static struct workqueue_struct *virtblk_wq;
24
25 struct virtio_blk_vq {
26 struct virtqueue *vq;
27 spinlock_t lock;
28 char name[VQ_NAME_LEN];
29 } ____cacheline_aligned_in_smp;
30
31 struct virtio_blk {
32 struct virtio_device *vdev;
33
34 /* The disk structure for the kernel. */
35 struct gendisk *disk;
36
37 /* Block layer tags. */
38 struct blk_mq_tag_set tag_set;
39
40 /* Process context for config space updates */
41 struct work_struct config_work;
42
43 /* What host tells us, plus 2 for header & tailer. */
44 unsigned int sg_elems;
45
46 /* Ida index - used to track minor number allocations. */
47 int index;
48
49 /* num of vqs */
50 int num_vqs;
51 struct virtio_blk_vq *vqs;
52 };
53
54 struct virtblk_req {
55 #ifdef CONFIG_VIRTIO_BLK_SCSI
56 struct scsi_request sreq; /* for SCSI passthrough, must be first */
57 u8 sense[SCSI_SENSE_BUFFERSIZE];
58 struct virtio_scsi_inhdr in_hdr;
59 #endif
60 struct virtio_blk_outhdr out_hdr;
61 u8 status;
62 struct scatterlist sg[];
63 };
64
65 static 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
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
84 static int virtblk_add_req_scsi(struct virtqueue *vq, struct virtblk_req *vbr,
85 struct scatterlist *data_sg, bool have_data)
86 {
87 struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
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;
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
112 static 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
123 static 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;
128
129 /*
130 * Only allow the generic SCSI ioctls if the host can support it.
131 */
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
139 static 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 }
145 static inline void virtblk_scsi_reques_done(struct request *req)
146 {
147 }
148 #define virtblk_ioctl NULL
149 #endif /* CONFIG_VIRTIO_BLK_SCSI */
150
151 static 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;
159
160 if (have_data) {
161 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
162 sgs[num_out++] = data_sg;
163 else
164 sgs[num_out + num_in++] = data_sg;
165 }
166
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);
171 }
172
173 static inline void virtblk_request_done(struct request *req)
174 {
175 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
176 int error = virtblk_result(vbr);
177
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:
183 req->errors = (error != 0);
184 break;
185 }
186
187 blk_mq_end_request(req, error);
188 }
189
190 static void virtblk_done(struct virtqueue *vq)
191 {
192 struct virtio_blk *vblk = vq->vdev->priv;
193 bool req_done = false;
194 int qid = vq->index;
195 struct virtblk_req *vbr;
196 unsigned long flags;
197 unsigned int len;
198
199 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
200 do {
201 virtqueue_disable_cb(vq);
202 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
203 struct request *req = blk_mq_rq_from_pdu(vbr);
204
205 blk_mq_complete_request(req, req->errors);
206 req_done = true;
207 }
208 if (unlikely(virtqueue_is_broken(vq)))
209 break;
210 } while (!virtqueue_enable_cb(vq));
211
212 /* In case queue is stopped waiting for more buffers. */
213 if (req_done)
214 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
215 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
216 }
217
218 static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
219 const struct blk_mq_queue_data *bd)
220 {
221 struct virtio_blk *vblk = hctx->queue->queuedata;
222 struct request *req = bd->rq;
223 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
224 unsigned long flags;
225 unsigned int num;
226 int qid = hctx->queue_num;
227 int err;
228 bool notify = false;
229
230 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
231
232 if (req_op(req) == REQ_OP_FLUSH) {
233 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_FLUSH);
234 vbr->out_hdr.sector = 0;
235 vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
236 } else {
237 switch (req->cmd_type) {
238 case REQ_TYPE_FS:
239 vbr->out_hdr.type = 0;
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));
242 break;
243 case REQ_TYPE_BLOCK_PC:
244 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_SCSI_CMD);
245 vbr->out_hdr.sector = 0;
246 vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
247 break;
248 case REQ_TYPE_DRV_PRIV:
249 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
250 vbr->out_hdr.sector = 0;
251 vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
252 break;
253 default:
254 /* We don't put anything else in the queue. */
255 BUG();
256 }
257 }
258
259 blk_mq_start_request(req);
260
261 num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
262 if (num) {
263 if (rq_data_dir(req) == WRITE)
264 vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
265 else
266 vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
267 }
268
269 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
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);
274 if (err) {
275 virtqueue_kick(vblk->vqs[qid].vq);
276 blk_mq_stop_hw_queue(hctx);
277 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
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;
283 }
284
285 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
286 notify = true;
287 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
288
289 if (notify)
290 virtqueue_notify(vblk->vqs[qid].vq);
291 return BLK_MQ_RQ_QUEUE_OK;
292 }
293
294 /* return id (s/n) string for *disk to *id_str
295 */
296 static int virtblk_get_id(struct gendisk *disk, char *id_str)
297 {
298 struct virtio_blk *vblk = disk->private_data;
299 struct request_queue *q = vblk->disk->queue;
300 struct request *req;
301 int err;
302
303 req = blk_get_request(q, READ, GFP_KERNEL);
304 if (IS_ERR(req))
305 return PTR_ERR(req);
306 req->cmd_type = REQ_TYPE_DRV_PRIV;
307
308 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
309 if (err)
310 goto out;
311
312 err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
313 out:
314 blk_put_request(req);
315 return err;
316 }
317
318 /* We provide getgeo only to please some old bootloader/partitioning tools */
319 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
320 {
321 struct virtio_blk *vblk = bd->bd_disk->private_data;
322
323 /* see if the host passed in geometry config */
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);
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 }
337 return 0;
338 }
339
340 static const struct block_device_operations virtblk_fops = {
341 .ioctl = virtblk_ioctl,
342 .owner = THIS_MODULE,
343 .getgeo = virtblk_getgeo,
344 };
345
346 static int index_to_minor(int index)
347 {
348 return index << PART_BITS;
349 }
350
351 static int minor_to_index(int minor)
352 {
353 return minor >> PART_BITS;
354 }
355
356 static 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 }
375
376 static DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
377
378 static 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];
385 char *envp[] = { "RESIZE=1", NULL };
386 u64 capacity;
387
388 /* Host must always specify the capacity. */
389 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
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
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));
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);
410 revalidate_disk(vblk->disk);
411 kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
412 }
413
414 static 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
421 static int init_vq(struct virtio_blk *vblk)
422 {
423 int err;
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
437 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
438 if (!vblk->vqs)
439 return -ENOMEM;
440
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);
444 if (!names || !callbacks || !vqs) {
445 err = -ENOMEM;
446 goto out;
447 }
448
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)
458 goto out;
459
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
466 out:
467 kfree(vqs);
468 kfree(callbacks);
469 kfree(names);
470 if (err)
471 kfree(vblk->vqs);
472 return err;
473 }
474
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 */
479 static 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
503 static int virtblk_get_cache_mode(struct virtio_device *vdev)
504 {
505 u8 writeback;
506 int err;
507
508 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
509 struct virtio_blk_config, wce,
510 &writeback);
511
512 /*
513 * If WCE is not configurable and flush is not available,
514 * assume no writeback cache is in use.
515 */
516 if (err)
517 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
518
519 return writeback;
520 }
521
522 static 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
527 blk_queue_write_cache(vblk->disk->queue, writeback, false);
528 revalidate_disk(vblk->disk);
529 }
530
531 static const char *const virtblk_cache_types[] = {
532 "write through", "write back"
533 };
534
535 static ssize_t
536 virtblk_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;
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
552 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
553 virtblk_update_cache_mode(vdev);
554 return count;
555 }
556
557 static ssize_t
558 virtblk_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
569 static const struct device_attribute dev_attr_cache_type_ro =
570 __ATTR(cache_type, S_IRUGO,
571 virtblk_cache_type_show, NULL);
572 static 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
576 static int virtblk_init_request(void *data, struct request *rq,
577 unsigned int hctx_idx, unsigned int request_idx,
578 unsigned int numa_node)
579 {
580 struct virtio_blk *vblk = data;
581 struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
582
583 #ifdef CONFIG_VIRTIO_BLK_SCSI
584 vbr->sreq.sense = vbr->sense;
585 #endif
586 sg_init_table(vbr->sg, vblk->sg_elems);
587 return 0;
588 }
589
590 static struct blk_mq_ops virtio_mq_ops = {
591 .queue_rq = virtio_queue_rq,
592 .complete = virtblk_request_done,
593 .init_request = virtblk_init_request,
594 };
595
596 static unsigned int virtblk_queue_depth;
597 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
598
599 static int virtblk_probe(struct virtio_device *vdev)
600 {
601 struct virtio_blk *vblk;
602 struct request_queue *q;
603 int err, index;
604
605 u64 cap;
606 u32 v, blk_size, sg_elems, opt_io_size;
607 u16 min_io_size;
608 u8 physical_block_exp, alignment_offset;
609
610 if (!vdev->config->get) {
611 dev_err(&vdev->dev, "%s failure: config access disabled\n",
612 __func__);
613 return -EINVAL;
614 }
615
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;
621
622 /* We need to know how many segments before we allocate. */
623 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
624 struct virtio_blk_config, seg_max,
625 &sg_elems);
626
627 /* We need at least one SG element, whatever they say. */
628 if (err || !sg_elems)
629 sg_elems = 1;
630
631 /* We need an extra sg elements at head and tail. */
632 sg_elems += 2;
633 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
634 if (!vblk) {
635 err = -ENOMEM;
636 goto out_free_index;
637 }
638
639 vblk->vdev = vdev;
640 vblk->sg_elems = sg_elems;
641
642 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
643
644 err = init_vq(vblk);
645 if (err)
646 goto out_free_vblk;
647
648 /* FIXME: How many partitions? How long is a piece of string? */
649 vblk->disk = alloc_disk(1 << PART_BITS);
650 if (!vblk->disk) {
651 err = -ENOMEM;
652 goto out_free_vq;
653 }
654
655 /* Default queue sizing is to fill the ring. */
656 if (!virtblk_queue_depth) {
657 virtblk_queue_depth = vblk->vqs[0].vq->num_free;
658 /* ... but without indirect descs, we use 2 descs per req */
659 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
660 virtblk_queue_depth /= 2;
661 }
662
663 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
664 vblk->tag_set.ops = &virtio_mq_ops;
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 =
669 sizeof(struct virtblk_req) +
670 sizeof(struct scatterlist) * sg_elems;
671 vblk->tag_set.driver_data = vblk;
672 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
673
674 err = blk_mq_alloc_tag_set(&vblk->tag_set);
675 if (err)
676 goto out_put_disk;
677
678 q = blk_mq_init_queue(&vblk->tag_set);
679 if (IS_ERR(q)) {
680 err = -ENOMEM;
681 goto out_free_tags;
682 }
683 vblk->disk->queue = q;
684
685 q->queuedata = vblk;
686
687 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
688
689 vblk->disk->major = major;
690 vblk->disk->first_minor = index_to_minor(index);
691 vblk->disk->private_data = vblk;
692 vblk->disk->fops = &virtblk_fops;
693 vblk->disk->flags |= GENHD_FL_EXT_DEVT;
694 vblk->index = index;
695
696 /* configure queue flush support */
697 virtblk_update_cache_mode(vdev);
698
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
703 /* Host must always specify the capacity. */
704 virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
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
714 /* We can handle whatever the host told us to handle. */
715 blk_queue_max_segments(q, vblk->sg_elems-2);
716
717 /* No need to bounce any requests */
718 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
719
720 /* No real sector limit. */
721 blk_queue_max_hw_sectors(q, -1U);
722
723 /* Host can optionally specify maximum segment size and number of
724 * segments. */
725 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
726 struct virtio_blk_config, size_max, &v);
727 if (!err)
728 blk_queue_max_segment_size(q, v);
729 else
730 blk_queue_max_segment_size(q, -1U);
731
732 /* Host can optionally specify the block size of the device */
733 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
734 struct virtio_blk_config, blk_size,
735 &blk_size);
736 if (!err)
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 */
742 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
743 struct virtio_blk_config, physical_block_exp,
744 &physical_block_exp);
745 if (!err && physical_block_exp)
746 blk_queue_physical_block_size(q,
747 blk_size * (1 << physical_block_exp));
748
749 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
750 struct virtio_blk_config, alignment_offset,
751 &alignment_offset);
752 if (!err && alignment_offset)
753 blk_queue_alignment_offset(q, blk_size * alignment_offset);
754
755 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
756 struct virtio_blk_config, min_io_size,
757 &min_io_size);
758 if (!err && min_io_size)
759 blk_queue_io_min(q, blk_size * min_io_size);
760
761 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
762 struct virtio_blk_config, opt_io_size,
763 &opt_io_size);
764 if (!err && opt_io_size)
765 blk_queue_io_opt(q, blk_size * opt_io_size);
766
767 virtio_device_ready(vdev);
768
769 device_add_disk(&vdev->dev, vblk->disk);
770 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
771 if (err)
772 goto out_del_disk;
773
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;
782 return 0;
783
784 out_del_disk:
785 del_gendisk(vblk->disk);
786 blk_cleanup_queue(vblk->disk->queue);
787 out_free_tags:
788 blk_mq_free_tag_set(&vblk->tag_set);
789 out_put_disk:
790 put_disk(vblk->disk);
791 out_free_vq:
792 vdev->config->del_vqs(vdev);
793 out_free_vblk:
794 kfree(vblk);
795 out_free_index:
796 ida_simple_remove(&vd_index_ida, index);
797 out:
798 return err;
799 }
800
801 static void virtblk_remove(struct virtio_device *vdev)
802 {
803 struct virtio_blk *vblk = vdev->priv;
804 int index = vblk->index;
805 int refc;
806
807 /* Make sure no work handler is accessing the device. */
808 flush_work(&vblk->config_work);
809
810 del_gendisk(vblk->disk);
811 blk_cleanup_queue(vblk->disk->queue);
812
813 blk_mq_free_tag_set(&vblk->tag_set);
814
815 /* Stop all the virtqueues. */
816 vdev->config->reset(vdev);
817
818 refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
819 put_disk(vblk->disk);
820 vdev->config->del_vqs(vdev);
821 kfree(vblk->vqs);
822 kfree(vblk);
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);
827 }
828
829 #ifdef CONFIG_PM_SLEEP
830 static 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
837 /* Make sure no work handler is accessing the device. */
838 flush_work(&vblk->config_work);
839
840 blk_mq_stop_hw_queues(vblk->disk->queue);
841
842 vdev->config->del_vqs(vdev);
843 return 0;
844 }
845
846 static int virtblk_restore(struct virtio_device *vdev)
847 {
848 struct virtio_blk *vblk = vdev->priv;
849 int ret;
850
851 ret = init_vq(vdev->priv);
852 if (ret)
853 return ret;
854
855 virtio_device_ready(vdev);
856
857 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
858 return 0;
859 }
860 #endif
861
862 static const struct virtio_device_id id_table[] = {
863 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
864 { 0 },
865 };
866
867 static unsigned int features_legacy[] = {
868 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
869 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
870 #ifdef CONFIG_VIRTIO_BLK_SCSI
871 VIRTIO_BLK_F_SCSI,
872 #endif
873 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
874 VIRTIO_BLK_F_MQ,
875 }
876 ;
877 static 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,
880 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
881 VIRTIO_BLK_F_MQ,
882 };
883
884 static struct virtio_driver virtio_blk = {
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,
895 #ifdef CONFIG_PM_SLEEP
896 .freeze = virtblk_freeze,
897 .restore = virtblk_restore,
898 #endif
899 };
900
901 static int __init init(void)
902 {
903 int error;
904
905 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
906 if (!virtblk_wq)
907 return -ENOMEM;
908
909 major = register_blkdev(0, "virtblk");
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
920 out_unregister_blkdev:
921 unregister_blkdev(major, "virtblk");
922 out_destroy_workqueue:
923 destroy_workqueue(virtblk_wq);
924 return error;
925 }
926
927 static void __exit fini(void)
928 {
929 unregister_virtio_driver(&virtio_blk);
930 unregister_blkdev(major, "virtblk");
931 destroy_workqueue(virtblk_wq);
932 }
933 module_init(init);
934 module_exit(fini);
935
936 MODULE_DEVICE_TABLE(virtio, id_table);
937 MODULE_DESCRIPTION("Virtio block driver");
938 MODULE_LICENSE("GPL");