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