xref: /openbmc/linux/drivers/scsi/scsi_debugfs.c (revision 7ce05074)
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_debugfs.h"
7 
8 #define SCSI_CMD_FLAG_NAME(name)[const_ilog2(SCMD_##name)] = #name
9 static const char *const scsi_cmd_flags[] = {
10 	SCSI_CMD_FLAG_NAME(TAGGED),
11 	SCSI_CMD_FLAG_NAME(INITIALIZED),
12 };
13 #undef SCSI_CMD_FLAG_NAME
14 
15 static int scsi_flags_show(struct seq_file *m, const unsigned long flags,
16 			   const char *const *flag_name, int flag_name_count)
17 {
18 	bool sep = false;
19 	int i;
20 
21 	for_each_set_bit(i, &flags, BITS_PER_LONG) {
22 		if (sep)
23 			seq_puts(m, "|");
24 		sep = true;
25 		if (i < flag_name_count && flag_name[i])
26 			seq_puts(m, flag_name[i]);
27 		else
28 			seq_printf(m, "%d", i);
29 	}
30 	return 0;
31 }
32 
33 void scsi_show_rq(struct seq_file *m, struct request *rq)
34 {
35 	struct scsi_cmnd *cmd = container_of(scsi_req(rq), typeof(*cmd), req);
36 	int alloc_ms = jiffies_to_msecs(jiffies - cmd->jiffies_at_alloc);
37 	int timeout_ms = jiffies_to_msecs(rq->timeout);
38 	const u8 *const cdb = READ_ONCE(cmd->cmnd);
39 	char buf[80] = "(?)";
40 
41 	if (cdb)
42 		__scsi_format_command(buf, sizeof(buf), cdb, cmd->cmd_len);
43 	seq_printf(m, ", .cmd=%s, .retries=%d, .result = %#x, .flags=", buf,
44 		   cmd->retries, cmd->result);
45 	scsi_flags_show(m, cmd->flags, scsi_cmd_flags,
46 			ARRAY_SIZE(scsi_cmd_flags));
47 	seq_printf(m, ", .timeout=%d.%03d, allocated %d.%03d s ago",
48 		   timeout_ms / 1000, timeout_ms % 1000,
49 		   alloc_ms / 1000, alloc_ms % 1000);
50 }
51