xref: /freebsd/sys/cam/scsi/scsi_all.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Implementation of Utility functions for all SCSI device types.
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 
35 #ifdef _KERNEL
36 #include <opt_scsi.h>
37 
38 #include <sys/systm.h>
39 #include <sys/libkern.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #else
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #endif
48 
49 #include <cam/cam.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <sys/sbuf.h>
55 #ifndef _KERNEL
56 #include <camlib.h>
57 
58 #ifndef FALSE
59 #define FALSE   0
60 #endif /* FALSE */
61 #ifndef TRUE
62 #define TRUE    1
63 #endif /* TRUE */
64 #define ERESTART        -1              /* restart syscall */
65 #define EJUSTRETURN     -2              /* don't modify regs, just return */
66 #endif /* !_KERNEL */
67 
68 /*
69  * This is the default number of milliseconds we wait for devices to settle
70  * after a SCSI bus reset.
71  */
72 #ifndef SCSI_DELAY
73 #define SCSI_DELAY 2000
74 #endif
75 /*
76  * All devices need _some_ sort of bus settle delay, so we'll set it to
77  * a minimum value of 100ms. Note that this is pertinent only for SPI-
78  * not transport like Fibre Channel or iSCSI where 'delay' is completely
79  * meaningless.
80  */
81 #ifndef SCSI_MIN_DELAY
82 #define SCSI_MIN_DELAY 100
83 #endif
84 /*
85  * Make sure the user isn't using seconds instead of milliseconds.
86  */
87 #if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
88 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
89 #endif
90 
91 int scsi_delay;
92 
93 static int	ascentrycomp(const void *key, const void *member);
94 static int	senseentrycomp(const void *key, const void *member);
95 static void	fetchtableentries(int sense_key, int asc, int ascq,
96 				  struct scsi_inquiry_data *,
97 				  const struct sense_key_table_entry **,
98 				  const struct asc_table_entry **);
99 #ifdef _KERNEL
100 static void	init_scsi_delay(void);
101 static int	sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
102 static int	set_scsi_delay(int delay);
103 #endif
104 
105 #if !defined(SCSI_NO_OP_STRINGS)
106 
107 #define	D	(1 << T_DIRECT)
108 #define	T	(1 << T_SEQUENTIAL)
109 #define	L	(1 << T_PRINTER)
110 #define	P	(1 << T_PROCESSOR)
111 #define	W	(1 << T_WORM)
112 #define	R	(1 << T_CDROM)
113 #define	O	(1 << T_OPTICAL)
114 #define	M	(1 << T_CHANGER)
115 #define	A	(1 << T_STORARRAY)
116 #define	E	(1 << T_ENCLOSURE)
117 #define	B	(1 << T_RBC)
118 #define	K	(1 << T_OCRW)
119 #define	V	(1 << T_ADC)
120 #define	F	(1 << T_OSD)
121 #define	S	(1 << T_SCANNER)
122 #define	C	(1 << T_COMM)
123 
124 #define ALL	(D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
125 
126 static struct op_table_entry plextor_cd_ops[] = {
127 	{ 0xD8, R, "CD-DA READ" }
128 };
129 
130 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
131 	{
132 		/*
133 		 * I believe that 0xD8 is the Plextor proprietary command
134 		 * to read CD-DA data.  I'm not sure which Plextor CDROM
135 		 * models support the command, though.  I know for sure
136 		 * that the 4X, 8X, and 12X models do, and presumably the
137 		 * 12-20X does.  I don't know about any earlier models,
138 		 * though.  If anyone has any more complete information,
139 		 * feel free to change this quirk entry.
140 		 */
141 		{T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
142 		sizeof(plextor_cd_ops)/sizeof(struct op_table_entry),
143 		plextor_cd_ops
144 	}
145 };
146 
147 static struct op_table_entry scsi_op_codes[] = {
148 	/*
149 	 * From: http://www.t10.org/lists/op-num.txt
150 	 * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
151 	 *              and Jung-uk Kim (jkim@FreeBSD.org)
152 	 *
153 	 * Note:  order is important in this table, scsi_op_desc() currently
154 	 * depends on the opcodes in the table being in order to save
155 	 * search time.
156 	 * Note:  scanner and comm. devices are carried over from the previous
157 	 * version because they were removed in the latest spec.
158 	 */
159 	/* File: OP-NUM.TXT
160 	 *
161 	 * SCSI Operation Codes
162 	 * Numeric Sorted Listing
163 	 * as of  3/11/08
164 	 *
165 	 *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
166 	 *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
167 	 *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
168 	 *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
169 	 *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
170 	 *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
171 	 *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
172 	 *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
173 	 *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
174 	 *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
175 	 *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
176 	 *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
177 	 *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
178 	 *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
179 	 * OP  DTLPWROMAEBKVF  Description
180 	 * --  --------------  ---------------------------------------------- */
181 	/* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
182 	{ 0x00,	ALL, "TEST UNIT READY" },
183 	/* 01   M              REWIND */
184 	{ 0x01,	T, "REWIND" },
185 	/* 01  Z V ZZZZ        REZERO UNIT */
186 	{ 0x01,	D | W | R | O | M, "REZERO UNIT" },
187 	/* 02  VVVVVV V */
188 	/* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
189 	{ 0x03,	ALL, "REQUEST SENSE" },
190 	/* 04  M    OO         FORMAT UNIT */
191 	{ 0x04,	D | R | O, "FORMAT UNIT" },
192 	/* 04   O              FORMAT MEDIUM */
193 	{ 0x04,	T, "FORMAT MEDIUM" },
194 	/* 04    O             FORMAT */
195 	{ 0x04,	L, "FORMAT" },
196 	/* 05  VMVVVV V        READ BLOCK LIMITS */
197 	{ 0x05,	T, "READ BLOCK LIMITS" },
198 	/* 06  VVVVVV V */
199 	/* 07  OVV O OV        REASSIGN BLOCKS */
200 	{ 0x07,	D | W | O, "REASSIGN BLOCKS" },
201 	/* 07         O        INITIALIZE ELEMENT STATUS */
202 	{ 0x07,	M, "INITIALIZE ELEMENT STATUS" },
203 	/* 08  MOV O OV        READ(6) */
204 	{ 0x08,	D | T | W | O, "READ(6)" },
205 	/* 08     O            RECEIVE */
206 	{ 0x08,	P, "RECEIVE" },
207 	/* 08                  GET MESSAGE(6) */
208 	{ 0x08, C, "GET MESSAGE(6)" },
209 	/* 09  VVVVVV V */
210 	/* 0A  OO  O OV        WRITE(6) */
211 	{ 0x0A,	D | T | W | O, "WRITE(6)" },
212 	/* 0A     M            SEND(6) */
213 	{ 0x0A,	P, "SEND(6)" },
214 	/* 0A                  SEND MESSAGE(6) */
215 	{ 0x0A, C, "SEND MESSAGE(6)" },
216 	/* 0A    M             PRINT */
217 	{ 0x0A,	L, "PRINT" },
218 	/* 0B  Z   ZOZV        SEEK(6) */
219 	{ 0x0B,	D | W | R | O, "SEEK(6)" },
220 	/* 0B   O              SET CAPACITY */
221 	{ 0x0B,	T, "SET CAPACITY" },
222 	/* 0B    O             SLEW AND PRINT */
223 	{ 0x0B,	L, "SLEW AND PRINT" },
224 	/* 0C  VVVVVV V */
225 	/* 0D  VVVVVV V */
226 	/* 0E  VVVVVV V */
227 	/* 0F  VOVVVV V        READ REVERSE(6) */
228 	{ 0x0F,	T, "READ REVERSE(6)" },
229 	/* 10  VM VVV          WRITE FILEMARKS(6) */
230 	{ 0x10,	T, "WRITE FILEMARKS(6)" },
231 	/* 10    O             SYNCHRONIZE BUFFER */
232 	{ 0x10,	L, "SYNCHRONIZE BUFFER" },
233 	/* 11  VMVVVV          SPACE(6) */
234 	{ 0x11,	T, "SPACE(6)" },
235 	/* 12  MMMMMMMMMMMMMM  INQUIRY */
236 	{ 0x12,	ALL, "INQUIRY" },
237 	/* 13  V VVVV */
238 	/* 13   O              VERIFY(6) */
239 	{ 0x13,	T, "VERIFY(6)" },
240 	/* 14  VOOVVV          RECOVER BUFFERED DATA */
241 	{ 0x14,	T | L, "RECOVER BUFFERED DATA" },
242 	/* 15  OMO O OOOO OO   MODE SELECT(6) */
243 	{ 0x15,	ALL & ~(P | R | B | F), "MODE SELECT(6)" },
244 	/* 16  ZZMZO OOOZ O    RESERVE(6) */
245 	{ 0x16,	ALL & ~(R | B | V | F | C), "RESERVE(6)" },
246 	/* 16         Z        RESERVE ELEMENT(6) */
247 	{ 0x16,	M, "RESERVE ELEMENT(6)" },
248 	/* 17  ZZMZO OOOZ O    RELEASE(6) */
249 	{ 0x17,	ALL & ~(R | B | V | F | C), "RELEASE(6)" },
250 	/* 17         Z        RELEASE ELEMENT(6) */
251 	{ 0x17,	M, "RELEASE ELEMENT(6)" },
252 	/* 18  ZZZZOZO    Z    COPY */
253 	{ 0x18,	D | T | L | P | W | R | O | K | S, "COPY" },
254 	/* 19  VMVVVV          ERASE(6) */
255 	{ 0x19,	T, "ERASE(6)" },
256 	/* 1A  OMO O OOOO OO   MODE SENSE(6) */
257 	{ 0x1A,	ALL & ~(P | R | B | F), "MODE SENSE(6)" },
258 	/* 1B  O   OOO O MO O  START STOP UNIT */
259 	{ 0x1B,	D | W | R | O | A | B | K | F, "START STOP UNIT" },
260 	/* 1B   O          M   LOAD UNLOAD */
261 	{ 0x1B,	T | V, "LOAD UNLOAD" },
262 	/* 1B                  SCAN */
263 	{ 0x1B, S, "SCAN" },
264 	/* 1B    O             STOP PRINT */
265 	{ 0x1B,	L, "STOP PRINT" },
266 	/* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
267 	{ 0x1B,	M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
268 	/* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
269 	{ 0x1C,	ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
270 	/* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
271 	{ 0x1D,	ALL & ~(R | B), "SEND DIAGNOSTIC" },
272 	/* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
273 	{ 0x1E,	D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
274 	/* 1F */
275 	/* 20  V   VVV    V */
276 	/* 21  V   VVV    V */
277 	/* 22  V   VVV    V */
278 	/* 23  V   V V    V */
279 	/* 23       O          READ FORMAT CAPACITIES */
280 	{ 0x23,	R, "READ FORMAT CAPACITIES" },
281 	/* 24  V   VV          SET WINDOW */
282 	{ 0x24, S, "SET WINDOW" },
283 	/* 25  M   M M   M     READ CAPACITY(10) */
284 	{ 0x25,	D | W | O | B, "READ CAPACITY(10)" },
285 	/* 25       O          READ CAPACITY */
286 	{ 0x25,	R, "READ CAPACITY" },
287 	/* 25             M    READ CARD CAPACITY */
288 	{ 0x25,	K, "READ CARD CAPACITY" },
289 	/* 25                  GET WINDOW */
290 	{ 0x25, S, "GET WINDOW" },
291 	/* 26  V   VV */
292 	/* 27  V   VV */
293 	/* 28  M   MOM   MM    READ(10) */
294 	{ 0x28,	D | W | R | O | B | K | S, "READ(10)" },
295 	/* 28                  GET MESSAGE(10) */
296 	{ 0x28, C, "GET MESSAGE(10)" },
297 	/* 29  V   VVO         READ GENERATION */
298 	{ 0x29,	O, "READ GENERATION" },
299 	/* 2A  O   MOM   MO    WRITE(10) */
300 	{ 0x2A,	D | W | R | O | B | K, "WRITE(10)" },
301 	/* 2A                  SEND(10) */
302 	{ 0x2A, S, "SEND(10)" },
303 	/* 2A                  SEND MESSAGE(10) */
304 	{ 0x2A, C, "SEND MESSAGE(10)" },
305 	/* 2B  Z   OOO    O    SEEK(10) */
306 	{ 0x2B,	D | W | R | O | K, "SEEK(10)" },
307 	/* 2B   O              LOCATE(10) */
308 	{ 0x2B,	T, "LOCATE(10)" },
309 	/* 2B         O        POSITION TO ELEMENT */
310 	{ 0x2B,	M, "POSITION TO ELEMENT" },
311 	/* 2C  V    OO         ERASE(10) */
312 	{ 0x2C,	R | O, "ERASE(10)" },
313 	/* 2D        O         READ UPDATED BLOCK */
314 	{ 0x2D,	O, "READ UPDATED BLOCK" },
315 	/* 2D  V */
316 	/* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
317 	{ 0x2E,	D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
318 	/* 2F  O   OOO         VERIFY(10) */
319 	{ 0x2F,	D | W | R | O, "VERIFY(10)" },
320 	/* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
321 	{ 0x30,	D | W | R | O, "SEARCH DATA HIGH(10)" },
322 	/* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
323 	{ 0x31,	D | W | R | O, "SEARCH DATA EQUAL(10)" },
324 	/* 31                  OBJECT POSITION */
325 	{ 0x31, S, "OBJECT POSITION" },
326 	/* 32  Z   ZZZ         SEARCH DATA LOW(10) */
327 	{ 0x32,	D | W | R | O, "SEARCH DATA LOW(10)" },
328 	/* 33  Z   OZO         SET LIMITS(10) */
329 	{ 0x33,	D | W | R | O, "SET LIMITS(10)" },
330 	/* 34  O   O O    O    PRE-FETCH(10) */
331 	{ 0x34,	D | W | O | K, "PRE-FETCH(10)" },
332 	/* 34   M              READ POSITION */
333 	{ 0x34,	T, "READ POSITION" },
334 	/* 34                  GET DATA BUFFER STATUS */
335 	{ 0x34, S, "GET DATA BUFFER STATUS" },
336 	/* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
337 	{ 0x35,	D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
338 	/* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
339 	{ 0x36,	D | W | O | K, "LOCK UNLOCK CACHE(10)" },
340 	/* 37  O     O         READ DEFECT DATA(10) */
341 	{ 0x37,	D | O, "READ DEFECT DATA(10)" },
342 	/* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
343 	{ 0x37,	M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
344 	/* 38      O O    O    MEDIUM SCAN */
345 	{ 0x38,	W | O | K, "MEDIUM SCAN" },
346 	/* 39  ZZZZOZO    Z    COMPARE */
347 	{ 0x39,	D | T | L | P | W | R | O | K | S, "COMPARE" },
348 	/* 3A  ZZZZOZO    Z    COPY AND VERIFY */
349 	{ 0x3A,	D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
350 	/* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
351 	{ 0x3B,	ALL, "WRITE BUFFER" },
352 	/* 3C  OOOOOOOOOO OOO  READ BUFFER */
353 	{ 0x3C,	ALL & ~(B), "READ BUFFER" },
354 	/* 3D        O         UPDATE BLOCK */
355 	{ 0x3D,	O, "UPDATE BLOCK" },
356 	/* 3E  O   O O         READ LONG(10) */
357 	{ 0x3E,	D | W | O, "READ LONG(10)" },
358 	/* 3F  O   O O         WRITE LONG(10) */
359 	{ 0x3F,	D | W | O, "WRITE LONG(10)" },
360 	/* 40  ZZZZOZOZ        CHANGE DEFINITION */
361 	{ 0x40,	D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
362 	/* 41  O               WRITE SAME(10) */
363 	{ 0x41,	D, "WRITE SAME(10)" },
364 	/* 42       O          READ SUB-CHANNEL */
365 	{ 0x42,	R, "READ SUB-CHANNEL" },
366 	/* 43       O          READ TOC/PMA/ATIP */
367 	{ 0x43,	R, "READ TOC/PMA/ATIP" },
368 	/* 44   M          M   REPORT DENSITY SUPPORT */
369 	{ 0x44,	T | V, "REPORT DENSITY SUPPORT" },
370 	/* 44                  READ HEADER */
371 	/* 45       O          PLAY AUDIO(10) */
372 	{ 0x45,	R, "PLAY AUDIO(10)" },
373 	/* 46       M          GET CONFIGURATION */
374 	{ 0x46,	R, "GET CONFIGURATION" },
375 	/* 47       O          PLAY AUDIO MSF */
376 	{ 0x47,	R, "PLAY AUDIO MSF" },
377 	/* 48 */
378 	/* 49 */
379 	/* 4A       M          GET EVENT STATUS NOTIFICATION */
380 	{ 0x4A,	R, "GET EVENT STATUS NOTIFICATION" },
381 	/* 4B       O          PAUSE/RESUME */
382 	{ 0x4B,	R, "PAUSE/RESUME" },
383 	/* 4C  OOOOO OOOO OOO  LOG SELECT */
384 	{ 0x4C,	ALL & ~(R | B), "LOG SELECT" },
385 	/* 4D  OOOOO OOOO OMO  LOG SENSE */
386 	{ 0x4D,	ALL & ~(R | B), "LOG SENSE" },
387 	/* 4E       O          STOP PLAY/SCAN */
388 	{ 0x4E,	R, "STOP PLAY/SCAN" },
389 	/* 4F */
390 	/* 50  O               XDWRITE(10) */
391 	{ 0x50,	D, "XDWRITE(10)" },
392 	/* 51  O               XPWRITE(10) */
393 	{ 0x51,	D, "XPWRITE(10)" },
394 	/* 51       O          READ DISC INFORMATION */
395 	{ 0x51,	R, "READ DISC INFORMATION" },
396 	/* 52  O               XDREAD(10) */
397 	{ 0x52,	D, "XDREAD(10)" },
398 	/* 52       O          READ TRACK INFORMATION */
399 	{ 0x52,	R, "READ TRACK INFORMATION" },
400 	/* 53       O          RESERVE TRACK */
401 	{ 0x53,	R, "RESERVE TRACK" },
402 	/* 54       O          SEND OPC INFORMATION */
403 	{ 0x54,	R, "SEND OPC INFORMATION" },
404 	/* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
405 	{ 0x55,	ALL & ~(P), "MODE SELECT(10)" },
406 	/* 56  ZZMZO OOOZ      RESERVE(10) */
407 	{ 0x56,	ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
408 	/* 56         Z        RESERVE ELEMENT(10) */
409 	{ 0x56,	M, "RESERVE ELEMENT(10)" },
410 	/* 57  ZZMZO OOOZ      RELEASE(10) */
411 	{ 0x57,	ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
412 	/* 57         Z        RELEASE ELEMENT(10) */
413 	{ 0x57,	M, "RELEASE ELEMENT(10)" },
414 	/* 58       O          REPAIR TRACK */
415 	{ 0x58,	R, "REPAIR TRACK" },
416 	/* 59 */
417 	/* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
418 	{ 0x5A,	ALL & ~(P), "MODE SENSE(10)" },
419 	/* 5B       O          CLOSE TRACK/SESSION */
420 	{ 0x5B,	R, "CLOSE TRACK/SESSION" },
421 	/* 5C       O          READ BUFFER CAPACITY */
422 	{ 0x5C,	R, "READ BUFFER CAPACITY" },
423 	/* 5D       O          SEND CUE SHEET */
424 	{ 0x5D,	R, "SEND CUE SHEET" },
425 	/* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
426 	{ 0x5E,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
427 	/* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
428 	{ 0x5F,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
429 	/* 7E  OO   O OOOO O   extended CDB */
430 	{ 0x7E,	D | T | R | M | A | E | B | V, "extended CDB" },
431 	/* 7F  O            M  variable length CDB (more than 16 bytes) */
432 	{ 0x7F,	D | F, "variable length CDB (more than 16 bytes)" },
433 	/* 80  Z               XDWRITE EXTENDED(16) */
434 	{ 0x80,	D, "XDWRITE EXTENDED(16)" },
435 	/* 80   M              WRITE FILEMARKS(16) */
436 	{ 0x80,	T, "WRITE FILEMARKS(16)" },
437 	/* 81  Z               REBUILD(16) */
438 	{ 0x81,	D, "REBUILD(16)" },
439 	/* 81   O              READ REVERSE(16) */
440 	{ 0x81,	T, "READ REVERSE(16)" },
441 	/* 82  Z               REGENERATE(16) */
442 	{ 0x82,	D, "REGENERATE(16)" },
443 	/* 83  OOOOO O    OO   EXTENDED COPY */
444 	{ 0x83,	D | T | L | P | W | O | K | V, "EXTENDED COPY" },
445 	/* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
446 	{ 0x84,	D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
447 	/* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
448 	{ 0x85,	D | R | B, "ATA COMMAND PASS THROUGH(16)" },
449 	/* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
450 	{ 0x86,	ALL & ~(L | R | F), "ACCESS CONTROL IN" },
451 	/* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
452 	{ 0x87,	ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
453 	/*
454 	 * XXX READ(16)/WRITE(16) were not listed for CD/DVE in op-num.txt
455 	 * but we had it since r1.40.  Do we really want them?
456 	 */
457 	/* 88  MM  O O   O     READ(16) */
458 	{ 0x88,	D | T | W | O | B, "READ(16)" },
459 	/* 89 */
460 	/* 8A  OM  O O   O     WRITE(16) */
461 	{ 0x8A,	D | T | W | O | B, "WRITE(16)" },
462 	/* 8B  O               ORWRITE */
463 	{ 0x8B,	D, "ORWRITE" },
464 	/* 8C  OO  O OO  O M   READ ATTRIBUTE */
465 	{ 0x8C,	D | T | W | O | M | B | V, "READ ATTRIBUTE" },
466 	/* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
467 	{ 0x8D,	D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
468 	/* 8E  O   O O   O     WRITE AND VERIFY(16) */
469 	{ 0x8E,	D | W | O | B, "WRITE AND VERIFY(16)" },
470 	/* 8F  OO  O O   O     VERIFY(16) */
471 	{ 0x8F,	D | T | W | O | B, "VERIFY(16)" },
472 	/* 90  O   O O   O     PRE-FETCH(16) */
473 	{ 0x90,	D | W | O | B, "PRE-FETCH(16)" },
474 	/* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
475 	{ 0x91,	D | W | O | B, "SYNCHRONIZE CACHE(16)" },
476 	/* 91   O              SPACE(16) */
477 	{ 0x91,	T, "SPACE(16)" },
478 	/* 92  Z   O O         LOCK UNLOCK CACHE(16) */
479 	{ 0x92,	D | W | O, "LOCK UNLOCK CACHE(16)" },
480 	/* 92   O              LOCATE(16) */
481 	{ 0x92,	T, "LOCATE(16)" },
482 	/* 93  O               WRITE SAME(16) */
483 	{ 0x93,	D, "WRITE SAME(16)" },
484 	/* 93   M              ERASE(16) */
485 	{ 0x93,	T, "ERASE(16)" },
486 	/* 94 [usage proposed by SCSI Socket Services project] */
487 	/* 95 [usage proposed by SCSI Socket Services project] */
488 	/* 96 [usage proposed by SCSI Socket Services project] */
489 	/* 97 [usage proposed by SCSI Socket Services project] */
490 	/* 98 */
491 	/* 99 */
492 	/* 9A */
493 	/* 9B */
494 	/* 9C */
495 	/* 9D */
496 	/* XXX KDM ALL for this?  op-num.txt defines it for none.. */
497 	/* 9E                  SERVICE ACTION IN(16) */
498 	{ 0x9E, ALL, "SERVICE ACTION IN(16)" },
499 	/* XXX KDM ALL for this?  op-num.txt defines it for ADC.. */
500 	/* 9F              M   SERVICE ACTION OUT(16) */
501 	{ 0x9F,	ALL, "SERVICE ACTION OUT(16)" },
502 	/* A0  MMOOO OMMM OMO  REPORT LUNS */
503 	{ 0xA0,	ALL & ~(R | B), "REPORT LUNS" },
504 	/* A1       O          BLANK */
505 	{ 0xA1,	R, "BLANK" },
506 	/* A1  O         O     ATA COMMAND PASS THROUGH(12) */
507 	{ 0xA1,	D | B, "ATA COMMAND PASS THROUGH(12)" },
508 	/* A2  OO   O      O   SECURITY PROTOCOL IN */
509 	{ 0xA2,	D | T | R | V, "SECURITY PROTOCOL IN" },
510 	/* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
511 	{ 0xA3,	ALL & ~(P | R | F), "MAINTENANCE (IN)" },
512 	/* A3       O          SEND KEY */
513 	{ 0xA3,	R, "SEND KEY" },
514 	/* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
515 	{ 0xA4,	ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
516 	/* A4       O          REPORT KEY */
517 	{ 0xA4,	R, "REPORT KEY" },
518 	/* A5   O  O OM        MOVE MEDIUM */
519 	{ 0xA5,	T | W | O | M, "MOVE MEDIUM" },
520 	/* A5       O          PLAY AUDIO(12) */
521 	{ 0xA5,	R, "PLAY AUDIO(12)" },
522 	/* A6         O        EXCHANGE MEDIUM */
523 	{ 0xA6,	M, "EXCHANGE MEDIUM" },
524 	/* A6       O          LOAD/UNLOAD C/DVD */
525 	{ 0xA6,	R, "LOAD/UNLOAD C/DVD" },
526 	/* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
527 	{ 0xA7,	D | T | W | O, "MOVE MEDIUM ATTACHED" },
528 	/* A7       O          SET READ AHEAD */
529 	{ 0xA7,	R, "SET READ AHEAD" },
530 	/* A8  O   OOO         READ(12) */
531 	{ 0xA8,	D | W | R | O, "READ(12)" },
532 	/* A8                  GET MESSAGE(12) */
533 	{ 0xA8, C, "GET MESSAGE(12)" },
534 	/* A9              O   SERVICE ACTION OUT(12) */
535 	{ 0xA9,	V, "SERVICE ACTION OUT(12)" },
536 	/* AA  O   OOO         WRITE(12) */
537 	{ 0xAA,	D | W | R | O, "WRITE(12)" },
538 	/* AA                  SEND MESSAGE(12) */
539 	{ 0xAA, C, "SEND MESSAGE(12)" },
540 	/* AB       O      O   SERVICE ACTION IN(12) */
541 	{ 0xAB,	R | V, "SERVICE ACTION IN(12)" },
542 	/* AC        O         ERASE(12) */
543 	{ 0xAC,	O, "ERASE(12)" },
544 	/* AC       O          GET PERFORMANCE */
545 	{ 0xAC,	R, "GET PERFORMANCE" },
546 	/* AD       O          READ DVD STRUCTURE */
547 	{ 0xAD,	R, "READ DVD STRUCTURE" },
548 	/* AE  O   O O         WRITE AND VERIFY(12) */
549 	{ 0xAE,	D | W | O, "WRITE AND VERIFY(12)" },
550 	/* AF  O   OZO         VERIFY(12) */
551 	{ 0xAF,	D | W | R | O, "VERIFY(12)" },
552 	/* B0      ZZZ         SEARCH DATA HIGH(12) */
553 	{ 0xB0,	W | R | O, "SEARCH DATA HIGH(12)" },
554 	/* B1      ZZZ         SEARCH DATA EQUAL(12) */
555 	{ 0xB1,	W | R | O, "SEARCH DATA EQUAL(12)" },
556 	/* B2      ZZZ         SEARCH DATA LOW(12) */
557 	{ 0xB2,	W | R | O, "SEARCH DATA LOW(12)" },
558 	/* B3  Z   OZO         SET LIMITS(12) */
559 	{ 0xB3,	D | W | R | O, "SET LIMITS(12)" },
560 	/* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
561 	{ 0xB4,	D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
562 	/* B5  OO   O      O   SECURITY PROTOCOL OUT */
563 	{ 0xB5,	D | T | R | V, "SECURITY PROTOCOL OUT" },
564 	/* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
565 	{ 0xB5,	M, "REQUEST VOLUME ELEMENT ADDRESS" },
566 	/* B6         O        SEND VOLUME TAG */
567 	{ 0xB6,	M, "SEND VOLUME TAG" },
568 	/* B6       O          SET STREAMING */
569 	{ 0xB6,	R, "SET STREAMING" },
570 	/* B7  O     O         READ DEFECT DATA(12) */
571 	{ 0xB7,	D | O, "READ DEFECT DATA(12)" },
572 	/* B8   O  OZOM        READ ELEMENT STATUS */
573 	{ 0xB8,	T | W | R | O | M, "READ ELEMENT STATUS" },
574 	/* B9       O          READ CD MSF */
575 	{ 0xB9,	R, "READ CD MSF" },
576 	/* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
577 	{ 0xBA,	D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
578 	/* BA       O          SCAN */
579 	{ 0xBA,	R, "SCAN" },
580 	/* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
581 	{ 0xBB,	D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
582 	/* BB       O          SET CD SPEED */
583 	{ 0xBB,	R, "SET CD SPEED" },
584 	/* BC  O   O OOMO      SPARE (IN) */
585 	{ 0xBC,	D | W | O | M | A | E, "SPARE (IN)" },
586 	/* BD  O   O OOOO      SPARE (OUT) */
587 	{ 0xBD,	D | W | O | M | A | E, "SPARE (OUT)" },
588 	/* BD       O          MECHANISM STATUS */
589 	{ 0xBD,	R, "MECHANISM STATUS" },
590 	/* BE  O   O OOMO      VOLUME SET (IN) */
591 	{ 0xBE,	D | W | O | M | A | E, "VOLUME SET (IN)" },
592 	/* BE       O          READ CD */
593 	{ 0xBE,	R, "READ CD" },
594 	/* BF  O   O OOOO      VOLUME SET (OUT) */
595 	{ 0xBF,	D | W | O | M | A | E, "VOLUME SET (OUT)" },
596 	/* BF       O          SEND DVD STRUCTURE */
597 	{ 0xBF,	R, "SEND DVD STRUCTURE" }
598 };
599 
600 const char *
601 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
602 {
603 	caddr_t match;
604 	int i, j;
605 	u_int32_t opmask;
606 	u_int16_t pd_type;
607 	int       num_ops[2];
608 	struct op_table_entry *table[2];
609 	int num_tables;
610 
611 	pd_type = SID_TYPE(inq_data);
612 
613 	match = cam_quirkmatch((caddr_t)inq_data,
614 			       (caddr_t)scsi_op_quirk_table,
615 			       sizeof(scsi_op_quirk_table)/
616 			       sizeof(*scsi_op_quirk_table),
617 			       sizeof(*scsi_op_quirk_table),
618 			       scsi_inquiry_match);
619 
620 	if (match != NULL) {
621 		table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
622 		num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
623 		table[1] = scsi_op_codes;
624 		num_ops[1] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
625 		num_tables = 2;
626 	} else {
627 		/*
628 		 * If this is true, we have a vendor specific opcode that
629 		 * wasn't covered in the quirk table.
630 		 */
631 		if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
632 			return("Vendor Specific Command");
633 
634 		table[0] = scsi_op_codes;
635 		num_ops[0] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
636 		num_tables = 1;
637 	}
638 
639 	/* RBC is 'Simplified' Direct Access Device */
640 	if (pd_type == T_RBC)
641 		pd_type = T_DIRECT;
642 
643 	opmask = 1 << pd_type;
644 
645 	for (j = 0; j < num_tables; j++) {
646 		for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
647 			if ((table[j][i].opcode == opcode)
648 			 && ((table[j][i].opmask & opmask) != 0))
649 				return(table[j][i].desc);
650 		}
651 	}
652 
653 	/*
654 	 * If we can't find a match for the command in the table, we just
655 	 * assume it's a vendor specifc command.
656 	 */
657 	return("Vendor Specific Command");
658 
659 }
660 
661 #else /* SCSI_NO_OP_STRINGS */
662 
663 const char *
664 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
665 {
666 	return("");
667 }
668 
669 #endif
670 
671 
672 #if !defined(SCSI_NO_SENSE_STRINGS)
673 #define SST(asc, ascq, action, desc) \
674 	asc, ascq, action, desc
675 #else
676 const char empty_string[] = "";
677 
678 #define SST(asc, ascq, action, desc) \
679 	asc, ascq, action, empty_string
680 #endif
681 
682 const struct sense_key_table_entry sense_key_table[] =
683 {
684 	{ SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
685 	{ SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
686 	{
687 	  SSD_KEY_NOT_READY, SS_TUR|SSQ_MANY|SSQ_DECREMENT_COUNT|EBUSY,
688 	  "NOT READY"
689 	},
690 	{ SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
691 	{ SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
692 	{ SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
693 	{ SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
694 	{ SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
695 	{ SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
696 	{ SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
697 	{ SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
698 	{ SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
699 	{ SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
700 	{ SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
701 	{ SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
702 	{ SSD_KEY_RESERVED, SS_FATAL|EIO, "RESERVED" }
703 };
704 
705 const int sense_key_table_size =
706     sizeof(sense_key_table)/sizeof(sense_key_table[0]);
707 
708 static struct asc_table_entry quantum_fireball_entries[] = {
709 	{ SST(0x04, 0x0b, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
710 	     "Logical unit not ready, initializing cmd. required") }
711 };
712 
713 static struct asc_table_entry sony_mo_entries[] = {
714 	{ SST(0x04, 0x00, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
715 	     "Logical unit not ready, cause not reportable") }
716 };
717 
718 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
719 	{
720 		/*
721 		 * XXX The Quantum Fireball ST and SE like to return 0x04 0x0b
722 		 * when they really should return 0x04 0x02.
723 		 */
724 		{T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
725 		/*num_sense_keys*/0,
726 		sizeof(quantum_fireball_entries)/sizeof(struct asc_table_entry),
727 		/*sense key entries*/NULL,
728 		quantum_fireball_entries
729 	},
730 	{
731 		/*
732 		 * This Sony MO drive likes to return 0x04, 0x00 when it
733 		 * isn't spun up.
734 		 */
735 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
736 		/*num_sense_keys*/0,
737 		sizeof(sony_mo_entries)/sizeof(struct asc_table_entry),
738 		/*sense key entries*/NULL,
739 		sony_mo_entries
740 	}
741 };
742 
743 const int sense_quirk_table_size =
744     sizeof(sense_quirk_table)/sizeof(sense_quirk_table[0]);
745 
746 static struct asc_table_entry asc_table[] = {
747 	/*
748 	 * From: http://www.t10.org/lists/asc-num.txt
749 	 * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
750 	 */
751 	/*
752 	 * File: ASC-NUM.TXT
753 	 *
754 	 * SCSI ASC/ASCQ Assignments
755 	 * Numeric Sorted Listing
756 	 * as of  7/29/08
757 	 *
758 	 * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
759 	 * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
760 	 * . L - PRINTER DEVICE (SSC)                           blank = reserved
761 	 * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
762 	 * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
763 	 * .  . R - CD DEVICE (MMC)
764 	 * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
765 	 * .  .  .M - MEDIA CHANGER DEVICE (SMC)
766 	 * .  .  . A - STORAGE ARRAY DEVICE (SCC)
767 	 * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
768 	 * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
769 	 * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
770 	 * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
771 	 * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
772 	 * DTLPWROMAEBKVF
773 	 * ASC      ASCQ  Action
774 	 * Description
775 	 */
776 	/* DTLPWROMAEBKVF */
777 	{ SST(0x00, 0x00, SS_NOP,
778 	    "No additional sense information") },
779 	/*  T             */
780 	{ SST(0x00, 0x01, SS_RDEF,
781 	    "Filemark detected") },
782 	/*  T             */
783 	{ SST(0x00, 0x02, SS_RDEF,
784 	    "End-of-partition/medium detected") },
785 	/*  T             */
786 	{ SST(0x00, 0x03, SS_RDEF,
787 	    "Setmark detected") },
788 	/*  T             */
789 	{ SST(0x00, 0x04, SS_RDEF,
790 	    "Beginning-of-partition/medium detected") },
791 	/*  TL            */
792 	{ SST(0x00, 0x05, SS_RDEF,
793 	    "End-of-data detected") },
794 	/* DTLPWROMAEBKVF */
795 	{ SST(0x00, 0x06, SS_RDEF,
796 	    "I/O process terminated") },
797 	/*  T             */
798 	{ SST(0x00, 0x07, SS_RDEF,	/* XXX TBD */
799 	    "Programmable early warning detected") },
800 	/*      R         */
801 	{ SST(0x00, 0x11, SS_FATAL | EBUSY,
802 	    "Audio play operation in progress") },
803 	/*      R         */
804 	{ SST(0x00, 0x12, SS_NOP,
805 	    "Audio play operation paused") },
806 	/*      R         */
807 	{ SST(0x00, 0x13, SS_NOP,
808 	    "Audio play operation successfully completed") },
809 	/*      R         */
810 	{ SST(0x00, 0x14, SS_RDEF,
811 	    "Audio play operation stopped due to error") },
812 	/*      R         */
813 	{ SST(0x00, 0x15, SS_NOP,
814 	    "No current audio status to return") },
815 	/* DTLPWROMAEBKVF */
816 	{ SST(0x00, 0x16, SS_FATAL | EBUSY,
817 	    "Operation in progress") },
818 	/* DTL WROMAEBKVF */
819 	{ SST(0x00, 0x17, SS_RDEF,
820 	    "Cleaning requested") },
821 	/*  T             */
822 	{ SST(0x00, 0x18, SS_RDEF,	/* XXX TBD */
823 	    "Erase operation in progress") },
824 	/*  T             */
825 	{ SST(0x00, 0x19, SS_RDEF,	/* XXX TBD */
826 	    "Locate operation in progress") },
827 	/*  T             */
828 	{ SST(0x00, 0x1A, SS_RDEF,	/* XXX TBD */
829 	    "Rewind operation in progress") },
830 	/*  T             */
831 	{ SST(0x00, 0x1B, SS_RDEF,	/* XXX TBD */
832 	    "Set capacity operation in progress") },
833 	/*  T             */
834 	{ SST(0x00, 0x1C, SS_RDEF,	/* XXX TBD */
835 	    "Verify operation in progress") },
836 	/* DT        B    */
837 	{ SST(0x00, 0x1D, SS_RDEF,	/* XXX TBD */
838 	    "ATA pass through information available") },
839 	/* DT   R MAEBKV  */
840 	{ SST(0x00, 0x1E, SS_RDEF,	/* XXX TBD */
841 	    "Conflicting SA creation request") },
842 	/* D   W O   BK   */
843 	{ SST(0x01, 0x00, SS_RDEF,
844 	    "No index/sector signal") },
845 	/* D   WRO   BK   */
846 	{ SST(0x02, 0x00, SS_RDEF,
847 	    "No seek complete") },
848 	/* DTL W O   BK   */
849 	{ SST(0x03, 0x00, SS_RDEF,
850 	    "Peripheral device write fault") },
851 	/*  T             */
852 	{ SST(0x03, 0x01, SS_RDEF,
853 	    "No write current") },
854 	/*  T             */
855 	{ SST(0x03, 0x02, SS_RDEF,
856 	    "Excessive write errors") },
857 	/* DTLPWROMAEBKVF */
858 	{ SST(0x04, 0x00, SS_TUR | SSQ_MANY | SSQ_DECREMENT_COUNT | EIO,
859 	    "Logical unit not ready, cause not reportable") },
860 	/* DTLPWROMAEBKVF */
861 	{ SST(0x04, 0x01, SS_TUR | SSQ_MANY | SSQ_DECREMENT_COUNT | EBUSY,
862 	    "Logical unit is in process of becoming ready") },
863 	/* DTLPWROMAEBKVF */
864 	{ SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
865 	    "Logical unit not ready, initializing command required") },
866 	/* DTLPWROMAEBKVF */
867 	{ SST(0x04, 0x03, SS_FATAL | ENXIO,
868 	    "Logical unit not ready, manual intervention required") },
869 	/* DTL  RO   B    */
870 	{ SST(0x04, 0x04, SS_FATAL | EBUSY,
871 	    "Logical unit not ready, format in progress") },
872 	/* DT  W O A BK F */
873 	{ SST(0x04, 0x05, SS_FATAL | EBUSY,
874 	    "Logical unit not ready, rebuild in progress") },
875 	/* DT  W O A BK   */
876 	{ SST(0x04, 0x06, SS_FATAL | EBUSY,
877 	    "Logical unit not ready, recalculation in progress") },
878 	/* DTLPWROMAEBKVF */
879 	{ SST(0x04, 0x07, SS_FATAL | EBUSY,
880 	    "Logical unit not ready, operation in progress") },
881 	/*      R         */
882 	{ SST(0x04, 0x08, SS_FATAL | EBUSY,
883 	    "Logical unit not ready, long write in progress") },
884 	/* DTLPWROMAEBKVF */
885 	{ SST(0x04, 0x09, SS_RDEF,	/* XXX TBD */
886 	    "Logical unit not ready, self-test in progress") },
887 	/* DTLPWROMAEBKVF */
888 	{ SST(0x04, 0x0A, SS_RDEF,	/* XXX TBD */
889 	    "Logical unit not accessible, asymmetric access state transition")},
890 	/* DTLPWROMAEBKVF */
891 	{ SST(0x04, 0x0B, SS_RDEF,	/* XXX TBD */
892 	    "Logical unit not accessible, target port in standby state") },
893 	/* DTLPWROMAEBKVF */
894 	{ SST(0x04, 0x0C, SS_RDEF,	/* XXX TBD */
895 	    "Logical unit not accessible, target port in unavailable state") },
896 	/*              F */
897 	{ SST(0x04, 0x0D, SS_RDEF,	/* XXX TBD */
898 	    "Logical unit not ready, structure check required") },
899 	/* DT  WROM  B    */
900 	{ SST(0x04, 0x10, SS_RDEF,	/* XXX TBD */
901 	    "Logical unit not ready, auxiliary memory not accessible") },
902 	/* DT  WRO AEB VF */
903 	{ SST(0x04, 0x11, SS_RDEF,	/* XXX TBD */
904 	    "Logical unit not ready, notify (enable spinup) required") },
905 	/*        M    V  */
906 	{ SST(0x04, 0x12, SS_RDEF,	/* XXX TBD */
907 	    "Logical unit not ready, offline") },
908 	/* DT   R MAEBKV  */
909 	{ SST(0x04, 0x13, SS_RDEF,	/* XXX TBD */
910 	    "Logical unit not ready, SA creation in progress") },
911 	/* DTL WROMAEBKVF */
912 	{ SST(0x05, 0x00, SS_RDEF,
913 	    "Logical unit does not respond to selection") },
914 	/* D   WROM  BK   */
915 	{ SST(0x06, 0x00, SS_RDEF,
916 	    "No reference position found") },
917 	/* DTL WROM  BK   */
918 	{ SST(0x07, 0x00, SS_RDEF,
919 	    "Multiple peripheral devices selected") },
920 	/* DTL WROMAEBKVF */
921 	{ SST(0x08, 0x00, SS_RDEF,
922 	    "Logical unit communication failure") },
923 	/* DTL WROMAEBKVF */
924 	{ SST(0x08, 0x01, SS_RDEF,
925 	    "Logical unit communication time-out") },
926 	/* DTL WROMAEBKVF */
927 	{ SST(0x08, 0x02, SS_RDEF,
928 	    "Logical unit communication parity error") },
929 	/* DT   ROM  BK   */
930 	{ SST(0x08, 0x03, SS_RDEF,
931 	    "Logical unit communication CRC error (Ultra-DMA/32)") },
932 	/* DTLPWRO    K   */
933 	{ SST(0x08, 0x04, SS_RDEF,	/* XXX TBD */
934 	    "Unreachable copy target") },
935 	/* DT  WRO   B    */
936 	{ SST(0x09, 0x00, SS_RDEF,
937 	    "Track following error") },
938 	/*     WRO    K   */
939 	{ SST(0x09, 0x01, SS_RDEF,
940 	    "Tracking servo failure") },
941 	/*     WRO    K   */
942 	{ SST(0x09, 0x02, SS_RDEF,
943 	    "Focus servo failure") },
944 	/*     WRO        */
945 	{ SST(0x09, 0x03, SS_RDEF,
946 	    "Spindle servo failure") },
947 	/* DT  WRO   B    */
948 	{ SST(0x09, 0x04, SS_RDEF,
949 	    "Head select fault") },
950 	/* DTLPWROMAEBKVF */
951 	{ SST(0x0A, 0x00, SS_FATAL | ENOSPC,
952 	    "Error log overflow") },
953 	/* DTLPWROMAEBKVF */
954 	{ SST(0x0B, 0x00, SS_RDEF,
955 	    "Warning") },
956 	/* DTLPWROMAEBKVF */
957 	{ SST(0x0B, 0x01, SS_RDEF,
958 	    "Warning - specified temperature exceeded") },
959 	/* DTLPWROMAEBKVF */
960 	{ SST(0x0B, 0x02, SS_RDEF,
961 	    "Warning - enclosure degraded") },
962 	/* DTLPWROMAEBKVF */
963 	{ SST(0x0B, 0x03, SS_RDEF,	/* XXX TBD */
964 	    "Warning - background self-test failed") },
965 	/* DTLPWRO AEBKVF */
966 	{ SST(0x0B, 0x04, SS_RDEF,	/* XXX TBD */
967 	    "Warning - background pre-scan detected medium error") },
968 	/* DTLPWRO AEBKVF */
969 	{ SST(0x0B, 0x05, SS_RDEF,	/* XXX TBD */
970 	    "Warning - background medium scan detected medium error") },
971 	/* DTLPWROMAEBKVF */
972 	{ SST(0x0B, 0x06, SS_RDEF,	/* XXX TBD */
973 	    "Warning - non-volatile cache now volatile") },
974 	/* DTLPWROMAEBKVF */
975 	{ SST(0x0B, 0x07, SS_RDEF,	/* XXX TBD */
976 	    "Warning - degraded power to non-volatile cache") },
977 	/*  T   R         */
978 	{ SST(0x0C, 0x00, SS_RDEF,
979 	    "Write error") },
980 	/*            K   */
981 	{ SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
982 	    "Write error - recovered with auto reallocation") },
983 	/* D   W O   BK   */
984 	{ SST(0x0C, 0x02, SS_RDEF,
985 	    "Write error - auto reallocation failed") },
986 	/* D   W O   BK   */
987 	{ SST(0x0C, 0x03, SS_RDEF,
988 	    "Write error - recommend reassignment") },
989 	/* DT  W O   B    */
990 	{ SST(0x0C, 0x04, SS_RDEF,
991 	    "Compression check miscompare error") },
992 	/* DT  W O   B    */
993 	{ SST(0x0C, 0x05, SS_RDEF,
994 	    "Data expansion occurred during compression") },
995 	/* DT  W O   B    */
996 	{ SST(0x0C, 0x06, SS_RDEF,
997 	    "Block not compressible") },
998 	/*      R         */
999 	{ SST(0x0C, 0x07, SS_RDEF,
1000 	    "Write error - recovery needed") },
1001 	/*      R         */
1002 	{ SST(0x0C, 0x08, SS_RDEF,
1003 	    "Write error - recovery failed") },
1004 	/*      R         */
1005 	{ SST(0x0C, 0x09, SS_RDEF,
1006 	    "Write error - loss of streaming") },
1007 	/*      R         */
1008 	{ SST(0x0C, 0x0A, SS_RDEF,
1009 	    "Write error - padding blocks added") },
1010 	/* DT  WROM  B    */
1011 	{ SST(0x0C, 0x0B, SS_RDEF,	/* XXX TBD */
1012 	    "Auxiliary memory write error") },
1013 	/* DTLPWRO AEBKVF */
1014 	{ SST(0x0C, 0x0C, SS_RDEF,	/* XXX TBD */
1015 	    "Write error - unexpected unsolicited data") },
1016 	/* DTLPWRO AEBKVF */
1017 	{ SST(0x0C, 0x0D, SS_RDEF,	/* XXX TBD */
1018 	    "Write error - not enough unsolicited data") },
1019 	/*      R         */
1020 	{ SST(0x0C, 0x0F, SS_RDEF,	/* XXX TBD */
1021 	    "Defects in error window") },
1022 	/* DTLPWRO A  K   */
1023 	{ SST(0x0D, 0x00, SS_RDEF,	/* XXX TBD */
1024 	    "Error detected by third party temporary initiator") },
1025 	/* DTLPWRO A  K   */
1026 	{ SST(0x0D, 0x01, SS_RDEF,	/* XXX TBD */
1027 	    "Third party device failure") },
1028 	/* DTLPWRO A  K   */
1029 	{ SST(0x0D, 0x02, SS_RDEF,	/* XXX TBD */
1030 	    "Copy target device not reachable") },
1031 	/* DTLPWRO A  K   */
1032 	{ SST(0x0D, 0x03, SS_RDEF,	/* XXX TBD */
1033 	    "Incorrect copy target device type") },
1034 	/* DTLPWRO A  K   */
1035 	{ SST(0x0D, 0x04, SS_RDEF,	/* XXX TBD */
1036 	    "Copy target device data underrun") },
1037 	/* DTLPWRO A  K   */
1038 	{ SST(0x0D, 0x05, SS_RDEF,	/* XXX TBD */
1039 	    "Copy target device data overrun") },
1040 	/* DT PWROMAEBK F */
1041 	{ SST(0x0E, 0x00, SS_RDEF,	/* XXX TBD */
1042 	    "Invalid information unit") },
1043 	/* DT PWROMAEBK F */
1044 	{ SST(0x0E, 0x01, SS_RDEF,	/* XXX TBD */
1045 	    "Information unit too short") },
1046 	/* DT PWROMAEBK F */
1047 	{ SST(0x0E, 0x02, SS_RDEF,	/* XXX TBD */
1048 	    "Information unit too long") },
1049 	/* DT P R MAEBK F */
1050 	{ SST(0x0E, 0x03, SS_RDEF,	/* XXX TBD */
1051 	    "Invalid field in command information unit") },
1052 	/* D   W O   BK   */
1053 	{ SST(0x10, 0x00, SS_RDEF,
1054 	    "ID CRC or ECC error") },
1055 	/* DT  W O        */
1056 	{ SST(0x10, 0x01, SS_RDEF,	/* XXX TBD */
1057 	    "Logical block guard check failed") },
1058 	/* DT  W O        */
1059 	{ SST(0x10, 0x02, SS_RDEF,	/* XXX TBD */
1060 	    "Logical block application tag check failed") },
1061 	/* DT  W O        */
1062 	{ SST(0x10, 0x03, SS_RDEF,	/* XXX TBD */
1063 	    "Logical block reference tag check failed") },
1064 	/* DT  WRO   BK   */
1065 	{ SST(0x11, 0x00, SS_RDEF,
1066 	    "Unrecovered read error") },
1067 	/* DT  WRO   BK   */
1068 	{ SST(0x11, 0x01, SS_RDEF,
1069 	    "Read retries exhausted") },
1070 	/* DT  WRO   BK   */
1071 	{ SST(0x11, 0x02, SS_RDEF,
1072 	    "Error too long to correct") },
1073 	/* DT  W O   BK   */
1074 	{ SST(0x11, 0x03, SS_RDEF,
1075 	    "Multiple read errors") },
1076 	/* D   W O   BK   */
1077 	{ SST(0x11, 0x04, SS_RDEF,
1078 	    "Unrecovered read error - auto reallocate failed") },
1079 	/*     WRO   B    */
1080 	{ SST(0x11, 0x05, SS_RDEF,
1081 	    "L-EC uncorrectable error") },
1082 	/*     WRO   B    */
1083 	{ SST(0x11, 0x06, SS_RDEF,
1084 	    "CIRC unrecovered error") },
1085 	/*     W O   B    */
1086 	{ SST(0x11, 0x07, SS_RDEF,
1087 	    "Data re-synchronization error") },
1088 	/*  T             */
1089 	{ SST(0x11, 0x08, SS_RDEF,
1090 	    "Incomplete block read") },
1091 	/*  T             */
1092 	{ SST(0x11, 0x09, SS_RDEF,
1093 	    "No gap found") },
1094 	/* DT    O   BK   */
1095 	{ SST(0x11, 0x0A, SS_RDEF,
1096 	    "Miscorrected error") },
1097 	/* D   W O   BK   */
1098 	{ SST(0x11, 0x0B, SS_RDEF,
1099 	    "Unrecovered read error - recommend reassignment") },
1100 	/* D   W O   BK   */
1101 	{ SST(0x11, 0x0C, SS_RDEF,
1102 	    "Unrecovered read error - recommend rewrite the data") },
1103 	/* DT  WRO   B    */
1104 	{ SST(0x11, 0x0D, SS_RDEF,
1105 	    "De-compression CRC error") },
1106 	/* DT  WRO   B    */
1107 	{ SST(0x11, 0x0E, SS_RDEF,
1108 	    "Cannot decompress using declared algorithm") },
1109 	/*      R         */
1110 	{ SST(0x11, 0x0F, SS_RDEF,
1111 	    "Error reading UPC/EAN number") },
1112 	/*      R         */
1113 	{ SST(0x11, 0x10, SS_RDEF,
1114 	    "Error reading ISRC number") },
1115 	/*      R         */
1116 	{ SST(0x11, 0x11, SS_RDEF,
1117 	    "Read error - loss of streaming") },
1118 	/* DT  WROM  B    */
1119 	{ SST(0x11, 0x12, SS_RDEF,	/* XXX TBD */
1120 	    "Auxiliary memory read error") },
1121 	/* DTLPWRO AEBKVF */
1122 	{ SST(0x11, 0x13, SS_RDEF,	/* XXX TBD */
1123 	    "Read error - failed retransmission request") },
1124 	/* D              */
1125 	{ SST(0x11, 0x14, SS_RDEF,	/* XXX TBD */
1126 	    "Read error - LBA marked bad by application client") },
1127 	/* D   W O   BK   */
1128 	{ SST(0x12, 0x00, SS_RDEF,
1129 	    "Address mark not found for ID field") },
1130 	/* D   W O   BK   */
1131 	{ SST(0x13, 0x00, SS_RDEF,
1132 	    "Address mark not found for data field") },
1133 	/* DTL WRO   BK   */
1134 	{ SST(0x14, 0x00, SS_RDEF,
1135 	    "Recorded entity not found") },
1136 	/* DT  WRO   BK   */
1137 	{ SST(0x14, 0x01, SS_RDEF,
1138 	    "Record not found") },
1139 	/*  T             */
1140 	{ SST(0x14, 0x02, SS_RDEF,
1141 	    "Filemark or setmark not found") },
1142 	/*  T             */
1143 	{ SST(0x14, 0x03, SS_RDEF,
1144 	    "End-of-data not found") },
1145 	/*  T             */
1146 	{ SST(0x14, 0x04, SS_RDEF,
1147 	    "Block sequence error") },
1148 	/* DT  W O   BK   */
1149 	{ SST(0x14, 0x05, SS_RDEF,
1150 	    "Record not found - recommend reassignment") },
1151 	/* DT  W O   BK   */
1152 	{ SST(0x14, 0x06, SS_RDEF,
1153 	    "Record not found - data auto-reallocated") },
1154 	/*  T             */
1155 	{ SST(0x14, 0x07, SS_RDEF,	/* XXX TBD */
1156 	    "Locate operation failure") },
1157 	/* DTL WROM  BK   */
1158 	{ SST(0x15, 0x00, SS_RDEF,
1159 	    "Random positioning error") },
1160 	/* DTL WROM  BK   */
1161 	{ SST(0x15, 0x01, SS_RDEF,
1162 	    "Mechanical positioning error") },
1163 	/* DT  WRO   BK   */
1164 	{ SST(0x15, 0x02, SS_RDEF,
1165 	    "Positioning error detected by read of medium") },
1166 	/* D   W O   BK   */
1167 	{ SST(0x16, 0x00, SS_RDEF,
1168 	    "Data synchronization mark error") },
1169 	/* D   W O   BK   */
1170 	{ SST(0x16, 0x01, SS_RDEF,
1171 	    "Data sync error - data rewritten") },
1172 	/* D   W O   BK   */
1173 	{ SST(0x16, 0x02, SS_RDEF,
1174 	    "Data sync error - recommend rewrite") },
1175 	/* D   W O   BK   */
1176 	{ SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1177 	    "Data sync error - data auto-reallocated") },
1178 	/* D   W O   BK   */
1179 	{ SST(0x16, 0x04, SS_RDEF,
1180 	    "Data sync error - recommend reassignment") },
1181 	/* DT  WRO   BK   */
1182 	{ SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1183 	    "Recovered data with no error correction applied") },
1184 	/* DT  WRO   BK   */
1185 	{ SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1186 	    "Recovered data with retries") },
1187 	/* DT  WRO   BK   */
1188 	{ SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1189 	    "Recovered data with positive head offset") },
1190 	/* DT  WRO   BK   */
1191 	{ SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1192 	    "Recovered data with negative head offset") },
1193 	/*     WRO   B    */
1194 	{ SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1195 	    "Recovered data with retries and/or CIRC applied") },
1196 	/* D   WRO   BK   */
1197 	{ SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1198 	    "Recovered data using previous sector ID") },
1199 	/* D   W O   BK   */
1200 	{ SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1201 	    "Recovered data without ECC - data auto-reallocated") },
1202 	/* D   WRO   BK   */
1203 	{ SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1204 	    "Recovered data without ECC - recommend reassignment") },
1205 	/* D   WRO   BK   */
1206 	{ SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1207 	    "Recovered data without ECC - recommend rewrite") },
1208 	/* D   WRO   BK   */
1209 	{ SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1210 	    "Recovered data without ECC - data rewritten") },
1211 	/* DT  WRO   BK   */
1212 	{ SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1213 	    "Recovered data with error correction applied") },
1214 	/* D   WRO   BK   */
1215 	{ SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1216 	    "Recovered data with error corr. & retries applied") },
1217 	/* D   WRO   BK   */
1218 	{ SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1219 	    "Recovered data - data auto-reallocated") },
1220 	/*      R         */
1221 	{ SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1222 	    "Recovered data with CIRC") },
1223 	/*      R         */
1224 	{ SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1225 	    "Recovered data with L-EC") },
1226 	/* D   WRO   BK   */
1227 	{ SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1228 	    "Recovered data - recommend reassignment") },
1229 	/* D   WRO   BK   */
1230 	{ SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1231 	    "Recovered data - recommend rewrite") },
1232 	/* D   W O   BK   */
1233 	{ SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1234 	    "Recovered data with ECC - data rewritten") },
1235 	/*      R         */
1236 	{ SST(0x18, 0x08, SS_RDEF,	/* XXX TBD */
1237 	    "Recovered data with linking") },
1238 	/* D     O    K   */
1239 	{ SST(0x19, 0x00, SS_RDEF,
1240 	    "Defect list error") },
1241 	/* D     O    K   */
1242 	{ SST(0x19, 0x01, SS_RDEF,
1243 	    "Defect list not available") },
1244 	/* D     O    K   */
1245 	{ SST(0x19, 0x02, SS_RDEF,
1246 	    "Defect list error in primary list") },
1247 	/* D     O    K   */
1248 	{ SST(0x19, 0x03, SS_RDEF,
1249 	    "Defect list error in grown list") },
1250 	/* DTLPWROMAEBKVF */
1251 	{ SST(0x1A, 0x00, SS_RDEF,
1252 	    "Parameter list length error") },
1253 	/* DTLPWROMAEBKVF */
1254 	{ SST(0x1B, 0x00, SS_RDEF,
1255 	    "Synchronous data transfer error") },
1256 	/* D     O   BK   */
1257 	{ SST(0x1C, 0x00, SS_RDEF,
1258 	    "Defect list not found") },
1259 	/* D     O   BK   */
1260 	{ SST(0x1C, 0x01, SS_RDEF,
1261 	    "Primary defect list not found") },
1262 	/* D     O   BK   */
1263 	{ SST(0x1C, 0x02, SS_RDEF,
1264 	    "Grown defect list not found") },
1265 	/* DT  WRO   BK   */
1266 	{ SST(0x1D, 0x00, SS_FATAL,
1267 	    "Miscompare during verify operation") },
1268 	/* D   W O   BK   */
1269 	{ SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1270 	    "Recovered ID with ECC correction") },
1271 	/* D     O    K   */
1272 	{ SST(0x1F, 0x00, SS_RDEF,
1273 	    "Partial defect list transfer") },
1274 	/* DTLPWROMAEBKVF */
1275 	{ SST(0x20, 0x00, SS_FATAL | EINVAL,
1276 	    "Invalid command operation code") },
1277 	/* DT PWROMAEBK   */
1278 	{ SST(0x20, 0x01, SS_RDEF,	/* XXX TBD */
1279 	    "Access denied - initiator pending-enrolled") },
1280 	/* DT PWROMAEBK   */
1281 	{ SST(0x20, 0x02, SS_RDEF,	/* XXX TBD */
1282 	    "Access denied - no access rights") },
1283 	/* DT PWROMAEBK   */
1284 	{ SST(0x20, 0x03, SS_RDEF,	/* XXX TBD */
1285 	    "Access denied - invalid mgmt ID key") },
1286 	/*  T             */
1287 	{ SST(0x20, 0x04, SS_RDEF,	/* XXX TBD */
1288 	    "Illegal command while in write capable state") },
1289 	/*  T             */
1290 	{ SST(0x20, 0x05, SS_RDEF,	/* XXX TBD */
1291 	    "Obsolete") },
1292 	/*  T             */
1293 	{ SST(0x20, 0x06, SS_RDEF,	/* XXX TBD */
1294 	    "Illegal command while in explicit address mode") },
1295 	/*  T             */
1296 	{ SST(0x20, 0x07, SS_RDEF,	/* XXX TBD */
1297 	    "Illegal command while in implicit address mode") },
1298 	/* DT PWROMAEBK   */
1299 	{ SST(0x20, 0x08, SS_RDEF,	/* XXX TBD */
1300 	    "Access denied - enrollment conflict") },
1301 	/* DT PWROMAEBK   */
1302 	{ SST(0x20, 0x09, SS_RDEF,	/* XXX TBD */
1303 	    "Access denied - invalid LU identifier") },
1304 	/* DT PWROMAEBK   */
1305 	{ SST(0x20, 0x0A, SS_RDEF,	/* XXX TBD */
1306 	    "Access denied - invalid proxy token") },
1307 	/* DT PWROMAEBK   */
1308 	{ SST(0x20, 0x0B, SS_RDEF,	/* XXX TBD */
1309 	    "Access denied - ACL LUN conflict") },
1310 	/* DT  WRO   BK   */
1311 	{ SST(0x21, 0x00, SS_FATAL | EINVAL,
1312 	    "Logical block address out of range") },
1313 	/* DT  WROM  BK   */
1314 	{ SST(0x21, 0x01, SS_FATAL | EINVAL,
1315 	    "Invalid element address") },
1316 	/*      R         */
1317 	{ SST(0x21, 0x02, SS_RDEF,	/* XXX TBD */
1318 	    "Invalid address for write") },
1319 	/*      R         */
1320 	{ SST(0x21, 0x03, SS_RDEF,	/* XXX TBD */
1321 	    "Invalid write crossing layer jump") },
1322 	/* D              */
1323 	{ SST(0x22, 0x00, SS_FATAL | EINVAL,
1324 	    "Illegal function (use 20 00, 24 00, or 26 00)") },
1325 	/* DTLPWROMAEBKVF */
1326 	{ SST(0x24, 0x00, SS_FATAL | EINVAL,
1327 	    "Invalid field in CDB") },
1328 	/* DTLPWRO AEBKVF */
1329 	{ SST(0x24, 0x01, SS_RDEF,	/* XXX TBD */
1330 	    "CDB decryption error") },
1331 	/*  T             */
1332 	{ SST(0x24, 0x02, SS_RDEF,	/* XXX TBD */
1333 	    "Obsolete") },
1334 	/*  T             */
1335 	{ SST(0x24, 0x03, SS_RDEF,	/* XXX TBD */
1336 	    "Obsolete") },
1337 	/*              F */
1338 	{ SST(0x24, 0x04, SS_RDEF,	/* XXX TBD */
1339 	    "Security audit value frozen") },
1340 	/*              F */
1341 	{ SST(0x24, 0x05, SS_RDEF,	/* XXX TBD */
1342 	    "Security working key frozen") },
1343 	/*              F */
1344 	{ SST(0x24, 0x06, SS_RDEF,	/* XXX TBD */
1345 	    "NONCE not unique") },
1346 	/*              F */
1347 	{ SST(0x24, 0x07, SS_RDEF,	/* XXX TBD */
1348 	    "NONCE timestamp out of range") },
1349 	/* DT   R MAEBKV  */
1350 	{ SST(0x24, 0x08, SS_RDEF,	/* XXX TBD */
1351 	    "Invalid XCDB") },
1352 	/* DTLPWROMAEBKVF */
1353 	{ SST(0x25, 0x00, SS_FATAL | ENXIO,
1354 	    "Logical unit not supported") },
1355 	/* DTLPWROMAEBKVF */
1356 	{ SST(0x26, 0x00, SS_FATAL | EINVAL,
1357 	    "Invalid field in parameter list") },
1358 	/* DTLPWROMAEBKVF */
1359 	{ SST(0x26, 0x01, SS_FATAL | EINVAL,
1360 	    "Parameter not supported") },
1361 	/* DTLPWROMAEBKVF */
1362 	{ SST(0x26, 0x02, SS_FATAL | EINVAL,
1363 	    "Parameter value invalid") },
1364 	/* DTLPWROMAE K   */
1365 	{ SST(0x26, 0x03, SS_FATAL | EINVAL,
1366 	    "Threshold parameters not supported") },
1367 	/* DTLPWROMAEBKVF */
1368 	{ SST(0x26, 0x04, SS_FATAL | EINVAL,
1369 	    "Invalid release of persistent reservation") },
1370 	/* DTLPWRO A BK   */
1371 	{ SST(0x26, 0x05, SS_RDEF,	/* XXX TBD */
1372 	    "Data decryption error") },
1373 	/* DTLPWRO    K   */
1374 	{ SST(0x26, 0x06, SS_RDEF,	/* XXX TBD */
1375 	    "Too many target descriptors") },
1376 	/* DTLPWRO    K   */
1377 	{ SST(0x26, 0x07, SS_RDEF,	/* XXX TBD */
1378 	    "Unsupported target descriptor type code") },
1379 	/* DTLPWRO    K   */
1380 	{ SST(0x26, 0x08, SS_RDEF,	/* XXX TBD */
1381 	    "Too many segment descriptors") },
1382 	/* DTLPWRO    K   */
1383 	{ SST(0x26, 0x09, SS_RDEF,	/* XXX TBD */
1384 	    "Unsupported segment descriptor type code") },
1385 	/* DTLPWRO    K   */
1386 	{ SST(0x26, 0x0A, SS_RDEF,	/* XXX TBD */
1387 	    "Unexpected inexact segment") },
1388 	/* DTLPWRO    K   */
1389 	{ SST(0x26, 0x0B, SS_RDEF,	/* XXX TBD */
1390 	    "Inline data length exceeded") },
1391 	/* DTLPWRO    K   */
1392 	{ SST(0x26, 0x0C, SS_RDEF,	/* XXX TBD */
1393 	    "Invalid operation for copy source or destination") },
1394 	/* DTLPWRO    K   */
1395 	{ SST(0x26, 0x0D, SS_RDEF,	/* XXX TBD */
1396 	    "Copy segment granularity violation") },
1397 	/* DT PWROMAEBK   */
1398 	{ SST(0x26, 0x0E, SS_RDEF,	/* XXX TBD */
1399 	    "Invalid parameter while port is enabled") },
1400 	/*              F */
1401 	{ SST(0x26, 0x0F, SS_RDEF,	/* XXX TBD */
1402 	    "Invalid data-out buffer integrity check value") },
1403 	/*  T             */
1404 	{ SST(0x26, 0x10, SS_RDEF,	/* XXX TBD */
1405 	    "Data decryption key fail limit reached") },
1406 	/*  T             */
1407 	{ SST(0x26, 0x11, SS_RDEF,	/* XXX TBD */
1408 	    "Incomplete key-associated data set") },
1409 	/*  T             */
1410 	{ SST(0x26, 0x12, SS_RDEF,	/* XXX TBD */
1411 	    "Vendor specific key reference not found") },
1412 	/* DT  WRO   BK   */
1413 	{ SST(0x27, 0x00, SS_FATAL | EACCES,
1414 	    "Write protected") },
1415 	/* DT  WRO   BK   */
1416 	{ SST(0x27, 0x01, SS_FATAL | EACCES,
1417 	    "Hardware write protected") },
1418 	/* DT  WRO   BK   */
1419 	{ SST(0x27, 0x02, SS_FATAL | EACCES,
1420 	    "Logical unit software write protected") },
1421 	/*  T   R         */
1422 	{ SST(0x27, 0x03, SS_FATAL | EACCES,
1423 	    "Associated write protect") },
1424 	/*  T   R         */
1425 	{ SST(0x27, 0x04, SS_FATAL | EACCES,
1426 	    "Persistent write protect") },
1427 	/*  T   R         */
1428 	{ SST(0x27, 0x05, SS_FATAL | EACCES,
1429 	    "Permanent write protect") },
1430 	/*      R       F */
1431 	{ SST(0x27, 0x06, SS_RDEF,	/* XXX TBD */
1432 	    "Conditional write protect") },
1433 	/* DTLPWROMAEBKVF */
1434 	{ SST(0x28, 0x00, SS_FATAL | ENXIO,
1435 	    "Not ready to ready change, medium may have changed") },
1436 	/* DT  WROM  B    */
1437 	{ SST(0x28, 0x01, SS_FATAL | ENXIO,
1438 	    "Import or export element accessed") },
1439 	/*      R         */
1440 	{ SST(0x28, 0x02, SS_RDEF,	/* XXX TBD */
1441 	    "Format-layer may have changed") },
1442 	/*        M       */
1443 	{ SST(0x28, 0x03, SS_RDEF,	/* XXX TBD */
1444 	    "Import/export element accessed, medium changed") },
1445 	/*
1446 	 * XXX JGibbs - All of these should use the same errno, but I don't
1447 	 * think ENXIO is the correct choice.  Should we borrow from
1448 	 * the networking errnos?  ECONNRESET anyone?
1449 	 */
1450 	/* DTLPWROMAEBKVF */
1451 	{ SST(0x29, 0x00, SS_FATAL | ENXIO,
1452 	    "Power on, reset, or bus device reset occurred") },
1453 	/* DTLPWROMAEBKVF */
1454 	{ SST(0x29, 0x01, SS_RDEF,
1455 	    "Power on occurred") },
1456 	/* DTLPWROMAEBKVF */
1457 	{ SST(0x29, 0x02, SS_RDEF,
1458 	    "SCSI bus reset occurred") },
1459 	/* DTLPWROMAEBKVF */
1460 	{ SST(0x29, 0x03, SS_RDEF,
1461 	    "Bus device reset function occurred") },
1462 	/* DTLPWROMAEBKVF */
1463 	{ SST(0x29, 0x04, SS_RDEF,
1464 	    "Device internal reset") },
1465 	/* DTLPWROMAEBKVF */
1466 	{ SST(0x29, 0x05, SS_RDEF,
1467 	    "Transceiver mode changed to single-ended") },
1468 	/* DTLPWROMAEBKVF */
1469 	{ SST(0x29, 0x06, SS_RDEF,
1470 	    "Transceiver mode changed to LVD") },
1471 	/* DTLPWROMAEBKVF */
1472 	{ SST(0x29, 0x07, SS_RDEF,	/* XXX TBD */
1473 	    "I_T nexus loss occurred") },
1474 	/* DTL WROMAEBKVF */
1475 	{ SST(0x2A, 0x00, SS_RDEF,
1476 	    "Parameters changed") },
1477 	/* DTL WROMAEBKVF */
1478 	{ SST(0x2A, 0x01, SS_RDEF,
1479 	    "Mode parameters changed") },
1480 	/* DTL WROMAE K   */
1481 	{ SST(0x2A, 0x02, SS_RDEF,
1482 	    "Log parameters changed") },
1483 	/* DTLPWROMAE K   */
1484 	{ SST(0x2A, 0x03, SS_RDEF,
1485 	    "Reservations preempted") },
1486 	/* DTLPWROMAE     */
1487 	{ SST(0x2A, 0x04, SS_RDEF,	/* XXX TBD */
1488 	    "Reservations released") },
1489 	/* DTLPWROMAE     */
1490 	{ SST(0x2A, 0x05, SS_RDEF,	/* XXX TBD */
1491 	    "Registrations preempted") },
1492 	/* DTLPWROMAEBKVF */
1493 	{ SST(0x2A, 0x06, SS_RDEF,	/* XXX TBD */
1494 	    "Asymmetric access state changed") },
1495 	/* DTLPWROMAEBKVF */
1496 	{ SST(0x2A, 0x07, SS_RDEF,	/* XXX TBD */
1497 	    "Implicit asymmetric access state transition failed") },
1498 	/* DT  WROMAEBKVF */
1499 	{ SST(0x2A, 0x08, SS_RDEF,	/* XXX TBD */
1500 	    "Priority changed") },
1501 	/* D              */
1502 	{ SST(0x2A, 0x09, SS_RDEF,	/* XXX TBD */
1503 	    "Capacity data has changed") },
1504 	/* DT             */
1505 	{ SST(0x2A, 0x0A, SS_RDEF,	/* XXX TBD */
1506 	    "Error history I_T nexus cleared") },
1507 	/* DT             */
1508 	{ SST(0x2A, 0x0B, SS_RDEF,	/* XXX TBD */
1509 	    "Error history snapshot released") },
1510 	/*              F */
1511 	{ SST(0x2A, 0x0C, SS_RDEF,	/* XXX TBD */
1512 	    "Error recovery attributes have changed") },
1513 	/*  T             */
1514 	{ SST(0x2A, 0x0D, SS_RDEF,	/* XXX TBD */
1515 	    "Data encryption capabilities changed") },
1516 	/* DT     M E  V  */
1517 	{ SST(0x2A, 0x10, SS_RDEF,	/* XXX TBD */
1518 	    "Timestamp changed") },
1519 	/*  T             */
1520 	{ SST(0x2A, 0x11, SS_RDEF,	/* XXX TBD */
1521 	    "Data encryption parameters changed by another I_T nexus") },
1522 	/*  T             */
1523 	{ SST(0x2A, 0x12, SS_RDEF,	/* XXX TBD */
1524 	    "Data encryption parameters changed by vendor specific event") },
1525 	/*  T             */
1526 	{ SST(0x2A, 0x13, SS_RDEF,	/* XXX TBD */
1527 	    "Data encryption key instance counter has changed") },
1528 	/* DT   R MAEBKV  */
1529 	{ SST(0x2A, 0x14, SS_RDEF,	/* XXX TBD */
1530 	    "SA creation capabilities data has changed") },
1531 	/* DTLPWRO    K   */
1532 	{ SST(0x2B, 0x00, SS_RDEF,
1533 	    "Copy cannot execute since host cannot disconnect") },
1534 	/* DTLPWROMAEBKVF */
1535 	{ SST(0x2C, 0x00, SS_RDEF,
1536 	    "Command sequence error") },
1537 	/*                */
1538 	{ SST(0x2C, 0x01, SS_RDEF,
1539 	    "Too many windows specified") },
1540 	/*                */
1541 	{ SST(0x2C, 0x02, SS_RDEF,
1542 	    "Invalid combination of windows specified") },
1543 	/*      R         */
1544 	{ SST(0x2C, 0x03, SS_RDEF,
1545 	    "Current program area is not empty") },
1546 	/*      R         */
1547 	{ SST(0x2C, 0x04, SS_RDEF,
1548 	    "Current program area is empty") },
1549 	/*           B    */
1550 	{ SST(0x2C, 0x05, SS_RDEF,	/* XXX TBD */
1551 	    "Illegal power condition request") },
1552 	/*      R         */
1553 	{ SST(0x2C, 0x06, SS_RDEF,	/* XXX TBD */
1554 	    "Persistent prevent conflict") },
1555 	/* DTLPWROMAEBKVF */
1556 	{ SST(0x2C, 0x07, SS_RDEF,	/* XXX TBD */
1557 	    "Previous busy status") },
1558 	/* DTLPWROMAEBKVF */
1559 	{ SST(0x2C, 0x08, SS_RDEF,	/* XXX TBD */
1560 	    "Previous task set full status") },
1561 	/* DTLPWROM EBKVF */
1562 	{ SST(0x2C, 0x09, SS_RDEF,	/* XXX TBD */
1563 	    "Previous reservation conflict status") },
1564 	/*              F */
1565 	{ SST(0x2C, 0x0A, SS_RDEF,	/* XXX TBD */
1566 	    "Partition or collection contains user objects") },
1567 	/*  T             */
1568 	{ SST(0x2C, 0x0B, SS_RDEF,	/* XXX TBD */
1569 	    "Not reserved") },
1570 	/*  T             */
1571 	{ SST(0x2D, 0x00, SS_RDEF,
1572 	    "Overwrite error on update in place") },
1573 	/*      R         */
1574 	{ SST(0x2E, 0x00, SS_RDEF,	/* XXX TBD */
1575 	    "Insufficient time for operation") },
1576 	/* DTLPWROMAEBKVF */
1577 	{ SST(0x2F, 0x00, SS_RDEF,
1578 	    "Commands cleared by another initiator") },
1579 	/* D              */
1580 	{ SST(0x2F, 0x01, SS_RDEF,	/* XXX TBD */
1581 	    "Commands cleared by power loss notification") },
1582 	/* DTLPWROMAEBKVF */
1583 	{ SST(0x2F, 0x02, SS_RDEF,	/* XXX TBD */
1584 	    "Commands cleared by device server") },
1585 	/* DT  WROM  BK   */
1586 	{ SST(0x30, 0x00, SS_RDEF,
1587 	    "Incompatible medium installed") },
1588 	/* DT  WRO   BK   */
1589 	{ SST(0x30, 0x01, SS_RDEF,
1590 	    "Cannot read medium - unknown format") },
1591 	/* DT  WRO   BK   */
1592 	{ SST(0x30, 0x02, SS_RDEF,
1593 	    "Cannot read medium - incompatible format") },
1594 	/* DT   R     K   */
1595 	{ SST(0x30, 0x03, SS_RDEF,
1596 	    "Cleaning cartridge installed") },
1597 	/* DT  WRO   BK   */
1598 	{ SST(0x30, 0x04, SS_RDEF,
1599 	    "Cannot write medium - unknown format") },
1600 	/* DT  WRO   BK   */
1601 	{ SST(0x30, 0x05, SS_RDEF,
1602 	    "Cannot write medium - incompatible format") },
1603 	/* DT  WRO   B    */
1604 	{ SST(0x30, 0x06, SS_RDEF,
1605 	    "Cannot format medium - incompatible medium") },
1606 	/* DTL WROMAEBKVF */
1607 	{ SST(0x30, 0x07, SS_RDEF,
1608 	    "Cleaning failure") },
1609 	/*      R         */
1610 	{ SST(0x30, 0x08, SS_RDEF,
1611 	    "Cannot write - application code mismatch") },
1612 	/*      R         */
1613 	{ SST(0x30, 0x09, SS_RDEF,
1614 	    "Current session not fixated for append") },
1615 	/* DT  WRO AEBK   */
1616 	{ SST(0x30, 0x0A, SS_RDEF,	/* XXX TBD */
1617 	    "Cleaning request rejected") },
1618 	/*  T             */
1619 	{ SST(0x30, 0x0C, SS_RDEF,	/* XXX TBD */
1620 	    "WORM medium - overwrite attempted") },
1621 	/*  T             */
1622 	{ SST(0x30, 0x0D, SS_RDEF,	/* XXX TBD */
1623 	    "WORM medium - integrity check") },
1624 	/*      R         */
1625 	{ SST(0x30, 0x10, SS_RDEF,	/* XXX TBD */
1626 	    "Medium not formatted") },
1627 	/*        M       */
1628 	{ SST(0x30, 0x11, SS_RDEF,	/* XXX TBD */
1629 	    "Incompatible volume type") },
1630 	/*        M       */
1631 	{ SST(0x30, 0x12, SS_RDEF,	/* XXX TBD */
1632 	    "Incompatible volume qualifier") },
1633 	/* DT  WRO   BK   */
1634 	{ SST(0x31, 0x00, SS_RDEF,
1635 	    "Medium format corrupted") },
1636 	/* D L  RO   B    */
1637 	{ SST(0x31, 0x01, SS_RDEF,
1638 	    "Format command failed") },
1639 	/*      R         */
1640 	{ SST(0x31, 0x02, SS_RDEF,	/* XXX TBD */
1641 	    "Zoned formatting failed due to spare linking") },
1642 	/* D   W O   BK   */
1643 	{ SST(0x32, 0x00, SS_RDEF,
1644 	    "No defect spare location available") },
1645 	/* D   W O   BK   */
1646 	{ SST(0x32, 0x01, SS_RDEF,
1647 	    "Defect list update failure") },
1648 	/*  T             */
1649 	{ SST(0x33, 0x00, SS_RDEF,
1650 	    "Tape length error") },
1651 	/* DTLPWROMAEBKVF */
1652 	{ SST(0x34, 0x00, SS_RDEF,
1653 	    "Enclosure failure") },
1654 	/* DTLPWROMAEBKVF */
1655 	{ SST(0x35, 0x00, SS_RDEF,
1656 	    "Enclosure services failure") },
1657 	/* DTLPWROMAEBKVF */
1658 	{ SST(0x35, 0x01, SS_RDEF,
1659 	    "Unsupported enclosure function") },
1660 	/* DTLPWROMAEBKVF */
1661 	{ SST(0x35, 0x02, SS_RDEF,
1662 	    "Enclosure services unavailable") },
1663 	/* DTLPWROMAEBKVF */
1664 	{ SST(0x35, 0x03, SS_RDEF,
1665 	    "Enclosure services transfer failure") },
1666 	/* DTLPWROMAEBKVF */
1667 	{ SST(0x35, 0x04, SS_RDEF,
1668 	    "Enclosure services transfer refused") },
1669 	/* DTL WROMAEBKVF */
1670 	{ SST(0x35, 0x05, SS_RDEF,	/* XXX TBD */
1671 	    "Enclosure services checksum error") },
1672 	/*   L            */
1673 	{ SST(0x36, 0x00, SS_RDEF,
1674 	    "Ribbon, ink, or toner failure") },
1675 	/* DTL WROMAEBKVF */
1676 	{ SST(0x37, 0x00, SS_RDEF,
1677 	    "Rounded parameter") },
1678 	/*           B    */
1679 	{ SST(0x38, 0x00, SS_RDEF,	/* XXX TBD */
1680 	    "Event status notification") },
1681 	/*           B    */
1682 	{ SST(0x38, 0x02, SS_RDEF,	/* XXX TBD */
1683 	    "ESN - power management class event") },
1684 	/*           B    */
1685 	{ SST(0x38, 0x04, SS_RDEF,	/* XXX TBD */
1686 	    "ESN - media class event") },
1687 	/*           B    */
1688 	{ SST(0x38, 0x06, SS_RDEF,	/* XXX TBD */
1689 	    "ESN - device busy class event") },
1690 	/* DTL WROMAE K   */
1691 	{ SST(0x39, 0x00, SS_RDEF,
1692 	    "Saving parameters not supported") },
1693 	/* DTL WROM  BK   */
1694 	{ SST(0x3A, 0x00, SS_FATAL | ENXIO,
1695 	    "Medium not present") },
1696 	/* DT  WROM  BK   */
1697 	{ SST(0x3A, 0x01, SS_FATAL | ENXIO,
1698 	    "Medium not present - tray closed") },
1699 	/* DT  WROM  BK   */
1700 	{ SST(0x3A, 0x02, SS_FATAL | ENXIO,
1701 	    "Medium not present - tray open") },
1702 	/* DT  WROM  B    */
1703 	{ SST(0x3A, 0x03, SS_RDEF,	/* XXX TBD */
1704 	    "Medium not present - loadable") },
1705 	/* DT  WRO   B    */
1706 	{ SST(0x3A, 0x04, SS_RDEF,	/* XXX TBD */
1707 	    "Medium not present - medium auxiliary memory accessible") },
1708 	/*  TL            */
1709 	{ SST(0x3B, 0x00, SS_RDEF,
1710 	    "Sequential positioning error") },
1711 	/*  T             */
1712 	{ SST(0x3B, 0x01, SS_RDEF,
1713 	    "Tape position error at beginning-of-medium") },
1714 	/*  T             */
1715 	{ SST(0x3B, 0x02, SS_RDEF,
1716 	    "Tape position error at end-of-medium") },
1717 	/*   L            */
1718 	{ SST(0x3B, 0x03, SS_RDEF,
1719 	    "Tape or electronic vertical forms unit not ready") },
1720 	/*   L            */
1721 	{ SST(0x3B, 0x04, SS_RDEF,
1722 	    "Slew failure") },
1723 	/*   L            */
1724 	{ SST(0x3B, 0x05, SS_RDEF,
1725 	    "Paper jam") },
1726 	/*   L            */
1727 	{ SST(0x3B, 0x06, SS_RDEF,
1728 	    "Failed to sense top-of-form") },
1729 	/*   L            */
1730 	{ SST(0x3B, 0x07, SS_RDEF,
1731 	    "Failed to sense bottom-of-form") },
1732 	/*  T             */
1733 	{ SST(0x3B, 0x08, SS_RDEF,
1734 	    "Reposition error") },
1735 	/*                */
1736 	{ SST(0x3B, 0x09, SS_RDEF,
1737 	    "Read past end of medium") },
1738 	/*                */
1739 	{ SST(0x3B, 0x0A, SS_RDEF,
1740 	    "Read past beginning of medium") },
1741 	/*                */
1742 	{ SST(0x3B, 0x0B, SS_RDEF,
1743 	    "Position past end of medium") },
1744 	/*  T             */
1745 	{ SST(0x3B, 0x0C, SS_RDEF,
1746 	    "Position past beginning of medium") },
1747 	/* DT  WROM  BK   */
1748 	{ SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
1749 	    "Medium destination element full") },
1750 	/* DT  WROM  BK   */
1751 	{ SST(0x3B, 0x0E, SS_RDEF,
1752 	    "Medium source element empty") },
1753 	/*      R         */
1754 	{ SST(0x3B, 0x0F, SS_RDEF,
1755 	    "End of medium reached") },
1756 	/* DT  WROM  BK   */
1757 	{ SST(0x3B, 0x11, SS_RDEF,
1758 	    "Medium magazine not accessible") },
1759 	/* DT  WROM  BK   */
1760 	{ SST(0x3B, 0x12, SS_RDEF,
1761 	    "Medium magazine removed") },
1762 	/* DT  WROM  BK   */
1763 	{ SST(0x3B, 0x13, SS_RDEF,
1764 	    "Medium magazine inserted") },
1765 	/* DT  WROM  BK   */
1766 	{ SST(0x3B, 0x14, SS_RDEF,
1767 	    "Medium magazine locked") },
1768 	/* DT  WROM  BK   */
1769 	{ SST(0x3B, 0x15, SS_RDEF,
1770 	    "Medium magazine unlocked") },
1771 	/*      R         */
1772 	{ SST(0x3B, 0x16, SS_RDEF,	/* XXX TBD */
1773 	    "Mechanical positioning or changer error") },
1774 	/*              F */
1775 	{ SST(0x3B, 0x17, SS_RDEF,	/* XXX TBD */
1776 	    "Read past end of user object") },
1777 	/*        M       */
1778 	{ SST(0x3B, 0x18, SS_RDEF,	/* XXX TBD */
1779 	    "Element disabled") },
1780 	/*        M       */
1781 	{ SST(0x3B, 0x19, SS_RDEF,	/* XXX TBD */
1782 	    "Element enabled") },
1783 	/*        M       */
1784 	{ SST(0x3B, 0x1A, SS_RDEF,	/* XXX TBD */
1785 	    "Data transfer device removed") },
1786 	/*        M       */
1787 	{ SST(0x3B, 0x1B, SS_RDEF,	/* XXX TBD */
1788 	    "Data transfer device inserted") },
1789 	/* DTLPWROMAE K   */
1790 	{ SST(0x3D, 0x00, SS_RDEF,
1791 	    "Invalid bits in IDENTIFY message") },
1792 	/* DTLPWROMAEBKVF */
1793 	{ SST(0x3E, 0x00, SS_RDEF,
1794 	    "Logical unit has not self-configured yet") },
1795 	/* DTLPWROMAEBKVF */
1796 	{ SST(0x3E, 0x01, SS_RDEF,
1797 	    "Logical unit failure") },
1798 	/* DTLPWROMAEBKVF */
1799 	{ SST(0x3E, 0x02, SS_RDEF,
1800 	    "Timeout on logical unit") },
1801 	/* DTLPWROMAEBKVF */
1802 	{ SST(0x3E, 0x03, SS_RDEF,	/* XXX TBD */
1803 	    "Logical unit failed self-test") },
1804 	/* DTLPWROMAEBKVF */
1805 	{ SST(0x3E, 0x04, SS_RDEF,	/* XXX TBD */
1806 	    "Logical unit unable to update self-test log") },
1807 	/* DTLPWROMAEBKVF */
1808 	{ SST(0x3F, 0x00, SS_RDEF,
1809 	    "Target operating conditions have changed") },
1810 	/* DTLPWROMAEBKVF */
1811 	{ SST(0x3F, 0x01, SS_RDEF,
1812 	    "Microcode has been changed") },
1813 	/* DTLPWROM  BK   */
1814 	{ SST(0x3F, 0x02, SS_RDEF,
1815 	    "Changed operating definition") },
1816 	/* DTLPWROMAEBKVF */
1817 	{ SST(0x3F, 0x03, SS_RDEF,
1818 	    "INQUIRY data has changed") },
1819 	/* DT  WROMAEBK   */
1820 	{ SST(0x3F, 0x04, SS_RDEF,
1821 	    "Component device attached") },
1822 	/* DT  WROMAEBK   */
1823 	{ SST(0x3F, 0x05, SS_RDEF,
1824 	    "Device identifier changed") },
1825 	/* DT  WROMAEB    */
1826 	{ SST(0x3F, 0x06, SS_RDEF,
1827 	    "Redundancy group created or modified") },
1828 	/* DT  WROMAEB    */
1829 	{ SST(0x3F, 0x07, SS_RDEF,
1830 	    "Redundancy group deleted") },
1831 	/* DT  WROMAEB    */
1832 	{ SST(0x3F, 0x08, SS_RDEF,
1833 	    "Spare created or modified") },
1834 	/* DT  WROMAEB    */
1835 	{ SST(0x3F, 0x09, SS_RDEF,
1836 	    "Spare deleted") },
1837 	/* DT  WROMAEBK   */
1838 	{ SST(0x3F, 0x0A, SS_RDEF,
1839 	    "Volume set created or modified") },
1840 	/* DT  WROMAEBK   */
1841 	{ SST(0x3F, 0x0B, SS_RDEF,
1842 	    "Volume set deleted") },
1843 	/* DT  WROMAEBK   */
1844 	{ SST(0x3F, 0x0C, SS_RDEF,
1845 	    "Volume set deassigned") },
1846 	/* DT  WROMAEBK   */
1847 	{ SST(0x3F, 0x0D, SS_RDEF,
1848 	    "Volume set reassigned") },
1849 	/* DTLPWROMAE     */
1850 	{ SST(0x3F, 0x0E, SS_RDEF,	/* XXX TBD */
1851 	    "Reported LUNs data has changed") },
1852 	/* DTLPWROMAEBKVF */
1853 	{ SST(0x3F, 0x0F, SS_RDEF,	/* XXX TBD */
1854 	    "Echo buffer overwritten") },
1855 	/* DT  WROM  B    */
1856 	{ SST(0x3F, 0x10, SS_RDEF,	/* XXX TBD */
1857 	    "Medium loadable") },
1858 	/* DT  WROM  B    */
1859 	{ SST(0x3F, 0x11, SS_RDEF,	/* XXX TBD */
1860 	    "Medium auxiliary memory accessible") },
1861 	/* DTLPWR MAEBK F */
1862 	{ SST(0x3F, 0x12, SS_RDEF,	/* XXX TBD */
1863 	    "iSCSI IP address added") },
1864 	/* DTLPWR MAEBK F */
1865 	{ SST(0x3F, 0x13, SS_RDEF,	/* XXX TBD */
1866 	    "iSCSI IP address removed") },
1867 	/* DTLPWR MAEBK F */
1868 	{ SST(0x3F, 0x14, SS_RDEF,	/* XXX TBD */
1869 	    "iSCSI IP address changed") },
1870 	/* D              */
1871 	{ SST(0x40, 0x00, SS_RDEF,
1872 	    "RAM failure") },		/* deprecated - use 40 NN instead */
1873 	/* DTLPWROMAEBKVF */
1874 	{ SST(0x40, 0x80, SS_RDEF,
1875 	    "Diagnostic failure: ASCQ = Component ID") },
1876 	/* DTLPWROMAEBKVF */
1877 	{ SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
1878 	    NULL) },			/* Range 0x80->0xFF */
1879 	/* D              */
1880 	{ SST(0x41, 0x00, SS_RDEF,
1881 	    "Data path failure") },	/* deprecated - use 40 NN instead */
1882 	/* D              */
1883 	{ SST(0x42, 0x00, SS_RDEF,
1884 	    "Power-on or self-test failure") },
1885 					/* deprecated - use 40 NN instead */
1886 	/* DTLPWROMAEBKVF */
1887 	{ SST(0x43, 0x00, SS_RDEF,
1888 	    "Message error") },
1889 	/* DTLPWROMAEBKVF */
1890 	{ SST(0x44, 0x00, SS_RDEF,
1891 	    "Internal target failure") },
1892 	/* DT        B    */
1893 	{ SST(0x44, 0x71, SS_RDEF,	/* XXX TBD */
1894 	    "ATA device failed set features") },
1895 	/* DTLPWROMAEBKVF */
1896 	{ SST(0x45, 0x00, SS_RDEF,
1897 	    "Select or reselect failure") },
1898 	/* DTLPWROM  BK   */
1899 	{ SST(0x46, 0x00, SS_RDEF,
1900 	    "Unsuccessful soft reset") },
1901 	/* DTLPWROMAEBKVF */
1902 	{ SST(0x47, 0x00, SS_RDEF,
1903 	    "SCSI parity error") },
1904 	/* DTLPWROMAEBKVF */
1905 	{ SST(0x47, 0x01, SS_RDEF,	/* XXX TBD */
1906 	    "Data phase CRC error detected") },
1907 	/* DTLPWROMAEBKVF */
1908 	{ SST(0x47, 0x02, SS_RDEF,	/* XXX TBD */
1909 	    "SCSI parity error detected during ST data phase") },
1910 	/* DTLPWROMAEBKVF */
1911 	{ SST(0x47, 0x03, SS_RDEF,	/* XXX TBD */
1912 	    "Information unit iuCRC error detected") },
1913 	/* DTLPWROMAEBKVF */
1914 	{ SST(0x47, 0x04, SS_RDEF,	/* XXX TBD */
1915 	    "Asynchronous information protection error detected") },
1916 	/* DTLPWROMAEBKVF */
1917 	{ SST(0x47, 0x05, SS_RDEF,	/* XXX TBD */
1918 	    "Protocol service CRC error") },
1919 	/* DT     MAEBKVF */
1920 	{ SST(0x47, 0x06, SS_RDEF,	/* XXX TBD */
1921 	    "PHY test function in progress") },
1922 	/* DT PWROMAEBK   */
1923 	{ SST(0x47, 0x7F, SS_RDEF,	/* XXX TBD */
1924 	    "Some commands cleared by iSCSI protocol event") },
1925 	/* DTLPWROMAEBKVF */
1926 	{ SST(0x48, 0x00, SS_RDEF,
1927 	    "Initiator detected error message received") },
1928 	/* DTLPWROMAEBKVF */
1929 	{ SST(0x49, 0x00, SS_RDEF,
1930 	    "Invalid message error") },
1931 	/* DTLPWROMAEBKVF */
1932 	{ SST(0x4A, 0x00, SS_RDEF,
1933 	    "Command phase error") },
1934 	/* DTLPWROMAEBKVF */
1935 	{ SST(0x4B, 0x00, SS_RDEF,
1936 	    "Data phase error") },
1937 	/* DT PWROMAEBK   */
1938 	{ SST(0x4B, 0x01, SS_RDEF,	/* XXX TBD */
1939 	    "Invalid target port transfer tag received") },
1940 	/* DT PWROMAEBK   */
1941 	{ SST(0x4B, 0x02, SS_RDEF,	/* XXX TBD */
1942 	    "Too much write data") },
1943 	/* DT PWROMAEBK   */
1944 	{ SST(0x4B, 0x03, SS_RDEF,	/* XXX TBD */
1945 	    "ACK/NAK timeout") },
1946 	/* DT PWROMAEBK   */
1947 	{ SST(0x4B, 0x04, SS_RDEF,	/* XXX TBD */
1948 	    "NAK received") },
1949 	/* DT PWROMAEBK   */
1950 	{ SST(0x4B, 0x05, SS_RDEF,	/* XXX TBD */
1951 	    "Data offset error") },
1952 	/* DT PWROMAEBK   */
1953 	{ SST(0x4B, 0x06, SS_RDEF,	/* XXX TBD */
1954 	    "Initiator response timeout") },
1955 	/* DTLPWROMAEBKVF */
1956 	{ SST(0x4C, 0x00, SS_RDEF,
1957 	    "Logical unit failed self-configuration") },
1958 	/* DTLPWROMAEBKVF */
1959 	{ SST(0x4D, 0x00, SS_RDEF,
1960 	    "Tagged overlapped commands: ASCQ = Queue tag ID") },
1961 	/* DTLPWROMAEBKVF */
1962 	{ SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
1963 	    NULL) },			/* Range 0x00->0xFF */
1964 	/* DTLPWROMAEBKVF */
1965 	{ SST(0x4E, 0x00, SS_RDEF,
1966 	    "Overlapped commands attempted") },
1967 	/*  T             */
1968 	{ SST(0x50, 0x00, SS_RDEF,
1969 	    "Write append error") },
1970 	/*  T             */
1971 	{ SST(0x50, 0x01, SS_RDEF,
1972 	    "Write append position error") },
1973 	/*  T             */
1974 	{ SST(0x50, 0x02, SS_RDEF,
1975 	    "Position error related to timing") },
1976 	/*  T   RO        */
1977 	{ SST(0x51, 0x00, SS_RDEF,
1978 	    "Erase failure") },
1979 	/*      R         */
1980 	{ SST(0x51, 0x01, SS_RDEF,	/* XXX TBD */
1981 	    "Erase failure - incomplete erase operation detected") },
1982 	/*  T             */
1983 	{ SST(0x52, 0x00, SS_RDEF,
1984 	    "Cartridge fault") },
1985 	/* DTL WROM  BK   */
1986 	{ SST(0x53, 0x00, SS_RDEF,
1987 	    "Media load or eject failed") },
1988 	/*  T             */
1989 	{ SST(0x53, 0x01, SS_RDEF,
1990 	    "Unload tape failure") },
1991 	/* DT  WROM  BK   */
1992 	{ SST(0x53, 0x02, SS_RDEF,
1993 	    "Medium removal prevented") },
1994 	/*        M       */
1995 	{ SST(0x53, 0x03, SS_RDEF,	/* XXX TBD */
1996 	    "Medium removal prevented by data transfer element") },
1997 	/*  T             */
1998 	{ SST(0x53, 0x04, SS_RDEF,	/* XXX TBD */
1999 	    "Medium thread or unthread failure") },
2000 	/*    P           */
2001 	{ SST(0x54, 0x00, SS_RDEF,
2002 	    "SCSI to host system interface failure") },
2003 	/*    P           */
2004 	{ SST(0x55, 0x00, SS_RDEF,
2005 	    "System resource failure") },
2006 	/* D     O   BK   */
2007 	{ SST(0x55, 0x01, SS_FATAL | ENOSPC,
2008 	    "System buffer full") },
2009 	/* DTLPWROMAE K   */
2010 	{ SST(0x55, 0x02, SS_RDEF,	/* XXX TBD */
2011 	    "Insufficient reservation resources") },
2012 	/* DTLPWROMAE K   */
2013 	{ SST(0x55, 0x03, SS_RDEF,	/* XXX TBD */
2014 	    "Insufficient resources") },
2015 	/* DTLPWROMAE K   */
2016 	{ SST(0x55, 0x04, SS_RDEF,	/* XXX TBD */
2017 	    "Insufficient registration resources") },
2018 	/* DT PWROMAEBK   */
2019 	{ SST(0x55, 0x05, SS_RDEF,	/* XXX TBD */
2020 	    "Insufficient access control resources") },
2021 	/* DT  WROM  B    */
2022 	{ SST(0x55, 0x06, SS_RDEF,	/* XXX TBD */
2023 	    "Auxiliary memory out of space") },
2024 	/*              F */
2025 	{ SST(0x55, 0x07, SS_RDEF,	/* XXX TBD */
2026 	    "Quota error") },
2027 	/*  T             */
2028 	{ SST(0x55, 0x08, SS_RDEF,	/* XXX TBD */
2029 	    "Maximum number of supplemental decryption keys exceeded") },
2030 	/*        M       */
2031 	{ SST(0x55, 0x09, SS_RDEF,	/* XXX TBD */
2032 	    "Medium auxiliary memory not accessible") },
2033 	/*        M       */
2034 	{ SST(0x55, 0x0A, SS_RDEF,	/* XXX TBD */
2035 	    "Data currently unavailable") },
2036 	/*      R         */
2037 	{ SST(0x57, 0x00, SS_RDEF,
2038 	    "Unable to recover table-of-contents") },
2039 	/*       O        */
2040 	{ SST(0x58, 0x00, SS_RDEF,
2041 	    "Generation does not exist") },
2042 	/*       O        */
2043 	{ SST(0x59, 0x00, SS_RDEF,
2044 	    "Updated block read") },
2045 	/* DTLPWRO   BK   */
2046 	{ SST(0x5A, 0x00, SS_RDEF,
2047 	    "Operator request or state change input") },
2048 	/* DT  WROM  BK   */
2049 	{ SST(0x5A, 0x01, SS_RDEF,
2050 	    "Operator medium removal request") },
2051 	/* DT  WRO A BK   */
2052 	{ SST(0x5A, 0x02, SS_RDEF,
2053 	    "Operator selected write protect") },
2054 	/* DT  WRO A BK   */
2055 	{ SST(0x5A, 0x03, SS_RDEF,
2056 	    "Operator selected write permit") },
2057 	/* DTLPWROM   K   */
2058 	{ SST(0x5B, 0x00, SS_RDEF,
2059 	    "Log exception") },
2060 	/* DTLPWROM   K   */
2061 	{ SST(0x5B, 0x01, SS_RDEF,
2062 	    "Threshold condition met") },
2063 	/* DTLPWROM   K   */
2064 	{ SST(0x5B, 0x02, SS_RDEF,
2065 	    "Log counter at maximum") },
2066 	/* DTLPWROM   K   */
2067 	{ SST(0x5B, 0x03, SS_RDEF,
2068 	    "Log list codes exhausted") },
2069 	/* D     O        */
2070 	{ SST(0x5C, 0x00, SS_RDEF,
2071 	    "RPL status change") },
2072 	/* D     O        */
2073 	{ SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2074 	    "Spindles synchronized") },
2075 	/* D     O        */
2076 	{ SST(0x5C, 0x02, SS_RDEF,
2077 	    "Spindles not synchronized") },
2078 	/* DTLPWROMAEBKVF */
2079 	{ SST(0x5D, 0x00, SS_RDEF,
2080 	    "Failure prediction threshold exceeded") },
2081 	/*      R    B    */
2082 	{ SST(0x5D, 0x01, SS_RDEF,	/* XXX TBD */
2083 	    "Media failure prediction threshold exceeded") },
2084 	/*      R         */
2085 	{ SST(0x5D, 0x02, SS_RDEF,	/* XXX TBD */
2086 	    "Logical unit failure prediction threshold exceeded") },
2087 	/*      R         */
2088 	{ SST(0x5D, 0x03, SS_RDEF,	/* XXX TBD */
2089 	    "Spare area exhaustion prediction threshold exceeded") },
2090 	/* D         B    */
2091 	{ SST(0x5D, 0x10, SS_RDEF,	/* XXX TBD */
2092 	    "Hardware impending failure general hard drive failure") },
2093 	/* D         B    */
2094 	{ SST(0x5D, 0x11, SS_RDEF,	/* XXX TBD */
2095 	    "Hardware impending failure drive error rate too high") },
2096 	/* D         B    */
2097 	{ SST(0x5D, 0x12, SS_RDEF,	/* XXX TBD */
2098 	    "Hardware impending failure data error rate too high") },
2099 	/* D         B    */
2100 	{ SST(0x5D, 0x13, SS_RDEF,	/* XXX TBD */
2101 	    "Hardware impending failure seek error rate too high") },
2102 	/* D         B    */
2103 	{ SST(0x5D, 0x14, SS_RDEF,	/* XXX TBD */
2104 	    "Hardware impending failure too many block reassigns") },
2105 	/* D         B    */
2106 	{ SST(0x5D, 0x15, SS_RDEF,	/* XXX TBD */
2107 	    "Hardware impending failure access times too high") },
2108 	/* D         B    */
2109 	{ SST(0x5D, 0x16, SS_RDEF,	/* XXX TBD */
2110 	    "Hardware impending failure start unit times too high") },
2111 	/* D         B    */
2112 	{ SST(0x5D, 0x17, SS_RDEF,	/* XXX TBD */
2113 	    "Hardware impending failure channel parametrics") },
2114 	/* D         B    */
2115 	{ SST(0x5D, 0x18, SS_RDEF,	/* XXX TBD */
2116 	    "Hardware impending failure controller detected") },
2117 	/* D         B    */
2118 	{ SST(0x5D, 0x19, SS_RDEF,	/* XXX TBD */
2119 	    "Hardware impending failure throughput performance") },
2120 	/* D         B    */
2121 	{ SST(0x5D, 0x1A, SS_RDEF,	/* XXX TBD */
2122 	    "Hardware impending failure seek time performance") },
2123 	/* D         B    */
2124 	{ SST(0x5D, 0x1B, SS_RDEF,	/* XXX TBD */
2125 	    "Hardware impending failure spin-up retry count") },
2126 	/* D         B    */
2127 	{ SST(0x5D, 0x1C, SS_RDEF,	/* XXX TBD */
2128 	    "Hardware impending failure drive calibration retry count") },
2129 	/* D         B    */
2130 	{ SST(0x5D, 0x20, SS_RDEF,	/* XXX TBD */
2131 	    "Controller impending failure general hard drive failure") },
2132 	/* D         B    */
2133 	{ SST(0x5D, 0x21, SS_RDEF,	/* XXX TBD */
2134 	    "Controller impending failure drive error rate too high") },
2135 	/* D         B    */
2136 	{ SST(0x5D, 0x22, SS_RDEF,	/* XXX TBD */
2137 	    "Controller impending failure data error rate too high") },
2138 	/* D         B    */
2139 	{ SST(0x5D, 0x23, SS_RDEF,	/* XXX TBD */
2140 	    "Controller impending failure seek error rate too high") },
2141 	/* D         B    */
2142 	{ SST(0x5D, 0x24, SS_RDEF,	/* XXX TBD */
2143 	    "Controller impending failure too many block reassigns") },
2144 	/* D         B    */
2145 	{ SST(0x5D, 0x25, SS_RDEF,	/* XXX TBD */
2146 	    "Controller impending failure access times too high") },
2147 	/* D         B    */
2148 	{ SST(0x5D, 0x26, SS_RDEF,	/* XXX TBD */
2149 	    "Controller impending failure start unit times too high") },
2150 	/* D         B    */
2151 	{ SST(0x5D, 0x27, SS_RDEF,	/* XXX TBD */
2152 	    "Controller impending failure channel parametrics") },
2153 	/* D         B    */
2154 	{ SST(0x5D, 0x28, SS_RDEF,	/* XXX TBD */
2155 	    "Controller impending failure controller detected") },
2156 	/* D         B    */
2157 	{ SST(0x5D, 0x29, SS_RDEF,	/* XXX TBD */
2158 	    "Controller impending failure throughput performance") },
2159 	/* D         B    */
2160 	{ SST(0x5D, 0x2A, SS_RDEF,	/* XXX TBD */
2161 	    "Controller impending failure seek time performance") },
2162 	/* D         B    */
2163 	{ SST(0x5D, 0x2B, SS_RDEF,	/* XXX TBD */
2164 	    "Controller impending failure spin-up retry count") },
2165 	/* D         B    */
2166 	{ SST(0x5D, 0x2C, SS_RDEF,	/* XXX TBD */
2167 	    "Controller impending failure drive calibration retry count") },
2168 	/* D         B    */
2169 	{ SST(0x5D, 0x30, SS_RDEF,	/* XXX TBD */
2170 	    "Data channel impending failure general hard drive failure") },
2171 	/* D         B    */
2172 	{ SST(0x5D, 0x31, SS_RDEF,	/* XXX TBD */
2173 	    "Data channel impending failure drive error rate too high") },
2174 	/* D         B    */
2175 	{ SST(0x5D, 0x32, SS_RDEF,	/* XXX TBD */
2176 	    "Data channel impending failure data error rate too high") },
2177 	/* D         B    */
2178 	{ SST(0x5D, 0x33, SS_RDEF,	/* XXX TBD */
2179 	    "Data channel impending failure seek error rate too high") },
2180 	/* D         B    */
2181 	{ SST(0x5D, 0x34, SS_RDEF,	/* XXX TBD */
2182 	    "Data channel impending failure too many block reassigns") },
2183 	/* D         B    */
2184 	{ SST(0x5D, 0x35, SS_RDEF,	/* XXX TBD */
2185 	    "Data channel impending failure access times too high") },
2186 	/* D         B    */
2187 	{ SST(0x5D, 0x36, SS_RDEF,	/* XXX TBD */
2188 	    "Data channel impending failure start unit times too high") },
2189 	/* D         B    */
2190 	{ SST(0x5D, 0x37, SS_RDEF,	/* XXX TBD */
2191 	    "Data channel impending failure channel parametrics") },
2192 	/* D         B    */
2193 	{ SST(0x5D, 0x38, SS_RDEF,	/* XXX TBD */
2194 	    "Data channel impending failure controller detected") },
2195 	/* D         B    */
2196 	{ SST(0x5D, 0x39, SS_RDEF,	/* XXX TBD */
2197 	    "Data channel impending failure throughput performance") },
2198 	/* D         B    */
2199 	{ SST(0x5D, 0x3A, SS_RDEF,	/* XXX TBD */
2200 	    "Data channel impending failure seek time performance") },
2201 	/* D         B    */
2202 	{ SST(0x5D, 0x3B, SS_RDEF,	/* XXX TBD */
2203 	    "Data channel impending failure spin-up retry count") },
2204 	/* D         B    */
2205 	{ SST(0x5D, 0x3C, SS_RDEF,	/* XXX TBD */
2206 	    "Data channel impending failure drive calibration retry count") },
2207 	/* D         B    */
2208 	{ SST(0x5D, 0x40, SS_RDEF,	/* XXX TBD */
2209 	    "Servo impending failure general hard drive failure") },
2210 	/* D         B    */
2211 	{ SST(0x5D, 0x41, SS_RDEF,	/* XXX TBD */
2212 	    "Servo impending failure drive error rate too high") },
2213 	/* D         B    */
2214 	{ SST(0x5D, 0x42, SS_RDEF,	/* XXX TBD */
2215 	    "Servo impending failure data error rate too high") },
2216 	/* D         B    */
2217 	{ SST(0x5D, 0x43, SS_RDEF,	/* XXX TBD */
2218 	    "Servo impending failure seek error rate too high") },
2219 	/* D         B    */
2220 	{ SST(0x5D, 0x44, SS_RDEF,	/* XXX TBD */
2221 	    "Servo impending failure too many block reassigns") },
2222 	/* D         B    */
2223 	{ SST(0x5D, 0x45, SS_RDEF,	/* XXX TBD */
2224 	    "Servo impending failure access times too high") },
2225 	/* D         B    */
2226 	{ SST(0x5D, 0x46, SS_RDEF,	/* XXX TBD */
2227 	    "Servo impending failure start unit times too high") },
2228 	/* D         B    */
2229 	{ SST(0x5D, 0x47, SS_RDEF,	/* XXX TBD */
2230 	    "Servo impending failure channel parametrics") },
2231 	/* D         B    */
2232 	{ SST(0x5D, 0x48, SS_RDEF,	/* XXX TBD */
2233 	    "Servo impending failure controller detected") },
2234 	/* D         B    */
2235 	{ SST(0x5D, 0x49, SS_RDEF,	/* XXX TBD */
2236 	    "Servo impending failure throughput performance") },
2237 	/* D         B    */
2238 	{ SST(0x5D, 0x4A, SS_RDEF,	/* XXX TBD */
2239 	    "Servo impending failure seek time performance") },
2240 	/* D         B    */
2241 	{ SST(0x5D, 0x4B, SS_RDEF,	/* XXX TBD */
2242 	    "Servo impending failure spin-up retry count") },
2243 	/* D         B    */
2244 	{ SST(0x5D, 0x4C, SS_RDEF,	/* XXX TBD */
2245 	    "Servo impending failure drive calibration retry count") },
2246 	/* D         B    */
2247 	{ SST(0x5D, 0x50, SS_RDEF,	/* XXX TBD */
2248 	    "Spindle impending failure general hard drive failure") },
2249 	/* D         B    */
2250 	{ SST(0x5D, 0x51, SS_RDEF,	/* XXX TBD */
2251 	    "Spindle impending failure drive error rate too high") },
2252 	/* D         B    */
2253 	{ SST(0x5D, 0x52, SS_RDEF,	/* XXX TBD */
2254 	    "Spindle impending failure data error rate too high") },
2255 	/* D         B    */
2256 	{ SST(0x5D, 0x53, SS_RDEF,	/* XXX TBD */
2257 	    "Spindle impending failure seek error rate too high") },
2258 	/* D         B    */
2259 	{ SST(0x5D, 0x54, SS_RDEF,	/* XXX TBD */
2260 	    "Spindle impending failure too many block reassigns") },
2261 	/* D         B    */
2262 	{ SST(0x5D, 0x55, SS_RDEF,	/* XXX TBD */
2263 	    "Spindle impending failure access times too high") },
2264 	/* D         B    */
2265 	{ SST(0x5D, 0x56, SS_RDEF,	/* XXX TBD */
2266 	    "Spindle impending failure start unit times too high") },
2267 	/* D         B    */
2268 	{ SST(0x5D, 0x57, SS_RDEF,	/* XXX TBD */
2269 	    "Spindle impending failure channel parametrics") },
2270 	/* D         B    */
2271 	{ SST(0x5D, 0x58, SS_RDEF,	/* XXX TBD */
2272 	    "Spindle impending failure controller detected") },
2273 	/* D         B    */
2274 	{ SST(0x5D, 0x59, SS_RDEF,	/* XXX TBD */
2275 	    "Spindle impending failure throughput performance") },
2276 	/* D         B    */
2277 	{ SST(0x5D, 0x5A, SS_RDEF,	/* XXX TBD */
2278 	    "Spindle impending failure seek time performance") },
2279 	/* D         B    */
2280 	{ SST(0x5D, 0x5B, SS_RDEF,	/* XXX TBD */
2281 	    "Spindle impending failure spin-up retry count") },
2282 	/* D         B    */
2283 	{ SST(0x5D, 0x5C, SS_RDEF,	/* XXX TBD */
2284 	    "Spindle impending failure drive calibration retry count") },
2285 	/* D         B    */
2286 	{ SST(0x5D, 0x60, SS_RDEF,	/* XXX TBD */
2287 	    "Firmware impending failure general hard drive failure") },
2288 	/* D         B    */
2289 	{ SST(0x5D, 0x61, SS_RDEF,	/* XXX TBD */
2290 	    "Firmware impending failure drive error rate too high") },
2291 	/* D         B    */
2292 	{ SST(0x5D, 0x62, SS_RDEF,	/* XXX TBD */
2293 	    "Firmware impending failure data error rate too high") },
2294 	/* D         B    */
2295 	{ SST(0x5D, 0x63, SS_RDEF,	/* XXX TBD */
2296 	    "Firmware impending failure seek error rate too high") },
2297 	/* D         B    */
2298 	{ SST(0x5D, 0x64, SS_RDEF,	/* XXX TBD */
2299 	    "Firmware impending failure too many block reassigns") },
2300 	/* D         B    */
2301 	{ SST(0x5D, 0x65, SS_RDEF,	/* XXX TBD */
2302 	    "Firmware impending failure access times too high") },
2303 	/* D         B    */
2304 	{ SST(0x5D, 0x66, SS_RDEF,	/* XXX TBD */
2305 	    "Firmware impending failure start unit times too high") },
2306 	/* D         B    */
2307 	{ SST(0x5D, 0x67, SS_RDEF,	/* XXX TBD */
2308 	    "Firmware impending failure channel parametrics") },
2309 	/* D         B    */
2310 	{ SST(0x5D, 0x68, SS_RDEF,	/* XXX TBD */
2311 	    "Firmware impending failure controller detected") },
2312 	/* D         B    */
2313 	{ SST(0x5D, 0x69, SS_RDEF,	/* XXX TBD */
2314 	    "Firmware impending failure throughput performance") },
2315 	/* D         B    */
2316 	{ SST(0x5D, 0x6A, SS_RDEF,	/* XXX TBD */
2317 	    "Firmware impending failure seek time performance") },
2318 	/* D         B    */
2319 	{ SST(0x5D, 0x6B, SS_RDEF,	/* XXX TBD */
2320 	    "Firmware impending failure spin-up retry count") },
2321 	/* D         B    */
2322 	{ SST(0x5D, 0x6C, SS_RDEF,	/* XXX TBD */
2323 	    "Firmware impending failure drive calibration retry count") },
2324 	/* DTLPWROMAEBKVF */
2325 	{ SST(0x5D, 0xFF, SS_RDEF,
2326 	    "Failure prediction threshold exceeded (false)") },
2327 	/* DTLPWRO A  K   */
2328 	{ SST(0x5E, 0x00, SS_RDEF,
2329 	    "Low power condition on") },
2330 	/* DTLPWRO A  K   */
2331 	{ SST(0x5E, 0x01, SS_RDEF,
2332 	    "Idle condition activated by timer") },
2333 	/* DTLPWRO A  K   */
2334 	{ SST(0x5E, 0x02, SS_RDEF,
2335 	    "Standby condition activated by timer") },
2336 	/* DTLPWRO A  K   */
2337 	{ SST(0x5E, 0x03, SS_RDEF,
2338 	    "Idle condition activated by command") },
2339 	/* DTLPWRO A  K   */
2340 	{ SST(0x5E, 0x04, SS_RDEF,
2341 	    "Standby condition activated by command") },
2342 	/*           B    */
2343 	{ SST(0x5E, 0x41, SS_RDEF,	/* XXX TBD */
2344 	    "Power state change to active") },
2345 	/*           B    */
2346 	{ SST(0x5E, 0x42, SS_RDEF,	/* XXX TBD */
2347 	    "Power state change to idle") },
2348 	/*           B    */
2349 	{ SST(0x5E, 0x43, SS_RDEF,	/* XXX TBD */
2350 	    "Power state change to standby") },
2351 	/*           B    */
2352 	{ SST(0x5E, 0x45, SS_RDEF,	/* XXX TBD */
2353 	    "Power state change to sleep") },
2354 	/*           BK   */
2355 	{ SST(0x5E, 0x47, SS_RDEF,	/* XXX TBD */
2356 	    "Power state change to device control") },
2357 	/*                */
2358 	{ SST(0x60, 0x00, SS_RDEF,
2359 	    "Lamp failure") },
2360 	/*                */
2361 	{ SST(0x61, 0x00, SS_RDEF,
2362 	    "Video acquisition error") },
2363 	/*                */
2364 	{ SST(0x61, 0x01, SS_RDEF,
2365 	    "Unable to acquire video") },
2366 	/*                */
2367 	{ SST(0x61, 0x02, SS_RDEF,
2368 	    "Out of focus") },
2369 	/*                */
2370 	{ SST(0x62, 0x00, SS_RDEF,
2371 	    "Scan head positioning error") },
2372 	/*      R         */
2373 	{ SST(0x63, 0x00, SS_RDEF,
2374 	    "End of user area encountered on this track") },
2375 	/*      R         */
2376 	{ SST(0x63, 0x01, SS_FATAL | ENOSPC,
2377 	    "Packet does not fit in available space") },
2378 	/*      R         */
2379 	{ SST(0x64, 0x00, SS_FATAL | ENXIO,
2380 	    "Illegal mode for this track") },
2381 	/*      R         */
2382 	{ SST(0x64, 0x01, SS_RDEF,
2383 	    "Invalid packet size") },
2384 	/* DTLPWROMAEBKVF */
2385 	{ SST(0x65, 0x00, SS_RDEF,
2386 	    "Voltage fault") },
2387 	/*                */
2388 	{ SST(0x66, 0x00, SS_RDEF,
2389 	    "Automatic document feeder cover up") },
2390 	/*                */
2391 	{ SST(0x66, 0x01, SS_RDEF,
2392 	    "Automatic document feeder lift up") },
2393 	/*                */
2394 	{ SST(0x66, 0x02, SS_RDEF,
2395 	    "Document jam in automatic document feeder") },
2396 	/*                */
2397 	{ SST(0x66, 0x03, SS_RDEF,
2398 	    "Document miss feed automatic in document feeder") },
2399 	/*         A      */
2400 	{ SST(0x67, 0x00, SS_RDEF,
2401 	    "Configuration failure") },
2402 	/*         A      */
2403 	{ SST(0x67, 0x01, SS_RDEF,
2404 	    "Configuration of incapable logical units failed") },
2405 	/*         A      */
2406 	{ SST(0x67, 0x02, SS_RDEF,
2407 	    "Add logical unit failed") },
2408 	/*         A      */
2409 	{ SST(0x67, 0x03, SS_RDEF,
2410 	    "Modification of logical unit failed") },
2411 	/*         A      */
2412 	{ SST(0x67, 0x04, SS_RDEF,
2413 	    "Exchange of logical unit failed") },
2414 	/*         A      */
2415 	{ SST(0x67, 0x05, SS_RDEF,
2416 	    "Remove of logical unit failed") },
2417 	/*         A      */
2418 	{ SST(0x67, 0x06, SS_RDEF,
2419 	    "Attachment of logical unit failed") },
2420 	/*         A      */
2421 	{ SST(0x67, 0x07, SS_RDEF,
2422 	    "Creation of logical unit failed") },
2423 	/*         A      */
2424 	{ SST(0x67, 0x08, SS_RDEF,	/* XXX TBD */
2425 	    "Assign failure occurred") },
2426 	/*         A      */
2427 	{ SST(0x67, 0x09, SS_RDEF,	/* XXX TBD */
2428 	    "Multiply assigned logical unit") },
2429 	/* DTLPWROMAEBKVF */
2430 	{ SST(0x67, 0x0A, SS_RDEF,	/* XXX TBD */
2431 	    "Set target port groups command failed") },
2432 	/* DT        B    */
2433 	{ SST(0x67, 0x0B, SS_RDEF,	/* XXX TBD */
2434 	    "ATA device feature not enabled") },
2435 	/*         A      */
2436 	{ SST(0x68, 0x00, SS_RDEF,
2437 	    "Logical unit not configured") },
2438 	/*         A      */
2439 	{ SST(0x69, 0x00, SS_RDEF,
2440 	    "Data loss on logical unit") },
2441 	/*         A      */
2442 	{ SST(0x69, 0x01, SS_RDEF,
2443 	    "Multiple logical unit failures") },
2444 	/*         A      */
2445 	{ SST(0x69, 0x02, SS_RDEF,
2446 	    "Parity/data mismatch") },
2447 	/*         A      */
2448 	{ SST(0x6A, 0x00, SS_RDEF,
2449 	    "Informational, refer to log") },
2450 	/*         A      */
2451 	{ SST(0x6B, 0x00, SS_RDEF,
2452 	    "State change has occurred") },
2453 	/*         A      */
2454 	{ SST(0x6B, 0x01, SS_RDEF,
2455 	    "Redundancy level got better") },
2456 	/*         A      */
2457 	{ SST(0x6B, 0x02, SS_RDEF,
2458 	    "Redundancy level got worse") },
2459 	/*         A      */
2460 	{ SST(0x6C, 0x00, SS_RDEF,
2461 	    "Rebuild failure occurred") },
2462 	/*         A      */
2463 	{ SST(0x6D, 0x00, SS_RDEF,
2464 	    "Recalculate failure occurred") },
2465 	/*         A      */
2466 	{ SST(0x6E, 0x00, SS_RDEF,
2467 	    "Command to logical unit failed") },
2468 	/*      R         */
2469 	{ SST(0x6F, 0x00, SS_RDEF,	/* XXX TBD */
2470 	    "Copy protection key exchange failure - authentication failure") },
2471 	/*      R         */
2472 	{ SST(0x6F, 0x01, SS_RDEF,	/* XXX TBD */
2473 	    "Copy protection key exchange failure - key not present") },
2474 	/*      R         */
2475 	{ SST(0x6F, 0x02, SS_RDEF,	/* XXX TBD */
2476 	    "Copy protection key exchange failure - key not established") },
2477 	/*      R         */
2478 	{ SST(0x6F, 0x03, SS_RDEF,	/* XXX TBD */
2479 	    "Read of scrambled sector without authentication") },
2480 	/*      R         */
2481 	{ SST(0x6F, 0x04, SS_RDEF,	/* XXX TBD */
2482 	    "Media region code is mismatched to logical unit region") },
2483 	/*      R         */
2484 	{ SST(0x6F, 0x05, SS_RDEF,	/* XXX TBD */
2485 	    "Drive region must be permanent/region reset count error") },
2486 	/*      R         */
2487 	{ SST(0x6F, 0x06, SS_RDEF,	/* XXX TBD */
2488 	    "Insufficient block count for binding NONCE recording") },
2489 	/*      R         */
2490 	{ SST(0x6F, 0x07, SS_RDEF,	/* XXX TBD */
2491 	    "Conflict in binding NONCE recording") },
2492 	/*  T             */
2493 	{ SST(0x70, 0x00, SS_RDEF,
2494 	    "Decompression exception short: ASCQ = Algorithm ID") },
2495 	/*  T             */
2496 	{ SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
2497 	    NULL) },			/* Range 0x00 -> 0xFF */
2498 	/*  T             */
2499 	{ SST(0x71, 0x00, SS_RDEF,
2500 	    "Decompression exception long: ASCQ = Algorithm ID") },
2501 	/*  T             */
2502 	{ SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
2503 	    NULL) },			/* Range 0x00 -> 0xFF */
2504 	/*      R         */
2505 	{ SST(0x72, 0x00, SS_RDEF,
2506 	    "Session fixation error") },
2507 	/*      R         */
2508 	{ SST(0x72, 0x01, SS_RDEF,
2509 	    "Session fixation error writing lead-in") },
2510 	/*      R         */
2511 	{ SST(0x72, 0x02, SS_RDEF,
2512 	    "Session fixation error writing lead-out") },
2513 	/*      R         */
2514 	{ SST(0x72, 0x03, SS_RDEF,
2515 	    "Session fixation error - incomplete track in session") },
2516 	/*      R         */
2517 	{ SST(0x72, 0x04, SS_RDEF,
2518 	    "Empty or partially written reserved track") },
2519 	/*      R         */
2520 	{ SST(0x72, 0x05, SS_RDEF,	/* XXX TBD */
2521 	    "No more track reservations allowed") },
2522 	/*      R         */
2523 	{ SST(0x72, 0x06, SS_RDEF,	/* XXX TBD */
2524 	    "RMZ extension is not allowed") },
2525 	/*      R         */
2526 	{ SST(0x72, 0x07, SS_RDEF,	/* XXX TBD */
2527 	    "No more test zone extensions are allowed") },
2528 	/*      R         */
2529 	{ SST(0x73, 0x00, SS_RDEF,
2530 	    "CD control error") },
2531 	/*      R         */
2532 	{ SST(0x73, 0x01, SS_RDEF,
2533 	    "Power calibration area almost full") },
2534 	/*      R         */
2535 	{ SST(0x73, 0x02, SS_FATAL | ENOSPC,
2536 	    "Power calibration area is full") },
2537 	/*      R         */
2538 	{ SST(0x73, 0x03, SS_RDEF,
2539 	    "Power calibration area error") },
2540 	/*      R         */
2541 	{ SST(0x73, 0x04, SS_RDEF,
2542 	    "Program memory area update failure") },
2543 	/*      R         */
2544 	{ SST(0x73, 0x05, SS_RDEF,
2545 	    "Program memory area is full") },
2546 	/*      R         */
2547 	{ SST(0x73, 0x06, SS_RDEF,	/* XXX TBD */
2548 	    "RMA/PMA is almost full") },
2549 	/*      R         */
2550 	{ SST(0x73, 0x10, SS_RDEF,	/* XXX TBD */
2551 	    "Current power calibration area almost full") },
2552 	/*      R         */
2553 	{ SST(0x73, 0x11, SS_RDEF,	/* XXX TBD */
2554 	    "Current power calibration area is full") },
2555 	/*      R         */
2556 	{ SST(0x73, 0x17, SS_RDEF,	/* XXX TBD */
2557 	    "RDZ is full") },
2558 	/*  T             */
2559 	{ SST(0x74, 0x00, SS_RDEF,	/* XXX TBD */
2560 	    "Security error") },
2561 	/*  T             */
2562 	{ SST(0x74, 0x01, SS_RDEF,	/* XXX TBD */
2563 	    "Unable to decrypt data") },
2564 	/*  T             */
2565 	{ SST(0x74, 0x02, SS_RDEF,	/* XXX TBD */
2566 	    "Unencrypted data encountered while decrypting") },
2567 	/*  T             */
2568 	{ SST(0x74, 0x03, SS_RDEF,	/* XXX TBD */
2569 	    "Incorrect data encryption key") },
2570 	/*  T             */
2571 	{ SST(0x74, 0x04, SS_RDEF,	/* XXX TBD */
2572 	    "Cryptographic integrity validation failed") },
2573 	/*  T             */
2574 	{ SST(0x74, 0x05, SS_RDEF,	/* XXX TBD */
2575 	    "Error decrypting data") },
2576 	/*  T             */
2577 	{ SST(0x74, 0x06, SS_RDEF,	/* XXX TBD */
2578 	    "Unknown signature verification key") },
2579 	/*  T             */
2580 	{ SST(0x74, 0x07, SS_RDEF,	/* XXX TBD */
2581 	    "Encryption parameters not useable") },
2582 	/* DT   R M E  VF */
2583 	{ SST(0x74, 0x08, SS_RDEF,	/* XXX TBD */
2584 	    "Digital signature validation failure") },
2585 	/*  T             */
2586 	{ SST(0x74, 0x09, SS_RDEF,	/* XXX TBD */
2587 	    "Encryption mode mismatch on read") },
2588 	/*  T             */
2589 	{ SST(0x74, 0x0A, SS_RDEF,	/* XXX TBD */
2590 	    "Encrypted block not raw read enabled") },
2591 	/*  T             */
2592 	{ SST(0x74, 0x0B, SS_RDEF,	/* XXX TBD */
2593 	    "Incorrect encryption parameters") },
2594 	/* DT   R MAEBKV  */
2595 	{ SST(0x74, 0x0C, SS_RDEF,	/* XXX TBD */
2596 	    "Unable to decrypt parameter list") },
2597 	/*  T             */
2598 	{ SST(0x74, 0x0D, SS_RDEF,	/* XXX TBD */
2599 	    "Encryption algorithm disabled") },
2600 	/* DT   R MAEBKV  */
2601 	{ SST(0x74, 0x10, SS_RDEF,	/* XXX TBD */
2602 	    "SA creation parameter value invalid") },
2603 	/* DT   R MAEBKV  */
2604 	{ SST(0x74, 0x11, SS_RDEF,	/* XXX TBD */
2605 	    "SA creation parameter value rejected") },
2606 	/* DT   R MAEBKV  */
2607 	{ SST(0x74, 0x12, SS_RDEF,	/* XXX TBD */
2608 	    "Invalid SA usage") },
2609 	/*  T             */
2610 	{ SST(0x74, 0x21, SS_RDEF,	/* XXX TBD */
2611 	    "Data encryption configuration prevented") },
2612 	/* DT   R MAEBKV  */
2613 	{ SST(0x74, 0x30, SS_RDEF,	/* XXX TBD */
2614 	    "SA creation parameter not supported") },
2615 	/* DT   R MAEBKV  */
2616 	{ SST(0x74, 0x40, SS_RDEF,	/* XXX TBD */
2617 	    "Authentication failed") },
2618 	/*             V  */
2619 	{ SST(0x74, 0x61, SS_RDEF,	/* XXX TBD */
2620 	    "External data encryption key manager access error") },
2621 	/*             V  */
2622 	{ SST(0x74, 0x62, SS_RDEF,	/* XXX TBD */
2623 	    "External data encryption key manager error") },
2624 	/*             V  */
2625 	{ SST(0x74, 0x63, SS_RDEF,	/* XXX TBD */
2626 	    "External data encryption key not found") },
2627 	/*             V  */
2628 	{ SST(0x74, 0x64, SS_RDEF,	/* XXX TBD */
2629 	    "External data encryption request not authorized") },
2630 	/*  T             */
2631 	{ SST(0x74, 0x6E, SS_RDEF,	/* XXX TBD */
2632 	    "External data encryption control timeout") },
2633 	/*  T             */
2634 	{ SST(0x74, 0x6F, SS_RDEF,	/* XXX TBD */
2635 	    "External data encryption control error") },
2636 	/* DT   R M E  V  */
2637 	{ SST(0x74, 0x71, SS_RDEF,	/* XXX TBD */
2638 	    "Logical unit access not authorized") },
2639 	/* D              */
2640 	{ SST(0x74, 0x79, SS_RDEF,	/* XXX TBD */
2641 	    "Security conflict in translated device") }
2642 };
2643 
2644 const int asc_table_size = sizeof(asc_table)/sizeof(asc_table[0]);
2645 
2646 struct asc_key
2647 {
2648 	int asc;
2649 	int ascq;
2650 };
2651 
2652 static int
2653 ascentrycomp(const void *key, const void *member)
2654 {
2655 	int asc;
2656 	int ascq;
2657 	const struct asc_table_entry *table_entry;
2658 
2659 	asc = ((const struct asc_key *)key)->asc;
2660 	ascq = ((const struct asc_key *)key)->ascq;
2661 	table_entry = (const struct asc_table_entry *)member;
2662 
2663 	if (asc >= table_entry->asc) {
2664 
2665 		if (asc > table_entry->asc)
2666 			return (1);
2667 
2668 		if (ascq <= table_entry->ascq) {
2669 			/* Check for ranges */
2670 			if (ascq == table_entry->ascq
2671 		 	 || ((table_entry->action & SSQ_RANGE) != 0
2672 		  	   && ascq >= (table_entry - 1)->ascq))
2673 				return (0);
2674 			return (-1);
2675 		}
2676 		return (1);
2677 	}
2678 	return (-1);
2679 }
2680 
2681 static int
2682 senseentrycomp(const void *key, const void *member)
2683 {
2684 	int sense_key;
2685 	const struct sense_key_table_entry *table_entry;
2686 
2687 	sense_key = *((const int *)key);
2688 	table_entry = (const struct sense_key_table_entry *)member;
2689 
2690 	if (sense_key >= table_entry->sense_key) {
2691 		if (sense_key == table_entry->sense_key)
2692 			return (0);
2693 		return (1);
2694 	}
2695 	return (-1);
2696 }
2697 
2698 static void
2699 fetchtableentries(int sense_key, int asc, int ascq,
2700 		  struct scsi_inquiry_data *inq_data,
2701 		  const struct sense_key_table_entry **sense_entry,
2702 		  const struct asc_table_entry **asc_entry)
2703 {
2704 	caddr_t match;
2705 	const struct asc_table_entry *asc_tables[2];
2706 	const struct sense_key_table_entry *sense_tables[2];
2707 	struct asc_key asc_ascq;
2708 	size_t asc_tables_size[2];
2709 	size_t sense_tables_size[2];
2710 	int num_asc_tables;
2711 	int num_sense_tables;
2712 	int i;
2713 
2714 	/* Default to failure */
2715 	*sense_entry = NULL;
2716 	*asc_entry = NULL;
2717 	match = NULL;
2718 	if (inq_data != NULL)
2719 		match = cam_quirkmatch((caddr_t)inq_data,
2720 				       (caddr_t)sense_quirk_table,
2721 				       sense_quirk_table_size,
2722 				       sizeof(*sense_quirk_table),
2723 				       scsi_inquiry_match);
2724 
2725 	if (match != NULL) {
2726 		struct scsi_sense_quirk_entry *quirk;
2727 
2728 		quirk = (struct scsi_sense_quirk_entry *)match;
2729 		asc_tables[0] = quirk->asc_info;
2730 		asc_tables_size[0] = quirk->num_ascs;
2731 		asc_tables[1] = asc_table;
2732 		asc_tables_size[1] = asc_table_size;
2733 		num_asc_tables = 2;
2734 		sense_tables[0] = quirk->sense_key_info;
2735 		sense_tables_size[0] = quirk->num_sense_keys;
2736 		sense_tables[1] = sense_key_table;
2737 		sense_tables_size[1] = sense_key_table_size;
2738 		num_sense_tables = 2;
2739 	} else {
2740 		asc_tables[0] = asc_table;
2741 		asc_tables_size[0] = asc_table_size;
2742 		num_asc_tables = 1;
2743 		sense_tables[0] = sense_key_table;
2744 		sense_tables_size[0] = sense_key_table_size;
2745 		num_sense_tables = 1;
2746 	}
2747 
2748 	asc_ascq.asc = asc;
2749 	asc_ascq.ascq = ascq;
2750 	for (i = 0; i < num_asc_tables; i++) {
2751 		void *found_entry;
2752 
2753 		found_entry = bsearch(&asc_ascq, asc_tables[i],
2754 				      asc_tables_size[i],
2755 				      sizeof(**asc_tables),
2756 				      ascentrycomp);
2757 
2758 		if (found_entry) {
2759 			*asc_entry = (struct asc_table_entry *)found_entry;
2760 			break;
2761 		}
2762 	}
2763 
2764 	for (i = 0; i < num_sense_tables; i++) {
2765 		void *found_entry;
2766 
2767 		found_entry = bsearch(&sense_key, sense_tables[i],
2768 				      sense_tables_size[i],
2769 				      sizeof(**sense_tables),
2770 				      senseentrycomp);
2771 
2772 		if (found_entry) {
2773 			*sense_entry =
2774 			    (struct sense_key_table_entry *)found_entry;
2775 			break;
2776 		}
2777 	}
2778 }
2779 
2780 void
2781 scsi_sense_desc(int sense_key, int asc, int ascq,
2782 		struct scsi_inquiry_data *inq_data,
2783 		const char **sense_key_desc, const char **asc_desc)
2784 {
2785 	const struct asc_table_entry *asc_entry;
2786 	const struct sense_key_table_entry *sense_entry;
2787 
2788 	fetchtableentries(sense_key, asc, ascq,
2789 			  inq_data,
2790 			  &sense_entry,
2791 			  &asc_entry);
2792 
2793 	*sense_key_desc = sense_entry->desc;
2794 
2795 	if (asc_entry != NULL)
2796 		*asc_desc = asc_entry->desc;
2797 	else if (asc >= 0x80 && asc <= 0xff)
2798 		*asc_desc = "Vendor Specific ASC";
2799 	else if (ascq >= 0x80 && ascq <= 0xff)
2800 		*asc_desc = "Vendor Specific ASCQ";
2801 	else
2802 		*asc_desc = "Reserved ASC/ASCQ pair";
2803 }
2804 
2805 /*
2806  * Given sense and device type information, return the appropriate action.
2807  * If we do not understand the specific error as identified by the ASC/ASCQ
2808  * pair, fall back on the more generic actions derived from the sense key.
2809  */
2810 scsi_sense_action
2811 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
2812 		  u_int32_t sense_flags)
2813 {
2814 	const struct asc_table_entry *asc_entry;
2815 	const struct sense_key_table_entry *sense_entry;
2816 	int error_code, sense_key, asc, ascq;
2817 	scsi_sense_action action;
2818 
2819 	scsi_extract_sense(&csio->sense_data, &error_code,
2820 			   &sense_key, &asc, &ascq);
2821 
2822 	if (error_code == SSD_DEFERRED_ERROR) {
2823 		/*
2824 		 * XXX dufault@FreeBSD.org
2825 		 * This error doesn't relate to the command associated
2826 		 * with this request sense.  A deferred error is an error
2827 		 * for a command that has already returned GOOD status
2828 		 * (see SCSI2 8.2.14.2).
2829 		 *
2830 		 * By my reading of that section, it looks like the current
2831 		 * command has been cancelled, we should now clean things up
2832 		 * (hopefully recovering any lost data) and then retry the
2833 		 * current command.  There are two easy choices, both wrong:
2834 		 *
2835 		 * 1. Drop through (like we had been doing), thus treating
2836 		 *    this as if the error were for the current command and
2837 		 *    return and stop the current command.
2838 		 *
2839 		 * 2. Issue a retry (like I made it do) thus hopefully
2840 		 *    recovering the current transfer, and ignoring the
2841 		 *    fact that we've dropped a command.
2842 		 *
2843 		 * These should probably be handled in a device specific
2844 		 * sense handler or punted back up to a user mode daemon
2845 		 */
2846 		action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
2847 	} else {
2848 		fetchtableentries(sense_key, asc, ascq,
2849 				  inq_data,
2850 				  &sense_entry,
2851 				  &asc_entry);
2852 
2853 		/*
2854 		 * Override the 'No additional Sense' entry (0,0)
2855 		 * with the error action of the sense key.
2856 		 */
2857 		if (asc_entry != NULL
2858 		 && (asc != 0 || ascq != 0))
2859 			action = asc_entry->action;
2860 		else
2861 			action = sense_entry->action;
2862 
2863 		if (sense_key == SSD_KEY_RECOVERED_ERROR) {
2864 			/*
2865 			 * The action succeeded but the device wants
2866 			 * the user to know that some recovery action
2867 			 * was required.
2868 			 */
2869 			action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
2870 			action |= SS_NOP|SSQ_PRINT_SENSE;
2871 		} else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
2872 			if ((sense_flags & SF_QUIET_IR) != 0)
2873 				action &= ~SSQ_PRINT_SENSE;
2874 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
2875 			if ((sense_flags & SF_RETRY_UA) != 0
2876 			 && (action & SS_MASK) == SS_FAIL) {
2877 				action &= ~(SS_MASK|SSQ_MASK);
2878 				action |= SS_RETRY|SSQ_DECREMENT_COUNT|
2879 					  SSQ_PRINT_SENSE;
2880 			}
2881 		}
2882 	}
2883 #ifdef _KERNEL
2884 	if (bootverbose)
2885 		sense_flags |= SF_PRINT_ALWAYS;
2886 #endif
2887 	if ((sense_flags & SF_PRINT_ALWAYS) != 0)
2888 		action |= SSQ_PRINT_SENSE;
2889 	else if ((sense_flags & SF_NO_PRINT) != 0)
2890 		action &= ~SSQ_PRINT_SENSE;
2891 
2892 	return (action);
2893 }
2894 
2895 char *
2896 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
2897 {
2898 	u_int8_t cdb_len;
2899 	int i;
2900 
2901 	if (cdb_ptr == NULL)
2902 		return("");
2903 
2904 	/* Silence warnings */
2905 	cdb_len = 0;
2906 
2907 	/*
2908 	 * This is taken from the SCSI-3 draft spec.
2909 	 * (T10/1157D revision 0.3)
2910 	 * The top 3 bits of an opcode are the group code.  The next 5 bits
2911 	 * are the command code.
2912 	 * Group 0:  six byte commands
2913 	 * Group 1:  ten byte commands
2914 	 * Group 2:  ten byte commands
2915 	 * Group 3:  reserved
2916 	 * Group 4:  sixteen byte commands
2917 	 * Group 5:  twelve byte commands
2918 	 * Group 6:  vendor specific
2919 	 * Group 7:  vendor specific
2920 	 */
2921 	switch((*cdb_ptr >> 5) & 0x7) {
2922 		case 0:
2923 			cdb_len = 6;
2924 			break;
2925 		case 1:
2926 		case 2:
2927 			cdb_len = 10;
2928 			break;
2929 		case 3:
2930 		case 6:
2931 		case 7:
2932 			/* in this case, just print out the opcode */
2933 			cdb_len = 1;
2934 			break;
2935 		case 4:
2936 			cdb_len = 16;
2937 			break;
2938 		case 5:
2939 			cdb_len = 12;
2940 			break;
2941 	}
2942 	*cdb_string = '\0';
2943 	for (i = 0; i < cdb_len; i++)
2944 		snprintf(cdb_string + strlen(cdb_string),
2945 			 len - strlen(cdb_string), "%x ", cdb_ptr[i]);
2946 
2947 	return(cdb_string);
2948 }
2949 
2950 const char *
2951 scsi_status_string(struct ccb_scsiio *csio)
2952 {
2953 	switch(csio->scsi_status) {
2954 	case SCSI_STATUS_OK:
2955 		return("OK");
2956 	case SCSI_STATUS_CHECK_COND:
2957 		return("Check Condition");
2958 	case SCSI_STATUS_BUSY:
2959 		return("Busy");
2960 	case SCSI_STATUS_INTERMED:
2961 		return("Intermediate");
2962 	case SCSI_STATUS_INTERMED_COND_MET:
2963 		return("Intermediate-Condition Met");
2964 	case SCSI_STATUS_RESERV_CONFLICT:
2965 		return("Reservation Conflict");
2966 	case SCSI_STATUS_CMD_TERMINATED:
2967 		return("Command Terminated");
2968 	case SCSI_STATUS_QUEUE_FULL:
2969 		return("Queue Full");
2970 	case SCSI_STATUS_ACA_ACTIVE:
2971 		return("ACA Active");
2972 	case SCSI_STATUS_TASK_ABORTED:
2973 		return("Task Aborted");
2974 	default: {
2975 		static char unkstr[64];
2976 		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
2977 			 csio->scsi_status);
2978 		return(unkstr);
2979 	}
2980 	}
2981 }
2982 
2983 /*
2984  * scsi_command_string() returns 0 for success and -1 for failure.
2985  */
2986 #ifdef _KERNEL
2987 int
2988 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
2989 #else /* !_KERNEL */
2990 int
2991 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio,
2992 		    struct sbuf *sb)
2993 #endif /* _KERNEL/!_KERNEL */
2994 {
2995 	struct scsi_inquiry_data *inq_data;
2996 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2997 #ifdef _KERNEL
2998 	struct	  ccb_getdev *cgd;
2999 #endif /* _KERNEL */
3000 
3001 #ifdef _KERNEL
3002 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3003 		return(-1);
3004 	/*
3005 	 * Get the device information.
3006 	 */
3007 	xpt_setup_ccb(&cgd->ccb_h,
3008 		      csio->ccb_h.path,
3009 		      CAM_PRIORITY_NORMAL);
3010 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3011 	xpt_action((union ccb *)cgd);
3012 
3013 	/*
3014 	 * If the device is unconfigured, just pretend that it is a hard
3015 	 * drive.  scsi_op_desc() needs this.
3016 	 */
3017 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3018 		cgd->inq_data.device = T_DIRECT;
3019 
3020 	inq_data = &cgd->inq_data;
3021 
3022 #else /* !_KERNEL */
3023 
3024 	inq_data = &device->inq_data;
3025 
3026 #endif /* _KERNEL/!_KERNEL */
3027 
3028 	if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3029 		sbuf_printf(sb, "%s. CDB: %s",
3030 			    scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data),
3031 			    scsi_cdb_string(csio->cdb_io.cdb_ptr, cdb_str,
3032 					    sizeof(cdb_str)));
3033 	} else {
3034 		sbuf_printf(sb, "%s. CDB: %s",
3035 			    scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data),
3036 			    scsi_cdb_string(csio->cdb_io.cdb_bytes, cdb_str,
3037 					    sizeof(cdb_str)));
3038 	}
3039 
3040 	return(0);
3041 }
3042 
3043 
3044 /*
3045  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
3046  */
3047 #ifdef _KERNEL
3048 int
3049 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
3050 		scsi_sense_string_flags flags)
3051 #else /* !_KERNEL */
3052 int
3053 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
3054 		struct sbuf *sb, scsi_sense_string_flags flags)
3055 #endif /* _KERNEL/!_KERNEL */
3056 {
3057 	struct	  scsi_sense_data *sense;
3058 	struct	  scsi_inquiry_data *inq_data;
3059 #ifdef _KERNEL
3060 	struct	  ccb_getdev *cgd;
3061 #endif /* _KERNEL */
3062 	u_int32_t info;
3063 	int	  error_code;
3064 	int	  sense_key;
3065 	int	  asc, ascq;
3066 	char	  path_str[64];
3067 
3068 #ifndef _KERNEL
3069 	if (device == NULL)
3070 		return(-1);
3071 #endif /* !_KERNEL */
3072 	if ((csio == NULL) || (sb == NULL))
3073 		return(-1);
3074 
3075 	/*
3076 	 * If the CDB is a physical address, we can't deal with it..
3077 	 */
3078 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
3079 		flags &= ~SSS_FLAG_PRINT_COMMAND;
3080 
3081 #ifdef _KERNEL
3082 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
3083 #else /* !_KERNEL */
3084 	cam_path_string(device, path_str, sizeof(path_str));
3085 #endif /* _KERNEL/!_KERNEL */
3086 
3087 #ifdef _KERNEL
3088 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3089 		return(-1);
3090 	/*
3091 	 * Get the device information.
3092 	 */
3093 	xpt_setup_ccb(&cgd->ccb_h,
3094 		      csio->ccb_h.path,
3095 		      CAM_PRIORITY_NORMAL);
3096 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3097 	xpt_action((union ccb *)cgd);
3098 
3099 	/*
3100 	 * If the device is unconfigured, just pretend that it is a hard
3101 	 * drive.  scsi_op_desc() needs this.
3102 	 */
3103 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3104 		cgd->inq_data.device = T_DIRECT;
3105 
3106 	inq_data = &cgd->inq_data;
3107 
3108 #else /* !_KERNEL */
3109 
3110 	inq_data = &device->inq_data;
3111 
3112 #endif /* _KERNEL/!_KERNEL */
3113 
3114 	sense = NULL;
3115 
3116 	if (flags & SSS_FLAG_PRINT_COMMAND) {
3117 
3118 		sbuf_cat(sb, path_str);
3119 
3120 #ifdef _KERNEL
3121 		scsi_command_string(csio, sb);
3122 #else /* !_KERNEL */
3123 		scsi_command_string(device, csio, sb);
3124 #endif /* _KERNEL/!_KERNEL */
3125 		sbuf_printf(sb, "\n");
3126 	}
3127 
3128 	/*
3129 	 * If the sense data is a physical pointer, forget it.
3130 	 */
3131 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
3132 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
3133 #ifdef _KERNEL
3134 			xpt_free_ccb((union ccb*)cgd);
3135 #endif /* _KERNEL/!_KERNEL */
3136 			return(-1);
3137 		} else {
3138 			/*
3139 			 * bcopy the pointer to avoid unaligned access
3140 			 * errors on finicky architectures.  We don't
3141 			 * ensure that the sense data is pointer aligned.
3142 			 */
3143 			bcopy(&csio->sense_data, &sense,
3144 			      sizeof(struct scsi_sense_data *));
3145 		}
3146 	} else {
3147 		/*
3148 		 * If the physical sense flag is set, but the sense pointer
3149 		 * is not also set, we assume that the user is an idiot and
3150 		 * return.  (Well, okay, it could be that somehow, the
3151 		 * entire csio is physical, but we would have probably core
3152 		 * dumped on one of the bogus pointer deferences above
3153 		 * already.)
3154 		 */
3155 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
3156 #ifdef _KERNEL
3157 			xpt_free_ccb((union ccb*)cgd);
3158 #endif /* _KERNEL/!_KERNEL */
3159 			return(-1);
3160 		} else
3161 			sense = &csio->sense_data;
3162 	}
3163 
3164 
3165 	sbuf_cat(sb, path_str);
3166 
3167 	error_code = sense->error_code & SSD_ERRCODE;
3168 	sense_key = sense->flags & SSD_KEY;
3169 
3170 	sbuf_printf(sb, "SCSI sense: ");
3171 	switch (error_code) {
3172 	case SSD_DEFERRED_ERROR:
3173 		sbuf_printf(sb, "Deferred error: ");
3174 
3175 		/* FALLTHROUGH */
3176 	case SSD_CURRENT_ERROR:
3177 	{
3178 		const char *sense_key_desc;
3179 		const char *asc_desc;
3180 
3181 		asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
3182 		ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
3183 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
3184 				&sense_key_desc, &asc_desc);
3185 		sbuf_cat(sb, sense_key_desc);
3186 
3187 		info = scsi_4btoul(sense->info);
3188 
3189 		if (sense->error_code & SSD_ERRCODE_VALID) {
3190 
3191 			switch (sense_key) {
3192 			case SSD_KEY_NOT_READY:
3193 			case SSD_KEY_ILLEGAL_REQUEST:
3194 			case SSD_KEY_UNIT_ATTENTION:
3195 			case SSD_KEY_DATA_PROTECT:
3196 				break;
3197 			case SSD_KEY_BLANK_CHECK:
3198 				sbuf_printf(sb, " req sz: %d (decimal)", info);
3199 				break;
3200 			default:
3201 				if (info) {
3202 					if (sense->flags & SSD_ILI) {
3203 						sbuf_printf(sb, " ILI (length "
3204 							"mismatch): %d", info);
3205 
3206 					} else {
3207 						sbuf_printf(sb, " info:%x",
3208 							    info);
3209 					}
3210 				}
3211 			}
3212 		} else if (info) {
3213 			sbuf_printf(sb, " info?:%x", info);
3214 		}
3215 
3216 		if (sense->extra_len >= 4) {
3217 			if (bcmp(sense->cmd_spec_info, "\0\0\0\0", 4)) {
3218 				sbuf_printf(sb, " csi:%x,%x,%x,%x",
3219 					    sense->cmd_spec_info[0],
3220 					    sense->cmd_spec_info[1],
3221 					    sense->cmd_spec_info[2],
3222 					    sense->cmd_spec_info[3]);
3223 			}
3224 		}
3225 
3226 		sbuf_printf(sb, " asc:%x,%x (%s)", asc, ascq, asc_desc);
3227 
3228 		if (sense->extra_len >= 7 && sense->fru) {
3229 			sbuf_printf(sb, " field replaceable unit: %x",
3230 				    sense->fru);
3231 		}
3232 
3233 		if ((sense->extra_len >= 10)
3234 		 && (sense->sense_key_spec[0] & SSD_SCS_VALID) != 0) {
3235 			switch(sense_key) {
3236 			case SSD_KEY_ILLEGAL_REQUEST: {
3237 				int bad_command;
3238 				char tmpstr2[40];
3239 
3240 				if (sense->sense_key_spec[0] & 0x40)
3241 					bad_command = 1;
3242 				else
3243 					bad_command = 0;
3244 
3245 				tmpstr2[0] = '\0';
3246 
3247 				/* Bit pointer is valid */
3248 				if (sense->sense_key_spec[0] & 0x08)
3249 					snprintf(tmpstr2, sizeof(tmpstr2),
3250 						 "bit %d ",
3251 						sense->sense_key_spec[0] & 0x7);
3252 				sbuf_printf(sb, ": %s byte %d %sis invalid",
3253 					    bad_command ? "Command" : "Data",
3254 					    scsi_2btoul(
3255 					    &sense->sense_key_spec[1]),
3256 					    tmpstr2);
3257 				break;
3258 			}
3259 			case SSD_KEY_RECOVERED_ERROR:
3260 			case SSD_KEY_HARDWARE_ERROR:
3261 			case SSD_KEY_MEDIUM_ERROR:
3262 				sbuf_printf(sb, " actual retry count: %d",
3263 					    scsi_2btoul(
3264 					    &sense->sense_key_spec[1]));
3265 				break;
3266 			default:
3267 				sbuf_printf(sb, " sks:%#x,%#x",
3268 					    sense->sense_key_spec[0],
3269 					    scsi_2btoul(
3270 					    &sense->sense_key_spec[1]));
3271 				break;
3272 			}
3273 		}
3274 		break;
3275 
3276 	}
3277 	default:
3278 		sbuf_printf(sb, "Error code 0x%x", sense->error_code);
3279 		if (sense->error_code & SSD_ERRCODE_VALID) {
3280 			sbuf_printf(sb, " at block no. %d (decimal)",
3281 				    info = scsi_4btoul(sense->info));
3282 		}
3283 	}
3284 
3285 	sbuf_printf(sb, "\n");
3286 
3287 #ifdef _KERNEL
3288 	xpt_free_ccb((union ccb*)cgd);
3289 #endif /* _KERNEL/!_KERNEL */
3290 	return(0);
3291 }
3292 
3293 
3294 
3295 #ifdef _KERNEL
3296 char *
3297 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
3298 #else /* !_KERNEL */
3299 char *
3300 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
3301 		  char *str, int str_len)
3302 #endif /* _KERNEL/!_KERNEL */
3303 {
3304 	struct sbuf sb;
3305 
3306 	sbuf_new(&sb, str, str_len, 0);
3307 
3308 #ifdef _KERNEL
3309 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3310 #else /* !_KERNEL */
3311 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3312 #endif /* _KERNEL/!_KERNEL */
3313 
3314 	sbuf_finish(&sb);
3315 
3316 	return(sbuf_data(&sb));
3317 }
3318 
3319 #ifdef _KERNEL
3320 void
3321 scsi_sense_print(struct ccb_scsiio *csio)
3322 {
3323 	struct sbuf sb;
3324 	char str[512];
3325 
3326 	sbuf_new(&sb, str, sizeof(str), 0);
3327 
3328 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3329 
3330 	sbuf_finish(&sb);
3331 
3332 	printf("%s", sbuf_data(&sb));
3333 }
3334 
3335 #else /* !_KERNEL */
3336 void
3337 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
3338 		 FILE *ofile)
3339 {
3340 	struct sbuf sb;
3341 	char str[512];
3342 
3343 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
3344 		return;
3345 
3346 	sbuf_new(&sb, str, sizeof(str), 0);
3347 
3348 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3349 
3350 	sbuf_finish(&sb);
3351 
3352 	fprintf(ofile, "%s", sbuf_data(&sb));
3353 }
3354 
3355 #endif /* _KERNEL/!_KERNEL */
3356 
3357 /*
3358  * This function currently requires at least 36 bytes, or
3359  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
3360  * function needs more or less data in the future, another length should be
3361  * defined in scsi_all.h to indicate the minimum amount of data necessary
3362  * for this routine to function properly.
3363  */
3364 void
3365 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
3366 {
3367 	u_int8_t type;
3368 	char *dtype, *qtype;
3369 	char vendor[16], product[48], revision[16], rstr[4];
3370 
3371 	type = SID_TYPE(inq_data);
3372 
3373 	/*
3374 	 * Figure out basic device type and qualifier.
3375 	 */
3376 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
3377 		qtype = "(vendor-unique qualifier)";
3378 	} else {
3379 		switch (SID_QUAL(inq_data)) {
3380 		case SID_QUAL_LU_CONNECTED:
3381 			qtype = "";
3382 			break;
3383 
3384 		case SID_QUAL_LU_OFFLINE:
3385 			qtype = "(offline)";
3386 			break;
3387 
3388 		case SID_QUAL_RSVD:
3389 			qtype = "(reserved qualifier)";
3390 			break;
3391 		default:
3392 		case SID_QUAL_BAD_LU:
3393 			qtype = "(LUN not supported)";
3394 			break;
3395 		}
3396 	}
3397 
3398 	switch (type) {
3399 	case T_DIRECT:
3400 		dtype = "Direct Access";
3401 		break;
3402 	case T_SEQUENTIAL:
3403 		dtype = "Sequential Access";
3404 		break;
3405 	case T_PRINTER:
3406 		dtype = "Printer";
3407 		break;
3408 	case T_PROCESSOR:
3409 		dtype = "Processor";
3410 		break;
3411 	case T_WORM:
3412 		dtype = "WORM";
3413 		break;
3414 	case T_CDROM:
3415 		dtype = "CD-ROM";
3416 		break;
3417 	case T_SCANNER:
3418 		dtype = "Scanner";
3419 		break;
3420 	case T_OPTICAL:
3421 		dtype = "Optical";
3422 		break;
3423 	case T_CHANGER:
3424 		dtype = "Changer";
3425 		break;
3426 	case T_COMM:
3427 		dtype = "Communication";
3428 		break;
3429 	case T_STORARRAY:
3430 		dtype = "Storage Array";
3431 		break;
3432 	case T_ENCLOSURE:
3433 		dtype = "Enclosure Services";
3434 		break;
3435 	case T_RBC:
3436 		dtype = "Simplified Direct Access";
3437 		break;
3438 	case T_OCRW:
3439 		dtype = "Optical Card Read/Write";
3440 		break;
3441 	case T_OSD:
3442 		dtype = "Object-Based Storage";
3443 		break;
3444 	case T_ADC:
3445 		dtype = "Automation/Drive Interface";
3446 		break;
3447 	case T_NODEVICE:
3448 		dtype = "Uninstalled";
3449 		break;
3450 	default:
3451 		dtype = "unknown";
3452 		break;
3453 	}
3454 
3455 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
3456 		   sizeof(vendor));
3457 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
3458 		   sizeof(product));
3459 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
3460 		   sizeof(revision));
3461 
3462 	if (SID_ANSI_REV(inq_data) == SCSI_REV_CCS)
3463 		bcopy("CCS", rstr, 4);
3464 	else
3465 		snprintf(rstr, sizeof (rstr), "%d", SID_ANSI_REV(inq_data));
3466 	printf("<%s %s %s> %s %s SCSI-%s device %s\n",
3467 	       vendor, product, revision,
3468 	       SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
3469 	       dtype, rstr, qtype);
3470 }
3471 
3472 /*
3473  * Table of syncrates that don't follow the "divisible by 4"
3474  * rule. This table will be expanded in future SCSI specs.
3475  */
3476 static struct {
3477 	u_int period_factor;
3478 	u_int period;	/* in 100ths of ns */
3479 } scsi_syncrates[] = {
3480 	{ 0x08, 625 },	/* FAST-160 */
3481 	{ 0x09, 1250 },	/* FAST-80 */
3482 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
3483 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
3484 	{ 0x0c, 5000 }	/* FAST-20 */
3485 };
3486 
3487 /*
3488  * Return the frequency in kHz corresponding to the given
3489  * sync period factor.
3490  */
3491 u_int
3492 scsi_calc_syncsrate(u_int period_factor)
3493 {
3494 	int i;
3495 	int num_syncrates;
3496 
3497 	/*
3498 	 * It's a bug if period is zero, but if it is anyway, don't
3499 	 * die with a divide fault- instead return something which
3500 	 * 'approximates' async
3501 	 */
3502 	if (period_factor == 0) {
3503 		return (3300);
3504 	}
3505 
3506 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3507 	/* See if the period is in the "exception" table */
3508 	for (i = 0; i < num_syncrates; i++) {
3509 
3510 		if (period_factor == scsi_syncrates[i].period_factor) {
3511 			/* Period in kHz */
3512 			return (100000000 / scsi_syncrates[i].period);
3513 		}
3514 	}
3515 
3516 	/*
3517 	 * Wasn't in the table, so use the standard
3518 	 * 4 times conversion.
3519 	 */
3520 	return (10000000 / (period_factor * 4 * 10));
3521 }
3522 
3523 /*
3524  * Return the SCSI sync parameter that corresponsd to
3525  * the passed in period in 10ths of ns.
3526  */
3527 u_int
3528 scsi_calc_syncparam(u_int period)
3529 {
3530 	int i;
3531 	int num_syncrates;
3532 
3533 	if (period == 0)
3534 		return (~0);	/* Async */
3535 
3536 	/* Adjust for exception table being in 100ths. */
3537 	period *= 10;
3538 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3539 	/* See if the period is in the "exception" table */
3540 	for (i = 0; i < num_syncrates; i++) {
3541 
3542 		if (period <= scsi_syncrates[i].period) {
3543 			/* Period in 100ths of ns */
3544 			return (scsi_syncrates[i].period_factor);
3545 		}
3546 	}
3547 
3548 	/*
3549 	 * Wasn't in the table, so use the standard
3550 	 * 1/4 period in ns conversion.
3551 	 */
3552 	return (period/400);
3553 }
3554 
3555 uint8_t *
3556 scsi_get_sas_addr(struct scsi_vpd_device_id *id, uint32_t len)
3557 {
3558 	uint8_t *bufp, *buf_end;
3559 	struct scsi_vpd_id_descriptor *descr;
3560 	struct scsi_vpd_id_naa_basic *naa;
3561 
3562 	bufp = buf_end = (uint8_t *)id;
3563 	bufp += SVPD_DEVICE_ID_HDR_LEN;
3564 	buf_end += len;
3565 	while (bufp < buf_end) {
3566 		descr = (struct scsi_vpd_id_descriptor *)bufp;
3567 		bufp += SVPD_DEVICE_ID_DESC_HDR_LEN;
3568 		/* Right now, we only care about SAS NAA IEEE Reg addrs */
3569 		if (((descr->id_type & SVPD_ID_PIV) != 0)
3570 		 && (descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) ==
3571 		     SCSI_PROTO_SAS
3572 		 && (descr->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_NAA){
3573 			naa = (struct scsi_vpd_id_naa_basic *)bufp;
3574 			if ((naa->naa >> 4) == SVPD_ID_NAA_IEEE_REG)
3575 				return bufp;
3576 		}
3577 		bufp += descr->length;
3578 	}
3579 
3580 	return NULL;
3581 }
3582 
3583 void
3584 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
3585 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
3586 		     u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
3587 {
3588 	struct scsi_test_unit_ready *scsi_cmd;
3589 
3590 	cam_fill_csio(csio,
3591 		      retries,
3592 		      cbfcnp,
3593 		      CAM_DIR_NONE,
3594 		      tag_action,
3595 		      /*data_ptr*/NULL,
3596 		      /*dxfer_len*/0,
3597 		      sense_len,
3598 		      sizeof(*scsi_cmd),
3599 		      timeout);
3600 
3601 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
3602 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3603 	scsi_cmd->opcode = TEST_UNIT_READY;
3604 }
3605 
3606 void
3607 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
3608 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3609 		   void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
3610 		   u_int8_t sense_len, u_int32_t timeout)
3611 {
3612 	struct scsi_request_sense *scsi_cmd;
3613 
3614 	cam_fill_csio(csio,
3615 		      retries,
3616 		      cbfcnp,
3617 		      CAM_DIR_IN,
3618 		      tag_action,
3619 		      data_ptr,
3620 		      dxfer_len,
3621 		      sense_len,
3622 		      sizeof(*scsi_cmd),
3623 		      timeout);
3624 
3625 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
3626 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3627 	scsi_cmd->opcode = REQUEST_SENSE;
3628 	scsi_cmd->length = dxfer_len;
3629 }
3630 
3631 void
3632 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
3633 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
3634 	     u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
3635 	     int evpd, u_int8_t page_code, u_int8_t sense_len,
3636 	     u_int32_t timeout)
3637 {
3638 	struct scsi_inquiry *scsi_cmd;
3639 
3640 	cam_fill_csio(csio,
3641 		      retries,
3642 		      cbfcnp,
3643 		      /*flags*/CAM_DIR_IN,
3644 		      tag_action,
3645 		      /*data_ptr*/inq_buf,
3646 		      /*dxfer_len*/inq_len,
3647 		      sense_len,
3648 		      sizeof(*scsi_cmd),
3649 		      timeout);
3650 
3651 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
3652 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3653 	scsi_cmd->opcode = INQUIRY;
3654 	if (evpd) {
3655 		scsi_cmd->byte2 |= SI_EVPD;
3656 		scsi_cmd->page_code = page_code;
3657 	}
3658 	/*
3659 	 * A 'transfer units' count of 256 is coded as
3660 	 * zero for all commands with a single byte count
3661 	 * field.
3662 	 */
3663 	if (inq_len == 256)
3664 		inq_len = 0;
3665 	scsi_cmd->length = inq_len;
3666 }
3667 
3668 void
3669 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
3670 		void (*cbfcnp)(struct cam_periph *, union ccb *),
3671 		u_int8_t tag_action, int dbd, u_int8_t page_code,
3672 		u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3673 		u_int8_t sense_len, u_int32_t timeout)
3674 {
3675 
3676 	scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
3677 			    page_code, page, param_buf, param_len, 0,
3678 			    sense_len, timeout);
3679 }
3680 
3681 void
3682 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
3683 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
3684 		    u_int8_t tag_action, int dbd, u_int8_t page_code,
3685 		    u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3686 		    int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
3687 {
3688 	u_int8_t cdb_len;
3689 
3690 	/*
3691 	 * Use the smallest possible command to perform the operation.
3692 	 */
3693 	if ((param_len < 256)
3694 	 && (minimum_cmd_size < 10)) {
3695 		/*
3696 		 * We can fit in a 6 byte cdb.
3697 		 */
3698 		struct scsi_mode_sense_6 *scsi_cmd;
3699 
3700 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
3701 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3702 		scsi_cmd->opcode = MODE_SENSE_6;
3703 		if (dbd != 0)
3704 			scsi_cmd->byte2 |= SMS_DBD;
3705 		scsi_cmd->page = page_code | page;
3706 		scsi_cmd->length = param_len;
3707 		cdb_len = sizeof(*scsi_cmd);
3708 	} else {
3709 		/*
3710 		 * Need a 10 byte cdb.
3711 		 */
3712 		struct scsi_mode_sense_10 *scsi_cmd;
3713 
3714 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
3715 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3716 		scsi_cmd->opcode = MODE_SENSE_10;
3717 		if (dbd != 0)
3718 			scsi_cmd->byte2 |= SMS_DBD;
3719 		scsi_cmd->page = page_code | page;
3720 		scsi_ulto2b(param_len, scsi_cmd->length);
3721 		cdb_len = sizeof(*scsi_cmd);
3722 	}
3723 	cam_fill_csio(csio,
3724 		      retries,
3725 		      cbfcnp,
3726 		      CAM_DIR_IN,
3727 		      tag_action,
3728 		      param_buf,
3729 		      param_len,
3730 		      sense_len,
3731 		      cdb_len,
3732 		      timeout);
3733 }
3734 
3735 void
3736 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
3737 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3738 		 u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3739 		 u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3740 		 u_int32_t timeout)
3741 {
3742 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
3743 			     scsi_page_fmt, save_pages, param_buf,
3744 			     param_len, 0, sense_len, timeout);
3745 }
3746 
3747 void
3748 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
3749 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
3750 		     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3751 		     u_int8_t *param_buf, u_int32_t param_len,
3752 		     int minimum_cmd_size, u_int8_t sense_len,
3753 		     u_int32_t timeout)
3754 {
3755 	u_int8_t cdb_len;
3756 
3757 	/*
3758 	 * Use the smallest possible command to perform the operation.
3759 	 */
3760 	if ((param_len < 256)
3761 	 && (minimum_cmd_size < 10)) {
3762 		/*
3763 		 * We can fit in a 6 byte cdb.
3764 		 */
3765 		struct scsi_mode_select_6 *scsi_cmd;
3766 
3767 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
3768 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3769 		scsi_cmd->opcode = MODE_SELECT_6;
3770 		if (scsi_page_fmt != 0)
3771 			scsi_cmd->byte2 |= SMS_PF;
3772 		if (save_pages != 0)
3773 			scsi_cmd->byte2 |= SMS_SP;
3774 		scsi_cmd->length = param_len;
3775 		cdb_len = sizeof(*scsi_cmd);
3776 	} else {
3777 		/*
3778 		 * Need a 10 byte cdb.
3779 		 */
3780 		struct scsi_mode_select_10 *scsi_cmd;
3781 
3782 		scsi_cmd =
3783 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
3784 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3785 		scsi_cmd->opcode = MODE_SELECT_10;
3786 		if (scsi_page_fmt != 0)
3787 			scsi_cmd->byte2 |= SMS_PF;
3788 		if (save_pages != 0)
3789 			scsi_cmd->byte2 |= SMS_SP;
3790 		scsi_ulto2b(param_len, scsi_cmd->length);
3791 		cdb_len = sizeof(*scsi_cmd);
3792 	}
3793 	cam_fill_csio(csio,
3794 		      retries,
3795 		      cbfcnp,
3796 		      CAM_DIR_OUT,
3797 		      tag_action,
3798 		      param_buf,
3799 		      param_len,
3800 		      sense_len,
3801 		      cdb_len,
3802 		      timeout);
3803 }
3804 
3805 void
3806 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
3807 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
3808 	       u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
3809 	       int save_pages, int ppc, u_int32_t paramptr,
3810 	       u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3811 	       u_int32_t timeout)
3812 {
3813 	struct scsi_log_sense *scsi_cmd;
3814 	u_int8_t cdb_len;
3815 
3816 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
3817 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3818 	scsi_cmd->opcode = LOG_SENSE;
3819 	scsi_cmd->page = page_code | page;
3820 	if (save_pages != 0)
3821 		scsi_cmd->byte2 |= SLS_SP;
3822 	if (ppc != 0)
3823 		scsi_cmd->byte2 |= SLS_PPC;
3824 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
3825 	scsi_ulto2b(param_len, scsi_cmd->length);
3826 	cdb_len = sizeof(*scsi_cmd);
3827 
3828 	cam_fill_csio(csio,
3829 		      retries,
3830 		      cbfcnp,
3831 		      /*flags*/CAM_DIR_IN,
3832 		      tag_action,
3833 		      /*data_ptr*/param_buf,
3834 		      /*dxfer_len*/param_len,
3835 		      sense_len,
3836 		      cdb_len,
3837 		      timeout);
3838 }
3839 
3840 void
3841 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
3842 		void (*cbfcnp)(struct cam_periph *, union ccb *),
3843 		u_int8_t tag_action, u_int8_t page_code, int save_pages,
3844 		int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
3845 		u_int8_t sense_len, u_int32_t timeout)
3846 {
3847 	struct scsi_log_select *scsi_cmd;
3848 	u_int8_t cdb_len;
3849 
3850 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
3851 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3852 	scsi_cmd->opcode = LOG_SELECT;
3853 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
3854 	if (save_pages != 0)
3855 		scsi_cmd->byte2 |= SLS_SP;
3856 	if (pc_reset != 0)
3857 		scsi_cmd->byte2 |= SLS_PCR;
3858 	scsi_ulto2b(param_len, scsi_cmd->length);
3859 	cdb_len = sizeof(*scsi_cmd);
3860 
3861 	cam_fill_csio(csio,
3862 		      retries,
3863 		      cbfcnp,
3864 		      /*flags*/CAM_DIR_OUT,
3865 		      tag_action,
3866 		      /*data_ptr*/param_buf,
3867 		      /*dxfer_len*/param_len,
3868 		      sense_len,
3869 		      cdb_len,
3870 		      timeout);
3871 }
3872 
3873 /*
3874  * Prevent or allow the user to remove the media
3875  */
3876 void
3877 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
3878 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
3879 	     u_int8_t tag_action, u_int8_t action,
3880 	     u_int8_t sense_len, u_int32_t timeout)
3881 {
3882 	struct scsi_prevent *scsi_cmd;
3883 
3884 	cam_fill_csio(csio,
3885 		      retries,
3886 		      cbfcnp,
3887 		      /*flags*/CAM_DIR_NONE,
3888 		      tag_action,
3889 		      /*data_ptr*/NULL,
3890 		      /*dxfer_len*/0,
3891 		      sense_len,
3892 		      sizeof(*scsi_cmd),
3893 		      timeout);
3894 
3895 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
3896 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3897 	scsi_cmd->opcode = PREVENT_ALLOW;
3898 	scsi_cmd->how = action;
3899 }
3900 
3901 /* XXX allow specification of address and PMI bit and LBA */
3902 void
3903 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
3904 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3905 		   u_int8_t tag_action,
3906 		   struct scsi_read_capacity_data *rcap_buf,
3907 		   u_int8_t sense_len, u_int32_t timeout)
3908 {
3909 	struct scsi_read_capacity *scsi_cmd;
3910 
3911 	cam_fill_csio(csio,
3912 		      retries,
3913 		      cbfcnp,
3914 		      /*flags*/CAM_DIR_IN,
3915 		      tag_action,
3916 		      /*data_ptr*/(u_int8_t *)rcap_buf,
3917 		      /*dxfer_len*/sizeof(*rcap_buf),
3918 		      sense_len,
3919 		      sizeof(*scsi_cmd),
3920 		      timeout);
3921 
3922 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
3923 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3924 	scsi_cmd->opcode = READ_CAPACITY;
3925 }
3926 
3927 void
3928 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
3929 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
3930 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
3931 		      struct scsi_read_capacity_data_long *rcap_buf,
3932 		      uint8_t sense_len, uint32_t timeout)
3933 {
3934 	struct scsi_read_capacity_16 *scsi_cmd;
3935 
3936 
3937 	cam_fill_csio(csio,
3938 		      retries,
3939 		      cbfcnp,
3940 		      /*flags*/CAM_DIR_IN,
3941 		      tag_action,
3942 		      /*data_ptr*/(u_int8_t *)rcap_buf,
3943 		      /*dxfer_len*/sizeof(*rcap_buf),
3944 		      sense_len,
3945 		      sizeof(*scsi_cmd),
3946 		      timeout);
3947 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
3948 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3949 	scsi_cmd->opcode = SERVICE_ACTION_IN;
3950 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
3951 	scsi_u64to8b(lba, scsi_cmd->addr);
3952 	scsi_ulto4b(sizeof(*rcap_buf), scsi_cmd->alloc_len);
3953 	if (pmi)
3954 		reladr |= SRC16_PMI;
3955 	if (reladr)
3956 		reladr |= SRC16_RELADR;
3957 }
3958 
3959 void
3960 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
3961 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3962 		 u_int8_t tag_action, u_int8_t select_report,
3963 		 struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
3964 		 u_int8_t sense_len, u_int32_t timeout)
3965 {
3966 	struct scsi_report_luns *scsi_cmd;
3967 
3968 	cam_fill_csio(csio,
3969 		      retries,
3970 		      cbfcnp,
3971 		      /*flags*/CAM_DIR_IN,
3972 		      tag_action,
3973 		      /*data_ptr*/(u_int8_t *)rpl_buf,
3974 		      /*dxfer_len*/alloc_len,
3975 		      sense_len,
3976 		      sizeof(*scsi_cmd),
3977 		      timeout);
3978 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
3979 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3980 	scsi_cmd->opcode = REPORT_LUNS;
3981 	scsi_cmd->select_report = select_report;
3982 	scsi_ulto4b(alloc_len, scsi_cmd->length);
3983 }
3984 
3985 void
3986 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
3987 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3988 		 u_int8_t tag_action, u_int8_t pdf,
3989 		 void *buf, u_int32_t alloc_len,
3990 		 u_int8_t sense_len, u_int32_t timeout)
3991 {
3992 	struct scsi_target_group *scsi_cmd;
3993 
3994 	cam_fill_csio(csio,
3995 		      retries,
3996 		      cbfcnp,
3997 		      /*flags*/CAM_DIR_IN,
3998 		      tag_action,
3999 		      /*data_ptr*/(u_int8_t *)buf,
4000 		      /*dxfer_len*/alloc_len,
4001 		      sense_len,
4002 		      sizeof(*scsi_cmd),
4003 		      timeout);
4004 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
4005 	bzero(scsi_cmd, sizeof(*scsi_cmd));
4006 	scsi_cmd->opcode = MAINTENANCE_IN;
4007 	scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
4008 	scsi_ulto4b(alloc_len, scsi_cmd->length);
4009 }
4010 
4011 void
4012 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
4013 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
4014 		 u_int8_t tag_action, void *buf, u_int32_t alloc_len,
4015 		 u_int8_t sense_len, u_int32_t timeout)
4016 {
4017 	struct scsi_target_group *scsi_cmd;
4018 
4019 	cam_fill_csio(csio,
4020 		      retries,
4021 		      cbfcnp,
4022 		      /*flags*/CAM_DIR_OUT,
4023 		      tag_action,
4024 		      /*data_ptr*/(u_int8_t *)buf,
4025 		      /*dxfer_len*/alloc_len,
4026 		      sense_len,
4027 		      sizeof(*scsi_cmd),
4028 		      timeout);
4029 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
4030 	bzero(scsi_cmd, sizeof(*scsi_cmd));
4031 	scsi_cmd->opcode = MAINTENANCE_OUT;
4032 	scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
4033 	scsi_ulto4b(alloc_len, scsi_cmd->length);
4034 }
4035 
4036 /*
4037  * Syncronize the media to the contents of the cache for
4038  * the given lba/count pair.  Specifying 0/0 means sync
4039  * the whole cache.
4040  */
4041 void
4042 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
4043 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
4044 		       u_int8_t tag_action, u_int32_t begin_lba,
4045 		       u_int16_t lb_count, u_int8_t sense_len,
4046 		       u_int32_t timeout)
4047 {
4048 	struct scsi_sync_cache *scsi_cmd;
4049 
4050 	cam_fill_csio(csio,
4051 		      retries,
4052 		      cbfcnp,
4053 		      /*flags*/CAM_DIR_NONE,
4054 		      tag_action,
4055 		      /*data_ptr*/NULL,
4056 		      /*dxfer_len*/0,
4057 		      sense_len,
4058 		      sizeof(*scsi_cmd),
4059 		      timeout);
4060 
4061 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
4062 	bzero(scsi_cmd, sizeof(*scsi_cmd));
4063 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
4064 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
4065 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
4066 }
4067 
4068 void
4069 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
4070 		void (*cbfcnp)(struct cam_periph *, union ccb *),
4071 		u_int8_t tag_action, int readop, u_int8_t byte2,
4072 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
4073 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4074 		u_int32_t timeout)
4075 {
4076 	u_int8_t cdb_len;
4077 	/*
4078 	 * Use the smallest possible command to perform the operation
4079 	 * as some legacy hardware does not support the 10 byte commands.
4080 	 * If any of the bits in byte2 is set, we have to go with a larger
4081 	 * command.
4082 	 */
4083 	if ((minimum_cmd_size < 10)
4084 	 && ((lba & 0x1fffff) == lba)
4085 	 && ((block_count & 0xff) == block_count)
4086 	 && (byte2 == 0)) {
4087 		/*
4088 		 * We can fit in a 6 byte cdb.
4089 		 */
4090 		struct scsi_rw_6 *scsi_cmd;
4091 
4092 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
4093 		scsi_cmd->opcode = readop ? READ_6 : WRITE_6;
4094 		scsi_ulto3b(lba, scsi_cmd->addr);
4095 		scsi_cmd->length = block_count & 0xff;
4096 		scsi_cmd->control = 0;
4097 		cdb_len = sizeof(*scsi_cmd);
4098 
4099 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4100 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
4101 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4102 			   scsi_cmd->length, dxfer_len));
4103 	} else if ((minimum_cmd_size < 12)
4104 		&& ((block_count & 0xffff) == block_count)
4105 		&& ((lba & 0xffffffff) == lba)) {
4106 		/*
4107 		 * Need a 10 byte cdb.
4108 		 */
4109 		struct scsi_rw_10 *scsi_cmd;
4110 
4111 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
4112 		scsi_cmd->opcode = readop ? READ_10 : WRITE_10;
4113 		scsi_cmd->byte2 = byte2;
4114 		scsi_ulto4b(lba, scsi_cmd->addr);
4115 		scsi_cmd->reserved = 0;
4116 		scsi_ulto2b(block_count, scsi_cmd->length);
4117 		scsi_cmd->control = 0;
4118 		cdb_len = sizeof(*scsi_cmd);
4119 
4120 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4121 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
4122 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4123 			   scsi_cmd->addr[3], scsi_cmd->length[0],
4124 			   scsi_cmd->length[1], dxfer_len));
4125 	} else if ((minimum_cmd_size < 16)
4126 		&& ((block_count & 0xffffffff) == block_count)
4127 		&& ((lba & 0xffffffff) == lba)) {
4128 		/*
4129 		 * The block count is too big for a 10 byte CDB, use a 12
4130 		 * byte CDB.
4131 		 */
4132 		struct scsi_rw_12 *scsi_cmd;
4133 
4134 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
4135 		scsi_cmd->opcode = readop ? READ_12 : WRITE_12;
4136 		scsi_cmd->byte2 = byte2;
4137 		scsi_ulto4b(lba, scsi_cmd->addr);
4138 		scsi_cmd->reserved = 0;
4139 		scsi_ulto4b(block_count, scsi_cmd->length);
4140 		scsi_cmd->control = 0;
4141 		cdb_len = sizeof(*scsi_cmd);
4142 
4143 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4144 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
4145 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4146 			   scsi_cmd->addr[3], scsi_cmd->length[0],
4147 			   scsi_cmd->length[1], scsi_cmd->length[2],
4148 			   scsi_cmd->length[3], dxfer_len));
4149 	} else {
4150 		/*
4151 		 * 16 byte CDB.  We'll only get here if the LBA is larger
4152 		 * than 2^32, or if the user asks for a 16 byte command.
4153 		 */
4154 		struct scsi_rw_16 *scsi_cmd;
4155 
4156 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
4157 		scsi_cmd->opcode = readop ? READ_16 : WRITE_16;
4158 		scsi_cmd->byte2 = byte2;
4159 		scsi_u64to8b(lba, scsi_cmd->addr);
4160 		scsi_cmd->reserved = 0;
4161 		scsi_ulto4b(block_count, scsi_cmd->length);
4162 		scsi_cmd->control = 0;
4163 		cdb_len = sizeof(*scsi_cmd);
4164 	}
4165 	cam_fill_csio(csio,
4166 		      retries,
4167 		      cbfcnp,
4168 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
4169 		      tag_action,
4170 		      data_ptr,
4171 		      dxfer_len,
4172 		      sense_len,
4173 		      cdb_len,
4174 		      timeout);
4175 }
4176 
4177 void
4178 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
4179 		void (*cbfcnp)(struct cam_periph *, union ccb *),
4180 		u_int8_t tag_action, int start, int load_eject,
4181 		int immediate, u_int8_t sense_len, u_int32_t timeout)
4182 {
4183 	struct scsi_start_stop_unit *scsi_cmd;
4184 	int extra_flags = 0;
4185 
4186 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
4187 	bzero(scsi_cmd, sizeof(*scsi_cmd));
4188 	scsi_cmd->opcode = START_STOP_UNIT;
4189 	if (start != 0) {
4190 		scsi_cmd->how |= SSS_START;
4191 		/* it takes a lot of power to start a drive */
4192 		extra_flags |= CAM_HIGH_POWER;
4193 	}
4194 	if (load_eject != 0)
4195 		scsi_cmd->how |= SSS_LOEJ;
4196 	if (immediate != 0)
4197 		scsi_cmd->byte2 |= SSS_IMMED;
4198 
4199 	cam_fill_csio(csio,
4200 		      retries,
4201 		      cbfcnp,
4202 		      /*flags*/CAM_DIR_NONE | extra_flags,
4203 		      tag_action,
4204 		      /*data_ptr*/NULL,
4205 		      /*dxfer_len*/0,
4206 		      sense_len,
4207 		      sizeof(*scsi_cmd),
4208 		      timeout);
4209 
4210 }
4211 
4212 
4213 /*
4214  * Try make as good a match as possible with
4215  * available sub drivers
4216  */
4217 int
4218 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4219 {
4220 	struct scsi_inquiry_pattern *entry;
4221 	struct scsi_inquiry_data *inq;
4222 
4223 	entry = (struct scsi_inquiry_pattern *)table_entry;
4224 	inq = (struct scsi_inquiry_data *)inqbuffer;
4225 
4226 	if (((SID_TYPE(inq) == entry->type)
4227 	  || (entry->type == T_ANY))
4228 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4229 				   : entry->media_type & SIP_MEDIA_FIXED)
4230 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4231 	 && (cam_strmatch(inq->product, entry->product,
4232 			  sizeof(inq->product)) == 0)
4233 	 && (cam_strmatch(inq->revision, entry->revision,
4234 			  sizeof(inq->revision)) == 0)) {
4235 		return (0);
4236 	}
4237         return (-1);
4238 }
4239 
4240 /*
4241  * Try make as good a match as possible with
4242  * available sub drivers
4243  */
4244 int
4245 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4246 {
4247 	struct scsi_static_inquiry_pattern *entry;
4248 	struct scsi_inquiry_data *inq;
4249 
4250 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
4251 	inq = (struct scsi_inquiry_data *)inqbuffer;
4252 
4253 	if (((SID_TYPE(inq) == entry->type)
4254 	  || (entry->type == T_ANY))
4255 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4256 				   : entry->media_type & SIP_MEDIA_FIXED)
4257 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4258 	 && (cam_strmatch(inq->product, entry->product,
4259 			  sizeof(inq->product)) == 0)
4260 	 && (cam_strmatch(inq->revision, entry->revision,
4261 			  sizeof(inq->revision)) == 0)) {
4262 		return (0);
4263 	}
4264         return (-1);
4265 }
4266 
4267 #ifdef _KERNEL
4268 static void
4269 init_scsi_delay(void)
4270 {
4271 	int delay;
4272 
4273 	delay = SCSI_DELAY;
4274 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
4275 
4276 	if (set_scsi_delay(delay) != 0) {
4277 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
4278 		set_scsi_delay(SCSI_DELAY);
4279 	}
4280 }
4281 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
4282 
4283 static int
4284 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
4285 {
4286 	int error, delay;
4287 
4288 	delay = scsi_delay;
4289 	error = sysctl_handle_int(oidp, &delay, 0, req);
4290 	if (error != 0 || req->newptr == NULL)
4291 		return (error);
4292 	return (set_scsi_delay(delay));
4293 }
4294 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
4295     0, 0, sysctl_scsi_delay, "I",
4296     "Delay to allow devices to settle after a SCSI bus reset (ms)");
4297 
4298 static int
4299 set_scsi_delay(int delay)
4300 {
4301 	/*
4302          * If someone sets this to 0, we assume that they want the
4303          * minimum allowable bus settle delay.
4304 	 */
4305 	if (delay == 0) {
4306 		printf("cam: using minimum scsi_delay (%dms)\n",
4307 		    SCSI_MIN_DELAY);
4308 		delay = SCSI_MIN_DELAY;
4309 	}
4310 	if (delay < SCSI_MIN_DELAY)
4311 		return (EINVAL);
4312 	scsi_delay = delay;
4313 	return (0);
4314 }
4315 #endif /* _KERNEL */
4316