1 /* SPDX-License-Identifier: GPL-2.0 */
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>
12 struct blk_flush_queue;
14 #define BLKDEV_MIN_RQ 4
15 #define BLKDEV_DEFAULT_RQ 128
22 typedef enum rq_end_io_ret (rq_end_io_fn)(struct request *, blk_status_t);
26 typedef __u32 __bitwise req_flags_t;
28 /* drive already may have started this one */
29 #define RQF_STARTED ((__force req_flags_t)(1 << 1))
30 /* may not be passed by ioscheduler */
31 #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
32 /* request for flush sequence */
33 #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
34 /* merge of different types, fail separately */
35 #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
36 /* track inflight for MQ */
37 #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
38 /* don't call prep for this one */
39 #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
40 /* vaguely specified driver internal error. Ignored by the block layer */
41 #define RQF_FAILED ((__force req_flags_t)(1 << 10))
42 /* don't warn about errors */
43 #define RQF_QUIET ((__force req_flags_t)(1 << 11))
44 /* elevator private data attached */
45 #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
46 /* account into disk and partition IO statistics */
47 #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
48 /* runtime pm request */
49 #define RQF_PM ((__force req_flags_t)(1 << 15))
50 /* on IO scheduler merge hash */
51 #define RQF_HASHED ((__force req_flags_t)(1 << 16))
52 /* track IO completion time */
53 #define RQF_STATS ((__force req_flags_t)(1 << 17))
54 /* Look at ->special_vec for the actual data payload instead of the
56 #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
57 /* The per-zone write lock is held for this request */
58 #define RQF_ZONE_WRITE_LOCKED ((__force req_flags_t)(1 << 19))
59 /* already slept for hybrid poll */
60 #define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
61 /* ->timeout has been called, don't expire again */
62 #define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
63 /* queue has elevator attached */
64 #define RQF_ELV ((__force req_flags_t)(1 << 22))
65 #define RQF_RESV ((__force req_flags_t)(1 << 23))
67 /* flags that prevent us from merging requests: */
68 #define RQF_NOMERGE_FLAGS \
69 (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
78 * Try to put the fields that are referenced together in the same cacheline.
80 * If you modify this structure, make sure to update blk_rq_init() and
81 * especially blk_mq_rq_ctx_init() to take care of the added fields.
84 struct request_queue *q;
85 struct blk_mq_ctx *mq_ctx;
86 struct blk_mq_hw_ctx *mq_hctx;
88 blk_opf_t cmd_flags; /* op and common flags */
96 /* the following two fields are internal, NEVER access directly */
97 unsigned int __data_len; /* total data len */
98 sector_t __sector; /* sector cursor */
104 struct list_head queuelist;
105 struct request *rq_next;
108 struct block_device *part;
109 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
110 /* Time that the first bio started allocating this request. */
113 /* Time that this request was allocated for this IO. */
115 /* Time that I/O was submitted to the device. */
116 u64 io_start_time_ns;
118 #ifdef CONFIG_BLK_WBT
119 unsigned short wbt_flags;
122 * rq sectors used for blk stats. It has the same value
123 * with blk_rq_sectors(rq), except that it never be zeroed
126 unsigned short stats_sectors;
129 * Number of scatter-gather DMA addr+len pairs after
130 * physical address coalescing is performed.
132 unsigned short nr_phys_segments;
134 #ifdef CONFIG_BLK_DEV_INTEGRITY
135 unsigned short nr_integrity_segments;
138 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
139 struct bio_crypt_ctx *crypt_ctx;
140 struct blk_crypto_keyslot *crypt_keyslot;
143 unsigned short write_hint;
144 unsigned short ioprio;
146 enum mq_rq_state state;
149 unsigned long deadline;
152 * The hash is used inside the scheduler, and killed once the
153 * request reaches the dispatch list. The ipi_list is only used
154 * to queue the request for softirq completion, which is long
155 * after the request has been unhashed (and even removed from
156 * the dispatch list).
159 struct hlist_node hash; /* merge hash */
160 struct llist_node ipi_list;
164 * The rb_node is only used inside the io scheduler, requests
165 * are pruned when moved to the dispatch queue. So let the
166 * completion_data share space with the rb_node.
169 struct rb_node rb_node; /* sort/lookup */
170 struct bio_vec special_vec;
171 void *completion_data;
176 * Three pointers are available for the IO schedulers, if they need
177 * more they have to dynamically allocate it. Flush requests are
178 * never put on the IO scheduler. So let the flush fields share
179 * space with the elevator data.
189 struct list_head list;
190 rq_end_io_fn *saved_end_io;
195 struct __call_single_data csd;
200 * completion callback.
202 rq_end_io_fn *end_io;
206 static inline enum req_op req_op(const struct request *req)
208 return req->cmd_flags & REQ_OP_MASK;
211 static inline bool blk_rq_is_passthrough(struct request *rq)
213 return blk_op_is_passthrough(req_op(rq));
216 static inline unsigned short req_get_ioprio(struct request *req)
221 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
223 #define rq_dma_dir(rq) \
224 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
226 #define rq_list_add(listptr, rq) do { \
227 (rq)->rq_next = *(listptr); \
231 #define rq_list_add_tail(lastpptr, rq) do { \
232 (rq)->rq_next = NULL; \
234 *(lastpptr) = &rq->rq_next; \
237 #define rq_list_pop(listptr) \
239 struct request *__req = NULL; \
240 if ((listptr) && *(listptr)) { \
241 __req = *(listptr); \
242 *(listptr) = __req->rq_next; \
247 #define rq_list_peek(listptr) \
249 struct request *__req = NULL; \
250 if ((listptr) && *(listptr)) \
251 __req = *(listptr); \
255 #define rq_list_for_each(listptr, pos) \
256 for (pos = rq_list_peek((listptr)); pos; pos = rq_list_next(pos))
258 #define rq_list_for_each_safe(listptr, pos, nxt) \
259 for (pos = rq_list_peek((listptr)), nxt = rq_list_next(pos); \
260 pos; pos = nxt, nxt = pos ? rq_list_next(pos) : NULL)
262 #define rq_list_next(rq) (rq)->rq_next
263 #define rq_list_empty(list) ((list) == (struct request *) NULL)
266 * rq_list_move() - move a struct request from one list to another
267 * @src: The source list @rq is currently in
268 * @dst: The destination list that @rq will be appended to
269 * @rq: The request to move
270 * @prev: The request preceding @rq in @src (NULL if @rq is the head)
272 static inline void rq_list_move(struct request **src, struct request **dst,
273 struct request *rq, struct request *prev)
276 prev->rq_next = rq->rq_next;
279 rq_list_add(dst, rq);
283 * enum blk_eh_timer_return - How the timeout handler should proceed
284 * @BLK_EH_DONE: The block driver completed the command or will complete it at
286 * @BLK_EH_RESET_TIMER: Reset the request timer and continue waiting for the
287 * request to complete.
289 enum blk_eh_timer_return {
294 #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
295 #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
298 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware
301 struct blk_mq_hw_ctx {
303 /** @lock: Protects the dispatch list. */
306 * @dispatch: Used for requests that are ready to be
307 * dispatched to the hardware but for some reason (e.g. lack of
308 * resources) could not be sent to the hardware. As soon as the
309 * driver can send new requests, requests at this list will
310 * be sent first for a fairer dispatch.
312 struct list_head dispatch;
314 * @state: BLK_MQ_S_* flags. Defines the state of the hw
315 * queue (active, scheduled to restart, stopped).
318 } ____cacheline_aligned_in_smp;
321 * @run_work: Used for scheduling a hardware queue run at a later time.
323 struct delayed_work run_work;
324 /** @cpumask: Map of available CPUs where this hctx can run. */
325 cpumask_var_t cpumask;
327 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU
328 * selection from @cpumask.
332 * @next_cpu_batch: Counter of how many works left in the batch before
333 * changing to the next CPU.
337 /** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */
341 * @sched_data: Pointer owned by the IO scheduler attached to a request
342 * queue. It's up to the IO scheduler how to use this pointer.
346 * @queue: Pointer to the request queue that owns this hardware context.
348 struct request_queue *queue;
349 /** @fq: Queue of requests that need to perform a flush operation. */
350 struct blk_flush_queue *fq;
353 * @driver_data: Pointer to data owned by the block driver that created
359 * @ctx_map: Bitmap for each software queue. If bit is on, there is a
360 * pending request in that software queue.
362 struct sbitmap ctx_map;
365 * @dispatch_from: Software queue to be used when no scheduler was
368 struct blk_mq_ctx *dispatch_from;
370 * @dispatch_busy: Number used by blk_mq_update_dispatch_busy() to
371 * decide if the hw_queue is busy using Exponential Weighted Moving
374 unsigned int dispatch_busy;
376 /** @type: HCTX_TYPE_* flags. Type of hardware queue. */
378 /** @nr_ctx: Number of software queues. */
379 unsigned short nr_ctx;
380 /** @ctxs: Array of software queues. */
381 struct blk_mq_ctx **ctxs;
383 /** @dispatch_wait_lock: Lock for dispatch_wait queue. */
384 spinlock_t dispatch_wait_lock;
386 * @dispatch_wait: Waitqueue to put requests when there is no tag
387 * available at the moment, to wait for another try in the future.
389 wait_queue_entry_t dispatch_wait;
392 * @wait_index: Index of next available dispatch_wait queue to insert
398 * @tags: Tags owned by the block driver. A tag at this set is only
399 * assigned when a request is dispatched from a hardware queue.
401 struct blk_mq_tags *tags;
403 * @sched_tags: Tags owned by I/O scheduler. If there is an I/O
404 * scheduler associated with a request queue, a tag is assigned when
405 * that request is allocated. Else, this member is not used.
407 struct blk_mq_tags *sched_tags;
409 /** @queued: Number of queued requests. */
410 unsigned long queued;
411 /** @run: Number of dispatched requests. */
414 /** @numa_node: NUMA node the storage adapter has been connected to. */
415 unsigned int numa_node;
416 /** @queue_num: Index of this hardware queue. */
417 unsigned int queue_num;
420 * @nr_active: Number of active requests. Only used when a tag set is
421 * shared across request queues.
425 /** @cpuhp_online: List to store request if CPU is going to die */
426 struct hlist_node cpuhp_online;
427 /** @cpuhp_dead: List to store request if some CPU die. */
428 struct hlist_node cpuhp_dead;
429 /** @kobj: Kernel object for sysfs. */
432 #ifdef CONFIG_BLK_DEBUG_FS
434 * @debugfs_dir: debugfs directory for this hardware queue. Named
435 * as cpu<cpu_number>.
437 struct dentry *debugfs_dir;
438 /** @sched_debugfs_dir: debugfs directory for the scheduler. */
439 struct dentry *sched_debugfs_dir;
443 * @hctx_list: if this hctx is not in use, this is an entry in
444 * q->unused_hctx_list.
446 struct list_head hctx_list;
450 * struct blk_mq_queue_map - Map software queues to hardware queues
451 * @mq_map: CPU ID to hardware queue index map. This is an array
452 * with nr_cpu_ids elements. Each element has a value in the range
453 * [@queue_offset, @queue_offset + @nr_queues).
454 * @nr_queues: Number of hardware queues to map CPU IDs onto.
455 * @queue_offset: First hardware queue to map onto. Used by the PCIe NVMe
456 * driver to map each hardware queue type (enum hctx_type) onto a distinct
457 * set of hardware queues.
459 struct blk_mq_queue_map {
460 unsigned int *mq_map;
461 unsigned int nr_queues;
462 unsigned int queue_offset;
466 * enum hctx_type - Type of hardware queue
467 * @HCTX_TYPE_DEFAULT: All I/O not otherwise accounted for.
468 * @HCTX_TYPE_READ: Just for READ I/O.
469 * @HCTX_TYPE_POLL: Polled I/O of any kind.
470 * @HCTX_MAX_TYPES: Number of types of hctx.
481 * struct blk_mq_tag_set - tag set that can be shared between request queues
482 * @map: One or more ctx -> hctx mappings. One map exists for each
483 * hardware queue type (enum hctx_type) that the driver wishes
484 * to support. There are no restrictions on maps being of the
485 * same size, and it's perfectly legal to share maps between
487 * @nr_maps: Number of elements in the @map array. A number in the range
488 * [1, HCTX_MAX_TYPES].
489 * @ops: Pointers to functions that implement block driver behavior.
490 * @nr_hw_queues: Number of hardware queues supported by the block driver that
491 * owns this data structure.
492 * @queue_depth: Number of tags per hardware queue, reserved tags included.
493 * @reserved_tags: Number of tags to set aside for BLK_MQ_REQ_RESERVED tag
495 * @cmd_size: Number of additional bytes to allocate per request. The block
496 * driver owns these additional bytes.
497 * @numa_node: NUMA node the storage adapter has been connected to.
498 * @timeout: Request processing timeout in jiffies.
499 * @flags: Zero or more BLK_MQ_F_* flags.
500 * @driver_data: Pointer to data owned by the block driver that created this
502 * @tags: Tag sets. One tag set per hardware queue. Has @nr_hw_queues
505 * Shared set of tags. Has @nr_hw_queues elements. If set,
506 * shared by all @tags.
507 * @tag_list_lock: Serializes tag_list accesses.
508 * @tag_list: List of the request queues that use this tag set. See also
509 * request_queue.tag_set_list.
511 struct blk_mq_tag_set {
512 struct blk_mq_queue_map map[HCTX_MAX_TYPES];
513 unsigned int nr_maps;
514 const struct blk_mq_ops *ops;
515 unsigned int nr_hw_queues;
516 unsigned int queue_depth;
517 unsigned int reserved_tags;
518 unsigned int cmd_size;
520 unsigned int timeout;
524 struct blk_mq_tags **tags;
526 struct blk_mq_tags *shared_tags;
528 struct mutex tag_list_lock;
529 struct list_head tag_list;
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 {
543 typedef bool (busy_tag_iter_fn)(struct request *, void *);
546 * struct blk_mq_ops - Callback functions that implements block driver
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
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 request **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
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
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 *,
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);
663 BLK_MQ_F_SHOULD_MERGE = 1 << 0,
664 BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1,
666 * Set when this device requires underlying blk-mq device for
669 BLK_MQ_F_STACKING = 1 << 2,
670 BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3,
671 BLK_MQ_F_BLOCKING = 1 << 5,
672 /* Do not allow an I/O scheduler to be configured. */
673 BLK_MQ_F_NO_SCHED = 1 << 6,
675 * Select 'none' during queue registration in case of a single hwq
676 * or shared hwqs instead of 'mq-deadline'.
678 BLK_MQ_F_NO_SCHED_BY_DEFAULT = 1 << 7,
679 BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,
680 BLK_MQ_F_ALLOC_POLICY_BITS = 1,
682 BLK_MQ_S_STOPPED = 0,
683 BLK_MQ_S_TAG_ACTIVE = 1,
684 BLK_MQ_S_SCHED_RESTART = 2,
686 /* hw queue is inactive after all its CPUs become offline */
687 BLK_MQ_S_INACTIVE = 3,
689 BLK_MQ_MAX_DEPTH = 10240,
691 BLK_MQ_CPU_WORK_BATCH = 8,
693 #define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \
694 ((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \
695 ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1))
696 #define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \
697 ((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \
698 << BLK_MQ_F_ALLOC_POLICY_START_BIT)
700 #define BLK_MQ_NO_HCTX_IDX (-1U)
702 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
703 struct lock_class_key *lkclass);
704 #define blk_mq_alloc_disk(set, queuedata) \
706 static struct lock_class_key __key; \
708 __blk_mq_alloc_disk(set, queuedata, &__key); \
710 struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
711 struct lock_class_key *lkclass);
712 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *);
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);
725 bool blk_mq_queue_inflight(struct request_queue *q);
728 /* return when out of requests */
729 BLK_MQ_REQ_NOWAIT = (__force blk_mq_req_flags_t)(1 << 0),
730 /* allocate from reserved pool */
731 BLK_MQ_REQ_RESERVED = (__force blk_mq_req_flags_t)(1 << 1),
733 BLK_MQ_REQ_PM = (__force blk_mq_req_flags_t)(1 << 2),
736 struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
737 blk_mq_req_flags_t flags);
738 struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
739 blk_opf_t opf, blk_mq_req_flags_t flags,
740 unsigned int hctx_idx);
743 * Tag address space map.
746 unsigned int nr_tags;
747 unsigned int nr_reserved_tags;
749 atomic_t active_queues;
751 struct sbitmap_queue bitmap_tags;
752 struct sbitmap_queue breserved_tags;
754 struct request **rqs;
755 struct request **static_rqs;
756 struct list_head page_list;
759 * used to clear request reference in rqs[] before freeing one
765 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,
768 if (tag < tags->nr_tags) {
769 prefetch(tags->rqs[tag]);
770 return tags->rqs[tag];
777 BLK_MQ_UNIQUE_TAG_BITS = 16,
778 BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
781 u32 blk_mq_unique_tag(struct request *rq);
783 static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
785 return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
788 static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
790 return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
794 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
795 * @rq: target request.
797 static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
799 return READ_ONCE(rq->state);
802 static inline int blk_mq_request_started(struct request *rq)
804 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
807 static inline int blk_mq_request_completed(struct request *rq)
809 return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
814 * Set the state to complete when completing a request from inside ->queue_rq.
815 * This is used by drivers that want to ensure special complete actions that
816 * need access to the request are called on failure, e.g. by nvme for
819 static inline void blk_mq_set_request_complete(struct request *rq)
821 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
825 * Complete the request directly instead of deferring it to softirq or
826 * completing it another CPU. Useful in preemptible instead of an interrupt.
828 static inline void blk_mq_complete_request_direct(struct request *rq,
829 void (*complete)(struct request *rq))
831 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
835 void blk_mq_start_request(struct request *rq);
836 void blk_mq_end_request(struct request *rq, blk_status_t error);
837 void __blk_mq_end_request(struct request *rq, blk_status_t error);
838 void blk_mq_end_request_batch(struct io_comp_batch *ib);
841 * Only need start/end time stamping if we have iostat or
842 * blk stats enabled, or using an IO scheduler.
844 static inline bool blk_mq_need_time_stamp(struct request *rq)
846 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_ELV));
849 static inline bool blk_mq_is_reserved_rq(struct request *rq)
851 return rq->rq_flags & RQF_RESV;
855 * Batched completions only work when there is no I/O error and no special
858 static inline bool blk_mq_add_to_batch(struct request *req,
859 struct io_comp_batch *iob, int ioerror,
860 void (*complete)(struct io_comp_batch *))
862 if (!iob || (req->rq_flags & RQF_ELV) || ioerror ||
863 (req->end_io && !blk_rq_is_passthrough(req)))
867 iob->complete = complete;
868 else if (iob->complete != complete)
870 iob->need_ts |= blk_mq_need_time_stamp(req);
871 rq_list_add(&iob->req_list, req);
875 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
876 void blk_mq_kick_requeue_list(struct request_queue *q);
877 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
878 void blk_mq_complete_request(struct request *rq);
879 bool blk_mq_complete_request_remote(struct request *rq);
880 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
881 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
882 void blk_mq_stop_hw_queues(struct request_queue *q);
883 void blk_mq_start_hw_queues(struct request_queue *q);
884 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
885 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
886 void blk_mq_quiesce_queue(struct request_queue *q);
887 void blk_mq_wait_quiesce_done(struct request_queue *q);
888 void blk_mq_unquiesce_queue(struct request_queue *q);
889 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
890 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
891 void blk_mq_run_hw_queues(struct request_queue *q, bool async);
892 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
893 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
894 busy_tag_iter_fn *fn, void *priv);
895 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
896 void blk_mq_freeze_queue(struct request_queue *q);
897 void blk_mq_unfreeze_queue(struct request_queue *q);
898 void blk_freeze_queue_start(struct request_queue *q);
899 void blk_mq_freeze_queue_wait(struct request_queue *q);
900 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
901 unsigned long timeout);
903 void blk_mq_map_queues(struct blk_mq_queue_map *qmap);
904 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
906 void blk_mq_quiesce_queue_nowait(struct request_queue *q);
908 unsigned int blk_mq_rq_cpu(struct request *rq);
910 bool __blk_should_fake_timeout(struct request_queue *q);
911 static inline bool blk_should_fake_timeout(struct request_queue *q)
913 if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) &&
914 test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
915 return __blk_should_fake_timeout(q);
920 * blk_mq_rq_from_pdu - cast a PDU to a request
921 * @pdu: the PDU (Protocol Data Unit) to be casted
925 * Driver command data is immediately after the request. So subtract request
926 * size to get back to the original request.
928 static inline struct request *blk_mq_rq_from_pdu(void *pdu)
930 return pdu - sizeof(struct request);
934 * blk_mq_rq_to_pdu - cast a request to a PDU
935 * @rq: the request to be casted
937 * Return: pointer to the PDU
939 * Driver command data is immediately after the request. So add request to get
942 static inline void *blk_mq_rq_to_pdu(struct request *rq)
947 #define queue_for_each_hw_ctx(q, hctx, i) \
948 xa_for_each(&(q)->hctx_table, (i), (hctx))
950 #define hctx_for_each_ctx(hctx, ctx, i) \
951 for ((i) = 0; (i) < (hctx)->nr_ctx && \
952 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
954 static inline void blk_mq_cleanup_rq(struct request *rq)
956 if (rq->q->mq_ops->cleanup_rq)
957 rq->q->mq_ops->cleanup_rq(rq);
960 static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio,
961 unsigned int nr_segs)
963 rq->nr_phys_segments = nr_segs;
964 rq->__data_len = bio->bi_iter.bi_size;
965 rq->bio = rq->biotail = bio;
966 rq->ioprio = bio_prio(bio);
969 void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
970 struct lock_class_key *key);
972 static inline bool rq_is_sync(struct request *rq)
974 return op_is_sync(rq->cmd_flags);
977 void blk_rq_init(struct request_queue *q, struct request *rq);
978 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
979 struct bio_set *bs, gfp_t gfp_mask,
980 int (*bio_ctr)(struct bio *, struct bio *, void *), void *data);
981 void blk_rq_unprep_clone(struct request *rq);
982 blk_status_t blk_insert_cloned_request(struct request *rq);
986 unsigned long offset;
987 unsigned short page_order;
988 unsigned short nr_entries;
993 int blk_rq_map_user(struct request_queue *, struct request *,
994 struct rq_map_data *, void __user *, unsigned long, gfp_t);
995 int blk_rq_map_user_io(struct request *, struct rq_map_data *,
996 void __user *, unsigned long, gfp_t, bool, int, bool, int);
997 int blk_rq_map_user_iov(struct request_queue *, struct request *,
998 struct rq_map_data *, const struct iov_iter *, gfp_t);
999 int blk_rq_unmap_user(struct bio *);
1000 int blk_rq_map_kern(struct request_queue *, struct request *, void *,
1001 unsigned int, gfp_t);
1002 int blk_rq_append_bio(struct request *rq, struct bio *bio);
1003 void blk_execute_rq_nowait(struct request *rq, bool at_head);
1004 blk_status_t blk_execute_rq(struct request *rq, bool at_head);
1005 bool blk_rq_is_poll(struct request *rq);
1007 struct req_iterator {
1008 struct bvec_iter iter;
1012 #define __rq_for_each_bio(_bio, rq) \
1014 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
1016 #define rq_for_each_segment(bvl, _rq, _iter) \
1017 __rq_for_each_bio(_iter.bio, _rq) \
1018 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
1020 #define rq_for_each_bvec(bvl, _rq, _iter) \
1021 __rq_for_each_bio(_iter.bio, _rq) \
1022 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
1024 #define rq_iter_last(bvec, _iter) \
1025 (_iter.bio->bi_next == NULL && \
1026 bio_iter_last(bvec, _iter.iter))
1029 * blk_rq_pos() : the current sector
1030 * blk_rq_bytes() : bytes left in the entire request
1031 * blk_rq_cur_bytes() : bytes left in the current segment
1032 * blk_rq_sectors() : sectors left in the entire request
1033 * blk_rq_cur_sectors() : sectors left in the current segment
1034 * blk_rq_stats_sectors() : sectors of the entire request used for stats
1036 static inline sector_t blk_rq_pos(const struct request *rq)
1038 return rq->__sector;
1041 static inline unsigned int blk_rq_bytes(const struct request *rq)
1043 return rq->__data_len;
1046 static inline int blk_rq_cur_bytes(const struct request *rq)
1050 if (!bio_has_data(rq->bio)) /* dataless requests such as discard */
1051 return rq->bio->bi_iter.bi_size;
1052 return bio_iovec(rq->bio).bv_len;
1055 static inline unsigned int blk_rq_sectors(const struct request *rq)
1057 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1060 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1062 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1065 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
1067 return rq->stats_sectors;
1071 * Some commands like WRITE SAME have a payload or data transfer size which
1072 * is different from the size of the request. Any driver that supports such
1073 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1074 * calculate the data transfer size.
1076 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1078 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1079 return rq->special_vec.bv_len;
1080 return blk_rq_bytes(rq);
1084 * Return the first full biovec in the request. The caller needs to check that
1085 * there are any bvecs before calling this helper.
1087 static inline struct bio_vec req_bvec(struct request *rq)
1089 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1090 return rq->special_vec;
1091 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1094 static inline unsigned int blk_rq_count_bios(struct request *rq)
1096 unsigned int nr_bios = 0;
1099 __rq_for_each_bio(bio, rq)
1105 void blk_steal_bios(struct bio_list *list, struct request *rq);
1108 * Request completion related functions.
1110 * blk_update_request() completes given number of bytes and updates
1111 * the request without completing it.
1113 bool blk_update_request(struct request *rq, blk_status_t error,
1114 unsigned int nr_bytes);
1115 void blk_abort_request(struct request *);
1118 * Number of physical segments as sent to the device.
1120 * Normally this is the number of discontiguous data segments sent by the
1121 * submitter. But for data-less command like discard we might have no
1122 * actual data segments submitted, but the driver might have to add it's
1123 * own special payload. In that case we still return 1 here so that this
1124 * special payload will be mapped.
1126 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1128 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1130 return rq->nr_phys_segments;
1134 * Number of discard segments (or ranges) the driver needs to fill in.
1135 * Each discard bio merged into a request is counted as one segment.
1137 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1139 return max_t(unsigned short, rq->nr_phys_segments, 1);
1142 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1143 struct scatterlist *sglist, struct scatterlist **last_sg);
1144 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1145 struct scatterlist *sglist)
1147 struct scatterlist *last_sg = NULL;
1149 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1151 void blk_dump_rq_flags(struct request *, char *);
1153 #ifdef CONFIG_BLK_DEV_ZONED
1154 static inline unsigned int blk_rq_zone_no(struct request *rq)
1156 return disk_zone_no(rq->q->disk, blk_rq_pos(rq));
1159 static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
1161 return disk_zone_is_seq(rq->q->disk, blk_rq_pos(rq));
1164 bool blk_req_needs_zone_write_lock(struct request *rq);
1165 bool blk_req_zone_write_trylock(struct request *rq);
1166 void __blk_req_zone_write_lock(struct request *rq);
1167 void __blk_req_zone_write_unlock(struct request *rq);
1169 static inline void blk_req_zone_write_lock(struct request *rq)
1171 if (blk_req_needs_zone_write_lock(rq))
1172 __blk_req_zone_write_lock(rq);
1175 static inline void blk_req_zone_write_unlock(struct request *rq)
1177 if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
1178 __blk_req_zone_write_unlock(rq);
1181 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1183 return rq->q->disk->seq_zones_wlock &&
1184 test_bit(blk_rq_zone_no(rq), rq->q->disk->seq_zones_wlock);
1187 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1189 if (!blk_req_needs_zone_write_lock(rq))
1191 return !blk_req_zone_is_write_locked(rq);
1193 #else /* CONFIG_BLK_DEV_ZONED */
1194 static inline bool blk_req_needs_zone_write_lock(struct request *rq)
1199 static inline void blk_req_zone_write_lock(struct request *rq)
1203 static inline void blk_req_zone_write_unlock(struct request *rq)
1206 static inline bool blk_req_zone_is_write_locked(struct request *rq)
1211 static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
1215 #endif /* CONFIG_BLK_DEV_ZONED */
1217 #endif /* BLK_MQ_H */