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