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