Linux 6.14-rc1
[linux.git] / include / linux / blk-mq.h
blob9ebb53f031cdb519c6b60cc2bc0c4b8b3f4d5ff4
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef BLK_MQ_H
3 #define BLK_MQ_H
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 #include <linux/rw_hint.h>
13 struct blk_mq_tags;
14 struct blk_flush_queue;
16 #define BLKDEV_MIN_RQ 4
17 #define BLKDEV_DEFAULT_RQ 128
19 enum rq_end_io_ret {
20 RQ_END_IO_NONE,
21 RQ_END_IO_FREE,
24 typedef enum rq_end_io_ret (rq_end_io_fn)(struct request *, blk_status_t);
27 * request flags */
28 typedef __u32 __bitwise req_flags_t;
30 /* Keep rqf_name[] in sync with the definitions below */
31 enum {
32 /* drive already may have started this one */
33 __RQF_STARTED,
34 /* request for flush sequence */
35 __RQF_FLUSH_SEQ,
36 /* merge of different types, fail separately */
37 __RQF_MIXED_MERGE,
38 /* don't call prep for this one */
39 __RQF_DONTPREP,
40 /* use hctx->sched_tags */
41 __RQF_SCHED_TAGS,
42 /* use an I/O scheduler for this request */
43 __RQF_USE_SCHED,
44 /* vaguely specified driver internal error. Ignored by block layer */
45 __RQF_FAILED,
46 /* don't warn about errors */
47 __RQF_QUIET,
48 /* account into disk and partition IO statistics */
49 __RQF_IO_STAT,
50 /* runtime pm request */
51 __RQF_PM,
52 /* on IO scheduler merge hash */
53 __RQF_HASHED,
54 /* track IO completion time */
55 __RQF_STATS,
56 /* Look at ->special_vec for the actual data payload instead of the
57 bio chain. */
58 __RQF_SPECIAL_PAYLOAD,
59 /* request completion needs to be signaled to zone write plugging. */
60 __RQF_ZONE_WRITE_PLUGGING,
61 /* ->timeout has been called, don't expire again */
62 __RQF_TIMED_OUT,
63 __RQF_RESV,
64 __RQF_BITS
67 #define RQF_STARTED ((__force req_flags_t)(1 << __RQF_STARTED))
68 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << __RQF_FLUSH_SEQ))
69 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << __RQF_MIXED_MERGE))
70 #define RQF_DONTPREP ((__force req_flags_t)(1 << __RQF_DONTPREP))
71 #define RQF_SCHED_TAGS ((__force req_flags_t)(1 << __RQF_SCHED_TAGS))
72 #define RQF_USE_SCHED ((__force req_flags_t)(1 << __RQF_USE_SCHED))
73 #define RQF_FAILED ((__force req_flags_t)(1 << __RQF_FAILED))
74 #define RQF_QUIET ((__force req_flags_t)(1 << __RQF_QUIET))
75 #define RQF_IO_STAT ((__force req_flags_t)(1 << __RQF_IO_STAT))
76 #define RQF_PM ((__force req_flags_t)(1 << __RQF_PM))
77 #define RQF_HASHED ((__force req_flags_t)(1 << __RQF_HASHED))
78 #define RQF_STATS ((__force req_flags_t)(1 << __RQF_STATS))
79 #define RQF_SPECIAL_PAYLOAD \
80 ((__force req_flags_t)(1 << __RQF_SPECIAL_PAYLOAD))
81 #define RQF_ZONE_WRITE_PLUGGING \
82 ((__force req_flags_t)(1 << __RQF_ZONE_WRITE_PLUGGING))
83 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << __RQF_TIMED_OUT))
84 #define RQF_RESV ((__force req_flags_t)(1 << __RQF_RESV))
86 /* flags that prevent us from merging requests: */
87 #define RQF_NOMERGE_FLAGS \
88 (RQF_STARTED | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
90 enum mq_rq_state {
91 MQ_RQ_IDLE = 0,
92 MQ_RQ_IN_FLIGHT = 1,
93 MQ_RQ_COMPLETE = 2,
97 * Try to put the fields that are referenced together in the same cacheline.
99 * If you modify this structure, make sure to update blk_rq_init() and
100 * especially blk_mq_rq_ctx_init() to take care of the added fields.
102 struct request {
103 struct request_queue *q;
104 struct blk_mq_ctx *mq_ctx;
105 struct blk_mq_hw_ctx *mq_hctx;
107 blk_opf_t cmd_flags; /* op and common flags */
108 req_flags_t rq_flags;
110 int tag;
111 int internal_tag;
113 unsigned int timeout;
115 /* the following two fields are internal, NEVER access directly */
116 unsigned int __data_len; /* total data len */
117 sector_t __sector; /* sector cursor */
119 struct bio *bio;
120 struct bio *biotail;
122 union {
123 struct list_head queuelist;
124 struct request *rq_next;
127 struct block_device *part;
128 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
129 /* Time that the first bio started allocating this request. */
130 u64 alloc_time_ns;
131 #endif
132 /* Time that this request was allocated for this IO. */
133 u64 start_time_ns;
134 /* Time that I/O was submitted to the device. */
135 u64 io_start_time_ns;
137 #ifdef CONFIG_BLK_WBT
138 unsigned short wbt_flags;
139 #endif
141 * rq sectors used for blk stats. It has the same value
142 * with blk_rq_sectors(rq), except that it never be zeroed
143 * by completion.
145 unsigned short stats_sectors;
148 * Number of scatter-gather DMA addr+len pairs after
149 * physical address coalescing is performed.
151 unsigned short nr_phys_segments;
152 unsigned short nr_integrity_segments;
154 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
155 struct bio_crypt_ctx *crypt_ctx;
156 struct blk_crypto_keyslot *crypt_keyslot;
157 #endif
159 enum mq_rq_state state;
160 atomic_t ref;
162 unsigned long deadline;
165 * The hash is used inside the scheduler, and killed once the
166 * request reaches the dispatch list. The ipi_list is only used
167 * to queue the request for softirq completion, which is long
168 * after the request has been unhashed (and even removed from
169 * the dispatch list).
171 union {
172 struct hlist_node hash; /* merge hash */
173 struct llist_node ipi_list;
177 * The rb_node is only used inside the io scheduler, requests
178 * are pruned when moved to the dispatch queue. special_vec must
179 * only be used if RQF_SPECIAL_PAYLOAD is set, and those cannot be
180 * insert into an IO scheduler.
182 union {
183 struct rb_node rb_node; /* sort/lookup */
184 struct bio_vec special_vec;
188 * Three pointers are available for the IO schedulers, if they need
189 * more they have to dynamically allocate it.
191 struct {
192 struct io_cq *icq;
193 void *priv[2];
194 } elv;
196 struct {
197 unsigned int seq;
198 rq_end_io_fn *saved_end_io;
199 } flush;
201 u64 fifo_time;
204 * completion callback.
206 rq_end_io_fn *end_io;
207 void *end_io_data;
210 static inline enum req_op req_op(const struct request *req)
212 return req->cmd_flags & REQ_OP_MASK;
215 static inline bool blk_rq_is_passthrough(struct request *rq)
217 return blk_op_is_passthrough(rq->cmd_flags);
220 static inline unsigned short req_get_ioprio(struct request *req)
222 if (req->bio)
223 return req->bio->bi_ioprio;
224 return 0;
227 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
229 #define rq_dma_dir(rq) \
230 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
232 static inline int rq_list_empty(const struct rq_list *rl)
234 return rl->head == NULL;
237 static inline void rq_list_init(struct rq_list *rl)
239 rl->head = NULL;
240 rl->tail = NULL;
243 static inline void rq_list_add_tail(struct rq_list *rl, struct request *rq)
245 rq->rq_next = NULL;
246 if (rl->tail)
247 rl->tail->rq_next = rq;
248 else
249 rl->head = rq;
250 rl->tail = rq;
253 static inline void rq_list_add_head(struct rq_list *rl, struct request *rq)
255 rq->rq_next = rl->head;
256 rl->head = rq;
257 if (!rl->tail)
258 rl->tail = rq;
261 static inline struct request *rq_list_pop(struct rq_list *rl)
263 struct request *rq = rl->head;
265 if (rq) {
266 rl->head = rl->head->rq_next;
267 if (!rl->head)
268 rl->tail = NULL;
269 rq->rq_next = NULL;
272 return rq;
275 static inline struct request *rq_list_peek(struct rq_list *rl)
277 return rl->head;
280 #define rq_list_for_each(rl, pos) \
281 for (pos = rq_list_peek((rl)); (pos); pos = pos->rq_next)
283 #define rq_list_for_each_safe(rl, pos, nxt) \
284 for (pos = rq_list_peek((rl)), nxt = pos->rq_next; \
285 pos; pos = nxt, nxt = pos ? pos->rq_next : NULL)
288 * enum blk_eh_timer_return - How the timeout handler should proceed
289 * @BLK_EH_DONE: The block driver completed the command or will complete it at
290 * a later time.
291 * @BLK_EH_RESET_TIMER: Reset the request timer and continue waiting for the
292 * request to complete.
294 enum blk_eh_timer_return {
295 BLK_EH_DONE,
296 BLK_EH_RESET_TIMER,
300 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware
301 * block device
303 struct blk_mq_hw_ctx {
304 struct {
305 /** @lock: Protects the dispatch list. */
306 spinlock_t lock;
308 * @dispatch: Used for requests that are ready to be
309 * dispatched to the hardware but for some reason (e.g. lack of
310 * resources) could not be sent to the hardware. As soon as the
311 * driver can send new requests, requests at this list will
312 * be sent first for a fairer dispatch.
314 struct list_head dispatch;
316 * @state: BLK_MQ_S_* flags. Defines the state of the hw
317 * queue (active, scheduled to restart, stopped).
319 unsigned long state;
320 } ____cacheline_aligned_in_smp;
323 * @run_work: Used for scheduling a hardware queue run at a later time.
325 struct delayed_work run_work;
326 /** @cpumask: Map of available CPUs where this hctx can run. */
327 cpumask_var_t cpumask;
329 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU
330 * selection from @cpumask.
332 int next_cpu;
334 * @next_cpu_batch: Counter of how many works left in the batch before
335 * changing to the next CPU.
337 int next_cpu_batch;
339 /** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */
340 unsigned long flags;
343 * @sched_data: Pointer owned by the IO scheduler attached to a request
344 * queue. It's up to the IO scheduler how to use this pointer.
346 void *sched_data;
348 * @queue: Pointer to the request queue that owns this hardware context.
350 struct request_queue *queue;
351 /** @fq: Queue of requests that need to perform a flush operation. */
352 struct blk_flush_queue *fq;
355 * @driver_data: Pointer to data owned by the block driver that created
356 * this hctx
358 void *driver_data;
361 * @ctx_map: Bitmap for each software queue. If bit is on, there is a
362 * pending request in that software queue.
364 struct sbitmap ctx_map;
367 * @dispatch_from: Software queue to be used when no scheduler was
368 * selected.
370 struct blk_mq_ctx *dispatch_from;
372 * @dispatch_busy: Number used by blk_mq_update_dispatch_busy() to
373 * decide if the hw_queue is busy using Exponential Weighted Moving
374 * Average algorithm.
376 unsigned int dispatch_busy;
378 /** @type: HCTX_TYPE_* flags. Type of hardware queue. */
379 unsigned short type;
380 /** @nr_ctx: Number of software queues. */
381 unsigned short nr_ctx;
382 /** @ctxs: Array of software queues. */
383 struct blk_mq_ctx **ctxs;
385 /** @dispatch_wait_lock: Lock for dispatch_wait queue. */
386 spinlock_t dispatch_wait_lock;
388 * @dispatch_wait: Waitqueue to put requests when there is no tag
389 * available at the moment, to wait for another try in the future.
391 wait_queue_entry_t dispatch_wait;
394 * @wait_index: Index of next available dispatch_wait queue to insert
395 * requests.
397 atomic_t wait_index;
400 * @tags: Tags owned by the block driver. A tag at this set is only
401 * assigned when a request is dispatched from a hardware queue.
403 struct blk_mq_tags *tags;
405 * @sched_tags: Tags owned by I/O scheduler. If there is an I/O
406 * scheduler associated with a request queue, a tag is assigned when
407 * that request is allocated. Else, this member is not used.
409 struct blk_mq_tags *sched_tags;
411 /** @numa_node: NUMA node the storage adapter has been connected to. */
412 unsigned int numa_node;
413 /** @queue_num: Index of this hardware queue. */
414 unsigned int queue_num;
417 * @nr_active: Number of active requests. Only used when a tag set is
418 * shared across request queues.
420 atomic_t nr_active;
422 /** @cpuhp_online: List to store request if CPU is going to die */
423 struct hlist_node cpuhp_online;
424 /** @cpuhp_dead: List to store request if some CPU die. */
425 struct hlist_node cpuhp_dead;
426 /** @kobj: Kernel object for sysfs. */
427 struct kobject kobj;
429 #ifdef CONFIG_BLK_DEBUG_FS
431 * @debugfs_dir: debugfs directory for this hardware queue. Named
432 * as cpu<cpu_number>.
434 struct dentry *debugfs_dir;
435 /** @sched_debugfs_dir: debugfs directory for the scheduler. */
436 struct dentry *sched_debugfs_dir;
437 #endif
440 * @hctx_list: if this hctx is not in use, this is an entry in
441 * q->unused_hctx_list.
443 struct list_head hctx_list;
447 * struct blk_mq_queue_map - Map software queues to hardware queues
448 * @mq_map: CPU ID to hardware queue index map. This is an array
449 * with nr_cpu_ids elements. Each element has a value in the range
450 * [@queue_offset, @queue_offset + @nr_queues).
451 * @nr_queues: Number of hardware queues to map CPU IDs onto.
452 * @queue_offset: First hardware queue to map onto. Used by the PCIe NVMe
453 * driver to map each hardware queue type (enum hctx_type) onto a distinct
454 * set of hardware queues.
456 struct blk_mq_queue_map {
457 unsigned int *mq_map;
458 unsigned int nr_queues;
459 unsigned int queue_offset;
463 * enum hctx_type - Type of hardware queue
464 * @HCTX_TYPE_DEFAULT: All I/O not otherwise accounted for.
465 * @HCTX_TYPE_READ: Just for READ I/O.
466 * @HCTX_TYPE_POLL: Polled I/O of any kind.
467 * @HCTX_MAX_TYPES: Number of types of hctx.
469 enum hctx_type {
470 HCTX_TYPE_DEFAULT,
471 HCTX_TYPE_READ,
472 HCTX_TYPE_POLL,
474 HCTX_MAX_TYPES,
478 * struct blk_mq_tag_set - tag set that can be shared between request queues
479 * @ops: Pointers to functions that implement block driver behavior.
480 * @map: One or more ctx -> hctx mappings. One map exists for each
481 * hardware queue type (enum hctx_type) that the driver wishes
482 * to support. There are no restrictions on maps being of the
483 * same size, and it's perfectly legal to share maps between
484 * types.
485 * @nr_maps: Number of elements in the @map array. A number in the range
486 * [1, HCTX_MAX_TYPES].
487 * @nr_hw_queues: Number of hardware queues supported by the block driver that
488 * owns this data structure.
489 * @queue_depth: Number of tags per hardware queue, reserved tags included.
490 * @reserved_tags: Number of tags to set aside for BLK_MQ_REQ_RESERVED tag
491 * allocations.
492 * @cmd_size: Number of additional bytes to allocate per request. The block
493 * driver owns these additional bytes.
494 * @numa_node: NUMA node the storage adapter has been connected to.
495 * @timeout: Request processing timeout in jiffies.
496 * @flags: Zero or more BLK_MQ_F_* flags.
497 * @driver_data: Pointer to data owned by the block driver that created this
498 * tag set.
499 * @tags: Tag sets. One tag set per hardware queue. Has @nr_hw_queues
500 * elements.
501 * @shared_tags:
502 * Shared set of tags. Has @nr_hw_queues elements. If set,
503 * shared by all @tags.
504 * @tag_list_lock: Serializes tag_list accesses.
505 * @tag_list: List of the request queues that use this tag set. See also
506 * request_queue.tag_set_list.
507 * @srcu: Use as lock when type of the request queue is blocking
508 * (BLK_MQ_F_BLOCKING).
510 struct blk_mq_tag_set {
511 const struct blk_mq_ops *ops;
512 struct blk_mq_queue_map map[HCTX_MAX_TYPES];
513 unsigned int nr_maps;
514 unsigned int nr_hw_queues;
515 unsigned int queue_depth;
516 unsigned int reserved_tags;
517 unsigned int cmd_size;
518 int numa_node;
519 unsigned int timeout;
520 unsigned int flags;
521 void *driver_data;
523 struct blk_mq_tags **tags;
525 struct blk_mq_tags *shared_tags;
527 struct mutex tag_list_lock;
528 struct list_head tag_list;
529 struct srcu_struct *srcu;
533 * struct blk_mq_queue_data - Data about a request inserted in a queue
535 * @rq: Request pointer.
536 * @last: If it is the last request in the queue.
538 struct blk_mq_queue_data {
539 struct request *rq;
540 bool last;
543 typedef bool (busy_tag_iter_fn)(struct request *, void *);
546 * struct blk_mq_ops - Callback functions that implements block driver
547 * behaviour.
549 struct blk_mq_ops {
551 * @queue_rq: Queue a new request from block IO.
553 blk_status_t (*queue_rq)(struct blk_mq_hw_ctx *,
554 const struct blk_mq_queue_data *);
557 * @commit_rqs: If a driver uses bd->last to judge when to submit
558 * requests to hardware, it must define this function. In case of errors
559 * that make us stop issuing further requests, this hook serves the
560 * purpose of kicking the hardware (which the last request otherwise
561 * would have done).
563 void (*commit_rqs)(struct blk_mq_hw_ctx *);
566 * @queue_rqs: Queue a list of new requests. Driver is guaranteed
567 * that each request belongs to the same queue. If the driver doesn't
568 * empty the @rqlist completely, then the rest will be queued
569 * individually by the block layer upon return.
571 void (*queue_rqs)(struct rq_list *rqlist);
574 * @get_budget: Reserve budget before queue request, once .queue_rq is
575 * run, it is driver's responsibility to release the
576 * reserved budget. Also we have to handle failure case
577 * of .get_budget for avoiding I/O deadlock.
579 int (*get_budget)(struct request_queue *);
582 * @put_budget: Release the reserved budget.
584 void (*put_budget)(struct request_queue *, int);
587 * @set_rq_budget_token: store rq's budget token
589 void (*set_rq_budget_token)(struct request *, int);
591 * @get_rq_budget_token: retrieve rq's budget token
593 int (*get_rq_budget_token)(struct request *);
596 * @timeout: Called on request timeout.
598 enum blk_eh_timer_return (*timeout)(struct request *);
601 * @poll: Called to poll for completion of a specific tag.
603 int (*poll)(struct blk_mq_hw_ctx *, struct io_comp_batch *);
606 * @complete: Mark the request as complete.
608 void (*complete)(struct request *);
611 * @init_hctx: Called when the block layer side of a hardware queue has
612 * been set up, allowing the driver to allocate/init matching
613 * structures.
615 int (*init_hctx)(struct blk_mq_hw_ctx *, void *, unsigned int);
617 * @exit_hctx: Ditto for exit/teardown.
619 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
622 * @init_request: Called for every command allocated by the block layer
623 * to allow the driver to set up driver specific data.
625 * Tag greater than or equal to queue_depth is for setting up
626 * flush request.
628 int (*init_request)(struct blk_mq_tag_set *set, struct request *,
629 unsigned int, unsigned int);
631 * @exit_request: Ditto for exit/teardown.
633 void (*exit_request)(struct blk_mq_tag_set *set, struct request *,
634 unsigned int);
637 * @cleanup_rq: Called before freeing one request which isn't completed
638 * yet, and usually for freeing the driver private data.
640 void (*cleanup_rq)(struct request *);
643 * @busy: If set, returns whether or not this queue currently is busy.
645 bool (*busy)(struct request_queue *);
648 * @map_queues: This allows drivers specify their own queue mapping by
649 * overriding the setup-time function that builds the mq_map.
651 void (*map_queues)(struct blk_mq_tag_set *set);
653 #ifdef CONFIG_BLK_DEBUG_FS
655 * @show_rq: Used by the debugfs implementation to show driver-specific
656 * information about a request.
658 void (*show_rq)(struct seq_file *m, struct request *rq);
659 #endif
662 /* Keep hctx_flag_name[] in sync with the definitions below */
663 enum {
664 BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1,
666 * Set when this device requires underlying blk-mq device for
667 * completing IO:
669 BLK_MQ_F_STACKING = 1 << 2,
670 BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3,
671 BLK_MQ_F_BLOCKING = 1 << 4,
674 * Alloc tags on a round-robin base instead of the first available one.
676 BLK_MQ_F_TAG_RR = 1 << 5,
679 * Select 'none' during queue registration in case of a single hwq
680 * or shared hwqs instead of 'mq-deadline'.
682 BLK_MQ_F_NO_SCHED_BY_DEFAULT = 1 << 6,
684 BLK_MQ_F_MAX = 1 << 7,
687 #define BLK_MQ_MAX_DEPTH (10240)
688 #define BLK_MQ_NO_HCTX_IDX (-1U)
690 enum {
691 /* Keep hctx_state_name[] in sync with the definitions below */
692 BLK_MQ_S_STOPPED,
693 BLK_MQ_S_TAG_ACTIVE,
694 BLK_MQ_S_SCHED_RESTART,
695 /* hw queue is inactive after all its CPUs become offline */
696 BLK_MQ_S_INACTIVE,
697 BLK_MQ_S_MAX
700 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set,
701 struct queue_limits *lim, void *queuedata,
702 struct lock_class_key *lkclass);
703 #define blk_mq_alloc_disk(set, lim, queuedata) \
704 ({ \
705 static struct lock_class_key __key; \
707 __blk_mq_alloc_disk(set, lim, queuedata, &__key); \
709 struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
710 struct lock_class_key *lkclass);
711 struct request_queue *blk_mq_alloc_queue(struct blk_mq_tag_set *set,
712 struct queue_limits *lim, void *queuedata);
713 int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
714 struct request_queue *q);
715 void blk_mq_destroy_queue(struct request_queue *);
717 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
718 int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
719 const struct blk_mq_ops *ops, unsigned int queue_depth,
720 unsigned int set_flags);
721 void blk_mq_free_tag_set(struct blk_mq_tag_set *set);
723 void blk_mq_free_request(struct request *rq);
724 int blk_rq_poll(struct request *rq, struct io_comp_batch *iob,
725 unsigned int poll_flags);
727 bool blk_mq_queue_inflight(struct request_queue *q);
729 enum {
730 /* return when out of requests */
731 BLK_MQ_REQ_NOWAIT = (__force blk_mq_req_flags_t)(1 << 0),
732 /* allocate from reserved pool */
733 BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 << 1),
734 /* set RQF_PM */
735 BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 << 2),
738 struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
739 blk_mq_req_flags_t flags);
740 struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
741 blk_opf_t opf, blk_mq_req_flags_t flags,
742 unsigned int hctx_idx);
745 * Tag address space map.
747 struct blk_mq_tags {
748 unsigned int nr_tags;
749 unsigned int nr_reserved_tags;
750 unsigned int active_queues;
752 struct sbitmap_queue bitmap_tags;
753 struct sbitmap_queue breserved_tags;
755 struct request **rqs;
756 struct request **static_rqs;
757 struct list_head page_list;
760 * used to clear request reference in rqs[] before freeing one
761 * request pool
763 spinlock_t lock;
766 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,
767 unsigned int tag)
769 if (tag < tags->nr_tags) {
770 prefetch(tags->rqs[tag]);
771 return tags->rqs[tag];
774 return NULL;
777 enum {
778 BLK_MQ_UNIQUE_TAG_BITS = 16,
779 BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
782 u32 blk_mq_unique_tag(struct request *rq);
784 static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
786 return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
789 static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
791 return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
795 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
796 * @rq: target request.
798 static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
800 return READ_ONCE(rq->state);
803 static inline int blk_mq_request_started(struct request *rq)
805 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
808 static inline int blk_mq_request_completed(struct request *rq)
810 return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
815 * Set the state to complete when completing a request from inside ->queue_rq.
816 * This is used by drivers that want to ensure special complete actions that
817 * need access to the request are called on failure, e.g. by nvme for
818 * multipathing.
820 static inline void blk_mq_set_request_complete(struct request *rq)
822 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
826 * Complete the request directly instead of deferring it to softirq or
827 * completing it another CPU. Useful in preemptible instead of an interrupt.
829 static inline void blk_mq_complete_request_direct(struct request *rq,
830 void (*complete)(struct request *rq))
832 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
833 complete(rq);
836 void blk_mq_start_request(struct request *rq);
837 void blk_mq_end_request(struct request *rq, blk_status_t error);
838 void __blk_mq_end_request(struct request *rq, blk_status_t error);
839 void blk_mq_end_request_batch(struct io_comp_batch *ib);
842 * Only need start/end time stamping if we have iostat or
843 * blk stats enabled, or using an IO scheduler.
845 static inline bool blk_mq_need_time_stamp(struct request *rq)
847 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED));
850 static inline bool blk_mq_is_reserved_rq(struct request *rq)
852 return rq->rq_flags & RQF_RESV;
856 * Batched completions only work when there is no I/O error and no special
857 * ->end_io handler.
859 static inline bool blk_mq_add_to_batch(struct request *req,
860 struct io_comp_batch *iob, int ioerror,
861 void (*complete)(struct io_comp_batch *))
864 * blk_mq_end_request_batch() can't end request allocated from
865 * sched tags
867 if (!iob || (req->rq_flags & RQF_SCHED_TAGS) || ioerror ||
868 (req->end_io && !blk_rq_is_passthrough(req)))
869 return false;
871 if (!iob->complete)
872 iob->complete = complete;
873 else if (iob->complete != complete)
874 return false;
875 iob->need_ts |= blk_mq_need_time_stamp(req);
876 rq_list_add_tail(&iob->req_list, req);
877 return true;
880 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
881 void blk_mq_kick_requeue_list(struct request_queue *q);
882 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
883 void blk_mq_complete_request(struct request *rq);
884 bool blk_mq_complete_request_remote(struct request *rq);
885 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
886 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
887 void blk_mq_stop_hw_queues(struct request_queue *q);
888 void blk_mq_start_hw_queues(struct request_queue *q);
889 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
890 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
891 void blk_mq_quiesce_queue(struct request_queue *q);
892 void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set);
893 void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set);
894 void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set);
895 void blk_mq_unquiesce_queue(struct request_queue *q);
896 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
897 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
898 void blk_mq_run_hw_queues(struct request_queue *q, bool async);
899 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
900 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
901 busy_tag_iter_fn *fn, void *priv);
902 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
903 void blk_mq_freeze_queue_nomemsave(struct request_queue *q);
904 void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q);
905 static inline unsigned int __must_check
906 blk_mq_freeze_queue(struct request_queue *q)
908 unsigned int memflags = memalloc_noio_save();
910 blk_mq_freeze_queue_nomemsave(q);
911 return memflags;
913 static inline void
914 blk_mq_unfreeze_queue(struct request_queue *q, unsigned int memflags)
916 blk_mq_unfreeze_queue_nomemrestore(q);
917 memalloc_noio_restore(memflags);
919 void blk_freeze_queue_start(struct request_queue *q);
920 void blk_mq_freeze_queue_wait(struct request_queue *q);
921 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
922 unsigned long timeout);
923 void blk_mq_unfreeze_queue_non_owner(struct request_queue *q);
924 void blk_freeze_queue_start_non_owner(struct request_queue *q);
926 void blk_mq_map_queues(struct blk_mq_queue_map *qmap);
927 void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap,
928 struct device *dev, unsigned int offset);
929 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
931 void blk_mq_quiesce_queue_nowait(struct request_queue *q);
933 unsigned int blk_mq_rq_cpu(struct request *rq);
935 bool __blk_should_fake_timeout(struct request_queue *q);
936 static inline bool blk_should_fake_timeout(struct request_queue *q)
938 if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) &&
939 test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
940 return __blk_should_fake_timeout(q);
941 return false;
945 * blk_mq_rq_from_pdu - cast a PDU to a request
946 * @pdu: the PDU (Protocol Data Unit) to be casted
948 * Return: request
950 * Driver command data is immediately after the request. So subtract request
951 * size to get back to the original request.
953 static inline struct request *blk_mq_rq_from_pdu(void *pdu)
955 return pdu - sizeof(struct request);
959 * blk_mq_rq_to_pdu - cast a request to a PDU
960 * @rq: the request to be casted
962 * Return: pointer to the PDU
964 * Driver command data is immediately after the request. So add request to get
965 * the PDU.
967 static inline void *blk_mq_rq_to_pdu(struct request *rq)
969 return rq + 1;
972 #define queue_for_each_hw_ctx(q, hctx, i) \
973 xa_for_each(&(q)->hctx_table, (i), (hctx))
975 #define hctx_for_each_ctx(hctx, ctx, i) \
976 for ((i) = 0; (i) < (hctx)->nr_ctx && \
977 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
979 static inline void blk_mq_cleanup_rq(struct request *rq)
981 if (rq->q->mq_ops->cleanup_rq)
982 rq->q->mq_ops->cleanup_rq(rq);
985 void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
986 struct lock_class_key *key);
988 static inline bool rq_is_sync(struct request *rq)
990 return op_is_sync(rq->cmd_flags);
993 void blk_rq_init(struct request_queue *q, struct request *rq);
994 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
995 struct bio_set *bs, gfp_t gfp_mask,
996 int (*bio_ctr)(struct bio *, struct bio *, void *), void *data);
997 void blk_rq_unprep_clone(struct request *rq);
998 blk_status_t blk_insert_cloned_request(struct request *rq);
1000 struct rq_map_data {
1001 struct page **pages;
1002 unsigned long offset;
1003 unsigned short page_order;
1004 unsigned short nr_entries;
1005 bool null_mapped;
1006 bool from_user;
1009 int blk_rq_map_user(struct request_queue *, struct request *,
1010 struct rq_map_data *, void __user *, unsigned long, gfp_t);
1011 int blk_rq_map_user_io(struct request *, struct rq_map_data *,
1012 void __user *, unsigned long, gfp_t, bool, int, bool, int);
1013 int blk_rq_map_user_iov(struct request_queue *, struct request *,
1014 struct rq_map_data *, const struct iov_iter *, gfp_t);
1015 int blk_rq_unmap_user(struct bio *);
1016 int blk_rq_map_kern(struct request_queue *, struct request *, void *,
1017 unsigned int, gfp_t);
1018 int blk_rq_append_bio(struct request *rq, struct bio *bio);
1019 void blk_execute_rq_nowait(struct request *rq, bool at_head);
1020 blk_status_t blk_execute_rq(struct request *rq, bool at_head);
1021 bool blk_rq_is_poll(struct request *rq);
1023 struct req_iterator {
1024 struct bvec_iter iter;
1025 struct bio *bio;
1028 #define __rq_for_each_bio(_bio, rq) \
1029 if ((rq->bio)) \
1030 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
1032 #define rq_for_each_segment(bvl, _rq, _iter) \
1033 __rq_for_each_bio(_iter.bio, _rq) \
1034 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
1036 #define rq_for_each_bvec(bvl, _rq, _iter) \
1037 __rq_for_each_bio(_iter.bio, _rq) \
1038 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
1040 #define rq_iter_last(bvec, _iter) \
1041 (_iter.bio->bi_next == NULL && \
1042 bio_iter_last(bvec, _iter.iter))
1045 * blk_rq_pos() : the current sector
1046 * blk_rq_bytes() : bytes left in the entire request
1047 * blk_rq_cur_bytes() : bytes left in the current segment
1048 * blk_rq_sectors() : sectors left in the entire request
1049 * blk_rq_cur_sectors() : sectors left in the current segment
1050 * blk_rq_stats_sectors() : sectors of the entire request used for stats
1052 static inline sector_t blk_rq_pos(const struct request *rq)
1054 return rq->__sector;
1057 static inline unsigned int blk_rq_bytes(const struct request *rq)
1059 return rq->__data_len;
1062 static inline int blk_rq_cur_bytes(const struct request *rq)
1064 if (!rq->bio)
1065 return 0;
1066 if (!bio_has_data(rq->bio)) /* dataless requests such as discard */
1067 return rq->bio->bi_iter.bi_size;
1068 return bio_iovec(rq->bio).bv_len;
1071 static inline unsigned int blk_rq_sectors(const struct request *rq)
1073 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1076 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1078 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1081 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
1083 return rq->stats_sectors;
1087 * Some commands like WRITE SAME have a payload or data transfer size which
1088 * is different from the size of the request. Any driver that supports such
1089 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1090 * calculate the data transfer size.
1092 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1094 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1095 return rq->special_vec.bv_len;
1096 return blk_rq_bytes(rq);
1100 * Return the first full biovec in the request. The caller needs to check that
1101 * there are any bvecs before calling this helper.
1103 static inline struct bio_vec req_bvec(struct request *rq)
1105 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1106 return rq->special_vec;
1107 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1110 static inline unsigned int blk_rq_count_bios(struct request *rq)
1112 unsigned int nr_bios = 0;
1113 struct bio *bio;
1115 __rq_for_each_bio(bio, rq)
1116 nr_bios++;
1118 return nr_bios;
1121 void blk_steal_bios(struct bio_list *list, struct request *rq);
1124 * Request completion related functions.
1126 * blk_update_request() completes given number of bytes and updates
1127 * the request without completing it.
1129 bool blk_update_request(struct request *rq, blk_status_t error,
1130 unsigned int nr_bytes);
1131 void blk_abort_request(struct request *);
1134 * Number of physical segments as sent to the device.
1136 * Normally this is the number of discontiguous data segments sent by the
1137 * submitter. But for data-less command like discard we might have no
1138 * actual data segments submitted, but the driver might have to add it's
1139 * own special payload. In that case we still return 1 here so that this
1140 * special payload will be mapped.
1142 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1144 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1145 return 1;
1146 return rq->nr_phys_segments;
1150 * Number of discard segments (or ranges) the driver needs to fill in.
1151 * Each discard bio merged into a request is counted as one segment.
1153 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1155 return max_t(unsigned short, rq->nr_phys_segments, 1);
1158 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1159 struct scatterlist *sglist, struct scatterlist **last_sg);
1160 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1161 struct scatterlist *sglist)
1163 struct scatterlist *last_sg = NULL;
1165 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1167 void blk_dump_rq_flags(struct request *, char *);
1169 #endif /* BLK_MQ_H */