blk-mq.h 33.9 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5
#ifndef BLK_MQ_H
#define BLK_MQ_H

#include <linux/blkdev.h>
6
#include <linux/sbitmap.h>
7
#include <linux/lockdep.h>
8
#include <linux/scatterlist.h>
J
Jens Axboe 已提交
9
#include <linux/prefetch.h>
10 11

struct blk_mq_tags;
12
struct blk_flush_queue;
13

14
#define BLKDEV_MIN_RQ	4
15
#define BLKDEV_DEFAULT_RQ	128
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

typedef void (rq_end_io_fn)(struct request *, blk_status_t);

/*
 * request flags */
typedef __u32 __bitwise req_flags_t;

/* drive already may have started this one */
#define RQF_STARTED		((__force req_flags_t)(1 << 1))
/* may not be passed by ioscheduler */
#define RQF_SOFTBARRIER		((__force req_flags_t)(1 << 3))
/* request for flush sequence */
#define RQF_FLUSH_SEQ		((__force req_flags_t)(1 << 4))
/* merge of different types, fail separately */
#define RQF_MIXED_MERGE		((__force req_flags_t)(1 << 5))
/* track inflight for MQ */
#define RQF_MQ_INFLIGHT		((__force req_flags_t)(1 << 6))
/* don't call prep for this one */
#define RQF_DONTPREP		((__force req_flags_t)(1 << 7))
/* vaguely specified driver internal error.  Ignored by the block layer */
#define RQF_FAILED		((__force req_flags_t)(1 << 10))
/* don't warn about errors */
#define RQF_QUIET		((__force req_flags_t)(1 << 11))
/* elevator private data attached */
#define RQF_ELVPRIV		((__force req_flags_t)(1 << 12))
/* account into disk and partition IO statistics */
#define RQF_IO_STAT		((__force req_flags_t)(1 << 13))
/* runtime pm request */
#define RQF_PM			((__force req_flags_t)(1 << 15))
/* on IO scheduler merge hash */
#define RQF_HASHED		((__force req_flags_t)(1 << 16))
/* track IO completion time */
#define RQF_STATS		((__force req_flags_t)(1 << 17))
/* Look at ->special_vec for the actual data payload instead of the
   bio chain. */
#define RQF_SPECIAL_PAYLOAD	((__force req_flags_t)(1 << 18))
/* The per-zone write lock is held for this request */
#define RQF_ZONE_WRITE_LOCKED	((__force req_flags_t)(1 << 19))
/* already slept for hybrid poll */
#define RQF_MQ_POLL_SLEPT	((__force req_flags_t)(1 << 20))
/* ->timeout has been called, don't expire again */
#define RQF_TIMED_OUT		((__force req_flags_t)(1 << 21))
58 59
/* queue has elevator attached */
#define RQF_ELV			((__force req_flags_t)(1 << 22))
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

/* flags that prevent us from merging requests: */
#define RQF_NOMERGE_FLAGS \
	(RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)

enum mq_rq_state {
	MQ_RQ_IDLE		= 0,
	MQ_RQ_IN_FLIGHT		= 1,
	MQ_RQ_COMPLETE		= 2,
};

/*
 * Try to put the fields that are referenced together in the same cacheline.
 *
 * If you modify this structure, make sure to update blk_rq_init() and
 * especially blk_mq_rq_ctx_init() to take care of the added fields.
 */
struct request {
	struct request_queue *q;
	struct blk_mq_ctx *mq_ctx;
	struct blk_mq_hw_ctx *mq_hctx;

	unsigned int cmd_flags;		/* op and common flags */
	req_flags_t rq_flags;

	int tag;
	int internal_tag;

88 89
	unsigned int timeout;

90 91 92 93 94 95 96
	/* the following two fields are internal, NEVER access directly */
	unsigned int __data_len;	/* total data len */
	sector_t __sector;		/* sector cursor */

	struct bio *bio;
	struct bio *biotail;

97 98 99 100
	union {
		struct list_head queuelist;
		struct request *rq_next;
	};
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	struct block_device *part;
#ifdef CONFIG_BLK_RQ_ALLOC_TIME
	/* Time that the first bio started allocating this request. */
	u64 alloc_time_ns;
#endif
	/* Time that this request was allocated for this IO. */
	u64 start_time_ns;
	/* Time that I/O was submitted to the device. */
	u64 io_start_time_ns;

#ifdef CONFIG_BLK_WBT
	unsigned short wbt_flags;
#endif
	/*
	 * rq sectors used for blk stats. It has the same value
	 * with blk_rq_sectors(rq), except that it never be zeroed
	 * by completion.
	 */
	unsigned short stats_sectors;

	/*
	 * Number of scatter-gather DMA addr+len pairs after
	 * physical address coalescing is performed.
	 */
	unsigned short nr_phys_segments;

#ifdef CONFIG_BLK_DEV_INTEGRITY
	unsigned short nr_integrity_segments;
#endif

#ifdef CONFIG_BLK_INLINE_ENCRYPTION
	struct bio_crypt_ctx *crypt_ctx;
134
	struct blk_crypto_keyslot *crypt_keyslot;
135 136 137 138 139 140
#endif

	unsigned short write_hint;
	unsigned short ioprio;

	enum mq_rq_state state;
141
	atomic_t ref;
142 143 144

