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