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 #define SCSI_LOG_SPOOLSIZE 4096
21 #if (SCSI_LOG_SPOOLSIZE / SCSI_LOG_BUFSIZE) > BITS_PER_LONG
22 #warning SCSI logging bitmask too large
26 char buffer
[SCSI_LOG_SPOOLSIZE
];
30 static DEFINE_PER_CPU(struct scsi_log_buf
, scsi_format_log
);
32 static char *scsi_log_reserve_buffer(size_t *len
)
34 struct scsi_log_buf
*buf
;
35 unsigned long map_bits
= sizeof(buf
->buffer
) / SCSI_LOG_BUFSIZE
;
36 unsigned long idx
= 0;
39 buf
= this_cpu_ptr(&scsi_format_log
);
40 idx
= find_first_zero_bit(&buf
->map
, map_bits
);
41 if (likely(idx
< map_bits
)) {
42 while (test_and_set_bit(idx
, &buf
->map
)) {
43 idx
= find_next_zero_bit(&buf
->map
, map_bits
, idx
);
48 if (WARN_ON(idx
>= map_bits
)) {
52 *len
= SCSI_LOG_BUFSIZE
;
53 return buf
->buffer
+ idx
* SCSI_LOG_BUFSIZE
;
56 static void scsi_log_release_buffer(char *bufptr
)
58 struct scsi_log_buf
*buf
;
62 buf
= this_cpu_ptr(&scsi_format_log
);
63 if (bufptr
>= buf
->buffer
&&
64 bufptr
< buf
->buffer
+ SCSI_LOG_SPOOLSIZE
) {
65 idx
= (bufptr
- buf
->buffer
) / SCSI_LOG_BUFSIZE
;
66 ret
= test_and_clear_bit(idx
, &buf
->map
);
72 static inline const char *scmd_name(const struct scsi_cmnd
*scmd
)
74 return scmd
->request
->rq_disk
?
75 scmd
->request
->rq_disk
->disk_name
: NULL
;
78 static size_t sdev_format_header(char *logbuf
, size_t logbuf_len
,
79 const char *name
, int tag
)
84 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
87 if (WARN_ON(off
>= logbuf_len
))
91 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
96 void sdev_prefix_printk(const char *level
, const struct scsi_device
*sdev
,
97 const char *name
, const char *fmt
, ...)
101 size_t off
= 0, logbuf_len
;
106 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
111 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
113 if (!WARN_ON(off
>= logbuf_len
)) {
115 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
118 dev_printk(level
, &sdev
->sdev_gendev
, "%s", logbuf
);
119 scsi_log_release_buffer(logbuf
);
121 EXPORT_SYMBOL(sdev_prefix_printk
);
123 void scmd_printk(const char *level
, const struct scsi_cmnd
*scmd
,
124 const char *fmt
, ...)
128 size_t off
= 0, logbuf_len
;
130 if (!scmd
|| !scmd
->cmnd
)
133 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
136 off
= sdev_format_header(logbuf
, logbuf_len
, scmd_name(scmd
),
138 if (off
< logbuf_len
) {
140 off
+= vscnprintf(logbuf
+ off
, logbuf_len
- off
, fmt
, args
);
143 dev_printk(level
, &scmd
->device
->sdev_gendev
, "%s", logbuf
);
144 scsi_log_release_buffer(logbuf
);
146 EXPORT_SYMBOL(scmd_printk
);
148 static size_t scsi_format_opcode_name(char *buffer
, size_t buf_len
,
149 const unsigned char *cdbp
)
152 const char *cdb_name
= NULL
, *sa_name
= NULL
;
156 if (cdb0
== VARIABLE_LENGTH_CMD
) {
157 int len
= scsi_varlen_cdb_length(cdbp
);
160 off
= scnprintf(buffer
, buf_len
,
161 "short variable length command, len=%d",
165 sa
= (cdbp
[8] << 8) + cdbp
[9];
169 if (!scsi_opcode_sa_name(cdb0
, sa
, &cdb_name
, &sa_name
)) {
171 off
= scnprintf(buffer
, buf_len
, "%s", cdb_name
);
173 off
= scnprintf(buffer
, buf_len
, "opcode=0x%x", cdb0
);
174 if (WARN_ON(off
>= buf_len
))
176 if (cdb0
>= VENDOR_SPECIFIC_CDB
)
177 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
179 else if (cdb0
>= 0x60 && cdb0
< 0x7e)
180 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
185 off
= scnprintf(buffer
, buf_len
, "%s", sa_name
);
187 off
= scnprintf(buffer
, buf_len
, "%s, sa=0x%x",
190 off
= scnprintf(buffer
, buf_len
,
191 "opcode=0x%x, sa=0x%x", cdb0
, sa
);
193 WARN_ON(off
>= buf_len
);
197 size_t __scsi_format_command(char *logbuf
, size_t logbuf_len
,
198 const unsigned char *cdb
, size_t cdb_len
)
203 off
= scsi_format_opcode_name(logbuf
, logbuf_len
, cdb
);
204 if (off
>= logbuf_len
)
206 len
= scsi_command_size(cdb
);
209 /* print out all bytes in cdb */
210 for (k
= 0; k
< len
; ++k
) {
211 if (off
> logbuf_len
- 3)
213 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
218 EXPORT_SYMBOL(__scsi_format_command
);
220 void scsi_print_command(struct scsi_cmnd
*cmd
)
224 size_t off
, logbuf_len
;
229 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
233 off
= sdev_format_header(logbuf
, logbuf_len
,
234 scmd_name(cmd
), cmd
->request
->tag
);
235 if (off
>= logbuf_len
)
237 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "CDB: ");
238 if (WARN_ON(off
>= logbuf_len
))
241 off
+= scsi_format_opcode_name(logbuf
+ off
, logbuf_len
- off
,
243 if (off
>= logbuf_len
)
246 /* print out all bytes in cdb */
247 if (cmd
->cmd_len
> 16) {
248 /* Print opcode in one line and use separate lines for CDB */
249 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "\n");
250 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
251 scsi_log_release_buffer(logbuf
);
252 for (k
= 0; k
< cmd
->cmd_len
; k
+= 16) {
253 size_t linelen
= min(cmd
->cmd_len
- k
, 16);
255 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
258 off
= sdev_format_header(logbuf
, logbuf_len
,
261 if (!WARN_ON(off
> logbuf_len
- 58)) {
262 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
264 hex_dump_to_buffer(&cmd
->cmnd
[k
], linelen
,
266 logbuf_len
- off
, false);
268 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s",
270 scsi_log_release_buffer(logbuf
);
274 if (!WARN_ON(off
> logbuf_len
- 49)) {
275 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, " ");
276 hex_dump_to_buffer(cmd
->cmnd
, cmd
->cmd_len
, 16, 1,
277 logbuf
+ off
, logbuf_len
- off
,
281 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
282 scsi_log_release_buffer(logbuf
);
284 EXPORT_SYMBOL(scsi_print_command
);
287 scsi_format_extd_sense(char *buffer
, size_t buf_len
,
288 unsigned char asc
, unsigned char ascq
)
291 const char *extd_sense_fmt
= NULL
;
292 const char *extd_sense_str
= scsi_extd_sense_format(asc
, ascq
,
295 if (extd_sense_str
) {
296 off
= scnprintf(buffer
, buf_len
, "Add. Sense: %s",
299 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
300 "(%s%x)", extd_sense_fmt
, ascq
);
303 off
= scnprintf(buffer
, buf_len
, "<<vendor>>");
304 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
307 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
309 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
316 scsi_format_sense_hdr(char *buffer
, size_t buf_len
,
317 const struct scsi_sense_hdr
*sshdr
)
319 const char *sense_txt
;
322 off
= scnprintf(buffer
, buf_len
, "Sense Key : ");
323 sense_txt
= scsi_sense_key_string(sshdr
->sense_key
);
325 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
328 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
329 "0x%x ", sshdr
->sense_key
);
330 off
+= scnprintf(buffer
+ off
, buf_len
- off
,
331 scsi_sense_is_deferred(sshdr
) ? "[deferred] " : "[current] ");
333 if (sshdr
->response_code
>= 0x72)
334 off
+= scnprintf(buffer
+ off
, buf_len
- off
, "[descriptor] ");
339 scsi_log_dump_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
340 const unsigned char *sense_buffer
, int sense_len
)
346 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
350 for (i
= 0; i
< sense_len
; i
+= 16) {
351 int len
= min(sense_len
- i
, 16);
354 off
= sdev_format_header(logbuf
, logbuf_len
,
356 hex_dump_to_buffer(&sense_buffer
[i
], len
, 16, 1,
357 logbuf
+ off
, logbuf_len
- off
,
359 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
361 scsi_log_release_buffer(logbuf
);
365 scsi_log_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
366 int tag
, const struct scsi_sense_hdr
*sshdr
)
369 size_t off
, logbuf_len
;
371 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
374 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
375 off
+= scsi_format_sense_hdr(logbuf
+ off
, logbuf_len
- off
, sshdr
);
376 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
377 scsi_log_release_buffer(logbuf
);
379 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
382 off
= sdev_format_header(logbuf
, logbuf_len
, name
, tag
);
383 off
+= scsi_format_extd_sense(logbuf
+ off
, logbuf_len
- off
,
384 sshdr
->asc
, sshdr
->ascq
);
385 dev_printk(KERN_INFO
, &sdev
->sdev_gendev
, "%s", logbuf
);
386 scsi_log_release_buffer(logbuf
);
390 scsi_log_print_sense(const struct scsi_device
*sdev
, const char *name
, int tag
,
391 const unsigned char *sense_buffer
, int sense_len
)
393 struct scsi_sense_hdr sshdr
;
395 if (scsi_normalize_sense(sense_buffer
, sense_len
, &sshdr
))
396 scsi_log_print_sense_hdr(sdev
, name
, tag
, &sshdr
);
398 scsi_log_dump_sense(sdev
, name
, tag
, sense_buffer
, sense_len
);
402 * Print normalized SCSI sense header with a prefix.
405 scsi_print_sense_hdr(const struct scsi_device
*sdev
, const char *name
,
406 const struct scsi_sense_hdr
*sshdr
)
408 scsi_log_print_sense_hdr(sdev
, name
, -1, sshdr
);
410 EXPORT_SYMBOL(scsi_print_sense_hdr
);
412 /* Normalize and print sense buffer with name prefix */
413 void __scsi_print_sense(const struct scsi_device
*sdev
, const char *name
,
414 const unsigned char *sense_buffer
, int sense_len
)
416 scsi_log_print_sense(sdev
, name
, -1, sense_buffer
, sense_len
);
418 EXPORT_SYMBOL(__scsi_print_sense
);
420 /* Normalize and print sense buffer in SCSI command */
421 void scsi_print_sense(const struct scsi_cmnd
*cmd
)
423 scsi_log_print_sense(cmd
->device
, scmd_name(cmd
), cmd
->request
->tag
,
424 cmd
->sense_buffer
, SCSI_SENSE_BUFFERSIZE
);
426 EXPORT_SYMBOL(scsi_print_sense
);
428 void scsi_print_result(const struct scsi_cmnd
*cmd
, const char *msg
,
432 size_t off
, logbuf_len
;
433 const char *mlret_string
= scsi_mlreturn_string(disposition
);
434 const char *hb_string
= scsi_hostbyte_string(cmd
->result
);
435 const char *db_string
= scsi_driverbyte_string(cmd
->result
);
437 logbuf
= scsi_log_reserve_buffer(&logbuf_len
);
441 off
= sdev_format_header(logbuf
, logbuf_len
,
442 scmd_name(cmd
), cmd
->request
->tag
);
444 if (off
>= logbuf_len
)
448 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
450 if (WARN_ON(off
>= logbuf_len
))
454 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
455 "%s ", mlret_string
);
457 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
458 "UNKNOWN(0x%02x) ", disposition
);
459 if (WARN_ON(off
>= logbuf_len
))
462 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
, "Result: ");
463 if (WARN_ON(off
>= logbuf_len
))
467 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
468 "hostbyte=%s ", hb_string
);
470 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
471 "hostbyte=0x%02x ", host_byte(cmd
->result
));
472 if (WARN_ON(off
>= logbuf_len
))
476 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
477 "driverbyte=%s", db_string
);
479 off
+= scnprintf(logbuf
+ off
, logbuf_len
- off
,
480 "driverbyte=0x%02x", driver_byte(cmd
->result
));
482 dev_printk(KERN_INFO
, &cmd
->device
->sdev_gendev
, "%s", logbuf
);
483 scsi_log_release_buffer(logbuf
);
485 EXPORT_SYMBOL(scsi_print_result
);