1*2e8b4c7fSandvar /* $NetBSD: scsi_sense.c,v 1.11 2023/12/28 19:58:11 andvar Exp $ */
2c9a47c22Sthorpej
3c9a47c22Sthorpej /*-
4c9a47c22Sthorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5c9a47c22Sthorpej * All rights reserved.
6c9a47c22Sthorpej *
7c9a47c22Sthorpej * This code is derived from software contributed to The NetBSD Foundation
8c9a47c22Sthorpej * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9c9a47c22Sthorpej * Simulation Facility, NASA Ames Research Center.
10c9a47c22Sthorpej *
11c9a47c22Sthorpej * Redistribution and use in source and binary forms, with or without
12c9a47c22Sthorpej * modification, are permitted provided that the following conditions
13c9a47c22Sthorpej * are met:
14c9a47c22Sthorpej * 1. Redistributions of source code must retain the above copyright
15c9a47c22Sthorpej * notice, this list of conditions and the following disclaimer.
16c9a47c22Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
17c9a47c22Sthorpej * notice, this list of conditions and the following disclaimer in the
18c9a47c22Sthorpej * documentation and/or other materials provided with the distribution.
19c9a47c22Sthorpej *
20c9a47c22Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21c9a47c22Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22c9a47c22Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23c9a47c22Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24c9a47c22Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25c9a47c22Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26c9a47c22Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27c9a47c22Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28c9a47c22Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29c9a47c22Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30c9a47c22Sthorpej * POSSIBILITY OF SUCH DAMAGE.
31c9a47c22Sthorpej */
32c9a47c22Sthorpej
33c9a47c22Sthorpej /*
34c9a47c22Sthorpej * SCSI sense interpretation. Lifted from src/sys/dev/scsipi/scsi_verbose.c,
35c9a47c22Sthorpej * and modified for userland.
36c9a47c22Sthorpej *
37c9a47c22Sthorpej * XXX THESE SHOULD BE IN A LIBRARY!
38c9a47c22Sthorpej */
39c2a3b5ecSagc #include <sys/cdefs.h>
40c2a3b5ecSagc
41c2a3b5ecSagc #ifndef lint
42*2e8b4c7fSandvar __RCSID("$NetBSD: scsi_sense.c,v 1.11 2023/12/28 19:58:11 andvar Exp $");
43c2a3b5ecSagc #endif
44c2a3b5ecSagc
45c9a47c22Sthorpej
46c9a47c22Sthorpej #include <sys/param.h>
47c9a47c22Sthorpej #include <sys/scsiio.h>
48c9a47c22Sthorpej #include <stdio.h>
49c9a47c22Sthorpej #include <string.h>
50c9a47c22Sthorpej
51c9a47c22Sthorpej #include <dev/scsipi/scsipi_all.h>
52c9a47c22Sthorpej #include <dev/scsipi/scsi_all.h>
53c9a47c22Sthorpej #include <dev/scsipi/scsipiconf.h>
54c9a47c22Sthorpej
55c9a47c22Sthorpej #include "extern.h"
56c9a47c22Sthorpej
57c9a47c22Sthorpej static const char *sense_keys[16] = {
58c9a47c22Sthorpej "No Additional Sense",
59c9a47c22Sthorpej "Recovered Error",
60c9a47c22Sthorpej "Not Ready",
61c9a47c22Sthorpej "Media Error",
62c9a47c22Sthorpej "Hardware Error",
63c9a47c22Sthorpej "Illegal Request",
64c9a47c22Sthorpej "Unit Attention",
65c9a47c22Sthorpej "Write Protected",
66c9a47c22Sthorpej "Blank Check",
67c9a47c22Sthorpej "Vendor Unique",
68c9a47c22Sthorpej "Copy Aborted",
69c9a47c22Sthorpej "Aborted Command",
70c9a47c22Sthorpej "Equal Error",
71c9a47c22Sthorpej "Volume Overflow",
72c9a47c22Sthorpej "Miscompare Error",
73c9a47c22Sthorpej "Reserved"
74c9a47c22Sthorpej };
75c9a47c22Sthorpej
76c9a47c22Sthorpej static const struct {
77c9a47c22Sthorpej unsigned char asc;
78c9a47c22Sthorpej unsigned char ascq;
79c9a47c22Sthorpej const char *description;
80c9a47c22Sthorpej } adesc[] = {
81c9a47c22Sthorpej { 0x00, 0x00, "No Additional Sense Information" },
82c9a47c22Sthorpej { 0x00, 0x01, "Filemark Detected" },
83c9a47c22Sthorpej { 0x00, 0x02, "End-Of-Partition/Medium Detected" },
84c9a47c22Sthorpej { 0x00, 0x03, "Setmark Detected" },
85c9a47c22Sthorpej { 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
86c9a47c22Sthorpej { 0x00, 0x05, "End-Of-Data Detected" },
87c9a47c22Sthorpej { 0x00, 0x06, "I/O Process Terminated" },
88c9a47c22Sthorpej { 0x00, 0x11, "Audio Play Operation In Progress" },
89c9a47c22Sthorpej { 0x00, 0x12, "Audio Play Operation Paused" },
90c9a47c22Sthorpej { 0x00, 0x13, "Audio Play Operation Successfully Completed" },
91c9a47c22Sthorpej { 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
92c9a47c22Sthorpej { 0x00, 0x15, "No Current Audio Status To Return" },
93c9a47c22Sthorpej { 0x01, 0x00, "No Index/Sector Signal" },
94c9a47c22Sthorpej { 0x02, 0x00, "No Seek Complete" },
95c9a47c22Sthorpej { 0x03, 0x00, "Peripheral Device Write Fault" },
96c9a47c22Sthorpej { 0x03, 0x01, "No Write Current" },
97c9a47c22Sthorpej { 0x03, 0x02, "Excessive Write Errors" },
98c9a47c22Sthorpej { 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
99c9a47c22Sthorpej { 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
100c9a47c22Sthorpej { 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
101c9a47c22Sthorpej { 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
102c9a47c22Sthorpej { 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
103c9a47c22Sthorpej { 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
104c9a47c22Sthorpej { 0x06, 0x00, "No Reference Position Found" },
105c9a47c22Sthorpej { 0x07, 0x00, "Multiple Peripheral Devices Selected" },
106c9a47c22Sthorpej { 0x08, 0x00, "Logical Unit Communication Failure" },
107c9a47c22Sthorpej { 0x08, 0x01, "Logical Unit Communication Timeout" },
108c9a47c22Sthorpej { 0x08, 0x02, "Logical Unit Communication Parity Error" },
109c9a47c22Sthorpej { 0x09, 0x00, "Track Following Error" },
110c9a47c22Sthorpej { 0x09, 0x01, "Tracking Servo Failure" },
111c9a47c22Sthorpej { 0x09, 0x02, "Focus Servo Failure" },
112c9a47c22Sthorpej { 0x09, 0x03, "Spindle Servo Failure" },
113c9a47c22Sthorpej { 0x0A, 0x00, "Error Log Overflow" },
114c9a47c22Sthorpej { 0x0C, 0x00, "Write Error" },
115c9a47c22Sthorpej { 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
116c9a47c22Sthorpej { 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
117c9a47c22Sthorpej { 0x10, 0x00, "ID CRC Or ECC Error" },
118c9a47c22Sthorpej { 0x11, 0x00, "Unrecovered Read Error" },
119c9a47c22Sthorpej { 0x11, 0x01, "Read Retried Exhausted" },
120c9a47c22Sthorpej { 0x11, 0x02, "Error Too Long To Correct" },
121c9a47c22Sthorpej { 0x11, 0x03, "Multiple Read Errors" },
122c9a47c22Sthorpej { 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
123c9a47c22Sthorpej { 0x11, 0x05, "L-EC Uncorrectable Error" },
124c9a47c22Sthorpej { 0x11, 0x06, "CIRC Unrecovered Error" },
125c9a47c22Sthorpej { 0x11, 0x07, "Data Resynchronization Error" },
126c9a47c22Sthorpej { 0x11, 0x08, "Incomplete Block Found" },
127c9a47c22Sthorpej { 0x11, 0x09, "No Gap Found" },
128c9a47c22Sthorpej { 0x11, 0x0A, "Miscorrected Error" },
129c9a47c22Sthorpej { 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
130c9a47c22Sthorpej { 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite the Data" },
131c9a47c22Sthorpej { 0x12, 0x00, "Address Mark Not Found for ID Field" },
132c9a47c22Sthorpej { 0x13, 0x00, "Address Mark Not Found for Data Field" },
133c9a47c22Sthorpej { 0x14, 0x00, "Recorded Entity Not Found" },
134c9a47c22Sthorpej { 0x14, 0x01, "Record Not Found" },
135c9a47c22Sthorpej { 0x14, 0x02, "Filemark or Setmark Not Found" },
136c9a47c22Sthorpej { 0x14, 0x03, "End-Of-Data Not Found" },
137c9a47c22Sthorpej { 0x14, 0x04, "Block Sequence Error" },
138c9a47c22Sthorpej { 0x15, 0x00, "Random Positioning Error" },
139c9a47c22Sthorpej { 0x15, 0x01, "Mechanical Positioning Error" },
140c9a47c22Sthorpej { 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
141c9a47c22Sthorpej { 0x16, 0x00, "Data Synchronization Mark Error" },
142c9a47c22Sthorpej { 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
143c9a47c22Sthorpej { 0x17, 0x01, "Recovered Data With Retries" },
144c9a47c22Sthorpej { 0x17, 0x02, "Recovered Data With Positive Head Offset" },
145c9a47c22Sthorpej { 0x17, 0x03, "Recovered Data With Negative Head Offset" },
146c9a47c22Sthorpej { 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
147c9a47c22Sthorpej { 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
148c9a47c22Sthorpej { 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
149c9a47c22Sthorpej { 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
150c9a47c22Sthorpej { 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
151c9a47c22Sthorpej { 0x18, 0x00, "Recovered Data With Error Correction Applied" },
152c9a47c22Sthorpej { 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
153c9a47c22Sthorpej { 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
154c9a47c22Sthorpej { 0x18, 0x03, "Recovered Data With CIRC" },
155c9a47c22Sthorpej { 0x18, 0x04, "Recovered Data With LEC" },
156c9a47c22Sthorpej { 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
157c9a47c22Sthorpej { 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
158c9a47c22Sthorpej { 0x19, 0x00, "Defect List Error" },
159c9a47c22Sthorpej { 0x19, 0x01, "Defect List Not Available" },
160c9a47c22Sthorpej { 0x19, 0x02, "Defect List Error in Primary List" },
161c9a47c22Sthorpej { 0x19, 0x03, "Defect List Error in Grown List" },
162c9a47c22Sthorpej { 0x1A, 0x00, "Parameter List Length Error" },
163c9a47c22Sthorpej { 0x1B, 0x00, "Synchronous Data Transfer Error" },
164c9a47c22Sthorpej { 0x1C, 0x00, "Defect List Not Found" },
165c9a47c22Sthorpej { 0x1C, 0x01, "Primary Defect List Not Found" },
166c9a47c22Sthorpej { 0x1C, 0x02, "Grown Defect List Not Found" },
167c9a47c22Sthorpej { 0x1D, 0x00, "Miscompare During Verify Operation" },
168c9a47c22Sthorpej { 0x1E, 0x00, "Recovered ID with ECC" },
169c9a47c22Sthorpej { 0x20, 0x00, "Invalid Command Operation Code" },
170c9a47c22Sthorpej { 0x21, 0x00, "Logical Block Address Out of Range" },
171c9a47c22Sthorpej { 0x21, 0x01, "Invalid Element Address" },
172c9a47c22Sthorpej { 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
173c9a47c22Sthorpej { 0x24, 0x00, "Illegal Field in CDB" },
174c9a47c22Sthorpej { 0x25, 0x00, "Logical Unit Not Supported" },
175c9a47c22Sthorpej { 0x26, 0x00, "Invalid Field In Parameter List" },
176c9a47c22Sthorpej { 0x26, 0x01, "Parameter Not Supported" },
177c9a47c22Sthorpej { 0x26, 0x02, "Parameter Value Invalid" },
178c9a47c22Sthorpej { 0x26, 0x03, "Threshold Parameters Not Supported" },
179c9a47c22Sthorpej { 0x27, 0x00, "Write Protected" },
180c9a47c22Sthorpej { 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
181c9a47c22Sthorpej { 0x28, 0x01, "Import Or Export Element Accessed" },
182c9a47c22Sthorpej { 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
183c9a47c22Sthorpej { 0x2A, 0x00, "Parameters Changed" },
184c9a47c22Sthorpej { 0x2A, 0x01, "Mode Parameters Changed" },
185c9a47c22Sthorpej { 0x2A, 0x02, "Log Parameters Changed" },
186c9a47c22Sthorpej { 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
187c9a47c22Sthorpej { 0x2C, 0x00, "Command Sequence Error" },
188c9a47c22Sthorpej { 0x2C, 0x01, "Too Many Windows Specified" },
189c9a47c22Sthorpej { 0x2C, 0x02, "Invalid Combination of Windows Specified" },
190c9a47c22Sthorpej { 0x2D, 0x00, "Overwrite Error On Update In Place" },
191c9a47c22Sthorpej { 0x2F, 0x00, "Commands Cleared By Another Initiator" },
192c9a47c22Sthorpej { 0x30, 0x00, "Incompatible Medium Installed" },
193c9a47c22Sthorpej { 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
194c9a47c22Sthorpej { 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
195c9a47c22Sthorpej { 0x30, 0x03, "Cleaning Cartridge Installed" },
196c9a47c22Sthorpej { 0x31, 0x00, "Medium Format Corrupted" },
197c9a47c22Sthorpej { 0x31, 0x01, "Format Command Failed" },
198c9a47c22Sthorpej { 0x32, 0x00, "No Defect Spare Location Available" },
199c9a47c22Sthorpej { 0x32, 0x01, "Defect List Update Failure" },
200c9a47c22Sthorpej { 0x33, 0x00, "Tape Length Error" },
201c9a47c22Sthorpej { 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
202c9a47c22Sthorpej { 0x37, 0x00, "Rounded Parameter" },
203c9a47c22Sthorpej { 0x39, 0x00, "Saving Parameters Not Supported" },
204c9a47c22Sthorpej { 0x3A, 0x00, "Medium Not Present" },
205c9a47c22Sthorpej { 0x3B, 0x00, "Positioning Error" },
206c9a47c22Sthorpej { 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
207c9a47c22Sthorpej { 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
208c9a47c22Sthorpej { 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
209c9a47c22Sthorpej { 0x3B, 0x04, "Slew Failure" },
210c9a47c22Sthorpej { 0x3B, 0x05, "Paper Jam" },
211c9a47c22Sthorpej { 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
212c9a47c22Sthorpej { 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
213c9a47c22Sthorpej { 0x3B, 0x08, "Reposition Error" },
214c9a47c22Sthorpej { 0x3B, 0x09, "Read Past End Of Medium" },
215492c086fSandvar { 0x3B, 0x0A, "Read Past Beginning Of Medium" },
216c9a47c22Sthorpej { 0x3B, 0x0B, "Position Past End Of Medium" },
217c9a47c22Sthorpej { 0x3B, 0x0C, "Position Past Beginning Of Medium" },
218c9a47c22Sthorpej { 0x3B, 0x0D, "Medium Destination Element Full" },
219c9a47c22Sthorpej { 0x3B, 0x0E, "Medium Source Element Empty" },
220d3c740d7Sjwise { 0x3D, 0x00, "Invalid Bits In IDENTIFY Message" },
221c9a47c22Sthorpej { 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
222c9a47c22Sthorpej { 0x3F, 0x00, "Target Operating Conditions Have Changed" },
223c9a47c22Sthorpej { 0x3F, 0x01, "Microcode Has Changed" },
224c9a47c22Sthorpej { 0x3F, 0x02, "Changed Operating Definition" },
225c9a47c22Sthorpej { 0x3F, 0x03, "INQUIRY Data Has Changed" },
226c9a47c22Sthorpej { 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
227c9a47c22Sthorpej { 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
228c9a47c22Sthorpej { 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
229c9a47c22Sthorpej { 0x43, 0x00, "Message Error" },
230c9a47c22Sthorpej { 0x44, 0x00, "Internal Target Failure" },
231c9a47c22Sthorpej { 0x45, 0x00, "Select Or Reselect Failure" },
232c9a47c22Sthorpej { 0x46, 0x00, "Unsuccessful Soft Reset" },
233c9a47c22Sthorpej { 0x47, 0x00, "SCSI Parity Error" },
234c9a47c22Sthorpej { 0x48, 0x00, "INITIATOR DETECTED ERROR Message Received" },
235c9a47c22Sthorpej { 0x49, 0x00, "Invalid Message Error" },
236c9a47c22Sthorpej { 0x4A, 0x00, "Command Phase Error" },
237c9a47c22Sthorpej { 0x4B, 0x00, "Data Phase Error" },
238c9a47c22Sthorpej { 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
239c9a47c22Sthorpej { 0x4E, 0x00, "Overlapped Commands Attempted" },
240c9a47c22Sthorpej { 0x50, 0x00, "Write Append Error" },
241c9a47c22Sthorpej { 0x50, 0x01, "Write Append Position Error" },
242c9a47c22Sthorpej { 0x50, 0x01, "Write Append Position Error" },
243c9a47c22Sthorpej { 0x50, 0x02, "Position Error Related To Timing" },
244c9a47c22Sthorpej { 0x51, 0x00, "Erase Failure" },
245c9a47c22Sthorpej { 0x52, 0x00, "Cartridge Fault" },
246c9a47c22Sthorpej { 0x53, 0x00, "Media Load or Eject Failed" },
247c9a47c22Sthorpej { 0x53, 0x01, "Unload Tape Failure" },
248c9a47c22Sthorpej { 0x53, 0x02, "Medium Removal Prevented" },
249c9a47c22Sthorpej { 0x54, 0x00, "SCSI To Host System Interface Failure" },
250c9a47c22Sthorpej { 0x55, 0x00, "System Resource Failure" },
251c9a47c22Sthorpej { 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
252c9a47c22Sthorpej { 0x58, 0x00, "Generation Does Not Exist" },
253c9a47c22Sthorpej { 0x59, 0x00, "Updated Block Read" },
254c9a47c22Sthorpej { 0x5A, 0x00, "Operator Request or State Change Input (Unspecified)" },
255c9a47c22Sthorpej { 0x5A, 0x01, "Operator Medium Removal Requested" },
256c9a47c22Sthorpej { 0x5A, 0x02, "Operator Selected Write Protect" },
257c9a47c22Sthorpej { 0x5A, 0x03, "Operator Selected Write Permit" },
258c9a47c22Sthorpej { 0x5B, 0x00, "Log Exception" },
259c9a47c22Sthorpej { 0x5B, 0x01, "Threshold Condition Met" },
260c9a47c22Sthorpej { 0x5B, 0x02, "Log Counter At Maximum" },
261c9a47c22Sthorpej { 0x5B, 0x03, "Log List Codes Exhausted" },
262c9a47c22Sthorpej { 0x5C, 0x00, "RPL Status Change" },
263c9a47c22Sthorpej { 0x5C, 0x01, "Spindles Synchronized" },
264c9a47c22Sthorpej { 0x5C, 0x02, "Spindles Not Synchronized" },
265c9a47c22Sthorpej { 0x60, 0x00, "Lamp Failure" },
266c9a47c22Sthorpej { 0x61, 0x00, "Video Acquisition Error" },
267c9a47c22Sthorpej { 0x61, 0x01, "Unable To Acquire Video" },
268c9a47c22Sthorpej { 0x61, 0x02, "Out Of Focus" },
269c9a47c22Sthorpej { 0x62, 0x00, "Scan Head Positioning Error" },
270c9a47c22Sthorpej { 0x63, 0x00, "End Of User Area Encountered On This Track" },
271c9a47c22Sthorpej { 0x64, 0x00, "Illegal Mode For This Track" },
272c9a47c22Sthorpej { 0x00, 0x00, NULL }
273c9a47c22Sthorpej };
274c9a47c22Sthorpej
2759bab889dSxtraeme static void asc2ascii(unsigned char, unsigned char, char *, size_t);
276c9a47c22Sthorpej
277c9a47c22Sthorpej static void
asc2ascii(unsigned char asc,unsigned char ascq,char * result,size_t reslen)2789bab889dSxtraeme asc2ascii(unsigned char asc, unsigned char ascq, char *result, size_t reslen)
279c9a47c22Sthorpej {
280c9a47c22Sthorpej int i = 0;
281c9a47c22Sthorpej
282c9a47c22Sthorpej while (adesc[i].description != NULL) {
283c9a47c22Sthorpej if (adesc[i].asc == asc && adesc[i].ascq == ascq)
284c9a47c22Sthorpej break;
285c9a47c22Sthorpej i++;
286c9a47c22Sthorpej }
287c9a47c22Sthorpej if (adesc[i].description == NULL) {
288c9a47c22Sthorpej if (asc == 0x40 && ascq != 0)
289c9a47c22Sthorpej (void) snprintf(result, reslen,
290c9a47c22Sthorpej "Diagnostic Failure on Component 0x%02x",
291c9a47c22Sthorpej ascq & 0xff);
292c9a47c22Sthorpej else
293c9a47c22Sthorpej (void) snprintf(result, reslen,
294c9a47c22Sthorpej "ASC 0x%02x ASCQ 0x%02x",
295c9a47c22Sthorpej asc & 0xff, ascq & 0xff);
296fe09a0efSitojun } else
297fe09a0efSitojun (void) strlcpy(result, adesc[i].description, reslen);
298c9a47c22Sthorpej }
299c9a47c22Sthorpej
300c9a47c22Sthorpej void
scsi_print_sense_data(const unsigned char * s,int slen,int verbosity)3019bab889dSxtraeme scsi_print_sense_data(const unsigned char *s, int slen, int verbosity)
302c9a47c22Sthorpej {
303c9a47c22Sthorpej int32_t info;
304c9a47c22Sthorpej int i, j, k;
305c9a47c22Sthorpej char sbs[132], *cp;
306c9a47c22Sthorpej
307c9a47c22Sthorpej /*
308c9a47c22Sthorpej * Basics - print out SENSE KEY
309c9a47c22Sthorpej */
310c9a47c22Sthorpej printf(" SENSE KEY: %s", scsi_decode_sense(s, 0, sbs, sizeof(sbs)));
311c9a47c22Sthorpej
312c9a47c22Sthorpej /*
313c9a47c22Sthorpej * Print out, unqualified but aligned, FMK, EOM and ILI status.
314c9a47c22Sthorpej */
315c9a47c22Sthorpej if (s[2] & 0xe0) {
316c9a47c22Sthorpej char pad;
317c9a47c22Sthorpej printf("\n ");
318c9a47c22Sthorpej pad = ' ';
319c9a47c22Sthorpej if (s[2] & SSD_FILEMARK) {
320c9a47c22Sthorpej printf("%c Filemark Detected", pad);
321c9a47c22Sthorpej pad = ',';
322c9a47c22Sthorpej }
323c9a47c22Sthorpej if (s[2] & SSD_EOM) {
324c9a47c22Sthorpej printf("%c EOM Detected", pad);
325c9a47c22Sthorpej pad = ',';
326c9a47c22Sthorpej }
327c9a47c22Sthorpej if (s[2] & SSD_ILI)
328c9a47c22Sthorpej printf("%c Incorrect Length Indicator Set", pad);
329c9a47c22Sthorpej }
330c9a47c22Sthorpej
331c9a47c22Sthorpej /*
332c9a47c22Sthorpej * Now we should figure out, based upon device type, how
333c9a47c22Sthorpej * to format the information field. Unfortunately, that's
334c9a47c22Sthorpej * not convenient here, so we'll print it as a signed
335c9a47c22Sthorpej * 32 bit integer.
336c9a47c22Sthorpej */
337c9a47c22Sthorpej info = _4btol(&s[3]);
338c9a47c22Sthorpej if (info)
339c9a47c22Sthorpej printf("\n INFO FIELD: %d", info);
340c9a47c22Sthorpej
341c9a47c22Sthorpej /*
342c9a47c22Sthorpej * Now we check additional length to see whether there is
343c9a47c22Sthorpej * more information to extract.
344c9a47c22Sthorpej */
345c9a47c22Sthorpej
346c9a47c22Sthorpej /* enough for command specific information? */
347c9a47c22Sthorpej if (((unsigned int) s[7]) < 4) {
348c9a47c22Sthorpej printf("\n");
349c9a47c22Sthorpej return;
350c9a47c22Sthorpej }
351c9a47c22Sthorpej info = _4btol(&s[8]);
352c9a47c22Sthorpej if (info)
353c9a47c22Sthorpej printf("\n COMMAND INFO: %d (0x%x)", info, info);
354c9a47c22Sthorpej
355c9a47c22Sthorpej /*
356c9a47c22Sthorpej * Decode ASC && ASCQ info, plus FRU, plus the rest...
357c9a47c22Sthorpej */
358c9a47c22Sthorpej
359c9a47c22Sthorpej cp = scsi_decode_sense(s, 1, sbs, sizeof(sbs));
360c9a47c22Sthorpej if (cp)
361c9a47c22Sthorpej printf("\n ASC/ASCQ: %s", cp);
362c9a47c22Sthorpej if (s[14] != 0)
363c9a47c22Sthorpej printf("\n FRU CODE: 0x%x", s[14] & 0xff);
364c9a47c22Sthorpej cp = scsi_decode_sense(s, 3, sbs, sizeof(sbs));
365c9a47c22Sthorpej if (cp)
366c9a47c22Sthorpej printf("\n SKSV: %s", cp);
367c9a47c22Sthorpej printf("\n");
368c9a47c22Sthorpej if (verbosity == 0) {
369c9a47c22Sthorpej printf("\n");
370c9a47c22Sthorpej return;
371c9a47c22Sthorpej }
372c9a47c22Sthorpej
373c9a47c22Sthorpej /*
374*2e8b4c7fSandvar * Now figure whether we should print any additional information.
375c9a47c22Sthorpej *
376c9a47c22Sthorpej * Where should we start from? If we had SKSV data,
377c9a47c22Sthorpej * start from offset 18, else from offset 15.
378c9a47c22Sthorpej *
379c9a47c22Sthorpej * From that point until the end of the buffer, check for any
380c9a47c22Sthorpej * nonzero data. If we have some, go back and print the lot,
381c9a47c22Sthorpej * otherwise we're done.
382c9a47c22Sthorpej */
383d993dd3bSmrg if (cp)
384c9a47c22Sthorpej i = 18;
385c9a47c22Sthorpej else
386c9a47c22Sthorpej i = 15;
387c9a47c22Sthorpej for (j = i; j < slen; j++)
388c9a47c22Sthorpej if (s[j])
389c9a47c22Sthorpej break;
390c9a47c22Sthorpej if (j == slen)
391c9a47c22Sthorpej return;
392c9a47c22Sthorpej
393c9a47c22Sthorpej printf("\n Additional Sense Information (byte %d out...):\n", i);
394c9a47c22Sthorpej if (i == 15) {
395c9a47c22Sthorpej printf("\n\t%2d:", i);
396c9a47c22Sthorpej k = 7;
397c9a47c22Sthorpej } else {
398c9a47c22Sthorpej printf("\n\t%2d:", i);
399c9a47c22Sthorpej k = 2;
400c9a47c22Sthorpej j -= 2;
401c9a47c22Sthorpej }
402c9a47c22Sthorpej while (j > 0) {
403c9a47c22Sthorpej if (i >= slen)
404c9a47c22Sthorpej break;
405c9a47c22Sthorpej if (k == 8) {
406c9a47c22Sthorpej k = 0;
407c9a47c22Sthorpej printf("\n\t%2d:", i);
408c9a47c22Sthorpej }
409c9a47c22Sthorpej printf(" 0x%02x", s[i] & 0xff);
410c9a47c22Sthorpej k++;
411c9a47c22Sthorpej j--;
412c9a47c22Sthorpej i++;
413c9a47c22Sthorpej }
414c9a47c22Sthorpej printf("\n\n");
415c9a47c22Sthorpej }
416c9a47c22Sthorpej
417c9a47c22Sthorpej char *
scsi_decode_sense(const unsigned char * snsbuf,int flag,char * rqsbuf,size_t rqsbuflen)4189bab889dSxtraeme scsi_decode_sense(const unsigned char *snsbuf, int flag, char *rqsbuf,
4199bab889dSxtraeme size_t rqsbuflen)
420c9a47c22Sthorpej {
421c9a47c22Sthorpej unsigned char skey;
422c9a47c22Sthorpej char localbuf[64];
423c9a47c22Sthorpej
424c9a47c22Sthorpej skey = 0;
425c9a47c22Sthorpej
426c9a47c22Sthorpej if (flag == 0 || flag == 2 || flag == 3)
427c9a47c22Sthorpej skey = snsbuf[2] & 0xf;
428c9a47c22Sthorpej if (flag == 0) { /* Sense Key Only */
429fe09a0efSitojun (void) strlcpy(rqsbuf, sense_keys[skey], rqsbuflen);
430c9a47c22Sthorpej return (rqsbuf);
431c9a47c22Sthorpej } else if (flag == 1) { /* ASC/ASCQ Only */
432c9a47c22Sthorpej asc2ascii(snsbuf[12], snsbuf[13], rqsbuf, rqsbuflen);
433c9a47c22Sthorpej return (rqsbuf);
434c9a47c22Sthorpej } else if (flag == 2) { /* Sense Key && ASC/ASCQ */
435c9a47c22Sthorpej asc2ascii(snsbuf[12], snsbuf[13], localbuf, sizeof(localbuf));
436c9a47c22Sthorpej (void) snprintf(rqsbuf, rqsbuflen, "%s, %s", sense_keys[skey],
437c9a47c22Sthorpej localbuf);
438c9a47c22Sthorpej return (rqsbuf);
439c9a47c22Sthorpej } else if (flag == 3 && snsbuf[7] >= 9 && (snsbuf[15] & 0x80)) {
440c9a47c22Sthorpej /*
441c9a47c22Sthorpej * SKSV Data
442c9a47c22Sthorpej */
443c9a47c22Sthorpej switch (skey) {
444c9a47c22Sthorpej case SKEY_ILLEGAL_REQUEST:
445c9a47c22Sthorpej if (snsbuf[15] & 0x8)
446c9a47c22Sthorpej (void) snprintf(rqsbuf, rqsbuflen,
447c9a47c22Sthorpej "Error in %s, Offset %d, bit %d",
448c9a47c22Sthorpej (snsbuf[15] & 0x40)? "CDB" : "Parameters",
449c9a47c22Sthorpej (snsbuf[16] & 0xff) << 8 |
450c9a47c22Sthorpej (snsbuf[17] & 0xff), snsbuf[15] & 0x7);
451c9a47c22Sthorpej else
452c9a47c22Sthorpej (void) snprintf(rqsbuf, rqsbuflen,
453c9a47c22Sthorpej "Error in %s, Offset %d",
454c9a47c22Sthorpej (snsbuf[15] & 0x40)? "CDB" : "Parameters",
455c9a47c22Sthorpej (snsbuf[16] & 0xff) << 8 |
456c9a47c22Sthorpej (snsbuf[17] & 0xff));
457c9a47c22Sthorpej return (rqsbuf);
458c9a47c22Sthorpej
459c9a47c22Sthorpej case SKEY_RECOVERED_ERROR:
460c9a47c22Sthorpej case SKEY_MEDIUM_ERROR:
461c9a47c22Sthorpej case SKEY_HARDWARE_ERROR:
462c9a47c22Sthorpej (void) snprintf(rqsbuf, rqsbuflen,
463c9a47c22Sthorpej "Actual Retry Count: %d",
464c9a47c22Sthorpej (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
465c9a47c22Sthorpej return (rqsbuf);
466c9a47c22Sthorpej
467c9a47c22Sthorpej case SKEY_NOT_READY:
468c9a47c22Sthorpej (void) snprintf(rqsbuf, rqsbuflen,
469c9a47c22Sthorpej "Progress Indicator: %d",
470c9a47c22Sthorpej (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
471c9a47c22Sthorpej return (rqsbuf);
472c9a47c22Sthorpej
473c9a47c22Sthorpej default:
474c9a47c22Sthorpej break;
475c9a47c22Sthorpej }
476c9a47c22Sthorpej }
477c9a47c22Sthorpej return (NULL);
478c9a47c22Sthorpej }
479c9a47c22Sthorpej
480c9a47c22Sthorpej void
scsi_print_sense(const char * name,const scsireq_t * req,int verbosity)4819bab889dSxtraeme scsi_print_sense(const char *name, const scsireq_t *req, int verbosity)
482c9a47c22Sthorpej {
483c9a47c22Sthorpej int i;
484c9a47c22Sthorpej
485c9a47c22Sthorpej printf("%s: Check Condition on CDB:", name);
486c9a47c22Sthorpej for (i = 0; i < req->cmdlen; i++)
487c9a47c22Sthorpej printf(" %02x", req->cmd[i]);
488c9a47c22Sthorpej printf("\n");
489c9a47c22Sthorpej scsi_print_sense_data(req->sense, req->senselen_used, verbosity);
490c9a47c22Sthorpej }
491