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