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