xref: /freebsd/sys/cam/scsi/scsi_all.c (revision d3e79182960ae56f52b5b3fbbfa6f4c2bbc53246)
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_gdev_type(cgd, csio->ccb_h.path);
3712 
3713 	/*
3714 	 * If the device is unconfigured, just pretend that it is a hard
3715 	 * drive.  scsi_op_desc() needs this.
3716 	 */
3717 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3718 		cgd->inq_data.device = T_DIRECT;
3719 
3720 	inq_data = &cgd->inq_data;
3721 
3722 #else /* !_KERNEL */
3723 
3724 	inq_data = &device->inq_data;
3725 
3726 #endif /* _KERNEL/!_KERNEL */
3727 
3728 	sbuf_printf(sb, "%s. CDB: ",
3729 		    scsi_op_desc(scsiio_cdb_ptr(csio)[0], inq_data));
3730 	scsi_cdb_sbuf(scsiio_cdb_ptr(csio), sb);
3731 
3732 #ifdef _KERNEL
3733 	xpt_free_ccb((union ccb *)cgd);
3734 #endif
3735 
3736 	return(0);
3737 }
3738 
3739 /*
3740  * Iterate over sense descriptors.  Each descriptor is passed into iter_func().
3741  * If iter_func() returns 0, list traversal continues.  If iter_func()
3742  * returns non-zero, list traversal is stopped.
3743  */
3744 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)3745 scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
3746 		  int (*iter_func)(struct scsi_sense_data_desc *sense,
3747 				   u_int, struct scsi_sense_desc_header *,
3748 				   void *), void *arg)
3749 {
3750 	int cur_pos;
3751 	int desc_len;
3752 
3753 	/*
3754 	 * First make sure the extra length field is present.
3755 	 */
3756 	if (SSD_DESC_IS_PRESENT(sense, sense_len, extra_len) == 0)
3757 		return;
3758 
3759 	/*
3760 	 * The length of data actually returned may be different than the
3761 	 * extra_len recorded in the structure.
3762 	 */
3763 	desc_len = sense_len -offsetof(struct scsi_sense_data_desc, sense_desc);
3764 
3765 	/*
3766 	 * Limit this further by the extra length reported, and the maximum
3767 	 * allowed extra length.
3768 	 */
3769 	desc_len = MIN(desc_len, MIN(sense->extra_len, SSD_EXTRA_MAX));
3770 
3771 	/*
3772 	 * Subtract the size of the header from the descriptor length.
3773 	 * This is to ensure that we have at least the header left, so we
3774 	 * don't have to check that inside the loop.  This can wind up
3775 	 * being a negative value.
3776 	 */
3777 	desc_len -= sizeof(struct scsi_sense_desc_header);
3778 
3779 	for (cur_pos = 0; cur_pos < desc_len;) {
3780 		struct scsi_sense_desc_header *header;
3781 
3782 		header = (struct scsi_sense_desc_header *)
3783 			&sense->sense_desc[cur_pos];
3784 
3785 		/*
3786 		 * Check to make sure we have the entire descriptor.  We
3787 		 * don't call iter_func() unless we do.
3788 		 *
3789 		 * Note that although cur_pos is at the beginning of the
3790 		 * descriptor, desc_len already has the header length
3791 		 * subtracted.  So the comparison of the length in the
3792 		 * header (which does not include the header itself) to
3793 		 * desc_len - cur_pos is correct.
3794 		 */
3795 		if (header->length > (desc_len - cur_pos))
3796 			break;
3797 
3798 		if (iter_func(sense, sense_len, header, arg) != 0)
3799 			break;
3800 
3801 		cur_pos += sizeof(*header) + header->length;
3802 	}
3803 }
3804 
3805 struct scsi_find_desc_info {
3806 	uint8_t desc_type;
3807 	struct scsi_sense_desc_header *header;
3808 };
3809 
3810 static int
scsi_find_desc_func(struct scsi_sense_data_desc * sense,u_int sense_len,struct scsi_sense_desc_header * header,void * arg)3811 scsi_find_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
3812 		    struct scsi_sense_desc_header *header, void *arg)
3813 {
3814 	struct scsi_find_desc_info *desc_info;
3815 
3816 	desc_info = (struct scsi_find_desc_info *)arg;
3817 
3818 	if (header->desc_type == desc_info->desc_type) {
3819 		desc_info->header = header;
3820 
3821 		/* We found the descriptor, tell the iterator to stop. */
3822 		return (1);
3823 	} else
3824 		return (0);
3825 }
3826 
3827 /*
3828  * Given a descriptor type, return a pointer to it if it is in the sense
3829  * data and not truncated.  Avoiding truncating sense data will simplify
3830  * things significantly for the caller.
3831  */
3832 uint8_t *
scsi_find_desc(struct scsi_sense_data_desc * sense,u_int sense_len,uint8_t desc_type)3833 scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
3834 	       uint8_t desc_type)
3835 {
3836 	struct scsi_find_desc_info desc_info;
3837 
3838 	desc_info.desc_type = desc_type;
3839 	desc_info.header = NULL;
3840 
3841 	scsi_desc_iterate(sense, sense_len, scsi_find_desc_func, &desc_info);
3842 
3843 	return ((uint8_t *)desc_info.header);
3844 }
3845 
3846 /*
3847  * Fill in SCSI descriptor sense data with the specified parameters.
3848  */
3849 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)3850 scsi_set_sense_data_desc_va(struct scsi_sense_data *sense_data,
3851     u_int *sense_len, scsi_sense_data_type sense_format, int current_error,
3852     int sense_key, int asc, int ascq, va_list ap)
3853 {
3854 	struct scsi_sense_data_desc *sense;
3855 	scsi_sense_elem_type elem_type;
3856 	int space, len;
3857 	uint8_t *desc, *data;
3858 
3859 	memset(sense_data, 0, sizeof(*sense_data));
3860 	sense = (struct scsi_sense_data_desc *)sense_data;
3861 	if (current_error != 0)
3862 		sense->error_code = SSD_DESC_CURRENT_ERROR;
3863 	else
3864 		sense->error_code = SSD_DESC_DEFERRED_ERROR;
3865 	sense->sense_key = sense_key;
3866 	sense->add_sense_code = asc;
3867 	sense->add_sense_code_qual = ascq;
3868 	sense->flags = 0;
3869 
3870 	desc = &sense->sense_desc[0];
3871 	space = *sense_len - offsetof(struct scsi_sense_data_desc, sense_desc);
3872 	while ((elem_type = va_arg(ap, scsi_sense_elem_type)) !=
3873 	    SSD_ELEM_NONE) {
3874 		if (elem_type >= SSD_ELEM_MAX) {
3875 			printf("%s: invalid sense type %d\n", __func__,
3876 			       elem_type);
3877 			break;
3878 		}
3879 		len = va_arg(ap, int);
3880 		data = va_arg(ap, uint8_t *);
3881 
3882 		switch (elem_type) {
3883 		case SSD_ELEM_SKIP:
3884 			break;
3885 		case SSD_ELEM_DESC:
3886 			if (space < len) {
3887 				sense->flags |= SSDD_SDAT_OVFL;
3888 				break;
3889 			}
3890 			bcopy(data, desc, len);
3891 			desc += len;
3892 			space -= len;
3893 			break;
3894 		case SSD_ELEM_SKS: {
3895 			struct scsi_sense_sks *sks = (void *)desc;
3896 
3897 			if (len > sizeof(sks->sense_key_spec))
3898 				break;
3899 			if (space < sizeof(*sks)) {
3900 				sense->flags |= SSDD_SDAT_OVFL;
3901 				break;
3902 			}
3903 			sks->desc_type = SSD_DESC_SKS;
3904 			sks->length = sizeof(*sks) -
3905 			    (offsetof(struct scsi_sense_sks, length) + 1);
3906 			bcopy(data, &sks->sense_key_spec, len);
3907 			desc += sizeof(*sks);
3908 			space -= sizeof(*sks);
3909 			break;
3910 		}
3911 		case SSD_ELEM_COMMAND: {
3912 			struct scsi_sense_command *cmd = (void *)desc;
3913 
3914 			if (len > sizeof(cmd->command_info))
3915 				break;
3916 			if (space < sizeof(*cmd)) {
3917 				sense->flags |= SSDD_SDAT_OVFL;
3918 				break;
3919 			}
3920 			cmd->desc_type = SSD_DESC_COMMAND;
3921 			cmd->length = sizeof(*cmd) -
3922 			    (offsetof(struct scsi_sense_command, length) + 1);
3923 			bcopy(data, &cmd->command_info[
3924 			    sizeof(cmd->command_info) - len], len);
3925 			desc += sizeof(*cmd);
3926 			space -= sizeof(*cmd);
3927 			break;
3928 		}
3929 		case SSD_ELEM_INFO: {
3930 			struct scsi_sense_info *info = (void *)desc;
3931 
3932 			if (len > sizeof(info->info))
3933 				break;
3934 			if (space < sizeof(*info)) {
3935 				sense->flags |= SSDD_SDAT_OVFL;
3936 				break;
3937 			}
3938 			info->desc_type = SSD_DESC_INFO;
3939 			info->length = sizeof(*info) -
3940 			    (offsetof(struct scsi_sense_info, length) + 1);
3941 			info->byte2 = SSD_INFO_VALID;
3942 			bcopy(data, &info->info[sizeof(info->info) - len], len);
3943 			desc += sizeof(*info);
3944 			space -= sizeof(*info);
3945 			break;
3946 		}
3947 		case SSD_ELEM_FRU: {
3948 			struct scsi_sense_fru *fru = (void *)desc;
3949 
3950 			if (len > sizeof(fru->fru))
3951 				break;
3952 			if (space < sizeof(*fru)) {
3953 				sense->flags |= SSDD_SDAT_OVFL;
3954 				break;
3955 			}
3956 			fru->desc_type = SSD_DESC_FRU;
3957 			fru->length = sizeof(*fru) -
3958 			    (offsetof(struct scsi_sense_fru, length) + 1);
3959 			fru->fru = *data;
3960 			desc += sizeof(*fru);
3961 			space -= sizeof(*fru);
3962 			break;
3963 		}
3964 		case SSD_ELEM_STREAM: {
3965 			struct scsi_sense_stream *stream = (void *)desc;
3966 
3967 			if (len > sizeof(stream->byte3))
3968 				break;
3969 			if (space < sizeof(*stream)) {
3970 				sense->flags |= SSDD_SDAT_OVFL;
3971 				break;
3972 			}
3973 			stream->desc_type = SSD_DESC_STREAM;
3974 			stream->length = sizeof(*stream) -
3975 			    (offsetof(struct scsi_sense_stream, length) + 1);
3976 			stream->byte3 = *data;
3977 			desc += sizeof(*stream);
3978 			space -= sizeof(*stream);
3979 			break;
3980 		}
3981 		default:
3982 			/*
3983 			 * We shouldn't get here, but if we do, do nothing.
3984 			 * We've already consumed the arguments above.
3985 			 */
3986 			break;
3987 		}
3988 	}
3989 	sense->extra_len = desc - &sense->sense_desc[0];
3990 	*sense_len = offsetof(struct scsi_sense_data_desc, extra_len) + 1 +
3991 	    sense->extra_len;
3992 }
3993 
3994 /*
3995  * Fill in SCSI fixed sense data with the specified parameters.
3996  */
3997 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)3998 scsi_set_sense_data_fixed_va(struct scsi_sense_data *sense_data,
3999     u_int *sense_len, scsi_sense_data_type sense_format, int current_error,
4000     int sense_key, int asc, int ascq, va_list ap)
4001 {
4002 	struct scsi_sense_data_fixed *sense;
4003 	scsi_sense_elem_type elem_type;
4004 	uint8_t *data;
4005 	int len;
4006 
4007 	memset(sense_data, 0, sizeof(*sense_data));
4008 	sense = (struct scsi_sense_data_fixed *)sense_data;
4009 	if (current_error != 0)
4010 		sense->error_code = SSD_CURRENT_ERROR;
4011 	else
4012 		sense->error_code = SSD_DEFERRED_ERROR;
4013 	sense->flags = sense_key & SSD_KEY;
4014 	sense->extra_len = 0;
4015 	if (*sense_len >= 13) {
4016 		sense->add_sense_code = asc;
4017 		sense->extra_len = MAX(sense->extra_len, 5);
4018 	} else
4019 		sense->flags |= SSD_SDAT_OVFL;
4020 	if (*sense_len >= 14) {
4021 		sense->add_sense_code_qual = ascq;
4022 		sense->extra_len = MAX(sense->extra_len, 6);
4023 	} else
4024 		sense->flags |= SSD_SDAT_OVFL;
4025 
4026 	while ((elem_type = va_arg(ap, scsi_sense_elem_type)) !=
4027 	    SSD_ELEM_NONE) {
4028 		if (elem_type >= SSD_ELEM_MAX) {
4029 			printf("%s: invalid sense type %d\n", __func__,
4030 			       elem_type);
4031 			break;
4032 		}
4033 		len = va_arg(ap, int);
4034 		data = va_arg(ap, uint8_t *);
4035 
4036 		switch (elem_type) {
4037 		case SSD_ELEM_SKIP:
4038 			break;
4039 		case SSD_ELEM_SKS:
4040 			if (len > sizeof(sense->sense_key_spec))
4041 				break;
4042 			if (*sense_len < 18) {
4043 				sense->flags |= SSD_SDAT_OVFL;
4044 				break;
4045 			}
4046 			bcopy(data, &sense->sense_key_spec[0], len);
4047 			sense->extra_len = MAX(sense->extra_len, 10);
4048 			break;
4049 		case SSD_ELEM_COMMAND:
4050 			if (*sense_len < 12) {
4051 				sense->flags |= SSD_SDAT_OVFL;
4052 				break;
4053 			}
4054 			if (len > sizeof(sense->cmd_spec_info)) {
4055 				data += len - sizeof(sense->cmd_spec_info);
4056 				len = sizeof(sense->cmd_spec_info);
4057 			}
4058 			bcopy(data, &sense->cmd_spec_info[
4059 			    sizeof(sense->cmd_spec_info) - len], len);
4060 			sense->extra_len = MAX(sense->extra_len, 4);
4061 			break;
4062 		case SSD_ELEM_INFO:
4063 			/* Set VALID bit only if no overflow. */
4064 			sense->error_code |= SSD_ERRCODE_VALID;
4065 			while (len > sizeof(sense->info)) {
4066 				if (data[0] != 0)
4067 					sense->error_code &= ~SSD_ERRCODE_VALID;
4068 				data ++;
4069 				len --;
4070 			}
4071 			bcopy(data, &sense->info[sizeof(sense->info) - len], len);
4072 			break;
4073 		case SSD_ELEM_FRU:
4074 			if (*sense_len < 15) {
4075 				sense->flags |= SSD_SDAT_OVFL;
4076 				break;
4077 			}
4078 			sense->fru = *data;
4079 			sense->extra_len = MAX(sense->extra_len, 7);
4080 			break;
4081 		case SSD_ELEM_STREAM:
4082 			sense->flags |= *data &
4083 			    (SSD_ILI | SSD_EOM | SSD_FILEMARK);
4084 			break;
4085 		default:
4086 
4087 			/*
4088 			 * We can't handle that in fixed format.  Skip it.
4089 			 */
4090 			break;
4091 		}
4092 	}
4093 	*sense_len = offsetof(struct scsi_sense_data_fixed, extra_len) + 1 +
4094 	    sense->extra_len;
4095 }
4096 
4097 /*
4098  * Fill in SCSI sense data with the specified parameters.  This routine can
4099  * fill in either fixed or descriptor type sense data.
4100  */
4101 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)4102 scsi_set_sense_data_va(struct scsi_sense_data *sense_data, u_int *sense_len,
4103 		      scsi_sense_data_type sense_format, int current_error,
4104 		      int sense_key, int asc, int ascq, va_list ap)
4105 {
4106 
4107 	if (*sense_len > SSD_FULL_SIZE)
4108 		*sense_len = SSD_FULL_SIZE;
4109 	if (sense_format == SSD_TYPE_DESC)
4110 		scsi_set_sense_data_desc_va(sense_data, sense_len,
4111 		    sense_format, current_error, sense_key, asc, ascq, ap);
4112 	else
4113 		scsi_set_sense_data_fixed_va(sense_data, sense_len,
4114 		    sense_format, current_error, sense_key, asc, ascq, ap);
4115 }
4116 
4117 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,...)4118 scsi_set_sense_data(struct scsi_sense_data *sense_data,
4119 		    scsi_sense_data_type sense_format, int current_error,
4120 		    int sense_key, int asc, int ascq, ...)
4121 {
4122 	va_list ap;
4123 	u_int	sense_len = SSD_FULL_SIZE;
4124 
4125 	va_start(ap, ascq);
4126 	scsi_set_sense_data_va(sense_data, &sense_len, sense_format,
4127 	    current_error, sense_key, asc, ascq, ap);
4128 	va_end(ap);
4129 }
4130 
4131 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,...)4132 scsi_set_sense_data_len(struct scsi_sense_data *sense_data, u_int *sense_len,
4133 		    scsi_sense_data_type sense_format, int current_error,
4134 		    int sense_key, int asc, int ascq, ...)
4135 {
4136 	va_list ap;
4137 
4138 	va_start(ap, ascq);
4139 	scsi_set_sense_data_va(sense_data, sense_len, sense_format,
4140 	    current_error, sense_key, asc, ascq, ap);
4141 	va_end(ap);
4142 }
4143 
4144 /*
4145  * Get sense information for three similar sense data types.
4146  */
4147 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)4148 scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
4149 		    uint8_t info_type, uint64_t *info, int64_t *signed_info)
4150 {
4151 	scsi_sense_data_type sense_type;
4152 
4153 	if (sense_len == 0)
4154 		goto bailout;
4155 
4156 	sense_type = scsi_sense_type(sense_data);
4157 
4158 	switch (sense_type) {
4159 	case SSD_TYPE_DESC: {
4160 		struct scsi_sense_data_desc *sense;
4161 		uint8_t *desc;
4162 
4163 		sense = (struct scsi_sense_data_desc *)sense_data;
4164 
4165 		desc = scsi_find_desc(sense, sense_len, info_type);
4166 		if (desc == NULL)
4167 			goto bailout;
4168 
4169 		switch (info_type) {
4170 		case SSD_DESC_INFO: {
4171 			struct scsi_sense_info *info_desc;
4172 
4173 			info_desc = (struct scsi_sense_info *)desc;
4174 
4175 			if ((info_desc->byte2 & SSD_INFO_VALID) == 0)
4176 				goto bailout;
4177 
4178 			*info = scsi_8btou64(info_desc->info);
4179 			if (signed_info != NULL)
4180 				*signed_info = *info;
4181 			break;
4182 		}
4183 		case SSD_DESC_COMMAND: {
4184 			struct scsi_sense_command *cmd_desc;
4185 
4186 			cmd_desc = (struct scsi_sense_command *)desc;
4187 
4188 			*info = scsi_8btou64(cmd_desc->command_info);
4189 			if (signed_info != NULL)
4190 				*signed_info = *info;
4191 			break;
4192 		}
4193 		case SSD_DESC_FRU: {
4194 			struct scsi_sense_fru *fru_desc;
4195 
4196 			fru_desc = (struct scsi_sense_fru *)desc;
4197 
4198 			if (fru_desc->fru == 0)
4199 				goto bailout;
4200 
4201 			*info = fru_desc->fru;
4202 			if (signed_info != NULL)
4203 				*signed_info = (int8_t)fru_desc->fru;
4204 			break;
4205 		}
4206 		default:
4207 			goto bailout;
4208 			break;
4209 		}
4210 		break;
4211 	}
4212 	case SSD_TYPE_FIXED: {
4213 		struct scsi_sense_data_fixed *sense;
4214 
4215 		sense = (struct scsi_sense_data_fixed *)sense_data;
4216 
4217 		switch (info_type) {
4218 		case SSD_DESC_INFO: {
4219 			uint32_t info_val;
4220 
4221 			if ((sense->error_code & SSD_ERRCODE_VALID) == 0)
4222 				goto bailout;
4223 
4224 			if (SSD_FIXED_IS_PRESENT(sense, sense_len, info) == 0)
4225 				goto bailout;
4226 
4227 			info_val = scsi_4btoul(sense->info);
4228 
4229 			*info = info_val;
4230 			if (signed_info != NULL)
4231 				*signed_info = (int32_t)info_val;
4232 			break;
4233 		}
4234 		case SSD_DESC_COMMAND: {
4235 			uint32_t cmd_val;
4236 
4237 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len,
4238 			     cmd_spec_info) == 0)
4239 			 || (SSD_FIXED_IS_FILLED(sense, cmd_spec_info) == 0))
4240 				goto bailout;
4241 
4242 			cmd_val = scsi_4btoul(sense->cmd_spec_info);
4243 			if (cmd_val == 0)
4244 				goto bailout;
4245 
4246 			*info = cmd_val;
4247 			if (signed_info != NULL)
4248 				*signed_info = (int32_t)cmd_val;
4249 			break;
4250 		}
4251 		case SSD_DESC_FRU:
4252 			if ((SSD_FIXED_IS_PRESENT(sense, sense_len, fru) == 0)
4253 			 || (SSD_FIXED_IS_FILLED(sense, fru) == 0))
4254 				goto bailout;
4255 
4256 			if (sense->fru == 0)
4257 				goto bailout;
4258 
4259 			*info = sense->fru;
4260 			if (signed_info != NULL)
4261 				*signed_info = (int8_t)sense->fru;
4262 			break;
4263 		default:
4264 			goto bailout;
4265 			break;
4266 		}
4267 		break;
4268 	}
4269 	default:
4270 		goto bailout;
4271 		break;
4272 	}
4273 
4274 	return (0);
4275 bailout:
4276 	return (1);
4277 }
4278 
4279 int
scsi_get_sks(struct scsi_sense_data * sense_data,u_int sense_len,uint8_t * sks)4280 scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len, uint8_t *sks)
4281 {
4282 	scsi_sense_data_type sense_type;
4283 
4284 	if (sense_len == 0)
4285 		goto bailout;
4286 
4287 	sense_type = scsi_sense_type(sense_data);
4288 
4289 	switch (sense_type) {
4290 	case SSD_TYPE_DESC: {
4291 		struct scsi_sense_data_desc *sense;
4292 		struct scsi_sense_sks *desc;
4293 
4294 		sense = (struct scsi_sense_data_desc *)sense_data;
4295 
4296 		desc = (struct scsi_sense_sks *)scsi_find_desc(sense, sense_len,
4297 							       SSD_DESC_SKS);
4298 		if (desc == NULL)
4299 			goto bailout;
4300 
4301 		if ((desc->sense_key_spec[0] & SSD_SKS_VALID) == 0)
4302 			goto bailout;
4303 
4304 		bcopy(desc->sense_key_spec, sks, sizeof(desc->sense_key_spec));
4305 		break;
4306 	}
4307 	case SSD_TYPE_FIXED: {
4308 		struct scsi_sense_data_fixed *sense;
4309 
4310 		sense = (struct scsi_sense_data_fixed *)sense_data;
4311 
4312 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, sense_key_spec)== 0)
4313 		 || (SSD_FIXED_IS_FILLED(sense, sense_key_spec) == 0))
4314 			goto bailout;
4315 
4316 		if ((sense->sense_key_spec[0] & SSD_SCS_VALID) == 0)
4317 			goto bailout;
4318 
4319 		bcopy(sense->sense_key_spec, sks,sizeof(sense->sense_key_spec));
4320 		break;
4321 	}
4322 	default:
4323 		goto bailout;
4324 		break;
4325 	}
4326 	return (0);
4327 bailout:
4328 	return (1);
4329 }
4330 
4331 /*
4332  * Provide a common interface for fixed and descriptor sense to detect
4333  * whether we have block-specific sense information.  It is clear by the
4334  * presence of the block descriptor in descriptor mode, but we have to
4335  * infer from the inquiry data and ILI bit in fixed mode.
4336  */
4337 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)4338 scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
4339 		    struct scsi_inquiry_data *inq_data, uint8_t *block_bits)
4340 {
4341 	scsi_sense_data_type sense_type;
4342 
4343 	if (inq_data != NULL) {
4344 		switch (SID_TYPE(inq_data)) {
4345 		case T_DIRECT:
4346 		case T_RBC:
4347 		case T_ZBC_HM:
4348 			break;
4349 		default:
4350 			goto bailout;
4351 			break;
4352 		}
4353 	}
4354 
4355 	sense_type = scsi_sense_type(sense_data);
4356 
4357 	switch (sense_type) {
4358 	case SSD_TYPE_DESC: {
4359 		struct scsi_sense_data_desc *sense;
4360 		struct scsi_sense_block *block;
4361 
4362 		sense = (struct scsi_sense_data_desc *)sense_data;
4363 
4364 		block = (struct scsi_sense_block *)scsi_find_desc(sense,
4365 		    sense_len, SSD_DESC_BLOCK);
4366 		if (block == NULL)
4367 			goto bailout;
4368 
4369 		*block_bits = block->byte3;
4370 		break;
4371 	}
4372 	case SSD_TYPE_FIXED: {
4373 		struct scsi_sense_data_fixed *sense;
4374 
4375 		sense = (struct scsi_sense_data_fixed *)sense_data;
4376 
4377 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4378 			goto bailout;
4379 
4380 		*block_bits = sense->flags & SSD_ILI;
4381 		break;
4382 	}
4383 	default:
4384 		goto bailout;
4385 		break;
4386 	}
4387 	return (0);
4388 bailout:
4389 	return (1);
4390 }
4391 
4392 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)4393 scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
4394 		     struct scsi_inquiry_data *inq_data, uint8_t *stream_bits)
4395 {
4396 	scsi_sense_data_type sense_type;
4397 
4398 	if (inq_data != NULL) {
4399 		switch (SID_TYPE(inq_data)) {
4400 		case T_SEQUENTIAL:
4401 			break;
4402 		default:
4403 			goto bailout;
4404 			break;
4405 		}
4406 	}
4407 
4408 	sense_type = scsi_sense_type(sense_data);
4409 
4410 	switch (sense_type) {
4411 	case SSD_TYPE_DESC: {
4412 		struct scsi_sense_data_desc *sense;
4413 		struct scsi_sense_stream *stream;
4414 
4415 		sense = (struct scsi_sense_data_desc *)sense_data;
4416 
4417 		stream = (struct scsi_sense_stream *)scsi_find_desc(sense,
4418 		    sense_len, SSD_DESC_STREAM);
4419 		if (stream == NULL)
4420 			goto bailout;
4421 
4422 		*stream_bits = stream->byte3;
4423 		break;
4424 	}
4425 	case SSD_TYPE_FIXED: {
4426 		struct scsi_sense_data_fixed *sense;
4427 
4428 		sense = (struct scsi_sense_data_fixed *)sense_data;
4429 
4430 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4431 			goto bailout;
4432 
4433 		*stream_bits = sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK);
4434 		break;
4435 	}
4436 	default:
4437 		goto bailout;
4438 		break;
4439 	}
4440 	return (0);
4441 bailout:
4442 	return (1);
4443 }
4444 
4445 void
scsi_info_sbuf(struct sbuf * sb,uint8_t * cdb,int cdb_len,struct scsi_inquiry_data * inq_data,uint64_t info)4446 scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4447 	       struct scsi_inquiry_data *inq_data, uint64_t info)
4448 {
4449 	sbuf_printf(sb, "Info: %#jx", info);
4450 }
4451 
4452 void
scsi_command_sbuf(struct sbuf * sb,uint8_t * cdb,int cdb_len,struct scsi_inquiry_data * inq_data,uint64_t csi)4453 scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4454 		  struct scsi_inquiry_data *inq_data, uint64_t csi)
4455 {
4456 	sbuf_printf(sb, "Command Specific Info: %#jx", csi);
4457 }
4458 
4459 void
scsi_progress_sbuf(struct sbuf * sb,uint16_t progress)4460 scsi_progress_sbuf(struct sbuf *sb, uint16_t progress)
4461 {
4462 	sbuf_printf(sb, "Progress: %d%% (%d/%d) complete",
4463 		    (progress * 100) / SSD_SKS_PROGRESS_DENOM,
4464 		    progress, SSD_SKS_PROGRESS_DENOM);
4465 }
4466 
4467 /*
4468  * Returns 1 for failure (i.e. SKS isn't valid) and 0 for success.
4469  */
4470 int
scsi_sks_sbuf(struct sbuf * sb,int sense_key,uint8_t * sks)4471 scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks)
4472 {
4473 
4474 	switch (sense_key) {
4475 	case SSD_KEY_ILLEGAL_REQUEST: {
4476 		struct scsi_sense_sks_field *field;
4477 		int bad_command;
4478 		char tmpstr[40];
4479 
4480 		/*Field Pointer*/
4481 		field = (struct scsi_sense_sks_field *)sks;
4482 
4483 		if (field->byte0 & SSD_SKS_FIELD_CMD)
4484 			bad_command = 1;
4485 		else
4486 			bad_command = 0;
4487 
4488 		tmpstr[0] = '\0';
4489 
4490 		/* Bit pointer is valid */
4491 		if (field->byte0 & SSD_SKS_BPV)
4492 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4493 				 field->byte0 & SSD_SKS_BIT_VALUE);
4494 
4495 		sbuf_printf(sb, "%s byte %d %sis invalid",
4496 			    bad_command ? "Command" : "Data",
4497 			    scsi_2btoul(field->field), tmpstr);
4498 		break;
4499 	}
4500 	case SSD_KEY_UNIT_ATTENTION: {
4501 		struct scsi_sense_sks_overflow *overflow;
4502 
4503 		overflow = (struct scsi_sense_sks_overflow *)sks;
4504 
4505 		/*UA Condition Queue Overflow*/
4506 		sbuf_printf(sb, "Unit Attention Condition Queue %s",
4507 			    (overflow->byte0 & SSD_SKS_OVERFLOW_SET) ?
4508 			    "Overflowed" : "Did Not Overflow??");
4509 		break;
4510 	}
4511 	case SSD_KEY_RECOVERED_ERROR:
4512 	case SSD_KEY_HARDWARE_ERROR:
4513 	case SSD_KEY_MEDIUM_ERROR: {
4514 		struct scsi_sense_sks_retry *retry;
4515 
4516 		/*Actual Retry Count*/
4517 		retry = (struct scsi_sense_sks_retry *)sks;
4518 
4519 		sbuf_printf(sb, "Actual Retry Count: %d",
4520 			    scsi_2btoul(retry->actual_retry_count));
4521 		break;
4522 	}
4523 	case SSD_KEY_NO_SENSE:
4524 	case SSD_KEY_NOT_READY: {
4525 		struct scsi_sense_sks_progress *progress;
4526 		int progress_val;
4527 
4528 		/*Progress Indication*/
4529 		progress = (struct scsi_sense_sks_progress *)sks;
4530 		progress_val = scsi_2btoul(progress->progress);
4531 
4532 		scsi_progress_sbuf(sb, progress_val);
4533 		break;
4534 	}
4535 	case SSD_KEY_COPY_ABORTED: {
4536 		struct scsi_sense_sks_segment *segment;
4537 		char tmpstr[40];
4538 
4539 		/*Segment Pointer*/
4540 		segment = (struct scsi_sense_sks_segment *)sks;
4541 
4542 		tmpstr[0] = '\0';
4543 
4544 		if (segment->byte0 & SSD_SKS_SEGMENT_BPV)
4545 			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4546 				 segment->byte0 & SSD_SKS_SEGMENT_BITPTR);
4547 
4548 		sbuf_printf(sb, "%s byte %d %sis invalid", (segment->byte0 &
4549 			    SSD_SKS_SEGMENT_SD) ? "Segment" : "Data",
4550 			    scsi_2btoul(segment->field), tmpstr);
4551 		break;
4552 	}
4553 	default:
4554 		sbuf_printf(sb, "Sense Key Specific: %#x,%#x", sks[0],
4555 			    scsi_2btoul(&sks[1]));
4556 		break;
4557 	}
4558 
4559 	return (0);
4560 }
4561 
4562 void
scsi_fru_sbuf(struct sbuf * sb,uint64_t fru)4563 scsi_fru_sbuf(struct sbuf *sb, uint64_t fru)
4564 {
4565 	sbuf_printf(sb, "Field Replaceable Unit: %d", (int)fru);
4566 }
4567 
4568 void
scsi_stream_sbuf(struct sbuf * sb,uint8_t stream_bits)4569 scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits)
4570 {
4571 	int need_comma;
4572 
4573 	need_comma = 0;
4574 	/*
4575 	 * XXX KDM this needs more descriptive decoding.
4576 	 */
4577 	sbuf_cat(sb, "Stream Command Sense Data: ");
4578 	if (stream_bits & SSD_DESC_STREAM_FM) {
4579 		sbuf_cat(sb, "Filemark");
4580 		need_comma = 1;
4581 	}
4582 
4583 	if (stream_bits & SSD_DESC_STREAM_EOM) {
4584 		sbuf_printf(sb, "%sEOM", (need_comma) ? "," : "");
4585 		need_comma = 1;
4586 	}
4587 
4588 	if (stream_bits & SSD_DESC_STREAM_ILI)
4589 		sbuf_printf(sb, "%sILI", (need_comma) ? "," : "");
4590 }
4591 
4592 void
scsi_block_sbuf(struct sbuf * sb,uint8_t block_bits)4593 scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits)
4594 {
4595 
4596 	sbuf_cat(sb, "Block Command Sense Data: ");
4597 	if (block_bits & SSD_DESC_BLOCK_ILI)
4598 		sbuf_cat(sb, "ILI");
4599 }
4600 
4601 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)4602 scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4603 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4604 		     struct scsi_inquiry_data *inq_data,
4605 		     struct scsi_sense_desc_header *header)
4606 {
4607 	struct scsi_sense_info *info;
4608 
4609 	info = (struct scsi_sense_info *)header;
4610 
4611 	if ((info->byte2 & SSD_INFO_VALID) == 0)
4612 		return;
4613 
4614 	scsi_info_sbuf(sb, cdb, cdb_len, inq_data, scsi_8btou64(info->info));
4615 }
4616 
4617 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)4618 scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4619 			u_int sense_len, uint8_t *cdb, int cdb_len,
4620 			struct scsi_inquiry_data *inq_data,
4621 			struct scsi_sense_desc_header *header)
4622 {
4623 	struct scsi_sense_command *command;
4624 
4625 	command = (struct scsi_sense_command *)header;
4626 
4627 	scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
4628 			  scsi_8btou64(command->command_info));
4629 }
4630 
4631 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)4632 scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4633 		    u_int sense_len, uint8_t *cdb, int cdb_len,
4634 		    struct scsi_inquiry_data *inq_data,
4635 		    struct scsi_sense_desc_header *header)
4636 {
4637 	struct scsi_sense_sks *sks;
4638 	int error_code, sense_key, asc, ascq;
4639 
4640 	sks = (struct scsi_sense_sks *)header;
4641 
4642 	if ((sks->sense_key_spec[0] & SSD_SKS_VALID) == 0)
4643 		return;
4644 
4645 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4646 			       &asc, &ascq, /*show_errors*/ 1);
4647 
4648 	scsi_sks_sbuf(sb, sense_key, sks->sense_key_spec);
4649 }
4650 
4651 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)4652 scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4653 		    u_int sense_len, uint8_t *cdb, int cdb_len,
4654 		    struct scsi_inquiry_data *inq_data,
4655 		    struct scsi_sense_desc_header *header)
4656 {
4657 	struct scsi_sense_fru *fru;
4658 
4659 	fru = (struct scsi_sense_fru *)header;
4660 
4661 	if (fru->fru == 0)
4662 		return;
4663 
4664 	scsi_fru_sbuf(sb, (uint64_t)fru->fru);
4665 }
4666 
4667 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)4668 scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4669 		       u_int sense_len, uint8_t *cdb, int cdb_len,
4670 		       struct scsi_inquiry_data *inq_data,
4671 		       struct scsi_sense_desc_header *header)
4672 {
4673 	struct scsi_sense_stream *stream;
4674 
4675 	stream = (struct scsi_sense_stream *)header;
4676 	scsi_stream_sbuf(sb, stream->byte3);
4677 }
4678 
4679 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)4680 scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4681 		      u_int sense_len, uint8_t *cdb, int cdb_len,
4682 		      struct scsi_inquiry_data *inq_data,
4683 		      struct scsi_sense_desc_header *header)
4684 {
4685 	struct scsi_sense_block *block;
4686 
4687 	block = (struct scsi_sense_block *)header;
4688 	scsi_block_sbuf(sb, block->byte3);
4689 }
4690 
4691 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)4692 scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4693 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4694 			 struct scsi_inquiry_data *inq_data,
4695 			 struct scsi_sense_desc_header *header)
4696 {
4697 	struct scsi_sense_progress *progress;
4698 	const char *sense_key_desc;
4699 	const char *asc_desc;
4700 	int progress_val;
4701 
4702 	progress = (struct scsi_sense_progress *)header;
4703 
4704 	/*
4705 	 * Get descriptions for the sense key, ASC, and ASCQ in the
4706 	 * progress descriptor.  These could be different than the values
4707 	 * in the overall sense data.
4708 	 */
4709 	scsi_sense_desc(progress->sense_key, progress->add_sense_code,
4710 			progress->add_sense_code_qual, inq_data,
4711 			&sense_key_desc, &asc_desc);
4712 
4713 	progress_val = scsi_2btoul(progress->progress);
4714 
4715 	/*
4716 	 * The progress indicator is for the operation described by the
4717 	 * sense key, ASC, and ASCQ in the descriptor.
4718 	 */
4719 	sbuf_cat(sb, sense_key_desc);
4720 	sbuf_printf(sb, " asc:%x,%x (%s): ", progress->add_sense_code,
4721 		    progress->add_sense_code_qual, asc_desc);
4722 	scsi_progress_sbuf(sb, progress_val);
4723 }
4724 
4725 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)4726 scsi_sense_ata_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4727 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4728 			 struct scsi_inquiry_data *inq_data,
4729 			 struct scsi_sense_desc_header *header)
4730 {
4731 	struct scsi_sense_ata_ret_desc *res;
4732 
4733 	res = (struct scsi_sense_ata_ret_desc *)header;
4734 
4735 	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s), ",
4736 	    res->status,
4737 	    (res->status & 0x80) ? "BSY " : "",
4738 	    (res->status & 0x40) ? "DRDY " : "",
4739 	    (res->status & 0x20) ? "DF " : "",
4740 	    (res->status & 0x10) ? "SERV " : "",
4741 	    (res->status & 0x08) ? "DRQ " : "",
4742 	    (res->status & 0x04) ? "CORR " : "",
4743 	    (res->status & 0x02) ? "IDX " : "",
4744 	    (res->status & 0x01) ? "ERR" : "");
4745 	if (res->status & 1) {
4746 	    sbuf_printf(sb, "error: %02x (%s%s%s%s%s%s%s%s), ",
4747 		res->error,
4748 		(res->error & 0x80) ? "ICRC " : "",
4749 		(res->error & 0x40) ? "UNC " : "",
4750 		(res->error & 0x20) ? "MC " : "",
4751 		(res->error & 0x10) ? "IDNF " : "",
4752 		(res->error & 0x08) ? "MCR " : "",
4753 		(res->error & 0x04) ? "ABRT " : "",
4754 		(res->error & 0x02) ? "NM " : "",
4755 		(res->error & 0x01) ? "ILI" : "");
4756 	}
4757 
4758 	if (res->flags & SSD_DESC_ATA_FLAG_EXTEND) {
4759 		sbuf_printf(sb, "count: %02x%02x, ",
4760 		    res->count_15_8, res->count_7_0);
4761 		sbuf_printf(sb, "LBA: %02x%02x%02x%02x%02x%02x, ",
4762 		    res->lba_47_40, res->lba_39_32, res->lba_31_24,
4763 		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4764 	} else {
4765 		sbuf_printf(sb, "count: %02x, ", res->count_7_0);
4766 		sbuf_printf(sb, "LBA: %02x%02x%02x, ",
4767 		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4768 	}
4769 	sbuf_printf(sb, "device: %02x, ", res->device);
4770 }
4771 
4772 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)4773 scsi_sense_forwarded_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4774 			 u_int sense_len, uint8_t *cdb, int cdb_len,
4775 			 struct scsi_inquiry_data *inq_data,
4776 			 struct scsi_sense_desc_header *header)
4777 {
4778 	struct scsi_sense_forwarded *forwarded;
4779 	const char *sense_key_desc;
4780 	const char *asc_desc;
4781 	int error_code, sense_key, asc, ascq;
4782 
4783 	forwarded = (struct scsi_sense_forwarded *)header;
4784 	scsi_extract_sense_len((struct scsi_sense_data *)forwarded->sense_data,
4785 	    forwarded->length - 2, &error_code, &sense_key, &asc, &ascq, 1);
4786 	scsi_sense_desc(sense_key, asc, ascq, NULL, &sense_key_desc, &asc_desc);
4787 
4788 	sbuf_printf(sb, "Forwarded sense: %s asc:%x,%x (%s): ",
4789 	    sense_key_desc, asc, ascq, asc_desc);
4790 }
4791 
4792 void
scsi_sense_dabd_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)4793 scsi_sense_dabd_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4794 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4795 		     struct scsi_inquiry_data *inq_data,
4796 		     struct scsi_sense_desc_header *header)
4797 {
4798 	struct scsi_sense_direct_access_block_device *dabd;
4799 	int error_code, sense_key, asc, ascq;
4800 
4801 	dabd = (struct scsi_sense_direct_access_block_device *)header;
4802 
4803 	sbuf_printf(sb, "Direct Access Block Device: fru: %d ",
4804 	    dabd->fru);
4805 	if (dabd->sks_byte & SSD_DESC_DABD_SKS_VALID) {
4806 		scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4807 		    &asc, &ascq, /*show_errors*/ 1);
4808 		scsi_sks_sbuf(sb, sense_key, dabd->data);
4809 	}
4810 	if (dabd->byte2 & SSD_DESC_DABD_VALID) {
4811 		scsi_info_sbuf(sb, cdb, cdb_len, inq_data,
4812 		    scsi_8btou64(dabd->info));
4813 		scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
4814 		    scsi_8btou64(dabd->command_info));
4815 	}
4816 }
4817 
4818 /*
4819  * Generic sense descriptor printing routine.  This is used when we have
4820  * not yet implemented a specific printing routine for this descriptor.
4821  */
4822 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)4823 scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4824 			u_int sense_len, uint8_t *cdb, int cdb_len,
4825 			struct scsi_inquiry_data *inq_data,
4826 			struct scsi_sense_desc_header *header)
4827 {
4828 	int i;
4829 	uint8_t *buf_ptr;
4830 
4831 	sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4832 
4833 	buf_ptr = (uint8_t *)&header[1];
4834 
4835 	for (i = 0; i < header->length; i++, buf_ptr++)
4836 		sbuf_printf(sb, " %02x", *buf_ptr);
4837 }
4838 
4839 /*
4840  * Keep this list in numeric order.  This speeds the array traversal.
4841  */
4842 struct scsi_sense_desc_printer {
4843 	uint8_t desc_type;
4844 	/*
4845 	 * The function arguments here are the superset of what is needed
4846 	 * to print out various different descriptors.  Command and
4847 	 * information descriptors need inquiry data and command type.
4848 	 * Sense key specific descriptors need the sense key.
4849 	 *
4850 	 * The sense, cdb, and inquiry data arguments may be NULL, but the
4851 	 * information printed may not be fully decoded as a result.
4852 	 */
4853 	void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4854 			   u_int sense_len, uint8_t *cdb, int cdb_len,
4855 			   struct scsi_inquiry_data *inq_data,
4856 			   struct scsi_sense_desc_header *header);
4857 } scsi_sense_printers[] = {
4858 	{SSD_DESC_INFO, scsi_sense_info_sbuf},
4859 	{SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4860 	{SSD_DESC_SKS, scsi_sense_sks_sbuf},
4861 	{SSD_DESC_FRU, scsi_sense_fru_sbuf},
4862 	{SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4863 	{SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4864 	{SSD_DESC_ATA, scsi_sense_ata_sbuf},
4865 	{SSD_DESC_PROGRESS, scsi_sense_progress_sbuf},
4866 	{SSD_DESC_DABD, scsi_sense_dabd_sbuf},
4867 	{SSD_DESC_FORWARDED, scsi_sense_forwarded_sbuf},
4868 };
4869 
4870 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)4871 scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4872 		     u_int sense_len, uint8_t *cdb, int cdb_len,
4873 		     struct scsi_inquiry_data *inq_data,
4874 		     struct scsi_sense_desc_header *header)
4875 {
4876 	u_int i;
4877 
4878 	for (i = 0; i < nitems(scsi_sense_printers); i++) {
4879 		struct scsi_sense_desc_printer *printer;
4880 
4881 		printer = &scsi_sense_printers[i];
4882 
4883 		/*
4884 		 * The list is sorted, so quit if we've passed our
4885 		 * descriptor number.
4886 		 */
4887 		if (printer->desc_type > header->desc_type)
4888 			break;
4889 
4890 		if (printer->desc_type != header->desc_type)
4891 			continue;
4892 
4893 		printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4894 				    inq_data, header);
4895 
4896 		return;
4897 	}
4898 
4899 	/*
4900 	 * No specific printing routine, so use the generic routine.
4901 	 */
4902 	scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4903 				inq_data, header);
4904 }
4905 
4906 scsi_sense_data_type
scsi_sense_type(struct scsi_sense_data * sense_data)4907 scsi_sense_type(struct scsi_sense_data *sense_data)
4908 {
4909 	switch (sense_data->error_code & SSD_ERRCODE) {
4910 	case SSD_DESC_CURRENT_ERROR:
4911 	case SSD_DESC_DEFERRED_ERROR:
4912 		return (SSD_TYPE_DESC);
4913 		break;
4914 	case SSD_CURRENT_ERROR:
4915 	case SSD_DEFERRED_ERROR:
4916 		return (SSD_TYPE_FIXED);
4917 		break;
4918 	default:
4919 		break;
4920 	}
4921 
4922 	return (SSD_TYPE_NONE);
4923 }
4924 
4925 struct scsi_print_sense_info {
4926 	struct sbuf *sb;
4927 	char *path_str;
4928 	uint8_t *cdb;
4929 	int cdb_len;
4930 	struct scsi_inquiry_data *inq_data;
4931 };
4932 
4933 static int
scsi_print_desc_func(struct scsi_sense_data_desc * sense,u_int sense_len,struct scsi_sense_desc_header * header,void * arg)4934 scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4935 		     struct scsi_sense_desc_header *header, void *arg)
4936 {
4937 	struct scsi_print_sense_info *print_info;
4938 
4939 	print_info = (struct scsi_print_sense_info *)arg;
4940 
4941 	switch (header->desc_type) {
4942 	case SSD_DESC_INFO:
4943 	case SSD_DESC_FRU:
4944 	case SSD_DESC_COMMAND:
4945 	case SSD_DESC_SKS:
4946 	case SSD_DESC_BLOCK:
4947 	case SSD_DESC_STREAM:
4948 		/*
4949 		 * We have already printed these descriptors, if they are
4950 		 * present.
4951 		 */
4952 		break;
4953 	default: {
4954 		sbuf_printf(print_info->sb, "%s", print_info->path_str);
4955 		scsi_sense_desc_sbuf(print_info->sb,
4956 				     (struct scsi_sense_data *)sense, sense_len,
4957 				     print_info->cdb, print_info->cdb_len,
4958 				     print_info->inq_data, header);
4959 		sbuf_putc(print_info->sb, '\n');
4960 		break;
4961 	}
4962 	}
4963 
4964 	/*
4965 	 * Tell the iterator that we want to see more descriptors if they
4966 	 * are present.
4967 	 */
4968 	return (0);
4969 }
4970 
4971 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)4972 scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4973 		     struct sbuf *sb, char *path_str,
4974 		     struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4975 		     int cdb_len)
4976 {
4977 	int error_code, sense_key, asc, ascq;
4978 
4979 	sbuf_cat(sb, path_str);
4980 
4981 	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4982 			       &asc, &ascq, /*show_errors*/ 1);
4983 
4984 	sbuf_cat(sb, "SCSI sense: ");
4985 	switch (error_code) {
4986 	case SSD_DEFERRED_ERROR:
4987 	case SSD_DESC_DEFERRED_ERROR:
4988 		sbuf_cat(sb, "Deferred error: ");
4989 
4990 		/* FALLTHROUGH */
4991 	case SSD_CURRENT_ERROR:
4992 	case SSD_DESC_CURRENT_ERROR:
4993 	{
4994 		struct scsi_sense_data_desc *desc_sense;
4995 		struct scsi_print_sense_info print_info;
4996 		const char *sense_key_desc;
4997 		const char *asc_desc;
4998 		uint8_t sks[3];
4999 		uint64_t val;
5000 		uint8_t bits;
5001 
5002 		/*
5003 		 * Get descriptions for the sense key, ASC, and ASCQ.  If
5004 		 * these aren't present in the sense data (i.e. the sense
5005 		 * data isn't long enough), the -1 values that
5006 		 * scsi_extract_sense_len() returns will yield default
5007 		 * or error descriptions.
5008 		 */
5009 		scsi_sense_desc(sense_key, asc, ascq, inq_data,
5010 				&sense_key_desc, &asc_desc);
5011 
5012 		/*
5013 		 * We first print the sense key and ASC/ASCQ.
5014 		 */
5015 		sbuf_cat(sb, sense_key_desc);
5016 		sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
5017 
5018 		/*
5019 		 * Print any block or stream device-specific information.
5020 		 */
5021 		if (scsi_get_block_info(sense, sense_len, inq_data,
5022 		    &bits) == 0 && bits != 0) {
5023 			sbuf_cat(sb, path_str);
5024 			scsi_block_sbuf(sb, bits);
5025 			sbuf_putc(sb, '\n');
5026 		} else if (scsi_get_stream_info(sense, sense_len, inq_data,
5027 		    &bits) == 0 && bits != 0) {
5028 			sbuf_cat(sb, path_str);
5029 			scsi_stream_sbuf(sb, bits);
5030 			sbuf_putc(sb, '\n');
5031 		}
5032 
5033 		/*
5034 		 * Print the info field.
5035 		 */
5036 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
5037 					&val, NULL) == 0) {
5038 			sbuf_cat(sb, path_str);
5039 			scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
5040 			sbuf_putc(sb, '\n');
5041 		}
5042 
5043 		/*
5044 		 * Print the FRU.
5045 		 */
5046 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
5047 					&val, NULL) == 0) {
5048 			sbuf_cat(sb, path_str);
5049 			scsi_fru_sbuf(sb, val);
5050 			sbuf_putc(sb, '\n');
5051 		}
5052 
5053 		/*
5054 		 * Print any command-specific information.
5055 		 */
5056 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
5057 					&val, NULL) == 0) {
5058 			sbuf_cat(sb, path_str);
5059 			scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
5060 			sbuf_putc(sb, '\n');
5061 		}
5062 
5063 		/*
5064 		 * Print out any sense-key-specific information.
5065 		 */
5066 		if (scsi_get_sks(sense, sense_len, sks) == 0) {
5067 			sbuf_cat(sb, path_str);
5068 			scsi_sks_sbuf(sb, sense_key, sks);
5069 			sbuf_putc(sb, '\n');
5070 		}
5071 
5072 		/*
5073 		 * If this is fixed sense, we're done.  If we have
5074 		 * descriptor sense, we might have more information
5075 		 * available.
5076 		 */
5077 		if (scsi_sense_type(sense) != SSD_TYPE_DESC)
5078 			break;
5079 
5080 		desc_sense = (struct scsi_sense_data_desc *)sense;
5081 
5082 		print_info.sb = sb;
5083 		print_info.path_str = path_str;
5084 		print_info.cdb = cdb;
5085 		print_info.cdb_len = cdb_len;
5086 		print_info.inq_data = inq_data;
5087 
5088 		/*
5089 		 * Print any sense descriptors that we have not already printed.
5090 		 */
5091 		scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
5092 				  &print_info);
5093 		break;
5094 	}
5095 	case -1:
5096 		/*
5097 		 * scsi_extract_sense_len() sets values to -1 if the
5098 		 * show_errors flag is set and they aren't present in the
5099 		 * sense data.  This means that sense_len is 0.
5100 		 */
5101 		sbuf_cat(sb, "No sense data present\n");
5102 		break;
5103 	default: {
5104 		sbuf_printf(sb, "Error code 0x%x", error_code);
5105 		if (sense->error_code & SSD_ERRCODE_VALID) {
5106 			struct scsi_sense_data_fixed *fixed_sense;
5107 
5108 			fixed_sense = (struct scsi_sense_data_fixed *)sense;
5109 
5110 			if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
5111 				uint32_t info;
5112 
5113 				info = scsi_4btoul(fixed_sense->info);
5114 
5115 				sbuf_printf(sb, " at block no. %d (decimal)",
5116 					    info);
5117 			}
5118 		}
5119 		sbuf_putc(sb, '\n');
5120 		break;
5121 	}
5122 	}
5123 }
5124 
5125 /*
5126  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
5127  */
5128 #ifdef _KERNEL
5129 int
scsi_sense_sbuf(struct ccb_scsiio * csio,struct sbuf * sb,scsi_sense_string_flags flags)5130 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
5131 		scsi_sense_string_flags flags)
5132 #else /* !_KERNEL */
5133 int
5134 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
5135 		struct sbuf *sb, scsi_sense_string_flags flags)
5136 #endif /* _KERNEL/!_KERNEL */
5137 {
5138 	struct	  scsi_sense_data *sense;
5139 	struct	  scsi_inquiry_data *inq_data;
5140 #ifdef _KERNEL
5141 	struct	  ccb_getdev *cgd;
5142 #endif /* _KERNEL */
5143 	char	  path_str[64];
5144 
5145 #ifndef _KERNEL
5146 	if (device == NULL)
5147 		return(-1);
5148 #endif /* !_KERNEL */
5149 	if ((csio == NULL) || (sb == NULL))
5150 		return(-1);
5151 
5152 	/*
5153 	 * If the CDB is a physical address, we can't deal with it..
5154 	 */
5155 	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
5156 		flags &= ~SSS_FLAG_PRINT_COMMAND;
5157 
5158 #ifdef _KERNEL
5159 	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
5160 #else /* !_KERNEL */
5161 	cam_path_string(device, path_str, sizeof(path_str));
5162 #endif /* _KERNEL/!_KERNEL */
5163 
5164 #ifdef _KERNEL
5165 	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
5166 		return(-1);
5167 	/*
5168 	 * Get the device information.
5169 	 */
5170 	xpt_gdev_type(cgd, csio->ccb_h.path);
5171 
5172 	/*
5173 	 * If the device is unconfigured, just pretend that it is a hard
5174 	 * drive.  scsi_op_desc() needs this.
5175 	 */
5176 	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
5177 		cgd->inq_data.device = T_DIRECT;
5178 
5179 	inq_data = &cgd->inq_data;
5180 
5181 #else /* !_KERNEL */
5182 
5183 	inq_data = &device->inq_data;
5184 
5185 #endif /* _KERNEL/!_KERNEL */
5186 
5187 	sense = NULL;
5188 
5189 	if (flags & SSS_FLAG_PRINT_COMMAND) {
5190 		sbuf_cat(sb, path_str);
5191 
5192 #ifdef _KERNEL
5193 		scsi_command_string(csio, sb);
5194 #else /* !_KERNEL */
5195 		scsi_command_string(device, csio, sb);
5196 #endif /* _KERNEL/!_KERNEL */
5197 		sbuf_putc(sb, '\n');
5198 	}
5199 
5200 	/*
5201 	 * If the sense data is a physical pointer, forget it.
5202 	 */
5203 	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
5204 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5205 #ifdef _KERNEL
5206 			xpt_free_ccb((union ccb*)cgd);
5207 #endif /* _KERNEL/!_KERNEL */
5208 			return(-1);
5209 		} else {
5210 			/*
5211 			 * bcopy the pointer to avoid unaligned access
5212 			 * errors on finicky architectures.  We don't
5213 			 * ensure that the sense data is pointer aligned.
5214 			 */
5215 			bcopy((struct scsi_sense_data **)&csio->sense_data,
5216 			    &sense, sizeof(struct scsi_sense_data *));
5217 		}
5218 	} else {
5219 		/*
5220 		 * If the physical sense flag is set, but the sense pointer
5221 		 * is not also set, we assume that the user is an idiot and
5222 		 * return.  (Well, okay, it could be that somehow, the
5223 		 * entire csio is physical, but we would have probably core
5224 		 * dumped on one of the bogus pointer deferences above
5225 		 * already.)
5226 		 */
5227 		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5228 #ifdef _KERNEL
5229 			xpt_free_ccb((union ccb*)cgd);
5230 #endif /* _KERNEL/!_KERNEL */
5231 			return(-1);
5232 		} else
5233 			sense = &csio->sense_data;
5234 	}
5235 
5236 	scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
5237 	    path_str, inq_data, scsiio_cdb_ptr(csio), csio->cdb_len);
5238 
5239 #ifdef _KERNEL
5240 	xpt_free_ccb((union ccb*)cgd);
5241 #endif /* _KERNEL/!_KERNEL */
5242 	return(0);
5243 }
5244 
5245 #ifdef _KERNEL
5246 char *
scsi_sense_string(struct ccb_scsiio * csio,char * str,int str_len)5247 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
5248 #else /* !_KERNEL */
5249 char *
5250 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
5251 		  char *str, int str_len)
5252 #endif /* _KERNEL/!_KERNEL */
5253 {
5254 	struct sbuf sb;
5255 
5256 	sbuf_new(&sb, str, str_len, 0);
5257 
5258 #ifdef _KERNEL
5259 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5260 #else /* !_KERNEL */
5261 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5262 #endif /* _KERNEL/!_KERNEL */
5263 
5264 	sbuf_finish(&sb);
5265 
5266 	return(sbuf_data(&sb));
5267 }
5268 
5269 #ifdef _KERNEL
5270 void
scsi_sense_print(struct ccb_scsiio * csio)5271 scsi_sense_print(struct ccb_scsiio *csio)
5272 {
5273 	struct sbuf sb;
5274 	char str[512];
5275 
5276 	sbuf_new(&sb, str, sizeof(str), 0);
5277 
5278 	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5279 
5280 	sbuf_finish(&sb);
5281 
5282 	sbuf_putbuf(&sb);
5283 }
5284 
5285 #else /* !_KERNEL */
5286 void
scsi_sense_print(struct cam_device * device,struct ccb_scsiio * csio,FILE * ofile)5287 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
5288 		 FILE *ofile)
5289 {
5290 	struct sbuf sb;
5291 	char str[512];
5292 
5293 	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
5294 		return;
5295 
5296 	sbuf_new(&sb, str, sizeof(str), 0);
5297 
5298 	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5299 
5300 	sbuf_finish(&sb);
5301 
5302 	fprintf(ofile, "%s", sbuf_data(&sb));
5303 }
5304 
5305 #endif /* _KERNEL/!_KERNEL */
5306 
5307 /*
5308  * Extract basic sense information.  This is backward-compatible with the
5309  * previous implementation.  For new implementations,
5310  * scsi_extract_sense_len() is recommended.
5311  */
5312 void
scsi_extract_sense(struct scsi_sense_data * sense_data,int * error_code,int * sense_key,int * asc,int * ascq)5313 scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
5314 		   int *sense_key, int *asc, int *ascq)
5315 {
5316 	scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
5317 			       sense_key, asc, ascq, /*show_errors*/ 0);
5318 }
5319 
5320 /*
5321  * Extract basic sense information from SCSI I/O CCB structure.
5322  */
5323 int
scsi_extract_sense_ccb(union ccb * ccb,int * error_code,int * sense_key,int * asc,int * ascq)5324 scsi_extract_sense_ccb(union ccb *ccb,
5325     int *error_code, int *sense_key, int *asc, int *ascq)
5326 {
5327 	struct scsi_sense_data *sense_data;
5328 
5329 	/* Make sure there are some sense data we can access. */
5330 	if (ccb->ccb_h.func_code != XPT_SCSI_IO ||
5331 	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
5332 	    (ccb->csio.scsi_status != SCSI_STATUS_CHECK_COND) ||
5333 	    (ccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0 ||
5334 	    (ccb->ccb_h.flags & CAM_SENSE_PHYS))
5335 		return (0);
5336 
5337 	if (ccb->ccb_h.flags & CAM_SENSE_PTR)
5338 		bcopy((struct scsi_sense_data **)&ccb->csio.sense_data,
5339 		    &sense_data, sizeof(struct scsi_sense_data *));
5340 	else
5341 		sense_data = &ccb->csio.sense_data;
5342 	scsi_extract_sense_len(sense_data,
5343 	    ccb->csio.sense_len - ccb->csio.sense_resid,
5344 	    error_code, sense_key, asc, ascq, 1);
5345 	if (*error_code == -1)
5346 		return (0);
5347 	return (1);
5348 }
5349 
5350 /*
5351  * Extract basic sense information.  If show_errors is set, sense values
5352  * will be set to -1 if they are not present.
5353  */
5354 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)5355 scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
5356 		       int *error_code, int *sense_key, int *asc, int *ascq,
5357 		       int show_errors)
5358 {
5359 	/*
5360 	 * If we have no length, we have no sense.
5361 	 */
5362 	if (sense_len == 0) {
5363 		if (show_errors == 0) {
5364 			*error_code = 0;
5365 			*sense_key = 0;
5366 			*asc = 0;
5367 			*ascq = 0;
5368 		} else {
5369 			*error_code = -1;
5370 			*sense_key = -1;
5371 			*asc = -1;
5372 			*ascq = -1;
5373 		}
5374 		return;
5375 	}
5376 
5377 	*error_code = sense_data->error_code & SSD_ERRCODE;
5378 
5379 	switch (*error_code) {
5380 	case SSD_DESC_CURRENT_ERROR:
5381 	case SSD_DESC_DEFERRED_ERROR: {
5382 		struct scsi_sense_data_desc *sense;
5383 
5384 		sense = (struct scsi_sense_data_desc *)sense_data;
5385 
5386 		if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
5387 			*sense_key = sense->sense_key & SSD_KEY;
5388 		else
5389 			*sense_key = (show_errors) ? -1 : 0;
5390 
5391 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
5392 			*asc = sense->add_sense_code;
5393 		else
5394 			*asc = (show_errors) ? -1 : 0;
5395 
5396 		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
5397 			*ascq = sense->add_sense_code_qual;
5398 		else
5399 			*ascq = (show_errors) ? -1 : 0;
5400 		break;
5401 	}
5402 	case SSD_CURRENT_ERROR:
5403 	case SSD_DEFERRED_ERROR:
5404 	default: {
5405 		struct scsi_sense_data_fixed *sense;
5406 
5407 		sense = (struct scsi_sense_data_fixed *)sense_data;
5408 
5409 		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
5410 			*sense_key = sense->flags & SSD_KEY;
5411 		else
5412 			*sense_key = (show_errors) ? -1 : 0;
5413 
5414 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
5415 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
5416 			*asc = sense->add_sense_code;
5417 		else
5418 			*asc = (show_errors) ? -1 : 0;
5419 
5420 		if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
5421 		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
5422 			*ascq = sense->add_sense_code_qual;
5423 		else
5424 			*ascq = (show_errors) ? -1 : 0;
5425 		break;
5426 	}
5427 	}
5428 }
5429 
5430 int
scsi_get_sense_key(struct scsi_sense_data * sense_data,u_int sense_len,int show_errors)5431 scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
5432 		   int show_errors)
5433 {
5434 	int error_code, sense_key, asc, ascq;
5435 
5436 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5437 			       &sense_key, &asc, &ascq, show_errors);
5438 
5439 	return (sense_key);
5440 }
5441 
5442 int
scsi_get_asc(struct scsi_sense_data * sense_data,u_int sense_len,int show_errors)5443 scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
5444 	     int show_errors)
5445 {
5446 	int error_code, sense_key, asc, ascq;
5447 
5448 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5449 			       &sense_key, &asc, &ascq, show_errors);
5450 
5451 	return (asc);
5452 }
5453 
5454 int
scsi_get_ascq(struct scsi_sense_data * sense_data,u_int sense_len,int show_errors)5455 scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
5456 	      int show_errors)
5457 {
5458 	int error_code, sense_key, asc, ascq;
5459 
5460 	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5461 			       &sense_key, &asc, &ascq, show_errors);
5462 
5463 	return (ascq);
5464 }
5465 
5466 /*
5467  * This function currently requires at least 36 bytes, or
5468  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
5469  * function needs more or less data in the future, another length should be
5470  * defined in scsi_all.h to indicate the minimum amount of data necessary
5471  * for this routine to function properly.
5472  */
5473 void
scsi_print_inquiry_sbuf(struct sbuf * sb,struct scsi_inquiry_data * inq_data)5474 scsi_print_inquiry_sbuf(struct sbuf *sb, struct scsi_inquiry_data *inq_data)
5475 {
5476 	uint8_t type;
5477 	char *dtype, *qtype;
5478 
5479 	type = SID_TYPE(inq_data);
5480 
5481 	/*
5482 	 * Figure out basic device type and qualifier.
5483 	 */
5484 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
5485 		qtype = " (vendor-unique qualifier)";
5486 	} else {
5487 		switch (SID_QUAL(inq_data)) {
5488 		case SID_QUAL_LU_CONNECTED:
5489 			qtype = "";
5490 			break;
5491 
5492 		case SID_QUAL_LU_OFFLINE:
5493 			qtype = " (offline)";
5494 			break;
5495 
5496 		case SID_QUAL_RSVD:
5497 			qtype = " (reserved qualifier)";
5498 			break;
5499 		default:
5500 		case SID_QUAL_BAD_LU:
5501 			qtype = " (LUN not supported)";
5502 			break;
5503 		}
5504 	}
5505 
5506 	switch (type) {
5507 	case T_DIRECT:
5508 		dtype = "Direct Access";
5509 		break;
5510 	case T_SEQUENTIAL:
5511 		dtype = "Sequential Access";
5512 		break;
5513 	case T_PRINTER:
5514 		dtype = "Printer";
5515 		break;
5516 	case T_PROCESSOR:
5517 		dtype = "Processor";
5518 		break;
5519 	case T_WORM:
5520 		dtype = "WORM";
5521 		break;
5522 	case T_CDROM:
5523 		dtype = "CD-ROM";
5524 		break;
5525 	case T_SCANNER:
5526 		dtype = "Scanner";
5527 		break;
5528 	case T_OPTICAL:
5529 		dtype = "Optical";
5530 		break;
5531 	case T_CHANGER:
5532 		dtype = "Changer";
5533 		break;
5534 	case T_COMM:
5535 		dtype = "Communication";
5536 		break;
5537 	case T_STORARRAY:
5538 		dtype = "Storage Array";
5539 		break;
5540 	case T_ENCLOSURE:
5541 		dtype = "Enclosure Services";
5542 		break;
5543 	case T_RBC:
5544 		dtype = "Simplified Direct Access";
5545 		break;
5546 	case T_OCRW:
5547 		dtype = "Optical Card Read/Write";
5548 		break;
5549 	case T_OSD:
5550 		dtype = "Object-Based Storage";
5551 		break;
5552 	case T_ADC:
5553 		dtype = "Automation/Drive Interface";
5554 		break;
5555 	case T_ZBC_HM:
5556 		dtype = "Host Managed Zoned Block";
5557 		break;
5558 	case T_NODEVICE:
5559 		dtype = "Uninstalled";
5560 		break;
5561 	default:
5562 		dtype = "unknown";
5563 		break;
5564 	}
5565 
5566 	scsi_print_inquiry_short_sbuf(sb, inq_data);
5567 
5568 	sbuf_printf(sb, "%s %s ", SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed", dtype);
5569 
5570 	if (SID_ANSI_REV(inq_data) == SCSI_REV_0)
5571 		sbuf_cat(sb, "SCSI ");
5572 	else if (SID_ANSI_REV(inq_data) <= SCSI_REV_SPC) {
5573 		sbuf_printf(sb, "SCSI-%d ", SID_ANSI_REV(inq_data));
5574 	} else {
5575 		sbuf_printf(sb, "SPC-%d SCSI ", SID_ANSI_REV(inq_data) - 2);
5576 	}
5577 	sbuf_printf(sb, "device%s\n", qtype);
5578 }
5579 
5580 void
scsi_print_inquiry(struct scsi_inquiry_data * inq_data)5581 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
5582 {
5583 	struct sbuf	sb;
5584 	char		buffer[120];
5585 
5586 	sbuf_new(&sb, buffer, 120, SBUF_FIXEDLEN);
5587 	scsi_print_inquiry_sbuf(&sb, inq_data);
5588 	sbuf_finish(&sb);
5589 	sbuf_putbuf(&sb);
5590 }
5591 
5592 void
scsi_print_inquiry_short_sbuf(struct sbuf * sb,struct scsi_inquiry_data * inq_data)5593 scsi_print_inquiry_short_sbuf(struct sbuf *sb, struct scsi_inquiry_data *inq_data)
5594 {
5595 
5596 	sbuf_putc(sb, '<');
5597 	cam_strvis_sbuf(sb, inq_data->vendor, sizeof(inq_data->vendor), 0);
5598 	sbuf_putc(sb, ' ');
5599 	cam_strvis_sbuf(sb, inq_data->product, sizeof(inq_data->product), 0);
5600 	sbuf_putc(sb, ' ');
5601 	cam_strvis_sbuf(sb, inq_data->revision, sizeof(inq_data->revision), 0);
5602 	sbuf_cat(sb, "> ");
5603 }
5604 
5605 void
scsi_print_inquiry_short(struct scsi_inquiry_data * inq_data)5606 scsi_print_inquiry_short(struct scsi_inquiry_data *inq_data)
5607 {
5608 	struct sbuf	sb;
5609 	char		buffer[84];
5610 
5611 	sbuf_new(&sb, buffer, 84, SBUF_FIXEDLEN);
5612 	scsi_print_inquiry_short_sbuf(&sb, inq_data);
5613 	sbuf_finish(&sb);
5614 	sbuf_putbuf(&sb);
5615 }
5616 
5617 /*
5618  * Table of syncrates that don't follow the "divisible by 4"
5619  * rule. This table will be expanded in future SCSI specs.
5620  */
5621 static struct {
5622 	u_int period_factor;
5623 	u_int period;	/* in 100ths of ns */
5624 } scsi_syncrates[] = {
5625 	{ 0x08, 625 },	/* FAST-160 */
5626 	{ 0x09, 1250 },	/* FAST-80 */
5627 	{ 0x0a, 2500 },	/* FAST-40 40MHz */
5628 	{ 0x0b, 3030 },	/* FAST-40 33MHz */
5629 	{ 0x0c, 5000 }	/* FAST-20 */
5630 };
5631 
5632 /*
5633  * Return the frequency in kHz corresponding to the given
5634  * sync period factor.
5635  */
5636 u_int
scsi_calc_syncsrate(u_int period_factor)5637 scsi_calc_syncsrate(u_int period_factor)
5638 {
5639 	u_int i;
5640 	u_int num_syncrates;
5641 
5642 	/*
5643 	 * It's a bug if period is zero, but if it is anyway, don't
5644 	 * die with a divide fault- instead return something which
5645 	 * 'approximates' async
5646 	 */
5647 	if (period_factor == 0) {
5648 		return (3300);
5649 	}
5650 
5651 	num_syncrates = nitems(scsi_syncrates);
5652 	/* See if the period is in the "exception" table */
5653 	for (i = 0; i < num_syncrates; i++) {
5654 		if (period_factor == scsi_syncrates[i].period_factor) {
5655 			/* Period in kHz */
5656 			return (100000000 / scsi_syncrates[i].period);
5657 		}
5658 	}
5659 
5660 	/*
5661 	 * Wasn't in the table, so use the standard
5662 	 * 4 times conversion.
5663 	 */
5664 	return (10000000 / (period_factor * 4 * 10));
5665 }
5666 
5667 /*
5668  * Return the SCSI sync parameter that corresponds to
5669  * the passed in period in 10ths of ns.
5670  */
5671 u_int
scsi_calc_syncparam(u_int period)5672 scsi_calc_syncparam(u_int period)
5673 {
5674 	u_int i;
5675 	u_int num_syncrates;
5676 
5677 	if (period == 0)
5678 		return (~0);	/* Async */
5679 
5680 	/* Adjust for exception table being in 100ths. */
5681 	period *= 10;
5682 	num_syncrates = nitems(scsi_syncrates);
5683 	/* See if the period is in the "exception" table */
5684 	for (i = 0; i < num_syncrates; i++) {
5685 		if (period <= scsi_syncrates[i].period) {
5686 			/* Period in 100ths of ns */
5687 			return (scsi_syncrates[i].period_factor);
5688 		}
5689 	}
5690 
5691 	/*
5692 	 * Wasn't in the table, so use the standard
5693 	 * 1/4 period in ns conversion.
5694 	 */
5695 	return (period/400);
5696 }
5697 
5698 int
scsi_devid_is_naa_ieee_reg(uint8_t * bufp)5699 scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
5700 {
5701 	struct scsi_vpd_id_descriptor *descr;
5702 	struct scsi_vpd_id_naa_basic *naa;
5703 	int n;
5704 
5705 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5706 	naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
5707 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5708 		return 0;
5709 	if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
5710 		return 0;
5711 	n = naa->naa >> SVPD_ID_NAA_NAA_SHIFT;
5712 	if (n != SVPD_ID_NAA_LOCAL_REG && n != SVPD_ID_NAA_IEEE_REG)
5713 		return 0;
5714 	return 1;
5715 }
5716 
5717 int
scsi_devid_is_sas_target(uint8_t * bufp)5718 scsi_devid_is_sas_target(uint8_t *bufp)
5719 {
5720 	struct scsi_vpd_id_descriptor *descr;
5721 
5722 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5723 	if (!scsi_devid_is_naa_ieee_reg(bufp))
5724 		return 0;
5725 	if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
5726 		return 0;
5727 	if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
5728 		return 0;
5729 	return 1;
5730 }
5731 
5732 int
scsi_devid_is_lun_eui64(uint8_t * bufp)5733 scsi_devid_is_lun_eui64(uint8_t *bufp)
5734 {
5735 	struct scsi_vpd_id_descriptor *descr;
5736 
5737 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5738 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5739 		return 0;
5740 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_EUI64)
5741 		return 0;
5742 	return 1;
5743 }
5744 
5745 int
scsi_devid_is_lun_naa(uint8_t * bufp)5746 scsi_devid_is_lun_naa(uint8_t *bufp)
5747 {
5748 	struct scsi_vpd_id_descriptor *descr;
5749 
5750 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5751 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5752 		return 0;
5753 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5754 		return 0;
5755 	return 1;
5756 }
5757 
5758 int
scsi_devid_is_lun_t10(uint8_t * bufp)5759 scsi_devid_is_lun_t10(uint8_t *bufp)
5760 {
5761 	struct scsi_vpd_id_descriptor *descr;
5762 
5763 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5764 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5765 		return 0;
5766 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_T10)
5767 		return 0;
5768 	return 1;
5769 }
5770 
5771 int
scsi_devid_is_lun_name(uint8_t * bufp)5772 scsi_devid_is_lun_name(uint8_t *bufp)
5773 {
5774 	struct scsi_vpd_id_descriptor *descr;
5775 
5776 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5777 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5778 		return 0;
5779 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_SCSI_NAME)
5780 		return 0;
5781 	return 1;
5782 }
5783 
5784 int
scsi_devid_is_lun_md5(uint8_t * bufp)5785 scsi_devid_is_lun_md5(uint8_t *bufp)
5786 {
5787 	struct scsi_vpd_id_descriptor *descr;
5788 
5789 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5790 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5791 		return 0;
5792 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_MD5_LUN_ID)
5793 		return 0;
5794 	return 1;
5795 }
5796 
5797 int
scsi_devid_is_lun_uuid(uint8_t * bufp)5798 scsi_devid_is_lun_uuid(uint8_t *bufp)
5799 {
5800 	struct scsi_vpd_id_descriptor *descr;
5801 
5802 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5803 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5804 		return 0;
5805 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_UUID)
5806 		return 0;
5807 	return 1;
5808 }
5809 
5810 int
scsi_devid_is_port_naa(uint8_t * bufp)5811 scsi_devid_is_port_naa(uint8_t *bufp)
5812 {
5813 	struct scsi_vpd_id_descriptor *descr;
5814 
5815 	descr = (struct scsi_vpd_id_descriptor *)bufp;
5816 	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_PORT)
5817 		return 0;
5818 	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5819 		return 0;
5820 	return 1;
5821 }
5822 
5823 struct scsi_vpd_id_descriptor *
scsi_get_devid_desc(struct scsi_vpd_id_descriptor * desc,uint32_t len,scsi_devid_checkfn_t ck_fn)5824 scsi_get_devid_desc(struct scsi_vpd_id_descriptor *desc, uint32_t len,
5825     scsi_devid_checkfn_t ck_fn)
5826 {
5827 	uint8_t *desc_buf_end;
5828 
5829 	desc_buf_end = (uint8_t *)desc + len;
5830 
5831 	for (; desc->identifier <= desc_buf_end &&
5832 	    desc->identifier + desc->length <= desc_buf_end;
5833 	    desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
5834 						    + desc->length)) {
5835 		if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
5836 			return (desc);
5837 	}
5838 	return (NULL);
5839 }
5840 
5841 struct scsi_vpd_id_descriptor *
scsi_get_devid(struct scsi_vpd_device_id * id,uint32_t page_len,scsi_devid_checkfn_t ck_fn)5842 scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
5843     scsi_devid_checkfn_t ck_fn)
5844 {
5845 	uint32_t len;
5846 
5847 	if (page_len < sizeof(*id))
5848 		return (NULL);
5849 	len = MIN(scsi_2btoul(id->length), page_len - sizeof(*id));
5850 	return (scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5851 	    id->desc_list, len, ck_fn));
5852 }
5853 
5854 int
scsi_transportid_sbuf(struct sbuf * sb,struct scsi_transportid_header * hdr,uint32_t valid_len)5855 scsi_transportid_sbuf(struct sbuf *sb, struct scsi_transportid_header *hdr,
5856 		      uint32_t valid_len)
5857 {
5858 	switch (hdr->format_protocol & SCSI_TRN_PROTO_MASK) {
5859 	case SCSI_PROTO_FC: {
5860 		struct scsi_transportid_fcp *fcp;
5861 		uint64_t n_port_name;
5862 
5863 		fcp = (struct scsi_transportid_fcp *)hdr;
5864 
5865 		n_port_name = scsi_8btou64(fcp->n_port_name);
5866 
5867 		sbuf_printf(sb, "FCP address: 0x%.16jx",(uintmax_t)n_port_name);
5868 		break;
5869 	}
5870 	case SCSI_PROTO_SPI: {
5871 		struct scsi_transportid_spi *spi;
5872 
5873 		spi = (struct scsi_transportid_spi *)hdr;
5874 
5875 		sbuf_printf(sb, "SPI address: %u,%u",
5876 			    scsi_2btoul(spi->scsi_addr),
5877 			    scsi_2btoul(spi->rel_trgt_port_id));
5878 		break;
5879 	}
5880 	case SCSI_PROTO_SSA:
5881 		/*
5882 		 * XXX KDM there is no transport ID defined in SPC-4 for
5883 		 * SSA.
5884 		 */
5885 		break;
5886 	case SCSI_PROTO_1394: {
5887 		struct scsi_transportid_1394 *sbp;
5888 		uint64_t eui64;
5889 
5890 		sbp = (struct scsi_transportid_1394 *)hdr;
5891 
5892 		eui64 = scsi_8btou64(sbp->eui64);
5893 		sbuf_printf(sb, "SBP address: 0x%.16jx", (uintmax_t)eui64);
5894 		break;
5895 	}
5896 	case SCSI_PROTO_RDMA: {
5897 		struct scsi_transportid_rdma *rdma;
5898 		unsigned int i;
5899 
5900 		rdma = (struct scsi_transportid_rdma *)hdr;
5901 
5902 		sbuf_cat(sb, "RDMA address: 0x");
5903 		for (i = 0; i < sizeof(rdma->initiator_port_id); i++)
5904 			sbuf_printf(sb, "%02x", rdma->initiator_port_id[i]);
5905 		break;
5906 	}
5907 	case SCSI_PROTO_ISCSI: {
5908 		uint32_t add_len, i;
5909 		uint8_t *iscsi_name = NULL;
5910 		int nul_found = 0;
5911 
5912 		sbuf_cat(sb, "iSCSI address: ");
5913 		if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5914 		    SCSI_TRN_ISCSI_FORMAT_DEVICE) {
5915 			struct scsi_transportid_iscsi_device *dev;
5916 
5917 			dev = (struct scsi_transportid_iscsi_device *)hdr;
5918 
5919 			/*
5920 			 * Verify how much additional data we really have.
5921 			 */
5922 			add_len = scsi_2btoul(dev->additional_length);
5923 			add_len = MIN(add_len, valid_len -
5924 				__offsetof(struct scsi_transportid_iscsi_device,
5925 					   iscsi_name));
5926 			iscsi_name = &dev->iscsi_name[0];
5927 
5928 		} else if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5929 			    SCSI_TRN_ISCSI_FORMAT_PORT) {
5930 			struct scsi_transportid_iscsi_port *port;
5931 
5932 			port = (struct scsi_transportid_iscsi_port *)hdr;
5933 
5934 			add_len = scsi_2btoul(port->additional_length);
5935 			add_len = MIN(add_len, valid_len -
5936 				__offsetof(struct scsi_transportid_iscsi_port,
5937 					   iscsi_name));
5938 			iscsi_name = &port->iscsi_name[0];
5939 		} else {
5940 			sbuf_printf(sb, "unknown format %x",
5941 				    (hdr->format_protocol &
5942 				     SCSI_TRN_FORMAT_MASK) >>
5943 				     SCSI_TRN_FORMAT_SHIFT);
5944 			break;
5945 		}
5946 		if (add_len == 0) {
5947 			sbuf_cat(sb, "not enough data");
5948 			break;
5949 		}
5950 		/*
5951 		 * This is supposed to be a NUL-terminated ASCII
5952 		 * string, but you never know.  So we're going to
5953 		 * check.  We need to do this because there is no
5954 		 * sbuf equivalent of strncat().
5955 		 */
5956 		for (i = 0; i < add_len; i++) {
5957 			if (iscsi_name[i] == '\0') {
5958 				nul_found = 1;
5959 				break;
5960 			}
5961 		}
5962 		/*
5963 		 * If there is a NUL in the name, we can just use
5964 		 * sbuf_cat().  Otherwise we need to use sbuf_bcat().
5965 		 */
5966 		if (nul_found != 0)
5967 			sbuf_cat(sb, iscsi_name);
5968 		else
5969 			sbuf_bcat(sb, iscsi_name, add_len);
5970 		break;
5971 	}
5972 	case SCSI_PROTO_SAS: {
5973 		struct scsi_transportid_sas *sas;
5974 		uint64_t sas_addr;
5975 
5976 		sas = (struct scsi_transportid_sas *)hdr;
5977 
5978 		sas_addr = scsi_8btou64(sas->sas_address);
5979 		sbuf_printf(sb, "SAS address: 0x%.16jx", (uintmax_t)sas_addr);
5980 		break;
5981 	}
5982 	case SCSI_PROTO_ADITP:
5983 	case SCSI_PROTO_ATA:
5984 	case SCSI_PROTO_UAS:
5985 		/*
5986 		 * No Transport ID format for ADI, ATA or USB is defined in
5987 		 * SPC-4.
5988 		 */
5989 		sbuf_printf(sb, "No known Transport ID format for protocol "
5990 			    "%#x", hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5991 		break;
5992 	case SCSI_PROTO_SOP: {
5993 		struct scsi_transportid_sop *sop;
5994 		struct scsi_sop_routing_id_norm *rid;
5995 
5996 		sop = (struct scsi_transportid_sop *)hdr;
5997 		rid = (struct scsi_sop_routing_id_norm *)sop->routing_id;
5998 
5999 		/*
6000 		 * Note that there is no alternate format specified in SPC-4
6001 		 * for the PCIe routing ID, so we don't really have a way
6002 		 * to know whether the second byte of the routing ID is
6003 		 * a device and function or just a function.  So we just
6004 		 * assume bus,device,function.
6005 		 */
6006 		sbuf_printf(sb, "SOP Routing ID: %u,%u,%u",
6007 			    rid->bus, rid->devfunc >> SCSI_TRN_SOP_DEV_SHIFT,
6008 			    rid->devfunc & SCSI_TRN_SOP_FUNC_NORM_MAX);
6009 		break;
6010 	}
6011 	case SCSI_PROTO_NONE:
6012 	default:
6013 		sbuf_printf(sb, "Unknown protocol %#x",
6014 			    hdr->format_protocol & SCSI_TRN_PROTO_MASK);
6015 		break;
6016 	}
6017 
6018 	return (0);
6019 }
6020 
6021 struct scsi_nv scsi_proto_map[] = {
6022 	{ "fcp", SCSI_PROTO_FC },
6023 	{ "spi", SCSI_PROTO_SPI },
6024 	{ "ssa", SCSI_PROTO_SSA },
6025 	{ "sbp", SCSI_PROTO_1394 },
6026 	{ "1394", SCSI_PROTO_1394 },
6027 	{ "srp", SCSI_PROTO_RDMA },
6028 	{ "rdma", SCSI_PROTO_RDMA },
6029 	{ "iscsi", SCSI_PROTO_ISCSI },
6030 	{ "iqn", SCSI_PROTO_ISCSI },
6031 	{ "sas", SCSI_PROTO_SAS },
6032 	{ "aditp", SCSI_PROTO_ADITP },
6033 	{ "ata", SCSI_PROTO_ATA },
6034 	{ "uas", SCSI_PROTO_UAS },
6035 	{ "usb", SCSI_PROTO_UAS },
6036 	{ "sop", SCSI_PROTO_SOP }
6037 };
6038 
6039 const char *
scsi_nv_to_str(struct scsi_nv * table,int num_table_entries,uint64_t value)6040 scsi_nv_to_str(struct scsi_nv *table, int num_table_entries, uint64_t value)
6041 {
6042 	int i;
6043 
6044 	for (i = 0; i < num_table_entries; i++) {
6045 		if (table[i].value == value)
6046 			return (table[i].name);
6047 	}
6048 
6049 	return (NULL);
6050 }
6051 
6052 /*
6053  * Given a name/value table, find a value matching the given name.
6054  * Return values:
6055  *	SCSI_NV_FOUND - match found
6056  *	SCSI_NV_AMBIGUOUS - more than one match, none of them exact
6057  *	SCSI_NV_NOT_FOUND - no match found
6058  */
6059 scsi_nv_status
scsi_get_nv(struct scsi_nv * table,int num_table_entries,char * name,int * table_entry,scsi_nv_flags flags)6060 scsi_get_nv(struct scsi_nv *table, int num_table_entries,
6061 	    char *name, int *table_entry, scsi_nv_flags flags)
6062 {
6063 	int i, num_matches = 0;
6064 
6065 	for (i = 0; i < num_table_entries; i++) {
6066 		size_t table_len, name_len;
6067 
6068 		table_len = strlen(table[i].name);
6069 		name_len = strlen(name);
6070 
6071 		if ((((flags & SCSI_NV_FLAG_IG_CASE) != 0)
6072 		  && (strncasecmp(table[i].name, name, name_len) == 0))
6073 		|| (((flags & SCSI_NV_FLAG_IG_CASE) == 0)
6074 		 && (strncmp(table[i].name, name, name_len) == 0))) {
6075 			*table_entry = i;
6076 
6077 			/*
6078 			 * Check for an exact match.  If we have the same
6079 			 * number of characters in the table as the argument,
6080 			 * and we already know they're the same, we have
6081 			 * an exact match.
6082 		 	 */
6083 			if (table_len == name_len)
6084 				return (SCSI_NV_FOUND);
6085 
6086 			/*
6087 			 * Otherwise, bump up the number of matches.  We'll
6088 			 * see later how many we have.
6089 			 */
6090 			num_matches++;
6091 		}
6092 	}
6093 
6094 	if (num_matches > 1)
6095 		return (SCSI_NV_AMBIGUOUS);
6096 	else if (num_matches == 1)
6097 		return (SCSI_NV_FOUND);
6098 	else
6099 		return (SCSI_NV_NOT_FOUND);
6100 }
6101 
6102 /*
6103  * Parse transport IDs for Fibre Channel, 1394 and SAS.  Since these are
6104  * all 64-bit numbers, the code is similar.
6105  */
6106 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)6107 scsi_parse_transportid_64bit(int proto_id, char *id_str,
6108 			     struct scsi_transportid_header **hdr,
6109 			     unsigned int *alloc_len,
6110 #ifdef _KERNEL
6111 			     struct malloc_type *type, int flags,
6112 #endif
6113 			     char *error_str, int error_str_len)
6114 {
6115 	uint64_t value;
6116 	char *endptr;
6117 	int retval;
6118 	size_t alloc_size;
6119 
6120 	retval = 0;
6121 
6122 	value = strtouq(id_str, &endptr, 0);
6123 	if (*endptr != '\0') {
6124 		if (error_str != NULL) {
6125 			snprintf(error_str, error_str_len, "%s: error "
6126 				 "parsing ID %s, 64-bit number required",
6127 				 __func__, id_str);
6128 		}
6129 		retval = 1;
6130 		goto bailout;
6131 	}
6132 
6133 	switch (proto_id) {
6134 	case SCSI_PROTO_FC:
6135 		alloc_size = sizeof(struct scsi_transportid_fcp);
6136 		break;
6137 	case SCSI_PROTO_1394:
6138 		alloc_size = sizeof(struct scsi_transportid_1394);
6139 		break;
6140 	case SCSI_PROTO_SAS:
6141 		alloc_size = sizeof(struct scsi_transportid_sas);
6142 		break;
6143 	default:
6144 		if (error_str != NULL) {
6145 			snprintf(error_str, error_str_len, "%s: unsupported "
6146 				 "protocol %d", __func__, proto_id);
6147 		}
6148 		retval = 1;
6149 		goto bailout;
6150 		break; /* NOTREACHED */
6151 	}
6152 #ifdef _KERNEL
6153 	*hdr = malloc(alloc_size, type, flags);
6154 #else /* _KERNEL */
6155 	*hdr = malloc(alloc_size);
6156 #endif /*_KERNEL */
6157 	if (*hdr == NULL) {
6158 		if (error_str != NULL) {
6159 			snprintf(error_str, error_str_len, "%s: unable to "
6160 				 "allocate %zu bytes", __func__, alloc_size);
6161 		}
6162 		retval = 1;
6163 		goto bailout;
6164 	}
6165 
6166 	*alloc_len = alloc_size;
6167 
6168 	bzero(*hdr, alloc_size);
6169 
6170 	switch (proto_id) {
6171 	case SCSI_PROTO_FC: {
6172 		struct scsi_transportid_fcp *fcp;
6173 
6174 		fcp = (struct scsi_transportid_fcp *)(*hdr);
6175 		fcp->format_protocol = SCSI_PROTO_FC |
6176 				       SCSI_TRN_FCP_FORMAT_DEFAULT;
6177 		scsi_u64to8b(value, fcp->n_port_name);
6178 		break;
6179 	}
6180 	case SCSI_PROTO_1394: {
6181 		struct scsi_transportid_1394 *sbp;
6182 
6183 		sbp = (struct scsi_transportid_1394 *)(*hdr);
6184 		sbp->format_protocol = SCSI_PROTO_1394 |
6185 				       SCSI_TRN_1394_FORMAT_DEFAULT;
6186 		scsi_u64to8b(value, sbp->eui64);
6187 		break;
6188 	}
6189 	case SCSI_PROTO_SAS: {
6190 		struct scsi_transportid_sas *sas;
6191 
6192 		sas = (struct scsi_transportid_sas *)(*hdr);
6193 		sas->format_protocol = SCSI_PROTO_SAS |
6194 				       SCSI_TRN_SAS_FORMAT_DEFAULT;
6195 		scsi_u64to8b(value, sas->sas_address);
6196 		break;
6197 	}
6198 	default:
6199 		break;
6200 	}
6201 bailout:
6202 	return (retval);
6203 }
6204 
6205 /*
6206  * Parse a SPI (Parallel SCSI) address of the form: id,rel_tgt_port
6207  */
6208 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)6209 scsi_parse_transportid_spi(char *id_str, struct scsi_transportid_header **hdr,
6210 			   unsigned int *alloc_len,
6211 #ifdef _KERNEL
6212 			   struct malloc_type *type, int flags,
6213 #endif
6214 			   char *error_str, int error_str_len)
6215 {
6216 	unsigned long scsi_addr, target_port;
6217 	struct scsi_transportid_spi *spi;
6218 	char *tmpstr, *endptr;
6219 	int retval;
6220 
6221 	retval = 0;
6222 
6223 	tmpstr = strsep(&id_str, ",");
6224 	if (tmpstr == NULL) {
6225 		if (error_str != NULL) {
6226 			snprintf(error_str, error_str_len,
6227 				 "%s: no ID found", __func__);
6228 		}
6229 		retval = 1;
6230 		goto bailout;
6231 	}
6232 	scsi_addr = strtoul(tmpstr, &endptr, 0);
6233 	if (*endptr != '\0') {
6234 		if (error_str != NULL) {
6235 			snprintf(error_str, error_str_len, "%s: error "
6236 				 "parsing SCSI ID %s, number required",
6237 				 __func__, tmpstr);
6238 		}
6239 		retval = 1;
6240 		goto bailout;
6241 	}
6242 
6243 	if (id_str == NULL) {
6244 		if (error_str != NULL) {
6245 			snprintf(error_str, error_str_len, "%s: no relative "
6246 				 "target port found", __func__);
6247 		}
6248 		retval = 1;
6249 		goto bailout;
6250 	}
6251 
6252 	target_port = strtoul(id_str, &endptr, 0);
6253 	if (*endptr != '\0') {
6254 		if (error_str != NULL) {
6255 			snprintf(error_str, error_str_len, "%s: error "
6256 				 "parsing relative target port %s, number "
6257 				 "required", __func__, id_str);
6258 		}
6259 		retval = 1;
6260 		goto bailout;
6261 	}
6262 #ifdef _KERNEL
6263 	spi = malloc(sizeof(*spi), type, flags);
6264 #else
6265 	spi = malloc(sizeof(*spi));
6266 #endif
6267 	if (spi == NULL) {
6268 		if (error_str != NULL) {
6269 			snprintf(error_str, error_str_len, "%s: unable to "
6270 				 "allocate %zu bytes", __func__,
6271 				 sizeof(*spi));
6272 		}
6273 		retval = 1;
6274 		goto bailout;
6275 	}
6276 	*alloc_len = sizeof(*spi);
6277 	bzero(spi, sizeof(*spi));
6278 
6279 	spi->format_protocol = SCSI_PROTO_SPI | SCSI_TRN_SPI_FORMAT_DEFAULT;
6280 	scsi_ulto2b(scsi_addr, spi->scsi_addr);
6281 	scsi_ulto2b(target_port, spi->rel_trgt_port_id);
6282 
6283 	*hdr = (struct scsi_transportid_header *)spi;
6284 bailout:
6285 	return (retval);
6286 }
6287 
6288 /*
6289  * Parse an RDMA/SRP Initiator Port ID string.  This is 32 hexadecimal digits,
6290  * optionally prefixed by "0x" or "0X".
6291  */
6292 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)6293 scsi_parse_transportid_rdma(char *id_str, struct scsi_transportid_header **hdr,
6294 			    unsigned int *alloc_len,
6295 #ifdef _KERNEL
6296 			    struct malloc_type *type, int flags,
6297 #endif
6298 			    char *error_str, int error_str_len)
6299 {
6300 	struct scsi_transportid_rdma *rdma;
6301 	int retval;
6302 	size_t id_len, rdma_id_size;
6303 	uint8_t rdma_id[SCSI_TRN_RDMA_PORT_LEN];
6304 	char *tmpstr;
6305 	unsigned int i, j;
6306 
6307 	retval = 0;
6308 	id_len = strlen(id_str);
6309 	rdma_id_size = SCSI_TRN_RDMA_PORT_LEN;
6310 
6311 	/*
6312 	 * Check the size.  It needs to be either 32 or 34 characters long.
6313 	 */
6314 	if ((id_len != (rdma_id_size * 2))
6315 	 && (id_len != ((rdma_id_size * 2) + 2))) {
6316 		if (error_str != NULL) {
6317 			snprintf(error_str, error_str_len, "%s: RDMA ID "
6318 				 "must be 32 hex digits (0x prefix "
6319 				 "optional), only %zu seen", __func__, id_len);
6320 		}
6321 		retval = 1;
6322 		goto bailout;
6323 	}
6324 
6325 	tmpstr = id_str;
6326 	/*
6327 	 * If the user gave us 34 characters, the string needs to start
6328 	 * with '0x'.
6329 	 */
6330 	if (id_len == ((rdma_id_size * 2) + 2)) {
6331 	 	if ((tmpstr[0] == '0')
6332 		 && ((tmpstr[1] == 'x') || (tmpstr[1] == 'X'))) {
6333 			tmpstr += 2;
6334 		} else {
6335 			if (error_str != NULL) {
6336 				snprintf(error_str, error_str_len, "%s: RDMA "
6337 					 "ID prefix, if used, must be \"0x\", "
6338 					 "got %s", __func__, tmpstr);
6339 			}
6340 			retval = 1;
6341 			goto bailout;
6342 		}
6343 	}
6344 	bzero(rdma_id, sizeof(rdma_id));
6345 
6346 	/*
6347 	 * Convert ASCII hex into binary bytes.  There is no standard
6348 	 * 128-bit integer type, and so no strtou128t() routine to convert
6349 	 * from hex into a large integer.  In the end, we're not going to
6350 	 * an integer, but rather to a byte array, so that and the fact
6351 	 * that we require the user to give us 32 hex digits simplifies the
6352 	 * logic.
6353 	 */
6354 	for (i = 0; i < (rdma_id_size * 2); i++) {
6355 		int cur_shift;
6356 		unsigned char c;
6357 
6358 		/* Increment the byte array one for every 2 hex digits */
6359 		j = i >> 1;
6360 
6361 		/*
6362 		 * The first digit in every pair is the most significant
6363 		 * 4 bits.  The second is the least significant 4 bits.
6364 		 */
6365 		if ((i % 2) == 0)
6366 			cur_shift = 4;
6367 		else
6368 			cur_shift = 0;
6369 
6370 		c = tmpstr[i];
6371 		/* Convert the ASCII hex character into a number */
6372 		if (isdigit(c))
6373 			c -= '0';
6374 		else if (isalpha(c))
6375 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
6376 		else {
6377 			if (error_str != NULL) {
6378 				snprintf(error_str, error_str_len, "%s: "
6379 					 "RDMA ID must be hex digits, got "
6380 					 "invalid character %c", __func__,
6381 					 tmpstr[i]);
6382 			}
6383 			retval = 1;
6384 			goto bailout;
6385 		}
6386 		/*
6387 		 * The converted number can't be less than 0; the type is
6388 		 * unsigned, and the subtraction logic will not give us
6389 		 * a negative number.  So we only need to make sure that
6390 		 * the value is not greater than 0xf.  (i.e. make sure the
6391 		 * user didn't give us a value like "0x12jklmno").
6392 		 */
6393 		if (c > 0xf) {
6394 			if (error_str != NULL) {
6395 				snprintf(error_str, error_str_len, "%s: "
6396 					 "RDMA ID must be hex digits, got "
6397 					 "invalid character %c", __func__,
6398 					 tmpstr[i]);
6399 			}
6400 			retval = 1;
6401 			goto bailout;
6402 		}
6403 
6404 		rdma_id[j] |= c << cur_shift;
6405 	}
6406 
6407 #ifdef _KERNEL
6408 	rdma = malloc(sizeof(*rdma), type, flags);
6409 #else
6410 	rdma = malloc(sizeof(*rdma));
6411 #endif
6412 	if (rdma == NULL) {
6413 		if (error_str != NULL) {
6414 			snprintf(error_str, error_str_len, "%s: unable to "
6415 				 "allocate %zu bytes", __func__,
6416 				 sizeof(*rdma));
6417 		}
6418 		retval = 1;
6419 		goto bailout;
6420 	}
6421 	*alloc_len = sizeof(*rdma);
6422 	bzero(rdma, *alloc_len);
6423 
6424 	rdma->format_protocol = SCSI_PROTO_RDMA | SCSI_TRN_RDMA_FORMAT_DEFAULT;
6425 	bcopy(rdma_id, rdma->initiator_port_id, SCSI_TRN_RDMA_PORT_LEN);
6426 
6427 	*hdr = (struct scsi_transportid_header *)rdma;
6428 
6429 bailout:
6430 	return (retval);
6431 }
6432 
6433 /*
6434  * Parse an iSCSI name.  The format is either just the name:
6435  *
6436  *	iqn.2012-06.com.example:target0
6437  * or the name, separator and initiator session ID:
6438  *
6439  *	iqn.2012-06.com.example:target0,i,0x123
6440  *
6441  * The separator format is exact.
6442  */
6443 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)6444 scsi_parse_transportid_iscsi(char *id_str, struct scsi_transportid_header **hdr,
6445 			     unsigned int *alloc_len,
6446 #ifdef _KERNEL
6447 			     struct malloc_type *type, int flags,
6448 #endif
6449 			     char *error_str, int error_str_len)
6450 {
6451 	size_t id_len, sep_len, id_size, name_len;
6452 	int retval;
6453 	unsigned int i, sep_pos, sep_found;
6454 	const char *sep_template = ",i,0x";
6455 	const char *iqn_prefix = "iqn.";
6456 	struct scsi_transportid_iscsi_device *iscsi;
6457 
6458 	retval = 0;
6459 	sep_found = 0;
6460 
6461 	id_len = strlen(id_str);
6462 	sep_len = strlen(sep_template);
6463 
6464 	/*
6465 	 * The separator is defined as exactly ',i,0x'.  Any other commas,
6466 	 * or any other form, is an error.  So look for a comma, and once
6467 	 * we find that, the next few characters must match the separator
6468 	 * exactly.  Once we get through the separator, there should be at
6469 	 * least one character.
6470 	 */
6471 	for (i = 0, sep_pos = 0; i < id_len; i++) {
6472 		if (sep_pos == 0) {
6473 		 	if (id_str[i] == sep_template[sep_pos])
6474 				sep_pos++;
6475 
6476 			continue;
6477 		}
6478 		if (sep_pos < sep_len) {
6479 			if (id_str[i] == sep_template[sep_pos]) {
6480 				sep_pos++;
6481 				continue;
6482 			}
6483 			if (error_str != NULL) {
6484 				snprintf(error_str, error_str_len, "%s: "
6485 					 "invalid separator in iSCSI name "
6486 					 "\"%s\"",
6487 					 __func__, id_str);
6488 			}
6489 			retval = 1;
6490 			goto bailout;
6491 		} else {
6492 			sep_found = 1;
6493 			break;
6494 		}
6495 	}
6496 
6497 	/*
6498 	 * Check to see whether we have a separator but no digits after it.
6499 	 */
6500 	if ((sep_pos != 0)
6501 	 && (sep_found == 0)) {
6502 		if (error_str != NULL) {
6503 			snprintf(error_str, error_str_len, "%s: no digits "
6504 				 "found after separator in iSCSI name \"%s\"",
6505 				 __func__, id_str);
6506 		}
6507 		retval = 1;
6508 		goto bailout;
6509 	}
6510 
6511 	/*
6512 	 * The incoming ID string has the "iqn." prefix stripped off.  We
6513 	 * need enough space for the base structure (the structures are the
6514 	 * same for the two iSCSI forms), the prefix, the ID string and a
6515 	 * terminating NUL.
6516 	 */
6517 	id_size = sizeof(*iscsi) + strlen(iqn_prefix) + id_len + 1;
6518 
6519 #ifdef _KERNEL
6520 	iscsi = malloc(id_size, type, flags);
6521 #else
6522 	iscsi = malloc(id_size);
6523 #endif
6524 	if (iscsi == NULL) {
6525 		if (error_str != NULL) {
6526 			snprintf(error_str, error_str_len, "%s: unable to "
6527 				 "allocate %zu bytes", __func__, id_size);
6528 		}
6529 		retval = 1;
6530 		goto bailout;
6531 	}
6532 	*alloc_len = id_size;
6533 	bzero(iscsi, id_size);
6534 
6535 	iscsi->format_protocol = SCSI_PROTO_ISCSI;
6536 	if (sep_found == 0)
6537 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_DEVICE;
6538 	else
6539 		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_PORT;
6540 	name_len = id_size - sizeof(*iscsi);
6541 	scsi_ulto2b(name_len, iscsi->additional_length);
6542 	snprintf(iscsi->iscsi_name, name_len, "%s%s", iqn_prefix, id_str);
6543 
6544 	*hdr = (struct scsi_transportid_header *)iscsi;
6545 
6546 bailout:
6547 	return (retval);
6548 }
6549 
6550 /*
6551  * Parse a SCSI over PCIe (SOP) identifier.  The Routing ID can either be
6552  * of the form 'bus,device,function' or 'bus,function'.
6553  */
6554 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)6555 scsi_parse_transportid_sop(char *id_str, struct scsi_transportid_header **hdr,
6556 			   unsigned int *alloc_len,
6557 #ifdef _KERNEL
6558 			   struct malloc_type *type, int flags,
6559 #endif
6560 			   char *error_str, int error_str_len)
6561 {
6562 	struct scsi_transportid_sop *sop;
6563 	unsigned long bus, device, function;
6564 	char *tmpstr, *endptr;
6565 	int retval, device_spec;
6566 
6567 	retval = 0;
6568 	device_spec = 0;
6569 	device = 0;
6570 
6571 	tmpstr = strsep(&id_str, ",");
6572 	if ((tmpstr == NULL)
6573 	 || (*tmpstr == '\0')) {
6574 		if (error_str != NULL) {
6575 			snprintf(error_str, error_str_len, "%s: no ID found",
6576 				 __func__);
6577 		}
6578 		retval = 1;
6579 		goto bailout;
6580 	}
6581 	bus = strtoul(tmpstr, &endptr, 0);
6582 	if (*endptr != '\0') {
6583 		if (error_str != NULL) {
6584 			snprintf(error_str, error_str_len, "%s: error "
6585 				 "parsing PCIe bus %s, number required",
6586 				 __func__, tmpstr);
6587 		}
6588 		retval = 1;
6589 		goto bailout;
6590 	}
6591 	if ((id_str == NULL)
6592 	 || (*id_str == '\0')) {
6593 		if (error_str != NULL) {
6594 			snprintf(error_str, error_str_len, "%s: no PCIe "
6595 				 "device or function found", __func__);
6596 		}
6597 		retval = 1;
6598 		goto bailout;
6599 	}
6600 	tmpstr = strsep(&id_str, ",");
6601 	function = strtoul(tmpstr, &endptr, 0);
6602 	if (*endptr != '\0') {
6603 		if (error_str != NULL) {
6604 			snprintf(error_str, error_str_len, "%s: error "
6605 				 "parsing PCIe device/function %s, number "
6606 				 "required", __func__, tmpstr);
6607 		}
6608 		retval = 1;
6609 		goto bailout;
6610 	}
6611 	/*
6612 	 * Check to see whether the user specified a third value.  If so,
6613 	 * the second is the device.
6614 	 */
6615 	if (id_str != NULL) {
6616 		if (*id_str == '\0') {
6617 			if (error_str != NULL) {
6618 				snprintf(error_str, error_str_len, "%s: "
6619 					 "no PCIe function found", __func__);
6620 			}
6621 			retval = 1;
6622 			goto bailout;
6623 		}
6624 		device = function;
6625 		device_spec = 1;
6626 		function = strtoul(id_str, &endptr, 0);
6627 		if (*endptr != '\0') {
6628 			if (error_str != NULL) {
6629 				snprintf(error_str, error_str_len, "%s: "
6630 					 "error parsing PCIe function %s, "
6631 					 "number required", __func__, id_str);
6632 			}
6633 			retval = 1;
6634 			goto bailout;
6635 		}
6636 	}
6637 	if (bus > SCSI_TRN_SOP_BUS_MAX) {
6638 		if (error_str != NULL) {
6639 			snprintf(error_str, error_str_len, "%s: bus value "
6640 				 "%lu greater than maximum %u", __func__,
6641 				 bus, SCSI_TRN_SOP_BUS_MAX);
6642 		}
6643 		retval = 1;
6644 		goto bailout;
6645 	}
6646 
6647 	if ((device_spec != 0)
6648 	 && (device > SCSI_TRN_SOP_DEV_MASK)) {
6649 		if (error_str != NULL) {
6650 			snprintf(error_str, error_str_len, "%s: device value "
6651 				 "%lu greater than maximum %u", __func__,
6652 				 device, SCSI_TRN_SOP_DEV_MAX);
6653 		}
6654 		retval = 1;
6655 		goto bailout;
6656 	}
6657 
6658 	if (((device_spec != 0)
6659 	  && (function > SCSI_TRN_SOP_FUNC_NORM_MAX))
6660 	 || ((device_spec == 0)
6661 	  && (function > SCSI_TRN_SOP_FUNC_ALT_MAX))) {
6662 		if (error_str != NULL) {
6663 			snprintf(error_str, error_str_len, "%s: function value "
6664 				 "%lu greater than maximum %u", __func__,
6665 				 function, (device_spec == 0) ?
6666 				 SCSI_TRN_SOP_FUNC_ALT_MAX :
6667 				 SCSI_TRN_SOP_FUNC_NORM_MAX);
6668 		}
6669 		retval = 1;
6670 		goto bailout;
6671 	}
6672 
6673 #ifdef _KERNEL
6674 	sop = malloc(sizeof(*sop), type, flags);
6675 #else
6676 	sop = malloc(sizeof(*sop));
6677 #endif
6678 	if (sop == NULL) {
6679 		if (error_str != NULL) {
6680 			snprintf(error_str, error_str_len, "%s: unable to "
6681 				 "allocate %zu bytes", __func__, sizeof(*sop));
6682 		}
6683 		retval = 1;
6684 		goto bailout;
6685 	}
6686 	*alloc_len = sizeof(*sop);
6687 	bzero(sop, sizeof(*sop));
6688 	sop->format_protocol = SCSI_PROTO_SOP | SCSI_TRN_SOP_FORMAT_DEFAULT;
6689 	if (device_spec != 0) {
6690 		struct scsi_sop_routing_id_norm rid;
6691 
6692 		rid.bus = bus;
6693 		rid.devfunc = (device << SCSI_TRN_SOP_DEV_SHIFT) | function;
6694 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6695 		      sizeof(sop->routing_id)));
6696 	} else {
6697 		struct scsi_sop_routing_id_alt rid;
6698 
6699 		rid.bus = bus;
6700 		rid.function = function;
6701 		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6702 		      sizeof(sop->routing_id)));
6703 	}
6704 
6705 	*hdr = (struct scsi_transportid_header *)sop;
6706 bailout:
6707 	return (retval);
6708 }
6709 
6710 /*
6711  * transportid_str: NUL-terminated string with format: protcol,id
6712  *		    The ID is protocol specific.
6713  * hdr:		    Storage will be allocated for the transport ID.
6714  * alloc_len:	    The amount of memory allocated is returned here.
6715  * type:	    Malloc bucket (kernel only).
6716  * flags:	    Malloc flags (kernel only).
6717  * error_str:	    If non-NULL, it will contain error information (without
6718  * 		    a terminating newline) if an error is returned.
6719  * error_str_len:   Allocated length of the error string.
6720  *
6721  * Returns 0 for success, non-zero for failure.
6722  */
6723 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)6724 scsi_parse_transportid(char *transportid_str,
6725 		       struct scsi_transportid_header **hdr,
6726 		       unsigned int *alloc_len,
6727 #ifdef _KERNEL
6728 		       struct malloc_type *type, int flags,
6729 #endif
6730 		       char *error_str, int error_str_len)
6731 {
6732 	char *tmpstr;
6733 	scsi_nv_status status;
6734 	u_int num_proto_entries;
6735 	int retval, table_entry;
6736 
6737 	retval = 0;
6738 	table_entry = 0;
6739 
6740 	/*
6741 	 * We do allow a period as well as a comma to separate the protocol
6742 	 * from the ID string.  This is to accommodate iSCSI names, which
6743 	 * start with "iqn.".
6744 	 */
6745 	tmpstr = strsep(&transportid_str, ",.");
6746 	if (tmpstr == NULL) {
6747 		if (error_str != NULL) {
6748 			snprintf(error_str, error_str_len,
6749 				 "%s: transportid_str is NULL", __func__);
6750 		}
6751 		retval = 1;
6752 		goto bailout;
6753 	}
6754 
6755 	num_proto_entries = nitems(scsi_proto_map);
6756 	status = scsi_get_nv(scsi_proto_map, num_proto_entries, tmpstr,
6757 			     &table_entry, SCSI_NV_FLAG_IG_CASE);
6758 	if (status != SCSI_NV_FOUND) {
6759 		if (error_str != NULL) {
6760 			snprintf(error_str, error_str_len, "%s: %s protocol "
6761 				 "name %s", __func__,
6762 				 (status == SCSI_NV_AMBIGUOUS) ? "ambiguous" :
6763 				 "invalid", tmpstr);
6764 		}
6765 		retval = 1;
6766 		goto bailout;
6767 	}
6768 	switch (scsi_proto_map[table_entry].value) {
6769 	case SCSI_PROTO_FC:
6770 	case SCSI_PROTO_1394:
6771 	case SCSI_PROTO_SAS:
6772 		retval = scsi_parse_transportid_64bit(
6773 		    scsi_proto_map[table_entry].value, transportid_str, hdr,
6774 		    alloc_len,
6775 #ifdef _KERNEL
6776 		    type, flags,
6777 #endif
6778 		    error_str, error_str_len);
6779 		break;
6780 	case SCSI_PROTO_SPI:
6781 		retval = scsi_parse_transportid_spi(transportid_str, hdr,
6782 		    alloc_len,
6783 #ifdef _KERNEL
6784 		    type, flags,
6785 #endif
6786 		    error_str, error_str_len);
6787 		break;
6788 	case SCSI_PROTO_RDMA:
6789 		retval = scsi_parse_transportid_rdma(transportid_str, hdr,
6790 		    alloc_len,
6791 #ifdef _KERNEL
6792 		    type, flags,
6793 #endif
6794 		    error_str, error_str_len);
6795 		break;
6796 	case SCSI_PROTO_ISCSI:
6797 		retval = scsi_parse_transportid_iscsi(transportid_str, hdr,
6798 		    alloc_len,
6799 #ifdef _KERNEL
6800 		    type, flags,
6801 #endif
6802 		    error_str, error_str_len);
6803 		break;
6804 	case SCSI_PROTO_SOP:
6805 		retval = scsi_parse_transportid_sop(transportid_str, hdr,
6806 		    alloc_len,
6807 #ifdef _KERNEL
6808 		    type, flags,
6809 #endif
6810 		    error_str, error_str_len);
6811 		break;
6812 	case SCSI_PROTO_SSA:
6813 	case SCSI_PROTO_ADITP:
6814 	case SCSI_PROTO_ATA:
6815 	case SCSI_PROTO_UAS:
6816 	case SCSI_PROTO_NONE:
6817 	default:
6818 		/*
6819 		 * There is no format defined for a Transport ID for these
6820 		 * protocols.  So even if the user gives us something, we
6821 		 * have no way to turn it into a standard SCSI Transport ID.
6822 		 */
6823 		retval = 1;
6824 		if (error_str != NULL) {
6825 			snprintf(error_str, error_str_len, "%s: no Transport "
6826 				 "ID format exists for protocol %s",
6827 				 __func__, tmpstr);
6828 		}
6829 		goto bailout;
6830 		break;	/* NOTREACHED */
6831 	}
6832 bailout:
6833 	return (retval);
6834 }
6835 
6836 struct scsi_attrib_table_entry scsi_mam_attr_table[] = {
6837 	{ SMA_ATTR_REM_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6838 	  "Remaining Capacity in Partition",
6839 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,/*parse_str*/ NULL },
6840 	{ SMA_ATTR_MAX_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6841 	  "Maximum Capacity in Partition",
6842 	  /*suffix*/"MB", /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6843 	{ SMA_ATTR_TAPEALERT_FLAGS, SCSI_ATTR_FLAG_HEX,
6844 	  "TapeAlert Flags",
6845 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6846 	{ SMA_ATTR_LOAD_COUNT, SCSI_ATTR_FLAG_NONE,
6847 	  "Load Count",
6848 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6849 	{ SMA_ATTR_MAM_SPACE_REMAINING, SCSI_ATTR_FLAG_NONE,
6850 	  "MAM Space Remaining",
6851 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6852 	  /*parse_str*/ NULL },
6853 	{ SMA_ATTR_DEV_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6854 	  "Assigning Organization",
6855 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6856 	  /*parse_str*/ NULL },
6857 	{ SMA_ATTR_FORMAT_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6858 	  "Format Density Code",
6859 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6860 	{ SMA_ATTR_INITIALIZATION_COUNT, SCSI_ATTR_FLAG_NONE,
6861 	  "Initialization Count",
6862 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6863 	{ SMA_ATTR_VOLUME_ID, SCSI_ATTR_FLAG_NONE,
6864 	  "Volume Identifier",
6865 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6866 	  /*parse_str*/ NULL },
6867 	{ SMA_ATTR_VOLUME_CHANGE_REF, SCSI_ATTR_FLAG_HEX,
6868 	  "Volume Change Reference",
6869 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6870 	  /*parse_str*/ NULL },
6871 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD, SCSI_ATTR_FLAG_NONE,
6872 	  "Device Vendor/Serial at Last Load",
6873 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6874 	  /*parse_str*/ NULL },
6875 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_1, SCSI_ATTR_FLAG_NONE,
6876 	  "Device Vendor/Serial at Last Load - 1",
6877 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6878 	  /*parse_str*/ NULL },
6879 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_2, SCSI_ATTR_FLAG_NONE,
6880 	  "Device Vendor/Serial at Last Load - 2",
6881 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6882 	  /*parse_str*/ NULL },
6883 	{ SMA_ATTR_DEV_SERIAL_LAST_LOAD_3, SCSI_ATTR_FLAG_NONE,
6884 	  "Device Vendor/Serial at Last Load - 3",
6885 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6886 	  /*parse_str*/ NULL },
6887 	{ SMA_ATTR_TOTAL_MB_WRITTEN_LT, SCSI_ATTR_FLAG_NONE,
6888 	  "Total MB Written in Medium Life",
6889 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6890 	  /*parse_str*/ NULL },
6891 	{ SMA_ATTR_TOTAL_MB_READ_LT, SCSI_ATTR_FLAG_NONE,
6892 	  "Total MB Read in Medium Life",
6893 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6894 	  /*parse_str*/ NULL },
6895 	{ SMA_ATTR_TOTAL_MB_WRITTEN_CUR, SCSI_ATTR_FLAG_NONE,
6896 	  "Total MB Written in Current/Last Load",
6897 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6898 	  /*parse_str*/ NULL },
6899 	{ SMA_ATTR_TOTAL_MB_READ_CUR, SCSI_ATTR_FLAG_NONE,
6900 	  "Total MB Read in Current/Last Load",
6901 	  /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6902 	  /*parse_str*/ NULL },
6903 	{ SMA_ATTR_FIRST_ENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6904 	  "Logical Position of First Encrypted Block",
6905 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6906 	  /*parse_str*/ NULL },
6907 	{ SMA_ATTR_NEXT_UNENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6908 	  "Logical Position of First Unencrypted Block after First "
6909 	  "Encrypted Block",
6910 	  /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6911 	  /*parse_str*/ NULL },
6912 	{ SMA_ATTR_MEDIUM_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6913 	  "Medium Usage History",
6914 	  /*suffix*/ NULL, /*to_str*/ NULL,
6915 	  /*parse_str*/ NULL },
6916 	{ SMA_ATTR_PART_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6917 	  "Partition Usage History",
6918 	  /*suffix*/ NULL, /*to_str*/ NULL,
6919 	  /*parse_str*/ NULL },
6920 	{ SMA_ATTR_MED_MANUF, SCSI_ATTR_FLAG_NONE,
6921 	  "Medium Manufacturer",
6922 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6923 	  /*parse_str*/ NULL },
6924 	{ SMA_ATTR_MED_SERIAL, SCSI_ATTR_FLAG_NONE,
6925 	  "Medium Serial Number",
6926 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6927 	  /*parse_str*/ NULL },
6928 	{ SMA_ATTR_MED_LENGTH, SCSI_ATTR_FLAG_NONE,
6929 	  "Medium Length",
6930 	  /*suffix*/"m", /*to_str*/ scsi_attrib_int_sbuf,
6931 	  /*parse_str*/ NULL },
6932 	{ SMA_ATTR_MED_WIDTH, SCSI_ATTR_FLAG_FP | SCSI_ATTR_FLAG_DIV_10 |
6933 	  SCSI_ATTR_FLAG_FP_1DIGIT,
6934 	  "Medium Width",
6935 	  /*suffix*/"mm", /*to_str*/ scsi_attrib_int_sbuf,
6936 	  /*parse_str*/ NULL },
6937 	{ SMA_ATTR_MED_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6938 	  "Assigning Organization",
6939 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6940 	  /*parse_str*/ NULL },
6941 	{ SMA_ATTR_MED_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6942 	  "Medium Density Code",
6943 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6944 	  /*parse_str*/ NULL },
6945 	{ SMA_ATTR_MED_MANUF_DATE, SCSI_ATTR_FLAG_NONE,
6946 	  "Medium Manufacture Date",
6947 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6948 	  /*parse_str*/ NULL },
6949 	{ SMA_ATTR_MAM_CAPACITY, SCSI_ATTR_FLAG_NONE,
6950 	  "MAM Capacity",
6951 	  /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6952 	  /*parse_str*/ NULL },
6953 	{ SMA_ATTR_MED_TYPE, SCSI_ATTR_FLAG_HEX,
6954 	  "Medium Type",
6955 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6956 	  /*parse_str*/ NULL },
6957 	{ SMA_ATTR_MED_TYPE_INFO, SCSI_ATTR_FLAG_HEX,
6958 	  "Medium Type Information",
6959 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6960 	  /*parse_str*/ NULL },
6961 	{ SMA_ATTR_MED_SERIAL_NUM, SCSI_ATTR_FLAG_NONE,
6962 	  "Medium Serial Number",
6963 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6964 	  /*parse_str*/ NULL },
6965 	{ SMA_ATTR_APP_VENDOR, SCSI_ATTR_FLAG_NONE,
6966 	  "Application Vendor",
6967 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6968 	  /*parse_str*/ NULL },
6969 	{ SMA_ATTR_APP_NAME, SCSI_ATTR_FLAG_NONE,
6970 	  "Application Name",
6971 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6972 	  /*parse_str*/ NULL },
6973 	{ SMA_ATTR_APP_VERSION, SCSI_ATTR_FLAG_NONE,
6974 	  "Application Version",
6975 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6976 	  /*parse_str*/ NULL },
6977 	{ SMA_ATTR_USER_MED_TEXT_LABEL, SCSI_ATTR_FLAG_NONE,
6978 	  "User Medium Text Label",
6979 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6980 	  /*parse_str*/ NULL },
6981 	{ SMA_ATTR_LAST_WRITTEN_TIME, SCSI_ATTR_FLAG_NONE,
6982 	  "Date and Time Last Written",
6983 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6984 	  /*parse_str*/ NULL },
6985 	{ SMA_ATTR_TEXT_LOCAL_ID, SCSI_ATTR_FLAG_HEX,
6986 	  "Text Localization Identifier",
6987 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6988 	  /*parse_str*/ NULL },
6989 	{ SMA_ATTR_BARCODE, SCSI_ATTR_FLAG_NONE,
6990 	  "Barcode",
6991 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6992 	  /*parse_str*/ NULL },
6993 	{ SMA_ATTR_HOST_OWNER_NAME, SCSI_ATTR_FLAG_NONE,
6994 	  "Owning Host Textual Name",
6995 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6996 	  /*parse_str*/ NULL },
6997 	{ SMA_ATTR_MEDIA_POOL, SCSI_ATTR_FLAG_NONE,
6998 	  "Media Pool",
6999 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
7000 	  /*parse_str*/ NULL },
7001 	{ SMA_ATTR_PART_USER_LABEL, SCSI_ATTR_FLAG_NONE,
7002 	  "Partition User Text Label",
7003 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
7004 	  /*parse_str*/ NULL },
7005 	{ SMA_ATTR_LOAD_UNLOAD_AT_PART, SCSI_ATTR_FLAG_NONE,
7006 	  "Load/Unload at Partition",
7007 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
7008 	  /*parse_str*/ NULL },
7009 	{ SMA_ATTR_APP_FORMAT_VERSION, SCSI_ATTR_FLAG_NONE,
7010 	  "Application Format Version",
7011 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
7012 	  /*parse_str*/ NULL },
7013 	{ SMA_ATTR_VOL_COHERENCY_INFO, SCSI_ATTR_FLAG_NONE,
7014 	  "Volume Coherency Information",
7015 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_volcoh_sbuf,
7016 	  /*parse_str*/ NULL },
7017 	{ 0x0ff1, SCSI_ATTR_FLAG_NONE,
7018 	  "Spectra MLM Creation",
7019 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7020 	  /*parse_str*/ NULL },
7021 	{ 0x0ff2, SCSI_ATTR_FLAG_NONE,
7022 	  "Spectra MLM C3",
7023 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7024 	  /*parse_str*/ NULL },
7025 	{ 0x0ff3, SCSI_ATTR_FLAG_NONE,
7026 	  "Spectra MLM RW",
7027 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7028 	  /*parse_str*/ NULL },
7029 	{ 0x0ff4, SCSI_ATTR_FLAG_NONE,
7030 	  "Spectra MLM SDC List",
7031 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7032 	  /*parse_str*/ NULL },
7033 	{ 0x0ff7, SCSI_ATTR_FLAG_NONE,
7034 	  "Spectra MLM Post Scan",
7035 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7036 	  /*parse_str*/ NULL },
7037 	{ 0x0ffe, SCSI_ATTR_FLAG_NONE,
7038 	  "Spectra MLM Checksum",
7039 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7040 	  /*parse_str*/ NULL },
7041 	{ 0x17f1, SCSI_ATTR_FLAG_NONE,
7042 	  "Spectra MLM Creation",
7043 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7044 	  /*parse_str*/ NULL },
7045 	{ 0x17f2, SCSI_ATTR_FLAG_NONE,
7046 	  "Spectra MLM C3",
7047 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7048 	  /*parse_str*/ NULL },
7049 	{ 0x17f3, SCSI_ATTR_FLAG_NONE,
7050 	  "Spectra MLM RW",
7051 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7052 	  /*parse_str*/ NULL },
7053 	{ 0x17f4, SCSI_ATTR_FLAG_NONE,
7054 	  "Spectra MLM SDC List",
7055 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7056 	  /*parse_str*/ NULL },
7057 	{ 0x17f7, SCSI_ATTR_FLAG_NONE,
7058 	  "Spectra MLM Post Scan",
7059 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7060 	  /*parse_str*/ NULL },
7061 	{ 0x17ff, SCSI_ATTR_FLAG_NONE,
7062 	  "Spectra MLM Checksum",
7063 	  /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
7064 	  /*parse_str*/ NULL },
7065 };
7066 
7067 /*
7068  * Print out Volume Coherency Information (Attribute 0x080c).
7069  * This field has two variable length members, including one at the
7070  * beginning, so it isn't practical to have a fixed structure definition.
7071  * This is current as of SSC4r03 (see section 4.2.21.3), dated March 25,
7072  * 2013.
7073  */
7074 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)7075 scsi_attrib_volcoh_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7076 			 uint32_t valid_len, uint32_t flags,
7077 			 uint32_t output_flags, char *error_str,
7078 			 int error_str_len)
7079 {
7080 	size_t avail_len;
7081 	uint32_t field_size;
7082 	uint64_t tmp_val;
7083 	uint8_t *cur_ptr;
7084 	int retval;
7085 	int vcr_len, as_len;
7086 
7087 	retval = 0;
7088 	tmp_val = 0;
7089 
7090 	field_size = scsi_2btoul(hdr->length);
7091 	avail_len = valid_len - sizeof(*hdr);
7092 	if (field_size > avail_len) {
7093 		if (error_str != NULL) {
7094 			snprintf(error_str, error_str_len, "Available "
7095 				 "length of attribute ID 0x%.4x %zu < field "
7096 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7097 				 field_size);
7098 		}
7099 		retval = 1;
7100 		goto bailout;
7101 	} else if (field_size == 0) {
7102 		/*
7103 		 * It isn't clear from the spec whether a field length of
7104 		 * 0 is invalid here.  It probably is, but be lenient here
7105 		 * to avoid inconveniencing the user.
7106 		 */
7107 		goto bailout;
7108 	}
7109 	cur_ptr = hdr->attribute;
7110 	vcr_len = *cur_ptr;
7111 	cur_ptr++;
7112 
7113 	sbuf_cat(sb, "\n\tVolume Change Reference Value:");
7114 
7115 	switch (vcr_len) {
7116 	case 0:
7117 		if (error_str != NULL) {
7118 			snprintf(error_str, error_str_len, "Volume Change "
7119 				 "Reference value has length of 0");
7120 		}
7121 		retval = 1;
7122 		goto bailout;
7123 		break; /*NOTREACHED*/
7124 	case 1:
7125 		tmp_val = *cur_ptr;
7126 		break;
7127 	case 2:
7128 		tmp_val = scsi_2btoul(cur_ptr);
7129 		break;
7130 	case 3:
7131 		tmp_val = scsi_3btoul(cur_ptr);
7132 		break;
7133 	case 4:
7134 		tmp_val = scsi_4btoul(cur_ptr);
7135 		break;
7136 	case 8:
7137 		tmp_val = scsi_8btou64(cur_ptr);
7138 		break;
7139 	default:
7140 		sbuf_putc(sb, '\n');
7141 		sbuf_hexdump(sb, cur_ptr, vcr_len, NULL, 0);
7142 		break;
7143 	}
7144 	if (vcr_len <= 8)
7145 		sbuf_printf(sb, " 0x%jx\n", (uintmax_t)tmp_val);
7146 
7147 	cur_ptr += vcr_len;
7148 	tmp_val = scsi_8btou64(cur_ptr);
7149 	sbuf_printf(sb, "\tVolume Coherency Count: %ju\n", (uintmax_t)tmp_val);
7150 
7151 	cur_ptr += sizeof(tmp_val);
7152 	tmp_val = scsi_8btou64(cur_ptr);
7153 	sbuf_printf(sb, "\tVolume Coherency Set Identifier: 0x%jx\n",
7154 		    (uintmax_t)tmp_val);
7155 
7156 	/*
7157 	 * Figure out how long the Application Client Specific Information
7158 	 * is and produce a hexdump.
7159 	 */
7160 	cur_ptr += sizeof(tmp_val);
7161 	as_len = scsi_2btoul(cur_ptr);
7162 	cur_ptr += sizeof(uint16_t);
7163 	sbuf_cat(sb, "\tApplication Client Specific Information: ");
7164 	if (((as_len == SCSI_LTFS_VER0_LEN)
7165 	  || (as_len == SCSI_LTFS_VER1_LEN))
7166 	 && (strncmp(cur_ptr, SCSI_LTFS_STR_NAME, SCSI_LTFS_STR_LEN) == 0)) {
7167 		sbuf_cat(sb, "LTFS\n");
7168 		cur_ptr += SCSI_LTFS_STR_LEN + 1;
7169 		if (cur_ptr[SCSI_LTFS_UUID_LEN] != '\0')
7170 			cur_ptr[SCSI_LTFS_UUID_LEN] = '\0';
7171 		sbuf_printf(sb, "\tLTFS UUID: %s\n", cur_ptr);
7172 		cur_ptr += SCSI_LTFS_UUID_LEN + 1;
7173 		/* XXX KDM check the length */
7174 		sbuf_printf(sb, "\tLTFS Version: %d\n", *cur_ptr);
7175 	} else {
7176 		sbuf_cat(sb, "Unknown\n");
7177 		sbuf_hexdump(sb, cur_ptr, as_len, NULL, 0);
7178 	}
7179 
7180 bailout:
7181 	return (retval);
7182 }
7183 
7184 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)7185 scsi_attrib_vendser_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7186 			 uint32_t valid_len, uint32_t flags,
7187 			 uint32_t output_flags, char *error_str,
7188 			 int error_str_len)
7189 {
7190 	size_t avail_len;
7191 	uint32_t field_size;
7192 	struct scsi_attrib_vendser *vendser;
7193 	cam_strvis_flags strvis_flags;
7194 	int retval = 0;
7195 
7196 	field_size = scsi_2btoul(hdr->length);
7197 	avail_len = valid_len - sizeof(*hdr);
7198 	if (field_size > avail_len) {
7199 		if (error_str != NULL) {
7200 			snprintf(error_str, error_str_len, "Available "
7201 				 "length of attribute ID 0x%.4x %zu < field "
7202 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7203 				 field_size);
7204 		}
7205 		retval = 1;
7206 		goto bailout;
7207 	} else if (field_size == 0) {
7208 		/*
7209 		 * A field size of 0 doesn't make sense here.  The device
7210 		 * can at least give you the vendor ID, even if it can't
7211 		 * give you the serial number.
7212 		 */
7213 		if (error_str != NULL) {
7214 			snprintf(error_str, error_str_len, "The length of "
7215 				 "attribute ID 0x%.4x is 0",
7216 				 scsi_2btoul(hdr->id));
7217 		}
7218 		retval = 1;
7219 		goto bailout;
7220 	}
7221 	vendser = (struct scsi_attrib_vendser *)hdr->attribute;
7222 
7223 	switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7224 	case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7225 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7226 		break;
7227 	case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7228 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7229 		break;
7230 	case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7231 	default:
7232 		strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7233 		break;
7234 	}
7235 	cam_strvis_sbuf(sb, vendser->vendor, sizeof(vendser->vendor),
7236 	    strvis_flags);
7237 	sbuf_putc(sb, ' ');
7238 	cam_strvis_sbuf(sb, vendser->serial_num, sizeof(vendser->serial_num),
7239 	    strvis_flags);
7240 bailout:
7241 	return (retval);
7242 }
7243 
7244 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)7245 scsi_attrib_hexdump_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7246 			 uint32_t valid_len, uint32_t flags,
7247 			 uint32_t output_flags, char *error_str,
7248 			 int error_str_len)
7249 {
7250 	uint32_t field_size;
7251 	ssize_t avail_len;
7252 	uint32_t print_len;
7253 	uint8_t *num_ptr;
7254 	int retval = 0;
7255 
7256 	field_size = scsi_2btoul(hdr->length);
7257 	avail_len = valid_len - sizeof(*hdr);
7258 	print_len = MIN(avail_len, field_size);
7259 	num_ptr = hdr->attribute;
7260 
7261 	if (print_len > 0) {
7262 		sbuf_putc(sb, '\n');
7263 		sbuf_hexdump(sb, num_ptr, print_len, NULL, 0);
7264 	}
7265 
7266 	return (retval);
7267 }
7268 
7269 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)7270 scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7271 		     uint32_t valid_len, uint32_t flags,
7272 		     uint32_t output_flags, char *error_str,
7273 		     int error_str_len)
7274 {
7275 	uint64_t print_number;
7276 	size_t avail_len;
7277 	uint32_t number_size;
7278 	int retval = 0;
7279 
7280 	number_size = scsi_2btoul(hdr->length);
7281 
7282 	avail_len = valid_len - sizeof(*hdr);
7283 	if (avail_len < number_size) {
7284 		if (error_str != NULL) {
7285 			snprintf(error_str, error_str_len, "Available "
7286 				 "length of attribute ID 0x%.4x %zu < field "
7287 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7288 				 number_size);
7289 		}
7290 		retval = 1;
7291 		goto bailout;
7292 	}
7293 
7294 	switch (number_size) {
7295 	case 0:
7296 		/*
7297 		 * We don't treat this as an error, since there may be
7298 		 * scenarios where a device reports a field but then gives
7299 		 * a length of 0.  See the note in scsi_attrib_ascii_sbuf().
7300 		 */
7301 		goto bailout;
7302 		break; /*NOTREACHED*/
7303 	case 1:
7304 		print_number = hdr->attribute[0];
7305 		break;
7306 	case 2:
7307 		print_number = scsi_2btoul(hdr->attribute);
7308 		break;
7309 	case 3:
7310 		print_number = scsi_3btoul(hdr->attribute);
7311 		break;
7312 	case 4:
7313 		print_number = scsi_4btoul(hdr->attribute);
7314 		break;
7315 	case 8:
7316 		print_number = scsi_8btou64(hdr->attribute);
7317 		break;
7318 	default:
7319 		/*
7320 		 * If we wind up here, the number is too big to print
7321 		 * normally, so just do a hexdump.
7322 		 */
7323 		retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7324 						  flags, output_flags,
7325 						  error_str, error_str_len);
7326 		goto bailout;
7327 		break;
7328 	}
7329 
7330 	if (flags & SCSI_ATTR_FLAG_FP) {
7331 #ifndef _KERNEL
7332 		long double num_float;
7333 
7334 		num_float = (long double)print_number;
7335 
7336 		if (flags & SCSI_ATTR_FLAG_DIV_10)
7337 			num_float /= 10;
7338 
7339 		sbuf_printf(sb, "%.*Lf", (flags & SCSI_ATTR_FLAG_FP_1DIGIT) ?
7340 			    1 : 0, num_float);
7341 #else /* _KERNEL */
7342 		sbuf_printf(sb, "%ju", (flags & SCSI_ATTR_FLAG_DIV_10) ?
7343 			    (print_number / 10) : print_number);
7344 #endif /* _KERNEL */
7345 	} else if (flags & SCSI_ATTR_FLAG_HEX) {
7346 		sbuf_printf(sb, "0x%jx", (uintmax_t)print_number);
7347 	} else
7348 		sbuf_printf(sb, "%ju", (uintmax_t)print_number);
7349 
7350 bailout:
7351 	return (retval);
7352 }
7353 
7354 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)7355 scsi_attrib_ascii_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7356 		       uint32_t valid_len, uint32_t flags,
7357 		       uint32_t output_flags, char *error_str,
7358 		       int error_str_len)
7359 {
7360 	size_t avail_len;
7361 	uint32_t field_size, print_size;
7362 	int retval = 0;
7363 
7364 	avail_len = valid_len - sizeof(*hdr);
7365 	field_size = scsi_2btoul(hdr->length);
7366 	print_size = MIN(avail_len, field_size);
7367 
7368 	if (print_size > 0) {
7369 		cam_strvis_flags strvis_flags;
7370 
7371 		switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7372 		case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7373 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7374 			break;
7375 		case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7376 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7377 			break;
7378 		case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7379 		default:
7380 			strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7381 			break;
7382 		}
7383 		cam_strvis_sbuf(sb, hdr->attribute, print_size, strvis_flags);
7384 	} else if (avail_len < field_size) {
7385 		/*
7386 		 * We only report an error if the user didn't allocate
7387 		 * enough space to hold the full value of this field.  If
7388 		 * the field length is 0, that is allowed by the spec.
7389 		 * e.g. in SPC-4r37, section 7.4.2.2.5, VOLUME IDENTIFIER
7390 		 * "This attribute indicates the current volume identifier
7391 		 * (see SMC-3) of the medium. If the device server supports
7392 		 * this attribute but does not have access to the volume
7393 		 * identifier, the device server shall report this attribute
7394 		 * with an attribute length value of zero."
7395 		 */
7396 		if (error_str != NULL) {
7397 			snprintf(error_str, error_str_len, "Available "
7398 				 "length of attribute ID 0x%.4x %zu < field "
7399 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7400 				 field_size);
7401 		}
7402 		retval = 1;
7403 	}
7404 
7405 	return (retval);
7406 }
7407 
7408 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)7409 scsi_attrib_text_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7410 		      uint32_t valid_len, uint32_t flags,
7411 		      uint32_t output_flags, char *error_str,
7412 		      int error_str_len)
7413 {
7414 	size_t avail_len;
7415 	uint32_t field_size, print_size;
7416 	int retval = 0;
7417 	int esc_text = 1;
7418 
7419 	avail_len = valid_len - sizeof(*hdr);
7420 	field_size = scsi_2btoul(hdr->length);
7421 	print_size = MIN(avail_len, field_size);
7422 
7423 	if ((output_flags & SCSI_ATTR_OUTPUT_TEXT_MASK) ==
7424 	     SCSI_ATTR_OUTPUT_TEXT_RAW)
7425 		esc_text = 0;
7426 
7427 	if (print_size > 0) {
7428 		uint32_t i;
7429 
7430 		for (i = 0; i < print_size; i++) {
7431 			if (hdr->attribute[i] == '\0')
7432 				continue;
7433 			else if (((unsigned char)hdr->attribute[i] < 0x80)
7434 			      || (esc_text == 0))
7435 				sbuf_putc(sb, hdr->attribute[i]);
7436 			else
7437 				sbuf_printf(sb, "%%%02x",
7438 				    (unsigned char)hdr->attribute[i]);
7439 		}
7440 	} else if (avail_len < field_size) {
7441 		/*
7442 		 * We only report an error if the user didn't allocate
7443 		 * enough space to hold the full value of this field.
7444 		 */
7445 		if (error_str != NULL) {
7446 			snprintf(error_str, error_str_len, "Available "
7447 				 "length of attribute ID 0x%.4x %zu < field "
7448 				 "length %u", scsi_2btoul(hdr->id), avail_len,
7449 				 field_size);
7450 		}
7451 		retval = 1;
7452 	}
7453 
7454 	return (retval);
7455 }
7456 
7457 struct scsi_attrib_table_entry *
scsi_find_attrib_entry(struct scsi_attrib_table_entry * table,size_t num_table_entries,uint32_t id)7458 scsi_find_attrib_entry(struct scsi_attrib_table_entry *table,
7459 		       size_t num_table_entries, uint32_t id)
7460 {
7461 	uint32_t i;
7462 
7463 	for (i = 0; i < num_table_entries; i++) {
7464 		if (table[i].id == id)
7465 			return (&table[i]);
7466 	}
7467 
7468 	return (NULL);
7469 }
7470 
7471 struct scsi_attrib_table_entry *
scsi_get_attrib_entry(uint32_t id)7472 scsi_get_attrib_entry(uint32_t id)
7473 {
7474 	return (scsi_find_attrib_entry(scsi_mam_attr_table,
7475 	    nitems(scsi_mam_attr_table), id));
7476 }
7477 
7478 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)7479 scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len,
7480    struct scsi_mam_attribute_header *hdr, uint32_t output_flags,
7481    char *error_str, size_t error_str_len)
7482 {
7483 	int retval;
7484 
7485 	switch (hdr->byte2 & SMA_FORMAT_MASK) {
7486 	case SMA_FORMAT_ASCII:
7487 		retval = scsi_attrib_ascii_sbuf(sb, hdr, valid_len,
7488 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,error_str_len);
7489 		break;
7490 	case SMA_FORMAT_BINARY:
7491 		if (scsi_2btoul(hdr->length) <= 8)
7492 			retval = scsi_attrib_int_sbuf(sb, hdr, valid_len,
7493 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7494 			    error_str_len);
7495 		else
7496 			retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7497 			    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7498 			    error_str_len);
7499 		break;
7500 	case SMA_FORMAT_TEXT:
7501 		retval = scsi_attrib_text_sbuf(sb, hdr, valid_len,
7502 		    SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7503 		    error_str_len);
7504 		break;
7505 	default:
7506 		if (error_str != NULL) {
7507 			snprintf(error_str, error_str_len, "Unknown attribute "
7508 			    "format 0x%x", hdr->byte2 & SMA_FORMAT_MASK);
7509 		}
7510 		retval = 1;
7511 		goto bailout;
7512 		break; /*NOTREACHED*/
7513 	}
7514 
7515 	sbuf_trim(sb);
7516 
7517 bailout:
7518 
7519 	return (retval);
7520 }
7521 
7522 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)7523 scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags,
7524 			struct scsi_mam_attribute_header *hdr,
7525 			uint32_t valid_len, const char *desc)
7526 {
7527 	int need_space = 0;
7528 	uint32_t len;
7529 	uint32_t id;
7530 
7531 	/*
7532 	 * We can't do anything if we don't have enough valid data for the
7533 	 * header.
7534 	 */
7535 	if (valid_len < sizeof(*hdr))
7536 		return;
7537 
7538 	id = scsi_2btoul(hdr->id);
7539 	/*
7540 	 * Note that we print out the value of the attribute listed in the
7541 	 * header, regardless of whether we actually got that many bytes
7542 	 * back from the device through the controller.  A truncated result
7543 	 * could be the result of a failure to ask for enough data; the
7544 	 * header indicates how many bytes are allocated for this attribute
7545 	 * in the MAM.
7546 	 */
7547 	len = scsi_2btoul(hdr->length);
7548 
7549 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_MASK) ==
7550 	    SCSI_ATTR_OUTPUT_FIELD_NONE)
7551 		return;
7552 
7553 	if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_DESC)
7554 	 && (desc != NULL)) {
7555 		sbuf_cat(sb, desc);
7556 		need_space = 1;
7557 	}
7558 
7559 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_NUM) {
7560 		sbuf_printf(sb, "%s(0x%.4x)", (need_space) ? " " : "", id);
7561 		need_space = 0;
7562 	}
7563 
7564 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_SIZE) {
7565 		sbuf_printf(sb, "%s[%d]", (need_space) ? " " : "", len);
7566 		need_space = 0;
7567 	}
7568 	if (output_flags & SCSI_ATTR_OUTPUT_FIELD_RW) {
7569 		sbuf_printf(sb, "%s(%s)", (need_space) ? " " : "",
7570 			    (hdr->byte2 & SMA_READ_ONLY) ? "RO" : "RW");
7571 	}
7572 	sbuf_cat(sb, ": ");
7573 }
7574 
7575 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)7576 scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7577 		 uint32_t valid_len, struct scsi_attrib_table_entry *user_table,
7578 		 size_t num_user_entries, int prefer_user_table,
7579 		 uint32_t output_flags, char *error_str, int error_str_len)
7580 {
7581 	int retval;
7582 	struct scsi_attrib_table_entry *table1 = NULL, *table2 = NULL;
7583 	struct scsi_attrib_table_entry *entry = NULL;
7584 	size_t table1_size = 0, table2_size = 0;
7585 	uint32_t id;
7586 
7587 	retval = 0;
7588 
7589 	if (valid_len < sizeof(*hdr)) {
7590 		retval = 1;
7591 		goto bailout;
7592 	}
7593 
7594 	id = scsi_2btoul(hdr->id);
7595 
7596 	if (user_table != NULL) {
7597 		if (prefer_user_table != 0) {
7598 			table1 = user_table;
7599 			table1_size = num_user_entries;
7600 			table2 = scsi_mam_attr_table;
7601 			table2_size = nitems(scsi_mam_attr_table);
7602 		} else {
7603 			table1 = scsi_mam_attr_table;
7604 			table1_size = nitems(scsi_mam_attr_table);
7605 			table2 = user_table;
7606 			table2_size = num_user_entries;
7607 		}
7608 	} else {
7609 		table1 = scsi_mam_attr_table;
7610 		table1_size = nitems(scsi_mam_attr_table);
7611 	}
7612 
7613 	entry = scsi_find_attrib_entry(table1, table1_size, id);
7614 	if (entry != NULL) {
7615 		scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len,
7616 					entry->desc);
7617 		if (entry->to_str == NULL)
7618 			goto print_default;
7619 		retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7620 				       output_flags, error_str, error_str_len);
7621 		goto bailout;
7622 	}
7623 	if (table2 != NULL) {
7624 		entry = scsi_find_attrib_entry(table2, table2_size, id);
7625 		if (entry != NULL) {
7626 			if (entry->to_str == NULL)
7627 				goto print_default;
7628 
7629 			scsi_attrib_prefix_sbuf(sb, output_flags, hdr,
7630 						valid_len, entry->desc);
7631 			retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7632 					       output_flags, error_str,
7633 					       error_str_len);
7634 			goto bailout;
7635 		}
7636 	}
7637 
7638 	scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, NULL);
7639 
7640 print_default:
7641 	retval = scsi_attrib_value_sbuf(sb, valid_len, hdr, output_flags,
7642 	    error_str, error_str_len);
7643 bailout:
7644 	if (retval == 0) {
7645 	 	if ((entry != NULL)
7646 		 && (entry->suffix != NULL))
7647 			sbuf_printf(sb, " %s", entry->suffix);
7648 
7649 		sbuf_trim(sb);
7650 		sbuf_putc(sb, '\n');
7651 	}
7652 
7653 	return (retval);
7654 }
7655 
7656 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)7657 scsi_test_unit_ready(struct ccb_scsiio *csio, uint32_t retries,
7658 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
7659 		     uint8_t tag_action, uint8_t sense_len, uint32_t timeout)
7660 {
7661 	struct scsi_test_unit_ready *scsi_cmd;
7662 
7663 	cam_fill_csio(csio,
7664 		      retries,
7665 		      cbfcnp,
7666 		      CAM_DIR_NONE,
7667 		      tag_action,
7668 		      /*data_ptr*/NULL,
7669 		      /*dxfer_len*/0,
7670 		      sense_len,
7671 		      sizeof(*scsi_cmd),
7672 		      timeout);
7673 
7674 	scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
7675 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7676 	scsi_cmd->opcode = TEST_UNIT_READY;
7677 }
7678 
7679 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)7680 scsi_request_sense(struct ccb_scsiio *csio, uint32_t retries,
7681 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7682 		   void *data_ptr, uint8_t dxfer_len, uint8_t tag_action,
7683 		   uint8_t sense_len, uint32_t timeout)
7684 {
7685 	struct scsi_request_sense *scsi_cmd;
7686 
7687 	cam_fill_csio(csio,
7688 		      retries,
7689 		      cbfcnp,
7690 		      CAM_DIR_IN,
7691 		      tag_action,
7692 		      data_ptr,
7693 		      dxfer_len,
7694 		      sense_len,
7695 		      sizeof(*scsi_cmd),
7696 		      timeout);
7697 
7698 	scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
7699 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7700 	scsi_cmd->opcode = REQUEST_SENSE;
7701 	scsi_cmd->length = dxfer_len;
7702 }
7703 
7704 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)7705 scsi_inquiry(struct ccb_scsiio *csio, uint32_t retries,
7706 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7707 	     uint8_t tag_action, uint8_t *inq_buf, uint32_t inq_len,
7708 	     int evpd, uint8_t page_code, uint8_t sense_len,
7709 	     uint32_t timeout)
7710 {
7711 	struct scsi_inquiry *scsi_cmd;
7712 
7713 	cam_fill_csio(csio,
7714 		      retries,
7715 		      cbfcnp,
7716 		      /*flags*/CAM_DIR_IN,
7717 		      tag_action,
7718 		      /*data_ptr*/inq_buf,
7719 		      /*dxfer_len*/inq_len,
7720 		      sense_len,
7721 		      sizeof(*scsi_cmd),
7722 		      timeout);
7723 
7724 	scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
7725 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7726 	scsi_cmd->opcode = INQUIRY;
7727 	if (evpd) {
7728 		scsi_cmd->byte2 |= SI_EVPD;
7729 		scsi_cmd->page_code = page_code;
7730 	}
7731 	scsi_ulto2b(inq_len, scsi_cmd->length);
7732 }
7733 
7734 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)7735 scsi_mode_sense(struct ccb_scsiio *csio, uint32_t retries,
7736     void (*cbfcnp)(struct cam_periph *, union ccb *), uint8_t tag_action,
7737     int dbd, uint8_t pc, uint8_t page, uint8_t *param_buf, uint32_t param_len,
7738     uint8_t sense_len, uint32_t timeout)
7739 {
7740 
7741 	scsi_mode_sense_subpage(csio, retries, cbfcnp, tag_action, dbd,
7742 	    pc, page, 0, param_buf, param_len, 0, sense_len, timeout);
7743 }
7744 
7745 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)7746 scsi_mode_sense_len(struct ccb_scsiio *csio, uint32_t retries,
7747     void (*cbfcnp)(struct cam_periph *, union ccb *), uint8_t tag_action,
7748     int dbd, uint8_t pc, uint8_t page, uint8_t *param_buf, uint32_t param_len,
7749     int minimum_cmd_size, uint8_t sense_len, uint32_t timeout)
7750 {
7751 
7752 	scsi_mode_sense_subpage(csio, retries, cbfcnp, tag_action, dbd,
7753 	    pc, page, 0, param_buf, param_len, minimum_cmd_size,
7754 	    sense_len, timeout);
7755 }
7756 
7757 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)7758 scsi_mode_sense_subpage(struct ccb_scsiio *csio, uint32_t retries,
7759     void (*cbfcnp)(struct cam_periph *, union ccb *), uint8_t tag_action,
7760     int dbd, uint8_t pc, uint8_t page, uint8_t subpage, uint8_t *param_buf,
7761     uint32_t param_len, int minimum_cmd_size, uint8_t sense_len,
7762     uint32_t timeout)
7763 {
7764 	uint8_t cdb_len;
7765 
7766 	/*
7767 	 * Use the smallest possible command to perform the operation.
7768 	 */
7769 	if ((param_len < 256)
7770 	 && (minimum_cmd_size < 10)) {
7771 		/*
7772 		 * We can fit in a 6 byte cdb.
7773 		 */
7774 		struct scsi_mode_sense_6 *scsi_cmd;
7775 
7776 		scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
7777 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7778 		scsi_cmd->opcode = MODE_SENSE_6;
7779 		if (dbd != 0)
7780 			scsi_cmd->byte2 |= SMS_DBD;
7781 		scsi_cmd->page = pc | page;
7782 		scsi_cmd->subpage = subpage;
7783 		scsi_cmd->length = param_len;
7784 		cdb_len = sizeof(*scsi_cmd);
7785 	} else {
7786 		/*
7787 		 * Need a 10 byte cdb.
7788 		 */
7789 		struct scsi_mode_sense_10 *scsi_cmd;
7790 
7791 		scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
7792 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7793 		scsi_cmd->opcode = MODE_SENSE_10;
7794 		if (dbd != 0)
7795 			scsi_cmd->byte2 |= SMS_DBD;
7796 		scsi_cmd->page = pc | page;
7797 		scsi_cmd->subpage = subpage;
7798 		scsi_ulto2b(param_len, scsi_cmd->length);
7799 		cdb_len = sizeof(*scsi_cmd);
7800 	}
7801 	cam_fill_csio(csio,
7802 		      retries,
7803 		      cbfcnp,
7804 		      CAM_DIR_IN,
7805 		      tag_action,
7806 		      param_buf,
7807 		      param_len,
7808 		      sense_len,
7809 		      cdb_len,
7810 		      timeout);
7811 }
7812 
7813 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)7814 scsi_mode_select(struct ccb_scsiio *csio, uint32_t retries,
7815 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
7816 		 uint8_t tag_action, int scsi_page_fmt, int save_pages,
7817 		 uint8_t *param_buf, uint32_t param_len, uint8_t sense_len,
7818 		 uint32_t timeout)
7819 {
7820 	scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
7821 			     scsi_page_fmt, save_pages, param_buf,
7822 			     param_len, 0, sense_len, timeout);
7823 }
7824 
7825 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)7826 scsi_mode_select_len(struct ccb_scsiio *csio, uint32_t retries,
7827 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
7828 		     uint8_t tag_action, int scsi_page_fmt, int save_pages,
7829 		     uint8_t *param_buf, uint32_t param_len,
7830 		     int minimum_cmd_size, uint8_t sense_len,
7831 		     uint32_t timeout)
7832 {
7833 	uint8_t cdb_len;
7834 
7835 	/*
7836 	 * Use the smallest possible command to perform the operation.
7837 	 */
7838 	if ((param_len < 256)
7839 	 && (minimum_cmd_size < 10)) {
7840 		/*
7841 		 * We can fit in a 6 byte cdb.
7842 		 */
7843 		struct scsi_mode_select_6 *scsi_cmd;
7844 
7845 		scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
7846 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7847 		scsi_cmd->opcode = MODE_SELECT_6;
7848 		if (scsi_page_fmt != 0)
7849 			scsi_cmd->byte2 |= SMS_PF;
7850 		if (save_pages != 0)
7851 			scsi_cmd->byte2 |= SMS_SP;
7852 		scsi_cmd->length = param_len;
7853 		cdb_len = sizeof(*scsi_cmd);
7854 	} else {
7855 		/*
7856 		 * Need a 10 byte cdb.
7857 		 */
7858 		struct scsi_mode_select_10 *scsi_cmd;
7859 
7860 		scsi_cmd =
7861 		    (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
7862 		bzero(scsi_cmd, sizeof(*scsi_cmd));
7863 		scsi_cmd->opcode = MODE_SELECT_10;
7864 		if (scsi_page_fmt != 0)
7865 			scsi_cmd->byte2 |= SMS_PF;
7866 		if (save_pages != 0)
7867 			scsi_cmd->byte2 |= SMS_SP;
7868 		scsi_ulto2b(param_len, scsi_cmd->length);
7869 		cdb_len = sizeof(*scsi_cmd);
7870 	}
7871 	cam_fill_csio(csio,
7872 		      retries,
7873 		      cbfcnp,
7874 		      CAM_DIR_OUT,
7875 		      tag_action,
7876 		      param_buf,
7877 		      param_len,
7878 		      sense_len,
7879 		      cdb_len,
7880 		      timeout);
7881 }
7882 
7883 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)7884 scsi_log_sense(struct ccb_scsiio *csio, uint32_t retries,
7885 	       void (*cbfcnp)(struct cam_periph *, union ccb *),
7886 	       uint8_t tag_action, uint8_t page_code, uint8_t page,
7887 	       int save_pages, int ppc, uint32_t paramptr,
7888 	       uint8_t *param_buf, uint32_t param_len, uint8_t sense_len,
7889 	       uint32_t timeout)
7890 {
7891 	struct scsi_log_sense *scsi_cmd;
7892 	uint8_t cdb_len;
7893 
7894 	scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
7895 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7896 	scsi_cmd->opcode = LOG_SENSE;
7897 	scsi_cmd->page = page_code | page;
7898 	if (save_pages != 0)
7899 		scsi_cmd->byte2 |= SLS_SP;
7900 	if (ppc != 0)
7901 		scsi_cmd->byte2 |= SLS_PPC;
7902 	scsi_ulto2b(paramptr, scsi_cmd->paramptr);
7903 	scsi_ulto2b(param_len, scsi_cmd->length);
7904 	cdb_len = sizeof(*scsi_cmd);
7905 
7906 	cam_fill_csio(csio,
7907 		      retries,
7908 		      cbfcnp,
7909 		      /*flags*/CAM_DIR_IN,
7910 		      tag_action,
7911 		      /*data_ptr*/param_buf,
7912 		      /*dxfer_len*/param_len,
7913 		      sense_len,
7914 		      cdb_len,
7915 		      timeout);
7916 }
7917 
7918 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)7919 scsi_log_select(struct ccb_scsiio *csio, uint32_t retries,
7920 		void (*cbfcnp)(struct cam_periph *, union ccb *),
7921 		uint8_t tag_action, uint8_t page_code, int save_pages,
7922 		int pc_reset, uint8_t *param_buf, uint32_t param_len,
7923 		uint8_t sense_len, uint32_t timeout)
7924 {
7925 	struct scsi_log_select *scsi_cmd;
7926 	uint8_t cdb_len;
7927 
7928 	scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
7929 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7930 	scsi_cmd->opcode = LOG_SELECT;
7931 	scsi_cmd->page = page_code & SLS_PAGE_CODE;
7932 	if (save_pages != 0)
7933 		scsi_cmd->byte2 |= SLS_SP;
7934 	if (pc_reset != 0)
7935 		scsi_cmd->byte2 |= SLS_PCR;
7936 	scsi_ulto2b(param_len, scsi_cmd->length);
7937 	cdb_len = sizeof(*scsi_cmd);
7938 
7939 	cam_fill_csio(csio,
7940 		      retries,
7941 		      cbfcnp,
7942 		      /*flags*/CAM_DIR_OUT,
7943 		      tag_action,
7944 		      /*data_ptr*/param_buf,
7945 		      /*dxfer_len*/param_len,
7946 		      sense_len,
7947 		      cdb_len,
7948 		      timeout);
7949 }
7950 
7951 /*
7952  * Prevent or allow the user to remove the media
7953  */
7954 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)7955 scsi_prevent(struct ccb_scsiio *csio, uint32_t retries,
7956 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
7957 	     uint8_t tag_action, uint8_t action,
7958 	     uint8_t sense_len, uint32_t timeout)
7959 {
7960 	struct scsi_prevent *scsi_cmd;
7961 
7962 	cam_fill_csio(csio,
7963 		      retries,
7964 		      cbfcnp,
7965 		      /*flags*/CAM_DIR_NONE,
7966 		      tag_action,
7967 		      /*data_ptr*/NULL,
7968 		      /*dxfer_len*/0,
7969 		      sense_len,
7970 		      sizeof(*scsi_cmd),
7971 		      timeout);
7972 
7973 	scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
7974 	bzero(scsi_cmd, sizeof(*scsi_cmd));
7975 	scsi_cmd->opcode = PREVENT_ALLOW;
7976 	scsi_cmd->how = action;
7977 }
7978 
7979 /* XXX allow specification of address and PMI bit and LBA */
7980 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)7981 scsi_read_capacity(struct ccb_scsiio *csio, uint32_t retries,
7982 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
7983 		   uint8_t tag_action,
7984 		   struct scsi_read_capacity_data *rcap_buf,
7985 		   uint8_t sense_len, uint32_t timeout)
7986 {
7987 	struct scsi_read_capacity *scsi_cmd;
7988 
7989 	cam_fill_csio(csio,
7990 		      retries,
7991 		      cbfcnp,
7992 		      /*flags*/CAM_DIR_IN,
7993 		      tag_action,
7994 		      /*data_ptr*/(uint8_t *)rcap_buf,
7995 		      /*dxfer_len*/sizeof(*rcap_buf),
7996 		      sense_len,
7997 		      sizeof(*scsi_cmd),
7998 		      timeout);
7999 
8000 	scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
8001 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8002 	scsi_cmd->opcode = READ_CAPACITY;
8003 }
8004 
8005 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)8006 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
8007 		      void (*cbfcnp)(struct cam_periph *, union ccb *),
8008 		      uint8_t tag_action, uint64_t lba, int reladr, int pmi,
8009 		      uint8_t *rcap_buf, int rcap_buf_len, uint8_t sense_len,
8010 		      uint32_t timeout)
8011 {
8012 	struct scsi_read_capacity_16 *scsi_cmd;
8013 
8014 	cam_fill_csio(csio,
8015 		      retries,
8016 		      cbfcnp,
8017 		      /*flags*/CAM_DIR_IN,
8018 		      tag_action,
8019 		      /*data_ptr*/(uint8_t *)rcap_buf,
8020 		      /*dxfer_len*/rcap_buf_len,
8021 		      sense_len,
8022 		      sizeof(*scsi_cmd),
8023 		      timeout);
8024 	scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
8025 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8026 	scsi_cmd->opcode = SERVICE_ACTION_IN;
8027 	scsi_cmd->service_action = SRC16_SERVICE_ACTION;
8028 	scsi_u64to8b(lba, scsi_cmd->addr);
8029 	scsi_ulto4b(rcap_buf_len, scsi_cmd->alloc_len);
8030 	if (pmi)
8031 		scsi_cmd->reladr |= SRC16_PMI;
8032 	if (reladr)
8033 		scsi_cmd->reladr |= SRC16_RELADR;
8034 }
8035 
8036 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)8037 scsi_report_luns(struct ccb_scsiio *csio, uint32_t retries,
8038 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8039 		 uint8_t tag_action, uint8_t select_report,
8040 		 struct scsi_report_luns_data *rpl_buf, uint32_t alloc_len,
8041 		 uint8_t sense_len, uint32_t timeout)
8042 {
8043 	struct scsi_report_luns *scsi_cmd;
8044 
8045 	cam_fill_csio(csio,
8046 		      retries,
8047 		      cbfcnp,
8048 		      /*flags*/CAM_DIR_IN,
8049 		      tag_action,
8050 		      /*data_ptr*/(uint8_t *)rpl_buf,
8051 		      /*dxfer_len*/alloc_len,
8052 		      sense_len,
8053 		      sizeof(*scsi_cmd),
8054 		      timeout);
8055 	scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
8056 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8057 	scsi_cmd->opcode = REPORT_LUNS;
8058 	scsi_cmd->select_report = select_report;
8059 	scsi_ulto4b(alloc_len, scsi_cmd->length);
8060 }
8061 
8062 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)8063 scsi_report_target_group(struct ccb_scsiio *csio, uint32_t retries,
8064 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8065 		 uint8_t tag_action, uint8_t pdf,
8066 		 void *buf, uint32_t alloc_len,
8067 		 uint8_t sense_len, uint32_t timeout)
8068 {
8069 	struct scsi_target_group *scsi_cmd;
8070 
8071 	cam_fill_csio(csio,
8072 		      retries,
8073 		      cbfcnp,
8074 		      /*flags*/CAM_DIR_IN,
8075 		      tag_action,
8076 		      /*data_ptr*/(uint8_t *)buf,
8077 		      /*dxfer_len*/alloc_len,
8078 		      sense_len,
8079 		      sizeof(*scsi_cmd),
8080 		      timeout);
8081 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
8082 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8083 	scsi_cmd->opcode = MAINTENANCE_IN;
8084 	scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
8085 	scsi_ulto4b(alloc_len, scsi_cmd->length);
8086 }
8087 
8088 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)8089 scsi_report_timestamp(struct ccb_scsiio *csio, uint32_t retries,
8090 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8091 		 uint8_t tag_action, uint8_t pdf,
8092 		 void *buf, uint32_t alloc_len,
8093 		 uint8_t sense_len, uint32_t timeout)
8094 {
8095 	struct scsi_timestamp *scsi_cmd;
8096 
8097 	cam_fill_csio(csio,
8098 		      retries,
8099 		      cbfcnp,
8100 		      /*flags*/CAM_DIR_IN,
8101 		      tag_action,
8102 		      /*data_ptr*/(uint8_t *)buf,
8103 		      /*dxfer_len*/alloc_len,
8104 		      sense_len,
8105 		      sizeof(*scsi_cmd),
8106 		      timeout);
8107 	scsi_cmd = (struct scsi_timestamp *)&csio->cdb_io.cdb_bytes;
8108 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8109 	scsi_cmd->opcode = MAINTENANCE_IN;
8110 	scsi_cmd->service_action = REPORT_TIMESTAMP | pdf;
8111 	scsi_ulto4b(alloc_len, scsi_cmd->length);
8112 }
8113 
8114 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)8115 scsi_set_target_group(struct ccb_scsiio *csio, uint32_t retries,
8116 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8117 		 uint8_t tag_action, void *buf, uint32_t alloc_len,
8118 		 uint8_t sense_len, uint32_t timeout)
8119 {
8120 	struct scsi_target_group *scsi_cmd;
8121 
8122 	cam_fill_csio(csio,
8123 		      retries,
8124 		      cbfcnp,
8125 		      /*flags*/CAM_DIR_OUT,
8126 		      tag_action,
8127 		      /*data_ptr*/(uint8_t *)buf,
8128 		      /*dxfer_len*/alloc_len,
8129 		      sense_len,
8130 		      sizeof(*scsi_cmd),
8131 		      timeout);
8132 	scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
8133 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8134 	scsi_cmd->opcode = MAINTENANCE_OUT;
8135 	scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
8136 	scsi_ulto4b(alloc_len, scsi_cmd->length);
8137 }
8138 
8139 void
scsi_create_timestamp(uint8_t * timestamp_6b_buf,uint64_t timestamp)8140 scsi_create_timestamp(uint8_t *timestamp_6b_buf,
8141 		      uint64_t timestamp)
8142 {
8143 	uint8_t buf[8];
8144 	scsi_u64to8b(timestamp, buf);
8145 	/*
8146 	 * Using memcopy starting at buf[2] because the set timestamp parameters
8147 	 * only has six bytes for the timestamp to fit into, and we don't have a
8148 	 * scsi_u64to6b function.
8149 	 */
8150 	memcpy(timestamp_6b_buf, &buf[2], 6);
8151 }
8152 
8153 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)8154 scsi_set_timestamp(struct ccb_scsiio *csio, uint32_t retries,
8155 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
8156 		   uint8_t tag_action, void *buf, uint32_t alloc_len,
8157 		   uint8_t sense_len, uint32_t timeout)
8158 {
8159 	struct scsi_timestamp *scsi_cmd;
8160 
8161 	cam_fill_csio(csio,
8162 		      retries,
8163 		      cbfcnp,
8164 		      /*flags*/CAM_DIR_OUT,
8165 		      tag_action,
8166 		      /*data_ptr*/(uint8_t *) buf,
8167 		      /*dxfer_len*/alloc_len,
8168 		      sense_len,
8169 		      sizeof(*scsi_cmd),
8170 		      timeout);
8171 	scsi_cmd = (struct scsi_timestamp *)&csio->cdb_io.cdb_bytes;
8172 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8173 	scsi_cmd->opcode = MAINTENANCE_OUT;
8174 	scsi_cmd->service_action = SET_TIMESTAMP;
8175 	scsi_ulto4b(alloc_len, scsi_cmd->length);
8176 }
8177 
8178 /*
8179  * Syncronize the media to the contents of the cache for
8180  * the given lba/count pair.  Specifying 0/0 means sync
8181  * the whole cache.
8182  */
8183 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)8184 scsi_synchronize_cache(struct ccb_scsiio *csio, uint32_t retries,
8185 		       void (*cbfcnp)(struct cam_periph *, union ccb *),
8186 		       uint8_t tag_action, uint32_t begin_lba,
8187 		       uint16_t lb_count, uint8_t sense_len,
8188 		       uint32_t timeout)
8189 {
8190 	struct scsi_sync_cache *scsi_cmd;
8191 
8192 	cam_fill_csio(csio,
8193 		      retries,
8194 		      cbfcnp,
8195 		      /*flags*/CAM_DIR_NONE,
8196 		      tag_action,
8197 		      /*data_ptr*/NULL,
8198 		      /*dxfer_len*/0,
8199 		      sense_len,
8200 		      sizeof(*scsi_cmd),
8201 		      timeout);
8202 
8203 	scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
8204 	bzero(scsi_cmd, sizeof(*scsi_cmd));
8205 	scsi_cmd->opcode = SYNCHRONIZE_CACHE;
8206 	scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
8207 	scsi_ulto2b(lb_count, scsi_cmd->lb_count);
8208 }
8209 
8210 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)8211 scsi_read_write(struct ccb_scsiio *csio, uint32_t retries,
8212 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8213 		uint8_t tag_action, int readop, uint8_t byte2,
8214 		int minimum_cmd_size, uint64_t lba, uint32_t block_count,
8215 		uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
8216 		uint32_t timeout)
8217 {
8218 	int read;
8219 	uint8_t cdb_len;
8220 
8221 	read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
8222 
8223 	/*
8224 	 * Use the smallest possible command to perform the operation
8225 	 * as some legacy hardware does not support the 10 byte commands.
8226 	 * If any of the bits in byte2 is set, we have to go with a larger
8227 	 * command.
8228 	 */
8229 	if ((minimum_cmd_size < 10)
8230 	 && ((lba & 0x1fffff) == lba)
8231 	 && ((block_count & 0xff) == block_count)
8232 	 && (byte2 == 0)) {
8233 		/*
8234 		 * We can fit in a 6 byte cdb.
8235 		 */
8236 		struct scsi_rw_6 *scsi_cmd;
8237 
8238 		scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
8239 		scsi_cmd->opcode = read ? READ_6 : WRITE_6;
8240 		scsi_ulto3b(lba, scsi_cmd->addr);
8241 		scsi_cmd->length = block_count & 0xff;
8242 		scsi_cmd->control = 0;
8243 		cdb_len = sizeof(*scsi_cmd);
8244 
8245 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8246 			  ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
8247 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8248 			   scsi_cmd->length, dxfer_len));
8249 	} else if ((minimum_cmd_size < 12)
8250 		&& ((block_count & 0xffff) == block_count)
8251 		&& ((lba & 0xffffffff) == lba)) {
8252 		/*
8253 		 * Need a 10 byte cdb.
8254 		 */
8255 		struct scsi_rw_10 *scsi_cmd;
8256 
8257 		scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
8258 		scsi_cmd->opcode = read ? READ_10 : WRITE_10;
8259 		scsi_cmd->byte2 = byte2;
8260 		scsi_ulto4b(lba, scsi_cmd->addr);
8261 		scsi_cmd->reserved = 0;
8262 		scsi_ulto2b(block_count, scsi_cmd->length);
8263 		scsi_cmd->control = 0;
8264 		cdb_len = sizeof(*scsi_cmd);
8265 
8266 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8267 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8268 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8269 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8270 			   scsi_cmd->length[1], dxfer_len));
8271 	} else if ((minimum_cmd_size < 16)
8272 		&& ((block_count & 0xffffffff) == block_count)
8273 		&& ((lba & 0xffffffff) == lba)) {
8274 		/*
8275 		 * The block count is too big for a 10 byte CDB, use a 12
8276 		 * byte CDB.
8277 		 */
8278 		struct scsi_rw_12 *scsi_cmd;
8279 
8280 		scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
8281 		scsi_cmd->opcode = read ? READ_12 : WRITE_12;
8282 		scsi_cmd->byte2 = byte2;
8283 		scsi_ulto4b(lba, scsi_cmd->addr);
8284 		scsi_cmd->reserved = 0;
8285 		scsi_ulto4b(block_count, scsi_cmd->length);
8286 		scsi_cmd->control = 0;
8287 		cdb_len = sizeof(*scsi_cmd);
8288 
8289 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8290 			  ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
8291 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8292 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8293 			   scsi_cmd->length[1], scsi_cmd->length[2],
8294 			   scsi_cmd->length[3], dxfer_len));
8295 	} else {
8296 		/*
8297 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8298 		 * than 2^32, or if the user asks for a 16 byte command.
8299 		 */
8300 		struct scsi_rw_16 *scsi_cmd;
8301 
8302 		scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
8303 		scsi_cmd->opcode = read ? READ_16 : WRITE_16;
8304 		scsi_cmd->byte2 = byte2;
8305 		scsi_u64to8b(lba, scsi_cmd->addr);
8306 		scsi_cmd->reserved = 0;
8307 		scsi_ulto4b(block_count, scsi_cmd->length);
8308 		scsi_cmd->control = 0;
8309 		cdb_len = sizeof(*scsi_cmd);
8310 	}
8311 	cam_fill_csio(csio,
8312 		      retries,
8313 		      cbfcnp,
8314 		      (read ? CAM_DIR_IN : CAM_DIR_OUT) |
8315 		      ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
8316 		      tag_action,
8317 		      data_ptr,
8318 		      dxfer_len,
8319 		      sense_len,
8320 		      cdb_len,
8321 		      timeout);
8322 }
8323 
8324 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)8325 scsi_write_same(struct ccb_scsiio *csio, uint32_t retries,
8326 		void (*cbfcnp)(struct cam_periph *, union ccb *),
8327 		uint8_t tag_action, uint8_t byte2,
8328 		int minimum_cmd_size, uint64_t lba, uint32_t block_count,
8329 		uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len,
8330 		uint32_t timeout)
8331 {
8332 	uint8_t cdb_len;
8333 	if ((minimum_cmd_size < 16) &&
8334 	    ((block_count & 0xffff) == block_count) &&
8335 	    ((lba & 0xffffffff) == lba)) {
8336 		/*
8337 		 * Need a 10 byte cdb.
8338 		 */
8339 		struct scsi_write_same_10 *scsi_cmd;
8340 
8341 		scsi_cmd = (struct scsi_write_same_10 *)&csio->cdb_io.cdb_bytes;
8342 		scsi_cmd->opcode = WRITE_SAME_10;
8343 		scsi_cmd->byte2 = byte2;
8344 		scsi_ulto4b(lba, scsi_cmd->addr);
8345 		scsi_cmd->group = 0;
8346 		scsi_ulto2b(block_count, scsi_cmd->length);
8347 		scsi_cmd->control = 0;
8348 		cdb_len = sizeof(*scsi_cmd);
8349 
8350 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8351 			  ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8352 			   scsi_cmd->addr[1], scsi_cmd->addr[2],
8353 			   scsi_cmd->addr[3], scsi_cmd->length[0],
8354 			   scsi_cmd->length[1], dxfer_len));
8355 	} else {
8356 		/*
8357 		 * 16 byte CDB.  We'll only get here if the LBA is larger
8358 		 * than 2^32, or if the user asks for a 16 byte command.
8359 		 */
8360 		struct scsi_write_same_16 *scsi_cmd;
8361 
8362 		scsi_cmd = (struct scsi_write_same_16 *)&csio->cdb_io.cdb_bytes;
8363 		scsi_cmd->opcode = WRITE_SAME_16;
8364 		scsi_cmd->byte2 = byte2;
8365 		scsi_u64to8b(lba, scsi_cmd->addr);
8366 		scsi_ulto4b(block_count, scsi_cmd->length);
8367 		scsi_cmd->group = 0;
8368 		scsi_cmd->control = 0;
8369 		cdb_len = sizeof(*scsi_cmd);
8370 
8371 		CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8372 			  ("16byte: %x%x%x%x%x%x%x%x:%x%x%x%x: %d\n",
8373 			   scsi_cmd->addr[0], scsi_cmd->addr[1],
8374 			   scsi_cmd->addr[2], scsi_cmd->addr[3],
8375 			   scsi_cmd->addr[4], scsi_cmd->addr[5],
8376 			   scsi_cmd->addr[6], scsi_cmd->addr[7],
8377 			   scsi_cmd->length[0], scsi_cmd->length[1],
8378 			   scsi_cmd->length[2], scsi_cmd->length[3],
8379 			   dxfer_len));
8380 	}
8381 	cam_fill_csio(csio,
8382 		      retries,
8383 		      cbfcnp,
8384 		      /*flags*/CAM_DIR_OUT,
8385 		      tag_action,
8386 		      data_ptr,
8387 		      dxfer_len,
8388 		      sense_len,
8389 		      cdb_len,
8390 		      timeout);
8391 }
8392 
8393 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)8394 scsi_ata_identify(struct ccb_scsiio *csio, uint32_t retries,
8395 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8396 		  uint8_t tag_action, uint8_t *data_ptr,
8397 		  uint16_t dxfer_len, uint8_t sense_len,
8398 		  uint32_t timeout)
8399 {
8400 	scsi_ata_pass(csio,
8401 		      retries,
8402 		      cbfcnp,
8403 		      /*flags*/CAM_DIR_IN,
8404 		      tag_action,
8405 		      /*protocol*/AP_PROTO_PIO_IN,
8406 		      /*ata_flags*/AP_FLAG_TDIR_FROM_DEV |
8407 				   AP_FLAG_BYT_BLOK_BLOCKS |
8408 				   AP_FLAG_TLEN_SECT_CNT,
8409 		      /*features*/0,
8410 		      /*sector_count*/dxfer_len / 512,
8411 		      /*lba*/0,
8412 		      /*command*/ATA_ATA_IDENTIFY,
8413 		      /*device*/ 0,
8414 		      /*icc*/ 0,
8415 		      /*auxiliary*/ 0,
8416 		      /*control*/0,
8417 		      data_ptr,
8418 		      dxfer_len,
8419 		      /*cdb_storage*/ NULL,
8420 		      /*cdb_storage_len*/ 0,
8421 		      /*minimum_cmd_size*/ 0,
8422 		      sense_len,
8423 		      timeout);
8424 }
8425 
8426 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)8427 scsi_ata_trim(struct ccb_scsiio *csio, uint32_t retries,
8428 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8429 	      uint8_t tag_action, uint16_t block_count,
8430 	      uint8_t *data_ptr, uint16_t dxfer_len, uint8_t sense_len,
8431 	      uint32_t timeout)
8432 {
8433 	scsi_ata_pass_16(csio,
8434 			 retries,
8435 			 cbfcnp,
8436 			 /*flags*/CAM_DIR_OUT,
8437 			 tag_action,
8438 			 /*protocol*/AP_EXTEND|AP_PROTO_DMA,
8439 			 /*ata_flags*/AP_FLAG_TLEN_SECT_CNT|AP_FLAG_BYT_BLOK_BLOCKS,
8440 			 /*features*/ATA_DSM_TRIM,
8441 			 /*sector_count*/block_count,
8442 			 /*lba*/0,
8443 			 /*command*/ATA_DATA_SET_MANAGEMENT,
8444 			 /*control*/0,
8445 			 data_ptr,
8446 			 dxfer_len,
8447 			 sense_len,
8448 			 timeout);
8449 }
8450 
8451 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)8452 scsi_ata_read_log(struct ccb_scsiio *csio, uint32_t retries,
8453 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
8454 		  uint8_t tag_action, uint32_t log_address,
8455 		  uint32_t page_number, uint16_t block_count,
8456 		  uint8_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
8457 		  uint8_t sense_len, uint32_t timeout)
8458 {
8459 	uint8_t command, protocol_out;
8460 	uint16_t count_out;
8461 	uint64_t lba;
8462 	int retval;
8463 
8464 	retval = 0;
8465 
8466 	switch (protocol) {
8467 	case AP_PROTO_DMA:
8468 		count_out = block_count;
8469 		command = ATA_READ_LOG_DMA_EXT;
8470 		protocol_out = AP_PROTO_DMA;
8471 		break;
8472 	case AP_PROTO_PIO_IN:
8473 	default:
8474 		count_out = block_count;
8475 		command = ATA_READ_LOG_EXT;
8476 		protocol_out = AP_PROTO_PIO_IN;
8477 		break;
8478 	}
8479 
8480 	lba = (((uint64_t)page_number & 0xff00) << 32) |
8481 	      ((page_number & 0x00ff) << 8) |
8482 	      (log_address & 0xff);
8483 
8484 	protocol_out |= AP_EXTEND;
8485 
8486 	retval = scsi_ata_pass(csio,
8487 			       retries,
8488 			       cbfcnp,
8489 			       /*flags*/CAM_DIR_IN,
8490 			       tag_action,
8491 			       /*protocol*/ protocol_out,
8492 			       /*ata_flags*/AP_FLAG_TLEN_SECT_CNT |
8493 					    AP_FLAG_BYT_BLOK_BLOCKS |
8494 					    AP_FLAG_TDIR_FROM_DEV,
8495 			       /*feature*/ 0,
8496 			       /*sector_count*/ count_out,
8497 			       /*lba*/ lba,
8498 			       /*command*/ command,
8499 			       /*device*/ 0,
8500 			       /*icc*/ 0,
8501 			       /*auxiliary*/ 0,
8502 			       /*control*/0,
8503 			       data_ptr,
8504 			       dxfer_len,
8505 			       /*cdb_storage*/ NULL,
8506 			       /*cdb_storage_len*/ 0,
8507 			       /*minimum_cmd_size*/ 0,
8508 			       sense_len,
8509 			       timeout);
8510 
8511 	return (retval);
8512 }
8513 
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)8514 int scsi_ata_setfeatures(struct ccb_scsiio *csio, uint32_t retries,
8515 			 void (*cbfcnp)(struct cam_periph *, union ccb *),
8516 			 uint8_t tag_action, uint8_t feature,
8517 			 uint64_t lba, uint32_t count,
8518 			 uint8_t sense_len, uint32_t timeout)
8519 {
8520 	return (scsi_ata_pass(csio,
8521 		retries,
8522 		cbfcnp,
8523 		/*flags*/CAM_DIR_NONE,
8524 		tag_action,
8525 		/*protocol*/AP_PROTO_PIO_IN,
8526 		/*ata_flags*/AP_FLAG_TDIR_FROM_DEV |
8527 			     AP_FLAG_BYT_BLOK_BYTES |
8528 			     AP_FLAG_TLEN_SECT_CNT,
8529 		/*features*/feature,
8530 		/*sector_count*/count,
8531 		/*lba*/lba,
8532 		/*command*/ATA_SETFEATURES,
8533 		/*device*/ 0,
8534 		/*icc*/ 0,
8535 		/*auxiliary*/0,
8536 		/*control*/0,
8537 		/*data_ptr*/NULL,
8538 		/*dxfer_len*/0,
8539 		/*cdb_storage*/NULL,
8540 		/*cdb_storage_len*/0,
8541 		/*minimum_cmd_size*/0,
8542 		sense_len,
8543 		timeout));
8544 }
8545 
8546 /*
8547  * Note! This is an unusual CDB building function because it can return
8548  * an error in the event that the command in question requires a variable
8549  * length CDB, but the caller has not given storage space for one or has not
8550  * given enough storage space.  If there is enough space available in the
8551  * standard SCSI CCB CDB bytes, we'll prefer that over passed in storage.
8552  */
8553 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)8554 scsi_ata_pass(struct ccb_scsiio *csio, uint32_t retries,
8555 	      void (*cbfcnp)(struct cam_periph *, union ccb *),
8556 	      uint32_t flags, uint8_t tag_action,
8557 	      uint8_t protocol, uint8_t ata_flags, uint16_t features,
8558 	      uint16_t sector_count, uint64_t lba, uint8_t command,
8559 	      uint8_t device, uint8_t icc, uint32_t auxiliary,
8560 	      uint8_t control, uint8_t *data_ptr, uint32_t dxfer_len,
8561 	      uint8_t *cdb_storage, size_t cdb_storage_len,
8562 	      int minimum_cmd_size, uint8_t sense_len, uint32_t timeout)
8563 {
8564 	uint32_t cam_flags;
8565 	uint8_t *cdb_ptr;
8566 	int cmd_size;
8567 	int retval;
8568 	uint8_t cdb_len;
8569 
8570 	retval = 0;
8571 	cam_flags = flags;
8572 
8573 	/*
8574 	 * Round the user's request to the nearest command size that is at
8575 	 * least as big as what he requested.
8576 	 */
8577 	if (minimum_cmd_size <= 12)
8578 		cmd_size = 12;
8579 	else if (minimum_cmd_size > 16)
8580 		cmd_size = 32;
8581 	else
8582 		cmd_size = 16;
8583 
8584 	/*
8585 	 * If we have parameters that require a 48-bit ATA command, we have to
8586 	 * use the 16 byte ATA PASS-THROUGH command at least.
8587 	 */
8588 	if (((lba > ATA_MAX_28BIT_LBA)
8589 	  || (sector_count > 255)
8590 	  || (features > 255)
8591 	  || (protocol & AP_EXTEND))
8592 	 && ((cmd_size < 16)
8593 	  || ((protocol & AP_EXTEND) == 0))) {
8594 		if (cmd_size < 16)
8595 			cmd_size = 16;
8596 		protocol |= AP_EXTEND;
8597 	}
8598 
8599 	/*
8600 	 * The icc and auxiliary ATA registers are only supported in the
8601 	 * 32-byte version of the ATA PASS-THROUGH command.
8602 	 */
8603 	if ((icc != 0)
8604 	 || (auxiliary != 0)) {
8605 		cmd_size = 32;
8606 		protocol |= AP_EXTEND;
8607 	}
8608 
8609 	if ((cmd_size > sizeof(csio->cdb_io.cdb_bytes))
8610 	 && ((cdb_storage == NULL)
8611 	  || (cdb_storage_len < cmd_size))) {
8612 		retval = 1;
8613 		goto bailout;
8614 	}
8615 
8616 	/*
8617 	 * At this point we know we have enough space to store the command
8618 	 * in one place or another.  We prefer the built-in array, but used
8619 	 * the passed in storage if necessary.
8620 	 */
8621 	if (cmd_size <= sizeof(csio->cdb_io.cdb_bytes))
8622 		cdb_ptr = csio->cdb_io.cdb_bytes;
8623 	else {
8624 		cdb_ptr = cdb_storage;
8625 		cam_flags |= CAM_CDB_POINTER;
8626 	}
8627 
8628 	if (cmd_size <= 12) {
8629 		struct ata_pass_12 *cdb;
8630 
8631 		cdb = (struct ata_pass_12 *)cdb_ptr;
8632 		cdb_len = sizeof(*cdb);
8633 		bzero(cdb, cdb_len);
8634 
8635 		cdb->opcode = ATA_PASS_12;
8636 		cdb->protocol = protocol;
8637 		cdb->flags = ata_flags;
8638 		cdb->features = features;
8639 		cdb->sector_count = sector_count;
8640 		cdb->lba_low = lba & 0xff;
8641 		cdb->lba_mid = (lba >> 8) & 0xff;
8642 		cdb->lba_high = (lba >> 16) & 0xff;
8643 		cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8644 		cdb->command = command;
8645 		cdb->control = control;
8646 	} else if (cmd_size <= 16) {
8647 		struct ata_pass_16 *cdb;
8648 
8649 		cdb = (struct ata_pass_16 *)cdb_ptr;
8650 		cdb_len = sizeof(*cdb);
8651 		bzero(cdb, cdb_len);
8652 
8653 		cdb->opcode = ATA_PASS_16;
8654 		cdb->protocol = protocol;
8655 		cdb->flags = ata_flags;
8656 		cdb->features = features & 0xff;
8657 		cdb->sector_count = sector_count & 0xff;
8658 		cdb->lba_low = lba & 0xff;
8659 		cdb->lba_mid = (lba >> 8) & 0xff;
8660 		cdb->lba_high = (lba >> 16) & 0xff;
8661 		/*
8662 		 * If AP_EXTEND is set, we're sending a 48-bit command.
8663 		 * Otherwise it's a 28-bit command.
8664 		 */
8665 		if (protocol & AP_EXTEND) {
8666 			cdb->lba_low_ext = (lba >> 24) & 0xff;
8667 			cdb->lba_mid_ext = (lba >> 32) & 0xff;
8668 			cdb->lba_high_ext = (lba >> 40) & 0xff;
8669 			cdb->features_ext = (features >> 8) & 0xff;
8670 			cdb->sector_count_ext = (sector_count >> 8) & 0xff;
8671 			cdb->device = device | ATA_DEV_LBA;
8672 		} else {
8673 			cdb->lba_low_ext = (lba >> 24) & 0xf;
8674 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8675 		}
8676 		cdb->command = command;
8677 		cdb->control = control;
8678 	} else {
8679 		struct ata_pass_32 *cdb;
8680 		uint8_t tmp_lba[8];
8681 
8682 		cdb = (struct ata_pass_32 *)cdb_ptr;
8683 		cdb_len = sizeof(*cdb);
8684 		bzero(cdb, cdb_len);
8685 		cdb->opcode = VARIABLE_LEN_CDB;
8686 		cdb->control = control;
8687 		cdb->length = sizeof(*cdb) - __offsetof(struct ata_pass_32,
8688 							service_action);
8689 		scsi_ulto2b(ATA_PASS_32_SA, cdb->service_action);
8690 		cdb->protocol = protocol;
8691 		cdb->flags = ata_flags;
8692 
8693 		if ((protocol & AP_EXTEND) == 0) {
8694 			lba &= 0x0fffffff;
8695 			cdb->device = ((lba >> 24) & 0xf) | ATA_DEV_LBA;
8696 			features &= 0xff;
8697 			sector_count &= 0xff;
8698 		} else {
8699 			cdb->device = device | ATA_DEV_LBA;
8700 		}
8701 		scsi_u64to8b(lba, tmp_lba);
8702 		bcopy(&tmp_lba[2], cdb->lba, sizeof(cdb->lba));
8703 		scsi_ulto2b(features, cdb->features);
8704 		scsi_ulto2b(sector_count, cdb->count);
8705 		cdb->command = command;
8706 		cdb->icc = icc;
8707 		scsi_ulto4b(auxiliary, cdb->auxiliary);
8708 	}
8709 
8710 	cam_fill_csio(csio,
8711 		      retries,
8712 		      cbfcnp,
8713 		      cam_flags,
8714 		      tag_action,
8715 		      data_ptr,
8716 		      dxfer_len,
8717 		      sense_len,
8718 		      cmd_size,
8719 		      timeout);
8720 bailout:
8721 	return (retval);
8722 }
8723 
8724 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)8725 scsi_ata_pass_16(struct ccb_scsiio *csio, uint32_t retries,
8726 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
8727 		 uint32_t flags, uint8_t tag_action,
8728 		 uint8_t protocol, uint8_t ata_flags, uint16_t features,
8729 		 uint16_t sector_count, uint64_t lba, uint8_t command,
8730 		 uint8_t control, uint8_t *data_ptr, uint16_t dxfer_len,
8731 		 uint8_t sense_len, uint32_t timeout)
8732 {
8733 	struct ata_pass_16 *ata_cmd;
8734 
8735 	ata_cmd = (struct ata_pass_16 *)&csio->cdb_io.cdb_bytes;
8736 	ata_cmd->opcode = ATA_PASS_16;
8737 	ata_cmd->protocol = protocol;
8738 	ata_cmd->flags = ata_flags;
8739 	ata_cmd->features_ext = features >> 8;
8740 	ata_cmd->features = features;
8741 	ata_cmd->sector_count_ext = sector_count >> 8;
8742 	ata_cmd->sector_count = sector_count;
8743 	ata_cmd->lba_low = lba;
8744 	ata_cmd->lba_mid = lba >> 8;
8745 	ata_cmd->lba_high = lba >> 16;
8746 	ata_cmd->device = ATA_DEV_LBA;
8747 	if (protocol & AP_EXTEND) {
8748 		ata_cmd->lba_low_ext = lba >> 24;
8749 		ata_cmd->lba_mid_ext = lba >> 32;
8750 		ata_cmd->lba_high_ext = lba >> 40;
8751 	} else
8752 		ata_cmd->device |= (lba >> 24) & 0x0f;
8753 	ata_cmd->command = command;
8754 	ata_cmd->control = control;
8755 
8756 	cam_fill_csio(csio,
8757 		      retries,
8758 		      cbfcnp,
8759 		      flags,
8760 		      tag_action,
8761 		      data_ptr,
8762 		      dxfer_len,
8763 		      sense_len,
8764 		      sizeof(*ata_cmd),
8765 		      timeout);
8766 }
8767 
8768 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)8769 scsi_unmap(struct ccb_scsiio *csio, uint32_t retries,
8770 	   void (*cbfcnp)(struct cam_periph *, union ccb *),
8771 	   uint8_t tag_action, uint8_t byte2,
8772 	   uint8_t *data_ptr, uint16_t dxfer_len, uint8_t sense_len,
8773 	   uint32_t timeout)
8774 {
8775 	struct scsi_unmap *scsi_cmd;
8776 
8777 	scsi_cmd = (struct scsi_unmap *)&csio->cdb_io.cdb_bytes;
8778 	scsi_cmd->opcode = UNMAP;
8779 	scsi_cmd->byte2 = byte2;
8780 	scsi_ulto4b(0, scsi_cmd->reserved);
8781 	scsi_cmd->group = 0;
8782 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
8783 	scsi_cmd->control = 0;
8784 
8785 	cam_fill_csio(csio,
8786 		      retries,
8787 		      cbfcnp,
8788 		      /*flags*/CAM_DIR_OUT,
8789 		      tag_action,
8790 		      data_ptr,
8791 		      dxfer_len,
8792 		      sense_len,
8793 		      sizeof(*scsi_cmd),
8794 		      timeout);
8795 }
8796 
8797 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)8798 scsi_receive_diagnostic_results(struct ccb_scsiio *csio, uint32_t retries,
8799 				void (*cbfcnp)(struct cam_periph *, union ccb*),
8800 				uint8_t tag_action, int pcv, uint8_t page_code,
8801 				uint8_t *data_ptr, uint16_t allocation_length,
8802 				uint8_t sense_len, uint32_t timeout)
8803 {
8804 	struct scsi_receive_diag *scsi_cmd;
8805 
8806 	scsi_cmd = (struct scsi_receive_diag *)&csio->cdb_io.cdb_bytes;
8807 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8808 	scsi_cmd->opcode = RECEIVE_DIAGNOSTIC;
8809 	if (pcv) {
8810 		scsi_cmd->byte2 |= SRD_PCV;
8811 		scsi_cmd->page_code = page_code;
8812 	}
8813 	scsi_ulto2b(allocation_length, scsi_cmd->length);
8814 
8815 	cam_fill_csio(csio,
8816 		      retries,
8817 		      cbfcnp,
8818 		      /*flags*/CAM_DIR_IN,
8819 		      tag_action,
8820 		      data_ptr,
8821 		      allocation_length,
8822 		      sense_len,
8823 		      sizeof(*scsi_cmd),
8824 		      timeout);
8825 }
8826 
8827 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)8828 scsi_send_diagnostic(struct ccb_scsiio *csio, uint32_t retries,
8829 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
8830 		     uint8_t tag_action, int unit_offline, int device_offline,
8831 		     int self_test, int page_format, int self_test_code,
8832 		     uint8_t *data_ptr, uint16_t param_list_length,
8833 		     uint8_t sense_len, uint32_t timeout)
8834 {
8835 	struct scsi_send_diag *scsi_cmd;
8836 
8837 	scsi_cmd = (struct scsi_send_diag *)&csio->cdb_io.cdb_bytes;
8838 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8839 	scsi_cmd->opcode = SEND_DIAGNOSTIC;
8840 
8841 	/*
8842 	 * The default self-test mode control and specific test
8843 	 * control are mutually exclusive.
8844 	 */
8845 	if (self_test)
8846 		self_test_code = SSD_SELF_TEST_CODE_NONE;
8847 
8848 	scsi_cmd->byte2 = ((self_test_code << SSD_SELF_TEST_CODE_SHIFT)
8849 			 & SSD_SELF_TEST_CODE_MASK)
8850 			| (unit_offline   ? SSD_UNITOFFL : 0)
8851 			| (device_offline ? SSD_DEVOFFL  : 0)
8852 			| (self_test      ? SSD_SELFTEST : 0)
8853 			| (page_format    ? SSD_PF       : 0);
8854 	scsi_ulto2b(param_list_length, scsi_cmd->length);
8855 
8856 	cam_fill_csio(csio,
8857 		      retries,
8858 		      cbfcnp,
8859 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8860 		      tag_action,
8861 		      data_ptr,
8862 		      param_list_length,
8863 		      sense_len,
8864 		      sizeof(*scsi_cmd),
8865 		      timeout);
8866 }
8867 
8868 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)8869 scsi_get_physical_element_status(struct ccb_scsiio *csio, uint32_t retries,
8870 				 void (*cbfcnp)(struct cam_periph *, union ccb *),
8871 				 uint8_t tag_action, uint8_t *data_ptr,
8872 				 uint16_t allocation_length, uint8_t report_type,
8873 				 uint32_t starting_element,
8874 				 uint8_t sense_len, uint32_t timeout)
8875 {
8876 	struct scsi_get_physical_element_status *scsi_cmd;
8877 
8878 	scsi_cmd = (struct scsi_get_physical_element_status *)&csio->cdb_io.cdb_bytes;
8879 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8880 	scsi_cmd->opcode = SERVICE_ACTION_IN;
8881 	scsi_cmd->service_action = GET_PHYSICAL_ELEMENT_STATUS;
8882 	scsi_ulto4b(starting_element, scsi_cmd->starting_element);
8883 	scsi_ulto4b(allocation_length, scsi_cmd->allocation_length);
8884 
8885 	cam_fill_csio(csio,
8886 		      retries,
8887 		      cbfcnp,
8888 		      /*flags*/ CAM_DIR_IN,
8889 		      tag_action,
8890 		      data_ptr,
8891 		      allocation_length,
8892 		      sense_len,
8893 		      sizeof(*scsi_cmd),
8894 		      timeout);
8895 }
8896 
8897 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)8898 scsi_remove_element_and_truncate(struct ccb_scsiio *csio, uint32_t retries,
8899 				 void (*cbfcnp)(struct cam_periph *, union ccb *),
8900 				 uint8_t tag_action,
8901 				 uint64_t requested_capacity, uint32_t element_id,
8902 				 uint8_t sense_len, uint32_t timeout)
8903 {
8904 	struct scsi_remove_element_and_truncate *scsi_cmd;
8905 
8906 	scsi_cmd = (struct scsi_remove_element_and_truncate *)&csio->cdb_io.cdb_bytes;
8907 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8908 	scsi_cmd->opcode = SERVICE_ACTION_IN;
8909 	scsi_cmd->service_action = REMOVE_ELEMENT_AND_TRUNCATE;
8910 	scsi_u64to8b(requested_capacity, scsi_cmd->requested_capacity);
8911 	scsi_ulto4b(element_id, scsi_cmd->element_identifier);
8912 
8913 	cam_fill_csio(csio,
8914 		      retries,
8915 		      cbfcnp,
8916 		      /*flags*/ CAM_DIR_OUT,
8917 		      tag_action,
8918 		      NULL,
8919 		      0,
8920 		      sense_len,
8921 		      sizeof(*scsi_cmd),
8922 		      timeout);
8923 }
8924 
8925 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)8926 scsi_restore_elements_and_rebuild(struct ccb_scsiio *csio, uint32_t retries,
8927 				  void (*cbfcnp)(struct cam_periph *, union ccb *),
8928 				  uint8_t tag_action,
8929 				  uint8_t sense_len, uint32_t timeout)
8930 {
8931 	struct scsi_service_action_in *scsi_cmd;
8932 
8933 	scsi_cmd = (struct scsi_service_action_in *)&csio->cdb_io.cdb_bytes;
8934 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8935 	scsi_cmd->opcode = SERVICE_ACTION_IN;
8936 	scsi_cmd->service_action = RESTORE_ELEMENTS_AND_REBUILD;
8937 
8938 	cam_fill_csio(csio,
8939 		      retries,
8940 		      cbfcnp,
8941 		      /*flags*/ CAM_DIR_OUT,
8942 		      tag_action,
8943 		      NULL,
8944 		      0,
8945 		      sense_len,
8946 		      sizeof(*scsi_cmd),
8947 		      timeout);
8948 }
8949 
8950 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)8951 scsi_read_buffer(struct ccb_scsiio *csio, uint32_t retries,
8952 			void (*cbfcnp)(struct cam_periph *, union ccb*),
8953 			uint8_t tag_action, int mode,
8954 			uint8_t buffer_id, uint32_t offset,
8955 			uint8_t *data_ptr, uint32_t allocation_length,
8956 			uint8_t sense_len, uint32_t timeout)
8957 {
8958 	struct scsi_read_buffer *scsi_cmd;
8959 
8960 	scsi_cmd = (struct scsi_read_buffer *)&csio->cdb_io.cdb_bytes;
8961 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8962 	scsi_cmd->opcode = READ_BUFFER;
8963 	scsi_cmd->byte2 = mode;
8964 	scsi_cmd->buffer_id = buffer_id;
8965 	scsi_ulto3b(offset, scsi_cmd->offset);
8966 	scsi_ulto3b(allocation_length, scsi_cmd->length);
8967 
8968 	cam_fill_csio(csio,
8969 		      retries,
8970 		      cbfcnp,
8971 		      /*flags*/CAM_DIR_IN,
8972 		      tag_action,
8973 		      data_ptr,
8974 		      allocation_length,
8975 		      sense_len,
8976 		      sizeof(*scsi_cmd),
8977 		      timeout);
8978 }
8979 
8980 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)8981 scsi_write_buffer(struct ccb_scsiio *csio, uint32_t retries,
8982 			void (*cbfcnp)(struct cam_periph *, union ccb *),
8983 			uint8_t tag_action, int mode,
8984 			uint8_t buffer_id, uint32_t offset,
8985 			uint8_t *data_ptr, uint32_t param_list_length,
8986 			uint8_t sense_len, uint32_t timeout)
8987 {
8988 	struct scsi_write_buffer *scsi_cmd;
8989 
8990 	scsi_cmd = (struct scsi_write_buffer *)&csio->cdb_io.cdb_bytes;
8991 	memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8992 	scsi_cmd->opcode = WRITE_BUFFER;
8993 	scsi_cmd->byte2 = mode;
8994 	scsi_cmd->buffer_id = buffer_id;
8995 	scsi_ulto3b(offset, scsi_cmd->offset);
8996 	scsi_ulto3b(param_list_length, scsi_cmd->length);
8997 
8998 	cam_fill_csio(csio,
8999 		      retries,
9000 		      cbfcnp,
9001 		      /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
9002 		      tag_action,
9003 		      data_ptr,
9004 		      param_list_length,
9005 		      sense_len,
9006 		      sizeof(*scsi_cmd),
9007 		      timeout);
9008 }
9009 
9010 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 power_condition,uint8_t sense_len,uint32_t timeout)9011 scsi_start_stop(struct ccb_scsiio *csio, uint32_t retries,
9012 		void (*cbfcnp)(struct cam_periph *, union ccb *),
9013 		uint8_t tag_action, int start, int load_eject,
9014 		int immediate, uint8_t power_condition, uint8_t sense_len,
9015 		uint32_t timeout)
9016 {
9017 	struct scsi_start_stop_unit *scsi_cmd;
9018 	int extra_flags = 0;
9019 
9020 	scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
9021 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9022 	scsi_cmd->opcode = START_STOP_UNIT;
9023 	if (start != 0) {
9024 		scsi_cmd->how |= SSS_START;
9025 		/* it takes a lot of power to start a drive */
9026 		extra_flags |= CAM_HIGH_POWER;
9027 	}
9028 	if (load_eject != 0)
9029 		scsi_cmd->how |= SSS_LOEJ;
9030 	scsi_cmd->how |= power_condition;
9031 	if (immediate != 0)
9032 		scsi_cmd->byte2 |= SSS_IMMED;
9033 
9034 	cam_fill_csio(csio,
9035 		      retries,
9036 		      cbfcnp,
9037 		      /*flags*/CAM_DIR_NONE | extra_flags,
9038 		      tag_action,
9039 		      /*data_ptr*/NULL,
9040 		      /*dxfer_len*/0,
9041 		      sense_len,
9042 		      sizeof(*scsi_cmd),
9043 		      timeout);
9044 }
9045 
9046 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)9047 scsi_read_attribute(struct ccb_scsiio *csio, uint32_t retries,
9048 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
9049 		    uint8_t tag_action, uint8_t service_action,
9050 		    uint32_t element, uint8_t elem_type, int logical_volume,
9051 		    int partition, uint32_t first_attribute, int cache,
9052 		    uint8_t *data_ptr, uint32_t length, int sense_len,
9053 		    uint32_t timeout)
9054 {
9055 	struct scsi_read_attribute *scsi_cmd;
9056 
9057 	scsi_cmd = (struct scsi_read_attribute *)&csio->cdb_io.cdb_bytes;
9058 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9059 
9060 	scsi_cmd->opcode = READ_ATTRIBUTE;
9061 	scsi_cmd->service_action = service_action;
9062 	scsi_ulto2b(element, scsi_cmd->element);
9063 	scsi_cmd->elem_type = elem_type;
9064 	scsi_cmd->logical_volume = logical_volume;
9065 	scsi_cmd->partition = partition;
9066 	scsi_ulto2b(first_attribute, scsi_cmd->first_attribute);
9067 	scsi_ulto4b(length, scsi_cmd->length);
9068 	if (cache != 0)
9069 		scsi_cmd->cache |= SRA_CACHE;
9070 
9071 	cam_fill_csio(csio,
9072 		      retries,
9073 		      cbfcnp,
9074 		      /*flags*/CAM_DIR_IN,
9075 		      tag_action,
9076 		      /*data_ptr*/data_ptr,
9077 		      /*dxfer_len*/length,
9078 		      sense_len,
9079 		      sizeof(*scsi_cmd),
9080 		      timeout);
9081 }
9082 
9083 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)9084 scsi_write_attribute(struct ccb_scsiio *csio, uint32_t retries,
9085 		    void (*cbfcnp)(struct cam_periph *, union ccb *),
9086 		    uint8_t tag_action, uint32_t element, int logical_volume,
9087 		    int partition, int wtc, uint8_t *data_ptr,
9088 		    uint32_t length, int sense_len, uint32_t timeout)
9089 {
9090 	struct scsi_write_attribute *scsi_cmd;
9091 
9092 	scsi_cmd = (struct scsi_write_attribute *)&csio->cdb_io.cdb_bytes;
9093 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9094 
9095 	scsi_cmd->opcode = WRITE_ATTRIBUTE;
9096 	if (wtc != 0)
9097 		scsi_cmd->byte2 = SWA_WTC;
9098 	scsi_ulto3b(element, scsi_cmd->element);
9099 	scsi_cmd->logical_volume = logical_volume;
9100 	scsi_cmd->partition = partition;
9101 	scsi_ulto4b(length, scsi_cmd->length);
9102 
9103 	cam_fill_csio(csio,
9104 		      retries,
9105 		      cbfcnp,
9106 		      /*flags*/CAM_DIR_OUT,
9107 		      tag_action,
9108 		      /*data_ptr*/data_ptr,
9109 		      /*dxfer_len*/length,
9110 		      sense_len,
9111 		      sizeof(*scsi_cmd),
9112 		      timeout);
9113 }
9114 
9115 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)9116 scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries,
9117 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
9118 			   uint8_t tag_action, int service_action,
9119 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
9120 			   int timeout)
9121 {
9122 	struct scsi_per_res_in *scsi_cmd;
9123 
9124 	scsi_cmd = (struct scsi_per_res_in *)&csio->cdb_io.cdb_bytes;
9125 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9126 
9127 	scsi_cmd->opcode = PERSISTENT_RES_IN;
9128 	scsi_cmd->action = service_action;
9129 	scsi_ulto2b(dxfer_len, scsi_cmd->length);
9130 
9131 	cam_fill_csio(csio,
9132 		      retries,
9133 		      cbfcnp,
9134 		      /*flags*/CAM_DIR_IN,
9135 		      tag_action,
9136 		      data_ptr,
9137 		      dxfer_len,
9138 		      sense_len,
9139 		      sizeof(*scsi_cmd),
9140 		      timeout);
9141 }
9142 
9143 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)9144 scsi_persistent_reserve_out(struct ccb_scsiio *csio, uint32_t retries,
9145 			    void (*cbfcnp)(struct cam_periph *, union ccb *),
9146 			    uint8_t tag_action, int service_action,
9147 			    int scope, int res_type, uint8_t *data_ptr,
9148 			    uint32_t dxfer_len, int sense_len, int timeout)
9149 {
9150 	struct scsi_per_res_out *scsi_cmd;
9151 
9152 	scsi_cmd = (struct scsi_per_res_out *)&csio->cdb_io.cdb_bytes;
9153 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9154 
9155 	scsi_cmd->opcode = PERSISTENT_RES_OUT;
9156 	scsi_cmd->action = service_action;
9157 	scsi_cmd->scope_type = scope | res_type;
9158 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
9159 
9160 	cam_fill_csio(csio,
9161 		      retries,
9162 		      cbfcnp,
9163 		      /*flags*/CAM_DIR_OUT,
9164 		      tag_action,
9165 		      /*data_ptr*/data_ptr,
9166 		      /*dxfer_len*/dxfer_len,
9167 		      sense_len,
9168 		      sizeof(*scsi_cmd),
9169 		      timeout);
9170 }
9171 
9172 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)9173 scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries,
9174 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
9175 			  uint8_t tag_action, uint32_t security_protocol,
9176 			  uint32_t security_protocol_specific, int byte4,
9177 			  uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
9178 			  int timeout)
9179 {
9180 	struct scsi_security_protocol_in *scsi_cmd;
9181 
9182 	scsi_cmd = (struct scsi_security_protocol_in *)&csio->cdb_io.cdb_bytes;
9183 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9184 
9185 	scsi_cmd->opcode = SECURITY_PROTOCOL_IN;
9186 
9187 	scsi_cmd->security_protocol = security_protocol;
9188 	scsi_ulto2b(security_protocol_specific,
9189 		    scsi_cmd->security_protocol_specific);
9190 	scsi_cmd->byte4 = byte4;
9191 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
9192 
9193 	cam_fill_csio(csio,
9194 		      retries,
9195 		      cbfcnp,
9196 		      /*flags*/CAM_DIR_IN,
9197 		      tag_action,
9198 		      data_ptr,
9199 		      dxfer_len,
9200 		      sense_len,
9201 		      sizeof(*scsi_cmd),
9202 		      timeout);
9203 }
9204 
9205 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)9206 scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries,
9207 			   void (*cbfcnp)(struct cam_periph *, union ccb *),
9208 			   uint8_t tag_action, uint32_t security_protocol,
9209 			   uint32_t security_protocol_specific, int byte4,
9210 			   uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
9211 			   int timeout)
9212 {
9213 	struct scsi_security_protocol_out *scsi_cmd;
9214 
9215 	scsi_cmd = (struct scsi_security_protocol_out *)&csio->cdb_io.cdb_bytes;
9216 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9217 
9218 	scsi_cmd->opcode = SECURITY_PROTOCOL_OUT;
9219 
9220 	scsi_cmd->security_protocol = security_protocol;
9221 	scsi_ulto2b(security_protocol_specific,
9222 		    scsi_cmd->security_protocol_specific);
9223 	scsi_cmd->byte4 = byte4;
9224 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
9225 
9226 	cam_fill_csio(csio,
9227 		      retries,
9228 		      cbfcnp,
9229 		      /*flags*/CAM_DIR_OUT,
9230 		      tag_action,
9231 		      data_ptr,
9232 		      dxfer_len,
9233 		      sense_len,
9234 		      sizeof(*scsi_cmd),
9235 		      timeout);
9236 }
9237 
9238 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)9239 scsi_report_supported_opcodes(struct ccb_scsiio *csio, uint32_t retries,
9240 			      void (*cbfcnp)(struct cam_periph *, union ccb *),
9241 			      uint8_t tag_action, int options, int req_opcode,
9242 			      int req_service_action, uint8_t *data_ptr,
9243 			      uint32_t dxfer_len, int sense_len, int timeout)
9244 {
9245 	struct scsi_report_supported_opcodes *scsi_cmd;
9246 
9247 	scsi_cmd = (struct scsi_report_supported_opcodes *)
9248 	    &csio->cdb_io.cdb_bytes;
9249 	bzero(scsi_cmd, sizeof(*scsi_cmd));
9250 
9251 	scsi_cmd->opcode = MAINTENANCE_IN;
9252 	scsi_cmd->service_action = REPORT_SUPPORTED_OPERATION_CODES;
9253 	scsi_cmd->options = options;
9254 	scsi_cmd->requested_opcode = req_opcode;
9255 	scsi_ulto2b(req_service_action, scsi_cmd->requested_service_action);
9256 	scsi_ulto4b(dxfer_len, scsi_cmd->length);
9257 
9258 	cam_fill_csio(csio,
9259 		      retries,
9260 		      cbfcnp,
9261 		      /*flags*/CAM_DIR_IN,
9262 		      tag_action,
9263 		      data_ptr,
9264 		      dxfer_len,
9265 		      sense_len,
9266 		      sizeof(*scsi_cmd),
9267 		      timeout);
9268 }
9269 
9270 /*
9271  * Try make as good a match as possible with
9272  * available sub drivers
9273  */
9274 int
scsi_inquiry_match(caddr_t inqbuffer,caddr_t table_entry)9275 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
9276 {
9277 	struct scsi_inquiry_pattern *entry;
9278 	struct scsi_inquiry_data *inq;
9279 
9280 	entry = (struct scsi_inquiry_pattern *)table_entry;
9281 	inq = (struct scsi_inquiry_data *)inqbuffer;
9282 
9283 	if (((SID_TYPE(inq) == entry->type)
9284 	  || (entry->type == T_ANY))
9285 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
9286 				   : entry->media_type & SIP_MEDIA_FIXED)
9287 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
9288 	 && (cam_strmatch(inq->product, entry->product,
9289 			  sizeof(inq->product)) == 0)
9290 	 && (cam_strmatch(inq->revision, entry->revision,
9291 			  sizeof(inq->revision)) == 0)) {
9292 		return (0);
9293 	}
9294         return (-1);
9295 }
9296 
9297 /*
9298  * Try make as good a match as possible with
9299  * available sub drivers
9300  */
9301 int
scsi_static_inquiry_match(caddr_t inqbuffer,caddr_t table_entry)9302 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
9303 {
9304 	struct scsi_static_inquiry_pattern *entry;
9305 	struct scsi_inquiry_data *inq;
9306 
9307 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
9308 	inq = (struct scsi_inquiry_data *)inqbuffer;
9309 
9310 	if (((SID_TYPE(inq) == entry->type)
9311 	  || (entry->type == T_ANY))
9312 	 && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
9313 				   : entry->media_type & SIP_MEDIA_FIXED)
9314 	 && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
9315 	 && (cam_strmatch(inq->product, entry->product,
9316 			  sizeof(inq->product)) == 0)
9317 	 && (cam_strmatch(inq->revision, entry->revision,
9318 			  sizeof(inq->revision)) == 0)) {
9319 		return (0);
9320 	}
9321         return (-1);
9322 }
9323 
9324 /**
9325  * Compare two buffers of vpd device descriptors for a match.
9326  *
9327  * \param lhs      Pointer to first buffer of descriptors to compare.
9328  * \param lhs_len  The length of the first buffer.
9329  * \param rhs	   Pointer to second buffer of descriptors to compare.
9330  * \param rhs_len  The length of the second buffer.
9331  *
9332  * \return  0 on a match, -1 otherwise.
9333  *
9334  * Treat rhs and lhs as arrays of vpd device id descriptors.  Walk lhs matching
9335  * against each element in rhs until all data are exhausted or we have found
9336  * a match.
9337  */
9338 int
scsi_devid_match(uint8_t * lhs,size_t lhs_len,uint8_t * rhs,size_t rhs_len)9339 scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
9340 {
9341 	struct scsi_vpd_id_descriptor *lhs_id;
9342 	struct scsi_vpd_id_descriptor *lhs_last;
9343 	struct scsi_vpd_id_descriptor *rhs_last;
9344 	uint8_t *lhs_end;
9345 	uint8_t *rhs_end;
9346 
9347 	lhs_end = lhs + lhs_len;
9348 	rhs_end = rhs + rhs_len;
9349 
9350 	/*
9351 	 * rhs_last and lhs_last are the last possible position of a valid
9352 	 * descriptor assuming it had a zero length identifier.  We use
9353 	 * these variables to insure we can safely dereference the length
9354 	 * field in our loop termination tests.
9355 	 */
9356 	lhs_last = (struct scsi_vpd_id_descriptor *)
9357 	    (lhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
9358 	rhs_last = (struct scsi_vpd_id_descriptor *)
9359 	    (rhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
9360 
9361 	lhs_id = (struct scsi_vpd_id_descriptor *)lhs;
9362 	while (lhs_id <= lhs_last
9363 	    && (lhs_id->identifier + lhs_id->length) <= lhs_end) {
9364 		struct scsi_vpd_id_descriptor *rhs_id;
9365 
9366 		rhs_id = (struct scsi_vpd_id_descriptor *)rhs;
9367 		while (rhs_id <= rhs_last
9368 		    && (rhs_id->identifier + rhs_id->length) <= rhs_end) {
9369 			if ((rhs_id->id_type &
9370 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK)) ==
9371 			    (lhs_id->id_type &
9372 			     (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK))
9373 			 && rhs_id->length == lhs_id->length
9374 			 && memcmp(rhs_id->identifier, lhs_id->identifier,
9375 				   rhs_id->length) == 0)
9376 				return (0);
9377 
9378 			rhs_id = (struct scsi_vpd_id_descriptor *)
9379 			   (rhs_id->identifier + rhs_id->length);
9380 		}
9381 		lhs_id = (struct scsi_vpd_id_descriptor *)
9382 		   (lhs_id->identifier + lhs_id->length);
9383 	}
9384 	return (-1);
9385 }
9386 
9387 #ifdef _KERNEL
9388 int
scsi_vpd_supported_page(struct cam_periph * periph,uint8_t page_id)9389 scsi_vpd_supported_page(struct cam_periph *periph, uint8_t page_id)
9390 {
9391 	struct cam_ed *device;
9392 	struct scsi_vpd_supported_pages *vpds;
9393 	int i, num_pages;
9394 
9395 	device = periph->path->device;
9396 	vpds = (struct scsi_vpd_supported_pages *)device->supported_vpds;
9397 
9398 	if (vpds != NULL) {
9399 		num_pages = device->supported_vpds_len -
9400 		    SVPD_SUPPORTED_PAGES_HDR_LEN;
9401 		for (i = 0; i < num_pages; i++) {
9402 			if (vpds->page_list[i] == page_id)
9403 				return (1);
9404 		}
9405 	}
9406 
9407 	return (0);
9408 }
9409 
9410 static void
decode_sks(struct sbuf * sb,int sk,uint8_t * sks)9411 decode_sks(struct sbuf *sb, int sk, uint8_t *sks)
9412 {
9413 	/*
9414 	 * The format of the sense key specific info varies based on key types.
9415 	 * The valid bit should be checked by the caller for fixed formats and
9416 	 * is always one for variable formats since it's mere presence signals
9417 	 * validity. SPC7 4.4.2.4.1 and 4.4.3.
9418 	 */
9419 	switch (sk) {
9420 	case SSD_KEY_ILLEGAL_REQUEST:
9421 	{
9422 		const char *type;
9423 
9424 		type = sks[0] & SSD_FIELDPTR_CMD ? "CDB" : "buffer";
9425 		sbuf_printf(sb, "error_in=\"%s\" ", type);
9426 		if (sks[0] & SSD_BITPTR_VALID) {
9427 			sbuf_printf(sb, "bit_ptr=%d ", sks[0] & SSD_BITPTR_VALUE);
9428 		}
9429 		sbuf_printf(sb, "byte=%d ", scsi_2btoul(sks + 1));
9430 		break;
9431 	}
9432 	case SSD_KEY_HARDWARE_ERROR:
9433 	case SSD_KEY_MEDIUM_ERROR:
9434 	case SSD_KEY_RECOVERED_ERROR:
9435 		sbuf_printf(sb, "retry_count=%d ", scsi_2btoul(sks + 1));
9436 		break;
9437 	case SSD_KEY_NO_SENSE:
9438 	case SSD_KEY_NOT_READY:
9439 		sbuf_printf(sb, "progress=%d ", scsi_2btoul(sks + 1));
9440 		break;
9441 	case SSD_KEY_COPY_ABORTED:
9442 	{
9443 		const char *type;
9444 
9445 		/* Note: segment number in cmd_info if SD=1 */
9446 		type = sks[0] & SSD_SD_VALID ? "rel" : "abs";
9447 		    sbuf_printf(sb, "segment=\"%s\" ", type);
9448 		if (sks[0] & SSD_BITPTR_VALID) {
9449 			sbuf_printf(sb, "bit_ptr=%d ", sks[0] & SSD_BITPTR_VALUE);
9450 		}
9451 		sbuf_printf(sb, "byte=%d ", scsi_2btoul(sks + 1));
9452 		break;
9453 	}
9454 	case SSD_KEY_UNIT_ATTENTION:
9455 		sbuf_printf(sb, "overflow=%d ", sks[0] & 0x1);
9456 		break;
9457 	default:
9458 		/*
9459 		 * NO DATA - SKSV should be zero, but no
9460 		 * reported data on 1 either.
9461 		 */
9462 		break;
9463 	}
9464 }
9465 
9466 /*
9467  * Decode the sense buffer we get back from the drive. See SPC7 4.4
9468  * for details.
9469  */
9470 void
scsi_format_sense_devd(struct ccb_scsiio * csio,struct sbuf * sb)9471 scsi_format_sense_devd(struct ccb_scsiio *csio, struct sbuf *sb)
9472 {
9473 	int serr, sk, asc, ascq, slen;
9474 	struct scsi_sense_data *sense_data;
9475 	uint8_t *walker, *ep;
9476 	union ccb *ccb = (union ccb *)csio;
9477 
9478 	sbuf_printf(sb, "scsi_status=%d ", csio->scsi_status);
9479 	if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq))
9480 		sbuf_printf(sb, "scsi_sense=\"%02x %02x %02x %02x\" ",
9481 		    serr, sk, asc, ascq);
9482 	if (csio->ccb_h.flags & CAM_SENSE_PTR)
9483 		bcopy((struct scsi_sense_data **)&csio->sense_data,
9484 		    &sense_data, sizeof(struct scsi_sense_data *));
9485 	else
9486 		sense_data = &csio->sense_data;
9487 	/*
9488 	 * Decode the rest of the sense buffer
9489 	 */
9490 	slen = csio->sense_len - csio->sense_resid;
9491 	walker = (uint8_t *)sense_data;
9492 	ep = walker + slen;
9493 	switch (*walker) {
9494 	case SSD_DESC_CURRENT_ERROR:
9495 	case SSD_DESC_DEFERRED_ERROR:
9496 	{
9497 		struct scsi_sense_data_desc *sdesc = (struct scsi_sense_data_desc *)walker;
9498 
9499 		walker = sdesc->sense_desc;
9500 		if (walker + sdesc->extra_len > ep)
9501 			return;	/* more data than buffer, just punt */
9502 		ep = walker + sdesc->extra_len;
9503 		while (walker < ep) {
9504 			struct scsi_sense_desc_header *hdr =
9505 			    (struct scsi_sense_desc_header *)walker;
9506 
9507 			switch (hdr->desc_type) {
9508 			case SSD_DESC_INFO:
9509 			{
9510 				struct scsi_sense_info *info;
9511 
9512 				info = (struct scsi_sense_info *)hdr;
9513 				if ((info->byte2 & SSD_INFO_VALID) == 0)
9514 					break;
9515 				sbuf_printf(sb, "info=0x%jx ",
9516 				    scsi_8btou64(info->info));
9517 				break;
9518 			}
9519 			case SSD_DESC_COMMAND:
9520 			{
9521 				struct scsi_sense_command *command;
9522 
9523 				command = (struct scsi_sense_command *)hdr;
9524 				sbuf_printf(sb, "cmd_info=0x%jx ",
9525 				    scsi_8btou64(command->command_info));
9526 				break;
9527 			}
9528 			case SSD_DESC_SKS:
9529 				/* len sanity */
9530 				decode_sks(sb, sk, walker + 2);
9531 				break;
9532 			case SSD_DESC_FRU:
9533 			{
9534 				struct scsi_sense_fru *fru;
9535 
9536 				fru = (struct scsi_sense_fru *)hdr;
9537 				sbuf_printf(sb, "fru=%u ", fru->fru);
9538 				break;
9539 			}
9540 			case SSD_DESC_ATA:
9541 			{
9542 				struct scsi_sense_ata_ret_desc *res;
9543 				uint16_t count;
9544 				uint64_t lba;
9545 
9546 				res = (struct scsi_sense_ata_ret_desc *)hdr;
9547 				sbuf_printf(sb, "ata_status=0x%02x ata_error=0x%02x ",
9548 				    res->status, res->error);
9549 				if (res->flags & SSD_DESC_ATA_FLAG_EXTEND) {
9550 					count = ((uint16_t)res->count_15_8 << 8) | res->count_7_0;
9551 					lba = ((uint64_t)res->lba_47_40 << 40) |
9552 					    ((uint64_t)res->lba_39_32 << 32) |
9553 					    ((uint64_t)res->lba_31_24 << 24) |
9554 					    ((uint64_t)res->lba_23_16 << 16) |
9555 					    ((uint64_t)res->lba_15_8 << 8) |
9556 					    res->lba_7_0;
9557 				} else {
9558 					count = res->count_7_0;
9559 					lba = ((uint64_t)res->lba_23_16 << 16) |
9560 					    ((uint64_t)res->lba_15_8 << 8) |
9561 					    res->lba_7_0;
9562 				}
9563 				sbuf_printf(sb, "count=%d lba=0x%jx ", count, (uintmax_t)lba);
9564 				break;
9565 			}
9566 			case SSD_DESC_DABD:
9567 			{
9568 				struct scsi_sense_direct_access_block_device *dabd;
9569 
9570 				dabd = (struct scsi_sense_direct_access_block_device *)hdr;
9571 
9572 				if (dabd->sks_byte & SSD_DESC_DABD_SKS_VALID)
9573 					decode_sks(sb, sk, dabd->data);
9574 				sbuf_printf(sb, "fru=%u ", dabd->fru);
9575 				if (dabd->byte2 & SSD_DESC_DABD_VALID) {
9576 					sbuf_printf(sb, "info=0x%jx ",
9577 					    scsi_8btou64(dabd->info));
9578 					sbuf_printf(sb, "cmd_info=0x%jx ",
9579 					    scsi_8btou64(dabd->command_info));
9580 				}
9581 			}
9582 			default:
9583 			{
9584 				uint8_t *cp;
9585 
9586 				if (hdr->desc_type >= SSD_DESC_VENDOR_MIN && hdr->desc_type <= SSD_DESC_VENDOR_MAX)
9587 					sbuf_printf(sb, "ven%02x=\"", hdr->desc_type);
9588 				else
9589 					sbuf_printf(sb, "desc%02x=\"", hdr->desc_type);
9590 				cp = (uint8_t *)&hdr[1];
9591 				for (int i = 0; i < hdr->length; i++, cp++)
9592 					sbuf_printf(sb, "%02x ", *cp);
9593 				sbuf_printf(sb, "\" ");
9594 				break;
9595 			}
9596 			} /* switch */
9597 
9598 			walker += sizeof(*hdr) + hdr->length;
9599 		}
9600 	}
9601 	case SSD_CURRENT_ERROR:
9602 	case SSD_DEFERRED_ERROR:
9603 	{
9604 		struct scsi_sense_data_fixed *sfixed = (struct scsi_sense_data_fixed *)walker;
9605 
9606 		if (sfixed->error_code & SSD_ERRCODE_VALID) {
9607 			uint32_t val = scsi_4btoul(sfixed->info);
9608 
9609 			sbuf_printf(sb, "info=0x%x ", val);
9610 		}
9611 		if (SSD_FIXED_IS_PRESENT(sfixed, slen, cmd_spec_info) &&
9612 		    SSD_FIXED_IS_FILLED(sfixed, cmd_spec_info)) {
9613 			uint32_t val = scsi_4btoul(sfixed->cmd_spec_info);
9614 
9615 			sbuf_printf(sb, "cmd_info=0x%x ", val);
9616 		}
9617 		if (SSD_FIXED_IS_PRESENT(sfixed, slen, fru) &&
9618 		    SSD_FIXED_IS_FILLED(sfixed, fru)) {
9619 
9620 			sbuf_printf(sb, "fru=0x%x ", sfixed->fru);
9621 		}
9622 		if (SSD_FIXED_IS_PRESENT(sfixed, slen, sense_key_spec) &&
9623 		    SSD_FIXED_IS_FILLED(sfixed, sense_key_spec) &&
9624 		    (sfixed->sense_key_spec[0] & SSD_SCS_VALID) != 0) {
9625 			decode_sks(sb, sk, sfixed->sense_key_spec);
9626 		}
9627 		/* Additional bytes not reported -- vendor specific */
9628 		/* Report the bytes present ? but can't use macros since it's at most 14 bytes */
9629 	}
9630 	} /* switch */
9631 }
9632 
9633 static void
init_scsi_delay(void * dummy __unused)9634 init_scsi_delay(void *dummy __unused)
9635 {
9636 	int delay;
9637 
9638 	delay = SCSI_DELAY;
9639 	TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
9640 
9641 	if (set_scsi_delay(delay) != 0) {
9642 		printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
9643 		set_scsi_delay(SCSI_DELAY);
9644 	}
9645 }
9646 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
9647 
9648 static int
sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)9649 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
9650 {
9651 	int error, delay;
9652 
9653 	delay = scsi_delay;
9654 	error = sysctl_handle_int(oidp, &delay, 0, req);
9655 	if (error != 0 || req->newptr == NULL)
9656 		return (error);
9657 	return (set_scsi_delay(delay));
9658 }
9659 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay,
9660     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
9661     0, 0, sysctl_scsi_delay, "I",
9662     "Delay to allow devices to settle after a SCSI bus reset (ms)");
9663 
9664 static int
set_scsi_delay(int delay)9665 set_scsi_delay(int delay)
9666 {
9667 	/*
9668          * If someone sets this to 0, we assume that they want the
9669          * minimum allowable bus settle delay.
9670 	 */
9671 	if (delay == 0) {
9672 		printf("cam: using minimum scsi_delay (%dms)\n",
9673 		    SCSI_MIN_DELAY);
9674 		delay = SCSI_MIN_DELAY;
9675 	}
9676 	if (delay < SCSI_MIN_DELAY)
9677 		return (EINVAL);
9678 	scsi_delay = delay;
9679 	return (0);
9680 }
9681 #endif /* _KERNEL */
9682