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