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 int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
106 struct scatterlist hdr, status, *sgs[3];
107 unsigned int num_out = 0, num_in = 0;
109 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
110 sgs[num_out++] = &hdr;
112 if (vbr->sg_table.nents) {
113 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
114 sgs[num_out++] = vbr->sg_table.sgl;
116 sgs[num_out + num_in++] = vbr->sg_table.sgl;
119 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
120 sgs[num_out + num_in++] = &status;
122 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
125 static int virtblk_setup_discard_write_zeroes(struct request *req, bool unmap)
127 unsigned short segments = blk_rq_nr_discard_segments(req);
128 unsigned short n = 0;
129 struct virtio_blk_discard_write_zeroes *range;
134 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
136 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
141 * Single max discard segment means multi-range discard isn't
142 * supported, and block layer only runs contiguity merge like
143 * normal RW request. So we can't reply on bio for retrieving
146 if (queue_max_discard_segments(req->q) == 1) {
147 range[0].flags = cpu_to_le32(flags);
148 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
149 range[0].sector = cpu_to_le64(blk_rq_pos(req));
152 __rq_for_each_bio(bio, req) {
153 u64 sector = bio->bi_iter.bi_sector;
154 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
156 range[n].flags = cpu_to_le32(flags);
157 range[n].num_sectors = cpu_to_le32(num_sectors);
158 range[n].sector = cpu_to_le64(sector);
163 WARN_ON_ONCE(n != segments);
165 req->special_vec.bv_page = virt_to_page(range);
166 req->special_vec.bv_offset = offset_in_page(range);
167 req->special_vec.bv_len = sizeof(*range) * segments;
168 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
173 static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
175 if (blk_rq_nr_phys_segments(req))
176 sg_free_table_chained(&vbr->sg_table,
177 VIRTIO_BLK_INLINE_SG_CNT);
180 static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
181 struct virtblk_req *vbr)
185 if (!blk_rq_nr_phys_segments(req))
188 vbr->sg_table.sgl = vbr->sg;
189 err = sg_alloc_table_chained(&vbr->sg_table,
190 blk_rq_nr_phys_segments(req),
192 VIRTIO_BLK_INLINE_SG_CNT);
196 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
199 static void virtblk_cleanup_cmd(struct request *req)
201 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
202 kfree(bvec_virt(&req->special_vec));
205 static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
207 struct virtblk_req *vbr)
212 vbr->out_hdr.sector = 0;
214 switch (req_op(req)) {
216 type = VIRTIO_BLK_T_IN;
217 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
221 type = VIRTIO_BLK_T_OUT;
222 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
226 type = VIRTIO_BLK_T_FLUSH;
229 type = VIRTIO_BLK_T_DISCARD;
231 case REQ_OP_WRITE_ZEROES:
232 type = VIRTIO_BLK_T_WRITE_ZEROES;
233 unmap = !(req->cmd_flags & REQ_NOUNMAP);
236 type = VIRTIO_BLK_T_GET_ID;
240 return BLK_STS_IOERR;
243 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
244 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
246 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
247 if (virtblk_setup_discard_write_zeroes(req, unmap))
248 return BLK_STS_RESOURCE;
254 static inline void virtblk_request_done(struct request *req)
256 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
258 virtblk_unmap_data(req, vbr);
259 virtblk_cleanup_cmd(req);
260 blk_mq_end_request(req, virtblk_result(vbr));
263 static void virtblk_done(struct virtqueue *vq)
265 struct virtio_blk *vblk = vq->vdev->priv;
266 bool req_done = false;
268 struct virtblk_req *vbr;
272 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
274 virtqueue_disable_cb(vq);
275 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
276 struct request *req = blk_mq_rq_from_pdu(vbr);
278 if (likely(!blk_should_fake_timeout(req->q)))
279 blk_mq_complete_request(req);
282 if (unlikely(virtqueue_is_broken(vq)))
284 } while (!virtqueue_enable_cb(vq));
286 /* In case queue is stopped waiting for more buffers. */
288 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
289 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
292 static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
294 struct virtio_blk *vblk = hctx->queue->queuedata;
295 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
298 spin_lock_irq(&vq->lock);
299 kick = virtqueue_kick_prepare(vq->vq);
300 spin_unlock_irq(&vq->lock);
303 virtqueue_notify(vq->vq);
306 static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
307 struct virtio_blk *vblk,
309 struct virtblk_req *vbr)
313 status = virtblk_setup_cmd(vblk->vdev, req, vbr);
314 if (unlikely(status))
317 blk_mq_start_request(req);
319 vbr->sg_table.nents = virtblk_map_data(hctx, req, vbr);
320 if (unlikely(vbr->sg_table.nents < 0)) {
321 virtblk_cleanup_cmd(req);
322 return BLK_STS_RESOURCE;
328 static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
329 const struct blk_mq_queue_data *bd)
331 struct virtio_blk *vblk = hctx->queue->queuedata;
332 struct request *req = bd->rq;
333 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
335 int qid = hctx->queue_num;
340 status = virtblk_prep_rq(hctx, vblk, req, vbr);
341 if (unlikely(status))
344 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
345 err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
347 virtqueue_kick(vblk->vqs[qid].vq);
348 /* Don't stop the queue if -ENOMEM: we may have failed to
349 * bounce the buffer due to global resource outage.
352 blk_mq_stop_hw_queue(hctx);
353 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
354 virtblk_unmap_data(req, vbr);
355 virtblk_cleanup_cmd(req);
358 return BLK_STS_DEV_RESOURCE;
360 return BLK_STS_RESOURCE;
362 return BLK_STS_IOERR;
366 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
368 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
371 virtqueue_notify(vblk->vqs[qid].vq);
375 static bool virtblk_prep_rq_batch(struct request *req)
377 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
378 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
380 req->mq_hctx->tags->rqs[req->tag] = req;
382 return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
385 static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
386 struct request **rqlist,
387 struct request **requeue_list)
393 spin_lock_irqsave(&vq->lock, flags);
395 while (!rq_list_empty(*rqlist)) {
396 struct request *req = rq_list_pop(rqlist);
397 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
399 err = virtblk_add_req(vq->vq, vbr);
401 virtblk_unmap_data(req, vbr);
402 virtblk_cleanup_cmd(req);
403 rq_list_add(requeue_list, req);
407 kick = virtqueue_kick_prepare(vq->vq);
408 spin_unlock_irqrestore(&vq->lock, flags);
413 static void virtio_queue_rqs(struct request **rqlist)
415 struct request *req, *next, *prev = NULL;
416 struct request *requeue_list = NULL;
418 rq_list_for_each_safe(rqlist, req, next) {
419 struct virtio_blk_vq *vq = req->mq_hctx->driver_data;
422 if (!virtblk_prep_rq_batch(req)) {
423 rq_list_move(rqlist, &requeue_list, req, prev);
429 if (!next || req->mq_hctx != next->mq_hctx) {
431 kick = virtblk_add_req_batch(vq, rqlist, &requeue_list);
433 virtqueue_notify(vq->vq);
441 *rqlist = requeue_list;
444 /* return id (s/n) string for *disk to *id_str
446 static int virtblk_get_id(struct gendisk *disk, char *id_str)
448 struct virtio_blk *vblk = disk->private_data;
449 struct request_queue *q = vblk->disk->queue;
453 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
457 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
461 blk_execute_rq(req, false);
462 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
464 blk_mq_free_request(req);
468 /* We provide getgeo only to please some old bootloader/partitioning tools */
469 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
471 struct virtio_blk *vblk = bd->bd_disk->private_data;
474 mutex_lock(&vblk->vdev_mutex);
481 /* see if the host passed in geometry config */
482 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
483 virtio_cread(vblk->vdev, struct virtio_blk_config,
484 geometry.cylinders, &geo->cylinders);
485 virtio_cread(vblk->vdev, struct virtio_blk_config,
486 geometry.heads, &geo->heads);
487 virtio_cread(vblk->vdev, struct virtio_blk_config,
488 geometry.sectors, &geo->sectors);
490 /* some standard values, similar to sd */
492 geo->sectors = 1 << 5;
493 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
496 mutex_unlock(&vblk->vdev_mutex);
500 static void virtblk_free_disk(struct gendisk *disk)
502 struct virtio_blk *vblk = disk->private_data;
504 ida_simple_remove(&vd_index_ida, vblk->index);
505 mutex_destroy(&vblk->vdev_mutex);
509 static const struct block_device_operations virtblk_fops = {
510 .owner = THIS_MODULE,
511 .getgeo = virtblk_getgeo,
512 .free_disk = virtblk_free_disk,
515 static int index_to_minor(int index)
517 return index << PART_BITS;
520 static int minor_to_index(int minor)
522 return minor >> PART_BITS;
525 static ssize_t serial_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
528 struct gendisk *disk = dev_to_disk(dev);
531 /* sysfs gives us a PAGE_SIZE buffer */
532 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
534 buf[VIRTIO_BLK_ID_BYTES] = '\0';
535 err = virtblk_get_id(disk, buf);
539 if (err == -EIO) /* Unsupported? Make it empty. */
545 static DEVICE_ATTR_RO(serial);
547 /* The queue's logical block size must be set before calling this */
548 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
550 struct virtio_device *vdev = vblk->vdev;
551 struct request_queue *q = vblk->disk->queue;
552 char cap_str_2[10], cap_str_10[10];
553 unsigned long long nblocks;
556 /* Host must always specify the capacity. */
557 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
559 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
561 string_get_size(nblocks, queue_logical_block_size(q),
562 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
563 string_get_size(nblocks, queue_logical_block_size(q),
564 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
566 dev_notice(&vdev->dev,
567 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
568 vblk->disk->disk_name,
569 resize ? "new size: " : "",
571 queue_logical_block_size(q),
575 set_capacity_and_notify(vblk->disk, capacity);
578 static void virtblk_config_changed_work(struct work_struct *work)
580 struct virtio_blk *vblk =
581 container_of(work, struct virtio_blk, config_work);
583 virtblk_update_capacity(vblk, true);
586 static void virtblk_config_changed(struct virtio_device *vdev)
588 struct virtio_blk *vblk = vdev->priv;
590 queue_work(virtblk_wq, &vblk->config_work);
593 static int init_vq(struct virtio_blk *vblk)
597 vq_callback_t **callbacks;
599 struct virtqueue **vqs;
600 unsigned short num_vqs;
601 unsigned int num_poll_vqs;
602 struct virtio_device *vdev = vblk->vdev;
603 struct irq_affinity desc = { 0, };
605 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
606 struct virtio_blk_config, num_queues,
611 if (!err && !num_vqs) {
612 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
616 num_vqs = min_t(unsigned int,
617 min_not_zero(num_request_queues, nr_cpu_ids),
620 num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
622 vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
623 vblk->io_queues[HCTX_TYPE_READ] = 0;
624 vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
626 dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
627 vblk->io_queues[HCTX_TYPE_DEFAULT],
628 vblk->io_queues[HCTX_TYPE_READ],
629 vblk->io_queues[HCTX_TYPE_POLL]);
631 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
635 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
636 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
637 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
638 if (!names || !callbacks || !vqs) {
643 for (i = 0; i < num_vqs - num_poll_vqs; i++) {
644 callbacks[i] = virtblk_done;
645 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
646 names[i] = vblk->vqs[i].name;
649 for (; i < num_vqs; i++) {
651 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
652 names[i] = vblk->vqs[i].name;
655 /* Discover virtqueues and write information to configuration. */
656 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
660 for (i = 0; i < num_vqs; i++) {
661 spin_lock_init(&vblk->vqs[i].lock);
662 vblk->vqs[i].vq = vqs[i];
664 vblk->num_vqs = num_vqs;
676 * Legacy naming scheme used for virtio devices. We are stuck with it for
677 * virtio blk but don't ever use it for any new driver.
679 static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
681 const int base = 'z' - 'a' + 1;
682 char *begin = buf + strlen(prefix);
683 char *end = buf + buflen;
693 *--p = 'a' + (index % unit);
694 index = (index / unit) - 1;
695 } while (index >= 0);
697 memmove(begin, p, end - p);
698 memcpy(buf, prefix, strlen(prefix));
703 static int virtblk_get_cache_mode(struct virtio_device *vdev)
708 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
709 struct virtio_blk_config, wce,
713 * If WCE is not configurable and flush is not available,
714 * assume no writeback cache is in use.
717 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
722 static void virtblk_update_cache_mode(struct virtio_device *vdev)
724 u8 writeback = virtblk_get_cache_mode(vdev);
725 struct virtio_blk *vblk = vdev->priv;
727 blk_queue_write_cache(vblk->disk->queue, writeback, false);
730 static const char *const virtblk_cache_types[] = {
731 "write through", "write back"
735 cache_type_store(struct device *dev, struct device_attribute *attr,
736 const char *buf, size_t count)
738 struct gendisk *disk = dev_to_disk(dev);
739 struct virtio_blk *vblk = disk->private_data;
740 struct virtio_device *vdev = vblk->vdev;
743 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
744 i = sysfs_match_string(virtblk_cache_types, buf);
748 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
749 virtblk_update_cache_mode(vdev);
754 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
756 struct gendisk *disk = dev_to_disk(dev);
757 struct virtio_blk *vblk = disk->private_data;
758 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
760 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
761 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
764 static DEVICE_ATTR_RW(cache_type);
766 static struct attribute *virtblk_attrs[] = {
767 &dev_attr_serial.attr,
768 &dev_attr_cache_type.attr,
772 static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
773 struct attribute *a, int n)
775 struct device *dev = kobj_to_dev(kobj);
776 struct gendisk *disk = dev_to_disk(dev);
777 struct virtio_blk *vblk = disk->private_data;
778 struct virtio_device *vdev = vblk->vdev;
780 if (a == &dev_attr_cache_type.attr &&
781 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
787 static const struct attribute_group virtblk_attr_group = {
788 .attrs = virtblk_attrs,
789 .is_visible = virtblk_attrs_are_visible,
792 static const struct attribute_group *virtblk_attr_groups[] = {
797 static int virtblk_map_queues(struct blk_mq_tag_set *set)
799 struct virtio_blk *vblk = set->driver_data;
802 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
803 struct blk_mq_queue_map *map = &set->map[i];
805 map->nr_queues = vblk->io_queues[i];
806 map->queue_offset = qoff;
807 qoff += map->nr_queues;
809 if (map->nr_queues == 0)
813 * Regular queues have interrupts and hence CPU affinity is
814 * defined by the core virtio code, but polling queues have
815 * no interrupts so we let the block layer assign CPU affinity.
817 if (i == HCTX_TYPE_POLL)
818 blk_mq_map_queues(&set->map[i]);
820 blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
826 static void virtblk_complete_batch(struct io_comp_batch *iob)
830 rq_list_for_each(&iob->req_list, req) {
831 virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
832 virtblk_cleanup_cmd(req);
834 blk_mq_end_request_batch(iob);
837 static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
839 struct virtio_blk *vblk = hctx->queue->queuedata;
840 struct virtio_blk_vq *vq = hctx->driver_data;
841 struct virtblk_req *vbr;
846 spin_lock_irqsave(&vq->lock, flags);
848 while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
849 struct request *req = blk_mq_rq_from_pdu(vbr);
852 if (!blk_mq_add_to_batch(req, iob, vbr->status,
853 virtblk_complete_batch))
854 blk_mq_complete_request(req);
858 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
860 spin_unlock_irqrestore(&vq->lock, flags);
865 static int virtblk_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
866 unsigned int hctx_idx)
868 struct virtio_blk *vblk = data;
869 struct virtio_blk_vq *vq = &vblk->vqs[hctx_idx];
871 WARN_ON(vblk->tag_set.tags[hctx_idx] != hctx->tags);
872 hctx->driver_data = vq;
876 static const struct blk_mq_ops virtio_mq_ops = {
877 .queue_rq = virtio_queue_rq,
878 .queue_rqs = virtio_queue_rqs,
879 .commit_rqs = virtio_commit_rqs,
880 .init_hctx = virtblk_init_hctx,
881 .complete = virtblk_request_done,
882 .map_queues = virtblk_map_queues,
883 .poll = virtblk_poll,
886 static unsigned int virtblk_queue_depth;
887 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
889 static int virtblk_probe(struct virtio_device *vdev)
891 struct virtio_blk *vblk;
892 struct request_queue *q;
895 u32 v, blk_size, max_size, sg_elems, opt_io_size;
897 u8 physical_block_exp, alignment_offset;
898 unsigned int queue_depth;
900 if (!vdev->config->get) {
901 dev_err(&vdev->dev, "%s failure: config access disabled\n",
906 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
912 /* We need to know how many segments before we allocate. */
913 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
914 struct virtio_blk_config, seg_max,
917 /* We need at least one SG element, whatever they say. */
918 if (err || !sg_elems)
921 /* Prevent integer overflows and honor max vq size */
922 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
924 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
930 mutex_init(&vblk->vdev_mutex);
934 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
940 /* Default queue sizing is to fill the ring. */
941 if (!virtblk_queue_depth) {
942 queue_depth = vblk->vqs[0].vq->num_free;
943 /* ... but without indirect descs, we use 2 descs per req */
944 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
947 queue_depth = virtblk_queue_depth;
950 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
951 vblk->tag_set.ops = &virtio_mq_ops;
952 vblk->tag_set.queue_depth = queue_depth;
953 vblk->tag_set.numa_node = NUMA_NO_NODE;
954 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
955 vblk->tag_set.cmd_size =
956 sizeof(struct virtblk_req) +
957 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
958 vblk->tag_set.driver_data = vblk;
959 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
960 vblk->tag_set.nr_maps = 1;
961 if (vblk->io_queues[HCTX_TYPE_POLL])
962 vblk->tag_set.nr_maps = 3;
964 err = blk_mq_alloc_tag_set(&vblk->tag_set);
968 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
969 if (IS_ERR(vblk->disk)) {
970 err = PTR_ERR(vblk->disk);
973 q = vblk->disk->queue;
975 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
977 vblk->disk->major = major;
978 vblk->disk->first_minor = index_to_minor(index);
979 vblk->disk->minors = 1 << PART_BITS;
980 vblk->disk->private_data = vblk;
981 vblk->disk->fops = &virtblk_fops;
984 /* configure queue flush support */
985 virtblk_update_cache_mode(vdev);
987 /* If disk is read-only in the host, the guest should obey */
988 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
989 set_disk_ro(vblk->disk, 1);
991 /* We can handle whatever the host told us to handle. */
992 blk_queue_max_segments(q, sg_elems);
994 /* No real sector limit. */
995 blk_queue_max_hw_sectors(q, -1U);
997 max_size = virtio_max_dma_size(vdev);
999 /* Host can optionally specify maximum segment size and number of
1001 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
1002 struct virtio_blk_config, size_max, &v);
1004 max_size = min(max_size, v);
1006 blk_queue_max_segment_size(q, max_size);
1008 /* Host can optionally specify the block size of the device */
1009 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
1010 struct virtio_blk_config, blk_size,
1013 err = blk_validate_block_size(blk_size);
1016 "virtio_blk: invalid block size: 0x%x\n",
1018 goto out_cleanup_disk;
1021 blk_queue_logical_block_size(q, blk_size);
1023 blk_size = queue_logical_block_size(q);
1025 /* Use topology information if available */
1026 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1027 struct virtio_blk_config, physical_block_exp,
1028 &physical_block_exp);
1029 if (!err && physical_block_exp)
1030 blk_queue_physical_block_size(q,
1031 blk_size * (1 << physical_block_exp));
1033 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1034 struct virtio_blk_config, alignment_offset,
1036 if (!err && alignment_offset)
1037 blk_queue_alignment_offset(q, blk_size * alignment_offset);
1039 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1040 struct virtio_blk_config, min_io_size,
1042 if (!err && min_io_size)
1043 blk_queue_io_min(q, blk_size * min_io_size);
1045 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1046 struct virtio_blk_config, opt_io_size,
1048 if (!err && opt_io_size)
1049 blk_queue_io_opt(q, blk_size * opt_io_size);
1051 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
1052 virtio_cread(vdev, struct virtio_blk_config,
1053 discard_sector_alignment, &v);
1055 q->limits.discard_granularity = v << SECTOR_SHIFT;
1057 q->limits.discard_granularity = blk_size;
1059 virtio_cread(vdev, struct virtio_blk_config,
1060 max_discard_sectors, &v);
1061 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
1063 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
1067 * max_discard_seg == 0 is out of spec but we always
1072 blk_queue_max_discard_segments(q,
1073 min(v, MAX_DISCARD_SEGMENTS));
1076 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
1077 virtio_cread(vdev, struct virtio_blk_config,
1078 max_write_zeroes_sectors, &v);
1079 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
1082 virtblk_update_capacity(vblk, false);
1083 virtio_device_ready(vdev);
1085 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
1087 goto out_cleanup_disk;
1092 blk_cleanup_disk(vblk->disk);
1094 blk_mq_free_tag_set(&vblk->tag_set);
1096 vdev->config->del_vqs(vdev);
1101 ida_simple_remove(&vd_index_ida, index);
1106 static void virtblk_remove(struct virtio_device *vdev)
1108 struct virtio_blk *vblk = vdev->priv;
1110 /* Make sure no work handler is accessing the device. */
1111 flush_work(&vblk->config_work);
1113 del_gendisk(vblk->disk);
1114 blk_cleanup_queue(vblk->disk->queue);
1115 blk_mq_free_tag_set(&vblk->tag_set);
1117 mutex_lock(&vblk->vdev_mutex);
1119 /* Stop all the virtqueues. */
1120 virtio_reset_device(vdev);
1122 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
1125 vdev->config->del_vqs(vdev);
1128 mutex_unlock(&vblk->vdev_mutex);
1130 put_disk(vblk->disk);
1133 #ifdef CONFIG_PM_SLEEP
1134 static int virtblk_freeze(struct virtio_device *vdev)
1136 struct virtio_blk *vblk = vdev->priv;
1138 /* Ensure we don't receive any more interrupts */
1139 virtio_reset_device(vdev);
1141 /* Make sure no work handler is accessing the device. */
1142 flush_work(&vblk->config_work);
1144 blk_mq_quiesce_queue(vblk->disk->queue);
1146 vdev->config->del_vqs(vdev);
1152 static int virtblk_restore(struct virtio_device *vdev)
1154 struct virtio_blk *vblk = vdev->priv;
1157 ret = init_vq(vdev->priv);
1161 virtio_device_ready(vdev);
1163 blk_mq_unquiesce_queue(vblk->disk->queue);
1168 static const struct virtio_device_id id_table[] = {
1169 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1173 static unsigned int features_legacy[] = {
1174 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1175 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1176 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1177 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1180 static unsigned int features[] = {
1181 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1182 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1183 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1184 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1187 static struct virtio_driver virtio_blk = {
1188 .feature_table = features,
1189 .feature_table_size = ARRAY_SIZE(features),
1190 .feature_table_legacy = features_legacy,
1191 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1192 .driver.name = KBUILD_MODNAME,
1193 .driver.owner = THIS_MODULE,
1194 .id_table = id_table,
1195 .probe = virtblk_probe,
1196 .remove = virtblk_remove,
1197 .config_changed = virtblk_config_changed,
1198 #ifdef CONFIG_PM_SLEEP
1199 .freeze = virtblk_freeze,
1200 .restore = virtblk_restore,
1204 static int __init virtio_blk_init(void)
1208 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1212 major = register_blkdev(0, "virtblk");
1215 goto out_destroy_workqueue;
1218 error = register_virtio_driver(&virtio_blk);
1220 goto out_unregister_blkdev;
1223 out_unregister_blkdev:
1224 unregister_blkdev(major, "virtblk");
1225 out_destroy_workqueue:
1226 destroy_workqueue(virtblk_wq);
1230 static void __exit virtio_blk_fini(void)
1232 unregister_virtio_driver(&virtio_blk);
1233 unregister_blkdev(major, "virtblk");
1234 destroy_workqueue(virtblk_wq);
1236 module_init(virtio_blk_init);
1237 module_exit(virtio_blk_fini);
1239 MODULE_DEVICE_TABLE(virtio, id_table);
1240 MODULE_DESCRIPTION("Virtio block driver");
1241 MODULE_LICENSE("GPL");