	unsigned long deadline;

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	/*
	 * The hash is used inside the scheduler, and killed once the
	 * request reaches the dispatch list. The ipi_list is only used
	 * to queue the request for softirq completion, which is long
	 * after the request has been unhashed (and even removed from
	 * the dispatch list).
	 */
	union {
		struct hlist_node hash;	/* merge hash */
		struct llist_node ipi_list;
	};

	/*
	 * The rb_node is only used inside the io scheduler, requests
	 * are pruned when moved to the dispatch queue. So let the
	 * completion_data share space with the rb_node.
	 */
	union {
		struct rb_node rb_node;	/* sort/lookup */
		struct bio_vec special_vec;
		void *completion_data;
	};


	/*
	 * Three pointers are available for the IO schedulers, if they need
	 * more they have to dynamically allocate it.  Flush requests are
	 * never put on the IO scheduler. So let the flush fields share
	 * space with the elevator data.
	 */
	union {
		struct {
			struct io_cq		*icq;
			void			*priv[2];
		} elv;

		struct {
			unsigned int		seq;
			struct list_head	list;
			rq_end_io_fn		*saved_end_io;
		} flush;
	};

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	union {
		struct __call_single_data csd;
		u64 fifo_time;
	};

	/*
	 * completion callback.
	 */
	rq_end_io_fn *end_io;
	void *end_io_data;
};

#define req_op(req) \
	((req)->cmd_flags & REQ_OP_MASK)

static inline bool blk_rq_is_passthrough(struct request *rq)
{
	return blk_op_is_passthrough(req_op(rq));
}

static inline unsigned short req_get_ioprio(struct request *req)
{
	return req->ioprio;
}

#define rq_data_dir(rq)		(op_is_write(req_op(rq)) ? WRITE : READ)

#define rq_dma_dir(rq) \
	(op_is_write(req_op(rq)) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#define rq_list_add(listptr, rq)	do {		\
	(rq)->rq_next = *(listptr);			\
	*(listptr) = rq;				\
} while (0)

#define rq_list_pop(listptr)				\
({							\
	struct request *__req = NULL;			\
	if ((listptr) && *(listptr))	{		\
		__req = *(listptr);			\
		*(listptr) = __req->rq_next;		\
	}						\
	__req;						\
})

#define rq_list_peek(listptr)				\
({							\
	struct request *__req = NULL;			\
	if ((listptr) && *(listptr))			\
		__req = *(listptr);			\
	__req;						\
})

#define rq_list_for_each(listptr, pos)			\
	for (pos = rq_list_peek((listptr)); pos; pos = rq_list_next(pos))

244 245 246 247
#define rq_list_for_each_safe(listptr, pos, nxt)			\
	for (pos = rq_list_peek((listptr)), nxt = rq_list_next(pos);	\
		pos; pos = nxt, nxt = pos ? rq_list_next(pos) : NULL)

248 249 250
#define rq_list_next(rq)	(rq)->rq_next
#define rq_list_empty(list)	((list) == (struct request *) NULL)

K
Keith Busch 已提交
251 252 253 254 255 256 257
/**
 * rq_list_move() - move a struct request from one list to another
 * @src: The source list @rq is currently in
 * @dst: The destination list that @rq will be appended to
 * @rq: The request to move
 * @prev: The request preceding @rq in @src (NULL if @rq is the head)
 */
Y
Yang Li 已提交
258
static inline void rq_list_move(struct request **src, struct request **dst,
K
Keith Busch 已提交
259 260 261 262 263 264 265 266 267
				struct request *rq, struct request *prev)
{
	if (prev)
		prev->rq_next = rq->rq_next;
	else
		*src = rq->rq_next;
	rq_list_add(dst, rq);
}

268 269 270 271 272 273 274 275
enum blk_eh_timer_return {
	BLK_EH_DONE,		/* drivers has completed the command */
	BLK_EH_RESET_TIMER,	/* reset timer and try again */
};

#define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
#define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */

276
/**
277 278
 * struct blk_mq_hw_ctx - State for a hardware queue facing the hardware
 * block device
279
 */
