1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/scsi/generic/sense.h>
29 #include <sys/scsi/generic/status.h>
30 
31 #include <stddef.h>
32 #include <stdio.h>
33 
34 #include <scsi/libscsi.h>
35 #include "libscsi_impl.h"
36 
37 typedef struct slist {
38 	char	*str;
39 	int	value;
40 } slist_t;
41 
42 static slist_t sensekey_strings[] = {
43 	{ "No sense error", 	KEY_NO_SENSE		},
44 	{ "Recoverable error",	KEY_RECOVERABLE_ERROR	},
45 	{ "Not ready error",	KEY_NOT_READY		},
46 	{ "Medium error",	KEY_MEDIUM_ERROR	},
47 	{ "Hardware error",	KEY_HARDWARE_ERROR	},
48 	{ "Illegal request",	KEY_ILLEGAL_REQUEST	},
49 	{ "Unit attention error", KEY_UNIT_ATTENTION	},
50 	{ "Write protect error", KEY_WRITE_PROTECT	},
51 	{ "Blank check error",	KEY_BLANK_CHECK		},
52 	{ "Vendor unique error", KEY_VENDOR_UNIQUE	},
53 	{ "Copy aborted error",	KEY_COPY_ABORTED	},
54 	{ "Aborted command",	KEY_ABORTED_COMMAND	},
55 	{ "Equal error",	KEY_EQUAL		},
56 	{ "Volume overflow",	KEY_VOLUME_OVERFLOW	},
57 	{ "Miscompare error",	KEY_MISCOMPARE		},
58 	{ "Reserved error",	KEY_RESERVED		},
59 	{ NULL,			0			}
60 };
61 
62 static struct asq_key_strings {
63 	uint_t asc;
64 	uint_t ascq;
65 	const char *message;
66 } extended_sense_list[] = {
67 	{ 0x00, 0x00, "no additional sense info" },
68 	{ 0x00, 0x01, "filemark detected" },
69 	{ 0x00, 0x02, "end of partition/medium detected" },
70 	{ 0x00, 0x03, "setmark detected" },
71 	{ 0x00, 0x04, "begining of partition/medium detected" },
72 	{ 0x00, 0x05, "end of data detected" },
73 	{ 0x00, 0x06, "i/o process terminated" },
74 	{ 0x00, 0x11, "audio play operation in progress" },
75 	{ 0x00, 0x12, "audio play operation paused" },
76 	{ 0x00, 0x13, "audio play operation successfully completed" },
77 	{ 0x00, 0x14, "audio play operation stopped due to error" },
78 	{ 0x00, 0x15, "no current audio status to return" },
79 	{ 0x00, 0x16, "operation in progress" },
80 	{ 0x00, 0x17, "cleaning requested" },
81 	{ 0x00, 0x18, "erase operation in progress" },
82 	{ 0x00, 0x19, "locate operation in progress" },
83 	{ 0x00, 0x1A, "rewind operation in progress" },
84 	{ 0x00, 0x1B, "set capacity operation in progress" },
85 	{ 0x00, 0x1C, "verify operation in progress" },
86 	{ 0x01, 0x00, "no index/sector signal" },
87 	{ 0x02, 0x00, "no seek complete" },
88 	{ 0x03, 0x00, "peripheral device write fault" },
89 	{ 0x03, 0x01, "no write current" },
90 	{ 0x03, 0x02, "excessive write errors" },
91 	{ 0x04, 0x00, "LUN not ready" },
92 	{ 0x04, 0x01, "LUN is becoming ready" },
93 	{ 0x04, 0x02, "LUN initializing command required" },
94 	{ 0x04, 0x03, "LUN not ready intervention required" },
95 	{ 0x04, 0x04, "LUN not ready format in progress" },
96 	{ 0x04, 0x05, "LUN not ready, rebuild in progress" },
97 	{ 0x04, 0x06, "LUN not ready, recalculation in progress" },
98 	{ 0x04, 0x07, "LUN not ready, operation in progress" },
99 	{ 0x04, 0x08, "LUN not ready, long write in progress" },
100 	{ 0x04, 0x09, "LUN not ready, self-test in progress" },
101 	{ 0x04, 0x0A, "LUN not accessible, asymmetric access state "
102 		"transition" },
103 	{ 0x04, 0x0B, "LUN not accessible, target port in standby state" },
104 	{ 0x04, 0x0C, "LUN not accessible, target port in unavailable state" },
105 	{ 0x04, 0x10, "LUN not ready, auxiliary memory not accessible" },
106 	{ 0x05, 0x00, "LUN does not respond to selection" },
107 	{ 0x06, 0x00, "reference position found" },
108 	{ 0x07, 0x00, "multiple peripheral devices selected" },
109 	{ 0x08, 0x00, "LUN communication failure" },
110 	{ 0x08, 0x01, "LUN communication time-out" },
111 	{ 0x08, 0x02, "LUN communication parity error" },
112 	{ 0x08, 0x03, "LUN communication crc error (ultra-DMA/32)" },
113 	{ 0x08, 0x04, "unreachable copy target" },
114 	{ 0x09, 0x00, "track following error" },
115 	{ 0x09, 0x01, "tracking servo failure" },
116 	{ 0x09, 0x02, "focus servo failure" },
117 	{ 0x09, 0x03, "spindle servo failure" },
118 	{ 0x09, 0x04, "head select fault" },
119 	{ 0x0a, 0x00, "error log overflow" },
120 	{ 0x0b, 0x00, "warning" },
121 	{ 0x0b, 0x01, "warning - specified temperature exceeded" },
122 	{ 0x0b, 0x02, "warning - enclosure degraded" },
123 	{ 0x0c, 0x00, "write error" },
124 	{ 0x0c, 0x01, "write error - recovered with auto reallocation" },
125 	{ 0x0c, 0x02, "write error - auto reallocation failed" },
126 	{ 0x0c, 0x03, "write error - recommend reassignment" },
127 	{ 0x0c, 0x04, "compression check miscompare error" },
128 	{ 0x0c, 0x05, "data expansion occurred during compression" },
129 	{ 0x0c, 0x06, "block not compressible" },
130 	{ 0x0c, 0x07, "write error - recovery needed" },
131 	{ 0x0c, 0x08, "write error - recovery failed" },
132 	{ 0x0c, 0x09, "write error - loss of streaming" },
133 	{ 0x0c, 0x0a, "write error - padding blocks added" },
134 	{ 0x0c, 0x0b, "auxiliary memory write error" },
135 	{ 0x0c, 0x0c, "write error - unexpected unsolicited data" },
136 	{ 0x0c, 0x0d, "write error - not enough unsolicited data" },
137 	{ 0x0d, 0x00, "error detected by third party temporary initiator" },
138 	{ 0x0d, 0x01, "third party device failure" },
139 	{ 0x0d, 0x02, "copy target device not reachable" },
140 	{ 0x0d, 0x03, "incorrect copy target device type" },
141 	{ 0x0d, 0x04, "copy target device data underrun" },
142 	{ 0x0d, 0x05, "copy target device data overrun" },
143 	{ 0x0e, 0x00, "invalid information unit" },
144 	{ 0x0e, 0x01, "information unit too short" },
145 	{ 0x0e, 0x02, "information unit too long" },
146 	{ 0x10, 0x00, "ID CRC or ECC error" },
147 	{ 0x11, 0x00, "unrecovered read error" },
148 	{ 0x11, 0x01, "read retries exhausted" },
149 	{ 0x11, 0x02, "error too long to correct" },
150 	{ 0x11, 0x03, "multiple read errors" },
151 	{ 0x11, 0x04, "unrecovered read error - auto reallocate failed" },
152 	{ 0x11, 0x05, "L-EC uncorrectable error" },
153 	{ 0x11, 0x06, "CIRC unrecovered error" },
154 	{ 0x11, 0x07, "data re-synchronization error" },
155 	{ 0x11, 0x08, "incomplete block read" },
156 	{ 0x11, 0x09, "no gap found" },
157 	{ 0x11, 0x0a, "miscorrected error" },
158 	{ 0x11, 0x0b, "unrecovered read error - recommend reassignment" },
159 	{ 0x11, 0x0c, "unrecovered read error - recommend rewrite the data" },
160 	{ 0x11, 0x0d, "de-compression crc error" },
161 	{ 0x11, 0x0e, "cannot decompress using declared algorithm" },
162 	{ 0x11, 0x0f, "error reading UPC/EAN number" },
163 	{ 0x11, 0x10, "error reading ISRC number" },
164 	{ 0x11, 0x11, "read error - loss of streaming" },
165 	{ 0x11, 0x12, "auxiliary memory read error" },
166 	{ 0x11, 0x13, "read error - failed retransmission request" },
167 	{ 0x12, 0x00, "address mark not found for ID field" },
168 	{ 0x13, 0x00, "address mark not found for data field" },
169 	{ 0x14, 0x00, "recorded entity not found" },
170 	{ 0x14, 0x01, "record not found" },
171 	{ 0x14, 0x02, "filemark or setmark not found" },
172 	{ 0x14, 0x03, "end-of-data not found" },
173 	{ 0x14, 0x04, "block sequence error" },
174 	{ 0x14, 0x05, "record not found - recommend reassignment" },
175 	{ 0x14, 0x06, "record not found - data auto-reallocated" },
176 	{ 0x14, 0x07, "locate operation failure" },
177 	{ 0x15, 0x00, "random positioning error" },
178 	{ 0x15, 0x01, "mechanical positioning error" },
179 	{ 0x15, 0x02, "positioning error detected by read of medium" },
180 	{ 0x16, 0x00, "data sync mark error" },
181 	{ 0x16, 0x01, "data sync error - data rewritten" },
182 	{ 0x16, 0x02, "data sync error - recommend rewrite" },
183 	{ 0x16, 0x03, "data sync error - data auto-reallocated" },
184 	{ 0x16, 0x04, "data sync error - recommend reassignment" },
185 	{ 0x17, 0x00, "recovered data with no error correction" },
186 	{ 0x17, 0x01, "recovered data with retries" },
187 	{ 0x17, 0x02, "recovered data with positive head offset" },
188 	{ 0x17, 0x03, "recovered data with negative head offset" },
189 	{ 0x17, 0x04, "recovered data with retries and/or CIRC applied" },
190 	{ 0x17, 0x05, "recovered data using previous sector id" },
191 	{ 0x17, 0x06, "recovered data without ECC - data auto-reallocated" },
192 	{ 0x17, 0x07, "recovered data without ECC - recommend reassignment" },
193 	{ 0x17, 0x08, "recovered data without ECC - recommend rewrite" },
194 	{ 0x17, 0x09, "recovered data without ECC - data rewritten" },
195 	{ 0x18, 0x00, "recovered data with error correction" },
196 	{ 0x18, 0x01, "recovered data with error corr. & retries applied" },
197 	{ 0x18, 0x02, "recovered data - data auto-reallocated" },
198 	{ 0x18, 0x03, "recovered data with CIRC" },
199 	{ 0x18, 0x04, "recovered data with L-EC" },
200 	{ 0x18, 0x05, "recovered data - recommend reassignment" },
201 	{ 0x18, 0x06, "recovered data - recommend rewrite" },
202 	{ 0x18, 0x07, "recovered data with ECC - data rewritten" },
203 	{ 0x18, 0x08, "recovered data with linking" },
204 	{ 0x19, 0x00, "defect list error" },
205 	{ 0x1a, 0x00, "parameter list length error" },
206 	{ 0x1b, 0x00, "synchronous data xfer error" },
207 	{ 0x1c, 0x00, "defect list not found" },
208 	{ 0x1c, 0x01, "primary defect list not found" },
209 	{ 0x1c, 0x02, "grown defect list not found" },
210 	{ 0x1d, 0x00, "miscompare during verify" },
211 	{ 0x1e, 0x00, "recovered ID with ECC" },
212 	{ 0x1f, 0x00, "partial defect list transfer" },
213 	{ 0x20, 0x00, "invalid command operation code" },
214 	{ 0x20, 0x01, "access denied - initiator pending-enrolled" },
215 	{ 0x20, 0x02, "access denied - no access rights" },
216 	{ 0x20, 0x03, "access denied - invalid mgmt id key" },
217 	{ 0x20, 0x04, "illegal command while in write capable state" },
218 	{ 0x20, 0x06, "illegal command while in explicit address mode" },
219 	{ 0x20, 0x07, "illegal command while in implicit address mode" },
220 	{ 0x20, 0x08, "access denied - enrollment conflict" },
221 	{ 0x20, 0x09, "access denied - invalid lu identifier" },
222 	{ 0x20, 0x0a, "access denied - invalid proxy token" },
223 	{ 0x20, 0x0b, "access denied - ACL LUN conflict" },
224 	{ 0x21, 0x00, "logical block address out of range" },
225 	{ 0x21, 0x01, "invalid element address" },
226 	{ 0x21, 0x02, "invalid address for write" },
227 	{ 0x22, 0x00, "illegal function" },
228 	{ 0x24, 0x00, "invalid field in cdb" },
229 	{ 0x24, 0x01, "cdb decryption error" },
230 	{ 0x25, 0x00, "LUN not supported" },
231 	{ 0x26, 0x00, "invalid field in param list" },
232 	{ 0x26, 0x01, "parameter not supported" },
233 	{ 0x26, 0x02, "parameter value invalid" },
234 	{ 0x26, 0x03, "threshold parameters not supported" },
235 	{ 0x26, 0x04, "invalid release of persistent reservation" },
236 	{ 0x26, 0x05, "data decryption error" },
237 	{ 0x26, 0x06, "too many target descriptors" },
238 	{ 0x26, 0x07, "unsupported target descriptor type code" },
239 	{ 0x26, 0x08, "too many segment descriptors" },
240 	{ 0x26, 0x09, "unsupported segment descriptor type code" },
241 	{ 0x26, 0x0a, "unexpected inexact segment" },
242 	{ 0x26, 0x0b, "inline data length exceeded" },
243 	{ 0x26, 0x0c, "invalid operation for copy source or destination" },
244 	{ 0x26, 0x0d, "copy segment granularity violation" },
245 	{ 0x27, 0x00, "write protected" },
246 	{ 0x27, 0x01, "hardware write protected" },
247 	{ 0x27, 0x02, "LUN software write protected" },
248 	{ 0x27, 0x03, "associated write protect" },
249 	{ 0x27, 0x04, "persistent write protect" },
250 	{ 0x27, 0x05, "permanent write protect" },
251 	{ 0x27, 0x06, "conditional write protect" },
252 	{ 0x28, 0x00, "medium may have changed" },
253 	{ 0x28, 0x01, "import or export element accessed" },
254 	{ 0x29, 0x00, "power on, reset, or bus reset occurred" },
255 	{ 0x29, 0x01, "power on occurred" },
256 	{ 0x29, 0x02, "scsi bus reset occurred" },
257 	{ 0x29, 0x03, "bus device reset message occurred" },
258 	{ 0x29, 0x04, "device internal reset" },
259 	{ 0x29, 0x05, "transceiver mode changed to single-ended" },
260 	{ 0x29, 0x06, "transceiver mode changed to LVD" },
261 	{ 0x29, 0x07, "i_t nexus loss occurred" },
262 	{ 0x2a, 0x00, "parameters changed" },
263 	{ 0x2a, 0x01, "mode parameters changed" },
264 	{ 0x2a, 0x02, "log parameters changed" },
265 	{ 0x2a, 0x03, "reservations preempted" },
266 	{ 0x2a, 0x04, "reservations released" },
267 	{ 0x2a, 0x05, "registrations preempted" },
268 	{ 0x2a, 0x06, "asymmetric access state changed" },
269 	{ 0x2a, 0x07, "implicit asymmetric access state transition failed" },
270 	{ 0x2b, 0x00, "copy cannot execute since host cannot disconnect" },
271 	{ 0x2c, 0x00, "command sequence error" },
272 	{ 0x2c, 0x03, "current program area is not empty" },
273 	{ 0x2c, 0x04, "current program area is empty" },
274 	{ 0x2c, 0x06, "persistent prevent conflict" },
275 	{ 0x2c, 0x07, "previous busy status" },
276 	{ 0x2c, 0x08, "previous task set full status" },
277 	{ 0x2c, 0x09, "previous reservation conflict status" },
278 	{ 0x2d, 0x00, "overwrite error on update in place" },
279 	{ 0x2e, 0x00, "insufficient time for operation" },
280 	{ 0x2f, 0x00, "commands cleared by another initiator" },
281 	{ 0x30, 0x00, "incompatible medium installed" },
282 	{ 0x30, 0x01, "cannot read medium - unknown format" },
283 	{ 0x30, 0x02, "cannot read medium - incompatible format" },
284 	{ 0x30, 0x03, "cleaning cartridge installed" },
285 	{ 0x30, 0x04, "cannot write medium - unknown format" },
286 	{ 0x30, 0x05, "cannot write medium - incompatible format" },
287 	{ 0x30, 0x06, "cannot format medium - incompatible medium" },
288 	{ 0x30, 0x07, "cleaning failure" },
289 	{ 0x30, 0x08, "cannot write - application code mismatch" },
290 	{ 0x30, 0x09, "current session not fixated for append" },
291 	{ 0x30, 0x10, "medium not formatted" },
292 	{ 0x31, 0x00, "medium format corrupted" },
293 	{ 0x31, 0x01, "format command failed" },
294 	{ 0x31, 0x02, "zoned formatting failed due to spare linking" },
295 	{ 0x32, 0x00, "no defect spare location available" },
296 	{ 0x32, 0x01, "defect list update failure" },
297 	{ 0x33, 0x00, "tape length error" },
298 	{ 0x34, 0x00, "enclosure failure" },
299 	{ 0x35, 0x00, "enclosure services failure" },
300 	{ 0x35, 0x01, "unsupported enclosure function" },
301 	{ 0x35, 0x02, "enclosure services unavailable" },
302 	{ 0x35, 0x03, "enclosure services transfer failure" },
303 	{ 0x35, 0x04, "enclosure services transfer refused" },
304 	{ 0x36, 0x00, "ribbon, ink, or toner failure" },
305 	{ 0x37, 0x00, "rounded parameter" },
306 	{ 0x39, 0x00, "saving parameters not supported" },
307 	{ 0x3a, 0x00, "medium not present" },
308 	{ 0x3a, 0x01, "medium not present - tray closed" },
309 	{ 0x3a, 0x02, "medium not present - tray open" },
310 	{ 0x3a, 0x03, "medium not present - loadable" },
311 	{ 0x3a, 0x04, "medium not present - medium auxiliary memory "
312 		"accessible" },
313 	{ 0x3b, 0x00, "sequential positioning error" },
314 	{ 0x3b, 0x01, "tape position error at beginning-of-medium" },
315 	{ 0x3b, 0x02, "tape position error at end-of-medium" },
316 	{ 0x3b, 0x08, "reposition error" },
317 	{ 0x3b, 0x0c, "position past beginning of medium" },
318 	{ 0x3b, 0x0d, "medium destination element full" },
319 	{ 0x3b, 0x0e, "medium source element empty" },
320 	{ 0x3b, 0x0f, "end of medium reached" },
321 	{ 0x3b, 0x11, "medium magazine not accessible" },
322 	{ 0x3b, 0x12, "medium magazine removed" },
323 	{ 0x3b, 0x13, "medium magazine inserted" },
324 	{ 0x3b, 0x14, "medium magazine locked" },
325 	{ 0x3b, 0x15, "medium magazine unlocked" },
326 	{ 0x3b, 0x16, "mechanical positioning or changer error" },
327 	{ 0x3d, 0x00, "invalid bits in indentify message" },
328 	{ 0x3e, 0x00, "LUN has not self-configured yet" },
329 	{ 0x3e, 0x01, "LUN failure" },
330 	{ 0x3e, 0x02, "timeout on LUN" },
331 	{ 0x3e, 0x03, "LUN failed self-test" },
332 	{ 0x3e, 0x04, "LUN unable to update self-test log" },
333 	{ 0x3f, 0x00, "target operating conditions have changed" },
334 	{ 0x3f, 0x01, "microcode has been changed" },
335 	{ 0x3f, 0x02, "changed operating definition" },
336 	{ 0x3f, 0x03, "inquiry data has changed" },
337 	{ 0x3f, 0x04, "component device attached" },
338 	{ 0x3f, 0x05, "device identifier changed" },
339 	{ 0x3f, 0x06, "redundancy group created or modified" },
340 	{ 0x3f, 0x07, "redundancy group deleted" },
341 	{ 0x3f, 0x08, "spare created or modified" },
342 	{ 0x3f, 0x09, "spare deleted" },
343 	{ 0x3f, 0x0a, "volume set created or modified" },
344 	{ 0x3f, 0x0b, "volume set deleted" },
345 	{ 0x3f, 0x0c, "volume set deassigned" },
346 	{ 0x3f, 0x0d, "volume set reassigned" },
347 	{ 0x3f, 0x0e, "reported LUNs data has changed" },
348 	{ 0x3f, 0x0f, "echo buffer overwritten" },
349 	{ 0x3f, 0x10, "medium loadable" },
350 	{ 0x3f, 0x11, "medium auxiliary memory accessible" },
351 	{ 0x40, 0x00, "ram failure" },
352 	{ 0x41, 0x00, "data path failure" },
353 	{ 0x42, 0x00, "power-on or self-test failure" },
354 	{ 0x43, 0x00, "message error" },
355 	{ 0x44, 0x00, "internal target failure" },
356 	{ 0x45, 0x00, "select or reselect failure" },
357 	{ 0x46, 0x00, "unsuccessful soft reset" },
358 	{ 0x47, 0x00, "scsi parity error" },
359 	{ 0x47, 0x01, "data phase crc error detected" },
360 	{ 0x47, 0x02, "scsi parity error detected during st data phase" },
361 	{ 0x47, 0x03, "information unit iucrc error detected" },
362 	{ 0x47, 0x04, "asynchronous information protection error detected" },
363 	{ 0x47, 0x05, "protocol service crc error" },
364 	{ 0x47, 0x7f, "some commands cleared by iscsi protocol event" },
365 	{ 0x48, 0x00, "initiator detected error message received" },
366 	{ 0x49, 0x00, "invalid message error" },
367 	{ 0x4a, 0x00, "command phase error" },
368 	{ 0x4b, 0x00, "data phase error" },
369 	{ 0x4b, 0x01, "invalid target port transfer tag received" },
370 	{ 0x4b, 0x02, "too much write data" },
371 	{ 0x4b, 0x03, "ack/nak timeout" },
372 	{ 0x4b, 0x04, "nak received" },
373 	{ 0x4b, 0x05, "data offset error" },
374 	{ 0x4c, 0x00, "logical unit failed self-configuration" },
375 	{ 0x4d, 0x00, "tagged overlapped commands (ASCQ = queue tag)" },
376 	{ 0x4e, 0x00, "overlapped commands attempted" },
377 	{ 0x50, 0x00, "write append error" },
378 	{ 0x51, 0x00, "erase failure" },
379 	{ 0x52, 0x00, "cartridge fault" },
380 	{ 0x53, 0x00, "media load or eject failed" },
381 	{ 0x53, 0x01, "unload tape failure" },
382 	{ 0x53, 0x02, "medium removal prevented" },
383 	{ 0x54, 0x00, "scsi to host system interface failure" },
384 	{ 0x55, 0x00, "system resource failure" },
385 	{ 0x55, 0x01, "system buffer full" },
386 	{ 0x55, 0x02, "insufficient reservation resources" },
387 	{ 0x55, 0x03, "insufficient resources" },
388 	{ 0x55, 0x04, "insufficient registration resources" },
389 	{ 0x55, 0x05, "insufficient access control resources" },
390 	{ 0x55, 0x06, "auxiliary memory out of space" },
391 	{ 0x57, 0x00, "unable to recover TOC" },
392 	{ 0x58, 0x00, "generation does not exist" },
393 	{ 0x59, 0x00, "updated block read" },
394 	{ 0x5a, 0x00, "operator request or state change input" },
395 	{ 0x5a, 0x01, "operator medium removal request" },
396 	{ 0x5a, 0x02, "operator selected write protect" },
397 	{ 0x5a, 0x03, "operator selected write permit" },
398 	{ 0x5b, 0x00, "log exception" },
399 	{ 0x5b, 0x01, "threshold condition met" },
400 	{ 0x5b, 0x02, "log counter at maximum" },
401 	{ 0x5b, 0x03, "log list codes exhausted" },
402 	{ 0x5c, 0x00, "RPL status change" },
403 	{ 0x5c, 0x01, "spindles synchronized" },
404 	{ 0x5c, 0x02, "spindles not synchronized" },
405 	{ 0x5d, 0x00, "drive operation marginal, service immediately"
406 		    " (failure prediction threshold exceeded)" },
407 	{ 0x5d, 0x01, "media failure prediction threshold exceeded" },
408 	{ 0x5d, 0x02, "LUN failure prediction threshold exceeded" },
409 	{ 0x5d, 0x03, "spare area exhaustion prediction threshold exceeded" },
410 	{ 0x5d, 0x10, "hardware impending failure general hard drive failure" },
411 	{ 0x5d, 0x11, "hardware impending failure drive error rate too high" },
412 	{ 0x5d, 0x12, "hardware impending failure data error rate too high" },
413 	{ 0x5d, 0x13, "hardware impending failure seek error rate too high" },
414 	{ 0x5d, 0x14, "hardware impending failure too many block reassigns" },
415 	{ 0x5d, 0x15, "hardware impending failure access times too high" },
416 	{ 0x5d, 0x16, "hardware impending failure start unit times too high" },
417 	{ 0x5d, 0x17, "hardware impending failure channel parametrics" },
418 	{ 0x5d, 0x18, "hardware impending failure controller detected" },
419 	{ 0x5d, 0x19, "hardware impending failure throughput performance" },
420 	{ 0x5d, 0x1a, "hardware impending failure seek time performance" },
421 	{ 0x5d, 0x1b, "hardware impending failure spin-up retry count" },
422 	{ 0x5d, 0x1c, "hardware impending failure drive calibration retry "
423 		"count" },
424 	{ 0x5d, 0x20, "controller impending failure general hard drive "
425 		"failure" },
426 	{ 0x5d, 0x21, "controller impending failure drive error rate too "
427 		"high" },
428 	{ 0x5d, 0x22, "controller impending failure data error rate too high" },
429 	{ 0x5d, 0x23, "controller impending failure seek error rate too high" },
430 	{ 0x5d, 0x24, "controller impending failure too many block reassigns" },
431 	{ 0x5d, 0x25, "controller impending failure access times too high" },
432 	{ 0x5d, 0x26, "controller impending failure start unit times too "
433 		"high" },
434 	{ 0x5d, 0x27, "controller impending failure channel parametrics" },
435 	{ 0x5d, 0x28, "controller impending failure controller detected" },
436 	{ 0x5d, 0x29, "controller impending failure throughput performance" },
437 	{ 0x5d, 0x2a, "controller impending failure seek time performance" },
438 	{ 0x5d, 0x2b, "controller impending failure spin-up retry count" },
439 	{ 0x5d, 0x2c, "controller impending failure drive calibration retry "
440 		"cnt" },
441 	{ 0x5d, 0x30, "data channel impending failure general hard drive "
442 		"failure" },
443 	{ 0x5d, 0x31, "data channel impending failure drive error rate too "
444 		"high" },
445 	{ 0x5d, 0x32, "data channel impending failure data error rate too "
446 		"high" },
447 	{ 0x5d, 0x33, "data channel impending failure seek error rate too "
448 		"high" },
449 	{ 0x5d, 0x34, "data channel impending failure too many block "
450 		"reassigns" },
451 	{ 0x5d, 0x35, "data channel impending failure access times too high" },
452 	{ 0x5d, 0x36, "data channel impending failure start unit times too "
453 		"high" },
454 	{ 0x5d, 0x37, "data channel impending failure channel parametrics" },
455 	{ 0x5d, 0x38, "data channel impending failure controller detected" },
456 	{ 0x5d, 0x39, "data channel impending failure throughput performance" },
457 	{ 0x5d, 0x3a, "data channel impending failure seek time performance" },
458 	{ 0x5d, 0x3b, "data channel impending failure spin-up retry count" },
459 	{ 0x5d, 0x3c, "data channel impending failure drive calibrate retry "
460 		"cnt" },
461 	{ 0x5d, 0x40, "servo impending failure general hard drive failure" },
462 	{ 0x5d, 0x41, "servo impending failure drive error rate too high" },
463 	{ 0x5d, 0x42, "servo impending failure data error rate too high" },
464 	{ 0x5d, 0x43, "servo impending failure seek error rate too high" },
465 	{ 0x5d, 0x44, "servo impending failure too many block reassigns" },
466 	{ 0x5d, 0x45, "servo impending failure access times too high" },
467 	{ 0x5d, 0x46, "servo impending failure start unit times too high" },
468 	{ 0x5d, 0x47, "servo impending failure channel parametrics" },
469 	{ 0x5d, 0x48, "servo impending failure controller detected" },
470 	{ 0x5d, 0x49, "servo impending failure throughput performance" },
471 	{ 0x5d, 0x4a, "servo impending failure seek time performance" },
472 	{ 0x5d, 0x4b, "servo impending failure spin-up retry count" },
473 	{ 0x5d, 0x4c, "servo impending failure drive calibration retry count" },
474 	{ 0x5d, 0x50, "spindle impending failure general hard drive failure" },
475 	{ 0x5d, 0x51, "spindle impending failure drive error rate too high" },
476 	{ 0x5d, 0x52, "spindle impending failure data error rate too high" },
477 	{ 0x5d, 0x53, "spindle impending failure seek error rate too high" },
478 	{ 0x5d, 0x54, "spindle impending failure too many block reassigns" },
479 	{ 0x5d, 0x55, "spindle impending failure access times too high" },
480 	{ 0x5d, 0x56, "spindle impending failure start unit times too high" },
481 	{ 0x5d, 0x57, "spindle impending failure channel parametrics" },
482 	{ 0x5d, 0x58, "spindle impending failure controller detected" },
483 	{ 0x5d, 0x59, "spindle impending failure throughput performance" },
484 	{ 0x5d, 0x5a, "spindle impending failure seek time performance" },
485 	{ 0x5d, 0x5b, "spindle impending failure spin-up retry count" },
486 	{ 0x5d, 0x5c, "spindle impending failure drive calibration retry "
487 		"count" },
488 	{ 0x5d, 0x60, "firmware impending failure general hard drive failure" },
489 	{ 0x5d, 0x61, "firmware impending failure drive error rate too high" },
490 	{ 0x5d, 0x62, "firmware impending failure data error rate too high" },
491 	{ 0x5d, 0x63, "firmware impending failure seek error rate too high" },
492 	{ 0x5d, 0x64, "firmware impending failure too many block reassigns" },
493 	{ 0x5d, 0x65, "firmware impending failure access times too high" },
494 	{ 0x5d, 0x66, "firmware impending failure start unit times too high" },
495 	{ 0x5d, 0x67, "firmware impending failure channel parametrics" },
496 	{ 0x5d, 0x68, "firmware impending failure controller detected" },
497 	{ 0x5d, 0x69, "firmware impending failure throughput performance" },
498 	{ 0x5d, 0x6a, "firmware impending failure seek time performance" },
499 	{ 0x5d, 0x6b, "firmware impending failure spin-up retry count" },
500 	{ 0x5d, 0x6c, "firmware impending failure drive calibration retry "
501 		"count" },
502 	{ 0x5d, 0xff, "failure prediction threshold exceeded (false)" },
503 	{ 0x5e, 0x00, "low power condition active" },
504 	{ 0x5e, 0x01, "idle condition activated by timer" },
505 	{ 0x5e, 0x02, "standby condition activated by timer" },
506 	{ 0x5e, 0x03, "idle condition activated by command" },
507 	{ 0x5e, 0x04, "standby condition activated by command" },
508 	{ 0x60, 0x00, "lamp failure" },
509 	{ 0x61, 0x00, "video aquisition error" },
510 	{ 0x62, 0x00, "scan head positioning error" },
511 	{ 0x63, 0x00, "end of user area encountered on this track" },
512 	{ 0x63, 0x01, "packet does not fit in available space" },
513 	{ 0x64, 0x00, "illegal mode for this track" },
514 	{ 0x64, 0x01, "invalid packet size" },
515 	{ 0x65, 0x00, "voltage fault" },
516 	{ 0x66, 0x00, "automatic document feeder cover up" },
517 	{ 0x67, 0x00, "configuration failure" },
518 	{ 0x67, 0x01, "configuration of incapable LUNs failed" },
519 	{ 0x67, 0x02, "add LUN failed" },
520 	{ 0x67, 0x03, "modification of LUN failed" },
521 	{ 0x67, 0x04, "exchange of LUN failed" },
522 	{ 0x67, 0x05, "remove of LUN failed" },
523 	{ 0x67, 0x06, "attachment of LUN failed" },
524 	{ 0x67, 0x07, "creation of LUN failed" },
525 	{ 0x67, 0x08, "assign failure occurred" },
526 	{ 0x67, 0x09, "multiply assigned LUN" },
527 	{ 0x67, 0x0a, "set target port groups command failed" },
528 	{ 0x68, 0x00, "logical unit not configured" },
529 	{ 0x69, 0x00, "data loss on logical unit" },
530 	{ 0x69, 0x01, "multiple LUN failures" },
531 	{ 0x69, 0x02, "parity/data mismatch" },
532 	{ 0x6a, 0x00, "informational, refer to log" },
533 	{ 0x6b, 0x00, "state change has occured" },
534 	{ 0x6b, 0x01, "redundancy level got better" },
535 	{ 0x6b, 0x02, "redundancy level got worse" },
536 	{ 0x6c, 0x00, "rebuild failure occured" },
537 	{ 0x6d, 0x00, "recalculate failure occured" },
538 	{ 0x6e, 0x00, "command to logical unit failed" },
539 	{ 0x6f, 0x00, "copy protect key exchange failure authentication "
540 		"failure" },
541 	{ 0x6f, 0x01, "copy protect key exchange failure key not present" },
542 	{ 0x6f, 0x02, "copy protect key exchange failure key not established" },
543 	{ 0x6f, 0x03, "read of scrambled sector without authentication" },
544 	{ 0x6f, 0x04, "media region code is mismatched to LUN region" },
545 	{ 0x6f, 0x05, "drive region must be permanent/region reset count "
546 		"error" },
547 	{ 0x70, 0xffff, "decompression exception short algorithm id of ASCQ" },
548 	{ 0x71, 0x00, "decompression exception long algorithm id" },
549 	{ 0x72, 0x00, "session fixation error" },
550 	{ 0x72, 0x01, "session fixation error writing lead-in" },
551 	{ 0x72, 0x02, "session fixation error writing lead-out" },
552 	{ 0x72, 0x03, "session fixation error - incomplete track in session" },
553 	{ 0x72, 0x04, "empty or partially written reserved track" },
554 	{ 0x72, 0x05, "no more track reservations allowed" },
555 	{ 0x73, 0x00, "cd control error" },
556 	{ 0x73, 0x01, "power calibration area almost full" },
557 	{ 0x73, 0x02, "power calibration area is full" },
558 	{ 0x73, 0x03, "power calibration area error" },
559 	{ 0x73, 0x04, "program memory area update failure" },
560 	{ 0x73, 0x05, "program memory area is full" },
561 	{ 0x73, 0x06, "rma/pma is almost full" },
562 	{ 0xffff, 0xffff, NULL }
563 };
564 
565 static const char *
find_string(slist_t * slist,int match_value)566 find_string(slist_t *slist, int match_value)
567 {
568 	for (; slist->str != NULL; slist++) {
569 		if (slist->value == match_value) {
570 			return (slist->str);
571 		}
572 	}
573 
574 	return (NULL);
575 }
576 
577 const char *
libscsi_sense_key_name(uint64_t key)578 libscsi_sense_key_name(uint64_t key)
579 {
580 	return (find_string(sensekey_strings, (int)key));
581 }
582 
583 /*
584  * Given an asc (Additional Sense Code) and ascq (Additional Sense Code
585  * Qualifier), return a string describing the error information.
586  */
587 const char *
libscsi_sense_code_name(uint64_t asc,uint64_t ascq)588 libscsi_sense_code_name(uint64_t asc, uint64_t ascq)
589 {
590 	int i = 0;
591 
592 	while (extended_sense_list[i].asc != 0xffff) {
593 		if ((asc == extended_sense_list[i].asc) &&
594 		    ((ascq == extended_sense_list[i].ascq) ||
595 		    (extended_sense_list[i].ascq == 0xffff))) {
596 			return ((char *)extended_sense_list[i].message);
597 		}
598 		i++;
599 	}
600 
601 	return (NULL);
602 }
603 
604 /*
605  * Retrieve "information" field from descriptor format sense data.  Iterates
606  * through each sense descriptor looking for the information descriptor and
607  * returns the information field from that descriptor.
608  */
609 static diskaddr_t
scsi_extract_sense_info_descr(struct scsi_descr_sense_hdr * sdsp,size_t len)610 scsi_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp, size_t len)
611 {
612 	diskaddr_t result;
613 	uint8_t *descr_offset;
614 	size_t valid_sense_length;
615 	struct scsi_information_sense_descr *isd;
616 
617 	/*
618 	 * Initialize result to -1 indicating there is no information
619 	 * descriptor
620 	 */
621 	result = (diskaddr_t)-1;
622 
623 	/*
624 	 * The first descriptor will immediately follow the header
625 	 */
626 	descr_offset = (uint8_t *)(sdsp+1);
627 
628 	/*
629 	 * Calculate the amount of valid sense data
630 	 */
631 	valid_sense_length =
632 	    MIN((sizeof (struct scsi_descr_sense_hdr) +
633 	    sdsp->ds_addl_sense_length), len);
634 
635 	/*
636 	 * Iterate through the list of descriptors, stopping when we run out of
637 	 * sense data
638 	 */
639 	while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <=
640 	    (uint8_t *)sdsp + valid_sense_length) {
641 		/*
642 		 * Check if this is an information descriptor.  We can use the
643 		 * scsi_information_sense_descr structure as a template since
644 		 * the first two fields are always the same
645 		 */
646 		isd = (struct scsi_information_sense_descr *)descr_offset;
647 		if (isd->isd_descr_type == DESCR_INFORMATION) {
648 			/*
649 			 * Found an information descriptor.  Copy the
650 			 * information field.  There will only be one
651 			 * information descriptor so we can stop looking.
652 			 */
653 			result =
654 			    (((diskaddr_t)isd->isd_information[0] << 56) |
655 			    ((diskaddr_t)isd->isd_information[1] << 48) |
656 			    ((diskaddr_t)isd->isd_information[2] << 40) |
657 			    ((diskaddr_t)isd->isd_information[3] << 32) |
658 			    ((diskaddr_t)isd->isd_information[4] << 24) |
659 			    ((diskaddr_t)isd->isd_information[5] << 16) |
660 			    ((diskaddr_t)isd->isd_information[6] << 8)  |
661 			    ((diskaddr_t)isd->isd_information[7]));
662 			break;
663 		}
664 
665 		/*
666 		 * Get pointer to the next descriptor.  The "additional length"
667 		 * field holds the length of the descriptor except for the
668 		 * "type" and "additional length" fields, so we need to add 2 to
669 		 * get the total length.
670 		 */
671 		descr_offset += (isd->isd_addl_length + 2);
672 	}
673 
674 	return (result);
675 }
676 
677 int
libscsi_action_parse_sense(const libscsi_action_t * ap,uint64_t * keyp,uint64_t * ascp,uint64_t * ascqp,diskaddr_t * blkp)678 libscsi_action_parse_sense(const libscsi_action_t *ap, uint64_t *keyp,
679     uint64_t *ascp, uint64_t *ascqp, diskaddr_t *blkp)
680 {
681 	struct scsi_extended_sense *xsp;
682 	struct scsi_descr_sense_hdr *sdsp;
683 	size_t len;
684 
685 	if (libscsi_action_get_sense(ap, (uint8_t **)&xsp, NULL, &len) != 0)
686 		return (-1);
687 
688 	sdsp = (struct scsi_descr_sense_hdr *)xsp;
689 
690 	if (keyp != NULL)
691 		*keyp = (uint64_t)xsp->es_key;
692 
693 	switch (xsp->es_code) {
694 	case CODE_FMT_DESCR_CURRENT:
695 	case CODE_FMT_DESCR_DEFERRED:
696 		if (blkp != NULL)
697 			*blkp = (diskaddr_t)
698 			    scsi_extract_sense_info_descr(sdsp, len);
699 		if (ascp != NULL)
700 			*ascp = (uint64_t)sdsp->ds_add_code;
701 		if (ascqp != NULL)
702 			*ascqp = (uint64_t)sdsp->ds_qual_code;
703 		break;
704 	case CODE_FMT_FIXED_CURRENT:
705 	case CODE_FMT_FIXED_DEFERRED:
706 	default:
707 		if (xsp->es_valid && blkp != NULL)
708 			*blkp = (diskaddr_t)
709 			    ((xsp->es_info_1 << 24) | (xsp->es_info_2 << 16) |
710 			    (xsp->es_info_3 << 8) | xsp->es_info_4);
711 		if (xsp->es_add_len >= 6) {
712 			if (ascp != NULL)
713 				*ascp = (uint64_t)xsp->es_add_code;
714 			if (ascqp != NULL)
715 				*ascqp = (uint64_t)xsp->es_qual_code;
716 		}
717 		break;
718 	}
719 
720 	return (0);
721 }
722