Merge tag 'hyperv-next-signed-20230902' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / block / virtio_blk.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
e467cde2
RR
2//#define DEBUG
3#include <linux/spinlock.h>
5a0e3ad6 4#include <linux/slab.h>
e467cde2
RR
5#include <linux/blkdev.h>
6#include <linux/hdreg.h>
0c8d44f2 7#include <linux/module.h>
4678d6f9 8#include <linux/mutex.h>
ad71473d 9#include <linux/interrupt.h>
e467cde2
RR
10#include <linux/virtio.h>
11#include <linux/virtio_blk.h>
3d1266c7 12#include <linux/scatterlist.h>
7a7c924c 13#include <linux/string_helpers.h>
5087a50e 14#include <linux/idr.h>
1cf7e9c6 15#include <linux/blk-mq.h>
ad71473d 16#include <linux/blk-mq-virtio.h>
1cf7e9c6 17#include <linux/numa.h>
95bfec41 18#include <linux/vmalloc.h>
55a2415b 19#include <uapi/linux/virtio_ring.h>
3d1266c7 20
4f3bf19c 21#define PART_BITS 4
6a27b656 22#define VQ_NAME_LEN 16
1f23816b 23#define MAX_DISCARD_SEGMENTS 256u
e467cde2 24
63947b34
SH
25/* The maximum number of sg elements that fit into a virtqueue */
26#define VIRTIO_BLK_MAX_SG_ELEMS 32768
27
02746e26
MG
28#ifdef CONFIG_ARCH_NO_SG_CHAIN
29#define VIRTIO_BLK_INLINE_SG_CNT 0
30#else
31#define VIRTIO_BLK_INLINE_SG_CNT 2
32#endif
33
0989c41b 34static unsigned int num_request_queues;
ead65f76 35module_param(num_request_queues, uint, 0644);
0989c41b
MG
36MODULE_PARM_DESC(num_request_queues,
37 "Limit the number of request queues to use for blk device. "
38 "0 for no limit. "
39 "Values > nr_cpu_ids truncated to nr_cpu_ids.");
40
4e040052
SK
41static unsigned int poll_queues;
42module_param(poll_queues, uint, 0644);
43MODULE_PARM_DESC(poll_queues, "The number of dedicated virtqueues for polling I/O");
44
5087a50e
MT
45static int major;
46static DEFINE_IDA(vd_index_ida);
47
2a647bfe 48static struct workqueue_struct *virtblk_wq;
4f3bf19c 49
6a27b656
ML
50struct virtio_blk_vq {
51 struct virtqueue *vq;
52 spinlock_t lock;
53 char name[VQ_NAME_LEN];
54} ____cacheline_aligned_in_smp;
55
bb6ec576 56struct virtio_blk {
90b5feb8
SH
57 /*
58 * This mutex must be held by anything that may run after
59 * virtblk_remove() sets vblk->vdev to NULL.
60 *
61 * blk-mq, virtqueue processing, and sysfs attribute code paths are
62 * shut down before vblk->vdev is set to NULL and therefore do not need
63 * to hold this mutex.
64 */
65 struct mutex vdev_mutex;
e467cde2 66 struct virtio_device *vdev;
e467cde2
RR
67
68 /* The disk structure for the kernel. */
69 struct gendisk *disk;
70
24d2f903
CH
71 /* Block layer tags. */
72 struct blk_mq_tag_set tag_set;
73
7a7c924c
CH
74 /* Process context for config space updates */
75 struct work_struct config_work;
76
5087a50e
MT
77 /* Ida index - used to track minor number allocations. */
78 int index;
6a27b656
ML
79
80 /* num of vqs */
81 int num_vqs;
4e040052 82 int io_queues[HCTX_MAX_TYPES];
6a27b656 83 struct virtio_blk_vq *vqs;
95bfec41
DF
84
85 /* For zoned device */
86 unsigned int zone_sectors;
e467cde2
RR
87};
88
bb6ec576 89struct virtblk_req {
95bfec41 90 /* Out header */
97b50a65 91 struct virtio_blk_outhdr out_hdr;
95bfec41
DF
92
93 /* In header */
94 union {
95 u8 status;
96
97 /*
98 * The zone append command has an extended in header.
f1ba4e67
DF
99 * The status field in zone_append_in_hdr must always
100 * be the last byte.
95bfec41
DF
101 */
102 struct {
f1ba4e67 103 __virtio64 sector;
95bfec41 104 u8 status;
f1ba4e67
DF
105 } zone_append;
106 } in_hdr;
95bfec41
DF
107
108 size_t in_hdr_len;
109
02746e26 110 struct sg_table sg_table;
a98755c5 111 struct scatterlist sg[];
e467cde2
RR
112};
113
95bfec41 114static inline blk_status_t virtblk_result(u8 status)
a98755c5 115{
95bfec41 116 switch (status) {
a98755c5 117 case VIRTIO_BLK_S_OK:
2a842aca 118 return BLK_STS_OK;
a98755c5 119 case VIRTIO_BLK_S_UNSUPP:
2a842aca 120 return BLK_STS_NOTSUPP;
95bfec41
DF
121 case VIRTIO_BLK_S_ZONE_OPEN_RESOURCE:
122 return BLK_STS_ZONE_OPEN_RESOURCE;
123 case VIRTIO_BLK_S_ZONE_ACTIVE_RESOURCE:
124 return BLK_STS_ZONE_ACTIVE_RESOURCE;
125 case VIRTIO_BLK_S_IOERR:
126 case VIRTIO_BLK_S_ZONE_UNALIGNED_WP:
a98755c5 127 default:
2a842aca 128 return BLK_STS_IOERR;
a98755c5
AH
129 }
130}
131
8d12ec10
SY
132static inline struct virtio_blk_vq *get_virtio_blk_vq(struct blk_mq_hw_ctx *hctx)
133{
134 struct virtio_blk *vblk = hctx->queue->queuedata;
135 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
136
137 return vq;
138}
139
0e9911fa 140static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr)
97b50a65 141{
95bfec41 142 struct scatterlist out_hdr, in_hdr, *sgs[3];
97b50a65
CH
143 unsigned int num_out = 0, num_in = 0;
144
95bfec41
DF
145 sg_init_one(&out_hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
146 sgs[num_out++] = &out_hdr;
20af3cfd 147
0e9911fa 148 if (vbr->sg_table.nents) {
19c1c5a6 149 if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
0e9911fa 150 sgs[num_out++] = vbr->sg_table.sgl;
8f39db9d 151 else
0e9911fa 152 sgs[num_out + num_in++] = vbr->sg_table.sgl;
20af3cfd
PB
153 }
154
f1ba4e67 155 sg_init_one(&in_hdr, &vbr->in_hdr.status, vbr->in_hdr_len);
95bfec41 156 sgs[num_out + num_in++] = &in_hdr;
8f39db9d
PB
157
158 return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
5ee21a52
PB
159}
160
e60d6407 161static int virtblk_setup_discard_write_zeroes_erase(struct request *req, bool unmap)
1f23816b
CL
162{
163 unsigned short segments = blk_rq_nr_discard_segments(req);
164 unsigned short n = 0;
165 struct virtio_blk_discard_write_zeroes *range;
166 struct bio *bio;
167 u32 flags = 0;
168
169 if (unmap)
170 flags |= VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP;
171
172 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
173 if (!range)
174 return -ENOMEM;
175
af822aa6
ML
176 /*
177 * Single max discard segment means multi-range discard isn't
178 * supported, and block layer only runs contiguity merge like
179 * normal RW request. So we can't reply on bio for retrieving
180 * each range info.
181 */
182 if (queue_max_discard_segments(req->q) == 1) {
183 range[0].flags = cpu_to_le32(flags);
184 range[0].num_sectors = cpu_to_le32(blk_rq_sectors(req));
185 range[0].sector = cpu_to_le64(blk_rq_pos(req));
186 n = 1;
187 } else {
188 __rq_for_each_bio(bio, req) {
189 u64 sector = bio->bi_iter.bi_sector;
190 u32 num_sectors = bio->bi_iter.bi_size >> SECTOR_SHIFT;
191
192 range[n].flags = cpu_to_le32(flags);
193 range[n].num_sectors = cpu_to_le32(num_sectors);
194 range[n].sector = cpu_to_le64(sector);
195 n++;
196 }
1f23816b
CL
197 }
198
af822aa6
ML
199 WARN_ON_ONCE(n != segments);
200
b831f3a1 201 bvec_set_virt(&req->special_vec, range, sizeof(*range) * segments);
1f23816b
CL
202 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
203
204 return 0;
205}
206
02746e26 207static void virtblk_unmap_data(struct request *req, struct virtblk_req *vbr)
a98755c5 208{
02746e26
MG
209 if (blk_rq_nr_phys_segments(req))
210 sg_free_table_chained(&vbr->sg_table,
211 VIRTIO_BLK_INLINE_SG_CNT);
212}
213
214static int virtblk_map_data(struct blk_mq_hw_ctx *hctx, struct request *req,
215 struct virtblk_req *vbr)
216{
217 int err;
218
219 if (!blk_rq_nr_phys_segments(req))
220 return 0;
a98755c5 221
02746e26
MG
222 vbr->sg_table.sgl = vbr->sg;
223 err = sg_alloc_table_chained(&vbr->sg_table,
224 blk_rq_nr_phys_segments(req),
225 vbr->sg_table.sgl,
226 VIRTIO_BLK_INLINE_SG_CNT);
227 if (unlikely(err))
228 return -ENOMEM;
a98755c5 229
02746e26
MG
230 return blk_rq_map_sg(hctx->queue, req, vbr->sg_table.sgl);
231}
232
233static void virtblk_cleanup_cmd(struct request *req)
234{
358b348b
CH
235 if (req->rq_flags & RQF_SPECIAL_PAYLOAD)
236 kfree(bvec_virt(&req->special_vec));
02746e26
MG
237}
238
f0839372
MT
239static blk_status_t virtblk_setup_cmd(struct virtio_device *vdev,
240 struct request *req,
241 struct virtblk_req *vbr)
02746e26 242{
f1ba4e67 243 size_t in_hdr_len = sizeof(vbr->in_hdr.status);
02746e26
MG
244 bool unmap = false;
245 u32 type;
95bfec41 246 u64 sector = 0;
02746e26 247
f1ba4e67
DF
248 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) && op_is_zone_mgmt(req_op(req)))
249 return BLK_STS_NOTSUPP;
250
95bfec41
DF
251 /* Set fields for all request types */
252 vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, req_get_ioprio(req));
02746e26
MG
253
254 switch (req_op(req)) {
255 case REQ_OP_READ:
256 type = VIRTIO_BLK_T_IN;
95bfec41 257 sector = blk_rq_pos(req);
02746e26
MG
258 break;
259 case REQ_OP_WRITE:
260 type = VIRTIO_BLK_T_OUT;
95bfec41 261 sector = blk_rq_pos(req);
02746e26
MG
262 break;
263 case REQ_OP_FLUSH:
264 type = VIRTIO_BLK_T_FLUSH;
265 break;
266 case REQ_OP_DISCARD:
267 type = VIRTIO_BLK_T_DISCARD;
268 break;
269 case REQ_OP_WRITE_ZEROES:
270 type = VIRTIO_BLK_T_WRITE_ZEROES;
271 unmap = !(req->cmd_flags & REQ_NOUNMAP);
272 break;
e60d6407
AK
273 case REQ_OP_SECURE_ERASE:
274 type = VIRTIO_BLK_T_SECURE_ERASE;
275 break;
95bfec41
DF
276 case REQ_OP_ZONE_OPEN:
277 type = VIRTIO_BLK_T_ZONE_OPEN;
278 sector = blk_rq_pos(req);
279 break;
280 case REQ_OP_ZONE_CLOSE:
281 type = VIRTIO_BLK_T_ZONE_CLOSE;
282 sector = blk_rq_pos(req);
283 break;
284 case REQ_OP_ZONE_FINISH:
285 type = VIRTIO_BLK_T_ZONE_FINISH;
286 sector = blk_rq_pos(req);
02746e26 287 break;
95bfec41
DF
288 case REQ_OP_ZONE_APPEND:
289 type = VIRTIO_BLK_T_ZONE_APPEND;
290 sector = blk_rq_pos(req);
f1ba4e67 291 in_hdr_len = sizeof(vbr->in_hdr.zone_append);
02746e26 292 break;
95bfec41
DF
293 case REQ_OP_ZONE_RESET:
294 type = VIRTIO_BLK_T_ZONE_RESET;
295 sector = blk_rq_pos(req);
296 break;
297 case REQ_OP_ZONE_RESET_ALL:
298 type = VIRTIO_BLK_T_ZONE_RESET_ALL;
299 break;
300 case REQ_OP_DRV_IN:
f1ba4e67
DF
301 /*
302 * Out header has already been prepared by the caller (virtblk_get_id()
303 * or virtblk_submit_zone_report()), nothing to do here.
304 */
95bfec41 305 return 0;
02746e26
MG
306 default:
307 WARN_ON_ONCE(1);
308 return BLK_STS_IOERR;
309 }
310
95bfec41
DF
311 /* Set fields for non-REQ_OP_DRV_IN request types */
312 vbr->in_hdr_len = in_hdr_len;
02746e26 313 vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
95bfec41 314 vbr->out_hdr.sector = cpu_to_virtio64(vdev, sector);
02746e26 315
e60d6407
AK
316 if (type == VIRTIO_BLK_T_DISCARD || type == VIRTIO_BLK_T_WRITE_ZEROES ||
317 type == VIRTIO_BLK_T_SECURE_ERASE) {
318 if (virtblk_setup_discard_write_zeroes_erase(req, unmap))
02746e26
MG
319 return BLK_STS_RESOURCE;
320 }
321
322 return 0;
323}
324
f1ba4e67
DF
325/*
326 * The status byte is always the last byte of the virtblk request
327 * in-header. This helper fetches its value for all in-header formats
328 * that are currently defined.
329 */
330static inline u8 virtblk_vbr_status(struct virtblk_req *vbr)
331{
332 return *((u8 *)&vbr->in_hdr + vbr->in_hdr_len - 1);
333}
334
02746e26
MG
335static inline void virtblk_request_done(struct request *req)
336{
337 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
f1ba4e67
DF
338 blk_status_t status = virtblk_result(virtblk_vbr_status(vbr));
339 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
02746e26
MG
340
341 virtblk_unmap_data(req, vbr);
342 virtblk_cleanup_cmd(req);
95bfec41
DF
343
344 if (req_op(req) == REQ_OP_ZONE_APPEND)
f1ba4e67
DF
345 req->__sector = virtio64_to_cpu(vblk->vdev,
346 vbr->in_hdr.zone_append.sector);
95bfec41
DF
347
348 blk_mq_end_request(req, status);
a98755c5
AH
349}
350
351static void virtblk_done(struct virtqueue *vq)
e467cde2
RR
352{
353 struct virtio_blk *vblk = vq->vdev->priv;
afd384f0
MT
354 bool req_done = false;
355 int qid = vq->index;
356 struct virtblk_req *vbr;
e467cde2 357 unsigned long flags;
afd384f0 358 unsigned int len;
e467cde2 359
afd384f0 360 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
bb811108
AH
361 do {
362 virtqueue_disable_cb(vq);
afd384f0
MT
363 while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
364 struct request *req = blk_mq_rq_from_pdu(vbr);
85dada09 365
afd384f0
MT
366 if (likely(!blk_should_fake_timeout(req->q)))
367 blk_mq_complete_request(req);
368 req_done = true;
369 }
7f03b17d
HG
370 if (unlikely(virtqueue_is_broken(vq)))
371 break;
bb811108 372 } while (!virtqueue_enable_cb(vq));
1cf7e9c6 373
afd384f0
MT
374 /* In case queue is stopped waiting for more buffers. */
375 if (req_done)
1b4a3258 376 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
afd384f0 377 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
a98755c5
AH
378}
379
944e7c87
JA
380static void virtio_commit_rqs(struct blk_mq_hw_ctx *hctx)
381{
382 struct virtio_blk *vblk = hctx->queue->queuedata;
383 struct virtio_blk_vq *vq = &vblk->vqs[hctx->queue_num];
384 bool kick;
385
386 spin_lock_irq(&vq->lock);
387 kick = virtqueue_kick_prepare(vq->vq);
388 spin_unlock_irq(&vq->lock);
389
390 if (kick)
391 virtqueue_notify(vq->vq);
392}
393
258896fc
DF
394static blk_status_t virtblk_fail_to_queue(struct request *req, int rc)
395{
396 virtblk_cleanup_cmd(req);
397 switch (rc) {
398 case -ENOSPC:
399 return BLK_STS_DEV_RESOURCE;
400 case -ENOMEM:
401 return BLK_STS_RESOURCE;
402 default:
403 return BLK_STS_IOERR;
404 }
405}
406
0e9911fa
SK
407static blk_status_t virtblk_prep_rq(struct blk_mq_hw_ctx *hctx,
408 struct virtio_blk *vblk,
409 struct request *req,
410 struct virtblk_req *vbr)
411{
412 blk_status_t status;
a26116c1 413 int num;
0e9911fa
SK
414
415 status = virtblk_setup_cmd(vblk->vdev, req, vbr);
416 if (unlikely(status))
417 return status;
418
a26116c1
RM
419 num = virtblk_map_data(hctx, req, vbr);
420 if (unlikely(num < 0))
258896fc 421 return virtblk_fail_to_queue(req, -ENOMEM);
a26116c1 422 vbr->sg_table.nents = num;
0e9911fa 423
37fafe6b
SK
424 blk_mq_start_request(req);
425
0e9911fa
SK
426 return BLK_STS_OK;
427}
428
fc17b653 429static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
74c45052 430 const struct blk_mq_queue_data *bd)
e467cde2 431{
1cf7e9c6 432 struct virtio_blk *vblk = hctx->queue->queuedata;
74c45052 433 struct request *req = bd->rq;
9d74e257 434 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
1cf7e9c6 435 unsigned long flags;
6a27b656 436 int qid = hctx->queue_num;
e8edca6f 437 bool notify = false;
f0839372
MT
438 blk_status_t status;
439 int err;
e467cde2 440
0e9911fa 441 status = virtblk_prep_rq(hctx, vblk, req, vbr);
f0839372
MT
442 if (unlikely(status))
443 return status;
aebf526b 444
6a27b656 445 spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
0e9911fa 446 err = virtblk_add_req(vblk->vqs[qid].vq, vbr);
5261b85e 447 if (err) {
6a27b656 448 virtqueue_kick(vblk->vqs[qid].vq);
f5f6b95c
HP
449 /* Don't stop the queue if -ENOMEM: we may have failed to
450 * bounce the buffer due to global resource outage.
451 */
452 if (err == -ENOSPC)
453 blk_mq_stop_hw_queue(hctx);
6a27b656 454 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
02746e26 455 virtblk_unmap_data(req, vbr);
258896fc 456 return virtblk_fail_to_queue(req, err);
a98755c5
AH
457 }
458
74c45052 459 if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
e8edca6f 460 notify = true;
6a27b656 461 spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
e8edca6f
ML
462
463 if (notify)
6a27b656 464 virtqueue_notify(vblk->vqs[qid].vq);
fc17b653 465 return BLK_STS_OK;
a98755c5
AH
466}
467
0e9911fa
SK
468static bool virtblk_prep_rq_batch(struct request *req)
469{
470 struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
471 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
472
473 req->mq_hctx->tags->rqs[req->tag] = req;
474
475 return virtblk_prep_rq(req->mq_hctx, vblk, req, vbr) == BLK_STS_OK;
476}
477
478static bool virtblk_add_req_batch(struct virtio_blk_vq *vq,
37fafe6b 479 struct request **rqlist)
0e9911fa
SK
480{
481 unsigned long flags;
482 int err;
483 bool kick;
484
485 spin_lock_irqsave(&vq->lock, flags);
486
487 while (!rq_list_empty(*rqlist)) {
488 struct request *req = rq_list_pop(rqlist);
489 struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
490
491 err = virtblk_add_req(vq->vq, vbr);
492 if (err) {
493 virtblk_unmap_data(req, vbr);
494 virtblk_cleanup_cmd(req);
37fafe6b 495 blk_mq_requeue_request(req, true);
0e9911fa
SK
496 }
497 }
498
499 kick = virtqueue_kick_prepare(vq->vq);
500 spin_unlock_irqrestore(&vq->lock, flags);
501
502 return kick;
503}
504
505static void virtio_queue_rqs(struct request **rqlist)
506{
507 struct request *req, *next, *prev = NULL;
508 struct request *requeue_list = NULL;
509
510 rq_list_for_each_safe(rqlist, req, next) {
8d12ec10 511 struct virtio_blk_vq *vq = get_virtio_blk_vq(req->mq_hctx);
0e9911fa
SK
512 bool kick;
513
514 if (!virtblk_prep_rq_batch(req)) {
515 rq_list_move(rqlist, &requeue_list, req, prev);
516 req = prev;
517 if (!req)
518 continue;
519 }
520
521 if (!next || req->mq_hctx != next->mq_hctx) {
522 req->rq_next = NULL;
37fafe6b 523 kick = virtblk_add_req_batch(vq, rqlist);
0e9911fa
SK
524 if (kick)
525 virtqueue_notify(vq->vq);
526
527 *rqlist = next;
528 prev = NULL;
529 } else
530 prev = req;
531 }
532
533 *rqlist = requeue_list;
534}
535
95bfec41
DF
536#ifdef CONFIG_BLK_DEV_ZONED
537static void *virtblk_alloc_report_buffer(struct virtio_blk *vblk,
538 unsigned int nr_zones,
95bfec41
DF
539 size_t *buflen)
540{
541 struct request_queue *q = vblk->disk->queue;
542 size_t bufsize;
543 void *buf;
544
545 nr_zones = min_t(unsigned int, nr_zones,
f1ba4e67 546 get_capacity(vblk->disk) >> ilog2(vblk->zone_sectors));
95bfec41
DF
547
548 bufsize = sizeof(struct virtio_blk_zone_report) +
549 nr_zones * sizeof(struct virtio_blk_zone_descriptor);
550 bufsize = min_t(size_t, bufsize,
551 queue_max_hw_sectors(q) << SECTOR_SHIFT);
552 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
553
554 while (bufsize >= sizeof(struct virtio_blk_zone_report)) {
555 buf = __vmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
556 if (buf) {
557 *buflen = bufsize;
558 return buf;
559 }
560 bufsize >>= 1;
561 }
562
563 return NULL;
564}
565
566static int virtblk_submit_zone_report(struct virtio_blk *vblk,
567 char *report_buf, size_t report_len,
568 sector_t sector)
569{
570 struct request_queue *q = vblk->disk->queue;
571 struct request *req;
572 struct virtblk_req *vbr;
573 int err;
574
575 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
576 if (IS_ERR(req))
577 return PTR_ERR(req);
578
579 vbr = blk_mq_rq_to_pdu(req);
f1ba4e67 580 vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
95bfec41
DF
581 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_ZONE_REPORT);
582 vbr->out_hdr.sector = cpu_to_virtio64(vblk->vdev, sector);
583
584 err = blk_rq_map_kern(q, req, report_buf, report_len, GFP_KERNEL);
585 if (err)
586 goto out;
587
588 blk_execute_rq(req, false);
f1ba4e67 589 err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
95bfec41
DF
590out:
591 blk_mq_free_request(req);
592 return err;
593}
594
595static int virtblk_parse_zone(struct virtio_blk *vblk,
596 struct virtio_blk_zone_descriptor *entry,
f1ba4e67 597 unsigned int idx, report_zones_cb cb, void *data)
95bfec41
DF
598{
599 struct blk_zone zone = { };
600
f1ba4e67
DF
601 zone.start = virtio64_to_cpu(vblk->vdev, entry->z_start);
602 if (zone.start + vblk->zone_sectors <= get_capacity(vblk->disk))
603 zone.len = vblk->zone_sectors;
604 else
605 zone.len = get_capacity(vblk->disk) - zone.start;
606 zone.capacity = virtio64_to_cpu(vblk->vdev, entry->z_cap);
607 zone.wp = virtio64_to_cpu(vblk->vdev, entry->z_wp);
608
609 switch (entry->z_type) {
610 case VIRTIO_BLK_ZT_SWR:
611 zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
612 break;
613 case VIRTIO_BLK_ZT_SWP:
614 zone.type = BLK_ZONE_TYPE_SEQWRITE_PREF;
615 break;
616 case VIRTIO_BLK_ZT_CONV:
617 zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
618 break;
619 default:
620 dev_err(&vblk->vdev->dev, "zone %llu: invalid type %#x\n",
621 zone.start, entry->z_type);
622 return -EIO;
95bfec41
DF
623 }
624
f1ba4e67
DF
625 switch (entry->z_state) {
626 case VIRTIO_BLK_ZS_EMPTY:
627 zone.cond = BLK_ZONE_COND_EMPTY;
628 break;
629 case VIRTIO_BLK_ZS_CLOSED:
630 zone.cond = BLK_ZONE_COND_CLOSED;
631 break;
632 case VIRTIO_BLK_ZS_FULL:
633 zone.cond = BLK_ZONE_COND_FULL;
95bfec41 634 zone.wp = zone.start + zone.len;
f1ba4e67
DF
635 break;
636 case VIRTIO_BLK_ZS_EOPEN:
637 zone.cond = BLK_ZONE_COND_EXP_OPEN;
638 break;
639 case VIRTIO_BLK_ZS_IOPEN:
640 zone.cond = BLK_ZONE_COND_IMP_OPEN;
641 break;
642 case VIRTIO_BLK_ZS_NOT_WP:
643 zone.cond = BLK_ZONE_COND_NOT_WP;
644 break;
645 case VIRTIO_BLK_ZS_RDONLY:
646 zone.cond = BLK_ZONE_COND_READONLY;
647 zone.wp = ULONG_MAX;
648 break;
649 case VIRTIO_BLK_ZS_OFFLINE:
650 zone.cond = BLK_ZONE_COND_OFFLINE;
651 zone.wp = ULONG_MAX;
652 break;
653 default:
654 dev_err(&vblk->vdev->dev, "zone %llu: invalid condition %#x\n",
655 zone.start, entry->z_state);
656 return -EIO;
657 }
95bfec41 658
f1ba4e67
DF
659 /*
660 * The callback below checks the validity of the reported
661 * entry data, no need to further validate it here.
662 */
95bfec41
DF
663 return cb(&zone, idx, data);
664}
665
666static int virtblk_report_zones(struct gendisk *disk, sector_t sector,
667 unsigned int nr_zones, report_zones_cb cb,
668 void *data)
669{
670 struct virtio_blk *vblk = disk->private_data;
671 struct virtio_blk_zone_report *report;
f1ba4e67 672 unsigned long long nz, i;
95bfec41 673 size_t buflen;
f1ba4e67
DF
674 unsigned int zone_idx = 0;
675 int ret;
95bfec41
DF
676
677 if (WARN_ON_ONCE(!vblk->zone_sectors))
678 return -EOPNOTSUPP;
679
f1ba4e67 680 report = virtblk_alloc_report_buffer(vblk, nr_zones, &buflen);
95bfec41
DF
681 if (!report)
682 return -ENOMEM;
683
f1ba4e67
DF
684 mutex_lock(&vblk->vdev_mutex);
685
686 if (!vblk->vdev) {
687 ret = -ENXIO;
688 goto fail_report;
689 }
690
95bfec41
DF
691 while (zone_idx < nr_zones && sector < get_capacity(vblk->disk)) {
692 memset(report, 0, buflen);
693
694 ret = virtblk_submit_zone_report(vblk, (char *)report,
695 buflen, sector);
f1ba4e67
DF
696 if (ret)
697 goto fail_report;
698
699 nz = min_t(u64, virtio64_to_cpu(vblk->vdev, report->nr_zones),
700 nr_zones);
95bfec41
DF
701 if (!nz)
702 break;
703
704 for (i = 0; i < nz && zone_idx < nr_zones; i++) {
705 ret = virtblk_parse_zone(vblk, &report->zones[i],
f1ba4e67 706 zone_idx, cb, data);
95bfec41 707 if (ret)
f1ba4e67
DF
708 goto fail_report;
709
710 sector = virtio64_to_cpu(vblk->vdev,
711 report->zones[i].z_start) +
712 vblk->zone_sectors;
95bfec41
DF
713 zone_idx++;
714 }
715 }
716
717 if (zone_idx > 0)
718 ret = zone_idx;
719 else
720 ret = -EINVAL;
f1ba4e67
DF
721fail_report:
722 mutex_unlock(&vblk->vdev_mutex);
95bfec41
DF
723 kvfree(report);
724 return ret;
725}
726
727static void virtblk_revalidate_zones(struct virtio_blk *vblk)
728{
729 u8 model;
730
95bfec41
DF
731 virtio_cread(vblk->vdev, struct virtio_blk_config,
732 zoned.model, &model);
f1ba4e67
DF
733 switch (model) {
734 default:
735 dev_err(&vblk->vdev->dev, "unknown zone model %d\n", model);
736 fallthrough;
737 case VIRTIO_BLK_Z_NONE:
738 case VIRTIO_BLK_Z_HA:
739 disk_set_zoned(vblk->disk, BLK_ZONED_NONE);
740 return;
741 case VIRTIO_BLK_Z_HM:
742 WARN_ON_ONCE(!vblk->zone_sectors);
743 if (!blk_revalidate_disk_zones(vblk->disk, NULL))
744 set_capacity_and_notify(vblk->disk, 0);
745 }
95bfec41
DF
746}
747
748static int virtblk_probe_zoned_device(struct virtio_device *vdev,
749 struct virtio_blk *vblk,
750 struct request_queue *q)
751{
f1ba4e67 752 u32 v, wg;
95bfec41 753 u8 model;
95bfec41
DF
754
755 virtio_cread(vdev, struct virtio_blk_config,
756 zoned.model, &model);
757
758 switch (model) {
759 case VIRTIO_BLK_Z_NONE:
f1ba4e67
DF
760 case VIRTIO_BLK_Z_HA:
761 /* Present the host-aware device as non-zoned */
95bfec41
DF
762 return 0;
763 case VIRTIO_BLK_Z_HM:
764 break;
95bfec41
DF
765 default:
766 dev_err(&vdev->dev, "unsupported zone model %d\n", model);
767 return -EINVAL;
768 }
769
770 dev_dbg(&vdev->dev, "probing host-managed zoned device\n");
771
772 disk_set_zoned(vblk->disk, BLK_ZONED_HM);
773 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
774
775 virtio_cread(vdev, struct virtio_blk_config,
776 zoned.max_open_zones, &v);
f1ba4e67
DF
777 disk_set_max_open_zones(vblk->disk, v);
778 dev_dbg(&vdev->dev, "max open zones = %u\n", v);
95bfec41
DF
779
780 virtio_cread(vdev, struct virtio_blk_config,
781 zoned.max_active_zones, &v);
f1ba4e67
DF
782 disk_set_max_active_zones(vblk->disk, v);
783 dev_dbg(&vdev->dev, "max active zones = %u\n", v);
95bfec41
DF
784
785 virtio_cread(vdev, struct virtio_blk_config,
f1ba4e67
DF
786 zoned.write_granularity, &wg);
787 if (!wg) {
95bfec41
DF
788 dev_warn(&vdev->dev, "zero write granularity reported\n");
789 return -ENODEV;
790 }
f1ba4e67
DF
791 blk_queue_physical_block_size(q, wg);
792 blk_queue_io_min(q, wg);
95bfec41 793
f1ba4e67 794 dev_dbg(&vdev->dev, "write granularity = %u\n", wg);
95bfec41
DF
795
796 /*
797 * virtio ZBD specification doesn't require zones to be a power of
798 * two sectors in size, but the code in this driver expects that.
799 */
f1ba4e67
DF
800 virtio_cread(vdev, struct virtio_blk_config, zoned.zone_sectors,
801 &vblk->zone_sectors);
95bfec41
DF
802 if (vblk->zone_sectors == 0 || !is_power_of_2(vblk->zone_sectors)) {
803 dev_err(&vdev->dev,
804 "zoned device with non power of two zone size %u\n",
805 vblk->zone_sectors);
806 return -ENODEV;
807 }
a3d96ed2 808 blk_queue_chunk_sectors(q, vblk->zone_sectors);
95bfec41
DF
809 dev_dbg(&vdev->dev, "zone sectors = %u\n", vblk->zone_sectors);
810
811 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
812 dev_warn(&vblk->vdev->dev,
813 "ignoring negotiated F_DISCARD for zoned device\n");
814 blk_queue_max_discard_sectors(q, 0);
815 }
816
a3d96ed2
DLM
817 virtio_cread(vdev, struct virtio_blk_config,
818 zoned.max_append_sectors, &v);
819 if (!v) {
820 dev_warn(&vdev->dev, "zero max_append_sectors reported\n");
821 return -ENODEV;
95bfec41 822 }
a3d96ed2
DLM
823 if ((v << SECTOR_SHIFT) < wg) {
824 dev_err(&vdev->dev,
825 "write granularity %u exceeds max_append_sectors %u limit\n",
826 wg, v);
827 return -ENODEV;
828 }
829 blk_queue_max_zone_append_sectors(q, v);
830 dev_dbg(&vdev->dev, "max append sectors = %u\n", v);
95bfec41 831
a3d96ed2 832 return blk_revalidate_disk_zones(vblk->disk, NULL);
95bfec41
DF
833}
834
95bfec41
DF
835#else
836
837/*
838 * Zoned block device support is not configured in this kernel.
10805eb5
DF
839 * Host-managed zoned devices can't be supported, but others are
840 * good to go as regular block devices.
95bfec41
DF
841 */
842#define virtblk_report_zones NULL
f1ba4e67 843
95bfec41
DF
844static inline void virtblk_revalidate_zones(struct virtio_blk *vblk)
845{
846}
f1ba4e67 847
95bfec41
DF
848static inline int virtblk_probe_zoned_device(struct virtio_device *vdev,
849 struct virtio_blk *vblk, struct request_queue *q)
850{
10805eb5 851 u8 model;
95bfec41 852
10805eb5
DF
853 virtio_cread(vdev, struct virtio_blk_config, zoned.model, &model);
854 if (model == VIRTIO_BLK_Z_HM) {
855 dev_err(&vdev->dev,
856 "virtio_blk: zoned devices are not supported");
857 return -EOPNOTSUPP;
858 }
859
860 return 0;
95bfec41
DF
861}
862#endif /* CONFIG_BLK_DEV_ZONED */
863
4cb2ea28 864/* return id (s/n) string for *disk to *id_str
865 */
866static int virtblk_get_id(struct gendisk *disk, char *id_str)
867{
868 struct virtio_blk *vblk = disk->private_data;
f9596695 869 struct request_queue *q = vblk->disk->queue;
4cb2ea28 870 struct request *req;
95bfec41 871 struct virtblk_req *vbr;
e4c4776d 872 int err;
4cb2ea28 873
0bf6d96c 874 req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
f9596695 875 if (IS_ERR(req))
4cb2ea28 876 return PTR_ERR(req);
f9596695 877
95bfec41 878 vbr = blk_mq_rq_to_pdu(req);
f1ba4e67 879 vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
95bfec41
DF
880 vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
881 vbr->out_hdr.sector = 0;
882
f9596695
CH
883 err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
884 if (err)
885 goto out;
886
b84ba30b 887 blk_execute_rq(req, false);
f1ba4e67 888 err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
f9596695 889out:
0bf6d96c 890 blk_mq_free_request(req);
e4c4776d 891 return err;
4cb2ea28 892}
893
135da0b0
CB
894/* We provide getgeo only to please some old bootloader/partitioning tools */
895static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
896{
48e4043d 897 struct virtio_blk *vblk = bd->bd_disk->private_data;
90b5feb8
SH
898 int ret = 0;
899
900 mutex_lock(&vblk->vdev_mutex);
901
902 if (!vblk->vdev) {
903 ret = -ENXIO;
904 goto out;
905 }
48e4043d
RH
906
907 /* see if the host passed in geometry config */
855e0c52
RR
908 if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
909 virtio_cread(vblk->vdev, struct virtio_blk_config,
910 geometry.cylinders, &geo->cylinders);
911 virtio_cread(vblk->vdev, struct virtio_blk_config,
912 geometry.heads, &geo->heads);
913 virtio_cread(vblk->vdev, struct virtio_blk_config,
914 geometry.sectors, &geo->sectors);
48e4043d
RH
915 } else {
916 /* some standard values, similar to sd */
917 geo->heads = 1 << 6;
918 geo->sectors = 1 << 5;
919 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
920 }
90b5feb8
SH
921out:
922 mutex_unlock(&vblk->vdev_mutex);
923 return ret;
135da0b0
CB
924}
925
24b45e6c
CH
926static void virtblk_free_disk(struct gendisk *disk)
927{
928 struct virtio_blk *vblk = disk->private_data;
929
92a34c46 930 ida_free(&vd_index_ida, vblk->index);
24b45e6c
CH
931 mutex_destroy(&vblk->vdev_mutex);
932 kfree(vblk);
933}
934
83d5cde4 935static const struct block_device_operations virtblk_fops = {
24b45e6c
CH
936 .owner = THIS_MODULE,
937 .getgeo = virtblk_getgeo,
938 .free_disk = virtblk_free_disk,
95bfec41 939 .report_zones = virtblk_report_zones,
e467cde2
RR
940};
941
d50ed907
CB
942static int index_to_minor(int index)
943{
944 return index << PART_BITS;
945}
946
5087a50e
MT
947static int minor_to_index(int minor)
948{
949 return minor >> PART_BITS;
950}
951
e982c4d0
HR
952static ssize_t serial_show(struct device *dev,
953 struct device_attribute *attr, char *buf)
a5eb9e4f
RH
954{
955 struct gendisk *disk = dev_to_disk(dev);
956 int err;
957
958 /* sysfs gives us a PAGE_SIZE buffer */
959 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
960
961 buf[VIRTIO_BLK_ID_BYTES] = '\0';
962 err = virtblk_get_id(disk, buf);
963 if (!err)
964 return strlen(buf);
965
966 if (err == -EIO) /* Unsupported? Make it empty. */
967 return 0;
968
969 return err;
970}
393c525b 971
e982c4d0 972static DEVICE_ATTR_RO(serial);
a5eb9e4f 973
daf2a501
SH
974/* The queue's logical block size must be set before calling this */
975static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
7a7c924c 976{
7a7c924c
CH
977 struct virtio_device *vdev = vblk->vdev;
978 struct request_queue *q = vblk->disk->queue;
979 char cap_str_2[10], cap_str_10[10];
1046d304 980 unsigned long long nblocks;
b9f28d86 981 u64 capacity;
7a7c924c
CH
982
983 /* Host must always specify the capacity. */
855e0c52 984 virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
7a7c924c 985
1046d304
SH
986 nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
987
988 string_get_size(nblocks, queue_logical_block_size(q),
b9f28d86 989 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
1046d304 990 string_get_size(nblocks, queue_logical_block_size(q),
b9f28d86 991 STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
7a7c924c
CH
992
993 dev_notice(&vdev->dev,
daf2a501
SH
994 "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
995 vblk->disk->disk_name,
996 resize ? "new size: " : "",
1046d304
SH
997 nblocks,
998 queue_logical_block_size(q),
999 cap_str_10,
1000 cap_str_2);
7a7c924c 1001
449f4ec9 1002 set_capacity_and_notify(vblk->disk, capacity);
daf2a501
SH
1003}
1004
1005static void virtblk_config_changed_work(struct work_struct *work)
1006{
1007 struct virtio_blk *vblk =
1008 container_of(work, struct virtio_blk, config_work);
daf2a501 1009
95bfec41 1010 virtblk_revalidate_zones(vblk);
daf2a501 1011 virtblk_update_capacity(vblk, true);
7a7c924c
CH
1012}
1013
1014static void virtblk_config_changed(struct virtio_device *vdev)
1015{
1016 struct virtio_blk *vblk = vdev->priv;
1017
1018 queue_work(virtblk_wq, &vblk->config_work);
1019}
1020
6abd6e5a
AS
1021static int init_vq(struct virtio_blk *vblk)
1022{
2ff98449 1023 int err;
6a27b656
ML
1024 int i;
1025 vq_callback_t **callbacks;
1026 const char **names;
1027 struct virtqueue **vqs;
1028 unsigned short num_vqs;
4e040052 1029 unsigned int num_poll_vqs;
6a27b656 1030 struct virtio_device *vdev = vblk->vdev;
ad71473d 1031 struct irq_affinity desc = { 0, };
6a27b656
ML
1032
1033 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
1034 struct virtio_blk_config, num_queues,
1035 &num_vqs);
1036 if (err)
1037 num_vqs = 1;
4e040052 1038
6ae6ff6f 1039 if (!err && !num_vqs) {
63b4ffa4 1040 dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
6ae6ff6f
JW
1041 return -EINVAL;
1042 }
6a27b656 1043
0989c41b
MG
1044 num_vqs = min_t(unsigned int,
1045 min_not_zero(num_request_queues, nr_cpu_ids),
1046 num_vqs);
bf348f9b 1047
4e040052
SK
1048 num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
1049
1050 vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
1051 vblk->io_queues[HCTX_TYPE_READ] = 0;
1052 vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
1053
1054 dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
1055 vblk->io_queues[HCTX_TYPE_DEFAULT],
1056 vblk->io_queues[HCTX_TYPE_READ],
1057 vblk->io_queues[HCTX_TYPE_POLL]);
1058
668866b6 1059 vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
347a5293
MH
1060 if (!vblk->vqs)
1061 return -ENOMEM;
6a27b656 1062
668866b6
ME
1063 names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
1064 callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
1065 vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
347a5293
MH
1066 if (!names || !callbacks || !vqs) {
1067 err = -ENOMEM;
1068 goto out;
1069 }
6abd6e5a 1070
4e040052 1071 for (i = 0; i < num_vqs - num_poll_vqs; i++) {
6a27b656
ML
1072 callbacks[i] = virtblk_done;
1073 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
1074 names[i] = vblk->vqs[i].name;
1075 }
1076
4e040052
SK
1077 for (; i < num_vqs; i++) {
1078 callbacks[i] = NULL;
1079 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
1080 names[i] = vblk->vqs[i].name;
1081 }
1082
6a27b656 1083 /* Discover virtqueues and write information to configuration. */
9b2bbdb2 1084 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
6a27b656 1085 if (err)
347a5293 1086 goto out;
6abd6e5a 1087
6a27b656
ML
1088 for (i = 0; i < num_vqs; i++) {
1089 spin_lock_init(&vblk->vqs[i].lock);
1090 vblk->vqs[i].vq = vqs[i];
1091 }
1092 vblk->num_vqs = num_vqs;
1093
347a5293 1094out:
6a27b656 1095 kfree(vqs);
6a27b656 1096 kfree(callbacks);
6a27b656 1097 kfree(names);
6a27b656
ML
1098 if (err)
1099 kfree(vblk->vqs);
6abd6e5a
AS
1100 return err;
1101}
1102
c0aa3e09
RM
1103/*
1104 * Legacy naming scheme used for virtio devices. We are stuck with it for
1105 * virtio blk but don't ever use it for any new driver.
1106 */
1107static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
1108{
1109 const int base = 'z' - 'a' + 1;
1110 char *begin = buf + strlen(prefix);
1111 char *end = buf + buflen;
1112 char *p;
1113 int unit;
1114
1115 p = end - 1;
1116 *p = '\0';
1117 unit = base;
1118 do {
1119 if (p == begin)
1120 return -EINVAL;
1121 *--p = 'a' + (index % unit);
1122 index = (index / unit) - 1;
1123 } while (index >= 0);
1124
1125 memmove(begin, p, end - p);
1126 memcpy(buf, prefix, strlen(prefix));
1127
1128 return 0;
1129}
1130
cd5d5038
PB
1131static int virtblk_get_cache_mode(struct virtio_device *vdev)
1132{
1133 u8 writeback;
1134 int err;
1135
855e0c52
RR
1136 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
1137 struct virtio_blk_config, wce,
1138 &writeback);
592002f5
MT
1139
1140 /*
1141 * If WCE is not configurable and flush is not available,
1142 * assume no writeback cache is in use.
1143 */
cd5d5038 1144 if (err)
592002f5 1145 writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
cd5d5038
PB
1146
1147 return writeback;
1148}
1149
1150static void virtblk_update_cache_mode(struct virtio_device *vdev)
1151{
1152 u8 writeback = virtblk_get_cache_mode(vdev);
1153 struct virtio_blk *vblk = vdev->priv;
1154
ad9126ac 1155 blk_queue_write_cache(vblk->disk->queue, writeback, false);
cd5d5038
PB
1156}
1157
1158static const char *const virtblk_cache_types[] = {
1159 "write through", "write back"
1160};
1161
1162static ssize_t
e982c4d0
HR
1163cache_type_store(struct device *dev, struct device_attribute *attr,
1164 const char *buf, size_t count)
cd5d5038
PB
1165{
1166 struct gendisk *disk = dev_to_disk(dev);
1167 struct virtio_blk *vblk = disk->private_data;
1168 struct virtio_device *vdev = vblk->vdev;
1169 int i;
cd5d5038
PB
1170
1171 BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
f53d5aa0 1172 i = sysfs_match_string(virtblk_cache_types, buf);
cd5d5038 1173 if (i < 0)
f53d5aa0 1174 return i;
cd5d5038 1175
855e0c52 1176 virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
cd5d5038
PB
1177 virtblk_update_cache_mode(vdev);
1178 return count;
1179}
1180
1181static ssize_t
e982c4d0 1182cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
cd5d5038
PB
1183{
1184 struct gendisk *disk = dev_to_disk(dev);
1185 struct virtio_blk *vblk = disk->private_data;
1186 u8 writeback = virtblk_get_cache_mode(vblk->vdev);
1187
1188 BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
f1aa12f5 1189 return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
cd5d5038
PB
1190}
1191
e982c4d0
HR
1192static DEVICE_ATTR_RW(cache_type);
1193
1194static struct attribute *virtblk_attrs[] = {
1195 &dev_attr_serial.attr,
1196 &dev_attr_cache_type.attr,
1197 NULL,
1198};
1199
1200static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
1201 struct attribute *a, int n)
1202{
4ce79063 1203 struct device *dev = kobj_to_dev(kobj);
e982c4d0
HR
1204 struct gendisk *disk = dev_to_disk(dev);
1205 struct virtio_blk *vblk = disk->private_data;
1206 struct virtio_device *vdev = vblk->vdev;
1207
1208 if (a == &dev_attr_cache_type.attr &&
1209 !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
1210 return S_IRUGO;
1211
1212 return a->mode;
1213}
1214
1215static const struct attribute_group virtblk_attr_group = {
1216 .attrs = virtblk_attrs,
1217 .is_visible = virtblk_attrs_are_visible,
1218};
1219
1220static const struct attribute_group *virtblk_attr_groups[] = {
1221 &virtblk_attr_group,
1222 NULL,
1223};
cd5d5038 1224
a4e1d0b7 1225static void virtblk_map_queues(struct blk_mq_tag_set *set)
ad71473d
CH
1226{
1227 struct virtio_blk *vblk = set->driver_data;
4e040052
SK
1228 int i, qoff;
1229
1230 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
1231 struct blk_mq_queue_map *map = &set->map[i];
1232
1233 map->nr_queues = vblk->io_queues[i];
1234 map->queue_offset = qoff;
1235 qoff += map->nr_queues;
1236
1237 if (map->nr_queues == 0)
1238 continue;
1239
1240 /*
1241 * Regular queues have interrupts and hence CPU affinity is
1242 * defined by the core virtio code, but polling queues have
1243 * no interrupts so we let the block layer assign CPU affinity.
1244 */
1245 if (i == HCTX_TYPE_POLL)
1246 blk_mq_map_queues(&set->map[i]);
1247 else
1248 blk_mq_virtio_map_queues(&set->map[i], vblk->vdev, 0);
1249 }
4e040052
SK
1250}
1251
afd384f0
MT
1252static void virtblk_complete_batch(struct io_comp_batch *iob)
1253{
1254 struct request *req;
1255
1256 rq_list_for_each(&iob->req_list, req) {
1257 virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
1258 virtblk_cleanup_cmd(req);
1259 }
1260 blk_mq_end_request_batch(iob);
1261}
1262
4e040052
SK
1263static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1264{
1265 struct virtio_blk *vblk = hctx->queue->queuedata;
8d12ec10 1266 struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
afd384f0 1267 struct virtblk_req *vbr;
4e040052 1268 unsigned long flags;
afd384f0 1269 unsigned int len;
4e040052
SK
1270 int found = 0;
1271
1272 spin_lock_irqsave(&vq->lock, flags);
afd384f0
MT
1273
1274 while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
1275 struct request *req = blk_mq_rq_from_pdu(vbr);
1276
1277 found++;
1278 if (!blk_mq_complete_request_remote(req) &&
1279 !blk_mq_add_to_batch(req, iob, virtblk_vbr_status(vbr),
1280 virtblk_complete_batch))
1281 virtblk_request_done(req);
1282 }
4e040052
SK
1283
1284 if (found)
1285 blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
1286
1287 spin_unlock_irqrestore(&vq->lock, flags);
1288
1289 return found;
1290}
1291
f363b089 1292static const struct blk_mq_ops virtio_mq_ops = {
1cf7e9c6 1293 .queue_rq = virtio_queue_rq,
0e9911fa 1294 .queue_rqs = virtio_queue_rqs,
944e7c87 1295 .commit_rqs = virtio_commit_rqs,
5124c285 1296 .complete = virtblk_request_done,
ad71473d 1297 .map_queues = virtblk_map_queues,
4e040052 1298 .poll = virtblk_poll,
1cf7e9c6
JA
1299};
1300
24d2f903
CH
1301static unsigned int virtblk_queue_depth;
1302module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
1cf7e9c6 1303
8d85fce7 1304static int virtblk_probe(struct virtio_device *vdev)
e467cde2
RR
1305{
1306 struct virtio_blk *vblk;
69740c8b 1307 struct request_queue *q;
5087a50e 1308 int err, index;
a98755c5 1309
fd1068e1 1310 u32 v, blk_size, max_size, sg_elems, opt_io_size;
e60d6407
AK
1311 u32 max_discard_segs = 0;
1312 u32 discard_granularity = 0;
69740c8b
CH
1313 u16 min_io_size;
1314 u8 physical_block_exp, alignment_offset;
d1e9aa9c 1315 unsigned int queue_depth;
e467cde2 1316
ff631988
MT
1317 if (!vdev->config->get) {
1318 dev_err(&vdev->dev, "%s failure: config access disabled\n",
1319 __func__);
1320 return -EINVAL;
1321 }
1322
92a34c46
PR
1323 err = ida_alloc_range(&vd_index_ida, 0,
1324 minor_to_index(1 << MINORBITS) - 1, GFP_KERNEL);
5087a50e
MT
1325 if (err < 0)
1326 goto out;
1327 index = err;
4f3bf19c 1328
0864b79a 1329 /* We need to know how many segments before we allocate. */
855e0c52
RR
1330 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
1331 struct virtio_blk_config, seg_max,
1332 &sg_elems);
a5b365a6
CH
1333
1334 /* We need at least one SG element, whatever they say. */
1335 if (err || !sg_elems)
0864b79a
RR
1336 sg_elems = 1;
1337
63947b34
SH
1338 /* Prevent integer overflows and honor max vq size */
1339 sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
1340
1cf7e9c6 1341 vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
e467cde2
RR
1342 if (!vblk) {
1343 err = -ENOMEM;
5087a50e 1344 goto out_free_index;
e467cde2
RR
1345 }
1346
90b5feb8
SH
1347 mutex_init(&vblk->vdev_mutex);
1348
e467cde2 1349 vblk->vdev = vdev;
a98755c5 1350
7a7c924c 1351 INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
e467cde2 1352
6abd6e5a
AS
1353 err = init_vq(vblk);
1354 if (err)
e467cde2 1355 goto out_free_vblk;
e467cde2 1356
fc4324b4 1357 /* Default queue sizing is to fill the ring. */
6105d1fe 1358 if (!virtblk_queue_depth) {
d1e9aa9c 1359 queue_depth = vblk->vqs[0].vq->num_free;
fc4324b4
RR
1360 /* ... but without indirect descs, we use 2 descs per req */
1361 if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
d1e9aa9c
JQ
1362 queue_depth /= 2;
1363 } else {
1364 queue_depth = virtblk_queue_depth;
fc4324b4 1365 }
24d2f903
CH
1366
1367 memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
1368 vblk->tag_set.ops = &virtio_mq_ops;
d1e9aa9c 1369 vblk->tag_set.queue_depth = queue_depth;
24d2f903
CH
1370 vblk->tag_set.numa_node = NUMA_NO_NODE;
1371 vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1372 vblk->tag_set.cmd_size =
1cf7e9c6 1373 sizeof(struct virtblk_req) +
02746e26 1374 sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
24d2f903 1375 vblk->tag_set.driver_data = vblk;
6a27b656 1376 vblk->tag_set.nr_hw_queues = vblk->num_vqs;
4e040052
SK
1377 vblk->tag_set.nr_maps = 1;
1378 if (vblk->io_queues[HCTX_TYPE_POLL])
1379 vblk->tag_set.nr_maps = 3;
1cf7e9c6 1380
24d2f903
CH
1381 err = blk_mq_alloc_tag_set(&vblk->tag_set);
1382 if (err)
89a5f065 1383 goto out_free_vq;
24d2f903 1384
89a5f065
CH
1385 vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, vblk);
1386 if (IS_ERR(vblk->disk)) {
1387 err = PTR_ERR(vblk->disk);
24d2f903 1388 goto out_free_tags;
e467cde2 1389 }
89a5f065 1390 q = vblk->disk->queue;
7d116b62 1391
c0aa3e09 1392 virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
d50ed907 1393
e467cde2 1394 vblk->disk->major = major;
d50ed907 1395 vblk->disk->first_minor = index_to_minor(index);
89a5f065 1396 vblk->disk->minors = 1 << PART_BITS;
e467cde2
RR
1397 vblk->disk->private_data = vblk;
1398 vblk->disk->fops = &virtblk_fops;
5087a50e 1399 vblk->index = index;
4f3bf19c 1400
02c42b7a 1401 /* configure queue flush support */
cd5d5038 1402 virtblk_update_cache_mode(vdev);
e467cde2 1403
3ef53609
CB
1404 /* If disk is read-only in the host, the guest should obey */
1405 if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
1406 set_disk_ro(vblk->disk, 1);
1407
0864b79a 1408 /* We can handle whatever the host told us to handle. */
e030759a 1409 blk_queue_max_segments(q, sg_elems);
0864b79a 1410
4b7f7e20 1411 /* No real sector limit. */
f4e468f7 1412 blk_queue_max_hw_sectors(q, UINT_MAX);
4b7f7e20 1413
fd1068e1
JR
1414 max_size = virtio_max_dma_size(vdev);
1415
a586d4f6
RR
1416 /* Host can optionally specify maximum segment size and number of
1417 * segments. */
855e0c52
RR
1418 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
1419 struct virtio_blk_config, size_max, &v);
e467cde2 1420 if (!err)
fd1068e1
JR
1421 max_size = min(max_size, v);
1422
1423 blk_queue_max_segment_size(q, max_size);
e467cde2 1424
066f4d82 1425 /* Host can optionally specify the block size of the device */
855e0c52
RR
1426 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
1427 struct virtio_blk_config, blk_size,
1428 &blk_size);
57a13a5b
XY
1429 if (!err) {
1430 err = blk_validate_block_size(blk_size);
1431 if (err) {
1432 dev_err(&vdev->dev,
1433 "virtio_blk: invalid block size: 0x%x\n",
1434 blk_size);
1435 goto out_cleanup_disk;
1436 }
1437
69740c8b 1438 blk_queue_logical_block_size(q, blk_size);
57a13a5b 1439 } else
69740c8b
CH
1440 blk_size = queue_logical_block_size(q);
1441
1442 /* Use topology information if available */
855e0c52
RR
1443 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1444 struct virtio_blk_config, physical_block_exp,
1445 &physical_block_exp);
69740c8b
CH
1446 if (!err && physical_block_exp)
1447 blk_queue_physical_block_size(q,
1448 blk_size * (1 << physical_block_exp));
1449
855e0c52
RR
1450 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1451 struct virtio_blk_config, alignment_offset,
1452 &alignment_offset);
69740c8b
CH
1453 if (!err && alignment_offset)
1454 blk_queue_alignment_offset(q, blk_size * alignment_offset);
1455
855e0c52
RR
1456 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1457 struct virtio_blk_config, min_io_size,
1458 &min_io_size);
69740c8b
CH
1459 if (!err && min_io_size)
1460 blk_queue_io_min(q, blk_size * min_io_size);
1461
855e0c52
RR
1462 err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
1463 struct virtio_blk_config, opt_io_size,
1464 &opt_io_size);
69740c8b
CH
1465 if (!err && opt_io_size)
1466 blk_queue_io_opt(q, blk_size * opt_io_size);
1467
1f23816b 1468 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
1f23816b 1469 virtio_cread(vdev, struct virtio_blk_config,
e60d6407 1470 discard_sector_alignment, &discard_granularity);
1f23816b
CL
1471
1472 virtio_cread(vdev, struct virtio_blk_config,
1473 max_discard_sectors, &v);
1474 blk_queue_max_discard_sectors(q, v ? v : UINT_MAX);
1475
1476 virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
e60d6407 1477 &max_discard_segs);
1f23816b
CL
1478 }
1479
1480 if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
1481 virtio_cread(vdev, struct virtio_blk_config,
1482 max_write_zeroes_sectors, &v);
1483 blk_queue_max_write_zeroes_sectors(q, v ? v : UINT_MAX);
1484 }
1485
e60d6407
AK
1486 /* The discard and secure erase limits are combined since the Linux
1487 * block layer uses the same limit for both commands.
1488 *
1489 * If both VIRTIO_BLK_F_SECURE_ERASE and VIRTIO_BLK_F_DISCARD features
1490 * are negotiated, we will use the minimum between the limits.
1491 *
1492 * discard sector alignment is set to the minimum between discard_sector_alignment
1493 * and secure_erase_sector_alignment.
1494 *
1495 * max discard sectors is set to the minimum between max_discard_seg and
1496 * max_secure_erase_seg.
1497 */
1498 if (virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
1499
1500 virtio_cread(vdev, struct virtio_blk_config,
1501 secure_erase_sector_alignment, &v);
1502
1503 /* secure_erase_sector_alignment should not be zero, the device should set a
1504 * valid number of sectors.
1505 */
1506 if (!v) {
1507 dev_err(&vdev->dev,
1508 "virtio_blk: secure_erase_sector_alignment can't be 0\n");
1509 err = -EINVAL;
1510 goto out_cleanup_disk;
1511 }
1512
1513 discard_granularity = min_not_zero(discard_granularity, v);
1514
1515 virtio_cread(vdev, struct virtio_blk_config,
1516 max_secure_erase_sectors, &v);
1517
1518 /* max_secure_erase_sectors should not be zero, the device should set a
1519 * valid number of sectors.
1520 */
1521 if (!v) {
1522 dev_err(&vdev->dev,
1523 "virtio_blk: max_secure_erase_sectors can't be 0\n");
1524 err = -EINVAL;
1525 goto out_cleanup_disk;
1526 }
1527
1528 blk_queue_max_secure_erase_sectors(q, v);
1529
1530 virtio_cread(vdev, struct virtio_blk_config,
1531 max_secure_erase_seg, &v);
1532
1533 /* max_secure_erase_seg should not be zero, the device should set a
1534 * valid number of segments
1535 */
1536 if (!v) {
1537 dev_err(&vdev->dev,
1538 "virtio_blk: max_secure_erase_seg can't be 0\n");
1539 err = -EINVAL;
1540 goto out_cleanup_disk;
1541 }
1542
1543 max_discard_segs = min_not_zero(max_discard_segs, v);
1544 }
1545
1546 if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD) ||
1547 virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
1548 /* max_discard_seg and discard_granularity will be 0 only
1549 * if max_discard_seg and discard_sector_alignment fields in the virtio
1550 * config are 0 and VIRTIO_BLK_F_SECURE_ERASE feature is not negotiated.
1551 * In this case, we use default values.
1552 */
1553 if (!max_discard_segs)
1554 max_discard_segs = sg_elems;
1555
1556 blk_queue_max_discard_segments(q,
1557 min(max_discard_segs, MAX_DISCARD_SEGMENTS));
1558
1559 if (discard_granularity)
1560 q->limits.discard_granularity = discard_granularity << SECTOR_SHIFT;
1561 else
1562 q->limits.discard_granularity = blk_size;
1563 }
1564
daf2a501 1565 virtblk_update_capacity(vblk, false);
7a11370e
MT
1566 virtio_device_ready(vdev);
1567
10805eb5
DF
1568 /*
1569 * All steps that follow use the VQs therefore they need to be
1570 * placed after the virtio_device_ready() call above.
1571 */
1572 if (virtio_has_feature(vdev, VIRTIO_BLK_F_ZONED)) {
95bfec41
DF
1573 err = virtblk_probe_zoned_device(vdev, vblk, q);
1574 if (err)
1575 goto out_cleanup_disk;
1576 }
1577
dbb301f9
LC
1578 err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
1579 if (err)
1580 goto out_cleanup_disk;
1581
e467cde2
RR
1582 return 0;
1583
dbb301f9 1584out_cleanup_disk:
8b9ab626 1585 put_disk(vblk->disk);
24d2f903
CH
1586out_free_tags:
1587 blk_mq_free_tag_set(&vblk->tag_set);
e467cde2 1588out_free_vq:
d2a7ddda 1589 vdev->config->del_vqs(vdev);
e7eea44e 1590 kfree(vblk->vqs);
e467cde2
RR
1591out_free_vblk:
1592 kfree(vblk);
5087a50e 1593out_free_index:
92a34c46 1594 ida_free(&vd_index_ida, index);
e467cde2
RR
1595out:
1596 return err;
1597}
1598
8d85fce7 1599static void virtblk_remove(struct virtio_device *vdev)
e467cde2
RR
1600{
1601 struct virtio_blk *vblk = vdev->priv;
e467cde2 1602
cc74f719
MT
1603 /* Make sure no work handler is accessing the device. */
1604 flush_work(&vblk->config_work);
7a7c924c 1605
02e2b124 1606 del_gendisk(vblk->disk);
24d2f903
CH
1607 blk_mq_free_tag_set(&vblk->tag_set);
1608
90b5feb8
SH
1609 mutex_lock(&vblk->vdev_mutex);
1610
6e5aa7ef 1611 /* Stop all the virtqueues. */
d9679d00 1612 virtio_reset_device(vdev);
6e5aa7ef 1613
90b5feb8
SH
1614 /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
1615 vblk->vdev = NULL;
1616
d2a7ddda 1617 vdev->config->del_vqs(vdev);
6a27b656 1618 kfree(vblk->vqs);
f4953fe6 1619
90b5feb8
SH
1620 mutex_unlock(&vblk->vdev_mutex);
1621
24b45e6c 1622 put_disk(vblk->disk);
e467cde2
RR
1623}
1624
89107000 1625#ifdef CONFIG_PM_SLEEP
f8fb5bc2
AS
1626static int virtblk_freeze(struct virtio_device *vdev)
1627{
1628 struct virtio_blk *vblk = vdev->priv;
1629
1630 /* Ensure we don't receive any more interrupts */
d9679d00 1631 virtio_reset_device(vdev);
f8fb5bc2 1632
cc74f719 1633 /* Make sure no work handler is accessing the device. */
f8fb5bc2
AS
1634 flush_work(&vblk->config_work);
1635
9b3e9905 1636 blk_mq_quiesce_queue(vblk->disk->queue);
f8fb5bc2
AS
1637
1638 vdev->config->del_vqs(vdev);
b71ba22e
XY
1639 kfree(vblk->vqs);
1640
f8fb5bc2
AS
1641 return 0;
1642}
1643
1644static int virtblk_restore(struct virtio_device *vdev)
1645{
1646 struct virtio_blk *vblk = vdev->priv;
1647 int ret;
1648
f8fb5bc2 1649 ret = init_vq(vdev->priv);
6d62c37f
MT
1650 if (ret)
1651 return ret;
1652
1653 virtio_device_ready(vdev);
1cf7e9c6 1654
9b3e9905 1655 blk_mq_unquiesce_queue(vblk->disk->queue);
6d62c37f 1656 return 0;
f8fb5bc2
AS
1657}
1658#endif
1659
47483e25 1660static const struct virtio_device_id id_table[] = {
e467cde2
RR
1661 { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
1662 { 0 },
1663};
1664
19c1c5a6 1665static unsigned int features_legacy[] = {
02c42b7a 1666 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
97b50a65 1667 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
592002f5 1668 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1f23816b 1669 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
e60d6407 1670 VIRTIO_BLK_F_SECURE_ERASE,
19c1c5a6
MT
1671}
1672;
1673static unsigned int features[] = {
1674 VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
1675 VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
592002f5 1676 VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
1f23816b 1677 VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
10805eb5 1678 VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED,
c45a6816
RR
1679};
1680
8d85fce7 1681static struct virtio_driver virtio_blk = {
19c1c5a6
MT
1682 .feature_table = features,
1683 .feature_table_size = ARRAY_SIZE(features),
1684 .feature_table_legacy = features_legacy,
1685 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
1686 .driver.name = KBUILD_MODNAME,
1687 .driver.owner = THIS_MODULE,
1688 .id_table = id_table,
1689 .probe = virtblk_probe,
1690 .remove = virtblk_remove,
1691 .config_changed = virtblk_config_changed,
89107000 1692#ifdef CONFIG_PM_SLEEP
19c1c5a6
MT
1693 .freeze = virtblk_freeze,
1694 .restore = virtblk_restore,
f8fb5bc2 1695#endif
e467cde2
RR
1696};
1697
bcfe9b6c 1698static int __init virtio_blk_init(void)
e467cde2 1699{
7a7c924c
CH
1700 int error;
1701
1702 virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
1703 if (!virtblk_wq)
1704 return -ENOMEM;
1705
4f3bf19c 1706 major = register_blkdev(0, "virtblk");
7a7c924c
CH
1707 if (major < 0) {
1708 error = major;
1709 goto out_destroy_workqueue;
1710 }
1711
1712 error = register_virtio_driver(&virtio_blk);
1713 if (error)
1714 goto out_unregister_blkdev;
1715 return 0;
1716
1717out_unregister_blkdev:
1718 unregister_blkdev(major, "virtblk");
1719out_destroy_workqueue:
1720 destroy_workqueue(virtblk_wq);
1721 return error;
e467cde2
RR
1722}
1723
bcfe9b6c 1724static void __exit virtio_blk_fini(void)
e467cde2
RR
1725{
1726 unregister_virtio_driver(&virtio_blk);
38f37b57 1727 unregister_blkdev(major, "virtblk");
7a7c924c 1728 destroy_workqueue(virtblk_wq);
e467cde2 1729}
bcfe9b6c
RD
1730module_init(virtio_blk_init);
1731module_exit(virtio_blk_fini);
e467cde2
RR
1732
1733MODULE_DEVICE_TABLE(virtio, id_table);
1734MODULE_DESCRIPTION("Virtio block driver");
1735MODULE_LICENSE("GPL");