blob: c443cd64fc9b46051d978adb5bd95ce0b1d3a5fe [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Rusty Russelle467cde2007-10-22 11:03:38 +10002//#define DEBUG
3#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Rusty Russelle467cde2007-10-22 11:03:38 +10005#include <linux/blkdev.h>
6#include <linux/hdreg.h>
Paul Gortmaker0c8d44f2011-07-01 15:56:05 -04007#include <linux/module.h>
Michael S. Tsirkin4678d6f92012-01-12 15:44:44 +10308#include <linux/mutex.h>
Christoph Hellwigad714732017-02-05 18:15:25 +01009#include <linux/interrupt.h>
Rusty Russelle467cde2007-10-22 11:03:38 +100010#include <linux/virtio.h>
11#include <linux/virtio_blk.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020012#include <linux/scatterlist.h>
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010013#include <linux/string_helpers.h>
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020014#include <linux/idr.h>
Jens Axboe1cf7e9c2013-11-01 10:52:52 -060015#include <linux/blk-mq.h>
Christoph Hellwigad714732017-02-05 18:15:25 +010016#include <linux/blk-mq-virtio.h>
Jens Axboe1cf7e9c2013-11-01 10:52:52 -060017#include <linux/numa.h>
Michael S. Tsirkin55a24152020-04-17 03:14:34 -040018#include <uapi/linux/virtio_ring.h>
Jens Axboe3d1266c2007-10-24 13:21:21 +020019
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010020#define PART_BITS 4
Ming Lei6a27b652014-06-26 17:41:48 +080021#define VQ_NAME_LEN 16
Changpeng Liu1f238162018-11-01 15:40:35 -070022#define MAX_DISCARD_SEGMENTS 256u
Rusty Russelle467cde2007-10-22 11:03:38 +100023
Stefan Hajnoczi63947b32021-05-24 16:40:20 +010024/* The maximum number of sg elements that fit into a virtqueue */
25#define VIRTIO_BLK_MAX_SG_ELEMS 32768
26
Max Gurtovoy02746e22021-09-01 16:14:34 +030027#ifdef CONFIG_ARCH_NO_SG_CHAIN
28#define VIRTIO_BLK_INLINE_SG_CNT 0
29#else
30#define VIRTIO_BLK_INLINE_SG_CNT 2
31#endif
32
Max Gurtovoy0989c412021-09-02 23:46:22 +030033static unsigned int num_request_queues;
Michael S. Tsirkinead65f72021-10-24 09:41:40 -040034module_param(num_request_queues, uint, 0644);
Max Gurtovoy0989c412021-09-02 23:46:22 +030035MODULE_PARM_DESC(num_request_queues,
36 "Limit the number of request queues to use for blk device. "
37 "0 for no limit. "
38 "Values > nr_cpu_ids truncated to nr_cpu_ids.");
39
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020040static int major;
41static DEFINE_IDA(vd_index_ida);
42
Jonghwan Choi2a647bf2013-05-20 10:25:39 +093043static struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010044
Ming Lei6a27b652014-06-26 17:41:48 +080045struct virtio_blk_vq {
46 struct virtqueue *vq;
47 spinlock_t lock;
48 char name[VQ_NAME_LEN];
49} ____cacheline_aligned_in_smp;
50
Michael S. Tsirkinbb6ec572015-01-15 13:33:31 +020051struct virtio_blk {
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +010052 /*
53 * This mutex must be held by anything that may run after
54 * virtblk_remove() sets vblk->vdev to NULL.
55 *
56 * blk-mq, virtqueue processing, and sysfs attribute code paths are
57 * shut down before vblk->vdev is set to NULL and therefore do not need
58 * to hold this mutex.
59 */
60 struct mutex vdev_mutex;
Rusty Russelle467cde2007-10-22 11:03:38 +100061 struct virtio_device *vdev;
Rusty Russelle467cde2007-10-22 11:03:38 +100062
63 /* The disk structure for the kernel. */
64 struct gendisk *disk;
65
Christoph Hellwig24d2f902014-04-15 14:14:00 -060066 /* Block layer tags. */
67 struct blk_mq_tag_set tag_set;
68
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010069 /* Process context for config space updates */
70 struct work_struct config_work;
71
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +010072 /*
73 * Tracks references from block_device_operations open/release and
74 * virtio_driver probe/remove so this object can be freed once no
75 * longer in use.
76 */
77 refcount_t refs;
78
Rusty Russell0864b792008-12-30 09:26:05 -060079 /* What host tells us, plus 2 for header & tailer. */
80 unsigned int sg_elems;
81
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020082 /* Ida index - used to track minor number allocations. */
83 int index;
Ming Lei6a27b652014-06-26 17:41:48 +080084
85 /* num of vqs */
86 int num_vqs;
87 struct virtio_blk_vq *vqs;
Rusty Russelle467cde2007-10-22 11:03:38 +100088};
89
Michael S. Tsirkinbb6ec572015-01-15 13:33:31 +020090struct virtblk_req {
Christoph Hellwig97b50a62017-01-28 09:32:53 +010091 struct virtio_blk_outhdr out_hdr;
92 u8 status;
Max Gurtovoy02746e22021-09-01 16:14:34 +030093 struct sg_table sg_table;
Asias Hea98755c2012-08-08 16:07:04 +080094 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +100095};
96
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020097static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
Asias Hea98755c2012-08-08 16:07:04 +080098{
99 switch (vbr->status) {
100 case VIRTIO_BLK_S_OK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200101 return BLK_STS_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800102 case VIRTIO_BLK_S_UNSUPP:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200103 return BLK_STS_NOTSUPP;
Asias Hea98755c2012-08-08 16:07:04 +0800104 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200105 return BLK_STS_IOERR;
Asias Hea98755c2012-08-08 16:07:04 +0800106 }
107}
108
Christoph Hellwig97b50a62017-01-28 09:32:53 +0100109static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
110 struct scatterlist *data_sg, bool have_data)
111{
112 struct scatterlist hdr, status, *sgs[3];
113 unsigned int num_out = 0, num_in = 0;
114
115 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
116 sgs[num_out++] = &hdr;
117
118 if (have_data) {
119 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
120 sgs[num_out++] = data_sg;
121 else
122 sgs[num_out + num_in++] = data_sg;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030123 }
124
125 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
126 sgs[num_out + num_in++] = &status;
127
128 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030129}
Asias Hec85a1f92012-08-08 16:07:05 +0800130
Changpeng Liu1f238162018-11-01 15:40:35 -0700131static int virtblk_setup_discard_write_zeroes(struct request *req, bool unmap)
132{
133 unsigned short segments = blk_rq_nr_discard_segments(req);
134 unsigned short n = 0;
135 struct virtio_blk_discard_write_zeroes *range;
136 struct bio *bio;
137 u32 flags = 0;
138
139 if (unmap)
140 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
141
142 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
143 if (!range)
144 return -ENOMEM;
145
Ming Leiaf822aa2020-08-17 17:52:40 +0800146 /*
147 * Single max discard segment means multi-range discard isn't
148 * supported, and block layer only runs contiguity merge like
149 * normal RW request. So we can't reply on bio for retrieving
150 * each range info.
151 */
152 if (queue_max_discard_segments(req->q) == 1) {
153 range[0].flags = cpu_to_le32(flags);
154 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
155 range[0].sector = cpu_to_le64(blk_rq_pos(req));
156 n = 1;
157 } else {
158 __rq_for_each_bio(bio, req) {
159 u64 sector = bio->bi_iter.bi_sector;
160 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Changpeng Liu1f238162018-11-01 15:40:35 -0700161
Ming Leiaf822aa2020-08-17 17:52:40 +0800162 range[n].flags = cpu_to_le32(flags);
163 range[n].num_sectors = cpu_to_le32(num_sectors);
164 range[n].sector = cpu_to_le64(sector);
165 n++;
166 }
Changpeng Liu1f238162018-11-01 15:40:35 -0700167 }
168
Ming Leiaf822aa2020-08-17 17:52:40 +0800169 WARN_ON_ONCE(n != segments);
170
Changpeng Liu1f238162018-11-01 15:40:35 -0700171 req->special_vec.bv_page = virt_to_page(range);
172 req->special_vec.bv_offset = offset_in_page(range);
173 req->special_vec.bv_len = sizeof(*range) * segments;
174 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
175
176 return 0;
177}
178
Max Gurtovoy02746e22021-09-01 16:14:34 +0300179static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
180{
181 if (blk_rq_nr_phys_segments(req))
182 sg_free_table_chained(&vbr->sg_table,
183 VIRTIO_BLK_INLINE_SG_CNT);
184}
185
186static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
187 struct virtblk_req *vbr)
188{
189 int err;
190
191 if (!blk_rq_nr_phys_segments(req))
192 return 0;
193
194 vbr->sg_table.sgl = vbr->sg;
195 err = sg_alloc_table_chained(&vbr->sg_table,
196 blk_rq_nr_phys_segments(req),
197 vbr->sg_table.sgl,
198 VIRTIO_BLK_INLINE_SG_CNT);
199 if (unlikely(err))
200 return -ENOMEM;
201
202 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
203}
204
205static void virtblk_cleanup_cmd(struct request *req)
206{
207 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
208 kfree(bvec_virt(&req->special_vec));
209}
210
Michael S. Tsirkinf0839372021-10-25 03:54:03 -0400211static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
212 struct request *req,
213 struct virtblk_req *vbr)
Max Gurtovoy02746e22021-09-01 16:14:34 +0300214{
215 bool unmap = false;
216 u32 type;
217
218 vbr->out_hdr.sector = 0;
219
220 switch (req_op(req)) {
221 case REQ_OP_READ:
222 type = VIRTIO_BLK_T_IN;
223 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
224 blk_rq_pos(req));
225 break;
226 case REQ_OP_WRITE:
227 type = VIRTIO_BLK_T_OUT;
228 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
229 blk_rq_pos(req));
230 break;
231 case REQ_OP_FLUSH:
232 type = VIRTIO_BLK_T_FLUSH;
233 break;
234 case REQ_OP_DISCARD:
235 type = VIRTIO_BLK_T_DISCARD;
236 break;
237 case REQ_OP_WRITE_ZEROES:
238 type = VIRTIO_BLK_T_WRITE_ZEROES;
239 unmap = !(req->cmd_flags & REQ_NOUNMAP);
240 break;
241 case REQ_OP_DRV_IN:
242 type = VIRTIO_BLK_T_GET_ID;
243 break;
244 default:
245 WARN_ON_ONCE(1);
246 return BLK_STS_IOERR;
247 }
248
249 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
250 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
251
252 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
253 if (virtblk_setup_discard_write_zeroes(req, unmap))
254 return BLK_STS_RESOURCE;
255 }
256
257 return 0;
258}
259
Christoph Hellwig5124c282014-02-10 03:24:39 -0800260static inline void virtblk_request_done(struct request *req)
Asias Hec85a1f92012-08-08 16:07:05 +0800261{
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200262 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Asias Hea98755c2012-08-08 16:07:04 +0800263
Max Gurtovoy02746e22021-09-01 16:14:34 +0300264 virtblk_unmap_data(req, vbr);
265 virtblk_cleanup_cmd(req);
Christoph Hellwigd19633d2017-04-20 16:03:00 +0200266 blk_mq_end_request(req, virtblk_result(vbr));
Asias Hea98755c2012-08-08 16:07:04 +0800267}
268
269static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000270{
271 struct virtio_blk *vblk = vq->vdev->priv;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600272 bool req_done = false;
Ming Lei6a27b652014-06-26 17:41:48 +0800273 int qid = vq->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000274 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000275 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800276 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000277
Ming Lei6a27b652014-06-26 17:41:48 +0800278 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800279 do {
280 virtqueue_disable_cb(vq);
Ming Lei6a27b652014-06-26 17:41:48 +0800281 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
Christoph Hellwig85dada02017-01-28 09:32:52 +0100282 struct request *req = blk_mq_rq_from_pdu(vbr);
283
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200284 if (likely(!blk_should_fake_timeout(req->q)))
285 blk_mq_complete_request(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600286 req_done = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000287 }
Heinz Graalfs7f03b172013-10-29 09:40:30 +1030288 if (unlikely(virtqueue_is_broken(vq)))
289 break;
Asias Hebb811102012-09-25 10:36:17 +0800290 } while (!virtqueue_enable_cb(vq));
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600291
Rusty Russelle467cde2007-10-22 11:03:38 +1000292 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800293 if (req_done)
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200294 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
Ming Lei6a27b652014-06-26 17:41:48 +0800295 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800296}
297
Jens Axboe944e7c82018-11-26 11:00:12 -0700298static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
299{
300 struct virtio_blk *vblk = hctx->queue->queuedata;
301 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
302 bool kick;
303
304 spin_lock_irq(&vq->lock);
305 kick = virtqueue_kick_prepare(vq->vq);
306 spin_unlock_irq(&vq->lock);
307
308 if (kick)
309 virtqueue_notify(vq->vq);
310}
311
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200312static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -0600313 const struct blk_mq_queue_data *bd)
Rusty Russelle467cde2007-10-22 11:03:38 +1000314{
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600315 struct virtio_blk *vblk = hctx->queue->queuedata;
Jens Axboe74c45052014-10-29 11:14:52 -0600316 struct request *req = bd->rq;
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200317 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600318 unsigned long flags;
Ye Guojin0466a392021-11-17 06:39:55 +0000319 int num;
Ming Lei6a27b652014-06-26 17:41:48 +0800320 int qid = hctx->queue_num;
Ming Leie8edca62014-05-30 10:49:29 +0800321 bool notify = false;
Michael S. Tsirkinf0839372021-10-25 03:54:03 -0400322 blk_status_t status;
323 int err;
Rusty Russelle467cde2007-10-22 11:03:38 +1000324
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600325 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000326
Michael S. Tsirkinf0839372021-10-25 03:54:03 -0400327 status = virtblk_setup_cmd(vblk->vdev, req, vbr);
328 if (unlikely(status))
329 return status;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100330
Christoph Hellwige2490072014-09-13 16:40:09 -0700331 blk_mq_start_request(req);
332
Max Gurtovoy02746e22021-09-01 16:14:34 +0300333 num = virtblk_map_data(hctx, req, vbr);
334 if (unlikely(num < 0)) {
335 virtblk_cleanup_cmd(req);
336 return BLK_STS_RESOURCE;
Rusty Russelle467cde2007-10-22 11:03:38 +1000337 }
338
Ming Lei6a27b652014-06-26 17:41:48 +0800339 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300340 err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg_table.sgl, num);
Rusty Russell5261b852014-03-13 11:23:39 +1030341 if (err) {
Ming Lei6a27b652014-06-26 17:41:48 +0800342 virtqueue_kick(vblk->vqs[qid].vq);
Halil Pasicf5f6b952020-02-13 13:37:27 +0100343 /* Don't stop the queue if -ENOMEM: we may have failed to
344 * bounce the buffer due to global resource outage.
345 */
346 if (err == -ENOSPC)
347 blk_mq_stop_hw_queue(hctx);
Ming Lei6a27b652014-06-26 17:41:48 +0800348 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300349 virtblk_unmap_data(req, vbr);
350 virtblk_cleanup_cmd(req);
Halil Pasic3d973b22020-02-13 13:37:28 +0100351 switch (err) {
352 case -ENOSPC:
Ming Lei86ff7c22018-01-30 22:04:57 -0500353 return BLK_STS_DEV_RESOURCE;
Halil Pasic3d973b22020-02-13 13:37:28 +0100354 case -ENOMEM:
355 return BLK_STS_RESOURCE;
356 default:
357 return BLK_STS_IOERR;
358 }
Asias Hea98755c2012-08-08 16:07:04 +0800359 }
360
Jens Axboe74c45052014-10-29 11:14:52 -0600361 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
Ming Leie8edca62014-05-30 10:49:29 +0800362 notify = true;
Ming Lei6a27b652014-06-26 17:41:48 +0800363 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Ming Leie8edca62014-05-30 10:49:29 +0800364
365 if (notify)
Ming Lei6a27b652014-06-26 17:41:48 +0800366 virtqueue_notify(vblk->vqs[qid].vq);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200367 return BLK_STS_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800368}
369
john cooper4cb2ea22010-03-25 01:33:33 -0400370/* return id (s/n) string for *disk to *id_str
371 */
372static int virtblk_get_id(struct gendisk *disk, char *id_str)
373{
374 struct virtio_blk *vblk = disk->private_data;
Christoph Hellwigf9596692016-07-19 11:31:49 +0200375 struct request_queue *q = vblk->disk->queue;
john cooper4cb2ea22010-03-25 01:33:33 -0400376 struct request *req;
Mike Snitzere4c47762010-10-09 12:12:13 +1030377 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400378
Christoph Hellwig0bf6d962021-10-25 09:05:07 +0200379 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
Christoph Hellwigf9596692016-07-19 11:31:49 +0200380 if (IS_ERR(req))
john cooper4cb2ea22010-03-25 01:33:33 -0400381 return PTR_ERR(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030382
Christoph Hellwigf9596692016-07-19 11:31:49 +0200383 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
384 if (err)
385 goto out;
386
Christoph Hellwigb84ba302021-11-26 13:18:01 +0100387 blk_execute_rq(req, false);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200388 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
Christoph Hellwigf9596692016-07-19 11:31:49 +0200389out:
Christoph Hellwig0bf6d962021-10-25 09:05:07 +0200390 blk_mq_free_request(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030391 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400392}
393
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100394static void virtblk_get(struct virtio_blk *vblk)
395{
396 refcount_inc(&vblk->refs);
397}
398
399static void virtblk_put(struct virtio_blk *vblk)
400{
401 if (refcount_dec_and_test(&vblk->refs)) {
402 ida_simple_remove(&vd_index_ida, vblk->index);
403 mutex_destroy(&vblk->vdev_mutex);
404 kfree(vblk);
405 }
406}
407
408static int virtblk_open(struct block_device *bd, fmode_t mode)
409{
410 struct virtio_blk *vblk = bd->bd_disk->private_data;
411 int ret = 0;
412
413 mutex_lock(&vblk->vdev_mutex);
414
415 if (vblk->vdev)
416 virtblk_get(vblk);
417 else
418 ret = -ENXIO;
419
420 mutex_unlock(&vblk->vdev_mutex);
421 return ret;
422}
423
424static void virtblk_release(struct gendisk *disk, fmode_t mode)
425{
426 struct virtio_blk *vblk = disk->private_data;
427
428 virtblk_put(vblk);
429}
430
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100431/* We provide getgeo only to please some old bootloader/partitioning tools */
432static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
433{
Ryan Harper48e40432008-04-16 13:56:37 -0500434 struct virtio_blk *vblk = bd->bd_disk->private_data;
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100435 int ret = 0;
436
437 mutex_lock(&vblk->vdev_mutex);
438
439 if (!vblk->vdev) {
440 ret = -ENXIO;
441 goto out;
442 }
Ryan Harper48e40432008-04-16 13:56:37 -0500443
444 /* see if the host passed in geometry config */
Rusty Russell855e0c52013-10-14 18:11:51 +1030445 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
446 virtio_cread(vblk->vdev, struct virtio_blk_config,
447 geometry.cylinders, &geo->cylinders);
448 virtio_cread(vblk->vdev, struct virtio_blk_config,
449 geometry.heads, &geo->heads);
450 virtio_cread(vblk->vdev, struct virtio_blk_config,
451 geometry.sectors, &geo->sectors);
Ryan Harper48e40432008-04-16 13:56:37 -0500452 } else {
453 /* some standard values, similar to sd */
454 geo->heads = 1 << 6;
455 geo->sectors = 1 << 5;
456 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
457 }
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100458out:
459 mutex_unlock(&vblk->vdev_mutex);
460 return ret;
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100461}
462
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700463static const struct block_device_operations virtblk_fops = {
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100464 .owner = THIS_MODULE,
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100465 .open = virtblk_open,
466 .release = virtblk_release,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100467 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000468};
469
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100470static int index_to_minor(int index)
471{
472 return index << PART_BITS;
473}
474
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200475static int minor_to_index(int minor)
476{
477 return minor >> PART_BITS;
478}
479
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200480static ssize_t serial_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500482{
483 struct gendisk *disk = dev_to_disk(dev);
484 int err;
485
486 /* sysfs gives us a PAGE_SIZE buffer */
487 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
488
489 buf[VIRTIO_BLK_ID_BYTES] = '\0';
490 err = virtblk_get_id(disk, buf);
491 if (!err)
492 return strlen(buf);
493
494 if (err == -EIO) /* Unsupported? Make it empty. */
495 return 0;
496
497 return err;
498}
Michael S. Tsirkin393c5252014-10-23 16:08:44 +0300499
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200500static DEVICE_ATTR_RO(serial);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500501
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000502/* The queue's logical block size must be set before calling this */
503static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100504{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100505 struct virtio_device *vdev = vblk->vdev;
506 struct request_queue *q = vblk->disk->queue;
507 char cap_str_2[10], cap_str_10[10];
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100508 unsigned long long nblocks;
James Bottomleyb9f28d82015-03-05 18:47:01 -0800509 u64 capacity;
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100510
511 /* Host must always specify the capacity. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030512 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100513
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100514 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
515
516 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800517 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100518 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800519 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100520
521 dev_notice(&vdev->dev,
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000522 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
523 vblk->disk->disk_name,
524 resize ? "new size: " : "",
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100525 nblocks,
526 queue_logical_block_size(q),
527 cap_str_10,
528 cap_str_2);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100529
Christoph Hellwig449f4ec2020-11-16 15:56:56 +0100530 set_capacity_and_notify(vblk->disk, capacity);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000531}
532
533static void virtblk_config_changed_work(struct work_struct *work)
534{
535 struct virtio_blk *vblk =
536 container_of(work, struct virtio_blk, config_work);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000537
538 virtblk_update_capacity(vblk, true);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100539}
540
541static void virtblk_config_changed(struct virtio_device *vdev)
542{
543 struct virtio_blk *vblk = vdev->priv;
544
545 queue_work(virtblk_wq, &vblk->config_work);
546}
547
Amit Shah6abd6e52011-12-22 16:58:29 +0530548static int init_vq(struct virtio_blk *vblk)
549{
Markus Elfring2ff98442016-09-13 13:43:50 +0200550 int err;
Ming Lei6a27b652014-06-26 17:41:48 +0800551 int i;
552 vq_callback_t **callbacks;
553 const char **names;
554 struct virtqueue **vqs;
555 unsigned short num_vqs;
556 struct virtio_device *vdev = vblk->vdev;
Christoph Hellwigad714732017-02-05 18:15:25 +0100557 struct irq_affinity desc = { 0, };
Amit Shah6abd6e52011-12-22 16:58:29 +0530558
Ming Lei6a27b652014-06-26 17:41:48 +0800559 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
560 struct virtio_blk_config, num_queues,
561 &num_vqs);
562 if (err)
563 num_vqs = 1;
Jason Wang6ae6ff62021-10-19 15:01:43 +0800564 if (!err && !num_vqs) {
Colin Ian King63b4ffa2021-10-25 11:22:40 +0100565 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
Jason Wang6ae6ff62021-10-19 15:01:43 +0800566 return -EINVAL;
567 }
Amit Shah6abd6e52011-12-22 16:58:29 +0530568
Max Gurtovoy0989c412021-09-02 23:46:22 +0300569 num_vqs = min_t(unsigned int,
570 min_not_zero(num_request_queues, nr_cpu_ids),
571 num_vqs);
Dongli Zhangbf348f92019-03-27 18:36:34 +0800572
Markus Elfring668866b2016-09-13 11:32:22 +0200573 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800574 if (!vblk->vqs)
575 return -ENOMEM;
576
Markus Elfring668866b2016-09-13 11:32:22 +0200577 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
578 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
579 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800580 if (!names || !callbacks || !vqs) {
Ming Lei6a27b652014-06-26 17:41:48 +0800581 err = -ENOMEM;
582 goto out;
583 }
584
Ming Lei6a27b652014-06-26 17:41:48 +0800585 for (i = 0; i < num_vqs; i++) {
586 callbacks[i] = virtblk_done;
587 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
588 names[i] = vblk->vqs[i].name;
589 }
590
591 /* Discover virtqueues and write information to configuration. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200592 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
Ming Lei6a27b652014-06-26 17:41:48 +0800593 if (err)
Minfei Huang347a5292016-08-09 16:39:20 +0800594 goto out;
Ming Lei6a27b652014-06-26 17:41:48 +0800595
596 for (i = 0; i < num_vqs; i++) {
597 spin_lock_init(&vblk->vqs[i].lock);
598 vblk->vqs[i].vq = vqs[i];
599 }
600 vblk->num_vqs = num_vqs;
601
Minfei Huang347a5292016-08-09 16:39:20 +0800602out:
Ming Lei6a27b652014-06-26 17:41:48 +0800603 kfree(vqs);
Ming Lei6a27b652014-06-26 17:41:48 +0800604 kfree(callbacks);
Ming Lei6a27b652014-06-26 17:41:48 +0800605 kfree(names);
Ming Lei6a27b652014-06-26 17:41:48 +0800606 if (err)
607 kfree(vblk->vqs);
Amit Shah6abd6e52011-12-22 16:58:29 +0530608 return err;
609}
610
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800611/*
612 * Legacy naming scheme used for virtio devices. We are stuck with it for
613 * virtio blk but don't ever use it for any new driver.
614 */
615static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
616{
617 const int base = 'z' - 'a' + 1;
618 char *begin = buf + strlen(prefix);
619 char *end = buf + buflen;
620 char *p;
621 int unit;
622
623 p = end - 1;
624 *p = '\0';
625 unit = base;
626 do {
627 if (p == begin)
628 return -EINVAL;
629 *--p = 'a' + (index % unit);
630 index = (index / unit) - 1;
631 } while (index >= 0);
632
633 memmove(begin, p, end - p);
634 memcpy(buf, prefix, strlen(prefix));
635
636 return 0;
637}
638
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200639static int virtblk_get_cache_mode(struct virtio_device *vdev)
640{
641 u8 writeback;
642 int err;
643
Rusty Russell855e0c52013-10-14 18:11:51 +1030644 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
645 struct virtio_blk_config, wce,
646 &writeback);
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200647
648 /*
649 * If WCE is not configurable and flush is not available,
650 * assume no writeback cache is in use.
651 */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200652 if (err)
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200653 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200654
655 return writeback;
656}
657
658static void virtblk_update_cache_mode(struct virtio_device *vdev)
659{
660 u8 writeback = virtblk_get_cache_mode(vdev);
661 struct virtio_blk *vblk = vdev->priv;
662
Jens Axboead9126a2016-03-30 10:12:58 -0600663 blk_queue_write_cache(vblk->disk->queue, writeback, false);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200664}
665
666static const char *const virtblk_cache_types[] = {
667 "write through", "write back"
668};
669
670static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200671cache_type_store(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200673{
674 struct gendisk *disk = dev_to_disk(dev);
675 struct virtio_blk *vblk = disk->private_data;
676 struct virtio_device *vdev = vblk->vdev;
677 int i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200678
679 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300680 i = sysfs_match_string(virtblk_cache_types, buf);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200681 if (i < 0)
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300682 return i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200683
Rusty Russell855e0c52013-10-14 18:11:51 +1030684 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200685 virtblk_update_cache_mode(vdev);
686 return count;
687}
688
689static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200690cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200691{
692 struct gendisk *disk = dev_to_disk(dev);
693 struct virtio_blk *vblk = disk->private_data;
694 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
695
696 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
Ye Guojinf1aa12f2021-10-21 06:51:11 +0000697 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200698}
699
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200700static DEVICE_ATTR_RW(cache_type);
701
702static struct attribute *virtblk_attrs[] = {
703 &dev_attr_serial.attr,
704 &dev_attr_cache_type.attr,
705 NULL,
706};
707
708static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
709 struct attribute *a, int n)
710{
Tian Tao4ce79062020-08-21 09:19:15 +0800711 struct device *dev = kobj_to_dev(kobj);
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200712 struct gendisk *disk = dev_to_disk(dev);
713 struct virtio_blk *vblk = disk->private_data;
714 struct virtio_device *vdev = vblk->vdev;
715
716 if (a == &dev_attr_cache_type.attr &&
717 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
718 return S_IRUGO;
719
720 return a->mode;
721}
722
723static const struct attribute_group virtblk_attr_group = {
724 .attrs = virtblk_attrs,
725 .is_visible = virtblk_attrs_are_visible,
726};
727
728static const struct attribute_group *virtblk_attr_groups[] = {
729 &virtblk_attr_group,
730 NULL,
731};
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200732
Christoph Hellwigad714732017-02-05 18:15:25 +0100733static int virtblk_map_queues(struct blk_mq_tag_set *set)
734{
735 struct virtio_blk *vblk = set->driver_data;
736
Dongli Zhang9bc00752019-03-12 09:31:56 +0800737 return blk_mq_virtio_map_queues(&set->map[HCTX_TYPE_DEFAULT],
738 vblk->vdev, 0);
Christoph Hellwigad714732017-02-05 18:15:25 +0100739}
740
Eric Biggersf363b082017-03-30 13:39:16 -0700741static const struct blk_mq_ops virtio_mq_ops = {
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600742 .queue_rq = virtio_queue_rq,
Jens Axboe944e7c82018-11-26 11:00:12 -0700743 .commit_rqs = virtio_commit_rqs,
Christoph Hellwig5124c282014-02-10 03:24:39 -0800744 .complete = virtblk_request_done,
Christoph Hellwigad714732017-02-05 18:15:25 +0100745 .map_queues = virtblk_map_queues,
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600746};
747
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600748static unsigned int virtblk_queue_depth;
749module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600750
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800751static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000752{
753 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600754 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200755 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800756
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100757 u32 v, blk_size, max_size, sg_elems, opt_io_size;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600758 u16 min_io_size;
759 u8 physical_block_exp, alignment_offset;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800760 unsigned int queue_depth;
Rusty Russelle467cde2007-10-22 11:03:38 +1000761
Michael S. Tsirkinff631982021-10-04 11:31:00 -0400762 if (!vdev->config->get) {
763 dev_err(&vdev->dev, "%s failure: config access disabled\n",
764 __func__);
765 return -EINVAL;
766 }
767
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200768 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
769 GFP_KERNEL);
770 if (err < 0)
771 goto out;
772 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100773
Rusty Russell0864b792008-12-30 09:26:05 -0600774 /* We need to know how many segments before we allocate. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030775 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
776 struct virtio_blk_config, seg_max,
777 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200778
779 /* We need at least one SG element, whatever they say. */
780 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600781 sg_elems = 1;
782
Stefan Hajnoczi63947b32021-05-24 16:40:20 +0100783 /* Prevent integer overflows and honor max vq size */
784 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
785
786 /* We need extra sg elements at head and tail. */
Rusty Russell0864b792008-12-30 09:26:05 -0600787 sg_elems += 2;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600788 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000789 if (!vblk) {
790 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200791 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000792 }
793
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100794 /* This reference is dropped in virtblk_remove(). */
795 refcount_set(&vblk->refs, 1);
796 mutex_init(&vblk->vdev_mutex);
797
Rusty Russelle467cde2007-10-22 11:03:38 +1000798 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600799 vblk->sg_elems = sg_elems;
Asias Hea98755c2012-08-08 16:07:04 +0800800
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100801 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000802
Amit Shah6abd6e52011-12-22 16:58:29 +0530803 err = init_vq(vblk);
804 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000805 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000806
Rusty Russellfc4324b2014-03-19 17:08:24 +1030807 /* Default queue sizing is to fill the ring. */
Max Gurtovoy6105d1f2021-09-05 11:57:17 +0300808 if (!virtblk_queue_depth) {
Joseph Qid1e9aa92021-01-22 17:21:46 +0800809 queue_depth = vblk->vqs[0].vq->num_free;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030810 /* ... but without indirect descs, we use 2 descs per req */
811 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
Joseph Qid1e9aa92021-01-22 17:21:46 +0800812 queue_depth /= 2;
813 } else {
814 queue_depth = virtblk_queue_depth;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030815 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600816
817 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
818 vblk->tag_set.ops = &virtio_mq_ops;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800819 vblk->tag_set.queue_depth = queue_depth;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600820 vblk->tag_set.numa_node = NUMA_NO_NODE;
821 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
822 vblk->tag_set.cmd_size =
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600823 sizeof(struct virtblk_req) +
Max Gurtovoy02746e22021-09-01 16:14:34 +0300824 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600825 vblk->tag_set.driver_data = vblk;
Ming Lei6a27b652014-06-26 17:41:48 +0800826 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600827
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600828 err = blk_mq_alloc_tag_set(&vblk->tag_set);
829 if (err)
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300830 goto out_free_vq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600831
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300832 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
833 if (IS_ERR(vblk->disk)) {
834 err = PTR_ERR(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600835 goto out_free_tags;
Rusty Russelle467cde2007-10-22 11:03:38 +1000836 }
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300837 q = vblk->disk->queue;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900838
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800839 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100840
Rusty Russelle467cde2007-10-22 11:03:38 +1000841 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100842 vblk->disk->first_minor = index_to_minor(index);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300843 vblk->disk->minors = 1 << PART_BITS;
Rusty Russelle467cde2007-10-22 11:03:38 +1000844 vblk->disk->private_data = vblk;
845 vblk->disk->fops = &virtblk_fops;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200846 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100847
Tejun Heo02c42b72010-09-03 11:56:18 +0200848 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200849 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000850
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200851 /* If disk is read-only in the host, the guest should obey */
852 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
853 set_disk_ro(vblk->disk, 1);
854
Rusty Russell0864b792008-12-30 09:26:05 -0600855 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500856 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600857
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600858 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500859 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600860
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100861 max_size = virtio_max_dma_size(vdev);
862
Rusty Russella586d4f2008-02-04 23:49:56 -0500863 /* Host can optionally specify maximum segment size and number of
864 * segments. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030865 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
866 struct virtio_blk_config, size_max, &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000867 if (!err)
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100868 max_size = min(max_size, v);
869
870 blk_queue_max_segment_size(q, max_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000871
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200872 /* Host can optionally specify the block size of the device */
Rusty Russell855e0c52013-10-14 18:11:51 +1030873 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
874 struct virtio_blk_config, blk_size,
875 &blk_size);
Xie Yongji57a13a52021-10-26 22:40:15 +0800876 if (!err) {
877 err = blk_validate_block_size(blk_size);
878 if (err) {
879 dev_err(&vdev->dev,
880 "virtio_blk: invalid block size: 0x%x\n",
881 blk_size);
882 goto out_cleanup_disk;
883 }
884
Christoph Hellwig69740c82010-02-24 14:22:25 -0600885 blk_queue_logical_block_size(q, blk_size);
Xie Yongji57a13a52021-10-26 22:40:15 +0800886 } else
Christoph Hellwig69740c82010-02-24 14:22:25 -0600887 blk_size = queue_logical_block_size(q);
888
889 /* Use topology information if available */
Rusty Russell855e0c52013-10-14 18:11:51 +1030890 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
891 struct virtio_blk_config, physical_block_exp,
892 &physical_block_exp);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600893 if (!err && physical_block_exp)
894 blk_queue_physical_block_size(q,
895 blk_size * (1 << physical_block_exp));
896
Rusty Russell855e0c52013-10-14 18:11:51 +1030897 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
898 struct virtio_blk_config, alignment_offset,
899 &alignment_offset);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600900 if (!err && alignment_offset)
901 blk_queue_alignment_offset(q, blk_size * alignment_offset);
902
Rusty Russell855e0c52013-10-14 18:11:51 +1030903 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
904 struct virtio_blk_config, min_io_size,
905 &min_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600906 if (!err && min_io_size)
907 blk_queue_io_min(q, blk_size * min_io_size);
908
Rusty Russell855e0c52013-10-14 18:11:51 +1030909 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
910 struct virtio_blk_config, opt_io_size,
911 &opt_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600912 if (!err && opt_io_size)
913 blk_queue_io_opt(q, blk_size * opt_io_size);
914
Changpeng Liu1f238162018-11-01 15:40:35 -0700915 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
916 q->limits.discard_granularity = blk_size;
917
918 virtio_cread(vdev, struct virtio_blk_config,
919 discard_sector_alignment, &v);
920 q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;
921
922 virtio_cread(vdev, struct virtio_blk_config,
923 max_discard_sectors, &v);
924 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
925
926 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
927 &v);
928 blk_queue_max_discard_segments(q,
929 min_not_zero(v,
930 MAX_DISCARD_SEGMENTS));
931
932 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
933 }
934
935 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
936 virtio_cread(vdev, struct virtio_blk_config,
937 max_write_zeroes_sectors, &v);
938 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
939 }
940
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000941 virtblk_update_capacity(vblk, false);
Michael S. Tsirkin7a113702014-10-15 10:22:30 +1030942 virtio_device_ready(vdev);
943
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200944 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
945 if (err)
946 goto out_cleanup_disk;
947
Rusty Russelle467cde2007-10-22 11:03:38 +1000948 return 0;
949
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200950out_cleanup_disk:
Xie Yongji82e89ea2021-08-09 18:16:09 +0800951 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600952out_free_tags:
953 blk_mq_free_tag_set(&vblk->tag_set);
Rusty Russelle467cde2007-10-22 11:03:38 +1000954out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600955 vdev->config->del_vqs(vdev);
Hou Taoe7eea442020-06-15 12:14:59 +0800956 kfree(vblk->vqs);
Rusty Russelle467cde2007-10-22 11:03:38 +1000957out_free_vblk:
958 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200959out_free_index:
960 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000961out:
962 return err;
963}
964
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800965static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000966{
967 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000968
Michael S. Tsirkincc74f712014-10-15 10:22:26 +1030969 /* Make sure no work handler is accessing the device. */
970 flush_work(&vblk->config_work);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100971
Asias He02e2b122012-05-25 10:34:47 +0800972 del_gendisk(vblk->disk);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300973 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600974 blk_mq_free_tag_set(&vblk->tag_set);
975
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100976 mutex_lock(&vblk->vdev_mutex);
977
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500978 /* Stop all the virtqueues. */
Michael S. Tsirkind9679d02021-10-13 06:55:44 -0400979 virtio_reset_device(vdev);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500980
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100981 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
982 vblk->vdev = NULL;
983
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600984 vdev->config->del_vqs(vdev);
Ming Lei6a27b652014-06-26 17:41:48 +0800985 kfree(vblk->vqs);
Alexander Graff4953fe2013-01-02 15:37:17 +1030986
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100987 mutex_unlock(&vblk->vdev_mutex);
988
989 virtblk_put(vblk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000990}
991
Aaron Lu89107002013-09-17 09:25:23 +0930992#ifdef CONFIG_PM_SLEEP
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530993static int virtblk_freeze(struct virtio_device *vdev)
994{
995 struct virtio_blk *vblk = vdev->priv;
996
997 /* Ensure we don't receive any more interrupts */
Michael S. Tsirkind9679d02021-10-13 06:55:44 -0400998 virtio_reset_device(vdev);
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530999
Michael S. Tsirkincc74f712014-10-15 10:22:26 +10301000 /* Make sure no work handler is accessing the device. */
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301001 flush_work(&vblk->config_work);
1002
Sagi Grimberg9b3e9902017-07-04 10:03:03 +03001003 blk_mq_quiesce_queue(vblk->disk->queue);
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301004
1005 vdev->config->del_vqs(vdev);
Xie Yongjib71ba222021-05-17 16:43:32 +08001006 kfree(vblk->vqs);
1007
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301008 return 0;
1009}
1010
1011static int virtblk_restore(struct virtio_device *vdev)
1012{
1013 struct virtio_blk *vblk = vdev->priv;
1014 int ret;
1015
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301016 ret = init_vq(vdev->priv);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301017 if (ret)
1018 return ret;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -06001019
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301020 virtio_device_ready(vdev);
1021
Sagi Grimberg9b3e9902017-07-04 10:03:03 +03001022 blk_mq_unquiesce_queue(vblk->disk->queue);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301023 return 0;
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301024}
1025#endif
1026
Márton Németh47483e22010-01-10 13:40:02 +01001027static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +10001028 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1029 { 0 },
1030};
1031
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001032static unsigned int features_legacy[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +02001033 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
Christoph Hellwig97b50a62017-01-28 09:32:53 +01001034 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001035 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001036 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001037}
1038;
1039static unsigned int features[] = {
1040 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1041 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001042 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001043 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Rusty Russellc45a6812008-05-02 21:50:50 -05001044};
1045
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001046static struct virtio_driver virtio_blk = {
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001047 .feature_table = features,
1048 .feature_table_size = ARRAY_SIZE(features),
1049 .feature_table_legacy = features_legacy,
1050 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1051 .driver.name = KBUILD_MODNAME,
1052 .driver.owner = THIS_MODULE,
1053 .id_table = id_table,
1054 .probe = virtblk_probe,
1055 .remove = virtblk_remove,
1056 .config_changed = virtblk_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301057#ifdef CONFIG_PM_SLEEP
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001058 .freeze = virtblk_freeze,
1059 .restore = virtblk_restore,
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301060#endif
Rusty Russelle467cde2007-10-22 11:03:38 +10001061};
1062
1063static int __init init(void)
1064{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001065 int error;
1066
1067 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1068 if (!virtblk_wq)
1069 return -ENOMEM;
1070
Christian Borntraeger4f3bf192008-01-31 15:53:53 +01001071 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001072 if (major < 0) {
1073 error = major;
1074 goto out_destroy_workqueue;
1075 }
1076
1077 error = register_virtio_driver(&virtio_blk);
1078 if (error)
1079 goto out_unregister_blkdev;
1080 return 0;
1081
1082out_unregister_blkdev:
1083 unregister_blkdev(major, "virtblk");
1084out_destroy_workqueue:
1085 destroy_workqueue(virtblk_wq);
1086 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +10001087}
1088
1089static void __exit fini(void)
1090{
Rusty Russelle467cde2007-10-22 11:03:38 +10001091 unregister_virtio_driver(&virtio_blk);
Michael S. Tsirkin38f37b52014-10-23 18:57:19 +03001092 unregister_blkdev(major, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001093 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +10001094}
1095module_init(init);
1096module_exit(fini);
1097
1098MODULE_DEVICE_TABLE(virtio, id_table);
1099MODULE_DESCRIPTION("Virtio block driver");
1100MODULE_LICENSE("GPL");