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