4 * Copyright (C) 2014 SUSE Linux Products GmbH
5 * Copyright (C) 2014 Hannes Reinecke <hare@suse.de>
7 * This file is released under the GPLv2
10 #include <linux/kernel.h>
11 #include <linux/atomic.h>
13 #include <scsi/scsi.h>
14 #include <scsi/scsi_cmnd.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_eh.h>
17 #include <scsi/scsi_dbg.h>
19 static char *scsi_log_reserve_buffer(size_t *len
)
22 return kmalloc(*len
, GFP_ATOMIC
);
25 static void scsi_log_release_buffer(char *bufptr
)
30 static inline const char *scmd_name(const struct scsi_cmnd
*scmd
)
32 return scmd
->request
->rq_disk
?
33 scmd
->request
->rq_disk
->disk_name
: NULL
;
36 static size_t sdev_format_header(char *logbuf
, size_t logbuf_len
,
37 const char *name
, int tag
)
42 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
45 if (WARN_ON(off
>= logbuf_len
))
49 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
54 void sdev_prefix_printk(const char *level
, const struct scsi_device
*sdev
,
55 const char *name
, const char *fmt
, ...)
59 size_t off
= 0, logbuf_len
;
64 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
69 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
71 if (!WARN_ON(off
>= logbuf_len
)) {
73 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
76 dev_printk(level
, &sdev
->sdev_gendev
, "%s", logbuf
);
77 scsi_log_release_buffer(logbuf
);
79 EXPORT_SYMBOL(sdev_prefix_printk
);
81 void scmd_printk(const char *level
, const struct scsi_cmnd
*scmd
,
86 size_t off
= 0, logbuf_len
;
88 if (!scmd
|| !scmd
->cmnd
)
91 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
94 off
= sdev_format_header(logbuf
, logbuf_len
, scmd_name(scmd
),
96 if (off
< logbuf_len
) {
98 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
101 dev_printk(level
, &scmd
->device
->sdev_gendev
, "%s", logbuf
);
102 scsi_log_release_buffer(logbuf
);
104 EXPORT_SYMBOL(scmd_printk
);
106 static size_t scsi_format_opcode_name(char *buffer
, size_t buf_len
,
107 const unsigned char *cdbp
)
110 const char *cdb_name
= NULL
, *sa_name
= NULL
;
114 if (cdb0
== VARIABLE_LENGTH_CMD
) {
115 int len
= scsi_varlen_cdb_length(cdbp
);
118 off
= scnprintf(buffer
, buf_len
,
119 "short variable length command, len=%d",
123 sa
= (cdbp
[8] << 8) + cdbp
[9];
127 if (!scsi_opcode_sa_name(cdb0
, sa
, &cdb_name
, &sa_name
)) {
129 off
= scnprintf(buffer
, buf_len
, "%s", cdb_name
);
131 off
= scnprintf(buffer
, buf_len
, "opcode=0x%x", cdb0
);
132 if (WARN_ON(off
>= buf_len
))
134 if (cdb0
>= VENDOR_SPECIFIC_CDB
)
135 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
137 else if (cdb0
>= 0x60 && cdb0
< 0x7e)
138 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
143 off
= scnprintf(buffer
, buf_len
, "%s", sa_name
);
145 off
= scnprintf(buffer
, buf_len
, "%s, sa=0x%x",
148 off
= scnprintf(buffer
, buf_len
,
149 "opcode=0x%x, sa=0x%x", cdb0
, sa
);
151 WARN_ON(off
>= buf_len
);
155 size_t __scsi_format_command(char *logbuf
, size_t logbuf_len
,
156 const unsigned char *cdb
, size_t cdb_len
)
161 off
= scsi_format_opcode_name(logbuf
, logbuf_len
, cdb
);
162 if (off
>= logbuf_len
)
164 len
= scsi_command_size(cdb
);
167 /* print out all bytes in cdb */
168 for (k
= 0; k
< len
; ++k
) {
169 if (off
> logbuf_len
- 3)
171 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
176 EXPORT_SYMBOL(__scsi_format_command
);
178 void scsi_print_command(struct scsi_cmnd
*cmd
)
182 size_t off
, logbuf_len
;
187 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
191 off
= sdev_format_header(logbuf
, logbuf_len
,
192 scmd_name(cmd
), cmd
->request
->tag
);
193 if (off
>= logbuf_len
)
195 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "CDB: ");
196 if (WARN_ON(off
>= logbuf_len
))
199 off
+= scsi_format_opcode_name(logbuf
+ off
, logbuf_len
- off
,
201 if (off
>= logbuf_len
)
204 /* print out all bytes in cdb */
205 if (cmd
->cmd_len
> 16) {
206 /* Print opcode in one line and use separate lines for CDB */
207 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "\n");
208 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
209 scsi_log_release_buffer(logbuf
);
210 for (k
= 0; k
< cmd
->cmd_len
; k
+= 16) {
211 size_t linelen
= min(cmd
->cmd_len
- k
, 16);
213 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
216 off
= sdev_format_header(logbuf
, logbuf_len
,
219 if (!WARN_ON(off
> logbuf_len
- 58)) {
220 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
222 hex_dump_to_buffer(&cmd
->cmnd
[k
], linelen
,
224 logbuf_len
- off
, false);
226 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s",
228 scsi_log_release_buffer(logbuf
);
232 if (!WARN_ON(off
> logbuf_len
- 49)) {
233 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, " ");
234 hex_dump_to_buffer(cmd
->cmnd
, cmd
->cmd_len
, 16, 1,
235 logbuf
+ off
, logbuf_len
- off
,
239 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
240 scsi_log_release_buffer(logbuf
);
242 EXPORT_SYMBOL(scsi_print_command
);
245 scsi_format_extd_sense(char *buffer
, size_t buf_len
,
246 unsigned char asc
, unsigned char ascq
)
249 const char *extd_sense_fmt
= NULL
;
250 const char *extd_sense_str
= scsi_extd_sense_format(asc
, ascq
,
253 if (extd_sense_str
) {
254 off
= scnprintf(buffer
, buf_len
, "Add. Sense: %s",
257 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
258 "(%s%x)", extd_sense_fmt
, ascq
);
261 off
= scnprintf(buffer
, buf_len
, "<<vendor>>");
262 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
265 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
267 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
274 scsi_format_sense_hdr(char *buffer
, size_t buf_len
,
275 const struct scsi_sense_hdr
*sshdr
)
277 const char *sense_txt
;
280 off
= scnprintf(buffer
, buf_len
, "Sense Key : ");
281 sense_txt
= scsi_sense_key_string(sshdr
->sense_key
);
283 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
286 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
287 "0x%x ", sshdr
->sense_key
);
288 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
289 scsi_sense_is_deferred(sshdr
) ? "[deferred] " : "[current] ");
291 if (sshdr
->response_code
>= 0x72)
292 off
+= scnprintf(buffer
+ off
, buf_len
- off
, "[descriptor] ");
297 scsi_log_dump_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
298 const unsigned char *sense_buffer
, int sense_len
)
304 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
308 for (i
= 0; i
< sense_len
; i
+= 16) {
309 int len
= min(sense_len
- i
, 16);
312 off
= sdev_format_header(logbuf
, logbuf_len
,
314 hex_dump_to_buffer(&sense_buffer
[i
], len
, 16, 1,
315 logbuf
+ off
, logbuf_len
- off
,
317 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
319 scsi_log_release_buffer(logbuf
);
323 scsi_log_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
324 int tag
, const struct scsi_sense_hdr
*sshdr
)
327 size_t off
, logbuf_len
;
329 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
332 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
333 off
+= scsi_format_sense_hdr(logbuf
+ off
, logbuf_len
- off
, sshdr
);
334 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
335 scsi_log_release_buffer(logbuf
);
337 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
340 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
341 off
+= scsi_format_extd_sense(logbuf
+ off
, logbuf_len
- off
,
342 sshdr
->asc
, sshdr
->ascq
);
343 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
344 scsi_log_release_buffer(logbuf
);
348 scsi_log_print_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
349 const unsigned char *sense_buffer
, int sense_len
)
351 struct scsi_sense_hdr sshdr
;
353 if (scsi_normalize_sense(sense_buffer
, sense_len
, &sshdr
))
354 scsi_log_print_sense_hdr(sdev
, name
, tag
, &sshdr
);
356 scsi_log_dump_sense(sdev
, name
, tag
, sense_buffer
, sense_len
);
360 * Print normalized SCSI sense header with a prefix.
363 scsi_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
364 const struct scsi_sense_hdr
*sshdr
)
366 scsi_log_print_sense_hdr(sdev
, name
, -1, sshdr
);
368 EXPORT_SYMBOL(scsi_print_sense_hdr
);
370 /* Normalize and print sense buffer with name prefix */
371 void __scsi_print_sense(const struct scsi_device
*sdev
, const char *name
,
372 const unsigned char *sense_buffer
, int sense_len
)
374 scsi_log_print_sense(sdev
, name
, -1, sense_buffer
, sense_len
);
376 EXPORT_SYMBOL(__scsi_print_sense
);
378 /* Normalize and print sense buffer in SCSI command */
379 void scsi_print_sense(const struct scsi_cmnd
*cmd
)
381 scsi_log_print_sense(cmd
->device
, scmd_name(cmd
), cmd
->request
->tag
,
382 cmd
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
);
384 EXPORT_SYMBOL(scsi_print_sense
);
386 void scsi_print_result(const struct scsi_cmnd
*cmd
, const char *msg
,
390 size_t off
, logbuf_len
;
391 const char *mlret_string
= scsi_mlreturn_string(disposition
);
392 const char *hb_string
= scsi_hostbyte_string(cmd
->result
);
393 const char *db_string
= scsi_driverbyte_string(cmd
->result
);
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", driver_byte(cmd
->result
));
440 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
441 scsi_log_release_buffer(logbuf
);
443 EXPORT_SYMBOL(scsi_print_result
);