xref: /freebsd/sys/cam/scsi/scsi_all.c (revision eaa797943eeac5614edfdc8f6309f332343c3dd2)
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_NOP,
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_NOP,
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 void
4656 scsi_sense_ata_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4657 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4658 			 struct scsi_inquiry_data *inq_data,
4659 			 struct scsi_sense_desc_header *header)
4660 {
4661 	struct scsi_sense_ata_ret_desc *res;
4662 
4663 	res = (struct scsi_sense_ata_ret_desc *)header;
4664 
4665 	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s), ",
4666 	    res->status,
4667 	    (res->status & 0x80) ? "BSY " : "",
4668 	    (res->status & 0x40) ? "DRDY " : "",
4669 	    (res->status & 0x20) ? "DF " : "",
4670 	    (res->status & 0x10) ? "SERV " : "",
4671 	    (res->status & 0x08) ? "DRQ " : "",
4672 	    (res->status & 0x04) ? "CORR " : "",
4673 	    (res->status & 0x02) ? "IDX " : "",
4674 	    (res->status & 0x01) ? "ERR" : "");
4675 	if (res->status & 1) {
4676 	    sbuf_printf(sb, "error: %02x (%s%s%s%s%s%s%s%s), ",
4677 		res->error,
4678 		(res->error & 0x80) ? "ICRC " : "",
4679 		(res->error & 0x40) ? "UNC " : "",
4680 		(res->error & 0x20) ? "MC " : "",
4681 		(res->error & 0x10) ? "IDNF " : "",
4682 		(res->error & 0x08) ? "MCR " : "",
4683 		(res->error & 0x04) ? "ABRT " : "",
4684 		(res->error & 0x02) ? "NM " : "",
4685 		(res->error & 0x01) ? "ILI" : "");
4686 	}
4687 
4688 	if (res->flags & SSD_DESC_ATA_FLAG_EXTEND) {
4689 		sbuf_printf(sb, "count: %02x%02x, ",
4690 		    res->count_15_8, res->count_7_0);
4691 		sbuf_printf(sb, "LBA: %02x%02x%02x%02x%02x%02x, ",
4692 		    res->lba_47_40, res->lba_39_32, res->lba_31_24,
4693 		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4694 	} else {
4695 		sbuf_printf(sb, "count: %02x, ", res->count_7_0);
4696 		sbuf_printf(sb, "LBA: %02x%02x%02x, ",
4697 		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4698 	}
4699 	sbuf_printf(sb, "device: %02x, ", res->device);
4700 }
4701 
4702 /*
4703  * Generic sense descriptor printing routine.  This is used when we have
4704  * not yet implemented a specific printing routine for this descriptor.
4705  */
4706 void
4707 scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4708 			u_int sense_len, uint8_t *cdb, int cdb_len,
4709 			struct scsi_inquiry_data *inq_data,
4710 			struct scsi_sense_desc_header *header)
4711 {
4712 	int i;
4713 	uint8_t *buf_ptr;
4714 
4715 	sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4716 
4717 	buf_ptr = (uint8_t *)&header[1];
4718 
4719 	for (i = 0; i < header->length; i++, buf_ptr++)
4720 		sbuf_printf(sb, " %02x", *buf_ptr);
4721 }
4722 
4723 /*
4724  * Keep this list in numeric order.  This speeds the array traversal.
4725  */
4726 struct scsi_sense_desc_printer {
4727 	uint8_t desc_type;
4728 	/*
4729 	 * The function arguments here are the superset of what is needed
4730 	 * to print out various different descriptors.  Command and
4731 	 * information descriptors need inquiry data and command type.
4732 	 * Sense key specific descriptors need the sense key.
4733 	 *
4734 	 * The sense, cdb, and inquiry data arguments may be NULL, but the
4735 	 * information printed may not be fully decoded as a result.
4736 	 */
4737 	void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4738 			   u_int sense_len, uint8_t *cdb, int cdb_len,
4739 			   struct scsi_inquiry_data *inq_data,
4740 			   struct scsi_sense_desc_header *header);
4741 } scsi_sense_printers[] = {
4742 	{SSD_DESC_INFO, scsi_sense_info_sbuf},
4743 	{SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4744 	{SSD_DESC_SKS, scsi_sense_sks_sbuf},
4745 	{SSD_DESC_FRU, scsi_sense_fru_sbuf},
4746 	{SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4747 	{SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4748 	{SSD_DESC_ATA, scsi_sense_ata_sbuf},
4749 	{SSD_DESC_PROGRESS, scsi_sense_progress_sbuf}
4750 };
4751 
4752 void
4753 scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4754 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4755 		     struct scsi_inquiry_data *inq_data,
4756 		     struct scsi_sense_desc_header *header)
4757 {
4758 	u_int i;
4759 
4760 	for (i = 0; i < nitems(scsi_sense_printers); i++) {
4761 		struct scsi_sense_desc_printer *printer;
4762 
4763 		printer = &scsi_sense_printers[i];
4764 
4765 		/*
4766 		 * The list is sorted, so quit if we've passed our
4767 		 * descriptor number.
4768 		 */
4769 		if (printer->desc_type > header->desc_type)
4770 			break;
4771 
4772 		if (printer->desc_type != header->desc_type)
4773 			continue;
4774 
4775 		printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4776 				    inq_data, header);
4777 
4778 		return;
4779 	}
4780 
4781 	/*
4782 	 * No specific printing routine, so use the generic routine.
4783 	 */
4784 	scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4785 				inq_data, header);
4786 }
4787 
4788 scsi_sense_data_type
4789 scsi_sense_type(struct scsi_sense_data *sense_data)
4790 {
4791 	switch (sense_data->error_code & SSD_ERRCODE) {
4792 	case SSD_DESC_CURRENT_ERROR:
4793 	case SSD_DESC_DEFERRED_ERROR:
4794 		return (SSD_TYPE_DESC);
4795 		break;
4796 	case SSD_CURRENT_ERROR:
4797 	case SSD_DEFERRED_ERROR:
4798 		return (SSD_TYPE_FIXED);
4799 		break;
4800 	default:
4801 		break;
4802 	}
4803 
4804 	return (SSD_TYPE_NONE);
4805 }
4806 
4807 struct scsi_print_sense_info {
4808 	struct sbuf *sb;
4809 	char *path_str;
4810 	uint8_t *cdb;
4811 	int cdb_len;
4812 	struct scsi_inquiry_data *inq_data;
4813 };
4814 
4815 static int
4816 scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4817 		     struct scsi_sense_desc_header *header, void *arg)
4818 {
4819 	struct scsi_print_sense_info *print_info;
4820 
4821 	print_info = (struct scsi_print_sense_info *)arg;
4822 
4823 	switch (header->desc_type) {
4824 	case SSD_DESC_INFO:
4825 	case SSD_DESC_FRU:
4826 	case SSD_DESC_COMMAND:
4827 	case SSD_DESC_SKS:
4828 	case SSD_DESC_BLOCK:
4829 	case SSD_DESC_STREAM:
4830 		/*
4831 		 * We have already printed these descriptors, if they are
4832 		 * present.
4833 		 */
4834 		break;
4835 	default: {
4836 		sbuf_printf(print_info->sb, "%s", print_info->path_str);
4837 		scsi_sense_desc_sbuf(print_info->sb,
4838 				     (struct scsi_sense_data *)sense, sense_len,
4839 				     print_info->cdb, print_info->cdb_len,
4840 				     print_info->inq_data, header);
4841 		sbuf_printf(print_info->sb, "\n");
4842 		break;
4843 	}
4844 	}
4845 
4846 	/*
4847 	 * Tell the iterator that we want to see more descriptors if they
4848 	 * are present.
4849 	 */
4850 	return (0);
4851 }
4852 
4853 void
4854 scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4855 		     struct sbuf *sb, char *path_str,
4856 		     struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4857 		     int cdb_len)
4858 {
4859 	int error_code, sense_key, asc, ascq;
4860 
4861 	sbuf_cat(sb, path_str);
4862 
4863 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4864 			       &asc, &ascq, /*show_errors*/ 1);
4865 
4866 	sbuf_printf(sb, "SCSI sense: ");
4867 	switch (error_code) {
4868 	case SSD_DEFERRED_ERROR:
4869 	case SSD_DESC_DEFERRED_ERROR:
4870 		sbuf_printf(sb, "Deferred error: ");
4871 
4872 		/* FALLTHROUGH */
4873 	case SSD_CURRENT_ERROR:
4874 	case SSD_DESC_CURRENT_ERROR:
4875 	{
4876 		struct scsi_sense_data_desc *desc_sense;
4877 		struct scsi_print_sense_info print_info;
4878 		const char *sense_key_desc;
4879 		const char *asc_desc;
4880 		uint8_t sks[3];
4881 		uint64_t val;
4882 		int info_valid;
4883 
4884 		/*
4885 		 * Get descriptions for the sense key, ASC, and ASCQ.  If
4886 		 * these aren't present in the sense data (i.e. the sense
4887 		 * data isn't long enough), the -1 values that
4888 		 * scsi_extract_sense_len() returns will yield default
4889 		 * or error descriptions.
4890 		 */
4891 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
4892 				&sense_key_desc, &asc_desc);
4893 
4894 		/*
4895 		 * We first print the sense key and ASC/ASCQ.
4896 		 */
4897 		sbuf_cat(sb, sense_key_desc);
4898 		sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
4899 
4900 		/*
4901 		 * Get the info field if it is valid.
4902 		 */
4903 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
4904 					&val, NULL) == 0)
4905 			info_valid = 1;
4906 		else
4907 			info_valid = 0;
4908 
4909 		if (info_valid != 0) {
4910 			uint8_t bits;
4911 
4912 			/*
4913 			 * Determine whether we have any block or stream
4914 			 * device-specific information.
4915 			 */
4916 			if (scsi_get_block_info(sense, sense_len, inq_data,
4917 						&bits) == 0) {
4918 				sbuf_cat(sb, path_str);
4919 				scsi_block_sbuf(sb, bits, val);
4920 				sbuf_printf(sb, "\n");
4921 			} else if (scsi_get_stream_info(sense, sense_len,
4922 							inq_data, &bits) == 0) {
4923 				sbuf_cat(sb, path_str);
4924 				scsi_stream_sbuf(sb, bits, val);
4925 				sbuf_printf(sb, "\n");
4926 			} else if (val != 0) {
4927 				/*
4928 				 * The information field can be valid but 0.
4929 				 * If the block or stream bits aren't set,
4930 				 * and this is 0, it isn't terribly useful
4931 				 * to print it out.
4932 				 */
4933 				sbuf_cat(sb, path_str);
4934 				scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
4935 				sbuf_printf(sb, "\n");
4936 			}
4937 		}
4938 
4939 		/*
4940 		 * Print the FRU.
4941 		 */
4942 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
4943 					&val, NULL) == 0) {
4944 			sbuf_cat(sb, path_str);
4945 			scsi_fru_sbuf(sb, val);
4946 			sbuf_printf(sb, "\n");
4947 		}
4948 
4949 		/*
4950 		 * Print any command-specific information.
4951 		 */
4952 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
4953 					&val, NULL) == 0) {
4954 			sbuf_cat(sb, path_str);
4955 			scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
4956 			sbuf_printf(sb, "\n");
4957 		}
4958 
4959 		/*
4960 		 * Print out any sense-key-specific information.
4961 		 */
4962 		if (scsi_get_sks(sense, sense_len, sks) == 0) {
4963 			sbuf_cat(sb, path_str);
4964 			scsi_sks_sbuf(sb, sense_key, sks);
4965 			sbuf_printf(sb, "\n");
4966 		}
4967 
4968 		/*
4969 		 * If this is fixed sense, we're done.  If we have
4970 		 * descriptor sense, we might have more information
4971 		 * available.
4972 		 */
4973 		if (scsi_sense_type(sense) != SSD_TYPE_DESC)
4974 			break;
4975 
4976 		desc_sense = (struct scsi_sense_data_desc *)sense;
4977 
4978 		print_info.sb = sb;
4979 		print_info.path_str = path_str;
4980 		print_info.cdb = cdb;
4981 		print_info.cdb_len = cdb_len;
4982 		print_info.inq_data = inq_data;
4983 
4984 		/*
4985 		 * Print any sense descriptors that we have not already printed.
4986 		 */
4987 		scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
4988 				  &print_info);
4989 		break;
4990 
4991 	}
4992 	case -1:
4993 		/*
4994 		 * scsi_extract_sense_len() sets values to -1 if the
4995 		 * show_errors flag is set and they aren't present in the
4996 		 * sense data.  This means that sense_len is 0.
4997 		 */
4998 		sbuf_printf(sb, "No sense data present\n");
4999 		break;
5000 	default: {
5001 		sbuf_printf(sb, "Error code 0x%x", error_code);
5002 		if (sense->error_code & SSD_ERRCODE_VALID) {
5003 			struct scsi_sense_data_fixed *fixed_sense;
5004 
5005 			fixed_sense = (struct scsi_sense_data_fixed *)sense;
5006 
5007 			if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
5008 				uint32_t info;
5009 
5010 				info = scsi_4btoul(fixed_sense->info);
5011 
5012 				sbuf_printf(sb, " at block no. %d (decimal)",
5013 					    info);
5014 			}
5015 		}
5016 		sbuf_printf(sb, "\n");
5017 		break;
5018 	}
5019 	}
5020 }
5021 
5022 /*
5023  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
5024  */
5025 #ifdef _KERNEL
5026 int
5027 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
5028 		scsi_sense_string_flags flags)
5029 #else /* !_KERNEL */
5030 int
5031 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
5032 		struct sbuf *sb, scsi_sense_string_flags flags)
5033 #endif /* _KERNEL/!_KERNEL */
5034 {
5035 	struct	  scsi_sense_data *sense;
5036 	struct	  scsi_inquiry_data *inq_data;
5037 #ifdef _KERNEL
5038 	struct	  ccb_getdev *cgd;
5039 #endif /* _KERNEL */
5040 	char	  path_str[64];
5041 	uint8_t	  *cdb;
5042 
5043 #ifndef _KERNEL
5044 	if (device == NULL)
5045 		return(-1);
5046 #endif /* !_KERNEL */
5047 	if ((csio == NULL) || (sb == NULL))
5048 		return(-1);
5049 
5050 	/*
5051 	 * If the CDB is a physical address, we can't deal with it..
5052 	 */
5053 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
5054 		flags &= ~SSS_FLAG_PRINT_COMMAND;
5055 
5056 #ifdef _KERNEL
5057 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
5058 #else /* !_KERNEL */
5059 	cam_path_string(device, path_str, sizeof(path_str));
5060 #endif /* _KERNEL/!_KERNEL */
5061 
5062 #ifdef _KERNEL
5063 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
5064 		return(-1);
5065 	/*
5066 	 * Get the device information.
5067 	 */
5068 	xpt_setup_ccb(&cgd->ccb_h,
5069 		      csio->ccb_h.path,
5070 		      CAM_PRIORITY_NORMAL);
5071 	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
5072 	xpt_action((union ccb *)cgd);
5073 
5074 	/*
5075 	 * If the device is unconfigured, just pretend that it is a hard
5076 	 * drive.  scsi_op_desc() needs this.
5077 	 */
5078 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
5079 		cgd->inq_data.device = T_DIRECT;
5080 
5081 	inq_data = &cgd->inq_data;
5082 
5083 #else /* !_KERNEL */
5084 
5085 	inq_data = &device->inq_data;
5086 
5087 #endif /* _KERNEL/!_KERNEL */
5088 
5089 	sense = NULL;
5090 
5091 	if (flags & SSS_FLAG_PRINT_COMMAND) {
5092 
5093 		sbuf_cat(sb, path_str);
5094 
5095 #ifdef _KERNEL
5096 		scsi_command_string(csio, sb);
5097 #else /* !_KERNEL */
5098 		scsi_command_string(device, csio, sb);
5099 #endif /* _KERNEL/!_KERNEL */
5100 		sbuf_printf(sb, "\n");
5101 	}
5102 
5103 	/*
5104 	 * If the sense data is a physical pointer, forget it.
5105 	 */
5106 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
5107 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5108 #ifdef _KERNEL
5109 			xpt_free_ccb((union ccb*)cgd);
5110 #endif /* _KERNEL/!_KERNEL */
5111 			return(-1);
5112 		} else {
5113 			/*
5114 			 * bcopy the pointer to avoid unaligned access
5115 			 * errors on finicky architectures.  We don't
5116 			 * ensure that the sense data is pointer aligned.
5117 			 */
5118 			bcopy(&csio->sense_data, &sense,
5119 			      sizeof(struct scsi_sense_data *));
5120 		}
5121 	} else {
5122 		/*
5123 		 * If the physical sense flag is set, but the sense pointer
5124 		 * is not also set, we assume that the user is an idiot and
5125 		 * return.  (Well, okay, it could be that somehow, the
5126 		 * entire csio is physical, but we would have probably core
5127 		 * dumped on one of the bogus pointer deferences above
5128 		 * already.)
5129 		 */
5130 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5131 #ifdef _KERNEL
5132 			xpt_free_ccb((union ccb*)cgd);
5133 #endif /* _KERNEL/!_KERNEL */
5134 			return(-1);
5135 		} else
5136 			sense = &csio->sense_data;
5137 	}
5138 
5139 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
5140 		cdb = csio->cdb_io.cdb_ptr;
5141 	else
5142 		cdb = csio->cdb_io.cdb_bytes;
5143 
5144 	scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
5145 			     path_str, inq_data, cdb, csio->cdb_len);
5146 
5147 #ifdef _KERNEL
5148 	xpt_free_ccb((union ccb*)cgd);
5149 #endif /* _KERNEL/!_KERNEL */
5150 	return(0);
5151 }
5152 
5153 
5154 
5155 #ifdef _KERNEL
5156 char *
5157 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
5158 #else /* !_KERNEL */
5159 char *
5160 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
5161 		  char *str, int str_len)
5162 #endif /* _KERNEL/!_KERNEL */
5163 {
5164 	struct sbuf sb;
5165 
5166 	sbuf_new(&sb, str, str_len, 0);
5167 
5168 #ifdef _KERNEL
5169 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5170 #else /* !_KERNEL */
5171 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5172 #endif /* _KERNEL/!_KERNEL */
5173 
5174 	sbuf_finish(&sb);
5175 
5176 	return(sbuf_data(&sb));
5177 }
5178 
5179 #ifdef _KERNEL
5180 void
5181 scsi_sense_print(struct ccb_scsiio *csio)
5182 {
5183 	struct sbuf sb;
5184 	char str[512];
5185 
5186 	sbuf_new(&sb, str, sizeof(str), 0);
5187 
5188 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5189 
5190 	sbuf_finish(&sb);
5191 
5192 	printf("%s", sbuf_data(&sb));
5193 }
5194 
5195 #else /* !_KERNEL */
5196 void
5197 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
5198 		 FILE *ofile)
5199 {
5200 	struct sbuf sb;
5201 	char str[512];
5202 
5203 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
5204 		return;
5205 
5206 	sbuf_new(&sb, str, sizeof(str), 0);
5207 
5208 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5209 
5210 	sbuf_finish(&sb);
5211 
5212 	fprintf(ofile, "%s", sbuf_data(&sb));
5213 }
5214 
5215 #endif /* _KERNEL/!_KERNEL */
5216 
5217 /*
5218  * Extract basic sense information.  This is backward-compatible with the
5219  * previous implementation.  For new implementations,
5220  * scsi_extract_sense_len() is recommended.
5221  */
5222 void
5223 scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
5224 		   int *sense_key, int *asc, int *ascq)
5225 {
5226 	scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
5227 			       sense_key, asc, ascq, /*show_errors*/ 0);
5228 }
5229 
5230 /*
5231  * Extract basic sense information from SCSI I/O CCB structure.
5232  */
5233 int
5234 scsi_extract_sense_ccb(union ccb *ccb,
5235     int *error_code, int *sense_key, int *asc, int *ascq)
5236 {
5237 	struct scsi_sense_data *sense_data;
5238 
5239 	/* Make sure there are some sense data we can access. */
5240 	if (ccb->ccb_h.func_code != XPT_SCSI_IO ||
5241 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
5242 	    (ccb->csio.scsi_status != SCSI_STATUS_CHECK_COND) ||
5243 	    (ccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0 ||
5244 	    (ccb->ccb_h.flags & CAM_SENSE_PHYS))
5245 		return (0);
5246 
5247 	if (ccb->ccb_h.flags & CAM_SENSE_PTR)
5248 		bcopy(&ccb->csio.sense_data, &sense_data,
5249 		    sizeof(struct scsi_sense_data *));
5250 	else
5251 		sense_data = &ccb->csio.sense_data;
5252 	scsi_extract_sense_len(sense_data,
5253 	    ccb->csio.sense_len - ccb->csio.sense_resid,
5254 	    error_code, sense_key, asc, ascq, 1);
5255 	if (*error_code == -1)
5256 		return (0);
5257 	return (1);
5258 }
5259 
5260 /*
5261  * Extract basic sense information.  If show_errors is set, sense values
5262  * will be set to -1 if they are not present.
5263  */
5264 void
5265 scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
5266 		       int *error_code, int *sense_key, int *asc, int *ascq,
5267 		       int show_errors)
5268 {
5269 	/*
5270 	 * If we have no length, we have no sense.
5271 	 */
5272 	if (sense_len == 0) {
5273 		if (show_errors == 0) {
5274 			*error_code = 0;
5275 			*sense_key = 0;
5276 			*asc = 0;
5277 			*ascq = 0;
5278 		} else {
5279 			*error_code = -1;
5280 			*sense_key = -1;
5281 			*asc = -1;
5282 			*ascq = -1;
5283 		}
5284 		return;
5285 	}
5286 
5287 	*error_code = sense_data->error_code & SSD_ERRCODE;
5288 
5289 	switch (*error_code) {
5290 	case SSD_DESC_CURRENT_ERROR:
5291 	case SSD_DESC_DEFERRED_ERROR: {
5292 		struct scsi_sense_data_desc *sense;
5293 
5294 		sense = (struct scsi_sense_data_desc *)sense_data;
5295 
5296 		if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
5297 			*sense_key = sense->sense_key & SSD_KEY;
5298 		else
5299 			*sense_key = (show_errors) ? -1 : 0;
5300 
5301 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
5302 			*asc = sense->add_sense_code;
5303 		else
5304 			*asc = (show_errors) ? -1 : 0;
5305 
5306 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
5307 			*ascq = sense->add_sense_code_qual;
5308 		else
5309 			*ascq = (show_errors) ? -1 : 0;
5310 		break;
5311 	}
5312 	case SSD_CURRENT_ERROR:
5313 	case SSD_DEFERRED_ERROR:
5314 	default: {
5315 		struct scsi_sense_data_fixed *sense;
5316 
5317 		sense = (struct scsi_sense_data_fixed *)sense_data;
5318 
5319 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
5320 			*sense_key = sense->flags & SSD_KEY;
5321 		else
5322 			*sense_key = (show_errors) ? -1 : 0;
5323 
5324 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
5325 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
5326 			*asc = sense->add_sense_code;
5327 		else
5328 			*asc = (show_errors) ? -1 : 0;
5329 
5330 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
5331 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
5332 			*ascq = sense->add_sense_code_qual;
5333 		else
5334 			*ascq = (show_errors) ? -1 : 0;
5335 		break;
5336 	}
5337 	}
5338 }
5339 
5340 int
5341 scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
5342 		   int show_errors)
5343 {
5344 	int error_code, sense_key, asc, ascq;
5345 
5346 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5347 			       &sense_key, &asc, &ascq, show_errors);
5348 
5349 	return (sense_key);
5350 }
5351 
5352 int
5353 scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
5354 	     int show_errors)
5355 {
5356 	int error_code, sense_key, asc, ascq;
5357 
5358 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5359 			       &sense_key, &asc, &ascq, show_errors);
5360 
5361 	return (asc);
5362 }
5363 
5364 int
5365 scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
5366 	      int show_errors)
5367 {
5368 	int error_code, sense_key, asc, ascq;
5369 
5370 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5371 			       &sense_key, &asc, &ascq, show_errors);
5372 
5373 	return (ascq);
5374 }
5375 
5376 /*
5377  * This function currently requires at least 36 bytes, or
5378  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
5379  * function needs more or less data in the future, another length should be
5380  * defined in scsi_all.h to indicate the minimum amount of data necessary
5381  * for this routine to function properly.
5382  */
5383 void
5384 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
5385 {
5386 	u_int8_t type;
5387 	char *dtype, *qtype;
5388 	char vendor[16], product[48], revision[16], rstr[12];
5389 
5390 	type = SID_TYPE(inq_data);
5391 
5392 	/*
5393 	 * Figure out basic device type and qualifier.
5394 	 */
5395 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
5396 		qtype = " (vendor-unique qualifier)";
5397 	} else {
5398 		switch (SID_QUAL(inq_data)) {
5399 		case SID_QUAL_LU_CONNECTED:
5400 			qtype = "";
5401 			break;
5402 
5403 		case SID_QUAL_LU_OFFLINE:
5404 			qtype = " (offline)";
5405 			break;
5406 
5407 		case SID_QUAL_RSVD:
5408 			qtype = " (reserved qualifier)";
5409 			break;
5410 		default:
5411 		case SID_QUAL_BAD_LU:
5412 			qtype = " (LUN not supported)";
5413 			break;
5414 		}
5415 	}
5416 
5417 	switch (type) {
5418 	case T_DIRECT:
5419 		dtype = "Direct Access";
5420 		break;
5421 	case T_SEQUENTIAL:
5422 		dtype = "Sequential Access";
5423 		break;
5424 	case T_PRINTER:
5425 		dtype = "Printer";
5426 		break;
5427 	case T_PROCESSOR:
5428 		dtype = "Processor";
5429 		break;
5430 	case T_WORM:
5431 		dtype = "WORM";
5432 		break;
5433 	case T_CDROM:
5434 		dtype = "CD-ROM";
5435 		break;
5436 	case T_SCANNER:
5437 		dtype = "Scanner";
5438 		break;
5439 	case T_OPTICAL:
5440 		dtype = "Optical";
5441 		break;
5442 	case T_CHANGER:
5443 		dtype = "Changer";
5444 		break;
5445 	case T_COMM:
5446 		dtype = "Communication";
5447 		break;
5448 	case T_STORARRAY:
5449 		dtype = "Storage Array";
5450 		break;
5451 	case T_ENCLOSURE:
5452 		dtype = "Enclosure Services";
5453 		break;
5454 	case T_RBC:
5455 		dtype = "Simplified Direct Access";
5456 		break;
5457 	case T_OCRW:
5458 		dtype = "Optical Card Read/Write";
5459 		break;
5460 	case T_OSD:
5461 		dtype = "Object-Based Storage";
5462 		break;
5463 	case T_ADC:
5464 		dtype = "Automation/Drive Interface";
5465 		break;
5466 	case T_ZBC_HM:
5467 		dtype = "Host Managed Zoned Block";
5468 		break;
5469 	case T_NODEVICE:
5470 		dtype = "Uninstalled";
5471 		break;
5472 	default:
5473 		dtype = "unknown";
5474 		break;
5475 	}
5476 
5477 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5478 		   sizeof(vendor));
5479 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5480 		   sizeof(product));
5481 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5482 		   sizeof(revision));
5483 
5484 	if (SID_ANSI_REV(inq_data) == SCSI_REV_0)
5485 		snprintf(rstr, sizeof(rstr), "SCSI");
5486 	else if (SID_ANSI_REV(inq_data) <= SCSI_REV_SPC) {
5487 		snprintf(rstr, sizeof(rstr), "SCSI-%d",
5488 		    SID_ANSI_REV(inq_data));
5489 	} else {
5490 		snprintf(rstr, sizeof(rstr), "SPC-%d SCSI",
5491 		    SID_ANSI_REV(inq_data) - 2);
5492 	}
5493 	printf("<%s %s %s> %s %s %s device%s\n",
5494 	       vendor, product, revision,
5495 	       SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
5496 	       dtype, rstr, qtype);
5497 }
5498 
5499 void
5500 scsi_print_inquiry_short(struct scsi_inquiry_data *inq_data)
5501 {
5502 	char vendor[16], product[48], revision[16];
5503 
5504 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5505 		   sizeof(vendor));
5506 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5507 		   sizeof(product));
5508 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5509 		   sizeof(revision));
5510 
5511 	printf("<%s %s %s>", vendor, product, revision);
5512 }
5513 
5514 /*
5515  * Table of syncrates that don't follow the "divisible by 4"
5516  * rule. This table will be expanded in future SCSI specs.
5517  */
5518 static struct {
5519 	u_int period_factor;
5520 	u_int period;	/* in 100ths of ns */
5521 } scsi_syncrates[] = {
5522 	{ 0x08, 625 },	/* FAST-160 */
5523 	{ 0x09, 1250 },	/* FAST-80 */
5524 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
5525 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
5526 	{ 0x0c, 5000 }	/* FAST-20 */
5527 };
5528 
5529 /*
5530  * Return the frequency in kHz corresponding to the given
5531  * sync period factor.
5532  */
5533 u_int
5534 scsi_calc_syncsrate(u_int period_factor)
5535 {
5536 	u_int i;
5537 	u_int num_syncrates;
5538 
5539 	/*
5540 	 * It's a bug if period is zero, but if it is anyway, don't
5541 	 * die with a divide fault- instead return something which
5542 	 * 'approximates' async
5543 	 */
5544 	if (period_factor == 0) {
5545 		return (3300);
5546 	}
5547 
5548 	num_syncrates = nitems(scsi_syncrates);
5549 	/* See if the period is in the "exception" table */
5550 	for (i = 0; i < num_syncrates; i++) {
5551 
5552 		if (period_factor == scsi_syncrates[i].period_factor) {
5553 			/* Period in kHz */
5554 			return (100000000 / scsi_syncrates[i].period);
5555 		}
5556 	}
5557 
5558 	/*
5559 	 * Wasn't in the table, so use the standard
5560 	 * 4 times conversion.
5561 	 */
5562 	return (10000000 / (period_factor * 4 * 10));
5563 }
5564 
5565 /*
5566  * Return the SCSI sync parameter that corresponds to
5567  * the passed in period in 10ths of ns.
5568  */
5569 u_int
5570 scsi_calc_syncparam(u_int period)
5571 {
5572 	u_int i;
5573 	u_int num_syncrates;
5574 
5575 	if (period == 0)
5576 		return (~0);	/* Async */
5577 
5578 	/* Adjust for exception table being in 100ths. */
5579 	period *= 10;
5580 	num_syncrates = nitems(scsi_syncrates);
5581 	/* See if the period is in the "exception" table */
5582 	for (i = 0; i < num_syncrates; i++) {
5583 
5584 		if (period <= scsi_syncrates[i].period) {
5585 			/* Period in 100ths of ns */
5586 			return (scsi_syncrates[i].period_factor);
5587 		}
5588 	}
5589 
5590 	/*
5591 	 * Wasn't in the table, so use the standard
5592 	 * 1/4 period in ns conversion.
5593 	 */
5594 	return (period/400);
5595 }
5596 
5597 int
5598 scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
5599 {
5600 	struct scsi_vpd_id_descriptor *descr;
5601 	struct scsi_vpd_id_naa_basic *naa;
5602 
5603 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5604 	naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
5605 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5606 		return 0;
5607 	if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
5608 		return 0;
5609 	if ((naa->naa >> SVPD_ID_NAA_NAA_SHIFT) != SVPD_ID_NAA_IEEE_REG)
5610 		return 0;
5611 	return 1;
5612 }
5613 
5614 int
5615 scsi_devid_is_sas_target(uint8_t *bufp)
5616 {
5617 	struct scsi_vpd_id_descriptor *descr;
5618 
5619 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5620 	if (!scsi_devid_is_naa_ieee_reg(bufp))
5621 		return 0;
5622 	if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
5623 		return 0;
5624 	if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
5625 		return 0;
5626 	return 1;
5627 }
5628 
5629 int
5630 scsi_devid_is_lun_eui64(uint8_t *bufp)
5631 {
5632 	struct scsi_vpd_id_descriptor *descr;
5633 
5634 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5635 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5636 		return 0;
5637 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_EUI64)
5638 		return 0;
5639 	return 1;
5640 }
5641 
5642 int
5643 scsi_devid_is_lun_naa(uint8_t *bufp)
5644 {
5645 	struct scsi_vpd_id_descriptor *descr;
5646 
5647 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5648 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5649 		return 0;
5650 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5651 		return 0;
5652 	return 1;
5653 }
5654 
5655 int
5656 scsi_devid_is_lun_t10(uint8_t *bufp)
5657 {
5658 	struct scsi_vpd_id_descriptor *descr;
5659 
5660 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5661 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5662 		return 0;
5663 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_T10)
5664 		return 0;
5665 	return 1;
5666 }
5667 
5668 int
5669 scsi_devid_is_lun_name(uint8_t *bufp)
5670 {
5671 	struct scsi_vpd_id_descriptor *descr;
5672 
5673 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5674 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5675 		return 0;
5676 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_SCSI_NAME)
5677 		return 0;
5678 	return 1;
5679 }
5680 
5681 int
5682 scsi_devid_is_port_naa(uint8_t *bufp)
5683 {
5684 	struct scsi_vpd_id_descriptor *descr;
5685 
5686 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5687 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_PORT)
5688 		return 0;
5689 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5690 		return 0;
5691 	return 1;
5692 }
5693 
5694 struct scsi_vpd_id_descriptor *
5695 scsi_get_devid_desc(struct scsi_vpd_id_descriptor *desc, uint32_t len,
5696     scsi_devid_checkfn_t ck_fn)
5697 {
5698 	uint8_t *desc_buf_end;
5699 
5700 	desc_buf_end = (uint8_t *)desc + len;
5701 
5702 	for (; desc->identifier <= desc_buf_end &&
5703 	    desc->identifier + desc->length <= desc_buf_end;
5704 	    desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
5705 						    + desc->length)) {
5706 
5707 		if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
5708 			return (desc);
5709 	}
5710 	return (NULL);
5711 }
5712 
5713 struct scsi_vpd_id_descriptor *
5714 scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
5715     scsi_devid_checkfn_t ck_fn)
5716 {
5717 	uint32_t len;
5718 
5719 	if (page_len < sizeof(*id))
5720 		return (NULL);
5721 	len = MIN(scsi_2btoul(id->length), page_len - sizeof(*id));
5722 	return (scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5723 	    id->desc_list, len, ck_fn));
5724 }
5725 
5726 int
5727 scsi_transportid_sbuf(struct sbuf *sb, struct scsi_transportid_header *hdr,
5728 		      uint32_t valid_len)
5729 {
5730 	switch (hdr->format_protocol & SCSI_TRN_PROTO_MASK) {
5731 	case SCSI_PROTO_FC: {
5732 		struct scsi_transportid_fcp *fcp;
5733 		uint64_t n_port_name;
5734 
5735 		fcp = (struct scsi_transportid_fcp *)hdr;
5736 
5737 		n_port_name = scsi_8btou64(fcp->n_port_name);
5738 
5739 		sbuf_printf(sb, "FCP address: 0x%.16jx",(uintmax_t)n_port_name);
5740 		break;
5741 	}
5742 	case SCSI_PROTO_SPI: {
5743 		struct scsi_transportid_spi *spi;
5744 
5745 		spi = (struct scsi_transportid_spi *)hdr;
5746 
5747 		sbuf_printf(sb, "SPI address: %u,%u",
5748 			    scsi_2btoul(spi->scsi_addr),
5749 			    scsi_2btoul(spi->rel_trgt_port_id));
5750 		break;
5751 	}
5752 	case SCSI_PROTO_SSA:
5753 		/*
5754 		 * XXX KDM there is no transport ID defined in SPC-4 for
5755 		 * SSA.
5756 		 */
5757 		break;
5758 	case SCSI_PROTO_1394: {
5759 		struct scsi_transportid_1394 *sbp;
5760 		uint64_t eui64;
5761 
5762 		sbp = (struct scsi_transportid_1394 *)hdr;
5763 
5764 		eui64 = scsi_8btou64(sbp->eui64);
5765 		sbuf_printf(sb, "SBP address: 0x%.16jx", (uintmax_t)eui64);
5766 		break;
5767 	}
5768 	case SCSI_PROTO_RDMA: {
5769 		struct scsi_transportid_rdma *rdma;
5770 		unsigned int i;
5771 
5772 		rdma = (struct scsi_transportid_rdma *)hdr;
5773 
5774 		sbuf_printf(sb, "RDMA address: 0x");
5775 		for (i = 0; i < sizeof(rdma->initiator_port_id); i++)
5776 			sbuf_printf(sb, "%02x", rdma->initiator_port_id[i]);
5777 		break;
5778 	}
5779 	case SCSI_PROTO_ISCSI: {
5780 		uint32_t add_len, i;
5781 		uint8_t *iscsi_name = NULL;
5782 		int nul_found = 0;
5783 
5784 		sbuf_printf(sb, "iSCSI address: ");
5785 		if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5786 		    SCSI_TRN_ISCSI_FORMAT_DEVICE) {
5787 			struct scsi_transportid_iscsi_device *dev;
5788 
5789 			dev = (struct scsi_transportid_iscsi_device *)hdr;
5790 
5791 			/*
5792 			 * Verify how much additional data we really have.
5793 			 */
5794 			add_len = scsi_2btoul(dev->additional_length);
5795 			add_len = MIN(add_len, valid_len -
5796 				__offsetof(struct scsi_transportid_iscsi_device,
5797 					   iscsi_name));
5798 			iscsi_name = &dev->iscsi_name[0];
5799 
5800 		} else if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5801 			    SCSI_TRN_ISCSI_FORMAT_PORT) {
5802 			struct scsi_transportid_iscsi_port *port;
5803 
5804 			port = (struct scsi_transportid_iscsi_port *)hdr;
5805 
5806 			add_len = scsi_2btoul(port->additional_length);
5807 			add_len = MIN(add_len, valid_len -
5808 				__offsetof(struct scsi_transportid_iscsi_port,
5809 					   iscsi_name));
5810 			iscsi_name = &port->iscsi_name[0];
5811 		} else {
5812 			sbuf_printf(sb, "unknown format %x",
5813 				    (hdr->format_protocol &
5814 				     SCSI_TRN_FORMAT_MASK) >>
5815 				     SCSI_TRN_FORMAT_SHIFT);
5816 			break;
5817 		}
5818 		if (add_len == 0) {
5819 			sbuf_printf(sb, "not enough data");
5820 			break;
5821 		}
5822 		/*
5823 		 * This is supposed to be a NUL-terminated ASCII
5824 		 * string, but you never know.  So we're going to
5825 		 * check.  We need to do this because there is no
5826 		 * sbuf equivalent of strncat().
5827 		 */
5828 		for (i = 0; i < add_len; i++) {
5829 			if (iscsi_name[i] == '\0') {
5830 				nul_found = 1;
5831 				break;
5832 			}
5833 		}
5834 		/*
5835 		 * If there is a NUL in the name, we can just use
5836 		 * sbuf_cat().  Otherwise we need to use sbuf_bcat().
5837 		 */
5838 		if (nul_found != 0)
5839 			sbuf_cat(sb, iscsi_name);
5840 		else
5841 			sbuf_bcat(sb, iscsi_name, add_len);
5842 		break;
5843 	}
5844 	case SCSI_PROTO_SAS: {
5845 		struct scsi_transportid_sas *sas;
5846 		uint64_t sas_addr;
5847 
5848 		sas = (struct scsi_transportid_sas *)hdr;
5849 
5850 		sas_addr = scsi_8btou64(sas->sas_address);
5851 		sbuf_printf(sb, "SAS address: 0x%.16jx", (uintmax_t)sas_addr);
5852 		break;
5853 	}
5854 	case SCSI_PROTO_ADITP:
5855 	case SCSI_PROTO_ATA:
5856 	case SCSI_PROTO_UAS:
5857 		/*
5858 		 * No Transport ID format for ADI, ATA or USB is defined in
5859 		 * SPC-4.
5860 		 */
5861 		sbuf_printf(sb, "No known Transport ID format for protocol "
5862 			    "%#x", hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5863 		break;
5864 	case SCSI_PROTO_SOP: {
5865 		struct scsi_transportid_sop *sop;
5866 		struct scsi_sop_routing_id_norm *rid;
5867 
5868 		sop = (struct scsi_transportid_sop *)hdr;
5869 		rid = (struct scsi_sop_routing_id_norm *)sop->routing_id;
5870 
5871 		/*
5872 		 * Note that there is no alternate format specified in SPC-4
5873 		 * for the PCIe routing ID, so we don't really have a way
5874 		 * to know whether the second byte of the routing ID is
5875 		 * a device and function or just a function.  So we just
5876 		 * assume bus,device,function.
5877 		 */
5878 		sbuf_printf(sb, "SOP Routing ID: %u,%u,%u",
5879 			    rid->bus, rid->devfunc >> SCSI_TRN_SOP_DEV_SHIFT,
5880 			    rid->devfunc & SCSI_TRN_SOP_FUNC_NORM_MAX);
5881 		break;
5882 	}
5883 	case SCSI_PROTO_NONE:
5884 	default:
5885 		sbuf_printf(sb, "Unknown protocol %#x",
5886 			    hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5887 		break;
5888 	}
5889 
5890 	return (0);
5891 }
5892 
5893 struct scsi_nv scsi_proto_map[] = {
5894 	{ "fcp", SCSI_PROTO_FC },
5895 	{ "spi", SCSI_PROTO_SPI },
5896 	{ "ssa", SCSI_PROTO_SSA },
5897 	{ "sbp", SCSI_PROTO_1394 },
5898 	{ "1394", SCSI_PROTO_1394 },
5899 	{ "srp", SCSI_PROTO_RDMA },
5900 	{ "rdma", SCSI_PROTO_RDMA },
5901 	{ "iscsi", SCSI_PROTO_ISCSI },
5902 	{ "iqn", SCSI_PROTO_ISCSI },
5903 	{ "sas", SCSI_PROTO_SAS },
5904 	{ "aditp", SCSI_PROTO_ADITP },
5905 	{ "ata", SCSI_PROTO_ATA },
5906 	{ "uas", SCSI_PROTO_UAS },
5907 	{ "usb", SCSI_PROTO_UAS },
5908 	{ "sop", SCSI_PROTO_SOP }
5909 };
5910 
5911 const char *
5912 scsi_nv_to_str(struct scsi_nv *table, int num_table_entries, uint64_t value)
5913 {
5914 	int i;
5915 
5916 	for (i = 0; i < num_table_entries; i++) {
5917 		if (table[i].value == value)
5918 			return (table[i].name);
5919 	}
5920 
5921 	return (NULL);
5922 }
5923 
5924 /*
5925  * Given a name/value table, find a value matching the given name.
5926  * Return values:
5927  *	SCSI_NV_FOUND - match found
5928  *	SCSI_NV_AMBIGUOUS - more than one match, none of them exact
5929  *	SCSI_NV_NOT_FOUND - no match found
5930  */
5931 scsi_nv_status
5932 scsi_get_nv(struct scsi_nv *table, int num_table_entries,
5933 	    char *name, int *table_entry, scsi_nv_flags flags)
5934 {
5935 	int i, num_matches = 0;
5936 
5937 	for (i = 0; i < num_table_entries; i++) {
5938 		size_t table_len, name_len;
5939 
5940 		table_len = strlen(table[i].name);
5941 		name_len = strlen(name);
5942 
5943 		if ((((flags & SCSI_NV_FLAG_IG_CASE) != 0)
5944 		  && (strncasecmp(table[i].name, name, name_len) == 0))
5945 		|| (((flags & SCSI_NV_FLAG_IG_CASE) == 0)
5946 		 && (strncmp(table[i].name, name, name_len) == 0))) {
5947 			*table_entry = i;
5948 
5949 			/*
5950 			 * Check for an exact match.  If we have the same
5951 			 * number of characters in the table as the argument,
5952 			 * and we already know they're the same, we have
5953 			 * an exact match.
5954 		 	 */
5955 			if (table_len == name_len)
5956 				return (SCSI_NV_FOUND);
5957 
5958 			/*
5959 			 * Otherwise, bump up the number of matches.  We'll
5960 			 * see later how many we have.
5961 			 */
5962 			num_matches++;
5963 		}
5964 	}
5965 
5966 	if (num_matches > 1)
5967 		return (SCSI_NV_AMBIGUOUS);
5968 	else if (num_matches == 1)
5969 		return (SCSI_NV_FOUND);
5970 	else
5971 		return (SCSI_NV_NOT_FOUND);
5972 }
5973 
5974 /*
5975  * Parse transport IDs for Fibre Channel, 1394 and SAS.  Since these are
5976  * all 64-bit numbers, the code is similar.
5977  */
5978 int
5979 scsi_parse_transportid_64bit(int proto_id, char *id_str,
5980 			     struct scsi_transportid_header **hdr,
5981 			     unsigned int *alloc_len,
5982 #ifdef _KERNEL
5983 			     struct malloc_type *type, int flags,
5984 #endif
5985 			     char *error_str, int error_str_len)
5986 {
5987 	uint64_t value;
5988 	char *endptr;
5989 	int retval;
5990 	size_t alloc_size;
5991 
5992 	retval = 0;
5993 
5994 	value = strtouq(id_str, &endptr, 0);
5995 	if (*endptr != '\0') {
5996 		if (error_str != NULL) {
5997 			snprintf(error_str, error_str_len, "%s: error "
5998 				 "parsing ID %s, 64-bit number required",
5999 				 __func__, id_str);
6000 		}
6001 		retval = 1;
6002 		goto bailout;
6003 	}
6004 
6005 	switch (proto_id) {
6006 	case SCSI_PROTO_FC:
6007 		alloc_size = sizeof(struct scsi_transportid_fcp);
6008 		break;
6009 	case SCSI_PROTO_1394:
6010 		alloc_size = sizeof(struct scsi_transportid_1394);
6011 		break;
6012 	case SCSI_PROTO_SAS:
6013 		alloc_size = sizeof(struct scsi_transportid_sas);
6014 		break;
6015 	default:
6016 		if (error_str != NULL) {
6017 			snprintf(error_str, error_str_len, "%s: unsupported "
6018 				 "protocol %d", __func__, proto_id);
6019 		}
6020 		retval = 1;
6021 		goto bailout;
6022 		break; /* NOTREACHED */
6023 	}
6024 #ifdef _KERNEL
6025 	*hdr = malloc(alloc_size, type, flags);
6026 #else /* _KERNEL */
6027 	*hdr = malloc(alloc_size);
6028 #endif /*_KERNEL */
6029 	if (*hdr == NULL) {
6030 		if (error_str != NULL) {
6031 			snprintf(error_str, error_str_len, "%s: unable to "
6032 				 "allocate %zu bytes", __func__, alloc_size);
6033 		}
6034 		retval = 1;
6035 		goto bailout;
6036 	}
6037 
6038 	*alloc_len = alloc_size;
6039 
6040 	bzero(*hdr, alloc_size);
6041 
6042 	switch (proto_id) {
6043 	case SCSI_PROTO_FC: {
6044 		struct scsi_transportid_fcp *fcp;
6045 
6046 		fcp = (struct scsi_transportid_fcp *)(*hdr);
6047 		fcp->format_protocol = SCSI_PROTO_FC |
6048 				       SCSI_TRN_FCP_FORMAT_DEFAULT;
6049 		scsi_u64to8b(value, fcp->n_port_name);
6050 		break;
6051 	}
6052 	case SCSI_PROTO_1394: {
6053 		struct scsi_transportid_1394 *sbp;
6054 
6055 		sbp = (struct scsi_transportid_1394 *)(*hdr);
6056 		sbp->format_protocol = SCSI_PROTO_1394 |
6057 				       SCSI_TRN_1394_FORMAT_DEFAULT;
6058 		scsi_u64to8b(value, sbp->eui64);
6059 		break;
6060 	}
6061 	case SCSI_PROTO_SAS: {
6062 		struct scsi_transportid_sas *sas;
6063 
6064 		sas = (struct scsi_transportid_sas *)(*hdr);
6065 		sas->format_protocol = SCSI_PROTO_SAS |
6066 				       SCSI_TRN_SAS_FORMAT_DEFAULT;
6067 		scsi_u64to8b(value, sas->sas_address);
6068 		break;
6069 	}
6070 	default:
6071 		break;
6072 	}
6073 bailout:
6074 	return (retval);
6075 }
6076 
6077 /*
6078  * Parse a SPI (Parallel SCSI) address of the form: id,rel_tgt_port
6079  */
6080 int
6081 scsi_parse_transportid_spi(char *id_str, struct scsi_transportid_header **hdr,
6082 			   unsigned int *alloc_len,
6083 #ifdef _KERNEL
6084 			   struct malloc_type *type, int flags,
6085 #endif
6086 			   char *error_str, int error_str_len)
6087 {
6088 	unsigned long scsi_addr, target_port;
6089 	struct scsi_transportid_spi *spi;
6090 	char *tmpstr, *endptr;
6091 	int retval;
6092 
6093 	retval = 0;
6094 
6095 	tmpstr = strsep(&id_str, ",");
6096 	if (tmpstr == NULL) {
6097 		if (error_str != NULL) {
6098 			snprintf(error_str, error_str_len,
6099 				 "%s: no ID found", __func__);
6100 		}
6101 		retval = 1;
6102 		goto bailout;
6103 	}
6104 	scsi_addr = strtoul(tmpstr, &endptr, 0);
6105 	if (*endptr != '\0') {
6106 		if (error_str != NULL) {
6107 			snprintf(error_str, error_str_len, "%s: error "
6108 				 "parsing SCSI ID %s, number required",
6109 				 __func__, tmpstr);
6110 		}
6111 		retval = 1;
6112 		goto bailout;
6113 	}
6114 
6115 	if (id_str == NULL) {
6116 		if (error_str != NULL) {
6117 			snprintf(error_str, error_str_len, "%s: no relative "
6118 				 "target port found", __func__);
6119 		}
6120 		retval = 1;
6121 		goto bailout;
6122 	}
6123 
6124 	target_port = strtoul(id_str, &endptr, 0);
6125 	if (*endptr != '\0') {
6126 		if (error_str != NULL) {
6127 			snprintf(error_str, error_str_len, "%s: error "
6128 				 "parsing relative target port %s, number "
6129 				 "required", __func__, id_str);
6130 		}
6131 		retval = 1;
6132 		goto bailout;
6133 	}
6134 #ifdef _KERNEL
6135 	spi = malloc(sizeof(*spi), type, flags);
6136 #else
6137 	spi = malloc(sizeof(*spi));
6138 #endif
6139 	if (spi == NULL) {
6140 		if (error_str != NULL) {
6141 			snprintf(error_str, error_str_len, "%s: unable to "
6142 				 "allocate %zu bytes", __func__,
6143 				 sizeof(*spi));
6144 		}
6145 		retval = 1;
6146 		goto bailout;
6147 	}
6148 	*alloc_len = sizeof(*spi);
6149 	bzero(spi, sizeof(*spi));
6150 
6151 	spi->format_protocol = SCSI_PROTO_SPI | SCSI_TRN_SPI_FORMAT_DEFAULT;
6152 	scsi_ulto2b(scsi_addr, spi->scsi_addr);
6153 	scsi_ulto2b(target_port, spi->rel_trgt_port_id);
6154 
6155 	*hdr = (struct scsi_transportid_header *)spi;
6156 bailout:
6157 	return (retval);
6158 }
6159 
6160 /*
6161  * Parse an RDMA/SRP Initiator Port ID string.  This is 32 hexadecimal digits,
6162  * optionally prefixed by "0x" or "0X".
6163  */
6164 int
6165 scsi_parse_transportid_rdma(char *id_str, struct scsi_transportid_header **hdr,
6166 			    unsigned int *alloc_len,
6167 #ifdef _KERNEL
6168 			    struct malloc_type *type, int flags,
6169 #endif
6170 			    char *error_str, int error_str_len)
6171 {
6172 	struct scsi_transportid_rdma *rdma;
6173 	int retval;
6174 	size_t id_len, rdma_id_size;
6175 	uint8_t rdma_id[SCSI_TRN_RDMA_PORT_LEN];
6176 	char *tmpstr;
6177 	unsigned int i, j;
6178 
6179 	retval = 0;
6180 	id_len = strlen(id_str);
6181 	rdma_id_size = SCSI_TRN_RDMA_PORT_LEN;
6182 
6183 	/*
6184 	 * Check the size.  It needs to be either 32 or 34 characters long.
6185 	 */
6186 	if ((id_len != (rdma_id_size * 2))
6187 	 && (id_len != ((rdma_id_size * 2) + 2))) {
6188 		if (error_str != NULL) {
6189 			snprintf(error_str, error_str_len, "%s: RDMA ID "
6190 				 "must be 32 hex digits (0x prefix "
6191 				 "optional), only %zu seen", __func__, id_len);
6192 		}
6193 		retval = 1;
6194 		goto bailout;
6195 	}
6196 
6197 	tmpstr = id_str;
6198 	/*
6199 	 * If the user gave us 34 characters, the string needs to start
6200 	 * with '0x'.
6201 	 */
6202 	if (id_len == ((rdma_id_size * 2) + 2)) {
6203 	 	if ((tmpstr[0] == '0')
6204 		 && ((tmpstr[1] == 'x') || (tmpstr[1] == 'X'))) {
6205 			tmpstr += 2;
6206 		} else {
6207 			if (error_str != NULL) {
6208 				snprintf(error_str, error_str_len, "%s: RDMA "
6209 					 "ID prefix, if used, must be \"0x\", "
6210 					 "got %s", __func__, tmpstr);
6211 			}
6212 			retval = 1;
6213 			goto bailout;
6214 		}
6215 	}
6216 	bzero(rdma_id, sizeof(rdma_id));
6217 
6218 	/*
6219 	 * Convert ASCII hex into binary bytes.  There is no standard
6220 	 * 128-bit integer type, and so no strtou128t() routine to convert
6221 	 * from hex into a large integer.  In the end, we're not going to
6222 	 * an integer, but rather to a byte array, so that and the fact
6223 	 * that we require the user to give us 32 hex digits simplifies the
6224 	 * logic.
6225 	 */
6226 	for (i = 0; i < (rdma_id_size * 2); i++) {
6227 		int cur_shift;
6228 		unsigned char c;
6229 
6230 		/* Increment the byte array one for every 2 hex digits */
6231 		j = i >> 1;
6232 
6233 		/*
6234 		 * The first digit in every pair is the most significant
6235 		 * 4 bits.  The second is the least significant 4 bits.
6236 		 */
6237 		if ((i % 2) == 0)
6238 			cur_shift = 4;
6239 		else
6240 			cur_shift = 0;
6241 
6242 		c = tmpstr[i];
6243 		/* Convert the ASCII hex character into a number */
6244 		if (isdigit(c))
6245 			c -= '0';
6246 		else if (isalpha(c))
6247 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
6248 		else {
6249 			if (error_str != NULL) {
6250 				snprintf(error_str, error_str_len, "%s: "
6251 					 "RDMA ID must be hex digits, got "
6252 					 "invalid character %c", __func__,
6253 					 tmpstr[i]);
6254 			}
6255 			retval = 1;
6256 			goto bailout;
6257 		}
6258 		/*
6259 		 * The converted number can't be less than 0; the type is
6260 		 * unsigned, and the subtraction logic will not give us
6261 		 * a negative number.  So we only need to make sure that
6262 		 * the value is not greater than 0xf.  (i.e. make sure the
6263 		 * user didn't give us a value like "0x12jklmno").
6264 		 */
6265 		if (c > 0xf) {
6266 			if (error_str != NULL) {
6267 				snprintf(error_str, error_str_len, "%s: "
6268 					 "RDMA ID must be hex digits, got "
6269 					 "invalid character %c", __func__,
6270 					 tmpstr[i]);
6271 			}
6272 			retval = 1;
6273 			goto bailout;
6274 		}
6275 
6276 		rdma_id[j] |= c << cur_shift;
6277 	}
6278 
6279 #ifdef _KERNEL
6280 	rdma = malloc(sizeof(*rdma), type, flags);
6281 #else
6282 	rdma = malloc(sizeof(*rdma));
6283 #endif
6284 	if (rdma == NULL) {
6285 		if (error_str != NULL) {
6286 			snprintf(error_str, error_str_len, "%s: unable to "
6287 				 "allocate %zu bytes", __func__,
6288 				 sizeof(*rdma));
6289 		}
6290 		retval = 1;
6291 		goto bailout;
6292 	}
6293 	*alloc_len = sizeof(*rdma);
6294 	bzero(rdma, *alloc_len);
6295 
6296 	rdma->format_protocol = SCSI_PROTO_RDMA | SCSI_TRN_RDMA_FORMAT_DEFAULT;
6297 	bcopy(rdma_id, rdma->initiator_port_id, SCSI_TRN_RDMA_PORT_LEN);
6298 
6299 	*hdr = (struct scsi_transportid_header *)rdma;
6300 
6301 bailout:
6302 	return (retval);
6303 }
6304 
6305 /*
6306  * Parse an iSCSI name.  The format is either just the name:
6307  *
6308  *	iqn.2012-06.com.example:target0
6309  * or the name, separator and initiator session ID:
6310  *
6311  *	iqn.2012-06.com.example:target0,i,0x123
6312  *
6313  * The separator format is exact.
6314  */
6315 int
6316 scsi_parse_transportid_iscsi(char *id_str, struct scsi_transportid_header **hdr,
6317 			     unsigned int *alloc_len,
6318 #ifdef _KERNEL
6319 			     struct malloc_type *type, int flags,
6320 #endif
6321 			     char *error_str, int error_str_len)
6322 {
6323 	size_t id_len, sep_len, id_size, name_len;
6324 	int retval;
6325 	unsigned int i, sep_pos, sep_found;
6326 	const char *sep_template = ",i,0x";
6327 	const char *iqn_prefix = "iqn.";
6328 	struct scsi_transportid_iscsi_device *iscsi;
6329 
6330 	retval = 0;
6331 	sep_found = 0;
6332 
6333 	id_len = strlen(id_str);
6334 	sep_len = strlen(sep_template);
6335 
6336 	/*
6337 	 * The separator is defined as exactly ',i,0x'.  Any other commas,
6338 	 * or any other form, is an error.  So look for a comma, and once
6339 	 * we find that, the next few characters must match the separator
6340 	 * exactly.  Once we get through the separator, there should be at
6341 	 * least one character.
6342 	 */
6343 	for (i = 0, sep_pos = 0; i < id_len; i++) {
6344 		if (sep_pos == 0) {
6345 		 	if (id_str[i] == sep_template[sep_pos])
6346 				sep_pos++;
6347 
6348 			continue;
6349 		}
6350 		if (sep_pos < sep_len) {
6351 			if (id_str[i] == sep_template[sep_pos]) {
6352 				sep_pos++;
6353 				continue;
6354 			}
6355 			if (error_str != NULL) {
6356 				snprintf(error_str, error_str_len, "%s: "
6357 					 "invalid separator in iSCSI name "
6358 					 "\"%s\"",
6359 					 __func__, id_str);
6360 			}
6361 			retval = 1;
6362 			goto bailout;
6363 		} else {
6364 			sep_found = 1;
6365 			break;
6366 		}
6367 	}
6368 
6369 	/*
6370 	 * Check to see whether we have a separator but no digits after it.
6371 	 */
6372 	if ((sep_pos != 0)
6373 	 && (sep_found == 0)) {
6374 		if (error_str != NULL) {
6375 			snprintf(error_str, error_str_len, "%s: no digits "
6376 				 "found after separator in iSCSI name \"%s\"",
6377 				 __func__, id_str);
6378 		}
6379 		retval = 1;
6380 		goto bailout;
6381 	}
6382 
6383 	/*
6384 	 * The incoming ID string has the "iqn." prefix stripped off.  We
6385 	 * need enough space for the base structure (the structures are the
6386 	 * same for the two iSCSI forms), the prefix, the ID string and a
6387 	 * terminating NUL.
6388 	 */
6389 	id_size = sizeof(*iscsi) + strlen(iqn_prefix) + id_len + 1;
6390 
6391 #ifdef _KERNEL
6392 	iscsi = malloc(id_size, type, flags);
6393 #else
6394 	iscsi = malloc(id_size);
6395 #endif
6396 	if (iscsi == NULL) {
6397 		if (error_str != NULL) {
6398 			snprintf(error_str, error_str_len, "%s: unable to "
6399 				 "allocate %zu bytes", __func__, id_size);
6400 		}
6401 		retval = 1;
6402 		goto bailout;
6403 	}
6404 	*alloc_len = id_size;
6405 	bzero(iscsi, id_size);
6406 
6407 	iscsi->format_protocol = SCSI_PROTO_ISCSI;
6408 	if (sep_found == 0)
6409 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_DEVICE;
6410 	else
6411 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_PORT;
6412 	name_len = id_size - sizeof(*iscsi);
6413 	scsi_ulto2b(name_len, iscsi->additional_length);
6414 	snprintf(iscsi->iscsi_name, name_len, "%s%s", iqn_prefix, id_str);
6415 
6416 	*hdr = (struct scsi_transportid_header *)iscsi;
6417 
6418 bailout:
6419 	return (retval);
6420 }
6421 
6422 /*
6423  * Parse a SCSI over PCIe (SOP) identifier.  The Routing ID can either be
6424  * of the form 'bus,device,function' or 'bus,function'.
6425  */
6426 int
6427 scsi_parse_transportid_sop(char *id_str, struct scsi_transportid_header **hdr,
6428 			   unsigned int *alloc_len,
6429 #ifdef _KERNEL
6430 			   struct malloc_type *type, int flags,
6431 #endif
6432 			   char *error_str, int error_str_len)
6433 {
6434 	struct scsi_transportid_sop *sop;
6435 	unsigned long bus, device, function;
6436 	char *tmpstr, *endptr;
6437 	int retval, device_spec;
6438 
6439 	retval = 0;
6440 	device_spec = 0;
6441 	device = 0;
6442 
6443 	tmpstr = strsep(&id_str, ",");
6444 	if ((tmpstr == NULL)
6445 	 || (*tmpstr == '\0')) {
6446 		if (error_str != NULL) {
6447 			snprintf(error_str, error_str_len, "%s: no ID found",
6448 				 __func__);
6449 		}
6450 		retval = 1;
6451 		goto bailout;
6452 	}
6453 	bus = strtoul(tmpstr, &endptr, 0);
6454 	if (*endptr != '\0') {
6455 		if (error_str != NULL) {
6456 			snprintf(error_str, error_str_len, "%s: error "
6457 				 "parsing PCIe bus %s, number required",
6458 				 __func__, tmpstr);
6459 		}
6460 		retval = 1;
6461 		goto bailout;
6462 	}
6463 	if ((id_str == NULL)
6464 	 || (*id_str == '\0')) {
6465 		if (error_str != NULL) {
6466 			snprintf(error_str, error_str_len, "%s: no PCIe "
6467 				 "device or function found", __func__);
6468 		}
6469 		retval = 1;
6470 		goto bailout;
6471 	}
6472 	tmpstr = strsep(&id_str, ",");
6473 	function = strtoul(tmpstr, &endptr, 0);
6474 	if (*endptr != '\0') {
6475 		if (error_str != NULL) {
6476 			snprintf(error_str, error_str_len, "%s: error "
6477 				 "parsing PCIe device/function %s, number "
6478 				 "required", __func__, tmpstr);
6479 		}
6480 		retval = 1;
6481 		goto bailout;
6482 	}
6483 	/*
6484 	 * Check to see whether the user specified a third value.  If so,
6485 	 * the second is the device.
6486 	 */
6487 	if (id_str != NULL) {
6488 		if (*id_str == '\0') {
6489 			if (error_str != NULL) {
6490 				snprintf(error_str, error_str_len, "%s: "
6491 					 "no PCIe function found", __func__);
6492 			}
6493 			retval = 1;
6494 			goto bailout;
6495 		}
6496 		device = function;
6497 		device_spec = 1;
6498 		function = strtoul(id_str, &endptr, 0);
6499 		if (*endptr != '\0') {
6500 			if (error_str != NULL) {
6501 				snprintf(error_str, error_str_len, "%s: "
6502 					 "error parsing PCIe function %s, "
6503 					 "number required", __func__, id_str);
6504 			}
6505 			retval = 1;
6506 			goto bailout;
6507 		}
6508 	}
6509 	if (bus > SCSI_TRN_SOP_BUS_MAX) {
6510 		if (error_str != NULL) {
6511 			snprintf(error_str, error_str_len, "%s: bus value "
6512 				 "%lu greater than maximum %u", __func__,
6513 				 bus, SCSI_TRN_SOP_BUS_MAX);
6514 		}
6515 		retval = 1;
6516 		goto bailout;
6517 	}
6518 
6519 	if ((device_spec != 0)
6520 	 && (device > SCSI_TRN_SOP_DEV_MASK)) {
6521 		if (error_str != NULL) {
6522 			snprintf(error_str, error_str_len, "%s: device value "
6523 				 "%lu greater than maximum %u", __func__,
6524 				 device, SCSI_TRN_SOP_DEV_MAX);
6525 		}
6526 		retval = 1;
6527 		goto bailout;
6528 	}
6529 
6530 	if (((device_spec != 0)
6531 	  && (function > SCSI_TRN_SOP_FUNC_NORM_MAX))
6532 	 || ((device_spec == 0)
6533 	  && (function > SCSI_TRN_SOP_FUNC_ALT_MAX))) {
6534 		if (error_str != NULL) {
6535 			snprintf(error_str, error_str_len, "%s: function value "
6536 				 "%lu greater than maximum %u", __func__,
6537 				 function, (device_spec == 0) ?
6538 				 SCSI_TRN_SOP_FUNC_ALT_MAX :
6539 				 SCSI_TRN_SOP_FUNC_NORM_MAX);
6540 		}
6541 		retval = 1;
6542 		goto bailout;
6543 	}
6544 
6545 #ifdef _KERNEL
6546 	sop = malloc(sizeof(*sop), type, flags);
6547 #else
6548 	sop = malloc(sizeof(*sop));
6549 #endif
6550 	if (sop == NULL) {
6551 		if (error_str != NULL) {
6552 			snprintf(error_str, error_str_len, "%s: unable to "
6553 				 "allocate %zu bytes", __func__, sizeof(*sop));
6554 		}
6555 		retval = 1;
6556 		goto bailout;
6557 	}
6558 	*alloc_len = sizeof(*sop);
6559 	bzero(sop, sizeof(*sop));
6560 	sop->format_protocol = SCSI_PROTO_SOP | SCSI_TRN_SOP_FORMAT_DEFAULT;
6561 	if (device_spec != 0) {
6562 		struct scsi_sop_routing_id_norm rid;
6563 
6564 		rid.bus = bus;
6565 		rid.devfunc = (device << SCSI_TRN_SOP_DEV_SHIFT) | function;
6566 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6567 		      sizeof(sop->routing_id)));
6568 	} else {
6569 		struct scsi_sop_routing_id_alt rid;
6570 
6571 		rid.bus = bus;
6572 		rid.function = function;
6573 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6574 		      sizeof(sop->routing_id)));
6575 	}
6576 
6577 	*hdr = (struct scsi_transportid_header *)sop;
6578 bailout:
6579 	return (retval);
6580 }
6581 
6582 /*
6583  * transportid_str: NUL-terminated string with format: protcol,id
6584  *		    The ID is protocol specific.
6585  * hdr:		    Storage will be allocated for the transport ID.
6586  * alloc_len:	    The amount of memory allocated is returned here.
6587  * type:	    Malloc bucket (kernel only).
6588  * flags:	    Malloc flags (kernel only).
6589  * error_str:	    If non-NULL, it will contain error information (without
6590  * 		    a terminating newline) if an error is returned.
6591  * error_str_len:   Allocated length of the error string.
6592  *
6593  * Returns 0 for success, non-zero for failure.
6594  */
6595 int
6596 scsi_parse_transportid(char *transportid_str,
6597 		       struct scsi_transportid_header **hdr,
6598 		       unsigned int *alloc_len,
6599 #ifdef _KERNEL
6600 		       struct malloc_type *type, int flags,
6601 #endif
6602 		       char *error_str, int error_str_len)
6603 {
6604 	char *tmpstr;
6605 	scsi_nv_status status;
6606 	u_int num_proto_entries;
6607 	int retval, table_entry;
6608 
6609 	retval = 0;
6610 	table_entry = 0;
6611 
6612 	/*
6613 	 * We do allow a period as well as a comma to separate the protocol
6614 	 * from the ID string.  This is to accommodate iSCSI names, which
6615 	 * start with "iqn.".
6616 	 */
6617 	tmpstr = strsep(&transportid_str, ",.");
6618 	if (tmpstr == NULL) {
6619 		if (error_str != NULL) {
6620 			snprintf(error_str, error_str_len,
6621 				 "%s: transportid_str is NULL", __func__);
6622 		}
6623 		retval = 1;
6624 		goto bailout;
6625 	}
6626 
6627 	num_proto_entries = nitems(scsi_proto_map);
6628 	status = scsi_get_nv(scsi_proto_map, num_proto_entries, tmpstr,
6629 			     &table_entry, SCSI_NV_FLAG_IG_CASE);
6630 	if (status != SCSI_NV_FOUND) {
6631 		if (error_str != NULL) {
6632 			snprintf(error_str, error_str_len, "%s: %s protocol "
6633 				 "name %s", __func__,
6634 				 (status == SCSI_NV_AMBIGUOUS) ? "ambiguous" :
6635 				 "invalid", tmpstr);
6636 		}
6637 		retval = 1;
6638 		goto bailout;
6639 	}
6640 	switch (scsi_proto_map[table_entry].value) {
6641 	case SCSI_PROTO_FC:
6642 	case SCSI_PROTO_1394:
6643 	case SCSI_PROTO_SAS:
6644 		retval = scsi_parse_transportid_64bit(
6645 		    scsi_proto_map[table_entry].value, transportid_str, hdr,
6646 		    alloc_len,
6647 #ifdef _KERNEL
6648 		    type, flags,
6649 #endif
6650 		    error_str, error_str_len);
6651 		break;
6652 	case SCSI_PROTO_SPI:
6653 		retval = scsi_parse_transportid_spi(transportid_str, hdr,
6654 		    alloc_len,
6655 #ifdef _KERNEL
6656 		    type, flags,
6657 #endif
6658 		    error_str, error_str_len);
6659 		break;
6660 	case SCSI_PROTO_RDMA:
6661 		retval = scsi_parse_transportid_rdma(transportid_str, hdr,
6662 		    alloc_len,
6663 #ifdef _KERNEL
6664 		    type, flags,
6665 #endif
6666 		    error_str, error_str_len);
6667 		break;
6668 	case SCSI_PROTO_ISCSI:
6669 		retval = scsi_parse_transportid_iscsi(transportid_str, hdr,
6670 		    alloc_len,
6671 #ifdef _KERNEL
6672 		    type, flags,
6673 #endif
6674 		    error_str, error_str_len);
6675 		break;
6676 	case SCSI_PROTO_SOP:
6677 		retval = scsi_parse_transportid_sop(transportid_str, hdr,
6678 		    alloc_len,
6679 #ifdef _KERNEL
6680 		    type, flags,
6681 #endif
6682 		    error_str, error_str_len);
6683 		break;
6684 	case SCSI_PROTO_SSA:
6685 	case SCSI_PROTO_ADITP:
6686 	case SCSI_PROTO_ATA:
6687 	case SCSI_PROTO_UAS:
6688 	case SCSI_PROTO_NONE:
6689 	default:
6690 		/*
6691 		 * There is no format defined for a Transport ID for these
6692 		 * protocols.  So even if the user gives us something, we
6693 		 * have no way to turn it into a standard SCSI Transport ID.
6694 		 */
6695 		retval = 1;
6696 		if (error_str != NULL) {
6697 			snprintf(error_str, error_str_len, "%s: no Transport "
6698 				 "ID format exists for protocol %s",
6699 				 __func__, tmpstr);
6700 		}
6701 		goto bailout;
6702 		break;	/* NOTREACHED */
6703 	}
6704 bailout:
6705 	return (retval);
6706 }
6707 
6708 struct scsi_attrib_table_entry scsi_mam_attr_table[] = {
6709 	{ SMA_ATTR_REM_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6710 	  "Remaining Capacity in Partition",
6711 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,/*parse_str*/ NULL },
6712 	{ SMA_ATTR_MAX_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6713 	  "Maximum Capacity in Partition",
6714 	  /*suffix*/"MB", /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6715 	{ SMA_ATTR_TAPEALERT_FLAGS, SCSI_ATTR_FLAG_HEX,
6716 	  "TapeAlert Flags",
6717 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6718 	{ SMA_ATTR_LOAD_COUNT, SCSI_ATTR_FLAG_NONE,
6719 	  "Load Count",
6720 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6721 	{ SMA_ATTR_MAM_SPACE_REMAINING, SCSI_ATTR_FLAG_NONE,
6722 	  "MAM Space Remaining",
6723 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6724 	  /*parse_str*/ NULL },
6725 	{ SMA_ATTR_DEV_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6726 	  "Assigning Organization",
6727 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6728 	  /*parse_str*/ NULL },
6729 	{ SMA_ATTR_FORMAT_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6730 	  "Format Density Code",
6731 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6732 	{ SMA_ATTR_INITIALIZATION_COUNT, SCSI_ATTR_FLAG_NONE,
6733 	  "Initialization Count",
6734 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6735 	{ SMA_ATTR_VOLUME_ID, SCSI_ATTR_FLAG_NONE,
6736 	  "Volume Identifier",
6737 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6738 	  /*parse_str*/ NULL },
6739 	{ SMA_ATTR_VOLUME_CHANGE_REF, SCSI_ATTR_FLAG_HEX,
6740 	  "Volume Change Reference",
6741 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6742 	  /*parse_str*/ NULL },
6743 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD, SCSI_ATTR_FLAG_NONE,
6744 	  "Device Vendor/Serial at Last Load",
6745 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6746 	  /*parse_str*/ NULL },
6747 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_1, SCSI_ATTR_FLAG_NONE,
6748 	  "Device Vendor/Serial at Last Load - 1",
6749 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6750 	  /*parse_str*/ NULL },
6751 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_2, SCSI_ATTR_FLAG_NONE,
6752 	  "Device Vendor/Serial at Last Load - 2",
6753 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6754 	  /*parse_str*/ NULL },
6755 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_3, SCSI_ATTR_FLAG_NONE,
6756 	  "Device Vendor/Serial at Last Load - 3",
6757 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6758 	  /*parse_str*/ NULL },
6759 	{ SMA_ATTR_TOTAL_MB_WRITTEN_LT, SCSI_ATTR_FLAG_NONE,
6760 	  "Total MB Written in Medium Life",
6761 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6762 	  /*parse_str*/ NULL },
6763 	{ SMA_ATTR_TOTAL_MB_READ_LT, SCSI_ATTR_FLAG_NONE,
6764 	  "Total MB Read in Medium Life",
6765 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6766 	  /*parse_str*/ NULL },
6767 	{ SMA_ATTR_TOTAL_MB_WRITTEN_CUR, SCSI_ATTR_FLAG_NONE,
6768 	  "Total MB Written in Current/Last Load",
6769 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6770 	  /*parse_str*/ NULL },
6771 	{ SMA_ATTR_TOTAL_MB_READ_CUR, SCSI_ATTR_FLAG_NONE,
6772 	  "Total MB Read in Current/Last Load",
6773 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6774 	  /*parse_str*/ NULL },
6775 	{ SMA_ATTR_FIRST_ENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6776 	  "Logical Position of First Encrypted Block",
6777 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6778 	  /*parse_str*/ NULL },
6779 	{ SMA_ATTR_NEXT_UNENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6780 	  "Logical Position of First Unencrypted Block after First "
6781 	  "Encrypted Block",
6782 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6783 	  /*parse_str*/ NULL },
6784 	{ SMA_ATTR_MEDIUM_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6785 	  "Medium Usage History",
6786 	  /*suffix*/ NULL, /*to_str*/ NULL,
6787 	  /*parse_str*/ NULL },
6788 	{ SMA_ATTR_PART_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6789 	  "Partition Usage History",
6790 	  /*suffix*/ NULL, /*to_str*/ NULL,
6791 	  /*parse_str*/ NULL },
6792 	{ SMA_ATTR_MED_MANUF, SCSI_ATTR_FLAG_NONE,
6793 	  "Medium Manufacturer",
6794 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6795 	  /*parse_str*/ NULL },
6796 	{ SMA_ATTR_MED_SERIAL, SCSI_ATTR_FLAG_NONE,
6797 	  "Medium Serial Number",
6798 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6799 	  /*parse_str*/ NULL },
6800 	{ SMA_ATTR_MED_LENGTH, SCSI_ATTR_FLAG_NONE,
6801 	  "Medium Length",
6802 	  /*suffix*/"m", /*to_str*/ scsi_attrib_int_sbuf,
6803 	  /*parse_str*/ NULL },
6804 	{ SMA_ATTR_MED_WIDTH, SCSI_ATTR_FLAG_FP | SCSI_ATTR_FLAG_DIV_10 |
6805 	  SCSI_ATTR_FLAG_FP_1DIGIT,
6806 	  "Medium Width",
6807 	  /*suffix*/"mm", /*to_str*/ scsi_attrib_int_sbuf,
6808 	  /*parse_str*/ NULL },
6809 	{ SMA_ATTR_MED_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6810 	  "Assigning Organization",
6811 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6812 	  /*parse_str*/ NULL },
6813 	{ SMA_ATTR_MED_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6814 	  "Medium Density Code",
6815 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6816 	  /*parse_str*/ NULL },
6817 	{ SMA_ATTR_MED_MANUF_DATE, SCSI_ATTR_FLAG_NONE,
6818 	  "Medium Manufacture Date",
6819 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6820 	  /*parse_str*/ NULL },
6821 	{ SMA_ATTR_MAM_CAPACITY, SCSI_ATTR_FLAG_NONE,
6822 	  "MAM Capacity",
6823 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6824 	  /*parse_str*/ NULL },
6825 	{ SMA_ATTR_MED_TYPE, SCSI_ATTR_FLAG_HEX,
6826 	  "Medium Type",
6827 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6828 	  /*parse_str*/ NULL },
6829 	{ SMA_ATTR_MED_TYPE_INFO, SCSI_ATTR_FLAG_HEX,
6830 	  "Medium Type Information",
6831 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6832 	  /*parse_str*/ NULL },
6833 	{ SMA_ATTR_MED_SERIAL_NUM, SCSI_ATTR_FLAG_NONE,
6834 	  "Medium Serial Number",
6835 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6836 	  /*parse_str*/ NULL },
6837 	{ SMA_ATTR_APP_VENDOR, SCSI_ATTR_FLAG_NONE,
6838 	  "Application Vendor",
6839 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6840 	  /*parse_str*/ NULL },
6841 	{ SMA_ATTR_APP_NAME, SCSI_ATTR_FLAG_NONE,
6842 	  "Application Name",
6843 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6844 	  /*parse_str*/ NULL },
6845 	{ SMA_ATTR_APP_VERSION, SCSI_ATTR_FLAG_NONE,
6846 	  "Application Version",
6847 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6848 	  /*parse_str*/ NULL },
6849 	{ SMA_ATTR_USER_MED_TEXT_LABEL, SCSI_ATTR_FLAG_NONE,
6850 	  "User Medium Text Label",
6851 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6852 	  /*parse_str*/ NULL },
6853 	{ SMA_ATTR_LAST_WRITTEN_TIME, SCSI_ATTR_FLAG_NONE,
6854 	  "Date and Time Last Written",
6855 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6856 	  /*parse_str*/ NULL },
6857 	{ SMA_ATTR_TEXT_LOCAL_ID, SCSI_ATTR_FLAG_HEX,
6858 	  "Text Localization Identifier",
6859 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6860 	  /*parse_str*/ NULL },
6861 	{ SMA_ATTR_BARCODE, SCSI_ATTR_FLAG_NONE,
6862 	  "Barcode",
6863 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6864 	  /*parse_str*/ NULL },
6865 	{ SMA_ATTR_HOST_OWNER_NAME, SCSI_ATTR_FLAG_NONE,
6866 	  "Owning Host Textual Name",
6867 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6868 	  /*parse_str*/ NULL },
6869 	{ SMA_ATTR_MEDIA_POOL, SCSI_ATTR_FLAG_NONE,
6870 	  "Media Pool",
6871 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6872 	  /*parse_str*/ NULL },
6873 	{ SMA_ATTR_PART_USER_LABEL, SCSI_ATTR_FLAG_NONE,
6874 	  "Partition User Text Label",
6875 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6876 	  /*parse_str*/ NULL },
6877 	{ SMA_ATTR_LOAD_UNLOAD_AT_PART, SCSI_ATTR_FLAG_NONE,
6878 	  "Load/Unload at Partition",
6879 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6880 	  /*parse_str*/ NULL },
6881 	{ SMA_ATTR_APP_FORMAT_VERSION, SCSI_ATTR_FLAG_NONE,
6882 	  "Application Format Version",
6883 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6884 	  /*parse_str*/ NULL },
6885 	{ SMA_ATTR_VOL_COHERENCY_INFO, SCSI_ATTR_FLAG_NONE,
6886 	  "Volume Coherency Information",
6887 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_volcoh_sbuf,
6888 	  /*parse_str*/ NULL },
6889 	{ 0x0ff1, SCSI_ATTR_FLAG_NONE,
6890 	  "Spectra MLM Creation",
6891 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6892 	  /*parse_str*/ NULL },
6893 	{ 0x0ff2, SCSI_ATTR_FLAG_NONE,
6894 	  "Spectra MLM C3",
6895 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6896 	  /*parse_str*/ NULL },
6897 	{ 0x0ff3, SCSI_ATTR_FLAG_NONE,
6898 	  "Spectra MLM RW",
6899 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6900 	  /*parse_str*/ NULL },
6901 	{ 0x0ff4, SCSI_ATTR_FLAG_NONE,
6902 	  "Spectra MLM SDC List",
6903 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6904 	  /*parse_str*/ NULL },
6905 	{ 0x0ff7, SCSI_ATTR_FLAG_NONE,
6906 	  "Spectra MLM Post Scan",
6907 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6908 	  /*parse_str*/ NULL },
6909 	{ 0x0ffe, SCSI_ATTR_FLAG_NONE,
6910 	  "Spectra MLM Checksum",
6911 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6912 	  /*parse_str*/ NULL },
6913 	{ 0x17f1, SCSI_ATTR_FLAG_NONE,
6914 	  "Spectra MLM Creation",
6915 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6916 	  /*parse_str*/ NULL },
6917 	{ 0x17f2, SCSI_ATTR_FLAG_NONE,
6918 	  "Spectra MLM C3",
6919 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6920 	  /*parse_str*/ NULL },
6921 	{ 0x17f3, SCSI_ATTR_FLAG_NONE,
6922 	  "Spectra MLM RW",
6923 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6924 	  /*parse_str*/ NULL },
6925 	{ 0x17f4, SCSI_ATTR_FLAG_NONE,
6926 	  "Spectra MLM SDC List",
6927 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6928 	  /*parse_str*/ NULL },
6929 	{ 0x17f7, SCSI_ATTR_FLAG_NONE,
6930 	  "Spectra MLM Post Scan",
6931 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6932 	  /*parse_str*/ NULL },
6933 	{ 0x17ff, SCSI_ATTR_FLAG_NONE,
6934 	  "Spectra MLM Checksum",
6935 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6936 	  /*parse_str*/ NULL },
6937 };
6938 
6939 /*
6940  * Print out Volume Coherency Information (Attribute 0x080c).
6941  * This field has two variable length members, including one at the
6942  * beginning, so it isn't practical to have a fixed structure definition.
6943  * This is current as of SSC4r03 (see section 4.2.21.3), dated March 25,
6944  * 2013.
6945  */
6946 int
6947 scsi_attrib_volcoh_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
6948 			 uint32_t valid_len, uint32_t flags,
6949 			 uint32_t output_flags, char *error_str,
6950 			 int error_str_len)
6951 {
6952 	size_t avail_len;
6953 	uint32_t field_size;
6954 	uint64_t tmp_val;
6955 	uint8_t *cur_ptr;
6956 	int retval;
6957 	int vcr_len, as_len;
6958 
6959 	retval = 0;
6960 	tmp_val = 0;
6961 
6962 	field_size = scsi_2btoul(hdr->length);
6963 	avail_len = valid_len - sizeof(*hdr);
6964 	if (field_size > avail_len) {
6965 		if (error_str != NULL) {
6966 			snprintf(error_str, error_str_len, "Available "
6967 				 "length of attribute ID 0x%.4x %zu < field "
6968 				 "length %u", scsi_2btoul(hdr->id), avail_len,
6969 				 field_size);
6970 		}
6971 		retval = 1;
6972 		goto bailout;
6973 	} else if (field_size == 0) {
6974 		/*
6975 		 * It isn't clear from the spec whether a field length of
6976 		 * 0 is invalid here.  It probably is, but be lenient here
6977 		 * to avoid inconveniencing the user.
6978 		 */
6979 		goto bailout;
6980 	}
6981 	cur_ptr = hdr->attribute;
6982 	vcr_len = *cur_ptr;
6983 	cur_ptr++;
6984 
6985 	sbuf_printf(sb, "\n\tVolume Change Reference Value:");
6986 
6987 	switch (vcr_len) {
6988 	case 0:
6989 		if (error_str != NULL) {
6990 			snprintf(error_str, error_str_len, "Volume Change "
6991 				 "Reference value has length of 0");
6992 		}
6993 		retval = 1;
6994 		goto bailout;
6995 		break; /*NOTREACHED*/
6996 	case 1:
6997 		tmp_val = *cur_ptr;
6998 		break;
6999 	case 2:
7000 		tmp_val = scsi_2btoul(cur_ptr);
7001 		break;
7002 	case 3:
7003 		tmp_val = scsi_3btoul(cur_ptr);
7004 		break;
7005 	case 4:
7006 		tmp_val = scsi_4btoul(cur_ptr);
7007 		break;
7008 	case 8:
7009 		tmp_val = scsi_8btou64(cur_ptr);
7010 		break;
7011 	default:
7012 		sbuf_printf(sb, "\n");
7013 		sbuf_hexdump(sb, cur_ptr, vcr_len, NULL, 0);
7014 		break;
7015 	}
7016 	if (vcr_len <= 8)
7017 		sbuf_printf(sb, " 0x%jx\n", (uintmax_t)tmp_val);
7018 
7019 	cur_ptr += vcr_len;
7020 	tmp_val = scsi_8btou64(cur_ptr);
7021 	sbuf_printf(sb, "\tVolume Coherency Count: %ju\n", (uintmax_t)tmp_val);
7022 
7023 	cur_ptr += sizeof(tmp_val);
7024 	tmp_val = scsi_8btou64(cur_ptr);
7025 	sbuf_printf(sb, "\tVolume Coherency Set Identifier: 0x%jx\n",
7026 		    (uintmax_t)tmp_val);
7027 
7028 	/*
7029 	 * Figure out how long the Application Client Specific Information
7030 	 * is and produce a hexdump.
7031 	 */
7032 	cur_ptr += sizeof(tmp_val);
7033 	as_len = scsi_2btoul(cur_ptr);
7034 	cur_ptr += sizeof(uint16_t);
7035 	sbuf_printf(sb, "\tApplication Client Specific Information: ");
7036 	if (((as_len == SCSI_LTFS_VER0_LEN)
7037 	  || (as_len == SCSI_LTFS_VER1_LEN))
7038 	 && (strncmp(cur_ptr, SCSI_LTFS_STR_NAME, SCSI_LTFS_STR_LEN) == 0)) {
7039 		sbuf_printf(sb, "LTFS\n");
7040 		cur_ptr += SCSI_LTFS_STR_LEN + 1;
7041 		if (cur_ptr[SCSI_LTFS_UUID_LEN] != '\0')
7042 			cur_ptr[SCSI_LTFS_UUID_LEN] = '\0';
7043 		sbuf_printf(sb, "\tLTFS UUID: %s\n", cur_ptr);
7044 		cur_ptr += SCSI_LTFS_UUID_LEN + 1;
7045 		/* XXX KDM check the length */
7046 		sbuf_printf(sb, "\tLTFS Version: %d\n", *cur_ptr);
7047 	} else {
7048 		sbuf_printf(sb, "Unknown\n");
7049 		sbuf_hexdump(sb, cur_ptr, as_len, NULL, 0);
7050 	}
7051 
7052 bailout:
7053 	return (retval);
7054 }
7055 
7056 int
7057 scsi_attrib_vendser_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7058 			 uint32_t valid_len, uint32_t flags,
7059 			 uint32_t output_flags, char *error_str,
7060 			 int error_str_len)
7061 {
7062 	size_t avail_len;
7063 	uint32_t field_size;
7064 	struct scsi_attrib_vendser *vendser;
7065 	cam_strvis_flags strvis_flags;
7066 	int retval = 0;
7067 
7068 	field_size = scsi_2btoul(hdr->length);
7069 	avail_len = valid_len - sizeof(*hdr);
7070 	if (field_size > avail_len) {
7071 		if (error_str != NULL) {
7072 			snprintf(error_str, error_str_len, "Available "
7073 				 "length of attribute ID 0x%.4x %zu < field "
7074 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7075 				 field_size);
7076 		}
7077 		retval = 1;
7078 		goto bailout;
7079 	} else if (field_size == 0) {
7080 		/*
7081 		 * A field size of 0 doesn't make sense here.  The device
7082 		 * can at least give you the vendor ID, even if it can't
7083 		 * give you the serial number.
7084 		 */
7085 		if (error_str != NULL) {
7086 			snprintf(error_str, error_str_len, "The length of "
7087 				 "attribute ID 0x%.4x is 0",
7088 				 scsi_2btoul(hdr->id));
7089 		}
7090 		retval = 1;
7091 		goto bailout;
7092 	}
7093 	vendser = (struct scsi_attrib_vendser *)hdr->attribute;
7094 
7095 	switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7096 	case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7097 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7098 		break;
7099 	case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7100 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7101 		break;
7102 	case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7103 	default:
7104 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7105 		break;;
7106 	}
7107 	cam_strvis_sbuf(sb, vendser->vendor, sizeof(vendser->vendor),
7108 	    strvis_flags);
7109 	sbuf_putc(sb, ' ');
7110 	cam_strvis_sbuf(sb, vendser->serial_num, sizeof(vendser->serial_num),
7111 	    strvis_flags);
7112 bailout:
7113 	return (retval);
7114 }
7115 
7116 int
7117 scsi_attrib_hexdump_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7118 			 uint32_t valid_len, uint32_t flags,
7119 			 uint32_t output_flags, char *error_str,
7120 			 int error_str_len)
7121 {
7122 	uint32_t field_size;
7123 	ssize_t avail_len;
7124 	uint32_t print_len;
7125 	uint8_t *num_ptr;
7126 	int retval = 0;
7127 
7128 	field_size = scsi_2btoul(hdr->length);
7129 	avail_len = valid_len - sizeof(*hdr);
7130 	print_len = MIN(avail_len, field_size);
7131 	num_ptr = hdr->attribute;
7132 
7133 	if (print_len > 0) {
7134 		sbuf_printf(sb, "\n");
7135 		sbuf_hexdump(sb, num_ptr, print_len, NULL, 0);
7136 	}
7137 
7138 	return (retval);
7139 }
7140 
7141 int
7142 scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7143 		     uint32_t valid_len, uint32_t flags,
7144 		     uint32_t output_flags, char *error_str,
7145 		     int error_str_len)
7146 {
7147 	uint64_t print_number;
7148 	size_t avail_len;
7149 	uint32_t number_size;
7150 	int retval = 0;
7151 
7152 	number_size = scsi_2btoul(hdr->length);
7153 
7154 	avail_len = valid_len - sizeof(*hdr);
7155 	if (avail_len < number_size) {
7156 		if (error_str != NULL) {
7157 			snprintf(error_str, error_str_len, "Available "
7158 				 "length of attribute ID 0x%.4x %zu < field "
7159 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7160 				 number_size);
7161 		}
7162 		retval = 1;
7163 		goto bailout;
7164 	}
7165 
7166 	switch (number_size) {
7167 	case 0:
7168 		/*
7169 		 * We don't treat this as an error, since there may be
7170 		 * scenarios where a device reports a field but then gives
7171 		 * a length of 0.  See the note in scsi_attrib_ascii_sbuf().
7172 		 */
7173 		goto bailout;
7174 		break; /*NOTREACHED*/
7175 	case 1:
7176 		print_number = hdr->attribute[0];
7177 		break;
7178 	case 2:
7179 		print_number = scsi_2btoul(hdr->attribute);
7180 		break;
7181 	case 3:
7182 		print_number = scsi_3btoul(hdr->attribute);
7183 		break;
7184 	case 4:
7185 		print_number = scsi_4btoul(hdr->attribute);
7186 		break;
7187 	case 8:
7188 		print_number = scsi_8btou64(hdr->attribute);
7189 		break;
7190 	default:
7191 		/*
7192 		 * If we wind up here, the number is too big to print
7193 		 * normally, so just do a hexdump.
7194 		 */
7195 		retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7196 						  flags, output_flags,
7197 						  error_str, error_str_len);
7198 		goto bailout;
7199 		break;
7200 	}
7201 
7202 	if (flags & SCSI_ATTR_FLAG_FP) {
7203 #ifndef _KERNEL
7204 		long double num_float;
7205 
7206 		num_float = (long double)print_number;
7207 
7208 		if (flags & SCSI_ATTR_FLAG_DIV_10)
7209 			num_float /= 10;
7210 
7211 		sbuf_printf(sb, "%.*Lf", (flags & SCSI_ATTR_FLAG_FP_1DIGIT) ?
7212 			    1 : 0, num_float);
7213 #else /* _KERNEL */
7214 		sbuf_printf(sb, "%ju", (flags & SCSI_ATTR_FLAG_DIV_10) ?
7215 			    (print_number / 10) : print_number);
7216 #endif /* _KERNEL */
7217 	} else if (flags & SCSI_ATTR_FLAG_HEX) {
7218 		sbuf_printf(sb, "0x%jx", (uintmax_t)print_number);
7219 	} else
7220 		sbuf_printf(sb, "%ju", (uintmax_t)print_number);
7221 
7222 bailout:
7223 	return (retval);
7224 }
7225 
7226 int
7227 scsi_attrib_ascii_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7228 		       uint32_t valid_len, uint32_t flags,
7229 		       uint32_t output_flags, char *error_str,
7230 		       int error_str_len)
7231 {
7232 	size_t avail_len;
7233 	uint32_t field_size, print_size;
7234 	int retval = 0;
7235 
7236 	avail_len = valid_len - sizeof(*hdr);
7237 	field_size = scsi_2btoul(hdr->length);
7238 	print_size = MIN(avail_len, field_size);
7239 
7240 	if (print_size > 0) {
7241 		cam_strvis_flags strvis_flags;
7242 
7243 		switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7244 		case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7245 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7246 			break;
7247 		case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7248 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7249 			break;
7250 		case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7251 		default:
7252 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7253 			break;
7254 		}
7255 		cam_strvis_sbuf(sb, hdr->attribute, print_size, strvis_flags);
7256 	} else if (avail_len < field_size) {
7257 		/*
7258 		 * We only report an error if the user didn't allocate
7259 		 * enough space to hold the full value of this field.  If
7260 		 * the field length is 0, that is allowed by the spec.
7261 		 * e.g. in SPC-4r37, section 7.4.2.2.5, VOLUME IDENTIFIER
7262 		 * "This attribute indicates the current volume identifier
7263 		 * (see SMC-3) of the medium. If the device server supports
7264 		 * this attribute but does not have access to the volume
7265 		 * identifier, the device server shall report this attribute
7266 		 * with an attribute length value of zero."
7267 		 */
7268 		if (error_str != NULL) {
7269 			snprintf(error_str, error_str_len, "Available "
7270 				 "length of attribute ID 0x%.4x %zu < field "
7271 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7272 				 field_size);
7273 		}
7274 		retval = 1;
7275 	}
7276 
7277 	return (retval);
7278 }
7279 
7280 int
7281 scsi_attrib_text_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7282 		      uint32_t valid_len, uint32_t flags,
7283 		      uint32_t output_flags, char *error_str,
7284 		      int error_str_len)
7285 {
7286 	size_t avail_len;
7287 	uint32_t field_size, print_size;
7288 	int retval = 0;
7289 	int esc_text = 1;
7290 
7291 	avail_len = valid_len - sizeof(*hdr);
7292 	field_size = scsi_2btoul(hdr->length);
7293 	print_size = MIN(avail_len, field_size);
7294 
7295 	if ((output_flags & SCSI_ATTR_OUTPUT_TEXT_MASK) ==
7296 	     SCSI_ATTR_OUTPUT_TEXT_RAW)
7297 		esc_text = 0;
7298 
7299 	if (print_size > 0) {
7300 		uint32_t i;
7301 
7302 		for (i = 0; i < print_size; i++) {
7303 			if (hdr->attribute[i] == '\0')
7304 				continue;
7305 			else if (((unsigned char)hdr->attribute[i] < 0x80)
7306 			      || (esc_text == 0))
7307 				sbuf_putc(sb, hdr->attribute[i]);
7308 			else
7309 				sbuf_printf(sb, "%%%02x",
7310 				    (unsigned char)hdr->attribute[i]);
7311 		}
7312 	} else if (avail_len < field_size) {
7313 		/*
7314 		 * We only report an error if the user didn't allocate
7315 		 * enough space to hold the full value of this field.
7316 		 */
7317 		if (error_str != NULL) {
7318 			snprintf(error_str, error_str_len, "Available "
7319 				 "length of attribute ID 0x%.4x %zu < field "
7320 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7321 				 field_size);
7322 		}
7323 		retval = 1;
7324 	}
7325 
7326 	return (retval);
7327 }
7328 
7329 struct scsi_attrib_table_entry *
7330 scsi_find_attrib_entry(struct scsi_attrib_table_entry *table,
7331 		       size_t num_table_entries, uint32_t id)
7332 {
7333 	uint32_t i;
7334 
7335 	for (i = 0; i < num_table_entries; i++) {
7336 		if (table[i].id == id)
7337 			return (&table[i]);
7338 	}
7339 
7340 	return (NULL);
7341 }
7342 
7343 struct scsi_attrib_table_entry *
7344 scsi_get_attrib_entry(uint32_t id)
7345 {
7346 	return (scsi_find_attrib_entry(scsi_mam_attr_table,
7347 	    nitems(scsi_mam_attr_table), id));
7348 }
7349 
7350 int
7351 scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len,
7352    struct scsi_mam_attribute_header *hdr, uint32_t output_flags,
7353    char *error_str, size_t error_str_len)
7354 {
7355 	int retval;
7356 
7357 	switch (hdr->byte2 & SMA_FORMAT_MASK) {
7358 	case SMA_FORMAT_ASCII:
7359 		retval = scsi_attrib_ascii_sbuf(sb, hdr, valid_len,
7360 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,error_str_len);
7361 		break;
7362 	case SMA_FORMAT_BINARY:
7363 		if (scsi_2btoul(hdr->length) <= 8)
7364 			retval = scsi_attrib_int_sbuf(sb, hdr, valid_len,
7365 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7366 			    error_str_len);
7367 		else
7368 			retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7369 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7370 			    error_str_len);
7371 		break;
7372 	case SMA_FORMAT_TEXT:
7373 		retval = scsi_attrib_text_sbuf(sb, hdr, valid_len,
7374 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7375 		    error_str_len);
7376 		break;
7377 	default:
7378 		if (error_str != NULL) {
7379 			snprintf(error_str, error_str_len, "Unknown attribute "
7380 			    "format 0x%x", hdr->byte2 & SMA_FORMAT_MASK);
7381 		}
7382 		retval = 1;
7383 		goto bailout;
7384 		break; /*NOTREACHED*/
7385 	}
7386 
7387 	sbuf_trim(sb);
7388 
7389 bailout:
7390 
7391 	return (retval);
7392 }
7393 
7394 void
7395 scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags,
7396 			struct scsi_mam_attribute_header *hdr,
7397 			uint32_t valid_len, const char *desc)
7398 {
7399 	int need_space = 0;
7400 	uint32_t len;
7401 	uint32_t id;
7402 
7403 	/*
7404 	 * We can't do anything if we don't have enough valid data for the
7405 	 * header.
7406 	 */
7407 	if (valid_len < sizeof(*hdr))
7408 		return;
7409 
7410 	id = scsi_2btoul(hdr->id);
7411 	/*
7412 	 * Note that we print out the value of the attribute listed in the
7413 	 * header, regardless of whether we actually got that many bytes
7414 	 * back from the device through the controller.  A truncated result
7415 	 * could be the result of a failure to ask for enough data; the
7416 	 * header indicates how many bytes are allocated for this attribute
7417 	 * in the MAM.
7418 	 */
7419 	len = scsi_2btoul(hdr->length);
7420 
7421 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_MASK) ==
7422 	    SCSI_ATTR_OUTPUT_FIELD_NONE)
7423 		return;
7424 
7425 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_DESC)
7426 	 && (desc != NULL)) {
7427 		sbuf_printf(sb, "%s", desc);
7428 		need_space = 1;
7429 	}
7430 
7431 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_NUM) {
7432 		sbuf_printf(sb, "%s(0x%.4x)", (need_space) ? " " : "", id);
7433 		need_space = 0;
7434 	}
7435 
7436 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_SIZE) {
7437 		sbuf_printf(sb, "%s[%d]", (need_space) ? " " : "", len);
7438 		need_space = 0;
7439 	}
7440 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_RW) {
7441 		sbuf_printf(sb, "%s(%s)", (need_space) ? " " : "",
7442 			    (hdr->byte2 & SMA_READ_ONLY) ? "RO" : "RW");
7443 	}
7444 	sbuf_printf(sb, ": ");
7445 }
7446 
7447 int
7448 scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7449 		 uint32_t valid_len, struct scsi_attrib_table_entry *user_table,
7450 		 size_t num_user_entries, int prefer_user_table,
7451 		 uint32_t output_flags, char *error_str, int error_str_len)
7452 {
7453 	int retval;
7454 	struct scsi_attrib_table_entry *table1 = NULL, *table2 = NULL;
7455 	struct scsi_attrib_table_entry *entry = NULL;
7456 	size_t table1_size = 0, table2_size = 0;
7457 	uint32_t id;
7458 
7459 	retval = 0;
7460 
7461 	if (valid_len < sizeof(*hdr)) {
7462 		retval = 1;
7463 		goto bailout;
7464 	}
7465 
7466 	id = scsi_2btoul(hdr->id);
7467 
7468 	if (user_table != NULL) {
7469 		if (prefer_user_table != 0) {
7470 			table1 = user_table;
7471 			table1_size = num_user_entries;
7472 			table2 = scsi_mam_attr_table;
7473 			table2_size = nitems(scsi_mam_attr_table);
7474 		} else {
7475 			table1 = scsi_mam_attr_table;
7476 			table1_size = nitems(scsi_mam_attr_table);
7477 			table2 = user_table;
7478 			table2_size = num_user_entries;
7479 		}
7480 	} else {
7481 		table1 = scsi_mam_attr_table;
7482 		table1_size = nitems(scsi_mam_attr_table);
7483 	}
7484 
7485 	entry = scsi_find_attrib_entry(table1, table1_size, id);
7486 	if (entry != NULL) {
7487 		scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len,
7488 					entry->desc);
7489 		if (entry->to_str == NULL)
7490 			goto print_default;
7491 		retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7492 				       output_flags, error_str, error_str_len);
7493 		goto bailout;
7494 	}
7495 	if (table2 != NULL) {
7496 		entry = scsi_find_attrib_entry(table2, table2_size, id);
7497 		if (entry != NULL) {
7498 			if (entry->to_str == NULL)
7499 				goto print_default;
7500 
7501 			scsi_attrib_prefix_sbuf(sb, output_flags, hdr,
7502 						valid_len, entry->desc);
7503 			retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7504 					       output_flags, error_str,
7505 					       error_str_len);
7506 			goto bailout;
7507 		}
7508 	}
7509 
7510 	scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, NULL);
7511 
7512 print_default:
7513 	retval = scsi_attrib_value_sbuf(sb, valid_len, hdr, output_flags,
7514 	    error_str, error_str_len);
7515 bailout:
7516 	if (retval == 0) {
7517 	 	if ((entry != NULL)
7518 		 && (entry->suffix != NULL))
7519 			sbuf_printf(sb, " %s", entry->suffix);
7520 
7521 		sbuf_trim(sb);
7522 		sbuf_printf(sb, "\n");
7523 	}
7524 
7525 	return (retval);
7526 }
7527 
7528 void
7529 scsi_test_unit_ready(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 sense_len, u_int32_t timeout)
7532 {
7533 	struct scsi_test_unit_ready *scsi_cmd;
7534 
7535 	cam_fill_csio(csio,
7536 		      retries,
7537 		      cbfcnp,
7538 		      CAM_DIR_NONE,
7539 		      tag_action,
7540 		      /*data_ptr*/NULL,
7541 		      /*dxfer_len*/0,
7542 		      sense_len,
7543 		      sizeof(*scsi_cmd),
7544 		      timeout);
7545 
7546 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
7547 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7548 	scsi_cmd->opcode = TEST_UNIT_READY;
7549 }
7550 
7551 void
7552 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
7553 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7554 		   void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
7555 		   u_int8_t sense_len, u_int32_t timeout)
7556 {
7557 	struct scsi_request_sense *scsi_cmd;
7558 
7559 	cam_fill_csio(csio,
7560 		      retries,
7561 		      cbfcnp,
7562 		      CAM_DIR_IN,
7563 		      tag_action,
7564 		      data_ptr,
7565 		      dxfer_len,
7566 		      sense_len,
7567 		      sizeof(*scsi_cmd),
7568 		      timeout);
7569 
7570 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
7571 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7572 	scsi_cmd->opcode = REQUEST_SENSE;
7573 	scsi_cmd->length = dxfer_len;
7574 }
7575 
7576 void
7577 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
7578 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7579 	     u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
7580 	     int evpd, u_int8_t page_code, u_int8_t sense_len,
7581 	     u_int32_t timeout)
7582 {
7583 	struct scsi_inquiry *scsi_cmd;
7584 
7585 	cam_fill_csio(csio,
7586 		      retries,
7587 		      cbfcnp,
7588 		      /*flags*/CAM_DIR_IN,
7589 		      tag_action,
7590 		      /*data_ptr*/inq_buf,
7591 		      /*dxfer_len*/inq_len,
7592 		      sense_len,
7593 		      sizeof(*scsi_cmd),
7594 		      timeout);
7595 
7596 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
7597 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7598 	scsi_cmd->opcode = INQUIRY;
7599 	if (evpd) {
7600 		scsi_cmd->byte2 |= SI_EVPD;
7601 		scsi_cmd->page_code = page_code;
7602 	}
7603 	scsi_ulto2b(inq_len, scsi_cmd->length);
7604 }
7605 
7606 void
7607 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
7608 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7609 		u_int8_t tag_action, int dbd, u_int8_t page_code,
7610 		u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7611 		u_int8_t sense_len, u_int32_t timeout)
7612 {
7613 
7614 	scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
7615 			    page_code, page, param_buf, param_len, 0,
7616 			    sense_len, timeout);
7617 }
7618 
7619 void
7620 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
7621 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
7622 		    u_int8_t tag_action, int dbd, u_int8_t page_code,
7623 		    u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7624 		    int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
7625 {
7626 	u_int8_t cdb_len;
7627 
7628 	/*
7629 	 * Use the smallest possible command to perform the operation.
7630 	 */
7631 	if ((param_len < 256)
7632 	 && (minimum_cmd_size < 10)) {
7633 		/*
7634 		 * We can fit in a 6 byte cdb.
7635 		 */
7636 		struct scsi_mode_sense_6 *scsi_cmd;
7637 
7638 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
7639 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7640 		scsi_cmd->opcode = MODE_SENSE_6;
7641 		if (dbd != 0)
7642 			scsi_cmd->byte2 |= SMS_DBD;
7643 		scsi_cmd->page = page_code | page;
7644 		scsi_cmd->length = param_len;
7645 		cdb_len = sizeof(*scsi_cmd);
7646 	} else {
7647 		/*
7648 		 * Need a 10 byte cdb.
7649 		 */
7650 		struct scsi_mode_sense_10 *scsi_cmd;
7651 
7652 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
7653 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7654 		scsi_cmd->opcode = MODE_SENSE_10;
7655 		if (dbd != 0)
7656 			scsi_cmd->byte2 |= SMS_DBD;
7657 		scsi_cmd->page = page_code | page;
7658 		scsi_ulto2b(param_len, scsi_cmd->length);
7659 		cdb_len = sizeof(*scsi_cmd);
7660 	}
7661 	cam_fill_csio(csio,
7662 		      retries,
7663 		      cbfcnp,
7664 		      CAM_DIR_IN,
7665 		      tag_action,
7666 		      param_buf,
7667 		      param_len,
7668 		      sense_len,
7669 		      cdb_len,
7670 		      timeout);
7671 }
7672 
7673 void
7674 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
7675 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7676 		 u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7677 		 u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7678 		 u_int32_t timeout)
7679 {
7680 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
7681 			     scsi_page_fmt, save_pages, param_buf,
7682 			     param_len, 0, sense_len, timeout);
7683 }
7684 
7685 void
7686 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
7687 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
7688 		     u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7689 		     u_int8_t *param_buf, u_int32_t param_len,
7690 		     int minimum_cmd_size, u_int8_t sense_len,
7691 		     u_int32_t timeout)
7692 {
7693 	u_int8_t cdb_len;
7694 
7695 	/*
7696 	 * Use the smallest possible command to perform the operation.
7697 	 */
7698 	if ((param_len < 256)
7699 	 && (minimum_cmd_size < 10)) {
7700 		/*
7701 		 * We can fit in a 6 byte cdb.
7702 		 */
7703 		struct scsi_mode_select_6 *scsi_cmd;
7704 
7705 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
7706 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7707 		scsi_cmd->opcode = MODE_SELECT_6;
7708 		if (scsi_page_fmt != 0)
7709 			scsi_cmd->byte2 |= SMS_PF;
7710 		if (save_pages != 0)
7711 			scsi_cmd->byte2 |= SMS_SP;
7712 		scsi_cmd->length = param_len;
7713 		cdb_len = sizeof(*scsi_cmd);
7714 	} else {
7715 		/*
7716 		 * Need a 10 byte cdb.
7717 		 */
7718 		struct scsi_mode_select_10 *scsi_cmd;
7719 
7720 		scsi_cmd =
7721 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
7722 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7723 		scsi_cmd->opcode = MODE_SELECT_10;
7724 		if (scsi_page_fmt != 0)
7725 			scsi_cmd->byte2 |= SMS_PF;
7726 		if (save_pages != 0)
7727 			scsi_cmd->byte2 |= SMS_SP;
7728 		scsi_ulto2b(param_len, scsi_cmd->length);
7729 		cdb_len = sizeof(*scsi_cmd);
7730 	}
7731 	cam_fill_csio(csio,
7732 		      retries,
7733 		      cbfcnp,
7734 		      CAM_DIR_OUT,
7735 		      tag_action,
7736 		      param_buf,
7737 		      param_len,
7738 		      sense_len,
7739 		      cdb_len,
7740 		      timeout);
7741 }
7742 
7743 void
7744 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
7745 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
7746 	       u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
7747 	       int save_pages, int ppc, u_int32_t paramptr,
7748 	       u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7749 	       u_int32_t timeout)
7750 {
7751 	struct scsi_log_sense *scsi_cmd;
7752 	u_int8_t cdb_len;
7753 
7754 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
7755 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7756 	scsi_cmd->opcode = LOG_SENSE;
7757 	scsi_cmd->page = page_code | page;
7758 	if (save_pages != 0)
7759 		scsi_cmd->byte2 |= SLS_SP;
7760 	if (ppc != 0)
7761 		scsi_cmd->byte2 |= SLS_PPC;
7762 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
7763 	scsi_ulto2b(param_len, scsi_cmd->length);
7764 	cdb_len = sizeof(*scsi_cmd);
7765 
7766 	cam_fill_csio(csio,
7767 		      retries,
7768 		      cbfcnp,
7769 		      /*flags*/CAM_DIR_IN,
7770 		      tag_action,
7771 		      /*data_ptr*/param_buf,
7772 		      /*dxfer_len*/param_len,
7773 		      sense_len,
7774 		      cdb_len,
7775 		      timeout);
7776 }
7777 
7778 void
7779 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
7780 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7781 		u_int8_t tag_action, u_int8_t page_code, int save_pages,
7782 		int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
7783 		u_int8_t sense_len, u_int32_t timeout)
7784 {
7785 	struct scsi_log_select *scsi_cmd;
7786 	u_int8_t cdb_len;
7787 
7788 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
7789 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7790 	scsi_cmd->opcode = LOG_SELECT;
7791 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
7792 	if (save_pages != 0)
7793 		scsi_cmd->byte2 |= SLS_SP;
7794 	if (pc_reset != 0)
7795 		scsi_cmd->byte2 |= SLS_PCR;
7796 	scsi_ulto2b(param_len, scsi_cmd->length);
7797 	cdb_len = sizeof(*scsi_cmd);
7798 
7799 	cam_fill_csio(csio,
7800 		      retries,
7801 		      cbfcnp,
7802 		      /*flags*/CAM_DIR_OUT,
7803 		      tag_action,
7804 		      /*data_ptr*/param_buf,
7805 		      /*dxfer_len*/param_len,
7806 		      sense_len,
7807 		      cdb_len,
7808 		      timeout);
7809 }
7810 
7811 /*
7812  * Prevent or allow the user to remove the media
7813  */
7814 void
7815 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
7816 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7817 	     u_int8_t tag_action, u_int8_t action,
7818 	     u_int8_t sense_len, u_int32_t timeout)
7819 {
7820 	struct scsi_prevent *scsi_cmd;
7821 
7822 	cam_fill_csio(csio,
7823 		      retries,
7824 		      cbfcnp,
7825 		      /*flags*/CAM_DIR_NONE,
7826 		      tag_action,
7827 		      /*data_ptr*/NULL,
7828 		      /*dxfer_len*/0,
7829 		      sense_len,
7830 		      sizeof(*scsi_cmd),
7831 		      timeout);
7832 
7833 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
7834 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7835 	scsi_cmd->opcode = PREVENT_ALLOW;
7836 	scsi_cmd->how = action;
7837 }
7838 
7839 /* XXX allow specification of address and PMI bit and LBA */
7840 void
7841 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
7842 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7843 		   u_int8_t tag_action,
7844 		   struct scsi_read_capacity_data *rcap_buf,
7845 		   u_int8_t sense_len, u_int32_t timeout)
7846 {
7847 	struct scsi_read_capacity *scsi_cmd;
7848 
7849 	cam_fill_csio(csio,
7850 		      retries,
7851 		      cbfcnp,
7852 		      /*flags*/CAM_DIR_IN,
7853 		      tag_action,
7854 		      /*data_ptr*/(u_int8_t *)rcap_buf,
7855 		      /*dxfer_len*/sizeof(*rcap_buf),
7856 		      sense_len,
7857 		      sizeof(*scsi_cmd),
7858 		      timeout);
7859 
7860 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
7861 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7862 	scsi_cmd->opcode = READ_CAPACITY;
7863 }
7864 
7865 void
7866 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
7867 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
7868 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
7869 		      uint8_t *rcap_buf, int rcap_buf_len, uint8_t sense_len,
7870 		      uint32_t timeout)
7871 {
7872 	struct scsi_read_capacity_16 *scsi_cmd;
7873 
7874 
7875 	cam_fill_csio(csio,
7876 		      retries,
7877 		      cbfcnp,
7878 		      /*flags*/CAM_DIR_IN,
7879 		      tag_action,
7880 		      /*data_ptr*/(u_int8_t *)rcap_buf,
7881 		      /*dxfer_len*/rcap_buf_len,
7882 		      sense_len,
7883 		      sizeof(*scsi_cmd),
7884 		      timeout);
7885 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
7886 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7887 	scsi_cmd->opcode = SERVICE_ACTION_IN;
7888 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
7889 	scsi_u64to8b(lba, scsi_cmd->addr);
7890 	scsi_ulto4b(rcap_buf_len, scsi_cmd->alloc_len);
7891 	if (pmi)
7892 		reladr |= SRC16_PMI;
7893 	if (reladr)
7894 		reladr |= SRC16_RELADR;
7895 }
7896 
7897 void
7898 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
7899 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7900 		 u_int8_t tag_action, u_int8_t select_report,
7901 		 struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
7902 		 u_int8_t sense_len, u_int32_t timeout)
7903 {
7904 	struct scsi_report_luns *scsi_cmd;
7905 
7906 	cam_fill_csio(csio,
7907 		      retries,
7908 		      cbfcnp,
7909 		      /*flags*/CAM_DIR_IN,
7910 		      tag_action,
7911 		      /*data_ptr*/(u_int8_t *)rpl_buf,
7912 		      /*dxfer_len*/alloc_len,
7913 		      sense_len,
7914 		      sizeof(*scsi_cmd),
7915 		      timeout);
7916 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
7917 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7918 	scsi_cmd->opcode = REPORT_LUNS;
7919 	scsi_cmd->select_report = select_report;
7920 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7921 }
7922 
7923 void
7924 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7925 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7926 		 u_int8_t tag_action, u_int8_t pdf,
7927 		 void *buf, u_int32_t alloc_len,
7928 		 u_int8_t sense_len, u_int32_t timeout)
7929 {
7930 	struct scsi_target_group *scsi_cmd;
7931 
7932 	cam_fill_csio(csio,
7933 		      retries,
7934 		      cbfcnp,
7935 		      /*flags*/CAM_DIR_IN,
7936 		      tag_action,
7937 		      /*data_ptr*/(u_int8_t *)buf,
7938 		      /*dxfer_len*/alloc_len,
7939 		      sense_len,
7940 		      sizeof(*scsi_cmd),
7941 		      timeout);
7942 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7943 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7944 	scsi_cmd->opcode = MAINTENANCE_IN;
7945 	scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
7946 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7947 }
7948 
7949 void
7950 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7951 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7952 		 u_int8_t tag_action, void *buf, u_int32_t alloc_len,
7953 		 u_int8_t sense_len, u_int32_t timeout)
7954 {
7955 	struct scsi_target_group *scsi_cmd;
7956 
7957 	cam_fill_csio(csio,
7958 		      retries,
7959 		      cbfcnp,
7960 		      /*flags*/CAM_DIR_OUT,
7961 		      tag_action,
7962 		      /*data_ptr*/(u_int8_t *)buf,
7963 		      /*dxfer_len*/alloc_len,
7964 		      sense_len,
7965 		      sizeof(*scsi_cmd),
7966 		      timeout);
7967 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7968 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7969 	scsi_cmd->opcode = MAINTENANCE_OUT;
7970 	scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
7971 	scsi_ulto4b(alloc_len, scsi_cmd->length);
7972 }
7973 
7974 /*
7975  * Syncronize the media to the contents of the cache for
7976  * the given lba/count pair.  Specifying 0/0 means sync
7977  * the whole cache.
7978  */
7979 void
7980 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
7981 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
7982 		       u_int8_t tag_action, u_int32_t begin_lba,
7983 		       u_int16_t lb_count, u_int8_t sense_len,
7984 		       u_int32_t timeout)
7985 {
7986 	struct scsi_sync_cache *scsi_cmd;
7987 
7988 	cam_fill_csio(csio,
7989 		      retries,
7990 		      cbfcnp,
7991 		      /*flags*/CAM_DIR_NONE,
7992 		      tag_action,
7993 		      /*data_ptr*/NULL,
7994 		      /*dxfer_len*/0,
7995 		      sense_len,
7996 		      sizeof(*scsi_cmd),
7997 		      timeout);
7998 
7999 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
8000 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8001 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
8002 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
8003 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
8004 }
8005 
8006 void
8007 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
8008 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8009 		u_int8_t tag_action, int readop, u_int8_t byte2,
8010 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
8011 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
8012 		u_int32_t timeout)
8013 {
8014 	int read;
8015 	u_int8_t cdb_len;
8016 
8017 	read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
8018 
8019 	/*
8020 	 * Use the smallest possible command to perform the operation
8021 	 * as some legacy hardware does not support the 10 byte commands.
8022 	 * If any of the bits in byte2 is set, we have to go with a larger
8023 	 * command.
8024 	 */
8025 	if ((minimum_cmd_size < 10)
8026 	 && ((lba & 0x1fffff) == lba)
8027 	 && ((block_count & 0xff) == block_count)
8028 	 && (byte2 == 0)) {
8029 		/*
8030 		 * We can fit in a 6 byte cdb.
8031 		 */
8032 		struct scsi_rw_6 *scsi_cmd;
8033 
8034 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
8035 		scsi_cmd->opcode = read ? READ_6 : WRITE_6;
8036 		scsi_ulto3b(lba, scsi_cmd->addr);
8037 		scsi_cmd->length = block_count & 0xff;
8038 		scsi_cmd->control = 0;
8039 		cdb_len = sizeof(*scsi_cmd);
8040 
8041 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8042 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
8043 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8044 			   scsi_cmd->length, dxfer_len));
8045 	} else if ((minimum_cmd_size < 12)
8046 		&& ((block_count & 0xffff) == block_count)
8047 		&& ((lba & 0xffffffff) == lba)) {
8048 		/*
8049 		 * Need a 10 byte cdb.
8050 		 */
8051 		struct scsi_rw_10 *scsi_cmd;
8052 
8053 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
8054 		scsi_cmd->opcode = read ? READ_10 : WRITE_10;
8055 		scsi_cmd->byte2 = byte2;
8056 		scsi_ulto4b(lba, scsi_cmd->addr);
8057 		scsi_cmd->reserved = 0;
8058 		scsi_ulto2b(block_count, scsi_cmd->length);
8059 		scsi_cmd->control = 0;
8060 		cdb_len = sizeof(*scsi_cmd);
8061 
8062 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8063 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8064 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8065 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8066 			   scsi_cmd->length[1], dxfer_len));
8067 	} else if ((minimum_cmd_size < 16)
8068 		&& ((block_count & 0xffffffff) == block_count)
8069 		&& ((lba & 0xffffffff) == lba)) {
8070 		/*
8071 		 * The block count is too big for a 10 byte CDB, use a 12
8072 		 * byte CDB.
8073 		 */
8074 		struct scsi_rw_12 *scsi_cmd;
8075 
8076 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
8077 		scsi_cmd->opcode = read ? READ_12 : WRITE_12;
8078 		scsi_cmd->byte2 = byte2;
8079 		scsi_ulto4b(lba, scsi_cmd->addr);
8080 		scsi_cmd->reserved = 0;
8081 		scsi_ulto4b(block_count, scsi_cmd->length);
8082 		scsi_cmd->control = 0;
8083 		cdb_len = sizeof(*scsi_cmd);
8084 
8085 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8086 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
8087 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8088 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8089 			   scsi_cmd->length[1], scsi_cmd->length[2],
8090 			   scsi_cmd->length[3], dxfer_len));
8091 	} else {
8092 		/*
8093 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8094 		 * than 2^32, or if the user asks for a 16 byte command.
8095 		 */
8096 		struct scsi_rw_16 *scsi_cmd;
8097 
8098 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
8099 		scsi_cmd->opcode = read ? READ_16 : WRITE_16;
8100 		scsi_cmd->byte2 = byte2;
8101 		scsi_u64to8b(lba, scsi_cmd->addr);
8102 		scsi_cmd->reserved = 0;
8103 		scsi_ulto4b(block_count, scsi_cmd->length);
8104 		scsi_cmd->control = 0;
8105 		cdb_len = sizeof(*scsi_cmd);
8106 	}
8107 	cam_fill_csio(csio,
8108 		      retries,
8109 		      cbfcnp,
8110 		      (read ? CAM_DIR_IN : CAM_DIR_OUT) |
8111 		      ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
8112 		      tag_action,
8113 		      data_ptr,
8114 		      dxfer_len,
8115 		      sense_len,
8116 		      cdb_len,
8117 		      timeout);
8118 }
8119 
8120 void
8121 scsi_write_same(struct ccb_scsiio *csio, u_int32_t retries,
8122 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8123 		u_int8_t tag_action, u_int8_t byte2,
8124 		int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
8125 		u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
8126 		u_int32_t timeout)
8127 {
8128 	u_int8_t cdb_len;
8129 	if ((minimum_cmd_size < 16) &&
8130 	    ((block_count & 0xffff) == block_count) &&
8131 	    ((lba & 0xffffffff) == lba)) {
8132 		/*
8133 		 * Need a 10 byte cdb.
8134 		 */
8135 		struct scsi_write_same_10 *scsi_cmd;
8136 
8137 		scsi_cmd = (struct scsi_write_same_10 *)&csio->cdb_io.cdb_bytes;
8138 		scsi_cmd->opcode = WRITE_SAME_10;
8139 		scsi_cmd->byte2 = byte2;
8140 		scsi_ulto4b(lba, scsi_cmd->addr);
8141 		scsi_cmd->group = 0;
8142 		scsi_ulto2b(block_count, scsi_cmd->length);
8143 		scsi_cmd->control = 0;
8144 		cdb_len = sizeof(*scsi_cmd);
8145 
8146 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8147 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8148 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8149 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8150 			   scsi_cmd->length[1], dxfer_len));
8151 	} else {
8152 		/*
8153 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8154 		 * than 2^32, or if the user asks for a 16 byte command.
8155 		 */
8156 		struct scsi_write_same_16 *scsi_cmd;
8157 
8158 		scsi_cmd = (struct scsi_write_same_16 *)&csio->cdb_io.cdb_bytes;
8159 		scsi_cmd->opcode = WRITE_SAME_16;
8160 		scsi_cmd->byte2 = byte2;
8161 		scsi_u64to8b(lba, scsi_cmd->addr);
8162 		scsi_ulto4b(block_count, scsi_cmd->length);
8163 		scsi_cmd->group = 0;
8164 		scsi_cmd->control = 0;
8165 		cdb_len = sizeof(*scsi_cmd);
8166 
8167 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8168 			  ("16byte: %x%x%x%x%x%x%x%x:%x%x%x%x: %d\n",
8169 			   scsi_cmd->addr[0], scsi_cmd->addr[1],
8170 			   scsi_cmd->addr[2], scsi_cmd->addr[3],
8171 			   scsi_cmd->addr[4], scsi_cmd->addr[5],
8172 			   scsi_cmd->addr[6], scsi_cmd->addr[7],
8173 			   scsi_cmd->length[0], scsi_cmd->length[1],
8174 			   scsi_cmd->length[2], scsi_cmd->length[3],
8175 			   dxfer_len));
8176 	}
8177 	cam_fill_csio(csio,
8178 		      retries,
8179 		      cbfcnp,
8180 		      /*flags*/CAM_DIR_OUT,
8181 		      tag_action,
8182 		      data_ptr,
8183 		      dxfer_len,
8184 		      sense_len,
8185 		      cdb_len,
8186 		      timeout);
8187 }
8188 
8189 void
8190 scsi_ata_identify(struct ccb_scsiio *csio, u_int32_t retries,
8191 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8192 		  u_int8_t tag_action, u_int8_t *data_ptr,
8193 		  u_int16_t dxfer_len, u_int8_t sense_len,
8194 		  u_int32_t timeout)
8195 {
8196 	scsi_ata_pass(csio,
8197 		      retries,
8198 		      cbfcnp,
8199 		      /*flags*/CAM_DIR_IN,
8200 		      tag_action,
8201 		      /*protocol*/AP_PROTO_PIO_IN,
8202 		      /*ata_flags*/AP_FLAG_TDIR_FROM_DEV |
8203 				   AP_FLAG_BYT_BLOK_BYTES |
8204 				   AP_FLAG_TLEN_SECT_CNT,
8205 		      /*features*/0,
8206 		      /*sector_count*/dxfer_len,
8207 		      /*lba*/0,
8208 		      /*command*/ATA_ATA_IDENTIFY,
8209 		      /*device*/ 0,
8210 		      /*icc*/ 0,
8211 		      /*auxiliary*/ 0,
8212 		      /*control*/0,
8213 		      data_ptr,
8214 		      dxfer_len,
8215 		      /*cdb_storage*/ NULL,
8216 		      /*cdb_storage_len*/ 0,
8217 		      /*minimum_cmd_size*/ 0,
8218 		      sense_len,
8219 		      timeout);
8220 }
8221 
8222 void
8223 scsi_ata_trim(struct ccb_scsiio *csio, u_int32_t retries,
8224 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8225 	      u_int8_t tag_action, u_int16_t block_count,
8226 	      u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8227 	      u_int32_t timeout)
8228 {
8229 	scsi_ata_pass_16(csio,
8230 			 retries,
8231 			 cbfcnp,
8232 			 /*flags*/CAM_DIR_OUT,
8233 			 tag_action,
8234 			 /*protocol*/AP_EXTEND|AP_PROTO_DMA,
8235 			 /*ata_flags*/AP_FLAG_TLEN_SECT_CNT|AP_FLAG_BYT_BLOK_BLOCKS,
8236 			 /*features*/ATA_DSM_TRIM,
8237 			 /*sector_count*/block_count,
8238 			 /*lba*/0,
8239 			 /*command*/ATA_DATA_SET_MANAGEMENT,
8240 			 /*control*/0,
8241 			 data_ptr,
8242 			 dxfer_len,
8243 			 sense_len,
8244 			 timeout);
8245 }
8246 
8247 int
8248 scsi_ata_read_log(struct ccb_scsiio *csio, uint32_t retries,
8249 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8250 		  uint8_t tag_action, uint32_t log_address,
8251 		  uint32_t page_number, uint16_t block_count,
8252 		  uint8_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
8253 		  uint8_t sense_len, uint32_t timeout)
8254 {
8255 	uint8_t command, protocol_out;
8256 	uint16_t count_out;
8257 	uint64_t lba;
8258 	int retval;
8259 
8260 	retval = 0;
8261 
8262 	switch (protocol) {
8263 	case AP_PROTO_DMA:
8264 		count_out = block_count;
8265 		command = ATA_READ_LOG_DMA_EXT;
8266 		protocol_out = AP_PROTO_DMA;
8267 		break;
8268 	case AP_PROTO_PIO_IN:
8269 	default:
8270 		count_out = block_count;
8271 		command = ATA_READ_LOG_EXT;
8272 		protocol_out = AP_PROTO_PIO_IN;
8273 		break;
8274 	}
8275 
8276 	lba = (((uint64_t)page_number & 0xff00) << 32) |
8277 	      ((page_number & 0x00ff) << 8) |
8278 	      (log_address & 0xff);
8279 
8280 	protocol_out |= AP_EXTEND;
8281 
8282 	retval = scsi_ata_pass(csio,
8283 			       retries,
8284 			       cbfcnp,
8285 			       /*flags*/CAM_DIR_IN,
8286 			       tag_action,
8287 			       /*protocol*/ protocol_out,
8288 			       /*ata_flags*/AP_FLAG_TLEN_SECT_CNT |
8289 					    AP_FLAG_BYT_BLOK_BLOCKS |
8290 					    AP_FLAG_TDIR_FROM_DEV,
8291 			       /*feature*/ 0,
8292 			       /*sector_count*/ count_out,
8293 			       /*lba*/ lba,
8294 			       /*command*/ command,
8295 			       /*device*/ 0,
8296 			       /*icc*/ 0,
8297 			       /*auxiliary*/ 0,
8298 			       /*control*/0,
8299 			       data_ptr,
8300 			       dxfer_len,
8301 			       /*cdb_storage*/ NULL,
8302 			       /*cdb_storage_len*/ 0,
8303 			       /*minimum_cmd_size*/ 0,
8304 			       sense_len,
8305 			       timeout);
8306 
8307 	return (retval);
8308 }
8309 
8310 /*
8311  * Note! This is an unusual CDB building function because it can return
8312  * an error in the event that the command in question requires a variable
8313  * length CDB, but the caller has not given storage space for one or has not
8314  * given enough storage space.  If there is enough space available in the
8315  * standard SCSI CCB CDB bytes, we'll prefer that over passed in storage.
8316  */
8317 int
8318 scsi_ata_pass(struct ccb_scsiio *csio, uint32_t retries,
8319 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8320 	      uint32_t flags, uint8_t tag_action,
8321 	      uint8_t protocol, uint8_t ata_flags, uint16_t features,
8322 	      uint16_t sector_count, uint64_t lba, uint8_t command,
8323 	      uint8_t device, uint8_t icc, uint32_t auxiliary,
8324 	      uint8_t control, u_int8_t *data_ptr, uint32_t dxfer_len,
8325 	      uint8_t *cdb_storage, size_t cdb_storage_len,
8326 	      int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
8327 {
8328 	uint32_t cam_flags;
8329 	uint8_t *cdb_ptr;
8330 	int cmd_size;
8331 	int retval;
8332 	uint8_t cdb_len;
8333 
8334 	retval = 0;
8335 	cam_flags = flags;
8336 
8337 	/*
8338 	 * Round the user's request to the nearest command size that is at
8339 	 * least as big as what he requested.
8340 	 */
8341 	if (minimum_cmd_size <= 12)
8342 		cmd_size = 12;
8343 	else if (minimum_cmd_size > 16)
8344 		cmd_size = 32;
8345 	else
8346 		cmd_size = 16;
8347 
8348 	/*
8349 	 * If we have parameters that require a 48-bit ATA command, we have to
8350 	 * use the 16 byte ATA PASS-THROUGH command at least.
8351 	 */
8352 	if (((lba > ATA_MAX_28BIT_LBA)
8353 	  || (sector_count > 255)
8354 	  || (features > 255)
8355 	  || (protocol & AP_EXTEND))
8356 	 && ((cmd_size < 16)
8357 	  || ((protocol & AP_EXTEND) == 0))) {
8358 		if (cmd_size < 16)
8359 			cmd_size = 16;
8360 		protocol |= AP_EXTEND;
8361 	}
8362 
8363 	/*
8364 	 * The icc and auxiliary ATA registers are only supported in the
8365 	 * 32-byte version of the ATA PASS-THROUGH command.
8366 	 */
8367 	if ((icc != 0)
8368 	 || (auxiliary != 0)) {
8369 		cmd_size = 32;
8370 		protocol |= AP_EXTEND;
8371 	}
8372 
8373 
8374 	if ((cmd_size > sizeof(csio->cdb_io.cdb_bytes))
8375 	 && ((cdb_storage == NULL)
8376 	  || (cdb_storage_len < cmd_size))) {
8377 		retval = 1;
8378 		goto bailout;
8379 	}
8380 
8381 	/*
8382 	 * At this point we know we have enough space to store the command
8383 	 * in one place or another.  We prefer the built-in array, but used
8384 	 * the passed in storage if necessary.
8385 	 */
8386 	if (cmd_size <= sizeof(csio->cdb_io.cdb_bytes))
8387 		cdb_ptr = csio->cdb_io.cdb_bytes;
8388 	else {
8389 		cdb_ptr = cdb_storage;
8390 		cam_flags |= CAM_CDB_POINTER;
8391 	}
8392 
8393 	if (cmd_size <= 12) {
8394 		struct ata_pass_12 *cdb;
8395 
8396 		cdb = (struct ata_pass_12 *)cdb_ptr;
8397 		cdb_len = sizeof(*cdb);
8398 		bzero(cdb, cdb_len);
8399 
8400 		cdb->opcode = ATA_PASS_12;
8401 		cdb->protocol = protocol;
8402 		cdb->flags = ata_flags;
8403 		cdb->features = features;
8404 		cdb->sector_count = sector_count;
8405 		cdb->lba_low = lba & 0xff;
8406 		cdb->lba_mid = (lba >> 8) & 0xff;
8407 		cdb->lba_high = (lba >> 16) & 0xff;
8408 		cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8409 		cdb->command = command;
8410 		cdb->control = control;
8411 	} else if (cmd_size <= 16) {
8412 		struct ata_pass_16 *cdb;
8413 
8414 		cdb = (struct ata_pass_16 *)cdb_ptr;
8415 		cdb_len = sizeof(*cdb);
8416 		bzero(cdb, cdb_len);
8417 
8418 		cdb->opcode = ATA_PASS_16;
8419 		cdb->protocol = protocol;
8420 		cdb->flags = ata_flags;
8421 		cdb->features = features & 0xff;
8422 		cdb->sector_count = sector_count & 0xff;
8423 		cdb->lba_low = lba & 0xff;
8424 		cdb->lba_mid = (lba >> 8) & 0xff;
8425 		cdb->lba_high = (lba >> 16) & 0xff;
8426 		/*
8427 		 * If AP_EXTEND is set, we're sending a 48-bit command.
8428 		 * Otherwise it's a 28-bit command.
8429 		 */
8430 		if (protocol & AP_EXTEND) {
8431 			cdb->lba_low_ext = (lba >> 24) & 0xff;
8432 			cdb->lba_mid_ext = (lba >> 32) & 0xff;
8433 			cdb->lba_high_ext = (lba >> 40) & 0xff;
8434 			cdb->features_ext = (features >> 8) & 0xff;
8435 			cdb->sector_count_ext = (sector_count >> 8) & 0xff;
8436 			cdb->device = device | ATA_DEV_LBA;
8437 		} else {
8438 			cdb->lba_low_ext = (lba >> 24) & 0xf;
8439 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8440 		}
8441 		cdb->command = command;
8442 		cdb->control = control;
8443 	} else {
8444 		struct ata_pass_32 *cdb;
8445 		uint8_t tmp_lba[8];
8446 
8447 		cdb = (struct ata_pass_32 *)cdb_ptr;
8448 		cdb_len = sizeof(*cdb);
8449 		bzero(cdb, cdb_len);
8450 		cdb->opcode = VARIABLE_LEN_CDB;
8451 		cdb->control = control;
8452 		cdb->length = sizeof(*cdb) - __offsetof(struct ata_pass_32,
8453 							service_action);
8454 		scsi_ulto2b(ATA_PASS_32_SA, cdb->service_action);
8455 		cdb->protocol = protocol;
8456 		cdb->flags = ata_flags;
8457 
8458 		if ((protocol & AP_EXTEND) == 0) {
8459 			lba &= 0x0fffffff;
8460 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8461 			features &= 0xff;
8462 			sector_count &= 0xff;
8463 		} else {
8464 			cdb->device = device | ATA_DEV_LBA;
8465 		}
8466 		scsi_u64to8b(lba, tmp_lba);
8467 		bcopy(&tmp_lba[2], cdb->lba, sizeof(cdb->lba));
8468 		scsi_ulto2b(features, cdb->features);
8469 		scsi_ulto2b(sector_count, cdb->count);
8470 		cdb->command = command;
8471 		cdb->icc = icc;
8472 		scsi_ulto4b(auxiliary, cdb->auxiliary);
8473 	}
8474 
8475 	cam_fill_csio(csio,
8476 		      retries,
8477 		      cbfcnp,
8478 		      cam_flags,
8479 		      tag_action,
8480 		      data_ptr,
8481 		      dxfer_len,
8482 		      sense_len,
8483 		      cmd_size,
8484 		      timeout);
8485 bailout:
8486 	return (retval);
8487 }
8488 
8489 void
8490 scsi_ata_pass_16(struct ccb_scsiio *csio, u_int32_t retries,
8491 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8492 		 u_int32_t flags, u_int8_t tag_action,
8493 		 u_int8_t protocol, u_int8_t ata_flags, u_int16_t features,
8494 		 u_int16_t sector_count, uint64_t lba, u_int8_t command,
8495 		 u_int8_t control, u_int8_t *data_ptr, u_int16_t dxfer_len,
8496 		 u_int8_t sense_len, u_int32_t timeout)
8497 {
8498 	struct ata_pass_16 *ata_cmd;
8499 
8500 	ata_cmd = (struct ata_pass_16 *)&csio->cdb_io.cdb_bytes;
8501 	ata_cmd->opcode = ATA_PASS_16;
8502 	ata_cmd->protocol = protocol;
8503 	ata_cmd->flags = ata_flags;
8504 	ata_cmd->features_ext = features >> 8;
8505 	ata_cmd->features = features;
8506 	ata_cmd->sector_count_ext = sector_count >> 8;
8507 	ata_cmd->sector_count = sector_count;
8508 	ata_cmd->lba_low = lba;
8509 	ata_cmd->lba_mid = lba >> 8;
8510 	ata_cmd->lba_high = lba >> 16;
8511 	ata_cmd->device = ATA_DEV_LBA;
8512 	if (protocol & AP_EXTEND) {
8513 		ata_cmd->lba_low_ext = lba >> 24;
8514 		ata_cmd->lba_mid_ext = lba >> 32;
8515 		ata_cmd->lba_high_ext = lba >> 40;
8516 	} else
8517 		ata_cmd->device |= (lba >> 24) & 0x0f;
8518 	ata_cmd->command = command;
8519 	ata_cmd->control = control;
8520 
8521 	cam_fill_csio(csio,
8522 		      retries,
8523 		      cbfcnp,
8524 		      flags,
8525 		      tag_action,
8526 		      data_ptr,
8527 		      dxfer_len,
8528 		      sense_len,
8529 		      sizeof(*ata_cmd),
8530 		      timeout);
8531 }
8532 
8533 void
8534 scsi_unmap(struct ccb_scsiio *csio, u_int32_t retries,
8535 	   void (*cbfcnp)(struct cam_periph *, union ccb *),
8536 	   u_int8_t tag_action, u_int8_t byte2,
8537 	   u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8538 	   u_int32_t timeout)
8539 {
8540 	struct scsi_unmap *scsi_cmd;
8541 
8542 	scsi_cmd = (struct scsi_unmap *)&csio->cdb_io.cdb_bytes;
8543 	scsi_cmd->opcode = UNMAP;
8544 	scsi_cmd->byte2 = byte2;
8545 	scsi_ulto4b(0, scsi_cmd->reserved);
8546 	scsi_cmd->group = 0;
8547 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
8548 	scsi_cmd->control = 0;
8549 
8550 	cam_fill_csio(csio,
8551 		      retries,
8552 		      cbfcnp,
8553 		      /*flags*/CAM_DIR_OUT,
8554 		      tag_action,
8555 		      data_ptr,
8556 		      dxfer_len,
8557 		      sense_len,
8558 		      sizeof(*scsi_cmd),
8559 		      timeout);
8560 }
8561 
8562 void
8563 scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
8564 				void (*cbfcnp)(struct cam_periph *, union ccb*),
8565 				uint8_t tag_action, int pcv, uint8_t page_code,
8566 				uint8_t *data_ptr, uint16_t allocation_length,
8567 				uint8_t sense_len, uint32_t timeout)
8568 {
8569 	struct scsi_receive_diag *scsi_cmd;
8570 
8571 	scsi_cmd = (struct scsi_receive_diag *)&csio->cdb_io.cdb_bytes;
8572 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8573 	scsi_cmd->opcode = RECEIVE_DIAGNOSTIC;
8574 	if (pcv) {
8575 		scsi_cmd->byte2 |= SRD_PCV;
8576 		scsi_cmd->page_code = page_code;
8577 	}
8578 	scsi_ulto2b(allocation_length, scsi_cmd->length);
8579 
8580 	cam_fill_csio(csio,
8581 		      retries,
8582 		      cbfcnp,
8583 		      /*flags*/CAM_DIR_IN,
8584 		      tag_action,
8585 		      data_ptr,
8586 		      allocation_length,
8587 		      sense_len,
8588 		      sizeof(*scsi_cmd),
8589 		      timeout);
8590 }
8591 
8592 void
8593 scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
8594 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
8595 		     uint8_t tag_action, int unit_offline, int device_offline,
8596 		     int self_test, int page_format, int self_test_code,
8597 		     uint8_t *data_ptr, uint16_t param_list_length,
8598 		     uint8_t sense_len, uint32_t timeout)
8599 {
8600 	struct scsi_send_diag *scsi_cmd;
8601 
8602 	scsi_cmd = (struct scsi_send_diag *)&csio->cdb_io.cdb_bytes;
8603 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8604 	scsi_cmd->opcode = SEND_DIAGNOSTIC;
8605 
8606 	/*
8607 	 * The default self-test mode control and specific test
8608 	 * control are mutually exclusive.
8609 	 */
8610 	if (self_test)
8611 		self_test_code = SSD_SELF_TEST_CODE_NONE;
8612 
8613 	scsi_cmd->byte2 = ((self_test_code << SSD_SELF_TEST_CODE_SHIFT)
8614 			 & SSD_SELF_TEST_CODE_MASK)
8615 			| (unit_offline   ? SSD_UNITOFFL : 0)
8616 			| (device_offline ? SSD_DEVOFFL  : 0)
8617 			| (self_test      ? SSD_SELFTEST : 0)
8618 			| (page_format    ? SSD_PF       : 0);
8619 	scsi_ulto2b(param_list_length, scsi_cmd->length);
8620 
8621 	cam_fill_csio(csio,
8622 		      retries,
8623 		      cbfcnp,
8624 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8625 		      tag_action,
8626 		      data_ptr,
8627 		      param_list_length,
8628 		      sense_len,
8629 		      sizeof(*scsi_cmd),
8630 		      timeout);
8631 }
8632 
8633 void
8634 scsi_read_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8635 			void (*cbfcnp)(struct cam_periph *, union ccb*),
8636 			uint8_t tag_action, int mode,
8637 			uint8_t buffer_id, u_int32_t offset,
8638 			uint8_t *data_ptr, uint32_t allocation_length,
8639 			uint8_t sense_len, uint32_t timeout)
8640 {
8641 	struct scsi_read_buffer *scsi_cmd;
8642 
8643 	scsi_cmd = (struct scsi_read_buffer *)&csio->cdb_io.cdb_bytes;
8644 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8645 	scsi_cmd->opcode = READ_BUFFER;
8646 	scsi_cmd->byte2 = mode;
8647 	scsi_cmd->buffer_id = buffer_id;
8648 	scsi_ulto3b(offset, scsi_cmd->offset);
8649 	scsi_ulto3b(allocation_length, scsi_cmd->length);
8650 
8651 	cam_fill_csio(csio,
8652 		      retries,
8653 		      cbfcnp,
8654 		      /*flags*/CAM_DIR_IN,
8655 		      tag_action,
8656 		      data_ptr,
8657 		      allocation_length,
8658 		      sense_len,
8659 		      sizeof(*scsi_cmd),
8660 		      timeout);
8661 }
8662 
8663 void
8664 scsi_write_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8665 			void (*cbfcnp)(struct cam_periph *, union ccb *),
8666 			uint8_t tag_action, int mode,
8667 			uint8_t buffer_id, u_int32_t offset,
8668 			uint8_t *data_ptr, uint32_t param_list_length,
8669 			uint8_t sense_len, uint32_t timeout)
8670 {
8671 	struct scsi_write_buffer *scsi_cmd;
8672 
8673 	scsi_cmd = (struct scsi_write_buffer *)&csio->cdb_io.cdb_bytes;
8674 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8675 	scsi_cmd->opcode = WRITE_BUFFER;
8676 	scsi_cmd->byte2 = mode;
8677 	scsi_cmd->buffer_id = buffer_id;
8678 	scsi_ulto3b(offset, scsi_cmd->offset);
8679 	scsi_ulto3b(param_list_length, scsi_cmd->length);
8680 
8681 	cam_fill_csio(csio,
8682 		      retries,
8683 		      cbfcnp,
8684 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8685 		      tag_action,
8686 		      data_ptr,
8687 		      param_list_length,
8688 		      sense_len,
8689 		      sizeof(*scsi_cmd),
8690 		      timeout);
8691 }
8692 
8693 void
8694 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
8695 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8696 		u_int8_t tag_action, int start, int load_eject,
8697 		int immediate, u_int8_t sense_len, u_int32_t timeout)
8698 {
8699 	struct scsi_start_stop_unit *scsi_cmd;
8700 	int extra_flags = 0;
8701 
8702 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
8703 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8704 	scsi_cmd->opcode = START_STOP_UNIT;
8705 	if (start != 0) {
8706 		scsi_cmd->how |= SSS_START;
8707 		/* it takes a lot of power to start a drive */
8708 		extra_flags |= CAM_HIGH_POWER;
8709 	}
8710 	if (load_eject != 0)
8711 		scsi_cmd->how |= SSS_LOEJ;
8712 	if (immediate != 0)
8713 		scsi_cmd->byte2 |= SSS_IMMED;
8714 
8715 	cam_fill_csio(csio,
8716 		      retries,
8717 		      cbfcnp,
8718 		      /*flags*/CAM_DIR_NONE | extra_flags,
8719 		      tag_action,
8720 		      /*data_ptr*/NULL,
8721 		      /*dxfer_len*/0,
8722 		      sense_len,
8723 		      sizeof(*scsi_cmd),
8724 		      timeout);
8725 }
8726 
8727 void
8728 scsi_read_attribute(struct ccb_scsiio *csio, u_int32_t retries,
8729 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
8730 		    u_int8_t tag_action, u_int8_t service_action,
8731 		    uint32_t element, u_int8_t elem_type, int logical_volume,
8732 		    int partition, u_int32_t first_attribute, int cache,
8733 		    u_int8_t *data_ptr, u_int32_t length, int sense_len,
8734 		    u_int32_t timeout)
8735 {
8736 	struct scsi_read_attribute *scsi_cmd;
8737 
8738 	scsi_cmd = (struct scsi_read_attribute *)&csio->cdb_io.cdb_bytes;
8739 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8740 
8741 	scsi_cmd->opcode = READ_ATTRIBUTE;
8742 	scsi_cmd->service_action = service_action;
8743 	scsi_ulto2b(element, scsi_cmd->element);
8744 	scsi_cmd->elem_type = elem_type;
8745 	scsi_cmd->logical_volume = logical_volume;
8746 	scsi_cmd->partition = partition;
8747 	scsi_ulto2b(first_attribute, scsi_cmd->first_attribute);
8748 	scsi_ulto4b(length, scsi_cmd->length);
8749 	if (cache != 0)
8750 		scsi_cmd->cache |= SRA_CACHE;
8751 
8752 	cam_fill_csio(csio,
8753 		      retries,
8754 		      cbfcnp,
8755 		      /*flags*/CAM_DIR_IN,
8756 		      tag_action,
8757 		      /*data_ptr*/data_ptr,
8758 		      /*dxfer_len*/length,
8759 		      sense_len,
8760 		      sizeof(*scsi_cmd),
8761 		      timeout);
8762 }
8763 
8764 void
8765 scsi_write_attribute(struct ccb_scsiio *csio, u_int32_t retries,
8766 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
8767 		    u_int8_t tag_action, uint32_t element, int logical_volume,
8768 		    int partition, int wtc, u_int8_t *data_ptr,
8769 		    u_int32_t length, int sense_len, u_int32_t timeout)
8770 {
8771 	struct scsi_write_attribute *scsi_cmd;
8772 
8773 	scsi_cmd = (struct scsi_write_attribute *)&csio->cdb_io.cdb_bytes;
8774 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8775 
8776 	scsi_cmd->opcode = WRITE_ATTRIBUTE;
8777 	if (wtc != 0)
8778 		scsi_cmd->byte2 = SWA_WTC;
8779 	scsi_ulto3b(element, scsi_cmd->element);
8780 	scsi_cmd->logical_volume = logical_volume;
8781 	scsi_cmd->partition = partition;
8782 	scsi_ulto4b(length, scsi_cmd->length);
8783 
8784 	cam_fill_csio(csio,
8785 		      retries,
8786 		      cbfcnp,
8787 		      /*flags*/CAM_DIR_OUT,
8788 		      tag_action,
8789 		      /*data_ptr*/data_ptr,
8790 		      /*dxfer_len*/length,
8791 		      sense_len,
8792 		      sizeof(*scsi_cmd),
8793 		      timeout);
8794 }
8795 
8796 void
8797 scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries,
8798 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
8799 			   uint8_t tag_action, int service_action,
8800 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8801 			   int timeout)
8802 {
8803 	struct scsi_per_res_in *scsi_cmd;
8804 
8805 	scsi_cmd = (struct scsi_per_res_in *)&csio->cdb_io.cdb_bytes;
8806 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8807 
8808 	scsi_cmd->opcode = PERSISTENT_RES_IN;
8809 	scsi_cmd->action = service_action;
8810 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
8811 
8812 	cam_fill_csio(csio,
8813 		      retries,
8814 		      cbfcnp,
8815 		      /*flags*/CAM_DIR_IN,
8816 		      tag_action,
8817 		      data_ptr,
8818 		      dxfer_len,
8819 		      sense_len,
8820 		      sizeof(*scsi_cmd),
8821 		      timeout);
8822 }
8823 
8824 void
8825 scsi_persistent_reserve_out(struct ccb_scsiio *csio, uint32_t retries,
8826 			    void (*cbfcnp)(struct cam_periph *, union ccb *),
8827 			    uint8_t tag_action, int service_action,
8828 			    int scope, int res_type, uint8_t *data_ptr,
8829 			    uint32_t dxfer_len, int sense_len, int timeout)
8830 {
8831 	struct scsi_per_res_out *scsi_cmd;
8832 
8833 	scsi_cmd = (struct scsi_per_res_out *)&csio->cdb_io.cdb_bytes;
8834 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8835 
8836 	scsi_cmd->opcode = PERSISTENT_RES_OUT;
8837 	scsi_cmd->action = service_action;
8838 	scsi_cmd->scope_type = scope | res_type;
8839 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8840 
8841 	cam_fill_csio(csio,
8842 		      retries,
8843 		      cbfcnp,
8844 		      /*flags*/CAM_DIR_OUT,
8845 		      tag_action,
8846 		      /*data_ptr*/data_ptr,
8847 		      /*dxfer_len*/dxfer_len,
8848 		      sense_len,
8849 		      sizeof(*scsi_cmd),
8850 		      timeout);
8851 }
8852 
8853 void
8854 scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries,
8855 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
8856 			  uint8_t tag_action, uint32_t security_protocol,
8857 			  uint32_t security_protocol_specific, int byte4,
8858 			  uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8859 			  int timeout)
8860 {
8861 	struct scsi_security_protocol_in *scsi_cmd;
8862 
8863 	scsi_cmd = (struct scsi_security_protocol_in *)&csio->cdb_io.cdb_bytes;
8864 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8865 
8866 	scsi_cmd->opcode = SECURITY_PROTOCOL_IN;
8867 
8868 	scsi_cmd->security_protocol = security_protocol;
8869 	scsi_ulto2b(security_protocol_specific,
8870 		    scsi_cmd->security_protocol_specific);
8871 	scsi_cmd->byte4 = byte4;
8872 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8873 
8874 	cam_fill_csio(csio,
8875 		      retries,
8876 		      cbfcnp,
8877 		      /*flags*/CAM_DIR_IN,
8878 		      tag_action,
8879 		      data_ptr,
8880 		      dxfer_len,
8881 		      sense_len,
8882 		      sizeof(*scsi_cmd),
8883 		      timeout);
8884 }
8885 
8886 void
8887 scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries,
8888 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
8889 			   uint8_t tag_action, uint32_t security_protocol,
8890 			   uint32_t security_protocol_specific, int byte4,
8891 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8892 			   int timeout)
8893 {
8894 	struct scsi_security_protocol_out *scsi_cmd;
8895 
8896 	scsi_cmd = (struct scsi_security_protocol_out *)&csio->cdb_io.cdb_bytes;
8897 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8898 
8899 	scsi_cmd->opcode = SECURITY_PROTOCOL_OUT;
8900 
8901 	scsi_cmd->security_protocol = security_protocol;
8902 	scsi_ulto2b(security_protocol_specific,
8903 		    scsi_cmd->security_protocol_specific);
8904 	scsi_cmd->byte4 = byte4;
8905 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8906 
8907 	cam_fill_csio(csio,
8908 		      retries,
8909 		      cbfcnp,
8910 		      /*flags*/CAM_DIR_OUT,
8911 		      tag_action,
8912 		      data_ptr,
8913 		      dxfer_len,
8914 		      sense_len,
8915 		      sizeof(*scsi_cmd),
8916 		      timeout);
8917 }
8918 
8919 void
8920 scsi_report_supported_opcodes(struct ccb_scsiio *csio, uint32_t retries,
8921 			      void (*cbfcnp)(struct cam_periph *, union ccb *),
8922 			      uint8_t tag_action, int options, int req_opcode,
8923 			      int req_service_action, uint8_t *data_ptr,
8924 			      uint32_t dxfer_len, int sense_len, int timeout)
8925 {
8926 	struct scsi_report_supported_opcodes *scsi_cmd;
8927 
8928 	scsi_cmd = (struct scsi_report_supported_opcodes *)
8929 	    &csio->cdb_io.cdb_bytes;
8930 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8931 
8932 	scsi_cmd->opcode = MAINTENANCE_IN;
8933 	scsi_cmd->service_action = REPORT_SUPPORTED_OPERATION_CODES;
8934 	scsi_cmd->options = options;
8935 	scsi_cmd->requested_opcode = req_opcode;
8936 	scsi_ulto2b(req_service_action, scsi_cmd->requested_service_action);
8937 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
8938 
8939 	cam_fill_csio(csio,
8940 		      retries,
8941 		      cbfcnp,
8942 		      /*flags*/CAM_DIR_IN,
8943 		      tag_action,
8944 		      data_ptr,
8945 		      dxfer_len,
8946 		      sense_len,
8947 		      sizeof(*scsi_cmd),
8948 		      timeout);
8949 }
8950 
8951 /*
8952  * Try make as good a match as possible with
8953  * available sub drivers
8954  */
8955 int
8956 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8957 {
8958 	struct scsi_inquiry_pattern *entry;
8959 	struct scsi_inquiry_data *inq;
8960 
8961 	entry = (struct scsi_inquiry_pattern *)table_entry;
8962 	inq = (struct scsi_inquiry_data *)inqbuffer;
8963 
8964 	if (((SID_TYPE(inq) == entry->type)
8965 	  || (entry->type == T_ANY))
8966 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8967 				   : entry->media_type & SIP_MEDIA_FIXED)
8968 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8969 	 && (cam_strmatch(inq->product, entry->product,
8970 			  sizeof(inq->product)) == 0)
8971 	 && (cam_strmatch(inq->revision, entry->revision,
8972 			  sizeof(inq->revision)) == 0)) {
8973 		return (0);
8974 	}
8975         return (-1);
8976 }
8977 
8978 /*
8979  * Try make as good a match as possible with
8980  * available sub drivers
8981  */
8982 int
8983 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8984 {
8985 	struct scsi_static_inquiry_pattern *entry;
8986 	struct scsi_inquiry_data *inq;
8987 
8988 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
8989 	inq = (struct scsi_inquiry_data *)inqbuffer;
8990 
8991 	if (((SID_TYPE(inq) == entry->type)
8992 	  || (entry->type == T_ANY))
8993 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8994 				   : entry->media_type & SIP_MEDIA_FIXED)
8995 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8996 	 && (cam_strmatch(inq->product, entry->product,
8997 			  sizeof(inq->product)) == 0)
8998 	 && (cam_strmatch(inq->revision, entry->revision,
8999 			  sizeof(inq->revision)) == 0)) {
9000 		return (0);
9001 	}
9002         return (-1);
9003 }
9004 
9005 /**
9006  * Compare two buffers of vpd device descriptors for a match.
9007  *
9008  * \param lhs      Pointer to first buffer of descriptors to compare.
9009  * \param lhs_len  The length of the first buffer.
9010  * \param rhs	   Pointer to second buffer of descriptors to compare.
9011  * \param rhs_len  The length of the second buffer.
9012  *
9013  * \return  0 on a match, -1 otherwise.
9014  *
9015  * Treat rhs and lhs as arrays of vpd device id descriptors.  Walk lhs matching
9016  * against each element in rhs until all data are exhausted or we have found
9017  * a match.
9018  */
9019 int
9020 scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
9021 {
9022 	struct scsi_vpd_id_descriptor *lhs_id;
9023 	struct scsi_vpd_id_descriptor *lhs_last;
9024 	struct scsi_vpd_id_descriptor *rhs_last;
9025 	uint8_t *lhs_end;
9026 	uint8_t *rhs_end;
9027 
9028 	lhs_end = lhs + lhs_len;
9029 	rhs_end = rhs + rhs_len;
9030 
9031 	/*
9032 	 * rhs_last and lhs_last are the last posible position of a valid
9033 	 * descriptor assuming it had a zero length identifier.  We use
9034 	 * these variables to insure we can safely dereference the length
9035 	 * field in our loop termination tests.
9036 	 */
9037 	lhs_last = (struct scsi_vpd_id_descriptor *)
9038 	    (lhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
9039 	rhs_last = (struct scsi_vpd_id_descriptor *)
9040 	    (rhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
9041 
9042 	lhs_id = (struct scsi_vpd_id_descriptor *)lhs;
9043 	while (lhs_id <= lhs_last
9044 	    && (lhs_id->identifier + lhs_id->length) <= lhs_end) {
9045 		struct scsi_vpd_id_descriptor *rhs_id;
9046 
9047 		rhs_id = (struct scsi_vpd_id_descriptor *)rhs;
9048 		while (rhs_id <= rhs_last
9049 		    && (rhs_id->identifier + rhs_id->length) <= rhs_end) {
9050 
9051 			if ((rhs_id->id_type &
9052 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK)) ==
9053 			    (lhs_id->id_type &
9054 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK))
9055 			 && rhs_id->length == lhs_id->length
9056 			 && memcmp(rhs_id->identifier, lhs_id->identifier,
9057 				   rhs_id->length) == 0)
9058 				return (0);
9059 
9060 			rhs_id = (struct scsi_vpd_id_descriptor *)
9061 			   (rhs_id->identifier + rhs_id->length);
9062 		}
9063 		lhs_id = (struct scsi_vpd_id_descriptor *)
9064 		   (lhs_id->identifier + lhs_id->length);
9065 	}
9066 	return (-1);
9067 }
9068 
9069 #ifdef _KERNEL
9070 int
9071 scsi_vpd_supported_page(struct cam_periph *periph, uint8_t page_id)
9072 {
9073 	struct cam_ed *device;
9074 	struct scsi_vpd_supported_pages *vpds;
9075 	int i, num_pages;
9076 
9077 	device = periph->path->device;
9078 	vpds = (struct scsi_vpd_supported_pages *)device->supported_vpds;
9079 
9080 	if (vpds != NULL) {
9081 		num_pages = device->supported_vpds_len -
9082 		    SVPD_SUPPORTED_PAGES_HDR_LEN;
9083 		for (i = 0; i < num_pages; i++) {
9084 			if (vpds->page_list[i] == page_id)
9085 				return (1);
9086 		}
9087 	}
9088 
9089 	return (0);
9090 }
9091 
9092 static void
9093 init_scsi_delay(void)
9094 {
9095 	int delay;
9096 
9097 	delay = SCSI_DELAY;
9098 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
9099 
9100 	if (set_scsi_delay(delay) != 0) {
9101 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
9102 		set_scsi_delay(SCSI_DELAY);
9103 	}
9104 }
9105 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
9106 
9107 static int
9108 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
9109 {
9110 	int error, delay;
9111 
9112 	delay = scsi_delay;
9113 	error = sysctl_handle_int(oidp, &delay, 0, req);
9114 	if (error != 0 || req->newptr == NULL)
9115 		return (error);
9116 	return (set_scsi_delay(delay));
9117 }
9118 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
9119     0, 0, sysctl_scsi_delay, "I",
9120     "Delay to allow devices to settle after a SCSI bus reset (ms)");
9121 
9122 static int
9123 set_scsi_delay(int delay)
9124 {
9125 	/*
9126          * If someone sets this to 0, we assume that they want the
9127          * minimum allowable bus settle delay.
9128 	 */
9129 	if (delay == 0) {
9130 		printf("cam: using minimum scsi_delay (%dms)\n",
9131 		    SCSI_MIN_DELAY);
9132 		delay = SCSI_MIN_DELAY;
9133 	}
9134 	if (delay < SCSI_MIN_DELAY)
9135 		return (EINVAL);
9136 	scsi_delay = delay;
9137 	return (0);
9138 }
9139 #endif /* _KERNEL */
9140