1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef BLK_MQ_H
3 #define BLK_MQ_H
4
5 #include <linux/blkdev.h>
6 #include <linux/sbitmap.h>
7 #include <linux/lockdep.h>
8 #include <linux/scatterlist.h>
9 #include <linux/prefetch.h>
10 #include <linux/srcu.h>
11 #include <linux/rw_hint.h>
12
13 struct blk_mq_tags;
14 struct blk_flush_queue;
15
16 #define BLKDEV_MIN_RQ 4
17 #define BLKDEV_DEFAULT_RQ 128
18
19 enum rq_end_io_ret {
20 RQ_END_IO_NONE,
21 RQ_END_IO_FREE,
22 };
23
24 typedef enum rq_end_io_ret (rq_end_io_fn)(struct request *, blk_status_t);
25
26 /*
27 * request flags */
28 typedef __u32 __bitwise req_flags_t;
29
30 /* Keep rqf_name[] in sync with the definitions below */
31 enum rqf_flags {
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
65 };
66
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))
85
86 /* flags that prevent us from merging requests: */
87 #define RQF_NOMERGE_FLAGS \
88 (RQF_STARTED | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
89
90 enum mq_rq_state {
91 MQ_RQ_IDLE = 0,
92 MQ_RQ_IN_FLIGHT = 1,
93 MQ_RQ_COMPLETE = 2,
94 };
95
96 /*
97 * Try to put the fields that are referenced together in the same cacheline.
98 *
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.
101 */
102 struct request {
103 struct request_queue *q;
104 struct blk_mq_ctx *mq_ctx;
105 struct blk_mq_hw_ctx *mq_hctx;
106
107 blk_opf_t cmd_flags; /* op and common flags */
108 req_flags_t rq_flags;
109
110 int tag;
111 int internal_tag;
112
113 unsigned int timeout;
114
115 /* the following two fields are internal, NEVER access directly */
116 unsigned int __data_len; /* total data len */
117 sector_t __sector; /* sector cursor */
118
119 struct bio *bio;
120 struct bio *biotail;
121
122 union {
123 struct list_head queuelist;
124 struct request *rq_next;
125 };
126
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;
136
137 #ifdef CONFIG_BLK_WBT
138 unsigned short wbt_flags;
139 #endif
140 /*
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.
144 */
145 unsigned short stats_sectors;
146
147 /*
148 * Number of scatter-gather DMA addr+len pairs after
149 * physical address coalescing is performed.
150 */
151 unsigned short nr_phys_segments;
152 unsigned short nr_integrity_segments;
153
154 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
155 struct bio_crypt_ctx *crypt_ctx;
156 struct blk_crypto_keyslot *crypt_keyslot;
157 #endif
158
159 enum mq_rq_state state;
160 atomic_t ref;
161
162 unsigned long deadline;
163
164 /*
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).
170 */
171 union {
172 struct hlist_node hash; /* merge hash */
173 struct llist_node ipi_list;
174 };
175
176 /*
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.
181 */
182 union {
183 struct rb_node rb_node; /* sort/lookup */
184 struct bio_vec special_vec;
185 };
186
187 /*
188 * Three pointers are available for the IO schedulers, if they need
189 * more they have to dynamically allocate it.
190 */
191 struct {
192 struct io_cq *icq;
193 void *priv[2];
194 } elv;
195
196 struct {
197 unsigned int seq;
198 rq_end_io_fn *saved_end_io;
199 } flush;
200
201 u64 fifo_time;
202
203 /*
204 * completion callback.
205 */
206 rq_end_io_fn *end_io;
207 void *end_io_data;
208 };
209
req_op(const struct request * req)210 static inline enum req_op req_op(const struct request *req)
211 {
212 return req->cmd_flags & REQ_OP_MASK;
213 }
214
blk_rq_is_passthrough(struct request * rq)215 static inline bool blk_rq_is_passthrough(struct request *rq)
216 {
217 return blk_op_is_passthrough(rq->cmd_flags);
218 }
219
req_get_ioprio(struct request * req)220 static inline unsigned short req_get_ioprio(struct request *req)
221 {
222 if (req->bio)
223 return req->bio->bi_ioprio;
224 return 0;
225 }
226
227 #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
228
229 #define rq_dma_dir(rq) \
230 (op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
231
rq_list_empty(const struct rq_list * rl)232 static inline int rq_list_empty(const struct rq_list *rl)
233 {
234 return rl->head == NULL;
235 }
236
rq_list_init(struct rq_list * rl)237 static inline void rq_list_init(struct rq_list *rl)
238 {
239 rl->head = NULL;
240 rl->tail = NULL;
241 }
242
rq_list_add_tail(struct rq_list * rl,struct request * rq)243 static inline void rq_list_add_tail(struct rq_list *rl, struct request *rq)
244 {
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;
251 }
252
rq_list_add_head(struct rq_list * rl,struct request * rq)253 static inline void rq_list_add_head(struct rq_list *rl, struct request *rq)
254 {
255 rq->rq_next = rl->head;
256 rl->head = rq;
257 if (!rl->tail)
258 rl->tail = rq;
259 }
260
rq_list_pop(struct rq_list * rl)261 static inline struct request *rq_list_pop(struct rq_list *rl)
262 {
263 struct request *rq = rl->head;
264
265 if (rq) {
266 rl->head = rl->head->rq_next;
267 if (!rl->head)
268 rl->tail = NULL;
269 rq->rq_next = NULL;
270 }
271
272 return rq;
273 }
274
rq_list_peek(struct rq_list * rl)275 static inline struct request *rq_list_peek(struct rq_list *rl)
276 {
277 return rl->head;
278 }
279
280 #define rq_list_for_each(rl, pos) \
281 for (pos = rq_list_peek((rl)); (pos); pos = pos->rq_next)
282
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)
286
287 /**
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.
293 */
294 enum blk_eh_timer_return {
295 BLK_EH_DONE,
296 BLK_EH_RESET_TIMER,
297 };
298
299 /**
300 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware
301 * block device
302 */
303 struct blk_mq_hw_ctx {
304 struct {
305 /** @lock: Protects the dispatch list. */
306 spinlock_t lock;
307 /**
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.
313 */
314 struct list_head dispatch;
315 /**
316 * @state: BLK_MQ_S_* flags. Defines the state of the hw
317 * queue (active, scheduled to restart, stopped).
318 */
319 unsigned long state;
320 } ____cacheline_aligned_in_smp;
321
322 /**
323 * @run_work: Used for scheduling a hardware queue run at a later time.
324 */
325 struct delayed_work run_work;
326 /** @cpumask: Map of available CPUs where this hctx can run. */
327 cpumask_var_t cpumask;
328 /**
329 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU
330 * selection from @cpumask.
331 */
332 int next_cpu;
333 /**
334 * @next_cpu_batch: Counter of how many works left in the batch before
335 * changing to the next CPU.
336 */
337 int next_cpu_batch;
338
339 /** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */
340 unsigned long flags;
341
342 /**
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.
345 */
346 void *sched_data;
347 /**
348 * @queue: Pointer to the request queue that owns this hardware context.
349 */
350 struct request_queue *queue;
351 /** @fq: Queue of requests that need to perform a flush operation. */
352 struct blk_flush_queue *fq;
353
354 /**
355 * @driver_data: Pointer to data owned by the block driver that created
356 * this hctx
357 */
358 void *driver_data;
359
360 /**
361 * @ctx_map: Bitmap for each software queue. If bit is on, there is a
362 * pending request in that software queue.
363 */
364 struct sbitmap ctx_map;
365
366 /**
367 * @dispatch_from: Software queue to be used when no scheduler was
368 * selected.
369 */
370 struct blk_mq_ctx *dispatch_from;
371 /**
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.
375 */
376 unsigned int dispatch_busy;
377
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;
384
385 /** @dispatch_wait_lock: Lock for dispatch_wait queue. */
386 spinlock_t dispatch_wait_lock;
387 /**
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.
390 */
391 wait_queue_entry_t dispatch_wait;
392
393 /**
394 * @wait_index: Index of next available dispatch_wait queue to insert
395 * requests.
396 */
397 atomic_t wait_index;
398
399 /**
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.
402 */
403 struct blk_mq_tags *tags;
404 /**
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.
408 */
409 struct blk_mq_tags *sched_tags;
410
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;
415
416 /**
417 * @nr_active: Number of active requests. Only used when a tag set is
418 * shared across request queues.
419 */
420 atomic_t nr_active;
421
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;
428
429 #ifdef CONFIG_BLK_DEBUG_FS
430 /**
431 * @debugfs_dir: debugfs directory for this hardware queue. Named
432 * as cpu<cpu_number>.
433 */
434 struct dentry *debugfs_dir;
435 /** @sched_debugfs_dir: debugfs directory for the scheduler. */
436 struct dentry *sched_debugfs_dir;
437 #endif
438
439 /**
440 * @hctx_list: if this hctx is not in use, this is an entry in
441 * q->unused_hctx_list.
442 */
443 struct list_head hctx_list;
444 };
445
446 /**
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.
455 */
456 struct blk_mq_queue_map {
457 unsigned int *mq_map;
458 unsigned int nr_queues;
459 unsigned int queue_offset;
460 };
461
462 /**
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.
468 */
469 enum hctx_type {
470 HCTX_TYPE_DEFAULT,
471 HCTX_TYPE_READ,
472 HCTX_TYPE_POLL,
473
474 HCTX_MAX_TYPES,
475 };
476
477 /**
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).
509 */
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;
522
523 struct blk_mq_tags **tags;
524
525 struct blk_mq_tags *shared_tags;
526
527 struct mutex tag_list_lock;
528 struct list_head tag_list;
529 struct srcu_struct *srcu;
530 };
531
532 /**
533 * struct blk_mq_queue_data - Data about a request inserted in a queue
534 *
535 * @rq: Request pointer.
536 * @last: If it is the last request in the queue.
537 */
538 struct blk_mq_queue_data {
539 struct request *rq;
540 bool last;
541 };
542
543 typedef bool (busy_tag_iter_fn)(struct request *, void *);
544
545 /**
546 * struct blk_mq_ops - Callback functions that implements block driver
547 * behaviour.
548 */
549 struct blk_mq_ops {
550 /**
551 * @queue_rq: Queue a new request from block IO.
552 */
553 blk_status_t (*queue_rq)(struct blk_mq_hw_ctx *,
554 const struct blk_mq_queue_data *);
555
556 /**
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).
562 */
563 void (*commit_rqs)(struct blk_mq_hw_ctx *);
564
565 /**
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.
570 */
571 void (*queue_rqs)(struct rq_list *rqlist);
572
573 /**
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.
578 */
579 int (*get_budget)(struct request_queue *);
580
581 /**
582 * @put_budget: Release the reserved budget.
583 */
584 void (*put_budget)(struct request_queue *, int);
585
586 /**
587 * @set_rq_budget_token: store rq's budget token
588 */
589 void (*set_rq_budget_token)(struct request *, int);
590 /**
591 * @get_rq_budget_token: retrieve rq's budget token
592 */
593 int (*get_rq_budget_token)(struct request *);
594
595 /**
596 * @timeout: Called on request timeout.
597 */
598 enum blk_eh_timer_return (*timeout)(struct request *);
599
600 /**
601 * @poll: Called to poll for completion of a specific tag.
602 */
603 int (*poll)(struct blk_mq_hw_ctx *, struct io_comp_batch *);
604
605 /**
606 * @complete: Mark the request as complete.
607 */
608 void (*complete)(struct request *);
609
610 /**
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.
614 */
615 int (*init_hctx)(struct blk_mq_hw_ctx *, void *, unsigned int);
616 /**
617 * @exit_hctx: Ditto for exit/teardown.
618 */
619 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
620
621 /**
622 * @init_request: Called for every command allocated by the block layer
623 * to allow the driver to set up driver specific data.
624 *
625 * Tag greater than or equal to queue_depth is for setting up
626 * flush request.
627 */
628 int (*init_request)(struct blk_mq_tag_set *set, struct request *,
629 unsigned int, unsigned int);
630 /**
631 * @exit_request: Ditto for exit/teardown.
632 */
633 void (*exit_request)(struct blk_mq_tag_set *set, struct request *,
634 unsigned int);
635
636 /**
637 * @cleanup_rq: Called before freeing one request which isn't completed
638 * yet, and usually for freeing the driver private data.
639 */
640 void (*cleanup_rq)(struct request *);
641
642 /**
643 * @busy: If set, returns whether or not this queue currently is busy.
644 */
645 bool (*busy)(struct request_queue *);
646
647 /**
648 * @map_queues: This allows drivers specify their own queue mapping by
649 * overriding the setup-time function that builds the mq_map.
650 */
651 void (*map_queues)(struct blk_mq_tag_set *set);
652
653 #ifdef CONFIG_BLK_DEBUG_FS
654 /**
655 * @show_rq: Used by the debugfs implementation to show driver-specific
656 * information about a request.
657 */
658 void (*show_rq)(struct seq_file *m, struct request *rq);
659 #endif
660 };
661
662 /* Keep hctx_flag_name[] in sync with the definitions below */
663 enum {
664 BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1,
665 /*
666 * Set when this device requires underlying blk-mq device for
667 * completing IO:
668 */
669 BLK_MQ_F_STACKING = 1 << 2,
670 BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3,
671 BLK_MQ_F_BLOCKING = 1 << 4,
672
673 /*
674 * Alloc tags on a round-robin base instead of the first available one.
675 */
676 BLK_MQ_F_TAG_RR = 1 << 5,
677
678 /*
679 * Select 'none' during queue registration in case of a single hwq
680 * or shared hwqs instead of 'mq-deadline'.
681 */
682 BLK_MQ_F_NO_SCHED_BY_DEFAULT = 1 << 6,
683
684 BLK_MQ_F_MAX = 1 << 7,
685 };
686
687 #define BLK_MQ_MAX_DEPTH (10240)
688 #define BLK_MQ_NO_HCTX_IDX (-1U)
689
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
698 };
699
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; \
706 \
707 __blk_mq_alloc_disk(set, lim, queuedata, &__key); \
708 })
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 *);
716
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);
722
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);
726
727 bool blk_mq_queue_inflight(struct request_queue *q);
728
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),
736 };
737
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);
743
744 /*
745 * Tag address space map.
746 */
747 struct blk_mq_tags {
748 unsigned int nr_tags;
749 unsigned int nr_reserved_tags;
750 unsigned int active_queues;
751
752 struct sbitmap_queue bitmap_tags;
753 struct sbitmap_queue breserved_tags;
754
755 struct request **rqs;
756 struct request **static_rqs;
757 struct list_head page_list;
758
759 /*
760 * used to clear request reference in rqs[] before freeing one
761 * request pool
762 */
763 spinlock_t lock;
764 };
765
blk_mq_tag_to_rq(struct blk_mq_tags * tags,unsigned int tag)766 static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,
767 unsigned int tag)
768 {
769 if (tag < tags->nr_tags) {
770 prefetch(tags->rqs[tag]);
771 return tags->rqs[tag];
772 }
773
774 return NULL;
775 }
776
777 enum {
778 BLK_MQ_UNIQUE_TAG_BITS = 16,
779 BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
780 };
781
782 u32 blk_mq_unique_tag(struct request *rq);
783
blk_mq_unique_tag_to_hwq(u32 unique_tag)784 static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
785 {
786 return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
787 }
788
blk_mq_unique_tag_to_tag(u32 unique_tag)789 static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
790 {
791 return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
792 }
793
794 /**
795 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
796 * @rq: target request.
797 */
blk_mq_rq_state(struct request * rq)798 static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
799 {
800 return READ_ONCE(rq->state);
801 }
802
blk_mq_request_started(struct request * rq)803 static inline int blk_mq_request_started(struct request *rq)
804 {
805 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
806 }
807
blk_mq_request_completed(struct request * rq)808 static inline int blk_mq_request_completed(struct request *rq)
809 {
810 return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
811 }
812
813 /*
814 *
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.
819 */
blk_mq_set_request_complete(struct request * rq)820 static inline void blk_mq_set_request_complete(struct request *rq)
821 {
822 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
823 }
824
825 /*
826 * Complete the request directly instead of deferring it to softirq or
827 * completing it another CPU. Useful in preemptible instead of an interrupt.
828 */
blk_mq_complete_request_direct(struct request * rq,void (* complete)(struct request * rq))829 static inline void blk_mq_complete_request_direct(struct request *rq,
830 void (*complete)(struct request *rq))
831 {
832 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
833 complete(rq);
834 }
835
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);
840
841 /*
842 * Only need start/end time stamping if we have iostat or
843 * blk stats enabled, or using an IO scheduler.
844 */
blk_mq_need_time_stamp(struct request * rq)845 static inline bool blk_mq_need_time_stamp(struct request *rq)
846 {
847 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_USE_SCHED));
848 }
849
blk_mq_is_reserved_rq(struct request * rq)850 static inline bool blk_mq_is_reserved_rq(struct request *rq)
851 {
852 return rq->rq_flags & RQF_RESV;
853 }
854
855 /**
856 * blk_mq_add_to_batch() - add a request to the completion batch
857 * @req: The request to add to batch
858 * @iob: The batch to add the request
859 * @is_error: Specify true if the request failed with an error
860 * @complete: The completaion handler for the request
861 *
862 * Batched completions only work when there is no I/O error and no special
863 * ->end_io handler.
864 *
865 * Return: true when the request was added to the batch, otherwise false
866 */
blk_mq_add_to_batch(struct request * req,struct io_comp_batch * iob,bool is_error,void (* complete)(struct io_comp_batch *))867 static inline bool blk_mq_add_to_batch(struct request *req,
868 struct io_comp_batch *iob, bool is_error,
869 void (*complete)(struct io_comp_batch *))
870 {
871 /*
872 * Check various conditions that exclude batch processing:
873 * 1) No batch container
874 * 2) Has scheduler data attached
875 * 3) Not a passthrough request and end_io set
876 * 4) Not a passthrough request and failed with an error
877 */
878 if (!iob)
879 return false;
880 if (req->rq_flags & RQF_SCHED_TAGS)
881 return false;
882 if (!blk_rq_is_passthrough(req)) {
883 if (req->end_io)
884 return false;
885 if (is_error)
886 return false;
887 }
888
889 if (!iob->complete)
890 iob->complete = complete;
891 else if (iob->complete != complete)
892 return false;
893 iob->need_ts |= blk_mq_need_time_stamp(req);
894 rq_list_add_tail(&iob->req_list, req);
895 return true;
896 }
897
898 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
899 void blk_mq_kick_requeue_list(struct request_queue *q);
900 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
901 void blk_mq_complete_request(struct request *rq);
902 bool blk_mq_complete_request_remote(struct request *rq);
903 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
904 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
905 void blk_mq_stop_hw_queues(struct request_queue *q);
906 void blk_mq_start_hw_queues(struct request_queue *q);
907 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
908 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
909 void blk_mq_quiesce_queue(struct request_queue *q);
910 void blk_mq_wait_quiesce_done(struct blk_mq_tag_set *set);
911 void blk_mq_quiesce_tagset(struct blk_mq_tag_set *set);
912 void blk_mq_unquiesce_tagset(struct blk_mq_tag_set *set);
913 void blk_mq_unquiesce_queue(struct request_queue *q);
914 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
915 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
916 void blk_mq_run_hw_queues(struct request_queue *q, bool async);
917 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
918 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
919 busy_tag_iter_fn *fn, void *priv);
920 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
921 void blk_mq_freeze_queue_nomemsave(struct request_queue *q);
922 void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q);
923 static inline unsigned int __must_check
blk_mq_freeze_queue(struct request_queue * q)924 blk_mq_freeze_queue(struct request_queue *q)
925 {
926 unsigned int memflags = memalloc_noio_save();
927
928 blk_mq_freeze_queue_nomemsave(q);
929 return memflags;
930 }
931 static inline void
blk_mq_unfreeze_queue(struct request_queue * q,unsigned int memflags)932 blk_mq_unfreeze_queue(struct request_queue *q, unsigned int memflags)
933 {
934 blk_mq_unfreeze_queue_nomemrestore(q);
935 memalloc_noio_restore(memflags);
936 }
937 void blk_freeze_queue_start(struct request_queue *q);
938 void blk_mq_freeze_queue_wait(struct request_queue *q);
939 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
940 unsigned long timeout);
941 void blk_mq_unfreeze_queue_non_owner(struct request_queue *q);
942 void blk_freeze_queue_start_non_owner(struct request_queue *q);
943
944 void blk_mq_map_queues(struct blk_mq_queue_map *qmap);
945 void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap,
946 struct device *dev, unsigned int offset);
947 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);
948
949 void blk_mq_quiesce_queue_nowait(struct request_queue *q);
950
951 unsigned int blk_mq_rq_cpu(struct request *rq);
952
953 bool __blk_should_fake_timeout(struct request_queue *q);
blk_should_fake_timeout(struct request_queue * q)954 static inline bool blk_should_fake_timeout(struct request_queue *q)
955 {
956 if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) &&
957 test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
958 return __blk_should_fake_timeout(q);
959 return false;
960 }
961
962 /**
963 * blk_mq_rq_from_pdu - cast a PDU to a request
964 * @pdu: the PDU (Protocol Data Unit) to be casted
965 *
966 * Return: request
967 *
968 * Driver command data is immediately after the request. So subtract request
969 * size to get back to the original request.
970 */
blk_mq_rq_from_pdu(void * pdu)971 static inline struct request *blk_mq_rq_from_pdu(void *pdu)
972 {
973 return pdu - sizeof(struct request);
974 }
975
976 /**
977 * blk_mq_rq_to_pdu - cast a request to a PDU
978 * @rq: the request to be casted
979 *
980 * Return: pointer to the PDU
981 *
982 * Driver command data is immediately after the request. So add request to get
983 * the PDU.
984 */
blk_mq_rq_to_pdu(struct request * rq)985 static inline void *blk_mq_rq_to_pdu(struct request *rq)
986 {
987 return rq + 1;
988 }
989
990 #define queue_for_each_hw_ctx(q, hctx, i) \
991 xa_for_each(&(q)->hctx_table, (i), (hctx))
992
993 #define hctx_for_each_ctx(hctx, ctx, i) \
994 for ((i) = 0; (i) < (hctx)->nr_ctx && \
995 ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
996
blk_mq_cleanup_rq(struct request * rq)997 static inline void blk_mq_cleanup_rq(struct request *rq)
998 {
999 if (rq->q->mq_ops->cleanup_rq)
1000 rq->q->mq_ops->cleanup_rq(rq);
1001 }
1002
1003 void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
1004 struct lock_class_key *key);
1005
rq_is_sync(struct request * rq)1006 static inline bool rq_is_sync(struct request *rq)
1007 {
1008 return op_is_sync(rq->cmd_flags);
1009 }
1010
1011 void blk_rq_init(struct request_queue *q, struct request *rq);
1012 int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
1013 struct bio_set *bs, gfp_t gfp_mask,
1014 int (*bio_ctr)(struct bio *, struct bio *, void *), void *data);
1015 void blk_rq_unprep_clone(struct request *rq);
1016 blk_status_t blk_insert_cloned_request(struct request *rq);
1017
1018 struct rq_map_data {
1019 struct page **pages;
1020 unsigned long offset;
1021 unsigned short page_order;
1022 unsigned short nr_entries;
1023 bool null_mapped;
1024 bool from_user;
1025 };
1026
1027 int blk_rq_map_user(struct request_queue *, struct request *,
1028 struct rq_map_data *, void __user *, unsigned long, gfp_t);
1029 int blk_rq_map_user_io(struct request *, struct rq_map_data *,
1030 void __user *, unsigned long, gfp_t, bool, int, bool, int);
1031 int blk_rq_map_user_iov(struct request_queue *, struct request *,
1032 struct rq_map_data *, const struct iov_iter *, gfp_t);
1033 int blk_rq_unmap_user(struct bio *);
1034 int blk_rq_map_kern(struct request_queue *, struct request *, void *,
1035 unsigned int, gfp_t);
1036 int blk_rq_append_bio(struct request *rq, struct bio *bio);
1037 void blk_execute_rq_nowait(struct request *rq, bool at_head);
1038 blk_status_t blk_execute_rq(struct request *rq, bool at_head);
1039 bool blk_rq_is_poll(struct request *rq);
1040
1041 struct req_iterator {
1042 struct bvec_iter iter;
1043 struct bio *bio;
1044 };
1045
1046 #define __rq_for_each_bio(_bio, rq) \
1047 if ((rq->bio)) \
1048 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
1049
1050 #define rq_for_each_segment(bvl, _rq, _iter) \
1051 __rq_for_each_bio(_iter.bio, _rq) \
1052 bio_for_each_segment(bvl, _iter.bio, _iter.iter)
1053
1054 #define rq_for_each_bvec(bvl, _rq, _iter) \
1055 __rq_for_each_bio(_iter.bio, _rq) \
1056 bio_for_each_bvec(bvl, _iter.bio, _iter.iter)
1057
1058 #define rq_iter_last(bvec, _iter) \
1059 (_iter.bio->bi_next == NULL && \
1060 bio_iter_last(bvec, _iter.iter))
1061
1062 /*
1063 * blk_rq_pos() : the current sector
1064 * blk_rq_bytes() : bytes left in the entire request
1065 * blk_rq_cur_bytes() : bytes left in the current segment
1066 * blk_rq_sectors() : sectors left in the entire request
1067 * blk_rq_cur_sectors() : sectors left in the current segment
1068 * blk_rq_stats_sectors() : sectors of the entire request used for stats
1069 */
blk_rq_pos(const struct request * rq)1070 static inline sector_t blk_rq_pos(const struct request *rq)
1071 {
1072 return rq->__sector;
1073 }
1074
blk_rq_bytes(const struct request * rq)1075 static inline unsigned int blk_rq_bytes(const struct request *rq)
1076 {
1077 return rq->__data_len;
1078 }
1079
blk_rq_cur_bytes(const struct request * rq)1080 static inline int blk_rq_cur_bytes(const struct request *rq)
1081 {
1082 if (!rq->bio)
1083 return 0;
1084 if (!bio_has_data(rq->bio)) /* dataless requests such as discard */
1085 return rq->bio->bi_iter.bi_size;
1086 return bio_iovec(rq->bio).bv_len;
1087 }
1088
blk_rq_sectors(const struct request * rq)1089 static inline unsigned int blk_rq_sectors(const struct request *rq)
1090 {
1091 return blk_rq_bytes(rq) >> SECTOR_SHIFT;
1092 }
1093
blk_rq_cur_sectors(const struct request * rq)1094 static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
1095 {
1096 return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
1097 }
1098
blk_rq_stats_sectors(const struct request * rq)1099 static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
1100 {
1101 return rq->stats_sectors;
1102 }
1103
1104 /*
1105 * Some commands like WRITE SAME have a payload or data transfer size which
1106 * is different from the size of the request. Any driver that supports such
1107 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
1108 * calculate the data transfer size.
1109 */
blk_rq_payload_bytes(struct request * rq)1110 static inline unsigned int blk_rq_payload_bytes(struct request *rq)
1111 {
1112 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1113 return rq->special_vec.bv_len;
1114 return blk_rq_bytes(rq);
1115 }
1116
1117 /*
1118 * Return the first full biovec in the request. The caller needs to check that
1119 * there are any bvecs before calling this helper.
1120 */
req_bvec(struct request * rq)1121 static inline struct bio_vec req_bvec(struct request *rq)
1122 {
1123 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1124 return rq->special_vec;
1125 return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
1126 }
1127
blk_rq_count_bios(struct request * rq)1128 static inline unsigned int blk_rq_count_bios(struct request *rq)
1129 {
1130 unsigned int nr_bios = 0;
1131 struct bio *bio;
1132
1133 __rq_for_each_bio(bio, rq)
1134 nr_bios++;
1135
1136 return nr_bios;
1137 }
1138
1139 void blk_steal_bios(struct bio_list *list, struct request *rq);
1140
1141 /*
1142 * Request completion related functions.
1143 *
1144 * blk_update_request() completes given number of bytes and updates
1145 * the request without completing it.
1146 */
1147 bool blk_update_request(struct request *rq, blk_status_t error,
1148 unsigned int nr_bytes);
1149 void blk_abort_request(struct request *);
1150
1151 /*
1152 * Number of physical segments as sent to the device.
1153 *
1154 * Normally this is the number of discontiguous data segments sent by the
1155 * submitter. But for data-less command like discard we might have no
1156 * actual data segments submitted, but the driver might have to add it's
1157 * own special payload. In that case we still return 1 here so that this
1158 * special payload will be mapped.
1159 */
blk_rq_nr_phys_segments(struct request * rq)1160 static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
1161 {
1162 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1163 return 1;
1164 return rq->nr_phys_segments;
1165 }
1166
1167 /*
1168 * Number of discard segments (or ranges) the driver needs to fill in.
1169 * Each discard bio merged into a request is counted as one segment.
1170 */
blk_rq_nr_discard_segments(struct request * rq)1171 static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
1172 {
1173 return max_t(unsigned short, rq->nr_phys_segments, 1);
1174 }
1175
1176 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
1177 struct scatterlist *sglist, struct scatterlist **last_sg);
blk_rq_map_sg(struct request_queue * q,struct request * rq,struct scatterlist * sglist)1178 static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1179 struct scatterlist *sglist)
1180 {
1181 struct scatterlist *last_sg = NULL;
1182
1183 return __blk_rq_map_sg(q, rq, sglist, &last_sg);
1184 }
1185 void blk_dump_rq_flags(struct request *, char *);
1186
1187 #endif /* BLK_MQ_H */
1188