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