xref: /freebsd/sys/cam/scsi/scsi_all.c (revision d96700a6da2afa88607fbd7405ade439424d10d9)
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/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/ctype.h>
48 #else
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <ctype.h>
54 #endif
55 
56 #include <cam/cam.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_queue.h>
59 #include <cam/cam_xpt.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <sys/ata.h>
62 #include <sys/sbuf.h>
63 
64 #ifdef _KERNEL
65 #include <cam/cam_periph.h>
66 #include <cam/cam_xpt_sim.h>
67 #include <cam/cam_xpt_periph.h>
68 #include <cam/cam_xpt_internal.h>
69 #else
70 #include <camlib.h>
71 #include <stddef.h>
72 
73 #ifndef FALSE
74 #define FALSE   0
75 #endif /* FALSE */
76 #ifndef TRUE
77 #define TRUE    1
78 #endif /* TRUE */
79 #define ERESTART        -1              /* restart syscall */
80 #define EJUSTRETURN     -2              /* don't modify regs, just return */
81 #endif /* !_KERNEL */
82 
83 /*
84  * This is the default number of milliseconds we wait for devices to settle
85  * after a SCSI bus reset.
86  */
87 #ifndef SCSI_DELAY
88 #define SCSI_DELAY 2000
89 #endif
90 /*
91  * All devices need _some_ sort of bus settle delay, so we'll set it to
92  * a minimum value of 100ms. Note that this is pertinent only for SPI-
93  * not transport like Fibre Channel or iSCSI where 'delay' is completely
94  * meaningless.
95  */
96 #ifndef SCSI_MIN_DELAY
97 #define SCSI_MIN_DELAY 100
98 #endif
99 /*
100  * Make sure the user isn't using seconds instead of milliseconds.
101  */
102 #if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
103 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
104 #endif
105 
106 int scsi_delay;
107 
108 static int	ascentrycomp(const void *key, const void *member);
109 static int	senseentrycomp(const void *key, const void *member);
110 static void	fetchtableentries(int sense_key, int asc, int ascq,
111 				  struct scsi_inquiry_data *,
112 				  const struct sense_key_table_entry **,
113 				  const struct asc_table_entry **);
114 
115 #ifdef _KERNEL
116 static void	init_scsi_delay(void);
117 static int	sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
118 static int	set_scsi_delay(int delay);
119 #endif
120 
121 #if !defined(SCSI_NO_OP_STRINGS)
122 
123 #define	D	(1 << T_DIRECT)
124 #define	T	(1 << T_SEQUENTIAL)
125 #define	L	(1 << T_PRINTER)
126 #define	P	(1 << T_PROCESSOR)
127 #define	W	(1 << T_WORM)
128 #define	R	(1 << T_CDROM)
129 #define	O	(1 << T_OPTICAL)
130 #define	M	(1 << T_CHANGER)
131 #define	A	(1 << T_STORARRAY)
132 #define	E	(1 << T_ENCLOSURE)
133 #define	B	(1 << T_RBC)
134 #define	K	(1 << T_OCRW)
135 #define	V	(1 << T_ADC)
136 #define	F	(1 << T_OSD)
137 #define	S	(1 << T_SCANNER)
138 #define	C	(1 << T_COMM)
139 
140 #define ALL	(D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
141 
142 static struct op_table_entry plextor_cd_ops[] = {
143 	{ 0xD8, R, "CD-DA READ" }
144 };
145 
146 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
147 	{
148 		/*
149 		 * I believe that 0xD8 is the Plextor proprietary command
150 		 * to read CD-DA data.  I'm not sure which Plextor CDROM
151 		 * models support the command, though.  I know for sure
152 		 * that the 4X, 8X, and 12X models do, and presumably the
153 		 * 12-20X does.  I don't know about any earlier models,
154 		 * though.  If anyone has any more complete information,
155 		 * feel free to change this quirk entry.
156 		 */
157 		{T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
158 		nitems(plextor_cd_ops),
159 		plextor_cd_ops
160 	}
161 };
162 
163 static struct op_table_entry scsi_op_codes[] = {
164 	/*
165 	 * From: http://www.t10.org/lists/op-num.txt
166 	 * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
167 	 *              and Jung-uk Kim (jkim@FreeBSD.org)
168 	 *
169 	 * Note:  order is important in this table, scsi_op_desc() currently
170 	 * depends on the opcodes in the table being in order to save
171 	 * search time.
172 	 * Note:  scanner and comm. devices are carried over from the previous
173 	 * version because they were removed in the latest spec.
174 	 */
175 	/* File: OP-NUM.TXT
176 	 *
177 	 * SCSI Operation Codes
178 	 * Numeric Sorted Listing
179 	 * as of  5/26/15
180 	 *
181 	 *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
182 	 *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
183 	 *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
184 	 *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
185 	 *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
186 	 *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
187 	 *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
188 	 *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
189 	 *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
190 	 *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
191 	 *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
192 	 *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
193 	 *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
194 	 *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
195 	 * OP  DTLPWROMAEBKVF  Description
196 	 * --  --------------  ---------------------------------------------- */
197 	/* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
198 	{ 0x00,	ALL, "TEST UNIT READY" },
199 	/* 01   M              REWIND */
200 	{ 0x01,	T, "REWIND" },
201 	/* 01  Z V ZZZZ        REZERO UNIT */
202 	{ 0x01,	D | W | R | O | M, "REZERO UNIT" },
203 	/* 02  VVVVVV V */
204 	/* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
205 	{ 0x03,	ALL, "REQUEST SENSE" },
206 	/* 04  M    OO         FORMAT UNIT */
207 	{ 0x04,	D | R | O, "FORMAT UNIT" },
208 	/* 04   O              FORMAT MEDIUM */
209 	{ 0x04,	T, "FORMAT MEDIUM" },
210 	/* 04    O             FORMAT */
211 	{ 0x04,	L, "FORMAT" },
212 	/* 05  VMVVVV V        READ BLOCK LIMITS */
213 	{ 0x05,	T, "READ BLOCK LIMITS" },
214 	/* 06  VVVVVV V */
215 	/* 07  OVV O OV        REASSIGN BLOCKS */
216 	{ 0x07,	D | W | O, "REASSIGN BLOCKS" },
217 	/* 07         O        INITIALIZE ELEMENT STATUS */
218 	{ 0x07,	M, "INITIALIZE ELEMENT STATUS" },
219 	/* 08  MOV O OV        READ(6) */
220 	{ 0x08,	D | T | W | O, "READ(6)" },
221 	/* 08     O            RECEIVE */
222 	{ 0x08,	P, "RECEIVE" },
223 	/* 08                  GET MESSAGE(6) */
224 	{ 0x08, C, "GET MESSAGE(6)" },
225 	/* 09  VVVVVV V */
226 	/* 0A  OO  O OV        WRITE(6) */
227 	{ 0x0A,	D | T | W | O, "WRITE(6)" },
228 	/* 0A     M            SEND(6) */
229 	{ 0x0A,	P, "SEND(6)" },
230 	/* 0A                  SEND MESSAGE(6) */
231 	{ 0x0A, C, "SEND MESSAGE(6)" },
232 	/* 0A    M             PRINT */
233 	{ 0x0A,	L, "PRINT" },
234 	/* 0B  Z   ZOZV        SEEK(6) */
235 	{ 0x0B,	D | W | R | O, "SEEK(6)" },
236 	/* 0B   O              SET CAPACITY */
237 	{ 0x0B,	T, "SET CAPACITY" },
238 	/* 0B    O             SLEW AND PRINT */
239 	{ 0x0B,	L, "SLEW AND PRINT" },
240 	/* 0C  VVVVVV V */
241 	/* 0D  VVVVVV V */
242 	/* 0E  VVVVVV V */
243 	/* 0F  VOVVVV V        READ REVERSE(6) */
244 	{ 0x0F,	T, "READ REVERSE(6)" },
245 	/* 10  VM VVV          WRITE FILEMARKS(6) */
246 	{ 0x10,	T, "WRITE FILEMARKS(6)" },
247 	/* 10    O             SYNCHRONIZE BUFFER */
248 	{ 0x10,	L, "SYNCHRONIZE BUFFER" },
249 	/* 11  VMVVVV          SPACE(6) */
250 	{ 0x11,	T, "SPACE(6)" },
251 	/* 12  MMMMMMMMMMMMMM  INQUIRY */
252 	{ 0x12,	ALL, "INQUIRY" },
253 	/* 13  V VVVV */
254 	/* 13   O              VERIFY(6) */
255 	{ 0x13,	T, "VERIFY(6)" },
256 	/* 14  VOOVVV          RECOVER BUFFERED DATA */
257 	{ 0x14,	T | L, "RECOVER BUFFERED DATA" },
258 	/* 15  OMO O OOOO OO   MODE SELECT(6) */
259 	{ 0x15,	ALL & ~(P | R | B | F), "MODE SELECT(6)" },
260 	/* 16  ZZMZO OOOZ O    RESERVE(6) */
261 	{ 0x16,	ALL & ~(R | B | V | F | C), "RESERVE(6)" },
262 	/* 16         Z        RESERVE ELEMENT(6) */
263 	{ 0x16,	M, "RESERVE ELEMENT(6)" },
264 	/* 17  ZZMZO OOOZ O    RELEASE(6) */
265 	{ 0x17,	ALL & ~(R | B | V | F | C), "RELEASE(6)" },
266 	/* 17         Z        RELEASE ELEMENT(6) */
267 	{ 0x17,	M, "RELEASE ELEMENT(6)" },
268 	/* 18  ZZZZOZO    Z    COPY */
269 	{ 0x18,	D | T | L | P | W | R | O | K | S, "COPY" },
270 	/* 19  VMVVVV          ERASE(6) */
271 	{ 0x19,	T, "ERASE(6)" },
272 	/* 1A  OMO O OOOO OO   MODE SENSE(6) */
273 	{ 0x1A,	ALL & ~(P | R | B | F), "MODE SENSE(6)" },
274 	/* 1B  O   OOO O MO O  START STOP UNIT */
275 	{ 0x1B,	D | W | R | O | A | B | K | F, "START STOP UNIT" },
276 	/* 1B   O          M   LOAD UNLOAD */
277 	{ 0x1B,	T | V, "LOAD UNLOAD" },
278 	/* 1B                  SCAN */
279 	{ 0x1B, S, "SCAN" },
280 	/* 1B    O             STOP PRINT */
281 	{ 0x1B,	L, "STOP PRINT" },
282 	/* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
283 	{ 0x1B,	M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
284 	/* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
285 	{ 0x1C,	ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
286 	/* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
287 	{ 0x1D,	ALL & ~(R | B), "SEND DIAGNOSTIC" },
288 	/* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
289 	{ 0x1E,	D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
290 	/* 1F */
291 	/* 20  V   VVV    V */
292 	/* 21  V   VVV    V */
293 	/* 22  V   VVV    V */
294 	/* 23  V   V V    V */
295 	/* 23       O          READ FORMAT CAPACITIES */
296 	{ 0x23,	R, "READ FORMAT CAPACITIES" },
297 	/* 24  V   VV          SET WINDOW */
298 	{ 0x24, S, "SET WINDOW" },
299 	/* 25  M   M M   M     READ CAPACITY(10) */
300 	{ 0x25,	D | W | O | B, "READ CAPACITY(10)" },
301 	/* 25       O          READ CAPACITY */
302 	{ 0x25,	R, "READ CAPACITY" },
303 	/* 25             M    READ CARD CAPACITY */
304 	{ 0x25,	K, "READ CARD CAPACITY" },
305 	/* 25                  GET WINDOW */
306 	{ 0x25, S, "GET WINDOW" },
307 	/* 26  V   VV */
308 	/* 27  V   VV */
309 	/* 28  M   MOM   MM    READ(10) */
310 	{ 0x28,	D | W | R | O | B | K | S, "READ(10)" },
311 	/* 28                  GET MESSAGE(10) */
312 	{ 0x28, C, "GET MESSAGE(10)" },
313 	/* 29  V   VVO         READ GENERATION */
314 	{ 0x29,	O, "READ GENERATION" },
315 	/* 2A  O   MOM   MO    WRITE(10) */
316 	{ 0x2A,	D | W | R | O | B | K, "WRITE(10)" },
317 	/* 2A                  SEND(10) */
318 	{ 0x2A, S, "SEND(10)" },
319 	/* 2A                  SEND MESSAGE(10) */
320 	{ 0x2A, C, "SEND MESSAGE(10)" },
321 	/* 2B  Z   OOO    O    SEEK(10) */
322 	{ 0x2B,	D | W | R | O | K, "SEEK(10)" },
323 	/* 2B   O              LOCATE(10) */
324 	{ 0x2B,	T, "LOCATE(10)" },
325 	/* 2B         O        POSITION TO ELEMENT */
326 	{ 0x2B,	M, "POSITION TO ELEMENT" },
327 	/* 2C  V    OO         ERASE(10) */
328 	{ 0x2C,	R | O, "ERASE(10)" },
329 	/* 2D        O         READ UPDATED BLOCK */
330 	{ 0x2D,	O, "READ UPDATED BLOCK" },
331 	/* 2D  V */
332 	/* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
333 	{ 0x2E,	D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
334 	/* 2F  O   OOO         VERIFY(10) */
335 	{ 0x2F,	D | W | R | O, "VERIFY(10)" },
336 	/* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
337 	{ 0x30,	D | W | R | O, "SEARCH DATA HIGH(10)" },
338 	/* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
339 	{ 0x31,	D | W | R | O, "SEARCH DATA EQUAL(10)" },
340 	/* 31                  OBJECT POSITION */
341 	{ 0x31, S, "OBJECT POSITION" },
342 	/* 32  Z   ZZZ         SEARCH DATA LOW(10) */
343 	{ 0x32,	D | W | R | O, "SEARCH DATA LOW(10)" },
344 	/* 33  Z   OZO         SET LIMITS(10) */
345 	{ 0x33,	D | W | R | O, "SET LIMITS(10)" },
346 	/* 34  O   O O    O    PRE-FETCH(10) */
347 	{ 0x34,	D | W | O | K, "PRE-FETCH(10)" },
348 	/* 34   M              READ POSITION */
349 	{ 0x34,	T, "READ POSITION" },
350 	/* 34                  GET DATA BUFFER STATUS */
351 	{ 0x34, S, "GET DATA BUFFER STATUS" },
352 	/* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
353 	{ 0x35,	D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
354 	/* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
355 	{ 0x36,	D | W | O | K, "LOCK UNLOCK CACHE(10)" },
356 	/* 37  O     O         READ DEFECT DATA(10) */
357 	{ 0x37,	D | O, "READ DEFECT DATA(10)" },
358 	/* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
359 	{ 0x37,	M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
360 	/* 38      O O    O    MEDIUM SCAN */
361 	{ 0x38,	W | O | K, "MEDIUM SCAN" },
362 	/* 39  ZZZZOZO    Z    COMPARE */
363 	{ 0x39,	D | T | L | P | W | R | O | K | S, "COMPARE" },
364 	/* 3A  ZZZZOZO    Z    COPY AND VERIFY */
365 	{ 0x3A,	D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
366 	/* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
367 	{ 0x3B,	ALL, "WRITE BUFFER" },
368 	/* 3C  OOOOOOOOOO OOO  READ BUFFER */
369 	{ 0x3C,	ALL & ~(B), "READ BUFFER" },
370 	/* 3D        O         UPDATE BLOCK */
371 	{ 0x3D,	O, "UPDATE BLOCK" },
372 	/* 3E  O   O O         READ LONG(10) */
373 	{ 0x3E,	D | W | O, "READ LONG(10)" },
374 	/* 3F  O   O O         WRITE LONG(10) */
375 	{ 0x3F,	D | W | O, "WRITE LONG(10)" },
376 	/* 40  ZZZZOZOZ        CHANGE DEFINITION */
377 	{ 0x40,	D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
378 	/* 41  O               WRITE SAME(10) */
379 	{ 0x41,	D, "WRITE SAME(10)" },
380 	/* 42       O          UNMAP */
381 	{ 0x42,	D, "UNMAP" },
382 	/* 42       O          READ SUB-CHANNEL */
383 	{ 0x42,	R, "READ SUB-CHANNEL" },
384 	/* 43       O          READ TOC/PMA/ATIP */
385 	{ 0x43,	R, "READ TOC/PMA/ATIP" },
386 	/* 44   M          M   REPORT DENSITY SUPPORT */
387 	{ 0x44,	T | V, "REPORT DENSITY SUPPORT" },
388 	/* 44                  READ HEADER */
389 	/* 45       O          PLAY AUDIO(10) */
390 	{ 0x45,	R, "PLAY AUDIO(10)" },
391 	/* 46       M          GET CONFIGURATION */
392 	{ 0x46,	R, "GET CONFIGURATION" },
393 	/* 47       O          PLAY AUDIO MSF */
394 	{ 0x47,	R, "PLAY AUDIO MSF" },
395 	/* 48 */
396 	/* 49 */
397 	/* 4A       M          GET EVENT STATUS NOTIFICATION */
398 	{ 0x4A,	R, "GET EVENT STATUS NOTIFICATION" },
399 	/* 4B       O          PAUSE/RESUME */
400 	{ 0x4B,	R, "PAUSE/RESUME" },
401 	/* 4C  OOOOO OOOO OOO  LOG SELECT */
402 	{ 0x4C,	ALL & ~(R | B), "LOG SELECT" },
403 	/* 4D  OOOOO OOOO OMO  LOG SENSE */
404 	{ 0x4D,	ALL & ~(R | B), "LOG SENSE" },
405 	/* 4E       O          STOP PLAY/SCAN */
406 	{ 0x4E,	R, "STOP PLAY/SCAN" },
407 	/* 4F */
408 	/* 50  O               XDWRITE(10) */
409 	{ 0x50,	D, "XDWRITE(10)" },
410 	/* 51  O               XPWRITE(10) */
411 	{ 0x51,	D, "XPWRITE(10)" },
412 	/* 51       O          READ DISC INFORMATION */
413 	{ 0x51,	R, "READ DISC INFORMATION" },
414 	/* 52  O               XDREAD(10) */
415 	{ 0x52,	D, "XDREAD(10)" },
416 	/* 52       O          READ TRACK INFORMATION */
417 	{ 0x52,	R, "READ TRACK INFORMATION" },
418 	/* 53       O          RESERVE TRACK */
419 	{ 0x53,	R, "RESERVE TRACK" },
420 	/* 54       O          SEND OPC INFORMATION */
421 	{ 0x54,	R, "SEND OPC INFORMATION" },
422 	/* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
423 	{ 0x55,	ALL & ~(P), "MODE SELECT(10)" },
424 	/* 56  ZZMZO OOOZ      RESERVE(10) */
425 	{ 0x56,	ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
426 	/* 56         Z        RESERVE ELEMENT(10) */
427 	{ 0x56,	M, "RESERVE ELEMENT(10)" },
428 	/* 57  ZZMZO OOOZ      RELEASE(10) */
429 	{ 0x57,	ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
430 	/* 57         Z        RELEASE ELEMENT(10) */
431 	{ 0x57,	M, "RELEASE ELEMENT(10)" },
432 	/* 58       O          REPAIR TRACK */
433 	{ 0x58,	R, "REPAIR TRACK" },
434 	/* 59 */
435 	/* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
436 	{ 0x5A,	ALL & ~(P), "MODE SENSE(10)" },
437 	/* 5B       O          CLOSE TRACK/SESSION */
438 	{ 0x5B,	R, "CLOSE TRACK/SESSION" },
439 	/* 5C       O          READ BUFFER CAPACITY */
440 	{ 0x5C,	R, "READ BUFFER CAPACITY" },
441 	/* 5D       O          SEND CUE SHEET */
442 	{ 0x5D,	R, "SEND CUE SHEET" },
443 	/* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
444 	{ 0x5E,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
445 	/* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
446 	{ 0x5F,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
447 	/* 7E  OO   O OOOO O   extended CDB */
448 	{ 0x7E,	D | T | R | M | A | E | B | V, "extended CDB" },
449 	/* 7F  O            M  variable length CDB (more than 16 bytes) */
450 	{ 0x7F,	D | F, "variable length CDB (more than 16 bytes)" },
451 	/* 80  Z               XDWRITE EXTENDED(16) */
452 	{ 0x80,	D, "XDWRITE EXTENDED(16)" },
453 	/* 80   M              WRITE FILEMARKS(16) */
454 	{ 0x80,	T, "WRITE FILEMARKS(16)" },
455 	/* 81  Z               REBUILD(16) */
456 	{ 0x81,	D, "REBUILD(16)" },
457 	/* 81   O              READ REVERSE(16) */
458 	{ 0x81,	T, "READ REVERSE(16)" },
459 	/* 82  Z               REGENERATE(16) */
460 	{ 0x82,	D, "REGENERATE(16)" },
461 	/* 83  OOOOO O    OO   EXTENDED COPY */
462 	{ 0x83,	D | T | L | P | W | O | K | V, "EXTENDED COPY" },
463 	/* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
464 	{ 0x84,	D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
465 	/* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
466 	{ 0x85,	D | R | B, "ATA COMMAND PASS THROUGH(16)" },
467 	/* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
468 	{ 0x86,	ALL & ~(L | R | F), "ACCESS CONTROL IN" },
469 	/* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
470 	{ 0x87,	ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
471 	/*
472 	 * XXX READ(16)/WRITE(16) were not listed for CD/DVE in op-num.txt
473 	 * but we had it since r1.40.  Do we really want them?
474 	 */
475 	/* 88  MM  O O   O     READ(16) */
476 	{ 0x88,	D | T | W | O | B, "READ(16)" },
477 	/* 89  O               COMPARE AND WRITE*/
478 	{ 0x89,	D, "COMPARE AND WRITE" },
479 	/* 8A  OM  O O   O     WRITE(16) */
480 	{ 0x8A,	D | T | W | O | B, "WRITE(16)" },
481 	/* 8B  O               ORWRITE */
482 	{ 0x8B,	D, "ORWRITE" },
483 	/* 8C  OO  O OO  O M   READ ATTRIBUTE */
484 	{ 0x8C,	D | T | W | O | M | B | V, "READ ATTRIBUTE" },
485 	/* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
486 	{ 0x8D,	D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
487 	/* 8E  O   O O   O     WRITE AND VERIFY(16) */
488 	{ 0x8E,	D | W | O | B, "WRITE AND VERIFY(16)" },
489 	/* 8F  OO  O O   O     VERIFY(16) */
490 	{ 0x8F,	D | T | W | O | B, "VERIFY(16)" },
491 	/* 90  O   O O   O     PRE-FETCH(16) */
492 	{ 0x90,	D | W | O | B, "PRE-FETCH(16)" },
493 	/* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
494 	{ 0x91,	D | W | O | B, "SYNCHRONIZE CACHE(16)" },
495 	/* 91   O              SPACE(16) */
496 	{ 0x91,	T, "SPACE(16)" },
497 	/* 92  Z   O O         LOCK UNLOCK CACHE(16) */
498 	{ 0x92,	D | W | O, "LOCK UNLOCK CACHE(16)" },
499 	/* 92   O              LOCATE(16) */
500 	{ 0x92,	T, "LOCATE(16)" },
501 	/* 93  O               WRITE SAME(16) */
502 	{ 0x93,	D, "WRITE SAME(16)" },
503 	/* 93   M              ERASE(16) */
504 	{ 0x93,	T, "ERASE(16)" },
505 	/* 94  O               ZBC OUT */
506 	{ 0x94,	ALL, "ZBC OUT" },
507 	/* 95  O               ZBC IN */
508 	{ 0x95,	ALL, "ZBC IN" },
509 	/* 96 */
510 	/* 97 */
511 	/* 98 */
512 	/* 99 */
513 	/* 9A  O               WRITE STREAM(16) */
514 	{ 0x9A,	D, "WRITE STREAM(16)" },
515 	/* 9B  OOOOOOOOOO OOO  READ BUFFER(16) */
516 	{ 0x9B,	ALL & ~(B) , "READ BUFFER(16)" },
517 	/* 9C  O              WRITE ATOMIC(16) */
518 	{ 0x9C, D, "WRITE ATOMIC(16)" },
519 	/* 9D                  SERVICE ACTION BIDIRECTIONAL */
520 	{ 0x9D, ALL, "SERVICE ACTION BIDIRECTIONAL" },
521 	/* XXX KDM ALL for this?  op-num.txt defines it for none.. */
522 	/* 9E                  SERVICE ACTION IN(16) */
523 	{ 0x9E, ALL, "SERVICE ACTION IN(16)" },
524 	/* 9F              M   SERVICE ACTION OUT(16) */
525 	{ 0x9F,	ALL, "SERVICE ACTION OUT(16)" },
526 	/* A0  MMOOO OMMM OMO  REPORT LUNS */
527 	{ 0xA0,	ALL & ~(R | B), "REPORT LUNS" },
528 	/* A1       O          BLANK */
529 	{ 0xA1,	R, "BLANK" },
530 	/* A1  O         O     ATA COMMAND PASS THROUGH(12) */
531 	{ 0xA1,	D | B, "ATA COMMAND PASS THROUGH(12)" },
532 	/* A2  OO   O      O   SECURITY PROTOCOL IN */
533 	{ 0xA2,	D | T | R | V, "SECURITY PROTOCOL IN" },
534 	/* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
535 	{ 0xA3,	ALL & ~(P | R | F), "MAINTENANCE (IN)" },
536 	/* A3       O          SEND KEY */
537 	{ 0xA3,	R, "SEND KEY" },
538 	/* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
539 	{ 0xA4,	ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
540 	/* A4       O          REPORT KEY */
541 	{ 0xA4,	R, "REPORT KEY" },
542 	/* A5   O  O OM        MOVE MEDIUM */
543 	{ 0xA5,	T | W | O | M, "MOVE MEDIUM" },
544 	/* A5       O          PLAY AUDIO(12) */
545 	{ 0xA5,	R, "PLAY AUDIO(12)" },
546 	/* A6         O        EXCHANGE MEDIUM */
547 	{ 0xA6,	M, "EXCHANGE MEDIUM" },
548 	/* A6       O          LOAD/UNLOAD C/DVD */
549 	{ 0xA6,	R, "LOAD/UNLOAD C/DVD" },
550 	/* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
551 	{ 0xA7,	D | T | W | O, "MOVE MEDIUM ATTACHED" },
552 	/* A7       O          SET READ AHEAD */
553 	{ 0xA7,	R, "SET READ AHEAD" },
554 	/* A8  O   OOO         READ(12) */
555 	{ 0xA8,	D | W | R | O, "READ(12)" },
556 	/* A8                  GET MESSAGE(12) */
557 	{ 0xA8, C, "GET MESSAGE(12)" },
558 	/* A9              O   SERVICE ACTION OUT(12) */
559 	{ 0xA9,	V, "SERVICE ACTION OUT(12)" },
560 	/* AA  O   OOO         WRITE(12) */
561 	{ 0xAA,	D | W | R | O, "WRITE(12)" },
562 	/* AA                  SEND MESSAGE(12) */
563 	{ 0xAA, C, "SEND MESSAGE(12)" },
564 	/* AB       O      O   SERVICE ACTION IN(12) */
565 	{ 0xAB,	R | V, "SERVICE ACTION IN(12)" },
566 	/* AC        O         ERASE(12) */
567 	{ 0xAC,	O, "ERASE(12)" },
568 	/* AC       O          GET PERFORMANCE */
569 	{ 0xAC,	R, "GET PERFORMANCE" },
570 	/* AD       O          READ DVD STRUCTURE */
571 	{ 0xAD,	R, "READ DVD STRUCTURE" },
572 	/* AE  O   O O         WRITE AND VERIFY(12) */
573 	{ 0xAE,	D | W | O, "WRITE AND VERIFY(12)" },
574 	/* AF  O   OZO         VERIFY(12) */
575 	{ 0xAF,	D | W | R | O, "VERIFY(12)" },
576 	/* B0      ZZZ         SEARCH DATA HIGH(12) */
577 	{ 0xB0,	W | R | O, "SEARCH DATA HIGH(12)" },
578 	/* B1      ZZZ         SEARCH DATA EQUAL(12) */
579 	{ 0xB1,	W | R | O, "SEARCH DATA EQUAL(12)" },
580 	/* B2      ZZZ         SEARCH DATA LOW(12) */
581 	{ 0xB2,	W | R | O, "SEARCH DATA LOW(12)" },
582 	/* B3  Z   OZO         SET LIMITS(12) */
583 	{ 0xB3,	D | W | R | O, "SET LIMITS(12)" },
584 	/* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
585 	{ 0xB4,	D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
586 	/* B5  OO   O      O   SECURITY PROTOCOL OUT */
587 	{ 0xB5,	D | T | R | V, "SECURITY PROTOCOL OUT" },
588 	/* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
589 	{ 0xB5,	M, "REQUEST VOLUME ELEMENT ADDRESS" },
590 	/* B6         O        SEND VOLUME TAG */
591 	{ 0xB6,	M, "SEND VOLUME TAG" },
592 	/* B6       O          SET STREAMING */
593 	{ 0xB6,	R, "SET STREAMING" },
594 	/* B7  O     O         READ DEFECT DATA(12) */
595 	{ 0xB7,	D | O, "READ DEFECT DATA(12)" },
596 	/* B8   O  OZOM        READ ELEMENT STATUS */
597 	{ 0xB8,	T | W | R | O | M, "READ ELEMENT STATUS" },
598 	/* B9       O          READ CD MSF */
599 	{ 0xB9,	R, "READ CD MSF" },
600 	/* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
601 	{ 0xBA,	D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
602 	/* BA       O          SCAN */
603 	{ 0xBA,	R, "SCAN" },
604 	/* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
605 	{ 0xBB,	D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
606 	/* BB       O          SET CD SPEED */
607 	{ 0xBB,	R, "SET CD SPEED" },
608 	/* BC  O   O OOMO      SPARE (IN) */
609 	{ 0xBC,	D | W | O | M | A | E, "SPARE (IN)" },
610 	/* BD  O   O OOOO      SPARE (OUT) */
611 	{ 0xBD,	D | W | O | M | A | E, "SPARE (OUT)" },
612 	/* BD       O          MECHANISM STATUS */
613 	{ 0xBD,	R, "MECHANISM STATUS" },
614 	/* BE  O   O OOMO      VOLUME SET (IN) */
615 	{ 0xBE,	D | W | O | M | A | E, "VOLUME SET (IN)" },
616 	/* BE       O          READ CD */
617 	{ 0xBE,	R, "READ CD" },
618 	/* BF  O   O OOOO      VOLUME SET (OUT) */
619 	{ 0xBF,	D | W | O | M | A | E, "VOLUME SET (OUT)" },
620 	/* BF       O          SEND DVD STRUCTURE */
621 	{ 0xBF,	R, "SEND DVD STRUCTURE" }
622 };
623 
624 const char *
625 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
626 {
627 	caddr_t match;
628 	int i, j;
629 	u_int32_t opmask;
630 	u_int16_t pd_type;
631 	int       num_ops[2];
632 	struct op_table_entry *table[2];
633 	int num_tables;
634 
635 	/*
636 	 * If we've got inquiry data, use it to determine what type of
637 	 * device we're dealing with here.  Otherwise, assume direct
638 	 * access.
639 	 */
640 	if (inq_data == NULL) {
641 		pd_type = T_DIRECT;
642 		match = NULL;
643 	} else {
644 		pd_type = SID_TYPE(inq_data);
645 
646 		match = cam_quirkmatch((caddr_t)inq_data,
647 				       (caddr_t)scsi_op_quirk_table,
648 				       nitems(scsi_op_quirk_table),
649 				       sizeof(*scsi_op_quirk_table),
650 				       scsi_inquiry_match);
651 	}
652 
653 	if (match != NULL) {
654 		table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
655 		num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
656 		table[1] = scsi_op_codes;
657 		num_ops[1] = nitems(scsi_op_codes);
658 		num_tables = 2;
659 	} else {
660 		/*
661 		 * If this is true, we have a vendor specific opcode that
662 		 * wasn't covered in the quirk table.
663 		 */
664 		if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
665 			return("Vendor Specific Command");
666 
667 		table[0] = scsi_op_codes;
668 		num_ops[0] = nitems(scsi_op_codes);
669 		num_tables = 1;
670 	}
671 
672 	/* RBC is 'Simplified' Direct Access Device */
673 	if (pd_type == T_RBC)
674 		pd_type = T_DIRECT;
675 
676 	/*
677 	 * Host managed drives are direct access for the most part.
678 	 */
679 	if (pd_type == T_ZBC_HM)
680 		pd_type = T_DIRECT;
681 
682 	/* Map NODEVICE to Direct Access Device to handle REPORT LUNS, etc. */
683 	if (pd_type == T_NODEVICE)
684 		pd_type = T_DIRECT;
685 
686 	opmask = 1 << pd_type;
687 
688 	for (j = 0; j < num_tables; j++) {
689 		for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
690 			if ((table[j][i].opcode == opcode)
691 			 && ((table[j][i].opmask & opmask) != 0))
692 				return(table[j][i].desc);
693 		}
694 	}
695 
696 	/*
697 	 * If we can't find a match for the command in the table, we just
698 	 * assume it's a vendor specifc command.
699 	 */
700 	return("Vendor Specific Command");
701 
702 }
703 
704 #else /* SCSI_NO_OP_STRINGS */
705 
706 const char *
707 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
708 {
709 	return("");
710 }
711 
712 #endif
713 
714 
715 #if !defined(SCSI_NO_SENSE_STRINGS)
716 #define SST(asc, ascq, action, desc) \
717 	asc, ascq, action, desc
718 #else
719 const char empty_string[] = "";
720 
721 #define SST(asc, ascq, action, desc) \
722 	asc, ascq, action, empty_string
723 #endif
724 
725 const struct sense_key_table_entry sense_key_table[] =
726 {
727 	{ SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
728 	{ SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
729 	{ SSD_KEY_NOT_READY, SS_RDEF, "NOT READY" },
730 	{ SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
731 	{ SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
732 	{ SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
733 	{ SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
734 	{ SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
735 	{ SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
736 	{ SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
737 	{ SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
738 	{ SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
739 	{ SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
740 	{ SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
741 	{ SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
742 	{ SSD_KEY_COMPLETED, SS_NOP, "COMPLETED" }
743 };
744 
745 static struct asc_table_entry quantum_fireball_entries[] = {
746 	{ SST(0x04, 0x0b, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
747 	     "Logical unit not ready, initializing cmd. required") }
748 };
749 
750 static struct asc_table_entry sony_mo_entries[] = {
751 	{ SST(0x04, 0x00, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
752 	     "Logical unit not ready, cause not reportable") }
753 };
754 
755 static struct asc_table_entry hgst_entries[] = {
756 	{ SST(0x04, 0xF0, SS_RDEF,
757 	    "Vendor Unique - Logical Unit Not Ready") },
758 	{ SST(0x0A, 0x01, SS_RDEF,
759 	    "Unrecovered Super Certification Log Write Error") },
760 	{ SST(0x0A, 0x02, SS_RDEF,
761 	    "Unrecovered Super Certification Log Read Error") },
762 	{ SST(0x15, 0x03, SS_RDEF,
763 	    "Unrecovered Sector Error") },
764 	{ SST(0x3E, 0x04, SS_RDEF,
765 	    "Unrecovered Self-Test Hard-Cache Test Fail") },
766 	{ SST(0x3E, 0x05, SS_RDEF,
767 	    "Unrecovered Self-Test OTF-Cache Fail") },
768 	{ SST(0x40, 0x00, SS_RDEF,
769 	    "Unrecovered SAT No Buffer Overflow Error") },
770 	{ SST(0x40, 0x01, SS_RDEF,
771 	    "Unrecovered SAT Buffer Overflow Error") },
772 	{ SST(0x40, 0x02, SS_RDEF,
773 	    "Unrecovered SAT No Buffer Overflow With ECS Fault") },
774 	{ SST(0x40, 0x03, SS_RDEF,
775 	    "Unrecovered SAT Buffer Overflow With ECS Fault") },
776 	{ SST(0x40, 0x81, SS_RDEF,
777 	    "DRAM Failure") },
778 	{ SST(0x44, 0x0B, SS_RDEF,
779 	    "Vendor Unique - Internal Target Failure") },
780 	{ SST(0x44, 0xF2, SS_RDEF,
781 	    "Vendor Unique - Internal Target Failure") },
782 	{ SST(0x44, 0xF6, SS_RDEF,
783 	    "Vendor Unique - Internal Target Failure") },
784 	{ SST(0x44, 0xF9, SS_RDEF,
785 	    "Vendor Unique - Internal Target Failure") },
786 	{ SST(0x44, 0xFA, SS_RDEF,
787 	    "Vendor Unique - Internal Target Failure") },
788 	{ SST(0x5D, 0x22, SS_RDEF,
789 	    "Extreme Over-Temperature Warning") },
790 	{ SST(0x5D, 0x50, SS_RDEF,
791 	    "Load/Unload cycle Count Warning") },
792 	{ SST(0x81, 0x00, SS_RDEF,
793 	    "Vendor Unique - Internal Logic Error") },
794 	{ SST(0x85, 0x00, SS_RDEF,
795 	    "Vendor Unique - Internal Key Seed Error") },
796 };
797 
798 static struct asc_table_entry seagate_entries[] = {
799 	{ SST(0x04, 0xF0, SS_RDEF,
800 	    "Logical Unit Not Ready, super certify in Progress") },
801 	{ SST(0x08, 0x86, SS_RDEF,
802 	    "Write Fault Data Corruption") },
803 	{ SST(0x09, 0x0D, SS_RDEF,
804 	    "Tracking Failure") },
805 	{ SST(0x09, 0x0E, SS_RDEF,
806 	    "ETF Failure") },
807 	{ SST(0x0B, 0x5D, SS_RDEF,
808 	    "Pre-SMART Warning") },
809 	{ SST(0x0B, 0x85, SS_RDEF,
810 	    "5V Voltage Warning") },
811 	{ SST(0x0B, 0x8C, SS_RDEF,
812 	    "12V Voltage Warning") },
813 	{ SST(0x0C, 0xFF, SS_RDEF,
814 	    "Write Error - Too many error recovery revs") },
815 	{ SST(0x11, 0xFF, SS_RDEF,
816 	    "Unrecovered Read Error - Too many error recovery revs") },
817 	{ SST(0x19, 0x0E, SS_RDEF,
818 	    "Fewer than 1/2 defect list copies") },
819 	{ SST(0x20, 0xF3, SS_RDEF,
820 	    "Illegal CDB linked to skip mask cmd") },
821 	{ SST(0x24, 0xF0, SS_RDEF,
822 	    "Illegal byte in CDB, LBA not matching") },
823 	{ SST(0x24, 0xF1, SS_RDEF,
824 	    "Illegal byte in CDB, LEN not matching") },
825 	{ SST(0x24, 0xF2, SS_RDEF,
826 	    "Mask not matching transfer length") },
827 	{ SST(0x24, 0xF3, SS_RDEF,
828 	    "Drive formatted without plist") },
829 	{ SST(0x26, 0x95, SS_RDEF,
830 	    "Invalid Field Parameter - CAP File") },
831 	{ SST(0x26, 0x96, SS_RDEF,
832 	    "Invalid Field Parameter - RAP File") },
833 	{ SST(0x26, 0x97, SS_RDEF,
834 	    "Invalid Field Parameter - TMS Firmware Tag") },
835 	{ SST(0x26, 0x98, SS_RDEF,
836 	    "Invalid Field Parameter - Check Sum") },
837 	{ SST(0x26, 0x99, SS_RDEF,
838 	    "Invalid Field Parameter - Firmware Tag") },
839 	{ SST(0x29, 0x08, SS_RDEF,
840 	    "Write Log Dump data") },
841 	{ SST(0x29, 0x09, SS_RDEF,
842 	    "Write Log Dump data") },
843 	{ SST(0x29, 0x0A, SS_RDEF,
844 	    "Reserved disk space") },
845 	{ SST(0x29, 0x0B, SS_RDEF,
846 	    "SDBP") },
847 	{ SST(0x29, 0x0C, SS_RDEF,
848 	    "SDBP") },
849 	{ SST(0x31, 0x91, SS_RDEF,
850 	    "Format Corrupted World Wide Name (WWN) is Invalid") },
851 	{ SST(0x32, 0x03, SS_RDEF,
852 	    "Defect List - Length exceeds Command Allocated Length") },
853 	{ SST(0x33, 0x00, SS_RDEF,
854 	    "Flash not ready for access") },
855 	{ SST(0x3F, 0x70, SS_RDEF,
856 	    "Invalid RAP block") },
857 	{ SST(0x3F, 0x71, SS_RDEF,
858 	    "RAP/ETF mismatch") },
859 	{ SST(0x3F, 0x90, SS_RDEF,
860 	    "Invalid CAP block") },
861 	{ SST(0x3F, 0x91, SS_RDEF,
862 	    "World Wide Name (WWN) Mismatch") },
863 	{ SST(0x40, 0x01, SS_RDEF,
864 	    "DRAM Parity Error") },
865 	{ SST(0x40, 0x02, SS_RDEF,
866 	    "DRAM Parity Error") },
867 	{ SST(0x42, 0x0A, SS_RDEF,
868 	    "Loopback Test") },
869 	{ SST(0x42, 0x0B, SS_RDEF,
870 	    "Loopback Test") },
871 	{ SST(0x44, 0xF2, SS_RDEF,
872 	    "Compare error during data integrity check") },
873 	{ SST(0x44, 0xF6, SS_RDEF,
874 	    "Unrecoverable error during data integrity check") },
875 	{ SST(0x47, 0x80, SS_RDEF,
876 	    "Fibre Channel Sequence Error") },
877 	{ SST(0x4E, 0x01, SS_RDEF,
878 	    "Information Unit Too Short") },
879 	{ SST(0x80, 0x00, SS_RDEF,
880 	    "General Firmware Error / Command Timeout") },
881 	{ SST(0x80, 0x01, SS_RDEF,
882 	    "Command Timeout") },
883 	{ SST(0x80, 0x02, SS_RDEF,
884 	    "Command Timeout") },
885 	{ SST(0x80, 0x80, SS_RDEF,
886 	    "FC FIFO Error During Read Transfer") },
887 	{ SST(0x80, 0x81, SS_RDEF,
888 	    "FC FIFO Error During Write Transfer") },
889 	{ SST(0x80, 0x82, SS_RDEF,
890 	    "DISC FIFO Error During Read Transfer") },
891 	{ SST(0x80, 0x83, SS_RDEF,
892 	    "DISC FIFO Error During Write Transfer") },
893 	{ SST(0x80, 0x84, SS_RDEF,
894 	    "LBA Seeded LRC Error on Read") },
895 	{ SST(0x80, 0x85, SS_RDEF,
896 	    "LBA Seeded LRC Error on Write") },
897 	{ SST(0x80, 0x86, SS_RDEF,
898 	    "IOEDC Error on Read") },
899 	{ SST(0x80, 0x87, SS_RDEF,
900 	    "IOEDC Error on Write") },
901 	{ SST(0x80, 0x88, SS_RDEF,
902 	    "Host Parity Check Failed") },
903 	{ SST(0x80, 0x89, SS_RDEF,
904 	    "IOEDC error on read detected by formatter") },
905 	{ SST(0x80, 0x8A, SS_RDEF,
906 	    "Host Parity Errors / Host FIFO Initialization Failed") },
907 	{ SST(0x80, 0x8B, SS_RDEF,
908 	    "Host Parity Errors") },
909 	{ SST(0x80, 0x8C, SS_RDEF,
910 	    "Host Parity Errors") },
911 	{ SST(0x80, 0x8D, SS_RDEF,
912 	    "Host Parity Errors") },
913 	{ SST(0x81, 0x00, SS_RDEF,
914 	    "LA Check Failed") },
915 	{ SST(0x82, 0x00, SS_RDEF,
916 	    "Internal client detected insufficient buffer") },
917 	{ SST(0x84, 0x00, SS_RDEF,
918 	    "Scheduled Diagnostic And Repair") },
919 };
920 
921 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
922 	{
923 		/*
924 		 * XXX The Quantum Fireball ST and SE like to return 0x04 0x0b
925 		 * when they really should return 0x04 0x02.
926 		 */
927 		{T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
928 		/*num_sense_keys*/0,
929 		nitems(quantum_fireball_entries),
930 		/*sense key entries*/NULL,
931 		quantum_fireball_entries
932 	},
933 	{
934 		/*
935 		 * This Sony MO drive likes to return 0x04, 0x00 when it
936 		 * isn't spun up.
937 		 */
938 		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
939 		/*num_sense_keys*/0,
940 		nitems(sony_mo_entries),
941 		/*sense key entries*/NULL,
942 		sony_mo_entries
943 	},
944 	{
945 		/*
946 		 * HGST vendor-specific error codes
947 		 */
948 		{T_DIRECT, SIP_MEDIA_FIXED, "HGST", "*", "*"},
949 		/*num_sense_keys*/0,
950 		nitems(hgst_entries),
951 		/*sense key entries*/NULL,
952 		hgst_entries
953 	},
954 	{
955 		/*
956 		 * SEAGATE vendor-specific error codes
957 		 */
958 		{T_DIRECT, SIP_MEDIA_FIXED, "SEAGATE", "*", "*"},
959 		/*num_sense_keys*/0,
960 		nitems(seagate_entries),
961 		/*sense key entries*/NULL,
962 		seagate_entries
963 	}
964 };
965 
966 const u_int sense_quirk_table_size = nitems(sense_quirk_table);
967 
968 static struct asc_table_entry asc_table[] = {
969 	/*
970 	 * From: http://www.t10.org/lists/asc-num.txt
971 	 * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
972 	 */
973 	/*
974 	 * File: ASC-NUM.TXT
975 	 *
976 	 * SCSI ASC/ASCQ Assignments
977 	 * Numeric Sorted Listing
978 	 * as of  8/12/15
979 	 *
980 	 * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
981 	 * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
982 	 * . L - PRINTER DEVICE (SSC)                           blank = reserved
983 	 * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
984 	 * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
985 	 * .  . R - CD DEVICE (MMC)
986 	 * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
987 	 * .  .  .M - MEDIA CHANGER DEVICE (SMC)
988 	 * .  .  . A - STORAGE ARRAY DEVICE (SCC)
989 	 * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
990 	 * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
991 	 * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
992 	 * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
993 	 * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
994 	 * DTLPWROMAEBKVF
995 	 * ASC      ASCQ  Action
996 	 * Description
997 	 */
998 	/* DTLPWROMAEBKVF */
999 	{ SST(0x00, 0x00, SS_NOP,
1000 	    "No additional sense information") },
1001 	/*  T             */
1002 	{ SST(0x00, 0x01, SS_RDEF,
1003 	    "Filemark detected") },
1004 	/*  T             */
1005 	{ SST(0x00, 0x02, SS_RDEF,
1006 	    "End-of-partition/medium detected") },
1007 	/*  T             */
1008 	{ SST(0x00, 0x03, SS_RDEF,
1009 	    "Setmark detected") },
1010 	/*  T             */
1011 	{ SST(0x00, 0x04, SS_RDEF,
1012 	    "Beginning-of-partition/medium detected") },
1013 	/*  TL            */
1014 	{ SST(0x00, 0x05, SS_RDEF,
1015 	    "End-of-data detected") },
1016 	/* DTLPWROMAEBKVF */
1017 	{ SST(0x00, 0x06, SS_RDEF,
1018 	    "I/O process terminated") },
1019 	/*  T             */
1020 	{ SST(0x00, 0x07, SS_RDEF,	/* XXX TBD */
1021 	    "Programmable early warning detected") },
1022 	/*      R         */
1023 	{ SST(0x00, 0x11, SS_FATAL | EBUSY,
1024 	    "Audio play operation in progress") },
1025 	/*      R         */
1026 	{ SST(0x00, 0x12, SS_NOP,
1027 	    "Audio play operation paused") },
1028 	/*      R         */
1029 	{ SST(0x00, 0x13, SS_NOP,
1030 	    "Audio play operation successfully completed") },
1031 	/*      R         */
1032 	{ SST(0x00, 0x14, SS_RDEF,
1033 	    "Audio play operation stopped due to error") },
1034 	/*      R         */
1035 	{ SST(0x00, 0x15, SS_NOP,
1036 	    "No current audio status to return") },
1037 	/* DTLPWROMAEBKVF */
1038 	{ SST(0x00, 0x16, SS_FATAL | EBUSY,
1039 	    "Operation in progress") },
1040 	/* DTL WROMAEBKVF */
1041 	{ SST(0x00, 0x17, SS_RDEF,
1042 	    "Cleaning requested") },
1043 	/*  T             */
1044 	{ SST(0x00, 0x18, SS_RDEF,	/* XXX TBD */
1045 	    "Erase operation in progress") },
1046 	/*  T             */
1047 	{ SST(0x00, 0x19, SS_RDEF,	/* XXX TBD */
1048 	    "Locate operation in progress") },
1049 	/*  T             */
1050 	{ SST(0x00, 0x1A, SS_RDEF,	/* XXX TBD */
1051 	    "Rewind operation in progress") },
1052 	/*  T             */
1053 	{ SST(0x00, 0x1B, SS_RDEF,	/* XXX TBD */
1054 	    "Set capacity operation in progress") },
1055 	/*  T             */
1056 	{ SST(0x00, 0x1C, SS_RDEF,	/* XXX TBD */
1057 	    "Verify operation in progress") },
1058 	/* DT        B    */
1059 	{ SST(0x00, 0x1D, SS_RDEF,	/* XXX TBD */
1060 	    "ATA pass through information available") },
1061 	/* DT   R MAEBKV  */
1062 	{ SST(0x00, 0x1E, SS_RDEF,	/* XXX TBD */
1063 	    "Conflicting SA creation request") },
1064 	/* DT        B    */
1065 	{ SST(0x00, 0x1F, SS_RDEF,	/* XXX TBD */
1066 	    "Logical unit transitioning to another power condition") },
1067 	/* DT P      B    */
1068 	{ SST(0x00, 0x20, SS_RDEF,	/* XXX TBD */
1069 	    "Extended copy information available") },
1070 	/* D              */
1071 	{ SST(0x00, 0x21, SS_RDEF,	/* XXX TBD */
1072 	    "Atomic command aborted due to ACA") },
1073 	/* D   W O   BK   */
1074 	{ SST(0x01, 0x00, SS_RDEF,
1075 	    "No index/sector signal") },
1076 	/* D   WRO   BK   */
1077 	{ SST(0x02, 0x00, SS_RDEF,
1078 	    "No seek complete") },
1079 	/* DTL W O   BK   */
1080 	{ SST(0x03, 0x00, SS_RDEF,
1081 	    "Peripheral device write fault") },
1082 	/*  T             */
1083 	{ SST(0x03, 0x01, SS_RDEF,
1084 	    "No write current") },
1085 	/*  T             */
1086 	{ SST(0x03, 0x02, SS_RDEF,
1087 	    "Excessive write errors") },
1088 	/* DTLPWROMAEBKVF */
1089 	{ SST(0x04, 0x00, SS_RDEF,
1090 	    "Logical unit not ready, cause not reportable") },
1091 	/* DTLPWROMAEBKVF */
1092 	{ SST(0x04, 0x01, SS_WAIT | EBUSY,
1093 	    "Logical unit is in process of becoming ready") },
1094 	/* DTLPWROMAEBKVF */
1095 	{ SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
1096 	    "Logical unit not ready, initializing command required") },
1097 	/* DTLPWROMAEBKVF */
1098 	{ SST(0x04, 0x03, SS_FATAL | ENXIO,
1099 	    "Logical unit not ready, manual intervention required") },
1100 	/* DTL  RO   B    */
1101 	{ SST(0x04, 0x04, SS_FATAL | EBUSY,
1102 	    "Logical unit not ready, format in progress") },
1103 	/* DT  W O A BK F */
1104 	{ SST(0x04, 0x05, SS_FATAL | EBUSY,
1105 	    "Logical unit not ready, rebuild in progress") },
1106 	/* DT  W O A BK   */
1107 	{ SST(0x04, 0x06, SS_FATAL | EBUSY,
1108 	    "Logical unit not ready, recalculation in progress") },
1109 	/* DTLPWROMAEBKVF */
1110 	{ SST(0x04, 0x07, SS_FATAL | EBUSY,
1111 	    "Logical unit not ready, operation in progress") },
1112 	/*      R         */
1113 	{ SST(0x04, 0x08, SS_FATAL | EBUSY,
1114 	    "Logical unit not ready, long write in progress") },
1115 	/* DTLPWROMAEBKVF */
1116 	{ SST(0x04, 0x09, SS_RDEF,	/* XXX TBD */
1117 	    "Logical unit not ready, self-test in progress") },
1118 	/* DTLPWROMAEBKVF */
1119 	{ SST(0x04, 0x0A, SS_WAIT | ENXIO,
1120 	    "Logical unit not accessible, asymmetric access state transition")},
1121 	/* DTLPWROMAEBKVF */
1122 	{ SST(0x04, 0x0B, SS_FATAL | ENXIO,
1123 	    "Logical unit not accessible, target port in standby state") },
1124 	/* DTLPWROMAEBKVF */
1125 	{ SST(0x04, 0x0C, SS_FATAL | ENXIO,
1126 	    "Logical unit not accessible, target port in unavailable state") },
1127 	/*              F */
1128 	{ SST(0x04, 0x0D, SS_RDEF,	/* XXX TBD */
1129 	    "Logical unit not ready, structure check required") },
1130 	/* DTL WR MAEBKVF */
1131 	{ SST(0x04, 0x0E, SS_RDEF,	/* XXX TBD */
1132 	    "Logical unit not ready, security session in progress") },
1133 	/* DT  WROM  B    */
1134 	{ SST(0x04, 0x10, SS_RDEF,	/* XXX TBD */
1135 	    "Logical unit not ready, auxiliary memory not accessible") },
1136 	/* DT  WRO AEB VF */
1137 	{ SST(0x04, 0x11, SS_WAIT | EBUSY,
1138 	    "Logical unit not ready, notify (enable spinup) required") },
1139 	/*        M    V  */
1140 	{ SST(0x04, 0x12, SS_RDEF,	/* XXX TBD */
1141 	    "Logical unit not ready, offline") },
1142 	/* DT   R MAEBKV  */
1143 	{ SST(0x04, 0x13, SS_RDEF,	/* XXX TBD */
1144 	    "Logical unit not ready, SA creation in progress") },
1145 	/* D         B    */
1146 	{ SST(0x04, 0x14, SS_RDEF,	/* XXX TBD */
1147 	    "Logical unit not ready, space allocation in progress") },
1148 	/*        M       */
1149 	{ SST(0x04, 0x15, SS_RDEF,	/* XXX TBD */
1150 	    "Logical unit not ready, robotics disabled") },
1151 	/*        M       */
1152 	{ SST(0x04, 0x16, SS_RDEF,	/* XXX TBD */
1153 	    "Logical unit not ready, configuration required") },
1154 	/*        M       */
1155 	{ SST(0x04, 0x17, SS_RDEF,	/* XXX TBD */
1156 	    "Logical unit not ready, calibration required") },
1157 	/*        M       */
1158 	{ SST(0x04, 0x18, SS_RDEF,	/* XXX TBD */
1159 	    "Logical unit not ready, a door is open") },
1160 	/*        M       */
1161 	{ SST(0x04, 0x19, SS_RDEF,	/* XXX TBD */
1162 	    "Logical unit not ready, operating in sequential mode") },
1163 	/* DT        B    */
1164 	{ SST(0x04, 0x1A, SS_RDEF,	/* XXX TBD */
1165 	    "Logical unit not ready, START/STOP UNIT command in progress") },
1166 	/* D         B    */
1167 	{ SST(0x04, 0x1B, SS_RDEF,	/* XXX TBD */
1168 	    "Logical unit not ready, sanitize in progress") },
1169 	/* DT     MAEB    */
1170 	{ SST(0x04, 0x1C, SS_RDEF,	/* XXX TBD */
1171 	    "Logical unit not ready, additional power use not yet granted") },
1172 	/* D              */
1173 	{ SST(0x04, 0x1D, SS_RDEF,	/* XXX TBD */
1174 	    "Logical unit not ready, configuration in progress") },
1175 	/* D              */
1176 	{ SST(0x04, 0x1E, SS_FATAL | ENXIO,
1177 	    "Logical unit not ready, microcode activation required") },
1178 	/* DTLPWROMAEBKVF */
1179 	{ SST(0x04, 0x1F, SS_FATAL | ENXIO,
1180 	    "Logical unit not ready, microcode download required") },
1181 	/* DTLPWROMAEBKVF */
1182 	{ SST(0x04, 0x20, SS_RDEF,	/* XXX TBD */
1183 	    "Logical unit not ready, logical unit reset required") },
1184 	/* DTLPWROMAEBKVF */
1185 	{ SST(0x04, 0x21, SS_RDEF,	/* XXX TBD */
1186 	    "Logical unit not ready, hard reset required") },
1187 	/* DTLPWROMAEBKVF */
1188 	{ SST(0x04, 0x22, SS_RDEF,	/* XXX TBD */
1189 	    "Logical unit not ready, power cycle required") },
1190 	/* DTL WROMAEBKVF */
1191 	{ SST(0x05, 0x00, SS_RDEF,
1192 	    "Logical unit does not respond to selection") },
1193 	/* D   WROM  BK   */
1194 	{ SST(0x06, 0x00, SS_RDEF,
1195 	    "No reference position found") },
1196 	/* DTL WROM  BK   */
1197 	{ SST(0x07, 0x00, SS_RDEF,
1198 	    "Multiple peripheral devices selected") },
1199 	/* DTL WROMAEBKVF */
1200 	{ SST(0x08, 0x00, SS_RDEF,
1201 	    "Logical unit communication failure") },
1202 	/* DTL WROMAEBKVF */
1203 	{ SST(0x08, 0x01, SS_RDEF,
1204 	    "Logical unit communication time-out") },
1205 	/* DTL WROMAEBKVF */
1206 	{ SST(0x08, 0x02, SS_RDEF,
1207 	    "Logical unit communication parity error") },
1208 	/* DT   ROM  BK   */
1209 	{ SST(0x08, 0x03, SS_RDEF,
1210 	    "Logical unit communication CRC error (Ultra-DMA/32)") },
1211 	/* DTLPWRO    K   */
1212 	{ SST(0x08, 0x04, SS_RDEF,	/* XXX TBD */
1213 	    "Unreachable copy target") },
1214 	/* DT  WRO   B    */
1215 	{ SST(0x09, 0x00, SS_RDEF,
1216 	    "Track following error") },
1217 	/*     WRO    K   */
1218 	{ SST(0x09, 0x01, SS_RDEF,
1219 	    "Tracking servo failure") },
1220 	/*     WRO    K   */
1221 	{ SST(0x09, 0x02, SS_RDEF,
1222 	    "Focus servo failure") },
1223 	/*     WRO        */
1224 	{ SST(0x09, 0x03, SS_RDEF,
1225 	    "Spindle servo failure") },
1226 	/* DT  WRO   B    */
1227 	{ SST(0x09, 0x04, SS_RDEF,
1228 	    "Head select fault") },
1229 	/* DT   RO   B    */
1230 	{ SST(0x09, 0x05, SS_RDEF,
1231 	    "Vibration induced tracking error") },
1232 	/* DTLPWROMAEBKVF */
1233 	{ SST(0x0A, 0x00, SS_FATAL | ENOSPC,
1234 	    "Error log overflow") },
1235 	/* DTLPWROMAEBKVF */
1236 	{ SST(0x0B, 0x00, SS_RDEF,
1237 	    "Warning") },
1238 	/* DTLPWROMAEBKVF */
1239 	{ SST(0x0B, 0x01, SS_RDEF,
1240 	    "Warning - specified temperature exceeded") },
1241 	/* DTLPWROMAEBKVF */
1242 	{ SST(0x0B, 0x02, SS_RDEF,
1243 	    "Warning - enclosure degraded") },
1244 	/* DTLPWROMAEBKVF */
1245 	{ SST(0x0B, 0x03, SS_RDEF,	/* XXX TBD */
1246 	    "Warning - background self-test failed") },
1247 	/* DTLPWRO AEBKVF */
1248 	{ SST(0x0B, 0x04, SS_RDEF,	/* XXX TBD */
1249 	    "Warning - background pre-scan detected medium error") },
1250 	/* DTLPWRO AEBKVF */
1251 	{ SST(0x0B, 0x05, SS_RDEF,	/* XXX TBD */
1252 	    "Warning - background medium scan detected medium error") },
1253 	/* DTLPWROMAEBKVF */
1254 	{ SST(0x0B, 0x06, SS_RDEF,	/* XXX TBD */
1255 	    "Warning - non-volatile cache now volatile") },
1256 	/* DTLPWROMAEBKVF */
1257 	{ SST(0x0B, 0x07, SS_RDEF,	/* XXX TBD */
1258 	    "Warning - degraded power to non-volatile cache") },
1259 	/* DTLPWROMAEBKVF */
1260 	{ SST(0x0B, 0x08, SS_RDEF,	/* XXX TBD */
1261 	    "Warning - power loss expected") },
1262 	/* D              */
1263 	{ SST(0x0B, 0x09, SS_RDEF,	/* XXX TBD */
1264 	    "Warning - device statistics notification available") },
1265 	/* DTLPWROMAEBKVF */
1266 	{ SST(0x0B, 0x0A, SS_RDEF,	/* XXX TBD */
1267 	    "Warning - High critical temperature limit exceeded") },
1268 	/* DTLPWROMAEBKVF */
1269 	{ SST(0x0B, 0x0B, SS_RDEF,	/* XXX TBD */
1270 	    "Warning - Low critical temperature limit exceeded") },
1271 	/* DTLPWROMAEBKVF */
1272 	{ SST(0x0B, 0x0C, SS_RDEF,	/* XXX TBD */
1273 	    "Warning - High operating temperature limit exceeded") },
1274 	/* DTLPWROMAEBKVF */
1275 	{ SST(0x0B, 0x0D, SS_RDEF,	/* XXX TBD */
1276 	    "Warning - Low operating temperature limit exceeded") },
1277 	/* DTLPWROMAEBKVF */
1278 	{ SST(0x0B, 0x0E, SS_RDEF,	/* XXX TBD */
1279 	    "Warning - High citical humidity limit exceeded") },
1280 	/* DTLPWROMAEBKVF */
1281 	{ SST(0x0B, 0x0F, SS_RDEF,	/* XXX TBD */
1282 	    "Warning - Low citical humidity limit exceeded") },
1283 	/* DTLPWROMAEBKVF */
1284 	{ SST(0x0B, 0x10, SS_RDEF,	/* XXX TBD */
1285 	    "Warning - High operating humidity limit exceeded") },
1286 	/* DTLPWROMAEBKVF */
1287 	{ SST(0x0B, 0x11, SS_RDEF,	/* XXX TBD */
1288 	    "Warning - Low operating humidity limit exceeded") },
1289 	/*  T   R         */
1290 	{ SST(0x0C, 0x00, SS_RDEF,
1291 	    "Write error") },
1292 	/*            K   */
1293 	{ SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1294 	    "Write error - recovered with auto reallocation") },
1295 	/* D   W O   BK   */
1296 	{ SST(0x0C, 0x02, SS_RDEF,
1297 	    "Write error - auto reallocation failed") },
1298 	/* D   W O   BK   */
1299 	{ SST(0x0C, 0x03, SS_RDEF,
1300 	    "Write error - recommend reassignment") },
1301 	/* DT  W O   B    */
1302 	{ SST(0x0C, 0x04, SS_RDEF,
1303 	    "Compression check miscompare error") },
1304 	/* DT  W O   B    */
1305 	{ SST(0x0C, 0x05, SS_RDEF,
1306 	    "Data expansion occurred during compression") },
1307 	/* DT  W O   B    */
1308 	{ SST(0x0C, 0x06, SS_RDEF,
1309 	    "Block not compressible") },
1310 	/*      R         */
1311 	{ SST(0x0C, 0x07, SS_RDEF,
1312 	    "Write error - recovery needed") },
1313 	/*      R         */
1314 	{ SST(0x0C, 0x08, SS_RDEF,
1315 	    "Write error - recovery failed") },
1316 	/*      R         */
1317 	{ SST(0x0C, 0x09, SS_RDEF,
1318 	    "Write error - loss of streaming") },
1319 	/*      R         */
1320 	{ SST(0x0C, 0x0A, SS_RDEF,
1321 	    "Write error - padding blocks added") },
1322 	/* DT  WROM  B    */
1323 	{ SST(0x0C, 0x0B, SS_RDEF,	/* XXX TBD */
1324 	    "Auxiliary memory write error") },
1325 	/* DTLPWRO AEBKVF */
1326 	{ SST(0x0C, 0x0C, SS_RDEF,	/* XXX TBD */
1327 	    "Write error - unexpected unsolicited data") },
1328 	/* DTLPWRO AEBKVF */
1329 	{ SST(0x0C, 0x0D, SS_RDEF,	/* XXX TBD */
1330 	    "Write error - not enough unsolicited data") },
1331 	/* DT  W O   BK   */
1332 	{ SST(0x0C, 0x0E, SS_RDEF,	/* XXX TBD */
1333 	    "Multiple write errors") },
1334 	/*      R         */
1335 	{ SST(0x0C, 0x0F, SS_RDEF,	/* XXX TBD */
1336 	    "Defects in error window") },
1337 	/* D              */
1338 	{ SST(0x0C, 0x10, SS_RDEF,	/* XXX TBD */
1339 	    "Incomplete multiple atomic write operations") },
1340 	/* D              */
1341 	{ SST(0x0C, 0x11, SS_RDEF,	/* XXX TBD */
1342 	    "Write error - recovery scan needed") },
1343 	/* D              */
1344 	{ SST(0x0C, 0x12, SS_RDEF,	/* XXX TBD */
1345 	    "Write error - insufficient zone resources") },
1346 	/* DTLPWRO A  K   */
1347 	{ SST(0x0D, 0x00, SS_RDEF,	/* XXX TBD */
1348 	    "Error detected by third party temporary initiator") },
1349 	/* DTLPWRO A  K   */
1350 	{ SST(0x0D, 0x01, SS_RDEF,	/* XXX TBD */
1351 	    "Third party device failure") },
1352 	/* DTLPWRO A  K   */
1353 	{ SST(0x0D, 0x02, SS_RDEF,	/* XXX TBD */
1354 	    "Copy target device not reachable") },
1355 	/* DTLPWRO A  K   */
1356 	{ SST(0x0D, 0x03, SS_RDEF,	/* XXX TBD */
1357 	    "Incorrect copy target device type") },
1358 	/* DTLPWRO A  K   */
1359 	{ SST(0x0D, 0x04, SS_RDEF,	/* XXX TBD */
1360 	    "Copy target device data underrun") },
1361 	/* DTLPWRO A  K   */
1362 	{ SST(0x0D, 0x05, SS_RDEF,	/* XXX TBD */
1363 	    "Copy target device data overrun") },
1364 	/* DT PWROMAEBK F */
1365 	{ SST(0x0E, 0x00, SS_RDEF,	/* XXX TBD */
1366 	    "Invalid information unit") },
1367 	/* DT PWROMAEBK F */
1368 	{ SST(0x0E, 0x01, SS_RDEF,	/* XXX TBD */
1369 	    "Information unit too short") },
1370 	/* DT PWROMAEBK F */
1371 	{ SST(0x0E, 0x02, SS_RDEF,	/* XXX TBD */
1372 	    "Information unit too long") },
1373 	/* DT P R MAEBK F */
1374 	{ SST(0x0E, 0x03, SS_RDEF,	/* XXX TBD */
1375 	    "Invalid field in command information unit") },
1376 	/* D   W O   BK   */
1377 	{ SST(0x10, 0x00, SS_RDEF,
1378 	    "ID CRC or ECC error") },
1379 	/* DT  W O        */
1380 	{ SST(0x10, 0x01, SS_RDEF,	/* XXX TBD */
1381 	    "Logical block guard check failed") },
1382 	/* DT  W O        */
1383 	{ SST(0x10, 0x02, SS_RDEF,	/* XXX TBD */
1384 	    "Logical block application tag check failed") },
1385 	/* DT  W O        */
1386 	{ SST(0x10, 0x03, SS_RDEF,	/* XXX TBD */
1387 	    "Logical block reference tag check failed") },
1388 	/*  T             */
1389 	{ SST(0x10, 0x04, SS_RDEF,	/* XXX TBD */
1390 	    "Logical block protection error on recovered buffer data") },
1391 	/*  T             */
1392 	{ SST(0x10, 0x05, SS_RDEF,	/* XXX TBD */
1393 	    "Logical block protection method error") },
1394 	/* DT  WRO   BK   */
1395 	{ SST(0x11, 0x00, SS_FATAL|EIO,
1396 	    "Unrecovered read error") },
1397 	/* DT  WRO   BK   */
1398 	{ SST(0x11, 0x01, SS_FATAL|EIO,
1399 	    "Read retries exhausted") },
1400 	/* DT  WRO   BK   */
1401 	{ SST(0x11, 0x02, SS_FATAL|EIO,
1402 	    "Error too long to correct") },
1403 	/* DT  W O   BK   */
1404 	{ SST(0x11, 0x03, SS_FATAL|EIO,
1405 	    "Multiple read errors") },
1406 	/* D   W O   BK   */
1407 	{ SST(0x11, 0x04, SS_FATAL|EIO,
1408 	    "Unrecovered read error - auto reallocate failed") },
1409 	/*     WRO   B    */
1410 	{ SST(0x11, 0x05, SS_FATAL|EIO,
1411 	    "L-EC uncorrectable error") },
1412 	/*     WRO   B    */
1413 	{ SST(0x11, 0x06, SS_FATAL|EIO,
1414 	    "CIRC unrecovered error") },
1415 	/*     W O   B    */
1416 	{ SST(0x11, 0x07, SS_RDEF,
1417 	    "Data re-synchronization error") },
1418 	/*  T             */
1419 	{ SST(0x11, 0x08, SS_RDEF,
1420 	    "Incomplete block read") },
1421 	/*  T             */
1422 	{ SST(0x11, 0x09, SS_RDEF,
1423 	    "No gap found") },
1424 	/* DT    O   BK   */
1425 	{ SST(0x11, 0x0A, SS_RDEF,
1426 	    "Miscorrected error") },
1427 	/* D   W O   BK   */
1428 	{ SST(0x11, 0x0B, SS_FATAL|EIO,
1429 	    "Unrecovered read error - recommend reassignment") },
1430 	/* D   W O   BK   */
1431 	{ SST(0x11, 0x0C, SS_FATAL|EIO,
1432 	    "Unrecovered read error - recommend rewrite the data") },
1433 	/* DT  WRO   B    */
1434 	{ SST(0x11, 0x0D, SS_RDEF,
1435 	    "De-compression CRC error") },
1436 	/* DT  WRO   B    */
1437 	{ SST(0x11, 0x0E, SS_RDEF,
1438 	    "Cannot decompress using declared algorithm") },
1439 	/*      R         */
1440 	{ SST(0x11, 0x0F, SS_RDEF,
1441 	    "Error reading UPC/EAN number") },
1442 	/*      R         */
1443 	{ SST(0x11, 0x10, SS_RDEF,
1444 	    "Error reading ISRC number") },
1445 	/*      R         */
1446 	{ SST(0x11, 0x11, SS_RDEF,
1447 	    "Read error - loss of streaming") },
1448 	/* DT  WROM  B    */
1449 	{ SST(0x11, 0x12, SS_RDEF,	/* XXX TBD */
1450 	    "Auxiliary memory read error") },
1451 	/* DTLPWRO AEBKVF */
1452 	{ SST(0x11, 0x13, SS_RDEF,	/* XXX TBD */
1453 	    "Read error - failed retransmission request") },
1454 	/* D              */
1455 	{ SST(0x11, 0x14, SS_RDEF,	/* XXX TBD */
1456 	    "Read error - LBA marked bad by application client") },
1457 	/* D              */
1458 	{ SST(0x11, 0x15, SS_RDEF,	/* XXX TBD */
1459 	    "Write after sanitize required") },
1460 	/* D   W O   BK   */
1461 	{ SST(0x12, 0x00, SS_RDEF,
1462 	    "Address mark not found for ID field") },
1463 	/* D   W O   BK   */
1464 	{ SST(0x13, 0x00, SS_RDEF,
1465 	    "Address mark not found for data field") },
1466 	/* DTL WRO   BK   */
1467 	{ SST(0x14, 0x00, SS_RDEF,
1468 	    "Recorded entity not found") },
1469 	/* DT  WRO   BK   */
1470 	{ SST(0x14, 0x01, SS_RDEF,
1471 	    "Record not found") },
1472 	/*  T             */
1473 	{ SST(0x14, 0x02, SS_RDEF,
1474 	    "Filemark or setmark not found") },
1475 	/*  T             */
1476 	{ SST(0x14, 0x03, SS_RDEF,
1477 	    "End-of-data not found") },
1478 	/*  T             */
1479 	{ SST(0x14, 0x04, SS_RDEF,
1480 	    "Block sequence error") },
1481 	/* DT  W O   BK   */
1482 	{ SST(0x14, 0x05, SS_RDEF,
1483 	    "Record not found - recommend reassignment") },
1484 	/* DT  W O   BK   */
1485 	{ SST(0x14, 0x06, SS_RDEF,
1486 	    "Record not found - data auto-reallocated") },
1487 	/*  T             */
1488 	{ SST(0x14, 0x07, SS_RDEF,	/* XXX TBD */
1489 	    "Locate operation failure") },
1490 	/* DTL WROM  BK   */
1491 	{ SST(0x15, 0x00, SS_RDEF,
1492 	    "Random positioning error") },
1493 	/* DTL WROM  BK   */
1494 	{ SST(0x15, 0x01, SS_RDEF,
1495 	    "Mechanical positioning error") },
1496 	/* DT  WRO   BK   */
1497 	{ SST(0x15, 0x02, SS_RDEF,
1498 	    "Positioning error detected by read of medium") },
1499 	/* D   W O   BK   */
1500 	{ SST(0x16, 0x00, SS_RDEF,
1501 	    "Data synchronization mark error") },
1502 	/* D   W O   BK   */
1503 	{ SST(0x16, 0x01, SS_RDEF,
1504 	    "Data sync error - data rewritten") },
1505 	/* D   W O   BK   */
1506 	{ SST(0x16, 0x02, SS_RDEF,
1507 	    "Data sync error - recommend rewrite") },
1508 	/* D   W O   BK   */
1509 	{ SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1510 	    "Data sync error - data auto-reallocated") },
1511 	/* D   W O   BK   */
1512 	{ SST(0x16, 0x04, SS_RDEF,
1513 	    "Data sync error - recommend reassignment") },
1514 	/* DT  WRO   BK   */
1515 	{ SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1516 	    "Recovered data with no error correction applied") },
1517 	/* DT  WRO   BK   */
1518 	{ SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1519 	    "Recovered data with retries") },
1520 	/* DT  WRO   BK   */
1521 	{ SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1522 	    "Recovered data with positive head offset") },
1523 	/* DT  WRO   BK   */
1524 	{ SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1525 	    "Recovered data with negative head offset") },
1526 	/*     WRO   B    */
1527 	{ SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1528 	    "Recovered data with retries and/or CIRC applied") },
1529 	/* D   WRO   BK   */
1530 	{ SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1531 	    "Recovered data using previous sector ID") },
1532 	/* D   W O   BK   */
1533 	{ SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1534 	    "Recovered data without ECC - data auto-reallocated") },
1535 	/* D   WRO   BK   */
1536 	{ SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1537 	    "Recovered data without ECC - recommend reassignment") },
1538 	/* D   WRO   BK   */
1539 	{ SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1540 	    "Recovered data without ECC - recommend rewrite") },
1541 	/* D   WRO   BK   */
1542 	{ SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1543 	    "Recovered data without ECC - data rewritten") },
1544 	/* DT  WRO   BK   */
1545 	{ SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1546 	    "Recovered data with error correction applied") },
1547 	/* D   WRO   BK   */
1548 	{ SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1549 	    "Recovered data with error corr. & retries applied") },
1550 	/* D   WRO   BK   */
1551 	{ SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1552 	    "Recovered data - data auto-reallocated") },
1553 	/*      R         */
1554 	{ SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1555 	    "Recovered data with CIRC") },
1556 	/*      R         */
1557 	{ SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1558 	    "Recovered data with L-EC") },
1559 	/* D   WRO   BK   */
1560 	{ SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1561 	    "Recovered data - recommend reassignment") },
1562 	/* D   WRO   BK   */
1563 	{ SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1564 	    "Recovered data - recommend rewrite") },
1565 	/* D   W O   BK   */
1566 	{ SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1567 	    "Recovered data with ECC - data rewritten") },
1568 	/*      R         */
1569 	{ SST(0x18, 0x08, SS_RDEF,	/* XXX TBD */
1570 	    "Recovered data with linking") },
1571 	/* D     O    K   */
1572 	{ SST(0x19, 0x00, SS_RDEF,
1573 	    "Defect list error") },
1574 	/* D     O    K   */
1575 	{ SST(0x19, 0x01, SS_RDEF,
1576 	    "Defect list not available") },
1577 	/* D     O    K   */
1578 	{ SST(0x19, 0x02, SS_RDEF,
1579 	    "Defect list error in primary list") },
1580 	/* D     O    K   */
1581 	{ SST(0x19, 0x03, SS_RDEF,
1582 	    "Defect list error in grown list") },
1583 	/* DTLPWROMAEBKVF */
1584 	{ SST(0x1A, 0x00, SS_RDEF,
1585 	    "Parameter list length error") },
1586 	/* DTLPWROMAEBKVF */
1587 	{ SST(0x1B, 0x00, SS_RDEF,
1588 	    "Synchronous data transfer error") },
1589 	/* D     O   BK   */
1590 	{ SST(0x1C, 0x00, SS_RDEF,
1591 	    "Defect list not found") },
1592 	/* D     O   BK   */
1593 	{ SST(0x1C, 0x01, SS_RDEF,
1594 	    "Primary defect list not found") },
1595 	/* D     O   BK   */
1596 	{ SST(0x1C, 0x02, SS_RDEF,
1597 	    "Grown defect list not found") },
1598 	/* DT  WRO   BK   */
1599 	{ SST(0x1D, 0x00, SS_FATAL,
1600 	    "Miscompare during verify operation") },
1601 	/* D         B    */
1602 	{ SST(0x1D, 0x01, SS_RDEF,	/* XXX TBD */
1603 	    "Miscomparable verify of unmapped LBA") },
1604 	/* D   W O   BK   */
1605 	{ SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1606 	    "Recovered ID with ECC correction") },
1607 	/* D     O    K   */
1608 	{ SST(0x1F, 0x00, SS_RDEF,
1609 	    "Partial defect list transfer") },
1610 	/* DTLPWROMAEBKVF */
1611 	{ SST(0x20, 0x00, SS_FATAL | EINVAL,
1612 	    "Invalid command operation code") },
1613 	/* DT PWROMAEBK   */
1614 	{ SST(0x20, 0x01, SS_RDEF,	/* XXX TBD */
1615 	    "Access denied - initiator pending-enrolled") },
1616 	/* DT PWROMAEBK   */
1617 	{ SST(0x20, 0x02, SS_RDEF,	/* XXX TBD */
1618 	    "Access denied - no access rights") },
1619 	/* DT PWROMAEBK   */
1620 	{ SST(0x20, 0x03, SS_RDEF,	/* XXX TBD */
1621 	    "Access denied - invalid mgmt ID key") },
1622 	/*  T             */
1623 	{ SST(0x20, 0x04, SS_RDEF,	/* XXX TBD */
1624 	    "Illegal command while in write capable state") },
1625 	/*  T             */
1626 	{ SST(0x20, 0x05, SS_RDEF,	/* XXX TBD */
1627 	    "Obsolete") },
1628 	/*  T             */
1629 	{ SST(0x20, 0x06, SS_RDEF,	/* XXX TBD */
1630 	    "Illegal command while in explicit address mode") },
1631 	/*  T             */
1632 	{ SST(0x20, 0x07, SS_RDEF,	/* XXX TBD */
1633 	    "Illegal command while in implicit address mode") },
1634 	/* DT PWROMAEBK   */
1635 	{ SST(0x20, 0x08, SS_RDEF,	/* XXX TBD */
1636 	    "Access denied - enrollment conflict") },
1637 	/* DT PWROMAEBK   */
1638 	{ SST(0x20, 0x09, SS_RDEF,	/* XXX TBD */
1639 	    "Access denied - invalid LU identifier") },
1640 	/* DT PWROMAEBK   */
1641 	{ SST(0x20, 0x0A, SS_RDEF,	/* XXX TBD */
1642 	    "Access denied - invalid proxy token") },
1643 	/* DT PWROMAEBK   */
1644 	{ SST(0x20, 0x0B, SS_RDEF,	/* XXX TBD */
1645 	    "Access denied - ACL LUN conflict") },
1646 	/*  T             */
1647 	{ SST(0x20, 0x0C, SS_FATAL | EINVAL,
1648 	    "Illegal command when not in append-only mode") },
1649 	/* DT  WRO   BK   */
1650 	{ SST(0x21, 0x00, SS_FATAL | EINVAL,
1651 	    "Logical block address out of range") },
1652 	/* DT  WROM  BK   */
1653 	{ SST(0x21, 0x01, SS_FATAL | EINVAL,
1654 	    "Invalid element address") },
1655 	/*      R         */
1656 	{ SST(0x21, 0x02, SS_RDEF,	/* XXX TBD */
1657 	    "Invalid address for write") },
1658 	/*      R         */
1659 	{ SST(0x21, 0x03, SS_RDEF,	/* XXX TBD */
1660 	    "Invalid write crossing layer jump") },
1661 	/* D              */
1662 	{ SST(0x21, 0x04, SS_RDEF,	/* XXX TBD */
1663 	    "Unaligned write command") },
1664 	/* D              */
1665 	{ SST(0x21, 0x05, SS_RDEF,	/* XXX TBD */
1666 	    "Write boundary violation") },
1667 	/* D              */
1668 	{ SST(0x21, 0x06, SS_RDEF,	/* XXX TBD */
1669 	    "Attempt to read invalid data") },
1670 	/* D              */
1671 	{ SST(0x21, 0x07, SS_RDEF,	/* XXX TBD */
1672 	    "Read boundary violation") },
1673 	/* D              */
1674 	{ SST(0x22, 0x00, SS_FATAL | EINVAL,
1675 	    "Illegal function (use 20 00, 24 00, or 26 00)") },
1676 	/* DT P      B    */
1677 	{ SST(0x23, 0x00, SS_FATAL | EINVAL,
1678 	    "Invalid token operation, cause not reportable") },
1679 	/* DT P      B    */
1680 	{ SST(0x23, 0x01, SS_FATAL | EINVAL,
1681 	    "Invalid token operation, unsupported token type") },
1682 	/* DT P      B    */
1683 	{ SST(0x23, 0x02, SS_FATAL | EINVAL,
1684 	    "Invalid token operation, remote token usage not supported") },
1685 	/* DT P      B    */
1686 	{ SST(0x23, 0x03, SS_FATAL | EINVAL,
1687 	    "Invalid token operation, remote ROD token creation not supported") },
1688 	/* DT P      B    */
1689 	{ SST(0x23, 0x04, SS_FATAL | EINVAL,
1690 	    "Invalid token operation, token unknown") },
1691 	/* DT P      B    */
1692 	{ SST(0x23, 0x05, SS_FATAL | EINVAL,
1693 	    "Invalid token operation, token corrupt") },
1694 	/* DT P      B    */
1695 	{ SST(0x23, 0x06, SS_FATAL | EINVAL,
1696 	    "Invalid token operation, token revoked") },
1697 	/* DT P      B    */
1698 	{ SST(0x23, 0x07, SS_FATAL | EINVAL,
1699 	    "Invalid token operation, token expired") },
1700 	/* DT P      B    */
1701 	{ SST(0x23, 0x08, SS_FATAL | EINVAL,
1702 	    "Invalid token operation, token cancelled") },
1703 	/* DT P      B    */
1704 	{ SST(0x23, 0x09, SS_FATAL | EINVAL,
1705 	    "Invalid token operation, token deleted") },
1706 	/* DT P      B    */
1707 	{ SST(0x23, 0x0A, SS_FATAL | EINVAL,
1708 	    "Invalid token operation, invalid token length") },
1709 	/* DTLPWROMAEBKVF */
1710 	{ SST(0x24, 0x00, SS_FATAL | EINVAL,
1711 	    "Invalid field in CDB") },
1712 	/* DTLPWRO AEBKVF */
1713 	{ SST(0x24, 0x01, SS_RDEF,	/* XXX TBD */
1714 	    "CDB decryption error") },
1715 	/*  T             */
1716 	{ SST(0x24, 0x02, SS_RDEF,	/* XXX TBD */
1717 	    "Obsolete") },
1718 	/*  T             */
1719 	{ SST(0x24, 0x03, SS_RDEF,	/* XXX TBD */
1720 	    "Obsolete") },
1721 	/*              F */
1722 	{ SST(0x24, 0x04, SS_RDEF,	/* XXX TBD */
1723 	    "Security audit value frozen") },
1724 	/*              F */
1725 	{ SST(0x24, 0x05, SS_RDEF,	/* XXX TBD */
1726 	    "Security working key frozen") },
1727 	/*              F */
1728 	{ SST(0x24, 0x06, SS_RDEF,	/* XXX TBD */
1729 	    "NONCE not unique") },
1730 	/*              F */
1731 	{ SST(0x24, 0x07, SS_RDEF,	/* XXX TBD */
1732 	    "NONCE timestamp out of range") },
1733 	/* DT   R MAEBKV  */
1734 	{ SST(0x24, 0x08, SS_RDEF,	/* XXX TBD */
1735 	    "Invalid XCDB") },
1736 	/* DTLPWROMAEBKVF */
1737 	{ SST(0x25, 0x00, SS_FATAL | ENXIO | SSQ_LOST,
1738 	    "Logical unit not supported") },
1739 	/* DTLPWROMAEBKVF */
1740 	{ SST(0x26, 0x00, SS_FATAL | EINVAL,
1741 	    "Invalid field in parameter list") },
1742 	/* DTLPWROMAEBKVF */
1743 	{ SST(0x26, 0x01, SS_FATAL | EINVAL,
1744 	    "Parameter not supported") },
1745 	/* DTLPWROMAEBKVF */
1746 	{ SST(0x26, 0x02, SS_FATAL | EINVAL,
1747 	    "Parameter value invalid") },
1748 	/* DTLPWROMAE K   */
1749 	{ SST(0x26, 0x03, SS_FATAL | EINVAL,
1750 	    "Threshold parameters not supported") },
1751 	/* DTLPWROMAEBKVF */
1752 	{ SST(0x26, 0x04, SS_FATAL | EINVAL,
1753 	    "Invalid release of persistent reservation") },
1754 	/* DTLPWRO A BK   */
1755 	{ SST(0x26, 0x05, SS_RDEF,	/* XXX TBD */
1756 	    "Data decryption error") },
1757 	/* DTLPWRO    K   */
1758 	{ SST(0x26, 0x06, SS_FATAL | EINVAL,
1759 	    "Too many target descriptors") },
1760 	/* DTLPWRO    K   */
1761 	{ SST(0x26, 0x07, SS_FATAL | EINVAL,
1762 	    "Unsupported target descriptor type code") },
1763 	/* DTLPWRO    K   */
1764 	{ SST(0x26, 0x08, SS_FATAL | EINVAL,
1765 	    "Too many segment descriptors") },
1766 	/* DTLPWRO    K   */
1767 	{ SST(0x26, 0x09, SS_FATAL | EINVAL,
1768 	    "Unsupported segment descriptor type code") },
1769 	/* DTLPWRO    K   */
1770 	{ SST(0x26, 0x0A, SS_FATAL | EINVAL,
1771 	    "Unexpected inexact segment") },
1772 	/* DTLPWRO    K   */
1773 	{ SST(0x26, 0x0B, SS_FATAL | EINVAL,
1774 	    "Inline data length exceeded") },
1775 	/* DTLPWRO    K   */
1776 	{ SST(0x26, 0x0C, SS_FATAL | EINVAL,
1777 	    "Invalid operation for copy source or destination") },
1778 	/* DTLPWRO    K   */
1779 	{ SST(0x26, 0x0D, SS_FATAL | EINVAL,
1780 	    "Copy segment granularity violation") },
1781 	/* DT PWROMAEBK   */
1782 	{ SST(0x26, 0x0E, SS_RDEF,	/* XXX TBD */
1783 	    "Invalid parameter while port is enabled") },
1784 	/*              F */
1785 	{ SST(0x26, 0x0F, SS_RDEF,	/* XXX TBD */
1786 	    "Invalid data-out buffer integrity check value") },
1787 	/*  T             */
1788 	{ SST(0x26, 0x10, SS_RDEF,	/* XXX TBD */
1789 	    "Data decryption key fail limit reached") },
1790 	/*  T             */
1791 	{ SST(0x26, 0x11, SS_RDEF,	/* XXX TBD */
1792 	    "Incomplete key-associated data set") },
1793 	/*  T             */
1794 	{ SST(0x26, 0x12, SS_RDEF,	/* XXX TBD */
1795 	    "Vendor specific key reference not found") },
1796 	/* D              */
1797 	{ SST(0x26, 0x13, SS_RDEF,	/* XXX TBD */
1798 	    "Application tag mode page is invalid") },
1799 	/* DT  WRO   BK   */
1800 	{ SST(0x27, 0x00, SS_FATAL | EACCES,
1801 	    "Write protected") },
1802 	/* DT  WRO   BK   */
1803 	{ SST(0x27, 0x01, SS_FATAL | EACCES,
1804 	    "Hardware write protected") },
1805 	/* DT  WRO   BK   */
1806 	{ SST(0x27, 0x02, SS_FATAL | EACCES,
1807 	    "Logical unit software write protected") },
1808 	/*  T   R         */
1809 	{ SST(0x27, 0x03, SS_FATAL | EACCES,
1810 	    "Associated write protect") },
1811 	/*  T   R         */
1812 	{ SST(0x27, 0x04, SS_FATAL | EACCES,
1813 	    "Persistent write protect") },
1814 	/*  T   R         */
1815 	{ SST(0x27, 0x05, SS_FATAL | EACCES,
1816 	    "Permanent write protect") },
1817 	/*      R       F */
1818 	{ SST(0x27, 0x06, SS_RDEF,	/* XXX TBD */
1819 	    "Conditional write protect") },
1820 	/* D         B    */
1821 	{ SST(0x27, 0x07, SS_FATAL | ENOSPC,
1822 	    "Space allocation failed write protect") },
1823 	/* D              */
1824 	{ SST(0x27, 0x08, SS_FATAL | EACCES,
1825 	    "Zone is read only") },
1826 	/* DTLPWROMAEBKVF */
1827 	{ SST(0x28, 0x00, SS_FATAL | ENXIO,
1828 	    "Not ready to ready change, medium may have changed") },
1829 	/* DT  WROM  B    */
1830 	{ SST(0x28, 0x01, SS_FATAL | ENXIO,
1831 	    "Import or export element accessed") },
1832 	/*      R         */
1833 	{ SST(0x28, 0x02, SS_RDEF,	/* XXX TBD */
1834 	    "Format-layer may have changed") },
1835 	/*        M       */
1836 	{ SST(0x28, 0x03, SS_RDEF,	/* XXX TBD */
1837 	    "Import/export element accessed, medium changed") },
1838 	/*
1839 	 * XXX JGibbs - All of these should use the same errno, but I don't
1840 	 * think ENXIO is the correct choice.  Should we borrow from
1841 	 * the networking errnos?  ECONNRESET anyone?
1842 	 */
1843 	/* DTLPWROMAEBKVF */
1844 	{ SST(0x29, 0x00, SS_FATAL | ENXIO,
1845 	    "Power on, reset, or bus device reset occurred") },
1846 	/* DTLPWROMAEBKVF */
1847 	{ SST(0x29, 0x01, SS_RDEF,
1848 	    "Power on occurred") },
1849 	/* DTLPWROMAEBKVF */
1850 	{ SST(0x29, 0x02, SS_RDEF,
1851 	    "SCSI bus reset occurred") },
1852 	/* DTLPWROMAEBKVF */
1853 	{ SST(0x29, 0x03, SS_RDEF,
1854 	    "Bus device reset function occurred") },
1855 	/* DTLPWROMAEBKVF */
1856 	{ SST(0x29, 0x04, SS_RDEF,
1857 	    "Device internal reset") },
1858 	/* DTLPWROMAEBKVF */
1859 	{ SST(0x29, 0x05, SS_RDEF,
1860 	    "Transceiver mode changed to single-ended") },
1861 	/* DTLPWROMAEBKVF */
1862 	{ SST(0x29, 0x06, SS_RDEF,
1863 	    "Transceiver mode changed to LVD") },
1864 	/* DTLPWROMAEBKVF */
1865 	{ SST(0x29, 0x07, SS_RDEF,	/* XXX TBD */
1866 	    "I_T nexus loss occurred") },
1867 	/* DTL WROMAEBKVF */
1868 	{ SST(0x2A, 0x00, SS_RDEF,
1869 	    "Parameters changed") },
1870 	/* DTL WROMAEBKVF */
1871 	{ SST(0x2A, 0x01, SS_RDEF,
1872 	    "Mode parameters changed") },
1873 	/* DTL WROMAE K   */
1874 	{ SST(0x2A, 0x02, SS_RDEF,
1875 	    "Log parameters changed") },
1876 	/* DTLPWROMAE K   */
1877 	{ SST(0x2A, 0x03, SS_RDEF,
1878 	    "Reservations preempted") },
1879 	/* DTLPWROMAE     */
1880 	{ SST(0x2A, 0x04, SS_RDEF,	/* XXX TBD */
1881 	    "Reservations released") },
1882 	/* DTLPWROMAE     */
1883 	{ SST(0x2A, 0x05, SS_RDEF,	/* XXX TBD */
1884 	    "Registrations preempted") },
1885 	/* DTLPWROMAEBKVF */
1886 	{ SST(0x2A, 0x06, SS_RDEF,	/* XXX TBD */
1887 	    "Asymmetric access state changed") },
1888 	/* DTLPWROMAEBKVF */
1889 	{ SST(0x2A, 0x07, SS_RDEF,	/* XXX TBD */
1890 	    "Implicit asymmetric access state transition failed") },
1891 	/* DT  WROMAEBKVF */
1892 	{ SST(0x2A, 0x08, SS_RDEF,	/* XXX TBD */
1893 	    "Priority changed") },
1894 	/* D              */
1895 	{ SST(0x2A, 0x09, SS_RDEF,	/* XXX TBD */
1896 	    "Capacity data has changed") },
1897 	/* DT             */
1898 	{ SST(0x2A, 0x0A, SS_RDEF,	/* XXX TBD */
1899 	    "Error history I_T nexus cleared") },
1900 	/* DT             */
1901 	{ SST(0x2A, 0x0B, SS_RDEF,	/* XXX TBD */
1902 	    "Error history snapshot released") },
1903 	/*              F */
1904 	{ SST(0x2A, 0x0C, SS_RDEF,	/* XXX TBD */
1905 	    "Error recovery attributes have changed") },
1906 	/*  T             */
1907 	{ SST(0x2A, 0x0D, SS_RDEF,	/* XXX TBD */
1908 	    "Data encryption capabilities changed") },
1909 	/* DT     M E  V  */
1910 	{ SST(0x2A, 0x10, SS_RDEF,	/* XXX TBD */
1911 	    "Timestamp changed") },
1912 	/*  T             */
1913 	{ SST(0x2A, 0x11, SS_RDEF,	/* XXX TBD */
1914 	    "Data encryption parameters changed by another I_T nexus") },
1915 	/*  T             */
1916 	{ SST(0x2A, 0x12, SS_RDEF,	/* XXX TBD */
1917 	    "Data encryption parameters changed by vendor specific event") },
1918 	/*  T             */
1919 	{ SST(0x2A, 0x13, SS_RDEF,	/* XXX TBD */
1920 	    "Data encryption key instance counter has changed") },
1921 	/* DT   R MAEBKV  */
1922 	{ SST(0x2A, 0x14, SS_RDEF,	/* XXX TBD */
1923 	    "SA creation capabilities data has changed") },
1924 	/*  T     M    V  */
1925 	{ SST(0x2A, 0x15, SS_RDEF,	/* XXX TBD */
1926 	    "Medium removal prevention preempted") },
1927 	/* DTLPWRO    K   */
1928 	{ SST(0x2B, 0x00, SS_RDEF,
1929 	    "Copy cannot execute since host cannot disconnect") },
1930 	/* DTLPWROMAEBKVF */
1931 	{ SST(0x2C, 0x00, SS_RDEF,
1932 	    "Command sequence error") },
1933 	/*                */
1934 	{ SST(0x2C, 0x01, SS_RDEF,
1935 	    "Too many windows specified") },
1936 	/*                */
1937 	{ SST(0x2C, 0x02, SS_RDEF,
1938 	    "Invalid combination of windows specified") },
1939 	/*      R         */
1940 	{ SST(0x2C, 0x03, SS_RDEF,
1941 	    "Current program area is not empty") },
1942 	/*      R         */
1943 	{ SST(0x2C, 0x04, SS_RDEF,
1944 	    "Current program area is empty") },
1945 	/*           B    */
1946 	{ SST(0x2C, 0x05, SS_RDEF,	/* XXX TBD */
1947 	    "Illegal power condition request") },
1948 	/*      R         */
1949 	{ SST(0x2C, 0x06, SS_RDEF,	/* XXX TBD */
1950 	    "Persistent prevent conflict") },
1951 	/* DTLPWROMAEBKVF */
1952 	{ SST(0x2C, 0x07, SS_RDEF,	/* XXX TBD */
1953 	    "Previous busy status") },
1954 	/* DTLPWROMAEBKVF */
1955 	{ SST(0x2C, 0x08, SS_RDEF,	/* XXX TBD */
1956 	    "Previous task set full status") },
1957 	/* DTLPWROM EBKVF */
1958 	{ SST(0x2C, 0x09, SS_RDEF,	/* XXX TBD */
1959 	    "Previous reservation conflict status") },
1960 	/*              F */
1961 	{ SST(0x2C, 0x0A, SS_RDEF,	/* XXX TBD */
1962 	    "Partition or collection contains user objects") },
1963 	/*  T             */
1964 	{ SST(0x2C, 0x0B, SS_RDEF,	/* XXX TBD */
1965 	    "Not reserved") },
1966 	/* D              */
1967 	{ SST(0x2C, 0x0C, SS_RDEF,	/* XXX TBD */
1968 	    "ORWRITE generation does not match") },
1969 	/* D              */
1970 	{ SST(0x2C, 0x0D, SS_RDEF,	/* XXX TBD */
1971 	    "Reset write pointer not allowed") },
1972 	/* D              */
1973 	{ SST(0x2C, 0x0E, SS_RDEF,	/* XXX TBD */
1974 	    "Zone is offline") },
1975 	/* D              */
1976 	{ SST(0x2C, 0x0F, SS_RDEF,	/* XXX TBD */
1977 	    "Stream not open") },
1978 	/* D              */
1979 	{ SST(0x2C, 0x10, SS_RDEF,	/* XXX TBD */
1980 	    "Unwritten data in zone") },
1981 	/*  T             */
1982 	{ SST(0x2D, 0x00, SS_RDEF,
1983 	    "Overwrite error on update in place") },
1984 	/*      R         */
1985 	{ SST(0x2E, 0x00, SS_RDEF,	/* XXX TBD */
1986 	    "Insufficient time for operation") },
1987 	/* D              */
1988 	{ SST(0x2E, 0x01, SS_RDEF,	/* XXX TBD */
1989 	    "Command timeout before processing") },
1990 	/* D              */
1991 	{ SST(0x2E, 0x02, SS_RDEF,	/* XXX TBD */
1992 	    "Command timeout during processing") },
1993 	/* D              */
1994 	{ SST(0x2E, 0x03, SS_RDEF,	/* XXX TBD */
1995 	    "Command timeout during processing due to error recovery") },
1996 	/* DTLPWROMAEBKVF */
1997 	{ SST(0x2F, 0x00, SS_RDEF,
1998 	    "Commands cleared by another initiator") },
1999 	/* D              */
2000 	{ SST(0x2F, 0x01, SS_RDEF,	/* XXX TBD */
2001 	    "Commands cleared by power loss notification") },
2002 	/* DTLPWROMAEBKVF */
2003 	{ SST(0x2F, 0x02, SS_RDEF,	/* XXX TBD */
2004 	    "Commands cleared by device server") },
2005 	/* DTLPWROMAEBKVF */
2006 	{ SST(0x2F, 0x03, SS_RDEF,	/* XXX TBD */
2007 	    "Some commands cleared by queuing layer event") },
2008 	/* DT  WROM  BK   */
2009 	{ SST(0x30, 0x00, SS_RDEF,
2010 	    "Incompatible medium installed") },
2011 	/* DT  WRO   BK   */
2012 	{ SST(0x30, 0x01, SS_RDEF,
2013 	    "Cannot read medium - unknown format") },
2014 	/* DT  WRO   BK   */
2015 	{ SST(0x30, 0x02, SS_RDEF,
2016 	    "Cannot read medium - incompatible format") },
2017 	/* DT   R     K   */
2018 	{ SST(0x30, 0x03, SS_RDEF,
2019 	    "Cleaning cartridge installed") },
2020 	/* DT  WRO   BK   */
2021 	{ SST(0x30, 0x04, SS_RDEF,
2022 	    "Cannot write medium - unknown format") },
2023 	/* DT  WRO   BK   */
2024 	{ SST(0x30, 0x05, SS_RDEF,
2025 	    "Cannot write medium - incompatible format") },
2026 	/* DT  WRO   B    */
2027 	{ SST(0x30, 0x06, SS_RDEF,
2028 	    "Cannot format medium - incompatible medium") },
2029 	/* DTL WROMAEBKVF */
2030 	{ SST(0x30, 0x07, SS_RDEF,
2031 	    "Cleaning failure") },
2032 	/*      R         */
2033 	{ SST(0x30, 0x08, SS_RDEF,
2034 	    "Cannot write - application code mismatch") },
2035 	/*      R         */
2036 	{ SST(0x30, 0x09, SS_RDEF,
2037 	    "Current session not fixated for append") },
2038 	/* DT  WRO AEBK   */
2039 	{ SST(0x30, 0x0A, SS_RDEF,	/* XXX TBD */
2040 	    "Cleaning request rejected") },
2041 	/*  T             */
2042 	{ SST(0x30, 0x0C, SS_RDEF,	/* XXX TBD */
2043 	    "WORM medium - overwrite attempted") },
2044 	/*  T             */
2045 	{ SST(0x30, 0x0D, SS_RDEF,	/* XXX TBD */
2046 	    "WORM medium - integrity check") },
2047 	/*      R         */
2048 	{ SST(0x30, 0x10, SS_RDEF,	/* XXX TBD */
2049 	    "Medium not formatted") },
2050 	/*        M       */
2051 	{ SST(0x30, 0x11, SS_RDEF,	/* XXX TBD */
2052 	    "Incompatible volume type") },
2053 	/*        M       */
2054 	{ SST(0x30, 0x12, SS_RDEF,	/* XXX TBD */
2055 	    "Incompatible volume qualifier") },
2056 	/*        M       */
2057 	{ SST(0x30, 0x13, SS_RDEF,	/* XXX TBD */
2058 	    "Cleaning volume expired") },
2059 	/* DT  WRO   BK   */
2060 	{ SST(0x31, 0x00, SS_RDEF,
2061 	    "Medium format corrupted") },
2062 	/* D L  RO   B    */
2063 	{ SST(0x31, 0x01, SS_RDEF,
2064 	    "Format command failed") },
2065 	/*      R         */
2066 	{ SST(0x31, 0x02, SS_RDEF,	/* XXX TBD */
2067 	    "Zoned formatting failed due to spare linking") },
2068 	/* D         B    */
2069 	{ SST(0x31, 0x03, SS_RDEF,	/* XXX TBD */
2070 	    "SANITIZE command failed") },
2071 	/* D   W O   BK   */
2072 	{ SST(0x32, 0x00, SS_RDEF,
2073 	    "No defect spare location available") },
2074 	/* D   W O   BK   */
2075 	{ SST(0x32, 0x01, SS_RDEF,
2076 	    "Defect list update failure") },
2077 	/*  T             */
2078 	{ SST(0x33, 0x00, SS_RDEF,
2079 	    "Tape length error") },
2080 	/* DTLPWROMAEBKVF */
2081 	{ SST(0x34, 0x00, SS_RDEF,
2082 	    "Enclosure failure") },
2083 	/* DTLPWROMAEBKVF */
2084 	{ SST(0x35, 0x00, SS_RDEF,
2085 	    "Enclosure services failure") },
2086 	/* DTLPWROMAEBKVF */
2087 	{ SST(0x35, 0x01, SS_RDEF,
2088 	    "Unsupported enclosure function") },
2089 	/* DTLPWROMAEBKVF */
2090 	{ SST(0x35, 0x02, SS_RDEF,
2091 	    "Enclosure services unavailable") },
2092 	/* DTLPWROMAEBKVF */
2093 	{ SST(0x35, 0x03, SS_RDEF,
2094 	    "Enclosure services transfer failure") },
2095 	/* DTLPWROMAEBKVF */
2096 	{ SST(0x35, 0x04, SS_RDEF,
2097 	    "Enclosure services transfer refused") },
2098 	/* DTL WROMAEBKVF */
2099 	{ SST(0x35, 0x05, SS_RDEF,	/* XXX TBD */
2100 	    "Enclosure services checksum error") },
2101 	/*   L            */
2102 	{ SST(0x36, 0x00, SS_RDEF,
2103 	    "Ribbon, ink, or toner failure") },
2104 	/* DTL WROMAEBKVF */
2105 	{ SST(0x37, 0x00, SS_RDEF,
2106 	    "Rounded parameter") },
2107 	/*           B    */
2108 	{ SST(0x38, 0x00, SS_RDEF,	/* XXX TBD */
2109 	    "Event status notification") },
2110 	/*           B    */
2111 	{ SST(0x38, 0x02, SS_RDEF,	/* XXX TBD */
2112 	    "ESN - power management class event") },
2113 	/*           B    */
2114 	{ SST(0x38, 0x04, SS_RDEF,	/* XXX TBD */
2115 	    "ESN - media class event") },
2116 	/*           B    */
2117 	{ SST(0x38, 0x06, SS_RDEF,	/* XXX TBD */
2118 	    "ESN - device busy class event") },
2119 	/* D              */
2120 	{ SST(0x38, 0x07, SS_RDEF,	/* XXX TBD */
2121 	    "Thin provisioning soft threshold reached") },
2122 	/* DTL WROMAE K   */
2123 	{ SST(0x39, 0x00, SS_RDEF,
2124 	    "Saving parameters not supported") },
2125 	/* DTL WROM  BK   */
2126 	{ SST(0x3A, 0x00, SS_FATAL | ENXIO,
2127 	    "Medium not present") },
2128 	/* DT  WROM  BK   */
2129 	{ SST(0x3A, 0x01, SS_FATAL | ENXIO,
2130 	    "Medium not present - tray closed") },
2131 	/* DT  WROM  BK   */
2132 	{ SST(0x3A, 0x02, SS_FATAL | ENXIO,
2133 	    "Medium not present - tray open") },
2134 	/* DT  WROM  B    */
2135 	{ SST(0x3A, 0x03, SS_RDEF,	/* XXX TBD */
2136 	    "Medium not present - loadable") },
2137 	/* DT  WRO   B    */
2138 	{ SST(0x3A, 0x04, SS_RDEF,	/* XXX TBD */
2139 	    "Medium not present - medium auxiliary memory accessible") },
2140 	/*  TL            */
2141 	{ SST(0x3B, 0x00, SS_RDEF,
2142 	    "Sequential positioning error") },
2143 	/*  T             */
2144 	{ SST(0x3B, 0x01, SS_RDEF,
2145 	    "Tape position error at beginning-of-medium") },
2146 	/*  T             */
2147 	{ SST(0x3B, 0x02, SS_RDEF,
2148 	    "Tape position error at end-of-medium") },
2149 	/*   L            */
2150 	{ SST(0x3B, 0x03, SS_RDEF,
2151 	    "Tape or electronic vertical forms unit not ready") },
2152 	/*   L            */
2153 	{ SST(0x3B, 0x04, SS_RDEF,
2154 	    "Slew failure") },
2155 	/*   L            */
2156 	{ SST(0x3B, 0x05, SS_RDEF,
2157 	    "Paper jam") },
2158 	/*   L            */
2159 	{ SST(0x3B, 0x06, SS_RDEF,
2160 	    "Failed to sense top-of-form") },
2161 	/*   L            */
2162 	{ SST(0x3B, 0x07, SS_RDEF,
2163 	    "Failed to sense bottom-of-form") },
2164 	/*  T             */
2165 	{ SST(0x3B, 0x08, SS_RDEF,
2166 	    "Reposition error") },
2167 	/*                */
2168 	{ SST(0x3B, 0x09, SS_RDEF,
2169 	    "Read past end of medium") },
2170 	/*                */
2171 	{ SST(0x3B, 0x0A, SS_RDEF,
2172 	    "Read past beginning of medium") },
2173 	/*                */
2174 	{ SST(0x3B, 0x0B, SS_RDEF,
2175 	    "Position past end of medium") },
2176 	/*  T             */
2177 	{ SST(0x3B, 0x0C, SS_RDEF,
2178 	    "Position past beginning of medium") },
2179 	/* DT  WROM  BK   */
2180 	{ SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
2181 	    "Medium destination element full") },
2182 	/* DT  WROM  BK   */
2183 	{ SST(0x3B, 0x0E, SS_RDEF,
2184 	    "Medium source element empty") },
2185 	/*      R         */
2186 	{ SST(0x3B, 0x0F, SS_RDEF,
2187 	    "End of medium reached") },
2188 	/* DT  WROM  BK   */
2189 	{ SST(0x3B, 0x11, SS_RDEF,
2190 	    "Medium magazine not accessible") },
2191 	/* DT  WROM  BK   */
2192 	{ SST(0x3B, 0x12, SS_RDEF,
2193 	    "Medium magazine removed") },
2194 	/* DT  WROM  BK   */
2195 	{ SST(0x3B, 0x13, SS_RDEF,
2196 	    "Medium magazine inserted") },
2197 	/* DT  WROM  BK   */
2198 	{ SST(0x3B, 0x14, SS_RDEF,
2199 	    "Medium magazine locked") },
2200 	/* DT  WROM  BK   */
2201 	{ SST(0x3B, 0x15, SS_RDEF,
2202 	    "Medium magazine unlocked") },
2203 	/*      R         */
2204 	{ SST(0x3B, 0x16, SS_RDEF,	/* XXX TBD */
2205 	    "Mechanical positioning or changer error") },
2206 	/*              F */
2207 	{ SST(0x3B, 0x17, SS_RDEF,	/* XXX TBD */
2208 	    "Read past end of user object") },
2209 	/*        M       */
2210 	{ SST(0x3B, 0x18, SS_RDEF,	/* XXX TBD */
2211 	    "Element disabled") },
2212 	/*        M       */
2213 	{ SST(0x3B, 0x19, SS_RDEF,	/* XXX TBD */
2214 	    "Element enabled") },
2215 	/*        M       */
2216 	{ SST(0x3B, 0x1A, SS_RDEF,	/* XXX TBD */
2217 	    "Data transfer device removed") },
2218 	/*        M       */
2219 	{ SST(0x3B, 0x1B, SS_RDEF,	/* XXX TBD */
2220 	    "Data transfer device inserted") },
2221 	/*  T             */
2222 	{ SST(0x3B, 0x1C, SS_RDEF,	/* XXX TBD */
2223 	    "Too many logical objects on partition to support operation") },
2224 	/* DTLPWROMAE K   */
2225 	{ SST(0x3D, 0x00, SS_RDEF,
2226 	    "Invalid bits in IDENTIFY message") },
2227 	/* DTLPWROMAEBKVF */
2228 	{ SST(0x3E, 0x00, SS_RDEF,
2229 	    "Logical unit has not self-configured yet") },
2230 	/* DTLPWROMAEBKVF */
2231 	{ SST(0x3E, 0x01, SS_RDEF,
2232 	    "Logical unit failure") },
2233 	/* DTLPWROMAEBKVF */
2234 	{ SST(0x3E, 0x02, SS_RDEF,
2235 	    "Timeout on logical unit") },
2236 	/* DTLPWROMAEBKVF */
2237 	{ SST(0x3E, 0x03, SS_RDEF,	/* XXX TBD */
2238 	    "Logical unit failed self-test") },
2239 	/* DTLPWROMAEBKVF */
2240 	{ SST(0x3E, 0x04, SS_RDEF,	/* XXX TBD */
2241 	    "Logical unit unable to update self-test log") },
2242 	/* DTLPWROMAEBKVF */
2243 	{ SST(0x3F, 0x00, SS_RDEF,
2244 	    "Target operating conditions have changed") },
2245 	/* DTLPWROMAEBKVF */
2246 	{ SST(0x3F, 0x01, SS_RDEF,
2247 	    "Microcode has been changed") },
2248 	/* DTLPWROM  BK   */
2249 	{ SST(0x3F, 0x02, SS_RDEF,
2250 	    "Changed operating definition") },
2251 	/* DTLPWROMAEBKVF */
2252 	{ SST(0x3F, 0x03, SS_RDEF,
2253 	    "INQUIRY data has changed") },
2254 	/* DT  WROMAEBK   */
2255 	{ SST(0x3F, 0x04, SS_RDEF,
2256 	    "Component device attached") },
2257 	/* DT  WROMAEBK   */
2258 	{ SST(0x3F, 0x05, SS_RDEF,
2259 	    "Device identifier changed") },
2260 	/* DT  WROMAEB    */
2261 	{ SST(0x3F, 0x06, SS_RDEF,
2262 	    "Redundancy group created or modified") },
2263 	/* DT  WROMAEB    */
2264 	{ SST(0x3F, 0x07, SS_RDEF,
2265 	    "Redundancy group deleted") },
2266 	/* DT  WROMAEB    */
2267 	{ SST(0x3F, 0x08, SS_RDEF,
2268 	    "Spare created or modified") },
2269 	/* DT  WROMAEB    */
2270 	{ SST(0x3F, 0x09, SS_RDEF,
2271 	    "Spare deleted") },
2272 	/* DT  WROMAEBK   */
2273 	{ SST(0x3F, 0x0A, SS_RDEF,
2274 	    "Volume set created or modified") },
2275 	/* DT  WROMAEBK   */
2276 	{ SST(0x3F, 0x0B, SS_RDEF,
2277 	    "Volume set deleted") },
2278 	/* DT  WROMAEBK   */
2279 	{ SST(0x3F, 0x0C, SS_RDEF,
2280 	    "Volume set deassigned") },
2281 	/* DT  WROMAEBK   */
2282 	{ SST(0x3F, 0x0D, SS_RDEF,
2283 	    "Volume set reassigned") },
2284 	/* DTLPWROMAE     */
2285 	{ SST(0x3F, 0x0E, SS_RDEF | SSQ_RESCAN ,
2286 	    "Reported LUNs data has changed") },
2287 	/* DTLPWROMAEBKVF */
2288 	{ SST(0x3F, 0x0F, SS_RDEF,	/* XXX TBD */
2289 	    "Echo buffer overwritten") },
2290 	/* DT  WROM  B    */
2291 	{ SST(0x3F, 0x10, SS_RDEF,	/* XXX TBD */
2292 	    "Medium loadable") },
2293 	/* DT  WROM  B    */
2294 	{ SST(0x3F, 0x11, SS_RDEF,	/* XXX TBD */
2295 	    "Medium auxiliary memory accessible") },
2296 	/* DTLPWR MAEBK F */
2297 	{ SST(0x3F, 0x12, SS_RDEF,	/* XXX TBD */
2298 	    "iSCSI IP address added") },
2299 	/* DTLPWR MAEBK F */
2300 	{ SST(0x3F, 0x13, SS_RDEF,	/* XXX TBD */
2301 	    "iSCSI IP address removed") },
2302 	/* DTLPWR MAEBK F */
2303 	{ SST(0x3F, 0x14, SS_RDEF,	/* XXX TBD */
2304 	    "iSCSI IP address changed") },
2305 	/* DTLPWR MAEBK   */
2306 	{ SST(0x3F, 0x15, SS_RDEF,	/* XXX TBD */
2307 	    "Inspect referrals sense descriptors") },
2308 	/* DTLPWROMAEBKVF */
2309 	{ SST(0x3F, 0x16, SS_RDEF,	/* XXX TBD */
2310 	    "Microcode has been changed without reset") },
2311 	/* D              */
2312 	{ SST(0x3F, 0x17, SS_RDEF,	/* XXX TBD */
2313 	    "Zone transition to full") },
2314 	/* D              */
2315 	{ SST(0x40, 0x00, SS_RDEF,
2316 	    "RAM failure") },		/* deprecated - use 40 NN instead */
2317 	/* DTLPWROMAEBKVF */
2318 	{ SST(0x40, 0x80, SS_RDEF,
2319 	    "Diagnostic failure: ASCQ = Component ID") },
2320 	/* DTLPWROMAEBKVF */
2321 	{ SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
2322 	    NULL) },			/* Range 0x80->0xFF */
2323 	/* D              */
2324 	{ SST(0x41, 0x00, SS_RDEF,
2325 	    "Data path failure") },	/* deprecated - use 40 NN instead */
2326 	/* D              */
2327 	{ SST(0x42, 0x00, SS_RDEF,
2328 	    "Power-on or self-test failure") },
2329 					/* deprecated - use 40 NN instead */
2330 	/* DTLPWROMAEBKVF */
2331 	{ SST(0x43, 0x00, SS_RDEF,
2332 	    "Message error") },
2333 	/* DTLPWROMAEBKVF */
2334 	{ SST(0x44, 0x00, SS_RDEF,
2335 	    "Internal target failure") },
2336 	/* DT P   MAEBKVF */
2337 	{ SST(0x44, 0x01, SS_RDEF,	/* XXX TBD */
2338 	    "Persistent reservation information lost") },
2339 	/* DT        B    */
2340 	{ SST(0x44, 0x71, SS_RDEF,	/* XXX TBD */
2341 	    "ATA device failed set features") },
2342 	/* DTLPWROMAEBKVF */
2343 	{ SST(0x45, 0x00, SS_RDEF,
2344 	    "Select or reselect failure") },
2345 	/* DTLPWROM  BK   */
2346 	{ SST(0x46, 0x00, SS_RDEF,
2347 	    "Unsuccessful soft reset") },
2348 	/* DTLPWROMAEBKVF */
2349 	{ SST(0x47, 0x00, SS_RDEF,
2350 	    "SCSI parity error") },
2351 	/* DTLPWROMAEBKVF */
2352 	{ SST(0x47, 0x01, SS_RDEF,	/* XXX TBD */
2353 	    "Data phase CRC error detected") },
2354 	/* DTLPWROMAEBKVF */
2355 	{ SST(0x47, 0x02, SS_RDEF,	/* XXX TBD */
2356 	    "SCSI parity error detected during ST data phase") },
2357 	/* DTLPWROMAEBKVF */
2358 	{ SST(0x47, 0x03, SS_RDEF,	/* XXX TBD */
2359 	    "Information unit iuCRC error detected") },
2360 	/* DTLPWROMAEBKVF */
2361 	{ SST(0x47, 0x04, SS_RDEF,	/* XXX TBD */
2362 	    "Asynchronous information protection error detected") },
2363 	/* DTLPWROMAEBKVF */
2364 	{ SST(0x47, 0x05, SS_RDEF,	/* XXX TBD */
2365 	    "Protocol service CRC error") },
2366 	/* DT     MAEBKVF */
2367 	{ SST(0x47, 0x06, SS_RDEF,	/* XXX TBD */
2368 	    "PHY test function in progress") },
2369 	/* DT PWROMAEBK   */
2370 	{ SST(0x47, 0x7F, SS_RDEF,	/* XXX TBD */
2371 	    "Some commands cleared by iSCSI protocol event") },
2372 	/* DTLPWROMAEBKVF */
2373 	{ SST(0x48, 0x00, SS_RDEF,
2374 	    "Initiator detected error message received") },
2375 	/* DTLPWROMAEBKVF */
2376 	{ SST(0x49, 0x00, SS_RDEF,
2377 	    "Invalid message error") },
2378 	/* DTLPWROMAEBKVF */
2379 	{ SST(0x4A, 0x00, SS_RDEF,
2380 	    "Command phase error") },
2381 	/* DTLPWROMAEBKVF */
2382 	{ SST(0x4B, 0x00, SS_RDEF,
2383 	    "Data phase error") },
2384 	/* DT PWROMAEBK   */
2385 	{ SST(0x4B, 0x01, SS_RDEF,	/* XXX TBD */
2386 	    "Invalid target port transfer tag received") },
2387 	/* DT PWROMAEBK   */
2388 	{ SST(0x4B, 0x02, SS_RDEF,	/* XXX TBD */
2389 	    "Too much write data") },
2390 	/* DT PWROMAEBK   */
2391 	{ SST(0x4B, 0x03, SS_RDEF,	/* XXX TBD */
2392 	    "ACK/NAK timeout") },
2393 	/* DT PWROMAEBK   */
2394 	{ SST(0x4B, 0x04, SS_RDEF,	/* XXX TBD */
2395 	    "NAK received") },
2396 	/* DT PWROMAEBK   */
2397 	{ SST(0x4B, 0x05, SS_RDEF,	/* XXX TBD */
2398 	    "Data offset error") },
2399 	/* DT PWROMAEBK   */
2400 	{ SST(0x4B, 0x06, SS_RDEF,	/* XXX TBD */
2401 	    "Initiator response timeout") },
2402 	/* DT PWROMAEBK F */
2403 	{ SST(0x4B, 0x07, SS_RDEF,	/* XXX TBD */
2404 	    "Connection lost") },
2405 	/* DT PWROMAEBK F */
2406 	{ SST(0x4B, 0x08, SS_RDEF,	/* XXX TBD */
2407 	    "Data-in buffer overflow - data buffer size") },
2408 	/* DT PWROMAEBK F */
2409 	{ SST(0x4B, 0x09, SS_RDEF,	/* XXX TBD */
2410 	    "Data-in buffer overflow - data buffer descriptor area") },
2411 	/* DT PWROMAEBK F */
2412 	{ SST(0x4B, 0x0A, SS_RDEF,	/* XXX TBD */
2413 	    "Data-in buffer error") },
2414 	/* DT PWROMAEBK F */
2415 	{ SST(0x4B, 0x0B, SS_RDEF,	/* XXX TBD */
2416 	    "Data-out buffer overflow - data buffer size") },
2417 	/* DT PWROMAEBK F */
2418 	{ SST(0x4B, 0x0C, SS_RDEF,	/* XXX TBD */
2419 	    "Data-out buffer overflow - data buffer descriptor area") },
2420 	/* DT PWROMAEBK F */
2421 	{ SST(0x4B, 0x0D, SS_RDEF,	/* XXX TBD */
2422 	    "Data-out buffer error") },
2423 	/* DT PWROMAEBK F */
2424 	{ SST(0x4B, 0x0E, SS_RDEF,	/* XXX TBD */
2425 	    "PCIe fabric error") },
2426 	/* DT PWROMAEBK F */
2427 	{ SST(0x4B, 0x0F, SS_RDEF,	/* XXX TBD */
2428 	    "PCIe completion timeout") },
2429 	/* DT PWROMAEBK F */
2430 	{ SST(0x4B, 0x10, SS_RDEF,	/* XXX TBD */
2431 	    "PCIe completer abort") },
2432 	/* DT PWROMAEBK F */
2433 	{ SST(0x4B, 0x11, SS_RDEF,	/* XXX TBD */
2434 	    "PCIe poisoned TLP received") },
2435 	/* DT PWROMAEBK F */
2436 	{ SST(0x4B, 0x12, SS_RDEF,	/* XXX TBD */
2437 	    "PCIe ECRC check failed") },
2438 	/* DT PWROMAEBK F */
2439 	{ SST(0x4B, 0x13, SS_RDEF,	/* XXX TBD */
2440 	    "PCIe unsupported request") },
2441 	/* DT PWROMAEBK F */
2442 	{ SST(0x4B, 0x14, SS_RDEF,	/* XXX TBD */
2443 	    "PCIe ACS violation") },
2444 	/* DT PWROMAEBK F */
2445 	{ SST(0x4B, 0x15, SS_RDEF,	/* XXX TBD */
2446 	    "PCIe TLP prefix blocket") },
2447 	/* DTLPWROMAEBKVF */
2448 	{ SST(0x4C, 0x00, SS_RDEF,
2449 	    "Logical unit failed self-configuration") },
2450 	/* DTLPWROMAEBKVF */
2451 	{ SST(0x4D, 0x00, SS_RDEF,
2452 	    "Tagged overlapped commands: ASCQ = Queue tag ID") },
2453 	/* DTLPWROMAEBKVF */
2454 	{ SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
2455 	    NULL) },			/* Range 0x00->0xFF */
2456 	/* DTLPWROMAEBKVF */
2457 	{ SST(0x4E, 0x00, SS_RDEF,
2458 	    "Overlapped commands attempted") },
2459 	/*  T             */
2460 	{ SST(0x50, 0x00, SS_RDEF,
2461 	    "Write append error") },
2462 	/*  T             */
2463 	{ SST(0x50, 0x01, SS_RDEF,
2464 	    "Write append position error") },
2465 	/*  T             */
2466 	{ SST(0x50, 0x02, SS_RDEF,
2467 	    "Position error related to timing") },
2468 	/*  T   RO        */
2469 	{ SST(0x51, 0x00, SS_RDEF,
2470 	    "Erase failure") },
2471 	/*      R         */
2472 	{ SST(0x51, 0x01, SS_RDEF,	/* XXX TBD */
2473 	    "Erase failure - incomplete erase operation detected") },
2474 	/*  T             */
2475 	{ SST(0x52, 0x00, SS_RDEF,
2476 	    "Cartridge fault") },
2477 	/* DTL WROM  BK   */
2478 	{ SST(0x53, 0x00, SS_RDEF,
2479 	    "Media load or eject failed") },
2480 	/*  T             */
2481 	{ SST(0x53, 0x01, SS_RDEF,
2482 	    "Unload tape failure") },
2483 	/* DT  WROM  BK   */
2484 	{ SST(0x53, 0x02, SS_RDEF,
2485 	    "Medium removal prevented") },
2486 	/*        M       */
2487 	{ SST(0x53, 0x03, SS_RDEF,	/* XXX TBD */
2488 	    "Medium removal prevented by data transfer element") },
2489 	/*  T             */
2490 	{ SST(0x53, 0x04, SS_RDEF,	/* XXX TBD */
2491 	    "Medium thread or unthread failure") },
2492 	/*        M       */
2493 	{ SST(0x53, 0x05, SS_RDEF,	/* XXX TBD */
2494 	    "Volume identifier invalid") },
2495 	/*  T             */
2496 	{ SST(0x53, 0x06, SS_RDEF,	/* XXX TBD */
2497 	    "Volume identifier missing") },
2498 	/*        M       */
2499 	{ SST(0x53, 0x07, SS_RDEF,	/* XXX TBD */
2500 	    "Duplicate volume identifier") },
2501 	/*        M       */
2502 	{ SST(0x53, 0x08, SS_RDEF,	/* XXX TBD */
2503 	    "Element status unknown") },
2504 	/*        M       */
2505 	{ SST(0x53, 0x09, SS_RDEF,	/* XXX TBD */
2506 	    "Data transfer device error - load failed") },
2507 	/*        M       */
2508 	{ SST(0x53, 0x0A, SS_RDEF,	/* XXX TBD */
2509 	    "Data transfer device error - unload failed") },
2510 	/*        M       */
2511 	{ SST(0x53, 0x0B, SS_RDEF,	/* XXX TBD */
2512 	    "Data transfer device error - unload missing") },
2513 	/*        M       */
2514 	{ SST(0x53, 0x0C, SS_RDEF,	/* XXX TBD */
2515 	    "Data transfer device error - eject failed") },
2516 	/*        M       */
2517 	{ SST(0x53, 0x0D, SS_RDEF,	/* XXX TBD */
2518 	    "Data transfer device error - library communication failed") },
2519 	/*    P           */
2520 	{ SST(0x54, 0x00, SS_RDEF,
2521 	    "SCSI to host system interface failure") },
2522 	/*    P           */
2523 	{ SST(0x55, 0x00, SS_RDEF,
2524 	    "System resource failure") },
2525 	/* D     O   BK   */
2526 	{ SST(0x55, 0x01, SS_FATAL | ENOSPC,
2527 	    "System buffer full") },
2528 	/* DTLPWROMAE K   */
2529 	{ SST(0x55, 0x02, SS_RDEF,	/* XXX TBD */
2530 	    "Insufficient reservation resources") },
2531 	/* DTLPWROMAE K   */
2532 	{ SST(0x55, 0x03, SS_RDEF,	/* XXX TBD */
2533 	    "Insufficient resources") },
2534 	/* DTLPWROMAE K   */
2535 	{ SST(0x55, 0x04, SS_RDEF,	/* XXX TBD */
2536 	    "Insufficient registration resources") },
2537 	/* DT PWROMAEBK   */
2538 	{ SST(0x55, 0x05, SS_RDEF,	/* XXX TBD */
2539 	    "Insufficient access control resources") },
2540 	/* DT  WROM  B    */
2541 	{ SST(0x55, 0x06, SS_RDEF,	/* XXX TBD */
2542 	    "Auxiliary memory out of space") },
2543 	/*              F */
2544 	{ SST(0x55, 0x07, SS_RDEF,	/* XXX TBD */
2545 	    "Quota error") },
2546 	/*  T             */
2547 	{ SST(0x55, 0x08, SS_RDEF,	/* XXX TBD */
2548 	    "Maximum number of supplemental decryption keys exceeded") },
2549 	/*        M       */
2550 	{ SST(0x55, 0x09, SS_RDEF,	/* XXX TBD */
2551 	    "Medium auxiliary memory not accessible") },
2552 	/*        M       */
2553 	{ SST(0x55, 0x0A, SS_RDEF,	/* XXX TBD */
2554 	    "Data currently unavailable") },
2555 	/* DTLPWROMAEBKVF */
2556 	{ SST(0x55, 0x0B, SS_RDEF,	/* XXX TBD */
2557 	    "Insufficient power for operation") },
2558 	/* DT P      B    */
2559 	{ SST(0x55, 0x0C, SS_RDEF,	/* XXX TBD */
2560 	    "Insufficient resources to create ROD") },
2561 	/* DT P      B    */
2562 	{ SST(0x55, 0x0D, SS_RDEF,	/* XXX TBD */
2563 	    "Insufficient resources to create ROD token") },
2564 	/* D              */
2565 	{ SST(0x55, 0x0E, SS_RDEF,	/* XXX TBD */
2566 	    "Insufficient zone resources") },
2567 	/* D              */
2568 	{ SST(0x55, 0x0F, SS_RDEF,	/* XXX TBD */
2569 	    "Insufficient zone resources to complete write") },
2570 	/* D              */
2571 	{ SST(0x55, 0x10, SS_RDEF,	/* XXX TBD */
2572 	    "Maximum number of streams open") },
2573 	/*      R         */
2574 	{ SST(0x57, 0x00, SS_RDEF,
2575 	    "Unable to recover table-of-contents") },
2576 	/*       O        */
2577 	{ SST(0x58, 0x00, SS_RDEF,
2578 	    "Generation does not exist") },
2579 	/*       O        */
2580 	{ SST(0x59, 0x00, SS_RDEF,
2581 	    "Updated block read") },
2582 	/* DTLPWRO   BK   */
2583 	{ SST(0x5A, 0x00, SS_RDEF,
2584 	    "Operator request or state change input") },
2585 	/* DT  WROM  BK   */
2586 	{ SST(0x5A, 0x01, SS_RDEF,
2587 	    "Operator medium removal request") },
2588 	/* DT  WRO A BK   */
2589 	{ SST(0x5A, 0x02, SS_RDEF,
2590 	    "Operator selected write protect") },
2591 	/* DT  WRO A BK   */
2592 	{ SST(0x5A, 0x03, SS_RDEF,
2593 	    "Operator selected write permit") },
2594 	/* DTLPWROM   K   */
2595 	{ SST(0x5B, 0x00, SS_RDEF,
2596 	    "Log exception") },
2597 	/* DTLPWROM   K   */
2598 	{ SST(0x5B, 0x01, SS_RDEF,
2599 	    "Threshold condition met") },
2600 	/* DTLPWROM   K   */
2601 	{ SST(0x5B, 0x02, SS_RDEF,
2602 	    "Log counter at maximum") },
2603 	/* DTLPWROM   K   */
2604 	{ SST(0x5B, 0x03, SS_RDEF,
2605 	    "Log list codes exhausted") },
2606 	/* D     O        */
2607 	{ SST(0x5C, 0x00, SS_RDEF,
2608 	    "RPL status change") },
2609 	/* D     O        */
2610 	{ SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2611 	    "Spindles synchronized") },
2612 	/* D     O        */
2613 	{ SST(0x5C, 0x02, SS_RDEF,
2614 	    "Spindles not synchronized") },
2615 	/* DTLPWROMAEBKVF */
2616 	{ SST(0x5D, 0x00, SS_RDEF,
2617 	    "Failure prediction threshold exceeded") },
2618 	/*      R    B    */
2619 	{ SST(0x5D, 0x01, SS_RDEF,	/* XXX TBD */
2620 	    "Media failure prediction threshold exceeded") },
2621 	/*      R         */
2622 	{ SST(0x5D, 0x02, SS_RDEF,	/* XXX TBD */
2623 	    "Logical unit failure prediction threshold exceeded") },
2624 	/*      R         */
2625 	{ SST(0x5D, 0x03, SS_RDEF,	/* XXX TBD */
2626 	    "Spare area exhaustion prediction threshold exceeded") },
2627 	/* D         B    */
2628 	{ SST(0x5D, 0x10, SS_RDEF,	/* XXX TBD */
2629 	    "Hardware impending failure general hard drive failure") },
2630 	/* D         B    */
2631 	{ SST(0x5D, 0x11, SS_RDEF,	/* XXX TBD */
2632 	    "Hardware impending failure drive error rate too high") },
2633 	/* D         B    */
2634 	{ SST(0x5D, 0x12, SS_RDEF,	/* XXX TBD */
2635 	    "Hardware impending failure data error rate too high") },
2636 	/* D         B    */
2637 	{ SST(0x5D, 0x13, SS_RDEF,	/* XXX TBD */
2638 	    "Hardware impending failure seek error rate too high") },
2639 	/* D         B    */
2640 	{ SST(0x5D, 0x14, SS_RDEF,	/* XXX TBD */
2641 	    "Hardware impending failure too many block reassigns") },
2642 	/* D         B    */
2643 	{ SST(0x5D, 0x15, SS_RDEF,	/* XXX TBD */
2644 	    "Hardware impending failure access times too high") },
2645 	/* D         B    */
2646 	{ SST(0x5D, 0x16, SS_RDEF,	/* XXX TBD */
2647 	    "Hardware impending failure start unit times too high") },
2648 	/* D         B    */
2649 	{ SST(0x5D, 0x17, SS_RDEF,	/* XXX TBD */
2650 	    "Hardware impending failure channel parametrics") },
2651 	/* D         B    */
2652 	{ SST(0x5D, 0x18, SS_RDEF,	/* XXX TBD */
2653 	    "Hardware impending failure controller detected") },
2654 	/* D         B    */
2655 	{ SST(0x5D, 0x19, SS_RDEF,	/* XXX TBD */
2656 	    "Hardware impending failure throughput performance") },
2657 	/* D         B    */
2658 	{ SST(0x5D, 0x1A, SS_RDEF,	/* XXX TBD */
2659 	    "Hardware impending failure seek time performance") },
2660 	/* D         B    */
2661 	{ SST(0x5D, 0x1B, SS_RDEF,	/* XXX TBD */
2662 	    "Hardware impending failure spin-up retry count") },
2663 	/* D         B    */
2664 	{ SST(0x5D, 0x1C, SS_RDEF,	/* XXX TBD */
2665 	    "Hardware impending failure drive calibration retry count") },
2666 	/* D         B    */
2667 	{ SST(0x5D, 0x20, SS_RDEF,	/* XXX TBD */
2668 	    "Controller impending failure general hard drive failure") },
2669 	/* D         B    */
2670 	{ SST(0x5D, 0x21, SS_RDEF,	/* XXX TBD */
2671 	    "Controller impending failure drive error rate too high") },
2672 	/* D         B    */
2673 	{ SST(0x5D, 0x22, SS_RDEF,	/* XXX TBD */
2674 	    "Controller impending failure data error rate too high") },
2675 	/* D         B    */
2676 	{ SST(0x5D, 0x23, SS_RDEF,	/* XXX TBD */
2677 	    "Controller impending failure seek error rate too high") },
2678 	/* D         B    */
2679 	{ SST(0x5D, 0x24, SS_RDEF,	/* XXX TBD */
2680 	    "Controller impending failure too many block reassigns") },
2681 	/* D         B    */
2682 	{ SST(0x5D, 0x25, SS_RDEF,	/* XXX TBD */
2683 	    "Controller impending failure access times too high") },
2684 	/* D         B    */
2685 	{ SST(0x5D, 0x26, SS_RDEF,	/* XXX TBD */
2686 	    "Controller impending failure start unit times too high") },
2687 	/* D         B    */
2688 	{ SST(0x5D, 0x27, SS_RDEF,	/* XXX TBD */
2689 	    "Controller impending failure channel parametrics") },
2690 	/* D         B    */
2691 	{ SST(0x5D, 0x28, SS_RDEF,	/* XXX TBD */
2692 	    "Controller impending failure controller detected") },
2693 	/* D         B    */
2694 	{ SST(0x5D, 0x29, SS_RDEF,	/* XXX TBD */
2695 	    "Controller impending failure throughput performance") },
2696 	/* D         B    */
2697 	{ SST(0x5D, 0x2A, SS_RDEF,	/* XXX TBD */
2698 	    "Controller impending failure seek time performance") },
2699 	/* D         B    */
2700 	{ SST(0x5D, 0x2B, SS_RDEF,	/* XXX TBD */
2701 	    "Controller impending failure spin-up retry count") },
2702 	/* D         B    */
2703 	{ SST(0x5D, 0x2C, SS_RDEF,	/* XXX TBD */
2704 	    "Controller impending failure drive calibration retry count") },
2705 	/* D         B    */
2706 	{ SST(0x5D, 0x30, SS_RDEF,	/* XXX TBD */
2707 	    "Data channel impending failure general hard drive failure") },
2708 	/* D         B    */
2709 	{ SST(0x5D, 0x31, SS_RDEF,	/* XXX TBD */
2710 	    "Data channel impending failure drive error rate too high") },
2711 	/* D         B    */
2712 	{ SST(0x5D, 0x32, SS_RDEF,	/* XXX TBD */
2713 	    "Data channel impending failure data error rate too high") },
2714 	/* D         B    */
2715 	{ SST(0x5D, 0x33, SS_RDEF,	/* XXX TBD */
2716 	    "Data channel impending failure seek error rate too high") },
2717 	/* D         B    */
2718 	{ SST(0x5D, 0x34, SS_RDEF,	/* XXX TBD */
2719 	    "Data channel impending failure too many block reassigns") },
2720 	/* D         B    */
2721 	{ SST(0x5D, 0x35, SS_RDEF,	/* XXX TBD */
2722 	    "Data channel impending failure access times too high") },
2723 	/* D         B    */
2724 	{ SST(0x5D, 0x36, SS_RDEF,	/* XXX TBD */
2725 	    "Data channel impending failure start unit times too high") },
2726 	/* D         B    */
2727 	{ SST(0x5D, 0x37, SS_RDEF,	/* XXX TBD */
2728 	    "Data channel impending failure channel parametrics") },
2729 	/* D         B    */
2730 	{ SST(0x5D, 0x38, SS_RDEF,	/* XXX TBD */
2731 	    "Data channel impending failure controller detected") },
2732 	/* D         B    */
2733 	{ SST(0x5D, 0x39, SS_RDEF,	/* XXX TBD */
2734 	    "Data channel impending failure throughput performance") },
2735 	/* D         B    */
2736 	{ SST(0x5D, 0x3A, SS_RDEF,	/* XXX TBD */
2737 	    "Data channel impending failure seek time performance") },
2738 	/* D         B    */
2739 	{ SST(0x5D, 0x3B, SS_RDEF,	/* XXX TBD */
2740 	    "Data channel impending failure spin-up retry count") },
2741 	/* D         B    */
2742 	{ SST(0x5D, 0x3C, SS_RDEF,	/* XXX TBD */
2743 	    "Data channel impending failure drive calibration retry count") },
2744 	/* D         B    */
2745 	{ SST(0x5D, 0x40, SS_RDEF,	/* XXX TBD */
2746 	    "Servo impending failure general hard drive failure") },
2747 	/* D         B    */
2748 	{ SST(0x5D, 0x41, SS_RDEF,	/* XXX TBD */
2749 	    "Servo impending failure drive error rate too high") },
2750 	/* D         B    */
2751 	{ SST(0x5D, 0x42, SS_RDEF,	/* XXX TBD */
2752 	    "Servo impending failure data error rate too high") },
2753 	/* D         B    */
2754 	{ SST(0x5D, 0x43, SS_RDEF,	/* XXX TBD */
2755 	    "Servo impending failure seek error rate too high") },
2756 	/* D         B    */
2757 	{ SST(0x5D, 0x44, SS_RDEF,	/* XXX TBD */
2758 	    "Servo impending failure too many block reassigns") },
2759 	/* D         B    */
2760 	{ SST(0x5D, 0x45, SS_RDEF,	/* XXX TBD */
2761 	    "Servo impending failure access times too high") },
2762 	/* D         B    */
2763 	{ SST(0x5D, 0x46, SS_RDEF,	/* XXX TBD */
2764 	    "Servo impending failure start unit times too high") },
2765 	/* D         B    */
2766 	{ SST(0x5D, 0x47, SS_RDEF,	/* XXX TBD */
2767 	    "Servo impending failure channel parametrics") },
2768 	/* D         B    */
2769 	{ SST(0x5D, 0x48, SS_RDEF,	/* XXX TBD */
2770 	    "Servo impending failure controller detected") },
2771 	/* D         B    */
2772 	{ SST(0x5D, 0x49, SS_RDEF,	/* XXX TBD */
2773 	    "Servo impending failure throughput performance") },
2774 	/* D         B    */
2775 	{ SST(0x5D, 0x4A, SS_RDEF,	/* XXX TBD */
2776 	    "Servo impending failure seek time performance") },
2777 	/* D         B    */
2778 	{ SST(0x5D, 0x4B, SS_RDEF,	/* XXX TBD */
2779 	    "Servo impending failure spin-up retry count") },
2780 	/* D         B    */
2781 	{ SST(0x5D, 0x4C, SS_RDEF,	/* XXX TBD */
2782 	    "Servo impending failure drive calibration retry count") },
2783 	/* D         B    */
2784 	{ SST(0x5D, 0x50, SS_RDEF,	/* XXX TBD */
2785 	    "Spindle impending failure general hard drive failure") },
2786 	/* D         B    */
2787 	{ SST(0x5D, 0x51, SS_RDEF,	/* XXX TBD */
2788 	    "Spindle impending failure drive error rate too high") },
2789 	/* D         B    */
2790 	{ SST(0x5D, 0x52, SS_RDEF,	/* XXX TBD */
2791 	    "Spindle impending failure data error rate too high") },
2792 	/* D         B    */
2793 	{ SST(0x5D, 0x53, SS_RDEF,	/* XXX TBD */
2794 	    "Spindle impending failure seek error rate too high") },
2795 	/* D         B    */
2796 	{ SST(0x5D, 0x54, SS_RDEF,	/* XXX TBD */
2797 	    "Spindle impending failure too many block reassigns") },
2798 	/* D         B    */
2799 	{ SST(0x5D, 0x55, SS_RDEF,	/* XXX TBD */
2800 	    "Spindle impending failure access times too high") },
2801 	/* D         B    */
2802 	{ SST(0x5D, 0x56, SS_RDEF,	/* XXX TBD */
2803 	    "Spindle impending failure start unit times too high") },
2804 	/* D         B    */
2805 	{ SST(0x5D, 0x57, SS_RDEF,	/* XXX TBD */
2806 	    "Spindle impending failure channel parametrics") },
2807 	/* D         B    */
2808 	{ SST(0x5D, 0x58, SS_RDEF,	/* XXX TBD */
2809 	    "Spindle impending failure controller detected") },
2810 	/* D         B    */
2811 	{ SST(0x5D, 0x59, SS_RDEF,	/* XXX TBD */
2812 	    "Spindle impending failure throughput performance") },
2813 	/* D         B    */
2814 	{ SST(0x5D, 0x5A, SS_RDEF,	/* XXX TBD */
2815 	    "Spindle impending failure seek time performance") },
2816 	/* D         B    */
2817 	{ SST(0x5D, 0x5B, SS_RDEF,	/* XXX TBD */
2818 	    "Spindle impending failure spin-up retry count") },
2819 	/* D         B    */
2820 	{ SST(0x5D, 0x5C, SS_RDEF,	/* XXX TBD */
2821 	    "Spindle impending failure drive calibration retry count") },
2822 	/* D         B    */
2823 	{ SST(0x5D, 0x60, SS_RDEF,	/* XXX TBD */
2824 	    "Firmware impending failure general hard drive failure") },
2825 	/* D         B    */
2826 	{ SST(0x5D, 0x61, SS_RDEF,	/* XXX TBD */
2827 	    "Firmware impending failure drive error rate too high") },
2828 	/* D         B    */
2829 	{ SST(0x5D, 0x62, SS_RDEF,	/* XXX TBD */
2830 	    "Firmware impending failure data error rate too high") },
2831 	/* D         B    */
2832 	{ SST(0x5D, 0x63, SS_RDEF,	/* XXX TBD */
2833 	    "Firmware impending failure seek error rate too high") },
2834 	/* D         B    */
2835 	{ SST(0x5D, 0x64, SS_RDEF,	/* XXX TBD */
2836 	    "Firmware impending failure too many block reassigns") },
2837 	/* D         B    */
2838 	{ SST(0x5D, 0x65, SS_RDEF,	/* XXX TBD */
2839 	    "Firmware impending failure access times too high") },
2840 	/* D         B    */
2841 	{ SST(0x5D, 0x66, SS_RDEF,	/* XXX TBD */
2842 	    "Firmware impending failure start unit times too high") },
2843 	/* D         B    */
2844 	{ SST(0x5D, 0x67, SS_RDEF,	/* XXX TBD */
2845 	    "Firmware impending failure channel parametrics") },
2846 	/* D         B    */
2847 	{ SST(0x5D, 0x68, SS_RDEF,	/* XXX TBD */
2848 	    "Firmware impending failure controller detected") },
2849 	/* D         B    */
2850 	{ SST(0x5D, 0x69, SS_RDEF,	/* XXX TBD */
2851 	    "Firmware impending failure throughput performance") },
2852 	/* D         B    */
2853 	{ SST(0x5D, 0x6A, SS_RDEF,	/* XXX TBD */
2854 	    "Firmware impending failure seek time performance") },
2855 	/* D         B    */
2856 	{ SST(0x5D, 0x6B, SS_RDEF,	/* XXX TBD */
2857 	    "Firmware impending failure spin-up retry count") },
2858 	/* D         B    */
2859 	{ SST(0x5D, 0x6C, SS_RDEF,	/* XXX TBD */
2860 	    "Firmware impending failure drive calibration retry count") },
2861 	/* DTLPWROMAEBKVF */
2862 	{ SST(0x5D, 0xFF, SS_RDEF,
2863 	    "Failure prediction threshold exceeded (false)") },
2864 	/* DTLPWRO A  K   */
2865 	{ SST(0x5E, 0x00, SS_RDEF,
2866 	    "Low power condition on") },
2867 	/* DTLPWRO A  K   */
2868 	{ SST(0x5E, 0x01, SS_RDEF,
2869 	    "Idle condition activated by timer") },
2870 	/* DTLPWRO A  K   */
2871 	{ SST(0x5E, 0x02, SS_RDEF,
2872 	    "Standby condition activated by timer") },
2873 	/* DTLPWRO A  K   */
2874 	{ SST(0x5E, 0x03, SS_RDEF,
2875 	    "Idle condition activated by command") },
2876 	/* DTLPWRO A  K   */
2877 	{ SST(0x5E, 0x04, SS_RDEF,
2878 	    "Standby condition activated by command") },
2879 	/* DTLPWRO A  K   */
2880 	{ SST(0x5E, 0x05, SS_RDEF,
2881 	    "Idle-B condition activated by timer") },
2882 	/* DTLPWRO A  K   */
2883 	{ SST(0x5E, 0x06, SS_RDEF,
2884 	    "Idle-B condition activated by command") },
2885 	/* DTLPWRO A  K   */
2886 	{ SST(0x5E, 0x07, SS_RDEF,
2887 	    "Idle-C condition activated by timer") },
2888 	/* DTLPWRO A  K   */
2889 	{ SST(0x5E, 0x08, SS_RDEF,
2890 	    "Idle-C condition activated by command") },
2891 	/* DTLPWRO A  K   */
2892 	{ SST(0x5E, 0x09, SS_RDEF,
2893 	    "Standby-Y condition activated by timer") },
2894 	/* DTLPWRO A  K   */
2895 	{ SST(0x5E, 0x0A, SS_RDEF,
2896 	    "Standby-Y condition activated by command") },
2897 	/*           B    */
2898 	{ SST(0x5E, 0x41, SS_RDEF,	/* XXX TBD */
2899 	    "Power state change to active") },
2900 	/*           B    */
2901 	{ SST(0x5E, 0x42, SS_RDEF,	/* XXX TBD */
2902 	    "Power state change to idle") },
2903 	/*           B    */
2904 	{ SST(0x5E, 0x43, SS_RDEF,	/* XXX TBD */
2905 	    "Power state change to standby") },
2906 	/*           B    */
2907 	{ SST(0x5E, 0x45, SS_RDEF,	/* XXX TBD */
2908 	    "Power state change to sleep") },
2909 	/*           BK   */
2910 	{ SST(0x5E, 0x47, SS_RDEF,	/* XXX TBD */
2911 	    "Power state change to device control") },
2912 	/*                */
2913 	{ SST(0x60, 0x00, SS_RDEF,
2914 	    "Lamp failure") },
2915 	/*                */
2916 	{ SST(0x61, 0x00, SS_RDEF,
2917 	    "Video acquisition error") },
2918 	/*                */
2919 	{ SST(0x61, 0x01, SS_RDEF,
2920 	    "Unable to acquire video") },
2921 	/*                */
2922 	{ SST(0x61, 0x02, SS_RDEF,
2923 	    "Out of focus") },
2924 	/*                */
2925 	{ SST(0x62, 0x00, SS_RDEF,
2926 	    "Scan head positioning error") },
2927 	/*      R         */
2928 	{ SST(0x63, 0x00, SS_RDEF,
2929 	    "End of user area encountered on this track") },
2930 	/*      R         */
2931 	{ SST(0x63, 0x01, SS_FATAL | ENOSPC,
2932 	    "Packet does not fit in available space") },
2933 	/*      R         */
2934 	{ SST(0x64, 0x00, SS_FATAL | ENXIO,
2935 	    "Illegal mode for this track") },
2936 	/*      R         */
2937 	{ SST(0x64, 0x01, SS_RDEF,
2938 	    "Invalid packet size") },
2939 	/* DTLPWROMAEBKVF */
2940 	{ SST(0x65, 0x00, SS_RDEF,
2941 	    "Voltage fault") },
2942 	/*                */
2943 	{ SST(0x66, 0x00, SS_RDEF,
2944 	    "Automatic document feeder cover up") },
2945 	/*                */
2946 	{ SST(0x66, 0x01, SS_RDEF,
2947 	    "Automatic document feeder lift up") },
2948 	/*                */
2949 	{ SST(0x66, 0x02, SS_RDEF,
2950 	    "Document jam in automatic document feeder") },
2951 	/*                */
2952 	{ SST(0x66, 0x03, SS_RDEF,
2953 	    "Document miss feed automatic in document feeder") },
2954 	/*         A      */
2955 	{ SST(0x67, 0x00, SS_RDEF,
2956 	    "Configuration failure") },
2957 	/*         A      */
2958 	{ SST(0x67, 0x01, SS_RDEF,
2959 	    "Configuration of incapable logical units failed") },
2960 	/*         A      */
2961 	{ SST(0x67, 0x02, SS_RDEF,
2962 	    "Add logical unit failed") },
2963 	/*         A      */
2964 	{ SST(0x67, 0x03, SS_RDEF,
2965 	    "Modification of logical unit failed") },
2966 	/*         A      */
2967 	{ SST(0x67, 0x04, SS_RDEF,
2968 	    "Exchange of logical unit failed") },
2969 	/*         A      */
2970 	{ SST(0x67, 0x05, SS_RDEF,
2971 	    "Remove of logical unit failed") },
2972 	/*         A      */
2973 	{ SST(0x67, 0x06, SS_RDEF,
2974 	    "Attachment of logical unit failed") },
2975 	/*         A      */
2976 	{ SST(0x67, 0x07, SS_RDEF,
2977 	    "Creation of logical unit failed") },
2978 	/*         A      */
2979 	{ SST(0x67, 0x08, SS_RDEF,	/* XXX TBD */
2980 	    "Assign failure occurred") },
2981 	/*         A      */
2982 	{ SST(0x67, 0x09, SS_RDEF,	/* XXX TBD */
2983 	    "Multiply assigned logical unit") },
2984 	/* DTLPWROMAEBKVF */
2985 	{ SST(0x67, 0x0A, SS_RDEF,	/* XXX TBD */
2986 	    "Set target port groups command failed") },
2987 	/* DT        B    */
2988 	{ SST(0x67, 0x0B, SS_RDEF,	/* XXX TBD */
2989 	    "ATA device feature not enabled") },
2990 	/*         A      */
2991 	{ SST(0x68, 0x00, SS_RDEF,
2992 	    "Logical unit not configured") },
2993 	/* D              */
2994 	{ SST(0x68, 0x01, SS_RDEF,
2995 	    "Subsidiary logical unit not configured") },
2996 	/*         A      */
2997 	{ SST(0x69, 0x00, SS_RDEF,
2998 	    "Data loss on logical unit") },
2999 	/*         A      */
3000 	{ SST(0x69, 0x01, SS_RDEF,
3001 	    "Multiple logical unit failures") },
3002 	/*         A      */
3003 	{ SST(0x69, 0x02, SS_RDEF,
3004 	    "Parity/data mismatch") },
3005 	/*         A      */
3006 	{ SST(0x6A, 0x00, SS_RDEF,
3007 	    "Informational, refer to log") },
3008 	/*         A      */
3009 	{ SST(0x6B, 0x00, SS_RDEF,
3010 	    "State change has occurred") },
3011 	/*         A      */
3012 	{ SST(0x6B, 0x01, SS_RDEF,
3013 	    "Redundancy level got better") },
3014 	/*         A      */
3015 	{ SST(0x6B, 0x02, SS_RDEF,
3016 	    "Redundancy level got worse") },
3017 	/*         A      */
3018 	{ SST(0x6C, 0x00, SS_RDEF,
3019 	    "Rebuild failure occurred") },
3020 	/*         A      */
3021 	{ SST(0x6D, 0x00, SS_RDEF,
3022 	    "Recalculate failure occurred") },
3023 	/*         A      */
3024 	{ SST(0x6E, 0x00, SS_RDEF,
3025 	    "Command to logical unit failed") },
3026 	/*      R         */
3027 	{ SST(0x6F, 0x00, SS_RDEF,	/* XXX TBD */
3028 	    "Copy protection key exchange failure - authentication failure") },
3029 	/*      R         */
3030 	{ SST(0x6F, 0x01, SS_RDEF,	/* XXX TBD */
3031 	    "Copy protection key exchange failure - key not present") },
3032 	/*      R         */
3033 	{ SST(0x6F, 0x02, SS_RDEF,	/* XXX TBD */
3034 	    "Copy protection key exchange failure - key not established") },
3035 	/*      R         */
3036 	{ SST(0x6F, 0x03, SS_RDEF,	/* XXX TBD */
3037 	    "Read of scrambled sector without authentication") },
3038 	/*      R         */
3039 	{ SST(0x6F, 0x04, SS_RDEF,	/* XXX TBD */
3040 	    "Media region code is mismatched to logical unit region") },
3041 	/*      R         */
3042 	{ SST(0x6F, 0x05, SS_RDEF,	/* XXX TBD */
3043 	    "Drive region must be permanent/region reset count error") },
3044 	/*      R         */
3045 	{ SST(0x6F, 0x06, SS_RDEF,	/* XXX TBD */
3046 	    "Insufficient block count for binding NONCE recording") },
3047 	/*      R         */
3048 	{ SST(0x6F, 0x07, SS_RDEF,	/* XXX TBD */
3049 	    "Conflict in binding NONCE recording") },
3050 	/*  T             */
3051 	{ SST(0x70, 0x00, SS_RDEF,
3052 	    "Decompression exception short: ASCQ = Algorithm ID") },
3053 	/*  T             */
3054 	{ SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
3055 	    NULL) },			/* Range 0x00 -> 0xFF */
3056 	/*  T             */
3057 	{ SST(0x71, 0x00, SS_RDEF,
3058 	    "Decompression exception long: ASCQ = Algorithm ID") },
3059 	/*  T             */
3060 	{ SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
3061 	    NULL) },			/* Range 0x00 -> 0xFF */
3062 	/*      R         */
3063 	{ SST(0x72, 0x00, SS_RDEF,
3064 	    "Session fixation error") },
3065 	/*      R         */
3066 	{ SST(0x72, 0x01, SS_RDEF,
3067 	    "Session fixation error writing lead-in") },
3068 	/*      R         */
3069 	{ SST(0x72, 0x02, SS_RDEF,
3070 	    "Session fixation error writing lead-out") },
3071 	/*      R         */
3072 	{ SST(0x72, 0x03, SS_RDEF,
3073 	    "Session fixation error - incomplete track in session") },
3074 	/*      R         */
3075 	{ SST(0x72, 0x04, SS_RDEF,
3076 	    "Empty or partially written reserved track") },
3077 	/*      R         */
3078 	{ SST(0x72, 0x05, SS_RDEF,	/* XXX TBD */
3079 	    "No more track reservations allowed") },
3080 	/*      R         */
3081 	{ SST(0x72, 0x06, SS_RDEF,	/* XXX TBD */
3082 	    "RMZ extension is not allowed") },
3083 	/*      R         */
3084 	{ SST(0x72, 0x07, SS_RDEF,	/* XXX TBD */
3085 	    "No more test zone extensions are allowed") },
3086 	/*      R         */
3087 	{ SST(0x73, 0x00, SS_RDEF,
3088 	    "CD control error") },
3089 	/*      R         */
3090 	{ SST(0x73, 0x01, SS_RDEF,
3091 	    "Power calibration area almost full") },
3092 	/*      R         */
3093 	{ SST(0x73, 0x02, SS_FATAL | ENOSPC,
3094 	    "Power calibration area is full") },
3095 	/*      R         */
3096 	{ SST(0x73, 0x03, SS_RDEF,
3097 	    "Power calibration area error") },
3098 	/*      R         */
3099 	{ SST(0x73, 0x04, SS_RDEF,
3100 	    "Program memory area update failure") },
3101 	/*      R         */
3102 	{ SST(0x73, 0x05, SS_RDEF,
3103 	    "Program memory area is full") },
3104 	/*      R         */
3105 	{ SST(0x73, 0x06, SS_RDEF,	/* XXX TBD */
3106 	    "RMA/PMA is almost full") },
3107 	/*      R         */
3108 	{ SST(0x73, 0x10, SS_RDEF,	/* XXX TBD */
3109 	    "Current power calibration area almost full") },
3110 	/*      R         */
3111 	{ SST(0x73, 0x11, SS_RDEF,	/* XXX TBD */
3112 	    "Current power calibration area is full") },
3113 	/*      R         */
3114 	{ SST(0x73, 0x17, SS_RDEF,	/* XXX TBD */
3115 	    "RDZ is full") },
3116 	/*  T             */
3117 	{ SST(0x74, 0x00, SS_RDEF,	/* XXX TBD */
3118 	    "Security error") },
3119 	/*  T             */
3120 	{ SST(0x74, 0x01, SS_RDEF,	/* XXX TBD */
3121 	    "Unable to decrypt data") },
3122 	/*  T             */
3123 	{ SST(0x74, 0x02, SS_RDEF,	/* XXX TBD */
3124 	    "Unencrypted data encountered while decrypting") },
3125 	/*  T             */
3126 	{ SST(0x74, 0x03, SS_RDEF,	/* XXX TBD */
3127 	    "Incorrect data encryption key") },
3128 	/*  T             */
3129 	{ SST(0x74, 0x04, SS_RDEF,	/* XXX TBD */
3130 	    "Cryptographic integrity validation failed") },
3131 	/*  T             */
3132 	{ SST(0x74, 0x05, SS_RDEF,	/* XXX TBD */
3133 	    "Error decrypting data") },
3134 	/*  T             */
3135 	{ SST(0x74, 0x06, SS_RDEF,	/* XXX TBD */
3136 	    "Unknown signature verification key") },
3137 	/*  T             */
3138 	{ SST(0x74, 0x07, SS_RDEF,	/* XXX TBD */
3139 	    "Encryption parameters not useable") },
3140 	/* DT   R M E  VF */
3141 	{ SST(0x74, 0x08, SS_RDEF,	/* XXX TBD */
3142 	    "Digital signature validation failure") },
3143 	/*  T             */
3144 	{ SST(0x74, 0x09, SS_RDEF,	/* XXX TBD */
3145 	    "Encryption mode mismatch on read") },
3146 	/*  T             */
3147 	{ SST(0x74, 0x0A, SS_RDEF,	/* XXX TBD */
3148 	    "Encrypted block not raw read enabled") },
3149 	/*  T             */
3150 	{ SST(0x74, 0x0B, SS_RDEF,	/* XXX TBD */
3151 	    "Incorrect encryption parameters") },
3152 	/* DT   R MAEBKV  */
3153 	{ SST(0x74, 0x0C, SS_RDEF,	/* XXX TBD */
3154 	    "Unable to decrypt parameter list") },
3155 	/*  T             */
3156 	{ SST(0x74, 0x0D, SS_RDEF,	/* XXX TBD */
3157 	    "Encryption algorithm disabled") },
3158 	/* DT   R MAEBKV  */
3159 	{ SST(0x74, 0x10, SS_RDEF,	/* XXX TBD */
3160 	    "SA creation parameter value invalid") },
3161 	/* DT   R MAEBKV  */
3162 	{ SST(0x74, 0x11, SS_RDEF,	/* XXX TBD */
3163 	    "SA creation parameter value rejected") },
3164 	/* DT   R MAEBKV  */
3165 	{ SST(0x74, 0x12, SS_RDEF,	/* XXX TBD */
3166 	    "Invalid SA usage") },
3167 	/*  T             */
3168 	{ SST(0x74, 0x21, SS_RDEF,	/* XXX TBD */
3169 	    "Data encryption configuration prevented") },
3170 	/* DT   R MAEBKV  */
3171 	{ SST(0x74, 0x30, SS_RDEF,	/* XXX TBD */
3172 	    "SA creation parameter not supported") },
3173 	/* DT   R MAEBKV  */
3174 	{ SST(0x74, 0x40, SS_RDEF,	/* XXX TBD */
3175 	    "Authentication failed") },
3176 	/*             V  */
3177 	{ SST(0x74, 0x61, SS_RDEF,	/* XXX TBD */
3178 	    "External data encryption key manager access error") },
3179 	/*             V  */
3180 	{ SST(0x74, 0x62, SS_RDEF,	/* XXX TBD */
3181 	    "External data encryption key manager error") },
3182 	/*             V  */
3183 	{ SST(0x74, 0x63, SS_RDEF,	/* XXX TBD */
3184 	    "External data encryption key not found") },
3185 	/*             V  */
3186 	{ SST(0x74, 0x64, SS_RDEF,	/* XXX TBD */
3187 	    "External data encryption request not authorized") },
3188 	/*  T             */
3189 	{ SST(0x74, 0x6E, SS_RDEF,	/* XXX TBD */
3190 	    "External data encryption control timeout") },
3191 	/*  T             */
3192 	{ SST(0x74, 0x6F, SS_RDEF,	/* XXX TBD */
3193 	    "External data encryption control error") },
3194 	/* DT   R M E  V  */
3195 	{ SST(0x74, 0x71, SS_RDEF,	/* XXX TBD */
3196 	    "Logical unit access not authorized") },
3197 	/* D              */
3198 	{ SST(0x74, 0x79, SS_RDEF,	/* XXX TBD */
3199 	    "Security conflict in translated device") }
3200 };
3201 
3202 const u_int asc_table_size = nitems(asc_table);
3203 
3204 struct asc_key
3205 {
3206 	int asc;
3207 	int ascq;
3208 };
3209 
3210 static int
3211 ascentrycomp(const void *key, const void *member)
3212 {
3213 	int asc;
3214 	int ascq;
3215 	const struct asc_table_entry *table_entry;
3216 
3217 	asc = ((const struct asc_key *)key)->asc;
3218 	ascq = ((const struct asc_key *)key)->ascq;
3219 	table_entry = (const struct asc_table_entry *)member;
3220 
3221 	if (asc >= table_entry->asc) {
3222 
3223 		if (asc > table_entry->asc)
3224 			return (1);
3225 
3226 		if (ascq <= table_entry->ascq) {
3227 			/* Check for ranges */
3228 			if (ascq == table_entry->ascq
3229 		 	 || ((table_entry->action & SSQ_RANGE) != 0
3230 		  	   && ascq >= (table_entry - 1)->ascq))
3231 				return (0);
3232 			return (-1);
3233 		}
3234 		return (1);
3235 	}
3236 	return (-1);
3237 }
3238 
3239 static int
3240 senseentrycomp(const void *key, const void *member)
3241 {
3242 	int sense_key;
3243 	const struct sense_key_table_entry *table_entry;
3244 
3245 	sense_key = *((const int *)key);
3246 	table_entry = (const struct sense_key_table_entry *)member;
3247 
3248 	if (sense_key >= table_entry->sense_key) {
3249 		if (sense_key == table_entry->sense_key)
3250 			return (0);
3251 		return (1);
3252 	}
3253 	return (-1);
3254 }
3255 
3256 static void
3257 fetchtableentries(int sense_key, int asc, int ascq,
3258 		  struct scsi_inquiry_data *inq_data,
3259 		  const struct sense_key_table_entry **sense_entry,
3260 		  const struct asc_table_entry **asc_entry)
3261 {
3262 	caddr_t match;
3263 	const struct asc_table_entry *asc_tables[2];
3264 	const struct sense_key_table_entry *sense_tables[2];
3265 	struct asc_key asc_ascq;
3266 	size_t asc_tables_size[2];
3267 	size_t sense_tables_size[2];
3268 	int num_asc_tables;
3269 	int num_sense_tables;
3270 	int i;
3271 
3272 	/* Default to failure */
3273 	*sense_entry = NULL;
3274 	*asc_entry = NULL;
3275 	match = NULL;
3276 	if (inq_data != NULL)
3277 		match = cam_quirkmatch((caddr_t)inq_data,
3278 				       (caddr_t)sense_quirk_table,
3279 				       sense_quirk_table_size,
3280 				       sizeof(*sense_quirk_table),
3281 				       scsi_inquiry_match);
3282 
3283 	if (match != NULL) {
3284 		struct scsi_sense_quirk_entry *quirk;
3285 
3286 		quirk = (struct scsi_sense_quirk_entry *)match;
3287 		asc_tables[0] = quirk->asc_info;
3288 		asc_tables_size[0] = quirk->num_ascs;
3289 		asc_tables[1] = asc_table;
3290 		asc_tables_size[1] = asc_table_size;
3291 		num_asc_tables = 2;
3292 		sense_tables[0] = quirk->sense_key_info;
3293 		sense_tables_size[0] = quirk->num_sense_keys;
3294 		sense_tables[1] = sense_key_table;
3295 		sense_tables_size[1] = nitems(sense_key_table);
3296 		num_sense_tables = 2;
3297 	} else {
3298 		asc_tables[0] = asc_table;
3299 		asc_tables_size[0] = asc_table_size;
3300 		num_asc_tables = 1;
3301 		sense_tables[0] = sense_key_table;
3302 		sense_tables_size[0] = nitems(sense_key_table);
3303 		num_sense_tables = 1;
3304 	}
3305 
3306 	asc_ascq.asc = asc;
3307 	asc_ascq.ascq = ascq;
3308 	for (i = 0; i < num_asc_tables; i++) {
3309 		void *found_entry;
3310 
3311 		found_entry = bsearch(&asc_ascq, asc_tables[i],
3312 				      asc_tables_size[i],
3313 				      sizeof(**asc_tables),
3314 				      ascentrycomp);
3315 
3316 		if (found_entry) {
3317 			*asc_entry = (struct asc_table_entry *)found_entry;
3318 			break;
3319 		}
3320 	}
3321 
3322 	for (i = 0; i < num_sense_tables; i++) {
3323 		void *found_entry;
3324 
3325 		found_entry = bsearch(&sense_key, sense_tables[i],
3326 				      sense_tables_size[i],
3327 				      sizeof(**sense_tables),
3328 				      senseentrycomp);
3329 
3330 		if (found_entry) {
3331 			*sense_entry =
3332 			    (struct sense_key_table_entry *)found_entry;
3333 			break;
3334 		}
3335 	}
3336 }
3337 
3338 void
3339 scsi_sense_desc(int sense_key, int asc, int ascq,
3340 		struct scsi_inquiry_data *inq_data,
3341 		const char **sense_key_desc, const char **asc_desc)
3342 {
3343 	const struct asc_table_entry *asc_entry;
3344 	const struct sense_key_table_entry *sense_entry;
3345 
3346 	fetchtableentries(sense_key, asc, ascq,
3347 			  inq_data,
3348 			  &sense_entry,
3349 			  &asc_entry);
3350 
3351 	if (sense_entry != NULL)
3352 		*sense_key_desc = sense_entry->desc;
3353 	else
3354 		*sense_key_desc = "Invalid Sense Key";
3355 
3356 	if (asc_entry != NULL)
3357 		*asc_desc = asc_entry->desc;
3358 	else if (asc >= 0x80 && asc <= 0xff)
3359 		*asc_desc = "Vendor Specific ASC";
3360 	else if (ascq >= 0x80 && ascq <= 0xff)
3361 		*asc_desc = "Vendor Specific ASCQ";
3362 	else
3363 		*asc_desc = "Reserved ASC/ASCQ pair";
3364 }
3365 
3366 /*
3367  * Given sense and device type information, return the appropriate action.
3368  * If we do not understand the specific error as identified by the ASC/ASCQ
3369  * pair, fall back on the more generic actions derived from the sense key.
3370  */
3371 scsi_sense_action
3372 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
3373 		  u_int32_t sense_flags)
3374 {
3375 	const struct asc_table_entry *asc_entry;
3376 	const struct sense_key_table_entry *sense_entry;
3377 	int error_code, sense_key, asc, ascq;
3378 	scsi_sense_action action;
3379 
3380 	if (!scsi_extract_sense_ccb((union ccb *)csio,
3381 	    &error_code, &sense_key, &asc, &ascq)) {
3382 		action = SS_RETRY | SSQ_DECREMENT_COUNT | SSQ_PRINT_SENSE | EIO;
3383 	} else if ((error_code == SSD_DEFERRED_ERROR)
3384 	 || (error_code == SSD_DESC_DEFERRED_ERROR)) {
3385 		/*
3386 		 * XXX dufault@FreeBSD.org
3387 		 * This error doesn't relate to the command associated
3388 		 * with this request sense.  A deferred error is an error
3389 		 * for a command that has already returned GOOD status
3390 		 * (see SCSI2 8.2.14.2).
3391 		 *
3392 		 * By my reading of that section, it looks like the current
3393 		 * command has been cancelled, we should now clean things up
3394 		 * (hopefully recovering any lost data) and then retry the
3395 		 * current command.  There are two easy choices, both wrong:
3396 		 *
3397 		 * 1. Drop through (like we had been doing), thus treating
3398 		 *    this as if the error were for the current command and
3399 		 *    return and stop the current command.
3400 		 *
3401 		 * 2. Issue a retry (like I made it do) thus hopefully
3402 		 *    recovering the current transfer, and ignoring the
3403 		 *    fact that we've dropped a command.
3404 		 *
3405 		 * These should probably be handled in a device specific
3406 		 * sense handler or punted back up to a user mode daemon
3407 		 */
3408 		action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3409 	} else {
3410 		fetchtableentries(sense_key, asc, ascq,
3411 				  inq_data,
3412 				  &sense_entry,
3413 				  &asc_entry);
3414 
3415 		/*
3416 		 * Override the 'No additional Sense' entry (0,0)
3417 		 * with the error action of the sense key.
3418 		 */
3419 		if (asc_entry != NULL
3420 		 && (asc != 0 || ascq != 0))
3421 			action = asc_entry->action;
3422 		else if (sense_entry != NULL)
3423 			action = sense_entry->action;
3424 		else
3425 			action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3426 
3427 		if (sense_key == SSD_KEY_RECOVERED_ERROR) {
3428 			/*
3429 			 * The action succeeded but the device wants
3430 			 * the user to know that some recovery action
3431 			 * was required.
3432 			 */
3433 			action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
3434 			action |= SS_NOP|SSQ_PRINT_SENSE;
3435 		} else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
3436 			if ((sense_flags & SF_QUIET_IR) != 0)
3437 				action &= ~SSQ_PRINT_SENSE;
3438 		} else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
3439 			if ((sense_flags & SF_RETRY_UA) != 0
3440 			 && (action & SS_MASK) == SS_FAIL) {
3441 				action &= ~(SS_MASK|SSQ_MASK);
3442 				action |= SS_RETRY|SSQ_DECREMENT_COUNT|
3443 					  SSQ_PRINT_SENSE;
3444 			}
3445 			action |= SSQ_UA;
3446 		}
3447 	}
3448 	if ((action & SS_MASK) >= SS_START &&
3449 	    (sense_flags & SF_NO_RECOVERY)) {
3450 		action &= ~SS_MASK;
3451 		action |= SS_FAIL;
3452 	} else if ((action & SS_MASK) == SS_RETRY &&
3453 	    (sense_flags & SF_NO_RETRY)) {
3454 		action &= ~SS_MASK;
3455 		action |= SS_FAIL;
3456 	}
3457 	if ((sense_flags & SF_PRINT_ALWAYS) != 0)
3458 		action |= SSQ_PRINT_SENSE;
3459 	else if ((sense_flags & SF_NO_PRINT) != 0)
3460 		action &= ~SSQ_PRINT_SENSE;
3461 
3462 	return (action);
3463 }
3464 
3465 char *
3466 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
3467 {
3468 	struct sbuf sb;
3469 	int error;
3470 
3471 	if (len == 0)
3472 		return ("");
3473 
3474 	sbuf_new(&sb, cdb_string, len, SBUF_FIXEDLEN);
3475 
3476 	scsi_cdb_sbuf(cdb_ptr, &sb);
3477 
3478 	/* ENOMEM just means that the fixed buffer is full, OK to ignore */
3479 	error = sbuf_finish(&sb);
3480 	if (error != 0 && error != ENOMEM)
3481 		return ("");
3482 
3483 	return(sbuf_data(&sb));
3484 }
3485 
3486 void
3487 scsi_cdb_sbuf(u_int8_t *cdb_ptr, struct sbuf *sb)
3488 {
3489 	u_int8_t cdb_len;
3490 	int i;
3491 
3492 	if (cdb_ptr == NULL)
3493 		return;
3494 
3495 	/*
3496 	 * This is taken from the SCSI-3 draft spec.
3497 	 * (T10/1157D revision 0.3)
3498 	 * The top 3 bits of an opcode are the group code.  The next 5 bits
3499 	 * are the command code.
3500 	 * Group 0:  six byte commands
3501 	 * Group 1:  ten byte commands
3502 	 * Group 2:  ten byte commands
3503 	 * Group 3:  reserved
3504 	 * Group 4:  sixteen byte commands
3505 	 * Group 5:  twelve byte commands
3506 	 * Group 6:  vendor specific
3507 	 * Group 7:  vendor specific
3508 	 */
3509 	switch((*cdb_ptr >> 5) & 0x7) {
3510 		case 0:
3511 			cdb_len = 6;
3512 			break;
3513 		case 1:
3514 		case 2:
3515 			cdb_len = 10;
3516 			break;
3517 		case 3:
3518 		case 6:
3519 		case 7:
3520 			/* in this case, just print out the opcode */
3521 			cdb_len = 1;
3522 			break;
3523 		case 4:
3524 			cdb_len = 16;
3525 			break;
3526 		case 5:
3527 			cdb_len = 12;
3528 			break;
3529 	}
3530 
3531 	for (i = 0; i < cdb_len; i++)
3532 		sbuf_printf(sb, "%02hhx ", cdb_ptr[i]);
3533 
3534 	return;
3535 }
3536 
3537 const char *
3538 scsi_status_string(struct ccb_scsiio *csio)
3539 {
3540 	switch(csio->scsi_status) {
3541 	case SCSI_STATUS_OK:
3542 		return("OK");
3543 	case SCSI_STATUS_CHECK_COND:
3544 		return("Check Condition");
3545 	case SCSI_STATUS_BUSY:
3546 		return("Busy");
3547 	case SCSI_STATUS_INTERMED:
3548 		return("Intermediate");
3549 	case SCSI_STATUS_INTERMED_COND_MET:
3550 		return("Intermediate-Condition Met");
3551 	case SCSI_STATUS_RESERV_CONFLICT:
3552 		return("Reservation Conflict");
3553 	case SCSI_STATUS_CMD_TERMINATED:
3554 		return("Command Terminated");
3555 	case SCSI_STATUS_QUEUE_FULL:
3556 		return("Queue Full");
3557 	case SCSI_STATUS_ACA_ACTIVE:
3558 		return("ACA Active");
3559 	case SCSI_STATUS_TASK_ABORTED:
3560 		return("Task Aborted");
3561 	default: {
3562 		static char unkstr[64];
3563 		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
3564 			 csio->scsi_status);
3565 		return(unkstr);
3566 	}
3567 	}
3568 }
3569 
3570 /*
3571  * scsi_command_string() returns 0 for success and -1 for failure.
3572  */
3573 #ifdef _KERNEL
3574 int
3575 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
3576 #else /* !_KERNEL */
3577 int
3578 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio,
3579 		    struct sbuf *sb)
3580 #endif /* _KERNEL/!_KERNEL */
3581 {
3582 	struct scsi_inquiry_data *inq_data;
3583 #ifdef _KERNEL
3584 	struct	  ccb_getdev *cgd;
3585 #endif /* _KERNEL */
3586 
3587 #ifdef _KERNEL
3588 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3589 		return(-1);
3590 	/*
3591 	 * Get the device information.
3592 	 */
3593 	xpt_setup_ccb(&cgd->ccb_h,
3594 		      csio->ccb_h.path,
3595 		      CAM_PRIORITY_NORMAL);
3596 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3597 	xpt_action((union ccb *)cgd);
3598 
3599 	/*
3600 	 * If the device is unconfigured, just pretend that it is a hard
3601 	 * drive.  scsi_op_desc() needs this.
3602 	 */
3603 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3604 		cgd->inq_data.device = T_DIRECT;
3605 
3606 	inq_data = &cgd->inq_data;
3607 
3608 #else /* !_KERNEL */
3609 
3610 	inq_data = &device->inq_data;
3611 
3612 #endif /* _KERNEL/!_KERNEL */
3613 
3614 	if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3615 		sbuf_printf(sb, "%s. CDB: ",
3616 			    scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data));
3617 		scsi_cdb_sbuf(csio->cdb_io.cdb_ptr, sb);
3618 	} else {
3619 		sbuf_printf(sb, "%s. CDB: ",
3620 			    scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data));
3621 		scsi_cdb_sbuf(csio->cdb_io.cdb_bytes, sb);
3622 	}
3623 
3624 #ifdef _KERNEL
3625 	xpt_free_ccb((union ccb *)cgd);
3626 #endif
3627 
3628 	return(0);
3629 }
3630 
3631 /*
3632  * Iterate over sense descriptors.  Each descriptor is passed into iter_func().
3633  * If iter_func() returns 0, list traversal continues.  If iter_func()
3634  * returns non-zero, list traversal is stopped.
3635  */
3636 void
3637 scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
3638 		  int (*iter_func)(struct scsi_sense_data_desc *sense,
3639 				   u_int, struct scsi_sense_desc_header *,
3640 				   void *), void *arg)
3641 {
3642 	int cur_pos;
3643 	int desc_len;
3644 
3645 	/*
3646 	 * First make sure the extra length field is present.
3647 	 */
3648 	if (SSD_DESC_IS_PRESENT(sense, sense_len, extra_len) == 0)
3649 		return;
3650 
3651 	/*
3652 	 * The length of data actually returned may be different than the
3653 	 * extra_len recorded in the structure.
3654 	 */
3655 	desc_len = sense_len -offsetof(struct scsi_sense_data_desc, sense_desc);
3656 
3657 	/*
3658 	 * Limit this further by the extra length reported, and the maximum
3659 	 * allowed extra length.
3660 	 */
3661 	desc_len = MIN(desc_len, MIN(sense->extra_len, SSD_EXTRA_MAX));
3662 
3663 	/*
3664 	 * Subtract the size of the header from the descriptor length.
3665 	 * This is to ensure that we have at least the header left, so we
3666 	 * don't have to check that inside the loop.  This can wind up
3667 	 * being a negative value.
3668 	 */
3669 	desc_len -= sizeof(struct scsi_sense_desc_header);
3670 
3671 	for (cur_pos = 0; cur_pos < desc_len;) {
3672 		struct scsi_sense_desc_header *header;
3673 
3674 		header = (struct scsi_sense_desc_header *)
3675 			&sense->sense_desc[cur_pos];
3676 
3677 		/*
3678 		 * Check to make sure we have the entire descriptor.  We
3679 		 * don't call iter_func() unless we do.
3680 		 *
3681 		 * Note that although cur_pos is at the beginning of the
3682 		 * descriptor, desc_len already has the header length
3683 		 * subtracted.  So the comparison of the length in the
3684 		 * header (which does not include the header itself) to
3685 		 * desc_len - cur_pos is correct.
3686 		 */
3687 		if (header->length > (desc_len - cur_pos))
3688 			break;
3689 
3690 		if (iter_func(sense, sense_len, header, arg) != 0)
3691 			break;
3692 
3693 		cur_pos += sizeof(*header) + header->length;
3694 	}
3695 }
3696 
3697 struct scsi_find_desc_info {
3698 	uint8_t desc_type;
3699 	struct scsi_sense_desc_header *header;
3700 };
3701 
3702 static int
3703 scsi_find_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
3704 		    struct scsi_sense_desc_header *header, void *arg)
3705 {
3706 	struct scsi_find_desc_info *desc_info;
3707 
3708 	desc_info = (struct scsi_find_desc_info *)arg;
3709 
3710 	if (header->desc_type == desc_info->desc_type) {
3711 		desc_info->header = header;
3712 
3713 		/* We found the descriptor, tell the iterator to stop. */
3714 		return (1);
3715 	} else
3716 		return (0);
3717 }
3718 
3719 /*
3720  * Given a descriptor type, return a pointer to it if it is in the sense
3721  * data and not truncated.  Avoiding truncating sense data will simplify
3722  * things significantly for the caller.
3723  */
3724 uint8_t *
3725 scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
3726 	       uint8_t desc_type)
3727 {
3728 	struct scsi_find_desc_info desc_info;
3729 
3730 	desc_info.desc_type = desc_type;
3731 	desc_info.header = NULL;
3732 
3733 	scsi_desc_iterate(sense, sense_len, scsi_find_desc_func, &desc_info);
3734 
3735 	return ((uint8_t *)desc_info.header);
3736 }
3737 
3738 /*
3739  * Fill in SCSI sense data with the specified parameters.  This routine can
3740  * fill in either fixed or descriptor type sense data.
3741  */
3742 void
3743 scsi_set_sense_data_va(struct scsi_sense_data *sense_data,
3744 		      scsi_sense_data_type sense_format, int current_error,
3745 		      int sense_key, int asc, int ascq, va_list ap)
3746 {
3747 	int descriptor_sense;
3748 	scsi_sense_elem_type elem_type;
3749 
3750 	/*
3751 	 * Determine whether to return fixed or descriptor format sense
3752 	 * data.  If the user specifies SSD_TYPE_NONE for some reason,
3753 	 * they'll just get fixed sense data.
3754 	 */
3755 	if (sense_format == SSD_TYPE_DESC)
3756 		descriptor_sense = 1;
3757 	else
3758 		descriptor_sense = 0;
3759 
3760 	/*
3761 	 * Zero the sense data, so that we don't pass back any garbage data
3762 	 * to the user.
3763 	 */
3764 	memset(sense_data, 0, sizeof(*sense_data));
3765 
3766 	if (descriptor_sense != 0) {
3767 		struct scsi_sense_data_desc *sense;
3768 
3769 		sense = (struct scsi_sense_data_desc *)sense_data;
3770 		/*
3771 		 * The descriptor sense format eliminates the use of the
3772 		 * valid bit.
3773 		 */
3774 		if (current_error != 0)
3775 			sense->error_code = SSD_DESC_CURRENT_ERROR;
3776 		else
3777 			sense->error_code = SSD_DESC_DEFERRED_ERROR;
3778 		sense->sense_key = sense_key;
3779 		sense->add_sense_code = asc;
3780 		sense->add_sense_code_qual = ascq;
3781 		/*
3782 		 * Start off with no extra length, since the above data
3783 		 * fits in the standard descriptor sense information.
3784 		 */
3785 		sense->extra_len = 0;
3786 		while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3787 			scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3788 			int sense_len, len_to_copy;
3789 			uint8_t *data;
3790 
3791 			if (elem_type >= SSD_ELEM_MAX) {
3792 				printf("%s: invalid sense type %d\n", __func__,
3793 				       elem_type);
3794 				break;
3795 			}
3796 
3797 			sense_len = (int)va_arg(ap, int);
3798 			len_to_copy = MIN(sense_len, SSD_EXTRA_MAX -
3799 					  sense->extra_len);
3800 			data = (uint8_t *)va_arg(ap, uint8_t *);
3801 
3802 			/*
3803 			 * We've already consumed the arguments for this one.
3804 			 */
3805 			if (elem_type == SSD_ELEM_SKIP)
3806 				continue;
3807 
3808 			switch (elem_type) {
3809 			case SSD_ELEM_DESC: {
3810 
3811 				/*
3812 				 * This is a straight descriptor.  All we
3813 				 * need to do is copy the data in.
3814 				 */
3815 				bcopy(data, &sense->sense_desc[
3816 				      sense->extra_len], len_to_copy);
3817 				sense->extra_len += len_to_copy;
3818 				break;
3819 			}
3820 			case SSD_ELEM_SKS: {
3821 				struct scsi_sense_sks sks;
3822 
3823 				bzero(&sks, sizeof(sks));
3824 
3825 				/*
3826 				 * This is already-formatted sense key
3827 				 * specific data.  We just need to fill out
3828 				 * the header and copy everything in.
3829 				 */
3830 				bcopy(data, &sks.sense_key_spec,
3831 				      MIN(len_to_copy,
3832 				          sizeof(sks.sense_key_spec)));
3833 
3834 				sks.desc_type = SSD_DESC_SKS;
3835 				sks.length = sizeof(sks) -
3836 				    offsetof(struct scsi_sense_sks, reserved1);
3837 				bcopy(&sks,&sense->sense_desc[sense->extra_len],
3838 				      sizeof(sks));
3839 				sense->extra_len += sizeof(sks);
3840 				break;
3841 			}
3842 			case SSD_ELEM_INFO:
3843 			case SSD_ELEM_COMMAND: {
3844 				struct scsi_sense_command cmd;
3845 				struct scsi_sense_info info;
3846 				uint8_t *data_dest;
3847 				uint8_t *descriptor;
3848 				int descriptor_size, i, copy_len;
3849 
3850 				bzero(&cmd, sizeof(cmd));
3851 				bzero(&info, sizeof(info));
3852 
3853 				/*
3854 				 * Command or information data.  The
3855 				 * operate in pretty much the same way.
3856 				 */
3857 				if (elem_type == SSD_ELEM_COMMAND) {
3858 					len_to_copy = MIN(len_to_copy,
3859 					    sizeof(cmd.command_info));
3860 					descriptor = (uint8_t *)&cmd;
3861 					descriptor_size  = sizeof(cmd);
3862 					data_dest =(uint8_t *)&cmd.command_info;
3863 					cmd.desc_type = SSD_DESC_COMMAND;
3864 					cmd.length = sizeof(cmd) -
3865 					    offsetof(struct scsi_sense_command,
3866 						     reserved);
3867 				} else {
3868 					len_to_copy = MIN(len_to_copy,
3869 					    sizeof(info.info));
3870 					descriptor = (uint8_t *)&info;
3871 					descriptor_size = sizeof(cmd);
3872 					data_dest = (uint8_t *)&info.info;
3873 					info.desc_type = SSD_DESC_INFO;
3874 					info.byte2 = SSD_INFO_VALID;
3875 					info.length = sizeof(info) -
3876 					    offsetof(struct scsi_sense_info,
3877 						     byte2);
3878 				}
3879 
3880 				/*
3881 				 * Copy this in reverse because the spec
3882 				 * (SPC-4) says that when 4 byte quantities
3883 				 * are stored in this 8 byte field, the
3884 				 * first four bytes shall be 0.
3885 				 *
3886 				 * So we fill the bytes in from the end, and
3887 				 * if we have less than 8 bytes to copy,
3888 				 * the initial, most significant bytes will
3889 				 * be 0.
3890 				 */
3891 				for (i = sense_len - 1; i >= 0 &&
3892 				     len_to_copy > 0; i--, len_to_copy--)
3893 					data_dest[len_to_copy - 1] = data[i];
3894 
3895 				/*
3896 				 * This calculation looks much like the
3897 				 * initial len_to_copy calculation, but
3898 				 * we have to do it again here, because
3899 				 * we're looking at a larger amount that
3900 				 * may or may not fit.  It's not only the
3901 				 * data the user passed in, but also the
3902 				 * rest of the descriptor.
3903 				 */
3904 				copy_len = MIN(descriptor_size,
3905 				    SSD_EXTRA_MAX - sense->extra_len);
3906 				bcopy(descriptor, &sense->sense_desc[
3907 				      sense->extra_len], copy_len);
3908 				sense->extra_len += copy_len;
3909 				break;
3910 			}
3911 			case SSD_ELEM_FRU: {
3912 				struct scsi_sense_fru fru;
3913 				int copy_len;
3914 
3915 				bzero(&fru, sizeof(fru));
3916 
3917 				fru.desc_type = SSD_DESC_FRU;
3918 				fru.length = sizeof(fru) -
3919 				    offsetof(struct scsi_sense_fru, reserved);
3920 				fru.fru = *data;
3921 
3922 				copy_len = MIN(sizeof(fru), SSD_EXTRA_MAX -
3923 					       sense->extra_len);
3924 				bcopy(&fru, &sense->sense_desc[
3925 				      sense->extra_len], copy_len);
3926 				sense->extra_len += copy_len;
3927 				break;
3928 			}
3929 			case SSD_ELEM_STREAM: {
3930 				struct scsi_sense_stream stream_sense;
3931 				int copy_len;
3932 
3933 				bzero(&stream_sense, sizeof(stream_sense));
3934 				stream_sense.desc_type = SSD_DESC_STREAM;
3935 				stream_sense.length = sizeof(stream_sense) -
3936 				   offsetof(struct scsi_sense_stream, reserved);
3937 				stream_sense.byte3 = *data;
3938 
3939 				copy_len = MIN(sizeof(stream_sense),
3940 				    SSD_EXTRA_MAX - sense->extra_len);
3941 				bcopy(&stream_sense, &sense->sense_desc[
3942 				      sense->extra_len], copy_len);
3943 				sense->extra_len += copy_len;
3944 				break;
3945 			}
3946 			default:
3947 				/*
3948 				 * We shouldn't get here, but if we do, do
3949 				 * nothing.  We've already consumed the
3950 				 * arguments above.
3951 				 */
3952 				break;
3953 			}
3954 		}
3955 	} else {
3956 		struct scsi_sense_data_fixed *sense;
3957 
3958 		sense = (struct scsi_sense_data_fixed *)sense_data;
3959 
3960 		if (current_error != 0)
3961 			sense->error_code = SSD_CURRENT_ERROR;
3962 		else
3963 			sense->error_code = SSD_DEFERRED_ERROR;
3964 
3965 		sense->flags = sense_key;
3966 		sense->add_sense_code = asc;
3967 		sense->add_sense_code_qual = ascq;
3968 		/*
3969 		 * We've set the ASC and ASCQ, so we have 6 more bytes of
3970 		 * valid data.  If we wind up setting any of the other
3971 		 * fields, we'll bump this to 10 extra bytes.
3972 		 */
3973 		sense->extra_len = 6;
3974 
3975 		while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3976 			scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3977 			int sense_len, len_to_copy;
3978 			uint8_t *data;
3979 
3980 			if (elem_type >= SSD_ELEM_MAX) {
3981 				printf("%s: invalid sense type %d\n", __func__,
3982 				       elem_type);
3983 				break;
3984 			}
3985 			/*
3986 			 * If we get in here, just bump the extra length to
3987 			 * 10 bytes.  That will encompass anything we're
3988 			 * going to set here.
3989 			 */
3990 			sense->extra_len = 10;
3991 			sense_len = (int)va_arg(ap, int);
3992 			data = (uint8_t *)va_arg(ap, uint8_t *);
3993 
3994 			switch (elem_type) {
3995 			case SSD_ELEM_SKS:
3996 				/*
3997 				 * The user passed in pre-formatted sense
3998 				 * key specific data.
3999 				 */
4000 				bcopy(data, &sense->sense_key_spec[0],
4001 				      MIN(sizeof(sense->sense_key_spec),
4002 				      sense_len));
4003 				break;
4004 			case SSD_ELEM_INFO:
4005 			case SSD_ELEM_COMMAND: {
4006 				uint8_t *data_dest;
4007 				int i;
4008 
4009 				if (elem_type == SSD_ELEM_COMMAND) {
4010 					data_dest = &sense->cmd_spec_info[0];
4011 					len_to_copy = MIN(sense_len,
4012 					    sizeof(sense->cmd_spec_info));
4013 				} else {
4014 					data_dest = &sense->info[0];
4015 					len_to_copy = MIN(sense_len,
4016 					    sizeof(sense->info));
4017 					/*
4018 					 * We're setting the info field, so
4019 					 * set the valid bit.
4020 					 */
4021 					sense->error_code |= SSD_ERRCODE_VALID;
4022 				}
4023 
4024 				/*
4025 			 	 * Copy this in reverse so that if we have
4026 				 * less than 4 bytes to fill, the least
4027 				 * significant bytes will be at the end.
4028 				 * If we have more than 4 bytes, only the
4029 				 * least significant bytes will be included.
4030 				 */
4031 				for (i = sense_len - 1; i >= 0 &&
4032 				     len_to_copy > 0; i--, len_to_copy--)
4033 					data_dest[len_to_copy - 1] = data[i];
4034 
4035 				break;
4036 			}
4037 			case SSD_ELEM_FRU:
4038 				sense->fru = *data;
4039 				break;
4040 			case SSD_ELEM_STREAM:
4041 				sense->flags |= *data;
4042 				break;
4043 			case SSD_ELEM_DESC:
4044 			default:
4045 
4046 				/*
4047 				 * If the user passes in descriptor sense,
4048 				 * we can't handle that in fixed format.
4049 				 * So just skip it, and any unknown argument
4050 				 * types.
4051 				 */
4052 				break;
4053 			}
4054 		}
4055 	}
4056 }
4057 
4058 void
4059 scsi_set_sense_data(struct scsi_sense_data *sense_data,
4060 		    scsi_sense_data_type sense_format, int current_error,
4061 		    int sense_key, int asc, int ascq, ...)
4062 {
4063 	va_list ap;
4064 
4065 	va_start(ap, ascq);
4066 	scsi_set_sense_data_va(sense_data, sense_format, current_error,
4067 			       sense_key, asc, ascq, ap);
4068 	va_end(ap);
4069 }
4070 
4071 /*
4072  * Get sense information for three similar sense data types.
4073  */
4074 int
4075 scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
4076 		    uint8_t info_type, uint64_t *info, int64_t *signed_info)
4077 {
4078 	scsi_sense_data_type sense_type;
4079 
4080 	if (sense_len == 0)
4081 		goto bailout;
4082 
4083 	sense_type = scsi_sense_type(sense_data);
4084 
4085 	switch (sense_type) {
4086 	case SSD_TYPE_DESC: {
4087 		struct scsi_sense_data_desc *sense;
4088 		uint8_t *desc;
4089 
4090 		sense = (struct scsi_sense_data_desc *)sense_data;
4091 
4092 		desc = scsi_find_desc(sense, sense_len, info_type);
4093 		if (desc == NULL)
4094 			goto bailout;
4095 
4096 		switch (info_type) {
4097 		case SSD_DESC_INFO: {
4098 			struct scsi_sense_info *info_desc;
4099 
4100 			info_desc = (struct scsi_sense_info *)desc;
4101 			*info = scsi_8btou64(info_desc->info);
4102 			if (signed_info != NULL)
4103 				*signed_info = *info;
4104 			break;
4105 		}
4106 		case SSD_DESC_COMMAND: {
4107 			struct scsi_sense_command *cmd_desc;
4108 
4109 			cmd_desc = (struct scsi_sense_command *)desc;
4110 
4111 			*info = scsi_8btou64(cmd_desc->command_info);
4112 			if (signed_info != NULL)
4113 				*signed_info = *info;
4114 			break;
4115 		}
4116 		case SSD_DESC_FRU: {
4117 			struct scsi_sense_fru *fru_desc;
4118 
4119 			fru_desc = (struct scsi_sense_fru *)desc;
4120 
4121 			*info = fru_desc->fru;
4122 			if (signed_info != NULL)
4123 				*signed_info = (int8_t)fru_desc->fru;
4124 			break;
4125 		}
4126 		default:
4127 			goto bailout;
4128 			break;
4129 		}
4130 		break;
4131 	}
4132 	case SSD_TYPE_FIXED: {
4133 		struct scsi_sense_data_fixed *sense;
4134 
4135 		sense = (struct scsi_sense_data_fixed *)sense_data;
4136 
4137 		switch (info_type) {
4138 		case SSD_DESC_INFO: {
4139 			uint32_t info_val;
4140 
4141 			if ((sense->error_code & SSD_ERRCODE_VALID) == 0)
4142 				goto bailout;
4143 
4144 			if (SSD_FIXED_IS_PRESENT(sense, sense_len, info) == 0)
4145 				goto bailout;
4146 
4147 			info_val = scsi_4btoul(sense->info);
4148 
4149 			*info = info_val;
4150 			if (signed_info != NULL)
4151 				*signed_info = (int32_t)info_val;
4152 			break;
4153 		}
4154 		case SSD_DESC_COMMAND: {
4155 			uint32_t cmd_val;
4156 
4157 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len,
4158 			     cmd_spec_info) == 0)
4159 			 || (SSD_FIXED_IS_FILLED(sense, cmd_spec_info) == 0))
4160 				goto bailout;
4161 
4162 			cmd_val = scsi_4btoul(sense->cmd_spec_info);
4163 			if (cmd_val == 0)
4164 				goto bailout;
4165 
4166 			*info = cmd_val;
4167 			if (signed_info != NULL)
4168 				*signed_info = (int32_t)cmd_val;
4169 			break;
4170 		}
4171 		case SSD_DESC_FRU:
4172 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len, fru) == 0)
4173 			 || (SSD_FIXED_IS_FILLED(sense, fru) == 0))
4174 				goto bailout;
4175 
4176 			if (sense->fru == 0)
4177 				goto bailout;
4178 
4179 			*info = sense->fru;
4180 			if (signed_info != NULL)
4181 				*signed_info = (int8_t)sense->fru;
4182 			break;
4183 		default:
4184 			goto bailout;
4185 			break;
4186 		}
4187 		break;
4188 	}
4189 	default:
4190 		goto bailout;
4191 		break;
4192 	}
4193 
4194 	return (0);
4195 bailout:
4196 	return (1);
4197 }
4198 
4199 int
4200 scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len, uint8_t *sks)
4201 {
4202 	scsi_sense_data_type sense_type;
4203 
4204 	if (sense_len == 0)
4205 		goto bailout;
4206 
4207 	sense_type = scsi_sense_type(sense_data);
4208 
4209 	switch (sense_type) {
4210 	case SSD_TYPE_DESC: {
4211 		struct scsi_sense_data_desc *sense;
4212 		struct scsi_sense_sks *desc;
4213 
4214 		sense = (struct scsi_sense_data_desc *)sense_data;
4215 
4216 		desc = (struct scsi_sense_sks *)scsi_find_desc(sense, sense_len,
4217 							       SSD_DESC_SKS);
4218 		if (desc == NULL)
4219 			goto bailout;
4220 
4221 		/*
4222 		 * No need to check the SKS valid bit for descriptor sense.
4223 		 * If the descriptor is present, it is valid.
4224 		 */
4225 		bcopy(desc->sense_key_spec, sks, sizeof(desc->sense_key_spec));
4226 		break;
4227 	}
4228 	case SSD_TYPE_FIXED: {
4229 		struct scsi_sense_data_fixed *sense;
4230 
4231 		sense = (struct scsi_sense_data_fixed *)sense_data;
4232 
4233 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, sense_key_spec)== 0)
4234 		 || (SSD_FIXED_IS_FILLED(sense, sense_key_spec) == 0))
4235 			goto bailout;
4236 
4237 		if ((sense->sense_key_spec[0] & SSD_SCS_VALID) == 0)
4238 			goto bailout;
4239 
4240 		bcopy(sense->sense_key_spec, sks,sizeof(sense->sense_key_spec));
4241 		break;
4242 	}
4243 	default:
4244 		goto bailout;
4245 		break;
4246 	}
4247 	return (0);
4248 bailout:
4249 	return (1);
4250 }
4251 
4252 /*
4253  * Provide a common interface for fixed and descriptor sense to detect
4254  * whether we have block-specific sense information.  It is clear by the
4255  * presence of the block descriptor in descriptor mode, but we have to
4256  * infer from the inquiry data and ILI bit in fixed mode.
4257  */
4258 int
4259 scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
4260 		    struct scsi_inquiry_data *inq_data, uint8_t *block_bits)
4261 {
4262 	scsi_sense_data_type sense_type;
4263 
4264 	if (inq_data != NULL) {
4265 		switch (SID_TYPE(inq_data)) {
4266 		case T_DIRECT:
4267 		case T_RBC:
4268 		case T_ZBC_HM:
4269 			break;
4270 		default:
4271 			goto bailout;
4272 			break;
4273 		}
4274 	}
4275 
4276 	sense_type = scsi_sense_type(sense_data);
4277 
4278 	switch (sense_type) {
4279 	case SSD_TYPE_DESC: {
4280 		struct scsi_sense_data_desc *sense;
4281 		struct scsi_sense_block *block;
4282 
4283 		sense = (struct scsi_sense_data_desc *)sense_data;
4284 
4285 		block = (struct scsi_sense_block *)scsi_find_desc(sense,
4286 		    sense_len, SSD_DESC_BLOCK);
4287 		if (block == NULL)
4288 			goto bailout;
4289 
4290 		*block_bits = block->byte3;
4291 		break;
4292 	}
4293 	case SSD_TYPE_FIXED: {
4294 		struct scsi_sense_data_fixed *sense;
4295 
4296 		sense = (struct scsi_sense_data_fixed *)sense_data;
4297 
4298 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4299 			goto bailout;
4300 
4301 		if ((sense->flags & SSD_ILI) == 0)
4302 			goto bailout;
4303 
4304 		*block_bits = sense->flags & SSD_ILI;
4305 		break;
4306 	}
4307 	default:
4308 		goto bailout;
4309 		break;
4310 	}
4311 	return (0);
4312 bailout:
4313 	return (1);
4314 }
4315 
4316 int
4317 scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
4318 		     struct scsi_inquiry_data *inq_data, uint8_t *stream_bits)
4319 {
4320 	scsi_sense_data_type sense_type;
4321 
4322 	if (inq_data != NULL) {
4323 		switch (SID_TYPE(inq_data)) {
4324 		case T_SEQUENTIAL:
4325 			break;
4326 		default:
4327 			goto bailout;
4328 			break;
4329 		}
4330 	}
4331 
4332 	sense_type = scsi_sense_type(sense_data);
4333 
4334 	switch (sense_type) {
4335 	case SSD_TYPE_DESC: {
4336 		struct scsi_sense_data_desc *sense;
4337 		struct scsi_sense_stream *stream;
4338 
4339 		sense = (struct scsi_sense_data_desc *)sense_data;
4340 
4341 		stream = (struct scsi_sense_stream *)scsi_find_desc(sense,
4342 		    sense_len, SSD_DESC_STREAM);
4343 		if (stream == NULL)
4344 			goto bailout;
4345 
4346 		*stream_bits = stream->byte3;
4347 		break;
4348 	}
4349 	case SSD_TYPE_FIXED: {
4350 		struct scsi_sense_data_fixed *sense;
4351 
4352 		sense = (struct scsi_sense_data_fixed *)sense_data;
4353 
4354 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4355 			goto bailout;
4356 
4357 		if ((sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK)) == 0)
4358 			goto bailout;
4359 
4360 		*stream_bits = sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK);
4361 		break;
4362 	}
4363 	default:
4364 		goto bailout;
4365 		break;
4366 	}
4367 	return (0);
4368 bailout:
4369 	return (1);
4370 }
4371 
4372 void
4373 scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4374 	       struct scsi_inquiry_data *inq_data, uint64_t info)
4375 {
4376 	sbuf_printf(sb, "Info: %#jx", info);
4377 }
4378 
4379 void
4380 scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4381 		  struct scsi_inquiry_data *inq_data, uint64_t csi)
4382 {
4383 	sbuf_printf(sb, "Command Specific Info: %#jx", csi);
4384 }
4385 
4386 
4387 void
4388 scsi_progress_sbuf(struct sbuf *sb, uint16_t progress)
4389 {
4390 	sbuf_printf(sb, "Progress: %d%% (%d/%d) complete",
4391 		    (progress * 100) / SSD_SKS_PROGRESS_DENOM,
4392 		    progress, SSD_SKS_PROGRESS_DENOM);
4393 }
4394 
4395 /*
4396  * Returns 1 for failure (i.e. SKS isn't valid) and 0 for success.
4397  */
4398 int
4399 scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks)
4400 {
4401 	if ((sks[0] & SSD_SKS_VALID) == 0)
4402 		return (1);
4403 
4404 	switch (sense_key) {
4405 	case SSD_KEY_ILLEGAL_REQUEST: {
4406 		struct scsi_sense_sks_field *field;
4407 		int bad_command;
4408 		char tmpstr[40];
4409 
4410 		/*Field Pointer*/
4411 		field = (struct scsi_sense_sks_field *)sks;
4412 
4413 		if (field->byte0 & SSD_SKS_FIELD_CMD)
4414 			bad_command = 1;
4415 		else
4416 			bad_command = 0;
4417 
4418 		tmpstr[0] = '\0';
4419 
4420 		/* Bit pointer is valid */
4421 		if (field->byte0 & SSD_SKS_BPV)
4422 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4423 				 field->byte0 & SSD_SKS_BIT_VALUE);
4424 
4425 		sbuf_printf(sb, "%s byte %d %sis invalid",
4426 			    bad_command ? "Command" : "Data",
4427 			    scsi_2btoul(field->field), tmpstr);
4428 		break;
4429 	}
4430 	case SSD_KEY_UNIT_ATTENTION: {
4431 		struct scsi_sense_sks_overflow *overflow;
4432 
4433 		overflow = (struct scsi_sense_sks_overflow *)sks;
4434 
4435 		/*UA Condition Queue Overflow*/
4436 		sbuf_printf(sb, "Unit Attention Condition Queue %s",
4437 			    (overflow->byte0 & SSD_SKS_OVERFLOW_SET) ?
4438 			    "Overflowed" : "Did Not Overflow??");
4439 		break;
4440 	}
4441 	case SSD_KEY_RECOVERED_ERROR:
4442 	case SSD_KEY_HARDWARE_ERROR:
4443 	case SSD_KEY_MEDIUM_ERROR: {
4444 		struct scsi_sense_sks_retry *retry;
4445 
4446 		/*Actual Retry Count*/
4447 		retry = (struct scsi_sense_sks_retry *)sks;
4448 
4449 		sbuf_printf(sb, "Actual Retry Count: %d",
4450 			    scsi_2btoul(retry->actual_retry_count));
4451 		break;
4452 	}
4453 	case SSD_KEY_NO_SENSE:
4454 	case SSD_KEY_NOT_READY: {
4455 		struct scsi_sense_sks_progress *progress;
4456 		int progress_val;
4457 
4458 		/*Progress Indication*/
4459 		progress = (struct scsi_sense_sks_progress *)sks;
4460 		progress_val = scsi_2btoul(progress->progress);
4461 
4462 		scsi_progress_sbuf(sb, progress_val);
4463 		break;
4464 	}
4465 	case SSD_KEY_COPY_ABORTED: {
4466 		struct scsi_sense_sks_segment *segment;
4467 		char tmpstr[40];
4468 
4469 		/*Segment Pointer*/
4470 		segment = (struct scsi_sense_sks_segment *)sks;
4471 
4472 		tmpstr[0] = '\0';
4473 
4474 		if (segment->byte0 & SSD_SKS_SEGMENT_BPV)
4475 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4476 				 segment->byte0 & SSD_SKS_SEGMENT_BITPTR);
4477 
4478 		sbuf_printf(sb, "%s byte %d %sis invalid", (segment->byte0 &
4479 			    SSD_SKS_SEGMENT_SD) ? "Segment" : "Data",
4480 			    scsi_2btoul(segment->field), tmpstr);
4481 		break;
4482 	}
4483 	default:
4484 		sbuf_printf(sb, "Sense Key Specific: %#x,%#x", sks[0],
4485 			    scsi_2btoul(&sks[1]));
4486 		break;
4487 	}
4488 
4489 	return (0);
4490 }
4491 
4492 void
4493 scsi_fru_sbuf(struct sbuf *sb, uint64_t fru)
4494 {
4495 	sbuf_printf(sb, "Field Replaceable Unit: %d", (int)fru);
4496 }
4497 
4498 void
4499 scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits, uint64_t info)
4500 {
4501 	int need_comma;
4502 
4503 	need_comma = 0;
4504 	/*
4505 	 * XXX KDM this needs more descriptive decoding.
4506 	 */
4507 	if (stream_bits & SSD_DESC_STREAM_FM) {
4508 		sbuf_printf(sb, "Filemark");
4509 		need_comma = 1;
4510 	}
4511 
4512 	if (stream_bits & SSD_DESC_STREAM_EOM) {
4513 		sbuf_printf(sb, "%sEOM", (need_comma) ? "," : "");
4514 		need_comma = 1;
4515 	}
4516 
4517 	if (stream_bits & SSD_DESC_STREAM_ILI)
4518 		sbuf_printf(sb, "%sILI", (need_comma) ? "," : "");
4519 
4520 	sbuf_printf(sb, ": Info: %#jx", (uintmax_t) info);
4521 }
4522 
4523 void
4524 scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits, uint64_t info)
4525 {
4526 	if (block_bits & SSD_DESC_BLOCK_ILI)
4527 		sbuf_printf(sb, "ILI: residue %#jx", (uintmax_t) info);
4528 }
4529 
4530 void
4531 scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4532 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4533 		     struct scsi_inquiry_data *inq_data,
4534 		     struct scsi_sense_desc_header *header)
4535 {
4536 	struct scsi_sense_info *info;
4537 
4538 	info = (struct scsi_sense_info *)header;
4539 
4540 	scsi_info_sbuf(sb, cdb, cdb_len, inq_data, scsi_8btou64(info->info));
4541 }
4542 
4543 void
4544 scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4545 			u_int sense_len, uint8_t *cdb, int cdb_len,
4546 			struct scsi_inquiry_data *inq_data,
4547 			struct scsi_sense_desc_header *header)
4548 {
4549 	struct scsi_sense_command *command;
4550 
4551 	command = (struct scsi_sense_command *)header;
4552 
4553 	scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
4554 			  scsi_8btou64(command->command_info));
4555 }
4556 
4557 void
4558 scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4559 		    u_int sense_len, uint8_t *cdb, int cdb_len,
4560 		    struct scsi_inquiry_data *inq_data,
4561 		    struct scsi_sense_desc_header *header)
4562 {
4563 	struct scsi_sense_sks *sks;
4564 	int error_code, sense_key, asc, ascq;
4565 
4566 	sks = (struct scsi_sense_sks *)header;
4567 
4568 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4569 			       &asc, &ascq, /*show_errors*/ 1);
4570 
4571 	scsi_sks_sbuf(sb, sense_key, sks->sense_key_spec);
4572 }
4573 
4574 void
4575 scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4576 		    u_int sense_len, uint8_t *cdb, int cdb_len,
4577 		    struct scsi_inquiry_data *inq_data,
4578 		    struct scsi_sense_desc_header *header)
4579 {
4580 	struct scsi_sense_fru *fru;
4581 
4582 	fru = (struct scsi_sense_fru *)header;
4583 
4584 	scsi_fru_sbuf(sb, (uint64_t)fru->fru);
4585 }
4586 
4587 void
4588 scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4589 		       u_int sense_len, uint8_t *cdb, int cdb_len,
4590 		       struct scsi_inquiry_data *inq_data,
4591 		       struct scsi_sense_desc_header *header)
4592 {
4593 	struct scsi_sense_stream *stream;
4594 	uint64_t info;
4595 
4596 	stream = (struct scsi_sense_stream *)header;
4597 	info = 0;
4598 
4599 	scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4600 
4601 	scsi_stream_sbuf(sb, stream->byte3, info);
4602 }
4603 
4604 void
4605 scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4606 		      u_int sense_len, uint8_t *cdb, int cdb_len,
4607 		      struct scsi_inquiry_data *inq_data,
4608 		      struct scsi_sense_desc_header *header)
4609 {
4610 	struct scsi_sense_block *block;
4611 	uint64_t info;
4612 
4613 	block = (struct scsi_sense_block *)header;
4614 	info = 0;
4615 
4616 	scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4617 
4618 	scsi_block_sbuf(sb, block->byte3, info);
4619 }
4620 
4621 void
4622 scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4623 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4624 			 struct scsi_inquiry_data *inq_data,
4625 			 struct scsi_sense_desc_header *header)
4626 {
4627 	struct scsi_sense_progress *progress;
4628 	const char *sense_key_desc;
4629 	const char *asc_desc;
4630 	int progress_val;
4631 
4632 	progress = (struct scsi_sense_progress *)header;
4633 
4634 	/*
4635 	 * Get descriptions for the sense key, ASC, and ASCQ in the
4636 	 * progress descriptor.  These could be different than the values
4637 	 * in the overall sense data.
4638 	 */
4639 	scsi_sense_desc(progress->sense_key, progress->add_sense_code,
4640 			progress->add_sense_code_qual, inq_data,
4641 			&sense_key_desc, &asc_desc);
4642 
4643 	progress_val = scsi_2btoul(progress->progress);
4644 
4645 	/*
4646 	 * The progress indicator is for the operation described by the
4647 	 * sense key, ASC, and ASCQ in the descriptor.
4648 	 */
4649 	sbuf_cat(sb, sense_key_desc);
4650 	sbuf_printf(sb, " asc:%x,%x (%s): ", progress->add_sense_code,
4651 		    progress->add_sense_code_qual, asc_desc);
4652 	scsi_progress_sbuf(sb, progress_val);
4653 }
4654 
4655 /*
4656  * Generic sense descriptor printing routine.  This is used when we have
4657  * not yet implemented a specific printing routine for this descriptor.
4658  */
4659 void
4660 scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4661 			u_int sense_len, uint8_t *cdb, int cdb_len,
4662 			struct scsi_inquiry_data *inq_data,
4663 			struct scsi_sense_desc_header *header)
4664 {
4665 	int i;
4666 	uint8_t *buf_ptr;
4667 
4668 	sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4669 
4670 	buf_ptr = (uint8_t *)&header[1];
4671 
4672 	for (i = 0; i < header->length; i++, buf_ptr++)
4673 		sbuf_printf(sb, " %02x", *buf_ptr);
4674 }
4675 
4676 /*
4677  * Keep this list in numeric order.  This speeds the array traversal.
4678  */
4679 struct scsi_sense_desc_printer {
4680 	uint8_t desc_type;
4681 	/*
4682 	 * The function arguments here are the superset of what is needed
4683 	 * to print out various different descriptors.  Command and
4684 	 * information descriptors need inquiry data and command type.
4685 	 * Sense key specific descriptors need the sense key.
4686 	 *
4687 	 * The sense, cdb, and inquiry data arguments may be NULL, but the
4688 	 * information printed may not be fully decoded as a result.
4689 	 */
4690 	void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4691 			   u_int sense_len, uint8_t *cdb, int cdb_len,
4692 			   struct scsi_inquiry_data *inq_data,
4693 			   struct scsi_sense_desc_header *header);
4694 } scsi_sense_printers[] = {
4695 	{SSD_DESC_INFO, scsi_sense_info_sbuf},
4696 	{SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4697 	{SSD_DESC_SKS, scsi_sense_sks_sbuf},
4698 	{SSD_DESC_FRU, scsi_sense_fru_sbuf},
4699 	{SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4700 	{SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4701 	{SSD_DESC_PROGRESS, scsi_sense_progress_sbuf}
4702 };
4703 
4704 void
4705 scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4706 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4707 		     struct scsi_inquiry_data *inq_data,
4708 		     struct scsi_sense_desc_header *header)
4709 {
4710 	u_int i;
4711 
4712 	for (i = 0; i < nitems(scsi_sense_printers); i++) {
4713 		struct scsi_sense_desc_printer *printer;
4714 
4715 		printer = &scsi_sense_printers[i];
4716 
4717 		/*
4718 		 * The list is sorted, so quit if we've passed our
4719 		 * descriptor number.
4720 		 */
4721 		if (printer->desc_type > header->desc_type)
4722 			break;
4723 
4724 		if (printer->desc_type != header->desc_type)
4725 			continue;
4726 
4727 		printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4728 				    inq_data, header);
4729 
4730 		return;
4731 	}
4732 
4733 	/*
4734 	 * No specific printing routine, so use the generic routine.
4735 	 */
4736 	scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4737 				inq_data, header);
4738 }
4739 
4740 scsi_sense_data_type
4741 scsi_sense_type(struct scsi_sense_data *sense_data)
4742 {
4743 	switch (sense_data->error_code & SSD_ERRCODE) {
4744 	case SSD_DESC_CURRENT_ERROR:
4745 	case SSD_DESC_DEFERRED_ERROR:
4746 		return (SSD_TYPE_DESC);
4747 		break;
4748 	case SSD_CURRENT_ERROR:
4749 	case SSD_DEFERRED_ERROR:
4750 		return (SSD_TYPE_FIXED);
4751 		break;
4752 	default:
4753 		break;
4754 	}
4755 
4756 	return (SSD_TYPE_NONE);
4757 }
4758 
4759 struct scsi_print_sense_info {
4760 	struct sbuf *sb;
4761 	char *path_str;
4762 	uint8_t *cdb;
4763 	int cdb_len;
4764 	struct scsi_inquiry_data *inq_data;
4765 };
4766 
4767 static int
4768 scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4769 		     struct scsi_sense_desc_header *header, void *arg)
4770 {
4771 	struct scsi_print_sense_info *print_info;
4772 
4773 	print_info = (struct scsi_print_sense_info *)arg;
4774 
4775 	switch (header->desc_type) {
4776 	case SSD_DESC_INFO:
4777 	case SSD_DESC_FRU:
4778 	case SSD_DESC_COMMAND:
4779 	case SSD_DESC_SKS:
4780 	case SSD_DESC_BLOCK:
4781 	case SSD_DESC_STREAM:
4782 		/*
4783 		 * We have already printed these descriptors, if they are
4784 		 * present.
4785 		 */
4786 		break;
4787 	default: {
4788 		sbuf_printf(print_info->sb, "%s", print_info->path_str);
4789 		scsi_sense_desc_sbuf(print_info->sb,
4790 				     (struct scsi_sense_data *)sense, sense_len,
4791 				     print_info->cdb, print_info->cdb_len,
4792 				     print_info->inq_data, header);
4793 		sbuf_printf(print_info->sb, "\n");
4794 		break;
4795 	}
4796 	}
4797 
4798 	/*
4799 	 * Tell the iterator that we want to see more descriptors if they
4800 	 * are present.
4801 	 */
4802 	return (0);
4803 }
4804 
4805 void
4806 scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4807 		     struct sbuf *sb, char *path_str,
4808 		     struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4809 		     int cdb_len)
4810 {
4811 	int error_code, sense_key, asc, ascq;
4812 
4813 	sbuf_cat(sb, path_str);
4814 
4815 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4816 			       &asc, &ascq, /*show_errors*/ 1);
4817 
4818 	sbuf_printf(sb, "SCSI sense: ");
4819 	switch (error_code) {
4820 	case SSD_DEFERRED_ERROR:
4821 	case SSD_DESC_DEFERRED_ERROR:
4822 		sbuf_printf(sb, "Deferred error: ");
4823 
4824 		/* FALLTHROUGH */
4825 	case SSD_CURRENT_ERROR:
4826 	case SSD_DESC_CURRENT_ERROR:
4827 	{
4828 		struct scsi_sense_data_desc *desc_sense;
4829 		struct scsi_print_sense_info print_info;
4830 		const char *sense_key_desc;
4831 		const char *asc_desc;
4832 		uint8_t sks[3];
4833 		uint64_t val;
4834 		int info_valid;
4835 
4836 		/*
4837 		 * Get descriptions for the sense key, ASC, and ASCQ.  If
4838 		 * these aren't present in the sense data (i.e. the sense
4839 		 * data isn't long enough), the -1 values that
4840 		 * scsi_extract_sense_len() returns will yield default
4841 		 * or error descriptions.
4842 		 */
4843 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
4844 				&sense_key_desc, &asc_desc);
4845 
4846 		/*
4847 		 * We first print the sense key and ASC/ASCQ.
4848 		 */
4849 		sbuf_cat(sb, sense_key_desc);
4850 		sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
4851 
4852 		/*
4853 		 * Get the info field if it is valid.
4854 		 */
4855 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
4856 					&val, NULL) == 0)
4857 			info_valid = 1;
4858 		else
4859 			info_valid = 0;
4860 
4861 		if (info_valid != 0) {
4862 			uint8_t bits;
4863 
4864 			/*
4865 			 * Determine whether we have any block or stream
4866 			 * device-specific information.
4867 			 */
4868 			if (scsi_get_block_info(sense, sense_len, inq_data,
4869 						&bits) == 0) {
4870 				sbuf_cat(sb, path_str);
4871 				scsi_block_sbuf(sb, bits, val);
4872 				sbuf_printf(sb, "\n");
4873 			} else if (scsi_get_stream_info(sense, sense_len,
4874 							inq_data, &bits) == 0) {
4875 				sbuf_cat(sb, path_str);
4876 				scsi_stream_sbuf(sb, bits, val);
4877 				sbuf_printf(sb, "\n");
4878 			} else if (val != 0) {
4879 				/*
4880 				 * The information field can be valid but 0.
4881 				 * If the block or stream bits aren't set,
4882 				 * and this is 0, it isn't terribly useful
4883 				 * to print it out.
4884 				 */
4885 				sbuf_cat(sb, path_str);
4886 				scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
4887 				sbuf_printf(sb, "\n");
4888 			}
4889 		}
4890 
4891 		/*
4892 		 * Print the FRU.
4893 		 */
4894 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
4895 					&val, NULL) == 0) {
4896 			sbuf_cat(sb, path_str);
4897 			scsi_fru_sbuf(sb, val);
4898 			sbuf_printf(sb, "\n");
4899 		}
4900 
4901 		/*
4902 		 * Print any command-specific information.
4903 		 */
4904 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
4905 					&val, NULL) == 0) {
4906 			sbuf_cat(sb, path_str);
4907 			scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
4908 			sbuf_printf(sb, "\n");
4909 		}
4910 
4911 		/*
4912 		 * Print out any sense-key-specific information.
4913 		 */
4914 		if (scsi_get_sks(sense, sense_len, sks) == 0) {
4915 			sbuf_cat(sb, path_str);
4916 			scsi_sks_sbuf(sb, sense_key, sks);
4917 			sbuf_printf(sb, "\n");
4918 		}
4919 
4920 		/*
4921 		 * If this is fixed sense, we're done.  If we have
4922 		 * descriptor sense, we might have more information
4923 		 * available.
4924 		 */
4925 		if (scsi_sense_type(sense) != SSD_TYPE_DESC)
4926 			break;
4927 
4928 		desc_sense = (struct scsi_sense_data_desc *)sense;
4929 
4930 		print_info.sb = sb;
4931 		print_info.path_str = path_str;
4932 		print_info.cdb = cdb;
4933 		print_info.cdb_len = cdb_len;
4934 		print_info.inq_data = inq_data;
4935 
4936 		/*
4937 		 * Print any sense descriptors that we have not already printed.
4938 		 */
4939 		scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
4940 				  &print_info);
4941 		break;
4942 
4943 	}
4944 	case -1:
4945 		/*
4946 		 * scsi_extract_sense_len() sets values to -1 if the
4947 		 * show_errors flag is set and they aren't present in the
4948 		 * sense data.  This means that sense_len is 0.
4949 		 */
4950 		sbuf_printf(sb, "No sense data present\n");
4951 		break;
4952 	default: {
4953 		sbuf_printf(sb, "Error code 0x%x", error_code);
4954 		if (sense->error_code & SSD_ERRCODE_VALID) {
4955 			struct scsi_sense_data_fixed *fixed_sense;
4956 
4957 			fixed_sense = (struct scsi_sense_data_fixed *)sense;
4958 
4959 			if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
4960 				uint32_t info;
4961 
4962 				info = scsi_4btoul(fixed_sense->info);
4963 
4964 				sbuf_printf(sb, " at block no. %d (decimal)",
4965 					    info);
4966 			}
4967 		}
4968 		sbuf_printf(sb, "\n");
4969 		break;
4970 	}
4971 	}
4972 }
4973 
4974 /*
4975  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
4976  */
4977 #ifdef _KERNEL
4978 int
4979 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
4980 		scsi_sense_string_flags flags)
4981 #else /* !_KERNEL */
4982 int
4983 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
4984 		struct sbuf *sb, scsi_sense_string_flags flags)
4985 #endif /* _KERNEL/!_KERNEL */
4986 {
4987 	struct	  scsi_sense_data *sense;
4988 	struct	  scsi_inquiry_data *inq_data;
4989 #ifdef _KERNEL
4990 	struct	  ccb_getdev *cgd;
4991 #endif /* _KERNEL */
4992 	char	  path_str[64];
4993 	uint8_t	  *cdb;
4994 
4995 #ifndef _KERNEL
4996 	if (device == NULL)
4997 		return(-1);
4998 #endif /* !_KERNEL */
4999 	if ((csio == NULL) || (sb == NULL))
5000 		return(-1);
5001 
5002 	/*
5003 	 * If the CDB is a physical address, we can't deal with it..
5004 	 */
5005 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
5006 		flags &= ~SSS_FLAG_PRINT_COMMAND;
5007 
5008 #ifdef _KERNEL
5009 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
5010 #else /* !_KERNEL */
5011 	cam_path_string(device, path_str, sizeof(path_str));
5012 #endif /* _KERNEL/!_KERNEL */
5013 
5014 #ifdef _KERNEL
5015 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
5016 		return(-1);
5017 	/*
5018 	 * Get the device information.
5019 	 */
5020 	xpt_setup_ccb(&cgd->ccb_h,
5021 		      csio->ccb_h.path,
5022 		      CAM_PRIORITY_NORMAL);
5023 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
5024 	xpt_action((union ccb *)cgd);
5025 
5026 	/*
5027 	 * If the device is unconfigured, just pretend that it is a hard
5028 	 * drive.  scsi_op_desc() needs this.
5029 	 */
5030 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
5031 		cgd->inq_data.device = T_DIRECT;
5032 
5033 	inq_data = &cgd->inq_data;
5034 
5035 #else /* !_KERNEL */
5036 
5037 	inq_data = &device->inq_data;
5038 
5039 #endif /* _KERNEL/!_KERNEL */
5040 
5041 	sense = NULL;
5042 
5043 	if (flags & SSS_FLAG_PRINT_COMMAND) {
5044 
5045 		sbuf_cat(sb, path_str);
5046 
5047 #ifdef _KERNEL
5048 		scsi_command_string(csio, sb);
5049 #else /* !_KERNEL */
5050 		scsi_command_string(device, csio, sb);
5051 #endif /* _KERNEL/!_KERNEL */
5052 		sbuf_printf(sb, "\n");
5053 	}
5054 
5055 	/*
5056 	 * If the sense data is a physical pointer, forget it.
5057 	 */
5058 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
5059 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5060 #ifdef _KERNEL
5061 			xpt_free_ccb((union ccb*)cgd);
5062 #endif /* _KERNEL/!_KERNEL */
5063 			return(-1);
5064 		} else {
5065 			/*
5066 			 * bcopy the pointer to avoid unaligned access
5067 			 * errors on finicky architectures.  We don't
5068 			 * ensure that the sense data is pointer aligned.
5069 			 */
5070 			bcopy(&csio->sense_data, &sense,
5071 			      sizeof(struct scsi_sense_data *));
5072 		}
5073 	} else {
5074 		/*
5075 		 * If the physical sense flag is set, but the sense pointer
5076 		 * is not also set, we assume that the user is an idiot and
5077 		 * return.  (Well, okay, it could be that somehow, the
5078 		 * entire csio is physical, but we would have probably core
5079 		 * dumped on one of the bogus pointer deferences above
5080 		 * already.)
5081 		 */
5082 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5083 #ifdef _KERNEL
5084 			xpt_free_ccb((union ccb*)cgd);
5085 #endif /* _KERNEL/!_KERNEL */
5086 			return(-1);
5087 		} else
5088 			sense = &csio->sense_data;
5089 	}
5090 
5091 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
5092 		cdb = csio->cdb_io.cdb_ptr;
5093 	else
5094 		cdb = csio->cdb_io.cdb_bytes;
5095 
5096 	scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
5097 			     path_str, inq_data, cdb, csio->cdb_len);
5098 
5099 #ifdef _KERNEL
5100 	xpt_free_ccb((union ccb*)cgd);
5101 #endif /* _KERNEL/!_KERNEL */
5102 	return(0);
5103 }
5104 
5105 
5106 
5107 #ifdef _KERNEL
5108 char *
5109 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
5110 #else /* !_KERNEL */
5111 char *
5112 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
5113 		  char *str, int str_len)
5114 #endif /* _KERNEL/!_KERNEL */
5115 {
5116 	struct sbuf sb;
5117 
5118 	sbuf_new(&sb, str, str_len, 0);
5119 
5120 #ifdef _KERNEL
5121 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5122 #else /* !_KERNEL */
5123 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5124 #endif /* _KERNEL/!_KERNEL */
5125 
5126 	sbuf_finish(&sb);
5127 
5128 	return(sbuf_data(&sb));
5129 }
5130 
5131 #ifdef _KERNEL
5132 void
5133 scsi_sense_print(struct ccb_scsiio *csio)
5134 {
5135 	struct sbuf sb;
5136 	char str[512];
5137 
5138 	sbuf_new(&sb, str, sizeof(str), 0);
5139 
5140 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5141 
5142 	sbuf_finish(&sb);
5143 
5144 	printf("%s", sbuf_data(&sb));
5145 }
5146 
5147 #else /* !_KERNEL */
5148 void
5149 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
5150 		 FILE *ofile)
5151 {
5152 	struct sbuf sb;
5153 	char str[512];
5154 
5155 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
5156 		return;
5157 
5158 	sbuf_new(&sb, str, sizeof(str), 0);
5159 
5160 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5161 
5162 	sbuf_finish(&sb);
5163 
5164 	fprintf(ofile, "%s", sbuf_data(&sb));
5165 }
5166 
5167 #endif /* _KERNEL/!_KERNEL */
5168 
5169 /*
5170  * Extract basic sense information.  This is backward-compatible with the
5171  * previous implementation.  For new implementations,
5172  * scsi_extract_sense_len() is recommended.
5173  */
5174 void
5175 scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
5176 		   int *sense_key, int *asc, int *ascq)
5177 {
5178 	scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
5179 			       sense_key, asc, ascq, /*show_errors*/ 0);
5180 }
5181 
5182 /*
5183  * Extract basic sense information from SCSI I/O CCB structure.
5184  */
5185 int
5186 scsi_extract_sense_ccb(union ccb *ccb,
5187     int *error_code, int *sense_key, int *asc, int *ascq)
5188 {
5189 	struct scsi_sense_data *sense_data;
5190 
5191 	/* Make sure there are some sense data we can access. */
5192 	if (ccb->ccb_h.func_code != XPT_SCSI_IO ||
5193 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
5194 	    (ccb->csio.scsi_status != SCSI_STATUS_CHECK_COND) ||
5195 	    (ccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0 ||
5196 	    (ccb->ccb_h.flags & CAM_SENSE_PHYS))
5197 		return (0);
5198 
5199 	if (ccb->ccb_h.flags & CAM_SENSE_PTR)
5200 		bcopy(&ccb->csio.sense_data, &sense_data,
5201 		    sizeof(struct scsi_sense_data *));
5202 	else
5203 		sense_data = &ccb->csio.sense_data;
5204 	scsi_extract_sense_len(sense_data,
5205 	    ccb->csio.sense_len - ccb->csio.sense_resid,
5206 	    error_code, sense_key, asc, ascq, 1);
5207 	if (*error_code == -1)
5208 		return (0);
5209 	return (1);
5210 }
5211 
5212 /*
5213  * Extract basic sense information.  If show_errors is set, sense values
5214  * will be set to -1 if they are not present.
5215  */
5216 void
5217 scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
5218 		       int *error_code, int *sense_key, int *asc, int *ascq,
5219 		       int show_errors)
5220 {
5221 	/*
5222 	 * If we have no length, we have no sense.
5223 	 */
5224 	if (sense_len == 0) {
5225 		if (show_errors == 0) {
5226 			*error_code = 0;
5227 			*sense_key = 0;
5228 			*asc = 0;
5229 			*ascq = 0;
5230 		} else {
5231 			*error_code = -1;
5232 			*sense_key = -1;
5233 			*asc = -1;
5234 			*ascq = -1;
5235 		}
5236 		return;
5237 	}
5238 
5239 	*error_code = sense_data->error_code & SSD_ERRCODE;
5240 
5241 	switch (*error_code) {
5242 	case SSD_DESC_CURRENT_ERROR:
5243 	case SSD_DESC_DEFERRED_ERROR: {
5244 		struct scsi_sense_data_desc *sense;
5245 
5246 		sense = (struct scsi_sense_data_desc *)sense_data;
5247 
5248 		if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
5249 			*sense_key = sense->sense_key & SSD_KEY;
5250 		else
5251 			*sense_key = (show_errors) ? -1 : 0;
5252 
5253 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
5254 			*asc = sense->add_sense_code;
5255 		else
5256 			*asc = (show_errors) ? -1 : 0;
5257 
5258 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
5259 			*ascq = sense->add_sense_code_qual;
5260 		else
5261 			*ascq = (show_errors) ? -1 : 0;
5262 		break;
5263 	}
5264 	case SSD_CURRENT_ERROR:
5265 	case SSD_DEFERRED_ERROR:
5266 	default: {
5267 		struct scsi_sense_data_fixed *sense;
5268 
5269 		sense = (struct scsi_sense_data_fixed *)sense_data;
5270 
5271 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
5272 			*sense_key = sense->flags & SSD_KEY;
5273 		else
5274 			*sense_key = (show_errors) ? -1 : 0;
5275 
5276 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
5277 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
5278 			*asc = sense->add_sense_code;
5279 		else
5280 			*asc = (show_errors) ? -1 : 0;
5281 
5282 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
5283 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
5284 			*ascq = sense->add_sense_code_qual;
5285 		else
5286 			*ascq = (show_errors) ? -1 : 0;
5287 		break;
5288 	}
5289 	}
5290 }
5291 
5292 int
5293 scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
5294 		   int show_errors)
5295 {
5296 	int error_code, sense_key, asc, ascq;
5297 
5298 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5299 			       &sense_key, &asc, &ascq, show_errors);
5300 
5301 	return (sense_key);
5302 }
5303 
5304 int
5305 scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
5306 	     int show_errors)
5307 {
5308 	int error_code, sense_key, asc, ascq;
5309 
5310 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5311 			       &sense_key, &asc, &ascq, show_errors);
5312 
5313 	return (asc);
5314 }
5315 
5316 int
5317 scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
5318 	      int show_errors)
5319 {
5320 	int error_code, sense_key, asc, ascq;
5321 
5322 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5323 			       &sense_key, &asc, &ascq, show_errors);
5324 
5325 	return (ascq);
5326 }
5327 
5328 /*
5329  * This function currently requires at least 36 bytes, or
5330  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
5331  * function needs more or less data in the future, another length should be
5332  * defined in scsi_all.h to indicate the minimum amount of data necessary
5333  * for this routine to function properly.
5334  */
5335 void
5336 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
5337 {
5338 	u_int8_t type;
5339 	char *dtype, *qtype;
5340 	char vendor[16], product[48], revision[16], rstr[12];
5341 
5342 	type = SID_TYPE(inq_data);
5343 
5344 	/*
5345 	 * Figure out basic device type and qualifier.
5346 	 */
5347 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
5348 		qtype = " (vendor-unique qualifier)";
5349 	} else {
5350 		switch (SID_QUAL(inq_data)) {
5351 		case SID_QUAL_LU_CONNECTED:
5352 			qtype = "";
5353 			break;
5354 
5355 		case SID_QUAL_LU_OFFLINE:
5356 			qtype = " (offline)";
5357 			break;
5358 
5359 		case SID_QUAL_RSVD:
5360 			qtype = " (reserved qualifier)";
5361 			break;
5362 		default:
5363 		case SID_QUAL_BAD_LU:
5364 			qtype = " (LUN not supported)";
5365 			break;
5366 		}
5367 	}
5368 
5369 	switch (type) {
5370 	case T_DIRECT:
5371 		dtype = "Direct Access";
5372 		break;
5373 	case T_SEQUENTIAL:
5374 		dtype = "Sequential Access";
5375 		break;
5376 	case T_PRINTER:
5377 		dtype = "Printer";
5378 		break;
5379 	case T_PROCESSOR:
5380 		dtype = "Processor";
5381 		break;
5382 	case T_WORM:
5383 		dtype = "WORM";
5384 		break;
5385 	case T_CDROM:
5386 		dtype = "CD-ROM";
5387 		break;
5388 	case T_SCANNER:
5389 		dtype = "Scanner";
5390 		break;
5391 	case T_OPTICAL:
5392 		dtype = "Optical";
5393 		break;
5394 	case T_CHANGER:
5395 		dtype = "Changer";
5396 		break;
5397 	case T_COMM:
5398 		dtype = "Communication";
5399 		break;
5400 	case T_STORARRAY:
5401 		dtype = "Storage Array";
5402 		break;
5403 	case T_ENCLOSURE:
5404 		dtype = "Enclosure Services";
5405 		break;
5406 	case T_RBC:
5407 		dtype = "Simplified Direct Access";
5408 		break;
5409 	case T_OCRW:
5410 		dtype = "Optical Card Read/Write";
5411 		break;
5412 	case T_OSD:
5413 		dtype = "Object-Based Storage";
5414 		break;
5415 	case T_ADC:
5416 		dtype = "Automation/Drive Interface";
5417 		break;
5418 	case T_ZBC_HM:
5419 		dtype = "Host Managed Zoned Block";
5420 		break;
5421 	case T_NODEVICE:
5422 		dtype = "Uninstalled";
5423 		break;
5424 	default:
5425 		dtype = "unknown";
5426 		break;
5427 	}
5428 
5429 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5430 		   sizeof(vendor));
5431 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5432 		   sizeof(product));
5433 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5434 		   sizeof(revision));
5435 
5436 	if (SID_ANSI_REV(inq_data) == SCSI_REV_0)
5437 		snprintf(rstr, sizeof(rstr), "SCSI");
5438 	else if (SID_ANSI_REV(inq_data) <= SCSI_REV_SPC) {
5439 		snprintf(rstr, sizeof(rstr), "SCSI-%d",
5440 		    SID_ANSI_REV(inq_data));
5441 	} else {
5442 		snprintf(rstr, sizeof(rstr), "SPC-%d SCSI",
5443 		    SID_ANSI_REV(inq_data) - 2);
5444 	}
5445 	printf("<%s %s %s> %s %s %s device%s\n",
5446 	       vendor, product, revision,
5447 	       SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
5448 	       dtype, rstr, qtype);
5449 }
5450 
5451 void
5452 scsi_print_inquiry_short(struct scsi_inquiry_data *inq_data)
5453 {
5454 	char vendor[16], product[48], revision[16];
5455 
5456 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5457 		   sizeof(vendor));
5458 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5459 		   sizeof(product));
5460 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5461 		   sizeof(revision));
5462 
5463 	printf("<%s %s %s>", vendor, product, revision);
5464 }
5465 
5466 /*
5467  * Table of syncrates that don't follow the "divisible by 4"
5468  * rule. This table will be expanded in future SCSI specs.
5469  */
5470 static struct {
5471 	u_int period_factor;
5472 	u_int period;	/* in 100ths of ns */
5473 } scsi_syncrates[] = {
5474 	{ 0x08, 625 },	/* FAST-160 */
5475 	{ 0x09, 1250 },	/* FAST-80 */
5476 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
5477 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
5478 	{ 0x0c, 5000 }	/* FAST-20 */
5479 };
5480 
5481 /*
5482  * Return the frequency in kHz corresponding to the given
5483  * sync period factor.
5484  */
5485 u_int
5486 scsi_calc_syncsrate(u_int period_factor)
5487 {
5488 	u_int i;
5489 	u_int num_syncrates;
5490 
5491 	/*
5492 	 * It's a bug if period is zero, but if it is anyway, don't
5493 	 * die with a divide fault- instead return something which
5494 	 * 'approximates' async
5495 	 */
5496 	if (period_factor == 0) {
5497 		return (3300);
5498 	}
5499 
5500 	num_syncrates = nitems(scsi_syncrates);
5501 	/* See if the period is in the "exception" table */
5502 	for (i = 0; i < num_syncrates; i++) {
5503 
5504 		if (period_factor == scsi_syncrates[i].period_factor) {
5505 			/* Period in kHz */
5506 			return (100000000 / scsi_syncrates[i].period);
5507 		}
5508 	}
5509 
5510 	/*
5511 	 * Wasn't in the table, so use the standard
5512 	 * 4 times conversion.
5513 	 */
5514 	return (10000000 / (period_factor * 4 * 10));
5515 }
5516 
5517 /*
5518  * Return the SCSI sync parameter that corresponds to
5519  * the passed in period in 10ths of ns.
5520  */
5521 u_int
5522 scsi_calc_syncparam(u_int period)
5523 {
5524 	u_int i;
5525 	u_int num_syncrates;
5526 
5527 	if (period == 0)
5528 		return (~0);	/* Async */
5529 
5530 	/* Adjust for exception table being in 100ths. */
5531 	period *= 10;
5532 	num_syncrates = nitems(scsi_syncrates);
5533 	/* See if the period is in the "exception" table */
5534 	for (i = 0; i < num_syncrates; i++) {
5535 
5536 		if (period <= scsi_syncrates[i].period) {
5537 			/* Period in 100ths of ns */
5538 			return (scsi_syncrates[i].period_factor);
5539 		}
5540 	}
5541 
5542 	/*
5543 	 * Wasn't in the table, so use the standard
5544 	 * 1/4 period in ns conversion.
5545 	 */
5546 	return (period/400);
5547 }
5548 
5549 int
5550 scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
5551 {
5552 	struct scsi_vpd_id_descriptor *descr;
5553 	struct scsi_vpd_id_naa_basic *naa;
5554 
5555 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5556 	naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
5557 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5558 		return 0;
5559 	if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
5560 		return 0;
5561 	if ((naa->naa >> SVPD_ID_NAA_NAA_SHIFT) != SVPD_ID_NAA_IEEE_REG)
5562 		return 0;
5563 	return 1;
5564 }
5565 
5566 int
5567 scsi_devid_is_sas_target(uint8_t *bufp)
5568 {
5569 	struct scsi_vpd_id_descriptor *descr;
5570 
5571 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5572 	if (!scsi_devid_is_naa_ieee_reg(bufp))
5573 		return 0;
5574 	if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
5575 		return 0;
5576 	if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
5577 		return 0;
5578 	return 1;
5579 }
5580 
5581 int
5582 scsi_devid_is_lun_eui64(uint8_t *bufp)
5583 {
5584 	struct scsi_vpd_id_descriptor *descr;
5585 
5586 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5587 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5588 		return 0;
5589 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_EUI64)
5590 		return 0;
5591 	return 1;
5592 }
5593 
5594 int
5595 scsi_devid_is_lun_naa(uint8_t *bufp)
5596 {
5597 	struct scsi_vpd_id_descriptor *descr;
5598 
5599 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5600 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5601 		return 0;
5602 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5603 		return 0;
5604 	return 1;
5605 }
5606 
5607 int
5608 scsi_devid_is_lun_t10(uint8_t *bufp)
5609 {
5610 	struct scsi_vpd_id_descriptor *descr;
5611 
5612 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5613 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5614 		return 0;
5615 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_T10)
5616 		return 0;
5617 	return 1;
5618 }
5619 
5620 int
5621 scsi_devid_is_lun_name(uint8_t *bufp)
5622 {
5623 	struct scsi_vpd_id_descriptor *descr;
5624 
5625 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5626 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5627 		return 0;
5628 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_SCSI_NAME)
5629 		return 0;
5630 	return 1;
5631 }
5632 
5633 int
5634 scsi_devid_is_port_naa(uint8_t *bufp)
5635 {
5636 	struct scsi_vpd_id_descriptor *descr;
5637 
5638 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5639 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_PORT)
5640 		return 0;
5641 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5642 		return 0;
5643 	return 1;
5644 }
5645 
5646 struct scsi_vpd_id_descriptor *
5647 scsi_get_devid_desc(struct scsi_vpd_id_descriptor *desc, uint32_t len,
5648     scsi_devid_checkfn_t ck_fn)
5649 {
5650 	uint8_t *desc_buf_end;
5651 
5652 	desc_buf_end = (uint8_t *)desc + len;
5653 
5654 	for (; desc->identifier <= desc_buf_end &&
5655 	    desc->identifier + desc->length <= desc_buf_end;
5656 	    desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
5657 						    + desc->length)) {
5658 
5659 		if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
5660 			return (desc);
5661 	}
5662 	return (NULL);
5663 }
5664 
5665 struct scsi_vpd_id_descriptor *
5666 scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
5667     scsi_devid_checkfn_t ck_fn)
5668 {
5669 	uint32_t len;
5670 
5671 	if (page_len < sizeof(*id))
5672 		return (NULL);
5673 	len = MIN(scsi_2btoul(id->length), page_len - sizeof(*id));
5674 	return (scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5675 	    id->desc_list, len, ck_fn));
5676 }
5677 
5678 int
5679 scsi_transportid_sbuf(struct sbuf *sb, struct scsi_transportid_header *hdr,
5680 		      uint32_t valid_len)
5681 {
5682 	switch (hdr->format_protocol & SCSI_TRN_PROTO_MASK) {
5683 	case SCSI_PROTO_FC: {
5684 		struct scsi_transportid_fcp *fcp;
5685 		uint64_t n_port_name;
5686 
5687 		fcp = (struct scsi_transportid_fcp *)hdr;
5688 
5689 		n_port_name = scsi_8btou64(fcp->n_port_name);
5690 
5691 		sbuf_printf(sb, "FCP address: 0x%.16jx",(uintmax_t)n_port_name);
5692 		break;
5693 	}
5694 	case SCSI_PROTO_SPI: {
5695 		struct scsi_transportid_spi *spi;
5696 
5697 		spi = (struct scsi_transportid_spi *)hdr;
5698 
5699 		sbuf_printf(sb, "SPI address: %u,%u",
5700 			    scsi_2btoul(spi->scsi_addr),
5701 			    scsi_2btoul(spi->rel_trgt_port_id));
5702 		break;
5703 	}
5704 	case SCSI_PROTO_SSA:
5705 		/*
5706 		 * XXX KDM there is no transport ID defined in SPC-4 for
5707 		 * SSA.
5708 		 */
5709 		break;
5710 	case SCSI_PROTO_1394: {
5711 		struct scsi_transportid_1394 *sbp;
5712 		uint64_t eui64;
5713 
5714 		sbp = (struct scsi_transportid_1394 *)hdr;
5715 
5716 		eui64 = scsi_8btou64(sbp->eui64);
5717 		sbuf_printf(sb, "SBP address: 0x%.16jx", (uintmax_t)eui64);
5718 		break;
5719 	}
5720 	case SCSI_PROTO_RDMA: {
5721 		struct scsi_transportid_rdma *rdma;
5722 		unsigned int i;
5723 
5724 		rdma = (struct scsi_transportid_rdma *)hdr;
5725 
5726 		sbuf_printf(sb, "RDMA address: 0x");
5727 		for (i = 0; i < sizeof(rdma->initiator_port_id); i++)
5728 			sbuf_printf(sb, "%02x", rdma->initiator_port_id[i]);
5729 		break;
5730 	}
5731 	case SCSI_PROTO_ISCSI: {
5732 		uint32_t add_len, i;
5733 		uint8_t *iscsi_name = NULL;
5734 		int nul_found = 0;
5735 
5736 		sbuf_printf(sb, "iSCSI address: ");
5737 		if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5738 		    SCSI_TRN_ISCSI_FORMAT_DEVICE) {
5739 			struct scsi_transportid_iscsi_device *dev;
5740 
5741 			dev = (struct scsi_transportid_iscsi_device *)hdr;
5742 
5743 			/*
5744 			 * Verify how much additional data we really have.
5745 			 */
5746 			add_len = scsi_2btoul(dev->additional_length);
5747 			add_len = MIN(add_len, valid_len -
5748 				__offsetof(struct scsi_transportid_iscsi_device,
5749 					   iscsi_name));
5750 			iscsi_name = &dev->iscsi_name[0];
5751 
5752 		} else if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5753 			    SCSI_TRN_ISCSI_FORMAT_PORT) {
5754 			struct scsi_transportid_iscsi_port *port;
5755 
5756 			port = (struct scsi_transportid_iscsi_port *)hdr;
5757 
5758 			add_len = scsi_2btoul(port->additional_length);
5759 			add_len = MIN(add_len, valid_len -
5760 				__offsetof(struct scsi_transportid_iscsi_port,
5761 					   iscsi_name));
5762 			iscsi_name = &port->iscsi_name[0];
5763 		} else {
5764 			sbuf_printf(sb, "unknown format %x",
5765 				    (hdr->format_protocol &
5766 				     SCSI_TRN_FORMAT_MASK) >>
5767 				     SCSI_TRN_FORMAT_SHIFT);
5768 			break;
5769 		}
5770 		if (add_len == 0) {
5771 			sbuf_printf(sb, "not enough data");
5772 			break;
5773 		}
5774 		/*
5775 		 * This is supposed to be a NUL-terminated ASCII
5776 		 * string, but you never know.  So we're going to
5777 		 * check.  We need to do this because there is no
5778 		 * sbuf equivalent of strncat().
5779 		 */
5780 		for (i = 0; i < add_len; i++) {
5781 			if (iscsi_name[i] == '\0') {
5782 				nul_found = 1;
5783 				break;
5784 			}
5785 		}
5786 		/*
5787 		 * If there is a NUL in the name, we can just use
5788 		 * sbuf_cat().  Otherwise we need to use sbuf_bcat().
5789 		 */
5790 		if (nul_found != 0)
5791 			sbuf_cat(sb, iscsi_name);
5792 		else
5793 			sbuf_bcat(sb, iscsi_name, add_len);
5794 		break;
5795 	}
5796 	case SCSI_PROTO_SAS: {
5797 		struct scsi_transportid_sas *sas;
5798 		uint64_t sas_addr;
5799 
5800 		sas = (struct scsi_transportid_sas *)hdr;
5801 
5802 		sas_addr = scsi_8btou64(sas->sas_address);
5803 		sbuf_printf(sb, "SAS address: 0x%.16jx", (uintmax_t)sas_addr);
5804 		break;
5805 	}
5806 	case SCSI_PROTO_ADITP:
5807 	case SCSI_PROTO_ATA:
5808 	case SCSI_PROTO_UAS:
5809 		/*
5810 		 * No Transport ID format for ADI, ATA or USB is defined in
5811 		 * SPC-4.
5812 		 */
5813 		sbuf_printf(sb, "No known Transport ID format for protocol "
5814 			    "%#x", hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5815 		break;
5816 	case SCSI_PROTO_SOP: {
5817 		struct scsi_transportid_sop *sop;
5818 		struct scsi_sop_routing_id_norm *rid;
5819 
5820 		sop = (struct scsi_transportid_sop *)hdr;
5821 		rid = (struct scsi_sop_routing_id_norm *)sop->routing_id;
5822 
5823 		/*
5824 		 * Note that there is no alternate format specified in SPC-4
5825 		 * for the PCIe routing ID, so we don't really have a way
5826 		 * to know whether the second byte of the routing ID is
5827 		 * a device and function or just a function.  So we just
5828 		 * assume bus,device,function.
5829 		 */
5830 		sbuf_printf(sb, "SOP Routing ID: %u,%u,%u",
5831 			    rid->bus, rid->devfunc >> SCSI_TRN_SOP_DEV_SHIFT,
5832 			    rid->devfunc & SCSI_TRN_SOP_FUNC_NORM_MAX);
5833 		break;
5834 	}
5835 	case SCSI_PROTO_NONE:
5836 	default:
5837 		sbuf_printf(sb, "Unknown protocol %#x",
5838 			    hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5839 		break;
5840 	}
5841 
5842 	return (0);
5843 }
5844 
5845 struct scsi_nv scsi_proto_map[] = {
5846 	{ "fcp", SCSI_PROTO_FC },
5847 	{ "spi", SCSI_PROTO_SPI },
5848 	{ "ssa", SCSI_PROTO_SSA },
5849 	{ "sbp", SCSI_PROTO_1394 },
5850 	{ "1394", SCSI_PROTO_1394 },
5851 	{ "srp", SCSI_PROTO_RDMA },
5852 	{ "rdma", SCSI_PROTO_RDMA },
5853 	{ "iscsi", SCSI_PROTO_ISCSI },
5854 	{ "iqn", SCSI_PROTO_ISCSI },
5855 	{ "sas", SCSI_PROTO_SAS },
5856 	{ "aditp", SCSI_PROTO_ADITP },
5857 	{ "ata", SCSI_PROTO_ATA },
5858 	{ "uas", SCSI_PROTO_UAS },
5859 	{ "usb", SCSI_PROTO_UAS },
5860 	{ "sop", SCSI_PROTO_SOP }
5861 };
5862 
5863 const char *
5864 scsi_nv_to_str(struct scsi_nv *table, int num_table_entries, uint64_t value)
5865 {
5866 	int i;
5867 
5868 	for (i = 0; i < num_table_entries; i++) {
5869 		if (table[i].value == value)
5870 			return (table[i].name);
5871 	}
5872 
5873 	return (NULL);
5874 }
5875 
5876 /*
5877  * Given a name/value table, find a value matching the given name.
5878  * Return values:
5879  *	SCSI_NV_FOUND - match found
5880  *	SCSI_NV_AMBIGUOUS - more than one match, none of them exact
5881  *	SCSI_NV_NOT_FOUND - no match found
5882  */
5883 scsi_nv_status
5884 scsi_get_nv(struct scsi_nv *table, int num_table_entries,
5885 	    char *name, int *table_entry, scsi_nv_flags flags)
5886 {
5887 	int i, num_matches = 0;
5888 
5889 	for (i = 0; i < num_table_entries; i++) {
5890 		size_t table_len, name_len;
5891 
5892 		table_len = strlen(table[i].name);
5893 		name_len = strlen(name);
5894 
5895 		if ((((flags & SCSI_NV_FLAG_IG_CASE) != 0)
5896 		  && (strncasecmp(table[i].name, name, name_len) == 0))
5897 		|| (((flags & SCSI_NV_FLAG_IG_CASE) == 0)
5898 		 && (strncmp(table[i].name, name, name_len) == 0))) {
5899 			*table_entry = i;
5900 
5901 			/*
5902 			 * Check for an exact match.  If we have the same
5903 			 * number of characters in the table as the argument,
5904 			 * and we already know they're the same, we have
5905 			 * an exact match.
5906 		 	 */
5907 			if (table_len == name_len)
5908 				return (SCSI_NV_FOUND);
5909 
5910 			/*
5911 			 * Otherwise, bump up the number of matches.  We'll
5912 			 * see later how many we have.
5913 			 */
5914 			num_matches++;
5915 		}
5916 	}
5917 
5918 	if (num_matches > 1)
5919 		return (SCSI_NV_AMBIGUOUS);
5920 	else if (num_matches == 1)
5921 		return (SCSI_NV_FOUND);
5922 	else
5923 		return (SCSI_NV_NOT_FOUND);
5924 }
5925 
5926 /*
5927  * Parse transport IDs for Fibre Channel, 1394 and SAS.  Since these are
5928  * all 64-bit numbers, the code is similar.
5929  */
5930 int
5931 scsi_parse_transportid_64bit(int proto_id, char *id_str,
5932 			     struct scsi_transportid_header **hdr,
5933 			     unsigned int *alloc_len,
5934 #ifdef _KERNEL
5935 			     struct malloc_type *type, int flags,
5936 #endif
5937 			     char *error_str, int error_str_len)
5938 {
5939 	uint64_t value;
5940 	char *endptr;
5941 	int retval;
5942 	size_t alloc_size;
5943 
5944 	retval = 0;
5945 
5946 	value = strtouq(id_str, &endptr, 0);
5947 	if (*endptr != '\0') {
5948 		if (error_str != NULL) {
5949 			snprintf(error_str, error_str_len, "%s: error "
5950 				 "parsing ID %s, 64-bit number required",
5951 				 __func__, id_str);
5952 		}
5953 		retval = 1;
5954 		goto bailout;
5955 	}
5956 
5957 	switch (proto_id) {
5958 	case SCSI_PROTO_FC:
5959 		alloc_size = sizeof(struct scsi_transportid_fcp);
5960 		break;
5961 	case SCSI_PROTO_1394:
5962 		alloc_size = sizeof(struct scsi_transportid_1394);
5963 		break;
5964 	case SCSI_PROTO_SAS:
5965 		alloc_size = sizeof(struct scsi_transportid_sas);
5966 		break;
5967 	default:
5968 		if (error_str != NULL) {
5969 			snprintf(error_str, error_str_len, "%s: unsupported "
5970 				 "protocol %d", __func__, proto_id);
5971 		}
5972 		retval = 1;
5973 		goto bailout;
5974 		break; /* NOTREACHED */
5975 	}
5976 #ifdef _KERNEL
5977 	*hdr = malloc(alloc_size, type, flags);
5978 #else /* _KERNEL */
5979 	*hdr = malloc(alloc_size);
5980 #endif /*_KERNEL */
5981 	if (*hdr == NULL) {
5982 		if (error_str != NULL) {
5983 			snprintf(error_str, error_str_len, "%s: unable to "
5984 				 "allocate %zu bytes", __func__, alloc_size);
5985 		}
5986 		retval = 1;
5987 		goto bailout;
5988 	}
5989 
5990 	*alloc_len = alloc_size;
5991 
5992 	bzero(*hdr, alloc_size);
5993 
5994 	switch (proto_id) {
5995 	case SCSI_PROTO_FC: {
5996 		struct scsi_transportid_fcp *fcp;
5997 
5998 		fcp = (struct scsi_transportid_fcp *)(*hdr);
5999 		fcp->format_protocol = SCSI_PROTO_FC |
6000 				       SCSI_TRN_FCP_FORMAT_DEFAULT;
6001 		scsi_u64to8b(value, fcp->n_port_name);
6002 		break;
6003 	}
6004 	case SCSI_PROTO_1394: {
6005 		struct scsi_transportid_1394 *sbp;
6006 
6007 		sbp = (struct scsi_transportid_1394 *)(*hdr);
6008 		sbp->format_protocol = SCSI_PROTO_1394 |
6009 				       SCSI_TRN_1394_FORMAT_DEFAULT;
6010 		scsi_u64to8b(value, sbp->eui64);
6011 		break;
6012 	}
6013 	case SCSI_PROTO_SAS: {
6014 		struct scsi_transportid_sas *sas;
6015 
6016 		sas = (struct scsi_transportid_sas *)(*hdr);
6017 		sas->format_protocol = SCSI_PROTO_SAS |
6018 				       SCSI_TRN_SAS_FORMAT_DEFAULT;
6019 		scsi_u64to8b(value, sas->sas_address);
6020 		break;
6021 	}
6022 	default:
6023 		break;
6024 	}
6025 bailout:
6026 	return (retval);
6027 }
6028 
6029 /*
6030  * Parse a SPI (Parallel SCSI) address of the form: id,rel_tgt_port
6031  */
6032 int
6033 scsi_parse_transportid_spi(char *id_str, struct scsi_transportid_header **hdr,
6034 			   unsigned int *alloc_len,
6035 #ifdef _KERNEL
6036 			   struct malloc_type *type, int flags,
6037 #endif
6038 			   char *error_str, int error_str_len)
6039 {
6040 	unsigned long scsi_addr, target_port;
6041 	struct scsi_transportid_spi *spi;
6042 	char *tmpstr, *endptr;
6043 	int retval;
6044 
6045 	retval = 0;
6046 
6047 	tmpstr = strsep(&id_str, ",");
6048 	if (tmpstr == NULL) {
6049 		if (error_str != NULL) {
6050 			snprintf(error_str, error_str_len,
6051 				 "%s: no ID found", __func__);
6052 		}
6053 		retval = 1;
6054 		goto bailout;
6055 	}
6056 	scsi_addr = strtoul(tmpstr, &endptr, 0);
6057 	if (*endptr != '\0') {
6058 		if (error_str != NULL) {
6059 			snprintf(error_str, error_str_len, "%s: error "
6060 				 "parsing SCSI ID %s, number required",
6061 				 __func__, tmpstr);
6062 		}
6063 		retval = 1;
6064 		goto bailout;
6065 	}
6066 
6067 	if (id_str == NULL) {
6068 		if (error_str != NULL) {
6069 			snprintf(error_str, error_str_len, "%s: no relative "
6070 				 "target port found", __func__);
6071 		}
6072 		retval = 1;
6073 		goto bailout;
6074 	}
6075 
6076 	target_port = strtoul(id_str, &endptr, 0);
6077 	if (*endptr != '\0') {
6078 		if (error_str != NULL) {
6079 			snprintf(error_str, error_str_len, "%s: error "
6080 				 "parsing relative target port %s, number "
6081 				 "required", __func__, id_str);
6082 		}
6083 		retval = 1;
6084 		goto bailout;
6085 	}
6086 #ifdef _KERNEL
6087 	spi = malloc(sizeof(*spi), type, flags);
6088 #else
6089 	spi = malloc(sizeof(*spi));
6090 #endif
6091 	if (spi == NULL) {
6092 		if (error_str != NULL) {
6093 			snprintf(error_str, error_str_len, "%s: unable to "
6094 				 "allocate %zu bytes", __func__,
6095 				 sizeof(*spi));
6096 		}
6097 		retval = 1;
6098 		goto bailout;
6099 	}
6100 	*alloc_len = sizeof(*spi);
6101 	bzero(spi, sizeof(*spi));
6102 
6103 	spi->format_protocol = SCSI_PROTO_SPI | SCSI_TRN_SPI_FORMAT_DEFAULT;
6104 	scsi_ulto2b(scsi_addr, spi->scsi_addr);
6105 	scsi_ulto2b(target_port, spi->rel_trgt_port_id);
6106 
6107 	*hdr = (struct scsi_transportid_header *)spi;
6108 bailout:
6109 	return (retval);
6110 }
6111 
6112 /*
6113  * Parse an RDMA/SRP Initiator Port ID string.  This is 32 hexadecimal digits,
6114  * optionally prefixed by "0x" or "0X".
6115  */
6116 int
6117 scsi_parse_transportid_rdma(char *id_str, struct scsi_transportid_header **hdr,
6118 			    unsigned int *alloc_len,
6119 #ifdef _KERNEL
6120 			    struct malloc_type *type, int flags,
6121 #endif
6122 			    char *error_str, int error_str_len)
6123 {
6124 	struct scsi_transportid_rdma *rdma;
6125 	int retval;
6126 	size_t id_len, rdma_id_size;
6127 	uint8_t rdma_id[SCSI_TRN_RDMA_PORT_LEN];
6128 	char *tmpstr;
6129 	unsigned int i, j;
6130 
6131 	retval = 0;
6132 	id_len = strlen(id_str);
6133 	rdma_id_size = SCSI_TRN_RDMA_PORT_LEN;
6134 
6135 	/*
6136 	 * Check the size.  It needs to be either 32 or 34 characters long.
6137 	 */
6138 	if ((id_len != (rdma_id_size * 2))
6139 	 && (id_len != ((rdma_id_size * 2) + 2))) {
6140 		if (error_str != NULL) {
6141 			snprintf(error_str, error_str_len, "%s: RDMA ID "
6142 				 "must be 32 hex digits (0x prefix "
6143 				 "optional), only %zu seen", __func__, id_len);
6144 		}
6145 		retval = 1;
6146 		goto bailout;
6147 	}
6148 
6149 	tmpstr = id_str;
6150 	/*
6151 	 * If the user gave us 34 characters, the string needs to start
6152 	 * with '0x'.
6153 	 */
6154 	if (id_len == ((rdma_id_size * 2) + 2)) {
6155 	 	if ((tmpstr[0] == '0')
6156 		 && ((tmpstr[1] == 'x') || (tmpstr[1] == 'X'))) {
6157 			tmpstr += 2;
6158 		} else {
6159 			if (error_str != NULL) {
6160 				snprintf(error_str, error_str_len, "%s: RDMA "
6161 					 "ID prefix, if used, must be \"0x\", "
6162 					 "got %s", __func__, tmpstr);
6163 			}
6164 			retval = 1;
6165 			goto bailout;
6166 		}
6167 	}
6168 	bzero(rdma_id, sizeof(rdma_id));
6169 
6170 	/*
6171 	 * Convert ASCII hex into binary bytes.  There is no standard
6172 	 * 128-bit integer type, and so no strtou128t() routine to convert
6173 	 * from hex into a large integer.  In the end, we're not going to
6174 	 * an integer, but rather to a byte array, so that and the fact
6175 	 * that we require the user to give us 32 hex digits simplifies the
6176 	 * logic.
6177 	 */
6178 	for (i = 0; i < (rdma_id_size * 2); i++) {
6179 		int cur_shift;
6180 		unsigned char c;
6181 
6182 		/* Increment the byte array one for every 2 hex digits */
6183 		j = i >> 1;
6184 
6185 		/*
6186 		 * The first digit in every pair is the most significant
6187 		 * 4 bits.  The second is the least significant 4 bits.
6188 		 */
6189 		if ((i % 2) == 0)
6190 			cur_shift = 4;
6191 		else
6192 			cur_shift = 0;
6193 
6194 		c = tmpstr[i];
6195 		/* Convert the ASCII hex character into a number */
6196 		if (isdigit(c))
6197 			c -= '0';
6198 		else if (isalpha(c))
6199 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
6200 		else {
6201 			if (error_str != NULL) {
6202 				snprintf(error_str, error_str_len, "%s: "
6203 					 "RDMA ID must be hex digits, got "
6204 					 "invalid character %c", __func__,
6205 					 tmpstr[i]);
6206 			}
6207 			retval = 1;
6208 			goto bailout;
6209 		}
6210 		/*
6211 		 * The converted number can't be less than 0; the type is
6212 		 * unsigned, and the subtraction logic will not give us
6213 		 * a negative number.  So we only need to make sure that
6214 		 * the value is not greater than 0xf.  (i.e. make sure the
6215 		 * user didn't give us a value like "0x12jklmno").
6216 		 */
6217 		if (c > 0xf) {
6218 			if (error_str != NULL) {
6219 				snprintf(error_str, error_str_len, "%s: "
6220 					 "RDMA ID must be hex digits, got "
6221 					 "invalid character %c", __func__,
6222 					 tmpstr[i]);
6223 			}
6224 			retval = 1;
6225 			goto bailout;
6226 		}
6227 
6228 		rdma_id[j] |= c << cur_shift;
6229 	}
6230 
6231 #ifdef _KERNEL
6232 	rdma = malloc(sizeof(*rdma), type, flags);
6233 #else
6234 	rdma = malloc(sizeof(*rdma));
6235 #endif
6236 	if (rdma == NULL) {
6237 		if (error_str != NULL) {
6238 			snprintf(error_str, error_str_len, "%s: unable to "
6239 				 "allocate %zu bytes", __func__,
6240 				 sizeof(*rdma));
6241 		}
6242 		retval = 1;
6243 		goto bailout;
6244 	}
6245 	*alloc_len = sizeof(*rdma);
6246 	bzero(rdma, *alloc_len);
6247 
6248 	rdma->format_protocol = SCSI_PROTO_RDMA | SCSI_TRN_RDMA_FORMAT_DEFAULT;
6249 	bcopy(rdma_id, rdma->initiator_port_id, SCSI_TRN_RDMA_PORT_LEN);
6250 
6251 	*hdr = (struct scsi_transportid_header *)rdma;
6252 
6253 bailout:
6254 	return (retval);
6255 }
6256 
6257 /*
6258  * Parse an iSCSI name.  The format is either just the name:
6259  *
6260  *	iqn.2012-06.com.example:target0
6261  * or the name, separator and initiator session ID:
6262  *
6263  *	iqn.2012-06.com.example:target0,i,0x123
6264  *
6265  * The separator format is exact.
6266  */
6267 int
6268 scsi_parse_transportid_iscsi(char *id_str, struct scsi_transportid_header **hdr,
6269 			     unsigned int *alloc_len,
6270 #ifdef _KERNEL
6271 			     struct malloc_type *type, int flags,
6272 #endif
6273 			     char *error_str, int error_str_len)
6274 {
6275 	size_t id_len, sep_len, id_size, name_len;
6276 	int retval;
6277 	unsigned int i, sep_pos, sep_found;
6278 	const char *sep_template = ",i,0x";
6279 	const char *iqn_prefix = "iqn.";
6280 	struct scsi_transportid_iscsi_device *iscsi;
6281 
6282 	retval = 0;
6283 	sep_found = 0;
6284 
6285 	id_len = strlen(id_str);
6286 	sep_len = strlen(sep_template);
6287 
6288 	/*
6289 	 * The separator is defined as exactly ',i,0x'.  Any other commas,
6290 	 * or any other form, is an error.  So look for a comma, and once
6291 	 * we find that, the next few characters must match the separator
6292 	 * exactly.  Once we get through the separator, there should be at
6293 	 * least one character.
6294 	 */
6295 	for (i = 0, sep_pos = 0; i < id_len; i++) {
6296 		if (sep_pos == 0) {
6297 		 	if (id_str[i] == sep_template[sep_pos])
6298 				sep_pos++;
6299 
6300 			continue;
6301 		}
6302 		if (sep_pos < sep_len) {
6303 			if (id_str[i] == sep_template[sep_pos]) {
6304 				sep_pos++;
6305 				continue;
6306 			}
6307 			if (error_str != NULL) {
6308 				snprintf(error_str, error_str_len, "%s: "
6309 					 "invalid separator in iSCSI name "
6310 					 "\"%s\"",
6311 					 __func__, id_str);
6312 			}
6313 			retval = 1;
6314 			goto bailout;
6315 		} else {
6316 			sep_found = 1;
6317 			break;
6318 		}
6319 	}
6320 
6321 	/*
6322 	 * Check to see whether we have a separator but no digits after it.
6323 	 */
6324 	if ((sep_pos != 0)
6325 	 && (sep_found == 0)) {
6326 		if (error_str != NULL) {
6327 			snprintf(error_str, error_str_len, "%s: no digits "
6328 				 "found after separator in iSCSI name \"%s\"",
6329 				 __func__, id_str);
6330 		}
6331 		retval = 1;
6332 		goto bailout;
6333 	}
6334 
6335 	/*
6336 	 * The incoming ID string has the "iqn." prefix stripped off.  We
6337 	 * need enough space for the base structure (the structures are the
6338 	 * same for the two iSCSI forms), the prefix, the ID string and a
6339 	 * terminating NUL.
6340 	 */
6341 	id_size = sizeof(*iscsi) + strlen(iqn_prefix) + id_len + 1;
6342 
6343 #ifdef _KERNEL
6344 	iscsi = malloc(id_size, type, flags);
6345 #else
6346 	iscsi = malloc(id_size);
6347 #endif
6348 	if (iscsi == NULL) {
6349 		if (error_str != NULL) {
6350 			snprintf(error_str, error_str_len, "%s: unable to "
6351 				 "allocate %zu bytes", __func__, id_size);
6352 		}
6353 		retval = 1;
6354 		goto bailout;
6355 	}
6356 	*alloc_len = id_size;
6357 	bzero(iscsi, id_size);
6358 
6359 	iscsi->format_protocol = SCSI_PROTO_ISCSI;
6360 	if (sep_found == 0)
6361 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_DEVICE;
6362 	else
6363 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_PORT;
6364 	name_len = id_size - sizeof(*iscsi);
6365 	scsi_ulto2b(name_len, iscsi->additional_length);
6366 	snprintf(iscsi->iscsi_name, name_len, "%s%s", iqn_prefix, id_str);
6367 
6368 	*hdr = (struct scsi_transportid_header *)iscsi;
6369 
6370 bailout:
6371 	return (retval);
6372 }
6373 
6374 /*
6375  * Parse a SCSI over PCIe (SOP) identifier.  The Routing ID can either be
6376  * of the form 'bus,device,function' or 'bus,function'.
6377  */
6378 int
6379 scsi_parse_transportid_sop(char *id_str, struct scsi_transportid_header **hdr,
6380 			   unsigned int *alloc_len,
6381 #ifdef _KERNEL
6382 			   struct malloc_type *type, int flags,
6383 #endif
6384 			   char *error_str, int error_str_len)
6385 {
6386 	struct scsi_transportid_sop *sop;
6387 	unsigned long bus, device, function;
6388 	char *tmpstr, *endptr;
6389 	int retval, device_spec;
6390 
6391 	retval = 0;
6392 	device_spec = 0;
6393 	device = 0;
6394 
6395 	tmpstr = strsep(&id_str, ",");
6396 	if ((tmpstr == NULL)
6397 	 || (*tmpstr == '\0')) {
6398 		if (error_str != NULL) {
6399 			snprintf(error_str, error_str_len, "%s: no ID found",
6400 				 __func__);
6401 		}
6402 		retval = 1;
6403 		goto bailout;
6404 	}
6405 	bus = strtoul(tmpstr, &endptr, 0);
6406 	if (*endptr != '\0') {
6407 		if (error_str != NULL) {
6408 			snprintf(error_str, error_str_len, "%s: error "
6409 				 "parsing PCIe bus %s, number required",
6410 				 __func__, tmpstr);
6411 		}
6412 		retval = 1;
6413 		goto bailout;
6414 	}
6415 	if ((id_str == NULL)
6416 	 || (*id_str == '\0')) {
6417 		if (error_str != NULL) {
6418 			snprintf(error_str, error_str_len, "%s: no PCIe "
6419 				 "device or function found", __func__);
6420 		}
6421 		retval = 1;
6422 		goto bailout;
6423 	}
6424 	tmpstr = strsep(&id_str, ",");
6425 	function = strtoul(tmpstr, &endptr, 0);
6426 	if (*endptr != '\0') {
6427 		if (error_str != NULL) {
6428 			snprintf(error_str, error_str_len, "%s: error "
6429 				 "parsing PCIe device/function %s, number "
6430 				 "required", __func__, tmpstr);
6431 		}
6432 		retval = 1;
6433 		goto bailout;
6434 	}
6435 	/*
6436 	 * Check to see whether the user specified a third value.  If so,
6437 	 * the second is the device.
6438 	 */
6439 	if (id_str != NULL) {
6440 		if (*id_str == '\0') {
6441 			if (error_str != NULL) {
6442 				snprintf(error_str, error_str_len, "%s: "
6443 					 "no PCIe function found", __func__);
6444 			}
6445 			retval = 1;
6446 			goto bailout;
6447 		}
6448 		device = function;
6449 		device_spec = 1;
6450 		function = strtoul(id_str, &endptr, 0);
6451 		if (*endptr != '\0') {
6452 			if (error_str != NULL) {
6453 				snprintf(error_str, error_str_len, "%s: "
6454 					 "error parsing PCIe function %s, "
6455 					 "number required", __func__, id_str);
6456 			}
6457 			retval = 1;
6458 			goto bailout;
6459 		}
6460 	}
6461 	if (bus > SCSI_TRN_SOP_BUS_MAX) {
6462 		if (error_str != NULL) {
6463 			snprintf(error_str, error_str_len, "%s: bus value "
6464 				 "%lu greater than maximum %u", __func__,
6465 				 bus, SCSI_TRN_SOP_BUS_MAX);
6466 		}
6467 		retval = 1;
6468 		goto bailout;
6469 	}
6470 
6471 	if ((device_spec != 0)
6472 	 && (device > SCSI_TRN_SOP_DEV_MASK)) {
6473 		if (error_str != NULL) {
6474 			snprintf(error_str, error_str_len, "%s: device value "
6475 				 "%lu greater than maximum %u", __func__,
6476 				 device, SCSI_TRN_SOP_DEV_MAX);
6477 		}
6478 		retval = 1;
6479 		goto bailout;
6480 	}
6481 
6482 	if (((device_spec != 0)
6483 	  && (function > SCSI_TRN_SOP_FUNC_NORM_MAX))
6484 	 || ((device_spec == 0)
6485 	  && (function > SCSI_TRN_SOP_FUNC_ALT_MAX))) {
6486 		if (error_str != NULL) {
6487 			snprintf(error_str, error_str_len, "%s: function value "
6488 				 "%lu greater than maximum %u", __func__,
6489 				 function, (device_spec == 0) ?
6490 				 SCSI_TRN_SOP_FUNC_ALT_MAX :
6491 				 SCSI_TRN_SOP_FUNC_NORM_MAX);
6492 		}
6493 		retval = 1;
6494 		goto bailout;
6495 	}
6496 
6497 #ifdef _KERNEL
6498 	sop = malloc(sizeof(*sop), type, flags);
6499 #else
6500 	sop = malloc(sizeof(*sop));
6501 #endif
6502 	if (sop == NULL) {
6503 		if (error_str != NULL) {
6504 			snprintf(error_str, error_str_len, "%s: unable to "
6505 				 "allocate %zu bytes", __func__, sizeof(*sop));
6506 		}
6507 		retval = 1;
6508 		goto bailout;
6509 	}
6510 	*alloc_len = sizeof(*sop);
6511 	bzero(sop, sizeof(*sop));
6512 	sop->format_protocol = SCSI_PROTO_SOP | SCSI_TRN_SOP_FORMAT_DEFAULT;
6513 	if (device_spec != 0) {
6514 		struct scsi_sop_routing_id_norm rid;
6515 
6516 		rid.bus = bus;
6517 		rid.devfunc = (device << SCSI_TRN_SOP_DEV_SHIFT) | function;
6518 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6519 		      sizeof(sop->routing_id)));
6520 	} else {
6521 		struct scsi_sop_routing_id_alt rid;
6522 
6523 		rid.bus = bus;
6524 		rid.function = function;
6525 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6526 		      sizeof(sop->routing_id)));
6527 	}
6528 
6529 	*hdr = (struct scsi_transportid_header *)sop;
6530 bailout:
6531 	return (retval);
6532 }
6533 
6534 /*
6535  * transportid_str: NUL-terminated string with format: protcol,id
6536  *		    The ID is protocol specific.
6537  * hdr:		    Storage will be allocated for the transport ID.
6538  * alloc_len:	    The amount of memory allocated is returned here.
6539  * type:	    Malloc bucket (kernel only).
6540  * flags:	    Malloc flags (kernel only).
6541  * error_str:	    If non-NULL, it will contain error information (without
6542  * 		    a terminating newline) if an error is returned.
6543  * error_str_len:   Allocated length of the error string.
6544  *
6545  * Returns 0 for success, non-zero for failure.
6546  */
6547 int
6548 scsi_parse_transportid(char *transportid_str,
6549 		       struct scsi_transportid_header **hdr,
6550 		       unsigned int *alloc_len,
6551 #ifdef _KERNEL
6552 		       struct malloc_type *type, int flags,
6553 #endif
6554 		       char *error_str, int error_str_len)
6555 {
6556 	char *tmpstr;
6557 	scsi_nv_status status;
6558 	u_int num_proto_entries;
6559 	int retval, table_entry;
6560 
6561 	retval = 0;
6562 	table_entry = 0;
6563 
6564 	/*
6565 	 * We do allow a period as well as a comma to separate the protocol
6566 	 * from the ID string.  This is to accommodate iSCSI names, which
6567 	 * start with "iqn.".
6568 	 */
6569 	tmpstr = strsep(&transportid_str, ",.");
6570 	if (tmpstr == NULL) {
6571 		if (error_str != NULL) {
6572 			snprintf(error_str, error_str_len,
6573 				 "%s: transportid_str is NULL", __func__);
6574 		}
6575 		retval = 1;
6576 		goto bailout;
6577 	}
6578 
6579 	num_proto_entries = nitems(scsi_proto_map);
6580 	status = scsi_get_nv(scsi_proto_map, num_proto_entries, tmpstr,
6581 			     &table_entry, SCSI_NV_FLAG_IG_CASE);
6582 	if (status != SCSI_NV_FOUND) {
6583 		if (error_str != NULL) {
6584 			snprintf(error_str, error_str_len, "%s: %s protocol "
6585 				 "name %s", __func__,
6586 				 (status == SCSI_NV_AMBIGUOUS) ? "ambiguous" :
6587 				 "invalid", tmpstr);
6588 		}
6589 		retval = 1;
6590 		goto bailout;
6591 	}
6592 	switch (scsi_proto_map[table_entry].value) {
6593 	case SCSI_PROTO_FC:
6594 	case SCSI_PROTO_1394:
6595 	case SCSI_PROTO_SAS:
6596 		retval = scsi_parse_transportid_64bit(
6597 		    scsi_proto_map[table_entry].value, transportid_str, hdr,
6598 		    alloc_len,
6599 #ifdef _KERNEL
6600 		    type, flags,
6601 #endif
6602 		    error_str, error_str_len);
6603 		break;
6604 	case SCSI_PROTO_SPI:
6605 		retval = scsi_parse_transportid_spi(transportid_str, hdr,
6606 		    alloc_len,
6607 #ifdef _KERNEL
6608 		    type, flags,
6609 #endif
6610 		    error_str, error_str_len);
6611 		break;
6612 	case SCSI_PROTO_RDMA:
6613 		retval = scsi_parse_transportid_rdma(transportid_str, hdr,
6614 		    alloc_len,
6615 #ifdef _KERNEL
6616 		    type, flags,
6617 #endif
6618 		    error_str, error_str_len);
6619 		break;
6620 	case SCSI_PROTO_ISCSI:
6621 		retval = scsi_parse_transportid_iscsi(transportid_str, hdr,
6622 		    alloc_len,
6623 #ifdef _KERNEL
6624 		    type, flags,
6625 #endif
6626 		    error_str, error_str_len);
6627 		break;
6628 	case SCSI_PROTO_SOP:
6629 		retval = scsi_parse_transportid_sop(transportid_str, hdr,
6630 		    alloc_len,
6631 #ifdef _KERNEL
6632 		    type, flags,
6633 #endif
6634 		    error_str, error_str_len);
6635 		break;
6636 	case SCSI_PROTO_SSA:
6637 	case SCSI_PROTO_ADITP:
6638 	case SCSI_PROTO_ATA:
6639 	case SCSI_PROTO_UAS:
6640 	case SCSI_PROTO_NONE:
6641 	default:
6642 		/*
6643 		 * There is no format defined for a Transport ID for these
6644 		 * protocols.  So even if the user gives us something, we
6645 		 * have no way to turn it into a standard SCSI Transport ID.
6646 		 */
6647 		retval = 1;
6648 		if (error_str != NULL) {
6649 			snprintf(error_str, error_str_len, "%s: no Transport "
6650 				 "ID format exists for protocol %s",
6651 				 __func__, tmpstr);
6652 		}
6653 		goto bailout;
6654 		break;	/* NOTREACHED */
6655 	}
6656 bailout:
6657 	return (retval);
6658 }
6659 
6660 struct scsi_attrib_table_entry scsi_mam_attr_table[] = {
6661 	{ SMA_ATTR_REM_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6662 	  "Remaining Capacity in Partition",
6663 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,/*parse_str*/ NULL },
6664 	{ SMA_ATTR_MAX_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6665 	  "Maximum Capacity in Partition",
6666 	  /*suffix*/"MB", /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6667 	{ SMA_ATTR_TAPEALERT_FLAGS, SCSI_ATTR_FLAG_HEX,
6668 	  "TapeAlert Flags",
6669 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6670 	{ SMA_ATTR_LOAD_COUNT, SCSI_ATTR_FLAG_NONE,
6671 	  "Load Count",
6672 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6673 	{ SMA_ATTR_MAM_SPACE_REMAINING, SCSI_ATTR_FLAG_NONE,
6674 	  "MAM Space Remaining",
6675 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6676 	  /*parse_str*/ NULL },
6677 	{ SMA_ATTR_DEV_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6678 	  "Assigning Organization",
6679 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6680 	  /*parse_str*/ NULL },
6681 	{ SMA_ATTR_FORMAT_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6682 	  "Format Density Code",
6683 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6684 	{ SMA_ATTR_INITIALIZATION_COUNT, SCSI_ATTR_FLAG_NONE,
6685 	  "Initialization Count",
6686 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6687 	{ SMA_ATTR_VOLUME_ID, SCSI_ATTR_FLAG_NONE,
6688 	  "Volume Identifier",
6689 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6690 	  /*parse_str*/ NULL },
6691 	{ SMA_ATTR_VOLUME_CHANGE_REF, SCSI_ATTR_FLAG_HEX,
6692 	  "Volume Change Reference",
6693 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6694 	  /*parse_str*/ NULL },
6695 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD, SCSI_ATTR_FLAG_NONE,
6696 	  "Device Vendor/Serial at Last Load",
6697 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6698 	  /*parse_str*/ NULL },
6699 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_1, SCSI_ATTR_FLAG_NONE,
6700 	  "Device Vendor/Serial at Last Load - 1",
6701 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6702 	  /*parse_str*/ NULL },
6703 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_2, SCSI_ATTR_FLAG_NONE,
6704 	  "Device Vendor/Serial at Last Load - 2",
6705 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6706 	  /*parse_str*/ NULL },
6707 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_3, SCSI_ATTR_FLAG_NONE,
6708 	  "Device Vendor/Serial at Last Load - 3",
6709 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6710 	  /*parse_str*/ NULL },
6711 	{ SMA_ATTR_TOTAL_MB_WRITTEN_LT, SCSI_ATTR_FLAG_NONE,
6712 	  "Total MB Written in Medium Life",
6713 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6714 	  /*parse_str*/ NULL },
6715 	{ SMA_ATTR_TOTAL_MB_READ_LT, SCSI_ATTR_FLAG_NONE,
6716 	  "Total MB Read in Medium Life",
6717 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6718 	  /*parse_str*/ NULL },
6719 	{ SMA_ATTR_TOTAL_MB_WRITTEN_CUR, SCSI_ATTR_FLAG_NONE,
6720 	  "Total MB Written in Current/Last Load",
6721 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6722 	  /*parse_str*/ NULL },
6723 	{ SMA_ATTR_TOTAL_MB_READ_CUR, SCSI_ATTR_FLAG_NONE,
6724 	  "Total MB Read in Current/Last Load",
6725 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6726 	  /*parse_str*/ NULL },
6727 	{ SMA_ATTR_FIRST_ENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6728 	  "Logical Position of First Encrypted Block",
6729 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6730 	  /*parse_str*/ NULL },
6731 	{ SMA_ATTR_NEXT_UNENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6732 	  "Logical Position of First Unencrypted Block after First "
6733 	  "Encrypted Block",
6734 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6735 	  /*parse_str*/ NULL },
6736 	{ SMA_ATTR_MEDIUM_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6737 	  "Medium Usage History",
6738 	  /*suffix*/ NULL, /*to_str*/ NULL,
6739 	  /*parse_str*/ NULL },
6740 	{ SMA_ATTR_PART_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6741 	  "Partition Usage History",
6742 	  /*suffix*/ NULL, /*to_str*/ NULL,
6743 	  /*parse_str*/ NULL },
6744 	{ SMA_ATTR_MED_MANUF, SCSI_ATTR_FLAG_NONE,
6745 	  "Medium Manufacturer",
6746 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6747 	  /*parse_str*/ NULL },
6748 	{ SMA_ATTR_MED_SERIAL, SCSI_ATTR_FLAG_NONE,
6749 	  "Medium Serial Number",
6750 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6751 	  /*parse_str*/ NULL },
6752 	{ SMA_ATTR_MED_LENGTH, SCSI_ATTR_FLAG_NONE,
6753 	  "Medium Length",
6754 	  /*suffix*/"m", /*to_str*/ scsi_attrib_int_sbuf,
6755 	  /*parse_str*/ NULL },
6756 	{ SMA_ATTR_MED_WIDTH, SCSI_ATTR_FLAG_FP | SCSI_ATTR_FLAG_DIV_10 |
6757 	  SCSI_ATTR_FLAG_FP_1DIGIT,
6758 	  "Medium Width",
6759 	  /*suffix*/"mm", /*to_str*/ scsi_attrib_int_sbuf,
6760 	  /*parse_str*/ NULL },
6761 	{ SMA_ATTR_MED_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6762 	  "Assigning Organization",
6763 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6764 	  /*parse_str*/ NULL },
6765 	{ SMA_ATTR_MED_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6766 	  "Medium Density Code",
6767 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6768 	  /*parse_str*/ NULL },
6769 	{ SMA_ATTR_MED_MANUF_DATE, SCSI_ATTR_FLAG_NONE,
6770 	  "Medium Manufacture Date",
6771 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6772 	  /*parse_str*/ NULL },
6773 	{ SMA_ATTR_MAM_CAPACITY, SCSI_ATTR_FLAG_NONE,
6774 	  "MAM Capacity",
6775 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6776 	  /*parse_str*/ NULL },
6777 	{ SMA_ATTR_MED_TYPE, SCSI_ATTR_FLAG_HEX,
6778 	  "Medium Type",
6779 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6780 	  /*parse_str*/ NULL },
6781 	{ SMA_ATTR_MED_TYPE_INFO, SCSI_ATTR_FLAG_HEX,
6782 	  "Medium Type Information",
6783 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6784 	  /*parse_str*/ NULL },
6785 	{ SMA_ATTR_MED_SERIAL_NUM, SCSI_ATTR_FLAG_NONE,
6786 	  "Medium Serial Number",
6787 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6788 	  /*parse_str*/ NULL },
6789 	{ SMA_ATTR_APP_VENDOR, SCSI_ATTR_FLAG_NONE,
6790 	  "Application Vendor",
6791 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6792 	  /*parse_str*/ NULL },
6793 	{ SMA_ATTR_APP_NAME, SCSI_ATTR_FLAG_NONE,
6794 	  "Application Name",
6795 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6796 	  /*parse_str*/ NULL },
6797 	{ SMA_ATTR_APP_VERSION, SCSI_ATTR_FLAG_NONE,
6798 	  "Application Version",
6799 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6800 	  /*parse_str*/ NULL },
6801 	{ SMA_ATTR_USER_MED_TEXT_LABEL, SCSI_ATTR_FLAG_NONE,
6802 	  "User Medium Text Label",
6803 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6804 	  /*parse_str*/ NULL },
6805 	{ SMA_ATTR_LAST_WRITTEN_TIME, SCSI_ATTR_FLAG_NONE,
6806 	  "Date and Time Last Written",
6807 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6808 	  /*parse_str*/ NULL },
6809 	{ SMA_ATTR_TEXT_LOCAL_ID, SCSI_ATTR_FLAG_HEX,
6810 	  "Text Localization Identifier",
6811 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6812 	  /*parse_str*/ NULL },
6813 	{ SMA_ATTR_BARCODE, SCSI_ATTR_FLAG_NONE,
6814 	  "Barcode",
6815 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6816 	  /*parse_str*/ NULL },
6817 	{ SMA_ATTR_HOST_OWNER_NAME, SCSI_ATTR_FLAG_NONE,
6818 	  "Owning Host Textual Name",
6819 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6820 	  /*parse_str*/ NULL },
6821 	{ SMA_ATTR_MEDIA_POOL, SCSI_ATTR_FLAG_NONE,
6822 	  "Media Pool",
6823 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6824 	  /*parse_str*/ NULL },
6825 	{ SMA_ATTR_PART_USER_LABEL, SCSI_ATTR_FLAG_NONE,
6826 	  "Partition User Text Label",
6827 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6828 	  /*parse_str*/ NULL },
6829 	{ SMA_ATTR_LOAD_UNLOAD_AT_PART, SCSI_ATTR_FLAG_NONE,
6830 	  "Load/Unload at Partition",
6831 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6832 	  /*parse_str*/ NULL },
6833 	{ SMA_ATTR_APP_FORMAT_VERSION, SCSI_ATTR_FLAG_NONE,
6834 	  "Application Format Version",
6835 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6836 	  /*parse_str*/ NULL },
6837 	{ SMA_ATTR_VOL_COHERENCY_INFO, SCSI_ATTR_FLAG_NONE,
6838 	  "Volume Coherency Information",
6839 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_volcoh_sbuf,
6840 	  /*parse_str*/ NULL },
6841 	{ 0x0ff1, SCSI_ATTR_FLAG_NONE,
6842 	  "Spectra MLM Creation",
6843 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6844 	  /*parse_str*/ NULL },
6845 	{ 0x0ff2, SCSI_ATTR_FLAG_NONE,
6846 	  "Spectra MLM C3",
6847 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6848 	  /*parse_str*/ NULL },
6849 	{ 0x0ff3, SCSI_ATTR_FLAG_NONE,
6850 	  "Spectra MLM RW",
6851 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6852 	  /*parse_str*/ NULL },
6853 	{ 0x0ff4, SCSI_ATTR_FLAG_NONE,
6854 	  "Spectra MLM SDC List",
6855 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6856 	  /*parse_str*/ NULL },
6857 	{ 0x0ff7, SCSI_ATTR_FLAG_NONE,
6858 	  "Spectra MLM Post Scan",
6859 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6860 	  /*parse_str*/ NULL },
6861 	{ 0x0ffe, SCSI_ATTR_FLAG_NONE,
6862 	  "Spectra MLM Checksum",
6863 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6864 	  /*parse_str*/ NULL },
6865 	{ 0x17f1, SCSI_ATTR_FLAG_NONE,
6866 	  "Spectra MLM Creation",
6867 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6868 	  /*parse_str*/ NULL },
6869 	{ 0x17f2, SCSI_ATTR_FLAG_NONE,
6870 	  "Spectra MLM C3",
6871 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6872 	  /*parse_str*/ NULL },
6873 	{ 0x17f3, SCSI_ATTR_FLAG_NONE,
6874 	  "Spectra MLM RW",
6875 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6876 	  /*parse_str*/ NULL },
6877 	{ 0x17f4, SCSI_ATTR_FLAG_NONE,
6878 	  "Spectra MLM SDC List",
6879 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6880 	  /*parse_str*/ NULL },
6881 	{ 0x17f7, SCSI_ATTR_FLAG_NONE,
6882 	  "Spectra MLM Post Scan",
6883 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6884 	  /*parse_str*/ NULL },
6885 	{ 0x17ff, SCSI_ATTR_FLAG_NONE,
6886 	  "Spectra MLM Checksum",
6887 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6888 	  /*parse_str*/ NULL },
6889 };
6890 
6891 /*
6892  * Print out Volume Coherency Information (Attribute 0x080c).
6893  * This field has two variable length members, including one at the
6894  * beginning, so it isn't practical to have a fixed structure definition.
6895  * This is current as of SSC4r03 (see section 4.2.21.3), dated March 25,
6896  * 2013.
6897  */
6898 int
6899 scsi_attrib_volcoh_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
6900 			 uint32_t valid_len, uint32_t flags,
6901 			 uint32_t output_flags, char *error_str,
6902 			 int error_str_len)
6903 {
6904 	size_t avail_len;
6905 	uint32_t field_size;
6906 	uint64_t tmp_val;
6907 	uint8_t *cur_ptr;
6908 	int retval;
6909 	int vcr_len, as_len;
6910 
6911 	retval = 0;
6912 	tmp_val = 0;
6913 
6914 	field_size = scsi_2btoul(hdr->length);
6915 	avail_len = valid_len - sizeof(*hdr);
6916 	if (field_size > avail_len) {
6917 		if (error_str != NULL) {
6918 			snprintf(error_str, error_str_len, "Available "
6919 				 "length of attribute ID 0x%.4x %zu < field "
6920 				 "length %u", scsi_2btoul(hdr->id), avail_len,
6921 				 field_size);
6922 		}
6923 		retval = 1;
6924 		goto bailout;
6925 	} else if (field_size == 0) {
6926 		/*
6927 		 * It isn't clear from the spec whether a field length of
6928 		 * 0 is invalid here.  It probably is, but be lenient here
6929 		 * to avoid inconveniencing the user.
6930 		 */
6931 		goto bailout;
6932 	}
6933 	cur_ptr = hdr->attribute;
6934 	vcr_len = *cur_ptr;
6935 	cur_ptr++;
6936 
6937 	sbuf_printf(sb, "\n\tVolume Change Reference Value:");
6938 
6939 	switch (vcr_len) {
6940 	case 0:
6941 		if (error_str != NULL) {
6942 			snprintf(error_str, error_str_len, "Volume Change "
6943 				 "Reference value has length of 0");
6944 		}
6945 		retval = 1;
6946 		goto bailout;
6947 		break; /*NOTREACHED*/
6948 	case 1:
6949 		tmp_val = *cur_ptr;
6950 		break;
6951 	case 2:
6952 		tmp_val = scsi_2btoul(cur_ptr);
6953 		break;
6954 	case 3:
6955 		tmp_val = scsi_3btoul(cur_ptr);
6956 		break;
6957 	case 4:
6958 		tmp_val = scsi_4btoul(cur_ptr);
6959 		break;
6960 	case 8:
6961 		tmp_val = scsi_8btou64(cur_ptr);
6962 		break;
6963 	default:
6964 		sbuf_printf(sb, "\n");
6965 		sbuf_hexdump(sb, cur_ptr, vcr_len, NULL, 0);
6966 		break;
6967 	}
6968 	if (vcr_len <= 8)
6969 		sbuf_printf(sb, " 0x%jx\n", (uintmax_t)tmp_val);
6970 
6971 	cur_ptr += vcr_len;
6972 	tmp_val = scsi_8btou64(cur_ptr);
6973 	sbuf_printf(sb, "\tVolume Coherency Count: %ju\n", (uintmax_t)tmp_val);
6974 
6975 	cur_ptr += sizeof(tmp_val);
6976 	tmp_val = scsi_8btou64(cur_ptr);
6977 	sbuf_printf(sb, "\tVolume Coherency Set Identifier: 0x%jx\n",
6978 		    (uintmax_t)tmp_val);
6979 
6980 	/*
6981 	 * Figure out how long the Application Client Specific Information
6982 	 * is and produce a hexdump.
6983 	 */
6984 	cur_ptr += sizeof(tmp_val);
6985 	as_len = scsi_2btoul(cur_ptr);
6986 	cur_ptr += sizeof(uint16_t);
6987 	sbuf_printf(sb, "\tApplication Client Specific Information: ");
6988 	if (((as_len == SCSI_LTFS_VER0_LEN)
6989 	  || (as_len == SCSI_LTFS_VER1_LEN))
6990 	 && (strncmp(cur_ptr, SCSI_LTFS_STR_NAME, SCSI_LTFS_STR_LEN) == 0)) {
6991 		sbuf_printf(sb, "LTFS\n");
6992 		cur_ptr += SCSI_LTFS_STR_LEN + 1;
6993 		if (cur_ptr[SCSI_LTFS_UUID_LEN] != '\0')
6994 			cur_ptr[SCSI_LTFS_UUID_LEN] = '\0';
6995 		sbuf_printf(sb, "\tLTFS UUID: %s\n", cur_ptr);
6996 		cur_ptr += SCSI_LTFS_UUID_LEN + 1;
6997 		/* XXX KDM check the length */
6998 		sbuf_printf(sb, "\tLTFS Version: %d\n", *cur_ptr);
6999 	} else {
7000 		sbuf_printf(sb, "Unknown\n");
7001 		sbuf_hexdump(sb, cur_ptr, as_len, NULL, 0);
7002 	}
7003 
7004 bailout:
7005 	return (retval);
7006 }
7007 
7008 int
7009 scsi_attrib_vendser_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7010 			 uint32_t valid_len, uint32_t flags,
7011 			 uint32_t output_flags, char *error_str,
7012 			 int error_str_len)
7013 {
7014 	size_t avail_len;
7015 	uint32_t field_size;
7016 	struct scsi_attrib_vendser *vendser;
7017 	cam_strvis_flags strvis_flags;
7018 	int retval = 0;
7019 
7020 	field_size = scsi_2btoul(hdr->length);
7021 	avail_len = valid_len - sizeof(*hdr);
7022 	if (field_size > avail_len) {
7023 		if (error_str != NULL) {
7024 			snprintf(error_str, error_str_len, "Available "
7025 				 "length of attribute ID 0x%.4x %zu < field "
7026 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7027 				 field_size);
7028 		}
7029 		retval = 1;
7030 		goto bailout;
7031 	} else if (field_size == 0) {
7032 		/*
7033 		 * A field size of 0 doesn't make sense here.  The device
7034 		 * can at least give you the vendor ID, even if it can't
7035 		 * give you the serial number.
7036 		 */
7037 		if (error_str != NULL) {
7038 			snprintf(error_str, error_str_len, "The length of "
7039 				 "attribute ID 0x%.4x is 0",
7040 				 scsi_2btoul(hdr->id));
7041 		}
7042 		retval = 1;
7043 		goto bailout;
7044 	}
7045 	vendser = (struct scsi_attrib_vendser *)hdr->attribute;
7046 
7047 	switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7048 	case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7049 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7050 		break;
7051 	case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7052 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7053 		break;
7054 	case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7055 	default:
7056 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7057 		break;;
7058 	}
7059 	cam_strvis_sbuf(sb, vendser->vendor, sizeof(vendser->vendor),
7060 	    strvis_flags);
7061 	sbuf_putc(sb, ' ');
7062 	cam_strvis_sbuf(sb, vendser->serial_num, sizeof(vendser->serial_num),
7063 	    strvis_flags);
7064 bailout:
7065 	return (retval);
7066 }
7067 
7068 int
7069 scsi_attrib_hexdump_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7070 			 uint32_t valid_len, uint32_t flags,
7071 			 uint32_t output_flags, char *error_str,
7072 			 int error_str_len)
7073 {
7074 	uint32_t field_size;
7075 	ssize_t avail_len;
7076 	uint32_t print_len;
7077 	uint8_t *num_ptr;
7078 	int retval = 0;
7079 
7080 	field_size = scsi_2btoul(hdr->length);
7081 	avail_len = valid_len - sizeof(*hdr);
7082 	print_len = MIN(avail_len, field_size);
7083 	num_ptr = hdr->attribute;
7084 
7085 	if (print_len > 0) {
7086 		sbuf_printf(sb, "\n");
7087 		sbuf_hexdump(sb, num_ptr, print_len, NULL, 0);
7088 	}
7089 
7090 	return (retval);
7091 }
7092 
7093 int
7094 scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7095 		     uint32_t valid_len, uint32_t flags,
7096 		     uint32_t output_flags, char *error_str,
7097 		     int error_str_len)
7098 {
7099 	uint64_t print_number;
7100 	size_t avail_len;
7101 	uint32_t number_size;
7102 	int retval = 0;
7103 
7104 	number_size = scsi_2btoul(hdr->length);
7105 
7106 	avail_len = valid_len - sizeof(*hdr);
7107 	if (avail_len < number_size) {
7108 		if (error_str != NULL) {
7109 			snprintf(error_str, error_str_len, "Available "
7110 				 "length of attribute ID 0x%.4x %zu < field "
7111 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7112 				 number_size);
7113 		}
7114 		retval = 1;
7115 		goto bailout;
7116 	}
7117 
7118 	switch (number_size) {
7119 	case 0:
7120 		/*
7121 		 * We don't treat this as an error, since there may be
7122 		 * scenarios where a device reports a field but then gives
7123 		 * a length of 0.  See the note in scsi_attrib_ascii_sbuf().
7124 		 */
7125 		goto bailout;
7126 		break; /*NOTREACHED*/
7127 	case 1:
7128 		print_number = hdr->attribute[0];
7129 		break;
7130 	case 2:
7131 		print_number = scsi_2btoul(hdr->attribute);
7132 		break;
7133 	case 3:
7134 		print_number = scsi_3btoul(hdr->attribute);
7135 		break;
7136 	case 4:
7137 		print_number = scsi_4btoul(hdr->attribute);
7138 		break;
7139 	case 8:
7140 		print_number = scsi_8btou64(hdr->attribute);
7141 		break;
7142 	default:
7143 		/*
7144 		 * If we wind up here, the number is too big to print
7145 		 * normally, so just do a hexdump.
7146 		 */
7147 		retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7148 						  flags, output_flags,
7149 						  error_str, error_str_len);
7150 		goto bailout;
7151 		break;
7152 	}
7153 
7154 	if (flags & SCSI_ATTR_FLAG_FP) {
7155 #ifndef _KERNEL
7156 		long double num_float;
7157 
7158 		num_float = (long double)print_number;
7159 
7160 		if (flags & SCSI_ATTR_FLAG_DIV_10)
7161 			num_float /= 10;
7162 
7163 		sbuf_printf(sb, "%.*Lf", (flags & SCSI_ATTR_FLAG_FP_1DIGIT) ?
7164 			    1 : 0, num_float);
7165 #else /* _KERNEL */
7166 		sbuf_printf(sb, "%ju", (flags & SCSI_ATTR_FLAG_DIV_10) ?
7167 			    (print_number / 10) : print_number);
7168 #endif /* _KERNEL */
7169 	} else if (flags & SCSI_ATTR_FLAG_HEX) {
7170 		sbuf_printf(sb, "0x%jx", (uintmax_t)print_number);
7171 	} else
7172 		sbuf_printf(sb, "%ju", (uintmax_t)print_number);
7173 
7174 bailout:
7175 	return (retval);
7176 }
7177 
7178 int
7179 scsi_attrib_ascii_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7180 		       uint32_t valid_len, uint32_t flags,
7181 		       uint32_t output_flags, char *error_str,
7182 		       int error_str_len)
7183 {
7184 	size_t avail_len;
7185 	uint32_t field_size, print_size;
7186 	int retval = 0;
7187 
7188 	avail_len = valid_len - sizeof(*hdr);
7189 	field_size = scsi_2btoul(hdr->length);
7190 	print_size = MIN(avail_len, field_size);
7191 
7192 	if (print_size > 0) {
7193 		cam_strvis_flags strvis_flags;
7194 
7195 		switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7196 		case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7197 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7198 			break;
7199 		case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7200 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7201 			break;
7202 		case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7203 		default:
7204 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7205 			break;
7206 		}
7207 		cam_strvis_sbuf(sb, hdr->attribute, print_size, strvis_flags);
7208 	} else if (avail_len < field_size) {
7209 		/*
7210 		 * We only report an error if the user didn't allocate
7211 		 * enough space to hold the full value of this field.  If
7212 		 * the field length is 0, that is allowed by the spec.
7213 		 * e.g. in SPC-4r37, section 7.4.2.2.5, VOLUME IDENTIFIER
7214 		 * "This attribute indicates the current volume identifier
7215 		 * (see SMC-3) of the medium. If the device server supports
7216 		 * this attribute but does not have access to the volume
7217 		 * identifier, the device server shall report this attribute
7218 		 * with an attribute length value of zero."
7219 		 */
7220 		if (error_str != NULL) {
7221 			snprintf(error_str, error_str_len, "Available "
7222 				 "length of attribute ID 0x%.4x %zu < field "
7223 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7224 				 field_size);
7225 		}
7226 		retval = 1;
7227 	}
7228 
7229 	return (retval);
7230 }
7231 
7232 int
7233 scsi_attrib_text_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7234 		      uint32_t valid_len, uint32_t flags,
7235 		      uint32_t output_flags, char *error_str,
7236 		      int error_str_len)
7237 {
7238 	size_t avail_len;
7239 	uint32_t field_size, print_size;
7240 	int retval = 0;
7241 	int esc_text = 1;
7242 
7243 	avail_len = valid_len - sizeof(*hdr);
7244 	field_size = scsi_2btoul(hdr->length);
7245 	print_size = MIN(avail_len, field_size);
7246 
7247 	if ((output_flags & SCSI_ATTR_OUTPUT_TEXT_MASK) ==
7248 	     SCSI_ATTR_OUTPUT_TEXT_RAW)
7249 		esc_text = 0;
7250 
7251 	if (print_size > 0) {
7252 		uint32_t i;
7253 
7254 		for (i = 0; i < print_size; i++) {
7255 			if (hdr->attribute[i] == '\0')
7256 				continue;
7257 			else if (((unsigned char)hdr->attribute[i] < 0x80)
7258 			      || (esc_text == 0))
7259 				sbuf_putc(sb, hdr->attribute[i]);
7260 			else
7261 				sbuf_printf(sb, "%%%02x",
7262 				    (unsigned char)hdr->attribute[i]);
7263 		}
7264 	} else if (avail_len < field_size) {
7265 		/*
7266 		 * We only report an error if the user didn't allocate
7267 		 * enough space to hold the full value of this field.
7268 		 */
7269 		if (error_str != NULL) {
7270 			snprintf(error_str, error_str_len, "Available "
7271 				 "length of attribute ID 0x%.4x %zu < field "
7272 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7273 				 field_size);
7274 		}
7275 		retval = 1;
7276 	}
7277 
7278 	return (retval);
7279 }
7280 
7281 struct scsi_attrib_table_entry *
7282 scsi_find_attrib_entry(struct scsi_attrib_table_entry *table,
7283 		       size_t num_table_entries, uint32_t id)
7284 {
7285 	uint32_t i;
7286 
7287 	for (i = 0; i < num_table_entries; i++) {
7288 		if (table[i].id == id)
7289 			return (&table[i]);
7290 	}
7291 
7292 	return (NULL);
7293 }
7294 
7295 struct scsi_attrib_table_entry *
7296 scsi_get_attrib_entry(uint32_t id)
7297 {
7298 	return (scsi_find_attrib_entry(scsi_mam_attr_table,
7299 	    nitems(scsi_mam_attr_table), id));
7300 }
7301 
7302 int
7303 scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len,
7304    struct scsi_mam_attribute_header *hdr, uint32_t output_flags,
7305    char *error_str, size_t error_str_len)
7306 {
7307 	int retval;
7308 
7309 	switch (hdr->byte2 & SMA_FORMAT_MASK) {
7310 	case SMA_FORMAT_ASCII:
7311 		retval = scsi_attrib_ascii_sbuf(sb, hdr, valid_len,
7312 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,error_str_len);
7313 		break;
7314 	case SMA_FORMAT_BINARY:
7315 		if (scsi_2btoul(hdr->length) <= 8)
7316 			retval = scsi_attrib_int_sbuf(sb, hdr, valid_len,
7317 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7318 			    error_str_len);
7319 		else
7320 			retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7321 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7322 			    error_str_len);
7323 		break;
7324 	case SMA_FORMAT_TEXT:
7325 		retval = scsi_attrib_text_sbuf(sb, hdr, valid_len,
7326 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7327 		    error_str_len);
7328 		break;
7329 	default:
7330 		if (error_str != NULL) {
7331 			snprintf(error_str, error_str_len, "Unknown attribute "
7332 			    "format 0x%x", hdr->byte2 & SMA_FORMAT_MASK);
7333 		}
7334 		retval = 1;
7335 		goto bailout;
7336 		break; /*NOTREACHED*/
7337 	}
7338 
7339 	sbuf_trim(sb);
7340 
7341 bailout:
7342 
7343 	return (retval);
7344 }
7345 
7346 void
7347 scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags,
7348 			struct scsi_mam_attribute_header *hdr,
7349 			uint32_t valid_len, const char *desc)
7350 {
7351 	int need_space = 0;
7352 	uint32_t len;
7353 	uint32_t id;
7354 
7355 	/*
7356 	 * We can't do anything if we don't have enough valid data for the
7357 	 * header.
7358 	 */
7359 	if (valid_len < sizeof(*hdr))
7360 		return;
7361 
7362 	id = scsi_2btoul(hdr->id);
7363 	/*
7364 	 * Note that we print out the value of the attribute listed in the
7365 	 * header, regardless of whether we actually got that many bytes
7366 	 * back from the device through the controller.  A truncated result
7367 	 * could be the result of a failure to ask for enough data; the
7368 	 * header indicates how many bytes are allocated for this attribute
7369 	 * in the MAM.
7370 	 */
7371 	len = scsi_2btoul(hdr->length);
7372 
7373 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_MASK) ==
7374 	    SCSI_ATTR_OUTPUT_FIELD_NONE)
7375 		return;
7376 
7377 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_DESC)
7378 	 && (desc != NULL)) {
7379 		sbuf_printf(sb, "%s", desc);
7380 		need_space = 1;
7381 	}
7382 
7383 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_NUM) {
7384 		sbuf_printf(sb, "%s(0x%.4x)", (need_space) ? " " : "", id);
7385 		need_space = 0;
7386 	}
7387 
7388 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_SIZE) {
7389 		sbuf_printf(sb, "%s[%d]", (need_space) ? " " : "", len);
7390 		need_space = 0;
7391 	}
7392 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_RW) {
7393 		sbuf_printf(sb, "%s(%s)", (need_space) ? " " : "",
7394 			    (hdr->byte2 & SMA_READ_ONLY) ? "RO" : "RW");
7395 	}
7396 	sbuf_printf(sb, ": ");
7397 }
7398 
7399 int
7400 scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7401 		 uint32_t valid_len, struct scsi_attrib_table_entry *user_table,
7402 		 size_t num_user_entries, int prefer_user_table,
7403 		 uint32_t output_flags, char *error_str, int error_str_len)
7404 {
7405 	int retval;
7406 	struct scsi_attrib_table_entry *table1 = NULL, *table2 = NULL;
7407 	struct scsi_attrib_table_entry *entry = NULL;
7408 	size_t table1_size = 0, table2_size = 0;
7409 	uint32_t id;
7410 
7411 	retval = 0;
7412 
7413 	if (valid_len < sizeof(*hdr)) {
7414 		retval = 1;
7415 		goto bailout;
7416 	}
7417 
7418 	id = scsi_2btoul(hdr->id);
7419 
7420 	if (user_table != NULL) {
7421 		if (prefer_user_table != 0) {
7422 			table1 = user_table;
7423 			table1_size = num_user_entries;
7424 			table2 = scsi_mam_attr_table;
7425 			table2_size = nitems(scsi_mam_attr_table);
7426 		} else {
7427 			table1 = scsi_mam_attr_table;
7428 			table1_size = nitems(scsi_mam_attr_table);
7429 			table2 = user_table;
7430 			table2_size = num_user_entries;
7431 		}
7432 	} else {
7433 		table1 = scsi_mam_attr_table;
7434 		table1_size = nitems(scsi_mam_attr_table);
7435 	}
7436 
7437 	entry = scsi_find_attrib_entry(table1, table1_size, id);
7438 	if (entry != NULL) {
7439 		scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len,
7440 					entry->desc);
7441 		if (entry->to_str == NULL)
7442 			goto print_default;
7443 		retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7444 				       output_flags, error_str, error_str_len);
7445 		goto bailout;
7446 	}
7447 	if (table2 != NULL) {
7448 		entry = scsi_find_attrib_entry(table2, table2_size, id);
7449 		if (entry != NULL) {
7450 			if (entry->to_str == NULL)
7451 				goto print_default;
7452 
7453 			scsi_attrib_prefix_sbuf(sb, output_flags, hdr,
7454 						valid_len, entry->desc);
7455 			retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7456 					       output_flags, error_str,
7457 					       error_str_len);
7458 			goto bailout;
7459 		}
7460 	}
7461 
7462 	scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, NULL);
7463 
7464 print_default:
7465 	retval = scsi_attrib_value_sbuf(sb, valid_len, hdr, output_flags,
7466 	    error_str, error_str_len);
7467 bailout:
7468 	if (retval == 0) {
7469 	 	if ((entry != NULL)
7470 		 && (entry->suffix != NULL))
7471 			sbuf_printf(sb, " %s", entry->suffix);
7472 
7473 		sbuf_trim(sb);
7474 		sbuf_printf(sb, "\n");
7475 	}
7476 
7477 	return (retval);
7478 }
7479 
7480 void
7481 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
7482 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
7483 		     u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
7484 {
7485 	struct scsi_test_unit_ready *scsi_cmd;
7486 
7487 	cam_fill_csio(csio,
7488 		      retries,
7489 		      cbfcnp,
7490 		      CAM_DIR_NONE,
7491 		      tag_action,
7492 		      /*data_ptr*/NULL,
7493 		      /*dxfer_len*/0,
7494 		      sense_len,
7495 		      sizeof(*scsi_cmd),
7496 		      timeout);
7497 
7498 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
7499 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7500 	scsi_cmd->opcode = TEST_UNIT_READY;
7501 }
7502 
7503 void
7504 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
7505 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7506 		   void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
7507 		   u_int8_t sense_len, u_int32_t timeout)
7508 {
7509 	struct scsi_request_sense *scsi_cmd;
7510 
7511 	cam_fill_csio(csio,
7512 		      retries,
7513 		      cbfcnp,
7514 		      CAM_DIR_IN,
7515 		      tag_action,
7516 		      data_ptr,
7517 		      dxfer_len,
7518 		      sense_len,
7519 		      sizeof(*scsi_cmd),
7520 		      timeout);
7521 
7522 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
7523 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7524 	scsi_cmd->opcode = REQUEST_SENSE;
7525 	scsi_cmd->length = dxfer_len;
7526 }
7527 
7528 void
7529 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
7530 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7531 	     u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
7532 	     int evpd, u_int8_t page_code, u_int8_t sense_len,
7533 	     u_int32_t timeout)
7534 {
7535 	struct scsi_inquiry *scsi_cmd;
7536 
7537 	cam_fill_csio(csio,
7538 		      retries,
7539 		      cbfcnp,
7540 		      /*flags*/CAM_DIR_IN,
7541 		      tag_action,
7542 		      /*data_ptr*/inq_buf,
7543 		      /*dxfer_len*/inq_len,
7544 		      sense_len,
7545 		      sizeof(*scsi_cmd),
7546 		      timeout);
7547 
7548 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
7549 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7550 	scsi_cmd->opcode = INQUIRY;
7551 	if (evpd) {
7552 		scsi_cmd->byte2 |= SI_EVPD;
7553 		scsi_cmd->page_code = page_code;
7554 	}
7555 	scsi_ulto2b(inq_len, scsi_cmd->length);
7556 }
7557 
7558 void
7559 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
7560 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7561 		u_int8_t tag_action, int dbd, u_int8_t page_code,
7562 		u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7563 		u_int8_t sense_len, u_int32_t timeout)
7564 {
7565 
7566 	scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
7567 			    page_code, page, param_buf, param_len, 0,
7568 			    sense_len, timeout);
7569 }
7570 
7571 void
7572 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
7573 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
7574 		    u_int8_t tag_action, int dbd, u_int8_t page_code,
7575 		    u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7576 		    int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
7577 {
7578 	u_int8_t cdb_len;
7579 
7580 	/*
7581 	 * Use the smallest possible command to perform the operation.
7582 	 */
7583 	if ((param_len < 256)
7584 	 && (minimum_cmd_size < 10)) {
7585 		/*
7586 		 * We can fit in a 6 byte cdb.
7587 		 */
7588 		struct scsi_mode_sense_6 *scsi_cmd;
7589 
7590 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
7591 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7592 		scsi_cmd->opcode = MODE_SENSE_6;
7593 		if (dbd != 0)
7594 			scsi_cmd->byte2 |= SMS_DBD;
7595 		scsi_cmd->page = page_code | page;
7596 		scsi_cmd->length = param_len;
7597 		cdb_len = sizeof(*scsi_cmd);
7598 	} else {
7599 		/*
7600 		 * Need a 10 byte cdb.
7601 		 */
7602 		struct scsi_mode_sense_10 *scsi_cmd;
7603 
7604 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
7605 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7606 		scsi_cmd->opcode = MODE_SENSE_10;
7607 		if (dbd != 0)
7608 			scsi_cmd->byte2 |= SMS_DBD;
7609 		scsi_cmd->page = page_code | page;
7610 		scsi_ulto2b(param_len, scsi_cmd->length);
7611 		cdb_len = sizeof(*scsi_cmd);
7612 	}
7613 	cam_fill_csio(csio,
7614 		      retries,
7615 		      cbfcnp,
7616 		      CAM_DIR_IN,
7617 		      tag_action,
7618 		      param_buf,
7619 		      param_len,
7620 		      sense_len,
7621 		      cdb_len,
7622 		      timeout);
7623 }
7624 
7625 void
7626 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
7627 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7628 		 u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7629 		 u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7630 		 u_int32_t timeout)
7631 {
7632 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
7633 			     scsi_page_fmt, save_pages, param_buf,
7634 			     param_len, 0, sense_len, timeout);
7635 }
7636 
7637 void
7638 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
7639 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
7640 		     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7641 		     u_int8_t *param_buf, u_int32_t param_len,
7642 		     int minimum_cmd_size, u_int8_t sense_len,
7643 		     u_int32_t timeout)
7644 {
7645 	u_int8_t cdb_len;
7646 
7647 	/*
7648 	 * Use the smallest possible command to perform the operation.
7649 	 */
7650 	if ((param_len < 256)
7651 	 && (minimum_cmd_size < 10)) {
7652 		/*
7653 		 * We can fit in a 6 byte cdb.
7654 		 */
7655 		struct scsi_mode_select_6 *scsi_cmd;
7656 
7657 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
7658 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7659 		scsi_cmd->opcode = MODE_SELECT_6;
7660 		if (scsi_page_fmt != 0)
7661 			scsi_cmd->byte2 |= SMS_PF;
7662 		if (save_pages != 0)
7663 			scsi_cmd->byte2 |= SMS_SP;
7664 		scsi_cmd->length = param_len;
7665 		cdb_len = sizeof(*scsi_cmd);
7666 	} else {
7667 		/*
7668 		 * Need a 10 byte cdb.
7669 		 */
7670 		struct scsi_mode_select_10 *scsi_cmd;
7671 
7672 		scsi_cmd =
7673 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
7674 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7675 		scsi_cmd->opcode = MODE_SELECT_10;
7676 		if (scsi_page_fmt != 0)
7677 			scsi_cmd->byte2 |= SMS_PF;
7678 		if (save_pages != 0)
7679 			scsi_cmd->byte2 |= SMS_SP;
7680 		scsi_ulto2b(param_len, scsi_cmd->length);
7681 		cdb_len = sizeof(*scsi_cmd);
7682 	}
7683 	cam_fill_csio(csio,
7684 		      retries,
7685 		      cbfcnp,
7686 		      CAM_DIR_OUT,
7687 		      tag_action,
7688 		      param_buf,
7689 		      param_len,
7690 		      sense_len,
7691 		      cdb_len,
7692 		      timeout);
7693 }
7694 
7695 void
7696 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
7697 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
7698 	       u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
7699 	       int save_pages, int ppc, u_int32_t paramptr,
7700 	       u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7701 	       u_int32_t timeout)
7702 {
7703 	struct scsi_log_sense *scsi_cmd;
7704 	u_int8_t cdb_len;
7705 
7706 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
7707 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7708 	scsi_cmd->opcode = LOG_SENSE;
7709 	scsi_cmd->page = page_code | page;
7710 	if (save_pages != 0)
7711 		scsi_cmd->byte2 |= SLS_SP;
7712 	if (ppc != 0)
7713 		scsi_cmd->byte2 |= SLS_PPC;
7714 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
7715 	scsi_ulto2b(param_len, scsi_cmd->length);
7716 	cdb_len = sizeof(*scsi_cmd);
7717 
7718 	cam_fill_csio(csio,
7719 		      retries,
7720 		      cbfcnp,
7721 		      /*flags*/CAM_DIR_IN,
7722 		      tag_action,
7723 		      /*data_ptr*/param_buf,
7724 		      /*dxfer_len*/param_len,
7725 		      sense_len,
7726 		      cdb_len,
7727 		      timeout);
7728 }
7729 
7730 void
7731 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
7732 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7733 		u_int8_t tag_action, u_int8_t page_code, int save_pages,
7734 		int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
7735 		u_int8_t sense_len, u_int32_t timeout)
7736 {
7737 	struct scsi_log_select *scsi_cmd;
7738 	u_int8_t cdb_len;
7739 
7740 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
7741 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7742 	scsi_cmd->opcode = LOG_SELECT;
7743 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
7744 	if (save_pages != 0)
7745 		scsi_cmd->byte2 |= SLS_SP;
7746 	if (pc_reset != 0)
7747 		scsi_cmd->byte2 |= SLS_PCR;
7748 	scsi_ulto2b(param_len, scsi_cmd->length);
7749 	cdb_len = sizeof(*scsi_cmd);
7750 
7751 	cam_fill_csio(csio,
7752 		      retries,
7753 		      cbfcnp,
7754 		      /*flags*/CAM_DIR_OUT,
7755 		      tag_action,
7756 		      /*data_ptr*/param_buf,
7757 		      /*dxfer_len*/param_len,
7758 		      sense_len,
7759 		      cdb_len,
7760 		      timeout);
7761 }
7762 
7763 /*
7764  * Prevent or allow the user to remove the media
7765  */
7766 void
7767 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
7768 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7769 	     u_int8_t tag_action, u_int8_t action,
7770 	     u_int8_t sense_len, u_int32_t timeout)
7771 {
7772 	struct scsi_prevent *scsi_cmd;
7773 
7774 	cam_fill_csio(csio,
7775 		      retries,
7776 		      cbfcnp,
7777 		      /*flags*/CAM_DIR_NONE,
7778 		      tag_action,
7779 		      /*data_ptr*/NULL,
7780 		      /*dxfer_len*/0,
7781 		      sense_len,
7782 		      sizeof(*scsi_cmd),
7783 		      timeout);
7784 
7785 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
7786 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7787 	scsi_cmd->opcode = PREVENT_ALLOW;
7788 	scsi_cmd->how = action;
7789 }
7790 
7791 /* XXX allow specification of address and PMI bit and LBA */
7792 void
7793 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
7794 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7795 		   u_int8_t tag_action,
7796 		   struct scsi_read_capacity_data *rcap_buf,
7797 		   u_int8_t sense_len, u_int32_t timeout)
7798 {
7799 	struct scsi_read_capacity *scsi_cmd;
7800 
7801 	cam_fill_csio(csio,
7802 		      retries,
7803 		      cbfcnp,
7804 		      /*flags*/CAM_DIR_IN,
7805 		      tag_action,
7806 		      /*data_ptr*/(u_int8_t *)rcap_buf,
7807 		      /*dxfer_len*/sizeof(*rcap_buf),
7808 		      sense_len,
7809 		      sizeof(*scsi_cmd),
7810 		      timeout);
7811 
7812 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
7813 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7814 	scsi_cmd->opcode = READ_CAPACITY;
7815 }
7816 
7817 void
7818 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
7819 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
7820 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
7821 		      uint8_t *rcap_buf, int rcap_buf_len, uint8_t sense_len,
7822 		      uint32_t timeout)
7823 {
7824 	struct scsi_read_capacity_16 *scsi_cmd;
7825 
7826 
7827 	cam_fill_csio(csio,
7828 		      retries,
7829 		      cbfcnp,
7830 		      /*flags*/CAM_DIR_IN,
7831 		      tag_action,
7832 		      /*data_ptr*/(u_int8_t *)rcap_buf,
7833 		      /*dxfer_len*/rcap_buf_len,
7834 		      sense_len,
7835 		      sizeof(*scsi_cmd),
7836 		      timeout);
7837 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
7838 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7839 	scsi_cmd->opcode = SERVICE_ACTION_IN;
7840 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
7841 	scsi_u64to8b(lba, scsi_cmd->addr);
7842 	scsi_ulto4b(rcap_buf_len, scsi_cmd->alloc_len);
7843 	if (pmi)
7844 		reladr |= SRC16_PMI;
7845 	if (reladr)
7846 		reladr |= SRC16_RELADR;
7847 }
7848 
7849 void
7850 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
7851 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7852 		 u_int8_t tag_action, u_int8_t select_report,
7853 		 struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
7854 		 u_int8_t sense_len, u_int32_t timeout)
7855 {
7856 	struct scsi_report_luns *scsi_cmd;
7857 
7858 	cam_fill_csio(csio,
7859 		      retries,
7860 		      cbfcnp,
7861 		      /*flags*/CAM_DIR_IN,
7862 		      tag_action,
7863 		      /*data_ptr*/(u_int8_t *)rpl_buf,
7864 		      /*dxfer_len*/alloc_len,
7865 		      sense_len,
7866 		      sizeof(*scsi_cmd),
7867 		      timeout);
7868 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
7869 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7870 	scsi_cmd->opcode = REPORT_LUNS;
7871 	scsi_cmd->select_report = select_report;
7872 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7873 }
7874 
7875 void
7876 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7877 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7878 		 u_int8_t tag_action, u_int8_t pdf,
7879 		 void *buf, u_int32_t alloc_len,
7880 		 u_int8_t sense_len, u_int32_t timeout)
7881 {
7882 	struct scsi_target_group *scsi_cmd;
7883 
7884 	cam_fill_csio(csio,
7885 		      retries,
7886 		      cbfcnp,
7887 		      /*flags*/CAM_DIR_IN,
7888 		      tag_action,
7889 		      /*data_ptr*/(u_int8_t *)buf,
7890 		      /*dxfer_len*/alloc_len,
7891 		      sense_len,
7892 		      sizeof(*scsi_cmd),
7893 		      timeout);
7894 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7895 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7896 	scsi_cmd->opcode = MAINTENANCE_IN;
7897 	scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
7898 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7899 }
7900 
7901 void
7902 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7903 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7904 		 u_int8_t tag_action, void *buf, u_int32_t alloc_len,
7905 		 u_int8_t sense_len, u_int32_t timeout)
7906 {
7907 	struct scsi_target_group *scsi_cmd;
7908 
7909 	cam_fill_csio(csio,
7910 		      retries,
7911 		      cbfcnp,
7912 		      /*flags*/CAM_DIR_OUT,
7913 		      tag_action,
7914 		      /*data_ptr*/(u_int8_t *)buf,
7915 		      /*dxfer_len*/alloc_len,
7916 		      sense_len,
7917 		      sizeof(*scsi_cmd),
7918 		      timeout);
7919 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7920 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7921 	scsi_cmd->opcode = MAINTENANCE_OUT;
7922 	scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
7923 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7924 }
7925 
7926 /*
7927  * Syncronize the media to the contents of the cache for
7928  * the given lba/count pair.  Specifying 0/0 means sync
7929  * the whole cache.
7930  */
7931 void
7932 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
7933 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
7934 		       u_int8_t tag_action, u_int32_t begin_lba,
7935 		       u_int16_t lb_count, u_int8_t sense_len,
7936 		       u_int32_t timeout)
7937 {
7938 	struct scsi_sync_cache *scsi_cmd;
7939 
7940 	cam_fill_csio(csio,
7941 		      retries,
7942 		      cbfcnp,
7943 		      /*flags*/CAM_DIR_NONE,
7944 		      tag_action,
7945 		      /*data_ptr*/NULL,
7946 		      /*dxfer_len*/0,
7947 		      sense_len,
7948 		      sizeof(*scsi_cmd),
7949 		      timeout);
7950 
7951 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
7952 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7953 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
7954 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
7955 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
7956 }
7957 
7958 void
7959 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
7960 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7961 		u_int8_t tag_action, int readop, u_int8_t byte2,
7962 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
7963 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
7964 		u_int32_t timeout)
7965 {
7966 	int read;
7967 	u_int8_t cdb_len;
7968 
7969 	read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
7970 
7971 	/*
7972 	 * Use the smallest possible command to perform the operation
7973 	 * as some legacy hardware does not support the 10 byte commands.
7974 	 * If any of the bits in byte2 is set, we have to go with a larger
7975 	 * command.
7976 	 */
7977 	if ((minimum_cmd_size < 10)
7978 	 && ((lba & 0x1fffff) == lba)
7979 	 && ((block_count & 0xff) == block_count)
7980 	 && (byte2 == 0)) {
7981 		/*
7982 		 * We can fit in a 6 byte cdb.
7983 		 */
7984 		struct scsi_rw_6 *scsi_cmd;
7985 
7986 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
7987 		scsi_cmd->opcode = read ? READ_6 : WRITE_6;
7988 		scsi_ulto3b(lba, scsi_cmd->addr);
7989 		scsi_cmd->length = block_count & 0xff;
7990 		scsi_cmd->control = 0;
7991 		cdb_len = sizeof(*scsi_cmd);
7992 
7993 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
7994 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
7995 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
7996 			   scsi_cmd->length, dxfer_len));
7997 	} else if ((minimum_cmd_size < 12)
7998 		&& ((block_count & 0xffff) == block_count)
7999 		&& ((lba & 0xffffffff) == lba)) {
8000 		/*
8001 		 * Need a 10 byte cdb.
8002 		 */
8003 		struct scsi_rw_10 *scsi_cmd;
8004 
8005 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
8006 		scsi_cmd->opcode = read ? READ_10 : WRITE_10;
8007 		scsi_cmd->byte2 = byte2;
8008 		scsi_ulto4b(lba, scsi_cmd->addr);
8009 		scsi_cmd->reserved = 0;
8010 		scsi_ulto2b(block_count, scsi_cmd->length);
8011 		scsi_cmd->control = 0;
8012 		cdb_len = sizeof(*scsi_cmd);
8013 
8014 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8015 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8016 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8017 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8018 			   scsi_cmd->length[1], dxfer_len));
8019 	} else if ((minimum_cmd_size < 16)
8020 		&& ((block_count & 0xffffffff) == block_count)
8021 		&& ((lba & 0xffffffff) == lba)) {
8022 		/*
8023 		 * The block count is too big for a 10 byte CDB, use a 12
8024 		 * byte CDB.
8025 		 */
8026 		struct scsi_rw_12 *scsi_cmd;
8027 
8028 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
8029 		scsi_cmd->opcode = read ? READ_12 : WRITE_12;
8030 		scsi_cmd->byte2 = byte2;
8031 		scsi_ulto4b(lba, scsi_cmd->addr);
8032 		scsi_cmd->reserved = 0;
8033 		scsi_ulto4b(block_count, scsi_cmd->length);
8034 		scsi_cmd->control = 0;
8035 		cdb_len = sizeof(*scsi_cmd);
8036 
8037 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8038 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
8039 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8040 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8041 			   scsi_cmd->length[1], scsi_cmd->length[2],
8042 			   scsi_cmd->length[3], dxfer_len));
8043 	} else {
8044 		/*
8045 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8046 		 * than 2^32, or if the user asks for a 16 byte command.
8047 		 */
8048 		struct scsi_rw_16 *scsi_cmd;
8049 
8050 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
8051 		scsi_cmd->opcode = read ? READ_16 : WRITE_16;
8052 		scsi_cmd->byte2 = byte2;
8053 		scsi_u64to8b(lba, scsi_cmd->addr);
8054 		scsi_cmd->reserved = 0;
8055 		scsi_ulto4b(block_count, scsi_cmd->length);
8056 		scsi_cmd->control = 0;
8057 		cdb_len = sizeof(*scsi_cmd);
8058 	}
8059 	cam_fill_csio(csio,
8060 		      retries,
8061 		      cbfcnp,
8062 		      (read ? CAM_DIR_IN : CAM_DIR_OUT) |
8063 		      ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
8064 		      tag_action,
8065 		      data_ptr,
8066 		      dxfer_len,
8067 		      sense_len,
8068 		      cdb_len,
8069 		      timeout);
8070 }
8071 
8072 void
8073 scsi_write_same(struct ccb_scsiio *csio, u_int32_t retries,
8074 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8075 		u_int8_t tag_action, u_int8_t byte2,
8076 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
8077 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
8078 		u_int32_t timeout)
8079 {
8080 	u_int8_t cdb_len;
8081 	if ((minimum_cmd_size < 16) &&
8082 	    ((block_count & 0xffff) == block_count) &&
8083 	    ((lba & 0xffffffff) == lba)) {
8084 		/*
8085 		 * Need a 10 byte cdb.
8086 		 */
8087 		struct scsi_write_same_10 *scsi_cmd;
8088 
8089 		scsi_cmd = (struct scsi_write_same_10 *)&csio->cdb_io.cdb_bytes;
8090 		scsi_cmd->opcode = WRITE_SAME_10;
8091 		scsi_cmd->byte2 = byte2;
8092 		scsi_ulto4b(lba, scsi_cmd->addr);
8093 		scsi_cmd->group = 0;
8094 		scsi_ulto2b(block_count, scsi_cmd->length);
8095 		scsi_cmd->control = 0;
8096 		cdb_len = sizeof(*scsi_cmd);
8097 
8098 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8099 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8100 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8101 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8102 			   scsi_cmd->length[1], dxfer_len));
8103 	} else {
8104 		/*
8105 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8106 		 * than 2^32, or if the user asks for a 16 byte command.
8107 		 */
8108 		struct scsi_write_same_16 *scsi_cmd;
8109 
8110 		scsi_cmd = (struct scsi_write_same_16 *)&csio->cdb_io.cdb_bytes;
8111 		scsi_cmd->opcode = WRITE_SAME_16;
8112 		scsi_cmd->byte2 = byte2;
8113 		scsi_u64to8b(lba, scsi_cmd->addr);
8114 		scsi_ulto4b(block_count, scsi_cmd->length);
8115 		scsi_cmd->group = 0;
8116 		scsi_cmd->control = 0;
8117 		cdb_len = sizeof(*scsi_cmd);
8118 
8119 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8120 			  ("16byte: %x%x%x%x%x%x%x%x:%x%x%x%x: %d\n",
8121 			   scsi_cmd->addr[0], scsi_cmd->addr[1],
8122 			   scsi_cmd->addr[2], scsi_cmd->addr[3],
8123 			   scsi_cmd->addr[4], scsi_cmd->addr[5],
8124 			   scsi_cmd->addr[6], scsi_cmd->addr[7],
8125 			   scsi_cmd->length[0], scsi_cmd->length[1],
8126 			   scsi_cmd->length[2], scsi_cmd->length[3],
8127 			   dxfer_len));
8128 	}
8129 	cam_fill_csio(csio,
8130 		      retries,
8131 		      cbfcnp,
8132 		      /*flags*/CAM_DIR_OUT,
8133 		      tag_action,
8134 		      data_ptr,
8135 		      dxfer_len,
8136 		      sense_len,
8137 		      cdb_len,
8138 		      timeout);
8139 }
8140 
8141 void
8142 scsi_ata_identify(struct ccb_scsiio *csio, u_int32_t retries,
8143 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8144 		  u_int8_t tag_action, u_int8_t *data_ptr,
8145 		  u_int16_t dxfer_len, u_int8_t sense_len,
8146 		  u_int32_t timeout)
8147 {
8148 	scsi_ata_pass(csio,
8149 		      retries,
8150 		      cbfcnp,
8151 		      /*flags*/CAM_DIR_IN,
8152 		      tag_action,
8153 		      /*protocol*/AP_PROTO_PIO_IN,
8154 		      /*ata_flags*/AP_FLAG_TDIR_FROM_DEV |
8155 				   AP_FLAG_BYT_BLOK_BYTES |
8156 				   AP_FLAG_TLEN_SECT_CNT,
8157 		      /*features*/0,
8158 		      /*sector_count*/dxfer_len,
8159 		      /*lba*/0,
8160 		      /*command*/ATA_ATA_IDENTIFY,
8161 		      /*device*/ 0,
8162 		      /*icc*/ 0,
8163 		      /*auxiliary*/ 0,
8164 		      /*control*/0,
8165 		      data_ptr,
8166 		      dxfer_len,
8167 		      /*cdb_storage*/ NULL,
8168 		      /*cdb_storage_len*/ 0,
8169 		      /*minimum_cmd_size*/ 0,
8170 		      sense_len,
8171 		      timeout);
8172 }
8173 
8174 void
8175 scsi_ata_trim(struct ccb_scsiio *csio, u_int32_t retries,
8176 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8177 	      u_int8_t tag_action, u_int16_t block_count,
8178 	      u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8179 	      u_int32_t timeout)
8180 {
8181 	scsi_ata_pass_16(csio,
8182 			 retries,
8183 			 cbfcnp,
8184 			 /*flags*/CAM_DIR_OUT,
8185 			 tag_action,
8186 			 /*protocol*/AP_EXTEND|AP_PROTO_DMA,
8187 			 /*ata_flags*/AP_FLAG_TLEN_SECT_CNT|AP_FLAG_BYT_BLOK_BLOCKS,
8188 			 /*features*/ATA_DSM_TRIM,
8189 			 /*sector_count*/block_count,
8190 			 /*lba*/0,
8191 			 /*command*/ATA_DATA_SET_MANAGEMENT,
8192 			 /*control*/0,
8193 			 data_ptr,
8194 			 dxfer_len,
8195 			 sense_len,
8196 			 timeout);
8197 }
8198 
8199 int
8200 scsi_ata_read_log(struct ccb_scsiio *csio, uint32_t retries,
8201 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8202 		  uint8_t tag_action, uint32_t log_address,
8203 		  uint32_t page_number, uint16_t block_count,
8204 		  uint8_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
8205 		  uint8_t sense_len, uint32_t timeout)
8206 {
8207 	uint8_t command, protocol_out;
8208 	uint16_t count_out;
8209 	uint64_t lba;
8210 	int retval;
8211 
8212 	retval = 0;
8213 
8214 	switch (protocol) {
8215 	case AP_PROTO_DMA:
8216 		count_out = block_count;
8217 		command = ATA_READ_LOG_DMA_EXT;
8218 		protocol_out = AP_PROTO_DMA;
8219 		break;
8220 	case AP_PROTO_PIO_IN:
8221 	default:
8222 		count_out = block_count;
8223 		command = ATA_READ_LOG_EXT;
8224 		protocol_out = AP_PROTO_PIO_IN;
8225 		break;
8226 	}
8227 
8228 	lba = (((uint64_t)page_number & 0xff00) << 32) |
8229 	      ((page_number & 0x00ff) << 8) |
8230 	      (log_address & 0xff);
8231 
8232 	protocol_out |= AP_EXTEND;
8233 
8234 	retval = scsi_ata_pass(csio,
8235 			       retries,
8236 			       cbfcnp,
8237 			       /*flags*/CAM_DIR_IN,
8238 			       tag_action,
8239 			       /*protocol*/ protocol_out,
8240 			       /*ata_flags*/AP_FLAG_TLEN_SECT_CNT |
8241 					    AP_FLAG_BYT_BLOK_BLOCKS |
8242 					    AP_FLAG_TDIR_FROM_DEV,
8243 			       /*feature*/ 0,
8244 			       /*sector_count*/ count_out,
8245 			       /*lba*/ lba,
8246 			       /*command*/ command,
8247 			       /*device*/ 0,
8248 			       /*icc*/ 0,
8249 			       /*auxiliary*/ 0,
8250 			       /*control*/0,
8251 			       data_ptr,
8252 			       dxfer_len,
8253 			       /*cdb_storage*/ NULL,
8254 			       /*cdb_storage_len*/ 0,
8255 			       /*minimum_cmd_size*/ 0,
8256 			       sense_len,
8257 			       timeout);
8258 
8259 	return (retval);
8260 }
8261 
8262 /*
8263  * Note! This is an unusual CDB building function because it can return
8264  * an error in the event that the command in question requires a variable
8265  * length CDB, but the caller has not given storage space for one or has not
8266  * given enough storage space.  If there is enough space available in the
8267  * standard SCSI CCB CDB bytes, we'll prefer that over passed in storage.
8268  */
8269 int
8270 scsi_ata_pass(struct ccb_scsiio *csio, uint32_t retries,
8271 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8272 	      uint32_t flags, uint8_t tag_action,
8273 	      uint8_t protocol, uint8_t ata_flags, uint16_t features,
8274 	      uint16_t sector_count, uint64_t lba, uint8_t command,
8275 	      uint8_t device, uint8_t icc, uint32_t auxiliary,
8276 	      uint8_t control, u_int8_t *data_ptr, uint32_t dxfer_len,
8277 	      uint8_t *cdb_storage, size_t cdb_storage_len,
8278 	      int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
8279 {
8280 	uint32_t cam_flags;
8281 	uint8_t *cdb_ptr;
8282 	int cmd_size;
8283 	int retval;
8284 	uint8_t cdb_len;
8285 
8286 	retval = 0;
8287 	cam_flags = flags;
8288 
8289 	/*
8290 	 * Round the user's request to the nearest command size that is at
8291 	 * least as big as what he requested.
8292 	 */
8293 	if (minimum_cmd_size <= 12)
8294 		cmd_size = 12;
8295 	else if (minimum_cmd_size > 16)
8296 		cmd_size = 32;
8297 	else
8298 		cmd_size = 16;
8299 
8300 	/*
8301 	 * If we have parameters that require a 48-bit ATA command, we have to
8302 	 * use the 16 byte ATA PASS-THROUGH command at least.
8303 	 */
8304 	if (((lba > ATA_MAX_28BIT_LBA)
8305 	  || (sector_count > 255)
8306 	  || (features > 255)
8307 	  || (protocol & AP_EXTEND))
8308 	 && ((cmd_size < 16)
8309 	  || ((protocol & AP_EXTEND) == 0))) {
8310 		if (cmd_size < 16)
8311 			cmd_size = 16;
8312 		protocol |= AP_EXTEND;
8313 	}
8314 
8315 	/*
8316 	 * The icc and auxiliary ATA registers are only supported in the
8317 	 * 32-byte version of the ATA PASS-THROUGH command.
8318 	 */
8319 	if ((icc != 0)
8320 	 || (auxiliary != 0)) {
8321 		cmd_size = 32;
8322 		protocol |= AP_EXTEND;
8323 	}
8324 
8325 
8326 	if ((cmd_size > sizeof(csio->cdb_io.cdb_bytes))
8327 	 && ((cdb_storage == NULL)
8328 	  || (cdb_storage_len < cmd_size))) {
8329 		retval = 1;
8330 		goto bailout;
8331 	}
8332 
8333 	/*
8334 	 * At this point we know we have enough space to store the command
8335 	 * in one place or another.  We prefer the built-in array, but used
8336 	 * the passed in storage if necessary.
8337 	 */
8338 	if (cmd_size <= sizeof(csio->cdb_io.cdb_bytes))
8339 		cdb_ptr = csio->cdb_io.cdb_bytes;
8340 	else {
8341 		cdb_ptr = cdb_storage;
8342 		cam_flags |= CAM_CDB_POINTER;
8343 	}
8344 
8345 	if (cmd_size <= 12) {
8346 		struct ata_pass_12 *cdb;
8347 
8348 		cdb = (struct ata_pass_12 *)cdb_ptr;
8349 		cdb_len = sizeof(*cdb);
8350 		bzero(cdb, cdb_len);
8351 
8352 		cdb->opcode = ATA_PASS_12;
8353 		cdb->protocol = protocol;
8354 		cdb->flags = ata_flags;
8355 		cdb->features = features;
8356 		cdb->sector_count = sector_count;
8357 		cdb->lba_low = lba & 0xff;
8358 		cdb->lba_mid = (lba >> 8) & 0xff;
8359 		cdb->lba_high = (lba >> 16) & 0xff;
8360 		cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8361 		cdb->command = command;
8362 		cdb->control = control;
8363 	} else if (cmd_size <= 16) {
8364 		struct ata_pass_16 *cdb;
8365 
8366 		cdb = (struct ata_pass_16 *)cdb_ptr;
8367 		cdb_len = sizeof(*cdb);
8368 		bzero(cdb, cdb_len);
8369 
8370 		cdb->opcode = ATA_PASS_16;
8371 		cdb->protocol = protocol;
8372 		cdb->flags = ata_flags;
8373 		cdb->features = features & 0xff;
8374 		cdb->sector_count = sector_count & 0xff;
8375 		cdb->lba_low = lba & 0xff;
8376 		cdb->lba_mid = (lba >> 8) & 0xff;
8377 		cdb->lba_high = (lba >> 16) & 0xff;
8378 		/*
8379 		 * If AP_EXTEND is set, we're sending a 48-bit command.
8380 		 * Otherwise it's a 28-bit command.
8381 		 */
8382 		if (protocol & AP_EXTEND) {
8383 			cdb->lba_low_ext = (lba >> 24) & 0xff;
8384 			cdb->lba_mid_ext = (lba >> 32) & 0xff;
8385 			cdb->lba_high_ext = (lba >> 40) & 0xff;
8386 			cdb->features_ext = (features >> 8) & 0xff;
8387 			cdb->sector_count_ext = (sector_count >> 8) & 0xff;
8388 			cdb->device = device | ATA_DEV_LBA;
8389 		} else {
8390 			cdb->lba_low_ext = (lba >> 24) & 0xf;
8391 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8392 		}
8393 		cdb->command = command;
8394 		cdb->control = control;
8395 	} else {
8396 		struct ata_pass_32 *cdb;
8397 		uint8_t tmp_lba[8];
8398 
8399 		cdb = (struct ata_pass_32 *)cdb_ptr;
8400 		cdb_len = sizeof(*cdb);
8401 		bzero(cdb, cdb_len);
8402 		cdb->opcode = VARIABLE_LEN_CDB;
8403 		cdb->control = control;
8404 		cdb->length = sizeof(*cdb) - __offsetof(struct ata_pass_32,
8405 							service_action);
8406 		scsi_ulto2b(ATA_PASS_32_SA, cdb->service_action);
8407 		cdb->protocol = protocol;
8408 		cdb->flags = ata_flags;
8409 
8410 		if ((protocol & AP_EXTEND) == 0) {
8411 			lba &= 0x0fffffff;
8412 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8413 			features &= 0xff;
8414 			sector_count &= 0xff;
8415 		} else {
8416 			cdb->device = device | ATA_DEV_LBA;
8417 		}
8418 		scsi_u64to8b(lba, tmp_lba);
8419 		bcopy(&tmp_lba[2], cdb->lba, sizeof(cdb->lba));
8420 		scsi_ulto2b(features, cdb->features);
8421 		scsi_ulto2b(sector_count, cdb->count);
8422 		cdb->command = command;
8423 		cdb->icc = icc;
8424 		scsi_ulto4b(auxiliary, cdb->auxiliary);
8425 	}
8426 
8427 	cam_fill_csio(csio,
8428 		      retries,
8429 		      cbfcnp,
8430 		      cam_flags,
8431 		      tag_action,
8432 		      data_ptr,
8433 		      dxfer_len,
8434 		      sense_len,
8435 		      cmd_size,
8436 		      timeout);
8437 bailout:
8438 	return (retval);
8439 }
8440 
8441 void
8442 scsi_ata_pass_16(struct ccb_scsiio *csio, u_int32_t retries,
8443 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8444 		 u_int32_t flags, u_int8_t tag_action,
8445 		 u_int8_t protocol, u_int8_t ata_flags, u_int16_t features,
8446 		 u_int16_t sector_count, uint64_t lba, u_int8_t command,
8447 		 u_int8_t control, u_int8_t *data_ptr, u_int16_t dxfer_len,
8448 		 u_int8_t sense_len, u_int32_t timeout)
8449 {
8450 	struct ata_pass_16 *ata_cmd;
8451 
8452 	ata_cmd = (struct ata_pass_16 *)&csio->cdb_io.cdb_bytes;
8453 	ata_cmd->opcode = ATA_PASS_16;
8454 	ata_cmd->protocol = protocol;
8455 	ata_cmd->flags = ata_flags;
8456 	ata_cmd->features_ext = features >> 8;
8457 	ata_cmd->features = features;
8458 	ata_cmd->sector_count_ext = sector_count >> 8;
8459 	ata_cmd->sector_count = sector_count;
8460 	ata_cmd->lba_low = lba;
8461 	ata_cmd->lba_mid = lba >> 8;
8462 	ata_cmd->lba_high = lba >> 16;
8463 	ata_cmd->device = ATA_DEV_LBA;
8464 	if (protocol & AP_EXTEND) {
8465 		ata_cmd->lba_low_ext = lba >> 24;
8466 		ata_cmd->lba_mid_ext = lba >> 32;
8467 		ata_cmd->lba_high_ext = lba >> 40;
8468 	} else
8469 		ata_cmd->device |= (lba >> 24) & 0x0f;
8470 	ata_cmd->command = command;
8471 	ata_cmd->control = control;
8472 
8473 	cam_fill_csio(csio,
8474 		      retries,
8475 		      cbfcnp,
8476 		      flags,
8477 		      tag_action,
8478 		      data_ptr,
8479 		      dxfer_len,
8480 		      sense_len,
8481 		      sizeof(*ata_cmd),
8482 		      timeout);
8483 }
8484 
8485 void
8486 scsi_unmap(struct ccb_scsiio *csio, u_int32_t retries,
8487 	   void (*cbfcnp)(struct cam_periph *, union ccb *),
8488 	   u_int8_t tag_action, u_int8_t byte2,
8489 	   u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8490 	   u_int32_t timeout)
8491 {
8492 	struct scsi_unmap *scsi_cmd;
8493 
8494 	scsi_cmd = (struct scsi_unmap *)&csio->cdb_io.cdb_bytes;
8495 	scsi_cmd->opcode = UNMAP;
8496 	scsi_cmd->byte2 = byte2;
8497 	scsi_ulto4b(0, scsi_cmd->reserved);
8498 	scsi_cmd->group = 0;
8499 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
8500 	scsi_cmd->control = 0;
8501 
8502 	cam_fill_csio(csio,
8503 		      retries,
8504 		      cbfcnp,
8505 		      /*flags*/CAM_DIR_OUT,
8506 		      tag_action,
8507 		      data_ptr,
8508 		      dxfer_len,
8509 		      sense_len,
8510 		      sizeof(*scsi_cmd),
8511 		      timeout);
8512 }
8513 
8514 void
8515 scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
8516 				void (*cbfcnp)(struct cam_periph *, union ccb*),
8517 				uint8_t tag_action, int pcv, uint8_t page_code,
8518 				uint8_t *data_ptr, uint16_t allocation_length,
8519 				uint8_t sense_len, uint32_t timeout)
8520 {
8521 	struct scsi_receive_diag *scsi_cmd;
8522 
8523 	scsi_cmd = (struct scsi_receive_diag *)&csio->cdb_io.cdb_bytes;
8524 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8525 	scsi_cmd->opcode = RECEIVE_DIAGNOSTIC;
8526 	if (pcv) {
8527 		scsi_cmd->byte2 |= SRD_PCV;
8528 		scsi_cmd->page_code = page_code;
8529 	}
8530 	scsi_ulto2b(allocation_length, scsi_cmd->length);
8531 
8532 	cam_fill_csio(csio,
8533 		      retries,
8534 		      cbfcnp,
8535 		      /*flags*/CAM_DIR_IN,
8536 		      tag_action,
8537 		      data_ptr,
8538 		      allocation_length,
8539 		      sense_len,
8540 		      sizeof(*scsi_cmd),
8541 		      timeout);
8542 }
8543 
8544 void
8545 scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
8546 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
8547 		     uint8_t tag_action, int unit_offline, int device_offline,
8548 		     int self_test, int page_format, int self_test_code,
8549 		     uint8_t *data_ptr, uint16_t param_list_length,
8550 		     uint8_t sense_len, uint32_t timeout)
8551 {
8552 	struct scsi_send_diag *scsi_cmd;
8553 
8554 	scsi_cmd = (struct scsi_send_diag *)&csio->cdb_io.cdb_bytes;
8555 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8556 	scsi_cmd->opcode = SEND_DIAGNOSTIC;
8557 
8558 	/*
8559 	 * The default self-test mode control and specific test
8560 	 * control are mutually exclusive.
8561 	 */
8562 	if (self_test)
8563 		self_test_code = SSD_SELF_TEST_CODE_NONE;
8564 
8565 	scsi_cmd->byte2 = ((self_test_code << SSD_SELF_TEST_CODE_SHIFT)
8566 			 & SSD_SELF_TEST_CODE_MASK)
8567 			| (unit_offline   ? SSD_UNITOFFL : 0)
8568 			| (device_offline ? SSD_DEVOFFL  : 0)
8569 			| (self_test      ? SSD_SELFTEST : 0)
8570 			| (page_format    ? SSD_PF       : 0);
8571 	scsi_ulto2b(param_list_length, scsi_cmd->length);
8572 
8573 	cam_fill_csio(csio,
8574 		      retries,
8575 		      cbfcnp,
8576 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8577 		      tag_action,
8578 		      data_ptr,
8579 		      param_list_length,
8580 		      sense_len,
8581 		      sizeof(*scsi_cmd),
8582 		      timeout);
8583 }
8584 
8585 void
8586 scsi_read_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8587 			void (*cbfcnp)(struct cam_periph *, union ccb*),
8588 			uint8_t tag_action, int mode,
8589 			uint8_t buffer_id, u_int32_t offset,
8590 			uint8_t *data_ptr, uint32_t allocation_length,
8591 			uint8_t sense_len, uint32_t timeout)
8592 {
8593 	struct scsi_read_buffer *scsi_cmd;
8594 
8595 	scsi_cmd = (struct scsi_read_buffer *)&csio->cdb_io.cdb_bytes;
8596 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8597 	scsi_cmd->opcode = READ_BUFFER;
8598 	scsi_cmd->byte2 = mode;
8599 	scsi_cmd->buffer_id = buffer_id;
8600 	scsi_ulto3b(offset, scsi_cmd->offset);
8601 	scsi_ulto3b(allocation_length, scsi_cmd->length);
8602 
8603 	cam_fill_csio(csio,
8604 		      retries,
8605 		      cbfcnp,
8606 		      /*flags*/CAM_DIR_IN,
8607 		      tag_action,
8608 		      data_ptr,
8609 		      allocation_length,
8610 		      sense_len,
8611 		      sizeof(*scsi_cmd),
8612 		      timeout);
8613 }
8614 
8615 void
8616 scsi_write_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8617 			void (*cbfcnp)(struct cam_periph *, union ccb *),
8618 			uint8_t tag_action, int mode,
8619 			uint8_t buffer_id, u_int32_t offset,
8620 			uint8_t *data_ptr, uint32_t param_list_length,
8621 			uint8_t sense_len, uint32_t timeout)
8622 {
8623 	struct scsi_write_buffer *scsi_cmd;
8624 
8625 	scsi_cmd = (struct scsi_write_buffer *)&csio->cdb_io.cdb_bytes;
8626 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8627 	scsi_cmd->opcode = WRITE_BUFFER;
8628 	scsi_cmd->byte2 = mode;
8629 	scsi_cmd->buffer_id = buffer_id;
8630 	scsi_ulto3b(offset, scsi_cmd->offset);
8631 	scsi_ulto3b(param_list_length, scsi_cmd->length);
8632 
8633 	cam_fill_csio(csio,
8634 		      retries,
8635 		      cbfcnp,
8636 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8637 		      tag_action,
8638 		      data_ptr,
8639 		      param_list_length,
8640 		      sense_len,
8641 		      sizeof(*scsi_cmd),
8642 		      timeout);
8643 }
8644 
8645 void
8646 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
8647 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8648 		u_int8_t tag_action, int start, int load_eject,
8649 		int immediate, u_int8_t sense_len, u_int32_t timeout)
8650 {
8651 	struct scsi_start_stop_unit *scsi_cmd;
8652 	int extra_flags = 0;
8653 
8654 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
8655 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8656 	scsi_cmd->opcode = START_STOP_UNIT;
8657 	if (start != 0) {
8658 		scsi_cmd->how |= SSS_START;
8659 		/* it takes a lot of power to start a drive */
8660 		extra_flags |= CAM_HIGH_POWER;
8661 	}
8662 	if (load_eject != 0)
8663 		scsi_cmd->how |= SSS_LOEJ;
8664 	if (immediate != 0)
8665 		scsi_cmd->byte2 |= SSS_IMMED;
8666 
8667 	cam_fill_csio(csio,
8668 		      retries,
8669 		      cbfcnp,
8670 		      /*flags*/CAM_DIR_NONE | extra_flags,
8671 		      tag_action,
8672 		      /*data_ptr*/NULL,
8673 		      /*dxfer_len*/0,
8674 		      sense_len,
8675 		      sizeof(*scsi_cmd),
8676 		      timeout);
8677 }
8678 
8679 void
8680 scsi_read_attribute(struct ccb_scsiio *csio, u_int32_t retries,
8681 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
8682 		    u_int8_t tag_action, u_int8_t service_action,
8683 		    uint32_t element, u_int8_t elem_type, int logical_volume,
8684 		    int partition, u_int32_t first_attribute, int cache,
8685 		    u_int8_t *data_ptr, u_int32_t length, int sense_len,
8686 		    u_int32_t timeout)
8687 {
8688 	struct scsi_read_attribute *scsi_cmd;
8689 
8690 	scsi_cmd = (struct scsi_read_attribute *)&csio->cdb_io.cdb_bytes;
8691 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8692 
8693 	scsi_cmd->opcode = READ_ATTRIBUTE;
8694 	scsi_cmd->service_action = service_action;
8695 	scsi_ulto2b(element, scsi_cmd->element);
8696 	scsi_cmd->elem_type = elem_type;
8697 	scsi_cmd->logical_volume = logical_volume;
8698 	scsi_cmd->partition = partition;
8699 	scsi_ulto2b(first_attribute, scsi_cmd->first_attribute);
8700 	scsi_ulto4b(length, scsi_cmd->length);
8701 	if (cache != 0)
8702 		scsi_cmd->cache |= SRA_CACHE;
8703 
8704 	cam_fill_csio(csio,
8705 		      retries,
8706 		      cbfcnp,
8707 		      /*flags*/CAM_DIR_IN,
8708 		      tag_action,
8709 		      /*data_ptr*/data_ptr,
8710 		      /*dxfer_len*/length,
8711 		      sense_len,
8712 		      sizeof(*scsi_cmd),
8713 		      timeout);
8714 }
8715 
8716 void
8717 scsi_write_attribute(struct ccb_scsiio *csio, u_int32_t retries,
8718 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
8719 		    u_int8_t tag_action, uint32_t element, int logical_volume,
8720 		    int partition, int wtc, u_int8_t *data_ptr,
8721 		    u_int32_t length, int sense_len, u_int32_t timeout)
8722 {
8723 	struct scsi_write_attribute *scsi_cmd;
8724 
8725 	scsi_cmd = (struct scsi_write_attribute *)&csio->cdb_io.cdb_bytes;
8726 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8727 
8728 	scsi_cmd->opcode = WRITE_ATTRIBUTE;
8729 	if (wtc != 0)
8730 		scsi_cmd->byte2 = SWA_WTC;
8731 	scsi_ulto3b(element, scsi_cmd->element);
8732 	scsi_cmd->logical_volume = logical_volume;
8733 	scsi_cmd->partition = partition;
8734 	scsi_ulto4b(length, scsi_cmd->length);
8735 
8736 	cam_fill_csio(csio,
8737 		      retries,
8738 		      cbfcnp,
8739 		      /*flags*/CAM_DIR_OUT,
8740 		      tag_action,
8741 		      /*data_ptr*/data_ptr,
8742 		      /*dxfer_len*/length,
8743 		      sense_len,
8744 		      sizeof(*scsi_cmd),
8745 		      timeout);
8746 }
8747 
8748 void
8749 scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries,
8750 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
8751 			   uint8_t tag_action, int service_action,
8752 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8753 			   int timeout)
8754 {
8755 	struct scsi_per_res_in *scsi_cmd;
8756 
8757 	scsi_cmd = (struct scsi_per_res_in *)&csio->cdb_io.cdb_bytes;
8758 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8759 
8760 	scsi_cmd->opcode = PERSISTENT_RES_IN;
8761 	scsi_cmd->action = service_action;
8762 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
8763 
8764 	cam_fill_csio(csio,
8765 		      retries,
8766 		      cbfcnp,
8767 		      /*flags*/CAM_DIR_IN,
8768 		      tag_action,
8769 		      data_ptr,
8770 		      dxfer_len,
8771 		      sense_len,
8772 		      sizeof(*scsi_cmd),
8773 		      timeout);
8774 }
8775 
8776 void
8777 scsi_persistent_reserve_out(struct ccb_scsiio *csio, uint32_t retries,
8778 			    void (*cbfcnp)(struct cam_periph *, union ccb *),
8779 			    uint8_t tag_action, int service_action,
8780 			    int scope, int res_type, uint8_t *data_ptr,
8781 			    uint32_t dxfer_len, int sense_len, int timeout)
8782 {
8783 	struct scsi_per_res_out *scsi_cmd;
8784 
8785 	scsi_cmd = (struct scsi_per_res_out *)&csio->cdb_io.cdb_bytes;
8786 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8787 
8788 	scsi_cmd->opcode = PERSISTENT_RES_OUT;
8789 	scsi_cmd->action = service_action;
8790 	scsi_cmd->scope_type = scope | res_type;
8791 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8792 
8793 	cam_fill_csio(csio,
8794 		      retries,
8795 		      cbfcnp,
8796 		      /*flags*/CAM_DIR_OUT,
8797 		      tag_action,
8798 		      /*data_ptr*/data_ptr,
8799 		      /*dxfer_len*/dxfer_len,
8800 		      sense_len,
8801 		      sizeof(*scsi_cmd),
8802 		      timeout);
8803 }
8804 
8805 void
8806 scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries,
8807 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
8808 			  uint8_t tag_action, uint32_t security_protocol,
8809 			  uint32_t security_protocol_specific, int byte4,
8810 			  uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8811 			  int timeout)
8812 {
8813 	struct scsi_security_protocol_in *scsi_cmd;
8814 
8815 	scsi_cmd = (struct scsi_security_protocol_in *)&csio->cdb_io.cdb_bytes;
8816 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8817 
8818 	scsi_cmd->opcode = SECURITY_PROTOCOL_IN;
8819 
8820 	scsi_cmd->security_protocol = security_protocol;
8821 	scsi_ulto2b(security_protocol_specific,
8822 		    scsi_cmd->security_protocol_specific);
8823 	scsi_cmd->byte4 = byte4;
8824 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8825 
8826 	cam_fill_csio(csio,
8827 		      retries,
8828 		      cbfcnp,
8829 		      /*flags*/CAM_DIR_IN,
8830 		      tag_action,
8831 		      data_ptr,
8832 		      dxfer_len,
8833 		      sense_len,
8834 		      sizeof(*scsi_cmd),
8835 		      timeout);
8836 }
8837 
8838 void
8839 scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries,
8840 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
8841 			   uint8_t tag_action, uint32_t security_protocol,
8842 			   uint32_t security_protocol_specific, int byte4,
8843 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8844 			   int timeout)
8845 {
8846 	struct scsi_security_protocol_out *scsi_cmd;
8847 
8848 	scsi_cmd = (struct scsi_security_protocol_out *)&csio->cdb_io.cdb_bytes;
8849 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8850 
8851 	scsi_cmd->opcode = SECURITY_PROTOCOL_OUT;
8852 
8853 	scsi_cmd->security_protocol = security_protocol;
8854 	scsi_ulto2b(security_protocol_specific,
8855 		    scsi_cmd->security_protocol_specific);
8856 	scsi_cmd->byte4 = byte4;
8857 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8858 
8859 	cam_fill_csio(csio,
8860 		      retries,
8861 		      cbfcnp,
8862 		      /*flags*/CAM_DIR_OUT,
8863 		      tag_action,
8864 		      data_ptr,
8865 		      dxfer_len,
8866 		      sense_len,
8867 		      sizeof(*scsi_cmd),
8868 		      timeout);
8869 }
8870 
8871 void
8872 scsi_report_supported_opcodes(struct ccb_scsiio *csio, uint32_t retries,
8873 			      void (*cbfcnp)(struct cam_periph *, union ccb *),
8874 			      uint8_t tag_action, int options, int req_opcode,
8875 			      int req_service_action, uint8_t *data_ptr,
8876 			      uint32_t dxfer_len, int sense_len, int timeout)
8877 {
8878 	struct scsi_report_supported_opcodes *scsi_cmd;
8879 
8880 	scsi_cmd = (struct scsi_report_supported_opcodes *)
8881 	    &csio->cdb_io.cdb_bytes;
8882 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8883 
8884 	scsi_cmd->opcode = MAINTENANCE_IN;
8885 	scsi_cmd->service_action = REPORT_SUPPORTED_OPERATION_CODES;
8886 	scsi_cmd->options = options;
8887 	scsi_cmd->requested_opcode = req_opcode;
8888 	scsi_ulto2b(req_service_action, scsi_cmd->requested_service_action);
8889 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8890 
8891 	cam_fill_csio(csio,
8892 		      retries,
8893 		      cbfcnp,
8894 		      /*flags*/CAM_DIR_IN,
8895 		      tag_action,
8896 		      data_ptr,
8897 		      dxfer_len,
8898 		      sense_len,
8899 		      sizeof(*scsi_cmd),
8900 		      timeout);
8901 }
8902 
8903 /*
8904  * Try make as good a match as possible with
8905  * available sub drivers
8906  */
8907 int
8908 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8909 {
8910 	struct scsi_inquiry_pattern *entry;
8911 	struct scsi_inquiry_data *inq;
8912 
8913 	entry = (struct scsi_inquiry_pattern *)table_entry;
8914 	inq = (struct scsi_inquiry_data *)inqbuffer;
8915 
8916 	if (((SID_TYPE(inq) == entry->type)
8917 	  || (entry->type == T_ANY))
8918 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8919 				   : entry->media_type & SIP_MEDIA_FIXED)
8920 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8921 	 && (cam_strmatch(inq->product, entry->product,
8922 			  sizeof(inq->product)) == 0)
8923 	 && (cam_strmatch(inq->revision, entry->revision,
8924 			  sizeof(inq->revision)) == 0)) {
8925 		return (0);
8926 	}
8927         return (-1);
8928 }
8929 
8930 /*
8931  * Try make as good a match as possible with
8932  * available sub drivers
8933  */
8934 int
8935 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8936 {
8937 	struct scsi_static_inquiry_pattern *entry;
8938 	struct scsi_inquiry_data *inq;
8939 
8940 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
8941 	inq = (struct scsi_inquiry_data *)inqbuffer;
8942 
8943 	if (((SID_TYPE(inq) == entry->type)
8944 	  || (entry->type == T_ANY))
8945 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8946 				   : entry->media_type & SIP_MEDIA_FIXED)
8947 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8948 	 && (cam_strmatch(inq->product, entry->product,
8949 			  sizeof(inq->product)) == 0)
8950 	 && (cam_strmatch(inq->revision, entry->revision,
8951 			  sizeof(inq->revision)) == 0)) {
8952 		return (0);
8953 	}
8954         return (-1);
8955 }
8956 
8957 /**
8958  * Compare two buffers of vpd device descriptors for a match.
8959  *
8960  * \param lhs      Pointer to first buffer of descriptors to compare.
8961  * \param lhs_len  The length of the first buffer.
8962  * \param rhs	   Pointer to second buffer of descriptors to compare.
8963  * \param rhs_len  The length of the second buffer.
8964  *
8965  * \return  0 on a match, -1 otherwise.
8966  *
8967  * Treat rhs and lhs as arrays of vpd device id descriptors.  Walk lhs matching
8968  * against each element in rhs until all data are exhausted or we have found
8969  * a match.
8970  */
8971 int
8972 scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
8973 {
8974 	struct scsi_vpd_id_descriptor *lhs_id;
8975 	struct scsi_vpd_id_descriptor *lhs_last;
8976 	struct scsi_vpd_id_descriptor *rhs_last;
8977 	uint8_t *lhs_end;
8978 	uint8_t *rhs_end;
8979 
8980 	lhs_end = lhs + lhs_len;
8981 	rhs_end = rhs + rhs_len;
8982 
8983 	/*
8984 	 * rhs_last and lhs_last are the last posible position of a valid
8985 	 * descriptor assuming it had a zero length identifier.  We use
8986 	 * these variables to insure we can safely dereference the length
8987 	 * field in our loop termination tests.
8988 	 */
8989 	lhs_last = (struct scsi_vpd_id_descriptor *)
8990 	    (lhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
8991 	rhs_last = (struct scsi_vpd_id_descriptor *)
8992 	    (rhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
8993 
8994 	lhs_id = (struct scsi_vpd_id_descriptor *)lhs;
8995 	while (lhs_id <= lhs_last
8996 	    && (lhs_id->identifier + lhs_id->length) <= lhs_end) {
8997 		struct scsi_vpd_id_descriptor *rhs_id;
8998 
8999 		rhs_id = (struct scsi_vpd_id_descriptor *)rhs;
9000 		while (rhs_id <= rhs_last
9001 		    && (rhs_id->identifier + rhs_id->length) <= rhs_end) {
9002 
9003 			if ((rhs_id->id_type &
9004 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK)) ==
9005 			    (lhs_id->id_type &
9006 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK))
9007 			 && rhs_id->length == lhs_id->length
9008 			 && memcmp(rhs_id->identifier, lhs_id->identifier,
9009 				   rhs_id->length) == 0)
9010 				return (0);
9011 
9012 			rhs_id = (struct scsi_vpd_id_descriptor *)
9013 			   (rhs_id->identifier + rhs_id->length);
9014 		}
9015 		lhs_id = (struct scsi_vpd_id_descriptor *)
9016 		   (lhs_id->identifier + lhs_id->length);
9017 	}
9018 	return (-1);
9019 }
9020 
9021 #ifdef _KERNEL
9022 int
9023 scsi_vpd_supported_page(struct cam_periph *periph, uint8_t page_id)
9024 {
9025 	struct cam_ed *device;
9026 	struct scsi_vpd_supported_pages *vpds;
9027 	int i, num_pages;
9028 
9029 	device = periph->path->device;
9030 	vpds = (struct scsi_vpd_supported_pages *)device->supported_vpds;
9031 
9032 	if (vpds != NULL) {
9033 		num_pages = device->supported_vpds_len -
9034 		    SVPD_SUPPORTED_PAGES_HDR_LEN;
9035 		for (i = 0; i < num_pages; i++) {
9036 			if (vpds->page_list[i] == page_id)
9037 				return (1);
9038 		}
9039 	}
9040 
9041 	return (0);
9042 }
9043 
9044 static void
9045 init_scsi_delay(void)
9046 {
9047 	int delay;
9048 
9049 	delay = SCSI_DELAY;
9050 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
9051 
9052 	if (set_scsi_delay(delay) != 0) {
9053 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
9054 		set_scsi_delay(SCSI_DELAY);
9055 	}
9056 }
9057 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
9058 
9059 static int
9060 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
9061 {
9062 	int error, delay;
9063 
9064 	delay = scsi_delay;
9065 	error = sysctl_handle_int(oidp, &delay, 0, req);
9066 	if (error != 0 || req->newptr == NULL)
9067 		return (error);
9068 	return (set_scsi_delay(delay));
9069 }
9070 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
9071     0, 0, sysctl_scsi_delay, "I",
9072     "Delay to allow devices to settle after a SCSI bus reset (ms)");
9073 
9074 static int
9075 set_scsi_delay(int delay)
9076 {
9077 	/*
9078          * If someone sets this to 0, we assume that they want the
9079          * minimum allowable bus settle delay.
9080 	 */
9081 	if (delay == 0) {
9082 		printf("cam: using minimum scsi_delay (%dms)\n",
9083 		    SCSI_MIN_DELAY);
9084 		delay = SCSI_MIN_DELAY;
9085 	}
9086 	if (delay < SCSI_MIN_DELAY)
9087 		return (EINVAL);
9088 	scsi_delay = delay;
9089 	return (0);
9090 }
9091 #endif /* _KERNEL */
9092