| 1 | |
| 2 | |
| 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 |
|
| 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 |
|
| 68 | struct gendisk *disk;
|
| 69 |
|
| 70 |
|
| 71 | struct blk_mq_tag_set tag_set;
|
| 72 |
|
| 73 |
|
| 74 | struct work_struct config_work;
|
| 75 |
|
| 76 |
|
| 77 | int index;
|
| 78 |
|
| 79 |
|
| 80 | int num_vqs;
|
| 81 | int io_queues[HCTX_MAX_TYPES];
|
| 82 | struct virtio_blk_vq *vqs;
|
| 83 |
|
| 84 |
|
| 85 | unsigned int zone_sectors;
|
| 86 | };
|
| 87 |
|
| 88 | struct virtblk_req {
|
| 89 |
|
| 90 | struct virtio_blk_outhdr out_hdr;
|
| 91 |
|
| 92 |
|
| 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(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, num_out, num_in, 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(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_objs(*range, segments, 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(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(&req->special_vec, range, 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(req))
|
| 209 | sg_free_table_chained(&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(req))
|
| 219 | return 0;
|
| 220 |
|
| 221 | vbr->sg_table.sgl = vbr->sg;
|
| 222 | err = sg_alloc_table_chained(&vbr->sg_table,
|
| 223 | blk_rq_nr_phys_segments(req),
|
| 224 | vbr->sg_table.sgl,
|
| 225 | VIRTIO_BLK_INLINE_SG_CNT);
|
| 226 | if (unlikely(err))
|
| 227 | return -ENOMEM;
|
| 228 |
|
| 229 | return blk_rq_map_sg(req, 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(bvec_virt(&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(req_op(req)))
|
| 248 | return BLK_STS_NOTSUPP;
|
| 249 |
|
| 250 |
|
| 251 | vbr->out_hdr.ioprio = cpu_to_virtio32(vdev, 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(req);
|
| 257 | break;
|
| 258 | case REQ_OP_WRITE:
|
| 259 | type = VIRTIO_BLK_T_OUT;
|
| 260 | sector = blk_rq_pos(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(req);
|
| 278 | break;
|
| 279 | case REQ_OP_ZONE_CLOSE:
|
| 280 | type = VIRTIO_BLK_T_ZONE_CLOSE;
|
| 281 | sector = blk_rq_pos(req);
|
| 282 | break;
|
| 283 | case REQ_OP_ZONE_FINISH:
|
| 284 | type = VIRTIO_BLK_T_ZONE_FINISH;
|
| 285 | sector = blk_rq_pos(req);
|
| 286 | break;
|
| 287 | case REQ_OP_ZONE_APPEND:
|
| 288 | type = VIRTIO_BLK_T_ZONE_APPEND;
|
| 289 | sector = blk_rq_pos(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(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 |
|
| 311 | vbr->in_hdr_len = in_hdr_len;
|
| 312 | vbr->out_hdr.type = cpu_to_virtio32(vdev, type);
|
| 313 | vbr->out_hdr.sector = cpu_to_virtio64(vdev, 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(req);
|
| 337 | blk_status_t status = virtblk_result(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(vblk->vdev,
|
| 345 | vbr->in_hdr.zone_append.sector);
|
| 346 |
|
| 347 | blk_mq_end_request(req, 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(vblk->vqs[qid].vq, &len)) != NULL) {
|
| 363 | struct request *req = blk_mq_rq_from_pdu(vbr);
|
| 364 |
|
| 365 | if (likely(!blk_should_fake_timeout(req->q)))
|
| 366 | blk_mq_complete_request(req);
|
| 367 | req_done = true;
|
| 368 | }
|
| 369 | } while (!virtqueue_enable_cb(vq));
|
| 370 |
|
| 371 |
|
| 372 | if (req_done)
|
| 373 | blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
|
| 374 | spin_unlock_irqrestore(&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(&vq->lock);
|
| 384 | kick = virtqueue_kick_prepare(vq->vq);
|
| 385 | spin_unlock_irq(&vq->lock);
|
| 386 |
|
| 387 | if (kick)
|
| 388 | virtqueue_notify(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(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, -ENOMEM);
|
| 419 | vbr->sg_table.nents = num;
|
| 420 |
|
| 421 | blk_mq_start_request(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(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(vblk->vqs[qid].vq, vbr);
|
| 444 | if (err) {
|
| 445 | virtqueue_kick(vblk->vqs[qid].vq);
|
| 446 | |
| 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(&vblk->vqs[qid].lock, flags);
|
| 452 | virtblk_unmap_data(req, vbr);
|
| 453 | return virtblk_fail_to_queue(req, err);
|
| 454 | }
|
| 455 |
|
| 456 | if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
|
| 457 | notify = true;
|
| 458 | spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
|
| 459 |
|
| 460 | if (notify)
|
| 461 | virtqueue_notify(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(req);
|
| 469 |
|
| 470 | return virtblk_prep_rq(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(rqlist))) {
|
| 483 | struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
|
| 484 | int err;
|
| 485 |
|
| 486 | err = virtblk_add_req(vq->vq, vbr);
|
| 487 | if (err) {
|
| 488 | virtblk_unmap_data(req, vbr);
|
| 489 | virtblk_cleanup_cmd(req);
|
| 490 | blk_mq_requeue_request(req, true);
|
| 491 | }
|
| 492 | }
|
| 493 |
|
| 494 | kick = virtqueue_kick_prepare(vq->vq);
|
| 495 | spin_unlock_irqrestore(&vq->lock, flags);
|
| 496 |
|
| 497 | if (kick)
|
| 498 | virtqueue_notify(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(rqlist))) {
|
| 509 | struct virtio_blk_vq *this_vq = get_virtio_blk_vq(req->mq_hctx);
|
| 510 |
|
| 511 | if (vq && vq != this_vq)
|
| 512 | virtblk_add_req_batch(vq, &submit_list);
|
| 513 | vq = this_vq;
|
| 514 |
|
| 515 | if (virtblk_prep_rq_batch(req))
|
| 516 | rq_list_add_tail(&submit_list, req);
|
| 517 | else
|
| 518 | rq_list_add_tail(&requeue_list, req);
|
| 519 | }
|
| 520 |
|
| 521 | if (vq)
|
| 522 | virtblk_add_req_batch(vq, &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, REQ_OP_DRV_IN, 0);
|
| 566 | if (IS_ERR(req))
|
| 567 | return PTR_ERR(req);
|
| 568 |
|
| 569 | vbr = blk_mq_rq_to_pdu(req);
|
| 570 | vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
|
| 571 | vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_ZONE_REPORT);
|
| 572 | vbr->out_hdr.sector = cpu_to_virtio64(vblk->vdev, sector);
|
| 573 |
|
| 574 | err = blk_rq_map_kern(req, report_buf, report_len, GFP_KERNEL);
|
| 575 | if (err)
|
| 576 | goto out;
|
| 577 |
|
| 578 | blk_execute_rq(req, false);
|
| 579 | err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
|
| 580 | out:
|
| 581 | blk_mq_free_request(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(vblk->vdev, entry->z_start);
|
| 593 | if (zone.start + vblk->zone_sectors <= get_capacity(vblk->disk))
|
| 594 | zone.len = vblk->zone_sectors;
|
| 595 | else
|
| 596 | zone.len = get_capacity(vblk->disk) - zone.start;
|
| 597 | zone.capacity = virtio64_to_cpu(vblk->vdev, entry->z_cap);
|
| 598 | zone.wp = virtio64_to_cpu(vblk->vdev, 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(vblk->disk, &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);
|
| 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(vblk->disk)) {
|
| 683 | memset(report, 0, buflen);
|
| 684 |
|
| 685 | ret = virtblk_submit_zone_report(vblk, (char *)report,
|
| 686 | 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 | nz = min_t(u64, nz,
|
| 693 | (buflen - sizeof(*report)) / sizeof(report->zones[0]));
|
| 694 | if (!nz)
|
| 695 | break;
|
| 696 |
|
| 697 | for (i = 0; i < nz && zone_idx < nr_zones; i++) {
|
| 698 | ret = virtblk_parse_zone(vblk, &report->zones[i],
|
| 699 | zone_idx, args);
|
| 700 | if (ret)
|
| 701 | goto fail_report;
|
| 702 |
|
| 703 | sector = virtio64_to_cpu(vblk->vdev,
|
| 704 | report->zones[i].z_start) +
|
| 705 | vblk->zone_sectors;
|
| 706 | zone_idx++;
|
| 707 | }
|
| 708 | }
|
| 709 |
|
| 710 | if (zone_idx > 0)
|
| 711 | ret = zone_idx;
|
| 712 | else
|
| 713 | ret = -EINVAL;
|
| 714 | fail_report:
|
| 715 | mutex_unlock(&vblk->vdev_mutex);
|
| 716 | kvfree(report);
|
| 717 | return ret;
|
| 718 | }
|
| 719 |
|
| 720 | static int virtblk_read_zoned_limits(struct virtio_blk *vblk,
|
| 721 | struct queue_limits *lim)
|
| 722 | {
|
| 723 | struct virtio_device *vdev = vblk->vdev;
|
| 724 | u32 v, wg;
|
| 725 |
|
| 726 | dev_dbg(&vdev->dev, "probing host-managed zoned device\n");
|
| 727 |
|
| 728 | lim->features |= BLK_FEAT_ZONED;
|
| 729 |
|
| 730 | virtio_cread(vdev, struct virtio_blk_config,
|
| 731 | zoned.max_open_zones, &v);
|
| 732 | lim->max_open_zones = v;
|
| 733 | dev_dbg(&vdev->dev, "max open zones = %u\n", v);
|
| 734 |
|
| 735 | virtio_cread(vdev, struct virtio_blk_config,
|
| 736 | zoned.max_active_zones, &v);
|
| 737 | lim->max_active_zones = v;
|
| 738 | dev_dbg(&vdev->dev, "max active zones = %u\n", v);
|
| 739 |
|
| 740 | virtio_cread(vdev, struct virtio_blk_config,
|
| 741 | zoned.write_granularity, &wg);
|
| 742 | if (!wg) {
|
| 743 | dev_warn(&vdev->dev, "zero write granularity reported\n");
|
| 744 | return -ENODEV;
|
| 745 | }
|
| 746 | lim->zone_write_granularity = wg;
|
| 747 | lim->physical_block_size = wg;
|
| 748 | lim->io_min = wg;
|
| 749 |
|
| 750 | dev_dbg(&vdev->dev, "write granularity = %u\n", wg);
|
| 751 |
|
| 752 | |
| 753 | * virtio ZBD specification doesn't require zones to be a power of
|
| 754 | * two sectors in size, but the code in this driver expects that.
|
| 755 |
|
| 756 | virtio_cread(vdev, struct virtio_blk_config, zoned.zone_sectors,
|
| 757 | &vblk->zone_sectors);
|
| 758 | if (vblk->zone_sectors == 0 || !is_power_of_2(vblk->zone_sectors)) {
|
| 759 | dev_err(&vdev->dev,
|
| 760 | "zoned device with non power of two zone size %u\n",
|
| 761 | vblk->zone_sectors);
|
| 762 | return -ENODEV;
|
| 763 | }
|
| 764 | lim->chunk_sectors = vblk->zone_sectors;
|
| 765 | dev_dbg(&vdev->dev, "zone sectors = %u\n", vblk->zone_sectors);
|
| 766 |
|
| 767 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
|
| 768 | dev_warn(&vblk->vdev->dev,
|
| 769 | "ignoring negotiated F_DISCARD for zoned device\n");
|
| 770 | lim->max_hw_discard_sectors = 0;
|
| 771 | }
|
| 772 |
|
| 773 | virtio_cread(vdev, struct virtio_blk_config,
|
| 774 | zoned.max_append_sectors, &v);
|
| 775 | if (!v) {
|
| 776 | dev_warn(&vdev->dev, "zero max_append_sectors reported\n");
|
| 777 | return -ENODEV;
|
| 778 | }
|
| 779 | if ((v << SECTOR_SHIFT) < wg) {
|
| 780 | dev_err(&vdev->dev,
|
| 781 | "write granularity %u exceeds max_append_sectors %u limit\n",
|
| 782 | wg, v);
|
| 783 | return -ENODEV;
|
| 784 | }
|
| 785 | lim->max_hw_zone_append_sectors = v;
|
| 786 | dev_dbg(&vdev->dev, "max append sectors = %u\n", v);
|
| 787 |
|
| 788 | return 0;
|
| 789 | }
|
| 790 | #else
|
| 791 | |
| 792 | * Zoned block device support is not configured in this kernel, host-managed
|
| 793 | * zoned devices can't be supported.
|
| 794 |
|
| 795 | #define virtblk_report_zones NULL
|
| 796 | static inline int virtblk_read_zoned_limits(struct virtio_blk *vblk,
|
| 797 | struct queue_limits *lim)
|
| 798 | {
|
| 799 | dev_err(&vblk->vdev->dev,
|
| 800 | "virtio_blk: zoned devices are not supported");
|
| 801 | return -EOPNOTSUPP;
|
| 802 | }
|
| 803 | #endif
|
| 804 |
|
| 805 | |
| 806 |
|
| 807 | static int virtblk_get_id(struct gendisk *disk, char *id_str)
|
| 808 | {
|
| 809 | struct virtio_blk *vblk = disk->private_data;
|
| 810 | struct request_queue *q = vblk->disk->queue;
|
| 811 | struct request *req;
|
| 812 | struct virtblk_req *vbr;
|
| 813 | int err;
|
| 814 |
|
| 815 | req = blk_mq_alloc_request(q, REQ_OP_DRV_IN, 0);
|
| 816 | if (IS_ERR(req))
|
| 817 | return PTR_ERR(req);
|
| 818 |
|
| 819 | vbr = blk_mq_rq_to_pdu(req);
|
| 820 | vbr->in_hdr_len = sizeof(vbr->in_hdr.status);
|
| 821 | vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
|
| 822 | vbr->out_hdr.sector = 0;
|
| 823 |
|
| 824 | err = blk_rq_map_kern(req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
|
| 825 | if (err)
|
| 826 | goto out;
|
| 827 |
|
| 828 | blk_execute_rq(req, false);
|
| 829 | err = blk_status_to_errno(virtblk_result(vbr->in_hdr.status));
|
| 830 | out:
|
| 831 | blk_mq_free_request(req);
|
| 832 | return err;
|
| 833 | }
|
| 834 |
|
| 835 |
|
| 836 | static int virtblk_getgeo(struct gendisk *disk, struct hd_geometry *geo)
|
| 837 | {
|
| 838 | struct virtio_blk *vblk = disk->private_data;
|
| 839 | int ret = 0;
|
| 840 |
|
| 841 | mutex_lock(&vblk->vdev_mutex);
|
| 842 |
|
| 843 | if (!vblk->vdev) {
|
| 844 | ret = -ENXIO;
|
| 845 | goto out;
|
| 846 | }
|
| 847 |
|
| 848 |
|
| 849 | if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
|
| 850 | virtio_cread(vblk->vdev, struct virtio_blk_config,
|
| 851 | geometry.cylinders, &geo->cylinders);
|
| 852 | virtio_cread(vblk->vdev, struct virtio_blk_config,
|
| 853 | geometry.heads, &geo->heads);
|
| 854 | virtio_cread(vblk->vdev, struct virtio_blk_config,
|
| 855 | geometry.sectors, &geo->sectors);
|
| 856 | } else {
|
| 857 |
|
| 858 | geo->heads = 1 << 6;
|
| 859 | geo->sectors = 1 << 5;
|
| 860 | geo->cylinders = get_capacity(disk) >> 11;
|
| 861 | }
|
| 862 | out:
|
| 863 | mutex_unlock(&vblk->vdev_mutex);
|
| 864 | return ret;
|
| 865 | }
|
| 866 |
|
| 867 | static void virtblk_free_disk(struct gendisk *disk)
|
| 868 | {
|
| 869 | struct virtio_blk *vblk = disk->private_data;
|
| 870 |
|
| 871 | ida_free(&vd_index_ida, vblk->index);
|
| 872 | mutex_destroy(&vblk->vdev_mutex);
|
| 873 | kfree(vblk);
|
| 874 | }
|
| 875 |
|
| 876 | static const struct block_device_operations virtblk_fops = {
|
| 877 | .owner = THIS_MODULE,
|
| 878 | .getgeo = virtblk_getgeo,
|
| 879 | .free_disk = virtblk_free_disk,
|
| 880 | .report_zones = virtblk_report_zones,
|
| 881 | };
|
| 882 |
|
| 883 | static int index_to_minor(int index)
|
| 884 | {
|
| 885 | return index << PART_BITS;
|
| 886 | }
|
| 887 |
|
| 888 | static int minor_to_index(int minor)
|
| 889 | {
|
| 890 | return minor >> PART_BITS;
|
| 891 | }
|
| 892 |
|
| 893 | static ssize_t serial_show(struct device *dev,
|
| 894 | struct device_attribute *attr, char *buf)
|
| 895 | {
|
| 896 | struct gendisk *disk = dev_to_disk(dev);
|
| 897 | int err;
|
| 898 |
|
| 899 |
|
| 900 | BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
|
| 901 |
|
| 902 | buf[VIRTIO_BLK_ID_BYTES] = '\0';
|
| 903 | err = virtblk_get_id(disk, buf);
|
| 904 | if (!err)
|
| 905 | return strlen(buf);
|
| 906 |
|
| 907 | if (err == -EIO)
|
| 908 | return 0;
|
| 909 |
|
| 910 | return err;
|
| 911 | }
|
| 912 |
|
| 913 | static DEVICE_ATTR_RO(serial);
|
| 914 |
|
| 915 |
|
| 916 | static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
|
| 917 | {
|
| 918 | struct virtio_device *vdev = vblk->vdev;
|
| 919 | struct request_queue *q = vblk->disk->queue;
|
| 920 | char cap_str_2[10], cap_str_10[10];
|
| 921 | unsigned long long nblocks;
|
| 922 | u64 capacity;
|
| 923 |
|
| 924 |
|
| 925 | virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
|
| 926 |
|
| 927 | nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
|
| 928 |
|
| 929 | string_get_size(nblocks, queue_logical_block_size(q),
|
| 930 | STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
|
| 931 | string_get_size(nblocks, queue_logical_block_size(q),
|
| 932 | STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
|
| 933 |
|
| 934 | dev_notice(&vdev->dev,
|
| 935 | "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
|
| 936 | vblk->disk->disk_name,
|
| 937 | resize ? "new size: " : "",
|
| 938 | nblocks,
|
| 939 | queue_logical_block_size(q),
|
| 940 | cap_str_10,
|
| 941 | cap_str_2);
|
| 942 |
|
| 943 | set_capacity_and_notify(vblk->disk, capacity);
|
| 944 | }
|
| 945 |
|
| 946 | static void virtblk_config_changed_work(struct work_struct *work)
|
| 947 | {
|
| 948 | struct virtio_blk *vblk =
|
| 949 | container_of(work, struct virtio_blk, config_work);
|
| 950 |
|
| 951 | virtblk_update_capacity(vblk, true);
|
| 952 | }
|
| 953 |
|
| 954 | static void virtblk_config_changed(struct virtio_device *vdev)
|
| 955 | {
|
| 956 | struct virtio_blk *vblk = vdev->priv;
|
| 957 |
|
| 958 | queue_work(virtblk_wq, &vblk->config_work);
|
| 959 | }
|
| 960 |
|
| 961 | static int init_vq(struct virtio_blk *vblk)
|
| 962 | {
|
| 963 | int err;
|
| 964 | unsigned short i;
|
| 965 | struct virtqueue_info *vqs_info;
|
| 966 | struct virtqueue **vqs;
|
| 967 | unsigned short num_vqs;
|
| 968 | unsigned short num_poll_vqs;
|
| 969 | struct virtio_device *vdev = vblk->vdev;
|
| 970 | struct irq_affinity desc = { 0, };
|
| 971 |
|
| 972 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
|
| 973 | struct virtio_blk_config, num_queues,
|
| 974 | &num_vqs);
|
| 975 | if (err)
|
| 976 | num_vqs = 1;
|
| 977 |
|
| 978 | if (!err && !num_vqs) {
|
| 979 | dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
|
| 980 | return -EINVAL;
|
| 981 | }
|
| 982 |
|
| 983 | num_vqs = blk_mq_num_possible_queues(
|
| 984 | min_not_zero(num_request_queues, num_vqs));
|
| 985 |
|
| 986 | num_poll_vqs = min_t(unsigned int, poll_queues, num_vqs - 1);
|
| 987 |
|
| 988 | vblk->io_queues[HCTX_TYPE_DEFAULT] = num_vqs - num_poll_vqs;
|
| 989 | vblk->io_queues[HCTX_TYPE_READ] = 0;
|
| 990 | vblk->io_queues[HCTX_TYPE_POLL] = num_poll_vqs;
|
| 991 |
|
| 992 | dev_info(&vdev->dev, "%d/%d/%d default/read/poll queues\n",
|
| 993 | vblk->io_queues[HCTX_TYPE_DEFAULT],
|
| 994 | vblk->io_queues[HCTX_TYPE_READ],
|
| 995 | vblk->io_queues[HCTX_TYPE_POLL]);
|
| 996 |
|
| 997 | vblk->vqs = kmalloc_objs(*vblk->vqs, num_vqs);
|
| 998 | if (!vblk->vqs)
|
| 999 | return -ENOMEM;
|
| 1000 |
|
| 1001 | vqs_info = kzalloc_objs(*vqs_info, num_vqs);
|
| 1002 | vqs = kmalloc_objs(*vqs, num_vqs);
|
| 1003 | if (!vqs_info || !vqs) {
|
| 1004 | err = -ENOMEM;
|
| 1005 | goto out;
|
| 1006 | }
|
| 1007 |
|
| 1008 | for (i = 0; i < num_vqs - num_poll_vqs; i++) {
|
| 1009 | vqs_info[i].callback = virtblk_done;
|
| 1010 | snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%u", i);
|
| 1011 | vqs_info[i].name = vblk->vqs[i].name;
|
| 1012 | }
|
| 1013 |
|
| 1014 | for (; i < num_vqs; i++) {
|
| 1015 | snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%u", i);
|
| 1016 | vqs_info[i].name = vblk->vqs[i].name;
|
| 1017 | }
|
| 1018 |
|
| 1019 |
|
| 1020 | err = virtio_find_vqs(vdev, num_vqs, vqs, vqs_info, &desc);
|
| 1021 | if (err)
|
| 1022 | goto out;
|
| 1023 |
|
| 1024 | for (i = 0; i < num_vqs; i++) {
|
| 1025 | spin_lock_init(&vblk->vqs[i].lock);
|
| 1026 | vblk->vqs[i].vq = vqs[i];
|
| 1027 | }
|
| 1028 | vblk->num_vqs = num_vqs;
|
| 1029 |
|
| 1030 | out:
|
| 1031 | kfree(vqs);
|
| 1032 | kfree(vqs_info);
|
| 1033 | if (err) {
|
| 1034 | kfree(vblk->vqs);
|
| 1035 | |
| 1036 | * Set to NULL to prevent freeing vqs again during freezing.
|
| 1037 |
|
| 1038 | vblk->vqs = NULL;
|
| 1039 | }
|
| 1040 | return err;
|
| 1041 | }
|
| 1042 |
|
| 1043 | |
| 1044 | * Legacy naming scheme used for virtio devices. We are stuck with it for
|
| 1045 | * virtio blk but don't ever use it for any new driver.
|
| 1046 |
|
| 1047 | static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
|
| 1048 | {
|
| 1049 | const int base = 'z' - 'a' + 1;
|
| 1050 | char *begin = buf + strlen(prefix);
|
| 1051 | char *end = buf + buflen;
|
| 1052 | char *p;
|
| 1053 | int unit;
|
| 1054 |
|
| 1055 | p = end - 1;
|
| 1056 | *p = '\0';
|
| 1057 | unit = base;
|
| 1058 | do {
|
| 1059 | if (p == begin)
|
| 1060 | return -EINVAL;
|
| 1061 | *--p = 'a' + (index % unit);
|
| 1062 | index = (index / unit) - 1;
|
| 1063 | } while (index >= 0);
|
| 1064 |
|
| 1065 | memmove(begin, p, end - p);
|
| 1066 | memcpy(buf, prefix, strlen(prefix));
|
| 1067 |
|
| 1068 | return 0;
|
| 1069 | }
|
| 1070 |
|
| 1071 | static int virtblk_get_cache_mode(struct virtio_device *vdev)
|
| 1072 | {
|
| 1073 | u8 writeback;
|
| 1074 | int err;
|
| 1075 |
|
| 1076 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
|
| 1077 | struct virtio_blk_config, wce,
|
| 1078 | &writeback);
|
| 1079 |
|
| 1080 | |
| 1081 | * If WCE is not configurable and flush is not available,
|
| 1082 | * assume no writeback cache is in use.
|
| 1083 |
|
| 1084 | if (err)
|
| 1085 | writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
|
| 1086 |
|
| 1087 | return writeback;
|
| 1088 | }
|
| 1089 |
|
| 1090 | static const char *const virtblk_cache_types[] = {
|
| 1091 | "write through", "write back"
|
| 1092 | };
|
| 1093 |
|
| 1094 | static ssize_t
|
| 1095 | cache_type_store(struct device *dev, struct device_attribute *attr,
|
| 1096 | const char *buf, size_t count)
|
| 1097 | {
|
| 1098 | struct gendisk *disk = dev_to_disk(dev);
|
| 1099 | struct virtio_blk *vblk = disk->private_data;
|
| 1100 | struct virtio_device *vdev = vblk->vdev;
|
| 1101 | struct queue_limits lim;
|
| 1102 | int i;
|
| 1103 |
|
| 1104 | BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
|
| 1105 | i = sysfs_match_string(virtblk_cache_types, buf);
|
| 1106 | if (i < 0)
|
| 1107 | return i;
|
| 1108 |
|
| 1109 | virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
|
| 1110 |
|
| 1111 | lim = queue_limits_start_update(disk->queue);
|
| 1112 | if (virtblk_get_cache_mode(vdev))
|
| 1113 | lim.features |= BLK_FEAT_WRITE_CACHE;
|
| 1114 | else
|
| 1115 | lim.features &= ~BLK_FEAT_WRITE_CACHE;
|
| 1116 | i = queue_limits_commit_update_frozen(disk->queue, &lim);
|
| 1117 | if (i)
|
| 1118 | return i;
|
| 1119 | return count;
|
| 1120 | }
|
| 1121 |
|
| 1122 | static ssize_t
|
| 1123 | cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
|
| 1124 | {
|
| 1125 | struct gendisk *disk = dev_to_disk(dev);
|
| 1126 | struct virtio_blk *vblk = disk->private_data;
|
| 1127 | u8 writeback = virtblk_get_cache_mode(vblk->vdev);
|
| 1128 |
|
| 1129 | BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
|
| 1130 | return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
|
| 1131 | }
|
| 1132 |
|
| 1133 | static DEVICE_ATTR_RW(cache_type);
|
| 1134 |
|
| 1135 | static struct attribute *virtblk_attrs[] = {
|
| 1136 | &dev_attr_serial.attr,
|
| 1137 | &dev_attr_cache_type.attr,
|
| 1138 | NULL,
|
| 1139 | };
|
| 1140 |
|
| 1141 | static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
|
| 1142 | struct attribute *a, int n)
|
| 1143 | {
|
| 1144 | struct device *dev = kobj_to_dev(kobj);
|
| 1145 | struct gendisk *disk = dev_to_disk(dev);
|
| 1146 | struct virtio_blk *vblk = disk->private_data;
|
| 1147 | struct virtio_device *vdev = vblk->vdev;
|
| 1148 |
|
| 1149 | if (a == &dev_attr_cache_type.attr &&
|
| 1150 | !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
|
| 1151 | return S_IRUGO;
|
| 1152 |
|
| 1153 | return a->mode;
|
| 1154 | }
|
| 1155 |
|
| 1156 | static const struct attribute_group virtblk_attr_group = {
|
| 1157 | .attrs = virtblk_attrs,
|
| 1158 | .is_visible = virtblk_attrs_are_visible,
|
| 1159 | };
|
| 1160 |
|
| 1161 | static const struct attribute_group *virtblk_attr_groups[] = {
|
| 1162 | &virtblk_attr_group,
|
| 1163 | NULL,
|
| 1164 | };
|
| 1165 |
|
| 1166 | static void virtblk_map_queues(struct blk_mq_tag_set *set)
|
| 1167 | {
|
| 1168 | struct virtio_blk *vblk = set->driver_data;
|
| 1169 | int i, qoff;
|
| 1170 |
|
| 1171 | for (i = 0, qoff = 0; i < set->nr_maps; i++) {
|
| 1172 | struct blk_mq_queue_map *map = &set->map[i];
|
| 1173 |
|
| 1174 | map->nr_queues = vblk->io_queues[i];
|
| 1175 | map->queue_offset = qoff;
|
| 1176 | qoff += map->nr_queues;
|
| 1177 |
|
| 1178 | if (map->nr_queues == 0)
|
| 1179 | continue;
|
| 1180 |
|
| 1181 | |
| 1182 | * Regular queues have interrupts and hence CPU affinity is
|
| 1183 | * defined by the core virtio code, but polling queues have
|
| 1184 | * no interrupts so we let the block layer assign CPU affinity.
|
| 1185 |
|
| 1186 | if (i == HCTX_TYPE_POLL)
|
| 1187 | blk_mq_map_queues(&set->map[i]);
|
| 1188 | else
|
| 1189 | blk_mq_map_hw_queues(&set->map[i],
|
| 1190 | &vblk->vdev->dev, 0);
|
| 1191 | }
|
| 1192 | }
|
| 1193 |
|
| 1194 | static void virtblk_complete_batch(struct io_comp_batch *iob)
|
| 1195 | {
|
| 1196 | struct request *req;
|
| 1197 |
|
| 1198 | rq_list_for_each(&iob->req_list, req) {
|
| 1199 | virtblk_unmap_data(req, blk_mq_rq_to_pdu(req));
|
| 1200 | virtblk_cleanup_cmd(req);
|
| 1201 | }
|
| 1202 | blk_mq_end_request_batch(iob);
|
| 1203 | }
|
| 1204 |
|
| 1205 | static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
|
| 1206 | {
|
| 1207 | struct virtio_blk *vblk = hctx->queue->queuedata;
|
| 1208 | struct virtio_blk_vq *vq = get_virtio_blk_vq(hctx);
|
| 1209 | struct virtblk_req *vbr;
|
| 1210 | unsigned long flags;
|
| 1211 | unsigned int len;
|
| 1212 | int found = 0;
|
| 1213 |
|
| 1214 | spin_lock_irqsave(&vq->lock, flags);
|
| 1215 |
|
| 1216 | while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
|
| 1217 | struct request *req = blk_mq_rq_from_pdu(vbr);
|
| 1218 | u8 status = virtblk_vbr_status(vbr);
|
| 1219 |
|
| 1220 | found++;
|
| 1221 | if (!blk_mq_complete_request_remote(req) &&
|
| 1222 | !blk_mq_add_to_batch(req, iob, status != VIRTIO_BLK_S_OK,
|
| 1223 | virtblk_complete_batch))
|
| 1224 | virtblk_request_done(req);
|
| 1225 | }
|
| 1226 |
|
| 1227 | if (found)
|
| 1228 | blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
|
| 1229 |
|
| 1230 | spin_unlock_irqrestore(&vq->lock, flags);
|
| 1231 |
|
| 1232 | return found;
|
| 1233 | }
|
| 1234 |
|
| 1235 | static const struct blk_mq_ops virtio_mq_ops = {
|
| 1236 | .queue_rq = virtio_queue_rq,
|
| 1237 | .queue_rqs = virtio_queue_rqs,
|
| 1238 | .commit_rqs = virtio_commit_rqs,
|
| 1239 | .complete = virtblk_request_done,
|
| 1240 | .map_queues = virtblk_map_queues,
|
| 1241 | .poll = virtblk_poll,
|
| 1242 | };
|
| 1243 |
|
| 1244 | static unsigned int virtblk_queue_depth;
|
| 1245 | module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
|
| 1246 |
|
| 1247 | static int virtblk_read_limits(struct virtio_blk *vblk,
|
| 1248 | struct queue_limits *lim)
|
| 1249 | {
|
| 1250 | struct virtio_device *vdev = vblk->vdev;
|
| 1251 | u32 v, max_size, sg_elems, opt_io_size;
|
| 1252 | u32 max_discard_segs = 0;
|
| 1253 | u32 discard_granularity = 0;
|
| 1254 | u16 min_io_size;
|
| 1255 | u8 physical_block_exp, alignment_offset;
|
| 1256 | size_t max_dma_size;
|
| 1257 | int err;
|
| 1258 |
|
| 1259 |
|
| 1260 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
|
| 1261 | struct virtio_blk_config, seg_max,
|
| 1262 | &sg_elems);
|
| 1263 |
|
| 1264 |
|
| 1265 | if (err || !sg_elems)
|
| 1266 | sg_elems = 1;
|
| 1267 |
|
| 1268 |
|
| 1269 | sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
|
| 1270 |
|
| 1271 |
|
| 1272 | lim->max_segments = sg_elems;
|
| 1273 |
|
| 1274 |
|
| 1275 | lim->max_hw_sectors = UINT_MAX;
|
| 1276 |
|
| 1277 | max_dma_size = virtio_max_dma_size(vdev);
|
| 1278 | max_size = max_dma_size > U32_MAX ? U32_MAX : max_dma_size;
|
| 1279 |
|
| 1280 | |
| 1281 | * segments.
|
| 1282 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
|
| 1283 | struct virtio_blk_config, size_max, &v);
|
| 1284 | if (!err)
|
| 1285 | max_size = min(max_size, v);
|
| 1286 |
|
| 1287 | lim->max_segment_size = max_size;
|
| 1288 |
|
| 1289 |
|
| 1290 | virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
|
| 1291 | struct virtio_blk_config, blk_size,
|
| 1292 | &lim->logical_block_size);
|
| 1293 |
|
| 1294 |
|
| 1295 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
|
| 1296 | struct virtio_blk_config, physical_block_exp,
|
| 1297 | &physical_block_exp);
|
| 1298 | if (!err && physical_block_exp)
|
| 1299 | lim->physical_block_size =
|
| 1300 | lim->logical_block_size * (1 << physical_block_exp);
|
| 1301 |
|
| 1302 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
|
| 1303 | struct virtio_blk_config, alignment_offset,
|
| 1304 | &alignment_offset);
|
| 1305 | if (!err && alignment_offset)
|
| 1306 | lim->alignment_offset =
|
| 1307 | lim->logical_block_size * alignment_offset;
|
| 1308 |
|
| 1309 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
|
| 1310 | struct virtio_blk_config, min_io_size,
|
| 1311 | &min_io_size);
|
| 1312 | if (!err && min_io_size)
|
| 1313 | lim->io_min = lim->logical_block_size * min_io_size;
|
| 1314 |
|
| 1315 | err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
|
| 1316 | struct virtio_blk_config, opt_io_size,
|
| 1317 | &opt_io_size);
|
| 1318 | if (!err && opt_io_size)
|
| 1319 | lim->io_opt = lim->logical_block_size * opt_io_size;
|
| 1320 |
|
| 1321 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
|
| 1322 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1323 | discard_sector_alignment, &discard_granularity);
|
| 1324 |
|
| 1325 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1326 | max_discard_sectors, &v);
|
| 1327 | lim->max_hw_discard_sectors = v ? v : UINT_MAX;
|
| 1328 |
|
| 1329 | virtio_cread(vdev, struct virtio_blk_config, max_discard_seg,
|
| 1330 | &max_discard_segs);
|
| 1331 | }
|
| 1332 |
|
| 1333 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
|
| 1334 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1335 | max_write_zeroes_sectors, &v);
|
| 1336 | lim->max_write_zeroes_sectors = v ? v : UINT_MAX;
|
| 1337 | }
|
| 1338 |
|
| 1339 | |
| 1340 | * block layer uses the same limit for both commands.
|
| 1341 | *
|
| 1342 | * If both VIRTIO_BLK_F_SECURE_ERASE and VIRTIO_BLK_F_DISCARD features
|
| 1343 | * are negotiated, we will use the minimum between the limits.
|
| 1344 | *
|
| 1345 | * discard sector alignment is set to the minimum between discard_sector_alignment
|
| 1346 | * and secure_erase_sector_alignment.
|
| 1347 | *
|
| 1348 | * max discard sectors is set to the minimum between max_discard_seg and
|
| 1349 | * max_secure_erase_seg.
|
| 1350 |
|
| 1351 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
|
| 1352 |
|
| 1353 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1354 | secure_erase_sector_alignment, &v);
|
| 1355 |
|
| 1356 | |
| 1357 | * valid number of sectors.
|
| 1358 |
|
| 1359 | if (!v) {
|
| 1360 | dev_err(&vdev->dev,
|
| 1361 | "virtio_blk: secure_erase_sector_alignment can't be 0\n");
|
| 1362 | return -EINVAL;
|
| 1363 | }
|
| 1364 |
|
| 1365 | discard_granularity = min_not_zero(discard_granularity, v);
|
| 1366 |
|
| 1367 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1368 | max_secure_erase_sectors, &v);
|
| 1369 |
|
| 1370 | |
| 1371 | * valid number of sectors.
|
| 1372 |
|
| 1373 | if (!v) {
|
| 1374 | dev_err(&vdev->dev,
|
| 1375 | "virtio_blk: max_secure_erase_sectors can't be 0\n");
|
| 1376 | return -EINVAL;
|
| 1377 | }
|
| 1378 |
|
| 1379 | lim->max_secure_erase_sectors = v;
|
| 1380 |
|
| 1381 | virtio_cread(vdev, struct virtio_blk_config,
|
| 1382 | max_secure_erase_seg, &v);
|
| 1383 |
|
| 1384 | |
| 1385 | * valid number of segments
|
| 1386 |
|
| 1387 | if (!v) {
|
| 1388 | dev_err(&vdev->dev,
|
| 1389 | "virtio_blk: max_secure_erase_seg can't be 0\n");
|
| 1390 | return -EINVAL;
|
| 1391 | }
|
| 1392 |
|
| 1393 | max_discard_segs = min_not_zero(max_discard_segs, v);
|
| 1394 | }
|
| 1395 |
|
| 1396 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD) ||
|
| 1397 | virtio_has_feature(vdev, VIRTIO_BLK_F_SECURE_ERASE)) {
|
| 1398 | |
| 1399 | * if max_discard_seg and discard_sector_alignment fields in the virtio
|
| 1400 | * config are 0 and VIRTIO_BLK_F_SECURE_ERASE feature is not negotiated.
|
| 1401 | * In this case, we use default values.
|
| 1402 |
|
| 1403 | if (!max_discard_segs)
|
| 1404 | max_discard_segs = sg_elems;
|
| 1405 |
|
| 1406 | lim->max_discard_segments =
|
| 1407 | min(max_discard_segs, MAX_DISCARD_SEGMENTS);
|
| 1408 |
|
| 1409 | if (discard_granularity)
|
| 1410 | lim->discard_granularity =
|
| 1411 | discard_granularity << SECTOR_SHIFT;
|
| 1412 | else
|
| 1413 | lim->discard_granularity = lim->logical_block_size;
|
| 1414 | }
|
| 1415 |
|
| 1416 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_ZONED)) {
|
| 1417 | u8 model;
|
| 1418 |
|
| 1419 | virtio_cread(vdev, struct virtio_blk_config, zoned.model, &model);
|
| 1420 | switch (model) {
|
| 1421 | case VIRTIO_BLK_Z_NONE:
|
| 1422 | case VIRTIO_BLK_Z_HA:
|
| 1423 |
|
| 1424 | return 0;
|
| 1425 | case VIRTIO_BLK_Z_HM:
|
| 1426 | err = virtblk_read_zoned_limits(vblk, lim);
|
| 1427 | if (err)
|
| 1428 | return err;
|
| 1429 | break;
|
| 1430 | default:
|
| 1431 | dev_err(&vdev->dev, "unsupported zone model %d\n", model);
|
| 1432 | return -EINVAL;
|
| 1433 | }
|
| 1434 | }
|
| 1435 |
|
| 1436 | return 0;
|
| 1437 | }
|
| 1438 |
|
| 1439 | static int virtblk_probe(struct virtio_device *vdev)
|
| 1440 | {
|
| 1441 | struct virtio_blk *vblk;
|
| 1442 | struct queue_limits lim = {
|
| 1443 | .features = BLK_FEAT_ROTATIONAL,
|
| 1444 | .logical_block_size = SECTOR_SIZE,
|
| 1445 | };
|
| 1446 | int err, index;
|
| 1447 | unsigned int queue_depth;
|
| 1448 |
|
| 1449 | if (!vdev->config->get) {
|
| 1450 | dev_err(&vdev->dev, "%s failure: config access disabled\n",
|
| 1451 | __func__);
|
| 1452 | return -EINVAL;
|
| 1453 | }
|
| 1454 |
|
| 1455 | err = ida_alloc_range(&vd_index_ida, 0,
|
| 1456 | minor_to_index(1 << MINORBITS) - 1, GFP_KERNEL);
|
| 1457 | if (err < 0)
|
| 1458 | goto out;
|
| 1459 | index = err;
|
| 1460 |
|
| 1461 | vdev->priv = vblk = kmalloc_obj(*vblk);
|
| 1462 | if (!vblk) {
|
| 1463 | err = -ENOMEM;
|
| 1464 | goto out_free_index;
|
| 1465 | }
|
| 1466 |
|
| 1467 | mutex_init(&vblk->vdev_mutex);
|
| 1468 |
|
| 1469 | vblk->vdev = vdev;
|
| 1470 |
|
| 1471 | INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
|
| 1472 |
|
| 1473 | err = init_vq(vblk);
|
| 1474 | if (err)
|
| 1475 | goto out_free_vblk;
|
| 1476 |
|
| 1477 |
|
| 1478 | if (!virtblk_queue_depth) {
|
| 1479 | queue_depth = vblk->vqs[0].vq->num_free;
|
| 1480 |
|
| 1481 | if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
|
| 1482 | queue_depth /= 2;
|
| 1483 | } else {
|
| 1484 | queue_depth = virtblk_queue_depth;
|
| 1485 | }
|
| 1486 |
|
| 1487 | memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
|
| 1488 | vblk->tag_set.ops = &virtio_mq_ops;
|
| 1489 | vblk->tag_set.queue_depth = queue_depth;
|
| 1490 | vblk->tag_set.numa_node = NUMA_NO_NODE;
|
| 1491 | vblk->tag_set.cmd_size =
|
| 1492 | sizeof(struct virtblk_req) +
|
| 1493 | sizeof(struct scatterlist) * VIRTIO_BLK_INLINE_SG_CNT;
|
| 1494 | vblk->tag_set.driver_data = vblk;
|
| 1495 | vblk->tag_set.nr_hw_queues = vblk->num_vqs;
|
| 1496 | vblk->tag_set.nr_maps = 1;
|
| 1497 | if (vblk->io_queues[HCTX_TYPE_POLL])
|
| 1498 | vblk->tag_set.nr_maps = 3;
|
| 1499 |
|
| 1500 | err = blk_mq_alloc_tag_set(&vblk->tag_set);
|
| 1501 | if (err)
|
| 1502 | goto out_free_vq;
|
| 1503 |
|
| 1504 | err = virtblk_read_limits(vblk, &lim);
|
| 1505 | if (err)
|
| 1506 | goto out_free_tags;
|
| 1507 |
|
| 1508 | if (virtblk_get_cache_mode(vdev))
|
| 1509 | lim.features |= BLK_FEAT_WRITE_CACHE;
|
| 1510 |
|
| 1511 | vblk->disk = blk_mq_alloc_disk(&vblk->tag_set, &lim, vblk);
|
| 1512 | if (IS_ERR(vblk->disk)) {
|
| 1513 | err = PTR_ERR(vblk->disk);
|
| 1514 | goto out_free_tags;
|
| 1515 | }
|
| 1516 |
|
| 1517 | virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
|
| 1518 |
|
| 1519 | vblk->disk->major = major;
|
| 1520 | vblk->disk->first_minor = index_to_minor(index);
|
| 1521 | vblk->disk->minors = 1 << PART_BITS;
|
| 1522 | vblk->disk->private_data = vblk;
|
| 1523 | vblk->disk->fops = &virtblk_fops;
|
| 1524 | vblk->index = index;
|
| 1525 |
|
| 1526 |
|
| 1527 | if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
|
| 1528 | set_disk_ro(vblk->disk, 1);
|
| 1529 |
|
| 1530 | virtblk_update_capacity(vblk, false);
|
| 1531 | virtio_device_ready(vdev);
|
| 1532 |
|
| 1533 | |
| 1534 | * All steps that follow use the VQs therefore they need to be
|
| 1535 | * placed after the virtio_device_ready() call above.
|
| 1536 |
|
| 1537 | if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
|
| 1538 | (lim.features & BLK_FEAT_ZONED)) {
|
| 1539 | err = blk_revalidate_disk_zones(vblk->disk);
|
| 1540 | if (err)
|
| 1541 | goto out_cleanup_disk;
|
| 1542 | }
|
| 1543 |
|
| 1544 | err = device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
|
| 1545 | if (err)
|
| 1546 | goto out_cleanup_disk;
|
| 1547 |
|
| 1548 | return 0;
|
| 1549 |
|
| 1550 | out_cleanup_disk:
|
| 1551 | put_disk(vblk->disk);
|
| 1552 | out_free_tags:
|
| 1553 | blk_mq_free_tag_set(&vblk->tag_set);
|
| 1554 | out_free_vq:
|
| 1555 | vdev->config->del_vqs(vdev);
|
| 1556 | kfree(vblk->vqs);
|
| 1557 | out_free_vblk:
|
| 1558 | kfree(vblk);
|
| 1559 | out_free_index:
|
| 1560 | ida_free(&vd_index_ida, index);
|
| 1561 | out:
|
| 1562 | return err;
|
| 1563 | }
|
| 1564 |
|
| 1565 | static void virtblk_remove(struct virtio_device *vdev)
|
| 1566 | {
|
| 1567 | struct virtio_blk *vblk = vdev->priv;
|
| 1568 |
|
| 1569 |
|
| 1570 | flush_work(&vblk->config_work);
|
| 1571 |
|
| 1572 | del_gendisk(vblk->disk);
|
| 1573 | blk_mq_free_tag_set(&vblk->tag_set);
|
| 1574 |
|
| 1575 | mutex_lock(&vblk->vdev_mutex);
|
| 1576 |
|
| 1577 |
|
| 1578 | virtio_reset_device(vdev);
|
| 1579 |
|
| 1580 |
|
| 1581 | vblk->vdev = NULL;
|
| 1582 |
|
| 1583 | vdev->config->del_vqs(vdev);
|
| 1584 | kfree(vblk->vqs);
|
| 1585 |
|
| 1586 | mutex_unlock(&vblk->vdev_mutex);
|
| 1587 |
|
| 1588 | put_disk(vblk->disk);
|
| 1589 | }
|
| 1590 |
|
| 1591 | static int virtblk_freeze_priv(struct virtio_device *vdev)
|
| 1592 | {
|
| 1593 | struct virtio_blk *vblk = vdev->priv;
|
| 1594 | struct request_queue *q = vblk->disk->queue;
|
| 1595 | unsigned int memflags;
|
| 1596 |
|
| 1597 |
|
| 1598 | memflags = blk_mq_freeze_queue(q);
|
| 1599 | blk_mq_quiesce_queue_nowait(q);
|
| 1600 | blk_mq_unfreeze_queue(q, memflags);
|
| 1601 |
|
| 1602 |
|
| 1603 | virtio_reset_device(vdev);
|
| 1604 |
|
| 1605 |
|
| 1606 | flush_work(&vblk->config_work);
|
| 1607 |
|
| 1608 | vdev->config->del_vqs(vdev);
|
| 1609 | kfree(vblk->vqs);
|
| 1610 | |
| 1611 | * Set to NULL to prevent freeing vqs again after a failed vqs
|
| 1612 | * allocation during resume. Note that kfree() already handles NULL
|
| 1613 | * pointers safely.
|
| 1614 |
|
| 1615 | vblk->vqs = NULL;
|
| 1616 |
|
| 1617 | return 0;
|
| 1618 | }
|
| 1619 |
|
| 1620 | static int virtblk_restore_priv(struct virtio_device *vdev)
|
| 1621 | {
|
| 1622 | struct virtio_blk *vblk = vdev->priv;
|
| 1623 | int ret;
|
| 1624 |
|
| 1625 | ret = init_vq(vdev->priv);
|
| 1626 | if (ret)
|
| 1627 | return ret;
|
| 1628 |
|
| 1629 | virtio_device_ready(vdev);
|
| 1630 | blk_mq_unquiesce_queue(vblk->disk->queue);
|
| 1631 |
|
| 1632 | return 0;
|
| 1633 | }
|
| 1634 |
|
| 1635 | #ifdef CONFIG_PM_SLEEP
|
| 1636 | static int virtblk_freeze(struct virtio_device *vdev)
|
| 1637 | {
|
| 1638 | return virtblk_freeze_priv(vdev);
|
| 1639 | }
|
| 1640 |
|
| 1641 | static int virtblk_restore(struct virtio_device *vdev)
|
| 1642 | {
|
| 1643 | return virtblk_restore_priv(vdev);
|
| 1644 | }
|
| 1645 | #endif
|
| 1646 |
|
| 1647 | static int virtblk_reset_prepare(struct virtio_device *vdev)
|
| 1648 | {
|
| 1649 | return virtblk_freeze_priv(vdev);
|
| 1650 | }
|
| 1651 |
|
| 1652 | static int virtblk_reset_done(struct virtio_device *vdev)
|
| 1653 | {
|
| 1654 | return virtblk_restore_priv(vdev);
|
| 1655 | }
|
| 1656 |
|
| 1657 | static const struct virtio_device_id id_table[] = {
|
| 1658 | { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
|
| 1659 | { 0 },
|
| 1660 | };
|
| 1661 |
|
| 1662 | static unsigned int features_legacy[] = {
|
| 1663 | VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
|
| 1664 | VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
|
| 1665 | VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
|
| 1666 | VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
|
| 1667 | VIRTIO_BLK_F_SECURE_ERASE,
|
| 1668 | }
|
| 1669 | ;
|
| 1670 | static unsigned int features[] = {
|
| 1671 | VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
|
| 1672 | VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
|
| 1673 | VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
|
| 1674 | VIRTIO_BLK_F_MQ, VIRTIO_BLK_F_DISCARD, VIRTIO_BLK_F_WRITE_ZEROES,
|
| 1675 | VIRTIO_BLK_F_SECURE_ERASE, VIRTIO_BLK_F_ZONED,
|
| 1676 | };
|
| 1677 |
|
| 1678 | static struct virtio_driver virtio_blk = {
|
| 1679 | .feature_table = features,
|
| 1680 | .feature_table_size = ARRAY_SIZE(features),
|
| 1681 | .feature_table_legacy = features_legacy,
|
| 1682 | .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
|
| 1683 | .driver.name = KBUILD_MODNAME,
|
| 1684 | .id_table = id_table,
|
| 1685 | .probe = virtblk_probe,
|
| 1686 | .remove = virtblk_remove,
|
| 1687 | .config_changed = virtblk_config_changed,
|
| 1688 | #ifdef CONFIG_PM_SLEEP
|
| 1689 | .freeze = virtblk_freeze,
|
| 1690 | .restore = virtblk_restore,
|
| 1691 | #endif
|
| 1692 | .reset_prepare = virtblk_reset_prepare,
|
| 1693 | .reset_done = virtblk_reset_done,
|
| 1694 | };
|
| 1695 |
|
| 1696 | static int __init virtio_blk_init(void)
|
| 1697 | {
|
| 1698 | int error;
|
| 1699 |
|
| 1700 | virtblk_wq = alloc_workqueue("virtio-blk", WQ_PERCPU, 0);
|
| 1701 | if (!virtblk_wq)
|
| 1702 | return -ENOMEM;
|
| 1703 |
|
| 1704 | major = register_blkdev(0, "virtblk");
|
| 1705 | if (major < 0) {
|
| 1706 | error = major;
|
| 1707 | goto out_destroy_workqueue;
|
| 1708 | }
|
| 1709 |
|
| 1710 | error = register_virtio_driver(&virtio_blk);
|
| 1711 | if (error)
|
| 1712 | goto out_unregister_blkdev;
|
| 1713 | return 0;
|
| 1714 |
|
| 1715 | out_unregister_blkdev:
|
| 1716 | unregister_blkdev(major, "virtblk");
|
| 1717 | out_destroy_workqueue:
|
| 1718 | destroy_workqueue(virtblk_wq);
|
| 1719 | return error;
|
| 1720 | }
|
| 1721 |
|
| 1722 | static void __exit virtio_blk_fini(void)
|
| 1723 | {
|
| 1724 | unregister_virtio_driver(&virtio_blk);
|
| 1725 | unregister_blkdev(major, "virtblk");
|
| 1726 | destroy_workqueue(virtblk_wq);
|
| 1727 | }
|
| 1728 | module_init(virtio_blk_init);
|
| 1729 | module_exit(virtio_blk_fini);
|
| 1730 |
|
| 1731 | MODULE_DEVICE_TABLE(virtio, id_table);
|
| 1732 | MODULE_DESCRIPTION("Virtio block driver");
|
| 1733 | MODULE_LICENSE("GPL");
|