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
25 static DEFINE_IDA(vd_index_ida);
27 static struct workqueue_struct *virtblk_wq;
29 struct virtio_blk_vq {
32 char name[VQ_NAME_LEN];
33 } ____cacheline_aligned_in_smp;
36 struct virtio_device *vdev;
38 /* The disk structure for the kernel. */
41 /* Block layer tags. */
42 struct blk_mq_tag_set tag_set;
44 /* Process context for config space updates */
45 struct work_struct config_work;
47 /* What host tells us, plus 2 for header & tailer. */
48 unsigned int sg_elems;
50 /* Ida index - used to track minor number allocations. */
55 struct virtio_blk_vq *vqs;
59 struct virtio_blk_outhdr out_hdr;
61 struct scatterlist sg[];
64 static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
66 switch (vbr->status) {
69 case VIRTIO_BLK_S_UNSUPP:
70 return BLK_STS_NOTSUPP;
76 static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
77 struct scatterlist *data_sg, bool have_data)
79 struct scatterlist hdr, status, *sgs[3];
80 unsigned int num_out = 0, num_in = 0;
82 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
83 sgs[num_out++] = &hdr;
86 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
87 sgs[num_out++] = data_sg;
89 sgs[num_out + num_in++] = data_sg;
92 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
93 sgs[num_out + num_in++] = &status;
95 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
98 static int virtblk_setup_discard_write_zeroes(struct request *req, bool unmap)
100 unsigned short segments = blk_rq_nr_discard_segments(req);
101 unsigned short n = 0;
102 struct virtio_blk_discard_write_zeroes *range;
107 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
109 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
113 __rq_for_each_bio(bio, req) {
114 u64 sector = bio->bi_iter.bi_sector;
115 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
117 range[n].flags = cpu_to_le32(flags);
118 range[n].num_sectors = cpu_to_le32(num_sectors);
119 range[n].sector = cpu_to_le64(sector);
123 req->special_vec.bv_page = virt_to_page(range);
124 req->special_vec.bv_offset = offset_in_page(range);
125 req->special_vec.bv_len = sizeof(*range) * segments;
126 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
131 static inline void virtblk_request_done(struct request *req)
133 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
135 if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
136 kfree(page_address(req->special_vec.bv_page) +
137 req->special_vec.bv_offset);
140 blk_mq_end_request(req, virtblk_result(vbr));
143 static void virtblk_done(struct virtqueue *vq)
145 struct virtio_blk *vblk = vq->vdev->priv;
146 bool req_done = false;
148 struct virtblk_req *vbr;
152 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
154 virtqueue_disable_cb(vq);
155 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
156 struct request *req = blk_mq_rq_from_pdu(vbr);
158 blk_mq_complete_request(req);
161 if (unlikely(virtqueue_is_broken(vq)))
163 } while (!virtqueue_enable_cb(vq));
165 /* In case queue is stopped waiting for more buffers. */
167 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
168 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
171 static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
173 struct virtio_blk *vblk = hctx->queue->queuedata;
174 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
177 spin_lock_irq(&vq->lock);
178 kick = virtqueue_kick_prepare(vq->vq);
179 spin_unlock_irq(&vq->lock);
182 virtqueue_notify(vq->vq);
185 static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
186 const struct blk_mq_queue_data *bd)
188 struct virtio_blk *vblk = hctx->queue->queuedata;
189 struct request *req = bd->rq;
190 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
193 int qid = hctx->queue_num;
199 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
201 switch (req_op(req)) {
207 type = VIRTIO_BLK_T_FLUSH;
210 type = VIRTIO_BLK_T_DISCARD;
212 case REQ_OP_WRITE_ZEROES:
213 type = VIRTIO_BLK_T_WRITE_ZEROES;
214 unmap = !(req->cmd_flags & REQ_NOUNMAP);
217 type = VIRTIO_BLK_T_GET_ID;
221 return BLK_STS_IOERR;
224 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, type);
225 vbr->out_hdr.sector = type ?
226 0 : cpu_to_virtio64(vblk->vdev, blk_rq_pos(req));
227 vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
229 blk_mq_start_request(req);
231 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
232 err = virtblk_setup_discard_write_zeroes(req, unmap);
234 return BLK_STS_RESOURCE;
237 num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
239 if (rq_data_dir(req) == WRITE)
240 vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
242 vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
245 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
246 err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg, num);
248 virtqueue_kick(vblk->vqs[qid].vq);
249 /* Don't stop the queue if -ENOMEM: we may have failed to
250 * bounce the buffer due to global resource outage.
253 blk_mq_stop_hw_queue(hctx);
254 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
257 return BLK_STS_DEV_RESOURCE;
259 return BLK_STS_RESOURCE;
261 return BLK_STS_IOERR;
265 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
267 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
270 virtqueue_notify(vblk->vqs[qid].vq);
274 /* return id (s/n) string for *disk to *id_str
276 static int virtblk_get_id(struct gendisk *disk, char *id_str)
278 struct virtio_blk *vblk = disk->private_data;
279 struct request_queue *q = vblk->disk->queue;
283 req = blk_get_request(q, REQ_OP_DRV_IN, 0);
287 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
291 blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
292 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
294 blk_put_request(req);
298 /* We provide getgeo only to please some old bootloader/partitioning tools */
299 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
301 struct virtio_blk *vblk = bd->bd_disk->private_data;
303 /* see if the host passed in geometry config */
304 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
305 virtio_cread(vblk->vdev, struct virtio_blk_config,
306 geometry.cylinders, &geo->cylinders);
307 virtio_cread(vblk->vdev, struct virtio_blk_config,
308 geometry.heads, &geo->heads);
309 virtio_cread(vblk->vdev, struct virtio_blk_config,
310 geometry.sectors, &geo->sectors);
312 /* some standard values, similar to sd */
314 geo->sectors = 1 << 5;
315 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
320 static const struct block_device_operations virtblk_fops = {
321 .owner = THIS_MODULE,
322 .getgeo = virtblk_getgeo,
325 static int index_to_minor(int index)
327 return index << PART_BITS;
330 static int minor_to_index(int minor)
332 return minor >> PART_BITS;
335 static ssize_t serial_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct gendisk *disk = dev_to_disk(dev);
341 /* sysfs gives us a PAGE_SIZE buffer */
342 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
344 buf[VIRTIO_BLK_ID_BYTES] = '\0';
345 err = virtblk_get_id(disk, buf);
349 if (err == -EIO) /* Unsupported? Make it empty. */
355 static DEVICE_ATTR_RO(serial);
357 /* The queue's logical block size must be set before calling this */
358 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
360 struct virtio_device *vdev = vblk->vdev;
361 struct request_queue *q = vblk->disk->queue;
362 char cap_str_2[10], cap_str_10[10];
363 unsigned long long nblocks;
366 /* Host must always specify the capacity. */
367 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
369 /* If capacity is too big, truncate with warning. */
370 if ((sector_t)capacity != capacity) {
371 dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
372 (unsigned long long)capacity);
373 capacity = (sector_t)-1;
376 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
378 string_get_size(nblocks, queue_logical_block_size(q),
379 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
380 string_get_size(nblocks, queue_logical_block_size(q),
381 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
383 dev_notice(&vdev->dev,
384 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
385 vblk->disk->disk_name,
386 resize ? "new size: " : "",
388 queue_logical_block_size(q),
392 set_capacity_revalidate_and_notify(vblk->disk, capacity, true);
395 static void virtblk_config_changed_work(struct work_struct *work)
397 struct virtio_blk *vblk =
398 container_of(work, struct virtio_blk, config_work);
400 virtblk_update_capacity(vblk, true);
403 static void virtblk_config_changed(struct virtio_device *vdev)
405 struct virtio_blk *vblk = vdev->priv;
407 queue_work(virtblk_wq, &vblk->config_work);
410 static int init_vq(struct virtio_blk *vblk)
414 vq_callback_t **callbacks;
416 struct virtqueue **vqs;
417 unsigned short num_vqs;
418 struct virtio_device *vdev = vblk->vdev;
419 struct irq_affinity desc = { 0, };
421 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
422 struct virtio_blk_config, num_queues,
427 num_vqs = min_t(unsigned int, nr_cpu_ids, num_vqs);
429 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
433 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
434 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
435 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
436 if (!names || !callbacks || !vqs) {
441 for (i = 0; i < num_vqs; i++) {
442 callbacks[i] = virtblk_done;
443 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
444 names[i] = vblk->vqs[i].name;
447 /* Discover virtqueues and write information to configuration. */
448 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
452 for (i = 0; i < num_vqs; i++) {
453 spin_lock_init(&vblk->vqs[i].lock);
454 vblk->vqs[i].vq = vqs[i];
456 vblk->num_vqs = num_vqs;
468 * Legacy naming scheme used for virtio devices. We are stuck with it for
469 * virtio blk but don't ever use it for any new driver.
471 static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
473 const int base = 'z' - 'a' + 1;
474 char *begin = buf + strlen(prefix);
475 char *end = buf + buflen;
485 *--p = 'a' + (index % unit);
486 index = (index / unit) - 1;
487 } while (index >= 0);
489 memmove(begin, p, end - p);
490 memcpy(buf, prefix, strlen(prefix));
495 static int virtblk_get_cache_mode(struct virtio_device *vdev)
500 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
501 struct virtio_blk_config, wce,
505 * If WCE is not configurable and flush is not available,
506 * assume no writeback cache is in use.
509 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
514 static void virtblk_update_cache_mode(struct virtio_device *vdev)
516 u8 writeback = virtblk_get_cache_mode(vdev);
517 struct virtio_blk *vblk = vdev->priv;
519 blk_queue_write_cache(vblk->disk->queue, writeback, false);
520 revalidate_disk(vblk->disk);
523 static const char *const virtblk_cache_types[] = {
524 "write through", "write back"
528 cache_type_store(struct device *dev, struct device_attribute *attr,
529 const char *buf, size_t count)
531 struct gendisk *disk = dev_to_disk(dev);
532 struct virtio_blk *vblk = disk->private_data;
533 struct virtio_device *vdev = vblk->vdev;
536 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
537 i = sysfs_match_string(virtblk_cache_types, buf);
541 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
542 virtblk_update_cache_mode(vdev);
547 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
549 struct gendisk *disk = dev_to_disk(dev);
550 struct virtio_blk *vblk = disk->private_data;
551 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
553 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
554 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
557 static DEVICE_ATTR_RW(cache_type);
559 static struct attribute *virtblk_attrs[] = {
560 &dev_attr_serial.attr,
561 &dev_attr_cache_type.attr,
565 static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
566 struct attribute *a, int n)
568 struct device *dev = container_of(kobj, struct device, kobj);
569 struct gendisk *disk = dev_to_disk(dev);
570 struct virtio_blk *vblk = disk->private_data;
571 struct virtio_device *vdev = vblk->vdev;
573 if (a == &dev_attr_cache_type.attr &&
574 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
580 static const struct attribute_group virtblk_attr_group = {
581 .attrs = virtblk_attrs,
582 .is_visible = virtblk_attrs_are_visible,
585 static const struct attribute_group *virtblk_attr_groups[] = {
590 static int virtblk_init_request(struct blk_mq_tag_set *set, struct request *rq,
591 unsigned int hctx_idx, unsigned int numa_node)
593 struct virtio_blk *vblk = set->driver_data;
594 struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
596 sg_init_table(vbr->sg, vblk->sg_elems);
600 static int virtblk_map_queues(struct blk_mq_tag_set *set)
602 struct virtio_blk *vblk = set->driver_data;
604 return blk_mq_virtio_map_queues(&set->map[HCTX_TYPE_DEFAULT],
608 static const struct blk_mq_ops virtio_mq_ops = {
609 .queue_rq = virtio_queue_rq,
610 .commit_rqs = virtio_commit_rqs,
611 .complete = virtblk_request_done,
612 .init_request = virtblk_init_request,
613 .map_queues = virtblk_map_queues,
616 static unsigned int virtblk_queue_depth;
617 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
619 static int virtblk_probe(struct virtio_device *vdev)
621 struct virtio_blk *vblk;
622 struct request_queue *q;
625 u32 v, blk_size, max_size, sg_elems, opt_io_size;
627 u8 physical_block_exp, alignment_offset;
629 if (!vdev->config->get) {
630 dev_err(&vdev->dev, "%s failure: config access disabled\n",
635 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
641 /* We need to know how many segments before we allocate. */
642 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
643 struct virtio_blk_config, seg_max,
646 /* We need at least one SG element, whatever they say. */
647 if (err || !sg_elems)
650 /* We need an extra sg elements at head and tail. */
652 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
659 vblk->sg_elems = sg_elems;
661 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
667 /* FIXME: How many partitions? How long is a piece of string? */
668 vblk->disk = alloc_disk(1 << PART_BITS);
674 /* Default queue sizing is to fill the ring. */
675 if (!virtblk_queue_depth) {
676 virtblk_queue_depth = vblk->vqs[0].vq->num_free;
677 /* ... but without indirect descs, we use 2 descs per req */
678 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
679 virtblk_queue_depth /= 2;
682 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
683 vblk->tag_set.ops = &virtio_mq_ops;
684 vblk->tag_set.queue_depth = virtblk_queue_depth;
685 vblk->tag_set.numa_node = NUMA_NO_NODE;
686 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
687 vblk->tag_set.cmd_size =
688 sizeof(struct virtblk_req) +
689 sizeof(struct scatterlist) * sg_elems;
690 vblk->tag_set.driver_data = vblk;
691 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
693 err = blk_mq_alloc_tag_set(&vblk->tag_set);
697 q = blk_mq_init_queue(&vblk->tag_set);
702 vblk->disk->queue = q;
706 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
708 vblk->disk->major = major;
709 vblk->disk->first_minor = index_to_minor(index);
710 vblk->disk->private_data = vblk;
711 vblk->disk->fops = &virtblk_fops;
712 vblk->disk->flags |= GENHD_FL_EXT_DEVT;
715 /* configure queue flush support */
716 virtblk_update_cache_mode(vdev);
718 /* If disk is read-only in the host, the guest should obey */
719 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
720 set_disk_ro(vblk->disk, 1);
722 /* We can handle whatever the host told us to handle. */
723 blk_queue_max_segments(q, vblk->sg_elems-2);
725 /* No real sector limit. */
726 blk_queue_max_hw_sectors(q, -1U);
728 max_size = virtio_max_dma_size(vdev);
730 /* Host can optionally specify maximum segment size and number of
732 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
733 struct virtio_blk_config, size_max, &v);
735 max_size = min(max_size, v);
737 blk_queue_max_segment_size(q, max_size);
739 /* Host can optionally specify the block size of the device */
740 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
741 struct virtio_blk_config, blk_size,
744 blk_queue_logical_block_size(q, blk_size);
746 blk_size = queue_logical_block_size(q);
748 /* Use topology information if available */
749 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
750 struct virtio_blk_config, physical_block_exp,
751 &physical_block_exp);
752 if (!err && physical_block_exp)
753 blk_queue_physical_block_size(q,
754 blk_size * (1 << physical_block_exp));
756 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
757 struct virtio_blk_config, alignment_offset,
759 if (!err && alignment_offset)
760 blk_queue_alignment_offset(q, blk_size * alignment_offset);
762 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
763 struct virtio_blk_config, min_io_size,
765 if (!err && min_io_size)
766 blk_queue_io_min(q, blk_size * min_io_size);
768 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
769 struct virtio_blk_config, opt_io_size,
771 if (!err && opt_io_size)
772 blk_queue_io_opt(q, blk_size * opt_io_size);
774 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
775 q->limits.discard_granularity = blk_size;
777 virtio_cread(vdev, struct virtio_blk_config,
778 discard_sector_alignment, &v);
779 q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;
781 virtio_cread(vdev, struct virtio_blk_config,
782 max_discard_sectors, &v);
783 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
785 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
787 blk_queue_max_discard_segments(q,
789 MAX_DISCARD_SEGMENTS));
791 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
794 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
795 virtio_cread(vdev, struct virtio_blk_config,
796 max_write_zeroes_sectors, &v);
797 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
800 virtblk_update_capacity(vblk, false);
801 virtio_device_ready(vdev);
803 device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
807 blk_mq_free_tag_set(&vblk->tag_set);
809 put_disk(vblk->disk);
811 vdev->config->del_vqs(vdev);
815 ida_simple_remove(&vd_index_ida, index);
820 static void virtblk_remove(struct virtio_device *vdev)
822 struct virtio_blk *vblk = vdev->priv;
823 int index = vblk->index;
826 /* Make sure no work handler is accessing the device. */
827 flush_work(&vblk->config_work);
829 del_gendisk(vblk->disk);
830 blk_cleanup_queue(vblk->disk->queue);
832 blk_mq_free_tag_set(&vblk->tag_set);
834 /* Stop all the virtqueues. */
835 vdev->config->reset(vdev);
837 refc = kref_read(&disk_to_dev(vblk->disk)->kobj.kref);
838 put_disk(vblk->disk);
839 vdev->config->del_vqs(vdev);
843 /* Only free device id if we don't have any users */
845 ida_simple_remove(&vd_index_ida, index);
848 #ifdef CONFIG_PM_SLEEP
849 static int virtblk_freeze(struct virtio_device *vdev)
851 struct virtio_blk *vblk = vdev->priv;
853 /* Ensure we don't receive any more interrupts */
854 vdev->config->reset(vdev);
856 /* Make sure no work handler is accessing the device. */
857 flush_work(&vblk->config_work);
859 blk_mq_quiesce_queue(vblk->disk->queue);
861 vdev->config->del_vqs(vdev);
865 static int virtblk_restore(struct virtio_device *vdev)
867 struct virtio_blk *vblk = vdev->priv;
870 ret = init_vq(vdev->priv);
874 virtio_device_ready(vdev);
876 blk_mq_unquiesce_queue(vblk->disk->queue);
881 static const struct virtio_device_id id_table[] = {
882 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
886 static unsigned int features_legacy[] = {
887 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
888 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
889 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
890 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
893 static unsigned int features[] = {
894 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
895 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
896 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
897 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
900 static struct virtio_driver virtio_blk = {
901 .feature_table = features,
902 .feature_table_size = ARRAY_SIZE(features),
903 .feature_table_legacy = features_legacy,
904 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
905 .driver.name = KBUILD_MODNAME,
906 .driver.owner = THIS_MODULE,
907 .id_table = id_table,
908 .probe = virtblk_probe,
909 .remove = virtblk_remove,
910 .config_changed = virtblk_config_changed,
911 #ifdef CONFIG_PM_SLEEP
912 .freeze = virtblk_freeze,
913 .restore = virtblk_restore,
917 static int __init init(void)
921 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
925 major = register_blkdev(0, "virtblk");
928 goto out_destroy_workqueue;
931 error = register_virtio_driver(&virtio_blk);
933 goto out_unregister_blkdev;
936 out_unregister_blkdev:
937 unregister_blkdev(major, "virtblk");
938 out_destroy_workqueue:
939 destroy_workqueue(virtblk_wq);
943 static void __exit fini(void)
945 unregister_virtio_driver(&virtio_blk);
946 unregister_blkdev(major, "virtblk");
947 destroy_workqueue(virtblk_wq);
952 MODULE_DEVICE_TABLE(virtio, id_table);
953 MODULE_DESCRIPTION("Virtio block driver");
954 MODULE_LICENSE("GPL");