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 <linux/vmalloc.h>
19 #include <uapi/linux/virtio_ring.h>
22 #define VQ_NAME_LEN 16
23 #define MAX_DISCARD_SEGMENTS 256u
25 /* The maximum number of sg elements that fit into a virtqueue */
26 #define VIRTIO_BLK_MAX_SG_ELEMS 32768
28 #ifdef CONFIG_ARCH_NO_SG_CHAIN
29 #define VIRTIO_BLK_INLINE_SG_CNT 0
31 #define VIRTIO_BLK_INLINE_SG_CNT 2
34 static unsigned int num_request_queues;
35 module_param(num_request_queues, uint, 0644);
36 MODULE_PARM_DESC(num_request_queues,
37 "Limit the number of request queues to use for blk device. "
39 "Values > nr_cpu_ids truncated to nr_cpu_ids.");
41 static unsigned int poll_queues;
42 module_param(poll_queues, uint, 0644);
43 MODULE_PARM_DESC(poll_queues, "The number of dedicated virtqueues for polling I/O");
46 static DEFINE_IDA(vd_index_ida);
48 static struct workqueue_struct *virtblk_wq;
50 struct virtio_blk_vq {
53 char name[VQ_NAME_LEN];
54 } ____cacheline_aligned_in_smp;
58 * This mutex must be held by anything that may run after
59 * virtblk_remove() sets vblk->vdev to NULL.
61 * blk-mq, virtqueue processing, and sysfs attribute code paths are
62 * shut down before vblk->vdev is set to NULL and therefore do not need
65 struct mutex vdev_mutex;
66 struct virtio_device *vdev;
68 /* The disk structure for the kernel. */
71 /* Block layer tags. */
72 struct blk_mq_tag_set tag_set;
74 /* Process context for config space updates */
75 struct work_struct config_work;
77 /* Ida index - used to track minor number allocations. */
82 int io_queues[HCTX_MAX_TYPES];
83 struct virtio_blk_vq *vqs;
85 /* For zoned device */
86 unsigned int zone_sectors;
91 struct virtio_blk_outhdr out_hdr;
98 * The zone append command has an extended in header.
99 * The status field in zone_append_in_hdr must always
110 struct sg_table sg_table;
111 struct scatterlist sg[];
114 static inline blk_status_t virtblk_result(u8 status)
117 case VIRTIO_BLK_S_OK:
119 case VIRTIO_BLK_S_UNSUPP:
120 return BLK_STS_NOTSUPP;
121 case VIRTIO_BLK_S_ZONE_OPEN_RESOURCE:
122 return BLK_STS_ZONE_OPEN_RESOURCE;
123 case VIRTIO_BLK_S_ZONE_ACTIVE_RESOURCE:
124 return BLK_STS_ZONE_ACTIVE_RESOURCE;
125 case VIRTIO_BLK_S_IOERR:
126 case VIRTIO_BLK_S_ZONE_UNALIGNED_WP:
128 return BLK_STS_IOERR;
132 static inline struct virtio_blk_vq *get_virtio_blk_vq(struct blk_mq_hw_ctx *hctx)
134 struct virtio_blk *vblk = hctx->queue->queuedata;
135 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
140 static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
142 struct scatterlist out_hdr, in_hdr, *sgs[3];
143 unsigned int num_out = 0, num_in = 0;
145 sg_init_one(&out_hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
146 sgs[num_out++] = &out_hdr;
148 if (vbr->sg_table.nents) {
149 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
150 sgs[num_out++] = vbr->sg_table.sgl;
152 sgs[num_out + num_in++] = vbr->sg_table.sgl;
155 sg_init_one(&in_hdr, &vbr->in_hdr.status, vbr->in_hdr_len);
156 sgs[num_out + num_in++] = &in_hdr;
158 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
161 static int virtblk_setup_discard_write_zeroes_erase(struct request *req, bool unmap)
163 unsigned short segments = blk_rq_nr_discard_segments(req);
164 unsigned short n = 0;
165 struct virtio_blk_discard_write_zeroes *range;
170 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
172 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
177 * Single max discard segment means multi-range discard isn't
178 * supported, and block layer only runs contiguity merge like
179 * normal RW request. So we can't reply on bio for retrieving
182 if (queue_max_discard_segments(req->q) == 1) {
183 range[0].flags = cpu_to_le32(flags);
184 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
185 range[0].sector = cpu_to_le64(blk_rq_pos(req));
188 __rq_for_each_bio(bio, req) {
189 u64 sector = bio->bi_iter.bi_sector;
190 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
192 range[n].flags = cpu_to_le32(flags);
193 range[n].num_sectors = cpu_to_le32(num_sectors);
194 range[n].sector = cpu_to_le64(sector);
199 WARN_ON_ONCE(n != segments);
201 bvec_set_virt(&req->special_vec, range, sizeof(*range) * segments);
202 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
207 static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
209 if (blk_rq_nr_phys_segments(req))
210 sg_free_table_chained(&vbr->sg_table,
211 VIRTIO_BLK_INLINE_SG_CNT);
214 static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
215 struct virtblk_req *vbr)
219 if (!blk_rq_nr_phys_segments(req))
222 vbr->sg_table.sgl = vbr->sg;
223 err = sg_alloc_table_chained(&vbr->sg_table,
224 blk_rq_nr_phys_segments(req),
226 VIRTIO_BLK_INLINE_SG_CNT);
230 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
233 static void virtblk_cleanup_cmd(struct request *req)
235 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
236 kfree(bvec_virt(&req->special_vec));
239 static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
241 struct virtblk_req *vbr)
243 size_t in_hdr_len = sizeof(vbr->in_hdr.status);
248 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) && op_is_zone_mgmt(req_op(req)))
249 return BLK_STS_NOTSUPP;
251 /* Set fields for all request types */
252 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
254 switch (req_op(req)) {
256 type = VIRTIO_BLK_T_IN;
257 sector = blk_rq_pos(req);
260 type = VIRTIO_BLK_T_OUT;
261 sector = blk_rq_pos(req);
264 type = VIRTIO_BLK_T_FLUSH;
267 type = VIRTIO_BLK_T_DISCARD;
269 case REQ_OP_WRITE_ZEROES:
270 type = VIRTIO_BLK_T_WRITE_ZEROES;
271 unmap = !(req->cmd_flags & REQ_NOUNMAP);
273 case REQ_OP_SECURE_ERASE:
274 type = VIRTIO_BLK_T_SECURE_ERASE;
276 case REQ_OP_ZONE_OPEN:
277 type = VIRTIO_BLK_T_ZONE_OPEN;
278 sector = blk_rq_pos(req);
280 case REQ_OP_ZONE_CLOSE:
281 type = VIRTIO_BLK_T_ZONE_CLOSE;
282 sector = blk_rq_pos(req);
284 case REQ_OP_ZONE_FINISH:
285 type = VIRTIO_BLK_T_ZONE_FINISH;
286 sector = blk_rq_pos(req);
288 case REQ_OP_ZONE_APPEND:
289 type = VIRTIO_BLK_T_ZONE_APPEND;
290 sector = blk_rq_pos(req);
291 in_hdr_len = sizeof(vbr->in_hdr.zone_append);
293 case REQ_OP_ZONE_RESET:
294 type = VIRTIO_BLK_T_ZONE_RESET;
295 sector = blk_rq_pos(req);
297 case REQ_OP_ZONE_RESET_ALL:
298 type = VIRTIO_BLK_T_ZONE_RESET_ALL;
302 * Out header has already been prepared by the caller (virtblk_get_id()
303 * or virtblk_submit_zone_report()), nothing to do here.
308 return BLK_STS_IOERR;
311 /* Set fields for non-REQ_OP_DRV_IN request types */
312 vbr->in_hdr_len = in_hdr_len;
313 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
314 vbr->out_hdr.sector = cpu_to_virtio64(vdev, sector);
316 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES ||
317 type == VIRTIO_BLK_T_SECURE_ERASE) {
318 if (virtblk_setup_discard_write_zeroes_erase(req, unmap))
319 return BLK_STS_RESOURCE;
326 * The status byte is always the last byte of the virtblk request
327 * in-header. This helper fetches its value for all in-header formats
328 * that are currently defined.
330 static inline u8 virtblk_vbr_status(struct virtblk_req *vbr)
332 return *((u8 *)&vbr->in_hdr + vbr->in_hdr_len - 1);
335 static inline void virtblk_request_done(struct request *req)
337 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
338 blk_status_t status = virtblk_result(virtblk_vbr_status(vbr));
339 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
341 virtblk_unmap_data(req, vbr);
342 virtblk_cleanup_cmd(req);
344 if (req_op(req) == REQ_OP_ZONE_APPEND)
345 req->__sector = virtio64_to_cpu(vblk->vdev,
346 vbr->in_hdr.zone_append.sector);
348 blk_mq_end_request(req, status);
351 static void virtblk_done(struct virtqueue *vq)
353 struct virtio_blk *vblk = vq->vdev->priv;
354 bool req_done = false;
356 struct virtblk_req *vbr;
360 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
362 virtqueue_disable_cb(vq);
363 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
364 struct request *req = blk_mq_rq_from_pdu(vbr);
366 if (likely(!blk_should_fake_timeout(req->q)))
367 blk_mq_complete_request(req);
370 if (unlikely(virtqueue_is_broken(vq)))
372 } while (!virtqueue_enable_cb(vq));
374 /* In case queue is stopped waiting for more buffers. */
376 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
377 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
380 static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
382 struct virtio_blk *vblk = hctx->queue->queuedata;
383 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
386 spin_lock_irq(&vq->lock);
387 kick = virtqueue_kick_prepare(vq->vq);
388 spin_unlock_irq(&vq->lock);
391 virtqueue_notify(vq->vq);
394 static blk_status_t virtblk_fail_to_queue(struct request *req, int rc)
396 virtblk_cleanup_cmd(req);
399 return BLK_STS_DEV_RESOURCE;
401 return BLK_STS_RESOURCE;
403 return BLK_STS_IOERR;
407 static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
408 struct virtio_blk *vblk,
410 struct virtblk_req *vbr)
415 status = virtblk_setup_cmd(vblk->vdev, req, vbr);
416 if (unlikely(status))
419 num = virtblk_map_data(hctx, req, vbr);
420 if (unlikely(num < 0))
421 return virtblk_fail_to_queue(req, -ENOMEM);
422 vbr->sg_table.nents = num;
424 blk_mq_start_request(req);
429 static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
430 const struct blk_mq_queue_data *bd)
432 struct virtio_blk *vblk = hctx->queue->queuedata;
433 struct request *req = bd->rq;
434 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
436 int qid = hctx->queue_num;
441 status = virtblk_prep_rq(hctx, vblk, req, vbr);
442 if (unlikely(status))
445 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
446 err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
448 virtqueue_kick(vblk->vqs[qid].vq);
449 /* Don't stop the queue if -ENOMEM: we may have failed to
450 * bounce the buffer due to global resource outage.
453 blk_mq_stop_hw_queue(hctx);
454 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
455 virtblk_unmap_data(req, vbr);
456 return virtblk_fail_to_queue(req, err);
459 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
461 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
464 virtqueue_notify(vblk->vqs[qid].vq);
468 static bool virtblk_prep_rq_batch(struct request *req)
470 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
471 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
473 return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
476 static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
477 struct request **rqlist)
483 spin_lock_irqsave(&vq->lock, flags);
485 while (!rq_list_empty(*rqlist)) {
486 struct request *req = rq_list_pop(rqlist);
487 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
489 err = virtblk_add_req(vq->vq, vbr);
491 virtblk_unmap_data(req, vbr);
492 virtblk_cleanup_cmd(req);
493 blk_mq_requeue_request(req, true);
497 kick = virtqueue_kick_prepare(vq->vq);
498 spin_unlock_irqrestore(&vq->lock, flags);
503 static void virtio_queue_rqs(struct request **rqlist)
505 struct request *req, *next, *prev = NULL;
506 struct request *requeue_list = NULL;
508 rq_list_for_each_safe(rqlist, req, next) {
509 struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
512 if (!virtblk_prep_rq_batch(req)) {
513 rq_list_move(rqlist, &requeue_list, req, prev);
519 if (!next || req->mq_hctx != next->mq_hctx) {
521 kick = virtblk_add_req_batch(vq, rqlist);
523 virtqueue_notify(vq->vq);
531 *rqlist = requeue_list;
534 #ifdef CONFIG_BLK_DEV_ZONED
535 static void *virtblk_alloc_report_buffer(struct virtio_blk *vblk,
536 unsigned int nr_zones,
539 struct request_queue *q = vblk->disk->queue;
543 nr_zones = min_t(unsigned int, nr_zones,
544 get_capacity(vblk->disk) >> ilog2(vblk->zone_sectors));
546 bufsize = sizeof(struct virtio_blk_zone_report) +
547 nr_zones * sizeof(struct virtio_blk_zone_descriptor);
548 bufsize = min_t(size_t, bufsize,
549 queue_max_hw_sectors(q) << SECTOR_SHIFT);
550 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
552 while (bufsize >= sizeof(struct virtio_blk_zone_report)) {
553 buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
564 static int virtblk_submit_zone_report(struct virtio_blk *vblk,
565 char *report_buf, size_t report_len,
568 struct request_queue *q = vblk->disk->queue;
570 struct virtblk_req *vbr;
573 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
577 vbr = blk_mq_rq_to_pdu(req);
578 vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
579 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_ZONE_REPORT);
580 vbr->out_hdr.sector = cpu_to_virtio64(vblk->vdev, sector);
582 err = blk_rq_map_kern(q, req, report_buf, report_len, GFP_KERNEL);
586 blk_execute_rq(req, false);
587 err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
589 blk_mq_free_request(req);
593 static int virtblk_parse_zone(struct virtio_blk *vblk,
594 struct virtio_blk_zone_descriptor *entry,
595 unsigned int idx, report_zones_cb cb, void *data)
597 struct blk_zone zone = { };
599 zone.start = virtio64_to_cpu(vblk->vdev, entry->z_start);
600 if (zone.start + vblk->zone_sectors <= get_capacity(vblk->disk))
601 zone.len = vblk->zone_sectors;
603 zone.len = get_capacity(vblk->disk) - zone.start;
604 zone.capacity = virtio64_to_cpu(vblk->vdev, entry->z_cap);
605 zone.wp = virtio64_to_cpu(vblk->vdev, entry->z_wp);
607 switch (entry->z_type) {
608 case VIRTIO_BLK_ZT_SWR:
609 zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
611 case VIRTIO_BLK_ZT_SWP:
612 zone.type = BLK_ZONE_TYPE_SEQWRITE_PREF;
614 case VIRTIO_BLK_ZT_CONV:
615 zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
618 dev_err(&vblk->vdev->dev, "zone %llu: invalid type %#x\n",
619 zone.start, entry->z_type);
623 switch (entry->z_state) {
624 case VIRTIO_BLK_ZS_EMPTY:
625 zone.cond = BLK_ZONE_COND_EMPTY;
627 case VIRTIO_BLK_ZS_CLOSED:
628 zone.cond = BLK_ZONE_COND_CLOSED;
630 case VIRTIO_BLK_ZS_FULL:
631 zone.cond = BLK_ZONE_COND_FULL;
632 zone.wp = zone.start + zone.len;
634 case VIRTIO_BLK_ZS_EOPEN:
635 zone.cond = BLK_ZONE_COND_EXP_OPEN;
637 case VIRTIO_BLK_ZS_IOPEN:
638 zone.cond = BLK_ZONE_COND_IMP_OPEN;
640 case VIRTIO_BLK_ZS_NOT_WP:
641 zone.cond = BLK_ZONE_COND_NOT_WP;
643 case VIRTIO_BLK_ZS_RDONLY:
644 zone.cond = BLK_ZONE_COND_READONLY;
647 case VIRTIO_BLK_ZS_OFFLINE:
648 zone.cond = BLK_ZONE_COND_OFFLINE;
652 dev_err(&vblk->vdev->dev, "zone %llu: invalid condition %#x\n",
653 zone.start, entry->z_state);
658 * The callback below checks the validity of the reported
659 * entry data, no need to further validate it here.
661 return cb(&zone, idx, data);
664 static int virtblk_report_zones(struct gendisk *disk, sector_t sector,
665 unsigned int nr_zones, report_zones_cb cb,
668 struct virtio_blk *vblk = disk->private_data;
669 struct virtio_blk_zone_report *report;
670 unsigned long long nz, i;
672 unsigned int zone_idx = 0;
675 if (WARN_ON_ONCE(!vblk->zone_sectors))
678 report = virtblk_alloc_report_buffer(vblk, nr_zones, &buflen);
682 mutex_lock(&vblk->vdev_mutex);
689 while (zone_idx < nr_zones && sector < get_capacity(vblk->disk)) {
690 memset(report, 0, buflen);
692 ret = virtblk_submit_zone_report(vblk, (char *)report,
697 nz = min_t(u64, virtio64_to_cpu(vblk->vdev, report->nr_zones),
702 for (i = 0; i < nz && zone_idx < nr_zones; i++) {
703 ret = virtblk_parse_zone(vblk, &report->zones[i],
708 sector = virtio64_to_cpu(vblk->vdev,
709 report->zones[i].z_start) +
720 mutex_unlock(&vblk->vdev_mutex);
725 static int virtblk_probe_zoned_device(struct virtio_device *vdev,
726 struct virtio_blk *vblk,
727 struct request_queue *q)
731 dev_dbg(&vdev->dev, "probing host-managed zoned device\n");
733 disk_set_zoned(vblk->disk);
734 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
736 virtio_cread(vdev, struct virtio_blk_config,
737 zoned.max_open_zones, &v);
738 disk_set_max_open_zones(vblk->disk, v);
739 dev_dbg(&vdev->dev, "max open zones = %u\n", v);
741 virtio_cread(vdev, struct virtio_blk_config,
742 zoned.max_active_zones, &v);
743 disk_set_max_active_zones(vblk->disk, v);
744 dev_dbg(&vdev->dev, "max active zones = %u\n", v);
746 virtio_cread(vdev, struct virtio_blk_config,
747 zoned.write_granularity, &wg);
749 dev_warn(&vdev->dev, "zero write granularity reported\n");
752 blk_queue_physical_block_size(q, wg);
753 blk_queue_io_min(q, wg);
755 dev_dbg(&vdev->dev, "write granularity = %u\n", wg);
758 * virtio ZBD specification doesn't require zones to be a power of
759 * two sectors in size, but the code in this driver expects that.
761 virtio_cread(vdev, struct virtio_blk_config, zoned.zone_sectors,
762 &vblk->zone_sectors);
763 if (vblk->zone_sectors == 0 || !is_power_of_2(vblk->zone_sectors)) {
765 "zoned device with non power of two zone size %u\n",
769 blk_queue_chunk_sectors(q, vblk->zone_sectors);
770 dev_dbg(&vdev->dev, "zone sectors = %u\n", vblk->zone_sectors);
772 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
773 dev_warn(&vblk->vdev->dev,
774 "ignoring negotiated F_DISCARD for zoned device\n");
775 blk_queue_max_discard_sectors(q, 0);
778 virtio_cread(vdev, struct virtio_blk_config,
779 zoned.max_append_sectors, &v);
781 dev_warn(&vdev->dev, "zero max_append_sectors reported\n");
784 if ((v << SECTOR_SHIFT) < wg) {
786 "write granularity %u exceeds max_append_sectors %u limit\n",
790 blk_queue_max_zone_append_sectors(q, v);
791 dev_dbg(&vdev->dev, "max append sectors = %u\n", v);
793 return blk_revalidate_disk_zones(vblk->disk, NULL);
799 * Zoned block device support is not configured in this kernel.
800 * Host-managed zoned devices can't be supported, but others are
801 * good to go as regular block devices.
803 #define virtblk_report_zones NULL
805 static inline int virtblk_probe_zoned_device(struct virtio_device *vdev,
806 struct virtio_blk *vblk, struct request_queue *q)
809 "virtio_blk: zoned devices are not supported");
812 #endif /* CONFIG_BLK_DEV_ZONED */
814 /* return id (s/n) string for *disk to *id_str
816 static int virtblk_get_id(struct gendisk *disk, char *id_str)
818 struct virtio_blk *vblk = disk->private_data;
819 struct request_queue *q = vblk->disk->queue;
821 struct virtblk_req *vbr;
824 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
828 vbr = blk_mq_rq_to_pdu(req);
829 vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
830 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
831 vbr->out_hdr.sector = 0;
833 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
837 blk_execute_rq(req, false);
838 err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
840 blk_mq_free_request(req);
844 /* We provide getgeo only to please some old bootloader/partitioning tools */
845 static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
847 struct virtio_blk *vblk = bd->bd_disk->private_data;
850 mutex_lock(&vblk->vdev_mutex);
857 /* see if the host passed in geometry config */
858 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
859 virtio_cread(vblk->vdev, struct virtio_blk_config,
860 geometry.cylinders, &geo->cylinders);
861 virtio_cread(vblk->vdev, struct virtio_blk_config,
862 geometry.heads, &geo->heads);
863 virtio_cread(vblk->vdev, struct virtio_blk_config,
864 geometry.sectors, &geo->sectors);
866 /* some standard values, similar to sd */
868 geo->sectors = 1 << 5;
869 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
872 mutex_unlock(&vblk->vdev_mutex);
876 static void virtblk_free_disk(struct gendisk *disk)
878 struct virtio_blk *vblk = disk->private_data;
880 ida_free(&vd_index_ida, vblk->index);
881 mutex_destroy(&vblk->vdev_mutex);
885 static const struct block_device_operations virtblk_fops = {
886 .owner = THIS_MODULE,
887 .getgeo = virtblk_getgeo,
888 .free_disk = virtblk_free_disk,
889 .report_zones = virtblk_report_zones,
892 static int index_to_minor(int index)
894 return index << PART_BITS;
897 static int minor_to_index(int minor)
899 return minor >> PART_BITS;
902 static ssize_t serial_show(struct device *dev,
903 struct device_attribute *attr, char *buf)
905 struct gendisk *disk = dev_to_disk(dev);
908 /* sysfs gives us a PAGE_SIZE buffer */
909 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
911 buf[VIRTIO_BLK_ID_BYTES] = '\0';
912 err = virtblk_get_id(disk, buf);
916 if (err == -EIO) /* Unsupported? Make it empty. */
922 static DEVICE_ATTR_RO(serial);
924 /* The queue's logical block size must be set before calling this */
925 static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
927 struct virtio_device *vdev = vblk->vdev;
928 struct request_queue *q = vblk->disk->queue;
929 char cap_str_2[10], cap_str_10[10];
930 unsigned long long nblocks;
933 /* Host must always specify the capacity. */
934 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
936 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
938 string_get_size(nblocks, queue_logical_block_size(q),
939 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
940 string_get_size(nblocks, queue_logical_block_size(q),
941 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
943 dev_notice(&vdev->dev,
944 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
945 vblk->disk->disk_name,
946 resize ? "new size: " : "",
948 queue_logical_block_size(q),
952 set_capacity_and_notify(vblk->disk, capacity);
955 static void virtblk_config_changed_work(struct work_struct *work)
957 struct virtio_blk *vblk =
958 container_of(work, struct virtio_blk, config_work);
960 virtblk_update_capacity(vblk, true);
963 static void virtblk_config_changed(struct virtio_device *vdev)
965 struct virtio_blk *vblk = vdev->priv;
967 queue_work(virtblk_wq, &vblk->config_work);
970 static int init_vq(struct virtio_blk *vblk)
974 vq_callback_t **callbacks;
976 struct virtqueue **vqs;
977 unsigned short num_vqs;
978 unsigned short num_poll_vqs;
979 struct virtio_device *vdev = vblk->vdev;
980 struct irq_affinity desc = { 0, };
982 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
983 struct virtio_blk_config, num_queues,
988 if (!err && !num_vqs) {
989 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
993 num_vqs = min_t(unsigned int,
994 min_not_zero(num_request_queues, nr_cpu_ids),
997 num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
999 vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
1000 vblk->io_queues[HCTX_TYPE_READ] = 0;
1001 vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
1003 dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
1004 vblk->io_queues[HCTX_TYPE_DEFAULT],
1005 vblk->io_queues[HCTX_TYPE_READ],
1006 vblk->io_queues[HCTX_TYPE_POLL]);
1008 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
1012 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
1013 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
1014 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
1015 if (!names || !callbacks || !vqs) {
1020 for (i = 0; i < num_vqs - num_poll_vqs; i++) {
1021 callbacks[i] = virtblk_done;
1022 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%u", i);
1023 names[i] = vblk->vqs[i].name;
1026 for (; i < num_vqs; i++) {
1027 callbacks[i] = NULL;
1028 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%u", i);
1029 names[i] = vblk->vqs[i].name;
1032 /* Discover virtqueues and write information to configuration. */
1033 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
1037 for (i = 0; i < num_vqs; i++) {
1038 spin_lock_init(&vblk->vqs[i].lock);
1039 vblk->vqs[i].vq = vqs[i];
1041 vblk->num_vqs = num_vqs;
1053 * Legacy naming scheme used for virtio devices. We are stuck with it for
1054 * virtio blk but don't ever use it for any new driver.
1056 static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
1058 const int base = 'z' - 'a' + 1;
1059 char *begin = buf + strlen(prefix);
1060 char *end = buf + buflen;
1070 *--p = 'a' + (index % unit);
1071 index = (index / unit) - 1;
1072 } while (index >= 0);
1074 memmove(begin, p, end - p);
1075 memcpy(buf, prefix, strlen(prefix));
1080 static int virtblk_get_cache_mode(struct virtio_device *vdev)
1085 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
1086 struct virtio_blk_config, wce,
1090 * If WCE is not configurable and flush is not available,
1091 * assume no writeback cache is in use.
1094 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
1099 static void virtblk_update_cache_mode(struct virtio_device *vdev)
1101 u8 writeback = virtblk_get_cache_mode(vdev);
1102 struct virtio_blk *vblk = vdev->priv;
1104 blk_queue_write_cache(vblk->disk->queue, writeback, false);
1107 static const char *const virtblk_cache_types[] = {
1108 "write through", "write back"
1112 cache_type_store(struct device *dev, struct device_attribute *attr,
1113 const char *buf, size_t count)
1115 struct gendisk *disk = dev_to_disk(dev);
1116 struct virtio_blk *vblk = disk->private_data;
1117 struct virtio_device *vdev = vblk->vdev;
1120 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
1121 i = sysfs_match_string(virtblk_cache_types, buf);
1125 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
1126 virtblk_update_cache_mode(vdev);
1131 cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
1133 struct gendisk *disk = dev_to_disk(dev);
1134 struct virtio_blk *vblk = disk->private_data;
1135 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
1137 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
1138 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
1141 static DEVICE_ATTR_RW(cache_type);
1143 static struct attribute *virtblk_attrs[] = {
1144 &dev_attr_serial.attr,
1145 &dev_attr_cache_type.attr,
1149 static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
1150 struct attribute *a, int n)
1152 struct device *dev = kobj_to_dev(kobj);
1153 struct gendisk *disk = dev_to_disk(dev);
1154 struct virtio_blk *vblk = disk->private_data;
1155 struct virtio_device *vdev = vblk->vdev;
1157 if (a == &dev_attr_cache_type.attr &&
1158 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
1164 static const struct attribute_group virtblk_attr_group = {
1165 .attrs = virtblk_attrs,
1166 .is_visible = virtblk_attrs_are_visible,
1169 static const struct attribute_group *virtblk_attr_groups[] = {
1170 &virtblk_attr_group,
1174 static void virtblk_map_queues(struct blk_mq_tag_set *set)
1176 struct virtio_blk *vblk = set->driver_data;
1179 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
1180 struct blk_mq_queue_map *map = &set->map[i];
1182 map->nr_queues = vblk->io_queues[i];
1183 map->queue_offset = qoff;
1184 qoff += map->nr_queues;
1186 if (map->nr_queues == 0)
1190 * Regular queues have interrupts and hence CPU affinity is
1191 * defined by the core virtio code, but polling queues have
1192 * no interrupts so we let the block layer assign CPU affinity.
1194 if (i == HCTX_TYPE_POLL)
1195 blk_mq_map_queues(&set->map[i]);
1197 blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
1201 static void virtblk_complete_batch(struct io_comp_batch *iob)
1203 struct request *req;
1205 rq_list_for_each(&iob->req_list, req) {
1206 virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
1207 virtblk_cleanup_cmd(req);
1209 blk_mq_end_request_batch(iob);
1212 static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1214 struct virtio_blk *vblk = hctx->queue->queuedata;
1215 struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
1216 struct virtblk_req *vbr;
1217 unsigned long flags;
1221 spin_lock_irqsave(&vq->lock, flags);
1223 while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
1224 struct request *req = blk_mq_rq_from_pdu(vbr);
1227 if (!blk_mq_complete_request_remote(req) &&
1228 !blk_mq_add_to_batch(req, iob, virtblk_vbr_status(vbr),
1229 virtblk_complete_batch))
1230 virtblk_request_done(req);
1234 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
1236 spin_unlock_irqrestore(&vq->lock, flags);
1241 static const struct blk_mq_ops virtio_mq_ops = {
1242 .queue_rq = virtio_queue_rq,
1243 .queue_rqs = virtio_queue_rqs,
1244 .commit_rqs = virtio_commit_rqs,
1245 .complete = virtblk_request_done,
1246 .map_queues = virtblk_map_queues,
1247 .poll = virtblk_poll,
1250 static unsigned int virtblk_queue_depth;
1251 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
1253 static int virtblk_probe(struct virtio_device *vdev)
1255 struct virtio_blk *vblk;
1256 struct request_queue *q;
1259 u32 v, blk_size, max_size, sg_elems, opt_io_size;
1260 u32 max_discard_segs = 0;
1261 u32 discard_granularity = 0;
1263 u8 physical_block_exp, alignment_offset;
1264 unsigned int queue_depth;
1265 size_t max_dma_size;
1267 if (!vdev->config->get) {
1268 dev_err(&vdev->dev, "%s failure: config access disabled\n",
1273 err = ida_alloc_range(&vd_index_ida, 0,
1274 minor_to_index(1 << MINORBITS) - 1, GFP_KERNEL);
1279 /* We need to know how many segments before we allocate. */
1280 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
1281 struct virtio_blk_config, seg_max,
1284 /* We need at least one SG element, whatever they say. */
1285 if (err || !sg_elems)
1288 /* Prevent integer overflows and honor max vq size */
1289 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
1291 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
1294 goto out_free_index;
1297 mutex_init(&vblk->vdev_mutex);
1301 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
1303 err = init_vq(vblk);
1307 /* Default queue sizing is to fill the ring. */
1308 if (!virtblk_queue_depth) {
1309 queue_depth = vblk->vqs[0].vq->num_free;
1310 /* ... but without indirect descs, we use 2 descs per req */
1311 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
1314 queue_depth = virtblk_queue_depth;
1317 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
1318 vblk->tag_set.ops = &virtio_mq_ops;
1319 vblk->tag_set.queue_depth = queue_depth;
1320 vblk->tag_set.numa_node = NUMA_NO_NODE;
1321 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1322 vblk->tag_set.cmd_size =
1323 sizeof(struct virtblk_req) +
1324 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
1325 vblk->tag_set.driver_data = vblk;
1326 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
1327 vblk->tag_set.nr_maps = 1;
1328 if (vblk->io_queues[HCTX_TYPE_POLL])
1329 vblk->tag_set.nr_maps = 3;
1331 err = blk_mq_alloc_tag_set(&vblk->tag_set);
1335 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
1336 if (IS_ERR(vblk->disk)) {
1337 err = PTR_ERR(vblk->disk);
1340 q = vblk->disk->queue;
1342 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
1344 vblk->disk->major = major;
1345 vblk->disk->first_minor = index_to_minor(index);
1346 vblk->disk->minors = 1 << PART_BITS;
1347 vblk->disk->private_data = vblk;
1348 vblk->disk->fops = &virtblk_fops;
1349 vblk->index = index;
1351 /* configure queue flush support */
1352 virtblk_update_cache_mode(vdev);
1354 /* If disk is read-only in the host, the guest should obey */
1355 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
1356 set_disk_ro(vblk->disk, 1);
1358 /* We can handle whatever the host told us to handle. */
1359 blk_queue_max_segments(q, sg_elems);
1361 /* No real sector limit. */
1362 blk_queue_max_hw_sectors(q, UINT_MAX);
1364 max_dma_size = virtio_max_dma_size(vdev);
1365 max_size = max_dma_size > U32_MAX ? U32_MAX : max_dma_size;
1367 /* Host can optionally specify maximum segment size and number of
1369 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
1370 struct virtio_blk_config, size_max, &v);
1372 max_size = min(max_size, v);
1374 blk_queue_max_segment_size(q, max_size);
1376 /* Host can optionally specify the block size of the device */
1377 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
1378 struct virtio_blk_config, blk_size,
1381 err = blk_validate_block_size(blk_size);
1384 "virtio_blk: invalid block size: 0x%x\n",
1386 goto out_cleanup_disk;
1389 blk_queue_logical_block_size(q, blk_size);
1391 blk_size = queue_logical_block_size(q);
1393 /* Use topology information if available */
1394 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1395 struct virtio_blk_config, physical_block_exp,
1396 &physical_block_exp);
1397 if (!err && physical_block_exp)
1398 blk_queue_physical_block_size(q,
1399 blk_size * (1 << physical_block_exp));
1401 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1402 struct virtio_blk_config, alignment_offset,
1404 if (!err && alignment_offset)
1405 blk_queue_alignment_offset(q, blk_size * alignment_offset);
1407 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1408 struct virtio_blk_config, min_io_size,
1410 if (!err && min_io_size)
1411 blk_queue_io_min(q, blk_size * min_io_size);
1413 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1414 struct virtio_blk_config, opt_io_size,
1416 if (!err && opt_io_size)
1417 blk_queue_io_opt(q, blk_size * opt_io_size);
1419 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
1420 virtio_cread(vdev, struct virtio_blk_config,
1421 discard_sector_alignment, &discard_granularity);
1423 virtio_cread(vdev, struct virtio_blk_config,
1424 max_discard_sectors, &v);
1425 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
1427 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
1431 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
1432 virtio_cread(vdev, struct virtio_blk_config,
1433 max_write_zeroes_sectors, &v);
1434 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
1437 /* The discard and secure erase limits are combined since the Linux
1438 * block layer uses the same limit for both commands.
1440 * If both VIRTIO_BLK_F_SECURE_ERASE and VIRTIO_BLK_F_DISCARD features
1441 * are negotiated, we will use the minimum between the limits.
1443 * discard sector alignment is set to the minimum between discard_sector_alignment
1444 * and secure_erase_sector_alignment.
1446 * max discard sectors is set to the minimum between max_discard_seg and
1447 * max_secure_erase_seg.
1449 if (virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
1451 virtio_cread(vdev, struct virtio_blk_config,
1452 secure_erase_sector_alignment, &v);
1454 /* secure_erase_sector_alignment should not be zero, the device should set a
1455 * valid number of sectors.
1459 "virtio_blk: secure_erase_sector_alignment can't be 0\n");
1461 goto out_cleanup_disk;
1464 discard_granularity = min_not_zero(discard_granularity, v);
1466 virtio_cread(vdev, struct virtio_blk_config,
1467 max_secure_erase_sectors, &v);
1469 /* max_secure_erase_sectors should not be zero, the device should set a
1470 * valid number of sectors.
1474 "virtio_blk: max_secure_erase_sectors can't be 0\n");
1476 goto out_cleanup_disk;
1479 blk_queue_max_secure_erase_sectors(q, v);
1481 virtio_cread(vdev, struct virtio_blk_config,
1482 max_secure_erase_seg, &v);
1484 /* max_secure_erase_seg should not be zero, the device should set a
1485 * valid number of segments
1489 "virtio_blk: max_secure_erase_seg can't be 0\n");
1491 goto out_cleanup_disk;
1494 max_discard_segs = min_not_zero(max_discard_segs, v);
1497 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD) ||
1498 virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
1499 /* max_discard_seg and discard_granularity will be 0 only
1500 * if max_discard_seg and discard_sector_alignment fields in the virtio
1501 * config are 0 and VIRTIO_BLK_F_SECURE_ERASE feature is not negotiated.
1502 * In this case, we use default values.
1504 if (!max_discard_segs)
1505 max_discard_segs = sg_elems;
1507 blk_queue_max_discard_segments(q,
1508 min(max_discard_segs, MAX_DISCARD_SEGMENTS));
1510 if (discard_granularity)
1511 q->limits.discard_granularity = discard_granularity << SECTOR_SHIFT;
1513 q->limits.discard_granularity = blk_size;
1516 virtblk_update_capacity(vblk, false);
1517 virtio_device_ready(vdev);
1520 * All steps that follow use the VQs therefore they need to be
1521 * placed after the virtio_device_ready() call above.
1523 if (virtio_has_feature(vdev, VIRTIO_BLK_F_ZONED)) {
1526 virtio_cread(vdev, struct virtio_blk_config, zoned.model,
1529 case VIRTIO_BLK_Z_NONE:
1530 case VIRTIO_BLK_Z_HA:
1531 /* Present the host-aware device as non-zoned */
1533 case VIRTIO_BLK_Z_HM:
1534 err = virtblk_probe_zoned_device(vdev, vblk, q);
1536 goto out_cleanup_disk;
1539 dev_err(&vdev->dev, "unsupported zone model %d\n",
1542 goto out_cleanup_disk;
1546 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
1548 goto out_cleanup_disk;
1553 put_disk(vblk->disk);
1555 blk_mq_free_tag_set(&vblk->tag_set);
1557 vdev->config->del_vqs(vdev);
1562 ida_free(&vd_index_ida, index);
1567 static void virtblk_remove(struct virtio_device *vdev)
1569 struct virtio_blk *vblk = vdev->priv;
1571 /* Make sure no work handler is accessing the device. */
1572 flush_work(&vblk->config_work);
1574 del_gendisk(vblk->disk);
1575 blk_mq_free_tag_set(&vblk->tag_set);
1577 mutex_lock(&vblk->vdev_mutex);
1579 /* Stop all the virtqueues. */
1580 virtio_reset_device(vdev);
1582 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
1585 vdev->config->del_vqs(vdev);
1588 mutex_unlock(&vblk->vdev_mutex);
1590 put_disk(vblk->disk);
1593 #ifdef CONFIG_PM_SLEEP
1594 static int virtblk_freeze(struct virtio_device *vdev)
1596 struct virtio_blk *vblk = vdev->priv;
1598 /* Ensure we don't receive any more interrupts */
1599 virtio_reset_device(vdev);
1601 /* Make sure no work handler is accessing the device. */
1602 flush_work(&vblk->config_work);
1604 blk_mq_quiesce_queue(vblk->disk->queue);
1606 vdev->config->del_vqs(vdev);
1612 static int virtblk_restore(struct virtio_device *vdev)
1614 struct virtio_blk *vblk = vdev->priv;
1617 ret = init_vq(vdev->priv);
1621 virtio_device_ready(vdev);
1623 blk_mq_unquiesce_queue(vblk->disk->queue);
1628 static const struct virtio_device_id id_table[] = {
1629 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1633 static unsigned int features_legacy[] = {
1634 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1635 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1636 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1637 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1638 VIRTIO_BLK_F_SECURE_ERASE,
1641 static unsigned int features[] = {
1642 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1643 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
1644 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1645 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
1646 VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED,
1649 static struct virtio_driver virtio_blk = {
1650 .feature_table = features,
1651 .feature_table_size = ARRAY_SIZE(features),
1652 .feature_table_legacy = features_legacy,
1653 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1654 .driver.name = KBUILD_MODNAME,
1655 .driver.owner = THIS_MODULE,
1656 .id_table = id_table,
1657 .probe = virtblk_probe,
1658 .remove = virtblk_remove,
1659 .config_changed = virtblk_config_changed,
1660 #ifdef CONFIG_PM_SLEEP
1661 .freeze = virtblk_freeze,
1662 .restore = virtblk_restore,
1666 static int __init virtio_blk_init(void)
1670 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1674 major = register_blkdev(0, "virtblk");
1677 goto out_destroy_workqueue;
1680 error = register_virtio_driver(&virtio_blk);
1682 goto out_unregister_blkdev;
1685 out_unregister_blkdev:
1686 unregister_blkdev(major, "virtblk");
1687 out_destroy_workqueue:
1688 destroy_workqueue(virtblk_wq);
1692 static void __exit virtio_blk_fini(void)
1694 unregister_virtio_driver(&virtio_blk);
1695 unregister_blkdev(major, "virtblk");
1696 destroy_workqueue(virtblk_wq);
1698 module_init(virtio_blk_init);
1699 module_exit(virtio_blk_fini);
1701 MODULE_DEVICE_TABLE(virtio, id_table);
1702 MODULE_DESCRIPTION("Virtio block driver");
1703 MODULE_LICENSE("GPL");