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