280 281
struct blk_mq_hw_ctx {
	struct {
282
		/** @lock: Protects the dispatch list. */
283
		spinlock_t		lock;
284 285 286 287 288 289 290
		/**
		 * @dispatch: Used for requests that are ready to be
		 * dispatched to the hardware but for some reason (e.g. lack of
		 * resources) could not be sent to the hardware. As soon as the
		 * driver can send new requests, requests at this list will
		 * be sent first for a fairer dispatch.
		 */
291
		struct list_head	dispatch;
292 293 294 295 296
		 /**
		  * @state: BLK_MQ_S_* flags. Defines the state of the hw
		  * queue (active, scheduled to restart, stopped).
		  */
		unsigned long		state;
297 298
	} ____cacheline_aligned_in_smp;

299 300 301
	/**
	 * @run_work: Used for scheduling a hardware queue run at a later time.
	 */
302
	struct delayed_work	run_work;
303
	/** @cpumask: Map of available CPUs where this hctx can run. */
304
	cpumask_var_t		cpumask;
305 306 307 308
	/**
	 * @next_cpu: Used by blk_mq_hctx_next_cpu() for round-robin CPU
	 * selection from @cpumask.
	 */
309
	int			next_cpu;
310 311 312 313
	/**
	 * @next_cpu_batch: Counter of how many works left in the batch before
	 * changing to the next CPU.
	 */
314
	int			next_cpu_batch;
315

316 317
	/** @flags: BLK_MQ_F_* flags. Defines the behaviour of the queue. */
	unsigned long		flags;
318

319 320 321 322
	/**
	 * @sched_data: Pointer owned by the IO scheduler attached to a request
	 * queue. It's up to the IO scheduler how to use this pointer.
	 */
323
	void			*sched_data;
324 325 326
	/**
	 * @queue: Pointer to the request queue that owns this hardware context.
	 */
327
	struct request_queue	*queue;
328
	/** @fq: Queue of requests that need to perform a flush operation. */
329
	struct blk_flush_queue	*fq;
330

331 332 333 334
	/**
	 * @driver_data: Pointer to data owned by the block driver that created
	 * this hctx
	 */
335 336
	void			*driver_data;

337 338 339 340
	/**
	 * @ctx_map: Bitmap for each software queue. If bit is on, there is a
	 * pending request in that software queue.
	 */
341
	struct sbitmap		ctx_map;
342

343 344 345 346
	/**
	 * @dispatch_from: Software queue to be used when no scheduler was
	 * selected.
	 */
347
	struct blk_mq_ctx	*dispatch_from;
348 349 350 351 352
	/**
	 * @dispatch_busy: Number used by blk_mq_update_dispatch_busy() to
	 * decide if the hw_queue is busy using Exponential Weighted Moving
	 * Average algorithm.
	 */
353
	unsigned int		dispatch_busy;
354

355
	/** @type: HCTX_TYPE_* flags. Type of hardware queue. */
356
	unsigned short		type;
357
	/** @nr_ctx: Number of software queues. */
358
	unsigned short		nr_ctx;
359
	/** @ctxs: Array of software queues. */
360
	struct blk_mq_ctx	**ctxs;
361

362
	/** @dispatch_wait_lock: Lock for dispatch_wait queue. */
363
	spinlock_t		dispatch_wait_lock;
364 365 366 367
	/**
	 * @dispatch_wait: Waitqueue to put requests when there is no tag
	 * available at the moment, to wait for another try in the future.
	 */
368
	wait_queue_entry_t	dispatch_wait;
369 370 371 372 373

	/**
	 * @wait_index: Index of next available dispatch_wait queue to insert
	 * requests.
	 */
374
	atomic_t		wait_index;
375

376 377 378 379
	/**
	 * @tags: Tags owned by the block driver. A tag at this set is only
	 * assigned when a request is dispatched from a hardware queue.
	 */
380
	struct blk_mq_tags	*tags;
381 382 383 384 385
	/**
	 * @sched_tags: Tags owned by I/O scheduler. If there is an I/O
	 * scheduler associated with a request queue, a tag is assigned when
	 * that request is allocated. Else, this member is not used.
	 */
386
	struct blk_mq_tags	*sched_tags;
387

388
	/** @queued: Number of queued requests. */
389
	unsigned long		queued;
390
	/** @run: Number of dispatched requests. */
391 392
	unsigned long		run;

393
	/** @numa_node: NUMA node the storage adapter has been connected to. */
394
	unsigned int		numa_node;
395
	/** @queue_num: Index of this hardware queue. */
396
	unsigned int		queue_num;
397

398 399 400 401
	/**
	 * @nr_active: Number of active requests. Only used when a tag set is
	 * shared across request queues.
	 */
402 403
	atomic_t		nr_active;

404 405
	/** @cpuhp_online: List to store request if CPU is going to die */
	struct hlist_node	cpuhp_online;
406
	/** @cpuhp_dead: List to store request if some CPU die. */
407
	struct hlist_node	cpuhp_dead;
408
	/** @kobj: Kernel object for sysfs. */
409
	struct kobject		kobj;
J
Jens Axboe 已提交
410

411
#ifdef CONFIG_BLK_DEBUG_FS
412 413 414 415
	/**
	 * @debugfs_dir: debugfs directory for this hardware queue. Named
	 * as cpu<cpu_number>.
	 */
416
	struct dentry		*debugfs_dir;
417
	/** @sched_debugfs_dir:	debugfs directory for the scheduler. */
418
	struct dentry		*sched_debugfs_dir;
419
#endif
420

421 422 423 424
	/**
	 * @hctx_list: if this hctx is not in use, this is an entry in
	 * q->unused_hctx_list.
	 */
425
	struct list_head	hctx_list;
426 427
};

428
/**
429
 * struct blk_mq_queue_map - Map software queues to hardware queues
430 431 432 433 434 435 436 437
 * @mq_map:       CPU ID to hardware queue index map. This is an array
 *	with nr_cpu_ids elements. Each element has a value in the range
 *	[@queue_offset, @queue_offset + @nr_queues).
 * @nr_queues:    Number of hardware queues to map CPU IDs onto.
 * @queue_offset: First hardware queue to map onto. Used by the PCIe NVMe
 *	driver to map each hardware queue type (enum hctx_type) onto a distinct
 *	set of hardware queues.
 */
J
Jens Axboe 已提交
438 439 440
struct blk_mq_queue_map {
	unsigned int *mq_map;
	unsigned int nr_queues;
441
	unsigned int queue_offset;
J
Jens Axboe 已提交
442 443
};

