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