blob: 9dd0099d2bd22c6e2df13762da477be1af5a5d3d [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
211static int virtblk_setup_cmd(struct virtio_device *vdev, struct request *req,
212 struct virtblk_req *vbr)
213{
214 bool unmap = false;
215 u32 type;
216
217 vbr->out_hdr.sector = 0;
218
219 switch (req_op(req)) {
220 case REQ_OP_READ:
221 type = VIRTIO_BLK_T_IN;
222 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
223 blk_rq_pos(req));
224 break;
225 case REQ_OP_WRITE:
226 type = VIRTIO_BLK_T_OUT;
227 vbr->out_hdr.sector = cpu_to_virtio64(vdev,
228 blk_rq_pos(req));
229 break;
230 case REQ_OP_FLUSH:
231 type = VIRTIO_BLK_T_FLUSH;
232 break;
233 case REQ_OP_DISCARD:
234 type = VIRTIO_BLK_T_DISCARD;
235 break;
236 case REQ_OP_WRITE_ZEROES:
237 type = VIRTIO_BLK_T_WRITE_ZEROES;
238 unmap = !(req->cmd_flags & REQ_NOUNMAP);
239 break;
240 case REQ_OP_DRV_IN:
241 type = VIRTIO_BLK_T_GET_ID;
242 break;
243 default:
244 WARN_ON_ONCE(1);
245 return BLK_STS_IOERR;
246 }
247
248 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
249 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
250
251 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES) {
252 if (virtblk_setup_discard_write_zeroes(req, unmap))
253 return BLK_STS_RESOURCE;
254 }
255
256 return 0;
257}
258
Christoph Hellwig5124c282014-02-10 03:24:39 -0800259static inline void virtblk_request_done(struct request *req)
Asias Hec85a1f92012-08-08 16:07:05 +0800260{
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200261 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Asias Hea98755c2012-08-08 16:07:04 +0800262
Max Gurtovoy02746e22021-09-01 16:14:34 +0300263 virtblk_unmap_data(req, vbr);
264 virtblk_cleanup_cmd(req);
Christoph Hellwigd19633d2017-04-20 16:03:00 +0200265 blk_mq_end_request(req, virtblk_result(vbr));
Asias Hea98755c2012-08-08 16:07:04 +0800266}
267
268static void virtblk_done(struct virtqueue *vq)
Rusty Russelle467cde2007-10-22 11:03:38 +1000269{
270 struct virtio_blk *vblk = vq->vdev->priv;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600271 bool req_done = false;
Ming Lei6a27b652014-06-26 17:41:48 +0800272 int qid = vq->index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000273 struct virtblk_req *vbr;
Rusty Russelle467cde2007-10-22 11:03:38 +1000274 unsigned long flags;
Asias Hea98755c2012-08-08 16:07:04 +0800275 unsigned int len;
Rusty Russelle467cde2007-10-22 11:03:38 +1000276
Ming Lei6a27b652014-06-26 17:41:48 +0800277 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Asias Hebb811102012-09-25 10:36:17 +0800278 do {
279 virtqueue_disable_cb(vq);
Ming Lei6a27b652014-06-26 17:41:48 +0800280 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
Christoph Hellwig85dada02017-01-28 09:32:52 +0100281 struct request *req = blk_mq_rq_from_pdu(vbr);
282
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200283 if (likely(!blk_should_fake_timeout(req->q)))
284 blk_mq_complete_request(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600285 req_done = true;
Rusty Russelle467cde2007-10-22 11:03:38 +1000286 }
Heinz Graalfs7f03b172013-10-29 09:40:30 +1030287 if (unlikely(virtqueue_is_broken(vq)))
288 break;
Asias Hebb811102012-09-25 10:36:17 +0800289 } while (!virtqueue_enable_cb(vq));
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600290
Rusty Russelle467cde2007-10-22 11:03:38 +1000291 /* In case queue is stopped waiting for more buffers. */
Asias Hea98755c2012-08-08 16:07:04 +0800292 if (req_done)
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200293 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
Ming Lei6a27b652014-06-26 17:41:48 +0800294 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Asias Hea98755c2012-08-08 16:07:04 +0800295}
296
Jens Axboe944e7c82018-11-26 11:00:12 -0700297static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
298{
299 struct virtio_blk *vblk = hctx->queue->queuedata;
300 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
301 bool kick;
302
303 spin_lock_irq(&vq->lock);
304 kick = virtqueue_kick_prepare(vq->vq);
305 spin_unlock_irq(&vq->lock);
306
307 if (kick)
308 virtqueue_notify(vq->vq);
309}
310
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200311static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -0600312 const struct blk_mq_queue_data *bd)
Rusty Russelle467cde2007-10-22 11:03:38 +1000313{
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600314 struct virtio_blk *vblk = hctx->queue->queuedata;
Jens Axboe74c45052014-10-29 11:14:52 -0600315 struct request *req = bd->rq;
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200316 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600317 unsigned long flags;
Paolo Bonzini20af3cf2013-03-20 15:44:27 +1030318 unsigned int num;
Ming Lei6a27b652014-06-26 17:41:48 +0800319 int qid = hctx->queue_num;
Rusty Russell5261b852014-03-13 11:23:39 +1030320 int err;
Ming Leie8edca62014-05-30 10:49:29 +0800321 bool notify = false;
Rusty Russelle467cde2007-10-22 11:03:38 +1000322
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600323 BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
Rusty Russelle467cde2007-10-22 11:03:38 +1000324
Max Gurtovoy02746e22021-09-01 16:14:34 +0300325 err = virtblk_setup_cmd(vblk->vdev, req, vbr);
326 if (unlikely(err))
327 return err;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100328
Christoph Hellwige2490072014-09-13 16:40:09 -0700329 blk_mq_start_request(req);
330
Max Gurtovoy02746e22021-09-01 16:14:34 +0300331 num = virtblk_map_data(hctx, req, vbr);
332 if (unlikely(num < 0)) {
333 virtblk_cleanup_cmd(req);
334 return BLK_STS_RESOURCE;
Rusty Russelle467cde2007-10-22 11:03:38 +1000335 }
336
Ming Lei6a27b652014-06-26 17:41:48 +0800337 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300338 err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg_table.sgl, num);
Rusty Russell5261b852014-03-13 11:23:39 +1030339 if (err) {
Ming Lei6a27b652014-06-26 17:41:48 +0800340 virtqueue_kick(vblk->vqs[qid].vq);
Halil Pasicf5f6b952020-02-13 13:37:27 +0100341 /* Don't stop the queue if -ENOMEM: we may have failed to
342 * bounce the buffer due to global resource outage.
343 */
344 if (err == -ENOSPC)
345 blk_mq_stop_hw_queue(hctx);
Ming Lei6a27b652014-06-26 17:41:48 +0800346 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Max Gurtovoy02746e22021-09-01 16:14:34 +0300347 virtblk_unmap_data(req, vbr);
348 virtblk_cleanup_cmd(req);
Halil Pasic3d973b22020-02-13 13:37:28 +0100349 switch (err) {
350 case -ENOSPC:
Ming Lei86ff7c22018-01-30 22:04:57 -0500351 return BLK_STS_DEV_RESOURCE;
Halil Pasic3d973b22020-02-13 13:37:28 +0100352 case -ENOMEM:
353 return BLK_STS_RESOURCE;
354 default:
355 return BLK_STS_IOERR;
356 }
Asias Hea98755c2012-08-08 16:07:04 +0800357 }
358
Jens Axboe74c45052014-10-29 11:14:52 -0600359 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
Ming Leie8edca62014-05-30 10:49:29 +0800360 notify = true;
Ming Lei6a27b652014-06-26 17:41:48 +0800361 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
Ming Leie8edca62014-05-30 10:49:29 +0800362
363 if (notify)
Ming Lei6a27b652014-06-26 17:41:48 +0800364 virtqueue_notify(vblk->vqs[qid].vq);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200365 return BLK_STS_OK;
Asias Hea98755c2012-08-08 16:07:04 +0800366}
367
john cooper4cb2ea22010-03-25 01:33:33 -0400368/* return id (s/n) string for *disk to *id_str
369 */
370static int virtblk_get_id(struct gendisk *disk, char *id_str)
371{
372 struct virtio_blk *vblk = disk->private_data;
Christoph Hellwigf9596692016-07-19 11:31:49 +0200373 struct request_queue *q = vblk->disk->queue;
john cooper4cb2ea22010-03-25 01:33:33 -0400374 struct request *req;
Mike Snitzere4c47762010-10-09 12:12:13 +1030375 int err;
john cooper4cb2ea22010-03-25 01:33:33 -0400376
Christoph Hellwigff005a02018-05-09 09:54:05 +0200377 req = blk_get_request(q, REQ_OP_DRV_IN, 0);
Christoph Hellwigf9596692016-07-19 11:31:49 +0200378 if (IS_ERR(req))
john cooper4cb2ea22010-03-25 01:33:33 -0400379 return PTR_ERR(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030380
Christoph Hellwigf9596692016-07-19 11:31:49 +0200381 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
382 if (err)
383 goto out;
384
Guoqing Jiang684da762021-01-25 05:49:58 +0100385 blk_execute_rq(vblk->disk, req, false);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200386 err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
Christoph Hellwigf9596692016-07-19 11:31:49 +0200387out:
388 blk_put_request(req);
Mike Snitzere4c47762010-10-09 12:12:13 +1030389 return err;
john cooper4cb2ea22010-03-25 01:33:33 -0400390}
391
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100392static void virtblk_get(struct virtio_blk *vblk)
393{
394 refcount_inc(&vblk->refs);
395}
396
397static void virtblk_put(struct virtio_blk *vblk)
398{
399 if (refcount_dec_and_test(&vblk->refs)) {
400 ida_simple_remove(&vd_index_ida, vblk->index);
401 mutex_destroy(&vblk->vdev_mutex);
402 kfree(vblk);
403 }
404}
405
406static int virtblk_open(struct block_device *bd, fmode_t mode)
407{
408 struct virtio_blk *vblk = bd->bd_disk->private_data;
409 int ret = 0;
410
411 mutex_lock(&vblk->vdev_mutex);
412
413 if (vblk->vdev)
414 virtblk_get(vblk);
415 else
416 ret = -ENXIO;
417
418 mutex_unlock(&vblk->vdev_mutex);
419 return ret;
420}
421
422static void virtblk_release(struct gendisk *disk, fmode_t mode)
423{
424 struct virtio_blk *vblk = disk->private_data;
425
426 virtblk_put(vblk);
427}
428
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100429/* We provide getgeo only to please some old bootloader/partitioning tools */
430static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
431{
Ryan Harper48e40432008-04-16 13:56:37 -0500432 struct virtio_blk *vblk = bd->bd_disk->private_data;
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100433 int ret = 0;
434
435 mutex_lock(&vblk->vdev_mutex);
436
437 if (!vblk->vdev) {
438 ret = -ENXIO;
439 goto out;
440 }
Ryan Harper48e40432008-04-16 13:56:37 -0500441
442 /* see if the host passed in geometry config */
Rusty Russell855e0c52013-10-14 18:11:51 +1030443 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
444 virtio_cread(vblk->vdev, struct virtio_blk_config,
445 geometry.cylinders, &geo->cylinders);
446 virtio_cread(vblk->vdev, struct virtio_blk_config,
447 geometry.heads, &geo->heads);
448 virtio_cread(vblk->vdev, struct virtio_blk_config,
449 geometry.sectors, &geo->sectors);
Ryan Harper48e40432008-04-16 13:56:37 -0500450 } else {
451 /* some standard values, similar to sd */
452 geo->heads = 1 << 6;
453 geo->sectors = 1 << 5;
454 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
455 }
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100456out:
457 mutex_unlock(&vblk->vdev_mutex);
458 return ret;
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100459}
460
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700461static const struct block_device_operations virtblk_fops = {
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100462 .owner = THIS_MODULE,
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100463 .open = virtblk_open,
464 .release = virtblk_release,
Christian Borntraeger135da0b2008-01-23 17:56:50 +0100465 .getgeo = virtblk_getgeo,
Rusty Russelle467cde2007-10-22 11:03:38 +1000466};
467
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100468static int index_to_minor(int index)
469{
470 return index << PART_BITS;
471}
472
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200473static int minor_to_index(int minor)
474{
475 return minor >> PART_BITS;
476}
477
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200478static ssize_t serial_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500480{
481 struct gendisk *disk = dev_to_disk(dev);
482 int err;
483
484 /* sysfs gives us a PAGE_SIZE buffer */
485 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
486
487 buf[VIRTIO_BLK_ID_BYTES] = '\0';
488 err = virtblk_get_id(disk, buf);
489 if (!err)
490 return strlen(buf);
491
492 if (err == -EIO) /* Unsupported? Make it empty. */
493 return 0;
494
495 return err;
496}
Michael S. Tsirkin393c5252014-10-23 16:08:44 +0300497
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200498static DEVICE_ATTR_RO(serial);
Ryan Harpera5eb9e42010-06-23 22:19:57 -0500499
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000500/* The queue's logical block size must be set before calling this */
501static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100502{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100503 struct virtio_device *vdev = vblk->vdev;
504 struct request_queue *q = vblk->disk->queue;
505 char cap_str_2[10], cap_str_10[10];
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100506 unsigned long long nblocks;
James Bottomleyb9f28d82015-03-05 18:47:01 -0800507 u64 capacity;
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100508
509 /* Host must always specify the capacity. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030510 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100511
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100512 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
513
514 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800515 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100516 string_get_size(nblocks, queue_logical_block_size(q),
James Bottomleyb9f28d82015-03-05 18:47:01 -0800517 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100518
519 dev_notice(&vdev->dev,
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000520 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
521 vblk->disk->disk_name,
522 resize ? "new size: " : "",
Stefan Hajnoczi1046d302017-07-26 15:32:23 +0100523 nblocks,
524 queue_logical_block_size(q),
525 cap_str_10,
526 cap_str_2);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100527
Christoph Hellwig449f4ec2020-11-16 15:56:56 +0100528 set_capacity_and_notify(vblk->disk, capacity);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000529}
530
531static void virtblk_config_changed_work(struct work_struct *work)
532{
533 struct virtio_blk *vblk =
534 container_of(work, struct virtio_blk, config_work);
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000535
536 virtblk_update_capacity(vblk, true);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100537}
538
539static void virtblk_config_changed(struct virtio_device *vdev)
540{
541 struct virtio_blk *vblk = vdev->priv;
542
543 queue_work(virtblk_wq, &vblk->config_work);
544}
545
Amit Shah6abd6e52011-12-22 16:58:29 +0530546static int init_vq(struct virtio_blk *vblk)
547{
Markus Elfring2ff98442016-09-13 13:43:50 +0200548 int err;
Ming Lei6a27b652014-06-26 17:41:48 +0800549 int i;
550 vq_callback_t **callbacks;
551 const char **names;
552 struct virtqueue **vqs;
553 unsigned short num_vqs;
554 struct virtio_device *vdev = vblk->vdev;
Christoph Hellwigad714732017-02-05 18:15:25 +0100555 struct irq_affinity desc = { 0, };
Amit Shah6abd6e52011-12-22 16:58:29 +0530556
Ming Lei6a27b652014-06-26 17:41:48 +0800557 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
558 struct virtio_blk_config, num_queues,
559 &num_vqs);
560 if (err)
561 num_vqs = 1;
Jason Wang6ae6ff62021-10-19 15:01:43 +0800562 if (!err && !num_vqs) {
Colin Ian King63b4ffa2021-10-25 11:22:40 +0100563 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
Jason Wang6ae6ff62021-10-19 15:01:43 +0800564 return -EINVAL;
565 }
Amit Shah6abd6e52011-12-22 16:58:29 +0530566
Max Gurtovoy0989c412021-09-02 23:46:22 +0300567 num_vqs = min_t(unsigned int,
568 min_not_zero(num_request_queues, nr_cpu_ids),
569 num_vqs);
Dongli Zhangbf348f92019-03-27 18:36:34 +0800570
Markus Elfring668866b2016-09-13 11:32:22 +0200571 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800572 if (!vblk->vqs)
573 return -ENOMEM;
574
Markus Elfring668866b2016-09-13 11:32:22 +0200575 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
576 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
577 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
Minfei Huang347a5292016-08-09 16:39:20 +0800578 if (!names || !callbacks || !vqs) {
Ming Lei6a27b652014-06-26 17:41:48 +0800579 err = -ENOMEM;
580 goto out;
581 }
582
Ming Lei6a27b652014-06-26 17:41:48 +0800583 for (i = 0; i < num_vqs; i++) {
584 callbacks[i] = virtblk_done;
585 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
586 names[i] = vblk->vqs[i].name;
587 }
588
589 /* Discover virtqueues and write information to configuration. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200590 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
Ming Lei6a27b652014-06-26 17:41:48 +0800591 if (err)
Minfei Huang347a5292016-08-09 16:39:20 +0800592 goto out;
Ming Lei6a27b652014-06-26 17:41:48 +0800593
594 for (i = 0; i < num_vqs; i++) {
595 spin_lock_init(&vblk->vqs[i].lock);
596 vblk->vqs[i].vq = vqs[i];
597 }
598 vblk->num_vqs = num_vqs;
599
Minfei Huang347a5292016-08-09 16:39:20 +0800600out:
Ming Lei6a27b652014-06-26 17:41:48 +0800601 kfree(vqs);
Ming Lei6a27b652014-06-26 17:41:48 +0800602 kfree(callbacks);
Ming Lei6a27b652014-06-26 17:41:48 +0800603 kfree(names);
Ming Lei6a27b652014-06-26 17:41:48 +0800604 if (err)
605 kfree(vblk->vqs);
Amit Shah6abd6e52011-12-22 16:58:29 +0530606 return err;
607}
608
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800609/*
610 * Legacy naming scheme used for virtio devices. We are stuck with it for
611 * virtio blk but don't ever use it for any new driver.
612 */
613static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
614{
615 const int base = 'z' - 'a' + 1;
616 char *begin = buf + strlen(prefix);
617 char *end = buf + buflen;
618 char *p;
619 int unit;
620
621 p = end - 1;
622 *p = '\0';
623 unit = base;
624 do {
625 if (p == begin)
626 return -EINVAL;
627 *--p = 'a' + (index % unit);
628 index = (index / unit) - 1;
629 } while (index >= 0);
630
631 memmove(begin, p, end - p);
632 memcpy(buf, prefix, strlen(prefix));
633
634 return 0;
635}
636
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200637static int virtblk_get_cache_mode(struct virtio_device *vdev)
638{
639 u8 writeback;
640 int err;
641
Rusty Russell855e0c52013-10-14 18:11:51 +1030642 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
643 struct virtio_blk_config, wce,
644 &writeback);
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200645
646 /*
647 * If WCE is not configurable and flush is not available,
648 * assume no writeback cache is in use.
649 */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200650 if (err)
Michael S. Tsirkin592002f2016-02-24 17:07:27 +0200651 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200652
653 return writeback;
654}
655
656static void virtblk_update_cache_mode(struct virtio_device *vdev)
657{
658 u8 writeback = virtblk_get_cache_mode(vdev);
659 struct virtio_blk *vblk = vdev->priv;
660
Jens Axboead9126a2016-03-30 10:12:58 -0600661 blk_queue_write_cache(vblk->disk->queue, writeback, false);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200662}
663
664static const char *const virtblk_cache_types[] = {
665 "write through", "write back"
666};
667
668static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200669cache_type_store(struct device *dev, struct device_attribute *attr,
670 const char *buf, size_t count)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200671{
672 struct gendisk *disk = dev_to_disk(dev);
673 struct virtio_blk *vblk = disk->private_data;
674 struct virtio_device *vdev = vblk->vdev;
675 int i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200676
677 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300678 i = sysfs_match_string(virtblk_cache_types, buf);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200679 if (i < 0)
Andy Shevchenkof53d5aa2017-06-09 15:07:42 +0300680 return i;
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200681
Rusty Russell855e0c52013-10-14 18:11:51 +1030682 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200683 virtblk_update_cache_mode(vdev);
684 return count;
685}
686
687static ssize_t
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200688cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200689{
690 struct gendisk *disk = dev_to_disk(dev);
691 struct virtio_blk *vblk = disk->private_data;
692 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
693
694 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
Ye Guojinf1aa12f2021-10-21 06:51:11 +0000695 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200696}
697
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200698static DEVICE_ATTR_RW(cache_type);
699
700static struct attribute *virtblk_attrs[] = {
701 &dev_attr_serial.attr,
702 &dev_attr_cache_type.attr,
703 NULL,
704};
705
706static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
707 struct attribute *a, int n)
708{
Tian Tao4ce79062020-08-21 09:19:15 +0800709 struct device *dev = kobj_to_dev(kobj);
Hannes Reineckee982c4d2018-09-28 08:17:23 +0200710 struct gendisk *disk = dev_to_disk(dev);
711 struct virtio_blk *vblk = disk->private_data;
712 struct virtio_device *vdev = vblk->vdev;
713
714 if (a == &dev_attr_cache_type.attr &&
715 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
716 return S_IRUGO;
717
718 return a->mode;
719}
720
721static const struct attribute_group virtblk_attr_group = {
722 .attrs = virtblk_attrs,
723 .is_visible = virtblk_attrs_are_visible,
724};
725
726static const struct attribute_group *virtblk_attr_groups[] = {
727 &virtblk_attr_group,
728 NULL,
729};
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200730
Christoph Hellwigad714732017-02-05 18:15:25 +0100731static int virtblk_map_queues(struct blk_mq_tag_set *set)
732{
733 struct virtio_blk *vblk = set->driver_data;
734
Dongli Zhang9bc00752019-03-12 09:31:56 +0800735 return blk_mq_virtio_map_queues(&set->map[HCTX_TYPE_DEFAULT],
736 vblk->vdev, 0);
Christoph Hellwigad714732017-02-05 18:15:25 +0100737}
738
Eric Biggersf363b082017-03-30 13:39:16 -0700739static const struct blk_mq_ops virtio_mq_ops = {
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600740 .queue_rq = virtio_queue_rq,
Jens Axboe944e7c82018-11-26 11:00:12 -0700741 .commit_rqs = virtio_commit_rqs,
Christoph Hellwig5124c282014-02-10 03:24:39 -0800742 .complete = virtblk_request_done,
Christoph Hellwigad714732017-02-05 18:15:25 +0100743 .map_queues = virtblk_map_queues,
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600744};
745
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600746static unsigned int virtblk_queue_depth;
747module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600748
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800749static int virtblk_probe(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000750{
751 struct virtio_blk *vblk;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600752 struct request_queue *q;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200753 int err, index;
Asias Hea98755c2012-08-08 16:07:04 +0800754
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100755 u32 v, blk_size, max_size, sg_elems, opt_io_size;
Christoph Hellwig69740c82010-02-24 14:22:25 -0600756 u16 min_io_size;
757 u8 physical_block_exp, alignment_offset;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800758 unsigned int queue_depth;
Rusty Russelle467cde2007-10-22 11:03:38 +1000759
Michael S. Tsirkinff631982021-10-04 11:31:00 -0400760 if (!vdev->config->get) {
761 dev_err(&vdev->dev, "%s failure: config access disabled\n",
762 __func__);
763 return -EINVAL;
764 }
765
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200766 err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
767 GFP_KERNEL);
768 if (err < 0)
769 goto out;
770 index = err;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100771
Rusty Russell0864b792008-12-30 09:26:05 -0600772 /* We need to know how many segments before we allocate. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030773 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
774 struct virtio_blk_config, seg_max,
775 &sg_elems);
Christoph Hellwiga5b365a2010-05-25 14:17:54 +0200776
777 /* We need at least one SG element, whatever they say. */
778 if (err || !sg_elems)
Rusty Russell0864b792008-12-30 09:26:05 -0600779 sg_elems = 1;
780
Stefan Hajnoczi63947b32021-05-24 16:40:20 +0100781 /* Prevent integer overflows and honor max vq size */
782 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
783
784 /* We need extra sg elements at head and tail. */
Rusty Russell0864b792008-12-30 09:26:05 -0600785 sg_elems += 2;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600786 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
Rusty Russelle467cde2007-10-22 11:03:38 +1000787 if (!vblk) {
788 err = -ENOMEM;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200789 goto out_free_index;
Rusty Russelle467cde2007-10-22 11:03:38 +1000790 }
791
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100792 /* This reference is dropped in virtblk_remove(). */
793 refcount_set(&vblk->refs, 1);
794 mutex_init(&vblk->vdev_mutex);
795
Rusty Russelle467cde2007-10-22 11:03:38 +1000796 vblk->vdev = vdev;
Rusty Russell0864b792008-12-30 09:26:05 -0600797 vblk->sg_elems = sg_elems;
Asias Hea98755c2012-08-08 16:07:04 +0800798
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100799 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
Rusty Russelle467cde2007-10-22 11:03:38 +1000800
Amit Shah6abd6e52011-12-22 16:58:29 +0530801 err = init_vq(vblk);
802 if (err)
Rusty Russelle467cde2007-10-22 11:03:38 +1000803 goto out_free_vblk;
Rusty Russelle467cde2007-10-22 11:03:38 +1000804
Rusty Russellfc4324b2014-03-19 17:08:24 +1030805 /* Default queue sizing is to fill the ring. */
Max Gurtovoy6105d1f2021-09-05 11:57:17 +0300806 if (!virtblk_queue_depth) {
Joseph Qid1e9aa92021-01-22 17:21:46 +0800807 queue_depth = vblk->vqs[0].vq->num_free;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030808 /* ... but without indirect descs, we use 2 descs per req */
809 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
Joseph Qid1e9aa92021-01-22 17:21:46 +0800810 queue_depth /= 2;
811 } else {
812 queue_depth = virtblk_queue_depth;
Rusty Russellfc4324b2014-03-19 17:08:24 +1030813 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600814
815 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
816 vblk->tag_set.ops = &virtio_mq_ops;
Joseph Qid1e9aa92021-01-22 17:21:46 +0800817 vblk->tag_set.queue_depth = queue_depth;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600818 vblk->tag_set.numa_node = NUMA_NO_NODE;
819 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
820 vblk->tag_set.cmd_size =
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600821 sizeof(struct virtblk_req) +
Max Gurtovoy02746e22021-09-01 16:14:34 +0300822 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600823 vblk->tag_set.driver_data = vblk;
Ming Lei6a27b652014-06-26 17:41:48 +0800824 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -0600825
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600826 err = blk_mq_alloc_tag_set(&vblk->tag_set);
827 if (err)
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300828 goto out_free_vq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600829
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300830 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
831 if (IS_ERR(vblk->disk)) {
832 err = PTR_ERR(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600833 goto out_free_tags;
Rusty Russelle467cde2007-10-22 11:03:38 +1000834 }
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300835 q = vblk->disk->queue;
Fernando Luis Vázquez Cao7d116b62008-10-27 18:45:15 +0900836
Ren Mingxinc0aa3e02012-04-10 15:28:05 +0800837 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100838
Rusty Russelle467cde2007-10-22 11:03:38 +1000839 vblk->disk->major = major;
Christian Borntraegerd50ed902008-02-01 09:05:00 +0100840 vblk->disk->first_minor = index_to_minor(index);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300841 vblk->disk->minors = 1 << PART_BITS;
Rusty Russelle467cde2007-10-22 11:03:38 +1000842 vblk->disk->private_data = vblk;
843 vblk->disk->fops = &virtblk_fops;
Fam Zheng5fa31422015-09-06 17:05:42 +0800844 vblk->disk->flags |= GENHD_FL_EXT_DEVT;
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200845 vblk->index = index;
Christian Borntraeger4f3bf192008-01-31 15:53:53 +0100846
Tejun Heo02c42b72010-09-03 11:56:18 +0200847 /* configure queue flush support */
Paolo Bonzinicd5d5032012-07-03 15:19:37 +0200848 virtblk_update_cache_mode(vdev);
Rusty Russelle467cde2007-10-22 11:03:38 +1000849
Christian Borntraeger3ef53602008-05-16 11:17:03 +0200850 /* If disk is read-only in the host, the guest should obey */
851 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
852 set_disk_ro(vblk->disk, 1);
853
Rusty Russell0864b792008-12-30 09:26:05 -0600854 /* We can handle whatever the host told us to handle. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500855 blk_queue_max_segments(q, vblk->sg_elems-2);
Rusty Russell0864b792008-12-30 09:26:05 -0600856
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600857 /* No real sector limit. */
Martin K. Petersenee714f22010-03-10 00:48:32 -0500858 blk_queue_max_hw_sectors(q, -1U);
Rusty Russell4b7f7e22008-12-30 09:26:04 -0600859
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100860 max_size = virtio_max_dma_size(vdev);
861
Rusty Russella586d4f2008-02-04 23:49:56 -0500862 /* Host can optionally specify maximum segment size and number of
863 * segments. */
Rusty Russell855e0c52013-10-14 18:11:51 +1030864 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
865 struct virtio_blk_config, size_max, &v);
Rusty Russelle467cde2007-10-22 11:03:38 +1000866 if (!err)
Joerg Roedelfd1068e2019-02-07 12:59:17 +0100867 max_size = min(max_size, v);
868
869 blk_queue_max_segment_size(q, max_size);
Rusty Russelle467cde2007-10-22 11:03:38 +1000870
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200871 /* Host can optionally specify the block size of the device */
Rusty Russell855e0c52013-10-14 18:11:51 +1030872 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
873 struct virtio_blk_config, blk_size,
874 &blk_size);
Christian Borntraeger066f4d82008-05-29 11:08:26 +0200875 if (!err)
Christoph Hellwig69740c82010-02-24 14:22:25 -0600876 blk_queue_logical_block_size(q, blk_size);
877 else
878 blk_size = queue_logical_block_size(q);
879
880 /* Use topology information if available */
Rusty Russell855e0c52013-10-14 18:11:51 +1030881 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
882 struct virtio_blk_config, physical_block_exp,
883 &physical_block_exp);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600884 if (!err && physical_block_exp)
885 blk_queue_physical_block_size(q,
886 blk_size * (1 << physical_block_exp));
887
Rusty Russell855e0c52013-10-14 18:11:51 +1030888 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
889 struct virtio_blk_config, alignment_offset,
890 &alignment_offset);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600891 if (!err && alignment_offset)
892 blk_queue_alignment_offset(q, blk_size * alignment_offset);
893
Rusty Russell855e0c52013-10-14 18:11:51 +1030894 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
895 struct virtio_blk_config, min_io_size,
896 &min_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600897 if (!err && min_io_size)
898 blk_queue_io_min(q, blk_size * min_io_size);
899
Rusty Russell855e0c52013-10-14 18:11:51 +1030900 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
901 struct virtio_blk_config, opt_io_size,
902 &opt_io_size);
Christoph Hellwig69740c82010-02-24 14:22:25 -0600903 if (!err && opt_io_size)
904 blk_queue_io_opt(q, blk_size * opt_io_size);
905
Changpeng Liu1f238162018-11-01 15:40:35 -0700906 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
907 q->limits.discard_granularity = blk_size;
908
909 virtio_cread(vdev, struct virtio_blk_config,
910 discard_sector_alignment, &v);
911 q->limits.discard_alignment = v ? v << SECTOR_SHIFT : 0;
912
913 virtio_cread(vdev, struct virtio_blk_config,
914 max_discard_sectors, &v);
915 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
916
917 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
918 &v);
919 blk_queue_max_discard_segments(q,
920 min_not_zero(v,
921 MAX_DISCARD_SEGMENTS));
922
923 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
924 }
925
926 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
927 virtio_cread(vdev, struct virtio_blk_config,
928 max_write_zeroes_sectors, &v);
929 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
930 }
931
Stefan Hajnoczidaf2a502018-01-03 16:03:39 +0000932 virtblk_update_capacity(vblk, false);
Michael S. Tsirkin7a113702014-10-15 10:22:30 +1030933 virtio_device_ready(vdev);
934
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200935 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
936 if (err)
937 goto out_cleanup_disk;
938
Rusty Russelle467cde2007-10-22 11:03:38 +1000939 return 0;
940
Luis Chamberlaindbb301f92021-08-18 16:45:41 +0200941out_cleanup_disk:
Xie Yongji82e89ea2021-08-09 18:16:09 +0800942 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600943out_free_tags:
944 blk_mq_free_tag_set(&vblk->tag_set);
Rusty Russelle467cde2007-10-22 11:03:38 +1000945out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600946 vdev->config->del_vqs(vdev);
Hou Taoe7eea442020-06-15 12:14:59 +0800947 kfree(vblk->vqs);
Rusty Russelle467cde2007-10-22 11:03:38 +1000948out_free_vblk:
949 kfree(vblk);
Michael S. Tsirkin5087a502011-10-30 21:29:59 +0200950out_free_index:
951 ida_simple_remove(&vd_index_ida, index);
Rusty Russelle467cde2007-10-22 11:03:38 +1000952out:
953 return err;
954}
955
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800956static void virtblk_remove(struct virtio_device *vdev)
Rusty Russelle467cde2007-10-22 11:03:38 +1000957{
958 struct virtio_blk *vblk = vdev->priv;
Rusty Russelle467cde2007-10-22 11:03:38 +1000959
Michael S. Tsirkincc74f712014-10-15 10:22:26 +1030960 /* Make sure no work handler is accessing the device. */
961 flush_work(&vblk->config_work);
Christoph Hellwig7a7c9242011-02-01 21:43:48 +0100962
Asias He02e2b122012-05-25 10:34:47 +0800963 del_gendisk(vblk->disk);
Christoph Hellwig89a5f062021-06-02 09:53:19 +0300964 blk_cleanup_disk(vblk->disk);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600965 blk_mq_free_tag_set(&vblk->tag_set);
966
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100967 mutex_lock(&vblk->vdev_mutex);
968
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500969 /* Stop all the virtqueues. */
970 vdev->config->reset(vdev);
971
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100972 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
973 vblk->vdev = NULL;
974
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600975 vdev->config->del_vqs(vdev);
Ming Lei6a27b652014-06-26 17:41:48 +0800976 kfree(vblk->vqs);
Alexander Graff4953fe2013-01-02 15:37:17 +1030977
Stefan Hajnoczi90b5feb2020-04-30 15:04:42 +0100978 mutex_unlock(&vblk->vdev_mutex);
979
980 virtblk_put(vblk);
Rusty Russelle467cde2007-10-22 11:03:38 +1000981}
982
Aaron Lu89107002013-09-17 09:25:23 +0930983#ifdef CONFIG_PM_SLEEP
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530984static int virtblk_freeze(struct virtio_device *vdev)
985{
986 struct virtio_blk *vblk = vdev->priv;
987
988 /* Ensure we don't receive any more interrupts */
989 vdev->config->reset(vdev);
990
Michael S. Tsirkincc74f712014-10-15 10:22:26 +1030991 /* Make sure no work handler is accessing the device. */
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530992 flush_work(&vblk->config_work);
993
Sagi Grimberg9b3e9902017-07-04 10:03:03 +0300994 blk_mq_quiesce_queue(vblk->disk->queue);
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530995
996 vdev->config->del_vqs(vdev);
Xie Yongjib71ba222021-05-17 16:43:32 +0800997 kfree(vblk->vqs);
998
Amit Shahf8fb5bc2011-12-22 16:58:30 +0530999 return 0;
1000}
1001
1002static int virtblk_restore(struct virtio_device *vdev)
1003{
1004 struct virtio_blk *vblk = vdev->priv;
1005 int ret;
1006
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301007 ret = init_vq(vdev->priv);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301008 if (ret)
1009 return ret;
Jens Axboe1cf7e9c2013-11-01 10:52:52 -06001010
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301011 virtio_device_ready(vdev);
1012
Sagi Grimberg9b3e9902017-07-04 10:03:03 +03001013 blk_mq_unquiesce_queue(vblk->disk->queue);
Michael S. Tsirkin6d62c372014-10-15 10:22:32 +10301014 return 0;
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301015}
1016#endif
1017
Márton Németh47483e22010-01-10 13:40:02 +01001018static const struct virtio_device_id id_table[] = {
Rusty Russelle467cde2007-10-22 11:03:38 +10001019 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1020 { 0 },
1021};
1022
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001023static unsigned int features_legacy[] = {
Tejun Heo02c42b72010-09-03 11:56:18 +02001024 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
Christoph Hellwig97b50a62017-01-28 09:32:53 +01001025 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001026 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001027 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001028}
1029;
1030static unsigned int features[] = {
1031 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1032 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
Michael S. Tsirkin592002f2016-02-24 17:07:27 +02001033 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
Changpeng Liu1f238162018-11-01 15:40:35 -07001034 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
Rusty Russellc45a6812008-05-02 21:50:50 -05001035};
1036
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001037static struct virtio_driver virtio_blk = {
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001038 .feature_table = features,
1039 .feature_table_size = ARRAY_SIZE(features),
1040 .feature_table_legacy = features_legacy,
1041 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1042 .driver.name = KBUILD_MODNAME,
1043 .driver.owner = THIS_MODULE,
1044 .id_table = id_table,
1045 .probe = virtblk_probe,
1046 .remove = virtblk_remove,
1047 .config_changed = virtblk_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301048#ifdef CONFIG_PM_SLEEP
Michael S. Tsirkin19c1c5a2014-10-07 16:39:49 +02001049 .freeze = virtblk_freeze,
1050 .restore = virtblk_restore,
Amit Shahf8fb5bc2011-12-22 16:58:30 +05301051#endif
Rusty Russelle467cde2007-10-22 11:03:38 +10001052};
1053
1054static int __init init(void)
1055{
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001056 int error;
1057
1058 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1059 if (!virtblk_wq)
1060 return -ENOMEM;
1061
Christian Borntraeger4f3bf192008-01-31 15:53:53 +01001062 major = register_blkdev(0, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001063 if (major < 0) {
1064 error = major;
1065 goto out_destroy_workqueue;
1066 }
1067
1068 error = register_virtio_driver(&virtio_blk);
1069 if (error)
1070 goto out_unregister_blkdev;
1071 return 0;
1072
1073out_unregister_blkdev:
1074 unregister_blkdev(major, "virtblk");
1075out_destroy_workqueue:
1076 destroy_workqueue(virtblk_wq);
1077 return error;
Rusty Russelle467cde2007-10-22 11:03:38 +10001078}
1079
1080static void __exit fini(void)
1081{
Rusty Russelle467cde2007-10-22 11:03:38 +10001082 unregister_virtio_driver(&virtio_blk);
Michael S. Tsirkin38f37b52014-10-23 18:57:19 +03001083 unregister_blkdev(major, "virtblk");
Christoph Hellwig7a7c9242011-02-01 21:43:48 +01001084 destroy_workqueue(virtblk_wq);
Rusty Russelle467cde2007-10-22 11:03:38 +10001085}
1086module_init(init);
1087module_exit(fini);
1088
1089MODULE_DEVICE_TABLE(virtio, id_table);
1090MODULE_DESCRIPTION("Virtio block driver");
1091MODULE_LICENSE("GPL");