1/*-
2 * Implementation of Utility functions for all SCSI device types.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions, and the following disclaimer,
15 *    without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/types.h>
37#include <sys/stdint.h>
38
39#ifdef _KERNEL
40#include "opt_scsi.h"
41
42#include <sys/systm.h>
43#include <sys/libkern.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/malloc.h>
47#include <sys/mutex.h>
48#include <sys/sysctl.h>
49#include <sys/ctype.h>
50#else
51#include <errno.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <ctype.h>
56#endif
57
58#include <cam/cam.h>
59#include <cam/cam_ccb.h>
60#include <cam/cam_queue.h>
61#include <cam/cam_xpt.h>
62#include <cam/scsi/scsi_all.h>
63#include <sys/ata.h>
64#include <sys/sbuf.h>
65
66#ifdef _KERNEL
67#include <cam/cam_periph.h>
68#include <cam/cam_xpt_sim.h>
69#include <cam/cam_xpt_periph.h>
70#include <cam/cam_xpt_internal.h>
71#else
72#include <camlib.h>
73#include <stddef.h>
74
75#ifndef FALSE
76#define FALSE   0
77#endif /* FALSE */
78#ifndef TRUE
79#define TRUE    1
80#endif /* TRUE */
81#define ERESTART        -1              /* restart syscall */
82#define EJUSTRETURN     -2              /* don't modify regs, just return */
83#endif /* !_KERNEL */
84
85/*
86 * This is the default number of milliseconds we wait for devices to settle
87 * after a SCSI bus reset.
88 */
89#ifndef SCSI_DELAY
90#define SCSI_DELAY 2000
91#endif
92/*
93 * All devices need _some_ sort of bus settle delay, so we'll set it to
94 * a minimum value of 100ms. Note that this is pertinent only for SPI-
95 * not transport like Fibre Channel or iSCSI where 'delay' is completely
96 * meaningless.
97 */
98#ifndef SCSI_MIN_DELAY
99#define SCSI_MIN_DELAY 100
100#endif
101/*
102 * Make sure the user isn't using seconds instead of milliseconds.
103 */
104#if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
105#error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
106#endif
107
108int scsi_delay;
109
110static int	ascentrycomp(const void *key, const void *member);
111static int	senseentrycomp(const void *key, const void *member);
112static void	fetchtableentries(int sense_key, int asc, int ascq,
113				  struct scsi_inquiry_data *,
114				  const struct sense_key_table_entry **,
115				  const struct asc_table_entry **);
116
117#ifdef _KERNEL
118static void	init_scsi_delay(void);
119static int	sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
120static int	set_scsi_delay(int delay);
121#endif
122
123#if !defined(SCSI_NO_OP_STRINGS)
124
125#define	D	(1 << T_DIRECT)
126#define	T	(1 << T_SEQUENTIAL)
127#define	L	(1 << T_PRINTER)
128#define	P	(1 << T_PROCESSOR)
129#define	W	(1 << T_WORM)
130#define	R	(1 << T_CDROM)
131#define	O	(1 << T_OPTICAL)
132#define	M	(1 << T_CHANGER)
133#define	A	(1 << T_STORARRAY)
134#define	E	(1 << T_ENCLOSURE)
135#define	B	(1 << T_RBC)
136#define	K	(1 << T_OCRW)
137#define	V	(1 << T_ADC)
138#define	F	(1 << T_OSD)
139#define	S	(1 << T_SCANNER)
140#define	C	(1 << T_COMM)
141
142#define ALL	(D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
143
144static struct op_table_entry plextor_cd_ops[] = {
145	{ 0xD8, R, "CD-DA READ" }
146};
147
148static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
149	{
150		/*
151		 * I believe that 0xD8 is the Plextor proprietary command
152		 * to read CD-DA data.  I'm not sure which Plextor CDROM
153		 * models support the command, though.  I know for sure
154		 * that the 4X, 8X, and 12X models do, and presumably the
155		 * 12-20X does.  I don't know about any earlier models,
156		 * though.  If anyone has any more complete information,
157		 * feel free to change this quirk entry.
158		 */
159		{T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
160		nitems(plextor_cd_ops),
161		plextor_cd_ops
162	}
163};
164
165static struct op_table_entry scsi_op_codes[] = {
166	/*
167	 * From: http://www.t10.org/lists/op-num.txt
168	 * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
169	 *              and Jung-uk Kim (jkim@FreeBSD.org)
170	 *
171	 * Note:  order is important in this table, scsi_op_desc() currently
172	 * depends on the opcodes in the table being in order to save
173	 * search time.
174	 * Note:  scanner and comm. devices are carried over from the previous
175	 * version because they were removed in the latest spec.
176	 */
177	/* File: OP-NUM.TXT
178	 *
179	 * SCSI Operation Codes
180	 * Numeric Sorted Listing
181	 * as of  5/26/15
182	 *
183	 *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
184	 *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
185	 *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
186	 *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
187	 *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
188	 *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
189	 *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
190	 *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
191	 *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
192	 *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
193	 *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
194	 *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
195	 *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
196	 *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
197	 * OP  DTLPWROMAEBKVF  Description
198	 * --  --------------  ---------------------------------------------- */
199	/* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
200	{ 0x00,	ALL, "TEST UNIT READY" },
201	/* 01   M              REWIND */
202	{ 0x01,	T, "REWIND" },
203	/* 01  Z V ZZZZ        REZERO UNIT */
204	{ 0x01,	D | W | R | O | M, "REZERO UNIT" },
205	/* 02  VVVVVV V */
206	/* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
207	{ 0x03,	ALL, "REQUEST SENSE" },
208	/* 04  M    OO         FORMAT UNIT */
209	{ 0x04,	D | R | O, "FORMAT UNIT" },
210	/* 04   O              FORMAT MEDIUM */
211	{ 0x04,	T, "FORMAT MEDIUM" },
212	/* 04    O             FORMAT */
213	{ 0x04,	L, "FORMAT" },
214	/* 05  VMVVVV V        READ BLOCK LIMITS */
215	{ 0x05,	T, "READ BLOCK LIMITS" },
216	/* 06  VVVVVV V */
217	/* 07  OVV O OV        REASSIGN BLOCKS */
218	{ 0x07,	D | W | O, "REASSIGN BLOCKS" },
219	/* 07         O        INITIALIZE ELEMENT STATUS */
220	{ 0x07,	M, "INITIALIZE ELEMENT STATUS" },
221	/* 08  MOV O OV        READ(6) */
222	{ 0x08,	D | T | W | O, "READ(6)" },
223	/* 08     O            RECEIVE */
224	{ 0x08,	P, "RECEIVE" },
225	/* 08                  GET MESSAGE(6) */
226	{ 0x08, C, "GET MESSAGE(6)" },
227	/* 09  VVVVVV V */
228	/* 0A  OO  O OV        WRITE(6) */
229	{ 0x0A,	D | T | W | O, "WRITE(6)" },
230	/* 0A     M            SEND(6) */
231	{ 0x0A,	P, "SEND(6)" },
232	/* 0A                  SEND MESSAGE(6) */
233	{ 0x0A, C, "SEND MESSAGE(6)" },
234	/* 0A    M             PRINT */
235	{ 0x0A,	L, "PRINT" },
236	/* 0B  Z   ZOZV        SEEK(6) */
237	{ 0x0B,	D | W | R | O, "SEEK(6)" },
238	/* 0B   O              SET CAPACITY */
239	{ 0x0B,	T, "SET CAPACITY" },
240	/* 0B    O             SLEW AND PRINT */
241	{ 0x0B,	L, "SLEW AND PRINT" },
242	/* 0C  VVVVVV V */
243	/* 0D  VVVVVV V */
244	/* 0E  VVVVVV V */
245	/* 0F  VOVVVV V        READ REVERSE(6) */
246	{ 0x0F,	T, "READ REVERSE(6)" },
247	/* 10  VM VVV          WRITE FILEMARKS(6) */
248	{ 0x10,	T, "WRITE FILEMARKS(6)" },
249	/* 10    O             SYNCHRONIZE BUFFER */
250	{ 0x10,	L, "SYNCHRONIZE BUFFER" },
251	/* 11  VMVVVV          SPACE(6) */
252	{ 0x11,	T, "SPACE(6)" },
253	/* 12  MMMMMMMMMMMMMM  INQUIRY */
254	{ 0x12,	ALL, "INQUIRY" },
255	/* 13  V VVVV */
256	/* 13   O              VERIFY(6) */
257	{ 0x13,	T, "VERIFY(6)" },
258	/* 14  VOOVVV          RECOVER BUFFERED DATA */
259	{ 0x14,	T | L, "RECOVER BUFFERED DATA" },
260	/* 15  OMO O OOOO OO   MODE SELECT(6) */
261	{ 0x15,	ALL & ~(P | R | B | F), "MODE SELECT(6)" },
262	/* 16  ZZMZO OOOZ O    RESERVE(6) */
263	{ 0x16,	ALL & ~(R | B | V | F | C), "RESERVE(6)" },
264	/* 16         Z        RESERVE ELEMENT(6) */
265	{ 0x16,	M, "RESERVE ELEMENT(6)" },
266	/* 17  ZZMZO OOOZ O    RELEASE(6) */
267	{ 0x17,	ALL & ~(R | B | V | F | C), "RELEASE(6)" },
268	/* 17         Z        RELEASE ELEMENT(6) */
269	{ 0x17,	M, "RELEASE ELEMENT(6)" },
270	/* 18  ZZZZOZO    Z    COPY */
271	{ 0x18,	D | T | L | P | W | R | O | K | S, "COPY" },
272	/* 19  VMVVVV          ERASE(6) */
273	{ 0x19,	T, "ERASE(6)" },
274	/* 1A  OMO O OOOO OO   MODE SENSE(6) */
275	{ 0x1A,	ALL & ~(P | R | B | F), "MODE SENSE(6)" },
276	/* 1B  O   OOO O MO O  START STOP UNIT */
277	{ 0x1B,	D | W | R | O | A | B | K | F, "START STOP UNIT" },
278	/* 1B   O          M   LOAD UNLOAD */
279	{ 0x1B,	T | V, "LOAD UNLOAD" },
280	/* 1B                  SCAN */
281	{ 0x1B, S, "SCAN" },
282	/* 1B    O             STOP PRINT */
283	{ 0x1B,	L, "STOP PRINT" },
284	/* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
285	{ 0x1B,	M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
286	/* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
287	{ 0x1C,	ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
288	/* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
289	{ 0x1D,	ALL & ~(R | B), "SEND DIAGNOSTIC" },
290	/* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
291	{ 0x1E,	D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
292	/* 1F */
293	/* 20  V   VVV    V */
294	/* 21  V   VVV    V */
295	/* 22  V   VVV    V */
296	/* 23  V   V V    V */
297	/* 23       O          READ FORMAT CAPACITIES */
298	{ 0x23,	R, "READ FORMAT CAPACITIES" },
299	/* 24  V   VV          SET WINDOW */
300	{ 0x24, S, "SET WINDOW" },
301	/* 25  M   M M   M     READ CAPACITY(10) */
302	{ 0x25,	D | W | O | B, "READ CAPACITY(10)" },
303	/* 25       O          READ CAPACITY */
304	{ 0x25,	R, "READ CAPACITY" },
305	/* 25             M    READ CARD CAPACITY */
306	{ 0x25,	K, "READ CARD CAPACITY" },
307	/* 25                  GET WINDOW */
308	{ 0x25, S, "GET WINDOW" },
309	/* 26  V   VV */
310	/* 27  V   VV */
311	/* 28  M   MOM   MM    READ(10) */
312	{ 0x28,	D | W | R | O | B | K | S, "READ(10)" },
313	/* 28                  GET MESSAGE(10) */
314	{ 0x28, C, "GET MESSAGE(10)" },
315	/* 29  V   VVO         READ GENERATION */
316	{ 0x29,	O, "READ GENERATION" },
317	/* 2A  O   MOM   MO    WRITE(10) */
318	{ 0x2A,	D | W | R | O | B | K, "WRITE(10)" },
319	/* 2A                  SEND(10) */
320	{ 0x2A, S, "SEND(10)" },
321	/* 2A                  SEND MESSAGE(10) */
322	{ 0x2A, C, "SEND MESSAGE(10)" },
323	/* 2B  Z   OOO    O    SEEK(10) */
324	{ 0x2B,	D | W | R | O | K, "SEEK(10)" },
325	/* 2B   O              LOCATE(10) */
326	{ 0x2B,	T, "LOCATE(10)" },
327	/* 2B         O        POSITION TO ELEMENT */
328	{ 0x2B,	M, "POSITION TO ELEMENT" },
329	/* 2C  V    OO         ERASE(10) */
330	{ 0x2C,	R | O, "ERASE(10)" },
331	/* 2D        O         READ UPDATED BLOCK */
332	{ 0x2D,	O, "READ UPDATED BLOCK" },
333	/* 2D  V */
334	/* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
335	{ 0x2E,	D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
336	/* 2F  O   OOO         VERIFY(10) */
337	{ 0x2F,	D | W | R | O, "VERIFY(10)" },
338	/* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
339	{ 0x30,	D | W | R | O, "SEARCH DATA HIGH(10)" },
340	/* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
341	{ 0x31,	D | W | R | O, "SEARCH DATA EQUAL(10)" },
342	/* 31                  OBJECT POSITION */
343	{ 0x31, S, "OBJECT POSITION" },
344	/* 32  Z   ZZZ         SEARCH DATA LOW(10) */
345	{ 0x32,	D | W | R | O, "SEARCH DATA LOW(10)" },
346	/* 33  Z   OZO         SET LIMITS(10) */
347	{ 0x33,	D | W | R | O, "SET LIMITS(10)" },
348	/* 34  O   O O    O    PRE-FETCH(10) */
349	{ 0x34,	D | W | O | K, "PRE-FETCH(10)" },
350	/* 34   M              READ POSITION */
351	{ 0x34,	T, "READ POSITION" },
352	/* 34                  GET DATA BUFFER STATUS */
353	{ 0x34, S, "GET DATA BUFFER STATUS" },
354	/* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
355	{ 0x35,	D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
356	/* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
357	{ 0x36,	D | W | O | K, "LOCK UNLOCK CACHE(10)" },
358	/* 37  O     O         READ DEFECT DATA(10) */
359	{ 0x37,	D | O, "READ DEFECT DATA(10)" },
360	/* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
361	{ 0x37,	M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
362	/* 38      O O    O    MEDIUM SCAN */
363	{ 0x38,	W | O | K, "MEDIUM SCAN" },
364	/* 39  ZZZZOZO    Z    COMPARE */
365	{ 0x39,	D | T | L | P | W | R | O | K | S, "COMPARE" },
366	/* 3A  ZZZZOZO    Z    COPY AND VERIFY */
367	{ 0x3A,	D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
368	/* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
369	{ 0x3B,	ALL, "WRITE BUFFER" },
370	/* 3C  OOOOOOOOOO OOO  READ BUFFER */
371	{ 0x3C,	ALL & ~(B), "READ BUFFER" },
372	/* 3D        O         UPDATE BLOCK */
373	{ 0x3D,	O, "UPDATE BLOCK" },
374	/* 3E  O   O O         READ LONG(10) */
375	{ 0x3E,	D | W | O, "READ LONG(10)" },
376	/* 3F  O   O O         WRITE LONG(10) */
377	{ 0x3F,	D | W | O, "WRITE LONG(10)" },
378	/* 40  ZZZZOZOZ        CHANGE DEFINITION */
379	{ 0x40,	D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
380	/* 41  O               WRITE SAME(10) */
381	{ 0x41,	D, "WRITE SAME(10)" },
382	/* 42  O               UNMAP */
383	{ 0x42,	D, "UNMAP" },
384	/* 42       O          READ SUB-CHANNEL */
385	{ 0x42,	R, "READ SUB-CHANNEL" },
386	/* 43       O          READ TOC/PMA/ATIP */
387	{ 0x43,	R, "READ TOC/PMA/ATIP" },
388	/* 44   M          M   REPORT DENSITY SUPPORT */
389	{ 0x44,	T | V, "REPORT DENSITY SUPPORT" },
390	/* 44                  READ HEADER */
391	/* 45       O          PLAY AUDIO(10) */
392	{ 0x45,	R, "PLAY AUDIO(10)" },
393	/* 46       M          GET CONFIGURATION */
394	{ 0x46,	R, "GET CONFIGURATION" },
395	/* 47       O          PLAY AUDIO MSF */
396	{ 0x47,	R, "PLAY AUDIO MSF" },
397	/* 48  O               SANITIZE */
398	{ 0x48,	D, "SANITIZE" },
399	/* 49 */
400	/* 4A       M          GET EVENT STATUS NOTIFICATION */
401	{ 0x4A,	R, "GET EVENT STATUS NOTIFICATION" },
402	/* 4B       O          PAUSE/RESUME */
403	{ 0x4B,	R, "PAUSE/RESUME" },
404	/* 4C  OOOOO OOOO OOO  LOG SELECT */
405	{ 0x4C,	ALL & ~(R | B), "LOG SELECT" },
406	/* 4D  OOOOO OOOO OMO  LOG SENSE */
407	{ 0x4D,	ALL & ~(R | B), "LOG SENSE" },
408	/* 4E       O          STOP PLAY/SCAN */
409	{ 0x4E,	R, "STOP PLAY/SCAN" },
410	/* 4F */
411	/* 50  O               XDWRITE(10) */
412	{ 0x50,	D, "XDWRITE(10)" },
413	/* 51  O               XPWRITE(10) */
414	{ 0x51,	D, "XPWRITE(10)" },
415	/* 51       O          READ DISC INFORMATION */
416	{ 0x51,	R, "READ DISC INFORMATION" },
417	/* 52  O               XDREAD(10) */
418	{ 0x52,	D, "XDREAD(10)" },
419	/* 52       O          READ TRACK INFORMATION */
420	{ 0x52,	R, "READ TRACK INFORMATION" },
421	/* 53       O          RESERVE TRACK */
422	{ 0x53,	R, "RESERVE TRACK" },
423	/* 54       O          SEND OPC INFORMATION */
424	{ 0x54,	R, "SEND OPC INFORMATION" },
425	/* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
426	{ 0x55,	ALL & ~(P), "MODE SELECT(10)" },
427	/* 56  ZZMZO OOOZ      RESERVE(10) */
428	{ 0x56,	ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
429	/* 56         Z        RESERVE ELEMENT(10) */
430	{ 0x56,	M, "RESERVE ELEMENT(10)" },
431	/* 57  ZZMZO OOOZ      RELEASE(10) */
432	{ 0x57,	ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
433	/* 57         Z        RELEASE ELEMENT(10) */
434	{ 0x57,	M, "RELEASE ELEMENT(10)" },
435	/* 58       O          REPAIR TRACK */
436	{ 0x58,	R, "REPAIR TRACK" },
437	/* 59 */
438	/* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
439	{ 0x5A,	ALL & ~(P), "MODE SENSE(10)" },
440	/* 5B       O          CLOSE TRACK/SESSION */
441	{ 0x5B,	R, "CLOSE TRACK/SESSION" },
442	/* 5C       O          READ BUFFER CAPACITY */
443	{ 0x5C,	R, "READ BUFFER CAPACITY" },
444	/* 5D       O          SEND CUE SHEET */
445	{ 0x5D,	R, "SEND CUE SHEET" },
446	/* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
447	{ 0x5E,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
448	/* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
449	{ 0x5F,	ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
450	/* 7E  OO   O OOOO O   extended CDB */
451	{ 0x7E,	D | T | R | M | A | E | B | V, "extended CDB" },
452	/* 7F  O            M  variable length CDB (more than 16 bytes) */
453	{ 0x7F,	D | F, "variable length CDB (more than 16 bytes)" },
454	/* 80  Z               XDWRITE EXTENDED(16) */
455	{ 0x80,	D, "XDWRITE EXTENDED(16)" },
456	/* 80   M              WRITE FILEMARKS(16) */
457	{ 0x80,	T, "WRITE FILEMARKS(16)" },
458	/* 81  Z               REBUILD(16) */
459	{ 0x81,	D, "REBUILD(16)" },
460	/* 81   O              READ REVERSE(16) */
461	{ 0x81,	T, "READ REVERSE(16)" },
462	/* 82  Z               REGENERATE(16) */
463	{ 0x82,	D, "REGENERATE(16)" },
464	/* 83  OOOOO O    OO   EXTENDED COPY */
465	{ 0x83,	D | T | L | P | W | O | K | V, "EXTENDED COPY" },
466	/* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
467	{ 0x84,	D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
468	/* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
469	{ 0x85,	D | R | B, "ATA COMMAND PASS THROUGH(16)" },
470	/* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
471	{ 0x86,	ALL & ~(L | R | F), "ACCESS CONTROL IN" },
472	/* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
473	{ 0x87,	ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
474	/* 88  MM  O O   O     READ(16) */
475	{ 0x88,	D | T | W | O | B, "READ(16)" },
476	/* 89  O               COMPARE AND WRITE*/
477	{ 0x89,	D, "COMPARE AND WRITE" },
478	/* 8A  OM  O O   O     WRITE(16) */
479	{ 0x8A,	D | T | W | O | B, "WRITE(16)" },
480	/* 8B  O               ORWRITE */
481	{ 0x8B,	D, "ORWRITE" },
482	/* 8C  OO  O OO  O M   READ ATTRIBUTE */
483	{ 0x8C,	D | T | W | O | M | B | V, "READ ATTRIBUTE" },
484	/* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
485	{ 0x8D,	D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
486	/* 8E  O   O O   O     WRITE AND VERIFY(16) */
487	{ 0x8E,	D | W | O | B, "WRITE AND VERIFY(16)" },
488	/* 8F  OO  O O   O     VERIFY(16) */
489	{ 0x8F,	D | T | W | O | B, "VERIFY(16)" },
490	/* 90  O   O O   O     PRE-FETCH(16) */
491	{ 0x90,	D | W | O | B, "PRE-FETCH(16)" },
492	/* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
493	{ 0x91,	D | W | O | B, "SYNCHRONIZE CACHE(16)" },
494	/* 91   O              SPACE(16) */
495	{ 0x91,	T, "SPACE(16)" },
496	/* 92  Z   O O         LOCK UNLOCK CACHE(16) */
497	{ 0x92,	D | W | O, "LOCK UNLOCK CACHE(16)" },
498	/* 92   O              LOCATE(16) */
499	{ 0x92,	T, "LOCATE(16)" },
500	/* 93  O               WRITE SAME(16) */
501	{ 0x93,	D, "WRITE SAME(16)" },
502	/* 93   M              ERASE(16) */
503	{ 0x93,	T, "ERASE(16)" },
504	/* 94  O               ZBC OUT */
505	{ 0x94,	ALL, "ZBC OUT" },
506	/* 95  O               ZBC IN */
507	{ 0x95,	ALL, "ZBC IN" },
508	/* 96 */
509	/* 97 */
510	/* 98 */
511	/* 99 */
512	/* 9A  O               WRITE STREAM(16) */
513	{ 0x9A,	D, "WRITE STREAM(16)" },
514	/* 9B  OOOOOOOOOO OOO  READ BUFFER(16) */
515	{ 0x9B,	ALL & ~(B) , "READ BUFFER(16)" },
516	/* 9C  O              WRITE ATOMIC(16) */
517	{ 0x9C, D, "WRITE ATOMIC(16)" },
518	/* 9D                  SERVICE ACTION BIDIRECTIONAL */
519	{ 0x9D, ALL, "SERVICE ACTION BIDIRECTIONAL" },
520	/* XXX KDM ALL for this?  op-num.txt defines it for none.. */
521	/* 9E                  SERVICE ACTION IN(16) */
522	{ 0x9E, ALL, "SERVICE ACTION IN(16)" },
523	/* 9F              M   SERVICE ACTION OUT(16) */
524	{ 0x9F,	ALL, "SERVICE ACTION OUT(16)" },
525	/* A0  MMOOO OMMM OMO  REPORT LUNS */
526	{ 0xA0,	ALL & ~(R | B), "REPORT LUNS" },
527	/* A1       O          BLANK */
528	{ 0xA1,	R, "BLANK" },
529	/* A1  O         O     ATA COMMAND PASS THROUGH(12) */
530	{ 0xA1,	D | B, "ATA COMMAND PASS THROUGH(12)" },
531	/* A2  OO   O      O   SECURITY PROTOCOL IN */
532	{ 0xA2,	D | T | R | V, "SECURITY PROTOCOL IN" },
533	/* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
534	{ 0xA3,	ALL & ~(P | R | F), "MAINTENANCE (IN)" },
535	/* A3       O          SEND KEY */
536	{ 0xA3,	R, "SEND KEY" },
537	/* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
538	{ 0xA4,	ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
539	/* A4       O          REPORT KEY */
540	{ 0xA4,	R, "REPORT KEY" },
541	/* A5   O  O OM        MOVE MEDIUM */
542	{ 0xA5,	T | W | O | M, "MOVE MEDIUM" },
543	/* A5       O          PLAY AUDIO(12) */
544	{ 0xA5,	R, "PLAY AUDIO(12)" },
545	/* A6         O        EXCHANGE MEDIUM */
546	{ 0xA6,	M, "EXCHANGE MEDIUM" },
547	/* A6       O          LOAD/UNLOAD C/DVD */
548	{ 0xA6,	R, "LOAD/UNLOAD C/DVD" },
549	/* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
550	{ 0xA7,	D | T | W | O, "MOVE MEDIUM ATTACHED" },
551	/* A7       O          SET READ AHEAD */
552	{ 0xA7,	R, "SET READ AHEAD" },
553	/* A8  O   OOO         READ(12) */
554	{ 0xA8,	D | W | R | O, "READ(12)" },
555	/* A8                  GET MESSAGE(12) */
556	{ 0xA8, C, "GET MESSAGE(12)" },
557	/* A9              O   SERVICE ACTION OUT(12) */
558	{ 0xA9,	V, "SERVICE ACTION OUT(12)" },
559	/* AA  O   OOO         WRITE(12) */
560	{ 0xAA,	D | W | R | O, "WRITE(12)" },
561	/* AA                  SEND MESSAGE(12) */
562	{ 0xAA, C, "SEND MESSAGE(12)" },
563	/* AB       O      O   SERVICE ACTION IN(12) */
564	{ 0xAB,	R | V, "SERVICE ACTION IN(12)" },
565	/* AC        O         ERASE(12) */
566	{ 0xAC,	O, "ERASE(12)" },
567	/* AC       O          GET PERFORMANCE */
568	{ 0xAC,	R, "GET PERFORMANCE" },
569	/* AD       O          READ DVD STRUCTURE */
570	{ 0xAD,	R, "READ DVD STRUCTURE" },
571	/* AE  O   O O         WRITE AND VERIFY(12) */
572	{ 0xAE,	D | W | O, "WRITE AND VERIFY(12)" },
573	/* AF  O   OZO         VERIFY(12) */
574	{ 0xAF,	D | W | R | O, "VERIFY(12)" },
575	/* B0      ZZZ         SEARCH DATA HIGH(12) */
576	{ 0xB0,	W | R | O, "SEARCH DATA HIGH(12)" },
577	/* B1      ZZZ         SEARCH DATA EQUAL(12) */
578	{ 0xB1,	W | R | O, "SEARCH DATA EQUAL(12)" },
579	/* B2      ZZZ         SEARCH DATA LOW(12) */
580	{ 0xB2,	W | R | O, "SEARCH DATA LOW(12)" },
581	/* B3  Z   OZO         SET LIMITS(12) */
582	{ 0xB3,	D | W | R | O, "SET LIMITS(12)" },
583	/* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
584	{ 0xB4,	D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
585	/* B5  OO   O      O   SECURITY PROTOCOL OUT */
586	{ 0xB5,	D | T | R | V, "SECURITY PROTOCOL OUT" },
587	/* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
588	{ 0xB5,	M, "REQUEST VOLUME ELEMENT ADDRESS" },
589	/* B6         O        SEND VOLUME TAG */
590	{ 0xB6,	M, "SEND VOLUME TAG" },
591	/* B6       O          SET STREAMING */
592	{ 0xB6,	R, "SET STREAMING" },
593	/* B7  O     O         READ DEFECT DATA(12) */
594	{ 0xB7,	D | O, "READ DEFECT DATA(12)" },
595	/* B8   O  OZOM        READ ELEMENT STATUS */
596	{ 0xB8,	T | W | R | O | M, "READ ELEMENT STATUS" },
597	/* B9       O          READ CD MSF */
598	{ 0xB9,	R, "READ CD MSF" },
599	/* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
600	{ 0xBA,	D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
601	/* BA       O          SCAN */
602	{ 0xBA,	R, "SCAN" },
603	/* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
604	{ 0xBB,	D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
605	/* BB       O          SET CD SPEED */
606	{ 0xBB,	R, "SET CD SPEED" },
607	/* BC  O   O OOMO      SPARE (IN) */
608	{ 0xBC,	D | W | O | M | A | E, "SPARE (IN)" },
609	/* BD  O   O OOOO      SPARE (OUT) */
610	{ 0xBD,	D | W | O | M | A | E, "SPARE (OUT)" },
611	/* BD       O          MECHANISM STATUS */
612	{ 0xBD,	R, "MECHANISM STATUS" },
613	/* BE  O   O OOMO      VOLUME SET (IN) */
614	{ 0xBE,	D | W | O | M | A | E, "VOLUME SET (IN)" },
615	/* BE       O          READ CD */
616	{ 0xBE,	R, "READ CD" },
617	/* BF  O   O OOOO      VOLUME SET (OUT) */
618	{ 0xBF,	D | W | O | M | A | E, "VOLUME SET (OUT)" },
619	/* BF       O          SEND DVD STRUCTURE */
620	{ 0xBF,	R, "SEND DVD STRUCTURE" }
621};
622
623const char *
624scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
625{
626	caddr_t match;
627	int i, j;
628	u_int32_t opmask;
629	u_int16_t pd_type;
630	int       num_ops[2];
631	struct op_table_entry *table[2];
632	int num_tables;
633
634	/*
635	 * If we've got inquiry data, use it to determine what type of
636	 * device we're dealing with here.  Otherwise, assume direct
637	 * access.
638	 */
639	if (inq_data == NULL) {
640		pd_type = T_DIRECT;
641		match = NULL;
642	} else {
643		pd_type = SID_TYPE(inq_data);
644
645		match = cam_quirkmatch((caddr_t)inq_data,
646				       (caddr_t)scsi_op_quirk_table,
647				       nitems(scsi_op_quirk_table),
648				       sizeof(*scsi_op_quirk_table),
649				       scsi_inquiry_match);
650	}
651
652	if (match != NULL) {
653		table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
654		num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
655		table[1] = scsi_op_codes;
656		num_ops[1] = nitems(scsi_op_codes);
657		num_tables = 2;
658	} else {
659		/*
660		 * If this is true, we have a vendor specific opcode that
661		 * wasn't covered in the quirk table.
662		 */
663		if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
664			return("Vendor Specific Command");
665
666		table[0] = scsi_op_codes;
667		num_ops[0] = nitems(scsi_op_codes);
668		num_tables = 1;
669	}
670
671	/* RBC is 'Simplified' Direct Access Device */
672	if (pd_type == T_RBC)
673		pd_type = T_DIRECT;
674
675	/*
676	 * Host managed drives are direct access for the most part.
677	 */
678	if (pd_type == T_ZBC_HM)
679		pd_type = T_DIRECT;
680
681	/* Map NODEVICE to Direct Access Device to handle REPORT LUNS, etc. */
682	if (pd_type == T_NODEVICE)
683		pd_type = T_DIRECT;
684
685	opmask = 1 << pd_type;
686
687	for (j = 0; j < num_tables; j++) {
688		for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
689			if ((table[j][i].opcode == opcode)
690			 && ((table[j][i].opmask & opmask) != 0))
691				return(table[j][i].desc);
692		}
693	}
694
695	/*
696	 * If we can't find a match for the command in the table, we just
697	 * assume it's a vendor specifc command.
698	 */
699	return("Vendor Specific Command");
700
701}
702
703#else /* SCSI_NO_OP_STRINGS */
704
705const char *
706scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
707{
708	return("");
709}
710
711#endif
712
713#if !defined(SCSI_NO_SENSE_STRINGS)
714#define SST(asc, ascq, action, desc) \
715	asc, ascq, action, desc
716#else
717const char empty_string[] = "";
718
719#define SST(asc, ascq, action, desc) \
720	asc, ascq, action, empty_string
721#endif
722
723const struct sense_key_table_entry sense_key_table[] =
724{
725	{ SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
726	{ SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
727	{ SSD_KEY_NOT_READY, SS_RDEF, "NOT READY" },
728	{ SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
729	{ SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
730	{ SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
731	{ SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
732	{ SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
733	{ SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
734	{ SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
735	{ SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
736	{ SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
737	{ SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
738	{ SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
739	{ SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
740	{ SSD_KEY_COMPLETED, SS_NOP, "COMPLETED" }
741};
742
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		nitems(quantum_fireball_entries),
928		/*sense key entries*/NULL,
929		quantum_fireball_entries
930	},
931	{
932		/*
933		 * This Sony MO drive likes to return 0x04, 0x00 when it
934		 * isn't spun up.
935		 */
936		{T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
937		/*num_sense_keys*/0,
938		nitems(sony_mo_entries),
939		/*sense key entries*/NULL,
940		sony_mo_entries
941	},
942	{
943		/*
944		 * HGST vendor-specific error codes
945		 */
946		{T_DIRECT, SIP_MEDIA_FIXED, "HGST", "*", "*"},
947		/*num_sense_keys*/0,
948		nitems(hgst_entries),
949		/*sense key entries*/NULL,
950		hgst_entries
951	},
952	{
953		/*
954		 * SEAGATE vendor-specific error codes
955		 */
956		{T_DIRECT, SIP_MEDIA_FIXED, "SEAGATE", "*", "*"},
957		/*num_sense_keys*/0,
958		nitems(seagate_entries),
959		/*sense key entries*/NULL,
960		seagate_entries
961	}
962};
963
964const u_int sense_quirk_table_size = nitems(sense_quirk_table);
965
966static struct asc_table_entry asc_table[] = {
967	/*
968	 * From: http://www.t10.org/lists/asc-num.txt
969	 * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
970	 */
971	/*
972	 * File: ASC-NUM.TXT
973	 *
974	 * SCSI ASC/ASCQ Assignments
975	 * Numeric Sorted Listing
976	 * as of  8/12/15
977	 *
978	 * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
979	 * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
980	 * . L - PRINTER DEVICE (SSC)                           blank = reserved
981	 * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
982	 * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
983	 * .  . R - CD DEVICE (MMC)
984	 * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
985	 * .  .  .M - MEDIA CHANGER DEVICE (SMC)
986	 * .  .  . A - STORAGE ARRAY DEVICE (SCC)
987	 * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
988	 * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
989	 * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
990	 * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
991	 * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
992	 * DTLPWROMAEBKVF
993	 * ASC      ASCQ  Action
994	 * Description
995	 */
996	/* DTLPWROMAEBKVF */
997	{ SST(0x00, 0x00, SS_NOP,
998	    "No additional sense information") },
999	/*  T             */
1000	{ SST(0x00, 0x01, SS_RDEF,
1001	    "Filemark detected") },
1002	/*  T             */
1003	{ SST(0x00, 0x02, SS_RDEF,
1004	    "End-of-partition/medium detected") },
1005	/*  T             */
1006	{ SST(0x00, 0x03, SS_RDEF,
1007	    "Setmark detected") },
1008	/*  T             */
1009	{ SST(0x00, 0x04, SS_RDEF,
1010	    "Beginning-of-partition/medium detected") },
1011	/*  TL            */
1012	{ SST(0x00, 0x05, SS_RDEF,
1013	    "End-of-data detected") },
1014	/* DTLPWROMAEBKVF */
1015	{ SST(0x00, 0x06, SS_RDEF,
1016	    "I/O process terminated") },
1017	/*  T             */
1018	{ SST(0x00, 0x07, SS_RDEF,	/* XXX TBD */
1019	    "Programmable early warning detected") },
1020	/*      R         */
1021	{ SST(0x00, 0x11, SS_FATAL | EBUSY,
1022	    "Audio play operation in progress") },
1023	/*      R         */
1024	{ SST(0x00, 0x12, SS_NOP,
1025	    "Audio play operation paused") },
1026	/*      R         */
1027	{ SST(0x00, 0x13, SS_NOP,
1028	    "Audio play operation successfully completed") },
1029	/*      R         */
1030	{ SST(0x00, 0x14, SS_RDEF,
1031	    "Audio play operation stopped due to error") },
1032	/*      R         */
1033	{ SST(0x00, 0x15, SS_NOP,
1034	    "No current audio status to return") },
1035	/* DTLPWROMAEBKVF */
1036	{ SST(0x00, 0x16, SS_FATAL | EBUSY,
1037	    "Operation in progress") },
1038	/* DTL WROMAEBKVF */
1039	{ SST(0x00, 0x17, SS_RDEF,
1040	    "Cleaning requested") },
1041	/*  T             */
1042	{ SST(0x00, 0x18, SS_RDEF,	/* XXX TBD */
1043	    "Erase operation in progress") },
1044	/*  T             */
1045	{ SST(0x00, 0x19, SS_RDEF,	/* XXX TBD */
1046	    "Locate operation in progress") },
1047	/*  T             */
1048	{ SST(0x00, 0x1A, SS_RDEF,	/* XXX TBD */
1049	    "Rewind operation in progress") },
1050	/*  T             */
1051	{ SST(0x00, 0x1B, SS_RDEF,	/* XXX TBD */
1052	    "Set capacity operation in progress") },
1053	/*  T             */
1054	{ SST(0x00, 0x1C, SS_RDEF,	/* XXX TBD */
1055	    "Verify operation in progress") },
1056	/* DT        B    */
1057	{ SST(0x00, 0x1D, SS_NOP,
1058	    "ATA pass through information available") },
1059	/* DT   R MAEBKV  */
1060	{ SST(0x00, 0x1E, SS_RDEF,	/* XXX TBD */
1061	    "Conflicting SA creation request") },
1062	/* DT        B    */
1063	{ SST(0x00, 0x1F, SS_RDEF,	/* XXX TBD */
1064	    "Logical unit transitioning to another power condition") },
1065	/* DT P      B    */
1066	{ SST(0x00, 0x20, SS_NOP,
1067	    "Extended copy information available") },
1068	/* D              */
1069	{ SST(0x00, 0x21, SS_RDEF,	/* XXX TBD */
1070	    "Atomic command aborted due to ACA") },
1071	/* D   W O   BK   */
1072	{ SST(0x01, 0x00, SS_RDEF,
1073	    "No index/sector signal") },
1074	/* D   WRO   BK   */
1075	{ SST(0x02, 0x00, SS_RDEF,
1076	    "No seek complete") },
1077	/* DTL W O   BK   */
1078	{ SST(0x03, 0x00, SS_RDEF,
1079	    "Peripheral device write fault") },
1080	/*  T             */
1081	{ SST(0x03, 0x01, SS_RDEF,
1082	    "No write current") },
1083	/*  T             */
1084	{ SST(0x03, 0x02, SS_RDEF,
1085	    "Excessive write errors") },
1086	/* DTLPWROMAEBKVF */
1087	{ SST(0x04, 0x00, SS_RDEF,
1088	    "Logical unit not ready, cause not reportable") },
1089	/* DTLPWROMAEBKVF */
1090	{ SST(0x04, 0x01, SS_WAIT | EBUSY,
1091	    "Logical unit is in process of becoming ready") },
1092	/* DTLPWROMAEBKVF */
1093	{ SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
1094	    "Logical unit not ready, initializing command required") },
1095	/* DTLPWROMAEBKVF */
1096	{ SST(0x04, 0x03, SS_FATAL | ENXIO,
1097	    "Logical unit not ready, manual intervention required") },
1098	/* DTL  RO   B    */
1099	{ SST(0x04, 0x04, SS_FATAL | EBUSY,
1100	    "Logical unit not ready, format in progress") },
1101	/* DT  W O A BK F */
1102	{ SST(0x04, 0x05, SS_FATAL | EBUSY,
1103	    "Logical unit not ready, rebuild in progress") },
1104	/* DT  W O A BK   */
1105	{ SST(0x04, 0x06, SS_FATAL | EBUSY,
1106	    "Logical unit not ready, recalculation in progress") },
1107	/* DTLPWROMAEBKVF */
1108	{ SST(0x04, 0x07, SS_FATAL | EBUSY,
1109	    "Logical unit not ready, operation in progress") },
1110	/*      R         */
1111	{ SST(0x04, 0x08, SS_FATAL | EBUSY,
1112	    "Logical unit not ready, long write in progress") },
1113	/* DTLPWROMAEBKVF */
1114	{ SST(0x04, 0x09, SS_FATAL | EBUSY,
1115	    "Logical unit not ready, self-test in progress") },
1116	/* DTLPWROMAEBKVF */
1117	{ SST(0x04, 0x0A, SS_WAIT | ENXIO,
1118	    "Logical unit not accessible, asymmetric access state transition")},
1119	/* DTLPWROMAEBKVF */
1120	{ SST(0x04, 0x0B, SS_FATAL | ENXIO,
1121	    "Logical unit not accessible, target port in standby state") },
1122	/* DTLPWROMAEBKVF */
1123	{ SST(0x04, 0x0C, SS_FATAL | ENXIO,
1124	    "Logical unit not accessible, target port in unavailable state") },
1125	/*              F */
1126	{ SST(0x04, 0x0D, SS_RDEF,	/* XXX TBD */
1127	    "Logical unit not ready, structure check required") },
1128	/* DTL WR MAEBKVF */
1129	{ SST(0x04, 0x0E, SS_RDEF,	/* XXX TBD */
1130	    "Logical unit not ready, security session in progress") },
1131	/* DT  WROM  B    */
1132	{ SST(0x04, 0x10, SS_FATAL | ENODEV,
1133	    "Logical unit not ready, auxiliary memory not accessible") },
1134	/* DT  WRO AEB VF */
1135	{ SST(0x04, 0x11, SS_WAIT | ENXIO,
1136	    "Logical unit not ready, notify (enable spinup) required") },
1137	/*        M    V  */
1138	{ SST(0x04, 0x12, SS_FATAL | ENXIO,
1139	    "Logical unit not ready, offline") },
1140	/* DT   R MAEBKV  */
1141	{ SST(0x04, 0x13, SS_WAIT | EBUSY,
1142	    "Logical unit not ready, SA creation in progress") },
1143	/* D         B    */
1144	{ SST(0x04, 0x14, SS_WAIT | ENOSPC,
1145	    "Logical unit not ready, space allocation in progress") },
1146	/*        M       */
1147	{ SST(0x04, 0x15, SS_FATAL | ENXIO,
1148	    "Logical unit not ready, robotics disabled") },
1149	/*        M       */
1150	{ SST(0x04, 0x16, SS_FATAL | ENXIO,
1151	    "Logical unit not ready, configuration required") },
1152	/*        M       */
1153	{ SST(0x04, 0x17, SS_FATAL | ENXIO,
1154	    "Logical unit not ready, calibration required") },
1155	/*        M       */
1156	{ SST(0x04, 0x18, SS_FATAL | ENXIO,
1157	    "Logical unit not ready, a door is open") },
1158	/*        M       */
1159	{ SST(0x04, 0x19, SS_FATAL | ENODEV,
1160	    "Logical unit not ready, operating in sequential mode") },
1161	/* DT        B    */
1162	{ SST(0x04, 0x1A, SS_WAIT | EBUSY,
1163	    "Logical unit not ready, START/STOP UNIT command in progress") },
1164	/* D         B    */
1165	{ SST(0x04, 0x1B, SS_WAIT | EBUSY,
1166	    "Logical unit not ready, sanitize in progress") },
1167	/* DT     MAEB    */
1168	{ SST(0x04, 0x1C, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
1169	    "Logical unit not ready, additional power use not yet granted") },
1170	/* D              */
1171	{ SST(0x04, 0x1D, SS_WAIT | EBUSY,
1172	    "Logical unit not ready, configuration in progress") },
1173	/* D              */
1174	{ SST(0x04, 0x1E, SS_FATAL | ENXIO,
1175	    "Logical unit not ready, microcode activation required") },
1176	/* DTLPWROMAEBKVF */
1177	{ SST(0x04, 0x1F, SS_FATAL | ENXIO,
1178	    "Logical unit not ready, microcode download required") },
1179	/* DTLPWROMAEBKVF */
1180	{ SST(0x04, 0x20, SS_FATAL | ENXIO,
1181	    "Logical unit not ready, logical unit reset required") },
1182	/* DTLPWROMAEBKVF */
1183	{ SST(0x04, 0x21, SS_FATAL | ENXIO,
1184	    "Logical unit not ready, hard reset required") },
1185	/* DTLPWROMAEBKVF */
1186	{ SST(0x04, 0x22, SS_FATAL | ENXIO,
1187	    "Logical unit not ready, power cycle required") },
1188	/* D              */
1189	{ SST(0x04, 0x23, SS_FATAL | ENXIO,
1190	    "Logical unit not ready, affiliation required") },
1191	/* D              */
1192	{ SST(0x04, 0x24, SS_FATAL | EBUSY,
1193	    "Depopulation in progress") },
1194	/* DTL WROMAEBKVF */
1195	{ SST(0x05, 0x00, SS_RDEF,
1196	    "Logical unit does not respond to selection") },
1197	/* D   WROM  BK   */
1198	{ SST(0x06, 0x00, SS_RDEF,
1199	    "No reference position found") },
1200	/* DTL WROM  BK   */
1201	{ SST(0x07, 0x00, SS_RDEF,
1202	    "Multiple peripheral devices selected") },
1203	/* DTL WROMAEBKVF */
1204	{ SST(0x08, 0x00, SS_RDEF,
1205	    "Logical unit communication failure") },
1206	/* DTL WROMAEBKVF */
1207	{ SST(0x08, 0x01, SS_RDEF,
1208	    "Logical unit communication time-out") },
1209	/* DTL WROMAEBKVF */
1210	{ SST(0x08, 0x02, SS_RDEF,
1211	    "Logical unit communication parity error") },
1212	/* DT   ROM  BK   */
1213	{ SST(0x08, 0x03, SS_RDEF,
1214	    "Logical unit communication CRC error (Ultra-DMA/32)") },
1215	/* DTLPWRO    K   */
1216	{ SST(0x08, 0x04, SS_RDEF,	/* XXX TBD */
1217	    "Unreachable copy target") },
1218	/* DT  WRO   B    */
1219	{ SST(0x09, 0x00, SS_RDEF,
1220	    "Track following error") },
1221	/*     WRO    K   */
1222	{ SST(0x09, 0x01, SS_RDEF,
1223	    "Tracking servo failure") },
1224	/*     WRO    K   */
1225	{ SST(0x09, 0x02, SS_RDEF,
1226	    "Focus servo failure") },
1227	/*     WRO        */
1228	{ SST(0x09, 0x03, SS_RDEF,
1229	    "Spindle servo failure") },
1230	/* DT  WRO   B    */
1231	{ SST(0x09, 0x04, SS_RDEF,
1232	    "Head select fault") },
1233	/* DT   RO   B    */
1234	{ SST(0x09, 0x05, SS_RDEF,
1235	    "Vibration induced tracking error") },
1236	/* DTLPWROMAEBKVF */
1237	{ SST(0x0A, 0x00, SS_FATAL | ENOSPC,
1238	    "Error log overflow") },
1239	/* DTLPWROMAEBKVF */
1240	{ SST(0x0B, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1241	    "Warning") },
1242	/* DTLPWROMAEBKVF */
1243	{ SST(0x0B, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1244	    "Warning - specified temperature exceeded") },
1245	/* DTLPWROMAEBKVF */
1246	{ SST(0x0B, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1247	    "Warning - enclosure degraded") },
1248	/* DTLPWROMAEBKVF */
1249	{ SST(0x0B, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1250	    "Warning - background self-test failed") },
1251	/* DTLPWRO AEBKVF */
1252	{ SST(0x0B, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1253	    "Warning - background pre-scan detected medium error") },
1254	/* DTLPWRO AEBKVF */
1255	{ SST(0x0B, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1256	    "Warning - background medium scan detected medium error") },
1257	/* DTLPWROMAEBKVF */
1258	{ SST(0x0B, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1259	    "Warning - non-volatile cache now volatile") },
1260	/* DTLPWROMAEBKVF */
1261	{ SST(0x0B, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1262	    "Warning - degraded power to non-volatile cache") },
1263	/* DTLPWROMAEBKVF */
1264	{ SST(0x0B, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1265	    "Warning - power loss expected") },
1266	/* D              */
1267	{ SST(0x0B, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1268	    "Warning - device statistics notification available") },
1269	/* DTLPWROMAEBKVF */
1270	{ SST(0x0B, 0x0A, SS_NOP | SSQ_PRINT_SENSE,
1271	    "Warning - High critical temperature limit exceeded") },
1272	/* DTLPWROMAEBKVF */
1273	{ SST(0x0B, 0x0B, SS_NOP | SSQ_PRINT_SENSE,
1274	    "Warning - Low critical temperature limit exceeded") },
1275	/* DTLPWROMAEBKVF */
1276	{ SST(0x0B, 0x0C, SS_NOP | SSQ_PRINT_SENSE,
1277	    "Warning - High operating temperature limit exceeded") },
1278	/* DTLPWROMAEBKVF */
1279	{ SST(0x0B, 0x0D, SS_NOP | SSQ_PRINT_SENSE,
1280	    "Warning - Low operating temperature limit exceeded") },
1281	/* DTLPWROMAEBKVF */
1282	{ SST(0x0B, 0x0E, SS_NOP | SSQ_PRINT_SENSE,
1283	    "Warning - High citical humidity limit exceeded") },
1284	/* DTLPWROMAEBKVF */
1285	{ SST(0x0B, 0x0F, SS_NOP | SSQ_PRINT_SENSE,
1286	    "Warning - Low citical humidity limit exceeded") },
1287	/* DTLPWROMAEBKVF */
1288	{ SST(0x0B, 0x10, SS_NOP | SSQ_PRINT_SENSE,
1289	    "Warning - High operating humidity limit exceeded") },
1290	/* DTLPWROMAEBKVF */
1291	{ SST(0x0B, 0x11, SS_NOP | SSQ_PRINT_SENSE,
1292	    "Warning - Low operating humidity limit exceeded") },
1293	/*  T   R         */
1294	{ SST(0x0C, 0x00, SS_RDEF,
1295	    "Write error") },
1296	/*            K   */
1297	{ SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1298	    "Write error - recovered with auto reallocation") },
1299	/* D   W O   BK   */
1300	{ SST(0x0C, 0x02, SS_RDEF,
1301	    "Write error - auto reallocation failed") },
1302	/* D   W O   BK   */
1303	{ SST(0x0C, 0x03, SS_RDEF,
1304	    "Write error - recommend reassignment") },
1305	/* DT  W O   B    */
1306	{ SST(0x0C, 0x04, SS_RDEF,
1307	    "Compression check miscompare error") },
1308	/* DT  W O   B    */
1309	{ SST(0x0C, 0x05, SS_RDEF,
1310	    "Data expansion occurred during compression") },
1311	/* DT  W O   B    */
1312	{ SST(0x0C, 0x06, SS_RDEF,
1313	    "Block not compressible") },
1314	/*      R         */
1315	{ SST(0x0C, 0x07, SS_RDEF,
1316	    "Write error - recovery needed") },
1317	/*      R         */
1318	{ SST(0x0C, 0x08, SS_RDEF,
1319	    "Write error - recovery failed") },
1320	/*      R         */
1321	{ SST(0x0C, 0x09, SS_RDEF,
1322	    "Write error - loss of streaming") },
1323	/*      R         */
1324	{ SST(0x0C, 0x0A, SS_RDEF,
1325	    "Write error - padding blocks added") },
1326	/* DT  WROM  B    */
1327	{ SST(0x0C, 0x0B, SS_RDEF,	/* XXX TBD */
1328	    "Auxiliary memory write error") },
1329	/* DTLPWRO AEBKVF */
1330	{ SST(0x0C, 0x0C, SS_RDEF,	/* XXX TBD */
1331	    "Write error - unexpected unsolicited data") },
1332	/* DTLPWRO AEBKVF */
1333	{ SST(0x0C, 0x0D, SS_RDEF,	/* XXX TBD */
1334	    "Write error - not enough unsolicited data") },
1335	/* DT  W O   BK   */
1336	{ SST(0x0C, 0x0E, SS_RDEF,	/* XXX TBD */
1337	    "Multiple write errors") },
1338	/*      R         */
1339	{ SST(0x0C, 0x0F, SS_RDEF,	/* XXX TBD */
1340	    "Defects in error window") },
1341	/* D              */
1342	{ SST(0x0C, 0x10, SS_RDEF,	/* XXX TBD */
1343	    "Incomplete multiple atomic write operations") },
1344	/* D              */
1345	{ SST(0x0C, 0x11, SS_RDEF,	/* XXX TBD */
1346	    "Write error - recovery scan needed") },
1347	/* D              */
1348	{ SST(0x0C, 0x12, SS_RDEF,	/* XXX TBD */
1349	    "Write error - insufficient zone resources") },
1350	/* DTLPWRO A  K   */
1351	{ SST(0x0D, 0x00, SS_RDEF,	/* XXX TBD */
1352	    "Error detected by third party temporary initiator") },
1353	/* DTLPWRO A  K   */
1354	{ SST(0x0D, 0x01, SS_RDEF,	/* XXX TBD */
1355	    "Third party device failure") },
1356	/* DTLPWRO A  K   */
1357	{ SST(0x0D, 0x02, SS_RDEF,	/* XXX TBD */
1358	    "Copy target device not reachable") },
1359	/* DTLPWRO A  K   */
1360	{ SST(0x0D, 0x03, SS_RDEF,	/* XXX TBD */
1361	    "Incorrect copy target device type") },
1362	/* DTLPWRO A  K   */
1363	{ SST(0x0D, 0x04, SS_RDEF,	/* XXX TBD */
1364	    "Copy target device data underrun") },
1365	/* DTLPWRO A  K   */
1366	{ SST(0x0D, 0x05, SS_RDEF,	/* XXX TBD */
1367	    "Copy target device data overrun") },
1368	/* DT PWROMAEBK F */
1369	{ SST(0x0E, 0x00, SS_RDEF,	/* XXX TBD */
1370	    "Invalid information unit") },
1371	/* DT PWROMAEBK F */
1372	{ SST(0x0E, 0x01, SS_RDEF,	/* XXX TBD */
1373	    "Information unit too short") },
1374	/* DT PWROMAEBK F */
1375	{ SST(0x0E, 0x02, SS_RDEF,	/* XXX TBD */
1376	    "Information unit too long") },
1377	/* DT P R MAEBK F */
1378	{ SST(0x0E, 0x03, SS_FATAL | EINVAL,
1379	    "Invalid field in command information unit") },
1380	/* D   W O   BK   */
1381	{ SST(0x10, 0x00, SS_RDEF,
1382	    "ID CRC or ECC error") },
1383	/* DT  W O        */
1384	{ SST(0x10, 0x01, SS_RDEF,	/* XXX TBD */
1385	    "Logical block guard check failed") },
1386	/* DT  W O        */
1387	{ SST(0x10, 0x02, SS_RDEF,	/* XXX TBD */
1388	    "Logical block application tag check failed") },
1389	/* DT  W O        */
1390	{ SST(0x10, 0x03, SS_RDEF,	/* XXX TBD */
1391	    "Logical block reference tag check failed") },
1392	/*  T             */
1393	{ SST(0x10, 0x04, SS_RDEF,	/* XXX TBD */
1394	    "Logical block protection error on recovered buffer data") },
1395	/*  T             */
1396	{ SST(0x10, 0x05, SS_RDEF,	/* XXX TBD */
1397	    "Logical block protection method error") },
1398	/* DT  WRO   BK   */
1399	{ SST(0x11, 0x00, SS_FATAL|EIO,
1400	    "Unrecovered read error") },
1401	/* DT  WRO   BK   */
1402	{ SST(0x11, 0x01, SS_FATAL|EIO,
1403	    "Read retries exhausted") },
1404	/* DT  WRO   BK   */
1405	{ SST(0x11, 0x02, SS_FATAL|EIO,
1406	    "Error too long to correct") },
1407	/* DT  W O   BK   */
1408	{ SST(0x11, 0x03, SS_FATAL|EIO,
1409	    "Multiple read errors") },
1410	/* D   W O   BK   */
1411	{ SST(0x11, 0x04, SS_FATAL|EIO,
1412	    "Unrecovered read error - auto reallocate failed") },
1413	/*     WRO   B    */
1414	{ SST(0x11, 0x05, SS_FATAL|EIO,
1415	    "L-EC uncorrectable error") },
1416	/*     WRO   B    */
1417	{ SST(0x11, 0x06, SS_FATAL|EIO,
1418	    "CIRC unrecovered error") },
1419	/*     W O   B    */
1420	{ SST(0x11, 0x07, SS_RDEF,
1421	    "Data re-synchronization error") },
1422	/*  T             */
1423	{ SST(0x11, 0x08, SS_RDEF,
1424	    "Incomplete block read") },
1425	/*  T             */
1426	{ SST(0x11, 0x09, SS_RDEF,
1427	    "No gap found") },
1428	/* DT    O   BK   */
1429	{ SST(0x11, 0x0A, SS_RDEF,
1430	    "Miscorrected error") },
1431	/* D   W O   BK   */
1432	{ SST(0x11, 0x0B, SS_FATAL|EIO,
1433	    "Unrecovered read error - recommend reassignment") },
1434	/* D   W O   BK   */
1435	{ SST(0x11, 0x0C, SS_FATAL|EIO,
1436	    "Unrecovered read error - recommend rewrite the data") },
1437	/* DT  WRO   B    */
1438	{ SST(0x11, 0x0D, SS_RDEF,
1439	    "De-compression CRC error") },
1440	/* DT  WRO   B    */
1441	{ SST(0x11, 0x0E, SS_RDEF,
1442	    "Cannot decompress using declared algorithm") },
1443	/*      R         */
1444	{ SST(0x11, 0x0F, SS_RDEF,
1445	    "Error reading UPC/EAN number") },
1446	/*      R         */
1447	{ SST(0x11, 0x10, SS_RDEF,
1448	    "Error reading ISRC number") },
1449	/*      R         */
1450	{ SST(0x11, 0x11, SS_RDEF,
1451	    "Read error - loss of streaming") },
1452	/* DT  WROM  B    */
1453	{ SST(0x11, 0x12, SS_RDEF,	/* XXX TBD */
1454	    "Auxiliary memory read error") },
1455	/* DTLPWRO AEBKVF */
1456	{ SST(0x11, 0x13, SS_RDEF,	/* XXX TBD */
1457	    "Read error - failed retransmission request") },
1458	/* D              */
1459	{ SST(0x11, 0x14, SS_RDEF,	/* XXX TBD */
1460	    "Read error - LBA marked bad by application client") },
1461	/* D              */
1462	{ SST(0x11, 0x15, SS_FATAL | EIO,
1463	    "Write after sanitize required") },
1464	/* D   W O   BK   */
1465	{ SST(0x12, 0x00, SS_RDEF,
1466	    "Address mark not found for ID field") },
1467	/* D   W O   BK   */
1468	{ SST(0x13, 0x00, SS_RDEF,
1469	    "Address mark not found for data field") },
1470	/* DTL WRO   BK   */
1471	{ SST(0x14, 0x00, SS_RDEF,
1472	    "Recorded entity not found") },
1473	/* DT  WRO   BK   */
1474	{ SST(0x14, 0x01, SS_RDEF,
1475	    "Record not found") },
1476	/*  T             */
1477	{ SST(0x14, 0x02, SS_RDEF,
1478	    "Filemark or setmark not found") },
1479	/*  T             */
1480	{ SST(0x14, 0x03, SS_RDEF,
1481	    "End-of-data not found") },
1482	/*  T             */
1483	{ SST(0x14, 0x04, SS_RDEF,
1484	    "Block sequence error") },
1485	/* DT  W O   BK   */
1486	{ SST(0x14, 0x05, SS_RDEF,
1487	    "Record not found - recommend reassignment") },
1488	/* DT  W O   BK   */
1489	{ SST(0x14, 0x06, SS_RDEF,
1490	    "Record not found - data auto-reallocated") },
1491	/*  T             */
1492	{ SST(0x14, 0x07, SS_RDEF,	/* XXX TBD */
1493	    "Locate operation failure") },
1494	/* DTL WROM  BK   */
1495	{ SST(0x15, 0x00, SS_RDEF,
1496	    "Random positioning error") },
1497	/* DTL WROM  BK   */
1498	{ SST(0x15, 0x01, SS_RDEF,
1499	    "Mechanical positioning error") },
1500	/* DT  WRO   BK   */
1501	{ SST(0x15, 0x02, SS_RDEF,
1502	    "Positioning error detected by read of medium") },
1503	/* D   W O   BK   */
1504	{ SST(0x16, 0x00, SS_RDEF,
1505	    "Data synchronization mark error") },
1506	/* D   W O   BK   */
1507	{ SST(0x16, 0x01, SS_RDEF,
1508	    "Data sync error - data rewritten") },
1509	/* D   W O   BK   */
1510	{ SST(0x16, 0x02, SS_RDEF,
1511	    "Data sync error - recommend rewrite") },
1512	/* D   W O   BK   */
1513	{ SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1514	    "Data sync error - data auto-reallocated") },
1515	/* D   W O   BK   */
1516	{ SST(0x16, 0x04, SS_RDEF,
1517	    "Data sync error - recommend reassignment") },
1518	/* DT  WRO   BK   */
1519	{ SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1520	    "Recovered data with no error correction applied") },
1521	/* DT  WRO   BK   */
1522	{ SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1523	    "Recovered data with retries") },
1524	/* DT  WRO   BK   */
1525	{ SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1526	    "Recovered data with positive head offset") },
1527	/* DT  WRO   BK   */
1528	{ SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1529	    "Recovered data with negative head offset") },
1530	/*     WRO   B    */
1531	{ SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1532	    "Recovered data with retries and/or CIRC applied") },
1533	/* D   WRO   BK   */
1534	{ SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1535	    "Recovered data using previous sector ID") },
1536	/* D   W O   BK   */
1537	{ SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1538	    "Recovered data without ECC - data auto-reallocated") },
1539	/* D   WRO   BK   */
1540	{ SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1541	    "Recovered data without ECC - recommend reassignment") },
1542	/* D   WRO   BK   */
1543	{ SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1544	    "Recovered data without ECC - recommend rewrite") },
1545	/* D   WRO   BK   */
1546	{ SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1547	    "Recovered data without ECC - data rewritten") },
1548	/* DT  WRO   BK   */
1549	{ SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1550	    "Recovered data with error correction applied") },
1551	/* D   WRO   BK   */
1552	{ SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1553	    "Recovered data with error corr. & retries applied") },
1554	/* D   WRO   BK   */
1555	{ SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1556	    "Recovered data - data auto-reallocated") },
1557	/*      R         */
1558	{ SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1559	    "Recovered data with CIRC") },
1560	/*      R         */
1561	{ SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1562	    "Recovered data with L-EC") },
1563	/* D   WRO   BK   */
1564	{ SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1565	    "Recovered data - recommend reassignment") },
1566	/* D   WRO   BK   */
1567	{ SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1568	    "Recovered data - recommend rewrite") },
1569	/* D   W O   BK   */
1570	{ SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1571	    "Recovered data with ECC - data rewritten") },
1572	/*      R         */
1573	{ SST(0x18, 0x08, SS_RDEF,	/* XXX TBD */
1574	    "Recovered data with linking") },
1575	/* D     O    K   */
1576	{ SST(0x19, 0x00, SS_RDEF,
1577	    "Defect list error") },
1578	/* D     O    K   */
1579	{ SST(0x19, 0x01, SS_RDEF,
1580	    "Defect list not available") },
1581	/* D     O    K   */
1582	{ SST(0x19, 0x02, SS_RDEF,
1583	    "Defect list error in primary list") },
1584	/* D     O    K   */
1585	{ SST(0x19, 0x03, SS_RDEF,
1586	    "Defect list error in grown list") },
1587	/* DTLPWROMAEBKVF */
1588	{ SST(0x1A, 0x00, SS_RDEF,
1589	    "Parameter list length error") },
1590	/* DTLPWROMAEBKVF */
1591	{ SST(0x1B, 0x00, SS_RDEF,
1592	    "Synchronous data transfer error") },
1593	/* D     O   BK   */
1594	{ SST(0x1C, 0x00, SS_RDEF,
1595	    "Defect list not found") },
1596	/* D     O   BK   */
1597	{ SST(0x1C, 0x01, SS_RDEF,
1598	    "Primary defect list not found") },
1599	/* D     O   BK   */
1600	{ SST(0x1C, 0x02, SS_RDEF,
1601	    "Grown defect list not found") },
1602	/* DT  WRO   BK   */
1603	{ SST(0x1D, 0x00, SS_FATAL,
1604	    "Miscompare during verify operation") },
1605	/* D         B    */
1606	{ SST(0x1D, 0x01, SS_RDEF,	/* XXX TBD */
1607	    "Miscomparable verify of unmapped LBA") },
1608	/* D   W O   BK   */
1609	{ SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1610	    "Recovered ID with ECC correction") },
1611	/* D     O    K   */
1612	{ SST(0x1F, 0x00, SS_RDEF,
1613	    "Partial defect list transfer") },
1614	/* DTLPWROMAEBKVF */
1615	{ SST(0x20, 0x00, SS_FATAL | EINVAL,
1616	    "Invalid command operation code") },
1617	/* DT PWROMAEBK   */
1618	{ SST(0x20, 0x01, SS_RDEF,	/* XXX TBD */
1619	    "Access denied - initiator pending-enrolled") },
1620	/* DT PWROMAEBK   */
1621	{ SST(0x20, 0x02, SS_FATAL | EPERM,
1622	    "Access denied - no access rights") },
1623	/* DT PWROMAEBK   */
1624	{ SST(0x20, 0x03, SS_RDEF,	/* XXX TBD */
1625	    "Access denied - invalid mgmt ID key") },
1626	/*  T             */
1627	{ SST(0x20, 0x04, SS_RDEF,	/* XXX TBD */
1628	    "Illegal command while in write capable state") },
1629	/*  T             */
1630	{ SST(0x20, 0x05, SS_RDEF,	/* XXX TBD */
1631	    "Obsolete") },
1632	/*  T             */
1633	{ SST(0x20, 0x06, SS_RDEF,	/* XXX TBD */
1634	    "Illegal command while in explicit address mode") },
1635	/*  T             */
1636	{ SST(0x20, 0x07, SS_RDEF,	/* XXX TBD */
1637	    "Illegal command while in implicit address mode") },
1638	/* DT PWROMAEBK   */
1639	{ SST(0x20, 0x08, SS_RDEF,	/* XXX TBD */
1640	    "Access denied - enrollment conflict") },
1641	/* DT PWROMAEBK   */
1642	{ SST(0x20, 0x09, SS_RDEF,	/* XXX TBD */
1643	    "Access denied - invalid LU identifier") },
1644	/* DT PWROMAEBK   */
1645	{ SST(0x20, 0x0A, SS_RDEF,	/* XXX TBD */
1646	    "Access denied - invalid proxy token") },
1647	/* DT PWROMAEBK   */
1648	{ SST(0x20, 0x0B, SS_RDEF,	/* XXX TBD */
1649	    "Access denied - ACL LUN conflict") },
1650	/*  T             */
1651	{ SST(0x20, 0x0C, SS_FATAL | EINVAL,
1652	    "Illegal command when not in append-only mode") },
1653	/* DT  WRO   BK   */
1654	{ SST(0x21, 0x00, SS_FATAL | EINVAL,
1655	    "Logical block address out of range") },
1656	/* DT  WROM  BK   */
1657	{ SST(0x21, 0x01, SS_FATAL | EINVAL,
1658	    "Invalid element address") },
1659	/*      R         */
1660	{ SST(0x21, 0x02, SS_RDEF,	/* XXX TBD */
1661	    "Invalid address for write") },
1662	/*      R         */
1663	{ SST(0x21, 0x03, SS_RDEF,	/* XXX TBD */
1664	    "Invalid write crossing layer jump") },
1665	/* D              */
1666	{ SST(0x21, 0x04, SS_RDEF,	/* XXX TBD */
1667	    "Unaligned write command") },
1668	/* D              */
1669	{ SST(0x21, 0x05, SS_RDEF,	/* XXX TBD */
1670	    "Write boundary violation") },
1671	/* D              */
1672	{ SST(0x21, 0x06, SS_RDEF,	/* XXX TBD */
1673	    "Attempt to read invalid data") },
1674	/* D              */
1675	{ SST(0x21, 0x07, SS_RDEF,	/* XXX TBD */
1676	    "Read boundary violation") },
1677	/* D              */
1678	{ SST(0x22, 0x00, SS_FATAL | EINVAL,
1679	    "Illegal function (use 20 00, 24 00, or 26 00)") },
1680	/* DT P      B    */
1681	{ SST(0x23, 0x00, SS_FATAL | EINVAL,
1682	    "Invalid token operation, cause not reportable") },
1683	/* DT P      B    */
1684	{ SST(0x23, 0x01, SS_FATAL | EINVAL,
1685	    "Invalid token operation, unsupported token type") },
1686	/* DT P      B    */
1687	{ SST(0x23, 0x02, SS_FATAL | EINVAL,
1688	    "Invalid token operation, remote token usage not supported") },
1689	/* DT P      B    */
1690	{ SST(0x23, 0x03, SS_FATAL | EINVAL,
1691	    "Invalid token operation, remote ROD token creation not supported") },
1692	/* DT P      B    */
1693	{ SST(0x23, 0x04, SS_FATAL | EINVAL,
1694	    "Invalid token operation, token unknown") },
1695	/* DT P      B    */
1696	{ SST(0x23, 0x05, SS_FATAL | EINVAL,
1697	    "Invalid token operation, token corrupt") },
1698	/* DT P      B    */
1699	{ SST(0x23, 0x06, SS_FATAL | EINVAL,
1700	    "Invalid token operation, token revoked") },
1701	/* DT P      B    */
1702	{ SST(0x23, 0x07, SS_FATAL | EINVAL,
1703	    "Invalid token operation, token expired") },
1704	/* DT P      B    */
1705	{ SST(0x23, 0x08, SS_FATAL | EINVAL,
1706	    "Invalid token operation, token cancelled") },
1707	/* DT P      B    */
1708	{ SST(0x23, 0x09, SS_FATAL | EINVAL,
1709	    "Invalid token operation, token deleted") },
1710	/* DT P      B    */
1711	{ SST(0x23, 0x0A, SS_FATAL | EINVAL,
1712	    "Invalid token operation, invalid token length") },
1713	/* DTLPWROMAEBKVF */
1714	{ SST(0x24, 0x00, SS_FATAL | EINVAL,
1715	    "Invalid field in CDB") },
1716	/* DTLPWRO AEBKVF */
1717	{ SST(0x24, 0x01, SS_RDEF,	/* XXX TBD */
1718	    "CDB decryption error") },
1719	/*  T             */
1720	{ SST(0x24, 0x02, SS_RDEF,	/* XXX TBD */
1721	    "Obsolete") },
1722	/*  T             */
1723	{ SST(0x24, 0x03, SS_RDEF,	/* XXX TBD */
1724	    "Obsolete") },
1725	/*              F */
1726	{ SST(0x24, 0x04, SS_RDEF,	/* XXX TBD */
1727	    "Security audit value frozen") },
1728	/*              F */
1729	{ SST(0x24, 0x05, SS_RDEF,	/* XXX TBD */
1730	    "Security working key frozen") },
1731	/*              F */
1732	{ SST(0x24, 0x06, SS_RDEF,	/* XXX TBD */
1733	    "NONCE not unique") },
1734	/*              F */
1735	{ SST(0x24, 0x07, SS_RDEF,	/* XXX TBD */
1736	    "NONCE timestamp out of range") },
1737	/* DT   R MAEBKV  */
1738	{ SST(0x24, 0x08, SS_RDEF,	/* XXX TBD */
1739	    "Invalid XCDB") },
1740	/* DTLPWROMAEBKVF */
1741	{ SST(0x25, 0x00, SS_FATAL | ENXIO | SSQ_LOST,
1742	    "Logical unit not supported") },
1743	/* DTLPWROMAEBKVF */
1744	{ SST(0x26, 0x00, SS_FATAL | EINVAL,
1745	    "Invalid field in parameter list") },
1746	/* DTLPWROMAEBKVF */
1747	{ SST(0x26, 0x01, SS_FATAL | EINVAL,
1748	    "Parameter not supported") },
1749	/* DTLPWROMAEBKVF */
1750	{ SST(0x26, 0x02, SS_FATAL | EINVAL,
1751	    "Parameter value invalid") },
1752	/* DTLPWROMAE K   */
1753	{ SST(0x26, 0x03, SS_FATAL | EINVAL,
1754	    "Threshold parameters not supported") },
1755	/* DTLPWROMAEBKVF */
1756	{ SST(0x26, 0x04, SS_FATAL | EINVAL,
1757	    "Invalid release of persistent reservation") },
1758	/* DTLPWRO A BK   */
1759	{ SST(0x26, 0x05, SS_RDEF,	/* XXX TBD */
1760	    "Data decryption error") },
1761	/* DTLPWRO    K   */
1762	{ SST(0x26, 0x06, SS_FATAL | EINVAL,
1763	    "Too many target descriptors") },
1764	/* DTLPWRO    K   */
1765	{ SST(0x26, 0x07, SS_FATAL | EINVAL,
1766	    "Unsupported target descriptor type code") },
1767	/* DTLPWRO    K   */
1768	{ SST(0x26, 0x08, SS_FATAL | EINVAL,
1769	    "Too many segment descriptors") },
1770	/* DTLPWRO    K   */
1771	{ SST(0x26, 0x09, SS_FATAL | EINVAL,
1772	    "Unsupported segment descriptor type code") },
1773	/* DTLPWRO    K   */
1774	{ SST(0x26, 0x0A, SS_FATAL | EINVAL,
1775	    "Unexpected inexact segment") },
1776	/* DTLPWRO    K   */
1777	{ SST(0x26, 0x0B, SS_FATAL | EINVAL,
1778	    "Inline data length exceeded") },
1779	/* DTLPWRO    K   */
1780	{ SST(0x26, 0x0C, SS_FATAL | EINVAL,
1781	    "Invalid operation for copy source or destination") },
1782	/* DTLPWRO    K   */
1783	{ SST(0x26, 0x0D, SS_FATAL | EINVAL,
1784	    "Copy segment granularity violation") },
1785	/* DT PWROMAEBK   */
1786	{ SST(0x26, 0x0E, SS_RDEF,	/* XXX TBD */
1787	    "Invalid parameter while port is enabled") },
1788	/*              F */
1789	{ SST(0x26, 0x0F, SS_RDEF,	/* XXX TBD */
1790	    "Invalid data-out buffer integrity check value") },
1791	/*  T             */
1792	{ SST(0x26, 0x10, SS_RDEF,	/* XXX TBD */
1793	    "Data decryption key fail limit reached") },
1794	/*  T             */
1795	{ SST(0x26, 0x11, SS_RDEF,	/* XXX TBD */
1796	    "Incomplete key-associated data set") },
1797	/*  T             */
1798	{ SST(0x26, 0x12, SS_RDEF,	/* XXX TBD */
1799	    "Vendor specific key reference not found") },
1800	/* D              */
1801	{ SST(0x26, 0x13, SS_RDEF,	/* XXX TBD */
1802	    "Application tag mode page is invalid") },
1803	/* DT  WRO   BK   */
1804	{ SST(0x27, 0x00, SS_FATAL | EACCES,
1805	    "Write protected") },
1806	/* DT  WRO   BK   */
1807	{ SST(0x27, 0x01, SS_FATAL | EACCES,
1808	    "Hardware write protected") },
1809	/* DT  WRO   BK   */
1810	{ SST(0x27, 0x02, SS_FATAL | EACCES,
1811	    "Logical unit software write protected") },
1812	/*  T   R         */
1813	{ SST(0x27, 0x03, SS_FATAL | EACCES,
1814	    "Associated write protect") },
1815	/*  T   R         */
1816	{ SST(0x27, 0x04, SS_FATAL | EACCES,
1817	    "Persistent write protect") },
1818	/*  T   R         */
1819	{ SST(0x27, 0x05, SS_FATAL | EACCES,
1820	    "Permanent write protect") },
1821	/*      R       F */
1822	{ SST(0x27, 0x06, SS_RDEF,	/* XXX TBD */
1823	    "Conditional write protect") },
1824	/* D         B    */
1825	{ SST(0x27, 0x07, SS_FATAL | ENOSPC,
1826	    "Space allocation failed write protect") },
1827	/* D              */
1828	{ SST(0x27, 0x08, SS_FATAL | EACCES,
1829	    "Zone is read only") },
1830	/* DTLPWROMAEBKVF */
1831	{ SST(0x28, 0x00, SS_FATAL | ENXIO,
1832	    "Not ready to ready change, medium may have changed") },
1833	/* DT  WROM  B    */
1834	{ SST(0x28, 0x01, SS_FATAL | ENXIO,
1835	    "Import or export element accessed") },
1836	/*      R         */
1837	{ SST(0x28, 0x02, SS_RDEF,	/* XXX TBD */
1838	    "Format-layer may have changed") },
1839	/*        M       */
1840	{ SST(0x28, 0x03, SS_RDEF,	/* XXX TBD */
1841	    "Import/export element accessed, medium changed") },
1842	/*
1843	 * XXX JGibbs - All of these should use the same errno, but I don't
1844	 * think ENXIO is the correct choice.  Should we borrow from
1845	 * the networking errnos?  ECONNRESET anyone?
1846	 */
1847	/* DTLPWROMAEBKVF */
1848	{ SST(0x29, 0x00, SS_FATAL | ENXIO,
1849	    "Power on, reset, or bus device reset occurred") },
1850	/* DTLPWROMAEBKVF */
1851	{ SST(0x29, 0x01, SS_RDEF,
1852	    "Power on occurred") },
1853	/* DTLPWROMAEBKVF */
1854	{ SST(0x29, 0x02, SS_RDEF,
1855	    "SCSI bus reset occurred") },
1856	/* DTLPWROMAEBKVF */
1857	{ SST(0x29, 0x03, SS_RDEF,
1858	    "Bus device reset function occurred") },
1859	/* DTLPWROMAEBKVF */
1860	{ SST(0x29, 0x04, SS_RDEF,
1861	    "Device internal reset") },
1862	/* DTLPWROMAEBKVF */
1863	{ SST(0x29, 0x05, SS_RDEF,
1864	    "Transceiver mode changed to single-ended") },
1865	/* DTLPWROMAEBKVF */
1866	{ SST(0x29, 0x06, SS_RDEF,
1867	    "Transceiver mode changed to LVD") },
1868	/* DTLPWROMAEBKVF */
1869	{ SST(0x29, 0x07, SS_RDEF,	/* XXX TBD */
1870	    "I_T nexus loss occurred") },
1871	/* DTL WROMAEBKVF */
1872	{ SST(0x2A, 0x00, SS_RDEF,
1873	    "Parameters changed") },
1874	/* DTL WROMAEBKVF */
1875	{ SST(0x2A, 0x01, SS_RDEF,
1876	    "Mode parameters changed") },
1877	/* DTL WROMAE K   */
1878	{ SST(0x2A, 0x02, SS_RDEF,
1879	    "Log parameters changed") },
1880	/* DTLPWROMAE K   */
1881	{ SST(0x2A, 0x03, SS_RDEF,
1882	    "Reservations preempted") },
1883	/* DTLPWROMAE     */
1884	{ SST(0x2A, 0x04, SS_RDEF,	/* XXX TBD */
1885	    "Reservations released") },
1886	/* DTLPWROMAE     */
1887	{ SST(0x2A, 0x05, SS_RDEF,	/* XXX TBD */
1888	    "Registrations preempted") },
1889	/* DTLPWROMAEBKVF */
1890	{ SST(0x2A, 0x06, SS_RDEF,	/* XXX TBD */
1891	    "Asymmetric access state changed") },
1892	/* DTLPWROMAEBKVF */
1893	{ SST(0x2A, 0x07, SS_RDEF,	/* XXX TBD */
1894	    "Implicit asymmetric access state transition failed") },
1895	/* DT  WROMAEBKVF */
1896	{ SST(0x2A, 0x08, SS_RDEF,	/* XXX TBD */
1897	    "Priority changed") },
1898	/* D              */
1899	{ SST(0x2A, 0x09, SS_RDEF,	/* XXX TBD */
1900	    "Capacity data has changed") },
1901	/* DT             */
1902	{ SST(0x2A, 0x0A, SS_RDEF,	/* XXX TBD */
1903	    "Error history I_T nexus cleared") },
1904	/* DT             */
1905	{ SST(0x2A, 0x0B, SS_RDEF,	/* XXX TBD */
1906	    "Error history snapshot released") },
1907	/*              F */
1908	{ SST(0x2A, 0x0C, SS_RDEF,	/* XXX TBD */
1909	    "Error recovery attributes have changed") },
1910	/*  T             */
1911	{ SST(0x2A, 0x0D, SS_RDEF,	/* XXX TBD */
1912	    "Data encryption capabilities changed") },
1913	/* DT     M E  V  */
1914	{ SST(0x2A, 0x10, SS_RDEF,	/* XXX TBD */
1915	    "Timestamp changed") },
1916	/*  T             */
1917	{ SST(0x2A, 0x11, SS_RDEF,	/* XXX TBD */
1918	    "Data encryption parameters changed by another I_T nexus") },
1919	/*  T             */
1920	{ SST(0x2A, 0x12, SS_RDEF,	/* XXX TBD */
1921	    "Data encryption parameters changed by vendor specific event") },
1922	/*  T             */
1923	{ SST(0x2A, 0x13, SS_RDEF,	/* XXX TBD */
1924	    "Data encryption key instance counter has changed") },
1925	/* DT   R MAEBKV  */
1926	{ SST(0x2A, 0x14, SS_RDEF,	/* XXX TBD */
1927	    "SA creation capabilities data has changed") },
1928	/*  T     M    V  */
1929	{ SST(0x2A, 0x15, SS_RDEF,	/* XXX TBD */
1930	    "Medium removal prevention preempted") },
1931	/* DTLPWRO    K   */
1932	{ SST(0x2B, 0x00, SS_RDEF,
1933	    "Copy cannot execute since host cannot disconnect") },
1934	/* DTLPWROMAEBKVF */
1935	{ SST(0x2C, 0x00, SS_RDEF,
1936	    "Command sequence error") },
1937	/*                */
1938	{ SST(0x2C, 0x01, SS_RDEF,
1939	    "Too many windows specified") },
1940	/*                */
1941	{ SST(0x2C, 0x02, SS_RDEF,
1942	    "Invalid combination of windows specified") },
1943	/*      R         */
1944	{ SST(0x2C, 0x03, SS_RDEF,
1945	    "Current program area is not empty") },
1946	/*      R         */
1947	{ SST(0x2C, 0x04, SS_RDEF,
1948	    "Current program area is empty") },
1949	/*           B    */
1950	{ SST(0x2C, 0x05, SS_RDEF,	/* XXX TBD */
1951	    "Illegal power condition request") },
1952	/*      R         */
1953	{ SST(0x2C, 0x06, SS_RDEF,	/* XXX TBD */
1954	    "Persistent prevent conflict") },
1955	/* DTLPWROMAEBKVF */
1956	{ SST(0x2C, 0x07, SS_RDEF,	/* XXX TBD */
1957	    "Previous busy status") },
1958	/* DTLPWROMAEBKVF */
1959	{ SST(0x2C, 0x08, SS_RDEF,	/* XXX TBD */
1960	    "Previous task set full status") },
1961	/* DTLPWROM EBKVF */
1962	{ SST(0x2C, 0x09, SS_RDEF,	/* XXX TBD */
1963	    "Previous reservation conflict status") },
1964	/*              F */
1965	{ SST(0x2C, 0x0A, SS_RDEF,	/* XXX TBD */
1966	    "Partition or collection contains user objects") },
1967	/*  T             */
1968	{ SST(0x2C, 0x0B, SS_RDEF,	/* XXX TBD */
1969	    "Not reserved") },
1970	/* D              */
1971	{ SST(0x2C, 0x0C, SS_RDEF,	/* XXX TBD */
1972	    "ORWRITE generation does not match") },
1973	/* D              */
1974	{ SST(0x2C, 0x0D, SS_RDEF,	/* XXX TBD */
1975	    "Reset write pointer not allowed") },
1976	/* D              */
1977	{ SST(0x2C, 0x0E, SS_RDEF,	/* XXX TBD */
1978	    "Zone is offline") },
1979	/* D              */
1980	{ SST(0x2C, 0x0F, SS_RDEF,	/* XXX TBD */
1981	    "Stream not open") },
1982	/* D              */
1983	{ SST(0x2C, 0x10, SS_RDEF,	/* XXX TBD */
1984	    "Unwritten data in zone") },
1985	/*  T             */
1986	{ SST(0x2D, 0x00, SS_RDEF,
1987	    "Overwrite error on update in place") },
1988	/*      R         */
1989	{ SST(0x2E, 0x00, SS_RDEF,	/* XXX TBD */
1990	    "Insufficient time for operation") },
1991	/* D              */
1992	{ SST(0x2E, 0x01, SS_RDEF,	/* XXX TBD */
1993	    "Command timeout before processing") },
1994	/* D              */
1995	{ SST(0x2E, 0x02, SS_RDEF,	/* XXX TBD */
1996	    "Command timeout during processing") },
1997	/* D              */
1998	{ SST(0x2E, 0x03, SS_RDEF,	/* XXX TBD */
1999	    "Command timeout during processing due to error recovery") },
2000	/* DTLPWROMAEBKVF */
2001	{ SST(0x2F, 0x00, SS_RDEF,
2002	    "Commands cleared by another initiator") },
2003	/* D              */
2004	{ SST(0x2F, 0x01, SS_RDEF,	/* XXX TBD */
2005	    "Commands cleared by power loss notification") },
2006	/* DTLPWROMAEBKVF */
2007	{ SST(0x2F, 0x02, SS_RDEF,	/* XXX TBD */
2008	    "Commands cleared by device server") },
2009	/* DTLPWROMAEBKVF */
2010	{ SST(0x2F, 0x03, SS_RDEF,	/* XXX TBD */
2011	    "Some commands cleared by queuing layer event") },
2012	/* DT  WROM  BK   */
2013	{ SST(0x30, 0x00, SS_RDEF,
2014	    "Incompatible medium installed") },
2015	/* DT  WRO   BK   */
2016	{ SST(0x30, 0x01, SS_RDEF,
2017	    "Cannot read medium - unknown format") },
2018	/* DT  WRO   BK   */
2019	{ SST(0x30, 0x02, SS_RDEF,
2020	    "Cannot read medium - incompatible format") },
2021	/* DT   R     K   */
2022	{ SST(0x30, 0x03, SS_RDEF,
2023	    "Cleaning cartridge installed") },
2024	/* DT  WRO   BK   */
2025	{ SST(0x30, 0x04, SS_RDEF,
2026	    "Cannot write medium - unknown format") },
2027	/* DT  WRO   BK   */
2028	{ SST(0x30, 0x05, SS_RDEF,
2029	    "Cannot write medium - incompatible format") },
2030	/* DT  WRO   B    */
2031	{ SST(0x30, 0x06, SS_RDEF,
2032	    "Cannot format medium - incompatible medium") },
2033	/* DTL WROMAEBKVF */
2034	{ SST(0x30, 0x07, SS_RDEF,
2035	    "Cleaning failure") },
2036	/*      R         */
2037	{ SST(0x30, 0x08, SS_RDEF,
2038	    "Cannot write - application code mismatch") },
2039	/*      R         */
2040	{ SST(0x30, 0x09, SS_RDEF,
2041	    "Current session not fixated for append") },
2042	/* DT  WRO AEBK   */
2043	{ SST(0x30, 0x0A, SS_RDEF,	/* XXX TBD */
2044	    "Cleaning request rejected") },
2045	/*  T             */
2046	{ SST(0x30, 0x0C, SS_RDEF,	/* XXX TBD */
2047	    "WORM medium - overwrite attempted") },
2048	/*  T             */
2049	{ SST(0x30, 0x0D, SS_RDEF,	/* XXX TBD */
2050	    "WORM medium - integrity check") },
2051	/*      R         */
2052	{ SST(0x30, 0x10, SS_RDEF,	/* XXX TBD */
2053	    "Medium not formatted") },
2054	/*        M       */
2055	{ SST(0x30, 0x11, SS_RDEF,	/* XXX TBD */
2056	    "Incompatible volume type") },
2057	/*        M       */
2058	{ SST(0x30, 0x12, SS_RDEF,	/* XXX TBD */
2059	    "Incompatible volume qualifier") },
2060	/*        M       */
2061	{ SST(0x30, 0x13, SS_RDEF,	/* XXX TBD */
2062	    "Cleaning volume expired") },
2063	/* DT  WRO   BK   */
2064	{ SST(0x31, 0x00, SS_FATAL | ENXIO,
2065	    "Medium format corrupted") },
2066	/* D L  RO   B    */
2067	{ SST(0x31, 0x01, SS_RDEF,
2068	    "Format command failed") },
2069	/*      R         */
2070	{ SST(0x31, 0x02, SS_RDEF,	/* XXX TBD */
2071	    "Zoned formatting failed due to spare linking") },
2072	/* D         B    */
2073	{ SST(0x31, 0x03, SS_FATAL | EIO,
2074	    "SANITIZE command failed") },
2075	/* D   W O   BK   */
2076	{ SST(0x32, 0x00, SS_RDEF,
2077	    "No defect spare location available") },
2078	/* D   W O   BK   */
2079	{ SST(0x32, 0x01, SS_RDEF,
2080	    "Defect list update failure") },
2081	/*  T             */
2082	{ SST(0x33, 0x00, SS_RDEF,
2083	    "Tape length error") },
2084	/* DTLPWROMAEBKVF */
2085	{ SST(0x34, 0x00, SS_RDEF,
2086	    "Enclosure failure") },
2087	/* DTLPWROMAEBKVF */
2088	{ SST(0x35, 0x00, SS_RDEF,
2089	    "Enclosure services failure") },
2090	/* DTLPWROMAEBKVF */
2091	{ SST(0x35, 0x01, SS_RDEF,
2092	    "Unsupported enclosure function") },
2093	/* DTLPWROMAEBKVF */
2094	{ SST(0x35, 0x02, SS_RDEF,
2095	    "Enclosure services unavailable") },
2096	/* DTLPWROMAEBKVF */
2097	{ SST(0x35, 0x03, SS_RDEF,
2098	    "Enclosure services transfer failure") },
2099	/* DTLPWROMAEBKVF */
2100	{ SST(0x35, 0x04, SS_RDEF,
2101	    "Enclosure services transfer refused") },
2102	/* DTL WROMAEBKVF */
2103	{ SST(0x35, 0x05, SS_RDEF,	/* XXX TBD */
2104	    "Enclosure services checksum error") },
2105	/*   L            */
2106	{ SST(0x36, 0x00, SS_RDEF,
2107	    "Ribbon, ink, or toner failure") },
2108	/* DTL WROMAEBKVF */
2109	{ SST(0x37, 0x00, SS_RDEF,
2110	    "Rounded parameter") },
2111	/*           B    */
2112	{ SST(0x38, 0x00, SS_RDEF,	/* XXX TBD */
2113	    "Event status notification") },
2114	/*           B    */
2115	{ SST(0x38, 0x02, SS_RDEF,	/* XXX TBD */
2116	    "ESN - power management class event") },
2117	/*           B    */
2118	{ SST(0x38, 0x04, SS_RDEF,	/* XXX TBD */
2119	    "ESN - media class event") },
2120	/*           B    */
2121	{ SST(0x38, 0x06, SS_RDEF,	/* XXX TBD */
2122	    "ESN - device busy class event") },
2123	/* D              */
2124	{ SST(0x38, 0x07, SS_RDEF,	/* XXX TBD */
2125	    "Thin provisioning soft threshold reached") },
2126	/* DTL WROMAE K   */
2127	{ SST(0x39, 0x00, SS_RDEF,
2128	    "Saving parameters not supported") },
2129	/* DTL WROM  BK   */
2130	{ SST(0x3A, 0x00, SS_FATAL | ENXIO,
2131	    "Medium not present") },
2132	/* DT  WROM  BK   */
2133	{ SST(0x3A, 0x01, SS_FATAL | ENXIO,
2134	    "Medium not present - tray closed") },
2135	/* DT  WROM  BK   */
2136	{ SST(0x3A, 0x02, SS_FATAL | ENXIO,
2137	    "Medium not present - tray open") },
2138	/* DT  WROM  B    */
2139	{ SST(0x3A, 0x03, SS_RDEF,	/* XXX TBD */
2140	    "Medium not present - loadable") },
2141	/* DT  WRO   B    */
2142	{ SST(0x3A, 0x04, SS_RDEF,	/* XXX TBD */
2143	    "Medium not present - medium auxiliary memory accessible") },
2144	/*  TL            */
2145	{ SST(0x3B, 0x00, SS_RDEF,
2146	    "Sequential positioning error") },
2147	/*  T             */
2148	{ SST(0x3B, 0x01, SS_RDEF,
2149	    "Tape position error at beginning-of-medium") },
2150	/*  T             */
2151	{ SST(0x3B, 0x02, SS_RDEF,
2152	    "Tape position error at end-of-medium") },
2153	/*   L            */
2154	{ SST(0x3B, 0x03, SS_RDEF,
2155	    "Tape or electronic vertical forms unit not ready") },
2156	/*   L            */
2157	{ SST(0x3B, 0x04, SS_RDEF,
2158	    "Slew failure") },
2159	/*   L            */
2160	{ SST(0x3B, 0x05, SS_RDEF,
2161	    "Paper jam") },
2162	/*   L            */
2163	{ SST(0x3B, 0x06, SS_RDEF,
2164	    "Failed to sense top-of-form") },
2165	/*   L            */
2166	{ SST(0x3B, 0x07, SS_RDEF,
2167	    "Failed to sense bottom-of-form") },
2168	/*  T             */
2169	{ SST(0x3B, 0x08, SS_RDEF,
2170	    "Reposition error") },
2171	/*                */
2172	{ SST(0x3B, 0x09, SS_RDEF,
2173	    "Read past end of medium") },
2174	/*                */
2175	{ SST(0x3B, 0x0A, SS_RDEF,
2176	    "Read past beginning of medium") },
2177	/*                */
2178	{ SST(0x3B, 0x0B, SS_RDEF,
2179	    "Position past end of medium") },
2180	/*  T             */
2181	{ SST(0x3B, 0x0C, SS_RDEF,
2182	    "Position past beginning of medium") },
2183	/* DT  WROM  BK   */
2184	{ SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
2185	    "Medium destination element full") },
2186	/* DT  WROM  BK   */
2187	{ SST(0x3B, 0x0E, SS_RDEF,
2188	    "Medium source element empty") },
2189	/*      R         */
2190	{ SST(0x3B, 0x0F, SS_RDEF,
2191	    "End of medium reached") },
2192	/* DT  WROM  BK   */
2193	{ SST(0x3B, 0x11, SS_RDEF,
2194	    "Medium magazine not accessible") },
2195	/* DT  WROM  BK   */
2196	{ SST(0x3B, 0x12, SS_RDEF,
2197	    "Medium magazine removed") },
2198	/* DT  WROM  BK   */
2199	{ SST(0x3B, 0x13, SS_RDEF,
2200	    "Medium magazine inserted") },
2201	/* DT  WROM  BK   */
2202	{ SST(0x3B, 0x14, SS_RDEF,
2203	    "Medium magazine locked") },
2204	/* DT  WROM  BK   */
2205	{ SST(0x3B, 0x15, SS_RDEF,
2206	    "Medium magazine unlocked") },
2207	/*      R         */
2208	{ SST(0x3B, 0x16, SS_RDEF,	/* XXX TBD */
2209	    "Mechanical positioning or changer error") },
2210	/*              F */
2211	{ SST(0x3B, 0x17, SS_RDEF,	/* XXX TBD */
2212	    "Read past end of user object") },
2213	/*        M       */
2214	{ SST(0x3B, 0x18, SS_RDEF,	/* XXX TBD */
2215	    "Element disabled") },
2216	/*        M       */
2217	{ SST(0x3B, 0x19, SS_RDEF,	/* XXX TBD */
2218	    "Element enabled") },
2219	/*        M       */
2220	{ SST(0x3B, 0x1A, SS_RDEF,	/* XXX TBD */
2221	    "Data transfer device removed") },
2222	/*        M       */
2223	{ SST(0x3B, 0x1B, SS_RDEF,	/* XXX TBD */
2224	    "Data transfer device inserted") },
2225	/*  T             */
2226	{ SST(0x3B, 0x1C, SS_RDEF,	/* XXX TBD */
2227	    "Too many logical objects on partition to support operation") },
2228	/* DTLPWROMAE K   */
2229	{ SST(0x3D, 0x00, SS_RDEF,
2230	    "Invalid bits in IDENTIFY message") },
2231	/* DTLPWROMAEBKVF */
2232	{ SST(0x3E, 0x00, SS_RDEF,
2233	    "Logical unit has not self-configured yet") },
2234	/* DTLPWROMAEBKVF */
2235	{ SST(0x3E, 0x01, SS_RDEF,
2236	    "Logical unit failure") },
2237	/* DTLPWROMAEBKVF */
2238	{ SST(0x3E, 0x02, SS_RDEF,
2239	    "Timeout on logical unit") },
2240	/* DTLPWROMAEBKVF */
2241	{ SST(0x3E, 0x03, SS_RDEF,	/* XXX TBD */
2242	    "Logical unit failed self-test") },
2243	/* DTLPWROMAEBKVF */
2244	{ SST(0x3E, 0x04, SS_RDEF,	/* XXX TBD */
2245	    "Logical unit unable to update self-test log") },
2246	/* DTLPWROMAEBKVF */
2247	{ SST(0x3F, 0x00, SS_RDEF,
2248	    "Target operating conditions have changed") },
2249	/* DTLPWROMAEBKVF */
2250	{ SST(0x3F, 0x01, SS_RDEF,
2251	    "Microcode has been changed") },
2252	/* DTLPWROM  BK   */
2253	{ SST(0x3F, 0x02, SS_RDEF,
2254	    "Changed operating definition") },
2255	/* DTLPWROMAEBKVF */
2256	{ SST(0x3F, 0x03, SS_RDEF,
2257	    "INQUIRY data has changed") },
2258	/* DT  WROMAEBK   */
2259	{ SST(0x3F, 0x04, SS_RDEF,
2260	    "Component device attached") },
2261	/* DT  WROMAEBK   */
2262	{ SST(0x3F, 0x05, SS_RDEF,
2263	    "Device identifier changed") },
2264	/* DT  WROMAEB    */
2265	{ SST(0x3F, 0x06, SS_RDEF,
2266	    "Redundancy group created or modified") },
2267	/* DT  WROMAEB    */
2268	{ SST(0x3F, 0x07, SS_RDEF,
2269	    "Redundancy group deleted") },
2270	/* DT  WROMAEB    */
2271	{ SST(0x3F, 0x08, SS_RDEF,
2272	    "Spare created or modified") },
2273	/* DT  WROMAEB    */
2274	{ SST(0x3F, 0x09, SS_RDEF,
2275	    "Spare deleted") },
2276	/* DT  WROMAEBK   */
2277	{ SST(0x3F, 0x0A, SS_RDEF,
2278	    "Volume set created or modified") },
2279	/* DT  WROMAEBK   */
2280	{ SST(0x3F, 0x0B, SS_RDEF,
2281	    "Volume set deleted") },
2282	/* DT  WROMAEBK   */
2283	{ SST(0x3F, 0x0C, SS_RDEF,
2284	    "Volume set deassigned") },
2285	/* DT  WROMAEBK   */
2286	{ SST(0x3F, 0x0D, SS_RDEF,
2287	    "Volume set reassigned") },
2288	/* DTLPWROMAE     */
2289	{ SST(0x3F, 0x0E, SS_RDEF | SSQ_RESCAN ,
2290	    "Reported LUNs data has changed") },
2291	/* DTLPWROMAEBKVF */
2292	{ SST(0x3F, 0x0F, SS_RDEF,	/* XXX TBD */
2293	    "Echo buffer overwritten") },
2294	/* DT  WROM  B    */
2295	{ SST(0x3F, 0x10, SS_RDEF,	/* XXX TBD */
2296	    "Medium loadable") },
2297	/* DT  WROM  B    */
2298	{ SST(0x3F, 0x11, SS_RDEF,	/* XXX TBD */
2299	    "Medium auxiliary memory accessible") },
2300	/* DTLPWR MAEBK F */
2301	{ SST(0x3F, 0x12, SS_RDEF,	/* XXX TBD */
2302	    "iSCSI IP address added") },
2303	/* DTLPWR MAEBK F */
2304	{ SST(0x3F, 0x13, SS_RDEF,	/* XXX TBD */
2305	    "iSCSI IP address removed") },
2306	/* DTLPWR MAEBK F */
2307	{ SST(0x3F, 0x14, SS_RDEF,	/* XXX TBD */
2308	    "iSCSI IP address changed") },
2309	/* DTLPWR MAEBK   */
2310	{ SST(0x3F, 0x15, SS_RDEF,	/* XXX TBD */
2311	    "Inspect referrals sense descriptors") },
2312	/* DTLPWROMAEBKVF */
2313	{ SST(0x3F, 0x16, SS_RDEF,	/* XXX TBD */
2314	    "Microcode has been changed without reset") },
2315	/* D              */
2316	{ SST(0x3F, 0x17, SS_RDEF,	/* XXX TBD */
2317	    "Zone transition to full") },
2318	/* D              */
2319	{ SST(0x40, 0x00, SS_RDEF,
2320	    "RAM failure") },		/* deprecated - use 40 NN instead */
2321	/* DTLPWROMAEBKVF */
2322	{ SST(0x40, 0x80, SS_RDEF,
2323	    "Diagnostic failure: ASCQ = Component ID") },
2324	/* DTLPWROMAEBKVF */
2325	{ SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
2326	    NULL) },			/* Range 0x80->0xFF */
2327	/* D              */
2328	{ SST(0x41, 0x00, SS_RDEF,
2329	    "Data path failure") },	/* deprecated - use 40 NN instead */
2330	/* D              */
2331	{ SST(0x42, 0x00, SS_RDEF,
2332	    "Power-on or self-test failure") },
2333					/* deprecated - use 40 NN instead */
2334	/* DTLPWROMAEBKVF */
2335	{ SST(0x43, 0x00, SS_RDEF,
2336	    "Message error") },
2337	/* DTLPWROMAEBKVF */
2338	{ SST(0x44, 0x00, SS_FATAL | EIO,
2339	    "Internal target failure") },
2340	/* DT P   MAEBKVF */
2341	{ SST(0x44, 0x01, SS_RDEF,	/* XXX TBD */
2342	    "Persistent reservation information lost") },
2343	/* DT        B    */
2344	{ SST(0x44, 0x71, SS_RDEF,	/* XXX TBD */
2345	    "ATA device failed set features") },
2346	/* DTLPWROMAEBKVF */
2347	{ SST(0x45, 0x00, SS_RDEF,
2348	    "Select or reselect failure") },
2349	/* DTLPWROM  BK   */
2350	{ SST(0x46, 0x00, SS_RDEF,
2351	    "Unsuccessful soft reset") },
2352	/* DTLPWROMAEBKVF */
2353	{ SST(0x47, 0x00, SS_RDEF,
2354	    "SCSI parity error") },
2355	/* DTLPWROMAEBKVF */
2356	{ SST(0x47, 0x01, SS_RDEF,	/* XXX TBD */
2357	    "Data phase CRC error detected") },
2358	/* DTLPWROMAEBKVF */
2359	{ SST(0x47, 0x02, SS_RDEF,	/* XXX TBD */
2360	    "SCSI parity error detected during ST data phase") },
2361	/* DTLPWROMAEBKVF */
2362	{ SST(0x47, 0x03, SS_RDEF,	/* XXX TBD */
2363	    "Information unit iuCRC error detected") },
2364	/* DTLPWROMAEBKVF */
2365	{ SST(0x47, 0x04, SS_RDEF,	/* XXX TBD */
2366	    "Asynchronous information protection error detected") },
2367	/* DTLPWROMAEBKVF */
2368	{ SST(0x47, 0x05, SS_RDEF,	/* XXX TBD */
2369	    "Protocol service CRC error") },
2370	/* DT     MAEBKVF */
2371	{ SST(0x47, 0x06, SS_RDEF,	/* XXX TBD */
2372	    "PHY test function in progress") },
2373	/* DT PWROMAEBK   */
2374	{ SST(0x47, 0x7F, SS_RDEF,	/* XXX TBD */
2375	    "Some commands cleared by iSCSI protocol event") },
2376	/* DTLPWROMAEBKVF */
2377	{ SST(0x48, 0x00, SS_RDEF,
2378	    "Initiator detected error message received") },
2379	/* DTLPWROMAEBKVF */
2380	{ SST(0x49, 0x00, SS_RDEF,
2381	    "Invalid message error") },
2382	/* DTLPWROMAEBKVF */
2383	{ SST(0x4A, 0x00, SS_RDEF,
2384	    "Command phase error") },
2385	/* DTLPWROMAEBKVF */
2386	{ SST(0x4B, 0x00, SS_RDEF,
2387	    "Data phase error") },
2388	/* DT PWROMAEBK   */
2389	{ SST(0x4B, 0x01, SS_RDEF,	/* XXX TBD */
2390	    "Invalid target port transfer tag received") },
2391	/* DT PWROMAEBK   */
2392	{ SST(0x4B, 0x02, SS_RDEF,	/* XXX TBD */
2393	    "Too much write data") },
2394	/* DT PWROMAEBK   */
2395	{ SST(0x4B, 0x03, SS_RDEF,	/* XXX TBD */
2396	    "ACK/NAK timeout") },
2397	/* DT PWROMAEBK   */
2398	{ SST(0x4B, 0x04, SS_RDEF,	/* XXX TBD */
2399	    "NAK received") },
2400	/* DT PWROMAEBK   */
2401	{ SST(0x4B, 0x05, SS_RDEF,	/* XXX TBD */
2402	    "Data offset error") },
2403	/* DT PWROMAEBK   */
2404	{ SST(0x4B, 0x06, SS_RDEF,	/* XXX TBD */
2405	    "Initiator response timeout") },
2406	/* DT PWROMAEBK F */
2407	{ SST(0x4B, 0x07, SS_RDEF,	/* XXX TBD */
2408	    "Connection lost") },
2409	/* DT PWROMAEBK F */
2410	{ SST(0x4B, 0x08, SS_RDEF,	/* XXX TBD */
2411	    "Data-in buffer overflow - data buffer size") },
2412	/* DT PWROMAEBK F */
2413	{ SST(0x4B, 0x09, SS_RDEF,	/* XXX TBD */
2414	    "Data-in buffer overflow - data buffer descriptor area") },
2415	/* DT PWROMAEBK F */
2416	{ SST(0x4B, 0x0A, SS_RDEF,	/* XXX TBD */
2417	    "Data-in buffer error") },
2418	/* DT PWROMAEBK F */
2419	{ SST(0x4B, 0x0B, SS_RDEF,	/* XXX TBD */
2420	    "Data-out buffer overflow - data buffer size") },
2421	/* DT PWROMAEBK F */
2422	{ SST(0x4B, 0x0C, SS_RDEF,	/* XXX TBD */
2423	    "Data-out buffer overflow - data buffer descriptor area") },
2424	/* DT PWROMAEBK F */
2425	{ SST(0x4B, 0x0D, SS_RDEF,	/* XXX TBD */
2426	    "Data-out buffer error") },
2427	/* DT PWROMAEBK F */
2428	{ SST(0x4B, 0x0E, SS_RDEF,	/* XXX TBD */
2429	    "PCIe fabric error") },
2430	/* DT PWROMAEBK F */
2431	{ SST(0x4B, 0x0F, SS_RDEF,	/* XXX TBD */
2432	    "PCIe completion timeout") },
2433	/* DT PWROMAEBK F */
2434	{ SST(0x4B, 0x10, SS_RDEF,	/* XXX TBD */
2435	    "PCIe completer abort") },
2436	/* DT PWROMAEBK F */
2437	{ SST(0x4B, 0x11, SS_RDEF,	/* XXX TBD */
2438	    "PCIe poisoned TLP received") },
2439	/* DT PWROMAEBK F */
2440	{ SST(0x4B, 0x12, SS_RDEF,	/* XXX TBD */
2441	    "PCIe ECRC check failed") },
2442	/* DT PWROMAEBK F */
2443	{ SST(0x4B, 0x13, SS_RDEF,	/* XXX TBD */
2444	    "PCIe unsupported request") },
2445	/* DT PWROMAEBK F */
2446	{ SST(0x4B, 0x14, SS_RDEF,	/* XXX TBD */
2447	    "PCIe ACS violation") },
2448	/* DT PWROMAEBK F */
2449	{ SST(0x4B, 0x15, SS_RDEF,	/* XXX TBD */
2450	    "PCIe TLP prefix blocket") },
2451	/* DTLPWROMAEBKVF */
2452	{ SST(0x4C, 0x00, SS_RDEF,
2453	    "Logical unit failed self-configuration") },
2454	/* DTLPWROMAEBKVF */
2455	{ SST(0x4D, 0x00, SS_RDEF,
2456	    "Tagged overlapped commands: ASCQ = Queue tag ID") },
2457	/* DTLPWROMAEBKVF */
2458	{ SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
2459	    NULL) },			/* Range 0x00->0xFF */
2460	/* DTLPWROMAEBKVF */
2461	{ SST(0x4E, 0x00, SS_RDEF,
2462	    "Overlapped commands attempted") },
2463	/*  T             */
2464	{ SST(0x50, 0x00, SS_RDEF,
2465	    "Write append error") },
2466	/*  T             */
2467	{ SST(0x50, 0x01, SS_RDEF,
2468	    "Write append position error") },
2469	/*  T             */
2470	{ SST(0x50, 0x02, SS_RDEF,
2471	    "Position error related to timing") },
2472	/*  T   RO        */
2473	{ SST(0x51, 0x00, SS_RDEF,
2474	    "Erase failure") },
2475	/*      R         */
2476	{ SST(0x51, 0x01, SS_RDEF,	/* XXX TBD */
2477	    "Erase failure - incomplete erase operation detected") },
2478	/*  T             */
2479	{ SST(0x52, 0x00, SS_RDEF,
2480	    "Cartridge fault") },
2481	/* DTL WROM  BK   */
2482	{ SST(0x53, 0x00, SS_RDEF,
2483	    "Media load or eject failed") },
2484	/*  T             */
2485	{ SST(0x53, 0x01, SS_RDEF,
2486	    "Unload tape failure") },
2487	/* DT  WROM  BK   */
2488	{ SST(0x53, 0x02, SS_RDEF,
2489	    "Medium removal prevented") },
2490	/*        M       */
2491	{ SST(0x53, 0x03, SS_RDEF,	/* XXX TBD */
2492	    "Medium removal prevented by data transfer element") },
2493	/*  T             */
2494	{ SST(0x53, 0x04, SS_RDEF,	/* XXX TBD */
2495	    "Medium thread or unthread failure") },
2496	/*        M       */
2497	{ SST(0x53, 0x05, SS_RDEF,	/* XXX TBD */
2498	    "Volume identifier invalid") },
2499	/*  T             */
2500	{ SST(0x53, 0x06, SS_RDEF,	/* XXX TBD */
2501	    "Volume identifier missing") },
2502	/*        M       */
2503	{ SST(0x53, 0x07, SS_RDEF,	/* XXX TBD */
2504	    "Duplicate volume identifier") },
2505	/*        M       */
2506	{ SST(0x53, 0x08, SS_RDEF,	/* XXX TBD */
2507	    "Element status unknown") },
2508	/*        M       */
2509	{ SST(0x53, 0x09, SS_RDEF,	/* XXX TBD */
2510	    "Data transfer device error - load failed") },
2511	/*        M       */
2512	{ SST(0x53, 0x0A, SS_RDEF,	/* XXX TBD */
2513	    "Data transfer device error - unload failed") },
2514	/*        M       */
2515	{ SST(0x53, 0x0B, SS_RDEF,	/* XXX TBD */
2516	    "Data transfer device error - unload missing") },
2517	/*        M       */
2518	{ SST(0x53, 0x0C, SS_RDEF,	/* XXX TBD */
2519	    "Data transfer device error - eject failed") },
2520	/*        M       */
2521	{ SST(0x53, 0x0D, SS_RDEF,	/* XXX TBD */
2522	    "Data transfer device error - library communication failed") },
2523	/*    P           */
2524	{ SST(0x54, 0x00, SS_RDEF,
2525	    "SCSI to host system interface failure") },
2526	/*    P           */
2527	{ SST(0x55, 0x00, SS_RDEF,
2528	    "System resource failure") },
2529	/* D     O   BK   */
2530	{ SST(0x55, 0x01, SS_FATAL | ENOSPC,
2531	    "System buffer full") },
2532	/* DTLPWROMAE K   */
2533	{ SST(0x55, 0x02, SS_RDEF,	/* XXX TBD */
2534	    "Insufficient reservation resources") },
2535	/* DTLPWROMAE K   */
2536	{ SST(0x55, 0x03, SS_RDEF,	/* XXX TBD */
2537	    "Insufficient resources") },
2538	/* DTLPWROMAE K   */
2539	{ SST(0x55, 0x04, SS_RDEF,	/* XXX TBD */
2540	    "Insufficient registration resources") },
2541	/* DT PWROMAEBK   */
2542	{ SST(0x55, 0x05, SS_RDEF,	/* XXX TBD */
2543	    "Insufficient access control resources") },
2544	/* DT  WROM  B    */
2545	{ SST(0x55, 0x06, SS_RDEF,	/* XXX TBD */
2546	    "Auxiliary memory out of space") },
2547	/*              F */
2548	{ SST(0x55, 0x07, SS_RDEF,	/* XXX TBD */
2549	    "Quota error") },
2550	/*  T             */
2551	{ SST(0x55, 0x08, SS_RDEF,	/* XXX TBD */
2552	    "Maximum number of supplemental decryption keys exceeded") },
2553	/*        M       */
2554	{ SST(0x55, 0x09, SS_RDEF,	/* XXX TBD */
2555	    "Medium auxiliary memory not accessible") },
2556	/*        M       */
2557	{ SST(0x55, 0x0A, SS_RDEF,	/* XXX TBD */
2558	    "Data currently unavailable") },
2559	/* DTLPWROMAEBKVF */
2560	{ SST(0x55, 0x0B, SS_RDEF,	/* XXX TBD */
2561	    "Insufficient power for operation") },
2562	/* DT P      B    */
2563	{ SST(0x55, 0x0C, SS_RDEF,	/* XXX TBD */
2564	    "Insufficient resources to create ROD") },
2565	/* DT P      B    */
2566	{ SST(0x55, 0x0D, SS_RDEF,	/* XXX TBD */
2567	    "Insufficient resources to create ROD token") },
2568	/* D              */
2569	{ SST(0x55, 0x0E, SS_RDEF,	/* XXX TBD */
2570	    "Insufficient zone resources") },
2571	/* D              */
2572	{ SST(0x55, 0x0F, SS_RDEF,	/* XXX TBD */
2573	    "Insufficient zone resources to complete write") },
2574	/* D              */
2575	{ SST(0x55, 0x10, SS_RDEF,	/* XXX TBD */
2576	    "Maximum number of streams open") },
2577	/*      R         */
2578	{ SST(0x57, 0x00, SS_RDEF,
2579	    "Unable to recover table-of-contents") },
2580	/*       O        */
2581	{ SST(0x58, 0x00, SS_RDEF,
2582	    "Generation does not exist") },
2583	/*       O        */
2584	{ SST(0x59, 0x00, SS_RDEF,
2585	    "Updated block read") },
2586	/* DTLPWRO   BK   */
2587	{ SST(0x5A, 0x00, SS_RDEF,
2588	    "Operator request or state change input") },
2589	/* DT  WROM  BK   */
2590	{ SST(0x5A, 0x01, SS_RDEF,
2591	    "Operator medium removal request") },
2592	/* DT  WRO A BK   */
2593	{ SST(0x5A, 0x02, SS_RDEF,
2594	    "Operator selected write protect") },
2595	/* DT  WRO A BK   */
2596	{ SST(0x5A, 0x03, SS_RDEF,
2597	    "Operator selected write permit") },
2598	/* DTLPWROM   K   */
2599	{ SST(0x5B, 0x00, SS_RDEF,
2600	    "Log exception") },
2601	/* DTLPWROM   K   */
2602	{ SST(0x5B, 0x01, SS_RDEF,
2603	    "Threshold condition met") },
2604	/* DTLPWROM   K   */
2605	{ SST(0x5B, 0x02, SS_RDEF,
2606	    "Log counter at maximum") },
2607	/* DTLPWROM   K   */
2608	{ SST(0x5B, 0x03, SS_RDEF,
2609	    "Log list codes exhausted") },
2610	/* D     O        */
2611	{ SST(0x5C, 0x00, SS_RDEF,
2612	    "RPL status change") },
2613	/* D     O        */
2614	{ SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2615	    "Spindles synchronized") },
2616	/* D     O        */
2617	{ SST(0x5C, 0x02, SS_RDEF,
2618	    "Spindles not synchronized") },
2619	/* DTLPWROMAEBKVF */
2620	{ SST(0x5D, 0x00, SS_NOP | SSQ_PRINT_SENSE,
2621	    "Failure prediction threshold exceeded") },
2622	/*      R    B    */
2623	{ SST(0x5D, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2624	    "Media failure prediction threshold exceeded") },
2625	/*      R         */
2626	{ SST(0x5D, 0x02, SS_NOP | SSQ_PRINT_SENSE,
2627	    "Logical unit failure prediction threshold exceeded") },
2628	/*      R         */
2629	{ SST(0x5D, 0x03, SS_NOP | SSQ_PRINT_SENSE,
2630	    "Spare area exhaustion prediction threshold exceeded") },
2631	/* D         B    */
2632	{ SST(0x5D, 0x10, SS_NOP | SSQ_PRINT_SENSE,
2633	    "Hardware impending failure general hard drive failure") },
2634	/* D         B    */
2635	{ SST(0x5D, 0x11, SS_NOP | SSQ_PRINT_SENSE,
2636	    "Hardware impending failure drive error rate too high") },
2637	/* D         B    */
2638	{ SST(0x5D, 0x12, SS_NOP | SSQ_PRINT_SENSE,
2639	    "Hardware impending failure data error rate too high") },
2640	/* D         B    */
2641	{ SST(0x5D, 0x13, SS_NOP | SSQ_PRINT_SENSE,
2642	    "Hardware impending failure seek error rate too high") },
2643	/* D         B    */
2644	{ SST(0x5D, 0x14, SS_NOP | SSQ_PRINT_SENSE,
2645	    "Hardware impending failure too many block reassigns") },
2646	/* D         B    */
2647	{ SST(0x5D, 0x15, SS_NOP | SSQ_PRINT_SENSE,
2648	    "Hardware impending failure access times too high") },
2649	/* D         B    */
2650	{ SST(0x5D, 0x16, SS_NOP | SSQ_PRINT_SENSE,
2651	    "Hardware impending failure start unit times too high") },
2652	/* D         B    */
2653	{ SST(0x5D, 0x17, SS_NOP | SSQ_PRINT_SENSE,
2654	    "Hardware impending failure channel parametrics") },
2655	/* D         B    */
2656	{ SST(0x5D, 0x18, SS_NOP | SSQ_PRINT_SENSE,
2657	    "Hardware impending failure controller detected") },
2658	/* D         B    */
2659	{ SST(0x5D, 0x19, SS_NOP | SSQ_PRINT_SENSE,
2660	    "Hardware impending failure throughput performance") },
2661	/* D         B    */
2662	{ SST(0x5D, 0x1A, SS_NOP | SSQ_PRINT_SENSE,
2663	    "Hardware impending failure seek time performance") },
2664	/* D         B    */
2665	{ SST(0x5D, 0x1B, SS_NOP | SSQ_PRINT_SENSE,
2666	    "Hardware impending failure spin-up retry count") },
2667	/* D         B    */
2668	{ SST(0x5D, 0x1C, SS_NOP | SSQ_PRINT_SENSE,
2669	    "Hardware impending failure drive calibration retry count") },
2670	/* D         B    */
2671	{ SST(0x5D, 0x1D, SS_NOP | SSQ_PRINT_SENSE,
2672	    "Hardware impending failure power loss protection circuit") },
2673	/* D         B    */
2674	{ SST(0x5D, 0x20, SS_NOP | SSQ_PRINT_SENSE,
2675	    "Controller impending failure general hard drive failure") },
2676	/* D         B    */
2677	{ SST(0x5D, 0x21, SS_NOP | SSQ_PRINT_SENSE,
2678	    "Controller impending failure drive error rate too high") },
2679	/* D         B    */
2680	{ SST(0x5D, 0x22, SS_NOP | SSQ_PRINT_SENSE,
2681	    "Controller impending failure data error rate too high") },
2682	/* D         B    */
2683	{ SST(0x5D, 0x23, SS_NOP | SSQ_PRINT_SENSE,
2684	    "Controller impending failure seek error rate too high") },
2685	/* D         B    */
2686	{ SST(0x5D, 0x24, SS_NOP | SSQ_PRINT_SENSE,
2687	    "Controller impending failure too many block reassigns") },
2688	/* D         B    */
2689	{ SST(0x5D, 0x25, SS_NOP | SSQ_PRINT_SENSE,
2690	    "Controller impending failure access times too high") },
2691	/* D         B    */
2692	{ SST(0x5D, 0x26, SS_NOP | SSQ_PRINT_SENSE,
2693	    "Controller impending failure start unit times too high") },
2694	/* D         B    */
2695	{ SST(0x5D, 0x27, SS_NOP | SSQ_PRINT_SENSE,
2696	    "Controller impending failure channel parametrics") },
2697	/* D         B    */
2698	{ SST(0x5D, 0x28, SS_NOP | SSQ_PRINT_SENSE,
2699	    "Controller impending failure controller detected") },
2700	/* D         B    */
2701	{ SST(0x5D, 0x29, SS_NOP | SSQ_PRINT_SENSE,
2702	    "Controller impending failure throughput performance") },
2703	/* D         B    */
2704	{ SST(0x5D, 0x2A, SS_NOP | SSQ_PRINT_SENSE,
2705	    "Controller impending failure seek time performance") },
2706	/* D         B    */
2707	{ SST(0x5D, 0x2B, SS_NOP | SSQ_PRINT_SENSE,
2708	    "Controller impending failure spin-up retry count") },
2709	/* D         B    */
2710	{ SST(0x5D, 0x2C, SS_NOP | SSQ_PRINT_SENSE,
2711	    "Controller impending failure drive calibration retry count") },
2712	/* D         B    */
2713	{ SST(0x5D, 0x30, SS_NOP | SSQ_PRINT_SENSE,
2714	    "Data channel impending failure general hard drive failure") },
2715	/* D         B    */
2716	{ SST(0x5D, 0x31, SS_NOP | SSQ_PRINT_SENSE,
2717	    "Data channel impending failure drive error rate too high") },
2718	/* D         B    */
2719	{ SST(0x5D, 0x32, SS_NOP | SSQ_PRINT_SENSE,
2720	    "Data channel impending failure data error rate too high") },
2721	/* D         B    */
2722	{ SST(0x5D, 0x33, SS_NOP | SSQ_PRINT_SENSE,
2723	    "Data channel impending failure seek error rate too high") },
2724	/* D         B    */
2725	{ SST(0x5D, 0x34, SS_NOP | SSQ_PRINT_SENSE,
2726	    "Data channel impending failure too many block reassigns") },
2727	/* D         B    */
2728	{ SST(0x5D, 0x35, SS_NOP | SSQ_PRINT_SENSE,
2729	    "Data channel impending failure access times too high") },
2730	/* D         B    */
2731	{ SST(0x5D, 0x36, SS_NOP | SSQ_PRINT_SENSE,
2732	    "Data channel impending failure start unit times too high") },
2733	/* D         B    */
2734	{ SST(0x5D, 0x37, SS_NOP | SSQ_PRINT_SENSE,
2735	    "Data channel impending failure channel parametrics") },
2736	/* D         B    */
2737	{ SST(0x5D, 0x38, SS_NOP | SSQ_PRINT_SENSE,
2738	    "Data channel impending failure controller detected") },
2739	/* D         B    */
2740	{ SST(0x5D, 0x39, SS_NOP | SSQ_PRINT_SENSE,
2741	    "Data channel impending failure throughput performance") },
2742	/* D         B    */
2743	{ SST(0x5D, 0x3A, SS_NOP | SSQ_PRINT_SENSE,
2744	    "Data channel impending failure seek time performance") },
2745	/* D         B    */
2746	{ SST(0x5D, 0x3B, SS_NOP | SSQ_PRINT_SENSE,
2747	    "Data channel impending failure spin-up retry count") },
2748	/* D         B    */
2749	{ SST(0x5D, 0x3C, SS_NOP | SSQ_PRINT_SENSE,
2750	    "Data channel impending failure drive calibration retry count") },
2751	/* D         B    */
2752	{ SST(0x5D, 0x40, SS_NOP | SSQ_PRINT_SENSE,
2753	    "Servo impending failure general hard drive failure") },
2754	/* D         B    */
2755	{ SST(0x5D, 0x41, SS_NOP | SSQ_PRINT_SENSE,
2756	    "Servo impending failure drive error rate too high") },
2757	/* D         B    */
2758	{ SST(0x5D, 0x42, SS_NOP | SSQ_PRINT_SENSE,
2759	    "Servo impending failure data error rate too high") },
2760	/* D         B    */
2761	{ SST(0x5D, 0x43, SS_NOP | SSQ_PRINT_SENSE,
2762	    "Servo impending failure seek error rate too high") },
2763	/* D         B    */
2764	{ SST(0x5D, 0x44, SS_NOP | SSQ_PRINT_SENSE,
2765	    "Servo impending failure too many block reassigns") },
2766	/* D         B    */
2767	{ SST(0x5D, 0x45, SS_NOP | SSQ_PRINT_SENSE,
2768	    "Servo impending failure access times too high") },
2769	/* D         B    */
2770	{ SST(0x5D, 0x46, SS_NOP | SSQ_PRINT_SENSE,
2771	    "Servo impending failure start unit times too high") },
2772	/* D         B    */
2773	{ SST(0x5D, 0x47, SS_NOP | SSQ_PRINT_SENSE,
2774	    "Servo impending failure channel parametrics") },
2775	/* D         B    */
2776	{ SST(0x5D, 0x48, SS_NOP | SSQ_PRINT_SENSE,
2777	    "Servo impending failure controller detected") },
2778	/* D         B    */
2779	{ SST(0x5D, 0x49, SS_NOP | SSQ_PRINT_SENSE,
2780	    "Servo impending failure throughput performance") },
2781	/* D         B    */
2782	{ SST(0x5D, 0x4A, SS_NOP | SSQ_PRINT_SENSE,
2783	    "Servo impending failure seek time performance") },
2784	/* D         B    */
2785	{ SST(0x5D, 0x4B, SS_NOP | SSQ_PRINT_SENSE,
2786	    "Servo impending failure spin-up retry count") },
2787	/* D         B    */
2788	{ SST(0x5D, 0x4C, SS_NOP | SSQ_PRINT_SENSE,
2789	    "Servo impending failure drive calibration retry count") },
2790	/* D         B    */
2791	{ SST(0x5D, 0x50, SS_NOP | SSQ_PRINT_SENSE,
2792	    "Spindle impending failure general hard drive failure") },
2793	/* D         B    */
2794	{ SST(0x5D, 0x51, SS_NOP | SSQ_PRINT_SENSE,
2795	    "Spindle impending failure drive error rate too high") },
2796	/* D         B    */
2797	{ SST(0x5D, 0x52, SS_NOP | SSQ_PRINT_SENSE,
2798	    "Spindle impending failure data error rate too high") },
2799	/* D         B    */
2800	{ SST(0x5D, 0x53, SS_NOP | SSQ_PRINT_SENSE,
2801	    "Spindle impending failure seek error rate too high") },
2802	/* D         B    */
2803	{ SST(0x5D, 0x54, SS_NOP | SSQ_PRINT_SENSE,
2804	    "Spindle impending failure too many block reassigns") },
2805	/* D         B    */
2806	{ SST(0x5D, 0x55, SS_NOP | SSQ_PRINT_SENSE,
2807	    "Spindle impending failure access times too high") },
2808	/* D         B    */
2809	{ SST(0x5D, 0x56, SS_NOP | SSQ_PRINT_SENSE,
2810	    "Spindle impending failure start unit times too high") },
2811	/* D         B    */
2812	{ SST(0x5D, 0x57, SS_NOP | SSQ_PRINT_SENSE,
2813	    "Spindle impending failure channel parametrics") },
2814	/* D         B    */
2815	{ SST(0x5D, 0x58, SS_NOP | SSQ_PRINT_SENSE,
2816	    "Spindle impending failure controller detected") },
2817	/* D         B    */
2818	{ SST(0x5D, 0x59, SS_NOP | SSQ_PRINT_SENSE,
2819	    "Spindle impending failure throughput performance") },
2820	/* D         B    */
2821	{ SST(0x5D, 0x5A, SS_NOP | SSQ_PRINT_SENSE,
2822	    "Spindle impending failure seek time performance") },
2823	/* D         B    */
2824	{ SST(0x5D, 0x5B, SS_NOP | SSQ_PRINT_SENSE,
2825	    "Spindle impending failure spin-up retry count") },
2826	/* D         B    */
2827	{ SST(0x5D, 0x5C, SS_NOP | SSQ_PRINT_SENSE,
2828	    "Spindle impending failure drive calibration retry count") },
2829	/* D         B    */
2830	{ SST(0x5D, 0x60, SS_NOP | SSQ_PRINT_SENSE,
2831	    "Firmware impending failure general hard drive failure") },
2832	/* D         B    */
2833	{ SST(0x5D, 0x61, SS_NOP | SSQ_PRINT_SENSE,
2834	    "Firmware impending failure drive error rate too high") },
2835	/* D         B    */
2836	{ SST(0x5D, 0x62, SS_NOP | SSQ_PRINT_SENSE,
2837	    "Firmware impending failure data error rate too high") },
2838	/* D         B    */
2839	{ SST(0x5D, 0x63, SS_NOP | SSQ_PRINT_SENSE,
2840	    "Firmware impending failure seek error rate too high") },
2841	/* D         B    */
2842	{ SST(0x5D, 0x64, SS_NOP | SSQ_PRINT_SENSE,
2843	    "Firmware impending failure too many block reassigns") },
2844	/* D         B    */
2845	{ SST(0x5D, 0x65, SS_NOP | SSQ_PRINT_SENSE,
2846	    "Firmware impending failure access times too high") },
2847	/* D         B    */
2848	{ SST(0x5D, 0x66, SS_NOP | SSQ_PRINT_SENSE,
2849	    "Firmware impending failure start unit times too high") },
2850	/* D         B    */
2851	{ SST(0x5D, 0x67, SS_NOP | SSQ_PRINT_SENSE,
2852	    "Firmware impending failure channel parametrics") },
2853	/* D         B    */
2854	{ SST(0x5D, 0x68, SS_NOP | SSQ_PRINT_SENSE,
2855	    "Firmware impending failure controller detected") },
2856	/* D         B    */
2857	{ SST(0x5D, 0x69, SS_NOP | SSQ_PRINT_SENSE,
2858	    "Firmware impending failure throughput performance") },
2859	/* D         B    */
2860	{ SST(0x5D, 0x6A, SS_NOP | SSQ_PRINT_SENSE,
2861	    "Firmware impending failure seek time performance") },
2862	/* D         B    */
2863	{ SST(0x5D, 0x6B, SS_NOP | SSQ_PRINT_SENSE,
2864	    "Firmware impending failure spin-up retry count") },
2865	/* D         B    */
2866	{ SST(0x5D, 0x6C, SS_NOP | SSQ_PRINT_SENSE,
2867	    "Firmware impending failure drive calibration retry count") },
2868	/* D         B    */
2869	{ SST(0x5D, 0x73, SS_NOP | SSQ_PRINT_SENSE,
2870	    "Media impending failure endurance limit met") },
2871	/* DTLPWROMAEBKVF */
2872	{ SST(0x5D, 0xFF, SS_NOP | SSQ_PRINT_SENSE,
2873	    "Failure prediction threshold exceeded (false)") },
2874	/* DTLPWRO A  K   */
2875	{ SST(0x5E, 0x00, SS_RDEF,
2876	    "Low power condition on") },
2877	/* DTLPWRO A  K   */
2878	{ SST(0x5E, 0x01, SS_RDEF,
2879	    "Idle condition activated by timer") },
2880	/* DTLPWRO A  K   */
2881	{ SST(0x5E, 0x02, SS_RDEF,
2882	    "Standby condition activated by timer") },
2883	/* DTLPWRO A  K   */
2884	{ SST(0x5E, 0x03, SS_RDEF,
2885	    "Idle condition activated by command") },
2886	/* DTLPWRO A  K   */
2887	{ SST(0x5E, 0x04, SS_RDEF,
2888	    "Standby condition activated by command") },
2889	/* DTLPWRO A  K   */
2890	{ SST(0x5E, 0x05, SS_RDEF,
2891	    "Idle-B condition activated by timer") },
2892	/* DTLPWRO A  K   */
2893	{ SST(0x5E, 0x06, SS_RDEF,
2894	    "Idle-B condition activated by command") },
2895	/* DTLPWRO A  K   */
2896	{ SST(0x5E, 0x07, SS_RDEF,
2897	    "Idle-C condition activated by timer") },
2898	/* DTLPWRO A  K   */
2899	{ SST(0x5E, 0x08, SS_RDEF,
2900	    "Idle-C condition activated by command") },
2901	/* DTLPWRO A  K   */
2902	{ SST(0x5E, 0x09, SS_RDEF,
2903	    "Standby-Y condition activated by timer") },
2904	/* DTLPWRO A  K   */
2905	{ SST(0x5E, 0x0A, SS_RDEF,
2906	    "Standby-Y condition activated by command") },
2907	/*           B    */
2908	{ SST(0x5E, 0x41, SS_RDEF,	/* XXX TBD */
2909	    "Power state change to active") },
2910	/*           B    */
2911	{ SST(0x5E, 0x42, SS_RDEF,	/* XXX TBD */
2912	    "Power state change to idle") },
2913	/*           B    */
2914	{ SST(0x5E, 0x43, SS_RDEF,	/* XXX TBD */
2915	    "Power state change to standby") },
2916	/*           B    */
2917	{ SST(0x5E, 0x45, SS_RDEF,	/* XXX TBD */
2918	    "Power state change to sleep") },
2919	/*           BK   */
2920	{ SST(0x5E, 0x47, SS_RDEF,	/* XXX TBD */
2921	    "Power state change to device control") },
2922	/*                */
2923	{ SST(0x60, 0x00, SS_RDEF,
2924	    "Lamp failure") },
2925	/*                */
2926	{ SST(0x61, 0x00, SS_RDEF,
2927	    "Video acquisition error") },
2928	/*                */
2929	{ SST(0x61, 0x01, SS_RDEF,
2930	    "Unable to acquire video") },
2931	/*                */
2932	{ SST(0x61, 0x02, SS_RDEF,
2933	    "Out of focus") },
2934	/*                */
2935	{ SST(0x62, 0x00, SS_RDEF,
2936	    "Scan head positioning error") },
2937	/*      R         */
2938	{ SST(0x63, 0x00, SS_RDEF,
2939	    "End of user area encountered on this track") },
2940	/*      R         */
2941	{ SST(0x63, 0x01, SS_FATAL | ENOSPC,
2942	    "Packet does not fit in available space") },
2943	/*      R         */
2944	{ SST(0x64, 0x00, SS_FATAL | ENXIO,
2945	    "Illegal mode for this track") },
2946	/*      R         */
2947	{ SST(0x64, 0x01, SS_RDEF,
2948	    "Invalid packet size") },
2949	/* DTLPWROMAEBKVF */
2950	{ SST(0x65, 0x00, SS_RDEF,
2951	    "Voltage fault") },
2952	/*                */
2953	{ SST(0x66, 0x00, SS_RDEF,
2954	    "Automatic document feeder cover up") },
2955	/*                */
2956	{ SST(0x66, 0x01, SS_RDEF,
2957	    "Automatic document feeder lift up") },
2958	/*                */
2959	{ SST(0x66, 0x02, SS_RDEF,
2960	    "Document jam in automatic document feeder") },
2961	/*                */
2962	{ SST(0x66, 0x03, SS_RDEF,
2963	    "Document miss feed automatic in document feeder") },
2964	/*         A      */
2965	{ SST(0x67, 0x00, SS_RDEF,
2966	    "Configuration failure") },
2967	/*         A      */
2968	{ SST(0x67, 0x01, SS_RDEF,
2969	    "Configuration of incapable logical units failed") },
2970	/*         A      */
2971	{ SST(0x67, 0x02, SS_RDEF,
2972	    "Add logical unit failed") },
2973	/*         A      */
2974	{ SST(0x67, 0x03, SS_RDEF,
2975	    "Modification of logical unit failed") },
2976	/*         A      */
2977	{ SST(0x67, 0x04, SS_RDEF,
2978	    "Exchange of logical unit failed") },
2979	/*         A      */
2980	{ SST(0x67, 0x05, SS_RDEF,
2981	    "Remove of logical unit failed") },
2982	/*         A      */
2983	{ SST(0x67, 0x06, SS_RDEF,
2984	    "Attachment of logical unit failed") },
2985	/*         A      */
2986	{ SST(0x67, 0x07, SS_RDEF,
2987	    "Creation of logical unit failed") },
2988	/*         A      */
2989	{ SST(0x67, 0x08, SS_RDEF,	/* XXX TBD */
2990	    "Assign failure occurred") },
2991	/*         A      */
2992	{ SST(0x67, 0x09, SS_RDEF,	/* XXX TBD */
2993	    "Multiply assigned logical unit") },
2994	/* DTLPWROMAEBKVF */
2995	{ SST(0x67, 0x0A, SS_RDEF,	/* XXX TBD */
2996	    "Set target port groups command failed") },
2997	/* DT        B    */
2998	{ SST(0x67, 0x0B, SS_RDEF,	/* XXX TBD */
2999	    "ATA device feature not enabled") },
3000	/*         A      */
3001	{ SST(0x68, 0x00, SS_RDEF,
3002	    "Logical unit not configured") },
3003	/* D              */
3004	{ SST(0x68, 0x01, SS_RDEF,
3005	    "Subsidiary logical unit not configured") },
3006	/*         A      */
3007	{ SST(0x69, 0x00, SS_RDEF,
3008	    "Data loss on logical unit") },
3009	/*         A      */
3010	{ SST(0x69, 0x01, SS_RDEF,
3011	    "Multiple logical unit failures") },
3012	/*         A      */
3013	{ SST(0x69, 0x02, SS_RDEF,
3014	    "Parity/data mismatch") },
3015	/*         A      */
3016	{ SST(0x6A, 0x00, SS_RDEF,
3017	    "Informational, refer to log") },
3018	/*         A      */
3019	{ SST(0x6B, 0x00, SS_RDEF,
3020	    "State change has occurred") },
3021	/*         A      */
3022	{ SST(0x6B, 0x01, SS_RDEF,
3023	    "Redundancy level got better") },
3024	/*         A      */
3025	{ SST(0x6B, 0x02, SS_RDEF,
3026	    "Redundancy level got worse") },
3027	/*         A      */
3028	{ SST(0x6C, 0x00, SS_RDEF,
3029	    "Rebuild failure occurred") },
3030	/*         A      */
3031	{ SST(0x6D, 0x00, SS_RDEF,
3032	    "Recalculate failure occurred") },
3033	/*         A      */
3034	{ SST(0x6E, 0x00, SS_RDEF,
3035	    "Command to logical unit failed") },
3036	/*      R         */
3037	{ SST(0x6F, 0x00, SS_RDEF,	/* XXX TBD */
3038	    "Copy protection key exchange failure - authentication failure") },
3039	/*      R         */
3040	{ SST(0x6F, 0x01, SS_RDEF,	/* XXX TBD */
3041	    "Copy protection key exchange failure - key not present") },
3042	/*      R         */
3043	{ SST(0x6F, 0x02, SS_RDEF,	/* XXX TBD */
3044	    "Copy protection key exchange failure - key not established") },
3045	/*      R         */
3046	{ SST(0x6F, 0x03, SS_RDEF,	/* XXX TBD */
3047	    "Read of scrambled sector without authentication") },
3048	/*      R         */
3049	{ SST(0x6F, 0x04, SS_RDEF,	/* XXX TBD */
3050	    "Media region code is mismatched to logical unit region") },
3051	/*      R         */
3052	{ SST(0x6F, 0x05, SS_RDEF,	/* XXX TBD */
3053	    "Drive region must be permanent/region reset count error") },
3054	/*      R         */
3055	{ SST(0x6F, 0x06, SS_RDEF,	/* XXX TBD */
3056	    "Insufficient block count for binding NONCE recording") },
3057	/*      R         */
3058	{ SST(0x6F, 0x07, SS_RDEF,	/* XXX TBD */
3059	    "Conflict in binding NONCE recording") },
3060	/*  T             */
3061	{ SST(0x70, 0x00, SS_RDEF,
3062	    "Decompression exception short: ASCQ = Algorithm ID") },
3063	/*  T             */
3064	{ SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
3065	    NULL) },			/* Range 0x00 -> 0xFF */
3066	/*  T             */
3067	{ SST(0x71, 0x00, SS_RDEF,
3068	    "Decompression exception long: ASCQ = Algorithm ID") },
3069	/*  T             */
3070	{ SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
3071	    NULL) },			/* Range 0x00 -> 0xFF */
3072	/*      R         */
3073	{ SST(0x72, 0x00, SS_RDEF,
3074	    "Session fixation error") },
3075	/*      R         */
3076	{ SST(0x72, 0x01, SS_RDEF,
3077	    "Session fixation error writing lead-in") },
3078	/*      R         */
3079	{ SST(0x72, 0x02, SS_RDEF,
3080	    "Session fixation error writing lead-out") },
3081	/*      R         */
3082	{ SST(0x72, 0x03, SS_RDEF,
3083	    "Session fixation error - incomplete track in session") },
3084	/*      R         */
3085	{ SST(0x72, 0x04, SS_RDEF,
3086	    "Empty or partially written reserved track") },
3087	/*      R         */
3088	{ SST(0x72, 0x05, SS_RDEF,	/* XXX TBD */
3089	    "No more track reservations allowed") },
3090	/*      R         */
3091	{ SST(0x72, 0x06, SS_RDEF,	/* XXX TBD */
3092	    "RMZ extension is not allowed") },
3093	/*      R         */
3094	{ SST(0x72, 0x07, SS_RDEF,	/* XXX TBD */
3095	    "No more test zone extensions are allowed") },
3096	/*      R         */
3097	{ SST(0x73, 0x00, SS_RDEF,
3098	    "CD control error") },
3099	/*      R         */
3100	{ SST(0x73, 0x01, SS_RDEF,
3101	    "Power calibration area almost full") },
3102	/*      R         */
3103	{ SST(0x73, 0x02, SS_FATAL | ENOSPC,
3104	    "Power calibration area is full") },
3105	/*      R         */
3106	{ SST(0x73, 0x03, SS_RDEF,
3107	    "Power calibration area error") },
3108	/*      R         */
3109	{ SST(0x73, 0x04, SS_RDEF,
3110	    "Program memory area update failure") },
3111	/*      R         */
3112	{ SST(0x73, 0x05, SS_RDEF,
3113	    "Program memory area is full") },
3114	/*      R         */
3115	{ SST(0x73, 0x06, SS_RDEF,	/* XXX TBD */
3116	    "RMA/PMA is almost full") },
3117	/*      R         */
3118	{ SST(0x73, 0x10, SS_RDEF,	/* XXX TBD */
3119	    "Current power calibration area almost full") },
3120	/*      R         */
3121	{ SST(0x73, 0x11, SS_RDEF,	/* XXX TBD */
3122	    "Current power calibration area is full") },
3123	/*      R         */
3124	{ SST(0x73, 0x17, SS_RDEF,	/* XXX TBD */
3125	    "RDZ is full") },
3126	/*  T             */
3127	{ SST(0x74, 0x00, SS_RDEF,	/* XXX TBD */
3128	    "Security error") },
3129	/*  T             */
3130	{ SST(0x74, 0x01, SS_RDEF,	/* XXX TBD */
3131	    "Unable to decrypt data") },
3132	/*  T             */
3133	{ SST(0x74, 0x02, SS_RDEF,	/* XXX TBD */
3134	    "Unencrypted data encountered while decrypting") },
3135	/*  T             */
3136	{ SST(0x74, 0x03, SS_RDEF,	/* XXX TBD */
3137	    "Incorrect data encryption key") },
3138	/*  T             */
3139	{ SST(0x74, 0x04, SS_RDEF,	/* XXX TBD */
3140	    "Cryptographic integrity validation failed") },
3141	/*  T             */
3142	{ SST(0x74, 0x05, SS_RDEF,	/* XXX TBD */
3143	    "Error decrypting data") },
3144	/*  T             */
3145	{ SST(0x74, 0x06, SS_RDEF,	/* XXX TBD */
3146	    "Unknown signature verification key") },
3147	/*  T             */
3148	{ SST(0x74, 0x07, SS_RDEF,	/* XXX TBD */
3149	    "Encryption parameters not useable") },
3150	/* DT   R M E  VF */
3151	{ SST(0x74, 0x08, SS_RDEF,	/* XXX TBD */
3152	    "Digital signature validation failure") },
3153	/*  T             */
3154	{ SST(0x74, 0x09, SS_RDEF,	/* XXX TBD */
3155	    "Encryption mode mismatch on read") },
3156	/*  T             */
3157	{ SST(0x74, 0x0A, SS_RDEF,	/* XXX TBD */
3158	    "Encrypted block not raw read enabled") },
3159	/*  T             */
3160	{ SST(0x74, 0x0B, SS_RDEF,	/* XXX TBD */
3161	    "Incorrect encryption parameters") },
3162	/* DT   R MAEBKV  */
3163	{ SST(0x74, 0x0C, SS_RDEF,	/* XXX TBD */
3164	    "Unable to decrypt parameter list") },
3165	/*  T             */
3166	{ SST(0x74, 0x0D, SS_RDEF,	/* XXX TBD */
3167	    "Encryption algorithm disabled") },
3168	/* DT   R MAEBKV  */
3169	{ SST(0x74, 0x10, SS_RDEF,	/* XXX TBD */
3170	    "SA creation parameter value invalid") },
3171	/* DT   R MAEBKV  */
3172	{ SST(0x74, 0x11, SS_RDEF,	/* XXX TBD */
3173	    "SA creation parameter value rejected") },
3174	/* DT   R MAEBKV  */
3175	{ SST(0x74, 0x12, SS_RDEF,	/* XXX TBD */
3176	    "Invalid SA usage") },
3177	/*  T             */
3178	{ SST(0x74, 0x21, SS_RDEF,	/* XXX TBD */
3179	    "Data encryption configuration prevented") },
3180	/* DT   R MAEBKV  */
3181	{ SST(0x74, 0x30, SS_RDEF,	/* XXX TBD */
3182	    "SA creation parameter not supported") },
3183	/* DT   R MAEBKV  */
3184	{ SST(0x74, 0x40, SS_RDEF,	/* XXX TBD */
3185	    "Authentication failed") },
3186	/*             V  */
3187	{ SST(0x74, 0x61, SS_RDEF,	/* XXX TBD */
3188	    "External data encryption key manager access error") },
3189	/*             V  */
3190	{ SST(0x74, 0x62, SS_RDEF,	/* XXX TBD */
3191	    "External data encryption key manager error") },
3192	/*             V  */
3193	{ SST(0x74, 0x63, SS_RDEF,	/* XXX TBD */
3194	    "External data encryption key not found") },
3195	/*             V  */
3196	{ SST(0x74, 0x64, SS_RDEF,	/* XXX TBD */
3197	    "External data encryption request not authorized") },
3198	/*  T             */
3199	{ SST(0x74, 0x6E, SS_RDEF,	/* XXX TBD */
3200	    "External data encryption control timeout") },
3201	/*  T             */
3202	{ SST(0x74, 0x6F, SS_RDEF,	/* XXX TBD */
3203	    "External data encryption control error") },
3204	/* DT   R M E  V  */
3205	{ SST(0x74, 0x71, SS_FATAL | EACCES,
3206	    "Logical unit access not authorized") },
3207	/* D              */
3208	{ SST(0x74, 0x79, SS_FATAL | EACCES,
3209	    "Security conflict in translated device") }
3210};
3211
3212const u_int asc_table_size = nitems(asc_table);
3213
3214struct asc_key
3215{
3216	int asc;
3217	int ascq;
3218};
3219
3220static int
3221ascentrycomp(const void *key, const void *member)
3222{
3223	int asc;
3224	int ascq;
3225	const struct asc_table_entry *table_entry;
3226
3227	asc = ((const struct asc_key *)key)->asc;
3228	ascq = ((const struct asc_key *)key)->ascq;
3229	table_entry = (const struct asc_table_entry *)member;
3230
3231	if (asc >= table_entry->asc) {
3232		if (asc > table_entry->asc)
3233			return (1);
3234
3235		if (ascq <= table_entry->ascq) {
3236			/* Check for ranges */
3237			if (ascq == table_entry->ascq
3238		 	 || ((table_entry->action & SSQ_RANGE) != 0
3239		  	   && ascq >= (table_entry - 1)->ascq))
3240				return (0);
3241			return (-1);
3242		}
3243		return (1);
3244	}
3245	return (-1);
3246}
3247
3248static int
3249senseentrycomp(const void *key, const void *member)
3250{
3251	int sense_key;
3252	const struct sense_key_table_entry *table_entry;
3253
3254	sense_key = *((const int *)key);
3255	table_entry = (const struct sense_key_table_entry *)member;
3256
3257	if (sense_key >= table_entry->sense_key) {
3258		if (sense_key == table_entry->sense_key)
3259			return (0);
3260		return (1);
3261	}
3262	return (-1);
3263}
3264
3265static void
3266fetchtableentries(int sense_key, int asc, int ascq,
3267		  struct scsi_inquiry_data *inq_data,
3268		  const struct sense_key_table_entry **sense_entry,
3269		  const struct asc_table_entry **asc_entry)
3270{
3271	caddr_t match;
3272	const struct asc_table_entry *asc_tables[2];
3273	const struct sense_key_table_entry *sense_tables[2];
3274	struct asc_key asc_ascq;
3275	size_t asc_tables_size[2];
3276	size_t sense_tables_size[2];
3277	int num_asc_tables;
3278	int num_sense_tables;
3279	int i;
3280
3281	/* Default to failure */
3282	*sense_entry = NULL;
3283	*asc_entry = NULL;
3284	match = NULL;
3285	if (inq_data != NULL)
3286		match = cam_quirkmatch((caddr_t)inq_data,
3287				       (caddr_t)sense_quirk_table,
3288				       sense_quirk_table_size,
3289				       sizeof(*sense_quirk_table),
3290				       scsi_inquiry_match);
3291
3292	if (match != NULL) {
3293		struct scsi_sense_quirk_entry *quirk;
3294
3295		quirk = (struct scsi_sense_quirk_entry *)match;
3296		asc_tables[0] = quirk->asc_info;
3297		asc_tables_size[0] = quirk->num_ascs;
3298		asc_tables[1] = asc_table;
3299		asc_tables_size[1] = asc_table_size;
3300		num_asc_tables = 2;
3301		sense_tables[0] = quirk->sense_key_info;
3302		sense_tables_size[0] = quirk->num_sense_keys;
3303		sense_tables[1] = sense_key_table;
3304		sense_tables_size[1] = nitems(sense_key_table);
3305		num_sense_tables = 2;
3306	} else {
3307		asc_tables[0] = asc_table;
3308		asc_tables_size[0] = asc_table_size;
3309		num_asc_tables = 1;
3310		sense_tables[0] = sense_key_table;
3311		sense_tables_size[0] = nitems(sense_key_table);
3312		num_sense_tables = 1;
3313	}
3314
3315	asc_ascq.asc = asc;
3316	asc_ascq.ascq = ascq;
3317	for (i = 0; i < num_asc_tables; i++) {
3318		void *found_entry;
3319
3320		found_entry = bsearch(&asc_ascq, asc_tables[i],
3321				      asc_tables_size[i],
3322				      sizeof(**asc_tables),
3323				      ascentrycomp);
3324
3325		if (found_entry) {
3326			*asc_entry = (struct asc_table_entry *)found_entry;
3327			break;
3328		}
3329	}
3330
3331	for (i = 0; i < num_sense_tables; i++) {
3332		void *found_entry;
3333
3334		found_entry = bsearch(&sense_key, sense_tables[i],
3335				      sense_tables_size[i],
3336				      sizeof(**sense_tables),
3337				      senseentrycomp);
3338
3339		if (found_entry) {
3340			*sense_entry =
3341			    (struct sense_key_table_entry *)found_entry;
3342			break;
3343		}
3344	}
3345}
3346
3347void
3348scsi_sense_desc(int sense_key, int asc, int ascq,
3349		struct scsi_inquiry_data *inq_data,
3350		const char **sense_key_desc, const char **asc_desc)
3351{
3352	const struct asc_table_entry *asc_entry;
3353	const struct sense_key_table_entry *sense_entry;
3354
3355	fetchtableentries(sense_key, asc, ascq,
3356			  inq_data,
3357			  &sense_entry,
3358			  &asc_entry);
3359
3360	if (sense_entry != NULL)
3361		*sense_key_desc = sense_entry->desc;
3362	else
3363		*sense_key_desc = "Invalid Sense Key";
3364
3365	if (asc_entry != NULL)
3366		*asc_desc = asc_entry->desc;
3367	else if (asc >= 0x80 && asc <= 0xff)
3368		*asc_desc = "Vendor Specific ASC";
3369	else if (ascq >= 0x80 && ascq <= 0xff)
3370		*asc_desc = "Vendor Specific ASCQ";
3371	else
3372		*asc_desc = "Reserved ASC/ASCQ pair";
3373}
3374
3375/*
3376 * Given sense and device type information, return the appropriate action.
3377 * If we do not understand the specific error as identified by the ASC/ASCQ
3378 * pair, fall back on the more generic actions derived from the sense key.
3379 */
3380scsi_sense_action
3381scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
3382		  u_int32_t sense_flags)
3383{
3384	const struct asc_table_entry *asc_entry;
3385	const struct sense_key_table_entry *sense_entry;
3386	int error_code, sense_key, asc, ascq;
3387	scsi_sense_action action;
3388
3389	if (!scsi_extract_sense_ccb((union ccb *)csio,
3390	    &error_code, &sense_key, &asc, &ascq)) {
3391		action = SS_RDEF;
3392	} else if ((error_code == SSD_DEFERRED_ERROR)
3393	 || (error_code == SSD_DESC_DEFERRED_ERROR)) {
3394		/*
3395		 * XXX dufault@FreeBSD.org
3396		 * This error doesn't relate to the command associated
3397		 * with this request sense.  A deferred error is an error
3398		 * for a command that has already returned GOOD status
3399		 * (see SCSI2 8.2.14.2).
3400		 *
3401		 * By my reading of that section, it looks like the current
3402		 * command has been cancelled, we should now clean things up
3403		 * (hopefully recovering any lost data) and then retry the
3404		 * current command.  There are two easy choices, both wrong:
3405		 *
3406		 * 1. Drop through (like we had been doing), thus treating
3407		 *    this as if the error were for the current command and
3408		 *    return and stop the current command.
3409		 *
3410		 * 2. Issue a retry (like I made it do) thus hopefully
3411		 *    recovering the current transfer, and ignoring the
3412		 *    fact that we've dropped a command.
3413		 *
3414		 * These should probably be handled in a device specific
3415		 * sense handler or punted back up to a user mode daemon
3416		 */
3417		action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3418	} else {
3419		fetchtableentries(sense_key, asc, ascq,
3420				  inq_data,
3421				  &sense_entry,
3422				  &asc_entry);
3423
3424		/*
3425		 * Override the 'No additional Sense' entry (0,0)
3426		 * with the error action of the sense key.
3427		 */
3428		if (asc_entry != NULL
3429		 && (asc != 0 || ascq != 0))
3430			action = asc_entry->action;
3431		else if (sense_entry != NULL)
3432			action = sense_entry->action;
3433		else
3434			action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3435
3436		if (sense_key == SSD_KEY_RECOVERED_ERROR) {
3437			/*
3438			 * The action succeeded but the device wants
3439			 * the user to know that some recovery action
3440			 * was required.
3441			 */
3442			action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
3443			action |= SS_NOP|SSQ_PRINT_SENSE;
3444		} else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
3445			if ((sense_flags & SF_QUIET_IR) != 0)
3446				action &= ~SSQ_PRINT_SENSE;
3447		} else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
3448			if ((sense_flags & SF_RETRY_UA) != 0
3449			 && (action & SS_MASK) == SS_FAIL) {
3450				action &= ~(SS_MASK|SSQ_MASK);
3451				action |= SS_RETRY|SSQ_DECREMENT_COUNT|
3452					  SSQ_PRINT_SENSE;
3453			}
3454			action |= SSQ_UA;
3455		}
3456	}
3457	if ((action & SS_MASK) >= SS_START &&
3458	    (sense_flags & SF_NO_RECOVERY)) {
3459		action &= ~SS_MASK;
3460		action |= SS_FAIL;
3461	} else if ((action & SS_MASK) == SS_RETRY &&
3462	    (sense_flags & SF_NO_RETRY)) {
3463		action &= ~SS_MASK;
3464		action |= SS_FAIL;
3465	}
3466	if ((sense_flags & SF_PRINT_ALWAYS) != 0)
3467		action |= SSQ_PRINT_SENSE;
3468	else if ((sense_flags & SF_NO_PRINT) != 0)
3469		action &= ~SSQ_PRINT_SENSE;
3470
3471	return (action);
3472}
3473
3474char *
3475scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
3476{
3477	struct sbuf sb;
3478	int error;
3479
3480	if (len == 0)
3481		return ("");
3482
3483	sbuf_new(&sb, cdb_string, len, SBUF_FIXEDLEN);
3484
3485	scsi_cdb_sbuf(cdb_ptr, &sb);
3486
3487	/* ENOMEM just means that the fixed buffer is full, OK to ignore */
3488	error = sbuf_finish(&sb);
3489	if (error != 0 && error != ENOMEM)
3490		return ("");
3491
3492	return(sbuf_data(&sb));
3493}
3494
3495void
3496scsi_cdb_sbuf(u_int8_t *cdb_ptr, struct sbuf *sb)
3497{
3498	u_int8_t cdb_len;
3499	int i;
3500
3501	if (cdb_ptr == NULL)
3502		return;
3503
3504	/*
3505	 * This is taken from the SCSI-3 draft spec.
3506	 * (T10/1157D revision 0.3)
3507	 * The top 3 bits of an opcode are the group code.  The next 5 bits
3508	 * are the command code.
3509	 * Group 0:  six byte commands
3510	 * Group 1:  ten byte commands
3511	 * Group 2:  ten byte commands
3512	 * Group 3:  reserved
3513	 * Group 4:  sixteen byte commands
3514	 * Group 5:  twelve byte commands
3515	 * Group 6:  vendor specific
3516	 * Group 7:  vendor specific
3517	 */
3518	switch((*cdb_ptr >> 5) & 0x7) {
3519		case 0:
3520			cdb_len = 6;
3521			break;
3522		case 1:
3523		case 2:
3524			cdb_len = 10;
3525			break;
3526		case 3:
3527		case 6:
3528		case 7:
3529			/* in this case, just print out the opcode */
3530			cdb_len = 1;
3531			break;
3532		case 4:
3533			cdb_len = 16;
3534			break;
3535		case 5:
3536			cdb_len = 12;
3537			break;
3538	}
3539
3540	for (i = 0; i < cdb_len; i++)
3541		sbuf_printf(sb, "%02hhx ", cdb_ptr[i]);
3542
3543	return;
3544}
3545
3546const char *
3547scsi_status_string(struct ccb_scsiio *csio)
3548{
3549	switch(csio->scsi_status) {
3550	case SCSI_STATUS_OK:
3551		return("OK");
3552	case SCSI_STATUS_CHECK_COND:
3553		return("Check Condition");
3554	case SCSI_STATUS_BUSY:
3555		return("Busy");
3556	case SCSI_STATUS_INTERMED:
3557		return("Intermediate");
3558	case SCSI_STATUS_INTERMED_COND_MET:
3559		return("Intermediate-Condition Met");
3560	case SCSI_STATUS_RESERV_CONFLICT:
3561		return("Reservation Conflict");
3562	case SCSI_STATUS_CMD_TERMINATED:
3563		return("Command Terminated");
3564	case SCSI_STATUS_QUEUE_FULL:
3565		return("Queue Full");
3566	case SCSI_STATUS_ACA_ACTIVE:
3567		return("ACA Active");
3568	case SCSI_STATUS_TASK_ABORTED:
3569		return("Task Aborted");
3570	default: {
3571		static char unkstr[64];
3572		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
3573			 csio->scsi_status);
3574		return(unkstr);
3575	}
3576	}
3577}
3578
3579/*
3580 * scsi_command_string() returns 0 for success and -1 for failure.
3581 */
3582#ifdef _KERNEL
3583int
3584scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
3585#else /* !_KERNEL */
3586int
3587scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio,
3588		    struct sbuf *sb)
3589#endif /* _KERNEL/!_KERNEL */
3590{
3591	struct scsi_inquiry_data *inq_data;
3592#ifdef _KERNEL
3593	struct	  ccb_getdev *cgd;
3594#endif /* _KERNEL */
3595
3596#ifdef _KERNEL
3597	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3598		return(-1);
3599	/*
3600	 * Get the device information.
3601	 */
3602	xpt_setup_ccb(&cgd->ccb_h,
3603		      csio->ccb_h.path,
3604		      CAM_PRIORITY_NORMAL);
3605	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3606	xpt_action((union ccb *)cgd);
3607
3608	/*
3609	 * If the device is unconfigured, just pretend that it is a hard
3610	 * drive.  scsi_op_desc() needs this.
3611	 */
3612	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3613		cgd->inq_data.device = T_DIRECT;
3614
3615	inq_data = &cgd->inq_data;
3616
3617#else /* !_KERNEL */
3618
3619	inq_data = &device->inq_data;
3620
3621#endif /* _KERNEL/!_KERNEL */
3622
3623	sbuf_printf(sb, "%s. CDB: ",
3624		    scsi_op_desc(scsiio_cdb_ptr(csio)[0], inq_data));
3625	scsi_cdb_sbuf(scsiio_cdb_ptr(csio), sb);
3626
3627#ifdef _KERNEL
3628	xpt_free_ccb((union ccb *)cgd);
3629#endif
3630
3631	return(0);
3632}
3633
3634/*
3635 * Iterate over sense descriptors.  Each descriptor is passed into iter_func().
3636 * If iter_func() returns 0, list traversal continues.  If iter_func()
3637 * returns non-zero, list traversal is stopped.
3638 */
3639void
3640scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
3641		  int (*iter_func)(struct scsi_sense_data_desc *sense,
3642				   u_int, struct scsi_sense_desc_header *,
3643				   void *), void *arg)
3644{
3645	int cur_pos;
3646	int desc_len;
3647
3648	/*
3649	 * First make sure the extra length field is present.
3650	 */
3651	if (SSD_DESC_IS_PRESENT(sense, sense_len, extra_len) == 0)
3652		return;
3653
3654	/*
3655	 * The length of data actually returned may be different than the
3656	 * extra_len recorded in the structure.
3657	 */
3658	desc_len = sense_len -offsetof(struct scsi_sense_data_desc, sense_desc);
3659
3660	/*
3661	 * Limit this further by the extra length reported, and the maximum
3662	 * allowed extra length.
3663	 */
3664	desc_len = MIN(desc_len, MIN(sense->extra_len, SSD_EXTRA_MAX));
3665
3666	/*
3667	 * Subtract the size of the header from the descriptor length.
3668	 * This is to ensure that we have at least the header left, so we
3669	 * don't have to check that inside the loop.  This can wind up
3670	 * being a negative value.
3671	 */
3672	desc_len -= sizeof(struct scsi_sense_desc_header);
3673
3674	for (cur_pos = 0; cur_pos < desc_len;) {
3675		struct scsi_sense_desc_header *header;
3676
3677		header = (struct scsi_sense_desc_header *)
3678			&sense->sense_desc[cur_pos];
3679
3680		/*
3681		 * Check to make sure we have the entire descriptor.  We
3682		 * don't call iter_func() unless we do.
3683		 *
3684		 * Note that although cur_pos is at the beginning of the
3685		 * descriptor, desc_len already has the header length
3686		 * subtracted.  So the comparison of the length in the
3687		 * header (which does not include the header itself) to
3688		 * desc_len - cur_pos is correct.
3689		 */
3690		if (header->length > (desc_len - cur_pos))
3691			break;
3692
3693		if (iter_func(sense, sense_len, header, arg) != 0)
3694			break;
3695
3696		cur_pos += sizeof(*header) + header->length;
3697	}
3698}
3699
3700struct scsi_find_desc_info {
3701	uint8_t desc_type;
3702	struct scsi_sense_desc_header *header;
3703};
3704
3705static int
3706scsi_find_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
3707		    struct scsi_sense_desc_header *header, void *arg)
3708{
3709	struct scsi_find_desc_info *desc_info;
3710
3711	desc_info = (struct scsi_find_desc_info *)arg;
3712
3713	if (header->desc_type == desc_info->desc_type) {
3714		desc_info->header = header;
3715
3716		/* We found the descriptor, tell the iterator to stop. */
3717		return (1);
3718	} else
3719		return (0);
3720}
3721
3722/*
3723 * Given a descriptor type, return a pointer to it if it is in the sense
3724 * data and not truncated.  Avoiding truncating sense data will simplify
3725 * things significantly for the caller.
3726 */
3727uint8_t *
3728scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
3729	       uint8_t desc_type)
3730{
3731	struct scsi_find_desc_info desc_info;
3732
3733	desc_info.desc_type = desc_type;
3734	desc_info.header = NULL;
3735
3736	scsi_desc_iterate(sense, sense_len, scsi_find_desc_func, &desc_info);
3737
3738	return ((uint8_t *)desc_info.header);
3739}
3740
3741/*
3742 * Fill in SCSI descriptor sense data with the specified parameters.
3743 */
3744static void
3745scsi_set_sense_data_desc_va(struct scsi_sense_data *sense_data,
3746    u_int *sense_len, scsi_sense_data_type sense_format, int current_error,
3747    int sense_key, int asc, int ascq, va_list ap)
3748{
3749	struct scsi_sense_data_desc *sense;
3750	scsi_sense_elem_type elem_type;
3751	int space, len;
3752	uint8_t *desc, *data;
3753
3754	memset(sense_data, 0, sizeof(*sense_data));
3755	sense = (struct scsi_sense_data_desc *)sense_data;
3756	if (current_error != 0)
3757		sense->error_code = SSD_DESC_CURRENT_ERROR;
3758	else
3759		sense->error_code = SSD_DESC_DEFERRED_ERROR;
3760	sense->sense_key = sense_key;
3761	sense->add_sense_code = asc;
3762	sense->add_sense_code_qual = ascq;
3763	sense->flags = 0;
3764
3765	desc = &sense->sense_desc[0];
3766	space = *sense_len - offsetof(struct scsi_sense_data_desc, sense_desc);
3767	while ((elem_type = va_arg(ap, scsi_sense_elem_type)) !=
3768	    SSD_ELEM_NONE) {
3769		if (elem_type >= SSD_ELEM_MAX) {
3770			printf("%s: invalid sense type %d\n", __func__,
3771			       elem_type);
3772			break;
3773		}
3774		len = va_arg(ap, int);
3775		data = va_arg(ap, uint8_t *);
3776
3777		switch (elem_type) {
3778		case SSD_ELEM_SKIP:
3779			break;
3780		case SSD_ELEM_DESC:
3781			if (space < len) {
3782				sense->flags |= SSDD_SDAT_OVFL;
3783				break;
3784			}
3785			bcopy(data, desc, len);
3786			desc += len;
3787			space -= len;
3788			break;
3789		case SSD_ELEM_SKS: {
3790			struct scsi_sense_sks *sks = (void *)desc;
3791
3792			if (len > sizeof(sks->sense_key_spec))
3793				break;
3794			if (space < sizeof(*sks)) {
3795				sense->flags |= SSDD_SDAT_OVFL;
3796				break;
3797			}
3798			sks->desc_type = SSD_DESC_SKS;
3799			sks->length = sizeof(*sks) -
3800			    (offsetof(struct scsi_sense_sks, length) + 1);
3801			bcopy(data, &sks->sense_key_spec, len);
3802			desc += sizeof(*sks);
3803			space -= sizeof(*sks);
3804			break;
3805		}
3806		case SSD_ELEM_COMMAND: {
3807			struct scsi_sense_command *cmd = (void *)desc;
3808
3809			if (len > sizeof(cmd->command_info))
3810				break;
3811			if (space < sizeof(*cmd)) {
3812				sense->flags |= SSDD_SDAT_OVFL;
3813				break;
3814			}
3815			cmd->desc_type = SSD_DESC_COMMAND;
3816			cmd->length = sizeof(*cmd) -
3817			    (offsetof(struct scsi_sense_command, length) + 1);
3818			bcopy(data, &cmd->command_info[
3819			    sizeof(cmd->command_info) - len], len);
3820			desc += sizeof(*cmd);
3821			space -= sizeof(*cmd);
3822			break;
3823		}
3824		case SSD_ELEM_INFO: {
3825			struct scsi_sense_info *info = (void *)desc;
3826
3827			if (len > sizeof(info->info))
3828				break;
3829			if (space < sizeof(*info)) {
3830				sense->flags |= SSDD_SDAT_OVFL;
3831				break;
3832			}
3833			info->desc_type = SSD_DESC_INFO;
3834			info->length = sizeof(*info) -
3835			    (offsetof(struct scsi_sense_info, length) + 1);
3836			info->byte2 = SSD_INFO_VALID;
3837			bcopy(data, &info->info[sizeof(info->info) - len], len);
3838			desc += sizeof(*info);
3839			space -= sizeof(*info);
3840			break;
3841		}
3842		case SSD_ELEM_FRU: {
3843			struct scsi_sense_fru *fru = (void *)desc;
3844
3845			if (len > sizeof(fru->fru))
3846				break;
3847			if (space < sizeof(*fru)) {
3848				sense->flags |= SSDD_SDAT_OVFL;
3849				break;
3850			}
3851			fru->desc_type = SSD_DESC_FRU;
3852			fru->length = sizeof(*fru) -
3853			    (offsetof(struct scsi_sense_fru, length) + 1);
3854			fru->fru = *data;
3855			desc += sizeof(*fru);
3856			space -= sizeof(*fru);
3857			break;
3858		}
3859		case SSD_ELEM_STREAM: {
3860			struct scsi_sense_stream *stream = (void *)desc;
3861
3862			if (len > sizeof(stream->byte3))
3863				break;
3864			if (space < sizeof(*stream)) {
3865				sense->flags |= SSDD_SDAT_OVFL;
3866				break;
3867			}
3868			stream->desc_type = SSD_DESC_STREAM;
3869			stream->length = sizeof(*stream) -
3870			    (offsetof(struct scsi_sense_stream, length) + 1);
3871			stream->byte3 = *data;
3872			desc += sizeof(*stream);
3873			space -= sizeof(*stream);
3874			break;
3875		}
3876		default:
3877			/*
3878			 * We shouldn't get here, but if we do, do nothing.
3879			 * We've already consumed the arguments above.
3880			 */
3881			break;
3882		}
3883	}
3884	sense->extra_len = desc - &sense->sense_desc[0];
3885	*sense_len = offsetof(struct scsi_sense_data_desc, extra_len) + 1 +
3886	    sense->extra_len;
3887}
3888
3889/*
3890 * Fill in SCSI fixed sense data with the specified parameters.
3891 */
3892static void
3893scsi_set_sense_data_fixed_va(struct scsi_sense_data *sense_data,
3894    u_int *sense_len, scsi_sense_data_type sense_format, int current_error,
3895    int sense_key, int asc, int ascq, va_list ap)
3896{
3897	struct scsi_sense_data_fixed *sense;
3898	scsi_sense_elem_type elem_type;
3899	uint8_t *data;
3900	int len;
3901
3902	memset(sense_data, 0, sizeof(*sense_data));
3903	sense = (struct scsi_sense_data_fixed *)sense_data;
3904	if (current_error != 0)
3905		sense->error_code = SSD_CURRENT_ERROR;
3906	else
3907		sense->error_code = SSD_DEFERRED_ERROR;
3908	sense->flags = sense_key & SSD_KEY;
3909	sense->extra_len = 0;
3910	if (*sense_len >= 13) {
3911		sense->add_sense_code = asc;
3912		sense->extra_len = MAX(sense->extra_len, 5);
3913	} else
3914		sense->flags |= SSD_SDAT_OVFL;
3915	if (*sense_len >= 14) {
3916		sense->add_sense_code_qual = ascq;
3917		sense->extra_len = MAX(sense->extra_len, 6);
3918	} else
3919		sense->flags |= SSD_SDAT_OVFL;
3920
3921	while ((elem_type = va_arg(ap, scsi_sense_elem_type)) !=
3922	    SSD_ELEM_NONE) {
3923		if (elem_type >= SSD_ELEM_MAX) {
3924			printf("%s: invalid sense type %d\n", __func__,
3925			       elem_type);
3926			break;
3927		}
3928		len = va_arg(ap, int);
3929		data = va_arg(ap, uint8_t *);
3930
3931		switch (elem_type) {
3932		case SSD_ELEM_SKIP:
3933			break;
3934		case SSD_ELEM_SKS:
3935			if (len > sizeof(sense->sense_key_spec))
3936				break;
3937			if (*sense_len < 18) {
3938				sense->flags |= SSD_SDAT_OVFL;
3939				break;
3940			}
3941			bcopy(data, &sense->sense_key_spec[0], len);
3942			sense->extra_len = MAX(sense->extra_len, 10);
3943			break;
3944		case SSD_ELEM_COMMAND:
3945			if (*sense_len < 12) {
3946				sense->flags |= SSD_SDAT_OVFL;
3947				break;
3948			}
3949			if (len > sizeof(sense->cmd_spec_info)) {
3950				data += len - sizeof(sense->cmd_spec_info);
3951				len = sizeof(sense->cmd_spec_info);
3952			}
3953			bcopy(data, &sense->cmd_spec_info[
3954			    sizeof(sense->cmd_spec_info) - len], len);
3955			sense->extra_len = MAX(sense->extra_len, 4);
3956			break;
3957		case SSD_ELEM_INFO:
3958			/* Set VALID bit only if no overflow. */
3959			sense->error_code |= SSD_ERRCODE_VALID;
3960			while (len > sizeof(sense->info)) {
3961				if (data[0] != 0)
3962					sense->error_code &= ~SSD_ERRCODE_VALID;
3963				data ++;
3964				len --;
3965			}
3966			bcopy(data, &sense->info[sizeof(sense->info) - len], len);
3967			break;
3968		case SSD_ELEM_FRU:
3969			if (*sense_len < 15) {
3970				sense->flags |= SSD_SDAT_OVFL;
3971				break;
3972			}
3973			sense->fru = *data;
3974			sense->extra_len = MAX(sense->extra_len, 7);
3975			break;
3976		case SSD_ELEM_STREAM:
3977			sense->flags |= *data &
3978			    (SSD_ILI | SSD_EOM | SSD_FILEMARK);
3979			break;
3980		default:
3981
3982			/*
3983			 * We can't handle that in fixed format.  Skip it.
3984			 */
3985			break;
3986		}
3987	}
3988	*sense_len = offsetof(struct scsi_sense_data_fixed, extra_len) + 1 +
3989	    sense->extra_len;
3990}
3991
3992/*
3993 * Fill in SCSI sense data with the specified parameters.  This routine can
3994 * fill in either fixed or descriptor type sense data.
3995 */
3996void
3997scsi_set_sense_data_va(struct scsi_sense_data *sense_data, u_int *sense_len,
3998		      scsi_sense_data_type sense_format, int current_error,
3999		      int sense_key, int asc, int ascq, va_list ap)
4000{
4001
4002	if (*sense_len > SSD_FULL_SIZE)
4003		*sense_len = SSD_FULL_SIZE;
4004	if (sense_format == SSD_TYPE_DESC)
4005		scsi_set_sense_data_desc_va(sense_data, sense_len,
4006		    sense_format, current_error, sense_key, asc, ascq, ap);
4007	else
4008		scsi_set_sense_data_fixed_va(sense_data, sense_len,
4009		    sense_format, current_error, sense_key, asc, ascq, ap);
4010}
4011
4012void
4013scsi_set_sense_data(struct scsi_sense_data *sense_data,
4014		    scsi_sense_data_type sense_format, int current_error,
4015		    int sense_key, int asc, int ascq, ...)
4016{
4017	va_list ap;
4018	u_int	sense_len = SSD_FULL_SIZE;
4019
4020	va_start(ap, ascq);
4021	scsi_set_sense_data_va(sense_data, &sense_len, sense_format,
4022	    current_error, sense_key, asc, ascq, ap);
4023	va_end(ap);
4024}
4025
4026void
4027scsi_set_sense_data_len(struct scsi_sense_data *sense_data, u_int *sense_len,
4028		    scsi_sense_data_type sense_format, int current_error,
4029		    int sense_key, int asc, int ascq, ...)
4030{
4031	va_list ap;
4032
4033	va_start(ap, ascq);
4034	scsi_set_sense_data_va(sense_data, sense_len, sense_format,
4035	    current_error, sense_key, asc, ascq, ap);
4036	va_end(ap);
4037}
4038
4039/*
4040 * Get sense information for three similar sense data types.
4041 */
4042int
4043scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
4044		    uint8_t info_type, uint64_t *info, int64_t *signed_info)
4045{
4046	scsi_sense_data_type sense_type;
4047
4048	if (sense_len == 0)
4049		goto bailout;
4050
4051	sense_type = scsi_sense_type(sense_data);
4052
4053	switch (sense_type) {
4054	case SSD_TYPE_DESC: {
4055		struct scsi_sense_data_desc *sense;
4056		uint8_t *desc;
4057
4058		sense = (struct scsi_sense_data_desc *)sense_data;
4059
4060		desc = scsi_find_desc(sense, sense_len, info_type);
4061		if (desc == NULL)
4062			goto bailout;
4063
4064		switch (info_type) {
4065		case SSD_DESC_INFO: {
4066			struct scsi_sense_info *info_desc;
4067
4068			info_desc = (struct scsi_sense_info *)desc;
4069
4070			if ((info_desc->byte2 & SSD_INFO_VALID) == 0)
4071				goto bailout;
4072
4073			*info = scsi_8btou64(info_desc->info);
4074			if (signed_info != NULL)
4075				*signed_info = *info;
4076			break;
4077		}
4078		case SSD_DESC_COMMAND: {
4079			struct scsi_sense_command *cmd_desc;
4080
4081			cmd_desc = (struct scsi_sense_command *)desc;
4082
4083			*info = scsi_8btou64(cmd_desc->command_info);
4084			if (signed_info != NULL)
4085				*signed_info = *info;
4086			break;
4087		}
4088		case SSD_DESC_FRU: {
4089			struct scsi_sense_fru *fru_desc;
4090
4091			fru_desc = (struct scsi_sense_fru *)desc;
4092
4093			if (fru_desc->fru == 0)
4094				goto bailout;
4095
4096			*info = fru_desc->fru;
4097			if (signed_info != NULL)
4098				*signed_info = (int8_t)fru_desc->fru;
4099			break;
4100		}
4101		default:
4102			goto bailout;
4103			break;
4104		}
4105		break;
4106	}
4107	case SSD_TYPE_FIXED: {
4108		struct scsi_sense_data_fixed *sense;
4109
4110		sense = (struct scsi_sense_data_fixed *)sense_data;
4111
4112		switch (info_type) {
4113		case SSD_DESC_INFO: {
4114			uint32_t info_val;
4115
4116			if ((sense->error_code & SSD_ERRCODE_VALID) == 0)
4117				goto bailout;
4118
4119			if (SSD_FIXED_IS_PRESENT(sense, sense_len, info) == 0)
4120				goto bailout;
4121
4122			info_val = scsi_4btoul(sense->info);
4123
4124			*info = info_val;
4125			if (signed_info != NULL)
4126				*signed_info = (int32_t)info_val;
4127			break;
4128		}
4129		case SSD_DESC_COMMAND: {
4130			uint32_t cmd_val;
4131
4132			if ((SSD_FIXED_IS_PRESENT(sense, sense_len,
4133			     cmd_spec_info) == 0)
4134			 || (SSD_FIXED_IS_FILLED(sense, cmd_spec_info) == 0))
4135				goto bailout;
4136
4137			cmd_val = scsi_4btoul(sense->cmd_spec_info);
4138			if (cmd_val == 0)
4139				goto bailout;
4140
4141			*info = cmd_val;
4142			if (signed_info != NULL)
4143				*signed_info = (int32_t)cmd_val;
4144			break;
4145		}
4146		case SSD_DESC_FRU:
4147			if ((SSD_FIXED_IS_PRESENT(sense, sense_len, fru) == 0)
4148			 || (SSD_FIXED_IS_FILLED(sense, fru) == 0))
4149				goto bailout;
4150
4151			if (sense->fru == 0)
4152				goto bailout;
4153
4154			*info = sense->fru;
4155			if (signed_info != NULL)
4156				*signed_info = (int8_t)sense->fru;
4157			break;
4158		default:
4159			goto bailout;
4160			break;
4161		}
4162		break;
4163	}
4164	default:
4165		goto bailout;
4166		break;
4167	}
4168
4169	return (0);
4170bailout:
4171	return (1);
4172}
4173
4174int
4175scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len, uint8_t *sks)
4176{
4177	scsi_sense_data_type sense_type;
4178
4179	if (sense_len == 0)
4180		goto bailout;
4181
4182	sense_type = scsi_sense_type(sense_data);
4183
4184	switch (sense_type) {
4185	case SSD_TYPE_DESC: {
4186		struct scsi_sense_data_desc *sense;
4187		struct scsi_sense_sks *desc;
4188
4189		sense = (struct scsi_sense_data_desc *)sense_data;
4190
4191		desc = (struct scsi_sense_sks *)scsi_find_desc(sense, sense_len,
4192							       SSD_DESC_SKS);
4193		if (desc == NULL)
4194			goto bailout;
4195
4196		if ((desc->sense_key_spec[0] & SSD_SKS_VALID) == 0)
4197			goto bailout;
4198
4199		bcopy(desc->sense_key_spec, sks, sizeof(desc->sense_key_spec));
4200		break;
4201	}
4202	case SSD_TYPE_FIXED: {
4203		struct scsi_sense_data_fixed *sense;
4204
4205		sense = (struct scsi_sense_data_fixed *)sense_data;
4206
4207		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, sense_key_spec)== 0)
4208		 || (SSD_FIXED_IS_FILLED(sense, sense_key_spec) == 0))
4209			goto bailout;
4210
4211		if ((sense->sense_key_spec[0] & SSD_SCS_VALID) == 0)
4212			goto bailout;
4213
4214		bcopy(sense->sense_key_spec, sks,sizeof(sense->sense_key_spec));
4215		break;
4216	}
4217	default:
4218		goto bailout;
4219		break;
4220	}
4221	return (0);
4222bailout:
4223	return (1);
4224}
4225
4226/*
4227 * Provide a common interface for fixed and descriptor sense to detect
4228 * whether we have block-specific sense information.  It is clear by the
4229 * presence of the block descriptor in descriptor mode, but we have to
4230 * infer from the inquiry data and ILI bit in fixed mode.
4231 */
4232int
4233scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
4234		    struct scsi_inquiry_data *inq_data, uint8_t *block_bits)
4235{
4236	scsi_sense_data_type sense_type;
4237
4238	if (inq_data != NULL) {
4239		switch (SID_TYPE(inq_data)) {
4240		case T_DIRECT:
4241		case T_RBC:
4242		case T_ZBC_HM:
4243			break;
4244		default:
4245			goto bailout;
4246			break;
4247		}
4248	}
4249
4250	sense_type = scsi_sense_type(sense_data);
4251
4252	switch (sense_type) {
4253	case SSD_TYPE_DESC: {
4254		struct scsi_sense_data_desc *sense;
4255		struct scsi_sense_block *block;
4256
4257		sense = (struct scsi_sense_data_desc *)sense_data;
4258
4259		block = (struct scsi_sense_block *)scsi_find_desc(sense,
4260		    sense_len, SSD_DESC_BLOCK);
4261		if (block == NULL)
4262			goto bailout;
4263
4264		*block_bits = block->byte3;
4265		break;
4266	}
4267	case SSD_TYPE_FIXED: {
4268		struct scsi_sense_data_fixed *sense;
4269
4270		sense = (struct scsi_sense_data_fixed *)sense_data;
4271
4272		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4273			goto bailout;
4274
4275		*block_bits = sense->flags & SSD_ILI;
4276		break;
4277	}
4278	default:
4279		goto bailout;
4280		break;
4281	}
4282	return (0);
4283bailout:
4284	return (1);
4285}
4286
4287int
4288scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
4289		     struct scsi_inquiry_data *inq_data, uint8_t *stream_bits)
4290{
4291	scsi_sense_data_type sense_type;
4292
4293	if (inq_data != NULL) {
4294		switch (SID_TYPE(inq_data)) {
4295		case T_SEQUENTIAL:
4296			break;
4297		default:
4298			goto bailout;
4299			break;
4300		}
4301	}
4302
4303	sense_type = scsi_sense_type(sense_data);
4304
4305	switch (sense_type) {
4306	case SSD_TYPE_DESC: {
4307		struct scsi_sense_data_desc *sense;
4308		struct scsi_sense_stream *stream;
4309
4310		sense = (struct scsi_sense_data_desc *)sense_data;
4311
4312		stream = (struct scsi_sense_stream *)scsi_find_desc(sense,
4313		    sense_len, SSD_DESC_STREAM);
4314		if (stream == NULL)
4315			goto bailout;
4316
4317		*stream_bits = stream->byte3;
4318		break;
4319	}
4320	case SSD_TYPE_FIXED: {
4321		struct scsi_sense_data_fixed *sense;
4322
4323		sense = (struct scsi_sense_data_fixed *)sense_data;
4324
4325		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4326			goto bailout;
4327
4328		*stream_bits = sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK);
4329		break;
4330	}
4331	default:
4332		goto bailout;
4333		break;
4334	}
4335	return (0);
4336bailout:
4337	return (1);
4338}
4339
4340void
4341scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4342	       struct scsi_inquiry_data *inq_data, uint64_t info)
4343{
4344	sbuf_printf(sb, "Info: %#jx", info);
4345}
4346
4347void
4348scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4349		  struct scsi_inquiry_data *inq_data, uint64_t csi)
4350{
4351	sbuf_printf(sb, "Command Specific Info: %#jx", csi);
4352}
4353
4354void
4355scsi_progress_sbuf(struct sbuf *sb, uint16_t progress)
4356{
4357	sbuf_printf(sb, "Progress: %d%% (%d/%d) complete",
4358		    (progress * 100) / SSD_SKS_PROGRESS_DENOM,
4359		    progress, SSD_SKS_PROGRESS_DENOM);
4360}
4361
4362/*
4363 * Returns 1 for failure (i.e. SKS isn't valid) and 0 for success.
4364 */
4365int
4366scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks)
4367{
4368
4369	switch (sense_key) {
4370	case SSD_KEY_ILLEGAL_REQUEST: {
4371		struct scsi_sense_sks_field *field;
4372		int bad_command;
4373		char tmpstr[40];
4374
4375		/*Field Pointer*/
4376		field = (struct scsi_sense_sks_field *)sks;
4377
4378		if (field->byte0 & SSD_SKS_FIELD_CMD)
4379			bad_command = 1;
4380		else
4381			bad_command = 0;
4382
4383		tmpstr[0] = '\0';
4384
4385		/* Bit pointer is valid */
4386		if (field->byte0 & SSD_SKS_BPV)
4387			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4388				 field->byte0 & SSD_SKS_BIT_VALUE);
4389
4390		sbuf_printf(sb, "%s byte %d %sis invalid",
4391			    bad_command ? "Command" : "Data",
4392			    scsi_2btoul(field->field), tmpstr);
4393		break;
4394	}
4395	case SSD_KEY_UNIT_ATTENTION: {
4396		struct scsi_sense_sks_overflow *overflow;
4397
4398		overflow = (struct scsi_sense_sks_overflow *)sks;
4399
4400		/*UA Condition Queue Overflow*/
4401		sbuf_printf(sb, "Unit Attention Condition Queue %s",
4402			    (overflow->byte0 & SSD_SKS_OVERFLOW_SET) ?
4403			    "Overflowed" : "Did Not Overflow??");
4404		break;
4405	}
4406	case SSD_KEY_RECOVERED_ERROR:
4407	case SSD_KEY_HARDWARE_ERROR:
4408	case SSD_KEY_MEDIUM_ERROR: {
4409		struct scsi_sense_sks_retry *retry;
4410
4411		/*Actual Retry Count*/
4412		retry = (struct scsi_sense_sks_retry *)sks;
4413
4414		sbuf_printf(sb, "Actual Retry Count: %d",
4415			    scsi_2btoul(retry->actual_retry_count));
4416		break;
4417	}
4418	case SSD_KEY_NO_SENSE:
4419	case SSD_KEY_NOT_READY: {
4420		struct scsi_sense_sks_progress *progress;
4421		int progress_val;
4422
4423		/*Progress Indication*/
4424		progress = (struct scsi_sense_sks_progress *)sks;
4425		progress_val = scsi_2btoul(progress->progress);
4426
4427		scsi_progress_sbuf(sb, progress_val);
4428		break;
4429	}
4430	case SSD_KEY_COPY_ABORTED: {
4431		struct scsi_sense_sks_segment *segment;
4432		char tmpstr[40];
4433
4434		/*Segment Pointer*/
4435		segment = (struct scsi_sense_sks_segment *)sks;
4436
4437		tmpstr[0] = '\0';
4438
4439		if (segment->byte0 & SSD_SKS_SEGMENT_BPV)
4440			snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4441				 segment->byte0 & SSD_SKS_SEGMENT_BITPTR);
4442
4443		sbuf_printf(sb, "%s byte %d %sis invalid", (segment->byte0 &
4444			    SSD_SKS_SEGMENT_SD) ? "Segment" : "Data",
4445			    scsi_2btoul(segment->field), tmpstr);
4446		break;
4447	}
4448	default:
4449		sbuf_printf(sb, "Sense Key Specific: %#x,%#x", sks[0],
4450			    scsi_2btoul(&sks[1]));
4451		break;
4452	}
4453
4454	return (0);
4455}
4456
4457void
4458scsi_fru_sbuf(struct sbuf *sb, uint64_t fru)
4459{
4460	sbuf_printf(sb, "Field Replaceable Unit: %d", (int)fru);
4461}
4462
4463void
4464scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits)
4465{
4466	int need_comma;
4467
4468	need_comma = 0;
4469	/*
4470	 * XXX KDM this needs more descriptive decoding.
4471	 */
4472	sbuf_printf(sb, "Stream Command Sense Data: ");
4473	if (stream_bits & SSD_DESC_STREAM_FM) {
4474		sbuf_printf(sb, "Filemark");
4475		need_comma = 1;
4476	}
4477
4478	if (stream_bits & SSD_DESC_STREAM_EOM) {
4479		sbuf_printf(sb, "%sEOM", (need_comma) ? "," : "");
4480		need_comma = 1;
4481	}
4482
4483	if (stream_bits & SSD_DESC_STREAM_ILI)
4484		sbuf_printf(sb, "%sILI", (need_comma) ? "," : "");
4485}
4486
4487void
4488scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits)
4489{
4490
4491	sbuf_printf(sb, "Block Command Sense Data: ");
4492	if (block_bits & SSD_DESC_BLOCK_ILI)
4493		sbuf_printf(sb, "ILI");
4494}
4495
4496void
4497scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4498		     u_int sense_len, uint8_t *cdb, int cdb_len,
4499		     struct scsi_inquiry_data *inq_data,
4500		     struct scsi_sense_desc_header *header)
4501{
4502	struct scsi_sense_info *info;
4503
4504	info = (struct scsi_sense_info *)header;
4505
4506	if ((info->byte2 & SSD_INFO_VALID) == 0)
4507		return;
4508
4509	scsi_info_sbuf(sb, cdb, cdb_len, inq_data, scsi_8btou64(info->info));
4510}
4511
4512void
4513scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4514			u_int sense_len, uint8_t *cdb, int cdb_len,
4515			struct scsi_inquiry_data *inq_data,
4516			struct scsi_sense_desc_header *header)
4517{
4518	struct scsi_sense_command *command;
4519
4520	command = (struct scsi_sense_command *)header;
4521
4522	scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
4523			  scsi_8btou64(command->command_info));
4524}
4525
4526void
4527scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4528		    u_int sense_len, uint8_t *cdb, int cdb_len,
4529		    struct scsi_inquiry_data *inq_data,
4530		    struct scsi_sense_desc_header *header)
4531{
4532	struct scsi_sense_sks *sks;
4533	int error_code, sense_key, asc, ascq;
4534
4535	sks = (struct scsi_sense_sks *)header;
4536
4537	if ((sks->sense_key_spec[0] & SSD_SKS_VALID) == 0)
4538		return;
4539
4540	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4541			       &asc, &ascq, /*show_errors*/ 1);
4542
4543	scsi_sks_sbuf(sb, sense_key, sks->sense_key_spec);
4544}
4545
4546void
4547scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4548		    u_int sense_len, uint8_t *cdb, int cdb_len,
4549		    struct scsi_inquiry_data *inq_data,
4550		    struct scsi_sense_desc_header *header)
4551{
4552	struct scsi_sense_fru *fru;
4553
4554	fru = (struct scsi_sense_fru *)header;
4555
4556	if (fru->fru == 0)
4557		return;
4558
4559	scsi_fru_sbuf(sb, (uint64_t)fru->fru);
4560}
4561
4562void
4563scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4564		       u_int sense_len, uint8_t *cdb, int cdb_len,
4565		       struct scsi_inquiry_data *inq_data,
4566		       struct scsi_sense_desc_header *header)
4567{
4568	struct scsi_sense_stream *stream;
4569
4570	stream = (struct scsi_sense_stream *)header;
4571	scsi_stream_sbuf(sb, stream->byte3);
4572}
4573
4574void
4575scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4576		      u_int sense_len, uint8_t *cdb, int cdb_len,
4577		      struct scsi_inquiry_data *inq_data,
4578		      struct scsi_sense_desc_header *header)
4579{
4580	struct scsi_sense_block *block;
4581
4582	block = (struct scsi_sense_block *)header;
4583	scsi_block_sbuf(sb, block->byte3);
4584}
4585
4586void
4587scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4588			 u_int sense_len, uint8_t *cdb, int cdb_len,
4589			 struct scsi_inquiry_data *inq_data,
4590			 struct scsi_sense_desc_header *header)
4591{
4592	struct scsi_sense_progress *progress;
4593	const char *sense_key_desc;
4594	const char *asc_desc;
4595	int progress_val;
4596
4597	progress = (struct scsi_sense_progress *)header;
4598
4599	/*
4600	 * Get descriptions for the sense key, ASC, and ASCQ in the
4601	 * progress descriptor.  These could be different than the values
4602	 * in the overall sense data.
4603	 */
4604	scsi_sense_desc(progress->sense_key, progress->add_sense_code,
4605			progress->add_sense_code_qual, inq_data,
4606			&sense_key_desc, &asc_desc);
4607
4608	progress_val = scsi_2btoul(progress->progress);
4609
4610	/*
4611	 * The progress indicator is for the operation described by the
4612	 * sense key, ASC, and ASCQ in the descriptor.
4613	 */
4614	sbuf_cat(sb, sense_key_desc);
4615	sbuf_printf(sb, " asc:%x,%x (%s): ", progress->add_sense_code,
4616		    progress->add_sense_code_qual, asc_desc);
4617	scsi_progress_sbuf(sb, progress_val);
4618}
4619
4620void
4621scsi_sense_ata_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4622			 u_int sense_len, uint8_t *cdb, int cdb_len,
4623			 struct scsi_inquiry_data *inq_data,
4624			 struct scsi_sense_desc_header *header)
4625{
4626	struct scsi_sense_ata_ret_desc *res;
4627
4628	res = (struct scsi_sense_ata_ret_desc *)header;
4629
4630	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s), ",
4631	    res->status,
4632	    (res->status & 0x80) ? "BSY " : "",
4633	    (res->status & 0x40) ? "DRDY " : "",
4634	    (res->status & 0x20) ? "DF " : "",
4635	    (res->status & 0x10) ? "SERV " : "",
4636	    (res->status & 0x08) ? "DRQ " : "",
4637	    (res->status & 0x04) ? "CORR " : "",
4638	    (res->status & 0x02) ? "IDX " : "",
4639	    (res->status & 0x01) ? "ERR" : "");
4640	if (res->status & 1) {
4641	    sbuf_printf(sb, "error: %02x (%s%s%s%s%s%s%s%s), ",
4642		res->error,
4643		(res->error & 0x80) ? "ICRC " : "",
4644		(res->error & 0x40) ? "UNC " : "",
4645		(res->error & 0x20) ? "MC " : "",
4646		(res->error & 0x10) ? "IDNF " : "",
4647		(res->error & 0x08) ? "MCR " : "",
4648		(res->error & 0x04) ? "ABRT " : "",
4649		(res->error & 0x02) ? "NM " : "",
4650		(res->error & 0x01) ? "ILI" : "");
4651	}
4652
4653	if (res->flags & SSD_DESC_ATA_FLAG_EXTEND) {
4654		sbuf_printf(sb, "count: %02x%02x, ",
4655		    res->count_15_8, res->count_7_0);
4656		sbuf_printf(sb, "LBA: %02x%02x%02x%02x%02x%02x, ",
4657		    res->lba_47_40, res->lba_39_32, res->lba_31_24,
4658		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4659	} else {
4660		sbuf_printf(sb, "count: %02x, ", res->count_7_0);
4661		sbuf_printf(sb, "LBA: %02x%02x%02x, ",
4662		    res->lba_23_16, res->lba_15_8, res->lba_7_0);
4663	}
4664	sbuf_printf(sb, "device: %02x, ", res->device);
4665}
4666
4667void
4668scsi_sense_forwarded_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4669			 u_int sense_len, uint8_t *cdb, int cdb_len,
4670			 struct scsi_inquiry_data *inq_data,
4671			 struct scsi_sense_desc_header *header)
4672{
4673	struct scsi_sense_forwarded *forwarded;
4674	const char *sense_key_desc;
4675	const char *asc_desc;
4676	int error_code, sense_key, asc, ascq;
4677
4678	forwarded = (struct scsi_sense_forwarded *)header;
4679	scsi_extract_sense_len((struct scsi_sense_data *)forwarded->sense_data,
4680	    forwarded->length - 2, &error_code, &sense_key, &asc, &ascq, 1);
4681	scsi_sense_desc(sense_key, asc, ascq, NULL, &sense_key_desc, &asc_desc);
4682
4683	sbuf_printf(sb, "Forwarded sense: %s asc:%x,%x (%s): ",
4684	    sense_key_desc, asc, ascq, asc_desc);
4685}
4686
4687/*
4688 * Generic sense descriptor printing routine.  This is used when we have
4689 * not yet implemented a specific printing routine for this descriptor.
4690 */
4691void
4692scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4693			u_int sense_len, uint8_t *cdb, int cdb_len,
4694			struct scsi_inquiry_data *inq_data,
4695			struct scsi_sense_desc_header *header)
4696{
4697	int i;
4698	uint8_t *buf_ptr;
4699
4700	sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4701
4702	buf_ptr = (uint8_t *)&header[1];
4703
4704	for (i = 0; i < header->length; i++, buf_ptr++)
4705		sbuf_printf(sb, " %02x", *buf_ptr);
4706}
4707
4708/*
4709 * Keep this list in numeric order.  This speeds the array traversal.
4710 */
4711struct scsi_sense_desc_printer {
4712	uint8_t desc_type;
4713	/*
4714	 * The function arguments here are the superset of what is needed
4715	 * to print out various different descriptors.  Command and
4716	 * information descriptors need inquiry data and command type.
4717	 * Sense key specific descriptors need the sense key.
4718	 *
4719	 * The sense, cdb, and inquiry data arguments may be NULL, but the
4720	 * information printed may not be fully decoded as a result.
4721	 */
4722	void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4723			   u_int sense_len, uint8_t *cdb, int cdb_len,
4724			   struct scsi_inquiry_data *inq_data,
4725			   struct scsi_sense_desc_header *header);
4726} scsi_sense_printers[] = {
4727	{SSD_DESC_INFO, scsi_sense_info_sbuf},
4728	{SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4729	{SSD_DESC_SKS, scsi_sense_sks_sbuf},
4730	{SSD_DESC_FRU, scsi_sense_fru_sbuf},
4731	{SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4732	{SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4733	{SSD_DESC_ATA, scsi_sense_ata_sbuf},
4734	{SSD_DESC_PROGRESS, scsi_sense_progress_sbuf},
4735	{SSD_DESC_FORWARDED, scsi_sense_forwarded_sbuf}
4736};
4737
4738void
4739scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4740		     u_int sense_len, uint8_t *cdb, int cdb_len,
4741		     struct scsi_inquiry_data *inq_data,
4742		     struct scsi_sense_desc_header *header)
4743{
4744	u_int i;
4745
4746	for (i = 0; i < nitems(scsi_sense_printers); i++) {
4747		struct scsi_sense_desc_printer *printer;
4748
4749		printer = &scsi_sense_printers[i];
4750
4751		/*
4752		 * The list is sorted, so quit if we've passed our
4753		 * descriptor number.
4754		 */
4755		if (printer->desc_type > header->desc_type)
4756			break;
4757
4758		if (printer->desc_type != header->desc_type)
4759			continue;
4760
4761		printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4762				    inq_data, header);
4763
4764		return;
4765	}
4766
4767	/*
4768	 * No specific printing routine, so use the generic routine.
4769	 */
4770	scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4771				inq_data, header);
4772}
4773
4774scsi_sense_data_type
4775scsi_sense_type(struct scsi_sense_data *sense_data)
4776{
4777	switch (sense_data->error_code & SSD_ERRCODE) {
4778	case SSD_DESC_CURRENT_ERROR:
4779	case SSD_DESC_DEFERRED_ERROR:
4780		return (SSD_TYPE_DESC);
4781		break;
4782	case SSD_CURRENT_ERROR:
4783	case SSD_DEFERRED_ERROR:
4784		return (SSD_TYPE_FIXED);
4785		break;
4786	default:
4787		break;
4788	}
4789
4790	return (SSD_TYPE_NONE);
4791}
4792
4793struct scsi_print_sense_info {
4794	struct sbuf *sb;
4795	char *path_str;
4796	uint8_t *cdb;
4797	int cdb_len;
4798	struct scsi_inquiry_data *inq_data;
4799};
4800
4801static int
4802scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4803		     struct scsi_sense_desc_header *header, void *arg)
4804{
4805	struct scsi_print_sense_info *print_info;
4806
4807	print_info = (struct scsi_print_sense_info *)arg;
4808
4809	switch (header->desc_type) {
4810	case SSD_DESC_INFO:
4811	case SSD_DESC_FRU:
4812	case SSD_DESC_COMMAND:
4813	case SSD_DESC_SKS:
4814	case SSD_DESC_BLOCK:
4815	case SSD_DESC_STREAM:
4816		/*
4817		 * We have already printed these descriptors, if they are
4818		 * present.
4819		 */
4820		break;
4821	default: {
4822		sbuf_printf(print_info->sb, "%s", print_info->path_str);
4823		scsi_sense_desc_sbuf(print_info->sb,
4824				     (struct scsi_sense_data *)sense, sense_len,
4825				     print_info->cdb, print_info->cdb_len,
4826				     print_info->inq_data, header);
4827		sbuf_printf(print_info->sb, "\n");
4828		break;
4829	}
4830	}
4831
4832	/*
4833	 * Tell the iterator that we want to see more descriptors if they
4834	 * are present.
4835	 */
4836	return (0);
4837}
4838
4839void
4840scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4841		     struct sbuf *sb, char *path_str,
4842		     struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4843		     int cdb_len)
4844{
4845	int error_code, sense_key, asc, ascq;
4846
4847	sbuf_cat(sb, path_str);
4848
4849	scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4850			       &asc, &ascq, /*show_errors*/ 1);
4851
4852	sbuf_printf(sb, "SCSI sense: ");
4853	switch (error_code) {
4854	case SSD_DEFERRED_ERROR:
4855	case SSD_DESC_DEFERRED_ERROR:
4856		sbuf_printf(sb, "Deferred error: ");
4857
4858		/* FALLTHROUGH */
4859	case SSD_CURRENT_ERROR:
4860	case SSD_DESC_CURRENT_ERROR:
4861	{
4862		struct scsi_sense_data_desc *desc_sense;
4863		struct scsi_print_sense_info print_info;
4864		const char *sense_key_desc;
4865		const char *asc_desc;
4866		uint8_t sks[3];
4867		uint64_t val;
4868		uint8_t bits;
4869
4870		/*
4871		 * Get descriptions for the sense key, ASC, and ASCQ.  If
4872		 * these aren't present in the sense data (i.e. the sense
4873		 * data isn't long enough), the -1 values that
4874		 * scsi_extract_sense_len() returns will yield default
4875		 * or error descriptions.
4876		 */
4877		scsi_sense_desc(sense_key, asc, ascq, inq_data,
4878				&sense_key_desc, &asc_desc);
4879
4880		/*
4881		 * We first print the sense key and ASC/ASCQ.
4882		 */
4883		sbuf_cat(sb, sense_key_desc);
4884		sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
4885
4886		/*
4887		 * Print any block or stream device-specific information.
4888		 */
4889		if (scsi_get_block_info(sense, sense_len, inq_data,
4890		    &bits) == 0 && bits != 0) {
4891			sbuf_cat(sb, path_str);
4892			scsi_block_sbuf(sb, bits);
4893			sbuf_printf(sb, "\n");
4894		} else if (scsi_get_stream_info(sense, sense_len, inq_data,
4895		    &bits) == 0 && bits != 0) {
4896			sbuf_cat(sb, path_str);
4897			scsi_stream_sbuf(sb, bits);
4898			sbuf_printf(sb, "\n");
4899		}
4900
4901		/*
4902		 * Print the info field.
4903		 */
4904		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
4905					&val, NULL) == 0) {
4906			sbuf_cat(sb, path_str);
4907			scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
4908			sbuf_printf(sb, "\n");
4909		}
4910
4911		/*
4912		 * Print the FRU.
4913		 */
4914		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
4915					&val, NULL) == 0) {
4916			sbuf_cat(sb, path_str);
4917			scsi_fru_sbuf(sb, val);
4918			sbuf_printf(sb, "\n");
4919		}
4920
4921		/*
4922		 * Print any command-specific information.
4923		 */
4924		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
4925					&val, NULL) == 0) {
4926			sbuf_cat(sb, path_str);
4927			scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
4928			sbuf_printf(sb, "\n");
4929		}
4930
4931		/*
4932		 * Print out any sense-key-specific information.
4933		 */
4934		if (scsi_get_sks(sense, sense_len, sks) == 0) {
4935			sbuf_cat(sb, path_str);
4936			scsi_sks_sbuf(sb, sense_key, sks);
4937			sbuf_printf(sb, "\n");
4938		}
4939
4940		/*
4941		 * If this is fixed sense, we're done.  If we have
4942		 * descriptor sense, we might have more information
4943		 * available.
4944		 */
4945		if (scsi_sense_type(sense) != SSD_TYPE_DESC)
4946			break;
4947
4948		desc_sense = (struct scsi_sense_data_desc *)sense;
4949
4950		print_info.sb = sb;
4951		print_info.path_str = path_str;
4952		print_info.cdb = cdb;
4953		print_info.cdb_len = cdb_len;
4954		print_info.inq_data = inq_data;
4955
4956		/*
4957		 * Print any sense descriptors that we have not already printed.
4958		 */
4959		scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
4960				  &print_info);
4961		break;
4962	}
4963	case -1:
4964		/*
4965		 * scsi_extract_sense_len() sets values to -1 if the
4966		 * show_errors flag is set and they aren't present in the
4967		 * sense data.  This means that sense_len is 0.
4968		 */
4969		sbuf_printf(sb, "No sense data present\n");
4970		break;
4971	default: {
4972		sbuf_printf(sb, "Error code 0x%x", error_code);
4973		if (sense->error_code & SSD_ERRCODE_VALID) {
4974			struct scsi_sense_data_fixed *fixed_sense;
4975
4976			fixed_sense = (struct scsi_sense_data_fixed *)sense;
4977
4978			if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
4979				uint32_t info;
4980
4981				info = scsi_4btoul(fixed_sense->info);
4982
4983				sbuf_printf(sb, " at block no. %d (decimal)",
4984					    info);
4985			}
4986		}
4987		sbuf_printf(sb, "\n");
4988		break;
4989	}
4990	}
4991}
4992
4993/*
4994 * scsi_sense_sbuf() returns 0 for success and -1 for failure.
4995 */
4996#ifdef _KERNEL
4997int
4998scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
4999		scsi_sense_string_flags flags)
5000#else /* !_KERNEL */
5001int
5002scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio,
5003		struct sbuf *sb, scsi_sense_string_flags flags)
5004#endif /* _KERNEL/!_KERNEL */
5005{
5006	struct	  scsi_sense_data *sense;
5007	struct	  scsi_inquiry_data *inq_data;
5008#ifdef _KERNEL
5009	struct	  ccb_getdev *cgd;
5010#endif /* _KERNEL */
5011	char	  path_str[64];
5012
5013#ifndef _KERNEL
5014	if (device == NULL)
5015		return(-1);
5016#endif /* !_KERNEL */
5017	if ((csio == NULL) || (sb == NULL))
5018		return(-1);
5019
5020	/*
5021	 * If the CDB is a physical address, we can't deal with it..
5022	 */
5023	if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
5024		flags &= ~SSS_FLAG_PRINT_COMMAND;
5025
5026#ifdef _KERNEL
5027	xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
5028#else /* !_KERNEL */
5029	cam_path_string(device, path_str, sizeof(path_str));
5030#endif /* _KERNEL/!_KERNEL */
5031
5032#ifdef _KERNEL
5033	if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
5034		return(-1);
5035	/*
5036	 * Get the device information.
5037	 */
5038	xpt_setup_ccb(&cgd->ccb_h,
5039		      csio->ccb_h.path,
5040		      CAM_PRIORITY_NORMAL);
5041	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
5042	xpt_action((union ccb *)cgd);
5043
5044	/*
5045	 * If the device is unconfigured, just pretend that it is a hard
5046	 * drive.  scsi_op_desc() needs this.
5047	 */
5048	if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
5049		cgd->inq_data.device = T_DIRECT;
5050
5051	inq_data = &cgd->inq_data;
5052
5053#else /* !_KERNEL */
5054
5055	inq_data = &device->inq_data;
5056
5057#endif /* _KERNEL/!_KERNEL */
5058
5059	sense = NULL;
5060
5061	if (flags & SSS_FLAG_PRINT_COMMAND) {
5062		sbuf_cat(sb, path_str);
5063
5064#ifdef _KERNEL
5065		scsi_command_string(csio, sb);
5066#else /* !_KERNEL */
5067		scsi_command_string(device, csio, sb);
5068#endif /* _KERNEL/!_KERNEL */
5069		sbuf_printf(sb, "\n");
5070	}
5071
5072	/*
5073	 * If the sense data is a physical pointer, forget it.
5074	 */
5075	if (csio->ccb_h.flags & CAM_SENSE_PTR) {
5076		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5077#ifdef _KERNEL
5078			xpt_free_ccb((union ccb*)cgd);
5079#endif /* _KERNEL/!_KERNEL */
5080			return(-1);
5081		} else {
5082			/*
5083			 * bcopy the pointer to avoid unaligned access
5084			 * errors on finicky architectures.  We don't
5085			 * ensure that the sense data is pointer aligned.
5086			 */
5087			bcopy((struct scsi_sense_data **)&csio->sense_data,
5088			    &sense, sizeof(struct scsi_sense_data *));
5089		}
5090	} else {
5091		/*
5092		 * If the physical sense flag is set, but the sense pointer
5093		 * is not also set, we assume that the user is an idiot and
5094		 * return.  (Well, okay, it could be that somehow, the
5095		 * entire csio is physical, but we would have probably core
5096		 * dumped on one of the bogus pointer deferences above
5097		 * already.)
5098		 */
5099		if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5100#ifdef _KERNEL
5101			xpt_free_ccb((union ccb*)cgd);
5102#endif /* _KERNEL/!_KERNEL */
5103			return(-1);
5104		} else
5105			sense = &csio->sense_data;
5106	}
5107
5108	scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
5109	    path_str, inq_data, scsiio_cdb_ptr(csio), csio->cdb_len);
5110
5111#ifdef _KERNEL
5112	xpt_free_ccb((union ccb*)cgd);
5113#endif /* _KERNEL/!_KERNEL */
5114	return(0);
5115}
5116
5117#ifdef _KERNEL
5118char *
5119scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
5120#else /* !_KERNEL */
5121char *
5122scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
5123		  char *str, int str_len)
5124#endif /* _KERNEL/!_KERNEL */
5125{
5126	struct sbuf sb;
5127
5128	sbuf_new(&sb, str, str_len, 0);
5129
5130#ifdef _KERNEL
5131	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5132#else /* !_KERNEL */
5133	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5134#endif /* _KERNEL/!_KERNEL */
5135
5136	sbuf_finish(&sb);
5137
5138	return(sbuf_data(&sb));
5139}
5140
5141#ifdef _KERNEL
5142void
5143scsi_sense_print(struct ccb_scsiio *csio)
5144{
5145	struct sbuf sb;
5146	char str[512];
5147
5148	sbuf_new(&sb, str, sizeof(str), 0);
5149
5150	scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5151
5152	sbuf_finish(&sb);
5153
5154	sbuf_putbuf(&sb);
5155}
5156
5157#else /* !_KERNEL */
5158void
5159scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio,
5160		 FILE *ofile)
5161{
5162	struct sbuf sb;
5163	char str[512];
5164
5165	if ((device == NULL) || (csio == NULL) || (ofile == NULL))
5166		return;
5167
5168	sbuf_new(&sb, str, sizeof(str), 0);
5169
5170	scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5171
5172	sbuf_finish(&sb);
5173
5174	fprintf(ofile, "%s", sbuf_data(&sb));
5175}
5176
5177#endif /* _KERNEL/!_KERNEL */
5178
5179/*
5180 * Extract basic sense information.  This is backward-compatible with the
5181 * previous implementation.  For new implementations,
5182 * scsi_extract_sense_len() is recommended.
5183 */
5184void
5185scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
5186		   int *sense_key, int *asc, int *ascq)
5187{
5188	scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
5189			       sense_key, asc, ascq, /*show_errors*/ 0);
5190}
5191
5192/*
5193 * Extract basic sense information from SCSI I/O CCB structure.
5194 */
5195int
5196scsi_extract_sense_ccb(union ccb *ccb,
5197    int *error_code, int *sense_key, int *asc, int *ascq)
5198{
5199	struct scsi_sense_data *sense_data;
5200
5201	/* Make sure there are some sense data we can access. */
5202	if (ccb->ccb_h.func_code != XPT_SCSI_IO ||
5203	    (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
5204	    (ccb->csio.scsi_status != SCSI_STATUS_CHECK_COND) ||
5205	    (ccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0 ||
5206	    (ccb->ccb_h.flags & CAM_SENSE_PHYS))
5207		return (0);
5208
5209	if (ccb->ccb_h.flags & CAM_SENSE_PTR)
5210		bcopy((struct scsi_sense_data **)&ccb->csio.sense_data,
5211		    &sense_data, sizeof(struct scsi_sense_data *));
5212	else
5213		sense_data = &ccb->csio.sense_data;
5214	scsi_extract_sense_len(sense_data,
5215	    ccb->csio.sense_len - ccb->csio.sense_resid,
5216	    error_code, sense_key, asc, ascq, 1);
5217	if (*error_code == -1)
5218		return (0);
5219	return (1);
5220}
5221
5222/*
5223 * Extract basic sense information.  If show_errors is set, sense values
5224 * will be set to -1 if they are not present.
5225 */
5226void
5227scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
5228		       int *error_code, int *sense_key, int *asc, int *ascq,
5229		       int show_errors)
5230{
5231	/*
5232	 * If we have no length, we have no sense.
5233	 */
5234	if (sense_len == 0) {
5235		if (show_errors == 0) {
5236			*error_code = 0;
5237			*sense_key = 0;
5238			*asc = 0;
5239			*ascq = 0;
5240		} else {
5241			*error_code = -1;
5242			*sense_key = -1;
5243			*asc = -1;
5244			*ascq = -1;
5245		}
5246		return;
5247	}
5248
5249	*error_code = sense_data->error_code & SSD_ERRCODE;
5250
5251	switch (*error_code) {
5252	case SSD_DESC_CURRENT_ERROR:
5253	case SSD_DESC_DEFERRED_ERROR: {
5254		struct scsi_sense_data_desc *sense;
5255
5256		sense = (struct scsi_sense_data_desc *)sense_data;
5257
5258		if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
5259			*sense_key = sense->sense_key & SSD_KEY;
5260		else
5261			*sense_key = (show_errors) ? -1 : 0;
5262
5263		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
5264			*asc = sense->add_sense_code;
5265		else
5266			*asc = (show_errors) ? -1 : 0;
5267
5268		if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
5269			*ascq = sense->add_sense_code_qual;
5270		else
5271			*ascq = (show_errors) ? -1 : 0;
5272		break;
5273	}
5274	case SSD_CURRENT_ERROR:
5275	case SSD_DEFERRED_ERROR:
5276	default: {
5277		struct scsi_sense_data_fixed *sense;
5278
5279		sense = (struct scsi_sense_data_fixed *)sense_data;
5280
5281		if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
5282			*sense_key = sense->flags & SSD_KEY;
5283		else
5284			*sense_key = (show_errors) ? -1 : 0;
5285
5286		if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
5287		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
5288			*asc = sense->add_sense_code;
5289		else
5290			*asc = (show_errors) ? -1 : 0;
5291
5292		if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
5293		 && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
5294			*ascq = sense->add_sense_code_qual;
5295		else
5296			*ascq = (show_errors) ? -1 : 0;
5297		break;
5298	}
5299	}
5300}
5301
5302int
5303scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
5304		   int show_errors)
5305{
5306	int error_code, sense_key, asc, ascq;
5307
5308	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5309			       &sense_key, &asc, &ascq, show_errors);
5310
5311	return (sense_key);
5312}
5313
5314int
5315scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
5316	     int show_errors)
5317{
5318	int error_code, sense_key, asc, ascq;
5319
5320	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5321			       &sense_key, &asc, &ascq, show_errors);
5322
5323	return (asc);
5324}
5325
5326int
5327scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
5328	      int show_errors)
5329{
5330	int error_code, sense_key, asc, ascq;
5331
5332	scsi_extract_sense_len(sense_data, sense_len, &error_code,
5333			       &sense_key, &asc, &ascq, show_errors);
5334
5335	return (ascq);
5336}
5337
5338/*
5339 * This function currently requires at least 36 bytes, or
5340 * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
5341 * function needs more or less data in the future, another length should be
5342 * defined in scsi_all.h to indicate the minimum amount of data necessary
5343 * for this routine to function properly.
5344 */
5345void
5346scsi_print_inquiry_sbuf(struct sbuf *sb, struct scsi_inquiry_data *inq_data)
5347{
5348	u_int8_t type;
5349	char *dtype, *qtype;
5350
5351	type = SID_TYPE(inq_data);
5352
5353	/*
5354	 * Figure out basic device type and qualifier.
5355	 */
5356	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
5357		qtype = " (vendor-unique qualifier)";
5358	} else {
5359		switch (SID_QUAL(inq_data)) {
5360		case SID_QUAL_LU_CONNECTED:
5361			qtype = "";
5362			break;
5363
5364		case SID_QUAL_LU_OFFLINE:
5365			qtype = " (offline)";
5366			break;
5367
5368		case SID_QUAL_RSVD:
5369			qtype = " (reserved qualifier)";
5370			break;
5371		default:
5372		case SID_QUAL_BAD_LU:
5373			qtype = " (LUN not supported)";
5374			break;
5375		}
5376	}
5377
5378	switch (type) {
5379	case T_DIRECT:
5380		dtype = "Direct Access";
5381		break;
5382	case T_SEQUENTIAL:
5383		dtype = "Sequential Access";
5384		break;
5385	case T_PRINTER:
5386		dtype = "Printer";
5387		break;
5388	case T_PROCESSOR:
5389		dtype = "Processor";
5390		break;
5391	case T_WORM:
5392		dtype = "WORM";
5393		break;
5394	case T_CDROM:
5395		dtype = "CD-ROM";
5396		break;
5397	case T_SCANNER:
5398		dtype = "Scanner";
5399		break;
5400	case T_OPTICAL:
5401		dtype = "Optical";
5402		break;
5403	case T_CHANGER:
5404		dtype = "Changer";
5405		break;
5406	case T_COMM:
5407		dtype = "Communication";
5408		break;
5409	case T_STORARRAY:
5410		dtype = "Storage Array";
5411		break;
5412	case T_ENCLOSURE:
5413		dtype = "Enclosure Services";
5414		break;
5415	case T_RBC:
5416		dtype = "Simplified Direct Access";
5417		break;
5418	case T_OCRW:
5419		dtype = "Optical Card Read/Write";
5420		break;
5421	case T_OSD:
5422		dtype = "Object-Based Storage";
5423		break;
5424	case T_ADC:
5425		dtype = "Automation/Drive Interface";
5426		break;
5427	case T_ZBC_HM:
5428		dtype = "Host Managed Zoned Block";
5429		break;
5430	case T_NODEVICE:
5431		dtype = "Uninstalled";
5432		break;
5433	default:
5434		dtype = "unknown";
5435		break;
5436	}
5437
5438	scsi_print_inquiry_short_sbuf(sb, inq_data);
5439
5440	sbuf_printf(sb, "%s %s ", SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed", dtype);
5441
5442	if (SID_ANSI_REV(inq_data) == SCSI_REV_0)
5443		sbuf_printf(sb, "SCSI ");
5444	else if (SID_ANSI_REV(inq_data) <= SCSI_REV_SPC) {
5445		sbuf_printf(sb, "SCSI-%d ", SID_ANSI_REV(inq_data));
5446	} else {
5447		sbuf_printf(sb, "SPC-%d SCSI ", SID_ANSI_REV(inq_data) - 2);
5448	}
5449	sbuf_printf(sb, "device%s\n", qtype);
5450}
5451
5452void
5453scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
5454{
5455	struct sbuf	sb;
5456	char		buffer[120];
5457
5458	sbuf_new(&sb, buffer, 120, SBUF_FIXEDLEN);
5459	scsi_print_inquiry_sbuf(&sb, inq_data);
5460	sbuf_finish(&sb);
5461	sbuf_putbuf(&sb);
5462}
5463
5464void
5465scsi_print_inquiry_short_sbuf(struct sbuf *sb, struct scsi_inquiry_data *inq_data)
5466{
5467
5468	sbuf_printf(sb, "<");
5469	cam_strvis_sbuf(sb, inq_data->vendor, sizeof(inq_data->vendor), 0);
5470	sbuf_printf(sb, " ");
5471	cam_strvis_sbuf(sb, inq_data->product, sizeof(inq_data->product), 0);
5472	sbuf_printf(sb, " ");
5473	cam_strvis_sbuf(sb, inq_data->revision, sizeof(inq_data->revision), 0);
5474	sbuf_printf(sb, "> ");
5475}
5476
5477void
5478scsi_print_inquiry_short(struct scsi_inquiry_data *inq_data)
5479{
5480	struct sbuf	sb;
5481	char		buffer[84];
5482
5483	sbuf_new(&sb, buffer, 84, SBUF_FIXEDLEN);
5484	scsi_print_inquiry_short_sbuf(&sb, inq_data);
5485	sbuf_finish(&sb);
5486	sbuf_putbuf(&sb);
5487}
5488
5489/*
5490 * Table of syncrates that don't follow the "divisible by 4"
5491 * rule. This table will be expanded in future SCSI specs.
5492 */
5493static struct {
5494	u_int period_factor;
5495	u_int period;	/* in 100ths of ns */
5496} scsi_syncrates[] = {
5497	{ 0x08, 625 },	/* FAST-160 */
5498	{ 0x09, 1250 },	/* FAST-80 */
5499	{ 0x0a, 2500 },	/* FAST-40 40MHz */
5500	{ 0x0b, 3030 },	/* FAST-40 33MHz */
5501	{ 0x0c, 5000 }	/* FAST-20 */
5502};
5503
5504/*
5505 * Return the frequency in kHz corresponding to the given
5506 * sync period factor.
5507 */
5508u_int
5509scsi_calc_syncsrate(u_int period_factor)
5510{
5511	u_int i;
5512	u_int num_syncrates;
5513
5514	/*
5515	 * It's a bug if period is zero, but if it is anyway, don't
5516	 * die with a divide fault- instead return something which
5517	 * 'approximates' async
5518	 */
5519	if (period_factor == 0) {
5520		return (3300);
5521	}
5522
5523	num_syncrates = nitems(scsi_syncrates);
5524	/* See if the period is in the "exception" table */
5525	for (i = 0; i < num_syncrates; i++) {
5526		if (period_factor == scsi_syncrates[i].period_factor) {
5527			/* Period in kHz */
5528			return (100000000 / scsi_syncrates[i].period);
5529		}
5530	}
5531
5532	/*
5533	 * Wasn't in the table, so use the standard
5534	 * 4 times conversion.
5535	 */
5536	return (10000000 / (period_factor * 4 * 10));
5537}
5538
5539/*
5540 * Return the SCSI sync parameter that corresponds to
5541 * the passed in period in 10ths of ns.
5542 */
5543u_int
5544scsi_calc_syncparam(u_int period)
5545{
5546	u_int i;
5547	u_int num_syncrates;
5548
5549	if (period == 0)
5550		return (~0);	/* Async */
5551
5552	/* Adjust for exception table being in 100ths. */
5553	period *= 10;
5554	num_syncrates = nitems(scsi_syncrates);
5555	/* See if the period is in the "exception" table */
5556	for (i = 0; i < num_syncrates; i++) {
5557		if (period <= scsi_syncrates[i].period) {
5558			/* Period in 100ths of ns */
5559			return (scsi_syncrates[i].period_factor);
5560		}
5561	}
5562
5563	/*
5564	 * Wasn't in the table, so use the standard
5565	 * 1/4 period in ns conversion.
5566	 */
5567	return (period/400);
5568}
5569
5570int
5571scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
5572{
5573	struct scsi_vpd_id_descriptor *descr;
5574	struct scsi_vpd_id_naa_basic *naa;
5575	int n;
5576
5577	descr = (struct scsi_vpd_id_descriptor *)bufp;
5578	naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
5579	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5580		return 0;
5581	if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
5582		return 0;
5583	n = naa->naa >> SVPD_ID_NAA_NAA_SHIFT;
5584	if (n != SVPD_ID_NAA_LOCAL_REG && n != SVPD_ID_NAA_IEEE_REG)
5585		return 0;
5586	return 1;
5587}
5588
5589int
5590scsi_devid_is_sas_target(uint8_t *bufp)
5591{
5592	struct scsi_vpd_id_descriptor *descr;
5593
5594	descr = (struct scsi_vpd_id_descriptor *)bufp;
5595	if (!scsi_devid_is_naa_ieee_reg(bufp))
5596		return 0;
5597	if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
5598		return 0;
5599	if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
5600		return 0;
5601	return 1;
5602}
5603
5604int
5605scsi_devid_is_lun_eui64(uint8_t *bufp)
5606{
5607	struct scsi_vpd_id_descriptor *descr;
5608
5609	descr = (struct scsi_vpd_id_descriptor *)bufp;
5610	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5611		return 0;
5612	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_EUI64)
5613		return 0;
5614	return 1;
5615}
5616
5617int
5618scsi_devid_is_lun_naa(uint8_t *bufp)
5619{
5620	struct scsi_vpd_id_descriptor *descr;
5621
5622	descr = (struct scsi_vpd_id_descriptor *)bufp;
5623	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5624		return 0;
5625	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5626		return 0;
5627	return 1;
5628}
5629
5630int
5631scsi_devid_is_lun_t10(uint8_t *bufp)
5632{
5633	struct scsi_vpd_id_descriptor *descr;
5634
5635	descr = (struct scsi_vpd_id_descriptor *)bufp;
5636	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5637		return 0;
5638	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_T10)
5639		return 0;
5640	return 1;
5641}
5642
5643int
5644scsi_devid_is_lun_name(uint8_t *bufp)
5645{
5646	struct scsi_vpd_id_descriptor *descr;
5647
5648	descr = (struct scsi_vpd_id_descriptor *)bufp;
5649	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5650		return 0;
5651	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_SCSI_NAME)
5652		return 0;
5653	return 1;
5654}
5655
5656int
5657scsi_devid_is_lun_md5(uint8_t *bufp)
5658{
5659	struct scsi_vpd_id_descriptor *descr;
5660
5661	descr = (struct scsi_vpd_id_descriptor *)bufp;
5662	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5663		return 0;
5664	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_MD5_LUN_ID)
5665		return 0;
5666	return 1;
5667}
5668
5669int
5670scsi_devid_is_lun_uuid(uint8_t *bufp)
5671{
5672	struct scsi_vpd_id_descriptor *descr;
5673
5674	descr = (struct scsi_vpd_id_descriptor *)bufp;
5675	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5676		return 0;
5677	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_UUID)
5678		return 0;
5679	return 1;
5680}
5681
5682int
5683scsi_devid_is_port_naa(uint8_t *bufp)
5684{
5685	struct scsi_vpd_id_descriptor *descr;
5686
5687	descr = (struct scsi_vpd_id_descriptor *)bufp;
5688	if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_PORT)
5689		return 0;
5690	if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5691		return 0;
5692	return 1;
5693}
5694
5695struct scsi_vpd_id_descriptor *
5696scsi_get_devid_desc(struct scsi_vpd_id_descriptor *desc, uint32_t len,
5697    scsi_devid_checkfn_t ck_fn)
5698{
5699	uint8_t *desc_buf_end;
5700
5701	desc_buf_end = (uint8_t *)desc + len;
5702
5703	for (; desc->identifier <= desc_buf_end &&
5704	    desc->identifier + desc->length <= desc_buf_end;
5705	    desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
5706						    + desc->length)) {
5707		if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
5708			return (desc);
5709	}
5710	return (NULL);
5711}
5712
5713struct scsi_vpd_id_descriptor *
5714scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
5715    scsi_devid_checkfn_t ck_fn)
5716{
5717	uint32_t len;
5718
5719	if (page_len < sizeof(*id))
5720		return (NULL);
5721	len = MIN(scsi_2btoul(id->length), page_len - sizeof(*id));
5722	return (scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5723	    id->desc_list, len, ck_fn));
5724}
5725
5726int
5727scsi_transportid_sbuf(struct sbuf *sb, struct scsi_transportid_header *hdr,
5728		      uint32_t valid_len)
5729{
5730	switch (hdr->format_protocol & SCSI_TRN_PROTO_MASK) {
5731	case SCSI_PROTO_FC: {
5732		struct scsi_transportid_fcp *fcp;
5733		uint64_t n_port_name;
5734
5735		fcp = (struct scsi_transportid_fcp *)hdr;
5736
5737		n_port_name = scsi_8btou64(fcp->n_port_name);
5738
5739		sbuf_printf(sb, "FCP address: 0x%.16jx",(uintmax_t)n_port_name);
5740		break;
5741	}
5742	case SCSI_PROTO_SPI: {
5743		struct scsi_transportid_spi *spi;
5744
5745		spi = (struct scsi_transportid_spi *)hdr;
5746
5747		sbuf_printf(sb, "SPI address: %u,%u",
5748			    scsi_2btoul(spi->scsi_addr),
5749			    scsi_2btoul(spi->rel_trgt_port_id));
5750		break;
5751	}
5752	case SCSI_PROTO_SSA:
5753		/*
5754		 * XXX KDM there is no transport ID defined in SPC-4 for
5755		 * SSA.
5756		 */
5757		break;
5758	case SCSI_PROTO_1394: {
5759		struct scsi_transportid_1394 *sbp;
5760		uint64_t eui64;
5761
5762		sbp = (struct scsi_transportid_1394 *)hdr;
5763
5764		eui64 = scsi_8btou64(sbp->eui64);
5765		sbuf_printf(sb, "SBP address: 0x%.16jx", (uintmax_t)eui64);
5766		break;
5767	}
5768	case SCSI_PROTO_RDMA: {
5769		struct scsi_transportid_rdma *rdma;
5770		unsigned int i;
5771
5772		rdma = (struct scsi_transportid_rdma *)hdr;
5773
5774		sbuf_printf(sb, "RDMA address: 0x");
5775		for (i = 0; i < sizeof(rdma->initiator_port_id); i++)
5776			sbuf_printf(sb, "%02x", rdma->initiator_port_id[i]);
5777		break;
5778	}
5779	case SCSI_PROTO_ISCSI: {
5780		uint32_t add_len, i;
5781		uint8_t *iscsi_name = NULL;
5782		int nul_found = 0;
5783
5784		sbuf_printf(sb, "iSCSI address: ");
5785		if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5786		    SCSI_TRN_ISCSI_FORMAT_DEVICE) {
5787			struct scsi_transportid_iscsi_device *dev;
5788
5789			dev = (struct scsi_transportid_iscsi_device *)hdr;
5790
5791			/*
5792			 * Verify how much additional data we really have.
5793			 */
5794			add_len = scsi_2btoul(dev->additional_length);
5795			add_len = MIN(add_len, valid_len -
5796				__offsetof(struct scsi_transportid_iscsi_device,
5797					   iscsi_name));
5798			iscsi_name = &dev->iscsi_name[0];
5799
5800		} else if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5801			    SCSI_TRN_ISCSI_FORMAT_PORT) {
5802			struct scsi_transportid_iscsi_port *port;
5803
5804			port = (struct scsi_transportid_iscsi_port *)hdr;
5805
5806			add_len = scsi_2btoul(port->additional_length);
5807			add_len = MIN(add_len, valid_len -
5808				__offsetof(struct scsi_transportid_iscsi_port,
5809					   iscsi_name));
5810			iscsi_name = &port->iscsi_name[0];
5811		} else {
5812			sbuf_printf(sb, "unknown format %x",
5813				    (hdr->format_protocol &
5814				     SCSI_TRN_FORMAT_MASK) >>
5815				     SCSI_TRN_FORMAT_SHIFT);
5816			break;
5817		}
5818		if (add_len == 0) {
5819			sbuf_printf(sb, "not enough data");
5820			break;
5821		}
5822		/*
5823		 * This is supposed to be a NUL-terminated ASCII
5824		 * string, but you never know.  So we're going to
5825		 * check.  We need to do this because there is no
5826		 * sbuf equivalent of strncat().
5827		 */
5828		for (i = 0; i < add_len; i++) {
5829			if (iscsi_name[i] == '\0') {
5830				nul_found = 1;
5831				break;
5832			}
5833		}
5834		/*
5835		 * If there is a NUL in the name, we can just use
5836		 * sbuf_cat().  Otherwise we need to use sbuf_bcat().
5837		 */
5838		if (nul_found != 0)
5839			sbuf_cat(sb, iscsi_name);
5840		else
5841			sbuf_bcat(sb, iscsi_name, add_len);
5842		break;
5843	}
5844	case SCSI_PROTO_SAS: {
5845		struct scsi_transportid_sas *sas;
5846		uint64_t sas_addr;
5847
5848		sas = (struct scsi_transportid_sas *)hdr;
5849
5850		sas_addr = scsi_8btou64(sas->sas_address);
5851		sbuf_printf(sb, "SAS address: 0x%.16jx", (uintmax_t)sas_addr);
5852		break;
5853	}
5854	case SCSI_PROTO_ADITP:
5855	case SCSI_PROTO_ATA:
5856	case SCSI_PROTO_UAS:
5857		/*
5858		 * No Transport ID format for ADI, ATA or USB is defined in
5859		 * SPC-4.
5860		 */
5861		sbuf_printf(sb, "No known Transport ID format for protocol "
5862			    "%#x", hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5863		break;
5864	case SCSI_PROTO_SOP: {
5865		struct scsi_transportid_sop *sop;
5866		struct scsi_sop_routing_id_norm *rid;
5867
5868		sop = (struct scsi_transportid_sop *)hdr;
5869		rid = (struct scsi_sop_routing_id_norm *)sop->routing_id;
5870
5871		/*
5872		 * Note that there is no alternate format specified in SPC-4
5873		 * for the PCIe routing ID, so we don't really have a way
5874		 * to know whether the second byte of the routing ID is
5875		 * a device and function or just a function.  So we just
5876		 * assume bus,device,function.
5877		 */
5878		sbuf_printf(sb, "SOP Routing ID: %u,%u,%u",
5879			    rid->bus, rid->devfunc >> SCSI_TRN_SOP_DEV_SHIFT,
5880			    rid->devfunc & SCSI_TRN_SOP_FUNC_NORM_MAX);
5881		break;
5882	}
5883	case SCSI_PROTO_NONE:
5884	default:
5885		sbuf_printf(sb, "Unknown protocol %#x",
5886			    hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5887		break;
5888	}
5889
5890	return (0);
5891}
5892
5893struct scsi_nv scsi_proto_map[] = {
5894	{ "fcp", SCSI_PROTO_FC },
5895	{ "spi", SCSI_PROTO_SPI },
5896	{ "ssa", SCSI_PROTO_SSA },
5897	{ "sbp", SCSI_PROTO_1394 },
5898	{ "1394", SCSI_PROTO_1394 },
5899	{ "srp", SCSI_PROTO_RDMA },
5900	{ "rdma", SCSI_PROTO_RDMA },
5901	{ "iscsi", SCSI_PROTO_ISCSI },
5902	{ "iqn", SCSI_PROTO_ISCSI },
5903	{ "sas", SCSI_PROTO_SAS },
5904	{ "aditp", SCSI_PROTO_ADITP },
5905	{ "ata", SCSI_PROTO_ATA },
5906	{ "uas", SCSI_PROTO_UAS },
5907	{ "usb", SCSI_PROTO_UAS },
5908	{ "sop", SCSI_PROTO_SOP }
5909};
5910
5911const char *
5912scsi_nv_to_str(struct scsi_nv *table, int num_table_entries, uint64_t value)
5913{
5914	int i;
5915
5916	for (i = 0; i < num_table_entries; i++) {
5917		if (table[i].value == value)
5918			return (table[i].name);
5919	}
5920
5921	return (NULL);
5922}
5923
5924/*
5925 * Given a name/value table, find a value matching the given name.
5926 * Return values:
5927 *	SCSI_NV_FOUND - match found
5928 *	SCSI_NV_AMBIGUOUS - more than one match, none of them exact
5929 *	SCSI_NV_NOT_FOUND - no match found
5930 */
5931scsi_nv_status
5932scsi_get_nv(struct scsi_nv *table, int num_table_entries,
5933	    char *name, int *table_entry, scsi_nv_flags flags)
5934{
5935	int i, num_matches = 0;
5936
5937	for (i = 0; i < num_table_entries; i++) {
5938		size_t table_len, name_len;
5939
5940		table_len = strlen(table[i].name);
5941		name_len = strlen(name);
5942
5943		if ((((flags & SCSI_NV_FLAG_IG_CASE) != 0)
5944		  && (strncasecmp(table[i].name, name, name_len) == 0))
5945		|| (((flags & SCSI_NV_FLAG_IG_CASE) == 0)
5946		 && (strncmp(table[i].name, name, name_len) == 0))) {
5947			*table_entry = i;
5948
5949			/*
5950			 * Check for an exact match.  If we have the same
5951			 * number of characters in the table as the argument,
5952			 * and we already know they're the same, we have
5953			 * an exact match.
5954		 	 */
5955			if (table_len == name_len)
5956				return (SCSI_NV_FOUND);
5957
5958			/*
5959			 * Otherwise, bump up the number of matches.  We'll
5960			 * see later how many we have.
5961			 */
5962			num_matches++;
5963		}
5964	}
5965
5966	if (num_matches > 1)
5967		return (SCSI_NV_AMBIGUOUS);
5968	else if (num_matches == 1)
5969		return (SCSI_NV_FOUND);
5970	else
5971		return (SCSI_NV_NOT_FOUND);
5972}
5973
5974/*
5975 * Parse transport IDs for Fibre Channel, 1394 and SAS.  Since these are
5976 * all 64-bit numbers, the code is similar.
5977 */
5978int
5979scsi_parse_transportid_64bit(int proto_id, char *id_str,
5980			     struct scsi_transportid_header **hdr,
5981			     unsigned int *alloc_len,
5982#ifdef _KERNEL
5983			     struct malloc_type *type, int flags,
5984#endif
5985			     char *error_str, int error_str_len)
5986{
5987	uint64_t value;
5988	char *endptr;
5989	int retval;
5990	size_t alloc_size;
5991
5992	retval = 0;
5993
5994	value = strtouq(id_str, &endptr, 0);
5995	if (*endptr != '\0') {
5996		if (error_str != NULL) {
5997			snprintf(error_str, error_str_len, "%s: error "
5998				 "parsing ID %s, 64-bit number required",
5999				 __func__, id_str);
6000		}
6001		retval = 1;
6002		goto bailout;
6003	}
6004
6005	switch (proto_id) {
6006	case SCSI_PROTO_FC:
6007		alloc_size = sizeof(struct scsi_transportid_fcp);
6008		break;
6009	case SCSI_PROTO_1394:
6010		alloc_size = sizeof(struct scsi_transportid_1394);
6011		break;
6012	case SCSI_PROTO_SAS:
6013		alloc_size = sizeof(struct scsi_transportid_sas);
6014		break;
6015	default:
6016		if (error_str != NULL) {
6017			snprintf(error_str, error_str_len, "%s: unsupported "
6018				 "protocol %d", __func__, proto_id);
6019		}
6020		retval = 1;
6021		goto bailout;
6022		break; /* NOTREACHED */
6023	}
6024#ifdef _KERNEL
6025	*hdr = malloc(alloc_size, type, flags);
6026#else /* _KERNEL */
6027	*hdr = malloc(alloc_size);
6028#endif /*_KERNEL */
6029	if (*hdr == NULL) {
6030		if (error_str != NULL) {
6031			snprintf(error_str, error_str_len, "%s: unable to "
6032				 "allocate %zu bytes", __func__, alloc_size);
6033		}
6034		retval = 1;
6035		goto bailout;
6036	}
6037
6038	*alloc_len = alloc_size;
6039
6040	bzero(*hdr, alloc_size);
6041
6042	switch (proto_id) {
6043	case SCSI_PROTO_FC: {
6044		struct scsi_transportid_fcp *fcp;
6045
6046		fcp = (struct scsi_transportid_fcp *)(*hdr);
6047		fcp->format_protocol = SCSI_PROTO_FC |
6048				       SCSI_TRN_FCP_FORMAT_DEFAULT;
6049		scsi_u64to8b(value, fcp->n_port_name);
6050		break;
6051	}
6052	case SCSI_PROTO_1394: {
6053		struct scsi_transportid_1394 *sbp;
6054
6055		sbp = (struct scsi_transportid_1394 *)(*hdr);
6056		sbp->format_protocol = SCSI_PROTO_1394 |
6057				       SCSI_TRN_1394_FORMAT_DEFAULT;
6058		scsi_u64to8b(value, sbp->eui64);
6059		break;
6060	}
6061	case SCSI_PROTO_SAS: {
6062		struct scsi_transportid_sas *sas;
6063
6064		sas = (struct scsi_transportid_sas *)(*hdr);
6065		sas->format_protocol = SCSI_PROTO_SAS |
6066				       SCSI_TRN_SAS_FORMAT_DEFAULT;
6067		scsi_u64to8b(value, sas->sas_address);
6068		break;
6069	}
6070	default:
6071		break;
6072	}
6073bailout:
6074	return (retval);
6075}
6076
6077/*
6078 * Parse a SPI (Parallel SCSI) address of the form: id,rel_tgt_port
6079 */
6080int
6081scsi_parse_transportid_spi(char *id_str, struct scsi_transportid_header **hdr,
6082			   unsigned int *alloc_len,
6083#ifdef _KERNEL
6084			   struct malloc_type *type, int flags,
6085#endif
6086			   char *error_str, int error_str_len)
6087{
6088	unsigned long scsi_addr, target_port;
6089	struct scsi_transportid_spi *spi;
6090	char *tmpstr, *endptr;
6091	int retval;
6092
6093	retval = 0;
6094
6095	tmpstr = strsep(&id_str, ",");
6096	if (tmpstr == NULL) {
6097		if (error_str != NULL) {
6098			snprintf(error_str, error_str_len,
6099				 "%s: no ID found", __func__);
6100		}
6101		retval = 1;
6102		goto bailout;
6103	}
6104	scsi_addr = strtoul(tmpstr, &endptr, 0);
6105	if (*endptr != '\0') {
6106		if (error_str != NULL) {
6107			snprintf(error_str, error_str_len, "%s: error "
6108				 "parsing SCSI ID %s, number required",
6109				 __func__, tmpstr);
6110		}
6111		retval = 1;
6112		goto bailout;
6113	}
6114
6115	if (id_str == NULL) {
6116		if (error_str != NULL) {
6117			snprintf(error_str, error_str_len, "%s: no relative "
6118				 "target port found", __func__);
6119		}
6120		retval = 1;
6121		goto bailout;
6122	}
6123
6124	target_port = strtoul(id_str, &endptr, 0);
6125	if (*endptr != '\0') {
6126		if (error_str != NULL) {
6127			snprintf(error_str, error_str_len, "%s: error "
6128				 "parsing relative target port %s, number "
6129				 "required", __func__, id_str);
6130		}
6131		retval = 1;
6132		goto bailout;
6133	}
6134#ifdef _KERNEL
6135	spi = malloc(sizeof(*spi), type, flags);
6136#else
6137	spi = malloc(sizeof(*spi));
6138#endif
6139	if (spi == NULL) {
6140		if (error_str != NULL) {
6141			snprintf(error_str, error_str_len, "%s: unable to "
6142				 "allocate %zu bytes", __func__,
6143				 sizeof(*spi));
6144		}
6145		retval = 1;
6146		goto bailout;
6147	}
6148	*alloc_len = sizeof(*spi);
6149	bzero(spi, sizeof(*spi));
6150
6151	spi->format_protocol = SCSI_PROTO_SPI | SCSI_TRN_SPI_FORMAT_DEFAULT;
6152	scsi_ulto2b(scsi_addr, spi->scsi_addr);
6153	scsi_ulto2b(target_port, spi->rel_trgt_port_id);
6154
6155	*hdr = (struct scsi_transportid_header *)spi;
6156bailout:
6157	return (retval);
6158}
6159
6160/*
6161 * Parse an RDMA/SRP Initiator Port ID string.  This is 32 hexadecimal digits,
6162 * optionally prefixed by "0x" or "0X".
6163 */
6164int
6165scsi_parse_transportid_rdma(char *id_str, struct scsi_transportid_header **hdr,
6166			    unsigned int *alloc_len,
6167#ifdef _KERNEL
6168			    struct malloc_type *type, int flags,
6169#endif
6170			    char *error_str, int error_str_len)
6171{
6172	struct scsi_transportid_rdma *rdma;
6173	int retval;
6174	size_t id_len, rdma_id_size;
6175	uint8_t rdma_id[SCSI_TRN_RDMA_PORT_LEN];
6176	char *tmpstr;
6177	unsigned int i, j;
6178
6179	retval = 0;
6180	id_len = strlen(id_str);
6181	rdma_id_size = SCSI_TRN_RDMA_PORT_LEN;
6182
6183	/*
6184	 * Check the size.  It needs to be either 32 or 34 characters long.
6185	 */
6186	if ((id_len != (rdma_id_size * 2))
6187	 && (id_len != ((rdma_id_size * 2) + 2))) {
6188		if (error_str != NULL) {
6189			snprintf(error_str, error_str_len, "%s: RDMA ID "
6190				 "must be 32 hex digits (0x prefix "
6191				 "optional), only %zu seen", __func__, id_len);
6192		}
6193		retval = 1;
6194		goto bailout;
6195	}
6196
6197	tmpstr = id_str;
6198	/*
6199	 * If the user gave us 34 characters, the string needs to start
6200	 * with '0x'.
6201	 */
6202	if (id_len == ((rdma_id_size * 2) + 2)) {
6203	 	if ((tmpstr[0] == '0')
6204		 && ((tmpstr[1] == 'x') || (tmpstr[1] == 'X'))) {
6205			tmpstr += 2;
6206		} else {
6207			if (error_str != NULL) {
6208				snprintf(error_str, error_str_len, "%s: RDMA "
6209					 "ID prefix, if used, must be \"0x\", "
6210					 "got %s", __func__, tmpstr);
6211			}
6212			retval = 1;
6213			goto bailout;
6214		}
6215	}
6216	bzero(rdma_id, sizeof(rdma_id));
6217
6218	/*
6219	 * Convert ASCII hex into binary bytes.  There is no standard
6220	 * 128-bit integer type, and so no strtou128t() routine to convert
6221	 * from hex into a large integer.  In the end, we're not going to
6222	 * an integer, but rather to a byte array, so that and the fact
6223	 * that we require the user to give us 32 hex digits simplifies the
6224	 * logic.
6225	 */
6226	for (i = 0; i < (rdma_id_size * 2); i++) {
6227		int cur_shift;
6228		unsigned char c;
6229
6230		/* Increment the byte array one for every 2 hex digits */
6231		j = i >> 1;
6232
6233		/*
6234		 * The first digit in every pair is the most significant
6235		 * 4 bits.  The second is the least significant 4 bits.
6236		 */
6237		if ((i % 2) == 0)
6238			cur_shift = 4;
6239		else
6240			cur_shift = 0;
6241
6242		c = tmpstr[i];
6243		/* Convert the ASCII hex character into a number */
6244		if (isdigit(c))
6245			c -= '0';
6246		else if (isalpha(c))
6247			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
6248		else {
6249			if (error_str != NULL) {
6250				snprintf(error_str, error_str_len, "%s: "
6251					 "RDMA ID must be hex digits, got "
6252					 "invalid character %c", __func__,
6253					 tmpstr[i]);
6254			}
6255			retval = 1;
6256			goto bailout;
6257		}
6258		/*
6259		 * The converted number can't be less than 0; the type is
6260		 * unsigned, and the subtraction logic will not give us
6261		 * a negative number.  So we only need to make sure that
6262		 * the value is not greater than 0xf.  (i.e. make sure the
6263		 * user didn't give us a value like "0x12jklmno").
6264		 */
6265		if (c > 0xf) {
6266			if (error_str != NULL) {
6267				snprintf(error_str, error_str_len, "%s: "
6268					 "RDMA ID must be hex digits, got "
6269					 "invalid character %c", __func__,
6270					 tmpstr[i]);
6271			}
6272			retval = 1;
6273			goto bailout;
6274		}
6275
6276		rdma_id[j] |= c << cur_shift;
6277	}
6278
6279#ifdef _KERNEL
6280	rdma = malloc(sizeof(*rdma), type, flags);
6281#else
6282	rdma = malloc(sizeof(*rdma));
6283#endif
6284	if (rdma == NULL) {
6285		if (error_str != NULL) {
6286			snprintf(error_str, error_str_len, "%s: unable to "
6287				 "allocate %zu bytes", __func__,
6288				 sizeof(*rdma));
6289		}
6290		retval = 1;
6291		goto bailout;
6292	}
6293	*alloc_len = sizeof(*rdma);
6294	bzero(rdma, *alloc_len);
6295
6296	rdma->format_protocol = SCSI_PROTO_RDMA | SCSI_TRN_RDMA_FORMAT_DEFAULT;
6297	bcopy(rdma_id, rdma->initiator_port_id, SCSI_TRN_RDMA_PORT_LEN);
6298
6299	*hdr = (struct scsi_transportid_header *)rdma;
6300
6301bailout:
6302	return (retval);
6303}
6304
6305/*
6306 * Parse an iSCSI name.  The format is either just the name:
6307 *
6308 *	iqn.2012-06.com.example:target0
6309 * or the name, separator and initiator session ID:
6310 *
6311 *	iqn.2012-06.com.example:target0,i,0x123
6312 *
6313 * The separator format is exact.
6314 */
6315int
6316scsi_parse_transportid_iscsi(char *id_str, struct scsi_transportid_header **hdr,
6317			     unsigned int *alloc_len,
6318#ifdef _KERNEL
6319			     struct malloc_type *type, int flags,
6320#endif
6321			     char *error_str, int error_str_len)
6322{
6323	size_t id_len, sep_len, id_size, name_len;
6324	int retval;
6325	unsigned int i, sep_pos, sep_found;
6326	const char *sep_template = ",i,0x";
6327	const char *iqn_prefix = "iqn.";
6328	struct scsi_transportid_iscsi_device *iscsi;
6329
6330	retval = 0;
6331	sep_found = 0;
6332
6333	id_len = strlen(id_str);
6334	sep_len = strlen(sep_template);
6335
6336	/*
6337	 * The separator is defined as exactly ',i,0x'.  Any other commas,
6338	 * or any other form, is an error.  So look for a comma, and once
6339	 * we find that, the next few characters must match the separator
6340	 * exactly.  Once we get through the separator, there should be at
6341	 * least one character.
6342	 */
6343	for (i = 0, sep_pos = 0; i < id_len; i++) {
6344		if (sep_pos == 0) {
6345		 	if (id_str[i] == sep_template[sep_pos])
6346				sep_pos++;
6347
6348			continue;
6349		}
6350		if (sep_pos < sep_len) {
6351			if (id_str[i] == sep_template[sep_pos]) {
6352				sep_pos++;
6353				continue;
6354			}
6355			if (error_str != NULL) {
6356				snprintf(error_str, error_str_len, "%s: "
6357					 "invalid separator in iSCSI name "
6358					 "\"%s\"",
6359					 __func__, id_str);
6360			}
6361			retval = 1;
6362			goto bailout;
6363		} else {
6364			sep_found = 1;
6365			break;
6366		}
6367	}
6368
6369	/*
6370	 * Check to see whether we have a separator but no digits after it.
6371	 */
6372	if ((sep_pos != 0)
6373	 && (sep_found == 0)) {
6374		if (error_str != NULL) {
6375			snprintf(error_str, error_str_len, "%s: no digits "
6376				 "found after separator in iSCSI name \"%s\"",
6377				 __func__, id_str);
6378		}
6379		retval = 1;
6380		goto bailout;
6381	}
6382
6383	/*
6384	 * The incoming ID string has the "iqn." prefix stripped off.  We
6385	 * need enough space for the base structure (the structures are the
6386	 * same for the two iSCSI forms), the prefix, the ID string and a
6387	 * terminating NUL.
6388	 */
6389	id_size = sizeof(*iscsi) + strlen(iqn_prefix) + id_len + 1;
6390
6391#ifdef _KERNEL
6392	iscsi = malloc(id_size, type, flags);
6393#else
6394	iscsi = malloc(id_size);
6395#endif
6396	if (iscsi == NULL) {
6397		if (error_str != NULL) {
6398			snprintf(error_str, error_str_len, "%s: unable to "
6399				 "allocate %zu bytes", __func__, id_size);
6400		}
6401		retval = 1;
6402		goto bailout;
6403	}
6404	*alloc_len = id_size;
6405	bzero(iscsi, id_size);
6406
6407	iscsi->format_protocol = SCSI_PROTO_ISCSI;
6408	if (sep_found == 0)
6409		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_DEVICE;
6410	else
6411		iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_PORT;
6412	name_len = id_size - sizeof(*iscsi);
6413	scsi_ulto2b(name_len, iscsi->additional_length);
6414	snprintf(iscsi->iscsi_name, name_len, "%s%s", iqn_prefix, id_str);
6415
6416	*hdr = (struct scsi_transportid_header *)iscsi;
6417
6418bailout:
6419	return (retval);
6420}
6421
6422/*
6423 * Parse a SCSI over PCIe (SOP) identifier.  The Routing ID can either be
6424 * of the form 'bus,device,function' or 'bus,function'.
6425 */
6426int
6427scsi_parse_transportid_sop(char *id_str, struct scsi_transportid_header **hdr,
6428			   unsigned int *alloc_len,
6429#ifdef _KERNEL
6430			   struct malloc_type *type, int flags,
6431#endif
6432			   char *error_str, int error_str_len)
6433{
6434	struct scsi_transportid_sop *sop;
6435	unsigned long bus, device, function;
6436	char *tmpstr, *endptr;
6437	int retval, device_spec;
6438
6439	retval = 0;
6440	device_spec = 0;
6441	device = 0;
6442
6443	tmpstr = strsep(&id_str, ",");
6444	if ((tmpstr == NULL)
6445	 || (*tmpstr == '\0')) {
6446		if (error_str != NULL) {
6447			snprintf(error_str, error_str_len, "%s: no ID found",
6448				 __func__);
6449		}
6450		retval = 1;
6451		goto bailout;
6452	}
6453	bus = strtoul(tmpstr, &endptr, 0);
6454	if (*endptr != '\0') {
6455		if (error_str != NULL) {
6456			snprintf(error_str, error_str_len, "%s: error "
6457				 "parsing PCIe bus %s, number required",
6458				 __func__, tmpstr);
6459		}
6460		retval = 1;
6461		goto bailout;
6462	}
6463	if ((id_str == NULL)
6464	 || (*id_str == '\0')) {
6465		if (error_str != NULL) {
6466			snprintf(error_str, error_str_len, "%s: no PCIe "
6467				 "device or function found", __func__);
6468		}
6469		retval = 1;
6470		goto bailout;
6471	}
6472	tmpstr = strsep(&id_str, ",");
6473	function = strtoul(tmpstr, &endptr, 0);
6474	if (*endptr != '\0') {
6475		if (error_str != NULL) {
6476			snprintf(error_str, error_str_len, "%s: error "
6477				 "parsing PCIe device/function %s, number "
6478				 "required", __func__, tmpstr);
6479		}
6480		retval = 1;
6481		goto bailout;
6482	}
6483	/*
6484	 * Check to see whether the user specified a third value.  If so,
6485	 * the second is the device.
6486	 */
6487	if (id_str != NULL) {
6488		if (*id_str == '\0') {
6489			if (error_str != NULL) {
6490				snprintf(error_str, error_str_len, "%s: "
6491					 "no PCIe function found", __func__);
6492			}
6493			retval = 1;
6494			goto bailout;
6495		}
6496		device = function;
6497		device_spec = 1;
6498		function = strtoul(id_str, &endptr, 0);
6499		if (*endptr != '\0') {
6500			if (error_str != NULL) {
6501				snprintf(error_str, error_str_len, "%s: "
6502					 "error parsing PCIe function %s, "
6503					 "number required", __func__, id_str);
6504			}
6505			retval = 1;
6506			goto bailout;
6507		}
6508	}
6509	if (bus > SCSI_TRN_SOP_BUS_MAX) {
6510		if (error_str != NULL) {
6511			snprintf(error_str, error_str_len, "%s: bus value "
6512				 "%lu greater than maximum %u", __func__,
6513				 bus, SCSI_TRN_SOP_BUS_MAX);
6514		}
6515		retval = 1;
6516		goto bailout;
6517	}
6518
6519	if ((device_spec != 0)
6520	 && (device > SCSI_TRN_SOP_DEV_MASK)) {
6521		if (error_str != NULL) {
6522			snprintf(error_str, error_str_len, "%s: device value "
6523				 "%lu greater than maximum %u", __func__,
6524				 device, SCSI_TRN_SOP_DEV_MAX);
6525		}
6526		retval = 1;
6527		goto bailout;
6528	}
6529
6530	if (((device_spec != 0)
6531	  && (function > SCSI_TRN_SOP_FUNC_NORM_MAX))
6532	 || ((device_spec == 0)
6533	  && (function > SCSI_TRN_SOP_FUNC_ALT_MAX))) {
6534		if (error_str != NULL) {
6535			snprintf(error_str, error_str_len, "%s: function value "
6536				 "%lu greater than maximum %u", __func__,
6537				 function, (device_spec == 0) ?
6538				 SCSI_TRN_SOP_FUNC_ALT_MAX :
6539				 SCSI_TRN_SOP_FUNC_NORM_MAX);
6540		}
6541		retval = 1;
6542		goto bailout;
6543	}
6544
6545#ifdef _KERNEL
6546	sop = malloc(sizeof(*sop), type, flags);
6547#else
6548	sop = malloc(sizeof(*sop));
6549#endif
6550	if (sop == NULL) {
6551		if (error_str != NULL) {
6552			snprintf(error_str, error_str_len, "%s: unable to "
6553				 "allocate %zu bytes", __func__, sizeof(*sop));
6554		}
6555		retval = 1;
6556		goto bailout;
6557	}
6558	*alloc_len = sizeof(*sop);
6559	bzero(sop, sizeof(*sop));
6560	sop->format_protocol = SCSI_PROTO_SOP | SCSI_TRN_SOP_FORMAT_DEFAULT;
6561	if (device_spec != 0) {
6562		struct scsi_sop_routing_id_norm rid;
6563
6564		rid.bus = bus;
6565		rid.devfunc = (device << SCSI_TRN_SOP_DEV_SHIFT) | function;
6566		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6567		      sizeof(sop->routing_id)));
6568	} else {
6569		struct scsi_sop_routing_id_alt rid;
6570
6571		rid.bus = bus;
6572		rid.function = function;
6573		bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6574		      sizeof(sop->routing_id)));
6575	}
6576
6577	*hdr = (struct scsi_transportid_header *)sop;
6578bailout:
6579	return (retval);
6580}
6581
6582/*
6583 * transportid_str: NUL-terminated string with format: protcol,id
6584 *		    The ID is protocol specific.
6585 * hdr:		    Storage will be allocated for the transport ID.
6586 * alloc_len:	    The amount of memory allocated is returned here.
6587 * type:	    Malloc bucket (kernel only).
6588 * flags:	    Malloc flags (kernel only).
6589 * error_str:	    If non-NULL, it will contain error information (without
6590 * 		    a terminating newline) if an error is returned.
6591 * error_str_len:   Allocated length of the error string.
6592 *
6593 * Returns 0 for success, non-zero for failure.
6594 */
6595int
6596scsi_parse_transportid(char *transportid_str,
6597		       struct scsi_transportid_header **hdr,
6598		       unsigned int *alloc_len,
6599#ifdef _KERNEL
6600		       struct malloc_type *type, int flags,
6601#endif
6602		       char *error_str, int error_str_len)
6603{
6604	char *tmpstr;
6605	scsi_nv_status status;
6606	u_int num_proto_entries;
6607	int retval, table_entry;
6608
6609	retval = 0;
6610	table_entry = 0;
6611
6612	/*
6613	 * We do allow a period as well as a comma to separate the protocol
6614	 * from the ID string.  This is to accommodate iSCSI names, which
6615	 * start with "iqn.".
6616	 */
6617	tmpstr = strsep(&transportid_str, ",.");
6618	if (tmpstr == NULL) {
6619		if (error_str != NULL) {
6620			snprintf(error_str, error_str_len,
6621				 "%s: transportid_str is NULL", __func__);
6622		}
6623		retval = 1;
6624		goto bailout;
6625	}
6626
6627	num_proto_entries = nitems(scsi_proto_map);
6628	status = scsi_get_nv(scsi_proto_map, num_proto_entries, tmpstr,
6629			     &table_entry, SCSI_NV_FLAG_IG_CASE);
6630	if (status !=