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