444 445 446 447 448 449 450
/**
 * enum hctx_type - Type of hardware queue
 * @HCTX_TYPE_DEFAULT:	All I/O not otherwise accounted for.
 * @HCTX_TYPE_READ:	Just for READ I/O.
 * @HCTX_TYPE_POLL:	Polled I/O of any kind.
 * @HCTX_MAX_TYPES:	Number of types of hctx.
 */
451
enum hctx_type {
452 453 454
	HCTX_TYPE_DEFAULT,
	HCTX_TYPE_READ,
	HCTX_TYPE_POLL,
455 456

	HCTX_MAX_TYPES,
J
Jens Axboe 已提交
457 458
};

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/**
 * struct blk_mq_tag_set - tag set that can be shared between request queues
 * @map:	   One or more ctx -> hctx mappings. One map exists for each
 *		   hardware queue type (enum hctx_type) that the driver wishes
 *		   to support. There are no restrictions on maps being of the
 *		   same size, and it's perfectly legal to share maps between
 *		   types.
 * @nr_maps:	   Number of elements in the @map array. A number in the range
 *		   [1, HCTX_MAX_TYPES].
 * @ops:	   Pointers to functions that implement block driver behavior.
 * @nr_hw_queues:  Number of hardware queues supported by the block driver that
 *		   owns this data structure.
 * @queue_depth:   Number of tags per hardware queue, reserved tags included.
 * @reserved_tags: Number of tags to set aside for BLK_MQ_REQ_RESERVED tag
 *		   allocations.
 * @cmd_size:	   Number of additional bytes to allocate per request. The block
 *		   driver owns these additional bytes.
 * @numa_node:	   NUMA node the storage adapter has been connected to.
 * @timeout:	   Request processing timeout in jiffies.
 * @flags:	   Zero or more BLK_MQ_F_* flags.
 * @driver_data:   Pointer to data owned by the block driver that created this
 *		   tag set.
 * @tags:	   Tag sets. One tag set per hardware queue. Has @nr_hw_queues
 *		   elements.
483 484 485
 * @shared_tags:
 *		   Shared set of tags. Has @nr_hw_queues elements. If set,
 *		   shared by all @tags.
486 487 488 489
 * @tag_list_lock: Serializes tag_list accesses.
 * @tag_list:	   List of the request queues that use this tag set. See also
 *		   request_queue.tag_set_list.
 */
490
struct blk_mq_tag_set {
J
Jens Axboe 已提交
491
	struct blk_mq_queue_map	map[HCTX_MAX_TYPES];
492
	unsigned int		nr_maps;
J
Jens Axboe 已提交
493
	const struct blk_mq_ops	*ops;
494 495
	unsigned int		nr_hw_queues;
	unsigned int		queue_depth;
496
	unsigned int		reserved_tags;
497
	unsigned int		cmd_size;
498 499
	int			numa_node;
	unsigned int		timeout;
500
	unsigned int		flags;
501 502 503
	void			*driver_data;

	struct blk_mq_tags	**tags;
504

505
	struct blk_mq_tags	*shared_tags;
506

507 508
	struct mutex		tag_list_lock;
	struct list_head	tag_list;
509 510
};

511 512 513 514 515 516
/**
 * struct blk_mq_queue_data - Data about a request inserted in a queue
 *
 * @rq:   Request pointer.
 * @last: If it is the last request in the queue.
 */
517 518 519 520 521
struct blk_mq_queue_data {
	struct request *rq;
	bool last;
};

522
typedef bool (busy_tag_iter_fn)(struct request *, void *, bool);
J
Jens Axboe 已提交
523

524 525 526 527
/**
 * struct blk_mq_ops - Callback functions that implements block driver
 * behaviour.
 */
