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 blk_mq_start_request(req);
327 vbr->sg_table.nents = virtblk_map_data(hctx, req, vbr);
328 if (unlikely(vbr->sg_table.nents < 0)) {
329 virtblk_cleanup_cmd(req);
330 return BLK_STS_RESOURCE;
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,
395 struct request **requeue_list)
401 spin_lock_irqsave(&vq->lock, flags);
403 while (!rq_list_empty(*rqlist)) {
404 struct request *req = rq_list_pop(rqlist);
405 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
407 err = virtblk_add_req(vq->vq, vbr);
409 virtblk_unmap_data(req, vbr);
410 virtblk_cleanup_cmd(req);
411 rq_list_add(requeue_list, req);
415 kick = virtqueue_kick_prepare(vq->vq);
416 spin_unlock_irqrestore(&vq->lock, flags);
421 static void virtio_queue_rqs(struct request **rqlist)
423 struct request *req, *next, *prev = NULL;
424 struct request *requeue_list = NULL;
426 rq_list_for_each_safe(rqlist, req, next) {
427 struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
430 if (!virtblk_prep_rq_batch(req)) {
431 rq_list_move(rqlist, &requeue_list, req, prev);
437 if (!next || req->mq_hctx != next->mq_hctx) {
439 kick = virtblk_add_req_batch(vq, rqlist, &requeue_list);
441 virtqueue_notify(vq->vq);
449 *rqlist = requeue_list;
452 /* return id (s/n) string for *disk to *id_str
454 static int virtblk_get_id(struct gendisk *disk, char *id_str)
456 struct virtio_blk *vblk = disk->private_data;
457 struct request_queue *q = vblk->disk->queue;
461 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
465 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
469 blk_execute_rq(req, false);
470 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
472 blk_mq_free_request(req);
476 /* We provide getgeo only to please some old bootloader/partitioning tools */
477 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
479 struct virtio_blk *vblk = bd->bd_disk->private_data;
482 mutex_lock(&vblk->vdev_mutex);
489 /* see if the host passed in geometry config */
490 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
491 virtio_cread(vblk->vdev, struct virtio_blk_config,
492 geometry.cylinders, &geo->cylinders);
493 virtio_cread(vblk->vdev, struct virtio_blk_config,
494 geometry.heads, &geo->heads);
495 virtio_cread(vblk->vdev, struct virtio_blk_config,
496 geometry.sectors, &geo->sectors);
498 /* some standard values, similar to sd */
500 geo->sectors = 1 << 5;
501 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
504 mutex_unlock(&vblk->vdev_mutex);
508 static void virtblk_free_disk(struct gendisk *disk)
510 struct virtio_blk *vblk = disk->private_data;
512 ida_simple_remove(&vd_index_ida, vblk->index);
513 mutex_destroy(&vblk->vdev_mutex);
517 static const struct block_device_operations virtblk_fops = {
518 .owner = THIS_MODULE,
519 .getgeo = virtblk_getgeo,
520 .free_disk = virtblk_free_disk,
523 static int index_to_minor(int index)
525 return index << PART_BITS;
528 static int minor_to_index(int minor)
530 return minor >> PART_BITS;
533 static ssize_t serial_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 struct gendisk *disk = dev_to_disk(dev);
539 /* sysfs gives us a PAGE_SIZE buffer */
540 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
542 buf[VIRTIO_BLK_ID_BYTES] = '\0';
543 err = virtblk_get_id(disk, buf);
547 if (err == -EIO) /* Unsupported? Make it empty. */
553 static DEVICE_ATTR_RO(serial);
555 /* The queue's logical block size must be set before calling this */
556 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
558 struct virtio_device *vdev = vblk->vdev;
559 struct request_queue *q = vblk->disk->queue;
560 char cap_str_2[10], cap_str_10[10];
561 unsigned long long nblocks;
564 /* Host must always specify the capacity. */
565 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
567 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
569 string_get_size(nblocks, queue_logical_block_size(q),
570 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
571 string_get_size(nblocks, queue_logical_block_size(q),
572 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
574 dev_notice(&vdev->dev,
575 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
576 vblk->disk->disk_name,
577 resize ? "new size: " : "",
579 queue_logical_block_size(q),
583 set_capacity_and_notify(vblk->disk, capacity);
586 static void virtblk_config_changed_work(struct work_struct *work)
588 struct virtio_blk *vblk =
589 container_of(work, struct virtio_blk, config_work);
591 virtblk_update_capacity(vblk, true);
594 static void virtblk_config_changed(struct virtio_device *vdev)
596 struct virtio_blk *vblk = vdev->priv;
598 queue_work(virtblk_wq, &vblk->config_work);
601 static int init_vq(struct virtio_blk *vblk)
605 vq_callback_t **callbacks;
607 struct virtqueue **vqs;
608 unsigned short num_vqs;
609 unsigned int num_poll_vqs;
610 struct virtio_device *vdev = vblk->vdev;
611 struct irq_affinity desc = { 0, };
613 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
614 struct virtio_blk_config, num_queues,
619 if (!err && !num_vqs) {
620 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
624 num_vqs = min_t(unsigned int,
625 min_not_zero(num_request_queues, nr_cpu_ids),
628 num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
630 vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
631 vblk->io_queues[HCTX_TYPE_READ] = 0;
632 vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
634 dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
635 vblk->io_queues[HCTX_TYPE_DEFAULT],
636 vblk->io_queues[HCTX_TYPE_READ],
637 vblk->io_queues[HCTX_TYPE_POLL]);
639 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
643 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
644 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
645 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
646 if (!names || !callbacks || !vqs) {
651 for (i = 0; i < num_vqs - num_poll_vqs; i++) {
652 callbacks[i] = virtblk_done;
653 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
654 names[i] = vblk->vqs[i].name;
657 for (; i < num_vqs; i++) {
659 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
660 names[i] = vblk->vqs[i].name;
663 /* Discover virtqueues and write information to configuration. */
664 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
668 for (i = 0; i < num_vqs; i++) {
669 spin_lock_init(&vblk->vqs[i].lock);
670 vblk->vqs[i].vq = vqs[i];
672 vblk->num_vqs = num_vqs;
684 * Legacy naming scheme used for virtio devices. We are stuck with it for
685 * virtio blk but don't ever use it for any new driver.
687 static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
689 const int base = 'z' - 'a' + 1;
690 char *begin = buf + strlen(prefix);
691 char *end = buf + buflen;
701 *--p = 'a' + (index % unit);
702 index = (index / unit) - 1;
703 } while (index >= 0);
705 memmove(begin, p, end - p);
706 memcpy(buf, prefix, strlen(prefix));
711 static int virtblk_get_cache_mode(struct virtio_device *vdev)
716 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
717 struct virtio_blk_config, wce,
721 * If WCE is not configurable and flush is not available,
722 * assume no writeback cache is in use.
725 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
730 static void virtblk_update_cache_mode(struct virtio_device *vdev)
732 u8 writeback = virtblk_get_cache_mode(vdev);
733 struct virtio_blk *vblk = vdev->priv;
735 blk_queue_write_cache(vblk->disk->queue, writeback, false);
738 static const char *const virtblk_cache_types[] = {
739 "write through", "write back"
743 cache_type_store(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
746 struct gendisk *disk = dev_to_disk(dev);
747 struct virtio_blk *vblk = disk->private_data;
748 struct virtio_device *vdev = vblk->vdev;
751 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
752 i = sysfs_match_string(virtblk_cache_types, buf);
756 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
757 virtblk_update_cache_mode(vdev);
762 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
764 struct gendisk *disk = dev_to_disk(dev);
765 struct virtio_blk *vblk = disk->private_data;
766 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
768 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
769 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
772 static DEVICE_ATTR_RW(cache_type);
774 static struct attribute *virtblk_attrs[] = {
775 &dev_attr_serial.attr,
776 &dev_attr_cache_type.attr,
780 static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
781 struct attribute *a, int n)
783 struct device *dev = kobj_to_dev(kobj);
784 struct gendisk *disk = dev_to_disk(dev);
785 struct virtio_blk *vblk = disk->private_data;
786 struct virtio_device *vdev = vblk->vdev;
788 if (a == &dev_attr_cache_type.attr &&
789 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
795 static const struct attribute_group virtblk_attr_group = {
796 .attrs = virtblk_attrs,
797 .is_visible = virtblk_attrs_are_visible,
800 static const struct attribute_group *virtblk_attr_groups[] = {
805 static int virtblk_map_queues(struct blk_mq_tag_set *set)
807 struct virtio_blk *vblk = set->driver_data;
810 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
811 struct blk_mq_queue_map *map = &set->map[i];
813 map->nr_queues = vblk->io_queues[i];
814 map->queue_offset = qoff;
815 qoff += map->nr_queues;
817 if (map->nr_queues == 0)
821 * Regular queues have interrupts and hence CPU affinity is
822 * defined by the core virtio code, but polling queues have
823 * no interrupts so we let the block layer assign CPU affinity.
825 if (i == HCTX_TYPE_POLL)
826 blk_mq_map_queues(&set->map[i]);
828 blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
834 static void virtblk_complete_batch(struct io_comp_batch *iob)
838 rq_list_for_each(&iob->req_list, req) {
839 virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
840 virtblk_cleanup_cmd(req);
842 blk_mq_end_request_batch(iob);
845 static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
847 struct virtio_blk *vblk = hctx->queue->queuedata;
848 struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
849 struct virtblk_req *vbr;
854 spin_lock_irqsave(&vq->lock, flags);
856 while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
857 struct request *req = blk_mq_rq_from_pdu(vbr);
860 if (!blk_mq_add_to_batch(req, iob, vbr->status,
861 virtblk_complete_batch))
862 blk_mq_complete_request(req);
866 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
868 spin_unlock_irqrestore(&vq->lock, flags);
873 static const struct blk_mq_ops virtio_mq_ops = {
874 .queue_rq = virtio_queue_rq,
875 .queue_rqs = virtio_queue_rqs,
876 .commit_rqs = virtio_commit_rqs,
877 .complete = virtblk_request_done,
878 .map_queues = virtblk_map_queues,
879 .poll = virtblk_poll,
882 static unsigned int virtblk_queue_depth;
883 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
885 static int virtblk_probe(struct virtio_device *vdev)
887 struct virtio_blk *vblk;
888 struct request_queue *q;
891 u32 v, blk_size, max_size, sg_elems, opt_io_size;
893 u8 physical_block_exp, alignment_offset;
894 unsigned int queue_depth;
896 if (!vdev->config->get) {
897 dev_err(&vdev->dev, "%s failure: config access disabled\n",
902 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
908 /* We need to know how many segments before we allocate. */
909 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
910 struct virtio_blk_config, seg_max,
913 /* We need at least one SG element, whatever they say. */
914 if (err || !sg_elems)
917 /* Prevent integer overflows and honor max vq size */
918 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
920 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
926 mutex_init(&vblk->vdev_mutex);
930 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
936 /* Default queue sizing is to fill the ring. */
937 if (!virtblk_queue_depth) {
938 queue_depth = vblk->vqs[0].vq->num_free;
939 /* ... but without indirect descs, we use 2 descs per req */
940 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
943 queue_depth = virtblk_queue_depth;
946 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
947 vblk->tag_set.ops = &virtio_mq_ops;
948 vblk->tag_set.queue_depth = queue_depth;
949 vblk->tag_set.numa_node = NUMA_NO_NODE;
950 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
951 vblk->tag_set.cmd_size =
952 sizeof(struct virtblk_req) +
953 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
954 vblk->tag_set.driver_data = vblk;
955 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
956 vblk->tag_set.nr_maps = 1;
957 if (vblk->io_queues[HCTX_TYPE_POLL])
958 vblk->tag_set.nr_maps = 3;
960 err = blk_mq_alloc_tag_set(&vblk->tag_set);
964 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
965 if (IS_ERR(vblk->disk)) {
966 err = PTR_ERR(vblk->disk);
969 q = vblk->disk->queue;
971 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
973 vblk->disk->major = major;
974 vblk->disk->first_minor = index_to_minor(index);
975 vblk->disk->minors = 1 << PART_BITS;
976 vblk->disk->private_data = vblk;
977 vblk->disk->fops = &virtblk_fops;
980 /* configure queue flush support */
981 virtblk_update_cache_mode(vdev);
983 /* If disk is read-only in the host, the guest should obey */
984 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
985 set_disk_ro(vblk->disk, 1);
987 /* We can handle whatever the host told us to handle. */
988 blk_queue_max_segments(q, sg_elems);
990 /* No real sector limit. */
991 blk_queue_max_hw_sectors(q, -1U);
993 max_size = virtio_max_dma_size(vdev);
995 /* Host can optionally specify maximum segment size and number of
997 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
998 struct virtio_blk_config, size_max, &v);
1000 max_size = min(max_size, v);
1002 blk_queue_max_segment_size(q, max_size);
1004 /* Host can optionally specify the block size of the device */
1005 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
1006 struct virtio_blk_config, blk_size,
1009 err = blk_validate_block_size(blk_size);
1012 "virtio_blk: invalid block size: 0x%x\n",
1014 goto out_cleanup_disk;
1017 blk_queue_logical_block_size(q, blk_size);
1019 blk_size = queue_logical_block_size(q);
1021 /* Use topology information if available */
1022 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1023 struct virtio_blk_config, physical_block_exp,
1024 &physical_block_exp);
1025 if (!err && physical_block_exp)
1026 blk_queue_physical_block_size(q,
1027 blk_size * (1 << physical_block_exp));
1029 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1030 struct virtio_blk_config, alignment_offset,
1032 if (!err && alignment_offset)
1033 blk_queue_alignment_offset(q, blk_size * alignment_offset);
1035 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1036 struct virtio_blk_config, min_io_size,
1038 if (!err && min_io_size)
1039 blk_queue_io_min(q, blk_size * min_io_size);
1041 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1042 struct virtio_blk_config, opt_io_size,
1044 if (!err && opt_io_size)
1045 blk_queue_io_opt(q, blk_size * opt_io_size);
1047 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
1048 virtio_cread(vdev, struct virtio_blk_config,
1049 discard_sector_alignment, &v);
1051 q->limits.discard_granularity = v << SECTOR_SHIFT;
1053 q->limits.discard_granularity = blk_size;
1055 virtio_cread(vdev, struct virtio_blk_config,
1056 max_discard_sectors, &v);
1057 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
1059 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
1063 * max_discard_seg == 0 is out of spec but we always
1068 blk_queue_max_discard_segments(q,
1069 min(v, MAX_DISCARD_SEGMENTS));
1072 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
1073 virtio_cread(vdev, struct virtio_blk_config,
1074 max_write_zeroes_sectors, &v);
1075 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
1078 virtblk_update_capacity(vblk, false);
1079 virtio_device_ready(vdev);
1081 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
1083 goto out_cleanup_disk;
1088 put_disk(vblk->disk);
1090 blk_mq_free_tag_set(&vblk->tag_set);
1092 vdev->config->del_vqs(vdev);
1097 ida_simple_remove(&vd_index_ida, index);
1102 static void virtblk_remove(struct virtio_device *vdev)
1104 struct virtio_blk *vblk = vdev->priv;
1106 /* Make sure no work handler is accessing the device. */
1107 flush_work(&vblk->config_work);
1109 del_gendisk(vblk->disk);
1110 blk_mq_free_tag_set(&vblk->tag_set);
1112 mutex_lock(&vblk->vdev_mutex);
1114 /* Stop all the virtqueues. */
1115 virtio_reset_device(vdev);
1117 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
1120 vdev->config->del_vqs(vdev);
1123 mutex_unlock(&vblk->vdev_mutex);
1125 put_disk(vblk->disk);
1128 #ifdef CONFIG_PM_SLEEP
1129 static int virtblk_freeze(struct virtio_device *vdev)
1131 struct virtio_blk *vblk = vdev->priv;
1133 /* Ensure we don't receive any more interrupts */
1134 virtio_reset_device(vdev);
1136 /* Make sure no work handler is accessing the device. */
1137 flush_work(&vblk->config_work);
1139 blk_mq_quiesce_queue(vblk->disk->queue);
1141 vdev->config->del_vqs(vdev);
1147 static int virtblk_restore(struct virtio_device *vdev)
1149 struct virtio_blk *vblk = vdev->priv;
1152 ret = init_vq(vdev->priv);
1156 virtio_device_ready(vdev);
1158 blk_mq_unquiesce_queue(vblk->disk->queue);
1163 static const struct virtio_device_id id_table[] = {
1164 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1168 static unsigned int features_legacy[] = {
1169 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1170 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1171 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1172 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1175 static unsigned int features[] = {
1176 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1177 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1178 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1179 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1182 static struct virtio_driver virtio_blk = {
1183 .feature_table = features,
1184 .feature_table_size = ARRAY_SIZE(features),
1185 .feature_table_legacy = features_legacy,
1186 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1187 .driver.name = KBUILD_MODNAME,
1188 .driver.owner = THIS_MODULE,
1189 .id_table = id_table,
1190 .probe = virtblk_probe,
1191 .remove = virtblk_remove,
1192 .config_changed = virtblk_config_changed,
1193 #ifdef CONFIG_PM_SLEEP
1194 .freeze = virtblk_freeze,
1195 .restore = virtblk_restore,
1199 static int __init virtio_blk_init(void)
1203 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1207 major = register_blkdev(0, "virtblk");
1210 goto out_destroy_workqueue;
1213 error = register_virtio_driver(&virtio_blk);
1215 goto out_unregister_blkdev;
1218 out_unregister_blkdev:
1219 unregister_blkdev(major, "virtblk");
1220 out_destroy_workqueue:
1221 destroy_workqueue(virtblk_wq);
1225 static void __exit virtio_blk_fini(void)
1227 unregister_virtio_driver(&virtio_blk);
1228 unregister_blkdev(major, "virtblk");
1229 destroy_workqueue(virtblk_wq);
1231 module_init(virtio_blk_init);
1232 module_exit(virtio_blk_fini);
1234 MODULE_DEVICE_TABLE(virtio, id_table);
1235 MODULE_DESCRIPTION("Virtio block driver");
1236 MODULE_LICENSE("GPL");