1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2014 SUSE Linux Products GmbH
6 * Copyright (C) 2014 Hannes Reinecke <hare@suse.de>
9 #include <linux/kernel.h>
10 #include <linux/atomic.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_cmnd.h>
14 #include <scsi/scsi_device.h>
15 #include <scsi/scsi_eh.h>
16 #include <scsi/scsi_dbg.h>
18 static char *scsi_log_reserve_buffer(size_t *len
)
21 return kmalloc(*len
, GFP_ATOMIC
);
24 static void scsi_log_release_buffer(char *bufptr
)
29 static inline const char *scmd_name(const struct scsi_cmnd
*scmd
)
31 struct request
*rq
= scsi_cmd_to_rq((struct scsi_cmnd
*)scmd
);
33 if (!rq
->q
|| !rq
->q
->disk
)
35 return rq
->q
->disk
->disk_name
;
38 static size_t sdev_format_header(char *logbuf
, size_t logbuf_len
,
39 const char *name
, int tag
)
44 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
47 if (WARN_ON(off
>= logbuf_len
))
51 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
56 void sdev_prefix_printk(const char *level
, const struct scsi_device
*sdev
,
57 const char *name
, const char *fmt
, ...)
61 size_t off
= 0, logbuf_len
;
66 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
71 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
73 if (!WARN_ON(off
>= logbuf_len
)) {
75 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
78 dev_printk(level
, &sdev
->sdev_gendev
, "%s", logbuf
);
79 scsi_log_release_buffer(logbuf
);
81 EXPORT_SYMBOL(sdev_prefix_printk
);
83 void scmd_printk(const char *level
, const struct scsi_cmnd
*scmd
,
88 size_t off
= 0, logbuf_len
;
93 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
96 off
= sdev_format_header(logbuf
, logbuf_len
, scmd_name(scmd
),
97 scsi_cmd_to_rq((struct scsi_cmnd
*)scmd
)->tag
);
98 if (off
< logbuf_len
) {
100 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
103 dev_printk(level
, &scmd
->device
->sdev_gendev
, "%s", logbuf
);
104 scsi_log_release_buffer(logbuf
);
106 EXPORT_SYMBOL(scmd_printk
);
108 static size_t scsi_format_opcode_name(char *buffer
, size_t buf_len
,
109 const unsigned char *cdbp
)
112 const char *cdb_name
= NULL
, *sa_name
= NULL
;
116 if (cdb0
== VARIABLE_LENGTH_CMD
) {
117 int len
= scsi_varlen_cdb_length(cdbp
);
120 off
= scnprintf(buffer
, buf_len
,
121 "short variable length command, len=%d",
125 sa
= (cdbp
[8] << 8) + cdbp
[9];
129 if (!scsi_opcode_sa_name(cdb0
, sa
, &cdb_name
, &sa_name
)) {
131 off
= scnprintf(buffer
, buf_len
, "%s", cdb_name
);
133 off
= scnprintf(buffer
, buf_len
, "opcode=0x%x", cdb0
);
134 if (WARN_ON(off
>= buf_len
))
136 if (cdb0
>= VENDOR_SPECIFIC_CDB
)
137 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
139 else if (cdb0
>= 0x60 && cdb0
< 0x7e)
140 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
145 off
= scnprintf(buffer
, buf_len
, "%s", sa_name
);
147 off
= scnprintf(buffer
, buf_len
, "%s, sa=0x%x",
150 off
= scnprintf(buffer
, buf_len
,
151 "opcode=0x%x, sa=0x%x", cdb0
, sa
);
153 WARN_ON(off
>= buf_len
);
157 size_t __scsi_format_command(char *logbuf
, size_t logbuf_len
,
158 const unsigned char *cdb
, size_t cdb_len
)
163 off
= scsi_format_opcode_name(logbuf
, logbuf_len
, cdb
);
164 if (off
>= logbuf_len
)
166 len
= scsi_command_size(cdb
);
169 /* print out all bytes in cdb */
170 for (k
= 0; k
< len
; ++k
) {
171 if (off
> logbuf_len
- 3)
173 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
178 EXPORT_SYMBOL(__scsi_format_command
);
180 void scsi_print_command(struct scsi_cmnd
*cmd
)
184 size_t off
, logbuf_len
;
186 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
190 off
= sdev_format_header(logbuf
, logbuf_len
,
191 scmd_name(cmd
), scsi_cmd_to_rq(cmd
)->tag
);
192 if (off
>= logbuf_len
)
194 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "CDB: ");
195 if (WARN_ON(off
>= logbuf_len
))
198 off
+= scsi_format_opcode_name(logbuf
+ off
, logbuf_len
- off
,
200 if (off
>= logbuf_len
)
203 /* print out all bytes in cdb */
204 if (cmd
->cmd_len
> 16) {
205 /* Print opcode in one line and use separate lines for CDB */
206 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "\n");
207 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
208 for (k
= 0; k
< cmd
->cmd_len
; k
+= 16) {
209 size_t linelen
= min(cmd
->cmd_len
- k
, 16);
211 off
= sdev_format_header(logbuf
, logbuf_len
,
213 scsi_cmd_to_rq(cmd
)->tag
);
214 if (!WARN_ON(off
> logbuf_len
- 58)) {
215 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
217 hex_dump_to_buffer(&cmd
->cmnd
[k
], linelen
,
219 logbuf_len
- off
, false);
221 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s",
226 if (!WARN_ON(off
> logbuf_len
- 49)) {
227 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, " ");
228 hex_dump_to_buffer(cmd
->cmnd
, cmd
->cmd_len
, 16, 1,
229 logbuf
+ off
, logbuf_len
- off
,
233 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
235 scsi_log_release_buffer(logbuf
);
237 EXPORT_SYMBOL(scsi_print_command
);
240 scsi_format_extd_sense(char *buffer
, size_t buf_len
,
241 unsigned char asc
, unsigned char ascq
)
244 const char *extd_sense_fmt
= NULL
;
245 const char *extd_sense_str
= scsi_extd_sense_format(asc
, ascq
,
248 if (extd_sense_str
) {
249 off
= scnprintf(buffer
, buf_len
, "Add. Sense: %s",
252 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
253 "(%s%x)", extd_sense_fmt
, ascq
);
256 off
= scnprintf(buffer
, buf_len
, "<<vendor>>");
257 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
260 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
262 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
269 scsi_format_sense_hdr(char *buffer
, size_t buf_len
,
270 const struct scsi_sense_hdr
*sshdr
)
272 const char *sense_txt
;
275 off
= scnprintf(buffer
, buf_len
, "Sense Key : ");
276 sense_txt
= scsi_sense_key_string(sshdr
->sense_key
);
278 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
281 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
282 "0x%x ", sshdr
->sense_key
);
283 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
284 scsi_sense_is_deferred(sshdr
) ? "[deferred] " : "[current] ");
286 if (sshdr
->response_code
>= 0x72)
287 off
+= scnprintf(buffer
+ off
, buf_len
- off
, "[descriptor] ");
292 scsi_log_dump_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
293 const unsigned char *sense_buffer
, int sense_len
)
299 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
303 for (i
= 0; i
< sense_len
; i
+= 16) {
304 int len
= min(sense_len
- i
, 16);
307 off
= sdev_format_header(logbuf
, logbuf_len
,
309 hex_dump_to_buffer(&sense_buffer
[i
], len
, 16, 1,
310 logbuf
+ off
, logbuf_len
- off
,
312 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
314 scsi_log_release_buffer(logbuf
);
318 scsi_log_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
319 int tag
, const struct scsi_sense_hdr
*sshdr
)
322 size_t off
, logbuf_len
;
324 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
327 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
328 off
+= scsi_format_sense_hdr(logbuf
+ off
, logbuf_len
- off
, sshdr
);
329 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
330 scsi_log_release_buffer(logbuf
);
332 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
335 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
336 off
+= scsi_format_extd_sense(logbuf
+ off
, logbuf_len
- off
,
337 sshdr
->asc
, sshdr
->ascq
);
338 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
339 scsi_log_release_buffer(logbuf
);
343 scsi_log_print_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
344 const unsigned char *sense_buffer
, int sense_len
)
346 struct scsi_sense_hdr sshdr
;
348 if (scsi_normalize_sense(sense_buffer
, sense_len
, &sshdr
))
349 scsi_log_print_sense_hdr(sdev
, name
, tag
, &sshdr
);
351 scsi_log_dump_sense(sdev
, name
, tag
, sense_buffer
, sense_len
);
355 * Print normalized SCSI sense header with a prefix.
358 scsi_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
359 const struct scsi_sense_hdr
*sshdr
)
361 scsi_log_print_sense_hdr(sdev
, name
, -1, sshdr
);
363 EXPORT_SYMBOL(scsi_print_sense_hdr
);
365 /* Normalize and print sense buffer with name prefix */
366 void __scsi_print_sense(const struct scsi_device
*sdev
, const char *name
,
367 const unsigned char *sense_buffer
, int sense_len
)
369 scsi_log_print_sense(sdev
, name
, -1, sense_buffer
, sense_len
);
371 EXPORT_SYMBOL(__scsi_print_sense
);
373 /* Normalize and print sense buffer in SCSI command */
374 void scsi_print_sense(const struct scsi_cmnd
*cmd
)
376 scsi_log_print_sense(cmd
->device
, scmd_name(cmd
),
377 scsi_cmd_to_rq((struct scsi_cmnd
*)cmd
)->tag
,
378 cmd
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
);
380 EXPORT_SYMBOL(scsi_print_sense
);
382 void scsi_print_result(const struct scsi_cmnd
*cmd
, const char *msg
,
386 size_t off
, logbuf_len
;
387 const char *mlret_string
= scsi_mlreturn_string(disposition
);
388 const char *hb_string
= scsi_hostbyte_string(cmd
->result
);
389 unsigned long cmd_age
= (jiffies
- cmd
->jiffies_at_alloc
) / HZ
;
391 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
395 off
= sdev_format_header(logbuf
, logbuf_len
, scmd_name(cmd
),
396 scsi_cmd_to_rq((struct scsi_cmnd
*)cmd
)->tag
);
398 if (off
>= logbuf_len
)
402 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
404 if (WARN_ON(off
>= logbuf_len
))
408 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
409 "%s ", mlret_string
);
411 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
412 "UNKNOWN(0x%02x) ", disposition
);
413 if (WARN_ON(off
>= logbuf_len
))
416 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "Result: ");
417 if (WARN_ON(off
>= logbuf_len
))
421 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
422 "hostbyte=%s ", hb_string
);
424 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
425 "hostbyte=0x%02x ", host_byte(cmd
->result
));
426 if (WARN_ON(off
>= logbuf_len
))
429 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
430 "driverbyte=DRIVER_OK ");
432 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
433 "cmd_age=%lus", cmd_age
);
436 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
437 scsi_log_release_buffer(logbuf
);
439 EXPORT_SYMBOL(scsi_print_result
);