528
struct blk_mq_ops {
529 530
	/**
	 * @queue_rq: Queue a new request from block IO.
531
	 */
532 533
	blk_status_t (*queue_rq)(struct blk_mq_hw_ctx *,
				 const struct blk_mq_queue_data *);
534

535 536 537 538
	/**
	 * @commit_rqs: If a driver uses bd->last to judge when to submit
	 * requests to hardware, it must define this function. In case of errors
	 * that make us stop issuing further requests, this hook serves the
J
Jens Axboe 已提交
539 540 541
	 * purpose of kicking the hardware (which the last request otherwise
	 * would have done).
	 */
542
	void (*commit_rqs)(struct blk_mq_hw_ctx *);
J
Jens Axboe 已提交
543

J
Jens Axboe 已提交
544 545 546 547 548 549 550 551
	/**
	 * @queue_rqs: Queue a list of new requests. Driver is guaranteed
	 * that each request belongs to the same queue. If the driver doesn't
	 * empty the @rqlist completely, then the rest will be queued
	 * individually by the block layer upon return.
	 */
	void (*queue_rqs)(struct request **rqlist);

552 553
	/**
	 * @get_budget: Reserve budget before queue request, once .queue_rq is
554 555 556 557
	 * run, it is driver's responsibility to release the
	 * reserved budget. Also we have to handle failure case
	 * of .get_budget for avoiding I/O deadlock.
	 */
558
	int (*get_budget)(struct request_queue *);
559

560 561 562
	/**
	 * @put_budget: Release the reserved budget.
	 */
563
	void (*put_budget)(struct request_queue *, int);
564

565 566
	/**
	 * @set_rq_budget_token: store rq's budget token
567 568
	 */
	void (*set_rq_budget_token)(struct request *, int);
569 570
	/**
	 * @get_rq_budget_token: retrieve rq's budget token
571 572 573
	 */
	int (*get_rq_budget_token)(struct request *);

574 575
	/**
	 * @timeout: Called on request timeout.
576
	 */
577
	enum blk_eh_timer_return (*timeout)(struct request *, bool);
578

579 580
	/**
	 * @poll: Called to poll for completion of a specific tag.
J
Jens Axboe 已提交
581
	 */
582
	int (*poll)(struct blk_mq_hw_ctx *, struct io_comp_batch *);
J
Jens Axboe 已提交
583

584 585 586
	/**
	 * @complete: Mark the request as complete.
	 */
587
	void (*complete)(struct request *);
588

589 590 591 592
	/**
	 * @init_hctx: Called when the block layer side of a hardware queue has
	 * been set up, allowing the driver to allocate/init matching
	 * structures.
593
	 */
594
	int (*init_hctx)(struct blk_mq_hw_ctx *, void *, unsigned int);
595 596 597
	/**
	 * @exit_hctx: Ditto for exit/teardown.
	 */
598
	void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
599

600 601 602
	/**
	 * @init_request: Called for every command allocated by the block layer
	 * to allow the driver to set up driver specific data.
603 604 605
	 *
	 * Tag greater than or equal to queue_depth is for setting up
	 * flush request.
606
	 */
607 608
	int (*init_request)(struct blk_mq_tag_set *set, struct request *,
			    unsigned int, unsigned int);
609 610 611
	/**
	 * @exit_request: Ditto for exit/teardown.
	 */
612 613
	void (*exit_request)(struct blk_mq_tag_set *set, struct request *,
			     unsigned int);
614 615 616 617

	/**
	 * @cleanup_rq: Called before freeing one request which isn't completed
	 * yet, and usually for freeing the driver private data.
M
Ming Lei 已提交
618
	 */
619
	void (*cleanup_rq)(struct request *);
M
Ming Lei 已提交
620

621 622
	/**
	 * @busy: If set, returns whether or not this queue currently is busy.
J
Jens Axboe 已提交
623
	 */
624
	bool (*busy)(struct request_queue *);
J
Jens Axboe 已提交
625

626 627 628 629
	/**
	 * @map_queues: This allows drivers specify their own queue mapping by
	 * overriding the setup-time function that builds the mq_map.
	 */
630
	int (*map_queues)(struct blk_mq_tag_set *set);
631 632

#ifdef CONFIG_BLK_DEBUG_FS
633 634
	/**
	 * @show_rq: Used by the debugfs implementation to show driver-specific
635 636 637 638
	 * information about a request.
	 */
	void (*show_rq)(struct seq_file *m, struct request *rq);
#endif
639 640 641 642
};

enum {
	BLK_MQ_F_SHOULD_MERGE	= 1 << 0,
643
	BLK_MQ_F_TAG_QUEUE_SHARED = 1 << 1,
644 645 646 647 648
	/*
	 * Set when this device requires underlying blk-mq device for
	 * completing IO:
	 */
	BLK_MQ_F_STACKING	= 1 << 2,
649
	BLK_MQ_F_TAG_HCTX_SHARED = 1 << 3,
650
	BLK_MQ_F_BLOCKING	= 1 << 5,
651
	/* Do not allow an I/O scheduler to be configured. */
652
	BLK_MQ_F_NO_SCHED	= 1 << 6,
653 654 655 656 657
	/*
	 * Select 'none' during queue registration in case of a single hwq
	 * or shared hwqs instead of 'mq-deadline'.
	 */
	BLK_MQ_F_NO_SCHED_BY_DEFAULT	= 1 << 7,
S
Shaohua Li 已提交
658 659
	BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,
	BLK_MQ_F_ALLOC_POLICY_BITS = 1,
660

661
	BLK_MQ_S_STOPPED	= 0,
662
	BLK_MQ_S_TAG_ACTIVE	= 1,
663
	BLK_MQ_S_SCHED_RESTART	= 2,
664

665 666 667
	/* hw queue is inactive after all its CPUs become offline */
	BLK_MQ_S_INACTIVE	= 3,

668
	BLK_MQ_MAX_DEPTH	= 10240,
669 670

	BLK_MQ_CPU_WORK_BATCH	= 8,
671
};
S
Shaohua Li 已提交
672 673 674 675 676 677
#define BLK_MQ_FLAG_TO_ALLOC_POLICY(flags) \
	((flags >> BLK_MQ_F_ALLOC_POLICY_START_BIT) & \
		((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1))
#define BLK_ALLOC_POLICY_TO_MQ_FLAG(policy) \
	((policy & ((1 << BLK_MQ_F_ALLOC_POLICY_BITS) - 1)) \
		<< BLK_MQ_F_ALLOC_POLICY_START_BIT)
678

679 680
#define BLK_MQ_NO_HCTX_IDX	(-1U)

681 682
struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
		struct lock_class_key *lkclass);
683 684 685 686
#define blk_mq_alloc_disk(set, queuedata)				\
({									\
	static struct lock_class_key __key;				\
									\
687
	__blk_mq_alloc_disk(set, queuedata, &__key);			\
688
})
689 690
struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
		struct lock_class_key *lkclass);
