1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/spinlock.h>
4 #include <linux/slab.h>
5 #include <linux/blkdev.h>
6 #include <linux/hdreg.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/interrupt.h>
10 #include <linux/virtio.h>
11 #include <linux/virtio_blk.h>
12 #include <linux/scatterlist.h>
13 #include <linux/string_helpers.h>
14 #include <linux/idr.h>
15 #include <linux/blk-mq.h>
16 #include <linux/blk-mq-virtio.h>
17 #include <linux/numa.h>
18 #include <uapi/linux/virtio_ring.h>
21 #define VQ_NAME_LEN 16
22 #define MAX_DISCARD_SEGMENTS 256u
24 /* The maximum number of sg elements that fit into a virtqueue */
25 #define VIRTIO_BLK_MAX_SG_ELEMS 32768
27 #ifdef CONFIG_ARCH_NO_SG_CHAIN
28 #define VIRTIO_BLK_INLINE_SG_CNT 0
30 #define VIRTIO_BLK_INLINE_SG_CNT 2
33 static unsigned int num_request_queues;
34 module_param(num_request_queues, uint, 0644);
35 MODULE_PARM_DESC(num_request_queues,
36 "Limit the number of request queues to use for blk device. "
38 "Values > nr_cpu_ids truncated to nr_cpu_ids.");
40 static unsigned int poll_queues;
41 module_param(poll_queues, uint, 0644);
42 MODULE_PARM_DESC(poll_queues, "The number of dedicated virtqueues for polling I/O");
45 static DEFINE_IDA(vd_index_ida);
47 static struct workqueue_struct *virtblk_wq;
49 struct virtio_blk_vq {
52 char name[VQ_NAME_LEN];
53 } ____cacheline_aligned_in_smp;
57 * This mutex must be held by anything that may run after
58 * virtblk_remove() sets vblk->vdev to NULL.
60 * blk-mq, virtqueue processing, and sysfs attribute code paths are
61 * shut down before vblk->vdev is set to NULL and therefore do not need
64 struct mutex vdev_mutex;
65 struct virtio_device *vdev;
67 /* The disk structure for the kernel. */
70 /* Block layer tags. */
71 struct blk_mq_tag_set tag_set;
73 /* Process context for config space updates */
74 struct work_struct config_work;
76 /* Ida index - used to track minor number allocations. */
81 int io_queues[HCTX_MAX_TYPES];
82 struct virtio_blk_vq *vqs;
86 struct virtio_blk_outhdr out_hdr;
88 struct sg_table sg_table;
89 struct scatterlist sg[];
92 static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
94 switch (vbr->status) {
97 case VIRTIO_BLK_S_UNSUPP:
98 return BLK_STS_NOTSUPP;
100 return BLK_STS_IOERR;
104 static inline struct virtio_blk_vq *get_virtio_blk_vq(struct blk_mq_hw_ctx *hctx)
106 struct virtio_blk *vblk = hctx->queue->queuedata;
107 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
112 static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
114 struct scatterlist hdr, status, *sgs[3];
115 unsigned int num_out = 0, num_in = 0;
117 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
118 sgs[num_out++] = &hdr;
120 if (vbr->sg_table.nents) {
121 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
122 sgs[num_out++] = vbr->sg_table.sgl;
124 sgs[num_out + num_in++] = vbr->sg_table.sgl;
127 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
128 sgs[num_out + num_in++] = &status;
130 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
133 static int virtblk_setup_discard_write_zeroes(struct request *req, bool unmap)
135 unsigned short segments = blk_rq_nr_discard_segments(req);
136 unsigned short n = 0;
137 struct virtio_blk_discard_write_zeroes *range;
142 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
144 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
149 * Single max discard segment means multi-range discard isn't
150 * supported, and block layer only runs contiguity merge like
151 * normal RW request. So we can't reply on bio for retrieving
154 if (queue_max_discard_segments(req->q) == 1) {
155 range[0].flags = cpu_to_le32(flags);
156 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
157 range[0].sector = cpu_to_le64(blk_rq_pos(req));
160 __rq_for_each_bio(bio, req) {
161 u64 sector = bio->bi_iter.bi_sector;
162 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
164 range[n].flags = cpu_to_le32(flags);
165 range[n].num_sectors = cpu_to_le32(num_sectors);
166 range[n].sector = cpu_to_le64(sector);
171 WARN_ON_ONCE(n != segments);
173 req->special_vec.bv_page = virt_to_page(range);
174 req->special_vec.bv_offset = offset_in_page(range);
175 req->special_vec.bv_len = sizeof(*range) * segments;
176 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
181 static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
183 if (blk_rq_nr_phys_segments(req))
184 sg_free_table_chained(&vbr->sg_table,
185 VIRTIO_BLK_INLINE_SG_CNT);
188 static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
189 struct virtblk_req *vbr)
193 if (!blk_rq_nr_phys_segments(req))
196 vbr->sg_table.sgl = vbr->sg;
197 err = sg_alloc_table_chained(&vbr->sg_table,
198 blk_rq_nr_phys_segments(req),
200 VIRTIO_BLK_INLINE_SG_CNT);
204 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
207 static void virtblk_cleanup_cmd(struct request *req)
209 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
210 kfree(bvec_virt(&req->special_vec));
213 static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
215 struct virtblk_req *vbr)
220 vbr->out_hdr.sector = 0;
222 switch (req_op(req)) {
224 type = VIRTIO_BLK_T_IN;
225 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
229 type = VIRTIO_BLK_T_OUT;
230 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
234 type = VIRTIO_BLK_T_FLUSH;
237 type = VIRTIO_BLK_T_DISCARD;
239 case REQ_OP_WRITE_ZEROES:
240 type = VIRTIO_BLK_T_WRITE_ZEROES;
241 unmap = !(req->cmd_flags & REQ_NOUNMAP);
244 type = VIRTIO_BLK_T_GET_ID;
248 return BLK_STS_IOERR;
251 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
252 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
254 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
255 if (virtblk_setup_discard_write_zeroes(req, unmap))
256 return BLK_STS_RESOURCE;
262 static inline void virtblk_request_done(struct request *req)
264 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
266 virtblk_unmap_data(req, vbr);
267 virtblk_cleanup_cmd(req);
268 blk_mq_end_request(req, virtblk_result(vbr));
271 static void virtblk_done(struct virtqueue *vq)
273 struct virtio_blk *vblk = vq->vdev->priv;
274 bool req_done = false;
276 struct virtblk_req *vbr;
280 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
282 virtqueue_disable_cb(vq);
283 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
284 struct request *req = blk_mq_rq_from_pdu(vbr);
286 if (likely(!blk_should_fake_timeout(req->q)))
287 blk_mq_complete_request(req);
290 if (unlikely(virtqueue_is_broken(vq)))
292 } while (!virtqueue_enable_cb(vq));
294 /* In case queue is stopped waiting for more buffers. */
296 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
297 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
300 static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
302 struct virtio_blk *vblk = hctx->queue->queuedata;
303 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
306 spin_lock_irq(&vq->lock);
307 kick = virtqueue_kick_prepare(vq->vq);
308 spin_unlock_irq(&vq->lock);
311 virtqueue_notify(vq->vq);
314 static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
315 struct virtio_blk *vblk,
317 struct virtblk_req *vbr)
321 status = virtblk_setup_cmd(vblk->vdev, req, vbr);
322 if (unlikely(status))
325 vbr->sg_table.nents = virtblk_map_data(hctx, req, vbr);
326 if (unlikely(vbr->sg_table.nents < 0)) {
327 virtblk_cleanup_cmd(req);
328 return BLK_STS_RESOURCE;
331 blk_mq_start_request(req);
336 static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
337 const struct blk_mq_queue_data *bd)
339 struct virtio_blk *vblk = hctx->queue->queuedata;
340 struct request *req = bd->rq;
341 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
343 int qid = hctx->queue_num;
348 status = virtblk_prep_rq(hctx, vblk, req, vbr);
349 if (unlikely(status))
352 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
353 err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
355 virtqueue_kick(vblk->vqs[qid].vq);
356 /* Don't stop the queue if -ENOMEM: we may have failed to
357 * bounce the buffer due to global resource outage.
360 blk_mq_stop_hw_queue(hctx);
361 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
362 virtblk_unmap_data(req, vbr);
363 virtblk_cleanup_cmd(req);
366 return BLK_STS_DEV_RESOURCE;
368 return BLK_STS_RESOURCE;
370 return BLK_STS_IOERR;
374 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
376 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
379 virtqueue_notify(vblk->vqs[qid].vq);
383 static bool virtblk_prep_rq_batch(struct request *req)
385 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
386 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
388 req->mq_hctx->tags->rqs[req->tag] = req;
390 return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
393 static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
394 struct request **rqlist)
400 spin_lock_irqsave(&vq->lock, flags);
402 while (!rq_list_empty(*rqlist)) {
403 struct request *req = rq_list_pop(rqlist);
404 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
406 err = virtblk_add_req(vq->vq, vbr);
408 virtblk_unmap_data(req, vbr);
409 virtblk_cleanup_cmd(req);
410 blk_mq_requeue_request(req, true);
414 kick = virtqueue_kick_prepare(vq->vq);
415 spin_unlock_irqrestore(&vq->lock, flags);
420 static void virtio_queue_rqs(struct request **rqlist)
422 struct request *req, *next, *prev = NULL;
423 struct request *requeue_list = NULL;
425 rq_list_for_each_safe(rqlist, req, next) {
426 struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
429 if (!virtblk_prep_rq_batch(req)) {
430 rq_list_move(rqlist, &requeue_list, req, prev);
436 if (!next || req->mq_hctx != next->mq_hctx) {
438 kick = virtblk_add_req_batch(vq, rqlist);
440 virtqueue_notify(vq->vq);
448 *rqlist = requeue_list;
451 /* return id (s/n) string for *disk to *id_str
453 static int virtblk_get_id(struct gendisk *disk, char *id_str)
455 struct virtio_blk *vblk = disk->private_data;
456 struct request_queue *q = vblk->disk->queue;
460 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
464 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
468 blk_execute_rq(req, false);
469 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
471 blk_mq_free_request(req);
475 /* We provide getgeo only to please some old bootloader/partitioning tools */
476 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
478 struct virtio_blk *vblk = bd->bd_disk->private_data;
481 mutex_lock(&vblk->vdev_mutex);
488 /* see if the host passed in geometry config */
489 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
490 virtio_cread(vblk->vdev, struct virtio_blk_config,
491 geometry.cylinders, &geo->cylinders);
492 virtio_cread(vblk->vdev, struct virtio_blk_config,
493 geometry.heads, &geo->heads);
494 virtio_cread(vblk->vdev, struct virtio_blk_config,
495 geometry.sectors, &geo->sectors);
497 /* some standard values, similar to sd */
499 geo->sectors = 1 << 5;
500 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
503 mutex_unlock(&vblk->vdev_mutex);
507 static void virtblk_free_disk(struct gendisk *disk)
509 struct virtio_blk *vblk = disk->private_data;
511 ida_simple_remove(&vd_index_ida, vblk->index);
512 mutex_destroy(&vblk->vdev_mutex);
516 static const struct block_device_operations virtblk_fops = {
517 .owner = THIS_MODULE,
518 .getgeo = virtblk_getgeo,
519 .free_disk = virtblk_free_disk,
522 static int index_to_minor(int index)
524 return index << PART_BITS;
527 static int minor_to_index(int minor)
529 return minor >> PART_BITS;
532 static ssize_t serial_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct gendisk *disk = dev_to_disk(dev);
538 /* sysfs gives us a PAGE_SIZE buffer */
539 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
541 buf[VIRTIO_BLK_ID_BYTES] = '\0';
542 err = virtblk_get_id(disk, buf);
546 if (err == -EIO) /* Unsupported? Make it empty. */
552 static DEVICE_ATTR_RO(serial);
554 /* The queue's logical block size must be set before calling this */
555 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
557 struct virtio_device *vdev = vblk->vdev;
558 struct request_queue *q = vblk->disk->queue;
559 char cap_str_2[10], cap_str_10[10];
560 unsigned long long nblocks;
563 /* Host must always specify the capacity. */
564 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
566 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
568 string_get_size(nblocks, queue_logical_block_size(q),
569 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
570 string_get_size(nblocks, queue_logical_block_size(q),
571 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
573 dev_notice(&vdev->dev,
574 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
575 vblk->disk->disk_name,
576 resize ? "new size: " : "",
578 queue_logical_block_size(q),
582 set_capacity_and_notify(vblk->disk, capacity);
585 static void virtblk_config_changed_work(struct work_struct *work)
587 struct virtio_blk *vblk =
588 container_of(work, struct virtio_blk, config_work);
590 virtblk_update_capacity(vblk, true);
593 static void virtblk_config_changed(struct virtio_device *vdev)
595 struct virtio_blk *vblk = vdev->priv;
597 queue_work(virtblk_wq, &vblk->config_work);
600 static int init_vq(struct virtio_blk *vblk)
604 vq_callback_t **callbacks;
606 struct virtqueue **vqs;
607 unsigned short num_vqs;
608 unsigned int num_poll_vqs;
609 struct virtio_device *vdev = vblk->vdev;
610 struct irq_affinity desc = { 0, };
612 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
613 struct virtio_blk_config, num_queues,
618 if (!err && !num_vqs) {
619 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
623 num_vqs = min_t(unsigned int,
624 min_not_zero(num_request_queues, nr_cpu_ids),
627 num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
629 vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
630 vblk->io_queues[HCTX_TYPE_READ] = 0;
631 vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
633 dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
634 vblk->io_queues[HCTX_TYPE_DEFAULT],
635 vblk->io_queues[HCTX_TYPE_READ],
636 vblk->io_queues[HCTX_TYPE_POLL]);
638 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
642 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
643 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
644 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
645 if (!names || !callbacks || !vqs) {
650 for (i = 0; i < num_vqs - num_poll_vqs; i++) {
651 callbacks[i] = virtblk_done;
652 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
653 names[i] = vblk->vqs[i].name;
656 for (; i < num_vqs; i++) {
658 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
659 names[i] = vblk->vqs[i].name;
662 /* Discover virtqueues and write information to configuration. */
663 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
667 for (i = 0; i < num_vqs; i++) {
668 spin_lock_init(&vblk->vqs[i].lock);
669 vblk->vqs[i].vq = vqs[i];
671 vblk->num_vqs = num_vqs;
683 * Legacy naming scheme used for virtio devices. We are stuck with it for
684 * virtio blk but don't ever use it for any new driver.
686 static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
688 const int base = 'z' - 'a' + 1;
689 char *begin = buf + strlen(prefix);
690 char *end = buf + buflen;
700 *--p = 'a' + (index % unit);
701 index = (index / unit) - 1;
702 } while (index >= 0);
704 memmove(begin, p, end - p);
705 memcpy(buf, prefix, strlen(prefix));
710 static int virtblk_get_cache_mode(struct virtio_device *vdev)
715 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
716 struct virtio_blk_config, wce,
720 * If WCE is not configurable and flush is not available,
721 * assume no writeback cache is in use.
724 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
729 static void virtblk_update_cache_mode(struct virtio_device *vdev)
731 u8 writeback = virtblk_get_cache_mode(vdev);
732 struct virtio_blk *vblk = vdev->priv;
734 blk_queue_write_cache(vblk->disk->queue, writeback, false);
737 static const char *const virtblk_cache_types[] = {
738 "write through", "write back"
742 cache_type_store(struct device *dev, struct device_attribute *attr,
743 const char *buf, size_t count)
745 struct gendisk *disk = dev_to_disk(dev);
746 struct virtio_blk *vblk = disk->private_data;
747 struct virtio_device *vdev = vblk->vdev;
750 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
751 i = sysfs_match_string(virtblk_cache_types, buf);
755 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
756 virtblk_update_cache_mode(vdev);
761 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
763 struct gendisk *disk = dev_to_disk(dev);
764 struct virtio_blk *vblk = disk->private_data;
765 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
767 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
768 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
771 static DEVICE_ATTR_RW(cache_type);
773 static struct attribute *virtblk_attrs[] = {
774 &dev_attr_serial.attr,
775 &dev_attr_cache_type.attr,
779 static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
780 struct attribute *a, int n)
782 struct device *dev = kobj_to_dev(kobj);
783 struct gendisk *disk = dev_to_disk(dev);
784 struct virtio_blk *vblk = disk->private_data;
785 struct virtio_device *vdev = vblk->vdev;
787 if (a == &dev_attr_cache_type.attr &&
788 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
794 static const struct attribute_group virtblk_attr_group = {
795 .attrs = virtblk_attrs,
796 .is_visible = virtblk_attrs_are_visible,
799 static const struct attribute_group *virtblk_attr_groups[] = {
804 static void virtblk_map_queues(struct blk_mq_tag_set *set)
806 struct virtio_blk *vblk = set->driver_data;
809 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
810 struct blk_mq_queue_map *map = &set->map[i];
812 map->nr_queues = vblk->io_queues[i];
813 map->queue_offset = qoff;
814 qoff += map->nr_queues;
816 if (map->nr_queues == 0)
820 * Regular queues have interrupts and hence CPU affinity is
821 * defined by the core virtio code, but polling queues have
822 * no interrupts so we let the block layer assign CPU affinity.
824 if (i == HCTX_TYPE_POLL)
825 blk_mq_map_queues(&set->map[i]);
827 blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
831 static void virtblk_complete_batch(struct io_comp_batch *iob)
835 rq_list_for_each(&iob->req_list, req) {
836 virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
837 virtblk_cleanup_cmd(req);
839 blk_mq_end_request_batch(iob);
842 static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
844 struct virtio_blk *vblk = hctx->queue->queuedata;
845 struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
846 struct virtblk_req *vbr;
851 spin_lock_irqsave(&vq->lock, flags);
853 while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
854 struct request *req = blk_mq_rq_from_pdu(vbr);
857 if (!blk_mq_add_to_batch(req, iob, vbr->status,
858 virtblk_complete_batch))
859 blk_mq_complete_request(req);
863 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
865 spin_unlock_irqrestore(&vq->lock, flags);
870 static const struct blk_mq_ops virtio_mq_ops = {
871 .queue_rq = virtio_queue_rq,
872 .queue_rqs = virtio_queue_rqs,
873 .commit_rqs = virtio_commit_rqs,
874 .complete = virtblk_request_done,
875 .map_queues = virtblk_map_queues,
876 .poll = virtblk_poll,
879 static unsigned int virtblk_queue_depth;
880 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
882 static int virtblk_probe(struct virtio_device *vdev)
884 struct virtio_blk *vblk;
885 struct request_queue *q;
888 u32 v, blk_size, max_size, sg_elems, opt_io_size;
890 u8 physical_block_exp, alignment_offset;
891 unsigned int queue_depth;
893 if (!vdev->config->get) {
894 dev_err(&vdev->dev, "%s failure: config access disabled\n",
899 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
905 /* We need to know how many segments before we allocate. */
906 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
907 struct virtio_blk_config, seg_max,
910 /* We need at least one SG element, whatever they say. */
911 if (err || !sg_elems)
914 /* Prevent integer overflows and honor max vq size */
915 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
917 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
923 mutex_init(&vblk->vdev_mutex);
927 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
933 /* Default queue sizing is to fill the ring. */
934 if (!virtblk_queue_depth) {
935 queue_depth = vblk->vqs[0].vq->num_free;
936 /* ... but without indirect descs, we use 2 descs per req */
937 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
940 queue_depth = virtblk_queue_depth;
943 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
944 vblk->tag_set.ops = &virtio_mq_ops;
945 vblk->tag_set.queue_depth = queue_depth;
946 vblk->tag_set.numa_node = NUMA_NO_NODE;
947 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
948 vblk->tag_set.cmd_size =
949 sizeof(struct virtblk_req) +
950 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
951 vblk->tag_set.driver_data = vblk;
952 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
953 vblk->tag_set.nr_maps = 1;
954 if (vblk->io_queues[HCTX_TYPE_POLL])
955 vblk->tag_set.nr_maps = 3;
957 err = blk_mq_alloc_tag_set(&vblk->tag_set);
961 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
962 if (IS_ERR(vblk->disk)) {
963 err = PTR_ERR(vblk->disk);
966 q = vblk->disk->queue;
968 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
970 vblk->disk->major = major;
971 vblk->disk->first_minor = index_to_minor(index);
972 vblk->disk->minors = 1 << PART_BITS;
973 vblk->disk->private_data = vblk;
974 vblk->disk->fops = &virtblk_fops;
977 /* configure queue flush support */
978 virtblk_update_cache_mode(vdev);
980 /* If disk is read-only in the host, the guest should obey */
981 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
982 set_disk_ro(vblk->disk, 1);
984 /* We can handle whatever the host told us to handle. */
985 blk_queue_max_segments(q, sg_elems);
987 /* No real sector limit. */
988 blk_queue_max_hw_sectors(q, -1U);
990 max_size = virtio_max_dma_size(vdev);
992 /* Host can optionally specify maximum segment size and number of
994 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
995 struct virtio_blk_config, size_max, &v);
997 max_size = min(max_size, v);
999 blk_queue_max_segment_size(q, max_size);
1001 /* Host can optionally specify the block size of the device */
1002 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
1003 struct virtio_blk_config, blk_size,
1006 err = blk_validate_block_size(blk_size);
1009 "virtio_blk: invalid block size: 0x%x\n",
1011 goto out_cleanup_disk;
1014 blk_queue_logical_block_size(q, blk_size);
1016 blk_size = queue_logical_block_size(q);
1018 /* Use topology information if available */
1019 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1020 struct virtio_blk_config, physical_block_exp,
1021 &physical_block_exp);
1022 if (!err && physical_block_exp)
1023 blk_queue_physical_block_size(q,
1024 blk_size * (1 << physical_block_exp));
1026 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1027 struct virtio_blk_config, alignment_offset,
1029 if (!err && alignment_offset)
1030 blk_queue_alignment_offset(q, blk_size * alignment_offset);
1032 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1033 struct virtio_blk_config, min_io_size,
1035 if (!err && min_io_size)
1036 blk_queue_io_min(q, blk_size * min_io_size);
1038 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1039 struct virtio_blk_config, opt_io_size,
1041 if (!err && opt_io_size)
1042 blk_queue_io_opt(q, blk_size * opt_io_size);
1044 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
1045 virtio_cread(vdev, struct virtio_blk_config,
1046 discard_sector_alignment, &v);
1048 q->limits.discard_granularity = v << SECTOR_SHIFT;
1050 q->limits.discard_granularity = blk_size;
1052 virtio_cread(vdev, struct virtio_blk_config,
1053 max_discard_sectors, &v);
1054 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
1056 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
1060 * max_discard_seg == 0 is out of spec but we always
1065 blk_queue_max_discard_segments(q,
1066 min(v, MAX_DISCARD_SEGMENTS));
1069 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
1070 virtio_cread(vdev, struct virtio_blk_config,
1071 max_write_zeroes_sectors, &v);
1072 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
1075 virtblk_update_capacity(vblk, false);
1076 virtio_device_ready(vdev);
1078 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
1080 goto out_cleanup_disk;
1085 put_disk(vblk->disk);
1087 blk_mq_free_tag_set(&vblk->tag_set);
1089 vdev->config->del_vqs(vdev);
1094 ida_simple_remove(&vd_index_ida, index);
1099 static void virtblk_remove(struct virtio_device *vdev)
1101 struct virtio_blk *vblk = vdev->priv;
1103 /* Make sure no work handler is accessing the device. */
1104 flush_work(&vblk->config_work);
1106 del_gendisk(vblk->disk);
1107 blk_mq_free_tag_set(&vblk->tag_set);
1109 mutex_lock(&vblk->vdev_mutex);
1111 /* Stop all the virtqueues. */
1112 virtio_reset_device(vdev);
1114 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
1117 vdev->config->del_vqs(vdev);
1120 mutex_unlock(&vblk->vdev_mutex);
1122 put_disk(vblk->disk);
1125 #ifdef CONFIG_PM_SLEEP
1126 static int virtblk_freeze(struct virtio_device *vdev)
1128 struct virtio_blk *vblk = vdev->priv;
1130 /* Ensure we don't receive any more interrupts */
1131 virtio_reset_device(vdev);
1133 /* Make sure no work handler is accessing the device. */
1134 flush_work(&vblk->config_work);
1136 blk_mq_quiesce_queue(vblk->disk->queue);
1138 vdev->config->del_vqs(vdev);
1144 static int virtblk_restore(struct virtio_device *vdev)
1146 struct virtio_blk *vblk = vdev->priv;
1149 ret = init_vq(vdev->priv);
1153 virtio_device_ready(vdev);
1155 blk_mq_unquiesce_queue(vblk->disk->queue);
1160 static const struct virtio_device_id id_table[] = {
1161 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1165 static unsigned int features_legacy[] = {
1166 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1167 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1168 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1169 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1172 static unsigned int features[] = {
1173 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1174 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1175 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1176 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1179 static struct virtio_driver virtio_blk = {
1180 .feature_table = features,
1181 .feature_table_size = ARRAY_SIZE(features),
1182 .feature_table_legacy = features_legacy,
1183 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1184 .driver.name = KBUILD_MODNAME,
1185 .driver.owner = THIS_MODULE,
1186 .id_table = id_table,
1187 .probe = virtblk_probe,
1188 .remove = virtblk_remove,
1189 .config_changed = virtblk_config_changed,
1190 #ifdef CONFIG_PM_SLEEP
1191 .freeze = virtblk_freeze,
1192 .restore = virtblk_restore,
1196 static int __init virtio_blk_init(void)
1200 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1204 major = register_blkdev(0, "virtblk");
1207 goto out_destroy_workqueue;
1210 error = register_virtio_driver(&virtio_blk);
1212 goto out_unregister_blkdev;
1215 out_unregister_blkdev:
1216 unregister_blkdev(major, "virtblk");
1217 out_destroy_workqueue:
1218 destroy_workqueue(virtblk_wq);
1222 static void __exit virtio_blk_fini(void)
1224 unregister_virtio_driver(&virtio_blk);
1225 unregister_blkdev(major, "virtblk");
1226 destroy_workqueue(virtblk_wq);
1228 module_init(virtio_blk_init);
1229 module_exit(virtio_blk_fini);
1231 MODULE_DEVICE_TABLE(virtio, id_table);
1232 MODULE_DESCRIPTION("Virtio block driver");
1233 MODULE_LICENSE("GPL");