xref: /linux/block/blk-mq-debugfs.c (revision 8658dca8bd5666f50e02400f844079e520641d8c)
107e4feadSOmar Sandoval /*
207e4feadSOmar Sandoval  * Copyright (C) 2017 Facebook
307e4feadSOmar Sandoval  *
407e4feadSOmar Sandoval  * This program is free software; you can redistribute it and/or
507e4feadSOmar Sandoval  * modify it under the terms of the GNU General Public
607e4feadSOmar Sandoval  * License v2 as published by the Free Software Foundation.
707e4feadSOmar Sandoval  *
807e4feadSOmar Sandoval  * This program is distributed in the hope that it will be useful,
907e4feadSOmar Sandoval  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1007e4feadSOmar Sandoval  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1107e4feadSOmar Sandoval  * General Public License for more details.
1207e4feadSOmar Sandoval  *
1307e4feadSOmar Sandoval  * You should have received a copy of the GNU General Public License
1407e4feadSOmar Sandoval  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
1507e4feadSOmar Sandoval  */
1607e4feadSOmar Sandoval 
1707e4feadSOmar Sandoval #include <linux/kernel.h>
1807e4feadSOmar Sandoval #include <linux/blkdev.h>
1907e4feadSOmar Sandoval #include <linux/debugfs.h>
2007e4feadSOmar Sandoval 
2107e4feadSOmar Sandoval #include <linux/blk-mq.h>
2218fbda91SOmar Sandoval #include "blk.h"
2307e4feadSOmar Sandoval #include "blk-mq.h"
24d96b37c0SOmar Sandoval #include "blk-mq-tag.h"
2507e4feadSOmar Sandoval 
2607e4feadSOmar Sandoval struct blk_mq_debugfs_attr {
2707e4feadSOmar Sandoval 	const char *name;
2807e4feadSOmar Sandoval 	umode_t mode;
2907e4feadSOmar Sandoval 	const struct file_operations *fops;
3007e4feadSOmar Sandoval };
3107e4feadSOmar Sandoval 
32950cd7e9SOmar Sandoval static int blk_mq_debugfs_seq_open(struct inode *inode, struct file *file,
33950cd7e9SOmar Sandoval 				   const struct seq_operations *ops)
34950cd7e9SOmar Sandoval {
35950cd7e9SOmar Sandoval 	struct seq_file *m;
36950cd7e9SOmar Sandoval 	int ret;
37950cd7e9SOmar Sandoval 
38950cd7e9SOmar Sandoval 	ret = seq_open(file, ops);
39950cd7e9SOmar Sandoval 	if (!ret) {
40950cd7e9SOmar Sandoval 		m = file->private_data;
41950cd7e9SOmar Sandoval 		m->private = inode->i_private;
42950cd7e9SOmar Sandoval 	}
43950cd7e9SOmar Sandoval 	return ret;
44950cd7e9SOmar Sandoval }
45950cd7e9SOmar Sandoval 
4691d68905SBart Van Assche static int blk_flags_show(struct seq_file *m, const unsigned long flags,
4791d68905SBart Van Assche 			  const char *const *flag_name, int flag_name_count)
4891d68905SBart Van Assche {
4991d68905SBart Van Assche 	bool sep = false;
5091d68905SBart Van Assche 	int i;
5191d68905SBart Van Assche 
5291d68905SBart Van Assche 	for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
5391d68905SBart Van Assche 		if (!(flags & BIT(i)))
5491d68905SBart Van Assche 			continue;
5591d68905SBart Van Assche 		if (sep)
5691d68905SBart Van Assche 			seq_puts(m, " ");
5791d68905SBart Van Assche 		sep = true;
5891d68905SBart Van Assche 		if (i < flag_name_count && flag_name[i])
5991d68905SBart Van Assche 			seq_puts(m, flag_name[i]);
6091d68905SBart Van Assche 		else
6191d68905SBart Van Assche 			seq_printf(m, "%d", i);
6291d68905SBart Van Assche 	}
6391d68905SBart Van Assche 	return 0;
6491d68905SBart Van Assche }
6591d68905SBart Van Assche 
6691d68905SBart Van Assche static const char *const blk_queue_flag_name[] = {
6791d68905SBart Van Assche 	[QUEUE_FLAG_QUEUED]	 = "QUEUED",
6891d68905SBart Van Assche 	[QUEUE_FLAG_STOPPED]	 = "STOPPED",
6991d68905SBart Van Assche 	[QUEUE_FLAG_SYNCFULL]	 = "SYNCFULL",
7091d68905SBart Van Assche 	[QUEUE_FLAG_ASYNCFULL]	 = "ASYNCFULL",
7191d68905SBart Van Assche 	[QUEUE_FLAG_DYING]	 = "DYING",
7291d68905SBart Van Assche 	[QUEUE_FLAG_BYPASS]	 = "BYPASS",
7391d68905SBart Van Assche 	[QUEUE_FLAG_BIDI]	 = "BIDI",
7491d68905SBart Van Assche 	[QUEUE_FLAG_NOMERGES]	 = "NOMERGES",
7591d68905SBart Van Assche 	[QUEUE_FLAG_SAME_COMP]	 = "SAME_COMP",
7691d68905SBart Van Assche 	[QUEUE_FLAG_FAIL_IO]	 = "FAIL_IO",
7791d68905SBart Van Assche 	[QUEUE_FLAG_STACKABLE]	 = "STACKABLE",
7891d68905SBart Van Assche 	[QUEUE_FLAG_NONROT]	 = "NONROT",
7991d68905SBart Van Assche 	[QUEUE_FLAG_IO_STAT]	 = "IO_STAT",
8091d68905SBart Van Assche 	[QUEUE_FLAG_DISCARD]	 = "DISCARD",
8191d68905SBart Van Assche 	[QUEUE_FLAG_NOXMERGES]	 = "NOXMERGES",
8291d68905SBart Van Assche 	[QUEUE_FLAG_ADD_RANDOM]	 = "ADD_RANDOM",
8391d68905SBart Van Assche 	[QUEUE_FLAG_SECERASE]	 = "SECERASE",
8491d68905SBart Van Assche 	[QUEUE_FLAG_SAME_FORCE]	 = "SAME_FORCE",
8591d68905SBart Van Assche 	[QUEUE_FLAG_DEAD]	 = "DEAD",
8691d68905SBart Van Assche 	[QUEUE_FLAG_INIT_DONE]	 = "INIT_DONE",
8791d68905SBart Van Assche 	[QUEUE_FLAG_NO_SG_MERGE] = "NO_SG_MERGE",
8891d68905SBart Van Assche 	[QUEUE_FLAG_POLL]	 = "POLL",
8991d68905SBart Van Assche 	[QUEUE_FLAG_WC]		 = "WC",
9091d68905SBart Van Assche 	[QUEUE_FLAG_FUA]	 = "FUA",
9191d68905SBart Van Assche 	[QUEUE_FLAG_FLUSH_NQ]	 = "FLUSH_NQ",
9291d68905SBart Van Assche 	[QUEUE_FLAG_DAX]	 = "DAX",
9391d68905SBart Van Assche 	[QUEUE_FLAG_STATS]	 = "STATS",
9491d68905SBart Van Assche 	[QUEUE_FLAG_POLL_STATS]	 = "POLL_STATS",
9591d68905SBart Van Assche 	[QUEUE_FLAG_REGISTERED]	 = "REGISTERED",
9691d68905SBart Van Assche };
9791d68905SBart Van Assche 
9891d68905SBart Van Assche static int blk_queue_flags_show(struct seq_file *m, void *v)
9991d68905SBart Van Assche {
10091d68905SBart Van Assche 	struct request_queue *q = m->private;
10191d68905SBart Van Assche 
10291d68905SBart Van Assche 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
10391d68905SBart Van Assche 		       ARRAY_SIZE(blk_queue_flag_name));
104fd07dc81SBart Van Assche 	seq_puts(m, "\n");
10591d68905SBart Van Assche 	return 0;
10691d68905SBart Van Assche }
10791d68905SBart Van Assche 
10891d68905SBart Van Assche static ssize_t blk_queue_flags_store(struct file *file, const char __user *ubuf,
10991d68905SBart Van Assche 				     size_t len, loff_t *offp)
11091d68905SBart Van Assche {
11191d68905SBart Van Assche 	struct request_queue *q = file_inode(file)->i_private;
11291d68905SBart Van Assche 	char op[16] = { }, *s;
11391d68905SBart Van Assche 
11491d68905SBart Van Assche 	len = min(len, sizeof(op) - 1);
11591d68905SBart Van Assche 	if (copy_from_user(op, ubuf, len))
11691d68905SBart Van Assche 		return -EFAULT;
11791d68905SBart Van Assche 	s = op;
11891d68905SBart Van Assche 	strsep(&s, " \t\n"); /* strip trailing whitespace */
11991d68905SBart Van Assche 	if (strcmp(op, "run") == 0) {
12091d68905SBart Van Assche 		blk_mq_run_hw_queues(q, true);
12191d68905SBart Van Assche 	} else if (strcmp(op, "start") == 0) {
12291d68905SBart Van Assche 		blk_mq_start_stopped_hw_queues(q, true);
12391d68905SBart Van Assche 	} else {
12491d68905SBart Van Assche 		pr_err("%s: unsupported operation %s. Use either 'run' or 'start'\n",
12591d68905SBart Van Assche 		       __func__, op);
12691d68905SBart Van Assche 		return -EINVAL;
12791d68905SBart Van Assche 	}
12891d68905SBart Van Assche 	return len;
12991d68905SBart Van Assche }
13091d68905SBart Van Assche 
13191d68905SBart Van Assche static int blk_queue_flags_open(struct inode *inode, struct file *file)
13291d68905SBart Van Assche {
13391d68905SBart Van Assche 	return single_open(file, blk_queue_flags_show, inode->i_private);
13491d68905SBart Van Assche }
13591d68905SBart Van Assche 
13691d68905SBart Van Assche static const struct file_operations blk_queue_flags_fops = {
13791d68905SBart Van Assche 	.open		= blk_queue_flags_open,
13891d68905SBart Van Assche 	.read		= seq_read,
13991d68905SBart Van Assche 	.llseek		= seq_lseek,
14091d68905SBart Van Assche 	.release	= single_release,
14191d68905SBart Van Assche 	.write		= blk_queue_flags_store,
14291d68905SBart Van Assche };
14391d68905SBart Van Assche 
14434dbad5dSOmar Sandoval static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
14534dbad5dSOmar Sandoval {
14634dbad5dSOmar Sandoval 	if (stat->nr_samples) {
14734dbad5dSOmar Sandoval 		seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
14834dbad5dSOmar Sandoval 			   stat->nr_samples, stat->mean, stat->min, stat->max);
14934dbad5dSOmar Sandoval 	} else {
15034dbad5dSOmar Sandoval 		seq_puts(m, "samples=0");
15134dbad5dSOmar Sandoval 	}
15234dbad5dSOmar Sandoval }
15334dbad5dSOmar Sandoval 
15434dbad5dSOmar Sandoval static int queue_poll_stat_show(struct seq_file *m, void *v)
15534dbad5dSOmar Sandoval {
15634dbad5dSOmar Sandoval 	struct request_queue *q = m->private;
1570206319fSStephen Bates 	int bucket;
15834dbad5dSOmar Sandoval 
1590206319fSStephen Bates 	for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
1600206319fSStephen Bates 		seq_printf(m, "read  (%d Bytes): ", 1 << (9+bucket));
1610206319fSStephen Bates 		print_stat(m, &q->poll_stat[2*bucket]);
16234dbad5dSOmar Sandoval 		seq_puts(m, "\n");
16334dbad5dSOmar Sandoval 
1640206319fSStephen Bates 		seq_printf(m, "write (%d Bytes): ",  1 << (9+bucket));
1650206319fSStephen Bates 		print_stat(m, &q->poll_stat[2*bucket+1]);
16634dbad5dSOmar Sandoval 		seq_puts(m, "\n");
1670206319fSStephen Bates 	}
16834dbad5dSOmar Sandoval 	return 0;
16934dbad5dSOmar Sandoval }
17034dbad5dSOmar Sandoval 
17134dbad5dSOmar Sandoval static int queue_poll_stat_open(struct inode *inode, struct file *file)
17234dbad5dSOmar Sandoval {
17334dbad5dSOmar Sandoval 	return single_open(file, queue_poll_stat_show, inode->i_private);
17434dbad5dSOmar Sandoval }
17534dbad5dSOmar Sandoval 
17634dbad5dSOmar Sandoval static const struct file_operations queue_poll_stat_fops = {
17734dbad5dSOmar Sandoval 	.open		= queue_poll_stat_open,
17834dbad5dSOmar Sandoval 	.read		= seq_read,
17934dbad5dSOmar Sandoval 	.llseek		= seq_lseek,
18034dbad5dSOmar Sandoval 	.release	= single_release,
18134dbad5dSOmar Sandoval };
18234dbad5dSOmar Sandoval 
183f5c0b091SBart Van Assche static const char *const hctx_state_name[] = {
184f5c0b091SBart Van Assche 	[BLK_MQ_S_STOPPED]	 = "STOPPED",
185f5c0b091SBart Van Assche 	[BLK_MQ_S_TAG_ACTIVE]	 = "TAG_ACTIVE",
186f5c0b091SBart Van Assche 	[BLK_MQ_S_SCHED_RESTART] = "SCHED_RESTART",
187f5c0b091SBart Van Assche 	[BLK_MQ_S_TAG_WAITING]	 = "TAG_WAITING",
188f5c0b091SBart Van Assche 
189f5c0b091SBart Van Assche };
1909abb2ad2SOmar Sandoval static int hctx_state_show(struct seq_file *m, void *v)
1919abb2ad2SOmar Sandoval {
1929abb2ad2SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
1939abb2ad2SOmar Sandoval 
194f5c0b091SBart Van Assche 	blk_flags_show(m, hctx->state, hctx_state_name,
195f5c0b091SBart Van Assche 		       ARRAY_SIZE(hctx_state_name));
196fd07dc81SBart Van Assche 	seq_puts(m, "\n");
1979abb2ad2SOmar Sandoval 	return 0;
1989abb2ad2SOmar Sandoval }
1999abb2ad2SOmar Sandoval 
2009abb2ad2SOmar Sandoval static int hctx_state_open(struct inode *inode, struct file *file)
2019abb2ad2SOmar Sandoval {
2029abb2ad2SOmar Sandoval 	return single_open(file, hctx_state_show, inode->i_private);
2039abb2ad2SOmar Sandoval }
2049abb2ad2SOmar Sandoval 
2059abb2ad2SOmar Sandoval static const struct file_operations hctx_state_fops = {
2069abb2ad2SOmar Sandoval 	.open		= hctx_state_open,
2079abb2ad2SOmar Sandoval 	.read		= seq_read,
2089abb2ad2SOmar Sandoval 	.llseek		= seq_lseek,
2099abb2ad2SOmar Sandoval 	.release	= single_release,
2109abb2ad2SOmar Sandoval };
2119abb2ad2SOmar Sandoval 
212f5c0b091SBart Van Assche static const char *const alloc_policy_name[] = {
213f5c0b091SBart Van Assche 	[BLK_TAG_ALLOC_FIFO]	= "fifo",
214f5c0b091SBart Van Assche 	[BLK_TAG_ALLOC_RR]	= "rr",
215f5c0b091SBart Van Assche };
216f5c0b091SBart Van Assche 
217f5c0b091SBart Van Assche static const char *const hctx_flag_name[] = {
218f5c0b091SBart Van Assche 	[ilog2(BLK_MQ_F_SHOULD_MERGE)]	= "SHOULD_MERGE",
219f5c0b091SBart Van Assche 	[ilog2(BLK_MQ_F_TAG_SHARED)]	= "TAG_SHARED",
220f5c0b091SBart Van Assche 	[ilog2(BLK_MQ_F_SG_MERGE)]	= "SG_MERGE",
221f5c0b091SBart Van Assche 	[ilog2(BLK_MQ_F_BLOCKING)]	= "BLOCKING",
222f5c0b091SBart Van Assche 	[ilog2(BLK_MQ_F_NO_SCHED)]	= "NO_SCHED",
223f5c0b091SBart Van Assche };
224f5c0b091SBart Van Assche 
2259abb2ad2SOmar Sandoval static int hctx_flags_show(struct seq_file *m, void *v)
2269abb2ad2SOmar Sandoval {
2279abb2ad2SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
228f5c0b091SBart Van Assche 	const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
2299abb2ad2SOmar Sandoval 
230f5c0b091SBart Van Assche 	seq_puts(m, "alloc_policy=");
231f5c0b091SBart Van Assche 	if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
232f5c0b091SBart Van Assche 	    alloc_policy_name[alloc_policy])
233f5c0b091SBart Van Assche 		seq_puts(m, alloc_policy_name[alloc_policy]);
234f5c0b091SBart Van Assche 	else
235f5c0b091SBart Van Assche 		seq_printf(m, "%d", alloc_policy);
236f5c0b091SBart Van Assche 	seq_puts(m, " ");
237f5c0b091SBart Van Assche 	blk_flags_show(m,
238f5c0b091SBart Van Assche 		       hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
239f5c0b091SBart Van Assche 		       hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
240fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2419abb2ad2SOmar Sandoval 	return 0;
2429abb2ad2SOmar Sandoval }
2439abb2ad2SOmar Sandoval 
2449abb2ad2SOmar Sandoval static int hctx_flags_open(struct inode *inode, struct file *file)
2459abb2ad2SOmar Sandoval {
2469abb2ad2SOmar Sandoval 	return single_open(file, hctx_flags_show, inode->i_private);
2479abb2ad2SOmar Sandoval }
2489abb2ad2SOmar Sandoval 
2499abb2ad2SOmar Sandoval static const struct file_operations hctx_flags_fops = {
2509abb2ad2SOmar Sandoval 	.open		= hctx_flags_open,
2519abb2ad2SOmar Sandoval 	.read		= seq_read,
2529abb2ad2SOmar Sandoval 	.llseek		= seq_lseek,
2539abb2ad2SOmar Sandoval 	.release	= single_release,
2549abb2ad2SOmar Sandoval };
2559abb2ad2SOmar Sandoval 
256*8658dca8SBart Van Assche static const char *const op_name[] = {
257*8658dca8SBart Van Assche 	[REQ_OP_READ]		= "READ",
258*8658dca8SBart Van Assche 	[REQ_OP_WRITE]		= "WRITE",
259*8658dca8SBart Van Assche 	[REQ_OP_FLUSH]		= "FLUSH",
260*8658dca8SBart Van Assche 	[REQ_OP_DISCARD]	= "DISCARD",
261*8658dca8SBart Van Assche 	[REQ_OP_ZONE_REPORT]	= "ZONE_REPORT",
262*8658dca8SBart Van Assche 	[REQ_OP_SECURE_ERASE]	= "SECURE_ERASE",
263*8658dca8SBart Van Assche 	[REQ_OP_ZONE_RESET]	= "ZONE_RESET",
264*8658dca8SBart Van Assche 	[REQ_OP_WRITE_SAME]	= "WRITE_SAME",
265*8658dca8SBart Van Assche 	[REQ_OP_WRITE_ZEROES]	= "WRITE_ZEROES",
266*8658dca8SBart Van Assche 	[REQ_OP_SCSI_IN]	= "SCSI_IN",
267*8658dca8SBart Van Assche 	[REQ_OP_SCSI_OUT]	= "SCSI_OUT",
268*8658dca8SBart Van Assche 	[REQ_OP_DRV_IN]		= "DRV_IN",
269*8658dca8SBart Van Assche 	[REQ_OP_DRV_OUT]	= "DRV_OUT",
270*8658dca8SBart Van Assche };
271*8658dca8SBart Van Assche 
272*8658dca8SBart Van Assche static const char *const cmd_flag_name[] = {
273*8658dca8SBart Van Assche 	[__REQ_FAILFAST_DEV]		= "FAILFAST_DEV",
274*8658dca8SBart Van Assche 	[__REQ_FAILFAST_TRANSPORT]	= "FAILFAST_TRANSPORT",
275*8658dca8SBart Van Assche 	[__REQ_FAILFAST_DRIVER]		= "FAILFAST_DRIVER",
276*8658dca8SBart Van Assche 	[__REQ_SYNC]			= "SYNC",
277*8658dca8SBart Van Assche 	[__REQ_META]			= "META",
278*8658dca8SBart Van Assche 	[__REQ_PRIO]			= "PRIO",
279*8658dca8SBart Van Assche 	[__REQ_NOMERGE]			= "NOMERGE",
280*8658dca8SBart Van Assche 	[__REQ_IDLE]			= "IDLE",
281*8658dca8SBart Van Assche 	[__REQ_INTEGRITY]		= "INTEGRITY",
282*8658dca8SBart Van Assche 	[__REQ_FUA]			= "FUA",
283*8658dca8SBart Van Assche 	[__REQ_PREFLUSH]		= "PREFLUSH",
284*8658dca8SBart Van Assche 	[__REQ_RAHEAD]			= "RAHEAD",
285*8658dca8SBart Van Assche 	[__REQ_BACKGROUND]		= "BACKGROUND",
286*8658dca8SBart Van Assche 	[__REQ_NR_BITS]			= "NR_BITS",
287*8658dca8SBart Van Assche };
288*8658dca8SBart Van Assche 
289*8658dca8SBart Van Assche static const char *const rqf_name[] = {
290*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_SORTED)]		= "SORTED",
291*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_STARTED)]		= "STARTED",
292*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_QUEUED)]		= "QUEUED",
293*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_SOFTBARRIER)]		= "SOFTBARRIER",
294*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_FLUSH_SEQ)]		= "FLUSH_SEQ",
295*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_MIXED_MERGE)]		= "MIXED_MERGE",
296*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_MQ_INFLIGHT)]		= "MQ_INFLIGHT",
297*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_DONTPREP)]		= "DONTPREP",
298*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_PREEMPT)]		= "PREEMPT",
299*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_COPY_USER)]		= "COPY_USER",
300*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_FAILED)]		= "FAILED",
301*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_QUIET)]			= "QUIET",
302*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_ELVPRIV)]		= "ELVPRIV",
303*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_IO_STAT)]		= "IO_STAT",
304*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_ALLOCED)]		= "ALLOCED",
305*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_PM)]			= "PM",
306*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_HASHED)]		= "HASHED",
307*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_STATS)]			= "STATS",
308*8658dca8SBart Van Assche 	[ilog2((__force u32)RQF_SPECIAL_PAYLOAD)]	= "SPECIAL_PAYLOAD",
309*8658dca8SBart Van Assche };
310*8658dca8SBart Van Assche 
311950cd7e9SOmar Sandoval static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
312950cd7e9SOmar Sandoval {
313950cd7e9SOmar Sandoval 	struct request *rq = list_entry_rq(v);
314*8658dca8SBart Van Assche 	const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
315950cd7e9SOmar Sandoval 
316*8658dca8SBart Van Assche 	seq_printf(m, "%p {.op=", rq);
317*8658dca8SBart Van Assche 	if (op < ARRAY_SIZE(op_name) && op_name[op])
318*8658dca8SBart Van Assche 		seq_printf(m, "%s", op_name[op]);
319*8658dca8SBart Van Assche 	else
320*8658dca8SBart Van Assche 		seq_printf(m, "%d", op);
321*8658dca8SBart Van Assche 	seq_puts(m, ", .cmd_flags=");
322*8658dca8SBart Van Assche 	blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
323*8658dca8SBart Van Assche 		       ARRAY_SIZE(cmd_flag_name));
324*8658dca8SBart Van Assche 	seq_puts(m, ", .rq_flags=");
325*8658dca8SBart Van Assche 	blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
326*8658dca8SBart Van Assche 		       ARRAY_SIZE(rqf_name));
327*8658dca8SBart Van Assche 	seq_printf(m, ", .tag=%d, .internal_tag=%d}\n", rq->tag,
328*8658dca8SBart Van Assche 		   rq->internal_tag);
329950cd7e9SOmar Sandoval 	return 0;
330950cd7e9SOmar Sandoval }
331950cd7e9SOmar Sandoval 
332950cd7e9SOmar Sandoval static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
333f3bcb0e6SBart Van Assche 	__acquires(&hctx->lock)
334950cd7e9SOmar Sandoval {
335950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
336950cd7e9SOmar Sandoval 
337950cd7e9SOmar Sandoval 	spin_lock(&hctx->lock);
338950cd7e9SOmar Sandoval 	return seq_list_start(&hctx->dispatch, *pos);
339950cd7e9SOmar Sandoval }
340950cd7e9SOmar Sandoval 
341950cd7e9SOmar Sandoval static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
342950cd7e9SOmar Sandoval {
343950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
344950cd7e9SOmar Sandoval 
345950cd7e9SOmar Sandoval 	return seq_list_next(v, &hctx->dispatch, pos);
346950cd7e9SOmar Sandoval }
347950cd7e9SOmar Sandoval 
348950cd7e9SOmar Sandoval static void hctx_dispatch_stop(struct seq_file *m, void *v)
349f3bcb0e6SBart Van Assche 	__releases(&hctx->lock)
350950cd7e9SOmar Sandoval {
351950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
352950cd7e9SOmar Sandoval 
353950cd7e9SOmar Sandoval 	spin_unlock(&hctx->lock);
354950cd7e9SOmar Sandoval }
355950cd7e9SOmar Sandoval 
356950cd7e9SOmar Sandoval static const struct seq_operations hctx_dispatch_seq_ops = {
357950cd7e9SOmar Sandoval 	.start	= hctx_dispatch_start,
358950cd7e9SOmar Sandoval 	.next	= hctx_dispatch_next,
359950cd7e9SOmar Sandoval 	.stop	= hctx_dispatch_stop,
360950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
361950cd7e9SOmar Sandoval };
362950cd7e9SOmar Sandoval 
363950cd7e9SOmar Sandoval static int hctx_dispatch_open(struct inode *inode, struct file *file)
364950cd7e9SOmar Sandoval {
365950cd7e9SOmar Sandoval 	return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops);
366950cd7e9SOmar Sandoval }
367950cd7e9SOmar Sandoval 
368950cd7e9SOmar Sandoval static const struct file_operations hctx_dispatch_fops = {
369950cd7e9SOmar Sandoval 	.open		= hctx_dispatch_open,
370950cd7e9SOmar Sandoval 	.read		= seq_read,
371950cd7e9SOmar Sandoval 	.llseek		= seq_lseek,
372950cd7e9SOmar Sandoval 	.release	= seq_release,
373950cd7e9SOmar Sandoval };
374950cd7e9SOmar Sandoval 
3750bfa5288SOmar Sandoval static int hctx_ctx_map_show(struct seq_file *m, void *v)
3760bfa5288SOmar Sandoval {
3770bfa5288SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
3780bfa5288SOmar Sandoval 
3790bfa5288SOmar Sandoval 	sbitmap_bitmap_show(&hctx->ctx_map, m);
3800bfa5288SOmar Sandoval 	return 0;
3810bfa5288SOmar Sandoval }
3820bfa5288SOmar Sandoval 
3830bfa5288SOmar Sandoval static int hctx_ctx_map_open(struct inode *inode, struct file *file)
3840bfa5288SOmar Sandoval {
3850bfa5288SOmar Sandoval 	return single_open(file, hctx_ctx_map_show, inode->i_private);
3860bfa5288SOmar Sandoval }
3870bfa5288SOmar Sandoval 
3880bfa5288SOmar Sandoval static const struct file_operations hctx_ctx_map_fops = {
3890bfa5288SOmar Sandoval 	.open		= hctx_ctx_map_open,
3900bfa5288SOmar Sandoval 	.read		= seq_read,
3910bfa5288SOmar Sandoval 	.llseek		= seq_lseek,
3920bfa5288SOmar Sandoval 	.release	= single_release,
3930bfa5288SOmar Sandoval };
3940bfa5288SOmar Sandoval 
395d96b37c0SOmar Sandoval static void blk_mq_debugfs_tags_show(struct seq_file *m,
396d96b37c0SOmar Sandoval 				     struct blk_mq_tags *tags)
397d96b37c0SOmar Sandoval {
398d96b37c0SOmar Sandoval 	seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
399d96b37c0SOmar Sandoval 	seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
400d96b37c0SOmar Sandoval 	seq_printf(m, "active_queues=%d\n",
401d96b37c0SOmar Sandoval 		   atomic_read(&tags->active_queues));
402d96b37c0SOmar Sandoval 
403d96b37c0SOmar Sandoval 	seq_puts(m, "\nbitmap_tags:\n");
404d96b37c0SOmar Sandoval 	sbitmap_queue_show(&tags->bitmap_tags, m);
405d96b37c0SOmar Sandoval 
406d96b37c0SOmar Sandoval 	if (tags->nr_reserved_tags) {
407d96b37c0SOmar Sandoval 		seq_puts(m, "\nbreserved_tags:\n");
408d96b37c0SOmar Sandoval 		sbitmap_queue_show(&tags->breserved_tags, m);
409d96b37c0SOmar Sandoval 	}
410d96b37c0SOmar Sandoval }
411d96b37c0SOmar Sandoval 
412d96b37c0SOmar Sandoval static int hctx_tags_show(struct seq_file *m, void *v)
413d96b37c0SOmar Sandoval {
414d96b37c0SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
415d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
4168c0f14eaSBart Van Assche 	int res;
417d96b37c0SOmar Sandoval 
4188c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4198c0f14eaSBart Van Assche 	if (res)
4208c0f14eaSBart Van Assche 		goto out;
421d96b37c0SOmar Sandoval 	if (hctx->tags)
422d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->tags);
423d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
424d96b37c0SOmar Sandoval 
4258c0f14eaSBart Van Assche out:
4268c0f14eaSBart Van Assche 	return res;
427d96b37c0SOmar Sandoval }
428d96b37c0SOmar Sandoval 
429d96b37c0SOmar Sandoval static int hctx_tags_open(struct inode *inode, struct file *file)
430d96b37c0SOmar Sandoval {
431d96b37c0SOmar Sandoval 	return single_open(file, hctx_tags_show, inode->i_private);
432d96b37c0SOmar Sandoval }
433d96b37c0SOmar Sandoval 
434d96b37c0SOmar Sandoval static const struct file_operations hctx_tags_fops = {
435d96b37c0SOmar Sandoval 	.open		= hctx_tags_open,
436d96b37c0SOmar Sandoval 	.read		= seq_read,
437d96b37c0SOmar Sandoval 	.llseek		= seq_lseek,
438d96b37c0SOmar Sandoval 	.release	= single_release,
439d96b37c0SOmar Sandoval };
440d96b37c0SOmar Sandoval 
441d7e3621aSOmar Sandoval static int hctx_tags_bitmap_show(struct seq_file *m, void *v)
442d7e3621aSOmar Sandoval {
443d7e3621aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
444d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
4458c0f14eaSBart Van Assche 	int res;
446d7e3621aSOmar Sandoval 
4478c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4488c0f14eaSBart Van Assche 	if (res)
4498c0f14eaSBart Van Assche 		goto out;
450d7e3621aSOmar Sandoval 	if (hctx->tags)
451d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
452d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
4538c0f14eaSBart Van Assche 
4548c0f14eaSBart Van Assche out:
4558c0f14eaSBart Van Assche 	return res;
456d7e3621aSOmar Sandoval }
457d7e3621aSOmar Sandoval 
458d7e3621aSOmar Sandoval static int hctx_tags_bitmap_open(struct inode *inode, struct file *file)
459d7e3621aSOmar Sandoval {
460d7e3621aSOmar Sandoval 	return single_open(file, hctx_tags_bitmap_show, inode->i_private);
461d7e3621aSOmar Sandoval }
462d7e3621aSOmar Sandoval 
463d7e3621aSOmar Sandoval static const struct file_operations hctx_tags_bitmap_fops = {
464d7e3621aSOmar Sandoval 	.open		= hctx_tags_bitmap_open,
465d7e3621aSOmar Sandoval 	.read		= seq_read,
466d7e3621aSOmar Sandoval 	.llseek		= seq_lseek,
467d7e3621aSOmar Sandoval 	.release	= single_release,
468d7e3621aSOmar Sandoval };
469d7e3621aSOmar Sandoval 
470d96b37c0SOmar Sandoval static int hctx_sched_tags_show(struct seq_file *m, void *v)
471d96b37c0SOmar Sandoval {
472d96b37c0SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
473d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
4748c0f14eaSBart Van Assche 	int res;
475d96b37c0SOmar Sandoval 
4768c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4778c0f14eaSBart Van Assche 	if (res)
4788c0f14eaSBart Van Assche 		goto out;
479d96b37c0SOmar Sandoval 	if (hctx->sched_tags)
480d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
481d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
482d96b37c0SOmar Sandoval 
4838c0f14eaSBart Van Assche out:
4848c0f14eaSBart Van Assche 	return res;
485d96b37c0SOmar Sandoval }
486d96b37c0SOmar Sandoval 
487d96b37c0SOmar Sandoval static int hctx_sched_tags_open(struct inode *inode, struct file *file)
488d96b37c0SOmar Sandoval {
489d96b37c0SOmar Sandoval 	return single_open(file, hctx_sched_tags_show, inode->i_private);
490d96b37c0SOmar Sandoval }
491d96b37c0SOmar Sandoval 
492d96b37c0SOmar Sandoval static const struct file_operations hctx_sched_tags_fops = {
493d96b37c0SOmar Sandoval 	.open		= hctx_sched_tags_open,
494d96b37c0SOmar Sandoval 	.read		= seq_read,
495d96b37c0SOmar Sandoval 	.llseek		= seq_lseek,
496d96b37c0SOmar Sandoval 	.release	= single_release,
497d96b37c0SOmar Sandoval };
498d96b37c0SOmar Sandoval 
499d7e3621aSOmar Sandoval static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v)
500d7e3621aSOmar Sandoval {
501d7e3621aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
502d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
5038c0f14eaSBart Van Assche 	int res;
504d7e3621aSOmar Sandoval 
5058c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5068c0f14eaSBart Van Assche 	if (res)
5078c0f14eaSBart Van Assche 		goto out;
508d7e3621aSOmar Sandoval 	if (hctx->sched_tags)
509d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
510d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5118c0f14eaSBart Van Assche 
5128c0f14eaSBart Van Assche out:
5138c0f14eaSBart Van Assche 	return res;
514d7e3621aSOmar Sandoval }
515d7e3621aSOmar Sandoval 
516d7e3621aSOmar Sandoval static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file)
517d7e3621aSOmar Sandoval {
518d7e3621aSOmar Sandoval 	return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private);
519d7e3621aSOmar Sandoval }
520d7e3621aSOmar Sandoval 
521d7e3621aSOmar Sandoval static const struct file_operations hctx_sched_tags_bitmap_fops = {
522d7e3621aSOmar Sandoval 	.open		= hctx_sched_tags_bitmap_open,
523d7e3621aSOmar Sandoval 	.read		= seq_read,
524d7e3621aSOmar Sandoval 	.llseek		= seq_lseek,
525d7e3621aSOmar Sandoval 	.release	= single_release,
526d7e3621aSOmar Sandoval };
527d7e3621aSOmar Sandoval 
528be215473SOmar Sandoval static int hctx_io_poll_show(struct seq_file *m, void *v)
529be215473SOmar Sandoval {
530be215473SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
531be215473SOmar Sandoval 
532be215473SOmar Sandoval 	seq_printf(m, "considered=%lu\n", hctx->poll_considered);
533be215473SOmar Sandoval 	seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
534be215473SOmar Sandoval 	seq_printf(m, "success=%lu\n", hctx->poll_success);
535be215473SOmar Sandoval 	return 0;
536be215473SOmar Sandoval }
537be215473SOmar Sandoval 
538be215473SOmar Sandoval static int hctx_io_poll_open(struct inode *inode, struct file *file)
539be215473SOmar Sandoval {
540be215473SOmar Sandoval 	return single_open(file, hctx_io_poll_show, inode->i_private);
541be215473SOmar Sandoval }
542be215473SOmar Sandoval 
543be215473SOmar Sandoval static ssize_t hctx_io_poll_write(struct file *file, const char __user *buf,
544be215473SOmar Sandoval 				  size_t count, loff_t *ppos)
545be215473SOmar Sandoval {
546be215473SOmar Sandoval 	struct seq_file *m = file->private_data;
547be215473SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
548be215473SOmar Sandoval 
549be215473SOmar Sandoval 	hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
550be215473SOmar Sandoval 	return count;
551be215473SOmar Sandoval }
552be215473SOmar Sandoval 
553be215473SOmar Sandoval static const struct file_operations hctx_io_poll_fops = {
554be215473SOmar Sandoval 	.open		= hctx_io_poll_open,
555be215473SOmar Sandoval 	.read		= seq_read,
556be215473SOmar Sandoval 	.write		= hctx_io_poll_write,
557be215473SOmar Sandoval 	.llseek		= seq_lseek,
558be215473SOmar Sandoval 	.release	= single_release,
559be215473SOmar Sandoval };
560be215473SOmar Sandoval 
561be215473SOmar Sandoval static int hctx_dispatched_show(struct seq_file *m, void *v)
562be215473SOmar Sandoval {
563be215473SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
564be215473SOmar Sandoval 	int i;
565be215473SOmar Sandoval 
566be215473SOmar Sandoval 	seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
567be215473SOmar Sandoval 
568be215473SOmar Sandoval 	for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
569be215473SOmar Sandoval 		unsigned int d = 1U << (i - 1);
570be215473SOmar Sandoval 
571be215473SOmar Sandoval 		seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
572be215473SOmar Sandoval 	}
573be215473SOmar Sandoval 
574be215473SOmar Sandoval 	seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
575be215473SOmar Sandoval 	return 0;
576be215473SOmar Sandoval }
577be215473SOmar Sandoval 
578be215473SOmar Sandoval static int hctx_dispatched_open(struct inode *inode, struct file *file)
579be215473SOmar Sandoval {
580be215473SOmar Sandoval 	return single_open(file, hctx_dispatched_show, inode->i_private);
581be215473SOmar Sandoval }
582be215473SOmar Sandoval 
583be215473SOmar Sandoval static ssize_t hctx_dispatched_write(struct file *file, const char __user *buf,
584be215473SOmar Sandoval 				     size_t count, loff_t *ppos)
585be215473SOmar Sandoval {
586be215473SOmar Sandoval 	struct seq_file *m = file->private_data;
587be215473SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
588be215473SOmar Sandoval 	int i;
589be215473SOmar Sandoval 
590be215473SOmar Sandoval 	for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
591be215473SOmar Sandoval 		hctx->dispatched[i] = 0;
592be215473SOmar Sandoval 	return count;
593be215473SOmar Sandoval }
594be215473SOmar Sandoval 
595be215473SOmar Sandoval static const struct file_operations hctx_dispatched_fops = {
596be215473SOmar Sandoval 	.open		= hctx_dispatched_open,
597be215473SOmar Sandoval 	.read		= seq_read,
598be215473SOmar Sandoval 	.write		= hctx_dispatched_write,
599be215473SOmar Sandoval 	.llseek		= seq_lseek,
600be215473SOmar Sandoval 	.release	= single_release,
601be215473SOmar Sandoval };
602be215473SOmar Sandoval 
6034a46f05eSOmar Sandoval static int hctx_queued_show(struct seq_file *m, void *v)
6044a46f05eSOmar Sandoval {
6054a46f05eSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
6064a46f05eSOmar Sandoval 
6074a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->queued);
6084a46f05eSOmar Sandoval 	return 0;
6094a46f05eSOmar Sandoval }
6104a46f05eSOmar Sandoval 
6114a46f05eSOmar Sandoval static int hctx_queued_open(struct inode *inode, struct file *file)
6124a46f05eSOmar Sandoval {
6134a46f05eSOmar Sandoval 	return single_open(file, hctx_queued_show, inode->i_private);
6144a46f05eSOmar Sandoval }
6154a46f05eSOmar Sandoval 
6164a46f05eSOmar Sandoval static ssize_t hctx_queued_write(struct file *file, const char __user *buf,
6174a46f05eSOmar Sandoval 				 size_t count, loff_t *ppos)
6184a46f05eSOmar Sandoval {
6194a46f05eSOmar Sandoval 	struct seq_file *m = file->private_data;
6204a46f05eSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
6214a46f05eSOmar Sandoval 
6224a46f05eSOmar Sandoval 	hctx->queued = 0;
6234a46f05eSOmar Sandoval 	return count;
6244a46f05eSOmar Sandoval }
6254a46f05eSOmar Sandoval 
6264a46f05eSOmar Sandoval static const struct file_operations hctx_queued_fops = {
6274a46f05eSOmar Sandoval 	.open		= hctx_queued_open,
6284a46f05eSOmar Sandoval 	.read		= seq_read,
6294a46f05eSOmar Sandoval 	.write		= hctx_queued_write,
6304a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
6314a46f05eSOmar Sandoval 	.release	= single_release,
6324a46f05eSOmar Sandoval };
6334a46f05eSOmar Sandoval 
6344a46f05eSOmar Sandoval static int hctx_run_show(struct seq_file *m, void *v)
6354a46f05eSOmar Sandoval {
6364a46f05eSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
6374a46f05eSOmar Sandoval 
6384a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->run);
6394a46f05eSOmar Sandoval 	return 0;
6404a46f05eSOmar Sandoval }
6414a46f05eSOmar Sandoval 
6424a46f05eSOmar Sandoval static int hctx_run_open(struct inode *inode, struct file *file)
6434a46f05eSOmar Sandoval {
6444a46f05eSOmar Sandoval 	return single_open(file, hctx_run_show, inode->i_private);
6454a46f05eSOmar Sandoval }
6464a46f05eSOmar Sandoval 
6474a46f05eSOmar Sandoval static ssize_t hctx_run_write(struct file *file, const char __user *buf,
6484a46f05eSOmar Sandoval 				 size_t count, loff_t *ppos)
6494a46f05eSOmar Sandoval {
6504a46f05eSOmar Sandoval 	struct seq_file *m = file->private_data;
6514a46f05eSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
6524a46f05eSOmar Sandoval 
6534a46f05eSOmar Sandoval 	hctx->run = 0;
6544a46f05eSOmar Sandoval 	return count;
6554a46f05eSOmar Sandoval }
6564a46f05eSOmar Sandoval 
6574a46f05eSOmar Sandoval static const struct file_operations hctx_run_fops = {
6584a46f05eSOmar Sandoval 	.open		= hctx_run_open,
6594a46f05eSOmar Sandoval 	.read		= seq_read,
6604a46f05eSOmar Sandoval 	.write		= hctx_run_write,
6614a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
6624a46f05eSOmar Sandoval 	.release	= single_release,
6634a46f05eSOmar Sandoval };
6644a46f05eSOmar Sandoval 
6654a46f05eSOmar Sandoval static int hctx_active_show(struct seq_file *m, void *v)
6664a46f05eSOmar Sandoval {
6674a46f05eSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
6684a46f05eSOmar Sandoval 
6694a46f05eSOmar Sandoval 	seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
6704a46f05eSOmar Sandoval 	return 0;
6714a46f05eSOmar Sandoval }
6724a46f05eSOmar Sandoval 
6734a46f05eSOmar Sandoval static int hctx_active_open(struct inode *inode, struct file *file)
6744a46f05eSOmar Sandoval {
6754a46f05eSOmar Sandoval 	return single_open(file, hctx_active_show, inode->i_private);
6764a46f05eSOmar Sandoval }
6774a46f05eSOmar Sandoval 
6784a46f05eSOmar Sandoval static const struct file_operations hctx_active_fops = {
6794a46f05eSOmar Sandoval 	.open		= hctx_active_open,
6804a46f05eSOmar Sandoval 	.read		= seq_read,
6814a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
6824a46f05eSOmar Sandoval 	.release	= single_release,
6834a46f05eSOmar Sandoval };
6844a46f05eSOmar Sandoval 
685950cd7e9SOmar Sandoval static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
686f3bcb0e6SBart Van Assche 	__acquires(&ctx->lock)
687950cd7e9SOmar Sandoval {
688950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
689950cd7e9SOmar Sandoval 
690950cd7e9SOmar Sandoval 	spin_lock(&ctx->lock);
691950cd7e9SOmar Sandoval 	return seq_list_start(&ctx->rq_list, *pos);
692950cd7e9SOmar Sandoval }
693950cd7e9SOmar Sandoval 
694950cd7e9SOmar Sandoval static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
695950cd7e9SOmar Sandoval {
696950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
697950cd7e9SOmar Sandoval 
698950cd7e9SOmar Sandoval 	return seq_list_next(v, &ctx->rq_list, pos);
699950cd7e9SOmar Sandoval }
700950cd7e9SOmar Sandoval 
701950cd7e9SOmar Sandoval static void ctx_rq_list_stop(struct seq_file *m, void *v)
702f3bcb0e6SBart Van Assche 	__releases(&ctx->lock)
703950cd7e9SOmar Sandoval {
704950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
705950cd7e9SOmar Sandoval 
706950cd7e9SOmar Sandoval 	spin_unlock(&ctx->lock);
707950cd7e9SOmar Sandoval }
708950cd7e9SOmar Sandoval 
709950cd7e9SOmar Sandoval static const struct seq_operations ctx_rq_list_seq_ops = {
710950cd7e9SOmar Sandoval 	.start	= ctx_rq_list_start,
711950cd7e9SOmar Sandoval 	.next	= ctx_rq_list_next,
712950cd7e9SOmar Sandoval 	.stop	= ctx_rq_list_stop,
713950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
714950cd7e9SOmar Sandoval };
715950cd7e9SOmar Sandoval 
716950cd7e9SOmar Sandoval static int ctx_rq_list_open(struct inode *inode, struct file *file)
717950cd7e9SOmar Sandoval {
718950cd7e9SOmar Sandoval 	return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops);
719950cd7e9SOmar Sandoval }
720950cd7e9SOmar Sandoval 
721950cd7e9SOmar Sandoval static const struct file_operations ctx_rq_list_fops = {
722950cd7e9SOmar Sandoval 	.open		= ctx_rq_list_open,
723950cd7e9SOmar Sandoval 	.read		= seq_read,
724950cd7e9SOmar Sandoval 	.llseek		= seq_lseek,
725950cd7e9SOmar Sandoval 	.release	= seq_release,
726950cd7e9SOmar Sandoval };
727950cd7e9SOmar Sandoval 
7284a46f05eSOmar Sandoval static int ctx_dispatched_show(struct seq_file *m, void *v)
7294a46f05eSOmar Sandoval {
7304a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
7314a46f05eSOmar Sandoval 
7324a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
7334a46f05eSOmar Sandoval 	return 0;
7344a46f05eSOmar Sandoval }
7354a46f05eSOmar Sandoval 
7364a46f05eSOmar Sandoval static int ctx_dispatched_open(struct inode *inode, struct file *file)
7374a46f05eSOmar Sandoval {
7384a46f05eSOmar Sandoval 	return single_open(file, ctx_dispatched_show, inode->i_private);
7394a46f05eSOmar Sandoval }
7404a46f05eSOmar Sandoval 
7414a46f05eSOmar Sandoval static ssize_t ctx_dispatched_write(struct file *file, const char __user *buf,
7424a46f05eSOmar Sandoval 				    size_t count, loff_t *ppos)
7434a46f05eSOmar Sandoval {
7444a46f05eSOmar Sandoval 	struct seq_file *m = file->private_data;
7454a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
7464a46f05eSOmar Sandoval 
7474a46f05eSOmar Sandoval 	ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
7484a46f05eSOmar Sandoval 	return count;
7494a46f05eSOmar Sandoval }
7504a46f05eSOmar Sandoval 
7514a46f05eSOmar Sandoval static const struct file_operations ctx_dispatched_fops = {
7524a46f05eSOmar Sandoval 	.open		= ctx_dispatched_open,
7534a46f05eSOmar Sandoval 	.read		= seq_read,
7544a46f05eSOmar Sandoval 	.write		= ctx_dispatched_write,
7554a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
7564a46f05eSOmar Sandoval 	.release	= single_release,
7574a46f05eSOmar Sandoval };
7584a46f05eSOmar Sandoval 
7594a46f05eSOmar Sandoval static int ctx_merged_show(struct seq_file *m, void *v)
7604a46f05eSOmar Sandoval {
7614a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
7624a46f05eSOmar Sandoval 
7634a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", ctx->rq_merged);
7644a46f05eSOmar Sandoval 	return 0;
7654a46f05eSOmar Sandoval }
7664a46f05eSOmar Sandoval 
7674a46f05eSOmar Sandoval static int ctx_merged_open(struct inode *inode, struct file *file)
7684a46f05eSOmar Sandoval {
7694a46f05eSOmar Sandoval 	return single_open(file, ctx_merged_show, inode->i_private);
7704a46f05eSOmar Sandoval }
7714a46f05eSOmar Sandoval 
7724a46f05eSOmar Sandoval static ssize_t ctx_merged_write(struct file *file, const char __user *buf,
7734a46f05eSOmar Sandoval 				    size_t count, loff_t *ppos)
7744a46f05eSOmar Sandoval {
7754a46f05eSOmar Sandoval 	struct seq_file *m = file->private_data;
7764a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
7774a46f05eSOmar Sandoval 
7784a46f05eSOmar Sandoval 	ctx->rq_merged = 0;
7794a46f05eSOmar Sandoval 	return count;
7804a46f05eSOmar Sandoval }
7814a46f05eSOmar Sandoval 
7824a46f05eSOmar Sandoval static const struct file_operations ctx_merged_fops = {
7834a46f05eSOmar Sandoval 	.open		= ctx_merged_open,
7844a46f05eSOmar Sandoval 	.read		= seq_read,
7854a46f05eSOmar Sandoval 	.write		= ctx_merged_write,
7864a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
7874a46f05eSOmar Sandoval 	.release	= single_release,
7884a46f05eSOmar Sandoval };
7894a46f05eSOmar Sandoval 
7904a46f05eSOmar Sandoval static int ctx_completed_show(struct seq_file *m, void *v)
7914a46f05eSOmar Sandoval {
7924a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
7934a46f05eSOmar Sandoval 
7944a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
7954a46f05eSOmar Sandoval 	return 0;
7964a46f05eSOmar Sandoval }
7974a46f05eSOmar Sandoval 
7984a46f05eSOmar Sandoval static int ctx_completed_open(struct inode *inode, struct file *file)
7994a46f05eSOmar Sandoval {
8004a46f05eSOmar Sandoval 	return single_open(file, ctx_completed_show, inode->i_private);
8014a46f05eSOmar Sandoval }
8024a46f05eSOmar Sandoval 
8034a46f05eSOmar Sandoval static ssize_t ctx_completed_write(struct file *file, const char __user *buf,
8044a46f05eSOmar Sandoval 				   size_t count, loff_t *ppos)
8054a46f05eSOmar Sandoval {
8064a46f05eSOmar Sandoval 	struct seq_file *m = file->private_data;
8074a46f05eSOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
8084a46f05eSOmar Sandoval 
8094a46f05eSOmar Sandoval 	ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
8104a46f05eSOmar Sandoval 	return count;
8114a46f05eSOmar Sandoval }
8124a46f05eSOmar Sandoval 
8134a46f05eSOmar Sandoval static const struct file_operations ctx_completed_fops = {
8144a46f05eSOmar Sandoval 	.open		= ctx_completed_open,
8154a46f05eSOmar Sandoval 	.read		= seq_read,
8164a46f05eSOmar Sandoval 	.write		= ctx_completed_write,
8174a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
8184a46f05eSOmar Sandoval 	.release	= single_release,
8194a46f05eSOmar Sandoval };
8204a46f05eSOmar Sandoval 
82134dbad5dSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
82234dbad5dSOmar Sandoval 	{"poll_stat", 0400, &queue_poll_stat_fops},
82365ca1ca3SBart Van Assche 	{"state", 0600, &blk_queue_flags_fops},
82434dbad5dSOmar Sandoval 	{},
82534dbad5dSOmar Sandoval };
82634dbad5dSOmar Sandoval 
82707e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
8289abb2ad2SOmar Sandoval 	{"state", 0400, &hctx_state_fops},
8299abb2ad2SOmar Sandoval 	{"flags", 0400, &hctx_flags_fops},
830950cd7e9SOmar Sandoval 	{"dispatch", 0400, &hctx_dispatch_fops},
8310bfa5288SOmar Sandoval 	{"ctx_map", 0400, &hctx_ctx_map_fops},
832d96b37c0SOmar Sandoval 	{"tags", 0400, &hctx_tags_fops},
833d7e3621aSOmar Sandoval 	{"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
834d96b37c0SOmar Sandoval 	{"sched_tags", 0400, &hctx_sched_tags_fops},
835d7e3621aSOmar Sandoval 	{"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
836be215473SOmar Sandoval 	{"io_poll", 0600, &hctx_io_poll_fops},
837be215473SOmar Sandoval 	{"dispatched", 0600, &hctx_dispatched_fops},
8384a46f05eSOmar Sandoval 	{"queued", 0600, &hctx_queued_fops},
8394a46f05eSOmar Sandoval 	{"run", 0600, &hctx_run_fops},
8404a46f05eSOmar Sandoval 	{"active", 0400, &hctx_active_fops},
84172f2f8f6SBart Van Assche 	{},
84207e4feadSOmar Sandoval };
84307e4feadSOmar Sandoval 
84407e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
845950cd7e9SOmar Sandoval 	{"rq_list", 0400, &ctx_rq_list_fops},
8464a46f05eSOmar Sandoval 	{"dispatched", 0600, &ctx_dispatched_fops},
8474a46f05eSOmar Sandoval 	{"merged", 0600, &ctx_merged_fops},
8484a46f05eSOmar Sandoval 	{"completed", 0600, &ctx_completed_fops},
84972f2f8f6SBart Van Assche 	{},
85007e4feadSOmar Sandoval };
85107e4feadSOmar Sandoval 
8524c9e4019SBart Van Assche int blk_mq_debugfs_register(struct request_queue *q)
85307e4feadSOmar Sandoval {
85418fbda91SOmar Sandoval 	if (!blk_debugfs_root)
85507e4feadSOmar Sandoval 		return -ENOENT;
85607e4feadSOmar Sandoval 
8574c9e4019SBart Van Assche 	q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
8584c9e4019SBart Van Assche 					    blk_debugfs_root);
85907e4feadSOmar Sandoval 	if (!q->debugfs_dir)
86007e4feadSOmar Sandoval 		goto err;
86107e4feadSOmar Sandoval 
86262d6c949SBart Van Assche 	if (blk_mq_debugfs_register_mq(q))
86307e4feadSOmar Sandoval 		goto err;
86407e4feadSOmar Sandoval 
86507e4feadSOmar Sandoval 	return 0;
86607e4feadSOmar Sandoval 
86707e4feadSOmar Sandoval err:
86807e4feadSOmar Sandoval 	blk_mq_debugfs_unregister(q);
86907e4feadSOmar Sandoval 	return -ENOMEM;
87007e4feadSOmar Sandoval }
87107e4feadSOmar Sandoval 
87207e4feadSOmar Sandoval void blk_mq_debugfs_unregister(struct request_queue *q)
87307e4feadSOmar Sandoval {
87407e4feadSOmar Sandoval 	debugfs_remove_recursive(q->debugfs_dir);
87507e4feadSOmar Sandoval 	q->mq_debugfs_dir = NULL;
87607e4feadSOmar Sandoval 	q->debugfs_dir = NULL;
87707e4feadSOmar Sandoval }
87807e4feadSOmar Sandoval 
87972f2f8f6SBart Van Assche static bool debugfs_create_files(struct dentry *parent, void *data,
88072f2f8f6SBart Van Assche 				const struct blk_mq_debugfs_attr *attr)
88172f2f8f6SBart Van Assche {
88272f2f8f6SBart Van Assche 	for (; attr->name; attr++) {
88372f2f8f6SBart Van Assche 		if (!debugfs_create_file(attr->name, attr->mode, parent,
88472f2f8f6SBart Van Assche 					 data, attr->fops))
88572f2f8f6SBart Van Assche 			return false;
88672f2f8f6SBart Van Assche 	}
88772f2f8f6SBart Van Assche 	return true;
88872f2f8f6SBart Van Assche }
88972f2f8f6SBart Van Assche 
89007e4feadSOmar Sandoval static int blk_mq_debugfs_register_ctx(struct request_queue *q,
89107e4feadSOmar Sandoval 				       struct blk_mq_ctx *ctx,
89207e4feadSOmar Sandoval 				       struct dentry *hctx_dir)
89307e4feadSOmar Sandoval {
89407e4feadSOmar Sandoval 	struct dentry *ctx_dir;
89507e4feadSOmar Sandoval 	char name[20];
89607e4feadSOmar Sandoval 
89707e4feadSOmar Sandoval 	snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
89807e4feadSOmar Sandoval 	ctx_dir = debugfs_create_dir(name, hctx_dir);
89907e4feadSOmar Sandoval 	if (!ctx_dir)
90007e4feadSOmar Sandoval 		return -ENOMEM;
90107e4feadSOmar Sandoval 
90272f2f8f6SBart Van Assche 	if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
90307e4feadSOmar Sandoval 		return -ENOMEM;
90407e4feadSOmar Sandoval 
90507e4feadSOmar Sandoval 	return 0;
90607e4feadSOmar Sandoval }
90707e4feadSOmar Sandoval 
90807e4feadSOmar Sandoval static int blk_mq_debugfs_register_hctx(struct request_queue *q,
90907e4feadSOmar Sandoval 					struct blk_mq_hw_ctx *hctx)
91007e4feadSOmar Sandoval {
91107e4feadSOmar Sandoval 	struct blk_mq_ctx *ctx;
91207e4feadSOmar Sandoval 	struct dentry *hctx_dir;
91307e4feadSOmar Sandoval 	char name[20];
91407e4feadSOmar Sandoval 	int i;
91507e4feadSOmar Sandoval 
91607e4feadSOmar Sandoval 	snprintf(name, sizeof(name), "%u", hctx->queue_num);
91707e4feadSOmar Sandoval 	hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
91807e4feadSOmar Sandoval 	if (!hctx_dir)
91907e4feadSOmar Sandoval 		return -ENOMEM;
92007e4feadSOmar Sandoval 
92172f2f8f6SBart Van Assche 	if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
92207e4feadSOmar Sandoval 		return -ENOMEM;
92307e4feadSOmar Sandoval 
92407e4feadSOmar Sandoval 	hctx_for_each_ctx(hctx, ctx, i) {
92507e4feadSOmar Sandoval 		if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
92607e4feadSOmar Sandoval 			return -ENOMEM;
92707e4feadSOmar Sandoval 	}
92807e4feadSOmar Sandoval 
92907e4feadSOmar Sandoval 	return 0;
93007e4feadSOmar Sandoval }
93107e4feadSOmar Sandoval 
93262d6c949SBart Van Assche int blk_mq_debugfs_register_mq(struct request_queue *q)
93307e4feadSOmar Sandoval {
93407e4feadSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
93507e4feadSOmar Sandoval 	int i;
93607e4feadSOmar Sandoval 
93707e4feadSOmar Sandoval 	if (!q->debugfs_dir)
93807e4feadSOmar Sandoval 		return -ENOENT;
93907e4feadSOmar Sandoval 
94007e4feadSOmar Sandoval 	q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
94107e4feadSOmar Sandoval 	if (!q->mq_debugfs_dir)
94207e4feadSOmar Sandoval 		goto err;
94307e4feadSOmar Sandoval 
94434dbad5dSOmar Sandoval 	if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
94534dbad5dSOmar Sandoval 		goto err;
94634dbad5dSOmar Sandoval 
94707e4feadSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
94807e4feadSOmar Sandoval 		if (blk_mq_debugfs_register_hctx(q, hctx))
94907e4feadSOmar Sandoval 			goto err;
95007e4feadSOmar Sandoval 	}
95107e4feadSOmar Sandoval 
95207e4feadSOmar Sandoval 	return 0;
95307e4feadSOmar Sandoval 
95407e4feadSOmar Sandoval err:
95562d6c949SBart Van Assche 	blk_mq_debugfs_unregister_mq(q);
95607e4feadSOmar Sandoval 	return -ENOMEM;
95707e4feadSOmar Sandoval }
95807e4feadSOmar Sandoval 
95962d6c949SBart Van Assche void blk_mq_debugfs_unregister_mq(struct request_queue *q)
96007e4feadSOmar Sandoval {
96107e4feadSOmar Sandoval 	debugfs_remove_recursive(q->mq_debugfs_dir);
96207e4feadSOmar Sandoval 	q->mq_debugfs_dir = NULL;
96307e4feadSOmar Sandoval }
964