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