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