1// SPDX-License-Identifier: GPL-2.0
2#include <linux/bitops.h>
3#include <linux/seq_file.h>
4#include <scsi/scsi_cmnd.h>
5#include <scsi/scsi_dbg.h>
6#include <scsi/scsi_host.h>
7#include "scsi_debugfs.h"
8
9#define SCSI_CMD_FLAG_NAME(name)[const_ilog2(SCMD_##name)] = #name
10static const char *const scsi_cmd_flags[] = {
11	SCSI_CMD_FLAG_NAME(TAGGED),
12	SCSI_CMD_FLAG_NAME(INITIALIZED),
13	SCSI_CMD_FLAG_NAME(LAST),
14};
15#undef SCSI_CMD_FLAG_NAME
16
17static int scsi_flags_show(struct seq_file *m, const unsigned long flags,
18			   const char *const *flag_name, int flag_name_count)
19{
20	bool sep = false;
21	int i;
22
23	for_each_set_bit(i, &flags, BITS_PER_LONG) {
24		if (sep)
25			seq_puts(m, "|");
26		sep = true;
27		if (i < flag_name_count && flag_name[i])
28			seq_puts(m, flag_name[i]);
29		else
30			seq_printf(m, "%d", i);
31	}
32	return 0;
33}
34
35void scsi_show_rq(struct seq_file *m, struct request *rq)
36{
37	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq), *cmd2;
38	struct Scsi_Host *shost = cmd->device->host;
39	int alloc_ms = jiffies_to_msecs(jiffies - cmd->jiffies_at_alloc);
40	int timeout_ms = jiffies_to_msecs(rq->timeout);
41	const char *list_info = NULL;
42	char buf[80] = "(?)";
43
44	spin_lock_irq(shost->host_lock);
45	list_for_each_entry(cmd2, &shost->eh_abort_list, eh_entry) {
46		if (cmd == cmd2) {
47			list_info = "on eh_abort_list";
48			goto unlock;
49		}
50	}
51	list_for_each_entry(cmd2, &shost->eh_cmd_q, eh_entry) {
52		if (cmd == cmd2) {
53			list_info = "on eh_cmd_q";
54			goto unlock;
55		}
56	}
57unlock:
58	spin_unlock_irq(shost->host_lock);
59
60	__scsi_format_command(buf, sizeof(buf), cmd->cmnd, cmd->cmd_len);
61	seq_printf(m, ", .cmd=%s, .retries=%d, .allowed=%d, .result = %#x, %s%s.flags=",
62		   buf, cmd->retries, cmd->allowed, cmd->result,
63		   list_info ? : "", list_info ? ", " : "");
64	scsi_flags_show(m, cmd->flags, scsi_cmd_flags,
65			ARRAY_SIZE(scsi_cmd_flags));
66	seq_printf(m, ", .timeout=%d.%03d, allocated %d.%03d s ago",
67		   timeout_ms / 1000, timeout_ms % 1000,
68		   alloc_ms / 1000, alloc_ms % 1000);
69}
70