]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cam/scsi/scsi_all.c
MFC r248992:
[FreeBSD/stable/8.git] / sys / cam / scsi / scsi_all.c
1 /*-
2  * Implementation of Utility functions for all SCSI device types.
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34
35 #ifdef _KERNEL
36 #include <opt_scsi.h>
37
38 #include <sys/systm.h>
39 #include <sys/libkern.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #else
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #endif
48
49 #include <cam/cam.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <sys/ata.h>
55 #include <sys/sbuf.h>
56 #ifndef _KERNEL
57 #include <camlib.h>
58
59 #ifndef FALSE
60 #define FALSE   0
61 #endif /* FALSE */
62 #ifndef TRUE
63 #define TRUE    1
64 #endif /* TRUE */
65 #define ERESTART        -1              /* restart syscall */
66 #define EJUSTRETURN     -2              /* don't modify regs, just return */
67 #endif /* !_KERNEL */
68
69 /*
70  * This is the default number of milliseconds we wait for devices to settle
71  * after a SCSI bus reset.
72  */
73 #ifndef SCSI_DELAY
74 #define SCSI_DELAY 2000
75 #endif
76 /*
77  * All devices need _some_ sort of bus settle delay, so we'll set it to
78  * a minimum value of 100ms. Note that this is pertinent only for SPI-
79  * not transport like Fibre Channel or iSCSI where 'delay' is completely
80  * meaningless.
81  */
82 #ifndef SCSI_MIN_DELAY
83 #define SCSI_MIN_DELAY 100
84 #endif
85 /*
86  * Make sure the user isn't using seconds instead of milliseconds.
87  */
88 #if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
89 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
90 #endif
91
92 int scsi_delay;
93
94 static int      ascentrycomp(const void *key, const void *member);
95 static int      senseentrycomp(const void *key, const void *member);
96 static void     fetchtableentries(int sense_key, int asc, int ascq,
97                                   struct scsi_inquiry_data *,
98                                   const struct sense_key_table_entry **,
99                                   const struct asc_table_entry **);
100 #ifdef _KERNEL
101 static void     init_scsi_delay(void);
102 static int      sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
103 static int      set_scsi_delay(int delay);
104 #endif
105
106 #if !defined(SCSI_NO_OP_STRINGS)
107
108 #define D       (1 << T_DIRECT)
109 #define T       (1 << T_SEQUENTIAL)
110 #define L       (1 << T_PRINTER)
111 #define P       (1 << T_PROCESSOR)
112 #define W       (1 << T_WORM)
113 #define R       (1 << T_CDROM)
114 #define O       (1 << T_OPTICAL)
115 #define M       (1 << T_CHANGER)
116 #define A       (1 << T_STORARRAY)
117 #define E       (1 << T_ENCLOSURE)
118 #define B       (1 << T_RBC)
119 #define K       (1 << T_OCRW)
120 #define V       (1 << T_ADC)
121 #define F       (1 << T_OSD)
122 #define S       (1 << T_SCANNER)
123 #define C       (1 << T_COMM)
124
125 #define ALL     (D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
126
127 static struct op_table_entry plextor_cd_ops[] = {
128         { 0xD8, R, "CD-DA READ" }
129 };
130
131 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
132         {
133                 /*
134                  * I believe that 0xD8 is the Plextor proprietary command
135                  * to read CD-DA data.  I'm not sure which Plextor CDROM
136                  * models support the command, though.  I know for sure
137                  * that the 4X, 8X, and 12X models do, and presumably the
138                  * 12-20X does.  I don't know about any earlier models,
139                  * though.  If anyone has any more complete information,
140                  * feel free to change this quirk entry.
141                  */
142                 {T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
143                 sizeof(plextor_cd_ops)/sizeof(struct op_table_entry),
144                 plextor_cd_ops
145         }
146 };
147
148 static struct op_table_entry scsi_op_codes[] = {
149         /*
150          * From: http://www.t10.org/lists/op-num.txt
151          * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
152          *              and Jung-uk Kim (jkim@FreeBSD.org)
153          *
154          * Note:  order is important in this table, scsi_op_desc() currently
155          * depends on the opcodes in the table being in order to save
156          * search time.
157          * Note:  scanner and comm. devices are carried over from the previous
158          * version because they were removed in the latest spec.
159          */
160         /* File: OP-NUM.TXT
161          *
162          * SCSI Operation Codes
163          * Numeric Sorted Listing
164          * as of  3/11/08
165          *
166          *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
167          *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
168          *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
169          *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
170          *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
171          *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
172          *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
173          *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
174          *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
175          *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
176          *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
177          *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
178          *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
179          *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
180          * OP  DTLPWROMAEBKVF  Description
181          * --  --------------  ---------------------------------------------- */
182         /* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
183         { 0x00, ALL, "TEST UNIT READY" },
184         /* 01   M              REWIND */
185         { 0x01, T, "REWIND" },
186         /* 01  Z V ZZZZ        REZERO UNIT */
187         { 0x01, D | W | R | O | M, "REZERO UNIT" },
188         /* 02  VVVVVV V */
189         /* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
190         { 0x03, ALL, "REQUEST SENSE" },
191         /* 04  M    OO         FORMAT UNIT */
192         { 0x04, D | R | O, "FORMAT UNIT" },
193         /* 04   O              FORMAT MEDIUM */
194         { 0x04, T, "FORMAT MEDIUM" },
195         /* 04    O             FORMAT */
196         { 0x04, L, "FORMAT" },
197         /* 05  VMVVVV V        READ BLOCK LIMITS */
198         { 0x05, T, "READ BLOCK LIMITS" },
199         /* 06  VVVVVV V */
200         /* 07  OVV O OV        REASSIGN BLOCKS */
201         { 0x07, D | W | O, "REASSIGN BLOCKS" },
202         /* 07         O        INITIALIZE ELEMENT STATUS */
203         { 0x07, M, "INITIALIZE ELEMENT STATUS" },
204         /* 08  MOV O OV        READ(6) */
205         { 0x08, D | T | W | O, "READ(6)" },
206         /* 08     O            RECEIVE */
207         { 0x08, P, "RECEIVE" },
208         /* 08                  GET MESSAGE(6) */
209         { 0x08, C, "GET MESSAGE(6)" },
210         /* 09  VVVVVV V */
211         /* 0A  OO  O OV        WRITE(6) */
212         { 0x0A, D | T | W | O, "WRITE(6)" },
213         /* 0A     M            SEND(6) */
214         { 0x0A, P, "SEND(6)" },
215         /* 0A                  SEND MESSAGE(6) */
216         { 0x0A, C, "SEND MESSAGE(6)" },
217         /* 0A    M             PRINT */
218         { 0x0A, L, "PRINT" },
219         /* 0B  Z   ZOZV        SEEK(6) */
220         { 0x0B, D | W | R | O, "SEEK(6)" },
221         /* 0B   O              SET CAPACITY */
222         { 0x0B, T, "SET CAPACITY" },
223         /* 0B    O             SLEW AND PRINT */
224         { 0x0B, L, "SLEW AND PRINT" },
225         /* 0C  VVVVVV V */
226         /* 0D  VVVVVV V */
227         /* 0E  VVVVVV V */
228         /* 0F  VOVVVV V        READ REVERSE(6) */
229         { 0x0F, T, "READ REVERSE(6)" },
230         /* 10  VM VVV          WRITE FILEMARKS(6) */
231         { 0x10, T, "WRITE FILEMARKS(6)" },
232         /* 10    O             SYNCHRONIZE BUFFER */
233         { 0x10, L, "SYNCHRONIZE BUFFER" },
234         /* 11  VMVVVV          SPACE(6) */
235         { 0x11, T, "SPACE(6)" },
236         /* 12  MMMMMMMMMMMMMM  INQUIRY */
237         { 0x12, ALL, "INQUIRY" },
238         /* 13  V VVVV */
239         /* 13   O              VERIFY(6) */
240         { 0x13, T, "VERIFY(6)" },
241         /* 14  VOOVVV          RECOVER BUFFERED DATA */
242         { 0x14, T | L, "RECOVER BUFFERED DATA" },
243         /* 15  OMO O OOOO OO   MODE SELECT(6) */
244         { 0x15, ALL & ~(P | R | B | F), "MODE SELECT(6)" },
245         /* 16  ZZMZO OOOZ O    RESERVE(6) */
246         { 0x16, ALL & ~(R | B | V | F | C), "RESERVE(6)" },
247         /* 16         Z        RESERVE ELEMENT(6) */
248         { 0x16, M, "RESERVE ELEMENT(6)" },
249         /* 17  ZZMZO OOOZ O    RELEASE(6) */
250         { 0x17, ALL & ~(R | B | V | F | C), "RELEASE(6)" },
251         /* 17         Z        RELEASE ELEMENT(6) */
252         { 0x17, M, "RELEASE ELEMENT(6)" },
253         /* 18  ZZZZOZO    Z    COPY */
254         { 0x18, D | T | L | P | W | R | O | K | S, "COPY" },
255         /* 19  VMVVVV          ERASE(6) */
256         { 0x19, T, "ERASE(6)" },
257         /* 1A  OMO O OOOO OO   MODE SENSE(6) */
258         { 0x1A, ALL & ~(P | R | B | F), "MODE SENSE(6)" },
259         /* 1B  O   OOO O MO O  START STOP UNIT */
260         { 0x1B, D | W | R | O | A | B | K | F, "START STOP UNIT" },
261         /* 1B   O          M   LOAD UNLOAD */
262         { 0x1B, T | V, "LOAD UNLOAD" },
263         /* 1B                  SCAN */
264         { 0x1B, S, "SCAN" },
265         /* 1B    O             STOP PRINT */
266         { 0x1B, L, "STOP PRINT" },
267         /* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
268         { 0x1B, M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
269         /* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
270         { 0x1C, ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
271         /* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
272         { 0x1D, ALL & ~(R | B), "SEND DIAGNOSTIC" },
273         /* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
274         { 0x1E, D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
275         /* 1F */
276         /* 20  V   VVV    V */
277         /* 21  V   VVV    V */
278         /* 22  V   VVV    V */
279         /* 23  V   V V    V */
280         /* 23       O          READ FORMAT CAPACITIES */
281         { 0x23, R, "READ FORMAT CAPACITIES" },
282         /* 24  V   VV          SET WINDOW */
283         { 0x24, S, "SET WINDOW" },
284         /* 25  M   M M   M     READ CAPACITY(10) */
285         { 0x25, D | W | O | B, "READ CAPACITY(10)" },
286         /* 25       O          READ CAPACITY */
287         { 0x25, R, "READ CAPACITY" },
288         /* 25             M    READ CARD CAPACITY */
289         { 0x25, K, "READ CARD CAPACITY" },
290         /* 25                  GET WINDOW */
291         { 0x25, S, "GET WINDOW" },
292         /* 26  V   VV */
293         /* 27  V   VV */
294         /* 28  M   MOM   MM    READ(10) */
295         { 0x28, D | W | R | O | B | K | S, "READ(10)" },
296         /* 28                  GET MESSAGE(10) */
297         { 0x28, C, "GET MESSAGE(10)" },
298         /* 29  V   VVO         READ GENERATION */
299         { 0x29, O, "READ GENERATION" },
300         /* 2A  O   MOM   MO    WRITE(10) */
301         { 0x2A, D | W | R | O | B | K, "WRITE(10)" },
302         /* 2A                  SEND(10) */
303         { 0x2A, S, "SEND(10)" },
304         /* 2A                  SEND MESSAGE(10) */
305         { 0x2A, C, "SEND MESSAGE(10)" },
306         /* 2B  Z   OOO    O    SEEK(10) */
307         { 0x2B, D | W | R | O | K, "SEEK(10)" },
308         /* 2B   O              LOCATE(10) */
309         { 0x2B, T, "LOCATE(10)" },
310         /* 2B         O        POSITION TO ELEMENT */
311         { 0x2B, M, "POSITION TO ELEMENT" },
312         /* 2C  V    OO         ERASE(10) */
313         { 0x2C, R | O, "ERASE(10)" },
314         /* 2D        O         READ UPDATED BLOCK */
315         { 0x2D, O, "READ UPDATED BLOCK" },
316         /* 2D  V */
317         /* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
318         { 0x2E, D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
319         /* 2F  O   OOO         VERIFY(10) */
320         { 0x2F, D | W | R | O, "VERIFY(10)" },
321         /* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
322         { 0x30, D | W | R | O, "SEARCH DATA HIGH(10)" },
323         /* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
324         { 0x31, D | W | R | O, "SEARCH DATA EQUAL(10)" },
325         /* 31                  OBJECT POSITION */
326         { 0x31, S, "OBJECT POSITION" },
327         /* 32  Z   ZZZ         SEARCH DATA LOW(10) */
328         { 0x32, D | W | R | O, "SEARCH DATA LOW(10)" },
329         /* 33  Z   OZO         SET LIMITS(10) */
330         { 0x33, D | W | R | O, "SET LIMITS(10)" },
331         /* 34  O   O O    O    PRE-FETCH(10) */
332         { 0x34, D | W | O | K, "PRE-FETCH(10)" },
333         /* 34   M              READ POSITION */
334         { 0x34, T, "READ POSITION" },
335         /* 34                  GET DATA BUFFER STATUS */
336         { 0x34, S, "GET DATA BUFFER STATUS" },
337         /* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
338         { 0x35, D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
339         /* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
340         { 0x36, D | W | O | K, "LOCK UNLOCK CACHE(10)" },
341         /* 37  O     O         READ DEFECT DATA(10) */
342         { 0x37, D | O, "READ DEFECT DATA(10)" },
343         /* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
344         { 0x37, M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
345         /* 38      O O    O    MEDIUM SCAN */
346         { 0x38, W | O | K, "MEDIUM SCAN" },
347         /* 39  ZZZZOZO    Z    COMPARE */
348         { 0x39, D | T | L | P | W | R | O | K | S, "COMPARE" },
349         /* 3A  ZZZZOZO    Z    COPY AND VERIFY */
350         { 0x3A, D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
351         /* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
352         { 0x3B, ALL, "WRITE BUFFER" },
353         /* 3C  OOOOOOOOOO OOO  READ BUFFER */
354         { 0x3C, ALL & ~(B), "READ BUFFER" },
355         /* 3D        O         UPDATE BLOCK */
356         { 0x3D, O, "UPDATE BLOCK" },
357         /* 3E  O   O O         READ LONG(10) */
358         { 0x3E, D | W | O, "READ LONG(10)" },
359         /* 3F  O   O O         WRITE LONG(10) */
360         { 0x3F, D | W | O, "WRITE LONG(10)" },
361         /* 40  ZZZZOZOZ        CHANGE DEFINITION */
362         { 0x40, D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
363         /* 41  O               WRITE SAME(10) */
364         { 0x41, D, "WRITE SAME(10)" },
365         /* 42       O          UNMAP */
366         { 0x42, D, "UNMAP" },
367         /* 42       O          READ SUB-CHANNEL */
368         { 0x42, R, "READ SUB-CHANNEL" },
369         /* 43       O          READ TOC/PMA/ATIP */
370         { 0x43, R, "READ TOC/PMA/ATIP" },
371         /* 44   M          M   REPORT DENSITY SUPPORT */
372         { 0x44, T | V, "REPORT DENSITY SUPPORT" },
373         /* 44                  READ HEADER */
374         /* 45       O          PLAY AUDIO(10) */
375         { 0x45, R, "PLAY AUDIO(10)" },
376         /* 46       M          GET CONFIGURATION */
377         { 0x46, R, "GET CONFIGURATION" },
378         /* 47       O          PLAY AUDIO MSF */
379         { 0x47, R, "PLAY AUDIO MSF" },
380         /* 48 */
381         /* 49 */
382         /* 4A       M          GET EVENT STATUS NOTIFICATION */
383         { 0x4A, R, "GET EVENT STATUS NOTIFICATION" },
384         /* 4B       O          PAUSE/RESUME */
385         { 0x4B, R, "PAUSE/RESUME" },
386         /* 4C  OOOOO OOOO OOO  LOG SELECT */
387         { 0x4C, ALL & ~(R | B), "LOG SELECT" },
388         /* 4D  OOOOO OOOO OMO  LOG SENSE */
389         { 0x4D, ALL & ~(R | B), "LOG SENSE" },
390         /* 4E       O          STOP PLAY/SCAN */
391         { 0x4E, R, "STOP PLAY/SCAN" },
392         /* 4F */
393         /* 50  O               XDWRITE(10) */
394         { 0x50, D, "XDWRITE(10)" },
395         /* 51  O               XPWRITE(10) */
396         { 0x51, D, "XPWRITE(10)" },
397         /* 51       O          READ DISC INFORMATION */
398         { 0x51, R, "READ DISC INFORMATION" },
399         /* 52  O               XDREAD(10) */
400         { 0x52, D, "XDREAD(10)" },
401         /* 52       O          READ TRACK INFORMATION */
402         { 0x52, R, "READ TRACK INFORMATION" },
403         /* 53       O          RESERVE TRACK */
404         { 0x53, R, "RESERVE TRACK" },
405         /* 54       O          SEND OPC INFORMATION */
406         { 0x54, R, "SEND OPC INFORMATION" },
407         /* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
408         { 0x55, ALL & ~(P), "MODE SELECT(10)" },
409         /* 56  ZZMZO OOOZ      RESERVE(10) */
410         { 0x56, ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
411         /* 56         Z        RESERVE ELEMENT(10) */
412         { 0x56, M, "RESERVE ELEMENT(10)" },
413         /* 57  ZZMZO OOOZ      RELEASE(10) */
414         { 0x57, ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
415         /* 57         Z        RELEASE ELEMENT(10) */
416         { 0x57, M, "RELEASE ELEMENT(10)" },
417         /* 58       O          REPAIR TRACK */
418         { 0x58, R, "REPAIR TRACK" },
419         /* 59 */
420         /* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
421         { 0x5A, ALL & ~(P), "MODE SENSE(10)" },
422         /* 5B       O          CLOSE TRACK/SESSION */
423         { 0x5B, R, "CLOSE TRACK/SESSION" },
424         /* 5C       O          READ BUFFER CAPACITY */
425         { 0x5C, R, "READ BUFFER CAPACITY" },
426         /* 5D       O          SEND CUE SHEET */
427         { 0x5D, R, "SEND CUE SHEET" },
428         /* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
429         { 0x5E, ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
430         /* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
431         { 0x5F, ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
432         /* 7E  OO   O OOOO O   extended CDB */
433         { 0x7E, D | T | R | M | A | E | B | V, "extended CDB" },
434         /* 7F  O            M  variable length CDB (more than 16 bytes) */
435         { 0x7F, D | F, "variable length CDB (more than 16 bytes)" },
436         /* 80  Z               XDWRITE EXTENDED(16) */
437         { 0x80, D, "XDWRITE EXTENDED(16)" },
438         /* 80   M              WRITE FILEMARKS(16) */
439         { 0x80, T, "WRITE FILEMARKS(16)" },
440         /* 81  Z               REBUILD(16) */
441         { 0x81, D, "REBUILD(16)" },
442         /* 81   O              READ REVERSE(16) */
443         { 0x81, T, "READ REVERSE(16)" },
444         /* 82  Z               REGENERATE(16) */
445         { 0x82, D, "REGENERATE(16)" },
446         /* 83  OOOOO O    OO   EXTENDED COPY */
447         { 0x83, D | T | L | P | W | O | K | V, "EXTENDED COPY" },
448         /* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
449         { 0x84, D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
450         /* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
451         { 0x85, D | R | B, "ATA COMMAND PASS THROUGH(16)" },
452         /* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
453         { 0x86, ALL & ~(L | R | F), "ACCESS CONTROL IN" },
454         /* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
455         { 0x87, ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
456         /*
457          * XXX READ(16)/WRITE(16) were not listed for CD/DVE in op-num.txt
458          * but we had it since r1.40.  Do we really want them?
459          */
460         /* 88  MM  O O   O     READ(16) */
461         { 0x88, D | T | W | O | B, "READ(16)" },
462         /* 89 */
463         /* 8A  OM  O O   O     WRITE(16) */
464         { 0x8A, D | T | W | O | B, "WRITE(16)" },
465         /* 8B  O               ORWRITE */
466         { 0x8B, D, "ORWRITE" },
467         /* 8C  OO  O OO  O M   READ ATTRIBUTE */
468         { 0x8C, D | T | W | O | M | B | V, "READ ATTRIBUTE" },
469         /* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
470         { 0x8D, D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
471         /* 8E  O   O O   O     WRITE AND VERIFY(16) */
472         { 0x8E, D | W | O | B, "WRITE AND VERIFY(16)" },
473         /* 8F  OO  O O   O     VERIFY(16) */
474         { 0x8F, D | T | W | O | B, "VERIFY(16)" },
475         /* 90  O   O O   O     PRE-FETCH(16) */
476         { 0x90, D | W | O | B, "PRE-FETCH(16)" },
477         /* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
478         { 0x91, D | W | O | B, "SYNCHRONIZE CACHE(16)" },
479         /* 91   O              SPACE(16) */
480         { 0x91, T, "SPACE(16)" },
481         /* 92  Z   O O         LOCK UNLOCK CACHE(16) */
482         { 0x92, D | W | O, "LOCK UNLOCK CACHE(16)" },
483         /* 92   O              LOCATE(16) */
484         { 0x92, T, "LOCATE(16)" },
485         /* 93  O               WRITE SAME(16) */
486         { 0x93, D, "WRITE SAME(16)" },
487         /* 93   M              ERASE(16) */
488         { 0x93, T, "ERASE(16)" },
489         /* 94 [usage proposed by SCSI Socket Services project] */
490         /* 95 [usage proposed by SCSI Socket Services project] */
491         /* 96 [usage proposed by SCSI Socket Services project] */
492         /* 97 [usage proposed by SCSI Socket Services project] */
493         /* 98 */
494         /* 99 */
495         /* 9A */
496         /* 9B */
497         /* 9C */
498         /* 9D */
499         /* XXX KDM ALL for this?  op-num.txt defines it for none.. */
500         /* 9E                  SERVICE ACTION IN(16) */
501         { 0x9E, ALL, "SERVICE ACTION IN(16)" },
502         /* XXX KDM ALL for this?  op-num.txt defines it for ADC.. */
503         /* 9F              M   SERVICE ACTION OUT(16) */
504         { 0x9F, ALL, "SERVICE ACTION OUT(16)" },
505         /* A0  MMOOO OMMM OMO  REPORT LUNS */
506         { 0xA0, ALL & ~(R | B), "REPORT LUNS" },
507         /* A1       O          BLANK */
508         { 0xA1, R, "BLANK" },
509         /* A1  O         O     ATA COMMAND PASS THROUGH(12) */
510         { 0xA1, D | B, "ATA COMMAND PASS THROUGH(12)" },
511         /* A2  OO   O      O   SECURITY PROTOCOL IN */
512         { 0xA2, D | T | R | V, "SECURITY PROTOCOL IN" },
513         /* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
514         { 0xA3, ALL & ~(P | R | F), "MAINTENANCE (IN)" },
515         /* A3       O          SEND KEY */
516         { 0xA3, R, "SEND KEY" },
517         /* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
518         { 0xA4, ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
519         /* A4       O          REPORT KEY */
520         { 0xA4, R, "REPORT KEY" },
521         /* A5   O  O OM        MOVE MEDIUM */
522         { 0xA5, T | W | O | M, "MOVE MEDIUM" },
523         /* A5       O          PLAY AUDIO(12) */
524         { 0xA5, R, "PLAY AUDIO(12)" },
525         /* A6         O        EXCHANGE MEDIUM */
526         { 0xA6, M, "EXCHANGE MEDIUM" },
527         /* A6       O          LOAD/UNLOAD C/DVD */
528         { 0xA6, R, "LOAD/UNLOAD C/DVD" },
529         /* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
530         { 0xA7, D | T | W | O, "MOVE MEDIUM ATTACHED" },
531         /* A7       O          SET READ AHEAD */
532         { 0xA7, R, "SET READ AHEAD" },
533         /* A8  O   OOO         READ(12) */
534         { 0xA8, D | W | R | O, "READ(12)" },
535         /* A8                  GET MESSAGE(12) */
536         { 0xA8, C, "GET MESSAGE(12)" },
537         /* A9              O   SERVICE ACTION OUT(12) */
538         { 0xA9, V, "SERVICE ACTION OUT(12)" },
539         /* AA  O   OOO         WRITE(12) */
540         { 0xAA, D | W | R | O, "WRITE(12)" },
541         /* AA                  SEND MESSAGE(12) */
542         { 0xAA, C, "SEND MESSAGE(12)" },
543         /* AB       O      O   SERVICE ACTION IN(12) */
544         { 0xAB, R | V, "SERVICE ACTION IN(12)" },
545         /* AC        O         ERASE(12) */
546         { 0xAC, O, "ERASE(12)" },
547         /* AC       O          GET PERFORMANCE */
548         { 0xAC, R, "GET PERFORMANCE" },
549         /* AD       O          READ DVD STRUCTURE */
550         { 0xAD, R, "READ DVD STRUCTURE" },
551         /* AE  O   O O         WRITE AND VERIFY(12) */
552         { 0xAE, D | W | O, "WRITE AND VERIFY(12)" },
553         /* AF  O   OZO         VERIFY(12) */
554         { 0xAF, D | W | R | O, "VERIFY(12)" },
555         /* B0      ZZZ         SEARCH DATA HIGH(12) */
556         { 0xB0, W | R | O, "SEARCH DATA HIGH(12)" },
557         /* B1      ZZZ         SEARCH DATA EQUAL(12) */
558         { 0xB1, W | R | O, "SEARCH DATA EQUAL(12)" },
559         /* B2      ZZZ         SEARCH DATA LOW(12) */
560         { 0xB2, W | R | O, "SEARCH DATA LOW(12)" },
561         /* B3  Z   OZO         SET LIMITS(12) */
562         { 0xB3, D | W | R | O, "SET LIMITS(12)" },
563         /* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
564         { 0xB4, D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
565         /* B5  OO   O      O   SECURITY PROTOCOL OUT */
566         { 0xB5, D | T | R | V, "SECURITY PROTOCOL OUT" },
567         /* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
568         { 0xB5, M, "REQUEST VOLUME ELEMENT ADDRESS" },
569         /* B6         O        SEND VOLUME TAG */
570         { 0xB6, M, "SEND VOLUME TAG" },
571         /* B6       O          SET STREAMING */
572         { 0xB6, R, "SET STREAMING" },
573         /* B7  O     O         READ DEFECT DATA(12) */
574         { 0xB7, D | O, "READ DEFECT DATA(12)" },
575         /* B8   O  OZOM        READ ELEMENT STATUS */
576         { 0xB8, T | W | R | O | M, "READ ELEMENT STATUS" },
577         /* B9       O          READ CD MSF */
578         { 0xB9, R, "READ CD MSF" },
579         /* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
580         { 0xBA, D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
581         /* BA       O          SCAN */
582         { 0xBA, R, "SCAN" },
583         /* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
584         { 0xBB, D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
585         /* BB       O          SET CD SPEED */
586         { 0xBB, R, "SET CD SPEED" },
587         /* BC  O   O OOMO      SPARE (IN) */
588         { 0xBC, D | W | O | M | A | E, "SPARE (IN)" },
589         /* BD  O   O OOOO      SPARE (OUT) */
590         { 0xBD, D | W | O | M | A | E, "SPARE (OUT)" },
591         /* BD       O          MECHANISM STATUS */
592         { 0xBD, R, "MECHANISM STATUS" },
593         /* BE  O   O OOMO      VOLUME SET (IN) */
594         { 0xBE, D | W | O | M | A | E, "VOLUME SET (IN)" },
595         /* BE       O          READ CD */
596         { 0xBE, R, "READ CD" },
597         /* BF  O   O OOOO      VOLUME SET (OUT) */
598         { 0xBF, D | W | O | M | A | E, "VOLUME SET (OUT)" },
599         /* BF       O          SEND DVD STRUCTURE */
600         { 0xBF, R, "SEND DVD STRUCTURE" }
601 };
602
603 const char *
604 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
605 {
606         caddr_t match;
607         int i, j;
608         u_int32_t opmask;
609         u_int16_t pd_type;
610         int       num_ops[2];
611         struct op_table_entry *table[2];
612         int num_tables;
613
614         pd_type = SID_TYPE(inq_data);
615
616         match = cam_quirkmatch((caddr_t)inq_data,
617                                (caddr_t)scsi_op_quirk_table,
618                                sizeof(scsi_op_quirk_table)/
619                                sizeof(*scsi_op_quirk_table),
620                                sizeof(*scsi_op_quirk_table),
621                                scsi_inquiry_match);
622
623         if (match != NULL) {
624                 table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
625                 num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
626                 table[1] = scsi_op_codes;
627                 num_ops[1] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
628                 num_tables = 2;
629         } else {
630                 /*      
631                  * If this is true, we have a vendor specific opcode that
632                  * wasn't covered in the quirk table.
633                  */
634                 if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
635                         return("Vendor Specific Command");
636
637                 table[0] = scsi_op_codes;
638                 num_ops[0] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
639                 num_tables = 1;
640         }
641
642         /* RBC is 'Simplified' Direct Access Device */
643         if (pd_type == T_RBC)
644                 pd_type = T_DIRECT;
645
646         opmask = 1 << pd_type;
647
648         for (j = 0; j < num_tables; j++) {
649                 for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
650                         if ((table[j][i].opcode == opcode) 
651                          && ((table[j][i].opmask & opmask) != 0))
652                                 return(table[j][i].desc);
653                 }
654         }
655         
656         /*
657          * If we can't find a match for the command in the table, we just
658          * assume it's a vendor specifc command.
659          */
660         return("Vendor Specific Command");
661
662 }
663
664 #else /* SCSI_NO_OP_STRINGS */
665
666 const char *
667 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
668 {
669         return("");
670 }
671
672 #endif
673
674
675 #if !defined(SCSI_NO_SENSE_STRINGS)
676 #define SST(asc, ascq, action, desc) \
677         asc, ascq, action, desc
678 #else 
679 const char empty_string[] = "";
680
681 #define SST(asc, ascq, action, desc) \
682         asc, ascq, action, empty_string
683 #endif 
684
685 const struct sense_key_table_entry sense_key_table[] = 
686 {
687         { SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
688         { SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
689         { SSD_KEY_NOT_READY, SS_RDEF, "NOT READY" },
690         { SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
691         { SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
692         { SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
693         { SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
694         { SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
695         { SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
696         { SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
697         { SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
698         { SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
699         { SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
700         { SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
701         { SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
702         { SSD_KEY_RESERVED, SS_FATAL|EIO, "RESERVED" }
703 };
704
705 const int sense_key_table_size =
706     sizeof(sense_key_table)/sizeof(sense_key_table[0]);
707
708 static struct asc_table_entry quantum_fireball_entries[] = {
709         { SST(0x04, 0x0b, SS_START | SSQ_DECREMENT_COUNT | ENXIO, 
710              "Logical unit not ready, initializing cmd. required") }
711 };
712
713 static struct asc_table_entry sony_mo_entries[] = {
714         { SST(0x04, 0x00, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
715              "Logical unit not ready, cause not reportable") }
716 };
717
718 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
719         {
720                 /*
721                  * XXX The Quantum Fireball ST and SE like to return 0x04 0x0b
722                  * when they really should return 0x04 0x02.
723                  */
724                 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
725                 /*num_sense_keys*/0,
726                 sizeof(quantum_fireball_entries)/sizeof(struct asc_table_entry),
727                 /*sense key entries*/NULL,
728                 quantum_fireball_entries
729         },
730         {
731                 /*
732                  * This Sony MO drive likes to return 0x04, 0x00 when it
733                  * isn't spun up.
734                  */
735                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
736                 /*num_sense_keys*/0,
737                 sizeof(sony_mo_entries)/sizeof(struct asc_table_entry),
738                 /*sense key entries*/NULL,
739                 sony_mo_entries
740         }
741 };
742
743 const int sense_quirk_table_size =
744     sizeof(sense_quirk_table)/sizeof(sense_quirk_table[0]);
745
746 static struct asc_table_entry asc_table[] = {
747         /*
748          * From: http://www.t10.org/lists/asc-num.txt
749          * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
750          */
751         /*
752          * File: ASC-NUM.TXT
753          *
754          * SCSI ASC/ASCQ Assignments
755          * Numeric Sorted Listing
756          * as of  5/20/12
757          *
758          * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
759          * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
760          * . L - PRINTER DEVICE (SSC)                           blank = reserved
761          * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
762          * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
763          * .  . R - CD DEVICE (MMC)
764          * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
765          * .  .  .M - MEDIA CHANGER DEVICE (SMC)
766          * .  .  . A - STORAGE ARRAY DEVICE (SCC)
767          * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
768          * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
769          * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
770          * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
771          * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
772          * DTLPWROMAEBKVF
773          * ASC      ASCQ  Action
774          * Description
775          */
776         /* DTLPWROMAEBKVF */
777         { SST(0x00, 0x00, SS_NOP,
778             "No additional sense information") },
779         /*  T             */
780         { SST(0x00, 0x01, SS_RDEF,
781             "Filemark detected") },
782         /*  T             */
783         { SST(0x00, 0x02, SS_RDEF,
784             "End-of-partition/medium detected") },
785         /*  T             */
786         { SST(0x00, 0x03, SS_RDEF,
787             "Setmark detected") },
788         /*  T             */
789         { SST(0x00, 0x04, SS_RDEF,
790             "Beginning-of-partition/medium detected") },
791         /*  TL            */
792         { SST(0x00, 0x05, SS_RDEF,
793             "End-of-data detected") },
794         /* DTLPWROMAEBKVF */
795         { SST(0x00, 0x06, SS_RDEF,
796             "I/O process terminated") },
797         /*  T             */
798         { SST(0x00, 0x07, SS_RDEF,      /* XXX TBD */
799             "Programmable early warning detected") },
800         /*      R         */
801         { SST(0x00, 0x11, SS_FATAL | EBUSY,
802             "Audio play operation in progress") },
803         /*      R         */
804         { SST(0x00, 0x12, SS_NOP,
805             "Audio play operation paused") },
806         /*      R         */
807         { SST(0x00, 0x13, SS_NOP,
808             "Audio play operation successfully completed") },
809         /*      R         */
810         { SST(0x00, 0x14, SS_RDEF,
811             "Audio play operation stopped due to error") },
812         /*      R         */
813         { SST(0x00, 0x15, SS_NOP,
814             "No current audio status to return") },
815         /* DTLPWROMAEBKVF */
816         { SST(0x00, 0x16, SS_FATAL | EBUSY,
817             "Operation in progress") },
818         /* DTL WROMAEBKVF */
819         { SST(0x00, 0x17, SS_RDEF,
820             "Cleaning requested") },
821         /*  T             */
822         { SST(0x00, 0x18, SS_RDEF,      /* XXX TBD */
823             "Erase operation in progress") },
824         /*  T             */
825         { SST(0x00, 0x19, SS_RDEF,      /* XXX TBD */
826             "Locate operation in progress") },
827         /*  T             */
828         { SST(0x00, 0x1A, SS_RDEF,      /* XXX TBD */
829             "Rewind operation in progress") },
830         /*  T             */
831         { SST(0x00, 0x1B, SS_RDEF,      /* XXX TBD */
832             "Set capacity operation in progress") },
833         /*  T             */
834         { SST(0x00, 0x1C, SS_RDEF,      /* XXX TBD */
835             "Verify operation in progress") },
836         /* DT        B    */
837         { SST(0x00, 0x1D, SS_RDEF,      /* XXX TBD */
838             "ATA pass through information available") },
839         /* DT   R MAEBKV  */
840         { SST(0x00, 0x1E, SS_RDEF,      /* XXX TBD */
841             "Conflicting SA creation request") },
842         /* DT        B    */
843         { SST(0x00, 0x1F, SS_RDEF,      /* XXX TBD */
844             "Logical unit transitioning to another power condition") },
845         /* DT P      B    */
846         { SST(0x00, 0x20, SS_RDEF,      /* XXX TBD */
847             "Extended copy information available") },
848         /* D   W O   BK   */
849         { SST(0x01, 0x00, SS_RDEF,
850             "No index/sector signal") },
851         /* D   WRO   BK   */
852         { SST(0x02, 0x00, SS_RDEF,
853             "No seek complete") },
854         /* DTL W O   BK   */
855         { SST(0x03, 0x00, SS_RDEF,
856             "Peripheral device write fault") },
857         /*  T             */
858         { SST(0x03, 0x01, SS_RDEF,
859             "No write current") },
860         /*  T             */
861         { SST(0x03, 0x02, SS_RDEF,
862             "Excessive write errors") },
863         /* DTLPWROMAEBKVF */
864         { SST(0x04, 0x00, SS_RDEF,
865             "Logical unit not ready, cause not reportable") },
866         /* DTLPWROMAEBKVF */
867         { SST(0x04, 0x01, SS_TUR | SSQ_MANY | SSQ_DECREMENT_COUNT | EBUSY,
868             "Logical unit is in process of becoming ready") },
869         /* DTLPWROMAEBKVF */
870         { SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
871             "Logical unit not ready, initializing command required") },
872         /* DTLPWROMAEBKVF */
873         { SST(0x04, 0x03, SS_FATAL | ENXIO,
874             "Logical unit not ready, manual intervention required") },
875         /* DTL  RO   B    */
876         { SST(0x04, 0x04, SS_FATAL | EBUSY,
877             "Logical unit not ready, format in progress") },
878         /* DT  W O A BK F */
879         { SST(0x04, 0x05, SS_FATAL | EBUSY,
880             "Logical unit not ready, rebuild in progress") },
881         /* DT  W O A BK   */
882         { SST(0x04, 0x06, SS_FATAL | EBUSY,
883             "Logical unit not ready, recalculation in progress") },
884         /* DTLPWROMAEBKVF */
885         { SST(0x04, 0x07, SS_FATAL | EBUSY,
886             "Logical unit not ready, operation in progress") },
887         /*      R         */
888         { SST(0x04, 0x08, SS_FATAL | EBUSY,
889             "Logical unit not ready, long write in progress") },
890         /* DTLPWROMAEBKVF */
891         { SST(0x04, 0x09, SS_RDEF,      /* XXX TBD */
892             "Logical unit not ready, self-test in progress") },
893         /* DTLPWROMAEBKVF */
894         { SST(0x04, 0x0A, SS_RDEF,      /* XXX TBD */
895             "Logical unit not accessible, asymmetric access state transition")},
896         /* DTLPWROMAEBKVF */
897         { SST(0x04, 0x0B, SS_RDEF,      /* XXX TBD */
898             "Logical unit not accessible, target port in standby state") },
899         /* DTLPWROMAEBKVF */
900         { SST(0x04, 0x0C, SS_RDEF,      /* XXX TBD */
901             "Logical unit not accessible, target port in unavailable state") },
902         /*              F */
903         { SST(0x04, 0x0D, SS_RDEF,      /* XXX TBD */
904             "Logical unit not ready, structure check required") },
905         /* DT  WROM  B    */
906         { SST(0x04, 0x10, SS_RDEF,      /* XXX TBD */
907             "Logical unit not ready, auxiliary memory not accessible") },
908         /* DT  WRO AEB VF */
909         { SST(0x04, 0x11, SS_RDEF,      /* XXX TBD */
910             "Logical unit not ready, notify (enable spinup) required") },
911         /*        M    V  */
912         { SST(0x04, 0x12, SS_RDEF,      /* XXX TBD */
913             "Logical unit not ready, offline") },
914         /* DT   R MAEBKV  */
915         { SST(0x04, 0x13, SS_RDEF,      /* XXX TBD */
916             "Logical unit not ready, SA creation in progress") },
917         /* D         B    */
918         { SST(0x04, 0x14, SS_RDEF,      /* XXX TBD */
919             "Logical unit not ready, space allocation in progress") },
920         /*        M       */
921         { SST(0x04, 0x15, SS_RDEF,      /* XXX TBD */
922             "Logical unit not ready, robotics disabled") },
923         /*        M       */
924         { SST(0x04, 0x16, SS_RDEF,      /* XXX TBD */
925             "Logical unit not ready, configuration required") },
926         /*        M       */
927         { SST(0x04, 0x17, SS_RDEF,      /* XXX TBD */
928             "Logical unit not ready, calibration required") },
929         /*        M       */
930         { SST(0x04, 0x18, SS_RDEF,      /* XXX TBD */
931             "Logical unit not ready, a door is open") },
932         /*        M       */
933         { SST(0x04, 0x19, SS_RDEF,      /* XXX TBD */
934             "Logical unit not ready, operating in sequential mode") },
935         /* DT        B    */
936         { SST(0x04, 0x1A, SS_RDEF,      /* XXX TBD */
937             "Logical unit not ready, START/STOP UNIT command in progress") },
938         /* D         B    */
939         { SST(0x04, 0x1B, SS_RDEF,      /* XXX TBD */
940             "Logical unit not ready, sanitize in progress") },
941         /* DT     MAEB    */
942         { SST(0x04, 0x1C, SS_RDEF,      /* XXX TBD */
943             "Logical unit not ready, additional power use not yet granted") },
944         /* DTL WROMAEBKVF */
945         { SST(0x05, 0x00, SS_RDEF,
946             "Logical unit does not respond to selection") },
947         /* D   WROM  BK   */
948         { SST(0x06, 0x00, SS_RDEF,
949             "No reference position found") },
950         /* DTL WROM  BK   */
951         { SST(0x07, 0x00, SS_RDEF,
952             "Multiple peripheral devices selected") },
953         /* DTL WROMAEBKVF */
954         { SST(0x08, 0x00, SS_RDEF,
955             "Logical unit communication failure") },
956         /* DTL WROMAEBKVF */
957         { SST(0x08, 0x01, SS_RDEF,
958             "Logical unit communication time-out") },
959         /* DTL WROMAEBKVF */
960         { SST(0x08, 0x02, SS_RDEF,
961             "Logical unit communication parity error") },
962         /* DT   ROM  BK   */
963         { SST(0x08, 0x03, SS_RDEF,
964             "Logical unit communication CRC error (Ultra-DMA/32)") },
965         /* DTLPWRO    K   */
966         { SST(0x08, 0x04, SS_RDEF,      /* XXX TBD */
967             "Unreachable copy target") },
968         /* DT  WRO   B    */
969         { SST(0x09, 0x00, SS_RDEF,
970             "Track following error") },
971         /*     WRO    K   */
972         { SST(0x09, 0x01, SS_RDEF,
973             "Tracking servo failure") },
974         /*     WRO    K   */
975         { SST(0x09, 0x02, SS_RDEF,
976             "Focus servo failure") },
977         /*     WRO        */
978         { SST(0x09, 0x03, SS_RDEF,
979             "Spindle servo failure") },
980         /* DT  WRO   B    */
981         { SST(0x09, 0x04, SS_RDEF,
982             "Head select fault") },
983         /* DTLPWROMAEBKVF */
984         { SST(0x0A, 0x00, SS_FATAL | ENOSPC,
985             "Error log overflow") },
986         /* DTLPWROMAEBKVF */
987         { SST(0x0B, 0x00, SS_RDEF,
988             "Warning") },
989         /* DTLPWROMAEBKVF */
990         { SST(0x0B, 0x01, SS_RDEF,
991             "Warning - specified temperature exceeded") },
992         /* DTLPWROMAEBKVF */
993         { SST(0x0B, 0x02, SS_RDEF,
994             "Warning - enclosure degraded") },
995         /* DTLPWROMAEBKVF */
996         { SST(0x0B, 0x03, SS_RDEF,      /* XXX TBD */
997             "Warning - background self-test failed") },
998         /* DTLPWRO AEBKVF */
999         { SST(0x0B, 0x04, SS_RDEF,      /* XXX TBD */
1000             "Warning - background pre-scan detected medium error") },
1001         /* DTLPWRO AEBKVF */
1002         { SST(0x0B, 0x05, SS_RDEF,      /* XXX TBD */
1003             "Warning - background medium scan detected medium error") },
1004         /* DTLPWROMAEBKVF */
1005         { SST(0x0B, 0x06, SS_RDEF,      /* XXX TBD */
1006             "Warning - non-volatile cache now volatile") },
1007         /* DTLPWROMAEBKVF */
1008         { SST(0x0B, 0x07, SS_RDEF,      /* XXX TBD */
1009             "Warning - degraded power to non-volatile cache") },
1010         /* DTLPWROMAEBKVF */
1011         { SST(0x0B, 0x08, SS_RDEF,      /* XXX TBD */
1012             "Warning - power loss expected") },
1013         /* D              */
1014         { SST(0x0B, 0x09, SS_RDEF,      /* XXX TBD */
1015             "Warning - device statistics notification available") },
1016         /*  T   R         */
1017         { SST(0x0C, 0x00, SS_RDEF,
1018             "Write error") },
1019         /*            K   */
1020         { SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1021             "Write error - recovered with auto reallocation") },
1022         /* D   W O   BK   */
1023         { SST(0x0C, 0x02, SS_RDEF,
1024             "Write error - auto reallocation failed") },
1025         /* D   W O   BK   */
1026         { SST(0x0C, 0x03, SS_RDEF,
1027             "Write error - recommend reassignment") },
1028         /* DT  W O   B    */
1029         { SST(0x0C, 0x04, SS_RDEF,
1030             "Compression check miscompare error") },
1031         /* DT  W O   B    */
1032         { SST(0x0C, 0x05, SS_RDEF,
1033             "Data expansion occurred during compression") },
1034         /* DT  W O   B    */
1035         { SST(0x0C, 0x06, SS_RDEF,
1036             "Block not compressible") },
1037         /*      R         */
1038         { SST(0x0C, 0x07, SS_RDEF,
1039             "Write error - recovery needed") },
1040         /*      R         */
1041         { SST(0x0C, 0x08, SS_RDEF,
1042             "Write error - recovery failed") },
1043         /*      R         */
1044         { SST(0x0C, 0x09, SS_RDEF,
1045             "Write error - loss of streaming") },
1046         /*      R         */
1047         { SST(0x0C, 0x0A, SS_RDEF,
1048             "Write error - padding blocks added") },
1049         /* DT  WROM  B    */
1050         { SST(0x0C, 0x0B, SS_RDEF,      /* XXX TBD */
1051             "Auxiliary memory write error") },
1052         /* DTLPWRO AEBKVF */
1053         { SST(0x0C, 0x0C, SS_RDEF,      /* XXX TBD */
1054             "Write error - unexpected unsolicited data") },
1055         /* DTLPWRO AEBKVF */
1056         { SST(0x0C, 0x0D, SS_RDEF,      /* XXX TBD */
1057             "Write error - not enough unsolicited data") },
1058         /* DT  W O   BK   */
1059         { SST(0x0C, 0x0E, SS_RDEF,      /* XXX TBD */
1060             "Multiple write errors") },
1061         /*      R         */
1062         { SST(0x0C, 0x0F, SS_RDEF,      /* XXX TBD */
1063             "Defects in error window") },
1064         /* DTLPWRO A  K   */
1065         { SST(0x0D, 0x00, SS_RDEF,      /* XXX TBD */
1066             "Error detected by third party temporary initiator") },
1067         /* DTLPWRO A  K   */
1068         { SST(0x0D, 0x01, SS_RDEF,      /* XXX TBD */
1069             "Third party device failure") },
1070         /* DTLPWRO A  K   */
1071         { SST(0x0D, 0x02, SS_RDEF,      /* XXX TBD */
1072             "Copy target device not reachable") },
1073         /* DTLPWRO A  K   */
1074         { SST(0x0D, 0x03, SS_RDEF,      /* XXX TBD */
1075             "Incorrect copy target device type") },
1076         /* DTLPWRO A  K   */
1077         { SST(0x0D, 0x04, SS_RDEF,      /* XXX TBD */
1078             "Copy target device data underrun") },
1079         /* DTLPWRO A  K   */
1080         { SST(0x0D, 0x05, SS_RDEF,      /* XXX TBD */
1081             "Copy target device data overrun") },
1082         /* DT PWROMAEBK F */
1083         { SST(0x0E, 0x00, SS_RDEF,      /* XXX TBD */
1084             "Invalid information unit") },
1085         /* DT PWROMAEBK F */
1086         { SST(0x0E, 0x01, SS_RDEF,      /* XXX TBD */
1087             "Information unit too short") },
1088         /* DT PWROMAEBK F */
1089         { SST(0x0E, 0x02, SS_RDEF,      /* XXX TBD */
1090             "Information unit too long") },
1091         /* DT P R MAEBK F */
1092         { SST(0x0E, 0x03, SS_RDEF,      /* XXX TBD */
1093             "Invalid field in command information unit") },
1094         /* D   W O   BK   */
1095         { SST(0x10, 0x00, SS_RDEF,
1096             "ID CRC or ECC error") },
1097         /* DT  W O        */
1098         { SST(0x10, 0x01, SS_RDEF,      /* XXX TBD */
1099             "Logical block guard check failed") },
1100         /* DT  W O        */
1101         { SST(0x10, 0x02, SS_RDEF,      /* XXX TBD */
1102             "Logical block application tag check failed") },
1103         /* DT  W O        */
1104         { SST(0x10, 0x03, SS_RDEF,      /* XXX TBD */
1105             "Logical block reference tag check failed") },
1106         /*  T             */
1107         { SST(0x10, 0x04, SS_RDEF,      /* XXX TBD */
1108             "Logical block protection error on recovered buffer data") },
1109         /*  T             */
1110         { SST(0x10, 0x05, SS_RDEF,      /* XXX TBD */
1111             "Logical block protection method error") },
1112         /* DT  WRO   BK   */
1113         { SST(0x11, 0x00, SS_RDEF,
1114             "Unrecovered read error") },
1115         /* DT  WRO   BK   */
1116         { SST(0x11, 0x01, SS_RDEF,
1117             "Read retries exhausted") },
1118         /* DT  WRO   BK   */
1119         { SST(0x11, 0x02, SS_RDEF,
1120             "Error too long to correct") },
1121         /* DT  W O   BK   */
1122         { SST(0x11, 0x03, SS_RDEF,
1123             "Multiple read errors") },
1124         /* D   W O   BK   */
1125         { SST(0x11, 0x04, SS_RDEF,
1126             "Unrecovered read error - auto reallocate failed") },
1127         /*     WRO   B    */
1128         { SST(0x11, 0x05, SS_RDEF,
1129             "L-EC uncorrectable error") },
1130         /*     WRO   B    */
1131         { SST(0x11, 0x06, SS_RDEF,
1132             "CIRC unrecovered error") },
1133         /*     W O   B    */
1134         { SST(0x11, 0x07, SS_RDEF,
1135             "Data re-synchronization error") },
1136         /*  T             */
1137         { SST(0x11, 0x08, SS_RDEF,
1138             "Incomplete block read") },
1139         /*  T             */
1140         { SST(0x11, 0x09, SS_RDEF,
1141             "No gap found") },
1142         /* DT    O   BK   */
1143         { SST(0x11, 0x0A, SS_RDEF,
1144             "Miscorrected error") },
1145         /* D   W O   BK   */
1146         { SST(0x11, 0x0B, SS_RDEF,
1147             "Unrecovered read error - recommend reassignment") },
1148         /* D   W O   BK   */
1149         { SST(0x11, 0x0C, SS_RDEF,
1150             "Unrecovered read error - recommend rewrite the data") },
1151         /* DT  WRO   B    */
1152         { SST(0x11, 0x0D, SS_RDEF,
1153             "De-compression CRC error") },
1154         /* DT  WRO   B    */
1155         { SST(0x11, 0x0E, SS_RDEF,
1156             "Cannot decompress using declared algorithm") },
1157         /*      R         */
1158         { SST(0x11, 0x0F, SS_RDEF,
1159             "Error reading UPC/EAN number") },
1160         /*      R         */
1161         { SST(0x11, 0x10, SS_RDEF,
1162             "Error reading ISRC number") },
1163         /*      R         */
1164         { SST(0x11, 0x11, SS_RDEF,
1165             "Read error - loss of streaming") },
1166         /* DT  WROM  B    */
1167         { SST(0x11, 0x12, SS_RDEF,      /* XXX TBD */
1168             "Auxiliary memory read error") },
1169         /* DTLPWRO AEBKVF */
1170         { SST(0x11, 0x13, SS_RDEF,      /* XXX TBD */
1171             "Read error - failed retransmission request") },
1172         /* D              */
1173         { SST(0x11, 0x14, SS_RDEF,      /* XXX TBD */
1174             "Read error - LBA marked bad by application client") },
1175         /* D   W O   BK   */
1176         { SST(0x12, 0x00, SS_RDEF,
1177             "Address mark not found for ID field") },
1178         /* D   W O   BK   */
1179         { SST(0x13, 0x00, SS_RDEF,
1180             "Address mark not found for data field") },
1181         /* DTL WRO   BK   */
1182         { SST(0x14, 0x00, SS_RDEF,
1183             "Recorded entity not found") },
1184         /* DT  WRO   BK   */
1185         { SST(0x14, 0x01, SS_RDEF,
1186             "Record not found") },
1187         /*  T             */
1188         { SST(0x14, 0x02, SS_RDEF,
1189             "Filemark or setmark not found") },
1190         /*  T             */
1191         { SST(0x14, 0x03, SS_RDEF,
1192             "End-of-data not found") },
1193         /*  T             */
1194         { SST(0x14, 0x04, SS_RDEF,
1195             "Block sequence error") },
1196         /* DT  W O   BK   */
1197         { SST(0x14, 0x05, SS_RDEF,
1198             "Record not found - recommend reassignment") },
1199         /* DT  W O   BK   */
1200         { SST(0x14, 0x06, SS_RDEF,
1201             "Record not found - data auto-reallocated") },
1202         /*  T             */
1203         { SST(0x14, 0x07, SS_RDEF,      /* XXX TBD */
1204             "Locate operation failure") },
1205         /* DTL WROM  BK   */
1206         { SST(0x15, 0x00, SS_RDEF,
1207             "Random positioning error") },
1208         /* DTL WROM  BK   */
1209         { SST(0x15, 0x01, SS_RDEF,
1210             "Mechanical positioning error") },
1211         /* DT  WRO   BK   */
1212         { SST(0x15, 0x02, SS_RDEF,
1213             "Positioning error detected by read of medium") },
1214         /* D   W O   BK   */
1215         { SST(0x16, 0x00, SS_RDEF,
1216             "Data synchronization mark error") },
1217         /* D   W O   BK   */
1218         { SST(0x16, 0x01, SS_RDEF,
1219             "Data sync error - data rewritten") },
1220         /* D   W O   BK   */
1221         { SST(0x16, 0x02, SS_RDEF,
1222             "Data sync error - recommend rewrite") },
1223         /* D   W O   BK   */
1224         { SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1225             "Data sync error - data auto-reallocated") },
1226         /* D   W O   BK   */
1227         { SST(0x16, 0x04, SS_RDEF,
1228             "Data sync error - recommend reassignment") },
1229         /* DT  WRO   BK   */
1230         { SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1231             "Recovered data with no error correction applied") },
1232         /* DT  WRO   BK   */
1233         { SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1234             "Recovered data with retries") },
1235         /* DT  WRO   BK   */
1236         { SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1237             "Recovered data with positive head offset") },
1238         /* DT  WRO   BK   */
1239         { SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1240             "Recovered data with negative head offset") },
1241         /*     WRO   B    */
1242         { SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1243             "Recovered data with retries and/or CIRC applied") },
1244         /* D   WRO   BK   */
1245         { SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1246             "Recovered data using previous sector ID") },
1247         /* D   W O   BK   */
1248         { SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1249             "Recovered data without ECC - data auto-reallocated") },
1250         /* D   WRO   BK   */
1251         { SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1252             "Recovered data without ECC - recommend reassignment") },
1253         /* D   WRO   BK   */
1254         { SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1255             "Recovered data without ECC - recommend rewrite") },
1256         /* D   WRO   BK   */
1257         { SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1258             "Recovered data without ECC - data rewritten") },
1259         /* DT  WRO   BK   */
1260         { SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1261             "Recovered data with error correction applied") },
1262         /* D   WRO   BK   */
1263         { SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1264             "Recovered data with error corr. & retries applied") },
1265         /* D   WRO   BK   */
1266         { SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1267             "Recovered data - data auto-reallocated") },
1268         /*      R         */
1269         { SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1270             "Recovered data with CIRC") },
1271         /*      R         */
1272         { SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1273             "Recovered data with L-EC") },
1274         /* D   WRO   BK   */
1275         { SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1276             "Recovered data - recommend reassignment") },
1277         /* D   WRO   BK   */
1278         { SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1279             "Recovered data - recommend rewrite") },
1280         /* D   W O   BK   */
1281         { SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1282             "Recovered data with ECC - data rewritten") },
1283         /*      R         */
1284         { SST(0x18, 0x08, SS_RDEF,      /* XXX TBD */
1285             "Recovered data with linking") },
1286         /* D     O    K   */
1287         { SST(0x19, 0x00, SS_RDEF,
1288             "Defect list error") },
1289         /* D     O    K   */
1290         { SST(0x19, 0x01, SS_RDEF,
1291             "Defect list not available") },
1292         /* D     O    K   */
1293         { SST(0x19, 0x02, SS_RDEF,
1294             "Defect list error in primary list") },
1295         /* D     O    K   */
1296         { SST(0x19, 0x03, SS_RDEF,
1297             "Defect list error in grown list") },
1298         /* DTLPWROMAEBKVF */
1299         { SST(0x1A, 0x00, SS_RDEF,
1300             "Parameter list length error") },
1301         /* DTLPWROMAEBKVF */
1302         { SST(0x1B, 0x00, SS_RDEF,
1303             "Synchronous data transfer error") },
1304         /* D     O   BK   */
1305         { SST(0x1C, 0x00, SS_RDEF,
1306             "Defect list not found") },
1307         /* D     O   BK   */
1308         { SST(0x1C, 0x01, SS_RDEF,
1309             "Primary defect list not found") },
1310         /* D     O   BK   */
1311         { SST(0x1C, 0x02, SS_RDEF,
1312             "Grown defect list not found") },
1313         /* DT  WRO   BK   */
1314         { SST(0x1D, 0x00, SS_FATAL,
1315             "Miscompare during verify operation") },
1316         /* D         B    */
1317         { SST(0x1D, 0x01, SS_RDEF,      /* XXX TBD */
1318             "Miscomparable verify of unmapped LBA") },
1319         /* D   W O   BK   */
1320         { SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1321             "Recovered ID with ECC correction") },
1322         /* D     O    K   */
1323         { SST(0x1F, 0x00, SS_RDEF,
1324             "Partial defect list transfer") },
1325         /* DTLPWROMAEBKVF */
1326         { SST(0x20, 0x00, SS_FATAL | EINVAL,
1327             "Invalid command operation code") },
1328         /* DT PWROMAEBK   */
1329         { SST(0x20, 0x01, SS_RDEF,      /* XXX TBD */
1330             "Access denied - initiator pending-enrolled") },
1331         /* DT PWROMAEBK   */
1332         { SST(0x20, 0x02, SS_RDEF,      /* XXX TBD */
1333             "Access denied - no access rights") },
1334         /* DT PWROMAEBK   */
1335         { SST(0x20, 0x03, SS_RDEF,      /* XXX TBD */
1336             "Access denied - invalid mgmt ID key") },
1337         /*  T             */
1338         { SST(0x20, 0x04, SS_RDEF,      /* XXX TBD */
1339             "Illegal command while in write capable state") },
1340         /*  T             */
1341         { SST(0x20, 0x05, SS_RDEF,      /* XXX TBD */
1342             "Obsolete") },
1343         /*  T             */
1344         { SST(0x20, 0x06, SS_RDEF,      /* XXX TBD */
1345             "Illegal command while in explicit address mode") },
1346         /*  T             */
1347         { SST(0x20, 0x07, SS_RDEF,      /* XXX TBD */
1348             "Illegal command while in implicit address mode") },
1349         /* DT PWROMAEBK   */
1350         { SST(0x20, 0x08, SS_RDEF,      /* XXX TBD */
1351             "Access denied - enrollment conflict") },
1352         /* DT PWROMAEBK   */
1353         { SST(0x20, 0x09, SS_RDEF,      /* XXX TBD */
1354             "Access denied - invalid LU identifier") },
1355         /* DT PWROMAEBK   */
1356         { SST(0x20, 0x0A, SS_RDEF,      /* XXX TBD */
1357             "Access denied - invalid proxy token") },
1358         /* DT PWROMAEBK   */
1359         { SST(0x20, 0x0B, SS_RDEF,      /* XXX TBD */
1360             "Access denied - ACL LUN conflict") },
1361         /*  T             */
1362         { SST(0x20, 0x0C, SS_FATAL | EINVAL,
1363             "Illegal command when not in append-only mode") },
1364         /* DT  WRO   BK   */
1365         { SST(0x21, 0x00, SS_FATAL | EINVAL,
1366             "Logical block address out of range") },
1367         /* DT  WROM  BK   */
1368         { SST(0x21, 0x01, SS_FATAL | EINVAL,
1369             "Invalid element address") },
1370         /*      R         */
1371         { SST(0x21, 0x02, SS_RDEF,      /* XXX TBD */
1372             "Invalid address for write") },
1373         /*      R         */
1374         { SST(0x21, 0x03, SS_RDEF,      /* XXX TBD */
1375             "Invalid write crossing layer jump") },
1376         /* D              */
1377         { SST(0x22, 0x00, SS_FATAL | EINVAL,
1378             "Illegal function (use 20 00, 24 00, or 26 00)") },
1379         /* DT P      B    */
1380         { SST(0x23, 0x00, SS_RDEF,      /* XXX TBD */
1381             "Invalid token operation, cause not reportable") },
1382         /* DT P      B    */
1383         { SST(0x23, 0x01, SS_RDEF,      /* XXX TBD */
1384             "Invalid token operation, unsupported token type") },
1385         /* DT P      B    */
1386         { SST(0x23, 0x02, SS_RDEF,      /* XXX TBD */
1387             "Invalid token operation, remote token usage not supported") },
1388         /* DT P      B    */
1389         { SST(0x23, 0x03, SS_RDEF,      /* XXX TBD */
1390             "Invalid token operation, remote ROD token creation not supported") },
1391         /* DT P      B    */
1392         { SST(0x23, 0x04, SS_RDEF,      /* XXX TBD */
1393             "Invalid token operation, token unknown") },
1394         /* DT P      B    */
1395         { SST(0x23, 0x05, SS_RDEF,      /* XXX TBD */
1396             "Invalid token operation, token corrupt") },
1397         /* DT P      B    */
1398         { SST(0x23, 0x06, SS_RDEF,      /* XXX TBD */
1399             "Invalid token operation, token revoked") },
1400         /* DT P      B    */
1401         { SST(0x23, 0x07, SS_RDEF,      /* XXX TBD */
1402             "Invalid token operation, token expired") },
1403         /* DT P      B    */
1404         { SST(0x23, 0x08, SS_RDEF,      /* XXX TBD */
1405             "Invalid token operation, token cancelled") },
1406         /* DT P      B    */
1407         { SST(0x23, 0x09, SS_RDEF,      /* XXX TBD */
1408             "Invalid token operation, token deleted") },
1409         /* DT P      B    */
1410         { SST(0x23, 0x0A, SS_RDEF,      /* XXX TBD */
1411             "Invalid token operation, invalid token length") },
1412         /* DTLPWROMAEBKVF */
1413         { SST(0x24, 0x00, SS_FATAL | EINVAL,
1414             "Invalid field in CDB") },
1415         /* DTLPWRO AEBKVF */
1416         { SST(0x24, 0x01, SS_RDEF,      /* XXX TBD */
1417             "CDB decryption error") },
1418         /*  T             */
1419         { SST(0x24, 0x02, SS_RDEF,      /* XXX TBD */
1420             "Obsolete") },
1421         /*  T             */
1422         { SST(0x24, 0x03, SS_RDEF,      /* XXX TBD */
1423             "Obsolete") },
1424         /*              F */
1425         { SST(0x24, 0x04, SS_RDEF,      /* XXX TBD */
1426             "Security audit value frozen") },
1427         /*              F */
1428         { SST(0x24, 0x05, SS_RDEF,      /* XXX TBD */
1429             "Security working key frozen") },
1430         /*              F */
1431         { SST(0x24, 0x06, SS_RDEF,      /* XXX TBD */
1432             "NONCE not unique") },
1433         /*              F */
1434         { SST(0x24, 0x07, SS_RDEF,      /* XXX TBD */
1435             "NONCE timestamp out of range") },
1436         /* DT   R MAEBKV  */
1437         { SST(0x24, 0x08, SS_RDEF,      /* XXX TBD */
1438             "Invalid XCDB") },
1439         /* DTLPWROMAEBKVF */
1440         { SST(0x25, 0x00, SS_FATAL | ENXIO,
1441             "Logical unit not supported") },
1442         /* DTLPWROMAEBKVF */
1443         { SST(0x26, 0x00, SS_FATAL | EINVAL,
1444             "Invalid field in parameter list") },
1445         /* DTLPWROMAEBKVF */
1446         { SST(0x26, 0x01, SS_FATAL | EINVAL,
1447             "Parameter not supported") },
1448         /* DTLPWROMAEBKVF */
1449         { SST(0x26, 0x02, SS_FATAL | EINVAL,
1450             "Parameter value invalid") },
1451         /* DTLPWROMAE K   */
1452         { SST(0x26, 0x03, SS_FATAL | EINVAL,
1453             "Threshold parameters not supported") },
1454         /* DTLPWROMAEBKVF */
1455         { SST(0x26, 0x04, SS_FATAL | EINVAL,
1456             "Invalid release of persistent reservation") },
1457         /* DTLPWRO A BK   */
1458         { SST(0x26, 0x05, SS_RDEF,      /* XXX TBD */
1459             "Data decryption error") },
1460         /* DTLPWRO    K   */
1461         { SST(0x26, 0x06, SS_RDEF,      /* XXX TBD */
1462             "Too many target descriptors") },
1463         /* DTLPWRO    K   */
1464         { SST(0x26, 0x07, SS_RDEF,      /* XXX TBD */
1465             "Unsupported target descriptor type code") },
1466         /* DTLPWRO    K   */
1467         { SST(0x26, 0x08, SS_RDEF,      /* XXX TBD */
1468             "Too many segment descriptors") },
1469         /* DTLPWRO    K   */
1470         { SST(0x26, 0x09, SS_RDEF,      /* XXX TBD */
1471             "Unsupported segment descriptor type code") },
1472         /* DTLPWRO    K   */
1473         { SST(0x26, 0x0A, SS_RDEF,      /* XXX TBD */
1474             "Unexpected inexact segment") },
1475         /* DTLPWRO    K   */
1476         { SST(0x26, 0x0B, SS_RDEF,      /* XXX TBD */
1477             "Inline data length exceeded") },
1478         /* DTLPWRO    K   */
1479         { SST(0x26, 0x0C, SS_RDEF,      /* XXX TBD */
1480             "Invalid operation for copy source or destination") },
1481         /* DTLPWRO    K   */
1482         { SST(0x26, 0x0D, SS_RDEF,      /* XXX TBD */
1483             "Copy segment granularity violation") },
1484         /* DT PWROMAEBK   */
1485         { SST(0x26, 0x0E, SS_RDEF,      /* XXX TBD */
1486             "Invalid parameter while port is enabled") },
1487         /*              F */
1488         { SST(0x26, 0x0F, SS_RDEF,      /* XXX TBD */
1489             "Invalid data-out buffer integrity check value") },
1490         /*  T             */
1491         { SST(0x26, 0x10, SS_RDEF,      /* XXX TBD */
1492             "Data decryption key fail limit reached") },
1493         /*  T             */
1494         { SST(0x26, 0x11, SS_RDEF,      /* XXX TBD */
1495             "Incomplete key-associated data set") },
1496         /*  T             */
1497         { SST(0x26, 0x12, SS_RDEF,      /* XXX TBD */
1498             "Vendor specific key reference not found") },
1499         /* DT  WRO   BK   */
1500         { SST(0x27, 0x00, SS_FATAL | EACCES,
1501             "Write protected") },
1502         /* DT  WRO   BK   */
1503         { SST(0x27, 0x01, SS_FATAL | EACCES,
1504             "Hardware write protected") },
1505         /* DT  WRO   BK   */
1506         { SST(0x27, 0x02, SS_FATAL | EACCES,
1507             "Logical unit software write protected") },
1508         /*  T   R         */
1509         { SST(0x27, 0x03, SS_FATAL | EACCES,
1510             "Associated write protect") },
1511         /*  T   R         */
1512         { SST(0x27, 0x04, SS_FATAL | EACCES,
1513             "Persistent write protect") },
1514         /*  T   R         */
1515         { SST(0x27, 0x05, SS_FATAL | EACCES,
1516             "Permanent write protect") },
1517         /*      R       F */
1518         { SST(0x27, 0x06, SS_RDEF,      /* XXX TBD */
1519             "Conditional write protect") },
1520         /* D         B    */
1521         { SST(0x27, 0x07, SS_RDEF,      /* XXX TBD */
1522             "Space allocation failed write protect") },
1523         /* DTLPWROMAEBKVF */
1524         { SST(0x28, 0x00, SS_FATAL | ENXIO,
1525             "Not ready to ready change, medium may have changed") },
1526         /* DT  WROM  B    */
1527         { SST(0x28, 0x01, SS_FATAL | ENXIO,
1528             "Import or export element accessed") },
1529         /*      R         */
1530         { SST(0x28, 0x02, SS_RDEF,      /* XXX TBD */
1531             "Format-layer may have changed") },
1532         /*        M       */
1533         { SST(0x28, 0x03, SS_RDEF,      /* XXX TBD */
1534             "Import/export element accessed, medium changed") },
1535         /*
1536          * XXX JGibbs - All of these should use the same errno, but I don't
1537          * think ENXIO is the correct choice.  Should we borrow from
1538          * the networking errnos?  ECONNRESET anyone?
1539          */
1540         /* DTLPWROMAEBKVF */
1541         { SST(0x29, 0x00, SS_FATAL | ENXIO,
1542             "Power on, reset, or bus device reset occurred") },
1543         /* DTLPWROMAEBKVF */
1544         { SST(0x29, 0x01, SS_RDEF,
1545             "Power on occurred") },
1546         /* DTLPWROMAEBKVF */
1547         { SST(0x29, 0x02, SS_RDEF,
1548             "SCSI bus reset occurred") },
1549         /* DTLPWROMAEBKVF */
1550         { SST(0x29, 0x03, SS_RDEF,
1551             "Bus device reset function occurred") },
1552         /* DTLPWROMAEBKVF */
1553         { SST(0x29, 0x04, SS_RDEF,
1554             "Device internal reset") },
1555         /* DTLPWROMAEBKVF */
1556         { SST(0x29, 0x05, SS_RDEF,
1557             "Transceiver mode changed to single-ended") },
1558         /* DTLPWROMAEBKVF */
1559         { SST(0x29, 0x06, SS_RDEF,
1560             "Transceiver mode changed to LVD") },
1561         /* DTLPWROMAEBKVF */
1562         { SST(0x29, 0x07, SS_RDEF,      /* XXX TBD */
1563             "I_T nexus loss occurred") },
1564         /* DTL WROMAEBKVF */
1565         { SST(0x2A, 0x00, SS_RDEF,
1566             "Parameters changed") },
1567         /* DTL WROMAEBKVF */
1568         { SST(0x2A, 0x01, SS_RDEF,
1569             "Mode parameters changed") },
1570         /* DTL WROMAE K   */
1571         { SST(0x2A, 0x02, SS_RDEF,
1572             "Log parameters changed") },
1573         /* DTLPWROMAE K   */
1574         { SST(0x2A, 0x03, SS_RDEF,
1575             "Reservations preempted") },
1576         /* DTLPWROMAE     */
1577         { SST(0x2A, 0x04, SS_RDEF,      /* XXX TBD */
1578             "Reservations released") },
1579         /* DTLPWROMAE     */
1580         { SST(0x2A, 0x05, SS_RDEF,      /* XXX TBD */
1581             "Registrations preempted") },
1582         /* DTLPWROMAEBKVF */
1583         { SST(0x2A, 0x06, SS_RDEF,      /* XXX TBD */
1584             "Asymmetric access state changed") },
1585         /* DTLPWROMAEBKVF */
1586         { SST(0x2A, 0x07, SS_RDEF,      /* XXX TBD */
1587             "Implicit asymmetric access state transition failed") },
1588         /* DT  WROMAEBKVF */
1589         { SST(0x2A, 0x08, SS_RDEF,      /* XXX TBD */
1590             "Priority changed") },
1591         /* D              */
1592         { SST(0x2A, 0x09, SS_RDEF,      /* XXX TBD */
1593             "Capacity data has changed") },
1594         /* DT             */
1595         { SST(0x2A, 0x0A, SS_RDEF,      /* XXX TBD */
1596             "Error history I_T nexus cleared") },
1597         /* DT             */
1598         { SST(0x2A, 0x0B, SS_RDEF,      /* XXX TBD */
1599             "Error history snapshot released") },
1600         /*              F */
1601         { SST(0x2A, 0x0C, SS_RDEF,      /* XXX TBD */
1602             "Error recovery attributes have changed") },
1603         /*  T             */
1604         { SST(0x2A, 0x0D, SS_RDEF,      /* XXX TBD */
1605             "Data encryption capabilities changed") },
1606         /* DT     M E  V  */
1607         { SST(0x2A, 0x10, SS_RDEF,      /* XXX TBD */
1608             "Timestamp changed") },
1609         /*  T             */
1610         { SST(0x2A, 0x11, SS_RDEF,      /* XXX TBD */
1611             "Data encryption parameters changed by another I_T nexus") },
1612         /*  T             */
1613         { SST(0x2A, 0x12, SS_RDEF,      /* XXX TBD */
1614             "Data encryption parameters changed by vendor specific event") },
1615         /*  T             */
1616         { SST(0x2A, 0x13, SS_RDEF,      /* XXX TBD */
1617             "Data encryption key instance counter has changed") },
1618         /* DT   R MAEBKV  */
1619         { SST(0x2A, 0x14, SS_RDEF,      /* XXX TBD */
1620             "SA creation capabilities data has changed") },
1621         /*  T     M    V  */
1622         { SST(0x2A, 0x15, SS_RDEF,      /* XXX TBD */
1623             "Medium removal prevention preempted") },
1624         /* DTLPWRO    K   */
1625         { SST(0x2B, 0x00, SS_RDEF,
1626             "Copy cannot execute since host cannot disconnect") },
1627         /* DTLPWROMAEBKVF */
1628         { SST(0x2C, 0x00, SS_RDEF,
1629             "Command sequence error") },
1630         /*                */
1631         { SST(0x2C, 0x01, SS_RDEF,
1632             "Too many windows specified") },
1633         /*                */
1634         { SST(0x2C, 0x02, SS_RDEF,
1635             "Invalid combination of windows specified") },
1636         /*      R         */
1637         { SST(0x2C, 0x03, SS_RDEF,
1638             "Current program area is not empty") },
1639         /*      R         */
1640         { SST(0x2C, 0x04, SS_RDEF,
1641             "Current program area is empty") },
1642         /*           B    */
1643         { SST(0x2C, 0x05, SS_RDEF,      /* XXX TBD */
1644             "Illegal power condition request") },
1645         /*      R         */
1646         { SST(0x2C, 0x06, SS_RDEF,      /* XXX TBD */
1647             "Persistent prevent conflict") },
1648         /* DTLPWROMAEBKVF */
1649         { SST(0x2C, 0x07, SS_RDEF,      /* XXX TBD */
1650             "Previous busy status") },
1651         /* DTLPWROMAEBKVF */
1652         { SST(0x2C, 0x08, SS_RDEF,      /* XXX TBD */
1653             "Previous task set full status") },
1654         /* DTLPWROM EBKVF */
1655         { SST(0x2C, 0x09, SS_RDEF,      /* XXX TBD */
1656             "Previous reservation conflict status") },
1657         /*              F */
1658         { SST(0x2C, 0x0A, SS_RDEF,      /* XXX TBD */
1659             "Partition or collection contains user objects") },
1660         /*  T             */
1661         { SST(0x2C, 0x0B, SS_RDEF,      /* XXX TBD */
1662             "Not reserved") },
1663         /* D              */
1664         { SST(0x2C, 0x0C, SS_RDEF,      /* XXX TBD */
1665             "ORWRITE generation does not match") },
1666         /*  T             */
1667         { SST(0x2D, 0x00, SS_RDEF,
1668             "Overwrite error on update in place") },
1669         /*      R         */
1670         { SST(0x2E, 0x00, SS_RDEF,      /* XXX TBD */
1671             "Insufficient time for operation") },
1672         /* DTLPWROMAEBKVF */
1673         { SST(0x2F, 0x00, SS_RDEF,
1674             "Commands cleared by another initiator") },
1675         /* D              */
1676         { SST(0x2F, 0x01, SS_RDEF,      /* XXX TBD */
1677             "Commands cleared by power loss notification") },
1678         /* DTLPWROMAEBKVF */
1679         { SST(0x2F, 0x02, SS_RDEF,      /* XXX TBD */
1680             "Commands cleared by device server") },
1681         /* DT  WROM  BK   */
1682         { SST(0x30, 0x00, SS_RDEF,
1683             "Incompatible medium installed") },
1684         /* DT  WRO   BK   */
1685         { SST(0x30, 0x01, SS_RDEF,
1686             "Cannot read medium - unknown format") },
1687         /* DT  WRO   BK   */
1688         { SST(0x30, 0x02, SS_RDEF,
1689             "Cannot read medium - incompatible format") },
1690         /* DT   R     K   */
1691         { SST(0x30, 0x03, SS_RDEF,
1692             "Cleaning cartridge installed") },
1693         /* DT  WRO   BK   */
1694         { SST(0x30, 0x04, SS_RDEF,
1695             "Cannot write medium - unknown format") },
1696         /* DT  WRO   BK   */
1697         { SST(0x30, 0x05, SS_RDEF,
1698             "Cannot write medium - incompatible format") },
1699         /* DT  WRO   B    */
1700         { SST(0x30, 0x06, SS_RDEF,
1701             "Cannot format medium - incompatible medium") },
1702         /* DTL WROMAEBKVF */
1703         { SST(0x30, 0x07, SS_RDEF,
1704             "Cleaning failure") },
1705         /*      R         */
1706         { SST(0x30, 0x08, SS_RDEF,
1707             "Cannot write - application code mismatch") },
1708         /*      R         */
1709         { SST(0x30, 0x09, SS_RDEF,
1710             "Current session not fixated for append") },
1711         /* DT  WRO AEBK   */
1712         { SST(0x30, 0x0A, SS_RDEF,      /* XXX TBD */
1713             "Cleaning request rejected") },
1714         /*  T             */
1715         { SST(0x30, 0x0C, SS_RDEF,      /* XXX TBD */
1716             "WORM medium - overwrite attempted") },
1717         /*  T             */
1718         { SST(0x30, 0x0D, SS_RDEF,      /* XXX TBD */
1719             "WORM medium - integrity check") },
1720         /*      R         */
1721         { SST(0x30, 0x10, SS_RDEF,      /* XXX TBD */
1722             "Medium not formatted") },
1723         /*        M       */
1724         { SST(0x30, 0x11, SS_RDEF,      /* XXX TBD */
1725             "Incompatible volume type") },
1726         /*        M       */
1727         { SST(0x30, 0x12, SS_RDEF,      /* XXX TBD */
1728             "Incompatible volume qualifier") },
1729         /*        M       */
1730         { SST(0x30, 0x13, SS_RDEF,      /* XXX TBD */
1731             "Cleaning volume expired") },
1732         /* DT  WRO   BK   */
1733         { SST(0x31, 0x00, SS_RDEF,
1734             "Medium format corrupted") },
1735         /* D L  RO   B    */
1736         { SST(0x31, 0x01, SS_RDEF,
1737             "Format command failed") },
1738         /*      R         */
1739         { SST(0x31, 0x02, SS_RDEF,      /* XXX TBD */
1740             "Zoned formatting failed due to spare linking") },
1741         /* D         B    */
1742         { SST(0x31, 0x03, SS_RDEF,      /* XXX TBD */
1743             "SANITIZE command failed") },
1744         /* D   W O   BK   */
1745         { SST(0x32, 0x00, SS_RDEF,
1746             "No defect spare location available") },
1747         /* D   W O   BK   */
1748         { SST(0x32, 0x01, SS_RDEF,
1749             "Defect list update failure") },
1750         /*  T             */
1751         { SST(0x33, 0x00, SS_RDEF,
1752             "Tape length error") },
1753         /* DTLPWROMAEBKVF */
1754         { SST(0x34, 0x00, SS_RDEF,
1755             "Enclosure failure") },
1756         /* DTLPWROMAEBKVF */
1757         { SST(0x35, 0x00, SS_RDEF,
1758             "Enclosure services failure") },
1759         /* DTLPWROMAEBKVF */
1760         { SST(0x35, 0x01, SS_RDEF,
1761             "Unsupported enclosure function") },
1762         /* DTLPWROMAEBKVF */
1763         { SST(0x35, 0x02, SS_RDEF,
1764             "Enclosure services unavailable") },
1765         /* DTLPWROMAEBKVF */
1766         { SST(0x35, 0x03, SS_RDEF,
1767             "Enclosure services transfer failure") },
1768         /* DTLPWROMAEBKVF */
1769         { SST(0x35, 0x04, SS_RDEF,
1770             "Enclosure services transfer refused") },
1771         /* DTL WROMAEBKVF */
1772         { SST(0x35, 0x05, SS_RDEF,      /* XXX TBD */
1773             "Enclosure services checksum error") },
1774         /*   L            */
1775         { SST(0x36, 0x00, SS_RDEF,
1776             "Ribbon, ink, or toner failure") },
1777         /* DTL WROMAEBKVF */
1778         { SST(0x37, 0x00, SS_RDEF,
1779             "Rounded parameter") },
1780         /*           B    */
1781         { SST(0x38, 0x00, SS_RDEF,      /* XXX TBD */
1782             "Event status notification") },
1783         /*           B    */
1784         { SST(0x38, 0x02, SS_RDEF,      /* XXX TBD */
1785             "ESN - power management class event") },
1786         /*           B    */
1787         { SST(0x38, 0x04, SS_RDEF,      /* XXX TBD */
1788             "ESN - media class event") },
1789         /*           B    */
1790         { SST(0x38, 0x06, SS_RDEF,      /* XXX TBD */
1791             "ESN - device busy class event") },
1792         /* D              */
1793         { SST(0x38, 0x07, SS_RDEF,      /* XXX TBD */
1794             "Thin provisioning soft threshold reached") },
1795         /* DTL WROMAE K   */
1796         { SST(0x39, 0x00, SS_RDEF,
1797             "Saving parameters not supported") },
1798         /* DTL WROM  BK   */
1799         { SST(0x3A, 0x00, SS_FATAL | ENXIO,
1800             "Medium not present") },
1801         /* DT  WROM  BK   */
1802         { SST(0x3A, 0x01, SS_FATAL | ENXIO,
1803             "Medium not present - tray closed") },
1804         /* DT  WROM  BK   */
1805         { SST(0x3A, 0x02, SS_FATAL | ENXIO,
1806             "Medium not present - tray open") },
1807         /* DT  WROM  B    */
1808         { SST(0x3A, 0x03, SS_RDEF,      /* XXX TBD */
1809             "Medium not present - loadable") },
1810         /* DT  WRO   B    */
1811         { SST(0x3A, 0x04, SS_RDEF,      /* XXX TBD */
1812             "Medium not present - medium auxiliary memory accessible") },
1813         /*  TL            */
1814         { SST(0x3B, 0x00, SS_RDEF,
1815             "Sequential positioning error") },
1816         /*  T             */
1817         { SST(0x3B, 0x01, SS_RDEF,
1818             "Tape position error at beginning-of-medium") },
1819         /*  T             */
1820         { SST(0x3B, 0x02, SS_RDEF,
1821             "Tape position error at end-of-medium") },
1822         /*   L            */
1823         { SST(0x3B, 0x03, SS_RDEF,
1824             "Tape or electronic vertical forms unit not ready") },
1825         /*   L            */
1826         { SST(0x3B, 0x04, SS_RDEF,
1827             "Slew failure") },
1828         /*   L            */
1829         { SST(0x3B, 0x05, SS_RDEF,
1830             "Paper jam") },
1831         /*   L            */
1832         { SST(0x3B, 0x06, SS_RDEF,
1833             "Failed to sense top-of-form") },
1834         /*   L            */
1835         { SST(0x3B, 0x07, SS_RDEF,
1836             "Failed to sense bottom-of-form") },
1837         /*  T             */
1838         { SST(0x3B, 0x08, SS_RDEF,
1839             "Reposition error") },
1840         /*                */
1841         { SST(0x3B, 0x09, SS_RDEF,
1842             "Read past end of medium") },
1843         /*                */
1844         { SST(0x3B, 0x0A, SS_RDEF,
1845             "Read past beginning of medium") },
1846         /*                */
1847         { SST(0x3B, 0x0B, SS_RDEF,
1848             "Position past end of medium") },
1849         /*  T             */
1850         { SST(0x3B, 0x0C, SS_RDEF,
1851             "Position past beginning of medium") },
1852         /* DT  WROM  BK   */
1853         { SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
1854             "Medium destination element full") },
1855         /* DT  WROM  BK   */
1856         { SST(0x3B, 0x0E, SS_RDEF,
1857             "Medium source element empty") },
1858         /*      R         */
1859         { SST(0x3B, 0x0F, SS_RDEF,
1860             "End of medium reached") },
1861         /* DT  WROM  BK   */
1862         { SST(0x3B, 0x11, SS_RDEF,
1863             "Medium magazine not accessible") },
1864         /* DT  WROM  BK   */
1865         { SST(0x3B, 0x12, SS_RDEF,
1866             "Medium magazine removed") },
1867         /* DT  WROM  BK   */
1868         { SST(0x3B, 0x13, SS_RDEF,
1869             "Medium magazine inserted") },
1870         /* DT  WROM  BK   */
1871         { SST(0x3B, 0x14, SS_RDEF,
1872             "Medium magazine locked") },
1873         /* DT  WROM  BK   */
1874         { SST(0x3B, 0x15, SS_RDEF,
1875             "Medium magazine unlocked") },
1876         /*      R         */
1877         { SST(0x3B, 0x16, SS_RDEF,      /* XXX TBD */
1878             "Mechanical positioning or changer error") },
1879         /*              F */
1880         { SST(0x3B, 0x17, SS_RDEF,      /* XXX TBD */
1881             "Read past end of user object") },
1882         /*        M       */
1883         { SST(0x3B, 0x18, SS_RDEF,      /* XXX TBD */
1884             "Element disabled") },
1885         /*        M       */
1886         { SST(0x3B, 0x19, SS_RDEF,      /* XXX TBD */
1887             "Element enabled") },
1888         /*        M       */
1889         { SST(0x3B, 0x1A, SS_RDEF,      /* XXX TBD */
1890             "Data transfer device removed") },
1891         /*        M       */
1892         { SST(0x3B, 0x1B, SS_RDEF,      /* XXX TBD */
1893             "Data transfer device inserted") },
1894         /*  T             */
1895         { SST(0x3B, 0x1C, SS_RDEF,      /* XXX TBD */
1896             "Too many logical objects on partition to support operation") },
1897         /* DTLPWROMAE K   */
1898         { SST(0x3D, 0x00, SS_RDEF,
1899             "Invalid bits in IDENTIFY message") },
1900         /* DTLPWROMAEBKVF */
1901         { SST(0x3E, 0x00, SS_RDEF,
1902             "Logical unit has not self-configured yet") },
1903         /* DTLPWROMAEBKVF */
1904         { SST(0x3E, 0x01, SS_RDEF,
1905             "Logical unit failure") },
1906         /* DTLPWROMAEBKVF */
1907         { SST(0x3E, 0x02, SS_RDEF,
1908             "Timeout on logical unit") },
1909         /* DTLPWROMAEBKVF */
1910         { SST(0x3E, 0x03, SS_RDEF,      /* XXX TBD */
1911             "Logical unit failed self-test") },
1912         /* DTLPWROMAEBKVF */
1913         { SST(0x3E, 0x04, SS_RDEF,      /* XXX TBD */
1914             "Logical unit unable to update self-test log") },
1915         /* DTLPWROMAEBKVF */
1916         { SST(0x3F, 0x00, SS_RDEF,
1917             "Target operating conditions have changed") },
1918         /* DTLPWROMAEBKVF */
1919         { SST(0x3F, 0x01, SS_RDEF,
1920             "Microcode has been changed") },
1921         /* DTLPWROM  BK   */
1922         { SST(0x3F, 0x02, SS_RDEF,
1923             "Changed operating definition") },
1924         /* DTLPWROMAEBKVF */
1925         { SST(0x3F, 0x03, SS_RDEF,
1926             "INQUIRY data has changed") },
1927         /* DT  WROMAEBK   */
1928         { SST(0x3F, 0x04, SS_RDEF,
1929             "Component device attached") },
1930         /* DT  WROMAEBK   */
1931         { SST(0x3F, 0x05, SS_RDEF,
1932             "Device identifier changed") },
1933         /* DT  WROMAEB    */
1934         { SST(0x3F, 0x06, SS_RDEF,
1935             "Redundancy group created or modified") },
1936         /* DT  WROMAEB    */
1937         { SST(0x3F, 0x07, SS_RDEF,
1938             "Redundancy group deleted") },
1939         /* DT  WROMAEB    */
1940         { SST(0x3F, 0x08, SS_RDEF,
1941             "Spare created or modified") },
1942         /* DT  WROMAEB    */
1943         { SST(0x3F, 0x09, SS_RDEF,
1944             "Spare deleted") },
1945         /* DT  WROMAEBK   */
1946         { SST(0x3F, 0x0A, SS_RDEF,
1947             "Volume set created or modified") },
1948         /* DT  WROMAEBK   */
1949         { SST(0x3F, 0x0B, SS_RDEF,
1950             "Volume set deleted") },
1951         /* DT  WROMAEBK   */
1952         { SST(0x3F, 0x0C, SS_RDEF,
1953             "Volume set deassigned") },
1954         /* DT  WROMAEBK   */
1955         { SST(0x3F, 0x0D, SS_RDEF,
1956             "Volume set reassigned") },
1957         /* DTLPWROMAE     */
1958         { SST(0x3F, 0x0E, SS_RDEF,      /* XXX TBD */
1959             "Reported LUNs data has changed") },
1960         /* DTLPWROMAEBKVF */
1961         { SST(0x3F, 0x0F, SS_RDEF,      /* XXX TBD */
1962             "Echo buffer overwritten") },
1963         /* DT  WROM  B    */
1964         { SST(0x3F, 0x10, SS_RDEF,      /* XXX TBD */
1965             "Medium loadable") },
1966         /* DT  WROM  B    */
1967         { SST(0x3F, 0x11, SS_RDEF,      /* XXX TBD */
1968             "Medium auxiliary memory accessible") },
1969         /* DTLPWR MAEBK F */
1970         { SST(0x3F, 0x12, SS_RDEF,      /* XXX TBD */
1971             "iSCSI IP address added") },
1972         /* DTLPWR MAEBK F */
1973         { SST(0x3F, 0x13, SS_RDEF,      /* XXX TBD */
1974             "iSCSI IP address removed") },
1975         /* DTLPWR MAEBK F */
1976         { SST(0x3F, 0x14, SS_RDEF,      /* XXX TBD */
1977             "iSCSI IP address changed") },
1978         /* D              */
1979         { SST(0x40, 0x00, SS_RDEF,
1980             "RAM failure") },           /* deprecated - use 40 NN instead */
1981         /* DTLPWROMAEBKVF */
1982         { SST(0x40, 0x80, SS_RDEF,
1983             "Diagnostic failure: ASCQ = Component ID") },
1984         /* DTLPWROMAEBKVF */
1985         { SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
1986             NULL) },                    /* Range 0x80->0xFF */
1987         /* D              */
1988         { SST(0x41, 0x00, SS_RDEF,
1989             "Data path failure") },     /* deprecated - use 40 NN instead */
1990         /* D              */
1991         { SST(0x42, 0x00, SS_RDEF,
1992             "Power-on or self-test failure") },
1993                                         /* deprecated - use 40 NN instead */
1994         /* DTLPWROMAEBKVF */
1995         { SST(0x43, 0x00, SS_RDEF,
1996             "Message error") },
1997         /* DTLPWROMAEBKVF */
1998         { SST(0x44, 0x00, SS_RDEF,
1999             "Internal target failure") },
2000         /* DT P   MAEBKVF */
2001         { SST(0x44, 0x01, SS_RDEF,      /* XXX TBD */
2002             "Persistent reservation information lost") },
2003         /* DT        B    */
2004         { SST(0x44, 0x71, SS_RDEF,      /* XXX TBD */
2005             "ATA device failed set features") },
2006         /* DTLPWROMAEBKVF */
2007         { SST(0x45, 0x00, SS_RDEF,
2008             "Select or reselect failure") },
2009         /* DTLPWROM  BK   */
2010         { SST(0x46, 0x00, SS_RDEF,
2011             "Unsuccessful soft reset") },
2012         /* DTLPWROMAEBKVF */
2013         { SST(0x47, 0x00, SS_RDEF,
2014             "SCSI parity error") },
2015         /* DTLPWROMAEBKVF */
2016         { SST(0x47, 0x01, SS_RDEF,      /* XXX TBD */
2017             "Data phase CRC error detected") },
2018         /* DTLPWROMAEBKVF */
2019         { SST(0x47, 0x02, SS_RDEF,      /* XXX TBD */
2020             "SCSI parity error detected during ST data phase") },
2021         /* DTLPWROMAEBKVF */
2022         { SST(0x47, 0x03, SS_RDEF,      /* XXX TBD */
2023             "Information unit iuCRC error detected") },
2024         /* DTLPWROMAEBKVF */
2025         { SST(0x47, 0x04, SS_RDEF,      /* XXX TBD */
2026             "Asynchronous information protection error detected") },
2027         /* DTLPWROMAEBKVF */
2028         { SST(0x47, 0x05, SS_RDEF,      /* XXX TBD */
2029             "Protocol service CRC error") },
2030         /* DT     MAEBKVF */
2031         { SST(0x47, 0x06, SS_RDEF,      /* XXX TBD */
2032             "PHY test function in progress") },
2033         /* DT PWROMAEBK   */
2034         { SST(0x47, 0x7F, SS_RDEF,      /* XXX TBD */
2035             "Some commands cleared by iSCSI protocol event") },
2036         /* DTLPWROMAEBKVF */
2037         { SST(0x48, 0x00, SS_RDEF,
2038             "Initiator detected error message received") },
2039         /* DTLPWROMAEBKVF */
2040         { SST(0x49, 0x00, SS_RDEF,
2041             "Invalid message error") },
2042         /* DTLPWROMAEBKVF */
2043         { SST(0x4A, 0x00, SS_RDEF,
2044             "Command phase error") },
2045         /* DTLPWROMAEBKVF */
2046         { SST(0x4B, 0x00, SS_RDEF,
2047             "Data phase error") },
2048         /* DT PWROMAEBK   */
2049         { SST(0x4B, 0x01, SS_RDEF,      /* XXX TBD */
2050             "Invalid target port transfer tag received") },
2051         /* DT PWROMAEBK   */
2052         { SST(0x4B, 0x02, SS_RDEF,      /* XXX TBD */
2053             "Too much write data") },
2054         /* DT PWROMAEBK   */
2055         { SST(0x4B, 0x03, SS_RDEF,      /* XXX TBD */
2056             "ACK/NAK timeout") },
2057         /* DT PWROMAEBK   */
2058         { SST(0x4B, 0x04, SS_RDEF,      /* XXX TBD */
2059             "NAK received") },
2060         /* DT PWROMAEBK   */
2061         { SST(0x4B, 0x05, SS_RDEF,      /* XXX TBD */
2062             "Data offset error") },
2063         /* DT PWROMAEBK   */
2064         { SST(0x4B, 0x06, SS_RDEF,      /* XXX TBD */
2065             "Initiator response timeout") },
2066         /* DT PWROMAEBK F */
2067         { SST(0x4B, 0x07, SS_RDEF,      /* XXX TBD */
2068             "Connection lost") },
2069         /* DT PWROMAEBK F */
2070         { SST(0x4B, 0x08, SS_RDEF,      /* XXX TBD */
2071             "Data-in buffer overflow - data buffer size") },
2072         /* DT PWROMAEBK F */
2073         { SST(0x4B, 0x09, SS_RDEF,      /* XXX TBD */
2074             "Data-in buffer overflow - data buffer descriptor area") },
2075         /* DT PWROMAEBK F */
2076         { SST(0x4B, 0x0A, SS_RDEF,      /* XXX TBD */
2077             "Data-in buffer error") },
2078         /* DT PWROMAEBK F */
2079         { SST(0x4B, 0x0B, SS_RDEF,      /* XXX TBD */
2080             "Data-out buffer overflow - data buffer size") },
2081         /* DT PWROMAEBK F */
2082         { SST(0x4B, 0x0C, SS_RDEF,      /* XXX TBD */
2083             "Data-out buffer overflow - data buffer descriptor area") },
2084         /* DT PWROMAEBK F */
2085         { SST(0x4B, 0x0D, SS_RDEF,      /* XXX TBD */
2086             "Data-out buffer error") },
2087         /* DTLPWROMAEBKVF */
2088         { SST(0x4C, 0x00, SS_RDEF,
2089             "Logical unit failed self-configuration") },
2090         /* DTLPWROMAEBKVF */
2091         { SST(0x4D, 0x00, SS_RDEF,
2092             "Tagged overlapped commands: ASCQ = Queue tag ID") },
2093         /* DTLPWROMAEBKVF */
2094         { SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
2095             NULL) },                    /* Range 0x00->0xFF */
2096         /* DTLPWROMAEBKVF */
2097         { SST(0x4E, 0x00, SS_RDEF,
2098             "Overlapped commands attempted") },
2099         /*  T             */
2100         { SST(0x50, 0x00, SS_RDEF,
2101             "Write append error") },
2102         /*  T             */
2103         { SST(0x50, 0x01, SS_RDEF,
2104             "Write append position error") },
2105         /*  T             */
2106         { SST(0x50, 0x02, SS_RDEF,
2107             "Position error related to timing") },
2108         /*  T   RO        */
2109         { SST(0x51, 0x00, SS_RDEF,
2110             "Erase failure") },
2111         /*      R         */
2112         { SST(0x51, 0x01, SS_RDEF,      /* XXX TBD */
2113             "Erase failure - incomplete erase operation detected") },
2114         /*  T             */
2115         { SST(0x52, 0x00, SS_RDEF,
2116             "Cartridge fault") },
2117         /* DTL WROM  BK   */
2118         { SST(0x53, 0x00, SS_RDEF,
2119             "Media load or eject failed") },
2120         /*  T             */
2121         { SST(0x53, 0x01, SS_RDEF,
2122             "Unload tape failure") },
2123         /* DT  WROM  BK   */
2124         { SST(0x53, 0x02, SS_RDEF,
2125             "Medium removal prevented") },
2126         /*        M       */
2127         { SST(0x53, 0x03, SS_RDEF,      /* XXX TBD */
2128             "Medium removal prevented by data transfer element") },
2129         /*  T             */
2130         { SST(0x53, 0x04, SS_RDEF,      /* XXX TBD */
2131             "Medium thread or unthread failure") },
2132         /*        M       */
2133         { SST(0x53, 0x05, SS_RDEF,      /* XXX TBD */
2134             "Volume identifier invalid") },
2135         /*  T             */
2136         { SST(0x53, 0x06, SS_RDEF,      /* XXX TBD */
2137             "Volume identifier missing") },
2138         /*        M       */
2139         { SST(0x53, 0x07, SS_RDEF,      /* XXX TBD */
2140             "Duplicate volume identifier") },
2141         /*        M       */
2142         { SST(0x53, 0x08, SS_RDEF,      /* XXX TBD */
2143             "Element status unknown") },
2144         /*    P           */
2145         { SST(0x54, 0x00, SS_RDEF,
2146             "SCSI to host system interface failure") },
2147         /*    P           */
2148         { SST(0x55, 0x00, SS_RDEF,
2149             "System resource failure") },
2150         /* D     O   BK   */
2151         { SST(0x55, 0x01, SS_FATAL | ENOSPC,
2152             "System buffer full") },
2153         /* DTLPWROMAE K   */
2154         { SST(0x55, 0x02, SS_RDEF,      /* XXX TBD */
2155             "Insufficient reservation resources") },
2156         /* DTLPWROMAE K   */
2157         { SST(0x55, 0x03, SS_RDEF,      /* XXX TBD */
2158             "Insufficient resources") },
2159         /* DTLPWROMAE K   */
2160         { SST(0x55, 0x04, SS_RDEF,      /* XXX TBD */
2161             "Insufficient registration resources") },
2162         /* DT PWROMAEBK   */
2163         { SST(0x55, 0x05, SS_RDEF,      /* XXX TBD */
2164             "Insufficient access control resources") },
2165         /* DT  WROM  B    */
2166         { SST(0x55, 0x06, SS_RDEF,      /* XXX TBD */
2167             "Auxiliary memory out of space") },
2168         /*              F */
2169         { SST(0x55, 0x07, SS_RDEF,      /* XXX TBD */
2170             "Quota error") },
2171         /*  T             */
2172         { SST(0x55, 0x08, SS_RDEF,      /* XXX TBD */
2173             "Maximum number of supplemental decryption keys exceeded") },
2174         /*        M       */
2175         { SST(0x55, 0x09, SS_RDEF,      /* XXX TBD */
2176             "Medium auxiliary memory not accessible") },
2177         /*        M       */
2178         { SST(0x55, 0x0A, SS_RDEF,      /* XXX TBD */
2179             "Data currently unavailable") },
2180         /* DTLPWROMAEBKVF */
2181         { SST(0x55, 0x0B, SS_RDEF,      /* XXX TBD */
2182             "Insufficient power for operation") },
2183         /* DT P      B    */
2184         { SST(0x55, 0x0C, SS_RDEF,      /* XXX TBD */
2185             "Insufficient resources to create ROD") },
2186         /* DT P      B    */
2187         { SST(0x55, 0x0D, SS_RDEF,      /* XXX TBD */
2188             "Insufficient resources to create ROD token") },
2189         /*      R         */
2190         { SST(0x57, 0x00, SS_RDEF,
2191             "Unable to recover table-of-contents") },
2192         /*       O        */
2193         { SST(0x58, 0x00, SS_RDEF,
2194             "Generation does not exist") },
2195         /*       O        */
2196         { SST(0x59, 0x00, SS_RDEF,
2197             "Updated block read") },
2198         /* DTLPWRO   BK   */
2199         { SST(0x5A, 0x00, SS_RDEF,
2200             "Operator request or state change input") },
2201         /* DT  WROM  BK   */
2202         { SST(0x5A, 0x01, SS_RDEF,
2203             "Operator medium removal request") },
2204         /* DT  WRO A BK   */
2205         { SST(0x5A, 0x02, SS_RDEF,
2206             "Operator selected write protect") },
2207         /* DT  WRO A BK   */
2208         { SST(0x5A, 0x03, SS_RDEF,
2209             "Operator selected write permit") },
2210         /* DTLPWROM   K   */
2211         { SST(0x5B, 0x00, SS_RDEF,
2212             "Log exception") },
2213         /* DTLPWROM   K   */
2214         { SST(0x5B, 0x01, SS_RDEF,
2215             "Threshold condition met") },
2216         /* DTLPWROM   K   */
2217         { SST(0x5B, 0x02, SS_RDEF,
2218             "Log counter at maximum") },
2219         /* DTLPWROM   K   */
2220         { SST(0x5B, 0x03, SS_RDEF,
2221             "Log list codes exhausted") },
2222         /* D     O        */
2223         { SST(0x5C, 0x00, SS_RDEF,
2224             "RPL status change") },
2225         /* D     O        */
2226         { SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2227             "Spindles synchronized") },
2228         /* D     O        */
2229         { SST(0x5C, 0x02, SS_RDEF,
2230             "Spindles not synchronized") },
2231         /* DTLPWROMAEBKVF */
2232         { SST(0x5D, 0x00, SS_RDEF,
2233             "Failure prediction threshold exceeded") },
2234         /*      R    B    */
2235         { SST(0x5D, 0x01, SS_RDEF,      /* XXX TBD */
2236             "Media failure prediction threshold exceeded") },
2237         /*      R         */
2238         { SST(0x5D, 0x02, SS_RDEF,      /* XXX TBD */
2239             "Logical unit failure prediction threshold exceeded") },
2240         /*      R         */
2241         { SST(0x5D, 0x03, SS_RDEF,      /* XXX TBD */
2242             "Spare area exhaustion prediction threshold exceeded") },
2243         /* D         B    */
2244         { SST(0x5D, 0x10, SS_RDEF,      /* XXX TBD */
2245             "Hardware impending failure general hard drive failure") },
2246         /* D         B    */
2247         { SST(0x5D, 0x11, SS_RDEF,      /* XXX TBD */
2248             "Hardware impending failure drive error rate too high") },
2249         /* D         B    */
2250         { SST(0x5D, 0x12, SS_RDEF,      /* XXX TBD */
2251             "Hardware impending failure data error rate too high") },
2252         /* D         B    */
2253         { SST(0x5D, 0x13, SS_RDEF,      /* XXX TBD */
2254             "Hardware impending failure seek error rate too high") },
2255         /* D         B    */
2256         { SST(0x5D, 0x14, SS_RDEF,      /* XXX TBD */
2257             "Hardware impending failure too many block reassigns") },
2258         /* D         B    */
2259         { SST(0x5D, 0x15, SS_RDEF,      /* XXX TBD */
2260             "Hardware impending failure access times too high") },
2261         /* D         B    */
2262         { SST(0x5D, 0x16, SS_RDEF,      /* XXX TBD */
2263             "Hardware impending failure start unit times too high") },
2264         /* D         B    */
2265         { SST(0x5D, 0x17, SS_RDEF,      /* XXX TBD */
2266             "Hardware impending failure channel parametrics") },
2267         /* D         B    */
2268         { SST(0x5D, 0x18, SS_RDEF,      /* XXX TBD */
2269             "Hardware impending failure controller detected") },
2270         /* D         B    */
2271         { SST(0x5D, 0x19, SS_RDEF,      /* XXX TBD */
2272             "Hardware impending failure throughput performance") },
2273         /* D         B    */
2274         { SST(0x5D, 0x1A, SS_RDEF,      /* XXX TBD */
2275             "Hardware impending failure seek time performance") },
2276         /* D         B    */
2277         { SST(0x5D, 0x1B, SS_RDEF,      /* XXX TBD */
2278             "Hardware impending failure spin-up retry count") },
2279         /* D         B    */
2280         { SST(0x5D, 0x1C, SS_RDEF,      /* XXX TBD */
2281             "Hardware impending failure drive calibration retry count") },
2282         /* D         B    */
2283         { SST(0x5D, 0x20, SS_RDEF,      /* XXX TBD */
2284             "Controller impending failure general hard drive failure") },
2285         /* D         B    */
2286         { SST(0x5D, 0x21, SS_RDEF,      /* XXX TBD */
2287             "Controller impending failure drive error rate too high") },
2288         /* D         B    */
2289         { SST(0x5D, 0x22, SS_RDEF,      /* XXX TBD */
2290             "Controller impending failure data error rate too high") },
2291         /* D         B    */
2292         { SST(0x5D, 0x23, SS_RDEF,      /* XXX TBD */
2293             "Controller impending failure seek error rate too high") },
2294         /* D         B    */
2295         { SST(0x5D, 0x24, SS_RDEF,      /* XXX TBD */
2296             "Controller impending failure too many block reassigns") },
2297         /* D         B    */
2298         { SST(0x5D, 0x25, SS_RDEF,      /* XXX TBD */
2299             "Controller impending failure access times too high") },
2300         /* D         B    */
2301         { SST(0x5D, 0x26, SS_RDEF,      /* XXX TBD */
2302             "Controller impending failure start unit times too high") },
2303         /* D         B    */
2304         { SST(0x5D, 0x27, SS_RDEF,      /* XXX TBD */
2305             "Controller impending failure channel parametrics") },
2306         /* D         B    */
2307         { SST(0x5D, 0x28, SS_RDEF,      /* XXX TBD */
2308             "Controller impending failure controller detected") },
2309         /* D         B    */
2310         { SST(0x5D, 0x29, SS_RDEF,      /* XXX TBD */
2311             "Controller impending failure throughput performance") },
2312         /* D         B    */
2313         { SST(0x5D, 0x2A, SS_RDEF,      /* XXX TBD */
2314             "Controller impending failure seek time performance") },
2315         /* D         B    */
2316         { SST(0x5D, 0x2B, SS_RDEF,      /* XXX TBD */
2317             "Controller impending failure spin-up retry count") },
2318         /* D         B    */
2319         { SST(0x5D, 0x2C, SS_RDEF,      /* XXX TBD */
2320             "Controller impending failure drive calibration retry count") },
2321         /* D         B    */
2322         { SST(0x5D, 0x30, SS_RDEF,      /* XXX TBD */
2323             "Data channel impending failure general hard drive failure") },
2324         /* D         B    */
2325         { SST(0x5D, 0x31, SS_RDEF,      /* XXX TBD */
2326             "Data channel impending failure drive error rate too high") },
2327         /* D         B    */
2328         { SST(0x5D, 0x32, SS_RDEF,      /* XXX TBD */
2329             "Data channel impending failure data error rate too high") },
2330         /* D         B    */
2331         { SST(0x5D, 0x33, SS_RDEF,      /* XXX TBD */
2332             "Data channel impending failure seek error rate too high") },
2333         /* D         B    */
2334         { SST(0x5D, 0x34, SS_RDEF,      /* XXX TBD */
2335             "Data channel impending failure too many block reassigns") },
2336         /* D         B    */
2337         { SST(0x5D, 0x35, SS_RDEF,      /* XXX TBD */
2338             "Data channel impending failure access times too high") },
2339         /* D         B    */
2340         { SST(0x5D, 0x36, SS_RDEF,      /* XXX TBD */
2341             "Data channel impending failure start unit times too high") },
2342         /* D         B    */
2343         { SST(0x5D, 0x37, SS_RDEF,      /* XXX TBD */
2344             "Data channel impending failure channel parametrics") },
2345         /* D         B    */
2346         { SST(0x5D, 0x38, SS_RDEF,      /* XXX TBD */
2347             "Data channel impending failure controller detected") },
2348         /* D         B    */
2349         { SST(0x5D, 0x39, SS_RDEF,      /* XXX TBD */
2350             "Data channel impending failure throughput performance") },
2351         /* D         B    */
2352         { SST(0x5D, 0x3A, SS_RDEF,      /* XXX TBD */
2353             "Data channel impending failure seek time performance") },
2354         /* D         B    */
2355         { SST(0x5D, 0x3B, SS_RDEF,      /* XXX TBD */
2356             "Data channel impending failure spin-up retry count") },
2357         /* D         B    */
2358         { SST(0x5D, 0x3C, SS_RDEF,      /* XXX TBD */
2359             "Data channel impending failure drive calibration retry count") },
2360         /* D         B    */
2361         { SST(0x5D, 0x40, SS_RDEF,      /* XXX TBD */
2362             "Servo impending failure general hard drive failure") },
2363         /* D         B    */
2364         { SST(0x5D, 0x41, SS_RDEF,      /* XXX TBD */
2365             "Servo impending failure drive error rate too high") },
2366         /* D         B    */
2367         { SST(0x5D, 0x42, SS_RDEF,      /* XXX TBD */
2368             "Servo impending failure data error rate too high") },
2369         /* D         B    */
2370         { SST(0x5D, 0x43, SS_RDEF,      /* XXX TBD */
2371             "Servo impending failure seek error rate too high") },
2372         /* D         B    */
2373         { SST(0x5D, 0x44, SS_RDEF,      /* XXX TBD */
2374             "Servo impending failure too many block reassigns") },
2375         /* D         B    */
2376         { SST(0x5D, 0x45, SS_RDEF,      /* XXX TBD */
2377             "Servo impending failure access times too high") },
2378         /* D         B    */
2379         { SST(0x5D, 0x46, SS_RDEF,      /* XXX TBD */
2380             "Servo impending failure start unit times too high") },
2381         /* D         B    */
2382         { SST(0x5D, 0x47, SS_RDEF,      /* XXX TBD */
2383             "Servo impending failure channel parametrics") },
2384         /* D         B    */
2385         { SST(0x5D, 0x48, SS_RDEF,      /* XXX TBD */
2386             "Servo impending failure controller detected") },
2387         /* D         B    */
2388         { SST(0x5D, 0x49, SS_RDEF,      /* XXX TBD */
2389             "Servo impending failure throughput performance") },
2390         /* D         B    */
2391         { SST(0x5D, 0x4A, SS_RDEF,      /* XXX TBD */
2392             "Servo impending failure seek time performance") },
2393         /* D         B    */
2394         { SST(0x5D, 0x4B, SS_RDEF,      /* XXX TBD */
2395             "Servo impending failure spin-up retry count") },
2396         /* D         B    */
2397         { SST(0x5D, 0x4C, SS_RDEF,      /* XXX TBD */
2398             "Servo impending failure drive calibration retry count") },
2399         /* D         B    */
2400         { SST(0x5D, 0x50, SS_RDEF,      /* XXX TBD */
2401             "Spindle impending failure general hard drive failure") },
2402         /* D         B    */
2403         { SST(0x5D, 0x51, SS_RDEF,      /* XXX TBD */
2404             "Spindle impending failure drive error rate too high") },
2405         /* D         B    */
2406         { SST(0x5D, 0x52, SS_RDEF,      /* XXX TBD */
2407             "Spindle impending failure data error rate too high") },
2408         /* D         B    */
2409         { SST(0x5D, 0x53, SS_RDEF,      /* XXX TBD */
2410             "Spindle impending failure seek error rate too high") },
2411         /* D         B    */
2412         { SST(0x5D, 0x54, SS_RDEF,      /* XXX TBD */
2413             "Spindle impending failure too many block reassigns") },
2414         /* D         B    */
2415         { SST(0x5D, 0x55, SS_RDEF,      /* XXX TBD */
2416             "Spindle impending failure access times too high") },
2417         /* D         B    */
2418         { SST(0x5D, 0x56, SS_RDEF,      /* XXX TBD */
2419             "Spindle impending failure start unit times too high") },
2420         /* D         B    */
2421         { SST(0x5D, 0x57, SS_RDEF,      /* XXX TBD */
2422             "Spindle impending failure channel parametrics") },
2423         /* D         B    */
2424         { SST(0x5D, 0x58, SS_RDEF,      /* XXX TBD */
2425             "Spindle impending failure controller detected") },
2426         /* D         B    */
2427         { SST(0x5D, 0x59, SS_RDEF,      /* XXX TBD */
2428             "Spindle impending failure throughput performance") },
2429         /* D         B    */
2430         { SST(0x5D, 0x5A, SS_RDEF,      /* XXX TBD */
2431             "Spindle impending failure seek time performance") },
2432         /* D         B    */
2433         { SST(0x5D, 0x5B, SS_RDEF,      /* XXX TBD */
2434             "Spindle impending failure spin-up retry count") },
2435         /* D         B    */
2436         { SST(0x5D, 0x5C, SS_RDEF,      /* XXX TBD */
2437             "Spindle impending failure drive calibration retry count") },
2438         /* D         B    */
2439         { SST(0x5D, 0x60, SS_RDEF,      /* XXX TBD */
2440             "Firmware impending failure general hard drive failure") },
2441         /* D         B    */
2442         { SST(0x5D, 0x61, SS_RDEF,      /* XXX TBD */
2443             "Firmware impending failure drive error rate too high") },
2444         /* D         B    */
2445         { SST(0x5D, 0x62, SS_RDEF,      /* XXX TBD */
2446             "Firmware impending failure data error rate too high") },
2447         /* D         B    */
2448         { SST(0x5D, 0x63, SS_RDEF,      /* XXX TBD */
2449             "Firmware impending failure seek error rate too high") },
2450         /* D         B    */
2451         { SST(0x5D, 0x64, SS_RDEF,      /* XXX TBD */
2452             "Firmware impending failure too many block reassigns") },
2453         /* D         B    */
2454         { SST(0x5D, 0x65, SS_RDEF,      /* XXX TBD */
2455             "Firmware impending failure access times too high") },
2456         /* D         B    */
2457         { SST(0x5D, 0x66, SS_RDEF,      /* XXX TBD */
2458             "Firmware impending failure start unit times too high") },
2459         /* D         B    */
2460         { SST(0x5D, 0x67, SS_RDEF,      /* XXX TBD */
2461             "Firmware impending failure channel parametrics") },
2462         /* D         B    */
2463         { SST(0x5D, 0x68, SS_RDEF,      /* XXX TBD */
2464             "Firmware impending failure controller detected") },
2465         /* D         B    */
2466         { SST(0x5D, 0x69, SS_RDEF,      /* XXX TBD */
2467             "Firmware impending failure throughput performance") },
2468         /* D         B    */
2469         { SST(0x5D, 0x6A, SS_RDEF,      /* XXX TBD */
2470             "Firmware impending failure seek time performance") },
2471         /* D         B    */
2472         { SST(0x5D, 0x6B, SS_RDEF,      /* XXX TBD */
2473             "Firmware impending failure spin-up retry count") },
2474         /* D         B    */
2475         { SST(0x5D, 0x6C, SS_RDEF,      /* XXX TBD */
2476             "Firmware impending failure drive calibration retry count") },
2477         /* DTLPWROMAEBKVF */
2478         { SST(0x5D, 0xFF, SS_RDEF,
2479             "Failure prediction threshold exceeded (false)") },
2480         /* DTLPWRO A  K   */
2481         { SST(0x5E, 0x00, SS_RDEF,
2482             "Low power condition on") },
2483         /* DTLPWRO A  K   */
2484         { SST(0x5E, 0x01, SS_RDEF,
2485             "Idle condition activated by timer") },
2486         /* DTLPWRO A  K   */
2487         { SST(0x5E, 0x02, SS_RDEF,
2488             "Standby condition activated by timer") },
2489         /* DTLPWRO A  K   */
2490         { SST(0x5E, 0x03, SS_RDEF,
2491             "Idle condition activated by command") },
2492         /* DTLPWRO A  K   */
2493         { SST(0x5E, 0x04, SS_RDEF,
2494             "Standby condition activated by command") },
2495         /* DTLPWRO A  K   */
2496         { SST(0x5E, 0x05, SS_RDEF,
2497             "Idle-B condition activated by timer") },
2498         /* DTLPWRO A  K   */
2499         { SST(0x5E, 0x06, SS_RDEF,
2500             "Idle-B condition activated by command") },
2501         /* DTLPWRO A  K   */
2502         { SST(0x5E, 0x07, SS_RDEF,
2503             "Idle-C condition activated by timer") },
2504         /* DTLPWRO A  K   */
2505         { SST(0x5E, 0x08, SS_RDEF,
2506             "Idle-C condition activated by command") },
2507         /* DTLPWRO A  K   */
2508         { SST(0x5E, 0x09, SS_RDEF,
2509             "Standby-Y condition activated by timer") },
2510         /* DTLPWRO A  K   */
2511         { SST(0x5E, 0x0A, SS_RDEF,
2512             "Standby-Y condition activated by command") },
2513         /*           B    */
2514         { SST(0x5E, 0x41, SS_RDEF,      /* XXX TBD */
2515             "Power state change to active") },
2516         /*           B    */
2517         { SST(0x5E, 0x42, SS_RDEF,      /* XXX TBD */
2518             "Power state change to idle") },
2519         /*           B    */
2520         { SST(0x5E, 0x43, SS_RDEF,      /* XXX TBD */
2521             "Power state change to standby") },
2522         /*           B    */
2523         { SST(0x5E, 0x45, SS_RDEF,      /* XXX TBD */
2524             "Power state change to sleep") },
2525         /*           BK   */
2526         { SST(0x5E, 0x47, SS_RDEF,      /* XXX TBD */
2527             "Power state change to device control") },
2528         /*                */
2529         { SST(0x60, 0x00, SS_RDEF,
2530             "Lamp failure") },
2531         /*                */
2532         { SST(0x61, 0x00, SS_RDEF,
2533             "Video acquisition error") },
2534         /*                */
2535         { SST(0x61, 0x01, SS_RDEF,
2536             "Unable to acquire video") },
2537         /*                */
2538         { SST(0x61, 0x02, SS_RDEF,
2539             "Out of focus") },
2540         /*                */
2541         { SST(0x62, 0x00, SS_RDEF,
2542             "Scan head positioning error") },
2543         /*      R         */
2544         { SST(0x63, 0x00, SS_RDEF,
2545             "End of user area encountered on this track") },
2546         /*      R         */
2547         { SST(0x63, 0x01, SS_FATAL | ENOSPC,
2548             "Packet does not fit in available space") },
2549         /*      R         */
2550         { SST(0x64, 0x00, SS_FATAL | ENXIO,
2551             "Illegal mode for this track") },
2552         /*      R         */
2553         { SST(0x64, 0x01, SS_RDEF,
2554             "Invalid packet size") },
2555         /* DTLPWROMAEBKVF */
2556         { SST(0x65, 0x00, SS_RDEF,
2557             "Voltage fault") },
2558         /*                */
2559         { SST(0x66, 0x00, SS_RDEF,
2560             "Automatic document feeder cover up") },
2561         /*                */
2562         { SST(0x66, 0x01, SS_RDEF,
2563             "Automatic document feeder lift up") },
2564         /*                */
2565         { SST(0x66, 0x02, SS_RDEF,
2566             "Document jam in automatic document feeder") },
2567         /*                */
2568         { SST(0x66, 0x03, SS_RDEF,
2569             "Document miss feed automatic in document feeder") },
2570         /*         A      */
2571         { SST(0x67, 0x00, SS_RDEF,
2572             "Configuration failure") },
2573         /*         A      */
2574         { SST(0x67, 0x01, SS_RDEF,
2575             "Configuration of incapable logical units failed") },
2576         /*         A      */
2577         { SST(0x67, 0x02, SS_RDEF,
2578             "Add logical unit failed") },
2579         /*         A      */
2580         { SST(0x67, 0x03, SS_RDEF,
2581             "Modification of logical unit failed") },
2582         /*         A      */
2583         { SST(0x67, 0x04, SS_RDEF,
2584             "Exchange of logical unit failed") },
2585         /*         A      */
2586         { SST(0x67, 0x05, SS_RDEF,
2587             "Remove of logical unit failed") },
2588         /*         A      */
2589         { SST(0x67, 0x06, SS_RDEF,
2590             "Attachment of logical unit failed") },
2591         /*         A      */
2592         { SST(0x67, 0x07, SS_RDEF,
2593             "Creation of logical unit failed") },
2594         /*         A      */
2595         { SST(0x67, 0x08, SS_RDEF,      /* XXX TBD */
2596             "Assign failure occurred") },
2597         /*         A      */
2598         { SST(0x67, 0x09, SS_RDEF,      /* XXX TBD */
2599             "Multiply assigned logical unit") },
2600         /* DTLPWROMAEBKVF */
2601         { SST(0x67, 0x0A, SS_RDEF,      /* XXX TBD */
2602             "Set target port groups command failed") },
2603         /* DT        B    */
2604         { SST(0x67, 0x0B, SS_RDEF,      /* XXX TBD */
2605             "ATA device feature not enabled") },
2606         /*         A      */
2607         { SST(0x68, 0x00, SS_RDEF,
2608             "Logical unit not configured") },
2609         /*         A      */
2610         { SST(0x69, 0x00, SS_RDEF,
2611             "Data loss on logical unit") },
2612         /*         A      */
2613         { SST(0x69, 0x01, SS_RDEF,
2614             "Multiple logical unit failures") },
2615         /*         A      */
2616         { SST(0x69, 0x02, SS_RDEF,
2617             "Parity/data mismatch") },
2618         /*         A      */
2619         { SST(0x6A, 0x00, SS_RDEF,
2620             "Informational, refer to log") },
2621         /*         A      */
2622         { SST(0x6B, 0x00, SS_RDEF,
2623             "State change has occurred") },
2624         /*         A      */
2625         { SST(0x6B, 0x01, SS_RDEF,
2626             "Redundancy level got better") },
2627         /*         A      */
2628         { SST(0x6B, 0x02, SS_RDEF,
2629             "Redundancy level got worse") },
2630         /*         A      */
2631         { SST(0x6C, 0x00, SS_RDEF,
2632             "Rebuild failure occurred") },
2633         /*         A      */
2634         { SST(0x6D, 0x00, SS_RDEF,
2635             "Recalculate failure occurred") },
2636         /*         A      */
2637         { SST(0x6E, 0x00, SS_RDEF,
2638             "Command to logical unit failed") },
2639         /*      R         */
2640         { SST(0x6F, 0x00, SS_RDEF,      /* XXX TBD */
2641             "Copy protection key exchange failure - authentication failure") },
2642         /*      R         */
2643         { SST(0x6F, 0x01, SS_RDEF,      /* XXX TBD */
2644             "Copy protection key exchange failure - key not present") },
2645         /*      R         */
2646         { SST(0x6F, 0x02, SS_RDEF,      /* XXX TBD */
2647             "Copy protection key exchange failure - key not established") },
2648         /*      R         */
2649         { SST(0x6F, 0x03, SS_RDEF,      /* XXX TBD */
2650             "Read of scrambled sector without authentication") },
2651         /*      R         */
2652         { SST(0x6F, 0x04, SS_RDEF,      /* XXX TBD */
2653             "Media region code is mismatched to logical unit region") },
2654         /*      R         */
2655         { SST(0x6F, 0x05, SS_RDEF,      /* XXX TBD */
2656             "Drive region must be permanent/region reset count error") },
2657         /*      R         */
2658         { SST(0x6F, 0x06, SS_RDEF,      /* XXX TBD */
2659             "Insufficient block count for binding NONCE recording") },
2660         /*      R         */
2661         { SST(0x6F, 0x07, SS_RDEF,      /* XXX TBD */
2662             "Conflict in binding NONCE recording") },
2663         /*  T             */
2664         { SST(0x70, 0x00, SS_RDEF,
2665             "Decompression exception short: ASCQ = Algorithm ID") },
2666         /*  T             */
2667         { SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
2668             NULL) },                    /* Range 0x00 -> 0xFF */
2669         /*  T             */
2670         { SST(0x71, 0x00, SS_RDEF,
2671             "Decompression exception long: ASCQ = Algorithm ID") },
2672         /*  T             */
2673         { SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
2674             NULL) },                    /* Range 0x00 -> 0xFF */
2675         /*      R         */
2676         { SST(0x72, 0x00, SS_RDEF,
2677             "Session fixation error") },
2678         /*      R         */
2679         { SST(0x72, 0x01, SS_RDEF,
2680             "Session fixation error writing lead-in") },
2681         /*      R         */
2682         { SST(0x72, 0x02, SS_RDEF,
2683             "Session fixation error writing lead-out") },
2684         /*      R         */
2685         { SST(0x72, 0x03, SS_RDEF,
2686             "Session fixation error - incomplete track in session") },
2687         /*      R         */
2688         { SST(0x72, 0x04, SS_RDEF,
2689             "Empty or partially written reserved track") },
2690         /*      R         */
2691         { SST(0x72, 0x05, SS_RDEF,      /* XXX TBD */
2692             "No more track reservations allowed") },
2693         /*      R         */
2694         { SST(0x72, 0x06, SS_RDEF,      /* XXX TBD */
2695             "RMZ extension is not allowed") },
2696         /*      R         */
2697         { SST(0x72, 0x07, SS_RDEF,      /* XXX TBD */
2698             "No more test zone extensions are allowed") },
2699         /*      R         */
2700         { SST(0x73, 0x00, SS_RDEF,
2701             "CD control error") },
2702         /*      R         */
2703         { SST(0x73, 0x01, SS_RDEF,
2704             "Power calibration area almost full") },
2705         /*      R         */
2706         { SST(0x73, 0x02, SS_FATAL | ENOSPC,
2707             "Power calibration area is full") },
2708         /*      R         */
2709         { SST(0x73, 0x03, SS_RDEF,
2710             "Power calibration area error") },
2711         /*      R         */
2712         { SST(0x73, 0x04, SS_RDEF,
2713             "Program memory area update failure") },
2714         /*      R         */
2715         { SST(0x73, 0x05, SS_RDEF,
2716             "Program memory area is full") },
2717         /*      R         */
2718         { SST(0x73, 0x06, SS_RDEF,      /* XXX TBD */
2719             "RMA/PMA is almost full") },
2720         /*      R         */
2721         { SST(0x73, 0x10, SS_RDEF,      /* XXX TBD */
2722             "Current power calibration area almost full") },
2723         /*      R         */
2724         { SST(0x73, 0x11, SS_RDEF,      /* XXX TBD */
2725             "Current power calibration area is full") },
2726         /*      R         */
2727         { SST(0x73, 0x17, SS_RDEF,      /* XXX TBD */
2728             "RDZ is full") },
2729         /*  T             */
2730         { SST(0x74, 0x00, SS_RDEF,      /* XXX TBD */
2731             "Security error") },
2732         /*  T             */
2733         { SST(0x74, 0x01, SS_RDEF,      /* XXX TBD */
2734             "Unable to decrypt data") },
2735         /*  T             */
2736         { SST(0x74, 0x02, SS_RDEF,      /* XXX TBD */
2737             "Unencrypted data encountered while decrypting") },
2738         /*  T             */
2739         { SST(0x74, 0x03, SS_RDEF,      /* XXX TBD */
2740             "Incorrect data encryption key") },
2741         /*  T             */
2742         { SST(0x74, 0x04, SS_RDEF,      /* XXX TBD */
2743             "Cryptographic integrity validation failed") },
2744         /*  T             */
2745         { SST(0x74, 0x05, SS_RDEF,      /* XXX TBD */
2746             "Error decrypting data") },
2747         /*  T             */
2748         { SST(0x74, 0x06, SS_RDEF,      /* XXX TBD */
2749             "Unknown signature verification key") },
2750         /*  T             */
2751         { SST(0x74, 0x07, SS_RDEF,      /* XXX TBD */
2752             "Encryption parameters not useable") },
2753         /* DT   R M E  VF */
2754         { SST(0x74, 0x08, SS_RDEF,      /* XXX TBD */
2755             "Digital signature validation failure") },
2756         /*  T             */
2757         { SST(0x74, 0x09, SS_RDEF,      /* XXX TBD */
2758             "Encryption mode mismatch on read") },
2759         /*  T             */
2760         { SST(0x74, 0x0A, SS_RDEF,      /* XXX TBD */
2761             "Encrypted block not raw read enabled") },
2762         /*  T             */
2763         { SST(0x74, 0x0B, SS_RDEF,      /* XXX TBD */
2764             "Incorrect encryption parameters") },
2765         /* DT   R MAEBKV  */
2766         { SST(0x74, 0x0C, SS_RDEF,      /* XXX TBD */
2767             "Unable to decrypt parameter list") },
2768         /*  T             */
2769         { SST(0x74, 0x0D, SS_RDEF,      /* XXX TBD */
2770             "Encryption algorithm disabled") },
2771         /* DT   R MAEBKV  */
2772         { SST(0x74, 0x10, SS_RDEF,      /* XXX TBD */
2773             "SA creation parameter value invalid") },
2774         /* DT   R MAEBKV  */
2775         { SST(0x74, 0x11, SS_RDEF,      /* XXX TBD */
2776             "SA creation parameter value rejected") },
2777         /* DT   R MAEBKV  */
2778         { SST(0x74, 0x12, SS_RDEF,      /* XXX TBD */
2779             "Invalid SA usage") },
2780         /*  T             */
2781         { SST(0x74, 0x21, SS_RDEF,      /* XXX TBD */
2782             "Data encryption configuration prevented") },
2783         /* DT   R MAEBKV  */
2784         { SST(0x74, 0x30, SS_RDEF,      /* XXX TBD */
2785             "SA creation parameter not supported") },
2786         /* DT   R MAEBKV  */
2787         { SST(0x74, 0x40, SS_RDEF,      /* XXX TBD */
2788             "Authentication failed") },
2789         /*             V  */
2790         { SST(0x74, 0x61, SS_RDEF,      /* XXX TBD */
2791             "External data encryption key manager access error") },
2792         /*             V  */
2793         { SST(0x74, 0x62, SS_RDEF,      /* XXX TBD */
2794             "External data encryption key manager error") },
2795         /*             V  */
2796         { SST(0x74, 0x63, SS_RDEF,      /* XXX TBD */
2797             "External data encryption key not found") },
2798         /*             V  */
2799         { SST(0x74, 0x64, SS_RDEF,      /* XXX TBD */
2800             "External data encryption request not authorized") },
2801         /*  T             */
2802         { SST(0x74, 0x6E, SS_RDEF,      /* XXX TBD */
2803             "External data encryption control timeout") },
2804         /*  T             */
2805         { SST(0x74, 0x6F, SS_RDEF,      /* XXX TBD */
2806             "External data encryption control error") },
2807         /* DT   R M E  V  */
2808         { SST(0x74, 0x71, SS_RDEF,      /* XXX TBD */
2809             "Logical unit access not authorized") },
2810         /* D              */
2811         { SST(0x74, 0x79, SS_RDEF,      /* XXX TBD */
2812             "Security conflict in translated device") }
2813 };
2814
2815 const int asc_table_size = sizeof(asc_table)/sizeof(asc_table[0]);
2816
2817 struct asc_key
2818 {
2819         int asc;
2820         int ascq;
2821 };
2822
2823 static int
2824 ascentrycomp(const void *key, const void *member)
2825 {
2826         int asc;
2827         int ascq;
2828         const struct asc_table_entry *table_entry;
2829
2830         asc = ((const struct asc_key *)key)->asc;
2831         ascq = ((const struct asc_key *)key)->ascq;
2832         table_entry = (const struct asc_table_entry *)member;
2833
2834         if (asc >= table_entry->asc) {
2835
2836                 if (asc > table_entry->asc)
2837                         return (1);
2838
2839                 if (ascq <= table_entry->ascq) {
2840                         /* Check for ranges */
2841                         if (ascq == table_entry->ascq
2842                          || ((table_entry->action & SSQ_RANGE) != 0
2843                            && ascq >= (table_entry - 1)->ascq))
2844                                 return (0);
2845                         return (-1);
2846                 }
2847                 return (1);
2848         }
2849         return (-1);
2850 }
2851
2852 static int
2853 senseentrycomp(const void *key, const void *member)
2854 {
2855         int sense_key;
2856         const struct sense_key_table_entry *table_entry;
2857
2858         sense_key = *((const int *)key);
2859         table_entry = (const struct sense_key_table_entry *)member;
2860
2861         if (sense_key >= table_entry->sense_key) {
2862                 if (sense_key == table_entry->sense_key)
2863                         return (0);
2864                 return (1);
2865         }
2866         return (-1);
2867 }
2868
2869 static void
2870 fetchtableentries(int sense_key, int asc, int ascq,
2871                   struct scsi_inquiry_data *inq_data,
2872                   const struct sense_key_table_entry **sense_entry,
2873                   const struct asc_table_entry **asc_entry)
2874 {
2875         caddr_t match;
2876         const struct asc_table_entry *asc_tables[2];
2877         const struct sense_key_table_entry *sense_tables[2];
2878         struct asc_key asc_ascq;
2879         size_t asc_tables_size[2];
2880         size_t sense_tables_size[2];
2881         int num_asc_tables;
2882         int num_sense_tables;
2883         int i;
2884
2885         /* Default to failure */
2886         *sense_entry = NULL;
2887         *asc_entry = NULL;
2888         match = NULL;
2889         if (inq_data != NULL)
2890                 match = cam_quirkmatch((caddr_t)inq_data,
2891                                        (caddr_t)sense_quirk_table,
2892                                        sense_quirk_table_size,
2893                                        sizeof(*sense_quirk_table),
2894                                        scsi_inquiry_match);
2895
2896         if (match != NULL) {
2897                 struct scsi_sense_quirk_entry *quirk;
2898
2899                 quirk = (struct scsi_sense_quirk_entry *)match;
2900                 asc_tables[0] = quirk->asc_info;
2901                 asc_tables_size[0] = quirk->num_ascs;
2902                 asc_tables[1] = asc_table;
2903                 asc_tables_size[1] = asc_table_size;
2904                 num_asc_tables = 2;
2905                 sense_tables[0] = quirk->sense_key_info;
2906                 sense_tables_size[0] = quirk->num_sense_keys;
2907                 sense_tables[1] = sense_key_table;
2908                 sense_tables_size[1] = sense_key_table_size;
2909                 num_sense_tables = 2;
2910         } else {
2911                 asc_tables[0] = asc_table;
2912                 asc_tables_size[0] = asc_table_size;
2913                 num_asc_tables = 1;
2914                 sense_tables[0] = sense_key_table;
2915                 sense_tables_size[0] = sense_key_table_size;
2916                 num_sense_tables = 1;
2917         }
2918
2919         asc_ascq.asc = asc;
2920         asc_ascq.ascq = ascq;
2921         for (i = 0; i < num_asc_tables; i++) {
2922                 void *found_entry;
2923
2924                 found_entry = bsearch(&asc_ascq, asc_tables[i],
2925                                       asc_tables_size[i],
2926                                       sizeof(**asc_tables),
2927                                       ascentrycomp);
2928
2929                 if (found_entry) {
2930                         *asc_entry = (struct asc_table_entry *)found_entry;
2931                         break;
2932                 }
2933         }
2934
2935         for (i = 0; i < num_sense_tables; i++) {
2936                 void *found_entry;
2937
2938                 found_entry = bsearch(&sense_key, sense_tables[i],
2939                                       sense_tables_size[i],
2940                                       sizeof(**sense_tables),
2941                                       senseentrycomp);
2942
2943                 if (found_entry) {
2944                         *sense_entry =
2945                             (struct sense_key_table_entry *)found_entry;
2946                         break;
2947                 }
2948         }
2949 }
2950
2951 void
2952 scsi_sense_desc(int sense_key, int asc, int ascq,
2953                 struct scsi_inquiry_data *inq_data,
2954                 const char **sense_key_desc, const char **asc_desc)
2955 {
2956         const struct asc_table_entry *asc_entry;
2957         const struct sense_key_table_entry *sense_entry;
2958
2959         fetchtableentries(sense_key, asc, ascq,
2960                           inq_data,
2961                           &sense_entry,
2962                           &asc_entry);
2963
2964         *sense_key_desc = sense_entry->desc;
2965
2966         if (asc_entry != NULL)
2967                 *asc_desc = asc_entry->desc;
2968         else if (asc >= 0x80 && asc <= 0xff)
2969                 *asc_desc = "Vendor Specific ASC";
2970         else if (ascq >= 0x80 && ascq <= 0xff)
2971                 *asc_desc = "Vendor Specific ASCQ";
2972         else
2973                 *asc_desc = "Reserved ASC/ASCQ pair";
2974 }
2975
2976 /*
2977  * Given sense and device type information, return the appropriate action.
2978  * If we do not understand the specific error as identified by the ASC/ASCQ
2979  * pair, fall back on the more generic actions derived from the sense key.
2980  */
2981 scsi_sense_action
2982 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
2983                   u_int32_t sense_flags)
2984 {
2985         const struct asc_table_entry *asc_entry;
2986         const struct sense_key_table_entry *sense_entry;
2987         int error_code, sense_key, asc, ascq;
2988         scsi_sense_action action;
2989
2990         scsi_extract_sense(&csio->sense_data, &error_code,
2991                            &sense_key, &asc, &ascq);
2992
2993         if (error_code == SSD_DEFERRED_ERROR) {
2994                 /*
2995                  * XXX dufault@FreeBSD.org
2996                  * This error doesn't relate to the command associated
2997                  * with this request sense.  A deferred error is an error
2998                  * for a command that has already returned GOOD status
2999                  * (see SCSI2 8.2.14.2).
3000                  *
3001                  * By my reading of that section, it looks like the current
3002                  * command has been cancelled, we should now clean things up
3003                  * (hopefully recovering any lost data) and then retry the
3004                  * current command.  There are two easy choices, both wrong:
3005                  *
3006                  * 1. Drop through (like we had been doing), thus treating
3007                  *    this as if the error were for the current command and
3008                  *    return and stop the current command.
3009                  * 
3010                  * 2. Issue a retry (like I made it do) thus hopefully
3011                  *    recovering the current transfer, and ignoring the
3012                  *    fact that we've dropped a command.
3013                  *
3014                  * These should probably be handled in a device specific
3015                  * sense handler or punted back up to a user mode daemon
3016                  */
3017                 action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3018         } else {
3019                 fetchtableentries(sense_key, asc, ascq,
3020                                   inq_data,
3021                                   &sense_entry,
3022                                   &asc_entry);
3023
3024                 /*
3025                  * Override the 'No additional Sense' entry (0,0)
3026                  * with the error action of the sense key.
3027                  */
3028                 if (asc_entry != NULL
3029                  && (asc != 0 || ascq != 0))
3030                         action = asc_entry->action;
3031                 else
3032                         action = sense_entry->action;
3033
3034                 if (sense_key == SSD_KEY_RECOVERED_ERROR) {
3035                         /*
3036                          * The action succeeded but the device wants
3037                          * the user to know that some recovery action
3038                          * was required.
3039                          */
3040                         action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
3041                         action |= SS_NOP|SSQ_PRINT_SENSE;
3042                 } else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
3043                         if ((sense_flags & SF_QUIET_IR) != 0)
3044                                 action &= ~SSQ_PRINT_SENSE;
3045                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
3046                         if ((sense_flags & SF_RETRY_UA) != 0
3047                          && (action & SS_MASK) == SS_FAIL) {
3048                                 action &= ~(SS_MASK|SSQ_MASK);
3049                                 action |= SS_RETRY|SSQ_DECREMENT_COUNT|
3050                                           SSQ_PRINT_SENSE;
3051                         }
3052                 }
3053         }
3054 #ifdef _KERNEL
3055         if (bootverbose)
3056                 sense_flags |= SF_PRINT_ALWAYS;
3057 #endif
3058         if ((sense_flags & SF_PRINT_ALWAYS) != 0)
3059                 action |= SSQ_PRINT_SENSE;
3060         else if ((sense_flags & SF_NO_PRINT) != 0)
3061                 action &= ~SSQ_PRINT_SENSE;
3062
3063         return (action);
3064 }
3065
3066 char *
3067 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
3068 {
3069         u_int8_t cdb_len;
3070         int i;
3071
3072         if (cdb_ptr == NULL)
3073                 return("");
3074
3075         /* Silence warnings */
3076         cdb_len = 0;
3077
3078         /*
3079          * This is taken from the SCSI-3 draft spec.
3080          * (T10/1157D revision 0.3)
3081          * The top 3 bits of an opcode are the group code.  The next 5 bits
3082          * are the command code.
3083          * Group 0:  six byte commands
3084          * Group 1:  ten byte commands
3085          * Group 2:  ten byte commands
3086          * Group 3:  reserved
3087          * Group 4:  sixteen byte commands
3088          * Group 5:  twelve byte commands
3089          * Group 6:  vendor specific
3090          * Group 7:  vendor specific
3091          */
3092         switch((*cdb_ptr >> 5) & 0x7) {
3093                 case 0:
3094                         cdb_len = 6;
3095                         break;
3096                 case 1:
3097                 case 2:
3098                         cdb_len = 10;
3099                         break;
3100                 case 3:
3101                 case 6:
3102                 case 7:
3103                         /* in this case, just print out the opcode */
3104                         cdb_len = 1;
3105                         break;
3106                 case 4:
3107                         cdb_len = 16;
3108                         break;
3109                 case 5:
3110                         cdb_len = 12;
3111                         break;
3112         }
3113         *cdb_string = '\0';
3114         for (i = 0; i < cdb_len; i++)
3115                 snprintf(cdb_string + strlen(cdb_string),
3116                          len - strlen(cdb_string), "%02hhx ", cdb_ptr[i]);
3117
3118         return(cdb_string);
3119 }
3120
3121 const char *
3122 scsi_status_string(struct ccb_scsiio *csio)
3123 {
3124         switch(csio->scsi_status) {
3125         case SCSI_STATUS_OK:
3126                 return("OK");
3127         case SCSI_STATUS_CHECK_COND:
3128                 return("Check Condition");
3129         case SCSI_STATUS_BUSY:
3130                 return("Busy");
3131         case SCSI_STATUS_INTERMED:
3132                 return("Intermediate");
3133         case SCSI_STATUS_INTERMED_COND_MET:
3134                 return("Intermediate-Condition Met");
3135         case SCSI_STATUS_RESERV_CONFLICT:
3136                 return("Reservation Conflict");
3137         case SCSI_STATUS_CMD_TERMINATED:
3138                 return("Command Terminated");
3139         case SCSI_STATUS_QUEUE_FULL:
3140                 return("Queue Full");
3141         case SCSI_STATUS_ACA_ACTIVE:
3142                 return("ACA Active");
3143         case SCSI_STATUS_TASK_ABORTED:
3144                 return("Task Aborted");
3145         default: {
3146                 static char unkstr[64];
3147                 snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
3148                          csio->scsi_status);
3149                 return(unkstr);
3150         }
3151         }
3152 }
3153
3154 /*
3155  * scsi_command_string() returns 0 for success and -1 for failure.
3156  */
3157 #ifdef _KERNEL
3158 int
3159 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
3160 #else /* !_KERNEL */
3161 int
3162 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio, 
3163                     struct sbuf *sb)
3164 #endif /* _KERNEL/!_KERNEL */
3165 {
3166         struct scsi_inquiry_data *inq_data;
3167         char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3168 #ifdef _KERNEL
3169         struct    ccb_getdev *cgd;
3170 #endif /* _KERNEL */
3171
3172 #ifdef _KERNEL
3173         if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3174                 return(-1);
3175         /*
3176          * Get the device information.
3177          */
3178         xpt_setup_ccb(&cgd->ccb_h,
3179                       csio->ccb_h.path,
3180                       CAM_PRIORITY_NORMAL);
3181         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3182         xpt_action((union ccb *)cgd);
3183
3184         /*
3185          * If the device is unconfigured, just pretend that it is a hard
3186          * drive.  scsi_op_desc() needs this.
3187          */
3188         if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3189                 cgd->inq_data.device = T_DIRECT;
3190
3191         inq_data = &cgd->inq_data;
3192
3193 #else /* !_KERNEL */
3194
3195         inq_data = &device->inq_data;
3196
3197 #endif /* _KERNEL/!_KERNEL */
3198
3199         if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3200                 sbuf_printf(sb, "%s. CDB: %s", 
3201                             scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data),
3202                             scsi_cdb_string(csio->cdb_io.cdb_ptr, cdb_str,
3203                                             sizeof(cdb_str)));
3204         } else {
3205                 sbuf_printf(sb, "%s. CDB: %s",
3206                             scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data),
3207                             scsi_cdb_string(csio->cdb_io.cdb_bytes, cdb_str,
3208                                             sizeof(cdb_str)));
3209         }
3210
3211 #ifdef _KERNEL
3212         xpt_free_ccb((union ccb *)cgd);
3213 #endif
3214
3215         return(0);
3216 }
3217
3218
3219 /*
3220  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
3221  */
3222 #ifdef _KERNEL
3223 int
3224 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
3225                 scsi_sense_string_flags flags)
3226 #else /* !_KERNEL */
3227 int
3228 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio, 
3229                 struct sbuf *sb, scsi_sense_string_flags flags)
3230 #endif /* _KERNEL/!_KERNEL */
3231 {
3232         struct    scsi_sense_data *sense;
3233         struct    scsi_inquiry_data *inq_data;
3234 #ifdef _KERNEL
3235         struct    ccb_getdev *cgd;
3236 #endif /* _KERNEL */
3237         u_int32_t info;
3238         int       error_code;
3239         int       sense_key;
3240         int       asc, ascq;
3241         char      path_str[64];
3242
3243 #ifndef _KERNEL
3244         if (device == NULL)
3245                 return(-1);
3246 #endif /* !_KERNEL */
3247         if ((csio == NULL) || (sb == NULL))
3248                 return(-1);
3249
3250         /*
3251          * If the CDB is a physical address, we can't deal with it..
3252          */
3253         if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
3254                 flags &= ~SSS_FLAG_PRINT_COMMAND;
3255
3256 #ifdef _KERNEL
3257         xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
3258 #else /* !_KERNEL */
3259         cam_path_string(device, path_str, sizeof(path_str));
3260 #endif /* _KERNEL/!_KERNEL */
3261
3262 #ifdef _KERNEL
3263         if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3264                 return(-1);
3265         /*
3266          * Get the device information.
3267          */
3268         xpt_setup_ccb(&cgd->ccb_h,
3269                       csio->ccb_h.path,
3270                       CAM_PRIORITY_NORMAL);
3271         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3272         xpt_action((union ccb *)cgd);
3273
3274         /*
3275          * If the device is unconfigured, just pretend that it is a hard
3276          * drive.  scsi_op_desc() needs this.
3277          */
3278         if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3279                 cgd->inq_data.device = T_DIRECT;
3280
3281         inq_data = &cgd->inq_data;
3282
3283 #else /* !_KERNEL */
3284
3285         inq_data = &device->inq_data;
3286
3287 #endif /* _KERNEL/!_KERNEL */
3288
3289         sense = NULL;
3290
3291         if (flags & SSS_FLAG_PRINT_COMMAND) {
3292
3293                 sbuf_cat(sb, path_str);
3294
3295 #ifdef _KERNEL
3296                 scsi_command_string(csio, sb);
3297 #else /* !_KERNEL */
3298                 scsi_command_string(device, csio, sb);
3299 #endif /* _KERNEL/!_KERNEL */
3300                 sbuf_printf(sb, "\n");
3301         }
3302
3303         /*
3304          * If the sense data is a physical pointer, forget it.
3305          */
3306         if (csio->ccb_h.flags & CAM_SENSE_PTR) {
3307                 if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
3308 #ifdef _KERNEL
3309                         xpt_free_ccb((union ccb*)cgd);
3310 #endif /* _KERNEL/!_KERNEL */
3311                         return(-1);
3312                 } else {
3313                         /* 
3314                          * bcopy the pointer to avoid unaligned access
3315                          * errors on finicky architectures.  We don't
3316                          * ensure that the sense data is pointer aligned.
3317                          */
3318                         bcopy(&csio->sense_data, &sense, 
3319                               sizeof(struct scsi_sense_data *));
3320                 }
3321         } else {
3322                 /*
3323                  * If the physical sense flag is set, but the sense pointer
3324                  * is not also set, we assume that the user is an idiot and
3325                  * return.  (Well, okay, it could be that somehow, the
3326                  * entire csio is physical, but we would have probably core
3327                  * dumped on one of the bogus pointer deferences above
3328                  * already.)
3329                  */
3330                 if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
3331 #ifdef _KERNEL
3332                         xpt_free_ccb((union ccb*)cgd);
3333 #endif /* _KERNEL/!_KERNEL */
3334                         return(-1);
3335                 } else
3336                         sense = &csio->sense_data;
3337         }
3338
3339
3340         sbuf_cat(sb, path_str);
3341
3342         error_code = sense->error_code & SSD_ERRCODE;
3343         sense_key = sense->flags & SSD_KEY;
3344
3345         sbuf_printf(sb, "SCSI sense: ");
3346         switch (error_code) {
3347         case SSD_DEFERRED_ERROR:
3348                 sbuf_printf(sb, "Deferred error: ");
3349
3350                 /* FALLTHROUGH */
3351         case SSD_CURRENT_ERROR:
3352         {
3353                 const char *sense_key_desc;
3354                 const char *asc_desc;
3355
3356                 asc = (sense->extra_len >= 5) ? sense->add_sense_code : 0;
3357                 ascq = (sense->extra_len >= 6) ? sense->add_sense_code_qual : 0;
3358                 scsi_sense_desc(sense_key, asc, ascq, inq_data,
3359                                 &sense_key_desc, &asc_desc);
3360                 sbuf_cat(sb, sense_key_desc);
3361
3362                 info = scsi_4btoul(sense->info);
3363                 
3364                 if (sense->error_code & SSD_ERRCODE_VALID) {
3365
3366                         switch (sense_key) {
3367                         case SSD_KEY_NOT_READY:
3368                         case SSD_KEY_ILLEGAL_REQUEST:
3369                         case SSD_KEY_UNIT_ATTENTION:
3370                         case SSD_KEY_DATA_PROTECT:
3371                                 break;
3372                         case SSD_KEY_BLANK_CHECK:
3373                                 sbuf_printf(sb, " req sz: %d (decimal)", info);
3374                                 break;
3375                         default:
3376                                 if (info) {
3377                                         if (sense->flags & SSD_ILI) {
3378                                                 sbuf_printf(sb, " ILI (length "
3379                                                         "mismatch): %d", info);
3380                         
3381                                         } else {
3382                                                 sbuf_printf(sb, " info:%x", 
3383                                                             info);
3384                                         }
3385                                 }
3386                         }
3387                 } else if (info) {
3388                         sbuf_printf(sb, " info?:%x", info);
3389                 }
3390
3391                 if (sense->extra_len >= 4) {
3392                         if (bcmp(sense->cmd_spec_info, "\0\0\0\0", 4)) {
3393                                 sbuf_printf(sb, " csi:%x,%x,%x,%x",
3394                                             sense->cmd_spec_info[0],
3395                                             sense->cmd_spec_info[1],
3396                                             sense->cmd_spec_info[2],
3397                                             sense->cmd_spec_info[3]);
3398                         }
3399                 }
3400
3401                 sbuf_printf(sb, " asc:%x,%x (%s)", asc, ascq, asc_desc);
3402
3403                 if (sense->extra_len >= 7 && sense->fru) {
3404                         sbuf_printf(sb, " field replaceable unit: %x", 
3405                                     sense->fru);
3406                 }
3407
3408                 if ((sense->extra_len >= 10)
3409                  && (sense->sense_key_spec[0] & SSD_SCS_VALID) != 0) {
3410                         switch(sense_key) {
3411                         case SSD_KEY_ILLEGAL_REQUEST: {
3412                                 int bad_command;
3413                                 char tmpstr2[40];
3414
3415                                 if (sense->sense_key_spec[0] & 0x40)
3416                                         bad_command = 1;
3417                                 else
3418                                         bad_command = 0;
3419
3420                                 tmpstr2[0] = '\0';
3421
3422                                 /* Bit pointer is valid */
3423                                 if (sense->sense_key_spec[0] & 0x08)
3424                                         snprintf(tmpstr2, sizeof(tmpstr2),
3425                                                  "bit %d ",
3426                                                 sense->sense_key_spec[0] & 0x7);
3427                                 sbuf_printf(sb, ": %s byte %d %sis invalid",
3428                                             bad_command ? "Command" : "Data",
3429                                             scsi_2btoul(
3430                                             &sense->sense_key_spec[1]),
3431                                             tmpstr2);
3432                                 break;
3433                         }
3434                         case SSD_KEY_RECOVERED_ERROR:
3435                         case SSD_KEY_HARDWARE_ERROR:
3436                         case SSD_KEY_MEDIUM_ERROR:
3437                                 sbuf_printf(sb, " actual retry count: %d",
3438                                             scsi_2btoul(
3439                                             &sense->sense_key_spec[1]));
3440                                 break;
3441                         default:
3442                                 sbuf_printf(sb, " sks:%#x,%#x", 
3443                                             sense->sense_key_spec[0],
3444                                             scsi_2btoul(
3445                                             &sense->sense_key_spec[1]));
3446                                 break;
3447                         }
3448                 }
3449                 break;
3450
3451         }
3452         default:
3453                 sbuf_printf(sb, "Error code 0x%x", sense->error_code);
3454                 if (sense->error_code & SSD_ERRCODE_VALID) {
3455                         sbuf_printf(sb, " at block no. %d (decimal)",
3456                                     info = scsi_4btoul(sense->info));
3457                 }
3458         }
3459
3460         sbuf_printf(sb, "\n");
3461
3462 #ifdef _KERNEL
3463         xpt_free_ccb((union ccb*)cgd);
3464 #endif /* _KERNEL/!_KERNEL */
3465         return(0);
3466 }
3467
3468
3469
3470 #ifdef _KERNEL
3471 char *
3472 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
3473 #else /* !_KERNEL */
3474 char *
3475 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
3476                   char *str, int str_len)
3477 #endif /* _KERNEL/!_KERNEL */
3478 {
3479         struct sbuf sb;
3480
3481         sbuf_new(&sb, str, str_len, 0);
3482
3483 #ifdef _KERNEL
3484         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3485 #else /* !_KERNEL */
3486         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3487 #endif /* _KERNEL/!_KERNEL */
3488
3489         sbuf_finish(&sb);
3490
3491         return(sbuf_data(&sb));
3492 }
3493
3494 #ifdef _KERNEL
3495 void 
3496 scsi_sense_print(struct ccb_scsiio *csio)
3497 {
3498         struct sbuf sb;
3499         char str[512];
3500
3501         sbuf_new(&sb, str, sizeof(str), 0);
3502
3503         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
3504
3505         sbuf_finish(&sb);
3506
3507         printf("%s", sbuf_data(&sb));
3508 }
3509
3510 #else /* !_KERNEL */
3511 void
3512 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio, 
3513                  FILE *ofile)
3514 {
3515         struct sbuf sb;
3516         char str[512];
3517
3518         if ((device == NULL) || (csio == NULL) || (ofile == NULL))
3519                 return;
3520
3521         sbuf_new(&sb, str, sizeof(str), 0);
3522
3523         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
3524
3525         sbuf_finish(&sb);
3526
3527         fprintf(ofile, "%s", sbuf_data(&sb));
3528 }
3529
3530 #endif /* _KERNEL/!_KERNEL */
3531
3532 /*
3533  * This function currently requires at least 36 bytes, or
3534  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
3535  * function needs more or less data in the future, another length should be
3536  * defined in scsi_all.h to indicate the minimum amount of data necessary
3537  * for this routine to function properly.
3538  */
3539 void
3540 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
3541 {
3542         u_int8_t type;
3543         char *dtype, *qtype;
3544         char vendor[16], product[48], revision[16], rstr[4];
3545
3546         type = SID_TYPE(inq_data);
3547
3548         /*
3549          * Figure out basic device type and qualifier.
3550          */
3551         if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
3552                 qtype = "(vendor-unique qualifier)";
3553         } else {
3554                 switch (SID_QUAL(inq_data)) {
3555                 case SID_QUAL_LU_CONNECTED:
3556                         qtype = "";
3557                         break;
3558
3559                 case SID_QUAL_LU_OFFLINE:
3560                         qtype = "(offline)";
3561                         break;
3562
3563                 case SID_QUAL_RSVD:
3564                         qtype = "(reserved qualifier)";
3565                         break;
3566                 default:
3567                 case SID_QUAL_BAD_LU:
3568                         qtype = "(LUN not supported)";
3569                         break;
3570                 }
3571         }
3572
3573         switch (type) {
3574         case T_DIRECT:
3575                 dtype = "Direct Access";
3576                 break;
3577         case T_SEQUENTIAL:
3578                 dtype = "Sequential Access";
3579                 break;
3580         case T_PRINTER:
3581                 dtype = "Printer";
3582                 break;
3583         case T_PROCESSOR:
3584                 dtype = "Processor";
3585                 break;
3586         case T_WORM:
3587                 dtype = "WORM";
3588                 break;
3589         case T_CDROM:
3590                 dtype = "CD-ROM";
3591                 break;
3592         case T_SCANNER:
3593                 dtype = "Scanner";
3594                 break;
3595         case T_OPTICAL:
3596                 dtype = "Optical";
3597                 break;
3598         case T_CHANGER:
3599                 dtype = "Changer";
3600                 break;
3601         case T_COMM:
3602                 dtype = "Communication";
3603                 break;
3604         case T_STORARRAY:
3605                 dtype = "Storage Array";
3606                 break;
3607         case T_ENCLOSURE:
3608                 dtype = "Enclosure Services";
3609                 break;
3610         case T_RBC:
3611                 dtype = "Simplified Direct Access";
3612                 break;
3613         case T_OCRW:
3614                 dtype = "Optical Card Read/Write";
3615                 break;
3616         case T_OSD:
3617                 dtype = "Object-Based Storage";
3618                 break;
3619         case T_ADC:
3620                 dtype = "Automation/Drive Interface";
3621                 break;
3622         case T_NODEVICE:
3623                 dtype = "Uninstalled";
3624                 break;
3625         default:
3626                 dtype = "unknown";
3627                 break;
3628         }
3629
3630         cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
3631                    sizeof(vendor));
3632         cam_strvis(product, inq_data->product, sizeof(inq_data->product),
3633                    sizeof(product));
3634         cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
3635                    sizeof(revision));
3636
3637         if (SID_ANSI_REV(inq_data) == SCSI_REV_CCS)
3638                 bcopy("CCS", rstr, 4);
3639         else
3640                 snprintf(rstr, sizeof (rstr), "%d", SID_ANSI_REV(inq_data));
3641         printf("<%s %s %s> %s %s SCSI-%s device %s\n",
3642                vendor, product, revision,
3643                SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
3644                dtype, rstr, qtype);
3645 }
3646
3647 /*
3648  * Table of syncrates that don't follow the "divisible by 4"
3649  * rule. This table will be expanded in future SCSI specs.
3650  */
3651 static struct {
3652         u_int period_factor;
3653         u_int period;   /* in 100ths of ns */
3654 } scsi_syncrates[] = {
3655         { 0x08, 625 },  /* FAST-160 */
3656         { 0x09, 1250 }, /* FAST-80 */
3657         { 0x0a, 2500 }, /* FAST-40 40MHz */
3658         { 0x0b, 3030 }, /* FAST-40 33MHz */
3659         { 0x0c, 5000 }  /* FAST-20 */
3660 };
3661
3662 /*
3663  * Return the frequency in kHz corresponding to the given
3664  * sync period factor.
3665  */
3666 u_int
3667 scsi_calc_syncsrate(u_int period_factor)
3668 {
3669         int i;
3670         int num_syncrates;
3671
3672         /*
3673          * It's a bug if period is zero, but if it is anyway, don't
3674          * die with a divide fault- instead return something which
3675          * 'approximates' async
3676          */
3677         if (period_factor == 0) {
3678                 return (3300);
3679         }
3680
3681         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3682         /* See if the period is in the "exception" table */
3683         for (i = 0; i < num_syncrates; i++) {
3684
3685                 if (period_factor == scsi_syncrates[i].period_factor) {
3686                         /* Period in kHz */
3687                         return (100000000 / scsi_syncrates[i].period);
3688                 }
3689         }
3690
3691         /*
3692          * Wasn't in the table, so use the standard
3693          * 4 times conversion.
3694          */
3695         return (10000000 / (period_factor * 4 * 10));
3696 }
3697
3698 /*
3699  * Return the SCSI sync parameter that corresponsd to
3700  * the passed in period in 10ths of ns.
3701  */
3702 u_int
3703 scsi_calc_syncparam(u_int period)
3704 {
3705         int i;
3706         int num_syncrates;
3707
3708         if (period == 0)
3709                 return (~0);    /* Async */
3710
3711         /* Adjust for exception table being in 100ths. */
3712         period *= 10;
3713         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
3714         /* See if the period is in the "exception" table */
3715         for (i = 0; i < num_syncrates; i++) {
3716
3717                 if (period <= scsi_syncrates[i].period) {
3718                         /* Period in 100ths of ns */
3719                         return (scsi_syncrates[i].period_factor);
3720                 }
3721         }
3722
3723         /*
3724          * Wasn't in the table, so use the standard
3725          * 1/4 period in ns conversion.
3726          */
3727         return (period/400);
3728 }
3729
3730 void
3731 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
3732                      void (*cbfcnp)(struct cam_periph *, union ccb *),
3733                      u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
3734 {
3735         struct scsi_test_unit_ready *scsi_cmd;
3736
3737         cam_fill_csio(csio,
3738                       retries,
3739                       cbfcnp,
3740                       CAM_DIR_NONE,
3741                       tag_action,
3742                       /*data_ptr*/NULL,
3743                       /*dxfer_len*/0,
3744                       sense_len,
3745                       sizeof(*scsi_cmd),
3746                       timeout);
3747
3748         scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
3749         bzero(scsi_cmd, sizeof(*scsi_cmd));
3750         scsi_cmd->opcode = TEST_UNIT_READY;
3751 }
3752
3753 void
3754 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
3755                    void (*cbfcnp)(struct cam_periph *, union ccb *),
3756                    void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
3757                    u_int8_t sense_len, u_int32_t timeout)
3758 {
3759         struct scsi_request_sense *scsi_cmd;
3760
3761         cam_fill_csio(csio,
3762                       retries,
3763                       cbfcnp,
3764                       CAM_DIR_IN,
3765                       tag_action,
3766                       data_ptr,
3767                       dxfer_len,
3768                       sense_len,
3769                       sizeof(*scsi_cmd),
3770                       timeout);
3771
3772         scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
3773         bzero(scsi_cmd, sizeof(*scsi_cmd));
3774         scsi_cmd->opcode = REQUEST_SENSE;
3775         scsi_cmd->length = dxfer_len;
3776 }
3777
3778 void
3779 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
3780              void (*cbfcnp)(struct cam_periph *, union ccb *),
3781              u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
3782              int evpd, u_int8_t page_code, u_int8_t sense_len,
3783              u_int32_t timeout)
3784 {
3785         struct scsi_inquiry *scsi_cmd;
3786
3787         cam_fill_csio(csio,
3788                       retries,
3789                       cbfcnp,
3790                       /*flags*/CAM_DIR_IN,
3791                       tag_action,
3792                       /*data_ptr*/inq_buf,
3793                       /*dxfer_len*/inq_len,
3794                       sense_len,
3795                       sizeof(*scsi_cmd),
3796                       timeout);
3797
3798         scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
3799         bzero(scsi_cmd, sizeof(*scsi_cmd));
3800         scsi_cmd->opcode = INQUIRY;
3801         if (evpd) {
3802                 scsi_cmd->byte2 |= SI_EVPD;
3803                 scsi_cmd->page_code = page_code;                
3804         }
3805         /*
3806          * A 'transfer units' count of 256 is coded as
3807          * zero for all commands with a single byte count
3808          * field. 
3809          */
3810         if (inq_len == 256)
3811                 inq_len = 0;
3812         scsi_cmd->length = inq_len;
3813 }
3814
3815 void
3816 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
3817                 void (*cbfcnp)(struct cam_periph *, union ccb *),
3818                 u_int8_t tag_action, int dbd, u_int8_t page_code,
3819                 u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3820                 u_int8_t sense_len, u_int32_t timeout)
3821 {
3822
3823         scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
3824                             page_code, page, param_buf, param_len, 0,
3825                             sense_len, timeout);
3826 }
3827
3828 void
3829 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
3830                     void (*cbfcnp)(struct cam_periph *, union ccb *),
3831                     u_int8_t tag_action, int dbd, u_int8_t page_code,
3832                     u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
3833                     int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
3834 {
3835         u_int8_t cdb_len;
3836
3837         /*
3838          * Use the smallest possible command to perform the operation.
3839          */
3840         if ((param_len < 256)
3841          && (minimum_cmd_size < 10)) {
3842                 /*
3843                  * We can fit in a 6 byte cdb.
3844                  */
3845                 struct scsi_mode_sense_6 *scsi_cmd;
3846
3847                 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
3848                 bzero(scsi_cmd, sizeof(*scsi_cmd));
3849                 scsi_cmd->opcode = MODE_SENSE_6;
3850                 if (dbd != 0)
3851                         scsi_cmd->byte2 |= SMS_DBD;
3852                 scsi_cmd->page = page_code | page;
3853                 scsi_cmd->length = param_len;
3854                 cdb_len = sizeof(*scsi_cmd);
3855         } else {
3856                 /*
3857                  * Need a 10 byte cdb.
3858                  */
3859                 struct scsi_mode_sense_10 *scsi_cmd;
3860
3861                 scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
3862                 bzero(scsi_cmd, sizeof(*scsi_cmd));
3863                 scsi_cmd->opcode = MODE_SENSE_10;
3864                 if (dbd != 0)
3865                         scsi_cmd->byte2 |= SMS_DBD;
3866                 scsi_cmd->page = page_code | page;
3867                 scsi_ulto2b(param_len, scsi_cmd->length);
3868                 cdb_len = sizeof(*scsi_cmd);
3869         }
3870         cam_fill_csio(csio,
3871                       retries,
3872                       cbfcnp,
3873                       CAM_DIR_IN,
3874                       tag_action,
3875                       param_buf,
3876                       param_len,
3877                       sense_len,
3878                       cdb_len,
3879                       timeout);
3880 }
3881
3882 void
3883 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
3884                  void (*cbfcnp)(struct cam_periph *, union ccb *),
3885                  u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3886                  u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3887                  u_int32_t timeout)
3888 {
3889         scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
3890                              scsi_page_fmt, save_pages, param_buf,
3891                              param_len, 0, sense_len, timeout);
3892 }
3893
3894 void
3895 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
3896                      void (*cbfcnp)(struct cam_periph *, union ccb *),
3897                      u_int8_t tag_action, int scsi_page_fmt, int save_pages,
3898                      u_int8_t *param_buf, u_int32_t param_len,
3899                      int minimum_cmd_size, u_int8_t sense_len,
3900                      u_int32_t timeout)
3901 {
3902         u_int8_t cdb_len;
3903
3904         /*
3905          * Use the smallest possible command to perform the operation.
3906          */
3907         if ((param_len < 256)
3908          && (minimum_cmd_size < 10)) {
3909                 /*
3910                  * We can fit in a 6 byte cdb.
3911                  */
3912                 struct scsi_mode_select_6 *scsi_cmd;
3913
3914                 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
3915                 bzero(scsi_cmd, sizeof(*scsi_cmd));
3916                 scsi_cmd->opcode = MODE_SELECT_6;
3917                 if (scsi_page_fmt != 0)
3918                         scsi_cmd->byte2 |= SMS_PF;
3919                 if (save_pages != 0)
3920                         scsi_cmd->byte2 |= SMS_SP;
3921                 scsi_cmd->length = param_len;
3922                 cdb_len = sizeof(*scsi_cmd);
3923         } else {
3924                 /*
3925                  * Need a 10 byte cdb.
3926                  */
3927                 struct scsi_mode_select_10 *scsi_cmd;
3928
3929                 scsi_cmd =
3930                     (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
3931                 bzero(scsi_cmd, sizeof(*scsi_cmd));
3932                 scsi_cmd->opcode = MODE_SELECT_10;
3933                 if (scsi_page_fmt != 0)
3934                         scsi_cmd->byte2 |= SMS_PF;
3935                 if (save_pages != 0)
3936                         scsi_cmd->byte2 |= SMS_SP;
3937                 scsi_ulto2b(param_len, scsi_cmd->length);
3938                 cdb_len = sizeof(*scsi_cmd);
3939         }
3940         cam_fill_csio(csio,
3941                       retries,
3942                       cbfcnp,
3943                       CAM_DIR_OUT,
3944                       tag_action,
3945                       param_buf,
3946                       param_len,
3947                       sense_len,
3948                       cdb_len,
3949                       timeout);
3950 }
3951
3952 void
3953 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
3954                void (*cbfcnp)(struct cam_periph *, union ccb *),
3955                u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
3956                int save_pages, int ppc, u_int32_t paramptr,
3957                u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
3958                u_int32_t timeout)
3959 {
3960         struct scsi_log_sense *scsi_cmd;
3961         u_int8_t cdb_len;
3962
3963         scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
3964         bzero(scsi_cmd, sizeof(*scsi_cmd));
3965         scsi_cmd->opcode = LOG_SENSE;
3966         scsi_cmd->page = page_code | page;
3967         if (save_pages != 0)
3968                 scsi_cmd->byte2 |= SLS_SP;
3969         if (ppc != 0)
3970                 scsi_cmd->byte2 |= SLS_PPC;
3971         scsi_ulto2b(paramptr, scsi_cmd->paramptr);
3972         scsi_ulto2b(param_len, scsi_cmd->length);
3973         cdb_len = sizeof(*scsi_cmd);
3974
3975         cam_fill_csio(csio,
3976                       retries,
3977                       cbfcnp,
3978                       /*flags*/CAM_DIR_IN,
3979                       tag_action,
3980                       /*data_ptr*/param_buf,
3981                       /*dxfer_len*/param_len,
3982                       sense_len,
3983                       cdb_len,
3984                       timeout);
3985 }
3986
3987 void
3988 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
3989                 void (*cbfcnp)(struct cam_periph *, union ccb *),
3990                 u_int8_t tag_action, u_int8_t page_code, int save_pages,
3991                 int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
3992                 u_int8_t sense_len, u_int32_t timeout)
3993 {
3994         struct scsi_log_select *scsi_cmd;
3995         u_int8_t cdb_len;
3996
3997         scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
3998         bzero(scsi_cmd, sizeof(*scsi_cmd));
3999         scsi_cmd->opcode = LOG_SELECT;
4000         scsi_cmd->page = page_code & SLS_PAGE_CODE;
4001         if (save_pages != 0)
4002                 scsi_cmd->byte2 |= SLS_SP;
4003         if (pc_reset != 0)
4004                 scsi_cmd->byte2 |= SLS_PCR;
4005         scsi_ulto2b(param_len, scsi_cmd->length);
4006         cdb_len = sizeof(*scsi_cmd);
4007
4008         cam_fill_csio(csio,
4009                       retries,
4010                       cbfcnp,
4011                       /*flags*/CAM_DIR_OUT,
4012                       tag_action,
4013                       /*data_ptr*/param_buf,
4014                       /*dxfer_len*/param_len,
4015                       sense_len,
4016                       cdb_len,
4017                       timeout);
4018 }
4019
4020 /*
4021  * Prevent or allow the user to remove the media
4022  */
4023 void
4024 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
4025              void (*cbfcnp)(struct cam_periph *, union ccb *),
4026              u_int8_t tag_action, u_int8_t action,
4027              u_int8_t sense_len, u_int32_t timeout)
4028 {
4029         struct scsi_prevent *scsi_cmd;
4030
4031         cam_fill_csio(csio,
4032                       retries,
4033                       cbfcnp,
4034                       /*flags*/CAM_DIR_NONE,
4035                       tag_action,
4036                       /*data_ptr*/NULL,
4037                       /*dxfer_len*/0,
4038                       sense_len,
4039                       sizeof(*scsi_cmd),
4040                       timeout);
4041
4042         scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
4043         bzero(scsi_cmd, sizeof(*scsi_cmd));
4044         scsi_cmd->opcode = PREVENT_ALLOW;
4045         scsi_cmd->how = action;
4046 }
4047
4048 /* XXX allow specification of address and PMI bit and LBA */
4049 void
4050 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
4051                    void (*cbfcnp)(struct cam_periph *, union ccb *),
4052                    u_int8_t tag_action,
4053                    struct scsi_read_capacity_data *rcap_buf,
4054                    u_int8_t sense_len, u_int32_t timeout)
4055 {
4056         struct scsi_read_capacity *scsi_cmd;
4057
4058         cam_fill_csio(csio,
4059                       retries,
4060                       cbfcnp,
4061                       /*flags*/CAM_DIR_IN,
4062                       tag_action,
4063                       /*data_ptr*/(u_int8_t *)rcap_buf,
4064                       /*dxfer_len*/sizeof(*rcap_buf),
4065                       sense_len,
4066                       sizeof(*scsi_cmd),
4067                       timeout);
4068
4069         scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
4070         bzero(scsi_cmd, sizeof(*scsi_cmd));
4071         scsi_cmd->opcode = READ_CAPACITY;
4072 }
4073
4074 void
4075 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
4076                       void (*cbfcnp)(struct cam_periph *, union ccb *),
4077                       uint8_t tag_action, uint64_t lba, int reladr, int pmi,
4078                       struct scsi_read_capacity_data_long *rcap_buf,
4079                       uint8_t sense_len, uint32_t timeout)
4080 {
4081         struct scsi_read_capacity_16 *scsi_cmd;
4082
4083         
4084         cam_fill_csio(csio,
4085                       retries,
4086                       cbfcnp,
4087                       /*flags*/CAM_DIR_IN,
4088                       tag_action,
4089                       /*data_ptr*/(u_int8_t *)rcap_buf,
4090                       /*dxfer_len*/sizeof(*rcap_buf),
4091                       sense_len,
4092                       sizeof(*scsi_cmd),
4093                       timeout);
4094         scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
4095         bzero(scsi_cmd, sizeof(*scsi_cmd));
4096         scsi_cmd->opcode = SERVICE_ACTION_IN;
4097         scsi_cmd->service_action = SRC16_SERVICE_ACTION;
4098         scsi_u64to8b(lba, scsi_cmd->addr);
4099         scsi_ulto4b(sizeof(*rcap_buf), scsi_cmd->alloc_len);
4100         if (pmi)
4101                 reladr |= SRC16_PMI;
4102         if (reladr)
4103                 reladr |= SRC16_RELADR;
4104 }
4105
4106 void
4107 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
4108                  void (*cbfcnp)(struct cam_periph *, union ccb *),
4109                  u_int8_t tag_action, u_int8_t select_report,
4110                  struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
4111                  u_int8_t sense_len, u_int32_t timeout)
4112 {
4113         struct scsi_report_luns *scsi_cmd;
4114
4115         cam_fill_csio(csio,
4116                       retries,
4117                       cbfcnp,
4118                       /*flags*/CAM_DIR_IN,
4119                       tag_action,
4120                       /*data_ptr*/(u_int8_t *)rpl_buf,
4121                       /*dxfer_len*/alloc_len,
4122                       sense_len,
4123                       sizeof(*scsi_cmd),
4124                       timeout);
4125         scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
4126         bzero(scsi_cmd, sizeof(*scsi_cmd));
4127         scsi_cmd->opcode = REPORT_LUNS;
4128         scsi_cmd->select_report = select_report;
4129         scsi_ulto4b(alloc_len, scsi_cmd->length);
4130 }
4131
4132 void
4133 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
4134                  void (*cbfcnp)(struct cam_periph *, union ccb *),
4135                  u_int8_t tag_action, u_int8_t pdf,
4136                  void *buf, u_int32_t alloc_len,
4137                  u_int8_t sense_len, u_int32_t timeout)
4138 {
4139         struct scsi_target_group *scsi_cmd;
4140
4141         cam_fill_csio(csio,
4142                       retries,
4143                       cbfcnp,
4144                       /*flags*/CAM_DIR_IN,
4145                       tag_action,
4146                       /*data_ptr*/(u_int8_t *)buf,
4147                       /*dxfer_len*/alloc_len,
4148                       sense_len,
4149                       sizeof(*scsi_cmd),
4150                       timeout);
4151         scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
4152         bzero(scsi_cmd, sizeof(*scsi_cmd));
4153         scsi_cmd->opcode = MAINTENANCE_IN;
4154         scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
4155         scsi_ulto4b(alloc_len, scsi_cmd->length);
4156 }
4157
4158 void
4159 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
4160                  void (*cbfcnp)(struct cam_periph *, union ccb *),
4161                  u_int8_t tag_action, void *buf, u_int32_t alloc_len,
4162                  u_int8_t sense_len, u_int32_t timeout)
4163 {
4164         struct scsi_target_group *scsi_cmd;
4165
4166         cam_fill_csio(csio,
4167                       retries,
4168                       cbfcnp,
4169                       /*flags*/CAM_DIR_OUT,
4170                       tag_action,
4171                       /*data_ptr*/(u_int8_t *)buf,
4172                       /*dxfer_len*/alloc_len,
4173                       sense_len,
4174                       sizeof(*scsi_cmd),
4175                       timeout);
4176         scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
4177         bzero(scsi_cmd, sizeof(*scsi_cmd));
4178         scsi_cmd->opcode = MAINTENANCE_OUT;
4179         scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
4180         scsi_ulto4b(alloc_len, scsi_cmd->length);
4181 }
4182
4183 /*
4184  * Syncronize the media to the contents of the cache for
4185  * the given lba/count pair.  Specifying 0/0 means sync
4186  * the whole cache.
4187  */
4188 void
4189 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
4190                        void (*cbfcnp)(struct cam_periph *, union ccb *),
4191                        u_int8_t tag_action, u_int32_t begin_lba,
4192                        u_int16_t lb_count, u_int8_t sense_len,
4193                        u_int32_t timeout)
4194 {
4195         struct scsi_sync_cache *scsi_cmd;
4196
4197         cam_fill_csio(csio,
4198                       retries,
4199                       cbfcnp,
4200                       /*flags*/CAM_DIR_NONE,
4201                       tag_action,
4202                       /*data_ptr*/NULL,
4203                       /*dxfer_len*/0,
4204                       sense_len,
4205                       sizeof(*scsi_cmd),
4206                       timeout);
4207
4208         scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
4209         bzero(scsi_cmd, sizeof(*scsi_cmd));
4210         scsi_cmd->opcode = SYNCHRONIZE_CACHE;
4211         scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
4212         scsi_ulto2b(lb_count, scsi_cmd->lb_count);
4213 }
4214
4215 void
4216 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
4217                 void (*cbfcnp)(struct cam_periph *, union ccb *),
4218                 u_int8_t tag_action, int readop, u_int8_t byte2,
4219                 int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
4220                 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4221                 u_int32_t timeout)
4222 {
4223         u_int8_t cdb_len;
4224         /*
4225          * Use the smallest possible command to perform the operation
4226          * as some legacy hardware does not support the 10 byte commands.
4227          * If any of the bits in byte2 is set, we have to go with a larger
4228          * command.
4229          */
4230         if ((minimum_cmd_size < 10)
4231          && ((lba & 0x1fffff) == lba)
4232          && ((block_count & 0xff) == block_count)
4233          && (byte2 == 0)) {
4234                 /*
4235                  * We can fit in a 6 byte cdb.
4236                  */
4237                 struct scsi_rw_6 *scsi_cmd;
4238
4239                 scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
4240                 scsi_cmd->opcode = readop ? READ_6 : WRITE_6;
4241                 scsi_ulto3b(lba, scsi_cmd->addr);
4242                 scsi_cmd->length = block_count & 0xff;
4243                 scsi_cmd->control = 0;
4244                 cdb_len = sizeof(*scsi_cmd);
4245
4246                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4247                           ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
4248                            scsi_cmd->addr[1], scsi_cmd->addr[2],
4249                            scsi_cmd->length, dxfer_len));
4250         } else if ((minimum_cmd_size < 12)
4251                 && ((block_count & 0xffff) == block_count)
4252                 && ((lba & 0xffffffff) == lba)) {
4253                 /*
4254                  * Need a 10 byte cdb.
4255                  */
4256                 struct scsi_rw_10 *scsi_cmd;
4257
4258                 scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
4259                 scsi_cmd->opcode = readop ? READ_10 : WRITE_10;
4260                 scsi_cmd->byte2 = byte2;
4261                 scsi_ulto4b(lba, scsi_cmd->addr);
4262                 scsi_cmd->reserved = 0;
4263                 scsi_ulto2b(block_count, scsi_cmd->length);
4264                 scsi_cmd->control = 0;
4265                 cdb_len = sizeof(*scsi_cmd);
4266
4267                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4268                           ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
4269                            scsi_cmd->addr[1], scsi_cmd->addr[2],
4270                            scsi_cmd->addr[3], scsi_cmd->length[0],
4271                            scsi_cmd->length[1], dxfer_len));
4272         } else if ((minimum_cmd_size < 16)
4273                 && ((block_count & 0xffffffff) == block_count)
4274                 && ((lba & 0xffffffff) == lba)) {
4275                 /* 
4276                  * The block count is too big for a 10 byte CDB, use a 12
4277                  * byte CDB.
4278                  */
4279                 struct scsi_rw_12 *scsi_cmd;
4280
4281                 scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
4282                 scsi_cmd->opcode = readop ? READ_12 : WRITE_12;
4283                 scsi_cmd->byte2 = byte2;
4284                 scsi_ulto4b(lba, scsi_cmd->addr);
4285                 scsi_cmd->reserved = 0;
4286                 scsi_ulto4b(block_count, scsi_cmd->length);
4287                 scsi_cmd->control = 0;
4288                 cdb_len = sizeof(*scsi_cmd);
4289
4290                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4291                           ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
4292                            scsi_cmd->addr[1], scsi_cmd->addr[2],
4293                            scsi_cmd->addr[3], scsi_cmd->length[0],
4294                            scsi_cmd->length[1], scsi_cmd->length[2],
4295                            scsi_cmd->length[3], dxfer_len));
4296         } else {
4297                 /*
4298                  * 16 byte CDB.  We'll only get here if the LBA is larger
4299                  * than 2^32, or if the user asks for a 16 byte command.
4300                  */
4301                 struct scsi_rw_16 *scsi_cmd;
4302
4303                 scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
4304                 scsi_cmd->opcode = readop ? READ_16 : WRITE_16;
4305                 scsi_cmd->byte2 = byte2;
4306                 scsi_u64to8b(lba, scsi_cmd->addr);
4307                 scsi_cmd->reserved = 0;
4308                 scsi_ulto4b(block_count, scsi_cmd->length);
4309                 scsi_cmd->control = 0;
4310                 cdb_len = sizeof(*scsi_cmd);
4311         }
4312         cam_fill_csio(csio,
4313                       retries,
4314                       cbfcnp,
4315                       /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
4316                       tag_action,
4317                       data_ptr,
4318                       dxfer_len,
4319                       sense_len,
4320                       cdb_len,
4321                       timeout);
4322 }
4323
4324 void
4325 scsi_write_same(struct ccb_scsiio *csio, u_int32_t retries,
4326                 void (*cbfcnp)(struct cam_periph *, union ccb *),
4327                 u_int8_t tag_action, u_int8_t byte2,
4328                 int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
4329                 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
4330                 u_int32_t timeout)
4331 {
4332         u_int8_t cdb_len;
4333         if ((minimum_cmd_size < 16) &&
4334             ((block_count & 0xffff) == block_count) &&
4335             ((lba & 0xffffffff) == lba)) {
4336                 /*
4337                  * Need a 10 byte cdb.
4338                  */
4339                 struct scsi_write_same_10 *scsi_cmd;
4340
4341                 scsi_cmd = (struct scsi_write_same_10 *)&csio->cdb_io.cdb_bytes;
4342                 scsi_cmd->opcode = WRITE_SAME_10;
4343                 scsi_cmd->byte2 = byte2;
4344                 scsi_ulto4b(lba, scsi_cmd->addr);
4345                 scsi_cmd->group = 0;
4346                 scsi_ulto2b(block_count, scsi_cmd->length);
4347                 scsi_cmd->control = 0;
4348                 cdb_len = sizeof(*scsi_cmd);
4349
4350                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4351                           ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
4352                            scsi_cmd->addr[1], scsi_cmd->addr[2],
4353                            scsi_cmd->addr[3], scsi_cmd->length[0],
4354                            scsi_cmd->length[1], dxfer_len));
4355         } else {
4356                 /*
4357                  * 16 byte CDB.  We'll only get here if the LBA is larger
4358                  * than 2^32, or if the user asks for a 16 byte command.
4359                  */
4360                 struct scsi_write_same_16 *scsi_cmd;
4361
4362                 scsi_cmd = (struct scsi_write_same_16 *)&csio->cdb_io.cdb_bytes;
4363                 scsi_cmd->opcode = WRITE_SAME_16;
4364                 scsi_cmd->byte2 = byte2;
4365                 scsi_u64to8b(lba, scsi_cmd->addr);
4366                 scsi_ulto4b(block_count, scsi_cmd->length);
4367                 scsi_cmd->group = 0;
4368                 scsi_cmd->control = 0;
4369                 cdb_len = sizeof(*scsi_cmd);
4370
4371                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
4372                           ("16byte: %x%x%x%x%x%x%x%x:%x%x%x%x: %d\n",
4373                            scsi_cmd->addr[0], scsi_cmd->addr[1],
4374                            scsi_cmd->addr[2], scsi_cmd->addr[3],
4375                            scsi_cmd->addr[4], scsi_cmd->addr[5],
4376                            scsi_cmd->addr[6], scsi_cmd->addr[7],
4377                            scsi_cmd->length[0], scsi_cmd->length[1],
4378                            scsi_cmd->length[2], scsi_cmd->length[3],
4379                            dxfer_len));
4380         }
4381         cam_fill_csio(csio,
4382                       retries,
4383                       cbfcnp,
4384                       /*flags*/CAM_DIR_OUT,
4385                       tag_action,
4386                       data_ptr,
4387                       dxfer_len,
4388                       sense_len,
4389                       cdb_len,
4390                       timeout);
4391 }
4392
4393 void
4394 scsi_ata_pass_16(struct ccb_scsiio *csio, u_int32_t retries,
4395                  void (*cbfcnp)(struct cam_periph *, union ccb *),
4396                  u_int32_t flags, u_int8_t tag_action,
4397                  u_int8_t protocol, u_int8_t ata_flags, u_int16_t features,
4398                  u_int16_t sector_count, uint64_t lba, u_int8_t command,
4399                  u_int8_t control, u_int8_t *data_ptr, u_int16_t dxfer_len,
4400                  u_int8_t sense_len, u_int32_t timeout)
4401 {
4402         struct ata_pass_16 *ata_cmd;
4403
4404         ata_cmd = (struct ata_pass_16 *)&csio->cdb_io.cdb_bytes;
4405         ata_cmd->opcode = ATA_PASS_16;
4406         ata_cmd->protocol = protocol;
4407         ata_cmd->flags = ata_flags;
4408         ata_cmd->features_ext = features >> 8;
4409         ata_cmd->features = features;
4410         ata_cmd->sector_count_ext = sector_count >> 8;
4411         ata_cmd->sector_count = sector_count;
4412         ata_cmd->lba_low = lba;
4413         ata_cmd->lba_mid = lba >> 8;
4414         ata_cmd->lba_high = lba >> 16;
4415         ata_cmd->device = ATA_DEV_LBA;
4416         if (protocol & AP_EXTEND) {
4417                 ata_cmd->lba_low_ext = lba >> 24;
4418                 ata_cmd->lba_mid_ext = lba >> 32;
4419                 ata_cmd->lba_high_ext = lba >> 40;
4420         } else
4421                 ata_cmd->device |= (lba >> 24) & 0x0f;
4422         ata_cmd->command = command;
4423         ata_cmd->control = control;
4424
4425         cam_fill_csio(csio,
4426                       retries,
4427                       cbfcnp,
4428                       flags,
4429                       tag_action,
4430                       data_ptr,
4431                       dxfer_len,
4432                       sense_len,
4433                       sizeof(*ata_cmd),
4434                       timeout);
4435 }
4436
4437 void
4438 scsi_unmap(struct ccb_scsiio *csio, u_int32_t retries,
4439            void (*cbfcnp)(struct cam_periph *, union ccb *),
4440            u_int8_t tag_action, u_int8_t byte2,
4441            u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
4442            u_int32_t timeout)
4443 {
4444         struct scsi_unmap *scsi_cmd;
4445
4446         scsi_cmd = (struct scsi_unmap *)&csio->cdb_io.cdb_bytes;
4447         scsi_cmd->opcode = UNMAP;
4448         scsi_cmd->byte2 = byte2;
4449         scsi_ulto4b(0, scsi_cmd->reserved);
4450         scsi_cmd->group = 0;
4451         scsi_ulto2b(dxfer_len, scsi_cmd->length);
4452         scsi_cmd->control = 0;
4453
4454         cam_fill_csio(csio,
4455                       retries,
4456                       cbfcnp,
4457                       /*flags*/CAM_DIR_OUT,
4458                       tag_action,
4459                       data_ptr,
4460                       dxfer_len,
4461                       sense_len,
4462                       sizeof(*scsi_cmd),
4463                       timeout);
4464 }
4465
4466 void 
4467 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
4468                 void (*cbfcnp)(struct cam_periph *, union ccb *),
4469                 u_int8_t tag_action, int start, int load_eject,
4470                 int immediate, u_int8_t sense_len, u_int32_t timeout)
4471 {
4472         struct scsi_start_stop_unit *scsi_cmd;
4473         int extra_flags = 0;
4474
4475         scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
4476         bzero(scsi_cmd, sizeof(*scsi_cmd));
4477         scsi_cmd->opcode = START_STOP_UNIT;
4478         if (start != 0) {
4479                 scsi_cmd->how |= SSS_START;
4480                 /* it takes a lot of power to start a drive */
4481                 extra_flags |= CAM_HIGH_POWER;
4482         }
4483         if (load_eject != 0)
4484                 scsi_cmd->how |= SSS_LOEJ;
4485         if (immediate != 0)
4486                 scsi_cmd->byte2 |= SSS_IMMED;
4487
4488         cam_fill_csio(csio,
4489                       retries,
4490                       cbfcnp,
4491                       /*flags*/CAM_DIR_NONE | extra_flags,
4492                       tag_action,
4493                       /*data_ptr*/NULL,
4494                       /*dxfer_len*/0,
4495                       sense_len,
4496                       sizeof(*scsi_cmd),
4497                       timeout);
4498
4499 }
4500
4501
4502 /*      
4503  * Try make as good a match as possible with
4504  * available sub drivers
4505  */
4506 int
4507 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4508 {
4509         struct scsi_inquiry_pattern *entry;
4510         struct scsi_inquiry_data *inq;
4511  
4512         entry = (struct scsi_inquiry_pattern *)table_entry;
4513         inq = (struct scsi_inquiry_data *)inqbuffer;
4514
4515         if (((SID_TYPE(inq) == entry->type)
4516           || (entry->type == T_ANY))
4517          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4518                                    : entry->media_type & SIP_MEDIA_FIXED)
4519          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4520          && (cam_strmatch(inq->product, entry->product,
4521                           sizeof(inq->product)) == 0)
4522          && (cam_strmatch(inq->revision, entry->revision,
4523                           sizeof(inq->revision)) == 0)) {
4524                 return (0);
4525         }
4526         return (-1);
4527 }
4528
4529 /*      
4530  * Try make as good a match as possible with
4531  * available sub drivers
4532  */
4533 int
4534 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
4535 {
4536         struct scsi_static_inquiry_pattern *entry;
4537         struct scsi_inquiry_data *inq;
4538  
4539         entry = (struct scsi_static_inquiry_pattern *)table_entry;
4540         inq = (struct scsi_inquiry_data *)inqbuffer;
4541
4542         if (((SID_TYPE(inq) == entry->type)
4543           || (entry->type == T_ANY))
4544          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
4545                                    : entry->media_type & SIP_MEDIA_FIXED)
4546          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
4547          && (cam_strmatch(inq->product, entry->product,
4548                           sizeof(inq->product)) == 0)
4549          && (cam_strmatch(inq->revision, entry->revision,
4550                           sizeof(inq->revision)) == 0)) {
4551                 return (0);
4552         }
4553         return (-1);
4554 }
4555
4556 #ifdef _KERNEL
4557 static void
4558 init_scsi_delay(void)
4559 {
4560         int delay;
4561
4562         delay = SCSI_DELAY;
4563         TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
4564
4565         if (set_scsi_delay(delay) != 0) {
4566                 printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
4567                 set_scsi_delay(SCSI_DELAY);
4568         }
4569 }
4570 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
4571
4572 static int
4573 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
4574 {
4575         int error, delay;
4576
4577         delay = scsi_delay;
4578         error = sysctl_handle_int(oidp, &delay, 0, req);
4579         if (error != 0 || req->newptr == NULL)
4580                 return (error);
4581         return (set_scsi_delay(delay));
4582 }
4583 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
4584     0, 0, sysctl_scsi_delay, "I",
4585     "Delay to allow devices to settle after a SCSI bus reset (ms)");
4586
4587 static int
4588 set_scsi_delay(int delay)
4589 {
4590         /*
4591          * If someone sets this to 0, we assume that they want the
4592          * minimum allowable bus settle delay.
4593          */
4594         if (delay == 0) {
4595                 printf("cam: using minimum scsi_delay (%dms)\n",
4596                     SCSI_MIN_DELAY);
4597                 delay = SCSI_MIN_DELAY;
4598         }
4599         if (delay < SCSI_MIN_DELAY)
4600                 return (EINVAL);
4601         scsi_delay = delay;
4602         return (0);
4603 }
4604 #endif /* _KERNEL */