1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef BLK_MQ_H 3 #define BLK_MQ_H 4 5 #include <linux/blkdev.h> 6 #include <linux/sbitmap.h> 7 #include <linux/lockdep.h> 8 #include <linux/scatterlist.h> 9 #include <linux/prefetch.h> 10 #include <linux/srcu.h> 11 12 struct blk_mq_tags; 13 struct blk_flush_queue; 14 15 #define BLKDEV_MIN_RQ 4 16 #define BLKDEV_DEFAULT_RQ 128 17 18 enum rq_end_io_ret { 19 RQ_END_IO_NONE, 20 RQ_END_IO_FREE, 21 }; 22 23 typedef enum rq_end_io_ret (rq_end_io_fn)(struct request *, blk_status_t); 24 25 /* 26 * request flags */ 27 typedef __u32 __bitwise req_flags_t; 28 29 /* drive already may have started this one */ 30 #define RQF_STARTED ((__force req_flags_t)(1 << 1)) 31 /* request for flush sequence */ 32 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4)) 33 /* merge of different types, fail separately */ 34 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5)) 35 /* don't call prep for this one */ 36 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7)) 37 /* use hctx->sched_tags */ 38 #define RQF_SCHED_TAGS ((__force req_flags_t)(1 << 8)) 39 /* use an I/O scheduler for this request */ 40 #define RQF_USE_SCHED ((__force req_flags_t)(1 << 9)) 41 /* vaguely specified driver internal error. Ignored by the block layer */ 42 #define RQF_FAILED ((__force req_flags_t)(1 << 10)) 43 /* don't warn about errors */ 44 #define RQF_QUIET ((__force req_flags_t)(1 << 11)) 45 /* account into disk and partition IO statistics */ 46 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13)) 47 /* runtime pm request */ 48 #define RQF_PM ((__force req_flags_t)(1 << 15)) 49 /* on IO scheduler merge hash */ 50 #define RQF_HASHED ((__force req_flags_t)(1 << 16)) 51 /* track IO completion time */ 52 #define RQF_STATS ((__force req_flags_t)(1 << 17)) 53 /* Look at ->special_vec for the actual data payload instead of the 54 bio chain. */ 55 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18)) 56 /* The per-zone write lock is held for this request */ 57 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19)) 58 /* ->timeout has been called, don't expire again */ 59 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21)) 60 #define RQF_RESV ((__force req_flags_t)(1 << 23)) 61 62 /* flags that prevent us from merging requests: */ 63 #define RQF_NOMERGE_FLAGS \ 64 (RQF_STARTED | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD) 65 66 enum mq_rq_state { 67 MQ_RQ_IDLE = 0, 68 MQ_RQ_IN_FLIGHT = 1, 69 MQ_RQ_COMPLETE = 2, 70 }; 71 72 /* 73 * Try to put the fields that are referenced together in the same cacheline. 74 * 75 * If you modify this structure, make sure to update blk_rq_init() and 76 * especially blk_mq_rq_ctx_init() to take care of the added fields. 77 */ 78 struct request { 79 struct request_queue *q; 80 struct blk_mq_ctx *mq_ctx; 81 struct blk_mq_hw_ctx *mq_hctx; 82 83 blk_opf_t cmd_flags; /* op and common flags */ 84 req_flags_t rq_flags; 85 86 int tag; 87 int internal_tag; 88 89 unsigned int timeout; 90 91 /* the following two fields are internal, NEVER access directly */ 92 unsigned int __data_len; /* total data len */ 93 sector_t __sector; /* sector cursor */ 94 95 struct bio *bio; 96 struct bio *biotail; 97 98 union { 99 struct list_head queuelist; 100 struct request *rq_next; 101 }; 102 103 struct block_device *part; 104 #ifdef CONFIG_BLK_RQ_ALLOC_TIME 105 /* Time that the first bio started allocating this request. */ 106 u64 alloc_time_ns; 107 #endif 108 /* Time that this request was allocated for this IO. */ 109 u64 start_time_ns; 110 /* Time that I/O was submitted to the device. */ 111 u64 io_start_time_ns; 112 113 #ifdef CONFIG_BLK_WBT 114 unsigned short wbt_flags; 115 #endif 116 /* 117 * rq sectors used for blk stats. It has the same value 118 * with blk_rq_sectors(rq), except that it never be zeroed 119 * by completion. 120 */ 121 unsigned short stats_sectors; 122 123 /* 124 * Number of scatter-gather DMA addr+len pairs after 125 * physical address coalescing is performed. 126 */ 127 unsigned short nr_phys_segments; 128 129 #ifdef CONFIG_BLK_DEV_INTEGRITY 130 unsigned short nr_integrity_segments; 131 #endif 132 133 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 134 struct bio_crypt_ctx *crypt_ctx; 135 struct blk_crypto_keyslot *crypt_keyslot; 136 #endif 137 138 unsigned short ioprio; 139 140 enum mq_rq_state state; 141 atomic_t ref; 142 143 unsigned long deadline; 144 145 /* 146 * The hash is used inside the scheduler, and killed once the 147 * request reaches the dispatch list. The ipi_list is only used 148 * to queue the request for softirq completion, which is long 149 * after the request has been unhashed (and even removed from 150 * the dispatch list). 151 */ 152 union { 153 struct hlist_node hash; /* merge hash */ 154 struct llist_node ipi_list; 155 }; 156 157 /* 158 * The rb_node is only used inside the io scheduler, requests 159 * are pruned when moved to the dispatch queue. special_vec must 160 * only be used if RQF_SPECIAL_PAYLOAD is set, and those cannot be 161 * insert into an IO scheduler. 162 */ 163 union { 164 struct rb_node rb_node; /* sort/lookup */ 165 struct bio_vec special_vec; 166 }; 167 168 /* 169 * Three pointers are available for the IO schedulers, if they need 170 * more they have to dynamically allocate it. 171 */ 172 struct { 173 struct io_cq *icq; 174 void *priv[2]; 175 } elv; 176 177 struct { 178 unsigned int seq; 179 rq_end_io_fn *saved_end_io; 180 } flush; 181 182 u64 fifo_time; 183 184 /* 185 * completion callback. 186 */ 187 rq_end_io_fn *end_io; 188 void *end_io_data; 189 }; 190 191 static inline enum req_op req_op(const struct request *req) 192 { 193 return req->cmd_flags & REQ_OP_MASK; 194 } 195 196 static inline bool blk_rq_is_passthrough(struct request *rq) 197 { 198 return blk_op_is_passthrough(rq->cmd_flags); 199 } 200 201 static inline unsigned short req_get_ioprio(struct request *req) 202 { 203 return req->ioprio; 204 } 205 206 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ) 207 208 #define rq_dma_dir(rq) \ 209 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE) 210 211 #define rq_list_add(listptr, rq) do { \ 212 (rq)->rq_next = *(listptr); \ 213 *(listptr) = rq; \ 214 } while (0) 215 216 #define rq_list_add_tail(lastpptr, rq) do { \ 217 (rq)->rq_next = NULL; \ 218 **(lastpptr) = rq; \ 219 *(lastpptr) = &rq->rq_next; \ 220 } while (0) 221 222 #define rq_list_pop(listptr) \ 223 ({ \ 224 struct request *__req = NULL; \ 225 if ((listptr) && *(listptr)) { \ 226 __req = *(listptr); \ 227 *(listptr) = __req->rq_next; \ 228 } \ 229 __req; \ 230 }) 231 232 #define rq_list_peek(listptr) \ 233 ({ \ 234 struct request *__req = NULL; \ 235 if ((listptr) && *(listptr)) \ 236 __req = *(listptr); \ 237 __req; \ 238 }) 239 240 #define rq_list_for_each(listptr, pos) \ 241 for (pos = rq_list_peek((listptr)); pos; pos = rq_list_next(pos)) 242 243 #define rq_list_for_each_safe(listptr, pos, nxt) \ 244 for (pos = rq_list_peek((listptr)), nxt = rq_list_next(pos); \ 245 pos; pos = nxt, nxt = pos ? rq_list_next(pos) : NULL) 246 247 #define rq_list_next(rq) (rq)->rq_next 248 #define rq_list_empty(list) ((list) == (struct request *) NULL) 249 250 /** 251 * rq_list_move() - move a struct request from one list to another 252 * @src: The source list @rq is currently in 253 * @dst: The destination list that @rq will be appended to 254 * @rq: The request to move 255 * @prev: The request preceding @rq in @src (NULL if @rq is the head) 256 */ 257 static inline void rq_list_move(struct request **src, struct request **dst, 258 struct request *rq, struct request *prev) 259 { 260 if (prev) 261 prev->rq_next = rq->rq_next; 262 else 263 *src = rq->rq_next; 264 rq_list_add(dst, rq); 265 } 266 267 /** 268 * enum blk_eh_timer_return - How the timeout handler should proceed 269 * @BLK_EH_DONE: The block driver completed the command or will complete it at 270 * a later time. 271 * @BLK_EH_RESET_TIMER: Reset the request timer and continue waiting for the 272 * request to complete. 273 */ 274 enum blk_eh_timer_return { 275 BLK_EH_DONE, 276 BLK_EH_RESET_TIMER, 277 }; 278 279 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */ 280 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */ 281 282 /** 283 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware 284 * block device 285 */ 286 struct blk_mq_hw_ctx { 287 struct { 288 /** @lock: Protects the dispatch list. */ 289 spinlock_t lock; 290 /** 291 * @dispatch: Used for requests that are ready to be 292 * dispatched to the hardware but for some reason (e.g. lack of 293 * resources) could not be sent to the hardware. As soon as the 294 * driver can send new requests, requests at this list will 295 * be sent first for a fairer dispatch. 296 */ 297 struct list_head dispatch; 298 /** 299 * @state: BLK_MQ_S_* flags. Defines the state of the hw 300 * queue (active, scheduled to restart, stopped). 301 */ 302 unsigned long state; 303 } ____cacheline_aligned_in_smp; 304 305 /** 306 * @run_work: Used for scheduling a hardware queue run at a later time. 307 */ 308 struct delayed_work run_work; 309 /** @cpumask: Map of available CPUs where this hctx can run. */ 310 cpumask_var_t cpumask; 311 /** 312 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU 313 * selection from @cpumask. 314 */ 315 int next_cpu; 316 /** 317 * @next_cpu_batch: Counter of how many works left in the batch before 318 * changing to the next CPU. 319 */ 320 int next_cpu_batch; 321 322 /** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */ 323 unsigned long flags; 324 325 /** 326 * @sched_data: Pointer owned by the IO scheduler attached to a request 327 * queue. It's up to the IO scheduler how to use this pointer. 328 */ 329 void *sched_data; 330 /** 331 * @queue: Pointer to the request queue that owns this hardware context. 332 */ 333 struct request_queue *queue; 334 /** @fq: Queue of requests that need to perform a flush operation. */ 335 struct blk_flush_queue *fq; 336 337 /** 338 * @driver_data: Pointer to data owned by the block driver that created 339 * this hctx 340 */ 341 void *driver_data; 342 343 /** 344 * @ctx_map: Bitmap for each software queue. If bit is on, there is a 345 * pending request in that software queue. 346 */ 347 struct sbitmap ctx_map; 348 349 /** 350 * @dispatch_from: Software queue to be used when no scheduler was 351 * selected. 352 */ 353 struct blk_mq_ctx *dispatch_from; 354 /** 355 * @dispatch_busy: Number used by blk_mq_update_dispatch_busy() to 356 * decide if the hw_queue is busy using Exponential Weighted Moving 357 * Average algorithm. 358 */ 359 unsigned int dispatch_busy; 360 361 /** @type: HCTX_TYPE_* flags. Type of hardware queue. */ 362 unsigned short type; 363 /** @nr_ctx: Number of software queues. */ 364 unsigned short nr_ctx; 365 /** @ctxs: Array of software queues. */ 366 struct blk_mq_ctx **ctxs; 367 368 /** @dispatch_wait_lock: Lock for dispatch_wait queue. */ 369 spinlock_t dispatch_wait_lock; 370 /** 371 * @dispatch_wait: Waitqueue to put requests when there is no tag 372 * available at the moment, to wait for another try in the future. 373 */ 374 wait_queue_entry_t dispatch_wait; 375 376 /** 377 * @wait_index: Index of next available dispatch_wait queue to insert 378 * requests. 379 */ 380 atomic_t wait_index; 381 382 /** 383 * @tags: Tags owned by the block driver. A tag at this set is only 384 * assigned when a request is dispatched from a hardware queue. 385 */ 386 struct blk_mq_tags *tags; 387 /** 388 * @sched_tags: Tags owned by I/O scheduler. If there is an I/O 389 * scheduler associated with a request queue, a tag is assigned when 390 * that request is allocated. Else, this member is not used. 391 */ 392 struct blk_mq_tags *sched_tags; 393 394 /** @run: Number of dispatched requests. */ 395 unsigned long run; 396 397 /** @numa_node: NUMA node the storage adapter has been connected to. */ 398 unsigned int numa_node; 399 /** @queue_num: Index of this hardware queue. */ 400 unsigned int queue_num; 401 402 /** 403 * @nr_active: Number of active requests. Only used when a tag set is 404 * shared across request queues. 405 */ 406 atomic_t nr_active; 407 408 /** @cpuhp_online: List to store request if CPU is going to die */ 409 struct hlist_node cpuhp_online; 410 /** @cpuhp_dead: List to store request if some CPU die. */ 411 struct hlist_node cpuhp_dead; 412 /** @kobj: Kernel object for sysfs. */ 413 struct kobject kobj; 414 415 #ifdef CONFIG_BLK_DEBUG_FS 416 /** 417 * @debugfs_dir: debugfs directory for this hardware queue. Named 418 * as cpu<cpu_number>. 419 */ 420 struct dentry *debugfs_dir; 421 /** @sched_debugfs_dir: debugfs directory for the scheduler. */ 422 struct dentry *sched_debugfs_dir; 423 #endif 424 425 /** 426 * @hctx_list: if this hctx is not in use, this is an entry in 427 * q->unused_hctx_list. 428 */ 429 struct list_head hctx_list; 430 }; 431 432 /** 433 * struct blk_mq_queue_map - Map software queues to hardware queues 434 * @mq_map: CPU ID to hardware queue index map. This is an array 435 * with nr_cpu_ids elements. Each element has a value in the range 436 * [@queue_offset, @queue_offset + @nr_queues). 437 * @nr_queues: Number of hardware queues to map CPU IDs onto. 438 * @queue_offset: First hardware queue to map onto. Used by the PCIe NVMe 439 * driver to map each hardware queue type (enum hctx_type) onto a distinct 440 * set of hardware queues. 441 */ 442 struct blk_mq_queue_map { 443 unsigned int *mq_map; 444 unsigned int nr_queues; 445 unsigned int queue_offset; 446 }; 447 448 /** 449 * enum hctx_type - Type of hardware queue 450 * @HCTX_TYPE_DEFAULT: All I/O not otherwise accounted for. 451 * @HCTX_TYPE_READ: Just for READ I/O. 452 * @HCTX_TYPE_POLL: Polled I/O of any kind. 453 * @HCTX_MAX_TYPES: Number of types of hctx. 454 */ 455 enum hctx_type { 456 HCTX_TYPE_DEFAULT, 457 HCTX_TYPE_READ, 458 HCTX_TYPE_POLL, 459 460 HCTX_MAX_TYPES, 461 }; 462 463 /** 464 * struct blk_mq_tag_set - tag set that can be shared between request queues 465 * @ops: Pointers to functions that implement block driver behavior. 466 * @map: One or more ctx -> hctx mappings. One map exists for each 467 * hardware queue type (enum hctx_type) that the driver wishes 468 * to support. There are no restrictions on maps being of the 469 * same size, and it's perfectly legal to share maps between 470 * types. 471 * @nr_maps: Number of elements in the @map array. A number in the range 472 * [1, HCTX_MAX_TYPES]. 473 * @nr_hw_queues: Number of hardware queues supported by the block driver that 474 * owns this data structure. 475 * @queue_depth: Number of tags per hardware queue, reserved tags included. 476 * @reserved_tags: Number of tags to set aside for BLK_MQ_REQ_RESERVED tag 477 * allocations. 478 * @cmd_size: Number of additional bytes to allocate per request. The block 479 * driver owns these additional bytes. 480 * @numa_node: NUMA node the storage adapter has been connected to. 481 * @timeout: Request processing timeout in jiffies. 482 * @flags: Zero or more BLK_MQ_F_* flags. 483 * @driver_data: Pointer to data owned by the block driver that created this 484 * tag set. 485 * @tags: Tag sets. One tag set per hardware queue. Has @nr_hw_queues 486 * elements. 487 * @shared_tags: 488 * Shared set of tags. Has @nr_hw_queues elements. If set, 489 * shared by all @tags. 490 * @tag_list_lock: Serializes tag_list accesses. 491 * @tag_list: List of the request queues that use this tag set. See also 492 * request_queue.tag_set_list. 493 * @srcu: Use as lock when type of the request queue is blocking 494 * (BLK_MQ_F_BLOCKING). 495 */ 496 struct blk_mq_tag_set { 497 const struct blk_mq_ops *ops; 498 struct blk_mq_queue_map map[HCTX_MAX_TYPES]; 499 unsigned int nr_maps; 500 unsigned int nr_hw_queues; 501 unsigned int queue_depth; 502 unsigned int reserved_tags; 503 unsigned int cmd_size; 504 int numa_node; 505 unsigned int timeout; 506 unsigned int flags; 507 void *driver_data; 508 509 struct blk_mq_tags **tags; 510 511 struct blk_mq_tags *shared_tags; 512 513 struct mutex tag_list_lock; 514 struct list_head tag_list; 515 struct srcu_struct *srcu; 516 }; 517 518 /** 519 * struct blk_mq_queue_data - Data about a request inserted in a queue 520 * 521 * @rq: Request pointer. 522 * @last: If it is the last request in the queue. 523 */ 524 struct blk_mq_queue_data { 525 struct request *rq; 526 bool last; 527 }; 528 529 typedef bool (busy_tag_iter_fn)(struct request *, void *); 530 531 /** 532 * struct blk_mq_ops - Callback functions that implements block driver 533 * behaviour. 534 */ 535 struct blk_mq_ops { 536 /** 537 * @queue_rq: Queue a new request from block IO. 538 */ 539 blk_status_t (*queue_rq)(struct blk_mq_hw_ctx *, 540 const struct blk_mq_queue_data *); 541 542 /** 543 * @commit_rqs: If a driver uses bd->last to judge when to submit 544 * requests to hardware, it must define this function. In case of errors 545 * that make us stop issuing further requests, this hook serves the 546 * purpose of kicking the hardware (which the last request otherwise 547 * would have done). 548 */ 549 void (*commit_rqs)(struct blk_mq_hw_ctx *); 550 551 /** 552 * @queue_rqs: Queue a list of new requests. Driver is guaranteed 553 * that each request belongs to the same queue. If the driver doesn't 554 * empty the @rqlist completely, then the rest will be queued 555 * individually by the block layer upon return. 556 */ 557 void (*queue_rqs)(struct request **rqlist); 558 559 /** 560 * @get_budget: Reserve budget before queue request, once .queue_rq is 561 * run, it is driver's responsibility to release the 562 * reserved budget. Also we have to handle failure case 563 * of .get_budget for avoiding I/O deadlock. 564 */ 565 int (*get_budget)(struct request_queue *); 566 567 /** 568 * @put_budget: Release the reserved budget. 569 */ 570 void (*put_budget)(struct request_queue *, int); 571 572 /** 573 * @set_rq_budget_token: store rq's budget token 574 */ 575 void (*set_rq_budget_token)(struct request *, int); 576 /** 577 * @get_rq_budget_token: retrieve rq's budget token 578 */ 579 int (*get_rq_budget_token)(struct request *); 580 581 /** 582 * @timeout: Called on request timeout. 583 */ 584 enum blk_eh_timer_return (*timeout)(struct request *); 585 586 /** 587 * @poll: Called to poll for completion of a specific tag. 588 */ 589 int (*poll)(struct blk_mq_hw_ctx *, struct io_comp_batch *); 590 591 /** 592 * @complete: Mark the request as complete. 593 */ 594 void (*complete)(struct request *); 595 596 /** 597 * @init_hctx: Called when the block layer side of a hardware queue has 598 * been set up, allowing the driver to allocate/init matching 599 * structures. 600 */ 601 int (*init_hctx)(struct blk_mq_hw_ctx *, void *, unsigned int); 602 /** 603 * @exit_hctx: Ditto for exit/teardown. 604 */ 605 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int); 606 607 /** 608 * @init_request: Called for every command allocated by the block layer 609 * to allow the driver to set up driver specific data. 610 * 611 * Tag greater than or equal to queue_depth is for setting up 612 * flush request. 613 */ 614 int (*init_request)(struct blk_mq_tag_set *set, struct request *, 615 unsigned int, unsigned int); 616 /** 617 * @exit_request: Ditto for exit/teardown. 618 */ 619 void (*exit_request)(struct blk_mq_tag_set *set, struct request *, 620 unsigned int); 621 622 /** 623 * @cleanup_rq: Called before freeing one request which isn't completed 624 * yet, and usually for freeing the driver private data. 625 */ 626 void (*cleanup_rq)(struct request *); 627 628 /** 629 * @busy: If set, returns whether or not this queue currently is busy. 630 */ 631 bool (*busy)(struct request_queue *); 632 633 /** 634 * @map_queues: This allows drivers specify their own queue mapping by 635 * overriding the setup-time function that builds the mq_map. 636 */ 637 void (*map_queues)(struct blk_mq_tag_set *set); 638 639 #ifdef CONFIG_BLK_DEBUG_FS 640 /** 641 * @show_rq: Used by the debugfs implementation to show driver-specific 642 * information about a request. 643 */ 644 void (*show_rq)(struct seq_file *m, struct request *rq); 645 #endif 646 }; 647 648 enum { 649 BLK_MQ_F_SHOULD_MERGE = 1 << 0, 650 BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1, 651 /* 652 * Set when this device requires underlying blk-mq device for 653 * completing IO: 654 */ 655 BLK_MQ_F_STACKING = 1 << 2, 656 BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3, 657 BLK_MQ_F_BLOCKING = 1 << 5, 658 /* Do not allow an I/O scheduler to be configured. */ 659 BLK_MQ_F_NO_SCHED = 1 << 6, 660 /* 661 * Select 'none' during queue registration in case of a single hwq 662 * or shared hwqs instead of 'mq-deadline'. 663 */ 664 BLK_MQ_F_NO_SCHED_BY_DEFAULT = 1 << 7, 665 BLK_MQ_F_ALLOC_POLICY_START_BIT = 8, 666 BLK_MQ_F_ALLOC_POLICY_BITS = 1, 667 668 BLK_MQ_S_STOPPED = 0, 669 BLK_MQ_S_TAG_ACTIVE = 1, 670 BLK_MQ_S_SCHED_RESTART = 2, 671 672 /* hw queue is inactive after all its CPUs become offline */ 673 BLK_MQ_S_INACTIVE = 3, 674 675 BLK_MQ_MAX_DEPTH = 10240, 676 677 BLK_MQ_CPU_WORK_BATCH = 8, 678 }; 679 #define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \ 680 ((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \ 681 ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) 682 #define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \ 683 ((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \ 684 << BLK_MQ_F_ALLOC_POLICY_START_BIT) 685 686 #define BLK_MQ_NO_HCTX_IDX (-1U) 687 688 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata, 689 struct lock_class_key *lkclass); 690 #define blk_mq_alloc_disk(set, queuedata) \ 691 ({ \ 692 static struct lock_class_key __key; \ 693 \ 694 __blk_mq_alloc_disk(set, queuedata, &__key); \ 695 }) 696 struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q, 697 struct lock_class_key *lkclass); 698 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *); 699 int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, 700 struct request_queue *q); 701 void blk_mq_destroy_queue(struct request_queue *); 702 703 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set); 704 int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set, 705 const struct blk_mq_ops *ops, unsigned int queue_depth, 706 unsigned int set_flags); 707 void blk_mq_free_tag_set(struct blk_mq_tag_set *set); 708 709 void blk_mq_free_request(struct request *rq); 710 int blk_rq_poll(struct request *rq, struct io_comp_batch *iob, 711 unsigned int poll_flags); 712 713 bool blk_mq_queue_inflight(struct request_queue *q); 714 715 enum { 716 /* return when out of requests */ 717 BLK_MQ_REQ_NOWAIT = (__force blk_mq_req_flags_t)(1 << 0), 718 /* allocate from reserved pool */ 719 BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 << 1), 720 /* set RQF_PM */ 721 BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 << 2), 722 }; 723 724 struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf, 725 blk_mq_req_flags_t flags); 726 struct request *blk_mq_alloc_request_hctx(struct request_queue *q, 727 blk_opf_t opf, blk_mq_req_flags_t flags, 728 unsigned int hctx_idx); 729 730 /* 731 * Tag address space map. 732 */ 733 struct blk_mq_tags { 734 unsigned int nr_tags; 735 unsigned int nr_reserved_tags; 736 unsigned int active_queues; 737 738 struct sbitmap_queue bitmap_tags; 739 struct sbitmap_queue breserved_tags; 740 741 struct request **rqs; 742 struct request **static_rqs; 743 struct list_head page_list; 744 745 /* 746 * used to clear request reference in rqs[] before freeing one 747 * request pool 748 */ 749 spinlock_t lock; 750 }; 751 752 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, 753 unsigned int tag) 754 { 755 if (tag < tags->nr_tags) { 756 prefetch(tags->rqs[tag]); 757 return tags->rqs[tag]; 758 } 759 760 return NULL; 761 } 762 763 enum { 764 BLK_MQ_UNIQUE_TAG_BITS = 16, 765 BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1, 766 }; 767 768 u32 blk_mq_unique_tag(struct request *rq); 769 770 static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag) 771 { 772 return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS; 773 } 774 775 static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag) 776 { 777 return unique_tag & BLK_MQ_UNIQUE_TAG_MASK; 778 } 779 780 /** 781 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request 782 * @rq: target request. 783 */ 784 static inline enum mq_rq_state blk_mq_rq_state(struct request *rq) 785 { 786 return READ_ONCE(rq->state); 787 } 788 789 static inline int blk_mq_request_started(struct request *rq) 790 { 791 return blk_mq_rq_state(rq) != MQ_RQ_IDLE; 792 } 793 794 static inline int blk_mq_request_completed(struct request *rq) 795 { 796 return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE; 797 } 798 799 /* 800 * 801 * Set the state to complete when completing a request from inside ->queue_rq. 802 * This is used by drivers that want to ensure special complete actions that 803 * need access to the request are called on failure, e.g. by nvme for 804 * multipathing. 805 */ 806 static inline void blk_mq_set_request_complete(struct request *rq) 807 { 808 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); 809 } 810 811 /* 812 * Complete the request directly instead of deferring it to softirq or 813 * completing it another CPU. Useful in preemptible instead of an interrupt. 814 */ 815 static inline void blk_mq_complete_request_direct(struct request *rq, 816 void (*complete)(struct request *rq)) 817 { 818 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); 819 complete(rq); 820 } 821 822 void blk_mq_start_request(struct request *rq); 823 void blk_mq_end_request(struct request *rq, blk_status_t error); 824 void __blk_mq_end_request(struct request *rq, blk_status_t error); 825 void blk_mq_end_request_batch(struct io_comp_batch *ib); 826 827 /* 828 * Only need start/end time stamping if we have iostat or 829 * blk stats enabled, or using an IO scheduler. 830 */ 831 static inline bool blk_mq_need_time_stamp(struct request *rq) 832 { 833 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED)); 834 } 835 836 static inline bool blk_mq_is_reserved_rq(struct request *rq) 837 { 838 return rq->rq_flags & RQF_RESV; 839 } 840 841 /* 842 * Batched completions only work when there is no I/O error and no special 843 * ->end_io handler. 844 */ 845 static inline bool blk_mq_add_to_batch(struct request *req, 846 struct io_comp_batch *iob, int ioerror, 847 void (*complete)(struct io_comp_batch *)) 848 { 849 /* 850 * blk_mq_end_request_batch() can't end request allocated from 851 * sched tags 852 */ 853 if (!iob || (req->rq_flags & RQF_SCHED_TAGS) || ioerror || 854 (req->end_io && !blk_rq_is_passthrough(req))) 855 return false; 856 857 if (!iob->complete) 858 iob->complete = complete; 859 else if (iob->complete != complete) 860 return false; 861 iob->need_ts |= blk_mq_need_time_stamp(req); 862 rq_list_add(&iob->req_list, req); 863 return true; 864 } 865 866 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list); 867 void blk_mq_kick_requeue_list(struct request_queue *q); 868 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs); 869 void blk_mq_complete_request(struct request *rq); 870 bool blk_mq_complete_request_remote(struct request *rq); 871 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx); 872 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx); 873 void blk_mq_stop_hw_queues(struct request_queue *q); 874 void blk_mq_start_hw_queues(struct request_queue *q); 875 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async); 876 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async); 877 void blk_mq_quiesce_queue(struct request_queue *q); 878 void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set); 879 void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set); 880 void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set); 881 void blk_mq_unquiesce_queue(struct request_queue *q); 882 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs); 883 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async); 884 void blk_mq_run_hw_queues(struct request_queue *q, bool async); 885 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs); 886 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, 887 busy_tag_iter_fn *fn, void *priv); 888 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset); 889 void blk_mq_freeze_queue(struct request_queue *q); 890 void blk_mq_unfreeze_queue(struct request_queue *q); 891 void blk_freeze_queue_start(struct request_queue *q); 892 void blk_mq_freeze_queue_wait(struct request_queue *q); 893 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, 894 unsigned long timeout); 895 896 void blk_mq_map_queues(struct blk_mq_queue_map *qmap); 897 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues); 898 899 void blk_mq_quiesce_queue_nowait(struct request_queue *q); 900 901 unsigned int blk_mq_rq_cpu(struct request *rq); 902 903 bool __blk_should_fake_timeout(struct request_queue *q); 904 static inline bool blk_should_fake_timeout(struct request_queue *q) 905 { 906 if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) && 907 test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags)) 908 return __blk_should_fake_timeout(q); 909 return false; 910 } 911 912 /** 913 * blk_mq_rq_from_pdu - cast a PDU to a request 914 * @pdu: the PDU (Protocol Data Unit) to be casted 915 * 916 * Return: request 917 * 918 * Driver command data is immediately after the request. So subtract request 919 * size to get back to the original request. 920 */ 921 static inline struct request *blk_mq_rq_from_pdu(void *pdu) 922 { 923 return pdu - sizeof(struct request); 924 } 925 926 /** 927 * blk_mq_rq_to_pdu - cast a request to a PDU 928 * @rq: the request to be casted 929 * 930 * Return: pointer to the PDU 931 * 932 * Driver command data is immediately after the request. So add request to get 933 * the PDU. 934 */ 935 static inline void *blk_mq_rq_to_pdu(struct request *rq) 936 { 937 return rq + 1; 938 } 939 940 #define queue_for_each_hw_ctx(q, hctx, i) \ 941 xa_for_each(&(q)->hctx_table, (i), (hctx)) 942 943 #define hctx_for_each_ctx(hctx, ctx, i) \ 944 for ((i) = 0; (i) < (hctx)->nr_ctx && \ 945 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++) 946 947 static inline void blk_mq_cleanup_rq(struct request *rq) 948 { 949 if (rq->q->mq_ops->cleanup_rq) 950 rq->q->mq_ops->cleanup_rq(rq); 951 } 952 953 static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio, 954 unsigned int nr_segs) 955 { 956 rq->nr_phys_segments = nr_segs; 957 rq->__data_len = bio->bi_iter.bi_size; 958 rq->bio = rq->biotail = bio; 959 rq->ioprio = bio_prio(bio); 960 } 961 962 void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx, 963 struct lock_class_key *key); 964 965 static inline bool rq_is_sync(struct request *rq) 966 { 967 return op_is_sync(rq->cmd_flags); 968 } 969 970 void blk_rq_init(struct request_queue *q, struct request *rq); 971 int blk_rq_prep_clone(struct request *rq, struct request *rq_src, 972 struct bio_set *bs, gfp_t gfp_mask, 973 int (*bio_ctr)(struct bio *, struct bio *, void *), void *data); 974 void blk_rq_unprep_clone(struct request *rq); 975 blk_status_t blk_insert_cloned_request(struct request *rq); 976 977 struct rq_map_data { 978 struct page **pages; 979 unsigned long offset; 980 unsigned short page_order; 981 unsigned short nr_entries; 982 bool null_mapped; 983 bool from_user; 984 }; 985 986 int blk_rq_map_user(struct request_queue *, struct request *, 987 struct rq_map_data *, void __user *, unsigned long, gfp_t); 988 int blk_rq_map_user_io(struct request *, struct rq_map_data *, 989 void __user *, unsigned long, gfp_t, bool, int, bool, int); 990 int blk_rq_map_user_iov(struct request_queue *, struct request *, 991 struct rq_map_data *, const struct iov_iter *, gfp_t); 992 int blk_rq_unmap_user(struct bio *); 993 int blk_rq_map_kern(struct request_queue *, struct request *, void *, 994 unsigned int, gfp_t); 995 int blk_rq_append_bio(struct request *rq, struct bio *bio); 996 void blk_execute_rq_nowait(struct request *rq, bool at_head); 997 blk_status_t blk_execute_rq(struct request *rq, bool at_head); 998 bool blk_rq_is_poll(struct request *rq); 999 1000 struct req_iterator { 1001 struct bvec_iter iter; 1002 struct bio *bio; 1003 }; 1004 1005 #define __rq_for_each_bio(_bio, rq) \ 1006 if ((rq->bio)) \ 1007 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next) 1008 1009 #define rq_for_each_segment(bvl, _rq, _iter) \ 1010 __rq_for_each_bio(_iter.bio, _rq) \ 1011 bio_for_each_segment(bvl, _iter.bio, _iter.iter) 1012 1013 #define rq_for_each_bvec(bvl, _rq, _iter) \ 1014 __rq_for_each_bio(_iter.bio, _rq) \ 1015 bio_for_each_bvec(bvl, _iter.bio, _iter.iter) 1016 1017 #define rq_iter_last(bvec, _iter) \ 1018 (_iter.bio->bi_next == NULL && \ 1019 bio_iter_last(bvec, _iter.iter)) 1020 1021 /* 1022 * blk_rq_pos() : the current sector 1023 * blk_rq_bytes() : bytes left in the entire request 1024 * blk_rq_cur_bytes() : bytes left in the current segment 1025 * blk_rq_sectors() : sectors left in the entire request 1026 * blk_rq_cur_sectors() : sectors left in the current segment 1027 * blk_rq_stats_sectors() : sectors of the entire request used for stats 1028 */ 1029 static inline sector_t blk_rq_pos(const struct request *rq) 1030 { 1031 return rq->__sector; 1032 } 1033 1034 static inline unsigned int blk_rq_bytes(const struct request *rq) 1035 { 1036 return rq->__data_len; 1037 } 1038 1039 static inline int blk_rq_cur_bytes(const struct request *rq) 1040 { 1041 if (!rq->bio) 1042 return 0; 1043 if (!bio_has_data(rq->bio)) /* dataless requests such as discard */ 1044 return rq->bio->bi_iter.bi_size; 1045 return bio_iovec(rq->bio).bv_len; 1046 } 1047 1048 static inline unsigned int blk_rq_sectors(const struct request *rq) 1049 { 1050 return blk_rq_bytes(rq) >> SECTOR_SHIFT; 1051 } 1052 1053 static inline unsigned int blk_rq_cur_sectors(const struct request *rq) 1054 { 1055 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT; 1056 } 1057 1058 static inline unsigned int blk_rq_stats_sectors(const struct request *rq) 1059 { 1060 return rq->stats_sectors; 1061 } 1062 1063 /* 1064 * Some commands like WRITE SAME have a payload or data transfer size which 1065 * is different from the size of the request. Any driver that supports such 1066 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to 1067 * calculate the data transfer size. 1068 */ 1069 static inline unsigned int blk_rq_payload_bytes(struct request *rq) 1070 { 1071 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1072 return rq->special_vec.bv_len; 1073 return blk_rq_bytes(rq); 1074 } 1075 1076 /* 1077 * Return the first full biovec in the request. The caller needs to check that 1078 * there are any bvecs before calling this helper. 1079 */ 1080 static inline struct bio_vec req_bvec(struct request *rq) 1081 { 1082 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1083 return rq->special_vec; 1084 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter); 1085 } 1086 1087 static inline unsigned int blk_rq_count_bios(struct request *rq) 1088 { 1089 unsigned int nr_bios = 0; 1090 struct bio *bio; 1091 1092 __rq_for_each_bio(bio, rq) 1093 nr_bios++; 1094 1095 return nr_bios; 1096 } 1097 1098 void blk_steal_bios(struct bio_list *list, struct request *rq); 1099 1100 /* 1101 * Request completion related functions. 1102 * 1103 * blk_update_request() completes given number of bytes and updates 1104 * the request without completing it. 1105 */ 1106 bool blk_update_request(struct request *rq, blk_status_t error, 1107 unsigned int nr_bytes); 1108 void blk_abort_request(struct request *); 1109 1110 /* 1111 * Number of physical segments as sent to the device. 1112 * 1113 * Normally this is the number of discontiguous data segments sent by the 1114 * submitter. But for data-less command like discard we might have no 1115 * actual data segments submitted, but the driver might have to add it's 1116 * own special payload. In that case we still return 1 here so that this 1117 * special payload will be mapped. 1118 */ 1119 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq) 1120 { 1121 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 1122 return 1; 1123 return rq->nr_phys_segments; 1124 } 1125 1126 /* 1127 * Number of discard segments (or ranges) the driver needs to fill in. 1128 * Each discard bio merged into a request is counted as one segment. 1129 */ 1130 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq) 1131 { 1132 return max_t(unsigned short, rq->nr_phys_segments, 1); 1133 } 1134 1135 int __blk_rq_map_sg(struct request_queue *q, struct request *rq, 1136 struct scatterlist *sglist, struct scatterlist **last_sg); 1137 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq, 1138 struct scatterlist *sglist) 1139 { 1140 struct scatterlist *last_sg = NULL; 1141 1142 return __blk_rq_map_sg(q, rq, sglist, &last_sg); 1143 } 1144 void blk_dump_rq_flags(struct request *, char *); 1145 1146 #ifdef CONFIG_BLK_DEV_ZONED 1147 static inline unsigned int blk_rq_zone_no(struct request *rq) 1148 { 1149 return disk_zone_no(rq->q->disk, blk_rq_pos(rq)); 1150 } 1151 1152 static inline unsigned int blk_rq_zone_is_seq(struct request *rq) 1153 { 1154 return disk_zone_is_seq(rq->q->disk, blk_rq_pos(rq)); 1155 } 1156 1157 /** 1158 * blk_rq_is_seq_zoned_write() - Check if @rq requires write serialization. 1159 * @rq: Request to examine. 1160 * 1161 * Note: REQ_OP_ZONE_APPEND requests do not require serialization. 1162 */ 1163 static inline bool blk_rq_is_seq_zoned_write(struct request *rq) 1164 { 1165 return op_needs_zoned_write_locking(req_op(rq)) && 1166 blk_rq_zone_is_seq(rq); 1167 } 1168 1169 bool blk_req_needs_zone_write_lock(struct request *rq); 1170 bool blk_req_zone_write_trylock(struct request *rq); 1171 void __blk_req_zone_write_lock(struct request *rq); 1172 void __blk_req_zone_write_unlock(struct request *rq); 1173 1174 static inline void blk_req_zone_write_lock(struct request *rq) 1175 { 1176 if (blk_req_needs_zone_write_lock(rq)) 1177 __blk_req_zone_write_lock(rq); 1178 } 1179 1180 static inline void blk_req_zone_write_unlock(struct request *rq) 1181 { 1182 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED) 1183 __blk_req_zone_write_unlock(rq); 1184 } 1185 1186 static inline bool blk_req_zone_is_write_locked(struct request *rq) 1187 { 1188 return rq->q->disk->seq_zones_wlock && 1189 test_bit(blk_rq_zone_no(rq), rq->q->disk->seq_zones_wlock); 1190 } 1191 1192 static inline bool blk_req_can_dispatch_to_zone(struct request *rq) 1193 { 1194 if (!blk_req_needs_zone_write_lock(rq)) 1195 return true; 1196 return !blk_req_zone_is_write_locked(rq); 1197 } 1198 #else /* CONFIG_BLK_DEV_ZONED */ 1199 static inline bool blk_rq_is_seq_zoned_write(struct request *rq) 1200 { 1201 return false; 1202 } 1203 1204 static inline bool blk_req_needs_zone_write_lock(struct request *rq) 1205 { 1206 return false; 1207 } 1208 1209 static inline void blk_req_zone_write_lock(struct request *rq) 1210 { 1211 } 1212 1213 static inline void blk_req_zone_write_unlock(struct request *rq) 1214 { 1215 } 1216 static inline bool blk_req_zone_is_write_locked(struct request *rq) 1217 { 1218 return false; 1219 } 1220 1221 static inline bool blk_req_can_dispatch_to_zone(struct request *rq) 1222 { 1223 return true; 1224 } 1225 #endif /* CONFIG_BLK_DEV_ZONED */ 1226 1227 #endif /* BLK_MQ_H */ 1228