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