blob: dbcf2a7e4a00cb9e30bbb27a5904fe99fc6b12c6 [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 int virtblk_queue_count_set(const char *val,
34 const struct kernel_param *kp)
35{
36 return param_set_uint_minmax(val, kp, 1, nr_cpu_ids);
37}
38
39static const struct kernel_param_ops queue_count_ops = {
40 .set = virtblk_queue_count_set,
41 .get = param_get_uint,
42};
43
44static unsigned int num_request_queues;
45module_param_cb(num_request_queues, &queue_count_ops, &num_request_queues,
46 0644);
47MODULE_PARM_DESC(num_request_queues,
48 "Limit the number of request queues to use for blk device. "
49 "0 for no limit. "
50 "Values > nr_cpu_ids truncated to nr_cpu_ids.");
51
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020052static int major;
53static DEFINE_IDA(vd_index_ida);
54
Jonghwan Choi2a647bf2013-05-20 10:25:39 +093055static struct workqueue_struct *virtblk_wq;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +010056
Ming Lei6a27b652014-06-26 17:41:48 +080057struct virtio_blk_vq {
58 struct virtqueue *vq;
59 spinlock_t lock;
60 char name[VQ_NAME_LEN];
61} ____cacheline_aligned_in_smp;
62
Michael S. Tsirkinbb6ec572015-01-15 13:33:31 +020063struct virtio_blk {
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +010064 /*
65 * This mutex must be held by anything that may run after
66 * virtblk_remove() sets vblk->vdev to NULL.
67 *
68 * blk-mq, virtqueue processing, and sysfs attribute code paths are
69 * shut down before vblk->vdev is set to NULL and therefore do not need
70 * to hold this mutex.
71 */
72 struct mutex vdev_mutex;
Rusty Russelle467cde2007-10-22 11:03:38 +100073 struct virtio_device *vdev;
Rusty Russelle467cde2007-10-22 11:03:38 +100074
75 /* The disk structure for the kernel. */
76 struct gendisk *disk;
77
Christoph Hellwig24d2f902014-04-15 14:14:00 -060078 /* Block layer tags. */
79 struct blk_mq_tag_set tag_set;
80
Christoph Hellwig7a7c9242011-02-01 21:43:48 +010081 /* Process context for config space updates */
82 struct work_struct config_work;
83
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +010084 /*
85 * Tracks references from block_device_operations open/release and
86 * virtio_driver probe/remove so this object can be freed once no
87 * longer in use.
88 */
89 refcount_t refs;
90
Rusty Russell0864b792008-12-30 09:26:05 -060091 /* What host tells us, plus 2 for header & tailer. */
92 unsigned int sg_elems;
93
Michael S. Tsirkin5087a502011-10-30 21:29:59 +020094 /* Ida index - used to track minor number allocations. */
95 int index;
Ming Lei6a27b652014-06-26 17:41:48 +080096
97 /* num of vqs */
98 int num_vqs;
99 struct virtio_blk_vq *vqs;
Rusty Russelle467cde2007-10-22 11:03:38 +1000100};
101
Michael S. Tsirkinbb6ec572015-01-15 13:33:31 +0200102struct virtblk_req {
Christoph Hellwig97b50a62017-01-28 09:32:53 +0100103 struct virtio_blk_outhdr out_hdr;
104 u8 status;
Max Gurtovoy02746e22021-09-01 16:14:34 +0300105 struct sg_table sg_table;
Asias Hea98755c2012-08-08 16:07:04 +0800106 struct scatterlist sg[];
Rusty Russelle467cde2007-10-22 11:03:38 +1000107};
108
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200109static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
Asias Hea98755c2012-08-08 16:07:04 +0800110{
111 switch (vbr->status) {
112 case VIRTIO_BLK_S_OK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200113 return BLK_STS_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800114 case VIRTIO_BLK_S_UNSUPP:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200115 return BLK_STS_NOTSUPP;
Asias Hea98755c2012-08-08 16:07:04 +0800116 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200117 return BLK_STS_IOERR;
Asias Hea98755c2012-08-08 16:07:04 +0800118 }
119}
120
Christoph Hellwig97b50a62017-01-28 09:32:53 +0100121static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
122 struct scatterlist *data_sg, bool have_data)
123{
124 struct scatterlist hdr, status, *sgs[3];
125 unsigned int num_out = 0, num_in = 0;
126
127 sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
128 sgs[num_out++] = &hdr;
129
130 if (have_data) {
131 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
132 sgs[num_out++] = data_sg;
133 else
134 sgs[num_out + num_in++] = data_sg;
Paolo Bonzini8f39db92013-03-20 15:44:27 +1030135 }
136
137 sg_init_one(&status, &vbr->status, sizeof(vbr->status));
138 sgs[num_out + num_in++] = &status;
139
140 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
Paolo Bonzini5ee21a52013-03-20 15:44:27 +1030141}
Asias Hec85a1f92012-08-08 16:07:05 +0800142
Changpeng Liu1f238162018-11-01 15:40:35 -0700143static int virtblk_setup_discard_write_zeroes(struct request *req, bool unmap)
144{
145 unsigned short segments = blk_rq_nr_discard_segments(req);
146 unsigned short n = 0;
147 struct virtio_blk_discard_write_zeroes *range;
148 struct bio *bio;
149 u32 flags = 0;
150
151 if (unmap)
152 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
153
154 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
155 if (!range)
156 return -ENOMEM;
157
Ming Leiaf822aa2020-08-17 17:52:40 +0800158 /*
159 * Single max discard segment means multi-range discard isn't
160 * supported, and block layer only runs contiguity merge like
161 * normal RW request. So we can't reply on bio for retrieving
162 * each range info.
163 */
164 if (queue_max_discard_segments(req->q) == 1) {
165 range[0].flags = cpu_to_le32(flags);
166 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
167 range[0].sector = cpu_to_le64(blk_rq_pos(req));
168 n = 1;
169 } else {
170 __rq_for_each_bio(bio, req) {
171 u64 sector = bio->bi_iter.bi_sector;
172 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
Changpeng Liu1f238162018-11-01 15:40:35 -0700173
Ming Leiaf822aa2020-08-17 17:52:40 +0800174 range[n].flags = cpu_to_le32(flags);
175 range[n].num_sectors = cpu_to_le32(num_sectors);
176 range[n].sector = cpu_to_le64(sector);
177 n++;
178 }
Changpeng Liu1f238162018-11-01 15:40:35 -0700179 }
180
Ming Leiaf822aa2020-08-17 17:52:40 +0800181 WARN_ON_ONCE(n != segments);
182
Changpeng Liu1f238162018-11-01 15:40:35 -0700183 req->special_vec.bv_page = virt_to_page(range);
184 req->special_vec.bv_offset = offset_in_page(range);
185 req->special_vec.bv_len = sizeof(*range) * segments;
186 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
187
188 return 0;
189}
190
Max Gurtovoy02746e22021-09-01 16:14:34 +0300191static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
192{
193 if (blk_rq_nr_phys_segments(req))
194 sg_free_table_chained(&vbr->sg_table,
195 VIRTIO_BLK_INLINE_SG_CNT);
196}
197
198static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
199 struct virtblk_req *vbr)
200{
201 int err;
202
203 if (!blk_rq_nr_phys_segments(req))
204 return 0;
205
206 vbr->sg_table.sgl = vbr->sg;
207 err = sg_alloc_table_chained(&vbr->sg_table,
208 blk_rq_nr_phys_segments(req),
209 vbr->sg_table.sgl,
210 VIRTIO_BLK_INLINE_SG_CNT);
211 if (unlikely(err))
212 return -ENOMEM;
213
214 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
215}
216
217static void virtblk_cleanup_cmd(struct request *req)
218{
219 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
220 kfree(bvec_virt(&req->special_vec));
221}
222
223static int virtblk_setup_cmd(struct virtio_device *vdev, struct request *req,
224 struct virtblk_req *vbr)
225{
226 bool unmap = false;
227 u32 type;
228
229 vbr->out_hdr.sector = 0;
230
231 switch (req_op(req)) {
232 case REQ_OP_READ:
233 type = VIRTIO_BLK_T_IN;
234 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
235 blk_rq_pos(req));
236 break;
237 case REQ_OP_WRITE:
238 type = VIRTIO_BLK_T_OUT;
239 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
240 blk_rq_pos(req));
241 break;
242 case REQ_OP_FLUSH:
243 type = VIRTIO_BLK_T_FLUSH;
244 break;
245 case REQ_OP_DISCARD:
246 type = VIRTIO_BLK_T_DISCARD;
247 break;
248 case REQ_OP_WRITE_ZEROES:
249 type = VIRTIO_BLK_T_WRITE_ZEROES;
250 unmap = !(req->cmd_flags & REQ_NOUNMAP);
251 break;
252 case REQ_OP_DRV_IN:
253 type = VIRTIO_BLK_T_GET_ID;
254 break;
255 default:
256 WARN_ON_ONCE(1);
257 return BLK_STS_IOERR;
258 }
259
260 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
261 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
262
263 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
264 if (virtblk_setup_discard_write_zeroes(req, unmap))
265 return BLK_STS_RESOURCE;
266 }
267
268 return 0;
269}
270
Christoph Hellwig5124c282014-02-10 03:24:39 -0800271static inline void virtblk_request_done(struct request *req)
Asias Hec85a1f92012-08-08 16:07:05 +0800272{
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200273 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Asias Hea98755c2012-08-08 16:07:04 +0800274
Max Gurtovoy02746e22021-09-01 16:14:34 +0300275 virtblk_unmap_data(req, vbr);
276 virtblk_cleanup_cmd(req);
Christoph Hellwigd19633d2017-04-20 16:03:00 +0200277 blk_mq_end_request(req, virtblk_result(vbr));
Asias Hea98755c2012-08-08 16:07:04 +0800278}
279
280static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000281{
282 struct virtio_blk *vblk = vq->vdev->priv;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600283 bool req_done = false;
Ming Lei6a27b652014-06-26 17:41:48 +0800284 int qid = vq->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000285 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000286 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800287 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000288
Ming Lei6a27b652014-06-26 17:41:48 +0800289 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800290 do {
291 virtqueue_disable_cb(vq);
Ming Lei6a27b652014-06-26 17:41:48 +0800292 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
Christoph Hellwig85dada02017-01-28 09:32:52 +0100293 struct request *req = blk_mq_rq_from_pdu(vbr);
294
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200295 if (likely(!blk_should_fake_timeout(req->q)))
296 blk_mq_complete_request(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600297 req_done = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000298 }
Heinz Graalfs7f03b172013-10-29 09:40:30 +1030299 if (unlikely(virtqueue_is_broken(vq)))
300 break;
Asias Hebb811102012-09-25 10:36:17 +0800301 } while (!virtqueue_enable_cb(vq));
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600302
Rusty Russelle467cde2007-10-22 11:03:38 +1000303 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800304 if (req_done)
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200305 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
Ming Lei6a27b652014-06-26 17:41:48 +0800306 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800307}
308
Jens Axboe944e7c82018-11-26 11:00:12 -0700309static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
310{
311 struct virtio_blk *vblk = hctx->queue->queuedata;
312 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
313 bool kick;
314
315 spin_lock_irq(&vq->lock);
316 kick = virtqueue_kick_prepare(vq->vq);
317 spin_unlock_irq(&vq->lock);
318
319 if (kick)
320 virtqueue_notify(vq->vq);
321}
322
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200323static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -0600324 const struct blk_mq_queue_data *bd)
Rusty Russelle467cde2007-10-22 11:03:38 +1000325{
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600326 struct virtio_blk *vblk = hctx->queue->queuedata;
Jens Axboe74c45052014-10-29 11:14:52 -0600327 struct request *req = bd->rq;
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200328 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600329 unsigned long flags;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030330 unsigned int num;
Ming Lei6a27b652014-06-26 17:41:48 +0800331 int qid = hctx->queue_num;
Rusty Russell5261b852014-03-13 11:23:39 +1030332 int err;
Ming Leie8edca62014-05-30 10:49:29 +0800333 bool notify = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000334
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600335 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000336
Max Gurtovoy02746e22021-09-01 16:14:34 +0300337 err = virtblk_setup_cmd(vblk->vdev, req, vbr);
338 if (unlikely(err))
339 return err;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100340
Christoph Hellwige2490072014-09-13 16:40:09 -0700341 blk_mq_start_request(req);
342
Max Gurtovoy02746e22021-09-01 16:14:34 +0300343 num = virtblk_map_data(hctx, req, vbr);
344 if (unlikely(num < 0)) {
345 virtblk_cleanup_cmd(req);
346 return BLK_STS_RESOURCE;
Rusty Russelle467cde2007-10-22 11:03:38 +1000347 }
348
Ming Lei6a27b652014-06-26 17:41:48 +0800349 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300350 err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg_table.sgl, num);
Rusty Russell5261b852014-03-13 11:23:39 +1030351 if (err) {
Ming Lei6a27b652014-06-26 17:41:48 +0800352 virtqueue_kick(vblk->vqs[qid].vq);
Halil Pasicf5f6b952020-02-13 13:37:27 +0100353 /* Don't stop the queue if -ENOMEM: we may have failed to
354 * bounce the buffer due to global resource outage.
355 */
356 if (err == -ENOSPC)
357 blk_mq_stop_hw_queue(hctx);
Ming Lei6a27b652014-06-26 17:41:48 +0800358 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300359 virtblk_unmap_data(req, vbr);
360 virtblk_cleanup_cmd(req);
Halil Pasic3d973b22020-02-13 13:37:28 +0100361 switch (err) {
362 case -ENOSPC:
Ming Lei86ff7c22018-01-30 22:04:57 -0500363 return BLK_STS_DEV_RESOURCE;
Halil Pasic3d973b22020-02-13 13:37:28 +0100364 case -ENOMEM:
365 return BLK_STS_RESOURCE;
366 default:
367 return BLK_STS_IOERR;
368 }
Asias Hea98755c2012-08-08 16:07:04 +0800369 }
370
Jens Axboe74c45052014-10-29 11:14:52 -0600371 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
Ming Leie8edca62014-05-30 10:49:29 +0800372 notify = true;
Ming Lei6a27b652014-06-26 17:41:48 +0800373 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Ming Leie8edca62014-05-30 10:49:29 +0800374
375 if (notify)
Ming Lei6a27b652014-06-26 17:41:48 +0800376 virtqueue_notify(vblk->vqs[qid].vq);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200377 return BLK_STS_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800378}
379
john cooper4cb2ea22010-03-25 01:33:33 -0400380/* return id (s/n) string for *disk to *id_str
381 */
382static int virtblk_get_id(struct gendisk *disk, char *id_str)
383{
384 struct virtio_blk *vblk = disk->private_data;
Christoph Hellwigf9596692016-07-19 11:31:49 +0200385 struct request_queue *q = vblk->disk->queue;
john cooper4cb2ea22010-03-25 01:33:33 -0400386 struct request *req;
Mike Snitzere4c47762010-10-09 12:12:13 +1030387 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400388
Christoph Hellwigff005a02018-05-09 09:54:05 +0200389 req = blk_get_request(q, REQ_OP_DRV_IN, 0);
Christoph Hellwigf9596692016-07-19 11:31:49 +0200390 if (IS_ERR(req))
john cooper4cb2ea22010-03-25 01:33:33 -0400391 return PTR_ERR(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030392
Christoph Hellwigf9596692016-07-19 11:31:49 +0200393 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
394 if (err)
395 goto out;
396
Guoqing Jiang684da762021-01-25 05:49:58 +0100397 blk_execute_rq(vblk->disk, req, false);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200398 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
Christoph Hellwigf9596692016-07-19 11:31:49 +0200399out:
400 blk_put_request(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030401 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400402}
403
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100404static void virtblk_get(struct virtio_blk *vblk)
405{
406 refcount_inc(&vblk->refs);
407}
408
409static void virtblk_put(struct virtio_blk *vblk)
410{
411 if (refcount_dec_and_test(&vblk->refs)) {
412 ida_simple_remove(&vd_index_ida, vblk->index);
413 mutex_destroy(&vblk->vdev_mutex);
414 kfree(vblk);
415 }
416}
417
418static int virtblk_open(struct block_device *bd, fmode_t mode)
419{
420 struct virtio_blk *vblk = bd->bd_disk->private_data;
421 int ret = 0;
422
423 mutex_lock(&vblk->vdev_mutex);
424
425 if (vblk->vdev)
426 virtblk_get(vblk);
427 else
428 ret = -ENXIO;
429
430 mutex_unlock(&vblk->vdev_mutex);
431 return ret;
432}
433
434static void virtblk_release(struct gendisk *disk, fmode_t mode)
435{
436 struct virtio_blk *vblk = disk->private_data;
437
438 virtblk_put(vblk);
439}
440
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100441/* We provide getgeo only to please some old bootloader/partitioning tools */
442static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
443{
Ryan Harper48e40432008-04-16 13:56:37 -0500444 struct virtio_blk *vblk = bd->bd_disk->private_data;
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100445 int ret = 0;
446
447 mutex_lock(&vblk->vdev_mutex);
448
449 if (!vblk->vdev) {
450 ret = -ENXIO;
451 goto out;
452 }
Ryan Harper48e40432008-04-16 13:56:37 -0500453
454 /* see if the host passed in geometry config */
Rusty Russell855e0c52013-10-14 18:11:51 +1030455 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
456 virtio_cread(vblk->vdev, struct virtio_blk_config,
457 geometry.cylinders, &geo->cylinders);
458 virtio_cread(vblk->vdev, struct virtio_blk_config,
459 geometry.heads, &geo->heads);
460 virtio_cread(vblk->vdev, struct virtio_blk_config,
461 geometry.sectors, &geo->sectors);
Ryan Harper48e40432008-04-16 13:56:37 -0500462 } else {
463 /* some standard values, similar to sd */
464 geo->heads = 1 << 6;
465 geo->sectors = 1 << 5;
466 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
467 }
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100468out:
469 mutex_unlock(&vblk->vdev_mutex);
470 return ret;
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100471}
472
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700473static const struct block_device_operations virtblk_fops = {
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100474 .owner = THIS_MODULE,
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100475 .open = virtblk_open,
476 .release = virtblk_release,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100477 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000478};
479
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100480static int index_to_minor(int index)
481{
482 return index << PART_BITS;
483}
484
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200485static int minor_to_index(int minor)
486{
487 return minor >> PART_BITS;
488}
489
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200490static ssize_t serial_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500492{
493 struct gendisk *disk = dev_to_disk(dev);
494 int err;
495
496 /* sysfs gives us a PAGE_SIZE buffer */
497 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
498
499 buf[VIRTIO_BLK_ID_BYTES] = '\0';
500 err = virtblk_get_id(disk, buf);
501 if (!err)
502 return strlen(buf);
503
504 if (err == -EIO) /* Unsupported? Make it empty. */
505 return 0;
506
507 return err;
508}
Michael S. Tsirkin393c5252014-10-23 16:08:44 +0300509
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200510static DEVICE_ATTR_RO(serial);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500511
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000512/* The queue's logical block size must be set before calling this */
513static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100514{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100515 struct virtio_device *vdev = vblk->vdev;
516 struct request_queue *q = vblk->disk->queue;
517 char cap_str_2[10], cap_str_10[10];
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100518 unsigned long long nblocks;
James Bottomleyb9f28d82015-03-05 18:47:01 -0800519 u64 capacity;
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100520
521 /* Host must always specify the capacity. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030522 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100523
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100524 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
525
526 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800527 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100528 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800529 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100530
531 dev_notice(&vdev->dev,
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000532 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
533 vblk->disk->disk_name,
534 resize ? "new size: " : "",
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100535 nblocks,
536 queue_logical_block_size(q),
537 cap_str_10,
538 cap_str_2);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100539
Christoph Hellwig449f4ec2020-11-16 15:56:56 +0100540 set_capacity_and_notify(vblk->disk, capacity);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000541}
542
543static void virtblk_config_changed_work(struct work_struct *work)
544{
545 struct virtio_blk *vblk =
546 container_of(work, struct virtio_blk, config_work);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000547
548 virtblk_update_capacity(vblk, true);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100549}
550
551static void virtblk_config_changed(struct virtio_device *vdev)
552{
553 struct virtio_blk *vblk = vdev->priv;
554
555 queue_work(virtblk_wq, &vblk->config_work);
556}
557
Amit Shah6abd6e52011-12-22 16:58:29 +0530558static int init_vq(struct virtio_blk *vblk)
559{
Markus Elfring2ff98442016-09-13 13:43:50 +0200560 int err;
Ming Lei6a27b652014-06-26 17:41:48 +0800561 int i;
562 vq_callback_t **callbacks;
563 const char **names;
564 struct virtqueue **vqs;
565 unsigned short num_vqs;
566 struct virtio_device *vdev = vblk->vdev;
Christoph Hellwigad714732017-02-05 18:15:25 +0100567 struct irq_affinity desc = { 0, };
Amit Shah6abd6e52011-12-22 16:58:29 +0530568
Ming Lei6a27b652014-06-26 17:41:48 +0800569 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
570 struct virtio_blk_config, num_queues,
571 &num_vqs);
572 if (err)
573 num_vqs = 1;
Jason Wang6ae6ff62021-10-19 15:01:43 +0800574 if (!err && !num_vqs) {
575 dev_err(&vdev->dev, "MQ advertisted but zero queues reported\n");
576 return -EINVAL;
577 }
Amit Shah6abd6e52011-12-22 16:58:29 +0530578
Max Gurtovoy0989c412021-09-02 23:46:22 +0300579 num_vqs = min_t(unsigned int,
580 min_not_zero(num_request_queues, nr_cpu_ids),
581 num_vqs);
Dongli Zhangbf348f92019-03-27 18:36:34 +0800582
Markus Elfring668866b2016-09-13 11:32:22 +0200583 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800584 if (!vblk->vqs)
585 return -ENOMEM;
586
Markus Elfring668866b2016-09-13 11:32:22 +0200587 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
588 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
589 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800590 if (!names || !callbacks || !vqs) {
Ming Lei6a27b652014-06-26 17:41:48 +0800591 err = -ENOMEM;
592 goto out;
593 }
594
Ming Lei6a27b652014-06-26 17:41:48 +0800595 for (i = 0; i < num_vqs; i++) {
596 callbacks[i] = virtblk_done;
597 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
598 names[i] = vblk->vqs[i].name;
599 }
600
601 /* Discover virtqueues and write information to configuration. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200602 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
Ming Lei6a27b652014-06-26 17:41:48 +0800603 if (err)
Minfei Huang347a5292016-08-09 16:39:20 +0800604 goto out;
Ming Lei6a27b652014-06-26 17:41:48 +0800605
606 for (i = 0; i < num_vqs; i++) {
607 spin_lock_init(&vblk->vqs[i].lock);
608 vblk->vqs[i].vq = vqs[i];
609 }
610 vblk->num_vqs = num_vqs;
611
Minfei Huang347a5292016-08-09 16:39:20 +0800612out:
Ming Lei6a27b652014-06-26 17:41:48 +0800613 kfree(vqs);
Ming Lei6a27b652014-06-26 17:41:48 +0800614 kfree(callbacks);
Ming Lei6a27b652014-06-26 17:41:48 +0800615 kfree(names);
Ming Lei6a27b652014-06-26 17:41:48 +0800616 if (err)
617 kfree(vblk->vqs);
Amit Shah6abd6e52011-12-22 16:58:29 +0530618 return err;
619}
620
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800621/*
622 * Legacy naming scheme used for virtio devices. We are stuck with it for
623 * virtio blk but don't ever use it for any new driver.
624 */
625static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
626{
627 const int base = 'z' - 'a' + 1;
628 char *begin = buf + strlen(prefix);
629 char *end = buf + buflen;
630 char *p;
631 int unit;
632
633 p = end - 1;
634 *p = '\0';
635 unit = base;
636 do {
637 if (p == begin)
638 return -EINVAL;
639 *--p = 'a' + (index % unit);
640 index = (index / unit) - 1;
641 } while (index >= 0);
642
643 memmove(begin, p, end - p);
644 memcpy(buf, prefix, strlen(prefix));
645
646 return 0;
647}
648
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200649static int virtblk_get_cache_mode(struct virtio_device *vdev)
650{
651 u8 writeback;
652 int err;
653
Rusty Russell855e0c52013-10-14 18:11:51 +1030654 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
655 struct virtio_blk_config, wce,
656 &writeback);
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200657
658 /*
659 * If WCE is not configurable and flush is not available,
660 * assume no writeback cache is in use.
661 */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200662 if (err)
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200663 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200664
665 return writeback;
666}
667
668static void virtblk_update_cache_mode(struct virtio_device *vdev)
669{
670 u8 writeback = virtblk_get_cache_mode(vdev);
671 struct virtio_blk *vblk = vdev->priv;
672
Jens Axboead9126a2016-03-30 10:12:58 -0600673 blk_queue_write_cache(vblk->disk->queue, writeback, false);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200674}
675
676static const char *const virtblk_cache_types[] = {
677 "write through", "write back"
678};
679
680static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200681cache_type_store(struct device *dev, struct device_attribute *attr,
682 const char *buf, size_t count)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200683{
684 struct gendisk *disk = dev_to_disk(dev);
685 struct virtio_blk *vblk = disk->private_data;
686 struct virtio_device *vdev = vblk->vdev;
687 int i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200688
689 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300690 i = sysfs_match_string(virtblk_cache_types, buf);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200691 if (i < 0)
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300692 return i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200693
Rusty Russell855e0c52013-10-14 18:11:51 +1030694 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200695 virtblk_update_cache_mode(vdev);
696 return count;
697}
698
699static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200700cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200701{
702 struct gendisk *disk = dev_to_disk(dev);
703 struct virtio_blk *vblk = disk->private_data;
704 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
705
706 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
707 return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
708}
709
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200710static DEVICE_ATTR_RW(cache_type);
711
712static struct attribute *virtblk_attrs[] = {
713 &dev_attr_serial.attr,
714 &dev_attr_cache_type.attr,
715 NULL,
716};
717
718static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
719 struct attribute *a, int n)
720{
Tian Tao4ce79062020-08-21 09:19:15 +0800721 struct device *dev = kobj_to_dev(kobj);
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200722 struct gendisk *disk = dev_to_disk(dev);
723 struct virtio_blk *vblk = disk->private_data;
724 struct virtio_device *vdev = vblk->vdev;
725
726 if (a == &dev_attr_cache_type.attr &&
727 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
728 return S_IRUGO;
729
730 return a->mode;
731}
732
733static const struct attribute_group virtblk_attr_group = {
734 .attrs = virtblk_attrs,
735 .is_visible = virtblk_attrs_are_visible,
736};
737
738static const struct attribute_group *virtblk_attr_groups[] = {
739 &virtblk_attr_group,
740 NULL,
741};
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200742
Christoph Hellwigad714732017-02-05 18:15:25 +0100743static int virtblk_map_queues(struct blk_mq_tag_set *set)
744{
745 struct virtio_blk *vblk = set->driver_data;
746
Dongli Zhang9bc00752019-03-12 09:31:56 +0800747 return blk_mq_virtio_map_queues(&set->map[HCTX_TYPE_DEFAULT],
748 vblk->vdev, 0);
Christoph Hellwigad714732017-02-05 18:15:25 +0100749}
750
Eric Biggersf363b082017-03-30 13:39:16 -0700751static const struct blk_mq_ops virtio_mq_ops = {
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600752 .queue_rq = virtio_queue_rq,
Jens Axboe944e7c82018-11-26 11:00:12 -0700753 .commit_rqs = virtio_commit_rqs,
Christoph Hellwig5124c282014-02-10 03:24:39 -0800754 .complete = virtblk_request_done,
Christoph Hellwigad714732017-02-05 18:15:25 +0100755 .map_queues = virtblk_map_queues,
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600756};
757
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600758static unsigned int virtblk_queue_depth;
759module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600760
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800761static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000762{
763 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600764 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200765 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800766
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100767 u32 v, blk_size, max_size, sg_elems, opt_io_size;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600768 u16 min_io_size;
769 u8 physical_block_exp, alignment_offset;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800770 unsigned int queue_depth;
Rusty Russelle467cde2007-10-22 11:03:38 +1000771
Michael S. Tsirkinff631982021-10-04 11:31:00 -0400772 if (!vdev->config->get) {
773 dev_err(&vdev->dev, "%s failure: config access disabled\n",
774 __func__);
775 return -EINVAL;
776 }
777
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200778 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
779 GFP_KERNEL);
780 if (err < 0)
781 goto out;
782 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100783
Rusty Russell0864b792008-12-30 09:26:05 -0600784 /* We need to know how many segments before we allocate. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030785 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
786 struct virtio_blk_config, seg_max,
787 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200788
789 /* We need at least one SG element, whatever they say. */
790 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600791 sg_elems = 1;
792
Stefan Hajnoczi63947b32021-05-24 16:40:20 +0100793 /* Prevent integer overflows and honor max vq size */
794 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
795
796 /* We need extra sg elements at head and tail. */
Rusty Russell0864b792008-12-30 09:26:05 -0600797 sg_elems += 2;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600798 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000799 if (!vblk) {
800 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200801 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000802 }
803
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100804 /* This reference is dropped in virtblk_remove(). */
805 refcount_set(&vblk->refs, 1);
806 mutex_init(&vblk->vdev_mutex);
807
Rusty Russelle467cde2007-10-22 11:03:38 +1000808 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600809 vblk->sg_elems = sg_elems;
Asias Hea98755c2012-08-08 16:07:04 +0800810
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100811 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000812
Amit Shah6abd6e52011-12-22 16:58:29 +0530813 err = init_vq(vblk);
814 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000815 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000816
Rusty Russellfc4324b2014-03-19 17:08:24 +1030817 /* Default queue sizing is to fill the ring. */
Max Gurtovoy6105d1f2021-09-05 11:57:17 +0300818 if (!virtblk_queue_depth) {
Joseph Qid1e9aa92021-01-22 17:21:46 +0800819 queue_depth = vblk->vqs[0].vq->num_free;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030820 /* ... but without indirect descs, we use 2 descs per req */
821 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
Joseph Qid1e9aa92021-01-22 17:21:46 +0800822 queue_depth /= 2;
823 } else {
824 queue_depth = virtblk_queue_depth;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030825 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600826
827 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
828 vblk->tag_set.ops = &virtio_mq_ops;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800829 vblk->tag_set.queue_depth = queue_depth;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600830 vblk->tag_set.numa_node = NUMA_NO_NODE;
831 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
832 vblk->tag_set.cmd_size =
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600833 sizeof(struct virtblk_req) +
Max Gurtovoy02746e22021-09-01 16:14:34 +0300834 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600835 vblk->tag_set.driver_data = vblk;
Ming Lei6a27b652014-06-26 17:41:48 +0800836 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600837
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600838 err = blk_mq_alloc_tag_set(&vblk->tag_set);
839 if (err)
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300840 goto out_free_vq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600841
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300842 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
843 if (IS_ERR(vblk->disk)) {
844 err = PTR_ERR(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600845 goto out_free_tags;
Rusty Russelle467cde2007-10-22 11:03:38 +1000846 }
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300847 q = vblk->disk->queue;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900848
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800849 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100850
Rusty Russelle467cde2007-10-22 11:03:38 +1000851 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100852 vblk->disk->first_minor = index_to_minor(index);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300853 vblk->disk->minors = 1 << PART_BITS;
Rusty Russelle467cde2007-10-22 11:03:38 +1000854 vblk->disk->private_data = vblk;
855 vblk->disk->fops = &virtblk_fops;
Fam Zheng5fa31422015-09-06 17:05:42 +0800856 vblk->disk->flags |= GENHD_FL_EXT_DEVT;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200857 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100858
Tejun Heo02c42b72010-09-03 11:56:18 +0200859 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200860 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000861
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200862 /* If disk is read-only in the host, the guest should obey */
863 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
864 set_disk_ro(vblk->disk, 1);
865
Rusty Russell0864b792008-12-30 09:26:05 -0600866 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500867 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600868
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600869 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500870 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600871
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100872 max_size = virtio_max_dma_size(vdev);
873
Rusty Russella586d4f2008-02-04 23:49:56 -0500874 /* Host can optionally specify maximum segment size and number of
875 * segments. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030876 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
877 struct virtio_blk_config, size_max, &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000878 if (!err)
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100879 max_size = min(max_size, v);
880
881 blk_queue_max_segment_size(q, max_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000882
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200883 /* Host can optionally specify the block size of the device */
Rusty Russell855e0c52013-10-14 18:11:51 +1030884 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
885 struct virtio_blk_config, blk_size,
886 &blk_size);
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200887 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600888 blk_queue_logical_block_size(q, blk_size);
889 else
890 blk_size = queue_logical_block_size(q);
891
892 /* Use topology information if available */
Rusty Russell855e0c52013-10-14 18:11:51 +1030893 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
894 struct virtio_blk_config, physical_block_exp,
895 &physical_block_exp);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600896 if (!err && physical_block_exp)
897 blk_queue_physical_block_size(q,
898 blk_size * (1 << physical_block_exp));
899
Rusty Russell855e0c52013-10-14 18:11:51 +1030900 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
901 struct virtio_blk_config, alignment_offset,
902 &alignment_offset);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600903 if (!err && alignment_offset)
904 blk_queue_alignment_offset(q, blk_size * alignment_offset);
905
Rusty Russell855e0c52013-10-14 18:11:51 +1030906 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
907 struct virtio_blk_config, min_io_size,
908 &min_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600909 if (!err && min_io_size)
910 blk_queue_io_min(q, blk_size * min_io_size);
911
Rusty Russell855e0c52013-10-14 18:11:51 +1030912 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
913 struct virtio_blk_config, opt_io_size,
914 &opt_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600915 if (!err && opt_io_size)
916 blk_queue_io_opt(q, blk_size * opt_io_size);
917
Changpeng Liu1f238162018-11-01 15:40:35 -0700918 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
919 q->limits.discard_granularity = blk_size;
920
921 virtio_cread(vdev, struct virtio_blk_config,
922 discard_sector_alignment, &v);
923 q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;
924
925 virtio_cread(vdev, struct virtio_blk_config,
926 max_discard_sectors, &v);
927 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
928
929 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
930 &v);
931 blk_queue_max_discard_segments(q,
932 min_not_zero(v,
933 MAX_DISCARD_SEGMENTS));
934
935 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
936 }
937
938 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
939 virtio_cread(vdev, struct virtio_blk_config,
940 max_write_zeroes_sectors, &v);
941 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
942 }
943
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000944 virtblk_update_capacity(vblk, false);
Michael S. Tsirkin7a113702014-10-15 10:22:30 +1030945 virtio_device_ready(vdev);
946
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200947 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
948 if (err)
949 goto out_cleanup_disk;
950
Rusty Russelle467cde2007-10-22 11:03:38 +1000951 return 0;
952
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200953out_cleanup_disk:
Xie Yongji82e89ea2021-08-09 18:16:09 +0800954 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600955out_free_tags:
956 blk_mq_free_tag_set(&vblk->tag_set);
Rusty Russelle467cde2007-10-22 11:03:38 +1000957out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600958 vdev->config->del_vqs(vdev);
Hou Taoe7eea442020-06-15 12:14:59 +0800959 kfree(vblk->vqs);
Rusty Russelle467cde2007-10-22 11:03:38 +1000960out_free_vblk:
961 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200962out_free_index:
963 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000964out:
965 return err;
966}
967
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800968static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000969{
970 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000971
Michael S. Tsirkincc74f712014-10-15 10:22:26 +1030972 /* Make sure no work handler is accessing the device. */
973 flush_work(&vblk->config_work);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100974
Asias He02e2b122012-05-25 10:34:47 +0800975 del_gendisk(vblk->disk);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300976 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600977 blk_mq_free_tag_set(&vblk->tag_set);
978
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100979 mutex_lock(&vblk->vdev_mutex);
980
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500981 /* Stop all the virtqueues. */
982 vdev->config->reset(vdev);
983
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100984 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
985 vblk->vdev = NULL;
986
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600987 vdev->config->del_vqs(vdev);
Ming Lei6a27b652014-06-26 17:41:48 +0800988 kfree(vblk->vqs);
Alexander Graff4953fe2013-01-02 15:37:17 +1030989
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100990 mutex_unlock(&vblk->vdev_mutex);
991
992 virtblk_put(vblk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000993}
994
Aaron Lu89107002013-09-17 09:25:23 +0930995#ifdef CONFIG_PM_SLEEP
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530996static int virtblk_freeze(struct virtio_device *vdev)
997{
998 struct virtio_blk *vblk = vdev->priv;
999
1000 /* Ensure we don't receive any more interrupts */
1001 vdev->config->reset(vdev);
1002
Michael S. Tsirkincc74f712014-10-15 10:22:26 +10301003 /* Make sure no work handler is accessing the device. */
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301004 flush_work(&vblk->config_work);
1005
Sagi Grimberg9b3e9902017-07-04 10:03:03 +03001006 blk_mq_quiesce_queue(vblk->disk->queue);
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301007
1008 vdev->config->del_vqs(vdev);
Xie Yongjib71ba222021-05-17 16:43:32 +08001009 kfree(vblk->vqs);
1010
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301011 return 0;
1012}
1013
1014static int virtblk_restore(struct virtio_device *vdev)
1015{
1016 struct virtio_blk *vblk = vdev->priv;
1017 int ret;
1018
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301019 ret = init_vq(vdev->priv);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301020 if (ret)
1021 return ret;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -06001022
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301023 virtio_device_ready(vdev);
1024
Sagi Grimberg9b3e9902017-07-04 10:03:03 +03001025 blk_mq_unquiesce_queue(vblk->disk->queue);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301026 return 0;
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301027}
1028#endif
1029
Márton Németh47483e22010-01-10 13:40:02 +01001030static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +10001031 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1032 { 0 },
1033};
1034
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001035static unsigned int features_legacy[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +02001036 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
Christoph Hellwig97b50a62017-01-28 09:32:53 +01001037 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001038 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001039 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001040}
1041;
1042static unsigned int features[] = {
1043 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1044 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001045 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001046 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Rusty Russellc45a6812008-05-02 21:50:50 -05001047};
1048
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001049static struct virtio_driver virtio_blk = {
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001050 .feature_table = features,
1051 .feature_table_size = ARRAY_SIZE(features),
1052 .feature_table_legacy = features_legacy,
1053 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1054 .driver.name = KBUILD_MODNAME,
1055 .driver.owner = THIS_MODULE,
1056 .id_table = id_table,
1057 .probe = virtblk_probe,
1058 .remove = virtblk_remove,
1059 .config_changed = virtblk_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301060#ifdef CONFIG_PM_SLEEP
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001061 .freeze = virtblk_freeze,
1062 .restore = virtblk_restore,
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301063#endif
Rusty Russelle467cde2007-10-22 11:03:38 +10001064};
1065
1066static int __init init(void)
1067{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001068 int error;
1069
1070 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1071 if (!virtblk_wq)
1072 return -ENOMEM;
1073
Christian Borntraeger4f3bf192008-01-31 15:53:53 +01001074 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001075 if (major < 0) {
1076 error = major;
1077 goto out_destroy_workqueue;
1078 }
1079
1080 error = register_virtio_driver(&virtio_blk);
1081 if (error)
1082 goto out_unregister_blkdev;
1083 return 0;
1084
1085out_unregister_blkdev:
1086 unregister_blkdev(major, "virtblk");
1087out_destroy_workqueue:
1088 destroy_workqueue(virtblk_wq);
1089 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +10001090}
1091
1092static void __exit fini(void)
1093{
Rusty Russelle467cde2007-10-22 11:03:38 +10001094 unregister_virtio_driver(&virtio_blk);
Michael S. Tsirkin38f37b52014-10-23 18:57:19 +03001095 unregister_blkdev(major, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001096 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +10001097}
1098module_init(init);
1099module_exit(fini);
1100
1101MODULE_DEVICE_TABLE(virtio, id_table);
1102MODULE_DESCRIPTION("Virtio block driver");
1103MODULE_LICENSE("GPL");