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