printf: Remove unused 'bprintf'
[drm/drm-misc.git] / include / linux / blk-mq.h
blobc596e0e4cb751ab00f2150cf086fcdc5ad32b02e
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,
299 /* Keep alloc_policy_name[] in sync with the definitions below */
300 enum {
301 BLK_TAG_ALLOC_FIFO, /* allocate starting from 0 */
302 BLK_TAG_ALLOC_RR, /* allocate starting from last allocated tag */
303 BLK_TAG_ALLOC_MAX
307 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware
308 * block device
310 struct blk_mq_hw_ctx {
311 struct {
312 /** @lock: Protects the dispatch list. */
313 spinlock_t lock;
315 * @dispatch: Used for requests that are ready to be
316 * dispatched to the hardware but for some reason (e.g. lack of
317 * resources) could not be sent to the hardware. As soon as the
318 * driver can send new requests, requests at this list will
319 * be sent first for a fairer dispatch.
321 struct list_head dispatch;
323 * @state: BLK_MQ_S_* flags. Defines the state of the hw
324 * queue (active, scheduled to restart, stopped).
326 unsigned long state;
327 } ____cacheline_aligned_in_smp;
330 * @run_work: Used for scheduling a hardware queue run at a later time.
332 struct delayed_work run_work;
333 /** @cpumask: Map of available CPUs where this hctx can run. */
334 cpumask_var_t cpumask;
336 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU
337 * selection from @cpumask.
339 int next_cpu;
341 * @next_cpu_batch: Counter of how many works left in the batch before
342 * changing to the next CPU.
344 int next_cpu_batch;
346 /** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */
347 unsigned long flags;
350 * @sched_data: Pointer owned by the IO scheduler attached to a request
351 * queue. It's up to the IO scheduler how to use this pointer.
353 void *sched_data;
355 * @queue: Pointer to the request queue that owns this hardware context.
357 struct request_queue *queue;
358 /** @fq: Queue of requests that need to perform a flush operation. */
359 struct blk_flush_queue *fq;
362 * @driver_data: Pointer to data owned by the block driver that created
363 * this hctx
365 void *driver_data;
368 * @ctx_map: Bitmap for each software queue. If bit is on, there is a
369 * pending request in that software queue.
371 struct sbitmap ctx_map;
374 * @dispatch_from: Software queue to be used when no scheduler was
375 * selected.
377 struct blk_mq_ctx *dispatch_from;
379 * @dispatch_busy: Number used by blk_mq_update_dispatch_busy() to
380 * decide if the hw_queue is busy using Exponential Weighted Moving
381 * Average algorithm.
383 unsigned int dispatch_busy;
385 /** @type: HCTX_TYPE_* flags. Type of hardware queue. */
386 unsigned short type;
387 /** @nr_ctx: Number of software queues. */
388 unsigned short nr_ctx;
389 /** @ctxs: Array of software queues. */
390 struct blk_mq_ctx **ctxs;
392 /** @dispatch_wait_lock: Lock for dispatch_wait queue. */
393 spinlock_t dispatch_wait_lock;
395 * @dispatch_wait: Waitqueue to put requests when there is no tag
396 * available at the moment, to wait for another try in the future.
398 wait_queue_entry_t dispatch_wait;
401 * @wait_index: Index of next available dispatch_wait queue to insert
402 * requests.
404 atomic_t wait_index;
407 * @tags: Tags owned by the block driver. A tag at this set is only
408 * assigned when a request is dispatched from a hardware queue.
410 struct blk_mq_tags *tags;
412 * @sched_tags: Tags owned by I/O scheduler. If there is an I/O
413 * scheduler associated with a request queue, a tag is assigned when
414 * that request is allocated. Else, this member is not used.
416 struct blk_mq_tags *sched_tags;
418 /** @numa_node: NUMA node the storage adapter has been connected to. */
419 unsigned int numa_node;
420 /** @queue_num: Index of this hardware queue. */
421 unsigned int queue_num;
424 * @nr_active: Number of active requests. Only used when a tag set is
425 * shared across request queues.
427 atomic_t nr_active;
429 /** @cpuhp_online: List to store request if CPU is going to die */
430 struct hlist_node cpuhp_online;
431 /** @cpuhp_dead: List to store request if some CPU die. */
432 struct hlist_node cpuhp_dead;
433 /** @kobj: Kernel object for sysfs. */
434 struct kobject kobj;
436 #ifdef CONFIG_BLK_DEBUG_FS
438 * @debugfs_dir: debugfs directory for this hardware queue. Named
439 * as cpu<cpu_number>.
441 struct dentry *debugfs_dir;
442 /** @sched_debugfs_dir: debugfs directory for the scheduler. */
443 struct dentry *sched_debugfs_dir;
444 #endif
447 * @hctx_list: if this hctx is not in use, this is an entry in
448 * q->unused_hctx_list.
450 struct list_head hctx_list;
454 * struct blk_mq_queue_map - Map software queues to hardware queues
455 * @mq_map: CPU ID to hardware queue index map. This is an array
456 * with nr_cpu_ids elements. Each element has a value in the range
457 * [@queue_offset, @queue_offset + @nr_queues).
458 * @nr_queues: Number of hardware queues to map CPU IDs onto.
459 * @queue_offset: First hardware queue to map onto. Used by the PCIe NVMe
460 * driver to map each hardware queue type (enum hctx_type) onto a distinct
461 * set of hardware queues.
463 struct blk_mq_queue_map {
464 unsigned int *mq_map;
465 unsigned int nr_queues;
466 unsigned int queue_offset;
470 * enum hctx_type - Type of hardware queue
471 * @HCTX_TYPE_DEFAULT: All I/O not otherwise accounted for.
472 * @HCTX_TYPE_READ: Just for READ I/O.
473 * @HCTX_TYPE_POLL: Polled I/O of any kind.
474 * @HCTX_MAX_TYPES: Number of types of hctx.
476 enum hctx_type {
477 HCTX_TYPE_DEFAULT,
478 HCTX_TYPE_READ,
479 HCTX_TYPE_POLL,
481 HCTX_MAX_TYPES,
485 * struct blk_mq_tag_set - tag set that can be shared between request queues
486 * @ops: Pointers to functions that implement block driver behavior.
487 * @map: One or more ctx -> hctx mappings. One map exists for each
488 * hardware queue type (enum hctx_type) that the driver wishes
489 * to support. There are no restrictions on maps being of the
490 * same size, and it's perfectly legal to share maps between
491 * types.
492 * @nr_maps: Number of elements in the @map array. A number in the range
493 * [1, HCTX_MAX_TYPES].
494 * @nr_hw_queues: Number of hardware queues supported by the block driver that
495 * owns this data structure.
496 * @queue_depth: Number of tags per hardware queue, reserved tags included.
497 * @reserved_tags: Number of tags to set aside for BLK_MQ_REQ_RESERVED tag
498 * allocations.
499 * @cmd_size: Number of additional bytes to allocate per request. The block
500 * driver owns these additional bytes.
501 * @numa_node: NUMA node the storage adapter has been connected to.
502 * @timeout: Request processing timeout in jiffies.
503 * @flags: Zero or more BLK_MQ_F_* flags.
504 * @driver_data: Pointer to data owned by the block driver that created this
505 * tag set.
506 * @tags: Tag sets. One tag set per hardware queue. Has @nr_hw_queues
507 * elements.
508 * @shared_tags:
509 * Shared set of tags. Has @nr_hw_queues elements. If set,
510 * shared by all @tags.
511 * @tag_list_lock: Serializes tag_list accesses.
512 * @tag_list: List of the request queues that use this tag set. See also
513 * request_queue.tag_set_list.
514 * @srcu: Use as lock when type of the request queue is blocking
515 * (BLK_MQ_F_BLOCKING).
517 struct blk_mq_tag_set {
518 const struct blk_mq_ops *ops;
519 struct blk_mq_queue_map map[HCTX_MAX_TYPES];
520 unsigned int nr_maps;
521 unsigned int nr_hw_queues;
522 unsigned int queue_depth;
523 unsigned int reserved_tags;
524 unsigned int cmd_size;
525 int numa_node;
526 unsigned int timeout;
527 unsigned int flags;
528 void *driver_data;
530 struct blk_mq_tags **tags;
532 struct blk_mq_tags *shared_tags;
534 struct mutex tag_list_lock;
535 struct list_head tag_list;
536 struct srcu_struct *srcu;
540 * struct blk_mq_queue_data - Data about a request inserted in a queue
542 * @rq: Request pointer.
543 * @last: If it is the last request in the queue.
545 struct blk_mq_queue_data {
546 struct request *rq;
547 bool last;
550 typedef bool (busy_tag_iter_fn)(struct request *, void *);
553 * struct blk_mq_ops - Callback functions that implements block driver
554 * behaviour.
556 struct blk_mq_ops {
558 * @queue_rq: Queue a new request from block IO.
560 blk_status_t (*queue_rq)(struct blk_mq_hw_ctx *,
561 const struct blk_mq_queue_data *);
564 * @commit_rqs: If a driver uses bd->last to judge when to submit
565 * requests to hardware, it must define this function. In case of errors
566 * that make us stop issuing further requests, this hook serves the
567 * purpose of kicking the hardware (which the last request otherwise
568 * would have done).
570 void (*commit_rqs)(struct blk_mq_hw_ctx *);
573 * @queue_rqs: Queue a list of new requests. Driver is guaranteed
574 * that each request belongs to the same queue. If the driver doesn't
575 * empty the @rqlist completely, then the rest will be queued
576 * individually by the block layer upon return.
578 void (*queue_rqs)(struct rq_list *rqlist);
581 * @get_budget: Reserve budget before queue request, once .queue_rq is
582 * run, it is driver's responsibility to release the
583 * reserved budget. Also we have to handle failure case
584 * of .get_budget for avoiding I/O deadlock.
586 int (*get_budget)(struct request_queue *);
589 * @put_budget: Release the reserved budget.
591 void (*put_budget)(struct request_queue *, int);
594 * @set_rq_budget_token: store rq's budget token
596 void (*set_rq_budget_token)(struct request *, int);
598 * @get_rq_budget_token: retrieve rq's budget token
600 int (*get_rq_budget_token)(struct request *);
603 * @timeout: Called on request timeout.
605 enum blk_eh_timer_return (*timeout)(struct request *);
608 * @poll: Called to poll for completion of a specific tag.
610 int (*poll)(struct blk_mq_hw_ctx *, struct io_comp_batch *);
613 * @complete: Mark the request as complete.
615 void (*complete)(struct request *);
618 * @init_hctx: Called when the block layer side of a hardware queue has
619 * been set up, allowing the driver to allocate/init matching
620 * structures.
622 int (*init_hctx)(struct blk_mq_hw_ctx *, void *, unsigned int);
624 * @exit_hctx: Ditto for exit/teardown.
626 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
629 * @init_request: Called for every command allocated by the block layer
630 * to allow the driver to set up driver specific data.
632 * Tag greater than or equal to queue_depth is for setting up
633 * flush request.
635 int (*init_request)(struct blk_mq_tag_set *set, struct request *,
636 unsigned int, unsigned int);
638 * @exit_request: Ditto for exit/teardown.
640 void (*exit_request)(struct blk_mq_tag_set *set, struct request *,
641 unsigned int);
644 * @cleanup_rq: Called before freeing one request which isn't completed
645 * yet, and usually for freeing the driver private data.
647 void (*cleanup_rq)(struct request *);
650 * @busy: If set, returns whether or not this queue currently is busy.
652 bool (*busy)(struct request_queue *);
655 * @map_queues: This allows drivers specify their own queue mapping by
656 * overriding the setup-time function that builds the mq_map.
658 void (*map_queues)(struct blk_mq_tag_set *set);
660 #ifdef CONFIG_BLK_DEBUG_FS
662 * @show_rq: Used by the debugfs implementation to show driver-specific
663 * information about a request.
665 void (*show_rq)(struct seq_file *m, struct request *rq);
666 #endif
669 /* Keep hctx_flag_name[] in sync with the definitions below */
670 enum {
671 BLK_MQ_F_SHOULD_MERGE = 1 << 0,
672 BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1,
674 * Set when this device requires underlying blk-mq device for
675 * completing IO:
677 BLK_MQ_F_STACKING = 1 << 2,
678 BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3,
679 BLK_MQ_F_BLOCKING = 1 << 4,
680 /* Do not allow an I/O scheduler to be configured. */
681 BLK_MQ_F_NO_SCHED = 1 << 5,
684 * Select 'none' during queue registration in case of a single hwq
685 * or shared hwqs instead of 'mq-deadline'.
687 BLK_MQ_F_NO_SCHED_BY_DEFAULT = 1 << 6,
688 BLK_MQ_F_ALLOC_POLICY_START_BIT = 7,
689 BLK_MQ_F_ALLOC_POLICY_BITS = 1,
691 #define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \
692 ((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \
693 ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1))
694 #define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \
695 ((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \
696 << BLK_MQ_F_ALLOC_POLICY_START_BIT)
698 #define BLK_MQ_MAX_DEPTH (10240)
699 #define BLK_MQ_NO_HCTX_IDX (-1U)
701 enum {
702 /* Keep hctx_state_name[] in sync with the definitions below */
703 BLK_MQ_S_STOPPED,
704 BLK_MQ_S_TAG_ACTIVE,
705 BLK_MQ_S_SCHED_RESTART,
706 /* hw queue is inactive after all its CPUs become offline */
707 BLK_MQ_S_INACTIVE,
708 BLK_MQ_S_MAX
711 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set,
712 struct queue_limits *lim, void *queuedata,
713 struct lock_class_key *lkclass);
714 #define blk_mq_alloc_disk(set, lim, queuedata) \
715 ({ \
716 static struct lock_class_key __key; \
718 __blk_mq_alloc_disk(set, lim, queuedata, &__key); \
720 struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
721 struct lock_class_key *lkclass);
722 struct request_queue *blk_mq_alloc_queue(struct blk_mq_tag_set *set,
723 struct queue_limits *lim, void *queuedata);
724 int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
725 struct request_queue *q);
726 void blk_mq_destroy_queue(struct request_queue *);
728 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
729 int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
730 const struct blk_mq_ops *ops, unsigned int queue_depth,
731 unsigned int set_flags);
732 void blk_mq_free_tag_set(struct blk_mq_tag_set *set);
734 void blk_mq_free_request(struct request *rq);
735 int blk_rq_poll(struct request *rq, struct io_comp_batch *iob,
736 unsigned int poll_flags);
738 bool blk_mq_queue_inflight(struct request_queue *q);
740 enum {
741 /* return when out of requests */
742 BLK_MQ_REQ_NOWAIT = (__force blk_mq_req_flags_t)(1 << 0),
743 /* allocate from reserved pool */
744 BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 << 1),
745 /* set RQF_PM */
746 BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 << 2),
749 struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
750 blk_mq_req_flags_t flags);
751 struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
752 blk_opf_t opf, blk_mq_req_flags_t flags,
753 unsigned int hctx_idx);
756 * Tag address space map.
758 struct blk_mq_tags {
759 unsigned int nr_tags;
760 unsigned int nr_reserved_tags;
761 unsigned int active_queues;
763 struct sbitmap_queue bitmap_tags;
764 struct sbitmap_queue breserved_tags;
766 struct request **rqs;
767 struct request **static_rqs;
768 struct list_head page_list;
771 * used to clear request reference in rqs[] before freeing one
772 * request pool
774 spinlock_t lock;
777 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,
778 unsigned int tag)
780 if (tag < tags->nr_tags) {
781 prefetch(tags->rqs[tag]);
782 return tags->rqs[tag];
785 return NULL;
788 enum {
789 BLK_MQ_UNIQUE_TAG_BITS = 16,
790 BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
793 u32 blk_mq_unique_tag(struct request *rq);
795 static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
797 return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
800 static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
802 return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
806 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
807 * @rq: target request.
809 static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
811 return READ_ONCE(rq->state);
814 static inline int blk_mq_request_started(struct request *rq)
816 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
819 static inline int blk_mq_request_completed(struct request *rq)
821 return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
826 * Set the state to complete when completing a request from inside ->queue_rq.
827 * This is used by drivers that want to ensure special complete actions that
828 * need access to the request are called on failure, e.g. by nvme for
829 * multipathing.
831 static inline void blk_mq_set_request_complete(struct request *rq)
833 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
837 * Complete the request directly instead of deferring it to softirq or
838 * completing it another CPU. Useful in preemptible instead of an interrupt.
840 static inline void blk_mq_complete_request_direct(struct request *rq,
841 void (*complete)(struct request *rq))
843 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
844 complete(rq);
847 void blk_mq_start_request(struct request *rq);
848 void blk_mq_end_request(struct request *rq, blk_status_t error);
849 void __blk_mq_end_request(struct request *rq, blk_status_t error);
850 void blk_mq_end_request_batch(struct io_comp_batch *ib);
853 * Only need start/end time stamping if we have iostat or
854 * blk stats enabled, or using an IO scheduler.
856 static inline bool blk_mq_need_time_stamp(struct request *rq)
858 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED));
861 static inline bool blk_mq_is_reserved_rq(struct request *rq)
863 return rq->rq_flags & RQF_RESV;
867 * Batched completions only work when there is no I/O error and no special
868 * ->end_io handler.
870 static inline bool blk_mq_add_to_batch(struct request *req,
871 struct io_comp_batch *iob, int ioerror,
872 void (*complete)(struct io_comp_batch *))
875 * blk_mq_end_request_batch() can't end request allocated from
876 * sched tags
878 if (!iob || (req->rq_flags & RQF_SCHED_TAGS) || ioerror ||
879 (req->end_io && !blk_rq_is_passthrough(req)))
880 return false;
882 if (!iob->complete)
883 iob->complete = complete;
884 else if (iob->complete != complete)
885 return false;
886 iob->need_ts |= blk_mq_need_time_stamp(req);
887 rq_list_add_tail(&iob->req_list, req);
888 return true;
891 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
892 void blk_mq_kick_requeue_list(struct request_queue *q);
893 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
894 void blk_mq_complete_request(struct request *rq);
895 bool blk_mq_complete_request_remote(struct request *rq);
896 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
897 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
898 void blk_mq_stop_hw_queues(struct request_queue *q);
899 void blk_mq_start_hw_queues(struct request_queue *q);
900 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
901 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
902 void blk_mq_quiesce_queue(struct request_queue *q);
903 void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set);
904 void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set);
905 void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set);
906 void blk_mq_unquiesce_queue(struct request_queue *q);
907 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
908 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
909 void blk_mq_run_hw_queues(struct request_queue *q, bool async);
910 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
911 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
912 busy_tag_iter_fn *fn, void *priv);
913 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
914 void blk_mq_freeze_queue(struct request_queue *q);
915 void blk_mq_unfreeze_queue(struct request_queue *q);
916 void blk_freeze_queue_start(struct request_queue *q);
917 void blk_mq_freeze_queue_wait(struct request_queue *q);
918 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
919 unsigned long timeout);
920 void blk_mq_unfreeze_queue_non_owner(struct request_queue *q);
921 void blk_freeze_queue_start_non_owner(struct request_queue *q);
923 void blk_mq_map_queues(struct blk_mq_queue_map *qmap);
924 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
926 void blk_mq_quiesce_queue_nowait(struct request_queue *q);
928 unsigned int blk_mq_rq_cpu(struct request *rq);
930 bool __blk_should_fake_timeout(struct request_queue *q);
931 static inline bool blk_should_fake_timeout(struct request_queue *q)
933 if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) &&
934 test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
935 return __blk_should_fake_timeout(q);
936 return false;
940 * blk_mq_rq_from_pdu - cast a PDU to a request
941 * @pdu: the PDU (Protocol Data Unit) to be casted
943 * Return: request
945 * Driver command data is immediately after the request. So subtract request
946 * size to get back to the original request.
948 static inline struct request *blk_mq_rq_from_pdu(void *pdu)
950 return pdu - sizeof(struct request);
954 * blk_mq_rq_to_pdu - cast a request to a PDU
955 * @rq: the request to be casted
957 * Return: pointer to the PDU
959 * Driver command data is immediately after the request. So add request to get
960 * the PDU.
962 static inline void *blk_mq_rq_to_pdu(struct request *rq)
964 return rq + 1;
967 #define queue_for_each_hw_ctx(q, hctx, i) \
968 xa_for_each(&(q)->hctx_table, (i), (hctx))
970 #define hctx_for_each_ctx(hctx, ctx, i) \
971 for ((i) = 0; (i) < (hctx)->nr_ctx && \
972 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
974 static inline void blk_mq_cleanup_rq(struct request *rq)
976 if (rq->q->mq_ops->cleanup_rq)
977 rq->q->mq_ops->cleanup_rq(rq);
980 static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio,
981 unsigned int nr_segs)
983 rq->nr_phys_segments = nr_segs;
984 rq->__data_len = bio->bi_iter.bi_size;
985 rq->bio = rq->biotail = bio;
988 void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
989 struct lock_class_key *key);
991 static inline bool rq_is_sync(struct request *rq)
993 return op_is_sync(rq->cmd_flags);
996 void blk_rq_init(struct request_queue *q, struct request *rq);
997 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
998 struct bio_set *bs, gfp_t gfp_mask,
999 int (*bio_ctr)(struct bio *, struct bio *, void *), void *data);
1000 void blk_rq_unprep_clone(struct request *rq);
1001 blk_status_t blk_insert_cloned_request(struct request *rq);
1003 struct rq_map_data {
1004 struct page **pages;
1005 unsigned long offset;
1006 unsigned short page_order;
1007 unsigned short nr_entries;
1008 bool null_mapped;
1009 bool from_user;
1012 int blk_rq_map_user(struct request_queue *, struct request *,
1013 struct rq_map_data *, void __user *, unsigned long, gfp_t);
1014 int blk_rq_map_user_io(struct request *, struct rq_map_data *,
1015 void __user *, unsigned long, gfp_t, bool, int, bool, int);
1016 int blk_rq_map_user_iov(struct request_queue *, struct request *,
1017 struct rq_map_data *, const struct iov_iter *, gfp_t);
1018 int blk_rq_unmap_user(struct bio *);
1019 int blk_rq_map_kern(struct request_queue *, struct request *, void *,
1020 unsigned int, gfp_t);
1021 int blk_rq_append_bio(struct request *rq, struct bio *bio);
1022 void blk_execute_rq_nowait(struct request *rq, bool at_head);
1023 blk_status_t blk_execute_rq(struct request *rq, bool at_head);
1024 bool blk_rq_is_poll(struct request *rq);
1026 struct req_iterator {
1027 struct bvec_iter iter;
1028 struct bio *bio;
1031 #define __rq_for_each_bio(_bio, rq) \
1032 if ((rq->bio)) \
1033 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
1035 #define rq_for_each_segment(bvl, _rq, _iter) \
1036 __rq_for_each_bio(_iter.bio, _rq) \
1037 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
1039 #define rq_for_each_bvec(bvl, _rq, _iter) \
1040 __rq_for_each_bio(_iter.bio, _rq) \
1041 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
1043 #define rq_iter_last(bvec, _iter) \
1044 (_iter.bio->bi_next == NULL && \
1045 bio_iter_last(bvec, _iter.iter))
1048 * blk_rq_pos() : the current sector
1049 * blk_rq_bytes() : bytes left in the entire request
1050 * blk_rq_cur_bytes() : bytes left in the current segment
1051 * blk_rq_sectors() : sectors left in the entire request
1052 * blk_rq_cur_sectors() : sectors left in the current segment
1053 * blk_rq_stats_sectors() : sectors of the entire request used for stats
1055 static inline sector_t blk_rq_pos(const struct request *rq)
1057 return rq->__sector;
1060 static inline unsigned int blk_rq_bytes(const struct request *rq)
1062 return rq->__data_len;
1065 static inline int blk_rq_cur_bytes(const struct request *rq)
1067 if (!rq->bio)
1068 return 0;
1069 if (!bio_has_data(rq->bio)) /* dataless requests such as discard */
1070 return rq->bio->bi_iter.bi_size;
1071 return bio_iovec(rq->bio).bv_len;
1074 static inline unsigned int blk_rq_sectors(const struct request *rq)
1076 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1079 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1081 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1084 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
1086 return rq->stats_sectors;
1090 * Some commands like WRITE SAME have a payload or data transfer size which
1091 * is different from the size of the request. Any driver that supports such
1092 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1093 * calculate the data transfer size.
1095 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1097 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1098 return rq->special_vec.bv_len;
1099 return blk_rq_bytes(rq);
1103 * Return the first full biovec in the request. The caller needs to check that
1104 * there are any bvecs before calling this helper.
1106 static inline struct bio_vec req_bvec(struct request *rq)
1108 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1109 return rq->special_vec;
1110 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1113 static inline unsigned int blk_rq_count_bios(struct request *rq)
1115 unsigned int nr_bios = 0;
1116 struct bio *bio;
1118 __rq_for_each_bio(bio, rq)
1119 nr_bios++;
1121 return nr_bios;
1124 void blk_steal_bios(struct bio_list *list, struct request *rq);
1127 * Request completion related functions.
1129 * blk_update_request() completes given number of bytes and updates
1130 * the request without completing it.
1132 bool blk_update_request(struct request *rq, blk_status_t error,
1133 unsigned int nr_bytes);
1134 void blk_abort_request(struct request *);
1137 * Number of physical segments as sent to the device.
1139 * Normally this is the number of discontiguous data segments sent by the
1140 * submitter. But for data-less command like discard we might have no
1141 * actual data segments submitted, but the driver might have to add it's
1142 * own special payload. In that case we still return 1 here so that this
1143 * special payload will be mapped.
1145 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1147 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1148 return 1;
1149 return rq->nr_phys_segments;
1153 * Number of discard segments (or ranges) the driver needs to fill in.
1154 * Each discard bio merged into a request is counted as one segment.
1156 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1158 return max_t(unsigned short, rq->nr_phys_segments, 1);
1161 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1162 struct scatterlist *sglist, struct scatterlist **last_sg);
1163 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1164 struct scatterlist *sglist)
1166 struct scatterlist *last_sg = NULL;
1168 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1170 void blk_dump_rq_flags(struct request *, char *);
1172 #endif /* BLK_MQ_H */