xref: /freebsd/sys/cam/scsi/scsi_all.c (revision 9bd497b8354567454e075076d40c996e21bd6095)
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 	/*
3003 	 * Get the device information.
3004 	 */
3005 	xpt_setup_ccb(&cgd.ccb_h,
3006 		      csio->ccb_h.path,
3007 		      CAM_PRIORITY_NORMAL);
3008 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3009 	xpt_action((union ccb *)&cgd);
3010 
3011 	/*
3012 	 * If the device is unconfigured, just pretend that it is a hard
3013 	 * drive.  scsi_op_desc() needs this.
3014 	 */
3015 	if (cgd.ccb_h.status == CAM_DEV_NOT_THERE)
3016 		cgd.inq_data.device = T_DIRECT;
3017 
3018 	inq_data = &cgd.inq_data;
3019 
3020 #else /* !_KERNEL */
3021 
3022 	inq_data = &device->inq_data;
3023 
3024 #endif /* _KERNEL/!_KERNEL */
3025 
3026 	if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3027 		sbuf_printf(sb, "%s. CDB: %s",
3028 			    scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data),
3029 			    scsi_cdb_string(csio->cdb_io.cdb_ptr, cdb_str,
3030 					    sizeof(cdb_str)));
3031 	} else {
3032 		sbuf_printf(sb, "%s. CDB: %s",
3033 			    scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data),
3034 			    scsi_cdb_string(csio->cdb_io.cdb_bytes, cdb_str,
3035 					    sizeof(cdb_str)));
3036 	}
3037 
3038 	return(0);
3039 }
3040 
3041 
3042 /*
3043  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
3044  */
3045 #ifdef _KERNEL
3046 int
3047 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
3048 		scsi_sense_string_flags flags)
3049 #else /* !_KERNEL */
3050 int
3051 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
3052 		struct sbuf *sb, scsi_sense_string_flags flags)
3053 #endif /* _KERNEL/!_KERNEL */
3054 {
3055 	struct	  scsi_sense_data *sense;
3056 	struct	  scsi_inquiry_data *inq_data;
3057 #ifdef _KERNEL
3058 	struct	  ccb_getdev cgd;
3059 #endif /* _KERNEL */
3060 	u_int32_t info;
3061 	int	  error_code;
3062 	int	  sense_key;
3063 	int	  asc, ascq;
3064 	char	  path_str[64];
3065 
3066 #ifndef _KERNEL
3067 	if (device == NULL)
3068 		return(-1);
3069 #endif /* !_KERNEL */
3070 	if ((csio == NULL) || (sb == NULL))
3071 		return(-1);
3072 
3073 	/*
3074 	 * If the CDB is a physical address, we can't deal with it..
3075 	 */
3076 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
3077 		flags &= ~SSS_FLAG_PRINT_COMMAND;
3078 
3079 #ifdef _KERNEL
3080 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
3081 #else /* !_KERNEL */
3082 	cam_path_string(device, path_str, sizeof(path_str));
3083 #endif /* _KERNEL/!_KERNEL */
3084 
3085 #ifdef _KERNEL
3086 	/*
3087 	 * Get the device information.
3088 	 */
3089 	xpt_setup_ccb(&cgd.ccb_h,
3090 		      csio->ccb_h.path,
3091 		      CAM_PRIORITY_NORMAL);
3092 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
3093 	xpt_action((union ccb *)&cgd);
3094 
3095 	/*
3096 	 * If the device is unconfigured, just pretend that it is a hard
3097 	 * drive.  scsi_op_desc() needs this.
3098 	 */
3099 	if (cgd.ccb_h.status == CAM_DEV_NOT_THERE)
3100 		cgd.inq_data.device = T_DIRECT;
3101 
3102 	inq_data = &cgd.inq_data;
3103 
3104 #else /* !_KERNEL */
3105 
3106 	inq_data = &device->inq_data;
3107 
3108 #endif /* _KERNEL/!_KERNEL */
3109 
3110 	sense = NULL;
3111 
3112 	if (flags & SSS_FLAG_PRINT_COMMAND) {
3113 
3114 		sbuf_cat(sb, path_str);
3115 
3116 #ifdef _KERNEL
3117 		scsi_command_string(csio, sb);
3118 #else /* !_KERNEL */
3119 		scsi_command_string(device, csio, sb);
3120 #endif /* _KERNEL/!_KERNEL */
3121 		sbuf_printf(sb, "\n");
3122 	}
3123 
3124 	/*
3125 	 * If the sense data is a physical pointer, forget it.
3126 	 */
3127 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
3128 		if (csio->ccb_h.flags & CAM_SENSE_PHYS)
3129 			return(-1);
3130 		else {
3131 			/*
3132 			 * bcopy the pointer to avoid unaligned access
3133 			 * errors on finicky architectures.  We don't
3134 			 * ensure that the sense data is pointer aligned.
3135 			 */
3136 			bcopy(&csio->sense_data, &sense,
3137 			      sizeof(struct scsi_sense_data *));
3138 		}
3139 	} else {
3140 		/*
3141 		 * If the physical sense flag is set, but the sense pointer
3142 		 * is not also set, we assume that the user is an idiot and
3143 		 * return.  (Well, okay, it could be that somehow, the
3144 		 * entire csio is physical, but we would have probably core
3145 		 * dumped on one of the bogus pointer deferences above
3146 		 * already.)
3147 		 */
3148 		if (csio->ccb_h.flags & CAM_SENSE_PHYS)
3149 			return(-1);
3150 		else
3151 			sense = &csio->sense_data;
3152 	}
3153 
3154 
3155 	sbuf_cat(sb, path_str);
3156 
3157 	error_code = sense->error_code & SSD_ERRCODE;
3158 	sense_key = sense->flags & SSD_KEY;
3159 
3160 	switch (error_code) {
3161 	case SSD_DEFERRED_ERROR:
3162 		sbuf_printf(sb, "Deferred Error: ");
3163 
3164 		/* FALLTHROUGH */
3165 	case SSD_CURRENT_ERROR:
3166 	{
3167 		const char *sense_key_desc;
3168 		const char *asc_desc;
3169 
3170 		asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
3171 		ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
3172 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
3173 				&sense_key_desc, &asc_desc);
3174 		sbuf_cat(sb, sense_key_desc);
3175 
3176 		info = scsi_4btoul(sense->info);
3177 
3178 		if (sense->error_code & SSD_ERRCODE_VALID) {
3179 
3180 			switch (sense_key) {
3181 			case SSD_KEY_NOT_READY:
3182 			case SSD_KEY_ILLEGAL_REQUEST:
3183 			case SSD_KEY_UNIT_ATTENTION:
3184 			case SSD_KEY_DATA_PROTECT:
3185 				break;
3186 			case SSD_KEY_BLANK_CHECK:
3187 				sbuf_printf(sb, " req sz: %d (decimal)", info);
3188 				break;
3189 			default:
3190 				if (info) {
3191 					if (sense->flags & SSD_ILI) {
3192 						sbuf_printf(sb, " ILI (length "
3193 							"mismatch): %d", info);
3194 
3195 					} else {
3196 						sbuf_printf(sb, " info:%x",
3197 							    info);
3198 					}
3199 				}
3200 			}
3201 		} else if (info) {
3202 			sbuf_printf(sb, " info?:%x", info);
3203 		}
3204 
3205 		if (sense->extra_len >= 4) {
3206 			if (bcmp(sense->cmd_spec_info, "\0\0\0\0", 4)) {
3207 				sbuf_printf(sb, " csi:%x,%x,%x,%x",
3208 					    sense->cmd_spec_info[0],
3209 					    sense->cmd_spec_info[1],
3210 					    sense->cmd_spec_info[2],
3211 					    sense->cmd_spec_info[3]);
3212 			}
3213 		}
3214 
3215 		sbuf_printf(sb, " asc:%x,%x\n%s%s", asc, ascq,
3216 			    path_str, asc_desc);
3217 
3218 		if (sense->extra_len >= 7 && sense->fru) {
3219 			sbuf_printf(sb, " field replaceable unit: %x",
3220 				    sense->fru);
3221 		}
3222 
3223 		if ((sense->extra_len >= 10)
3224 		 && (sense->sense_key_spec[0] & SSD_SCS_VALID) != 0) {
3225 			switch(sense_key) {
3226 			case SSD_KEY_ILLEGAL_REQUEST: {
3227 				int bad_command;
3228 				char tmpstr2[40];
3229 
3230 				if (sense->sense_key_spec[0] & 0x40)
3231 					bad_command = 1;
3232 				else
3233 					bad_command = 0;
3234 
3235 				tmpstr2[0] = '\0';
3236 
3237 				/* Bit pointer is valid */
3238 				if (sense->sense_key_spec[0] & 0x08)
3239 					snprintf(tmpstr2, sizeof(tmpstr2),
3240 						 "bit %d ",
3241 						sense->sense_key_spec[0] & 0x7);
3242 				sbuf_printf(sb, ": %s byte %d %sis invalid",
3243 					    bad_command ? "Command" : "Data",
3244 					    scsi_2btoul(
3245 					    &sense->sense_key_spec[1]),
3246 					    tmpstr2);
3247 				break;
3248 			}
3249 			case SSD_KEY_RECOVERED_ERROR:
3250 			case SSD_KEY_HARDWARE_ERROR:
3251 			case SSD_KEY_MEDIUM_ERROR:
3252 				sbuf_printf(sb, " actual retry count: %d",
3253 					    scsi_2btoul(
3254 					    &sense->sense_key_spec[1]));
3255 				break;
3256 			default:
3257 				sbuf_printf(sb, " sks:%#x,%#x",
3258 					    sense->sense_key_spec[0],
3259 					    scsi_2btoul(
3260 					    &sense->sense_key_spec[1]));
3261 				break;
3262 			}
3263 		}
3264 		break;
3265 
3266 	}
3267 	default:
3268 		sbuf_printf(sb, "Sense Error Code 0x%x", sense->error_code);
3269 		if (sense->error_code & SSD_ERRCODE_VALID) {
3270 			sbuf_printf(sb, " at block no. %d (decimal)",
3271 				    info = scsi_4btoul(sense->info));
3272 		}
3273 	}
3274 
3275 	sbuf_printf(sb, "\n");
3276 
3277 	return(0);
3278 }
3279 
3280 
3281 
3282 #ifdef _KERNEL
3283 char *
3284 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
3285 #else /* !_KERNEL */
3286 char *
3287 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
3288 		  char *str, int str_len)
3289 #endif /* _KERNEL/!_KERNEL */
3290 {
3291 	struct sbuf sb;
3292 
3293 	sbuf_new(&sb, str, str_len, 0);
3294 
3295 #ifdef _KERNEL
3296 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3297 #else /* !_KERNEL */
3298 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3299 #endif /* _KERNEL/!_KERNEL */
3300 
3301 	sbuf_finish(&sb);
3302 
3303 	return(sbuf_data(&sb));
3304 }
3305 
3306 #ifdef _KERNEL
3307 void
3308 scsi_sense_print(struct ccb_scsiio *csio)
3309 {
3310 	struct sbuf sb;
3311 	char str[512];
3312 
3313 	sbuf_new(&sb, str, sizeof(str), 0);
3314 
3315 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3316 
3317 	sbuf_finish(&sb);
3318 
3319 	printf("%s", sbuf_data(&sb));
3320 }
3321 
3322 #else /* !_KERNEL */
3323 void
3324 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
3325 		 FILE *ofile)
3326 {
3327 	struct sbuf sb;
3328 	char str[512];
3329 
3330 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
3331 		return;
3332 
3333 	sbuf_new(&sb, str, sizeof(str), 0);
3334 
3335 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3336 
3337 	sbuf_finish(&sb);
3338 
3339 	fprintf(ofile, "%s", sbuf_data(&sb));
3340 }
3341 
3342 #endif /* _KERNEL/!_KERNEL */
3343 
3344 /*
3345  * This function currently requires at least 36 bytes, or
3346  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
3347  * function needs more or less data in the future, another length should be
3348  * defined in scsi_all.h to indicate the minimum amount of data necessary
3349  * for this routine to function properly.
3350  */
3351 void
3352 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
3353 {
3354 	u_int8_t type;
3355 	char *dtype, *qtype;
3356 	char vendor[16], product[48], revision[16], rstr[4];
3357 
3358 	type = SID_TYPE(inq_data);
3359 
3360 	/*
3361 	 * Figure out basic device type and qualifier.
3362 	 */
3363 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
3364 		qtype = "(vendor-unique qualifier)";
3365 	} else {
3366 		switch (SID_QUAL(inq_data)) {
3367 		case SID_QUAL_LU_CONNECTED:
3368 			qtype = "";
3369 			break;
3370 
3371 		case SID_QUAL_LU_OFFLINE:
3372 			qtype = "(offline)";
3373 			break;
3374 
3375 		case SID_QUAL_RSVD:
3376 			qtype = "(reserved qualifier)";
3377 			break;
3378 		default:
3379 		case SID_QUAL_BAD_LU:
3380 			qtype = "(LUN not supported)";
3381 			break;
3382 		}
3383 	}
3384 
3385 	switch (type) {
3386 	case T_DIRECT:
3387 		dtype = "Direct Access";
3388 		break;
3389 	case T_SEQUENTIAL:
3390 		dtype = "Sequential Access";
3391 		break;
3392 	case T_PRINTER:
3393 		dtype = "Printer";
3394 		break;
3395 	case T_PROCESSOR:
3396 		dtype = "Processor";
3397 		break;
3398 	case T_WORM:
3399 		dtype = "WORM";
3400 		break;
3401 	case T_CDROM:
3402 		dtype = "CD-ROM";
3403 		break;
3404 	case T_SCANNER:
3405 		dtype = "Scanner";
3406 		break;
3407 	case T_OPTICAL:
3408 		dtype = "Optical";
3409 		break;
3410 	case T_CHANGER:
3411 		dtype = "Changer";
3412 		break;
3413 	case T_COMM:
3414 		dtype = "Communication";
3415 		break;
3416 	case T_STORARRAY:
3417 		dtype = "Storage Array";
3418 		break;
3419 	case T_ENCLOSURE:
3420 		dtype = "Enclosure Services";
3421 		break;
3422 	case T_RBC:
3423 		dtype = "Simplified Direct Access";
3424 		break;
3425 	case T_OCRW:
3426 		dtype = "Optical Card Read/Write";
3427 		break;
3428 	case T_OSD:
3429 		dtype = "Object-Based Storage";
3430 		break;
3431 	case T_ADC:
3432 		dtype = "Automation/Drive Interface";
3433 		break;
3434 	case T_NODEVICE:
3435 		dtype = "Uninstalled";
3436 		break;
3437 	default:
3438 		dtype = "unknown";
3439 		break;
3440 	}
3441 
3442 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
3443 		   sizeof(vendor));
3444 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
3445 		   sizeof(product));
3446 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
3447 		   sizeof(revision));
3448 
3449 	if (SID_ANSI_REV(inq_data) == SCSI_REV_CCS)
3450 		bcopy("CCS", rstr, 4);
3451 	else
3452 		snprintf(rstr, sizeof (rstr), "%d", SID_ANSI_REV(inq_data));
3453 	printf("<%s %s %s> %s %s SCSI-%s device %s\n",
3454 	       vendor, product, revision,
3455 	       SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
3456 	       dtype, rstr, qtype);
3457 }
3458 
3459 /*
3460  * Table of syncrates that don't follow the "divisible by 4"
3461  * rule. This table will be expanded in future SCSI specs.
3462  */
3463 static struct {
3464 	u_int period_factor;
3465 	u_int period;	/* in 100ths of ns */
3466 } scsi_syncrates[] = {
3467 	{ 0x08, 625 },	/* FAST-160 */
3468 	{ 0x09, 1250 },	/* FAST-80 */
3469 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
3470 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
3471 	{ 0x0c, 5000 }	/* FAST-20 */
3472 };
3473 
3474 /*
3475  * Return the frequency in kHz corresponding to the given
3476  * sync period factor.
3477  */
3478 u_int
3479 scsi_calc_syncsrate(u_int period_factor)
3480 {
3481 	int i;
3482 	int num_syncrates;
3483 
3484 	/*
3485 	 * It's a bug if period is zero, but if it is anyway, don't
3486 	 * die with a divide fault- instead return something which
3487 	 * 'approximates' async
3488 	 */
3489 	if (period_factor == 0) {
3490 		return (3300);
3491 	}
3492 
3493 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3494 	/* See if the period is in the "exception" table */
3495 	for (i = 0; i < num_syncrates; i++) {
3496 
3497 		if (period_factor == scsi_syncrates[i].period_factor) {
3498 			/* Period in kHz */
3499 			return (100000000 / scsi_syncrates[i].period);
3500 		}
3501 	}
3502 
3503 	/*
3504 	 * Wasn't in the table, so use the standard
3505 	 * 4 times conversion.
3506 	 */
3507 	return (10000000 / (period_factor * 4 * 10));
3508 }
3509 
3510 /*
3511  * Return the SCSI sync parameter that corresponsd to
3512  * the passed in period in 10ths of ns.
3513  */
3514 u_int
3515 scsi_calc_syncparam(u_int period)
3516 {
3517 	int i;
3518 	int num_syncrates;
3519 
3520 	if (period == 0)
3521 		return (~0);	/* Async */
3522 
3523 	/* Adjust for exception table being in 100ths. */
3524 	period *= 10;
3525 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3526 	/* See if the period is in the "exception" table */
3527 	for (i = 0; i < num_syncrates; i++) {
3528 
3529 		if (period <= scsi_syncrates[i].period) {
3530 			/* Period in 100ths of ns */
3531 			return (scsi_syncrates[i].period_factor);
3532 		}
3533 	}
3534 
3535 	/*
3536 	 * Wasn't in the table, so use the standard
3537 	 * 1/4 period in ns conversion.
3538 	 */
3539 	return (period/400);
3540 }
3541 
3542 void
3543 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
3544 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
3545 		     u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
3546 {
3547 	struct scsi_test_unit_ready *scsi_cmd;
3548 
3549 	cam_fill_csio(csio,
3550 		      retries,
3551 		      cbfcnp,
3552 		      CAM_DIR_NONE,
3553 		      tag_action,
3554 		      /*data_ptr*/NULL,
3555 		      /*dxfer_len*/0,
3556 		      sense_len,
3557 		      sizeof(*scsi_cmd),
3558 		      timeout);
3559 
3560 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
3561 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3562 	scsi_cmd->opcode = TEST_UNIT_READY;
3563 }
3564 
3565 void
3566 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
3567 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3568 		   void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
3569 		   u_int8_t sense_len, u_int32_t timeout)
3570 {
3571 	struct scsi_request_sense *scsi_cmd;
3572 
3573 	cam_fill_csio(csio,
3574 		      retries,
3575 		      cbfcnp,
3576 		      CAM_DIR_IN,
3577 		      tag_action,
3578 		      data_ptr,
3579 		      dxfer_len,
3580 		      sense_len,
3581 		      sizeof(*scsi_cmd),
3582 		      timeout);
3583 
3584 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
3585 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3586 	scsi_cmd->opcode = REQUEST_SENSE;
3587 	scsi_cmd->length = dxfer_len;
3588 }
3589 
3590 void
3591 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
3592 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
3593 	     u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
3594 	     int evpd, u_int8_t page_code, u_int8_t sense_len,
3595 	     u_int32_t timeout)
3596 {
3597 	struct scsi_inquiry *scsi_cmd;
3598 
3599 	cam_fill_csio(csio,
3600 		      retries,
3601 		      cbfcnp,
3602 		      /*flags*/CAM_DIR_IN,
3603 		      tag_action,
3604 		      /*data_ptr*/inq_buf,
3605 		      /*dxfer_len*/inq_len,
3606 		      sense_len,
3607 		      sizeof(*scsi_cmd),
3608 		      timeout);
3609 
3610 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
3611 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3612 	scsi_cmd->opcode = INQUIRY;
3613 	if (evpd) {
3614 		scsi_cmd->byte2 |= SI_EVPD;
3615 		scsi_cmd->page_code = page_code;
3616 	}
3617 	/*
3618 	 * A 'transfer units' count of 256 is coded as
3619 	 * zero for all commands with a single byte count
3620 	 * field.
3621 	 */
3622 	if (inq_len == 256)
3623 		inq_len = 0;
3624 	scsi_cmd->length = inq_len;
3625 }
3626 
3627 void
3628 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
3629 		void (*cbfcnp)(struct cam_periph *, union ccb *),
3630 		u_int8_t tag_action, int dbd, u_int8_t page_code,
3631 		u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3632 		u_int8_t sense_len, u_int32_t timeout)
3633 {
3634 
3635 	scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
3636 			    page_code, page, param_buf, param_len, 0,
3637 			    sense_len, timeout);
3638 }
3639 
3640 void
3641 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
3642 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
3643 		    u_int8_t tag_action, int dbd, u_int8_t page_code,
3644 		    u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3645 		    int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
3646 {
3647 	u_int8_t cdb_len;
3648 
3649 	/*
3650 	 * Use the smallest possible command to perform the operation.
3651 	 */
3652 	if ((param_len < 256)
3653 	 && (minimum_cmd_size < 10)) {
3654 		/*
3655 		 * We can fit in a 6 byte cdb.
3656 		 */
3657 		struct scsi_mode_sense_6 *scsi_cmd;
3658 
3659 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
3660 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3661 		scsi_cmd->opcode = MODE_SENSE_6;
3662 		if (dbd != 0)
3663 			scsi_cmd->byte2 |= SMS_DBD;
3664 		scsi_cmd->page = page_code | page;
3665 		scsi_cmd->length = param_len;
3666 		cdb_len = sizeof(*scsi_cmd);
3667 	} else {
3668 		/*
3669 		 * Need a 10 byte cdb.
3670 		 */
3671 		struct scsi_mode_sense_10 *scsi_cmd;
3672 
3673 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
3674 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3675 		scsi_cmd->opcode = MODE_SENSE_10;
3676 		if (dbd != 0)
3677 			scsi_cmd->byte2 |= SMS_DBD;
3678 		scsi_cmd->page = page_code | page;
3679 		scsi_ulto2b(param_len, scsi_cmd->length);
3680 		cdb_len = sizeof(*scsi_cmd);
3681 	}
3682 	cam_fill_csio(csio,
3683 		      retries,
3684 		      cbfcnp,
3685 		      CAM_DIR_IN,
3686 		      tag_action,
3687 		      param_buf,
3688 		      param_len,
3689 		      sense_len,
3690 		      cdb_len,
3691 		      timeout);
3692 }
3693 
3694 void
3695 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
3696 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3697 		 u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3698 		 u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3699 		 u_int32_t timeout)
3700 {
3701 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
3702 			     scsi_page_fmt, save_pages, param_buf,
3703 			     param_len, 0, sense_len, timeout);
3704 }
3705 
3706 void
3707 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
3708 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
3709 		     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3710 		     u_int8_t *param_buf, u_int32_t param_len,
3711 		     int minimum_cmd_size, u_int8_t sense_len,
3712 		     u_int32_t timeout)
3713 {
3714 	u_int8_t cdb_len;
3715 
3716 	/*
3717 	 * Use the smallest possible command to perform the operation.
3718 	 */
3719 	if ((param_len < 256)
3720 	 && (minimum_cmd_size < 10)) {
3721 		/*
3722 		 * We can fit in a 6 byte cdb.
3723 		 */
3724 		struct scsi_mode_select_6 *scsi_cmd;
3725 
3726 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
3727 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3728 		scsi_cmd->opcode = MODE_SELECT_6;
3729 		if (scsi_page_fmt != 0)
3730 			scsi_cmd->byte2 |= SMS_PF;
3731 		if (save_pages != 0)
3732 			scsi_cmd->byte2 |= SMS_SP;
3733 		scsi_cmd->length = param_len;
3734 		cdb_len = sizeof(*scsi_cmd);
3735 	} else {
3736 		/*
3737 		 * Need a 10 byte cdb.
3738 		 */
3739 		struct scsi_mode_select_10 *scsi_cmd;
3740 
3741 		scsi_cmd =
3742 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
3743 		bzero(scsi_cmd, sizeof(*scsi_cmd));
3744 		scsi_cmd->opcode = MODE_SELECT_10;
3745 		if (scsi_page_fmt != 0)
3746 			scsi_cmd->byte2 |= SMS_PF;
3747 		if (save_pages != 0)
3748 			scsi_cmd->byte2 |= SMS_SP;
3749 		scsi_ulto2b(param_len, scsi_cmd->length);
3750 		cdb_len = sizeof(*scsi_cmd);
3751 	}
3752 	cam_fill_csio(csio,
3753 		      retries,
3754 		      cbfcnp,
3755 		      CAM_DIR_OUT,
3756 		      tag_action,
3757 		      param_buf,
3758 		      param_len,
3759 		      sense_len,
3760 		      cdb_len,
3761 		      timeout);
3762 }
3763 
3764 void
3765 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
3766 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
3767 	       u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
3768 	       int save_pages, int ppc, u_int32_t paramptr,
3769 	       u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3770 	       u_int32_t timeout)
3771 {
3772 	struct scsi_log_sense *scsi_cmd;
3773 	u_int8_t cdb_len;
3774 
3775 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
3776 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3777 	scsi_cmd->opcode = LOG_SENSE;
3778 	scsi_cmd->page = page_code | page;
3779 	if (save_pages != 0)
3780 		scsi_cmd->byte2 |= SLS_SP;
3781 	if (ppc != 0)
3782 		scsi_cmd->byte2 |= SLS_PPC;
3783 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
3784 	scsi_ulto2b(param_len, scsi_cmd->length);
3785 	cdb_len = sizeof(*scsi_cmd);
3786 
3787 	cam_fill_csio(csio,
3788 		      retries,
3789 		      cbfcnp,
3790 		      /*flags*/CAM_DIR_IN,
3791 		      tag_action,
3792 		      /*data_ptr*/param_buf,
3793 		      /*dxfer_len*/param_len,
3794 		      sense_len,
3795 		      cdb_len,
3796 		      timeout);
3797 }
3798 
3799 void
3800 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
3801 		void (*cbfcnp)(struct cam_periph *, union ccb *),
3802 		u_int8_t tag_action, u_int8_t page_code, int save_pages,
3803 		int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
3804 		u_int8_t sense_len, u_int32_t timeout)
3805 {
3806 	struct scsi_log_select *scsi_cmd;
3807 	u_int8_t cdb_len;
3808 
3809 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
3810 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3811 	scsi_cmd->opcode = LOG_SELECT;
3812 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
3813 	if (save_pages != 0)
3814 		scsi_cmd->byte2 |= SLS_SP;
3815 	if (pc_reset != 0)
3816 		scsi_cmd->byte2 |= SLS_PCR;
3817 	scsi_ulto2b(param_len, scsi_cmd->length);
3818 	cdb_len = sizeof(*scsi_cmd);
3819 
3820 	cam_fill_csio(csio,
3821 		      retries,
3822 		      cbfcnp,
3823 		      /*flags*/CAM_DIR_OUT,
3824 		      tag_action,
3825 		      /*data_ptr*/param_buf,
3826 		      /*dxfer_len*/param_len,
3827 		      sense_len,
3828 		      cdb_len,
3829 		      timeout);
3830 }
3831 
3832 /*
3833  * Prevent or allow the user to remove the media
3834  */
3835 void
3836 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
3837 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
3838 	     u_int8_t tag_action, u_int8_t action,
3839 	     u_int8_t sense_len, u_int32_t timeout)
3840 {
3841 	struct scsi_prevent *scsi_cmd;
3842 
3843 	cam_fill_csio(csio,
3844 		      retries,
3845 		      cbfcnp,
3846 		      /*flags*/CAM_DIR_NONE,
3847 		      tag_action,
3848 		      /*data_ptr*/NULL,
3849 		      /*dxfer_len*/0,
3850 		      sense_len,
3851 		      sizeof(*scsi_cmd),
3852 		      timeout);
3853 
3854 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
3855 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3856 	scsi_cmd->opcode = PREVENT_ALLOW;
3857 	scsi_cmd->how = action;
3858 }
3859 
3860 /* XXX allow specification of address and PMI bit and LBA */
3861 void
3862 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
3863 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3864 		   u_int8_t tag_action,
3865 		   struct scsi_read_capacity_data *rcap_buf,
3866 		   u_int8_t sense_len, u_int32_t timeout)
3867 {
3868 	struct scsi_read_capacity *scsi_cmd;
3869 
3870 	cam_fill_csio(csio,
3871 		      retries,
3872 		      cbfcnp,
3873 		      /*flags*/CAM_DIR_IN,
3874 		      tag_action,
3875 		      /*data_ptr*/(u_int8_t *)rcap_buf,
3876 		      /*dxfer_len*/sizeof(*rcap_buf),
3877 		      sense_len,
3878 		      sizeof(*scsi_cmd),
3879 		      timeout);
3880 
3881 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
3882 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3883 	scsi_cmd->opcode = READ_CAPACITY;
3884 }
3885 
3886 void
3887 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
3888 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
3889 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
3890 		      struct scsi_read_capacity_data_long *rcap_buf,
3891 		      uint8_t sense_len, uint32_t timeout)
3892 {
3893 	struct scsi_read_capacity_16 *scsi_cmd;
3894 
3895 
3896 	cam_fill_csio(csio,
3897 		      retries,
3898 		      cbfcnp,
3899 		      /*flags*/CAM_DIR_IN,
3900 		      tag_action,
3901 		      /*data_ptr*/(u_int8_t *)rcap_buf,
3902 		      /*dxfer_len*/sizeof(*rcap_buf),
3903 		      sense_len,
3904 		      sizeof(*scsi_cmd),
3905 		      timeout);
3906 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
3907 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3908 	scsi_cmd->opcode = SERVICE_ACTION_IN;
3909 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
3910 	scsi_u64to8b(lba, scsi_cmd->addr);
3911 	scsi_ulto4b(sizeof(*rcap_buf), scsi_cmd->alloc_len);
3912 	if (pmi)
3913 		reladr |= SRC16_PMI;
3914 	if (reladr)
3915 		reladr |= SRC16_RELADR;
3916 }
3917 
3918 void
3919 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
3920 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3921 		 u_int8_t tag_action, u_int8_t select_report,
3922 		 struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
3923 		 u_int8_t sense_len, u_int32_t timeout)
3924 {
3925 	struct scsi_report_luns *scsi_cmd;
3926 
3927 	cam_fill_csio(csio,
3928 		      retries,
3929 		      cbfcnp,
3930 		      /*flags*/CAM_DIR_IN,
3931 		      tag_action,
3932 		      /*data_ptr*/(u_int8_t *)rpl_buf,
3933 		      /*dxfer_len*/alloc_len,
3934 		      sense_len,
3935 		      sizeof(*scsi_cmd),
3936 		      timeout);
3937 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
3938 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3939 	scsi_cmd->opcode = REPORT_LUNS;
3940 	scsi_cmd->select_report = select_report;
3941 	scsi_ulto4b(alloc_len, scsi_cmd->length);
3942 }
3943 
3944 /*
3945  * Syncronize the media to the contents of the cache for
3946  * the given lba/count pair.  Specifying 0/0 means sync
3947  * the whole cache.
3948  */
3949 void
3950 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
3951 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
3952 		       u_int8_t tag_action, u_int32_t begin_lba,
3953 		       u_int16_t lb_count, u_int8_t sense_len,
3954 		       u_int32_t timeout)
3955 {
3956 	struct scsi_sync_cache *scsi_cmd;
3957 
3958 	cam_fill_csio(csio,
3959 		      retries,
3960 		      cbfcnp,
3961 		      /*flags*/CAM_DIR_NONE,
3962 		      tag_action,
3963 		      /*data_ptr*/NULL,
3964 		      /*dxfer_len*/0,
3965 		      sense_len,
3966 		      sizeof(*scsi_cmd),
3967 		      timeout);
3968 
3969 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
3970 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3971 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
3972 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
3973 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
3974 }
3975 
3976 void
3977 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3978 		void (*cbfcnp)(struct cam_periph *, union ccb *),
3979 		u_int8_t tag_action, int readop, u_int8_t byte2,
3980 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
3981 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
3982 		u_int32_t timeout)
3983 {
3984 	u_int8_t cdb_len;
3985 	/*
3986 	 * Use the smallest possible command to perform the operation
3987 	 * as some legacy hardware does not support the 10 byte commands.
3988 	 * If any of the bits in byte2 is set, we have to go with a larger
3989 	 * command.
3990 	 */
3991 	if ((minimum_cmd_size < 10)
3992 	 && ((lba & 0x1fffff) == lba)
3993 	 && ((block_count & 0xff) == block_count)
3994 	 && (byte2 == 0)) {
3995 		/*
3996 		 * We can fit in a 6 byte cdb.
3997 		 */
3998 		struct scsi_rw_6 *scsi_cmd;
3999 
4000 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
4001 		scsi_cmd->opcode = readop ? READ_6 : WRITE_6;
4002 		scsi_ulto3b(lba, scsi_cmd->addr);
4003 		scsi_cmd->length = block_count & 0xff;
4004 		scsi_cmd->control = 0;
4005 		cdb_len = sizeof(*scsi_cmd);
4006 
4007 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4008 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
4009 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4010 			   scsi_cmd->length, dxfer_len));
4011 	} else if ((minimum_cmd_size < 12)
4012 		&& ((block_count & 0xffff) == block_count)
4013 		&& ((lba & 0xffffffff) == lba)) {
4014 		/*
4015 		 * Need a 10 byte cdb.
4016 		 */
4017 		struct scsi_rw_10 *scsi_cmd;
4018 
4019 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
4020 		scsi_cmd->opcode = readop ? READ_10 : WRITE_10;
4021 		scsi_cmd->byte2 = byte2;
4022 		scsi_ulto4b(lba, scsi_cmd->addr);
4023 		scsi_cmd->reserved = 0;
4024 		scsi_ulto2b(block_count, scsi_cmd->length);
4025 		scsi_cmd->control = 0;
4026 		cdb_len = sizeof(*scsi_cmd);
4027 
4028 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4029 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
4030 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4031 			   scsi_cmd->addr[3], scsi_cmd->length[0],
4032 			   scsi_cmd->length[1], dxfer_len));
4033 	} else if ((minimum_cmd_size < 16)
4034 		&& ((block_count & 0xffffffff) == block_count)
4035 		&& ((lba & 0xffffffff) == lba)) {
4036 		/*
4037 		 * The block count is too big for a 10 byte CDB, use a 12
4038 		 * byte CDB.
4039 		 */
4040 		struct scsi_rw_12 *scsi_cmd;
4041 
4042 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
4043 		scsi_cmd->opcode = readop ? READ_12 : WRITE_12;
4044 		scsi_cmd->byte2 = byte2;
4045 		scsi_ulto4b(lba, scsi_cmd->addr);
4046 		scsi_cmd->reserved = 0;
4047 		scsi_ulto4b(block_count, scsi_cmd->length);
4048 		scsi_cmd->control = 0;
4049 		cdb_len = sizeof(*scsi_cmd);
4050 
4051 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4052 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
4053 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
4054 			   scsi_cmd->addr[3], scsi_cmd->length[0],
4055 			   scsi_cmd->length[1], scsi_cmd->length[2],
4056 			   scsi_cmd->length[3], dxfer_len));
4057 	} else {
4058 		/*
4059 		 * 16 byte CDB.  We'll only get here if the LBA is larger
4060 		 * than 2^32, or if the user asks for a 16 byte command.
4061 		 */
4062 		struct scsi_rw_16 *scsi_cmd;
4063 
4064 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
4065 		scsi_cmd->opcode = readop ? READ_16 : WRITE_16;
4066 		scsi_cmd->byte2 = byte2;
4067 		scsi_u64to8b(lba, scsi_cmd->addr);
4068 		scsi_cmd->reserved = 0;
4069 		scsi_ulto4b(block_count, scsi_cmd->length);
4070 		scsi_cmd->control = 0;
4071 		cdb_len = sizeof(*scsi_cmd);
4072 	}
4073 	cam_fill_csio(csio,
4074 		      retries,
4075 		      cbfcnp,
4076 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
4077 		      tag_action,
4078 		      data_ptr,
4079 		      dxfer_len,
4080 		      sense_len,
4081 		      cdb_len,
4082 		      timeout);
4083 }
4084 
4085 void
4086 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
4087 		void (*cbfcnp)(struct cam_periph *, union ccb *),
4088 		u_int8_t tag_action, int start, int load_eject,
4089 		int immediate, u_int8_t sense_len, u_int32_t timeout)
4090 {
4091 	struct scsi_start_stop_unit *scsi_cmd;
4092 	int extra_flags = 0;
4093 
4094 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
4095 	bzero(scsi_cmd, sizeof(*scsi_cmd));
4096 	scsi_cmd->opcode = START_STOP_UNIT;
4097 	if (start != 0) {
4098 		scsi_cmd->how |= SSS_START;
4099 		/* it takes a lot of power to start a drive */
4100 		extra_flags |= CAM_HIGH_POWER;
4101 	}
4102 	if (load_eject != 0)
4103 		scsi_cmd->how |= SSS_LOEJ;
4104 	if (immediate != 0)
4105 		scsi_cmd->byte2 |= SSS_IMMED;
4106 
4107 	cam_fill_csio(csio,
4108 		      retries,
4109 		      cbfcnp,
4110 		      /*flags*/CAM_DIR_NONE | extra_flags,
4111 		      tag_action,
4112 		      /*data_ptr*/NULL,
4113 		      /*dxfer_len*/0,
4114 		      sense_len,
4115 		      sizeof(*scsi_cmd),
4116 		      timeout);
4117 
4118 }
4119 
4120 
4121 /*
4122  * Try make as good a match as possible with
4123  * available sub drivers
4124  */
4125 int
4126 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4127 {
4128 	struct scsi_inquiry_pattern *entry;
4129 	struct scsi_inquiry_data *inq;
4130 
4131 	entry = (struct scsi_inquiry_pattern *)table_entry;
4132 	inq = (struct scsi_inquiry_data *)inqbuffer;
4133 
4134 	if (((SID_TYPE(inq) == entry->type)
4135 	  || (entry->type == T_ANY))
4136 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4137 				   : entry->media_type & SIP_MEDIA_FIXED)
4138 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4139 	 && (cam_strmatch(inq->product, entry->product,
4140 			  sizeof(inq->product)) == 0)
4141 	 && (cam_strmatch(inq->revision, entry->revision,
4142 			  sizeof(inq->revision)) == 0)) {
4143 		return (0);
4144 	}
4145         return (-1);
4146 }
4147 
4148 /*
4149  * Try make as good a match as possible with
4150  * available sub drivers
4151  */
4152 int
4153 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4154 {
4155 	struct scsi_static_inquiry_pattern *entry;
4156 	struct scsi_inquiry_data *inq;
4157 
4158 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
4159 	inq = (struct scsi_inquiry_data *)inqbuffer;
4160 
4161 	if (((SID_TYPE(inq) == entry->type)
4162 	  || (entry->type == T_ANY))
4163 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4164 				   : entry->media_type & SIP_MEDIA_FIXED)
4165 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4166 	 && (cam_strmatch(inq->product, entry->product,
4167 			  sizeof(inq->product)) == 0)
4168 	 && (cam_strmatch(inq->revision, entry->revision,
4169 			  sizeof(inq->revision)) == 0)) {
4170 		return (0);
4171 	}
4172         return (-1);
4173 }
4174 
4175 #ifdef _KERNEL
4176 static void
4177 init_scsi_delay(void)
4178 {
4179 	int delay;
4180 
4181 	delay = SCSI_DELAY;
4182 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
4183 
4184 	if (set_scsi_delay(delay) != 0) {
4185 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
4186 		set_scsi_delay(SCSI_DELAY);
4187 	}
4188 }
4189 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
4190 
4191 static int
4192 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
4193 {
4194 	int error, delay;
4195 
4196 	delay = scsi_delay;
4197 	error = sysctl_handle_int(oidp, &delay, 0, req);
4198 	if (error != 0 || req->newptr == NULL)
4199 		return (error);
4200 	return (set_scsi_delay(delay));
4201 }
4202 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
4203     0, 0, sysctl_scsi_delay, "I",
4204     "Delay to allow devices to settle after a SCSI bus reset (ms)");
4205 
4206 static int
4207 set_scsi_delay(int delay)
4208 {
4209 	/*
4210          * If someone sets this to 0, we assume that they want the
4211          * minimum allowable bus settle delay.
4212 	 */
4213 	if (delay == 0) {
4214 		printf("cam: using minimum scsi_delay (%dms)\n",
4215 		    SCSI_MIN_DELAY);
4216 		delay = SCSI_MIN_DELAY;
4217 	}
4218 	if (delay < SCSI_MIN_DELAY)
4219 		return (EINVAL);
4220 	scsi_delay = delay;
4221 	return (0);
4222 }
4223 #endif /* _KERNEL */
4224