691
struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *);
692 693
int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
		struct request_queue *q);
694
void blk_mq_destroy_queue(struct request_queue *);
695

696
int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set);
697 698 699
int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
		const struct blk_mq_ops *ops, unsigned int queue_depth,
		unsigned int set_flags);
700 701
void blk_mq_free_tag_set(struct blk_mq_tag_set *set);

702
void blk_mq_free_request(struct request *rq);
703

704
bool blk_mq_queue_inflight(struct request_queue *q);
705

706
enum {
707 708 709 710
	/* return when out of requests */
	BLK_MQ_REQ_NOWAIT	= (__force blk_mq_req_flags_t)(1 << 0),
	/* allocate from reserved pool */
	BLK_MQ_REQ_RESERVED	= (__force blk_mq_req_flags_t)(1 << 1),
711 712
	/* set RQF_PM */
	BLK_MQ_REQ_PM		= (__force blk_mq_req_flags_t)(1 << 2),
713 714
};

715
struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
716
		blk_mq_req_flags_t flags);
717
struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
718 719
		unsigned int op, blk_mq_req_flags_t flags,
		unsigned int hctx_idx);
J
Jens Axboe 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

/*
 * Tag address space map.
 */
struct blk_mq_tags {
	unsigned int nr_tags;
	unsigned int nr_reserved_tags;

	atomic_t active_queues;

	struct sbitmap_queue bitmap_tags;
	struct sbitmap_queue breserved_tags;

	struct request **rqs;
	struct request **static_rqs;
	struct list_head page_list;

	/*
	 * used to clear request reference in rqs[] before freeing one
	 * request pool
	 */
	spinlock_t lock;
};

static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,
					       unsigned int tag)
{
	if (tag < tags->nr_tags) {
		prefetch(tags->rqs[tag]);
		return tags->rqs[tag];
	}

	return NULL;
}
754

B
Bart Van Assche 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
enum {
	BLK_MQ_UNIQUE_TAG_BITS = 16,
	BLK_MQ_UNIQUE_TAG_MASK = (1 << BLK_MQ_UNIQUE_TAG_BITS) - 1,
};

u32 blk_mq_unique_tag(struct request *rq);

static inline u16 blk_mq_unique_tag_to_hwq(u32 unique_tag)
{
	return unique_tag >> BLK_MQ_UNIQUE_TAG_BITS;
}

static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
{
	return unique_tag & BLK_MQ_UNIQUE_TAG_MASK;
}

P
Pavel Begunkov 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
/**
 * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
 * @rq: target request.
 */
static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
{
	return READ_ONCE(rq->state);
}

static inline int blk_mq_request_started(struct request *rq)
{
	return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
}

static inline int blk_mq_request_completed(struct request *rq)
{
	return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
}
790

791 792 793 794 795 796 797 798 799 800 801 802
/*
 * 
 * Set the state to complete when completing a request from inside ->queue_rq.
 * This is used by drivers that want to ensure special complete actions that
 * need access to the request are called on failure, e.g. by nvme for
 * multipathing.
 */
static inline void blk_mq_set_request_complete(struct request *rq)
{
	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
}

803 804 805 806 807 808 809 810 811 812 813
/*
 * Complete the request directly instead of deferring it to softirq or
 * completing it another CPU. Useful in preemptible instead of an interrupt.
 */
static inline void blk_mq_complete_request_direct(struct request *rq,
		   void (*complete)(struct request *rq))
{
	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
	complete(rq);
}

814
void blk_mq_start_request(struct request *rq);
815 816
void blk_mq_end_request(struct request *rq, blk_status_t error);
void __blk_mq_end_request(struct request *rq, blk_status_t error);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
void blk_mq_end_request_batch(struct io_comp_batch *ib);

/*
 * Only need start/end time stamping if we have iostat or
 * blk stats enabled, or using an IO scheduler.
 */
static inline bool blk_mq_need_time_stamp(struct request *rq)
{
	return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS | RQF_ELV));
}

/*
 * Batched completions only work when there is no I/O error and no special
 * ->end_io handler.
 */
static inline bool blk_mq_add_to_batch(struct request *req,
				       struct io_comp_batch *iob, int ioerror,
				       void (*complete)(struct io_comp_batch *))
{
	if (!iob || (req->rq_flags & RQF_ELV) || req->end_io || ioerror)
		return false;
	if (!iob->complete)
		iob->complete = complete;
	else if (iob->complete != complete)
		return false;
	iob->need_ts |= blk_mq_need_time_stamp(req);
	rq_list_add(&iob->req_list, req);
	return true;
}
846

847
void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
848
void blk_mq_kick_requeue_list(struct request_queue *q);
849
void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
850
void blk_mq_complete_request(struct request *rq);
851
bool blk_mq_complete_request_remote(struct request *rq);
852
bool blk_mq_queue_stopped(struct request_queue *q);
853 854
void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx);
void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx);
855
void blk_mq_stop_hw_queues(struct request_queue *q);
856
void blk_mq_start_hw_queues(struct request_queue *q);
857
void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
858
void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async);
859
void blk_mq_quiesce_queue(struct request_queue *q);
860
void blk_mq_wait_quiesce_done(struct request_queue *q);
861
void blk_mq_unquiesce_queue(struct request_queue *q);
862
void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
863
void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
864
void blk_mq_run_hw_queues(struct request_queue *q, bool async);
865
void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
866 867
void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
		busy_tag_iter_fn *fn, void *priv);
