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 return scmd
->request
->rq_disk
?
32 scmd
->request
->rq_disk
->disk_name
: NULL
;
35 static size_t sdev_format_header(char *logbuf
, size_t logbuf_len
,
36 const char *name
, int tag
)
41 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
44 if (WARN_ON(off
>= logbuf_len
))
48 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
53 void sdev_prefix_printk(const char *level
, const struct scsi_device
*sdev
,
54 const char *name
, const char *fmt
, ...)
58 size_t off
= 0, logbuf_len
;
63 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
68 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
70 if (!WARN_ON(off
>= logbuf_len
)) {
72 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
75 dev_printk(level
, &sdev
->sdev_gendev
, "%s", logbuf
);
76 scsi_log_release_buffer(logbuf
);
78 EXPORT_SYMBOL(sdev_prefix_printk
);
80 void scmd_printk(const char *level
, const struct scsi_cmnd
*scmd
,
85 size_t off
= 0, logbuf_len
;
87 if (!scmd
|| !scmd
->cmnd
)
90 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
93 off
= sdev_format_header(logbuf
, logbuf_len
, scmd_name(scmd
),
95 if (off
< logbuf_len
) {
97 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
100 dev_printk(level
, &scmd
->device
->sdev_gendev
, "%s", logbuf
);
101 scsi_log_release_buffer(logbuf
);
103 EXPORT_SYMBOL(scmd_printk
);
105 static size_t scsi_format_opcode_name(char *buffer
, size_t buf_len
,
106 const unsigned char *cdbp
)
109 const char *cdb_name
= NULL
, *sa_name
= NULL
;
113 if (cdb0
== VARIABLE_LENGTH_CMD
) {
114 int len
= scsi_varlen_cdb_length(cdbp
);
117 off
= scnprintf(buffer
, buf_len
,
118 "short variable length command, len=%d",
122 sa
= (cdbp
[8] << 8) + cdbp
[9];
126 if (!scsi_opcode_sa_name(cdb0
, sa
, &cdb_name
, &sa_name
)) {
128 off
= scnprintf(buffer
, buf_len
, "%s", cdb_name
);
130 off
= scnprintf(buffer
, buf_len
, "opcode=0x%x", cdb0
);
131 if (WARN_ON(off
>= buf_len
))
133 if (cdb0
>= VENDOR_SPECIFIC_CDB
)
134 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
136 else if (cdb0
>= 0x60 && cdb0
< 0x7e)
137 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
142 off
= scnprintf(buffer
, buf_len
, "%s", sa_name
);
144 off
= scnprintf(buffer
, buf_len
, "%s, sa=0x%x",
147 off
= scnprintf(buffer
, buf_len
,
148 "opcode=0x%x, sa=0x%x", cdb0
, sa
);
150 WARN_ON(off
>= buf_len
);
154 size_t __scsi_format_command(char *logbuf
, size_t logbuf_len
,
155 const unsigned char *cdb
, size_t cdb_len
)
160 off
= scsi_format_opcode_name(logbuf
, logbuf_len
, cdb
);
161 if (off
>= logbuf_len
)
163 len
= scsi_command_size(cdb
);
166 /* print out all bytes in cdb */
167 for (k
= 0; k
< len
; ++k
) {
168 if (off
> logbuf_len
- 3)
170 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
175 EXPORT_SYMBOL(__scsi_format_command
);
177 void scsi_print_command(struct scsi_cmnd
*cmd
)
181 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
), cmd
->request
->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 scsi_log_release_buffer(logbuf
);
209 for (k
= 0; k
< cmd
->cmd_len
; k
+= 16) {
210 size_t linelen
= min(cmd
->cmd_len
- k
, 16);
212 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
215 off
= sdev_format_header(logbuf
, logbuf_len
,
218 if (!WARN_ON(off
> logbuf_len
- 58)) {
219 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
221 hex_dump_to_buffer(&cmd
->cmnd
[k
], linelen
,
223 logbuf_len
- off
, false);
225 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s",
227 scsi_log_release_buffer(logbuf
);
231 if (!WARN_ON(off
> logbuf_len
- 49)) {
232 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, " ");
233 hex_dump_to_buffer(cmd
->cmnd
, cmd
->cmd_len
, 16, 1,
234 logbuf
+ off
, logbuf_len
- off
,
238 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
239 scsi_log_release_buffer(logbuf
);
241 EXPORT_SYMBOL(scsi_print_command
);
244 scsi_format_extd_sense(char *buffer
, size_t buf_len
,
245 unsigned char asc
, unsigned char ascq
)
248 const char *extd_sense_fmt
= NULL
;
249 const char *extd_sense_str
= scsi_extd_sense_format(asc
, ascq
,
252 if (extd_sense_str
) {
253 off
= scnprintf(buffer
, buf_len
, "Add. Sense: %s",
256 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
257 "(%s%x)", extd_sense_fmt
, ascq
);
260 off
= scnprintf(buffer
, buf_len
, "<<vendor>>");
261 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
264 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
266 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
273 scsi_format_sense_hdr(char *buffer
, size_t buf_len
,
274 const struct scsi_sense_hdr
*sshdr
)
276 const char *sense_txt
;
279 off
= scnprintf(buffer
, buf_len
, "Sense Key : ");
280 sense_txt
= scsi_sense_key_string(sshdr
->sense_key
);
282 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
285 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
286 "0x%x ", sshdr
->sense_key
);
287 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
288 scsi_sense_is_deferred(sshdr
) ? "[deferred] " : "[current] ");
290 if (sshdr
->response_code
>= 0x72)
291 off
+= scnprintf(buffer
+ off
, buf_len
- off
, "[descriptor] ");
296 scsi_log_dump_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
297 const unsigned char *sense_buffer
, int sense_len
)
303 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
307 for (i
= 0; i
< sense_len
; i
+= 16) {
308 int len
= min(sense_len
- i
, 16);
311 off
= sdev_format_header(logbuf
, logbuf_len
,
313 hex_dump_to_buffer(&sense_buffer
[i
], len
, 16, 1,
314 logbuf
+ off
, logbuf_len
- off
,
316 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
318 scsi_log_release_buffer(logbuf
);
322 scsi_log_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
323 int tag
, const struct scsi_sense_hdr
*sshdr
)
326 size_t off
, logbuf_len
;
328 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
331 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
332 off
+= scsi_format_sense_hdr(logbuf
+ off
, logbuf_len
- off
, sshdr
);
333 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
334 scsi_log_release_buffer(logbuf
);
336 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
339 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
340 off
+= scsi_format_extd_sense(logbuf
+ off
, logbuf_len
- off
,
341 sshdr
->asc
, sshdr
->ascq
);
342 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
343 scsi_log_release_buffer(logbuf
);
347 scsi_log_print_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
348 const unsigned char *sense_buffer
, int sense_len
)
350 struct scsi_sense_hdr sshdr
;
352 if (scsi_normalize_sense(sense_buffer
, sense_len
, &sshdr
))
353 scsi_log_print_sense_hdr(sdev
, name
, tag
, &sshdr
);
355 scsi_log_dump_sense(sdev
, name
, tag
, sense_buffer
, sense_len
);
359 * Print normalized SCSI sense header with a prefix.
362 scsi_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
363 const struct scsi_sense_hdr
*sshdr
)
365 scsi_log_print_sense_hdr(sdev
, name
, -1, sshdr
);
367 EXPORT_SYMBOL(scsi_print_sense_hdr
);
369 /* Normalize and print sense buffer with name prefix */
370 void __scsi_print_sense(const struct scsi_device
*sdev
, const char *name
,
371 const unsigned char *sense_buffer
, int sense_len
)
373 scsi_log_print_sense(sdev
, name
, -1, sense_buffer
, sense_len
);
375 EXPORT_SYMBOL(__scsi_print_sense
);
377 /* Normalize and print sense buffer in SCSI command */
378 void scsi_print_sense(const struct scsi_cmnd
*cmd
)
380 scsi_log_print_sense(cmd
->device
, scmd_name(cmd
), cmd
->request
->tag
,
381 cmd
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
);
383 EXPORT_SYMBOL(scsi_print_sense
);
385 void scsi_print_result(const struct scsi_cmnd
*cmd
, const char *msg
,
389 size_t off
, logbuf_len
;
390 const char *mlret_string
= scsi_mlreturn_string(disposition
);
391 const char *hb_string
= scsi_hostbyte_string(cmd
->result
);
392 const char *db_string
= scsi_driverbyte_string(cmd
->result
);
393 unsigned long cmd_age
= (jiffies
- cmd
->jiffies_at_alloc
) / HZ
;
395 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
399 off
= sdev_format_header(logbuf
, logbuf_len
,
400 scmd_name(cmd
), cmd
->request
->tag
);
402 if (off
>= logbuf_len
)
406 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
408 if (WARN_ON(off
>= logbuf_len
))
412 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
413 "%s ", mlret_string
);
415 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
416 "UNKNOWN(0x%02x) ", disposition
);
417 if (WARN_ON(off
>= logbuf_len
))
420 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "Result: ");
421 if (WARN_ON(off
>= logbuf_len
))
425 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
426 "hostbyte=%s ", hb_string
);
428 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
429 "hostbyte=0x%02x ", host_byte(cmd
->result
));
430 if (WARN_ON(off
>= logbuf_len
))
434 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
435 "driverbyte=%s ", db_string
);
437 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
438 "driverbyte=0x%02x ",
439 driver_byte(cmd
->result
));
441 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
442 "cmd_age=%lus", cmd_age
);
445 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
446 scsi_log_release_buffer(logbuf
);
448 EXPORT_SYMBOL(scsi_print_result
);