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