blob: 1f8f80b2dbfcb38b7598092cd06675c10191d912 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Hannes Reineckeded85c12015-01-08 07:43:42 +01002/*
3 * scsi_logging.c
4 *
5 * Copyright (C) 2014 SUSE Linux Products GmbH
6 * Copyright (C) 2014 Hannes Reinecke <hare@suse.de>
Hannes Reineckeded85c12015-01-08 07:43:42 +01007 */
8
9#include <linux/kernel.h>
10#include <linux/atomic.h>
11
12#include <scsi/scsi.h>
13#include <scsi/scsi_cmnd.h>
14#include <scsi/scsi_device.h>
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010015#include <scsi/scsi_eh.h>
Hannes Reineckeded85c12015-01-08 07:43:42 +010016#include <scsi/scsi_dbg.h>
17
Hannes Reineckeded85c12015-01-08 07:43:42 +010018static char *scsi_log_reserve_buffer(size_t *len)
19{
Bart Van Asschedccc96a2019-08-01 15:38:14 -070020 *len = 128;
21 return kmalloc(*len, GFP_ATOMIC);
Hannes Reineckeded85c12015-01-08 07:43:42 +010022}
23
24static void scsi_log_release_buffer(char *bufptr)
25{
Bart Van Asschedccc96a2019-08-01 15:38:14 -070026 kfree(bufptr);
Hannes Reineckeded85c12015-01-08 07:43:42 +010027}
28
Hannes Reinecke21045512015-01-08 07:43:46 +010029static inline const char *scmd_name(const struct scsi_cmnd *scmd)
30{
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -070031 struct request *rq = scsi_cmd_to_rq((struct scsi_cmnd *)scmd);
32
Christoph Hellwigf3fa33a2021-11-26 13:18:00 +010033 if (!rq->q->disk)
34 return NULL;
35 return rq->q->disk->disk_name;
Hannes Reinecke21045512015-01-08 07:43:46 +010036}
37
38static size_t sdev_format_header(char *logbuf, size_t logbuf_len,
39 const char *name, int tag)
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010040{
41 size_t off = 0;
42
Hannes Reinecke21045512015-01-08 07:43:46 +010043 if (name)
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010044 off += scnprintf(logbuf + off, logbuf_len - off,
Hannes Reinecke21045512015-01-08 07:43:46 +010045 "[%s] ", name);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010046
47 if (WARN_ON(off >= logbuf_len))
48 return off;
49
50 if (tag >= 0)
51 off += scnprintf(logbuf + off, logbuf_len - off,
52 "tag#%d ", tag);
53 return off;
54}
55
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010056void sdev_prefix_printk(const char *level, const struct scsi_device *sdev,
57 const char *name, const char *fmt, ...)
Hannes Reineckeded85c12015-01-08 07:43:42 +010058{
59 va_list args;
60 char *logbuf;
61 size_t off = 0, logbuf_len;
Hannes Reineckeded85c12015-01-08 07:43:42 +010062
63 if (!sdev)
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010064 return;
Hannes Reineckeded85c12015-01-08 07:43:42 +010065
66 logbuf = scsi_log_reserve_buffer(&logbuf_len);
67 if (!logbuf)
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010068 return;
Hannes Reineckeded85c12015-01-08 07:43:42 +010069
70 if (name)
71 off += scnprintf(logbuf + off, logbuf_len - off,
72 "[%s] ", name);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010073 if (!WARN_ON(off >= logbuf_len)) {
74 va_start(args, fmt);
75 off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
76 va_end(args);
77 }
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010078 dev_printk(level, &sdev->sdev_gendev, "%s", logbuf);
Hannes Reineckeded85c12015-01-08 07:43:42 +010079 scsi_log_release_buffer(logbuf);
Hannes Reineckeded85c12015-01-08 07:43:42 +010080}
81EXPORT_SYMBOL(sdev_prefix_printk);
82
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010083void scmd_printk(const char *level, const struct scsi_cmnd *scmd,
Hannes Reineckeded85c12015-01-08 07:43:42 +010084 const char *fmt, ...)
85{
Hannes Reineckeded85c12015-01-08 07:43:42 +010086 va_list args;
87 char *logbuf;
88 size_t off = 0, logbuf_len;
Hannes Reineckeded85c12015-01-08 07:43:42 +010089
90 if (!scmd || !scmd->cmnd)
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010091 return;
Hannes Reineckeded85c12015-01-08 07:43:42 +010092
93 logbuf = scsi_log_reserve_buffer(&logbuf_len);
94 if (!logbuf)
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +010095 return;
Hannes Reinecke21045512015-01-08 07:43:46 +010096 off = sdev_format_header(logbuf, logbuf_len, scmd_name(scmd),
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -070097 scsi_cmd_to_rq((struct scsi_cmnd *)scmd)->tag);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +010098 if (off < logbuf_len) {
99 va_start(args, fmt);
100 off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
101 va_end(args);
102 }
Hannes Reinecke9c4a6b12015-02-04 08:11:34 +0100103 dev_printk(level, &scmd->device->sdev_gendev, "%s", logbuf);
Hannes Reineckeded85c12015-01-08 07:43:42 +0100104 scsi_log_release_buffer(logbuf);
Hannes Reineckeded85c12015-01-08 07:43:42 +0100105}
106EXPORT_SYMBOL(scmd_printk);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100107
108static size_t scsi_format_opcode_name(char *buffer, size_t buf_len,
109 const unsigned char *cdbp)
110{
111 int sa, cdb0;
112 const char *cdb_name = NULL, *sa_name = NULL;
113 size_t off;
114
115 cdb0 = cdbp[0];
116 if (cdb0 == VARIABLE_LENGTH_CMD) {
117 int len = scsi_varlen_cdb_length(cdbp);
118
119 if (len < 10) {
120 off = scnprintf(buffer, buf_len,
121 "short variable length command, len=%d",
122 len);
123 return off;
124 }
125 sa = (cdbp[8] << 8) + cdbp[9];
126 } else
127 sa = cdbp[1] & 0x1f;
128
129 if (!scsi_opcode_sa_name(cdb0, sa, &cdb_name, &sa_name)) {
130 if (cdb_name)
131 off = scnprintf(buffer, buf_len, "%s", cdb_name);
132 else {
133 off = scnprintf(buffer, buf_len, "opcode=0x%x", cdb0);
134 if (WARN_ON(off >= buf_len))
135 return off;
136 if (cdb0 >= VENDOR_SPECIFIC_CDB)
137 off += scnprintf(buffer + off, buf_len - off,
138 " (vendor)");
139 else if (cdb0 >= 0x60 && cdb0 < 0x7e)
140 off += scnprintf(buffer + off, buf_len - off,
141 " (reserved)");
142 }
143 } else {
144 if (sa_name)
145 off = scnprintf(buffer, buf_len, "%s", sa_name);
146 else if (cdb_name)
147 off = scnprintf(buffer, buf_len, "%s, sa=0x%x",
148 cdb_name, sa);
149 else
150 off = scnprintf(buffer, buf_len,
151 "opcode=0x%x, sa=0x%x", cdb0, sa);
152 }
153 WARN_ON(off >= buf_len);
154 return off;
155}
156
157size_t __scsi_format_command(char *logbuf, size_t logbuf_len,
158 const unsigned char *cdb, size_t cdb_len)
159{
160 int len, k;
161 size_t off;
162
163 off = scsi_format_opcode_name(logbuf, logbuf_len, cdb);
164 if (off >= logbuf_len)
165 return off;
166 len = scsi_command_size(cdb);
167 if (cdb_len < len)
168 len = cdb_len;
169 /* print out all bytes in cdb */
170 for (k = 0; k < len; ++k) {
171 if (off > logbuf_len - 3)
172 break;
173 off += scnprintf(logbuf + off, logbuf_len - off,
174 " %02x", cdb[k]);
175 }
176 return off;
177}
178EXPORT_SYMBOL(__scsi_format_command);
179
180void scsi_print_command(struct scsi_cmnd *cmd)
181{
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100182 int k;
183 char *logbuf;
184 size_t off, logbuf_len;
185
186 if (!cmd->cmnd)
187 return;
188
189 logbuf = scsi_log_reserve_buffer(&logbuf_len);
190 if (!logbuf)
191 return;
192
Hannes Reinecke21045512015-01-08 07:43:46 +0100193 off = sdev_format_header(logbuf, logbuf_len,
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -0700194 scmd_name(cmd), scsi_cmd_to_rq(cmd)->tag);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100195 if (off >= logbuf_len)
196 goto out_printk;
197 off += scnprintf(logbuf + off, logbuf_len - off, "CDB: ");
198 if (WARN_ON(off >= logbuf_len))
199 goto out_printk;
200
201 off += scsi_format_opcode_name(logbuf + off, logbuf_len - off,
202 cmd->cmnd);
203 if (off >= logbuf_len)
204 goto out_printk;
205
206 /* print out all bytes in cdb */
207 if (cmd->cmd_len > 16) {
208 /* Print opcode in one line and use separate lines for CDB */
209 off += scnprintf(logbuf + off, logbuf_len - off, "\n");
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100210 dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100211 for (k = 0; k < cmd->cmd_len; k += 16) {
212 size_t linelen = min(cmd->cmd_len - k, 16);
213
Hannes Reinecke21045512015-01-08 07:43:46 +0100214 off = sdev_format_header(logbuf, logbuf_len,
215 scmd_name(cmd),
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -0700216 scsi_cmd_to_rq(cmd)->tag);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100217 if (!WARN_ON(off > logbuf_len - 58)) {
218 off += scnprintf(logbuf + off, logbuf_len - off,
219 "CDB[%02x]: ", k);
220 hex_dump_to_buffer(&cmd->cmnd[k], linelen,
221 16, 1, logbuf + off,
222 logbuf_len - off, false);
223 }
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100224 dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s",
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100225 logbuf);
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100226 }
Ye Bin811f3942020-07-17 17:09:21 +0800227 goto out;
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100228 }
229 if (!WARN_ON(off > logbuf_len - 49)) {
230 off += scnprintf(logbuf + off, logbuf_len - off, " ");
231 hex_dump_to_buffer(cmd->cmnd, cmd->cmd_len, 16, 1,
232 logbuf + off, logbuf_len - off,
233 false);
234 }
235out_printk:
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100236 dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
Ye Bin811f3942020-07-17 17:09:21 +0800237out:
Hannes Reinecke9e5ed2a2015-01-08 07:43:44 +0100238 scsi_log_release_buffer(logbuf);
239}
240EXPORT_SYMBOL(scsi_print_command);
Hannes Reinecke21045512015-01-08 07:43:46 +0100241
242static size_t
243scsi_format_extd_sense(char *buffer, size_t buf_len,
244 unsigned char asc, unsigned char ascq)
245{
246 size_t off = 0;
247 const char *extd_sense_fmt = NULL;
248 const char *extd_sense_str = scsi_extd_sense_format(asc, ascq,
249 &extd_sense_fmt);
250
251 if (extd_sense_str) {
252 off = scnprintf(buffer, buf_len, "Add. Sense: %s",
253 extd_sense_str);
254 if (extd_sense_fmt)
255 off += scnprintf(buffer + off, buf_len - off,
256 "(%s%x)", extd_sense_fmt, ascq);
257 } else {
258 if (asc >= 0x80)
259 off = scnprintf(buffer, buf_len, "<<vendor>>");
260 off += scnprintf(buffer + off, buf_len - off,
261 "ASC=0x%x ", asc);
262 if (ascq >= 0x80)
263 off += scnprintf(buffer + off, buf_len - off,
264 "<<vendor>>");
265 off += scnprintf(buffer + off, buf_len - off,
266 "ASCQ=0x%x ", ascq);
267 }
268 return off;
269}
270
271static size_t
272scsi_format_sense_hdr(char *buffer, size_t buf_len,
273 const struct scsi_sense_hdr *sshdr)
274{
275 const char *sense_txt;
276 size_t off;
277
278 off = scnprintf(buffer, buf_len, "Sense Key : ");
279 sense_txt = scsi_sense_key_string(sshdr->sense_key);
280 if (sense_txt)
281 off += scnprintf(buffer + off, buf_len - off,
282 "%s ", sense_txt);
283 else
284 off += scnprintf(buffer + off, buf_len - off,
285 "0x%x ", sshdr->sense_key);
286 off += scnprintf(buffer + off, buf_len - off,
287 scsi_sense_is_deferred(sshdr) ? "[deferred] " : "[current] ");
288
289 if (sshdr->response_code >= 0x72)
290 off += scnprintf(buffer + off, buf_len - off, "[descriptor] ");
291 return off;
292}
293
294static void
295scsi_log_dump_sense(const struct scsi_device *sdev, const char *name, int tag,
296 const unsigned char *sense_buffer, int sense_len)
297{
298 char *logbuf;
299 size_t logbuf_len;
300 int i;
301
302 logbuf = scsi_log_reserve_buffer(&logbuf_len);
303 if (!logbuf)
304 return;
305
306 for (i = 0; i < sense_len; i += 16) {
307 int len = min(sense_len - i, 16);
308 size_t off;
309
310 off = sdev_format_header(logbuf, logbuf_len,
311 name, tag);
312 hex_dump_to_buffer(&sense_buffer[i], len, 16, 1,
313 logbuf + off, logbuf_len - off,
314 false);
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100315 dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
Hannes Reinecke21045512015-01-08 07:43:46 +0100316 }
317 scsi_log_release_buffer(logbuf);
318}
319
320static void
321scsi_log_print_sense_hdr(const struct scsi_device *sdev, const char *name,
322 int tag, const struct scsi_sense_hdr *sshdr)
323{
324 char *logbuf;
325 size_t off, logbuf_len;
326
327 logbuf = scsi_log_reserve_buffer(&logbuf_len);
328 if (!logbuf)
329 return;
330 off = sdev_format_header(logbuf, logbuf_len, name, tag);
331 off += scsi_format_sense_hdr(logbuf + off, logbuf_len - off, sshdr);
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100332 dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
Hannes Reinecke21045512015-01-08 07:43:46 +0100333 scsi_log_release_buffer(logbuf);
334
335 logbuf = scsi_log_reserve_buffer(&logbuf_len);
336 if (!logbuf)
337 return;
338 off = sdev_format_header(logbuf, logbuf_len, name, tag);
339 off += scsi_format_extd_sense(logbuf + off, logbuf_len - off,
340 sshdr->asc, sshdr->ascq);
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100341 dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
Hannes Reinecke21045512015-01-08 07:43:46 +0100342 scsi_log_release_buffer(logbuf);
343}
344
345static void
346scsi_log_print_sense(const struct scsi_device *sdev, const char *name, int tag,
347 const unsigned char *sense_buffer, int sense_len)
348{
349 struct scsi_sense_hdr sshdr;
350
351 if (scsi_normalize_sense(sense_buffer, sense_len, &sshdr))
352 scsi_log_print_sense_hdr(sdev, name, tag, &sshdr);
353 else
354 scsi_log_dump_sense(sdev, name, tag, sense_buffer, sense_len);
355}
356
357/*
358 * Print normalized SCSI sense header with a prefix.
359 */
360void
361scsi_print_sense_hdr(const struct scsi_device *sdev, const char *name,
362 const struct scsi_sense_hdr *sshdr)
363{
364 scsi_log_print_sense_hdr(sdev, name, -1, sshdr);
365}
366EXPORT_SYMBOL(scsi_print_sense_hdr);
367
368/* Normalize and print sense buffer with name prefix */
369void __scsi_print_sense(const struct scsi_device *sdev, const char *name,
370 const unsigned char *sense_buffer, int sense_len)
371{
372 scsi_log_print_sense(sdev, name, -1, sense_buffer, sense_len);
373}
374EXPORT_SYMBOL(__scsi_print_sense);
375
376/* Normalize and print sense buffer in SCSI command */
377void scsi_print_sense(const struct scsi_cmnd *cmd)
378{
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -0700379 scsi_log_print_sense(cmd->device, scmd_name(cmd),
380 scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag,
Hannes Reinecke21045512015-01-08 07:43:46 +0100381 cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
382}
383EXPORT_SYMBOL(scsi_print_sense);
Hannes Reinecke026f8da2015-01-08 07:43:47 +0100384
385void scsi_print_result(const struct scsi_cmnd *cmd, const char *msg,
386 int disposition)
387{
388 char *logbuf;
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);
Milan P. Gandhi8ee132b2019-09-26 10:55:02 +0530392 unsigned long cmd_age = (jiffies - cmd->jiffies_at_alloc) / HZ;
Hannes Reinecke026f8da2015-01-08 07:43:47 +0100393
394 logbuf = scsi_log_reserve_buffer(&logbuf_len);
395 if (!logbuf)
396 return;
397
Bart Van Asscheaa8e25e2021-08-09 16:03:05 -0700398 off = sdev_format_header(logbuf, logbuf_len, scmd_name(cmd),
399 scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag);
Hannes Reinecke026f8da2015-01-08 07:43:47 +0100400
401 if (off >= logbuf_len)
402 goto out_printk;
403
404 if (msg) {
405 off += scnprintf(logbuf + off, logbuf_len - off,
406 "%s: ", msg);
407 if (WARN_ON(off >= logbuf_len))
408 goto out_printk;
409 }
410 if (mlret_string)
411 off += scnprintf(logbuf + off, logbuf_len - off,
412 "%s ", mlret_string);
413 else
414 off += scnprintf(logbuf + off, logbuf_len - off,
415 "UNKNOWN(0x%02x) ", disposition);
416 if (WARN_ON(off >= logbuf_len))
417 goto out_printk;
418
419 off += scnprintf(logbuf + off, logbuf_len - off, "Result: ");
420 if (WARN_ON(off >= logbuf_len))
421 goto out_printk;
422
423 if (hb_string)
424 off += scnprintf(logbuf + off, logbuf_len - off,
425 "hostbyte=%s ", hb_string);
426 else
427 off += scnprintf(logbuf + off, logbuf_len - off,
428 "hostbyte=0x%02x ", host_byte(cmd->result));
429 if (WARN_ON(off >= logbuf_len))
430 goto out_printk;
431
Hannes Reinecke54c29082021-04-27 10:30:20 +0200432 off += scnprintf(logbuf + off, logbuf_len - off,
433 "driverbyte=DRIVER_OK ");
Milan P. Gandhi8ee132b2019-09-26 10:55:02 +0530434
435 off += scnprintf(logbuf + off, logbuf_len - off,
436 "cmd_age=%lus", cmd_age);
437
Hannes Reinecke026f8da2015-01-08 07:43:47 +0100438out_printk:
Christoph Hellwig2fc583c2015-01-18 16:11:28 +0100439 dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
Hannes Reinecke026f8da2015-01-08 07:43:47 +0100440 scsi_log_release_buffer(logbuf);
441}
442EXPORT_SYMBOL(scsi_print_result);