xref: /freebsd/sys/cam/scsi/scsi_all.c (revision 10b59a9b4add0320d52c15ce057dd697261e7dfc)
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 #include <sys/types.h>
35 #include <sys/stdint.h>
36 
37 #ifdef _KERNEL
38 #include <opt_scsi.h>
39 
40 #include <sys/systm.h>
41 #include <sys/libkern.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #else
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #endif
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <sys/sbuf.h>
57 #ifndef _KERNEL
58 #include <camlib.h>
59 #include <stddef.h>
60 
61 #ifndef FALSE
62 #define FALSE   0
63 #endif /* FALSE */
64 #ifndef TRUE
65 #define TRUE    1
66 #endif /* TRUE */
67 #define ERESTART        -1              /* restart syscall */
68 #define EJUSTRETURN     -2              /* don't modify regs, just return */
69 #endif /* !_KERNEL */
70 
71 /*
72  * This is the default number of milliseconds we wait for devices to settle
73  * after a SCSI bus reset.
74  */
75 #ifndef SCSI_DELAY
76 #define SCSI_DELAY 2000
77 #endif
78 /*
79  * All devices need _some_ sort of bus settle delay, so we'll set it to
80  * a minimum value of 100ms. Note that this is pertinent only for SPI-
81  * not transport like Fibre Channel or iSCSI where 'delay' is completely
82  * meaningless.
83  */
84 #ifndef SCSI_MIN_DELAY
85 #define SCSI_MIN_DELAY 100
86 #endif
87 /*
88  * Make sure the user isn't using seconds instead of milliseconds.
89  */
90 #if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
91 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
92 #endif
93 
94 int scsi_delay;
95 
96 static int	ascentrycomp(const void *key, const void *member);
97 static int	senseentrycomp(const void *key, const void *member);
98 static void	fetchtableentries(int sense_key, int asc, int ascq,
99 				  struct scsi_inquiry_data *,
100 				  const struct sense_key_table_entry **,
101 				  const struct asc_table_entry **);
102 #ifdef _KERNEL
103 static void	init_scsi_delay(void);
104 static int	sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
105 static int	set_scsi_delay(int delay);
106 #endif
107 
108 #if !defined(SCSI_NO_OP_STRINGS)
109 
110 #define	D	(1 << T_DIRECT)
111 #define	T	(1 << T_SEQUENTIAL)
112 #define	L	(1 << T_PRINTER)
113 #define	P	(1 << T_PROCESSOR)
114 #define	W	(1 << T_WORM)
115 #define	R	(1 << T_CDROM)
116 #define	O	(1 << T_OPTICAL)
117 #define	M	(1 << T_CHANGER)
118 #define	A	(1 << T_STORARRAY)
119 #define	E	(1 << T_ENCLOSURE)
120 #define	B	(1 << T_RBC)
121 #define	K	(1 << T_OCRW)
122 #define	V	(1 << T_ADC)
123 #define	F	(1 << T_OSD)
124 #define	S	(1 << T_SCANNER)
125 #define	C	(1 << T_COMM)
126 
127 #define ALL	(D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
128 
129 static struct op_table_entry plextor_cd_ops[] = {
130 	{ 0xD8, R, "CD-DA READ" }
131 };
132 
133 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
134 	{
135 		/*
136 		 * I believe that 0xD8 is the Plextor proprietary command
137 		 * to read CD-DA data.  I'm not sure which Plextor CDROM
138 		 * models support the command, though.  I know for sure
139 		 * that the 4X, 8X, and 12X models do, and presumably the
140 		 * 12-20X does.  I don't know about any earlier models,
141 		 * though.  If anyone has any more complete information,
142 		 * feel free to change this quirk entry.
143 		 */
144 		{T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
145 		sizeof(plextor_cd_ops)/sizeof(struct op_table_entry),
146 		plextor_cd_ops
147 	}
148 };
149 
150 static struct op_table_entry scsi_op_codes[] = {
151 	/*
152 	 * From: http://www.t10.org/lists/op-num.txt
153 	 * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
154 	 *              and Jung-uk Kim (jkim@FreeBSD.org)
155 	 *
156 	 * Note:  order is important in this table, scsi_op_desc() currently
157 	 * depends on the opcodes in the table being in order to save
158 	 * search time.
159 	 * Note:  scanner and comm. devices are carried over from the previous
160 	 * version because they were removed in the latest spec.
161 	 */
162 	/* File: OP-NUM.TXT
163 	 *
164 	 * SCSI Operation Codes
165 	 * Numeric Sorted Listing
166 	 * as of  3/11/08
167 	 *
168 	 *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
169 	 *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
170 	 *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
171 	 *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
172 	 *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
173 	 *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
174 	 *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
175 	 *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
176 	 *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
177 	 *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
178 	 *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
179 	 *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
180 	 *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
181 	 *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
182 	 * OP  DTLPWROMAEBKVF  Description
183 	 * --  --------------  ---------------------------------------------- */
184 	/* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
185 	{ 0x00,	ALL, "TEST UNIT READY" },
186 	/* 01   M              REWIND */
187 	{ 0x01,	T, "REWIND" },
188 	/* 01  Z V ZZZZ        REZERO UNIT */
189 	{ 0x01,	D | W | R | O | M, "REZERO UNIT" },
190 	/* 02  VVVVVV V */
191 	/* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
192 	{ 0x03,	ALL, "REQUEST SENSE" },
193 	/* 04  M    OO         FORMAT UNIT */
194 	{ 0x04,	D | R | O, "FORMAT UNIT" },
195 	/* 04   O              FORMAT MEDIUM */
196 	{ 0x04,	T, "FORMAT MEDIUM" },
197 	/* 04    O             FORMAT */
198 	{ 0x04,	L, "FORMAT" },
199 	/* 05  VMVVVV V        READ BLOCK LIMITS */
200 	{ 0x05,	T, "READ BLOCK LIMITS" },
201 	/* 06  VVVVVV V */
202 	/* 07  OVV O OV        REASSIGN BLOCKS */
203 	{ 0x07,	D | W | O, "REASSIGN BLOCKS" },
204 	/* 07         O        INITIALIZE ELEMENT STATUS */
205 	{ 0x07,	M, "INITIALIZE ELEMENT STATUS" },
206 	/* 08  MOV O OV        READ(6) */
207 	{ 0x08,	D | T | W | O, "READ(6)" },
208 	/* 08     O            RECEIVE */
209 	{ 0x08,	P, "RECEIVE" },
210 	/* 08                  GET MESSAGE(6) */
211 	{ 0x08, C, "GET MESSAGE(6)" },
212 	/* 09  VVVVVV V */
213 	/* 0A  OO  O OV        WRITE(6) */
214 	{ 0x0A,	D | T | W | O, "WRITE(6)" },
215 	/* 0A     M            SEND(6) */
216 	{ 0x0A,	P, "SEND(6)" },
217 	/* 0A                  SEND MESSAGE(6) */
218 	{ 0x0A, C, "SEND MESSAGE(6)" },
219 	/* 0A    M             PRINT */
220 	{ 0x0A,	L, "PRINT" },
221 	/* 0B  Z   ZOZV        SEEK(6) */
222 	{ 0x0B,	D | W | R | O, "SEEK(6)" },
223 	/* 0B   O              SET CAPACITY */
224 	{ 0x0B,	T, "SET CAPACITY" },
225 	/* 0B    O             SLEW AND PRINT */
226 	{ 0x0B,	L, "SLEW AND PRINT" },
227 	/* 0C  VVVVVV V */
228 	/* 0D  VVVVVV V */
229 	/* 0E  VVVVVV V */
230 	/* 0F  VOVVVV V        READ REVERSE(6) */
231 	{ 0x0F,	T, "READ REVERSE(6)" },
232 	/* 10  VM VVV          WRITE FILEMARKS(6) */
233 	{ 0x10,	T, "WRITE FILEMARKS(6)" },
234 	/* 10    O             SYNCHRONIZE BUFFER */
235 	{ 0x10,	L, "SYNCHRONIZE BUFFER" },
236 	/* 11  VMVVVV          SPACE(6) */
237 	{ 0x11,	T, "SPACE(6)" },
238 	/* 12  MMMMMMMMMMMMMM  INQUIRY */
239 	{ 0x12,	ALL, "INQUIRY" },
240 	/* 13  V VVVV */
241 	/* 13   O              VERIFY(6) */
242 	{ 0x13,	T, "VERIFY(6)" },
243 	/* 14  VOOVVV          RECOVER BUFFERED DATA */
244 	{ 0x14,	T | L, "RECOVER BUFFERED DATA" },
245 	/* 15  OMO O OOOO OO   MODE SELECT(6) */
246 	{ 0x15,	ALL & ~(P | R | B | F), "MODE SELECT(6)" },
247 	/* 16  ZZMZO OOOZ O    RESERVE(6) */
248 	{ 0x16,	ALL & ~(R | B | V | F | C), "RESERVE(6)" },
249 	/* 16         Z        RESERVE ELEMENT(6) */
250 	{ 0x16,	M, "RESERVE ELEMENT(6)" },
251 	/* 17  ZZMZO OOOZ O    RELEASE(6) */
252 	{ 0x17,	ALL & ~(R | B | V | F | C), "RELEASE(6)" },
253 	/* 17         Z        RELEASE ELEMENT(6) */
254 	{ 0x17,	M, "RELEASE ELEMENT(6)" },
255 	/* 18  ZZZZOZO    Z    COPY */
256 	{ 0x18,	D | T | L | P | W | R | O | K | S, "COPY" },
257 	/* 19  VMVVVV          ERASE(6) */
258 	{ 0x19,	T, "ERASE(6)" },
259 	/* 1A  OMO O OOOO OO   MODE SENSE(6) */
260 	{ 0x1A,	ALL & ~(P | R | B | F), "MODE SENSE(6)" },
261 	/* 1B  O   OOO O MO O  START STOP UNIT */
262 	{ 0x1B,	D | W | R | O | A | B | K | F, "START STOP UNIT" },
263 	/* 1B   O          M   LOAD UNLOAD */
264 	{ 0x1B,	T | V, "LOAD UNLOAD" },
265 	/* 1B                  SCAN */
266 	{ 0x1B, S, "SCAN" },
267 	/* 1B    O             STOP PRINT */
268 	{ 0x1B,	L, "STOP PRINT" },
269 	/* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
270 	{ 0x1B,	M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
271 	/* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
272 	{ 0x1C,	ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
273 	/* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
274 	{ 0x1D,	ALL & ~(R | B), "SEND DIAGNOSTIC" },
275 	/* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
276 	{ 0x1E,	D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
277 	/* 1F */
278 	/* 20  V   VVV    V */
279 	/* 21  V   VVV    V */
280 	/* 22  V   VVV    V */
281 	/* 23  V   V V    V */
282 	/* 23       O          READ FORMAT CAPACITIES */
283 	{ 0x23,	R, "READ FORMAT CAPACITIES" },
284 	/* 24  V   VV          SET WINDOW */
285 	{ 0x24, S, "SET WINDOW" },
286 	/* 25  M   M M   M     READ CAPACITY(10) */
287 	{ 0x25,	D | W | O | B, "READ CAPACITY(10)" },
288 	/* 25       O          READ CAPACITY */
289 	{ 0x25,	R, "READ CAPACITY" },
290 	/* 25             M    READ CARD CAPACITY */
291 	{ 0x25,	K, "READ CARD CAPACITY" },
292 	/* 25                  GET WINDOW */
293 	{ 0x25, S, "GET WINDOW" },
294 	/* 26  V   VV */
295 	/* 27  V   VV */
296 	/* 28  M   MOM   MM    READ(10) */
297 	{ 0x28,	D | W | R | O | B | K | S, "READ(10)" },
298 	/* 28                  GET MESSAGE(10) */
299 	{ 0x28, C, "GET MESSAGE(10)" },
300 	/* 29  V   VVO         READ GENERATION */
301 	{ 0x29,	O, "READ GENERATION" },
302 	/* 2A  O   MOM   MO    WRITE(10) */
303 	{ 0x2A,	D | W | R | O | B | K, "WRITE(10)" },
304 	/* 2A                  SEND(10) */
305 	{ 0x2A, S, "SEND(10)" },
306 	/* 2A                  SEND MESSAGE(10) */
307 	{ 0x2A, C, "SEND MESSAGE(10)" },
308 	/* 2B  Z   OOO    O    SEEK(10) */
309 	{ 0x2B,	D | W | R | O | K, "SEEK(10)" },
310 	/* 2B   O              LOCATE(10) */
311 	{ 0x2B,	T, "LOCATE(10)" },
312 	/* 2B         O        POSITION TO ELEMENT */
313 	{ 0x2B,	M, "POSITION TO ELEMENT" },
314 	/* 2C  V    OO         ERASE(10) */
315 	{ 0x2C,	R | O, "ERASE(10)" },
316 	/* 2D        O         READ UPDATED BLOCK */
317 	{ 0x2D,	O, "READ UPDATED BLOCK" },
318 	/* 2D  V */
319 	/* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
320 	{ 0x2E,	D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
321 	/* 2F  O   OOO         VERIFY(10) */
322 	{ 0x2F,	D | W | R | O, "VERIFY(10)" },
323 	/* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
324 	{ 0x30,	D | W | R | O, "SEARCH DATA HIGH(10)" },
325 	/* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
326 	{ 0x31,	D | W | R | O, "SEARCH DATA EQUAL(10)" },
327 	/* 31                  OBJECT POSITION */
328 	{ 0x31, S, "OBJECT POSITION" },
329 	/* 32  Z   ZZZ         SEARCH DATA LOW(10) */
330 	{ 0x32,	D | W | R | O, "SEARCH DATA LOW(10)" },
331 	/* 33  Z   OZO         SET LIMITS(10) */
332 	{ 0x33,	D | W | R | O, "SET LIMITS(10)" },
333 	/* 34  O   O O    O    PRE-FETCH(10) */
334 	{ 0x34,	D | W | O | K, "PRE-FETCH(10)" },
335 	/* 34   M              READ POSITION */
336 	{ 0x34,	T, "READ POSITION" },
337 	/* 34                  GET DATA BUFFER STATUS */
338 	{ 0x34, S, "GET DATA BUFFER STATUS" },
339 	/* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
340 	{ 0x35,	D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
341 	/* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
342 	{ 0x36,	D | W | O | K, "LOCK UNLOCK CACHE(10)" },
343 	/* 37  O     O         READ DEFECT DATA(10) */
344 	{ 0x37,	D | O, "READ DEFECT DATA(10)" },
345 	/* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
346 	{ 0x37,	M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
347 	/* 38      O O    O    MEDIUM SCAN */
348 	{ 0x38,	W | O | K, "MEDIUM SCAN" },
349 	/* 39  ZZZZOZO    Z    COMPARE */
350 	{ 0x39,	D | T | L | P | W | R | O | K | S, "COMPARE" },
351 	/* 3A  ZZZZOZO    Z    COPY AND VERIFY */
352 	{ 0x3A,	D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
353 	/* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
354 	{ 0x3B,	ALL, "WRITE BUFFER" },
355 	/* 3C  OOOOOOOOOO OOO  READ BUFFER */
356 	{ 0x3C,	ALL & ~(B), "READ BUFFER" },
357 	/* 3D        O         UPDATE BLOCK */
358 	{ 0x3D,	O, "UPDATE BLOCK" },
359 	/* 3E  O   O O         READ LONG(10) */
360 	{ 0x3E,	D | W | O, "READ LONG(10)" },
361 	/* 3F  O   O O         WRITE LONG(10) */
362 	{ 0x3F,	D | W | O, "WRITE LONG(10)" },
363 	/* 40  ZZZZOZOZ        CHANGE DEFINITION */
364 	{ 0x40,	D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
365 	/* 41  O               WRITE SAME(10) */
366 	{ 0x41,	D, "WRITE SAME(10)" },
367 	/* 42       O          READ SUB-CHANNEL */
368 	{ 0x42,	R, "READ SUB-CHANNEL" },
369 	/* 43       O          READ TOC/PMA/ATIP */
370 	{ 0x43,	R, "READ TOC/PMA/ATIP" },
371 	/* 44   M          M   REPORT DENSITY SUPPORT */
372 	{ 0x44,	T | V, "REPORT DENSITY SUPPORT" },
373 	/* 44                  READ HEADER */
374 	/* 45       O          PLAY AUDIO(10) */
375 	{ 0x45,	R, "PLAY AUDIO(10)" },
376 	/* 46       M          GET CONFIGURATION */
377 	{ 0x46,	R, "GET CONFIGURATION" },
378 	/* 47       O          PLAY AUDIO MSF */
379 	{ 0x47,	R, "PLAY AUDIO MSF" },
380 	/* 48 */
381 	/* 49 */
382 	/* 4A       M          GET EVENT STATUS NOTIFICATION */
383 	{ 0x4A,	R, "GET EVENT STATUS NOTIFICATION" },
384 	/* 4B       O          PAUSE/RESUME */
385 	{ 0x4B,	R, "PAUSE/RESUME" },
386 	/* 4C  OOOOO OOOO OOO  LOG SELECT */
387 	{ 0x4C,	ALL & ~(R | B), "LOG SELECT" },
388 	/* 4D  OOOOO OOOO OMO  LOG SENSE */
389 	{ 0x4D,	ALL & ~(R | B), "LOG SENSE" },
390 	/* 4E       O          STOP PLAY/SCAN */
391 	{ 0x4E,	R, "STOP PLAY/SCAN" },
392 	/* 4F */
393 	/* 50  O               XDWRITE(10) */
394 	{ 0x50,	D, "XDWRITE(10)" },
395 	/* 51  O               XPWRITE(10) */
396 	{ 0x51,	D, "XPWRITE(10)" },
397 	/* 51       O          READ DISC INFORMATION */
398 	{ 0x51,	R, "READ DISC INFORMATION" },
399 	/* 52  O               XDREAD(10) */
400 	{ 0x52,	D, "XDREAD(10)" },
401 	/* 52       O          READ TRACK INFORMATION */
402 	{ 0x52,	R, "READ TRACK INFORMATION" },
403 	/* 53       O          RESERVE TRACK */
404 	{ 0x53,	R, "RESERVE TRACK" },
405 	/* 54       O          SEND OPC INFORMATION */
406 	{ 0x54,	R, "SEND OPC INFORMATION" },
407 	/* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
408 	{ 0x55,	ALL & ~(P), "MODE SELECT(10)" },
409 	/* 56  ZZMZO OOOZ      RESERVE(10) */
410 	{ 0x56,	ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
411 	/* 56         Z        RESERVE ELEMENT(10) */
412 	{ 0x56,	M, "RESERVE ELEMENT(10)" },
413 	/* 57  ZZMZO OOOZ      RELEASE(10) */
414 	{ 0x57,	ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
415 	/* 57         Z        RELEASE ELEMENT(10) */
416 	{ 0x57,	M, "RELEASE ELEMENT(10)" },
417 	/* 58       O          REPAIR TRACK */
418 	{ 0x58,	R, "REPAIR TRACK" },
419 	/* 59 */
420 	/* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
421 	{ 0x5A,	ALL & ~(P), "MODE SENSE(10)" },
422 	/* 5B       O          CLOSE TRACK/SESSION */
423 	{ 0x5B,	R, "CLOSE TRACK/SESSION" },
424 	/* 5C       O          READ BUFFER CAPACITY */
425 	{ 0x5C,	R, "READ BUFFER CAPACITY" },
426 	/* 5D       O          SEND CUE SHEET */
427 	{ 0x5D,	R, "SEND CUE SHEET" },
428 	/* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
429 	{ 0x5E,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
430 	/* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
431 	{ 0x5F,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
432 	/* 7E  OO   O OOOO O   extended CDB */
433 	{ 0x7E,	D | T | R | M | A | E | B | V, "extended CDB" },
434 	/* 7F  O            M  variable length CDB (more than 16 bytes) */
435 	{ 0x7F,	D | F, "variable length CDB (more than 16 bytes)" },
436 	/* 80  Z               XDWRITE EXTENDED(16) */
437 	{ 0x80,	D, "XDWRITE EXTENDED(16)" },
438 	/* 80   M              WRITE FILEMARKS(16) */
439 	{ 0x80,	T, "WRITE FILEMARKS(16)" },
440 	/* 81  Z               REBUILD(16) */
441 	{ 0x81,	D, "REBUILD(16)" },
442 	/* 81   O              READ REVERSE(16) */
443 	{ 0x81,	T, "READ REVERSE(16)" },
444 	/* 82  Z               REGENERATE(16) */
445 	{ 0x82,	D, "REGENERATE(16)" },
446 	/* 83  OOOOO O    OO   EXTENDED COPY */
447 	{ 0x83,	D | T | L | P | W | O | K | V, "EXTENDED COPY" },
448 	/* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
449 	{ 0x84,	D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
450 	/* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
451 	{ 0x85,	D | R | B, "ATA COMMAND PASS THROUGH(16)" },
452 	/* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
453 	{ 0x86,	ALL & ~(L | R | F), "ACCESS CONTROL IN" },
454 	/* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
455 	{ 0x87,	ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
456 	/*
457 	 * XXX READ(16)/WRITE(16) were not listed for CD/DVE in op-num.txt
458 	 * but we had it since r1.40.  Do we really want them?
459 	 */
460 	/* 88  MM  O O   O     READ(16) */
461 	{ 0x88,	D | T | W | O | B, "READ(16)" },
462 	/* 89 */
463 	/* 8A  OM  O O   O     WRITE(16) */
464 	{ 0x8A,	D | T | W | O | B, "WRITE(16)" },
465 	/* 8B  O               ORWRITE */
466 	{ 0x8B,	D, "ORWRITE" },
467 	/* 8C  OO  O OO  O M   READ ATTRIBUTE */
468 	{ 0x8C,	D | T | W | O | M | B | V, "READ ATTRIBUTE" },
469 	/* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
470 	{ 0x8D,	D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
471 	/* 8E  O   O O   O     WRITE AND VERIFY(16) */
472 	{ 0x8E,	D | W | O | B, "WRITE AND VERIFY(16)" },
473 	/* 8F  OO  O O   O     VERIFY(16) */
474 	{ 0x8F,	D | T | W | O | B, "VERIFY(16)" },
475 	/* 90  O   O O   O     PRE-FETCH(16) */
476 	{ 0x90,	D | W | O | B, "PRE-FETCH(16)" },
477 	/* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
478 	{ 0x91,	D | W | O | B, "SYNCHRONIZE CACHE(16)" },
479 	/* 91   O              SPACE(16) */
480 	{ 0x91,	T, "SPACE(16)" },
481 	/* 92  Z   O O         LOCK UNLOCK CACHE(16) */
482 	{ 0x92,	D | W | O, "LOCK UNLOCK CACHE(16)" },
483 	/* 92   O              LOCATE(16) */
484 	{ 0x92,	T, "LOCATE(16)" },
485 	/* 93  O               WRITE SAME(16) */
486 	{ 0x93,	D, "WRITE SAME(16)" },
487 	/* 93   M              ERASE(16) */
488 	{ 0x93,	T, "ERASE(16)" },
489 	/* 94 [usage proposed by SCSI Socket Services project] */
490 	/* 95 [usage proposed by SCSI Socket Services project] */
491 	/* 96 [usage proposed by SCSI Socket Services project] */
492 	/* 97 [usage proposed by SCSI Socket Services project] */
493 	/* 98 */
494 	/* 99 */
495 	/* 9A */
496 	/* 9B */
497 	/* 9C */
498 	/* 9D */
499 	/* XXX KDM ALL for this?  op-num.txt defines it for none.. */
500 	/* 9E                  SERVICE ACTION IN(16) */
501 	{ 0x9E, ALL, "SERVICE ACTION IN(16)" },
502 	/* XXX KDM ALL for this?  op-num.txt defines it for ADC.. */
503 	/* 9F              M   SERVICE ACTION OUT(16) */
504 	{ 0x9F,	ALL, "SERVICE ACTION OUT(16)" },
505 	/* A0  MMOOO OMMM OMO  REPORT LUNS */
506 	{ 0xA0,	ALL & ~(R | B), "REPORT LUNS" },
507 	/* A1       O          BLANK */
508 	{ 0xA1,	R, "BLANK" },
509 	/* A1  O         O     ATA COMMAND PASS THROUGH(12) */
510 	{ 0xA1,	D | B, "ATA COMMAND PASS THROUGH(12)" },
511 	/* A2  OO   O      O   SECURITY PROTOCOL IN */
512 	{ 0xA2,	D | T | R | V, "SECURITY PROTOCOL IN" },
513 	/* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
514 	{ 0xA3,	ALL & ~(P | R | F), "MAINTENANCE (IN)" },
515 	/* A3       O          SEND KEY */
516 	{ 0xA3,	R, "SEND KEY" },
517 	/* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
518 	{ 0xA4,	ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
519 	/* A4       O          REPORT KEY */
520 	{ 0xA4,	R, "REPORT KEY" },
521 	/* A5   O  O OM        MOVE MEDIUM */
522 	{ 0xA5,	T | W | O | M, "MOVE MEDIUM" },
523 	/* A5       O          PLAY AUDIO(12) */
524 	{ 0xA5,	R, "PLAY AUDIO(12)" },
525 	/* A6         O        EXCHANGE MEDIUM */
526 	{ 0xA6,	M, "EXCHANGE MEDIUM" },
527 	/* A6       O          LOAD/UNLOAD C/DVD */
528 	{ 0xA6,	R, "LOAD/UNLOAD C/DVD" },
529 	/* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
530 	{ 0xA7,	D | T | W | O, "MOVE MEDIUM ATTACHED" },
531 	/* A7       O          SET READ AHEAD */
532 	{ 0xA7,	R, "SET READ AHEAD" },
533 	/* A8  O   OOO         READ(12) */
534 	{ 0xA8,	D | W | R | O, "READ(12)" },
535 	/* A8                  GET MESSAGE(12) */
536 	{ 0xA8, C, "GET MESSAGE(12)" },
537 	/* A9              O   SERVICE ACTION OUT(12) */
538 	{ 0xA9,	V, "SERVICE ACTION OUT(12)" },
539 	/* AA  O   OOO         WRITE(12) */
540 	{ 0xAA,	D | W | R | O, "WRITE(12)" },
541 	/* AA                  SEND MESSAGE(12) */
542 	{ 0xAA, C, "SEND MESSAGE(12)" },
543 	/* AB       O      O   SERVICE ACTION IN(12) */
544 	{ 0xAB,	R | V, "SERVICE ACTION IN(12)" },
545 	/* AC        O         ERASE(12) */
546 	{ 0xAC,	O, "ERASE(12)" },
547 	/* AC       O          GET PERFORMANCE */
548 	{ 0xAC,	R, "GET PERFORMANCE" },
549 	/* AD       O          READ DVD STRUCTURE */
550 	{ 0xAD,	R, "READ DVD STRUCTURE" },
551 	/* AE  O   O O         WRITE AND VERIFY(12) */
552 	{ 0xAE,	D | W | O, "WRITE AND VERIFY(12)" },
553 	/* AF  O   OZO         VERIFY(12) */
554 	{ 0xAF,	D | W | R | O, "VERIFY(12)" },
555 	/* B0      ZZZ         SEARCH DATA HIGH(12) */
556 	{ 0xB0,	W | R | O, "SEARCH DATA HIGH(12)" },
557 	/* B1      ZZZ         SEARCH DATA EQUAL(12) */
558 	{ 0xB1,	W | R | O, "SEARCH DATA EQUAL(12)" },
559 	/* B2      ZZZ         SEARCH DATA LOW(12) */
560 	{ 0xB2,	W | R | O, "SEARCH DATA LOW(12)" },
561 	/* B3  Z   OZO         SET LIMITS(12) */
562 	{ 0xB3,	D | W | R | O, "SET LIMITS(12)" },
563 	/* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
564 	{ 0xB4,	D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
565 	/* B5  OO   O      O   SECURITY PROTOCOL OUT */
566 	{ 0xB5,	D | T | R | V, "SECURITY PROTOCOL OUT" },
567 	/* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
568 	{ 0xB5,	M, "REQUEST VOLUME ELEMENT ADDRESS" },
569 	/* B6         O        SEND VOLUME TAG */
570 	{ 0xB6,	M, "SEND VOLUME TAG" },
571 	/* B6       O          SET STREAMING */
572 	{ 0xB6,	R, "SET STREAMING" },
573 	/* B7  O     O         READ DEFECT DATA(12) */
574 	{ 0xB7,	D | O, "READ DEFECT DATA(12)" },
575 	/* B8   O  OZOM        READ ELEMENT STATUS */
576 	{ 0xB8,	T | W | R | O | M, "READ ELEMENT STATUS" },
577 	/* B9       O          READ CD MSF */
578 	{ 0xB9,	R, "READ CD MSF" },
579 	/* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
580 	{ 0xBA,	D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
581 	/* BA       O          SCAN */
582 	{ 0xBA,	R, "SCAN" },
583 	/* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
584 	{ 0xBB,	D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
585 	/* BB       O          SET CD SPEED */
586 	{ 0xBB,	R, "SET CD SPEED" },
587 	/* BC  O   O OOMO      SPARE (IN) */
588 	{ 0xBC,	D | W | O | M | A | E, "SPARE (IN)" },
589 	/* BD  O   O OOOO      SPARE (OUT) */
590 	{ 0xBD,	D | W | O | M | A | E, "SPARE (OUT)" },
591 	/* BD       O          MECHANISM STATUS */
592 	{ 0xBD,	R, "MECHANISM STATUS" },
593 	/* BE  O   O OOMO      VOLUME SET (IN) */
594 	{ 0xBE,	D | W | O | M | A | E, "VOLUME SET (IN)" },
595 	/* BE       O          READ CD */
596 	{ 0xBE,	R, "READ CD" },
597 	/* BF  O   O OOOO      VOLUME SET (OUT) */
598 	{ 0xBF,	D | W | O | M | A | E, "VOLUME SET (OUT)" },
599 	/* BF       O          SEND DVD STRUCTURE */
600 	{ 0xBF,	R, "SEND DVD STRUCTURE" }
601 };
602 
603 const char *
604 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
605 {
606 	caddr_t match;
607 	int i, j;
608 	u_int32_t opmask;
609 	u_int16_t pd_type;
610 	int       num_ops[2];
611 	struct op_table_entry *table[2];
612 	int num_tables;
613 
614 	/*
615 	 * If we've got inquiry data, use it to determine what type of
616 	 * device we're dealing with here.  Otherwise, assume direct
617 	 * access.
618 	 */
619 	if (inq_data == NULL) {
620 		pd_type = T_DIRECT;
621 		match = NULL;
622 	} else {
623 		pd_type = SID_TYPE(inq_data);
624 
625 		match = cam_quirkmatch((caddr_t)inq_data,
626 				       (caddr_t)scsi_op_quirk_table,
627 				       sizeof(scsi_op_quirk_table)/
628 				       sizeof(*scsi_op_quirk_table),
629 				       sizeof(*scsi_op_quirk_table),
630 				       scsi_inquiry_match);
631 	}
632 
633 	if (match != NULL) {
634 		table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
635 		num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
636 		table[1] = scsi_op_codes;
637 		num_ops[1] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
638 		num_tables = 2;
639 	} else {
640 		/*
641 		 * If this is true, we have a vendor specific opcode that
642 		 * wasn't covered in the quirk table.
643 		 */
644 		if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
645 			return("Vendor Specific Command");
646 
647 		table[0] = scsi_op_codes;
648 		num_ops[0] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
649 		num_tables = 1;
650 	}
651 
652 	/* RBC is 'Simplified' Direct Access Device */
653 	if (pd_type == T_RBC)
654 		pd_type = T_DIRECT;
655 
656 	opmask = 1 << pd_type;
657 
658 	for (j = 0; j < num_tables; j++) {
659 		for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
660 			if ((table[j][i].opcode == opcode)
661 			 && ((table[j][i].opmask & opmask) != 0))
662 				return(table[j][i].desc);
663 		}
664 	}
665 
666 	/*
667 	 * If we can't find a match for the command in the table, we just
668 	 * assume it's a vendor specifc command.
669 	 */
670 	return("Vendor Specific Command");
671 
672 }
673 
674 #else /* SCSI_NO_OP_STRINGS */
675 
676 const char *
677 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
678 {
679 	return("");
680 }
681 
682 #endif
683 
684 
685 #if !defined(SCSI_NO_SENSE_STRINGS)
686 #define SST(asc, ascq, action, desc) \
687 	asc, ascq, action, desc
688 #else
689 const char empty_string[] = "";
690 
691 #define SST(asc, ascq, action, desc) \
692 	asc, ascq, action, empty_string
693 #endif
694 
695 const struct sense_key_table_entry sense_key_table[] =
696 {
697 	{ SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
698 	{ SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
699 	{
700 	  SSD_KEY_NOT_READY, SS_TUR|SSQ_MANY|SSQ_DECREMENT_COUNT|EBUSY,
701 	  "NOT READY"
702 	},
703 	{ SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
704 	{ SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
705 	{ SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
706 	{ SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
707 	{ SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
708 	{ SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
709 	{ SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
710 	{ SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
711 	{ SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
712 	{ SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
713 	{ SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
714 	{ SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
715 	{ SSD_KEY_COMPLETED, SS_NOP, "COMPLETED" }
716 };
717 
718 const int sense_key_table_size =
719     sizeof(sense_key_table)/sizeof(sense_key_table[0]);
720 
721 static struct asc_table_entry quantum_fireball_entries[] = {
722 	{ SST(0x04, 0x0b, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
723 	     "Logical unit not ready, initializing cmd. required") }
724 };
725 
726 static struct asc_table_entry sony_mo_entries[] = {
727 	{ SST(0x04, 0x00, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
728 	     "Logical unit not ready, cause not reportable") }
729 };
730 
731 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
732 	{
733 		/*
734 		 * XXX The Quantum Fireball ST and SE like to return 0x04 0x0b
735 		 * when they really should return 0x04 0x02.
736 		 */
737 		{T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
738 		/*num_sense_keys*/0,
739 		sizeof(quantum_fireball_entries)/sizeof(struct asc_table_entry),
740 		/*sense key entries*/NULL,
741 		quantum_fireball_entries
742 	},
743 	{
744 		/*
745 		 * This Sony MO drive likes to return 0x04, 0x00 when it
746 		 * isn't spun up.
747 		 */
748 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
749 		/*num_sense_keys*/0,
750 		sizeof(sony_mo_entries)/sizeof(struct asc_table_entry),
751 		/*sense key entries*/NULL,
752 		sony_mo_entries
753 	}
754 };
755 
756 const int sense_quirk_table_size =
757     sizeof(sense_quirk_table)/sizeof(sense_quirk_table[0]);
758 
759 static struct asc_table_entry asc_table[] = {
760 	/*
761 	 * From: http://www.t10.org/lists/asc-num.txt
762 	 * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
763 	 */
764 	/*
765 	 * File: ASC-NUM.TXT
766 	 *
767 	 * SCSI ASC/ASCQ Assignments
768 	 * Numeric Sorted Listing
769 	 * as of  7/29/08
770 	 *
771 	 * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
772 	 * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
773 	 * . L - PRINTER DEVICE (SSC)                           blank = reserved
774 	 * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
775 	 * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
776 	 * .  . R - CD DEVICE (MMC)
777 	 * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
778 	 * .  .  .M - MEDIA CHANGER DEVICE (SMC)
779 	 * .  .  . A - STORAGE ARRAY DEVICE (SCC)
780 	 * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
781 	 * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
782 	 * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
783 	 * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
784 	 * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
785 	 * DTLPWROMAEBKVF
786 	 * ASC      ASCQ  Action
787 	 * Description
788 	 */
789 	/* DTLPWROMAEBKVF */
790 	{ SST(0x00, 0x00, SS_NOP,
791 	    "No additional sense information") },
792 	/*  T             */
793 	{ SST(0x00, 0x01, SS_RDEF,
794 	    "Filemark detected") },
795 	/*  T             */
796 	{ SST(0x00, 0x02, SS_RDEF,
797 	    "End-of-partition/medium detected") },
798 	/*  T             */
799 	{ SST(0x00, 0x03, SS_RDEF,
800 	    "Setmark detected") },
801 	/*  T             */
802 	{ SST(0x00, 0x04, SS_RDEF,
803 	    "Beginning-of-partition/medium detected") },
804 	/*  TL            */
805 	{ SST(0x00, 0x05, SS_RDEF,
806 	    "End-of-data detected") },
807 	/* DTLPWROMAEBKVF */
808 	{ SST(0x00, 0x06, SS_RDEF,
809 	    "I/O process terminated") },
810 	/*  T             */
811 	{ SST(0x00, 0x07, SS_RDEF,	/* XXX TBD */
812 	    "Programmable early warning detected") },
813 	/*      R         */
814 	{ SST(0x00, 0x11, SS_FATAL | EBUSY,
815 	    "Audio play operation in progress") },
816 	/*      R         */
817 	{ SST(0x00, 0x12, SS_NOP,
818 	    "Audio play operation paused") },
819 	/*      R         */
820 	{ SST(0x00, 0x13, SS_NOP,
821 	    "Audio play operation successfully completed") },
822 	/*      R         */
823 	{ SST(0x00, 0x14, SS_RDEF,
824 	    "Audio play operation stopped due to error") },
825 	/*      R         */
826 	{ SST(0x00, 0x15, SS_NOP,
827 	    "No current audio status to return") },
828 	/* DTLPWROMAEBKVF */
829 	{ SST(0x00, 0x16, SS_FATAL | EBUSY,
830 	    "Operation in progress") },
831 	/* DTL WROMAEBKVF */
832 	{ SST(0x00, 0x17, SS_RDEF,
833 	    "Cleaning requested") },
834 	/*  T             */
835 	{ SST(0x00, 0x18, SS_RDEF,	/* XXX TBD */
836 	    "Erase operation in progress") },
837 	/*  T             */
838 	{ SST(0x00, 0x19, SS_RDEF,	/* XXX TBD */
839 	    "Locate operation in progress") },
840 	/*  T             */
841 	{ SST(0x00, 0x1A, SS_RDEF,	/* XXX TBD */
842 	    "Rewind operation in progress") },
843 	/*  T             */
844 	{ SST(0x00, 0x1B, SS_RDEF,	/* XXX TBD */
845 	    "Set capacity operation in progress") },
846 	/*  T             */
847 	{ SST(0x00, 0x1C, SS_RDEF,	/* XXX TBD */
848 	    "Verify operation in progress") },
849 	/* DT        B    */
850 	{ SST(0x00, 0x1D, SS_RDEF,	/* XXX TBD */
851 	    "ATA pass through information available") },
852 	/* DT   R MAEBKV  */
853 	{ SST(0x00, 0x1E, SS_RDEF,	/* XXX TBD */
854 	    "Conflicting SA creation request") },
855 	/* D   W O   BK   */
856 	{ SST(0x01, 0x00, SS_RDEF,
857 	    "No index/sector signal") },
858 	/* D   WRO   BK   */
859 	{ SST(0x02, 0x00, SS_RDEF,
860 	    "No seek complete") },
861 	/* DTL W O   BK   */
862 	{ SST(0x03, 0x00, SS_RDEF,
863 	    "Peripheral device write fault") },
864 	/*  T             */
865 	{ SST(0x03, 0x01, SS_RDEF,
866 	    "No write current") },
867 	/*  T             */
868 	{ SST(0x03, 0x02, SS_RDEF,
869 	    "Excessive write errors") },
870 	/* DTLPWROMAEBKVF */
871 	{ SST(0x04, 0x00, SS_TUR | SSQ_MANY | SSQ_DECREMENT_COUNT | EIO,
872 	    "Logical unit not ready, cause not reportable") },
873 	/* DTLPWROMAEBKVF */
874 	{ SST(0x04, 0x01, SS_TUR | SSQ_MANY | SSQ_DECREMENT_COUNT | EBUSY,
875 	    "Logical unit is in process of becoming ready") },
876 	/* DTLPWROMAEBKVF */
877 	{ SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
878 	    "Logical unit not ready, initializing command required") },
879 	/* DTLPWROMAEBKVF */
880 	{ SST(0x04, 0x03, SS_FATAL | ENXIO,
881 	    "Logical unit not ready, manual intervention required") },
882 	/* DTL  RO   B    */
883 	{ SST(0x04, 0x04, SS_FATAL | EBUSY,
884 	    "Logical unit not ready, format in progress") },
885 	/* DT  W O A BK F */
886 	{ SST(0x04, 0x05, SS_FATAL | EBUSY,
887 	    "Logical unit not ready, rebuild in progress") },
888 	/* DT  W O A BK   */
889 	{ SST(0x04, 0x06, SS_FATAL | EBUSY,
890 	    "Logical unit not ready, recalculation in progress") },
891 	/* DTLPWROMAEBKVF */
892 	{ SST(0x04, 0x07, SS_FATAL | EBUSY,
893 	    "Logical unit not ready, operation in progress") },
894 	/*      R         */
895 	{ SST(0x04, 0x08, SS_FATAL | EBUSY,
896 	    "Logical unit not ready, long write in progress") },
897 	/* DTLPWROMAEBKVF */
898 	{ SST(0x04, 0x09, SS_RDEF,	/* XXX TBD */
899 	    "Logical unit not ready, self-test in progress") },
900 	/* DTLPWROMAEBKVF */
901 	{ SST(0x04, 0x0A, SS_RDEF,	/* XXX TBD */
902 	    "Logical unit not accessible, asymmetric access state transition")},
903 	/* DTLPWROMAEBKVF */
904 	{ SST(0x04, 0x0B, SS_RDEF,	/* XXX TBD */
905 	    "Logical unit not accessible, target port in standby state") },
906 	/* DTLPWROMAEBKVF */
907 	{ SST(0x04, 0x0C, SS_RDEF,	/* XXX TBD */
908 	    "Logical unit not accessible, target port in unavailable state") },
909 	/*              F */
910 	{ SST(0x04, 0x0D, SS_RDEF,	/* XXX TBD */
911 	    "Logical unit not ready, structure check required") },
912 	/* DT  WROM  B    */
913 	{ SST(0x04, 0x10, SS_RDEF,	/* XXX TBD */
914 	    "Logical unit not ready, auxiliary memory not accessible") },
915 	/* DT  WRO AEB VF */
916 	{ SST(0x04, 0x11, SS_RDEF,	/* XXX TBD */
917 	    "Logical unit not ready, notify (enable spinup) required") },
918 	/*        M    V  */
919 	{ SST(0x04, 0x12, SS_RDEF,	/* XXX TBD */
920 	    "Logical unit not ready, offline") },
921 	/* DT   R MAEBKV  */
922 	{ SST(0x04, 0x13, SS_RDEF,	/* XXX TBD */
923 	    "Logical unit not ready, SA creation in progress") },
924 	/* DTL WROMAEBKVF */
925 	{ SST(0x05, 0x00, SS_RDEF,
926 	    "Logical unit does not respond to selection") },
927 	/* D   WROM  BK   */
928 	{ SST(0x06, 0x00, SS_RDEF,
929 	    "No reference position found") },
930 	/* DTL WROM  BK   */
931 	{ SST(0x07, 0x00, SS_RDEF,
932 	    "Multiple peripheral devices selected") },
933 	/* DTL WROMAEBKVF */
934 	{ SST(0x08, 0x00, SS_RDEF,
935 	    "Logical unit communication failure") },
936 	/* DTL WROMAEBKVF */
937 	{ SST(0x08, 0x01, SS_RDEF,
938 	    "Logical unit communication time-out") },
939 	/* DTL WROMAEBKVF */
940 	{ SST(0x08, 0x02, SS_RDEF,
941 	    "Logical unit communication parity error") },
942 	/* DT   ROM  BK   */
943 	{ SST(0x08, 0x03, SS_RDEF,
944 	    "Logical unit communication CRC error (Ultra-DMA/32)") },
945 	/* DTLPWRO    K   */
946 	{ SST(0x08, 0x04, SS_RDEF,	/* XXX TBD */
947 	    "Unreachable copy target") },
948 	/* DT  WRO   B    */
949 	{ SST(0x09, 0x00, SS_RDEF,
950 	    "Track following error") },
951 	/*     WRO    K   */
952 	{ SST(0x09, 0x01, SS_RDEF,
953 	    "Tracking servo failure") },
954 	/*     WRO    K   */
955 	{ SST(0x09, 0x02, SS_RDEF,
956 	    "Focus servo failure") },
957 	/*     WRO        */
958 	{ SST(0x09, 0x03, SS_RDEF,
959 	    "Spindle servo failure") },
960 	/* DT  WRO   B    */
961 	{ SST(0x09, 0x04, SS_RDEF,
962 	    "Head select fault") },
963 	/* DTLPWROMAEBKVF */
964 	{ SST(0x0A, 0x00, SS_FATAL | ENOSPC,
965 	    "Error log overflow") },
966 	/* DTLPWROMAEBKVF */
967 	{ SST(0x0B, 0x00, SS_RDEF,
968 	    "Warning") },
969 	/* DTLPWROMAEBKVF */
970 	{ SST(0x0B, 0x01, SS_RDEF,
971 	    "Warning - specified temperature exceeded") },
972 	/* DTLPWROMAEBKVF */
973 	{ SST(0x0B, 0x02, SS_RDEF,
974 	    "Warning - enclosure degraded") },
975 	/* DTLPWROMAEBKVF */
976 	{ SST(0x0B, 0x03, SS_RDEF,	/* XXX TBD */
977 	    "Warning - background self-test failed") },
978 	/* DTLPWRO AEBKVF */
979 	{ SST(0x0B, 0x04, SS_RDEF,	/* XXX TBD */
980 	    "Warning - background pre-scan detected medium error") },
981 	/* DTLPWRO AEBKVF */
982 	{ SST(0x0B, 0x05, SS_RDEF,	/* XXX TBD */
983 	    "Warning - background medium scan detected medium error") },
984 	/* DTLPWROMAEBKVF */
985 	{ SST(0x0B, 0x06, SS_RDEF,	/* XXX TBD */
986 	    "Warning - non-volatile cache now volatile") },
987 	/* DTLPWROMAEBKVF */
988 	{ SST(0x0B, 0x07, SS_RDEF,	/* XXX TBD */
989 	    "Warning - degraded power to non-volatile cache") },
990 	/*  T   R         */
991 	{ SST(0x0C, 0x00, SS_RDEF,
992 	    "Write error") },
993 	/*            K   */
994 	{ SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
995 	    "Write error - recovered with auto reallocation") },
996 	/* D   W O   BK   */
997 	{ SST(0x0C, 0x02, SS_RDEF,
998 	    "Write error - auto reallocation failed") },
999 	/* D   W O   BK   */
1000 	{ SST(0x0C, 0x03, SS_RDEF,
1001 	    "Write error - recommend reassignment") },
1002 	/* DT  W O   B    */
1003 	{ SST(0x0C, 0x04, SS_RDEF,
1004 	    "Compression check miscompare error") },
1005 	/* DT  W O   B    */
1006 	{ SST(0x0C, 0x05, SS_RDEF,
1007 	    "Data expansion occurred during compression") },
1008 	/* DT  W O   B    */
1009 	{ SST(0x0C, 0x06, SS_RDEF,
1010 	    "Block not compressible") },
1011 	/*      R         */
1012 	{ SST(0x0C, 0x07, SS_RDEF,
1013 	    "Write error - recovery needed") },
1014 	/*      R         */
1015 	{ SST(0x0C, 0x08, SS_RDEF,
1016 	    "Write error - recovery failed") },
1017 	/*      R         */
1018 	{ SST(0x0C, 0x09, SS_RDEF,
1019 	    "Write error - loss of streaming") },
1020 	/*      R         */
1021 	{ SST(0x0C, 0x0A, SS_RDEF,
1022 	    "Write error - padding blocks added") },
1023 	/* DT  WROM  B    */
1024 	{ SST(0x0C, 0x0B, SS_RDEF,	/* XXX TBD */
1025 	    "Auxiliary memory write error") },
1026 	/* DTLPWRO AEBKVF */
1027 	{ SST(0x0C, 0x0C, SS_RDEF,	/* XXX TBD */
1028 	    "Write error - unexpected unsolicited data") },
1029 	/* DTLPWRO AEBKVF */
1030 	{ SST(0x0C, 0x0D, SS_RDEF,	/* XXX TBD */
1031 	    "Write error - not enough unsolicited data") },
1032 	/*      R         */
1033 	{ SST(0x0C, 0x0F, SS_RDEF,	/* XXX TBD */
1034 	    "Defects in error window") },
1035 	/* DTLPWRO A  K   */
1036 	{ SST(0x0D, 0x00, SS_RDEF,	/* XXX TBD */
1037 	    "Error detected by third party temporary initiator") },
1038 	/* DTLPWRO A  K   */
1039 	{ SST(0x0D, 0x01, SS_RDEF,	/* XXX TBD */
1040 	    "Third party device failure") },
1041 	/* DTLPWRO A  K   */
1042 	{ SST(0x0D, 0x02, SS_RDEF,	/* XXX TBD */
1043 	    "Copy target device not reachable") },
1044 	/* DTLPWRO A  K   */
1045 	{ SST(0x0D, 0x03, SS_RDEF,	/* XXX TBD */
1046 	    "Incorrect copy target device type") },
1047 	/* DTLPWRO A  K   */
1048 	{ SST(0x0D, 0x04, SS_RDEF,	/* XXX TBD */
1049 	    "Copy target device data underrun") },
1050 	/* DTLPWRO A  K   */
1051 	{ SST(0x0D, 0x05, SS_RDEF,	/* XXX TBD */
1052 	    "Copy target device data overrun") },
1053 	/* DT PWROMAEBK F */
1054 	{ SST(0x0E, 0x00, SS_RDEF,	/* XXX TBD */
1055 	    "Invalid information unit") },
1056 	/* DT PWROMAEBK F */
1057 	{ SST(0x0E, 0x01, SS_RDEF,	/* XXX TBD */
1058 	    "Information unit too short") },
1059 	/* DT PWROMAEBK F */
1060 	{ SST(0x0E, 0x02, SS_RDEF,	/* XXX TBD */
1061 	    "Information unit too long") },
1062 	/* DT P R MAEBK F */
1063 	{ SST(0x0E, 0x03, SS_RDEF,	/* XXX TBD */
1064 	    "Invalid field in command information unit") },
1065 	/* D   W O   BK   */
1066 	{ SST(0x10, 0x00, SS_RDEF,
1067 	    "ID CRC or ECC error") },
1068 	/* DT  W O        */
1069 	{ SST(0x10, 0x01, SS_RDEF,	/* XXX TBD */
1070 	    "Logical block guard check failed") },
1071 	/* DT  W O        */
1072 	{ SST(0x10, 0x02, SS_RDEF,	/* XXX TBD */
1073 	    "Logical block application tag check failed") },
1074 	/* DT  W O        */
1075 	{ SST(0x10, 0x03, SS_RDEF,	/* XXX TBD */
1076 	    "Logical block reference tag check failed") },
1077 	/* DT  WRO   BK   */
1078 	{ SST(0x11, 0x00, SS_FATAL|EIO,
1079 	    "Unrecovered read error") },
1080 	/* DT  WRO   BK   */
1081 	{ SST(0x11, 0x01, SS_FATAL|EIO,
1082 	    "Read retries exhausted") },
1083 	/* DT  WRO   BK   */
1084 	{ SST(0x11, 0x02, SS_FATAL|EIO,
1085 	    "Error too long to correct") },
1086 	/* DT  W O   BK   */
1087 	{ SST(0x11, 0x03, SS_FATAL|EIO,
1088 	    "Multiple read errors") },
1089 	/* D   W O   BK   */
1090 	{ SST(0x11, 0x04, SS_FATAL|EIO,
1091 	    "Unrecovered read error - auto reallocate failed") },
1092 	/*     WRO   B    */
1093 	{ SST(0x11, 0x05, SS_FATAL|EIO,
1094 	    "L-EC uncorrectable error") },
1095 	/*     WRO   B    */
1096 	{ SST(0x11, 0x06, SS_FATAL|EIO,
1097 	    "CIRC unrecovered error") },
1098 	/*     W O   B    */
1099 	{ SST(0x11, 0x07, SS_RDEF,
1100 	    "Data re-synchronization error") },
1101 	/*  T             */
1102 	{ SST(0x11, 0x08, SS_RDEF,
1103 	    "Incomplete block read") },
1104 	/*  T             */
1105 	{ SST(0x11, 0x09, SS_RDEF,
1106 	    "No gap found") },
1107 	/* DT    O   BK   */
1108 	{ SST(0x11, 0x0A, SS_RDEF,
1109 	    "Miscorrected error") },
1110 	/* D   W O   BK   */
1111 	{ SST(0x11, 0x0B, SS_FATAL|EIO,
1112 	    "Unrecovered read error - recommend reassignment") },
1113 	/* D   W O   BK   */
1114 	{ SST(0x11, 0x0C, SS_FATAL|EIO,
1115 	    "Unrecovered read error - recommend rewrite the data") },
1116 	/* DT  WRO   B    */
1117 	{ SST(0x11, 0x0D, SS_RDEF,
1118 	    "De-compression CRC error") },
1119 	/* DT  WRO   B    */
1120 	{ SST(0x11, 0x0E, SS_RDEF,
1121 	    "Cannot decompress using declared algorithm") },
1122 	/*      R         */
1123 	{ SST(0x11, 0x0F, SS_RDEF,
1124 	    "Error reading UPC/EAN number") },
1125 	/*      R         */
1126 	{ SST(0x11, 0x10, SS_RDEF,
1127 	    "Error reading ISRC number") },
1128 	/*      R         */
1129 	{ SST(0x11, 0x11, SS_RDEF,
1130 	    "Read error - loss of streaming") },
1131 	/* DT  WROM  B    */
1132 	{ SST(0x11, 0x12, SS_RDEF,	/* XXX TBD */
1133 	    "Auxiliary memory read error") },
1134 	/* DTLPWRO AEBKVF */
1135 	{ SST(0x11, 0x13, SS_RDEF,	/* XXX TBD */
1136 	    "Read error - failed retransmission request") },
1137 	/* D              */
1138 	{ SST(0x11, 0x14, SS_RDEF,	/* XXX TBD */
1139 	    "Read error - LBA marked bad by application client") },
1140 	/* D   W O   BK   */
1141 	{ SST(0x12, 0x00, SS_RDEF,
1142 	    "Address mark not found for ID field") },
1143 	/* D   W O   BK   */
1144 	{ SST(0x13, 0x00, SS_RDEF,
1145 	    "Address mark not found for data field") },
1146 	/* DTL WRO   BK   */
1147 	{ SST(0x14, 0x00, SS_RDEF,
1148 	    "Recorded entity not found") },
1149 	/* DT  WRO   BK   */
1150 	{ SST(0x14, 0x01, SS_RDEF,
1151 	    "Record not found") },
1152 	/*  T             */
1153 	{ SST(0x14, 0x02, SS_RDEF,
1154 	    "Filemark or setmark not found") },
1155 	/*  T             */
1156 	{ SST(0x14, 0x03, SS_RDEF,
1157 	    "End-of-data not found") },
1158 	/*  T             */
1159 	{ SST(0x14, 0x04, SS_RDEF,
1160 	    "Block sequence error") },
1161 	/* DT  W O   BK   */
1162 	{ SST(0x14, 0x05, SS_RDEF,
1163 	    "Record not found - recommend reassignment") },
1164 	/* DT  W O   BK   */
1165 	{ SST(0x14, 0x06, SS_RDEF,
1166 	    "Record not found - data auto-reallocated") },
1167 	/*  T             */
1168 	{ SST(0x14, 0x07, SS_RDEF,	/* XXX TBD */
1169 	    "Locate operation failure") },
1170 	/* DTL WROM  BK   */
1171 	{ SST(0x15, 0x00, SS_RDEF,
1172 	    "Random positioning error") },
1173 	/* DTL WROM  BK   */
1174 	{ SST(0x15, 0x01, SS_RDEF,
1175 	    "Mechanical positioning error") },
1176 	/* DT  WRO   BK   */
1177 	{ SST(0x15, 0x02, SS_RDEF,
1178 	    "Positioning error detected by read of medium") },
1179 	/* D   W O   BK   */
1180 	{ SST(0x16, 0x00, SS_RDEF,
1181 	    "Data synchronization mark error") },
1182 	/* D   W O   BK   */
1183 	{ SST(0x16, 0x01, SS_RDEF,
1184 	    "Data sync error - data rewritten") },
1185 	/* D   W O   BK   */
1186 	{ SST(0x16, 0x02, SS_RDEF,
1187 	    "Data sync error - recommend rewrite") },
1188 	/* D   W O   BK   */
1189 	{ SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1190 	    "Data sync error - data auto-reallocated") },
1191 	/* D   W O   BK   */
1192 	{ SST(0x16, 0x04, SS_RDEF,
1193 	    "Data sync error - recommend reassignment") },
1194 	/* DT  WRO   BK   */
1195 	{ SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1196 	    "Recovered data with no error correction applied") },
1197 	/* DT  WRO   BK   */
1198 	{ SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1199 	    "Recovered data with retries") },
1200 	/* DT  WRO   BK   */
1201 	{ SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1202 	    "Recovered data with positive head offset") },
1203 	/* DT  WRO   BK   */
1204 	{ SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1205 	    "Recovered data with negative head offset") },
1206 	/*     WRO   B    */
1207 	{ SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1208 	    "Recovered data with retries and/or CIRC applied") },
1209 	/* D   WRO   BK   */
1210 	{ SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1211 	    "Recovered data using previous sector ID") },
1212 	/* D   W O   BK   */
1213 	{ SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1214 	    "Recovered data without ECC - data auto-reallocated") },
1215 	/* D   WRO   BK   */
1216 	{ SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1217 	    "Recovered data without ECC - recommend reassignment") },
1218 	/* D   WRO   BK   */
1219 	{ SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1220 	    "Recovered data without ECC - recommend rewrite") },
1221 	/* D   WRO   BK   */
1222 	{ SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1223 	    "Recovered data without ECC - data rewritten") },
1224 	/* DT  WRO   BK   */
1225 	{ SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1226 	    "Recovered data with error correction applied") },
1227 	/* D   WRO   BK   */
1228 	{ SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1229 	    "Recovered data with error corr. & retries applied") },
1230 	/* D   WRO   BK   */
1231 	{ SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1232 	    "Recovered data - data auto-reallocated") },
1233 	/*      R         */
1234 	{ SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1235 	    "Recovered data with CIRC") },
1236 	/*      R         */
1237 	{ SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1238 	    "Recovered data with L-EC") },
1239 	/* D   WRO   BK   */
1240 	{ SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1241 	    "Recovered data - recommend reassignment") },
1242 	/* D   WRO   BK   */
1243 	{ SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1244 	    "Recovered data - recommend rewrite") },
1245 	/* D   W O   BK   */
1246 	{ SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1247 	    "Recovered data with ECC - data rewritten") },
1248 	/*      R         */
1249 	{ SST(0x18, 0x08, SS_RDEF,	/* XXX TBD */
1250 	    "Recovered data with linking") },
1251 	/* D     O    K   */
1252 	{ SST(0x19, 0x00, SS_RDEF,
1253 	    "Defect list error") },
1254 	/* D     O    K   */
1255 	{ SST(0x19, 0x01, SS_RDEF,
1256 	    "Defect list not available") },
1257 	/* D     O    K   */
1258 	{ SST(0x19, 0x02, SS_RDEF,
1259 	    "Defect list error in primary list") },
1260 	/* D     O    K   */
1261 	{ SST(0x19, 0x03, SS_RDEF,
1262 	    "Defect list error in grown list") },
1263 	/* DTLPWROMAEBKVF */
1264 	{ SST(0x1A, 0x00, SS_RDEF,
1265 	    "Parameter list length error") },
1266 	/* DTLPWROMAEBKVF */
1267 	{ SST(0x1B, 0x00, SS_RDEF,
1268 	    "Synchronous data transfer error") },
1269 	/* D     O   BK   */
1270 	{ SST(0x1C, 0x00, SS_RDEF,
1271 	    "Defect list not found") },
1272 	/* D     O   BK   */
1273 	{ SST(0x1C, 0x01, SS_RDEF,
1274 	    "Primary defect list not found") },
1275 	/* D     O   BK   */
1276 	{ SST(0x1C, 0x02, SS_RDEF,
1277 	    "Grown defect list not found") },
1278 	/* DT  WRO   BK   */
1279 	{ SST(0x1D, 0x00, SS_FATAL,
1280 	    "Miscompare during verify operation") },
1281 	/* D   W O   BK   */
1282 	{ SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1283 	    "Recovered ID with ECC correction") },
1284 	/* D     O    K   */
1285 	{ SST(0x1F, 0x00, SS_RDEF,
1286 	    "Partial defect list transfer") },
1287 	/* DTLPWROMAEBKVF */
1288 	{ SST(0x20, 0x00, SS_FATAL | EINVAL,
1289 	    "Invalid command operation code") },
1290 	/* DT PWROMAEBK   */
1291 	{ SST(0x20, 0x01, SS_RDEF,	/* XXX TBD */
1292 	    "Access denied - initiator pending-enrolled") },
1293 	/* DT PWROMAEBK   */
1294 	{ SST(0x20, 0x02, SS_RDEF,	/* XXX TBD */
1295 	    "Access denied - no access rights") },
1296 	/* DT PWROMAEBK   */
1297 	{ SST(0x20, 0x03, SS_RDEF,	/* XXX TBD */
1298 	    "Access denied - invalid mgmt ID key") },
1299 	/*  T             */
1300 	{ SST(0x20, 0x04, SS_RDEF,	/* XXX TBD */
1301 	    "Illegal command while in write capable state") },
1302 	/*  T             */
1303 	{ SST(0x20, 0x05, SS_RDEF,	/* XXX TBD */
1304 	    "Obsolete") },
1305 	/*  T             */
1306 	{ SST(0x20, 0x06, SS_RDEF,	/* XXX TBD */
1307 	    "Illegal command while in explicit address mode") },
1308 	/*  T             */
1309 	{ SST(0x20, 0x07, SS_RDEF,	/* XXX TBD */
1310 	    "Illegal command while in implicit address mode") },
1311 	/* DT PWROMAEBK   */
1312 	{ SST(0x20, 0x08, SS_RDEF,	/* XXX TBD */
1313 	    "Access denied - enrollment conflict") },
1314 	/* DT PWROMAEBK   */
1315 	{ SST(0x20, 0x09, SS_RDEF,	/* XXX TBD */
1316 	    "Access denied - invalid LU identifier") },
1317 	/* DT PWROMAEBK   */
1318 	{ SST(0x20, 0x0A, SS_RDEF,	/* XXX TBD */
1319 	    "Access denied - invalid proxy token") },
1320 	/* DT PWROMAEBK   */
1321 	{ SST(0x20, 0x0B, SS_RDEF,	/* XXX TBD */
1322 	    "Access denied - ACL LUN conflict") },
1323 	/* DT  WRO   BK   */
1324 	{ SST(0x21, 0x00, SS_FATAL | EINVAL,
1325 	    "Logical block address out of range") },
1326 	/* DT  WROM  BK   */
1327 	{ SST(0x21, 0x01, SS_FATAL | EINVAL,
1328 	    "Invalid element address") },
1329 	/*      R         */
1330 	{ SST(0x21, 0x02, SS_RDEF,	/* XXX TBD */
1331 	    "Invalid address for write") },
1332 	/*      R         */
1333 	{ SST(0x21, 0x03, SS_RDEF,	/* XXX TBD */
1334 	    "Invalid write crossing layer jump") },
1335 	/* D              */
1336 	{ SST(0x22, 0x00, SS_FATAL | EINVAL,
1337 	    "Illegal function (use 20 00, 24 00, or 26 00)") },
1338 	/* DTLPWROMAEBKVF */
1339 	{ SST(0x24, 0x00, SS_FATAL | EINVAL,
1340 	    "Invalid field in CDB") },
1341 	/* DTLPWRO AEBKVF */
1342 	{ SST(0x24, 0x01, SS_RDEF,	/* XXX TBD */
1343 	    "CDB decryption error") },
1344 	/*  T             */
1345 	{ SST(0x24, 0x02, SS_RDEF,	/* XXX TBD */
1346 	    "Obsolete") },
1347 	/*  T             */
1348 	{ SST(0x24, 0x03, SS_RDEF,	/* XXX TBD */
1349 	    "Obsolete") },
1350 	/*              F */
1351 	{ SST(0x24, 0x04, SS_RDEF,	/* XXX TBD */
1352 	    "Security audit value frozen") },
1353 	/*              F */
1354 	{ SST(0x24, 0x05, SS_RDEF,	/* XXX TBD */
1355 	    "Security working key frozen") },
1356 	/*              F */
1357 	{ SST(0x24, 0x06, SS_RDEF,	/* XXX TBD */
1358 	    "NONCE not unique") },
1359 	/*              F */
1360 	{ SST(0x24, 0x07, SS_RDEF,	/* XXX TBD */
1361 	    "NONCE timestamp out of range") },
1362 	/* DT   R MAEBKV  */
1363 	{ SST(0x24, 0x08, SS_RDEF,	/* XXX TBD */
1364 	    "Invalid XCDB") },
1365 	/* DTLPWROMAEBKVF */
1366 	{ SST(0x25, 0x00, SS_FATAL | ENXIO,
1367 	    "Logical unit not supported") },
1368 	/* DTLPWROMAEBKVF */
1369 	{ SST(0x26, 0x00, SS_FATAL | EINVAL,
1370 	    "Invalid field in parameter list") },
1371 	/* DTLPWROMAEBKVF */
1372 	{ SST(0x26, 0x01, SS_FATAL | EINVAL,
1373 	    "Parameter not supported") },
1374 	/* DTLPWROMAEBKVF */
1375 	{ SST(0x26, 0x02, SS_FATAL | EINVAL,
1376 	    "Parameter value invalid") },
1377 	/* DTLPWROMAE K   */
1378 	{ SST(0x26, 0x03, SS_FATAL | EINVAL,
1379 	    "Threshold parameters not supported") },
1380 	/* DTLPWROMAEBKVF */
1381 	{ SST(0x26, 0x04, SS_FATAL | EINVAL,
1382 	    "Invalid release of persistent reservation") },
1383 	/* DTLPWRO A BK   */
1384 	{ SST(0x26, 0x05, SS_RDEF,	/* XXX TBD */
1385 	    "Data decryption error") },
1386 	/* DTLPWRO    K   */
1387 	{ SST(0x26, 0x06, SS_RDEF,	/* XXX TBD */
1388 	    "Too many target descriptors") },
1389 	/* DTLPWRO    K   */
1390 	{ SST(0x26, 0x07, SS_RDEF,	/* XXX TBD */
1391 	    "Unsupported target descriptor type code") },
1392 	/* DTLPWRO    K   */
1393 	{ SST(0x26, 0x08, SS_RDEF,	/* XXX TBD */
1394 	    "Too many segment descriptors") },
1395 	/* DTLPWRO    K   */
1396 	{ SST(0x26, 0x09, SS_RDEF,	/* XXX TBD */
1397 	    "Unsupported segment descriptor type code") },
1398 	/* DTLPWRO    K   */
1399 	{ SST(0x26, 0x0A, SS_RDEF,	/* XXX TBD */
1400 	    "Unexpected inexact segment") },
1401 	/* DTLPWRO    K   */
1402 	{ SST(0x26, 0x0B, SS_RDEF,	/* XXX TBD */
1403 	    "Inline data length exceeded") },
1404 	/* DTLPWRO    K   */
1405 	{ SST(0x26, 0x0C, SS_RDEF,	/* XXX TBD */
1406 	    "Invalid operation for copy source or destination") },
1407 	/* DTLPWRO    K   */
1408 	{ SST(0x26, 0x0D, SS_RDEF,	/* XXX TBD */
1409 	    "Copy segment granularity violation") },
1410 	/* DT PWROMAEBK   */
1411 	{ SST(0x26, 0x0E, SS_RDEF,	/* XXX TBD */
1412 	    "Invalid parameter while port is enabled") },
1413 	/*              F */
1414 	{ SST(0x26, 0x0F, SS_RDEF,	/* XXX TBD */
1415 	    "Invalid data-out buffer integrity check value") },
1416 	/*  T             */
1417 	{ SST(0x26, 0x10, SS_RDEF,	/* XXX TBD */
1418 	    "Data decryption key fail limit reached") },
1419 	/*  T             */
1420 	{ SST(0x26, 0x11, SS_RDEF,	/* XXX TBD */
1421 	    "Incomplete key-associated data set") },
1422 	/*  T             */
1423 	{ SST(0x26, 0x12, SS_RDEF,	/* XXX TBD */
1424 	    "Vendor specific key reference not found") },
1425 	/* DT  WRO   BK   */
1426 	{ SST(0x27, 0x00, SS_FATAL | EACCES,
1427 	    "Write protected") },
1428 	/* DT  WRO   BK   */
1429 	{ SST(0x27, 0x01, SS_FATAL | EACCES,
1430 	    "Hardware write protected") },
1431 	/* DT  WRO   BK   */
1432 	{ SST(0x27, 0x02, SS_FATAL | EACCES,
1433 	    "Logical unit software write protected") },
1434 	/*  T   R         */
1435 	{ SST(0x27, 0x03, SS_FATAL | EACCES,
1436 	    "Associated write protect") },
1437 	/*  T   R         */
1438 	{ SST(0x27, 0x04, SS_FATAL | EACCES,
1439 	    "Persistent write protect") },
1440 	/*  T   R         */
1441 	{ SST(0x27, 0x05, SS_FATAL | EACCES,
1442 	    "Permanent write protect") },
1443 	/*      R       F */
1444 	{ SST(0x27, 0x06, SS_RDEF,	/* XXX TBD */
1445 	    "Conditional write protect") },
1446 	/* DTLPWROMAEBKVF */
1447 	{ SST(0x28, 0x00, SS_FATAL | ENXIO,
1448 	    "Not ready to ready change, medium may have changed") },
1449 	/* DT  WROM  B    */
1450 	{ SST(0x28, 0x01, SS_FATAL | ENXIO,
1451 	    "Import or export element accessed") },
1452 	/*      R         */
1453 	{ SST(0x28, 0x02, SS_RDEF,	/* XXX TBD */
1454 	    "Format-layer may have changed") },
1455 	/*        M       */
1456 	{ SST(0x28, 0x03, SS_RDEF,	/* XXX TBD */
1457 	    "Import/export element accessed, medium changed") },
1458 	/*
1459 	 * XXX JGibbs - All of these should use the same errno, but I don't
1460 	 * think ENXIO is the correct choice.  Should we borrow from
1461 	 * the networking errnos?  ECONNRESET anyone?
1462 	 */
1463 	/* DTLPWROMAEBKVF */
1464 	{ SST(0x29, 0x00, SS_FATAL | ENXIO,
1465 	    "Power on, reset, or bus device reset occurred") },
1466 	/* DTLPWROMAEBKVF */
1467 	{ SST(0x29, 0x01, SS_RDEF,
1468 	    "Power on occurred") },
1469 	/* DTLPWROMAEBKVF */
1470 	{ SST(0x29, 0x02, SS_RDEF,
1471 	    "SCSI bus reset occurred") },
1472 	/* DTLPWROMAEBKVF */
1473 	{ SST(0x29, 0x03, SS_RDEF,
1474 	    "Bus device reset function occurred") },
1475 	/* DTLPWROMAEBKVF */
1476 	{ SST(0x29, 0x04, SS_RDEF,
1477 	    "Device internal reset") },
1478 	/* DTLPWROMAEBKVF */
1479 	{ SST(0x29, 0x05, SS_RDEF,
1480 	    "Transceiver mode changed to single-ended") },
1481 	/* DTLPWROMAEBKVF */
1482 	{ SST(0x29, 0x06, SS_RDEF,
1483 	    "Transceiver mode changed to LVD") },
1484 	/* DTLPWROMAEBKVF */
1485 	{ SST(0x29, 0x07, SS_RDEF,	/* XXX TBD */
1486 	    "I_T nexus loss occurred") },
1487 	/* DTL WROMAEBKVF */
1488 	{ SST(0x2A, 0x00, SS_RDEF,
1489 	    "Parameters changed") },
1490 	/* DTL WROMAEBKVF */
1491 	{ SST(0x2A, 0x01, SS_RDEF,
1492 	    "Mode parameters changed") },
1493 	/* DTL WROMAE K   */
1494 	{ SST(0x2A, 0x02, SS_RDEF,
1495 	    "Log parameters changed") },
1496 	/* DTLPWROMAE K   */
1497 	{ SST(0x2A, 0x03, SS_RDEF,
1498 	    "Reservations preempted") },
1499 	/* DTLPWROMAE     */
1500 	{ SST(0x2A, 0x04, SS_RDEF,	/* XXX TBD */
1501 	    "Reservations released") },
1502 	/* DTLPWROMAE     */
1503 	{ SST(0x2A, 0x05, SS_RDEF,	/* XXX TBD */
1504 	    "Registrations preempted") },
1505 	/* DTLPWROMAEBKVF */
1506 	{ SST(0x2A, 0x06, SS_RDEF,	/* XXX TBD */
1507 	    "Asymmetric access state changed") },
1508 	/* DTLPWROMAEBKVF */
1509 	{ SST(0x2A, 0x07, SS_RDEF,	/* XXX TBD */
1510 	    "Implicit asymmetric access state transition failed") },
1511 	/* DT  WROMAEBKVF */
1512 	{ SST(0x2A, 0x08, SS_RDEF,	/* XXX TBD */
1513 	    "Priority changed") },
1514 	/* D              */
1515 	{ SST(0x2A, 0x09, SS_RDEF,	/* XXX TBD */
1516 	    "Capacity data has changed") },
1517 	/* DT             */
1518 	{ SST(0x2A, 0x0A, SS_RDEF,	/* XXX TBD */
1519 	    "Error history I_T nexus cleared") },
1520 	/* DT             */
1521 	{ SST(0x2A, 0x0B, SS_RDEF,	/* XXX TBD */
1522 	    "Error history snapshot released") },
1523 	/*              F */
1524 	{ SST(0x2A, 0x0C, SS_RDEF,	/* XXX TBD */
1525 	    "Error recovery attributes have changed") },
1526 	/*  T             */
1527 	{ SST(0x2A, 0x0D, SS_RDEF,	/* XXX TBD */
1528 	    "Data encryption capabilities changed") },
1529 	/* DT     M E  V  */
1530 	{ SST(0x2A, 0x10, SS_RDEF,	/* XXX TBD */
1531 	    "Timestamp changed") },
1532 	/*  T             */
1533 	{ SST(0x2A, 0x11, SS_RDEF,	/* XXX TBD */
1534 	    "Data encryption parameters changed by another I_T nexus") },
1535 	/*  T             */
1536 	{ SST(0x2A, 0x12, SS_RDEF,	/* XXX TBD */
1537 	    "Data encryption parameters changed by vendor specific event") },
1538 	/*  T             */
1539 	{ SST(0x2A, 0x13, SS_RDEF,	/* XXX TBD */
1540 	    "Data encryption key instance counter has changed") },
1541 	/* DT   R MAEBKV  */
1542 	{ SST(0x2A, 0x14, SS_RDEF,	/* XXX TBD */
1543 	    "SA creation capabilities data has changed") },
1544 	/* DTLPWRO    K   */
1545 	{ SST(0x2B, 0x00, SS_RDEF,
1546 	    "Copy cannot execute since host cannot disconnect") },
1547 	/* DTLPWROMAEBKVF */
1548 	{ SST(0x2C, 0x00, SS_RDEF,
1549 	    "Command sequence error") },
1550 	/*                */
1551 	{ SST(0x2C, 0x01, SS_RDEF,
1552 	    "Too many windows specified") },
1553 	/*                */
1554 	{ SST(0x2C, 0x02, SS_RDEF,
1555 	    "Invalid combination of windows specified") },
1556 	/*      R         */
1557 	{ SST(0x2C, 0x03, SS_RDEF,
1558 	    "Current program area is not empty") },
1559 	/*      R         */
1560 	{ SST(0x2C, 0x04, SS_RDEF,
1561 	    "Current program area is empty") },
1562 	/*           B    */
1563 	{ SST(0x2C, 0x05, SS_RDEF,	/* XXX TBD */
1564 	    "Illegal power condition request") },
1565 	/*      R         */
1566 	{ SST(0x2C, 0x06, SS_RDEF,	/* XXX TBD */
1567 	    "Persistent prevent conflict") },
1568 	/* DTLPWROMAEBKVF */
1569 	{ SST(0x2C, 0x07, SS_RDEF,	/* XXX TBD */
1570 	    "Previous busy status") },
1571 	/* DTLPWROMAEBKVF */
1572 	{ SST(0x2C, 0x08, SS_RDEF,	/* XXX TBD */
1573 	    "Previous task set full status") },
1574 	/* DTLPWROM EBKVF */
1575 	{ SST(0x2C, 0x09, SS_RDEF,	/* XXX TBD */
1576 	    "Previous reservation conflict status") },
1577 	/*              F */
1578 	{ SST(0x2C, 0x0A, SS_RDEF,	/* XXX TBD */
1579 	    "Partition or collection contains user objects") },
1580 	/*  T             */
1581 	{ SST(0x2C, 0x0B, SS_RDEF,	/* XXX TBD */
1582 	    "Not reserved") },
1583 	/*  T             */
1584 	{ SST(0x2D, 0x00, SS_RDEF,
1585 	    "Overwrite error on update in place") },
1586 	/*      R         */
1587 	{ SST(0x2E, 0x00, SS_RDEF,	/* XXX TBD */
1588 	    "Insufficient time for operation") },
1589 	/* DTLPWROMAEBKVF */
1590 	{ SST(0x2F, 0x00, SS_RDEF,
1591 	    "Commands cleared by another initiator") },
1592 	/* D              */
1593 	{ SST(0x2F, 0x01, SS_RDEF,	/* XXX TBD */
1594 	    "Commands cleared by power loss notification") },
1595 	/* DTLPWROMAEBKVF */
1596 	{ SST(0x2F, 0x02, SS_RDEF,	/* XXX TBD */
1597 	    "Commands cleared by device server") },
1598 	/* DT  WROM  BK   */
1599 	{ SST(0x30, 0x00, SS_RDEF,
1600 	    "Incompatible medium installed") },
1601 	/* DT  WRO   BK   */
1602 	{ SST(0x30, 0x01, SS_RDEF,
1603 	    "Cannot read medium - unknown format") },
1604 	/* DT  WRO   BK   */
1605 	{ SST(0x30, 0x02, SS_RDEF,
1606 	    "Cannot read medium - incompatible format") },
1607 	/* DT   R     K   */
1608 	{ SST(0x30, 0x03, SS_RDEF,
1609 	    "Cleaning cartridge installed") },
1610 	/* DT  WRO   BK   */
1611 	{ SST(0x30, 0x04, SS_RDEF,
1612 	    "Cannot write medium - unknown format") },
1613 	/* DT  WRO   BK   */
1614 	{ SST(0x30, 0x05, SS_RDEF,
1615 	    "Cannot write medium - incompatible format") },
1616 	/* DT  WRO   B    */
1617 	{ SST(0x30, 0x06, SS_RDEF,
1618 	    "Cannot format medium - incompatible medium") },
1619 	/* DTL WROMAEBKVF */
1620 	{ SST(0x30, 0x07, SS_RDEF,
1621 	    "Cleaning failure") },
1622 	/*      R         */
1623 	{ SST(0x30, 0x08, SS_RDEF,
1624 	    "Cannot write - application code mismatch") },
1625 	/*      R         */
1626 	{ SST(0x30, 0x09, SS_RDEF,
1627 	    "Current session not fixated for append") },
1628 	/* DT  WRO AEBK   */
1629 	{ SST(0x30, 0x0A, SS_RDEF,	/* XXX TBD */
1630 	    "Cleaning request rejected") },
1631 	/*  T             */
1632 	{ SST(0x30, 0x0C, SS_RDEF,	/* XXX TBD */
1633 	    "WORM medium - overwrite attempted") },
1634 	/*  T             */
1635 	{ SST(0x30, 0x0D, SS_RDEF,	/* XXX TBD */
1636 	    "WORM medium - integrity check") },
1637 	/*      R         */
1638 	{ SST(0x30, 0x10, SS_RDEF,	/* XXX TBD */
1639 	    "Medium not formatted") },
1640 	/*        M       */
1641 	{ SST(0x30, 0x11, SS_RDEF,	/* XXX TBD */
1642 	    "Incompatible volume type") },
1643 	/*        M       */
1644 	{ SST(0x30, 0x12, SS_RDEF,	/* XXX TBD */
1645 	    "Incompatible volume qualifier") },
1646 	/* DT  WRO   BK   */
1647 	{ SST(0x31, 0x00, SS_RDEF,
1648 	    "Medium format corrupted") },
1649 	/* D L  RO   B    */
1650 	{ SST(0x31, 0x01, SS_RDEF,
1651 	    "Format command failed") },
1652 	/*      R         */
1653 	{ SST(0x31, 0x02, SS_RDEF,	/* XXX TBD */
1654 	    "Zoned formatting failed due to spare linking") },
1655 	/* D   W O   BK   */
1656 	{ SST(0x32, 0x00, SS_RDEF,
1657 	    "No defect spare location available") },
1658 	/* D   W O   BK   */
1659 	{ SST(0x32, 0x01, SS_RDEF,
1660 	    "Defect list update failure") },
1661 	/*  T             */
1662 	{ SST(0x33, 0x00, SS_RDEF,
1663 	    "Tape length error") },
1664 	/* DTLPWROMAEBKVF */
1665 	{ SST(0x34, 0x00, SS_RDEF,
1666 	    "Enclosure failure") },
1667 	/* DTLPWROMAEBKVF */
1668 	{ SST(0x35, 0x00, SS_RDEF,
1669 	    "Enclosure services failure") },
1670 	/* DTLPWROMAEBKVF */
1671 	{ SST(0x35, 0x01, SS_RDEF,
1672 	    "Unsupported enclosure function") },
1673 	/* DTLPWROMAEBKVF */
1674 	{ SST(0x35, 0x02, SS_RDEF,
1675 	    "Enclosure services unavailable") },
1676 	/* DTLPWROMAEBKVF */
1677 	{ SST(0x35, 0x03, SS_RDEF,
1678 	    "Enclosure services transfer failure") },
1679 	/* DTLPWROMAEBKVF */
1680 	{ SST(0x35, 0x04, SS_RDEF,
1681 	    "Enclosure services transfer refused") },
1682 	/* DTL WROMAEBKVF */
1683 	{ SST(0x35, 0x05, SS_RDEF,	/* XXX TBD */
1684 	    "Enclosure services checksum error") },
1685 	/*   L            */
1686 	{ SST(0x36, 0x00, SS_RDEF,
1687 	    "Ribbon, ink, or toner failure") },
1688 	/* DTL WROMAEBKVF */
1689 	{ SST(0x37, 0x00, SS_RDEF,
1690 	    "Rounded parameter") },
1691 	/*           B    */
1692 	{ SST(0x38, 0x00, SS_RDEF,	/* XXX TBD */
1693 	    "Event status notification") },
1694 	/*           B    */
1695 	{ SST(0x38, 0x02, SS_RDEF,	/* XXX TBD */
1696 	    "ESN - power management class event") },
1697 	/*           B    */
1698 	{ SST(0x38, 0x04, SS_RDEF,	/* XXX TBD */
1699 	    "ESN - media class event") },
1700 	/*           B    */
1701 	{ SST(0x38, 0x06, SS_RDEF,	/* XXX TBD */
1702 	    "ESN - device busy class event") },
1703 	/* DTL WROMAE K   */
1704 	{ SST(0x39, 0x00, SS_RDEF,
1705 	    "Saving parameters not supported") },
1706 	/* DTL WROM  BK   */
1707 	{ SST(0x3A, 0x00, SS_FATAL | ENXIO,
1708 	    "Medium not present") },
1709 	/* DT  WROM  BK   */
1710 	{ SST(0x3A, 0x01, SS_FATAL | ENXIO,
1711 	    "Medium not present - tray closed") },
1712 	/* DT  WROM  BK   */
1713 	{ SST(0x3A, 0x02, SS_FATAL | ENXIO,
1714 	    "Medium not present - tray open") },
1715 	/* DT  WROM  B    */
1716 	{ SST(0x3A, 0x03, SS_RDEF,	/* XXX TBD */
1717 	    "Medium not present - loadable") },
1718 	/* DT  WRO   B    */
1719 	{ SST(0x3A, 0x04, SS_RDEF,	/* XXX TBD */
1720 	    "Medium not present - medium auxiliary memory accessible") },
1721 	/*  TL            */
1722 	{ SST(0x3B, 0x00, SS_RDEF,
1723 	    "Sequential positioning error") },
1724 	/*  T             */
1725 	{ SST(0x3B, 0x01, SS_RDEF,
1726 	    "Tape position error at beginning-of-medium") },
1727 	/*  T             */
1728 	{ SST(0x3B, 0x02, SS_RDEF,
1729 	    "Tape position error at end-of-medium") },
1730 	/*   L            */
1731 	{ SST(0x3B, 0x03, SS_RDEF,
1732 	    "Tape or electronic vertical forms unit not ready") },
1733 	/*   L            */
1734 	{ SST(0x3B, 0x04, SS_RDEF,
1735 	    "Slew failure") },
1736 	/*   L            */
1737 	{ SST(0x3B, 0x05, SS_RDEF,
1738 	    "Paper jam") },
1739 	/*   L            */
1740 	{ SST(0x3B, 0x06, SS_RDEF,
1741 	    "Failed to sense top-of-form") },
1742 	/*   L            */
1743 	{ SST(0x3B, 0x07, SS_RDEF,
1744 	    "Failed to sense bottom-of-form") },
1745 	/*  T             */
1746 	{ SST(0x3B, 0x08, SS_RDEF,
1747 	    "Reposition error") },
1748 	/*                */
1749 	{ SST(0x3B, 0x09, SS_RDEF,
1750 	    "Read past end of medium") },
1751 	/*                */
1752 	{ SST(0x3B, 0x0A, SS_RDEF,
1753 	    "Read past beginning of medium") },
1754 	/*                */
1755 	{ SST(0x3B, 0x0B, SS_RDEF,
1756 	    "Position past end of medium") },
1757 	/*  T             */
1758 	{ SST(0x3B, 0x0C, SS_RDEF,
1759 	    "Position past beginning of medium") },
1760 	/* DT  WROM  BK   */
1761 	{ SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
1762 	    "Medium destination element full") },
1763 	/* DT  WROM  BK   */
1764 	{ SST(0x3B, 0x0E, SS_RDEF,
1765 	    "Medium source element empty") },
1766 	/*      R         */
1767 	{ SST(0x3B, 0x0F, SS_RDEF,
1768 	    "End of medium reached") },
1769 	/* DT  WROM  BK   */
1770 	{ SST(0x3B, 0x11, SS_RDEF,
1771 	    "Medium magazine not accessible") },
1772 	/* DT  WROM  BK   */
1773 	{ SST(0x3B, 0x12, SS_RDEF,
1774 	    "Medium magazine removed") },
1775 	/* DT  WROM  BK   */
1776 	{ SST(0x3B, 0x13, SS_RDEF,
1777 	    "Medium magazine inserted") },
1778 	/* DT  WROM  BK   */
1779 	{ SST(0x3B, 0x14, SS_RDEF,
1780 	    "Medium magazine locked") },
1781 	/* DT  WROM  BK   */
1782 	{ SST(0x3B, 0x15, SS_RDEF,
1783 	    "Medium magazine unlocked") },
1784 	/*      R         */
1785 	{ SST(0x3B, 0x16, SS_RDEF,	/* XXX TBD */
1786 	    "Mechanical positioning or changer error") },
1787 	/*              F */
1788 	{ SST(0x3B, 0x17, SS_RDEF,	/* XXX TBD */
1789 	    "Read past end of user object") },
1790 	/*        M       */
1791 	{ SST(0x3B, 0x18, SS_RDEF,	/* XXX TBD */
1792 	    "Element disabled") },
1793 	/*        M       */
1794 	{ SST(0x3B, 0x19, SS_RDEF,	/* XXX TBD */
1795 	    "Element enabled") },
1796 	/*        M       */
1797 	{ SST(0x3B, 0x1A, SS_RDEF,	/* XXX TBD */
1798 	    "Data transfer device removed") },
1799 	/*        M       */
1800 	{ SST(0x3B, 0x1B, SS_RDEF,	/* XXX TBD */
1801 	    "Data transfer device inserted") },
1802 	/* DTLPWROMAE K   */
1803 	{ SST(0x3D, 0x00, SS_RDEF,
1804 	    "Invalid bits in IDENTIFY message") },
1805 	/* DTLPWROMAEBKVF */
1806 	{ SST(0x3E, 0x00, SS_RDEF,
1807 	    "Logical unit has not self-configured yet") },
1808 	/* DTLPWROMAEBKVF */
1809 	{ SST(0x3E, 0x01, SS_RDEF,
1810 	    "Logical unit failure") },
1811 	/* DTLPWROMAEBKVF */
1812 	{ SST(0x3E, 0x02, SS_RDEF,
1813 	    "Timeout on logical unit") },
1814 	/* DTLPWROMAEBKVF */
1815 	{ SST(0x3E, 0x03, SS_RDEF,	/* XXX TBD */
1816 	    "Logical unit failed self-test") },
1817 	/* DTLPWROMAEBKVF */
1818 	{ SST(0x3E, 0x04, SS_RDEF,	/* XXX TBD */
1819 	    "Logical unit unable to update self-test log") },
1820 	/* DTLPWROMAEBKVF */
1821 	{ SST(0x3F, 0x00, SS_RDEF,
1822 	    "Target operating conditions have changed") },
1823 	/* DTLPWROMAEBKVF */
1824 	{ SST(0x3F, 0x01, SS_RDEF,
1825 	    "Microcode has been changed") },
1826 	/* DTLPWROM  BK   */
1827 	{ SST(0x3F, 0x02, SS_RDEF,
1828 	    "Changed operating definition") },
1829 	/* DTLPWROMAEBKVF */
1830 	{ SST(0x3F, 0x03, SS_RDEF,
1831 	    "INQUIRY data has changed") },
1832 	/* DT  WROMAEBK   */
1833 	{ SST(0x3F, 0x04, SS_RDEF,
1834 	    "Component device attached") },
1835 	/* DT  WROMAEBK   */
1836 	{ SST(0x3F, 0x05, SS_RDEF,
1837 	    "Device identifier changed") },
1838 	/* DT  WROMAEB    */
1839 	{ SST(0x3F, 0x06, SS_RDEF,
1840 	    "Redundancy group created or modified") },
1841 	/* DT  WROMAEB    */
1842 	{ SST(0x3F, 0x07, SS_RDEF,
1843 	    "Redundancy group deleted") },
1844 	/* DT  WROMAEB    */
1845 	{ SST(0x3F, 0x08, SS_RDEF,
1846 	    "Spare created or modified") },
1847 	/* DT  WROMAEB    */
1848 	{ SST(0x3F, 0x09, SS_RDEF,
1849 	    "Spare deleted") },
1850 	/* DT  WROMAEBK   */
1851 	{ SST(0x3F, 0x0A, SS_RDEF,
1852 	    "Volume set created or modified") },
1853 	/* DT  WROMAEBK   */
1854 	{ SST(0x3F, 0x0B, SS_RDEF,
1855 	    "Volume set deleted") },
1856 	/* DT  WROMAEBK   */
1857 	{ SST(0x3F, 0x0C, SS_RDEF,
1858 	    "Volume set deassigned") },
1859 	/* DT  WROMAEBK   */
1860 	{ SST(0x3F, 0x0D, SS_RDEF,
1861 	    "Volume set reassigned") },
1862 	/* DTLPWROMAE     */
1863 	{ SST(0x3F, 0x0E, SS_RDEF,	/* XXX TBD */
1864 	    "Reported LUNs data has changed") },
1865 	/* DTLPWROMAEBKVF */
1866 	{ SST(0x3F, 0x0F, SS_RDEF,	/* XXX TBD */
1867 	    "Echo buffer overwritten") },
1868 	/* DT  WROM  B    */
1869 	{ SST(0x3F, 0x10, SS_RDEF,	/* XXX TBD */
1870 	    "Medium loadable") },
1871 	/* DT  WROM  B    */
1872 	{ SST(0x3F, 0x11, SS_RDEF,	/* XXX TBD */
1873 	    "Medium auxiliary memory accessible") },
1874 	/* DTLPWR MAEBK F */
1875 	{ SST(0x3F, 0x12, SS_RDEF,	/* XXX TBD */
1876 	    "iSCSI IP address added") },
1877 	/* DTLPWR MAEBK F */
1878 	{ SST(0x3F, 0x13, SS_RDEF,	/* XXX TBD */
1879 	    "iSCSI IP address removed") },
1880 	/* DTLPWR MAEBK F */
1881 	{ SST(0x3F, 0x14, SS_RDEF,	/* XXX TBD */
1882 	    "iSCSI IP address changed") },
1883 	/* D              */
1884 	{ SST(0x40, 0x00, SS_RDEF,
1885 	    "RAM failure") },		/* deprecated - use 40 NN instead */
1886 	/* DTLPWROMAEBKVF */
1887 	{ SST(0x40, 0x80, SS_RDEF,
1888 	    "Diagnostic failure: ASCQ = Component ID") },
1889 	/* DTLPWROMAEBKVF */
1890 	{ SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
1891 	    NULL) },			/* Range 0x80->0xFF */
1892 	/* D              */
1893 	{ SST(0x41, 0x00, SS_RDEF,
1894 	    "Data path failure") },	/* deprecated - use 40 NN instead */
1895 	/* D              */
1896 	{ SST(0x42, 0x00, SS_RDEF,
1897 	    "Power-on or self-test failure") },
1898 					/* deprecated - use 40 NN instead */
1899 	/* DTLPWROMAEBKVF */
1900 	{ SST(0x43, 0x00, SS_RDEF,
1901 	    "Message error") },
1902 	/* DTLPWROMAEBKVF */
1903 	{ SST(0x44, 0x00, SS_RDEF,
1904 	    "Internal target failure") },
1905 	/* DT        B    */
1906 	{ SST(0x44, 0x71, SS_RDEF,	/* XXX TBD */
1907 	    "ATA device failed set features") },
1908 	/* DTLPWROMAEBKVF */
1909 	{ SST(0x45, 0x00, SS_RDEF,
1910 	    "Select or reselect failure") },
1911 	/* DTLPWROM  BK   */
1912 	{ SST(0x46, 0x00, SS_RDEF,
1913 	    "Unsuccessful soft reset") },
1914 	/* DTLPWROMAEBKVF */
1915 	{ SST(0x47, 0x00, SS_RDEF,
1916 	    "SCSI parity error") },
1917 	/* DTLPWROMAEBKVF */
1918 	{ SST(0x47, 0x01, SS_RDEF,	/* XXX TBD */
1919 	    "Data phase CRC error detected") },
1920 	/* DTLPWROMAEBKVF */
1921 	{ SST(0x47, 0x02, SS_RDEF,	/* XXX TBD */
1922 	    "SCSI parity error detected during ST data phase") },
1923 	/* DTLPWROMAEBKVF */
1924 	{ SST(0x47, 0x03, SS_RDEF,	/* XXX TBD */
1925 	    "Information unit iuCRC error detected") },
1926 	/* DTLPWROMAEBKVF */
1927 	{ SST(0x47, 0x04, SS_RDEF,	/* XXX TBD */
1928 	    "Asynchronous information protection error detected") },
1929 	/* DTLPWROMAEBKVF */
1930 	{ SST(0x47, 0x05, SS_RDEF,	/* XXX TBD */
1931 	    "Protocol service CRC error") },
1932 	/* DT     MAEBKVF */
1933 	{ SST(0x47, 0x06, SS_RDEF,	/* XXX TBD */
1934 	    "PHY test function in progress") },
1935 	/* DT PWROMAEBK   */
1936 	{ SST(0x47, 0x7F, SS_RDEF,	/* XXX TBD */
1937 	    "Some commands cleared by iSCSI protocol event") },
1938 	/* DTLPWROMAEBKVF */
1939 	{ SST(0x48, 0x00, SS_RDEF,
1940 	    "Initiator detected error message received") },
1941 	/* DTLPWROMAEBKVF */
1942 	{ SST(0x49, 0x00, SS_RDEF,
1943 	    "Invalid message error") },
1944 	/* DTLPWROMAEBKVF */
1945 	{ SST(0x4A, 0x00, SS_RDEF,
1946 	    "Command phase error") },
1947 	/* DTLPWROMAEBKVF */
1948 	{ SST(0x4B, 0x00, SS_RDEF,
1949 	    "Data phase error") },
1950 	/* DT PWROMAEBK   */
1951 	{ SST(0x4B, 0x01, SS_RDEF,	/* XXX TBD */
1952 	    "Invalid target port transfer tag received") },
1953 	/* DT PWROMAEBK   */
1954 	{ SST(0x4B, 0x02, SS_RDEF,	/* XXX TBD */
1955 	    "Too much write data") },
1956 	/* DT PWROMAEBK   */
1957 	{ SST(0x4B, 0x03, SS_RDEF,	/* XXX TBD */
1958 	    "ACK/NAK timeout") },
1959 	/* DT PWROMAEBK   */
1960 	{ SST(0x4B, 0x04, SS_RDEF,	/* XXX TBD */
1961 	    "NAK received") },
1962 	/* DT PWROMAEBK   */
1963 	{ SST(0x4B, 0x05, SS_RDEF,	/* XXX TBD */
1964 	    "Data offset error") },
1965 	/* DT PWROMAEBK   */
1966 	{ SST(0x4B, 0x06, SS_RDEF,	/* XXX TBD */
1967 	    "Initiator response timeout") },
1968 	/* DTLPWROMAEBKVF */
1969 	{ SST(0x4C, 0x00, SS_RDEF,
1970 	    "Logical unit failed self-configuration") },
1971 	/* DTLPWROMAEBKVF */
1972 	{ SST(0x4D, 0x00, SS_RDEF,
1973 	    "Tagged overlapped commands: ASCQ = Queue tag ID") },
1974 	/* DTLPWROMAEBKVF */
1975 	{ SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
1976 	    NULL) },			/* Range 0x00->0xFF */
1977 	/* DTLPWROMAEBKVF */
1978 	{ SST(0x4E, 0x00, SS_RDEF,
1979 	    "Overlapped commands attempted") },
1980 	/*  T             */
1981 	{ SST(0x50, 0x00, SS_RDEF,
1982 	    "Write append error") },
1983 	/*  T             */
1984 	{ SST(0x50, 0x01, SS_RDEF,
1985 	    "Write append position error") },
1986 	/*  T             */
1987 	{ SST(0x50, 0x02, SS_RDEF,
1988 	    "Position error related to timing") },
1989 	/*  T   RO        */
1990 	{ SST(0x51, 0x00, SS_RDEF,
1991 	    "Erase failure") },
1992 	/*      R         */
1993 	{ SST(0x51, 0x01, SS_RDEF,	/* XXX TBD */
1994 	    "Erase failure - incomplete erase operation detected") },
1995 	/*  T             */
1996 	{ SST(0x52, 0x00, SS_RDEF,
1997 	    "Cartridge fault") },
1998 	/* DTL WROM  BK   */
1999 	{ SST(0x53, 0x00, SS_RDEF,
2000 	    "Media load or eject failed") },
2001 	/*  T             */
2002 	{ SST(0x53, 0x01, SS_RDEF,
2003 	    "Unload tape failure") },
2004 	/* DT  WROM  BK   */
2005 	{ SST(0x53, 0x02, SS_RDEF,
2006 	    "Medium removal prevented") },
2007 	/*        M       */
2008 	{ SST(0x53, 0x03, SS_RDEF,	/* XXX TBD */
2009 	    "Medium removal prevented by data transfer element") },
2010 	/*  T             */
2011 	{ SST(0x53, 0x04, SS_RDEF,	/* XXX TBD */
2012 	    "Medium thread or unthread failure") },
2013 	/*    P           */
2014 	{ SST(0x54, 0x00, SS_RDEF,
2015 	    "SCSI to host system interface failure") },
2016 	/*    P           */
2017 	{ SST(0x55, 0x00, SS_RDEF,
2018 	    "System resource failure") },
2019 	/* D     O   BK   */
2020 	{ SST(0x55, 0x01, SS_FATAL | ENOSPC,
2021 	    "System buffer full") },
2022 	/* DTLPWROMAE K   */
2023 	{ SST(0x55, 0x02, SS_RDEF,	/* XXX TBD */
2024 	    "Insufficient reservation resources") },
2025 	/* DTLPWROMAE K   */
2026 	{ SST(0x55, 0x03, SS_RDEF,	/* XXX TBD */
2027 	    "Insufficient resources") },
2028 	/* DTLPWROMAE K   */
2029 	{ SST(0x55, 0x04, SS_RDEF,	/* XXX TBD */
2030 	    "Insufficient registration resources") },
2031 	/* DT PWROMAEBK   */
2032 	{ SST(0x55, 0x05, SS_RDEF,	/* XXX TBD */
2033 	    "Insufficient access control resources") },
2034 	/* DT  WROM  B    */
2035 	{ SST(0x55, 0x06, SS_RDEF,	/* XXX TBD */
2036 	    "Auxiliary memory out of space") },
2037 	/*              F */
2038 	{ SST(0x55, 0x07, SS_RDEF,	/* XXX TBD */
2039 	    "Quota error") },
2040 	/*  T             */
2041 	{ SST(0x55, 0x08, SS_RDEF,	/* XXX TBD */
2042 	    "Maximum number of supplemental decryption keys exceeded") },
2043 	/*        M       */
2044 	{ SST(0x55, 0x09, SS_RDEF,	/* XXX TBD */
2045 	    "Medium auxiliary memory not accessible") },
2046 	/*        M       */
2047 	{ SST(0x55, 0x0A, SS_RDEF,	/* XXX TBD */
2048 	    "Data currently unavailable") },
2049 	/*      R         */
2050 	{ SST(0x57, 0x00, SS_RDEF,
2051 	    "Unable to recover table-of-contents") },
2052 	/*       O        */
2053 	{ SST(0x58, 0x00, SS_RDEF,
2054 	    "Generation does not exist") },
2055 	/*       O        */
2056 	{ SST(0x59, 0x00, SS_RDEF,
2057 	    "Updated block read") },
2058 	/* DTLPWRO   BK   */
2059 	{ SST(0x5A, 0x00, SS_RDEF,
2060 	    "Operator request or state change input") },
2061 	/* DT  WROM  BK   */
2062 	{ SST(0x5A, 0x01, SS_RDEF,
2063 	    "Operator medium removal request") },
2064 	/* DT  WRO A BK   */
2065 	{ SST(0x5A, 0x02, SS_RDEF,
2066 	    "Operator selected write protect") },
2067 	/* DT  WRO A BK   */
2068 	{ SST(0x5A, 0x03, SS_RDEF,
2069 	    "Operator selected write permit") },
2070 	/* DTLPWROM   K   */
2071 	{ SST(0x5B, 0x00, SS_RDEF,
2072 	    "Log exception") },
2073 	/* DTLPWROM   K   */
2074 	{ SST(0x5B, 0x01, SS_RDEF,
2075 	    "Threshold condition met") },
2076 	/* DTLPWROM   K   */
2077 	{ SST(0x5B, 0x02, SS_RDEF,
2078 	    "Log counter at maximum") },
2079 	/* DTLPWROM   K   */
2080 	{ SST(0x5B, 0x03, SS_RDEF,
2081 	    "Log list codes exhausted") },
2082 	/* D     O        */
2083 	{ SST(0x5C, 0x00, SS_RDEF,
2084 	    "RPL status change") },
2085 	/* D     O        */
2086 	{ SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2087 	    "Spindles synchronized") },
2088 	/* D     O        */
2089 	{ SST(0x5C, 0x02, SS_RDEF,
2090 	    "Spindles not synchronized") },
2091 	/* DTLPWROMAEBKVF */
2092 	{ SST(0x5D, 0x00, SS_RDEF,
2093 	    "Failure prediction threshold exceeded") },
2094 	/*      R    B    */
2095 	{ SST(0x5D, 0x01, SS_RDEF,	/* XXX TBD */
2096 	    "Media failure prediction threshold exceeded") },
2097 	/*      R         */
2098 	{ SST(0x5D, 0x02, SS_RDEF,	/* XXX TBD */
2099 	    "Logical unit failure prediction threshold exceeded") },
2100 	/*      R         */
2101 	{ SST(0x5D, 0x03, SS_RDEF,	/* XXX TBD */
2102 	    "Spare area exhaustion prediction threshold exceeded") },
2103 	/* D         B    */
2104 	{ SST(0x5D, 0x10, SS_RDEF,	/* XXX TBD */
2105 	    "Hardware impending failure general hard drive failure") },
2106 	/* D         B    */
2107 	{ SST(0x5D, 0x11, SS_RDEF,	/* XXX TBD */
2108 	    "Hardware impending failure drive error rate too high") },
2109 	/* D         B    */
2110 	{ SST(0x5D, 0x12, SS_RDEF,	/* XXX TBD */
2111 	    "Hardware impending failure data error rate too high") },
2112 	/* D         B    */
2113 	{ SST(0x5D, 0x13, SS_RDEF,	/* XXX TBD */
2114 	    "Hardware impending failure seek error rate too high") },
2115 	/* D         B    */
2116 	{ SST(0x5D, 0x14, SS_RDEF,	/* XXX TBD */
2117 	    "Hardware impending failure too many block reassigns") },
2118 	/* D         B    */
2119 	{ SST(0x5D, 0x15, SS_RDEF,	/* XXX TBD */
2120 	    "Hardware impending failure access times too high") },
2121 	/* D         B    */
2122 	{ SST(0x5D, 0x16, SS_RDEF,	/* XXX TBD */
2123 	    "Hardware impending failure start unit times too high") },
2124 	/* D         B    */
2125 	{ SST(0x5D, 0x17, SS_RDEF,	/* XXX TBD */
2126 	    "Hardware impending failure channel parametrics") },
2127 	/* D         B    */
2128 	{ SST(0x5D, 0x18, SS_RDEF,	/* XXX TBD */
2129 	    "Hardware impending failure controller detected") },
2130 	/* D         B    */
2131 	{ SST(0x5D, 0x19, SS_RDEF,	/* XXX TBD */
2132 	    "Hardware impending failure throughput performance") },
2133 	/* D         B    */
2134 	{ SST(0x5D, 0x1A, SS_RDEF,	/* XXX TBD */
2135 	    "Hardware impending failure seek time performance") },
2136 	/* D         B    */
2137 	{ SST(0x5D, 0x1B, SS_RDEF,	/* XXX TBD */
2138 	    "Hardware impending failure spin-up retry count") },
2139 	/* D         B    */
2140 	{ SST(0x5D, 0x1C, SS_RDEF,	/* XXX TBD */
2141 	    "Hardware impending failure drive calibration retry count") },
2142 	/* D         B    */
2143 	{ SST(0x5D, 0x20, SS_RDEF,	/* XXX TBD */
2144 	    "Controller impending failure general hard drive failure") },
2145 	/* D         B    */
2146 	{ SST(0x5D, 0x21, SS_RDEF,	/* XXX TBD */
2147 	    "Controller impending failure drive error rate too high") },
2148 	/* D         B    */
2149 	{ SST(0x5D, 0x22, SS_RDEF,	/* XXX TBD */
2150 	    "Controller impending failure data error rate too high") },
2151 	/* D         B    */
2152 	{ SST(0x5D, 0x23, SS_RDEF,	/* XXX TBD */
2153 	    "Controller impending failure seek error rate too high") },
2154 	/* D         B    */
2155 	{ SST(0x5D, 0x24, SS_RDEF,	/* XXX TBD */
2156 	    "Controller impending failure too many block reassigns") },
2157 	/* D         B    */
2158 	{ SST(0x5D, 0x25, SS_RDEF,	/* XXX TBD */
2159 	    "Controller impending failure access times too high") },
2160 	/* D         B    */
2161 	{ SST(0x5D, 0x26, SS_RDEF,	/* XXX TBD */
2162 	    "Controller impending failure start unit times too high") },
2163 	/* D         B    */
2164 	{ SST(0x5D, 0x27, SS_RDEF,	/* XXX TBD */
2165 	    "Controller impending failure channel parametrics") },
2166 	/* D         B    */
2167 	{ SST(0x5D, 0x28, SS_RDEF,	/* XXX TBD */
2168 	    "Controller impending failure controller detected") },
2169 	/* D         B    */
2170 	{ SST(0x5D, 0x29, SS_RDEF,	/* XXX TBD */
2171 	    "Controller impending failure throughput performance") },
2172 	/* D         B    */
2173 	{ SST(0x5D, 0x2A, SS_RDEF,	/* XXX TBD */
2174 	    "Controller impending failure seek time performance") },
2175 	/* D         B    */
2176 	{ SST(0x5D, 0x2B, SS_RDEF,	/* XXX TBD */
2177 	    "Controller impending failure spin-up retry count") },
2178 	/* D         B    */
2179 	{ SST(0x5D, 0x2C, SS_RDEF,	/* XXX TBD */
2180 	    "Controller impending failure drive calibration retry count") },
2181 	/* D         B    */
2182 	{ SST(0x5D, 0x30, SS_RDEF,	/* XXX TBD */
2183 	    "Data channel impending failure general hard drive failure") },
2184 	/* D         B    */
2185 	{ SST(0x5D, 0x31, SS_RDEF,	/* XXX TBD */
2186 	    "Data channel impending failure drive error rate too high") },
2187 	/* D         B    */
2188 	{ SST(0x5D, 0x32, SS_RDEF,	/* XXX TBD */
2189 	    "Data channel impending failure data error rate too high") },
2190 	/* D         B    */
2191 	{ SST(0x5D, 0x33, SS_RDEF,	/* XXX TBD */
2192 	    "Data channel impending failure seek error rate too high") },
2193 	/* D         B    */
2194 	{ SST(0x5D, 0x34, SS_RDEF,	/* XXX TBD */
2195 	    "Data channel impending failure too many block reassigns") },
2196 	/* D         B    */
2197 	{ SST(0x5D, 0x35, SS_RDEF,	/* XXX TBD */
2198 	    "Data channel impending failure access times too high") },
2199 	/* D         B    */
2200 	{ SST(0x5D, 0x36, SS_RDEF,	/* XXX TBD */
2201 	    "Data channel impending failure start unit times too high") },
2202 	/* D         B    */
2203 	{ SST(0x5D, 0x37, SS_RDEF,	/* XXX TBD */
2204 	    "Data channel impending failure channel parametrics") },
2205 	/* D         B    */
2206 	{ SST(0x5D, 0x38, SS_RDEF,	/* XXX TBD */
2207 	    "Data channel impending failure controller detected") },
2208 	/* D         B    */
2209 	{ SST(0x5D, 0x39, SS_RDEF,	/* XXX TBD */
2210 	    "Data channel impending failure throughput performance") },
2211 	/* D         B    */
2212 	{ SST(0x5D, 0x3A, SS_RDEF,	/* XXX TBD */
2213 	    "Data channel impending failure seek time performance") },
2214 	/* D         B    */
2215 	{ SST(0x5D, 0x3B, SS_RDEF,	/* XXX TBD */
2216 	    "Data channel impending failure spin-up retry count") },
2217 	/* D         B    */
2218 	{ SST(0x5D, 0x3C, SS_RDEF,	/* XXX TBD */
2219 	    "Data channel impending failure drive calibration retry count") },
2220 	/* D         B    */
2221 	{ SST(0x5D, 0x40, SS_RDEF,	/* XXX TBD */
2222 	    "Servo impending failure general hard drive failure") },
2223 	/* D         B    */
2224 	{ SST(0x5D, 0x41, SS_RDEF,	/* XXX TBD */
2225 	    "Servo impending failure drive error rate too high") },
2226 	/* D         B    */
2227 	{ SST(0x5D, 0x42, SS_RDEF,	/* XXX TBD */
2228 	    "Servo impending failure data error rate too high") },
2229 	/* D         B    */
2230 	{ SST(0x5D, 0x43, SS_RDEF,	/* XXX TBD */
2231 	    "Servo impending failure seek error rate too high") },
2232 	/* D         B    */
2233 	{ SST(0x5D, 0x44, SS_RDEF,	/* XXX TBD */
2234 	    "Servo impending failure too many block reassigns") },
2235 	/* D         B    */
2236 	{ SST(0x5D, 0x45, SS_RDEF,	/* XXX TBD */
2237 	    "Servo impending failure access times too high") },
2238 	/* D         B    */
2239 	{ SST(0x5D, 0x46, SS_RDEF,	/* XXX TBD */
2240 	    "Servo impending failure start unit times too high") },
2241 	/* D         B    */
2242 	{ SST(0x5D, 0x47, SS_RDEF,	/* XXX TBD */
2243 	    "Servo impending failure channel parametrics") },
2244 	/* D         B    */
2245 	{ SST(0x5D, 0x48, SS_RDEF,	/* XXX TBD */
2246 	    "Servo impending failure controller detected") },
2247 	/* D         B    */
2248 	{ SST(0x5D, 0x49, SS_RDEF,	/* XXX TBD */
2249 	    "Servo impending failure throughput performance") },
2250 	/* D         B    */
2251 	{ SST(0x5D, 0x4A, SS_RDEF,	/* XXX TBD */
2252 	    "Servo impending failure seek time performance") },
2253 	/* D         B    */
2254 	{ SST(0x5D, 0x4B, SS_RDEF,	/* XXX TBD */
2255 	    "Servo impending failure spin-up retry count") },
2256 	/* D         B    */
2257 	{ SST(0x5D, 0x4C, SS_RDEF,	/* XXX TBD */
2258 	    "Servo impending failure drive calibration retry count") },
2259 	/* D         B    */
2260 	{ SST(0x5D, 0x50, SS_RDEF,	/* XXX TBD */
2261 	    "Spindle impending failure general hard drive failure") },
2262 	/* D         B    */
2263 	{ SST(0x5D, 0x51, SS_RDEF,	/* XXX TBD */
2264 	    "Spindle impending failure drive error rate too high") },
2265 	/* D         B    */
2266 	{ SST(0x5D, 0x52, SS_RDEF,	/* XXX TBD */
2267 	    "Spindle impending failure data error rate too high") },
2268 	/* D         B    */
2269 	{ SST(0x5D, 0x53, SS_RDEF,	/* XXX TBD */
2270 	    "Spindle impending failure seek error rate too high") },
2271 	/* D         B    */
2272 	{ SST(0x5D, 0x54, SS_RDEF,	/* XXX TBD */
2273 	    "Spindle impending failure too many block reassigns") },
2274 	/* D         B    */
2275 	{ SST(0x5D, 0x55, SS_RDEF,	/* XXX TBD */
2276 	    "Spindle impending failure access times too high") },
2277 	/* D         B    */
2278 	{ SST(0x5D, 0x56, SS_RDEF,	/* XXX TBD */
2279 	    "Spindle impending failure start unit times too high") },
2280 	/* D         B    */
2281 	{ SST(0x5D, 0x57, SS_RDEF,	/* XXX TBD */
2282 	    "Spindle impending failure channel parametrics") },
2283 	/* D         B    */
2284 	{ SST(0x5D, 0x58, SS_RDEF,	/* XXX TBD */
2285 	    "Spindle impending failure controller detected") },
2286 	/* D         B    */
2287 	{ SST(0x5D, 0x59, SS_RDEF,	/* XXX TBD */
2288 	    "Spindle impending failure throughput performance") },
2289 	/* D         B    */
2290 	{ SST(0x5D, 0x5A, SS_RDEF,	/* XXX TBD */
2291 	    "Spindle impending failure seek time performance") },
2292 	/* D         B    */
2293 	{ SST(0x5D, 0x5B, SS_RDEF,	/* XXX TBD */
2294 	    "Spindle impending failure spin-up retry count") },
2295 	/* D         B    */
2296 	{ SST(0x5D, 0x5C, SS_RDEF,	/* XXX TBD */
2297 	    "Spindle impending failure drive calibration retry count") },
2298 	/* D         B    */
2299 	{ SST(0x5D, 0x60, SS_RDEF,	/* XXX TBD */
2300 	    "Firmware impending failure general hard drive failure") },
2301 	/* D         B    */
2302 	{ SST(0x5D, 0x61, SS_RDEF,	/* XXX TBD */
2303 	    "Firmware impending failure drive error rate too high") },
2304 	/* D         B    */
2305 	{ SST(0x5D, 0x62, SS_RDEF,	/* XXX TBD */
2306 	    "Firmware impending failure data error rate too high") },
2307 	/* D         B    */
2308 	{ SST(0x5D, 0x63, SS_RDEF,	/* XXX TBD */
2309 	    "Firmware impending failure seek error rate too high") },
2310 	/* D         B    */
2311 	{ SST(0x5D, 0x64, SS_RDEF,	/* XXX TBD */
2312 	    "Firmware impending failure too many block reassigns") },
2313 	/* D         B    */
2314 	{ SST(0x5D, 0x65, SS_RDEF,	/* XXX TBD */
2315 	    "Firmware impending failure access times too high") },
2316 	/* D         B    */
2317 	{ SST(0x5D, 0x66, SS_RDEF,	/* XXX TBD */
2318 	    "Firmware impending failure start unit times too high") },
2319 	/* D         B    */
2320 	{ SST(0x5D, 0x67, SS_RDEF,	/* XXX TBD */
2321 	    "Firmware impending failure channel parametrics") },
2322 	/* D         B    */
2323 	{ SST(0x5D, 0x68, SS_RDEF,	/* XXX TBD */
2324 	    "Firmware impending failure controller detected") },
2325 	/* D         B    */
2326 	{ SST(0x5D, 0x69, SS_RDEF,	/* XXX TBD */
2327 	    "Firmware impending failure throughput performance") },
2328 	/* D         B    */
2329 	{ SST(0x5D, 0x6A, SS_RDEF,	/* XXX TBD */
2330 	    "Firmware impending failure seek time performance") },
2331 	/* D         B    */
2332 	{ SST(0x5D, 0x6B, SS_RDEF,	/* XXX TBD */
2333 	    "Firmware impending failure spin-up retry count") },
2334 	/* D         B    */
2335 	{ SST(0x5D, 0x6C, SS_RDEF,	/* XXX TBD */
2336 	    "Firmware impending failure drive calibration retry count") },
2337 	/* DTLPWROMAEBKVF */
2338 	{ SST(0x5D, 0xFF, SS_RDEF,
2339 	    "Failure prediction threshold exceeded (false)") },
2340 	/* DTLPWRO A  K   */
2341 	{ SST(0x5E, 0x00, SS_RDEF,
2342 	    "Low power condition on") },
2343 	/* DTLPWRO A  K   */
2344 	{ SST(0x5E, 0x01, SS_RDEF,
2345 	    "Idle condition activated by timer") },
2346 	/* DTLPWRO A  K   */
2347 	{ SST(0x5E, 0x02, SS_RDEF,
2348 	    "Standby condition activated by timer") },
2349 	/* DTLPWRO A  K   */
2350 	{ SST(0x5E, 0x03, SS_RDEF,
2351 	    "Idle condition activated by command") },
2352 	/* DTLPWRO A  K   */
2353 	{ SST(0x5E, 0x04, SS_RDEF,
2354 	    "Standby condition activated by command") },
2355 	/*           B    */
2356 	{ SST(0x5E, 0x41, SS_RDEF,	/* XXX TBD */
2357 	    "Power state change to active") },
2358 	/*           B    */
2359 	{ SST(0x5E, 0x42, SS_RDEF,	/* XXX TBD */
2360 	    "Power state change to idle") },
2361 	/*           B    */
2362 	{ SST(0x5E, 0x43, SS_RDEF,	/* XXX TBD */
2363 	    "Power state change to standby") },
2364 	/*           B    */
2365 	{ SST(0x5E, 0x45, SS_RDEF,	/* XXX TBD */
2366 	    "Power state change to sleep") },
2367 	/*           BK   */
2368 	{ SST(0x5E, 0x47, SS_RDEF,	/* XXX TBD */
2369 	    "Power state change to device control") },
2370 	/*                */
2371 	{ SST(0x60, 0x00, SS_RDEF,
2372 	    "Lamp failure") },
2373 	/*                */
2374 	{ SST(0x61, 0x00, SS_RDEF,
2375 	    "Video acquisition error") },
2376 	/*                */
2377 	{ SST(0x61, 0x01, SS_RDEF,
2378 	    "Unable to acquire video") },
2379 	/*                */
2380 	{ SST(0x61, 0x02, SS_RDEF,
2381 	    "Out of focus") },
2382 	/*                */
2383 	{ SST(0x62, 0x00, SS_RDEF,
2384 	    "Scan head positioning error") },
2385 	/*      R         */
2386 	{ SST(0x63, 0x00, SS_RDEF,
2387 	    "End of user area encountered on this track") },
2388 	/*      R         */
2389 	{ SST(0x63, 0x01, SS_FATAL | ENOSPC,
2390 	    "Packet does not fit in available space") },
2391 	/*      R         */
2392 	{ SST(0x64, 0x00, SS_FATAL | ENXIO,
2393 	    "Illegal mode for this track") },
2394 	/*      R         */
2395 	{ SST(0x64, 0x01, SS_RDEF,
2396 	    "Invalid packet size") },
2397 	/* DTLPWROMAEBKVF */
2398 	{ SST(0x65, 0x00, SS_RDEF,
2399 	    "Voltage fault") },
2400 	/*                */
2401 	{ SST(0x66, 0x00, SS_RDEF,
2402 	    "Automatic document feeder cover up") },
2403 	/*                */
2404 	{ SST(0x66, 0x01, SS_RDEF,
2405 	    "Automatic document feeder lift up") },
2406 	/*                */
2407 	{ SST(0x66, 0x02, SS_RDEF,
2408 	    "Document jam in automatic document feeder") },
2409 	/*                */
2410 	{ SST(0x66, 0x03, SS_RDEF,
2411 	    "Document miss feed automatic in document feeder") },
2412 	/*         A      */
2413 	{ SST(0x67, 0x00, SS_RDEF,
2414 	    "Configuration failure") },
2415 	/*         A      */
2416 	{ SST(0x67, 0x01, SS_RDEF,
2417 	    "Configuration of incapable logical units failed") },
2418 	/*         A      */
2419 	{ SST(0x67, 0x02, SS_RDEF,
2420 	    "Add logical unit failed") },
2421 	/*         A      */
2422 	{ SST(0x67, 0x03, SS_RDEF,
2423 	    "Modification of logical unit failed") },
2424 	/*         A      */
2425 	{ SST(0x67, 0x04, SS_RDEF,
2426 	    "Exchange of logical unit failed") },
2427 	/*         A      */
2428 	{ SST(0x67, 0x05, SS_RDEF,
2429 	    "Remove of logical unit failed") },
2430 	/*         A      */
2431 	{ SST(0x67, 0x06, SS_RDEF,
2432 	    "Attachment of logical unit failed") },
2433 	/*         A      */
2434 	{ SST(0x67, 0x07, SS_RDEF,
2435 	    "Creation of logical unit failed") },
2436 	/*         A      */
2437 	{ SST(0x67, 0x08, SS_RDEF,	/* XXX TBD */
2438 	    "Assign failure occurred") },
2439 	/*         A      */
2440 	{ SST(0x67, 0x09, SS_RDEF,	/* XXX TBD */
2441 	    "Multiply assigned logical unit") },
2442 	/* DTLPWROMAEBKVF */
2443 	{ SST(0x67, 0x0A, SS_RDEF,	/* XXX TBD */
2444 	    "Set target port groups command failed") },
2445 	/* DT        B    */
2446 	{ SST(0x67, 0x0B, SS_RDEF,	/* XXX TBD */
2447 	    "ATA device feature not enabled") },
2448 	/*         A      */
2449 	{ SST(0x68, 0x00, SS_RDEF,
2450 	    "Logical unit not configured") },
2451 	/*         A      */
2452 	{ SST(0x69, 0x00, SS_RDEF,
2453 	    "Data loss on logical unit") },
2454 	/*         A      */
2455 	{ SST(0x69, 0x01, SS_RDEF,
2456 	    "Multiple logical unit failures") },
2457 	/*         A      */
2458 	{ SST(0x69, 0x02, SS_RDEF,
2459 	    "Parity/data mismatch") },
2460 	/*         A      */
2461 	{ SST(0x6A, 0x00, SS_RDEF,
2462 	    "Informational, refer to log") },
2463 	/*         A      */
2464 	{ SST(0x6B, 0x00, SS_RDEF,
2465 	    "State change has occurred") },
2466 	/*         A      */
2467 	{ SST(0x6B, 0x01, SS_RDEF,
2468 	    "Redundancy level got better") },
2469 	/*         A      */
2470 	{ SST(0x6B, 0x02, SS_RDEF,
2471 	    "Redundancy level got worse") },
2472 	/*         A      */
2473 	{ SST(0x6C, 0x00, SS_RDEF,
2474 	    "Rebuild failure occurred") },
2475 	/*         A      */
2476 	{ SST(0x6D, 0x00, SS_RDEF,
2477 	    "Recalculate failure occurred") },
2478 	/*         A      */
2479 	{ SST(0x6E, 0x00, SS_RDEF,
2480 	    "Command to logical unit failed") },
2481 	/*      R         */
2482 	{ SST(0x6F, 0x00, SS_RDEF,	/* XXX TBD */
2483 	    "Copy protection key exchange failure - authentication failure") },
2484 	/*      R         */
2485 	{ SST(0x6F, 0x01, SS_RDEF,	/* XXX TBD */
2486 	    "Copy protection key exchange failure - key not present") },
2487 	/*      R         */
2488 	{ SST(0x6F, 0x02, SS_RDEF,	/* XXX TBD */
2489 	    "Copy protection key exchange failure - key not established") },
2490 	/*      R         */
2491 	{ SST(0x6F, 0x03, SS_RDEF,	/* XXX TBD */
2492 	    "Read of scrambled sector without authentication") },
2493 	/*      R         */
2494 	{ SST(0x6F, 0x04, SS_RDEF,	/* XXX TBD */
2495 	    "Media region code is mismatched to logical unit region") },
2496 	/*      R         */
2497 	{ SST(0x6F, 0x05, SS_RDEF,	/* XXX TBD */
2498 	    "Drive region must be permanent/region reset count error") },
2499 	/*      R         */
2500 	{ SST(0x6F, 0x06, SS_RDEF,	/* XXX TBD */
2501 	    "Insufficient block count for binding NONCE recording") },
2502 	/*      R         */
2503 	{ SST(0x6F, 0x07, SS_RDEF,	/* XXX TBD */
2504 	    "Conflict in binding NONCE recording") },
2505 	/*  T             */
2506 	{ SST(0x70, 0x00, SS_RDEF,
2507 	    "Decompression exception short: ASCQ = Algorithm ID") },
2508 	/*  T             */
2509 	{ SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
2510 	    NULL) },			/* Range 0x00 -> 0xFF */
2511 	/*  T             */
2512 	{ SST(0x71, 0x00, SS_RDEF,
2513 	    "Decompression exception long: ASCQ = Algorithm ID") },
2514 	/*  T             */
2515 	{ SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
2516 	    NULL) },			/* Range 0x00 -> 0xFF */
2517 	/*      R         */
2518 	{ SST(0x72, 0x00, SS_RDEF,
2519 	    "Session fixation error") },
2520 	/*      R         */
2521 	{ SST(0x72, 0x01, SS_RDEF,
2522 	    "Session fixation error writing lead-in") },
2523 	/*      R         */
2524 	{ SST(0x72, 0x02, SS_RDEF,
2525 	    "Session fixation error writing lead-out") },
2526 	/*      R         */
2527 	{ SST(0x72, 0x03, SS_RDEF,
2528 	    "Session fixation error - incomplete track in session") },
2529 	/*      R         */
2530 	{ SST(0x72, 0x04, SS_RDEF,
2531 	    "Empty or partially written reserved track") },
2532 	/*      R         */
2533 	{ SST(0x72, 0x05, SS_RDEF,	/* XXX TBD */
2534 	    "No more track reservations allowed") },
2535 	/*      R         */
2536 	{ SST(0x72, 0x06, SS_RDEF,	/* XXX TBD */
2537 	    "RMZ extension is not allowed") },
2538 	/*      R         */
2539 	{ SST(0x72, 0x07, SS_RDEF,	/* XXX TBD */
2540 	    "No more test zone extensions are allowed") },
2541 	/*      R         */
2542 	{ SST(0x73, 0x00, SS_RDEF,
2543 	    "CD control error") },
2544 	/*      R         */
2545 	{ SST(0x73, 0x01, SS_RDEF,
2546 	    "Power calibration area almost full") },
2547 	/*      R         */
2548 	{ SST(0x73, 0x02, SS_FATAL | ENOSPC,
2549 	    "Power calibration area is full") },
2550 	/*      R         */
2551 	{ SST(0x73, 0x03, SS_RDEF,
2552 	    "Power calibration area error") },
2553 	/*      R         */
2554 	{ SST(0x73, 0x04, SS_RDEF,
2555 	    "Program memory area update failure") },
2556 	/*      R         */
2557 	{ SST(0x73, 0x05, SS_RDEF,
2558 	    "Program memory area is full") },
2559 	/*      R         */
2560 	{ SST(0x73, 0x06, SS_RDEF,	/* XXX TBD */
2561 	    "RMA/PMA is almost full") },
2562 	/*      R         */
2563 	{ SST(0x73, 0x10, SS_RDEF,	/* XXX TBD */
2564 	    "Current power calibration area almost full") },
2565 	/*      R         */
2566 	{ SST(0x73, 0x11, SS_RDEF,	/* XXX TBD */
2567 	    "Current power calibration area is full") },
2568 	/*      R         */
2569 	{ SST(0x73, 0x17, SS_RDEF,	/* XXX TBD */
2570 	    "RDZ is full") },
2571 	/*  T             */
2572 	{ SST(0x74, 0x00, SS_RDEF,	/* XXX TBD */
2573 	    "Security error") },
2574 	/*  T             */
2575 	{ SST(0x74, 0x01, SS_RDEF,	/* XXX TBD */
2576 	    "Unable to decrypt data") },
2577 	/*  T             */
2578 	{ SST(0x74, 0x02, SS_RDEF,	/* XXX TBD */
2579 	    "Unencrypted data encountered while decrypting") },
2580 	/*  T             */
2581 	{ SST(0x74, 0x03, SS_RDEF,	/* XXX TBD */
2582 	    "Incorrect data encryption key") },
2583 	/*  T             */
2584 	{ SST(0x74, 0x04, SS_RDEF,	/* XXX TBD */
2585 	    "Cryptographic integrity validation failed") },
2586 	/*  T             */
2587 	{ SST(0x74, 0x05, SS_RDEF,	/* XXX TBD */
2588 	    "Error decrypting data") },
2589 	/*  T             */
2590 	{ SST(0x74, 0x06, SS_RDEF,	/* XXX TBD */
2591 	    "Unknown signature verification key") },
2592 	/*  T             */
2593 	{ SST(0x74, 0x07, SS_RDEF,	/* XXX TBD */
2594 	    "Encryption parameters not useable") },
2595 	/* DT   R M E  VF */
2596 	{ SST(0x74, 0x08, SS_RDEF,	/* XXX TBD */
2597 	    "Digital signature validation failure") },
2598 	/*  T             */
2599 	{ SST(0x74, 0x09, SS_RDEF,	/* XXX TBD */
2600 	    "Encryption mode mismatch on read") },
2601 	/*  T             */
2602 	{ SST(0x74, 0x0A, SS_RDEF,	/* XXX TBD */
2603 	    "Encrypted block not raw read enabled") },
2604 	/*  T             */
2605 	{ SST(0x74, 0x0B, SS_RDEF,	/* XXX TBD */
2606 	    "Incorrect encryption parameters") },
2607 	/* DT   R MAEBKV  */
2608 	{ SST(0x74, 0x0C, SS_RDEF,	/* XXX TBD */
2609 	    "Unable to decrypt parameter list") },
2610 	/*  T             */
2611 	{ SST(0x74, 0x0D, SS_RDEF,	/* XXX TBD */
2612 	    "Encryption algorithm disabled") },
2613 	/* DT   R MAEBKV  */
2614 	{ SST(0x74, 0x10, SS_RDEF,	/* XXX TBD */
2615 	    "SA creation parameter value invalid") },
2616 	/* DT   R MAEBKV  */
2617 	{ SST(0x74, 0x11, SS_RDEF,	/* XXX TBD */
2618 	    "SA creation parameter value rejected") },
2619 	/* DT   R MAEBKV  */
2620 	{ SST(0x74, 0x12, SS_RDEF,	/* XXX TBD */
2621 	    "Invalid SA usage") },
2622 	/*  T             */
2623 	{ SST(0x74, 0x21, SS_RDEF,	/* XXX TBD */
2624 	    "Data encryption configuration prevented") },
2625 	/* DT   R MAEBKV  */
2626 	{ SST(0x74, 0x30, SS_RDEF,	/* XXX TBD */
2627 	    "SA creation parameter not supported") },
2628 	/* DT   R MAEBKV  */
2629 	{ SST(0x74, 0x40, SS_RDEF,	/* XXX TBD */
2630 	    "Authentication failed") },
2631 	/*             V  */
2632 	{ SST(0x74, 0x61, SS_RDEF,	/* XXX TBD */
2633 	    "External data encryption key manager access error") },
2634 	/*             V  */
2635 	{ SST(0x74, 0x62, SS_RDEF,	/* XXX TBD */
2636 	    "External data encryption key manager error") },
2637 	/*             V  */
2638 	{ SST(0x74, 0x63, SS_RDEF,	/* XXX TBD */
2639 	    "External data encryption key not found") },
2640 	/*             V  */
2641 	{ SST(0x74, 0x64, SS_RDEF,	/* XXX TBD */
2642 	    "External data encryption request not authorized") },
2643 	/*  T             */
2644 	{ SST(0x74, 0x6E, SS_RDEF,	/* XXX TBD */
2645 	    "External data encryption control timeout") },
2646 	/*  T             */
2647 	{ SST(0x74, 0x6F, SS_RDEF,	/* XXX TBD */
2648 	    "External data encryption control error") },
2649 	/* DT   R M E  V  */
2650 	{ SST(0x74, 0x71, SS_RDEF,	/* XXX TBD */
2651 	    "Logical unit access not authorized") },
2652 	/* D              */
2653 	{ SST(0x74, 0x79, SS_RDEF,	/* XXX TBD */
2654 	    "Security conflict in translated device") }
2655 };
2656 
2657 const int asc_table_size = sizeof(asc_table)/sizeof(asc_table[0]);
2658 
2659 struct asc_key
2660 {
2661 	int asc;
2662 	int ascq;
2663 };
2664 
2665 static int
2666 ascentrycomp(const void *key, const void *member)
2667 {
2668 	int asc;
2669 	int ascq;
2670 	const struct asc_table_entry *table_entry;
2671 
2672 	asc = ((const struct asc_key *)key)->asc;
2673 	ascq = ((const struct asc_key *)key)->ascq;
2674 	table_entry = (const struct asc_table_entry *)member;
2675 
2676 	if (asc >= table_entry->asc) {
2677 
2678 		if (asc > table_entry->asc)
2679 			return (1);
2680 
2681 		if (ascq <= table_entry->ascq) {
2682 			/* Check for ranges */
2683 			if (ascq == table_entry->ascq
2684 		 	 || ((table_entry->action & SSQ_RANGE) != 0
2685 		  	   && ascq >= (table_entry - 1)->ascq))
2686 				return (0);
2687 			return (-1);
2688 		}
2689 		return (1);
2690 	}
2691 	return (-1);
2692 }
2693 
2694 static int
2695 senseentrycomp(const void *key, const void *member)
2696 {
2697 	int sense_key;
2698 	const struct sense_key_table_entry *table_entry;
2699 
2700 	sense_key = *((const int *)key);
2701 	table_entry = (const struct sense_key_table_entry *)member;
2702 
2703 	if (sense_key >= table_entry->sense_key) {
2704 		if (sense_key == table_entry->sense_key)
2705 			return (0);
2706 		return (1);
2707 	}
2708 	return (-1);
2709 }
2710 
2711 static void
2712 fetchtableentries(int sense_key, int asc, int ascq,
2713 		  struct scsi_inquiry_data *inq_data,
2714 		  const struct sense_key_table_entry **sense_entry,
2715 		  const struct asc_table_entry **asc_entry)
2716 {
2717 	caddr_t match;
2718 	const struct asc_table_entry *asc_tables[2];
2719 	const struct sense_key_table_entry *sense_tables[2];
2720 	struct asc_key asc_ascq;
2721 	size_t asc_tables_size[2];
2722 	size_t sense_tables_size[2];
2723 	int num_asc_tables;
2724 	int num_sense_tables;
2725 	int i;
2726 
2727 	/* Default to failure */
2728 	*sense_entry = NULL;
2729 	*asc_entry = NULL;
2730 	match = NULL;
2731 	if (inq_data != NULL)
2732 		match = cam_quirkmatch((caddr_t)inq_data,
2733 				       (caddr_t)sense_quirk_table,
2734 				       sense_quirk_table_size,
2735 				       sizeof(*sense_quirk_table),
2736 				       scsi_inquiry_match);
2737 
2738 	if (match != NULL) {
2739 		struct scsi_sense_quirk_entry *quirk;
2740 
2741 		quirk = (struct scsi_sense_quirk_entry *)match;
2742 		asc_tables[0] = quirk->asc_info;
2743 		asc_tables_size[0] = quirk->num_ascs;
2744 		asc_tables[1] = asc_table;
2745 		asc_tables_size[1] = asc_table_size;
2746 		num_asc_tables = 2;
2747 		sense_tables[0] = quirk->sense_key_info;
2748 		sense_tables_size[0] = quirk->num_sense_keys;
2749 		sense_tables[1] = sense_key_table;
2750 		sense_tables_size[1] = sense_key_table_size;
2751 		num_sense_tables = 2;
2752 	} else {
2753 		asc_tables[0] = asc_table;
2754 		asc_tables_size[0] = asc_table_size;
2755 		num_asc_tables = 1;
2756 		sense_tables[0] = sense_key_table;
2757 		sense_tables_size[0] = sense_key_table_size;
2758 		num_sense_tables = 1;
2759 	}
2760 
2761 	asc_ascq.asc = asc;
2762 	asc_ascq.ascq = ascq;
2763 	for (i = 0; i < num_asc_tables; i++) {
2764 		void *found_entry;
2765 
2766 		found_entry = bsearch(&asc_ascq, asc_tables[i],
2767 				      asc_tables_size[i],
2768 				      sizeof(**asc_tables),
2769 				      ascentrycomp);
2770 
2771 		if (found_entry) {
2772 			*asc_entry = (struct asc_table_entry *)found_entry;
2773 			break;
2774 		}
2775 	}
2776 
2777 	for (i = 0; i < num_sense_tables; i++) {
2778 		void *found_entry;
2779 
2780 		found_entry = bsearch(&sense_key, sense_tables[i],
2781 				      sense_tables_size[i],
2782 				      sizeof(**sense_tables),
2783 				      senseentrycomp);
2784 
2785 		if (found_entry) {
2786 			*sense_entry =
2787 			    (struct sense_key_table_entry *)found_entry;
2788 			break;
2789 		}
2790 	}
2791 }
2792 
2793 void
2794 scsi_sense_desc(int sense_key, int asc, int ascq,
2795 		struct scsi_inquiry_data *inq_data,
2796 		const char **sense_key_desc, const char **asc_desc)
2797 {
2798 	const struct asc_table_entry *asc_entry;
2799 	const struct sense_key_table_entry *sense_entry;
2800 
2801 	fetchtableentries(sense_key, asc, ascq,
2802 			  inq_data,
2803 			  &sense_entry,
2804 			  &asc_entry);
2805 
2806 	if (sense_entry != NULL)
2807 		*sense_key_desc = sense_entry->desc;
2808 	else
2809 		*sense_key_desc = "Invalid Sense Key";
2810 
2811 	if (asc_entry != NULL)
2812 		*asc_desc = asc_entry->desc;
2813 	else if (asc >= 0x80 && asc <= 0xff)
2814 		*asc_desc = "Vendor Specific ASC";
2815 	else if (ascq >= 0x80 && ascq <= 0xff)
2816 		*asc_desc = "Vendor Specific ASCQ";
2817 	else
2818 		*asc_desc = "Reserved ASC/ASCQ pair";
2819 }
2820 
2821 /*
2822  * Given sense and device type information, return the appropriate action.
2823  * If we do not understand the specific error as identified by the ASC/ASCQ
2824  * pair, fall back on the more generic actions derived from the sense key.
2825  */
2826 scsi_sense_action
2827 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
2828 		  u_int32_t sense_flags)
2829 {
2830 	const struct asc_table_entry *asc_entry;
2831 	const struct sense_key_table_entry *sense_entry;
2832 	int error_code, sense_key, asc, ascq;
2833 	scsi_sense_action action;
2834 
2835 	scsi_extract_sense_len(&csio->sense_data, csio->sense_len -
2836 			       csio->sense_resid, &error_code,
2837 			       &sense_key, &asc, &ascq, /*show_errors*/ 1);
2838 
2839 	if ((error_code == SSD_DEFERRED_ERROR)
2840 	 || (error_code == SSD_DESC_DEFERRED_ERROR)) {
2841 		/*
2842 		 * XXX dufault@FreeBSD.org
2843 		 * This error doesn't relate to the command associated
2844 		 * with this request sense.  A deferred error is an error
2845 		 * for a command that has already returned GOOD status
2846 		 * (see SCSI2 8.2.14.2).
2847 		 *
2848 		 * By my reading of that section, it looks like the current
2849 		 * command has been cancelled, we should now clean things up
2850 		 * (hopefully recovering any lost data) and then retry the
2851 		 * current command.  There are two easy choices, both wrong:
2852 		 *
2853 		 * 1. Drop through (like we had been doing), thus treating
2854 		 *    this as if the error were for the current command and
2855 		 *    return and stop the current command.
2856 		 *
2857 		 * 2. Issue a retry (like I made it do) thus hopefully
2858 		 *    recovering the current transfer, and ignoring the
2859 		 *    fact that we've dropped a command.
2860 		 *
2861 		 * These should probably be handled in a device specific
2862 		 * sense handler or punted back up to a user mode daemon
2863 		 */
2864 		action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
2865 	} else {
2866 		fetchtableentries(sense_key, asc, ascq,
2867 				  inq_data,
2868 				  &sense_entry,
2869 				  &asc_entry);
2870 
2871 		/*
2872 		 * Override the 'No additional Sense' entry (0,0)
2873 		 * with the error action of the sense key.
2874 		 */
2875 		if (asc_entry != NULL
2876 		 && (asc != 0 || ascq != 0))
2877 			action = asc_entry->action;
2878 		else if (sense_entry != NULL)
2879 			action = sense_entry->action;
2880 		else
2881 			action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
2882 
2883 		if (sense_key == SSD_KEY_RECOVERED_ERROR) {
2884 			/*
2885 			 * The action succeeded but the device wants
2886 			 * the user to know that some recovery action
2887 			 * was required.
2888 			 */
2889 			action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
2890 			action |= SS_NOP|SSQ_PRINT_SENSE;
2891 		} else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
2892 			if ((sense_flags & SF_QUIET_IR) != 0)
2893 				action &= ~SSQ_PRINT_SENSE;
2894 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
2895 			if ((sense_flags & SF_RETRY_UA) != 0
2896 			 && (action & SS_MASK) == SS_FAIL) {
2897 				action &= ~(SS_MASK|SSQ_MASK);
2898 				action |= SS_RETRY|SSQ_DECREMENT_COUNT|
2899 					  SSQ_PRINT_SENSE;
2900 			}
2901 		}
2902 	}
2903 #ifdef _KERNEL
2904 	if (bootverbose)
2905 		sense_flags |= SF_PRINT_ALWAYS;
2906 #endif
2907 	if ((sense_flags & SF_PRINT_ALWAYS) != 0)
2908 		action |= SSQ_PRINT_SENSE;
2909 	else if ((sense_flags & SF_NO_PRINT) != 0)
2910 		action &= ~SSQ_PRINT_SENSE;
2911 
2912 	return (action);
2913 }
2914 
2915 char *
2916 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
2917 {
2918 	u_int8_t cdb_len;
2919 	int i;
2920 
2921 	if (cdb_ptr == NULL)
2922 		return("");
2923 
2924 	/* Silence warnings */
2925 	cdb_len = 0;
2926 
2927 	/*
2928 	 * This is taken from the SCSI-3 draft spec.
2929 	 * (T10/1157D revision 0.3)
2930 	 * The top 3 bits of an opcode are the group code.  The next 5 bits
2931 	 * are the command code.
2932 	 * Group 0:  six byte commands
2933 	 * Group 1:  ten byte commands
2934 	 * Group 2:  ten byte commands
2935 	 * Group 3:  reserved
2936 	 * Group 4:  sixteen byte commands
2937 	 * Group 5:  twelve byte commands
2938 	 * Group 6:  vendor specific
2939 	 * Group 7:  vendor specific
2940 	 */
2941 	switch((*cdb_ptr >> 5) & 0x7) {
2942 		case 0:
2943 			cdb_len = 6;
2944 			break;
2945 		case 1:
2946 		case 2:
2947 			cdb_len = 10;
2948 			break;
2949 		case 3:
2950 		case 6:
2951 		case 7:
2952 			/* in this case, just print out the opcode */
2953 			cdb_len = 1;
2954 			break;
2955 		case 4:
2956 			cdb_len = 16;
2957 			break;
2958 		case 5:
2959 			cdb_len = 12;
2960 			break;
2961 	}
2962 	*cdb_string = '\0';
2963 	for (i = 0; i < cdb_len; i++)
2964 		snprintf(cdb_string + strlen(cdb_string),
2965 			 len - strlen(cdb_string), "%x ", cdb_ptr[i]);
2966 
2967 	return(cdb_string);
2968 }
2969 
2970 const char *
2971 scsi_status_string(struct ccb_scsiio *csio)
2972 {
2973 	switch(csio->scsi_status) {
2974 	case SCSI_STATUS_OK:
2975 		return("OK");
2976 	case SCSI_STATUS_CHECK_COND:
2977 		return("Check Condition");
2978 	case SCSI_STATUS_BUSY:
2979 		return("Busy");
2980 	case SCSI_STATUS_INTERMED:
2981 		return("Intermediate");
2982 	case SCSI_STATUS_INTERMED_COND_MET:
2983 		return("Intermediate-Condition Met");
2984 	case SCSI_STATUS_RESERV_CONFLICT:
2985 		return("Reservation Conflict");
2986 	case SCSI_STATUS_CMD_TERMINATED:
2987 		return("Command Terminated");
2988 	case SCSI_STATUS_QUEUE_FULL:
2989 		return("Queue Full");
2990 	case SCSI_STATUS_ACA_ACTIVE:
2991 		return("ACA Active");
2992 	case SCSI_STATUS_TASK_ABORTED:
2993 		return("Task Aborted");
2994 	default: {
2995 		static char unkstr[64];
2996 		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
2997 			 csio->scsi_status);
2998 		return(unkstr);
2999 	}
3000 	}
3001 }
3002 
3003 /*
3004  * scsi_command_string() returns 0 for success and -1 for failure.
3005  */
3006 #ifdef _KERNEL
3007 int
3008 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
3009 #else /* !_KERNEL */
3010 int
3011 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio,
3012 		    struct sbuf *sb)
3013 #endif /* _KERNEL/!_KERNEL */
3014 {
3015 	struct scsi_inquiry_data *inq_data;
3016 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3017 #ifdef _KERNEL
3018 	struct	  ccb_getdev *cgd;
3019 #endif /* _KERNEL */
3020 
3021 #ifdef _KERNEL
3022 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3023 		return(-1);
3024 	/*
3025 	 * Get the device information.
3026 	 */
3027 	xpt_setup_ccb(&cgd->ccb_h,
3028 		      csio->ccb_h.path,
3029 		      CAM_PRIORITY_NORMAL);
3030 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3031 	xpt_action((union ccb *)cgd);
3032 
3033 	/*
3034 	 * If the device is unconfigured, just pretend that it is a hard
3035 	 * drive.  scsi_op_desc() needs this.
3036 	 */
3037 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3038 		cgd->inq_data.device = T_DIRECT;
3039 
3040 	inq_data = &cgd->inq_data;
3041 
3042 #else /* !_KERNEL */
3043 
3044 	inq_data = &device->inq_data;
3045 
3046 #endif /* _KERNEL/!_KERNEL */
3047 
3048 	if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3049 		sbuf_printf(sb, "%s. CDB: %s",
3050 			    scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data),
3051 			    scsi_cdb_string(csio->cdb_io.cdb_ptr, cdb_str,
3052 					    sizeof(cdb_str)));
3053 	} else {
3054 		sbuf_printf(sb, "%s. CDB: %s",
3055 			    scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data),
3056 			    scsi_cdb_string(csio->cdb_io.cdb_bytes, cdb_str,
3057 					    sizeof(cdb_str)));
3058 	}
3059 
3060 	return(0);
3061 }
3062 
3063 /*
3064  * Iterate over sense descriptors.  Each descriptor is passed into iter_func().
3065  * If iter_func() returns 0, list traversal continues.  If iter_func()
3066  * returns non-zero, list traversal is stopped.
3067  */
3068 void
3069 scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
3070 		  int (*iter_func)(struct scsi_sense_data_desc *sense,
3071 				   u_int, struct scsi_sense_desc_header *,
3072 				   void *), void *arg)
3073 {
3074 	int cur_pos;
3075 	int desc_len;
3076 
3077 	/*
3078 	 * First make sure the extra length field is present.
3079 	 */
3080 	if (SSD_DESC_IS_PRESENT(sense, sense_len, extra_len) == 0)
3081 		return;
3082 
3083 	/*
3084 	 * The length of data actually returned may be different than the
3085 	 * extra_len recorded in the sturcture.
3086 	 */
3087 	desc_len = sense_len -offsetof(struct scsi_sense_data_desc, sense_desc);
3088 
3089 	/*
3090 	 * Limit this further by the extra length reported, and the maximum
3091 	 * allowed extra length.
3092 	 */
3093 	desc_len = MIN(desc_len, MIN(sense->extra_len, SSD_EXTRA_MAX));
3094 
3095 	/*
3096 	 * Subtract the size of the header from the descriptor length.
3097 	 * This is to ensure that we have at least the header left, so we
3098 	 * don't have to check that inside the loop.  This can wind up
3099 	 * being a negative value.
3100 	 */
3101 	desc_len -= sizeof(struct scsi_sense_desc_header);
3102 
3103 	for (cur_pos = 0; cur_pos < desc_len;) {
3104 		struct scsi_sense_desc_header *header;
3105 
3106 		header = (struct scsi_sense_desc_header *)
3107 			&sense->sense_desc[cur_pos];
3108 
3109 		/*
3110 		 * Check to make sure we have the entire descriptor.  We
3111 		 * don't call iter_func() unless we do.
3112 		 *
3113 		 * Note that although cur_pos is at the beginning of the
3114 		 * descriptor, desc_len already has the header length
3115 		 * subtracted.  So the comparison of the length in the
3116 		 * header (which does not include the header itself) to
3117 		 * desc_len - cur_pos is correct.
3118 		 */
3119 		if (header->length > (desc_len - cur_pos))
3120 			break;
3121 
3122 		if (iter_func(sense, sense_len, header, arg) != 0)
3123 			break;
3124 
3125 		cur_pos += sizeof(*header) + header->length;
3126 	}
3127 }
3128 
3129 struct scsi_find_desc_info {
3130 	uint8_t desc_type;
3131 	struct scsi_sense_desc_header *header;
3132 };
3133 
3134 static int
3135 scsi_find_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
3136 		    struct scsi_sense_desc_header *header, void *arg)
3137 {
3138 	struct scsi_find_desc_info *desc_info;
3139 
3140 	desc_info = (struct scsi_find_desc_info *)arg;
3141 
3142 	if (header->desc_type == desc_info->desc_type) {
3143 		desc_info->header = header;
3144 
3145 		/* We found the descriptor, tell the iterator to stop. */
3146 		return (1);
3147 	} else
3148 		return (0);
3149 }
3150 
3151 /*
3152  * Given a descriptor type, return a pointer to it if it is in the sense
3153  * data and not truncated.  Avoiding truncating sense data will simplify
3154  * things significantly for the caller.
3155  */
3156 uint8_t *
3157 scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
3158 	       uint8_t desc_type)
3159 {
3160 	struct scsi_find_desc_info desc_info;
3161 
3162 	desc_info.desc_type = desc_type;
3163 	desc_info.header = NULL;
3164 
3165 	scsi_desc_iterate(sense, sense_len, scsi_find_desc_func, &desc_info);
3166 
3167 	return ((uint8_t *)desc_info.header);
3168 }
3169 
3170 /*
3171  * Fill in SCSI sense data with the specified parameters.  This routine can
3172  * fill in either fixed or descriptor type sense data.
3173  */
3174 void
3175 scsi_set_sense_data_va(struct scsi_sense_data *sense_data,
3176 		      scsi_sense_data_type sense_format, int current_error,
3177 		      int sense_key, int asc, int ascq, va_list ap)
3178 {
3179 	int descriptor_sense;
3180 	scsi_sense_elem_type elem_type;
3181 
3182 	/*
3183 	 * Determine whether to return fixed or descriptor format sense
3184 	 * data.  If the user specifies SSD_TYPE_NONE for some reason,
3185 	 * they'll just get fixed sense data.
3186 	 */
3187 	if (sense_format == SSD_TYPE_DESC)
3188 		descriptor_sense = 1;
3189 	else
3190 		descriptor_sense = 0;
3191 
3192 	/*
3193 	 * Zero the sense data, so that we don't pass back any garbage data
3194 	 * to the user.
3195 	 */
3196 	memset(sense_data, 0, sizeof(*sense_data));
3197 
3198 	if (descriptor_sense != 0) {
3199 		struct scsi_sense_data_desc *sense;
3200 
3201 		sense = (struct scsi_sense_data_desc *)sense_data;
3202 		/*
3203 		 * The descriptor sense format eliminates the use of the
3204 		 * valid bit.
3205 		 */
3206 		if (current_error != 0)
3207 			sense->error_code = SSD_DESC_CURRENT_ERROR;
3208 		else
3209 			sense->error_code = SSD_DESC_DEFERRED_ERROR;
3210 		sense->sense_key = sense_key;
3211 		sense->add_sense_code = asc;
3212 		sense->add_sense_code_qual = ascq;
3213 		/*
3214 		 * Start off with no extra length, since the above data
3215 		 * fits in the standard descriptor sense information.
3216 		 */
3217 		sense->extra_len = 0;
3218 		while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3219 			scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3220 			int sense_len, len_to_copy;
3221 			uint8_t *data;
3222 
3223 			if (elem_type >= SSD_ELEM_MAX) {
3224 				printf("%s: invalid sense type %d\n", __func__,
3225 				       elem_type);
3226 				break;
3227 			}
3228 
3229 			sense_len = (int)va_arg(ap, int);
3230 			len_to_copy = MIN(sense_len, SSD_EXTRA_MAX -
3231 					  sense->extra_len);
3232 			data = (uint8_t *)va_arg(ap, uint8_t *);
3233 
3234 			/*
3235 			 * We've already consumed the arguments for this one.
3236 			 */
3237 			if (elem_type == SSD_ELEM_SKIP)
3238 				continue;
3239 
3240 			switch (elem_type) {
3241 			case SSD_ELEM_DESC: {
3242 
3243 				/*
3244 				 * This is a straight descriptor.  All we
3245 				 * need to do is copy the data in.
3246 				 */
3247 				bcopy(data, &sense->sense_desc[
3248 				      sense->extra_len], len_to_copy);
3249 				sense->extra_len += len_to_copy;
3250 				break;
3251 			}
3252 			case SSD_ELEM_SKS: {
3253 				struct scsi_sense_sks sks;
3254 
3255 				bzero(&sks, sizeof(sks));
3256 
3257 				/*
3258 				 * This is already-formatted sense key
3259 				 * specific data.  We just need to fill out
3260 				 * the header and copy everything in.
3261 				 */
3262 				bcopy(data, &sks.sense_key_spec,
3263 				      MIN(len_to_copy,
3264 				          sizeof(sks.sense_key_spec)));
3265 
3266 				sks.desc_type = SSD_DESC_SKS;
3267 				sks.length = sizeof(sks) -
3268 				    offsetof(struct scsi_sense_sks, reserved1);
3269 				bcopy(&sks,&sense->sense_desc[sense->extra_len],
3270 				      sizeof(sks));
3271 				sense->extra_len += sizeof(sks);
3272 				break;
3273 			}
3274 			case SSD_ELEM_INFO:
3275 			case SSD_ELEM_COMMAND: {
3276 				struct scsi_sense_command cmd;
3277 				struct scsi_sense_info info;
3278 				uint8_t *data_dest;
3279 				uint8_t *descriptor;
3280 				int descriptor_size, i, copy_len;
3281 
3282 				bzero(&cmd, sizeof(cmd));
3283 				bzero(&info, sizeof(info));
3284 
3285 				/*
3286 				 * Command or information data.  The
3287 				 * operate in pretty much the same way.
3288 				 */
3289 				if (elem_type == SSD_ELEM_COMMAND) {
3290 					len_to_copy = MIN(len_to_copy,
3291 					    sizeof(cmd.command_info));
3292 					descriptor = (uint8_t *)&cmd;
3293 					descriptor_size  = sizeof(cmd);
3294 					data_dest =(uint8_t *)&cmd.command_info;
3295 					cmd.desc_type = SSD_DESC_COMMAND;
3296 					cmd.length = sizeof(cmd) -
3297 					    offsetof(struct scsi_sense_command,
3298 						     reserved);
3299 				} else {
3300 					len_to_copy = MIN(len_to_copy,
3301 					    sizeof(info.info));
3302 					descriptor = (uint8_t *)&info;
3303 					descriptor_size = sizeof(cmd);
3304 					data_dest = (uint8_t *)&info.info;
3305 					info.desc_type = SSD_DESC_INFO;
3306 					info.byte2 = SSD_INFO_VALID;
3307 					info.length = sizeof(info) -
3308 					    offsetof(struct scsi_sense_info,
3309 						     byte2);
3310 				}
3311 
3312 				/*
3313 				 * Copy this in reverse because the spec
3314 				 * (SPC-4) says that when 4 byte quantities
3315 				 * are stored in this 8 byte field, the
3316 				 * first four bytes shall be 0.
3317 				 *
3318 				 * So we fill the bytes in from the end, and
3319 				 * if we have less than 8 bytes to copy,
3320 				 * the initial, most significant bytes will
3321 				 * be 0.
3322 				 */
3323 				for (i = sense_len - 1; i >= 0 &&
3324 				     len_to_copy > 0; i--, len_to_copy--)
3325 					data_dest[len_to_copy - 1] = data[i];
3326 
3327 				/*
3328 				 * This calculation looks much like the
3329 				 * initial len_to_copy calculation, but
3330 				 * we have to do it again here, because
3331 				 * we're looking at a larger amount that
3332 				 * may or may not fit.  It's not only the
3333 				 * data the user passed in, but also the
3334 				 * rest of the descriptor.
3335 				 */
3336 				copy_len = MIN(descriptor_size,
3337 				    SSD_EXTRA_MAX - sense->extra_len);
3338 				bcopy(descriptor, &sense->sense_desc[
3339 				      sense->extra_len], copy_len);
3340 				sense->extra_len += copy_len;
3341 				break;
3342 			}
3343 			case SSD_ELEM_FRU: {
3344 				struct scsi_sense_fru fru;
3345 				int copy_len;
3346 
3347 				bzero(&fru, sizeof(fru));
3348 
3349 				fru.desc_type = SSD_DESC_FRU;
3350 				fru.length = sizeof(fru) -
3351 				    offsetof(struct scsi_sense_fru, reserved);
3352 				fru.fru = *data;
3353 
3354 				copy_len = MIN(sizeof(fru), SSD_EXTRA_MAX -
3355 					       sense->extra_len);
3356 				bcopy(&fru, &sense->sense_desc[
3357 				      sense->extra_len], copy_len);
3358 				sense->extra_len += copy_len;
3359 				break;
3360 			}
3361 			case SSD_ELEM_STREAM: {
3362 				struct scsi_sense_stream stream_sense;
3363 				int copy_len;
3364 
3365 				bzero(&stream_sense, sizeof(stream_sense));
3366 				stream_sense.desc_type = SSD_DESC_STREAM;
3367 				stream_sense.length = sizeof(stream_sense) -
3368 				   offsetof(struct scsi_sense_stream, reserved);
3369 				stream_sense.byte3 = *data;
3370 
3371 				copy_len = MIN(sizeof(stream_sense),
3372 				    SSD_EXTRA_MAX - sense->extra_len);
3373 				bcopy(&stream_sense, &sense->sense_desc[
3374 				      sense->extra_len], copy_len);
3375 				sense->extra_len += copy_len;
3376 				break;
3377 			}
3378 			default:
3379 				/*
3380 				 * We shouldn't get here, but if we do, do
3381 				 * nothing.  We've already consumed the
3382 				 * arguments above.
3383 				 */
3384 				break;
3385 			}
3386 		}
3387 	} else {
3388 		struct scsi_sense_data_fixed *sense;
3389 
3390 		sense = (struct scsi_sense_data_fixed *)sense_data;
3391 
3392 		if (current_error != 0)
3393 			sense->error_code = SSD_CURRENT_ERROR;
3394 		else
3395 			sense->error_code = SSD_DEFERRED_ERROR;
3396 
3397 		sense->flags = sense_key;
3398 		sense->add_sense_code = asc;
3399 		sense->add_sense_code_qual = ascq;
3400 		/*
3401 		 * We've set the ASC and ASCQ, so we have 6 more bytes of
3402 		 * valid data.  If we wind up setting any of the other
3403 		 * fields, we'll bump this to 10 extra bytes.
3404 		 */
3405 		sense->extra_len = 6;
3406 
3407 		while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3408 			scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3409 			int sense_len, len_to_copy;
3410 			uint8_t *data;
3411 
3412 			if (elem_type >= SSD_ELEM_MAX) {
3413 				printf("%s: invalid sense type %d\n", __func__,
3414 				       elem_type);
3415 				break;
3416 			}
3417 			/*
3418 			 * If we get in here, just bump the extra length to
3419 			 * 10 bytes.  That will encompass anything we're
3420 			 * going to set here.
3421 			 */
3422 			sense->extra_len = 10;
3423 			sense_len = (int)va_arg(ap, int);
3424 			len_to_copy = MIN(sense_len, SSD_EXTRA_MAX -
3425 					  sense->extra_len);
3426 			data = (uint8_t *)va_arg(ap, uint8_t *);
3427 
3428 			switch (elem_type) {
3429 			case SSD_ELEM_SKS:
3430 				/*
3431 				 * The user passed in pre-formatted sense
3432 				 * key specific data.
3433 				 */
3434 				bcopy(data, &sense->sense_key_spec[0],
3435 				      MIN(sizeof(sense->sense_key_spec),
3436 				      sense_len));
3437 				break;
3438 			case SSD_ELEM_INFO:
3439 			case SSD_ELEM_COMMAND: {
3440 				uint8_t *data_dest;
3441 				int i;
3442 
3443 				if (elem_type == SSD_ELEM_COMMAND)
3444 					data_dest = &sense->cmd_spec_info[0];
3445 				else {
3446 					data_dest = &sense->info[0];
3447 					/*
3448 					 * We're setting the info field, so
3449 					 * set the valid bit.
3450 					 */
3451 					sense->error_code |= SSD_ERRCODE_VALID;
3452 				}
3453 
3454 				/*
3455 			 	 * Copy this in reverse so that if we have
3456 				 * less than 4 bytes to fill, the least
3457 				 * significant bytes will be at the end.
3458 				 * If we have more than 4 bytes, only the
3459 				 * least significant bytes will be included.
3460 				 */
3461 				for (i = sense_len - 1; i >= 0 &&
3462 				     len_to_copy > 0; i--, len_to_copy--)
3463 					data_dest[len_to_copy - 1] = data[i];
3464 
3465 				break;
3466 			}
3467 			case SSD_ELEM_FRU:
3468 				sense->fru = *data;
3469 				break;
3470 			case SSD_ELEM_STREAM:
3471 				sense->flags |= *data;
3472 				break;
3473 			case SSD_ELEM_DESC:
3474 			default:
3475 
3476 				/*
3477 				 * If the user passes in descriptor sense,
3478 				 * we can't handle that in fixed format.
3479 				 * So just skip it, and any unknown argument
3480 				 * types.
3481 				 */
3482 				break;
3483 			}
3484 		}
3485 	}
3486 }
3487 
3488 void
3489 scsi_set_sense_data(struct scsi_sense_data *sense_data,
3490 		    scsi_sense_data_type sense_format, int current_error,
3491 		    int sense_key, int asc, int ascq, ...)
3492 {
3493 	va_list ap;
3494 
3495 	va_start(ap, ascq);
3496 	scsi_set_sense_data_va(sense_data, sense_format, current_error,
3497 			       sense_key, asc, ascq, ap);
3498 	va_end(ap);
3499 }
3500 
3501 /*
3502  * Get sense information for three similar sense data types.
3503  */
3504 int
3505 scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
3506 		    uint8_t info_type, uint64_t *info, int64_t *signed_info)
3507 {
3508 	scsi_sense_data_type sense_type;
3509 
3510 	if (sense_len == 0)
3511 		goto bailout;
3512 
3513 	sense_type = scsi_sense_type(sense_data);
3514 
3515 	switch (sense_type) {
3516 	case SSD_TYPE_DESC: {
3517 		struct scsi_sense_data_desc *sense;
3518 		uint8_t *desc;
3519 
3520 		sense = (struct scsi_sense_data_desc *)sense_data;
3521 
3522 		desc = scsi_find_desc(sense, sense_len, info_type);
3523 		if (desc == NULL)
3524 			goto bailout;
3525 
3526 		switch (info_type) {
3527 		case SSD_DESC_INFO: {
3528 			struct scsi_sense_info *info_desc;
3529 
3530 			info_desc = (struct scsi_sense_info *)desc;
3531 			*info = scsi_8btou64(info_desc->info);
3532 			if (signed_info != NULL)
3533 				*signed_info = *info;
3534 			break;
3535 		}
3536 		case SSD_DESC_COMMAND: {
3537 			struct scsi_sense_command *cmd_desc;
3538 
3539 			cmd_desc = (struct scsi_sense_command *)desc;
3540 
3541 			*info = scsi_8btou64(cmd_desc->command_info);
3542 			if (signed_info != NULL)
3543 				*signed_info = *info;
3544 			break;
3545 		}
3546 		case SSD_DESC_FRU: {
3547 			struct scsi_sense_fru *fru_desc;
3548 
3549 			fru_desc = (struct scsi_sense_fru *)desc;
3550 
3551 			*info = fru_desc->fru;
3552 			if (signed_info != NULL)
3553 				*signed_info = (int8_t)fru_desc->fru;
3554 			break;
3555 		}
3556 		default:
3557 			goto bailout;
3558 			break;
3559 		}
3560 		break;
3561 	}
3562 	case SSD_TYPE_FIXED: {
3563 		struct scsi_sense_data_fixed *sense;
3564 
3565 		sense = (struct scsi_sense_data_fixed *)sense_data;
3566 
3567 		switch (info_type) {
3568 		case SSD_DESC_INFO: {
3569 			uint32_t info_val;
3570 
3571 			if ((sense->error_code & SSD_ERRCODE_VALID) == 0)
3572 				goto bailout;
3573 
3574 			if (SSD_FIXED_IS_PRESENT(sense, sense_len, info) == 0)
3575 				goto bailout;
3576 
3577 			info_val = scsi_4btoul(sense->info);
3578 
3579 			*info = info_val;
3580 			if (signed_info != NULL)
3581 				*signed_info = (int32_t)info_val;
3582 			break;
3583 		}
3584 		case SSD_DESC_COMMAND: {
3585 			uint32_t cmd_val;
3586 
3587 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len,
3588 			     cmd_spec_info) == 0)
3589 			 || (SSD_FIXED_IS_FILLED(sense, cmd_spec_info) == 0))
3590 				goto bailout;
3591 
3592 			cmd_val = scsi_4btoul(sense->cmd_spec_info);
3593 			if (cmd_val == 0)
3594 				goto bailout;
3595 
3596 			*info = cmd_val;
3597 			if (signed_info != NULL)
3598 				*signed_info = (int32_t)cmd_val;
3599 			break;
3600 		}
3601 		case SSD_DESC_FRU:
3602 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len, fru) == 0)
3603 			 || (SSD_FIXED_IS_FILLED(sense, fru) == 0))
3604 				goto bailout;
3605 
3606 			if (sense->fru == 0)
3607 				goto bailout;
3608 
3609 			*info = sense->fru;
3610 			if (signed_info != NULL)
3611 				*signed_info = (int8_t)sense->fru;
3612 			break;
3613 		default:
3614 			goto bailout;
3615 			break;
3616 		}
3617 		break;
3618 	}
3619 	default:
3620 		goto bailout;
3621 		break;
3622 	}
3623 
3624 	return (0);
3625 bailout:
3626 	return (1);
3627 }
3628 
3629 int
3630 scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len, uint8_t *sks)
3631 {
3632 	scsi_sense_data_type sense_type;
3633 
3634 	if (sense_len == 0)
3635 		goto bailout;
3636 
3637 	sense_type = scsi_sense_type(sense_data);
3638 
3639 	switch (sense_type) {
3640 	case SSD_TYPE_DESC: {
3641 		struct scsi_sense_data_desc *sense;
3642 		struct scsi_sense_sks *desc;
3643 
3644 		sense = (struct scsi_sense_data_desc *)sense_data;
3645 
3646 		desc = (struct scsi_sense_sks *)scsi_find_desc(sense, sense_len,
3647 							       SSD_DESC_SKS);
3648 		if (desc == NULL)
3649 			goto bailout;
3650 
3651 		/*
3652 		 * No need to check the SKS valid bit for descriptor sense.
3653 		 * If the descriptor is present, it is valid.
3654 		 */
3655 		bcopy(desc->sense_key_spec, sks, sizeof(desc->sense_key_spec));
3656 		break;
3657 	}
3658 	case SSD_TYPE_FIXED: {
3659 		struct scsi_sense_data_fixed *sense;
3660 
3661 		sense = (struct scsi_sense_data_fixed *)sense_data;
3662 
3663 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, sense_key_spec)== 0)
3664 		 || (SSD_FIXED_IS_FILLED(sense, sense_key_spec) == 0))
3665 			goto bailout;
3666 
3667 		if ((sense->sense_key_spec[0] & SSD_SCS_VALID) == 0)
3668 			goto bailout;
3669 
3670 		bcopy(sense->sense_key_spec, sks,sizeof(sense->sense_key_spec));
3671 		break;
3672 	}
3673 	default:
3674 		goto bailout;
3675 		break;
3676 	}
3677 	return (0);
3678 bailout:
3679 	return (1);
3680 }
3681 
3682 /*
3683  * Provide a common interface for fixed and descriptor sense to detect
3684  * whether we have block-specific sense information.  It is clear by the
3685  * presence of the block descriptor in descriptor mode, but we have to
3686  * infer from the inquiry data and ILI bit in fixed mode.
3687  */
3688 int
3689 scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
3690 		    struct scsi_inquiry_data *inq_data, uint8_t *block_bits)
3691 {
3692 	scsi_sense_data_type sense_type;
3693 
3694 	if (inq_data != NULL) {
3695 		switch (SID_TYPE(inq_data)) {
3696 		case T_DIRECT:
3697 		case T_RBC:
3698 			break;
3699 		default:
3700 			goto bailout;
3701 			break;
3702 		}
3703 	}
3704 
3705 	sense_type = scsi_sense_type(sense_data);
3706 
3707 	switch (sense_type) {
3708 	case SSD_TYPE_DESC: {
3709 		struct scsi_sense_data_desc *sense;
3710 		struct scsi_sense_block *block;
3711 
3712 		sense = (struct scsi_sense_data_desc *)sense_data;
3713 
3714 		block = (struct scsi_sense_block *)scsi_find_desc(sense,
3715 		    sense_len, SSD_DESC_BLOCK);
3716 		if (block == NULL)
3717 			goto bailout;
3718 
3719 		*block_bits = block->byte3;
3720 		break;
3721 	}
3722 	case SSD_TYPE_FIXED: {
3723 		struct scsi_sense_data_fixed *sense;
3724 
3725 		sense = (struct scsi_sense_data_fixed *)sense_data;
3726 
3727 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
3728 			goto bailout;
3729 
3730 		if ((sense->flags & SSD_ILI) == 0)
3731 			goto bailout;
3732 
3733 		*block_bits = sense->flags & SSD_ILI;
3734 		break;
3735 	}
3736 	default:
3737 		goto bailout;
3738 		break;
3739 	}
3740 	return (0);
3741 bailout:
3742 	return (1);
3743 }
3744 
3745 int
3746 scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
3747 		     struct scsi_inquiry_data *inq_data, uint8_t *stream_bits)
3748 {
3749 	scsi_sense_data_type sense_type;
3750 
3751 	if (inq_data != NULL) {
3752 		switch (SID_TYPE(inq_data)) {
3753 		case T_SEQUENTIAL:
3754 			break;
3755 		default:
3756 			goto bailout;
3757 			break;
3758 		}
3759 	}
3760 
3761 	sense_type = scsi_sense_type(sense_data);
3762 
3763 	switch (sense_type) {
3764 	case SSD_TYPE_DESC: {
3765 		struct scsi_sense_data_desc *sense;
3766 		struct scsi_sense_stream *stream;
3767 
3768 		sense = (struct scsi_sense_data_desc *)sense_data;
3769 
3770 		stream = (struct scsi_sense_stream *)scsi_find_desc(sense,
3771 		    sense_len, SSD_DESC_STREAM);
3772 		if (stream == NULL)
3773 			goto bailout;
3774 
3775 		*stream_bits = stream->byte3;
3776 		break;
3777 	}
3778 	case SSD_TYPE_FIXED: {
3779 		struct scsi_sense_data_fixed *sense;
3780 
3781 		sense = (struct scsi_sense_data_fixed *)sense_data;
3782 
3783 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
3784 			goto bailout;
3785 
3786 		if ((sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK)) == 0)
3787 			goto bailout;
3788 
3789 		*stream_bits = sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK);
3790 		break;
3791 	}
3792 	default:
3793 		goto bailout;
3794 		break;
3795 	}
3796 	return (0);
3797 bailout:
3798 	return (1);
3799 }
3800 
3801 void
3802 scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
3803 	       struct scsi_inquiry_data *inq_data, uint64_t info)
3804 {
3805 	sbuf_printf(sb, "Info: %#jx", info);
3806 }
3807 
3808 void
3809 scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
3810 		  struct scsi_inquiry_data *inq_data, uint64_t csi)
3811 {
3812 	sbuf_printf(sb, "Command Specific Info: %#jx", csi);
3813 }
3814 
3815 
3816 void
3817 scsi_progress_sbuf(struct sbuf *sb, uint16_t progress)
3818 {
3819 	sbuf_printf(sb, "Progress: %d%% (%d/%d) complete",
3820 		    (progress * 100) / SSD_SKS_PROGRESS_DENOM,
3821 		    progress, SSD_SKS_PROGRESS_DENOM);
3822 }
3823 
3824 /*
3825  * Returns 1 for failure (i.e. SKS isn't valid) and 0 for success.
3826  */
3827 int
3828 scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks)
3829 {
3830 	if ((sks[0] & SSD_SKS_VALID) == 0)
3831 		return (1);
3832 
3833 	switch (sense_key) {
3834 	case SSD_KEY_ILLEGAL_REQUEST: {
3835 		struct scsi_sense_sks_field *field;
3836 		int bad_command;
3837 		char tmpstr[40];
3838 
3839 		/*Field Pointer*/
3840 		field = (struct scsi_sense_sks_field *)sks;
3841 
3842 		if (field->byte0 & SSD_SKS_FIELD_CMD)
3843 			bad_command = 1;
3844 		else
3845 			bad_command = 0;
3846 
3847 		tmpstr[0] = '\0';
3848 
3849 		/* Bit pointer is valid */
3850 		if (field->byte0 & SSD_SKS_BPV)
3851 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
3852 				 field->byte0 & SSD_SKS_BIT_VALUE);
3853 
3854 		sbuf_printf(sb, "%s byte %d %sis invalid",
3855 			    bad_command ? "Command" : "Data",
3856 			    scsi_2btoul(field->field), tmpstr);
3857 		break;
3858 	}
3859 	case SSD_KEY_UNIT_ATTENTION: {
3860 		struct scsi_sense_sks_overflow *overflow;
3861 
3862 		overflow = (struct scsi_sense_sks_overflow *)sks;
3863 
3864 		/*UA Condition Queue Overflow*/
3865 		sbuf_printf(sb, "Unit Attention Condition Queue %s",
3866 			    (overflow->byte0 & SSD_SKS_OVERFLOW_SET) ?
3867 			    "Overflowed" : "Did Not Overflow??");
3868 		break;
3869 	}
3870 	case SSD_KEY_RECOVERED_ERROR:
3871 	case SSD_KEY_HARDWARE_ERROR:
3872 	case SSD_KEY_MEDIUM_ERROR: {
3873 		struct scsi_sense_sks_retry *retry;
3874 
3875 		/*Actual Retry Count*/
3876 		retry = (struct scsi_sense_sks_retry *)sks;
3877 
3878 		sbuf_printf(sb, "Actual Retry Count: %d",
3879 			    scsi_2btoul(retry->actual_retry_count));
3880 		break;
3881 	}
3882 	case SSD_KEY_NO_SENSE:
3883 	case SSD_KEY_NOT_READY: {
3884 		struct scsi_sense_sks_progress *progress;
3885 		int progress_val;
3886 
3887 		/*Progress Indication*/
3888 		progress = (struct scsi_sense_sks_progress *)sks;
3889 		progress_val = scsi_2btoul(progress->progress);
3890 
3891 		scsi_progress_sbuf(sb, progress_val);
3892 		break;
3893 	}
3894 	case SSD_KEY_COPY_ABORTED: {
3895 		struct scsi_sense_sks_segment *segment;
3896 		char tmpstr[40];
3897 
3898 		/*Segment Pointer*/
3899 		segment = (struct scsi_sense_sks_segment *)sks;
3900 
3901 		tmpstr[0] = '\0';
3902 
3903 		if (segment->byte0 & SSD_SKS_SEGMENT_BPV)
3904 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
3905 				 segment->byte0 & SSD_SKS_SEGMENT_BITPTR);
3906 
3907 		sbuf_printf(sb, "%s byte %d %sis invalid", (segment->byte0 &
3908 			    SSD_SKS_SEGMENT_SD) ? "Segment" : "Data",
3909 			    scsi_2btoul(segment->field), tmpstr);
3910 		break;
3911 	}
3912 	default:
3913 		sbuf_printf(sb, "Sense Key Specific: %#x,%#x", sks[0],
3914 			    scsi_2btoul(&sks[1]));
3915 		break;
3916 	}
3917 
3918 	return (0);
3919 }
3920 
3921 void
3922 scsi_fru_sbuf(struct sbuf *sb, uint64_t fru)
3923 {
3924 	sbuf_printf(sb, "Field Replaceable Unit: %d", (int)fru);
3925 }
3926 
3927 void
3928 scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits, uint64_t info)
3929 {
3930 	int need_comma;
3931 
3932 	need_comma = 0;
3933 	/*
3934 	 * XXX KDM this needs more descriptive decoding.
3935 	 */
3936 	if (stream_bits & SSD_DESC_STREAM_FM) {
3937 		sbuf_printf(sb, "Filemark");
3938 		need_comma = 1;
3939 	}
3940 
3941 	if (stream_bits & SSD_DESC_STREAM_EOM) {
3942 		sbuf_printf(sb, "%sEOM", (need_comma) ? "," : "");
3943 		need_comma = 1;
3944 	}
3945 
3946 	if (stream_bits & SSD_DESC_STREAM_ILI)
3947 		sbuf_printf(sb, "%sILI", (need_comma) ? "," : "");
3948 
3949 	sbuf_printf(sb, ": Info: %#jx", (uintmax_t) info);
3950 }
3951 
3952 void
3953 scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits, uint64_t info)
3954 {
3955 	if (block_bits & SSD_DESC_BLOCK_ILI)
3956 		sbuf_printf(sb, "ILI: residue %#jx", (uintmax_t) info);
3957 }
3958 
3959 void
3960 scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
3961 		     u_int sense_len, uint8_t *cdb, int cdb_len,
3962 		     struct scsi_inquiry_data *inq_data,
3963 		     struct scsi_sense_desc_header *header)
3964 {
3965 	struct scsi_sense_info *info;
3966 
3967 	info = (struct scsi_sense_info *)header;
3968 
3969 	scsi_info_sbuf(sb, cdb, cdb_len, inq_data, scsi_8btou64(info->info));
3970 }
3971 
3972 void
3973 scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
3974 			u_int sense_len, uint8_t *cdb, int cdb_len,
3975 			struct scsi_inquiry_data *inq_data,
3976 			struct scsi_sense_desc_header *header)
3977 {
3978 	struct scsi_sense_command *command;
3979 
3980 	command = (struct scsi_sense_command *)header;
3981 
3982 	scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
3983 			  scsi_8btou64(command->command_info));
3984 }
3985 
3986 void
3987 scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
3988 		    u_int sense_len, uint8_t *cdb, int cdb_len,
3989 		    struct scsi_inquiry_data *inq_data,
3990 		    struct scsi_sense_desc_header *header)
3991 {
3992 	struct scsi_sense_sks *sks;
3993 	int error_code, sense_key, asc, ascq;
3994 
3995 	sks = (struct scsi_sense_sks *)header;
3996 
3997 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
3998 			       &asc, &ascq, /*show_errors*/ 1);
3999 
4000 	scsi_sks_sbuf(sb, sense_key, sks->sense_key_spec);
4001 }
4002 
4003 void
4004 scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4005 		    u_int sense_len, uint8_t *cdb, int cdb_len,
4006 		    struct scsi_inquiry_data *inq_data,
4007 		    struct scsi_sense_desc_header *header)
4008 {
4009 	struct scsi_sense_fru *fru;
4010 
4011 	fru = (struct scsi_sense_fru *)header;
4012 
4013 	scsi_fru_sbuf(sb, (uint64_t)fru->fru);
4014 }
4015 
4016 void
4017 scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4018 		       u_int sense_len, uint8_t *cdb, int cdb_len,
4019 		       struct scsi_inquiry_data *inq_data,
4020 		       struct scsi_sense_desc_header *header)
4021 {
4022 	struct scsi_sense_stream *stream;
4023 	uint64_t info;
4024 
4025 	stream = (struct scsi_sense_stream *)header;
4026 	info = 0;
4027 
4028 	scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4029 
4030 	scsi_stream_sbuf(sb, stream->byte3, info);
4031 }
4032 
4033 void
4034 scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4035 		      u_int sense_len, uint8_t *cdb, int cdb_len,
4036 		      struct scsi_inquiry_data *inq_data,
4037 		      struct scsi_sense_desc_header *header)
4038 {
4039 	struct scsi_sense_block *block;
4040 	uint64_t info;
4041 
4042 	block = (struct scsi_sense_block *)header;
4043 	info = 0;
4044 
4045 	scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4046 
4047 	scsi_block_sbuf(sb, block->byte3, info);
4048 }
4049 
4050 void
4051 scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4052 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4053 			 struct scsi_inquiry_data *inq_data,
4054 			 struct scsi_sense_desc_header *header)
4055 {
4056 	struct scsi_sense_progress *progress;
4057 	const char *sense_key_desc;
4058 	const char *asc_desc;
4059 	int progress_val;
4060 
4061 	progress = (struct scsi_sense_progress *)header;
4062 
4063 	/*
4064 	 * Get descriptions for the sense key, ASC, and ASCQ in the
4065 	 * progress descriptor.  These could be different than the values
4066 	 * in the overall sense data.
4067 	 */
4068 	scsi_sense_desc(progress->sense_key, progress->add_sense_code,
4069 			progress->add_sense_code_qual, inq_data,
4070 			&sense_key_desc, &asc_desc);
4071 
4072 	progress_val = scsi_2btoul(progress->progress);
4073 
4074 	/*
4075 	 * The progress indicator is for the operation described by the
4076 	 * sense key, ASC, and ASCQ in the descriptor.
4077 	 */
4078 	sbuf_cat(sb, sense_key_desc);
4079 	sbuf_printf(sb, " asc:%x,%x (%s): ", progress->add_sense_code,
4080 		    progress->add_sense_code_qual, asc_desc);
4081 	scsi_progress_sbuf(sb, progress_val);
4082 }
4083 
4084 /*
4085  * Generic sense descriptor printing routine.  This is used when we have
4086  * not yet implemented a specific printing routine for this descriptor.
4087  */
4088 void
4089 scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4090 			u_int sense_len, uint8_t *cdb, int cdb_len,
4091 			struct scsi_inquiry_data *inq_data,
4092 			struct scsi_sense_desc_header *header)
4093 {
4094 	int i;
4095 	uint8_t *buf_ptr;
4096 
4097 	sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4098 
4099 	buf_ptr = (uint8_t *)&header[1];
4100 
4101 	for (i = 0; i < header->length; i++, buf_ptr++)
4102 		sbuf_printf(sb, " %02x", *buf_ptr);
4103 }
4104 
4105 /*
4106  * Keep this list in numeric order.  This speeds the array traversal.
4107  */
4108 struct scsi_sense_desc_printer {
4109 	uint8_t desc_type;
4110 	/*
4111 	 * The function arguments here are the superset of what is needed
4112 	 * to print out various different descriptors.  Command and
4113 	 * information descriptors need inquiry data and command type.
4114 	 * Sense key specific descriptors need the sense key.
4115 	 *
4116 	 * The sense, cdb, and inquiry data arguments may be NULL, but the
4117 	 * information printed may not be fully decoded as a result.
4118 	 */
4119 	void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4120 			   u_int sense_len, uint8_t *cdb, int cdb_len,
4121 			   struct scsi_inquiry_data *inq_data,
4122 			   struct scsi_sense_desc_header *header);
4123 } scsi_sense_printers[] = {
4124 	{SSD_DESC_INFO, scsi_sense_info_sbuf},
4125 	{SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4126 	{SSD_DESC_SKS, scsi_sense_sks_sbuf},
4127 	{SSD_DESC_FRU, scsi_sense_fru_sbuf},
4128 	{SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4129 	{SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4130 	{SSD_DESC_PROGRESS, scsi_sense_progress_sbuf}
4131 };
4132 
4133 void
4134 scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4135 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4136 		     struct scsi_inquiry_data *inq_data,
4137 		     struct scsi_sense_desc_header *header)
4138 {
4139 	int i, found;
4140 
4141 	for (i = 0, found = 0; i < (sizeof(scsi_sense_printers) /
4142 	     sizeof(scsi_sense_printers[0])); i++) {
4143 		struct scsi_sense_desc_printer *printer;
4144 
4145 		printer = &scsi_sense_printers[i];
4146 
4147 		/*
4148 		 * The list is sorted, so quit if we've passed our
4149 		 * descriptor number.
4150 		 */
4151 		if (printer->desc_type > header->desc_type)
4152 			break;
4153 
4154 		if (printer->desc_type != header->desc_type)
4155 			continue;
4156 
4157 		printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4158 				    inq_data, header);
4159 
4160 		return;
4161 	}
4162 
4163 	/*
4164 	 * No specific printing routine, so use the generic routine.
4165 	 */
4166 	scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4167 				inq_data, header);
4168 }
4169 
4170 scsi_sense_data_type
4171 scsi_sense_type(struct scsi_sense_data *sense_data)
4172 {
4173 	switch (sense_data->error_code & SSD_ERRCODE) {
4174 	case SSD_DESC_CURRENT_ERROR:
4175 	case SSD_DESC_DEFERRED_ERROR:
4176 		return (SSD_TYPE_DESC);
4177 		break;
4178 	case SSD_CURRENT_ERROR:
4179 	case SSD_DEFERRED_ERROR:
4180 		return (SSD_TYPE_FIXED);
4181 		break;
4182 	default:
4183 		break;
4184 	}
4185 
4186 	return (SSD_TYPE_NONE);
4187 }
4188 
4189 struct scsi_print_sense_info {
4190 	struct sbuf *sb;
4191 	char *path_str;
4192 	uint8_t *cdb;
4193 	int cdb_len;
4194 	struct scsi_inquiry_data *inq_data;
4195 };
4196 
4197 static int
4198 scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4199 		     struct scsi_sense_desc_header *header, void *arg)
4200 {
4201 	struct scsi_print_sense_info *print_info;
4202 
4203 	print_info = (struct scsi_print_sense_info *)arg;
4204 
4205 	switch (header->desc_type) {
4206 	case SSD_DESC_INFO:
4207 	case SSD_DESC_FRU:
4208 	case SSD_DESC_COMMAND:
4209 	case SSD_DESC_SKS:
4210 	case SSD_DESC_BLOCK:
4211 	case SSD_DESC_STREAM:
4212 		/*
4213 		 * We have already printed these descriptors, if they are
4214 		 * present.
4215 		 */
4216 		break;
4217 	default: {
4218 		sbuf_printf(print_info->sb, "%s", print_info->path_str);
4219 		scsi_sense_desc_sbuf(print_info->sb,
4220 				     (struct scsi_sense_data *)sense, sense_len,
4221 				     print_info->cdb, print_info->cdb_len,
4222 				     print_info->inq_data, header);
4223 		sbuf_printf(print_info->sb, "\n");
4224 		break;
4225 	}
4226 	}
4227 
4228 	/*
4229 	 * Tell the iterator that we want to see more descriptors if they
4230 	 * are present.
4231 	 */
4232 	return (0);
4233 }
4234 
4235 void
4236 scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4237 		     struct sbuf *sb, char *path_str,
4238 		     struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4239 		     int cdb_len)
4240 {
4241 	int error_code, sense_key, asc, ascq;
4242 
4243 	sbuf_cat(sb, path_str);
4244 
4245 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4246 			       &asc, &ascq, /*show_errors*/ 1);
4247 
4248 	sbuf_printf(sb, "SCSI sense: ");
4249 	switch (error_code) {
4250 	case SSD_DEFERRED_ERROR:
4251 	case SSD_DESC_DEFERRED_ERROR:
4252 		sbuf_printf(sb, "Deferred error: ");
4253 
4254 		/* FALLTHROUGH */
4255 	case SSD_CURRENT_ERROR:
4256 	case SSD_DESC_CURRENT_ERROR:
4257 	{
4258 		struct scsi_sense_data_desc *desc_sense;
4259 		struct scsi_print_sense_info print_info;
4260 		const char *sense_key_desc;
4261 		const char *asc_desc;
4262 		uint8_t sks[3];
4263 		uint64_t val;
4264 		int info_valid;
4265 
4266 		/*
4267 		 * Get descriptions for the sense key, ASC, and ASCQ.  If
4268 		 * these aren't present in the sense data (i.e. the sense
4269 		 * data isn't long enough), the -1 values that
4270 		 * scsi_extract_sense_len() returns will yield default
4271 		 * or error descriptions.
4272 		 */
4273 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
4274 				&sense_key_desc, &asc_desc);
4275 
4276 		/*
4277 		 * We first print the sense key and ASC/ASCQ.
4278 		 */
4279 		sbuf_cat(sb, sense_key_desc);
4280 		sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
4281 
4282 		/*
4283 		 * Get the info field if it is valid.
4284 		 */
4285 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
4286 					&val, NULL) == 0)
4287 			info_valid = 1;
4288 		else
4289 			info_valid = 0;
4290 
4291 		if (info_valid != 0) {
4292 			uint8_t bits;
4293 
4294 			/*
4295 			 * Determine whether we have any block or stream
4296 			 * device-specific information.
4297 			 */
4298 			if (scsi_get_block_info(sense, sense_len, inq_data,
4299 						&bits) == 0) {
4300 				sbuf_cat(sb, path_str);
4301 				scsi_block_sbuf(sb, bits, val);
4302 				sbuf_printf(sb, "\n");
4303 			} else if (scsi_get_stream_info(sense, sense_len,
4304 							inq_data, &bits) == 0) {
4305 				sbuf_cat(sb, path_str);
4306 				scsi_stream_sbuf(sb, bits, val);
4307 				sbuf_printf(sb, "\n");
4308 			} else if (val != 0) {
4309 				/*
4310 				 * The information field can be valid but 0.
4311 				 * If the block or stream bits aren't set,
4312 				 * and this is 0, it isn't terribly useful
4313 				 * to print it out.
4314 				 */
4315 				sbuf_cat(sb, path_str);
4316 				scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
4317 				sbuf_printf(sb, "\n");
4318 			}
4319 		}
4320 
4321 		/*
4322 		 * Print the FRU.
4323 		 */
4324 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
4325 					&val, NULL) == 0) {
4326 			sbuf_cat(sb, path_str);
4327 			scsi_fru_sbuf(sb, val);
4328 			sbuf_printf(sb, "\n");
4329 		}
4330 
4331 		/*
4332 		 * Print any command-specific information.
4333 		 */
4334 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
4335 					&val, NULL) == 0) {
4336 			sbuf_cat(sb, path_str);
4337 			scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
4338 			sbuf_printf(sb, "\n");
4339 		}
4340 
4341 		/*
4342 		 * Print out any sense-key-specific information.
4343 		 */
4344 		if (scsi_get_sks(sense, sense_len, sks) == 0) {
4345 			sbuf_cat(sb, path_str);
4346 			scsi_sks_sbuf(sb, sense_key, sks);
4347 			sbuf_printf(sb, "\n");
4348 		}
4349 
4350 		/*
4351 		 * If this is fixed sense, we're done.  If we have
4352 		 * descriptor sense, we might have more information
4353 		 * available.
4354 		 */
4355 		if (scsi_sense_type(sense) != SSD_TYPE_DESC)
4356 			break;
4357 
4358 		desc_sense = (struct scsi_sense_data_desc *)sense;
4359 
4360 		print_info.sb = sb;
4361 		print_info.path_str = path_str;
4362 		print_info.cdb = cdb;
4363 		print_info.cdb_len = cdb_len;
4364 		print_info.inq_data = inq_data;
4365 
4366 		/*
4367 		 * Print any sense descriptors that we have not already printed.
4368 		 */
4369 		scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
4370 				  &print_info);
4371 		break;
4372 
4373 	}
4374 	case -1:
4375 		/*
4376 		 * scsi_extract_sense_len() sets values to -1 if the
4377 		 * show_errors flag is set and they aren't present in the
4378 		 * sense data.  This means that sense_len is 0.
4379 		 */
4380 		sbuf_printf(sb, "No sense data present\n");
4381 		break;
4382 	default: {
4383 		sbuf_printf(sb, "Error code 0x%x", error_code);
4384 		if (sense->error_code & SSD_ERRCODE_VALID) {
4385 			struct scsi_sense_data_fixed *fixed_sense;
4386 
4387 			fixed_sense = (struct scsi_sense_data_fixed *)sense;
4388 
4389 			if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
4390 				uint32_t info;
4391 
4392 				info = scsi_4btoul(fixed_sense->info);
4393 
4394 				sbuf_printf(sb, " at block no. %d (decimal)",
4395 					    info);
4396 			}
4397 		}
4398 		sbuf_printf(sb, "\n");
4399 		break;
4400 	}
4401 	}
4402 }
4403 
4404 /*
4405  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
4406  */
4407 #ifdef _KERNEL
4408 int
4409 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
4410 		scsi_sense_string_flags flags)
4411 #else /* !_KERNEL */
4412 int
4413 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
4414 		struct sbuf *sb, scsi_sense_string_flags flags)
4415 #endif /* _KERNEL/!_KERNEL */
4416 {
4417 	struct	  scsi_sense_data *sense;
4418 	struct	  scsi_inquiry_data *inq_data;
4419 #ifdef _KERNEL
4420 	struct	  ccb_getdev *cgd;
4421 #endif /* _KERNEL */
4422 	char	  path_str[64];
4423 	uint8_t	  *cdb;
4424 
4425 #ifndef _KERNEL
4426 	if (device == NULL)
4427 		return(-1);
4428 #endif /* !_KERNEL */
4429 	if ((csio == NULL) || (sb == NULL))
4430 		return(-1);
4431 
4432 	/*
4433 	 * If the CDB is a physical address, we can't deal with it..
4434 	 */
4435 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
4436 		flags &= ~SSS_FLAG_PRINT_COMMAND;
4437 
4438 #ifdef _KERNEL
4439 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
4440 #else /* !_KERNEL */
4441 	cam_path_string(device, path_str, sizeof(path_str));
4442 #endif /* _KERNEL/!_KERNEL */
4443 
4444 #ifdef _KERNEL
4445 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
4446 		return(-1);
4447 	/*
4448 	 * Get the device information.
4449 	 */
4450 	xpt_setup_ccb(&cgd->ccb_h,
4451 		      csio->ccb_h.path,
4452 		      CAM_PRIORITY_NORMAL);
4453 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
4454 	xpt_action((union ccb *)cgd);
4455 
4456 	/*
4457 	 * If the device is unconfigured, just pretend that it is a hard
4458 	 * drive.  scsi_op_desc() needs this.
4459 	 */
4460 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
4461 		cgd->inq_data.device = T_DIRECT;
4462 
4463 	inq_data = &cgd->inq_data;
4464 
4465 #else /* !_KERNEL */
4466 
4467 	inq_data = &device->inq_data;
4468 
4469 #endif /* _KERNEL/!_KERNEL */
4470 
4471 	sense = NULL;
4472 
4473 	if (flags & SSS_FLAG_PRINT_COMMAND) {
4474 
4475 		sbuf_cat(sb, path_str);
4476 
4477 #ifdef _KERNEL
4478 		scsi_command_string(csio, sb);
4479 #else /* !_KERNEL */
4480 		scsi_command_string(device, csio, sb);
4481 #endif /* _KERNEL/!_KERNEL */
4482 		sbuf_printf(sb, "\n");
4483 	}
4484 
4485 	/*
4486 	 * If the sense data is a physical pointer, forget it.
4487 	 */
4488 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
4489 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
4490 #ifdef _KERNEL
4491 			xpt_free_ccb((union ccb*)cgd);
4492 #endif /* _KERNEL/!_KERNEL */
4493 			return(-1);
4494 		} else {
4495 			/*
4496 			 * bcopy the pointer to avoid unaligned access
4497 			 * errors on finicky architectures.  We don't
4498 			 * ensure that the sense data is pointer aligned.
4499 			 */
4500 			bcopy(&csio->sense_data, &sense,
4501 			      sizeof(struct scsi_sense_data *));
4502 		}
4503 	} else {
4504 		/*
4505 		 * If the physical sense flag is set, but the sense pointer
4506 		 * is not also set, we assume that the user is an idiot and
4507 		 * return.  (Well, okay, it could be that somehow, the
4508 		 * entire csio is physical, but we would have probably core
4509 		 * dumped on one of the bogus pointer deferences above
4510 		 * already.)
4511 		 */
4512 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
4513 #ifdef _KERNEL
4514 			xpt_free_ccb((union ccb*)cgd);
4515 #endif /* _KERNEL/!_KERNEL */
4516 			return(-1);
4517 		} else
4518 			sense = &csio->sense_data;
4519 	}
4520 
4521 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
4522 		cdb = csio->cdb_io.cdb_ptr;
4523 	else
4524 		cdb = csio->cdb_io.cdb_bytes;
4525 
4526 	scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
4527 			     path_str, inq_data, cdb, csio->cdb_len);
4528 
4529 #ifdef _KERNEL
4530 	xpt_free_ccb((union ccb*)cgd);
4531 #endif /* _KERNEL/!_KERNEL */
4532 	return(0);
4533 }
4534 
4535 
4536 
4537 #ifdef _KERNEL
4538 char *
4539 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
4540 #else /* !_KERNEL */
4541 char *
4542 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
4543 		  char *str, int str_len)
4544 #endif /* _KERNEL/!_KERNEL */
4545 {
4546 	struct sbuf sb;
4547 
4548 	sbuf_new(&sb, str, str_len, 0);
4549 
4550 #ifdef _KERNEL
4551 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
4552 #else /* !_KERNEL */
4553 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
4554 #endif /* _KERNEL/!_KERNEL */
4555 
4556 	sbuf_finish(&sb);
4557 
4558 	return(sbuf_data(&sb));
4559 }
4560 
4561 #ifdef _KERNEL
4562 void
4563 scsi_sense_print(struct ccb_scsiio *csio)
4564 {
4565 	struct sbuf sb;
4566 	char str[512];
4567 
4568 	sbuf_new(&sb, str, sizeof(str), 0);
4569 
4570 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
4571 
4572 	sbuf_finish(&sb);
4573 
4574 	printf("%s", sbuf_data(&sb));
4575 }
4576 
4577 #else /* !_KERNEL */
4578 void
4579 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
4580 		 FILE *ofile)
4581 {
4582 	struct sbuf sb;
4583 	char str[512];
4584 
4585 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
4586 		return;
4587 
4588 	sbuf_new(&sb, str, sizeof(str), 0);
4589 
4590 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
4591 
4592 	sbuf_finish(&sb);
4593 
4594 	fprintf(ofile, "%s", sbuf_data(&sb));
4595 }
4596 
4597 #endif /* _KERNEL/!_KERNEL */
4598 
4599 /*
4600  * Extract basic sense information.  This is backward-compatible with the
4601  * previous implementation.  For new implementations,
4602  * scsi_extract_sense_len() is recommended.
4603  */
4604 void
4605 scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
4606 		   int *sense_key, int *asc, int *ascq)
4607 {
4608 	scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
4609 			       sense_key, asc, ascq, /*show_errors*/ 0);
4610 }
4611 
4612 /*
4613  * Extract basic sense information.  If show_errors is set, sense values
4614  * will be set to -1 if they are not present.
4615  */
4616 void
4617 scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
4618 		       int *error_code, int *sense_key, int *asc, int *ascq,
4619 		       int show_errors)
4620 {
4621 	/*
4622 	 * If we have no length, we have no sense.
4623 	 */
4624 	if (sense_len == 0) {
4625 		if (show_errors == 0) {
4626 			*error_code = 0;
4627 			*sense_key = 0;
4628 			*asc = 0;
4629 			*ascq = 0;
4630 		} else {
4631 			*error_code = -1;
4632 			*sense_key = -1;
4633 			*asc = -1;
4634 			*ascq = -1;
4635 		}
4636 		return;
4637 	}
4638 
4639 	*error_code = sense_data->error_code & SSD_ERRCODE;
4640 
4641 	switch (*error_code) {
4642 	case SSD_DESC_CURRENT_ERROR:
4643 	case SSD_DESC_DEFERRED_ERROR: {
4644 		struct scsi_sense_data_desc *sense;
4645 
4646 		sense = (struct scsi_sense_data_desc *)sense_data;
4647 
4648 		if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
4649 			*sense_key = sense->sense_key & SSD_KEY;
4650 		else
4651 			*sense_key = (show_errors) ? -1 : 0;
4652 
4653 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
4654 			*asc = sense->add_sense_code;
4655 		else
4656 			*asc = (show_errors) ? -1 : 0;
4657 
4658 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
4659 			*ascq = sense->add_sense_code_qual;
4660 		else
4661 			*ascq = (show_errors) ? -1 : 0;
4662 		break;
4663 	}
4664 	case SSD_CURRENT_ERROR:
4665 	case SSD_DEFERRED_ERROR:
4666 	default: {
4667 		struct scsi_sense_data_fixed *sense;
4668 
4669 		sense = (struct scsi_sense_data_fixed *)sense_data;
4670 
4671 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
4672 			*sense_key = sense->flags & SSD_KEY;
4673 		else
4674 			*sense_key = (show_errors) ? -1 : 0;
4675 
4676 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
4677 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
4678 			*asc = sense->add_sense_code;
4679 		else
4680 			*asc = (show_errors) ? -1 : 0;
4681 
4682 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
4683 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
4684 			*ascq = sense->add_sense_code_qual;
4685 		else
4686 			*ascq = (show_errors) ? -1 : 0;
4687 		break;
4688 	}
4689 	}
4690 }
4691 
4692 int
4693 scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
4694 		   int show_errors)
4695 {
4696 	int error_code, sense_key, asc, ascq;
4697 
4698 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
4699 			       &sense_key, &asc, &ascq, show_errors);
4700 
4701 	return (sense_key);
4702 }
4703 
4704 int
4705 scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
4706 	     int show_errors)
4707 {
4708 	int error_code, sense_key, asc, ascq;
4709 
4710 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
4711 			       &sense_key, &asc, &ascq, show_errors);
4712 
4713 	return (asc);
4714 }
4715 
4716 int
4717 scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
4718 	      int show_errors)
4719 {
4720 	int error_code, sense_key, asc, ascq;
4721 
4722 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
4723 			       &sense_key, &asc, &ascq, show_errors);
4724 
4725 	return (ascq);
4726 }
4727 
4728 /*
4729  * This function currently requires at least 36 bytes, or
4730  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
4731  * function needs more or less data in the future, another length should be
4732  * defined in scsi_all.h to indicate the minimum amount of data necessary
4733  * for this routine to function properly.
4734  */
4735 void
4736 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
4737 {
4738 	u_int8_t type;
4739 	char *dtype, *qtype;
4740 	char vendor[16], product[48], revision[16], rstr[4];
4741 
4742 	type = SID_TYPE(inq_data);
4743 
4744 	/*
4745 	 * Figure out basic device type and qualifier.
4746 	 */
4747 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
4748 		qtype = "(vendor-unique qualifier)";
4749 	} else {
4750 		switch (SID_QUAL(inq_data)) {
4751 		case SID_QUAL_LU_CONNECTED:
4752 			qtype = "";
4753 			break;
4754 
4755 		case SID_QUAL_LU_OFFLINE:
4756 			qtype = "(offline)";
4757 			break;
4758 
4759 		case SID_QUAL_RSVD:
4760 			qtype = "(reserved qualifier)";
4761 			break;
4762 		default:
4763 		case SID_QUAL_BAD_LU:
4764 			qtype = "(LUN not supported)";
4765 			break;
4766 		}
4767 	}
4768 
4769 	switch (type) {
4770 	case T_DIRECT:
4771 		dtype = "Direct Access";
4772 		break;
4773 	case T_SEQUENTIAL:
4774 		dtype = "Sequential Access";
4775 		break;
4776 	case T_PRINTER:
4777 		dtype = "Printer";
4778 		break;
4779 	case T_PROCESSOR:
4780 		dtype = "Processor";
4781 		break;
4782 	case T_WORM:
4783 		dtype = "WORM";
4784 		break;
4785 	case T_CDROM:
4786 		dtype = "CD-ROM";
4787 		break;
4788 	case T_SCANNER:
4789 		dtype = "Scanner";
4790 		break;
4791 	case T_OPTICAL:
4792 		dtype = "Optical";
4793 		break;
4794 	case T_CHANGER:
4795 		dtype = "Changer";
4796 		break;
4797 	case T_COMM:
4798 		dtype = "Communication";
4799 		break;
4800 	case T_STORARRAY:
4801 		dtype = "Storage Array";
4802 		break;
4803 	case T_ENCLOSURE:
4804 		dtype = "Enclosure Services";
4805 		break;
4806 	case T_RBC:
4807 		dtype = "Simplified Direct Access";
4808 		break;
4809 	case T_OCRW:
4810 		dtype = "Optical Card Read/Write";
4811 		break;
4812 	case T_OSD:
4813 		dtype = "Object-Based Storage";
4814 		break;
4815 	case T_ADC:
4816 		dtype = "Automation/Drive Interface";
4817 		break;
4818 	case T_NODEVICE:
4819 		dtype = "Uninstalled";
4820 		break;
4821 	default:
4822 		dtype = "unknown";
4823 		break;
4824 	}
4825 
4826 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
4827 		   sizeof(vendor));
4828 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
4829 		   sizeof(product));
4830 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
4831 		   sizeof(revision));
4832 
4833 	if (SID_ANSI_REV(inq_data) == SCSI_REV_CCS)
4834 		bcopy("CCS", rstr, 4);
4835 	else
4836 		snprintf(rstr, sizeof (rstr), "%d", SID_ANSI_REV(inq_data));
4837 	printf("<%s %s %s> %s %s SCSI-%s device %s\n",
4838 	       vendor, product, revision,
4839 	       SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
4840 	       dtype, rstr, qtype);
4841 }
4842 
4843 /*
4844  * Table of syncrates that don't follow the "divisible by 4"
4845  * rule. This table will be expanded in future SCSI specs.
4846  */
4847 static struct {
4848 	u_int period_factor;
4849 	u_int period;	/* in 100ths of ns */
4850 } scsi_syncrates[] = {
4851 	{ 0x08, 625 },	/* FAST-160 */
4852 	{ 0x09, 1250 },	/* FAST-80 */
4853 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
4854 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
4855 	{ 0x0c, 5000 }	/* FAST-20 */
4856 };
4857 
4858 /*
4859  * Return the frequency in kHz corresponding to the given
4860  * sync period factor.
4861  */
4862 u_int
4863 scsi_calc_syncsrate(u_int period_factor)
4864 {
4865 	int i;
4866 	int num_syncrates;
4867 
4868 	/*
4869 	 * It's a bug if period is zero, but if it is anyway, don't
4870 	 * die with a divide fault- instead return something which
4871 	 * 'approximates' async
4872 	 */
4873 	if (period_factor == 0) {
4874 		return (3300);
4875 	}
4876 
4877 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
4878 	/* See if the period is in the "exception" table */
4879 	for (i = 0; i < num_syncrates; i++) {
4880 
4881 		if (period_factor == scsi_syncrates[i].period_factor) {
4882 			/* Period in kHz */
4883 			return (100000000 / scsi_syncrates[i].period);
4884 		}
4885 	}
4886 
4887 	/*
4888 	 * Wasn't in the table, so use the standard
4889 	 * 4 times conversion.
4890 	 */
4891 	return (10000000 / (period_factor * 4 * 10));
4892 }
4893 
4894 /*
4895  * Return the SCSI sync parameter that corresponsd to
4896  * the passed in period in 10ths of ns.
4897  */
4898 u_int
4899 scsi_calc_syncparam(u_int period)
4900 {
4901 	int i;
4902 	int num_syncrates;
4903 
4904 	if (period == 0)
4905 		return (~0);	/* Async */
4906 
4907 	/* Adjust for exception table being in 100ths. */
4908 	period *= 10;
4909 	num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
4910 	/* See if the period is in the "exception" table */
4911 	for (i = 0; i < num_syncrates; i++) {
4912 
4913 		if (period <= scsi_syncrates[i].period) {
4914 			/* Period in 100ths of ns */
4915 			return (scsi_syncrates[i].period_factor);
4916 		}
4917 	}
4918 
4919 	/*
4920 	 * Wasn't in the table, so use the standard
4921 	 * 1/4 period in ns conversion.
4922 	 */
4923 	return (period/400);
4924 }
4925 
4926 int
4927 scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
4928 {
4929 	struct scsi_vpd_id_descriptor *descr;
4930 	struct scsi_vpd_id_naa_basic *naa;
4931 
4932 	descr = (struct scsi_vpd_id_descriptor *)bufp;
4933 	naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
4934 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
4935 		return 0;
4936 	if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
4937 		return 0;
4938 	if ((naa->naa >> SVPD_ID_NAA_NAA_SHIFT) != SVPD_ID_NAA_IEEE_REG)
4939 		return 0;
4940 	return 1;
4941 }
4942 
4943 int
4944 scsi_devid_is_sas_target(uint8_t *bufp)
4945 {
4946 	struct scsi_vpd_id_descriptor *descr;
4947 
4948 	descr = (struct scsi_vpd_id_descriptor *)bufp;
4949 	if (!scsi_devid_is_naa_ieee_reg(bufp))
4950 		return 0;
4951 	if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
4952 		return 0;
4953 	if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
4954 		return 0;
4955 	return 1;
4956 }
4957 
4958 uint8_t *
4959 scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
4960     scsi_devid_checkfn_t ck_fn)
4961 {
4962 	struct scsi_vpd_id_descriptor *desc;
4963 	uint8_t *page_end;
4964 	uint8_t *desc_buf_end;
4965 
4966 	page_end = (uint8_t *)id + page_len;
4967 	if (page_end < id->desc_list)
4968 		return (NULL);
4969 
4970 	desc_buf_end = MIN(id->desc_list + scsi_2btoul(id->length), page_end);
4971 
4972 	for (desc = (struct scsi_vpd_id_descriptor *)id->desc_list;
4973 	     desc->identifier <= desc_buf_end
4974 	  && desc->identifier + desc->length <= desc_buf_end;
4975 	     desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
4976 						    + desc->length)) {
4977 
4978 		if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
4979 			return (desc->identifier);
4980 	}
4981 
4982 	return (NULL);
4983 }
4984 
4985 void
4986 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
4987 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
4988 		     u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
4989 {
4990 	struct scsi_test_unit_ready *scsi_cmd;
4991 
4992 	cam_fill_csio(csio,
4993 		      retries,
4994 		      cbfcnp,
4995 		      CAM_DIR_NONE,
4996 		      tag_action,
4997 		      /*data_ptr*/NULL,
4998 		      /*dxfer_len*/0,
4999 		      sense_len,
5000 		      sizeof(*scsi_cmd),
5001 		      timeout);
5002 
5003 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
5004 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5005 	scsi_cmd->opcode = TEST_UNIT_READY;
5006 }
5007 
5008 void
5009 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
5010 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
5011 		   void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
5012 		   u_int8_t sense_len, u_int32_t timeout)
5013 {
5014 	struct scsi_request_sense *scsi_cmd;
5015 
5016 	cam_fill_csio(csio,
5017 		      retries,
5018 		      cbfcnp,
5019 		      CAM_DIR_IN,
5020 		      tag_action,
5021 		      data_ptr,
5022 		      dxfer_len,
5023 		      sense_len,
5024 		      sizeof(*scsi_cmd),
5025 		      timeout);
5026 
5027 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
5028 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5029 	scsi_cmd->opcode = REQUEST_SENSE;
5030 	scsi_cmd->length = dxfer_len;
5031 }
5032 
5033 void
5034 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
5035 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
5036 	     u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
5037 	     int evpd, u_int8_t page_code, u_int8_t sense_len,
5038 	     u_int32_t timeout)
5039 {
5040 	struct scsi_inquiry *scsi_cmd;
5041 
5042 	cam_fill_csio(csio,
5043 		      retries,
5044 		      cbfcnp,
5045 		      /*flags*/CAM_DIR_IN,
5046 		      tag_action,
5047 		      /*data_ptr*/inq_buf,
5048 		      /*dxfer_len*/inq_len,
5049 		      sense_len,
5050 		      sizeof(*scsi_cmd),
5051 		      timeout);
5052 
5053 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
5054 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5055 	scsi_cmd->opcode = INQUIRY;
5056 	if (evpd) {
5057 		scsi_cmd->byte2 |= SI_EVPD;
5058 		scsi_cmd->page_code = page_code;
5059 	}
5060 	/*
5061 	 * A 'transfer units' count of 256 is coded as
5062 	 * zero for all commands with a single byte count
5063 	 * field.
5064 	 */
5065 	if (inq_len == 256)
5066 		inq_len = 0;
5067 	scsi_cmd->length = inq_len;
5068 }
5069 
5070 void
5071 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
5072 		void (*cbfcnp)(struct cam_periph *, union ccb *),
5073 		u_int8_t tag_action, int dbd, u_int8_t page_code,
5074 		u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
5075 		u_int8_t sense_len, u_int32_t timeout)
5076 {
5077 
5078 	scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
5079 			    page_code, page, param_buf, param_len, 0,
5080 			    sense_len, timeout);
5081 }
5082 
5083 void
5084 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
5085 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
5086 		    u_int8_t tag_action, int dbd, u_int8_t page_code,
5087 		    u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
5088 		    int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
5089 {
5090 	u_int8_t cdb_len;
5091 
5092 	/*
5093 	 * Use the smallest possible command to perform the operation.
5094 	 */
5095 	if ((param_len < 256)
5096 	 && (minimum_cmd_size < 10)) {
5097 		/*
5098 		 * We can fit in a 6 byte cdb.
5099 		 */
5100 		struct scsi_mode_sense_6 *scsi_cmd;
5101 
5102 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
5103 		bzero(scsi_cmd, sizeof(*scsi_cmd));
5104 		scsi_cmd->opcode = MODE_SENSE_6;
5105 		if (dbd != 0)
5106 			scsi_cmd->byte2 |= SMS_DBD;
5107 		scsi_cmd->page = page_code | page;
5108 		scsi_cmd->length = param_len;
5109 		cdb_len = sizeof(*scsi_cmd);
5110 	} else {
5111 		/*
5112 		 * Need a 10 byte cdb.
5113 		 */
5114 		struct scsi_mode_sense_10 *scsi_cmd;
5115 
5116 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
5117 		bzero(scsi_cmd, sizeof(*scsi_cmd));
5118 		scsi_cmd->opcode = MODE_SENSE_10;
5119 		if (dbd != 0)
5120 			scsi_cmd->byte2 |= SMS_DBD;
5121 		scsi_cmd->page = page_code | page;
5122 		scsi_ulto2b(param_len, scsi_cmd->length);
5123 		cdb_len = sizeof(*scsi_cmd);
5124 	}
5125 	cam_fill_csio(csio,
5126 		      retries,
5127 		      cbfcnp,
5128 		      CAM_DIR_IN,
5129 		      tag_action,
5130 		      param_buf,
5131 		      param_len,
5132 		      sense_len,
5133 		      cdb_len,
5134 		      timeout);
5135 }
5136 
5137 void
5138 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
5139 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
5140 		 u_int8_t tag_action, int scsi_page_fmt, int save_pages,
5141 		 u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
5142 		 u_int32_t timeout)
5143 {
5144 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
5145 			     scsi_page_fmt, save_pages, param_buf,
5146 			     param_len, 0, sense_len, timeout);
5147 }
5148 
5149 void
5150 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
5151 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
5152 		     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
5153 		     u_int8_t *param_buf, u_int32_t param_len,
5154 		     int minimum_cmd_size, u_int8_t sense_len,
5155 		     u_int32_t timeout)
5156 {
5157 	u_int8_t cdb_len;
5158 
5159 	/*
5160 	 * Use the smallest possible command to perform the operation.
5161 	 */
5162 	if ((param_len < 256)
5163 	 && (minimum_cmd_size < 10)) {
5164 		/*
5165 		 * We can fit in a 6 byte cdb.
5166 		 */
5167 		struct scsi_mode_select_6 *scsi_cmd;
5168 
5169 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
5170 		bzero(scsi_cmd, sizeof(*scsi_cmd));
5171 		scsi_cmd->opcode = MODE_SELECT_6;
5172 		if (scsi_page_fmt != 0)
5173 			scsi_cmd->byte2 |= SMS_PF;
5174 		if (save_pages != 0)
5175 			scsi_cmd->byte2 |= SMS_SP;
5176 		scsi_cmd->length = param_len;
5177 		cdb_len = sizeof(*scsi_cmd);
5178 	} else {
5179 		/*
5180 		 * Need a 10 byte cdb.
5181 		 */
5182 		struct scsi_mode_select_10 *scsi_cmd;
5183 
5184 		scsi_cmd =
5185 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
5186 		bzero(scsi_cmd, sizeof(*scsi_cmd));
5187 		scsi_cmd->opcode = MODE_SELECT_10;
5188 		if (scsi_page_fmt != 0)
5189 			scsi_cmd->byte2 |= SMS_PF;
5190 		if (save_pages != 0)
5191 			scsi_cmd->byte2 |= SMS_SP;
5192 		scsi_ulto2b(param_len, scsi_cmd->length);
5193 		cdb_len = sizeof(*scsi_cmd);
5194 	}
5195 	cam_fill_csio(csio,
5196 		      retries,
5197 		      cbfcnp,
5198 		      CAM_DIR_OUT,
5199 		      tag_action,
5200 		      param_buf,
5201 		      param_len,
5202 		      sense_len,
5203 		      cdb_len,
5204 		      timeout);
5205 }
5206 
5207 void
5208 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
5209 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
5210 	       u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
5211 	       int save_pages, int ppc, u_int32_t paramptr,
5212 	       u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
5213 	       u_int32_t timeout)
5214 {
5215 	struct scsi_log_sense *scsi_cmd;
5216 	u_int8_t cdb_len;
5217 
5218 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
5219 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5220 	scsi_cmd->opcode = LOG_SENSE;
5221 	scsi_cmd->page = page_code | page;
5222 	if (save_pages != 0)
5223 		scsi_cmd->byte2 |= SLS_SP;
5224 	if (ppc != 0)
5225 		scsi_cmd->byte2 |= SLS_PPC;
5226 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
5227 	scsi_ulto2b(param_len, scsi_cmd->length);
5228 	cdb_len = sizeof(*scsi_cmd);
5229 
5230 	cam_fill_csio(csio,
5231 		      retries,
5232 		      cbfcnp,
5233 		      /*flags*/CAM_DIR_IN,
5234 		      tag_action,
5235 		      /*data_ptr*/param_buf,
5236 		      /*dxfer_len*/param_len,
5237 		      sense_len,
5238 		      cdb_len,
5239 		      timeout);
5240 }
5241 
5242 void
5243 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
5244 		void (*cbfcnp)(struct cam_periph *, union ccb *),
5245 		u_int8_t tag_action, u_int8_t page_code, int save_pages,
5246 		int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
5247 		u_int8_t sense_len, u_int32_t timeout)
5248 {
5249 	struct scsi_log_select *scsi_cmd;
5250 	u_int8_t cdb_len;
5251 
5252 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
5253 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5254 	scsi_cmd->opcode = LOG_SELECT;
5255 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
5256 	if (save_pages != 0)
5257 		scsi_cmd->byte2 |= SLS_SP;
5258 	if (pc_reset != 0)
5259 		scsi_cmd->byte2 |= SLS_PCR;
5260 	scsi_ulto2b(param_len, scsi_cmd->length);
5261 	cdb_len = sizeof(*scsi_cmd);
5262 
5263 	cam_fill_csio(csio,
5264 		      retries,
5265 		      cbfcnp,
5266 		      /*flags*/CAM_DIR_OUT,
5267 		      tag_action,
5268 		      /*data_ptr*/param_buf,
5269 		      /*dxfer_len*/param_len,
5270 		      sense_len,
5271 		      cdb_len,
5272 		      timeout);
5273 }
5274 
5275 /*
5276  * Prevent or allow the user to remove the media
5277  */
5278 void
5279 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
5280 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
5281 	     u_int8_t tag_action, u_int8_t action,
5282 	     u_int8_t sense_len, u_int32_t timeout)
5283 {
5284 	struct scsi_prevent *scsi_cmd;
5285 
5286 	cam_fill_csio(csio,
5287 		      retries,
5288 		      cbfcnp,
5289 		      /*flags*/CAM_DIR_NONE,
5290 		      tag_action,
5291 		      /*data_ptr*/NULL,
5292 		      /*dxfer_len*/0,
5293 		      sense_len,
5294 		      sizeof(*scsi_cmd),
5295 		      timeout);
5296 
5297 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
5298 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5299 	scsi_cmd->opcode = PREVENT_ALLOW;
5300 	scsi_cmd->how = action;
5301 }
5302 
5303 /* XXX allow specification of address and PMI bit and LBA */
5304 void
5305 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
5306 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
5307 		   u_int8_t tag_action,
5308 		   struct scsi_read_capacity_data *rcap_buf,
5309 		   u_int8_t sense_len, u_int32_t timeout)
5310 {
5311 	struct scsi_read_capacity *scsi_cmd;
5312 
5313 	cam_fill_csio(csio,
5314 		      retries,
5315 		      cbfcnp,
5316 		      /*flags*/CAM_DIR_IN,
5317 		      tag_action,
5318 		      /*data_ptr*/(u_int8_t *)rcap_buf,
5319 		      /*dxfer_len*/sizeof(*rcap_buf),
5320 		      sense_len,
5321 		      sizeof(*scsi_cmd),
5322 		      timeout);
5323 
5324 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
5325 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5326 	scsi_cmd->opcode = READ_CAPACITY;
5327 }
5328 
5329 void
5330 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
5331 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
5332 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
5333 		      struct scsi_read_capacity_data_long *rcap_buf,
5334 		      uint8_t sense_len, uint32_t timeout)
5335 {
5336 	struct scsi_read_capacity_16 *scsi_cmd;
5337 
5338 
5339 	cam_fill_csio(csio,
5340 		      retries,
5341 		      cbfcnp,
5342 		      /*flags*/CAM_DIR_IN,
5343 		      tag_action,
5344 		      /*data_ptr*/(u_int8_t *)rcap_buf,
5345 		      /*dxfer_len*/sizeof(*rcap_buf),
5346 		      sense_len,
5347 		      sizeof(*scsi_cmd),
5348 		      timeout);
5349 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
5350 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5351 	scsi_cmd->opcode = SERVICE_ACTION_IN;
5352 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
5353 	scsi_u64to8b(lba, scsi_cmd->addr);
5354 	scsi_ulto4b(sizeof(*rcap_buf), scsi_cmd->alloc_len);
5355 	if (pmi)
5356 		reladr |= SRC16_PMI;
5357 	if (reladr)
5358 		reladr |= SRC16_RELADR;
5359 }
5360 
5361 void
5362 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
5363 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
5364 		 u_int8_t tag_action, u_int8_t select_report,
5365 		 struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
5366 		 u_int8_t sense_len, u_int32_t timeout)
5367 {
5368 	struct scsi_report_luns *scsi_cmd;
5369 
5370 	cam_fill_csio(csio,
5371 		      retries,
5372 		      cbfcnp,
5373 		      /*flags*/CAM_DIR_IN,
5374 		      tag_action,
5375 		      /*data_ptr*/(u_int8_t *)rpl_buf,
5376 		      /*dxfer_len*/alloc_len,
5377 		      sense_len,
5378 		      sizeof(*scsi_cmd),
5379 		      timeout);
5380 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
5381 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5382 	scsi_cmd->opcode = REPORT_LUNS;
5383 	scsi_cmd->select_report = select_report;
5384 	scsi_ulto4b(alloc_len, scsi_cmd->length);
5385 }
5386 
5387 void
5388 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
5389 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
5390 		 u_int8_t tag_action, u_int8_t pdf,
5391 		 void *buf, u_int32_t alloc_len,
5392 		 u_int8_t sense_len, u_int32_t timeout)
5393 {
5394 	struct scsi_target_group *scsi_cmd;
5395 
5396 	cam_fill_csio(csio,
5397 		      retries,
5398 		      cbfcnp,
5399 		      /*flags*/CAM_DIR_IN,
5400 		      tag_action,
5401 		      /*data_ptr*/(u_int8_t *)buf,
5402 		      /*dxfer_len*/alloc_len,
5403 		      sense_len,
5404 		      sizeof(*scsi_cmd),
5405 		      timeout);
5406 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
5407 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5408 	scsi_cmd->opcode = MAINTENANCE_IN;
5409 	scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
5410 	scsi_ulto4b(alloc_len, scsi_cmd->length);
5411 }
5412 
5413 void
5414 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
5415 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
5416 		 u_int8_t tag_action, void *buf, u_int32_t alloc_len,
5417 		 u_int8_t sense_len, u_int32_t timeout)
5418 {
5419 	struct scsi_target_group *scsi_cmd;
5420 
5421 	cam_fill_csio(csio,
5422 		      retries,
5423 		      cbfcnp,
5424 		      /*flags*/CAM_DIR_OUT,
5425 		      tag_action,
5426 		      /*data_ptr*/(u_int8_t *)buf,
5427 		      /*dxfer_len*/alloc_len,
5428 		      sense_len,
5429 		      sizeof(*scsi_cmd),
5430 		      timeout);
5431 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
5432 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5433 	scsi_cmd->opcode = MAINTENANCE_OUT;
5434 	scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
5435 	scsi_ulto4b(alloc_len, scsi_cmd->length);
5436 }
5437 
5438 /*
5439  * Syncronize the media to the contents of the cache for
5440  * the given lba/count pair.  Specifying 0/0 means sync
5441  * the whole cache.
5442  */
5443 void
5444 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
5445 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
5446 		       u_int8_t tag_action, u_int32_t begin_lba,
5447 		       u_int16_t lb_count, u_int8_t sense_len,
5448 		       u_int32_t timeout)
5449 {
5450 	struct scsi_sync_cache *scsi_cmd;
5451 
5452 	cam_fill_csio(csio,
5453 		      retries,
5454 		      cbfcnp,
5455 		      /*flags*/CAM_DIR_NONE,
5456 		      tag_action,
5457 		      /*data_ptr*/NULL,
5458 		      /*dxfer_len*/0,
5459 		      sense_len,
5460 		      sizeof(*scsi_cmd),
5461 		      timeout);
5462 
5463 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
5464 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5465 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
5466 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
5467 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
5468 }
5469 
5470 void
5471 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
5472 		void (*cbfcnp)(struct cam_periph *, union ccb *),
5473 		u_int8_t tag_action, int readop, u_int8_t byte2,
5474 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
5475 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
5476 		u_int32_t timeout)
5477 {
5478 	u_int8_t cdb_len;
5479 	/*
5480 	 * Use the smallest possible command to perform the operation
5481 	 * as some legacy hardware does not support the 10 byte commands.
5482 	 * If any of the bits in byte2 is set, we have to go with a larger
5483 	 * command.
5484 	 */
5485 	if ((minimum_cmd_size < 10)
5486 	 && ((lba & 0x1fffff) == lba)
5487 	 && ((block_count & 0xff) == block_count)
5488 	 && (byte2 == 0)) {
5489 		/*
5490 		 * We can fit in a 6 byte cdb.
5491 		 */
5492 		struct scsi_rw_6 *scsi_cmd;
5493 
5494 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
5495 		scsi_cmd->opcode = readop ? READ_6 : WRITE_6;
5496 		scsi_ulto3b(lba, scsi_cmd->addr);
5497 		scsi_cmd->length = block_count & 0xff;
5498 		scsi_cmd->control = 0;
5499 		cdb_len = sizeof(*scsi_cmd);
5500 
5501 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
5502 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
5503 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
5504 			   scsi_cmd->length, dxfer_len));
5505 	} else if ((minimum_cmd_size < 12)
5506 		&& ((block_count & 0xffff) == block_count)
5507 		&& ((lba & 0xffffffff) == lba)) {
5508 		/*
5509 		 * Need a 10 byte cdb.
5510 		 */
5511 		struct scsi_rw_10 *scsi_cmd;
5512 
5513 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
5514 		scsi_cmd->opcode = readop ? READ_10 : WRITE_10;
5515 		scsi_cmd->byte2 = byte2;
5516 		scsi_ulto4b(lba, scsi_cmd->addr);
5517 		scsi_cmd->reserved = 0;
5518 		scsi_ulto2b(block_count, scsi_cmd->length);
5519 		scsi_cmd->control = 0;
5520 		cdb_len = sizeof(*scsi_cmd);
5521 
5522 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
5523 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
5524 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
5525 			   scsi_cmd->addr[3], scsi_cmd->length[0],
5526 			   scsi_cmd->length[1], dxfer_len));
5527 	} else if ((minimum_cmd_size < 16)
5528 		&& ((block_count & 0xffffffff) == block_count)
5529 		&& ((lba & 0xffffffff) == lba)) {
5530 		/*
5531 		 * The block count is too big for a 10 byte CDB, use a 12
5532 		 * byte CDB.
5533 		 */
5534 		struct scsi_rw_12 *scsi_cmd;
5535 
5536 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
5537 		scsi_cmd->opcode = readop ? READ_12 : WRITE_12;
5538 		scsi_cmd->byte2 = byte2;
5539 		scsi_ulto4b(lba, scsi_cmd->addr);
5540 		scsi_cmd->reserved = 0;
5541 		scsi_ulto4b(block_count, scsi_cmd->length);
5542 		scsi_cmd->control = 0;
5543 		cdb_len = sizeof(*scsi_cmd);
5544 
5545 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
5546 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
5547 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
5548 			   scsi_cmd->addr[3], scsi_cmd->length[0],
5549 			   scsi_cmd->length[1], scsi_cmd->length[2],
5550 			   scsi_cmd->length[3], dxfer_len));
5551 	} else {
5552 		/*
5553 		 * 16 byte CDB.  We'll only get here if the LBA is larger
5554 		 * than 2^32, or if the user asks for a 16 byte command.
5555 		 */
5556 		struct scsi_rw_16 *scsi_cmd;
5557 
5558 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
5559 		scsi_cmd->opcode = readop ? READ_16 : WRITE_16;
5560 		scsi_cmd->byte2 = byte2;
5561 		scsi_u64to8b(lba, scsi_cmd->addr);
5562 		scsi_cmd->reserved = 0;
5563 		scsi_ulto4b(block_count, scsi_cmd->length);
5564 		scsi_cmd->control = 0;
5565 		cdb_len = sizeof(*scsi_cmd);
5566 	}
5567 	cam_fill_csio(csio,
5568 		      retries,
5569 		      cbfcnp,
5570 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
5571 		      tag_action,
5572 		      data_ptr,
5573 		      dxfer_len,
5574 		      sense_len,
5575 		      cdb_len,
5576 		      timeout);
5577 }
5578 
5579 void
5580 scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
5581 				void (*cbfcnp)(struct cam_periph *, union ccb*),
5582 				uint8_t tag_action, int pcv, uint8_t page_code,
5583 				uint8_t *data_ptr, uint16_t allocation_length,
5584 				uint8_t sense_len, uint32_t timeout)
5585 {
5586 	struct scsi_receive_diag *scsi_cmd;
5587 
5588 	scsi_cmd = (struct scsi_receive_diag *)&csio->cdb_io.cdb_bytes;
5589 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
5590 	scsi_cmd->opcode = RECEIVE_DIAGNOSTIC;
5591 	if (pcv) {
5592 		scsi_cmd->byte2 |= SRD_PCV;
5593 		scsi_cmd->page_code = page_code;
5594 	}
5595 	scsi_ulto2b(allocation_length, scsi_cmd->length);
5596 
5597 	cam_fill_csio(csio,
5598 		      retries,
5599 		      cbfcnp,
5600 		      /*flags*/CAM_DIR_IN,
5601 		      tag_action,
5602 		      data_ptr,
5603 		      allocation_length,
5604 		      sense_len,
5605 		      sizeof(*scsi_cmd),
5606 		      timeout);
5607 }
5608 
5609 void
5610 scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
5611 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
5612 		     uint8_t tag_action, int unit_offline, int device_offline,
5613 		     int self_test, int page_format, int self_test_code,
5614 		     uint8_t *data_ptr, uint16_t param_list_length,
5615 		     uint8_t sense_len, uint32_t timeout)
5616 {
5617 	struct scsi_send_diag *scsi_cmd;
5618 
5619 	scsi_cmd = (struct scsi_send_diag *)&csio->cdb_io.cdb_bytes;
5620 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
5621 	scsi_cmd->opcode = SEND_DIAGNOSTIC;
5622 
5623 	/*
5624 	 * The default self-test mode control and specific test
5625 	 * control are mutually exclusive.
5626 	 */
5627 	if (self_test)
5628 		self_test_code = SSD_SELF_TEST_CODE_NONE;
5629 
5630 	scsi_cmd->byte2 = ((self_test_code << SSD_SELF_TEST_CODE_SHIFT)
5631 			 & SSD_SELF_TEST_CODE_MASK)
5632 			| (unit_offline   ? SSD_UNITOFFL : 0)
5633 			| (device_offline ? SSD_DEVOFFL  : 0)
5634 			| (self_test      ? SSD_SELFTEST : 0)
5635 			| (page_format    ? SSD_PF       : 0);
5636 	scsi_ulto2b(param_list_length, scsi_cmd->length);
5637 
5638 	cam_fill_csio(csio,
5639 		      retries,
5640 		      cbfcnp,
5641 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
5642 		      tag_action,
5643 		      data_ptr,
5644 		      param_list_length,
5645 		      sense_len,
5646 		      sizeof(*scsi_cmd),
5647 		      timeout);
5648 }
5649 
5650 void
5651 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
5652 		void (*cbfcnp)(struct cam_periph *, union ccb *),
5653 		u_int8_t tag_action, int start, int load_eject,
5654 		int immediate, u_int8_t sense_len, u_int32_t timeout)
5655 {
5656 	struct scsi_start_stop_unit *scsi_cmd;
5657 	int extra_flags = 0;
5658 
5659 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
5660 	bzero(scsi_cmd, sizeof(*scsi_cmd));
5661 	scsi_cmd->opcode = START_STOP_UNIT;
5662 	if (start != 0) {
5663 		scsi_cmd->how |= SSS_START;
5664 		/* it takes a lot of power to start a drive */
5665 		extra_flags |= CAM_HIGH_POWER;
5666 	}
5667 	if (load_eject != 0)
5668 		scsi_cmd->how |= SSS_LOEJ;
5669 	if (immediate != 0)
5670 		scsi_cmd->byte2 |= SSS_IMMED;
5671 
5672 	cam_fill_csio(csio,
5673 		      retries,
5674 		      cbfcnp,
5675 		      /*flags*/CAM_DIR_NONE | extra_flags,
5676 		      tag_action,
5677 		      /*data_ptr*/NULL,
5678 		      /*dxfer_len*/0,
5679 		      sense_len,
5680 		      sizeof(*scsi_cmd),
5681 		      timeout);
5682 }
5683 
5684 
5685 /*
5686  * Try make as good a match as possible with
5687  * available sub drivers
5688  */
5689 int
5690 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
5691 {
5692 	struct scsi_inquiry_pattern *entry;
5693 	struct scsi_inquiry_data *inq;
5694 
5695 	entry = (struct scsi_inquiry_pattern *)table_entry;
5696 	inq = (struct scsi_inquiry_data *)inqbuffer;
5697 
5698 	if (((SID_TYPE(inq) == entry->type)
5699 	  || (entry->type == T_ANY))
5700 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
5701 				   : entry->media_type & SIP_MEDIA_FIXED)
5702 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
5703 	 && (cam_strmatch(inq->product, entry->product,
5704 			  sizeof(inq->product)) == 0)
5705 	 && (cam_strmatch(inq->revision, entry->revision,
5706 			  sizeof(inq->revision)) == 0)) {
5707 		return (0);
5708 	}
5709         return (-1);
5710 }
5711 
5712 /*
5713  * Try make as good a match as possible with
5714  * available sub drivers
5715  */
5716 int
5717 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
5718 {
5719 	struct scsi_static_inquiry_pattern *entry;
5720 	struct scsi_inquiry_data *inq;
5721 
5722 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
5723 	inq = (struct scsi_inquiry_data *)inqbuffer;
5724 
5725 	if (((SID_TYPE(inq) == entry->type)
5726 	  || (entry->type == T_ANY))
5727 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
5728 				   : entry->media_type & SIP_MEDIA_FIXED)
5729 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
5730 	 && (cam_strmatch(inq->product, entry->product,
5731 			  sizeof(inq->product)) == 0)
5732 	 && (cam_strmatch(inq->revision, entry->revision,
5733 			  sizeof(inq->revision)) == 0)) {
5734 		return (0);
5735 	}
5736         return (-1);
5737 }
5738 
5739 /**
5740  * Compare two buffers of vpd device descriptors for a match.
5741  *
5742  * \param lhs      Pointer to first buffer of descriptors to compare.
5743  * \param lhs_len  The length of the first buffer.
5744  * \param rhs	   Pointer to second buffer of descriptors to compare.
5745  * \param rhs_len  The length of the second buffer.
5746  *
5747  * \return  0 on a match, -1 otherwise.
5748  *
5749  * Treat rhs and lhs as arrays of vpd device id descriptors.  Walk lhs matching
5750  * agains each element in rhs until all data are exhausted or we have found
5751  * a match.
5752  */
5753 int
5754 scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
5755 {
5756 	struct scsi_vpd_id_descriptor *lhs_id;
5757 	struct scsi_vpd_id_descriptor *lhs_last;
5758 	struct scsi_vpd_id_descriptor *rhs_last;
5759 	uint8_t *lhs_end;
5760 	uint8_t *rhs_end;
5761 
5762 	lhs_end = lhs + lhs_len;
5763 	rhs_end = rhs + rhs_len;
5764 
5765 	/*
5766 	 * rhs_last and lhs_last are the last posible position of a valid
5767 	 * descriptor assuming it had a zero length identifier.  We use
5768 	 * these variables to insure we can safely dereference the length
5769 	 * field in our loop termination tests.
5770 	 */
5771 	lhs_last = (struct scsi_vpd_id_descriptor *)
5772 	    (lhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
5773 	rhs_last = (struct scsi_vpd_id_descriptor *)
5774 	    (rhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
5775 
5776 	lhs_id = (struct scsi_vpd_id_descriptor *)lhs;
5777 	while (lhs_id <= lhs_last
5778 	    && (lhs_id->identifier + lhs_id->length) <= lhs_end) {
5779 		struct scsi_vpd_id_descriptor *rhs_id;
5780 
5781 		rhs_id = (struct scsi_vpd_id_descriptor *)rhs;
5782 		while (rhs_id <= rhs_last
5783 		    && (rhs_id->identifier + rhs_id->length) <= rhs_end) {
5784 
5785 			if (rhs_id->length == lhs_id->length
5786 			 && memcmp(rhs_id->identifier, lhs_id->identifier,
5787 				   rhs_id->length) == 0)
5788 				return (0);
5789 
5790 			rhs_id = (struct scsi_vpd_id_descriptor *)
5791 			   (rhs_id->identifier + rhs_id->length);
5792 		}
5793 		lhs_id = (struct scsi_vpd_id_descriptor *)
5794 		   (lhs_id->identifier + lhs_id->length);
5795 	}
5796 	return (-1);
5797 }
5798 
5799 #ifdef _KERNEL
5800 static void
5801 init_scsi_delay(void)
5802 {
5803 	int delay;
5804 
5805 	delay = SCSI_DELAY;
5806 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
5807 
5808 	if (set_scsi_delay(delay) != 0) {
5809 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
5810 		set_scsi_delay(SCSI_DELAY);
5811 	}
5812 }
5813 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
5814 
5815 static int
5816 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
5817 {
5818 	int error, delay;
5819 
5820 	delay = scsi_delay;
5821 	error = sysctl_handle_int(oidp, &delay, 0, req);
5822 	if (error != 0 || req->newptr == NULL)
5823 		return (error);
5824 	return (set_scsi_delay(delay));
5825 }
5826 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
5827     0, 0, sysctl_scsi_delay, "I",
5828     "Delay to allow devices to settle after a SCSI bus reset (ms)");
5829 
5830 static int
5831 set_scsi_delay(int delay)
5832 {
5833 	/*
5834          * If someone sets this to 0, we assume that they want the
5835          * minimum allowable bus settle delay.
5836 	 */
5837 	if (delay == 0) {
5838 		printf("cam: using minimum scsi_delay (%dms)\n",
5839 		    SCSI_MIN_DELAY);
5840 		delay = SCSI_MIN_DELAY;
5841 	}
5842 	if (delay < SCSI_MIN_DELAY)
5843 		return (EINVAL);
5844 	scsi_delay = delay;
5845 	return (0);
5846 }
5847 #endif /* _KERNEL */
5848