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