868
void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
869
void blk_mq_freeze_queue(struct request_queue *q);
870
void blk_mq_unfreeze_queue(struct request_queue *q);
871
void blk_freeze_queue_start(struct request_queue *q);
872
void blk_mq_freeze_queue_wait(struct request_queue *q);
873 874
int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
				     unsigned long timeout);
875

J
Jens Axboe 已提交
876
int blk_mq_map_queues(struct blk_mq_queue_map *qmap);
K
Keith Busch 已提交
877 878
void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues);

879
void blk_mq_quiesce_queue_nowait(struct request_queue *q);
880

J
Jens Axboe 已提交
881 882
unsigned int blk_mq_rq_cpu(struct request *rq);

883 884 885 886 887 888 889 890 891
bool __blk_should_fake_timeout(struct request_queue *q);
static inline bool blk_should_fake_timeout(struct request_queue *q)
{
	if (IS_ENABLED(CONFIG_FAIL_IO_TIMEOUT) &&
	    test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
		return __blk_should_fake_timeout(q);
	return false;
}

892 893 894 895 896 897
/**
 * blk_mq_rq_from_pdu - cast a PDU to a request
 * @pdu: the PDU (Protocol Data Unit) to be casted
 *
 * Return: request
 *
898
 * Driver command data is immediately after the request. So subtract request
899
 * size to get back to the original request.
900 901 902 903 904
 */
static inline struct request *blk_mq_rq_from_pdu(void *pdu)
{
	return pdu - sizeof(struct request);
}
905 906 907 908 909 910 911 912 913 914

/**
 * blk_mq_rq_to_pdu - cast a request to a PDU
 * @rq: the request to be casted
 *
 * Return: pointer to the PDU
 *
 * Driver command data is immediately after the request. So add request to get
 * the PDU.
 */
915 916
static inline void *blk_mq_rq_to_pdu(struct request *rq)
{
J
Jens Axboe 已提交
917
	return rq + 1;
918 919 920
}

#define queue_for_each_hw_ctx(q, hctx, i)				\
M
Ming Lei 已提交
921
	xa_for_each(&(q)->hctx_table, (i), (hctx))
922 923

#define hctx_for_each_ctx(hctx, ctx, i)					\
924 925
	for ((i) = 0; (i) < (hctx)->nr_ctx &&				\
	     ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
926

M
Ming Lei 已提交
927 928 929 930 931 932
static inline void blk_mq_cleanup_rq(struct request *rq)
{
	if (rq->q->mq_ops->cleanup_rq)
		rq->q->mq_ops->cleanup_rq(rq);
}

933 934 935 936 937 938 939 940 941
static inline void blk_rq_bio_prep(struct request *rq, struct bio *bio,
		unsigned int nr_segs)
{
	rq->nr_phys_segments = nr_segs;
	rq->__data_len = bio->bi_iter.bi_size;
	rq->bio = rq->biotail = bio;
	rq->ioprio = bio_prio(bio);
}

942 943
void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
		struct lock_class_key *key);
944

945 946 947 948 949 950 951 952 953 954
static inline bool rq_is_sync(struct request *rq)
{
	return op_is_sync(rq->cmd_flags);
}

void blk_rq_init(struct request_queue *q, struct request *rq);
int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
		struct bio_set *bs, gfp_t gfp_mask,
		int (*bio_ctr)(struct bio *, struct bio *, void *), void *data);
void blk_rq_unprep_clone(struct request *rq);
955
blk_status_t blk_insert_cloned_request(struct request *rq);
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

struct rq_map_data {
	struct page **pages;
	int page_order;
	int nr_entries;
	unsigned long offset;
	int null_mapped;
	int from_user;
};

int blk_rq_map_user(struct request_queue *, struct request *,
		struct rq_map_data *, void __user *, unsigned long, gfp_t);
int blk_rq_map_user_iov(struct request_queue *, struct request *,
		struct rq_map_data *, const struct iov_iter *, gfp_t);
int blk_rq_unmap_user(struct bio *);
int blk_rq_map_kern(struct request_queue *, struct request *, void *,
		unsigned int, gfp_t);
int blk_rq_append_bio(struct request *rq, struct bio *bio);
974
void blk_execute_rq_nowait(struct request *rq, bool at_head);
975
blk_status_t blk_execute_rq(struct request *rq, bool at_head);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

struct req_iterator {
	struct bvec_iter iter;
	struct bio *bio;
};

#define __rq_for_each_bio(_bio, rq)	\
	if ((rq->bio))			\
		for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)

#define rq_for_each_segment(bvl, _rq, _iter)			\
	__rq_for_each_bio(_iter.bio, _rq)			\
		bio_for_each_segment(bvl, _iter.bio, _iter.iter)

#define rq_for_each_bvec(bvl, _rq, _iter)			\
	__rq_for_each_bio(_iter.bio, _rq)			\
		bio_for_each_bvec(bvl, _iter.bio, _iter.iter)

#define rq_iter_last(bvec, _iter)				\
		(_iter.bio->bi_next == NULL &&			\
		 bio_iter_last(bvec, _iter.iter))

