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