]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/scsi/scsi_all.c
MFC r310489: Implement printing forwarded sense data.
[FreeBSD/stable/10.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 #include <sys/ctype.h>
48 #else
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <ctype.h>
54 #endif
55
56 #include <cam/cam.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_queue.h>
59 #include <cam/cam_xpt.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <sys/ata.h>
62 #include <sys/sbuf.h>
63
64 #ifdef _KERNEL
65 #include <cam/cam_periph.h>
66 #include <cam/cam_xpt_sim.h>
67 #include <cam/cam_xpt_periph.h>
68 #include <cam/cam_xpt_internal.h>
69 #else
70 #include <camlib.h>
71 #include <stddef.h>
72
73 #ifndef FALSE
74 #define FALSE   0
75 #endif /* FALSE */
76 #ifndef TRUE
77 #define TRUE    1
78 #endif /* TRUE */
79 #define ERESTART        -1              /* restart syscall */
80 #define EJUSTRETURN     -2              /* don't modify regs, just return */
81 #endif /* !_KERNEL */
82
83 /*
84  * This is the default number of milliseconds we wait for devices to settle
85  * after a SCSI bus reset.
86  */
87 #ifndef SCSI_DELAY
88 #define SCSI_DELAY 2000
89 #endif
90 /*
91  * All devices need _some_ sort of bus settle delay, so we'll set it to
92  * a minimum value of 100ms. Note that this is pertinent only for SPI-
93  * not transport like Fibre Channel or iSCSI where 'delay' is completely
94  * meaningless.
95  */
96 #ifndef SCSI_MIN_DELAY
97 #define SCSI_MIN_DELAY 100
98 #endif
99 /*
100  * Make sure the user isn't using seconds instead of milliseconds.
101  */
102 #if (SCSI_DELAY < SCSI_MIN_DELAY && SCSI_DELAY != 0)
103 #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
104 #endif
105
106 int scsi_delay;
107
108 static int      ascentrycomp(const void *key, const void *member);
109 static int      senseentrycomp(const void *key, const void *member);
110 static void     fetchtableentries(int sense_key, int asc, int ascq,
111                                   struct scsi_inquiry_data *,
112                                   const struct sense_key_table_entry **,
113                                   const struct asc_table_entry **);
114 #ifdef _KERNEL
115 static void     init_scsi_delay(void);
116 static int      sysctl_scsi_delay(SYSCTL_HANDLER_ARGS);
117 static int      set_scsi_delay(int delay);
118 #endif
119
120 #if !defined(SCSI_NO_OP_STRINGS)
121
122 #define D       (1 << T_DIRECT)
123 #define T       (1 << T_SEQUENTIAL)
124 #define L       (1 << T_PRINTER)
125 #define P       (1 << T_PROCESSOR)
126 #define W       (1 << T_WORM)
127 #define R       (1 << T_CDROM)
128 #define O       (1 << T_OPTICAL)
129 #define M       (1 << T_CHANGER)
130 #define A       (1 << T_STORARRAY)
131 #define E       (1 << T_ENCLOSURE)
132 #define B       (1 << T_RBC)
133 #define K       (1 << T_OCRW)
134 #define V       (1 << T_ADC)
135 #define F       (1 << T_OSD)
136 #define S       (1 << T_SCANNER)
137 #define C       (1 << T_COMM)
138
139 #define ALL     (D | T | L | P | W | R | O | M | A | E | B | K | V | F | S | C)
140
141 static struct op_table_entry plextor_cd_ops[] = {
142         { 0xD8, R, "CD-DA READ" }
143 };
144
145 static struct scsi_op_quirk_entry scsi_op_quirk_table[] = {
146         {
147                 /*
148                  * I believe that 0xD8 is the Plextor proprietary command
149                  * to read CD-DA data.  I'm not sure which Plextor CDROM
150                  * models support the command, though.  I know for sure
151                  * that the 4X, 8X, and 12X models do, and presumably the
152                  * 12-20X does.  I don't know about any earlier models,
153                  * though.  If anyone has any more complete information,
154                  * feel free to change this quirk entry.
155                  */
156                 {T_CDROM, SIP_MEDIA_REMOVABLE, "PLEXTOR", "CD-ROM PX*", "*"},
157                 sizeof(plextor_cd_ops)/sizeof(struct op_table_entry),
158                 plextor_cd_ops
159         }
160 };
161
162 static struct op_table_entry scsi_op_codes[] = {
163         /*
164          * From: http://www.t10.org/lists/op-num.txt
165          * Modifications by Kenneth Merry (ken@FreeBSD.ORG)
166          *              and Jung-uk Kim (jkim@FreeBSD.org)
167          *
168          * Note:  order is important in this table, scsi_op_desc() currently
169          * depends on the opcodes in the table being in order to save
170          * search time.
171          * Note:  scanner and comm. devices are carried over from the previous
172          * version because they were removed in the latest spec.
173          */
174         /* File: OP-NUM.TXT
175          *
176          * SCSI Operation Codes
177          * Numeric Sorted Listing
178          * as of  5/26/15
179          *
180          *     D - DIRECT ACCESS DEVICE (SBC-2)                device column key
181          *     .T - SEQUENTIAL ACCESS DEVICE (SSC-2)           -----------------
182          *     . L - PRINTER DEVICE (SSC)                      M = Mandatory
183          *     .  P - PROCESSOR DEVICE (SPC)                   O = Optional
184          *     .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2) V = Vendor spec.
185          *     .  . R - CD/DVE DEVICE (MMC-3)                  Z = Obsolete
186          *     .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
187          *     .  .  .M - MEDIA CHANGER DEVICE (SMC-2)
188          *     .  .  . A - STORAGE ARRAY DEVICE (SCC-2)
189          *     .  .  . .E - ENCLOSURE SERVICES DEVICE (SES)
190          *     .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
191          *     .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
192          *     .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
193          *     .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
194          * OP  DTLPWROMAEBKVF  Description
195          * --  --------------  ---------------------------------------------- */
196         /* 00  MMMMMMMMMMMMMM  TEST UNIT READY */
197         { 0x00, ALL, "TEST UNIT READY" },
198         /* 01   M              REWIND */
199         { 0x01, T, "REWIND" },
200         /* 01  Z V ZZZZ        REZERO UNIT */
201         { 0x01, D | W | R | O | M, "REZERO UNIT" },
202         /* 02  VVVVVV V */
203         /* 03  MMMMMMMMMMOMMM  REQUEST SENSE */
204         { 0x03, ALL, "REQUEST SENSE" },
205         /* 04  M    OO         FORMAT UNIT */
206         { 0x04, D | R | O, "FORMAT UNIT" },
207         /* 04   O              FORMAT MEDIUM */
208         { 0x04, T, "FORMAT MEDIUM" },
209         /* 04    O             FORMAT */
210         { 0x04, L, "FORMAT" },
211         /* 05  VMVVVV V        READ BLOCK LIMITS */
212         { 0x05, T, "READ BLOCK LIMITS" },
213         /* 06  VVVVVV V */
214         /* 07  OVV O OV        REASSIGN BLOCKS */
215         { 0x07, D | W | O, "REASSIGN BLOCKS" },
216         /* 07         O        INITIALIZE ELEMENT STATUS */
217         { 0x07, M, "INITIALIZE ELEMENT STATUS" },
218         /* 08  MOV O OV        READ(6) */
219         { 0x08, D | T | W | O, "READ(6)" },
220         /* 08     O            RECEIVE */
221         { 0x08, P, "RECEIVE" },
222         /* 08                  GET MESSAGE(6) */
223         { 0x08, C, "GET MESSAGE(6)" },
224         /* 09  VVVVVV V */
225         /* 0A  OO  O OV        WRITE(6) */
226         { 0x0A, D | T | W | O, "WRITE(6)" },
227         /* 0A     M            SEND(6) */
228         { 0x0A, P, "SEND(6)" },
229         /* 0A                  SEND MESSAGE(6) */
230         { 0x0A, C, "SEND MESSAGE(6)" },
231         /* 0A    M             PRINT */
232         { 0x0A, L, "PRINT" },
233         /* 0B  Z   ZOZV        SEEK(6) */
234         { 0x0B, D | W | R | O, "SEEK(6)" },
235         /* 0B   O              SET CAPACITY */
236         { 0x0B, T, "SET CAPACITY" },
237         /* 0B    O             SLEW AND PRINT */
238         { 0x0B, L, "SLEW AND PRINT" },
239         /* 0C  VVVVVV V */
240         /* 0D  VVVVVV V */
241         /* 0E  VVVVVV V */
242         /* 0F  VOVVVV V        READ REVERSE(6) */
243         { 0x0F, T, "READ REVERSE(6)" },
244         /* 10  VM VVV          WRITE FILEMARKS(6) */
245         { 0x10, T, "WRITE FILEMARKS(6)" },
246         /* 10    O             SYNCHRONIZE BUFFER */
247         { 0x10, L, "SYNCHRONIZE BUFFER" },
248         /* 11  VMVVVV          SPACE(6) */
249         { 0x11, T, "SPACE(6)" },
250         /* 12  MMMMMMMMMMMMMM  INQUIRY */
251         { 0x12, ALL, "INQUIRY" },
252         /* 13  V VVVV */
253         /* 13   O              VERIFY(6) */
254         { 0x13, T, "VERIFY(6)" },
255         /* 14  VOOVVV          RECOVER BUFFERED DATA */
256         { 0x14, T | L, "RECOVER BUFFERED DATA" },
257         /* 15  OMO O OOOO OO   MODE SELECT(6) */
258         { 0x15, ALL & ~(P | R | B | F), "MODE SELECT(6)" },
259         /* 16  ZZMZO OOOZ O    RESERVE(6) */
260         { 0x16, ALL & ~(R | B | V | F | C), "RESERVE(6)" },
261         /* 16         Z        RESERVE ELEMENT(6) */
262         { 0x16, M, "RESERVE ELEMENT(6)" },
263         /* 17  ZZMZO OOOZ O    RELEASE(6) */
264         { 0x17, ALL & ~(R | B | V | F | C), "RELEASE(6)" },
265         /* 17         Z        RELEASE ELEMENT(6) */
266         { 0x17, M, "RELEASE ELEMENT(6)" },
267         /* 18  ZZZZOZO    Z    COPY */
268         { 0x18, D | T | L | P | W | R | O | K | S, "COPY" },
269         /* 19  VMVVVV          ERASE(6) */
270         { 0x19, T, "ERASE(6)" },
271         /* 1A  OMO O OOOO OO   MODE SENSE(6) */
272         { 0x1A, ALL & ~(P | R | B | F), "MODE SENSE(6)" },
273         /* 1B  O   OOO O MO O  START STOP UNIT */
274         { 0x1B, D | W | R | O | A | B | K | F, "START STOP UNIT" },
275         /* 1B   O          M   LOAD UNLOAD */
276         { 0x1B, T | V, "LOAD UNLOAD" },
277         /* 1B                  SCAN */
278         { 0x1B, S, "SCAN" },
279         /* 1B    O             STOP PRINT */
280         { 0x1B, L, "STOP PRINT" },
281         /* 1B         O        OPEN/CLOSE IMPORT/EXPORT ELEMENT */
282         { 0x1B, M, "OPEN/CLOSE IMPORT/EXPORT ELEMENT" },
283         /* 1C  OOOOO OOOM OOO  RECEIVE DIAGNOSTIC RESULTS */
284         { 0x1C, ALL & ~(R | B), "RECEIVE DIAGNOSTIC RESULTS" },
285         /* 1D  MMMMM MMOM MMM  SEND DIAGNOSTIC */
286         { 0x1D, ALL & ~(R | B), "SEND DIAGNOSTIC" },
287         /* 1E  OO  OOOO   O O  PREVENT ALLOW MEDIUM REMOVAL */
288         { 0x1E, D | T | W | R | O | M | K | F, "PREVENT ALLOW MEDIUM REMOVAL" },
289         /* 1F */
290         /* 20  V   VVV    V */
291         /* 21  V   VVV    V */
292         /* 22  V   VVV    V */
293         /* 23  V   V V    V */
294         /* 23       O          READ FORMAT CAPACITIES */
295         { 0x23, R, "READ FORMAT CAPACITIES" },
296         /* 24  V   VV          SET WINDOW */
297         { 0x24, S, "SET WINDOW" },
298         /* 25  M   M M   M     READ CAPACITY(10) */
299         { 0x25, D | W | O | B, "READ CAPACITY(10)" },
300         /* 25       O          READ CAPACITY */
301         { 0x25, R, "READ CAPACITY" },
302         /* 25             M    READ CARD CAPACITY */
303         { 0x25, K, "READ CARD CAPACITY" },
304         /* 25                  GET WINDOW */
305         { 0x25, S, "GET WINDOW" },
306         /* 26  V   VV */
307         /* 27  V   VV */
308         /* 28  M   MOM   MM    READ(10) */
309         { 0x28, D | W | R | O | B | K | S, "READ(10)" },
310         /* 28                  GET MESSAGE(10) */
311         { 0x28, C, "GET MESSAGE(10)" },
312         /* 29  V   VVO         READ GENERATION */
313         { 0x29, O, "READ GENERATION" },
314         /* 2A  O   MOM   MO    WRITE(10) */
315         { 0x2A, D | W | R | O | B | K, "WRITE(10)" },
316         /* 2A                  SEND(10) */
317         { 0x2A, S, "SEND(10)" },
318         /* 2A                  SEND MESSAGE(10) */
319         { 0x2A, C, "SEND MESSAGE(10)" },
320         /* 2B  Z   OOO    O    SEEK(10) */
321         { 0x2B, D | W | R | O | K, "SEEK(10)" },
322         /* 2B   O              LOCATE(10) */
323         { 0x2B, T, "LOCATE(10)" },
324         /* 2B         O        POSITION TO ELEMENT */
325         { 0x2B, M, "POSITION TO ELEMENT" },
326         /* 2C  V    OO         ERASE(10) */
327         { 0x2C, R | O, "ERASE(10)" },
328         /* 2D        O         READ UPDATED BLOCK */
329         { 0x2D, O, "READ UPDATED BLOCK" },
330         /* 2D  V */
331         /* 2E  O   OOO   MO    WRITE AND VERIFY(10) */
332         { 0x2E, D | W | R | O | B | K, "WRITE AND VERIFY(10)" },
333         /* 2F  O   OOO         VERIFY(10) */
334         { 0x2F, D | W | R | O, "VERIFY(10)" },
335         /* 30  Z   ZZZ         SEARCH DATA HIGH(10) */
336         { 0x30, D | W | R | O, "SEARCH DATA HIGH(10)" },
337         /* 31  Z   ZZZ         SEARCH DATA EQUAL(10) */
338         { 0x31, D | W | R | O, "SEARCH DATA EQUAL(10)" },
339         /* 31                  OBJECT POSITION */
340         { 0x31, S, "OBJECT POSITION" },
341         /* 32  Z   ZZZ         SEARCH DATA LOW(10) */
342         { 0x32, D | W | R | O, "SEARCH DATA LOW(10)" },
343         /* 33  Z   OZO         SET LIMITS(10) */
344         { 0x33, D | W | R | O, "SET LIMITS(10)" },
345         /* 34  O   O O    O    PRE-FETCH(10) */
346         { 0x34, D | W | O | K, "PRE-FETCH(10)" },
347         /* 34   M              READ POSITION */
348         { 0x34, T, "READ POSITION" },
349         /* 34                  GET DATA BUFFER STATUS */
350         { 0x34, S, "GET DATA BUFFER STATUS" },
351         /* 35  O   OOO   MO    SYNCHRONIZE CACHE(10) */
352         { 0x35, D | W | R | O | B | K, "SYNCHRONIZE CACHE(10)" },
353         /* 36  Z   O O    O    LOCK UNLOCK CACHE(10) */
354         { 0x36, D | W | O | K, "LOCK UNLOCK CACHE(10)" },
355         /* 37  O     O         READ DEFECT DATA(10) */
356         { 0x37, D | O, "READ DEFECT DATA(10)" },
357         /* 37         O        INITIALIZE ELEMENT STATUS WITH RANGE */
358         { 0x37, M, "INITIALIZE ELEMENT STATUS WITH RANGE" },
359         /* 38      O O    O    MEDIUM SCAN */
360         { 0x38, W | O | K, "MEDIUM SCAN" },
361         /* 39  ZZZZOZO    Z    COMPARE */
362         { 0x39, D | T | L | P | W | R | O | K | S, "COMPARE" },
363         /* 3A  ZZZZOZO    Z    COPY AND VERIFY */
364         { 0x3A, D | T | L | P | W | R | O | K | S, "COPY AND VERIFY" },
365         /* 3B  OOOOOOOOOOMOOO  WRITE BUFFER */
366         { 0x3B, ALL, "WRITE BUFFER" },
367         /* 3C  OOOOOOOOOO OOO  READ BUFFER */
368         { 0x3C, ALL & ~(B), "READ BUFFER" },
369         /* 3D        O         UPDATE BLOCK */
370         { 0x3D, O, "UPDATE BLOCK" },
371         /* 3E  O   O O         READ LONG(10) */
372         { 0x3E, D | W | O, "READ LONG(10)" },
373         /* 3F  O   O O         WRITE LONG(10) */
374         { 0x3F, D | W | O, "WRITE LONG(10)" },
375         /* 40  ZZZZOZOZ        CHANGE DEFINITION */
376         { 0x40, D | T | L | P | W | R | O | M | S | C, "CHANGE DEFINITION" },
377         /* 41  O               WRITE SAME(10) */
378         { 0x41, D, "WRITE SAME(10)" },
379         /* 42       O          UNMAP */
380         { 0x42, D, "UNMAP" },
381         /* 42       O          READ SUB-CHANNEL */
382         { 0x42, R, "READ SUB-CHANNEL" },
383         /* 43       O          READ TOC/PMA/ATIP */
384         { 0x43, R, "READ TOC/PMA/ATIP" },
385         /* 44   M          M   REPORT DENSITY SUPPORT */
386         { 0x44, T | V, "REPORT DENSITY SUPPORT" },
387         /* 44                  READ HEADER */
388         /* 45       O          PLAY AUDIO(10) */
389         { 0x45, R, "PLAY AUDIO(10)" },
390         /* 46       M          GET CONFIGURATION */
391         { 0x46, R, "GET CONFIGURATION" },
392         /* 47       O          PLAY AUDIO MSF */
393         { 0x47, R, "PLAY AUDIO MSF" },
394         /* 48 */
395         /* 49 */
396         /* 4A       M          GET EVENT STATUS NOTIFICATION */
397         { 0x4A, R, "GET EVENT STATUS NOTIFICATION" },
398         /* 4B       O          PAUSE/RESUME */
399         { 0x4B, R, "PAUSE/RESUME" },
400         /* 4C  OOOOO OOOO OOO  LOG SELECT */
401         { 0x4C, ALL & ~(R | B), "LOG SELECT" },
402         /* 4D  OOOOO OOOO OMO  LOG SENSE */
403         { 0x4D, ALL & ~(R | B), "LOG SENSE" },
404         /* 4E       O          STOP PLAY/SCAN */
405         { 0x4E, R, "STOP PLAY/SCAN" },
406         /* 4F */
407         /* 50  O               XDWRITE(10) */
408         { 0x50, D, "XDWRITE(10)" },
409         /* 51  O               XPWRITE(10) */
410         { 0x51, D, "XPWRITE(10)" },
411         /* 51       O          READ DISC INFORMATION */
412         { 0x51, R, "READ DISC INFORMATION" },
413         /* 52  O               XDREAD(10) */
414         { 0x52, D, "XDREAD(10)" },
415         /* 52       O          READ TRACK INFORMATION */
416         { 0x52, R, "READ TRACK INFORMATION" },
417         /* 53       O          RESERVE TRACK */
418         { 0x53, R, "RESERVE TRACK" },
419         /* 54       O          SEND OPC INFORMATION */
420         { 0x54, R, "SEND OPC INFORMATION" },
421         /* 55  OOO OMOOOOMOMO  MODE SELECT(10) */
422         { 0x55, ALL & ~(P), "MODE SELECT(10)" },
423         /* 56  ZZMZO OOOZ      RESERVE(10) */
424         { 0x56, ALL & ~(R | B | K | V | F | C), "RESERVE(10)" },
425         /* 56         Z        RESERVE ELEMENT(10) */
426         { 0x56, M, "RESERVE ELEMENT(10)" },
427         /* 57  ZZMZO OOOZ      RELEASE(10) */
428         { 0x57, ALL & ~(R | B | K | V | F | C), "RELEASE(10)" },
429         /* 57         Z        RELEASE ELEMENT(10) */
430         { 0x57, M, "RELEASE ELEMENT(10)" },
431         /* 58       O          REPAIR TRACK */
432         { 0x58, R, "REPAIR TRACK" },
433         /* 59 */
434         /* 5A  OOO OMOOOOMOMO  MODE SENSE(10) */
435         { 0x5A, ALL & ~(P), "MODE SENSE(10)" },
436         /* 5B       O          CLOSE TRACK/SESSION */
437         { 0x5B, R, "CLOSE TRACK/SESSION" },
438         /* 5C       O          READ BUFFER CAPACITY */
439         { 0x5C, R, "READ BUFFER CAPACITY" },
440         /* 5D       O          SEND CUE SHEET */
441         { 0x5D, R, "SEND CUE SHEET" },
442         /* 5E  OOOOO OOOO   M  PERSISTENT RESERVE IN */
443         { 0x5E, ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE IN" },
444         /* 5F  OOOOO OOOO   M  PERSISTENT RESERVE OUT */
445         { 0x5F, ALL & ~(R | B | K | V | C), "PERSISTENT RESERVE OUT" },
446         /* 7E  OO   O OOOO O   extended CDB */
447         { 0x7E, D | T | R | M | A | E | B | V, "extended CDB" },
448         /* 7F  O            M  variable length CDB (more than 16 bytes) */
449         { 0x7F, D | F, "variable length CDB (more than 16 bytes)" },
450         /* 80  Z               XDWRITE EXTENDED(16) */
451         { 0x80, D, "XDWRITE EXTENDED(16)" },
452         /* 80   M              WRITE FILEMARKS(16) */
453         { 0x80, T, "WRITE FILEMARKS(16)" },
454         /* 81  Z               REBUILD(16) */
455         { 0x81, D, "REBUILD(16)" },
456         /* 81   O              READ REVERSE(16) */
457         { 0x81, T, "READ REVERSE(16)" },
458         /* 82  Z               REGENERATE(16) */
459         { 0x82, D, "REGENERATE(16)" },
460         /* 83  OOOOO O    OO   EXTENDED COPY */
461         { 0x83, D | T | L | P | W | O | K | V, "EXTENDED COPY" },
462         /* 84  OOOOO O    OO   RECEIVE COPY RESULTS */
463         { 0x84, D | T | L | P | W | O | K | V, "RECEIVE COPY RESULTS" },
464         /* 85  O    O    O     ATA COMMAND PASS THROUGH(16) */
465         { 0x85, D | R | B, "ATA COMMAND PASS THROUGH(16)" },
466         /* 86  OO OO OOOOOOO   ACCESS CONTROL IN */
467         { 0x86, ALL & ~(L | R | F), "ACCESS CONTROL IN" },
468         /* 87  OO OO OOOOOOO   ACCESS CONTROL OUT */
469         { 0x87, ALL & ~(L | R | F), "ACCESS CONTROL OUT" },
470         /*
471          * XXX READ(16)/WRITE(16) were not listed for CD/DVE in op-num.txt
472          * but we had it since r1.40.  Do we really want them?
473          */
474         /* 88  MM  O O   O     READ(16) */
475         { 0x88, D | T | W | O | B, "READ(16)" },
476         /* 89  O               COMPARE AND WRITE*/
477         { 0x89, D, "COMPARE AND WRITE" },
478         /* 8A  OM  O O   O     WRITE(16) */
479         { 0x8A, D | T | W | O | B, "WRITE(16)" },
480         /* 8B  O               ORWRITE */
481         { 0x8B, D, "ORWRITE" },
482         /* 8C  OO  O OO  O M   READ ATTRIBUTE */
483         { 0x8C, D | T | W | O | M | B | V, "READ ATTRIBUTE" },
484         /* 8D  OO  O OO  O O   WRITE ATTRIBUTE */
485         { 0x8D, D | T | W | O | M | B | V, "WRITE ATTRIBUTE" },
486         /* 8E  O   O O   O     WRITE AND VERIFY(16) */
487         { 0x8E, D | W | O | B, "WRITE AND VERIFY(16)" },
488         /* 8F  OO  O O   O     VERIFY(16) */
489         { 0x8F, D | T | W | O | B, "VERIFY(16)" },
490         /* 90  O   O O   O     PRE-FETCH(16) */
491         { 0x90, D | W | O | B, "PRE-FETCH(16)" },
492         /* 91  O   O O   O     SYNCHRONIZE CACHE(16) */
493         { 0x91, D | W | O | B, "SYNCHRONIZE CACHE(16)" },
494         /* 91   O              SPACE(16) */
495         { 0x91, T, "SPACE(16)" },
496         /* 92  Z   O O         LOCK UNLOCK CACHE(16) */
497         { 0x92, D | W | O, "LOCK UNLOCK CACHE(16)" },
498         /* 92   O              LOCATE(16) */
499         { 0x92, T, "LOCATE(16)" },
500         /* 93  O               WRITE SAME(16) */
501         { 0x93, D, "WRITE SAME(16)" },
502         /* 93   M              ERASE(16) */
503         { 0x93, T, "ERASE(16)" },
504         /* 94  O               ZBC OUT */
505         { 0x94, D, "ZBC OUT" },
506         /* 95  O               ZBC OUT */
507         { 0x95, D, "ZBC OUT" },
508         /* 96 */
509         /* 97 */
510         /* 98 */
511         /* 99 */
512         /* 9A  O               WRITE STREAM(16) */
513         { 0x9A, D, "WRITE STREAM(16)" },
514         /* 9B  OOOOOOOOOO OOO  READ BUFFER(16) */
515         { 0x9B, ALL & ~(B) , "READ BUFFER(16)" },
516         /* 9C  O              WRITE ATOMIC(16) */
517         { 0x9C, D, "WRITE ATOMIC(16)" },
518         /* 9D                  SERVICE ACTION BIDIRECTIONAL */
519         { 0x9D, ALL, "SERVICE ACTION BIDIRECTIONAL" },
520         /* XXX KDM ALL for this?  op-num.txt defines it for none.. */
521         /* 9E                  SERVICE ACTION IN(16) */
522         { 0x9E, ALL, "SERVICE ACTION IN(16)" },
523         /* XXX KDM ALL for this?  op-num.txt defines it for ADC.. */
524         /* 9F              M   SERVICE ACTION OUT(16) */
525         { 0x9F, ALL, "SERVICE ACTION OUT(16)" },
526         /* A0  MMOOO OMMM OMO  REPORT LUNS */
527         { 0xA0, ALL & ~(R | B), "REPORT LUNS" },
528         /* A1       O          BLANK */
529         { 0xA1, R, "BLANK" },
530         /* A1  O         O     ATA COMMAND PASS THROUGH(12) */
531         { 0xA1, D | B, "ATA COMMAND PASS THROUGH(12)" },
532         /* A2  OO   O      O   SECURITY PROTOCOL IN */
533         { 0xA2, D | T | R | V, "SECURITY PROTOCOL IN" },
534         /* A3  OOO O OOMOOOM   MAINTENANCE (IN) */
535         { 0xA3, ALL & ~(P | R | F), "MAINTENANCE (IN)" },
536         /* A3       O          SEND KEY */
537         { 0xA3, R, "SEND KEY" },
538         /* A4  OOO O OOOOOOO   MAINTENANCE (OUT) */
539         { 0xA4, ALL & ~(P | R | F), "MAINTENANCE (OUT)" },
540         /* A4       O          REPORT KEY */
541         { 0xA4, R, "REPORT KEY" },
542         /* A5   O  O OM        MOVE MEDIUM */
543         { 0xA5, T | W | O | M, "MOVE MEDIUM" },
544         /* A5       O          PLAY AUDIO(12) */
545         { 0xA5, R, "PLAY AUDIO(12)" },
546         /* A6         O        EXCHANGE MEDIUM */
547         { 0xA6, M, "EXCHANGE MEDIUM" },
548         /* A6       O          LOAD/UNLOAD C/DVD */
549         { 0xA6, R, "LOAD/UNLOAD C/DVD" },
550         /* A7  ZZ  O O         MOVE MEDIUM ATTACHED */
551         { 0xA7, D | T | W | O, "MOVE MEDIUM ATTACHED" },
552         /* A7       O          SET READ AHEAD */
553         { 0xA7, R, "SET READ AHEAD" },
554         /* A8  O   OOO         READ(12) */
555         { 0xA8, D | W | R | O, "READ(12)" },
556         /* A8                  GET MESSAGE(12) */
557         { 0xA8, C, "GET MESSAGE(12)" },
558         /* A9              O   SERVICE ACTION OUT(12) */
559         { 0xA9, V, "SERVICE ACTION OUT(12)" },
560         /* AA  O   OOO         WRITE(12) */
561         { 0xAA, D | W | R | O, "WRITE(12)" },
562         /* AA                  SEND MESSAGE(12) */
563         { 0xAA, C, "SEND MESSAGE(12)" },
564         /* AB       O      O   SERVICE ACTION IN(12) */
565         { 0xAB, R | V, "SERVICE ACTION IN(12)" },
566         /* AC        O         ERASE(12) */
567         { 0xAC, O, "ERASE(12)" },
568         /* AC       O          GET PERFORMANCE */
569         { 0xAC, R, "GET PERFORMANCE" },
570         /* AD       O          READ DVD STRUCTURE */
571         { 0xAD, R, "READ DVD STRUCTURE" },
572         /* AE  O   O O         WRITE AND VERIFY(12) */
573         { 0xAE, D | W | O, "WRITE AND VERIFY(12)" },
574         /* AF  O   OZO         VERIFY(12) */
575         { 0xAF, D | W | R | O, "VERIFY(12)" },
576         /* B0      ZZZ         SEARCH DATA HIGH(12) */
577         { 0xB0, W | R | O, "SEARCH DATA HIGH(12)" },
578         /* B1      ZZZ         SEARCH DATA EQUAL(12) */
579         { 0xB1, W | R | O, "SEARCH DATA EQUAL(12)" },
580         /* B2      ZZZ         SEARCH DATA LOW(12) */
581         { 0xB2, W | R | O, "SEARCH DATA LOW(12)" },
582         /* B3  Z   OZO         SET LIMITS(12) */
583         { 0xB3, D | W | R | O, "SET LIMITS(12)" },
584         /* B4  ZZ  OZO         READ ELEMENT STATUS ATTACHED */
585         { 0xB4, D | T | W | R | O, "READ ELEMENT STATUS ATTACHED" },
586         /* B5  OO   O      O   SECURITY PROTOCOL OUT */
587         { 0xB5, D | T | R | V, "SECURITY PROTOCOL OUT" },
588         /* B5         O        REQUEST VOLUME ELEMENT ADDRESS */
589         { 0xB5, M, "REQUEST VOLUME ELEMENT ADDRESS" },
590         /* B6         O        SEND VOLUME TAG */
591         { 0xB6, M, "SEND VOLUME TAG" },
592         /* B6       O          SET STREAMING */
593         { 0xB6, R, "SET STREAMING" },
594         /* B7  O     O         READ DEFECT DATA(12) */
595         { 0xB7, D | O, "READ DEFECT DATA(12)" },
596         /* B8   O  OZOM        READ ELEMENT STATUS */
597         { 0xB8, T | W | R | O | M, "READ ELEMENT STATUS" },
598         /* B9       O          READ CD MSF */
599         { 0xB9, R, "READ CD MSF" },
600         /* BA  O   O OOMO      REDUNDANCY GROUP (IN) */
601         { 0xBA, D | W | O | M | A | E, "REDUNDANCY GROUP (IN)" },
602         /* BA       O          SCAN */
603         { 0xBA, R, "SCAN" },
604         /* BB  O   O OOOO      REDUNDANCY GROUP (OUT) */
605         { 0xBB, D | W | O | M | A | E, "REDUNDANCY GROUP (OUT)" },
606         /* BB       O          SET CD SPEED */
607         { 0xBB, R, "SET CD SPEED" },
608         /* BC  O   O OOMO      SPARE (IN) */
609         { 0xBC, D | W | O | M | A | E, "SPARE (IN)" },
610         /* BD  O   O OOOO      SPARE (OUT) */
611         { 0xBD, D | W | O | M | A | E, "SPARE (OUT)" },
612         /* BD       O          MECHANISM STATUS */
613         { 0xBD, R, "MECHANISM STATUS" },
614         /* BE  O   O OOMO      VOLUME SET (IN) */
615         { 0xBE, D | W | O | M | A | E, "VOLUME SET (IN)" },
616         /* BE       O          READ CD */
617         { 0xBE, R, "READ CD" },
618         /* BF  O   O OOOO      VOLUME SET (OUT) */
619         { 0xBF, D | W | O | M | A | E, "VOLUME SET (OUT)" },
620         /* BF       O          SEND DVD STRUCTURE */
621         { 0xBF, R, "SEND DVD STRUCTURE" }
622 };
623
624 const char *
625 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
626 {
627         caddr_t match;
628         int i, j;
629         u_int32_t opmask;
630         u_int16_t pd_type;
631         int       num_ops[2];
632         struct op_table_entry *table[2];
633         int num_tables;
634
635         /*
636          * If we've got inquiry data, use it to determine what type of
637          * device we're dealing with here.  Otherwise, assume direct
638          * access.
639          */
640         if (inq_data == NULL) {
641                 pd_type = T_DIRECT;
642                 match = NULL;
643         } else {
644                 pd_type = SID_TYPE(inq_data);
645
646                 match = cam_quirkmatch((caddr_t)inq_data,
647                                        (caddr_t)scsi_op_quirk_table,
648                                        sizeof(scsi_op_quirk_table)/
649                                        sizeof(*scsi_op_quirk_table),
650                                        sizeof(*scsi_op_quirk_table),
651                                        scsi_inquiry_match);
652         }
653
654         if (match != NULL) {
655                 table[0] = ((struct scsi_op_quirk_entry *)match)->op_table;
656                 num_ops[0] = ((struct scsi_op_quirk_entry *)match)->num_ops;
657                 table[1] = scsi_op_codes;
658                 num_ops[1] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
659                 num_tables = 2;
660         } else {
661                 /*      
662                  * If this is true, we have a vendor specific opcode that
663                  * wasn't covered in the quirk table.
664                  */
665                 if ((opcode > 0xBF) || ((opcode > 0x5F) && (opcode < 0x80)))
666                         return("Vendor Specific Command");
667
668                 table[0] = scsi_op_codes;
669                 num_ops[0] = sizeof(scsi_op_codes)/sizeof(scsi_op_codes[0]);
670                 num_tables = 1;
671         }
672
673         /* RBC is 'Simplified' Direct Access Device */
674         if (pd_type == T_RBC)
675                 pd_type = T_DIRECT;
676
677         /* Map NODEVICE to Direct Access Device to handle REPORT LUNS, etc. */
678         if (pd_type == T_NODEVICE)
679                 pd_type = T_DIRECT;
680
681         opmask = 1 << pd_type;
682
683         for (j = 0; j < num_tables; j++) {
684                 for (i = 0;i < num_ops[j] && table[j][i].opcode <= opcode; i++){
685                         if ((table[j][i].opcode == opcode) 
686                          && ((table[j][i].opmask & opmask) != 0))
687                                 return(table[j][i].desc);
688                 }
689         }
690         
691         /*
692          * If we can't find a match for the command in the table, we just
693          * assume it's a vendor specifc command.
694          */
695         return("Vendor Specific Command");
696
697 }
698
699 #else /* SCSI_NO_OP_STRINGS */
700
701 const char *
702 scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
703 {
704         return("");
705 }
706
707 #endif
708
709
710 #if !defined(SCSI_NO_SENSE_STRINGS)
711 #define SST(asc, ascq, action, desc) \
712         asc, ascq, action, desc
713 #else 
714 const char empty_string[] = "";
715
716 #define SST(asc, ascq, action, desc) \
717         asc, ascq, action, empty_string
718 #endif 
719
720 const struct sense_key_table_entry sense_key_table[] = 
721 {
722         { SSD_KEY_NO_SENSE, SS_NOP, "NO SENSE" },
723         { SSD_KEY_RECOVERED_ERROR, SS_NOP|SSQ_PRINT_SENSE, "RECOVERED ERROR" },
724         { SSD_KEY_NOT_READY, SS_RDEF, "NOT READY" },
725         { SSD_KEY_MEDIUM_ERROR, SS_RDEF, "MEDIUM ERROR" },
726         { SSD_KEY_HARDWARE_ERROR, SS_RDEF, "HARDWARE FAILURE" },
727         { SSD_KEY_ILLEGAL_REQUEST, SS_FATAL|EINVAL, "ILLEGAL REQUEST" },
728         { SSD_KEY_UNIT_ATTENTION, SS_FATAL|ENXIO, "UNIT ATTENTION" },
729         { SSD_KEY_DATA_PROTECT, SS_FATAL|EACCES, "DATA PROTECT" },
730         { SSD_KEY_BLANK_CHECK, SS_FATAL|ENOSPC, "BLANK CHECK" },
731         { SSD_KEY_Vendor_Specific, SS_FATAL|EIO, "Vendor Specific" },
732         { SSD_KEY_COPY_ABORTED, SS_FATAL|EIO, "COPY ABORTED" },
733         { SSD_KEY_ABORTED_COMMAND, SS_RDEF, "ABORTED COMMAND" },
734         { SSD_KEY_EQUAL, SS_NOP, "EQUAL" },
735         { SSD_KEY_VOLUME_OVERFLOW, SS_FATAL|EIO, "VOLUME OVERFLOW" },
736         { SSD_KEY_MISCOMPARE, SS_NOP, "MISCOMPARE" },
737         { SSD_KEY_COMPLETED, SS_NOP, "COMPLETED" }
738 };
739
740 const int sense_key_table_size =
741     sizeof(sense_key_table)/sizeof(sense_key_table[0]);
742
743 static struct asc_table_entry quantum_fireball_entries[] = {
744         { SST(0x04, 0x0b, SS_START | SSQ_DECREMENT_COUNT | ENXIO, 
745              "Logical unit not ready, initializing cmd. required") }
746 };
747
748 static struct asc_table_entry sony_mo_entries[] = {
749         { SST(0x04, 0x00, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
750              "Logical unit not ready, cause not reportable") }
751 };
752
753 static struct asc_table_entry hgst_entries[] = {
754         { SST(0x04, 0xF0, SS_RDEF,
755             "Vendor Unique - Logical Unit Not Ready") },
756         { SST(0x0A, 0x01, SS_RDEF,
757             "Unrecovered Super Certification Log Write Error") },
758         { SST(0x0A, 0x02, SS_RDEF,
759             "Unrecovered Super Certification Log Read Error") },
760         { SST(0x15, 0x03, SS_RDEF,
761             "Unrecovered Sector Error") },
762         { SST(0x3E, 0x04, SS_RDEF,
763             "Unrecovered Self-Test Hard-Cache Test Fail") },
764         { SST(0x3E, 0x05, SS_RDEF,
765             "Unrecovered Self-Test OTF-Cache Fail") },
766         { SST(0x40, 0x00, SS_RDEF,
767             "Unrecovered SAT No Buffer Overflow Error") },
768         { SST(0x40, 0x01, SS_RDEF,
769             "Unrecovered SAT Buffer Overflow Error") },
770         { SST(0x40, 0x02, SS_RDEF,
771             "Unrecovered SAT No Buffer Overflow With ECS Fault") },
772         { SST(0x40, 0x03, SS_RDEF,
773             "Unrecovered SAT Buffer Overflow With ECS Fault") },
774         { SST(0x40, 0x81, SS_RDEF,
775             "DRAM Failure") },
776         { SST(0x44, 0x0B, SS_RDEF,
777             "Vendor Unique - Internal Target Failure") },
778         { SST(0x44, 0xF2, SS_RDEF,
779             "Vendor Unique - Internal Target Failure") },
780         { SST(0x44, 0xF6, SS_RDEF,
781             "Vendor Unique - Internal Target Failure") },
782         { SST(0x44, 0xF9, SS_RDEF,
783             "Vendor Unique - Internal Target Failure") },
784         { SST(0x44, 0xFA, SS_RDEF,
785             "Vendor Unique - Internal Target Failure") },
786         { SST(0x5D, 0x22, SS_RDEF,
787             "Extreme Over-Temperature Warning") },
788         { SST(0x5D, 0x50, SS_RDEF,
789             "Load/Unload cycle Count Warning") },
790         { SST(0x81, 0x00, SS_RDEF,
791             "Vendor Unique - Internal Logic Error") },
792         { SST(0x85, 0x00, SS_RDEF,
793             "Vendor Unique - Internal Key Seed Error") },
794 };
795
796 static struct asc_table_entry seagate_entries[] = {
797         { SST(0x04, 0xF0, SS_RDEF,
798             "Logical Unit Not Ready, super certify in Progress") },
799         { SST(0x08, 0x86, SS_RDEF,
800             "Write Fault Data Corruption") },
801         { SST(0x09, 0x0D, SS_RDEF,
802             "Tracking Failure") },
803         { SST(0x09, 0x0E, SS_RDEF,
804             "ETF Failure") },
805         { SST(0x0B, 0x5D, SS_RDEF,
806             "Pre-SMART Warning") },
807         { SST(0x0B, 0x85, SS_RDEF,
808             "5V Voltage Warning") },
809         { SST(0x0B, 0x8C, SS_RDEF,
810             "12V Voltage Warning") },
811         { SST(0x0C, 0xFF, SS_RDEF,
812             "Write Error - Too many error recovery revs") },
813         { SST(0x11, 0xFF, SS_RDEF,
814             "Unrecovered Read Error - Too many error recovery revs") },
815         { SST(0x19, 0x0E, SS_RDEF,
816             "Fewer than 1/2 defect list copies") },
817         { SST(0x20, 0xF3, SS_RDEF,
818             "Illegal CDB linked to skip mask cmd") },
819         { SST(0x24, 0xF0, SS_RDEF,
820             "Illegal byte in CDB, LBA not matching") },
821         { SST(0x24, 0xF1, SS_RDEF,
822             "Illegal byte in CDB, LEN not matching") },
823         { SST(0x24, 0xF2, SS_RDEF,
824             "Mask not matching transfer length") },
825         { SST(0x24, 0xF3, SS_RDEF,
826             "Drive formatted without plist") },
827         { SST(0x26, 0x95, SS_RDEF,
828             "Invalid Field Parameter - CAP File") },
829         { SST(0x26, 0x96, SS_RDEF,
830             "Invalid Field Parameter - RAP File") },
831         { SST(0x26, 0x97, SS_RDEF,
832             "Invalid Field Parameter - TMS Firmware Tag") },
833         { SST(0x26, 0x98, SS_RDEF,
834             "Invalid Field Parameter - Check Sum") },
835         { SST(0x26, 0x99, SS_RDEF,
836             "Invalid Field Parameter - Firmware Tag") },
837         { SST(0x29, 0x08, SS_RDEF,
838             "Write Log Dump data") },
839         { SST(0x29, 0x09, SS_RDEF,
840             "Write Log Dump data") },
841         { SST(0x29, 0x0A, SS_RDEF,
842             "Reserved disk space") },
843         { SST(0x29, 0x0B, SS_RDEF,
844             "SDBP") },
845         { SST(0x29, 0x0C, SS_RDEF,
846             "SDBP") },
847         { SST(0x31, 0x91, SS_RDEF,
848             "Format Corrupted World Wide Name (WWN) is Invalid") },
849         { SST(0x32, 0x03, SS_RDEF,
850             "Defect List - Length exceeds Command Allocated Length") },
851         { SST(0x33, 0x00, SS_RDEF,
852             "Flash not ready for access") },
853         { SST(0x3F, 0x70, SS_RDEF,
854             "Invalid RAP block") },
855         { SST(0x3F, 0x71, SS_RDEF,
856             "RAP/ETF mismatch") },
857         { SST(0x3F, 0x90, SS_RDEF,
858             "Invalid CAP block") },
859         { SST(0x3F, 0x91, SS_RDEF,
860             "World Wide Name (WWN) Mismatch") },
861         { SST(0x40, 0x01, SS_RDEF,
862             "DRAM Parity Error") },
863         { SST(0x40, 0x02, SS_RDEF,
864             "DRAM Parity Error") },
865         { SST(0x42, 0x0A, SS_RDEF,
866             "Loopback Test") },
867         { SST(0x42, 0x0B, SS_RDEF,
868             "Loopback Test") },
869         { SST(0x44, 0xF2, SS_RDEF,
870             "Compare error during data integrity check") },
871         { SST(0x44, 0xF6, SS_RDEF,
872             "Unrecoverable error during data integrity check") },
873         { SST(0x47, 0x80, SS_RDEF,
874             "Fibre Channel Sequence Error") },
875         { SST(0x4E, 0x01, SS_RDEF,
876             "Information Unit Too Short") },
877         { SST(0x80, 0x00, SS_RDEF,
878             "General Firmware Error / Command Timeout") },
879         { SST(0x80, 0x01, SS_RDEF,
880             "Command Timeout") },
881         { SST(0x80, 0x02, SS_RDEF,
882             "Command Timeout") },
883         { SST(0x80, 0x80, SS_RDEF,
884             "FC FIFO Error During Read Transfer") },
885         { SST(0x80, 0x81, SS_RDEF,
886             "FC FIFO Error During Write Transfer") },
887         { SST(0x80, 0x82, SS_RDEF,
888             "DISC FIFO Error During Read Transfer") },
889         { SST(0x80, 0x83, SS_RDEF,
890             "DISC FIFO Error During Write Transfer") },
891         { SST(0x80, 0x84, SS_RDEF,
892             "LBA Seeded LRC Error on Read") },
893         { SST(0x80, 0x85, SS_RDEF,
894             "LBA Seeded LRC Error on Write") },
895         { SST(0x80, 0x86, SS_RDEF,
896             "IOEDC Error on Read") },
897         { SST(0x80, 0x87, SS_RDEF,
898             "IOEDC Error on Write") },
899         { SST(0x80, 0x88, SS_RDEF,
900             "Host Parity Check Failed") },
901         { SST(0x80, 0x89, SS_RDEF,
902             "IOEDC error on read detected by formatter") },
903         { SST(0x80, 0x8A, SS_RDEF,
904             "Host Parity Errors / Host FIFO Initialization Failed") },
905         { SST(0x80, 0x8B, SS_RDEF,
906             "Host Parity Errors") },
907         { SST(0x80, 0x8C, SS_RDEF,
908             "Host Parity Errors") },
909         { SST(0x80, 0x8D, SS_RDEF,
910             "Host Parity Errors") },
911         { SST(0x81, 0x00, SS_RDEF,
912             "LA Check Failed") },
913         { SST(0x82, 0x00, SS_RDEF,
914             "Internal client detected insufficient buffer") },
915         { SST(0x84, 0x00, SS_RDEF,
916             "Scheduled Diagnostic And Repair") },
917 };
918
919 static struct scsi_sense_quirk_entry sense_quirk_table[] = {
920         {
921                 /*
922                  * XXX The Quantum Fireball ST and SE like to return 0x04 0x0b
923                  * when they really should return 0x04 0x02.
924                  */
925                 {T_DIRECT, SIP_MEDIA_FIXED, "QUANTUM", "FIREBALL S*", "*"},
926                 /*num_sense_keys*/0,
927                 sizeof(quantum_fireball_entries)/sizeof(struct asc_table_entry),
928                 /*sense key entries*/NULL,
929                 quantum_fireball_entries
930         },
931         {
932                 /*
933                  * This Sony MO drive likes to return 0x04, 0x00 when it
934                  * isn't spun up.
935                  */
936                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SONY", "SMO-*", "*"},
937                 /*num_sense_keys*/0,
938                 sizeof(sony_mo_entries)/sizeof(struct asc_table_entry),
939                 /*sense key entries*/NULL,
940                 sony_mo_entries
941         },
942         {
943                 /*
944                  * HGST vendor-specific error codes
945                  */
946                 {T_DIRECT, SIP_MEDIA_FIXED, "HGST", "*", "*"},
947                 /*num_sense_keys*/0,
948                 sizeof(hgst_entries)/sizeof(struct asc_table_entry),
949                 /*sense key entries*/NULL,
950                 hgst_entries
951         },
952         {
953                 /*
954                  * SEAGATE vendor-specific error codes
955                  */
956                 {T_DIRECT, SIP_MEDIA_FIXED, "SEAGATE", "*", "*"},
957                 /*num_sense_keys*/0,
958                 sizeof(seagate_entries)/sizeof(struct asc_table_entry),
959                 /*sense key entries*/NULL,
960                 seagate_entries
961         }
962 };
963
964 const int sense_quirk_table_size =
965     sizeof(sense_quirk_table)/sizeof(sense_quirk_table[0]);
966
967 static struct asc_table_entry asc_table[] = {
968         /*
969          * From: http://www.t10.org/lists/asc-num.txt
970          * Modifications by Jung-uk Kim (jkim@FreeBSD.org)
971          */
972         /*
973          * File: ASC-NUM.TXT
974          *
975          * SCSI ASC/ASCQ Assignments
976          * Numeric Sorted Listing
977          * as of  8/12/15
978          *
979          * D - DIRECT ACCESS DEVICE (SBC-2)                   device column key
980          * .T - SEQUENTIAL ACCESS DEVICE (SSC)               -------------------
981          * . L - PRINTER DEVICE (SSC)                           blank = reserved
982          * .  P - PROCESSOR DEVICE (SPC)                     not blank = allowed
983          * .  .W - WRITE ONCE READ MULTIPLE DEVICE (SBC-2)
984          * .  . R - CD DEVICE (MMC)
985          * .  .  O - OPTICAL MEMORY DEVICE (SBC-2)
986          * .  .  .M - MEDIA CHANGER DEVICE (SMC)
987          * .  .  . A - STORAGE ARRAY DEVICE (SCC)
988          * .  .  .  E - ENCLOSURE SERVICES DEVICE (SES)
989          * .  .  .  .B - SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
990          * .  .  .  . K - OPTICAL CARD READER/WRITER DEVICE (OCRW)
991          * .  .  .  .  V - AUTOMATION/DRIVE INTERFACE (ADC)
992          * .  .  .  .  .F - OBJECT-BASED STORAGE (OSD)
993          * DTLPWROMAEBKVF
994          * ASC      ASCQ  Action
995          * Description
996          */
997         /* DTLPWROMAEBKVF */
998         { SST(0x00, 0x00, SS_NOP,
999             "No additional sense information") },
1000         /*  T             */
1001         { SST(0x00, 0x01, SS_RDEF,
1002             "Filemark detected") },
1003         /*  T             */
1004         { SST(0x00, 0x02, SS_RDEF,
1005             "End-of-partition/medium detected") },
1006         /*  T             */
1007         { SST(0x00, 0x03, SS_RDEF,
1008             "Setmark detected") },
1009         /*  T             */
1010         { SST(0x00, 0x04, SS_RDEF,
1011             "Beginning-of-partition/medium detected") },
1012         /*  TL            */
1013         { SST(0x00, 0x05, SS_RDEF,
1014             "End-of-data detected") },
1015         /* DTLPWROMAEBKVF */
1016         { SST(0x00, 0x06, SS_RDEF,
1017             "I/O process terminated") },
1018         /*  T             */
1019         { SST(0x00, 0x07, SS_RDEF,      /* XXX TBD */
1020             "Programmable early warning detected") },
1021         /*      R         */
1022         { SST(0x00, 0x11, SS_FATAL | EBUSY,
1023             "Audio play operation in progress") },
1024         /*      R         */
1025         { SST(0x00, 0x12, SS_NOP,
1026             "Audio play operation paused") },
1027         /*      R         */
1028         { SST(0x00, 0x13, SS_NOP,
1029             "Audio play operation successfully completed") },
1030         /*      R         */
1031         { SST(0x00, 0x14, SS_RDEF,
1032             "Audio play operation stopped due to error") },
1033         /*      R         */
1034         { SST(0x00, 0x15, SS_NOP,
1035             "No current audio status to return") },
1036         /* DTLPWROMAEBKVF */
1037         { SST(0x00, 0x16, SS_FATAL | EBUSY,
1038             "Operation in progress") },
1039         /* DTL WROMAEBKVF */
1040         { SST(0x00, 0x17, SS_RDEF,
1041             "Cleaning requested") },
1042         /*  T             */
1043         { SST(0x00, 0x18, SS_RDEF,      /* XXX TBD */
1044             "Erase operation in progress") },
1045         /*  T             */
1046         { SST(0x00, 0x19, SS_RDEF,      /* XXX TBD */
1047             "Locate operation in progress") },
1048         /*  T             */
1049         { SST(0x00, 0x1A, SS_RDEF,      /* XXX TBD */
1050             "Rewind operation in progress") },
1051         /*  T             */
1052         { SST(0x00, 0x1B, SS_RDEF,      /* XXX TBD */
1053             "Set capacity operation in progress") },
1054         /*  T             */
1055         { SST(0x00, 0x1C, SS_RDEF,      /* XXX TBD */
1056             "Verify operation in progress") },
1057         /* DT        B    */
1058         { SST(0x00, 0x1D, SS_NOP,
1059             "ATA pass through information available") },
1060         /* DT   R MAEBKV  */
1061         { SST(0x00, 0x1E, SS_RDEF,      /* XXX TBD */
1062             "Conflicting SA creation request") },
1063         /* DT        B    */
1064         { SST(0x00, 0x1F, SS_RDEF,      /* XXX TBD */
1065             "Logical unit transitioning to another power condition") },
1066         /* DT P      B    */
1067         { SST(0x00, 0x20, SS_NOP,
1068             "Extended copy information available") },
1069         /* D              */
1070         { SST(0x00, 0x21, SS_RDEF,      /* XXX TBD */
1071             "Atomic command aborted due to ACA") },
1072         /* D   W O   BK   */
1073         { SST(0x01, 0x00, SS_RDEF,
1074             "No index/sector signal") },
1075         /* D   WRO   BK   */
1076         { SST(0x02, 0x00, SS_RDEF,
1077             "No seek complete") },
1078         /* DTL W O   BK   */
1079         { SST(0x03, 0x00, SS_RDEF,
1080             "Peripheral device write fault") },
1081         /*  T             */
1082         { SST(0x03, 0x01, SS_RDEF,
1083             "No write current") },
1084         /*  T             */
1085         { SST(0x03, 0x02, SS_RDEF,
1086             "Excessive write errors") },
1087         /* DTLPWROMAEBKVF */
1088         { SST(0x04, 0x00, SS_RDEF,
1089             "Logical unit not ready, cause not reportable") },
1090         /* DTLPWROMAEBKVF */
1091         { SST(0x04, 0x01, SS_WAIT | EBUSY,
1092             "Logical unit is in process of becoming ready") },
1093         /* DTLPWROMAEBKVF */
1094         { SST(0x04, 0x02, SS_START | SSQ_DECREMENT_COUNT | ENXIO,
1095             "Logical unit not ready, initializing command required") },
1096         /* DTLPWROMAEBKVF */
1097         { SST(0x04, 0x03, SS_FATAL | ENXIO,
1098             "Logical unit not ready, manual intervention required") },
1099         /* DTL  RO   B    */
1100         { SST(0x04, 0x04, SS_FATAL | EBUSY,
1101             "Logical unit not ready, format in progress") },
1102         /* DT  W O A BK F */
1103         { SST(0x04, 0x05, SS_FATAL | EBUSY,
1104             "Logical unit not ready, rebuild in progress") },
1105         /* DT  W O A BK   */
1106         { SST(0x04, 0x06, SS_FATAL | EBUSY,
1107             "Logical unit not ready, recalculation in progress") },
1108         /* DTLPWROMAEBKVF */
1109         { SST(0x04, 0x07, SS_FATAL | EBUSY,
1110             "Logical unit not ready, operation in progress") },
1111         /*      R         */
1112         { SST(0x04, 0x08, SS_FATAL | EBUSY,
1113             "Logical unit not ready, long write in progress") },
1114         /* DTLPWROMAEBKVF */
1115         { SST(0x04, 0x09, SS_RDEF,      /* XXX TBD */
1116             "Logical unit not ready, self-test in progress") },
1117         /* DTLPWROMAEBKVF */
1118         { SST(0x04, 0x0A, SS_WAIT | ENXIO,
1119             "Logical unit not accessible, asymmetric access state transition")},
1120         /* DTLPWROMAEBKVF */
1121         { SST(0x04, 0x0B, SS_FATAL | ENXIO,
1122             "Logical unit not accessible, target port in standby state") },
1123         /* DTLPWROMAEBKVF */
1124         { SST(0x04, 0x0C, SS_FATAL | ENXIO,
1125             "Logical unit not accessible, target port in unavailable state") },
1126         /*              F */
1127         { SST(0x04, 0x0D, SS_RDEF,      /* XXX TBD */
1128             "Logical unit not ready, structure check required") },
1129         /* DTL WR MAEBKVF */
1130         { SST(0x04, 0x0E, SS_RDEF,      /* XXX TBD */
1131             "Logical unit not ready, security session in progress") },
1132         /* DT  WROM  B    */
1133         { SST(0x04, 0x10, SS_RDEF,      /* XXX TBD */
1134             "Logical unit not ready, auxiliary memory not accessible") },
1135         /* DT  WRO AEB VF */
1136         { SST(0x04, 0x11, SS_WAIT | EBUSY,
1137             "Logical unit not ready, notify (enable spinup) required") },
1138         /*        M    V  */
1139         { SST(0x04, 0x12, SS_RDEF,      /* XXX TBD */
1140             "Logical unit not ready, offline") },
1141         /* DT   R MAEBKV  */
1142         { SST(0x04, 0x13, SS_RDEF,      /* XXX TBD */
1143             "Logical unit not ready, SA creation in progress") },
1144         /* D         B    */
1145         { SST(0x04, 0x14, SS_RDEF,      /* XXX TBD */
1146             "Logical unit not ready, space allocation in progress") },
1147         /*        M       */
1148         { SST(0x04, 0x15, SS_RDEF,      /* XXX TBD */
1149             "Logical unit not ready, robotics disabled") },
1150         /*        M       */
1151         { SST(0x04, 0x16, SS_RDEF,      /* XXX TBD */
1152             "Logical unit not ready, configuration required") },
1153         /*        M       */
1154         { SST(0x04, 0x17, SS_RDEF,      /* XXX TBD */
1155             "Logical unit not ready, calibration required") },
1156         /*        M       */
1157         { SST(0x04, 0x18, SS_RDEF,      /* XXX TBD */
1158             "Logical unit not ready, a door is open") },
1159         /*        M       */
1160         { SST(0x04, 0x19, SS_RDEF,      /* XXX TBD */
1161             "Logical unit not ready, operating in sequential mode") },
1162         /* DT        B    */
1163         { SST(0x04, 0x1A, SS_RDEF,      /* XXX TBD */
1164             "Logical unit not ready, START/STOP UNIT command in progress") },
1165         /* D         B    */
1166         { SST(0x04, 0x1B, SS_RDEF,      /* XXX TBD */
1167             "Logical unit not ready, sanitize in progress") },
1168         /* DT     MAEB    */
1169         { SST(0x04, 0x1C, SS_RDEF,      /* XXX TBD */
1170             "Logical unit not ready, additional power use not yet granted") },
1171         /* D              */
1172         { SST(0x04, 0x1D, SS_RDEF,      /* XXX TBD */
1173             "Logical unit not ready, configuration in progress") },
1174         /* D              */
1175         { SST(0x04, 0x1E, SS_FATAL | ENXIO,
1176             "Logical unit not ready, microcode activation required") },
1177         /* DTLPWROMAEBKVF */
1178         { SST(0x04, 0x1F, SS_FATAL | ENXIO,
1179             "Logical unit not ready, microcode download required") },
1180         /* DTLPWROMAEBKVF */
1181         { SST(0x04, 0x20, SS_RDEF,      /* XXX TBD */
1182             "Logical unit not ready, logical unit reset required") },
1183         /* DTLPWROMAEBKVF */
1184         { SST(0x04, 0x21, SS_RDEF,      /* XXX TBD */
1185             "Logical unit not ready, hard reset required") },
1186         /* DTLPWROMAEBKVF */
1187         { SST(0x04, 0x22, SS_RDEF,      /* XXX TBD */
1188             "Logical unit not ready, power cycle required") },
1189         /* DTL WROMAEBKVF */
1190         { SST(0x05, 0x00, SS_RDEF,
1191             "Logical unit does not respond to selection") },
1192         /* D   WROM  BK   */
1193         { SST(0x06, 0x00, SS_RDEF,
1194             "No reference position found") },
1195         /* DTL WROM  BK   */
1196         { SST(0x07, 0x00, SS_RDEF,
1197             "Multiple peripheral devices selected") },
1198         /* DTL WROMAEBKVF */
1199         { SST(0x08, 0x00, SS_RDEF,
1200             "Logical unit communication failure") },
1201         /* DTL WROMAEBKVF */
1202         { SST(0x08, 0x01, SS_RDEF,
1203             "Logical unit communication time-out") },
1204         /* DTL WROMAEBKVF */
1205         { SST(0x08, 0x02, SS_RDEF,
1206             "Logical unit communication parity error") },
1207         /* DT   ROM  BK   */
1208         { SST(0x08, 0x03, SS_RDEF,
1209             "Logical unit communication CRC error (Ultra-DMA/32)") },
1210         /* DTLPWRO    K   */
1211         { SST(0x08, 0x04, SS_RDEF,      /* XXX TBD */
1212             "Unreachable copy target") },
1213         /* DT  WRO   B    */
1214         { SST(0x09, 0x00, SS_RDEF,
1215             "Track following error") },
1216         /*     WRO    K   */
1217         { SST(0x09, 0x01, SS_RDEF,
1218             "Tracking servo failure") },
1219         /*     WRO    K   */
1220         { SST(0x09, 0x02, SS_RDEF,
1221             "Focus servo failure") },
1222         /*     WRO        */
1223         { SST(0x09, 0x03, SS_RDEF,
1224             "Spindle servo failure") },
1225         /* DT  WRO   B    */
1226         { SST(0x09, 0x04, SS_RDEF,
1227             "Head select fault") },
1228         /* DT   RO   B    */
1229         { SST(0x09, 0x05, SS_RDEF,
1230             "Vibration induced tracking error") },
1231         /* DTLPWROMAEBKVF */
1232         { SST(0x0A, 0x00, SS_FATAL | ENOSPC,
1233             "Error log overflow") },
1234         /* DTLPWROMAEBKVF */
1235         { SST(0x0B, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1236             "Warning") },
1237         /* DTLPWROMAEBKVF */
1238         { SST(0x0B, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1239             "Warning - specified temperature exceeded") },
1240         /* DTLPWROMAEBKVF */
1241         { SST(0x0B, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1242             "Warning - enclosure degraded") },
1243         /* DTLPWROMAEBKVF */
1244         { SST(0x0B, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1245             "Warning - background self-test failed") },
1246         /* DTLPWRO AEBKVF */
1247         { SST(0x0B, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1248             "Warning - background pre-scan detected medium error") },
1249         /* DTLPWRO AEBKVF */
1250         { SST(0x0B, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1251             "Warning - background medium scan detected medium error") },
1252         /* DTLPWROMAEBKVF */
1253         { SST(0x0B, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1254             "Warning - non-volatile cache now volatile") },
1255         /* DTLPWROMAEBKVF */
1256         { SST(0x0B, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1257             "Warning - degraded power to non-volatile cache") },
1258         /* DTLPWROMAEBKVF */
1259         { SST(0x0B, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1260             "Warning - power loss expected") },
1261         /* D              */
1262         { SST(0x0B, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1263             "Warning - device statistics notification available") },
1264         /* DTLPWROMAEBKVF */
1265         { SST(0x0B, 0x0A, SS_NOP | SSQ_PRINT_SENSE,
1266             "Warning - High critical temperature limit exceeded") },
1267         /* DTLPWROMAEBKVF */
1268         { SST(0x0B, 0x0B, SS_NOP | SSQ_PRINT_SENSE,
1269             "Warning - Low critical temperature limit exceeded") },
1270         /* DTLPWROMAEBKVF */
1271         { SST(0x0B, 0x0C, SS_NOP | SSQ_PRINT_SENSE,
1272             "Warning - High operating temperature limit exceeded") },
1273         /* DTLPWROMAEBKVF */
1274         { SST(0x0B, 0x0D, SS_NOP | SSQ_PRINT_SENSE,
1275             "Warning - Low operating temperature limit exceeded") },
1276         /* DTLPWROMAEBKVF */
1277         { SST(0x0B, 0x0E, SS_NOP | SSQ_PRINT_SENSE,
1278             "Warning - High citical humidity limit exceeded") },
1279         /* DTLPWROMAEBKVF */
1280         { SST(0x0B, 0x0F, SS_NOP | SSQ_PRINT_SENSE,
1281             "Warning - Low citical humidity limit exceeded") },
1282         /* DTLPWROMAEBKVF */
1283         { SST(0x0B, 0x10, SS_NOP | SSQ_PRINT_SENSE,
1284             "Warning - High operating humidity limit exceeded") },
1285         /* DTLPWROMAEBKVF */
1286         { SST(0x0B, 0x11, SS_NOP | SSQ_PRINT_SENSE,
1287             "Warning - Low operating humidity limit exceeded") },
1288         /*  T   R         */
1289         { SST(0x0C, 0x00, SS_RDEF,
1290             "Write error") },
1291         /*            K   */
1292         { SST(0x0C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1293             "Write error - recovered with auto reallocation") },
1294         /* D   W O   BK   */
1295         { SST(0x0C, 0x02, SS_RDEF,
1296             "Write error - auto reallocation failed") },
1297         /* D   W O   BK   */
1298         { SST(0x0C, 0x03, SS_RDEF,
1299             "Write error - recommend reassignment") },
1300         /* DT  W O   B    */
1301         { SST(0x0C, 0x04, SS_RDEF,
1302             "Compression check miscompare error") },
1303         /* DT  W O   B    */
1304         { SST(0x0C, 0x05, SS_RDEF,
1305             "Data expansion occurred during compression") },
1306         /* DT  W O   B    */
1307         { SST(0x0C, 0x06, SS_RDEF,
1308             "Block not compressible") },
1309         /*      R         */
1310         { SST(0x0C, 0x07, SS_RDEF,
1311             "Write error - recovery needed") },
1312         /*      R         */
1313         { SST(0x0C, 0x08, SS_RDEF,
1314             "Write error - recovery failed") },
1315         /*      R         */
1316         { SST(0x0C, 0x09, SS_RDEF,
1317             "Write error - loss of streaming") },
1318         /*      R         */
1319         { SST(0x0C, 0x0A, SS_RDEF,
1320             "Write error - padding blocks added") },
1321         /* DT  WROM  B    */
1322         { SST(0x0C, 0x0B, SS_RDEF,      /* XXX TBD */
1323             "Auxiliary memory write error") },
1324         /* DTLPWRO AEBKVF */
1325         { SST(0x0C, 0x0C, SS_RDEF,      /* XXX TBD */
1326             "Write error - unexpected unsolicited data") },
1327         /* DTLPWRO AEBKVF */
1328         { SST(0x0C, 0x0D, SS_RDEF,      /* XXX TBD */
1329             "Write error - not enough unsolicited data") },
1330         /* DT  W O   BK   */
1331         { SST(0x0C, 0x0E, SS_RDEF,      /* XXX TBD */
1332             "Multiple write errors") },
1333         /*      R         */
1334         { SST(0x0C, 0x0F, SS_RDEF,      /* XXX TBD */
1335             "Defects in error window") },
1336         /* D              */
1337         { SST(0x0C, 0x10, SS_RDEF,      /* XXX TBD */
1338             "Incomplete multiple atomic write operations") },
1339         /* D              */
1340         { SST(0x0C, 0x11, SS_RDEF,      /* XXX TBD */
1341             "Write error - recovery scan needed") },
1342         /* D              */
1343         { SST(0x0C, 0x12, SS_RDEF,      /* XXX TBD */
1344             "Write error - insufficient zone resources") },
1345         /* DTLPWRO A  K   */
1346         { SST(0x0D, 0x00, SS_RDEF,      /* XXX TBD */
1347             "Error detected by third party temporary initiator") },
1348         /* DTLPWRO A  K   */
1349         { SST(0x0D, 0x01, SS_RDEF,      /* XXX TBD */
1350             "Third party device failure") },
1351         /* DTLPWRO A  K   */
1352         { SST(0x0D, 0x02, SS_RDEF,      /* XXX TBD */
1353             "Copy target device not reachable") },
1354         /* DTLPWRO A  K   */
1355         { SST(0x0D, 0x03, SS_RDEF,      /* XXX TBD */
1356             "Incorrect copy target device type") },
1357         /* DTLPWRO A  K   */
1358         { SST(0x0D, 0x04, SS_RDEF,      /* XXX TBD */
1359             "Copy target device data underrun") },
1360         /* DTLPWRO A  K   */
1361         { SST(0x0D, 0x05, SS_RDEF,      /* XXX TBD */
1362             "Copy target device data overrun") },
1363         /* DT PWROMAEBK F */
1364         { SST(0x0E, 0x00, SS_RDEF,      /* XXX TBD */
1365             "Invalid information unit") },
1366         /* DT PWROMAEBK F */
1367         { SST(0x0E, 0x01, SS_RDEF,      /* XXX TBD */
1368             "Information unit too short") },
1369         /* DT PWROMAEBK F */
1370         { SST(0x0E, 0x02, SS_RDEF,      /* XXX TBD */
1371             "Information unit too long") },
1372         /* DT P R MAEBK F */
1373         { SST(0x0E, 0x03, SS_RDEF,      /* XXX TBD */
1374             "Invalid field in command information unit") },
1375         /* D   W O   BK   */
1376         { SST(0x10, 0x00, SS_RDEF,
1377             "ID CRC or ECC error") },
1378         /* DT  W O        */
1379         { SST(0x10, 0x01, SS_RDEF,      /* XXX TBD */
1380             "Logical block guard check failed") },
1381         /* DT  W O        */
1382         { SST(0x10, 0x02, SS_RDEF,      /* XXX TBD */
1383             "Logical block application tag check failed") },
1384         /* DT  W O        */
1385         { SST(0x10, 0x03, SS_RDEF,      /* XXX TBD */
1386             "Logical block reference tag check failed") },
1387         /*  T             */
1388         { SST(0x10, 0x04, SS_RDEF,      /* XXX TBD */
1389             "Logical block protection error on recovered buffer data") },
1390         /*  T             */
1391         { SST(0x10, 0x05, SS_RDEF,      /* XXX TBD */
1392             "Logical block protection method error") },
1393         /* DT  WRO   BK   */
1394         { SST(0x11, 0x00, SS_FATAL|EIO,
1395             "Unrecovered read error") },
1396         /* DT  WRO   BK   */
1397         { SST(0x11, 0x01, SS_FATAL|EIO,
1398             "Read retries exhausted") },
1399         /* DT  WRO   BK   */
1400         { SST(0x11, 0x02, SS_FATAL|EIO,
1401             "Error too long to correct") },
1402         /* DT  W O   BK   */
1403         { SST(0x11, 0x03, SS_FATAL|EIO,
1404             "Multiple read errors") },
1405         /* D   W O   BK   */
1406         { SST(0x11, 0x04, SS_FATAL|EIO,
1407             "Unrecovered read error - auto reallocate failed") },
1408         /*     WRO   B    */
1409         { SST(0x11, 0x05, SS_FATAL|EIO,
1410             "L-EC uncorrectable error") },
1411         /*     WRO   B    */
1412         { SST(0x11, 0x06, SS_FATAL|EIO,
1413             "CIRC unrecovered error") },
1414         /*     W O   B    */
1415         { SST(0x11, 0x07, SS_RDEF,
1416             "Data re-synchronization error") },
1417         /*  T             */
1418         { SST(0x11, 0x08, SS_RDEF,
1419             "Incomplete block read") },
1420         /*  T             */
1421         { SST(0x11, 0x09, SS_RDEF,
1422             "No gap found") },
1423         /* DT    O   BK   */
1424         { SST(0x11, 0x0A, SS_RDEF,
1425             "Miscorrected error") },
1426         /* D   W O   BK   */
1427         { SST(0x11, 0x0B, SS_FATAL|EIO,
1428             "Unrecovered read error - recommend reassignment") },
1429         /* D   W O   BK   */
1430         { SST(0x11, 0x0C, SS_FATAL|EIO,
1431             "Unrecovered read error - recommend rewrite the data") },
1432         /* DT  WRO   B    */
1433         { SST(0x11, 0x0D, SS_RDEF,
1434             "De-compression CRC error") },
1435         /* DT  WRO   B    */
1436         { SST(0x11, 0x0E, SS_RDEF,
1437             "Cannot decompress using declared algorithm") },
1438         /*      R         */
1439         { SST(0x11, 0x0F, SS_RDEF,
1440             "Error reading UPC/EAN number") },
1441         /*      R         */
1442         { SST(0x11, 0x10, SS_RDEF,
1443             "Error reading ISRC number") },
1444         /*      R         */
1445         { SST(0x11, 0x11, SS_RDEF,
1446             "Read error - loss of streaming") },
1447         /* DT  WROM  B    */
1448         { SST(0x11, 0x12, SS_RDEF,      /* XXX TBD */
1449             "Auxiliary memory read error") },
1450         /* DTLPWRO AEBKVF */
1451         { SST(0x11, 0x13, SS_RDEF,      /* XXX TBD */
1452             "Read error - failed retransmission request") },
1453         /* D              */
1454         { SST(0x11, 0x14, SS_RDEF,      /* XXX TBD */
1455             "Read error - LBA marked bad by application client") },
1456         /* D              */
1457         { SST(0x11, 0x15, SS_RDEF,      /* XXX TBD */
1458             "Write after sanitize required") },
1459         /* D   W O   BK   */
1460         { SST(0x12, 0x00, SS_RDEF,
1461             "Address mark not found for ID field") },
1462         /* D   W O   BK   */
1463         { SST(0x13, 0x00, SS_RDEF,
1464             "Address mark not found for data field") },
1465         /* DTL WRO   BK   */
1466         { SST(0x14, 0x00, SS_RDEF,
1467             "Recorded entity not found") },
1468         /* DT  WRO   BK   */
1469         { SST(0x14, 0x01, SS_RDEF,
1470             "Record not found") },
1471         /*  T             */
1472         { SST(0x14, 0x02, SS_RDEF,
1473             "Filemark or setmark not found") },
1474         /*  T             */
1475         { SST(0x14, 0x03, SS_RDEF,
1476             "End-of-data not found") },
1477         /*  T             */
1478         { SST(0x14, 0x04, SS_RDEF,
1479             "Block sequence error") },
1480         /* DT  W O   BK   */
1481         { SST(0x14, 0x05, SS_RDEF,
1482             "Record not found - recommend reassignment") },
1483         /* DT  W O   BK   */
1484         { SST(0x14, 0x06, SS_RDEF,
1485             "Record not found - data auto-reallocated") },
1486         /*  T             */
1487         { SST(0x14, 0x07, SS_RDEF,      /* XXX TBD */
1488             "Locate operation failure") },
1489         /* DTL WROM  BK   */
1490         { SST(0x15, 0x00, SS_RDEF,
1491             "Random positioning error") },
1492         /* DTL WROM  BK   */
1493         { SST(0x15, 0x01, SS_RDEF,
1494             "Mechanical positioning error") },
1495         /* DT  WRO   BK   */
1496         { SST(0x15, 0x02, SS_RDEF,
1497             "Positioning error detected by read of medium") },
1498         /* D   W O   BK   */
1499         { SST(0x16, 0x00, SS_RDEF,
1500             "Data synchronization mark error") },
1501         /* D   W O   BK   */
1502         { SST(0x16, 0x01, SS_RDEF,
1503             "Data sync error - data rewritten") },
1504         /* D   W O   BK   */
1505         { SST(0x16, 0x02, SS_RDEF,
1506             "Data sync error - recommend rewrite") },
1507         /* D   W O   BK   */
1508         { SST(0x16, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1509             "Data sync error - data auto-reallocated") },
1510         /* D   W O   BK   */
1511         { SST(0x16, 0x04, SS_RDEF,
1512             "Data sync error - recommend reassignment") },
1513         /* DT  WRO   BK   */
1514         { SST(0x17, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1515             "Recovered data with no error correction applied") },
1516         /* DT  WRO   BK   */
1517         { SST(0x17, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1518             "Recovered data with retries") },
1519         /* DT  WRO   BK   */
1520         { SST(0x17, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1521             "Recovered data with positive head offset") },
1522         /* DT  WRO   BK   */
1523         { SST(0x17, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1524             "Recovered data with negative head offset") },
1525         /*     WRO   B    */
1526         { SST(0x17, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1527             "Recovered data with retries and/or CIRC applied") },
1528         /* D   WRO   BK   */
1529         { SST(0x17, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1530             "Recovered data using previous sector ID") },
1531         /* D   W O   BK   */
1532         { SST(0x17, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1533             "Recovered data without ECC - data auto-reallocated") },
1534         /* D   WRO   BK   */
1535         { SST(0x17, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1536             "Recovered data without ECC - recommend reassignment") },
1537         /* D   WRO   BK   */
1538         { SST(0x17, 0x08, SS_NOP | SSQ_PRINT_SENSE,
1539             "Recovered data without ECC - recommend rewrite") },
1540         /* D   WRO   BK   */
1541         { SST(0x17, 0x09, SS_NOP | SSQ_PRINT_SENSE,
1542             "Recovered data without ECC - data rewritten") },
1543         /* DT  WRO   BK   */
1544         { SST(0x18, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1545             "Recovered data with error correction applied") },
1546         /* D   WRO   BK   */
1547         { SST(0x18, 0x01, SS_NOP | SSQ_PRINT_SENSE,
1548             "Recovered data with error corr. & retries applied") },
1549         /* D   WRO   BK   */
1550         { SST(0x18, 0x02, SS_NOP | SSQ_PRINT_SENSE,
1551             "Recovered data - data auto-reallocated") },
1552         /*      R         */
1553         { SST(0x18, 0x03, SS_NOP | SSQ_PRINT_SENSE,
1554             "Recovered data with CIRC") },
1555         /*      R         */
1556         { SST(0x18, 0x04, SS_NOP | SSQ_PRINT_SENSE,
1557             "Recovered data with L-EC") },
1558         /* D   WRO   BK   */
1559         { SST(0x18, 0x05, SS_NOP | SSQ_PRINT_SENSE,
1560             "Recovered data - recommend reassignment") },
1561         /* D   WRO   BK   */
1562         { SST(0x18, 0x06, SS_NOP | SSQ_PRINT_SENSE,
1563             "Recovered data - recommend rewrite") },
1564         /* D   W O   BK   */
1565         { SST(0x18, 0x07, SS_NOP | SSQ_PRINT_SENSE,
1566             "Recovered data with ECC - data rewritten") },
1567         /*      R         */
1568         { SST(0x18, 0x08, SS_RDEF,      /* XXX TBD */
1569             "Recovered data with linking") },
1570         /* D     O    K   */
1571         { SST(0x19, 0x00, SS_RDEF,
1572             "Defect list error") },
1573         /* D     O    K   */
1574         { SST(0x19, 0x01, SS_RDEF,
1575             "Defect list not available") },
1576         /* D     O    K   */
1577         { SST(0x19, 0x02, SS_RDEF,
1578             "Defect list error in primary list") },
1579         /* D     O    K   */
1580         { SST(0x19, 0x03, SS_RDEF,
1581             "Defect list error in grown list") },
1582         /* DTLPWROMAEBKVF */
1583         { SST(0x1A, 0x00, SS_RDEF,
1584             "Parameter list length error") },
1585         /* DTLPWROMAEBKVF */
1586         { SST(0x1B, 0x00, SS_RDEF,
1587             "Synchronous data transfer error") },
1588         /* D     O   BK   */
1589         { SST(0x1C, 0x00, SS_RDEF,
1590             "Defect list not found") },
1591         /* D     O   BK   */
1592         { SST(0x1C, 0x01, SS_RDEF,
1593             "Primary defect list not found") },
1594         /* D     O   BK   */
1595         { SST(0x1C, 0x02, SS_RDEF,
1596             "Grown defect list not found") },
1597         /* DT  WRO   BK   */
1598         { SST(0x1D, 0x00, SS_FATAL,
1599             "Miscompare during verify operation") },
1600         /* D         B    */
1601         { SST(0x1D, 0x01, SS_RDEF,      /* XXX TBD */
1602             "Miscomparable verify of unmapped LBA") },
1603         /* D   W O   BK   */
1604         { SST(0x1E, 0x00, SS_NOP | SSQ_PRINT_SENSE,
1605             "Recovered ID with ECC correction") },
1606         /* D     O    K   */
1607         { SST(0x1F, 0x00, SS_RDEF,
1608             "Partial defect list transfer") },
1609         /* DTLPWROMAEBKVF */
1610         { SST(0x20, 0x00, SS_FATAL | EINVAL,
1611             "Invalid command operation code") },
1612         /* DT PWROMAEBK   */
1613         { SST(0x20, 0x01, SS_RDEF,      /* XXX TBD */
1614             "Access denied - initiator pending-enrolled") },
1615         /* DT PWROMAEBK   */
1616         { SST(0x20, 0x02, SS_RDEF,      /* XXX TBD */
1617             "Access denied - no access rights") },
1618         /* DT PWROMAEBK   */
1619         { SST(0x20, 0x03, SS_RDEF,      /* XXX TBD */
1620             "Access denied - invalid mgmt ID key") },
1621         /*  T             */
1622         { SST(0x20, 0x04, SS_RDEF,      /* XXX TBD */
1623             "Illegal command while in write capable state") },
1624         /*  T             */
1625         { SST(0x20, 0x05, SS_RDEF,      /* XXX TBD */
1626             "Obsolete") },
1627         /*  T             */
1628         { SST(0x20, 0x06, SS_RDEF,      /* XXX TBD */
1629             "Illegal command while in explicit address mode") },
1630         /*  T             */
1631         { SST(0x20, 0x07, SS_RDEF,      /* XXX TBD */
1632             "Illegal command while in implicit address mode") },
1633         /* DT PWROMAEBK   */
1634         { SST(0x20, 0x08, SS_RDEF,      /* XXX TBD */
1635             "Access denied - enrollment conflict") },
1636         /* DT PWROMAEBK   */
1637         { SST(0x20, 0x09, SS_RDEF,      /* XXX TBD */
1638             "Access denied - invalid LU identifier") },
1639         /* DT PWROMAEBK   */
1640         { SST(0x20, 0x0A, SS_RDEF,      /* XXX TBD */
1641             "Access denied - invalid proxy token") },
1642         /* DT PWROMAEBK   */
1643         { SST(0x20, 0x0B, SS_RDEF,      /* XXX TBD */
1644             "Access denied - ACL LUN conflict") },
1645         /*  T             */
1646         { SST(0x20, 0x0C, SS_FATAL | EINVAL,
1647             "Illegal command when not in append-only mode") },
1648         /* DT  WRO   BK   */
1649         { SST(0x21, 0x00, SS_FATAL | EINVAL,
1650             "Logical block address out of range") },
1651         /* DT  WROM  BK   */
1652         { SST(0x21, 0x01, SS_FATAL | EINVAL,
1653             "Invalid element address") },
1654         /*      R         */
1655         { SST(0x21, 0x02, SS_RDEF,      /* XXX TBD */
1656             "Invalid address for write") },
1657         /*      R         */
1658         { SST(0x21, 0x03, SS_RDEF,      /* XXX TBD */
1659             "Invalid write crossing layer jump") },
1660         /* D              */
1661         { SST(0x21, 0x04, SS_RDEF,      /* XXX TBD */
1662             "Unaligned write command") },
1663         /* D              */
1664         { SST(0x21, 0x05, SS_RDEF,      /* XXX TBD */
1665             "Write boundary violation") },
1666         /* D              */
1667         { SST(0x21, 0x06, SS_RDEF,      /* XXX TBD */
1668             "Attempt to read invalid data") },
1669         /* D              */
1670         { SST(0x21, 0x07, SS_RDEF,      /* XXX TBD */
1671             "Read boundary violation") },
1672         /* D              */
1673         { SST(0x22, 0x00, SS_FATAL | EINVAL,
1674             "Illegal function (use 20 00, 24 00, or 26 00)") },
1675         /* DT P      B    */
1676         { SST(0x23, 0x00, SS_FATAL | EINVAL,
1677             "Invalid token operation, cause not reportable") },
1678         /* DT P      B    */
1679         { SST(0x23, 0x01, SS_FATAL | EINVAL,
1680             "Invalid token operation, unsupported token type") },
1681         /* DT P      B    */
1682         { SST(0x23, 0x02, SS_FATAL | EINVAL,
1683             "Invalid token operation, remote token usage not supported") },
1684         /* DT P      B    */
1685         { SST(0x23, 0x03, SS_FATAL | EINVAL,
1686             "Invalid token operation, remote ROD token creation not supported") },
1687         /* DT P      B    */
1688         { SST(0x23, 0x04, SS_FATAL | EINVAL,
1689             "Invalid token operation, token unknown") },
1690         /* DT P      B    */
1691         { SST(0x23, 0x05, SS_FATAL | EINVAL,
1692             "Invalid token operation, token corrupt") },
1693         /* DT P      B    */
1694         { SST(0x23, 0x06, SS_FATAL | EINVAL,
1695             "Invalid token operation, token revoked") },
1696         /* DT P      B    */
1697         { SST(0x23, 0x07, SS_FATAL | EINVAL,
1698             "Invalid token operation, token expired") },
1699         /* DT P      B    */
1700         { SST(0x23, 0x08, SS_FATAL | EINVAL,
1701             "Invalid token operation, token cancelled") },
1702         /* DT P      B    */
1703         { SST(0x23, 0x09, SS_FATAL | EINVAL,
1704             "Invalid token operation, token deleted") },
1705         /* DT P      B    */
1706         { SST(0x23, 0x0A, SS_FATAL | EINVAL,
1707             "Invalid token operation, invalid token length") },
1708         /* DTLPWROMAEBKVF */
1709         { SST(0x24, 0x00, SS_FATAL | EINVAL,
1710             "Invalid field in CDB") },
1711         /* DTLPWRO AEBKVF */
1712         { SST(0x24, 0x01, SS_RDEF,      /* XXX TBD */
1713             "CDB decryption error") },
1714         /*  T             */
1715         { SST(0x24, 0x02, SS_RDEF,      /* XXX TBD */
1716             "Obsolete") },
1717         /*  T             */
1718         { SST(0x24, 0x03, SS_RDEF,      /* XXX TBD */
1719             "Obsolete") },
1720         /*              F */
1721         { SST(0x24, 0x04, SS_RDEF,      /* XXX TBD */
1722             "Security audit value frozen") },
1723         /*              F */
1724         { SST(0x24, 0x05, SS_RDEF,      /* XXX TBD */
1725             "Security working key frozen") },
1726         /*              F */
1727         { SST(0x24, 0x06, SS_RDEF,      /* XXX TBD */
1728             "NONCE not unique") },
1729         /*              F */
1730         { SST(0x24, 0x07, SS_RDEF,      /* XXX TBD */
1731             "NONCE timestamp out of range") },
1732         /* DT   R MAEBKV  */
1733         { SST(0x24, 0x08, SS_RDEF,      /* XXX TBD */
1734             "Invalid XCDB") },
1735         /* DTLPWROMAEBKVF */
1736         { SST(0x25, 0x00, SS_FATAL | ENXIO | SSQ_LOST,
1737             "Logical unit not supported") },
1738         /* DTLPWROMAEBKVF */
1739         { SST(0x26, 0x00, SS_FATAL | EINVAL,
1740             "Invalid field in parameter list") },
1741         /* DTLPWROMAEBKVF */
1742         { SST(0x26, 0x01, SS_FATAL | EINVAL,
1743             "Parameter not supported") },
1744         /* DTLPWROMAEBKVF */
1745         { SST(0x26, 0x02, SS_FATAL | EINVAL,
1746             "Parameter value invalid") },
1747         /* DTLPWROMAE K   */
1748         { SST(0x26, 0x03, SS_FATAL | EINVAL,
1749             "Threshold parameters not supported") },
1750         /* DTLPWROMAEBKVF */
1751         { SST(0x26, 0x04, SS_FATAL | EINVAL,
1752             "Invalid release of persistent reservation") },
1753         /* DTLPWRO A BK   */
1754         { SST(0x26, 0x05, SS_RDEF,      /* XXX TBD */
1755             "Data decryption error") },
1756         /* DTLPWRO    K   */
1757         { SST(0x26, 0x06, SS_FATAL | EINVAL,
1758             "Too many target descriptors") },
1759         /* DTLPWRO    K   */
1760         { SST(0x26, 0x07, SS_FATAL | EINVAL,
1761             "Unsupported target descriptor type code") },
1762         /* DTLPWRO    K   */
1763         { SST(0x26, 0x08, SS_FATAL | EINVAL,
1764             "Too many segment descriptors") },
1765         /* DTLPWRO    K   */
1766         { SST(0x26, 0x09, SS_FATAL | EINVAL,
1767             "Unsupported segment descriptor type code") },
1768         /* DTLPWRO    K   */
1769         { SST(0x26, 0x0A, SS_FATAL | EINVAL,
1770             "Unexpected inexact segment") },
1771         /* DTLPWRO    K   */
1772         { SST(0x26, 0x0B, SS_FATAL | EINVAL,
1773             "Inline data length exceeded") },
1774         /* DTLPWRO    K   */
1775         { SST(0x26, 0x0C, SS_FATAL | EINVAL,
1776             "Invalid operation for copy source or destination") },
1777         /* DTLPWRO    K   */
1778         { SST(0x26, 0x0D, SS_FATAL | EINVAL,
1779             "Copy segment granularity violation") },
1780         /* DT PWROMAEBK   */
1781         { SST(0x26, 0x0E, SS_RDEF,      /* XXX TBD */
1782             "Invalid parameter while port is enabled") },
1783         /*              F */
1784         { SST(0x26, 0x0F, SS_RDEF,      /* XXX TBD */
1785             "Invalid data-out buffer integrity check value") },
1786         /*  T             */
1787         { SST(0x26, 0x10, SS_RDEF,      /* XXX TBD */
1788             "Data decryption key fail limit reached") },
1789         /*  T             */
1790         { SST(0x26, 0x11, SS_RDEF,      /* XXX TBD */
1791             "Incomplete key-associated data set") },
1792         /*  T             */
1793         { SST(0x26, 0x12, SS_RDEF,      /* XXX TBD */
1794             "Vendor specific key reference not found") },
1795         /* D              */
1796         { SST(0x26, 0x13, SS_RDEF,      /* XXX TBD */
1797             "Application tag mode page is invalid") },
1798         /* DT  WRO   BK   */
1799         { SST(0x27, 0x00, SS_FATAL | EACCES,
1800             "Write protected") },
1801         /* DT  WRO   BK   */
1802         { SST(0x27, 0x01, SS_FATAL | EACCES,
1803             "Hardware write protected") },
1804         /* DT  WRO   BK   */
1805         { SST(0x27, 0x02, SS_FATAL | EACCES,
1806             "Logical unit software write protected") },
1807         /*  T   R         */
1808         { SST(0x27, 0x03, SS_FATAL | EACCES,
1809             "Associated write protect") },
1810         /*  T   R         */
1811         { SST(0x27, 0x04, SS_FATAL | EACCES,
1812             "Persistent write protect") },
1813         /*  T   R         */
1814         { SST(0x27, 0x05, SS_FATAL | EACCES,
1815             "Permanent write protect") },
1816         /*      R       F */
1817         { SST(0x27, 0x06, SS_RDEF,      /* XXX TBD */
1818             "Conditional write protect") },
1819         /* D         B    */
1820         { SST(0x27, 0x07, SS_FATAL | ENOSPC,
1821             "Space allocation failed write protect") },
1822         /* D              */
1823         { SST(0x27, 0x08, SS_FATAL | EACCES,
1824             "Zone is read only") },
1825         /* DTLPWROMAEBKVF */
1826         { SST(0x28, 0x00, SS_FATAL | ENXIO,
1827             "Not ready to ready change, medium may have changed") },
1828         /* DT  WROM  B    */
1829         { SST(0x28, 0x01, SS_FATAL | ENXIO,
1830             "Import or export element accessed") },
1831         /*      R         */
1832         { SST(0x28, 0x02, SS_RDEF,      /* XXX TBD */
1833             "Format-layer may have changed") },
1834         /*        M       */
1835         { SST(0x28, 0x03, SS_RDEF,      /* XXX TBD */
1836             "Import/export element accessed, medium changed") },
1837         /*
1838          * XXX JGibbs - All of these should use the same errno, but I don't
1839          * think ENXIO is the correct choice.  Should we borrow from
1840          * the networking errnos?  ECONNRESET anyone?
1841          */
1842         /* DTLPWROMAEBKVF */
1843         { SST(0x29, 0x00, SS_FATAL | ENXIO,
1844             "Power on, reset, or bus device reset occurred") },
1845         /* DTLPWROMAEBKVF */
1846         { SST(0x29, 0x01, SS_RDEF,
1847             "Power on occurred") },
1848         /* DTLPWROMAEBKVF */
1849         { SST(0x29, 0x02, SS_RDEF,
1850             "SCSI bus reset occurred") },
1851         /* DTLPWROMAEBKVF */
1852         { SST(0x29, 0x03, SS_RDEF,
1853             "Bus device reset function occurred") },
1854         /* DTLPWROMAEBKVF */
1855         { SST(0x29, 0x04, SS_RDEF,
1856             "Device internal reset") },
1857         /* DTLPWROMAEBKVF */
1858         { SST(0x29, 0x05, SS_RDEF,
1859             "Transceiver mode changed to single-ended") },
1860         /* DTLPWROMAEBKVF */
1861         { SST(0x29, 0x06, SS_RDEF,
1862             "Transceiver mode changed to LVD") },
1863         /* DTLPWROMAEBKVF */
1864         { SST(0x29, 0x07, SS_RDEF,      /* XXX TBD */
1865             "I_T nexus loss occurred") },
1866         /* DTL WROMAEBKVF */
1867         { SST(0x2A, 0x00, SS_RDEF,
1868             "Parameters changed") },
1869         /* DTL WROMAEBKVF */
1870         { SST(0x2A, 0x01, SS_RDEF,
1871             "Mode parameters changed") },
1872         /* DTL WROMAE K   */
1873         { SST(0x2A, 0x02, SS_RDEF,
1874             "Log parameters changed") },
1875         /* DTLPWROMAE K   */
1876         { SST(0x2A, 0x03, SS_RDEF,
1877             "Reservations preempted") },
1878         /* DTLPWROMAE     */
1879         { SST(0x2A, 0x04, SS_RDEF,      /* XXX TBD */
1880             "Reservations released") },
1881         /* DTLPWROMAE     */
1882         { SST(0x2A, 0x05, SS_RDEF,      /* XXX TBD */
1883             "Registrations preempted") },
1884         /* DTLPWROMAEBKVF */
1885         { SST(0x2A, 0x06, SS_RDEF,      /* XXX TBD */
1886             "Asymmetric access state changed") },
1887         /* DTLPWROMAEBKVF */
1888         { SST(0x2A, 0x07, SS_RDEF,      /* XXX TBD */
1889             "Implicit asymmetric access state transition failed") },
1890         /* DT  WROMAEBKVF */
1891         { SST(0x2A, 0x08, SS_RDEF,      /* XXX TBD */
1892             "Priority changed") },
1893         /* D              */
1894         { SST(0x2A, 0x09, SS_RDEF,      /* XXX TBD */
1895             "Capacity data has changed") },
1896         /* DT             */
1897         { SST(0x2A, 0x0A, SS_RDEF,      /* XXX TBD */
1898             "Error history I_T nexus cleared") },
1899         /* DT             */
1900         { SST(0x2A, 0x0B, SS_RDEF,      /* XXX TBD */
1901             "Error history snapshot released") },
1902         /*              F */
1903         { SST(0x2A, 0x0C, SS_RDEF,      /* XXX TBD */
1904             "Error recovery attributes have changed") },
1905         /*  T             */
1906         { SST(0x2A, 0x0D, SS_RDEF,      /* XXX TBD */
1907             "Data encryption capabilities changed") },
1908         /* DT     M E  V  */
1909         { SST(0x2A, 0x10, SS_RDEF,      /* XXX TBD */
1910             "Timestamp changed") },
1911         /*  T             */
1912         { SST(0x2A, 0x11, SS_RDEF,      /* XXX TBD */
1913             "Data encryption parameters changed by another I_T nexus") },
1914         /*  T             */
1915         { SST(0x2A, 0x12, SS_RDEF,      /* XXX TBD */
1916             "Data encryption parameters changed by vendor specific event") },
1917         /*  T             */
1918         { SST(0x2A, 0x13, SS_RDEF,      /* XXX TBD */
1919             "Data encryption key instance counter has changed") },
1920         /* DT   R MAEBKV  */
1921         { SST(0x2A, 0x14, SS_RDEF,      /* XXX TBD */
1922             "SA creation capabilities data has changed") },
1923         /*  T     M    V  */
1924         { SST(0x2A, 0x15, SS_RDEF,      /* XXX TBD */
1925             "Medium removal prevention preempted") },
1926         /* DTLPWRO    K   */
1927         { SST(0x2B, 0x00, SS_RDEF,
1928             "Copy cannot execute since host cannot disconnect") },
1929         /* DTLPWROMAEBKVF */
1930         { SST(0x2C, 0x00, SS_RDEF,
1931             "Command sequence error") },
1932         /*                */
1933         { SST(0x2C, 0x01, SS_RDEF,
1934             "Too many windows specified") },
1935         /*                */
1936         { SST(0x2C, 0x02, SS_RDEF,
1937             "Invalid combination of windows specified") },
1938         /*      R         */
1939         { SST(0x2C, 0x03, SS_RDEF,
1940             "Current program area is not empty") },
1941         /*      R         */
1942         { SST(0x2C, 0x04, SS_RDEF,
1943             "Current program area is empty") },
1944         /*           B    */
1945         { SST(0x2C, 0x05, SS_RDEF,      /* XXX TBD */
1946             "Illegal power condition request") },
1947         /*      R         */
1948         { SST(0x2C, 0x06, SS_RDEF,      /* XXX TBD */
1949             "Persistent prevent conflict") },
1950         /* DTLPWROMAEBKVF */
1951         { SST(0x2C, 0x07, SS_RDEF,      /* XXX TBD */
1952             "Previous busy status") },
1953         /* DTLPWROMAEBKVF */
1954         { SST(0x2C, 0x08, SS_RDEF,      /* XXX TBD */
1955             "Previous task set full status") },
1956         /* DTLPWROM EBKVF */
1957         { SST(0x2C, 0x09, SS_RDEF,      /* XXX TBD */
1958             "Previous reservation conflict status") },
1959         /*              F */
1960         { SST(0x2C, 0x0A, SS_RDEF,      /* XXX TBD */
1961             "Partition or collection contains user objects") },
1962         /*  T             */
1963         { SST(0x2C, 0x0B, SS_RDEF,      /* XXX TBD */
1964             "Not reserved") },
1965         /* D              */
1966         { SST(0x2C, 0x0C, SS_RDEF,      /* XXX TBD */
1967             "ORWRITE generation does not match") },
1968         /* D              */
1969         { SST(0x2C, 0x0D, SS_RDEF,      /* XXX TBD */
1970             "Reset write pointer not allowed") },
1971         /* D              */
1972         { SST(0x2C, 0x0E, SS_RDEF,      /* XXX TBD */
1973             "Zone is offline") },
1974         /* D              */
1975         { SST(0x2C, 0x0F, SS_RDEF,      /* XXX TBD */
1976             "Stream not open") },
1977         /* D              */
1978         { SST(0x2C, 0x10, SS_RDEF,      /* XXX TBD */
1979             "Unwritten data in zone") },
1980         /*  T             */
1981         { SST(0x2D, 0x00, SS_RDEF,
1982             "Overwrite error on update in place") },
1983         /*      R         */
1984         { SST(0x2E, 0x00, SS_RDEF,      /* XXX TBD */
1985             "Insufficient time for operation") },
1986         /* D              */
1987         { SST(0x2E, 0x01, SS_RDEF,      /* XXX TBD */
1988             "Command timeout before processing") },
1989         /* D              */
1990         { SST(0x2E, 0x02, SS_RDEF,      /* XXX TBD */
1991             "Command timeout during processing") },
1992         /* D              */
1993         { SST(0x2E, 0x03, SS_RDEF,      /* XXX TBD */
1994             "Command timeout during processing due to error recovery") },
1995         /* DTLPWROMAEBKVF */
1996         { SST(0x2F, 0x00, SS_RDEF,
1997             "Commands cleared by another initiator") },
1998         /* D              */
1999         { SST(0x2F, 0x01, SS_RDEF,      /* XXX TBD */
2000             "Commands cleared by power loss notification") },
2001         /* DTLPWROMAEBKVF */
2002         { SST(0x2F, 0x02, SS_RDEF,      /* XXX TBD */
2003             "Commands cleared by device server") },
2004         /* DTLPWROMAEBKVF */
2005         { SST(0x2F, 0x03, SS_RDEF,      /* XXX TBD */
2006             "Some commands cleared by queuing layer event") },
2007         /* DT  WROM  BK   */
2008         { SST(0x30, 0x00, SS_RDEF,
2009             "Incompatible medium installed") },
2010         /* DT  WRO   BK   */
2011         { SST(0x30, 0x01, SS_RDEF,
2012             "Cannot read medium - unknown format") },
2013         /* DT  WRO   BK   */
2014         { SST(0x30, 0x02, SS_RDEF,
2015             "Cannot read medium - incompatible format") },
2016         /* DT   R     K   */
2017         { SST(0x30, 0x03, SS_RDEF,
2018             "Cleaning cartridge installed") },
2019         /* DT  WRO   BK   */
2020         { SST(0x30, 0x04, SS_RDEF,
2021             "Cannot write medium - unknown format") },
2022         /* DT  WRO   BK   */
2023         { SST(0x30, 0x05, SS_RDEF,
2024             "Cannot write medium - incompatible format") },
2025         /* DT  WRO   B    */
2026         { SST(0x30, 0x06, SS_RDEF,
2027             "Cannot format medium - incompatible medium") },
2028         /* DTL WROMAEBKVF */
2029         { SST(0x30, 0x07, SS_RDEF,
2030             "Cleaning failure") },
2031         /*      R         */
2032         { SST(0x30, 0x08, SS_RDEF,
2033             "Cannot write - application code mismatch") },
2034         /*      R         */
2035         { SST(0x30, 0x09, SS_RDEF,
2036             "Current session not fixated for append") },
2037         /* DT  WRO AEBK   */
2038         { SST(0x30, 0x0A, SS_RDEF,      /* XXX TBD */
2039             "Cleaning request rejected") },
2040         /*  T             */
2041         { SST(0x30, 0x0C, SS_RDEF,      /* XXX TBD */
2042             "WORM medium - overwrite attempted") },
2043         /*  T             */
2044         { SST(0x30, 0x0D, SS_RDEF,      /* XXX TBD */
2045             "WORM medium - integrity check") },
2046         /*      R         */
2047         { SST(0x30, 0x10, SS_RDEF,      /* XXX TBD */
2048             "Medium not formatted") },
2049         /*        M       */
2050         { SST(0x30, 0x11, SS_RDEF,      /* XXX TBD */
2051             "Incompatible volume type") },
2052         /*        M       */
2053         { SST(0x30, 0x12, SS_RDEF,      /* XXX TBD */
2054             "Incompatible volume qualifier") },
2055         /*        M       */
2056         { SST(0x30, 0x13, SS_RDEF,      /* XXX TBD */
2057             "Cleaning volume expired") },
2058         /* DT  WRO   BK   */
2059         { SST(0x31, 0x00, SS_RDEF,
2060             "Medium format corrupted") },
2061         /* D L  RO   B    */
2062         { SST(0x31, 0x01, SS_RDEF,
2063             "Format command failed") },
2064         /*      R         */
2065         { SST(0x31, 0x02, SS_RDEF,      /* XXX TBD */
2066             "Zoned formatting failed due to spare linking") },
2067         /* D         B    */
2068         { SST(0x31, 0x03, SS_RDEF,      /* XXX TBD */
2069             "SANITIZE command failed") },
2070         /* D   W O   BK   */
2071         { SST(0x32, 0x00, SS_RDEF,
2072             "No defect spare location available") },
2073         /* D   W O   BK   */
2074         { SST(0x32, 0x01, SS_RDEF,
2075             "Defect list update failure") },
2076         /*  T             */
2077         { SST(0x33, 0x00, SS_RDEF,
2078             "Tape length error") },
2079         /* DTLPWROMAEBKVF */
2080         { SST(0x34, 0x00, SS_RDEF,
2081             "Enclosure failure") },
2082         /* DTLPWROMAEBKVF */
2083         { SST(0x35, 0x00, SS_RDEF,
2084             "Enclosure services failure") },
2085         /* DTLPWROMAEBKVF */
2086         { SST(0x35, 0x01, SS_RDEF,
2087             "Unsupported enclosure function") },
2088         /* DTLPWROMAEBKVF */
2089         { SST(0x35, 0x02, SS_RDEF,
2090             "Enclosure services unavailable") },
2091         /* DTLPWROMAEBKVF */
2092         { SST(0x35, 0x03, SS_RDEF,
2093             "Enclosure services transfer failure") },
2094         /* DTLPWROMAEBKVF */
2095         { SST(0x35, 0x04, SS_RDEF,
2096             "Enclosure services transfer refused") },
2097         /* DTL WROMAEBKVF */
2098         { SST(0x35, 0x05, SS_RDEF,      /* XXX TBD */
2099             "Enclosure services checksum error") },
2100         /*   L            */
2101         { SST(0x36, 0x00, SS_RDEF,
2102             "Ribbon, ink, or toner failure") },
2103         /* DTL WROMAEBKVF */
2104         { SST(0x37, 0x00, SS_RDEF,
2105             "Rounded parameter") },
2106         /*           B    */
2107         { SST(0x38, 0x00, SS_RDEF,      /* XXX TBD */
2108             "Event status notification") },
2109         /*           B    */
2110         { SST(0x38, 0x02, SS_RDEF,      /* XXX TBD */
2111             "ESN - power management class event") },
2112         /*           B    */
2113         { SST(0x38, 0x04, SS_RDEF,      /* XXX TBD */
2114             "ESN - media class event") },
2115         /*           B    */
2116         { SST(0x38, 0x06, SS_RDEF,      /* XXX TBD */
2117             "ESN - device busy class event") },
2118         /* D              */
2119         { SST(0x38, 0x07, SS_RDEF,      /* XXX TBD */
2120             "Thin provisioning soft threshold reached") },
2121         /* DTL WROMAE K   */
2122         { SST(0x39, 0x00, SS_RDEF,
2123             "Saving parameters not supported") },
2124         /* DTL WROM  BK   */
2125         { SST(0x3A, 0x00, SS_FATAL | ENXIO,
2126             "Medium not present") },
2127         /* DT  WROM  BK   */
2128         { SST(0x3A, 0x01, SS_FATAL | ENXIO,
2129             "Medium not present - tray closed") },
2130         /* DT  WROM  BK   */
2131         { SST(0x3A, 0x02, SS_FATAL | ENXIO,
2132             "Medium not present - tray open") },
2133         /* DT  WROM  B    */
2134         { SST(0x3A, 0x03, SS_RDEF,      /* XXX TBD */
2135             "Medium not present - loadable") },
2136         /* DT  WRO   B    */
2137         { SST(0x3A, 0x04, SS_RDEF,      /* XXX TBD */
2138             "Medium not present - medium auxiliary memory accessible") },
2139         /*  TL            */
2140         { SST(0x3B, 0x00, SS_RDEF,
2141             "Sequential positioning error") },
2142         /*  T             */
2143         { SST(0x3B, 0x01, SS_RDEF,
2144             "Tape position error at beginning-of-medium") },
2145         /*  T             */
2146         { SST(0x3B, 0x02, SS_RDEF,
2147             "Tape position error at end-of-medium") },
2148         /*   L            */
2149         { SST(0x3B, 0x03, SS_RDEF,
2150             "Tape or electronic vertical forms unit not ready") },
2151         /*   L            */
2152         { SST(0x3B, 0x04, SS_RDEF,
2153             "Slew failure") },
2154         /*   L            */
2155         { SST(0x3B, 0x05, SS_RDEF,
2156             "Paper jam") },
2157         /*   L            */
2158         { SST(0x3B, 0x06, SS_RDEF,
2159             "Failed to sense top-of-form") },
2160         /*   L            */
2161         { SST(0x3B, 0x07, SS_RDEF,
2162             "Failed to sense bottom-of-form") },
2163         /*  T             */
2164         { SST(0x3B, 0x08, SS_RDEF,
2165             "Reposition error") },
2166         /*                */
2167         { SST(0x3B, 0x09, SS_RDEF,
2168             "Read past end of medium") },
2169         /*                */
2170         { SST(0x3B, 0x0A, SS_RDEF,
2171             "Read past beginning of medium") },
2172         /*                */
2173         { SST(0x3B, 0x0B, SS_RDEF,
2174             "Position past end of medium") },
2175         /*  T             */
2176         { SST(0x3B, 0x0C, SS_RDEF,
2177             "Position past beginning of medium") },
2178         /* DT  WROM  BK   */
2179         { SST(0x3B, 0x0D, SS_FATAL | ENOSPC,
2180             "Medium destination element full") },
2181         /* DT  WROM  BK   */
2182         { SST(0x3B, 0x0E, SS_RDEF,
2183             "Medium source element empty") },
2184         /*      R         */
2185         { SST(0x3B, 0x0F, SS_RDEF,
2186             "End of medium reached") },
2187         /* DT  WROM  BK   */
2188         { SST(0x3B, 0x11, SS_RDEF,
2189             "Medium magazine not accessible") },
2190         /* DT  WROM  BK   */
2191         { SST(0x3B, 0x12, SS_RDEF,
2192             "Medium magazine removed") },
2193         /* DT  WROM  BK   */
2194         { SST(0x3B, 0x13, SS_RDEF,
2195             "Medium magazine inserted") },
2196         /* DT  WROM  BK   */
2197         { SST(0x3B, 0x14, SS_RDEF,
2198             "Medium magazine locked") },
2199         /* DT  WROM  BK   */
2200         { SST(0x3B, 0x15, SS_RDEF,
2201             "Medium magazine unlocked") },
2202         /*      R         */
2203         { SST(0x3B, 0x16, SS_RDEF,      /* XXX TBD */
2204             "Mechanical positioning or changer error") },
2205         /*              F */
2206         { SST(0x3B, 0x17, SS_RDEF,      /* XXX TBD */
2207             "Read past end of user object") },
2208         /*        M       */
2209         { SST(0x3B, 0x18, SS_RDEF,      /* XXX TBD */
2210             "Element disabled") },
2211         /*        M       */
2212         { SST(0x3B, 0x19, SS_RDEF,      /* XXX TBD */
2213             "Element enabled") },
2214         /*        M       */
2215         { SST(0x3B, 0x1A, SS_RDEF,      /* XXX TBD */
2216             "Data transfer device removed") },
2217         /*        M       */
2218         { SST(0x3B, 0x1B, SS_RDEF,      /* XXX TBD */
2219             "Data transfer device inserted") },
2220         /*  T             */
2221         { SST(0x3B, 0x1C, SS_RDEF,      /* XXX TBD */
2222             "Too many logical objects on partition to support operation") },
2223         /* DTLPWROMAE K   */
2224         { SST(0x3D, 0x00, SS_RDEF,
2225             "Invalid bits in IDENTIFY message") },
2226         /* DTLPWROMAEBKVF */
2227         { SST(0x3E, 0x00, SS_RDEF,
2228             "Logical unit has not self-configured yet") },
2229         /* DTLPWROMAEBKVF */
2230         { SST(0x3E, 0x01, SS_RDEF,
2231             "Logical unit failure") },
2232         /* DTLPWROMAEBKVF */
2233         { SST(0x3E, 0x02, SS_RDEF,
2234             "Timeout on logical unit") },
2235         /* DTLPWROMAEBKVF */
2236         { SST(0x3E, 0x03, SS_RDEF,      /* XXX TBD */
2237             "Logical unit failed self-test") },
2238         /* DTLPWROMAEBKVF */
2239         { SST(0x3E, 0x04, SS_RDEF,      /* XXX TBD */
2240             "Logical unit unable to update self-test log") },
2241         /* DTLPWROMAEBKVF */
2242         { SST(0x3F, 0x00, SS_RDEF,
2243             "Target operating conditions have changed") },
2244         /* DTLPWROMAEBKVF */
2245         { SST(0x3F, 0x01, SS_RDEF,
2246             "Microcode has been changed") },
2247         /* DTLPWROM  BK   */
2248         { SST(0x3F, 0x02, SS_RDEF,
2249             "Changed operating definition") },
2250         /* DTLPWROMAEBKVF */
2251         { SST(0x3F, 0x03, SS_RDEF,
2252             "INQUIRY data has changed") },
2253         /* DT  WROMAEBK   */
2254         { SST(0x3F, 0x04, SS_RDEF,
2255             "Component device attached") },
2256         /* DT  WROMAEBK   */
2257         { SST(0x3F, 0x05, SS_RDEF,
2258             "Device identifier changed") },
2259         /* DT  WROMAEB    */
2260         { SST(0x3F, 0x06, SS_RDEF,
2261             "Redundancy group created or modified") },
2262         /* DT  WROMAEB    */
2263         { SST(0x3F, 0x07, SS_RDEF,
2264             "Redundancy group deleted") },
2265         /* DT  WROMAEB    */
2266         { SST(0x3F, 0x08, SS_RDEF,
2267             "Spare created or modified") },
2268         /* DT  WROMAEB    */
2269         { SST(0x3F, 0x09, SS_RDEF,
2270             "Spare deleted") },
2271         /* DT  WROMAEBK   */
2272         { SST(0x3F, 0x0A, SS_RDEF,
2273             "Volume set created or modified") },
2274         /* DT  WROMAEBK   */
2275         { SST(0x3F, 0x0B, SS_RDEF,
2276             "Volume set deleted") },
2277         /* DT  WROMAEBK   */
2278         { SST(0x3F, 0x0C, SS_RDEF,
2279             "Volume set deassigned") },
2280         /* DT  WROMAEBK   */
2281         { SST(0x3F, 0x0D, SS_RDEF,
2282             "Volume set reassigned") },
2283         /* DTLPWROMAE     */
2284         { SST(0x3F, 0x0E, SS_RDEF | SSQ_RESCAN ,
2285             "Reported LUNs data has changed") },
2286         /* DTLPWROMAEBKVF */
2287         { SST(0x3F, 0x0F, SS_RDEF,      /* XXX TBD */
2288             "Echo buffer overwritten") },
2289         /* DT  WROM  B    */
2290         { SST(0x3F, 0x10, SS_RDEF,      /* XXX TBD */
2291             "Medium loadable") },
2292         /* DT  WROM  B    */
2293         { SST(0x3F, 0x11, SS_RDEF,      /* XXX TBD */
2294             "Medium auxiliary memory accessible") },
2295         /* DTLPWR MAEBK F */
2296         { SST(0x3F, 0x12, SS_RDEF,      /* XXX TBD */
2297             "iSCSI IP address added") },
2298         /* DTLPWR MAEBK F */
2299         { SST(0x3F, 0x13, SS_RDEF,      /* XXX TBD */
2300             "iSCSI IP address removed") },
2301         /* DTLPWR MAEBK F */
2302         { SST(0x3F, 0x14, SS_RDEF,      /* XXX TBD */
2303             "iSCSI IP address changed") },
2304         /* DTLPWR MAEBK   */
2305         { SST(0x3F, 0x15, SS_RDEF,      /* XXX TBD */
2306             "Inspect referrals sense descriptors") },
2307         /* DTLPWROMAEBKVF */
2308         { SST(0x3F, 0x16, SS_RDEF,      /* XXX TBD */
2309             "Microcode has been changed without reset") },
2310         /* D              */
2311         { SST(0x3F, 0x17, SS_RDEF,      /* XXX TBD */
2312             "Zone transition to full") },
2313         /* D              */
2314         { SST(0x40, 0x00, SS_RDEF,
2315             "RAM failure") },           /* deprecated - use 40 NN instead */
2316         /* DTLPWROMAEBKVF */
2317         { SST(0x40, 0x80, SS_RDEF,
2318             "Diagnostic failure: ASCQ = Component ID") },
2319         /* DTLPWROMAEBKVF */
2320         { SST(0x40, 0xFF, SS_RDEF | SSQ_RANGE,
2321             NULL) },                    /* Range 0x80->0xFF */
2322         /* D              */
2323         { SST(0x41, 0x00, SS_RDEF,
2324             "Data path failure") },     /* deprecated - use 40 NN instead */
2325         /* D              */
2326         { SST(0x42, 0x00, SS_RDEF,
2327             "Power-on or self-test failure") },
2328                                         /* deprecated - use 40 NN instead */
2329         /* DTLPWROMAEBKVF */
2330         { SST(0x43, 0x00, SS_RDEF,
2331             "Message error") },
2332         /* DTLPWROMAEBKVF */
2333         { SST(0x44, 0x00, SS_RDEF,
2334             "Internal target failure") },
2335         /* DT P   MAEBKVF */
2336         { SST(0x44, 0x01, SS_RDEF,      /* XXX TBD */
2337             "Persistent reservation information lost") },
2338         /* DT        B    */
2339         { SST(0x44, 0x71, SS_RDEF,      /* XXX TBD */
2340             "ATA device failed set features") },
2341         /* DTLPWROMAEBKVF */
2342         { SST(0x45, 0x00, SS_RDEF,
2343             "Select or reselect failure") },
2344         /* DTLPWROM  BK   */
2345         { SST(0x46, 0x00, SS_RDEF,
2346             "Unsuccessful soft reset") },
2347         /* DTLPWROMAEBKVF */
2348         { SST(0x47, 0x00, SS_RDEF,
2349             "SCSI parity error") },
2350         /* DTLPWROMAEBKVF */
2351         { SST(0x47, 0x01, SS_RDEF,      /* XXX TBD */
2352             "Data phase CRC error detected") },
2353         /* DTLPWROMAEBKVF */
2354         { SST(0x47, 0x02, SS_RDEF,      /* XXX TBD */
2355             "SCSI parity error detected during ST data phase") },
2356         /* DTLPWROMAEBKVF */
2357         { SST(0x47, 0x03, SS_RDEF,      /* XXX TBD */
2358             "Information unit iuCRC error detected") },
2359         /* DTLPWROMAEBKVF */
2360         { SST(0x47, 0x04, SS_RDEF,      /* XXX TBD */
2361             "Asynchronous information protection error detected") },
2362         /* DTLPWROMAEBKVF */
2363         { SST(0x47, 0x05, SS_RDEF,      /* XXX TBD */
2364             "Protocol service CRC error") },
2365         /* DT     MAEBKVF */
2366         { SST(0x47, 0x06, SS_RDEF,      /* XXX TBD */
2367             "PHY test function in progress") },
2368         /* DT PWROMAEBK   */
2369         { SST(0x47, 0x7F, SS_RDEF,      /* XXX TBD */
2370             "Some commands cleared by iSCSI protocol event") },
2371         /* DTLPWROMAEBKVF */
2372         { SST(0x48, 0x00, SS_RDEF,
2373             "Initiator detected error message received") },
2374         /* DTLPWROMAEBKVF */
2375         { SST(0x49, 0x00, SS_RDEF,
2376             "Invalid message error") },
2377         /* DTLPWROMAEBKVF */
2378         { SST(0x4A, 0x00, SS_RDEF,
2379             "Command phase error") },
2380         /* DTLPWROMAEBKVF */
2381         { SST(0x4B, 0x00, SS_RDEF,
2382             "Data phase error") },
2383         /* DT PWROMAEBK   */
2384         { SST(0x4B, 0x01, SS_RDEF,      /* XXX TBD */
2385             "Invalid target port transfer tag received") },
2386         /* DT PWROMAEBK   */
2387         { SST(0x4B, 0x02, SS_RDEF,      /* XXX TBD */
2388             "Too much write data") },
2389         /* DT PWROMAEBK   */
2390         { SST(0x4B, 0x03, SS_RDEF,      /* XXX TBD */
2391             "ACK/NAK timeout") },
2392         /* DT PWROMAEBK   */
2393         { SST(0x4B, 0x04, SS_RDEF,      /* XXX TBD */
2394             "NAK received") },
2395         /* DT PWROMAEBK   */
2396         { SST(0x4B, 0x05, SS_RDEF,      /* XXX TBD */
2397             "Data offset error") },
2398         /* DT PWROMAEBK   */
2399         { SST(0x4B, 0x06, SS_RDEF,      /* XXX TBD */
2400             "Initiator response timeout") },
2401         /* DT PWROMAEBK F */
2402         { SST(0x4B, 0x07, SS_RDEF,      /* XXX TBD */
2403             "Connection lost") },
2404         /* DT PWROMAEBK F */
2405         { SST(0x4B, 0x08, SS_RDEF,      /* XXX TBD */
2406             "Data-in buffer overflow - data buffer size") },
2407         /* DT PWROMAEBK F */
2408         { SST(0x4B, 0x09, SS_RDEF,      /* XXX TBD */
2409             "Data-in buffer overflow - data buffer descriptor area") },
2410         /* DT PWROMAEBK F */
2411         { SST(0x4B, 0x0A, SS_RDEF,      /* XXX TBD */
2412             "Data-in buffer error") },
2413         /* DT PWROMAEBK F */
2414         { SST(0x4B, 0x0B, SS_RDEF,      /* XXX TBD */
2415             "Data-out buffer overflow - data buffer size") },
2416         /* DT PWROMAEBK F */
2417         { SST(0x4B, 0x0C, SS_RDEF,      /* XXX TBD */
2418             "Data-out buffer overflow - data buffer descriptor area") },
2419         /* DT PWROMAEBK F */
2420         { SST(0x4B, 0x0D, SS_RDEF,      /* XXX TBD */
2421             "Data-out buffer error") },
2422         /* DT PWROMAEBK F */
2423         { SST(0x4B, 0x0E, SS_RDEF,      /* XXX TBD */
2424             "PCIe fabric error") },
2425         /* DT PWROMAEBK F */
2426         { SST(0x4B, 0x0F, SS_RDEF,      /* XXX TBD */
2427             "PCIe completion timeout") },
2428         /* DT PWROMAEBK F */
2429         { SST(0x4B, 0x10, SS_RDEF,      /* XXX TBD */
2430             "PCIe completer abort") },
2431         /* DT PWROMAEBK F */
2432         { SST(0x4B, 0x11, SS_RDEF,      /* XXX TBD */
2433             "PCIe poisoned TLP received") },
2434         /* DT PWROMAEBK F */
2435         { SST(0x4B, 0x12, SS_RDEF,      /* XXX TBD */
2436             "PCIe ECRC check failed") },
2437         /* DT PWROMAEBK F */
2438         { SST(0x4B, 0x13, SS_RDEF,      /* XXX TBD */
2439             "PCIe unsupported request") },
2440         /* DT PWROMAEBK F */
2441         { SST(0x4B, 0x14, SS_RDEF,      /* XXX TBD */
2442             "PCIe ACS violation") },
2443         /* DT PWROMAEBK F */
2444         { SST(0x4B, 0x15, SS_RDEF,      /* XXX TBD */
2445             "PCIe TLP prefix blocket") },
2446         /* DTLPWROMAEBKVF */
2447         { SST(0x4C, 0x00, SS_RDEF,
2448             "Logical unit failed self-configuration") },
2449         /* DTLPWROMAEBKVF */
2450         { SST(0x4D, 0x00, SS_RDEF,
2451             "Tagged overlapped commands: ASCQ = Queue tag ID") },
2452         /* DTLPWROMAEBKVF */
2453         { SST(0x4D, 0xFF, SS_RDEF | SSQ_RANGE,
2454             NULL) },                    /* Range 0x00->0xFF */
2455         /* DTLPWROMAEBKVF */
2456         { SST(0x4E, 0x00, SS_RDEF,
2457             "Overlapped commands attempted") },
2458         /*  T             */
2459         { SST(0x50, 0x00, SS_RDEF,
2460             "Write append error") },
2461         /*  T             */
2462         { SST(0x50, 0x01, SS_RDEF,
2463             "Write append position error") },
2464         /*  T             */
2465         { SST(0x50, 0x02, SS_RDEF,
2466             "Position error related to timing") },
2467         /*  T   RO        */
2468         { SST(0x51, 0x00, SS_RDEF,
2469             "Erase failure") },
2470         /*      R         */
2471         { SST(0x51, 0x01, SS_RDEF,      /* XXX TBD */
2472             "Erase failure - incomplete erase operation detected") },
2473         /*  T             */
2474         { SST(0x52, 0x00, SS_RDEF,
2475             "Cartridge fault") },
2476         /* DTL WROM  BK   */
2477         { SST(0x53, 0x00, SS_RDEF,
2478             "Media load or eject failed") },
2479         /*  T             */
2480         { SST(0x53, 0x01, SS_RDEF,
2481             "Unload tape failure") },
2482         /* DT  WROM  BK   */
2483         { SST(0x53, 0x02, SS_RDEF,
2484             "Medium removal prevented") },
2485         /*        M       */
2486         { SST(0x53, 0x03, SS_RDEF,      /* XXX TBD */
2487             "Medium removal prevented by data transfer element") },
2488         /*  T             */
2489         { SST(0x53, 0x04, SS_RDEF,      /* XXX TBD */
2490             "Medium thread or unthread failure") },
2491         /*        M       */
2492         { SST(0x53, 0x05, SS_RDEF,      /* XXX TBD */
2493             "Volume identifier invalid") },
2494         /*  T             */
2495         { SST(0x53, 0x06, SS_RDEF,      /* XXX TBD */
2496             "Volume identifier missing") },
2497         /*        M       */
2498         { SST(0x53, 0x07, SS_RDEF,      /* XXX TBD */
2499             "Duplicate volume identifier") },
2500         /*        M       */
2501         { SST(0x53, 0x08, SS_RDEF,      /* XXX TBD */
2502             "Element status unknown") },
2503         /*        M       */
2504         { SST(0x53, 0x09, SS_RDEF,      /* XXX TBD */
2505             "Data transfer device error - load failed") },
2506         /*        M       */
2507         { SST(0x53, 0x0A, SS_RDEF,      /* XXX TBD */
2508             "Data transfer device error - unload failed") },
2509         /*        M       */
2510         { SST(0x53, 0x0B, SS_RDEF,      /* XXX TBD */
2511             "Data transfer device error - unload missing") },
2512         /*        M       */
2513         { SST(0x53, 0x0C, SS_RDEF,      /* XXX TBD */
2514             "Data transfer device error - eject failed") },
2515         /*        M       */
2516         { SST(0x53, 0x0D, SS_RDEF,      /* XXX TBD */
2517             "Data transfer device error - library communication failed") },
2518         /*    P           */
2519         { SST(0x54, 0x00, SS_RDEF,
2520             "SCSI to host system interface failure") },
2521         /*    P           */
2522         { SST(0x55, 0x00, SS_RDEF,
2523             "System resource failure") },
2524         /* D     O   BK   */
2525         { SST(0x55, 0x01, SS_FATAL | ENOSPC,
2526             "System buffer full") },
2527         /* DTLPWROMAE K   */
2528         { SST(0x55, 0x02, SS_RDEF,      /* XXX TBD */
2529             "Insufficient reservation resources") },
2530         /* DTLPWROMAE K   */
2531         { SST(0x55, 0x03, SS_RDEF,      /* XXX TBD */
2532             "Insufficient resources") },
2533         /* DTLPWROMAE K   */
2534         { SST(0x55, 0x04, SS_RDEF,      /* XXX TBD */
2535             "Insufficient registration resources") },
2536         /* DT PWROMAEBK   */
2537         { SST(0x55, 0x05, SS_RDEF,      /* XXX TBD */
2538             "Insufficient access control resources") },
2539         /* DT  WROM  B    */
2540         { SST(0x55, 0x06, SS_RDEF,      /* XXX TBD */
2541             "Auxiliary memory out of space") },
2542         /*              F */
2543         { SST(0x55, 0x07, SS_RDEF,      /* XXX TBD */
2544             "Quota error") },
2545         /*  T             */
2546         { SST(0x55, 0x08, SS_RDEF,      /* XXX TBD */
2547             "Maximum number of supplemental decryption keys exceeded") },
2548         /*        M       */
2549         { SST(0x55, 0x09, SS_RDEF,      /* XXX TBD */
2550             "Medium auxiliary memory not accessible") },
2551         /*        M       */
2552         { SST(0x55, 0x0A, SS_RDEF,      /* XXX TBD */
2553             "Data currently unavailable") },
2554         /* DTLPWROMAEBKVF */
2555         { SST(0x55, 0x0B, SS_RDEF,      /* XXX TBD */
2556             "Insufficient power for operation") },
2557         /* DT P      B    */
2558         { SST(0x55, 0x0C, SS_RDEF,      /* XXX TBD */
2559             "Insufficient resources to create ROD") },
2560         /* DT P      B    */
2561         { SST(0x55, 0x0D, SS_RDEF,      /* XXX TBD */
2562             "Insufficient resources to create ROD token") },
2563         /* D              */
2564         { SST(0x55, 0x0E, SS_RDEF,      /* XXX TBD */
2565             "Insufficient zone resources") },
2566         /* D              */
2567         { SST(0x55, 0x0F, SS_RDEF,      /* XXX TBD */
2568             "Insufficient zone resources to complete write") },
2569         /* D              */
2570         { SST(0x55, 0x10, SS_RDEF,      /* XXX TBD */
2571             "Maximum number of streams open") },
2572         /*      R         */
2573         { SST(0x57, 0x00, SS_RDEF,
2574             "Unable to recover table-of-contents") },
2575         /*       O        */
2576         { SST(0x58, 0x00, SS_RDEF,
2577             "Generation does not exist") },
2578         /*       O        */
2579         { SST(0x59, 0x00, SS_RDEF,
2580             "Updated block read") },
2581         /* DTLPWRO   BK   */
2582         { SST(0x5A, 0x00, SS_RDEF,
2583             "Operator request or state change input") },
2584         /* DT  WROM  BK   */
2585         { SST(0x5A, 0x01, SS_RDEF,
2586             "Operator medium removal request") },
2587         /* DT  WRO A BK   */
2588         { SST(0x5A, 0x02, SS_RDEF,
2589             "Operator selected write protect") },
2590         /* DT  WRO A BK   */
2591         { SST(0x5A, 0x03, SS_RDEF,
2592             "Operator selected write permit") },
2593         /* DTLPWROM   K   */
2594         { SST(0x5B, 0x00, SS_RDEF,
2595             "Log exception") },
2596         /* DTLPWROM   K   */
2597         { SST(0x5B, 0x01, SS_RDEF,
2598             "Threshold condition met") },
2599         /* DTLPWROM   K   */
2600         { SST(0x5B, 0x02, SS_RDEF,
2601             "Log counter at maximum") },
2602         /* DTLPWROM   K   */
2603         { SST(0x5B, 0x03, SS_RDEF,
2604             "Log list codes exhausted") },
2605         /* D     O        */
2606         { SST(0x5C, 0x00, SS_RDEF,
2607             "RPL status change") },
2608         /* D     O        */
2609         { SST(0x5C, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2610             "Spindles synchronized") },
2611         /* D     O        */
2612         { SST(0x5C, 0x02, SS_RDEF,
2613             "Spindles not synchronized") },
2614         /* DTLPWROMAEBKVF */
2615         { SST(0x5D, 0x00, SS_NOP | SSQ_PRINT_SENSE,
2616             "Failure prediction threshold exceeded") },
2617         /*      R    B    */
2618         { SST(0x5D, 0x01, SS_NOP | SSQ_PRINT_SENSE,
2619             "Media failure prediction threshold exceeded") },
2620         /*      R         */
2621         { SST(0x5D, 0x02, SS_NOP | SSQ_PRINT_SENSE,
2622             "Logical unit failure prediction threshold exceeded") },
2623         /*      R         */
2624         { SST(0x5D, 0x03, SS_NOP | SSQ_PRINT_SENSE,
2625             "Spare area exhaustion prediction threshold exceeded") },
2626         /* D         B    */
2627         { SST(0x5D, 0x10, SS_NOP | SSQ_PRINT_SENSE,
2628             "Hardware impending failure general hard drive failure") },
2629         /* D         B    */
2630         { SST(0x5D, 0x11, SS_NOP | SSQ_PRINT_SENSE,
2631             "Hardware impending failure drive error rate too high") },
2632         /* D         B    */
2633         { SST(0x5D, 0x12, SS_NOP | SSQ_PRINT_SENSE,
2634             "Hardware impending failure data error rate too high") },
2635         /* D         B    */
2636         { SST(0x5D, 0x13, SS_NOP | SSQ_PRINT_SENSE,
2637             "Hardware impending failure seek error rate too high") },
2638         /* D         B    */
2639         { SST(0x5D, 0x14, SS_NOP | SSQ_PRINT_SENSE,
2640             "Hardware impending failure too many block reassigns") },
2641         /* D         B    */
2642         { SST(0x5D, 0x15, SS_NOP | SSQ_PRINT_SENSE,
2643             "Hardware impending failure access times too high") },
2644         /* D         B    */
2645         { SST(0x5D, 0x16, SS_NOP | SSQ_PRINT_SENSE,
2646             "Hardware impending failure start unit times too high") },
2647         /* D         B    */
2648         { SST(0x5D, 0x17, SS_NOP | SSQ_PRINT_SENSE,
2649             "Hardware impending failure channel parametrics") },
2650         /* D         B    */
2651         { SST(0x5D, 0x18, SS_NOP | SSQ_PRINT_SENSE,
2652             "Hardware impending failure controller detected") },
2653         /* D         B    */
2654         { SST(0x5D, 0x19, SS_NOP | SSQ_PRINT_SENSE,
2655             "Hardware impending failure throughput performance") },
2656         /* D         B    */
2657         { SST(0x5D, 0x1A, SS_NOP | SSQ_PRINT_SENSE,
2658             "Hardware impending failure seek time performance") },
2659         /* D         B    */
2660         { SST(0x5D, 0x1B, SS_NOP | SSQ_PRINT_SENSE,
2661             "Hardware impending failure spin-up retry count") },
2662         /* D         B    */
2663         { SST(0x5D, 0x1C, SS_NOP | SSQ_PRINT_SENSE,
2664             "Hardware impending failure drive calibration retry count") },
2665         /* D         B    */
2666         { SST(0x5D, 0x1D, SS_NOP | SSQ_PRINT_SENSE,
2667             "Hardware impending failure power loss protection circuit") },
2668         /* D         B    */
2669         { SST(0x5D, 0x20, SS_NOP | SSQ_PRINT_SENSE,
2670             "Controller impending failure general hard drive failure") },
2671         /* D         B    */
2672         { SST(0x5D, 0x21, SS_NOP | SSQ_PRINT_SENSE,
2673             "Controller impending failure drive error rate too high") },
2674         /* D         B    */
2675         { SST(0x5D, 0x22, SS_NOP | SSQ_PRINT_SENSE,
2676             "Controller impending failure data error rate too high") },
2677         /* D         B    */
2678         { SST(0x5D, 0x23, SS_NOP | SSQ_PRINT_SENSE,
2679             "Controller impending failure seek error rate too high") },
2680         /* D         B    */
2681         { SST(0x5D, 0x24, SS_NOP | SSQ_PRINT_SENSE,
2682             "Controller impending failure too many block reassigns") },
2683         /* D         B    */
2684         { SST(0x5D, 0x25, SS_NOP | SSQ_PRINT_SENSE,
2685             "Controller impending failure access times too high") },
2686         /* D         B    */
2687         { SST(0x5D, 0x26, SS_NOP | SSQ_PRINT_SENSE,
2688             "Controller impending failure start unit times too high") },
2689         /* D         B    */
2690         { SST(0x5D, 0x27, SS_NOP | SSQ_PRINT_SENSE,
2691             "Controller impending failure channel parametrics") },
2692         /* D         B    */
2693         { SST(0x5D, 0x28, SS_NOP | SSQ_PRINT_SENSE,
2694             "Controller impending failure controller detected") },
2695         /* D         B    */
2696         { SST(0x5D, 0x29, SS_NOP | SSQ_PRINT_SENSE,
2697             "Controller impending failure throughput performance") },
2698         /* D         B    */
2699         { SST(0x5D, 0x2A, SS_NOP | SSQ_PRINT_SENSE,
2700             "Controller impending failure seek time performance") },
2701         /* D         B    */
2702         { SST(0x5D, 0x2B, SS_NOP | SSQ_PRINT_SENSE,
2703             "Controller impending failure spin-up retry count") },
2704         /* D         B    */
2705         { SST(0x5D, 0x2C, SS_NOP | SSQ_PRINT_SENSE,
2706             "Controller impending failure drive calibration retry count") },
2707         /* D         B    */
2708         { SST(0x5D, 0x30, SS_NOP | SSQ_PRINT_SENSE,
2709             "Data channel impending failure general hard drive failure") },
2710         /* D         B    */
2711         { SST(0x5D, 0x31, SS_NOP | SSQ_PRINT_SENSE,
2712             "Data channel impending failure drive error rate too high") },
2713         /* D         B    */
2714         { SST(0x5D, 0x32, SS_NOP | SSQ_PRINT_SENSE,
2715             "Data channel impending failure data error rate too high") },
2716         /* D         B    */
2717         { SST(0x5D, 0x33, SS_NOP | SSQ_PRINT_SENSE,
2718             "Data channel impending failure seek error rate too high") },
2719         /* D         B    */
2720         { SST(0x5D, 0x34, SS_NOP | SSQ_PRINT_SENSE,
2721             "Data channel impending failure too many block reassigns") },
2722         /* D         B    */
2723         { SST(0x5D, 0x35, SS_NOP | SSQ_PRINT_SENSE,
2724             "Data channel impending failure access times too high") },
2725         /* D         B    */
2726         { SST(0x5D, 0x36, SS_NOP | SSQ_PRINT_SENSE,
2727             "Data channel impending failure start unit times too high") },
2728         /* D         B    */
2729         { SST(0x5D, 0x37, SS_NOP | SSQ_PRINT_SENSE,
2730             "Data channel impending failure channel parametrics") },
2731         /* D         B    */
2732         { SST(0x5D, 0x38, SS_NOP | SSQ_PRINT_SENSE,
2733             "Data channel impending failure controller detected") },
2734         /* D         B    */
2735         { SST(0x5D, 0x39, SS_NOP | SSQ_PRINT_SENSE,
2736             "Data channel impending failure throughput performance") },
2737         /* D         B    */
2738         { SST(0x5D, 0x3A, SS_NOP | SSQ_PRINT_SENSE,
2739             "Data channel impending failure seek time performance") },
2740         /* D         B    */
2741         { SST(0x5D, 0x3B, SS_NOP | SSQ_PRINT_SENSE,
2742             "Data channel impending failure spin-up retry count") },
2743         /* D         B    */
2744         { SST(0x5D, 0x3C, SS_NOP | SSQ_PRINT_SENSE,
2745             "Data channel impending failure drive calibration retry count") },
2746         /* D         B    */
2747         { SST(0x5D, 0x40, SS_NOP | SSQ_PRINT_SENSE,
2748             "Servo impending failure general hard drive failure") },
2749         /* D         B    */
2750         { SST(0x5D, 0x41, SS_NOP | SSQ_PRINT_SENSE,
2751             "Servo impending failure drive error rate too high") },
2752         /* D         B    */
2753         { SST(0x5D, 0x42, SS_NOP | SSQ_PRINT_SENSE,
2754             "Servo impending failure data error rate too high") },
2755         /* D         B    */
2756         { SST(0x5D, 0x43, SS_NOP | SSQ_PRINT_SENSE,
2757             "Servo impending failure seek error rate too high") },
2758         /* D         B    */
2759         { SST(0x5D, 0x44, SS_NOP | SSQ_PRINT_SENSE,
2760             "Servo impending failure too many block reassigns") },
2761         /* D         B    */
2762         { SST(0x5D, 0x45, SS_NOP | SSQ_PRINT_SENSE,
2763             "Servo impending failure access times too high") },
2764         /* D         B    */
2765         { SST(0x5D, 0x46, SS_NOP | SSQ_PRINT_SENSE,
2766             "Servo impending failure start unit times too high") },
2767         /* D         B    */
2768         { SST(0x5D, 0x47, SS_NOP | SSQ_PRINT_SENSE,
2769             "Servo impending failure channel parametrics") },
2770         /* D         B    */
2771         { SST(0x5D, 0x48, SS_NOP | SSQ_PRINT_SENSE,
2772             "Servo impending failure controller detected") },
2773         /* D         B    */
2774         { SST(0x5D, 0x49, SS_NOP | SSQ_PRINT_SENSE,
2775             "Servo impending failure throughput performance") },
2776         /* D         B    */
2777         { SST(0x5D, 0x4A, SS_NOP | SSQ_PRINT_SENSE,
2778             "Servo impending failure seek time performance") },
2779         /* D         B    */
2780         { SST(0x5D, 0x4B, SS_NOP | SSQ_PRINT_SENSE,
2781             "Servo impending failure spin-up retry count") },
2782         /* D         B    */
2783         { SST(0x5D, 0x4C, SS_NOP | SSQ_PRINT_SENSE,
2784             "Servo impending failure drive calibration retry count") },
2785         /* D         B    */
2786         { SST(0x5D, 0x50, SS_NOP | SSQ_PRINT_SENSE,
2787             "Spindle impending failure general hard drive failure") },
2788         /* D         B    */
2789         { SST(0x5D, 0x51, SS_NOP | SSQ_PRINT_SENSE,
2790             "Spindle impending failure drive error rate too high") },
2791         /* D         B    */
2792         { SST(0x5D, 0x52, SS_NOP | SSQ_PRINT_SENSE,
2793             "Spindle impending failure data error rate too high") },
2794         /* D         B    */
2795         { SST(0x5D, 0x53, SS_NOP | SSQ_PRINT_SENSE,
2796             "Spindle impending failure seek error rate too high") },
2797         /* D         B    */
2798         { SST(0x5D, 0x54, SS_NOP | SSQ_PRINT_SENSE,
2799             "Spindle impending failure too many block reassigns") },
2800         /* D         B    */
2801         { SST(0x5D, 0x55, SS_NOP | SSQ_PRINT_SENSE,
2802             "Spindle impending failure access times too high") },
2803         /* D         B    */
2804         { SST(0x5D, 0x56, SS_NOP | SSQ_PRINT_SENSE,
2805             "Spindle impending failure start unit times too high") },
2806         /* D         B    */
2807         { SST(0x5D, 0x57, SS_NOP | SSQ_PRINT_SENSE,
2808             "Spindle impending failure channel parametrics") },
2809         /* D         B    */
2810         { SST(0x5D, 0x58, SS_NOP | SSQ_PRINT_SENSE,
2811             "Spindle impending failure controller detected") },
2812         /* D         B    */
2813         { SST(0x5D, 0x59, SS_NOP | SSQ_PRINT_SENSE,
2814             "Spindle impending failure throughput performance") },
2815         /* D         B    */
2816         { SST(0x5D, 0x5A, SS_NOP | SSQ_PRINT_SENSE,
2817             "Spindle impending failure seek time performance") },
2818         /* D         B    */
2819         { SST(0x5D, 0x5B, SS_NOP | SSQ_PRINT_SENSE,
2820             "Spindle impending failure spin-up retry count") },
2821         /* D         B    */
2822         { SST(0x5D, 0x5C, SS_NOP | SSQ_PRINT_SENSE,
2823             "Spindle impending failure drive calibration retry count") },
2824         /* D         B    */
2825         { SST(0x5D, 0x60, SS_NOP | SSQ_PRINT_SENSE,
2826             "Firmware impending failure general hard drive failure") },
2827         /* D         B    */
2828         { SST(0x5D, 0x61, SS_NOP | SSQ_PRINT_SENSE,
2829             "Firmware impending failure drive error rate too high") },
2830         /* D         B    */
2831         { SST(0x5D, 0x62, SS_NOP | SSQ_PRINT_SENSE,
2832             "Firmware impending failure data error rate too high") },
2833         /* D         B    */
2834         { SST(0x5D, 0x63, SS_NOP | SSQ_PRINT_SENSE,
2835             "Firmware impending failure seek error rate too high") },
2836         /* D         B    */
2837         { SST(0x5D, 0x64, SS_NOP | SSQ_PRINT_SENSE,
2838             "Firmware impending failure too many block reassigns") },
2839         /* D         B    */
2840         { SST(0x5D, 0x65, SS_NOP | SSQ_PRINT_SENSE,
2841             "Firmware impending failure access times too high") },
2842         /* D         B    */
2843         { SST(0x5D, 0x66, SS_NOP | SSQ_PRINT_SENSE,
2844             "Firmware impending failure start unit times too high") },
2845         /* D         B    */
2846         { SST(0x5D, 0x67, SS_NOP | SSQ_PRINT_SENSE,
2847             "Firmware impending failure channel parametrics") },
2848         /* D         B    */
2849         { SST(0x5D, 0x68, SS_NOP | SSQ_PRINT_SENSE,
2850             "Firmware impending failure controller detected") },
2851         /* D         B    */
2852         { SST(0x5D, 0x69, SS_NOP | SSQ_PRINT_SENSE,
2853             "Firmware impending failure throughput performance") },
2854         /* D         B    */
2855         { SST(0x5D, 0x6A, SS_NOP | SSQ_PRINT_SENSE,
2856             "Firmware impending failure seek time performance") },
2857         /* D         B    */
2858         { SST(0x5D, 0x6B, SS_NOP | SSQ_PRINT_SENSE,
2859             "Firmware impending failure spin-up retry count") },
2860         /* D         B    */
2861         { SST(0x5D, 0x6C, SS_NOP | SSQ_PRINT_SENSE,
2862             "Firmware impending failure drive calibration retry count") },
2863         /* D         B    */
2864         { SST(0x5D, 0x73, SS_NOP | SSQ_PRINT_SENSE,
2865             "Media impending failure endurance limit met") },
2866         /* DTLPWROMAEBKVF */
2867         { SST(0x5D, 0xFF, SS_NOP | SSQ_PRINT_SENSE,
2868             "Failure prediction threshold exceeded (false)") },
2869         /* DTLPWRO A  K   */
2870         { SST(0x5E, 0x00, SS_RDEF,
2871             "Low power condition on") },
2872         /* DTLPWRO A  K   */
2873         { SST(0x5E, 0x01, SS_RDEF,
2874             "Idle condition activated by timer") },
2875         /* DTLPWRO A  K   */
2876         { SST(0x5E, 0x02, SS_RDEF,
2877             "Standby condition activated by timer") },
2878         /* DTLPWRO A  K   */
2879         { SST(0x5E, 0x03, SS_RDEF,
2880             "Idle condition activated by command") },
2881         /* DTLPWRO A  K   */
2882         { SST(0x5E, 0x04, SS_RDEF,
2883             "Standby condition activated by command") },
2884         /* DTLPWRO A  K   */
2885         { SST(0x5E, 0x05, SS_RDEF,
2886             "Idle-B condition activated by timer") },
2887         /* DTLPWRO A  K   */
2888         { SST(0x5E, 0x06, SS_RDEF,
2889             "Idle-B condition activated by command") },
2890         /* DTLPWRO A  K   */
2891         { SST(0x5E, 0x07, SS_RDEF,
2892             "Idle-C condition activated by timer") },
2893         /* DTLPWRO A  K   */
2894         { SST(0x5E, 0x08, SS_RDEF,
2895             "Idle-C condition activated by command") },
2896         /* DTLPWRO A  K   */
2897         { SST(0x5E, 0x09, SS_RDEF,
2898             "Standby-Y condition activated by timer") },
2899         /* DTLPWRO A  K   */
2900         { SST(0x5E, 0x0A, SS_RDEF,
2901             "Standby-Y condition activated by command") },
2902         /*           B    */
2903         { SST(0x5E, 0x41, SS_RDEF,      /* XXX TBD */
2904             "Power state change to active") },
2905         /*           B    */
2906         { SST(0x5E, 0x42, SS_RDEF,      /* XXX TBD */
2907             "Power state change to idle") },
2908         /*           B    */
2909         { SST(0x5E, 0x43, SS_RDEF,      /* XXX TBD */
2910             "Power state change to standby") },
2911         /*           B    */
2912         { SST(0x5E, 0x45, SS_RDEF,      /* XXX TBD */
2913             "Power state change to sleep") },
2914         /*           BK   */
2915         { SST(0x5E, 0x47, SS_RDEF,      /* XXX TBD */
2916             "Power state change to device control") },
2917         /*                */
2918         { SST(0x60, 0x00, SS_RDEF,
2919             "Lamp failure") },
2920         /*                */
2921         { SST(0x61, 0x00, SS_RDEF,
2922             "Video acquisition error") },
2923         /*                */
2924         { SST(0x61, 0x01, SS_RDEF,
2925             "Unable to acquire video") },
2926         /*                */
2927         { SST(0x61, 0x02, SS_RDEF,
2928             "Out of focus") },
2929         /*                */
2930         { SST(0x62, 0x00, SS_RDEF,
2931             "Scan head positioning error") },
2932         /*      R         */
2933         { SST(0x63, 0x00, SS_RDEF,
2934             "End of user area encountered on this track") },
2935         /*      R         */
2936         { SST(0x63, 0x01, SS_FATAL | ENOSPC,
2937             "Packet does not fit in available space") },
2938         /*      R         */
2939         { SST(0x64, 0x00, SS_FATAL | ENXIO,
2940             "Illegal mode for this track") },
2941         /*      R         */
2942         { SST(0x64, 0x01, SS_RDEF,
2943             "Invalid packet size") },
2944         /* DTLPWROMAEBKVF */
2945         { SST(0x65, 0x00, SS_RDEF,
2946             "Voltage fault") },
2947         /*                */
2948         { SST(0x66, 0x00, SS_RDEF,
2949             "Automatic document feeder cover up") },
2950         /*                */
2951         { SST(0x66, 0x01, SS_RDEF,
2952             "Automatic document feeder lift up") },
2953         /*                */
2954         { SST(0x66, 0x02, SS_RDEF,
2955             "Document jam in automatic document feeder") },
2956         /*                */
2957         { SST(0x66, 0x03, SS_RDEF,
2958             "Document miss feed automatic in document feeder") },
2959         /*         A      */
2960         { SST(0x67, 0x00, SS_RDEF,
2961             "Configuration failure") },
2962         /*         A      */
2963         { SST(0x67, 0x01, SS_RDEF,
2964             "Configuration of incapable logical units failed") },
2965         /*         A      */
2966         { SST(0x67, 0x02, SS_RDEF,
2967             "Add logical unit failed") },
2968         /*         A      */
2969         { SST(0x67, 0x03, SS_RDEF,
2970             "Modification of logical unit failed") },
2971         /*         A      */
2972         { SST(0x67, 0x04, SS_RDEF,
2973             "Exchange of logical unit failed") },
2974         /*         A      */
2975         { SST(0x67, 0x05, SS_RDEF,
2976             "Remove of logical unit failed") },
2977         /*         A      */
2978         { SST(0x67, 0x06, SS_RDEF,
2979             "Attachment of logical unit failed") },
2980         /*         A      */
2981         { SST(0x67, 0x07, SS_RDEF,
2982             "Creation of logical unit failed") },
2983         /*         A      */
2984         { SST(0x67, 0x08, SS_RDEF,      /* XXX TBD */
2985             "Assign failure occurred") },
2986         /*         A      */
2987         { SST(0x67, 0x09, SS_RDEF,      /* XXX TBD */
2988             "Multiply assigned logical unit") },
2989         /* DTLPWROMAEBKVF */
2990         { SST(0x67, 0x0A, SS_RDEF,      /* XXX TBD */
2991             "Set target port groups command failed") },
2992         /* DT        B    */
2993         { SST(0x67, 0x0B, SS_RDEF,      /* XXX TBD */
2994             "ATA device feature not enabled") },
2995         /*         A      */
2996         { SST(0x68, 0x00, SS_RDEF,
2997             "Logical unit not configured") },
2998         /* D              */
2999         { SST(0x68, 0x01, SS_RDEF,
3000             "Subsidiary logical unit not configured") },
3001         /*         A      */
3002         { SST(0x69, 0x00, SS_RDEF,
3003             "Data loss on logical unit") },
3004         /*         A      */
3005         { SST(0x69, 0x01, SS_RDEF,
3006             "Multiple logical unit failures") },
3007         /*         A      */
3008         { SST(0x69, 0x02, SS_RDEF,
3009             "Parity/data mismatch") },
3010         /*         A      */
3011         { SST(0x6A, 0x00, SS_RDEF,
3012             "Informational, refer to log") },
3013         /*         A      */
3014         { SST(0x6B, 0x00, SS_RDEF,
3015             "State change has occurred") },
3016         /*         A      */
3017         { SST(0x6B, 0x01, SS_RDEF,
3018             "Redundancy level got better") },
3019         /*         A      */
3020         { SST(0x6B, 0x02, SS_RDEF,
3021             "Redundancy level got worse") },
3022         /*         A      */
3023         { SST(0x6C, 0x00, SS_RDEF,
3024             "Rebuild failure occurred") },
3025         /*         A      */
3026         { SST(0x6D, 0x00, SS_RDEF,
3027             "Recalculate failure occurred") },
3028         /*         A      */
3029         { SST(0x6E, 0x00, SS_RDEF,
3030             "Command to logical unit failed") },
3031         /*      R         */
3032         { SST(0x6F, 0x00, SS_RDEF,      /* XXX TBD */
3033             "Copy protection key exchange failure - authentication failure") },
3034         /*      R         */
3035         { SST(0x6F, 0x01, SS_RDEF,      /* XXX TBD */
3036             "Copy protection key exchange failure - key not present") },
3037         /*      R         */
3038         { SST(0x6F, 0x02, SS_RDEF,      /* XXX TBD */
3039             "Copy protection key exchange failure - key not established") },
3040         /*      R         */
3041         { SST(0x6F, 0x03, SS_RDEF,      /* XXX TBD */
3042             "Read of scrambled sector without authentication") },
3043         /*      R         */
3044         { SST(0x6F, 0x04, SS_RDEF,      /* XXX TBD */
3045             "Media region code is mismatched to logical unit region") },
3046         /*      R         */
3047         { SST(0x6F, 0x05, SS_RDEF,      /* XXX TBD */
3048             "Drive region must be permanent/region reset count error") },
3049         /*      R         */
3050         { SST(0x6F, 0x06, SS_RDEF,      /* XXX TBD */
3051             "Insufficient block count for binding NONCE recording") },
3052         /*      R         */
3053         { SST(0x6F, 0x07, SS_RDEF,      /* XXX TBD */
3054             "Conflict in binding NONCE recording") },
3055         /*  T             */
3056         { SST(0x70, 0x00, SS_RDEF,
3057             "Decompression exception short: ASCQ = Algorithm ID") },
3058         /*  T             */
3059         { SST(0x70, 0xFF, SS_RDEF | SSQ_RANGE,
3060             NULL) },                    /* Range 0x00 -> 0xFF */
3061         /*  T             */
3062         { SST(0x71, 0x00, SS_RDEF,
3063             "Decompression exception long: ASCQ = Algorithm ID") },
3064         /*  T             */
3065         { SST(0x71, 0xFF, SS_RDEF | SSQ_RANGE,
3066             NULL) },                    /* Range 0x00 -> 0xFF */
3067         /*      R         */
3068         { SST(0x72, 0x00, SS_RDEF,
3069             "Session fixation error") },
3070         /*      R         */
3071         { SST(0x72, 0x01, SS_RDEF,
3072             "Session fixation error writing lead-in") },
3073         /*      R         */
3074         { SST(0x72, 0x02, SS_RDEF,
3075             "Session fixation error writing lead-out") },
3076         /*      R         */
3077         { SST(0x72, 0x03, SS_RDEF,
3078             "Session fixation error - incomplete track in session") },
3079         /*      R         */
3080         { SST(0x72, 0x04, SS_RDEF,
3081             "Empty or partially written reserved track") },
3082         /*      R         */
3083         { SST(0x72, 0x05, SS_RDEF,      /* XXX TBD */
3084             "No more track reservations allowed") },
3085         /*      R         */
3086         { SST(0x72, 0x06, SS_RDEF,      /* XXX TBD */
3087             "RMZ extension is not allowed") },
3088         /*      R         */
3089         { SST(0x72, 0x07, SS_RDEF,      /* XXX TBD */
3090             "No more test zone extensions are allowed") },
3091         /*      R         */
3092         { SST(0x73, 0x00, SS_RDEF,
3093             "CD control error") },
3094         /*      R         */
3095         { SST(0x73, 0x01, SS_RDEF,
3096             "Power calibration area almost full") },
3097         /*      R         */
3098         { SST(0x73, 0x02, SS_FATAL | ENOSPC,
3099             "Power calibration area is full") },
3100         /*      R         */
3101         { SST(0x73, 0x03, SS_RDEF,
3102             "Power calibration area error") },
3103         /*      R         */
3104         { SST(0x73, 0x04, SS_RDEF,
3105             "Program memory area update failure") },
3106         /*      R         */
3107         { SST(0x73, 0x05, SS_RDEF,
3108             "Program memory area is full") },
3109         /*      R         */
3110         { SST(0x73, 0x06, SS_RDEF,      /* XXX TBD */
3111             "RMA/PMA is almost full") },
3112         /*      R         */
3113         { SST(0x73, 0x10, SS_RDEF,      /* XXX TBD */
3114             "Current power calibration area almost full") },
3115         /*      R         */
3116         { SST(0x73, 0x11, SS_RDEF,      /* XXX TBD */
3117             "Current power calibration area is full") },
3118         /*      R         */
3119         { SST(0x73, 0x17, SS_RDEF,      /* XXX TBD */
3120             "RDZ is full") },
3121         /*  T             */
3122         { SST(0x74, 0x00, SS_RDEF,      /* XXX TBD */
3123             "Security error") },
3124         /*  T             */
3125         { SST(0x74, 0x01, SS_RDEF,      /* XXX TBD */
3126             "Unable to decrypt data") },
3127         /*  T             */
3128         { SST(0x74, 0x02, SS_RDEF,      /* XXX TBD */
3129             "Unencrypted data encountered while decrypting") },
3130         /*  T             */
3131         { SST(0x74, 0x03, SS_RDEF,      /* XXX TBD */
3132             "Incorrect data encryption key") },
3133         /*  T             */
3134         { SST(0x74, 0x04, SS_RDEF,      /* XXX TBD */
3135             "Cryptographic integrity validation failed") },
3136         /*  T             */
3137         { SST(0x74, 0x05, SS_RDEF,      /* XXX TBD */
3138             "Error decrypting data") },
3139         /*  T             */
3140         { SST(0x74, 0x06, SS_RDEF,      /* XXX TBD */
3141             "Unknown signature verification key") },
3142         /*  T             */
3143         { SST(0x74, 0x07, SS_RDEF,      /* XXX TBD */
3144             "Encryption parameters not useable") },
3145         /* DT   R M E  VF */
3146         { SST(0x74, 0x08, SS_RDEF,      /* XXX TBD */
3147             "Digital signature validation failure") },
3148         /*  T             */
3149         { SST(0x74, 0x09, SS_RDEF,      /* XXX TBD */
3150             "Encryption mode mismatch on read") },
3151         /*  T             */
3152         { SST(0x74, 0x0A, SS_RDEF,      /* XXX TBD */
3153             "Encrypted block not raw read enabled") },
3154         /*  T             */
3155         { SST(0x74, 0x0B, SS_RDEF,      /* XXX TBD */
3156             "Incorrect encryption parameters") },
3157         /* DT   R MAEBKV  */
3158         { SST(0x74, 0x0C, SS_RDEF,      /* XXX TBD */
3159             "Unable to decrypt parameter list") },
3160         /*  T             */
3161         { SST(0x74, 0x0D, SS_RDEF,      /* XXX TBD */
3162             "Encryption algorithm disabled") },
3163         /* DT   R MAEBKV  */
3164         { SST(0x74, 0x10, SS_RDEF,      /* XXX TBD */
3165             "SA creation parameter value invalid") },
3166         /* DT   R MAEBKV  */
3167         { SST(0x74, 0x11, SS_RDEF,      /* XXX TBD */
3168             "SA creation parameter value rejected") },
3169         /* DT   R MAEBKV  */
3170         { SST(0x74, 0x12, SS_RDEF,      /* XXX TBD */
3171             "Invalid SA usage") },
3172         /*  T             */
3173         { SST(0x74, 0x21, SS_RDEF,      /* XXX TBD */
3174             "Data encryption configuration prevented") },
3175         /* DT   R MAEBKV  */
3176         { SST(0x74, 0x30, SS_RDEF,      /* XXX TBD */
3177             "SA creation parameter not supported") },
3178         /* DT   R MAEBKV  */
3179         { SST(0x74, 0x40, SS_RDEF,      /* XXX TBD */
3180             "Authentication failed") },
3181         /*             V  */
3182         { SST(0x74, 0x61, SS_RDEF,      /* XXX TBD */
3183             "External data encryption key manager access error") },
3184         /*             V  */
3185         { SST(0x74, 0x62, SS_RDEF,      /* XXX TBD */
3186             "External data encryption key manager error") },
3187         /*             V  */
3188         { SST(0x74, 0x63, SS_RDEF,      /* XXX TBD */
3189             "External data encryption key not found") },
3190         /*             V  */
3191         { SST(0x74, 0x64, SS_RDEF,      /* XXX TBD */
3192             "External data encryption request not authorized") },
3193         /*  T             */
3194         { SST(0x74, 0x6E, SS_RDEF,      /* XXX TBD */
3195             "External data encryption control timeout") },
3196         /*  T             */
3197         { SST(0x74, 0x6F, SS_RDEF,      /* XXX TBD */
3198             "External data encryption control error") },
3199         /* DT   R M E  V  */
3200         { SST(0x74, 0x71, SS_FATAL | EACCES,
3201             "Logical unit access not authorized") },
3202         /* D              */
3203         { SST(0x74, 0x79, SS_FATAL | EACCES,
3204             "Security conflict in translated device") }
3205 };
3206
3207 const int asc_table_size = sizeof(asc_table)/sizeof(asc_table[0]);
3208
3209 struct asc_key
3210 {
3211         int asc;
3212         int ascq;
3213 };
3214
3215 static int
3216 ascentrycomp(const void *key, const void *member)
3217 {
3218         int asc;
3219         int ascq;
3220         const struct asc_table_entry *table_entry;
3221
3222         asc = ((const struct asc_key *)key)->asc;
3223         ascq = ((const struct asc_key *)key)->ascq;
3224         table_entry = (const struct asc_table_entry *)member;
3225
3226         if (asc >= table_entry->asc) {
3227
3228                 if (asc > table_entry->asc)
3229                         return (1);
3230
3231                 if (ascq <= table_entry->ascq) {
3232                         /* Check for ranges */
3233                         if (ascq == table_entry->ascq
3234                          || ((table_entry->action & SSQ_RANGE) != 0
3235                            && ascq >= (table_entry - 1)->ascq))
3236                                 return (0);
3237                         return (-1);
3238                 }
3239                 return (1);
3240         }
3241         return (-1);
3242 }
3243
3244 static int
3245 senseentrycomp(const void *key, const void *member)
3246 {
3247         int sense_key;
3248         const struct sense_key_table_entry *table_entry;
3249
3250         sense_key = *((const int *)key);
3251         table_entry = (const struct sense_key_table_entry *)member;
3252
3253         if (sense_key >= table_entry->sense_key) {
3254                 if (sense_key == table_entry->sense_key)
3255                         return (0);
3256                 return (1);
3257         }
3258         return (-1);
3259 }
3260
3261 static void
3262 fetchtableentries(int sense_key, int asc, int ascq,
3263                   struct scsi_inquiry_data *inq_data,
3264                   const struct sense_key_table_entry **sense_entry,
3265                   const struct asc_table_entry **asc_entry)
3266 {
3267         caddr_t match;
3268         const struct asc_table_entry *asc_tables[2];
3269         const struct sense_key_table_entry *sense_tables[2];
3270         struct asc_key asc_ascq;
3271         size_t asc_tables_size[2];
3272         size_t sense_tables_size[2];
3273         int num_asc_tables;
3274         int num_sense_tables;
3275         int i;
3276
3277         /* Default to failure */
3278         *sense_entry = NULL;
3279         *asc_entry = NULL;
3280         match = NULL;
3281         if (inq_data != NULL)
3282                 match = cam_quirkmatch((caddr_t)inq_data,
3283                                        (caddr_t)sense_quirk_table,
3284                                        sense_quirk_table_size,
3285                                        sizeof(*sense_quirk_table),
3286                                        scsi_inquiry_match);
3287
3288         if (match != NULL) {
3289                 struct scsi_sense_quirk_entry *quirk;
3290
3291                 quirk = (struct scsi_sense_quirk_entry *)match;
3292                 asc_tables[0] = quirk->asc_info;
3293                 asc_tables_size[0] = quirk->num_ascs;
3294                 asc_tables[1] = asc_table;
3295                 asc_tables_size[1] = asc_table_size;
3296                 num_asc_tables = 2;
3297                 sense_tables[0] = quirk->sense_key_info;
3298                 sense_tables_size[0] = quirk->num_sense_keys;
3299                 sense_tables[1] = sense_key_table;
3300                 sense_tables_size[1] = sense_key_table_size;
3301                 num_sense_tables = 2;
3302         } else {
3303                 asc_tables[0] = asc_table;
3304                 asc_tables_size[0] = asc_table_size;
3305                 num_asc_tables = 1;
3306                 sense_tables[0] = sense_key_table;
3307                 sense_tables_size[0] = sense_key_table_size;
3308                 num_sense_tables = 1;
3309         }
3310
3311         asc_ascq.asc = asc;
3312         asc_ascq.ascq = ascq;
3313         for (i = 0; i < num_asc_tables; i++) {
3314                 void *found_entry;
3315
3316                 found_entry = bsearch(&asc_ascq, asc_tables[i],
3317                                       asc_tables_size[i],
3318                                       sizeof(**asc_tables),
3319                                       ascentrycomp);
3320
3321                 if (found_entry) {
3322                         *asc_entry = (struct asc_table_entry *)found_entry;
3323                         break;
3324                 }
3325         }
3326
3327         for (i = 0; i < num_sense_tables; i++) {
3328                 void *found_entry;
3329
3330                 found_entry = bsearch(&sense_key, sense_tables[i],
3331                                       sense_tables_size[i],
3332                                       sizeof(**sense_tables),
3333                                       senseentrycomp);
3334
3335                 if (found_entry) {
3336                         *sense_entry =
3337                             (struct sense_key_table_entry *)found_entry;
3338                         break;
3339                 }
3340         }
3341 }
3342
3343 void
3344 scsi_sense_desc(int sense_key, int asc, int ascq,
3345                 struct scsi_inquiry_data *inq_data,
3346                 const char **sense_key_desc, const char **asc_desc)
3347 {
3348         const struct asc_table_entry *asc_entry;
3349         const struct sense_key_table_entry *sense_entry;
3350
3351         fetchtableentries(sense_key, asc, ascq,
3352                           inq_data,
3353                           &sense_entry,
3354                           &asc_entry);
3355
3356         if (sense_entry != NULL)
3357                 *sense_key_desc = sense_entry->desc;
3358         else
3359                 *sense_key_desc = "Invalid Sense Key";
3360
3361         if (asc_entry != NULL)
3362                 *asc_desc = asc_entry->desc;
3363         else if (asc >= 0x80 && asc <= 0xff)
3364                 *asc_desc = "Vendor Specific ASC";
3365         else if (ascq >= 0x80 && ascq <= 0xff)
3366                 *asc_desc = "Vendor Specific ASCQ";
3367         else
3368                 *asc_desc = "Reserved ASC/ASCQ pair";
3369 }
3370
3371 /*
3372  * Given sense and device type information, return the appropriate action.
3373  * If we do not understand the specific error as identified by the ASC/ASCQ
3374  * pair, fall back on the more generic actions derived from the sense key.
3375  */
3376 scsi_sense_action
3377 scsi_error_action(struct ccb_scsiio *csio, struct scsi_inquiry_data *inq_data,
3378                   u_int32_t sense_flags)
3379 {
3380         const struct asc_table_entry *asc_entry;
3381         const struct sense_key_table_entry *sense_entry;
3382         int error_code, sense_key, asc, ascq;
3383         scsi_sense_action action;
3384
3385         if (!scsi_extract_sense_ccb((union ccb *)csio,
3386             &error_code, &sense_key, &asc, &ascq)) {
3387                 action = SS_RETRY | SSQ_DECREMENT_COUNT | SSQ_PRINT_SENSE | EIO;
3388         } else if ((error_code == SSD_DEFERRED_ERROR)
3389          || (error_code == SSD_DESC_DEFERRED_ERROR)) {
3390                 /*
3391                  * XXX dufault@FreeBSD.org
3392                  * This error doesn't relate to the command associated
3393                  * with this request sense.  A deferred error is an error
3394                  * for a command that has already returned GOOD status
3395                  * (see SCSI2 8.2.14.2).
3396                  *
3397                  * By my reading of that section, it looks like the current
3398                  * command has been cancelled, we should now clean things up
3399                  * (hopefully recovering any lost data) and then retry the
3400                  * current command.  There are two easy choices, both wrong:
3401                  *
3402                  * 1. Drop through (like we had been doing), thus treating
3403                  *    this as if the error were for the current command and
3404                  *    return and stop the current command.
3405                  * 
3406                  * 2. Issue a retry (like I made it do) thus hopefully
3407                  *    recovering the current transfer, and ignoring the
3408                  *    fact that we've dropped a command.
3409                  *
3410                  * These should probably be handled in a device specific
3411                  * sense handler or punted back up to a user mode daemon
3412                  */
3413                 action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE;
3414         } else {
3415                 fetchtableentries(sense_key, asc, ascq,
3416                                   inq_data,
3417                                   &sense_entry,
3418                                   &asc_entry);
3419
3420                 /*
3421                  * Override the 'No additional Sense' entry (0,0)
3422                  * with the error action of the sense key.
3423                  */
3424                 if (asc_entry != NULL
3425                  && (asc != 0 || ascq != 0))
3426                         action = asc_entry->action;
3427                 else if (sense_entry != NULL)
3428                         action = sense_entry->action;
3429                 else
3430                         action = SS_RETRY|SSQ_DECREMENT_COUNT|SSQ_PRINT_SENSE; 
3431
3432                 if (sense_key == SSD_KEY_RECOVERED_ERROR) {
3433                         /*
3434                          * The action succeeded but the device wants
3435                          * the user to know that some recovery action
3436                          * was required.
3437                          */
3438                         action &= ~(SS_MASK|SSQ_MASK|SS_ERRMASK);
3439                         action |= SS_NOP|SSQ_PRINT_SENSE;
3440                 } else if (sense_key == SSD_KEY_ILLEGAL_REQUEST) {
3441                         if ((sense_flags & SF_QUIET_IR) != 0)
3442                                 action &= ~SSQ_PRINT_SENSE;
3443                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION) {
3444                         if ((sense_flags & SF_RETRY_UA) != 0
3445                          && (action & SS_MASK) == SS_FAIL) {
3446                                 action &= ~(SS_MASK|SSQ_MASK);
3447                                 action |= SS_RETRY|SSQ_DECREMENT_COUNT|
3448                                           SSQ_PRINT_SENSE;
3449                         }
3450                         action |= SSQ_UA;
3451                 }
3452         }
3453         if ((action & SS_MASK) >= SS_START &&
3454             (sense_flags & SF_NO_RECOVERY)) {
3455                 action &= ~SS_MASK;
3456                 action |= SS_FAIL;
3457         } else if ((action & SS_MASK) == SS_RETRY &&
3458             (sense_flags & SF_NO_RETRY)) {
3459                 action &= ~SS_MASK;
3460                 action |= SS_FAIL;
3461         }
3462         if ((sense_flags & SF_PRINT_ALWAYS) != 0)
3463                 action |= SSQ_PRINT_SENSE;
3464         else if ((sense_flags & SF_NO_PRINT) != 0)
3465                 action &= ~SSQ_PRINT_SENSE;
3466
3467         return (action);
3468 }
3469
3470 char *
3471 scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
3472 {
3473         struct sbuf sb;
3474         int error;
3475
3476         if (len == 0)
3477                 return ("");
3478
3479         sbuf_new(&sb, cdb_string, len, SBUF_FIXEDLEN);
3480
3481         scsi_cdb_sbuf(cdb_ptr, &sb);
3482
3483         /* ENOMEM just means that the fixed buffer is full, OK to ignore */
3484         error = sbuf_finish(&sb);
3485         if (error != 0 && error != ENOMEM)
3486                 return ("");
3487
3488         return(sbuf_data(&sb));
3489 }
3490
3491 void
3492 scsi_cdb_sbuf(u_int8_t *cdb_ptr, struct sbuf *sb)
3493 {
3494         u_int8_t cdb_len;
3495         int i;
3496
3497         if (cdb_ptr == NULL)
3498                 return;
3499
3500         /*
3501          * This is taken from the SCSI-3 draft spec.
3502          * (T10/1157D revision 0.3)
3503          * The top 3 bits of an opcode are the group code.  The next 5 bits
3504          * are the command code.
3505          * Group 0:  six byte commands
3506          * Group 1:  ten byte commands
3507          * Group 2:  ten byte commands
3508          * Group 3:  reserved
3509          * Group 4:  sixteen byte commands
3510          * Group 5:  twelve byte commands
3511          * Group 6:  vendor specific
3512          * Group 7:  vendor specific
3513          */
3514         switch((*cdb_ptr >> 5) & 0x7) {
3515                 case 0:
3516                         cdb_len = 6;
3517                         break;
3518                 case 1:
3519                 case 2:
3520                         cdb_len = 10;
3521                         break;
3522                 case 3:
3523                 case 6:
3524                 case 7:
3525                         /* in this case, just print out the opcode */
3526                         cdb_len = 1;
3527                         break;
3528                 case 4:
3529                         cdb_len = 16;
3530                         break;
3531                 case 5:
3532                         cdb_len = 12;
3533                         break;
3534         }
3535
3536         for (i = 0; i < cdb_len; i++)
3537                 sbuf_printf(sb, "%02hhx ", cdb_ptr[i]);
3538
3539         return;
3540 }
3541
3542 const char *
3543 scsi_status_string(struct ccb_scsiio *csio)
3544 {
3545         switch(csio->scsi_status) {
3546         case SCSI_STATUS_OK:
3547                 return("OK");
3548         case SCSI_STATUS_CHECK_COND:
3549                 return("Check Condition");
3550         case SCSI_STATUS_BUSY:
3551                 return("Busy");
3552         case SCSI_STATUS_INTERMED:
3553                 return("Intermediate");
3554         case SCSI_STATUS_INTERMED_COND_MET:
3555                 return("Intermediate-Condition Met");
3556         case SCSI_STATUS_RESERV_CONFLICT:
3557                 return("Reservation Conflict");
3558         case SCSI_STATUS_CMD_TERMINATED:
3559                 return("Command Terminated");
3560         case SCSI_STATUS_QUEUE_FULL:
3561                 return("Queue Full");
3562         case SCSI_STATUS_ACA_ACTIVE:
3563                 return("ACA Active");
3564         case SCSI_STATUS_TASK_ABORTED:
3565                 return("Task Aborted");
3566         default: {
3567                 static char unkstr[64];
3568                 snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
3569                          csio->scsi_status);
3570                 return(unkstr);
3571         }
3572         }
3573 }
3574
3575 /*
3576  * scsi_command_string() returns 0 for success and -1 for failure.
3577  */
3578 #ifdef _KERNEL
3579 int
3580 scsi_command_string(struct ccb_scsiio *csio, struct sbuf *sb)
3581 #else /* !_KERNEL */
3582 int
3583 scsi_command_string(struct cam_device *device, struct ccb_scsiio *csio, 
3584                     struct sbuf *sb)
3585 #endif /* _KERNEL/!_KERNEL */
3586 {
3587         struct scsi_inquiry_data *inq_data;
3588 #ifdef _KERNEL
3589         struct    ccb_getdev *cgd;
3590 #endif /* _KERNEL */
3591
3592 #ifdef _KERNEL
3593         if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
3594                 return(-1);
3595         /*
3596          * Get the device information.
3597          */
3598         xpt_setup_ccb(&cgd->ccb_h,
3599                       csio->ccb_h.path,
3600                       CAM_PRIORITY_NORMAL);
3601         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
3602         xpt_action((union ccb *)cgd);
3603
3604         /*
3605          * If the device is unconfigured, just pretend that it is a hard
3606          * drive.  scsi_op_desc() needs this.
3607          */
3608         if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
3609                 cgd->inq_data.device = T_DIRECT;
3610
3611         inq_data = &cgd->inq_data;
3612
3613 #else /* !_KERNEL */
3614
3615         inq_data = &device->inq_data;
3616
3617 #endif /* _KERNEL/!_KERNEL */
3618
3619         if ((csio->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3620                 sbuf_printf(sb, "%s. CDB: ", 
3621                             scsi_op_desc(csio->cdb_io.cdb_ptr[0], inq_data));
3622                 scsi_cdb_sbuf(csio->cdb_io.cdb_ptr, sb);
3623         } else {
3624                 sbuf_printf(sb, "%s. CDB: ",
3625                             scsi_op_desc(csio->cdb_io.cdb_bytes[0], inq_data));
3626                 scsi_cdb_sbuf(csio->cdb_io.cdb_bytes, sb);
3627         }
3628
3629 #ifdef _KERNEL
3630         xpt_free_ccb((union ccb *)cgd);
3631 #endif
3632
3633         return(0);
3634 }
3635
3636 /*
3637  * Iterate over sense descriptors.  Each descriptor is passed into iter_func(). 
3638  * If iter_func() returns 0, list traversal continues.  If iter_func()
3639  * returns non-zero, list traversal is stopped.
3640  */
3641 void
3642 scsi_desc_iterate(struct scsi_sense_data_desc *sense, u_int sense_len,
3643                   int (*iter_func)(struct scsi_sense_data_desc *sense,
3644                                    u_int, struct scsi_sense_desc_header *,
3645                                    void *), void *arg)
3646 {
3647         int cur_pos;
3648         int desc_len;
3649
3650         /*
3651          * First make sure the extra length field is present.
3652          */
3653         if (SSD_DESC_IS_PRESENT(sense, sense_len, extra_len) == 0)
3654                 return;
3655
3656         /*
3657          * The length of data actually returned may be different than the
3658          * extra_len recorded in the structure.
3659          */
3660         desc_len = sense_len -offsetof(struct scsi_sense_data_desc, sense_desc);
3661
3662         /*
3663          * Limit this further by the extra length reported, and the maximum
3664          * allowed extra length.
3665          */
3666         desc_len = MIN(desc_len, MIN(sense->extra_len, SSD_EXTRA_MAX));
3667
3668         /*
3669          * Subtract the size of the header from the descriptor length.
3670          * This is to ensure that we have at least the header left, so we
3671          * don't have to check that inside the loop.  This can wind up
3672          * being a negative value.
3673          */
3674         desc_len -= sizeof(struct scsi_sense_desc_header);
3675
3676         for (cur_pos = 0; cur_pos < desc_len;) {
3677                 struct scsi_sense_desc_header *header;
3678
3679                 header = (struct scsi_sense_desc_header *)
3680                         &sense->sense_desc[cur_pos];
3681
3682                 /*
3683                  * Check to make sure we have the entire descriptor.  We
3684                  * don't call iter_func() unless we do.
3685                  *
3686                  * Note that although cur_pos is at the beginning of the
3687                  * descriptor, desc_len already has the header length
3688                  * subtracted.  So the comparison of the length in the
3689                  * header (which does not include the header itself) to
3690                  * desc_len - cur_pos is correct.
3691                  */
3692                 if (header->length > (desc_len - cur_pos)) 
3693                         break;
3694
3695                 if (iter_func(sense, sense_len, header, arg) != 0)
3696                         break;
3697
3698                 cur_pos += sizeof(*header) + header->length;
3699         }
3700 }
3701
3702 struct scsi_find_desc_info {
3703         uint8_t desc_type;
3704         struct scsi_sense_desc_header *header;
3705 };
3706
3707 static int
3708 scsi_find_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
3709                     struct scsi_sense_desc_header *header, void *arg)
3710 {
3711         struct scsi_find_desc_info *desc_info;
3712
3713         desc_info = (struct scsi_find_desc_info *)arg;
3714
3715         if (header->desc_type == desc_info->desc_type) {
3716                 desc_info->header = header;
3717
3718                 /* We found the descriptor, tell the iterator to stop. */
3719                 return (1);
3720         } else
3721                 return (0);
3722 }
3723
3724 /*
3725  * Given a descriptor type, return a pointer to it if it is in the sense
3726  * data and not truncated.  Avoiding truncating sense data will simplify
3727  * things significantly for the caller.
3728  */
3729 uint8_t *
3730 scsi_find_desc(struct scsi_sense_data_desc *sense, u_int sense_len,
3731                uint8_t desc_type)
3732 {
3733         struct scsi_find_desc_info desc_info;
3734
3735         desc_info.desc_type = desc_type;
3736         desc_info.header = NULL;
3737
3738         scsi_desc_iterate(sense, sense_len, scsi_find_desc_func, &desc_info);
3739
3740         return ((uint8_t *)desc_info.header);
3741 }
3742
3743 /*
3744  * Fill in SCSI sense data with the specified parameters.  This routine can
3745  * fill in either fixed or descriptor type sense data.
3746  */
3747 void
3748 scsi_set_sense_data_va(struct scsi_sense_data *sense_data,
3749                       scsi_sense_data_type sense_format, int current_error,
3750                       int sense_key, int asc, int ascq, va_list ap) 
3751 {
3752         int descriptor_sense;
3753         scsi_sense_elem_type elem_type;
3754
3755         /*
3756          * Determine whether to return fixed or descriptor format sense
3757          * data.  If the user specifies SSD_TYPE_NONE for some reason,
3758          * they'll just get fixed sense data.
3759          */
3760         if (sense_format == SSD_TYPE_DESC)
3761                 descriptor_sense = 1;
3762         else
3763                 descriptor_sense = 0;
3764
3765         /*
3766          * Zero the sense data, so that we don't pass back any garbage data
3767          * to the user.
3768          */
3769         memset(sense_data, 0, sizeof(*sense_data));
3770
3771         if (descriptor_sense != 0) {
3772                 struct scsi_sense_data_desc *sense;
3773
3774                 sense = (struct scsi_sense_data_desc *)sense_data;
3775                 /*
3776                  * The descriptor sense format eliminates the use of the
3777                  * valid bit.
3778                  */
3779                 if (current_error != 0)
3780                         sense->error_code = SSD_DESC_CURRENT_ERROR;
3781                 else
3782                         sense->error_code = SSD_DESC_DEFERRED_ERROR;
3783                 sense->sense_key = sense_key;
3784                 sense->add_sense_code = asc;
3785                 sense->add_sense_code_qual = ascq;
3786                 /*
3787                  * Start off with no extra length, since the above data
3788                  * fits in the standard descriptor sense information.
3789                  */
3790                 sense->extra_len = 0;
3791                 while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3792                         scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3793                         int sense_len, len_to_copy;
3794                         uint8_t *data;
3795
3796                         if (elem_type >= SSD_ELEM_MAX) {
3797                                 printf("%s: invalid sense type %d\n", __func__,
3798                                        elem_type);
3799                                 break;
3800                         }
3801
3802                         sense_len = (int)va_arg(ap, int);
3803                         len_to_copy = MIN(sense_len, SSD_EXTRA_MAX -
3804                                           sense->extra_len);
3805                         data = (uint8_t *)va_arg(ap, uint8_t *);
3806
3807                         /*
3808                          * We've already consumed the arguments for this one.
3809                          */
3810                         if (elem_type == SSD_ELEM_SKIP)
3811                                 continue;
3812
3813                         switch (elem_type) {
3814                         case SSD_ELEM_DESC: {
3815
3816                                 /*
3817                                  * This is a straight descriptor.  All we
3818                                  * need to do is copy the data in.
3819                                  */
3820                                 bcopy(data, &sense->sense_desc[
3821                                       sense->extra_len], len_to_copy);
3822                                 sense->extra_len += len_to_copy;
3823                                 break;
3824                         }
3825                         case SSD_ELEM_SKS: {
3826                                 struct scsi_sense_sks sks;
3827
3828                                 bzero(&sks, sizeof(sks));
3829
3830                                 /*
3831                                  * This is already-formatted sense key
3832                                  * specific data.  We just need to fill out
3833                                  * the header and copy everything in.
3834                                  */
3835                                 bcopy(data, &sks.sense_key_spec,
3836                                       MIN(len_to_copy,
3837                                           sizeof(sks.sense_key_spec)));
3838
3839                                 sks.desc_type = SSD_DESC_SKS;
3840                                 sks.length = sizeof(sks) -
3841                                     offsetof(struct scsi_sense_sks, reserved1);
3842                                 bcopy(&sks,&sense->sense_desc[sense->extra_len],
3843                                       sizeof(sks));
3844                                 sense->extra_len += sizeof(sks);
3845                                 break;
3846                         }
3847                         case SSD_ELEM_INFO:
3848                         case SSD_ELEM_COMMAND: {
3849                                 struct scsi_sense_command cmd;
3850                                 struct scsi_sense_info info;
3851                                 uint8_t *data_dest;
3852                                 uint8_t *descriptor;
3853                                 int descriptor_size, i, copy_len;
3854
3855                                 bzero(&cmd, sizeof(cmd));
3856                                 bzero(&info, sizeof(info));
3857
3858                                 /*
3859                                  * Command or information data.  The
3860                                  * operate in pretty much the same way.
3861                                  */
3862                                 if (elem_type == SSD_ELEM_COMMAND) {
3863                                         len_to_copy = MIN(len_to_copy,
3864                                             sizeof(cmd.command_info));
3865                                         descriptor = (uint8_t *)&cmd;
3866                                         descriptor_size  = sizeof(cmd);
3867                                         data_dest =(uint8_t *)&cmd.command_info;
3868                                         cmd.desc_type = SSD_DESC_COMMAND;
3869                                         cmd.length = sizeof(cmd) -
3870                                             offsetof(struct scsi_sense_command,
3871                                                      reserved);
3872                                 } else {
3873                                         len_to_copy = MIN(len_to_copy,
3874                                             sizeof(info.info));
3875                                         descriptor = (uint8_t *)&info;
3876                                         descriptor_size = sizeof(cmd);
3877                                         data_dest = (uint8_t *)&info.info;
3878                                         info.desc_type = SSD_DESC_INFO;
3879                                         info.byte2 = SSD_INFO_VALID;
3880                                         info.length = sizeof(info) -
3881                                             offsetof(struct scsi_sense_info,
3882                                                      byte2);
3883                                 }
3884
3885                                 /*
3886                                  * Copy this in reverse because the spec
3887                                  * (SPC-4) says that when 4 byte quantities
3888                                  * are stored in this 8 byte field, the
3889                                  * first four bytes shall be 0.
3890                                  *
3891                                  * So we fill the bytes in from the end, and
3892                                  * if we have less than 8 bytes to copy,
3893                                  * the initial, most significant bytes will
3894                                  * be 0.
3895                                  */
3896                                 for (i = sense_len - 1; i >= 0 &&
3897                                      len_to_copy > 0; i--, len_to_copy--)
3898                                         data_dest[len_to_copy - 1] = data[i];
3899
3900                                 /*
3901                                  * This calculation looks much like the
3902                                  * initial len_to_copy calculation, but
3903                                  * we have to do it again here, because
3904                                  * we're looking at a larger amount that
3905                                  * may or may not fit.  It's not only the
3906                                  * data the user passed in, but also the
3907                                  * rest of the descriptor.
3908                                  */
3909                                 copy_len = MIN(descriptor_size,
3910                                     SSD_EXTRA_MAX - sense->extra_len);
3911                                 bcopy(descriptor, &sense->sense_desc[
3912                                       sense->extra_len], copy_len);
3913                                 sense->extra_len += copy_len;
3914                                 break;
3915                         }
3916                         case SSD_ELEM_FRU: {
3917                                 struct scsi_sense_fru fru;
3918                                 int copy_len;
3919
3920                                 bzero(&fru, sizeof(fru));
3921
3922                                 fru.desc_type = SSD_DESC_FRU;
3923                                 fru.length = sizeof(fru) -
3924                                     offsetof(struct scsi_sense_fru, reserved);
3925                                 fru.fru = *data;
3926
3927                                 copy_len = MIN(sizeof(fru), SSD_EXTRA_MAX -
3928                                                sense->extra_len);
3929                                 bcopy(&fru, &sense->sense_desc[
3930                                       sense->extra_len], copy_len);
3931                                 sense->extra_len += copy_len;
3932                                 break;
3933                         }
3934                         case SSD_ELEM_STREAM: {
3935                                 struct scsi_sense_stream stream_sense;
3936                                 int copy_len;
3937
3938                                 bzero(&stream_sense, sizeof(stream_sense));
3939                                 stream_sense.desc_type = SSD_DESC_STREAM;
3940                                 stream_sense.length = sizeof(stream_sense) -
3941                                    offsetof(struct scsi_sense_stream, reserved);
3942                                 stream_sense.byte3 = *data;
3943
3944                                 copy_len = MIN(sizeof(stream_sense),
3945                                     SSD_EXTRA_MAX - sense->extra_len);
3946                                 bcopy(&stream_sense, &sense->sense_desc[
3947                                       sense->extra_len], copy_len);
3948                                 sense->extra_len += copy_len;
3949                                 break;
3950                         }
3951                         default:
3952                                 /*
3953                                  * We shouldn't get here, but if we do, do
3954                                  * nothing.  We've already consumed the
3955                                  * arguments above.
3956                                  */
3957                                 break;
3958                         }
3959                 }
3960         } else {
3961                 struct scsi_sense_data_fixed *sense;
3962
3963                 sense = (struct scsi_sense_data_fixed *)sense_data;
3964
3965                 if (current_error != 0)
3966                         sense->error_code = SSD_CURRENT_ERROR;
3967                 else
3968                         sense->error_code = SSD_DEFERRED_ERROR;
3969
3970                 sense->flags = sense_key;
3971                 sense->add_sense_code = asc;
3972                 sense->add_sense_code_qual = ascq;
3973                 /*
3974                  * We've set the ASC and ASCQ, so we have 6 more bytes of
3975                  * valid data.  If we wind up setting any of the other
3976                  * fields, we'll bump this to 10 extra bytes.
3977                  */
3978                 sense->extra_len = 6;
3979
3980                 while ((elem_type = (scsi_sense_elem_type)va_arg(ap,
3981                         scsi_sense_elem_type)) != SSD_ELEM_NONE) {
3982                         int sense_len, len_to_copy;
3983                         uint8_t *data;
3984
3985                         if (elem_type >= SSD_ELEM_MAX) {
3986                                 printf("%s: invalid sense type %d\n", __func__,
3987                                        elem_type);
3988                                 break;
3989                         }
3990                         /*
3991                          * If we get in here, just bump the extra length to
3992                          * 10 bytes.  That will encompass anything we're
3993                          * going to set here.
3994                          */
3995                         sense->extra_len = 10;
3996                         sense_len = (int)va_arg(ap, int);
3997                         data = (uint8_t *)va_arg(ap, uint8_t *);
3998
3999                         switch (elem_type) {
4000                         case SSD_ELEM_SKS:
4001                                 /*
4002                                  * The user passed in pre-formatted sense
4003                                  * key specific data.
4004                                  */
4005                                 bcopy(data, &sense->sense_key_spec[0],
4006                                       MIN(sizeof(sense->sense_key_spec),
4007                                       sense_len));
4008                                 break;
4009                         case SSD_ELEM_INFO:
4010                         case SSD_ELEM_COMMAND: {
4011                                 uint8_t *data_dest;
4012                                 int i;
4013
4014                                 if (elem_type == SSD_ELEM_COMMAND) {
4015                                         data_dest = &sense->cmd_spec_info[0];
4016                                         len_to_copy = MIN(sense_len,
4017                                             sizeof(sense->cmd_spec_info));
4018                                 } else {
4019                                         data_dest = &sense->info[0];
4020                                         len_to_copy = MIN(sense_len,
4021                                             sizeof(sense->info));
4022
4023                                         /* Set VALID bit only if no overflow. */
4024                                         for (i = 0; i < sense_len - len_to_copy;
4025                                             i++) {
4026                                                 if (data[i] != 0)
4027                                                         break;
4028                                         }
4029                                         if (i >= sense_len - len_to_copy) {
4030                                                 sense->error_code |=
4031                                                     SSD_ERRCODE_VALID;
4032                                         }
4033                                 }
4034
4035                                 /*
4036                                  * Copy this in reverse so that if we have
4037                                  * less than 4 bytes to fill, the least
4038                                  * significant bytes will be at the end.
4039                                  * If we have more than 4 bytes, only the
4040                                  * least significant bytes will be included.
4041                                  */
4042                                 for (i = sense_len - 1; i >= 0 &&
4043                                      len_to_copy > 0; i--, len_to_copy--)
4044                                         data_dest[len_to_copy - 1] = data[i];
4045
4046                                 break;
4047                         }
4048                         case SSD_ELEM_FRU:
4049                                 sense->fru = *data;
4050                                 break;
4051                         case SSD_ELEM_STREAM:
4052                                 sense->flags |= *data;
4053                                 break;
4054                         case SSD_ELEM_DESC:
4055                         default:
4056
4057                                 /*
4058                                  * If the user passes in descriptor sense,
4059                                  * we can't handle that in fixed format.
4060                                  * So just skip it, and any unknown argument
4061                                  * types.
4062                                  */
4063                                 break;
4064                         }
4065                 }
4066         }
4067 }
4068
4069 void
4070 scsi_set_sense_data(struct scsi_sense_data *sense_data, 
4071                     scsi_sense_data_type sense_format, int current_error,
4072                     int sense_key, int asc, int ascq, ...) 
4073 {
4074         va_list ap;
4075
4076         va_start(ap, ascq);
4077         scsi_set_sense_data_va(sense_data, sense_format, current_error,
4078                                sense_key, asc, ascq, ap);
4079         va_end(ap);
4080 }
4081
4082 /*
4083  * Get sense information for three similar sense data types.
4084  */
4085 int
4086 scsi_get_sense_info(struct scsi_sense_data *sense_data, u_int sense_len,
4087                     uint8_t info_type, uint64_t *info, int64_t *signed_info)
4088 {
4089         scsi_sense_data_type sense_type;
4090
4091         if (sense_len == 0)
4092                 goto bailout;
4093
4094         sense_type = scsi_sense_type(sense_data);
4095
4096         switch (sense_type) {
4097         case SSD_TYPE_DESC: {
4098                 struct scsi_sense_data_desc *sense;
4099                 uint8_t *desc;
4100
4101                 sense = (struct scsi_sense_data_desc *)sense_data;
4102
4103                 desc = scsi_find_desc(sense, sense_len, info_type);
4104                 if (desc == NULL)
4105                         goto bailout;
4106
4107                 switch (info_type) {
4108                 case SSD_DESC_INFO: {
4109                         struct scsi_sense_info *info_desc;
4110
4111                         info_desc = (struct scsi_sense_info *)desc;
4112                         *info = scsi_8btou64(info_desc->info);
4113                         if (signed_info != NULL)
4114                                 *signed_info = *info;
4115                         break;
4116                 }
4117                 case SSD_DESC_COMMAND: {
4118                         struct scsi_sense_command *cmd_desc;
4119
4120                         cmd_desc = (struct scsi_sense_command *)desc;
4121
4122                         *info = scsi_8btou64(cmd_desc->command_info);
4123                         if (signed_info != NULL)
4124                                 *signed_info = *info;
4125                         break;
4126                 }
4127                 case SSD_DESC_FRU: {
4128                         struct scsi_sense_fru *fru_desc;
4129
4130                         fru_desc = (struct scsi_sense_fru *)desc;
4131
4132                         *info = fru_desc->fru;
4133                         if (signed_info != NULL)
4134                                 *signed_info = (int8_t)fru_desc->fru;
4135                         break;
4136                 }
4137                 default:
4138                         goto bailout;
4139                         break;
4140                 }
4141                 break;
4142         }
4143         case SSD_TYPE_FIXED: {
4144                 struct scsi_sense_data_fixed *sense;
4145
4146                 sense = (struct scsi_sense_data_fixed *)sense_data;
4147
4148                 switch (info_type) {
4149                 case SSD_DESC_INFO: {
4150                         uint32_t info_val;
4151
4152                         if ((sense->error_code & SSD_ERRCODE_VALID) == 0)
4153                                 goto bailout;
4154
4155                         if (SSD_FIXED_IS_PRESENT(sense, sense_len, info) == 0)
4156                                 goto bailout;
4157
4158                         info_val = scsi_4btoul(sense->info);
4159
4160                         *info = info_val;
4161                         if (signed_info != NULL)
4162                                 *signed_info = (int32_t)info_val;
4163                         break;
4164                 }
4165                 case SSD_DESC_COMMAND: {
4166                         uint32_t cmd_val;
4167
4168                         if ((SSD_FIXED_IS_PRESENT(sense, sense_len,
4169                              cmd_spec_info) == 0)
4170                          || (SSD_FIXED_IS_FILLED(sense, cmd_spec_info) == 0)) 
4171                                 goto bailout;
4172
4173                         cmd_val = scsi_4btoul(sense->cmd_spec_info);
4174                         if (cmd_val == 0)
4175                                 goto bailout;
4176
4177                         *info = cmd_val;
4178                         if (signed_info != NULL)
4179                                 *signed_info = (int32_t)cmd_val;
4180                         break;
4181                 }
4182                 case SSD_DESC_FRU:
4183                         if ((SSD_FIXED_IS_PRESENT(sense, sense_len, fru) == 0)
4184                          || (SSD_FIXED_IS_FILLED(sense, fru) == 0))
4185                                 goto bailout;
4186
4187                         if (sense->fru == 0)
4188                                 goto bailout;
4189
4190                         *info = sense->fru;
4191                         if (signed_info != NULL)
4192                                 *signed_info = (int8_t)sense->fru;
4193                         break;
4194                 default:
4195                         goto bailout;
4196                         break;
4197                 }
4198                 break;
4199         }
4200         default: 
4201                 goto bailout;
4202                 break;
4203         }
4204
4205         return (0);
4206 bailout:
4207         return (1);
4208 }
4209
4210 int
4211 scsi_get_sks(struct scsi_sense_data *sense_data, u_int sense_len, uint8_t *sks)
4212 {
4213         scsi_sense_data_type sense_type;
4214
4215         if (sense_len == 0)
4216                 goto bailout;
4217
4218         sense_type = scsi_sense_type(sense_data);
4219
4220         switch (sense_type) {
4221         case SSD_TYPE_DESC: {
4222                 struct scsi_sense_data_desc *sense;
4223                 struct scsi_sense_sks *desc;
4224
4225                 sense = (struct scsi_sense_data_desc *)sense_data;
4226
4227                 desc = (struct scsi_sense_sks *)scsi_find_desc(sense, sense_len,
4228                                                                SSD_DESC_SKS);
4229                 if (desc == NULL)
4230                         goto bailout;
4231
4232                 /*
4233                  * No need to check the SKS valid bit for descriptor sense.
4234                  * If the descriptor is present, it is valid.
4235                  */
4236                 bcopy(desc->sense_key_spec, sks, sizeof(desc->sense_key_spec));
4237                 break;
4238         }
4239         case SSD_TYPE_FIXED: {
4240                 struct scsi_sense_data_fixed *sense;
4241
4242                 sense = (struct scsi_sense_data_fixed *)sense_data;
4243
4244                 if ((SSD_FIXED_IS_PRESENT(sense, sense_len, sense_key_spec)== 0)
4245                  || (SSD_FIXED_IS_FILLED(sense, sense_key_spec) == 0))
4246                         goto bailout;
4247
4248                 if ((sense->sense_key_spec[0] & SSD_SCS_VALID) == 0)
4249                         goto bailout;
4250
4251                 bcopy(sense->sense_key_spec, sks,sizeof(sense->sense_key_spec));
4252                 break;
4253         }
4254         default:
4255                 goto bailout;
4256                 break;
4257         }
4258         return (0);
4259 bailout:
4260         return (1);
4261 }
4262
4263 /*
4264  * Provide a common interface for fixed and descriptor sense to detect
4265  * whether we have block-specific sense information.  It is clear by the
4266  * presence of the block descriptor in descriptor mode, but we have to
4267  * infer from the inquiry data and ILI bit in fixed mode.
4268  */
4269 int
4270 scsi_get_block_info(struct scsi_sense_data *sense_data, u_int sense_len,
4271                     struct scsi_inquiry_data *inq_data, uint8_t *block_bits)
4272 {
4273         scsi_sense_data_type sense_type;
4274
4275         if (inq_data != NULL) {
4276                 switch (SID_TYPE(inq_data)) {
4277                 case T_DIRECT:
4278                 case T_RBC:
4279                         break;
4280                 default:
4281                         goto bailout;
4282                         break;
4283                 }
4284         }
4285
4286         sense_type = scsi_sense_type(sense_data);
4287
4288         switch (sense_type) {
4289         case SSD_TYPE_DESC: {
4290                 struct scsi_sense_data_desc *sense;
4291                 struct scsi_sense_block *block;
4292
4293                 sense = (struct scsi_sense_data_desc *)sense_data;
4294
4295                 block = (struct scsi_sense_block *)scsi_find_desc(sense,
4296                     sense_len, SSD_DESC_BLOCK);
4297                 if (block == NULL)
4298                         goto bailout;
4299
4300                 *block_bits = block->byte3;
4301                 break;
4302         }
4303         case SSD_TYPE_FIXED: {
4304                 struct scsi_sense_data_fixed *sense;
4305
4306                 sense = (struct scsi_sense_data_fixed *)sense_data;
4307
4308                 if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4309                         goto bailout;
4310
4311                 if ((sense->flags & SSD_ILI) == 0)
4312                         goto bailout;
4313
4314                 *block_bits = sense->flags & SSD_ILI;
4315                 break;
4316         }
4317         default:
4318                 goto bailout;
4319                 break;
4320         }
4321         return (0);
4322 bailout:
4323         return (1);
4324 }
4325
4326 int
4327 scsi_get_stream_info(struct scsi_sense_data *sense_data, u_int sense_len,
4328                      struct scsi_inquiry_data *inq_data, uint8_t *stream_bits)
4329 {
4330         scsi_sense_data_type sense_type;
4331
4332         if (inq_data != NULL) {
4333                 switch (SID_TYPE(inq_data)) {
4334                 case T_SEQUENTIAL:
4335                         break;
4336                 default:
4337                         goto bailout;
4338                         break;
4339                 }
4340         }
4341
4342         sense_type = scsi_sense_type(sense_data);
4343
4344         switch (sense_type) {
4345         case SSD_TYPE_DESC: {
4346                 struct scsi_sense_data_desc *sense;
4347                 struct scsi_sense_stream *stream;
4348
4349                 sense = (struct scsi_sense_data_desc *)sense_data;
4350
4351                 stream = (struct scsi_sense_stream *)scsi_find_desc(sense,
4352                     sense_len, SSD_DESC_STREAM);
4353                 if (stream == NULL)
4354                         goto bailout;
4355
4356                 *stream_bits = stream->byte3;
4357                 break;
4358         }
4359         case SSD_TYPE_FIXED: {
4360                 struct scsi_sense_data_fixed *sense;
4361
4362                 sense = (struct scsi_sense_data_fixed *)sense_data;
4363
4364                 if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags) == 0)
4365                         goto bailout;
4366
4367                 if ((sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK)) == 0)
4368                         goto bailout;
4369
4370                 *stream_bits = sense->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK);
4371                 break;
4372         }
4373         default:
4374                 goto bailout;
4375                 break;
4376         }
4377         return (0);
4378 bailout:
4379         return (1);
4380 }
4381
4382 void
4383 scsi_info_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4384                struct scsi_inquiry_data *inq_data, uint64_t info)
4385 {
4386         sbuf_printf(sb, "Info: %#jx", info);
4387 }
4388
4389 void
4390 scsi_command_sbuf(struct sbuf *sb, uint8_t *cdb, int cdb_len,
4391                   struct scsi_inquiry_data *inq_data, uint64_t csi)
4392 {
4393         sbuf_printf(sb, "Command Specific Info: %#jx", csi);
4394 }
4395
4396
4397 void
4398 scsi_progress_sbuf(struct sbuf *sb, uint16_t progress)
4399 {
4400         sbuf_printf(sb, "Progress: %d%% (%d/%d) complete",
4401                     (progress * 100) / SSD_SKS_PROGRESS_DENOM,
4402                     progress, SSD_SKS_PROGRESS_DENOM);
4403 }
4404
4405 /*
4406  * Returns 1 for failure (i.e. SKS isn't valid) and 0 for success.
4407  */
4408 int
4409 scsi_sks_sbuf(struct sbuf *sb, int sense_key, uint8_t *sks)
4410 {
4411         if ((sks[0] & SSD_SKS_VALID) == 0)
4412                 return (1);
4413
4414         switch (sense_key) {
4415         case SSD_KEY_ILLEGAL_REQUEST: {
4416                 struct scsi_sense_sks_field *field;
4417                 int bad_command;
4418                 char tmpstr[40];
4419
4420                 /*Field Pointer*/
4421                 field = (struct scsi_sense_sks_field *)sks;
4422
4423                 if (field->byte0 & SSD_SKS_FIELD_CMD)
4424                         bad_command = 1;
4425                 else
4426                         bad_command = 0;
4427
4428                 tmpstr[0] = '\0';
4429
4430                 /* Bit pointer is valid */
4431                 if (field->byte0 & SSD_SKS_BPV)
4432                         snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4433                                  field->byte0 & SSD_SKS_BIT_VALUE);
4434
4435                 sbuf_printf(sb, "%s byte %d %sis invalid",
4436                             bad_command ? "Command" : "Data",
4437                             scsi_2btoul(field->field), tmpstr);
4438                 break;
4439         }
4440         case SSD_KEY_UNIT_ATTENTION: {
4441                 struct scsi_sense_sks_overflow *overflow;
4442
4443                 overflow = (struct scsi_sense_sks_overflow *)sks;
4444
4445                 /*UA Condition Queue Overflow*/
4446                 sbuf_printf(sb, "Unit Attention Condition Queue %s",
4447                             (overflow->byte0 & SSD_SKS_OVERFLOW_SET) ?
4448                             "Overflowed" : "Did Not Overflow??");
4449                 break;
4450         }
4451         case SSD_KEY_RECOVERED_ERROR:
4452         case SSD_KEY_HARDWARE_ERROR:
4453         case SSD_KEY_MEDIUM_ERROR: {
4454                 struct scsi_sense_sks_retry *retry;
4455
4456                 /*Actual Retry Count*/
4457                 retry = (struct scsi_sense_sks_retry *)sks;
4458
4459                 sbuf_printf(sb, "Actual Retry Count: %d",
4460                             scsi_2btoul(retry->actual_retry_count));
4461                 break;
4462         }
4463         case SSD_KEY_NO_SENSE:
4464         case SSD_KEY_NOT_READY: {
4465                 struct scsi_sense_sks_progress *progress;
4466                 int progress_val;
4467
4468                 /*Progress Indication*/
4469                 progress = (struct scsi_sense_sks_progress *)sks;
4470                 progress_val = scsi_2btoul(progress->progress);
4471
4472                 scsi_progress_sbuf(sb, progress_val);
4473                 break;
4474         }
4475         case SSD_KEY_COPY_ABORTED: {
4476                 struct scsi_sense_sks_segment *segment;
4477                 char tmpstr[40];
4478
4479                 /*Segment Pointer*/
4480                 segment = (struct scsi_sense_sks_segment *)sks;
4481
4482                 tmpstr[0] = '\0';
4483
4484                 if (segment->byte0 & SSD_SKS_SEGMENT_BPV)
4485                         snprintf(tmpstr, sizeof(tmpstr), "bit %d ",
4486                                  segment->byte0 & SSD_SKS_SEGMENT_BITPTR);
4487
4488                 sbuf_printf(sb, "%s byte %d %sis invalid", (segment->byte0 &
4489                             SSD_SKS_SEGMENT_SD) ? "Segment" : "Data",
4490                             scsi_2btoul(segment->field), tmpstr);
4491                 break;
4492         }
4493         default:
4494                 sbuf_printf(sb, "Sense Key Specific: %#x,%#x", sks[0],
4495                             scsi_2btoul(&sks[1]));
4496                 break;
4497         }
4498
4499         return (0);
4500 }
4501
4502 void
4503 scsi_fru_sbuf(struct sbuf *sb, uint64_t fru)
4504 {
4505         sbuf_printf(sb, "Field Replaceable Unit: %d", (int)fru);
4506 }
4507
4508 void
4509 scsi_stream_sbuf(struct sbuf *sb, uint8_t stream_bits, uint64_t info)
4510 {
4511         int need_comma;
4512
4513         need_comma = 0;
4514         /*
4515          * XXX KDM this needs more descriptive decoding.
4516          */
4517         if (stream_bits & SSD_DESC_STREAM_FM) {
4518                 sbuf_printf(sb, "Filemark");
4519                 need_comma = 1;
4520         }
4521
4522         if (stream_bits & SSD_DESC_STREAM_EOM) {
4523                 sbuf_printf(sb, "%sEOM", (need_comma) ? "," : "");
4524                 need_comma = 1;
4525         }
4526
4527         if (stream_bits & SSD_DESC_STREAM_ILI)
4528                 sbuf_printf(sb, "%sILI", (need_comma) ? "," : "");
4529
4530         sbuf_printf(sb, ": Info: %#jx", (uintmax_t) info);
4531 }
4532
4533 void
4534 scsi_block_sbuf(struct sbuf *sb, uint8_t block_bits, uint64_t info)
4535 {
4536         if (block_bits & SSD_DESC_BLOCK_ILI)
4537                 sbuf_printf(sb, "ILI: residue %#jx", (uintmax_t) info);
4538 }
4539
4540 void
4541 scsi_sense_info_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4542                      u_int sense_len, uint8_t *cdb, int cdb_len,
4543                      struct scsi_inquiry_data *inq_data,
4544                      struct scsi_sense_desc_header *header)
4545 {
4546         struct scsi_sense_info *info;
4547
4548         info = (struct scsi_sense_info *)header;
4549
4550         scsi_info_sbuf(sb, cdb, cdb_len, inq_data, scsi_8btou64(info->info));
4551 }
4552
4553 void
4554 scsi_sense_command_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4555                         u_int sense_len, uint8_t *cdb, int cdb_len,
4556                         struct scsi_inquiry_data *inq_data,
4557                         struct scsi_sense_desc_header *header)
4558 {
4559         struct scsi_sense_command *command;
4560
4561         command = (struct scsi_sense_command *)header;
4562
4563         scsi_command_sbuf(sb, cdb, cdb_len, inq_data,
4564                           scsi_8btou64(command->command_info));
4565 }
4566
4567 void
4568 scsi_sense_sks_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4569                     u_int sense_len, uint8_t *cdb, int cdb_len,
4570                     struct scsi_inquiry_data *inq_data,
4571                     struct scsi_sense_desc_header *header)
4572 {
4573         struct scsi_sense_sks *sks;
4574         int error_code, sense_key, asc, ascq;
4575
4576         sks = (struct scsi_sense_sks *)header;
4577
4578         scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4579                                &asc, &ascq, /*show_errors*/ 1);
4580
4581         scsi_sks_sbuf(sb, sense_key, sks->sense_key_spec);
4582 }
4583
4584 void
4585 scsi_sense_fru_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4586                     u_int sense_len, uint8_t *cdb, int cdb_len,
4587                     struct scsi_inquiry_data *inq_data,
4588                     struct scsi_sense_desc_header *header)
4589 {
4590         struct scsi_sense_fru *fru;
4591
4592         fru = (struct scsi_sense_fru *)header;
4593
4594         scsi_fru_sbuf(sb, (uint64_t)fru->fru);
4595 }
4596
4597 void
4598 scsi_sense_stream_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4599                        u_int sense_len, uint8_t *cdb, int cdb_len,
4600                        struct scsi_inquiry_data *inq_data,
4601                        struct scsi_sense_desc_header *header)
4602 {
4603         struct scsi_sense_stream *stream;
4604         uint64_t info;
4605
4606         stream = (struct scsi_sense_stream *)header;
4607         info = 0;
4608
4609         scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4610
4611         scsi_stream_sbuf(sb, stream->byte3, info);
4612 }
4613
4614 void
4615 scsi_sense_block_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4616                       u_int sense_len, uint8_t *cdb, int cdb_len,
4617                       struct scsi_inquiry_data *inq_data,
4618                       struct scsi_sense_desc_header *header)
4619 {
4620         struct scsi_sense_block *block;
4621         uint64_t info;
4622
4623         block = (struct scsi_sense_block *)header;
4624         info = 0;
4625
4626         scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info, NULL);
4627
4628         scsi_block_sbuf(sb, block->byte3, info);
4629 }
4630
4631 void
4632 scsi_sense_progress_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4633                          u_int sense_len, uint8_t *cdb, int cdb_len,
4634                          struct scsi_inquiry_data *inq_data,
4635                          struct scsi_sense_desc_header *header)
4636 {
4637         struct scsi_sense_progress *progress;
4638         const char *sense_key_desc;
4639         const char *asc_desc;
4640         int progress_val;
4641
4642         progress = (struct scsi_sense_progress *)header;
4643
4644         /*
4645          * Get descriptions for the sense key, ASC, and ASCQ in the
4646          * progress descriptor.  These could be different than the values
4647          * in the overall sense data.
4648          */
4649         scsi_sense_desc(progress->sense_key, progress->add_sense_code,
4650                         progress->add_sense_code_qual, inq_data,
4651                         &sense_key_desc, &asc_desc);
4652
4653         progress_val = scsi_2btoul(progress->progress);
4654
4655         /*
4656          * The progress indicator is for the operation described by the
4657          * sense key, ASC, and ASCQ in the descriptor.
4658          */
4659         sbuf_cat(sb, sense_key_desc);
4660         sbuf_printf(sb, " asc:%x,%x (%s): ", progress->add_sense_code, 
4661                     progress->add_sense_code_qual, asc_desc);
4662         scsi_progress_sbuf(sb, progress_val);
4663 }
4664
4665 void
4666 scsi_sense_forwarded_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4667                          u_int sense_len, uint8_t *cdb, int cdb_len,
4668                          struct scsi_inquiry_data *inq_data,
4669                          struct scsi_sense_desc_header *header)
4670 {
4671         struct scsi_sense_forwarded *forwarded;
4672         const char *sense_key_desc;
4673         const char *asc_desc;
4674         int error_code, sense_key, asc, ascq;
4675
4676         forwarded = (struct scsi_sense_forwarded *)header;
4677         scsi_extract_sense_len((struct scsi_sense_data *)forwarded->sense_data,
4678             forwarded->length - 2, &error_code, &sense_key, &asc, &ascq, 1);
4679         scsi_sense_desc(sense_key, asc, ascq, NULL, &sense_key_desc, &asc_desc);
4680
4681         sbuf_printf(sb, "Forwarded sense: %s asc:%x,%x (%s): ",
4682             sense_key_desc, asc, ascq, asc_desc);
4683 }
4684
4685 /*
4686  * Generic sense descriptor printing routine.  This is used when we have
4687  * not yet implemented a specific printing routine for this descriptor.
4688  */
4689 void
4690 scsi_sense_generic_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4691                         u_int sense_len, uint8_t *cdb, int cdb_len,
4692                         struct scsi_inquiry_data *inq_data,
4693                         struct scsi_sense_desc_header *header)
4694 {
4695         int i;
4696         uint8_t *buf_ptr;
4697
4698         sbuf_printf(sb, "Descriptor %#x:", header->desc_type);
4699
4700         buf_ptr = (uint8_t *)&header[1];
4701
4702         for (i = 0; i < header->length; i++, buf_ptr++)
4703                 sbuf_printf(sb, " %02x", *buf_ptr);
4704 }
4705
4706 /*
4707  * Keep this list in numeric order.  This speeds the array traversal.
4708  */
4709 struct scsi_sense_desc_printer {
4710         uint8_t desc_type;
4711         /*
4712          * The function arguments here are the superset of what is needed
4713          * to print out various different descriptors.  Command and
4714          * information descriptors need inquiry data and command type.
4715          * Sense key specific descriptors need the sense key.
4716          *
4717          * The sense, cdb, and inquiry data arguments may be NULL, but the
4718          * information printed may not be fully decoded as a result.
4719          */
4720         void (*print_func)(struct sbuf *sb, struct scsi_sense_data *sense,
4721                            u_int sense_len, uint8_t *cdb, int cdb_len,
4722                            struct scsi_inquiry_data *inq_data,
4723                            struct scsi_sense_desc_header *header);
4724 } scsi_sense_printers[] = {
4725         {SSD_DESC_INFO, scsi_sense_info_sbuf},
4726         {SSD_DESC_COMMAND, scsi_sense_command_sbuf},
4727         {SSD_DESC_SKS, scsi_sense_sks_sbuf},
4728         {SSD_DESC_FRU, scsi_sense_fru_sbuf},
4729         {SSD_DESC_STREAM, scsi_sense_stream_sbuf},
4730         {SSD_DESC_BLOCK, scsi_sense_block_sbuf},
4731         {SSD_DESC_PROGRESS, scsi_sense_progress_sbuf},
4732         {SSD_DESC_FORWARDED, scsi_sense_forwarded_sbuf}
4733 };
4734
4735 void
4736 scsi_sense_desc_sbuf(struct sbuf *sb, struct scsi_sense_data *sense,
4737                      u_int sense_len, uint8_t *cdb, int cdb_len,
4738                      struct scsi_inquiry_data *inq_data,
4739                      struct scsi_sense_desc_header *header)
4740 {
4741         int i;
4742
4743         for (i = 0; i < (sizeof(scsi_sense_printers) /
4744              sizeof(scsi_sense_printers[0])); i++) {
4745                 struct scsi_sense_desc_printer *printer;
4746
4747                 printer = &scsi_sense_printers[i];
4748
4749                 /*
4750                  * The list is sorted, so quit if we've passed our
4751                  * descriptor number.
4752                  */
4753                 if (printer->desc_type > header->desc_type)
4754                         break;
4755
4756                 if (printer->desc_type != header->desc_type)
4757                         continue;
4758
4759                 printer->print_func(sb, sense, sense_len, cdb, cdb_len,
4760                                     inq_data, header);
4761
4762                 return;
4763         }
4764
4765         /*
4766          * No specific printing routine, so use the generic routine.
4767          */
4768         scsi_sense_generic_sbuf(sb, sense, sense_len, cdb, cdb_len,
4769                                 inq_data, header);
4770 }
4771
4772 scsi_sense_data_type
4773 scsi_sense_type(struct scsi_sense_data *sense_data)
4774 {
4775         switch (sense_data->error_code & SSD_ERRCODE) {
4776         case SSD_DESC_CURRENT_ERROR:
4777         case SSD_DESC_DEFERRED_ERROR:
4778                 return (SSD_TYPE_DESC);
4779                 break;
4780         case SSD_CURRENT_ERROR:
4781         case SSD_DEFERRED_ERROR:
4782                 return (SSD_TYPE_FIXED);
4783                 break;
4784         default:
4785                 break;
4786         }
4787
4788         return (SSD_TYPE_NONE);
4789 }
4790
4791 struct scsi_print_sense_info {
4792         struct sbuf *sb;
4793         char *path_str;
4794         uint8_t *cdb;
4795         int cdb_len;
4796         struct scsi_inquiry_data *inq_data;
4797 };
4798
4799 static int
4800 scsi_print_desc_func(struct scsi_sense_data_desc *sense, u_int sense_len,
4801                      struct scsi_sense_desc_header *header, void *arg)
4802 {
4803         struct scsi_print_sense_info *print_info;
4804
4805         print_info = (struct scsi_print_sense_info *)arg;
4806
4807         switch (header->desc_type) {
4808         case SSD_DESC_INFO:
4809         case SSD_DESC_FRU:
4810         case SSD_DESC_COMMAND:
4811         case SSD_DESC_SKS:
4812         case SSD_DESC_BLOCK:
4813         case SSD_DESC_STREAM:
4814                 /*
4815                  * We have already printed these descriptors, if they are
4816                  * present.
4817                  */
4818                 break;
4819         default: {
4820                 sbuf_printf(print_info->sb, "%s", print_info->path_str);
4821                 scsi_sense_desc_sbuf(print_info->sb,
4822                                      (struct scsi_sense_data *)sense, sense_len,
4823                                      print_info->cdb, print_info->cdb_len,
4824                                      print_info->inq_data, header);
4825                 sbuf_printf(print_info->sb, "\n");
4826                 break;
4827         }
4828         }
4829
4830         /*
4831          * Tell the iterator that we want to see more descriptors if they
4832          * are present.
4833          */
4834         return (0);
4835 }
4836
4837 void
4838 scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len,
4839                      struct sbuf *sb, char *path_str,
4840                      struct scsi_inquiry_data *inq_data, uint8_t *cdb,
4841                      int cdb_len)
4842 {
4843         int error_code, sense_key, asc, ascq;
4844
4845         sbuf_cat(sb, path_str);
4846
4847         scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
4848                                &asc, &ascq, /*show_errors*/ 1);
4849
4850         sbuf_printf(sb, "SCSI sense: ");
4851         switch (error_code) {
4852         case SSD_DEFERRED_ERROR:
4853         case SSD_DESC_DEFERRED_ERROR:
4854                 sbuf_printf(sb, "Deferred error: ");
4855
4856                 /* FALLTHROUGH */
4857         case SSD_CURRENT_ERROR:
4858         case SSD_DESC_CURRENT_ERROR:
4859         {
4860                 struct scsi_sense_data_desc *desc_sense;
4861                 struct scsi_print_sense_info print_info;
4862                 const char *sense_key_desc;
4863                 const char *asc_desc;
4864                 uint8_t sks[3];
4865                 uint64_t val;
4866                 int info_valid;
4867
4868                 /*
4869                  * Get descriptions for the sense key, ASC, and ASCQ.  If
4870                  * these aren't present in the sense data (i.e. the sense
4871                  * data isn't long enough), the -1 values that
4872                  * scsi_extract_sense_len() returns will yield default
4873                  * or error descriptions.
4874                  */
4875                 scsi_sense_desc(sense_key, asc, ascq, inq_data,
4876                                 &sense_key_desc, &asc_desc);
4877
4878                 /*
4879                  * We first print the sense key and ASC/ASCQ.
4880                  */
4881                 sbuf_cat(sb, sense_key_desc);
4882                 sbuf_printf(sb, " asc:%x,%x (%s)\n", asc, ascq, asc_desc);
4883
4884                 /*
4885                  * Get the info field if it is valid.
4886                  */
4887                 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO,
4888                                         &val, NULL) == 0)
4889                         info_valid = 1;
4890                 else
4891                         info_valid = 0;
4892
4893                 if (info_valid != 0) {
4894                         uint8_t bits;
4895
4896                         /*
4897                          * Determine whether we have any block or stream
4898                          * device-specific information.
4899                          */
4900                         if (scsi_get_block_info(sense, sense_len, inq_data,
4901                                                 &bits) == 0) {
4902                                 sbuf_cat(sb, path_str);
4903                                 scsi_block_sbuf(sb, bits, val);
4904                                 sbuf_printf(sb, "\n");
4905                         } else if (scsi_get_stream_info(sense, sense_len,
4906                                                         inq_data, &bits) == 0) {
4907                                 sbuf_cat(sb, path_str);
4908                                 scsi_stream_sbuf(sb, bits, val);
4909                                 sbuf_printf(sb, "\n");
4910                         } else if (val != 0) {
4911                                 /*
4912                                  * The information field can be valid but 0.
4913                                  * If the block or stream bits aren't set,
4914                                  * and this is 0, it isn't terribly useful
4915                                  * to print it out.
4916                                  */
4917                                 sbuf_cat(sb, path_str);
4918                                 scsi_info_sbuf(sb, cdb, cdb_len, inq_data, val);
4919                                 sbuf_printf(sb, "\n");
4920                         }
4921                 }
4922
4923                 /* 
4924                  * Print the FRU.
4925                  */
4926                 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU,
4927                                         &val, NULL) == 0) {
4928                         sbuf_cat(sb, path_str);
4929                         scsi_fru_sbuf(sb, val);
4930                         sbuf_printf(sb, "\n");
4931                 }
4932
4933                 /*
4934                  * Print any command-specific information.
4935                  */
4936                 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
4937                                         &val, NULL) == 0) {
4938                         sbuf_cat(sb, path_str);
4939                         scsi_command_sbuf(sb, cdb, cdb_len, inq_data, val);
4940                         sbuf_printf(sb, "\n");
4941                 }
4942
4943                 /*
4944                  * Print out any sense-key-specific information.
4945                  */
4946                 if (scsi_get_sks(sense, sense_len, sks) == 0) {
4947                         sbuf_cat(sb, path_str);
4948                         scsi_sks_sbuf(sb, sense_key, sks);
4949                         sbuf_printf(sb, "\n");
4950                 }
4951
4952                 /*
4953                  * If this is fixed sense, we're done.  If we have
4954                  * descriptor sense, we might have more information
4955                  * available.
4956                  */
4957                 if (scsi_sense_type(sense) != SSD_TYPE_DESC)
4958                         break;
4959
4960                 desc_sense = (struct scsi_sense_data_desc *)sense;
4961
4962                 print_info.sb = sb;
4963                 print_info.path_str = path_str;
4964                 print_info.cdb = cdb;
4965                 print_info.cdb_len = cdb_len;
4966                 print_info.inq_data = inq_data;
4967
4968                 /*
4969                  * Print any sense descriptors that we have not already printed.
4970                  */
4971                 scsi_desc_iterate(desc_sense, sense_len, scsi_print_desc_func,
4972                                   &print_info);
4973                 break;
4974
4975         }
4976         case -1:
4977                 /*
4978                  * scsi_extract_sense_len() sets values to -1 if the
4979                  * show_errors flag is set and they aren't present in the
4980                  * sense data.  This means that sense_len is 0.
4981                  */
4982                 sbuf_printf(sb, "No sense data present\n");
4983                 break;
4984         default: {
4985                 sbuf_printf(sb, "Error code 0x%x", error_code);
4986                 if (sense->error_code & SSD_ERRCODE_VALID) {
4987                         struct scsi_sense_data_fixed *fixed_sense;
4988
4989                         fixed_sense = (struct scsi_sense_data_fixed *)sense;
4990
4991                         if (SSD_FIXED_IS_PRESENT(fixed_sense, sense_len, info)){
4992                                 uint32_t info;
4993
4994                                 info = scsi_4btoul(fixed_sense->info);
4995
4996                                 sbuf_printf(sb, " at block no. %d (decimal)",
4997                                             info);
4998                         }
4999                 }
5000                 sbuf_printf(sb, "\n");
5001                 break;
5002         }
5003         }
5004 }
5005
5006 /*
5007  * scsi_sense_sbuf() returns 0 for success and -1 for failure.
5008  */
5009 #ifdef _KERNEL
5010 int
5011 scsi_sense_sbuf(struct ccb_scsiio *csio, struct sbuf *sb,
5012                 scsi_sense_string_flags flags)
5013 #else /* !_KERNEL */
5014 int
5015 scsi_sense_sbuf(struct cam_device *device, struct ccb_scsiio *csio, 
5016                 struct sbuf *sb, scsi_sense_string_flags flags)
5017 #endif /* _KERNEL/!_KERNEL */
5018 {
5019         struct    scsi_sense_data *sense;
5020         struct    scsi_inquiry_data *inq_data;
5021 #ifdef _KERNEL
5022         struct    ccb_getdev *cgd;
5023 #endif /* _KERNEL */
5024         char      path_str[64];
5025         uint8_t   *cdb;
5026
5027 #ifndef _KERNEL
5028         if (device == NULL)
5029                 return(-1);
5030 #endif /* !_KERNEL */
5031         if ((csio == NULL) || (sb == NULL))
5032                 return(-1);
5033
5034         /*
5035          * If the CDB is a physical address, we can't deal with it..
5036          */
5037         if ((csio->ccb_h.flags & CAM_CDB_PHYS) != 0)
5038                 flags &= ~SSS_FLAG_PRINT_COMMAND;
5039
5040 #ifdef _KERNEL
5041         xpt_path_string(csio->ccb_h.path, path_str, sizeof(path_str));
5042 #else /* !_KERNEL */
5043         cam_path_string(device, path_str, sizeof(path_str));
5044 #endif /* _KERNEL/!_KERNEL */
5045
5046 #ifdef _KERNEL
5047         if ((cgd = (struct ccb_getdev*)xpt_alloc_ccb_nowait()) == NULL)
5048                 return(-1);
5049         /*
5050          * Get the device information.
5051          */
5052         xpt_setup_ccb(&cgd->ccb_h,
5053                       csio->ccb_h.path,
5054                       CAM_PRIORITY_NORMAL);
5055         cgd->ccb_h.func_code = XPT_GDEV_TYPE;
5056         xpt_action((union ccb *)cgd);
5057
5058         /*
5059          * If the device is unconfigured, just pretend that it is a hard
5060          * drive.  scsi_op_desc() needs this.
5061          */
5062         if (cgd->ccb_h.status == CAM_DEV_NOT_THERE)
5063                 cgd->inq_data.device = T_DIRECT;
5064
5065         inq_data = &cgd->inq_data;
5066
5067 #else /* !_KERNEL */
5068
5069         inq_data = &device->inq_data;
5070
5071 #endif /* _KERNEL/!_KERNEL */
5072
5073         sense = NULL;
5074
5075         if (flags & SSS_FLAG_PRINT_COMMAND) {
5076
5077                 sbuf_cat(sb, path_str);
5078
5079 #ifdef _KERNEL
5080                 scsi_command_string(csio, sb);
5081 #else /* !_KERNEL */
5082                 scsi_command_string(device, csio, sb);
5083 #endif /* _KERNEL/!_KERNEL */
5084                 sbuf_printf(sb, "\n");
5085         }
5086
5087         /*
5088          * If the sense data is a physical pointer, forget it.
5089          */
5090         if (csio->ccb_h.flags & CAM_SENSE_PTR) {
5091                 if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5092 #ifdef _KERNEL
5093                         xpt_free_ccb((union ccb*)cgd);
5094 #endif /* _KERNEL/!_KERNEL */
5095                         return(-1);
5096                 } else {
5097                         /* 
5098                          * bcopy the pointer to avoid unaligned access
5099                          * errors on finicky architectures.  We don't
5100                          * ensure that the sense data is pointer aligned.
5101                          */
5102                         bcopy(&csio->sense_data, &sense, 
5103                               sizeof(struct scsi_sense_data *));
5104                 }
5105         } else {
5106                 /*
5107                  * If the physical sense flag is set, but the sense pointer
5108                  * is not also set, we assume that the user is an idiot and
5109                  * return.  (Well, okay, it could be that somehow, the
5110                  * entire csio is physical, but we would have probably core
5111                  * dumped on one of the bogus pointer deferences above
5112                  * already.)
5113                  */
5114                 if (csio->ccb_h.flags & CAM_SENSE_PHYS) {
5115 #ifdef _KERNEL
5116                         xpt_free_ccb((union ccb*)cgd);
5117 #endif /* _KERNEL/!_KERNEL */
5118                         return(-1);
5119                 } else
5120                         sense = &csio->sense_data;
5121         }
5122
5123         if (csio->ccb_h.flags & CAM_CDB_POINTER)
5124                 cdb = csio->cdb_io.cdb_ptr;
5125         else
5126                 cdb = csio->cdb_io.cdb_bytes;
5127
5128         scsi_sense_only_sbuf(sense, csio->sense_len - csio->sense_resid, sb,
5129                              path_str, inq_data, cdb, csio->cdb_len);
5130                          
5131 #ifdef _KERNEL
5132         xpt_free_ccb((union ccb*)cgd);
5133 #endif /* _KERNEL/!_KERNEL */
5134         return(0);
5135 }
5136
5137
5138
5139 #ifdef _KERNEL
5140 char *
5141 scsi_sense_string(struct ccb_scsiio *csio, char *str, int str_len)
5142 #else /* !_KERNEL */
5143 char *
5144 scsi_sense_string(struct cam_device *device, struct ccb_scsiio *csio,
5145                   char *str, int str_len)
5146 #endif /* _KERNEL/!_KERNEL */
5147 {
5148         struct sbuf sb;
5149
5150         sbuf_new(&sb, str, str_len, 0);
5151
5152 #ifdef _KERNEL
5153         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5154 #else /* !_KERNEL */
5155         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5156 #endif /* _KERNEL/!_KERNEL */
5157
5158         sbuf_finish(&sb);
5159
5160         return(sbuf_data(&sb));
5161 }
5162
5163 #ifdef _KERNEL
5164 void 
5165 scsi_sense_print(struct ccb_scsiio *csio)
5166 {
5167         struct sbuf sb;
5168         char str[512];
5169
5170         sbuf_new(&sb, str, sizeof(str), 0);
5171
5172         scsi_sense_sbuf(csio, &sb, SSS_FLAG_PRINT_COMMAND);
5173
5174         sbuf_finish(&sb);
5175
5176         printf("%s", sbuf_data(&sb));
5177 }
5178
5179 #else /* !_KERNEL */
5180 void
5181 scsi_sense_print(struct cam_device *device, struct ccb_scsiio *csio, 
5182                  FILE *ofile)
5183 {
5184         struct sbuf sb;
5185         char str[512];
5186
5187         if ((device == NULL) || (csio == NULL) || (ofile == NULL))
5188                 return;
5189
5190         sbuf_new(&sb, str, sizeof(str), 0);
5191
5192         scsi_sense_sbuf(device, csio, &sb, SSS_FLAG_PRINT_COMMAND);
5193
5194         sbuf_finish(&sb);
5195
5196         fprintf(ofile, "%s", sbuf_data(&sb));
5197 }
5198
5199 #endif /* _KERNEL/!_KERNEL */
5200
5201 /*
5202  * Extract basic sense information.  This is backward-compatible with the
5203  * previous implementation.  For new implementations,
5204  * scsi_extract_sense_len() is recommended.
5205  */
5206 void
5207 scsi_extract_sense(struct scsi_sense_data *sense_data, int *error_code,
5208                    int *sense_key, int *asc, int *ascq)
5209 {
5210         scsi_extract_sense_len(sense_data, sizeof(*sense_data), error_code,
5211                                sense_key, asc, ascq, /*show_errors*/ 0);
5212 }
5213
5214 /*
5215  * Extract basic sense information from SCSI I/O CCB structure.
5216  */
5217 int
5218 scsi_extract_sense_ccb(union ccb *ccb,
5219     int *error_code, int *sense_key, int *asc, int *ascq)
5220 {
5221         struct scsi_sense_data *sense_data;
5222
5223         /* Make sure there are some sense data we can access. */
5224         if (ccb->ccb_h.func_code != XPT_SCSI_IO ||
5225             (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
5226             (ccb->csio.scsi_status != SCSI_STATUS_CHECK_COND) ||
5227             (ccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0 ||
5228             (ccb->ccb_h.flags & CAM_SENSE_PHYS))
5229                 return (0);
5230
5231         if (ccb->ccb_h.flags & CAM_SENSE_PTR)
5232                 bcopy(&ccb->csio.sense_data, &sense_data,
5233                     sizeof(struct scsi_sense_data *));
5234         else
5235                 sense_data = &ccb->csio.sense_data;
5236         scsi_extract_sense_len(sense_data,
5237             ccb->csio.sense_len - ccb->csio.sense_resid,
5238             error_code, sense_key, asc, ascq, 1);
5239         if (*error_code == -1)
5240                 return (0);
5241         return (1);
5242 }
5243
5244 /*
5245  * Extract basic sense information.  If show_errors is set, sense values
5246  * will be set to -1 if they are not present.
5247  */
5248 void
5249 scsi_extract_sense_len(struct scsi_sense_data *sense_data, u_int sense_len,
5250                        int *error_code, int *sense_key, int *asc, int *ascq,
5251                        int show_errors)
5252 {
5253         /*
5254          * If we have no length, we have no sense.
5255          */
5256         if (sense_len == 0) {
5257                 if (show_errors == 0) {
5258                         *error_code = 0;
5259                         *sense_key = 0;
5260                         *asc = 0;
5261                         *ascq = 0;
5262                 } else {
5263                         *error_code = -1;
5264                         *sense_key = -1;
5265                         *asc = -1;
5266                         *ascq = -1;
5267                 }
5268                 return;
5269         }
5270
5271         *error_code = sense_data->error_code & SSD_ERRCODE;
5272
5273         switch (*error_code) {
5274         case SSD_DESC_CURRENT_ERROR:
5275         case SSD_DESC_DEFERRED_ERROR: {
5276                 struct scsi_sense_data_desc *sense;
5277
5278                 sense = (struct scsi_sense_data_desc *)sense_data;
5279
5280                 if (SSD_DESC_IS_PRESENT(sense, sense_len, sense_key))
5281                         *sense_key = sense->sense_key & SSD_KEY;
5282                 else
5283                         *sense_key = (show_errors) ? -1 : 0;
5284
5285                 if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code))
5286                         *asc = sense->add_sense_code;
5287                 else
5288                         *asc = (show_errors) ? -1 : 0;
5289
5290                 if (SSD_DESC_IS_PRESENT(sense, sense_len, add_sense_code_qual))
5291                         *ascq = sense->add_sense_code_qual;
5292                 else
5293                         *ascq = (show_errors) ? -1 : 0;
5294                 break;
5295         }
5296         case SSD_CURRENT_ERROR:
5297         case SSD_DEFERRED_ERROR:
5298         default: {
5299                 struct scsi_sense_data_fixed *sense;
5300
5301                 sense = (struct scsi_sense_data_fixed *)sense_data;
5302
5303                 if (SSD_FIXED_IS_PRESENT(sense, sense_len, flags))
5304                         *sense_key = sense->flags & SSD_KEY;
5305                 else
5306                         *sense_key = (show_errors) ? -1 : 0;
5307
5308                 if ((SSD_FIXED_IS_PRESENT(sense, sense_len, add_sense_code))
5309                  && (SSD_FIXED_IS_FILLED(sense, add_sense_code)))
5310                         *asc = sense->add_sense_code;
5311                 else
5312                         *asc = (show_errors) ? -1 : 0;
5313
5314                 if ((SSD_FIXED_IS_PRESENT(sense, sense_len,add_sense_code_qual))
5315                  && (SSD_FIXED_IS_FILLED(sense, add_sense_code_qual)))
5316                         *ascq = sense->add_sense_code_qual;
5317                 else
5318                         *ascq = (show_errors) ? -1 : 0;
5319                 break;
5320         }
5321         }
5322 }
5323
5324 int
5325 scsi_get_sense_key(struct scsi_sense_data *sense_data, u_int sense_len,
5326                    int show_errors)
5327 {
5328         int error_code, sense_key, asc, ascq;
5329
5330         scsi_extract_sense_len(sense_data, sense_len, &error_code,
5331                                &sense_key, &asc, &ascq, show_errors);
5332
5333         return (sense_key);
5334 }
5335
5336 int
5337 scsi_get_asc(struct scsi_sense_data *sense_data, u_int sense_len,
5338              int show_errors)
5339 {
5340         int error_code, sense_key, asc, ascq;
5341
5342         scsi_extract_sense_len(sense_data, sense_len, &error_code,
5343                                &sense_key, &asc, &ascq, show_errors);
5344
5345         return (asc);
5346 }
5347
5348 int
5349 scsi_get_ascq(struct scsi_sense_data *sense_data, u_int sense_len,
5350               int show_errors)
5351 {
5352         int error_code, sense_key, asc, ascq;
5353
5354         scsi_extract_sense_len(sense_data, sense_len, &error_code,
5355                                &sense_key, &asc, &ascq, show_errors);
5356
5357         return (ascq);
5358 }
5359
5360 /*
5361  * This function currently requires at least 36 bytes, or
5362  * SHORT_INQUIRY_LENGTH, worth of data to function properly.  If this
5363  * function needs more or less data in the future, another length should be
5364  * defined in scsi_all.h to indicate the minimum amount of data necessary
5365  * for this routine to function properly.
5366  */
5367 void
5368 scsi_print_inquiry(struct scsi_inquiry_data *inq_data)
5369 {
5370         u_int8_t type;
5371         char *dtype, *qtype;
5372         char vendor[16], product[48], revision[16], rstr[12];
5373
5374         type = SID_TYPE(inq_data);
5375
5376         /*
5377          * Figure out basic device type and qualifier.
5378          */
5379         if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data)) {
5380                 qtype = " (vendor-unique qualifier)";
5381         } else {
5382                 switch (SID_QUAL(inq_data)) {
5383                 case SID_QUAL_LU_CONNECTED:
5384                         qtype = "";
5385                         break;
5386
5387                 case SID_QUAL_LU_OFFLINE:
5388                         qtype = " (offline)";
5389                         break;
5390
5391                 case SID_QUAL_RSVD:
5392                         qtype = " (reserved qualifier)";
5393                         break;
5394                 default:
5395                 case SID_QUAL_BAD_LU:
5396                         qtype = " (LUN not supported)";
5397                         break;
5398                 }
5399         }
5400
5401         switch (type) {
5402         case T_DIRECT:
5403                 dtype = "Direct Access";
5404                 break;
5405         case T_SEQUENTIAL:
5406                 dtype = "Sequential Access";
5407                 break;
5408         case T_PRINTER:
5409                 dtype = "Printer";
5410                 break;
5411         case T_PROCESSOR:
5412                 dtype = "Processor";
5413                 break;
5414         case T_WORM:
5415                 dtype = "WORM";
5416                 break;
5417         case T_CDROM:
5418                 dtype = "CD-ROM";
5419                 break;
5420         case T_SCANNER:
5421                 dtype = "Scanner";
5422                 break;
5423         case T_OPTICAL:
5424                 dtype = "Optical";
5425                 break;
5426         case T_CHANGER:
5427                 dtype = "Changer";
5428                 break;
5429         case T_COMM:
5430                 dtype = "Communication";
5431                 break;
5432         case T_STORARRAY:
5433                 dtype = "Storage Array";
5434                 break;
5435         case T_ENCLOSURE:
5436                 dtype = "Enclosure Services";
5437                 break;
5438         case T_RBC:
5439                 dtype = "Simplified Direct Access";
5440                 break;
5441         case T_OCRW:
5442                 dtype = "Optical Card Read/Write";
5443                 break;
5444         case T_OSD:
5445                 dtype = "Object-Based Storage";
5446                 break;
5447         case T_ADC:
5448                 dtype = "Automation/Drive Interface";
5449                 break;
5450         case T_NODEVICE:
5451                 dtype = "Uninstalled";
5452                 break;
5453         default:
5454                 dtype = "unknown";
5455                 break;
5456         }
5457
5458         cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5459                    sizeof(vendor));
5460         cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5461                    sizeof(product));
5462         cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5463                    sizeof(revision));
5464
5465         if (SID_ANSI_REV(inq_data) == SCSI_REV_0)
5466                 snprintf(rstr, sizeof(rstr), "SCSI");
5467         else if (SID_ANSI_REV(inq_data) <= SCSI_REV_SPC) {
5468                 snprintf(rstr, sizeof(rstr), "SCSI-%d",
5469                     SID_ANSI_REV(inq_data));
5470         } else {
5471                 snprintf(rstr, sizeof(rstr), "SPC-%d SCSI",
5472                     SID_ANSI_REV(inq_data) - 2);
5473         }
5474         printf("<%s %s %s> %s %s %s device%s\n",
5475                vendor, product, revision,
5476                SID_IS_REMOVABLE(inq_data) ? "Removable" : "Fixed",
5477                dtype, rstr, qtype);
5478 }
5479
5480 void
5481 scsi_print_inquiry_short(struct scsi_inquiry_data *inq_data)
5482 {
5483         char vendor[16], product[48], revision[16];
5484
5485         cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
5486                    sizeof(vendor));
5487         cam_strvis(product, inq_data->product, sizeof(inq_data->product),
5488                    sizeof(product));
5489         cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
5490                    sizeof(revision));
5491
5492         printf("<%s %s %s>", vendor, product, revision);
5493 }
5494
5495 /*
5496  * Table of syncrates that don't follow the "divisible by 4"
5497  * rule. This table will be expanded in future SCSI specs.
5498  */
5499 static struct {
5500         u_int period_factor;
5501         u_int period;   /* in 100ths of ns */
5502 } scsi_syncrates[] = {
5503         { 0x08, 625 },  /* FAST-160 */
5504         { 0x09, 1250 }, /* FAST-80 */
5505         { 0x0a, 2500 }, /* FAST-40 40MHz */
5506         { 0x0b, 3030 }, /* FAST-40 33MHz */
5507         { 0x0c, 5000 }  /* FAST-20 */
5508 };
5509
5510 /*
5511  * Return the frequency in kHz corresponding to the given
5512  * sync period factor.
5513  */
5514 u_int
5515 scsi_calc_syncsrate(u_int period_factor)
5516 {
5517         int i;
5518         int num_syncrates;
5519
5520         /*
5521          * It's a bug if period is zero, but if it is anyway, don't
5522          * die with a divide fault- instead return something which
5523          * 'approximates' async
5524          */
5525         if (period_factor == 0) {
5526                 return (3300);
5527         }
5528
5529         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
5530         /* See if the period is in the "exception" table */
5531         for (i = 0; i < num_syncrates; i++) {
5532
5533                 if (period_factor == scsi_syncrates[i].period_factor) {
5534                         /* Period in kHz */
5535                         return (100000000 / scsi_syncrates[i].period);
5536                 }
5537         }
5538
5539         /*
5540          * Wasn't in the table, so use the standard
5541          * 4 times conversion.
5542          */
5543         return (10000000 / (period_factor * 4 * 10));
5544 }
5545
5546 /*
5547  * Return the SCSI sync parameter that corresponsd to
5548  * the passed in period in 10ths of ns.
5549  */
5550 u_int
5551 scsi_calc_syncparam(u_int period)
5552 {
5553         int i;
5554         int num_syncrates;
5555
5556         if (period == 0)
5557                 return (~0);    /* Async */
5558
5559         /* Adjust for exception table being in 100ths. */
5560         period *= 10;
5561         num_syncrates = sizeof(scsi_syncrates) / sizeof(scsi_syncrates[0]);
5562         /* See if the period is in the "exception" table */
5563         for (i = 0; i < num_syncrates; i++) {
5564
5565                 if (period <= scsi_syncrates[i].period) {
5566                         /* Period in 100ths of ns */
5567                         return (scsi_syncrates[i].period_factor);
5568                 }
5569         }
5570
5571         /*
5572          * Wasn't in the table, so use the standard
5573          * 1/4 period in ns conversion.
5574          */
5575         return (period/400);
5576 }
5577
5578 int
5579 scsi_devid_is_naa_ieee_reg(uint8_t *bufp)
5580 {
5581         struct scsi_vpd_id_descriptor *descr;
5582         struct scsi_vpd_id_naa_basic *naa;
5583
5584         descr = (struct scsi_vpd_id_descriptor *)bufp;
5585         naa = (struct scsi_vpd_id_naa_basic *)descr->identifier;
5586         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5587                 return 0;
5588         if (descr->length < sizeof(struct scsi_vpd_id_naa_ieee_reg))
5589                 return 0;
5590         if ((naa->naa >> SVPD_ID_NAA_NAA_SHIFT) != SVPD_ID_NAA_IEEE_REG)
5591                 return 0;
5592         return 1;
5593 }
5594
5595 int
5596 scsi_devid_is_sas_target(uint8_t *bufp)
5597 {
5598         struct scsi_vpd_id_descriptor *descr;
5599
5600         descr = (struct scsi_vpd_id_descriptor *)bufp;
5601         if (!scsi_devid_is_naa_ieee_reg(bufp))
5602                 return 0;
5603         if ((descr->id_type & SVPD_ID_PIV) == 0) /* proto field reserved */
5604                 return 0;
5605         if ((descr->proto_codeset >> SVPD_ID_PROTO_SHIFT) != SCSI_PROTO_SAS)
5606                 return 0;
5607         return 1;
5608 }
5609
5610 int
5611 scsi_devid_is_lun_eui64(uint8_t *bufp)
5612 {
5613         struct scsi_vpd_id_descriptor *descr;
5614
5615         descr = (struct scsi_vpd_id_descriptor *)bufp;
5616         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5617                 return 0;
5618         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_EUI64)
5619                 return 0;
5620         return 1;
5621 }
5622
5623 int
5624 scsi_devid_is_lun_naa(uint8_t *bufp)
5625 {
5626         struct scsi_vpd_id_descriptor *descr;
5627
5628         descr = (struct scsi_vpd_id_descriptor *)bufp;
5629         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5630                 return 0;
5631         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5632                 return 0;
5633         return 1;
5634 }
5635
5636 int
5637 scsi_devid_is_lun_t10(uint8_t *bufp)
5638 {
5639         struct scsi_vpd_id_descriptor *descr;
5640
5641         descr = (struct scsi_vpd_id_descriptor *)bufp;
5642         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5643                 return 0;
5644         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_T10)
5645                 return 0;
5646         return 1;
5647 }
5648
5649 int
5650 scsi_devid_is_lun_name(uint8_t *bufp)
5651 {
5652         struct scsi_vpd_id_descriptor *descr;
5653
5654         descr = (struct scsi_vpd_id_descriptor *)bufp;
5655         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5656                 return 0;
5657         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_SCSI_NAME)
5658                 return 0;
5659         return 1;
5660 }
5661
5662 int
5663 scsi_devid_is_lun_md5(uint8_t *bufp)
5664 {
5665         struct scsi_vpd_id_descriptor *descr;
5666
5667         descr = (struct scsi_vpd_id_descriptor *)bufp;
5668         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5669                 return 0;
5670         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_MD5_LUN_ID)
5671                 return 0;
5672         return 1;
5673 }
5674
5675 int
5676 scsi_devid_is_lun_uuid(uint8_t *bufp)
5677 {
5678         struct scsi_vpd_id_descriptor *descr;
5679
5680         descr = (struct scsi_vpd_id_descriptor *)bufp;
5681         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_LUN)
5682                 return 0;
5683         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_UUID)
5684                 return 0;
5685         return 1;
5686 }
5687
5688 int
5689 scsi_devid_is_port_naa(uint8_t *bufp)
5690 {
5691         struct scsi_vpd_id_descriptor *descr;
5692
5693         descr = (struct scsi_vpd_id_descriptor *)bufp;
5694         if ((descr->id_type & SVPD_ID_ASSOC_MASK) != SVPD_ID_ASSOC_PORT)
5695                 return 0;
5696         if ((descr->id_type & SVPD_ID_TYPE_MASK) != SVPD_ID_TYPE_NAA)
5697                 return 0;
5698         return 1;
5699 }
5700
5701 struct scsi_vpd_id_descriptor *
5702 scsi_get_devid_desc(struct scsi_vpd_id_descriptor *desc, uint32_t len,
5703     scsi_devid_checkfn_t ck_fn)
5704 {
5705         uint8_t *desc_buf_end;
5706
5707         desc_buf_end = (uint8_t *)desc + len;
5708
5709         for (; desc->identifier <= desc_buf_end &&
5710             desc->identifier + desc->length <= desc_buf_end;
5711             desc = (struct scsi_vpd_id_descriptor *)(desc->identifier
5712                                                     + desc->length)) {
5713
5714                 if (ck_fn == NULL || ck_fn((uint8_t *)desc) != 0)
5715                         return (desc);
5716         }
5717         return (NULL);
5718 }
5719
5720 struct scsi_vpd_id_descriptor *
5721 scsi_get_devid(struct scsi_vpd_device_id *id, uint32_t page_len,
5722     scsi_devid_checkfn_t ck_fn)
5723 {
5724         uint32_t len;
5725
5726         if (page_len < sizeof(*id))
5727                 return (NULL);
5728         len = MIN(scsi_2btoul(id->length), page_len - sizeof(*id));
5729         return (scsi_get_devid_desc((struct scsi_vpd_id_descriptor *)
5730             id->desc_list, len, ck_fn));
5731 }
5732
5733 int
5734 scsi_transportid_sbuf(struct sbuf *sb, struct scsi_transportid_header *hdr,
5735                       uint32_t valid_len)
5736 {
5737         switch (hdr->format_protocol & SCSI_TRN_PROTO_MASK) {
5738         case SCSI_PROTO_FC: {
5739                 struct scsi_transportid_fcp *fcp;
5740                 uint64_t n_port_name;
5741
5742                 fcp = (struct scsi_transportid_fcp *)hdr;
5743
5744                 n_port_name = scsi_8btou64(fcp->n_port_name);
5745
5746                 sbuf_printf(sb, "FCP address: 0x%.16jx",(uintmax_t)n_port_name);
5747                 break;
5748         }
5749         case SCSI_PROTO_SPI: {
5750                 struct scsi_transportid_spi *spi;
5751
5752                 spi = (struct scsi_transportid_spi *)hdr;
5753
5754                 sbuf_printf(sb, "SPI address: %u,%u",
5755                             scsi_2btoul(spi->scsi_addr),
5756                             scsi_2btoul(spi->rel_trgt_port_id));
5757                 break;
5758         }
5759         case SCSI_PROTO_SSA:
5760                 /*
5761                  * XXX KDM there is no transport ID defined in SPC-4 for
5762                  * SSA.
5763                  */
5764                 break;
5765         case SCSI_PROTO_1394: {
5766                 struct scsi_transportid_1394 *sbp;
5767                 uint64_t eui64;
5768
5769                 sbp = (struct scsi_transportid_1394 *)hdr;
5770
5771                 eui64 = scsi_8btou64(sbp->eui64);
5772                 sbuf_printf(sb, "SBP address: 0x%.16jx", (uintmax_t)eui64);
5773                 break;
5774         }
5775         case SCSI_PROTO_RDMA: {
5776                 struct scsi_transportid_rdma *rdma;
5777                 unsigned int i;
5778
5779                 rdma = (struct scsi_transportid_rdma *)hdr;
5780
5781                 sbuf_printf(sb, "RDMA address: 0x");
5782                 for (i = 0; i < sizeof(rdma->initiator_port_id); i++)
5783                         sbuf_printf(sb, "%02x", rdma->initiator_port_id[i]);
5784                 break;
5785         }
5786         case SCSI_PROTO_ISCSI: {
5787                 uint32_t add_len, i;
5788                 uint8_t *iscsi_name = NULL;
5789                 int nul_found = 0;
5790
5791                 sbuf_printf(sb, "iSCSI address: ");
5792                 if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) == 
5793                     SCSI_TRN_ISCSI_FORMAT_DEVICE) {
5794                         struct scsi_transportid_iscsi_device *dev;
5795
5796                         dev = (struct scsi_transportid_iscsi_device *)hdr;
5797
5798                         /*
5799                          * Verify how much additional data we really have.
5800                          */
5801                         add_len = scsi_2btoul(dev->additional_length);
5802                         add_len = MIN(add_len, valid_len -
5803                                 __offsetof(struct scsi_transportid_iscsi_device,
5804                                            iscsi_name));
5805                         iscsi_name = &dev->iscsi_name[0];
5806
5807                 } else if ((hdr->format_protocol & SCSI_TRN_FORMAT_MASK) ==
5808                             SCSI_TRN_ISCSI_FORMAT_PORT) {
5809                         struct scsi_transportid_iscsi_port *port;
5810
5811                         port = (struct scsi_transportid_iscsi_port *)hdr;
5812                         
5813                         add_len = scsi_2btoul(port->additional_length);
5814                         add_len = MIN(add_len, valid_len -
5815                                 __offsetof(struct scsi_transportid_iscsi_port,
5816                                            iscsi_name));
5817                         iscsi_name = &port->iscsi_name[0];
5818                 } else {
5819                         sbuf_printf(sb, "unknown format %x",
5820                                     (hdr->format_protocol &
5821                                      SCSI_TRN_FORMAT_MASK) >>
5822                                      SCSI_TRN_FORMAT_SHIFT);
5823                         break;
5824                 }
5825                 if (add_len == 0) {
5826                         sbuf_printf(sb, "not enough data");
5827                         break;
5828                 }
5829                 /*
5830                  * This is supposed to be a NUL-terminated ASCII 
5831                  * string, but you never know.  So we're going to
5832                  * check.  We need to do this because there is no
5833                  * sbuf equivalent of strncat().
5834                  */
5835                 for (i = 0; i < add_len; i++) {
5836                         if (iscsi_name[i] == '\0') {
5837                                 nul_found = 1;
5838                                 break;
5839                         }
5840                 }
5841                 /*
5842                  * If there is a NUL in the name, we can just use
5843                  * sbuf_cat().  Otherwise we need to use sbuf_bcat().
5844                  */
5845                 if (nul_found != 0)
5846                         sbuf_cat(sb, iscsi_name);
5847                 else
5848                         sbuf_bcat(sb, iscsi_name, add_len);
5849                 break;
5850         }
5851         case SCSI_PROTO_SAS: {
5852                 struct scsi_transportid_sas *sas;
5853                 uint64_t sas_addr;
5854
5855                 sas = (struct scsi_transportid_sas *)hdr;
5856
5857                 sas_addr = scsi_8btou64(sas->sas_address);
5858                 sbuf_printf(sb, "SAS address: 0x%.16jx", (uintmax_t)sas_addr);
5859                 break;
5860         }
5861         case SCSI_PROTO_ADITP:
5862         case SCSI_PROTO_ATA:
5863         case SCSI_PROTO_UAS:
5864                 /*
5865                  * No Transport ID format for ADI, ATA or USB is defined in
5866                  * SPC-4.
5867                  */
5868                 sbuf_printf(sb, "No known Transport ID format for protocol "
5869                             "%#x", hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5870                 break;
5871         case SCSI_PROTO_SOP: {
5872                 struct scsi_transportid_sop *sop;
5873                 struct scsi_sop_routing_id_norm *rid;
5874
5875                 sop = (struct scsi_transportid_sop *)hdr;
5876                 rid = (struct scsi_sop_routing_id_norm *)sop->routing_id;
5877
5878                 /*
5879                  * Note that there is no alternate format specified in SPC-4
5880                  * for the PCIe routing ID, so we don't really have a way
5881                  * to know whether the second byte of the routing ID is
5882                  * a device and function or just a function.  So we just
5883                  * assume bus,device,function.
5884                  */
5885                 sbuf_printf(sb, "SOP Routing ID: %u,%u,%u",
5886                             rid->bus, rid->devfunc >> SCSI_TRN_SOP_DEV_SHIFT,
5887                             rid->devfunc & SCSI_TRN_SOP_FUNC_NORM_MAX);
5888                 break;
5889         }
5890         case SCSI_PROTO_NONE:
5891         default:
5892                 sbuf_printf(sb, "Unknown protocol %#x",
5893                             hdr->format_protocol & SCSI_TRN_PROTO_MASK);
5894                 break;
5895         }
5896
5897         return (0);
5898 }
5899
5900 struct scsi_nv scsi_proto_map[] = {
5901         { "fcp", SCSI_PROTO_FC },
5902         { "spi", SCSI_PROTO_SPI },
5903         { "ssa", SCSI_PROTO_SSA },
5904         { "sbp", SCSI_PROTO_1394 },
5905         { "1394", SCSI_PROTO_1394 },
5906         { "srp", SCSI_PROTO_RDMA },
5907         { "rdma", SCSI_PROTO_RDMA },
5908         { "iscsi", SCSI_PROTO_ISCSI },
5909         { "iqn", SCSI_PROTO_ISCSI },
5910         { "sas", SCSI_PROTO_SAS },
5911         { "aditp", SCSI_PROTO_ADITP },
5912         { "ata", SCSI_PROTO_ATA },
5913         { "uas", SCSI_PROTO_UAS },
5914         { "usb", SCSI_PROTO_UAS },
5915         { "sop", SCSI_PROTO_SOP }
5916 };
5917
5918 const char *
5919 scsi_nv_to_str(struct scsi_nv *table, int num_table_entries, uint64_t value)
5920 {
5921         int i;
5922
5923         for (i = 0; i < num_table_entries; i++) {
5924                 if (table[i].value == value)
5925                         return (table[i].name);
5926         }
5927
5928         return (NULL);
5929 }
5930
5931 /*
5932  * Given a name/value table, find a value matching the given name.
5933  * Return values:
5934  *      SCSI_NV_FOUND - match found
5935  *      SCSI_NV_AMBIGUOUS - more than one match, none of them exact
5936  *      SCSI_NV_NOT_FOUND - no match found
5937  */
5938 scsi_nv_status
5939 scsi_get_nv(struct scsi_nv *table, int num_table_entries,
5940             char *name, int *table_entry, scsi_nv_flags flags)
5941 {
5942         int i, num_matches = 0;
5943
5944         for (i = 0; i < num_table_entries; i++) {
5945                 size_t table_len, name_len;
5946
5947                 table_len = strlen(table[i].name);
5948                 name_len = strlen(name);
5949
5950                 if ((((flags & SCSI_NV_FLAG_IG_CASE) != 0)
5951                   && (strncasecmp(table[i].name, name, name_len) == 0))
5952                 || (((flags & SCSI_NV_FLAG_IG_CASE) == 0)
5953                  && (strncmp(table[i].name, name, name_len) == 0))) {
5954                         *table_entry = i;
5955
5956                         /*
5957                          * Check for an exact match.  If we have the same
5958                          * number of characters in the table as the argument,
5959                          * and we already know they're the same, we have
5960                          * an exact match.
5961                          */
5962                         if (table_len == name_len)
5963                                 return (SCSI_NV_FOUND);
5964
5965                         /*
5966                          * Otherwise, bump up the number of matches.  We'll
5967                          * see later how many we have.
5968                          */
5969                         num_matches++;
5970                 }
5971         }
5972
5973         if (num_matches > 1)
5974                 return (SCSI_NV_AMBIGUOUS);
5975         else if (num_matches == 1)
5976                 return (SCSI_NV_FOUND);
5977         else
5978                 return (SCSI_NV_NOT_FOUND);
5979 }
5980
5981 /*
5982  * Parse transport IDs for Fibre Channel, 1394 and SAS.  Since these are
5983  * all 64-bit numbers, the code is similar.
5984  */
5985 int
5986 scsi_parse_transportid_64bit(int proto_id, char *id_str,
5987                              struct scsi_transportid_header **hdr,
5988                              unsigned int *alloc_len,
5989 #ifdef _KERNEL
5990                              struct malloc_type *type, int flags,
5991 #endif
5992                              char *error_str, int error_str_len)
5993 {
5994         uint64_t value;
5995         char *endptr;
5996         int retval;
5997         size_t alloc_size;
5998
5999         retval = 0;
6000
6001         value = strtouq(id_str, &endptr, 0); 
6002         if (*endptr != '\0') {
6003                 if (error_str != NULL) {
6004                         snprintf(error_str, error_str_len, "%s: error "
6005                                  "parsing ID %s, 64-bit number required",
6006                                  __func__, id_str);
6007                 }
6008                 retval = 1;
6009                 goto bailout;
6010         }
6011
6012         switch (proto_id) {
6013         case SCSI_PROTO_FC:
6014                 alloc_size = sizeof(struct scsi_transportid_fcp);
6015                 break;
6016         case SCSI_PROTO_1394:
6017                 alloc_size = sizeof(struct scsi_transportid_1394);
6018                 break;
6019         case SCSI_PROTO_SAS:
6020                 alloc_size = sizeof(struct scsi_transportid_sas);
6021                 break;
6022         default:
6023                 if (error_str != NULL) {
6024                         snprintf(error_str, error_str_len, "%s: unsupoprted "
6025                                  "protocol %d", __func__, proto_id);
6026                 }
6027                 retval = 1;
6028                 goto bailout;
6029                 break; /* NOTREACHED */
6030         }
6031 #ifdef _KERNEL
6032         *hdr = malloc(alloc_size, type, flags);
6033 #else /* _KERNEL */
6034         *hdr = malloc(alloc_size);
6035 #endif /*_KERNEL */
6036         if (*hdr == NULL) {
6037                 if (error_str != NULL) {
6038                         snprintf(error_str, error_str_len, "%s: unable to "
6039                                  "allocate %zu bytes", __func__, alloc_size);
6040                 }
6041                 retval = 1;
6042                 goto bailout;
6043         }
6044
6045         *alloc_len = alloc_size;
6046
6047         bzero(*hdr, alloc_size);
6048
6049         switch (proto_id) {
6050         case SCSI_PROTO_FC: {
6051                 struct scsi_transportid_fcp *fcp;
6052
6053                 fcp = (struct scsi_transportid_fcp *)(*hdr);
6054                 fcp->format_protocol = SCSI_PROTO_FC |
6055                                        SCSI_TRN_FCP_FORMAT_DEFAULT;
6056                 scsi_u64to8b(value, fcp->n_port_name);
6057                 break;
6058         }
6059         case SCSI_PROTO_1394: {
6060                 struct scsi_transportid_1394 *sbp;
6061
6062                 sbp = (struct scsi_transportid_1394 *)(*hdr);
6063                 sbp->format_protocol = SCSI_PROTO_1394 |
6064                                        SCSI_TRN_1394_FORMAT_DEFAULT;
6065                 scsi_u64to8b(value, sbp->eui64);
6066                 break;
6067         }
6068         case SCSI_PROTO_SAS: {
6069                 struct scsi_transportid_sas *sas;
6070
6071                 sas = (struct scsi_transportid_sas *)(*hdr);
6072                 sas->format_protocol = SCSI_PROTO_SAS |
6073                                        SCSI_TRN_SAS_FORMAT_DEFAULT;
6074                 scsi_u64to8b(value, sas->sas_address);
6075                 break;
6076         }
6077         default:
6078                 break;
6079         }
6080 bailout:
6081         return (retval);
6082 }
6083
6084 /*
6085  * Parse a SPI (Parallel SCSI) address of the form: id,rel_tgt_port
6086  */
6087 int
6088 scsi_parse_transportid_spi(char *id_str, struct scsi_transportid_header **hdr,
6089                            unsigned int *alloc_len,
6090 #ifdef _KERNEL
6091                            struct malloc_type *type, int flags,
6092 #endif
6093                            char *error_str, int error_str_len)
6094 {
6095         unsigned long scsi_addr, target_port;
6096         struct scsi_transportid_spi *spi;
6097         char *tmpstr, *endptr;
6098         int retval;
6099
6100         retval = 0;
6101
6102         tmpstr = strsep(&id_str, ",");
6103         if (tmpstr == NULL) {
6104                 if (error_str != NULL) {
6105                         snprintf(error_str, error_str_len,
6106                                  "%s: no ID found", __func__);
6107                 }
6108                 retval = 1;
6109                 goto bailout;
6110         }
6111         scsi_addr = strtoul(tmpstr, &endptr, 0);
6112         if (*endptr != '\0') {
6113                 if (error_str != NULL) {
6114                         snprintf(error_str, error_str_len, "%s: error "
6115                                  "parsing SCSI ID %s, number required",
6116                                  __func__, tmpstr);
6117                 }
6118                 retval = 1;
6119                 goto bailout;
6120         }
6121
6122         if (id_str == NULL) {
6123                 if (error_str != NULL) {
6124                         snprintf(error_str, error_str_len, "%s: no relative "
6125                                  "target port found", __func__);
6126                 }
6127                 retval = 1;
6128                 goto bailout;
6129         }
6130
6131         target_port = strtoul(id_str, &endptr, 0);
6132         if (*endptr != '\0') {
6133                 if (error_str != NULL) {
6134                         snprintf(error_str, error_str_len, "%s: error "
6135                                  "parsing relative target port %s, number "
6136                                  "required", __func__, id_str);
6137                 }
6138                 retval = 1;
6139                 goto bailout;
6140         }
6141 #ifdef _KERNEL
6142         spi = malloc(sizeof(*spi), type, flags);
6143 #else
6144         spi = malloc(sizeof(*spi));
6145 #endif
6146         if (spi == NULL) {
6147                 if (error_str != NULL) {
6148                         snprintf(error_str, error_str_len, "%s: unable to "
6149                                  "allocate %zu bytes", __func__,
6150                                  sizeof(*spi));
6151                 }
6152                 retval = 1;
6153                 goto bailout;
6154         }
6155         *alloc_len = sizeof(*spi);
6156         bzero(spi, sizeof(*spi));
6157
6158         spi->format_protocol = SCSI_PROTO_SPI | SCSI_TRN_SPI_FORMAT_DEFAULT;
6159         scsi_ulto2b(scsi_addr, spi->scsi_addr);
6160         scsi_ulto2b(target_port, spi->rel_trgt_port_id);
6161
6162         *hdr = (struct scsi_transportid_header *)spi;
6163 bailout:
6164         return (retval);
6165 }
6166
6167 /*
6168  * Parse an RDMA/SRP Initiator Port ID string.  This is 32 hexadecimal digits,
6169  * optionally prefixed by "0x" or "0X".
6170  */
6171 int
6172 scsi_parse_transportid_rdma(char *id_str, struct scsi_transportid_header **hdr,
6173                             unsigned int *alloc_len,
6174 #ifdef _KERNEL
6175                             struct malloc_type *type, int flags,
6176 #endif
6177                             char *error_str, int error_str_len)
6178 {
6179         struct scsi_transportid_rdma *rdma;
6180         int retval;
6181         size_t id_len, rdma_id_size;
6182         uint8_t rdma_id[SCSI_TRN_RDMA_PORT_LEN];
6183         char *tmpstr;
6184         unsigned int i, j;
6185
6186         retval = 0;
6187         id_len = strlen(id_str);
6188         rdma_id_size = SCSI_TRN_RDMA_PORT_LEN;
6189
6190         /*
6191          * Check the size.  It needs to be either 32 or 34 characters long.
6192          */
6193         if ((id_len != (rdma_id_size * 2))
6194          && (id_len != ((rdma_id_size * 2) + 2))) {
6195                 if (error_str != NULL) {
6196                         snprintf(error_str, error_str_len, "%s: RDMA ID "
6197                                  "must be 32 hex digits (0x prefix "
6198                                  "optional), only %zu seen", __func__, id_len);
6199                 }
6200                 retval = 1;
6201                 goto bailout;
6202         }
6203
6204         tmpstr = id_str;
6205         /*
6206          * If the user gave us 34 characters, the string needs to start
6207          * with '0x'.
6208          */
6209         if (id_len == ((rdma_id_size * 2) + 2)) {
6210                 if ((tmpstr[0] == '0')
6211                  && ((tmpstr[1] == 'x') || (tmpstr[1] == 'X'))) {
6212                         tmpstr += 2;
6213                 } else {
6214                         if (error_str != NULL) {
6215                                 snprintf(error_str, error_str_len, "%s: RDMA "
6216                                          "ID prefix, if used, must be \"0x\", "
6217                                          "got %s", __func__, tmpstr);
6218                         }
6219                         retval = 1;
6220                         goto bailout;
6221                 }
6222         }
6223         bzero(rdma_id, sizeof(rdma_id));
6224
6225         /*
6226          * Convert ASCII hex into binary bytes.  There is no standard
6227          * 128-bit integer type, and so no strtou128t() routine to convert
6228          * from hex into a large integer.  In the end, we're not going to
6229          * an integer, but rather to a byte array, so that and the fact
6230          * that we require the user to give us 32 hex digits simplifies the
6231          * logic.
6232          */
6233         for (i = 0; i < (rdma_id_size * 2); i++) {
6234                 int cur_shift;
6235                 unsigned char c;
6236
6237                 /* Increment the byte array one for every 2 hex digits */
6238                 j = i >> 1;
6239
6240                 /*
6241                  * The first digit in every pair is the most significant
6242                  * 4 bits.  The second is the least significant 4 bits.
6243                  */
6244                 if ((i % 2) == 0)
6245                         cur_shift = 4;
6246                 else 
6247                         cur_shift = 0;
6248
6249                 c = tmpstr[i];
6250                 /* Convert the ASCII hex character into a number */
6251                 if (isdigit(c))
6252                         c -= '0';
6253                 else if (isalpha(c))
6254                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
6255                 else {
6256                         if (error_str != NULL) {
6257                                 snprintf(error_str, error_str_len, "%s: "
6258                                          "RDMA ID must be hex digits, got "
6259                                          "invalid character %c", __func__,
6260                                          tmpstr[i]);
6261                         }
6262                         retval = 1;
6263                         goto bailout;
6264                 }
6265                 /*
6266                  * The converted number can't be less than 0; the type is
6267                  * unsigned, and the subtraction logic will not give us 
6268                  * a negative number.  So we only need to make sure that
6269                  * the value is not greater than 0xf.  (i.e. make sure the
6270                  * user didn't give us a value like "0x12jklmno").
6271                  */
6272                 if (c > 0xf) {
6273                         if (error_str != NULL) {
6274                                 snprintf(error_str, error_str_len, "%s: "
6275                                          "RDMA ID must be hex digits, got "
6276                                          "invalid character %c", __func__,
6277                                          tmpstr[i]);
6278                         }
6279                         retval = 1;
6280                         goto bailout;
6281                 }
6282                 
6283                 rdma_id[j] |= c << cur_shift;
6284         }
6285
6286 #ifdef _KERNEL
6287         rdma = malloc(sizeof(*rdma), type, flags);
6288 #else
6289         rdma = malloc(sizeof(*rdma));
6290 #endif
6291         if (rdma == NULL) {
6292                 if (error_str != NULL) {
6293                         snprintf(error_str, error_str_len, "%s: unable to "
6294                                  "allocate %zu bytes", __func__,
6295                                  sizeof(*rdma));
6296                 }
6297                 retval = 1;
6298                 goto bailout;
6299         }
6300         *alloc_len = sizeof(*rdma);
6301         bzero(rdma, *alloc_len);
6302
6303         rdma->format_protocol = SCSI_PROTO_RDMA | SCSI_TRN_RDMA_FORMAT_DEFAULT;
6304         bcopy(rdma_id, rdma->initiator_port_id, SCSI_TRN_RDMA_PORT_LEN);
6305
6306         *hdr = (struct scsi_transportid_header *)rdma;
6307
6308 bailout:
6309         return (retval);
6310 }
6311
6312 /*
6313  * Parse an iSCSI name.  The format is either just the name:
6314  *
6315  *      iqn.2012-06.com.example:target0
6316  * or the name, separator and initiator session ID:
6317  *
6318  *      iqn.2012-06.com.example:target0,i,0x123
6319  *
6320  * The separator format is exact.
6321  */
6322 int
6323 scsi_parse_transportid_iscsi(char *id_str, struct scsi_transportid_header **hdr,
6324                              unsigned int *alloc_len,
6325 #ifdef _KERNEL
6326                              struct malloc_type *type, int flags,
6327 #endif
6328                              char *error_str, int error_str_len)
6329 {
6330         size_t id_len, sep_len, id_size, name_len;
6331         int retval;
6332         unsigned int i, sep_pos, sep_found;
6333         const char *sep_template = ",i,0x";
6334         const char *iqn_prefix = "iqn.";
6335         struct scsi_transportid_iscsi_device *iscsi;
6336
6337         retval = 0;
6338         sep_found = 0;
6339
6340         id_len = strlen(id_str);
6341         sep_len = strlen(sep_template);
6342
6343         /*
6344          * The separator is defined as exactly ',i,0x'.  Any other commas,
6345          * or any other form, is an error.  So look for a comma, and once
6346          * we find that, the next few characters must match the separator
6347          * exactly.  Once we get through the separator, there should be at
6348          * least one character.
6349          */
6350         for (i = 0, sep_pos = 0; i < id_len; i++) {
6351                 if (sep_pos == 0) {
6352                         if (id_str[i] == sep_template[sep_pos])
6353                                 sep_pos++;
6354
6355                         continue;
6356                 }
6357                 if (sep_pos < sep_len) {
6358                         if (id_str[i] == sep_template[sep_pos]) {
6359                                 sep_pos++;
6360                                 continue;
6361                         } 
6362                         if (error_str != NULL) {
6363                                 snprintf(error_str, error_str_len, "%s: "
6364                                          "invalid separator in iSCSI name "
6365                                          "\"%s\"",
6366                                          __func__, id_str);
6367                         }
6368                         retval = 1;
6369                         goto bailout;
6370                 } else {
6371                         sep_found = 1;
6372                         break;
6373                 }
6374         }
6375
6376         /*
6377          * Check to see whether we have a separator but no digits after it.
6378          */
6379         if ((sep_pos != 0)
6380          && (sep_found == 0)) {
6381                 if (error_str != NULL) {
6382                         snprintf(error_str, error_str_len, "%s: no digits "
6383                                  "found after separator in iSCSI name \"%s\"",
6384                                  __func__, id_str);
6385                 }
6386                 retval = 1;
6387                 goto bailout;
6388         }
6389
6390         /*
6391          * The incoming ID string has the "iqn." prefix stripped off.  We
6392          * need enough space for the base structure (the structures are the
6393          * same for the two iSCSI forms), the prefix, the ID string and a
6394          * terminating NUL.
6395          */
6396         id_size = sizeof(*iscsi) + strlen(iqn_prefix) + id_len + 1;
6397
6398 #ifdef _KERNEL
6399         iscsi = malloc(id_size, type, flags);
6400 #else
6401         iscsi = malloc(id_size);
6402 #endif
6403         if (iscsi == NULL) {
6404                 if (error_str != NULL) {
6405                         snprintf(error_str, error_str_len, "%s: unable to "
6406                                  "allocate %zu bytes", __func__, id_size);
6407                 }
6408                 retval = 1;
6409                 goto bailout;
6410         }
6411         *alloc_len = id_size;
6412         bzero(iscsi, id_size);
6413
6414         iscsi->format_protocol = SCSI_PROTO_ISCSI;
6415         if (sep_found == 0)
6416                 iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_DEVICE;
6417         else
6418                 iscsi->format_protocol |= SCSI_TRN_ISCSI_FORMAT_PORT;
6419         name_len = id_size - sizeof(*iscsi);
6420         scsi_ulto2b(name_len, iscsi->additional_length);
6421         snprintf(iscsi->iscsi_name, name_len, "%s%s", iqn_prefix, id_str);
6422
6423         *hdr = (struct scsi_transportid_header *)iscsi;
6424
6425 bailout:
6426         return (retval);
6427 }
6428
6429 /*
6430  * Parse a SCSI over PCIe (SOP) identifier.  The Routing ID can either be
6431  * of the form 'bus,device,function' or 'bus,function'.
6432  */
6433 int
6434 scsi_parse_transportid_sop(char *id_str, struct scsi_transportid_header **hdr,
6435                            unsigned int *alloc_len,
6436 #ifdef _KERNEL
6437                            struct malloc_type *type, int flags,
6438 #endif
6439                            char *error_str, int error_str_len)
6440 {
6441         struct scsi_transportid_sop *sop;
6442         unsigned long bus, device, function;
6443         char *tmpstr, *endptr;
6444         int retval, device_spec;
6445
6446         retval = 0;
6447         device_spec = 0;
6448         device = 0;
6449
6450         tmpstr = strsep(&id_str, ",");
6451         if ((tmpstr == NULL)
6452          || (*tmpstr == '\0')) {
6453                 if (error_str != NULL) {
6454                         snprintf(error_str, error_str_len, "%s: no ID found",
6455                                  __func__);
6456                 }
6457                 retval = 1;
6458                 goto bailout;
6459         }
6460         bus = strtoul(tmpstr, &endptr, 0);
6461         if (*endptr != '\0') {
6462                 if (error_str != NULL) {
6463                         snprintf(error_str, error_str_len, "%s: error "
6464                                  "parsing PCIe bus %s, number required",
6465                                  __func__, tmpstr);
6466                 }
6467                 retval = 1;
6468                 goto bailout;
6469         }
6470         if ((id_str == NULL) 
6471          || (*id_str == '\0')) {
6472                 if (error_str != NULL) {
6473                         snprintf(error_str, error_str_len, "%s: no PCIe "
6474                                  "device or function found", __func__);
6475                 }
6476                 retval = 1;
6477                 goto bailout;
6478         }
6479         tmpstr = strsep(&id_str, ",");
6480         function = strtoul(tmpstr, &endptr, 0);
6481         if (*endptr != '\0') {
6482                 if (error_str != NULL) {
6483                         snprintf(error_str, error_str_len, "%s: error "
6484                                  "parsing PCIe device/function %s, number "
6485                                  "required", __func__, tmpstr);
6486                 }
6487                 retval = 1;
6488                 goto bailout;
6489         }
6490         /*
6491          * Check to see whether the user specified a third value.  If so,
6492          * the second is the device.
6493          */
6494         if (id_str != NULL) {
6495                 if (*id_str == '\0') {
6496                         if (error_str != NULL) {
6497                                 snprintf(error_str, error_str_len, "%s: "
6498                                          "no PCIe function found", __func__);
6499                         }
6500                         retval = 1;
6501                         goto bailout;
6502                 }
6503                 device = function;
6504                 device_spec = 1;
6505                 function = strtoul(id_str, &endptr, 0);
6506                 if (*endptr != '\0') {
6507                         if (error_str != NULL) {
6508                                 snprintf(error_str, error_str_len, "%s: "
6509                                          "error parsing PCIe function %s, "
6510                                          "number required", __func__, id_str);
6511                         }
6512                         retval = 1;
6513                         goto bailout;
6514                 }
6515         }
6516         if (bus > SCSI_TRN_SOP_BUS_MAX) {
6517                 if (error_str != NULL) {
6518                         snprintf(error_str, error_str_len, "%s: bus value "
6519                                  "%lu greater than maximum %u", __func__,
6520                                  bus, SCSI_TRN_SOP_BUS_MAX);
6521                 }
6522                 retval = 1;
6523                 goto bailout;
6524         }
6525
6526         if ((device_spec != 0)
6527          && (device > SCSI_TRN_SOP_DEV_MASK)) {
6528                 if (error_str != NULL) {
6529                         snprintf(error_str, error_str_len, "%s: device value "
6530                                  "%lu greater than maximum %u", __func__,
6531                                  device, SCSI_TRN_SOP_DEV_MAX);
6532                 }
6533                 retval = 1;
6534                 goto bailout;
6535         }
6536
6537         if (((device_spec != 0)
6538           && (function > SCSI_TRN_SOP_FUNC_NORM_MAX))
6539          || ((device_spec == 0)
6540           && (function > SCSI_TRN_SOP_FUNC_ALT_MAX))) {
6541                 if (error_str != NULL) {
6542                         snprintf(error_str, error_str_len, "%s: function value "
6543                                  "%lu greater than maximum %u", __func__,
6544                                  function, (device_spec == 0) ?
6545                                  SCSI_TRN_SOP_FUNC_ALT_MAX : 
6546                                  SCSI_TRN_SOP_FUNC_NORM_MAX);
6547                 }
6548                 retval = 1;
6549                 goto bailout;
6550         }
6551
6552 #ifdef _KERNEL
6553         sop = malloc(sizeof(*sop), type, flags);
6554 #else
6555         sop = malloc(sizeof(*sop));
6556 #endif
6557         if (sop == NULL) {
6558                 if (error_str != NULL) {
6559                         snprintf(error_str, error_str_len, "%s: unable to "
6560                                  "allocate %zu bytes", __func__, sizeof(*sop));
6561                 }
6562                 retval = 1;
6563                 goto bailout;
6564         }
6565         *alloc_len = sizeof(*sop);
6566         bzero(sop, sizeof(*sop));
6567         sop->format_protocol = SCSI_PROTO_SOP | SCSI_TRN_SOP_FORMAT_DEFAULT;
6568         if (device_spec != 0) {
6569                 struct scsi_sop_routing_id_norm rid;
6570
6571                 rid.bus = bus;
6572                 rid.devfunc = (device << SCSI_TRN_SOP_DEV_SHIFT) | function;
6573                 bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6574                       sizeof(sop->routing_id)));
6575         } else {
6576                 struct scsi_sop_routing_id_alt rid;
6577
6578                 rid.bus = bus;
6579                 rid.function = function;
6580                 bcopy(&rid, sop->routing_id, MIN(sizeof(rid),
6581                       sizeof(sop->routing_id)));
6582         }
6583
6584         *hdr = (struct scsi_transportid_header *)sop;
6585 bailout:
6586         return (retval);
6587 }
6588
6589 /*
6590  * transportid_str: NUL-terminated string with format: protcol,id
6591  *                  The ID is protocol specific.
6592  * hdr:             Storage will be allocated for the transport ID.
6593  * alloc_len:       The amount of memory allocated is returned here.
6594  * type:            Malloc bucket (kernel only).
6595  * flags:           Malloc flags (kernel only).
6596  * error_str:       If non-NULL, it will contain error information (without
6597  *                  a terminating newline) if an error is returned.
6598  * error_str_len:   Allocated length of the error string.
6599  *
6600  * Returns 0 for success, non-zero for failure.
6601  */
6602 int
6603 scsi_parse_transportid(char *transportid_str,
6604                        struct scsi_transportid_header **hdr,
6605                        unsigned int *alloc_len,
6606 #ifdef _KERNEL
6607                        struct malloc_type *type, int flags,
6608 #endif
6609                        char *error_str, int error_str_len)
6610 {
6611         char *tmpstr;
6612         scsi_nv_status status;
6613         int retval, num_proto_entries, table_entry;
6614
6615         retval = 0;
6616         table_entry = 0;
6617
6618         /*
6619          * We do allow a period as well as a comma to separate the protocol
6620          * from the ID string.  This is to accommodate iSCSI names, which
6621          * start with "iqn.".
6622          */
6623         tmpstr = strsep(&transportid_str, ",.");
6624         if (tmpstr == NULL) {
6625                 if (error_str != NULL) {
6626                         snprintf(error_str, error_str_len,
6627                                  "%s: transportid_str is NULL", __func__);
6628                 }
6629                 retval = 1;
6630                 goto bailout;
6631         }
6632
6633         num_proto_entries = sizeof(scsi_proto_map) /
6634                             sizeof(scsi_proto_map[0]);
6635         status = scsi_get_nv(scsi_proto_map, num_proto_entries, tmpstr,
6636                              &table_entry, SCSI_NV_FLAG_IG_CASE);
6637         if (status != SCSI_NV_FOUND) {
6638                 if (error_str != NULL) {
6639                         snprintf(error_str, error_str_len, "%s: %s protocol "
6640                                  "name %s", __func__,
6641                                  (status == SCSI_NV_AMBIGUOUS) ? "ambiguous" :
6642                                  "invalid", tmpstr);
6643                 }
6644                 retval = 1;
6645                 goto bailout;
6646         }
6647         switch (scsi_proto_map[table_entry].value) {
6648         case SCSI_PROTO_FC:
6649         case SCSI_PROTO_1394:
6650         case SCSI_PROTO_SAS:
6651                 retval = scsi_parse_transportid_64bit(
6652                     scsi_proto_map[table_entry].value, transportid_str, hdr,
6653                     alloc_len,
6654 #ifdef _KERNEL
6655                     type, flags,
6656 #endif
6657                     error_str, error_str_len);
6658                 break;
6659         case SCSI_PROTO_SPI:
6660                 retval = scsi_parse_transportid_spi(transportid_str, hdr,
6661                     alloc_len,
6662 #ifdef _KERNEL
6663                     type, flags,
6664 #endif
6665                     error_str, error_str_len);
6666                 break;
6667         case SCSI_PROTO_RDMA:
6668                 retval = scsi_parse_transportid_rdma(transportid_str, hdr,
6669                     alloc_len,
6670 #ifdef _KERNEL
6671                     type, flags,
6672 #endif
6673                     error_str, error_str_len);
6674                 break;
6675         case SCSI_PROTO_ISCSI:
6676                 retval = scsi_parse_transportid_iscsi(transportid_str, hdr,
6677                     alloc_len,
6678 #ifdef _KERNEL
6679                     type, flags,
6680 #endif
6681                     error_str, error_str_len);
6682                 break;
6683         case SCSI_PROTO_SOP:
6684                 retval = scsi_parse_transportid_sop(transportid_str, hdr,
6685                     alloc_len,
6686 #ifdef _KERNEL
6687                     type, flags,
6688 #endif
6689                     error_str, error_str_len);
6690                 break;
6691         case SCSI_PROTO_SSA:
6692         case SCSI_PROTO_ADITP:
6693         case SCSI_PROTO_ATA:
6694         case SCSI_PROTO_UAS:
6695         case SCSI_PROTO_NONE:
6696         default:
6697                 /*
6698                  * There is no format defined for a Transport ID for these
6699                  * protocols.  So even if the user gives us something, we
6700                  * have no way to turn it into a standard SCSI Transport ID.
6701                  */
6702                 retval = 1;
6703                 if (error_str != NULL) {
6704                         snprintf(error_str, error_str_len, "%s: no Transport "
6705                                  "ID format exists for protocol %s",
6706                                  __func__, tmpstr);
6707                 }
6708                 goto bailout;
6709                 break;  /* NOTREACHED */
6710         }
6711 bailout:
6712         return (retval);
6713 }
6714
6715 struct scsi_attrib_table_entry scsi_mam_attr_table[] = {
6716         { SMA_ATTR_REM_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6717           "Remaining Capacity in Partition",
6718           /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,/*parse_str*/ NULL },
6719         { SMA_ATTR_MAX_CAP_PARTITION, SCSI_ATTR_FLAG_NONE,
6720           "Maximum Capacity in Partition",
6721           /*suffix*/"MB", /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6722         { SMA_ATTR_TAPEALERT_FLAGS, SCSI_ATTR_FLAG_HEX,
6723           "TapeAlert Flags",
6724           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6725         { SMA_ATTR_LOAD_COUNT, SCSI_ATTR_FLAG_NONE,
6726           "Load Count",
6727           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6728         { SMA_ATTR_MAM_SPACE_REMAINING, SCSI_ATTR_FLAG_NONE,
6729           "MAM Space Remaining",
6730           /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6731           /*parse_str*/ NULL },
6732         { SMA_ATTR_DEV_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6733           "Assigning Organization",
6734           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6735           /*parse_str*/ NULL },
6736         { SMA_ATTR_FORMAT_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6737           "Format Density Code",
6738           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6739         { SMA_ATTR_INITIALIZATION_COUNT, SCSI_ATTR_FLAG_NONE,
6740           "Initialization Count",
6741           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL },
6742         { SMA_ATTR_VOLUME_ID, SCSI_ATTR_FLAG_NONE,
6743           "Volume Identifier",
6744           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6745           /*parse_str*/ NULL },
6746         { SMA_ATTR_VOLUME_CHANGE_REF, SCSI_ATTR_FLAG_HEX,
6747           "Volume Change Reference",
6748           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6749           /*parse_str*/ NULL },
6750         { SMA_ATTR_DEV_SERIAL_LAST_LOAD, SCSI_ATTR_FLAG_NONE,
6751           "Device Vendor/Serial at Last Load",
6752           /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6753           /*parse_str*/ NULL },
6754         { SMA_ATTR_DEV_SERIAL_LAST_LOAD_1, SCSI_ATTR_FLAG_NONE,
6755           "Device Vendor/Serial at Last Load - 1",
6756           /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6757           /*parse_str*/ NULL },
6758         { SMA_ATTR_DEV_SERIAL_LAST_LOAD_2, SCSI_ATTR_FLAG_NONE,
6759           "Device Vendor/Serial at Last Load - 2",
6760           /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6761           /*parse_str*/ NULL },
6762         { SMA_ATTR_DEV_SERIAL_LAST_LOAD_3, SCSI_ATTR_FLAG_NONE,
6763           "Device Vendor/Serial at Last Load - 3",
6764           /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf,
6765           /*parse_str*/ NULL },
6766         { SMA_ATTR_TOTAL_MB_WRITTEN_LT, SCSI_ATTR_FLAG_NONE,
6767           "Total MB Written in Medium Life",
6768           /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6769           /*parse_str*/ NULL },
6770         { SMA_ATTR_TOTAL_MB_READ_LT, SCSI_ATTR_FLAG_NONE,
6771           "Total MB Read in Medium Life",
6772           /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6773           /*parse_str*/ NULL },
6774         { SMA_ATTR_TOTAL_MB_WRITTEN_CUR, SCSI_ATTR_FLAG_NONE,
6775           "Total MB Written in Current/Last Load",
6776           /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6777           /*parse_str*/ NULL },
6778         { SMA_ATTR_TOTAL_MB_READ_CUR, SCSI_ATTR_FLAG_NONE,
6779           "Total MB Read in Current/Last Load",
6780           /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,
6781           /*parse_str*/ NULL },
6782         { SMA_ATTR_FIRST_ENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6783           "Logical Position of First Encrypted Block",
6784           /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6785           /*parse_str*/ NULL },
6786         { SMA_ATTR_NEXT_UNENC_BLOCK, SCSI_ATTR_FLAG_NONE,
6787           "Logical Position of First Unencrypted Block after First "
6788           "Encrypted Block",
6789           /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf,
6790           /*parse_str*/ NULL },
6791         { SMA_ATTR_MEDIUM_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6792           "Medium Usage History",
6793           /*suffix*/ NULL, /*to_str*/ NULL,
6794           /*parse_str*/ NULL },
6795         { SMA_ATTR_PART_USAGE_HIST, SCSI_ATTR_FLAG_NONE,
6796           "Partition Usage History",
6797           /*suffix*/ NULL, /*to_str*/ NULL,
6798           /*parse_str*/ NULL },
6799         { SMA_ATTR_MED_MANUF, SCSI_ATTR_FLAG_NONE,
6800           "Medium Manufacturer",
6801           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6802           /*parse_str*/ NULL },
6803         { SMA_ATTR_MED_SERIAL, SCSI_ATTR_FLAG_NONE,
6804           "Medium Serial Number",
6805           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6806           /*parse_str*/ NULL },
6807         { SMA_ATTR_MED_LENGTH, SCSI_ATTR_FLAG_NONE,
6808           "Medium Length",
6809           /*suffix*/"m", /*to_str*/ scsi_attrib_int_sbuf,
6810           /*parse_str*/ NULL },
6811         { SMA_ATTR_MED_WIDTH, SCSI_ATTR_FLAG_FP | SCSI_ATTR_FLAG_DIV_10 |
6812           SCSI_ATTR_FLAG_FP_1DIGIT,
6813           "Medium Width",
6814           /*suffix*/"mm", /*to_str*/ scsi_attrib_int_sbuf,
6815           /*parse_str*/ NULL },
6816         { SMA_ATTR_MED_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE,
6817           "Assigning Organization",
6818           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6819           /*parse_str*/ NULL },
6820         { SMA_ATTR_MED_DENSITY_CODE, SCSI_ATTR_FLAG_HEX,
6821           "Medium Density Code",
6822           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6823           /*parse_str*/ NULL },
6824         { SMA_ATTR_MED_MANUF_DATE, SCSI_ATTR_FLAG_NONE,
6825           "Medium Manufacture Date",
6826           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6827           /*parse_str*/ NULL },
6828         { SMA_ATTR_MAM_CAPACITY, SCSI_ATTR_FLAG_NONE,
6829           "MAM Capacity",
6830           /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf,
6831           /*parse_str*/ NULL },
6832         { SMA_ATTR_MED_TYPE, SCSI_ATTR_FLAG_HEX,
6833           "Medium Type",
6834           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6835           /*parse_str*/ NULL },
6836         { SMA_ATTR_MED_TYPE_INFO, SCSI_ATTR_FLAG_HEX,
6837           "Medium Type Information",
6838           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6839           /*parse_str*/ NULL },
6840         { SMA_ATTR_MED_SERIAL_NUM, SCSI_ATTR_FLAG_NONE,
6841           "Medium Serial Number",
6842           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6843           /*parse_str*/ NULL },
6844         { SMA_ATTR_APP_VENDOR, SCSI_ATTR_FLAG_NONE,
6845           "Application Vendor",
6846           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6847           /*parse_str*/ NULL },
6848         { SMA_ATTR_APP_NAME, SCSI_ATTR_FLAG_NONE,
6849           "Application Name",
6850           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6851           /*parse_str*/ NULL },
6852         { SMA_ATTR_APP_VERSION, SCSI_ATTR_FLAG_NONE,
6853           "Application Version",
6854           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6855           /*parse_str*/ NULL },
6856         { SMA_ATTR_USER_MED_TEXT_LABEL, SCSI_ATTR_FLAG_NONE,
6857           "User Medium Text Label",
6858           /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6859           /*parse_str*/ NULL },
6860         { SMA_ATTR_LAST_WRITTEN_TIME, SCSI_ATTR_FLAG_NONE,
6861           "Date and Time Last Written",
6862           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6863           /*parse_str*/ NULL },
6864         { SMA_ATTR_TEXT_LOCAL_ID, SCSI_ATTR_FLAG_HEX,
6865           "Text Localization Identifier",
6866           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6867           /*parse_str*/ NULL },
6868         { SMA_ATTR_BARCODE, SCSI_ATTR_FLAG_NONE,
6869           "Barcode",
6870           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6871           /*parse_str*/ NULL },
6872         { SMA_ATTR_HOST_OWNER_NAME, SCSI_ATTR_FLAG_NONE,
6873           "Owning Host Textual Name",
6874           /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6875           /*parse_str*/ NULL },
6876         { SMA_ATTR_MEDIA_POOL, SCSI_ATTR_FLAG_NONE,
6877           "Media Pool",
6878           /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf,
6879           /*parse_str*/ NULL },
6880         { SMA_ATTR_PART_USER_LABEL, SCSI_ATTR_FLAG_NONE,
6881           "Partition User Text Label",
6882           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6883           /*parse_str*/ NULL },
6884         { SMA_ATTR_LOAD_UNLOAD_AT_PART, SCSI_ATTR_FLAG_NONE,
6885           "Load/Unload at Partition",
6886           /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf,
6887           /*parse_str*/ NULL },
6888         { SMA_ATTR_APP_FORMAT_VERSION, SCSI_ATTR_FLAG_NONE,
6889           "Application Format Version",
6890           /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf,
6891           /*parse_str*/ NULL },
6892         { SMA_ATTR_VOL_COHERENCY_INFO, SCSI_ATTR_FLAG_NONE,
6893           "Volume Coherency Information",
6894           /*suffix*/NULL, /*to_str*/ scsi_attrib_volcoh_sbuf,
6895           /*parse_str*/ NULL },
6896         { 0x0ff1, SCSI_ATTR_FLAG_NONE,
6897           "Spectra MLM Creation",
6898           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6899           /*parse_str*/ NULL },
6900         { 0x0ff2, SCSI_ATTR_FLAG_NONE,
6901           "Spectra MLM C3",
6902           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6903           /*parse_str*/ NULL },
6904         { 0x0ff3, SCSI_ATTR_FLAG_NONE,
6905           "Spectra MLM RW",
6906           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6907           /*parse_str*/ NULL },
6908         { 0x0ff4, SCSI_ATTR_FLAG_NONE,
6909           "Spectra MLM SDC List",
6910           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6911           /*parse_str*/ NULL },
6912         { 0x0ff7, SCSI_ATTR_FLAG_NONE,
6913           "Spectra MLM Post Scan",
6914           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6915           /*parse_str*/ NULL },
6916         { 0x0ffe, SCSI_ATTR_FLAG_NONE,
6917           "Spectra MLM Checksum",
6918           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6919           /*parse_str*/ NULL },
6920         { 0x17f1, SCSI_ATTR_FLAG_NONE,
6921           "Spectra MLM Creation",
6922           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6923           /*parse_str*/ NULL },
6924         { 0x17f2, SCSI_ATTR_FLAG_NONE,
6925           "Spectra MLM C3",
6926           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6927           /*parse_str*/ NULL },
6928         { 0x17f3, SCSI_ATTR_FLAG_NONE,
6929           "Spectra MLM RW",
6930           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6931           /*parse_str*/ NULL },
6932         { 0x17f4, SCSI_ATTR_FLAG_NONE,
6933           "Spectra MLM SDC List",
6934           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6935           /*parse_str*/ NULL },
6936         { 0x17f7, SCSI_ATTR_FLAG_NONE,
6937           "Spectra MLM Post Scan",
6938           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6939           /*parse_str*/ NULL },
6940         { 0x17ff, SCSI_ATTR_FLAG_NONE,
6941           "Spectra MLM Checksum",
6942           /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf,
6943           /*parse_str*/ NULL },
6944 };
6945
6946 /*
6947  * Print out Volume Coherency Information (Attribute 0x080c).
6948  * This field has two variable length members, including one at the
6949  * beginning, so it isn't practical to have a fixed structure definition.
6950  * This is current as of SSC4r03 (see section 4.2.21.3), dated March 25,
6951  * 2013.
6952  */
6953 int
6954 scsi_attrib_volcoh_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
6955                          uint32_t valid_len, uint32_t flags,
6956                          uint32_t output_flags, char *error_str,
6957                          int error_str_len)
6958 {
6959         size_t avail_len;
6960         uint32_t field_size;
6961         uint64_t tmp_val;
6962         uint8_t *cur_ptr;
6963         int retval;
6964         int vcr_len, as_len;
6965
6966         retval = 0;
6967         tmp_val = 0;
6968
6969         field_size = scsi_2btoul(hdr->length);
6970         avail_len = valid_len - sizeof(*hdr);
6971         if (field_size > avail_len) {
6972                 if (error_str != NULL) {
6973                         snprintf(error_str, error_str_len, "Available "
6974                                  "length of attribute ID 0x%.4x %zu < field "
6975                                  "length %u", scsi_2btoul(hdr->id), avail_len,
6976                                  field_size);
6977                 }
6978                 retval = 1;
6979                 goto bailout;
6980         } else if (field_size == 0) {
6981                 /*
6982                  * It isn't clear from the spec whether a field length of
6983                  * 0 is invalid here.  It probably is, but be lenient here
6984                  * to avoid inconveniencing the user.
6985                  */
6986                 goto bailout;
6987         }
6988         cur_ptr = hdr->attribute;
6989         vcr_len = *cur_ptr;
6990         cur_ptr++;
6991
6992         sbuf_printf(sb, "\n\tVolume Change Reference Value:");
6993
6994         switch (vcr_len) {
6995         case 0:
6996                 if (error_str != NULL) {
6997                         snprintf(error_str, error_str_len, "Volume Change "
6998                                  "Reference value has length of 0");
6999                 }
7000                 retval = 1;
7001                 goto bailout;
7002                 break; /*NOTREACHED*/
7003         case 1:
7004                 tmp_val = *cur_ptr;
7005                 break;
7006         case 2:
7007                 tmp_val = scsi_2btoul(cur_ptr);
7008                 break;
7009         case 3:
7010                 tmp_val = scsi_3btoul(cur_ptr);
7011                 break;
7012         case 4:
7013                 tmp_val = scsi_4btoul(cur_ptr);
7014                 break;
7015         case 8:
7016                 tmp_val = scsi_8btou64(cur_ptr);
7017                 break;
7018         default:
7019                 sbuf_printf(sb, "\n");
7020                 sbuf_hexdump(sb, cur_ptr, vcr_len, NULL, 0);
7021                 break;
7022         }
7023         if (vcr_len <= 8)
7024                 sbuf_printf(sb, " 0x%jx\n", (uintmax_t)tmp_val);
7025
7026         cur_ptr += vcr_len;
7027         tmp_val = scsi_8btou64(cur_ptr);
7028         sbuf_printf(sb, "\tVolume Coherency Count: %ju\n", (uintmax_t)tmp_val);
7029
7030         cur_ptr += sizeof(tmp_val);
7031         tmp_val = scsi_8btou64(cur_ptr);
7032         sbuf_printf(sb, "\tVolume Coherency Set Identifier: 0x%jx\n",
7033                     (uintmax_t)tmp_val);
7034
7035         /*
7036          * Figure out how long the Application Client Specific Information
7037          * is and produce a hexdump.
7038          */
7039         cur_ptr += sizeof(tmp_val);
7040         as_len = scsi_2btoul(cur_ptr);
7041         cur_ptr += sizeof(uint16_t);
7042         sbuf_printf(sb, "\tApplication Client Specific Information: ");
7043         if (((as_len == SCSI_LTFS_VER0_LEN)
7044           || (as_len == SCSI_LTFS_VER1_LEN))
7045          && (strncmp(cur_ptr, SCSI_LTFS_STR_NAME, SCSI_LTFS_STR_LEN) == 0)) {
7046                 sbuf_printf(sb, "LTFS\n");
7047                 cur_ptr += SCSI_LTFS_STR_LEN + 1;
7048                 if (cur_ptr[SCSI_LTFS_UUID_LEN] != '\0')
7049                         cur_ptr[SCSI_LTFS_UUID_LEN] = '\0';
7050                 sbuf_printf(sb, "\tLTFS UUID: %s\n", cur_ptr);
7051                 cur_ptr += SCSI_LTFS_UUID_LEN + 1;
7052                 /* XXX KDM check the length */
7053                 sbuf_printf(sb, "\tLTFS Version: %d\n", *cur_ptr);
7054         } else {
7055                 sbuf_printf(sb, "Unknown\n");
7056                 sbuf_hexdump(sb, cur_ptr, as_len, NULL, 0);
7057         }
7058
7059 bailout:
7060         return (retval);
7061 }
7062
7063 int
7064 scsi_attrib_vendser_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7065                          uint32_t valid_len, uint32_t flags, 
7066                          uint32_t output_flags, char *error_str,
7067                          int error_str_len)
7068 {
7069         size_t avail_len;
7070         uint32_t field_size;
7071         struct scsi_attrib_vendser *vendser;
7072         cam_strvis_flags strvis_flags;
7073         int retval = 0;
7074
7075         field_size = scsi_2btoul(hdr->length);
7076         avail_len = valid_len - sizeof(*hdr);
7077         if (field_size > avail_len) {
7078                 if (error_str != NULL) {
7079                         snprintf(error_str, error_str_len, "Available "
7080                                  "length of attribute ID 0x%.4x %zu < field "
7081                                  "length %u", scsi_2btoul(hdr->id), avail_len,
7082                                  field_size);
7083                 }
7084                 retval = 1;
7085                 goto bailout;
7086         } else if (field_size == 0) {
7087                 /*
7088                  * A field size of 0 doesn't make sense here.  The device
7089                  * can at least give you the vendor ID, even if it can't
7090                  * give you the serial number.
7091                  */
7092                 if (error_str != NULL) {
7093                         snprintf(error_str, error_str_len, "The length of "
7094                                  "attribute ID 0x%.4x is 0",
7095                                  scsi_2btoul(hdr->id));
7096                 }
7097                 retval = 1;
7098                 goto bailout;
7099         }
7100         vendser = (struct scsi_attrib_vendser *)hdr->attribute;
7101
7102         switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7103         case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7104                 strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7105                 break;
7106         case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7107                 strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7108                 break;
7109         case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7110         default:
7111                 strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7112                 break;;
7113         }
7114         cam_strvis_sbuf(sb, vendser->vendor, sizeof(vendser->vendor),
7115             strvis_flags);
7116         sbuf_putc(sb, ' ');
7117         cam_strvis_sbuf(sb, vendser->serial_num, sizeof(vendser->serial_num),
7118             strvis_flags);
7119 bailout:
7120         return (retval);
7121 }
7122
7123 int
7124 scsi_attrib_hexdump_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7125                          uint32_t valid_len, uint32_t flags,
7126                          uint32_t output_flags, char *error_str,
7127                          int error_str_len)
7128 {
7129         uint32_t field_size;
7130         ssize_t avail_len;
7131         uint32_t print_len;
7132         uint8_t *num_ptr;
7133         int retval = 0;
7134
7135         field_size = scsi_2btoul(hdr->length);
7136         avail_len = valid_len - sizeof(*hdr);
7137         print_len = MIN(avail_len, field_size);
7138         num_ptr = hdr->attribute;
7139
7140         if (print_len > 0) {
7141                 sbuf_printf(sb, "\n");
7142                 sbuf_hexdump(sb, num_ptr, print_len, NULL, 0);
7143         }
7144
7145         return (retval);
7146 }
7147
7148 int
7149 scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7150                      uint32_t valid_len, uint32_t flags,
7151                      uint32_t output_flags, char *error_str,
7152                      int error_str_len)
7153 {
7154         uint64_t print_number;
7155         size_t avail_len;
7156         uint32_t number_size;
7157         int retval = 0;
7158
7159         number_size = scsi_2btoul(hdr->length);
7160
7161         avail_len = valid_len - sizeof(*hdr);
7162         if (avail_len < number_size) { 
7163                 if (error_str != NULL) {
7164                         snprintf(error_str, error_str_len, "Available "
7165                                  "length of attribute ID 0x%.4x %zu < field "
7166                                  "length %u", scsi_2btoul(hdr->id), avail_len,
7167                                  number_size);
7168                 }
7169                 retval = 1;
7170                 goto bailout;
7171         }
7172
7173         switch (number_size) {
7174         case 0:
7175                 /*
7176                  * We don't treat this as an error, since there may be
7177                  * scenarios where a device reports a field but then gives
7178                  * a length of 0.  See the note in scsi_attrib_ascii_sbuf().
7179                  */
7180                 goto bailout;
7181                 break; /*NOTREACHED*/
7182         case 1:
7183                 print_number = hdr->attribute[0];
7184                 break;
7185         case 2:
7186                 print_number = scsi_2btoul(hdr->attribute);
7187                 break;
7188         case 3:
7189                 print_number = scsi_3btoul(hdr->attribute);
7190                 break;
7191         case 4:
7192                 print_number = scsi_4btoul(hdr->attribute);
7193                 break;
7194         case 8:
7195                 print_number = scsi_8btou64(hdr->attribute);
7196                 break;
7197         default:
7198                 /*
7199                  * If we wind up here, the number is too big to print
7200                  * normally, so just do a hexdump.
7201                  */
7202                 retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7203                                                   flags, output_flags,
7204                                                   error_str, error_str_len);
7205                 goto bailout;
7206                 break;
7207         }
7208
7209         if (flags & SCSI_ATTR_FLAG_FP) {
7210 #ifndef _KERNEL
7211                 long double num_float;
7212
7213                 num_float = (long double)print_number;
7214
7215                 if (flags & SCSI_ATTR_FLAG_DIV_10)
7216                         num_float /= 10;
7217
7218                 sbuf_printf(sb, "%.*Lf", (flags & SCSI_ATTR_FLAG_FP_1DIGIT) ?
7219                             1 : 0, num_float);
7220 #else /* _KERNEL */
7221                 sbuf_printf(sb, "%ju", (flags & SCSI_ATTR_FLAG_DIV_10) ?
7222                             (print_number / 10) : print_number);
7223 #endif /* _KERNEL */
7224         } else if (flags & SCSI_ATTR_FLAG_HEX) {
7225                 sbuf_printf(sb, "0x%jx", (uintmax_t)print_number);
7226         } else
7227                 sbuf_printf(sb, "%ju", (uintmax_t)print_number);
7228
7229 bailout:
7230         return (retval);
7231 }
7232
7233 int
7234 scsi_attrib_ascii_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7235                        uint32_t valid_len, uint32_t flags,
7236                        uint32_t output_flags, char *error_str,
7237                        int error_str_len)
7238 {
7239         size_t avail_len;
7240         uint32_t field_size, print_size;
7241         int retval = 0;
7242
7243         avail_len = valid_len - sizeof(*hdr);
7244         field_size = scsi_2btoul(hdr->length);
7245         print_size = MIN(avail_len, field_size);
7246
7247         if (print_size > 0) {
7248                 cam_strvis_flags strvis_flags;
7249
7250                 switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) {
7251                 case SCSI_ATTR_OUTPUT_NONASCII_TRIM:
7252                         strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM;
7253                         break;
7254                 case SCSI_ATTR_OUTPUT_NONASCII_RAW:
7255                         strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW;
7256                         break;
7257                 case SCSI_ATTR_OUTPUT_NONASCII_ESC:
7258                 default:
7259                         strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC;
7260                         break;
7261                 }
7262                 cam_strvis_sbuf(sb, hdr->attribute, print_size, strvis_flags);
7263         } else if (avail_len < field_size) {
7264                 /*
7265                  * We only report an error if the user didn't allocate
7266                  * enough space to hold the full value of this field.  If
7267                  * the field length is 0, that is allowed by the spec.
7268                  * e.g. in SPC-4r37, section 7.4.2.2.5, VOLUME IDENTIFIER
7269                  * "This attribute indicates the current volume identifier
7270                  * (see SMC-3) of the medium. If the device server supports
7271                  * this attribute but does not have access to the volume
7272                  * identifier, the device server shall report this attribute
7273                  * with an attribute length value of zero."
7274                  */
7275                 if (error_str != NULL) {
7276                         snprintf(error_str, error_str_len, "Available "
7277                                  "length of attribute ID 0x%.4x %zu < field "
7278                                  "length %u", scsi_2btoul(hdr->id), avail_len,
7279                                  field_size);
7280                 }
7281                 retval = 1;
7282         }
7283
7284         return (retval);
7285 }
7286
7287 int
7288 scsi_attrib_text_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7289                       uint32_t valid_len, uint32_t flags, 
7290                       uint32_t output_flags, char *error_str,
7291                       int error_str_len)
7292 {
7293         size_t avail_len;
7294         uint32_t field_size, print_size;
7295         int retval = 0;
7296         int esc_text = 1;
7297
7298         avail_len = valid_len - sizeof(*hdr);
7299         field_size = scsi_2btoul(hdr->length);
7300         print_size = MIN(avail_len, field_size);
7301
7302         if ((output_flags & SCSI_ATTR_OUTPUT_TEXT_MASK) ==
7303              SCSI_ATTR_OUTPUT_TEXT_RAW)
7304                 esc_text = 0;
7305
7306         if (print_size > 0) {
7307                 uint32_t i;
7308
7309                 for (i = 0; i < print_size; i++) {
7310                         if (hdr->attribute[i] == '\0')
7311                                 continue;
7312                         else if (((unsigned char)hdr->attribute[i] < 0x80)
7313                               || (esc_text == 0))
7314                                 sbuf_putc(sb, hdr->attribute[i]);
7315                         else
7316                                 sbuf_printf(sb, "%%%02x",
7317                                     (unsigned char)hdr->attribute[i]);
7318                 }
7319         } else if (avail_len < field_size) {
7320                 /*
7321                  * We only report an error if the user didn't allocate
7322                  * enough space to hold the full value of this field.
7323                  */
7324                 if (error_str != NULL) {
7325                         snprintf(error_str, error_str_len, "Available "
7326                                  "length of attribute ID 0x%.4x %zu < field "
7327                                  "length %u", scsi_2btoul(hdr->id), avail_len,
7328                                  field_size);
7329                 }
7330                 retval = 1;
7331         }
7332
7333         return (retval);
7334 }
7335
7336 struct scsi_attrib_table_entry *
7337 scsi_find_attrib_entry(struct scsi_attrib_table_entry *table,
7338                        size_t num_table_entries, uint32_t id)
7339 {
7340         uint32_t i;
7341
7342         for (i = 0; i < num_table_entries; i++) {
7343                 if (table[i].id == id)
7344                         return (&table[i]);
7345         }
7346
7347         return (NULL);
7348 }
7349
7350 struct scsi_attrib_table_entry *
7351 scsi_get_attrib_entry(uint32_t id)
7352 {
7353         return (scsi_find_attrib_entry(scsi_mam_attr_table,
7354                 sizeof(scsi_mam_attr_table) / sizeof(scsi_mam_attr_table[0]),
7355                 id));
7356 }
7357
7358 int
7359 scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len,
7360    struct scsi_mam_attribute_header *hdr, uint32_t output_flags,
7361    char *error_str, size_t error_str_len)
7362 {
7363         int retval;
7364
7365         switch (hdr->byte2 & SMA_FORMAT_MASK) {
7366         case SMA_FORMAT_ASCII:
7367                 retval = scsi_attrib_ascii_sbuf(sb, hdr, valid_len,
7368                     SCSI_ATTR_FLAG_NONE, output_flags, error_str,error_str_len);
7369                 break;
7370         case SMA_FORMAT_BINARY:
7371                 if (scsi_2btoul(hdr->length) <= 8)
7372                         retval = scsi_attrib_int_sbuf(sb, hdr, valid_len,
7373                             SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7374                             error_str_len);
7375                 else
7376                         retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len,
7377                             SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7378                             error_str_len);
7379                 break;
7380         case SMA_FORMAT_TEXT:
7381                 retval = scsi_attrib_text_sbuf(sb, hdr, valid_len,
7382                     SCSI_ATTR_FLAG_NONE, output_flags, error_str,
7383                     error_str_len);
7384                 break;
7385         default:
7386                 if (error_str != NULL) {
7387                         snprintf(error_str, error_str_len, "Unknown attribute "
7388                             "format 0x%x", hdr->byte2 & SMA_FORMAT_MASK);
7389                 }
7390                 retval = 1;
7391                 goto bailout;
7392                 break; /*NOTREACHED*/
7393         }
7394
7395         sbuf_trim(sb);
7396
7397 bailout:
7398
7399         return (retval);
7400 }
7401
7402 void
7403 scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags,
7404                         struct scsi_mam_attribute_header *hdr,
7405                         uint32_t valid_len, const char *desc)
7406 {
7407         int need_space = 0;
7408         uint32_t len;
7409         uint32_t id;
7410
7411         /*
7412          * We can't do anything if we don't have enough valid data for the
7413          * header.
7414          */
7415         if (valid_len < sizeof(*hdr))
7416                 return;
7417
7418         id = scsi_2btoul(hdr->id);
7419         /*
7420          * Note that we print out the value of the attribute listed in the
7421          * header, regardless of whether we actually got that many bytes
7422          * back from the device through the controller.  A truncated result
7423          * could be the result of a failure to ask for enough data; the
7424          * header indicates how many bytes are allocated for this attribute
7425          * in the MAM.
7426          */
7427         len = scsi_2btoul(hdr->length);
7428
7429         if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_MASK) ==
7430             SCSI_ATTR_OUTPUT_FIELD_NONE)
7431                 return;
7432
7433         if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_DESC)
7434          && (desc != NULL)) {
7435                 sbuf_printf(sb, "%s", desc);
7436                 need_space = 1;
7437         }
7438
7439         if (output_flags & SCSI_ATTR_OUTPUT_FIELD_NUM) {
7440                 sbuf_printf(sb, "%s(0x%.4x)", (need_space) ? " " : "", id);
7441                 need_space = 0;
7442         }
7443
7444         if (output_flags & SCSI_ATTR_OUTPUT_FIELD_SIZE) {
7445                 sbuf_printf(sb, "%s[%d]", (need_space) ? " " : "", len);
7446                 need_space = 0;
7447         }
7448         if (output_flags & SCSI_ATTR_OUTPUT_FIELD_RW) {
7449                 sbuf_printf(sb, "%s(%s)", (need_space) ? " " : "",
7450                             (hdr->byte2 & SMA_READ_ONLY) ? "RO" : "RW");
7451         }
7452         sbuf_printf(sb, ": ");
7453 }
7454
7455 int
7456 scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr,
7457                  uint32_t valid_len, struct scsi_attrib_table_entry *user_table,
7458                  size_t num_user_entries, int prefer_user_table,
7459                  uint32_t output_flags, char *error_str, int error_str_len)
7460 {
7461         int retval;
7462         struct scsi_attrib_table_entry *table1 = NULL, *table2 = NULL;
7463         struct scsi_attrib_table_entry *entry = NULL;
7464         size_t table1_size = 0, table2_size = 0;
7465         uint32_t id;
7466
7467         retval = 0;
7468
7469         if (valid_len < sizeof(*hdr)) {
7470                 retval = 1;
7471                 goto bailout;
7472         }
7473
7474         id = scsi_2btoul(hdr->id);
7475
7476         if (user_table != NULL) {
7477                 if (prefer_user_table != 0) {
7478                         table1 = user_table;
7479                         table1_size = num_user_entries;
7480                         table2 = scsi_mam_attr_table;
7481                         table2_size = sizeof(scsi_mam_attr_table) /
7482                                       sizeof(scsi_mam_attr_table[0]);
7483                 } else {
7484                         table1 = scsi_mam_attr_table;
7485                         table1_size = sizeof(scsi_mam_attr_table) /
7486                                       sizeof(scsi_mam_attr_table[0]);
7487                         table2 = user_table;
7488                         table2_size = num_user_entries;
7489                 }
7490         } else {
7491                 table1 = scsi_mam_attr_table;
7492                 table1_size = sizeof(scsi_mam_attr_table) /
7493                               sizeof(scsi_mam_attr_table[0]);
7494         }
7495
7496         entry = scsi_find_attrib_entry(table1, table1_size, id);
7497         if (entry != NULL) {
7498                 scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len,
7499                                         entry->desc);
7500                 if (entry->to_str == NULL)
7501                         goto print_default;
7502                 retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7503                                        output_flags, error_str, error_str_len);
7504                 goto bailout;
7505         }
7506         if (table2 != NULL) {
7507                 entry = scsi_find_attrib_entry(table2, table2_size, id);
7508                 if (entry != NULL) {
7509                         if (entry->to_str == NULL)
7510                                 goto print_default;
7511
7512                         scsi_attrib_prefix_sbuf(sb, output_flags, hdr,
7513                                                 valid_len, entry->desc);
7514                         retval = entry->to_str(sb, hdr, valid_len, entry->flags,
7515                                                output_flags, error_str,
7516                                                error_str_len);
7517                         goto bailout;
7518                 }
7519         }
7520
7521         scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, NULL);
7522
7523 print_default:
7524         retval = scsi_attrib_value_sbuf(sb, valid_len, hdr, output_flags,
7525             error_str, error_str_len);
7526 bailout:
7527         if (retval == 0) {
7528                 if ((entry != NULL)
7529                  && (entry->suffix != NULL))
7530                         sbuf_printf(sb, " %s", entry->suffix);
7531
7532                 sbuf_trim(sb);
7533                 sbuf_printf(sb, "\n");
7534         }
7535
7536         return (retval);
7537 }
7538
7539 void
7540 scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries,
7541                      void (*cbfcnp)(struct cam_periph *, union ccb *),
7542                      u_int8_t tag_action, u_int8_t sense_len, u_int32_t timeout)
7543 {
7544         struct scsi_test_unit_ready *scsi_cmd;
7545
7546         cam_fill_csio(csio,
7547                       retries,
7548                       cbfcnp,
7549                       CAM_DIR_NONE,
7550                       tag_action,
7551                       /*data_ptr*/NULL,
7552                       /*dxfer_len*/0,
7553                       sense_len,
7554                       sizeof(*scsi_cmd),
7555                       timeout);
7556
7557         scsi_cmd = (struct scsi_test_unit_ready *)&csio->cdb_io.cdb_bytes;
7558         bzero(scsi_cmd, sizeof(*scsi_cmd));
7559         scsi_cmd->opcode = TEST_UNIT_READY;
7560 }
7561
7562 void
7563 scsi_request_sense(struct ccb_scsiio *csio, u_int32_t retries,
7564                    void (*cbfcnp)(struct cam_periph *, union ccb *),
7565                    void *data_ptr, u_int8_t dxfer_len, u_int8_t tag_action,
7566                    u_int8_t sense_len, u_int32_t timeout)
7567 {
7568         struct scsi_request_sense *scsi_cmd;
7569
7570         cam_fill_csio(csio,
7571                       retries,
7572                       cbfcnp,
7573                       CAM_DIR_IN,
7574                       tag_action,
7575                       data_ptr,
7576                       dxfer_len,
7577                       sense_len,
7578                       sizeof(*scsi_cmd),
7579                       timeout);
7580
7581         scsi_cmd = (struct scsi_request_sense *)&csio->cdb_io.cdb_bytes;
7582         bzero(scsi_cmd, sizeof(*scsi_cmd));
7583         scsi_cmd->opcode = REQUEST_SENSE;
7584         scsi_cmd->length = dxfer_len;
7585 }
7586
7587 void
7588 scsi_inquiry(struct ccb_scsiio *csio, u_int32_t retries,
7589              void (*cbfcnp)(struct cam_periph *, union ccb *),
7590              u_int8_t tag_action, u_int8_t *inq_buf, u_int32_t inq_len,
7591              int evpd, u_int8_t page_code, u_int8_t sense_len,
7592              u_int32_t timeout)
7593 {
7594         struct scsi_inquiry *scsi_cmd;
7595
7596         cam_fill_csio(csio,
7597                       retries,
7598                       cbfcnp,
7599                       /*flags*/CAM_DIR_IN,
7600                       tag_action,
7601                       /*data_ptr*/inq_buf,
7602                       /*dxfer_len*/inq_len,
7603                       sense_len,
7604                       sizeof(*scsi_cmd),
7605                       timeout);
7606
7607         scsi_cmd = (struct scsi_inquiry *)&csio->cdb_io.cdb_bytes;
7608         bzero(scsi_cmd, sizeof(*scsi_cmd));
7609         scsi_cmd->opcode = INQUIRY;
7610         if (evpd) {
7611                 scsi_cmd->byte2 |= SI_EVPD;
7612                 scsi_cmd->page_code = page_code;                
7613         }
7614         scsi_ulto2b(inq_len, scsi_cmd->length);
7615 }
7616
7617 void
7618 scsi_mode_sense(struct ccb_scsiio *csio, u_int32_t retries,
7619                 void (*cbfcnp)(struct cam_periph *, union ccb *),
7620                 u_int8_t tag_action, int dbd, u_int8_t page_code,
7621                 u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7622                 u_int8_t sense_len, u_int32_t timeout)
7623 {
7624
7625         scsi_mode_sense_len(csio, retries, cbfcnp, tag_action, dbd,
7626                             page_code, page, param_buf, param_len, 0,
7627                             sense_len, timeout);
7628 }
7629
7630 void
7631 scsi_mode_sense_len(struct ccb_scsiio *csio, u_int32_t retries,
7632                     void (*cbfcnp)(struct cam_periph *, union ccb *),
7633                     u_int8_t tag_action, int dbd, u_int8_t page_code,
7634                     u_int8_t page, u_int8_t *param_buf, u_int32_t param_len,
7635                     int minimum_cmd_size, u_int8_t sense_len, u_int32_t timeout)
7636 {
7637         u_int8_t cdb_len;
7638
7639         /*
7640          * Use the smallest possible command to perform the operation.
7641          */
7642         if ((param_len < 256)
7643          && (minimum_cmd_size < 10)) {
7644                 /*
7645                  * We can fit in a 6 byte cdb.
7646                  */
7647                 struct scsi_mode_sense_6 *scsi_cmd;
7648
7649                 scsi_cmd = (struct scsi_mode_sense_6 *)&csio->cdb_io.cdb_bytes;
7650                 bzero(scsi_cmd, sizeof(*scsi_cmd));
7651                 scsi_cmd->opcode = MODE_SENSE_6;
7652                 if (dbd != 0)
7653                         scsi_cmd->byte2 |= SMS_DBD;
7654                 scsi_cmd->page = page_code | page;
7655                 scsi_cmd->length = param_len;
7656                 cdb_len = sizeof(*scsi_cmd);
7657         } else {
7658                 /*
7659                  * Need a 10 byte cdb.
7660                  */
7661                 struct scsi_mode_sense_10 *scsi_cmd;
7662
7663                 scsi_cmd = (struct scsi_mode_sense_10 *)&csio->cdb_io.cdb_bytes;
7664                 bzero(scsi_cmd, sizeof(*scsi_cmd));
7665                 scsi_cmd->opcode = MODE_SENSE_10;
7666                 if (dbd != 0)
7667                         scsi_cmd->byte2 |= SMS_DBD;
7668                 scsi_cmd->page = page_code | page;
7669                 scsi_ulto2b(param_len, scsi_cmd->length);
7670                 cdb_len = sizeof(*scsi_cmd);
7671         }
7672         cam_fill_csio(csio,
7673                       retries,
7674                       cbfcnp,
7675                       CAM_DIR_IN,
7676                       tag_action,
7677                       param_buf,
7678                       param_len,
7679                       sense_len,
7680                       cdb_len,
7681                       timeout);
7682 }
7683
7684 void
7685 scsi_mode_select(struct ccb_scsiio *csio, u_int32_t retries,
7686                  void (*cbfcnp)(struct cam_periph *, union ccb *),
7687                  u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7688                  u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7689                  u_int32_t timeout)
7690 {
7691         scsi_mode_select_len(csio, retries, cbfcnp, tag_action,
7692                              scsi_page_fmt, save_pages, param_buf,
7693                              param_len, 0, sense_len, timeout);
7694 }
7695
7696 void
7697 scsi_mode_select_len(struct ccb_scsiio *csio, u_int32_t retries,
7698                      void (*cbfcnp)(struct cam_periph *, union ccb *),
7699                      u_int8_t tag_action, int scsi_page_fmt, int save_pages,
7700                      u_int8_t *param_buf, u_int32_t param_len,
7701                      int minimum_cmd_size, u_int8_t sense_len,
7702                      u_int32_t timeout)
7703 {
7704         u_int8_t cdb_len;
7705
7706         /*
7707          * Use the smallest possible command to perform the operation.
7708          */
7709         if ((param_len < 256)
7710          && (minimum_cmd_size < 10)) {
7711                 /*
7712                  * We can fit in a 6 byte cdb.
7713                  */
7714                 struct scsi_mode_select_6 *scsi_cmd;
7715
7716                 scsi_cmd = (struct scsi_mode_select_6 *)&csio->cdb_io.cdb_bytes;
7717                 bzero(scsi_cmd, sizeof(*scsi_cmd));
7718                 scsi_cmd->opcode = MODE_SELECT_6;
7719                 if (scsi_page_fmt != 0)
7720                         scsi_cmd->byte2 |= SMS_PF;
7721                 if (save_pages != 0)
7722                         scsi_cmd->byte2 |= SMS_SP;
7723                 scsi_cmd->length = param_len;
7724                 cdb_len = sizeof(*scsi_cmd);
7725         } else {
7726                 /*
7727                  * Need a 10 byte cdb.
7728                  */
7729                 struct scsi_mode_select_10 *scsi_cmd;
7730
7731                 scsi_cmd =
7732                     (struct scsi_mode_select_10 *)&csio->cdb_io.cdb_bytes;
7733                 bzero(scsi_cmd, sizeof(*scsi_cmd));
7734                 scsi_cmd->opcode = MODE_SELECT_10;
7735                 if (scsi_page_fmt != 0)
7736                         scsi_cmd->byte2 |= SMS_PF;
7737                 if (save_pages != 0)
7738                         scsi_cmd->byte2 |= SMS_SP;
7739                 scsi_ulto2b(param_len, scsi_cmd->length);
7740                 cdb_len = sizeof(*scsi_cmd);
7741         }
7742         cam_fill_csio(csio,
7743                       retries,
7744                       cbfcnp,
7745                       CAM_DIR_OUT,
7746                       tag_action,
7747                       param_buf,
7748                       param_len,
7749                       sense_len,
7750                       cdb_len,
7751                       timeout);
7752 }
7753
7754 void
7755 scsi_log_sense(struct ccb_scsiio *csio, u_int32_t retries,
7756                void (*cbfcnp)(struct cam_periph *, union ccb *),
7757                u_int8_t tag_action, u_int8_t page_code, u_int8_t page,
7758                int save_pages, int ppc, u_int32_t paramptr,
7759                u_int8_t *param_buf, u_int32_t param_len, u_int8_t sense_len,
7760                u_int32_t timeout)
7761 {
7762         struct scsi_log_sense *scsi_cmd;
7763         u_int8_t cdb_len;
7764
7765         scsi_cmd = (struct scsi_log_sense *)&csio->cdb_io.cdb_bytes;
7766         bzero(scsi_cmd, sizeof(*scsi_cmd));
7767         scsi_cmd->opcode = LOG_SENSE;
7768         scsi_cmd->page = page_code | page;
7769         if (save_pages != 0)
7770                 scsi_cmd->byte2 |= SLS_SP;
7771         if (ppc != 0)
7772                 scsi_cmd->byte2 |= SLS_PPC;
7773         scsi_ulto2b(paramptr, scsi_cmd->paramptr);
7774         scsi_ulto2b(param_len, scsi_cmd->length);
7775         cdb_len = sizeof(*scsi_cmd);
7776
7777         cam_fill_csio(csio,
7778                       retries,
7779                       cbfcnp,
7780                       /*flags*/CAM_DIR_IN,
7781                       tag_action,
7782                       /*data_ptr*/param_buf,
7783                       /*dxfer_len*/param_len,
7784                       sense_len,
7785                       cdb_len,
7786                       timeout);
7787 }
7788
7789 void
7790 scsi_log_select(struct ccb_scsiio *csio, u_int32_t retries,
7791                 void (*cbfcnp)(struct cam_periph *, union ccb *),
7792                 u_int8_t tag_action, u_int8_t page_code, int save_pages,
7793                 int pc_reset, u_int8_t *param_buf, u_int32_t param_len,
7794                 u_int8_t sense_len, u_int32_t timeout)
7795 {
7796         struct scsi_log_select *scsi_cmd;
7797         u_int8_t cdb_len;
7798
7799         scsi_cmd = (struct scsi_log_select *)&csio->cdb_io.cdb_bytes;
7800         bzero(scsi_cmd, sizeof(*scsi_cmd));
7801         scsi_cmd->opcode = LOG_SELECT;
7802         scsi_cmd->page = page_code & SLS_PAGE_CODE;
7803         if (save_pages != 0)
7804                 scsi_cmd->byte2 |= SLS_SP;
7805         if (pc_reset != 0)
7806                 scsi_cmd->byte2 |= SLS_PCR;
7807         scsi_ulto2b(param_len, scsi_cmd->length);
7808         cdb_len = sizeof(*scsi_cmd);
7809
7810         cam_fill_csio(csio,
7811                       retries,
7812                       cbfcnp,
7813                       /*flags*/CAM_DIR_OUT,
7814                       tag_action,
7815                       /*data_ptr*/param_buf,
7816                       /*dxfer_len*/param_len,
7817                       sense_len,
7818                       cdb_len,
7819                       timeout);
7820 }
7821
7822 /*
7823  * Prevent or allow the user to remove the media
7824  */
7825 void
7826 scsi_prevent(struct ccb_scsiio *csio, u_int32_t retries,
7827              void (*cbfcnp)(struct cam_periph *, union ccb *),
7828              u_int8_t tag_action, u_int8_t action,
7829              u_int8_t sense_len, u_int32_t timeout)
7830 {
7831         struct scsi_prevent *scsi_cmd;
7832
7833         cam_fill_csio(csio,
7834                       retries,
7835                       cbfcnp,
7836                       /*flags*/CAM_DIR_NONE,
7837                       tag_action,
7838                       /*data_ptr*/NULL,
7839                       /*dxfer_len*/0,
7840                       sense_len,
7841                       sizeof(*scsi_cmd),
7842                       timeout);
7843
7844         scsi_cmd = (struct scsi_prevent *)&csio->cdb_io.cdb_bytes;
7845         bzero(scsi_cmd, sizeof(*scsi_cmd));
7846         scsi_cmd->opcode = PREVENT_ALLOW;
7847         scsi_cmd->how = action;
7848 }
7849
7850 /* XXX allow specification of address and PMI bit and LBA */
7851 void
7852 scsi_read_capacity(struct ccb_scsiio *csio, u_int32_t retries,
7853                    void (*cbfcnp)(struct cam_periph *, union ccb *),
7854                    u_int8_t tag_action,
7855                    struct scsi_read_capacity_data *rcap_buf,
7856                    u_int8_t sense_len, u_int32_t timeout)
7857 {
7858         struct scsi_read_capacity *scsi_cmd;
7859
7860         cam_fill_csio(csio,
7861                       retries,
7862                       cbfcnp,
7863                       /*flags*/CAM_DIR_IN,
7864                       tag_action,
7865                       /*data_ptr*/(u_int8_t *)rcap_buf,
7866                       /*dxfer_len*/sizeof(*rcap_buf),
7867                       sense_len,
7868                       sizeof(*scsi_cmd),
7869                       timeout);
7870
7871         scsi_cmd = (struct scsi_read_capacity *)&csio->cdb_io.cdb_bytes;
7872         bzero(scsi_cmd, sizeof(*scsi_cmd));
7873         scsi_cmd->opcode = READ_CAPACITY;
7874 }
7875
7876 void
7877 scsi_read_capacity_16(struct ccb_scsiio *csio, uint32_t retries,
7878                       void (*cbfcnp)(struct cam_periph *, union ccb *),
7879                       uint8_t tag_action, uint64_t lba, int reladr, int pmi,
7880                       uint8_t *rcap_buf, int rcap_buf_len, uint8_t sense_len,
7881                       uint32_t timeout)
7882 {
7883         struct scsi_read_capacity_16 *scsi_cmd;
7884
7885         
7886         cam_fill_csio(csio,
7887                       retries,
7888                       cbfcnp,
7889                       /*flags*/CAM_DIR_IN,
7890                       tag_action,
7891                       /*data_ptr*/(u_int8_t *)rcap_buf,
7892                       /*dxfer_len*/rcap_buf_len,
7893                       sense_len,
7894                       sizeof(*scsi_cmd),
7895                       timeout);
7896         scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
7897         bzero(scsi_cmd, sizeof(*scsi_cmd));
7898         scsi_cmd->opcode = SERVICE_ACTION_IN;
7899         scsi_cmd->service_action = SRC16_SERVICE_ACTION;
7900         scsi_u64to8b(lba, scsi_cmd->addr);
7901         scsi_ulto4b(rcap_buf_len, scsi_cmd->alloc_len);
7902         if (pmi)
7903                 reladr |= SRC16_PMI;
7904         if (reladr)
7905                 reladr |= SRC16_RELADR;
7906 }
7907
7908 void
7909 scsi_report_luns(struct ccb_scsiio *csio, u_int32_t retries,
7910                  void (*cbfcnp)(struct cam_periph *, union ccb *),
7911                  u_int8_t tag_action, u_int8_t select_report,
7912                  struct scsi_report_luns_data *rpl_buf, u_int32_t alloc_len,
7913                  u_int8_t sense_len, u_int32_t timeout)
7914 {
7915         struct scsi_report_luns *scsi_cmd;
7916
7917         cam_fill_csio(csio,
7918                       retries,
7919                       cbfcnp,
7920                       /*flags*/CAM_DIR_IN,
7921                       tag_action,
7922                       /*data_ptr*/(u_int8_t *)rpl_buf,
7923                       /*dxfer_len*/alloc_len,
7924                       sense_len,
7925                       sizeof(*scsi_cmd),
7926                       timeout);
7927         scsi_cmd = (struct scsi_report_luns *)&csio->cdb_io.cdb_bytes;
7928         bzero(scsi_cmd, sizeof(*scsi_cmd));
7929         scsi_cmd->opcode = REPORT_LUNS;
7930         scsi_cmd->select_report = select_report;
7931         scsi_ulto4b(alloc_len, scsi_cmd->length);
7932 }
7933
7934 void
7935 scsi_report_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7936                  void (*cbfcnp)(struct cam_periph *, union ccb *),
7937                  u_int8_t tag_action, u_int8_t pdf,
7938                  void *buf, u_int32_t alloc_len,
7939                  u_int8_t sense_len, u_int32_t timeout)
7940 {
7941         struct scsi_target_group *scsi_cmd;
7942
7943         cam_fill_csio(csio,
7944                       retries,
7945                       cbfcnp,
7946                       /*flags*/CAM_DIR_IN,
7947                       tag_action,
7948                       /*data_ptr*/(u_int8_t *)buf,
7949                       /*dxfer_len*/alloc_len,
7950                       sense_len,
7951                       sizeof(*scsi_cmd),
7952                       timeout);
7953         scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7954         bzero(scsi_cmd, sizeof(*scsi_cmd));
7955         scsi_cmd->opcode = MAINTENANCE_IN;
7956         scsi_cmd->service_action = REPORT_TARGET_PORT_GROUPS | pdf;
7957         scsi_ulto4b(alloc_len, scsi_cmd->length);
7958 }
7959
7960 void
7961 scsi_set_target_group(struct ccb_scsiio *csio, u_int32_t retries,
7962                  void (*cbfcnp)(struct cam_periph *, union ccb *),
7963                  u_int8_t tag_action, void *buf, u_int32_t alloc_len,
7964                  u_int8_t sense_len, u_int32_t timeout)
7965 {
7966         struct scsi_target_group *scsi_cmd;
7967
7968         cam_fill_csio(csio,
7969                       retries,
7970                       cbfcnp,
7971                       /*flags*/CAM_DIR_OUT,
7972                       tag_action,
7973                       /*data_ptr*/(u_int8_t *)buf,
7974                       /*dxfer_len*/alloc_len,
7975                       sense_len,
7976                       sizeof(*scsi_cmd),
7977                       timeout);
7978         scsi_cmd = (struct scsi_target_group *)&csio->cdb_io.cdb_bytes;
7979         bzero(scsi_cmd, sizeof(*scsi_cmd));
7980         scsi_cmd->opcode = MAINTENANCE_OUT;
7981         scsi_cmd->service_action = SET_TARGET_PORT_GROUPS;
7982         scsi_ulto4b(alloc_len, scsi_cmd->length);
7983 }
7984
7985 /*
7986  * Syncronize the media to the contents of the cache for
7987  * the given lba/count pair.  Specifying 0/0 means sync
7988  * the whole cache.
7989  */
7990 void
7991 scsi_synchronize_cache(struct ccb_scsiio *csio, u_int32_t retries,
7992                        void (*cbfcnp)(struct cam_periph *, union ccb *),
7993                        u_int8_t tag_action, u_int32_t begin_lba,
7994                        u_int16_t lb_count, u_int8_t sense_len,
7995                        u_int32_t timeout)
7996 {
7997         struct scsi_sync_cache *scsi_cmd;
7998
7999         cam_fill_csio(csio,
8000                       retries,
8001                       cbfcnp,
8002                       /*flags*/CAM_DIR_NONE,
8003                       tag_action,
8004                       /*data_ptr*/NULL,
8005                       /*dxfer_len*/0,
8006                       sense_len,
8007                       sizeof(*scsi_cmd),
8008                       timeout);
8009
8010         scsi_cmd = (struct scsi_sync_cache *)&csio->cdb_io.cdb_bytes;
8011         bzero(scsi_cmd, sizeof(*scsi_cmd));
8012         scsi_cmd->opcode = SYNCHRONIZE_CACHE;
8013         scsi_ulto4b(begin_lba, scsi_cmd->begin_lba);
8014         scsi_ulto2b(lb_count, scsi_cmd->lb_count);
8015 }
8016
8017 void
8018 scsi_read_write(struct ccb_scsiio *csio, u_int32_t retries,
8019                 void (*cbfcnp)(struct cam_periph *, union ccb *),
8020                 u_int8_t tag_action, int readop, u_int8_t byte2,
8021                 int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
8022                 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
8023                 u_int32_t timeout)
8024 {
8025         int read;
8026         u_int8_t cdb_len;
8027
8028         read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
8029
8030         /*
8031          * Use the smallest possible command to perform the operation
8032          * as some legacy hardware does not support the 10 byte commands.
8033          * If any of the bits in byte2 is set, we have to go with a larger
8034          * command.
8035          */
8036         if ((minimum_cmd_size < 10)
8037          && ((lba & 0x1fffff) == lba)
8038          && ((block_count & 0xff) == block_count)
8039          && (byte2 == 0)) {
8040                 /*
8041                  * We can fit in a 6 byte cdb.
8042                  */
8043                 struct scsi_rw_6 *scsi_cmd;
8044
8045                 scsi_cmd = (struct scsi_rw_6 *)&csio->cdb_io.cdb_bytes;
8046                 scsi_cmd->opcode = read ? READ_6 : WRITE_6;
8047                 scsi_ulto3b(lba, scsi_cmd->addr);
8048                 scsi_cmd->length = block_count & 0xff;
8049                 scsi_cmd->control = 0;
8050                 cdb_len = sizeof(*scsi_cmd);
8051
8052                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8053                           ("6byte: %x%x%x:%d:%d\n", scsi_cmd->addr[0],
8054                            scsi_cmd->addr[1], scsi_cmd->addr[2],
8055                            scsi_cmd->length, dxfer_len));
8056         } else if ((minimum_cmd_size < 12)
8057                 && ((block_count & 0xffff) == block_count)
8058                 && ((lba & 0xffffffff) == lba)) {
8059                 /*
8060                  * Need a 10 byte cdb.
8061                  */
8062                 struct scsi_rw_10 *scsi_cmd;
8063
8064                 scsi_cmd = (struct scsi_rw_10 *)&csio->cdb_io.cdb_bytes;
8065                 scsi_cmd->opcode = read ? READ_10 : WRITE_10;
8066                 scsi_cmd->byte2 = byte2;
8067                 scsi_ulto4b(lba, scsi_cmd->addr);
8068                 scsi_cmd->reserved = 0;
8069                 scsi_ulto2b(block_count, scsi_cmd->length);
8070                 scsi_cmd->control = 0;
8071                 cdb_len = sizeof(*scsi_cmd);
8072
8073                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8074                           ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8075                            scsi_cmd->addr[1], scsi_cmd->addr[2],
8076                            scsi_cmd->addr[3], scsi_cmd->length[0],
8077                            scsi_cmd->length[1], dxfer_len));
8078         } else if ((minimum_cmd_size < 16)
8079                 && ((block_count & 0xffffffff) == block_count)
8080                 && ((lba & 0xffffffff) == lba)) {
8081                 /* 
8082                  * The block count is too big for a 10 byte CDB, use a 12
8083                  * byte CDB.
8084                  */
8085                 struct scsi_rw_12 *scsi_cmd;
8086
8087                 scsi_cmd = (struct scsi_rw_12 *)&csio->cdb_io.cdb_bytes;
8088                 scsi_cmd->opcode = read ? READ_12 : WRITE_12;
8089                 scsi_cmd->byte2 = byte2;
8090                 scsi_ulto4b(lba, scsi_cmd->addr);
8091                 scsi_cmd->reserved = 0;
8092                 scsi_ulto4b(block_count, scsi_cmd->length);
8093                 scsi_cmd->control = 0;
8094                 cdb_len = sizeof(*scsi_cmd);
8095
8096                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8097                           ("12byte: %x%x%x%x:%x%x%x%x: %d\n", scsi_cmd->addr[0],
8098                            scsi_cmd->addr[1], scsi_cmd->addr[2],
8099                            scsi_cmd->addr[3], scsi_cmd->length[0],
8100                            scsi_cmd->length[1], scsi_cmd->length[2],
8101                            scsi_cmd->length[3], dxfer_len));
8102         } else {
8103                 /*
8104                  * 16 byte CDB.  We'll only get here if the LBA is larger
8105                  * than 2^32, or if the user asks for a 16 byte command.
8106                  */
8107                 struct scsi_rw_16 *scsi_cmd;
8108
8109                 scsi_cmd = (struct scsi_rw_16 *)&csio->cdb_io.cdb_bytes;
8110                 scsi_cmd->opcode = read ? READ_16 : WRITE_16;
8111                 scsi_cmd->byte2 = byte2;
8112                 scsi_u64to8b(lba, scsi_cmd->addr);
8113                 scsi_cmd->reserved = 0;
8114                 scsi_ulto4b(block_count, scsi_cmd->length);
8115                 scsi_cmd->control = 0;
8116                 cdb_len = sizeof(*scsi_cmd);
8117         }
8118         cam_fill_csio(csio,
8119                       retries,
8120                       cbfcnp,
8121                       (read ? CAM_DIR_IN : CAM_DIR_OUT) |
8122                       ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
8123                       tag_action,
8124                       data_ptr,
8125                       dxfer_len,
8126                       sense_len,
8127                       cdb_len,
8128                       timeout);
8129 }
8130
8131 void
8132 scsi_write_same(struct ccb_scsiio *csio, u_int32_t retries,
8133                 void (*cbfcnp)(struct cam_periph *, union ccb *),
8134                 u_int8_t tag_action, u_int8_t byte2,
8135                 int minimum_cmd_size, u_int64_t lba, u_int32_t block_count,
8136                 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
8137                 u_int32_t timeout)
8138 {
8139         u_int8_t cdb_len;
8140         if ((minimum_cmd_size < 16) &&
8141             ((block_count & 0xffff) == block_count) &&
8142             ((lba & 0xffffffff) == lba)) {
8143                 /*
8144                  * Need a 10 byte cdb.
8145                  */
8146                 struct scsi_write_same_10 *scsi_cmd;
8147
8148                 scsi_cmd = (struct scsi_write_same_10 *)&csio->cdb_io.cdb_bytes;
8149                 scsi_cmd->opcode = WRITE_SAME_10;
8150                 scsi_cmd->byte2 = byte2;
8151                 scsi_ulto4b(lba, scsi_cmd->addr);
8152                 scsi_cmd->group = 0;
8153                 scsi_ulto2b(block_count, scsi_cmd->length);
8154                 scsi_cmd->control = 0;
8155                 cdb_len = sizeof(*scsi_cmd);
8156
8157                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8158                           ("10byte: %x%x%x%x:%x%x: %d\n", scsi_cmd->addr[0],
8159                            scsi_cmd->addr[1], scsi_cmd->addr[2],
8160                            scsi_cmd->addr[3], scsi_cmd->length[0],
8161                            scsi_cmd->length[1], dxfer_len));
8162         } else {
8163                 /*
8164                  * 16 byte CDB.  We'll only get here if the LBA is larger
8165                  * than 2^32, or if the user asks for a 16 byte command.
8166                  */
8167                 struct scsi_write_same_16 *scsi_cmd;
8168
8169                 scsi_cmd = (struct scsi_write_same_16 *)&csio->cdb_io.cdb_bytes;
8170                 scsi_cmd->opcode = WRITE_SAME_16;
8171                 scsi_cmd->byte2 = byte2;
8172                 scsi_u64to8b(lba, scsi_cmd->addr);
8173                 scsi_ulto4b(block_count, scsi_cmd->length);
8174                 scsi_cmd->group = 0;
8175                 scsi_cmd->control = 0;
8176                 cdb_len = sizeof(*scsi_cmd);
8177
8178                 CAM_DEBUG(csio->ccb_h.path, CAM_DEBUG_SUBTRACE,
8179                           ("16byte: %x%x%x%x%x%x%x%x:%x%x%x%x: %d\n",
8180                            scsi_cmd->addr[0], scsi_cmd->addr[1],
8181                            scsi_cmd->addr[2], scsi_cmd->addr[3],
8182                            scsi_cmd->addr[4], scsi_cmd->addr[5],
8183                            scsi_cmd->addr[6], scsi_cmd->addr[7],
8184                            scsi_cmd->length[0], scsi_cmd->length[1],
8185                            scsi_cmd->length[2], scsi_cmd->length[3],
8186                            dxfer_len));
8187         }
8188         cam_fill_csio(csio,
8189                       retries,
8190                       cbfcnp,
8191                       /*flags*/CAM_DIR_OUT,
8192                       tag_action,
8193                       data_ptr,
8194                       dxfer_len,
8195                       sense_len,
8196                       cdb_len,
8197                       timeout);
8198 }
8199
8200 void
8201 scsi_ata_identify(struct ccb_scsiio *csio, u_int32_t retries,
8202                   void (*cbfcnp)(struct cam_periph *, union ccb *),
8203                   u_int8_t tag_action, u_int8_t *data_ptr,
8204                   u_int16_t dxfer_len, u_int8_t sense_len,
8205                   u_int32_t timeout)
8206 {
8207         scsi_ata_pass_16(csio,
8208                          retries,
8209                          cbfcnp,
8210                          /*flags*/CAM_DIR_IN,
8211                          tag_action,
8212                          /*protocol*/AP_PROTO_PIO_IN,
8213                          /*ata_flags*/AP_FLAG_TDIR_FROM_DEV|
8214                                 AP_FLAG_BYT_BLOK_BYTES|AP_FLAG_TLEN_SECT_CNT,
8215                          /*features*/0,
8216                          /*sector_count*/dxfer_len,
8217                          /*lba*/0,
8218                          /*command*/ATA_ATA_IDENTIFY,
8219                          /*control*/0,
8220                          data_ptr,
8221                          dxfer_len,
8222                          sense_len,
8223                          timeout);
8224 }
8225
8226 void
8227 scsi_ata_trim(struct ccb_scsiio *csio, u_int32_t retries,
8228               void (*cbfcnp)(struct cam_periph *, union ccb *),
8229               u_int8_t tag_action, u_int16_t block_count,
8230               u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8231               u_int32_t timeout)
8232 {
8233         scsi_ata_pass_16(csio,
8234                          retries,
8235                          cbfcnp,
8236                          /*flags*/CAM_DIR_OUT,
8237                          tag_action,
8238                          /*protocol*/AP_EXTEND|AP_PROTO_DMA,
8239                          /*ata_flags*/AP_FLAG_TLEN_SECT_CNT|AP_FLAG_BYT_BLOK_BLOCKS,
8240                          /*features*/ATA_DSM_TRIM,
8241                          /*sector_count*/block_count,
8242                          /*lba*/0,
8243                          /*command*/ATA_DATA_SET_MANAGEMENT,
8244                          /*control*/0,
8245                          data_ptr,
8246                          dxfer_len,
8247                          sense_len,
8248                          timeout);
8249 }
8250
8251 void
8252 scsi_ata_pass_16(struct ccb_scsiio *csio, u_int32_t retries,
8253                  void (*cbfcnp)(struct cam_periph *, union ccb *),
8254                  u_int32_t flags, u_int8_t tag_action,
8255                  u_int8_t protocol, u_int8_t ata_flags, u_int16_t features,
8256                  u_int16_t sector_count, uint64_t lba, u_int8_t command,
8257                  u_int8_t control, u_int8_t *data_ptr, u_int16_t dxfer_len,
8258                  u_int8_t sense_len, u_int32_t timeout)
8259 {
8260         struct ata_pass_16 *ata_cmd;
8261
8262         ata_cmd = (struct ata_pass_16 *)&csio->cdb_io.cdb_bytes;
8263         ata_cmd->opcode = ATA_PASS_16;
8264         ata_cmd->protocol = protocol;
8265         ata_cmd->flags = ata_flags;
8266         ata_cmd->features_ext = features >> 8;
8267         ata_cmd->features = features;
8268         ata_cmd->sector_count_ext = sector_count >> 8;
8269         ata_cmd->sector_count = sector_count;
8270         ata_cmd->lba_low = lba;
8271         ata_cmd->lba_mid = lba >> 8;
8272         ata_cmd->lba_high = lba >> 16;
8273         ata_cmd->device = ATA_DEV_LBA;
8274         if (protocol & AP_EXTEND) {
8275                 ata_cmd->lba_low_ext = lba >> 24;
8276                 ata_cmd->lba_mid_ext = lba >> 32;
8277                 ata_cmd->lba_high_ext = lba >> 40;
8278         } else
8279                 ata_cmd->device |= (lba >> 24) & 0x0f;
8280         ata_cmd->command = command;
8281         ata_cmd->control = control;
8282
8283         cam_fill_csio(csio,
8284                       retries,
8285                       cbfcnp,
8286                       flags,
8287                       tag_action,
8288                       data_ptr,
8289                       dxfer_len,
8290                       sense_len,
8291                       sizeof(*ata_cmd),
8292                       timeout);
8293 }
8294
8295 void
8296 scsi_unmap(struct ccb_scsiio *csio, u_int32_t retries,
8297            void (*cbfcnp)(struct cam_periph *, union ccb *),
8298            u_int8_t tag_action, u_int8_t byte2,
8299            u_int8_t *data_ptr, u_int16_t dxfer_len, u_int8_t sense_len,
8300            u_int32_t timeout)
8301 {
8302         struct scsi_unmap *scsi_cmd;
8303
8304         scsi_cmd = (struct scsi_unmap *)&csio->cdb_io.cdb_bytes;
8305         scsi_cmd->opcode = UNMAP;
8306         scsi_cmd->byte2 = byte2;
8307         scsi_ulto4b(0, scsi_cmd->reserved);
8308         scsi_cmd->group = 0;
8309         scsi_ulto2b(dxfer_len, scsi_cmd->length);
8310         scsi_cmd->control = 0;
8311
8312         cam_fill_csio(csio,
8313                       retries,
8314                       cbfcnp,
8315                       /*flags*/CAM_DIR_OUT,
8316                       tag_action,
8317                       data_ptr,
8318                       dxfer_len,
8319                       sense_len,
8320                       sizeof(*scsi_cmd),
8321                       timeout);
8322 }
8323
8324 void
8325 scsi_receive_diagnostic_results(struct ccb_scsiio *csio, u_int32_t retries,
8326                                 void (*cbfcnp)(struct cam_periph *, union ccb*),
8327                                 uint8_t tag_action, int pcv, uint8_t page_code,
8328                                 uint8_t *data_ptr, uint16_t allocation_length,
8329                                 uint8_t sense_len, uint32_t timeout)
8330 {
8331         struct scsi_receive_diag *scsi_cmd;
8332
8333         scsi_cmd = (struct scsi_receive_diag *)&csio->cdb_io.cdb_bytes;
8334         memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8335         scsi_cmd->opcode = RECEIVE_DIAGNOSTIC;
8336         if (pcv) {
8337                 scsi_cmd->byte2 |= SRD_PCV;
8338                 scsi_cmd->page_code = page_code;
8339         }
8340         scsi_ulto2b(allocation_length, scsi_cmd->length);
8341
8342         cam_fill_csio(csio,
8343                       retries,
8344                       cbfcnp,
8345                       /*flags*/CAM_DIR_IN,
8346                       tag_action,
8347                       data_ptr,
8348                       allocation_length,
8349                       sense_len,
8350                       sizeof(*scsi_cmd),
8351                       timeout);
8352 }
8353
8354 void
8355 scsi_send_diagnostic(struct ccb_scsiio *csio, u_int32_t retries,
8356                      void (*cbfcnp)(struct cam_periph *, union ccb *),
8357                      uint8_t tag_action, int unit_offline, int device_offline,
8358                      int self_test, int page_format, int self_test_code,
8359                      uint8_t *data_ptr, uint16_t param_list_length,
8360                      uint8_t sense_len, uint32_t timeout)
8361 {
8362         struct scsi_send_diag *scsi_cmd;
8363
8364         scsi_cmd = (struct scsi_send_diag *)&csio->cdb_io.cdb_bytes;
8365         memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8366         scsi_cmd->opcode = SEND_DIAGNOSTIC;
8367
8368         /*
8369          * The default self-test mode control and specific test
8370          * control are mutually exclusive.
8371          */
8372         if (self_test)
8373                 self_test_code = SSD_SELF_TEST_CODE_NONE;
8374
8375         scsi_cmd->byte2 = ((self_test_code << SSD_SELF_TEST_CODE_SHIFT)
8376                          & SSD_SELF_TEST_CODE_MASK)
8377                         | (unit_offline   ? SSD_UNITOFFL : 0)
8378                         | (device_offline ? SSD_DEVOFFL  : 0)
8379                         | (self_test      ? SSD_SELFTEST : 0)
8380                         | (page_format    ? SSD_PF       : 0);
8381         scsi_ulto2b(param_list_length, scsi_cmd->length);
8382
8383         cam_fill_csio(csio,
8384                       retries,
8385                       cbfcnp,
8386                       /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8387                       tag_action,
8388                       data_ptr,
8389                       param_list_length,
8390                       sense_len,
8391                       sizeof(*scsi_cmd),
8392                       timeout);
8393 }
8394
8395 void
8396 scsi_read_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8397                         void (*cbfcnp)(struct cam_periph *, union ccb*),
8398                         uint8_t tag_action, int mode,
8399                         uint8_t buffer_id, u_int32_t offset,
8400                         uint8_t *data_ptr, uint32_t allocation_length,
8401                         uint8_t sense_len, uint32_t timeout)
8402 {
8403         struct scsi_read_buffer *scsi_cmd;
8404
8405         scsi_cmd = (struct scsi_read_buffer *)&csio->cdb_io.cdb_bytes;
8406         memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8407         scsi_cmd->opcode = READ_BUFFER;
8408         scsi_cmd->byte2 = mode;
8409         scsi_cmd->buffer_id = buffer_id;
8410         scsi_ulto3b(offset, scsi_cmd->offset);
8411         scsi_ulto3b(allocation_length, scsi_cmd->length);
8412
8413         cam_fill_csio(csio,
8414                       retries,
8415                       cbfcnp,
8416                       /*flags*/CAM_DIR_IN,
8417                       tag_action,
8418                       data_ptr,
8419                       allocation_length,
8420                       sense_len,
8421                       sizeof(*scsi_cmd),
8422                       timeout);
8423 }
8424
8425 void
8426 scsi_write_buffer(struct ccb_scsiio *csio, u_int32_t retries,
8427                         void (*cbfcnp)(struct cam_periph *, union ccb *),
8428                         uint8_t tag_action, int mode,
8429                         uint8_t buffer_id, u_int32_t offset,
8430                         uint8_t *data_ptr, uint32_t param_list_length,
8431                         uint8_t sense_len, uint32_t timeout)
8432 {
8433         struct scsi_write_buffer *scsi_cmd;
8434
8435         scsi_cmd = (struct scsi_write_buffer *)&csio->cdb_io.cdb_bytes;
8436         memset(scsi_cmd, 0, sizeof(*scsi_cmd));
8437         scsi_cmd->opcode = WRITE_BUFFER;
8438         scsi_cmd->byte2 = mode;
8439         scsi_cmd->buffer_id = buffer_id;
8440         scsi_ulto3b(offset, scsi_cmd->offset);
8441         scsi_ulto3b(param_list_length, scsi_cmd->length);
8442
8443         cam_fill_csio(csio,
8444                       retries,
8445                       cbfcnp,
8446                       /*flags*/param_list_length ? CAM_DIR_OUT : CAM_DIR_NONE,
8447                       tag_action,
8448                       data_ptr,
8449                       param_list_length,
8450                       sense_len,
8451                       sizeof(*scsi_cmd),
8452                       timeout);
8453 }
8454
8455 void 
8456 scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries,
8457                 void (*cbfcnp)(struct cam_periph *, union ccb *),
8458                 u_int8_t tag_action, int start, int load_eject,
8459                 int immediate, u_int8_t sense_len, u_int32_t timeout)
8460 {
8461         struct scsi_start_stop_unit *scsi_cmd;
8462         int extra_flags = 0;
8463
8464         scsi_cmd = (struct scsi_start_stop_unit *)&csio->cdb_io.cdb_bytes;
8465         bzero(scsi_cmd, sizeof(*scsi_cmd));
8466         scsi_cmd->opcode = START_STOP_UNIT;
8467         if (start != 0) {
8468                 scsi_cmd->how |= SSS_START;
8469                 /* it takes a lot of power to start a drive */
8470                 extra_flags |= CAM_HIGH_POWER;
8471         }
8472         if (load_eject != 0)
8473                 scsi_cmd->how |= SSS_LOEJ;
8474         if (immediate != 0)
8475                 scsi_cmd->byte2 |= SSS_IMMED;
8476
8477         cam_fill_csio(csio,
8478                       retries,
8479                       cbfcnp,
8480                       /*flags*/CAM_DIR_NONE | extra_flags,
8481                       tag_action,
8482                       /*data_ptr*/NULL,
8483                       /*dxfer_len*/0,
8484                       sense_len,
8485                       sizeof(*scsi_cmd),
8486                       timeout);
8487 }
8488
8489 void
8490 scsi_read_attribute(struct ccb_scsiio *csio, u_int32_t retries, 
8491                     void (*cbfcnp)(struct cam_periph *, union ccb *),
8492                     u_int8_t tag_action, u_int8_t service_action,
8493                     uint32_t element, u_int8_t elem_type, int logical_volume,
8494                     int partition, u_int32_t first_attribute, int cache,
8495                     u_int8_t *data_ptr, u_int32_t length, int sense_len,
8496                     u_int32_t timeout)
8497 {
8498         struct scsi_read_attribute *scsi_cmd;
8499
8500         scsi_cmd = (struct scsi_read_attribute *)&csio->cdb_io.cdb_bytes;
8501         bzero(scsi_cmd, sizeof(*scsi_cmd));
8502
8503         scsi_cmd->opcode = READ_ATTRIBUTE;
8504         scsi_cmd->service_action = service_action;
8505         scsi_ulto2b(element, scsi_cmd->element);
8506         scsi_cmd->elem_type = elem_type;
8507         scsi_cmd->logical_volume = logical_volume;
8508         scsi_cmd->partition = partition;
8509         scsi_ulto2b(first_attribute, scsi_cmd->first_attribute);
8510         scsi_ulto4b(length, scsi_cmd->length);
8511         if (cache != 0)
8512                 scsi_cmd->cache |= SRA_CACHE;
8513         
8514         cam_fill_csio(csio,
8515                       retries,
8516                       cbfcnp,
8517                       /*flags*/CAM_DIR_IN,
8518                       tag_action,
8519                       /*data_ptr*/data_ptr,
8520                       /*dxfer_len*/length,
8521                       sense_len,
8522                       sizeof(*scsi_cmd),
8523                       timeout);
8524 }
8525
8526 void
8527 scsi_write_attribute(struct ccb_scsiio *csio, u_int32_t retries, 
8528                     void (*cbfcnp)(struct cam_periph *, union ccb *),
8529                     u_int8_t tag_action, uint32_t element, int logical_volume,
8530                     int partition, int wtc, u_int8_t *data_ptr,
8531                     u_int32_t length, int sense_len, u_int32_t timeout)
8532 {
8533         struct scsi_write_attribute *scsi_cmd;
8534
8535         scsi_cmd = (struct scsi_write_attribute *)&csio->cdb_io.cdb_bytes;
8536         bzero(scsi_cmd, sizeof(*scsi_cmd));
8537
8538         scsi_cmd->opcode = WRITE_ATTRIBUTE;
8539         if (wtc != 0)
8540                 scsi_cmd->byte2 = SWA_WTC;
8541         scsi_ulto3b(element, scsi_cmd->element);
8542         scsi_cmd->logical_volume = logical_volume;
8543         scsi_cmd->partition = partition;
8544         scsi_ulto4b(length, scsi_cmd->length);
8545
8546         cam_fill_csio(csio,
8547                       retries,
8548                       cbfcnp,
8549                       /*flags*/CAM_DIR_OUT,
8550                       tag_action,
8551                       /*data_ptr*/data_ptr,
8552                       /*dxfer_len*/length,
8553                       sense_len,
8554                       sizeof(*scsi_cmd),
8555                       timeout);
8556 }
8557
8558 void
8559 scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries, 
8560                            void (*cbfcnp)(struct cam_periph *, union ccb *),
8561                            uint8_t tag_action, int service_action,
8562                            uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8563                            int timeout)
8564 {
8565         struct scsi_per_res_in *scsi_cmd;
8566
8567         scsi_cmd = (struct scsi_per_res_in *)&csio->cdb_io.cdb_bytes;
8568         bzero(scsi_cmd, sizeof(*scsi_cmd));
8569
8570         scsi_cmd->opcode = PERSISTENT_RES_IN;
8571         scsi_cmd->action = service_action;
8572         scsi_ulto2b(dxfer_len, scsi_cmd->length);
8573
8574         cam_fill_csio(csio,
8575                       retries,
8576                       cbfcnp,
8577                       /*flags*/CAM_DIR_IN,
8578                       tag_action,
8579                       data_ptr,
8580                       dxfer_len,
8581                       sense_len,
8582                       sizeof(*scsi_cmd),
8583                       timeout);
8584 }
8585
8586 void
8587 scsi_persistent_reserve_out(struct ccb_scsiio *csio, uint32_t retries, 
8588                             void (*cbfcnp)(struct cam_periph *, union ccb *),
8589                             uint8_t tag_action, int service_action,
8590                             int scope, int res_type, uint8_t *data_ptr,
8591                             uint32_t dxfer_len, int sense_len, int timeout)
8592 {
8593         struct scsi_per_res_out *scsi_cmd;
8594
8595         scsi_cmd = (struct scsi_per_res_out *)&csio->cdb_io.cdb_bytes;
8596         bzero(scsi_cmd, sizeof(*scsi_cmd));
8597
8598         scsi_cmd->opcode = PERSISTENT_RES_OUT;
8599         scsi_cmd->action = service_action;
8600         scsi_cmd->scope_type = scope | res_type;
8601         scsi_ulto4b(dxfer_len, scsi_cmd->length);
8602
8603         cam_fill_csio(csio,
8604                       retries,
8605                       cbfcnp,
8606                       /*flags*/CAM_DIR_OUT,
8607                       tag_action,
8608                       /*data_ptr*/data_ptr,
8609                       /*dxfer_len*/dxfer_len,
8610                       sense_len,
8611                       sizeof(*scsi_cmd),
8612                       timeout);
8613 }
8614
8615 void
8616 scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries, 
8617                           void (*cbfcnp)(struct cam_periph *, union ccb *),
8618                           uint8_t tag_action, uint32_t security_protocol,
8619                           uint32_t security_protocol_specific, int byte4,
8620                           uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8621                           int timeout)
8622 {
8623         struct scsi_security_protocol_in *scsi_cmd;
8624
8625         scsi_cmd = (struct scsi_security_protocol_in *)&csio->cdb_io.cdb_bytes;
8626         bzero(scsi_cmd, sizeof(*scsi_cmd));
8627
8628         scsi_cmd->opcode = SECURITY_PROTOCOL_IN;
8629
8630         scsi_cmd->security_protocol = security_protocol;
8631         scsi_ulto2b(security_protocol_specific,
8632                     scsi_cmd->security_protocol_specific); 
8633         scsi_cmd->byte4 = byte4;
8634         scsi_ulto4b(dxfer_len, scsi_cmd->length);
8635
8636         cam_fill_csio(csio,
8637                       retries,
8638                       cbfcnp,
8639                       /*flags*/CAM_DIR_IN,
8640                       tag_action,
8641                       data_ptr,
8642                       dxfer_len,
8643                       sense_len,
8644                       sizeof(*scsi_cmd),
8645                       timeout);
8646 }
8647
8648 void
8649 scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries, 
8650                            void (*cbfcnp)(struct cam_periph *, union ccb *),
8651                            uint8_t tag_action, uint32_t security_protocol,
8652                            uint32_t security_protocol_specific, int byte4,
8653                            uint8_t *data_ptr, uint32_t dxfer_len, int sense_len,
8654                            int timeout)
8655 {
8656         struct scsi_security_protocol_out *scsi_cmd;
8657
8658         scsi_cmd = (struct scsi_security_protocol_out *)&csio->cdb_io.cdb_bytes;
8659         bzero(scsi_cmd, sizeof(*scsi_cmd));
8660
8661         scsi_cmd->opcode = SECURITY_PROTOCOL_OUT;
8662
8663         scsi_cmd->security_protocol = security_protocol;
8664         scsi_ulto2b(security_protocol_specific,
8665                     scsi_cmd->security_protocol_specific); 
8666         scsi_cmd->byte4 = byte4;
8667         scsi_ulto4b(dxfer_len, scsi_cmd->length);
8668
8669         cam_fill_csio(csio,
8670                       retries,
8671                       cbfcnp,
8672                       /*flags*/CAM_DIR_OUT,
8673                       tag_action,
8674                       data_ptr,
8675                       dxfer_len,
8676                       sense_len,
8677                       sizeof(*scsi_cmd),
8678                       timeout);
8679 }
8680
8681 void
8682 scsi_report_supported_opcodes(struct ccb_scsiio *csio, uint32_t retries, 
8683                               void (*cbfcnp)(struct cam_periph *, union ccb *),
8684                               uint8_t tag_action, int options, int req_opcode,
8685                               int req_service_action, uint8_t *data_ptr,
8686                               uint32_t dxfer_len, int sense_len, int timeout)
8687 {
8688         struct scsi_report_supported_opcodes *scsi_cmd;
8689
8690         scsi_cmd = (struct scsi_report_supported_opcodes *)
8691             &csio->cdb_io.cdb_bytes;
8692         bzero(scsi_cmd, sizeof(*scsi_cmd));
8693
8694         scsi_cmd->opcode = MAINTENANCE_IN;
8695         scsi_cmd->service_action = REPORT_SUPPORTED_OPERATION_CODES;
8696         scsi_cmd->options = options;
8697         scsi_cmd->requested_opcode = req_opcode;
8698         scsi_ulto2b(req_service_action, scsi_cmd->requested_service_action);
8699         scsi_ulto4b(dxfer_len, scsi_cmd->length);
8700
8701         cam_fill_csio(csio,
8702                       retries,
8703                       cbfcnp,
8704                       /*flags*/CAM_DIR_IN,
8705                       tag_action,
8706                       data_ptr,
8707                       dxfer_len,
8708                       sense_len,
8709                       sizeof(*scsi_cmd),
8710                       timeout);
8711 }
8712
8713 /*      
8714  * Try make as good a match as possible with
8715  * available sub drivers
8716  */
8717 int
8718 scsi_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8719 {
8720         struct scsi_inquiry_pattern *entry;
8721         struct scsi_inquiry_data *inq;
8722  
8723         entry = (struct scsi_inquiry_pattern *)table_entry;
8724         inq = (struct scsi_inquiry_data *)inqbuffer;
8725
8726         if (((SID_TYPE(inq) == entry->type)
8727           || (entry->type == T_ANY))
8728          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8729                                    : entry->media_type & SIP_MEDIA_FIXED)
8730          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8731          && (cam_strmatch(inq->product, entry->product,
8732                           sizeof(inq->product)) == 0)
8733          && (cam_strmatch(inq->revision, entry->revision,
8734                           sizeof(inq->revision)) == 0)) {
8735                 return (0);
8736         }
8737         return (-1);
8738 }
8739
8740 /*      
8741  * Try make as good a match as possible with
8742  * available sub drivers
8743  */
8744 int
8745 scsi_static_inquiry_match(caddr_t inqbuffer, caddr_t table_entry)
8746 {
8747         struct scsi_static_inquiry_pattern *entry;
8748         struct scsi_inquiry_data *inq;
8749  
8750         entry = (struct scsi_static_inquiry_pattern *)table_entry;
8751         inq = (struct scsi_inquiry_data *)inqbuffer;
8752
8753         if (((SID_TYPE(inq) == entry->type)
8754           || (entry->type == T_ANY))
8755          && (SID_IS_REMOVABLE(inq) ? entry->media_type & SIP_MEDIA_REMOVABLE
8756                                    : entry->media_type & SIP_MEDIA_FIXED)
8757          && (cam_strmatch(inq->vendor, entry->vendor, sizeof(inq->vendor)) == 0)
8758          && (cam_strmatch(inq->product, entry->product,
8759                           sizeof(inq->product)) == 0)
8760          && (cam_strmatch(inq->revision, entry->revision,
8761                           sizeof(inq->revision)) == 0)) {
8762                 return (0);
8763         }
8764         return (-1);
8765 }
8766
8767 /**
8768  * Compare two buffers of vpd device descriptors for a match.
8769  *
8770  * \param lhs      Pointer to first buffer of descriptors to compare.
8771  * \param lhs_len  The length of the first buffer.
8772  * \param rhs      Pointer to second buffer of descriptors to compare.
8773  * \param rhs_len  The length of the second buffer.
8774  *
8775  * \return  0 on a match, -1 otherwise.
8776  *
8777  * Treat rhs and lhs as arrays of vpd device id descriptors.  Walk lhs matching
8778  * against each element in rhs until all data are exhausted or we have found
8779  * a match.
8780  */
8781 int
8782 scsi_devid_match(uint8_t *lhs, size_t lhs_len, uint8_t *rhs, size_t rhs_len)
8783 {
8784         struct scsi_vpd_id_descriptor *lhs_id;
8785         struct scsi_vpd_id_descriptor *lhs_last;
8786         struct scsi_vpd_id_descriptor *rhs_last;
8787         uint8_t *lhs_end;
8788         uint8_t *rhs_end;
8789
8790         lhs_end = lhs + lhs_len;
8791         rhs_end = rhs + rhs_len;
8792
8793         /*
8794          * rhs_last and lhs_last are the last posible position of a valid
8795          * descriptor assuming it had a zero length identifier.  We use
8796          * these variables to insure we can safely dereference the length
8797          * field in our loop termination tests.
8798          */
8799         lhs_last = (struct scsi_vpd_id_descriptor *)
8800             (lhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
8801         rhs_last = (struct scsi_vpd_id_descriptor *)
8802             (rhs_end - __offsetof(struct scsi_vpd_id_descriptor, identifier));
8803
8804         lhs_id = (struct scsi_vpd_id_descriptor *)lhs;
8805         while (lhs_id <= lhs_last
8806             && (lhs_id->identifier + lhs_id->length) <= lhs_end) {
8807                 struct scsi_vpd_id_descriptor *rhs_id;
8808
8809                 rhs_id = (struct scsi_vpd_id_descriptor *)rhs;
8810                 while (rhs_id <= rhs_last
8811                     && (rhs_id->identifier + rhs_id->length) <= rhs_end) {
8812
8813                         if ((rhs_id->id_type &
8814                              (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK)) ==
8815                             (lhs_id->id_type &
8816                              (SVPD_ID_ASSOC_MASK | SVPD_ID_TYPE_MASK))
8817                          && rhs_id->length == lhs_id->length
8818                          && memcmp(rhs_id->identifier, lhs_id->identifier,
8819                                    rhs_id->length) == 0)
8820                                 return (0);
8821
8822                         rhs_id = (struct scsi_vpd_id_descriptor *)
8823                            (rhs_id->identifier + rhs_id->length);
8824                 }
8825                 lhs_id = (struct scsi_vpd_id_descriptor *)
8826                    (lhs_id->identifier + lhs_id->length);
8827         }
8828         return (-1);
8829 }
8830
8831 #ifdef _KERNEL
8832 int
8833 scsi_vpd_supported_page(struct cam_periph *periph, uint8_t page_id)
8834 {
8835         struct cam_ed *device;
8836         struct scsi_vpd_supported_pages *vpds;
8837         int i, num_pages;
8838
8839         device = periph->path->device;
8840         vpds = (struct scsi_vpd_supported_pages *)device->supported_vpds;
8841
8842         if (vpds != NULL) {
8843                 num_pages = device->supported_vpds_len -
8844                     SVPD_SUPPORTED_PAGES_HDR_LEN;
8845                 for (i = 0; i < num_pages; i++) {
8846                         if (vpds->page_list[i] == page_id)
8847                                 return (1);
8848                 }
8849         }
8850
8851         return (0);
8852 }
8853
8854 static void
8855 init_scsi_delay(void)
8856 {
8857         int delay;
8858
8859         delay = SCSI_DELAY;
8860         TUNABLE_INT_FETCH("kern.cam.scsi_delay", &delay);
8861
8862         if (set_scsi_delay(delay) != 0) {
8863                 printf("cam: invalid value for tunable kern.cam.scsi_delay\n");
8864                 set_scsi_delay(SCSI_DELAY);
8865         }
8866 }
8867 SYSINIT(scsi_delay, SI_SUB_TUNABLES, SI_ORDER_ANY, init_scsi_delay, NULL);
8868
8869 static int
8870 sysctl_scsi_delay(SYSCTL_HANDLER_ARGS)
8871 {
8872         int error, delay;
8873
8874         delay = scsi_delay;
8875         error = sysctl_handle_int(oidp, &delay, 0, req);
8876         if (error != 0 || req->newptr == NULL)
8877                 return (error);
8878         return (set_scsi_delay(delay));
8879 }
8880 SYSCTL_PROC(_kern_cam, OID_AUTO, scsi_delay, CTLTYPE_INT|CTLFLAG_RW,
8881     0, 0, sysctl_scsi_delay, "I",
8882     "Delay to allow devices to settle after a SCSI bus reset (ms)");
8883
8884 static int
8885 set_scsi_delay(int delay)
8886 {
8887         /*
8888          * If someone sets this to 0, we assume that they want the
8889          * minimum allowable bus settle delay.
8890          */
8891         if (delay == 0) {
8892                 printf("cam: using minimum scsi_delay (%dms)\n",
8893                     SCSI_MIN_DELAY);
8894                 delay = SCSI_MIN_DELAY;
8895         }
8896         if (delay < SCSI_MIN_DELAY)
8897                 return (EINVAL);
8898         scsi_delay = delay;
8899         return (0);
8900 }
8901 #endif /* _KERNEL */