/*
 * blk_rq_pos()			: the current sector
 * blk_rq_bytes()		: bytes left in the entire request
 * blk_rq_cur_bytes()		: bytes left in the current segment
 * blk_rq_sectors()		: sectors left in the entire request
 * blk_rq_cur_sectors()		: sectors left in the current segment
 * blk_rq_stats_sectors()	: sectors of the entire request used for stats
 */
static inline sector_t blk_rq_pos(const struct request *rq)
{
	return rq->__sector;
}

static inline unsigned int blk_rq_bytes(const struct request *rq)
{
	return rq->__data_len;
}

static inline int blk_rq_cur_bytes(const struct request *rq)
{
1018 1019 1020 1021 1022
	if (!rq->bio)
		return 0;
	if (!bio_has_data(rq->bio))	/* dataless requests such as discard */
		return rq->bio->bi_iter.bi_size;
	return bio_iovec(rq->bio).bv_len;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
}

static inline unsigned int blk_rq_sectors(const struct request *rq)
{
	return blk_rq_bytes(rq) >> SECTOR_SHIFT;
}

static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
{
	return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT;
}

static inline unsigned int blk_rq_stats_sectors(const struct request *rq)
{
	return rq->stats_sectors;
}

/*
 * Some commands like WRITE SAME have a payload or data transfer size which
 * is different from the size of the request.  Any driver that supports such
 * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
 * calculate the data transfer size.
 */
static inline unsigned int blk_rq_payload_bytes(struct request *rq)
{
	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
		return rq->special_vec.bv_len;
	return blk_rq_bytes(rq);
}

/*
 * Return the first full biovec in the request.  The caller needs to check that
 * there are any bvecs before calling this helper.
 */
static inline struct bio_vec req_bvec(struct request *rq)
{
	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
		return rq->special_vec;
	return mp_bvec_iter_bvec(rq->bio->bi_io_vec, rq->bio->bi_iter);
}

static inline unsigned int blk_rq_count_bios(struct request *rq)
{
	unsigned int nr_bios = 0;
	struct bio *bio;

	__rq_for_each_bio(bio, rq)
		nr_bios++;

	return nr_bios;
}

void blk_steal_bios(struct bio_list *list, struct request *rq);

/*
 * Request completion related functions.
 *
 * blk_update_request() completes given number of bytes and updates
 * the request without completing it.
 */
bool blk_update_request(struct request *rq, blk_status_t error,
			       unsigned int nr_bytes);
void blk_abort_request(struct request *);

/*
 * Number of physical segments as sent to the device.
 *
 * Normally this is the number of discontiguous data segments sent by the
 * submitter.  But for data-less command like discard we might have no
 * actual data segments submitted, but the driver might have to add it's
 * own special payload.  In that case we still return 1 here so that this
 * special payload will be mapped.
 */
static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
{
	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
		return 1;
	return rq->nr_phys_segments;
}

/*
 * Number of discard segments (or ranges) the driver needs to fill in.
 * Each discard bio merged into a request is counted as one segment.
 */
static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
{
	return max_t(unsigned short, rq->nr_phys_segments, 1);
}

int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
		struct scatterlist *sglist, struct scatterlist **last_sg);
static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
		struct scatterlist *sglist)
{
	struct scatterlist *last_sg = NULL;

	return __blk_rq_map_sg(q, rq, sglist, &last_sg);
}
void blk_dump_rq_flags(struct request *, char *);

#ifdef CONFIG_BLK_DEV_ZONED
static inline unsigned int blk_rq_zone_no(struct request *rq)
{
	return blk_queue_zone_no(rq->q, blk_rq_pos(rq));
}

static inline unsigned int blk_rq_zone_is_seq(struct request *rq)
{
	return blk_queue_zone_is_seq(rq->q, blk_rq_pos(rq));
}

bool blk_req_needs_zone_write_lock(struct request *rq);
bool blk_req_zone_write_trylock(struct request *rq);
void __blk_req_zone_write_lock(struct request *rq);
void __blk_req_zone_write_unlock(struct request *rq);

static inline void blk_req_zone_write_lock(struct request *rq)
{
	if (blk_req_needs_zone_write_lock(rq))
		__blk_req_zone_write_lock(rq);
}

static inline void blk_req_zone_write_unlock(struct request *rq)
{
	if (rq->rq_flags & RQF_ZONE_WRITE_LOCKED)
		__blk_req_zone_write_unlock(rq);
}

static inline bool blk_req_zone_is_write_locked(struct request *rq)
{
	return rq->q->seq_zones_wlock &&
		test_bit(blk_rq_zone_no(rq), rq->q->seq_zones_wlock);
}

static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
{
	if (!blk_req_needs_zone_write_lock(rq))
		return true;
	return !blk_req_zone_is_write_locked(rq);
}
#else /* CONFIG_BLK_DEV_ZONED */
static inline bool blk_req_needs_zone_write_lock(struct request *rq)
{
	return false;
}

static inline void blk_req_zone_write_lock(struct request *rq)
{
}

static inline void blk_req_zone_write_unlock(struct request *rq)
{
}
static inline bool blk_req_zone_is_write_locked(struct request *rq)
{
	return false;
}

static inline bool blk_req_can_dispatch_to_zone(struct request *rq)
{
	return true;
}
#endif /* CONFIG_BLK_DEV_ZONED */

#endif /* BLK_MQ_H */